![]() |
The DRD from Scheme: glue and tag groups | ![]() |
The C++ DRD is read-only from Scheme: there is no glue function to set a property. Properties are declared in markup, with drd-props in a style file, a package or the preamble of a document. The query functions are declared in
Scheme/Glue/build-glue-basic.scm
and implemented, for most of them, by the small wrappers of
Data/Tree/tree_traverse.cpp
. They all consult the_drd, i.e. the DRD of the current view; when working on a tree which belongs to another buffer, call (set-drd buffer-url) first.
Arguments described as trees may be given as Scheme trees (strees), since the glue converts them as content.
Returns the type name ("regular", "length", "color", …) of a label given as a symbol. For a variable, this is the type of its value.
(tree-label-macro? label)
The first is false only for plain variables (VAR_PARAMETER), the second only for plain macros (VAR_MACRO); both are true for macro parameters such as enunciation-sep. Used to build the focus menus of style parameters.
(tag-minimal-arity label)
(tag-maximal-arity label)
The arity bounds and the admissibility test of the data model; the maximum is 2147483647 for repeated arities.
(tree-minimal-arity t)
(tree-maximal-arity t)
(tree-possible-arity? t n)
(tree-insert_point t i)
The same for the label of t; tree-insert_point (note the underscore) tests whether children may be inserted at position i, and tree-is-dynamic? whether the arity is variable. focus-can-insert? (
generic/generic-edit.scm
) compares tree-arity with tree-maximal-arity.
(tree-accessible-child? t i)
(tree-accessible-children t)
(tree-all-accessible? t)
Accessibility in the current access mode (see get-access-mode). The library function tree-map-accessible-children (
kernel/library/tree.scm
) applies a function to the accessible children only.
(tree-name t)
(tree-long-name t)
(tree-child-name t i)
(tree-child-long-name t i)
The name of the tag (by default its label), the names of its children (empty if none was declared or inferred), and the type name of a child.
(tree-child-env t i var default)
(tree-child-env* t i env)
(tree-descendant-env t p var default)
The value of the variable var inside child i (resp. at the relative path p), or default if the DRD does not change it; the starred versions take and return a whole attr tree. The values are not evaluated.
True for with, with-package and macros inferred or declared with-like, provided t has children.
(get-access-mode)
\(0\) for normal, \(1\) for hidden and \(2\) for source access; set-access-mode returns the previous mode, which must be restored afterwards.
Sets the_drd to the DRD of the editor of a view of buffer (set_current_drd). It is not restored automatically;
texmacs/texmacs/tm-print.scm
resets it explicitly after printing auxiliary buffers.
A short example, valid in any document:
(tree-child-type '(hlink "text" "https://www.texmacs.org") 1)
;; => "url"
(tree-accessible-child? (stree->tree '(hlink "text" "x")) 1)
;; => #f
(tree-child-env '(frac "1" "2") 0 "math-display" "")
;; => "false"
(tag-possible-arity? 'with 4)
;; => #f (variable/value pairs followed by a body) |
The focus toolbar shows an input field for each “hidden” child of the focus tag (hidden-child? in
generic/generic-menu.scm
): a child which is not accessible, is not the name of a variable in a with-like tag, and whose type has an input format (type->format maps "color" to a color chooser, "url" to a file field, "adhoc", "graphical", … to none). The label of the field is the child name (tree-child-name*), with fall-backs on the preceding variable name or the type. The tag name shown in the variant menu comes from tree-name (focus-tag-name).
After variant-set has replaced the label of a tree, the cursor is moved to the first accessible child if its old child became inaccessible.
The automatic documentation of tags (
generic/generic-doc.scm
) describes the arguments by their types and accessibility, and the parameters by tree-label-type.
The LaTeX exporter computes math/text statistics following tree-child-env (
convert/latex/tmtex.scm
); the semantic math editor does the same in
math/math-sem-edit.scm
.
Many editing functions need to know that certain tags are “of the same kind”: section, subsection, … are variants of each other, theorem has an unnumbered variant theorem*, folded and unfolded are two states of one object. This information is not part of the C++ DRD; it is kept in Scheme, as named tag groups.
Defined in
utils/edit/variants.scm
. Each member is either a tag name (a symbol) or a list (subgroup) naming another group whose members are included. The first declaration of a group also defines three functions:
the list of all tags of the group, with subgroups expanded recursively (group-resolve);
membership test;
whether the cursor is inside a tag of the group.
Further declarations of the same group append members. Groups may be referenced as subgroups before they are declared.
(group-resolve group)
group-resolve returns the flattened list of tags, memoized in group-resolve-table (the memo table is reset by every define-group). group-find returns the innermost subgroup of group which directly contains label, or #f: this is how the variants of a tag are found.
For example,
text/text-drd.scm
declares
(define-group variant-tag
(section-tag) (list-tag) (figure-tag)
(enunciation-tag) (prominent-tag) …)
(define-group section-tag
part chapter appendix
section subsection subsubsection
paragraph subparagraph) |
so that (variants-of 'subsection) is the list of the section-tag group.
Groups of interchangeable tags. (variants-of label) returns the subgroup of variant-tag containing the label (taking numbered and unnumbered versions into account); it feeds the variant menu of the focus bar (focus-variants-of in
generic/generic-menu.scm
) and variant-circulate (keyboard shortcuts for cycling through variants). Modes overload focus-variants-of and variant-circulate for special cases (mathematics, switches, spreadsheets).
Groups of tags which are “similar” for structured navigation: (similar-to label) is used by traverse-incremental and traverse-extremal (
generic/generic-edit.scm
) to jump to the previous or next tag of the same kind (for instance from a theorem to the next proposition).
Tags which have an unnumbered variant obtained by appending *. symbol-numbered?, symbol-unnumbered?, numbered-toggle and variant-set-keep-numbering implement the numbering toggle of the focus bar.
Pairs of tags representing two states of one object, declared with (define-alternate first second), which also fills alternate-table. They drive alternate-toggle, fold, unfold and the folding icons;
math/math-drd.scm
uses them for wide/wide* and around/around*.
dynamic/dynamic-drd.scm
defines the macros define-toggle, define-fold and define-summarize, which put a pair of tags into toggle-first-tag/toggle-second-tag, folded-tag/unfolded-tag or summarized-tag/detailed-tag and declare them as alternates. The session, program and script modules use define-toggle for their folded and unfolded fields.
hidden-tag (tags with hidden content, for tree-show-hidden), mini-flow-tag, make-inline-tag, make-wrapped-tag, the many specific groups of
text/text-drd.scm
(theorem-tag, list-tag, titled-tag, doc-title-tag, …) used by the structure and title routines, spell-tag (used with group-resolve by
tools/spell/spell-edit.scm
), and the graphical groups of
graphics/graphics-drd.scm
.
utils/edit/variants.scm
The macro, the standard groups for source-like tags (argument-tag, value-tag, binary-operation-tag, reference-tag, citation-tag, …).
text/text-drd.scm
Sections, lists, enunciations, figures, frames, titles, …
math/math-drd.scm
Fractions, vertical scripts, textual operators, mathematical annotations.
dynamic/dynamic-drd.scm
Folds, switches, overlays, animations; and the session, program, script and spreadsheet variants in
dynamic/session-drd.scm
,
program-drd.scm
,
scripts-drd.scm
,
calc-drd.scm
.
generic/format-drd.scm,
source/source-drd.scm,
version/version-drd.scm,
doc/tmdoc-drd.scm,
education/edu-drd.scm,
tools/comment/comment-drd.scm,
tools/poster/poster-drd.scm,
graphics/graphics-drd.scm
Groups of the corresponding modes and packages. Some editing modules also declare groups directly (
database/db-edit.scm
,
table/table-edit.scm
,
math/math-edit.scm
, …).
These modules are not loaded at start-up: they are pulled in through the :use clauses of the corresponding editing modules (
text/text-edit.scm
,
math/math-edit.scm
,
dynamic/fold-edit.scm
, …), which are themselves loaded lazily. The contents of a group may therefore grow during a session.
To make the variants of a new environment available in the focus bar and the keyboard, and to allow structured navigation between them, a plug-in or a package module declares, for instance:
(texmacs-module (my-package my-package-drd)
(:use (utils edit variants)))
(define-group my-box-tag
my-box my-shadowed-box my-rounded-box)
(define-group variant-tag (my-box-tag))
(define-group similar-tag (my-box-tag)) |
To declare my-claim as numbered, so that the focus bar offers to toggle it with my-claim*, add it to numbered-tag (both macros must be defined in the style):
(define-group numbered-tag my-claim) |
A group can also be extended at run time: since define-group is a macro, this requires eval, as in tm-register-new-list-tag (
text/text-drd.scm
), which is called from the std-list package (through extern) to register new list environments defined by the user:
(eval ‘(define-group new-list-tag ,t)) |
The Scheme kernel also contains a small Prolog-like engine (
kernel/logic/
), loaded at start-up by
init-texmacs.scm
and documented in logical programming extensions. In the code base it is the other half of the “DRD” terminology: declarative descriptions of tags used by the converters. By convention predicate names end with %. The forms which matter here are:
Membership facts, tested with (logic-in? x name). Used for instance for the LaTeX command classes in
convert/latex/latex-command-drd.scm
(latex-command-1%, latex-modifier-1%, …) and the symbol classes in
latex-symbol-drd.scm
.
Tables, read with logic-ref and logic-ref-list; a key (:or k1 k2 …) covers several keys. Examples: latex-needs% and latex-package-priority% in
convert/latex/latex-drd.scm
, the operator tables of
convert/mathml/mathml-drd.scm
.
Tables whose values are evaluated functions. Every structured converter maps tags to conversion routines this way: tmtex-primitives% and tmtex-extra-methods% (
convert/latex/tmtex.scm
), tmhtml-primitives% (
convert/html/tmhtml.scm
), htmltm-methods%, tmmath-primitives%, mathtm-methods%.
General rules, used for instance to merge two dispatchers:
(logic-rules
((tmtex-methods% 'x 'y) (tmtex-primitives% 'x 'y))
((tmtex-methods% 'x 'y) (tmtex-extra-methods% 'x 'y))) |
after which (logic-ref tmtex-methods% tag) finds the routine in either table.
Adding support for a new tag in a converter therefore usually means adding an entry to the dispatcher of that converter, not touching the C++ DRD. Note that query results are memoized and never invalidated (see the pitfalls).