![]() |
Creating and following links from Scheme | ![]() |
The Scheme side of the linking system lives in
progs/link/
. Most of it is loaded lazily (
init-texmacs.scm
declares link-follow-ids, link-active-ids, link-mouse-ids, link-active-upwards, get-link-locations, register-link-locations and a few others with lazy-define), so the first event over an active locus loads the navigation code.
locus-edit.scm)
Returns a new identifier +xxxyyy, made of a random prefix chosen at startup and a counter, both in base 62. The identifiers are unique with high probability across sessions and users, which is what makes links between files possible.
Wraps the selection (or an empty string) in (locus (id new-id) …).
The identifier of the locus t (its first argument), or #f.
The locus tags carrying id (the parents of the trees returned by id->trees).
Replaces the bodies of all loci with identifier id by t.
Insert a link as an argument of a locus (just before the body) or remove it.
link-edit.scm)
Links are built in two steps, through the Link menu (
link-menu.scm
) and the keyboard shortcuts (
link-kbd.scm
) of the linking tool, which are only present when with-linking-tool? holds. First the participants are collected in the table link-participants, indexed by their position: (link-set-locus nr) stores a tree pointer to the innermost locus, link-set-url, link-set-target-url, link-set-script and link-set-target-script store url and script vertices. Then (make-link types) builds one link (link type v0 v1 …) per comma separated type and inserts it according to the link mode (set-link-mode):
in the source locus only;
in every locus taking part in the link (the default);
in the locus containing the cursor, which need not be a participant.
remove-link-of-types and remove-all-links remove the links of the current locus, taking the mode into account; in bidirectional mode they are meant to remove the copies stored in the other loci as well (but see pitfalls). The utilities link-flatten, link-type, link-attributes, link-vertices, vertex->id, vertex->url and vertex->script take links apart.
link-navigate.scm)
Following a link goes through two intermediate representations.
The lists are filtered (filter-link-list) on three criteria:
unless bidirectional navigation is on, only links whose first vertex is the locus itself are kept;
the type must be allowed (Link\(\rightarrow\)Active link types, stored in navigation-blocked-types);
the type must match the event: for "click", every type except focus and mouse-over; for "hover", every type; for "focus" and "mouse-over", only links of that type.
The navigation options are the preferences bidirectional navigation (default off), external navigation (on) and link pages (on). link-active-ids keeps the identifiers which have a link passing the "hover" filter; this is how the editor decides which loci are active.
ignores links of type automatic when there are links of other types;
follows all focus jumps at once;
otherwise, if there are several jumps and link pages are enabled, opens an auxiliary page listing them (build-navigation-page);
otherwise follows the single jump, or asks the user which extended type to follow.
Following a jump (navigation-item-follow) marks the source and the target as visited (declare-visited, then id-update retypesets the affected loci so that they change colour; hard identifiers starting with % are not recorded) and goes to the target vertex:
go-to-id puts the cursor at the end of the first locus with that identifier. If none is known, it tries to load the file containing it (resolve-id, see below) and tries again a little later.
go-to-url chooses a pair of handlers according to the root of the URL (url-handlers). In all cases the file part is opened with load-browse-buffer (for local files, a disambiguation page is built when the URL has several alternatives). For local files, the part after # or ? is then handled by a post handler according to the file format: go-to-label for TeXmacs documents, line and column positioning for source files; for http, https and doi it is ignored.
execute-script runs the script, subject to the security policy (see scripts and security), using the secure attribute recorded by the typesetter.
locus-link-follow (link return in the linking tool) follows the links of the loci around the cursor as a click would.
link-extern.scm)
A link may point to a locus in another file, which is not necessarily loaded. To find it, TeXmacs keeps a registry in $TEXMACS_HOME_PATH/system/registry.scm, which assigns a unique identifier to each linked file name (registry-id) and maps identifiers of files back to their names, and stores the necessary information in the documents themselves:
When a buffer is saved, buffer_export (
Texmacs/Data/new_buffer.cpp
) calls (get-link-locations name body) and stores the result as a links attribute of the document. For every link in the body whose vertices are loci located in other files (found among the open buffers or through the registry), it lists the identifier of the current file, the other files (target entries, with relative names) and, for each external locus, the file which contains it (locator entries).
When a document is loaded, import_loaded_tree passes the links attribute to (register-link-locations url links), which adds the entries to the registry and to the table of locators.
(resolve-id id) uses this information to load a file which contains the locus id, at most once per file and session.
get-constellation lists all registered files;
link-extract.scm
builds auxiliary pages listing the linked files (build-constellation-page), the loci of the current buffer (build-locus-page), or all environments of a given type, after turning them into loci if necessary (build-environment-page).