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.
Select all subtrees inside a hybrid tree expr according to a specific path pattern.
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 in a specified range.
Select all compound subtrees with the specified label. Example:
scheme] |
(select '(document (strong "x") (math
"a+b") (strong "y")) '(strong))
|
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))
|
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?.
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?)))
|
(: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.
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.