Subject: bug#26513: 25.2; pop-up-frames and *Completions* buffer

From: martin rudalics <[email-masked]>
Date: Thu, 17 Feb 2022 11:01:50 +0100

>> >From emacs -Q:
 >>
 >> M-x set-variable RET pop-up-frames RET t RET
 >> M-x global- TAB
 >>
 >> The *Completions* buffer is opened in a new frame, but the cursor is in
 >> the frame too, so the user has to switch back to the minibuffer to
 >> continue typing.
 >
 > This problem is still present in Emacs 29.  I guess the general solution
 > here would be for completion to ensure that it's gotten focus back again
 > after displaying the *Completions* buffer?

Maybe Stefan Monnier can tell us what he does in such case.

I can offer the attached, largely untested patch.  A possible
customization would then be

(customize-set-variable
  'display-buffer-alist
  '(("*Completions*"
     (display-buffer-reuse-window
      display-buffer-pop-up-frame)
     (reusable-frames . t)
     (redirect-frame-focus . t))))

martin

diff --git a/lisp/window.el b/lisp/window.el
index 582600e1c6..96aa2ecc2d 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -7598,6 +7598,9 @@ display-buffer
     Possible values are nil (the selected frame), t (any live
     frame), visible (any visible frame), 0 (any visible or
     iconified frame) or an existing live frame.
+ `redirect-frame-focus' -- The value t means to redirect focus
+    from the frame used for display to the frame selected at the
+    time `display-buffer' was called (if these are different).
  `pop-up-frame-parameters' -- The value specifies an alist of
     frame parameters to give a new frame, if one is created.
  `window-height' -- The value specifies the desired height of the
@@ -7740,6 +7743,7 @@ display-buffer-use-some-frame
               (lambda (frame)
                 (and (not (eq frame (selected-frame)))
                      (get-lru-window frame)))))
