![]() |
General architecture of TeXmacs | ![]() |
The core of TeXmacs is written in C++; most of the user interface, the menus, the keyboard bindings, the converters and many editing routines are written in the extension language Scheme (currently Guile). TeXmacs can be built with the traditional configure and make utilities, or with CMake. At the top level of the source distribution, one finds:
The C++ sources in the directory
src/src
.
The TeXmacs runtime data (Scheme programs, style files, fonts, documentation, icons, etc.) in the directory
src/TeXmacs
. After installation, this directory becomes $TEXMACS_PATH.
The plug-ins for external systems in
src/plugins
. They are copied into $TEXMACS_PATH/plugins when TeXmacs is built.
Build support (
configure.in
,
CMakeLists.txt
,
misc/m4
, cmake, etc.) and packaging data in
src/misc
and
src/packages
.
This chapter gives a bird's eye view on how these parts fit together. Several aspects are described in more detail in other chapters: see the documentation about the basic data types, the typesetter, macro expansion, the typeset boxes, the server, buffers, views and windows, the renderers and the abstract widget system.
The C++ sources in
src/src
are organized in the following directories:
Basic and generic data structures, which are used everywhere else. The subdirectory
Kernel/Abstractions
contains the reference counting machinery (
basic.hpp
), commands, observers and black boxes; Kernel/Containers contains arrays, lists, hash tables, hash sets and iterators; Kernel/Types contains strings, trees, tree labels, paths, modifications, rectangles and stretchable spaces.
Everything which is related to TeXmacs documents as data: the global edit tree (
Data/Document
), the data relation descriptors (
Data/Drd
), the observers which are attached to trees (
Data/Observers
), the undo/redo history and patches (Data/History), routines for analyzing and correcting trees (Data/Tree), string utilities and encodings (Data/String), small parsers for syntax highlighting (Data/Parser) and the C++ part of the converters (Data/Convert).
The interaction with the operating system: booting and preferences (
System/Boot
), URLs and timers (System/Classes), files (System/Files), natural and programming languages, hyphenation and dictionaries (
System/Language
), the connections with plug-ins through pipes, sockets and dynamic libraries (
System/Link
) and miscellaneous routines, such as the fast memory allocator (
System/Misc
).
Graphical data structures and the abstract interfaces to the graphical output and the user interface: fonts (Graphics/Fonts) and bitmap glyphs (
Graphics/Bitmap_fonts
), colors, pictures, points, curves and frames (Graphics/Types), renderers (
Graphics/Renderer
), the abstract widget and window interface (
Graphics/Gui
), handwriting recognition and some generic mathematical templates (
Graphics/Mathematics
).
The typesetter. It contains the typesetting environment and the evaluation of macros (Typeset/Env), the boxes (
Typeset/Boxes
), the incremental bridges between trees and boxes (
Typeset/Bridge
), the concatenation of lines (
Typeset/Concat
), line breaking (
Typeset/Line
), page breaking (
Typeset/Page
), stacks and tables.
An alternative, experimental implementation of the evaluator for style files and macros, which is independent from the typesetter.
The editor proper. The abstract class editor_rep is declared in
Edit/editor.hpp
; its implementation edit_main_rep (
Edit/Editor
) inherits from a series of classes which take care of the interface with the user (
Edit/Interface
), the modification of the document (
Edit/Modify
), selections, searching and replacing (
Edit/Replace
) and the processing of the document (
Edit/Process
).
The TeXmacs server, which manages buffers, views, windows and projects (
Texmacs/Data
,
Texmacs/Server
,
Texmacs/Window
), and the main program
Texmacs/Texmacs/texmacs.cpp
.
The interface with the Scheme interpreter. The generic object type and the calling conventions are defined in
Scheme/scheme.hpp
and
Scheme/Scheme
; the binding with Guile lives in
Scheme/Guile
. The “glue” which exports C++ routines to Scheme is generated from the specifications Scheme/Glue/build-glue-*.scm into the files Scheme/Glue/glue_*.cpp.
Implementations of the abstract interfaces for specific libraries or platforms. For instance, Plugins/Qt contains the default graphical user interface based on Qt (the other ports are in
Plugins/Vue
,
Plugins/NS
for Cocoa,
Plugins/SDL
,
Plugins/Qtwk
and
Plugins/Qt6
; see the graphical user interface),
Plugins/MuPDF
drawing and pictures with MuPDF,
Plugins/Freetype
the support for TrueType and OpenType fonts,
Plugins/Metafont
the support for TeX fonts,
Plugins/Pdf
the PDF renderer,
Plugins/Unix
, Plugins/MacOS and
Plugins/Windows
system specific code,
Plugins/Database
the database engine, and
Plugins/Widkit
the historical Widkit toolkit, used on
Plugins/X11
, SDL and Qtwk.
Roughly speaking, Kernel does not depend on anything else; Data and System only depend on Kernel (and on each other); Graphics builds on top of these; Typeset uses all previous parts; Edit relies on the typesetter and Texmacs uses everything. The directories in Plugins provide concrete implementations of abstract classes declared elsewhere (such as renderer_rep, widget_rep, window_rep or font_rep); which of them are compiled depends on the configuration.
The directory
src/TeXmacs
contains the part of TeXmacs which is not compiled. It is located at run time through the environment variable TEXMACS_PATH. The most important subdirectories are:
The Scheme programs. The boot sequence starts with
progs/init-texmacs.scm
; the kernel of the Scheme part (tm-define, menus, keyboard definitions, modes, plug-in declarations, etc.) is in
progs/kernel
. The other subdirectories correspond to the various editing modes (text, math, prog, graphics, table, etc.), to converters (convert), fonts (fonts) and so on.
The style files (.ts) and style packages.
Font data: TeX font metrics and Type 1 fonts, TrueType and OpenType fonts shipped with TeXmacs, encodings and virtual fonts, as well as the font database (
font-database.scm
and companions).
Language data: hyphenation patterns and dictionaries (
langs/natural
) and character encodings (
langs/encoding
).
The documentation, including the present document.
Miscellaneous data, such as the icons in
misc/pixmaps
, images, patterns, sounds, themes and helper scripts.
User specific data (preferences, personal styles, packages, plug-ins, font databases, etc.) are stored in the directory $TEXMACS_HOME_PATH, which defaults to ~/.TeXmacs on Unix systems.
TeXmacs represents all texts by trees. All open documents are subtrees of a single global tree the_et (the edit tree, declared in
Data/Document/new_document.hpp
); each buffer corresponds to a child of the_et and each editor knows the path rp to the root of its document. The inner nodes of a tree are labeled by tree labels: the built-in labels of the TeXmacs format are enumerated in
Kernel/Types/tree_label.hpp
, and new labels are created on the fly for user defined macros (make_tree_label). The leaves of the tree are strings, which are either invisible (such as lengths or the names of environment variables) or visible (the real text). Properties of the tags, such as their arity, the accessibility of their children and the types of their arguments, are maintained by the data relation descriptor or DRD (see
Data/Drd
and the chapter on macro expansion).
The meaning of the text and the way it is typeset essentially depend on the current environment. The environment (see the class edit_env_rep in
Typeset/env.hpp
) mainly consists of a hash table of type hashmap<string,tree>, which maps environment variables to their tree values. The current language and the current font are examples of system environment variables; new variables can be defined by the user, and macros are nothing but environment variables whose values are macro trees. When the typesetter enters a with tag or a macro body, the modified variables are saved in a second hash table, so that they can be restored afterwards.
All text strings in TeXmacs consist of sequences of either specific or universal symbols. A specific symbol is a character, different from '\0', '<' and '>'. A universal symbol is a string starting with '<', followed by an arbitrary sequence of characters different from '\0', '<' and '>', and ending with '>'. For instance, <alpha> stands for the Greek letter alpha and <#2212> for the Unicode character with code point U+2212. Specific characters are interpreted in the Cork encoding (the TeX T1 encoding); the conversions between this internal encoding and UTF-8 are done by routines such as utf8_to_cork and cork_to_utf8 in
Data/String/converter.cpp
. The meaning of symbols does not depend on the font which is used, but different fonts may render them in a different way (see the chapter on fonts).
The language of the text (see the abstract class language_rep in
System/Language/language.hpp
) is capable of performing a further semantic analysis of a text phrase. At least, it is capable of splitting a phrase into words (which are smaller phrases) and to inform the typesetter about the desired spaces between words and hyphenation information (the methods advance, get_hyphens and hyphenate). There are three main kinds of languages:
Natural languages (text_language), which use the hyphenation patterns from $TEXMACS_PATH/langs/natural/hyphen.
The mathematical language (math_language), which classifies mathematical symbols into groups (operators, relations, brackets, etc.) and determines the spacing between them. Semantic editing of formulas relies in addition on the packrat grammars implemented in System/Language/packrat_*.cpp.
Programming languages (prog_language and variants), which are mainly used for syntax highlighting of computer programs.
Spell checking is not done by the languages themselves, but by external tools (see
Plugins/Ispell
).
Roughly speaking, the typesetter of TeXmacs takes a tree on input and produces a box, while accessing and modifying the typesetting environment. The typesetting is incremental: the typesetter maintains a tree of bridges which mirrors the document tree and remembers the boxes which were produced during the previous run, so that only the modified parts of the document need to be typeset again.
The box class is multifunctional. Its principal method is used for displaying the box on a renderer (the screen, a printer, a PDF file or an image). But it also contains a lot of typesetting information, such as logical and ink bounding boxes, the positions of scripts, italic corrections, etc.
Another functionality of boxes is to convert between physical cursors (positions on the screen) and logical cursors (paths in the edit tree). Actually, boxes are also organized into a tree, which often simplifies the conversion. However, because of macro expansions and line and page breaking, the conversion routines may become quite intricate. Notice also that, besides a horizontal and vertical position, the physical cursor also contains an infinitesimal horizontal position. Roughly speaking, this infinitesimal coordinate is used to give certain boxes (such as color changes) an extra infinitesimal width.
All modifications of documents eventually break down into nine types of elementary modifications, which are declared in
Kernel/Types/modification.hpp
:
Replace a subtree by another tree.
Insert a string into a string leaf, or insert a sequence of children into a compound tree.
Remove a range of characters from a string leaf, or a range of children from a compound tree.
Split a leaf or a node into two consecutive ones (for instance, split a paragraph into two paragraphs).
The inverse operation: join two consecutive leaves or nodes.
Change the label of a node.
Insert a new node above a given subtree, which becomes one of its children.
The inverse operation: replace a node by one of its children.
A pseudo-modification which does not change the tree, but which records a cursor position (useful for undoing and for collaborative editing).
A modification consists of its type, a path and possibly a tree; it is constructed with the functions mod_assign, mod_insert, etc. The corresponding functions assign, insert, remove, split, join, assign_node, insert_node, remove_node and set_cursor (defined in
Kernel/Abstractions/observer.cpp
) take either a reference to a tree or a path in the_et, and call apply. From Scheme, the same operations are available as tree-assign, tree-insert, tree-remove, tree-split, tree-join, tree-assign-node, tree-insert-node and tree-remove-node (and their variants with an exclamation mark).
Every tree may carry an observer (the field obs of tree_rep; see
Kernel/Abstractions/observer.hpp
and
Data/Observers
). When an elementary modification is applied to a tree, its observers are first announced the modification, then notified of the precise change (notify_assign, notify_insert, etc.), the modification is performed, and the observers are finally informed that it is done. Several observers can be combined into lists. The most important ones are:
The inverse path observer (
ip_observer.cpp
), which allows each subtree of the_et to know its own location. Inverse paths are also stored in the boxes; they are the basis for the correspondence between the document and its typeset form.
The editor observer (
edit_observer.cpp
), which forwards the modifications to the editor. The editor updates the cursor position and notifies the typesetter, which invalidates the corresponding bridges, so that the modified parts will be typeset again during the next repaint.
The undo observer (
undo_observer.cpp
), which records the modifications into the archiver of the buffer (see
Data/History/archiver.hpp
). The archiver stores the history as patches (
Data/History/patch.hpp
), which can be inverted in order to undo or redo changes.
Tree pointers, tree positions and links, which keep track of locations in the document while it is being edited.
A typical modification goes through the following steps:
An input event, such as a key press, is transmitted by the graphical user interface to the editor, which calls the Scheme function keyboard-press. The keyboard bindings (defined with kbd-map in the Scheme code) or a menu entry then trigger an action, such as make-fraction, which is ultimately implemented by a C++ routine like edit_math_rep::make_fraction.
All modifications which this action makes to the edit tree break down into elementary modifications, which are applied using apply.
Before and after performing each elementary modification, the observers of the affected subtree are notified as explained above. In particular, all editors which view the same buffer are informed, the typesetter invalidates the modified parts, and the archiver records the modification.
Each user action like a keystroke or a mouse click is enclosed between calls of start_editing and end_editing. At the end, the pending modifications are confirmed as one step in the history; this determines the granularity of undo. Undo points can also be inserted explicitly using mark_start, mark_end and archive_state.
Finally, the event loop of the graphical user interface requests the typesetting of the invalid parts of the document and the repainting of the invalid regions of the screen.