![]() |
Integration of the style rewriter with the editor | ![]() |
The preprocessor symbol EXPERIMENTAL controls both the compilation of
src/src/Style/
and the hooks in the editor:
CMake: cmake -DENABLE_EXPERIMENTAL=ON …. The option is declared in
CMakeLists.txt:466
; when it is on, Style/*.cpp is globbed into TeXmacs_Style_SRCS and add_compile_definitions (EXPERIMENTAL=1) is executed; otherwise TeXmacs_Style_SRCS is empty. The include directories of
Style/
are added in both cases.
Autotools: ./configure --enable-experimental. The generated configure then defines EXPERIMENTAL in the configuration header and sets CONFIG_EXPERIMENTAL to "Memorizer Environment Evaluate", which
src/makefile.in
uses as the list of subdirectories of Style to compile (style_src).
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/
.
Under EXPERIMENTAL, editor_rep (
Edit/editor.hpp
) has three extra fields and one extra virtual method:
The “style environment”: a persistent environment holding the values of all environment variables at the start of the document.
The clean copy of the document body.
The memorizer of the last evaluation of cct in ste; its tree is the rewritten document.
Rebuilds ste; implemented by edit_typeset_rep::environment_update.
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:
The constructor of edit_main_rep (
Edit/Editor/edit_main.cpp
) sets cct= copy (subtree (et, rp)) and calls copy_ip (subtree (et, rp), cct), and initializes mem to the null memorizer.
After every modification of the document, edit_done (Edit/Modify/edit_modify.cpp:253-255) calls copy_announce (subtree (ed->et, ed->rp), ed->cct, mod / ed->rp).
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.
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.
Turning the prototype into something useful requires, at least:
fixing the compilation errors listed in the pitfalls;
removing or guarding the unconditional traces in evaluate, rewrite and evaluate_inactive, and the printing in apply_changes;
implementing the missing primitives (see the evaluator) and replacing the placeholders;
memoizing environment changes on the contents of the local bindings rather than on the pointer of a freshly created assoc_environment;
an actual consumer of the rewritten tree, for instance a comparison with the typesetter's expansion in a test suite.