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
  • emacs.stackexchange.com/tags/elisp/info Commented Dec 18, 2020 at 23:36
  • 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")). Commented Dec 18, 2020 at 23:39

1 Answer 1

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. Commented Dec 18, 2020 at 23:48
  • The (ignore-errors) command is precisely what I needed. Thanks! Commented Dec 19, 2020 at 0:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.