How Emacs determines a unit of work to undo
When you enter the command C-/, Emacs undoes some part of your recent changes to a buffer. When you enter C-/ again, it undoes another chunk of work.
I have read the Emacs manual entry on Undo but it is vague about exactly how it works. The manual says "Consecutive character insertion commands are usually grouped together into a single undo record" but it does not explain how it decides the number of character insertion commands that constitute a group. The number of characters it puts in a group seems random.
Can a开发者_运维知识库nyone explain the algorithm Emacs uses to group characters into undo records?
The logic for setting undo boundaries is mostly in self-insert-command
which is implemented in cmds.c
. You'll have to read the code for the full story, but basically:
- As long as you are just typing characters, there's an undo boundary every 20 characters you type.
- But if you issue a different editing command (you kill a word, for example), this causes an undo boundary to be added immediately, resetting the character count.
- Certain "hairy" insertions (as determined by
internal_self_insert
) cause an an undo boundary to be added immediately, and the character count to be reset. Reading the code, it looks as though these are: (1) inoverwrite-mode
, if you overwrote a character with one that has a different width, e.g. typing over a tab; (2) if the character you inserted caused an abbreviation to be expanded; (3) if the character you typed causedauto-fill-mode
to insert indentation. - In addition, any editing command that decides it would be a good idea to have an undo boundary can request it by calling
undo-boundary
. This does not cause the character count to be reset.
精彩评论