Generalize +evil-esc-hook into doom-escape-hook

This lets vanilla Emacs users in on having a universal "escape" key.
This commit is contained in:
Henrik Lissner 2018-01-06 02:38:34 -05:00
parent c8768f512d
commit 3cbddbfd88
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
8 changed files with 34 additions and 28 deletions

View file

@ -21,6 +21,30 @@
(?g . global)) (?g . global))
"A list of cons cells that map a letter to a evil state symbol.") "A list of cons cells that map a letter to a evil state symbol.")
;;
(defvar doom-escape-hook nil
"A hook run after C-g is pressed (or ESC in normal mode, for evil users). Both
keys trigger `doom/escape'.
If any hook returns non-nil, all hooks after it are ignored.")
(defun doom/escape ()
"Run the `doom-escape-hook'."
(interactive)
(cond ((minibuffer-window-active-p (minibuffer-window))
;; quit the minibuffer if open.
(abort-recursive-edit))
((and (featurep 'evil) (evil-ex-hl-active-p 'evil-ex-search))
;; disable ex search buffer highlights.
(evil-ex-nohighlight))
;; Run all escape hooks. If any returns non-nil, then stop there.
((run-hook-with-args-until-success 'doom-escape-hook))
;; Back to the default
(t (keyboard-quit))))
(global-set-key [remap keyboard-quit] #'doom/escape)
(advice-add #'evil-force-normal-state :after #'doom/escape)
;; ;;
(def-package! which-key (def-package! which-key

View file

@ -88,24 +88,6 @@
;; --- evil hacks ------------------------- ;; --- evil hacks -------------------------
(defvar +evil-esc-hook '(t)
"A hook run after ESC is pressed in normal mode (invoked by
`evil-force-normal-state'). If any hook returns non-nil, all hooks after it are
ignored.")
(defun +evil*attach-escape-hook ()
"Run the `+evil-esc-hook'."
(cond ((minibuffer-window-active-p (minibuffer-window))
;; quit the minibuffer if open.
(abort-recursive-edit))
((evil-ex-hl-active-p 'evil-ex-search)
;; disable ex search buffer highlights.
(evil-ex-nohighlight))
(t
;; Run all escape hooks. If any returns non-nil, then stop there.
(run-hook-with-args-until-success '+evil-esc-hook))))
(advice-add #'evil-force-normal-state :after #'+evil*attach-escape-hook)
(defun +evil*restore-normal-state-on-windmove (orig-fn &rest args) (defun +evil*restore-normal-state-on-windmove (orig-fn &rest args)
"If in anything but normal or motion mode when moving to another window, "If in anything but normal or motion mode when moving to another window,
restore normal mode. This prevents insert state from bleeding into other modes restore normal mode. This prevents insert state from bleeding into other modes
@ -246,7 +228,7 @@ across windows."
(when evil-exchange--overlays (when evil-exchange--overlays
(evil-exchange-cancel) (evil-exchange-cancel)
t)) t))
(add-hook '+evil-esc-hook #'+evil|escape-exchange)) (add-hook 'doom-escape-hook #'+evil|escape-exchange))
(def-package! evil-matchit (def-package! evil-matchit
@ -309,7 +291,7 @@ the new algorithm is confusing, like in python or ruby."
(evil-mc-undo-all-cursors) (evil-mc-undo-all-cursors)
(evil-mc-resume-cursors) (evil-mc-resume-cursors)
t)) t))
(add-hook '+evil-esc-hook #'+evil|escape-multiple-cursors)) (add-hook 'doom-escape-hook #'+evil|escape-multiple-cursors))
(def-package! evil-snipe (def-package! evil-snipe

View file

@ -139,14 +139,14 @@ current buffer."
:keymap +popup-mode-map :keymap +popup-mode-map
(cond (+popup-mode (cond (+popup-mode
(add-hook 'doom-unreal-buffer-functions #'+popup-p) (add-hook 'doom-unreal-buffer-functions #'+popup-p)
(add-hook '+evil-esc-hook #'+popup|close-on-escape t) (add-hook 'doom-escape-hook #'+popup|close-on-escape t)
(setq +popup--old-display-buffer-alist display-buffer-alist (setq +popup--old-display-buffer-alist display-buffer-alist
display-buffer-alist +popup--display-buffer-alist) display-buffer-alist +popup--display-buffer-alist)
(dolist (prop +popup-window-parameters) (dolist (prop +popup-window-parameters)
(push (cons prop 'writeable) window-persistent-parameters))) (push (cons prop 'writeable) window-persistent-parameters)))
(t (t
(remove-hook 'doom-unreal-buffer-functions #'+popup-p) (remove-hook 'doom-unreal-buffer-functions #'+popup-p)
(remove-hook '+evil-esc-hook #'+popup|close-on-escape) (remove-hook 'doom-escape-hook #'+popup|close-on-escape)
(setq display-buffer-alist +popup--old-display-buffer-alist) (setq display-buffer-alist +popup--old-display-buffer-alist)
(dolist (prop +popup-window-parameters) (dolist (prop +popup-window-parameters)
(assq-delete-all prop window-persistent-parameters))))) (assq-delete-all prop window-persistent-parameters)))))

View file

@ -37,7 +37,7 @@
(advice-add #'yas-expand :before #'sp-remove-active-pair-overlay) (advice-add #'yas-expand :before #'sp-remove-active-pair-overlay)
;; Exit snippets on ESC from normal mode ;; Exit snippets on ESC from normal mode
(add-hook '+evil-esc-hook #'yas-abort-snippet)) (add-hook 'doom-escape-hook #'yas-abort-snippet))
(def-package! auto-yasnippet (def-package! auto-yasnippet

View file

@ -12,7 +12,7 @@
(when flycheck-mode (when flycheck-mode
(ignore-errors (flycheck-buffer)) (ignore-errors (flycheck-buffer))
nil)) nil))
(add-hook '+evil-esc-hook #'+syntax-checkers|flycheck-buffer t) (add-hook 'doom-escape-hook #'+syntax-checkers|flycheck-buffer t)
;; With the option of flychecking the buffer on escape, so we don't need ;; With the option of flychecking the buffer on escape, so we don't need
;; auto-flychecking on idle-change: ;; auto-flychecking on idle-change:

View file

@ -32,10 +32,10 @@
(after! evil (after! evil
(defun +version-control|update-git-gutter () (defun +version-control|update-git-gutter ()
"Refresh git-gutter on ESC. Return nil to prevent shadowing other "Refresh git-gutter on ESC. Return nil to prevent shadowing other
`+evil-esc-hook' hooks." `doom-escape-hook' hooks."
(when git-gutter-mode (when git-gutter-mode
(ignore (git-gutter)))) (ignore (git-gutter))))
(add-hook '+evil-esc-hook #'+version-control|update-git-gutter t)) (add-hook 'doom-escape-hook #'+version-control|update-git-gutter t))
(def-hydra! +version-control@git-gutter (def-hydra! +version-control@git-gutter
(:body-pre (git-gutter-mode 1) :hint nil) (:body-pre (git-gutter-mode 1) :hint nil)

View file

@ -256,7 +256,7 @@ between the two."
(when (and (derived-mode-p 'org-mode) (when (and (derived-mode-p 'org-mode)
org-occur-highlights) org-occur-highlights)
(org-remove-occur-highlights))) (org-remove-occur-highlights)))
(add-hook '+evil-esc-hook #'+org|remove-occur-highlights) (add-hook 'doom-escape-hook #'+org|remove-occur-highlights)
(after! recentf (after! recentf
;; Don't clobber recentf with agenda files ;; Don't clobber recentf with agenda files

View file

@ -42,7 +42,7 @@
anzu--last-isearch-string anzu--overflow-p)) anzu--last-isearch-string anzu--overflow-p))
;; Ensure anzu state is cleared when searches & iedit are done ;; Ensure anzu state is cleared when searches & iedit are done
(add-hook 'isearch-mode-end-hook #'anzu--reset-status t) (add-hook 'isearch-mode-end-hook #'anzu--reset-status t)
(add-hook '+evil-esc-hook #'anzu--reset-status t) (add-hook 'doom-escape-hook #'anzu--reset-status t)
(add-hook 'iedit-mode-end-hook #'anzu--reset-status)) (add-hook 'iedit-mode-end-hook #'anzu--reset-status))