GNU TeXmacsUser manualReferenceAboutEnglish
Macro expansion during typesetting

1.Inverse paths and decorations

Every box produced by the typesetter carries an inverse path ip, the reversed path of its source subtree in the edit tree (see the boxes produced by the typesetter). An inverse path whose head is negative is a decoration: the box does not correspond to editable content, and the rest of the path indicates where the cursor should go when the box is clicked. The relevant definitions are in

Typeset/boxes.hpp

:

#define DECORATION (-1)
#define DECORATION_LEFT (-2)
#define DECORATION_MIDDLE (-3)
#define DECORATION_RIGHT (-4)
#define DETACHED (-5)
#define is_accessible(p) ((is_nil (p)) || ((p)->item >= 0))
#define is_decoration(p) ((!is_nil (p)) && ((p)->item < 0))
inline path descend (path ip, int i) {
  return (is_nil (ip) || (ip->item >= 0))? path (i, ip): ip; }
inline path decorate_right (path ip) {
  return (is_nil (ip) || (ip->item >= 0))? path (DECORATION_RIGHT, ip): ip; }

Descending into a decoration yields the same decoration, and decorating a decoration does not stack a second negative number. Hence all subtrees of a decorated tree share the decoration of its root.

Trees know their own inverse path through an ip_observer (

Data/Observers/ip_observer.cpp

). obtain_ip (t) returns it, or DETACHED if t has no inverse path or if its path contains a negative number anywhere. Trees which are produced by the evaluator are detached. The function

tree attach_dip (tree ref, path dip)
(give a tree a (decoration) inverse path)

returns ref itself if it already has a valid inverse path, and otherwise a copy of ref whose nodes are recursively attached to dip and its descendants (

Typeset/Boxes/Basic/boxes.cpp

). The macros attach_here (t, ip) and attach_right (t, ip) expand to the two arguments attach_dip (t, ip), ip, respectively attach_dip (t, decorate_right (ip)), decorate_right (ip), which are passed to the typesetting routines taking a tree and a path.

The first rule of attach_dip is what makes the whole scheme work: subtrees of the document which end up inside a computed tree keep their own inverse path. Conversely, concater_rep::typeset (tree t, path ip) starts with

if (!is_accessible (ip)) {
  path ip2= obtain_ip (t);
  if (ip2 != path (DETACHED))
    ip= ip2;
}

so that a document subtree met while typesetting decorated material is typeset with its genuine location, and is therefore editable.

2.Typesetting a macro application

Inline macro applications are typeset by concater_rep::typeset_compound (tree t, path ip) (

Typeset/Concat/concat_macro.cpp

). After looking up the macro f exactly as exec_compound does (an undefined tag is typeset with typeset_error), it proceeds as follows:

if (is_applicable (f)) {
  int i, n=N(f)-1, m=N(t)-d;
  env->macro_arg= list<hashmap<string,tree> > (
    hashmap<string,tree> (UNINIT), env->macro_arg);
  env->macro_src= list<hashmap<string,path> > (
    hashmap<string,path> (path (DECORATION)), env->macro_src);
  if (L(f) == XMACRO) {
    if (is_atomic (f[0])) {
      string var= f[0]->label;
      env->macro_arg->item (var)= t;
      env->macro_src->item (var)= ip;
    }
  }
  else for (i=0; i<n; i++)
    if (is_atomic (f[i])) {
      string var= f[i]->label;
      env->macro_arg->item (var)=
        i<m? t[i+d]: attach_dip (tree (UNINIT), decorate_right(ip));
      env->macro_src->item (var)= i<m? descend (ip,i+d): decorate_right(ip);
    }
  if (is_decoration (ip))
    typeset (attach_here (f[n], ip));
  else {
    marker (descend (ip, 0));
    typeset (attach_right (f[n], ip));
    marker (descend (ip, 1));
  }
  env->macro_arg= env->macro_arg->next;
  env->macro_src= env->macro_src->next;
}

So, in comparison with exec_compound:

