2023-02-05 From the kill ring to a register revisited

Two weeks ago I wrote about my little command to move the topmost (or some other) kill ring entry to a register. It turned out that this particular post – which I thought was rather niche – spurred more interest than apparently more useful posts I have. Go figure.

One commenter pointed out to me the truncate-string-to-width function, which does what my shorten-string does and a lot more. Go check its docstring to learn the differences. Note that my shorten-string has 8 lines of code and truncate-string-to-width has 51, so there’s that.

Another reader sent me an email where he told me that there is also s-truncate, in the s library by Magnar Sveen. That function is almost identical to my one but slightly shorter – on the other hand, it doesn’t support the case when the desired length is smaller than the length of suffix (although what my function does then is probably also not optimal).

The most interesting thing, however, in his email was a third idea to make the code of copy-kill-to-register more DRY. In Common Lisp there are so called “auxiliary variables” – variables local to a function, present in the argument list, but not specified when calling the function. It turns out that Emacs’ Common Lisp emulation library, cl, supports them, too. As the manual says, using them is equivalent to wrapping the function’s body in a let form. Unfortunately, it seems that auxiliary variables are bound after the interactive clause is processed, so that idea didn’t work. Another idea I had – to wrap the interactive clause inside the let* form – didn’t work, either, since interactive must be on the top level of the function to work (and in fact, it apparently must be the first thing in the function after the optional docstring).

Anyway, I didn’t know about &aux variables, so at least I learned something new.

CategoryEnglish, CategoryBlog, CategoryEmacs