GNU TeXmacsUser manualReferenceAboutEnglish
Selection of subexpressions

Besides pattern matching on trees, TeXmacs provides the routine select for pattern matching along paths. Given a tree, this mechanism typically allows the user to select all subtrees which are reached following a path which meets specific criteria. For instance, one might to select the second child of the last child or all square roots inside numerators of fractions. The syntax of the selection patterns is also used for high level tree accessors. The implementation can be found in

kernel/regexp/regexp-select.scm

.

(select expr pattern)
(select subexpressions following a pattern)

Select all subtrees inside a hybrid tree expr according to a specific path pattern. The result is a list of subtrees.

(tm-ref expr pattern-1 … pattern-n)
(first selected subexpression)

Return the first subexpression selected by the path pattern (pattern-1 … pattern-n), or #f if no subexpression matches. For instance, (tm-ref t 1 0) returns the first child of the second child of t.

Patterns are lists of atomic patterns of one of the following forms:

0, 1, 2, …
(select a specific child)

Given an integer n, select the n-th child of the input tree. For instance, (select '(frac "1" "2") '(0)) returns ("1").

:first, :last
(select first or last child)

Select first or last child of the input tree.

(:range start end)
(select children in a range)

Select all children with indices \(i\) such that \(\text{\tt{\text{{\ttfamily{start}}}}} \leqslant i < \text{\tt{\text{{\ttfamily{end}}}}}\).

label
(select children with a given label)

Select all compound subtrees with the specified label. Example:

Scheme]

(select '(document (strong "x") (math "a+b") (strong "y")) '(strong))

((strong "x") (strong "y"))

(:exclude label-1 … label-n)
(select children with other labels)

Select all compound children whose label is none of label-1 until label-n.

:%1, :%2, :%3, …
(select descendants of a given generation)

The pattern :%n, where n is a number, selects all descendants of the n-th generation. Example:

Scheme]

(select '(foo (bar "x" "y") (slash (dot))) '(:%2))

("x" "y" (dot))

:*
(select all descendants)

This pattern selects all descendants of the tree. For instance, (select t '(:* frac 0 :* sqrt)) selects all square roots inside numerators of fractions inside t.

(:match pattern)
(matching)

This pattern matches the input tree if and only the input tree matches the specified pattern according to match?. Example:

Scheme]

(select '(foo "x" (bar)) '(:%1 (:match :string?)))

("x")

Any Scheme predicate can be used in this way (see the description of :pred? in the section on matching regular expressions). Notice that the predicate is applied to the subexpression as it is: for instance, :atomic-tree? does not hold for the Scheme string "x", so that (select '(foo "x" (bar)) '(:* (:match :atomic-tree?))) returns (). A predicate which only accepts trees, such as the glue routine tree-atomic?, raises an error here, since :* also applies it to the lists.

(:replace expr)
(substitution)

Select expr, in which the variables bound by the previous patterns are replaced by their values. For instance,

(select '(foo "x" (bar "y")) '(:* (:match (bar 'a)) (:replace (baz 'a))))

returns ((baz "y")).

'var
(variables)

Select the input tree itself, while binding it to the variable var. As in the case of match?, the same variable may occur several times in a pattern, in which case the corresponding subexpressions must coincide.

(:or pattern-1 … pattern-n)

(:and pattern-1 … pattern-n)
(boolean expressions)

These rules allow for the selection of all subtrees which satisfy one among or all patterns pattern-1 until pattern-n.

(:and-not pattern pattern-1 … pattern-n)
(difference)

Select all subtrees which are selected by pattern, but by none of the patterns pattern-1 until pattern-n.

(:group pattern-1 … pattern-n)
(grouping)

Group the path pattern (pattern-1 … pattern-n) into a single atomic pattern, which is useful inside :or and :and.

In the case when the input tree is active, the function select supports some additional patterns which allow the user to navigate inside the tree.

:up
(parent)

This pattern selects the parent of the input tree, if it exists.

:down
(child containing the cursor)

If the cursor is inside some child of the input tree, then this pattern will select this child.

:next
(next child)

If the input tree is the \(i\)-th child of its parent, then this pattern will select the \((i + 1)\)-th child.

:previous
(previous child)

If the input tree is the \(i\)-th child of its parent, then this pattern will select the \((i - 1)\)-th child.

The patterns :first and :last also work for active trees, in which case they select the first resp. last child of the input tree, and the pattern :same selects the input tree itself.

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