GNU TeXmacsUser manualReferenceAboutEnglish
The DRD data model

1.The class drd_info

class drd_info_rep
(a data relation descriptor)

Declared in

Data/Drd/drd_info.hpp

, with three data members:

string name

A name used only for printing ("tm" for the standard DRD, "none" for style DRDs, the buffer title for editor DRDs, "preamble" for document DRDs).

rel_hashmap<tree_label,tag_info> info

The properties of each tag.

hashmap<string,tree> env

The environment (typically, all macro definitions of the style) from which the heuristic properties were computed, set by set_environment. It is also used as a fallback by get_syntax.

drd_info is the usual reference counted handle (CONCRETE), so copying a drd_info shares the object.

drd_info (string name)

drd_info (string name, drd_info base)
(constructors)

The second constructor creates a DRD which inherits from base. The member info is a relative hash map (

Kernel/Containers/rel_hashmap.hpp

): a list of hash maps in which a lookup info[l] returns the entry of the first map that contains l, and an access for writing info(l) first copies the entry from the base into the local map. Hence modifications never leak into the base DRD. All DRDs are directly or indirectly built on top of std_drd.

For a label which is not described anywhere in the chain, info[l] returns the default value of the hash map, tag_info (): arity zero (ARITY_NORMAL), no child records, type TYPE_REGULAR, border BORDER_YES. Code which needs to know whether a label is described at all uses drd_info_rep::contains (string l), which looks through the whole chain. For instance, correct_node (

Data/Tree/tree_modify.cpp

) and edit_dynamic_rep::insert_argument only enforce arities of described labels.

All setters follow the same pattern: return immediately if the property is frozen (see below), make a local copy of the record if necessary, and modify it. Setters for children take a physical index nr into the array of child records (see the child layout), and silently do nothing if nr is out of range. The high-level queries (get_type_child, is_accessible_child, get_writability_child, get_env_child, get_child_name) take a tree and a logical child number instead, and do the translation themselves.

2.The class tag_info

class tag_info_rep
(the properties of one tag)

A tag_info (

Data/Drd/tag_info.hpp

) consists of

parent_info pi

the properties of the tag itself;

array<child_info> ci

the properties of the children, in a compressed layout;

tree extra

either the empty string, or an attr tree with further named attributes (names and syntax).

tag_info (int arity, int extra, int am, int cm, bool frozen)
(create a record)

Creates a record with the given arity fields, arity mode am and child mode cm. The array ci receives \(0\) entries if arity+extra is zero, one entry for CHILD_UNIFORM, two for CHILD_BIFORM and arity+extra for CHILD_DETAILED. With frozen set, the arity, border, block, with-like and var-type properties and all child properties are frozen (but see the pitfalls about freeze_type).

The member functions inner_border, outer_border, with_like, var_parameter, var_macro_parameter, type, accessible, hidden, disable_writable, enable_writable, locals, name and long_name modify the record and return it again as a tag_info, so that they can be chained in the declarative style of

drd_std.cpp

:

fixed (2) -> name ("fraction") ->
  accessible (0) -> locals (0, "math-display", "false")

Note that accessible (i) and hidden (i) also set the type of the child to TYPE_REGULAR, so a type declaration must come after them to take effect.

3.Properties of the tag (parent_info)

parent_info is a 32-bit bit field. In the order of the fields (which is also the order of the bits in the serialized form, starting from the least significant bit):

Field Bits Values
type 5 TYPE_*
arity_mode 2 ARITY_NORMAL, ARITY_OPTIONS, ARITY_REPEAT, ARITY_VAR_REPEAT
arity_base 6 0–63
arity_extra 4 0–15
child_mode 2 CHILD_UNIFORM, CHILD_BIFORM, CHILD_DETAILED
border_mode 2 BORDER_YES, BORDER_INNER, BORDER_OUTER, BORDER_NO
block 2 BLOCK_NO, BLOCK_YES, BLOCK_OR
with_like 1 boolean
var_type 2 VAR_MACRO, VAR_PARAMETER, VAR_MACRO_PARAMETER
freeze_type, freeze_arity, freeze_border, freeze_block, freeze_with, freeze_var_type 6×1 booleans

