Subject: bug#63967: 28.2; switch-to-buffer in normal window fails if minibuffer window is active

From: Alan Mackenzie <[email-masked]>
Date: Sat, 10 Jun 2023 19:42:04 +0000

Hello, Stefan.

On Sat, Jun 10, 2023 at 11:49:43 -0400, Stefan Monnier wrote:
> Hi Alan,

> Could you explain why/when we need to call `Fset_frame_selected_window`
> in the code below:

> >   static void
> >   minibuffer_unwind (void)
> >   {
> >     struct frame *f;
> >     struct window *w;
> >     Lisp_Object window;
> >     Lisp_Object entry;
> >
> >     if (NILP (exp_MB_frame)) return; /* "Can't happen." */
> >     f = XFRAME (exp_MB_frame);
> >     window = f->minibuffer_window;
> >     w = XWINDOW (window);
> >     if (FRAME_LIVE_P (f))
> >       {
> > 	/* minibuf_window = sf->minibuffer_window; */
> > 	if (!NILP (w->prev_buffers))
> > 	  {
> > 	    entry = Fcar (w->prev_buffers);
> > 	    w->prev_buffers = Fcdr (w->prev_buffers);
> > 	    set_window_buffer (window, Fcar (entry), 0, 0);
> > 	    Fset_window_start (window, Fcar (Fcdr (entry)), Qnil);
> > 	    Fset_window_point (window, Fcar (Fcdr (Fcdr (entry))));
> > 	    /* set-window-configuration may/will have unselected the
> > 	       mini-window as the selected window.  Restore it. */
> > 	    Fset_frame_selected_window (exp_MB_frame, window, Qnil);  <<<<<<<<<
> > 	  }
> > 	else
> > 	  set_window_buffer (window, nth_minibuffer (0), 0, 0);
> >       }
> >   }

> I understand why we do the `set_window_buffer` and set its start and
> point, but I can't see why we'd need to select the mini-window:
> presumably if it needed to be (re)selected that should have been handled
> by the window-config save&restore, no?

There was a problem with the restoring of the window configuration
selecting a window other than the minibuffer; that is in circumstances
where the minibuffer window should have ended up being selected.

Fset_window_configuration seems to be the troublesome function in this
scenario.  As well as setting the window configuration, it also selects
a window.

Perhaps we should modify the minibuffer code to note which window should
be current after the completion or abortion of the minibuffer read
action.

>         Stefan

-- 
Alan Mackenzie (Nuremberg, Germany).