GNU TeXmacsUser manualReferenceAboutEnglish
Manipulating TeXmacs buffers

Buffers are identified by their URLs. Most of the routines below are glued directly to C++ functions in

Texmacs/Data/new_buffer.cpp

(see

Scheme/Glue/build-glue-basic.scm

); a few convenience wrappers are defined in

kernel/library/base.scm

,

texmacs/texmacs/tm-files.scm

and

texmacs/texmacs/tm-server.scm

. The underlying C++ data structures are described in buffers.

Basic buffer management
(buffer-list)
(list of all buffers)

This routine returns the list of all open buffers, the most recently created ones first.

(buffer-exists? buf)
(check whether a buffer is open)

Check whether buf (a URL or a string) is among the open buffers.

(current-buffer)
(current buffer)

Return the buffer of the current view, or #f if there is no current view. The underlying glued routine (current-buffer-url) returns (url-none) in the latter case.

(path->buffer p)
(buffer which contains a certain path)

Return the buffer which contains a certain path p, or #f. The glued variant path-to-buffer returns (url-none) instead of #f.

(tree->buffer t)
(buffer which contains a certain tree)

Return the buffer which contains a certain tree t, or #f.

(buffer->tree buf)

(buffer->path buf)
(body of a buffer as a tree or path)

Return the body of the buffer buf as an active tree (resp. its path in the global edit tree), or #f if the buffer does not exist.

(buffer->views buf)
(list of views on a buffer)

This routine returns the list of views on the buffer buf.

(buffer->windows buf)
(list of windows containing buffer)

This routine returns the list of windows in which the buffer buf is currently being displayed. The routine (buffer->window buf) returns the first of these windows, or #f.

(buffer-new)
(create a new buffer)

Create a new buffer with an empty document and return its URL. The URL is a scratch URL of the form $TEXMACS_HOME_PATH/texts/scratch/no_name_n.tm (the first such file which does not exist yet), for which buffer-has-name? returns #f. The buffer is not displayed in any window; use switch-to-buffer or open-buffer-in-window for this purpose (the glued routine (new-buffer) creates a new buffer and switches to it in the current window).

(buffer-rename buf new-name)
(rename a buffer)

Give a new name new-name to the buffer buf. Any other buffer which was already open under the name new-name is closed first. The master of the buffer is reset to new-name and a new title is proposed.

(buffer-close buf)
(close a buffer)

Close the buffer buf without asking for confirmation, even if it was modified. This routine, defined in

tm-server.scm

, calls the glued cpp-buffer-close; redefine it to act on the closing of buffers. Windows which displayed the buffer switch to another buffer. Closing the last buffer quits TeXmacs (unless it is running as a server).

