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

From: Juri Linkov <[email-masked]>
Date: Tue, 31 Aug 2021 09:49:29 +0300

>> This looks like the right signature.  Then the implementation
>> and an example of usage could look like this:
>
> [...]
>
>> +(defun context-menu-dictionary (menu)
>> +  "Dictionary context menu."
>> +  (when (thing-at-mouse last-input-event 'word)
>> +    (define-key menu [dictionary-separator] menu-bar-separator)
>> +    (define-key menu [dictionary-search-word-at-mouse]
>> +      '(menu-item "Dictionary Search" dictionary-search-word-at-mouse
>> +                  :help "Search the word at mouse click in dictionary")))
>> +  menu)
>
> Ah, I see.  Yes, that makes perfect sense to me -- go ahead and push
> (perhaps with some documentation).

In bug#50067 you suggested to add a new arg with the click event
to all context menu functions.  This will make code more readable:

  (defun context-menu-dictionary (menu click)
    (when (thing-at-mouse click 'word)

so I will change the call to provide the arg from last-input-event,
and maybe later it will be possible to acquire the same event
more directly, not from last-input-event, and all callers will get it.

Now the problem found in bug#9923:

1. 'C-h m' temporarily switches from the original *info* buffer
   to the *Help* buffer;

2. thing-at-mouse (called from context-menu-dictionary)
   uses (save-excursion (mouse-set-point event))

3. mouse-set-point calls (posn-set-point (event-end event))

4. in posn-set-point:
4.1. (select-window (posn-window position))
     switches back to the original *info* buffer
     that was selected in the window;

4.2. (goto-char (posn-point position))
     goes to the position that event-end got from the *Help* buffer,
     so in the *info* buffer moves to the position from the *Help* buffer.

So maybe need to add the same condition that I already added to 2 places:

    (eq (window-buffer (posn-window (event-start event)))
                       (current-buffer))

but I have no idea at what level to add it: in mouse-set-point?
Or deeper in posn-set-point?  Both are quite low-level,
and I don't know if this might break something.