[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer
From: |
Tino Calancha |
Subject: |
bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer |
Date: |
Tue, 04 Apr 2017 11:20:18 +0900 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) |
Tino Calancha <tino.calancha@gmail.com> writes:
> Juri Linkov <juri@linkov.net> writes:
>> occur-collect is intended for interactive use. As for programmatic use,
>> Dmitry is right: a universal idiom is (while (re-search-forward ...)).
>> This is why e.g. the docstring of ‘replace-regexp’ recommends to use
>> an explicit loop like (while (re-search-forward ...) (replace-match ...))
> OK thanks. Let me ask you my last proposal before come back to my dark
> cave and start painting animals in the walls.
>
> Any interest in something like this?:
>
> (defmacro with-collect-matches (regexp &optional group &rest body)
> "Collect matches for REGEXP and eval BODY for each match.
> BODY is evaluated with `it' bound to the match.
> Optional GROUP if non-nil, then is the regexp group to save. Otherwise,
> save the whole match."
Sorry, i was paiting a mammoth and i forgot something in the docstring:
--8<-----------------------------cut here---------------start------------->8---
(defmacro with-collect-matches (regexp &optional group &rest body)
"Collect matches for REGEXP and eval BODY for each match.
BODY is evaluated with `it' bound to the match.
Optional GROUP if non-nil, then is the regexp group to save. Otherwise,
save the whole match.
Return a list with the matches."
--8<-----------------------------cut here---------------end--------------->8---
So, for instance:
M-x find-library replace RET
M-: (length (with-collect-matches "^(defun \\(\\S +\\)" 1)) RET
=> 52
M-x find-library replace RET
M-: (length (with-collect-matches "^(defun \\(\\S +\\)" 1
(with-current-buffer (get-buffer-create "*Matches*")
(when (string-match "\\`query-" it)
(insert (format "%s\n" it)))))
=> 52
;; Same return as before but only write into *Matches* those
;; functions with name starting with "query-".
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/02
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Dmitry Gutov, 2017/04/02
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Juri Linkov, 2017/04/02
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/03
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/03
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Juri Linkov, 2017/04/03
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/03
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer,
Tino Calancha <=
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Marcin Borkowski, 2017/04/04
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/05
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, npostavs, 2017/04/05
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/07
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Drew Adams, 2017/04/07
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/08
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Drew Adams, 2017/04/08
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Tino Calancha, 2017/04/08
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Drew Adams, 2017/04/08
- bug#26338: 26.0.50; Collect all matches for REGEXP in current buffer, Philipp Stephani, 2017/04/08