GNU TeXmacsUser manualReferenceAboutEnglish
The tmfs Scheme API

1.Introduction

This page documents the Scheme interface of the TeXmacs file system. Unless stated otherwise, the functions and macros are defined in the module (kernel texmacs tm-file-system), file

src/TeXmacs/progs/kernel/texmacs/tm-file-system.scm

, and are available everywhere. In the descriptions, class is the first component of a tmfs URL and name the rest of the URL after tmfs://class/. See the primer for an introduction and the internals for the way these functions are used by the rest of TeXmacs.

2.Defining handlers

(tmfs-handler class action handle)
(register a handler)

Register the procedure handle for the operation action on the URLs of the class class. class is a string, or #t for the default handler of the operation. action is one of the symbols load, save, autosave, remove, wrap, date, title, permission?, master and format; the arguments of handle are those of the corresponding macros below. Registering a handler for a class and action which already have one replaces the old handler.

The following macros are more convenient. Each of them takes a head (class arg1 …), where class is a symbol and the other elements are the names of the variables to which the arguments of the handler are bound, followed by the body of the handler.

(tmfs-load-handler (class name) body)
(define how to load documents)

The body returns the document tmfs://class/name, either as a Scheme tree ((document (TeXmacs …) (style …) (body …))) or as a string in the format returned by tmfs-format. It should never return the empty string, which is interpreted as a failure. This is the only mandatory handler of a class.

(tmfs-save-handler (class name doc) body)
(define how to save documents)

The body saves the document doc, given as a Scheme tree. The save command of TeXmacs refuses to save the buffer unless the permission handler grants "write" access. The result of the handler is ignored.

(tmfs-title-handler (class name doc) body)
(define window titles)

The body returns a string to be used as the title of the buffer; the loaded document doc is provided so that the title may be extracted from it. Without a title handler, the complete URL is used as title.

(tmfs-permission-handler (class name kind) body)
(define access permissions)

The body returns a boolean which tells whether the access kind, either "read" or "write", is granted. Read permission is also what url-exists? tests. Without a permission handler, only read access is granted, unless the class has a wrap handler, in which case the permissions of the wrapped file are used.

(tmfs-master-handler (class name) body)
(define the master of documents)

The body returns the URL to be used as the master of the buffer (see buffer-set-master) when the document is loaded. Without a master handler, the buffer is its own master.

(tmfs-format-handler (class name) body)
(define the file format of documents)

The body returns the format of the document as a string, such as "texmacs" or "png". The format determines how the string produced by the load handler is parsed, and how the buffer is serialized before saving. Without a format handler, the format is "stm", the Scheme representation of TeXmacs documents. For instance, the revision handler uses the format of the underlying file:

(tmfs-format-handler (revision name)
  (with u (tmfs-string->url (tmfs-cdr name))
    (url-format u)))
(tmfs-wrap-handler (class name) body)
(declare the underlying file)

The body returns the URL of an ordinary file of which the document is a different presentation, or #f. The wrapped file is used by the default permission, date, removal and autosave handlers, and by buffer-last-save.

(tmfs-autosave-handler (class name suf) body)
(define autosave locations)

The body returns the URL where an autosave copy of the document with suffix suf ("~" or "#") should be stored, or #f if autosaving is not supported.

(tmfs-date-handler (class name) body)
(define modification dates)

The body returns the date of last modification of the document, in the same unit as url-last-modified, or #f.

(tmfs-remove-handler (class name) body)
(define removal)

The body removes the document and should return #t on success.

(lazy-tmfs-handler module class1 … classn)
(register handlers lazily)

Declare that the handlers of the classes class1, …, classn (symbols) are defined in module (a list of symbols such as (part part-tmfs)). None of the arguments is evaluated. The module is loaded when one of these classes is used for the first time.

(lazy-tmfs-force class)
(load the module of a class)

Load the module registered for class (a string or a symbol), if it has not been loaded yet. All the operations below call this function before looking up a handler.

3.Operations on tmfs URLs

The following functions take an URL u, as a string or an URL object, decompose it with tmfs-decompose-name and call the appropriate handler, or a default behavior. Most code should use the generic functions for files and buffers instead, which call these functions for tmfs URLs.

(tmfs-load u)
(load a document)

Return the document at u as a string. Called by the C++ routine get_from_server.

(tmfs-save u what)
(save a document)

Save the string what, which is converted from stm into a Scheme tree before being passed to the save handler. Called by the C++ routine save_to_server.

(tmfs-title u doc)
(title of a document)

Return the title of the document doc loaded from u. Called by the C++ routine propose_title, and by the menu of recent files.

(tmfs-permission? u kind)
(check access permissions)

Check whether the access kind ("read" or "write") to u is permitted. Called by the C++ routine is_of_type, hence by url-test? and url-exists?.

(tmfs-master u)
(master of a document)

Return the URL which should serve as the master of the buffer u; by default, u itself.

