unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: sbaugh@catern.com
To: Dmitry Gutov <dmitry@gutov.dev>
Cc: Spencer Baugh <sbaugh@janestreet.com>, 64088@debbugs.gnu.org
Subject: bug#64088: [PATCH] Make project-root for vc projects return an absolute path
Date: Sat, 19 Aug 2023 12:57:19 +0000 (UTC)	[thread overview]
Message-ID: <87lee7gpg1.fsf@catern.com> (raw)
In-Reply-To: <87ttsvgqnt.fsf@catern.com> (sbaugh@catern.com's message of "Sat,  19 Aug 2023 12:31:03 +0000 (UTC)")

[-- Attachment #1: Type: text/plain, Size: 2350 bytes --]

sbaugh@catern.com writes:
> Dmitry Gutov <dmitry@gutov.dev> writes:
>> On 11/07/2023 02:45, Spencer Baugh wrote:
>>> Dmitry Gutov <dmitry@gutov.dev> writes:
>>>> On 27/06/2023 23:01, Spencer Baugh wrote:
>>>>> OK, how about this?
>>>>
>>>> Maybe we should go in the other direction? And call
>>>> abbreviate-file-name on them?
>>>>
>>>> Because otherwise in project-prompt-project-dir we'll always show the
>>>> expanded directory names, taking up extra space and usually repeating
>>>> the full name of the user's home directory for no good reason.
>>> That seems reasonable if we let-bind directory-abbrev-alist to nil
>>> around it; otherwise we'll be dependent on the user's configuration and
>>> we might add a directory in one way, and then they add a new value to
>>> directory-abbrev-alist and we add it again a second way.
>>
>> That's also fair.
>>
>>> Although maybe that's fine?  And probably users of
>>> directory-abbrev-alist would like to have those abbreviations show up in
>>> project-prompt-project-dir.
>>
>> They probably would. But indeed if the list was saved with one
>> configuration, and read with another, it could lead to a mistake.
>>
>>> Also we could always call abbreviate-file-name at
>>> project-prompt-project-dir time.
>>
>> Meaning it will be called N times (for the number of projects) every
>> time the list is displayed, instead of just once, when the project is
>> saved/visited. But maybe it's fine too? How slow could that be?
>
> I was sad about this but I think I've got the solution now: We just
> maintain project--list in abbreviated form and project-list-file in
> expanded form.
>
> I think that solves all the problems: the persistent file is independent
> of user configuration, so if the user changes their abbreviations
> they'll get new ones the next time the file is read (presumably the next
> time they run Emacs).  But project--list is abbreviated, so
> project-prompt-project-dir gets abbreviated dirs.
>
> It does mean that if they change their directory-abbrev-alist while
> Emacs is running they could get duplicate entries, but that already
> could happen before this patch, and it's not a big problem IMO.
>
> See patch below.

Oops, this patch needed a few tweaks.  Especially, skipping the
expansion and abbreviation for remote files, to avoid making unnecesary
remote connections.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Expand-project-file-names-before-storing-them.patch --]
[-- Type: text/x-patch, Size: 3355 bytes --]

From 9523b4dea1234083e65b141a281c1108f52b046b Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@catern.com>
Date: Sat, 19 Aug 2023 08:24:45 -0400
Subject: [PATCH] Expand project file names before storing them

Before, whatever project-root returned, we stored as the root
directory of the project in project-list and project-list-file.  This
could lead to duplicate entries or bad behavior if projects were
accessed by different file names, e.g. both /home/user/src/emacs and
~/src/emacs.

Now project-list-file contains only expanded paths and project--list
contains only abbreviated paths.  We abbreviate filenames before
setting project--list, and expand filenames before writing to
project-list-file.  We only do this for local files, though, to avoid
making remote connections; the situation will still be bad for remote
projects, but at least this is an improvement.

* lisp/progmodes/project.el (project--write-project-list): Call
expand-file-name.
(project--read-project-list, project-remember-project)
(project--remove-from-project-list): Call abbreviate-file-name.
---
 lisp/progmodes/project.el | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 11fa93fb70d..21d5df8f0cc 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1586,7 +1586,12 @@ project--read-project-list
           (when (file-exists-p filename)
             (with-temp-buffer
               (insert-file-contents filename)
-              (read (current-buffer)))))
+              (mapcar
+               (lambda (elem)
+                 (let ((name (car elem)))
+                   (list (if (file-remote-p name) name
+                           (abbreviate-file-name name)))))
+               (read (current-buffer))))))
     (unless (seq-every-p
              (lambda (elt) (stringp (car-safe elt)))
              project--list)
@@ -1606,7 +1611,12 @@ project--write-project-list
       (insert ";;; -*- lisp-data -*-\n")
       (let ((print-length nil)
             (print-level nil))
-        (pp project--list (current-buffer)))
+        (pp (mapcar (lambda (elem)
+                      (let ((name (car elem)))
+                        (list (if (file-remote-p name) name
+                                (expand-file-name name)))))
+                    project--list)
+            (current-buffer)))
       (write-region nil nil filename nil 'silent))))
 
 ;;;###autoload
@@ -1615,7 +1625,7 @@ project-remember-project
 Save the result in `project-list-file' if the list of projects
 has changed, and NO-WRITE is nil."
   (project--ensure-read-project-list)
-  (let ((dir (project-root pr)))
+  (let ((dir (abbreviate-file-name (project-root pr))))
     (unless (equal (caar project--list) dir)
       (dolist (ent project--list)
         (when (equal dir (car ent))
@@ -1631,7 +1641,7 @@ project--remove-from-project-list
 from the list using REPORT-MESSAGE, which is a format string
 passed to `message' as its first argument."
   (project--ensure-read-project-list)
-  (when-let ((ent (assoc project-root project--list)))
+  (when-let ((ent (assoc (abbreviate-file-name project-root) project--list)))
     (setq project--list (delq ent project--list))
     (message report-message project-root)
     (project--write-project-list)))
-- 
2.41.0


  reply	other threads:[~2023-08-19 12:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-15 20:48 bug#64088: [PATCH] Make project-root for vc projects return an absolute path Spencer Baugh
2023-06-16  5:52 ` Eli Zaretskii
2023-06-17  2:43   ` Dmitry Gutov
2023-06-17  6:25     ` Eli Zaretskii
2023-06-18  0:43       ` Dmitry Gutov
2023-06-17  2:47 ` Dmitry Gutov
2023-06-27 20:01   ` Spencer Baugh
2023-07-10  2:17     ` Dmitry Gutov
2023-07-10 23:45       ` Spencer Baugh
2023-08-12  0:45         ` Dmitry Gutov
2023-08-19 12:31           ` sbaugh
2023-08-19 12:57             ` sbaugh [this message]
2023-08-23  0:24               ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lee7gpg1.fsf@catern.com \
    --to=sbaugh@catern.com \
    --cc=64088@debbugs.gnu.org \
    --cc=dmitry@gutov.dev \
    --cc=sbaugh@janestreet.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).