Skip to content

Commit cab136e

Browse files
committed
Add interactive highlights for ex global command pattern
1 parent 27c9dce commit cab136e

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

evil-commands.el

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,6 +4211,29 @@ Use `evil-flush-lines' if INVERT is nil, or `evil-keep-lines' if not."
42114211
(goto-char end-marker)
42124212
(set-marker end-marker nil)))
42134213

4214+
(evil-ex-define-argument-type global
4215+
"This handler highlights the pattern in :g/pattern/cmd."
4216+
:runner
4217+
(lambda (flag &optional arg)
4218+
(with-current-buffer evil-ex-original-buffer
4219+
(cond
4220+
((eq flag 'start)
4221+
(evil-ex-make-hl 'evil-ex-global
4222+
:face 'evil-ex-global-command-matches
4223+
:win (minibuffer-selected-window))
4224+
(setq flag 'update))
4225+
((eq flag 'stop)
4226+
(evil-ex-delete-hl 'evil-ex-global)))
4227+
4228+
(when (and evil-ex-global-command-interactive-highlight
4229+
(eq flag 'update))
4230+
(condition-case err
4231+
(let ((pattern (car (evil-ex-parse-global (or arg "")))))
4232+
(when (length> pattern 0)
4233+
(evil-ex-hl-change 'evil-ex-global
4234+
(evil-ex-make-pattern pattern evil-ex-search-case nil))))
4235+
(user-error (evil-ex-echo (error-message-string err))))))))
4236+
42144237
(evil-define-operator evil-ex-global
42154238
(beg end pattern command &optional invert)
42164239
"The Ex global command.

evil-tests.el

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8734,17 +8734,39 @@ maybe we need one line more with some text\n")
87348734
(ert-deftest evil-test-global ()
87358735
"Test `evil-ex-global'."
87368736
:tags '(evil ex global)
8737+
8738+
(defun evil-test-force-invoke-ex-hl-update-timer ()
8739+
"Force invoke ex-hl-update-timer."
8740+
(when evil-ex-hl-update-timer
8741+
(timer-event-handler evil-ex-hl-update-timer)))
8742+
(advice-add 'evil-ex-hl-idle-update :after
8743+
#'evil-test-force-invoke-ex-hl-update-timer)
8744+
8745+
(defun evil-test-save-last-ex-global-hls ()
8746+
"Save current 'evil-ex-global highlights to buffer-local variable."
8747+
(when-let* ((hl (cdr (assq 'evil-ex-global evil-ex-active-highlights-alist)))
8748+
(ovs (evil-ex-hl-overlays hl)))
8749+
(setq ovs (sort ovs (lambda (a b) (< (overlay-start a) (overlay-start b)))))
8750+
(setq-local evil-test-last-ex-global-hls
8751+
(seq-map (lambda (ov) (buffer-substring-no-properties (overlay-start ov) (overlay-end ov)))
8752+
ovs))))
8753+
(advice-add 'evil-ex-hl-update-highlights :after
8754+
#'evil-test-save-last-ex-global-hls)
8755+
87378756
(ert-info ("global delete")
87388757
(evil-test-buffer
87398758
"[n]o 1\nno 2\nno 3\nyes 4\nno 5\nno 6\nno 7\n"
87408759
(":g/yes/d" [return])
8760+
(should (equal evil-test-last-ex-global-hls '("yes")))
87418761
"no 1\nno 2\nno 3\n[n]o 5\nno 6\nno 7\n"))
87428762
(ert-info ("global delete, specifying case sensitivty")
87438763
(evil-test-buffer
87448764
"[a]lpha bravo charlie\nalpha Bravo charlie\nalpha BRAVO charlie\nalpha delta charlie"
87458765
(":g/\\CBravo/d" [return])
8766+
(should (equal evil-test-last-ex-global-hls '("Bravo")))
87468767
"alpha bravo charlie\n[a]lpha BRAVO charlie\nalpha delta charlie"
87478768
(":g/\\cBravo/d" [return])
8769+
(should (equal evil-test-last-ex-global-hls '("bravo" "BRAVO")))
87488770
"alpha delta charlie"))
87498771
(ert-info ("global delete with arg")
87508772
(evil-test-buffer
@@ -8777,6 +8799,7 @@ maybe we need one line more with some text\n")
87778799
("/yes" [return])
87788800
"no 1\nno 2\nno 3\nyes 4\nno 5\nno 6\nno 7\n"
87798801
(":g//d" [return])
8802+
(should (equal evil-test-last-ex-global-hls '("yes")))
87808803
"no 1\nno 2\nno 3\n[n]o 5\nno 6\nno 7\n"
87818804
(":v//d" [return])
87828805
""))
@@ -8787,6 +8810,7 @@ maybe we need one line more with some text\n")
87878810
("/isearch" [return])
87888811
"no 1\nno 2\nno 3\nisearch 4\nno 5\nno 6\nno 7\n"
87898812
(":g//d" [return])
8813+
(should (equal evil-test-last-ex-global-hls '("isearch")))
87908814
"no 1\nno 2\nno 3\n[n]o 5\nno 6\nno 7\n"
87918815
(":v//d" [return])
87928816
""))
@@ -8796,27 +8820,35 @@ maybe we need one line more with some text\n")
87968820
(evil-test-buffer
87978821
"this\nThis\n"
87988822
(":g/this/d" [return])
8823+
(should (equal evil-test-last-ex-global-hls '("this")))
87998824
"This\n"))
88008825
(let ((evil-ex-search-case 'insensitive))
88018826
(evil-test-buffer
88028827
"this\nThis\n"
88038828
(":g/this/d" [return])
8829+
(should (equal evil-test-last-ex-global-hls '("this" "This")))
88048830
""))
88058831
(let ((evil-ex-search-case 'smart))
88068832
(evil-test-buffer
88078833
"this\nThis\n"
88088834
(":g/this/d" [return])
8835+
(should (equal evil-test-last-ex-global-hls '("this" "This")))
88098836
"")
88108837
(evil-test-buffer
88118838
"this\nThis\n"
88128839
(":g/This/d" [return])
8840+
(should (equal evil-test-last-ex-global-hls '("This")))
88138841
"this\n"))))
88148842
(ert-info (":global should transform vim-style regexp when appropriate")
88158843
(let ((evil-ex-search-vim-style-regexp t))
88168844
(evil-test-buffer
88178845
"a\n1\nb\n2\nc\n3\n"
88188846
(":g/\\d/>")
8819-
"a\n 1\nb\n 2\nc\n 3\n"))))
8847+
(should (equal evil-test-last-ex-global-hls '("1" "2" "3")))
8848+
"a\n 1\nb\n 2\nc\n 3\n")))
8849+
8850+
(advice-remove 'evil-ex-hl-idle-update #'evil-test-force-invoke-ex-hl-update-timer)
8851+
(advice-remove 'evil-ex-hl-update-highlights #'evil-test-save-last-ex-global-hls))
88208852

88218853
(ert-deftest evil-test-normal ()
88228854
"Test `evil-ex-normal'."

evil-types.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ If visual state is inactive then those values are nil."
388388

389389
(evil-define-interactive-code "<g/>"
390390
"Ex global argument."
391+
:ex-arg global
391392
(when evil-called-from-ex-p
392393
(evil-ex-parse-global (or evil-ex-argument ""))))
393394

evil-vars.el

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,15 @@ line. If this option is non-nil, this behavior is reversed."
13531353
"Face for interactive replacement text."
13541354
:group 'evil)
13551355

1356+
(defcustom evil-ex-global-command-interactive-highlight t
1357+
"If non-nil, pattern matches in Ex global commands are interactively highlighted."
1358+
:type 'boolean
1359+
:group 'evil)
1360+
1361+
(defface evil-ex-global-command-matches '((t :inherit lazy-highlight))
1362+
"Face for interactive global command matches."
1363+
:group 'evil)
1364+
13561365
(defcustom evil-command-window-height 7
13571366
"Height (in lines) of the command line window.
13581367
Set to 0 to use the default height for `split-window'."

0 commit comments

Comments
 (0)