GNU TeXmacsUser manualReferenceAboutEnglish
Inserting, deleting and making structure

1.Shape of the edit tree

The structured operations rely on a few invariants of the edit tree, which they both assume and restore:

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.

2.Inserting

insert_tree (t, p_in_t).
This is the workhorse of all insertions (cpp-insert-go-to, insert-raw-go-to, and via insert in
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:

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.

Paragraph breaks.
insert_return (insert-raw-return) splits the current paragraph at the cursor. Whether a paragraph break is allowed at all is decided by accepts_return: directly inside a document, in the last argument of with, locus, style-with, canvas, macro bodies, floats and extensions (provided the cursor is on a “pure line”, see pure_line), and in a few other places. If needed, a document node is inserted around the current line first. insert_return returns true on failure. remove_return (p) joins the paragraph p with the next one.

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.

Spaces and images.
make_space, make_hspace, make_vspace_before, make_vspace_after and make_htab insert the corresponding primitives. When the cursor is right after a space of the same kind and with the same arity, the one argument versions add the new width to the existing one instead of inserting a second space, so that pressing a spacing shortcut repeatedly widens a single space. make_image (file, link, w, h, x, y) inserts an image, either as a link to the file or with the file contents embedded as raw-data.

3.Making compound structures

make_compound (l, n).
This is the generic constructor behind make (cpp-make, cpp-make-arity), in
edit_dynamic.cpp

. Given a tag and an optional arity it

  1. determines the smallest correct arity from the DRD if none is given, and the first accessible child, where the cursor will go;

  2. for with-like tags, gives the Scheme function with-like-check-insert a chance to handle the insertion;

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

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

  5. for block macros and for footnotes and folded comments, starts the first argument with an empty document;

  6. 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 ↩;

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

Activation and arguments.
activate (activate) removes the innermost inactive around the cursor; an inactive compound with a literal name is turned into the tag of that name. Scheme is notified with notify-activated. insert_argument (forward) and remove_argument (forward) (insert-argument, remove-argument, and the variants with a path) add or remove arguments of the innermost dynamic tag (one whose arity may vary, as told by drd->is_dynamic), at a position allowed by drd->insert_point and with an arity allowed by drd->correct_arity; in source mode, arguments can be inserted anywhere. These are the operations behind the generic structured-insert-horizontal and structured-remove-horizontal for tags without a more specific overload.
with and style-with.
make_with (var, val) wraps the selection in a with, first removing older changes of the same variable inside the selection (remove_changes_in), or inserts an empty with. insert_with (p, var, val) and remove_with (p, var) (path-insert-with, path-remove-with) add, change or remove a variable of the with at p or of an enclosing one. make_style_with and make_mod_active play the same role for style-with and for active, inactive and their variants. On the Scheme side, make-with sets a cell format instead when a table selection is active.
Hybrid commands.
Typing a backslash inserts a hybrid tag (
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.

Mathematics.
The constructors of
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.

4.Deleting

4.1.The deletion point

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.

4.2.Dispatch

remove_text_sub then distinguishes the following cases.

Paragraph boundaries

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.

Text

Inside a string, one character is removed (taking TeXmacs symbols like <alpha> as single characters).

Entering a tag

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.

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

4.3.Removing structure

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.

5.Pitfalls

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