unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#54296: Add buffer-matching functionality
@ 2022-03-07 22:33 Philip Kaludercic
  2022-03-09 16:20 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 47+ messages in thread
From: Philip Kaludercic @ 2022-03-07 22:33 UTC (permalink / raw)
  To: 54296

[-- Attachment #1: Type: text/plain, Size: 1954 bytes --]

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.

<tangent>
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.
</tangent>

[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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Generalise-buffer-matching-from-project.el.patch --]
[-- Type: text/patch, Size: 3359 bytes --]

From f268509ba1b617b65851231c5ae262f3b169cd6c Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
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


[-- Attachment #3: Type: text/plain, Size: 24 bytes --]


-- 
	Philip Kaludercic

^ permalink raw reply related	[flat|nested] 47+ messages in thread

end of thread, other threads:[~2022-06-17 13:39 UTC | newest]

Thread overview: 47+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-07 22:33 bug#54296: Add buffer-matching functionality Philip Kaludercic
2022-03-09 16:20 ` Lars Ingebrigtsen
2022-03-09 20:34   ` martin rudalics
2022-03-10 10:05   ` Philip Kaludercic
2022-03-10 11:53     ` Eli Zaretskii
2022-03-10 12:13       ` Philip Kaludercic
2022-03-10 14:52         ` bug#54296: [External] : " Drew Adams
2022-03-10 16:56         ` Eli Zaretskii
2022-03-11 16:21           ` Philip Kaludercic
2022-03-11 18:34             ` Eli Zaretskii
2022-03-13 20:40               ` Philip Kaludercic
2022-03-14  3:21                 ` Eli Zaretskii
2022-03-14  8:21                   ` Philip Kaludercic
2022-03-14 13:00                     ` Eli Zaretskii
2022-03-14 13:38                       ` Philip Kaludercic
2022-06-13  0:30                         ` Dmitry Gutov
     [not found]                         ` <add2d2d0-9cdf-9048-1a62-f34e585c582e@yandex.ru>
2022-06-13 12:04                           ` Eli Zaretskii
2022-06-14 18:43                             ` Dmitry Gutov
2022-06-14 18:47                               ` Eli Zaretskii
2022-06-14 19:36                                 ` Dmitry Gutov
2022-06-15  2:33                                   ` Eli Zaretskii
2022-06-15 11:48                                     ` Dmitry Gutov
2022-06-15 13:20                                       ` Eli Zaretskii
2022-06-15 15:56                                         ` Dmitry Gutov
2022-06-15 16:17                                           ` Eli Zaretskii
2022-06-15 16:51                                             ` Dmitry Gutov
2022-06-15 17:29                                               ` Eli Zaretskii
2022-06-16  0:47                                                 ` Dmitry Gutov
2022-06-16  5:51                                                   ` Eli Zaretskii
2022-06-17  1:21                                                     ` Dmitry Gutov
2022-06-17  5:48                                                       ` Eli Zaretskii
2022-06-17 13:39                                                         ` Dmitry Gutov
2022-06-15 16:59                                         ` Philip Kaludercic
2022-06-17  1:22                                           ` Dmitry Gutov
2022-04-14  8:25                       ` Philip Kaludercic
2022-04-15  6:56                         ` Eli Zaretskii
2022-04-15 10:57                           ` Philip Kaludercic
2022-06-13  0:30                   ` Dmitry Gutov
     [not found]                   ` <b4bf095c-7210-61ee-87af-3d8031caba89@yandex.ru>
2022-06-13 12:13                     ` Eli Zaretskii
2022-06-14 19:00                       ` Dmitry Gutov
2022-06-14 19:17                         ` Eli Zaretskii
2022-06-14 19:46                           ` Dmitry Gutov
2022-06-15  2:34                             ` Eli Zaretskii
2022-06-15 11:54                               ` Dmitry Gutov
2022-03-12 10:23       ` Augusto Stoffel
2022-03-12 11:07         ` Philip Kaludercic
2022-03-10 15:42     ` martin rudalics

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).