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

240 lines
9.5 KiB
EmacsLisp
Raw Normal View History

2017-05-22 23:57:52 +02:00
;; Evil
;; TODO: helm-show-yank-ring behaves like Emacs when pasting whole lines, not like Vim.
;; 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
;; TODO: Make Evil commands react more dynamically with read-only text.
;; Add support for I, C, D, S, s, c*, d*, R, r.
;; See https://github.com/emacs-evil/evil/issues/852.
2017-05-22 23:57:52 +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.
(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))
;; The evil-leader package has that over regular bindings that it centralizes
;; the leader key configuration and automatically makes it available in relevant
;; states. Should we map <leader<leader> to the most used command,
;; e.g. `helm-mini'? Could be misleading.
(require 'evil-leader)
;; Leader mode and its key must be set before evil-mode.
(evil-leader/set-leader "<SPC>")
(global-evil-leader-mode)
2017-05-22 23:57:52 +02:00
(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
;; Commenting.
2017-06-24 11:04:05 +02:00
;; 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
(defun eshell-or-new-session (&optional arg)
"Create an interactive Eshell buffer.
If there is already an Eshell session active, switch to it.
If current buffer is already an Eshell buffer, create a new one and switch to it.
2017-07-17 18:16:43 +02:00
See `eshell' for the numeric prefix ARG."
(interactive "P")
(if (eq major-mode 'eshell-mode)
(eshell (or arg t))
(eshell arg)))
(defun org-find-first-agenda ()
(interactive)
(when (not (boundp 'org-agenda-files))
(require 'org))
(find-file (car org-agenda-files)))
(evil-leader/set-key
"RET" 'eshell-or-new-session
"\\" 'toggle-window-split
;; "a" 'org-agenda
"b" 'buffer-menu
"e" 'find-file
"k" 'kill-this-buffer
"t" 'org-find-first-agenda
"|" 'swap-windows)
2017-06-25 14:37:15 +02:00
(when (fboundp 'magit-status)
;; Since it is an autoload, we cannot use `with-eval-after-load'.
;; Use S-SPC instead of SPC to browse commit details.
(evil-leader/set-key "v" 'magit-status))
(when (fboundp 'emms-smart-browse)
(evil-leader/set-key "A" 'helm-emms)
(evil-leader/set-key "a" 'emms-smart-browse))
(with-eval-after-load 'emms
(evil-leader/set-key
"p" 'emms-pause
"n" 'emms-next))
(with-eval-after-load 'init-helm
(evil-leader/set-key
"b" 'helm-mini
"e" 'helm-find-files
"E" 'helm-find
"g" 'helm-grep-git-or-ag
"G" 'helm-grep-git-all-or-ag
"r" 'helm-resume))
2017-05-22 23:57:52 +02:00
;; Motion map: useful for `Info-mode', `help-mode', etc.
;; See `evil-motion-state-modes'.
(evil-global-set-key 'motion (kbd "TAB") 'forward-button)
(evil-global-set-key 'motion (kbd "<backtab>") 'backward-button)
(evil-define-key 'motion Info-mode-map
(kbd "S-SPC") 'Info-scroll-up
"\C-f" 'Info-scroll-up
"\C-b" 'Info-scroll-down
"\M-sf" 'Info-goto-node
"gg" 'evil-goto-first-line)
(evil-define-key 'motion help-mode-map
(kbd "S-SPC") 'scroll-up-command
"\C-f" 'scroll-up-command
"\C-b" 'scroll-down-command
"\C-o" 'help-go-back)
;;; 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-05-22 23:57:52 +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.
(when (require 'with-editor nil t)
(add-hook 'with-editor-mode-hook 'evil-insert-state))
;; Allow for evil states in minibuffer. Double <ESC> exits.
(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)
;; 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)
;; 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
;; 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-.")))))
;; Multiple cursors.
2017-05-28 18:37:54 +02:00
;; 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
;; 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-07-17 18:16:43 +02:00
;; Add defun text-object.
(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)
(evil-define-text-object evgeni-inner-defun (count &optional beg end type)
(save-excursion
(mark-defun)
(evil-range (region-beginning) (region-end) type :expanded t)))
(define-key evil-inner-text-objects-map "m" 'evgeni-inner-defun)
;; TODO: Use motion map for package, transmission, emms, elfeed...?
(with-eval-after-load 'transmission (require 'init-evil-transmission))
(with-eval-after-load 'elfeed (require 'init-evil-elfeed))
;;; Emms: It is important to set the bindings after emms-browser has loaded,
;;; since the mode-maps are defconst'd.
(with-eval-after-load 'emms-browser (require 'init-evil-emms))
(when (require 'evil-mu4e nil t)
(evil-set-initial-state 'mu4e-compose-mode 'insert)
(defun mu4e-headers-unread () (interactive) (mu4e-headers-search "flag:unread AND NOT flag:trashed"))
(evil-leader/set-key "m" 'mu4e-headers-unread))
(with-eval-after-load 'init-helm (require 'init-evil-helm))
(with-eval-after-load 'calendar (require 'init-evil-calendar))
;; nXML
(evil-define-key 'normal nxml-mode-map "<" '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))
(with-eval-after-load 'package (require 'init-evil-package))
(with-eval-after-load 'eshell (require 'init-evil-eshell))
(with-eval-after-load 'pdf-view (require 'init-evil-pdf))
(provide 'init-evil)