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

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

[elpa] externals/m-buffer cf99d4209a 102/115: New function `m-buffer-mat


From: ELPA Syncer
Subject: [elpa] externals/m-buffer cf99d4209a 102/115: New function `m-buffer-match-multi'
Date: Tue, 19 Jul 2022 15:58:52 -0400 (EDT)

branch: externals/m-buffer
commit cf99d4209a3422cabe13420dd9b2cbfbb63f7bea
Author: Phillip Lord <phillip.lord@russet.org.uk>
Commit: Phillip Lord <phillip.lord@russet.org.uk>

    New function `m-buffer-match-multi'
---
 README.md              |  4 ++++
 m-buffer.el            | 37 ++++++++++++++++++++++++++++++++-----
 test/m-buffer-test.el  | 12 ++++++++++++
 test/one-two-three.txt |  4 ++++
 4 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md
index 9240cefa44..f583ab6c02 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,10 @@ with the FSF.
 
 ## Change Log
 
+### 0.14
+
+New function added `m-buffer-match-multi`
+
 ### 0.13
 
 New function added `m-buffer-at-string`
diff --git a/m-buffer.el b/m-buffer.el
index 47d82e7a7c..9cd3cbc93b 100644
--- a/m-buffer.el
+++ b/m-buffer.el
@@ -622,7 +622,6 @@ See also `replace-match'."
   ;; we have match-data
   (m-buffer-match-nth-group (or subexp 0) match-data))
 
-
 (defun m-buffer-delete-match (match-data &optional subexp)
   "Delete all MATCH-DATA.
 SUBEXP should be a number indicating the regexp group to delete.
@@ -746,15 +745,43 @@ MATCH is of form BUFFER-OR-WINDOW MATCH-OPTIONS.  See
 ;; first match by simply returning nil.
 
 ;; #+begin_src emacs-lisp
+(defun m-buffer-match-first (&rest match)
+  "Return the first match to MATCH.
+This matches more efficiently than matching all matches and
+taking the car. See `m-buffer-match' for further details of
+MATCH."
+  (m-buffer-apply-join
+   #'m-buffer-match match
+   :post-match (lambda () nil)))
+
 (defun m-buffer-match-first-line (&rest match)
   "Return a match to the first line of MATCH.
 This matches more efficiently than matching all lines and taking
 the car.  See `m-buffer-match' for further details of MATCH."
   (m-buffer-apply-join
-   'm-buffer-match match
-   :regexp m-buffer--line-regexp
-   :post-match (lambda () nil)))
-
+   'm-buffer-match-first match
+   :regexp m-buffer--line-regexp))
+
+(defun m-buffer-match-multi (regexps &rest match)
+  "Incrementally find matches to REGEXPS in MATCH.
+Finds the first match to the first element of regexps, then
+starting from the end of this match, the first match to the
+second element of regexps and so forth. See `m-buffer-match' for
+futher details of MATCH."
+  (when regexps
+      (let ((first-match
+             (m-buffer-apply-join
+              #'m-buffer-match-first
+              match
+              :regexp (car regexps))))
+        (append
+         first-match
+         (apply
+          #'m-buffer-match-multi
+          (cdr regexps)
+          (plist-put
+           match
+           :begin (car (m-buffer-match-end first-match))))))))
 ;; #+end_src
 
 ;; Emacs has a rather inconsistent interface here -- suddenly, we have a 
function
diff --git a/test/m-buffer-test.el b/test/m-buffer-test.el
index 029f5a1c51..7e00ee904a 100644
--- a/test/m-buffer-test.el
+++ b/test/m-buffer-test.el
@@ -217,6 +217,18 @@
       (m-buffer-match-first-line
        (current-buffer)))))))
 
+(ert-deftest multi-match ()
+  (should
+   (equal
+    '((1 4) (5 8) (13 18))
+    (m-buffer-wtb-of-file
+     "one-two-three.txt"
+     (m-buffer-marker-tree-to-pos
+      (m-buffer-match-multi
+       '("one" "two" "three")
+       :buffer (current-buffer))))))
+    )
+
 (ert-deftest sentence-end ()
   (should
    (equal
diff --git a/test/one-two-three.txt b/test/one-two-three.txt
new file mode 100644
index 0000000000..09cdb6c4bf
--- /dev/null
+++ b/test/one-two-three.txt
@@ -0,0 +1,4 @@
+one
+two
+one
+three



reply via email to

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