![]() |
The server: buffers, views and windows | ![]() |
This chapter describes the part of TeXmacs which sits between the graphical toolkit, the Scheme interpreter and the typesetter: the server, a singleton which owns the open documents and the windows, and the editors, which implement the interactive behaviour of the program. It explains how documents are organized into buffers, views and windows, how these objects are created and destroyed, how user events reach the editor, how modifications of a document are propagated (to the typesetter, to the other views on the same document and to the undo system), and how screen updates are scheduled.
An open document, identified by its name (a file name, a scratch name for new documents, or a tmfs:// URL for generated documents). The body of every buffer is a child of one global tree, the_et. A buffer may be shown in any number of windows, including none.
An editor on a buffer: cursor, selection, typeset boxes, zoom and undo history. A buffer may have several views; all of them share the document and see each other's changes immediately. A view is shown in at most one window; a view in no window is passive.
A top level window with menus, toolbars, a canvas and a footer; it shows exactly one view at a time.
At any moment one view is current. It determines the current buffer, the current window (if the view is shown in one), the current editor and the current DRD, and it is the implicit argument of all editing commands and of all routines which concern “the” window.
A few invariants explain much of the code:
Every buffer which is being edited has at least one view, even after its last window has been closed: the “modified” status of a buffer is computed from the undo histories of the editors of its views.
Documents are only modified through the elementary operations on the_et (assign, insert, …), whose observers notify every editor on the buffer and every undo history.
Editors never redraw directly: they record what changed, and the interpose handler of the server retypesets and invalidates the views shown in windows between two rounds of event processing.
Outside the files which implement them, buffers, views and windows are referred to by URLs, never by pointers.
The following diagram shows the main objects and the pointers between them. Arrows mean “holds a pointer or handle to”; the names in brackets are the fields.
server (handle, singleton the_server)
|
tm_server_rep
/ \
tm_config_rep tm_frame_rep (both : virtual server_rep)
bufs : array<tm_buffer>
|
tm_buffer_rep ---[buf]--> new_buffer_rep (name, master, title, …)
| ---[data]-> new_data_rep (style, init, ref, aux, …)
| ---[prj]--> tm_buffer_rep (the project, if any)
| ---[rp]---> path of the body in the_et
[vws]
|
tm_view_rep ---[ed]---> editor (edit_interface_rep …)
| |-[sv]--> server_rep
[win] |-[buf]-> tm_buffer_rep
| |-[cvw]-> canvas widget of the window
tm_window_rep ---[wid]--> texmacs_widget (menus, canvas, footer)
---[win]--> plain_window_widget (the top level window) |
The metadata objects are plain C++ objects allocated with tm_new and deleted with tm_delete; they are not reference counted. Ownership is as follows:
are owned by the global array bufs (
new_buffer.cpp
). They are created by insert_buffer and destroyed by remove_buffer.
are owned by their buffer (the array tm_buffer_rep::vws). They are created by get_new_view and destroyed by delete_view, or implicitly when their buffer is removed.
are owned by the static table tm_window_table (
new_window.cpp
), which maps window identifiers to tm_window_rep*. They are created by new_window and destroyed by delete_window. The windows of embedded TeXmacs widgets are the exception: they are not in this table and are owned by the widget (see embedded widgets).
Everything outside
Texmacs/Data/
refers to these objects by URL, and converts with the concrete and abstract functions:
The URL is the buffer name itself. concrete_buffer (url) searches bufs linearly and returns NULL if there is no such buffer; concrete_buffer_insist loads the file first if needed.
tmfs://view/nr/encoded-name. abstract_view builds it from the view number and the buffer name; concrete_view parses it, looks up the buffer, and then the view with the given number among the views of that buffer. The buffer name is encoded by encode_url: a relative name becomes here/…, a local absolute name default/…, and another root (tmfs, http, …) is followed by a slash and the rest of the name; decode_url reverses this (with special handling of drive letters on Windows).
tmfs://window/n, allocated by create_window_id; the counter is never decremented, so window identifiers are not reused during a session. concrete_window (url) is a lookup in tm_window_table; abstract_window returns the field id of the window.
The following functions (declared in
Data/new_buffer.hpp
,
Data/new_view.hpp
and
Data/new_window.hpp
) navigate between the three kinds of objects. They take and return URLs; url_none () is returned when there is no answer.
buffer_to_views, buffer_to_windows, get_recent_view, get_passive_view.
view_to_buffer, view_to_window, view_to_editor.
window_to_view, window_to_buffer.
path_to_buffer (path p) returns the buffer whose root path is a prefix of p.
get_all_buffers, get_all_views, windows_list, number_buffers, get_nr_windows.
concrete_buffer, concrete_buffer_insist (which loads the buffer if needed), concrete_view, abstract_view, concrete_window, abstract_window.
Code outside
Texmacs/Data
should use the URL based functions. The raw pointers tm_buffer, tm_view and tm_window are not reference counted, so they must never be stored across operations which might close a buffer or a window.
All file names are relative to
src/src/
unless stated otherwise. The directory Texmacs/ contains the server proper:
Texmacs/server.hpp
The abstract class server_rep, the handle server, get_server and a few global declarations. It also includes the headers of
Texmacs/Data/
, so that including
server.hpp
gives access to the whole URL based buffer, view and window interface.
Texmacs/tm_server.hpp,
Texmacs/Server/tm_server.cpp
The concrete server tm_server_rep, its constructor (which boots Scheme), the interpose and wait handlers, printing settings, global typesetting invalidation and quit.
Texmacs/tm_config.hpp,
Texmacs/Server/tm_config.cpp
The partial server tm_config_rep: font rules and the keyboard configuration.
Texmacs/tm_frame.hpp,
Texmacs/Window/tm_frame.cpp,
Texmacs/Window/tm_dialogue.cpp
The partial server tm_frame_rep: properties, menus, toolbars, canvas, footer and full screen mode of the current window; dialog windows, file choosers and interactive commands.
Texmacs/tm_buffer.hpp
The class tm_buffer_rep.
Texmacs/tm_window.hpp,
Texmacs/Window/tm_window.cpp
The classes tm_window_rep and tm_view_rep (whose constructor is in
Texmacs/Data/new_view.cpp
); window geometry, embedded TeXmacs widgets, menu caching, interactive input in the footer, and the “alternative” top level windows used for Scheme dialogs.
Texmacs/tm_data.hpp
The global array bufs and a convenience set_message; included by all files of
Texmacs/Data/
.
Texmacs/Data/new_buffer.hpp,
new_buffer.cpp
The class new_buffer_rep and all buffer level routines: list of buffers, names and titles, contents, save status, loading, saving and inclusions.
Texmacs/Data/new_view.hpp,
new_view.cpp
View identifiers, the current view, the view history, creation and destruction of views, attaching views to windows, and focus changes.
Texmacs/Data/new_window.hpp,
new_window.cpp
Window identifiers, creation and destruction of windows, and the high level commands which open, clone and close windows and buffers.
Texmacs/Data/new_project.hpp,
new_project.cpp
Projects.
Texmacs/Window/tm_button.cpp
Widgets which display a typeset box (box_widget, texmacs_output_widget) and the computation of the size of a typeset document (tree_extents).
Texmacs/Server/tm_debug.cpp
System and editor status reports, crash reports and the fatal error handler tm_failure.
Texmacs/Texmacs/texmacs.cpp
The main program: texmacs_entrypoint, TeXmacs_main, the command line options and the startup preferences.
Two closely related files live elsewhere:
Data/Document/new_data.hpp
(the class new_data_rep, with attach_data and detach_data) and
Data/Document/new_document.cpp
(the global edit tree the_et). The Scheme glue which exports the server is declared in
Scheme/Glue/build-glue-server.scm
and, for buffers, views and windows, in
Scheme/Glue/build-glue-basic.scm
.
The editor is in
Edit/
: the abstract class editor_rep in
Edit/editor.hpp
and its implementation edit_main_rep in
Edit/Editor/edit_main.hpp
, assembled from the classes in
Edit/Interface
(events, cursor, repainting, footer),
Edit/Modify
(modifications and undo),
Edit/Replace
(selections, search, spell checking) and
Edit/Process
. The observers attached to the edit tree are in
Data/Observers
, the undo history in Data/History, and the generic observer mechanism in
Kernel/Abstractions/observer.cpp
.
On the Scheme side, the most relevant files (relative to
src/TeXmacs/progs/
) are
kernel/library/base.scm
,
kernel/gui/kbd-handlers.scm
,
kernel/gui/kbd-define.scm
,
kernel/texmacs/tm-preferences.scm
,
kernel/texmacs/tm-file-system.scm
,
utils/library/cursor.scm
,
texmacs/texmacs/tm-files.scm
and
texmacs/texmacs/tm-server.scm
.
The pages are ordered from the static structure to the dynamic behaviour. Readers in a hurry may start with the walk-throughs, which follow common operations through all the layers.
Neighbouring subjects are described elsewhere: the typesetting of documents into boxes in the typesetting algorithm and the boxes produced by the typesetter; the editing operations in structured editing, search and automatic content; the widgets which make up menus, toolbars and dialogs in the abstract widget system and their toolkit implementations in the graphical user interface ports; and the Scheme programming interface in the Scheme buffer API.