Subject: Re: bug#58839: [Patch] Re: bug#58839: 29.0.50; project-kill-buffer fails when Eglot is running

From: Philip Kaludercic <[email-masked]>
Date: Tue, 01 Nov 2022 11:27:21 +0000

João Távora <[email-masked]> writes:

> On Tue, Nov 1, 2022 at 10:48 AM Philip Kaludercic <[email-masked]>
> wrote:
>
>>
>> BTW, if there are major objections to the language, I should point out
>> that the new `buffer-match-p' in Emacs 29 uses the same language and has
>> already found usage in a number of spots in core Emacs.  There would
>> still be time to address any issues you might have, and avoid a
>> long-term mistake.
>>
>
> For me, it looks like match-buffers is reinventing
> cl-remove-if-not and match-buffer-p is reinventing ... unary predicate
> function of a buffer?

You could say that, though I would argue it is an easier way to express
common predicates, while not making anything else more complicated.

E.g. `display-buffer-alist' makes use of it to associate display-buffer
rules with buffers.  Now you can add

      ((major-mode . help-mode) display-buffer-in-side-window)

instead of trying to match being a regular expression to catch all
*Help* buffer names of a function along the lines of

      (lambda (buf _alist)
        (with-current-buffer buf
          (derived-mode-p 'help-mode)))

> I'm not fond of these mini-languages because they're less expressive, they
> end up being only minimally less complicated and bug-prone, they can't
> automatically be byte-compiled for efficiency, and they can't automatically
> be byte-compiled for correctness/diagnostics.  If one makes a mistake,
> the backtrace is much more complicated.

I agree in principle, but this should be alleviated by using a lambda
function as a predicate.  The above check still works and can be used
anywhere you would use `buffer-match-p'.

> So these mini-languages may make sense to define filters in thunderbird or
> something, but throwing Elisp away here generally doesn't make sense to me.
>
> But there may be exceptions (although this project.el one doesn't seem one
> of them) so why don't you show examples of use of these new helpers and
> so we can compare side by side with the Elisp-only alternative.

I am biased, but I believe that the language could even find more use in
project.el, by having `project-buffers' just call `match-buffers' with a
special `buffer-match-p' predicate.  Here is a sketch of how that could
look like (I haven't tested it yet):

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index ac278edd40..b55c245368 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -352,15 +352,28 @@ project--remote-file-names
                 (concat remote-id file))
               local-files))))
 
+(defun project-includes-buffer-p (buffer dir)
+  "Return non-nil if the `default-directory' of BUFFER is below DIR."
+  (file-in-directory-p
+   (buffer-local-value 'default-directory buffer)
+   dir))
+
+(defcustom project-buffer-conditions
+  '(and (or buffer-file-name
+            (derived-mode . compilation-mode)
+            (derived-mode . dired-mode)
+            (derived-mode . diff-mode)
+            (derived-mode . comint-mode)
+            (derived-mode . eshell-mode)
+            (derived-mode . change-log-mode))
+        project-includes-buffer-p)
+  "A buffer predicate for matching what buffers belong to a project."
+  :type 'buffer-predicate)
+
 (cl-defgeneric project-buffers (project)
   "Return the list of all live buffers that belong to PROJECT."
-  (let ((root (expand-file-name (file-name-as-directory (project-root project))))
-        bufs)
-    (dolist (buf (buffer-list))
-      (when (string-prefix-p root (expand-file-name
-                                   (buffer-local-value 'default-directory buf)))
-        (push buf bufs)))
-    (nreverse bufs)))
+  (let ((root (expand-file-name (file-name-as-directory (project-root project)))))
+    (match-buffers project-buffer-conditions nil root)))
 
 (defgroup project-vc nil
   "Project implementation based on the VC package."
@@ -679,7 +692,7 @@ project-buffers
                      (project--git-submodules))))
          dd
          bufs)
-    (dolist (buf (buffer-list))
+    (dolist (buf (cl-call-next-method))
       (setq dd (expand-file-name (buffer-local-value 'default-directory buf)))
       (when (and (string-prefix-p root dd)
                  (not (cl-find-if (lambda (module) (string-prefix-p module dd))

> João