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

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

bug#63829: 29.0.90; project-find-file's future history breaks with commo


From: Spencer Baugh
Subject: bug#63829: 29.0.90; project-find-file's future history breaks with common-parent-directory
Date: Thu, 17 Aug 2023 16:12:33 -0400
User-agent: Gnus/5.13 (Gnus v5.13)

Spencer Baugh <sbaugh@janestreet.com> writes:
> In a subsequent mail I'll send a patch for the "future history"
> behavior of project-find-dir too.  (yet to be written)

Here is that.  Seems like a straightforward generalization, and
something that a user would expect to work.

>From 48880a795f75d1126a0f235bf57f00f478ee90a0 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Thu, 17 Aug 2023 16:11:01 -0400
Subject: [PATCH] Use current file name for more "other project" future history

In commit f38bcf37dc47ce172c985d1c621df3583eaad46c we supported using
the current buffer's file name as future history for project-find-file
even when switching to another project with project-switch-project.
Make this work for project-find-dir and project-or-external-find-file
too.

* lisp/progmodes/project.el (project--find-default-from): Add.
(project-find-file): Use project--find-default-from with
buffer-file-name.
(project-or-external-find-file): Use project--find-default-from with
buffer-file-name.
(project-find-dir): Use project--find-default-from with
default-directory.
---
 lisp/progmodes/project.el | 45 +++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 14 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index e0f1f995ff2..e0d05d45a55 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -989,6 +989,23 @@ project--read-regexp
     (read-regexp "Find regexp" (and sym (regexp-quote sym))
                  project-regexp-history-variable)))
 
+(defun project--find-default-from (filename project)
+  "Ensure FILENAME is in PROJECT.
+
+Usually, just return FILENAME.  But if
+`project-current-directory-override' is set, FILENAME is probably
+relative to that project; adjust it to be relative to PROJECT instead.
+
+This supports using a relative file name from the current buffer
+when switching projects with `project-switch-project' and then
+using a command like `project-find-file'."
+  (if-let (filename-proj (and project-current-directory-override
+                            (project-current nil default-directory)))
+      ;; file-name-concat requires Emacs 28+
+      (concat (file-name-as-directory (project-root project))
+              (file-relative-name filename (project-root filename-proj)))
+    filename))
+
 ;;;###autoload
 (defun project-find-file (&optional include-all)
   "Visit a file (with completion) in the current project.
@@ -1006,16 +1023,7 @@ project-find-file
          (dirs (list root)))
     (project-find-file-in
      (or (thing-at-point 'filename)
-         (and buffer-file-name
-              (if-let (buffer-proj (and project-current-directory-override
-                                        (project-current nil 
default-directory)))
-                  ;; Allow using the relative file name of the current
-                  ;; buffer in "other project" as well.
-                  (let ((buffer-root (project-root buffer-proj)))
-                    ;; file-name-concat requires Emacs 28+
-                    (concat (file-name-as-directory root)
-                            (file-relative-name buffer-file-name buffer-root)))
-                buffer-file-name)))
+         (and buffer-file-name (project--find-default-from buffer-file-name 
pr)))
      dirs pr include-all)))
 
 ;;;###autoload
@@ -1023,7 +1031,8 @@ project-or-external-find-file
   "Visit a file (with completion) in the current project or external roots.
 
 The filename at point (determined by `thing-at-point'), if any,
-is available as part of \"future history\".
+is available as part of \"future history\".  If none, the current
+buffer's file name is used.
 
 If INCLUDE-ALL is non-nil, or with prefix argument when called
 interactively, include all files under the project root, except
@@ -1033,7 +1042,10 @@ project-or-external-find-file
          (dirs (cons
                 (project-root pr)
                 (project-external-roots pr))))
-    (project-find-file-in (thing-at-point 'filename) dirs pr include-all)))
+    (project-find-file-in
+     (or (thing-at-point 'filename)
+         (and buffer-file-name (project--find-default-from buffer-file-name 
pr)))
+     dirs pr include-all)))
 
 (defcustom project-read-file-name-function #'project--read-file-cpd-relative
   "Function to call to read a file name from a list.
@@ -1185,7 +1197,10 @@ project--completing-read-strict
 
 ;;;###autoload
 (defun project-find-dir ()
-  "Start Dired in a directory inside the current project."
+  "Start Dired in a directory inside the current project.
+
+The current buffer's `default-directory' is available as part of
+\"future history\"."
   (interactive)
   (let* ((project (project-current t))
          (all-files (project-files project))
@@ -1200,7 +1215,9 @@ project-find-dir
                project "Dired"
                ;; Some completion UIs show duplicates.
                (delete-dups all-dirs)
-               nil 'file-name-history)))
+               nil 'file-name-history
+               (and default-directory
+                    (project--find-default-from default-directory project)))))
     (dired dir)))
 
 ;;;###autoload
-- 
2.39.3


reply via email to

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