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: Mon, 07 Mar 2022 22:33:29 +0000 Message-ID: <87ee3d4cli.fsf@posteo.net> 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="36501"; mail-complaints-to="usenet@ciao.gmane.io" To: 54296@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Mar 07 23:34:11 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 1nRLve-0009Kl-Uu for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 07 Mar 2022 23:34:11 +0100 Original-Received: from localhost ([::1]:43310 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nRLvd-0002Pz-JO for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 07 Mar 2022 17:34:09 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:50830) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRLvW-0002Pp-Ni for bug-gnu-emacs@gnu.org; Mon, 07 Mar 2022 17:34:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:60909) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nRLvW-0003l9-F0 for bug-gnu-emacs@gnu.org; Mon, 07 Mar 2022 17:34:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nRLvW-0006lQ-Az for bug-gnu-emacs@gnu.org; Mon, 07 Mar 2022 17:34: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: Mon, 07 Mar 2022 22:34:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 54296 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.164669242225970 (code B ref -1); Mon, 07 Mar 2022 22:34:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 7 Mar 2022 22:33:42 +0000 Original-Received: from localhost ([127.0.0.1]:54806 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nRLvC-0006ko-3Q for submit@debbugs.gnu.org; Mon, 07 Mar 2022 17:33:42 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:33776) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nRLvA-0006kf-FE for submit@debbugs.gnu.org; Mon, 07 Mar 2022 17:33:40 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:50716) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRLvA-0002O8-2c for bug-gnu-emacs@gnu.org; Mon, 07 Mar 2022 17:33:40 -0500 Original-Received: from mout02.posteo.de ([185.67.36.66]:49891) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRLv7-0003jO-FP for bug-gnu-emacs@gnu.org; Mon, 07 Mar 2022 17:33:39 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 8441C240103 for ; Mon, 7 Mar 2022 23:33:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1646692415; bh=6Ok2SGWJi4yNthyZSGYC4kbdoXJ/ksCA1pEwBc54f50=; h=From:To:Subject:Autocrypt:Date:From; b=VchmMhKyx/lF2uIwGs85rb75anZIKclXNcj3RXwG8LJdEvOeIho/mdnFVSN9vxbhv iIXANPMtbFljRYLrQgEwHXcvwzjrjC9vvANfqS/qqcogukm1I9vFDkH0eSSDSlpsvn /oByMMYEv1asmIb76hRcAWZSdfcXisv3VIr7VuUkoo3jmRTd0q5jgAnpl7CqecFnzD znijSbRuFoih8B8T30vfP0NnvBKJtSqclanHIa1OGDw211PY/xvLRB8oWzZkxgF6X+ H75jxbyRlRj7PrijtXfQGtLgaPP1z3azG6l2aWQOUJhEtnHCw2SJB5V96YoOi62KdC 8SU5I15MGwg1Q== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4KCCsp74vQz9rxD for ; Mon, 7 Mar 2022 23:33:34 +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 Received-SPF: pass client-ip=185.67.36.66; envelope-from=philipk@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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:228124 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Project.el currently has a small language for matching buffers, used by project-kill-buffers and project--read-project-buffer. As mentioned in [0], this could be generalised, as done in the patch below. As to what file this should be added to, should be discussed. Either way I would consider these functions useful and would have wanted to use them in my own code many times before. While difficult, it might also be useful for things like display-buffer-alist (the issue is that a function as a condition in display-buffer-alist has to accept two arguments, while the proposed patch only takes one). To match functions such as string-match, the argument of buffer-match could be reversed so that the function can be used as a testfn to assoc/alist-get. The reason this was not immediately done when project-kill-buffers was implemented, was that this would raise the "emacs" dependency of the ELPA package "project" to the latest release or even the current development version. To solve issues like these, I have been working on "compat", a yet-unreleased library added to GNU ELPA a while back that could be added as a dependency to project. That way newer functions, such as the ones propose below could be used, without breaking ELPA compatibility. To make this work properly in the near future, compat would have to follow the upstream development, before a release is made. If there is any interest in this kind of an arrangement, I could start a thread on emacs-devel to discuss the details. [0] https://mail.gnu.org/archive/html/emacs-devel/2020-09/msg00082.html [1] https://elpa.gnu.org/devel/compat.html In GNU Emacs 29.0.50 (build 13, x86_64-pc-linux-gnu, GTK+ Version 3.24.30, cairo version 1.16.0) of 2022-02-24 built on viero Repository revision: bd17fa2c7565f180cedbfa396c0b159e144178cb Repository branch: master Windowing system distributor 'The X.Org Foundation', version 11.0.12011000 --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Generalise-buffer-matching-from-project.el.patch >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 --=-=-= Content-Type: text/plain -- Philip Kaludercic --=-=-=--