Emacs - Skip whitespace kills
Asked Answered
T

3

6

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)

Tambac answered 24/8, 2012 at 2:43 Comment(6)
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.Haftarah
@Haftarah 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.Forint
@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.Tambac
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.Hydracid
@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.Haftarah
@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.Forint
S
3

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.

Sharisharia answered 24/8, 2012 at 12:30 Comment(2)
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.Tambac
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.Sharisharia
T
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.

Thoria answered 23/2, 2022 at 16:22 Comment(0)
O
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)
Older answered 23/9, 2021 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.