8.2.2 Lisp macro

The part of the condition-case expression that is evaluated in the expectation that all goes well has a when. The code uses when to determine whether the string variable points to text that exists.

A when expression is simply a programmers’ convenience. It is like an if without the possibility of an else clause. In your mind, you can replace when with if and understand what goes on. That is what the Lisp interpreter does.

Technically speaking, when is a Lisp macro. A Lisp macro enables you to define new control constructs and other language features. It tells the interpreter how to compute another Lisp expression which will in turn compute the value. In this case, the other expression is an if expression.

The kill-region function definition also has an unless macro; it is the opposite of when. The unless macro is like an if except that it has no then-clause, and it supplies an implicit nil for that.

For more about Lisp macros, see Macros in The GNU Emacs Lisp Reference Manual. The C programming language also provides macros. These are different, but also useful.

Regarding the when macro, in the condition-case expression, when the string has content, then another conditional expression is executed. This is an if with both a then-part and an else-part.

(if (eq last-command 'kill-region)
    (kill-append string (< end beg) yank-handler)
  (kill-new string nil yank-handler))

The then-part is evaluated if the previous command was another call to kill-region; if not, the else-part is evaluated.

yank-handler is an optional argument to kill-region that tells the kill-append and kill-new functions how deal with properties added to the text, such as bold or italics.

last-command is a variable that comes with Emacs that we have not seen before. Normally, whenever a function is executed, Emacs sets the value of last-command to the previous command.

In this segment of the definition, the if expression checks whether the previous command was kill-region. If it was,

(kill-append string (< end beg) yank-handler)

concatenates a copy of the newly clipped text to the just previously clipped text in the kill ring.