Next: , Previous: Digression into C, Up: Cutting & Storing Text

8.5 Initializing a Variable with defvar

The copy-region-as-kill function is written in Emacs Lisp. Two functions within it, kill-append and kill-new, copy a region in a buffer and save it in a variable called the kill-ring. This section describes how the kill-ring variable is created and initialized using the defvar special form.

(Again we note that the term kill-ring is a misnomer. The text that is clipped out of the buffer can be brought back; it is not a ring of corpses, but a ring of resurrectable text.)

In Emacs Lisp, a variable such as the kill-ring is created and given an initial value by using the defvar special form. The name comes from “define variable”.

The defvar special form is similar to setq in that it sets the value of a variable. It is unlike setq in two ways: first, it only sets the value of the variable if the variable does not already have a value. If the variable already has a value, defvar does not override the existing value. Second, defvar has a documentation string.

(There is a related macro, defcustom, designed for variables that people customize. It has more features than defvar. (See Setting Variables with defcustom.)