GNU TeXmacsUser manualReferenceAboutEnglish
Style and document DRDs

1.Layers

The properties of a tag depend on the style: theorem is a macro with one accessible argument in article, and an unknown label in a document without style. Each DRD is therefore built in layers on top of std_drd, using the inheritance of drd_info (name, base):

  1. std_drd: built-in primitives and variables.

  2. The style DRD: the macros and parameters of the style files and packages, computed once per style tuple.

  3. The editor DRD (editor_rep::drd), which starts from the style DRD and is completed with the definitions in the initial environment and the preamble, and with those in the body of the document up to the cursor.

Within a layer, properties come from two sources: the heuristic inference from the macro definitions, and explicit drd-props declarations. The latter freeze the properties they set, so they always win, whatever the order in which both are executed.

2.The DRD of a style

bool compute_env_and_drd (tree style)
(compute and memorize environment and DRD of a style)

Defined in

Data/Document/new_style.cpp

. The argument is a style tuple such as (tuple "article" "number-europe"). The function creates drd_info drd ("none", std_drd) and an edit_env whose drd member refers to it. Then:

The results are memorized in the global style_data_rep, in the maps style_cached and drd_cached. A set of “busy” styles protects against recursion; it returns false if the style is already being computed.

drd_info get_style_drd (tree style)

hashmap<string,tree> get_style_env (tree style)
(memoized access)

Return the memorized DRD or environment, computing them if needed. For a busy style, get_style_drd returns std_drd and get_style_env an empty environment.

drd_info get_document_drd (tree doc)
(DRD for a whole document tree)

Used by tree processing routines which are not attached to an editor. It takes the style of the document, gets its DRD and, if the document has a preamble (hide-preamble or show-preamble), creates a derived drd_info ("preamble", …) in which the style and the preamble are executed and the heuristics are run again. For a tree without a TeXmacs version attribute (typically a fragment), the current the_drd is used if it looks like a real document style is loaded (the theorem tag has a syntax), and the generic style otherwise. Used, through with_drd, by the correction routines of

Data/Tree/tree_correct.cpp

and

tree_brackets.cpp

.

3.Caching

Computing the environment of a style means loading and executing all its packages, which is slow; it is therefore cached at two levels.

In memory

The structure style_data_rep contains, per style tuple, the environment and the serialized local DRD entries (style_cache, style_drd) and the computed objects (style_cached, drd_cached).

On disk

style_set_cache (style, H, t) writes the pair (tuple H t), where t is the result of get_locals (see serialization), to a file in $TEXMACS_HOME_PATH/system/cache whose name is derived from the style tuple by cache_file_name (for instance __article__number-europe__). The file is written only if it does not exist yet. style_get_cache looks in memory first and then on disk.

style_invalidate_cache discards the in-memory structure and deletes the files __* in the cache directory. It is called by tm_server_rep::style_clear_cache (glue cpp-style-clear-cache, Scheme function style-clear-cache, menu Tools\(\rightarrow\)Update\(\rightarrow\)Styles), which then notifies all editors so that they recompute their environment. style-clear-cache is also called automatically when a file with suffix .ts is saved from TeXmacs (

texmacs/texmacs/tm-files.scm

). Style files edited with another editor are not detected: the cache must then be cleared by hand.

Since only the local entries are stored, a cached style DRD is only valid with the std_drd of the TeXmacs binary which produced it. The whole cache directory is emptied when the version changes (init_upgrade), after an interrupted start-up (acquire_boot_lock) and with the command line option -setup.

4.The DRD of an editor

Each editor owns a drd_info drd, created by the constructor of editor_rep (

Edit/Editor/edit_main.cpp

) as drd_info (buf->buf->title, std_drd). Its typesetting environment env is constructed with a reference to this member, so that assignments to drd are seen by the environment, and drd-props executed during typesetting modify the editor DRD.

void edit_typeset_rep::typeset_style_use_cache (tree style)
(install the style)

On a cache hit, the cached environment is patched into env and the cached entries are installed into the editor DRD with set_locals. On a miss, the environment and DRD are computed with get_style_env and get_style_drd, the cache is filled, and drd is replaced by the style DRD object (see the pitfalls for the consequences of this sharing).

void edit_typeset_rep::typeset_preamble ()
(environment and DRD at the start of the document)

Writes the default environment, installs the style, applies the initial environment of the document, reads back the resulting environment pre and runs drd->heuristic_init (pre). It is called lazily when the initial environment is first needed, before printing, from the conversion routines exec_html and exec_latex, and by typeset_invalidate_all, which is triggered by notify_change (THE_ENVIRONMENT) after any change of style, package or initial environment.

void edit_typeset_rep::drd_update ()
(take the document body into account)

Computes the environment just before the cursor (typeset_exec_until (tp)) and runs the heuristics on it, so that macros defined in the body of the document (before the cursor) get their properties. It is called at the end of edit_interface_rep::apply_changes, unless there are pending events (gui_interrupted); this is the “small amount of free time” mentioned in the user documentation.

