![]() |
The standard DRD and new primitives | ![]() |
drd_std.cpp
The file
Data/Drd/drd_std.cpp
defines three global objects (declared in
drd_std.hpp
):
The standard DRD, which describes every built-in primitive and the built-in environment variables. Every other DRD inherits from it.
The DRD of the current view, initially equal to std_drd; see style and document DRDs.
The table from the names of the primitives to their tree labels. The function std_contains (s) tests whether s is the name of a primitive.
The function init_std_drd fills these objects. It is called once during start-up (init_texmacs in
System/Boot/init_texmacs.cpp
) and, defensively, by get_style_drd; a static flag makes further calls no-ops.
Besides describing the primitives, init_std_drd has a second, less obvious, role: it is the place where the members of the enumeration tree_label (
Kernel/Types/tree_label.hpp
) receive their string names. Each declaration
static void
init (tree_label l, string name, tag_info ti) {
STD_CODE(name)= (int) l;
make_tree_label (l, name);
std_drd->info (l)= ti;
std_drd->freeze_arity (l);
std_drd->freeze_border (l);
} |
registers the name in the tables of
tree_label.cpp
(so that as_string (FRAC) is "frac" and as_tree_label ("frac") is FRAC), records it in STD_CODE, installs the tag_info, and freezes the arity and border. Since STD_CODE is used by the readers of documents (tm_reader in
Data/Convert/Texmacs/fromtm.cpp
, get_codes in
upgradetm.cpp
, scheme_tree_to_tree in
Data/Convert/Scheme/from_scheme.cpp
), a label which is not declared here is not recognized as a primitive when a document is loaded. At the time of writing every member of the enumeration has a declaration; two further labels, shown and ignore, are not in the enumeration but are created by make_tree_label and declared as primitives in the same way.
Environment variables are declared with
static void
init_var (string var, int tp, string vname= "") {
tree_label l= make_tree_label (var);
tag_info ti= fixed (0) -> var_parameter () -> type (tp);
if (vname != "") ti= ti->name (vname);
std_drd->info (l)= ti;
std_drd->freeze_arity (l);
std_drd->freeze_border (l);
} |
which gives the variable a VAR_PARAMETER record with a type, for instance init_var (FONT_SIZE, TYPE_FONT_SIZE) or init_var (PAGE_ODD_HEADER, TYPE_REGULAR, "odd page header"). The variable names are the string constants of
Data/Drd/vars.hpp
. Variables which are not declared here receive their type from the heuristics (see style and document DRDs).
The declarations are written in a small embedded language of helper functions and macros, defined at the top of
drd_std.cpp
. The arity is given by one of four constructors, which all create frozen records:
fixed (int arity, int extra=0, int child_mode= CHILD_UNIFORM)
options (int arity, int extra, int child_mode= CHILD_UNIFORM)
repeat (int arity, int extra, int child_mode= CHILD_UNIFORM)
Create a tag_info with arity mode ARITY_NORMAL, ARITY_OPTIONS, ARITY_REPEAT or ARITY_VAR_REPEAT. For fixed, arity+extra is the arity; the split only matters for the child layout (fixed (1, 1, BIFORM) is a tag with two children having two different records). For options (a, x), the arity ranges from \(a\) to \(a + x\). For repeat (a, x), there are \(a\) fixed children followed by any number of groups of \(x\) children. For var_repeat (a, x), groups of \(a\) children are followed by \(x\) fixed children (the arguments are swapped internally). The abbreviations BIFORM and DETAILED stand for CHILD_BIFORM and CHILD_DETAILED.
The result is then refined by chaining member functions of tag_info_rep with ->:
Macros such as regular (i), adhoc (i), raw (i), argument (i), variable (i), binding (i), boolean (i), integer (i), string_type (i), numeric (i), length (i), code (i), url_type (i), identifier (i), color_type (i), graphical (i), constraint (i), graphical_id (i), point_type (i), effect (i), animation (i) and duration (i), each expanding to type (i, TYPE_…). The index i is a physical index into the child records.
Macros returns_adhoc (), returns_boolean (), returns_integer (), returns_string (), returns_numeric (), returns_length (), returns_url (), returns_identifier (), returns_animation (), returns_duration (), returns_color (), returns_graphical (), returns_constraint () and returns_effect (), expanding to type (TYPE_…).
The methods accessible (i) and hidden (i), which also set the child type to regular, and disable_writable (i), enable_writable (i).
The method locals (i, var, val) stores the environment (attr var val) for child i; only one variable can be given this way.
The methods inner_border (), outer_border (), with_like (), var_parameter () and var_macro_parameter ().
The methods name (s), long_name (s), name (i, s) and long_name (i, s).
A few representative declarations:
init (FRAC, "frac",
fixed (2) -> name ("fraction") ->
accessible (0) -> locals (0, "math-display", "false"));
init (WITH, "with",
var_repeat (2, 1, BIFORM) -> with_like () ->
binding (0) -> accessible (1));
init (HLINK, "hlink",
fixed (1, 1, BIFORM) ->
accessible (0) -> name (0, "text") ->
url_type (1) -> name (1, "destination") ->
name ("hyperlink"));
init (TFORMAT, "tformat",
var_repeat (1, 1, BIFORM) -> inner_border () ->
accessible (1) -> name ("table format"));
init (EXTERN, "extern",
repeat (1, 1, BIFORM) -> code (0) -> regular (1)); |
They read as follows. frac has two children which share one record (CHILD_UNIFORM): both are accessible, and in both math-display is false. with has any number of variable/value pairs, typed as bindings, followed by one accessible body; it only modifies the environment. The text of hlink is accessible, its destination is an inaccessible URL. In tformat, the cursor may not stand on the tag itself, the format instructions are inaccessible and the table is accessible. extern is a function name followed by regular, but inaccessible, arguments.
The macro macro (i) defined at the top of the file refers to a non-existent constant TYPE_MACRO; it is never used, so the file compiles.
A new primitive touches many parts of the kernel. The DRD-related steps are:
Add a member to the enumeration tree_label in
Kernel/Types/tree_label.hpp
, before START_EXTENSIONS. The numbering of the enumeration is not stored in documents, so the position is free.
Add an init declaration in init_std_drd. Choose the arity constructor which matches the admissible arities exactly: the editor creates new instances with the smallest admissible arity (make_compound), deletes instances with a wrong arity (correct_node, drd_correct), and offers structured insertion of arguments only at the positions allowed by insert_point.
Declare as accessible exactly the children which the typesetter typesets as editable content (that is, with the inverse path of the child). Give the inaccessible children a precise type and a name: the focus bar shows an input field for each inaccessible child whose type is not adhoc, raw, graphical, point, obsolete or unknown, labelled by the child name (
generic/generic-menu.scm
), and source mode colors children by type.
Declare with locals any environment change in a child which matters for editing (mostly mode, math-display and prog-language): the keyboard, the spell checker, the mathematical parser and the converters use it. Declare inner_border () if the cursor should not stand on the tag itself, and with_like () if the tag only modifies the environment of its last child.
Implement the evaluation (
Typeset/Env/env_exec.cpp
) and the typesetting (
Typeset/Concat/concater.cpp
or
Typeset/Stack/stacker.cpp
), and, if needed, the converters. See the typesetter and macro expansion chapters, and the example of a graphical primitive.
Document the primitive in the format documentation (
doc/devel/format/regular/
).
Two practical remarks. First, once the primitive exists, its name is reserved: older documents or styles defining a macro of the same name will see the primitive instead (the upgrader in
Data/Convert/Texmacs/upgradetm.cpp
exists to rename such conflicts). Second, the style DRDs cached on disk in $TEXMACS_HOME_PATH/system/cache are only removed automatically when the version of TeXmacs changes, after a crash during start-up, or with the option -setup; during development, clear them with Tools\(\rightarrow\)Update\(\rightarrow\)Styles (style-clear-cache) after changing
drd_std.cpp
.
About 310 variables are declared with init_var. The types are used for the focus menus of style parameters (
generic/generic-menu.scm
checks tree-label-type to offer a color chooser, for instance), by the documentation generator (
generic/generic-doc.scm
) and in source mode. A few variables carry a name (the page headers and footers). Variables which are not declared, such as those introduced by style files, are typed by heuristic_init_parameter from their name (color, *-color, *-length, *-width) or from their value, and can be typed explicitly with <drd-props|var|parameter|type>.