GNU TeXmacsUser manualReferenceAboutEnglish
Pitfalls and debugging hints

1.Pitfalls for macro writers

Computing with an argument makes it read-only

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.

No closures

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.

Assignments are global

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.

with evaluates all values first

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.

Missing arguments

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.

Conditions must be true or false

if, case and while reject any other value with an error.

Style changes are cached

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.

DRD declarations freeze properties

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.

2.Pitfalls for C++ developers

Keep evaluation and typesetting consistent

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

.

Restore the argument stacks

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.

Use monitored writes for persistent changes

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.

Call update

Writing a built-in variable without update leaves the C++ caches (fn, pen, mode, …) stale.

Values are stored evaluated, except macros

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.

Rewriting is repeated

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.

Dependencies through rewriting are not tracked

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.

Global state

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.

A suspicious line

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.

3.Debugging hints

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