GNU TeXmacsUser manualReferenceAboutEnglish
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)

(path-end t p)
(start and end of a subtree)

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)

(path-previous t p)
(next and previous valid positions)

Return the next resp. previous valid cursor position, as with the arrow keys in text mode.

(path-next-word t p)

(path-previous-word t p)
(word based motion)

Move to the end of the next word resp. the start of the previous word.

(path-next-node t p)

(path-previous-node t p)
(node based motion)

Move to the next resp. previous position which is not inside the same leaf of the document tree.

(path-next-tag t p labels)

(path-previous-tag t p labels)
(move to the next tag of a given kind)

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)

(path-previous-argument t p)
(move between arguments)

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.

(path-previous-section t p)
(move to the previous section)

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.

Moving the cursor

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

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

Trees and the cursor

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)

(inside-which labels)
(search upwards)

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)

(tree-end t . l)
(leaves at the extremities)

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)

(tree-select t . l)
(cursor and selection)

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.

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