Skip to content

Commit 2343a2c

Browse files
committed
Add interactive highlights for ex global command pattern
1 parent 35fe630 commit 2343a2c

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
@@ -4233,6 +4233,29 @@ Use `evil-flush-lines' if INVERT is nil, or `evil-keep-lines' if not."
42334233
(goto-char end-marker)
42344234
(set-marker end-marker nil)))
42354235

4236+
(evil-ex-define-argument-type global
4237+
"This handler highlights the pattern in :g/pattern/cmd."
4238+
:runner
4239+
(lambda (flag &optional arg)
4240+
(with-current-buffer evil-ex-original-buffer
4241+
(cond
4242+
((eq flag 'start)
4243+
(evil-ex-make-hl 'evil-ex-global
4244+
:face 'evil-ex-global-command-matches
4245+
:win (minibuffer-selected-window))
4246+
(setq flag 'update))
4247+
((eq flag 'stop)
4248+
(evil-ex-delete-hl 'evil-ex-global)))
4249+
4250+
(when (and evil-ex-global-command-interactive-highlight
4251+
(eq flag 'update))
4252+
(condition-case err
4253+
(let ((pattern (car (evil-ex-parse-global (or arg "")))))
4254+
(when (length> pattern 0)
4255+
(evil-ex-hl-change 'evil-ex-global
4256+
(evil-ex-make-pattern pattern evil-ex-search-case nil))))
4257+
(user-error (evil-ex-echo (error-message-string err))))))))
4258+
42364259
(evil-define-operator evil-ex-global
42374260
(beg end pattern command &optional invert)
42384261
"The Ex global command.

evil-tests.el

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8865,17 +8865,39 @@ maybe we need one line more with some text\n")
88658865
(ert-deftest evil-test-global ()
88668866
"Test `evil-ex-global'."
88678867
:tags '(evil ex global)
8868+
8869+
(defun evil-test-force-invoke-ex-hl-update-timer ()
8870+
"Force invoke ex-hl-update-timer."
8871+
(when evil-ex-hl-update-timer
8872+
(timer-event-handler evil-ex-hl-update-timer)))
8873+
(advice-add 'evil-ex-hl-idle-update :after
8874+
#'evil-test-force-invoke-ex-hl-update-timer)
8875+
8876+
(defun evil-test-save-last-ex-global-hls ()
8877+
"Save current 'evil-ex-global highlights to buffer-local variable."
8878+
(when-let* ((hl (cdr (assq 'evil-ex-global evil-ex-active-highlights-alist)))
8879+
(ovs (evil-ex-hl-overlays hl)))
8880+
(setq ovs (sort ovs (lambda (a b) (< (overlay-start a) (overlay-start b)))))
8881+
(setq-local evil-test-last-ex-global-hls
8882+
(seq-map (lambda (ov) (buffer-substring-no-properties (overlay-start ov) (overlay-end ov)))
8883+
ovs))))
8884+
(advice-add 'evil-ex-hl-update-highlights :after
8885+
#'evil-test-save-last-ex-global-hls)
8886+
88688887
(ert-info ("global delete")
88698888
(evil-test-buffer
88708889
"[n]o 1\nno 2\nno 3\nyes 4\nno 5\nno 6\nno 7\n"
88718890
(":g/yes/d" [return])
8891+
(should (equal evil-test-last-ex-global-hls '("yes")))
88728892
"no 1\nno 2\nno 3\n[n]o 5\nno 6\nno 7\n"))
88738893
(ert-info ("global delete, specifying case sensitivty")
88748894
(evil-test-buffer
88758895
"[a]lpha bravo charlie\nalpha Bravo charlie\nalpha BRAVO charlie\nalpha delta charlie"
88768896
(":g/\\CBravo/d" [return])
8897+
(should (equal evil-test-last-ex-global-hls '("Bravo")))
88778898
"alpha bravo charlie\n[a]lpha BRAVO charlie\nalpha delta charlie"
88788899
(":g/\\cBravo/d" [return])
8900+
(should (equal evil-test-last-ex-global-hls '("bravo" "BRAVO")))
88798901
"alpha delta charlie"))
88808902
(ert-info ("global delete with arg")
88818903
(evil-test-buffer
@@ -8908,6 +8930,7 @@ maybe we need one line more with some text\n")
89088930
("/yes" [return])
89098931
"no 1\nno 2\nno 3\nyes 4\nno 5\nno 6\nno 7\n"
89108932
(":g//d" [return])
8933+
(should (equal evil-test-last-ex-global-hls '("yes")))
89118934
"no 1\nno 2\nno 3\n[n]o 5\nno 6\nno 7\n"
89128935
(":v//d" [return])
89138936
""))
@@ -8918,6 +8941,7 @@ maybe we need one line more with some text\n")
89188941
("/isearch" [return])
89198942
"no 1\nno 2\nno 3\nisearch 4\nno 5\nno 6\nno 7\n"
89208943
(":g//d" [return])
8944+
(should (equal evil-test-last-ex-global-hls '("isearch")))
89218945
"no 1\nno 2\nno 3\n[n]o 5\nno 6\nno 7\n"
89228946
(":v//d" [return])
89238947
""))
@@ -8927,27 +8951,35 @@ maybe we need one line more with some text\n")
89278951
(evil-test-buffer
89288952
"this\nThis\n"
89298953
(":g/this/d" [return])
8954+
(should (equal evil-test-last-ex-global-hls '("this")))
89308955
"This\n"))
89318956
(let ((evil-ex-search-case 'insensitive))
89328957
(evil-test-buffer
89338958
"this\nThis\n"
89348959
(":g/this/d" [return])
8960+
(should (equal evil-test-last-ex-global-hls '("this" "This")))
89358961
""))
89368962
(let ((evil-ex-search-case 'smart))
89378963
(evil-test-buffer
89388964
"this\nThis\n"
89398965
(":g/this/d" [return])
8966+
(should (equal evil-test-last-ex-global-hls '("this" "This")))
89408967
"")
89418968
(evil-test-buffer
89428969
"this\nThis\n"
89438970
(":g/This/d" [return])
8971+
(should (equal evil-test-last-ex-global-hls '("This")))
89448972
"this\n"))))
89458973
(ert-info (":global should transform vim-style regexp when appropriate")
89468974
(let ((evil-ex-search-vim-style-regexp t))
89478975
(evil-test-buffer
89488976
"a\n1\nb\n2\nc\n3\n"
89498977
(":g/\\d/>")
8950-
"a\n 1\nb\n 2\nc\n 3\n"))))
8978+
(should (equal evil-test-last-ex-global-hls '("1" "2" "3")))
8979+
"a\n 1\nb\n 2\nc\n 3\n")))
8980+
8981+
(advice-remove 'evil-ex-hl-idle-update #'evil-test-force-invoke-ex-hl-update-timer)
8982+
(advice-remove 'evil-ex-hl-update-highlights #'evil-test-save-last-ex-global-hls))
89518983

89528984
(ert-deftest evil-test-normal ()
89538985
"Test `evil-ex-normal'."

evil-types.el

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

403403
(evil-define-interactive-code "<g/>"
404404
"Ex global argument."
405+
:ex-arg global
405406
(when evil-called-from-ex-p
406407
(evil-ex-parse-global (or evil-ex-argument ""))))
407408

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)