GNU bug report logs - #62621
29.0.60; uniquify can't make buffers unique based on things other than filename

Previous Next

Package: emacs;

Reported by: Spencer Baugh <sbaugh <at> janestreet.com>

Date: Sun, 2 Apr 2023 17:38:02 UTC

Severity: normal

Found in version 29.0.60

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


Message #32 received at 62621 <at> debbugs.gnu.org (full text, mbox):

From: Spencer Baugh <sbaugh <at> janestreet.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: sbaugh <at> catern.com, 62621 <at> debbugs.gnu.org
Subject: Re: bug#62621: 29.0.60; uniquify can't make buffers unique based on
 things other than filename
Date: Fri, 14 Jul 2023 08:20:02 -0400
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: sbaugh <at> catern.com
>> Date: Fri, 14 Jul 2023 11:28:13 +0000 (UTC)
>> Cc: sbaugh <at> janestreet.com, 62621 <at> debbugs.gnu.org
>> 
>> Eli Zaretskii <eliz <at> gnu.org> writes:
>> 
>> > If there are a couple of simpler alternatives which could be offered
>> > via simple symbolic values, we should not force everyone to write
>> > functions, IMNSHO.  IOW, we should NOT immediately generalize to
>> > functions only because such generalization could make sense in some
>> > use cases.  Repeat after me: Use options whose values are functions
>> > are hard on our users, because they require them to be Lisp
>> > programmers.
>> 
>> I agree, and I'm happy to change it to use a simple symbolic value
>> 'project instead for the transform I wrote, but I'm not sure how best to
>> handle the dependencies: uniquify.el is in loadup.el, is it OK for it to
>> rely on project-uniquify-dirname-transform being autoloaded?
>
> I don't understand the difficulty.  If the function value could be
> defined in uniquify.el, why cannot a symbolic value be defined there?

I don't understand your question :)

Here's concretely the diff I would apply.  This makes uniquify.el mention a
function from project.el.  Is that OK?

diff --git a/lisp/uniquify.el b/lisp/uniquify.el
index bd49346da45..4783830b184 100644
--- a/lisp/uniquify.el
+++ b/lisp/uniquify.el
@@ -168,14 +168,10 @@ 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
-  "A function to transform the dirname used to uniquify a buffer.
-
-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."
-  :type '(choice (function-item :tag "Don't change the dirname" identity)
-                 function)
+(defcustom uniquify-dirname-transform nil
+  "How to transform the dirname used to uniquify a buffer."
+  :type '(choice (const :tag "Don't change the dirname" nil)
+                 (const :tag "Include project-name when uniquifying" 'project))
   :group 'uniquify)
 
 ;;; Utilities
@@ -279,11 +275,14 @@ uniquify-buffer-file-name
 	       (if (memq major-mode uniquify-list-buffers-directory-modes)
 		   list-buffers-directory))))
       (when filename
-	 (funcall uniquify-dirname-transform
-	          (directory-file-name
-	          (file-name-directory
-	           (expand-file-name
-	            (directory-file-name filename)))))))))
+	(funcall
+         (cond ((not uniquify-dirname-transform) #'identity)
+               ((eq uniquify-dirname-transform 'project) #'project-uniquify-dirname-transform)
+               (t (error "bad uniquify-dirname-transform: %s" 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'."

> If the symbolic values are specific to project, simply let-bind
> uniquify-dirname-transform to the value of the appropriate project.el
> defcustom when project.el calls uniquify.

These customizations are in effect all the time, not just when the user
is calling a project.el command.  e.g. rename-buffer triggers uniquify.

>> >> +(defcustom uniquify-dirname-transform #'identity
>> >> +  "A function to transform the dirname used to uniquify a buffer.
>> >
>> > "Function to transform buffer's `default-directory' for uniquifying its name."
>> 
>> Unfortunately, this isn't quite right.  uniquify never uses
>> default-directory, counterintuitively - by default, it uses the
>> directory of buffer-file-name, which can differ from default-directory.
>
> That's a minor issue, just use "buffer's directory" instead.
>
> But I wonder why uniquify does something counterintuitive like that.

I assume it's something like "default-directory changes (such as by M-x
cd) shouldn't change the buffer name too".

Although personally I would be totally fine with cd changing the buffer
name...

The fact that uniquify doesn't use default-directory also means it's
unable to uniquify buffers which aren't visiting files, which can be
annoying.  For example, if uniquify used default-directory then it could
uniquify *compilation* buffers for different projects, renaming them
based on the directory.  But because it can't, every package has to come
up with their own special buffer-renaming scheme... include project.el.

I could add support for making uniquify use default-directory instead,
if that's interesting.




This bug report was last modified 2 years and 19 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.