GNU TeXmacsUser manualReferenceAboutEnglish
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.

1.Buffers, views and windows in a nutshell

Buffer

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.

View

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.

Window

A top level window with menus, toolbars, a canvas and a footer; it shows exactly one view at a time.

Current view

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:

2.The objects and their connections

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)

3.Ownership and identifiers

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:

Buffers

are owned by the global array bufs (

new_buffer.cpp

). They are created by insert_buffer and destroyed by remove_buffer.

Views

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.

Windows

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:

Buffer

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.

View

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

Window

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.

4.Navigating between the objects

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.

From buffers

buffer_to_views, buffer_to_windows, get_recent_view, get_passive_view.

From views

view_to_buffer, view_to_window, view_to_editor.

From windows

window_to_view, window_to_buffer.

From paths

path_to_buffer (path p) returns the buffer whose root path is a prefix of p.

Enumerations

get_all_buffers, get_all_views, windows_list, number_buffers, get_nr_windows.

Low level

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.

5.Source files

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

.

6.Contents of this chapter

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.

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