forked from bonega/evil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevil-ex.el
1044 lines (965 loc) · 39.2 KB
/
evil-ex.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-ex.el --- Ex-mode
;; Author: Frank Fischer <frank fischer at mathematik.tu-chemnitz.de>
;; Maintainer: Vegard Øye <vegard_oye at hotmail.com>
;; Version: 0.0.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:
;; Ex is implemented as an extensible minilanguage, whose grammar
;; is stored in `evil-ex-grammar'. Ex commands are defined with
;; `evil-ex-define-cmd', which creates a binding from a string
;; to an interactive function. It is also possible to define key
;; sequences which execute a command immediately when entered:
;; such shortcuts go in `evil-ex-map'.
;;
;; To provide buffer and filename completion, as well as interactive
;; feedback, Ex defines the concept of an argument handler, specified
;; with `evil-ex-define-argument-type'. In the case of the
;; substitution command (":s/foo/bar"), the handler incrementally
;; highlights matches in the buffer as the substitution is typed.
(require 'evil-common)
(require 'evil-states)
;;; Code:
(defconst evil-ex-grammar
'((expression
(count command argument #'evil-ex-call-command)
((\? range) command argument #'evil-ex-call-command)
(line #'evil-goto-line)
(sexp #'eval-expression))
(count
number)
(command #'evil-ex-parse-command)
(binding
"[~&*@<>=:]+\\|[[:alpha:]-]+\\|!")
(bang
(\? (! space) "!" #'$1))
(argument
((\? space) (\? "\\(?:.\\|\n\\)+") #'$2))
(range
("%" #'(evil-ex-full-range))
(line (\? "[,;]" line #'$2) #'evil-ex-range))
(line
(base (\? offset) #'evil-ex-line)
((\? base) offset #'evil-ex-line))
(base
number
marker
search
("\\^" #'(evil-ex-first-line))
("\\$" #'(evil-ex-last-line))
("\\." #'(evil-ex-current-line)))
(offset
(+ signed-number #'+))
(marker
("'" "[-a-zA-Z_<>']" #'(evil-ex-marker $2)))
(search
forward
backward
next
prev
subst)
(forward
("/" "\\(?:[\\].\\|[^/,; ]\\)+" (! "/")
#'(evil-ex-re-fwd $2))
("/" "\\(?:[\\].\\|[^/]\\)+" "/"
#'(evil-ex-re-fwd $2)))
(backward
("\\?" "\\(?:[\\].\\|[^?,; ]\\)+" (! "\\?")
#'(evil-ex-re-bwd $2))
("\\?" "\\(?:[\\].\\|[^?]\\)+" "\\?"
#'(evil-ex-re-bwd $2)))
(next
"\\\\/" #'(evil-ex-prev-search))
(prev
"\\\\\\?" #'(evil-ex-prev-search))
(subst
"\\\\&" #'(evil-ex-prev-search))
(signed-number
(sign (\? number) #'evil-ex-signed-number))
(sign
"\\+\\|-" #'intern)
(number
"[0-9]+" #'string-to-number)
(space
"[ ]+")
(sexp
"(.*)" #'(car-safe (read-from-string $1))))
"Grammar for Ex.
An association list of syntactic symbols and their definitions.
The first entry is the start symbol. A symbol's definition may
reference other symbols, but the grammar cannot contain
left recursion. See `evil-parser' for a detailed explanation
of the syntax.")
(defun evil-ex-p ()
"Whether Ex is currently active."
(and evil-ex-current-buffer t))
(evil-define-command evil-ex (&optional initial-input)
"Enter an Ex command."
:keep-visual t
(interactive
(cond
((evil-visual-state-p)
'("'<,'>"))
(current-prefix-arg
(let ((arg (prefix-numeric-value current-prefix-arg)))
(cond ((< arg 0) (setq arg (1+ arg)))
((> arg 0) (setq arg (1- arg))))
(if (= arg 0) '(".")
`(,(format ".,.%+d" arg)))))))
(let ((minibuffer-local-completion-map evil-ex-completion-map)
(evil-ex-current-buffer (current-buffer))
(evil-ex-previous-command (unless initial-input
(car-safe evil-ex-history)))
evil-ex-argument-handler evil-ex-info-string result)
(add-hook 'minibuffer-setup-hook #'evil-ex-setup)
(setq result (read-from-minibuffer
":"
(or initial-input
(and evil-ex-previous-command
(format "(default: %s) "
evil-ex-previous-command)))
evil-ex-completion-map
nil
'evil-ex-history
evil-ex-previous-command
t))
(when (zerop (length result))
(setq result evil-ex-previous-command))
(evil-ex-update nil nil nil result)
(unless (zerop (length result))
(if evil-ex-expression
(eval evil-ex-expression)
(error "Ex: syntax error")))))
(defun evil-ex-delete-backward-char ()
"Close the minibuffer if it is empty.
Otherwise behaves like `delete-backward-char'."
(interactive)
(call-interactively
(if (zerop (length (minibuffer-contents)))
#'abort-recursive-edit
#'delete-backward-char)))
(defun evil-ex-setup ()
"Initialize Ex minibuffer."
(add-hook 'after-change-functions #'evil-ex-update nil t)
(add-hook 'minibuffer-exit-hook #'evil-ex-teardown)
(when evil-ex-previous-command
(add-hook 'pre-command-hook #'evil-ex-remove-default))
(remove-hook 'minibuffer-setup-hook #'evil-ex-setup)
(with-no-warnings
(make-variable-buffer-local 'completion-at-point-functions))
(setq completion-at-point-functions
'(evil-ex-completion-at-point)))
(put 'evil-ex-setup 'permanent-local-hook t)
(defun evil-ex-teardown ()
"Deinitialize Ex minibuffer."
(remove-hook 'minibuffer-exit-hook #'evil-ex-teardown)
(remove-hook 'after-change-functions #'evil-ex-update t)
(when evil-ex-argument-handler
(let ((runner (evil-ex-argument-handler-runner
evil-ex-argument-handler)))
(when runner
(funcall runner 'stop)))))
(put 'evil-ex-teardown 'permanent-local-hook t)
(defun evil-ex-remove-default ()
(delete-minibuffer-contents)
(remove-hook 'pre-command-hook #'evil-ex-remove-default))
(put 'evil-ex-remove-default 'permanent-local-hook t)
(defun evil-ex-update (&optional beg end len string)
"Update Ex variables when the minibuffer changes.
This function is usually called from `after-change-functions'
hook. If BEG is non-nil (which is the case when called from
`after-change-functions'), then an error description is shown
in case of incomplete or unknown commands."
(let* ((prompt (minibuffer-prompt-end))
(string (or string (buffer-substring prompt (point-max))))
arg bang cmd count expr func handler range tree type)
(cond
((and (eq this-command #'self-insert-command)
(commandp (setq cmd (lookup-key evil-ex-map string))))
(setq evil-ex-expression `(call-interactively #',cmd))
(when (minibufferp)
(exit-minibuffer)))
(t
(setq cmd nil)
;; store the buffer position of each character
;; as the `ex-index' text property
(dotimes (i (length string))
(add-text-properties
i (1+ i) (list 'ex-index (+ i prompt)) string))
(with-current-buffer evil-ex-current-buffer
(setq tree (evil-ex-parse string t)
expr (evil-ex-parse string))
(when (eq (car-safe expr) 'evil-ex-call-command)
(setq count (eval (nth 1 expr))
cmd (eval (nth 2 expr))
arg (eval (nth 3 expr))
range (cond
((evil-range-p count)
count)
((numberp count)
(evil-ex-range count count)))
bang (and (string-match ".!$" cmd) t))))
(setq evil-ex-tree tree
evil-ex-expression expr
evil-ex-range range
evil-ex-command cmd
evil-ex-bang bang
evil-ex-argument arg)
;; test the current command
(when (and cmd (minibufferp))
(setq func (evil-ex-completed-binding cmd t))
(cond
;; update argument-handler
(func
(when (setq type (evil-get-command-property
func :ex-arg))
(setq handler (cdr-safe
(assoc type
evil-ex-argument-types))))
(unless (eq handler evil-ex-argument-handler)
(let ((runner (and evil-ex-argument-handler
(evil-ex-argument-handler-runner
evil-ex-argument-handler))))
(when runner (funcall runner 'stop)))
(setq evil-ex-argument-handler handler)
(let ((runner (and evil-ex-argument-handler
(evil-ex-argument-handler-runner
evil-ex-argument-handler))))
(when runner (funcall runner 'start evil-ex-argument))))
(let ((runner (and evil-ex-argument-handler
(evil-ex-argument-handler-runner
evil-ex-argument-handler))))
(when runner (funcall runner 'update evil-ex-argument))))
((all-completions cmd evil-ex-commands)
;; show error message only when called from `after-change-functions'
(when beg (evil-ex-echo "Incomplete command")))
(t
;; show error message only when called from `after-change-functions'
(when beg (evil-ex-echo "Unknown command")))))))))
(put 'evil-ex-update 'permanent-local-hook t)
(defun evil-ex-echo (string &rest args)
"Display a message after the current Ex command."
(with-selected-window (minibuffer-window)
(with-current-buffer (window-buffer (minibuffer-window))
(unless (or evil-no-display
(zerop (length string)))
(let ((string (format " [%s]" (apply #'format string args)))
after-change-functions before-change-functions)
(put-text-property 0 (length string) 'face 'evil-ex-info string)
(minibuffer-message string))))))
(defun evil-ex-define-cmd (cmd function)
"Binds the function FUNCTION to the command CMD."
(if (string-match "^[^][]*\\(\\[\\(.*\\)\\]\\)[^][]*$" cmd)
(let ((abbrev (replace-match "" nil t cmd 1))
(full (replace-match "\\2" nil nil cmd 1)))
(evil-add-to-alist 'evil-ex-commands full function)
(evil-add-to-alist 'evil-ex-commands abbrev full))
(evil-add-to-alist 'evil-ex-commands cmd function)))
(defun evil-ex-make-argument-handler (runner completer)
(list runner completer))
(defun evil-ex-argument-handler-runner (arg-handler)
(car arg-handler))
(defun evil-ex-argument-handler-completer (arg-handler)
(cadr arg-handler))
(defmacro evil-ex-define-argument-type (arg-type doc &rest body)
"Defines a new handler for argument-type ARG-TYPE.
DOC is the documentation string. It is followed by a list of
keywords and function:
:completer FUNC Function to be called to initialize a
potential completion. FUNC must match the
requirements as described for the variable
`completion-at-point-functions'. When FUNC is
called the minibuffer content is narrowed to
exactly match the argument.
:runner FUNC Function to be called when the type of the
current argument changes or when the content
of this argument changes. This function
should take one obligatory argument FLAG
followed by an optional argument ARG. FLAG is
one of three symbol 'start, 'stop or
'update. When the argument type is recognized
for the first time and this handler is
started the FLAG is 'start. If the argument
type changes to something else or ex state
finished the handler FLAG is 'stop. If the
content of the argument has changed FLAG is
'update. If FLAG is either 'start or 'update
then ARG is the current value of this
argument. If FLAG is 'stop then arg is nil."
(declare (indent defun)
(debug (&define name
[&optional stringp]
[&rest [keywordp function-form]])))
(unless (stringp doc) (push doc body))
(let (runner completer)
(while (keywordp (car-safe body))
(let ((key (pop body))
(func (pop body)))
(cond
((eq key :runner)
(setq runner func))
((eq key :completer)
(setq completer func)))))
`(eval-and-compile
(evil-add-to-alist
'evil-ex-argument-types
',arg-type
'(,runner ,completer)))))
(defun evil-ex-filename-completion-at-point ()
"Completion at point function for file arguments."
(list
(point-min) (point-max)
(lambda (arg predicate flag)
(if (null arg)
default-directory
(let ((dir (or (file-name-directory arg)
(with-current-buffer evil-ex-current-buffer
default-directory)))
(fname (file-name-nondirectory arg)))
(cond
((null dir) (ding))
((null flag)
(let ((result (file-name-completion fname dir)))
(cond
((null result) nil)
((eq result t) t)
(t (concat dir result)))))
((eq t flag)
(file-name-all-completions fname dir))
((eq 'lambda flag)
(eq (file-name-completion fname dir) t))))))))
(evil-ex-define-argument-type file
"Handles a file argument."
:completer evil-ex-filename-completion-at-point)
(evil-ex-define-argument-type buffer
"Called to complete a buffer name argument."
:completer (lambda ()
(list
(point-min) (point-max)
(lambda (arg predicate flag)
(when arg
(let ((buffers (mapcar #'(lambda (buffer)
(cons (buffer-name buffer) nil))
(buffer-list t))))
(cond
((null flag)
(try-completion arg buffers predicate))
((eq t flag)
(all-completions arg buffers predicate))
((eq 'lambda flag)
(test-completion arg buffers predicate)))))))))
(declare-function shell-completion-vars "shell" ())
(defun evil-ex-init-shell-argument-completion (flag &optional arg)
"Prepares the current minibuffer for completion of shell commands.
This function must be called from the :runner function of some
argument handler that requires shell completion."
(when (and (eq flag 'start)
(not evil-ex-shell-argument-initialized)
(require 'shell nil t)
(require 'comint nil t))
(set (make-local-variable 'evil-ex-shell-argument-initialized) t)
(cond
;; Emacs 24
((fboundp 'comint-completion-at-point)
(shell-completion-vars))
(t
(set (make-local-variable 'minibuffer-default-add-function)
'minibuffer-default-add-shell-commands)))
(setq completion-at-point-functions
'(evil-ex-completion-at-point))))
;; because this variable is used only for Emacs 23 shell completion,
;; we put it here instead of "evil-vars.el"
(defvar evil-ex-shell-argument-range nil
"Internal helper variable for Emacs 23 shell completion.")
(defun evil-ex-complete-shell-command-at-point ()
"Completion at point function for shell commands."
(cond
;; Emacs 24
((fboundp 'comint-completion-at-point)
(comint-completion-at-point))
;; Emacs 23
((fboundp 'minibuffer-complete-shell-command)
(set (make-local-variable 'evil-ex-shell-argument-range)
(list (point-min) (point-max)))
#'(lambda ()
;; We narrow the buffer to the argument so
;; `minibuffer-complete-shell-command' will correctly detect
;; the beginning of the argument. When narrowing the buffer
;; to the argument the leading text in the minibuffer will be
;; hidden. Therefore we add a dummy overlay which shows that
;; text during narrowing.
(let* ((beg (car evil-ex-shell-argument-range))
(end (cdr evil-ex-shell-argument-range))
(prev-text (buffer-substring
(point-min)
(car evil-ex-shell-argument-range)))
(ov (make-overlay beg beg)))
(overlay-put ov 'before-string prev-text)
(save-restriction
(apply #'narrow-to-region evil-ex-shell-argument-range)
(minibuffer-complete-shell-command))
(delete-overlay ov))))))
(evil-ex-define-argument-type shell
"Shell argument type, supports completion."
:completer evil-ex-complete-shell-command-at-point
:runner evil-ex-init-shell-argument-completion)
(evil-ex-define-argument-type file-or-shell
"File or shell argument type.
If the current argument starts with a ! the rest of the argument
is considered a shell command, otherwise a file-name. Completion
works accordingly."
:completer (lambda ()
(if (and (< (point-min) (point-max))
(= (char-after (point-min)) ?!))
(save-restriction
(narrow-to-region (1+ (point-min)) (point-max))
(evil-ex-complete-shell-command-at-point))
(evil-ex-filename-completion-at-point)))
:runner evil-ex-init-shell-argument-completion)
(defun evil-ex-binding (command &optional noerror)
"Returns the final binding of COMMAND."
(let ((binding command))
(when binding
(string-match "^\\(.+?\\)\\!?$" binding)
(setq binding (match-string 1 binding))
(while (progn
(setq binding (cdr (assoc binding evil-ex-commands)))
(stringp binding)))
(unless binding
(setq binding (intern command)))
(if (commandp binding)
binding
(unless noerror
(error "Unknown command: `%s'" command))))))
(defun evil-ex-completed-binding (command &optional noerror)
"Returns the final binding of the completion of COMMAND."
(let ((completion (try-completion command evil-ex-commands)))
(evil-ex-binding (if (eq completion t) command
(or completion command))
noerror)))
;;; TODO: extensions likes :p :~ <cfile> ...
(defun evil-ex-replace-special-filenames (file-name)
"Replace special symbols in FILE-NAME.
Replaces % by the current file-name,
Replaces # by the alternate file-name in FILE-NAME."
(let ((current-fname (buffer-file-name))
(alternate-fname (and (other-buffer)
(buffer-file-name (other-buffer)))))
(when current-fname
(setq file-name
(replace-regexp-in-string "\\(^\\|[^\\\\]\\)\\(%\\)"
current-fname file-name
t t 2)))
(when alternate-fname
(setq file-name
(replace-regexp-in-string "\\(^\\|[^\\\\]\\)\\(#\\)"
alternate-fname file-name
t t 2)))
(setq file-name
(replace-regexp-in-string "\\\\\\([#%]\\)"
"\\1" file-name t)))
file-name)
(defun evil-ex-file-arg ()
"Returns the current Ex argument as a file name.
This function interprets special file names like # and %."
(unless (or (null evil-ex-argument)
(zerop (length evil-ex-argument)))
(evil-ex-replace-special-filenames evil-ex-argument)))
(defun evil-ex-run-completion-at-point ()
"Same as `completion-at-point' but disables `evil-ex-update' during call.
This function calls `evil-ex-update' explicitly when
`completion-at-point' finished."
(interactive)
(let ((after-change-functions
(remq 'evil-ex-update after-change-functions)))
(completion-at-point)
(evil-ex-update t)))
(defun evil-ex-completion-at-point ()
(let ((string (minibuffer-contents))
(prompt (minibuffer-prompt-end))
context start prefix)
(when (= (point) (+ (length string) prompt))
(evil-ex-update)
(setq context (evil-ex-syntactic-context (1- (point))))
(cond
((memq 'command context)
(setq start (or (get-text-property
0 'ex-index evil-ex-command)
(point))
prefix (buffer-substring prompt start))
(list start (point-max) #'evil-ex-complete-command))
((memq 'argument context)
(let ((arg (or evil-ex-argument "")))
(setq start (or (get-text-property
0 'ex-index arg)
(point))
prefix (buffer-substring prompt start))
(let* ((binding (evil-ex-completed-binding evil-ex-command))
(arg-type (evil-get-command-property binding :ex-arg))
(arg-handler (assoc arg-type evil-ex-argument-types))
(completer (and arg-handler
(evil-ex-argument-handler-completer
(cdr arg-handler)))))
(when completer
(save-restriction
(narrow-to-region start (point-max))
(funcall completer))))))))))
(defun evil-ex-complete-command (cmd predicate flag)
"Called to complete a command."
(cond
((eq flag nil)
(let ((result (try-completion cmd evil-ex-commands predicate)))
(if (and (eq result t) (evil-ex-command-force-p cmd))
cmd
result)))
((eq flag t)
(let ((result (all-completions cmd evil-ex-commands predicate))
new-result)
(mapc #'(lambda (x)
(push x new-result)
(when (evil-ex-command-force-p cmd)
(push (concat x "!") new-result)))
result)
new-result))
((eq flag 'lambda)
(test-completion cmd evil-ex-commands predicate))))
(defun evil-ex-repeat (count)
"Repeats the last ex command."
(interactive "P")
(when count
(goto-char (point-min))
(forward-line (1- count)))
(let ((evil-ex-current-buffer (current-buffer))
(hist evil-ex-history))
(while hist
(let ((evil-ex-last-cmd (pop hist)))
(when evil-ex-last-cmd
(evil-ex-update nil nil nil evil-ex-last-cmd)
(let ((binding (evil-ex-binding evil-ex-command)))
(unless (eq binding #'evil-ex-repeat)
(setq hist nil)
(if evil-ex-expression
(eval evil-ex-expression)
(error "Ex: syntax error")))))))))
(defun evil-ex-call-command (range command argument)
"Execute the given command COMMAND."
(let* ((count (when (numberp range) range))
(range (when (evil-range-p range) range))
(bang (and (string-match ".!$" command) t))
(evil-ex-range
(or range (and count (evil-ex-range count count))))
(evil-ex-command (evil-ex-completed-binding command))
(evil-ex-bang (and bang t))
(evil-ex-argument (copy-sequence argument))
(evil-this-type (evil-type evil-ex-range))
(current-prefix-arg count)
(prefix-arg current-prefix-arg))
(when (stringp evil-ex-argument)
(set-text-properties
0 (length evil-ex-argument) nil evil-ex-argument))
(let ((buf (current-buffer)))
(unwind-protect
(if (not evil-ex-range)
(call-interactively evil-ex-command)
;; set visual selection to match the region if an explicit
;; range has been specified
(let ((ex-range (evil-copy-range evil-ex-range))
beg end)
(evil-expand-range ex-range)
(setq beg (evil-range-beginning ex-range)
end (evil-range-end ex-range))
(evil-sort beg end)
(set-mark end)
(goto-char beg)
(activate-mark)
(call-interactively evil-ex-command)))
(when (buffer-live-p buf)
(with-current-buffer buf
(deactivate-mark)))))))
(defun evil-ex-line (base &optional offset)
"Return the line number of BASE plus OFFSET."
(+ (or base (line-number-at-pos))
(or offset 0)))
(defun evil-ex-first-line ()
"Return the line number of the first line."
(line-number-at-pos (point-min)))
(defun evil-ex-current-line ()
"Return the line number of the current line."
(line-number-at-pos (point)))
(defun evil-ex-last-line ()
"Return the line number of the last line."
(save-excursion
(goto-char (point-max))
(when (bolp)
(forward-line -1))
(line-number-at-pos)))
(defun evil-ex-range (beg-line &optional end-line)
"Returns the first and last position of the current range."
(evil-range
(evil-line-position beg-line)
(evil-line-position (or end-line beg-line) -1)
'line
:expanded t))
(defun evil-ex-full-range ()
"Return a range encompassing the whole buffer."
(evil-range (point-min) (point-max) 'line))
(defun evil-ex-marker (marker)
"Return MARKER's line number in the current buffer.
Signal an error if MARKER is in a different buffer."
(when (stringp marker)
(setq marker (aref marker 0)))
(setq marker (evil-get-marker marker))
(if (numberp marker)
(line-number-at-pos marker)
(error "Ex does not support markers in other files")))
(defun evil-ex-re-fwd (pattern)
"Search forward for PATTERN.
Returns the line number of the match."
(condition-case err
(save-excursion
(set-text-properties 0 (length pattern) nil pattern)
(evil-move-end-of-line)
(and (re-search-forward pattern nil t)
(line-number-at-pos (1- (match-end 0)))))
(invalid-regexp
(evil-ex-echo (cadr err))
nil)))
(defun evil-ex-re-bwd (pattern)
"Search backward for PATTERN.
Returns the line number of the match."
(condition-case err
(save-excursion
(set-text-properties 0 (length pattern) nil pattern)
(evil-move-beginning-of-line)
(and (re-search-backward pattern nil t)
(line-number-at-pos (match-beginning 0))))
(invalid-regexp
(evil-ex-echo (cadr err))
nil)))
(defun evil-ex-prev-search ()
(error "Previous search not yet implemented"))
(defun evil-ex-signed-number (sign &optional number)
"Return a signed number like -3 and +1.
NUMBER defaults to 1."
(funcall sign (or number 1)))
(defun evil-ex-eval (string &optional start)
"Evaluate STRING as an Ex command.
START is the start symbol, which defaults to `expression'."
;; disable the mark before executing, otherwise the visual region
;; may be used as operator range instead of the ex-range
(let ((form (evil-ex-parse string nil start))
transient-mark-mode deactivate-mark)
(eval form)))
(defun evil-ex-parse (string &optional syntax start)
"Parse STRING as an Ex expression and return an evaluation tree.
If SYNTAX is non-nil, return a syntax tree instead.
START is the start symbol, which defaults to `expression'."
(let* ((start (or start (car-safe (car-safe evil-ex-grammar))))
(match (evil-parser
string start evil-ex-grammar t syntax)))
(car-safe match)))
(defun evil-ex-parse-command (string)
"Parse STRING as an Ex binding."
(let ((result (evil-parser string 'binding evil-ex-grammar))
bang command)
(when result
(setq command (car-safe result)
string (cdr-safe result))
;; parse a following "!" as bang only if
;; the command has the property :ex-bang t
(when (evil-ex-command-force-p command)
(setq result (evil-parser string 'bang evil-ex-grammar)
bang (or (car-safe result) "")
string (cdr-safe result)
command (concat command bang)))
(cons command string))))
(defun evil-ex-command-force-p (command)
"Whether COMMAND accepts the bang argument."
(let ((binding (evil-ex-completed-binding command t)))
(when binding
(evil-get-command-property binding :ex-bang))))
(defun evil-flatten-syntax-tree (tree)
"Find all paths from the root of TREE to its leaves.
TREE is a syntax tree, i.e., all its leave nodes are strings.
The `nth' element in the result is the syntactic context
for the corresponding string index (counted from zero)."
(let* ((result nil)
(traverse nil)
(traverse
#'(lambda (tree path)
(if (stringp tree)
(dotimes (char (length tree))
(push path result))
(let ((path (cons (car tree) path)))
(dolist (subtree (cdr tree))
(funcall traverse subtree path)))))))
(funcall traverse tree nil)
(nreverse result)))
(defun evil-ex-syntactic-context (&optional pos)
"Return the syntactical context of the character at POS.
POS defaults to the current position of point."
(let* ((contexts (evil-flatten-syntax-tree evil-ex-tree))
(length (length contexts))
(pos (- (or pos (point)) (minibuffer-prompt-end))))
(when (>= pos length)
(setq pos (1- length)))
(when (< pos 0)
(setq pos 0))
(when contexts
(nth pos contexts))))
(defun evil-parser (string symbol grammar &optional greedy syntax)
"Parse STRING as a SYMBOL in GRAMMAR.
If GREEDY is non-nil, the whole of STRING must match.
If the parse succeeds, the return value is a cons cell
\(RESULT . TAIL), where RESULT is a parse tree and TAIL is
the remainder of STRING. Otherwise, the return value is nil.
GRAMMAR is an association list of symbols and their definitions.
A definition is either a list of production rules, which are
tried in succession, or a #'-quoted function, which is called
to parse the input.
A production rule can be one of the following:
nil matches the empty string.
A regular expression matches a substring.
A symbol matches a production for that symbol.
(X Y) matches X followed by Y.
(\\? X) matches zero or one of X.
(* X) matches zero or more of X.
(+ X) matches one or more of X.
(& X) matches X, but does not consume.
(! X) matches anything but X, but does not consume.
Thus, a simple grammar may look like:
((plus \"\\\\+\") ; plus <- \"+\"
(minus \"-\") ; minus <- \"-\"
(operator plus minus)) ; operator <- plus / minus
All input-consuming rules have a value. A regular expression evaluates
to the text matched, while a list evaluates to a list of values.
The value of a list may be overridden with a semantic action, which is
specified with a #'-quoted expression at the end:
(X Y #'foo)
The value of this rule is the result of calling foo with the values
of X and Y as arguments. Alternatively, the function call may be
specified explicitly:
(X Y #'(foo $1 $2))
Here, $1 refers to X and $2 refers to Y. $0 refers to the whole list.
Dollar expressions can also be used directly:
(X Y #'$1)
This matches X followed by Y, but ignores the value of Y;
the value of the list is the same as the value of X.
If the SYNTAX argument is non-nil, then all semantic actions
are ignored, and a syntax tree is constructed instead. The
syntax tree obeys the property that all the leave nodes are
parts of the input string. Thus, by traversing the syntax tree,
one can determine how each character was parsed.
The following symbols have reserved meanings within a grammar:
`\\?', `*', `+', `&', `!', `function', `alt', `seq' and nil."
(let ((string (or string ""))
func pair result rules tail)
(cond
;; epsilon
((member symbol '("" nil))
(setq pair (cons (if syntax "" nil) string)))
;; token
((stringp symbol)
(save-match-data
(when (or (eq (string-match symbol string) 0)
;; ignore leading whitespace
(and (eq (string-match "^[ \f\t\n\r\v]+" string) 0)
(eq (match-end 0)
(string-match
symbol string (match-end 0)))))
(setq result (match-string 0 string)
tail (substring string (match-end 0))
pair (cons result tail))
(when (and syntax pair)
(setq result (substring string 0
(- (length string)
(length tail))))
(setcar pair result)))))
;; symbol
((symbolp symbol)
(let ((context symbol))
(setq rules (cdr-safe (assq symbol grammar)))
(setq pair (evil-parser string `(alt ,@rules)
grammar greedy syntax))
(when (and syntax pair)
(setq result (car pair))
(if (and (listp result) (sequencep (car result)))
(setq result `(,symbol ,@result))
(setq result `(,symbol ,result)))
(setcar pair result))))
;; function
((eq (car-safe symbol) 'function)
(setq symbol (cadr symbol)
pair (funcall symbol string))
(when (and syntax pair)
(setq tail (or (cdr pair) "")
result (substring string 0
(- (length string)
(length tail))))
(setcar pair result)))
;; list
((listp symbol)
(setq rules symbol
symbol (car-safe rules))
(if (memq symbol '(& ! \? * + alt seq))
(setq rules (cdr rules))
(setq symbol 'seq))
(when (and (memq symbol '(+ alt seq))
(> (length rules) 1))
(setq func (car (last rules)))
(if (eq (car-safe func) 'function)
(setq rules (delq func (copy-sequence rules))
func (cadr func))
(setq func nil)))
(cond
;; positive lookahead
((eq symbol '&)
(when (evil-parser string rules grammar greedy syntax)
(setq pair (evil-parser string nil grammar nil syntax))))
;; negative lookahead
((eq symbol '!)
(unless (evil-parser string rules grammar greedy syntax)
(setq pair (evil-parser string nil grammar nil syntax))))
;; zero or one
((eq symbol '\?)
(setq rules (if (> (length rules) 1)
`(alt ,rules nil)
`(alt ,@rules nil))
pair (evil-parser string rules grammar greedy syntax)))
;; zero or more
((eq symbol '*)
(setq rules `(alt (+ ,@rules) nil)
pair (evil-parser string rules grammar greedy syntax)))
;; one or more
((eq symbol '+)
(let (current results)
(catch 'done
(while (setq current (evil-parser
string rules grammar nil syntax))
(setq result (car-safe current)
tail (or (cdr-safe current) "")
results (append results (if syntax result
(cdr-safe result))))
;; stop if stuck
(if (equal string tail)
(throw 'done nil)
(setq string tail))))
(when results
(setq func (or func 'list)
pair (cons results tail)))))
;; alternatives
((eq symbol 'alt)
(catch 'done
(dolist (rule rules)
(when (setq pair (evil-parser
string rule grammar greedy syntax))
(throw 'done pair)))))
;; sequence
(t
(setq func (or func 'list))
(let ((last (car-safe (last rules)))
current results rule)
(catch 'done
(while rules
(setq rule (pop rules)
current (evil-parser string rule grammar
(when greedy
(null rules))
syntax))
(cond
((null current)
(setq results nil)
(throw 'done nil))
(t
(setq result (car-safe current)
tail (cdr-safe current))
(unless (memq (car-safe rule) '(& !))
(if (and syntax
(or (null result)
(and (listp result)
(listp rule)
;; splice in single-element
;; (\? ...) expressions
(not (and (eq (car-safe rule) '\?)
(eq (length rule) 2))))))
(setq results (append results result))
(setq results (append results (list result)))))
(setq string (or tail ""))))))
(when results
(setq pair (cons results tail))))))
;; semantic action
(when (and pair func (not syntax))
(setq result (car pair))
(let* ((dexp
#'(lambda (obj)
(when (symbolp obj)
(let ((str (symbol-name obj)))
(when (string-match "\\$\\([0-9]+\\)" str)
(string-to-number (match-string 1 str)))))))
;; traverse a tree for dollar expressions
(dval nil)
(dval
#'(lambda (obj)
(if (listp obj)
(mapcar dval obj)
(let ((num (funcall dexp obj)))
(if num
(if (not (listp result))
result