unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Ryan C. Thompson" <rct@thompsonclan.org>
To: 19412@debbugs.gnu.org
Subject: bug#19412: 24.3; ido-write-file sometimes writes to a different directory than, it says it will
Date: Wed, 11 Mar 2020 12:46:44 -0400	[thread overview]
Message-ID: <f4d4b86f-97b3-bb8a-28a6-a6d1b09929f8@thompsonclan.org> (raw)
In-Reply-To: <2c4460c3-ad84-b706-189e-07429a3b2500@thompsonclan.org>

[-- Attachment #1: Type: text/plain, Size: 488 bytes --]

Ok, I think I have a working patch for this issue. I patched 
ido-file-internal, ido-read-file-name, and ido-read-directory-name, and 
I think that's all the code paths that need to be fixed. Now the test 
case described in the original report produces the correct result for 
me: writing to /tmp/mumble.frotz.

However, I should note that this is still relatively untested. I will 
test it out and try to make sure it doesn't cause any unexpected issues 
before I recommend merging it.


[-- Attachment #2: 0001-Fix-default-directory-handling-in-ido-file-fallback-.patch --]
[-- Type: text/plain, Size: 5683 bytes --]

From 4a9443ddd8be57b2a74cdabf5aa0d2cfbba35053 Mon Sep 17 00:00:00 2001
From: "Ryan C. Thompson" <rct@thompsonclan.org>
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


  reply	other threads:[~2020-03-11 16:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-19 20:21 bug#19412: 24.3; ido-write-file sometimes writes to a different directory than it says it will Don Morrison
2019-11-03 22:48 ` bug#19412: 24.3; ido-write-file sometimes writes to a different directory than, " Ryan C. Thompson
2019-11-04 14:52   ` Ryan C. Thompson
2019-11-04 15:55     ` Ryan C. Thompson
2020-03-11 16:46       ` Ryan C. Thompson [this message]
2020-08-12 16:44         ` Stefan Kangas
2021-01-10 23:12           ` Ryan C. Thompson
2021-01-11 14:14             ` Lars Ingebrigtsen
2021-01-11 14:28               ` Ryan C. Thompson
2021-01-11 18:43                 ` Lars Ingebrigtsen
2021-01-11 18:50                   ` Ryan C. Thompson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f4d4b86f-97b3-bb8a-28a6-a6d1b09929f8@thompsonclan.org \
    --to=rct@thompsonclan.org \
    --cc=19412@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).