Prompting With The Interactive Command

Most Emacs users who are at all familiar with Elisp know that you have to specify the interactive command for functions if you want them to be interactive—that is, if you want them to be callable with Meta+x. Most of those people also know that you can use the interactive command to prompt a user for function parameters.

What is less well known is that you can also pass a list of parameters for the function in the interactive command. By itself, that’s not too useful, of course, but rather than specifying a list, you can specify a function that returns a list. The function can do anything as long as it returns a list. Arialdo Martini has a post that illustrates this nicely.

He looks at the problem of surrounding a region with a pair of delimiters. The question is how to get the two delimiters. The most obvious—but also most difficult—solution is to simply add code to the body of the function to prompt for the delimiters. It’s far easier to use the interactive command to get them and this also has the advantage of allowing the function to be called from Elisp without any prompting.

But what happens if you want to limit the choices to a predetermined set? That’s exactly what completing-read is for. Sadly, there’s no interactive parameter for this but you can write a function that uses completing-read and returns the delimiters as a list.

Martini’s post uses this idea to implement a set of functions that surrounds a region with any of a predetermined set of delimiters. As Martini says, there’s already plenty of functions and packages that do this. The point of his post is to explore how you can use the interactive command to pass parameters to a function. It’s a good post and worth a few minutes of your time to read.

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