From f3057ad11d77f2008f14ef82699997fb1cafeb97 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Thu, 17 Apr 2025 21:21:29 +0300 Subject: [PATCH] Don't switch to another frame if window is not on the selected frame. * lisp/window.el (window--quit-restore-select-window): Add optional arg 'frame'. Don't switch to another frame if window is not on the selected frame (bug#71386). (quit-restore-window): Provide the 'frame' arg to 'window--quit-restore-select-window' calls. Patch by martin rudalics . * test/lisp/tab-bar-tests.el (tab-bar-tests-quit-restore-window): No need to reselect the frame after 'quit-window'. (cherry picked from commit 9f4347e00c02c3aa436118425a0cda2bc7f69d51) --- lisp/window.el | 17 ++++++++++++----- test/lisp/tab-bar-tests.el | 5 +---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lisp/window.el b/lisp/window.el index 0e89b490aec..052547de7df 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -5278,13 +5278,19 @@ the window has never shown before." :version "31.1" :group 'windows) -(defun window--quit-restore-select-window (window) +(defun window--quit-restore-select-window (window &optional frame) "Select WINDOW after having quit another one. Do not select an inactive minibuffer window." (when (and (window-live-p window) (or (not (window-minibuffer-p window)) (minibuffer-window-active-p window))) - (select-window window))) + ;; If WINDOW is not on the selected frame, don't switch to + ;; another frame. + (unless (and (eq frame (selected-frame)) + (not (eq frame (window-frame window)))) + (setq frame (window-frame window)) + (set-frame-selected-window frame window) + (select-frame-set-input-focus frame)))) (defun quit-restore-window (&optional window bury-or-kill) "Quit WINDOW and deal with its buffer. @@ -5343,6 +5349,7 @@ elsewhere. This value is used by `quit-windows-on'." (unless (eq (car buf) buffer) (throw 'prev-buffer (car buf)))))) (dedicated (window-dedicated-p window)) + (frame (window-frame window)) quad entry reset-prev) (cond ;; First try to delete dedicated windows that are not side windows. @@ -5350,7 +5357,7 @@ elsewhere. This value is used by `quit-windows-on'." (window--delete window 'dedicated (memq bury-or-kill '(kill killing)))) ;; If the previously selected window is still alive, select it. - (window--quit-restore-select-window quit-restore-2)) + (window--quit-restore-select-window quit-restore-2 frame)) ((and (not prev-buffer) (or (memq (nth 1 quit-restore) '(frame tab)) (and (eq (nth 1 quit-restore) 'window) @@ -5362,7 +5369,7 @@ elsewhere. This value is used by `quit-windows-on'." ;; Delete WINDOW if possible. (window--delete window nil (eq bury-or-kill 'kill))) ;; If the previously selected window is still alive, select it. - (window--quit-restore-select-window quit-restore-2)) + (window--quit-restore-select-window quit-restore-2 frame)) ((and (or (and quit-restore-window-no-switch (not prev-buffer)) ;; Ignore first of the previous buffers if ;; 'quit-restore-window-no-switch' says so. @@ -5372,7 +5379,7 @@ elsewhere. This value is used by `quit-windows-on'." (window--delete window nil (memq bury-or-kill '(kill killing)))) ;; If the previously selected window is still alive, select it. - (window--quit-restore-select-window quit-restore-2)) + (window--quit-restore-select-window quit-restore-2 frame)) ((or (and (listp (setq quad (nth 1 quit-restore-prev))) (buffer-live-p (car quad)) (eq (nth 3 quit-restore-prev) buffer) diff --git a/test/lisp/tab-bar-tests.el b/test/lisp/tab-bar-tests.el index 3f9ee3aa4b7..33ec580b450 100644 --- a/test/lisp/tab-bar-tests.el +++ b/test/lisp/tab-bar-tests.el @@ -89,9 +89,6 @@ (should (eq (length (window-list)) 2)) (should (equal (buffer-name) "*info*")) (quit-window) - ;; 'quit-window' unexpectedly selects the original frame, - ;; so move back to the created frame - (select-frame (car (frame-list))) (should (eq (length (window-list)) 1)) (should (eq (length (frame-list)) 2)) (should (equal (buffer-name) "*Messages*")) @@ -99,7 +96,7 @@ (should (eq (length (frame-list)) 2)) ;; Delete the created frame afterwards because with tty frames ;; the output of 'message' is bound to the original frame - (delete-frame)) + (delete-frame (car (frame-list)))) ;; 2.1. 'quit-restore-window' should close the tab ;; from initial window (bug#59862) -- 2.39.5