GNU TeXmacsUser manualReferenceAboutEnglish
Live documents and shared editing

1.Introduction

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:

  1. The C++ algebra of modifications and patches, which also underlies undo and redo.

  2. 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.

  3. 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.

2.Modifications and patches

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:

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.

3.Local live documents

3.1.States and history

utils/relate/live-document.scm

keeps three tables indexed by a live identifier lid (for remote documents, the URL tmfs://live/server/name):

live-documents

The current tree.

live-states

The list of state identifiers, most recent first. States are unique identifiers (create-unique-id).

live-changes

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).

3.2.Views

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.

4.The network protocol

4.1.Opening a remote live document

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.

4.2.Client to server

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.

4.3.Server to 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.

4.4.Rebasing on the client

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:

  1. flushes pending local edits (live-treat-pending);

  2. 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);

  3. retracts all local changes after that state (live-retract, which also updates the views): these changes are lost;

  4. 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);

  5. 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;

  6. 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.

4.5.Persistence and disconnection

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.

5.Interaction with undo and redo

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.

6.Extending live documents

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.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".