aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Kaludercic2022-03-07 20:49:42 +0100
committerPhilip Kaludercic2022-04-15 10:06:36 +0200
commitea54062fdf013768fbc64ad20846cba55af44909 (patch)
tree7d01380aef923c4d414bbe312af6c019c695f973
parent4d2aa420bd09ac5109a4c13bd163386ea276297e (diff)
downloademacs-ea54062fdf013768fbc64ad20846cba55af44909.tar.gz
emacs-ea54062fdf013768fbc64ad20846cba55af44909.zip
Generalise buffer matching from project.el
* subr.el (buffer-match): Add function to check if a buffer satisfies a condition. (match-buffers): Returns all buffers that satisfy a condition.
-rw-r--r--lisp/subr.el59
1 files changed, 59 insertions, 0 deletions
diff --git a/lisp/subr.el b/lisp/subr.el
index e7d5d36461c..d0b73db019f 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6651,4 +6651,63 @@ is inserted before adjusting the number of empty lines."
6651If OMIT-NULLS, empty lines will be removed from the results." 6651If OMIT-NULLS, empty lines will be removed from the results."
6652 (split-string string "\n" omit-nulls)) 6652 (split-string string "\n" omit-nulls))
6653 6653
6654(defun buffer-match-p (condition buffer-or-name &optional arg)
6655 "Return non-nil if BUFFER-OR-NAME matches CONDITION.
6656CONDITION is either:
6657- a regular expression, to match a buffer name,
6658- a predicate function that takes a buffer object and ARG as
6659 arguments, and returns non-nil if the buffer matches,
6660- a cons-cell, where the car describes how to interpret the cdr.
6661 The car can be one of the following:
6662 * `major-mode': the buffer matches if the buffer's major
6663 mode is derived from the major mode denoted by the cons-cell's
6664 cdr
6665 * `not': the cdr is interpreted as a negation of a condition.
6666 * `and': the cdr is a list of recursive conditions, that all have
6667 to be met.
6668 * `or': the cdr is a list of recursive condition, of which at
6669 least one has to be met."
6670 (letrec
6671 ((buffer (get-buffer buffer-or-name))
6672 (match
6673 (lambda (conditions)
6674 (catch 'match
6675 (dolist (condition conditions)
6676 (when (cond
6677 ((stringp condition)
6678 (string-match-p condition (buffer-name buffer)))
6679 ((functionp condition)
6680 (if (eq 1 (cdr (func-arity condition)))
6681 (funcall condition buffer)
6682 (funcall condition buffer arg)))
6683 ((eq (car-safe condition) 'major-mode)
6684 (provided-mode-derived-p
6685 (buffer-local-value 'major-mode buffer)
6686 (cdr condition)))
6687 ((eq (car-safe condition) 'not)
6688 (not (funcall match (cdr condition))))
6689 ((eq (car-safe condition) 'or)
6690 (funcall match (cdr condition)))
6691 ((eq (car-safe condition) 'and)
6692 (catch 'fail
6693 (dolist (c conditions)
6694 (unless (funcall match c)
6695 (throw 'fail nil)))
6696 t)))
6697 (throw 'match t)))))))
6698 (funcall match (list condition))))
6699
6700(defun match-buffers (condition &optional buffers arg)
6701 "Return a list of buffers that match CONDITION.
6702See `buffer-match' for details on CONDITION. By default all
6703buffers are checked, this can be restricted by passing an
6704optional argument BUFFERS, set to a list of buffers to check.
6705ARG is passed to `buffer-match', for predicate conditions in
6706CONDITION."
6707 (let (bufs)
6708 (dolist (buf (or buffers (buffer-list)))
6709 (when (buffer-match-p condition (get-buffer buf) arg)
6710 (push buf bufs)))
6711 bufs))
6712
6654;;; subr.el ends here 6713;;; subr.el ends here