Table 1. The fields of parent_info.

3.1.Type

The field type is the type of the value of the tag: regular content for most tags, but TYPE_NUMERIC for plus, TYPE_LENGTH for cm-length, TYPE_GRAPHICAL for line, TYPE_COLOR for rgb-color, and so on. Getters: get_type (tree_label) and get_type (tree); setter set_type (tree_label, int). The type of a variable (see var_type) is the type of its value. The list of types is given below.

3.2.Arity

The three fields arity_mode, arity_base and arity_extra describe the set of admissible arities \(n\):

ARITY_NORMAL

\(n =\operatorname{base}+\operatorname{extra}\) (fixed arity).

ARITY_OPTIONS

\(\operatorname{base} \leqslant n \leqslant \operatorname{base}+\operatorname{extra}\) (optional trailing arguments; the comment in

tag_info.hpp

says \(<\) but the code uses \(\leqslant\)).

ARITY_REPEAT

\(n =\operatorname{base}+ k \cdot \operatorname{extra}\) for \(k \geqslant 0\): a fixed prefix followed by repeated groups of \(\operatorname{extra}\) children.

ARITY_VAR_REPEAT

As ARITY_REPEAT, but the repeated groups come first and the \(\operatorname{base}\) fixed children come last, as in with (pairs of variables and values, followed by the body).

For ARITY_VAR_REPEAT, set_arity (l, arity, extra, am, cm) stores its arguments swapped (arity_base= extra, arity_extra= arity), so that arity is the length of the repeated group and extra the number of trailing children, as for the helper var_repeat of

drd_std.cpp

. The derived queries are:

int get_minimal_arity (tree_label l)

int get_maximal_arity (tree_label l)
(bounds)

\(\operatorname{base}+\operatorname{extra}\) for a fixed arity; \(\operatorname{base}\) otherwise for the minimum; \(\operatorname{base}+\operatorname{extra}\) for ARITY_OPTIONS and 0x7fffffff for the two repeat modes for the maximum.

bool correct_arity (tree_label l, int n)
(is \(n\) admissible?)

Used to validate trees (drd_correct, correct_node) and to find the arity of a new tag (edit_dynamic_rep::make_compound takes the smallest admissible positive arity, or zero for fixed arity \(0\)).

bool insert_point (tree_label l, int i, int n)
(may children be inserted at position \(i\)?)

For a tag of arity \(n\): never for a fixed arity; for ARITY_OPTIONS at any position \(\geqslant \operatorname{base}\) as long as \(n <\operatorname{base}+\operatorname{extra}\); for ARITY_REPEAT at positions inside the prefix or at the start of a group; for ARITY_VAR_REPEAT at the start of a group or inside the trailing part. Used by insert_argument and remove_empty_argument (

Edit/Modify/edit_dynamic.cpp

) to implement structured insertion of arguments.

bool is_dynamic (tree t, bool hack= true)
(does the tag have a variable arity?)

True if the arity mode is not ARITY_NORMAL, except for document, para, concat, table and row. With hack set, all non-primitive tags are considered dynamic (marked FIXME in the source). The editor (edit_dynamic_rep::find_dynamic) uses the default, the glue tree-is-dynamic? passes false.

get_old_arity returns the arity for a fixed arity and \(- 1\) otherwise; get_arity_mode, get_arity_base, get_arity_extra, get_child_mode give direct access to the fields, and get_nr_indices returns the number of child records.

3.3.Child layout

The field child_mode determines how a logical child number \(i\) of a tree with \(n\) children is mapped to an index into the array ci; this is implemented by tag_info_rep::get_index (int child, int n):

CHILD_UNIFORM

All children share ci[0].

CHILD_BIFORM