Note that set_locals adds or overwrites entries but never removes any, and heuristic_init likewise only updates the properties of the variables present in its argument. The editor DRD can therefore retain properties of macros which are no longer defined (for instance after the deletion of a definition in the preamble, or after a style change on the cache-hit path), until the buffer is reopened.

5.The heuristics in brief

drd_info_rep::heuristic_init (env) iterates over all variables of an environment and dispatches on the value:

macro

heuristic_init_macro: fixed arity equal to the number of parameters (CHILD_DETAILED), type of the tag = type of the body, with-like flag if the body is a chain of with-like constructs ending in the last argument, child names = parameter names, and for each parameter the result of a search for (arg x) in the body (arg_access), which determines whether the argument is accessible, its type, and its environment.

xmacro

heuristic_init_xmacro: ARITY_REPEAT with a minimal arity derived from the largest index used.

anything else

heuristic_init_parameter: arity zero, VAR_PARAMETER, and a type guessed from the name or the value. Zero-parameter macros whose body is a length or a localize become VAR_MACRO_PARAMETER.

Because the properties of a macro depend on those of the macros it uses (accessibility propagates through with, compound, nested macro calls and the child environments of primitives), the loop is repeated until no record changes, and abandoned with the warning bad heuristic drd convergence after ten rounds. The details, with examples, are in the macro expansion chapter.

The heuristics only use the DRD itself and the macro definitions; they never look at the boxes. A macro which uses an argument in a way the heuristics do not understand (inside extern, merge, a map-args of an inaccessible tag, …) gets an inaccessible argument, and a drd-props declaration is needed.

6.Explicit declarations: drd-props

drd-props is evaluated by edit_env_rep::exec_drd_props (

Typeset/Env/env_exec.cpp

) whenever the tree is executed: when a style is loaded, when the preamble is processed, or when a drd-props in the body of a document is typeset (concater_rep::typeset_drd_props, which also displays a flag). Its arguments are not evaluated. The first argument is the tag name (possibly a pseudo-label extern:f), followed by pairs of a property and a value:

Property Value Effect
arity \(n\), or <tuple|repeat|a|b>, repeat*, options set_arity with CHILD_DETAILED for a number, CHILD_BIFORM for a tuple; frozen
name string name attribute (not frozen)
syntax tree syntax attribute
border yes, inner, outer, no border mode; frozen
with-like yes, no with-like flag; frozen
locals attr tree environment of all child records; frozen
accessible, hidden, unaccessible index, all, none accessibility; frozen (none always means never accessible)
normal-writability, disable-writability, enable-writability index, all writability; frozen
returns type name type of the tag; frozen
parameter, macro-parameter type name variable kind and type; frozen
a type name index, all type of children; frozen

Table 1. Properties recognized by drd-props.

Unknown properties and values are silently ignored. Some consequences of the implementation are worth knowing:

Typical declarations in the standard style packages:

<drd-props|explain-macro|arity|<tuple|repeat|1|1>|accessible|all>

<drd-props|help-link|arity|2|accessible|0|url|1>

<drd-props|phantom|arity|1|accessible|none|syntax|<macro|body|>>

<drd-props|enunciation-sep|macro-parameter|string>

7.The current DRD and the access modes

7.1.the_drd

Most generic routines on trees (Data/Tree/,

System/Language/

, the converters) do not have access to an editor, and consult the global variable the_drd instead. It is set:

Code inside the editor uses the member drd directly, and the typesetter uses env->drd; outside the editor, the_drd is the only way. When writing a new routine which may be called on a tree not belonging to the current buffer (for instance on a document being converted), wrap it in with_drd with get_document_drd.

7.2.Access and writable modes

Whether a child is “accessible” depends not only on the DRD but also on a global access mode (

Data/Drd/drd_mode.hpp

):

DRD_ACCESS_NORMAL

The default: only ACCESSIBLE_ALWAYS children are accessible.

DRD_ACCESS_HIDDEN

Hidden children are accessible too. Used by search (edit_replace_rep::next_match), so that matches in folded content are found.

DRD_ACCESS_SOURCE

Every child is accessible. Used in source mode (when the mode of the document is src: make_cursor_accessible, selection_correct, compute_env_rects), for children whose environment has mode=src (is_accessible_cursor), inside inactive markup (is_modified_accessible), and by compute_selection and update_mouse_loci.

set_access_mode (mode) returns the previous mode, and all callers follow the pattern

int old_mode= set_access_mode (DRD_ACCESS_SOURCE);
…
set_access_mode (old_mode);

The writable mode (DRD_WRITABLE_NORMAL, DRD_WRITABLE_INPUT, DRD_WRITABLE_ANY, functions set_writable_mode and get_writable_mode) is maintained in the same way by is_accessible_cursor while it descends into children with disabled or enabled writability; see the data model. From Scheme, the access mode is available as get-access-mode and set-access-mode (with the integer values \(0\), \(1\), \(2\)), as in tree-perform-search (

generic/search-widgets.scm

).

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