![]() |
Meta information and logical programming | ![]() |
Small software projects usually consist of a collection of routines and data. In a large software project, where a typical contributor has no complete overview of the program, it is a good practice to associate additional meta-information to the individual routines and data. This meta-information typically serves documentation purposes, but becomes even more interesting if it can be used in an automated fashion to implement more general additional functionality.
The tm-define macro supports several options for associating meta-information to Scheme functions and symbols. For instance, the :synopsis, :argument and :returns options allow you to associate short documentation strings to the function, its arguments and its return value:
(tm-define (square x)
(:synopsis "Compute the square of @x")
(:argument x "A number")
(:returns "The square of @x")
(* x x)) |
This information is exploited by TeXmacs in several ways. For instance, the synopsis of the function can be retrieved by executing (help square). More interestingly, assuming that we defined square as above, typing ⌘⌃⇧X followed by square and ↩ allows you to execute square in an interactive way: you will be prompted for “A number” on the footer. Moreover, after typing ⌘⌃⇧X, you will be able to use “tab-completion” in order to enter square: typing SQU⇥ will usually complete into square.
In a similar vein, the :interactive and :check-mark options allow you to specify that a given routine requires interactive user input or when it should give rise to a check-mark when used in a menu. For instance, the statement
(tm-property (open-document-page-format)
(:interactive #t)) |
in the source code of TeXmacs (see
init-texmacs.scm
) states that open-document-page-format is an interactive command. As a consequence, the Document\(\rightarrow\)Page entry, which is defined by
("Page" (open-document-page-format)) |
will be followed by dots … in the Document menu. The interesting point here is that, although the command open-document-page-format may be reused several times in different menu entries, we only have to specify once that it is an interactive command. Similarly, consider the definition
(tm-define (toggle-session-math-input)
(:synopsis "Toggle mathematical input in sessions")
(:check-mark "v" session-math-input?)
…) |
Given a menu item with (toggle-session-math-input) as its associated action, this definition specifies in particular that a check-mark should be displayed before the menu item whenever the session-math-input? predicate holds.
Another frequently used option is :secure, which specifies that a given routine can be used inside TeXmacs documents, in particular inside extern and action macros. For instance, the default implementation of the folded tag allows the user to click on the “\(\circ\)” before the folded text so as to unfold the tag. When doing this, the scheme script mouse-unfold is launched. However, for this to work, the mouse-unfold function needs to be secure (see
utils/edit/variants.scm
):
(tm-define (mouse-unfold t)
(:type (-> void))
(:synopsis "Unfold using the mouse")
(:secure #t)
(when (tree->path t)
(tree-go-to t :start)
(when (tree-up t)
(alternate-unfold (tree-up t))))) |
You can read more about the tags which depend on Scheme scripts in “Invoking Scheme scripts from TeXmacs markup”.
The complete list of options currently understood by tm-define and tm-property (see
kernel/texmacs/tm-define.scm
) is: :mode and :require (for contextual overloading), :applicable (a condition under which a menu entry for the command is active, rather than greyed out), :synopsis, :synopsis*, :returns, :note, :type, :argument, :default and :proposals (a default value resp. a list of proposals for an argument when the command is executed interactively), :secure, :check-mark, :interactive and :balloon (a tooltip for menu entries). Unknown options raise an error.
In the future, the number of options for entering meta-information is likely to increase. TeXmacs also supports an additional mechanism for the automatic deduction of new meta-properties from existing meta-properties. This mechanism is based on a less general, but more efficient form of logical programming, which is described in the section on logical programming.