koldfront

Saving and restoring an Emacs macro #emacs

🕦︎ - 2025-04-17
Emacs logo

From time to time I add domains to a configuration file, header_checks, for my mail server, Postfix, to block them as spam.

To do this, I copy the email-address from a spam email, paste it into the configuration file, and modify it to escape the . and remove up until the @.

Doing that for a lot of spam is of course tedious, and so I usually create a macro in Emacs to do the pasting and massaging.

However, creating that macro is a bit tedious and that makes me not do it for the first couple of spams. Until I get too annoyed. So it keeps me from updating the checks. Not great.

Emacs is capable of generating the lisp code corresponding to a macro, so I thought "Hah, I will save the macro in a comment in the file, then I can evaluate it when I'm updating the file, and I don't have to record the macro every time!"

Alas, the command M-x insert-kdb-macro RET inserted some code that gave an error when evaluated - how odd?!

At first I assumed I was doing something wrong - the documentation only mentions saving a named macro and I was saving the last, unnamed one - but after looking at the lisp code of the function insert-kbd-macro (Emacs makes this easy), it was clear to me that it was supposed to work: it contains specific code to handle the last macro case.

I also tried an earlier version of Emacs (I usually run a development version), the one in Debian stable - version 28.2, and there the code generated was different, and, even more interestingly, worked!

So, I created a bug report for Emacs (#77317) and was swiftly told by one of the current Emacs maintainers that the documentation only says it works for named macros.

I argued that the function specifically has a branch for the last (and unnamed) keyboard macro, and that the functionality was broken in 2022, and Cc'ed the previous Emacs maintainer who made the breaking change (which was an improvement to the other branch of the function).

He asked for an explanation of my use case, and after I supplied that, he made a change to fix the problem, which I tested swiftly; it worked, and pushed it to the development version of Emacs a couple of days later.

Nice!

So, now I could easily generate the code to restore the macro and put it in a comment in the file.

Shortly after it hit me: Emacs can evaluate code from a file when loading it, I could have it restore the macro automatically on opening the file!

A quick 3 lines at the bottom of the file later:

# Local Variables:
# eval: (setq last-kbd-macro (kmacro--keys (kmacro "| C-y M-<left> <left> [ <right> ] C-r @ <right> M-<backspace> C-s ) <left>")))
# End:

and now, more than ever, Bob is indeed my uncle!

Add comment

How to comment, in excruciating detail…

To avoid spam many websites make you fill out a CAPTCHA, or log in via an account at a corporation such as Twitter, Facebook, Google or even Microsoft GitHub.

I have chosen to use a more old school method of spam prevention.

To post a comment here, you need to:

  • Configure a newsreader¹ to connect to the server koldfront.dk on port 1119 using nntps (nntp over TLS).
  • Open the newsgroup called lantern.koldfront and post a follow up to the article.
¹ Such as Thunderbird, Pan, slrn, tin or Gnus (part of Emacs).

Or, you can fill in this form:

+=