![]() |
The main program and crash handling | ![]() |
This page describes
Texmacs/Texmacs/texmacs.cpp
, which contains the main program, and
Texmacs/Server/tm_debug.cpp
, which handles fatal errors. The platform specific main functions (
Plugins/Unix/unix_entrypoint.cpp
,
Plugins/Windows/windows32_entrypoint.cpp
,
Plugins/Windows64/windows64_entrypoint.cpp
and, for Android,
src/packages/android/launcher/main.cpp
) prepare the environment and the arguments (the TeXmacs path, the AppImage search path and the Qt platform on Unix, conversion of the arguments to UTF-8 on Windows) and then call texmacs_entrypoint.
Startup happens in two stages. The first stage, texmacs_entrypoint (argc, argv), runs before the Scheme interpreter exists:
immediate_options sets TEXMACS_HOME_PATH if it is not set (~/.TeXmacs, or the platform equivalent) and handles the options which must act before anything is loaded: the cache and setup removal options, -headless, -open and -log-file (see below). It also sets TEXMACS_SERVER_CERT_DIR.
Resource limits are raised (the stack size if STACK_SIZE is defined; on macOS, the number of open files in boot_hacks), automatic widget refreshes are disabled until further notice (windows_delayed_refresh), the paths are initialized (TeXmacs_init_paths, which also fixes the environment of application bundles on macOS, Windows and Haiku) and the user preferences are loaded (load_user_preferences).
The Qt application object is created: a QTMApplication normally, a QTMCoreApplication without any GUI in headless mode. The gui scaling preference is applied first, through QT_SCALE_FACTOR.
In -open mode (not on macOS; the option also sets headless mode), the file is sent to an already running instance if there is one (send_to_single_instance, Qt only), otherwise a new TeXmacs is started on it with execl; in both cases the current process exits.
The fonts are initialized, the global edit tree is created with its root ip_observer, the caches are initialized (cache_initialize), and init_texmacs (
System/Boot/init_texmacs.cpp
) performs the remaining system initialization: the main paths, the user directories, the boot lock (if the lock file of a previous run is still there, that run crashed during startup, and the settings and caches are reset; the lock file itself stays until release_boot_lock), the succession status table, the standard DRD, the preferences again, the paths of the Scheme interpreter, the environment variables used by plug-ins and some miscellaneous and deprecated settings.
start_scheme (argc, argv, TeXmacs_main) calls TeXmacs_main. With Guile it does so through scm_boot_guile (or gh_enter), so that TeXmacs_main runs inside the Guile runtime; with TinyScheme it just calls it. The interpreter state itself (bootstrap code, types and glue) is set up later, by initialize_scheme in the constructor of the server.
The second stage, TeXmacs_main (argc, argv), runs inside the interpreter:
set_global_options parses the remaining command line options and reads a few preferences which must be known before the first window is built (native menu bar, mini bars).
init_plugins installs the plug-ins, gui_open opens the display and the default font is set.
The server is constructed (server sv), which boots the Scheme side; see the server classes.
On the first run after an installation or an upgrade (install_status), a command which loads the welcome message or the list of recent changes is added.
If no buffer has been opened yet, an empty window is opened (open_window). The comment in the code notes that this test is always true at this point, since files given on the command line are only loaded later.
A delayed command which gives the keyboard focus to the canvas is added, texmacs_started is set, the signal handlers are installed (segmentation faults go to the crash handler unless -disable-error-recovery was given; SIGTERM exits at once; SIGPIPE is ignored, so that a client closing a socket does not kill the process), the TeXmacs server for remote clients is started if requested (-server, and only if server_can_start ()), and the boot lock is released.
The collected startup commands are scheduled with exec_delayed and the GUI event loop is entered (gui_start_loop). Normally the program leaves the loop only through quit, which exits the process directly.
Most command line options do not act directly but produce Scheme commands, collected in two strings:
Filled by -x, -q, -c/-C, -W and -U; in headless mode without -server, (quit-TeXmacs) is appended (unless the option -X was given, which sets exec_exit to false). These commands are wrapped in a single begin and scheduled by the constructor of tm_server_rep, right after
init-texmacs.scm
and my-init-texmacs.scm have been loaded.
Filled with a load-buffer for each file named on the command line (the first one in the current window, the next ones with :new-window), the welcome or upgrade message, -build-manual, -reference-suite and -test-suite. These commands are scheduled by TeXmacs_main just before entering the event loop.
The delayed commands are executed by the event loop in the order in which they were scheduled (exec_pending in
Plugins/Qt/qt_gui.cpp
processes a FIFO queue). This has two consequences:
The commands of -x run before the files given on the command line are loaded.
quit-TeXmacs exits the process at once. So a -q, or the (quit-TeXmacs) appended in headless mode, prevents everything scheduled after it from running: the -x and -c options which come later on the command line, and all of extra_init_cmd (the files to load, -build-manual, -reference-suite, -test-suite). In headless mode these need -X.
The options are recognized with one or two leading dashes. The main ones are:
-h (help, also any unknown option), -v (version), -p, -hp, -bp (print the TeXmacs path, home path and binary path).
-s (silent), -V (verbose), -d (debug), the -debug-kind family (events, io, sockets, gnutls, bench, history, qt, qt-widgets, keyboard, packrat, flatten, parser, correct, convert, remote, live, and all, which only turns on events, std, io, history, bench, qt and qt-widgets), -disable-error-recovery, -log-file file.
-i file (replaces
init-texmacs.scm
), -b file (replaces
init-buffer.scm
), -x cmd (execute a Scheme command), -q (append (quit-TeXmacs) at this point of the command line), -X (do not quit automatically in headless mode).
-g wxh+x+y (geometry of the first window), -fn font (default font), -r (reverse video), -Oc/+Oc (clipping of TeX bitmap characters) and, for Qt 5 and earlier, the -retina options.
-headless or -H (no GUI), -c or -C in out (convert a file, through load-buffer and export-buffer), -W and -U in out (build or update a web site), -build-manual, -reference-suite, -test-suite. In the Qt version, -C, -W/-build-website and -U/-update-website imply headless mode (but not the lower case -c).
-S or -setup (forget the settings and caches), -delete-cache and the more specific -delete-kind-cache options, -delete-server-data, -delete-databases.
-server, -port n, -reset-server-preferences, -reset-admin-password, -tls-no-verify (see collaboration), and -open file (open in a running instance).
Several of these options are seen twice: by immediate_options and by set_global_options. For most of them (the cache options, -headless, -log-file) the first does the work and the second skips them; for -C, -W and -U the first sets headless mode and the second builds the command. On macOS, set_global_options skips -open but not its argument, which is then loaded as an ordinary file. A new option which must act before the preferences or the GUI exist belongs in immediate_options; all others belong in set_global_options and should, when possible, produce a Scheme command rather than act directly.
texmacs.cpp
defines a few global flags: headless_mode (read through is_headless () or directly as an extern, for instance by the interpose handler, which skips all screen updates in this mode), tls_no_verify (is_tls_no_verify ()) and disable_error_recovery (used only in
texmacs.cpp
).
tm_server.cpp
defines texmacs_started, which is set just before the event loop starts and is used by the wait handler to decide whether a window can be used.
Internal errors are signalled with the macros ASSERT (cond, msg) and FAILED (msg) of
Kernel/Abstractions/basic.hpp
. What they do depends on USE_EXCEPTIONS, which that header currently always defines.
Kernel/Abstractions/basic.cpp
). It stores the message in the_exception, builds a crash report with get_crash_report (see below), prints it on the console and throws the message as a string. The exception is caught at the following places:
key presses (edit_interface_rep::handle_keypress, which cancels the current edit and the pending shortcut) and mouse events (handle_mouse, which cancels the current edit; update_mouse_loci ignores the exception);
menu and toolbar actions (protected_call in
Scheme/Scheme/object.cpp
, which calls cancel_menu_action on the current editor);
typesetting (
edit_typeset.cpp
), which does not cancel anything: it reports “Typesetting failure, resetting to empty document”, replaces the body of the buffer by an empty document and typesets again;
as a last resort, every Qt event: QTMApplication::notify and QTMCoreApplication::notify catch the exception; a few slots (in
QTMMenuHelper.cpp
,
QTMGuiHelper.cpp
,
QTMFileDialog.cpp
and
QTMPipeLink.cpp
) are also wrapped in the macros BEGIN_SLOT and END_SLOT of
Plugins/Qt/qt_gui.hpp
. (The same code exists in
Plugins/Qt6/
.)
The editor and protected_call sites call handle_exceptions () right after catching; it prints the message and the report as an error (and so in the error console of TeXmacs) and clears the_exception. The Qt sites only store the message in the_exception, which is reported by the next call of handle_exceptions. In all cases the program goes on with the next event and nothing is saved.
Texmacs/Server/tm_debug.cpp
), followed by assert when DEBUG_ASSERT is defined (which the CMake build does). tm_failure tries to save as much as possible:
It sets the global rescue_mode (tested with in_rescue_mode ()). A second fatal error while in rescue mode exits at once (or returns, with DEBUG_ASSERT).
It writes the crash report to $TEXMACS_HOME_PATH/system/crash/crash_report_n, or prints it if this fails.
It writes the tree of the current buffer, with the path of each node, to a file with the same name followed by _tree (tree_report).
It autosaves all buffers (autosave-all), closes all plug-in pipes, runs the Scheme exit hook and clears the pending commands.
tm_failure does not exit by itself; it returns to the assert (which aborts) or to the code which raised the error. Note that it calls get_server and get_current_editor without checking that they exist, so an error before the first view exists fails a second time; rescue_mode stops the recursion.