forked from emacs-evil/evil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evil-core.el
1397 lines (1276 loc) · 54.9 KB
/
evil-core.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; evil-core.el --- Core functionality
;; Author: Vegard Øye <vegard_oye at hotmail.com>
;; Maintainer: Vegard Øye <vegard_oye at hotmail.com>
;; Version: 1.13.0
;;
;; This file is NOT part of GNU Emacs.
;;; License:
;; This file is part of Evil.
;;
;; Evil 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.
;;
;; Evil 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 Evil. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Evil is defined as a globalized minor mode, enabled with the toggle
;; function `evil-mode'. This in turn enables `evil-local-mode' in
;; every buffer, which sets up the buffer's state.
;;
;; Each state has its own keymaps, and these keymaps have status as
;; "emulation keymaps" with priority over regular keymaps. Emacs
;; maintains the following keymap hierarchy (highest priority first):
;;
;; * Overriding keymaps/overlay keymaps...
;; * Emulation mode keymaps...
;; - Evil keymaps...
;; * Minor mode keymaps...
;; * Local keymap (`local-set-key')
;; * Global keymap (`global-set-key')
;;
;; Within this hierarchy, Evil arranges the keymaps for the current
;; state as shown below:
;;
;; * Intercept keymaps...
;; * Local state keymap
;; * Minor-mode keymaps...
;; * Auxiliary keymaps...
;; * Overriding keymaps...
;; * Global state keymap
;; * Keymaps for other states...
;;
;; These keymaps are listed in `evil-mode-map-alist', which is listed
;; in `emulation-mode-map-alist'.
;;
;; Most of the key bindings for a state are stored in its global
;; keymap, which has a name such as `evil-normal-state-map'. (See the
;; file evil-maps.el, which contains all the default key bindings.) A
;; state also has a local keymap (`evil-normal-state-local-map'),
;; which may contain user customizations for the current buffer.
;; Furthermore, any Emacs mode may be assigned state bindings of its
;; own by passing the mode's keymap to the function `evil-define-key'
;; or `evil-define-minor-mode-key'. The former uses a specific map to
;; define the key in while the latter associates the key with a
;; particular mode. These mode-specific bindings are ultimately stored
;; in so-called auxiliary and minor-mode keymaps respectively, which
;; are sandwiched between the local keymap and the global keymap.
;; Finally, the state may also activate the keymaps of other states
;; (e.g., Normal state inherits bindings from Motion state).
;;
;; For integration purposes, a regular Emacs keymap may be "elevated"
;; to emulation status by passing it to `evil-make-intercept-map' or
;; `evil-make-overriding-map'. An "intercept" keymap has priority over
;; all other Evil keymaps. (Evil uses this facility when debugging and
;; for handling the "ESC" key in the terminal.) More common is the
;; "overriding" keymap, which only has priority over the global state
;; keymap. (This is useful for adapting key-heavy modes such as Dired,
;; where all but a few keys should be left as-is and should not be
;; shadowed by Evil's default bindings.)
;;
;; States are defined with the macro `evil-define-state', which
;; creates a command for switching to the state. This command,
;; for example `evil-normal-state' for Normal state, performs
;; the following tasks:
;;
;; * Setting `evil-state' to the new state.
;; * Refreshing the keymaps in `evil-mode-map-alist'.
;; * Updating the mode line.
;; - Normal state depends on `evil-normal-state-tag'.
;; * Adjusting the cursor's appearance.
;; - Normal state depends on `evil-normal-state-cursor'.
;; * Displaying a message in the echo area.
;; - Normal state depends on `evil-normal-state-message'.
;; * Running hooks.
;; - Normal state runs `evil-normal-state-entry-hook' when
;; entering, and `evil-normal-state-exit-hook' when exiting.
;;
;; The various properties of a state can be accessed through their
;; respective variables, or by passing a keyword and the state's name
;; to the `evil-state-property' function. Evil defines the states
;; Normal state ("normal"), Insert state ("insert"), Visual state
;; ("visual"), Replace state ("replace"), Operator-Pending state
;; ("operator"), Motion state ("motion") and Emacs state ("emacs").
(require 'evil-common)
;;; Code:
(declare-function evil-emacs-state-p "evil-states")
(declare-function evil-ex-p "evil-ex")
(defvar evil-mode-buffers)
(define-minor-mode evil-local-mode
"Minor mode for setting up Evil in a single buffer."
:init-value nil
(cond
((evil-disabled-buffer-p)
;; Don't leave the mode variable on in buffers where evil disabled, because
;; functions that check this variable will get an incorrect result (e.g.,
;; evil-refresh-cursor).
(setq evil-local-mode nil))
(evil-local-mode
(setq emulation-mode-map-alists
(evil-concat-lists '(evil-mode-map-alist)
emulation-mode-map-alists))
(evil-initialize-local-keymaps)
;; restore the proper value of `major-mode' in Fundamental buffers
(when (eq major-mode 'turn-on-evil-mode)
(setq major-mode 'fundamental-mode))
(when (minibufferp)
(setq-local evil-default-state 'insert)
(setq-local evil-echo-state nil))
;; The initial state is usually setup by `evil-initialize' when
;; the major-mode in a buffer changes. This preliminary
;; initialization is only for the case when `evil-local-mode' is
;; called directly for the first time in a buffer.
(unless evil-state (evil-initialize-state))
(add-hook 'input-method-activate-hook 'evil-activate-input-method t t)
(add-hook 'input-method-deactivate-hook 'evil-deactivate-input-method t t)
(add-hook 'activate-mark-hook 'evil-visual-activate-hook nil t)
(add-hook 'pre-command-hook 'evil-repeat-pre-hook)
(add-hook 'post-command-hook 'evil-repeat-post-hook))
(t
(evil-refresh-mode-line)
(remove-hook 'activate-mark-hook 'evil-visual-activate-hook t)
(remove-hook 'input-method-activate-hook 'evil-activate-input-method t)
(remove-hook 'input-method-deactivate-hook 'evil-deactivate-input-method t)
(evil-change-state nil))))
;; Make the variable permanent local. This is particular useful in
;; conjunction with nXhtml/mumamo because mumamo does not touch these
;; variables.
(put 'evil-local-mode 'permanent-local t)
(defun turn-on-evil-mode (&optional arg)
"Turn on Evil in the current buffer."
(interactive)
(evil-local-mode (or arg 1)))
(defun turn-off-evil-mode (&optional arg)
"Turn off Evil in the current buffer."
(interactive)
(evil-local-mode (or arg -1)))
;; The function `evil-initialize' should only be used to initialize
;; `evil-local-mode' from the globalized minor-mode `evil-mode'. It is
;; called whenever evil is enabled in a buffer for the first time or
;; when evil is active and the major-mode of the buffer changes. In
;; addition to enabling `evil-local-mode' it also sets the initial
;; evil-state according to the major-mode.
(defun evil-initialize ()
"Enable Evil in the current buffer, if appropriate.
To enable Evil globally, do (evil-mode 1)."
(unless (and (minibufferp) (not evil-want-minibuffer))
(evil-local-mode 1)
(evil-initialize-state)))
;;;###autoload (autoload 'evil-mode "evil" nil t)
(define-globalized-minor-mode evil-mode
evil-local-mode evil-initialize)
;; No hooks are run in Fundamental buffers, so other measures are
;; necessary to initialize Evil in these buffers. When Evil is
;; enabled globally, the default value of `major-mode' is set to
;; `turn-on-evil-mode', so that Evil is enabled in Fundamental
;; buffers as well. Then, the buffer-local value of `major-mode' is
;; changed back to `fundamental-mode'. (Since the `evil-mode' function
;; is created by a macro, we use `defadvice' to augment it.)
(defadvice evil-mode (after start-evil activate)
"Enable Evil in Fundamental mode."
(if evil-mode
(progn
(when (eq (default-value 'major-mode) 'fundamental-mode)
;; changed back by `evil-local-mode'
(setq-default major-mode 'turn-on-evil-mode))
(ad-enable-regexp "^evil")
(ad-activate-regexp "^evil")
(with-no-warnings (evil-esc-mode 1)))
(when (eq (default-value 'major-mode) 'turn-on-evil-mode)
(setq-default major-mode 'fundamental-mode))
(ad-disable-regexp "^evil")
(ad-update-regexp "^evil")
(with-no-warnings (evil-esc-mode -1))))
(defun evil-change-state (state &optional message)
"Change the state to STATE.
If STATE is nil, disable all states."
(let ((func (evil-state-property (or state evil-state) :toggle)))
(when (and (functionp func)
(or message (not (eq state evil-state))))
(funcall func (if state (and message 1) -1)))))
(defmacro evil-save-state (&rest body)
"Save the current state; execute BODY; restore the state."
(declare (indent defun)
(debug t))
`(let* ((evil-state evil-state)
(evil-previous-state evil-previous-state)
(evil-previous-state-alist (copy-tree evil-previous-state-alist))
(evil-next-state evil-next-state)
(old-state evil-state)
(inhibit-quit t)
(buf (current-buffer)))
(unwind-protect
(progn ,@body)
(when (buffer-live-p buf)
(with-current-buffer buf
(evil-change-state old-state))))))
(defmacro evil-with-state (state &rest body)
"Change to STATE and execute BODY without refreshing the display.
Restore the previous state afterwards."
(declare (indent defun)
(debug t))
`(evil-without-display
(evil-save-state
(evil-change-state ',state)
,@body)))
(defun evil-initializing-p (&optional buffer)
"Whether Evil is in the process of being initialized."
(memq (or buffer (current-buffer)) evil-mode-buffers))
(defun evil-initialize-state (&optional state buffer)
"Set up the initial state for BUFFER.
BUFFER defaults to the current buffer.
Uses STATE if specified, or calls `evil-initial-state-for-buffer'.
See also `evil-set-initial-state'."
(with-current-buffer (or buffer (current-buffer))
(if state (evil-change-state state)
(evil-change-to-initial-state buffer))))
(put 'evil-initialize-state 'permanent-local-hook t)
(defun evil-initial-state-for-buffer-name (&optional name default)
"Return the initial Evil state to use for a buffer with name NAME.
Matches the name against the regular expressions in
`evil-buffer-regexps'. If none matches, returns DEFAULT."
(let ((name (if (stringp name) name (buffer-name name)))
regexp state)
(when (stringp name)
(catch 'done
(dolist (entry evil-buffer-regexps default)
(setq regexp (car entry)
state (cdr entry))
(when (string-match regexp name)
(throw 'done state)))))))
(defun evil-disabled-buffer-p (&optional buffer)
"Whether Evil should be disabled in BUFFER."
(null (evil-initial-state-for-buffer-name buffer 'undefined)))
(defun evil-initial-state-for-buffer (&optional buffer default)
"Return the initial Evil state to use for BUFFER.
BUFFER defaults to the current buffer. Returns DEFAULT
if no initial state is associated with BUFFER.
See also `evil-initial-state'."
(with-current-buffer (or buffer (current-buffer))
(or (evil-initial-state-for-buffer-name (buffer-name))
(catch 'done
(dolist (mode minor-mode-map-alist)
(setq mode (car-safe mode))
(when (and (boundp mode) (symbol-value mode))
(when (setq mode (evil-initial-state mode))
(throw 'done mode)))))
(evil-initial-state major-mode nil t)
default)))
(defun evil-initial-state (mode &optional default follow-parent checked-modes)
"Return the Evil state to use for MODE or its alias.
Returns DEFAULT if no initial state is associated with MODE.
The initial state for a mode can be set with
`evil-set-initial-state'.
If FOLLOW-PARENT is non-nil, also check parent modes of MODE and
its alias. CHECKED-MODES is used internally and should not be set
initially."
(cond
((and mode (symbolp mode) (memq mode checked-modes))
(error "Circular reference detected in ancestors of %s\n%s"
major-mode checked-modes))
((and mode (symbolp mode))
(let ((mode-alias (let ((func (symbol-function mode)))
(when (symbolp func)
func)))
state modes)
(or
(catch 'done
(dolist (entry (evil-state-property t :modes) default)
(setq state (car entry)
modes (symbol-value (cdr entry)))
(when (or (memq mode modes)
(and mode-alias
(memq mode-alias modes)))
(throw 'done state))))
(when follow-parent
(evil-initial-state (get mode 'derived-mode-parent)
nil t (cons mode checked-modes)))
(when follow-parent
(evil-initial-state (get mode-alias 'derived-mode-parent)
nil t (cons mode-alias checked-modes))))))))
(defun evil-set-initial-state (mode state)
"Set the initial state for major mode MODE to STATE.
This is the state the buffer comes up in."
(dolist (modes (evil-state-property t :modes))
(setq modes (cdr-safe modes))
(set modes (delq mode (symbol-value modes))))
(when state
(add-to-list (evil-state-property state :modes) mode)))
(evil-define-command evil-change-to-initial-state
(&optional buffer message)
"Change the state of BUFFER to its initial state.
This is the state the buffer came up in. If Evil is not activated
then this function does nothing."
:keep-visual t
:suppress-operator t
(with-current-buffer (or buffer (current-buffer))
(when evil-local-mode
(evil-change-state (evil-initial-state-for-buffer
buffer (or evil-default-state 'normal))
message))))
(evil-define-command evil-change-to-previous-state
(&optional buffer message)
"Change the state of BUFFER to its previous state."
:keep-visual t
:repeat abort
:suppress-operator t
(with-current-buffer (or buffer (current-buffer))
(let ((prev-state evil-previous-state)
(prev-prev-state (cdr-safe (assoc evil-previous-state
evil-previous-state-alist))))
(evil-change-state nil)
(when prev-prev-state
(setq evil-previous-state prev-prev-state))
(evil-change-state (or prev-state evil-default-state 'normal)
message))))
;; When a buffer is created in a low-level way, it is invisible to
;; Evil (as well as other globalized minor modes) because no hooks are
;; run. This is appropriate since many buffers are used for throwaway
;; purposes. Passing the buffer to `set-window-buffer' indicates
;; otherwise, though, so advise this function to initialize Evil.
(defadvice set-window-buffer (before evil)
"Initialize Evil in the displayed buffer."
(when evil-mode
(when (get-buffer (ad-get-arg 1))
(with-current-buffer (ad-get-arg 1)
(unless evil-local-mode
(evil-local-mode 1))))))
;; Refresh cursor color.
;; Cursor color can only be set for each frame but not for each buffer.
(add-hook 'window-configuration-change-hook 'evil-refresh-cursor)
(defadvice select-window (after evil activate)
(evil-refresh-cursor))
(defun evil-generate-mode-line-tag (&optional state)
"Generate the evil mode-line tag for STATE."
(let ((tag (evil-state-property state :tag t)))
;; prepare mode-line: add tooltip
(if (stringp tag)
(propertize tag
'help-echo (evil-state-property state :name)
'mouse-face 'mode-line-highlight)
tag)))
(defun evil-refresh-mode-line (&optional state)
"Refresh mode line tag."
(when (listp mode-line-format)
(setq evil-mode-line-tag (evil-generate-mode-line-tag state))
;; refresh mode line data structure
;; first remove evil from mode-line
(setq mode-line-format (delq 'evil-mode-line-tag mode-line-format))
(let ((mlpos mode-line-format)
pred which where)
;; determine before/after which symbol the tag should be placed
(cond
((eq evil-mode-line-format 'before)
(setq where 'after which 'mode-line-position))
((eq evil-mode-line-format 'after)
(setq where 'after which 'mode-line-modes))
((consp evil-mode-line-format)
(setq where (car evil-mode-line-format)
which (cdr evil-mode-line-format))))
;; find the cons-cell of the symbol before/after which the tag
;; should be placed
(while (and mlpos
(let ((sym (or (car-safe (car mlpos)) (car mlpos))))
(not (eq which sym))))
(setq pred mlpos
mlpos (cdr mlpos)))
;; put evil tag at the right position in the mode line
(cond
((not mlpos)) ;; position not found, so do not add the tag
((eq where 'before)
(if pred
(setcdr pred (cons 'evil-mode-line-tag mlpos))
(setq mode-line-format
(cons 'evil-mode-line-tag mode-line-format))))
((eq where 'after)
(setcdr mlpos (cons 'evil-mode-line-tag (cdr mlpos)))))
(force-mode-line-update))))
;; input methods should be disabled in non-insertion states
(defun evil-activate-input-method ()
"Enable input method in states with :input-method non-nil."
(let (input-method-activate-hook
input-method-deactivate-hook)
(when (and evil-local-mode evil-state)
(setq evil-input-method current-input-method)
(unless (evil-state-property evil-state :input-method)
(deactivate-input-method)))))
(put 'evil-activate-input-method 'permanent-local-hook t)
(defun evil-deactivate-input-method ()
"Disable input method in all states."
(let (input-method-activate-hook
input-method-deactivate-hook)
(when (and evil-local-mode evil-state)
(setq evil-input-method nil))))
(put 'evil-deactivate-input-method 'permanent-local-hook t)
(defmacro evil-without-input-method-hooks (&rest body)
"Execute body with evil's activate/deactivate-input-method hooks deactivated.
This allows input methods to be used in normal-state."
`(unwind-protect
(progn
(remove-hook 'input-method-activate-hook 'evil-activate-input-method t)
(remove-hook 'input-method-deactivate-hook
'evil-deactivate-input-method t)
,@body)
(progn
(add-hook 'input-method-activate-hook 'evil-activate-input-method nil t)
(add-hook 'input-method-deactivate-hook
'evil-deactivate-input-method nil t))))
(defadvice toggle-input-method (around evil)
"Refresh `evil-input-method'."
(cond
((not evil-local-mode)
ad-do-it)
((evil-state-property evil-state :input-method)
ad-do-it)
(t
(let ((current-input-method evil-input-method))
ad-do-it))))
;; Local keymaps are implemented using buffer-local variables.
;; However, unless a buffer-local value already exists,
;; `define-key' acts on the variable's default (global) value.
;; So we need to initialize the variable whenever we enter a
;; new buffer or when the buffer-local values are reset.
(defun evil-initialize-local-keymaps ()
"Initialize a buffer-local value for local keymaps as necessary.
The initial value is that of `make-sparse-keymap'."
(dolist (entry evil-local-keymaps-alist)
(let ((mode (car entry))
(map (cdr entry)))
(unless (and (keymapp (symbol-value map))
(local-variable-p map))
(set map (make-sparse-keymap))))))
(defun evil-make-overriding-map (keymap &optional state copy)
"Give KEYMAP precedence over the global keymap of STATE.
The keymap will have lower precedence than custom STATE bindings.
If STATE is nil, give it precedence over all states.
If COPY is t, create a copy of KEYMAP and give that
higher precedence. See also `evil-make-intercept-map'."
(let ((key [override-state]))
(if (not copy)
(define-key keymap key (or state 'all))
(unless (keymapp copy)
(setq copy (assq-delete-all 'menu-bar (copy-keymap keymap))))
(define-key copy key (or state 'all))
(define-key keymap key copy))))
(defun evil-make-intercept-map (keymap &optional state aux)
"Give KEYMAP precedence over all Evil keymaps in STATE.
If STATE is nil, give it precedence over all states. If AUX is non-nil, make the
auxiliary keymap corresponding to KEYMAP in STATE an intercept keymap instead of
KEYMAP itself. See also `evil-make-overriding-map'."
(let ((key [intercept-state])
(keymap (if aux
(evil-get-auxiliary-keymap keymap state t t)
keymap)))
(define-key keymap key (or state 'all))))
(defmacro evil-define-keymap (keymap doc &rest body)
"Define a keymap KEYMAP listed in `evil-mode-map-alist'.
That means it will have precedence over regular keymaps.
DOC is the documentation for the variable. BODY, if specified,
is executed after toggling the mode. Optional keyword arguments
may be specified before the body code:
:mode VAR Mode variable. If unspecified, the variable
is based on the keymap name.
:local BOOLEAN Whether the keymap should be buffer-local, that is,
reinitialized for each buffer.
:func BOOLEAN Create a toggle function even if BODY is empty.
\(fn KEYMAP DOC [[KEY VAL]...] BODY...)"
(declare (indent defun)
(doc-string 2)
(debug (&define name
[&optional stringp]
[&rest [keywordp sexp]]
def-body)))
(let ((func t)
arg intercept key local mode overriding)
(while (keywordp (car-safe body))
(setq key (pop body)
arg (pop body))
(cond
((eq key :mode)
(setq mode arg))
((eq key :local)
(setq local arg))
((eq key :func)
(setq func arg))
((eq key :intercept)
(setq intercept arg))
((eq key :overriding)
(setq overriding arg))))
(setq mode (or mode
(intern (replace-regexp-in-string
"\\(?:-\\(?:mode-\\)?\\(?:key\\)?map\\)?$"
"-mode"
(symbol-name keymap)))))
`(progn
(defvar ,keymap ,(unless local '(make-sparse-keymap)))
(unless (get ',keymap 'variable-documentation)
(put ',keymap 'variable-documentation ,doc))
(defvar ,mode nil)
(unless (get ',mode 'variable-documentation)
(put ',mode 'variable-documentation ,doc))
(make-variable-buffer-local ',mode)
(put ',mode 'permanent-local t)
(when ,intercept
(evil-make-intercept-map ,keymap))
(when ,overriding
(evil-make-overriding-map ,keymap))
,@(if local
`((make-variable-buffer-local ',keymap)
(put ',keymap 'permanent-local t)
(evil-add-to-alist 'evil-local-keymaps-alist
',mode ',keymap))
`((evil-add-to-alist 'evil-global-keymaps-alist
',mode ',keymap)
(evil-add-to-alist 'evil-mode-map-alist
',mode ,keymap)))
,(when (or body func)
`(defun ,mode (&optional arg)
,@(when doc `(,doc))
(interactive)
(cond
((numberp arg)
(setq ,mode (> arg 0)))
(t
(setq ,mode (not ,mode))))
,@body))
',keymap)))
;; The ESC -> escape translation code has been provided by Stefan
;; Monnier in the discussion of GNU Emacs bug #13793.
(defun evil-esc-mode (&optional arg)
"Toggle interception of \\e (escape).
Enable with positive ARG and disable with negative ARG.
When enabled, `evil-esc-mode' modifies the entry of \\e in
`input-decode-map'. If such an event arrives, it is translated to
a plain 'escape event if no further event occurs within
`evil-esc-delay' seconds. Otherwise no translation happens and
the ESC prefix map (i.e. the map originally bound to \\e in
`input-decode-map`) is returned."
(cond
((or (null arg) (eq arg 0))
(evil-esc-mode (if evil-esc-mode -1 +1)))
((> arg 0)
(unless evil-esc-mode
(setq evil-esc-mode t)
(add-hook 'after-make-frame-functions #'evil-init-esc)
(mapc #'evil-init-esc (frame-list))))
((< arg 0)
(when evil-esc-mode
(remove-hook 'after-make-frame-functions #'evil-init-esc)
(mapc #'evil-deinit-esc (frame-list))
(setq evil-esc-mode nil)))))
(defun evil-init-esc (frame)
"Update `input-decode-map' in terminal."
(with-selected-frame frame
(let ((term (frame-terminal frame)))
(when (and
(or (eq evil-intercept-esc 'always)
(and evil-intercept-esc
(eq (terminal-live-p term) t))) ; only patch tty
(not (terminal-parameter term 'evil-esc-map)))
(let ((evil-esc-map (lookup-key input-decode-map [?\e])))
(set-terminal-parameter term 'evil-esc-map evil-esc-map)
(define-key input-decode-map [?\e]
`(menu-item "" ,evil-esc-map :filter ,#'evil-esc)))))))
(defun evil-deinit-esc (frame)
"Restore `input-decode-map' in terminal."
(with-selected-frame frame
(let ((term (frame-terminal frame)))
(when (terminal-live-p term)
(let ((evil-esc-map (terminal-parameter term 'evil-esc-map)))
(when evil-esc-map
(define-key input-decode-map [?\e] evil-esc-map)
(set-terminal-parameter term 'evil-esc-map nil)))))))
(defun evil-esc (map)
"Translate \\e to 'escape if no further event arrives.
This function is used to translate a \\e event either to 'escape
or to the standard ESC prefix translation map. If \\e arrives,
this function waits for `evil-esc-delay' seconds for another
event. If no other event arrives, the event is translated to
'escape, otherwise it is translated to the standard ESC prefix
map stored in `input-decode-map'. If `evil-inhibit-esc' is
non-nil or if evil is in emacs state, the event is always
translated to the ESC prefix.
The translation to 'escape happens only if the current command
has indeed been triggered by \\e. In other words, this will only
happen when the keymap is accessed from `read-key-sequence'. In
particular, if it is access from `define-key' the returned
mapping will always be the ESC prefix map."
(if (and (not evil-inhibit-esc)
(or evil-local-mode (evil-ex-p)
(active-minibuffer-window))
(not (evil-emacs-state-p))
(let ((keys (this-single-command-keys)))
(and (> (length keys) 0)
(= (aref keys (1- (length keys))) ?\e)))
(sit-for evil-esc-delay))
(prog1 [escape]
(when defining-kbd-macro
(end-kbd-macro)
(setq last-kbd-macro (vconcat last-kbd-macro [escape]))
(start-kbd-macro t t)))
map))
(defun evil-state-p (sym)
"Whether SYM is the name of a state."
(assq sym evil-state-properties))
(defun evil-state-keymaps (state &rest excluded)
"Return a keymap alist of keymaps activated by STATE.
If STATE references other states in its :enable property,
these states are recursively processed and added to the list.
\(The EXCLUDED argument is an internal safeguard against
infinite recursion, keeping track of processed states.)"
(let* ((state (or state evil-state))
(enable (evil-state-property state :enable))
(map (cons
(evil-state-property state :mode)
(evil-state-property state :keymap t)))
(local-map (cons
(evil-state-property state :local)
(evil-state-property state :local-keymap t)))
(minor-mode-maps (evil-state-minor-mode-keymaps state))
(aux-maps (evil-state-auxiliary-keymaps state))
(overriding-maps
(evil-state-overriding-keymaps state))
(intercept-maps
(evil-state-intercept-keymaps state))
(result `(,intercept-maps))
(remove-duplicates (null excluded)))
(unless (memq state enable)
(setq enable (cons state enable)))
;; process STATE's :enable property
(dolist (entry enable)
(cond
((memq entry excluded))
;; the keymaps for STATE
((eq entry state)
(setq result `(,@result
(,local-map)
,minor-mode-maps
,aux-maps
,overriding-maps
(,map)))
(push state excluded))
;; the keymaps for another state: call `evil-state-keymaps'
;; recursively, but keep track of processed states
((evil-state-p entry)
(setq result `(,@result
,(apply #'evil-state-keymaps entry excluded))))
;; a single keymap
((or (keymapp entry)
(and (keymapp (symbol-value entry))
(setq entry (symbol-value entry)))
(setq entry (evil-keymap-for-mode entry)))
(setq result `(,@result
((,(evil-mode-for-keymap entry t) .
,entry)))))))
;; postpone the expensive filtering of duplicates to the top level
(if remove-duplicates
(apply #'evil-concat-keymap-alists result)
(apply #'append result))))
(defun evil-normalize-keymaps (&optional state)
"Create a buffer-local value for `evil-mode-map-alist'.
This is a keymap alist, determined by the current state
\(or by STATE if specified)."
(let ((state (or state evil-state))
(excluded '(nil t))
map mode temp)
;; initialize buffer-local keymaps as necessary
(evil-initialize-local-keymaps)
;; deactivate keymaps of previous state
(dolist (entry evil-mode-map-alist)
(setq mode (car-safe entry)
map (cdr-safe entry))
;; don't deactivate overriding keymaps;
;; they are toggled by their associated mode
(if (or (memq mode excluded)
(evil-intercept-keymap-p map)
(evil-overriding-keymap-p map)
(evil-auxiliary-keymap-p map)
(evil-minor-mode-keymap-p map))
(push mode excluded)
(when (and (fboundp mode) (symbol-value mode))
(funcall mode -1))
(set mode nil)))
(setq evil-mode-map-alist nil)
;; activate keymaps of current state
(when state
(setq temp (evil-state-keymaps state))
(dolist (entry temp)
(setq mode (car entry)
map (cdr entry))
(unless (or (and (boundp mode) (symbol-value mode))
;; the minor-mode keymaps include modes that are not
;; necessarily active
(evil-minor-mode-keymap-p map))
(when (fboundp mode)
(funcall mode 1))
(set mode t))
;; refresh the keymap in case it has changed
;; (e.g., `evil-operator-shortcut-map' is
;; reset on toggling)
(if (or (memq mode excluded)
(evil-intercept-keymap-p map)
(evil-overriding-keymap-p map)
(evil-auxiliary-keymap-p map)
(evil-minor-mode-keymap-p map))
(push mode excluded)
(setcdr entry (or (evil-keymap-for-mode mode) map))))
;; update `evil-mode-map-alist'
(setq evil-mode-map-alist temp))))
(defun evil-mode-for-keymap (keymap &optional default)
"Return the minor mode associated with KEYMAP.
Returns DEFAULT if no mode is found.
See also `evil-keymap-for-mode'."
(let ((map (if (keymapp keymap) keymap (symbol-value keymap)))
(var (when (symbolp keymap) keymap)))
;; Check Evil variables first for speed purposes.
;; If all else fails, check `minor-mode-map-alist'.
(or (when var
(or (car (rassq var evil-global-keymaps-alist))
(car (rassq var evil-local-keymaps-alist))))
(car (rassq map (mapcar #'(lambda (e)
;; from (MODE-VAR . MAP-VAR)
;; to (MODE-VAR . MAP)
(cons (car-safe e)
(symbol-value (cdr-safe e))))
(append evil-global-keymaps-alist
evil-local-keymaps-alist))))
(car (rassq map minor-mode-map-alist))
default)))
(defun evil-keymap-for-mode (mode &optional variable)
"Return the keymap associated with MODE.
Return the keymap variable if VARIABLE is non-nil.
See also `evil-mode-for-keymap'."
(let* ((var (or (cdr (assq mode evil-global-keymaps-alist))
(cdr (assq mode evil-local-keymaps-alist))))
(map (or (symbol-value var)
(cdr (assq mode minor-mode-map-alist)))))
(if variable var map)))
(defun evil-state-auxiliary-keymaps (state)
"Return a keymap alist of auxiliary keymaps for STATE."
(let ((state (or state evil-state))
aux result)
(dolist (map (current-active-maps) result)
(when (setq aux (evil-get-auxiliary-keymap map state))
(push (cons (evil-mode-for-keymap map t) aux) result)))
(nreverse result)))
(defun evil-state-minor-mode-keymaps (state)
"Return a keymap alist of minor-mode keymaps for STATE."
(let* ((state (or state evil-state))
(state-entry (assq state evil-minor-mode-keymaps-alist)))
(when state-entry
(cdr state-entry))))
(defun evil-state-overriding-keymaps (&optional state)
"Return a keymap alist of overriding keymaps for STATE."
(let* ((state (or state evil-state))
result)
(dolist (map (current-active-maps))
(when (setq map (evil-overriding-keymap-p map state))
(push (cons (evil-mode-for-keymap map t) map) result)))
(nreverse result)))
(defun evil-state-intercept-keymaps (&optional state)
"Return a keymap alist of intercept keymaps for STATE."
(let* ((state (or state evil-state))
result)
(dolist (map (current-active-maps))
(when (setq map (or (evil-intercept-keymap-p map state)
(evil-intercept-keymap-p
(evil-get-auxiliary-keymap map state) state)))
(push (cons (evil-mode-for-keymap map t) map) result)))
(setq result (nreverse result))
result))
(defun evil-set-auxiliary-keymap (map state &optional aux)
"Set the auxiliary keymap for MAP in STATE to AUX.
If AUX is nil, create a new auxiliary keymap."
(unless (keymapp aux)
(setq aux (make-sparse-keymap)))
(unless (evil-auxiliary-keymap-p aux)
(evil-set-keymap-prompt
aux (format "Auxiliary keymap for %s"
(or (evil-state-property state :name)
(format "%s state" state)))))
(define-key map
(vconcat (list (intern (format "%s-state" state)))) aux)
aux)
(put 'evil-set-auxiliary-keymap 'lisp-indent-function 'defun)
(defun evil-get-auxiliary-keymap (map state &optional create ignore-parent)
"Get the auxiliary keymap for MAP in STATE.
If CREATE is non-nil, create an auxiliary keymap
if MAP does not have one. If CREATE and
IGNORE-PARENT are non-nil then a new auxiliary
keymap is created even if the parent of MAP has
one already."
(when state
(let* ((key (vconcat (list (intern (format "%s-state" state)))))
(parent-aux (when (and ignore-parent
(keymap-parent map)
state)
(lookup-key (keymap-parent map) key)))
(aux (if state (lookup-key map key) map)))
(cond
((and ignore-parent
(equal parent-aux aux)
create)
(evil-set-auxiliary-keymap map state))
((evil-auxiliary-keymap-p aux)
aux)
(create
(evil-set-auxiliary-keymap map state))))))
(defun evil-get-minor-mode-keymap (state mode)
"Get the auxiliary keymap for MODE in STATE, creating one if it
does not already exist."
(let ((state-entry (assq state evil-minor-mode-keymaps-alist)))
(if (and state-entry
(assq mode state-entry))
(cdr (assq mode state-entry))
(let ((map (make-sparse-keymap)))
(evil-set-keymap-prompt
map (format "Minor-mode keymap for %s in %s"
(symbol-name mode)
(or (evil-state-property state :name)
(format "%s state" state))))
(if state-entry
(setcdr state-entry
(append (list (cons mode map)) (cdr state-entry)))
(push (cons state (list (cons mode map)))
evil-minor-mode-keymaps-alist))
map))))
(defun evil-auxiliary-keymap-p (map)
"Whether MAP is an auxiliary keymap."
(and (keymapp map)
(string-match-p "Auxiliary keymap"
(or (keymap-prompt map) "")) t))
(defun evil-minor-mode-keymap-p (map)
"Whether MAP is a minor-mode keymap."
(and (keymapp map)
(string-match-p "Minor-mode keymap"
(or (keymap-prompt map) "")) t))
(defun evil-intercept-keymap-p (map &optional state)
"Whether MAP is an intercept keymap for STATE.
If STATE is nil, it means any state."
(let ((entry (and (keymapp map)
(lookup-key map [intercept-state]))))
(cond
((null entry)
nil)
((null state)
map)
((eq entry state)
map)
((eq entry 'all)
map))))
(defun evil-overriding-keymap-p (map &optional state)
"Whether MAP is an overriding keymap for STATE.
If STATE is nil, it means any state."
(let ((entry (and (keymapp map)
(lookup-key map [override-state]))))
(cond
((null entry)
nil)
((keymapp entry)
(evil-overriding-keymap-p entry state))
((null state)
map)
((eq entry state)
map)
((eq entry 'all)
map))))
(defun evil-intercept-keymap-state (map)
"Return the state for the intercept keymap MAP.
A return value of t means all states."
(let ((state (lookup-key map [intercept-state] map)))
(cond
((keymapp state)
(evil-intercept-keymap-state state))
((eq state 'all)
t)
(t
state))))
(defun evil-overriding-keymap-state (map)
"Return the state for the overriding keymap MAP.
A return value of t means all states."
(let ((state (lookup-key map [override-state] map)))
(cond
((keymapp state)
(evil-overriding-keymap-state state))
((eq state 'all)
t)
(t
state))))
(defun evil-send-leader ()
"Put symbol leader in `unread-command-events' to trigger any
<leader> bindings."
(interactive)
(setq prefix-arg current-prefix-arg)
(push '(t . leader) unread-command-events))
(defun evil-send-localleader ()
"Put symbol localleader in `unread-command-events' to trigger any
<localleader> bindings."
(interactive)
(setq prefix-arg current-prefix-arg)
(push '(t. localleader) unread-command-events))
(defun evil-set-leader (state key &optional localleader)
"Set KEY to trigger <leader> bindings in STATE.
KEY should be in the form produced by `kbd'. STATE is one of
`normal', `insert', `visual', `replace', `operator', `motion',
`emacs', a list of one or more of these, or nil. nil means all of
the above. If LOCAL is non-nil, set localleader instead."
(let* ((all-states '(normal insert visual replace operator motion emacs))
(states (cond ((listp state) state)
((member state all-states) (list state))
((null state) all-states)
;; Maybe throw error here
(t (list state))))