I am looking for a way to count regexp matches across all buffers.
count-matches
can count in current buffer. How to count across all buffers?
One possible implementation that allows:
(require 'cl-macs)
(cl-defun count-matches-in-buffers (regexp &key (buffers (buffer-list)) (predicate #'buffer-file-name))
"Count all matches of REGEXP in BUFFERS filtered by PREDICATE.
BUFFERS defaults to `(buffer-list)`.
PREDICATE defaults to buffers that are associated with files.
Use your own buffer list (buf1 buf2 buf3 ...) by using
(count-matches-in-buffers REGEXP :buffers '(buf1 buf2 buf3 ...))
Apply your own predicate function bound to symbol MY-PREDICATE by calling
(count-matches-in-buffers REGEXP :predicate #'MY-PREDICATE)
Combinations are also possible:
(count-matches-in-buffers REGEXP :buffers '(buf1 buf2 buf3 ...) :predicate #'MY-PREDICATE)
Prompt user for the REGEXP the buffer list BUFFERS and the PREDICATE when called interactively.
The defaults for BUFFERS and PREDICATE are preset.
So the user only needs to press <RETURN> for these two arguments.
"
(interactive (list (read-regexp "Regexp: ")
:buffers
(eval (read-from-minibuffer
"Buffers (elisp expression): "
"(buffer-list)"
nil t
nil
"(buffer-list)"))
:predicate
(eval (read-from-minibuffer
"Filter:"
"#'buffer-file-name"
nil t
nil
"#'identity"))))
(let
((ret
(cl-loop for buf
in (cl-remove-if-not predicate buffers)
sum (with-current-buffer buf
(save-excursion
(goto-char (point-min))
(count-matches regexp))))))
(when (called-interactively-p 'any)
(message "Number of matches: %d" ret))
ret))
Tested with GNU Emacs 26.2 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2019-04-12
.
You can paste this stuff into your init file.
dolist
, where the list is obtained by calling(buffer-list)
.