Restore evil-visual-state-map on edebug exit

problem:
Exiting edebug-mode, leaves the evil-visual-state-map
with only the two key bindings that are defined in:
evilified-state--setup-visual-state-keymap
y       evil-yank
escape  evil-exit-visual-state

solution:
Restore the evil-visual-state-map when exiting edebug-mode.

Added an alias for the new exit function:
evilified-state--evilified-state-on-exit

called: evil-evilified-state-exit
the opposite of: evil-evilified-state
This commit is contained in:
duianto 2021-02-18 21:16:15 +01:00 committed by Maximilian Wolff
parent 0220101c7f
commit f4999e8242
2 changed files with 29 additions and 8 deletions

View File

@ -53,6 +53,10 @@
"Local backup of normal state keymap.")
(make-variable-buffer-local 'evilified-state--normal-state-map)
(defvar evilified-state--visual-state-map nil
"Local backup of visual state keymap.")
(make-variable-buffer-local 'evilified-state--visual-state-map)
(evil-define-state evilified
"Evilified state.
Hybrid `emacs state' with carefully selected Vim key bindings.
@ -101,7 +105,9 @@ Needed to bypass keymaps set as text properties."
(defun evilified-state--restore-normal-state-keymap ()
"Restore the normal state keymap."
(setq-local evil-normal-state-map evilified-state--normal-state-map))
(setq-local evil-normal-state-map evilified-state--normal-state-map)
(define-key evil-normal-state-map [escape] 'evil-force-normal-state)
(evil-normal-state))
(defun evilified-state--clear-normal-state-keymap ()
"Clear the normal state keymap."
@ -109,11 +115,18 @@ Needed to bypass keymaps set as text properties."
(evil-normalize-keymaps))
(defun evilified-state--setup-visual-state-keymap ()
"Setup the normal state keymap."
"Setup the visual state keymap."
(unless evilified-state--visual-state-map
(setq-local evilified-state--visual-state-map
(copy-keymap evil-visual-state-map)))
(setq-local evil-visual-state-map
(cons 'keymap (list (cons ?y 'evil-yank)
(cons 'escape 'evil-exit-visual-state)))))
(defun evilified-state--restore-visual-state-keymap ()
"Restore the visual state keymap."
(setq-local evil-visual-state-map evilified-state--visual-state-map))
(defun evilified-state--evilified-state-on-entry ()
"Setup evilified state."
(when (derived-mode-p 'magit-mode)
@ -132,6 +145,18 @@ Needed to bypass keymaps set as text properties."
(add-hook 'evil-visual-state-exit-hook
'evilified-state--visual-state-on-exit nil 'local))
(defun evilified-state--evilified-state-on-exit ()
"Restore evil normal and visual states."
(evilified-state--restore-normal-state-keymap)
(evilified-state--restore-visual-state-keymap)
(remove-hook 'pre-command-hook 'evilified-state--pre-command-hook 'local)
(remove-hook 'evil-visual-state-entry-hook
'evilified-state--visual-state-on-entry 'local)
(remove-hook 'evil-visual-state-exit-hook
'evilified-state--visual-state-on-exit 'local))
(defalias 'evil-evilified-state-exit 'evilified-state--evilified-state-on-exit)
(defun evilified-state--visual-state-on-entry ()
"Setup visual state."
;; we need to clear temporarily the normal state keymap in order to reach
@ -141,7 +166,7 @@ Needed to bypass keymaps set as text properties."
(defun evilified-state--visual-state-on-exit ()
"Clean visual state"
(evilified-state--restore-normal-state-keymap))
(evilified-state--restore-visual-state-keymap))
(add-hook 'evil-evilified-state-entry-hook
'evilified-state--evilified-state-on-entry)

View File

@ -65,13 +65,9 @@ Unlike `eval-defun', this does not go to topmost function."
hybrid-style-enable-evilified-state))))
(if (not edebug-mode)
;; disable edebug-mode
(when evilified?
(remove-hook 'pre-command-hook 'evilified-state--pre-command-hook)
(define-key evil-normal-state-map [escape] 'evil-force-normal-state)
(evil-normal-state))
(when evilified? (evil-evilified-state-exit))
;; enable edebug-mode
(when evilified? (evil-evilified-state))
(evil-normalize-keymaps)
(when (and (fboundp 'golden-ratio-mode)
golden-ratio-mode)
(golden-ratio)))))