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

From: Eli Zaretskii <[email-masked]>
Date: Fri, 09 Jun 2023 14:16:17 +0300

> From: Al Petrofsky <[email-masked]>
> Date: Thu, 8 Jun 2023 17:32:56 -0400
> 
> While the minibuffer window is active, attempt to switch buffers in
> an ordinary window like so:
> 
> emacs -Q
> M-: (setq enable-recursive-minibuffers t) RET
> M-x C-x o C-x b foo RET
> 
> In emacs 27 and earlier, that will switch the buffer in the main
> window to the new buffer named "foo", but in emacs 28.2, it generates
> a bogus "user-error: Cannot switch buffers in minibuffer window".

Seems like read-buffer-to-switch (called by "C-x b") changes the
selected-window: when it returns, the rest of the function runs with
the minibuffer window being the selected-window, which is wrong in
this case.

I couldn't find the offending change, but I doubt that bisection would
help us in this case, given how many water went under the bridge of
using the minibuffer and saving/restoring the window configuration.

The ugly kludge below seems to fix the problem.

Stefan and Martin, any better ideas or hints?

diff --git a/lisp/window.el b/lisp/window.el
index a11b1a5..6777944 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -8941,7 +8941,9 @@ switch-to-buffer
                     "Cannot switch buffers in a dedicated window")))
                 ('pop nil)
                 (_ (set-window-dedicated-p nil nil) 'force-same-window)))))))
-     (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
+     (save-selected-window
+       (list
+        (read-buffer-to-switch "Switch to buffer: ") nil force-same-window))))
   (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))
         (set-window-start-and-point (not switch-to-buffer-obey-display-actions)))
     (cond