Everything else happens when the typesetter meets the primitives of the body. Since these are typeset (not evaluated), the ordinary typesetting code applies: with changes the environment around its body, if evaluates its condition and typesets only the chosen branch (concater_rep::typeset_if, with the path descend (ip, 1) or descend (ip, 2)), nested macro applications are expanded recursively, and so on. The contents of the chosen branch of an if remain editable if they come from an argument; the value of an eval or of a computation does not.

3.Typesetting an argument

concater_rep::typeset_argument handles (arg "x" i1 … ik):

string name = r->label;
tree value= env->macro_arg->item [name];
path valip= decorate_right (ip);
if (!is_func (value, BACKUP)) {
  path new_valip= env->macro_src->item [name];
  if (is_accessible (new_valip)) valip= new_valip;
}
marker (descend (ip, 0));
list<hashmap<string,tree> > old_var= env->macro_arg;
list<hashmap<string,path> > old_src= env->macro_src;
if (!is_nil (env->macro_arg)) env->macro_arg= env->macro_arg->next;
if (!is_nil (env->macro_src)) env->macro_src= env->macro_src->next;
… // descend into value and valip along i1, …, ik
typeset (attach_here (value, valip));
env->macro_arg= old_var;
env->macro_src= old_src;
marker (descend (ip, 1));

The argument is typeset with the inverse path recorded in macro_src (if it is accessible), in the frame of the caller. For an argument coming from the document this path is the true location of the argument, so the boxes of the argument are accessible, the cursor can enter them and modifications are routed to the right place. The markers around it have the path of the arg tag in the macro body, which is a decoration. With an index path, value and valip are descended in parallel, so (arg "x" "1") typesets the second child of the argument with the path of that child.

If the argument is itself decorated material (for instance because the macro was called from inside the body of another macro, with an argument which was computed there), macro_src contains a decoration and valip falls back to decorate_right (ip). A typical case is an argument which is passed on to another macro, as in (macro "x" (strong (arg "x"))): inside strong, the bound argument is the (decorated) tree (arg "x") of the outer body. Typesetting it pops the inner frame and resolves the reference in the outer frame, where the true path of the document subtree is found. In this way arguments remain editable through any number of macro levels.

3.1.A worked example

Consider the document fragment (hello "world") at inverse path ip, with the definition (assign "hello" (macro "name" (concat "Hello " (arg "name") "!"))). The typesetter

  1. pushes a frame binding name to the string world of the document and to the path descend (ip, 0);

  2. inserts a marker at descend (ip, 0) (cursor before the tag);

  3. typesets the body (concat "Hello " (arg "name") "!"), to which it attaches the path decorate_right (ip); the strings Hello and ! become decorated boxes;

  4. when it reaches (arg "name"), pops the frame and typesets world with path descend (ip, 0), that is, as the editable first child of hello;

  5. restores the frame, inserts a marker at descend (ip, 1) (cursor after the tag) and pops the frame.

By contrast, if the macro were defined as (macro "name" (merge "Hello " (arg "name") "!")), the merge primitive would be typeset by typeset_executable: the whole body, including the argument, would be evaluated by exec_merge into the fresh string Hello world!, which would be typeset as a decoration. The visual result is the same, but the word world can no longer be edited in place.

4.Other macro-related primitives

mark

(mark (arg "x") body) typesets body but surrounds it with markers carrying the paths of the argument x (concater_rep::typeset_mark). It is used when a macro displays a transformed version of an argument: the cursor positions at the borders of the rendering are identified with the borders of the argument. The source code rendering of inactive markup uses it extensively.

expand-as

(expand-as x body) typesets body (with path descend (ip, 1)), and evaluates to the value of body, but for the purposes of expand it stands for x. This tells routines which look for the accessible part of a macro (loci, hard-id, find-accessible) which argument the rendering represents.

value, or-value

The value of the variable is typeset with typeset_dynamic, as a decoration. A macro stored in a variable and typeset through value is thus displayed as source code, not expanded.

Executable primitives

