![]() |
Walk-throughs and guidelines | ![]() |
The other pages of this chapter describe the objects one by one. This page follows a few common operations through all of them, from the user action to the screen, and ends with the rules which follow from the design. Each step names the function which performs it, so that the walk-throughs can also be used as a map for setting breakpoints.
Running texmacs a.tm goes through the following steps.
texmacs_entrypoint initializes the paths, the preferences, the toolkit and the global edit tree the_et, and starts the Scheme interpreter, which calls TeXmacs_main (see the startup sequence).
set_global_options turns the file argument into the Scheme command (load-buffer "/…/a.tm") and appends it to extra_init_cmd; further files get :new-window.
The server is constructed; its constructor loads
init-texmacs.scm
, which defines all menus, keyboard bindings and user commands.
Since no buffer exists yet, open_window creates the scratch buffer no_name_1.tm with make_new_buffer, a window with new_window, and a first view with get_passive_view (which calls get_new_view, which creates the editor and runs
init-buffer.scm
); window_set_view attaches the view to the window (attach_view, resume, which installs the menus) and makes it current.
TeXmacs_main schedules extra_init_cmd with exec_delayed and enters the event loop.
In the first round of the event loop, the delayed command runs. load-buffer goes through its chain of checks (loading); buffer-load reads and converts the file (buffer_load, import_tree) and creates the buffer with set_buffer_tree, which allocates a slot of the_et.
load-buffer-open calls switch-to-buffer: switch_to_buffer obtains a passive view on a.tm (creating its editor) and window_set_view replaces the view of the scratch buffer in the window: the old view is detached (and suspended), the new one attached (and resumed) and made current. The scratch buffer stays open, without window.
The resume of the new editor has called notify_change with THE_FOCUS and THE_EXTENTS, and the constructor of the editor THE_TREE. At the end of the round, the interpose handler calls apply_changes on the editor, which typesets the document, passes its extents to the window, computes the cursor and invalidates the canvas; the toolkit then calls handle_repaint, and the document appears.
The Qt canvas widget receives a key event, translates it into a TeXmacs key name such as "a" or "C-x", and queues it with qt_gui_rep::process_keypress.
qt_gui_rep::update takes it from the queue and calls handle_keypress on the editor which owns the canvas. The editor calls start_editing (which sets the current author for the undo system) and passes the key to the Scheme function keyboard-press.
Unless a mode overrides it, keyboard-press calls key-press, that is, edit_interface_rep::key_press, which looks the key up as a shortcut with the server (get_keycomb, which calls kbd-find-key-binding). A plain letter is not bound, so it is inserted with kbd-insert, which eventually calls insert (path, tree) on the global edit tree.
The modification goes through apply in
observer.cpp
and is announced to the observers of the modified node: the ip_observers propagate it up to the root of the buffer, where the edit_observer of every editor on the buffer and the undo_observer of every archiver receive it. Each editor forwards it to its typesetter, which invalidates the corresponding boxes, corrects its cursor and calls notify_change (THE_TREE); each archiver records the inverse modification.
Back in handle_keypress, end_editing confirms the changes as one undo step.
The interpose handler calls apply_changes, which retypesets the invalid parts of the document, invalidates the screen regions which changed and recomputes the cursor; the toolkit repaints them. A short while later, if the user is idle, update_menus refreshes the menus, the icon bars and the footer.
Fast typing is not slowed down by typesetting, because the repaint step is postponed while ordinary keys keep arriving (see the event loop).
clone-window (clone_window) opens a new window and calls get_passive_view on the current buffer. Since its only view is attached to the first window, a second view, with its own editor, is created. The two editors share the body of the document (the same subtree of the_et) and the references and auxiliary data (buf->data), but each has its own copy of the style and initial environment, its own box tree, cursor, selection, zoom factor and undo history.
When the user types in the second window, the modification is announced to both edit observers on the buffer root, so both typesetters are updated and both cursors are corrected; the interpose handler then retypesets and repaints both windows. No synchronization code is involved: this is a consequence of the global edit tree. An undo in one window only undoes the changes made in that window, since the archivers tag each step with the author of the editor.
The user clicks on the close box. The toolkit calls the quit command of the TeXmacs widget, a kill_window_command_rep, which schedules (safely-kill-window id).
safely-kill-window quits TeXmacs (after confirmation) if this is the last toolkit window; otherwise it asks for confirmation if the buffer of the window is modified and calls kill-window.
kill_window makes a view in another window current and calls delete_window, which detaches the view (suspend), destroys the widgets and deletes the tm_window_rep. The view itself survives as a passive view.
After 100 milliseconds of idle time, buffer-close calls kill_buffer on the buffer of the closed window, which gives the other windows showing this buffer (if any) another buffer to show and removes the buffer with all its views.
Scheme code often needs to build or inspect a document without showing it, for instance to generate an auxiliary document or to convert a file. The pattern is
(let ((u (string->url "tmfs://aux/my-report")))
(buffer-set-body u '(document "")) ; create the buffer
(view-passive u) ; give it an editor
(with-buffer u ; make it current
(set-style-list '("article"))
(insert "Hello"))
(buffer-export u "/tmp/report.pdf" "pdf")
(buffer-close u)) |
Each line corresponds to a rule of this chapter: a buffer is created by setting its contents; editing commands need an editor, hence a view; the editor routines act on the current view, hence with-buffer; exporting to PDF uses the typesetter of the most recent view; and the buffer must be closed explicitly, since it is not attached to any window. Note that a passive view is not retypeset by the event loop; buffer_export typesets it when it prints.
Refer to buffers, views and windows by their names and URLs, not by tm_buffer, tm_view or tm_window pointers: these are not reference counted and are deleted when the objects are closed. Remember that view URLs change when a buffer is renamed.
Remember that the editor glue and the frame routines of the server act on the current view and its window. To act on another buffer, use with-buffer in Scheme (and make sure that the buffer has a view), or change the current view temporarily and restore it, as the SERVER macro does, in C++. Restore it also when an error occurs.
Modify documents only through the functions of
observer.cpp
(assign, insert, …) or the editing routines built on top of them. Direct assignments to subtrees of the_et bypass the observers, so neither the typesetter, nor the other views, nor the undo system would notice them.
Group the modifications of one user action between start_editing and end_editing (the event handlers already do this); otherwise they may be merged with the next action in the undo history, or be attributed to the wrong author.
Do not typeset or repaint from editing routines. Call notify_change with the appropriate flags; the interpose handler will call apply_changes at the next occasion. If up-to-date typesetting information is needed immediately (for instance a box or the environment at the cursor), apply_changes can be called explicitly, but only on views shown in a window.
Passive views are not updated by the interpose handler. Keep this in mind when the result of an operation on a buffer depends on the typesetting of that buffer.
Do not run arbitrary Scheme code, or open dialogs, from callbacks of the toolkit; schedule it with exec_delayed, as the close box of a window does.
The low level loading and saving routines return true on failure.