Subject: Re: bug#45072: 28.0.50; Emacs switches other buffer back uncontrollably, if other window's buffer is changed by user during minibuffer editing

From: Juri Linkov <[email-masked]>
Date: Wed, 04 Aug 2021 23:17:20 +0300

>>>> +  ;; When read_minibuf doesn't restore all previous windows,
>>>> +  ;; then at least pop down the completions window.
>>>> +  (unless read-minibuffer-restore-windows
>>>> +    (minibuffer-hide-completions))
>>>
>>> Hm...  Well, I guess that's what most people would want...  but...
>>
>> The new option read-minibuffer-restore-windows is quite unusable
>> without the above change: selecting a completion from the
>> completions buffer will leave the completions buffer on the screen.
>
> Yes.  But I think some people would want that -- that is, they might be
> `C-g'-ing out of the minubuffer just because they want to copy some of
> the text in the completions buffer.
>
> But on the other hand, that may be such a rare thing to do that just
> applying your proposed patch is the right thing, and then we could just
> see whether anybody actually requests that before tinkering any further
> with it...
>
> So...  I think you should just push the patch, and then we'll see.

Actually, my previous patch doesn't handle the case of `C-g'-ing
out of the minubuffer.  It hides the completions buffer
only after exiting in the normal way by RET.

But fortunately there is an existing hook 'minibuffer-exit-hook'
called in both cases of exiting by `C-g' and RET.
And the users can easily change it, e.g. to not pop down
the *Completions* buffer, or to remove more windows, etc.

diff --git a/etc/NEWS b/etc/NEWS
index f0fa686bc9..cca6956275 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -180,6 +180,8 @@ nor t.
 
 +++
 ** New user option 'read-minibuffer-restore-windows'.
+When customized to nil, it uses 'minibuffer-restore-windows' in
+'minibuffer-exit-hook' to remove only the *Completions* window.
 
 +++
 ** New system for displaying documentation for groups of functions.
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 3751ba80e0..79fb7e6afc 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -2328,6 +2342,16 @@ exit-minibuffer
   (setq deactivate-mark nil)
   (throw 'exit nil))
 
+(defun minibuffer-restore-windows ()
+  "Restore some windows on exit from minibuffer.
+When `read-minibuffer-restore-windows' is nil, then this function
+added to `minibuffer-exit-hook' will remove at least the window
+with the *Completions* buffer."
+  (unless read-minibuffer-restore-windows
+    (minibuffer-hide-completions)))
+
+(add-hook 'minibuffer-exit-hook 'minibuffer-restore-windows)
+
 (defun minibuffer-quit-recursive-edit ()
   "Quit the command that requested this recursive edit without error.
 Like `abort-recursive-edit' without aborting keyboard macro
diff --git a/src/minibuf.c b/src/minibuf.c
index 3ee0dca5e0..a054f0e20d 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -2535,8 +2535,11 @@ syms_of_minibuf (void)
 If this is non-nil (the default), reading input with the minibuffer will
 restore, on exit, the window configurations of the frame where the
 minibuffer was entered from and, if it is different, the frame that owns
-the associated minibuffer window.  If this is nil, no such restorations
-are done.  */);
+the associated minibuffer window.
+
+If this is nil, no such restorations are done.
+But still `minibuffer-restore-windows' in `minibuffer-exit-hook'
+will remove the window with the *Completions* buffer.  */);
   read_minibuffer_restore_windows = true;
 
   defsubr (&Sactive_minibuffer_window);