GNU TeXmacsUser manualReferenceAboutEnglish
The link registry in C++

1.The global tables

The file

Data/Observers/link.cpp

keeps three global tables:

hashmap<string,list<observer> > id_resolve

For each identifier, the observers which point to the subtrees carrying it.

hashmap<observer,list<string> > pointer_resolve

The converse: for each observer, the identifiers it stands for.

hashmap<tree,list<soft_link> > vertex_occurrences

For each vertex (a tree such as (id "x") or (url "…")), the links in which it occurs.

and a counter type_count of the registered links of each type, which all_link_types enumerates. The low level routines register_pointer, unregister_pointer, register_link and unregister_link maintain the tables; they are file local in practice and only called by link repositories.

The tables are keyed by observers rather than by trees because trees are edited: a locus must still be found after its body has been modified, split or replaced. The observers used for loci are tree pointers (

Data/Observers/tree_pointer.cpp

), which follow their subtree through modifications: on an assignment, a variant split or a variant join they move to the new subtree, on the removal of a node they move to the child which is kept, on detachment they move to the closest remaining tree, and, when created with flag set (as for all loci registered by the typesetter), they move up to a node which is inserted above them. obtain_tree (obs) returns the current tree of an observer.

2.Soft links and link repositories

class soft_link
(a registered link)

A concrete handle around a single tree, the link itself. Two soft links are equal only if they are the same object; this is what allows the same link tree to be registered several times (for instance once for each locus which carries it) and unregistered independently.

class link_repository_rep
(a set of loci and links)

Declared in

link.hpp

. It holds three lists, ids, loci (observers, in parallel with ids) and links, and has three methods:

insert_locus (id, t)

Creates a tree pointer to t (with the flag set), registers it for id and attaches it to t.

insert_locus (id, t, cb)

The same with a scheme_observer (t, cb), a tree pointer which also calls the Scheme function cb when the tree is modified (see below).

insert_link (ln)

Registers a soft link: every vertex ln->t[1], …, ln->t[n-1] is mapped to it, and the type counter of ln->t[0] is incremented. (The attribute argument added by the typesetter thus counts as a vertex as well; it never matches a real vertex.)

The destructor unregisters and detaches everything. The handle link_repository is reference counted; link_repository (true) creates a fresh, empty repository, and the null handle means “no repository”.

Since unregistration happens in the destructor, the lifetime of a repository decides how long its loci exist. Two kinds of repositories are used:

3.Queries

get_ids (tree t)

The identifiers of the loci whose pointer is attached to t (in the order of the observers of the tree). Exported as tree->ids.

get_trees (string id)

The current trees of all pointers registered for id, in order of registration. Exported as id->trees.

get_links (tree v)

The links in which the vertex v occurs. Exported as vertex->links.

all_link_types ()

Exported as current-link-types.

Note that get_trees returns the body of a locus (the tree passed to insert_locus), not the locus tag itself; the Scheme function id->loci goes one level up. The glue also exports bare tree pointers (tree->tree-pointer, tree-pointer->tree, tree-pointer-detach), which Scheme uses to remember positions in a document across edits.

4.Mirror links

Modifications of a locus are propagated to the loci it is linked to by mirror links. Every tree pointer calls link_announce (obs, mod) when its tree is about to be modified (tree_pointer_rep::announce). For each identifier of the observer and each link of the form (link "mirror" attrs (id a) (id b)) containing that identifier, the same modification is applied to the trees of the other identifier, provided it is applicable there (is_applicable), and provided those trees are not themselves being modified (not_done, which uses busy_modifying and busy_tree). Live mirrored documents (see live documents) rely on this mechanism.

5.Observers with callbacks

A tree pointer created with a callback name (scheme_observer, that is, an (observer id callback) argument of a locus, or the buffer notifier) calls the Scheme function

(callback 'announce tree modification)
(callback 'done tree modification)
(callback 'touched tree path)

before and after each modification of its tree, and when the tree or one of its descendants is touched (touch, which propagates upwards through the ip observers together with the relative path), but only while the tree is attached to the edit tree (ip_attached). The buffer notifier installed by tm_buffer_rep::attach_notifier is such an observer, on the body of the buffer, with the buffer name as identifier and buffer-notify (in

progs/part/part-shared.scm

) as callback; it is used to share whole buffers.

6.Visited loci and locus colours

declare_visited (id) and has_been_visited (id) manage a global set of visited keys; by convention the keys are "id:" followed by an identifier or "url:" followed by a destination. The set lives for the whole session and is shared by all buffers. The colours of loci are taken from the environment variables locus-color and visited-color; the value global means that the user preference of the same name is used, as returned by get_locus_rendering (defaults #404080 and #702070). The preference locus-on-paper (change or preserve) decides whether loci keep their colour when printed.

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