GNU TeXmacsUser manualReferenceAboutEnglish
The server classes and their connections

The server is the single object through which the rest of the program reaches the configuration, the windows and the Scheme side of TeXmacs. This page describes its classes: the abstract interface server_rep, the two partial implementations tm_config_rep (fonts and keyboard) and tm_frame_rep (the current window), and the concrete tm_server_rep, together with the way the server is constructed and connected to Scheme and to the editors. The buffers, views and windows which the server manages are described in the following pages; they are not members of the server but global tables of

Texmacs/Data/

.

1.The class hierarchy

The server is split into an abstract interface, two partial implementations and a concrete class which combines them by multiple inheritance:

class server_rep: public abstract_struct { … };      // server.hpp
class tm_config_rep: virtual public server_rep { … }; // tm_config.hpp
class tm_frame_rep:  virtual public server_rep { … }; // tm_frame.hpp
class tm_server_rep: public tm_config_rep,          // tm_server.hpp
                             public tm_frame_rep { … };

The inheritance from server_rep is virtual, so that there is a single server_rep sub-object (and a single reference count) in a tm_server_rep. Each of the three implementation classes implements one group of the pure virtual methods of server_rep; the two partial classes are not meant to be instantiated on their own.

class server_rep
(the abstract server interface)

Declared in

Texmacs/server.hpp

. Apart from the constructor and destructor, all its methods are pure virtual. This includes get_server (), which tm_server_rep implements by returning this, and which is used by code holding a server handle to obtain the raw pointer. They fall into three groups, which match the three implementation classes:

Global parameters

Font rules and keyboard configuration; implemented by tm_config_rep.

TeXmacs frames

Everything which concerns the current window; implemented by tm_frame_rep.

Miscellaneous

Refreshing, the interpose and wait handlers, printing, zoom, typesetting invalidation, quit, shell; implemented by tm_server_rep.

The header also declares a few free functions which are not methods: get_server, cpu_idle_time, menu_merge, quit_TeXmacs_code, gui_set_output_language, in_rescue_mode, and the low level create_buffer (url, tree) and new_buffer_in_this_window.

class server
(the handle)

A hand written reference counted handle around server_rep* (it does not use the ABSTRACT macros because the destruction must go through a dynamic_cast to tm_server_rep, see server_dec_count in

tm_server.cpp

). The default constructor server () creates a new tm_server_rep; the constructor from a server_rep* wraps an existing one.

2.The singleton and its construction

There is exactly one server. It is created as the local variable server sv in TeXmacs_main (

texmacs.cpp

) and lives until the GUI event loop terminates. The constructor of tm_server_rep stores a second handle in the global pointer

server* the_server= NULL;   // Server/tm_server.cpp

which is how the rest of the program reaches it:

server get_server ()

Returns *the_server and asserts that the server has been started.

bool is_server_started ()

Tests whether the_server is set. Code which may run before the server exists (very early errors, the wait handler, the crash handler) must test this first.

Because the_server holds a reference, the reference count of the server never drops to zero while the program runs; the server is not destroyed when sv goes out of scope, and quit leaves the process with _exit anyway (see below).

The constructor of tm_server_rep is also the place where the Scheme side of TeXmacs is booted:

  1. It registers itself in the_server, so that everything that follows may call get_server ().

  2. initialize_scheme () (in

    Scheme/Guile/guile_tm.cpp

    or the corresponding file of the other Scheme back-ends) evaluates a small bootstrap program, installs the Scheme types for trees, URLs, observers, widgets, … and calls initialize_glue () (

    Scheme/Scheme/glue.cpp

    ), which in turn calls initialize_glue_basic, initialize_glue_editor and initialize_glue_server.

  3. It installs texmacs_interpose_handler as the GUI interpose handler (gui_interpose) and texmacs_wait_handler as the wait handler (set_wait_handler).

  4. It executes the Scheme initialization file tm_init_file (by default $TEXMACS_PATH/progs/init-texmacs.scm, overridden by the -i option) and the user file my_init_file ($TEXMACS_HOME_PATH/progs/my-init-texmacs.scm). Loading

    init-texmacs.scm

    defines all modules, menus, keyboard bindings and so on; this is the bulk of the startup time.

  5. If command line options produced Scheme commands (my_init_cmds, for instance from -x, -c or -q), they are wrapped in a begin and scheduled with exec_delayed, so that they run once the event loop has started.

3.The connection to Scheme

The server is connected to Scheme in both directions.

From Scheme to the server.
The routines of server_rep are exported to Scheme by the glue generator. The file
Scheme/Glue/build-glue-server.scm

begins with

(build
  "get_server()->"
  "initialize_glue_server"
  (insert-kbd-wildcard insert_kbd_wildcard (void string string bool bool bool))
  …
  (window-get-serial get_window_serial (int))
  …
  (show-header show_header (void bool))
  …
  (quit-TeXmacs quit (void)))

