![]() |
The data relation descriptor | ![]() |
A macro definition says how a tag is rendered, but the editor needs other information about tags: how many arguments they take, which arguments may contain the cursor, what kind of content (text, length, color, …) each argument holds, which environment variables are changed inside an argument (so that, for instance, the editor knows that the body of equation is in math mode), whether a tag is only an environment modifier, and so on. This information is stored in the data relation descriptor or DRD; see also the user-level description in data relation descriptions and the primitive drd-props in macro primitives.
The code lives in
Data/Drd/
. The DRD of built-in tags is hard-coded in
drd_std.cpp
; the DRD of user tags is mostly inferred from their macro definitions, and can be refined or frozen by drd-props declarations in style files.
This page concentrates on the interplay between macros and the DRD; the complete reference for the DRD subsystem, on the C++ and on the Scheme side, is the chapter the data relation descriptor (DRD).
Its main member is rel_hashmap<tree_label,tag_info> info, a relative hash map: a DRD created with the constructor drd_info (string name, drd_info base) only stores the entries which differ from those of base, and looks up the others in base. All DRDs of documents and styles are built on top of the standard DRD std_drd. The member env holds the environment (as set by set_environment) from which the heuristics were computed. get_locals and set_locals convert the local entries to and from a tree, which is how DRDs are stored in the style cache.
Consists of a parent_info pi with properties of the tag itself, an array array<child_info> ci with properties of its children, and a tree extra for miscellaneous attributes (names, syntax). Both structures are bit fields (
Data/Drd/tag_info.hpp
):
type (the type of the value of the tag, one of the TYPE_* constants), arity_mode (ARITY_NORMAL, ARITY_OPTIONS, ARITY_REPEAT, ARITY_VAR_REPEAT), arity_base, arity_extra, child_mode (CHILD_UNIFORM, CHILD_BIFORM, CHILD_DETAILED: whether one child_info is shared by all children, two are used, or one per child), border_mode, block, with_like, var_type (VAR_MACRO, VAR_PARAMETER, VAR_MACRO_PARAMETER), and one freeze_* bit per property.
type, accessible (ACCESSIBLE_NEVER, ACCESSIBLE_HIDDEN, ACCESSIBLE_ALWAYS), writability, block, env (an index into a global table of environment trees, see drd_encode and drd_decode in
tag_info.cpp
) and the corresponding freeze_* bits.
The freeze_* bits are the key to the interplay between heuristics and explicit declarations: every set_* method returns immediately if the property is frozen, and drd-props freezes each property it sets. Hence explicit declarations always win over the heuristics, whatever the order in which they are executed.
The global variable the_drd (
drd_std.hpp
) points to the DRD of the current buffer; it is set by the editor and window code, and can be changed temporarily with the helper struct with_drd. The environment instead uses the DRD of its own buffer, through the reference edit_env_rep::drd.
drd_info_rep::is_accessible_child (tree t, int i) decides whether the cursor may enter the i-th child of t. The answer depends on the global access mode (
drd_mode.hpp
): DRD_ACCESS_NORMAL only accepts ACCESSIBLE_ALWAYS, DRD_ACCESS_HIDDEN also accepts ACCESSIBLE_HIDDEN, and DRD_ACCESS_SOURCE (used in source mode) makes every child accessible. For extern trees, the properties are looked up under the pseudo label extern:f, where f is the name of the Scheme function, so that drd-props can declare the accessibility of the arguments of individual Scheme macros.
Accessibility in the DRD and accessibility of boxes (inverse paths without negative entries) are two different things which should agree: the routines which move the cursor through the tree consult the DRD, whereas the boxes determine where the cursor can actually be displayed. A child which the DRD declares accessible but which the macro typesets as a decoration (because it computes with it), or conversely, leads to inconsistent cursor behaviour.
Iterates over all variables of env, calling heuristic_init_macro for macro values, heuristic_init_xmacro for xmacro values and heuristic_init_parameter otherwise. Each of these returns whether it changed the tag_info. Since the properties of a macro depend on those of the macros it uses, the whole loop is repeated until nothing changes; after ten rounds it gives up with the warning bad heuristic drd convergence.
heuristic_init_macro (string var, tree macro) does the following for a macro with n parameters:
it sets the arity to n (ARITY_NORMAL, CHILD_DETAILED);
a macro without parameters whose body is a length, or a localize of a string, is marked as a macro parameter (VAR_MACRO_PARAMETER) of type length, respectively string; otherwise the type of the tag is the type of the body;
it sets the with-like flag if the body is a chain of with-like constructs ending in the last argument (heuristic_with_like);
the parameter names become the child names;
for each parameter x, it calls arg_access (body, (arg x), (attr), type, found), which searches the body for occurrences of (arg x). The search descends into the children which are accessible according to the current DRD, accumulating the environment changes made on the way (with, tformat, the child environments of other tags as given by get_env_child). compound is treated as a call of the named tag, if is looked into through its first branch, and a map-args gives access if the mapped tag gives access to its first child. The search does not look inside nested macro definitions. If an accessible occurrence is found, the child is declared ACCESSIBLE_ALWAYS and the accumulated environment is stored as the environment of the child (with references to other arguments rewritten into argument numbers by rewrite_symbolic_arguments). The type of the child is taken from the context in which the argument occurs.
For instance, for a definition (assign "my-math" (macro "body" (with "mode" "math" (arg "body")))) the heuristics find that the argument is accessible, that it is in math mode, and that my-math is with-like. Arguments which only occur inside computations (such as merge, or extern with default DRD) are not accessible, which matches the fact that they are typeset as decorations.
heuristic_init_xmacro computes a minimal arity from the largest index i used in (arg x i) or map-args and declares a repeated arity (ARITY_REPEAT). heuristic_init_parameter declares a variable of arity zero and guesses its type from its name (color, *-color, *-length, *-width) or from its value (booleans, integers, numbers, lengths).
The heuristics are run at two moments: on the environment produced by the style (in compute_env_and_drd, see the typesetting environment), and on the initial environment of the document (typeset_preamble). edit_typeset_rep::drd_update runs them once more on the environment at the cursor, which takes into account macros defined in the body of the document.
edit_env_rep::exec_drd_props interprets (drd-props tag prop1 val1 …). Each recognized property is set and frozen in drd:
A number, or a tuple repeat, repeat* or options with two numbers.
Attributes used by the user interface.
yes, inner, outer or no.
yes or no.
An environment for all children.
With a child number, all or none.
With a child number or all.
A type name, for the tag itself.
(as accepted by drd_encode_type, such as regular, length, boolean) with a child number or all: the type of children.
Note that drd-props acts on the DRD passed to the environment at the moment it is executed: in a style file, this is the DRD being built by compute_env_and_drd, which is then cached along with the environment.