+         (selected-frame (selected-frame))
          (frame (car (filtered-frame-list predicate)))
          (window
           (and frame
@@ -7749,7 +7753,10 @@ display-buffer-use-some-frame
       (prog1
           (window--display-buffer buffer window 'reuse alist)
         (unless (cdr (assq 'inhibit-switch-frame alist))
-          (window--maybe-raise-frame frame))))))
+          (window--maybe-raise-frame frame))
+        (when (and (cdr (assq 'redirect-frame-focus alist))
+                   (not (eq frame selected-frame)))
+          (redirect-frame-focus frame selected-frame))))))
 
 (defun display-buffer-same-window (buffer alist)
   "Display BUFFER in the selected window.
@@ -7817,6 +7824,7 @@ display-buffer-reuse-window
 called only by `display-buffer' or a function directly or
 indirectly called by the latter."
   (let* ((alist-entry (assq 'reusable-frames alist))
+         (selected-frame (selected-frame))
 	 (frames (cond (alist-entry (cdr alist-entry))
 		       ((if (eq pop-up-frames 'graphic-only)
 			    (display-graphic-p)
@@ -7845,7 +7853,11 @@ display-buffer-reuse-window
     (when (window-live-p window)
       (prog1 (window--display-buffer buffer window 'reuse alist)
 	(unless (cdr (assq 'inhibit-switch-frame alist))
-	  (window--maybe-raise-frame (window-frame window)))))))
+	  (window--maybe-raise-frame (window-frame window)))
+        (when (and (cdr (assq 'redirect-frame-focus alist))
+                   (not (eq (window-frame window) selected-frame)))
+          (redirect-frame-focus
+           (window-frame window) selected-frame))))))
 
 (defun display-buffer-reuse-mode-window (buffer alist)
   "Return a window based on the mode of the buffer it displays.
@@ -7918,7 +7930,11 @@ display-buffer-reuse-mode-window
         (when (window-live-p window)
           (prog1 (window--display-buffer buffer window 'reuse alist)
             (unless (cdr (assq 'inhibit-switch-frame alist))
-              (window--maybe-raise-frame (window-frame window)))))))))
+              (window--maybe-raise-frame (window-frame window)))
+            (when (and (cdr (assq 'redirect-frame-focus alist))
+                       (not (eq (window-frame window) curframe)))
+              (redirect-frame-focus
+               (window-frame window) curframe))))))))
 
 (defun display-buffer--special-action (buffer)
   "Return special display action for BUFFER, if any.
@@ -7955,6 +7971,7 @@ display-buffer-pop-up-frame
   (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
 	 (pop-up-frame-alist (append params pop-up-frame-alist))
 	 (fun pop-up-frame-function)
+         (selected-frame (selected-frame))
 	 frame window)
     (when (and fun
 	       ;; Make BUFFER current so `make-frame' will use it as the
@@ -7963,8 +7980,11 @@ display-buffer-pop-up-frame
 		 (setq frame (funcall fun)))
 	       (setq window (frame-selected-window frame)))
       (prog1 (window--display-buffer buffer window 'frame alist)
-	(unless (cdr (assq 'inhibit-switch-frame alist))
-	  (window--maybe-raise-frame frame))))))
+        (unless (cdr (assq 'inhibit-switch-frame alist))
+	  (window--maybe-raise-frame frame))
+        (when (and (cdr (assq 'redirect-frame-focus alist))
+                   (not (eq frame selected-frame)))
+          (redirect-frame-focus frame selected-frame))))))
 
 (defun display-buffer-pop-up-window (buffer alist)
   "Display BUFFER by popping up a new window.
@@ -7986,7 +8006,8 @@ display-buffer-pop-up-window
 indirectly called by the latter."
   (let ((frame (or (window--frame-usable-p (selected-frame))
 		   (window--frame-usable-p (last-nonminibuffer-frame))))
-	window)
+        (selected-frame (selected-frame))
+        window)
     (when (and (or (not (frame-parameter frame 'unsplittable))
 		   ;; If the selected frame cannot be split, look at
 		   ;; `last-nonminibuffer-frame'.
@@ -8002,7 +8023,10 @@ display-buffer-pop-up-window
 
       (prog1 (window--display-buffer buffer window 'window alist)
 	(unless (cdr (assq 'inhibit-switch-frame alist))
-	  (window--maybe-raise-frame (window-frame window)))))))
+	  (window--maybe-raise-frame (window-frame window)))
+        (when (and (cdr (assq 'redirect-frame-focus alist))
+                   (not (eq frame selected-frame)))
+          (redirect-frame-focus frame selected-frame))))))
 
 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
   "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
@@ -8086,7 +8110,9 @@ display-buffer-in-child-frame
 
     (prog1 (window--display-buffer buffer window type alist)
       (unless (cdr (assq 'inhibit-switch-frame alist))
-	(window--maybe-raise-frame frame)))))
+	(window--maybe-raise-frame frame))
+      (when (cdr (assq 'redirect-frame-focus alist))
+        (redirect-frame-focus frame parent)))))
 
 (defun windows-sharing-edge (&optional window edge within)
   "Return list of live windows sharing the same edge with WINDOW.
@@ -8456,7 +8482,8 @@ display-buffer-use-some-window
   (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
 	 (frame (or (window--frame-usable-p (selected-frame))
 		    (window--frame-usable-p (last-nonminibuffer-frame))))
-	 (window
+         (selected-frame (selected-frame))
+         (window
 	  ;; Reuse an existing window.
 	  (or (get-lru-window frame nil not-this-window)
 	      (let ((window (get-buffer-window buffer 'visible)))
@@ -8486,7 +8513,11 @@ display-buffer-use-some-window
 	  (window--display-buffer buffer window 'reuse alist)
 	(window--even-window-sizes window)
 	(unless (cdr (assq 'inhibit-switch-frame alist))
-	  (window--maybe-raise-frame (window-frame window)))))))
+	  (window--maybe-raise-frame (window-frame window)))
+        (when (and (cdr (assq 'redirect-frame-focus alist))
+                   (not (eq (window-frame window) selected-frame)))
+          (redirect-frame-focus
+           (window-frame window) selected-frame))))))
 
 (defun display-buffer-no-window (_buffer alist)
   "Display BUFFER in no window.