![]() |
TeXmacs abbreviations | ![]() |
The TeXmacs Scheme kernel defines a few abbreviations and control structures which are used throughout the Scheme code of TeXmacs. Most of them are defined in
kernel/boot/abbrevs.scm
,
kernel/boot/srfi.scm
and
kernel/boot/ahash-table.scm
(relative to
src/TeXmacs/progs/
). Since they are loaded at boot time, they are available in all modules.
(== x y)
== is an abbreviation for equal? and != for its negation.
Negations of the corresponding standard predicates.
Test whether x is a list of length \(1\), \(2\), \(3\), \(4\), \(> 0\) resp. \(> 1\). The negations are nlist-1?, etc.
(in? x l)
Test whether x is (not) a member of the list l (using equal?). The routine (cons-new x l) adds x in front of l unless it is already a member.
(when cond body …)
Execute body if cond holds resp. does not hold.
Abbreviation for (let ((var val)) body …). If var is a list of variables, then val should evaluate to a list whose elements are bound to these variables, as in
(with (x y) (list 1 2) (+ x y)) |
Bind var to val and evaluate body only if val is not #f; otherwise return #f.
Each clause is either of the form (var expr), (expr) or a variable. The clauses are evaluated in sequence and #f is returned as soon as one of them evaluates to #f; otherwise, body is evaluated with the bindings made by the clauses. For instance:
(and-let* ((t (tree-innermost 'frac))
(num (tree-ref t 0))
((tree-atomic? num)))
(tree->string num)) |
Bind the multiple values returned by expr to vars and evaluate body. The kernel also provides the SRFI constructs case-lambda, cut and cute.
Temporarily set the (global) variable var to val while evaluating body, and restore the old value afterwards. The value of the last expression of body is returned.
Evaluate result, then body, and return the value of result.
(for (x l) body …)
(for (i start end) body …)
The first form evaluates body for each element x of the list l. The other forms evaluate body for \(\text{\tt{\text{{\ttfamily{i}}}}} = \text{\tt{\text{{\ttfamily{start}}}}}, \text{\tt{\text{{\ttfamily{start}}}}} + \text{\tt{\text{{\ttfamily{step}}}}}, \ldots\), as long as \(\text{\tt{\text{{\ttfamily{i}}}}} < \text{\tt{\text{{\ttfamily{end}}}}}\) (or \(\text{\tt{\text{{\ttfamily{i}}}}} > \text{\tt{\text{{\ttfamily{end}}}}}\) if step is negative); the default step is \(1\). The related macros (repeat n body …) and (twice body …) evaluate body n times resp. twice.
(.. start end)
Return the list of integers from start to end, with end excluded resp. included. An optional third argument specifies the step.
Abbreviation for (set! var (not var)).
TeXmacs uses its own names for hash tables based on equal?, so as to be independent of the underlying Scheme implementation:
(make-ahash-table)
(ahash-ref h key)
(ahash-set! h key val)
Create a new table, look up a key (#f is returned for missing keys), set resp. remove an entry. Further routines are ahash-size, ahash-fold, (ahash-ref* h key default), ahash-table->list, list->ahash-table, ahash-table-map, ahash-table-invert, ahash-table-append and ahash-table-difference.
Define a public hash table name (if not yet defined) and add the given entries to it: each key is associated to the list (val …). The entries are quasi-quoted.
Among the many small utilities on strings and lists defined in
kernel/library/base.scm
and
kernel/library/list.scm
, let us mention string-starts?, string-ends?, string-contains?, string-tail, string-split-lines, string-decompose, list-filter, list-find, list-remove-duplicates. The C++ glue provides further string routines, such as string-search-forwards, string-search-backwards and string-replace.
(eval-system cmd)
Run the shell command cmd and return its standard output as a string. The variant var-eval-system removes trailing newlines.
Run the program whose name and arguments are given by the list of strings args, without passing through a shell. The strings in the list in are written to the file descriptors in the list fd-in, and the output on the file descriptors in the list fd-out is collected. The result is a list whose first element is the exit code (as a string), followed by the collected outputs. For instance:
(evaluate-system (list "sort") '(0) (list "b\na\n") '(1 2)) |
returns ("0" "a\nb\n" "").
Start the shell command cmd in the background and call call-back with its output when it has finished. Returns #t on error.