How to define an interactive function with multiple prompts
Published on Apr 27, 2023, by Junji Zhi
Today I tried to define a function that prompts the user for multiple arguments in a single function call.
I knew I need to use the interactive
form. But when I read the interactive
form documentation,
it is a bit hard to read without code examples.
So I tried to tinker with it by trying this function:
(defun test-fn (arg1 arg2)
(interactive "sEnter arg1: \nsEnter arg2: ")
(message "arg1: %s, arg2: %s" arg1 arg2))
And it works as expected. The trick is to separate the prompts with a newline \n
.
This way, it defines a function an interactively-callable command that accepts multiple prompts in one function.
If you are curious about what "s" code does, take a look at this page.