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 expr pattern)
(select subexpressions following a pattern)

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:

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 in a specified range.

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

:#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?.

(: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")

(: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.

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.

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