Making a Function a Command

Marcin Borkowski (mbork) has an interesting post on Emacs keyboard macros in the wild. He had a seemingly conventional repetitive editing task that he wanted to accomplish via Emacs keyboard macros but a few problems came up. It’s a nice example of solving small problems as they occur.

What really surprised me, though, was that Borkowski discovered a way of turning a function into a command without altering its source code. If you’re vague on the difference between a function and a command, it’s essentially that you can call a command with Meta+x <command-name> while a function can be called only from other Elisp code. The way to turn a function into a command is to add an interactive special form to the beginning of the function.

That means it’s easy to make an Emacs function into a command since we have access to the source code. But who wants to go mucking around with Emacs source code, especially if it’s a temporary change? It turns out that there’s an easier way. Although this is documented at the “interactive” link above, it came as a surprise to me. Indeed, the documentation notes dryly that “This feature is seldom used.”

Take a look at Borkowski’s post to see how it’s done. It’s dead simple and a nice technique to have in your Emacs toolbox even if you seldom use it.

This entry was posted in General and tagged . Bookmark the permalink.