GNU TeXmacsUser manualReferenceAboutEnglish
The module system and lazy definitions

As explained above, each Scheme file inside TeXmacs or one of its plug-ins corresponds to a TeXmacs module. The individual TeXmacs modules are usually grouped together into an internal or external module, which corresponds to a directory on your hard disk.

Any TeXmacs module should start with an instruction of the form

(texmacs-module name
  (:use submodule-1 … submodule-n))

The name of the module is a list which corresponds to the location of the corresponding file. More precisely, TeXmacs searches for its modules in the directory $TEXMACS_PATH/progs, in the directories of $GUILE_LOAD_PATH, in your personal directory $TEXMACS_HOME_PATH/progs (that is, ~/.TeXmacs/progs), and in the progs subdirectories of the plug-ins (this path is used by S7 too). For instance, the module (math math-edit) corresponds to the file

    $TEXMACS_PATH/progs/math/math-edit.scm

The option (:inherit module-1 … module-n) can be used instead of :use in order to import the given modules and re-export all their public symbols. Outside a module declaration, the same effects are obtained with (use-modules module-1 … module-n) resp. (inherit-modules module-1 … module-n).

The user should explicitly specify all submodules on which the module depends, except those modules which are loaded by default, i.e. the language extensions in the directory

    $TEXMACS_PATH/progs/kernel

(loaded by

init-kernel.scm

). The utilities in $TEXMACS_PATH/progs/utils/library are not all loaded at boot time: use for instance (:use (utils library cursor)).

All symbols which are defined inside the module using define or define-macro are only visible within the module itself. In order to make the symbol publicly visible you should use tm-define or tm-define-macro, or define-public, define-public-macro and provide-public (the latter only defines the symbol when it is not yet defined). Because of implementation details for the contextual overloading system, a symbol declared with tm-define becomes visible inside all other modules. For define-public, this depends on the implementation of Scheme: with S7, the symbol is published for all modules (

boot-s7.scm

), while with Guile it is an ordinary export of the module, only visible in the modules which :use it. Code which works with S7 without a :use may thus fail with Guile: always import the modules you depend on.

Because the number of TeXmacs modules and plug-ins keeps on growing, it is inefficient to load all modules when booting. Instead, initialization files are assumed to declare the provided functionality in a lazy way: whenever the functionality is explicitly needed, TeXmacs is triggered to load the corresponding modules (if this was not already done). In addition, TeXmacs may load some of these modules during spare time, when the computer is waiting for user input. Indeed, this helps increasing the reactivity of TeXmacs at the first use of the functionality.

For instance, assume that you defined a large new editing function foo-action inside the module (foo-edit). Then your initialization file init-foo.scm would typically contain a line

(lazy-define (foo-edit) foo-action)

Similarly, lazy keyboard shortcuts and menus for foo might be defined using

(lazy-keyboard (foo-kbd) in-foo-mode?)
(lazy-menu (foo-menu) foo-menu)

Note that lazy-define only creates its stub when the function is not yet defined, and that (lazy-define-force name) loads the modules which promised name. The initialization files also use the following lazy declarations:

For more concrete examples, we recommend the user to take a look at the standard initialization file

init-texmacs.scm

.

On the negative side, the mechanism for lazy loading has the important consequence that you can no longer make assumptions on when a particular module is loaded. For instance, when you attempt to redefine a keyboard shortcut in your personal initialization file, it may happen that the standard definition is loaded after your “redefinition”. In that case, your redefinition remains without consequence.

For this reason, TeXmacs also provides the instruction import-from to force a particular module to be loaded. Similarly, the commands (lazy-keyboard-force #t), lazy-plugin-force, etc. may be used to force all lazy keyboard definitions resp. plug-ins to be loaded (without argument, lazy-keyboard-force only loads the keyboard definitions of the modes which are currently active). In other words, the use of laziness forces to make implicit dependencies between modules more explicit.

In the case when you want to redefine keyboard shortcuts, the contextual overloading system gives you an even more fine-grained control. The rule is simple: among the definitions whose conditions (such as the mode) hold, the most recent one wins; there is no ordering by specificity of the modes. For instance, assume that the keyboard shortcut XXX has been defined twice, both in general and in math mode. After calling (lazy-keyboard-force #t), a new general definition of the shortcut overrides both of them, also in math mode. To keep the special behaviour in math mode, define it again after your general definition:

(lazy-keyboard-force #t)
(kbd-map ("x x x" action))
(kbd-map (:mode in-math?) ("x x x" math-action))

Conversely, a definition loaded after yours, for instance by a module which had not been loaded yet, overrides your definition wherever its conditions hold.

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