Go to the previous, next section.

The  message Function

Like  + , the  message function takes a variable number of arguments. It is used to send messages to the user and is so useful that we will describe it here.

A message is printed in the echo area. For example, you can print a message in your echo area by evaluating the following list:

(message "This message appears in the echo area!")

The whole string between double quotation marks is a single argument and is printed in toto. (Note that in this example, the message itself will appear in the echo area within double quotes; that is because you see the value returned by the  message function. In most uses of  message in programs that you write, the text will be printed in the echo area as a side-effect, without the quotes. See section An Interactive  multiply-by-seven ., for an example of this.)

However, if there is a `%s' in the quoted string of characters, the  message function does not print the `%s' as such, but looks to the argument that follows the string. It evaluates the second argument and prints the value in the location in the string where the `%s' is.

You can see this by positioning the cursor after the following expression and typing C-x C-e:

(message "The name of this buffer is: %s." (buffer-name))

In Info,  "The name of this buffer is: *info*." will appear in the echo area. The function  buffer-name returns the name of the buffer as a string, which the  message function inserts in place of  %s .

To print a value as a decimal number, use `%d' in the same way as `%s'. For example, to print a message in the echo area that states the value of the  fill-column , evaluate the following:

(message "The value of fill-column is %d." fill-column)

On my system, when I evaluate this list,  "The value of fill-column is 72." appears in my echo area.

If there is more than one `%s' in the quoted string, the value of the first argument following the quoted string is printed at the location of the first `%s' and the value of the second argument is printed at the location of the second `%s', and so on. For example, if you evaluate the following,

(message "There are %d %s in the office!"
         (- fill-column 14) "pink elephants")

a rather whimsical message will appear in your echo area. On my system it says,  "There are 58 pink elephants in the office!" .

The expression  (- fill-column 14) is evaluated and the resulting number is inserted in place of the `%d'; and the string in double quotes,  "pink elephants" , is treated as a single argument and inserted in place of the `%s'. (That is to say, a string between double quotes evaluates to itself, like a number.)

Finally, here is a somewhat complex example that not only illustrates the computation of a number, but also shows how you can use an expression within an expression to generate the text that is substituted for `%s':

(message "He saw %d %s"
         (- fill-column 34)
         (concat "red "
                 (substring
                  "The quick brown foxes jumped." 16 21)
                 " leaping."))

In this example,  message has three arguments: the string,  "He saw %d %s" , the expression,  (- fill-column 32) , and the expression beginning with the function  concat . The value resulting from the evaluation of  (- fill-column 32) is inserted in place of the `%d'; and the value returned by the expression beginning with  concat is inserted in place of the `%s'.

When I evaluate the expression, the message,  "He saw 38 red foxes leaping." , appears in my echo area.

Go to the previous, next section.