![]() |
Inserting, deleting and making structure | ![]() |
The structured operations rely on a few invariants of the edit tree, which they both assume and restore:
A block of paragraphs is a document node whose children are paragraphs; the body of a buffer is such a node.
A paragraph which contains several pieces is a concat node. A concat never has zero or one child, never contains an empty string, never contains two adjacent strings, never contains a nested concat, and never contains a multi-paragraph object when it is the child of a document (such objects are moved into a paragraph of their own).
The cursor is a path whose last item is a position: a character offset inside a string, or 0 (before) and 1 (after) for a compound tree.
The routine which re-establishes the concat invariants is edit_text_rep::correct_concat (p, done) (
Edit/Modify/edit_text.cpp
). Starting from the child done, it removes empty strings, joins adjacent strings, flattens nested concat nodes, splits off multi-paragraph children into separate paragraphs, and replaces a concat of arity 0 or 1 by the empty string or its only child. Since every step is an elementary modification, positions and the cursor survive. correct (p) calls it on a concat, and moves up when the tree at p became the empty string. Most operations end with a call to correct or correct_concat on the parent of what they changed.
utils/library/cpp-wrap.scm
). It inserts the tree t at the cursor and puts the cursor at the position p_in_t inside the inserted copy. Unless the look and feel is Emacs, it first deletes the selection (selection_cut ("none")). Then:
a string inserted inside a string is inserted directly;
a document (several paragraphs) is inserted by breaking the current paragraph with insert_return, inserting the paragraphs between the two halves and joining the first and last inserted paragraphs with their neighbours (remove_return);
a multi-paragraph object which is not a document (a theorem, a list, …) is put in a new paragraph after the current one (make_return_after);
everything else is inserted inline: prepare_for_insert makes sure that the cursor is inside a concat, splitting the current string if needed, the tree is inserted as children of that concat, and correct_concat normalizes the result.
The one argument version insert_tree (t) (cpp-insert) normalizes t with simplify_correct and puts the cursor at its end. var_insert_tree (cpp-insert-go-to) wraps the selection: it cuts the selection to the primary clipboard, inserts the tree and pastes the selection back at the new cursor position.
The two routines make_return_before and make_return_after of
edit_dynamic.cpp
(make-return-before, make-return-after) move the cursor to the start or the end of the enclosing paragraph and insert a paragraph break there; they are used to put block content in a paragraph of its own.
edit_dynamic.cpp
. Given a tag and an optional arity it
determines the smallest correct arity from the DRD if none is given, and the first accessible child, where the cursor will go;
for with-like tags, gives the Scheme function with-like-check-insert a chance to handle the insertion;
inspects the macro definition of the tag in the current environment, to know whether it is a block macro (its body is a multi-paragraph construct), a “large” macro, or a table macro (its body contains a tformat whose last argument is a macro argument);
takes the current selection as the content of the new tag when it is small (or, for large macros, when it is any non table selection);
for block macros and for footnotes and folded comments, starts the first argument with an empty document;
wraps the tag in inactive if some children are not accessible (except in documents whose initial mode is src and in the preamble), so that the user can fill in all arguments and then activate it with ↩;
inserts the result with insert_tree, creates a 1 by 1 table with make_table for table macros, and inserts the cut selection.
It also sets a footer message which explains how to insert arguments or activate the tag. On the Scheme side, make has several overloads in
utils/library/cpp-wrap.scm
: with-like tags and inline tags wrap the selection themselves, and tags listed by make-wrapped-tag-list cut the selection to a temporary clipboard and paste it inside.
generic/generic-kbd.scm
) (make_hybrid, cpp-make-hybrid) in which the user types a name. activate_hybrid (activate-hybrid) then decides what it means: a LaTeX command known to the keyboard tables (activate_latex, which looks the name up with kbd_get_command and runs the command), an argument of the enclosing macro definition (inserted as arg), a primitive or macro (inserted with make_compound), or an environment variable (inserted as value). activate_symbol does the same for symbol tags.
edit_math.cpp
(make_fraction, make_sqrt, make_var_sqrt, make_script, make_lprime, make_rprime, make_below, make_above, make_wide, make_wide_under, make_neg, make_rigid, make_tree) all follow the same pattern: if a small selection is active, it becomes the first argument (and the cursor goes to the second one); otherwise an empty construct is inserted with the cursor in its first argument, and a footer message tells how to proceed. Primes are accumulated: typing a prime right after a prime extends the existing rprime. make_script does nothing in an empty script and moves to the end of an existing script of the same kind. The Scheme wrappers are in
utils/library/cpp-wrap.scm
and are called by the math keyboard and menus (
math/math-kbd.scm
,
math/math-menu.scm
); the semantic math editor in
math/math-sem-edit.scm
overrides some of them.
remove_text (forward) (remove-text) is the default action of backspace and delete. It is implemented in
edit_delete.cpp
as remove_text_sub, followed by empty_document_fix, which inserts an empty paragraph if the document no longer contains any accessible position.
get_deletion_point (p, last, rix, t, u, forward) first determines what is to be deleted. For a forward deletion at the end of a piece of a concat, it moves to the start of the next piece. Then it climbs out of formatting nodes (concat, document, …) as long as the cursor is at their border, so that t is the innermost tree in which the deletion takes place, last the position of the cursor in t, rix its rightmost position, and u the parent of t.
remove_text_sub then distinguishes the following cases.
When t is a document and the cursor is between two paragraphs, the paragraphs are joined (remove_return), except when one of them is a multi-paragraph or sectional object, in which case an empty paragraph is removed or the cursor moves into the neighbour. At the border of the whole document the parent decides: an empty footnote, float or folded comment disappears, an empty with or extension is removed, a non empty extension is skipped, and tables and graphical text get their own treatment.
Inside a string, one character is removed (taking TeXmacs symbols like <alpha> as single characters).
When the cursor is just before (forward) or after (backward) a compound tree, the tag decides: monolithic objects (spaces, tabs, raw-data, big operators, left, mid, right, and value or arg with a single argument) are deleted at once (back_monolithic); for around, the bracket is replaced by an invisible one (back_around); for primes, one prime is removed (back_prime); for wide, with and locus the cursor just enters the tag; for tables, back_table moves into the first or last cell; everything else goes to back_general, which in source mode turns an extension into an editable compound and otherwise moves into the nearest argument.
When the cursor is at the start (backward) or end (forward) of an argument, the parent u decides, through back_in_around, back_in_long_arrow, back_in_wide, back_in_tree, back_in_table, back_in_with and back_in_general. The common idea is that deleting in an empty argument removes that argument if the arity allows it (remove_empty_argument), and removes the whole tag when all its arguments are empty; otherwise the cursor moves to the neighbouring argument or out of the tag.
This is why, in TeXmacs, backspace never destroys a structure with content in one keystroke: it first moves into the structure, and only deletes it once it has been emptied. The Scheme layer refines this per context: kbd-remove is overloaded for sessions, programs, folding environments, databases, literate programs and the semantic math editor, and only the generic fallback calls remove-text (or cuts the selection when one is active).
remove_structure_upwards (remove-structure-upwards, the Delete entry of the focus tag menu in
generic/generic-menu.scm
, and the action of several kbd-remove overloads) removes the innermost non formatting tag around the cursor while keeping the argument that contains the cursor: the other arguments are deleted, the tag node is removed with remove_node, and the result is spliced into the surrounding document if both are paragraph blocks. For tables, cells and similar constructs which have no border of their own, it recurses so that the whole table disappears. remove_structure (forward) (remove-structure) removes a word or the neighbouring structure; it is exported but not used by the standard Scheme code.
insert_return and make_return_after return true on failure; make_return_before returns the result of insert_return.
Most operations act at the cursor tp and use the environment at the cursor (get_env_value), which is only valid after typesetting. edit_dynamic_rep::in_source even caches the mode in a static variable while an argument is being removed (env_locked), because the environment cannot be recomputed in the middle of a modification.
Merging spaces ignores the unit of the new space. In edit_text_rep::make_space (tree u) (
Edit/Modify/edit_text.cpp:293
) the unit of each argument of the existing space is compared with the unit of its own first argument (get_unit (t[0])) instead of with the unit of the new space u[i], so the test always succeeds for spaces of the same sign. Inserting a horizontal space of 1cm right after one of 0.5em therefore produces a single space of 1.5em.
remove_structure reads an uninitialized variable in the backward direction: the inner declaration int pos= max (start-1, 0) (
Edit/Modify/edit_delete.cpp:329
) shadows the outer pos, and the following end= pos uses the outer, never assigned one. The routine is only reachable through the glue (remove-structure).
make_return_before compares the number of paragraphs with q->item+1 (
Edit/Modify/edit_dynamic.cpp:646
), that is, with the first item of the path (the index of the buffer in the global tree) instead of the index of the paragraph (last_item (q)). The test whether the cursor is in the last paragraph is therefore wrong in general.
The Scheme wrapper (make-script r? sup?) (
utils/library/cpp-wrap.scm:93
) names its parameters in the wrong order: they are passed unchanged to make_script (sup, right), and all callers indeed use the order superscript?, right?.