Subject: Re: bug#77312: [PATCH] Add uniquify-get-unique-names

From: Dmitry Gutov <[email-masked]>
Date: Mon, 31 Mar 2025 22:58:19 +0300

Hi all,

On 28/03/2025 20:50, Spencer Baugh wrote:
> After some use, it seems better to return an alist rather than a hash
> table from this function; that alist is in the same order as the buffer
> list that was passed in, which is nice for completion.  So here's a
> version which does that.

Here's the corresponding patch to project.el to use the new function.

The use of new logic is predicated on the non-nil value of 
uniquify-buffer-name-style. The latter doesn't necessarily imply that 
the user wants the former, but seems a safe enough bet.

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index d954b78a745..020deb435f8 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1562,13 +1562,27 @@ project--read-project-buffer
             (and (memq (cdr buffer) buffers)
                  (not
                   (project--buffer-check
-                   (cdr buffer) project-ignore-buffer-conditions)))))
-         (buffer (read-buffer
-                  "Switch to buffer: "
-                  (when (funcall predicate (cons other-name other-buffer))
-                    other-name)
-                  nil
-                  predicate)))
+                   buffer project-ignore-buffer-conditions)))))
+         (buffer
+          (if uniquify-buffer-name-style
+              ;; Forgo the use of `buffer-read-function' (often nil) in
+              ;; favor of uniquifying the buffers better.
+              (let* ((unique-names (uniquify-get-unique-names buffers))
+                     (other-name (when (funcall predicate (cons other-name other-buffer))
+                                   (car (rassoc other-buffer unique-names))))
+                     (result (completing-read
+                              "Switch to buffer: "
+                              unique-names
+                              predicate
+                              nil nil nil
+                              other-name)))
+                (assoc-default result unique-names #'equal result))
+            (read-buffer
+             "Switch to buffer: "
+             (when (funcall predicate (cons other-name other-buffer))
+               other-name)
+             nil
+             predicate))))
     ;; XXX: This check hardcodes the default buffer-belonging relation
     ;; which `project-buffers' is allowed to override.  Straighten
     ;; this up sometime later.  Or not.  Since we can add a method