![]() |
Mathematical and customized input | ![]() |
The TeXmacs meta-format allows application output to contain structured text like mathematical formulas. In a similar way, you may use general TeXmacs content as the input for your application. By default, only the text part of such content is kept and sent to the application as a string. Moreover, all characters in the range 0–31 are ignored, except for '\t' and '\n' which are transformed into spaces. There are two methods to customize the way input is sent to your application. First of all, the configuration option
(:serializer ,routine) |
specifies a scheme function for converting TeXmacs trees to string input for your application, thereby overriding the default method. The routine is called as (routine plugin-name tree), where tree is the input in Scheme form, and should return the string to be sent, including the final newline. This method allows you for instance to treat multi-line input in a particular way or to perform transformations on the TeXmacs tree. The default serializer is verbatim-serialize (in
utils/plugins/plugin-cmd.scm
); custom serializers usually start by calling (pre-serialize plugin-name tree), which converts mathematical content using the rules described below.
The :serializer option is a very powerful, but also a very abstract way to customize input: it forces you to write a complete input transformation function. In many circumstances, the user really wants to rewrite two dimensional mathematical input to a more standard form, like rewriting \(\frac{a}{b}\) to (a/b). Therefore, a second way for customizing the input is to use the command
(plugin-input-converters myplugin
rules) |
This command specifies input conversion rules for myplugin for “mathematical input” and reasonable defaults are provided by TeXmacs. Each rule is of one of the following two forms:
Given two strings symbol and conversion, the rule
(symbol conversion) |
specifies that the TeXmacs symbol symbol should be converted to conversion.
Given a symbol tag and a Scheme function routine, the rule
(tag routine) |
specifies that routine will be used as the conversion routine for tag. This routine should just write a string to the standard output. The Scheme function plugin-input may be used for the recursive transformation of the arguments of the tag.
The input plug-in demonstrates the use of customized mathematical input. It consists of the files
input/Makefile
input/packages/session/input.ts
input/progs/init-input.scm
input/progs/input-input.scm
input/src/input.cpp
The Scheme configuration code in
init-input.scm
is given by
(plugin-configure input
(:require (url-exists-in-path? "input.bin"))
(:launch "input.bin")
(:session "Input"))
(when (supports-input?)
(lazy-input-converter (input-input) input)) |
The predicate supports-input? tests whether the plug-in is indeed operational (that is, whether input.bin exists in the path). The conversion rules in the module (input-input) are added in a lazy manner. In other words, the file
input-input.scm
will only be loaded when we explicitly request to make a conversion. The conversion rules in
input-input.scm
are given by
(plugin-input-converters input
(frac input-input-frac)
(special input-input-special)
("<vee>" "||")
("<wedge>" "&&")) |
This will cause \(\vee\) and \(\wedge\) to be rewritten as || and && respectively. Fractions \(\frac{a}{b}\) are rewritten as ((a):(b)) using the routine
(define (input-input-frac t)
(display "((")
(plugin-input (car t))
(display "):(")
(plugin-input (cadr t))
(display "))")) |
In the additional style file
input.ts
we also defined some additional markup special:
<assign|special|<macro|body|<block|<tformat|<cwith|1|1|1|1|cell-background|pastel green>|<table|<row|<cell|body>>>>>>> |
This tag is rewritten using the special conversion rule
(define (input-input-special t)
(display "[[[SPECIAL:")
(plugin-input (car t))
(display "]]]")) |
As to the C++ code in
input.cpp
, the startup banner attempts to put the shell session in mathematical input mode:
cout << DATA_BEGIN << "verbatim:";
cout << DATA_BEGIN << "command:(if (not (session-math-input?)) "
<< "(toggle-session-math-input))" << DATA_END;
cout << "Convert mathematical input into plain text";
cout << DATA_END;
cout.flush (); |
However, the command session-use-math-input no longer exists in the current version of TeXmacs, so this command fails. Mathematical input is now switched on by the user, using the entry Mathematical input of the input options of the session (this entry is only available for plug-ins which declared input converters, and calls toggle-session-math-input). The default for new sessions of a plug-in myplugin is taken from the boolean preference myplugin-math-input.
In the main loop, we content ourselves to reproduce the input as output:
char buffer[100];
cin.getline (buffer, 100, '\n');
cout << DATA_BEGIN << "verbatim:";
cout << buffer;
cout << DATA_END;
cout.flush (); |