![]() |
The glue and how to extend it | ![]() |
The C++ routines which are visible from Scheme are listed in three declaration files in
Scheme/Glue/
. Each of them is a single call of the macro build:
(build
"get_server()->" ; prefix of every call
"initialize_glue_server" ; name of the generated init function
(show-header show_header (void bool))
(kbd-pre-rewrite kbd_pre_rewrite (string string))
…) |
Each entry has the form (scheme-name cpp-name (return-type argument-type …)). The three files differ by their prefix:
build-glue-basic.scm
No prefix: free functions. This is the largest file (about 820 routines): trees, paths, URLs, files, buffers, views and windows, widgets, conversions, fonts, databases, and so on.
build-glue-editor.scm
Prefix get_current_editor()->: methods of the current editor (about 310 routines), which must be declared in the abstract class editor_rep (
Edit/editor.hpp
).
build-glue-server.scm
Prefix get_server()->: methods of the server (about 40 routines), declared in server_rep (see the server classes).
Scheme/Glue/build-glue.scm
defines build as a macro which, when the declaration file is loaded, prints C++ code on the standard output. For each entry it produces a function named after the Scheme name: tmg_ followed by the name in which - becomes _, ? becomes P, ! becomes S, < becomes F, > becomes 2, = becomes Q, and a final * becomes _dot. For instance, path-exists? becomes tmg_path_existsP, cpp-path->tree becomes tmg_cpp_path_2tree and cursor-path* becomes tmg_cursor_path_dot. The generated function
checks each argument with TMSCM_ASSERT_TYPE, where TYPE is the argument type in upper case;
converts it with tmscm_to_type;
calls prefixcpp-name (in1, in2, …);
converts the result with type_to_tmscm, or returns TMSCM_UNSPECIFIED for void.
For example, the entry (kbd-pre-rewrite kbd_pre_rewrite (string string)) of
build-glue-server.scm
yields
tmscm
tmg_kbd_pre_rewrite (tmscm arg1) {
TMSCM_ASSERT_STRING (arg1, TMSCM_ARG1, "kbd-pre-rewrite");
string in1= tmscm_to_string (arg1);
// TMSCM_DEFER_INTS;
string out= get_server()->kbd_pre_rewrite (in1);
// TMSCM_ALLOW_INTS;
return string_to_tmscm (out);
} |
At the end, the generator emits the initialization function named in the declaration file, which installs every routine with tmscm_install_procedure (all arguments are required; there are no optional or rest arguments). initialize_glue (
Scheme/Scheme/glue.cpp
) calls the three initialization functions, after installing by hand the predicates tree?, tm?, observer?, url?, modification?, patch? and blackbox?. The generated files are not compiled separately:
glue.cpp
includes them after all the headers that the glued routines need.
A type name t can be used in a declaration as soon as
glue.cpp
(or
guile_tm.hpp
) provides TMSCM_ASSERT_T, tmscm_to_t (for arguments) and t_to_tmscm (for results). The types currently used are:
bool, int, uint (arguments only), long (results only), double, string, tree_label (a symbol).
tree, url (a string is also accepted), observer, widget, promise_widget, command, modification, patch.
path (a list of integers), list_string and list_tree (results only), array_int, array_SI (results only), array_double, array_array_array_double (arguments only), array_string, array_tree, array_url, array_widget (arguments only), array_patch (arguments only), array_path.
object (any value, not checked), scheme_tree and content.
The last two deserve a comment. A scheme_tree is a tree which encodes a Scheme expression: lists become tuples, symbols become strings, strings are stored quoted, booleans become #t or #f, integers become their decimal representation and trees are converted with tree_to_scheme_tree; anything else, including floating point numbers, becomes "?". A content argument accepts a string, a tree, or a Scheme expression (label child …) whose children are again content, and converts it to a tree; results of type content are trees.
To add a new type, define the three items above in
glue.cpp
(and usually a tmscm_is_t predicate). For a C++ class which only needs to be passed around, the easiest is to box it, as is done for command:
tmscm command_to_tmscm (command o) {
return blackbox_to_tmscm (close_box<command> (o)); }
command tmscm_to_command (tmscm o) {
return open_box<command> (tmscm_to_blackbox (o)); } |
together with a tmscm_is_t test on type_box and the TMSCM_ASSERT_T macro. If values of the new type should print nicely, add a case to print_blackbox in
guile_tm.cpp
.
The generated files
glue_basic.cpp
,
glue_editor.cpp
and
glue_server.cpp
are part of the repository, and the CMake build compiles them as they are: it has no rule to regenerate them. After changing a declaration file, regenerate the corresponding file by hand, in the directory
Scheme/Glue/
:
./build-glue build-glue-basic.scm glue_basic.cpp [guile-binary] |
This needs a Guile interpreter (guile by default, or the binary given as third argument or in GUILE_BIN). In the traditional build, make GLUE in
src/src/
regenerates all three files with the GUILE_BIN found by configure.
build-glue first runs build-auto-doc, which regenerates two files from the three declaration files:
TeXmacs/progs/prog/glue-symbols.scm
(
make-apidoc-module.scm
), the list of all glued symbols returned by all-glued-symbols, used for the completion of Scheme code (
prog/scheme-autocomplete.scm
);
TeXmacs/doc/devel/scheme/api/glue-auto-doc.en.tm
(
make-apidoc-doc.scm
These two files, too, are committed, and should be regenerated together with the glue so that they stay in sync. At the time of writing, all three generated C++ files and
glue-symbols.scm
agree with the declarations (1181 routines in total).
Write the C++ routine. For a free function, declare it in a header and make sure that this header is included in the “Gluing” section near the end of
Scheme/Scheme/glue.cpp
(many headers are already there). For an editor method, add a pure virtual declaration to editor_rep in
Edit/editor.hpp
and implement it in the appropriate edit_*_rep class; for a server method, likewise in server_rep and the classes of Texmacs/. Use only argument and return types from the list above, or add a new type.
Add an entry to the right declaration file, for instance (for a hypothetical routine)
(buffer-word-count buffer_word_count (int url)) |
Follow the naming conventions of Scheme: ? for predicates, ! for destructive operations, and the prefix cpp- for low level routines that are meant to be wrapped by a Scheme function of the same name without the prefix (there are about 50 of these, such as cpp-buffer-close, which is wrapped by buffer-close in
texmacs/texmacs/tm-server.scm
).
Regenerate the glue as explained above and check the diff: the generated files,
glue-symbols.scm
and glue-auto-doc.en.tm should all have changed.
Rebuild TeXmacs. The new procedure is defined when the interpreter starts, before
init-texmacs.scm
is loaded, and can be called from any module.
If the routine is part of the user level API, wrap it with tm-define on the Scheme side to give it a synopsis, argument descriptions, or contextual variants.
Scheme code in TeXmacs is organized in modules declared with texmacs-module (see the module system and lazy definitions). Within a module, define creates a private definition and define-public an exported one. tm-define (
kernel/texmacs/tm-define.scm
) is different:
it always defines the function in the global module texmacs-user, so that it is visible everywhere;
a second tm-define of the same name does not replace the function but overloads it: the new definition may carry conditions such as (:mode in-math?) or (:require …), and falls back on the previous definition (available as former) when they are not met (see contextual overloading);
it accepts properties such as :synopsis, :argument, :proposals, :secure, :interactive, :check-mark or :balloon, which are used by menus, interactive commands and the documentation; tm-property adds such properties to an existing function.
lazy-define declares that a function is defined in a module which is only loaded when the function is first called. Glue routines are plain procedures: they have none of these properties unless a tm-define or tm-property is written for them.