branch: externals/m-buffer
commit 761ba8e4b7c770647d23a91871365e7bc6d9305b
Author: Phillip Lord <[email protected]>
Commit: Phillip Lord <[email protected]>
Equals and subtract functionality added.
---
m-buffer.el | 40 ++++++++++++++++++++++++++++++++++++++++
test/m-buffer-test.el | 30 +++++++++++++++++++++++++++++-
2 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/m-buffer.el b/m-buffer.el
index 185e35f981..7ca2dafab6 100644
--- a/m-buffer.el
+++ b/m-buffer.el
@@ -297,6 +297,46 @@ function. See `m-buffer-nil-marker' for details."
(m-buffer-marker-to-pos-nil
(apply 'm-buffer-match-end match)))
+(defun m-buffer-match-equal (m n)
+ "Returns true if m and n are cover the same region.
+Matches are equal if they match the same region; subgroups are
+ignored."
+ ;; can we speed this up by not making subsets?
+ (equal
+ (-take 2 m)
+ (-take 2 n)))
+
+(defun m-buffer-match-subtract (m n)
+ "Remove from M any matches in N.
+Matches are equivalent if overall they match the same
+area; subgroups are ignored.
+See also `m-buffer-match-exact-subtract' which often
+runs faster but has some restrictions."
+ (-remove
+ (lambda (o)
+ (-any?
+ (lambda (p)
+ (m-buffer-match-equal o p))
+ n))
+ m))
+
+(defun m-buffer-match-exact-subtract (m n)
+ "Remove from M any matches in N.
+Both M and N must be fully ordered, and any element in N must be
+in M."
+ ;; copy n
+ (let ((n-eaten n))
+ (-remove
+ (lambda (o)
+ ;; check the first element of n
+ (when (m-buffer-match-equal
+ (car n-eaten) o)
+ ;; we have a match so throw away the first element of n-eaten
+ ;; which we won't need again.
+ (setq n-eaten (-drop 1 n-eaten))
+ t))
+ m)))
+
;; marker/position utility functions
(defun m-buffer-nil-marker (markers)
"Takes a (nested) list of markers and nils them all.
diff --git a/test/m-buffer-test.el b/test/m-buffer-test.el
index 63ec642c8e..0dd7b5aa86 100644
--- a/test/m-buffer-test.el
+++ b/test/m-buffer-test.el
@@ -157,7 +157,6 @@
(m-buffer-nil-marker
(m-buffer-match-begin (current-buffer) "^one$"))))))
-
(ert-deftest replace-matches ()
(should
(equal
@@ -307,4 +306,33 @@
(current-buffer)
"A"
:case-fold-search t))))))
+
+
+(ert-deftest subtract ()
+ (should
+ (equal
+ '((1 6) (17 23) (34 39))
+ (m-buffer-wtb-of-file
+ "sentence-end.txt"
+ (m-buffer-marker-tree-to-pos
+ (m-buffer-match-subtract
+ (m-buffer-match-word
+ (current-buffer))
+ (m-buffer-match
+ (current-buffer) "sentence")))))))
+
+(ert-deftest exact-substract ()
+ (should
+ (equal
+ '((1 6) (17 23) (34 39))
+ (m-buffer-wtb-of-file
+ "sentence-end.txt"
+ (m-buffer-marker-tree-to-pos
+ (m-buffer-match-exact-subtract
+ (m-buffer-match-word
+ (current-buffer))
+ (m-buffer-match
+ (current-buffer) "sentence")))))))
+
+
;;; m-buffer-test.el ends here