Next: , Up: Cutting & Storing Text

Storing Text in a List

When text is cut out of a buffer, it is stored on a list. Successive pieces of text are stored on the list successively, so the list might look like this:

     ("a piece of text" "previous piece")

The function cons can be used to create a new list from a piece of text (an `atom', to use the jargon) and an existing list, like this:

     (cons "another piece"
           '("a piece of text" "previous piece"))

If you evaluate this expression, a list of three elements will appear in the echo area:

     ("another piece" "a piece of text" "previous piece")

With the car and nthcdr functions, you can retrieve whichever piece of text you want. For example, in the following code, nthcdr 1 ... returns the list with the first item removed; and the car returns the first element of that remainder—the second element of the original list:

     (car (nthcdr 1 '("another piece"
                      "a piece of text"
                      "previous piece")))
          ⇒ "a piece of text"

The actual functions in Emacs are more complex than this, of course. The code for cutting and retrieving text has to be written so that Emacs can figure out which element in the list you want—the first, second, third, or whatever. In addition, when you get to the end of the list, Emacs should give you the first element of the list, rather than nothing at all.

The list that holds the pieces of text is called the kill ring. This chapter leads up to a description of the kill ring and how it is used by first tracing how the zap-to-char function works. This function uses (or `calls') a function that invokes a function that manipulates the kill ring. Thus, before reaching the mountains, we climb the foothills.

A subsequent chapter describes how text that is cut from the buffer is retrieved. See Yanking Text Back.