New hybrid mode variable hybrid-mode-enable-evilified-state

When non nil then evilified buffer use evilified state, otherwise
they use the emacs state (may put the hybrid state if users get
confused).
This commit is contained in:
syl20bnr 2016-03-16 23:34:20 -04:00
parent 2d61e5304e
commit ca5b8be90b
2 changed files with 59 additions and 17 deletions

View File

@ -72,7 +72,7 @@ The `insert state' is replaced by the `emacs state'."
;; key bindings hooks for dynamic switching of editing styles
(run-hook-with-args 'spacemacs-editing-style-hook 'emacs)
;; initiate `emacs state' and enter the church
(holy-mode//update-states-for-current-buffers))
(holy-mode//update-states-for-current-buffers 'emacs))
(defun amen ()
"May the force be with you my son (or not)."
@ -88,19 +88,17 @@ The `insert state' is replaced by the `emacs state'."
;; restore key bindings
(run-hook-with-args 'spacemacs-editing-style-hook 'vim)
;; restore the states
(holy-mode//update-states-for-current-buffers t))
(holy-mode//update-states-for-current-buffers 'vim))
(defun holy-mode//update-states-for-current-buffers (&optional arg)
"Update the active state in all current buffers.
ARG non nil means that the editing style is `vim'."
(defun holy-mode//update-states-for-current-buffers (style)
"Update the active state in all current buffers given current STYLE."
(dolist (buffer (buffer-list))
(with-current-buffer buffer
;; switch to holy-mode
(when (not arg)
(evil-emacs-state))
;; disable holy-mode
(when (and arg (eq 'emacs evil-state))
(cond
((eq 'emacs style) (evil-emacs-state))
((and (eq 'vim style)
(eq 'emacs evil-state))
(cond
((memq major-mode evil-evilified-state-modes) (evil-evilified-state))
((memq major-mode evil-motion-state-modes) (evil-motion-state))
(t (evil-normal-state)))))))
(t (evil-normal-state))))))))

View File

@ -45,6 +45,11 @@ key bindings for hjkl navigation."
:group 'spacemacs
:type 'boolean)
(defcustom hybrid-mode-enable-evilified-state nil
"If non nil then evilified states is enabled in buffer supporting it."
:group 'spacemacs
:type 'boolean)
(defvar hybrid-mode-default-state-backup evil-default-state
"Backup of `evil-default-state'.")
@ -52,6 +57,16 @@ key bindings for hjkl navigation."
"Forces Hybrid state."
(evil-hybrid-state))
(defadvice evil-evilified-state (around hybrid-evilified-to-hybrid-state disable)
"Forces Hybrid state."
(if (equal -1 (ad-get-arg 0))
ad-do-it
(if hybrid-mode-enable-evilified-state
ad-do-it
;; seems better to set the emacs state instead of hybrid for evilified
;; buffers
(evil-emacs-state))))
;;;###autoload
(define-minor-mode hybrid-mode
"Global minor mode to replace insert state by hybrid state."
@ -66,20 +81,32 @@ key bindings for hjkl navigation."
"Enable the hybrid editing style."
(setq hybrid-mode-default-state-backup evil-default-state
evil-default-state hybrid-mode-default-state)
;; key bindings hooks for dynamic switching of editing styles
(run-hook-with-args 'spacemacs-editing-style-hook 'hybrid)
;; replace evil states by `hybrid state'
(ad-enable-advice 'evil-insert-state
'around 'hybrid-insert-to-hybrid-state)
(ad-activate 'evil-insert-state))
(ad-enable-advice 'evil-evilified-state
'around 'hybrid-evilified-to-hybrid-state)
(ad-activate 'evil-insert-state)
(ad-activate 'evil-evilified-state)
;; key bindings hooks for dynamic switching of editing styles
(run-hook-with-args 'spacemacs-editing-style-hook 'hybrid)
;; initiate `hybrid state'
(hybrid-mode//update-states-for-current-buffers 'hybrid))
(defun disable-hybrid-editing-style ()
"Disable the hybrid editing style (reverting to 'vim style)."
(setq evil-default-state hybrid-mode-default-state-backup)
;; restore key bindings
(run-hook-with-args 'spacemacs-editing-style-hook 'vim)
;; restore evil states
(ad-disable-advice 'evil-insert-state
'around 'hybrid-insert-to-hybrid-state)
(ad-activate 'evil-insert-state))
(ad-disable-advice 'evil-evilified-state
'around 'hybrid-evilified-to-hybrid-state)
(ad-activate 'evil-insert-state)
(ad-activate 'evil-evilified-state)
;; restore key bindings
(run-hook-with-args 'spacemacs-editing-style-hook 'vim)
;; restore the states
(hybrid-mode//update-states-for-current-buffers 'vim))
;; This code is from evil insert state definition, any change upstream
;; should be reflected here
@ -116,4 +143,21 @@ key bindings for hjkl navigation."
(and evil-local-mode
(memq (or state evil-state) '(insert hybrid))))
(defun hybrid-mode//update-states-for-current-buffers (style)
"Update the active state in all current buffers given current STYLE."
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(cond
((eq 'hybrid style)
(if (memq major-mode evil-evilified-state-modes)
(evil-evilified-state)
(funcall (intern (format "evil-%S-state"
hybrid-mode-default-state)))))
((and (eq 'vim style)
(memq evil-state '(hybrid emacs)))
(cond
((memq major-mode evil-evilified-state-modes) (evil-evilified-state))
((memq major-mode evil-motion-state-modes) (evil-motion-state))
(t (evil-normal-state))))))))
(provide 'hybrid-mode)