The first string is the prefix with which the generated C++ code calls each routine, so that (show-header #f) becomes get_server()->show_header (false); the second string is the name of the generated initialization function. The generated code is in

Scheme/Glue/glue_server.cpp

, which is included by

glue.cpp

. In the same way,

build-glue-editor.scm

uses the prefix get_current_editor()->, and

build-glue-basic.scm

exports free functions without a prefix; among those are all buffer, view, window and project routines of

Texmacs/Data/

(the block starting with the comment ;; buffers in that file). The correspondence between the Scheme names and the C++ routines is listed in the Scheme interface.

From the server to Scheme.
The server calls into Scheme with the functions of
Scheme/scheme.hpp

:

call (string fun, args…)

Synchronous call of a Scheme function, used for queries whose result is needed immediately: kbd-find-key-binding and kbd-get-command (keyboard), menu-expand, make-menu-widget, make-menu-widget* and cache-menu? (menus), tmfs-title (titles of tmfs:// buffers), buffer-initialize (buffer notifiers), learn-interactive (interactive commands), register-link-locations and get-link-locations (links), tree-export-encrypted (encryption hook), quit-TeXmacs-scheme and autosave-all (exit and crash).

exec_delayed (object cmd)

Asynchronous execution: the command is queued and executed by the event loop. This is used whenever the call originates from a GUI callback in which it would be unsafe to run arbitrary Scheme code, for instance in kill_window_command_rep::apply (the close box of a window schedules (safely-kill-window id)), in the dialog callbacks of

tm_dialogue.cpp

and for the startup commands.

eval (string) and exec_file (url)

Evaluation of a string or of a whole file; exec_file is used for the initialization files of the server and of new views, eval for (lazy-initialize-force) before building menus and to obtain the value of a menu symbol.

4.The connection to the editors

Editors are created by the view machinery, not by the server, but each of them points back to the server:

class editor_rep: public simple_widget_rep {
public:
  server_rep*  sv;   // the underlying texmacs server
  widget_rep*  cvw;  // non reference counted canvas widget
  tm_view_rep* mvw;  // master view
protected:
  tm_buffer    buf;  // the underlying buffer
  …
};
editor new_editor (server_rep* sv, tm_buffer buf);

(

Edit/editor.hpp

). get_new_view creates the editor with new_editor (get_server () -> get_server (), buf), that is, with the raw server_rep* of the singleton. Through sv, the editor accesses the keyboard configuration (sv->get_keycomb, sv->kbd_post_rewrite, …) and the window related services (footer, zoom, scrolling, menus). Since the latter act on the current window, an editor which needs them for its own window uses the SERVER macro, which makes the editor current for the duration of the call; see working in the context of another view.

Conversely, the server reaches the editors only through the views:

The editor also holds cvw, the canvas widget of its window (set by attach_view, and by texmacs_input_widget for embedded editors), and mvw, the master view of an embedded editor: for an editor inside a texmacs_input_widget, mvw is the view of the document which contains the widget, and is used to give the focus back to it when the embedded widget is closed.

5.The partial server tm_config_rep

class tm_config_rep
(fonts and keyboard)

Declared in

Texmacs/tm_config.hpp

, implemented in

Texmacs/Server/tm_config.cpp

. Its fields are

var_suffix, unvar_suffix

The variant and inverse variant keys preceded by a space (by default " tab" and " S-tab"), set by set-variant-keys.

pre_kbd_wildcards

Wildcards applied to key sequences when bindings are defined (kbd_pre_rewrite).

post_kbd_wildcards

Wildcards applied when keys are looked up (kbd_post_rewrite, get_keycomb).

system_kbd_decode

A table, filled on first use, which maps modifiers and special keys to their rendering in menus (kbd_system_rewrite); its contents depend on whether macOS fonts are used and on whether the GUI is Qt. kbd_system_rewrite also reads two preferences on every call: with the macOS look and feel, A-x is shown as escape x (kbd_system_prevails), and case sensitive shortcuts controls how letters are shown.

The bindings themselves are not stored in C++: find_key_binding and kbd_get_command call the Scheme functions kbd-find-key-binding and kbd-get-command. The central method is get_keycomb (which, status, cmd, shorth, help), called by the editor for each key press; it simplifies variant keys, applies the wildcards, looks the binding up and returns a status (0: no binding, 1: a command, 2: a shorthand string to insert, plus 3 when the key was reduced to a bare variant key). The details are in keyboard configuration.

set_font_rules (rules) passes a list of pairs to font_rule, which installs font substitution rules (see the font database).

6.The partial server tm_frame_rep

class tm_frame_rep
(the current window)

Declared in

Texmacs/tm_frame.hpp

, implemented in

Texmacs/Window/tm_frame.cpp

and

Texmacs/Window/tm_dialogue.cpp

. Almost every method forwards to the tm_window_rep of the current view, obtained with concrete_window (), after checking has_current_window (); the check is there so that Scheme code may call these routines while no window is current (for instance from a background buffer). The exceptions are the canvas routines (get_visible, set_scrollbars, scroll_where, scroll_to, get_extents, set_extents) and dialogue_start, which do not check and crash if there is no current window. Its own fields are

full_screen, full_screen_edit

The full screen state. full_screen_mode (on, edit) hides the header and footer when entering presentation mode (on without edit) and shows them in all other cases, including when leaving full screen, even if the user had hidden them before; it then calls set_full_screen on the window widget and full_screen_mode on the current editor (which sets a flag of the editor and invalidates the display). The state is stored in the server, not in the window, so it is global to the program. in_presentation_mode () (

tm_server.cpp

) is a shortcut for in_full_screen_mode ().

dialogue_win, dialogue_wid

The single dialog window used by dialogue_start, choose_file and interactive commands which use a popup (see dialogs and interactive commands). As there is only one such slot, a second dialog is silently ignored while the first is open (choose_file then still moves the keyboard focus to the old dialog).

The methods are grouped as follows (the window side is described in windows):

Properties

get_window_serial, and set_window_property / get_window_property with typed variants. Properties are an arbitrary tree to tree table in the window. The serial number is used by Scheme to keep per window data, for instance the cursor history in

utils/library/cursor.scm

.

Menus and bars

menu_main, menu_icons, side_tools, bottom_tools install a menu given by the name of a Scheme symbol; show_… and visible_… toggle and query the visibility of the header, the four icon bars, the two side tool areas, the two bottom tool areas and the footer. menu_widget builds a menu widget without installing it.

Canvas

set_window_zoom_factor (clamped to \([0.04, 25]\) and normalized with normal_zoom), get_visible, scroll_where, scroll_to, get_extents, set_extents, set_scrollbars.

Footer

set_left_footer, set_right_footer, and set_message and recall_message, which are forwarded to the current editor (the editor decides what to show in the footer).

Dialogs

dialogue_start, dialogue_inquire, dialogue_end, choose_file, interactive.

7.The concrete server tm_server_rep

class tm_server_rep
(the server)

Declared in

Texmacs/tm_server.hpp

, implemented in

Texmacs/Server/tm_server.cpp

. Its own fields are the default zoom factor def_zoomf and three fields of the idle monitor. The methods are:

interpose_handler ()

Called by the GUI each time it is about to wait for events. It processes input from plug-in pipes and sockets (under Qt: perform_select, unless QTPIPES is defined, and process_all_pipes; with the other toolkits: perform_select and exec_pending_commands), pending asynchronous evaluations (async_eval_pending), and, unless in headless mode, goes through the buffers and, for each buffer, calls apply_changes on the editors of all its views which are attached to a window and then animate on the same editors; it finishes with windows_refresh (). It finally synchronizes the databases (sync_databases) and ticks the idle monitor. This is the heart of the repaint scheduling described in the event loop and repaint scheduling.

idle_monitor_tick (), cpu_idle_time ()

At most once per second, compare the process CPU time with the elapsed time; if TeXmacs used less than half of a CPU, an idle counter is incremented, otherwise it is reset. cpu_idle_time returns the idle duration in milliseconds, so that background tasks can wait until the program is really idle.

wait_handler (message, arg)

Shows a “please wait” indicator in the current window, or prints the message on the console if there is no window yet.

refresh ()

Clears the menu cache of every window (tm_window_rep::refresh), forcing menus and toolbars to be rebuilt; used for instance after a change of the output language (gui_set_output_language).

style_clear_cache ()

Invalidates the style cache and re-initializes the style of every editor.

typeset_update (p), typeset_update_all (), inclusions_gc ()

Invalidate the typesetting of a subtree or of all documents in all views; inclusions_gc also empties the cache of included documents.

Printing

The printing command, the paper type and the resolution are stored in global variables of the printing code.

set_default_zoom_factor, get_default_zoom_factor

The zoom factor used for new windows (clamped and normalized like the window zoom factor).

is_yes (s)

Tests whether a user answer means “yes” in the current language (compares the first character with the translation of "yes").

quit ()

Closes all pipes to plug-ins, calls the Scheme hook quit-TeXmacs-scheme (which runs all the code registered with the on-exit macro of

kernel/boot/boot.scm

), clears the pending commands, destroys the Qt renderer objects and terminates the process with _exit (or exit in ADVANCED_DEVELOPER_MODE). A comment in the code explains that _exit is used because destructing Qt objects at exit sometimes crashes. quit_TeXmacs_code (code) does the same with a given exit code.

shell (s)

Runs a shell command.

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