reddit / emacs

Keep your kill ring clean with clean-kill-ring.el

nicholas_hubbard · 2022-02-24 02:15
I created the package [clean-kill-ring.el](https://github.com/NicholasBHubbard/clean-kill-ring.el) which solves a problem that I have long been annoyed with which is controlling what is allowed in the kill-ring. This package provides a `clean-kill-ring-mode` minor mode, that when active prevents any killed text that satisfies a predicate in `clean-kill-ring-filters` from entering the kill ring. By default the only active filter is `string-blank-p` to keep whitespace out of the kill ring. You can automatically remove duplicate items if you set `clean-kill-ring-prevent-duplicates` to a non-nil value. Hopefully some people find this useful.
00-11 · 2022-02-24 03:00
Sounds reasonable and useful. (Makes me wonder that I've never been bothered by junk in my `kill-ring`....)
RichieHHam · 2022-02-24 05:24
Great initiative.
Danrobi1 · 2022-02-24 06:58
Nice. Thanks for sharing.
lispstudent · 2022-02-24 07:21
This will help a lot with org-export. Thank you.
[deleted] · 2022-02-24 10:57
In 28.1 this can be replicated using `kill-transform-function` (return the argument if ok, nil if you want to filter it out). Filtering out duplicates has been possible using `kill-do-not-save-duplicates` for a while now. Also a comment on the code: (advice-add 'kill-new :after #'(lambda (&rest args) (progn (ignore args) (clean-kill-ring-clean-most-recent-entry)))) If you want to ignore an argument, just prefix the symbol with a _. Lambdas have implicit progns, so you don't need to wrap it twice. And it also isn't necessary for sharp-quote (or function-quote) a lambda, as they will evaluate to a funcallable object this or that way.
nicholas_hubbard · 2022-02-24 13:40
>Also a comment on the code: Thanks! I applied your advice about prefixing `args` with an underscore and removing the progn, ignore, and sharp quote. This part of the code is much cleaner now. >filtering out duplicates has been possible using `kill-do-not-save-duplicates` for a while now. As per the `kill-do-not-save-duplicates` docstring: >If non-nil, don't add a string to kill-ring if it duplicates the last one. This means that the string will only not be entered into the kill-ring if it the same as the most recent killed item. In contrast with `clean-kill-ring-prevent-duplicates` on if a string is entered into the kill-ring that is the same as any of the existing kill-ring members than the old members will be removed. This can be shown in the following REPL session: ;;; The behavior of kill-ring-do-not-save-duplicates ... ELISP> clean-kill-ring-mode nil ELISP> kill-do-not-save-duplicates t ELISP> (setq kill-ring nil) nil ELISP> (kill-new "foo") "foo" ELISP> (length kill-ring) 1 (#o1, #x1, ?\C-a) ELISP> (kill-new "bar") "bar" ELISP> (length kill-ring) 2 (#o2, #x2, ?\C-b) ELISP> (kill-new "foo") "foo" ELISP> (length kill-ring) 3 (#o3, #x3, ?\C-c) ELISP> (kill-new "foo") "foo" ELISP> (length kill-ring) 3 (#o3, #x3, ?\C-c) ;;; The behavior of clean-kill-ring-prevent-duplicates ... ELISP> clean-kill-ring-mode t ELISP> kill-do-not-save-duplicates nil ELISP> (setq kill-ring nil) nil ELISP> (kill-new "foo") "foo" ELISP> (length kill-ring) 1 (#o1, #x1, ?\C-a) ELISP> (kill-new "bar") "bar" ELISP> (length kill-ring) 2 (#o2, #x2, ?\C-b) ELISP> (kill-new "foo") "foo" ELISP> (length kill-ring) 2 (#o2, #x2, ?\C-b)
T_Verron · 2022-02-24 13:49
What does it mean to "ignore" an argument in this context?
[deleted] · 2022-02-24 17:41
:after advice is a function that is passed all the arguments of the function it advises (here `args`). In this case they aren't used, so the function "ignores" them.
[deleted] · 2022-02-24 17:45
> In contrast with clean-kill-ring-prevent-duplicates on if a string is entered into the kill-ring that is the same as any of the existing kill-ring members than the old members will be removed. Oh, Ok I didn't notice that. My bad.
T_Verron · 2022-02-24 18:03
But what's the difference with simply not doing anything with them?
[deleted] · 2022-02-24 20:23
That the byte compiler will complain if a variable is not used.
T_Verron · 2022-02-25 17:29
Aaah, thanks!