This can be done by advising the function that sets the clipboard.
;; Clipboard history.
(defvar my-clipboard-history-limit 10)
(defvar my-clipboard-history--list nil)
(defun my-clipboard-history-paste (&optional index)
(insert (nth (or index 0) my-clipboard-history--list)))
(defun my-clipboard-history-paste-penultimate ()
(interactive)
(my-clipboard-history-paste))
(defun my-clipboard-history--gui-set-selection (orig-fn type data)
(when (eq type 'CLIPBOARD)
(let ((old-data (gui-get-selection type)))
(when old-data
(let ((trim-number (- (length my-clipboard-history--list)
my-clipboard-history-limit)))
(when (> 0 trim-number)
(nbutlast my-clipboard-history--list trim-number)))
(push old-data my-clipboard-history--list))))
(funcall orig-fn type data))
;;;###autoload
(define-minor-mode my-clipboard-history-mode
"Highlight block under the cursor."
:global t
:lighter
""
(cond
(my-clipboard-history-mode
(advice-add
'gui-set-selection :around
#'my-clipboard-history--gui-set-selection))
(t
(advice-remove
'gui-set-selection
#'my-clipboard-history--gui-set-selection))))
(provide 'my-clipboard-history-mode)
You can bind this to Alt-P
in evil mode, for example:
(define-key evil-normal-state-map (kbd "M-p")
'my-clipboard-history-paste-penultimate)
(define-key evil-insert-state-map (kbd "M-p")
'my-clipboard-history-paste-penultimate)
xclipboard
program? – NickD Aug 10 '20 at 16:55