![]() |
Modifications and patches | ![]() |
A modification (
Kernel/Types/modification.hpp
) has a kind k, a path p and an optional tree t. The last items of the path encode the position and the arguments of the operation; root, index and argument extract them:
Replace the subtree at p by t.
Insert the children of t (or the characters of a string t) at position pos of the subtree at p.
Remove nr children or characters starting at pos.
Split the child pos at position at into two children, or join the children pos and pos+1.
Change the label of the subtree at p.
Wrap the subtree at p into t as its child pos, or replace the subtree at p by its child pos.
Change nothing, but record a cursor position. The editor uses it to restore the cursor and the selection on undo (see archive_state below).
All changes of the edit tree the_et go through apply (tree& ref, modification mod) (
observer.cpp:390
) and its wrappers assign, insert, remove, … When the tree is attached to the edit tree, the modification is made absolute (reverse (ip) * mod) and performed by raw_apply on the_et. Modifications made by observers while one is being performed are queued in upcoming and performed afterwards, unless they touch a path which is already busy, in which case they are silently dropped. Each raw_* routine calls announce on the observers of the subtree before the change and done after it; the undo observer records modifications in announce, since the inverse must be computed on the tree before the change (its notify_* methods only keep it attached to the right tree).
The functions of
commute.cpp
work on single modifications:
The modification which undoes m applied to t. It needs the tree before the change: the inverse of a removal re-inserts the removed part, the inverse of an assignment assigns the old subtree, and so on.
Given that m1 followed by m2 is defined, compute m2* and m1* such that m2* followed by m1* has the same effect, by shifting the paths of one modification over the other. It returns false when this is impossible, for instance when one modification changes the subtree which the other one replaces. commute, pull and co_pull are derived from it. This is the operational transformation used by the undo system and by live documents.
Merge two insertions of text into the same string at adjacent positions, or two adjacent removals from the same string, into one modification. Nothing else is joined.
A patch (
patch.hpp
) has one of five types:
A pair (mod, inv) of a modification and its inverse, built by patch (mod, inv). get_modification and get_inverse return the two parts.
A sequence of patches, applied from the first to the last. patch (p1, p2) is the sequence of two patches; the archiver builds its lists from such pairs.
A set of alternative patches. It is only used for the possible futures of the history.
A patch labeled with an author, built by patch (author, p).
A patch without effect, built by patch (author, create), which marks the moment an author (or a marker) appears. The archiver always records births with create = false, for slave authors and for markers alike; markers are told apart by their numbers (is_marker).
The functions on patches (
patch.cpp
) extend those on modifications: invert (p, t) reverses a compound and inverts its parts; apply (p, t) applies a patch to the edit tree; is_applicable tests whether it can be applied (the archiver asserts this before replaying a step); swap (
patch.cpp:429
) commutes patches recursively, refusing branches, and refusing to move a patch past the birth of its own author; join (
patch.cpp:554
) joins two patches of the same author whose only effective parts are joinable modifications, keeping the cursor modifications around them.
Three helpers prepare patches for the history:
Flattens nested compounds, drops pairs of a modification followed directly by its inverse, and moves a common author label of all the parts outside.
Removes the cursor modifications. A step which only contains such modifications is not stored in the history.
A cursor position for the first modification of p which is not a cursor modification, computed on the tree t before p is applied: for an insertion, the place where the text goes. After an undo or a redo the editor moves the cursor to the hint of the inverse of the replayed step, computed on the tree after the replay, so that the cursor ends up at the place of the change.
Patches are printed by operator <<; show-history prints the history of the current editor this way on the console.