Subject: bug#48674: Frames and minibuffer bug

From: Alan Mackenzie <[email-masked]>
Date: Thu, 27 May 2021 10:34:36 +0000

Hello, Iris and Martin.

Iris, thanks for taking the trouble to report this bug, and thanks even
more for cutting the test case down to the absolute minimum, making the
bug easier to diagnose.

On Wed, May 26, 2021 at 19:45:11 +0200, martin rudalics wrote:
> merge 48674 48675
> quit

> Thanks for the report.  I've just tried to merge this with the following
> one.

>  > 4. M-x (The bug appears, it is not possible to write in the minibuffer)

> The crucial part is the

>    (dolist (frame (frame-list))
>      (with-selected-frame frame

> setting the cursor is not necessary to reproduce it.  Since the bug does
> not appear with Emacs 27, this could be due to Alan's minibuffer window
> changes.

Sort of, almost.

What is happening is that the with-selected-frame invocation is
selecting (temporarily) a different frame from the minibuffer's frame.
This has the (intended) side effect of making the MB no longer selected
in that frame.  When the MB's frame becomes selected again, nothing
makes the mini-window the selected window.  This needs fixing.

I think it likely that this bug could be triggered in Emacs 27 by
putting something into minibuffer-setup-hook that selects a different
window in the MB's frame, though I haven't tried this.

>  > Note:
>  > As soon as the new frame is destroyed, it is possible again to write in the
>  > minibuffer.

> Right but as soon as I make another frame and type M-x I get

> completing-read-default: Command attempted to use minibuffer while in minibuffer

> so I'm at the same time in the active minibuffer and a normal buffer.

Yes.  I think the following patch should fix the trouble.  Iris, could
you please apply the patch (in directory ..../emacs/src) and rebuild
your Emacs-28, then try it out on your real lisp source code.  Should
you want any help with the patching or building, feel free to send me
private email.  Then please confirm that the problem is fixed, or tell
us what's still not working.  Thanks!

Martin, that Qt in the Fselect_window call (the NORECORD argument) -
would it be perhaps be better as Qnil?

diff --git a/src/minibuf.c b/src/minibuf.c
index cffb7fe787..3468643a7e 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -893,6 +893,11 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
 
   run_hook (Qminibuffer_setup_hook);
 
+  /* If the above hook has made the mini-window no longer the selected
+     window, restore it.  */
+  if (!EQ (selected_window, minibuf_window))
+    Fselect_window (minibuf_window , Qt);
+
   /* Don't allow the user to undo past this point.  */
   bset_undo_list (current_buffer, Qnil);

> martin

-- 
Alan Mackenzie (Nuremberg, Germany).