From d845f06a8155a35fe93769e600b79b951f723863 Mon Sep 17 00:00:00 2001
Message-ID: <d845f06a8155a35fe93769e600b79b951f723863.1692630504.git.adrian@medranocalvo.com>
From: =?UTF-8?q?Adri=C3=A1n=20Medra=C3=B1o=20Calvo?=
 <adrian@medranocalvo.com>
Date: Tue, 15 Aug 2023 00:00:00 +0000
Subject: [PATCH] Expose last window under pointer to Lisp

* src/xdisp.c (syms_of_xdisp): Add defvar
`Vmouse_autoselect_window_last_window'.
* src/haikuterm.c (haiku_read_socket):
* src/msdos.c (dos_rawgetc):
* src/nsterm.m ([EmacsView mouseMoved:]):
* src/pgtkterm.c (motion_notify_event):
* src/w32inevt.c (do_mouse_event):
* src/w32term.c (w32_read_socket):
* src/xterm.c (handle_one_xevent): Use it replacing static
`last_mouse_window'.

* lisp/window.el (mouse-autoselect-window-window)
(mouse-autoselect-window-previous-window): Rename variable.
---
 lisp/window.el  |  8 +++++---
 src/haikuterm.c |  5 ++---
 src/msdos.c     |  6 ++----
 src/nsterm.m    |  5 ++---
 src/pgtkterm.c  |  5 ++---
 src/w32inevt.c  |  7 ++-----
 src/w32term.c   |  5 ++---
 src/xdisp.c     |  7 +++++++
 src/xterm.c     | 11 ++++-------
 9 files changed, 28 insertions(+), 31 deletions(-)

diff --git a/lisp/window.el b/lisp/window.el
index 16f16a75418..b2677ea99f3 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -10391,8 +10391,10 @@ mouse-autoselect-window-position-1
 (defvar mouse-autoselect-window-position nil
   "Last mouse position recorded by delayed window autoselection.")
 
