From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.bugs Subject: bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer Date: Sun, 02 Apr 2017 21:41:30 +0900 Message-ID: <8737dr6kxx.fsf@calancha-pc> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1491136940 9219 195.159.176.226 (2 Apr 2017 12:42:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 2 Apr 2017 12:42:20 +0000 (UTC) Cc: juri linkov To: 26338@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Apr 02 14:42:12 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cuepf-0001I3-9s for geb-bug-gnu-emacs@m.gmane.org; Sun, 02 Apr 2017 14:42:11 +0200 Original-Received: from localhost ([::1]:54847 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuepj-0002Ls-O4 for geb-bug-gnu-emacs@m.gmane.org; Sun, 02 Apr 2017 08:42:15 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34145) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuepb-0002Lb-IB for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2017 08:42:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuepW-0004OU-Le for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2017 08:42:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:58288) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuepW-0004OG-HO for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2017 08:42:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cuepV-0005XT-SF; Sun, 02 Apr 2017 08:42:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Tino Calancha Original-Sender: "Debbugs-submit" Resent-CC: juri@linkov.net, bug-gnu-emacs@gnu.org Resent-Date: Sun, 02 Apr 2017 12:42:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 26338 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: juri linkov Original-Received: via spool by submit@debbugs.gnu.org id=B.149113691121272 (code B ref -1); Sun, 02 Apr 2017 12:42:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 2 Apr 2017 12:41:51 +0000 Original-Received: from localhost ([127.0.0.1]:56485 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cuepK-0005X0-V0 for submit@debbugs.gnu.org; Sun, 02 Apr 2017 08:41:51 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:43093) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cuepJ-0005Wk-1f for submit@debbugs.gnu.org; Sun, 02 Apr 2017 08:41:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuepC-00049p-Na for submit@debbugs.gnu.org; Sun, 02 Apr 2017 08:41:43 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:45425) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cuepC-00049e-KK for submit@debbugs.gnu.org; Sun, 02 Apr 2017 08:41:42 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34058) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cuepB-0002EX-C5 for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2017 08:41:42 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cuep6-00047J-A9 for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2017 08:41:41 -0400 Original-Received: from mail-pg0-x236.google.com ([2607:f8b0:400e:c05::236]:35171) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cuep6-000471-3V for bug-gnu-emacs@gnu.org; Sun, 02 Apr 2017 08:41:36 -0400 Original-Received: by mail-pg0-x236.google.com with SMTP id 81so97888066pgh.2 for ; Sun, 02 Apr 2017 05:41:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=zBEku83rOn40gJX1A4nArF72TIXa2PwLjP6x+6i1a5g=; b=FXNh/XMSsrvMF46tI35nfITZlgfEH8oqzD/vwiru3g/T06VGf9oAFauHvYKla5JmT2 WWda8wqFI4FTvuDQA6SF8NEEHC9ZnK1Ayih6a1rZesh0hHVTy7M2sFlyRz1vAgviBGK+ do+3b8Ycb6ePt1FDtfTJAeTNYbLnweLc8lsnFjw6UY8v6JoYUW3SGEaIZSCtUGs+nPFq YcQ6bJbvSfCQHC25DeBfAJiZrjtp+lhLM5D7+JyQKcuve7pyE+CAXtB6bOsFI4zhFIMN H90bSsJ4l9uqYGv4nMYDLvMNDztjAsn5H5RE05V0Mj9jNouR+wMixzKPRucYguVVb3iX iDHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=zBEku83rOn40gJX1A4nArF72TIXa2PwLjP6x+6i1a5g=; b=Yf+hXJBdfTMWwI+ABXIEHfkBK8WGTmfrrtNKBr25tTzm3vF+Zi0ZfcPSQ5n3Vj0gxp lfjY3/2cs/FCgMqIeCSS3t7K4YB0P4w8EpyGcVuPrzfdUiME0DuLdil/5gV58E6WksAG 70GkKc24FHvy91vWgNJNJkTnssXFQH+t4S8GemK2qosf7ItPUaeoqAc1WKErkrtfDWeT QpjOxbS8f/0iPahd7MrRiC98mDONbP6nH9qVo5AyQrIXbCiytRjUOyrdcJOInlQnnmKq OlY9xhzIzasnvjsAJ3ya82MQ0t+yBMcypiiag54JxcdFS0nANwcEkZuesYiYYzs3PVN2 FqaQ== X-Gm-Message-State: AFeK/H099nx7zeHRA0NTq7a9pQAvj7Hnw8S6cVpSFx/EyZlMdG71hUhRZzTzDD+/VlM+Ow== X-Received: by 10.98.201.77 with SMTP id k74mr11636419pfg.74.1491136894967; Sun, 02 Apr 2017 05:41:34 -0700 (PDT) Original-Received: from calancha-pc (234.204.100.220.dy.bbexcite.jp. [220.100.204.234]) by smtp.gmail.com with ESMTPSA id t66sm20236203pfk.53.2017.04.02.05.41.33 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 02 Apr 2017 05:41:34 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:131208 Archived-At: X-Debbugs-CC: Juri Linkov 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 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)) + (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