(switch-to-buffer buf)
(switch the editor's focus)

Display the buffer buf in the current window and make it the current buffer. If the buffer is not open yet, then it is loaded first. The variant (switch-to-buffer* buf) rather switches to a window which already displays buf, if such a window exists.

(buffer-focus buf)
(focus on a buffer)

Make the most recent view on buf (preferably one which is displayed in a window) the current view, without changing what is displayed in the windows. Returns #f if there is no view on buf. This routine is mainly used through the macro with-buffer below. The variant (buffer-focus* buf) also moves the keyboard focus to the new view.

(with-buffer buf body …)
(execute code in the context of a buffer)

Temporarily focus on the buffer buf, evaluate body, restore the focus and return the value of the last expression. If buf is not an open buffer, then body is not evaluated and #f is returned. The similar macro (with-window win body …) executes body in the context of the buffer displayed in the window win. Both macros are defined in

utils/library/cursor.scm

.

Information associated to buffers

(buffer-set buf rich-t)

(buffer-get buf)
(set/get the contents of the buffer)

Set the contents of the buffer buf to the rich tree rich-t, resp. get the rich contents of buf. Rich trees do not only contain the actual body of the document, but also some meta-data, such as its style, initial values of environment variables, and other auxiliary data attached to the document.

(buffer-set-body buf t)

(buffer-get-body buf)
(set/get the main body of the buffer)

Set the main body of the buffer buf to the tree t, resp. get the main body of buf.

(buffer-set-master buf master)

(buffer-get-master buf)
(set/get the master of the buffer)

Set the master of the buffer buf to master, resp. get the master of buf. The master of a buffer should again be a buffer. Usually, the master of a buffer is the buffer itself. Otherwise, the buffer will behave similarly as its master in some respects. For instance, if a buffer a/b.tm admits x/y.tm as its master, then a hyperlink to c.tm will point to x/c.tm and not to a/c.tm. The shorthand (buffer-master) returns the master of the current buffer.

(buffer-set-title buf name)

(buffer-get-title buf)
(set/get the title of the buffer)

Set the title of the buffer buf to the string name, resp. get the title of buf. The title is for instance used as the title for the window.

(buffer-last-save buf)

(buffer-last-visited buf)
(time when a buffer was visited/saved last)

Return the time when the buffer buf was saved (an integer) resp. visited (a floating point number) for the last time. For a tmfs buffer which wraps a file or another buffer (see url-wrap), buffer-last-save returns the time when the wrapped buffer was saved, or the time of last modification of the wrapped file (this wrapper is defined in

tm-files.scm

).

(buffer-modified? buf)

(buffer-pretend-saved buf)
(check for modifications since last save)

The predicate buffer-modified? check whether the buffer buf was modified since the last time it was saved. The routine buffer-pretend-saved can be used in order to pretend that the buffer buf was saved, without actually saving it. This can for instance be useful if no worthwhile changes occurred in the buffer since the genuine last save. Conversely, (buffer-pretend-modified buf) marks the buffer as modified. The routines buffer-modified-since-autosave? and buffer-pretend-autosaved are similar, but with respect to the last automatic save.

(buffer-has-name? buf)

(buffer-aux? buf)

(buffer-embedded? buf)
(kind of buffer)

The predicate buffer-has-name? checks whether buf has a genuine name (and not a scratch name as created by buffer-new). The predicate buffer-aux? checks whether the buffer is auxiliary, i.e. whether its master differs from the buffer itself. The predicate buffer-embedded? checks whether the buffer is displayed in an auxiliary window, as an embedded TeXmacs widget.

Synchronizing with the external world

Buffers inside TeXmacs usually correspond to actual files on disk or elsewhere. When changes occur on either side (e.g. when editing the buffer, or modifying the file on disk using an external program), the following routines can be used in order to synchronize the buffer inside TeXmacs with its corresponding file on disk.

(buffer-load buf)
(load buffer)

Retrieve the buffer buf from disk (or elsewhere). Returns #t on error and #f otherwise. The format being used for loading files is chosen as a function of the extension of buf.

(buffer-save buf)
(save buffer)

Save the buffer buf to disk (or elsewhere). Returns #t on error and #f otherwise. The format being used for saving files is chosen as a function of the extension of buf.

(buffer-import buf src fm)
(import buffer)

Import the buffer buf from src, using the format fm. Returns #t on error and #f otherwise.

(buffer-export buf dest fm)
(export buffer)

Export the buffer buf to dest, using the format fm. Returns #t on error and #f otherwise.

(tree-import src fm)
(import a tree)

Import a tree from the URL src, using the format fm. If src is relative and cannot be resolved, it is resolved with respect to the directory of the current buffer. On failure, the string tree "error" is returned.

(tree-export t dest fm)
(export a tree)

Export a tree to the URL dest, using the format fm. Returns #t on error and #f otherwise.

These are low level routines. The interactive commands (load-buffer name) and (save-buffer) from

texmacs/texmacs/tm-files.scm

in addition take care of opening the buffer in a window, of autosave files, permissions, confirmation dialogues, etc.

(tree-inclusion u)

(tree-load-style name)
(cached documents)

Return the body of the document u, as included with the include tag, resp. the body of the style package name (searched in $TEXMACS_STYLE_PATH, unless the name ends with .ts). Both results are cached.

Auxiliary buffers

Auxiliary buffers hold documents which are not files, such as the results of a search or a preview. They are named tmfs://aux/name, and are managed by routines of

kernel/texmacs/tm-file-system.scm

and

tm-files.scm

:

(aux-name aux)

(aux-set-document aux doc)

(aux-set-master aux master)
(auxiliary buffers)

The first routine returns the URL tmfs://aux/aux of the auxiliary buffer with the name aux (a string). The second one sets its document to doc (the buffer is created if needed), and the third one its master, which is used for resolving relative links. When nothing was set, the buffer contains an empty document of the generic style, and is its own master.

(open-auxiliary aux body . opt-master)
(show an auxiliary buffer)

Set the document of the auxiliary buffer aux to body, its master to opt-master (by default the master of the current buffer) and show it in the current window.

(with-aux u body …)
(work on a copy of a file)

Load the file u into the auxiliary buffer * Aux *, switch to it, evaluate body, switch back to the current buffer and return the value of the last expression.

Other routines
(buffer-copy buf u)
(copy a buffer)

Create a buffer u with a copy of the body, style, initial environment and references of buf, and return u.

(buffer-in-menu? u)
(menu of buffers)

Whether the buffer u should be listed in the Go menu: buffers of files and of tmfs://part/, tmfs://help/, tmfs://remote-file/ and tmfs://apidoc/.

(buffer-attach-notifier buf)
(notify changes)

Call buffer-initialize on the document of buf, and from then on buffer-notify on each of its modifications. These routines are defined in

part/part-shared.scm

, which uses them to keep shared documents and their copies in sync.

(kill-current-window-and-buffer)
(close the current window)

Close the current window, and its buffer if it is not displayed in another window. TeXmacs quits when it was the last buffer.

(project-attach name)

(project-detach)

(project-attached?)

(project-get)
(projects)

Make the current buffer a part of the project whose main file is name (relative to the directory of the buffer; the empty string detaches it), test whether the current buffer belongs to a project, resp. return the URL of the main file of its project. A buffer which belongs to a project shares its references and bibliography.

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