![]() |
Path-based navigation | ![]() |
As explained in the section on the TeXmacs editing model, cursor positions are represented by paths, i.e. lists of integers. The kernel provides a set of routines which compute new cursor paths from old ones, following the logical structure of the document. These routines are implemented in C++ (in
src/Data/Tree/tree_cursor.cpp
and
src/Data/Tree/tree_traverse.cpp
) and glued to Scheme (see
src/Scheme/Glue/build-glue-basic.scm
). They do not move the cursor themselves; this is done by (go-to p), and the current cursor path is returned by (cursor-path).
All routines below take a tree t (usually (root-tree)) and a cursor path p inside t, and return a new cursor path inside t. When no suitable position exists, p itself is usually returned. The exceptions, which take the path of a subtree instead of a cursor path, are indicated below.
(path-start t p)
Here p is the path of a subtree of t (not a cursor path). Return the first resp. last valid cursor position inside this subtree. For instance, (path-start (root-tree) (buffer-path)) is the start of the current buffer.
(path-next t p)
Return the next resp. previous valid cursor position, as with the arrow keys in text mode.
(path-next-word t p)
Move to the end of the next word resp. the start of the previous word.
(path-next-node t p)
Move to the next resp. previous position which is not inside the same leaf of the document tree.
(path-next-tag t p labels)
Move to the next resp. previous occurrence of a tag whose label is labels (a symbol) or belongs to labels (a list of symbols). The variants path-next-tag-same-argument and path-previous-tag-same-argument try to preserve the argument of the tag in which the cursor is.
(path-next-argument t p)
Here p is the path of an argument of a tag (a subtree path (q … i), not a cursor path). Return the cursor path at the start of the next resp. the end of the previous accessible argument of this tag, or the empty path if there is no such argument. In particular, given a cursor path inside a string, the result is the empty path: use (cDr p) to obtain the path of the argument which contains the cursor.
Return the path of the previous sectional tag (like section) before p (a subtree path, not a cursor position), or p itself if there is none.
Paths can be compared with path-less?, path-less-eq?, path-inf? and path-inf-eq?, and (path-exists? p) tests whether p is the path of an existing subtree of the root tree.
The module
progs/utils/library/cursor.scm
(to be included with (:use (utils library cursor))) defines corresponding commands which move the cursor inside the current buffer: go-to-next, go-to-previous, go-to-next-word, go-to-previous-word, go-to-next-node, go-to-previous-node, (go-to-next-tag labels), (go-to-previous-tag labels), etc. They are all built on top of
(tm-define (go-to-same-buffer fun)
(with p (fun (root-tree) (cursor-path))
(when (list-starts? (cDr p) (buffer-path))
(go-to p)
(select-from-cursor)))) |
which applies a path-based navigation routine and moves the cursor provided that the new position lies in the same buffer. The same module also provides more general commands, whose argument fun is a cursor command without arguments, such as go-to-next (not a path- routine), which is called as (fun):
(go-to-next-such-that fun pred?) repeatedly calls fun until the tree at the cursor satisfies pred?, or the cursor no longer moves (it then goes back to the initial position);
(go-to-next-inside fun . pattern) repeatedly calls fun until the cursor is inside a tree which matches pattern, a list of tag names, predicates and argument numbers;
(go-to-remain-inside fun . pattern) calls fun once, and goes back if the cursor left the innermost tree which matches pattern.
Finally, (go-to-next-tag-argument labels i) moves the cursor to the start of the argument i of the next tag with one of the labels.
For moving the cursor relative to a given tree, it is often more convenient to use tree-go-to, as in (tree-go-to t 0 :start), or to compute paths with (tree->path t accessors).
The module
utils/library/tree.scm
provides the following routines, where the optional arguments l are accessors as for tree-ref, possibly followed by :start, :end or an integer position, as for tree->path:
(tree-search-upwards t what)
The first routine returns the first ancestor of t, starting with t itself, which matches what (a tag name, a list of tag names or a predicate), or #f; the search stops at the buffer. The second one returns the label of the innermost tree around the cursor whose label is in the list labels, or #f.
(tree-start t . l)
Return the tree which contains the first resp. the last cursor position of the subtree of t given by l.
(tree-cursor-at? t . l)
Test whether the cursor is at the position given by l inside t (for instance (tree-cursor-at? t 0 :end)), resp. select the subtree of t given by l.