ambevar-dotfiles/.emacs.d/lisp/init-evil.el

246 lines
10 KiB
EmacsLisp
Raw Normal View History

2017-10-08 20:36:28 +02:00
;;; Evil
2017-05-22 23:57:52 +02:00
;;; TODO: helm-show-kill-ring behaves like Emacs when pasting whole lines, not like Vim.
2017-10-08 20:36:28 +02:00
;;; TODO: helm-mark-ring seems to have issues with Evil:
;;; - The first entry is not the last position but the current one.
;;; - Navigating through the marks randomly produces a "Marker points into wrong buffer" error.
;;; https://github.com/emacs-evil/evil/issues/845#issuecomment-306050231
2017-10-08 20:36:28 +02:00
;;; TODO: Make Evil commands react more dynamically with read-only text, like eshell, wdired.
;;; Add support for I, C, D, S, s, c*, d*, R, r.
;;; See https://github.com/emacs-evil/evil/issues/852.
2017-10-08 20:36:28 +02:00
;;; REVIEW: 'cw' fails on the last character of the line when \n does not terminate it.
;;; See https://github.com/emacs-evil/evil/issues/863.
2017-10-08 20:36:28 +02:00
;;; Several packages handle relative line numbering:
;;; - nlinum-relative: Seems slow as of May 2017.
;;; - linum-relative: integrates well but not with fringe string, must be a function.
;;; - relative-line-number: linum must be disabled before running this.
2017-05-22 23:57:52 +02:00
(when (require 'linum-relative nil t)
2017-06-18 20:52:00 +02:00
;; REVIEW: Current symbol is displayed on all lines when we run `occur', `set-variables',
;; `helm-occur', etc: https://github.com/coldnew/linum-relative/issues/40.
2017-05-22 23:57:52 +02:00
(setq linum-relative-current-symbol "")
(linum-relative-toggle))
(evil-mode 1)
(remove-hook 'evil-insert-state-exit-hook 'expand-abbrev)
;; (setq evil-want-abbrev-expand-on-insert-exit nil)
(setq undo-tree-mode-lighter "")
2017-05-22 23:57:52 +02:00
2017-07-29 14:00:24 +02:00
(setq evil-cross-lines t
evil-move-beyond-eol t ; Especially useful for Edebug.
2017-08-04 18:49:59 +02:00
evil-move-cursor-back nil
2017-07-29 14:00:24 +02:00
evil-want-fine-undo t
evil-symbol-word-search nil)
2017-10-08 20:36:28 +02:00
;;; The evil-leader package has that over regular bindings that it centralizes
;;; the leader key configuration and automatically makes it available in relevant
;;; states. It is not really needed with EXWM however.
(when (require 'evil-leader nil t) (require 'init-evil-leader))
2017-10-08 20:36:28 +02:00
;;; Commenting.
;;; M-; comments next line in VISUAL. This is because of a different newline
;;; definition between Emacs and Vim.
;;; https://github.com/redguardtoo/evil-nerd-commenter: does not work well with
;;; motions and text objects, e.g. it cannot comment up without M--.
;;; `evil-commentary' is the way to go. We don't need an additional minor-mode though.
(when (require 'evil-commentary nil t)
(evil-global-set-key 'normal "gc" 'evil-commentary)
(evil-global-set-key 'normal "gy" 'evil-commentary-yank))
2017-06-24 11:04:05 +02:00
;;; Term mode should be in emacs state. It confuses 'vi' otherwise.
;;; Upstream will not change this:
;;; https://github.com/emacs-evil/evil/issues/854#issuecomment-309085267
(evil-set-initial-state 'term-mode 'emacs)
2017-10-08 20:36:28 +02:00
;;; For git commit, web edits and others.
;;; Since `with-editor-mode' is not a major mode, `evil-set-initial-state' cannot
;;; be used.
2017-05-22 23:57:52 +02:00
(when (require 'with-editor nil t)
(add-hook 'with-editor-mode-hook 'evil-insert-state))
2017-10-08 20:36:28 +02:00
;;; Allow for evil states in minibuffer. Double <ESC> exits.
2017-05-22 23:57:52 +02:00
(dolist
(keymap
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/
;; Text-from-Minibuffer.html#Definition of minibuffer-local-map
'(minibuffer-local-map
minibuffer-local-ns-map
minibuffer-local-completion-map
minibuffer-local-must-match-map
minibuffer-local-isearch-map))
(evil-define-key 'normal (eval keymap) [escape] 'abort-recursive-edit)
(evil-define-key 'normal (eval keymap) [return] 'exit-minibuffer))
(defun evil-minibuffer-setup ()
(set (make-local-variable 'evil-echo-state) nil)
;; (evil-set-initial-state 'mode 'insert) is the evil-proper
;; way to do this, but the minibuffer doesn't have a mode.
;; The alternative is to create a minibuffer mode (here), but
;; then it may conflict with other packages' if they do the same.
(evil-insert 1))
(add-hook 'minibuffer-setup-hook 'evil-minibuffer-setup)
2017-10-08 20:36:28 +02:00
;;; Because of the above minibuffer-setup-hook, some bindings need be reset.
(evil-define-key 'normal evil-ex-completion-map [escape] 'abort-recursive-edit)
(evil-define-key 'insert evil-ex-completion-map "\M-p" 'previous-complete-history-element)
(evil-define-key 'insert evil-ex-completion-map "\M-n" 'next-complete-history-element)
2017-10-08 20:36:28 +02:00
;;; TODO: evil-ex history binding in normal mode do not work.
(evil-define-key 'normal evil-ex-completion-map "\M-p" 'previous-history-element)
(evil-define-key 'normal evil-ex-completion-map "\M-n" 'next-history-element)
(define-keys evil-ex-completion-map
"M-p" 'previous-history-element
"M-n" 'next-history-element)
2017-05-22 23:57:52 +02:00
2017-10-08 20:36:28 +02:00
;;; Go-to-definition.
;;; From https://emacs.stackexchange.com/questions/608/evil-map-keybindings-the-vim-way.
2017-05-22 23:57:52 +02:00
(evil-global-set-key
'normal "gd"
(lambda () (interactive)
(evil-execute-in-emacs-state)
(call-interactively (key-binding (kbd "M-.")))))
2017-10-08 20:36:28 +02:00
;;; Multiple cursors.
;;; This shadows evil-magit's "gr", but we can use "?g" for that instead.
;;; It shadows C-n/p (`evil-paste-pop'), but we use `helm-show-kill-ring' on
;;; another binding.
2017-05-22 23:57:52 +02:00
(when (require 'evil-mc nil t)
(global-evil-mc-mode 1)
2017-05-28 18:37:54 +02:00
(define-key evil-mc-key-map (kbd "C-<mouse-1>") 'evil-mc-toggle-cursor-on-click)
(set-face-attribute 'evil-mc-cursor-default-face nil :inherit nil :inverse-video nil :box "white")
(when (require 'evil-mc-extras nil t)
(global-evil-mc-extras-mode 1)))
2017-05-22 23:57:52 +02:00
2017-10-08 20:36:28 +02:00
;;; Change mode-line color by Evil state.
(setq evil-default-modeline-color (cons (face-background 'mode-line) (or (face-foreground 'mode-line) "black")))
(defun evil-color-modeline ()
(let ((color (cond ((minibufferp) evil-default-modeline-color)
((evil-insert-state-p) '("#006fa0" . "#ffffff")) ; 00bb00
((evil-emacs-state-p) '("#444488" . "#ffffff"))
(t evil-default-modeline-color))))
(set-face-background 'mode-line (car color))
(set-face-foreground 'mode-line (cdr color))))
(add-hook 'post-command-hook 'evil-color-modeline)
(setq evil-mode-line-format nil)
2017-10-08 20:36:28 +02:00
;;; Add defun text-object. TODO: Does not work for "around".
;;; https://github.com/emacs-evil/evil/issues/874
2017-07-17 18:16:43 +02:00
(evil-define-text-object evil-a-defun (count &optional beg end type)
"Select a defun."
(evil-select-an-object 'evil-defun beg end type count))
(evil-define-text-object evil-inner-defun (count &optional beg end type)
"Select inner defun."
(evil-select-inner-object 'evil-defun beg end type count))
(define-key evil-outer-text-objects-map "d" 'evil-a-defun)
(define-key evil-inner-text-objects-map "d" 'evil-inner-defun)
2017-10-08 20:36:28 +02:00
;;; Without the hook, the Edebug keys (f, n, i, etc.) would get mixed up on initialization.
2017-07-27 17:31:17 +02:00
(add-hook 'edebug-mode-hook 'evil-normalize-keymaps)
2017-10-08 20:36:28 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Mode specific bindings.
(require 'init-evil-info)
(require 'init-evil-help)
2017-08-31 20:46:51 +02:00
(require 'init-evil-man)
(with-eval-after-load 'transmission (require 'init-evil-transmission))
(with-eval-after-load 'elfeed (require 'init-evil-elfeed))
2017-09-01 13:09:54 +02:00
(with-eval-after-load 'emms (require 'init-evil-emms))
(with-eval-after-load 'mu4e
(when (require 'evil-mu4e nil t)
2017-10-10 17:09:19 +02:00
;; TODO: evil-mu4e needs a big overhaul, e.g. 'visual commands are not supported. Report upstream.
2017-07-27 17:32:11 +02:00
(evil-define-key 'motion mu4e-headers-mode-map
2017-10-10 17:09:19 +02:00
"i" 'mu4e-headers-mark-for-flag
"I" 'mu4e-headers-mark-for-unflag
;; "R" 'mu4e-headers-mark-for-refile
2017-07-31 08:59:54 +02:00
"p" 'mu4e-headers-toggle-include-related
2017-07-27 17:32:11 +02:00
"r" 'mu4e-compose-reply)
2017-10-10 17:09:19 +02:00
(evil-define-key 'visual mu4e-headers-mode-map
"u" 'mu4e-headers-mark-for-unmark)
(evil-define-key 'motion mu4e-view-mode-map
(kbd "SPC") 'mu4e-view-scroll-up-or-next
(kbd "TAB") 'shr-next-link
2017-10-10 17:09:19 +02:00
"i" 'mu4e-view-mark-for-flag
"I" 'mu4e-view-mark-for-unflag
;; "R" 'mu4e-view-mark-for-refile
2017-07-31 08:59:54 +02:00
"r" 'mu4e-compose-reply
"za" 'mu4e-view-save-attachment-multi
2017-10-10 17:09:19 +02:00
"\C-j" 'mu4e-view-headers-next
"\C-k" 'mu4e-view-headers-prev
"\M-j" 'mu4e-view-headers-next ; Custom
"\M-k" 'mu4e-view-headers-prev ; Custom
2017-07-31 08:59:54 +02:00
"h" 'evil-backward-char
"zh" 'mu4e-view-toggle-html
"gu" 'mu4e-view-go-to-url)
(evil-set-initial-state 'mu4e-compose-mode 'insert)))
(with-eval-after-load 'init-helm (require 'init-evil-helm))
(with-eval-after-load 'calendar (require 'init-evil-calendar))
2017-10-08 20:36:28 +02:00
;;; nXML
2017-10-10 17:06:09 +02:00
(evil-define-key 'normal nxml-mode-map
"\C-j" 'nxml-forward-element
"\C-k" 'nxml-backward-element
"\M-j" 'nxml-forward-element ; Custom
"\M-k" 'nxml-backward-element ; Custom
">" 'nxml-down-element
"<" 'nxml-backward-up-element)
(evil-define-key 'visual nxml-mode-map
"\C-j" 'nxml-forward-element
"\C-k" 'nxml-backward-element
"\M-j" 'nxml-forward-element ; Custom
"\M-k" 'nxml-backward-element ; Custom
">" 'nxml-down-element
"<" 'nxml-backward-up-element)
(with-eval-after-load 'magit
(when (require 'evil-magit nil t)
(evil-magit-define-key evil-magit-state 'magit-mode-map "<" 'magit-section-up)
;; C-j/k is the default, M-j/k is more consistent with our customization for Helm.
(evil-magit-define-key evil-magit-state 'magit-mode-map "M-j" 'magit-section-forward)
(evil-magit-define-key evil-magit-state 'magit-mode-map "M-k" 'magit-section-backward)))
(require 'evil-ediff nil t)
(with-eval-after-load 'org (require 'init-evil-org))
2017-10-08 20:36:28 +02:00
;; (when (require 'evil-org nil t)
;; (add-hook 'org-mode-hook 'evil-org-mode)
;; (evil-org-set-key-theme '(textobjects insert navigation additional shift todo heading)))
(with-eval-after-load 'package (require 'init-evil-package))
(with-eval-after-load 'eshell (require 'init-evil-eshell))
2017-10-11 14:35:29 +02:00
(with-eval-after-load 'outline (require 'init-evil-outline))
2017-07-31 09:00:25 +02:00
(with-eval-after-load 'image-mode (require 'init-evil-image))
(with-eval-after-load 'pdf-view
(require 'init-evil-pdf)
;; TODO: `image-mode-map' is the parent of `pdf-view-mode-map'. A bug(?) in
;; Evil overrides all image-mode-map bindings.
;; See https://github.com/emacs-evil/evil/issues/938
;; and https://github.com/politza/pdf-tools/issues/324.
;; A workaround is to re-load the image bindings after PDF bindings are set.
(load-library "init-evil-image"))
2017-07-31 09:00:25 +02:00
(with-eval-after-load 'term (require 'init-evil-term))
2017-08-03 15:34:22 +02:00
(with-eval-after-load 'ztree-diff (require 'init-evil-ztree))
2017-08-07 12:18:07 +02:00
(with-eval-after-load 'debug (require 'init-evil-debugger))
(with-eval-after-load 'debbugs (require 'init-evil-debbugs))
(with-eval-after-load 'gnus (require 'init-evil-gnus))
2017-10-10 18:44:04 +02:00
(with-eval-after-load 'cus-edit (require 'init-evil-custom))
(provide 'init-evil)