Subject: Re: bug#56530: 29.0.50; mouse-2 cut selected text when cua-mode is enabled

From: Juri Linkov <[email-masked]>
Date: Sun, 17 Jul 2022 21:49:41 +0300

>> but only like Visuwesh pointed out
>> when mouse-yank-at-point is set to t.  This is because
>> mouse-yank-primary has such line:
>>
>>   (or mouse-yank-at-point (mouse-set-point event))
>>
>> When delete-selection deletes the region where the mouse is clicked,
>> mouse-set-point loses track and sets point to a random position,
>> because the event contains fixed positions, not markers.
>> I wonder why?
>
> Sounds like a bug.

All affected mouse commands mouse-yank-from-menu, mouse-yank-at-click,
mouse-yank-primary, mouse-yank-secondary have the same line:

  (or mouse-yank-at-point (mouse-set-point event))

So when the region is deleted by delete-selection-pre-hook
`mouse-set-point' tries to set point using outdated information
of the event's position in the deleted region.

Maybe delete-selection-pre-hook could directly modify the event?
Everything works fine with this:

            ((eq type 'yank-on-region)
             (let ((pos (posn-point (event-end last-nonmenu-event))))
               (when (and (>= pos (region-beginning))
                          (<= pos (region-end)))
                 (delete-selection-helper 'yank)
                 (setf (nth 5 (nth 1 last-nonmenu-event)) (region-beginning)))))

But is this a good idea?