Two records. For all arity modes except ARITY_VAR_REPEAT, ci[0] describes the first \(\operatorname{base}\) children and ci[1] the others; for ARITY_VAR_REPEAT, ci[0] describes the repeated part and ci[1] the \(\operatorname{base}\) trailing children. So for with, ci[0] is the record of the variable/value pairs and ci[1] that of the body.

CHILD_DETAILED

One record per child of the “period”: for fixed and optional arities, \(i\) itself; for ARITY_REPEAT, the prefix children have their own records and the children of a group cycle through the next \(\operatorname{extra}\) records; for ARITY_VAR_REPEAT the group comes first.

If the computed index falls outside ci (for instance for a tree with more children than its declared arity), the high-level queries answer conservatively: not accessible (except in source mode), type TYPE_INVALID, writability WRITABILITY_DISABLE, empty name, and an empty environment "" from get_env_child.

3.4.Border

The field border_mode says whether the cursor may stand at the boundaries of the tag. It is interpreted bitwise (BORDER_NO is BORDER_INNER|BORDER_OUTER):

bool is_child_enforcing (tree t)
(bit BORDER_INNER set and N(t)>0)

The cursor may not be positioned on the tag itself (paths ending with 0 or 1 relative to t); it has to go inside a child. This is the case for document, concat, table, row, cell, hidden, …

bool is_parent_enforcing (tree t)
(bit BORDER_OUTER set and N(t)>0)

The cursor may not be positioned at the start of the first accessible child or the end of the last one; the positions outside the tag are used instead (see is_accessible_cursor in

Data/Tree/tree_cursor.cpp

).

var_without_border (tree_label l) is true for non-primitive tags with the BORDER_INNER bit; the editor treats such macros like concat when selecting (edit_select_rep::select_enlarge in

Edit/Replace/edit_select.cpp

, selection_adjust_border in

Data/Tree/tree_select.cpp

) and when removing structure (edit_text_rep::remove_structure_upwards in

Edit/Modify/edit_delete.cpp

). The heuristics never set the border; it is only changed by drd_std.cpp and by drd-props.

3.5.Block, with-like and variable type

block

Meant to tell whether the tag is block or inline content (BLOCK_OR: a block if one of its children which admit both is a block). The getters and setters are declared in

drd_info.hpp

but not implemented, and no code reads the field: it is currently unused.

with_like

The tag only modifies the environment of its last child, like with, with-package or a macro such as strong. Queried by is_with_like (tree t) (which also requires N(t)>0); used by the correction routines of

Data/Tree/tree_analyze.cpp

and

tree_correct.cpp

(with-correct and friends), by the heuristics (heuristic_with_like) and by make_compound, which delegates the insertion of with-like tags to the Scheme routine with-like-check-insert.

var_type

VAR_MACRO for tags (the default), VAR_PARAMETER for environment variables and VAR_MACRO_PARAMETER for macros which are used as parameters (a zero-argument macro holding a length or a localized string). The free functions is_macro and is_parameter (

Data/Tree/tree_traverse.cpp

, glue tree-label-macro? and tree-label-parameter?) test var_type != VAR_PARAMETER, resp. var_type != VAR_MACRO; they are used to build the focus menus of style parameters in

generic/generic-menu.scm

.

3.6.Freezing

Each property has a freeze_* bit. A frozen property is never changed by the corresponding set_* method, which simply returns. The methods freeze_type, freeze_arity, freeze_border, freeze_with_like, freeze_var_type (for the tag) and freeze_type, freeze_accessible, freeze_writability, freeze_env (for a child) set them. Freezing is how explicit declarations win over heuristics: drd-props freezes every property it sets, and init_std_drd freezes the arity and border of all primitives, so that a style cannot accidentally change the arity of frac by defining a macro frac.

4.Properties of the children (child_info)

child_info is also a bit field:

