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

1.The global edit tree

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:

path new_document ()

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.

void set_document (path rp, tree t)

Assigns a copy of t to the slot.

void delete_document (path rp)

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

2.Buffer names

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

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.

3.The buffer classes

3.1.The class new_buffer_rep

class new_buffer_rep
(file related information)

Declared in

Texmacs/Data/new_buffer.hpp

; a concrete_struct with the handle new_buffer. Its fields are:

url name

The name of the buffer, which is also its identifier.

url master

The base name used to resolve relative links and file names, and for navigation. Equal to name except for auxiliary buffers.

string fm

The format; initialized to "texmacs" and currently not updated by loading or saving (the format of a save is recomputed from the file name).

string title

The title shown in window titles and in the Go menu; computed by propose_title.

bool read_only

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.

bool secure

Whether the document may execute potentially insecure code; initialized with is_secure (name).

int last_save

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

time_t last_visit

The last time a view on the buffer became current; used by Scheme to sort recent buffers (buffer-last-visited).

3.2.The class new_data_rep

class new_data_rep
(everything but the body)

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:

tree project

The relative name of the project file, or the empty string.

tree style

The style, a tuple of style and package names.

init, fin

The initial and final values of environment variables.

ref, aux

Labels with their references, and the auxiliary data (tables of contents, bibliographies, indices, …).

att

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.

3.3.The class tm_buffer_rep

class tm_buffer_rep
(an open document)

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:

new_buffer buf

The file related information.

new_data data

The document data.

array<tm_view> vws

The views on the buffer.

tm_buffer prj

The buffer of the project, or NULL.

path rp

The root path of the body in the_et. The constructor obtains it with new_document () and the destructor releases it with delete_document.

link_repository lns

The links of the buffer as a whole, used by the buffer notifier.

bool notify

Whether the notifier is attached.

Its methods are:

attach_notifier ()

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.

needs_to_be_saved (), needs_to_be_autosaved ()

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.

4.Reference of the buffer routines

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.

The list of buffers.
get_all_buffers returns the names in reverse order of creation; number_buffers, get_current_buffer (asserts that there is a current view), get_current_buffer_safe, path_to_buffer (p) (the buffer whose root path is a prefix of p). remove_buffer is meant to delete all views, then remove the buffer from bufs and delete it; if this was the last buffer and no remote clients are connected, the program quits first. Note that its loop for (i=0; i<N(buf->vws); i++) delete_view (…) skips every other view, because delete_view removes the view from buf->vws; with two or more views, some views survive the deletion of their buffer, with a dangling buffer pointer, and remain in the view history. This is a bug in the code (it should loop while buf->vws is not empty).
Names and titles.
make_new_buffer creates an empty buffer with the first free scratch name no_name_i.tm; buffer_has_name is false for scratch names. propose_title computes a title: the file name, “No name [i]” for scratch buffers, the title given by the Scheme function tmfs-title for tmfs:// buffers, and a suffix “(2)”, “(3)”, … if another buffer has the same title. set_title_buffer also updates the title and file of all windows which show the buffer. rename_buffer is described below.
Contents.
set_buffer_tree (name, doc) creates the buffer if needed, splits the document with detach_data, stores the body (with set_document for a new buffer, with assign for an existing one, so that the editors are notified), passes the new data to the editors, recomputes the title, loads the project buffer if the document declares a project (for an existing buffer, only if the project changed), and marks the buffer as saved. get_buffer_tree returns attach_data (body, data, true). set_buffer_body and get_buffer_body work on the body only (set_buffer_body also marks the buffer as saved).
Master and auxiliary buffers.
set_master_buffer and get_master_buffer; changing the master notifies the editors with THE_ENVIRONMENT, since relative links must be re-resolved. is_aux_buffer tests whether the master differs from the name.
Save status.
buffer_modified, buffer_modified_since_autosave, pretend_buffer_modified (calls require_save on the editors), pretend_buffer_saved (calls notify_save on the editors and records the time stamp of the file in last_save), pretend_buffer_autosaved, get_last_save_buffer, set_last_save_buffer, last_visited.
Loading.
import_tree (u, fm) resolves u (also relative to the current buffer), reads the file and calls import_loaded_tree (s, u, fm). The latter guesses the format if it is generic, converts the string to a tree with generic_to_tree, registers the link locations stored in the document, and calls attach_subformat, which turns source files of a known programming language into a document with style code, mode prog and the right prog-language. buffer_import (name, src, fm) and buffer_load (name) put the result in a buffer.
Saving.
export_tree (doc, u, fm) converts a complete document with tree_to_generic and writes it; documents in TeXmacs format whose initial environment contains an encryption variable are first passed through the Scheme hook tree-export-encrypted. buffer_export (name, dest, fm) uses the most recent view on the buffer: for PostScript and PDF it prints with the editor, for verbatim and html it first lets the editor expand the body, then it synchronizes the data from the editor, attaches them (without the auxiliary data if the editor says so), adds the link locations and, for LaTeX, the view URL, so that the converter can later expand macros with the typesetter of that view (latex_expand). buffer_save (name) exports to the file itself, marks the buffer as saved and clears the “modified” mark of its windows.

As in much of this layer, the loading and saving routines return true on failure.

Inclusions and style trees.
load_inclusion (name) loads and caches documents included with include; reset_inclusions empties the cache. load_style_tree (package) loads and caches the body of a style package, and with_package_definitions (package, body) wraps a body in a with which sets all the variables assigned by the package; it implements the with-package primitive (
Style/Evaluate/evaluate_rewrite.cpp

,

Typeset/Env/env_exec.cpp

).

5.Life cycle of a buffer

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.

5.1.Creation

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

create_buffer (name, doc)

Only creates the buffer if it does not exist yet.

make_new_buffer ()

Creates an empty scratch buffer (buffer-new).

set_buffer_body (name, body)

Creates the buffer with default data if needed (buffer-set-body).

get_new_view (name), get_recent_view (name)

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

5.2.Loading

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:

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

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

  3. load-buffer-check-permissions checks that the file can be read, or created.

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

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

5.3.Saving and exporting

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

  1. 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;

  2. save-buffer-check-faithful, which asks for confirmation when the target format is not a faithful TeXmacs format;

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

5.4.Renaming

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.

5.5.Closing

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.

6.Projects

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

:

project_attach (prj_name)

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.

project_attached ()

True if the current buffer belongs to a project or is one.

project_get ()

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.

7.Pitfalls

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