![]() |
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.
This routine returns the list of all open buffers, the most recently created ones first.
Check whether buf (a URL or a string) is among the open buffers.
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.
Return the buffer which contains a certain path p, or #f. The glued variant path-to-buffer returns (url-none) instead of #f.
Return the buffer which contains a certain tree t, or #f.
(buffer->tree buf)
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.
This routine returns the list of views on the buffer buf.
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.
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).
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.
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).
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.
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.
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
.
(buffer-set buf rich-t)
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)
Set the main body of the buffer buf to the tree t, resp. get the main body of buf.
(buffer-set-master buf master)
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)
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)
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)
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)
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.
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.
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.
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.
Import the buffer buf from src, using the format fm. Returns #t on error and #f otherwise.
Export the buffer buf to dest, using the format fm. Returns #t on error and #f otherwise.
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.
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)
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 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)
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.
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.
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.
Create a buffer u with a copy of the body, style, initial environment and references of buf, and return u.
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/.
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.
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?)
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.