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

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

[elpa] externals/m-buffer ea281db21a 089/115: New function: m-buffer-par


From: ELPA Syncer
Subject: [elpa] externals/m-buffer ea281db21a 089/115: New function: m-buffer-partition-by-marker.
Date: Tue, 19 Jul 2022 15:58:51 -0400 (EDT)

branch: externals/m-buffer
commit ea281db21af0dd48ba08f7f8e0cd2902acdf5a96
Author: Phillip Lord <phillip.lord@newcastle.ac.uk>
Commit: Phillip Lord <phillip.lord@newcastle.ac.uk>

    New function: m-buffer-partition-by-marker.
---
 m-buffer.el           | 32 ++++++++++++++++++++++++++++++++
 test/m-buffer-test.el | 14 ++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/m-buffer.el b/m-buffer.el
index 21e8efae60..948033282f 100644
--- a/m-buffer.el
+++ b/m-buffer.el
@@ -405,6 +405,8 @@ function. See `m-buffer-nil-marker' for details."
 
 ;; ** Match Utility and Predicates
 
+;; *** Subtraction
+
 ;; Some predicates and the ability to subtract to lists of matches from each
 ;; other. This makes up for limitations in Emacs regexp which can't do "match x
 ;; but not y".
@@ -492,6 +494,36 @@ in M."
    matches))
 ;; #+end_src
 
+
+;; *** Partition
+
+;; Partition one set of markers by another. This is useful for finding matched
+;; pairs of markers.
+
+;; #+begin_src emacs-lisp
+(defun m-buffer--split-partition (partition)
+  (let ((current nil))
+    (lambda (n)
+      (when
+          (and partition
+               (<= (car partition) n))
+        (setq current (car partition))
+        (setq partition (-drop 1 partition)))
+      current)))
+
+(defun m-buffer-partition-by-marker (list partition)
+  "Given LIST of markers, split at markers in PARTITION.
+Returns a list of lists. The first element of each list is nil or
+the marker from PARTITION. The rest of the elements are those
+elements in LIST which are at the same position or later in the
+buffer than the element from PARTITION, but before the next
+element from PARTITION.
+
+Both LIST and PARTITION must be sorted."
+  (-group-by (m-buffer--split-partition partition) list))
+;; #+end_src
+
+
 ;; ** Marker manipulation functions
 
 ;; These functions do things to markers rather than the areas of the buffers
diff --git a/test/m-buffer-test.el b/test/m-buffer-test.el
index f81763fdef..afe828374b 100644
--- a/test/m-buffer-test.el
+++ b/test/m-buffer-test.el
@@ -407,4 +407,18 @@ should not have moved."
    (m-buffer-match-word
     (current-buffer) :regexp "notword")))
 
+
+(ert-deftest partition-by-markers ()
+  (should
+   (equal
+    '((nil 1)
+      (2 2 3 4)
+      (5 5 6 7)
+      (8 8 9))
+    (m-buffer-partition-by-marker
+     '(1 2 3 4 5 6 7 8 9)
+     '(2 5 8)
+     ))))
+
+
 ;;; m-buffer-test.el ends here



reply via email to

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