GNU TeXmacsUser manualReferenceAboutEnglish
Integration of the style rewriter with the editor

1.Enabling the code

The preprocessor symbol EXPERIMENTAL controls both the compilation of

src/src/Style/

and the hooks in the editor:

In the default configuration the symbol is undefined (System/config.h contains /* #undef EXPERIMENTAL */), none of the hooks below exist and no file of

Style/

is built. As explained in the pitfalls, an experimental build currently fails in three files of

Style/Evaluate/

.

2.State kept by the editor

Under EXPERIMENTAL, editor_rep (

Edit/editor.hpp

) has three extra fields and one extra virtual method:

environment ste

The “style environment”: a persistent environment holding the values of all environment variables at the start of the document.

tree cct

The clean copy of the document body.

memorizer mem

The memorizer of the last evaluation of cct in ste; its tree is the rewritten document.

environment_update ()

Rebuilds ste; implemented by edit_typeset_rep::environment_update.

3.The clean copy

The editor's own document lives in the global edit tree the_et and is modified in place by the modification routines; it cannot serve as a key for memoization, since an unchanged pointer does not mean unchanged contents. The clean copy is a second tree with the same contents, which is only ever updated functionally:

copy_announce (

Style/Memorizer/clean_copy.cpp

) replaces cct by clean_apply (cct, mod). The functions clean_assign, clean_insert, … of

Kernel/Types/modification.cpp

rebuild only the nodes on the path of the modification and reuse all other subtrees, so that unmodified parts of the new clean copy are the same tree objects as in the old one. copy_ip then gives the new nodes the inverse paths of the corresponding nodes of the real document: it walks both trees in parallel and, wherever the inverse paths differ, prepends an ip_observer with the source path to the observers of the copy. The recursion stops at shared subtrees, whose inverse paths already agree. Thanks to these inverse paths, the rewritten trees produced from the clean copy can be related to positions in the real document (decorate_ip in the evaluator).

Note that edit_done is called for every modification kind, including MOD_SET_CURSOR, for which clean_apply calls clean_set_cursor.

4.Re-evaluation after changes

edit_interface_rep::apply_changes (Edit/Interface/edit_interface.cpp:841-850) contains, right after the normal typesetting step:

#ifdef EXPERIMENTAL
  if (env_change & THE_ENVIRONMENT)
    environment_update ();
  if (env_change & THE_TREE) {
    cout << HRULE;
    mem= evaluate (ste, cct);
    tree rew= mem->get_tree ();
    cout << HRULE;
    cout << tree_to_texmacs (rew) << LF;
  }
#endif

environment_update (Edit/Editor/edit_typeset.cpp:351-362) prepares the typesetting environment as for normal typesetting (typeset_prepare), writes base-file-name, cur-file-name and secure into it, reads the whole environment into a hash map and calls primitive (ste, h), which replaces ste by a new primitive environment built from the map. Since ste is a new object after each environment change, and the memo table is keyed on environment pointers, a change of the environment (style, document settings) causes a complete re-evaluation; a change of the tree alone keeps ste and can reuse earlier results.

evaluate (ste, cct) returns the memorizer of the top-level evaluation, which is stored in mem. Storing it is what keeps the previous computation alive: its memorizers stay referenced from mem (through the children arrays) until the next evaluation has found them again, which is how is_memorized recognizes them (see memoization).

The rewritten tree is printed and then dropped. No other part of TeXmacs reads mem: a search for ->mem, ste and cct outside these hooks finds no users. In particular the typesetter keeps using edit_env_rep::exec, and the result of the rewriter is not compared with it.

5.What a revival would need

Turning the prototype into something useful requires, at least:

  1. fixing the compilation errors listed in the pitfalls;

  2. removing or guarding the unconditional traces in evaluate, rewrite and evaluate_inactive, and the printing in apply_changes;

  3. implementing the missing primitives (see the evaluator) and replacing the placeholders;

  4. memoizing environment changes on the contents of the local bindings rather than on the pointer of a freshly created assoc_environment;

  5. an actual consumer of the rewritten tree, for instance a comparison with the typesetter's expansion in a test suite.

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