Subject: bug#55983: 29.0.50; If xref-goto-xref pops up a frame, it doesn't set its input focus

From: <[email-masked]>
Date: Sun, 19 Jun 2022 15:37:45 +0200

Dmitry Gutov <[email-masked]> writes:

> Hi!
>
> Thanks for the report.
>
> On 14.06.2022 23:17, miha--- via Bug reports for GNU Emacs, the Swiss 
> army knife of text editors wrote:
>> To reproduce, customize pop-up-frames to non-nil and set up your window
>> manager such that it doesn't automatically set input focus on newly
>> created frames.
>
> TBH I'm not sure how to setup GNOME Shell to do this.
>
>> Now in an xref-mode buffer, press RET on a search result
>> (xref-goto-xref) and notice that the newly created frame doesn't receive
>> input focus. That is because xref-goto-xref uses `display-buffer',
>> followed by a `select-window'.
>> 
>> To contrast this behaviour with grep-mode, pressing RET in a grep-mode
>> buffer (compile-goto-error) does set input focus to the newly created
>> frame. That is because compile-goto-error uses `pop-to-buffer`, which
>> also calls `select-frame-set-input-focus'.
>
> Window management in Emacs is a tricky business, and the current 
> implementation is a result of https://debbugs.gnu.org/28814 which 
> implemented a particular kind of behavior and 
> https://debbugs.gnu.org/33870 which did try to make it more customizable 
> through display-buffer-alist. See both discussions for more detail.
>
> I don't have the time at the moment to try to rework it myself.
>
> Patches welcome, though. But please mind the original implementation
> intent.

Please see the attached patch. The only thing it adds is setting frame
input focus if necessary before calling `select-window'. Other behaviour
should remain the same as before. (The code and the comment is lifted
from the function `pop-to-buffer'.)

Best regards.

From 6fbdecf5e6a1726498287679bc276dfc0af0633f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Miha=20Rihtar=C5=A1i=C4=8D?= <[email-masked]>
Date: Sun, 19 Jun 2022 15:29:59 +0200
Subject: [PATCH] xref-goto-xref: Set input focus in addition to selecting
 window

* lisp/progmodes/xref.el (xref--show-location): Set input focus in
addition to selecting displayed window (Bug#55983).
---
 lisp/progmodes/xref.el | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 683589d71c..0213ab3cc5 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -645,9 +645,15 @@ xref--show-location
              (xref-buffer (current-buffer)))
         (cond (select
                (if (eq select 'quit) (quit-window nil nil))
-               (select-window
-                (with-current-buffer xref-buffer
-                  (xref--show-pos-in-buf marker buf))))
+               (let* ((old-frame (selected-frame))
+                      (window (with-current-buffer xref-buffer
+                                (xref--show-pos-in-buf marker buf)))
+                      (frame (window-frame window)))
+                 ;; If we chose another frame, make sure it gets input
+                 ;; focus.
+                 (unless (eq frame old-frame)
+                   (select-frame-set-input-focus frame))
+                 (select-window window)))
               (t
                (save-selected-window
                  (xref--with-dedicated-window
-- 
2.36.0