Node:Interactive Options, Next:, Previous:Interactive, Up:Writing Defuns



3.4 Different Options for interactive

In the example, multiply-by-seven used "p" as the argument to interactive. This argument told Emacs to interpret your typing either C-u followed by a number or <META> followed by a number as a command to pass that number to the function as its argument. Emacs has more than twenty characters predefined for use with interactive. In almost every case, one of these options will enable you to pass the right information interactively to a function. (See Interactive Codes.)

For example, the character r causes Emacs to pass the beginning and end of the region (the current values of point and mark) to the function as two separate arguments. It is used as follows:

(interactive "r")

On the other hand, a B tells Emacs to ask for the name of a buffer that will be passed to the function. When it sees a B, Emacs will ask for the name by prompting the user in the minibuffer, using a string that follows the B, as in "BAppend to buffer: ". Not only will Emacs prompt for the name, but Emacs will complete the name if you type enough of it and press <TAB>.

A function with two or more arguments can have information passed to each argument by adding parts to the string that follows interactive. When you do this, the information is passed to each argument in the same order it is specified in the interactive list. In the string, each part is separated from the next part by a \n, which is a newline. For example, you could follow "BAppend to buffer: " with a \n) and an r. This would cause Emacs to pass the values of point and mark to the function as well as prompt you for the buffer--three arguments in all.

In this case, the function definition would look like the following, where buffer, start, and end are the symbols to which interactive binds the buffer and the current values of the beginning and ending of the region:

(defun name-of-function (buffer start end)
  "documentation..."
  (interactive "BAppend to buffer: \nr")
  body-of-function...)

(The space after the colon in the prompt makes it look better when you are prompted. The append-to-buffer function looks exactly like this. See The Definition of append-to-buffer.)

If a function does not have arguments, then interactive does not require any. Such a function contains the simple expression (interactive). The mark-whole-buffer function is like this.

Alternatively, if the special letter-codes are not right for your application, you can pass your own arguments to interactive as a list. See interactive, for more information about this advanced technique.