GNU TeXmacsUser manualReferenceAboutEnglish
The standard DRD and new primitives

1.Role of
drd_std.cpp

The file

Data/Drd/drd_std.cpp

defines three global objects (declared in

drd_std.hpp

):

drd_info std_drd ("tm")

The standard DRD, which describes every built-in primitive and the built-in environment variables. Every other DRD inherits from it.

drd_info the_drd

The DRD of the current view, initially equal to std_drd; see style and document DRDs.

hashmap<string,int> STD_CODE

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

2.The declaration language

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)

var_repeat (int arity, int extra, int child_mode= CHILD_UNIFORM)
(arity constructors)

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 ->:

Children types

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.

Type of the value

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_…).

Accessibility

The methods accessible (i) and hidden (i), which also set the child type to regular, and disable_writable (i), enable_writable (i).

Environment

The method locals (i, var, val) stores the environment (attr var val) for child i; only one variable can be given this way.

Border and kind

The methods inner_border (), outer_border (), with_like (), var_parameter () and var_macro_parameter ().

Names

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.

3.Adding a new primitive

A new primitive touches many parts of the kernel. The DRD-related steps are:

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

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

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

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

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

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

.

4.Properties of the built-in environment variables

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

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