![]() |
Pitfalls and debugging hints | ![]() |
Any primitive which is evaluated rather than typeset (arithmetic, merge, change-case, eval, extern with arg, value of a stored tree, …) produces a fresh tree which is displayed as a decoration. If an argument must stay editable, only pass it through typesetting constructs (with, if, concat, other macros, …), use quote-arg for extern, and use mark or expand-as to tell the editor which argument a computed rendering stands for.
A macro does not capture the arguments of the macro in which it is defined. If the body of foo executes (assign "bar" (macro "y" (arg "x"))), then (arg "x") is looked up in the frame of bar when bar is applied, and yields the error arg x. Use quasi or quasiquote with unquote to insert the value at definition time.
An assign in a macro body is not undone at the end of the macro, and an assign in the document affects all following paragraphs. Use with for local changes.
In (with "a" "1" "b" (value "a") body), the variable b receives the old value of a, because all new values are computed before any of them is written.
An application with fewer arguments than parameters binds the remaining parameters to UNINIT, which the concater typesets like an error; extra arguments are silently ignored. Use an xmacro together with get-arity when the number of arguments varies.
if, case and while reject any other value with an error.
The environment and DRD of every style tuple are cached in memory and on disk in $TEXMACS_HOME_PATH/system/cache. After editing a .ts file outside TeXmacs, call style-clear-cache.
A property set with drd-props can no longer be changed by the heuristics. Values which exec_drd_props does not recognize are silently ignored, but in several cases the property is frozen anyway; for instance with-like only accepts yes and no, but freeze_with_like is called for any value.
The semantics of a primitive are implemented at least twice: in edit_env_rep::exec (and exec_until, expand, depends) and in the concater and bridge code. Several functions carry comments asking to keep them in sync (e.g. exec_if and concater_rep::typeset_if). When adding a primitive, also consider exec_until (otherwise the environment at the cursor is wrong inside it), depends (otherwise edits of macro arguments may not invalidate it) and the DRD in
drd_std.cpp
.
Every push of macro_arg must be matched by a push of macro_src, and every temporary pop (as in exec_arg) must restore both lists, also on error paths. exec_eval_args reads macro_arg->item only after checking that the stack is not empty; exec_arg and friends return an error tree in that case. A mismatch typically shows up much later as arguments resolved in the wrong frame.
A persistent change of the environment made with write or write_update is not recorded in back, and is therefore lost when the enclosing bridge is later replayed from its cached changes. Conversely, a temporary change must be restored exactly, or it leaks into the following paragraphs.
Writing a built-in variable without update leaves the C++ caches (fn, pen, mode, …) stale.
The getters get_int, get_length etc. do not evaluate the stored value, and return a default for compound values. Values stored by assign and with are already evaluated, but values written directly from C++ or taken from default_env may be expressions (for instance lengths such as (plus …)), which is why as_tmlen and exec_value evaluate their argument.
For extern, rewrite calls the Scheme function each time the tree is typeset, evaluated or searched by exec_until (exec_until_rewrite rewrites again). Scheme functions used through extern should be cheap and free of side effects.
depends only sees arg, quote-arg, map-args and eval-args in the unrewritten tree. A macro which accesses its argument in a more indirect way (for instance by building a tree which is later evaluated) may not be re-typeset when the argument changes.
Several pieces of state are global: the_drd, the current_rewrite_env used by texmacs-exec, the static quote_substitute flag, the style caches in
new_style.cpp
and default_env. On a cache miss, typeset_style_use_cache stores in the editor the drd_info object which is also kept in the style cache (drd_cached); since drd_info has reference semantics, the later heuristic_init calls for the document operate on that shared object.
In edit_env_rep::exec_provide, the test reads if (provides (t->label)) return "";, where t is the compound provide tree; the typesetting counterpart concater_rep::typeset_provide tests the evaluated variable name. Be careful when relying on provide in evaluated (as opposed to typeset) context, for instance in style files.
Most functions in
env_exec.cpp
and in the bridges contain commented-out trace statements (for instance // cout << "Execute: " << t << "\n"; at the start of exec, or the traces in notify_macro). Uncommenting them is the quickest way to follow an expansion.
An edit_env can be printed with operator <<, which prints the whole variable table.
From Scheme, (texmacs-exec '(value "font-size")) evaluates markup in the current environment and texmacs-exec* in the environment at the cursor; get-env, get-env-tree and get-full-env give access to the environment at the cursor.
Switching a document to source mode, or wrapping a fragment in inactive*, shows the markup as the kernel sees it; error trees produced by the evaluator (for instance compound foo for an undefined tag, or arg x for an unbound argument) are displayed in place.