![]() |
The archiver | ![]() |
The constructor of edit_modify_rep (
edit_modify.cpp:23
) allocates a new author for the editor and an archiver for the root path rp of its buffer. The archiver attaches an undo observer to that subtree of the_et and registers itself in the global set archs. Since there is one editor per view, a buffer shown in two windows has two archivers with different authors. Both observe the same subtree and record every modification, so in each of them the changes made in the other view are changes of another author.
The state of an archiver (
archiver.hpp
) is:
The modifications of the user action in progress, most recent first, each stored as a patch (inverse, modification). Applying current as it is undoes the action.
The history: an undo part and the possible futures, see below.
The author of the editor, and the author of the modifications in current.
The number of steps in the undo part, and its value at the last save and autosave (-1 when unknown). They tell whether the document is modified.
True while the archiver applies a patch itself. The modifications it makes are then not recorded; the global flag busy_versioning (busy-versioning? in Scheme) is set at the same time.
The history is a tree, built from compound and branch patches:
archive ::= branch [ undo-part, future_1, …, future_n ] undo-part ::= compound ( step, archive ) or nothing future_i ::= compound ( redo-step, archive ) |
A branch with a single element is represented by that element (make_branches). The step of the undo part is the last confirmed user action. It is a patch labeled with its author whose modifications are the inverses of the changes, so that applying it undoes the action; its get_inverse parts redo it. The futures are the changes which were undone and can be redone. Each one is again a step, followed by the history which applies after it. Undoing a step adds a future instead of replacing the existing ones, so the history keeps several futures: after undoing a change and making a different one, the undone change can still be redone after undoing the new one. In that case Edit\(\rightarrow\)Redo becomes a submenu with one entry per branch (redo-menu in
edit-menu.scm
).
undo_possibilities is 1 when there is an undo part and 0 otherwise; redo_possibilities is the number of futures.
archive_announce (
archiver.cpp:73
) receives each modification with its absolute path. Unless the archiver is replaying history, add computes the inverse on the tree before the change and puts the pair in front of current; the archiver is also put in the set pending_archs. If the global author (get_author ()) differs from the_owner, the pending modifications are first confirmed as a step of their own, so that a step always has a single author.
The editor encloses every user action between start_editing and end_editing (
edit_modify.cpp:293
): key presses in handle_keypress (
edit_keyboard.cpp:395
), mouse events in handle_mouse (
edit_mouse.cpp:699
) and menu actions in before_menu_action and after_menu_action (
edit_interface.cpp:1152
). start_editing sets the global author to the author of the editor. end_editing calls global_confirm, which confirms and simplifies every pending archiver, so an action which modifies several buffers adds one step to each of them. If the action throws an error (the handlers catch it since USE_EXCEPTIONS is defined), cancel_editing calls global_cancel, which applies current and so restores the documents.
confirm (
archiver.cpp:324
) labels current with its owner, compactifies it, and drops it if it only moves the cursor. Otherwise it becomes the new step: archive := (current ; archive). The old history, including its futures, becomes the history below the new step, so the futures are kept. When the new step belongs to another author (a plug-in, another view), normalize then moves those futures of the editor's author which commute with it up to the new step, so that the user can still redo them.
simplify (
archiver.cpp:385
) then merges the last two steps when they can be joined (see join in patches), when the older one has no futures, and when the save point is not between them. This is why a word typed character by character is undone at once, while a new paragraph, a deletion following an insertion or a save start a new step.
Before menu actions, keyboard shortcuts and try-modification, archive_state (
edit_modify.cpp:280
) records the cursor and the selection as cursor modifications whose data name the author. When such a step is undone, notify_set_cursor restores the cursor and the selection of that author only.
undo_one (
archiver.cpp:429
) computes the inverse of the step of the undo part on the current tree, applies the step, and makes the inverse a new future, then moves to the history below the step. redo_one (i) (
archiver.cpp:451
) applies future number i; the remaining futures become futures of the history below. Both refuse to act while current is not empty, and return a cursor position (cursor_hint) to which the editor moves.
The public undo and redo take authors into account. Before each step, undo calls expose (
archiver.cpp:226
), which tries to bring the most recent step of the editor's own author to the top by commuting it (swap) with the more recent steps of other authors. If the step on top is then the editor's own, only that step is undone. Otherwise the step of the other author is undone, and the loop continues until a step of the own author has been undone. In a session, for instance, the output of each evaluation has its own author, whose birth is recorded in the step which sent the input (start-slave in
plugin-eval.scm
). If the user edits another part of the document while the output arrives, undo removes the user's edit and leaves the output, since the two changes commute. Undoing the step which sent the input removes the output as well: a patch is never moved past the birth of its author. redo redoes future i and then continues as long as there is a single future which does not belong to the editor's author. Right after a step of the editor's author, it also stops if the next step belongs to another editor (the authors in genuine_authors), so that the steps of slave authors such as plug-ins are redone together with it.
A marker groups the steps made after it, or cancels them. Markers are numbers from new_marker (mark-new in Scheme), and are stored as steps containing a birth patch.
Confirm the pending modifications, then add a step which only contains the birth patch of m.
Confirm, then merge the steps above the marker into one step where possible (compress: consecutive steps of the same author without futures), and remove the marker step. When the marker is not found, a warning is printed and the history is emptied (
archiver.cpp:587
).
Cancel the pending modifications, then undo and forget the steps above the marker and remove it; return true. If a step of another author is found above the marker, the marker is removed, the remaining steps are kept and false is returned.
try-modification (
tree.scm:363
) combines them: the body is executed after mark_start and archive_state, and its changes become one step if it returns a true value, or are cancelled otherwise. Keyboard shortcuts use the same mechanism in try_shortcut (
edit_keyboard.cpp:98
): the effect of a key which starts a longer shortcut, for instance the insertion of < before =, is enclosed in a marker sh_mark, and mark_cancel takes it back when the next key completes the shortcut. interrupt_shortcut, called at every undo or redo, ends the marker.
Three more operations edit the top of the history directly: retract (remove-undo-mark) reopens the last step of the own author as current, so that the next modifications are added to it; forget (unredoable-undo) undoes it without creating a future; start_slave (a) adds the birth patch of author a to current.
need_save of an editor (
edit_modify.cpp:413
) compares last_save with the depth, corrected by one when the step on top is a marker (corrected_depth). notify_save confirms the pending changes and records the depth; require_save sets last_save to -1, so that the buffer counts as modified until the next save. A buffer is modified (buffer-modified?) when one of its views needs saving (
new_buffer.cpp:45
). Undoing back to the saved depth makes the document unmodified again, and the editor says so in the status bar (Your document is back in its original state).
The depth only counts steps, so it is an approximation: clear, expose and normalize set last_save to -1 because they reorder or forget steps, and redoing another branch than the first one past the save point does the same.