Next: , Up: Understanding current-kill

The Body of current-kill

The body of the function definition is a let expression, which itself has a body as well as a varlist.

The let expression declares a variable that will be only usable within the bounds of this function. This variable is called interprogram-paste and is for copying to another program. It is not for copying within this instance of GNU Emacs. Most window systems provide a facility for interprogram pasting. Sadly, that facility usually provides only for the last element. Most windowing systems have not adopted a ring of many possibilities, even though Emacs has provided it for decades.

The if expression has two parts, one if there exists interprogram-paste and one if not.

Let us consider the `if not' or else-part of the current-kill function. (The then-part uses the kill-new function, which we have already described. See The kill-new function.)

     (or kill-ring (error "Kill ring is empty"))
     (let ((ARGth-kill-element
            (nthcdr (mod (- n (length kill-ring-yank-pointer))
                         (length kill-ring))
                    kill-ring)))
       (or do-not-move
           (setq kill-ring-yank-pointer ARGth-kill-element))
       (car ARGth-kill-element))

The code first checks whether the kill ring has content; otherwise it signals an error.

Note that the or expression is very similar to testing length with an if:

     (if (zerop (length kill-ring))          ; if-part
         (error "Kill ring is empty"))       ; then-part
       ;; No else-part

If there is not anything in the kill ring, its length must be zero and an error message sent to the user: ‘Kill ring is empty’. The current-kill function uses an or expression which is simpler. But an if expression reminds us what goes on.

This if expression uses the function zerop which returns true if the value it is testing is zero. When zerop tests true, the then-part of the if is evaluated. The then-part is a list starting with the function error, which is a function that is similar to the message function (see The message Function) in that it prints a one-line message in the echo area. However, in addition to printing a message, error also stops evaluation of the function within which it is embedded. This means that the rest of the function will not be evaluated if the length of the kill ring is zero.

Then the current-kill function selects the element to return. The selection depends on the number of places that current-kill rotates and on where kill-ring-yank-pointer points.

Next, either the optional do-not-move argument is true or the current value of kill-ring-yank-pointer is set to point to the list. Finally, another expression returns the first element of the list even if the do-not-move argument is true.