Skip to content

Instantly share code, notes, and snippets.

@archenemies
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archenemies/58f1373eaa3e16ca0a3f to your computer and use it in GitHub Desktop.
Save archenemies/58f1373eaa3e16ca0a3f to your computer and use it in GitHub Desktop.
Emacs Yank Pointer Problem Description
Problem:
In Emacs, I can use C-y (yank) and then M-y (yank-pop) to go through a
list of recent bits of text which have been killed.
Having done this, the next time I do C-y, it yanks the last bit of
text selected via M-y, which is desired since it means I don't have to
rotate through the list to find my favorite item again. This behavior
is implemented using the variable "kill-ring-yank-pointer".
However, when I kill some text, the value of "kill-ring-yank-pointer"
is reset to point to the head of the "kill ring" (which is not really
a ring). This means that when I am interspersing a number of yanks (of
the same item of text) with an editing command such as M-d which also
populates the kill ring, then my "kill-ring-yank-pointer" is reset
each time, and each time I yank, I have to search farther and farther
back with an increasing number of M-y keystrokes, just to get the same
item I've been yanking.
Solution:
The kill ring, which is a list, should be treated as an array by
yank-pop. Yank-pop should create a new variable to serve the purpose
of "kill-ring-yank-pointer". However, rather than pointing to an
existing item in the kill ring, the new variable will just tell
yank-pop how to fix up the kill ring in case the last command was
yank-pop. For example, suppose the kill ring looks like this:
A B C D E
C-y M-y M-y will end up with C being inserted at the cursor. After
that, we DON'T want the kill ring to look like "C D E A B" because we
care more about A and B than about D or E, as they are more recent.
What we want it to look like is
C A B D E
However, suppose the next command is also M-y. Then the kill ring
should look like
D A B C E
because we care more about A and B than about C (C we only saw on our
way to D). In order to obtain this configuration, yank-pop will have
to have inserted C back where it belongs, and moved D to the front. It
would presumably do this by using an integer variable which is
incremented at each yank-pop, and reset to 0 in the 'yank' command, to
guide the insertion and extraction of elements from the kill ring.
----------------------------------------------------------------
Update 10 Aug 2015:
The above list-reordering algorithm has been used before for moving
between buffers. Try the iflipb package:
http://emacswiki.org/emacs/iflipb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment