Subject: bug#28513:

From: "Ryan C. Thompson" <[email-masked]>
Date: Fri, 1 Jan 2021 16:51:56 -0500

Hello,

I believe this bug (#28513) is a duplicate of #19412. In the discussion 
for #19412 I have investigated the problem and proposed a fix that 
doesn't require special-casing anything. However, due to being quite 
busy since then I have not had the time to properly test my fix. 
Briefly, the fix is to pass all the original args to the fallback 
function unchanged and then use "minibuffer-with-setup-hook" to simulate 
deleting the initial input and then typing whatever the user currently 
had typed into ido before they triggered the fallback.

I have rebased the patch from that thread onto the current master and 
attached it. To reiterate, this patch should be tested before merging.

Regards,

Ryan

From 41feb86c67390ce6c17fd59ddfc6e7288aa47d64 Mon Sep 17 00:00:00 2001
From: "Ryan C. Thompson" <[email-masked]>
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.

Note: This also reverts commit 526abadd07, which is intended to be a fix
for bug #28513 which I believe is a duplicate of bug #19412. I believe
the fix in this patch is better because it should fix every case
without the need to special-case specific functions.
---
 lisp/ido.el | 63 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 47 insertions(+), 16 deletions(-)

diff --git a/lisp/ido.el b/lisp/ido.el
index 89b6a62f5a..2d0a9e69e2 100644
--- a/lisp/ido.el
+++ b/lisp/ido.el
@@ -2363,20 +2363,23 @@ ido-file-internal
        ((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)
-          (if (eq this-command 'write-file)
-              (write-file (read-file-name
-                           "Write file: "
-                           default-directory
-                           (and buffer-file-name
-                                (expand-file-name
-                                 (file-name-nondirectory buffer-file-name)
-                                 default-directory)))
-                          t)
-	    (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
@@ -4871,7 +4874,8 @@ ido-read-file-name
   "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)
@@ -4925,7 +4929,21 @@ ido-read-file-name
     (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
@@ -4934,6 +4952,7 @@ ido-read-directory-name
 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
@@ -4950,12 +4969,24 @@ ido-read-directory-name
 			    (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
-			     default-dirname mustmatch initial)))
+        ;; 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-dirname mustmatch initial))))
      ((equal filename ".") ido-current-directory)
      (t (concat ido-current-directory filename)))))
 
-- 
2.29.2