Arithmetic, string and comparison primitives, quasiquote, while, provides and the like are evaluated with exec and the result is typeset by typeset_dynamic, which attaches decorate_right (ip) and inserts markers around it (concater_rep::typeset_executable).

eval, quasi

typeset_eval evaluates the argument once and typesets the result dynamically.

Rewritable primitives

typeset_rewrite typesets env->rewrite (t) with typeset_dynamic. Parts of the result which are document subtrees keep their inverse paths thanks to attach_dip; this is why extern with quote-arg and map-args preserve editability.

drd-props, assign, provide

These are executed for their side effects when they are typeset, and leave a control item or a flag (typeset_drd_props, typeset_assign).

5.Macro expansion in bridges

At the paragraph level the document is typeset incrementally by bridges (see the chapter on the typesetter). make_bridge (

Typeset/Bridge/bridge.cpp

) chooses the bridge class according to the label:

bridge_compound

User tags (labels from START_EXTENSIONS on), compound, include, hlink, action, active, style-only and their variants.

bridge_argument

arg.

bridge_with

with.

bridge_rewrite

extern, include*, with-package, rewrite-inactive.

bridge_eval

eval, quasi, map-args and the animation primitives anim-static, anim-dynamic.

bridge_expand_as, bridge_mark

expand-as, mark.

bridge_auto

Trees which are rendered through a fixed macro: inactive, inactive*, error trees, and all trees when the environment is in preamble mode (make_inactive_bridge).

bridge_compound_rep::my_typeset pushes a frame exactly like typeset_compound, and then calls initialize (f[n], d, f), which creates (or reuses) a body bridge for attach_right (f[n], ip). A marker for the application is inserted unless the DRD declares the tag child enforcing (the_drd->is_child_enforcing, which holds for tags with an inner border, see drd-props). bridge_argument_rep::my_typeset similarly creates a bridge for the argument, attach_here (value, valip), and typesets it with the frame popped.

5.1.Propagating edits through macros

The payoff of this organization is that edits inside macro arguments are cheap, even when the macro is a large block environment. Suppose the user types inside the second argument of a theorem-like block macro. The modification arrives at the bridge_compound of the application as notify_assign (p, u) (or notify_insert, notify_remove) with p->item equal to the child number. The bridge translates the child number into the name of the macro parameter and calls

notify_macro (MACRO_ASSIGN, fun[p->item-delta]->label, -1, p->next, u);

bridge_compound_rep::notify_macro pushes the argument frame (as in my_typeset) and forwards the notification to its body bridge with level l+1, that is 0. The body bridges forward it further: bridge_with simply passes it to its body; a nested bridge_compound pushes its own frame and increments the level; a bridge_argument at level 0 checks that the parameter name (and the index prefix of an arg with indices) matches, and then applies the modification directly to the bridge of the argument; at a positive level it pops a frame and forwards with level l-1. The level therefore counts, as in exec_until, the number of macro frames between the current position and the frame which binds the parameter. Every bridge on the way which is affected marks itself as CORRUPTED, so that exactly the paragraphs which depend on the argument are re-typeset.

Bridges which cannot forward precisely (bridge_rewrite, bridge_eval, and invalid bridges) fall back on edit_env_rep::depends to decide whether they depend on the modified parameter at the given level, and re-typeset themselves completely if they do.

6.Inactive markup

Source code is displayed by rewriting rather than by special boxes. The inactive tags, the preamble and the style file editor all end up in edit_env_rep::rewrite_inactive (tree t, tree var), where t is the tree to display and var is an arg reference to it. The result is ordinary markup using the style macros src-regular, src-var, src-arg, src-unknown and so on (see the static function highlight in

env_inactive.cpp

), whose children are again arg references with index paths such as (arg "x" "1" "0"). When this rendering is typeset, each reference is resolved by typeset_argument, which yields boxes with the correct source path, so that the source remains editable. The rendering depends on the environment variables src-style, src-special, src-compact and src-close (cached in src_style, src_special, …) and on the DRD types of the children (drd_info_rep::get_type_child).

How source is entered, edited and presented to the user is described in source mode.

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