From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#54296: Add buffer-matching functionality Date: Thu, 10 Mar 2022 12:13:59 +0000 Message-ID: <87cziudmyg.fsf@posteo.net> References: <87ee3d4cli.fsf@posteo.net> <87k0d35c82.fsf@gnus.org> <877d92unqn.fsf@posteo.net> <83bkyednvx.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8572"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 54296@debbugs.gnu.org, larsi@gnus.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Mar 10 13:15:51 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nSHhv-00021b-I3 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 10 Mar 2022 13:15:51 +0100 Original-Received: from localhost ([::1]:50154 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nSHhs-0002kE-G8 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 10 Mar 2022 07:15:50 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:32834) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nSHh8-0002gy-Vh for bug-gnu-emacs@gnu.org; Thu, 10 Mar 2022 07:15:04 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:39624) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nSHh8-0007jA-ML for bug-gnu-emacs@gnu.org; Thu, 10 Mar 2022 07:15:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nSHh8-0007Cc-Ik for bug-gnu-emacs@gnu.org; Thu, 10 Mar 2022 07:15:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 10 Mar 2022 12:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54296 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 54296-submit@debbugs.gnu.org id=B54296.164691445027597 (code B ref 54296); Thu, 10 Mar 2022 12:15:02 +0000 Original-Received: (at 54296) by debbugs.gnu.org; 10 Mar 2022 12:14:10 +0000 Original-Received: from localhost ([127.0.0.1]:33521 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nSHgH-0007B3-QP for submit@debbugs.gnu.org; Thu, 10 Mar 2022 07:14:10 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]:52225) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nSHgG-0007Ao-BT for 54296@debbugs.gnu.org; Thu, 10 Mar 2022 07:14:09 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 6E538240026 for <54296@debbugs.gnu.org>; Thu, 10 Mar 2022 13:14:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1646914442; bh=x9qvuxxqlIDjQ8IqvM8XXHjAnlZUszMF8Dibe/WFzj4=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=HpfA766Q9pDzDvY/BDiA8z7AvCQBBRFgHVp633BuN1vu8lWGRQCFh4CZUXxnbHcnI 9VCviJfZ7jIp+A0qNITX0pOBbOGicOTOl6D33XaQQB/K1Fn3LUnSH9uiU5y7eSqknl 6S/lkt5MaIwDS6i3BZMH1RodazHhTcKZfHLAXyifPlXel/8MnKD6wuYvq6zUFUPjuV BGLFCyyubGOorC3KVROMxm6dK+A4ktz8tw20ddw4Mt9RXi/fEAwx2fFawEPrD6mEro k/2J+mRdg2cKp0Hsy/K4TBBuzuDPokrspGPuF008mNopKfzMoup5Iilo5QMve/OsQv kVBRls+x7Qqxg== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4KDnzY22V6z6tpj; Thu, 10 Mar 2022 13:14:00 +0100 (CET) Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB In-Reply-To: <83bkyednvx.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 10 Mar 2022 13:53:54 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:228186 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Philip Kaludercic >> Date: Thu, 10 Mar 2022 10:05:04 +0000 >> Cc: 54296@debbugs.gnu.org >> >> +(defun buffer-match (condition buffer-or-name &optional arg) >> + "Return non-nil if BUFFER-OR-NAME matches CONDITION. > > This is a predicate function, so its name should be buffer-matches-p > or maybe buffer-matches-condition-p. I like buffer-matches-p. >> +CONDITION is is either: > ^^^^^ > Typo. Sorry about those, it and the other ones here: --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Generalise-buffer-matching-from-project.el.patch >From 8dfcabb8c655f06c8c7285c6da7efad7057b636e Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Mon, 7 Mar 2022 20:49:42 +0100 Subject: [PATCH 1/2] 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 | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/lisp/subr.el b/lisp/subr.el index 2321765f95..e55a4c5802 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -6613,4 +6613,68 @@ delete-line (forward-line 1) (point)))) +(defun buffer-match-p (condition buffer-or-name &optional arg) + "Return non-nil if BUFFER-OR-NAME matches CONDITION. +CONDITION is either: +- a regular expression, to match a buffer name, +- a predicate function that takes a buffer object and ARG as + arguments, and returns non-nil if the buffer matches, +- a cons-cell, where the car describes how to interpret the cdr. + The car can be one of the following: + * `major-mode': the buffer matches if the buffer's major + mode is eq to the cons-cell's cdr + * `derived-mode': the buffer matches 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 conditions, 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 + ((buffer (get-buffer buffer-or-name)) + (match + (lambda (conditions) + (catch 'match + (dolist (condition conditions) + (when (cond + ((stringp condition) + (string-match-p condition (buffer-name buffer))) + ((functionp condition) + (if (eq 1 (cdr (func-arity condition))) + (funcall condition buffer) + (funcall condition buffer arg))) + ((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 arg) + "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. +ARG is passed to `buffer-match', for predicate conditions in +CONDITION." + (let (bufs) + (dolist (buf (or buffers (buffer-list))) + (when (buffer-match-p condition (get-buffer buf) arg) + (push buf bufs))) + bufs)) + ;;; subr.el ends here -- 2.34.0 --=-=-= Content-Type: text/plain >> +- a cons-cell, where the car describes how to interpret the cdr. >> + The car can be one of the following: >> + * `major-mode': the buffer matches if the buffer's major >> + mode is eq to the cons-cell's cdr >> + * `derived-mode': the buffer matches if the buffer's major >> + mode is derived from the major mode denoted by the cons-cell's >> + cdr > > Do we really need both major-mode and derived-mode? It seems to have been useful in project.el, see `project-kill-buffer-conditions'. In that case you want to both be able to say something like "kill buffers only if they are in fundamental-mode", but also something like "kill all buffers that are based on comint-mode". -- Philip Kaludercic --=-=-=--