![]() |
The event loop, keyboard and mouse, and repaint scheduling | ![]() |
This page follows a user event from the toolkit to the screen. The central idea is that TeXmacs never reacts to an event by redrawing: events are queued, the editor updates the document and records what changed in a bit set, and a single routine, apply_changes, is later called from the interpose handler of the server to retypeset, recompute the cursor and selections, update the menus and invalidate screen regions. The toolkit then repaints the invalid regions. Schematically, for the Qt port:
Qt event (QTMWidget)
-> qt_gui_rep::process_keypress / process_mouse / … (queued_event)
-> qt_gui_rep::update (single shot timer)
|- process_delayed_commands (exec_delayed)
|- process_queued_events
| -> editor::handle_keypress / handle_mouse
| -> Scheme keyboard-press / mouse-event
| -> modifications, notify_change (flags)
|- interpose handler (tm_server_rep)
| -> apply_changes on every view shown in a window
| -> windows_refresh
|- repaint_all -> editor::handle_repaint |
The last two sections describe the keyboard configuration kept by the server, which is consulted for every key press, and the user preferences. The toolkit side of the event queue is described in more detail in the Qt implementation.
With the Qt back-end (
Plugins/Qt/qt_gui.cpp
, and its counterpart in
Plugins/Qt6/
), events delivered by Qt to TeXmacs widgets are not processed immediately. The widgets call qt_gui_rep::process_keypress, process_mouse, process_keyboard_focus, process_resize or process_command, which append a queued_event to a private queue with add_event and request an update. The update is performed by qt_gui_rep::update, which is triggered by a single shot timer, and roughly does the following:
execute the delayed Scheme commands whose time has come (process_delayed_commands);
process the queued events with process_queued_events, which calls handle_keypress, handle_mouse, handle_keyboard_focus or handle_notify_resize on the target widget (for the canvas of a document, this widget is the editor);
call the interpose handler and then repaint the invalid regions of all widgets (qt_simple_widget_rep::repaint_all ()); when only ordinary keys were typed during this round, this step is postponed for a few milliseconds so that fast typing is not slowed down by the updates;
restart the timer, immediately if events are still pending, otherwise after a short delay or when the next delayed command is due.
The interpose handler was installed by the server constructor with gui_interpose (texmacs_interpose_handler); it calls tm_server_rep::interpose_handler:
void
tm_server_rep::interpose_handler () {
… // communication with plug-ins, pending commands
async_eval_pending ();
if (!headless_mode) {
for (i=0; i<N(bufs); i++) {
tm_buffer buf= (tm_buffer) bufs[i];
for (j=0; j<N(buf->vws); j++) {
tm_view vw= (tm_view) buf->vws[j];
if (vw->win != NULL) vw->ed->apply_changes ();
}
… // same loop calling animate ()
}
windows_refresh ();
}
sync_databases ();
idle_monitor_tick ();
} |
Hence only active views (views displayed in a window) are updated and retypeset in the background; passive views are typeset on demand, for instance when an environment value is queried. windows_refresh sends refresh requests to the widgets of all windows, which in particular updates dynamic menus and widgets; it is throttled with windows_delayed_refresh. The idle monitor keeps track of the CPU usage in order to implement cpu_idle_time.
A key press is processed by the editor as follows.
edit_interface_rep::handle_keypress (key, t) (
Edit/Interface/edit_keyboard.cpp
) records the key for the optional display of typed keys, forces a first typesetting if needed, calls start_editing (), and passes the key to the Scheme function keyboard-press (or delayed-keyboard-press for pre-edit strings of input methods).
keyboard-press is defined with tm-define in
kernel/gui/kbd-handlers.scm
; its default implementation calls (key-press key). It is overloaded in several contexts, for instance inside input fields of widgets (
utils/misc/gui-utils.scm
), during interactive spell checking or in the shortcut editor.
key-press is the glue for edit_interface_rep::key_press. This routine handles input method and speech input, then tries to interpret the key, preceded by the keys of a pending shortcut (if any), as a shortcut with try_shortcut. If this fails, the key is inserted as text through the Scheme function kbd-insert (default: insert), after a call to archive_state ().
try_shortcut asks the server for the binding with sv->get_keycomb (see below). If the key sequence is bound, the pending changes are enclosed in a new undo marker (mark_start), the help text of the shortcut is displayed in the footer, and either the bound command is executed or the bound string is inserted with kbd-insert. When the next key extends the sequence to a longer shortcut, mark_cancel undoes the effect of the prefix before the longer shortcut is applied; this is how, for instance, repeated presses of a variant key cycle through symbols.
Back in handle_keypress, the focus loci are updated, notify_change (THE_DECORATIONS) is called and end_editing () confirms the undo step. If an exception is raised during the processing, the changes are cancelled with cancel_editing.
Keyboard focus changes are handled by handle_keyboard_focus, which updates got_focus, makes the view current when it obtains the focus and calls the Scheme hook keyboard-focus.
edit_interface_rep::handle_mouse (kind, x, y, mods, t, data) (
Edit/Interface/edit_mouse.cpp
) first makes sure that the document is typeset (the box tree is needed to interpret the coordinates), calls start_editing, converts the coordinates according to the magnification, detects the start of left and right drags, and passes the event to the Scheme function mouse-event. Its default definition in
kbd-handlers.scm
calls the glue routine mouse-any, that is, edit_interface_rep::mouse_any. The latter updates the loci under the mouse (hyperlinks, tooltips), dispatches to the graphics editor when the pointer is inside a graphics, and otherwise calls mouse_click, mouse_drag, mouse_select, mouse_extra_click, mouse_paste, mouse_adjust or mouse_scroll depending on the kind of event. Drop events are passed to mouse-drop-event. As for keyboard events, the handler ends with end_editing ().
Editing routines never redraw the screen directly. Instead, they call notify_change (int flags), which adds the flags to env_change and asks the GUI for an update (needs_update). The flags are defined in
editor.hpp
:
The document tree changed; the document has to be retypeset.
The initial environment or the style changed; everything has to be retypeset.
The cursor, the selection or the keyboard focus changed.
The size of the document or of the window changed.
Menus, toolbars and footer might have changed.
The active loci (hyperlinks, …) have to be redrawn.
Force an update of the menus.
Do not scroll to make the cursor visible.
Tooltips and highlighted spelling errors.
apply_changes (
Edit/Interface/edit_interface.cpp
), called by the interpose handler, processes the accumulated flags:
If nothing changed, it only updates the menus, toolbars and footer with update_menus (), provided that the document changed since the last such update and that the user has been idle for at least 1/6 of a second (idle_time).
It adapts the environment variables which depend on the window: the zoom factor, the page size for the automatic page medium, scroll bars and the visibility of window bars.
On THE_ENVIRONMENT it invalidates the whole typesetting; on THE_TREE or THE_ENVIRONMENT it retypesets the invalid parts (typeset) and invalidates the corresponding screen rectangles.
On changes of the tree, environment or extents it recomputes the extents of the document and passes them to the window.
On changes of the cursor, selection or focus it recomputes the graphical cursor, scrolls to make it visible (unless THE_FREEZE is set), and recomputes the rectangles which highlight the context, focus and semantic selection.
It recomputes the rectangles of the selection and of the alternative selections, triggers continuous spell checking, updates the loci under the mouse and the focus loci (calling the Scheme function link-follow-ids), and updates the menus if THE_MENUS is set.
Finally, it resets env_change and records the time of the change in last_change.
All drawing is done by invalidating rectangles (invalidate). The GUI then calls handle_repaint (renderer, x1, y1, x2, y2) (
Edit/Interface/edit_repaint.cpp
), which draws the background, the typeset boxes, the selections, the cursor and the other decorations into the renderer, using a “stored” or “shadow” renderer as a cache. handle_repaint expects env_change to be zero: all changes must have been applied before the screen is repainted.
update_menus () rebuilds the main menu, the icon bars and the side and bottom tools (through the SERVER macro, see menus and toolbars), updates the footer (set_footer), updates the “modified” indicator of all windows on the buffer, updates the DRD, and saves the user preferences if they were modified. The Scheme routine update-menus calls it directly.
Key bindings are defined in Scheme with the kbd-map macro of
kernel/gui/kbd-define.scm
, possibly conditioned on modes and contexts, and looked up with kbd-find-key-binding. The tm_config_rep part of the server implements the lookup of a key sequence entered by the user in get_keycomb (which, status, cmd, shorth, help):
The sequence is simplified with respect to the variant keys (variant_simplification). By default the variant key is tab and the reverse variant key is S-tab; they can be changed with set_variant_keys (Scheme: set-variant-keys). Trailing variant keys which do not lead to a binding are dropped, and a reverse variant key removes the last variant.
The post wildcards are applied (apply_wildcards). Wildcards are rewriting rules on key sequences, declared in Scheme with kbd-wildcards and registered with insert_kbd_wildcard (Scheme: insert-kbd-wildcard). Pre wildcards are applied to the keys in the definitions (kbd_pre_rewrite), post wildcards at lookup time (kbd_post_rewrite).
The binding is looked up with the Scheme function kbd-find-key-binding (find_key_binding). The result determines status: 0 if there is no binding, 1 if the binding is a command (returned in cmd), 2 if it is a string to be inserted (returned in shorth). In the last two cases help contains the help text of the binding. The status is increased by 3 if the sequence was reduced to a bare variant key.
kbd_system_rewrite translates a key sequence into a tree for displaying shortcuts in menus and in the footer, using system specific names or symbols for the modifiers and special keys (for instance the macOS modifier symbols). kbd_get_command looks up named commands through the Scheme function kbd-get-command. set_font_rules installs font substitution rules.
User preferences are stored as pairs of strings in $TEXMACS_HOME_PATH/system/preferences.scm. At the C++ level, load_user_preferences, save_user_preferences, get_user_preference and set_user_preference (
System/Boot/preferences.cpp
) access this file directly; they are used during startup, before Scheme is available. Afterwards, code should use get_preference (var, def) and set_preference (var, val) (
Scheme/Scheme/object.cpp
), which call the Scheme functions get-preference and set-preference once the preferences have been booted. On the Scheme side (
kernel/texmacs/tm-preferences.scm
), preferences are declared with default values and call-back functions using define-preferences; the call-back is invoked when the preference changes (notify-preference). Examples can be found in
texmacs/texmacs/tm-server.scm
. Modified preferences are written back to disk by save_user_preferences, which is called from update_menus.
Several preferences are read by the code discussed in this document: window decorations in new_window, show full context, show table cells, show focus and show only semantic focus in apply_changes, look and feel for the clipboard and keyboard conventions, and case sensitive shortcuts in kbd_system_rewrite.