Subject: Re: bug#32747: Be able to append mouse selections at the head of `kill-ring' (e.g, with C-M-w)

From: Lars Ingebrigtsen <[email-masked]>
Date: Sun, 22 May 2022 18:07:53 +0200

Lars Ingebrigtsen <[email-masked]> writes:

> And this seems to be because `C-M-w' works by setting `last-command' to
> `kill-region' to signal that the next `copy-region-as-kill' should
> append.  However, when dragging a region, `last-command' ends up being
> nil by the time we reach `copy-region-as-kill' -- presumably because we
> clear it somewhere in the mouse handling?
>
> Anybody have any ideas how to fix this, if we want to fix this?

The following patch fixes this, but it's not clear that we want to do
this.

`C-w C-d' (kill-region + kill-word) will make the second kill append
to the first, while similar commands like `M-w C-d' don't.  So the
mouse.el code has:

	 ;; Ignore
	 ;; `last-command' so we don't append to a preceding kill.

Because we don't want `C-w <drag-mouse-1>' to append to that `C-w',
apparently.

But -- if you do something like `C-d M-w', then that `M-w' is morally
equivalent to the <drag-mouse-1> (i.e., we're putting more stuff onto
the kill ring without killing text).

So why is `C-w <drag-mouse-1>' different here?  The patch makes them
behave the same way.

If we don't want that, we have to come up with a whole nother way to
implement `C-M-w', because it just sets last-command to `kill-region'.

Opinions?

diff --git a/lisp/mouse.el b/lisp/mouse.el
index 4b5f6ed223..5913992e44 100644
--- a/lisp/mouse.el
+++ b/lisp/mouse.el
@@ -1396,7 +1396,8 @@ mouse-set-region
 	 ;; Don't set this-command to `kill-region', so a following
 	 ;; C-w won't double the text in the kill ring.  Ignore
 	 ;; `last-command' so we don't append to a preceding kill.
-	 (let (this-command last-command deactivate-mark)
+	 (let ((last-command last-command)
+               this-command deactivate-mark)
 	   (copy-region-as-kill beg end)))
     (if (numberp beg) (goto-char beg))
     ;; On a text terminal, bounce the cursor.
@@ -1499,6 +1500,7 @@ mouse-drag-region
       (mouse-drag-and-drop-region start-event)
     ;; Give temporary modes such as isearch a chance to turn off.
     (run-hooks 'mouse-leave-buffer-hook)
+    (ignore-preserving-kill-region)
     (mouse-drag-track start-event)))
 
 ;; Inhibit the region-confinement when undoing mouse-drag-region
@@ -1708,7 +1710,8 @@ mouse-drag-track
                                             nil start-point))
                         ((>= mouse-row bottom)
                          (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
-                                            nil start-point))))))))
+                                            nil start-point))))))
+                 (ignore-preserving-kill-region)))
              map)
            t (lambda ()
                (funcall cleanup)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no