-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup-evil.el
330 lines (283 loc) · 12 KB
/
setup-evil.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
;;; setup-evil.el --- user evil configuration entry point.
;;; Commentary:
;; This file configures the behavior of evil in Emacs
;; here is some resources it refers to
;; https://github.com/cofi/dotfiles/blob/master/emacs.d/config/cofi-evil.el
;;; Code:
(require 'yangchenyun-util)
(prelude-require-package 'undo-tree)
(prelude-require-package 'evil-leader)
(prelude-require-package 'evil-nerd-commenter)
(prelude-require-package 'undo-tree)
(require 'evil-rails)
(setq evil-find-skip-newlines t)
(setq evil-move-cursor-back nil
evil-cross-lines t)
(setq evil-default-cursor t)
(setq evil-mode-line-format nil)
(setq evil-leader/leader ",")
(setq evil-want-C-w-in-emacs-state t)
(define-key evil-motion-state-map "\C-w" 'hydra-window/body)
(when evil-want-C-w-in-emacs-state
(define-key evil-emacs-state-map "\C-w" 'hydra-window/body))
(setq evil-search-module 'evil-search)
(global-evil-leader-mode)
(defun evilnc-default-hotkeys ()
"Set the hotkeys of evil-nerd-comment"
(interactive)
(global-set-key (kbd "M-/") 'evilnc-comment-or-uncomment-lines)
(global-set-key (kbd "C-c p") 'evilnc-comment-or-uncomment-paragraphs))
(evilnc-default-hotkeys)
;; FIXME a little overlap with powerline evil configuration
(setq evil-normal-state-tag
(propertize " N " 'face '((:background "red" :foreground "black")))
evil-emacs-state-tag
(propertize " E " 'face '((:background "blue violet" :foreground "black")))
evil-insert-state-tag
(propertize " I " 'face '((:background "green")))
evil-motion-state-tag
(propertize " M " 'face '((:background "blue")))
evil-visual-state-tag
(propertize " V " 'face '((:background "orange" :foreground "black")))
evil-operator-state-tag
(propertize " O " 'face '((:background "sky blue"))))
(require-and-exec 'evil-surround
(setq-default evil-surround-pairs-alist '((?\( . ("(" . ")"))
(?\[ . ("[" . "]"))
(?\{ . ("{" . "}"))
(?\) . ("( " . " )"))
(?\] . ("[ " . " ]"))
(?\} . ("{ " . " }"))
(?> . ("< " . " >"))
(?# . ("#{" . "}"))
(?p . ("(" . ")"))
(?b . ("[" . "]"))
(?B . ("{" . "}"))
(?< . ("<" . ">"))
(?t . evil-surround-read-tag)))
(defun yangchenyun/surround-add-pair (trigger begin-or-fun &optional end)
"Add a surround pair.
If `end' is nil `begin-or-fun' will be treated as a fun."
(push (cons (if (stringp trigger)
(string-to-char trigger)
trigger)
(if end
(cons begin-or-fun end)
begin-or-fun))
evil-surround-pairs-alist))
(global-evil-surround-mode 1)
(add-to-hooks (lambda ()
(yangchenyun/surround-add-pair "`" "`" "'"))
'(emacs-lisp-mode-hook lisp-mode-hook))
(add-to-hooks (lambda ()
(yangchenyun/surround-add-pair "~" "``" "``"))
'(markdown-mode-hook rst-mode-hook python-mode-hook))
(add-hook 'LaTeX-mode-hook (lambda ()
(yangchenyun/surround-add-pair "~" "\\texttt{" "}")
(yangchenyun/surround-add-pair "=" "\\verb=" "=")
(yangchenyun/surround-add-pair "/" "\\emph{" "}")
(yangchenyun/surround-add-pair "*" "\\textbf{" "}")
(yangchenyun/surround-add-pair "P" "\\(" "\\)")))
(add-to-hooks (lambda ()
(yangchenyun/surround-add-pair "c" ":class:`" "`")
(yangchenyun/surround-add-pair "f" ":func:`" "`")
(yangchenyun/surround-add-pair "m" ":meth:`" "`")
(yangchenyun/surround-add-pair "a" ":attr:`" "`")
(yangchenyun/surround-add-pair "e" ":exc:`" "`"))
'(rst-mode-hook python-mode-hook)))
(evil-mode 1)
(evil-define-command cofi/evil-maybe-exit ()
:repeat change
(interactive)
(let ((modified (buffer-modified-p))
(entry-key ?j)
(exit-key ?j))
(insert entry-key)
(let ((evt (read-event (format "Insert %c to exit insert state" exit-key) nil 0.5)))
(cond
((null evt) (message ""))
((and (integerp evt) (char-equal evt exit-key))
(delete-char -1)
(set-buffer-modified-p modified)
(push 'escape unread-command-events))
(t (push evt unread-command-events))))))
(fill-keymap evil-normal-state-map
"!" 'string-inflection-cycle
"Y" (kbd "y$")
"+" 'evil-numbers/inc-at-pt
"-" 'evil-numbers/dec-at-pt
"SPC" 'evil-scroll-down
"DEL" 'evil-scroll-up
"C-SPC" 'evil-ace-jump-word-mode
"go" 'evil-ace-jump-line-mode
"C-t" 'transpose-chars
"gH" 'evil-window-top
"gL" 'evil-window-bottom
"gM" 'evil-window-middle
"H" 'beginning-of-line
"L" 'end-of-line
"M-p" 'helm-projectile
"M-." 'ycmd-goto-definition
"M-?" 'ycmd-show-documentation
"M-," 'pop-tag-mark
;; smartparens
;; Finds opening '(' of the current list.
"(" 'sp-backward-up-sexp
;; Finds closing ')' of the current list.
")" 'sp-up-sexp
;; Go to the start of current/previous sexp
"[[" 'sp-backward-sexp
;; Go to the start of next sexp.
"]]" 'sp-forward-sexp
"}" 'sp-forward-barf-sexp
"{" 'sp-backward-barf-sexp
"gn" 'sp-next-sexp ;; in a AST sence
"gp" 'sp-previous-sexp
"gk" 'sp-kill-sexp
"gu" 'sp-raise-sexp
"gy" 'sp-copy-sexp
;; ispell shortcuts
"gs" 'ispell-word)
(fill-keymap evil-motion-state-map
"y" 'evil-yank
"Y" (kbd "y$")
"_" 'evil-first-non-blank
"C-e" 'end-of-line
"C-S-d" 'evil-scroll-up
"C-S-f" 'evil-scroll-page-up
"_" 'evil-first-non-blank
"C-y" nil)
(fill-keymap evil-insert-state-map
"j" 'cofi/evil-maybe-exit
"C-h" 'backward-delete-char
"C-k" 'kill-line
"C-y" 'yank
"M-p" 'helm-projectile
"C-e" 'end-of-line)
(fill-keymaps (list evil-operator-state-map
evil-visual-state-map)
"SPC" 'evil-scroll-down
"DEL" 'evil-scroll-up
"C-SPC" 'evil-ace-jump-word-mode)
(defun cofi/clear-empty-lines ()
(let ((line (buffer-substring (point-at-bol) (point-at-eol))))
(when (string-match "^ +$" line)
(delete-region (point-at-bol) (point-at-eol)))))
(add-hook 'evil-insert-state-exit-hook #'cofi/clear-empty-lines)
(evil-leader/set-key
"W" 'save-some-buffers
"k" 'kill-current-buffer
"K" 'kill-buffer-and-window
"f" 'ido-find-file
"d" 'dired-jump
"b" 'ibuffer
"m" 'compile
"/" 'evil-ex-nohighlight
"r" 'crux-recentf-ido-find-file
"i" 'imenu
"s" 'helm-occur
"S" 'eshell
"." 'smex
"p" 'hydra-project/body
;; nerd commenter
"ci" 'evilnc-comment-or-uncomment-lines
"cl" 'evilnc-comment-or-uncomment-to-the-line
"cc" 'evilnc-copy-and-comment-lines
"cp" 'evilnc-comment-or-uncomment-paragraphs
"cr" 'comment-or-uncomment-region
;; smartparens
;; https://github.com/emezeske/paredit.vim/blob/master/doc/paredit.txt
"<" 'sp-backward-slurp-sexp
">" 'sp-forward-slurp-sexp
"J" 'sp-join-sexp
"O" 'sp-split-sexp
"S" 'sp-splice-sexp
"w(" (prelude-wrap-with "(")
"w{" (prelude-wrap-with "{")
"w[" (prelude-wrap-with "[")
)
;; Integration with other mode
;; default initial state in modes
(cl-loop for (mode . state) in '((inferior-emacs-lisp-mode . emacs)
(pylookup-mode . emacs)
(comint-mode . emacs)
(ebib-entry-mode . emacs)
(ebib-index-mode . emacs)
(ebib-log-mode . emacs)
(elfeed-show-mode . emacs)
(elfeed-search-mode . emacs)
(gtags-select-mode . emacs)
(shell-mode . emacs)
(term-mode . emacs)
(bc-menu-mode . emacs)
(magit-branch-manager-mode . emacs)
(makey-key-mode . emacs)
(semantic-symref-results-mode . emacs)
(rdictcc-buffer-mode . emacs)
(gtags-browse-tags-mode . emacs)
(finder-mode . emacs)
(project-explorer-mode . motion)
(prodigy-mode . motion)
(ibuffer-mode . normal)
(org-agenda-mode . motion)
(erc-mode . normal))
do (evil-set-initial-state mode state))
(evil-add-hjkl-bindings gtags-browse-tags-mode-map 'emacs)
(evil-add-hjkl-bindings git-rebase-mode-map 'emacs)
(evil-add-hjkl-bindings occur-key-map 'emacs)
(evil-add-hjkl-bindings package-menu-mode-map 'emacs
"H" 'package-menu-quick-help
)
(setq ;; C-<leader> to access from all buffers
evil-leader/in-all-states t
;; enable <leader> anyway
evil-leader/no-prefix-mode-rx (list
"occur-mode"
"package-menu-mode"
"ibuffer-mode"
"magit-.*-mode"
))
;; TODO: redefine browse keybindings in man-mode & woman-mode to make
;; it work with evil
(eval-after-load 'woman-mode
'(progn
(defvar woman-mode-map)
(evil-make-overriding-map woman-mode-map 'motion)))
(eval-after-load 'org-agenda
'(progn
;; use the standard Dired bindings as a base
(defvar org-agenda-mode-map)
(evil-make-overriding-map org-agenda-mode-map 'motion)
(evil-add-hjkl-bindings org-agenda-mode-map 'motion
"j" 'org-agenda-next-line
"k" 'org-agenda-previous-line
"J" 'org-agenda-goto-date
"L" 'org-agenda-log-mode
"K" 'org-agenda-capture
"S" 'org-agenda-schedule
)))
(eval-after-load 'elisp-slime-nav-mode
'(progn
(evil-define-key 'insert smartparens-mode-map [(shift return)] 'sp-newline)))
(eval-after-load 'elisp-slime-nav
'(progn
(defvar elisp-slime-nav-mode-map)
(evil-make-overriding-map elisp-slime-nav-mode-map 'normal)
(evil-add-hjkl-bindings elisp-slime-nav-mode-map 'normal)))
(eval-after-load 'go-mode
'(progn
(evil-define-key 'normal go-mode-map [(meta ?.)] 'godef-jump)))
(eval-after-load 'smartparens
'(progn
(evil-define-key 'insert smartparens-mode-map [(shift return)] 'sp-newline)))
(eval-after-load 'e2wm
'(progn
(defvar e2wm:def-plugin-files-mode-map)
(evil-make-overriding-map e2wm:def-plugin-files-mode-map 'normal)
(defvar e2wm:def-plugin-imenu-mode-map)
(evil-make-overriding-map e2wm:def-plugin-imenu-mode-map 'normal)
(defvar e2wm:def-plugin-history-list-mode-map)
(evil-make-overriding-map e2wm:def-plugin-history-list-mode-map 'normal)
))
(provide 'setup-evil)
;;; setup-evil.el ends here