Subject: bug#27442: Un-obsolete x-clipboard-yank, or provide analogous functional

From: Lars Ingebrigtsen <[email-masked]>
Date: Mon, 05 Jul 2021 16:13:43 +0200

Constantine Kharlamov <[email-masked]> writes:

> 1. start `emacs -Q`
> 2. Press M-: to execute `(setq select-enable-clipboard nil)`
> 3. Copy a text in the system, i.e. outside of Emacs.
> 4. Press M-: to execute `(clipboard-yank)` (you'll get at the point a
> content from system clipboard)
> 5. Press M-< M-d (so now you have the word ";; This" in kill-ring)
> 6. Press M-: to execute `(clipboard-yank)`
>
> 	You will see ";; This" got pasted at point, not the value in
> the system clipboard.
>
> 7. Press M-: to execute `(x-clipboard-yank)`
>
> 	You will see content from the system clipboard got pasted at point.

Aaah!  This explains the weird behaviour I've been seeing when I'm
trying to paste stuff from the clipboard -- as you say,
`M-x clipboard-yank' doesn't work reliably if you've yanked the
clipboard contents once.

And that's because of this:

(defun gui-selection-value ()
  (let ((clip-text
         (when select-enable-clipboard
           (let ((text (gui--selection-value-internal 'CLIPBOARD)))
             (if (string= text "") (setq text nil))

             ;; Check the CLIPBOARD selection for 'newness', is it different
             ;; from what we remembered them to be last time we did a
             ;; cut/paste operation.
             (prog1
                 (unless (equal text gui--last-selected-text-clipboard)
                   text)
               (setq gui--last-selected-text-clipboard text)))))

So, indeed, if you `M-x clipboard-yank', then kill some text in Emacs,
you can't `M-x clipboard-yank' until you've clipboarded some other text,
yanked it, and then clipboarded the original text again.

Fixing this should be pretty trivial -- the following patch should do
the trick, I think (although I haven't tested it).  But I'm not quite
sure about the intended logic here, so I've added Stefan M to the CCs --
perhaps he has some comments.

diff --git a/lisp/menu-bar.el b/lisp/menu-bar.el
index 739e751d8a..dac04e113e 100644
--- a/lisp/menu-bar.el
+++ b/lisp/menu-bar.el
@@ -570,7 +570,8 @@ menu-bar-edit-menu
 (defun clipboard-yank ()
   "Insert the clipboard contents, or the last stretch of killed text."
   (interactive "*")
-  (let ((select-enable-clipboard t))
+  (let ((select-enable-clipboard t)
+        (gui--last-selected-text-clipboard nil))
     (yank)))
 
 (defun clipboard-kill-ring-save (beg end &optional region)

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