Subject: Re: 24.3; ido-write-file sometimes writes to a different directory than, it says it will

From: "Ryan C. Thompson" <[email-masked]>
Date: Mon, 4 Nov 2019 06:52:19 -0800

I think I have found a fix for this issue. A patch for ido.el is 
attached. The solution is essentially to simulate re-typing the current 
ido input into the fallback command's prompt rather than modifying 
dynamic variables to trick the fallback command into starting in the 
right place.

Note that this is NOT thoroughly tested yet. It seems to work for the 
specific case described in this bug (ido-write-file), but I need to test 
it for some time to make sure it isn't breaking other cases at the same 
time. I will use this fix in my Emacs for some time and report back.
From 11f7ce6952550cbcec35492f41baa14025911cfa Mon Sep 17 00:00:00 2001
From: "Ryan C. Thompson" <[email-masked]>
Date: Sun, 3 Nov 2019 23:40:13 -0800
Subject: [PATCH] Ensure correct behavior in ido file fallback completion

Fixes bug #19412. Previously, when falling back, ido would let-bind
default-directory to ido-current-directory so that read-file-name
would start in the right location. However, this has an unexpected
side effect (see the bug report). Instead, ido now leaves
default-directory alone and instead uses minibuffer-with-setup-hook to
simulate the user deleting the initial input and typing
ido-current-directory instead.
---
 lisp/ido.el | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/lisp/ido.el b/lisp/ido.el
index 2a660e6b0c..c17972c1cc 100644
--- a/lisp/ido.el
+++ b/lisp/ido.el
@@ -2428,11 +2428,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 ido-current-directory)))
+            (call-interactively this-command))))
 
        ((eq ido-exit 'switch-to-buffer)
 	(ido-buffer-internal
-- 
2.23.0