7

I'm trying to make the kill ring essentially ignore whitespace only entries (tabs, newlines, just spaces, etC), I'm fairly new to elisp and I'm pretty sure the way to do is by doing defadvice but I have a few questions.

  • Would it better to stop whitespace entries from ever getting into the kill ring in the first place, or to skip them on yank? I assumed the latter.

In which case, I'm completely lost on which function I should advise, its between current-kill, yank, and insert-for-yank - but am not entirely sure which I should manipulate to not yank whitespace from the kill ring.

Thanks!

EDIT: I'm pretty sure the way to do this is to manipulate `current-kill' to keep calling itself until it reaches a non whitespace entry? (or the end of the ring, whichever comes first)

Drew
  • 29,895
  • 7
  • 74
  • 104
user652650
  • 648
  • 6
  • 18
  • 1
    Can you give an example of a pattern of use that results in a lot of useless whitespace in the kill ring? I feel perplexed that this could ever be an issue for someone. – Sean Aug 24 '12 at 06:59
  • 2
    @Sean Take the case where you kill a region of code, which you'll yank a bit later. On the way to traversing where you're going to yank the code in, you a spot two blank lines next to each other :O. You kill one of them, and now, damn, there is an empty line on the kill ring. I experience this now and again, it's quite annoying. – Squidly Aug 24 '12 at 10:03
  • @Sean, MrBones has the use case pretty perfect, or often times you have a ton of whitespace after a lines content, and you'll kill to clean it up. – user652650 Aug 24 '12 at 11:43
  • 1
    Instead of advice, maybe just rebind the yank keys to functions of your own. This sounds to me like it would be a lot simpler and more robust. – tripleee Aug 24 '12 at 12:30
  • 1
    @MrBones It's the exact same situation as when you kill any other text on the way to yanking a previous kill. You just use `M-y` when you get there to back up to the kill you want. I just don't see why whitespace-only intermediate kills should be particularly objectionable. – Sean Aug 24 '12 at 17:42
  • @sean, It's simply that I never really want the whitespace, so either I keep on popping the ring every time, or I set kill to delete whitespace. – Squidly Aug 29 '12 at 09:47

3 Answers3

4

From the comments it seems that you are having problems with whitespaces in your kill-ring since you kill whitespace lines. My solution would be to avoid killing whitespace lines and to use the function delete-blank-line (C-x C-o) instead. This reduces a group a blank lines (including whitespaces and tabs) to a single blank line.

Nicolas Dudebout
  • 9,172
  • 2
  • 34
  • 43
  • What about a situation with a line ending in x chars of whitespace, that you kill? delete-blank-line doesn't work in that scenario. I think redefining yank may be the way, which would solve both scenarios, and remove the need for delete-blank-line. – user652650 Aug 24 '12 at 17:30
  • 1
    Use `whitespace-cleanup` in that case with `(add-hook 'before-save-hook 'whitespace-cleanup)`. It is usually easier to find the emacs way of doing things rather than modifying emacs. – Nicolas Dudebout Aug 24 '12 at 18:17
2

I wrote a package clean-kill-ring.el that provides functionality for controlling what is allowed in the kill ring.

By default enabling clean-kill-ring-mode prevents blank strings from entering the kill ring, but further customization is possible as well.

Nicholas Hubbard
  • 527
  • 2
  • 15
0

The following advice does not let whitespace be added to the kill ring in the first place:

(defun night/h-kill-skip-whitespace (orig-fn string &optional rest)
  (let* (
         (string-raw (substring-no-properties string))
         (space-p (not (string-match-p "[^ \t\n\r]" string-raw))))

    (cond
     ((not space-p)
      (apply orig-fn string rest))
     (t
      (message "skipped whitespace kill")
     ))))

(advice-add 'kill-new :around #'night/h-kill-skip-whitespace)
HappyFace
  • 3,439
  • 2
  • 24
  • 43