bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#66260: 29.0.92; project.el should support copying a file path relati


From: sbaugh
Subject: 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)
User-agent: Gnus/5.13 (Gnus v5.13)

Dmitry Gutov <dmitry@gutov.dev> writes:
> On 29/09/2023 02:26, sbaugh@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:

>From c5b2e49cc8117e063d9e16a9072eb07ddff8aaa5 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]