all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: sbaugh@catern.com
To: Dmitry Gutov <dmitry@gutov.dev>
Cc: 66260@debbugs.gnu.org
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)	[thread overview]
Message-ID: <87jzrgt8xu.fsf@catern.com> (raw)
In-Reply-To: <15010505-0b2f-5dc3-e16a-228183779d43@gutov.dev> (Dmitry Gutov's message of "Mon, 2 Oct 2023 01:44:38 +0300")

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

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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Use-project-root-in-project-find-file-if-all-files-a.patch --]
[-- Type: text/x-patch, Size: 2083 bytes --]

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


      parent reply	other threads:[~2023-10-21 13:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-28 23:26 bug#66260: 29.0.92; project.el should support copying a file path relative to the project root sbaugh
2023-09-29  6:57 ` Juri Linkov
2023-09-30 13:50   ` sbaugh
2023-09-30 14:47     ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-30 15:17       ` sbaugh
2023-10-01 22:44 ` Dmitry Gutov
2023-10-02  5:57   ` Eli Zaretskii
2023-10-02  6:53     ` Juri Linkov
2023-10-02 11:46       ` Dmitry Gutov
2023-10-03 20:49         ` sbaugh
2023-10-03 21:17           ` Dmitry Gutov
2023-10-02 11:13     ` Rudolf Adamkovič via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-21 13:13   ` sbaugh [this message]

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

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

  git send-email \
    --in-reply-to=87jzrgt8xu.fsf@catern.com \
    --to=sbaugh@catern.com \
    --cc=66260@debbugs.gnu.org \
    --cc=dmitry@gutov.dev \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.