0

I run emacs with a common configuration on osx and linux. I have some custom functions that need to get access to the GUI clipboard. I find interacting with the clipboard a bit of pain - so I've resorted to things like

 (shell-command-to-string "pbpaste || xclip -o")

before.

Is there a correct way of doing this (i.e. a single function that intends to work across all GUIs which someone else maintains?).

This came up again in the context of working on a script that might in theory become a library that is shared with other people - so I would prefer not to use hacks in it (though I am currently putting a hack into it).

4
  • Your question is unclear. Why do you need any special code to access the clipboard? Killing and yanking interoperates with the system clipboard on all three platforms.
    – db48x
    Commented Aug 19, 2023 at 22:18
  • Meh. You don't understand is not the same as the question is unclear. I want to get the gui clipboard in an emacs lisp function (rather than from the paste command). (car (kill-ring)) does not contain the gui clipboard until you paste. I guess you should use a temporary buffer and paste into it.
    – Att Righ
    Commented Aug 20, 2023 at 13:22
  • Having it look through the source-code (while was kind of what I was avoiding doing...). It looks like yank calls current-kill and(current-kill 0) will return the gui clipboard and update kill-ring - so I guess current-kill is the function I want.
    – Att Righ
    Commented Aug 20, 2023 at 13:25
  • melpa.org/#/?q=clipboard ?
    – nega
    Commented Aug 21, 2023 at 14:04

1 Answer 1

1

If configured correctly emacs will access the gui clipboard when you run (yank) and update the kill-ring. This is triggered by (current-kill 0) which you can use to get at the clipboard.

(defun show-clipboard ()
    (message (current-kill 0)))

Though not that current-kill adds an entry to the kill-ring.

Having a look at the documentation for current-kill

Rotate the yanking point by N places, and then return that kill.
If N is zero and `interprogram-paste-function' is set to a
function that returns a string or a list of strings, and if that
function doesn't return nil, then that string (or list) is added
to the front of the kill ring and the string (or first string in
the list) is returned as the latest kill.

On my machine interprogram-paste-function contains gui-selection-value... however if you call gui-selection-value twice it returns nil the section time, so I think current-kill shoud be used instead).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.