Refactor +evil-esc-hook

This commit is contained in:
Henrik Lissner 2017-05-25 12:22:05 +02:00
parent 5d47be15ef
commit 85751c519b
5 changed files with 36 additions and 37 deletions

View file

@ -296,21 +296,14 @@ properties."
(define-key map [remap evil-window-vsplit] #'ignore)) (define-key map [remap evil-window-vsplit] #'ignore))
(defun doom|popup-close-maybe () (defun doom|popup-close-maybe ()
"Close the current window if it's a popup with no :noesc property." "If current window is a popup, close it. If minibuffer is open, close it. If
(when (and (doom-popup-p) not in a popup, close all popups with an :autoclose property."
(not (doom-popup-prop :noesc))) (cond ((doom-popup-p)
(delete-window))) (unless (doom-popup-prop :noesc)
(add-hook '+evil-esc-hook #'doom|popup-close-maybe) (delete-window)))
(t
(defun doom|popup-close-all-maybe () (doom/popup-close-all))))
"Close popups with an :autoclose property when pressing ESC from normal (add-hook '+evil-esc-hook #'doom|popup-close-maybe t)
mode in any evil-mode buffer."
(unless (or (doom-popup-p)
(minibuffer-window-active-p (minibuffer-window))
(and (bound-and-true-p evil-mode)
(evil-ex-hl-active-p 'evil-ex-search)))
(doom/popup-close-all)))
(add-hook '+evil-esc-hook #'doom|popup-close-all-maybe)
;; Make evil-mode cooperate with popups ;; Make evil-mode cooperate with popups
(advice-add #'evil-command-window :override #'doom*popup-evil-command-window) (advice-add #'evil-command-window :override #'doom*popup-evil-command-window)

View file

@ -93,27 +93,24 @@
;; --- evil hacks ------------------------- ;; --- evil hacks -------------------------
(defvar +evil-esc-hook nil (defvar +evil-esc-hook '(t)
"A hook run after ESC is pressed in normal mode (invoked by "A hook run after ESC is pressed in normal mode (invoked by
`evil-force-normal-state').") `evil-force-normal-state'). If a hook returns non-nil, all hooks after it are
ignored.")
(defun +evil*attach-escape-hook () (defun +evil*attach-escape-hook ()
"Run the `+evil-esc-hook'." "Run the `+evil-esc-hook'."
(run-hooks '+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) (advice-add #'evil-force-normal-state :after #'+evil*attach-escape-hook)
(defun +evil|escape-minibuffer ()
"Quit the minibuffer if open."
(when (minibuffer-window-active-p (minibuffer-window))
(abort-recursive-edit)))
(defun +evil|escape-highlights ()
"Disable ex search buffer highlights."
(when (evil-ex-hl-active-p 'evil-ex-search)
(evil-ex-nohighlight)))
(add-hook! '+evil-esc-hook '(+evil|escape-minibuffer +evil|escape-highlights))
(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
@ -292,7 +289,9 @@ across windows."
:commands evil-exchange :commands evil-exchange
:config :config
(defun +evil|escape-exchange () (defun +evil|escape-exchange ()
(if evil-exchange--overlays (evil-exchange-cancel))) (when evil-exchange--overlays
(evil-exchange-cancel)
t))
(add-hook '+evil-esc-hook #'+evil|escape-exchange)) (add-hook '+evil-esc-hook #'+evil|escape-exchange))
@ -348,7 +347,8 @@ the new algorithm is confusing, like in python or ruby."
(defun +evil|escape-multiple-cursors () (defun +evil|escape-multiple-cursors ()
"Undo cursors and freeze them again (for next time)." "Undo cursors and freeze them again (for next time)."
(when (evil-mc-has-cursors-p) (when (evil-mc-has-cursors-p)
(evil-mc-undo-all-cursors))) (evil-mc-undo-all-cursors)
t))
(add-hook '+evil-esc-hook #'+evil|escape-multiple-cursors) (add-hook '+evil-esc-hook #'+evil|escape-multiple-cursors)
;; disable evil-escape in evil-mc ;; disable evil-escape in evil-mc

View file

@ -22,8 +22,10 @@
(after! evil (after! evil
;; Flycheck buffer on ESC in normal mode. ;; Flycheck buffer on ESC in normal mode.
(defun +syntax-checkers|flycheck-buffer () (defun +syntax-checkers|flycheck-buffer ()
(if flycheck-mode (ignore-errors (flycheck-buffer)))) (when flycheck-mode
(add-hook '+evil-esc-hook #'+syntax-checkers|flycheck-buffer))) (ignore-errors (flycheck-buffer))
nil))
(add-hook '+evil-esc-hook #'+syntax-checkers|flycheck-buffer t)))
(def-package! flycheck-pos-tip (def-package! flycheck-pos-tip

View file

@ -25,8 +25,11 @@
(add-hook 'focus-in-hook #'git-gutter:update-all-windows) (add-hook 'focus-in-hook #'git-gutter:update-all-windows)
(after! evil (after! evil
;; Refresh git-gutter on ESC (defun +version-control|update-git-gutter ()
(add-hook '+evil-esc-hook #'git-gutter))) "Refresh git-gutter on ESC. Return nil to prevent shadowing other
`+evil-esc-hook' hooks."
(ignore (git-gutter)))
(add-hook '+evil-esc-hook #'+version-control|update-git-gutter t)))
(def-package! git-timemachine (def-package! git-timemachine

View file

@ -381,7 +381,8 @@
;; Remove highlights on ESC ;; Remove highlights on ESC
(defun +org|remove-occur-highlights (&rest args) (defun +org|remove-occur-highlights (&rest args)
(when (derived-mode-p 'org-mode) (when (derived-mode-p 'org-mode)
(org-remove-occur-highlights))) (org-remove-occur-highlights)
t))
(add-hook '+evil-esc-hook #'+org|remove-occur-highlights) (add-hook '+evil-esc-hook #'+org|remove-occur-highlights)
(after! org-bullets (after! org-bullets