Field Bits Values (default first)
type 5 TYPE_ADHOC, other TYPE_*
accessible 2 ACCESSIBLE_NEVER, ACCESSIBLE_HIDDEN, ACCESSIBLE_ALWAYS
writability 2 WRITABILITY_NORMAL, WRITABILITY_DISABLE, WRITABILITY_ENABLE
block 2 BLOCK_REQUIRE_BLOCK, BLOCK_REQUIRE_INLINE, BLOCK_REQUIRE_NONE
env 16 index in the environment table
freeze_type, freeze_accessible, freeze_writability, freeze_block, freeze_env 5×1 booleans

Table 2. The fields of child_info.

4.1.Accessibility

accessible says whether the cursor may enter the child. ACCESSIBLE_ALWAYS children are ordinary editable content (the numerator of a fraction, the body of a with); ACCESSIBLE_NEVER children are only editable in source mode or through the focus bar (the URL of a hyperlink, the width of an image); ACCESSIBLE_HIDDEN children are accessible only when the global access mode allows hidden content (the body of hidden, which search must still traverse). The decision is taken by

bool is_accessible_child (tree t, int i)
(may the cursor enter t[i]?)

Translates i with get_index and compares the stored value with the global access mode (

Data/Drd/drd_mode.hpp

): DRD_ACCESS_NORMAL accepts only ACCESSIBLE_ALWAYS, DRD_ACCESS_HIDDEN also ACCESSIBLE_HIDDEN, and DRD_ACCESS_SOURCE accepts every existing child. For an extern tree whose first child is a string f, the record of the pseudo-label extern:f is used, which allows style files to declare the accessibility of the arguments of individual Scheme functions with drd-props (see

packages/customize/math/math-check.ts

).

Related queries: is_accessible_path (t, p) (all children along a path), all_accessible (l) (all records are ACCESSIBLE_ALWAYS, and there is at least one) and none_accessible (l). The low-level get_accessible (l, nr) and set_accessible (l, nr, mode) work on physical indices. How the access modes are switched is explained in style and document DRDs.

The accessibility in the DRD must agree with the boxes produced by the typesetter: a child declared accessible but typeset as a decoration (or conversely) leads to erratic cursor movement. See the macro expansion chapter for details.

4.2.Writability

writability implements read-only regions: WRITABILITY_DISABLE makes the whole subtree of the child read-only, except for descendants whose writability is re-enabled with WRITABILITY_ENABLE. The routine is_accessible_cursor (

Data/Tree/tree_cursor.cpp

) switches the global writable mode (set_writable_mode) to DRD_WRITABLE_INPUT when it enters a disabled child and back to DRD_WRITABLE_NORMAL when it enters an enabled one; in DRD_WRITABLE_INPUT mode no cursor position in a string is accessible (outside source mode). The macros disable-writability and enable-writability of

packages/gui/gui-widget.ts

are declared this way. Query: get_writability_child (t, i).

4.3.Block requirements

block (BLOCK_REQUIRE_*) was meant to say whether a child must contain block or inline content. Like the corresponding parent field it is never set nor read. Note that its numerical default is \(0\), i.e. BLOCK_REQUIRE_BLOCK.

4.4.Environment of a child

env describes how the environment changes when going from the tag into the child, for instance mode=math for the body of equation or math-display=false for scripts. The value is an attr tree (attr var-1 val-1 … var-n val-n) (the default is the empty with tree, meaning no change). To keep child_info small, these trees are stored in a global table and the record contains only their index:

int drd_encode (tree t)

tree drd_decode (int i)
(global environment table)

drd_encode returns the index of t in a global table, adding it if necessary (the table is never shrunk; more than \(2^{16}\) different environments trigger an assertion).

A value (arg i) with an integer i refers to the \(i\)-th child of the same tree: the heuristics produce such values when an argument is used in a with around another argument, for example for

<assign|my-code|<macro|lang|body|<with|prog-language|lang|body>>>

the environment of child \(1\) is (attr prog-language (arg 0)).

