GNU TeXmacsUser manualReferenceAboutEnglish
Function definition and contextual overloading

Conventional programming languages often provide some means to overload certain functions depending on the types of the arguments. TeXmacs provides additional context-based overloading mechanisms, which require the use of the tm-define construct for function definitions (and tm-define-macro for macro definitions). Definition with tm-define also allows the specification of properties of the function/macro: arguments, synopsis, etc.

Furthermore, one may use tm-property for associating additional properties, such as interactivity or default values for the arguments, of a function which is already defined, specifically functions exported from C++ code through the glue.

(tm-define head options* body*)
(TeXmacs function definition)
(tm-define-macro head options* body*)
(TeXmacs macro definition)

TeXmacs function and macro declarations are similar to usual declarations based on define and define-macro, except for the additional list of options and the fact that all functions and macros defined using tm-define and tm-define-macro are public. Each option is of the form (:kind arguments*) and the body starts at the first element of the list following head which is not of this form. Available options are :mode, :require and :applicable (for contextual overloading), :type, :synopsis, :synopsis*, :returns, :note, :argument, :default, :proposals, :secure, :check-mark, :interactive and :balloon. The implementation can be found in

kernel/texmacs/tm-define.scm

.

(tm-property head options*)
(TeXmacs properties definition)

tm-property allows the declaration of TeXmacs properties for functions which have already been defined, specifically for functions exported through the glue. Available options are the same as for tm-define, except for the ones used for contextual overloading.

Contextual overloading

We will first describe the most important :require option for contextual overloading, which was already discussed before.

(:require cond)
(argument based overloading)

This option specifies that one necessary condition for the declaration to be valid is that the condition cond is met. This condition may involve the arguments of the function.

As an example, let us consider the following definitions:

(tm-define (special t)
  (and-with p (tree-outer t)
    (special p)))

(tm-define (special t)
  (:require (tree-is? t 'frac))
  (tree-set! t ‘(frac ,(tree-ref t 1) ,(tree-ref t 0))))

(tm-define (special t)
  (:require (tree-is? t 'rsub))
  (tree-set! t ‘(rsup ,(tree-ref t 0))))

The default implementation of special is to apply special to the parent p of t as long as t is not the entire document itself. The two overloaded cases apply when t is either a fraction or a right subscript.

Assuming that your cursor is inside a fraction inside a subscript, calling special will swap the numerator and the denominator. On the other hand, if your cursor is inside a subscript inside a fraction, then calling special will change the subscript into a superscript.

When the conditions of several (re)declarations are met, then the last redeclaration will be used. Inside a redeclaration, one may also use the former keyword in order to explicitly access the former value of the redefined symbol.

(:mode mode)
(mode-based overloading)

This option is similar to (:require (mode)) and specifies that the definition is only valid when we are in a given mode. Here mode is a mode predicate such as in-math? or in-prog-scheme?. Contrary to conditions specified using :require, mode conditions do not depend on the arguments of the function. As with :require, the most recent definition whose conditions hold is used: definitions for more specific modes do not take precedence over definitions for more general modes, so that a general definition made later overrides them. New modes are defined using texmacs-modes and modes can inherit from other modes; inheritance only affects whether the mode predicate holds.

(texmacs-modes . modedefs)
(define new texmacs modes)

Use this macro to define new modes that you can use for contextual overloading, for instance in kbd-map. Modes may be made dependent on other modes. This macro takes a variable number of definitions as arguments, each of the form (mode-name conditions . dependencies). End your mode-name and any dependencies with one %; the macro then defines a predicate whose name is obtained by replacing % by ? (e.g. in-verbatim? below). The conditions may be #t for modes which only depend on other modes. For instance:

(texmacs-modes
  (in-verbatim% (inside? 'verbatim) in-text%)
  (in-tt% (inside? 'tt)))

When creating new modes remember to place first the faster checks (against booleans, etc.) for speed.

Other options for function and macro declarations

Besides the contextual overloading options, the tm-define and tm-define-macro primitives admit several other options for attaching additional information to the function or macro. We will now describe these options and explain how the additional information attached to functions can be exploited.

Warning 1. A current limitation of the implementation is that functions overloaded using :require and :mode cannot have different options. This means in particular that you cannot specify different values for :synopsis depending on the context.

(:synopsis short-help)
(short description)

This option gives a short description of the function or macro, in the form of a string short-help. As a convention, Scheme expressions may be encoded inside this string by using the @-prefix. For instance:

(tm-define (list-square l)
  (:synopsis "Appends the list @l to itself")
  (append l l))

The synopsis of a function is used for instance in order to provide a short help string for the function; it is also the help balloon of the menu items which call the function, when they have no :balloon.

(:argument var description)

(:argument var type description)
(argument description)

This option gives a short description of one of the arguments var to the function or macro. Such a description is used for instance for the prompts, when calling the function interactively. For these uses, the second format allows for the specification of a type (a string or a symbol) which changes how the widgets/prompts work. The default type is "string". Types ending with file (such as smart-file) and the type "directory" allow the user to choose a file or directory, and tab completion in the interactive prompt will traverse the file system. For instance, load-buffer in

texmacs/texmacs/tm-files.scm

is declared using (:argument name smart-file "File name").

(:default var body*)

(:proposals var body*)
(default values and proposals for arguments)

When calling the function interactively, the argument var is proposed with the default value obtained by evaluating body, resp. with the list of proposals obtained by evaluating body.

(:interactive #t)
(interactive functions)

Indicates that the function is interactive, i.e. that it may prompt the user for further input. In menus, the names of the corresponding entries are followed by dots.

(:check-mark text pred?)

(:balloon fun)
(menu decorations)

The :check-mark option specifies a check mark text (such as "*" or "v") and a predicate pred? which determines whether menu entries calling the function should be marked; the predicate is applied to the same arguments as the function. The :balloon option specifies a function which is applied to the same arguments and which should return the text of the help balloon for menu entries calling the function. By default, the synopsis is used as the help balloon.

(:secure #t)
(secure functions)

Declares the function to be secure, so that it can be called from untrusted documents (for instance through action tags).

(:returns description)
(return value description)

This option gives a short description of the return value of the function or macro.

(:type (-> from to))
(type conversion description)

This option specifies that a function or macro performs a conversion from the data type from to the data type to.

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