Node:rotate-yk-ptr remainder, Next:, Previous:Remainder Function, Up:rotate-yk-ptr body



Using % in rotate-yank-pointer

When the kill-ring-yank-pointer points to the beginning of the kill ring, and the argument passed to rotate-yank-pointer is 1, the % expression returns 1:

(- length (length kill-ring-yank-pointer))
     => 0

therefore,

(+ arg (- length (length kill-ring-yank-pointer)))
     => 1

and consequently:

(% (+ arg (- length (length kill-ring-yank-pointer)))
   length)
     => 1

regardless of the value of length.

As a result of this, the setq kill-ring-yank-pointer expression simplifies to:

(setq kill-ring-yank-pointer (nthcdr 1 kill-ring))

What it does is now easy to understand. Instead of pointing as it did to the first element of the kill ring, the kill-ring-yank-pointer is set to point to the second element.

Clearly, if the argument passed to rotate-yank-pointer is two, then the kill-ring-yank-pointer is set to (nthcdr 2 kill-ring); and so on for different values of the argument.

Similarly, if the kill-ring-yank-pointer starts out pointing to the second element of the kill ring, its length is shorter than the length of the kill ring by 1, so the computation of the remainder is based on the expression (% (+ arg 1) length). This means that the kill-ring-yank-pointer is moved from the second element of the kill ring to the third element if the argument passed to rotate-yank-pointer is 1.