-(defvar mouse-autoselect-window-window nil
+(defvar mouse-autoselect-window-previous-window nil
   "Last window recorded by delayed window autoselection.")
+(define-obsolete-variable-alias 'mouse-autoselect-window-window
+  'mouse-autoselect-window-previous-window "29.1")
 
 (defvar mouse-autoselect-window-state nil
   "When non-nil, special state of delayed window autoselection.
@@ -10425,7 +10427,7 @@ mouse-autoselect-window-start
 means suspend autoselection."
   ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
   (setq mouse-autoselect-window-position mouse-position)
-  (when window (setq mouse-autoselect-window-window window))
+  (when window (setq mouse-autoselect-window-previous-window window))
   (setq mouse-autoselect-window-state (when suspend 'suspend))
   ;; Install timer which runs `mouse-autoselect-window-select' after
   ;; `mouse-autoselect-window' seconds.
@@ -10473,7 +10475,7 @@ mouse-autoselect-window-select
                (and (>= mouse-autoselect-window 0)
                     ;; If `mouse-autoselect-window' is non-negative,
                     ;; select window if it's the same as before.
-                    (eq window mouse-autoselect-window-window))
+                    (eq window mouse-autoselect-window-previous-window))
                ;; Otherwise select window iff the mouse is at the same
                ;; position as before.  Observe that the first test
                ;; after starting autoselection usually fails since the
diff --git a/src/haikuterm.c b/src/haikuterm.c
index 8733b82fb2b..8341fd4cc53 100644
--- a/src/haikuterm.c
+++ b/src/haikuterm.c
@@ -3471,11 +3471,10 @@ haiku_read_socket (struct terminal *terminal, struct input_event *hold_quit)
 
 		if (!NILP (Vmouse_autoselect_window))
 		  {
-		    static Lisp_Object last_mouse_window;
 		    Lisp_Object window = window_from_coordinates (f, b->x, b->y, 0, 0, 0);
 
 		    if (WINDOWP (window)
-			&& !EQ (window, last_mouse_window)
+			&& !EQ (window, Vmouse_autoselect_window_last_window)
 			&& !EQ (window, selected_window)
 			&& !popup_activated_p
 			&& !MINI_WINDOW_P (XWINDOW (selected_window))
@@ -3486,7 +3485,7 @@ haiku_read_socket (struct terminal *terminal, struct input_event *hold_quit)
 			inev2.frame_or_window = window;
 		      }
 
-		    last_mouse_window = window;
+		    Vmouse_autoselect_window_last_window = window;
 		  }
 
 		if (f->auto_raise)
diff --git a/src/msdos.c b/src/msdos.c
index 75a39045cee..333fd9d59e6 100644
--- a/src/msdos.c
+++ b/src/msdos.c
@@ -2652,8 +2652,6 @@ dos_rawgetc (void)
 	  /* Generate SELECT_WINDOW_EVENTs when needed.  */
 	  if (!NILP (Vmouse_autoselect_window))
 	    {
-	      static Lisp_Object last_mouse_window;
-
 	      mouse_window = window_from_coordinates
 		(SELECTED_FRAME (), mouse_last_x, mouse_last_y, 0, 0, 0);
 	      /* A window will be selected only when it is not
@@ -2661,7 +2659,7 @@ dos_rawgetc (void)
 		 not in it.  A minibuffer window will be selected iff
 		 it is active.  */
 	      if (WINDOWP (mouse_window)
-		  && !EQ (mouse_window, last_mouse_window)
+		  && !EQ (mouse_window, Vmouse_autoselect_window_last_window)
 		  && !EQ (mouse_window, selected_window))
 		{
 		  event.kind = SELECT_WINDOW_EVENT;
@@ -2671,7 +2669,7 @@ dos_rawgetc (void)
 		  kbd_buffer_store_event (&event);
 		}
 	      /* Remember the last window where we saw the mouse.  */
-	      last_mouse_window = mouse_window;
+	      Vmouse_autoselect_window_last_window = mouse_window;
 	    }
 
 	  previous_help_echo_string = help_echo_string;
diff --git a/src/nsterm.m b/src/nsterm.m
index c809c0b824a..060898bb842 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -7530,12 +7530,11 @@ - (void)mouseMoved: (NSEvent *)e
   if (!NILP (Vmouse_autoselect_window))
     {
       NSTRACE_MSG ("mouse_autoselect_window");
-      static Lisp_Object last_mouse_window;
       Lisp_Object window
 	= window_from_coordinates (emacsframe, pt.x, pt.y, 0, 0, 0);
 
       if (WINDOWP (window)
-          && !EQ (window, last_mouse_window)
+          && !EQ (window, Vmouse_autoselect_window_last_window)
           && !EQ (window, selected_window)
 	  && !MINI_WINDOW_P (XWINDOW (selected_window))
           && (!NILP (focus_follows_mouse)
@@ -7548,7 +7547,7 @@ - (void)mouseMoved: (NSEvent *)e
           EV_TRAILER2 (e);
         }
       /* Remember the last window where we saw the mouse.  */
-      last_mouse_window = window;
+      Vmouse_autoselect_window_last_window = window;
     }
 
   dragging = (e.type == NSEventTypeLeftMouseDragged);
diff --git a/src/pgtkterm.c b/src/pgtkterm.c
index e767e15cc07..b343bd99fbe 100644
--- a/src/pgtkterm.c
+++ b/src/pgtkterm.c
@@ -5889,7 +5889,6 @@ motion_notify_event (GtkWidget *widget, GdkEvent *event,
 	     also when the target window is on another frame.  */
 	  && (f == XFRAME (selected_frame) || !NILP (focus_follows_mouse)))
 	{
-	  static Lisp_Object last_mouse_window;
 	  Lisp_Object window = window_from_coordinates
 	    (f, event->motion.x, event->motion.y, 0, false, false);
 
@@ -5902,7 +5901,7 @@ motion_notify_event (GtkWidget *widget, GdkEvent *event,
 	     autoselection the window is usually the window's text
 	     area including the margins.  */
 	  if (WINDOWP (window)
-	      && !EQ (window, last_mouse_window)
+	      && !EQ (window, Vmouse_autoselect_window_last_window)
 	      && !EQ (window, selected_window))
 	    {
 	      inev.ie.kind = SELECT_WINDOW_EVENT;
@@ -5910,7 +5909,7 @@ motion_notify_event (GtkWidget *widget, GdkEvent *event,
 	    }
 
 	  /* Remember the last window where we saw the mouse.  */
-	  last_mouse_window = window;
+	  Vmouse_autoselect_window_last_window = window;
 	}
 
       if (!note_mouse_movement (f, &event->motion))
diff --git a/src/w32inevt.c b/src/w32inevt.c
index 29717954cfd..c800291c2f8 100644
--- a/src/w32inevt.c
+++ b/src/w32inevt.c
@@ -467,7 +467,6 @@ do_mouse_event (MOUSE_EVENT_RECORD *event,
 		struct input_event *emacs_ev)
 {
   static DWORD button_state = 0;
-  static Lisp_Object last_mouse_window;
   DWORD but_change, mask, flags = event->dwEventFlags;
   int i;
 
@@ -502,7 +501,7 @@ do_mouse_event (MOUSE_EVENT_RECORD *event,
 		   not in it.  A minibuffer window will be selected iff
 		   it is active.  */
 		if (WINDOWP (mouse_window)
-		    && !EQ (mouse_window, last_mouse_window)
+		    && !EQ (mouse_window, Vmouse_autoselect_window_last_window)
 		    && !EQ (mouse_window, selected_window))
 		  {
 		    struct input_event event;
@@ -514,10 +513,8 @@ do_mouse_event (MOUSE_EVENT_RECORD *event,
 		    event.timestamp = movement_time;
 		    kbd_buffer_store_event (&event);
 		  }
-		last_mouse_window = mouse_window;
+		Vmouse_autoselect_window_last_window = mouse_window;
 	      }
-	    else
-	      last_mouse_window = Qnil;
 
 	    previous_help_echo_string = help_echo_string;
 	    help_echo_string = help_echo_object = help_echo_window = Qnil;
diff --git a/src/w32term.c b/src/w32term.c
index 2899e82b295..f9d86637024 100644
--- a/src/w32term.c
+++ b/src/w32term.c
@@ -5333,7 +5333,6 @@ w32_read_socket (struct terminal *terminal,
 		      || (!NILP (focus_follows_mouse)
 			  && !FRAME_NO_ACCEPT_FOCUS (f))))
 		{
-		  static Lisp_Object last_mouse_window;
 		  Lisp_Object window = window_from_coordinates
 		    (f, LOWORD (msg.msg.lParam), HIWORD (msg.msg.lParam), 0, 0, 0);
 
@@ -5342,7 +5341,7 @@ w32_read_socket (struct terminal *terminal,
 		     not in it.  Minibuffer window will be selected
 		     only when it is active.  */
 		  if (WINDOWP (window)
-		      && !EQ (window, last_mouse_window)
+		      && !EQ (window, Vmouse_autoselect_window_last_window)
 		      && !EQ (window, selected_window))
 		    {
 		      inev.kind = SELECT_WINDOW_EVENT;
@@ -5350,7 +5349,7 @@ w32_read_socket (struct terminal *terminal,
 		    }
 
 		  /* Remember the last window where we saw the mouse.  */
-		  last_mouse_window = window;
+		  Vmouse_autoselect_window_last_window = window;
 		}
 
 	      if (!w32_note_mouse_movement (f, &msg.msg))
diff --git a/src/xdisp.c b/src/xdisp.c
index 9cddcfeda27..dc0cbb658f6 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -36854,6 +36854,13 @@ syms_of_xdisp (void)
 of your window manager.  */);
   Vmouse_autoselect_window = Qnil;
 
+  DEFVAR_LISP ("mouse-autoselect-window-last-window",
+	       Vmouse_autoselect_window_last_window,
+    doc: /* Last window entered by the the mouse pointer.
+This variable is tracked only when both `mouse-autoselect-window' and
+`focus-follows-mouse' are non-nil. */);
+  Vmouse_autoselect_window_last_window = Qnil;
+
   DEFVAR_LISP ("auto-resize-tab-bars", Vauto_resize_tab_bars,
     doc: /* Non-nil means automatically resize tab-bars.
 This dynamically changes the tab-bar's height to the minimum height
diff --git a/src/xterm.c b/src/xterm.c
index 5840b15bcb7..36ba3103a70 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -20681,8 +20681,6 @@ handle_one_xevent (struct x_display_info *dpyinfo,
 		&& (f == XFRAME (selected_frame)
 		    || !NILP (focus_follows_mouse)))
 	      {
-		static Lisp_Object last_mouse_window;
-
 		if (xmotion.window != FRAME_X_WINDOW (f))
 		  {
 		    x_translate_coordinates (f, xmotion.x_root, xmotion.y_root,
@@ -20702,7 +20700,7 @@ handle_one_xevent (struct x_display_info *dpyinfo,
 		   autoselection the window is usually the window's text
 		   area including the margins.  */
 		if (WINDOWP (window)
-		    && !EQ (window, last_mouse_window)
+		    && !EQ (window, Vmouse_autoselect_window_last_window)
 		    && !EQ (window, selected_window))
 		  {
 		    inev.ie.kind = SELECT_WINDOW_EVENT;
@@ -20710,7 +20708,7 @@ handle_one_xevent (struct x_display_info *dpyinfo,
 		  }
 
 		/* Remember the last window where we saw the mouse.  */
-		last_mouse_window = window;
+		Vmouse_autoselect_window_last_window = window;
 	      }
 
             if (!x_note_mouse_movement (f, &xmotion, Qnil))
@@ -22593,7 +22591,6 @@ handle_one_xevent (struct x_display_info *dpyinfo,
 		      && (f == XFRAME (selected_frame)
 			  || !NILP (focus_follows_mouse)))
 		    {
-		      static Lisp_Object last_mouse_window;
 		      Lisp_Object window = window_from_coordinates (f, ev.x, ev.y, 0, false, false);
 
 		      /* A window will be autoselected only when it is not
@@ -22605,7 +22602,7 @@ handle_one_xevent (struct x_display_info *dpyinfo,
 			 autoselection the window is usually the window's text
 			 area including the margins.  */
 		      if (WINDOWP (window)
-			  && !EQ (window, last_mouse_window)
+			  && !EQ (window, Vmouse_autoselect_window_last_window)
 			  && !EQ (window, selected_window))
 			{
 			  inev.ie.kind = SELECT_WINDOW_EVENT;
@@ -22616,7 +22613,7 @@ handle_one_xevent (struct x_display_info *dpyinfo,
 			}
 
 		      /* Remember the last window where we saw the mouse.  */
-		      last_mouse_window = window;
+		      Vmouse_autoselect_window_last_window = window;
 		    }
 
 		  if (!x_note_mouse_movement (f, &ev, source ? source->name : Qnil))
-- 
2.41.0

