Previous: , Up: Arguments   [Contents][Index]

1.8.5 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 multiply-by-seven in detail, 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 at 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 an integer, 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 area4.

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 32)
         (concat "red "
                  "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 your fill column is 70 and you evaluate the expression, the message "He saw 38 red foxes leaping." appears in your echo area.



Actually, you can use %s to print a number. It is non-specific. %d prints only the part of a number left of a decimal point, and not anything that is not a number.

Previous: , Up: Arguments   [Contents][Index]