Subject: bug#79327: [PATCH] Use 'buffer-match-p' and 'match-buffer' in project

From: Philip Kaludercic <[email-masked]>
Date: Thu, 28 Aug 2025 06:17:51 +0000

Tags: patch

Hi, I would like to propose that project.el uses buffer-match-p and
match-buffers instead of the custom implementations.  For that to work
while project.el is on ELPA, I have added a dependency on Compat.

WDYT?

In GNU Emacs 31.0.50 (build 6, x86_64-pc-linux-gnu, GTK+ Version
 3.24.49, cairo version 1.18.4) of 2025-08-22 built on siskin
Repository revision: de3122d56695d5b8363a5e01ff711d6ba198bbd5
Repository branch: local/21Aug25
System Description: Debian GNU/Linux 13 (trixie)

Configured using:
 'configure --with-pgtk'

>From 8d72379a84375207b70fb006cc00b98a494b9f36 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Thu, 28 Aug 2025 00:31:47 +0200
Subject: [PATCH] Use 'buffer-match-p' and 'match-buffer' in project

* lisp/progmodes/project.el (compat): Require library.
(project--read-project-buffer): Use 'buffer-match-p' instead of
'project--buffer-check'.
(project--buffer-check, project--buffers-to-kill): Remove
functions.
(project-kill-buffers): Use 'match-buffers' instead of
'project--buffers-to-kill'.
---
 lisp/progmodes/project.el | 51 +++++----------------------------------
 1 file changed, 6 insertions(+), 45 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 8438060afa3..3682a619b27 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -2,7 +2,7 @@
 
 ;; Copyright (C) 2015-2025 Free Software Foundation, Inc.
 ;; Version: 0.11.1
-;; Package-Requires: ((emacs "26.1") (xref "1.7.0"))
+;; Package-Requires: ((emacs "26.1") (xref "1.7.0") (compat "29.1"))
 
 ;; This is a GNU ELPA :core package.  Avoid functionality that is not
 ;; compatible with the version of Emacs recorded above.
@@ -171,6 +171,7 @@
 (require 'cl-generic)
 (require 'cl-lib)
 (require 'seq)
+(require 'compat)
 (eval-when-compile (require 'subr-x))
 
 (defgroup project nil
@@ -1691,8 +1692,8 @@ project--read-project-buffer
             ;; BUFFER is an entry (BUF-NAME . BUF-OBJ) of Vbuffer_alist.
             (and (memq (cdr buffer) buffers)
                  (not
-                  (project--buffer-check
-                   buffer project-ignore-buffer-conditions)))))
+                  (buffer-match-p
+                   project-ignore-buffer-conditions buffer)))))
          (buffer
           (if (and (fboundp 'uniquify-get-unique-names)
                    uniquify-buffer-name-style)
@@ -1893,47 +1894,6 @@ project-kill-buffers-display-buffer-list
   :package-version '(project . "0.8.2"))
 ;;;###autoload(put 'project-kill-buffers-display-buffer-list 'safe-local-variable #'booleanp)
 
-;; FIXME: Could this be replaced by `buffer-match-p' in Emacs 29+?
-(defun project--buffer-check (buf conditions)
-  "Check if buffer BUF matches any element of the list CONDITIONS.
-See `project-kill-buffer-conditions' or
-`project-ignore-buffer-conditions' for more details on the
-form of CONDITIONS."
-  (catch 'match
-    (dolist (c conditions)
-      (when (cond
-             ((stringp c)
-              (string-match-p c (buffer-name buf)))
-             ((functionp c)
-              (funcall c buf))
-             ((eq (car-safe c) 'major-mode)
-              (eq (buffer-local-value 'major-mode buf)
-                  (cdr c)))
-             ((eq (car-safe c) 'derived-mode)
-              (provided-mode-derived-p
-               (buffer-local-value 'major-mode buf)
-               (cdr c)))
-             ((eq (car-safe c) 'not)
-              (not (project--buffer-check buf (cdr c))))
-             ((eq (car-safe c) 'or)
-              (project--buffer-check buf (cdr c)))
-             ((eq (car-safe c) 'and)
-              (seq-every-p
-               (apply-partially #'project--buffer-check
-                                buf)
-               (mapcar #'list (cdr c)))))
-        (throw 'match t)))))
-
-(defun project--buffers-to-kill (pr)
-  "Return list of buffers in project PR to kill.
-What buffers should or should not be killed is described
-in `project-kill-buffer-conditions'."
-  (let (bufs)
-    (dolist (buf (project-buffers pr))
-      (when (project--buffer-check buf project-kill-buffer-conditions)
-        (push buf bufs)))
-    bufs))
-
 ;;;###autoload
 (defun project-kill-buffers (&optional no-confirm project)
   "Kill the buffers belonging to the current project.
@@ -1950,7 +1910,8 @@ project-kill-buffers
 Also see the `project-kill-buffers-display-buffer-list' variable."
   (interactive)
   (let* ((pr (or project (project-current t)))
-         (bufs (project--buffers-to-kill pr))
+         (bufs (project-buffers pr))
+         (bufs (match-buffers project-kill-buffer-conditions bufs))
          (query-user (lambda ()
                        (yes-or-no-p
                         (format "Kill %d buffers in %s? "
-- 
2.47.2