1

I have an Elisp part involving (current-kill 0) to copy the current clipboard content into a variable. This works flawlessly as long as the kill-ring has content. However, if I just started the computer and did not copy anything yet (or use the command-line version of Emacs), running that script runs into the error "Kill ring is empty".

Trying (cond (kill-ring) my_code) seems dodgy since when starting Emacs and before (current-kill 0) is executed, kill-ring is actually nil as per C-h v kill-ring.

Is there a reliable way to find out if the clipboard actually holds content without getting the code running onto an error?

2
  • Are you asking how to check whether the kill-ring is empty (just test whether kill-ring is non-nil), or are you asking how to avoid the error that it's empty? If the latter, just put something in it, to start with (using kill-new - e.g. (kill-new "DUMMY")).
    – Drew
    Dec 18 '20 at 23:39
1

Are you asking how to check whether the kill-ring is empty? If so, just test whether kill-ring is non-nil:

(when kill-ring ...)

Or are you asking how to avoid the error that it's empty? If so, just put something in it, to start with, using kill-new. For example:

(kill-new "DUMMY")

You can also use ignore-errors to just ignore that error or all errors:

(ignore-errors
  ;; Code that expects a non-empty `kill-ring`
  )
2
  • I am indeed asking on how to avoid the error happening (or, if even better check the content of (current-kill 0) without errors). Bluntly placing a new entry would likely replace (as in move) a potentially valid entry from position 0 (zero). I will check if (ignore-errors) does what I expect. Thanks! Will be back shortly.
    – Phoenix
    Dec 18 '20 at 23:48
  • The (ignore-errors) command is precisely what I needed. Thanks!
    – Phoenix
    Dec 19 '20 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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