(tmfs-format u)
(file format of a document)

Return the file format of u; by default "stm". Called by the C++ routine file_format, hence by url-format.

(tmfs-wrap u)
(underlying file)

Return the file wrapped by u, or #f. The generic function url-wrap in

texmacs/texmacs/tm-files.scm

calls it for tmfs URLs and returns #f for other URLs.

(tmfs-date u)
(date of last modification)

Return the date of last modification of u, or #f. Used by url-last-modified and url-newer?, which are redefined in

tm-files.scm

for tmfs URLs.

(tmfs-remove u)
(remove a document)

Remove u and return #t on success. Used by url-remove.

(tmfs-autosave u suf)
(autosave location)

Return the URL where the autosave copy of u with suffix suf is stored, or #f. Used by url-autosave.

(tmfs-can-autosave? u)
(whether autosaving is supported)

Return #t if (tmfs-autosave u "~") is not #f. Called by the C++ routine get_from_server.

(tmfs-remote? u)
(whether the class has no local load handler)

Return #t if no load handler is known for the class of u, even after the lazy loading of its module.

(tmfs-decompose-name u)
(split an URL into class and name)

Return the list (class name) of two strings. The prefix tmfs:// is optional; if there is no slash, the class is "file".

(tmfs-decompose-name "tmfs://grep/type=doc&what=tmfs")
;; => ("grep" "type=doc&what=tmfs")

4.Queries

(query-ref qry var)
(value of a query variable)

Given a query string qry of the form var1=val1&var2=val2, return the value of the variable var, for instance "val1" for var equal to "var1". The empty string is returned if the variable does not occur. The value is decoded with tmstring->string.

(query->list qry)
(parse a query)

Return the association list of the variables and values of qry, with the escape %3A replaced by a colon.

(list->query l)
(build a query)

Build a query string from an association list of strings, replacing colons by %3A. For instance,

doc/docgrep.scm

opens search results with

(with query (list->query (list (cons "type" "doc") (cons "what" what)))
  (load-document (string-append "tmfs://grep/" query)))

5.Paths and embedded file names

(tmfs-pair? s)
(whether a name has several components)

Return a true value if the string s contains a slash.

(tmfs-car s)
(first component)

Return the part of s before the first slash, or #f if there is no slash.

(tmfs-cdr s)
(remaining components)

Return the part of s after the first slash, or #f if there is no slash.

(tmfs->list s)
(split a name at slashes)

Return the list of the components of s.

(list->tmfs l)
(join components)

Join a list of strings with slashes; inverse of tmfs->list.

(url->tmfs-string u)
(encode a file name)

Encode the URL u as a string which may be embedded in a tmfs name: tm/… for files under $TEXMACS_PATH, file/… for other local files, here/… for relative names and protocol/… otherwise.

(tmfs-string->url s)
(decode a file name)

Inverse of url->tmfs-string.

(strip-colon s)
(remove the colon of a drive letter)

Turn c:/dir into c/dir; used by url->tmfs-string on Windows.

6.Documents

(tmfs-document t)
(complete a document)

Convert the tree t into a Scheme tree and, if it is a document without a TeXmacs version tag, insert the version of TeXmacs. Useful to return imported documents from load handlers.

(object->tmstring obj)
(serialize a Scheme object)

Serialize obj as a string. tmfs-load converts a (document …) returned by a load handler with texmacs->stm; it only uses object->tmstring for the other results which are not strings.

The module (kernel gui gui-markup) provides macros which simplify the creation of documents: $generic and $tmdoc build complete documents with the generic and tmdoc styles, and $tmfs-title produces a title using the tmfs-title markup. For instance, the history handler is written as

(tmfs-load-handler (history name)
  (let* ((u (tmfs-string->url name))
         (h (version-history u))
         …)
    ($generic
     ($tmfs-title "History of " …)
     ($when (not h)
       "This file is not under version control.")
     …)))

7.Auxiliary buffers

(aux-name aux)
(name of an auxiliary buffer)

Return the URL tmfs://aux/aux.

(aux-set-document aux doc)
(set the contents of an auxiliary buffer)

Set the contents of the auxiliary buffer aux to doc and remember them in the table aux-buffers, so that the buffer can be reloaded.

(aux-set-master aux master)
(set the master of an auxiliary buffer)

Set the master of the auxiliary buffer aux and remember it in the table aux-masters.

(open-auxiliary aux body [master])
(open an auxiliary buffer)

Defined in

texmacs/texmacs/tm-files.scm

. Set the contents and the master (by default the master of the current buffer) of the auxiliary buffer aux and switch to it.

8.URL predicates

(url-rooted-tmfs? u)
(test for tmfs URLs)

Return #t if u is a tmfs URL. Glue for the C++ function is_rooted_tmfs.

(url-rooted-tmfs-protocol? u class)
(test for a given class)

Return #t if u is a tmfs URL of the class class, as in (url-rooted-tmfs-protocol? u "part").

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