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

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

[elpa] externals/el-search d2b8f84 184/332: New command `el-search-count


From: Stefan Monnier
Subject: [elpa] externals/el-search d2b8f84 184/332: New command `el-search-count-matches'
Date: Tue, 1 Dec 2020 15:48:40 -0500 (EST)

branch: externals/el-search
commit d2b8f8429356c0f5a89c80810e461f7bd4936edc
Author: Michael Heerdegen <michael_heerdegen@web.de>
Commit: Michael Heerdegen <michael_heerdegen@web.de>

    New command `el-search-count-matches'
---
 el-search.el | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/el-search.el b/el-search.el
index 824996c..63276c0 100644
--- a/el-search.el
+++ b/el-search.el
@@ -7,7 +7,7 @@
 ;; Created: 29 Jul 2015
 ;; Keywords: lisp
 ;; Compatibility: GNU Emacs 25
-;; Version: 1.4.0.1
+;; Version: 1.4.0.2
 ;; Package-Requires: ((emacs "25") (stream "2.2.4"))
 
 
@@ -1234,6 +1234,38 @@ PATTERN and combining the heuristic matchers of the 
subpatterns."
   (el-search--next-buffer el-search--current-search predicate)
   (el-search-continue-search))
 
+;;;###autoload
+(defun el-search-count-matches (pattern &optional rstart rend interactive)
+  "Like `count-matches' but accepting an el-search PATTERN instead of a regexp.
+
+Unlike `count-matches' matches \"inside\" other matches also count."
+  (interactive (list (el-search--read-pattern-for-interactive "How many 
matches for pattern: ")
+                     nil nil t))
+  ;; Code is mainly adopted from `count-matches'
+  (save-excursion
+    (if rstart
+        (if rend
+            (progn
+              (goto-char (min rstart rend))
+              (setq rend (max rstart rend)))
+          (goto-char rstart)
+          (setq rend (point-max)))
+      (if (and interactive (use-region-p))
+         (setq rstart (region-beginning)
+               rend (region-end))
+       (setq rstart (point)
+             rend (point-max)))
+      (goto-char rstart))
+    (let ((count 0)
+          (matcher  (el-search--matcher          pattern))
+          (hmatcher (el-search-heuristic-matcher pattern)))
+      (while (and (< (point) rend)
+                 (el-search--search-pattern-1 matcher t rend hmatcher))
+       (cl-incf count)
+       (el-search--skip-expression nil t))
+      (when interactive (message "%d occurrence%s" count (if (= count 1) "" 
"s")))
+      count)))
+
 (defun el-search--all-matches (search)
   "Return a stream of all matches of SEARCH.
 The returned stream will always start searching from the



reply via email to

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