From 4a9443ddd8be57b2a74cdabf5aa0d2cfbba35053 Mon Sep 17 00:00:00 2001 From: "Ryan C. Thompson" Date: Wed, 11 Mar 2020 12:24:24 -0400 Subject: [PATCH] Fix default directory handling in ido file fallback (bug#19412) Briefly, when falling back from ido file completion to normal file completion, previously the current directory at the time of falling back was treated as the default directory, which was wrong and caused unintuitive edge cases. Now, when falling back for file completion, ido uses the original default directory that ido was called with and then uses `minibuffer-with-setup-hook' to "simulate" typing in the currently entered directory, so that it is not treated as the default. See the bug description for more information. --- lisp/ido.el | 54 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/lisp/ido.el b/lisp/ido.el index 81883402ad..bf35df9547 100644 --- a/lisp/ido.el +++ b/lisp/ido.el @@ -2355,11 +2355,23 @@ If cursor is not at the end of the user input, move to end of input." ((eq ido-exit 'fallback) ;; Need to guard setting of default-directory here, since ;; we don't want to change directory of current buffer. - (let ((default-directory ido-current-directory) - (read-file-name-function nil)) + (let ((default-directory default-directory) + (read-file-name-function nil)) (setq this-command (or ido-fallback fallback 'find-file)) (run-hook-with-args 'ido-before-fallback-functions this-command) - (call-interactively this-command))) + ;; Workaround for bug#19412: ensure that pressing RET + ;; immediately after falling back with C-f will select the + ;; input rather than use the default (which is + ;; `default-directory'). + (minibuffer-with-setup-hook + (:append + (lambda () + ;; Clear out whatever started in the minibuffer and + ;; replace it with what the user had already entered + ;; into ido. + (delete-minibuffer-contents) + (insert (abbreviate-file-name ido-current-directory)))) + (call-interactively this-command)))) ((eq ido-exit 'switch-to-buffer) (ido-buffer-internal @@ -4841,7 +4853,8 @@ buffers that can be considered." "Ido replacement for the built-in `read-file-name'. Read file name, prompting with PROMPT and completing in directory DIR. See `read-file-name' for additional parameters." - (let (filename) + (let (filename + (orig-dir dir)) (cond ((and (not (memq this-command ido-read-file-name-non-ido)) (or (eq predicate 'file-directory-p) @@ -4895,7 +4908,21 @@ See `read-file-name' for additional parameters." (if (eq filename 'fallback) (let ((read-file-name-function nil)) (run-hook-with-args 'ido-before-fallback-functions 'read-file-name) - (read-file-name prompt dir default-filename mustmatch initial predicate)) + ;; Bug#19412: need to pass original DIR to `read-file-name' + ;; but start with current value of DIR entered in + ;; minibuffer, so that it correctly handles a default that + ;; is not in the current directory. See also bug#1516. + ;; (ido-trace "read-file-name fallback" (list prompt orig-dir default-filename mustmatch initial predicate)) + ;; (ido-trace "read-file-name fallback initial" dir) + (minibuffer-with-setup-hook + (:append + (lambda () + ;; Clear out whatever started in the minibuffer and + ;; replace it with what the user had already entered + ;; into ido. + (delete-minibuffer-contents) + (insert (abbreviate-file-name dir)))) + (read-file-name prompt orig-dir default-filename mustmatch initial predicate))) filename))) ;;;###autoload @@ -4904,6 +4931,7 @@ See `read-file-name' for additional parameters." Read directory name, prompting with PROMPT and completing in directory DIR. See `read-directory-name' for additional parameters." (let* (filename + (orig-dir dir) (minibuffer-completing-file-name t) (ido-context-switch-command 'ignore) ido-saved-vc-hb @@ -4920,11 +4948,25 @@ See `read-directory-name' for additional parameters." (expand-file-name initial ido-current-directory) ido-current-directory)) mustmatch initial)) + (setq dir ido-current-directory) (cond ((eq ido-exit 'fallback) (let ((read-file-name-function nil)) (run-hook-with-args 'ido-before-fallback-functions 'read-directory-name) - (read-directory-name prompt ido-current-directory + ;; Bug#19412: need to pass original DIR to `read-file-name' + ;; but start with current value of DIR entered in minibuffer, + ;; so that it correctly handles a default that is not in the + ;; current directory. + (minibuffer-with-setup-hook + (:append + (lambda () + ;; Clear out whatever started in the minibuffer and + ;; replace it with what the user had already entered + ;; into ido. + (delete-minibuffer-contents) + (insert (abbreviate-file-name dir)))) + (read-directory-name prompt orig-dir default-filename mustmatch initial predicate)) + (read-directory-name prompt ido-current-directory default-dirname mustmatch initial))) ((equal filename ".") ido-current-directory) (t (concat ido-current-directory filename))))) -- 2.25.0