![]() |
Live documents and shared editing | ![]() |
A live document is a document fragment which can be displayed and edited simultaneously in several places: in several buffers of the same TeXmacs process, or by several users connected to the same server. Live editing is built on three ingredients:
The C++ algebra of modifications and patches, which also underlies undo and redo.
A purely local layer (
utils/relate/live-document.scm
and
live-view.scm
) which maintains, for each live document, its current value, a history of states, and the views which display it.
A network layer (
utils/relate/live-connection.scm
,
client/client-live.scm
and
server/server-live.scm
) in which the server holds the reference copy of the document and orders the modifications of the clients.
The implementation is experimental: it works for simple cases, but the code still contains FIXME notes and debugging output, and conflicting modifications are resolved by discarding local changes (see below). For the user interface, see Remote tools and collaborative editing.
A modification (
Kernel/Types/modification.hpp
) is an elementary change of a tree at a given path. Its kind is one of MOD_ASSIGN, MOD_INSERT, MOD_REMOVE, MOD_SPLIT, MOD_JOIN, MOD_ASSIGN_NODE, MOD_INSERT_NODE, MOD_REMOVE_NODE and MOD_SET_CURSOR; in Scheme it is printed as a list (kind path tree) by modification->scheme and read back by scheme->modification (
kernel/library/patch.scm
), with kinds assign, insert, remove, split, join, assign-node, insert-node, remove-node and set-cursor. This is the form in which changes travel over the network.
A patch (
Data/History/patch.hpp
) is either a pair of a modification and its inverse (PATCH_MODIFICATION), a sequence of patches (PATCH_COMPOUND), a set of alternatives (PATCH_BRANCH, used for redo trees), a birth or death marker (PATCH_BIRTH) or a patch labeled with an author (PATCH_AUTHOR). The essential operations, available in Scheme under the names given in parentheses, are:
inversion with respect to the tree before the patch (patch-invert), application (patch-apply, patch-applicable?);
commutation (
Data/History/commute.cpp
): swap (p1, p2) tries to rewrite \(p_1 ; p_2\) as \(p_2^{} ; p_1^{}\) with the same effect, and can_pull, pull and co_pull (patch-can-pull?, patch-pull, patch-co-pull) return the two transformed patches. This is the operational transformation used both by the undo system (to undo one's own changes past the changes of other authors) and by live editing (to rebase local changes on top of remote ones).
The conversion between patches and the network form is done by patch->modlist and modlist->patch (
utils/relate/live-connection.scm
); the latter needs the tree to which the modifications apply in order to compute the inverses.
utils/relate/live-document.scm
keeps three tables indexed by a live identifier lid (for remote documents, the URL tmfs://live/server/name):
The current tree.
The list of state identifiers, most recent first. States are unique identifiers (create-unique-id).
For each state except the oldest one, the inverse patch leading back to the previous state.
(live-apply-patch lid p [state]) applies a patch, pushes a new state (a fresh one, or the given one) and returns it. From the history one can recover the document at any recorded state (live-get-document), the list of states and patches since a given state (live-get-state-list, live-get-patch-list), and the patch from a past state to the current one (live-get-inverse-patch). live-retract undoes the most recent states until a given one is current, and live-rewrite-history replaces the part of the history above a given state by an equivalent sequence of states and changes; it raises an error if the new history is not equivalent to the old one (patch-strong-equivalent?). To bound memory, live-forget-obsolete drops all states older than the oldest state still in use; the states in use are collected by the overloaded function live-states-in-use (states of views and of remote peers).
A view is a subtree of an ordinary buffer marked up with live-io (
packages/utilities/live.ts
) or live-io* (
packages/miscellaneous/live-document.ts
):
<live-io|view-id|live-id|body> |
The macro wraps the body in a locus whose identifier is the live identifier, attaches an observer which calls the Scheme function live-notify on every modification of the view, and contains a hidden extern call to live-initialize, which is evaluated when the view is first typeset.
live-view.scm
keeps, for each view identifier, the state of the live document which it displays.
Initialization. live-initialize either restores the view from an existing live document or calls live-retrieve, which by default creates the live document from the contents of the view and which is overloaded for remote documents (see below). If the same view identifier occurs twice (e.g. after copy and paste), live-view-separate gives fresh identifiers to the copies.
Local edits. live-notify ignores cursor movements and changes made while live-updating? is set, turns each modification into a patch and queues it in live-pending. The queue is flushed shortly afterwards (delayed with :idle 1) by live-treat-pending, which applies the accumulated patch of the edited view to the live document. If two views of the same document were edited in the same interval, only the first one is taken into account and the others are reset.
Propagation to views. live-update-views brings every view to the current state, by applying the patch from the view's state to the current state when possible, and by replacing the whole view otherwise. These changes are made with live-updating? set, and under a dedicated author live-author, obtained with new-author.
The load handler of tmfs://live/server/name (
client/client-live.scm
) returns a document in the style live-document whose body is a single live-io* view with an empty body. When the view is initialized, the overloaded live-retrieve sends (live-open lid) to the server. The service live-open (
server/server-live.scm
) creates the document if necessary (a database entry of type "live" and a file in the repository), loads it into memory, checks read access, registers the connection with live-connect and answers (state doc). The client then creates its local copy with the same state identifier ((live-create lid doc state)), records that the server is at that state (live-connect) and restores its views.
Both sides thus share the state identifiers. For every connection, the server remembers the state that the client is known to have (live-get-remote-state); the client remembers the state that the server is known to have.
When a local edit is applied to a remote live document, the overloaded live-apply-patch of
client-live.scm
sends
(live-modify lid mods old-state new-state) |
where mods is the list of modifications and the two states are the local states before and after the edit. The service live-modify accepts the change if and only if the user may write the document, the client's last known state and the server's current state both equal old-state, and the patch is applicable (live-applicable?). In that case it applies the patch, adopting new-state as its own new state, and answers #t; otherwise it answers #f and does nothing. In other words, the server never transforms patches: it only accepts patches which are based on its current state, and leaves the rebasing to the clients.
After each accepted change, live-broadcast considers every connected client whose known state differs from the current state, and sends it (server-remote-eval) the patch from its known state to the current state, again in the form (live-modify lid mods old-state new-state). At most one such request per client and document is outstanding at any time (table live-waiting); when the client confirms, the server updates the client's known state and checks again whether more changes need to be sent.
The call-back live-modify of
client-live.scm
receives a patch \(p\) based on old-state, a state which the client necessarily has in its history (it is the last state which the server acknowledged or sent). The local history may contain changes made since then which the server has not yet accepted. The call-back:
flushes pending local edits (live-treat-pending);
computes, with live-latest-compatible, the most recent local state such that all local changes between old-state and that state commute with \(p\) (tested with patch-can-pull? and by checking that both orders give the same tree);
retracts all local changes after that state (live-retract, which also updates the views): these changes are lost;
pulls \(p\) over the remaining local changes, giving a patch \(p^{}\) which applies to the current local document, and transforms the local changes accordingly (patch-pull, patch-co-pull);
applies \(p^{}\) without sending it back (the flag following-server-instruction? is set), and rewrites the history so that it reads: old-state, the server change leading to new-state, then the transformed local changes with fresh state identifiers;
records that the server is at new-state, answers #t and resends the remaining local changes, now based on new-state (live-resend-local-changes).
When the server refuses a client change (because another change was accepted first), the client does nothing: the server will send the concurrent change, the client will rebase its own change on top of it as above, and resend it. Since the server processes one message at a time, this converges as long as the changes commute. When they do not, the local changes which do not commute with the remote ones are discarded; the functions live-find-latest-compatible and live-rewrite-history print diagnostics in that case.
The server keeps live documents in memory. The document is written to the repository (as a TeXmacs snippet, live-save) when it is first created and whenever a client disconnects: the overloaded server-remove saves every live document the client was connected to and removes the client from the peers (live-hang-up). There is no periodic saving, so modifications made while clients stay connected are lost if the server process dies. Live documents are created readable and writable by "all". The service remote-list-live lists the live documents owned by the user, and live-exists? tests the existence of a document.
Each view of a buffer has an archiver (
Data/History/archiver.hpp
) which records modifications as patches labeled with their author. Changes coming from other participants are applied to the views under the author live-author, so they are recorded as changes of another author. When the user undoes, archiver_rep::undo first tries to move the user's own most recent change to the top of the history by commuting it with more recent changes of other authors (archiver_rep::expose, which relies on swap for patches); if this succeeds, only the own change is undone. Otherwise the changes of other authors on top of it are undone as well. The resulting modifications of the view are ordinary edits (live-updating? is not set), so live-notify propagates the undo to the live document and hence to the server like any other local change. There is no global, collaborative undo. The history itself is described in undo, redo and the modification history.
The local layer is independent of the network: the functions live-retrieve, live-apply-patch and live-states-in-use are designed to be overloaded (with :require or through former). A new transport, for instance live documents shared with a plug-in, would provide its own live-retrieve for its identifiers, overload live-apply-patch to forward local changes, and apply incoming changes with the same rebasing logic as the live-modify call-back.