![]() |
Undo, redo and the modification history | ![]() |
Every change of a document in TeXmacs, whether it comes from a key press, a menu, a Scheme program, a plug-in session or another participant of a live document, ends up as a sequence of elementary modifications of the global edit tree. The undo system records these modifications together with their inverses, groups them into steps, and replays the inverses on demand. It is a small but subtle subsystem: the history is not a simple stack but a tree with several possible futures, changes are labeled with their author so that one can undo one's own changes past those of others, and temporary markers allow a sequence of changes to be grouped into one step or cancelled as a whole.
This chapter describes the data structures and algorithms (
Data/History
), how the editor feeds them and decides the granularity of undo, the Scheme interface, and the known problems. The tree observers through which modifications are reported are described in the architecture chapter, the editor classes in the editor, and the use of the same patches for live collaboration in live documents.
A modification (
Kernel/Types/modification.hpp
) is one of nine elementary operations on a tree at a path: assign, insert, remove, split, join, assign-node, insert-node, remove-node and set-cursor. Every change of the edit tree the_et goes through apply (
observer.cpp:390
), which announces the modification to the observers of the affected subtree before performing it.
A patch (
Data/History/patch.hpp
) is a modification with its inverse, a sequence (compound) or a set of alternatives (branch) of patches, a patch labeled with an author, or a birth patch which marks the appearance of an author or of a marker. Patches can be inverted, applied, commuted and joined.
Each editor, that is each view of a buffer, owns an archiver (
Data/History/archiver.hpp
) for the root of its buffer. It accumulates the pending modifications in current and moves them into the history archive as one step when the current user action ends.
The archiver attaches an undo_observer (
undo_observer.cpp
) to the buffer root. Its announce method passes each modification to archive_announce, which records it unless the archiver is itself replaying history.
Authors and markers are numbers of type double: new_author returns the integers 1, 2, 3, …, new_marker the half-integers 1.5, 2.5, … Every editor has its own author; plug-in sessions (except Scheme sessions) get a fresh author for each evaluation.
The data flow for one key press is:
handle_keypress: start_editing () set_author (editor's author)
keyboard-press (scheme) -> insert, remove, …
apply (the_et, mod)
undo_observer::announce -> archive_announce -> archiver::add
current := (inverse, mod) ; current
raw_apply: the tree changes, other observers are notified
end_editing () -> global_confirm ()
for every archiver with pending changes: confirm (); simplify ()
archive := (author: current) ; archive |
Undo takes the first step of the archive, applies it (it consists of inverses) with recording switched off, and stores its inverse as a new branch of the future.
Kernel/Types/modification.hpp,
Kernel/Abstractions/observer.cpp
The modification type, apply and the raw_* routines which perform a modification and notify the observers.
Data/History/patch.hpp,
patch.cpp
Patches, authors and markers; inversion, application, commutation, joining, compactify and cursor_hint.
Data/History/commute.cpp
The same operations on single modifications: invert, swap, commute, pull and join.
Data/History/archiver.cpp
The history of an editor: recording, confirming and cancelling steps, simplification, undo, redo, authors, markers and the save state.
Data/Observers/undo_observer.cpp
The observer which feeds the archiver.
Edit/Modify/edit_modify.cpp
The editor side: start_editing, end_editing, archive_state, undo, redo and the save state of a view.
Edit/Interface/edit_keyboard.cpp,
edit_mouse.cpp,
edit_interface.cpp
The event handlers which delimit user actions, and the markers of keyboard shortcuts.
utils/library/tree.scm,
texmacs/menus/edit-menu.scm,
utils/plugins/plugin-eval.scm
The Scheme side: try-modification, the Edit\(\rightarrow\)Undo and Edit\(\rightarrow\)Redo menus, and the authors of plug-in output.