![]() |
Editing modes on the Scheme side | ![]() |
Almost all of the interactive behaviour of TeXmacs is written in Scheme. When the user presses ↩, the C++ editor only finds out which command is bound to the key; the command itself, and the decision of what ↩ means at the current position (a new paragraph, a new item of a list, a new row of a table, the evaluation of a session input, …), are taken by Scheme code which depends on the mode (text, mathematics, source code, …) and on the tags around the cursor.
This chapter describes how this code is organized: how modes are defined and tested, how functions, keyboard shortcuts and menus are specialized for a mode or a context, which generic hooks the editing modes implement, and what the main modes provide: text, mathematics, tables and the dynamic markup (folding, switches, slides, sessions, scripts, spreadsheets and animations).
The chapter builds on the following documents, which it does not repeat:
contextual overloading and the module system and lazy definitions, for the principles of tm-define and of the lazy loading of modules;
the TeXmacs editing model and the other chapters on programming routines for editing, for the Scheme interface to trees, paths and the cursor, and for a first example of a structured editing routine;
the DRD from Scheme, for tag groups (define-group) and for the DRD queries used by the context predicates;
the event loop, for the way the C++ editor receives keys and rebuilds menus;
sessions, connections and links, for the communication with the plug-ins behind sessions, scripts and spreadsheets.
An editing mode is a set of Scheme modules under $TEXMACS_PATH/progs/mode/ which extend generic definitions. Three mechanisms make the extension possible:
A mode is a named predicate, such as in-math?, declared with texmacs-modes in
kernel/texmacs/tm-modes.scm
. Modes form a hierarchy: in-math-in-session? is a sub-mode of both in-math? and in-session?.
Functions (tm-define), keyboard shortcuts (kbd-map) and menus (menu-bind, tm-menu) may be redefined under a condition, either a mode (:mode) or an arbitrary predicate (:require). The most recent definition whose condition holds wins.
Generic code calls a small set of functions, such as (kbd-enter t shift?) or (structured-insert-horizontal t forwards?), on the focus tree t; their default implementations recurse to the parent tree. A mode implements the behaviour of its tags by redefining the hooks for those tags.
For instance, when the user presses ↩ inside an itemize list, the key is bound (in
generic/generic-kbd.scm
) to (kbd-return), which calls (kbd-enter (focus-tree) #f). The focus tree is the innermost tag around the cursor, here the itemize tag. Among all definitions of kbd-enter, the one in
text/text-edit.scm
with the condition (list-context? t) applies, and inserts a new item. Inside a table which is itself inside the list, the table definition in
table/table-edit.scm
applies first, because the focus tree is then the table.
The relevant directories (relative to $TEXMACS_PATH/progs/) are:
kernel/texmacs/tm-modes.scm
The macro texmacs-modes, the standard modes, the sub-mode test and lazy-initialize.
kernel/texmacs/tm-define.scm
Conditional definitions: tm-define, tm-property and their options.
kernel/gui/kbd-define.scm
The keyboard tables, kbd-map, kbd-wildcards and lazy-keyboard.
kernel/gui/menu-define.scm
Menus: menu-bind, tm-menu, lazy-menu.
generic/
Behaviour common to all modes: the generic hooks (
generic-edit.scm
), the basic keyboard (
generic-kbd.scm
), the focus menus and focus icon bar (
generic-menu.scm
) and the format, insert and document menus.
Text mode: document titles, sections, lists, enunciations, floats and the language specific keyboards (
text/chinese/
,
text/cyrillic/
, …).
Mathematics: the large mathematical keyboard, brackets, scripts, equations and the semantic editing mode.
Tables and cells.
dynamic/
Folding, switches, overlays and slides; sessions and programs; scripts, plots and converters; spreadsheets; animations.
texmacs/keyboard/
Keyboard prefixes and wildcards common to all modes (
prefix-kbd.scm
) and the LaTeX style shortcuts (
latex-kbd.scm
).
init-texmacs.scm
The lazy declarations which tell when each mode module is loaded.
Other modes follow the same pattern and are not described here, notably the source mode (source/), the programming languages (
prog/
, see syntax highlighting and programming languages), graphics (graphics/, see the graphics editor) and the bibliographic database (database/, see the database and bibliographies).