![]() |
Example of a plug-in with Scheme code | ![]() |
Consider the world plug-in in the directory
$TEXMACS_PATH/examples/plugins
This plug-in shows how to extend TeXmacs with some additional Scheme code in the file
world/progs/init-world.scm
In order to test the world plug-in, you should recursively copy the directory
$TEXMACS_PATH/examples/plugins/world
to $TEXMACS_PATH/plugins or $TEXMACS_HOME_PATH/plugins. When relaunching TeXmacs, the plug-in should now be automatically recognized: shortly after startup, the message Using world plug-in! is printed in the terminal from which TeXmacs was launched.
The file
init-world.scm
essentially contains the following code:
(plugin-configure world
(:require #t))
(when (supports-world?)
(display* "Using world plug-in!\n")) |
The configuration option :require specifies a condition which needs to be satisfied for the plug-in to be detected by TeXmacs (later on, this will for instance allow us to check whether certain programs exist on the system). The configuration is aborted if the requirement is not fulfilled.
Assuming that the configuration succeeds, the supports-world? predicate will evaluate to #t. In our example, the body of the when statement corresponds to some further initialization code, which just sends a message to the standard output that we are using our plug-in. In general, this kind of initialization code should be very short and rather load a module which takes care of the real initialization. Indeed, keeping the init-myplugin.scm files simple will reduce the startup time of TeXmacs. Notice also that initialization files are loaded lazily: TeXmacs only loads them once it has been idle for about one second, or as soon as it needs information about all plug-ins (for instance when opening the Insert\(\rightarrow\)Session menu).
A plug-in with more Scheme code usually puts it into separate modules in its progs directory, which is automatically added to the load path. For instance, a module progs/world-menus.scm starting with (texmacs-module (world-menus)) can be loaded from the initialization file using (import-from (world-menus)), or lazily using lazy-menu, lazy-keyboard or lazy-define. In order to add a menu to the main menu bar, such a module may extend the menu plugin-menu, as in
(menu-bind plugin-menu
(:require (in-world?))
(=> "World" ("Hello" (insert "Hello world")))) |
where in-world? is the predicate which is automatically defined by plugin-configure (see the summary of configuration options). Since the world plug-in does not provide sessions, this predicate would only hold inside documents whose programming language has been set to world; a menu which should always be present may use (:require (supports-world?)) instead.