>From f268509ba1b617b65851231c5ae262f3b169cd6c Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Mon, 7 Mar 2022 20:49:42 +0100 Subject: [PATCH] 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. --- lisp/subr.el | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/lisp/subr.el b/lisp/subr.el index eb9af0b36d..1d0c9cf967 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -6613,4 +6613,63 @@ delete-line (forward-line 1) (point)))) +(defun buffer-match (buffer condition) + "Return non-nil if BUFFER matches CONDITION. +CONDITION is is either: +- a regular expression, to match a buffer name, +- a predicate function that takes a buffer object as argument + and returns non-nil if the buffer should be killed, +- a cons-cell, where the car describes how to interpret the cdr. + The car can be one of the following: + * `major-mode': the buffer is killed if the buffer's major + mode is eq to the cons-cell's cdr + * `derived-mode': the buffer is killed if the buffer's major + mode is derived from the major mode denoted by the cons-cell's + cdr + * `not': the cdr is interpreted as a negation of a condition. + * `and': the cdr is a list of recursive condition, that all have + to be met. + * `or': the cdr is a list of recursive condition, of which at + least one has to be met." + (letrec + ((match + (lambda (conditions) + (catch 'match + (dolist (condition conditions) + (when (cond + ((stringp condition) + (string-match-p condition (buffer-name buffer))) + ((symbolp condition) + (funcall condition buffer)) + ((eq (car-safe condition) 'major-mode) + (eq (buffer-local-value 'major-mode buffer) + (cdr condition))) + ((eq (car-safe condition) 'derived-mode) + (provided-mode-derived-p + (buffer-local-value 'major-mode buffer) + (cdr condition))) + ((eq (car-safe condition) 'not) + (not (funcall match (cdr condition)))) + ((eq (car-safe condition) 'or) + (funcall match (cdr condition))) + ((eq (car-safe condition) 'and) + (catch 'fail + (dolist (c conditions) + (unless (funcall match c) + (throw 'fail nil))) + t))) + (throw 'match t))))))) + (funcall match (list condition)))) + +(defun match-buffers (condition &optional buffers) + "Return a list of buffers that match CONDITION. +See `buffer-match' for details on CONDITION. By default all +buffers are checked, this can be restricted by passing an +optional argument BUFFERS, set to a list of buffers to check." + (let (bufs) + (dolist (buf (or buffers (buffer-list))) + (when (buffer-match buf condition) + (push buf bufs))) + bufs)) + ;;; subr.el ends here -- 2.34.0