2015-09-11 08:38:37 +00:00
|
|
|
;;; hybrid-mode.el --- Put one foot in the church of Emacs
|
|
|
|
|
2018-06-08 05:52:02 +00:00
|
|
|
;; Copyright (C) 2012-2018 Sylvain Benner & Contributors
|
2015-09-11 08:38:37 +00:00
|
|
|
;;
|
2016-01-12 02:40:54 +00:00
|
|
|
;; Authors: Justin Burkett <justin@burkett.cc>
|
|
|
|
;; Chris Ewald <chrisewald@gmail.com>
|
2015-09-11 08:38:37 +00:00
|
|
|
;; Keywords: convenience editing
|
|
|
|
;; Created: 12 Aug 2015
|
|
|
|
;; Version: 1.00
|
|
|
|
;; Package-Requires: ((emacs "24") (evil "1.0.9"))
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'evil)
|
|
|
|
|
2016-03-17 21:43:10 +00:00
|
|
|
(defcustom hybrid-mode-default-state 'normal
|
2016-03-17 03:40:09 +00:00
|
|
|
"Value of `evil-default-state' for hybrid-mode."
|
2015-12-14 19:10:18 +00:00
|
|
|
:group 'spacemacs
|
|
|
|
:type 'symbol)
|
2018-06-08 05:52:02 +00:00
|
|
|
(defvaralias 'hybrid-style-default-state 'hybrid-mode-default-state)
|
2015-12-14 19:10:18 +00:00
|
|
|
|
2016-03-17 02:43:50 +00:00
|
|
|
(defcustom hybrid-mode-enable-hjkl-bindings nil
|
2017-02-04 00:28:52 +00:00
|
|
|
"If non-nil then packages configuration should enable hjkl navigation."
|
2016-03-17 02:43:50 +00:00
|
|
|
:group 'spacemacs
|
|
|
|
:type 'boolean)
|
2018-06-08 05:52:02 +00:00
|
|
|
(defvaralias 'hybrid-style-enable-hjkl-bindings
|
|
|
|
'hybrid-mode-enable-hjkl-bindings)
|
2016-03-17 02:43:50 +00:00
|
|
|
|
2016-04-01 09:46:27 +00:00
|
|
|
(defcustom hybrid-mode-enable-evilified-state t
|
2017-02-04 00:28:52 +00:00
|
|
|
"If non-nil then evilified states is enabled in buffer supporting it."
|
2016-03-17 03:34:20 +00:00
|
|
|
:group 'spacemacs
|
|
|
|
:type 'boolean)
|
2018-06-08 05:52:02 +00:00
|
|
|
(defvaralias 'hybrid-style-enable-evilified-state
|
|
|
|
'hybrid-mode-enable-evilified-state)
|
2016-03-17 03:34:20 +00:00
|
|
|
|
2017-02-04 00:28:52 +00:00
|
|
|
(defcustom hybrid-mode-use-evil-search-module nil
|
|
|
|
"If non-nil then use evil own search module which is closer to Vim search
|
|
|
|
behavior (for instance it support C-r pasting)."
|
|
|
|
:group 'spacemacs
|
|
|
|
:type 'boolean)
|
2018-06-08 05:52:02 +00:00
|
|
|
(defvaralias 'hybrid-style-use-evil-search-module
|
|
|
|
'hybrid-mode-use-evil-search-module)
|
2017-02-04 00:28:52 +00:00
|
|
|
|
2016-03-17 02:43:50 +00:00
|
|
|
(defvar hybrid-mode-default-state-backup evil-default-state
|
|
|
|
"Backup of `evil-default-state'.")
|
|
|
|
|
2016-03-17 01:30:03 +00:00
|
|
|
(defadvice evil-insert-state (around hybrid-insert-to-hybrid-state disable)
|
|
|
|
"Forces Hybrid state."
|
|
|
|
(evil-hybrid-state))
|
2015-09-25 20:07:21 +00:00
|
|
|
|
2016-03-17 03:34:20 +00:00
|
|
|
(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))))
|
|
|
|
|
2015-09-11 08:38:37 +00:00
|
|
|
;;;###autoload
|
|
|
|
(define-minor-mode hybrid-mode
|
2016-03-17 01:30:03 +00:00
|
|
|
"Global minor mode to replace insert state by hybrid state."
|
2015-09-11 08:38:37 +00:00
|
|
|
:global t
|
2015-09-12 03:17:25 +00:00
|
|
|
:lighter " hybrid"
|
2015-09-11 08:38:37 +00:00
|
|
|
:group 'spacemacs
|
|
|
|
(if hybrid-mode
|
2016-03-17 01:30:03 +00:00
|
|
|
(enable-hybrid-editing-style)
|
2016-03-17 01:59:44 +00:00
|
|
|
(disable-hybrid-editing-style)))
|
2016-03-17 01:30:03 +00:00
|
|
|
|
|
|
|
(defun enable-hybrid-editing-style ()
|
|
|
|
"Enable the hybrid editing style."
|
|
|
|
(setq hybrid-mode-default-state-backup evil-default-state
|
|
|
|
evil-default-state hybrid-mode-default-state)
|
2016-03-17 03:34:20 +00:00
|
|
|
;; replace evil states by `hybrid state'
|
2016-03-17 01:30:03 +00:00
|
|
|
(ad-enable-advice 'evil-insert-state
|
|
|
|
'around 'hybrid-insert-to-hybrid-state)
|
2016-03-17 03:34:20 +00:00
|
|
|
(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))
|
2016-03-17 01:30:03 +00:00
|
|
|
|
|
|
|
(defun disable-hybrid-editing-style ()
|
|
|
|
"Disable the hybrid editing style (reverting to 'vim style)."
|
|
|
|
(setq evil-default-state hybrid-mode-default-state-backup)
|
2016-03-17 03:34:20 +00:00
|
|
|
;; restore evil states
|
2016-03-17 01:30:03 +00:00
|
|
|
(ad-disable-advice 'evil-insert-state
|
|
|
|
'around 'hybrid-insert-to-hybrid-state)
|
2016-03-17 03:34:20 +00:00
|
|
|
(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))
|
2016-03-17 01:30:03 +00:00
|
|
|
|
|
|
|
;; This code is from evil insert state definition, any change upstream
|
|
|
|
;; should be reflected here
|
2017-02-02 08:54:33 +00:00
|
|
|
;; see https://github.com/emacs-evil/evil/blob/56e92f7cb4e04e665670460093b41f58446b7a2b/evil-states.el#L108
|
2016-03-17 01:30:03 +00:00
|
|
|
(evil-define-state hybrid
|
|
|
|
"Hybrid state for hybrid mode."
|
|
|
|
:tag " <H> "
|
|
|
|
:cursor (bar . 2)
|
|
|
|
:message "-- HYBRID --"
|
|
|
|
:entry-hook (evil-start-track-last-insertion)
|
|
|
|
:exit-hook (evil-cleanup-insert-state evil-stop-track-last-insertion)
|
|
|
|
:input-method t
|
|
|
|
(cond
|
|
|
|
((evil-hybrid-state-p)
|
2016-10-02 13:03:16 +00:00
|
|
|
(add-hook 'post-command-hook #'evil-maybe-remove-spaces)
|
2016-03-17 01:30:03 +00:00
|
|
|
(add-hook 'pre-command-hook #'evil-insert-repeat-hook)
|
2016-10-02 13:03:16 +00:00
|
|
|
(setq evil-maybe-remove-spaces t)
|
2016-03-17 01:30:03 +00:00
|
|
|
(unless (eq evil-want-fine-undo t)
|
2016-06-09 02:34:23 +00:00
|
|
|
(evil-start-undo-step)))
|
2016-03-17 01:30:03 +00:00
|
|
|
(t
|
2016-10-02 13:03:16 +00:00
|
|
|
(remove-hook 'post-command-hook #'evil-maybe-remove-spaces)
|
2016-03-17 01:30:03 +00:00
|
|
|
(remove-hook 'pre-command-hook #'evil-insert-repeat-hook)
|
2016-10-02 13:03:16 +00:00
|
|
|
(evil-maybe-remove-spaces t)
|
2016-03-17 01:30:03 +00:00
|
|
|
(setq evil-insert-repeat-info evil-repeat-info)
|
|
|
|
(evil-set-marker ?^ nil t)
|
|
|
|
(unless (eq evil-want-fine-undo t)
|
2016-06-09 02:34:23 +00:00
|
|
|
(evil-end-undo-step))
|
2016-03-17 01:30:03 +00:00
|
|
|
(when evil-move-cursor-back
|
|
|
|
(when (or (evil-normal-state-p evil-next-state)
|
|
|
|
(evil-motion-state-p evil-next-state))
|
|
|
|
(evil-move-cursor-back))))))
|
|
|
|
|
|
|
|
(define-key evil-hybrid-state-map [escape] 'evil-normal-state)
|
|
|
|
|
|
|
|
;; Override stock evil function `evil-insert-state-p'
|
|
|
|
(defun evil-insert-state-p (&optional state)
|
|
|
|
"Whether the current state is insert."
|
|
|
|
(and evil-local-mode
|
|
|
|
(memq (or state evil-state) '(insert hybrid))))
|
2015-09-11 08:38:37 +00:00
|
|
|
|
2016-03-17 03:34:20 +00:00
|
|
|
(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))))))))
|
|
|
|
|
2015-09-11 08:38:37 +00:00
|
|
|
(provide 'hybrid-mode)
|