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

From: martin rudalics <[email-masked]>
Date: Tue, 8 Dec 2020 09:09:28 +0100

> The defaults are definitely fine, IMO.  But if you want very much to
 > have an opt-in behavior to disable restoring of the window
 > configuration of the frame, I won't object to such an option.

Patch attached, just in case.  99% untested.

martin
diff --git a/src/minibuf.c b/src/minibuf.c
index fc3fd92a88..9874e71a2f 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -500,19 +500,21 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,

   record_unwind_protect_void (choose_minibuf_frame);

-  record_unwind_protect (restore_window_configuration,
-                         Fcons (Qt, Fcurrent_window_configuration (Qnil)));
+  if (!read_from_minibuffer_no_restore)
+    record_unwind_protect (restore_window_configuration,
+			   Fcons (Qt, Fcurrent_window_configuration (Qnil)));

   /* If the minibuffer window is on a different frame, save that
      frame's configuration too.  */
   mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
-  if (!EQ (mini_frame, selected_frame))
+
+  if (!read_from_minibuffer_no_restore && !EQ (mini_frame, selected_frame))
     record_unwind_protect (restore_window_configuration,
 			   Fcons (/* Arrange for the frame later to be
-                                     switched back to the calling
-                                     frame. */
-                                  Qnil,
-                                  Fcurrent_window_configuration (mini_frame)));
+				     switched back to the calling
+				     frame. */
+				  Qnil,
+				  Fcurrent_window_configuration (mini_frame)));

   /* If the minibuffer is on an iconified or invisible frame,
      make it visible now.  */
@@ -2186,6 +2188,14 @@ syms_of_minibuf (void)
 uses to hide passwords.  */);
   Vread_hide_char = Qnil;

+  DEFVAR_BOOL ("read-from-minibuffer-no-restore", read_from_minibuffer_no_restore,
+	       doc: /* Non-nil means `read-from-minibuffer' does not restore window configurations.
+If this is nil, `read-from-minibuffer' will restore, on exit, the window
+configurations of the frame where it was called from and, if it is
+different, the frame that owns the minibuffer window.  If this is
+non-nil, no such restorations are done.  */);
+  read_from_minibuffer_no_restore = false;
+
   defsubr (&Sactive_minibuffer_window);
   defsubr (&Sset_minibuffer_window);
   defsubr (&Sread_from_minibuffer);