GNU TeXmacsUser manualReferenceAboutEnglish
The glue and how to extend it

1.Declaring glue routines

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

2.The generator

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

  1. checks each argument with TMSCM_ASSERT_TYPE, where TYPE is the argument type in upper case;

  2. converts it with tmscm_to_type;

  3. calls prefixcpp-name (in1, in2, …);

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

3.Types

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:

Immediate values

bool, int, uint (arguments only), long (results only), double, string, tree_label (a symbol).

Black boxes

tree, url (a string is also accepted), observer, widget, promise_widget, command, modification, patch.

Structured values

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.

Generic values

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

.

4.Regenerating the glue

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:

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

5.Adding a glue routine, step by step

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

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

    ).

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

  4. Rebuild TeXmacs. The new procedure is defined when the interpreter starts, before

    init-texmacs.scm

    is loaded, and can be called from any module.

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

6.On the Scheme side: define and tm-define

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:

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.

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