all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer
@ 2017-04-02 12:41 Tino Calancha
  2017-04-02 15:57 ` Dmitry Gutov
                   ` (2 more replies)
  0 siblings, 3 replies; 31+ messages in thread
From: Tino Calancha @ 2017-04-02 12:41 UTC (permalink / raw)
  To: 26338; +Cc: juri linkov

X-Debbugs-CC: Juri Linkov <juri@linkov.net>
Severity: wishlist

Hi,

we have `count-matches' in replace.el, which returns the
number of matches of a regexp.  Why not to have an standard
function `collect-matches' as well?

I know `xref-collect-matches' but it uses grep program: some users might
not have grep installed, or they may prefer to use Emacs regexps.

I've being using for a while something similar than the patch below.
Probably it doesn't need to be a command, just a normal function.

What do you think?
Regards,
Tino
--8<-----------------------------cut here---------------start------------->8---
commit ccc78b19aa044f6bdb27875937320ed06c2b517a
Author: Tino Calancha <tino.calancha@gmail.com>
Date:   Sun Apr 2 21:37:19 2017 +0900

    collect-matches: New command
    
    Collect all matches for REGEXP in current buffer (Bug#26338).
    * lisp/replace.el (collect-matches): New command.

diff --git a/lisp/replace.el b/lisp/replace.el
index a7b8ae6a34..6f2c6c9a2b 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -1002,6 +1002,44 @@ how-many
 				 (if (= count 1) "" "s")))
       count)))
 
+(defun collect-matches (regexp &optional region group limit)
+  "Collect matches for REGEXP following point.
+Optional arg REGION, if non-nil, mean restrict search to the
+ specified region.  Otherwise search the entire buffer.
+ REGION must be a list of (START . END) positions as returned by
+ `region-bounds'.
+Interactively, in Transient Mark mode when the mark is active, operate
+ on the contents of the region.  Otherwise, operate from point to the
+ end of (the accessible portion of) the buffer.
+Optional GROUP if non-nil, then is the regexp group to save.  Otherwise,
+ save the whole match.  Interactively, a numeric prefix set GROUP.
+Optional LIMIT if non-nil, then stop after such number of matches.
+ Otherwise collect all of them."
+  (interactive
+   (list (read-regexp "Collect matches for regexp: ")
+         (and (use-region-p) (region-bounds))
+         (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 0)
+         nil))
+  (unless group (setq group 0))
+  (let* ((count 0)
+         (start (if region (max (caar region) (point-min)) (point)))
+         (end (if region (min (cdar region) (point-max)) (point-max)))
+         res)
+    (save-excursion
+      (goto-char start)
+      (catch '--collect-matches-end
+        (while (re-search-forward regexp nil t)
+          (unless (>= (point) end)
+            (push (match-string-no-properties group) res)
+            (cl-incf count))
+          (when (or (>= (point) end)
+                    (and (natnump limit) (>= count limit)))
+            (throw '--collect-matches-end nil)))))
+    (message "%d Match%s: %s"
+             count (if (= count 1) "" "es")
+             (mapconcat 'identity (setq res (nreverse res)) " "))
+    res))
+
 \f
 (defvar occur-menu-map
   (let ((map (make-sparse-keymap)))
--8<-----------------------------cut here---------------end--------------->8---
In GNU Emacs 26.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.9)
 of 2017-04-02
Repository revision: afabe53b562675b6279cc670ceba32357fac2214





^ permalink raw reply related	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2020-09-15 15:41 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-02 12:41 bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer Tino Calancha
2017-04-02 15:57 ` Dmitry Gutov
2017-04-03  3:58   ` Tino Calancha
2017-04-02 22:10 ` Juri Linkov
2017-04-03  4:01   ` Tino Calancha
2017-04-03  6:13   ` Tino Calancha
2017-04-03 23:35     ` Juri Linkov
2017-04-04  1:37       ` Tino Calancha
2017-04-04  2:20         ` Tino Calancha
2017-04-04 14:32         ` Marcin Borkowski
2017-04-05 11:58           ` Tino Calancha
2017-04-05 13:11             ` npostavs
2017-04-07 10:06               ` Tino Calancha
2017-04-07 14:40                 ` Drew Adams
2017-04-08  4:45                   ` Tino Calancha
2017-04-08  5:49                     ` Drew Adams
2017-04-08 15:29                       ` Tino Calancha
2017-04-08 15:42                         ` Drew Adams
2017-04-08 11:46                     ` Philipp Stephani
2017-04-08 13:42                       ` Tino Calancha
2017-04-08 14:41                         ` Philipp Stephani
2017-04-08 15:20                           ` Tino Calancha
2017-04-22 19:42                             ` Philipp Stephani
2017-04-08 15:38                           ` npostavs
2017-04-22 19:36                             ` Philipp Stephani
2017-04-05 22:03             ` Juri Linkov
2017-04-07 14:47               ` Tino Calancha
2017-04-07 15:28                 ` Noam Postavsky
2017-04-07 15:54                   ` Drew Adams
2017-04-08 13:49                   ` Tino Calancha
2020-09-15 15:41 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.