![]() |
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 all subtrees inside a hybrid tree expr according to a specific path pattern. The result is a list of subtrees.
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:
Given an integer n, select the n-th child of the input tree. For instance, (select '(frac "1" "2") '(0)) returns ("1").
Select first or last child of the input tree.
Select all children with indices \(i\) such that \(\text{\tt{\text{{\ttfamily{start}}}}} \leqslant i < \text{\tt{\text{{\ttfamily{end}}}}}\).
Select all compound subtrees with the specified label. Example:
(select '(document (strong "x") (math
"a+b") (strong "y")) '(strong))
Scheme]
Select all compound children whose label is none of label-1 until label-n.
The pattern :%n, where n is a number, selects all descendants of the n-th generation. Example:
(select '(foo (bar "x" "y") (slash
(dot))) '(:%2))
Scheme]
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.
This pattern matches the input tree if and only the input tree matches the specified pattern according to match?. Example:
(select '(foo "x" (bar)) '(:%1 (:match :string?)))
Scheme]
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.
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")).
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)
These rules allow for the selection of all subtrees which satisfy one among or all patterns pattern-1 until pattern-n.
Select all subtrees which are selected by pattern, but by none of the patterns pattern-1 until pattern-n.
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.
This pattern selects the parent of the input tree, if it exists.
If the cursor is inside some child of the input tree, then this pattern will select this child.
If the input tree is the \(i\)-th child of its parent, then this pattern will select the \((i + 1)\)-th 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.