Subject: Re: bug#62621: 29.0.60; uniquify can't make buffers unique based on things other than filename

From: Spencer Baugh <[email-masked]>
Date: Mon, 24 Jul 2023 15:18:23 -0400

Spencer Baugh <[email-masked]> writes:
> Eli Zaretskii <[email-masked]> writes:
>>> From: Spencer Baugh <[email-masked]>
>>> Cc: [email-masked],  [email-masked],  [email-masked]
>>> Date: Fri, 21 Jul 2023 09:34:28 -0400
>>> 
>>> > Thanks, but it still falls short of what Dmitry described above: the
>>> > doc string doesn't "mention several functions that can be used".
>>> >
>>> >> +(defcustom uniquify-dirname-transform #'identity
>>> >> +  "Function to transform buffer's directory for uniquifying its name.
>>> >> +
>>> >> +It takes a single argument: the directory of the buffer.  It
>>> >> +should return a string filename (which does not need to actually
>>> >> +exist in the filesystem) to use for uniquifying the buffer name."
>>> >
>>> > Please read this carefully and try to put yourself in the shoes of a
>>> > user who needs to make sense out of this description.  The immediate
>>> > question I had is what does "transforming a buffer's directory" have
>>> > to do with "uniquifying the buffer name"?  Uniquifying a buffer's name
>>> > is not about its directory, at least not in general.  IOW, the
>>> > starting point of this description is too "inside" the implementation.
>>> 
>>> OK, how about this?
>>
>> The explanation of what project-uniquify-dirname-transform does should
>> in its doc string, not in the doc string of uniquify-dirname-transform
>> (which should refer to the former, and that is enough).
>>
>> The doc string of uniquify-dirname-transform should mention at least
>> 'identity' as the default (what you wrote does that, but without
>> mentioning the function's name), otherwise this still falls short of
>> what Dmitry described.
>>
>> And the last two paragraphs of the doc string of
>> uniquify-dirname-transform should be more-or-less reversed: first
>> describe the default, and that using some function other than
>> 'identity' can affect the result, then describe
>> project-uniquify-dirname-transform as one such non-default transform.
>
> OK, how about this?

Oops, that one didn't include the updated
project-uniquify-dirname-transform docstring.  The right patch now:

>From 39e508c96ddf6bc5361542aa030f199193329fe0 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Sun, 9 Jul 2023 22:21:03 -0400
Subject: [PATCH] Support transforming the dirname used by uniquify

By transforming the dirname, we can add additional information to use
during uniquifying.  A basic one: uniquifying buffer names based on
the project name.

* lisp/progmodes/project.el (project-uniquify-dirname-transform): Add.
* lisp/uniquify.el (uniquify-dirname-transform-default)
(uniquify-dirname-transform): Add. (bug#62621)
(uniquify-rationalize-file-buffer-names, uniquify-buffer-file-name):
Use uniquify-dirname-transform.
* test/lisp/uniquify-tests.el (uniquify-home,
uniquify-project-transform): Add tests.
---
 lisp/progmodes/project.el   | 17 +++++++++++++++++
 lisp/uniquify.el            | 37 ++++++++++++++++++++++++++++++++-----
 test/lisp/uniquify-tests.el | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 5 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index d482cc24d70..36c1005aef5 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1835,5 +1835,22 @@ project-switch-project
     (let ((project-current-directory-override dir))
       (call-interactively command))))
 
+;;;###autoload
+(defun project-uniquify-dirname-transform (dirname)
+  "Include `project-name' in DIRNAME if in a project.
+
+If you set `uniquify-dirname-transform' to this function,
+slash-separated components from `project-name' will be added to
+the buffer's name when buffers from two different projects would
+otherwise have the same name."
+  (if-let (proj (project-current nil dirname))
+      (let ((root (project-root proj)))
+        (expand-file-name
+         (file-name-concat
+          (file-name-directory root)
+          (project-name proj)
+          (file-relative-name dirname root))))
+    dirname))
+
 (provide 'project)
 ;;; project.el ends here
diff --git a/lisp/uniquify.el b/lisp/uniquify.el
index d1ca455b673..af00c95663d 100644
--- a/lisp/uniquify.el
+++ b/lisp/uniquify.el
@@ -168,6 +168,31 @@ uniquify-list-buffers-directory-modes
 That means that when `buffer-file-name' is set to nil, `list-buffers-directory'
 contains the name of the directory which the buffer is visiting.")
 
+(defcustom uniquify-dirname-transform #'identity
+  "Function to transform buffer's directory for uniquifying its name.
+
+If `uniquify-buffer-name-style' is non-nil and a buffer's name
+would be the same as some other buffer, then components from the
+buffer's directory name are added to the buffer's name until the
+buffer's name is unique.
+
+This function is used to transform the buffer's directory name
+before the uniquifying process, allowing the unique buffer name
+to include components from other sources.  The default is
+`identity', so only the buffer's directory name is used for
+uniquifying.  This function is called with the buffer's directory
+name and should return a file name (which does not need to
+actually exist in the filesystem) to use components from.
+
+To include components from `project-name', set this variable to
+`project-uniquify-dirname-transform'."
+  :type '(choice (function-item :tag "Don't change the dirname" identity)
+                 (function-item :tag "Include project name in dirname"
+                                #'project-uniquify-dirname-transform)
+                 function)
+  :version "30.1"
+  :group 'uniquify)
+
 ;;; Utilities
 
 ;; uniquify-fix-list data structure
@@ -209,7 +234,8 @@ uniquify-rationalize-file-buffer-names
   ;; this buffer.
   (with-current-buffer newbuf (setq uniquify-managed nil))
   (when dirname
-    (setq dirname (expand-file-name (directory-file-name dirname)))
+    (setq dirname (funcall uniquify-dirname-transform
+                           (expand-file-name (directory-file-name dirname))))
     (let ((fix-list (list (uniquify-make-item base dirname newbuf
                                               nil)))
 	  items)
@@ -268,10 +294,11 @@ uniquify-buffer-file-name
 	       (if (memq major-mode uniquify-list-buffers-directory-modes)
 		   list-buffers-directory))))
       (when filename
-	(directory-file-name
-	 (file-name-directory
-	  (expand-file-name
-	   (directory-file-name filename))))))))
+	 (funcall uniquify-dirname-transform
+	          (directory-file-name
+	          (file-name-directory
+	           (expand-file-name
+	            (directory-file-name filename)))))))))
 
 (defun uniquify-rerationalize-w/o-cb (fix-list)
   "Re-rationalize the buffers in FIX-LIST, but ignoring `current-buffer'."
diff --git a/test/lisp/uniquify-tests.el b/test/lisp/uniquify-tests.el
index abd61fa3504..e533c4b644c 100644
--- a/test/lisp/uniquify-tests.el
+++ b/test/lisp/uniquify-tests.el
@@ -88,6 +88,21 @@ uniquify-dirs
                        '("a/dir/" "b/dir/")))
         (mapc #'kill-buffer bufs)))))
 
+(ert-deftest uniquify-home ()
+  "uniquify works, albeit confusingly, in the presence of directories named \"~\""
+  (let (bufs)
+    (save-excursion
+      (push (find-file-noselect "~") bufs)
+      (push (find-file-noselect "./~") bufs)
+      (should (equal (mapcar #'buffer-name bufs)
+                     '("~<test>" "~<>")))
+      (push (find-file-noselect "~/foo") bufs)
+      (push (find-file-noselect "./~/foo") bufs)
+      (should (equal (mapcar #'buffer-name bufs)
+                     '("foo<~>" "foo</nonexistent>" "~<test>" "~<>")))
+      (while bufs
+        (kill-buffer (pop bufs))))))
+
 (ert-deftest uniquify-rename-to-dir ()
   "Giving a buffer a name which matches a directory doesn't rename the buffer"
   (let ((uniquify-buffer-name-style 'forward)
@@ -125,5 +140,23 @@ uniquify-space-prefix
     (should (equal (buffer-name) "| foo"))
     (kill-buffer)))
 
+(require 'project)
+(ert-deftest uniquify-project-transform ()
+  "`project-uniquify-dirname-transform' works"
+  (let ((uniquify-dirname-transform #'project-uniquify-dirname-transform)
+        (project-vc-name "foo1/bar")
+        bufs)
+    (save-excursion
+      (should (file-exists-p "../README"))
+      (push (find-file-noselect "../README") bufs)
+      (push (find-file-noselect "other/README") bufs)
+      (should (equal (mapcar #'buffer-name bufs)
+                     '("README<other>" "README<bar>")))
+      (push (find-file-noselect "foo2/bar/README") bufs)
+      (should (equal (mapcar #'buffer-name bufs)
+                     '("README<foo2/bar>" "README<other>" "README<foo1/bar>")))
+      (while bufs
+        (kill-buffer (pop bufs))))))
+
 (provide 'uniquify-tests)
 ;;; uniquify-tests.el ends here
-- 
2.39.3