Node:rotate-yk-ptr negative arg, Previous:rotate-yk-ptr arg, Up:yank

Passing a negative argument

Finally, the question arises, what happens if either the remainder function, %, or the nthcdr function is passed a negative argument, as they quite well may?

The answers can be found by a quick test. When (% -1 5) is evaluated, a negative number is returned; and if nthcdr is called with a negative number, it returns the same value as if it were called with a first argument of zero. This can be seen be evaluating the following code.

Here the => points to the result of evaluating the code preceding it. This was done by positioning the cursor after the code and typing C-x C-e (eval-last-sexp) in the usual fashion. You can do this if you are reading this in Info inside of GNU Emacs.

(% -1 5)
     => -1

(setq animals '(cats dogs elephants))
     => (cats dogs elephants)

(nthcdr 1 animals)
     => (dogs elephants)

(nthcdr 0 animals)
     => (cats dogs elephants)

(nthcdr -1 animals)
     => (cats dogs elephants)

So, if a minus sign or a negative number is passed to yank, the kill-ring-yank-point is rotated backwards until it reaches the beginning of the list. Then it stays there. Unlike the other case, when it jumps from the end of the list to the beginning of the list, making a ring, it stops. This makes sense. You often want to get back to the most recently clipped out piece of text, but you don't usually want to insert text from as many as thirty kill commands ago. So you need to work through the ring to get to the end, but won't cycle around it inadvertently if you are trying to come back to the beginning.

Incidentally, any number passed to yank with a minus sign preceding it will be treated as -1. This is evidently a simplification for writing the program. You don't need to jump back towards the beginning of the kill ring more than one place at a time and doing this is easier than writing a function to determine the magnitude of the number that follows the minus sign.