9

Is it possible to prevent kill-ring from storing whitespaces/empty lines? Right now after I do a couple of changes and then go to browse-kill-ring I often see something like this:

-------



-------
   merchant_uuid: "some_uuid"
-------
   it "creates a webhook" do
-------

-------

-------

Is there a way of forcing kill-ring to only append meaningful content?

Ignacy Moryc
  • 185
  • 6
  • 1
    Not an answer, but I'm using `helm-show-kill-ring` and it seems to filter out blank kill ring entries. I can still yank a bunch of whitespace though, so +1 for the question. – glucas Feb 09 '15 at 15:50
  • The closest thing that is built in seems to be `kill-do-not-save-duplicates`, which prevent `kill-new` from adding the same thing to the kill ring twice in a row. – glucas Feb 09 '15 at 17:52

2 Answers2

3

Here's one approach for dealing with blank (i.e. only whitespace) kills. Rather than filtering them out altogether, this will allow at most one blank entry in the kill ring. Each new kill will check the head of the kill-ring and replace it if it is blank.

(defun my/replace-blank-kill (args)
  (let ((string (car args))
        (replace (cdr args))
        (last (car-safe kill-ring)))
    (when (and last (string-blank-p last))
      (setq replace t))
    (list string replace)))

(advice-add 'kill-new :filter-args #'my/replace-blank-kill)

Note that I'm using the Emacs 24.4+ advice mechanism here to filter the arguments that eventually get passed to kill-new.

The effect is that you can still kill and yank a bunch of whitespace, but if you kill some whitespace and then kill something else, the whitespace entry will be discarded.

Scott Weldon
  • 2,695
  • 1
  • 17
  • 31
glucas
  • 20,175
  • 1
  • 51
  • 83
  • You could choose to not add the to the kill ring insteda if you prefer. This approach works for `kill`, `kill-ring-save`, etc. If you have commands that add to the kill ring without calling `kill-new` then it won't work, of course. – glucas Feb 09 '15 at 17:22
  • This one solved it for me. I think I don't use any commands that add to kill ring without kill-new, or at least I didn't notice any weird behaviour. – Ignacy Moryc Feb 10 '15 at 08:31
0

One problem is that any command may modify the kill-ring so the only hook powerful enough to do this is the post-command-hook. This is inefficient, but AFAIK the only way to do this with rigor. If that sounds fine to you, you can do this:

(defun kill-ring-clean-whitespace-only ()
  (when (string-match "^[[:space:]]*$" (car kill-ring))
    (setq kill-ring (cdr kill-ring))))

(add-hook 'post-command-hook #'kill-ring-clean-whitespace-only)

For efficiency's sake, I assume that all commands append to the kill-ring, this is probably safe, but note that a function may insert text anywhere they wish into the kill-ring. This is rare, though, and I'm sure you don't want to iterate over the entire kill ring on every command.

PythonNut
  • 10,243
  • 2
  • 29
  • 75
  • A slightly more efficient way to do this might be to clean up the kill ring right before you yank or browse it, either through defadvice or your own commands. – Sacha Chua Feb 09 '15 at 16:37
  • I though about that, but then we'd need a list of all commands that access the kill-ring for yanking. If anyone can come up with a list, then the rest would be easy (of course). – PythonNut Feb 09 '15 at 16:44