(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.
(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
"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
(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.
;;;###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))
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