![]() |
Buffers | ![]() |
A buffer is an open document. It exists independently of any window: a buffer may be shown in several windows, in one, or in none at all (help pages loaded in the background, the project file of a book, the auxiliary documents built by Scheme). This page describes how a buffer is represented, how it is named, which routines of
Texmacs/Data/new_buffer.cpp
manipulate it, and how buffers are created, loaded, saved, renamed and closed, both at the C++ level and by the user level commands written in Scheme.
A buffer is described by three objects: the tm_buffer_rep which ties everything together, the file related information new_buffer_rep, and the document data new_data_rep. The body of the document is stored in none of them, but in the global edit tree. The “modified” status is not stored at all: it is derived from the undo histories of the editors of the buffer.
tm_buffer_rep
|- buf : new_buffer name, master, title, last_save, …
|- data : new_data style, init, fin, ref, aux, att, project
|- rp : path ---> the_et[rp] = body of the document
|- vws : views ---> each with an editor (cursor, undo, …)
|- prj : tm_buffer ---> the project (master document), if any
|- lns, notify Scheme notifier (shared buffers) |
All document bodies are children of the global tree the_et (
Data/Document/new_document.cpp
), a tuple created at startup in texmacs_entrypoint. The three functions of
new_document.hpp
manage its slots:
Returns the path of a free slot, reusing a child equal to UNINIT if there is one, and appending a new child otherwise. The slot is initialized to an empty document.
Assigns a copy of t to the slot.
Sets the slot to UNINIT and removes the observers of the old subtree.
The path of the slot is the root path rp of the buffer and of all its editors. Since slots are reused, a root path identifies a buffer only for as long as the buffer exists.
Keeping all documents in one tree has two important consequences. First, any subtree of any open document can be designated by an absolute path, and the root ip_observer which is attached to the_et at startup allows to recover this path from the tree itself (obtain_ip); path_to_buffer (p) then finds the buffer whose root path is a prefix of p. Second, all modifications go through the same functions (such as assign (path, tree)), which forward them to the observers attached to the modified subtrees; this is how a change made in one view reaches the typesetters of all other views and the undo histories (see the modification pipeline).
A buffer is identified by its name, which is a URL: there is no separate naming scheme for buffers, and concrete_buffer (url) simply searches the global array bufs for a buffer with the given name. The name is typically
the file name of the document on disk or on the web;
a scratch URL produced by make_new_buffer for new documents (url_scratch ("no_name_", ".tm", i), with the first free \(i\)); such buffers are recognized by buffer_has_name, which returns false for them;
a tmfs:// URL for documents that are generated by Scheme handlers of the TeXmacs file system: help pages, auxiliary documents tmfs://aux/…, embedded input fields tmfs://aux/TeXmacs-input-n, document parts tmfs://part/…, and so on. See the TeXmacs file system.
Besides its name, a buffer has a master URL, with respect to which relative links and file names are resolved, and which is used for navigation. For ordinary documents the master is the name itself. A buffer whose master differs from its name is called auxiliary (is_aux_buffer, buffer-aux?): generated bibliographies, help pages opened from a document, the documents behind embedded widgets. Auxiliary buffers cannot be saved under their own name, but behave as if they were located at their master. The Scheme routine open-auxiliary in
texmacs/texmacs/tm-files.scm
creates such a buffer from a tree and a master (via aux-set-document and aux-set-master of
kernel/texmacs/tm-file-system.scm
), and load-buffer-open sets the master of a tmfs:// buffer to the one proposed by its handler (tmfs-master).
The title of a buffer, shown in window titles and in the Go menu, is computed by propose_title: the last component of the file name, “No name [i]” for scratch buffers, the title returned by the Scheme function tmfs-title for tmfs:// buffers, and a suffix “(2)”, “(3)”, … if another buffer already has the same title.
Since the name is the identifier, renaming a buffer (rename_buffer, used by Save as) changes the identity of the buffer and of all its views: see view identifiers.
Declared in
Texmacs/Data/new_buffer.hpp
; a concrete_struct with the handle new_buffer. Its fields are:
The name of the buffer, which is also its identifier.
The base name used to resolve relative links and file names, and for navigation. Equal to name except for auxiliary buffers.
The format; initialized to "texmacs" and currently not updated by loading or saving (the format of a save is recomputed from the file name).
The title shown in window titles and in the Go menu; computed by propose_title.
A read only buffer never needs to be saved, and its editors typeset it in read only mode. The flag is initialized to false and is currently not set anywhere else.
Whether the document may execute potentially insecure code; initialized with is_secure (name).
The time stamp of the file at the last load or save, used to detect changes on disk. Initialized to the most negative int but one, which means “never saved”.
The last time a view on the buffer became current; used by Scheme to sort recent buffers (buffer-last-visited).
Declared in
Data/Document/new_data.hpp
; a concrete_struct with the handle new_data. It holds the parts of a TeXmacs document other than the body:
The relative name of the project file, or the empty string.
The style, a tuple of style and package names.
The initial and final values of environment variables.
Labels with their references, and the auxiliary data (tables of contents, bibliographies, indices, …).
Attachments, such as the LaTeX source of an imported document.
detach_data (doc, data) splits a complete document into its body (the return value) and data; attach_data (body, data, no_aux) does the converse. attach_data leaves out empty parts, removes from the initial environment the screen dependent variables page-screen-width, page-screen-height and full-screen-mode and, unless no-zoom is set, the zoom factor; with no_aux it also leaves out the references and the auxiliary data.
The data are shared between the buffer and its editors in an asymmetric way. The editor keeps its own copies of the style and of the initial environment (edit_typeset_rep::set_data copies them in), and its own handle on the final environment, but reads and writes ref, aux and att directly in buf->data. The style and initial environment of the buffer are therefore not updated when the user changes them in the editor. They are replaced wholesale by set_buffer_tree, and copied back from the editor by buffer_export, which calls ed->get_data (buf->data) before writing the file (but not for PostScript and PDF). The only other writer is edit_typeset_rep::init_update, which stores some variables of project chapters (such as the first page number) directly in buf->data->init.
Declared in
Texmacs/tm_buffer.hpp
; tm_buffer is a plain pointer to it, and nil_buffer () and is_nil test for NULL. Its fields are:
The file related information.
The document data.
The views on the buffer.
The buffer of the project, or NULL.
The root path of the body in the_et. The constructor obtains it with new_document () and the destructor releases it with delete_document.
The links of the buffer as a whole, used by the buffer notifier.
Whether the notifier is attached.
Its methods are:
Calls the Scheme function buffer-initialize (defined in
part/part-shared.scm
) with the buffer name and its body, and registers the body as a locus with the link type "buffer-notify", so that every modification of the buffer is reported to the Scheme function buffer-notify. This is used to share and mirror whole buffers (see collaboration). It is called from Scheme with buffer-attach-notifier and only once per buffer.
True if the buffer is not read only and some view's editor reports unsaved changes (ed->need_save () and need_save (false)). The “modified” state is thus not stored in the buffer but derived from the undo history of the editors (see undo and redo); this is why a buffer must always keep at least one view.
Most routines of
new_buffer.cpp
take a buffer name and do nothing (or return a neutral value) if there is no such buffer. The exceptions are buffer_export and buffer_save, which go through get_recent_view and therefore create an empty buffer with that name, and last_visited, which returns the current time.
As in much of this layer, the loading and saving routines return true on failure.
Style/Evaluate/evaluate_rewrite.cpp
,
Typeset/Env/env_exec.cpp
).
The C++ routines above are deliberately simple; the policy (which questions are asked, what happens to autosave files, where the buffer is shown) lives in Scheme, in
texmacs/texmacs/tm-files.scm
(loading and saving) and
texmacs/texmacs/tm-server.scm
(closing). The following subsections follow a buffer through its life.
All buffers are created by set_buffer_tree (name, doc): if no buffer with the given name exists, the file local insert_buffer allocates a new tm_buffer_rep (whose constructor obtains a slot in the_et through new_document) and appends it to bufs; the document is split with detach_data, its body is stored with set_document and a title is proposed. The variants are
Only creates the buffer if it does not exist yet.
Creates an empty scratch buffer (buffer-new).
Creates the buffer with default data if needed (buffer-set-body).
Create an empty buffer as a side effect if there is none; see views and the current view.
A freshly created buffer has no view, and hence no editor: it is not typeset, its “modified” status is always false, and the routines which act on the current editor cannot be applied to it. The first view is created when the buffer is displayed, or explicitly with view-new or view-passive (the Scheme macro with-buffer silently does nothing on a buffer without views).
The C++ part of loading is buffer_load (name), which determines the format with file_format and calls buffer_import (name, name, fm); the latter reads and converts the file with import_tree and passes the result to set_buffer_tree. Note the convention of these low level routines: they return true on failure.
The user command load-buffer (File\(\rightarrow\)Load, files on the command line, hyperlinks) goes through a chain of Scheme functions, each of which either stops with a message or calls the next one:
load-buffer-main resolves the name: relative to $TEXMACS_FILE_PATH if the file only exists there, then relative to the current buffer (or to the working directory if there is none).
load-buffer-check-autosave proposes to load a more recent autosave file, or to rescue the file after a crash (unless the option :strict is given). If the user accepts, the autosave file is loaded with buffer-set and the buffer is marked as modified.
load-buffer-check-permissions checks that the file can be read, or created.
load-buffer-load does nothing if the buffer is already open, calls buffer-load if the file exists, and otherwise creates an empty document with the default style.
load-buffer-open displays the buffer: not at all with the option :background, in a new window with :new-window (open-buffer-in-window, that is, new_buffer_in_new_window), and otherwise in the current window with switch-to-buffer. It then records the file in the list of recent files, asks for the passphrase of an encrypted document, and sets the master of tmfs:// buffers.
load-buffer-in-new-window adds :new-window, but does nothing if the buffer is already shown in some window. revert-buffer re-imports the file and replaces the contents with buffer-set; as for any existing buffer, set_buffer_tree then uses assign, so that all views are updated through the modification pipeline, and passes the new document data to all editors (set_data and init_update).
buffer_save (name) exports the buffer to its own name in the format given by its suffix, marks it as saved with pretend_buffer_saved (which calls notify_save on the editors and records the time stamp of the file) and clears the “modified” mark of all windows showing it. buffer_export (name, dest, fm) does the real work. It needs an editor, so it takes the most recent view on the buffer (creating one if necessary), retrieves the body from the_et, applies the editor based conversions for the formats which need the typesetter (exec_verbatim, exec_html, or print_to_file for PostScript and PDF), copies the document data back from the editor (get_data, see the class new_data_rep), attaches them, adds the link locations and writes the result with export_tree. Like the loading routines, these functions return true on failure.
The user command save-buffer calls save-buffer-main, then
save-buffer-check-permissions, which asks for a file name for scratch buffers, refuses to save non existing, unmodified or unwritable buffers and warns when the file changed on disk since buffer-last-save;
save-buffer-check-faithful, which asks for confirmation when the target format is not a faithful TeXmacs format;
save-buffer-save, which finally calls buffer-save.
save-buffer-as renames the buffer with buffer-rename before saving it. Autosaving is implemented in the same file (autosave-buffer, autosave-all, autosave-propose).
rename_buffer (name, new_name) first kills any buffer which already has the new name, then changes name and master, notifies the editors with THE_ENVIRONMENT (relative links must be resolved again), updates the view history (view identifiers contain the buffer name; see notify_rename_before and notify_rename_after) and proposes a new title.
kill_buffer (name) (cpp-buffer-close) first gives every window which displays the buffer something else to show: the most recent passive view on another buffer or, failing that, a new view on the buffer of the most recent view on another buffer. If there is no other buffer at all, the window keeps its view. It then calls remove_buffer, which deletes the views of the buffer, removes it from bufs and deletes the tm_buffer_rep; its destructor frees the slot in the_et with delete_document. If the last buffer is removed and TeXmacs does not act as a server for remote clients (number_of_servers ()), the program quits.
At the user level, safely-kill-buffer asks for confirmation if the buffer is modified and then calls buffer-close; for an embedded buffer it deletes the alternative windows which contain it instead (see embedded widgets). Closing a window also closes its buffer (closing windows), and close-document chooses between the two according to the buffer management preference.
A project is a master document (typically a book) whose chapters are separate files. The chapters share the references and the auxiliary data of the master. Projects are implemented in
Texmacs/Data/new_project.cpp
:
Sets the project field of the data of the current buffer (an empty name detaches it), re-initializes and redecorates all its editors, marks the buffer as modified and loads the project buffer into prj.
True if the current buffer belongs to a project or is one.
The name of the project of the current buffer: the buffer itself for an implicit project, otherwise the project name resolved relative to the directory of the buffer. The project buffer is not loaded by this call.
A buffer is an implicit project (is_implicit_project, file local) if its suffix is tp, or if the project-flag initial variable is set to true: the first editor in which it is explicitly true or false decides, and otherwise the data of the buffer. The project buffer is loaded in the background, without any view, when a chapter is loaded or attached, so that its references are available (since it has no view, buffer_modified is always false for it). texmacs_output_widget also loads a project, given by the project attribute of the document it renders, to render pieces of a project with the right references.
Do not keep tm_buffer pointers across calls which may close buffers; keep the name and call concrete_buffer again.
A buffer without views has no editor: it is not typeset, it is never reported as modified, its modifications are not recorded in any undo history, and with-buffer does not execute its body on it. Create a view (view-passive) first if any of this matters.
get_buffer_tree (buffer-get) returns the style and initial environment stored in the buffer, which are only synchronized with the editors when the buffer is loaded, set or exported. After the user has changed the style or a document setting, the result may therefore be out of date; the body, on the other hand, is always current. It also leaves out the references and the auxiliary data.
buffer_export and buffer_save on a name which is not a buffer create an empty buffer with that name.
Because of the bug in remove_buffer described above, do not assume that all views of a closed buffer are gone.
get_current_buffer and import_tree for a name which cannot be resolved directly assert that there is a current view; use get_current_buffer_safe in code which may run without one.
The low level loading and saving routines return true on failure.