![]() |
High level modification routines | ![]() |
The routine tree-set and the corresponding macro tree-set! can be used as a higher level interface to the fundamental routines for modifying trees as described in the previous section. However, it is still up to the user to verify that the resulting edit tree is still correct.
(tree-set which accessors* new-value)
This routine replaces the tree (tree-ref which accessors*) by a new content value new-value. Besides the fact that the routine tree-set supports additional accessors for which (see the description of tree-ref below), tree-set differs from tree-assign in this respect that tree-set tries to cleverly decompose the assignment into fundamental modification routines. The objective of this decomposition is to make a less intrusive modifications in the document, so as to preserve as many tree positions and cursor positions as possible.
For instance, the operation (tree-set t t) is a no-operation for all trees t. A more complex operations like
(tree-set! t ‘(foo "Hop" ,(tree-ref t 2))) |
is decomposed into the following fundamental modifications:
(tree-remove-node! t 2)
(tree-insert-node! t 1 '(foo "Hop")) |
Like in the case tree-assign and tree-assign!, you should use the macro tree-set! in order to update the value of which if which is a Scheme variable and accessors is the empty list. These routines are defined in
progs/utils/library/tree.scm
.
In its simplest form, this routine allows for the quick access of a subtree of which via a list of integers accessors. For instance, if which contains the tree (frac "a" (sqrt "b")), then (tree-ref which 1 0) returns the tree "b".
In its general form, tree-ref relies on the routine select in order to compute the desired subtree. With which as in the above example, this makes it possible to retrieve the subtree (sqrt "b") using (tree-ref which 'sqrt) (a symbol selects the first child with the given label). In the case when there are several matches, the first match is returned. For instance, if which contains the tree (frac (sqrt "a") (sqrt "b")), then (tree-ref which 'sqrt) returns (sqrt "a"). The accessors :first and :last select the first and the last child. If no subtree matches, tree-ref returns #f.
In fact, the result of tree-ref is not necessarily a subtree of which: the select utility also accepts the accessors :up, :down, :next, :previous, etc. for navigating inside the edit tree starting with which. For instance, (tree-ref (cursor-tree) :up) returns the parent of the cursor tree. For more details, we refer to the documentation of select.
Besides the above routine for the direct modification of a subtree of the document, TeXmacs also provides several routines for inserting content at the current cursor position.
(insert what)
Insert the content what at the current cursor position. In the second form, the cursor is placed inside the inserted content at the position given by the accessors (integers, :first, :last, :start or :end), as in (insert '(frac "" "") :first :start). TeXmacs does some additional checking whether it is allowed to perform the insertion. For instance, it is disallowed to insert multi-paragraph content inside a mathematical formula. Whenever the user attempts to make an invalid insertion, then insert is equivalent to a no-operation.
(make lab)
This routine may be used to insert a valid tag with label lab. As many empty arguments as necessary (or arity arguments) are inserted in order to make the tag valid. Similarly, if lab is a multi-paragraph tag, then the necessary operations are performed to put the tag in a separate paragraph. When a selection is active, make usually wraps the tag around the selection. The routine is overloaded in many places, like any other tm-defined function.
Insert what and put the cursor at the position path relative to the inserted content, as in (insert-go-to "hello()" '(6)).
Insert a with tag which sets the environment variable var to val, around the selection if there is one.
(insert-return)
(make-return-before)
Start a new paragraph (the default action of the return key), resp. insert a new paragraph before or after the current one. The routine insert-return is defined in
generic/generic-edit.scm
on top of the glued insert-raw-return, so that it can be overloaded in particular modes.
(path-insert-with p var val)
Set the environment variable var to val for the subtree at the path p: if this subtree, or its parent, is a with tag, the variable is added to it or changed in it; otherwise the subtree is wrapped in a new with tag. The second routine removes var from such a with tag, and removes the tag when it was its only variable.
These routines are mostly defined in
progs/utils/library/cpp-wrap.scm
as wrappers of C++ routines, or glued directly from C++ (see
src/Scheme/Glue/build-glue-editor.scm
).