-
Notifications
You must be signed in to change notification settings - Fork 281
/
evil-commands.el
5264 lines (4840 loc) · 202 KB
/
evil-commands.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-commands.el --- Evil commands and operators -*- lexical-binding: t -*-
;; Author: Vegard Øye <vegard_oye at hotmail.com>
;; Maintainer: Vegard Øye <vegard_oye at hotmail.com>
;; Version: 1.15.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/>.
;;; Code:
(require 'evil-common)
(require 'evil-search)
(require 'evil-states)
(require 'evil-ex)
(require 'evil-types)
(require 'evil-command-window)
(require 'evil-jumps)
(require 'evil-vars)
(require 'cl-lib)
(require 'reveal)
(declare-function imenu--in-alist "imenu")
;;; Motions
;; Movement commands, or motions, are defined with the macro
;; `evil-define-motion'. A motion is a command with an optional
;; argument COUNT (interactively accessed by the code "<c>").
;; It may specify the :type command property (e.g., :type line),
;; which determines how it is handled by an operator command.
;; Furthermore, the command must have the command properties
;; :keep-visual t and :repeat motion; these are automatically
;; set by the `evil-define-motion' macro.
(evil-define-motion evil-forward-char (count &optional crosslines noerror)
"Move cursor to the right by COUNT characters.
Movement is restricted to the current line unless CROSSLINES is non-nil.
If NOERROR is non-nil, don't signal an error upon reaching the end
of the line or the buffer; just return nil."
:type exclusive
(interactive "<c>" (list evil-cross-lines
(evil-kbd-macro-suppress-motion-error)))
(cond
((not crosslines)
;; For efficiency, narrow the buffer to the projected
;; movement before determining the current line
(evil-with-restriction nil (min (+ (point) (or count 1) 1) (point-max))
(condition-case err
(evil-narrow-to-line (forward-char count))
(error
;; Restore the previous command (this one never happened).
;; This preserves the current column if the previous command
;; was `evil-next-line' or `evil-previous-line'.
(setq this-command last-command)
(unless noerror (signal (car err) (cdr err)))))))
(noerror (ignore-errors (evil-forward-char count crosslines)))
(t (evil-motion-loop (nil (or count 1))
(forward-char)
;; don't put the cursor on a newline
(or evil-move-beyond-eol
(evil-visual-state-p) (evil-operator-state-p)
(not (eolp)) (bolp)
(forward-char))))))
(evil-define-motion evil-backward-char (count &optional crosslines noerror)
"Move cursor to the left by COUNT characters.
Movement is restricted to the current line unless CROSSLINES is non-nil.
If NOERROR is non-nil, don't signal an error upon reaching the beginning
of the line or the buffer; just return nil."
:type exclusive
(interactive "<c>" (list evil-cross-lines
(evil-kbd-macro-suppress-motion-error)))
(cond
((not crosslines)
;; Restrict movement to the current line
(evil-with-restriction
(max (- (point) (or count 1)) (point-min))
(min (1+ (point)) (point-max))
(condition-case err
(evil-narrow-to-line (backward-char count))
(error
;; Restore the previous command (this one never happened).
;; This preserves the current column if the previous command
;; was `evil-next-line' or `evil-previous-line'.
(setq this-command last-command)
(unless noerror (signal (car err) (cdr err)))))))
(noerror (ignore-errors (evil-backward-char count crosslines)))
(t (evil-motion-loop (nil (or count 1))
(backward-char)
;; don't put the cursor on a newline
(unless (or (evil-visual-state-p) (evil-operator-state-p))
(evil-adjust-cursor))))))
(evil-define-motion evil-next-line (count)
"Move the cursor COUNT lines down."
:type line
(let (line-move-visual)
(evil-line-move (or count 1))))
(evil-define-motion evil-previous-line (count)
"Move the cursor COUNT lines up."
:type line
(let (line-move-visual)
(evil-line-move (- (or count 1)))))
(evil-define-motion evil-next-visual-line (count)
"Move the cursor COUNT screen lines down."
:type exclusive
(let ((line-move-visual t))
(evil-line-move (or count 1))))
(evil-define-motion evil-previous-visual-line (count)
"Move the cursor COUNT screen lines up."
:type exclusive
(let ((line-move-visual t))
(evil-line-move (- (or count 1)))))
;; Used for repeated commands like "dd"
(evil-define-motion evil-line (count)
"Move COUNT - 1 lines down."
:type line
(let (line-move-visual)
(evil-line-move (1- (or count 1)) t)))
(evil-define-motion evil-line-or-visual-line (count)
"Move COUNT - 1 lines down."
:type screen-line
(let ((line-move-visual (and evil-respect-visual-line-mode
visual-line-mode)))
(evil-line-move (1- (or count 1)) t)))
(evil-define-motion evil-beginning-of-line ()
"Move the cursor to the beginning of the current line."
:type exclusive
(move-beginning-of-line nil))
(evil-define-motion evil-end-of-line (count)
"Move the cursor to the end of the current line.
If COUNT is given, move COUNT - 1 lines downward first."
:type inclusive
(move-end-of-line count)
(when evil-track-eol
(setq temporary-goal-column most-positive-fixnum
this-command 'next-line))
(let ((evil-move-beyond-eol
(if (evil-visual-state-p) (not evil-v$-excludes-newline)
evil-move-beyond-eol)))
(evil-adjust-cursor))
;; Prevent "c$" and "d$" from deleting blank lines
(when (eolp) (setq evil-this-type 'exclusive)))
(evil-define-motion evil-beginning-of-visual-line ()
"Move the cursor to the first character of the current screen line."
:type exclusive
(beginning-of-visual-line))
(evil-define-motion evil-end-of-visual-line (count)
"Move the cursor to the last character of the current screen line.
If COUNT is given, move COUNT - 1 screen lines downward first."
:type inclusive
(end-of-visual-line count))
(evil-define-motion evil-end-of-line-or-visual-line (count)
"Move the cursor to the last character of the current screen
line if `visual-line-mode' is active and
`evil-respect-visual-line-mode' is non-nil. If COUNT is given,
move COUNT - 1 screen lines downward first."
:type inclusive
(if (and evil-respect-visual-line-mode
visual-line-mode)
(evil-end-of-visual-line count)
(evil-end-of-line count)))
(evil-define-motion evil-middle-of-visual-line ()
"Move the cursor to the middle of the current visual line."
:type exclusive
(beginning-of-visual-line)
(evil-with-restriction
nil
(save-excursion (end-of-visual-line) (point))
(move-to-column
(+ (current-column) -1 (/ (window-body-width) 2)))))
(evil-define-motion evil-percentage-of-line (count)
"Move the cursor to COUNT % of the width of the current line.
If no COUNT is given, default to 50%."
:type exclusive
(let ((line-length (- (line-end-position) (line-beginning-position)
(if evil-move-beyond-eol -1 0))))
(move-to-column (truncate (* line-length (or count 50)) 100))))
(evil-define-motion evil-first-non-blank ()
"Move the cursor to the first non-blank character of the current line."
:type exclusive
(evil-narrow-to-line (back-to-indentation)))
(evil-define-motion evil-last-non-blank (count)
"Move the cursor to the last non-blank character of the current line.
If COUNT is given, move COUNT - 1 lines downward first."
:type inclusive
(evil-move-end-of-line count)
(skip-chars-backward " \t")
(unless (bolp) (backward-char)))
(evil-define-motion evil-first-non-blank-of-visual-line ()
"Move the cursor to the first non blank character
of the current screen line."
:type exclusive
(evil-beginning-of-visual-line)
(skip-chars-forward " \t\r"))
(evil-define-motion evil-next-line-first-non-blank (count)
"Move the cursor COUNT lines down on the first non-blank character."
:type line
(let ((this-command this-command))
(evil-next-line (or count 1)))
(evil-first-non-blank))
(evil-define-motion evil-next-line-1-first-non-blank (count)
"Move the cursor COUNT-1 lines down on the first non-blank character."
:type line
(let ((this-command this-command))
(evil-next-line (1- (or count 1))))
(evil-first-non-blank))
(evil-define-motion evil-previous-line-first-non-blank (count)
"Move the cursor COUNT lines up on the first non-blank character."
:type line
(let ((this-command this-command))
(evil-previous-line (or count 1)))
(evil-first-non-blank))
(evil-define-motion evil-goto-line (count)
"Go to line COUNT. By default the last line."
:jump t
:type line
(evil-ensure-column
(if (null count)
(goto-char (point-max))
(goto-char (point-min))
(forward-line (1- count)))))
(evil-define-motion evil-goto-first-line (count)
"Go to line COUNT. By default the first line."
:jump t
:type line
(evil-goto-line (or count 1)))
(evil-define-motion evil-forward-word-begin (count &optional bigword)
"Move the cursor to the beginning of the COUNT-th next word.
If BIGWORD is non-nil, move by WORDS.
If this command is called in operator-pending state it behaves
differently. If point reaches the beginning of a word on a new
line point is moved back to the end of the previous line.
If called after a change operator, i.e. cw or cW,
`evil-want-change-word-to-end' is non-nil and point is on a word,
then both behave like ce or cE.
If point is at the end of the buffer and cannot be moved then
`end-of-buffer' is signaled."
:type exclusive
(let ((thing (if bigword 'evil-WORD 'evil-word))
(orig (point))
(count (or count 1)))
(evil-signal-at-bob-or-eob count)
(cond
;; default motion, beginning of next word
((not (evil-operator-state-p))
(evil-forward-beginning thing count))
;; the evil-change operator, maybe behave like ce or cE
((and evil-want-change-word-to-end
(memq evil-this-operator evil-change-commands)
(< orig (or (cdr-safe (bounds-of-thing-at-point thing)) orig)))
;; forward-thing moves point to the correct position because
;; this is an exclusive motion
(forward-thing thing count))
;; operator state
(t
(prog1 (evil-forward-beginning thing count)
;; if we reached the beginning of a word on a new line in
;; Operator-Pending state, go back to the end of the previous
;; line
(when (and (> (line-beginning-position) orig)
(looking-back "^[[:space:]]*" (line-beginning-position)))
;; move cursor back as long as the line contains only
;; whitespaces and is non-empty
(evil-move-end-of-line 0)
;; skip non-empty lines containing only spaces
(while (and (looking-back "^[[:space:]]+$" (line-beginning-position))
(not (<= (line-beginning-position) orig)))
(evil-move-end-of-line 0))
;; but if the previous line is empty, delete this line
(when (bolp) (forward-char))))))))
(evil-define-motion evil-forward-word-end (count &optional bigword)
"Move the cursor to the end of the COUNT-th next word.
If BIGWORD is non-nil, move by WORDS."
:type inclusive
(let ((thing (if bigword 'evil-WORD 'evil-word))
(count (or count 1)))
(evil-signal-at-bob-or-eob count)
;; Evil special behaviour: e or E on a one-character word in
;; operator state does not move point
(unless (and (evil-operator-state-p)
(= 1 count)
(let ((bnd (bounds-of-thing-at-point thing)))
(and bnd
(= (car bnd) (point))
(= (cdr bnd) (1+ (point)))))
(looking-at "[[:word:]]"))
(evil-forward-end thing count))))
(evil-define-motion evil-backward-word-begin (count &optional bigword)
"Move the cursor to the beginning of the COUNT-th previous word.
If BIGWORD is non-nil, move by WORDS."
:type exclusive
(let ((thing (if bigword 'evil-WORD 'evil-word)))
(evil-signal-at-bob-or-eob (- (or count 1)))
(evil-backward-beginning thing count)))
(evil-define-motion evil-backward-word-end (count &optional bigword)
"Move the cursor to the end of the COUNT-th previous word.
If BIGWORD is non-nil, move by WORDS."
:type inclusive
(let ((thing (if bigword 'evil-WORD 'evil-word)))
(evil-signal-at-bob-or-eob (- (or count 1)))
(evil-backward-end thing count)))
(evil-define-motion evil-forward-WORD-begin (count)
"Move the cursor to the beginning of the COUNT-th next WORD."
:type exclusive
(evil-forward-word-begin count t))
(evil-define-motion evil-forward-WORD-end (count)
"Move the cursor to the end of the COUNT-th next WORD."
:type inclusive
(evil-forward-word-end count t))
(evil-define-motion evil-backward-WORD-begin (count)
"Move the cursor to the beginning of the COUNT-th previous WORD."
:type exclusive
(evil-backward-word-begin count t))
(evil-define-motion evil-backward-WORD-end (count)
"Move the cursor to the end of the COUNT-th previous WORD."
:type inclusive
(evil-backward-word-end count t))
;; section movement
(evil-define-motion evil-forward-section-begin (count)
"Move the cursor to the beginning of the COUNT-th next section."
:jump t
:type exclusive
(evil-signal-at-bob-or-eob count)
(evil-forward-beginning 'evil-defun count))
(evil-define-motion evil-forward-section-end (count)
"Move the cursor to the end of the COUNT-th next section."
:jump t
:type inclusive
(evil-signal-at-bob-or-eob count)
(evil-forward-end 'evil-defun count)
(unless (eobp) (forward-line)))
(evil-define-motion evil-backward-section-begin (count)
"Move the cursor to the beginning of the COUNT-th previous section."
:jump t
:type exclusive
(evil-signal-at-bob-or-eob (- (or count 1)))
(evil-backward-beginning 'evil-defun count))
(evil-define-motion evil-backward-section-end (count)
"Move the cursor to the end of the COUNT-th previous section."
:jump t
:type inclusive
(evil-signal-at-bob-or-eob (- (or count 1)))
(end-of-line -1)
(evil-backward-end 'evil-defun count)
(unless (eobp) (forward-line)))
(evil-define-motion evil-forward-sentence-begin (count)
"Move to the next COUNT-th beginning of a sentence or end of a paragraph."
:jump t
:type exclusive
(evil-signal-at-bob-or-eob count)
(evil-forward-nearest count
#'(lambda (_cnt)
(evil-forward-beginning 'evil-sentence))
#'evil-forward-paragraph))
(evil-define-motion evil-backward-sentence-begin (count)
"Move to the previous COUNT-th beginning of a sentence or paragraph."
:jump t
:type exclusive
(evil-signal-at-bob-or-eob (- (or count 1)))
(evil-forward-nearest (- (or count 1))
#'(lambda (_cnt)
(evil-backward-beginning 'evil-sentence))
#'(lambda (_cnt)
(evil-backward-paragraph))))
(evil-define-motion evil-forward-paragraph (count)
"Move to the end of the COUNT-th next paragraph."
:jump t
:type exclusive
(evil-signal-at-bob-or-eob count)
(evil-forward-end 'evil-paragraph count)
(unless (eobp) (forward-line)))
(evil-define-motion evil-backward-paragraph (count)
"Move to the beginning of the COUNT-th previous paragraph."
:jump t
:type exclusive
(evil-signal-at-bob-or-eob (- (or count 1)))
(unless (eobp) (forward-line))
(evil-backward-beginning 'evil-paragraph count)
(unless (bobp) (forward-line -1)))
(defvar hif-ifx-else-endif-regexp)
(evil-define-motion evil-jump-item (count)
"Find the next item in this line after or under the cursor
and jump to the corresponding one."
:jump t
:type inclusive
(cond
;; COUNT% jumps to a line COUNT percentage down the file
(count
(evil-ensure-column
(goto-char
(let ((size (- (point-max) (point-min))))
(+ (point-min)
(if (> size 80000)
(* count (/ size 100))
(/ (* count size) 100))))))
(setq evil-this-type 'line))
((and (evil-looking-at-start-comment t)
(let ((pnt (point)))
(forward-comment 1)
(or (not (bolp))
(prog1 nil (goto-char pnt)))))
(backward-char))
((and (not (eolp)) (evil-looking-at-end-comment t))
(forward-comment -1))
((and
(memq major-mode '(c-mode c++-mode c-ts-mode c++-ts-mode))
(require 'hideif nil t)
(with-no-warnings
(let* ((hif-else-regexp (concat hif-cpp-prefix "\\(?:else\\|elif[ \t]+\\)"))
(hif-ifx-else-endif-regexp
(concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)))
(cond
((save-excursion (beginning-of-line) (or (hif-looking-at-ifX) (hif-looking-at-else)))
(hif-find-next-relevant)
(while (hif-looking-at-ifX)
(hif-ifdef-to-endif)
(hif-find-next-relevant))
t)
((save-excursion (beginning-of-line) (hif-looking-at-endif))
(hif-endif-to-ifdef)
t))))))
(t
(let* ((open (point-max))
(close (point-max))
(open-pair (ignore-errors
(save-excursion
;; consider the character right before eol given that
;; point may be placed there, e.g. in visual state
(when (and (eolp) (not (bolp)))
(backward-char))
(setq open (1- (scan-lists (point) 1 -1)))
(when (< open (line-end-position))
(goto-char open)
(forward-list)
(1- (point))))))
(close-pair (ignore-errors
(save-excursion
;; consider the character right before eol given that
;; point may be placed there, e.g. in visual state
(when (and (eolp) (not (bolp)))
(backward-char))
(setq close (1- (scan-lists (point) 1 1)))
(when (< close (line-end-position))
(goto-char (1+ close))
(backward-list)
(point))))))
(cond
((not (or open-pair close-pair))
;; nothing found, check if we are inside a string
(let ((pnt (point))
(bnd (bounds-of-thing-at-point 'evil-string)))
(if (not (and bnd (< (point) (cdr bnd))))
;; no, then we really failed
(user-error "No matching item found on the current line")
;; yes, go to the end of the string and try again
(let ((endstr (cdr bnd)))
(when (or (save-excursion
(goto-char endstr)
(let ((b (bounds-of-thing-at-point 'evil-string)))
(and b (< (point) (cdr b))))) ; not at end of string
(condition-case nil
(progn
(goto-char endstr)
(evil-jump-item)
nil)
(error t)))
;; failed again, go back to original point
(goto-char pnt)
(user-error "No matching item found on the current line"))))))
((< open close) (goto-char open-pair))
(t (goto-char close-pair)))))))
(declare-function flyspell-overlay-p "flyspell")
(evil-define-motion evil-next-flyspell-error (count)
"Go to the COUNT'th spelling mistake after point."
:jump t
(unless (bound-and-true-p flyspell-mode) (signal 'search-failed nil))
(let ((fwd (> (or count 1) 0)) (start (point)) (pos (point)) ov)
(dotimes (_ (abs (or count 1)))
(let ((limit (if fwd (point-max) (point-min))) wrappedp)
(when fwd (setq pos (save-excursion (goto-char pos)
(skip-syntax-forward "w") (point))))
(while (progn (if (if fwd (>= pos limit) (<= pos limit))
(if (or wrappedp (not evil-search-wrap))
(signal 'search-failed nil)
(setq wrappedp t
limit start
pos (if fwd (point-min)
(previous-overlay-change (point-max)))))
(setq pos (if fwd (next-overlay-change pos)
(previous-overlay-change pos))))
(not (setq ov (seq-find #'flyspell-overlay-p
(overlays-at pos))))))
(when wrappedp (let (message-log-max) (message "Search wrapped")))))
(goto-char (overlay-start ov))))
(evil-define-motion evil-prev-flyspell-error (count)
"Go to the COUNT'th spelling mistake preceding point."
:jump t
(evil-next-flyspell-error (- (or count 1))))
(evil-define-motion evil-previous-open-paren (count)
"Go to COUNT previous unmatched \"(\"."
:jump t
:type exclusive
(evil-up-paren ?\( ?\) (- (or count 1))))
(evil-define-motion evil-next-close-paren (count)
"Go to COUNT next unmatched \")\"."
:jump t
:type exclusive
(forward-char)
(evil-up-paren ?\( ?\) (or count 1))
(backward-char))
(evil-define-motion evil-previous-open-brace (count)
"Go to COUNT previous unmatched \"{\"."
:jump t
:type exclusive
(evil-up-paren ?{ ?} (- (or count 1))))
(evil-define-motion evil-next-close-brace (count)
"Go to COUNT next unmatched \"}\"."
:jump t
:type exclusive
(forward-char)
(evil-up-paren ?{ ?} (or count 1))
(backward-char))
(defun evil--lowercase-markers ()
"Get all lowercase markers."
(cl-remove-if-not (lambda (x) (and (markerp (cdr x))
(<= ?a (car x) ?z)))
evil-markers-alist))
(defun evil--next-mark (forwardp)
"Move to next lowercase mark.
Move forward if FORWARDP is non-nil, and backward otherwise. Loop back
to the beginning of buffer if the end is reached."
(let* ((pos (if forwardp (1+ (point)) (point)))
(centered-markers
(cl-sort
(or (evil--lowercase-markers) (user-error "No marks in this buffer"))
(if forwardp #'< #'>)
:key (lambda (x) (+ (if (< (cdr x) pos) 0 most-negative-fixnum)
(cdr x))))))
(goto-char (cdar centered-markers))))
(evil-define-motion evil-next-mark (count)
"Go to COUNT next lowercase mark."
:keep-visual t
:repeat nil
:type exclusive
:jump t
(dotimes (_ (or count 1))
(evil--next-mark t)))
(evil-define-motion evil-next-mark-line (count)
"Go to COUNT line of next lowercase mark after current line."
:keep-visual t
:repeat nil
:type exclusive
:jump t
(unless (evil--lowercase-markers) (user-error "No marks in this buffer"))
(dotimes (_ (or count 1))
(move-end-of-line nil)
(evil--next-mark t))
(evil-first-non-blank))
(evil-define-motion evil-previous-mark (count)
"Go to COUNT previous lowercase mark."
:keep-visual t
:repeat nil
:type exclusive
:jump t
(dotimes (_ (or count 1))
(evil--next-mark nil)))
(evil-define-motion evil-previous-mark-line (count)
"Go to COUNT line of previous lowercase mark before current line."
:keep-visual t
:repeat nil
:type exclusive
:jump t
(unless (evil--lowercase-markers) (user-error "No marks in this buffer"))
(dotimes (_ (or count 1))
(move-beginning-of-line nil)
(evil--next-mark nil))
(evil-first-non-blank))
(evil-define-command evil-set-col-0-mark (_beg end mark)
"Set MARK at column 0 of line of END.
Default is cursor line."
(interactive "<r><a>")
(if (< 1 (length mark))
(user-error "Trailing characters")
(save-excursion
(goto-char (if (eobp) end (1- end)))
(evil-beginning-of-line)
(evil-set-marker (string-to-char mark)))))
(evil-define-motion evil-find-char (count char)
"Move to the next COUNT'th occurrence of CHAR.
Movement is restricted to the current line unless `evil-cross-lines'
is non-nil."
:type inclusive
(interactive "<c><C>")
(setq count (or count 1))
(let ((fwd (> count 0))
(visual (and evil-respect-visual-line-mode
visual-line-mode))
case-fold-search)
(setq evil-last-find (list #'evil-find-char char fwd))
(when fwd (evil-forward-char 1 evil-cross-lines))
(unless (prog1
(search-forward
(char-to-string char)
(cond (evil-cross-lines nil)
((and fwd visual)
(save-excursion
(end-of-visual-line)
(point)))
(fwd (line-end-position))
(visual
(save-excursion
(beginning-of-visual-line)
(point)))
(t (line-beginning-position)))
t count)
(when fwd (backward-char)))
(user-error "Can't find `%c'" char))))
(evil-define-motion evil-find-char-backward (count char)
"Move to the previous COUNT'th occurrence of CHAR."
:type exclusive
(interactive "<c><C>")
(evil-find-char (- (or count 1)) char))
(evil-define-motion evil-find-char-to (count char)
"Move before the next COUNT'th occurrence of CHAR."
:type inclusive
(interactive "<c><C>")
(unwind-protect
(evil-find-char count char)
(setcar evil-last-find #'evil-find-char-to))
(if (> (or count 1) 0)
(backward-char)
(forward-char)))
(evil-define-motion evil-find-char-to-backward (count char)
"Move before the previous COUNT'th occurrence of CHAR."
:type exclusive
(interactive "<c><C>")
(evil-find-char-to (- (or count 1)) char))
(evil-define-motion evil-repeat-find-char (count)
"Repeat the last find COUNT times."
:type inclusive
(setq count (or count 1))
(cl-destructuring-bind (cmd char fwd)
(or evil-last-find (user-error "No previous search"))
;; ensure count is non-negative
(when (< count 0)
(setq count (- count)
fwd (not fwd)))
;; skip next character when repeating t or T
(and (eq cmd #'evil-find-char-to)
evil-repeat-find-to-skip-next
(= count 1)
(eql (if fwd (char-after (1+ (point))) (char-before)) char)
(setq count (1+ count)))
(let (evil-last-find)
(funcall cmd (if fwd count (- count)) char)
(unless (nth 2 evil-last-find)
(setq evil-this-type 'exclusive)))))
(evil-define-motion evil-repeat-find-char-reverse (count)
"Repeat the last find COUNT times in the opposite direction."
:type inclusive
(evil-repeat-find-char (- (or count 1))))
;; ceci n'est pas une pipe
(evil-define-motion evil-goto-column (count)
"Go to column COUNT on the current line.
Columns are counted from zero."
:type exclusive
(move-to-column (or count 0)))
(evil-define-command evil-goto-mark (char &optional noerror)
"Go to the marker specified by CHAR."
:keep-visual t
:repeat nil
:type exclusive
:jump t
(interactive (list (read-char)))
(let ((marker (evil-get-marker char)))
(cond
((markerp marker)
(switch-to-buffer (marker-buffer marker))
(goto-char marker))
((numberp marker) (goto-char marker))
((consp marker)
(when (or (find-buffer-visiting (car marker))
(and (y-or-n-p (format "Visit file %s again? "
(car marker)))
(find-file (car marker))))
(goto-char (cdr marker))))
((not noerror)
(user-error "Marker `%c' is not set%s" char
(if (evil-global-marker-p char) ""
" in this buffer"))))))
(evil-define-command evil-goto-mark-line (char &optional noerror)
"Go to the line of the marker specified by CHAR."
:keep-visual t
:repeat nil
:type line
:jump t
(interactive (list (read-char)))
(evil-goto-mark char noerror)
(evil-first-non-blank))
(declare-function goto-last-change "ext:goto-chg")
(declare-function goto-last-change-reverse "ext:goto-chg")
(evil-define-motion evil-goto-last-change (count)
"Like `goto-last-change' but takes a COUNT rather than a span."
(setq this-command 'goto-last-change)
(dotimes (_ (or count 1))
(goto-last-change nil)))
(evil-define-motion evil-goto-last-change-reverse (count)
"Like `goto-last-change-reverse' but takes a COUNT rather than a span."
(setq this-command 'goto-last-change-reverse)
(dotimes (_ (or count 1))
(goto-last-change-reverse nil)))
(evil-define-motion evil-jump-backward (count)
"Go to older position in jump list.
To go the other way, press \
\\<evil-motion-state-map>\\[evil-jump-forward]."
(evil--jump-backward count))
(evil-define-motion evil-jump-forward (count)
"Go to newer position in jump list.
To go the other way, press \
\\<evil-motion-state-map>\\[evil-jump-backward]."
(evil--jump-forward count))
(evil-define-motion evil-jump-backward-swap (_count)
"Go to the previous position in jump list.
The current position is placed in the jump list."
(let ((pnt (point)))
(evil--jump-backward 1)
(evil-set-jump pnt)))
(defvar xref-prompt-for-identifier)
(evil-define-motion evil-jump-to-tag (arg)
"Jump to tag under point.
If called with a prefix argument, provide a prompt
for specifying the tag."
:jump t
(interactive "P")
(cond
((fboundp 'xref-find-definitions)
(let ((xref-prompt-for-identifier arg))
(call-interactively #'xref-find-definitions)))
((fboundp 'find-tag)
(if arg (call-interactively #'find-tag)
(let ((tag (funcall (or find-tag-default-function
(get major-mode 'find-tag-default-function)
#'find-tag-default))))
(find-tag (or tag (user-error "No tag found around point"))))))))
(evil-define-motion evil-lookup ()
"Look up the keyword at point.
Calls `evil-lookup-func'."
(funcall evil-lookup-func))
(defun evil-ret-gen (count indent?)
(let ((widget (or (get-char-property (point) 'field)
(get-char-property (point) 'button)
(get-char-property (point) 'widget-doc))))
(cond
((and widget
(fboundp 'widget-type)
(fboundp 'widget-button-press)
(or (and (symbolp widget)
(get widget 'widget-type))
(and (consp widget)
(get (widget-type widget) 'widget-type))))
(when (evil-operator-state-p)
(setq evil-inhibit-operator t))
(when (fboundp 'widget-button-press)
(widget-button-press (point))))
((and (fboundp 'button-at)
(fboundp 'push-button)
(button-at (point)))
(when (evil-operator-state-p)
(setq evil-inhibit-operator t))
(push-button))
((or (evil-emacs-state-p)
(and (evil-insert-state-p)
(not buffer-read-only)))
(if (not indent?)
(newline count)
(delete-horizontal-space t)
(newline count)
(indent-according-to-mode)))
(t (evil-next-line-first-non-blank count)))))
(evil-define-motion evil-ret (count)
"Move the cursor COUNT lines down.
If point is on a widget or a button, click on it.
In Insert state, insert a newline."
:type line
(evil-ret-gen count nil))
(evil-define-motion evil-ret-and-indent (count)
"Move the cursor COUNT lines down.
If point is on a widget or a button, click on it.
In Insert state, insert a newline and indent."
:type line
(evil-ret-gen count t))
(evil-define-motion evil-window-top (count)
"Move the cursor to line COUNT from the top of the window."
:jump t
:type line
(evil-ensure-column
(move-to-window-line
(max (or count 0)
(if (= (point-min) (window-start)) 0 scroll-margin)))))
(evil-define-motion evil-window-middle ()
"Move the cursor to the middle line in the window."
:jump t
:type line
(evil-ensure-column (move-to-window-line nil)))
(evil-define-motion evil-window-bottom (count)
"Move the cursor to line COUNT from the bottom of the window."
:jump t
:type line
(evil-ensure-column
(move-to-window-line (- (max (or count 1) (1+ scroll-margin))))))
;; scrolling
(evil-define-command evil-scroll-line-up (count)
"Scroll the window COUNT lines upwards."
:repeat nil
:keep-visual t
(interactive "p")
(let (scroll-preserve-screen-position) (scroll-down count)))
(evil-define-command evil-scroll-line-down (count)
"Scroll the window COUNT lines downwards."
:repeat nil
:keep-visual t
(interactive "p")
(let (scroll-preserve-screen-position) (scroll-up count)))
(evil-define-command evil-scroll-count-reset ()
"Set `evil-scroll-count' to 0.
`evil-scroll-up' and `evil-scroll-down' will then scroll by half of
the screen (the default)."
:repeat nil
:keep-visual t
(interactive)
(setq evil-scroll-count 0))
(defun evil--get-scroll-count (count)
"Given a user-supplied COUNT, return scroll count."
(cl-flet ((posint (x) (and (natnump x) (< 0 x) x)))
(or (posint count)
(posint evil-scroll-count)
(/ (window-body-height) 2))))
;; With `scroll-preserve-screen-position' `scroll-up'/`scroll-down'
;; target the same cursor pixel Y-coordinate while `last-command' has
;; the `scroll-command' property. However the target needs updating
;; when e.g. scrolling maximally to the first or last line and then
;; switching scroll direction. Do this by resetting `last-command'
;; when `real-this-command' (`evil-ensure-column' modifies
;; `this-command') changed from last value:
(evil-define-command evil-scroll-up (count)
"Scroll the window and the cursor COUNT lines upwards.
If COUNT is not specified the function scrolls up `evil-scroll-count'
lines, which is the last used count.
If the scroll count is zero the command scrolls half the screen."
:repeat nil
:keep-visual t
(interactive "<c>")
(when (= (line-beginning-position) (point-min))
(signal 'beginning-of-buffer nil))
(setq count (evil--get-scroll-count count))
(evil-ensure-column
(let ((opoint (point)))
(condition-case nil
(let ((scroll-preserve-screen-position 'always)
(last-command (when (eq real-last-command real-this-command)
real-last-command)))
(scroll-down count))
(:success
;; Redo if `scroll-down' only did partial scroll up to BOB
(when (<= (window-start) (point-min))
(goto-char opoint)
(vertical-motion (- count))))
(beginning-of-buffer (vertical-motion (- count)))))))
(put 'evil-scroll-up 'scroll-command t)
(evil-define-command evil-scroll-down (count)
"Scroll the window and the cursor COUNT lines downwards.
If COUNT is not specified the function scrolls down
`evil-scroll-count' lines, which is the last used count.
If the scroll count is zero the command scrolls half the screen."
:repeat nil
:keep-visual t
(interactive "<c>")
(when (= (line-end-position) (point-max))
(signal 'end-of-buffer nil))
(setq count (evil--get-scroll-count count))
(evil-ensure-column
;; BUG #660: First check whether the eob is visible.
;; In that case we do not scroll but merely move point.
(if (<= (point-max) (window-end))
(vertical-motion count)
(condition-case nil
(let ((scroll-preserve-screen-position 'always)
(last-command (when (eq real-last-command real-this-command)
real-last-command)))
(scroll-up count))
(:success
;; If EOB became visible: Scroll it to the bottom
(save-excursion
(goto-char (window-start))
(vertical-motion (max 0 (- (window-height) 1 scroll-margin)))
(when (<= (point-max) (point)) (recenter -1))))
(end-of-buffer (goto-char (point-max)) (recenter -1))))))
(put 'evil-scroll-down 'scroll-command t)