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 Code Characters for
interactive
in The GNU Emacs Lisp Reference Manual.)
Consider the function zap-to-char
. Its interactive expression
is
(interactive "p\ncZap to char: ")
The first part of the argument to interactive
is ‘p’, with
which you are already familiar. This argument tells Emacs to
interpret a prefix, as a number to be passed to the function. You
can specify a prefix either by typing C-u followed by a number
or by typing META followed by a number. The prefix is the
number of specified characters. Thus, if your prefix is three and the
specified character is ‘x’, then you will delete all the text up
to and including the third next ‘x’. If you do not set a prefix,
then you delete all the text up to and including the specified
character, but no more.
The ‘c’ tells the function the name of the character to which to delete.
More formally, 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
can follow ‘p’ with a ‘\n’ and an ‘cZap to char: ’.
This causes Emacs to pass the value of the prefix argument (if there
is one) and the character.
In this case, the function definition looks like the following, where
arg
and char
are the symbols to which interactive
binds the prefix argument and the specified character:
(defun name-of-function (arg char) "documentation…" (interactive "p\ncZap to char: ") body-of-function…)
(The space after the colon in the prompt makes it look better when you
are prompted. See The Definition of
copy-to-buffer
, for an example.)
When a function does not take arguments, 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 The Definition of append-to-buffer
,
for an example. See Using Interactive
in The GNU Emacs Lisp Reference Manual, for a more complete
explanation about this technique.