![]() |
Modes, conditional definitions and lazy loading | ![]() |
Modes are declared with the macro texmacs-modes of
kernel/texmacs/tm-modes.scm
. Each entry has the form
(name% test parent% …) |
where the name must end with %, test is a Scheme expression or #t, and the optional parents are other modes. The entry defines a public predicate whose name is obtained by replacing the final % by ?; the predicate holds when the test and all parent predicates hold. For instance, the standard modes include
(texmacs-modes
(in-text% (and (== (get-env "mode") "text") (not (in-graphics?))))
(in-math% (and (== (get-env "mode") "math") (not (in-graphics?))))
(in-table% (and (inside? 'table) (not (in-graphics?))))
(in-session% (and (or (inside? 'session) (inside? 'program))
(not (in-graphics?))))
(in-math-in-session% #t in-math% in-session%)
(in-sem-math% (== (get-preference "semantic correctness") "on") in-math%)
…) |
so that (in-math-in-session?) is (and (in-math?) (in-session?)). Besides the predicate, the macro
associates the predicate with both symbols in-math% and in-math? (set-symbol-procedure!), which is how texmacs-in-mode? evaluates a mode given by name;
records each parent relation as a rule of the logic programming layer, so that (texmacs-submode? what of) can decide whether one mode is a sub-mode of another. Every mode is a sub-mode of always%, and prevail% is a sub-mode of every mode.
The predicates are defined in the module texmacs-user, so they are visible everywhere.
tm-modes.scm
defines the modes for the editing context (in-source?, in-text?, in-math?, in-prog?, in-hybrid?, in-table?, in-session?, …), for the document style (in-std?, in-beamer?, in-manual?, in-database?, …), for the current language (in-french?, in-math-german?, …), for the programming language (in-prog-python?, …), for the look and feel and for the input mode of the editor (search-mode?, spell-mode?, …). Plug-ins add their own modes in the same way.
Mode predicates are evaluated often, typically for every key press and every menu update, so they must be cheap and free of side effects.
A function defined with tm-define may be redefined any number of times, in any module, under a condition given by the option :mode (a mode predicate) or :require (an arbitrary expression over the arguments):
(tm-define (kbd-enter t shift?)
(:require (list-context? t))
(if shift? (make-return-after) (make-item))) |
The implementation in
kernel/texmacs/tm-define.scm
is simple: a conditional redefinition replaces the global binding by a function of the form
(lambda (t shift?)
(if (list-context? t)
(if shift? (make-return-after) (make-item))
(former t shift?))) |
where former is the previous binding. Consequently
the most recently loaded definition is tried first, so the order in which modules are loaded matters, and a general definition must be loaded before the specific ones;
several conditions (several :mode and :require options) are combined with and;
the body of a definition may call former explicitly to extend rather than replace the previous behaviour; this is how
math/math-sem-edit.scm
wraps kbd-insert, kbd-backspace and make;
a first definition with a condition gets an empty former and causes the warning “conditional master routine” on the console.
Other options attach properties instead of conditions (:synopsis, :argument, :proposals, :check-mark, :balloon, :interactive, :secure, :applicable, …). They are used by menus and by the interactive commands. tm-property attaches such properties to an existing function without redefining it.
Keyboard shortcuts are declared with kbd-map (
kernel/gui/kbd-define.scm
):
(kbd-map
(:mode in-math?)
("math:small (" (math-bracket-open "(" ")" #f))
…
…) |
Each entry maps a key sequence to either a string (a shorthand which is inserted with kbd-insert) or to code, which is wrapped in a procedure. The options (:mode pred) and (:require expr) at the start of the block become a list of condition procedures which is stored with each binding. The table kbd-map-table maps every key sequence to a list of (conditions, binding) pairs; a new binding for the same key and the same conditions replaces the old one, other bindings are put in front of the list. kbd-find-key-binding returns the first binding whose conditions all hold, so here too the most recently loaded definition wins.
Before a binding is stored, the key sequence is rewritten with the pre-wildcards of the server (kbd_pre_rewrite, see the partial server tm_config_rep). This is how symbolic prefixes such as math:small, structured:cmd or table are turned into concrete modifier combinations; the prefixes are declared with kbd-wildcards in
texmacs/keyboard/prefix-kbd.scm
and depend on the look and feel. The suffix var stands for the variant key (by default ⇥): "a var" is the binding for A followed by ⇥.
The full path of a key press is:
the toolkit calls edit_interface_rep::handle_keypress, which calls the Scheme function keyboard-press (
kernel/gui/kbd-handlers.scm
), which by default calls back the editor with key-press;
the editor (
Edit/Interface/edit_keyboard.cpp
) appends the key to the pending shortcut and asks the server for a binding (get_keycomb), which ends in kbd-find-key-binding;
if a binding is found, its command is executed, or its shorthand is inserted with (kbd-insert s);
otherwise, a single printable character is inserted with (kbd-insert s).
kbd-insert is itself overloaded: in math mode (
math/math-edit.scm
) it removes a space typed before an infix operator, and in semantic math mode it checks the syntactic correctness of the result. The details of the C++ side are in keyboard events.
A menu is a zero argument function which returns a menu description: (menu-bind name . items) expands into (tm-define (name) …), and tm-menu defines menus with arguments, typically the focus tree. Both accept the same options as tm-define, so menus are overloaded exactly like functions:
(menu-bind texmacs-mode-icons
(:mode in-database?)
(link db-insert-icons)
…) |
replaces the mode dependent icon bar inside bibliographic databases. The mode dependent parts of the interface are reached from a few fixed entry points:
The mode dependent icon bar (
texmacs/menus/main-menu.scm
), which links to text-icons, math-icons, prog-icons, … according to the mode.
generic/format-menu.scm
), which links to text-format-menu or math-format-menu.
The focus icon bar and the Focus menu (
generic/generic-menu.scm
), which are built from the focus tree, see the generic structured editing hooks.
The side and bottom tool areas, see widgets from Scheme.
The editor rebuilds these menus whenever the context may have changed; how the result is cached is described in windows, menus, dialogs and embedded widgets.
Most mode modules are not loaded at startup.
init-texmacs.scm
only declares when they are needed, with the macros of lazy definitions:
(lazy-keyboard (math math-kbd) in-math?)
(lazy-keyboard (math math-sem-edit) in-sem-math?)
(lazy-menu (math math-menu) math-format-menu math-format-icons …)
(lazy-initialize (math math-menu) (in-math?))
(lazy-define (math math-edit) brackets-refresh) |
Registers a module with a mode. Every key lookup first calls lazy-keyboard-force, which loads the modules of all modes which currently hold. In addition, each module is loaded unconditionally after 250 ms of idle time.
Declares the menus provided by a module (with lazy-define) and loads the module after 500 ms of idle time.
Declares functions provided by a module; the first call loads the module.
Loads a module when a predicate holds at the next call of lazy-initialize-force (which happens before the menus and toolbars of a window are rebuilt, see tm_window_rep::menu_main), or after 5 s of idle time.
Lazy loading interacts with conditional definitions: since the most recently loaded definition is tried first, a module which is loaded late takes precedence over modules loaded earlier, whatever the order of the declarations in
init-texmacs.scm
. A module therefore declares the modules it extends in its :use clause, which forces them to be loaded first; for example
math/math-kbd.scm
uses
generic/generic-kbd.scm
,
math/math-edit.scm
and
table/table-edit.scm
.
The modules of a mode follow a naming convention, which is also the order in which they usually depend on each other:
Tag groups (define-group, define-alternate) describing the markup of the mode: which tags are variants of each other, which are lists, sections, enunciations, … The context predicates of the edit module are written in terms of these groups. See tag groups.
Editing routines: context predicates (list-context?, script-context?, …), commands called from menus and keyboard (make-section, math-bracket-open, …), and the redefinitions of the generic hooks for the tags of the mode.
Keyboard shortcuts, in kbd-map blocks conditioned on the mode.
Menus and icon bars, including the redefinitions of the focus menus for the tags of the mode.
Scheme functions which are called while tags of the mode are typeset, through the extern primitive of the style files (for instance screens-index in
dynamic/fold-markup.scm
, or the callbacks of spreadsheets in
dynamic/calc-markup.scm
).
Documentation hooks, such as the help about tags shown by the focus menu.
Dialogs and side tools.
Speech input for the mode (
kernel/gui/speech-define.scm
), loaded together with the keyboard.
Not every mode has every file, and some files mix several roles (tag groups for tables are declared in
table/table-edit.scm
, for instance).
A redefinition with tm-define which repeats the condition of an earlier one completely hides it; this happens for instance with the standard-parameters of the input, output, errput and textput tags, which are defined both in
dynamic/session-edit.scm
and in
dynamic/program-edit.scm
(the bodies currently compute the same result).
Because the order of loading determines the precedence, the same sequence of user actions can behave differently depending on whether a lazy module has already been loaded. Code which overrides a definition from a lazily loaded module should :use that module.
A definition such as (tm-define (make . l) …) in
math/math-sem-edit.scm
has no condition: once the module is loaded, it wraps every call of make in the whole program. It tests the semantic correctness preference on each call.
kernel/gui/kbd-define.scm
accepts a bare symbol at the start of a kbd-map block (kbd-map-body), but turns it into the condition list (0 symbol), whose first element is not a procedure; no keymap uses this form, and it would fail at the first lookup of such a key.