1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Fix `clipboard-yank' inserting off the kill ring instead of CLIPBOARD

* lisp/select.el (gui-last-cut-in-clipboard)
(gui-last-cut-in-primary): New variables.
(gui-select-text): Set those variables.
(gui--selection-value-internal, gui-selection-value): Don't
return nil if the last cut did not own the chosen
selection.  (bug#56325)
This commit is contained in:
Po Lu 2022-07-01 10:23:13 +08:00
parent 833767e53f
commit 2289fafeaf

View file

@ -112,20 +112,28 @@ E.g. it doesn't exist under MS-Windows."
;; We keep track of the last selection here, so we can check the ;; We keep track of the last selection here, so we can check the
;; current selection against it, and avoid passing back with ;; current selection against it, and avoid passing back with
;; gui-selection-value the same text we previously killed or ;; gui-selection-value the same text we previously killed or
;; yanked. We track both ;; yanked. We track both separately in case another X application only
;; separately in case another X application only sets one of them ;; sets one of them we aren't fooled by the PRIMARY or CLIPBOARD
;; we aren't fooled by the PRIMARY or CLIPBOARD selection staying the same. ;; selection staying the same.
(defvar gui--last-selected-text-clipboard nil (defvar gui--last-selected-text-clipboard nil
"The value of the CLIPBOARD selection last seen.") "The value of the CLIPBOARD selection last seen.")
(defvar gui--last-selected-text-primary nil (defvar gui--last-selected-text-primary nil
"The value of the PRIMARY selection last seen.") "The value of the PRIMARY selection last seen.")
(defvar gui--last-selection-timestamp-clipboard nil (defvar gui--last-selection-timestamp-clipboard nil
"The timestamp of the CLIPBOARD selection last seen.") "The timestamp of the CLIPBOARD selection last seen.")
(defvar gui--last-selection-timestamp-primary nil (defvar gui--last-selection-timestamp-primary nil
"The timestamp of the PRIMARY selection last seen.") "The timestamp of the PRIMARY selection last seen.")
(defvar gui-last-cut-in-clipboard nil
"Whether or not the last call to `interprogram-cut-function' owned CLIPBOARD.")
(defvar gui-last-cut-in-primary nil
"Whether or not the last call to `interprogram-cut-function' owned PRIMARY.")
(defun gui--set-last-clipboard-selection (text) (defun gui--set-last-clipboard-selection (text)
"Save last clipboard selection. "Save last clipboard selection.
Save the selected text, passed as argument, and for window Save the selected text, passed as argument, and for window
@ -182,7 +190,10 @@ MS-Windows does not have a \"primary\" selection."
;; should not be reset by cut (Bug#16382). ;; should not be reset by cut (Bug#16382).
(setq saved-region-selection text) (setq saved-region-selection text)
(gui-set-selection 'CLIPBOARD text) (gui-set-selection 'CLIPBOARD text)
(gui--set-last-clipboard-selection text))) (gui--set-last-clipboard-selection text))
;; Record which selections we now have ownership over.
(setq gui-last-cut-in-clipboard select-enable-clipboard
gui-last-cut-in-primary select-enable-primary))
(define-obsolete-function-alias 'x-select-text 'gui-select-text "25.1") (define-obsolete-function-alias 'x-select-text 'gui-select-text "25.1")
(defcustom x-select-request-type nil (defcustom x-select-request-type nil
@ -218,13 +229,13 @@ decided by `x-select-request-type'. The return value is already
decoded. If `gui-get-selection' signals an error, return nil." decoded. If `gui-get-selection' signals an error, return nil."
;; The doc string of `interprogram-paste-function' says to return ;; The doc string of `interprogram-paste-function' says to return
;; nil if no other program has provided text to paste. ;; nil if no other program has provided text to paste.
(unless (and (unless (and gui-last-cut-in-clipboard
;; `gui-backend-selection-owner-p' might be unreliable on ;; `gui-backend-selection-owner-p' might be unreliable on
;; some other window systems. ;; some other window systems.
(memq window-system '(x haiku)) (memq window-system '(x haiku))
(eq type 'CLIPBOARD) (eq type 'CLIPBOARD)
;; Should we unify this with gui--clipboard-selection-unchanged-p? ;; Should we unify this with gui--clipboard-selection-unchanged-p?
(gui-backend-selection-owner-p type)) (gui-backend-selection-owner-p type))
(let ((request-type (if (memq window-system '(x pgtk haiku)) (let ((request-type (if (memq window-system '(x pgtk haiku))
(or x-select-request-type (or x-select-request-type
'(UTF8_STRING COMPOUND_TEXT STRING text/plain\;charset=utf-8)) '(UTF8_STRING COMPOUND_TEXT STRING text/plain\;charset=utf-8))
@ -254,7 +265,15 @@ decoded. If `gui-get-selection' signals an error, return nil."
;; (bug#53894) for further discussion about this DWIM ;; (bug#53894) for further discussion about this DWIM
;; action, and possible ways to make this check less ;; action, and possible ways to make this check less
;; fragile, if so desired. ;; fragile, if so desired.
(unless (gui--clipboard-selection-unchanged-p text)
;; Don't check the "newness" of CLIPBOARD if the last
;; call to `gui-select-text' didn't cause us to become
;; its owner. This lets the user yank text killed by
;; `clipboard-kill-region' with `clipboard-yank' without
;; interference from text killed by other means when
;; `select-enable-clipboard' is nil.
(unless (and gui-last-cut-in-clipboard
(gui--clipboard-selection-unchanged-p text))
(gui--set-last-clipboard-selection text) (gui--set-last-clipboard-selection text)
text)))) text))))
(primary-text (primary-text
@ -264,7 +283,8 @@ decoded. If `gui-get-selection' signals an error, return nil."
;; Check the PRIMARY selection for 'newness', is it different ;; Check the PRIMARY selection for 'newness', is it different
;; from what we remembered them to be last time we did a ;; from what we remembered them to be last time we did a
;; cut/paste operation. ;; cut/paste operation.
(unless (gui--primary-selection-unchanged-p text) (unless (and gui-last-cut-in-primary
(gui--primary-selection-unchanged-p text))
(gui--set-last-primary-selection text) (gui--set-last-primary-selection text)
text))))) text)))))