GNU TeXmacsUser manualReferenceAboutEnglish
Scheme objects in C++

1.The class object

class object
(a Scheme value seen from C++)

Declared in

Scheme/scheme.hpp

as a CONCRETE handle; its representation tmscm_object_rep (

Scheme/Scheme/object.hpp

) holds the Guile value. Objects are built from the usual TeXmacs types:

object ()

The empty list (), also returned by null_object ().

object (bool), object (int), object (double)

Booleans and numbers.

object (const char*), object (string)

A Scheme string. Use symbol_object (s) for a symbol.

object (tree), object (url), object (modification), object (patch)

Black boxes.

object (path), object (list<string>), object (list<tree>), object (array<double>)

Lists of the converted elements.

The constructor object (void*) is declared but deliberately left undefined: without it, any pointer would silently be converted to bool and become #t.

Lists are built and taken apart with cons, list_object (one to three elements), as_list_object (array<object>), car, cdr and the compositions up to cadddr. The operators == and != use equal?, hash calls the Scheme function hash, and printing an object on cout or cerr calls write or write-err.

2.Protection from the garbage collector

A Guile value referenced only from C++ memory would be freed by the garbage collector. Every tmscm_object_rep therefore registers its value in the Scheme list object-stack, created by initialize_scheme: the constructor conses a cell (value) onto the front of that list and keeps a pointer handle to the new link; object_to_tmscm returns the value through this handle.

The destructor may run while the garbage collector is active, so it must not touch Scheme data. It only pushes the handle on the C++ list destroy_list. The next time an object is constructed, the pending handles are cleared (their value is replaced by ()) and the cleared links which follow them are removed from object-stack. Values held by destroyed objects thus stay alive until the next object is created.

3.Predicates and conversions

The predicates is_null, is_list, is_bool, is_int, is_double, is_string, is_symbol, is_tree, is_path, is_url, is_array_double, is_array_string, is_modification, is_patch and is_widget test the type of the value.

The conversions as_type are lenient: if the value has the wrong type, most of them return a neutral value instead of failing.

Neutral value on mismatch

as_bool (false), as_int (0), as_double (0.0), as_string and as_symbol (""), as_tree (an empty tree), as_list_string, as_list_tree, as_path (empty), as_url (url ("")), as_modification (an assignment of "" at the root), as_patch (an empty compound patch), as_widget (a nil widget).

Assertion on mismatch

as_array_object (the value must be a list), as_array_double, as_array_string.

Other

as_scheme_tree converts any value to a scheme_tree (see the glue); as_command and as_promise_widget wrap the value without checking it (see below).

tree_to_stree, stree_to_tree, string_to_object and object_to_string call the Scheme functions tree->stree, stree->tree, string->object and object->string; content_to_tree converts directly in C++.

4.Evaluating and calling Scheme code

eval (string expr)

Parses and evaluates a string (through eval_scheme). eval (object expr) instead calls the Scheme function eval on an expression which is already a Scheme value.

eval_file (string), exec_file (url)

Load a file. exec_file is the one used for the initialization files of the server and of new views (

Texmacs/Server/tm_server.cpp

,

Texmacs/Data/new_view.cpp

).

call (fun, a1, …, a4), call (fun, array<object>)

Apply a function. fun may be an object (a procedure) or a string or const char*. In the latter case the string is evaluated on each call (with eval_scheme), so it is usually the name of a function, but may be any expression which yields a procedure.

scheme_cmd (s)

Turns a string or an expression into a procedure without arguments, (lambda () s); this is the usual argument of exec_delayed.

All of these go through the error catching of the back-end: if the Scheme code fails, the error is printed on the console and the result is a pair (key . args) (see the interpreter). Combined with the lenient conversions, a failed as_int (call ("f")) silently yields 0.

5.Commands and promises

command as_command (object fun)
(a C++ command which calls a Scheme procedure)

Returns an object_command_rep, a command_rep holding the procedure. Applying the command without arguments calls the procedure without arguments; apply (cmd, args) (

Kernel/Abstractions/command.hpp

) passes the elements of the list args as arguments. This is how Scheme callbacks are passed to widgets, timers and dialogs.

From Scheme, object->command creates such a command, command-eval and command-apply run one, and commands created in C++ are passed to Scheme as black boxes.

as_promise_widget (fun) similarly wraps a procedure without arguments as a promise<widget>, whose evaluation calls the procedure and fails if the result is not a widget.

6.Delayed execution

exec_delayed (cmd) appends a procedure without arguments to a queue which is processed by the event loop, in the order of insertion. With Qt the queue is command_queue in

Plugins/Qt/qt_gui.cpp

; with the other toolkits it is the static queue of

Scheme/Scheme/object.cpp

, processed by exec_pending_commands from the interpose handler. Both implementations behave in the same way:

The Scheme macro delayed (

kernel/texmacs/tm-dialogue.scm

) is built on the second form: options such as :pause, :idle, :every, :while or :refresh compile to a procedure which returns the number of milliseconds left as long as its condition is not met. A comment in that file notes that :idle, which waits until the user has been inactive for some time, does not work in headless mode. clear_pending_commands empties the queue; it is called when TeXmacs quits.

7.Protected calls

protected_call (cmd), exported as protected-call, is used by the menu code (

kernel/gui/menu-widget.scm

,

menu-convert.scm

) to run the action of a menu entry. It brackets the call with before_menu_action and after_menu_action of the current editor, and, if a C++ exception is raised, calls cancel_menu_action and reports it with handle_exceptions (see fatal errors and crash handling).

8.Preferences

get_preference, set_preference and notify_preference are also declared in

scheme.hpp

, because the preferences are managed in Scheme. Before notify_preferences_booted has been called, they fall back on the C++ table of user preferences (get_user_preference, set_user_preference), which is what allows the main program to read preferences before the interpreter runs.

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