GNU TeXmacsUser manualReferenceAboutEnglish
The Scheme widget language and its interpreter

1.Overview

The Scheme part of the widget system lives in

progs/kernel/gui/

:

gui-markup.scm

the style constants widget-style-* and the low-level macros $list, $when, $->, $input, … which build menu items; also the $form macros and, in its second half, macros for generating documents ($para, $itemize, …), which are not discussed here.

menu-define.scm

the user-level macros menu-bind, tm-menu, tm-widget, menu-dynamic and the translator gui-make with its table gui-make-table.

menu-widget.scm

the grammar of menu items, the interpreter make-menu-widget which turns them into C++ widgets, menu-expand, the top-level window functions (top-window, dialogue-window, …) and the side tools (tm-tool).

menu-convert.scm

an alternative, experimental interpreter which renders menu items as TeXmacs markup.

menu-test.scm

test widgets.

How to write menus and widgets is explained in “Extending the graphical user interface”. Here we describe what happens behind the scenes.

2.Three representations

It is important to distinguish three stages.

  1. Source. The body of a menu-bind, tm-menu or tm-widget is written in the widget language, for instance

    (tm-widget (my-widget cmd)
      (hlist
        (text "Name:")
        (input (set! my-name answer) "string" (list my-name) "20em")
        // (explicit-buttons ("Ok" (cmd "ok")))))

    At macro expansion time, each keyword is translated by gui-make into a call of a $-macro of

    gui-markup.scm

    .

  2. Menu items. When (my-widget cmd) is called, the $-macros build a plain list, the menu item, such as

    ((hlist (text "Name:")
            (input #<procedure> "string" #<procedure> "20em")
            (glue #f #f 5 0)
            (style 32 ("Ok" #<procedure>))))

    The parts of the widget that must be recomputed later (actions, predicates, proposals) are closures. The syntax of menu items is specified by the grammar :menu-item given with define-regexp-grammar at the beginning of

    menu-widget.scm

    .

  3. C++ widgets. The interpreter make-menu-widget walks through the menu item and calls the glued constructors (widget-hlist, widget-text, widget-input, …), producing a single C++ widget.

Menu items are also what the C++ kernel manipulates: the strings passed to menu_main, menu_icons or side_tools, such as "(horizontal (link texmacs-menu))", are quoted menu items, not source in the widget language. This is why both levels have keywords like link, dynamic or vertical.

3.The definition macros

All definition forms end up in menu-dynamic:

(tm-define-macro (menu-dynamic . l)
  ‘($list ,@(map gui-make l)))
(menu-bind name . body)
(define a menu without arguments)

(tm-menu (name . args) . body)

(tm-widget (name . args) . body)
(define a menu or widget)

These expand into tm-define of a function returning (menu-dynamic . body). Leading options such as (:require pred) are passed to tm-define, so that menus can be overloaded contextually like any other tm-defined function, and (former) inside the body refers to the previous definition. tm-menu and tm-widget are currently identical; menu-bind defines a function without arguments. The plain variants define-menu and define-widget use define.

(lazy-menu module . names)
(lazy loading)

Declares that the menus names are defined in module, which is loaded on first use (via lazy-define), or after an idle delay. It is used extensively in

init-texmacs.scm

. Before building the bars of a window, the kernel evaluates (lazy-initialize-force).

$list evaluates its arguments and flattens the sublists of the form (list . items) (with gui-normalize): this is how a keyword can expand into zero, one or several items.

4.The translation gui-make

gui-make maps each keyword of the widget language to a function of gui-make-table which returns a $-form. Symbols and strings are handled directly: --- becomes the separator $---, / and | the vertical separator, ===, //, >> and similar symbols become glue items, and a list whose head is a string or a label, as ("Ok" (cmd "ok")), becomes a button ($> "Ok" (cmd "ok")), which evaluates to (list "Ok" (lambda () (cmd "ok"))). New keywords can be added with extend-table, as

menu-define.scm

itself does for section-tabs, pick-color and pick-background.

The main subtlety is when the various parts are evaluated. The following table lists the translations of the control keywords.

Keyword Translation Evaluated
(link m) (link m) when the widget is built: the function m is called
(dynamic expr) splice the value of expr when the menu item is built
(if pred . items) (if thunk . items) when the widget is built: items omitted if false
(when pred . items) (when thunk . items) when the widget is built: items greyed if false
(assuming pred . items) $when: splice or nothing when the menu item is built
(for (x l) . items) (for fun thunk) when the widget is built
(loop (x l) . items) splice append-map when the menu item is built
let, let*, with, cond, receive the Scheme construct around menu-dynamic when the menu item is built
(promise expr) (promise thunk) when the widget is built; must return a menu item
(-> label . items), (=> …) (-> label . items) items built with the parent; submenu widget built when opened

In particular, when and assuming are not synonyms: the former greys out its items (by adding WIDGET_STYLE_INERT and WIDGET_STYLE_GREY) and re-evaluates the predicate each time the widget is built, while the latter includes or omits the items once and for all when the menu item is computed. (The macro names are swapped: when is translated with $assuming, and assuming with $when.)

Leaf keywords take closures as well. For example (input cmd type proposals width) is translated into

(list 'input (lambda (answer) cmd) type (lambda () proposals) width)

which explains why the result of input widgets is available as the variable answer in the user code. toggle, enum, choice, choices, filtered-choice, color-input, tree-view, texmacs-input and texmacs-output follow the same pattern (see their $-macros in

gui-markup.scm

). Style keywords (inert, explicit-buttons, bold, grey, mono, verb, plain-style) become (style n . items), where n is a widget-style-* constant; a positive n is or-ed into the current style, a negative one removed from it. The layout helpers padded, centered and bottom-buttons are expanded into vlist/hlist combinations with glue.

5.The interpreter make-menu-widget

(make-menu-widget p style)
(menu item to widget)

Transforms the menu item p into a C++ widget, using the initial style style (usually 0). The item must produce exactly one widget, which is why callers wrap menus into (vertical …) or (horizontal …). Malformed items are reported and replaced by a text widget displaying "Error".

The recursive work is done by (make-menu-items p style bar?), which returns a list of widgets. The flag bar? is true inside horizontal containers (horizontal, hlist, minibar) and changes the rendering of buttons (icon bar buttons show a pressed state instead of a check mark). The dispatch works as follows:

The builders of the dynamic keywords (if, when, for, mini, link, dynamic, promise) evaluate their closures at this moment and recurse, which is why a widget reflects the state of the editor at the time it is built.

5.1.Menu entries

make-menu-entry is the most elaborate builder. Given an entry (label action), it inspects the action. If the action is a closure of the form (lambda () (f args …)) (this is tested by promise-source), the properties of the tm-defined function f are used:

The label is turned into a widget by make-menu-label (widget-text after translation, widget-xpm for icons, widget-color for color samples, …) and the action into a C++ command by

(define (delay-command cmd)
  (object->command (lambda () (exec-delayed cmd))))

(define-macro (make-menu-command cmd)
  ‘(delay-command (lambda () (protected-call (lambda () ,cmd)))))

so that the action is executed later, from the main loop, surrounded by the editor's before_menu_action and after_menu_action (see “Windows, the main TeXmacs widget and the flow of events”). Call-backs with arguments are wrapped by menu-protect, which does the same for any number of arguments. Finally the entry becomes (widget-menu-button label command check shortcut style).

5.2.Submenus and lazy widgets

make-menu-submenu does not build the contents of a submenu. It passes a promise to widget-pulldown-button or widget-pullright-button:

(object->promise-widget
  (lambda () (make-menu-widget (list 'vertical items) style)))

The port evaluates this promise each time the submenu is about to be shown, so that every opening of a menu reflects the current state (the closures inside items are re-run). Only the list items itself is computed together with the parent menu; to make the list of entries of a submenu dynamic, one uses link or a dynamic call inside it.

6.Expansion and caching

Building widgets is expensive, and the menus and icon bars of the main window are requested after almost every change. The kernel therefore only rebuilds them when their expansion changes.

(menu-expand p)
(closure-free form of a menu item)

Returns a copy of p in which links, dynamic parts, if, for and promise items are evaluated and spliced, when predicates are replaced by #t or #f, the current values of input fields, toggles, enumerations and choices are evaluated, and all remaining closures are replaced by their source code (replace-procedures, using procedure-source). Submenus (->, =>) and refresh widgets are not expanded. The per-keyword behaviour is given by menu-expand-table.

Two menu items with equal expansions produce equivalent widgets. The C++ function tm_window_rep::get_menu_widget uses the expansion as the key of a per-window cache and skips the update when the expansion of a bar did not change. The predicate (cache-menu? r) forbids caching expansions which contain an input field (whose state lives in the toolkit widget).

A consequence is that a menu depending on some state which is invisible in its expansion (for instance a closure that reads a global variable, compared by its source) will not be updated automatically. In such cases, use an explicit refresh/refreshable widget, or make the state visible, for instance by testing it in an if or when.

7.Refresh widgets

Widgets inside dialogs and side tools are not rebuilt by update_menus. Their dynamic parts use one of three keywords, which map to the C++ constructors refresh_widget and refreshable_widget.

(refresh name kind)
(refresh by name)

Becomes (widget-refresh "name" "kind") (kind is written as a symbol and defaults to name). The C++ side evaluates (vertical (link name)), i.e. name must be a menu without arguments. On each refresh of a matching kind it computes menu-expand of this item and only rebuilds (with make-menu-widget) if the expansion differs from the displayed one; built widgets are cached by expansion when menu_caching is on.

(refreshable kind . items)
(refresh a closure)

Becomes (widget-refreshable thunk kind), where thunk builds (widget-vmenu …) from items. The widget is rebuilt on every refresh of a matching kind (unless the thunk returns the very same widget object). The kind is an expression, evaluated when the item is interpreted.

(cached kind valid? . items)
(refresh with memoization)

Like refreshable, but the built widget is stored in the table cached-widgets under kind and reused as long as valid? holds. (invalidate-now kind) removes the cached widget and refreshes.

A refresh is requested with (refresh-now kind), which sends SLOT_REFRESH to the auxiliary windows. The kind "any" in a widget matches every refresh, including the automatic ones that follow each menu action. A typical pattern:

(define my-counter 0)

(tm-widget (my-tool)
  (refreshable "my-counter"
    (text (number->string my-counter)))
  ("Increment" (set! my-counter (+ my-counter 1)) (refresh-now "my-counter")))

8.Windows and dialogs

menu-widget.scm

provides the standard ways of showing a widget in its own window, on top of the alt-window-* primitives:

(top-window menu-promise name . opts)
(show a widget in a window)

Calls (menu-promise) to obtain the menu item, builds (vertical …) around it with make-menu-widget*, creates a window with a fresh handle and shows it.

(dialogue-window menu-promise cmd name . opts)
(show a dialog)

Like top-window, but menu-promise is called with a closure which applies cmd to its arguments and then deletes the window. Widgets meant for dialogs therefore take this closure as their last argument (often named quit or cmd) and call it from their buttons.

In both functions, opts may start with buffer URLs, followed by a command to be executed when the window is closed. The buffers are associated to the window with make-window-deleter: a new dialog attached to the same buffer closes the previous one. The function interactive-window is used for widgets built directly in C++ (the printer and color picker dialogs): its promise receives a C++ command and returns a widget.

(interactive fun) (

kernel/texmacs/tm-dialogue.scm

) asks the user for the arguments of a function, using its :argument, :proposals and :default properties. It calls tm-interactive-hook, which is tm-interactive-new of

generic/generic-menu.scm

: when side tools are enabled, the question is shown in a transient bottom tool built in Scheme; otherwise the C++ function tm_frame_rep::interactive (glue tm-interactive) is used, which asks in the footer or builds an inputs_list_widget. user-ask and user-confirm are thin layers over tm-interactive.

9.Forms

(form name . items) groups input fields whose values are collected by name, without explicit call-backs. The macro $form binds the variables form-name, form-entries and form-text-entries around its items; the field keywords (form-input, form-enum, form-choice, form-choices, form-toggle) register the field in form-entries and translate into ordinary input widgets whose call-back stores the answer with form-named-set in the global table form-last. Inside the form, (form-ref field), (form-fields) and (form-values) give access to the collected values. A form-input passes an input type of the form field#form-name-n:type to input_text_widget; every port parses it (in Qt QTMLineEdit::set_type, in Cocoa

ns_dialogues.mm

, in Vue vue_input_text_widget_rep, in Widkit

input_widget.cpp

) and commits the text of fields whose serial starts with form- continuously, so that form-last is always up to date.

10.Side tools

The side, left and bottom tools of the main window are widgets too. The bars are filled by the kernel with (dynamic (texmacs-side-tools win)) and similar expressions; texmacs-side-tools (in

texmacs/menus/main-menu.scm

) lists, for each position, the tools attached to the window with (window->tools win . positions) and displays each of them with texmacs-side-tool.

A tool is defined with (tm-tool (name win . args) . body) (or tm-tool*, without the default centering and indentation). The macro defines a widget name and an overloaded case of texmacs-side-tool (with :require) which adds a title bar with a close button, built from the options (:name …) and (:quit …). Tools are attached to windows with set-window-tools, tool-select, tool-toggle and tool-close, which also toggle the visibility of the side and bottom bars. Modules providing tools can be loaded lazily with lazy-tool.

11.The markup interpreter

menu-convert.scm

contains a second interpreter, build-menu-widget, which has the same structure as make-menu-widget but turns menu items into TeXmacs markup (via functions markup-hlist, markup-menu-button, …). make-menu-widget** then displays this markup in a texmacs-input widget using the style packages gui-bright, gui-dark or gui-button. Actions are stored in a table and referenced from the markup through eval-nullary-mangled and eval-unary-mangled. This path is taken by make-menu-widget* when (has-markup-gui?) holds, that is when both preferences markup gui and developer tool are on; it is experimental, and described in the graphical user interface through markup.

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