2015-11-19 05:24:27 +00:00
|
|
|
;;; evil-evilified-state.el --- A minimalistic evil state
|
2016-01-12 02:40:54 +00:00
|
|
|
;;
|
|
|
|
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
|
2015-06-26 03:49:53 +00:00
|
|
|
;;
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
2015-08-12 05:31:44 +00:00
|
|
|
;; Keywords: convenience editing evil spacemacs
|
|
|
|
;; Created: 22 Mar 2015
|
|
|
|
;; Version: 1.0
|
2015-11-05 13:09:00 +00:00
|
|
|
;; Package-Requires: ((evil "1.0.9"))
|
2015-08-12 05:31:44 +00:00
|
|
|
|
2015-06-26 03:49:53 +00:00
|
|
|
;; This file is not part of GNU Emacs.
|
2015-08-12 05:31:44 +00:00
|
|
|
|
|
|
|
;; 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:
|
|
|
|
|
|
|
|
;; Define a `evilified' evil state inheriting from `emacs' state and
|
|
|
|
;; setting a minimalist list of Vim key bindings (like navigation, search, ...)
|
|
|
|
|
2015-11-19 05:24:27 +00:00
|
|
|
;; The shadowed original mode key bindings are automatically reassigned
|
|
|
|
;; following a set of rules:
|
|
|
|
|
2015-08-12 05:31:44 +00:00
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'evil)
|
2016-01-08 19:07:00 +00:00
|
|
|
(require 'bind-map)
|
2015-08-12 05:31:44 +00:00
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defvar evilified-state--modes nil
|
2015-08-12 05:31:44 +00:00
|
|
|
"List of all evilified modes.")
|
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defvar evilified-state--evil-surround nil
|
2015-08-12 05:31:44 +00:00
|
|
|
"Evil surround mode variable backup.")
|
2015-11-05 13:09:00 +00:00
|
|
|
(make-variable-buffer-local 'evilified-state--evil-surround)
|
2015-08-12 05:31:44 +00:00
|
|
|
|
2015-12-14 05:00:25 +00:00
|
|
|
(defvar evilified-state--normal-state-map nil
|
|
|
|
"Local backup of normal state keymap.")
|
|
|
|
(make-variable-buffer-local 'evilified-state--normal-state-map)
|
|
|
|
|
2015-08-12 05:31:44 +00:00
|
|
|
(evil-define-state evilified
|
|
|
|
"Evilified state.
|
|
|
|
Hybrid `emacs state' with carrefully selected Vim key bindings.
|
|
|
|
See spacemacs conventions for more info."
|
2015-12-06 06:06:41 +00:00
|
|
|
:tag " <N'> "
|
2015-08-12 05:31:44 +00:00
|
|
|
:enable (emacs)
|
|
|
|
:message "-- EVILIFIED BUFFER --"
|
|
|
|
:cursor box)
|
|
|
|
|
2016-01-08 19:07:00 +00:00
|
|
|
(bind-map spacemacs-default-map
|
|
|
|
:prefix-cmd spacemacs-cmds
|
|
|
|
:evil-keys (dotspacemacs-leader-key)
|
|
|
|
:evil-states (evilified)
|
|
|
|
:override-minor-modes t
|
|
|
|
:override-mode-name spacemacs-leader-override-mode)
|
|
|
|
|
2015-12-06 04:13:40 +00:00
|
|
|
(evil-define-command evil-force-evilified-state ()
|
|
|
|
"Switch to evilified state without recording current command."
|
|
|
|
:repeat abort
|
|
|
|
:suppress-operator t
|
|
|
|
(evil-evilified-state))
|
|
|
|
|
|
|
|
(defun evilified-state--pre-command-hook ()
|
2015-12-12 02:24:33 +00:00
|
|
|
"Redirect key bindings to `evilified-state'.
|
|
|
|
Needed to bypass keymaps set as text properties."
|
2015-12-06 04:13:40 +00:00
|
|
|
(unless (bound-and-true-p isearch-mode)
|
2015-12-12 02:24:33 +00:00
|
|
|
(when (memq evil-state '(evilified visual))
|
|
|
|
(let* ((map (get-char-property (point) 'keymap))
|
|
|
|
(evilified-map (when map (cdr (assq 'evilified-state map))))
|
|
|
|
(command (when (and evilified-map
|
|
|
|
(eq 1 (length (this-command-keys))))
|
|
|
|
(lookup-key evilified-map (this-command-keys)))))
|
|
|
|
(when command (setq this-command command))))))
|
2015-08-12 05:31:44 +00:00
|
|
|
|
2015-12-14 05:00:25 +00:00
|
|
|
(defun evilified-state--setup-normal-state-keymap ()
|
|
|
|
"Setup the normal state keymap."
|
|
|
|
(unless evilified-state--normal-state-map
|
|
|
|
(setq-local evilified-state--normal-state-map
|
|
|
|
(copy-keymap evil-normal-state-map)))
|
|
|
|
(setq-local evil-normal-state-map
|
|
|
|
(copy-keymap evilified-state--normal-state-map))
|
|
|
|
(define-key evil-normal-state-map [escape] 'evil-evilified-state))
|
|
|
|
|
|
|
|
(defun evilified-state--restore-normal-state-keymap ()
|
|
|
|
"Restore the normal state keymap."
|
|
|
|
(setq-local evil-normal-state-map evilified-state--normal-state-map))
|
|
|
|
|
|
|
|
(defun evilified-state--clear-normal-state-keymap ()
|
|
|
|
"Clear the normal state keymap."
|
2015-12-20 05:49:52 +00:00
|
|
|
(setq-local evil-normal-state-map (cons 'keymap nil))
|
|
|
|
(evil-normalize-keymaps))
|
2015-12-14 05:00:25 +00:00
|
|
|
|
|
|
|
(defun evilified-state--setup-visual-state-keymap ()
|
|
|
|
"Setup the normal state keymap."
|
|
|
|
(setq-local evil-visual-state-map
|
|
|
|
(cons 'keymap (list (cons ?y 'evil-yank)
|
|
|
|
(cons 'escape 'evil-exit-visual-state)))))
|
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defun evilified-state--evilified-state-on-entry ()
|
2015-08-12 05:31:44 +00:00
|
|
|
"Setup evilified state."
|
2015-12-06 04:13:40 +00:00
|
|
|
(when (derived-mode-p 'magit-mode)
|
2015-12-06 05:43:51 +00:00
|
|
|
;; Courtesy of evil-magit package
|
|
|
|
;; without this set-mark-command activates visual-state which is just
|
|
|
|
;; annoying ;; and introduces possible bugs
|
2015-12-06 04:13:40 +00:00
|
|
|
(remove-hook 'activate-mark-hook 'evil-visual-activate-hook t))
|
2015-09-10 03:33:37 +00:00
|
|
|
(when (bound-and-true-p evil-surround-mode)
|
|
|
|
(make-local-variable 'evil-surround-mode)
|
2015-08-12 05:31:44 +00:00
|
|
|
(evil-surround-mode -1))
|
2015-12-14 05:00:25 +00:00
|
|
|
(evilified-state--setup-normal-state-keymap)
|
|
|
|
(evilified-state--setup-visual-state-keymap)
|
|
|
|
(add-hook 'pre-command-hook 'evilified-state--pre-command-hook nil 'local)
|
|
|
|
(add-hook 'evil-visual-state-entry-hook
|
|
|
|
'evilified-state--visual-state-on-entry nil 'local)
|
|
|
|
(add-hook 'evil-visual-state-exit-hook
|
|
|
|
'evilified-state--visual-state-on-exit nil 'local))
|
2015-12-06 04:13:40 +00:00
|
|
|
|
|
|
|
(defun evilified-state--visual-state-on-entry ()
|
|
|
|
"Setup visual state."
|
2015-12-14 05:00:25 +00:00
|
|
|
;; we need to clear temporarily the normal state keymap in order to reach
|
|
|
|
;; the mode keymap
|
|
|
|
(when (eq 'evilified evil-previous-state)
|
2015-12-20 05:49:52 +00:00
|
|
|
(evilified-state--clear-normal-state-keymap)))
|
2015-12-06 04:13:40 +00:00
|
|
|
|
|
|
|
(defun evilified-state--visual-state-on-exit ()
|
|
|
|
"Clean visual state"
|
2015-12-20 05:49:52 +00:00
|
|
|
(evilified-state--restore-normal-state-keymap))
|
2015-12-06 04:13:40 +00:00
|
|
|
|
2015-12-14 05:00:25 +00:00
|
|
|
(add-hook 'evil-evilified-state-entry-hook
|
|
|
|
'evilified-state--evilified-state-on-entry)
|
2015-08-12 05:31:44 +00:00
|
|
|
|
|
|
|
;; default key bindings for all evilified buffers
|
|
|
|
(define-key evil-evilified-state-map "/" 'evil-search-forward)
|
|
|
|
(define-key evil-evilified-state-map ":" 'evil-ex)
|
|
|
|
(define-key evil-evilified-state-map "h" 'evil-backward-char)
|
|
|
|
(define-key evil-evilified-state-map "j" 'evil-next-visual-line)
|
|
|
|
(define-key evil-evilified-state-map "k" 'evil-previous-visual-line)
|
|
|
|
(define-key evil-evilified-state-map "l" 'evil-forward-char)
|
|
|
|
(define-key evil-evilified-state-map "n" 'evil-search-next)
|
|
|
|
(define-key evil-evilified-state-map "N" 'evil-search-previous)
|
|
|
|
(define-key evil-evilified-state-map "v" 'evil-visual-char)
|
|
|
|
(define-key evil-evilified-state-map "V" 'evil-visual-line)
|
|
|
|
(define-key evil-evilified-state-map "gg" 'evil-goto-first-line)
|
|
|
|
(define-key evil-evilified-state-map "G" 'evil-goto-line)
|
|
|
|
(define-key evil-evilified-state-map (kbd "C-f") 'evil-scroll-page-down)
|
|
|
|
(define-key evil-evilified-state-map (kbd "C-b") 'evil-scroll-page-up)
|
2016-01-13 20:28:35 +00:00
|
|
|
(define-key evil-evilified-state-map (kbd "C-e") 'evil-scroll-line-down)
|
|
|
|
(define-key evil-evilified-state-map (kbd "C-y") 'evil-scroll-line-up)
|
2015-08-12 05:31:44 +00:00
|
|
|
(define-key evil-evilified-state-map (kbd "C-d") 'evil-scroll-down)
|
|
|
|
(define-key evil-evilified-state-map (kbd "C-u") 'evil-scroll-up)
|
2015-09-23 00:23:01 +00:00
|
|
|
(define-key evil-evilified-state-map (kbd "C-z") 'evil-emacs-state)
|
2015-12-08 07:08:50 +00:00
|
|
|
(setq evil-evilified-state-map-original (copy-keymap evil-evilified-state-map))
|
2015-08-12 05:31:44 +00:00
|
|
|
|
2015-09-26 04:26:16 +00:00
|
|
|
;; old macro
|
2015-11-05 13:09:00 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defmacro evilified-state-evilify (mode map &rest body)
|
2015-09-26 04:26:16 +00:00
|
|
|
"Set `evilified state' as default for MODE.
|
|
|
|
|
|
|
|
BODY is a list of additional key bindings to apply for the given MAP in
|
|
|
|
`evilified state'."
|
|
|
|
(let ((defkey (when body `(evil-define-key 'evilified ,map ,@body))))
|
|
|
|
`(progn (unless ,(null mode)
|
2015-11-05 13:09:00 +00:00
|
|
|
(unless (memq ',mode evilified-state--modes)
|
|
|
|
(push ',mode evilified-state--modes))
|
2015-09-26 04:26:16 +00:00
|
|
|
(unless (or (bound-and-true-p holy-mode)
|
2015-12-15 21:36:18 +00:00
|
|
|
(eq 'evilified (evil-initial-state ',mode)))
|
|
|
|
(evil-set-initial-state ',mode 'evilified)))
|
2015-09-26 04:26:16 +00:00
|
|
|
(unless ,(null defkey) (,@defkey)))))
|
2015-11-05 13:09:00 +00:00
|
|
|
(put 'evilified-state-evilify 'lisp-indent-function 'defun)
|
2015-09-26 04:26:16 +00:00
|
|
|
|
|
|
|
;; new macro
|
2015-11-05 13:09:00 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defmacro evilified-state-evilify-map (map &rest props)
|
2015-09-26 04:26:16 +00:00
|
|
|
"Evilify MAP.
|
|
|
|
|
|
|
|
Avaiblabe PROPS:
|
|
|
|
|
|
|
|
`:mode SYMBOL'
|
|
|
|
A mode SYMBOL associated with MAP. Used to add SYMBOL to the list of modes
|
|
|
|
defaulting to `evilified-state'.
|
|
|
|
|
|
|
|
`:evilified-map SYMBOL'
|
|
|
|
A map SYMBOL of an alternate evilified map, if nil then
|
|
|
|
`evil-evilified-state-map' is used.
|
|
|
|
|
|
|
|
`:eval-after-load SYMBOL'
|
|
|
|
If specified the evilification of MAP is deferred to the loading of the feature
|
|
|
|
bound to SYMBOL. May be required for some lazy-loaded maps.
|
|
|
|
|
2015-12-06 05:43:51 +00:00
|
|
|
`:pre-bindings EXPRESSIONS'
|
|
|
|
One or several EXPRESSIONS with the form `KEY FUNCTION':
|
|
|
|
KEY1 FUNCTION1
|
|
|
|
KEY2 FUNCTION2
|
|
|
|
These bindings are set in MAP before the evilification happens.
|
|
|
|
|
2015-09-26 04:26:16 +00:00
|
|
|
`:bindings EXPRESSIONS'
|
|
|
|
One or several EXPRESSIONS with the form `KEY FUNCTION':
|
|
|
|
KEY1 FUNCTION1
|
|
|
|
KEY2 FUNCTION2
|
2015-12-06 05:43:51 +00:00
|
|
|
These bindings are set directly in evil-evilified-state-map submap.
|
2015-09-26 04:26:16 +00:00
|
|
|
...
|
|
|
|
Each pair KEYn FUNCTIONn is defined in MAP after the evilification of it."
|
|
|
|
(declare (indent 1))
|
|
|
|
(let* ((mode (plist-get props :mode))
|
2015-12-08 07:08:50 +00:00
|
|
|
(evilified-map (or (plist-get props :evilified-map)
|
|
|
|
'evil-evilified-state-map-original))
|
2015-09-26 04:26:16 +00:00
|
|
|
(eval-after-load (plist-get props :eval-after-load))
|
2015-12-06 05:43:51 +00:00
|
|
|
(pre-bindings (evilified-state--mplist-get props :pre-bindings))
|
2015-11-05 13:09:00 +00:00
|
|
|
(bindings (evilified-state--mplist-get props :bindings))
|
2015-09-26 04:26:16 +00:00
|
|
|
(defkey (when bindings `(evil-define-key 'evilified ,map ,@bindings)))
|
|
|
|
(body
|
2015-12-06 05:43:51 +00:00
|
|
|
(progn
|
|
|
|
(evilified-state--define-pre-bindings map pre-bindings)
|
2015-12-08 07:08:50 +00:00
|
|
|
`(
|
|
|
|
;; we need to work on a local copy of the evilified keymap to
|
|
|
|
;; prevent the original keymap from being mutated.
|
|
|
|
(setq evil-evilified-state-map (copy-keymap ,evilified-map))
|
|
|
|
(let* ((sorted-map (evilified-state--sort-keymap
|
|
|
|
evil-evilified-state-map))
|
2015-12-06 05:43:51 +00:00
|
|
|
processed)
|
|
|
|
(mapc (lambda (map-entry)
|
|
|
|
(unless (member (car map-entry) processed)
|
|
|
|
(setq processed (evilified-state--evilify-event
|
2015-12-08 07:08:50 +00:00
|
|
|
,map ',map evil-evilified-state-map
|
2015-12-06 05:43:51 +00:00
|
|
|
(car map-entry) (cdr map-entry)))))
|
2015-12-08 07:08:50 +00:00
|
|
|
sorted-map)
|
|
|
|
(unless ,(null defkey)
|
|
|
|
(,@defkey)))
|
2015-12-06 05:43:51 +00:00
|
|
|
(unless ,(null mode)
|
|
|
|
(evilified-state--configure-default-state ',mode))))))
|
2015-09-26 04:26:16 +00:00
|
|
|
(if (null eval-after-load)
|
2015-12-06 05:43:51 +00:00
|
|
|
`(progn ,@body)
|
|
|
|
`(with-eval-after-load ',eval-after-load (progn ,@body)))))
|
2015-11-05 13:09:00 +00:00
|
|
|
(put 'evilified-state-evilify-map 'lisp-indent-function 'defun)
|
2015-09-26 04:26:16 +00:00
|
|
|
|
2015-12-06 05:43:51 +00:00
|
|
|
(defun evilified-state--define-pre-bindings (map pre-bindings)
|
|
|
|
"Define PRE-BINDINGS in MAP."
|
|
|
|
(while pre-bindings
|
|
|
|
(let ((key (pop pre-bindings))
|
|
|
|
(func (pop pre-bindings)))
|
|
|
|
(eval `(define-key ,map key ,func)))))
|
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defun evilified-state--configure-default-state (mode)
|
2015-09-26 04:26:16 +00:00
|
|
|
"Configure default state for the passed mode."
|
2015-11-05 13:09:00 +00:00
|
|
|
(add-to-list 'evilified-state--modes mode)
|
2015-09-26 04:26:16 +00:00
|
|
|
(unless (bound-and-true-p holy-mode)
|
2015-12-15 21:36:18 +00:00
|
|
|
(evil-set-initial-state mode 'evilified)))
|
2015-09-26 04:26:16 +00:00
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defun evilified-state--evilify-event (map map-symbol evil-map event evil-value
|
2015-12-08 07:08:50 +00:00
|
|
|
&optional processed pending-funcs)
|
2015-09-26 04:26:16 +00:00
|
|
|
"Evilify EVENT in MAP and return a list of PROCESSED events."
|
|
|
|
(if (and event (or evil-value pending-funcs))
|
|
|
|
(let* ((kbd-event (kbd (single-key-description event)))
|
|
|
|
(map-value (lookup-key map kbd-event))
|
|
|
|
(evil-value (or evil-value
|
|
|
|
(lookup-key evil-map kbd-event)
|
|
|
|
(car (pop pending-funcs)))))
|
|
|
|
(when evil-value
|
|
|
|
(evil-define-key 'evilified map kbd-event evil-value))
|
|
|
|
(when map-value
|
|
|
|
(add-to-list 'pending-funcs (cons map-value event) 'append))
|
|
|
|
(push event processed)
|
2015-11-05 13:09:00 +00:00
|
|
|
(setq processed (evilified-state--evilify-event
|
2015-09-26 04:26:16 +00:00
|
|
|
map map-symbol evil-map
|
2015-11-05 13:09:00 +00:00
|
|
|
(evilified-state--find-new-event event) nil
|
2015-09-26 04:26:16 +00:00
|
|
|
processed pending-funcs)))
|
|
|
|
(when pending-funcs
|
|
|
|
(spacemacs-buffer/warning
|
|
|
|
(concat (format (concat "Auto-evilication could not remap these "
|
|
|
|
"functions in map `%s':\n")
|
|
|
|
map-symbol)
|
|
|
|
(mapconcat (lambda (x)
|
|
|
|
(format " - `%s' originally mapped on `%s'"
|
|
|
|
(car x) (single-key-description (cdr x))))
|
|
|
|
pending-funcs "\n")))))
|
|
|
|
processed)
|
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defun evilified-state--find-new-event (event)
|
2015-09-26 04:26:16 +00:00
|
|
|
"Return a new event for the evilified EVENT."
|
|
|
|
(when event
|
|
|
|
(cond
|
2015-09-30 01:35:03 +00:00
|
|
|
((equal event ?\a) nil) ; C-g (cannot remap C-g)
|
|
|
|
((equal event 32) ?') ; space
|
2015-09-30 01:16:25 +00:00
|
|
|
((equal event ?/) ?\\)
|
2015-09-30 01:27:21 +00:00
|
|
|
((equal event ?:) ?|)
|
2015-12-06 04:13:40 +00:00
|
|
|
((and (numberp event) (<= ?a event) (<= event ?z)) (- event 32))
|
2015-09-30 01:35:03 +00:00
|
|
|
((equal event ?G) (+ (expt 2 25) ?\a)) ; G is mapped directly to C-S-g
|
2015-12-06 04:13:40 +00:00
|
|
|
((and (numberp event) (<= ?A event) (<= event ?Z)) (- event 64))
|
|
|
|
((and (numberp event) (<= 1 event) (<= event 26)) (+ (expt 2 25) event)))))
|
2015-09-26 04:26:16 +00:00
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defun evilified-state--sort-keymap (map)
|
2015-09-26 04:26:16 +00:00
|
|
|
"Sort MAP following the order: `s' > `S' > `C-s' > `C-S-s'"
|
|
|
|
(let (list)
|
|
|
|
(map-keymap (lambda (a b) (push (cons a b) list)) map)
|
|
|
|
(sort list
|
|
|
|
(lambda (a b)
|
|
|
|
(setq a (car a) b (car b))
|
|
|
|
(if (integerp a)
|
|
|
|
(if (integerp b)
|
|
|
|
(if (and (< a 256) (< b 256))
|
|
|
|
(> a b)
|
|
|
|
(< a b))
|
|
|
|
t)
|
|
|
|
(if (integerp b) nil
|
|
|
|
(string< a b)))))))
|
|
|
|
|
2015-11-05 13:09:00 +00:00
|
|
|
(defun evilified-state--mplist-get (plist prop)
|
|
|
|
"Get the values associated to PROP in PLIST, a modified plist.
|
|
|
|
|
|
|
|
A modified plist is one where keys are keywords and values are
|
|
|
|
all non-keywords elements that follow it.
|
|
|
|
|
|
|
|
If there are multiple properties with the same keyword, only the first property
|
|
|
|
and its values is returned.
|
|
|
|
|
|
|
|
Currently this function infloops when the list is circular."
|
|
|
|
(let ((tail plist)
|
|
|
|
result)
|
|
|
|
(while (and (consp tail) (not (eq prop (car tail))))
|
|
|
|
(pop tail))
|
|
|
|
;; pop the found keyword
|
|
|
|
(pop tail)
|
|
|
|
(while (and (consp tail) (not (keywordp (car tail))))
|
|
|
|
(push (pop tail) result))
|
|
|
|
(nreverse result)))
|
|
|
|
|
2015-11-19 05:24:27 +00:00
|
|
|
(provide 'evil-evilified-state)
|
2015-08-12 05:31:44 +00:00
|
|
|
|
|
|
|
;;; core-evilified-state.el ends here
|