emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/evil e90aebc: Overhaul gn and gN + add tests (#1539)


From: ELPA Syncer
Subject: [nongnu] elpa/evil e90aebc: Overhaul gn and gN + add tests (#1539)
Date: Mon, 15 Nov 2021 17:57:46 -0500 (EST)

branch: elpa/evil
commit e90aebc4d0d03b308213d3fc39c660e706ace081
Author: Tom Dalziel <33435574+tomdl89@users.noreply.github.com>
Commit: GitHub <noreply@github.com>

    Overhaul gn and gN + add tests (#1539)
---
 evil-commands.el | 74 +++++++++++++++++++++++++++++++-------------------
 evil-tests.el    | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 28 deletions(-)

diff --git a/evil-commands.el b/evil-commands.el
index 147e6af..c910135 100644
--- a/evil-commands.el
+++ b/evil-commands.el
@@ -1383,39 +1383,57 @@ or line COUNT to the top of the window."
   :extend-selection nil
   (evil-select-xml-tag beg end type count))
 
-(evil-define-text-object evil-next-match (count &optional beg end type)
-  "Select next match."
+(defun evil-match (direction count)
+  "Find COUNTth next match in DIRECTION."
   (unless (and (boundp 'evil-search-module)
                (eq evil-search-module 'evil-search))
-    (user-error "next-match text objects only work with Evil search module."))
-  (let ((pnt (point)))
+    (user-error "Match text objects only work with Evil search module"))
+  (let ((pnt (point))
+        (count (abs count)) ;; Undo effect of evil-visual-direction
+        (evil-ex-search-direction 'backward)
+        (visual-state (evil-visual-state-p))
+        on-start-match in-match on-end-match)
+    (save-excursion
+      (unless (eobp) (forward-char)) ;; If on start of a match, stay there
+      (evil-ex-search 1)
+      (setq on-start-match (= evil-ex-search-match-beg pnt)
+            in-match (<= evil-ex-search-match-beg pnt (1- 
evil-ex-search-match-end))
+            on-end-match (= (1- evil-ex-search-match-end) pnt)
+            evil-ex-search-direction direction)
+      (cond
+       ((and visual-state on-start-match (eq 'backward direction))
+        (evil-ex-search count))
+       ((and visual-state on-end-match (eq 'forward direction))
+        (evil-ex-search count))
+       ((or in-match (eq 'backward direction))
+        (evil-ex-search (1- count)))
+       (t (evil-ex-search count)))
+      (setq pnt (point)))
+    (goto-char pnt)
     (cond
-     ((eq evil-ex-search-direction 'forward)
-      (unless (eobp) (forward-char))
-      (evil-ex-search-previous 1)
-      (when (and (<= evil-ex-search-match-beg pnt)
-                 (> evil-ex-search-match-end pnt)
-                 (not (evil-visual-state-p)))
-        (setq count (1- count)))
-      (if (> count 0) (evil-ex-search-next count)))
-     (t
-      (unless (eobp) (forward-char))
-      (evil-ex-search-next count))))
-  ;; active visual state if command is executed in normal state
-  (when (evil-normal-state-p)
-    (evil-visual-select evil-ex-search-match-beg evil-ex-search-match-end 
'inclusive +1 t))
-  (list evil-ex-search-match-beg evil-ex-search-match-end))
+     ((evil-normal-state-p)
+      (evil-visual-select evil-ex-search-match-beg
+                          evil-ex-search-match-end
+                          'inclusive
+                          (cl-case direction ('forward +1) ('backward -1))
+                          t)
+      (list evil-ex-search-match-beg evil-ex-search-match-end))
+     ((and visual-state (eq 'forward direction))
+      (goto-char (1- evil-ex-search-match-end)))
+     ((and visual-state (eq 'backward direction))
+      (goto-char evil-ex-search-match-beg))
+     ;; e.g. operator pending...
+     (t (list evil-ex-search-match-beg evil-ex-search-match-end)))))
 
-(evil-define-text-object evil-previous-match (count &optional beg end type)
+(evil-define-text-object evil-next-match (count &optional beg end type)
   "Select next match."
-  (unless (and (boundp 'evil-search-module)
-               (eq evil-search-module 'evil-search))
-    (user-error "previous-match text objects only work with Evil search 
module."))
-  (let ((evil-ex-search-direction
-         (if (eq evil-ex-search-direction 'backward)
-             'forward
-           'backward)))
-    (evil-next-match count beg end type)))
+  :extend-selection t
+  (evil-match 'forward count))
+
+(evil-define-text-object evil-previous-match (count &optional beg end type)
+  "Select previous match."
+  :extend-selection t
+  (evil-match 'backward count))
 
 ;;; Operator commands
 
diff --git a/evil-tests.el b/evil-tests.el
index 0ceb04d..9074e22 100644
--- a/evil-tests.el
+++ b/evil-tests.el
@@ -7806,6 +7806,89 @@ maybe we need one line more with some text\n"
         ("c/charlie" [return] "replacement " [escape] "4w.")
         "alpha replacement charlie delta golf replacement[ ]charlie india"))))
 
+(ert-deftest evil-test-ex-search-next+previous-match ()
+  :tags '(evil ex search)
+  (evil-without-display
+    (evil-select-search-module 'evil-search-module 'evil-search)
+    (ert-info ("evil-next-match in normal state")
+      (evil-test-buffer
+        "[b]ravo charlie delta charlie alpha charlie bravo"
+        ("/charlie" [return] "e")
+        "bravo charli[e] delta charlie alpha charlie bravo"
+        ("gn")
+        "bravo <charli[e]> delta charlie alpha charlie bravo"
+        ([escape] "b")
+        "bravo [c]harlie delta charlie alpha charlie bravo"
+        ("gn")
+        "bravo <charli[e]> delta charlie alpha charlie bravo"
+        ([escape] "w")
+        "bravo charlie [d]elta charlie alpha charlie bravo"
+        ("gn")
+        "bravo charlie delta <charli[e]> alpha charlie bravo"
+        ([escape] "^")
+        "[b]ravo charlie delta charlie alpha charlie bravo"
+        ("3gn")
+        "bravo charlie delta charlie alpha <charli[e]> bravo"))
+    (ert-info ("evil-previous-match in normal state")
+      (evil-test-buffer
+        "[b]ravo charlie delta charlie alpha charlie bravo"
+        ("/charlie" [return] "e")
+        "bravo charli[e] delta charlie alpha charlie bravo"
+        ("$gN")
+        "bravo charlie delta charlie alpha <[c]harlie> bravo"
+        ([escape] "gN")
+        "bravo charlie delta charlie alpha <[c]harlie> bravo"
+        ([escape] "e" "2gN")
+        "bravo charlie delta <[c]harlie> alpha charlie bravo"))
+    (ert-info ("evil-next-match in visual state")
+      (evil-test-buffer
+        "[b]ravo charlie delta charlie alpha charlie bravo"
+        ("/charlie" [return] "e")
+        "bravo charli[e] delta charlie alpha charlie bravo"
+        ("gn")
+        "bravo <charli[e]> delta charlie alpha charlie bravo"
+        ("gn")
+        "bravo <charlie delta charli[e]> alpha charlie bravo"
+        ("o")
+        "bravo <[c]harlie delta charlie> alpha charlie bravo"
+        ("gn")
+        "bravo charli<[e] delta charlie> alpha charlie bravo"
+        ([escape] "^v2gn")
+        "<bravo charlie delta charli[e]> alpha charlie bravo"))
+    (ert-info ("evil-previous-match in visual state")
+      (evil-test-buffer
+        "bravo charlie delta charlie alpha charlie brav[o]"
+        ("?charlie" [return])
+        "bravo charlie delta charlie alpha [c]harlie bravo"
+        ("gN")
+        "bravo charlie delta charlie alpha <[c]harlie> bravo"
+        ("gN")
+        "bravo charlie delta <[c]harlie alpha charlie> bravo"
+        ("o")
+        "bravo charlie delta <charlie alpha charli[e]> bravo"
+        ("gN")
+        "bravo charlie delta <charlie alpha [c]>harlie bravo"
+        ([escape] "$v2gN")
+        "bravo charlie delta <[c]harlie alpha charlie bravo>"))
+    (ert-info ("evil-match in operator state")
+      (evil-test-buffer
+        "[b]ravo charlie delta charlie alpha charlie bravo"
+        ("/charlie" [return])
+        "bravo [c]harlie delta charlie alpha charlie bravo"
+        ("cgn" "foo" [escape])
+        "bravo fo[o] delta charlie alpha charlie bravo"
+        (".")
+        "bravo foo delta fo[o] alpha charlie bravo"
+        ("$cgN" "bar" [escape])
+        "bravo foo delta foo alpha ba[r] bravo"))
+    (ert-info ("Unfound evil ex next match doesn't move cursor")
+      (evil-test-buffer
+       "[a]lpha bravo"
+       (should-error (execute-kbd-macro "/zulu"))
+       "[a]lpha bravo"
+       (should-error (execute-kbd-macro "gn"))
+       "[a]lpha bravo"))))
+
 (ert-deftest evil-test-isearch-word ()
   "Test isearch for word under point."
   :tags '(evil isearch)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]