tree get_env_child (tree t, int i, tree env)
(environment inside t[i])

Returns env (an attr tree) updated with the changes for child i: for with the variables of the tree itself are merged in; otherwise the stored environment is decoded, references (arg j) are replaced by copies of t[j], and the result is merged with drd_env_merge. Values are not evaluated: the caller decides whether to evaluate them (the editor does so in typeset_exec_until, most other callers only compare strings). The result is the empty string if i is outside the layout.

get_env_child (t, i, var, val) returns the value of a single variable (with default val), and get_env_descendant (t, p, env) and get_env_descendant (t, p, var, val) follow a whole path. The free functions drd_env_write, drd_env_merge and drd_env_read manipulate the attr trees (drd_env_write keeps the variables sorted). The low-level access is get_env (l, nr), set_env (l, nr, env) and freeze_env (l, nr).

5.Named attributes

The tree extra stores further attributes as an attr list, through set_attribute and get_attribute:

name, long-name

The name of the tag shown to the user (footer, focus bar, menus). get_name (l) defaults to the label itself and get_long_name (l) to the name.

name-i, long-name-i

The names of the children, by physical index. The heuristics use the parameter names of the macro. Queried by get_child_name (t, i) and get_child_long_name (t, i), which translate the logical index.

syntax

A tree describing how the tag should be understood by the mathematical parser (packrat grammars). It may be a symbol (<int>) or a macro which is applied to the arguments of the tag. get_syntax (l) falls back on the definition of the tag in env, i.e. a macro is by default “parsed as its expansion”; get_syntax (tree t, path p) performs the substitution of the arguments (wrapping them in quasi trees with their paths if p is given). Consumers:

System/Language/packrat_serializer.cpp

and symbol_type in

Data/Tree/tree_analyze.cpp

.

6.Types

The constants TYPE_* are used both for the value of a tag and for its children. drd_decode_type and drd_encode_type convert between the constants and the names used by drd-props and by the glue (tree-child-type, tree-label-type).

TYPE_REGULAR (regular)

Ordinary content.

TYPE_ADHOC (adhoc)

Content without a well-defined type; the default for children.

TYPE_RAW

Raw binary data (child of raw-data); has no name, so drd_decode_type returns unknown.

TYPE_VARIABLE (variable), TYPE_ARGUMENT (argument)

Names of environment variables and macro arguments.

TYPE_BINDING

Alternating variables and values, as in with and attr. get_type_child resolves it to TYPE_VARIABLE for even and TYPE_REGULAR for odd children; it has no name.

TYPE_BOOLEAN, TYPE_INTEGER, TYPE_STRING, TYPE_LENGTH, TYPE_NUMERIC

Scalars (boolean, integer, string, length, numeric).

TYPE_CODE, TYPE_IDENTIFIER, TYPE_URL, TYPE_COLOR

Program code, identifiers (labels, keys), URLs and colors (code, identifier, url, color).

TYPE_GRAPHICAL, TYPE_POINT, TYPE_CONSTRAINT, TYPE_GRAPHICAL_ID

Graphical objects, points, constraints and graphical identifiers.

TYPE_EFFECT, TYPE_ANIMATION, TYPE_DURATION, TYPE_FONT_SIZE

Graphical effects, animations, durations and font sizes.

TYPE_OBSOLETE, TYPE_UNKNOWN, TYPE_ERROR

Special values; TYPE_UNKNOWN is the initial value used by the heuristics. TYPE_INVALID (\(- 1\)) is returned by get_type_child for a child outside the layout and is never stored.

The child types are used for syntax coloring in source mode (highlight in

Typeset/Env/env_inactive.cpp

maps them to src-regular, src-var, src-length, …), by the focus bar to choose an input field for inaccessible children (type->format in

generic/generic-menu.scm

), by the correction routine is_correctable_child, and by the heuristics to infer the types of macro arguments.

7.Serialization

A DRD can be converted to a tree and back, which is how style DRDs are stored in the disk cache:

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