GNU TeXmacsUser manualReferenceAboutEnglish
A tmfs primer

1.The TeXmacs file system

Many things in TeXmacs can be referenced through a URL with tmfs as its protocol. Examples are help pages, search results, the history of a file under version control, auxiliary buffers used by dialogs, remote files and chat rooms. Internally, TeXmacs also uses tmfs URLs as identifiers for views and windows. A TeXmacs file system URL has the format

tmfs://class/name

The first component, class, selects a handler; the remainder, name, is passed as a string to that handler and is interpreted by it. A handler is a set of Scheme procedures which implement the basic operations on the documents of its class: loading the content, saving it (if this makes sense), computing a title for the window and establishing access permissions are the most important ones; see the tmfs Scheme API for the complete list.

Handlers which the user commonly encounters are help, apidoc, grep, history and revision. The format of name depends on the handler:

The catalogue of handlers describes the syntax accepted by each of the existing handlers.

Situations where using this system makes more sense than regular documents are for instance documentation, which must be chosen from several languages and possibly be compiled on the fly from various sources (see the module doc.apidoc and related modules), and automatically generated content, like the content resulting from the interaction with an external version control system (see the handlers history and revision in the module version.version-tmfs, and git and commit in the module version.version-git).

2.Implementing a handler

A handler is defined via tmfs-handler or, more conveniently, with the macros tmfs-load-handler, tmfs-save-handler, tmfs-title-handler, tmfs-permission-handler, tmfs-master-handler and a few others described in the API reference. Only the load handler is mandatory.

Below we implement a basic handler named simple which accepts queries with two variables, type and what. We use two procedures, one to create the document, another one to handle the requests.

Scheme]

(tm-define (simple-load header body)

‘(document

(TeXmacs ,(texmacs-version))

(style (tuple "generic"))

(body (document (section ,header) ,body))))

As you can see, we do not do much other than creating a TeXmacs document in Scheme form. The load handler is not complicated either. We only parse the query string with the help of query-ref and then return one of three possible documents.

Scheme]

(tmfs-load-handler (simple qry)

(let ((type (query-ref qry "type"))

(what (query-ref qry "what")))

(cond ((== type "very") (simple-load "Very simple" what))

((== type "totally") (simple-load "Totally simple" what))

(else (simple-load "Error"

(string-append "Query unknown: " what))))))

A load handler may return either a document in Scheme form, as above, or a string holding the serialized document. We can test the handler right away with:

Scheme]

(load-buffer "tmfs://simple/type=very&what=example")

or from a document using tags like hlink and branch: click here to test it. Notice that the URL is loaded anew each time the buffer is opened or reverted: there is no cache.

By default, a document of a class with a load handler can only be read. You may grant other permissions by implementing a permission handler. Such a handler receives the name and the kind of access requested, which is "read" or "write":

Scheme]

(tmfs-permission-handler (simple name type)

(display* "Name= " name "\nType= " type "\n")

(in? type (list "read" "write")))

Once write access is granted, TeXmacs lets the user save the buffer and calls the save handler with the document in Scheme form. If there is no save handler, saving silently does nothing.

Scheme]

(tmfs-save-handler (simple qry doc)

(display* "Saving " qry "\n")

(set-message "Nothing to save" "simple handler"))

Finally, the title of the window displaying the buffer is computed by the title handler. Without a title handler, the full URL is used as title.

Scheme]

(tmfs-title-handler (simple qry doc)

(string-append "Simple handler - " (query-ref qry "what")))

3.The main handler macros

(tmfs-load-handler (class name) body)
(define the load handler for class)

A load handler for class is invoked when TeXmacs needs the contents of a URL of the form tmfs://class/name. The body is evaluated with name bound to the string following the class (for instance a query to be parsed with query-ref) and must return a complete TeXmacs document, either in Scheme form or as a string. Consider the following example, which is actually defined in

kernel/texmacs/tm-file-system.scm

:

(tmfs-load-handler (id what)
  ‘(document
     (TeXmacs ,(texmacs-version))
     (style (tuple "generic"))
     (body (document ,what))))

This will display the text whatever when opening the URL tmfs://id/whatever.

The creation of the document may be simplified using the macros defined in the module kernel.gui.gui-markup, such as $generic, $tmdoc and $tmfs-title.

(tmfs-save-handler (class name doc) body)
(define the save handler for class)

A save handler is invoked when the user saves a buffer whose name is tmfs://class/name. The argument doc contains the document in Scheme form. The save command refuses to save the buffer unless the permission handler grants "write" access. The return value of the handler is ignored; errors should be reported by the handler itself.

(tmfs-title-handler (class name doc) body)
(define the title handler for class)

A title handler is invoked to build the title of a window displaying a buffer of the form tmfs://class/name. The argument doc is the loaded document, which may be used to extract a title from its contents. The handler is expected to return a string, preferably translated into the language of the user.

(tmfs-permission-handler (class name kind) body)
(define the permission handler for class)

A permission handler decides whether the document tmfs://class/name may be accessed in the way specified by kind, which is either "read" (the document may be loaded; this is also how existence is tested) or "write" (the document may be saved). The handler returns a boolean. Without a permission handler, only "read" access is granted.

(tmfs-master-handler (class name) body)
(define the master handler for class)

A master handler returns the URL which should serve as the master of the buffer tmfs://class/name. After loading, the buffer master is set to this URL (see buffer-set-master); it is used for instance to resolve relative hyperlinks and inclusions. The part handler uses it so that a part of a large document behaves like the file it was extracted from. Without a master handler, the buffer is its own master.

(query-ref qry var)
(return the value of the variable var in the query qry)

Given a query string qry of the form variable1=value1&variable2=value2, query-ref returns "value1" for the var "variable1", and so on. The empty string is returned if the variable does not occur in the query.

4.Installing the handler

A handler becomes available as soon as the module which defines it is loaded. In order to make it available from any menu item or document upon startup without loading your code at startup, register it in my-init-texmacs.scm (or, for handlers which are part of TeXmacs, in

init-texmacs.scm

) using the macro lazy-tmfs-handler. The module is then loaded the first time that a URL of one of the registered classes is accessed.

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

Inform TeXmacs that the handlers for the classes class1, …, classn are defined in the module module. The arguments are not evaluated: module is a list of symbols (like (doc tmdoc)) naming the Scheme module, and the classes are symbols. For instance,

init-texmacs.scm

contains

(lazy-tmfs-handler (doc tmdoc) help)

Only the listed classes trigger the loading of the module: if a module defines several handlers, list all of them.

Remark 1. The classes view and window must not be used as names of handlers, since URLs tmfs://view/… and tmfs://window/… are used internally by TeXmacs as identifiers for views and windows. The classes of the handlers listed in the catalogue, such as aux, id and import, are of course also taken. Finally, a URL tmfs://foo without any slash after the class is treated as a name in the class file.

More details about how handlers are dispatched and how the C++ file layer delegates to them can be found in the internals of tmfs.

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