Use current file name for more "other project" future history

In the previous commit we added support for 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, 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.
This commit is contained in:
Spencer Baugh 2023-08-23 05:11:55 +03:00 committed by Dmitry Gutov
parent e3209923c3
commit 8933c1877d

View file

@ -989,6 +989,23 @@ pattern to search for."
(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, 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 @@ for VCS directories listed in `vc-directory-exclusion-list'."
(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 @@ for VCS directories listed in `vc-directory-exclusion-list'."
"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
@ -1035,7 +1044,10 @@ for VCS directories listed in `vc-directory-exclusion-list'."
(project-root pr)
(project-external-roots pr)))
(project-file-history-behavior t))
(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.
@ -1190,7 +1202,10 @@ directories listed in `vc-directory-exclusion-list'."
;;;###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))
@ -1205,7 +1220,9 @@ directories listed in `vc-directory-exclusion-list'."
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