Node:multiply-by-seven in detail, Previous:Interactive multiply-by-seven, Up:Interactive



3.3.1 An Interactive multiply-by-seven

Let's look at the use of the special form interactive and then at the function message in the interactive version of multiply-by-seven. You will recall that the function definition looks like this:

(defun multiply-by-seven (number)       ; Interactive version.
  "Multiply NUMBER by seven."
  (interactive "p")
  (message "The result is %d" (* 7 number)))

In this function, the expression, (interactive "p"), is a list of two elements. The "p" tells Emacs to pass the prefix argument to the function and use its value for the argument of the function.

The argument will be a number. This means that the symbol number will be bound to a number in the line:

(message "The result is %d" (* 7 number))

For example, if your prefix argument is 5, the Lisp interpreter will evaluate the line as if it were:

(message "The result is %d" (* 7 5))

(If you are reading this in GNU Emacs, you can evaluate this expression yourself.) First, the interpreter will evaluate the inner list, which is (* 7 5). This returns a value of 35. Next, it will evaluate the outer list, passing the values of the second and subsequent elements of the list to the function message.

As we have seen, message is an Emacs Lisp function especially designed for sending a one line message to a user. (See The message function.) In summary, the message function prints its first argument in the echo area as is, except for occurrences of %d, %s, or %c. When it sees one of these control sequences, the function looks to the second and subsequent arguments and prints the value of the argument in the location in the string where the control sequence is located.

In the interactive multiply-by-seven function, the control string is %d, which requires a number, and the value returned by evaluating (* 7 5) is the number 35. Consequently, the number 35 is printed in place of the %d and the message is The result is 35.

(Note that when you call the function multiply-by-seven, the message is printed without quotes, but when you call message, the text is printed in double quotes. This is because the value returned by message is what appears in the echo area when you evaluate an expression whose first element is message; but when embedded in a function, message prints the text as a side effect without quotes.)