Subject: Re: bug#50256: thing-at-mouse

From: Juri Linkov <[email-masked]>
Date: Wed, 01 Sep 2021 10:17:56 +0300

> So maybe the check for current-buffer above should be added
> to mouse-set-point.

Now I found the real root of the problems.  All reported problems
can be solved by this short patch:

diff --git a/lisp/subr.el b/lisp/subr.el
index 0a31ef2b29..0b3b8d0e0f 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -1510,8 +1510,8 @@ event-start
 
 For more information, see Info node `(elisp)Click Events'."
   (if (consp event) (nth 1 event)
-    (or (posn-at-point)
-        (list (selected-window) (point) '(0 . 0) 0))))
+    (or (posn-at-point (window-point))
+        (list (selected-window) (window-point) '(0 . 0) 0))))
 
 (defun event-end (event)
   "Return the ending position of EVENT.
@@ -1519,8 +1519,8 @@ event-end
 
 See `event-start' for a description of the value returned."
   (if (consp event) (nth (if (consp (nth 2 event)) 2 1) event)
-    (or (posn-at-point)
-        (list (selected-window) (point) '(0 . 0) 0))))
+    (or (posn-at-point (window-point))
+        (list (selected-window) (window-point) '(0 . 0) 0))))

Both 'event-start' and 'event-end' created an event
with the window equal to the selected window,
but point from some random buffer,
not from selected window's buffer.

One question still remains: maybe this fix should be implemented
at a deeper level in posn-at-point when its arg POS is nil?

But actually, posn-at-point just uses this line:

  tem = Fpos_visible_in_window_p (pos, window, Qt);

So maybe this fix should be implemented in Fpos_visible_in_window_p,
i.e. at the end of this code

  if (EQ (pos, Qt))
    posint = -1;
  else if (!NILP (pos))
    posint = fix_position (pos);
  else if (w == XWINDOW (selected_window))
    posint = PT;
  else
    posint = marker_position (w->pointm);

it should get position from the selected window's buffer?