![]() |
Style and document DRDs | ![]() |
The properties of a tag depend on the style: theorem is a macro with one accessible argument in article, and an unknown label in a document without style. Each DRD is therefore built in layers on top of std_drd, using the inheritance of drd_info (name, base):
std_drd: built-in primitives and variables.
The style DRD: the macros and parameters of the style files and packages, computed once per style tuple.
The editor DRD (editor_rep::drd), which starts from the style DRD and is completed with the definitions in the initial environment and the preamble, and with those in the body of the document up to the cursor.
Within a layer, properties come from two sources: the heuristic inference from the macro definitions, and explicit drd-props declarations. The latter freeze the properties they set, so they always win, whatever the order in which both are executed.
Defined in
Data/Document/new_style.cpp
. The argument is a style tuple such as (tuple "article" "number-europe"). The function creates drd_info drd ("none", std_drd) and an edit_env whose drd member refers to it. Then:
if the style is in the cache (see below), the cached environment is installed with patch_env, the cached DRD entries with set_locals, and the environment is recorded with set_environment;
otherwise the style is executed (env->exec (tree (USE_PACKAGE, A (style)))), which evaluates all assign and drd-props of the style files (the latter act directly on drd), then the resulting environment is read back and drd->heuristic_init (H) infers the properties of all its macros and variables.
The results are memorized in the global style_data_rep, in the maps style_cached and drd_cached. A set of “busy” styles protects against recursion; it returns false if the style is already being computed.
drd_info get_style_drd (tree style)
Return the memorized DRD or environment, computing them if needed. For a busy style, get_style_drd returns std_drd and get_style_env an empty environment.
Used by tree processing routines which are not attached to an editor. It takes the style of the document, gets its DRD and, if the document has a preamble (hide-preamble or show-preamble), creates a derived drd_info ("preamble", …) in which the style and the preamble are executed and the heuristics are run again. For a tree without a TeXmacs version attribute (typically a fragment), the current the_drd is used if it looks like a real document style is loaded (the theorem tag has a syntax), and the generic style otherwise. Used, through with_drd, by the correction routines of
Data/Tree/tree_correct.cpp
and
tree_brackets.cpp
.
Computing the environment of a style means loading and executing all its packages, which is slow; it is therefore cached at two levels.
The structure style_data_rep contains, per style tuple, the environment and the serialized local DRD entries (style_cache, style_drd) and the computed objects (style_cached, drd_cached).
style_set_cache (style, H, t) writes the pair (tuple H t), where t is the result of get_locals (see serialization), to a file in $TEXMACS_HOME_PATH/system/cache whose name is derived from the style tuple by cache_file_name (for instance __article__number-europe__). The file is written only if it does not exist yet. style_get_cache looks in memory first and then on disk.
style_invalidate_cache discards the in-memory structure and deletes the files __* in the cache directory. It is called by tm_server_rep::style_clear_cache (glue cpp-style-clear-cache, Scheme function style-clear-cache, menu Tools\(\rightarrow\)Update\(\rightarrow\)Styles), which then notifies all editors so that they recompute their environment. style-clear-cache is also called automatically when a file with suffix .ts is saved from TeXmacs (
texmacs/texmacs/tm-files.scm
). Style files edited with another editor are not detected: the cache must then be cleared by hand.
Since only the local entries are stored, a cached style DRD is only valid with the std_drd of the TeXmacs binary which produced it. The whole cache directory is emptied when the version changes (init_upgrade), after an interrupted start-up (acquire_boot_lock) and with the command line option -setup.
Each editor owns a drd_info drd, created by the constructor of editor_rep (
Edit/Editor/edit_main.cpp
) as drd_info (buf->buf->title, std_drd). Its typesetting environment env is constructed with a reference to this member, so that assignments to drd are seen by the environment, and drd-props executed during typesetting modify the editor DRD.
On a cache hit, the cached environment is patched into env and the cached entries are installed into the editor DRD with set_locals. On a miss, the environment and DRD are computed with get_style_env and get_style_drd, the cache is filled, and drd is replaced by the style DRD object (see the pitfalls for the consequences of this sharing).
Writes the default environment, installs the style, applies the initial environment of the document, reads back the resulting environment pre and runs drd->heuristic_init (pre). It is called lazily when the initial environment is first needed, before printing, from the conversion routines exec_html and exec_latex, and by typeset_invalidate_all, which is triggered by notify_change (THE_ENVIRONMENT) after any change of style, package or initial environment.
Computes the environment just before the cursor (typeset_exec_until (tp)) and runs the heuristics on it, so that macros defined in the body of the document (before the cursor) get their properties. It is called at the end of edit_interface_rep::apply_changes, unless there are pending events (gui_interrupted); this is the “small amount of free time” mentioned in the user documentation.
Note that set_locals adds or overwrites entries but never removes any, and heuristic_init likewise only updates the properties of the variables present in its argument. The editor DRD can therefore retain properties of macros which are no longer defined (for instance after the deletion of a definition in the preamble, or after a style change on the cache-hit path), until the buffer is reopened.
drd_info_rep::heuristic_init (env) iterates over all variables of an environment and dispatches on the value:
heuristic_init_macro: fixed arity equal to the number of parameters (CHILD_DETAILED), type of the tag = type of the body, with-like flag if the body is a chain of with-like constructs ending in the last argument, child names = parameter names, and for each parameter the result of a search for (arg x) in the body (arg_access), which determines whether the argument is accessible, its type, and its environment.
heuristic_init_xmacro: ARITY_REPEAT with a minimal arity derived from the largest index used.
heuristic_init_parameter: arity zero, VAR_PARAMETER, and a type guessed from the name or the value. Zero-parameter macros whose body is a length or a localize become VAR_MACRO_PARAMETER.
Because the properties of a macro depend on those of the macros it uses (accessibility propagates through with, compound, nested macro calls and the child environments of primitives), the loop is repeated until no record changes, and abandoned with the warning bad heuristic drd convergence after ten rounds. The details, with examples, are in the macro expansion chapter.
The heuristics only use the DRD itself and the macro definitions; they never look at the boxes. A macro which uses an argument in a way the heuristics do not understand (inside extern, merge, a map-args of an inaccessible tag, …) gets an inaccessible argument, and a drd-props declaration is needed.
drd-props is evaluated by edit_env_rep::exec_drd_props (
Typeset/Env/env_exec.cpp
) whenever the tree is executed: when a style is loaded, when the preamble is processed, or when a drd-props in the body of a document is typeset (concater_rep::typeset_drd_props, which also displays a flag). Its arguments are not evaluated. The first argument is the tag name (possibly a pseudo-label extern:f), followed by pairs of a property and a value:
|
||||||||||||||||||||||||||||||||||||
Unknown properties and values are silently ignored. Some consequences of the implementation are worth knowing:
Indices are physical indices into the child records, not child numbers. For a fixed arity (CHILD_DETAILED) both coincide; after arity|<tuple|repeat|1|1> (CHILD_BIFORM), index \(0\) is the fixed part and index \(1\) all the repeated children.
Since the child records are created by the arity, the arity property must come first when the tag has no record yet (in particular for extern:f pseudo-labels); otherwise index properties refer to non-existent records and are ignored.
The value no of border currently sets BORDER_INNER instead of BORDER_NO (see the pitfalls).
Type names are those accepted by drd_encode_type: regular, adhoc, variable, argument, boolean, integer, string, length, numeric, code, identifier, url, color, graphical, point, constraint, graphical-id, effect, animation, duration, font-size, unknown and error.
Typical declarations in the standard style packages:
<drd-props|explain-macro|arity|<tuple|repeat|1|1>|accessible|all> <drd-props|help-link|arity|2|accessible|0|url|1> <drd-props|phantom|arity|1|accessible|none|syntax|<macro|body|>> <drd-props|enunciation-sep|macro-parameter|string> |
Most generic routines on trees (Data/Tree/,
System/Language/
, the converters) do not have access to an editor, and consult the global variable the_drd instead. It is set:
to the DRD of the editor of the current view, by set_current_view (
Texmacs/Data/new_view.cpp
), which is called whenever the focus changes to another view;
to the DRD of the view of a given buffer, by set_current_drd (url) (glue set-drd, used by
texmacs/texmacs/tm-print.scm
while printing);
temporarily, to the DRD of the editor of a window while its menus are computed (tm_window_rep::get_menu_widget), and to the editor DRD in edit_interface_rep::resume;
temporarily, with the helper with_drd, whose constructor saves the_drd and installs another DRD and whose destructor restores it:
tree
superfluous_with_correct (tree t) {
with_drd drd (get_document_drd (t));
return superfluous_with_correct (t, tree (WITH, MODE, "text"));
} |
Code inside the editor uses the member drd directly, and the typesetter uses env->drd; outside the editor, the_drd is the only way. When writing a new routine which may be called on a tree not belonging to the current buffer (for instance on a document being converted), wrap it in with_drd with get_document_drd.
Whether a child is “accessible” depends not only on the DRD but also on a global access mode (
Data/Drd/drd_mode.hpp
):
The default: only ACCESSIBLE_ALWAYS children are accessible.
Hidden children are accessible too. Used by search (edit_replace_rep::next_match), so that matches in folded content are found.
Every child is accessible. Used in source mode (when the mode of the document is src: make_cursor_accessible, selection_correct, compute_env_rects), for children whose environment has mode=src (is_accessible_cursor), inside inactive markup (is_modified_accessible), and by compute_selection and update_mouse_loci.
set_access_mode (mode) returns the previous mode, and all callers follow the pattern
int old_mode= set_access_mode (DRD_ACCESS_SOURCE);
…
set_access_mode (old_mode); |
The writable mode (DRD_WRITABLE_NORMAL, DRD_WRITABLE_INPUT, DRD_WRITABLE_ANY, functions set_writable_mode and get_writable_mode) is maintained in the same way by is_accessible_cursor while it descends into children with disabled or enabled writability; see the data model. From Scheme, the access mode is available as get-access-mode and set-access-mode (with the integer values \(0\), \(1\), \(2\)), as in tree-perform-search (
generic/search-widgets.scm
).