master d56e98592ef: Clicking mode line menus in inactive windows now switches the window

Juri Linkov <[email protected]> Tue, 30 Jun 2026 11:48:25 -0400 (EDT)
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit d56e98592ef949f31687ff8adf16a12411efb1e5
Author: Juri Linkov <[email protected]>
Commit: Juri Linkov <[email protected]>

    Clicking mode line menus in inactive windows now switches the window
    
    * lisp/bindings.el (mode-line-window-select): New function (bug#81178).
    (x-pre-popup-menu-hook): Add it to 'x-pre-popup-menu-hook'.
---
 etc/NEWS         |  9 +++++++++
 lisp/bindings.el | 12 ++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 1537464d653..5db066a7466 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -80,6 +80,15 @@ left margin is present, Emacs will now display the overlay arrow into
 this margin.  Edebug is now using this feature by explicitly setting up
 a left margin for it.
 
+** Mode Line
+
+*** Popup menus invoked from mode line select another window.
+Clicking mode line elements in inactive windows now first
+selects another window before displaying the menu, so the
+commands invoked from the menu will operate on the right buffer.
+You can restore the previous behavior by removing
+'mode-line-window-select' from 'x-pre-popup-menu-hook'.
+
 
 * Editing Changes in Emacs 32.1
 
diff --git a/lisp/bindings.el b/lisp/bindings.el
index b13dc0a705c..198ad12c5c6 100644
--- a/lisp/bindings.el
+++ b/lisp/bindings.el
@@ -889,6 +889,18 @@ or not."
 	     (with-selected-window (minibuffer-window)
 	       (eq window (minibuffer-selected-window)))))))
 
+(defun mode-line-window-select (&optional _ignored-arg)
+  "Select the window on whose mode-line the mouse was clicked."
+  (when-let* ((_ (mouse-event-p last-input-event))
+	      (event (event-start last-input-event))
+	      (_ (eq (posn-area event) 'mode-line))
+	      (window (posn-window event))
+	      (_ (not (eq (selected-window) window))))
+    (mouse-select-window last-input-event)))
+
+(add-hook 'x-pre-popup-menu-hook #'mode-line-window-select)
+
+
 (defmacro bound-and-true-p (var)
   "Return the value of symbol VAR if it is bound, else nil.
 Note that if `lexical-binding' is in effect, this function isn't