GNU bug report logs - #66260
29.0.92; project.el should support copying a file path relative to the project root

Previous Next

Package: emacs;

Reported by: sbaugh <at> catern.com

Date: Thu, 28 Sep 2023 23:28:02 UTC

Severity: wishlist

Found in version 29.0.92

Full log


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

From: sbaugh <at> catern.com
To: Dmitry Gutov <dmitry <at> gutov.dev>
Cc: 66260 <at> debbugs.gnu.org
Subject: Re: bug#66260: 29.0.92; project.el should support copying a file path
 relative to the project root
Date: Sat, 21 Oct 2023 13:13:34 +0000 (UTC)
[Message part 1 (text/plain, inline)]
Dmitry Gutov <dmitry <at> gutov.dev> writes:
> On 29/09/2023 02:26, sbaugh <at> catern.com wrote:
>> When communicating with others, it is often useful to copy a file path
>> to the current file which is relative to the project root.  For example
>> I'd rather send someone the path lisp/progmodes/project.el than
>> ~/src/emacs/trunk/lisp/progmodes/project.el.
>> project.el should have a way to copy this file path.
>> Almost good enough is:
>>   C-x p f       ;; project-find-file
>>   M-n	       ;; next-history-element
>>   C-k	       ;; kill-line
>> but with project--read-file-cpd-relative, if all the files in
>> project-files start with a common-parent-directory which is a
>> subdirectory of project-root, the resulting file path will not be
>> relative to the project-root but to the subdirectory.
>
> A problem with modifying the above function's behavior like that is
> that sometimes it is passed a wider range of file names, where not all
> of them are inside the project root (M-x
> project-or-external-find-file).

Oh, what if we just *check* if the common-parent-directory is below the
project root, and in that case use the project root instead?  That
elegantly resolves this without breaking that.

Like this:

[0001-Use-project-root-in-project-find-file-if-all-files-a.patch (text/x-patch, inline)]
From c5b2e49cc8117e063d9e16a9072eb07ddff8aaa5 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh <at> catern.com>
Date: Sat, 21 Oct 2023 09:11:52 -0400
Subject: [PATCH] Use project-root in project-find-file if all files are below
 it

project-files can return a list of files all of whom are in a
subdirectory of project-root.  We should still use project-root in
project--read-file-cpd-relative in that case, for several reasons:

- Now M-n will always bring up the full path of the current file for
  copying

- Now if the user wants to manually type in a path starting at the
  root, they can.

* lisp/progmodes/project.el (project--read-file-cpd-relative):
Always use project-root if all files are below that. (bug#66260)
---
 lisp/progmodes/project.el | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 0d6539113cc..dcd1cab4f96 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1117,9 +1117,16 @@ project--read-file-cpd-relative
 MB-DEFAULT is used as part of \"future history\", to be inserted
 by the user at will."
   (let* ((common-parent-directory
-          (let ((common-prefix (try-completion "" all-files)))
-            (if (> (length common-prefix) 0)
-                (file-name-directory common-prefix))))
+          (let* ((common-prefix (try-completion "" all-files))
+                 (root (project-root (project-current)))
+                 (expand-root (expand-file-name root))
+                 (abbrev-root (abbreviate-file-name root)))
+            (cond
+             ;; We try use the project-root even if all the files have
+             ;; a c-p-d below the project-root.
+             ((string-prefix-p expand-root common-prefix) expand-root)
+             ((string-prefix-p abbrev-root common-prefix) abbrev-root)
+             ((> (length common-prefix) 0) (file-name-directory common-prefix)))))
          (cpd-length (length common-parent-directory))
          (prompt (if (zerop cpd-length)
                      prompt
-- 
2.41.0


This bug report was last modified 1 year and 293 days ago.

Previous Next


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