Subject: bug#65116: 29.1; query-replace-read-args fails reading second arg in detached minibuf

From: Alan Mackenzie <[email-masked]>
Date: Sat, 13 Jan 2024 20:06:36 +0000

Hello again, Jim.

On Sat, Jan 13, 2024 at 11:00:50 -0600, Jim Rees wrote:
> Could it have something to do with the mouse warping? Why is the mouse
> warping at all? I would prefer it stay right where it is. I'm pretty sure it
> did at some recent time in the past, maybe emacs 26.

I think I've got it.  The problem was which frame has X-Windows's
focus.  Most of the time, that's the minibuffer's/echo-area's frame,
because at the end of most commands, something gets written to the
echo-area.

In the current bug situation, after the first read-minibuffer of M-%,
nothing is output to the echo-area, but the GUI focus is switched to the
main frame.  It stays there for the second read-minibuffer, and that's
what you saw on your terminal.

The solution would appear to be explicitly to switch the focus to the
minibuffer frame before attempting to read from it.  The following patch
does this, and appears to work for me.

diff --git a/src/minibuf.c b/src/minibuf.c
index f4f9da9c3f9..d940c0d717f 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -825,6 +825,11 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
   /* Use set_window_buffer instead of Fset_window_buffer (see
      discussion of bug#11984, bug#12025, bug#12026).  */
   set_window_buffer (minibuf_window, Fcurrent_buffer (), 0, 0);
+  /* Make sure the minibuffer's frame has the input focus.  This is
+     particularly for the twm window manager with detached minibuffer.
+     (See Bug#65116.)  */
+  if (!EQ (mini_frame, selected_frame))
+    call2 (Qselect_frame_set_input_focus, mini_frame, Qt);
   Fselect_window (minibuf_window, Qnil);
   XWINDOW (minibuf_window)->hscroll = 0;
   XWINDOW (minibuf_window)->suspend_auto_hscroll = 0;

Would you please apply the patch, rebuild your Emacs, and see if the
problem really is solved.  The patch should apply cleanly to either the
Emacs master, or the 29.x sources.  (I'm assuming you have no troubles
with patching and building, but if so, feel free to send me private
email.)  Then please confirm things are OK so that I can close the bug,
or tell me what's still not working properly.

Thanks!

-- 
Alan Mackenzie (Nuremberg, Germany).