![]() |
The generic structured editing hooks | ![]() |
Most editing commands act on the focus tree, returned by (focus-tree) (
kernel/library/tree.scm
), which is the tree at the path (get-focus-path) computed by the editor (edit_select_rep::focus_get in
Edit/Replace/edit_select.cpp
):
if a focus has been set explicitly (for instance by clicking on a tag in the focus bar), that tree;
otherwise, if there is a selection, the tree around the selection;
otherwise, the innermost tag around the cursor.
In the last two cases the editor skips the purely structural tags document, concat, tformat, table, row, cell, shown, hidden and a few others, so that inside a table the focus is the table environment (such as tabular) and inside a list it is the list environment.
Generic commands are written as functions of the focus tree with a default implementation which recurses to the parent, and a base case for the buffer itself:
(tm-define (kbd-enter t shift?)
(and-with p (tree-outer t)
(kbd-enter p shift?)))
(tm-define (kbd-enter t shift?)
(:require (tree-is-buffer? t))
(insert-return))
(tm-define (kbd-return)
(kbd-enter (focus-tree) #f)) |
(
generic/generic-edit.scm
; tree-outer returns the parent except for the buffer tree). A mode handles a tag by a conditional redefinition for that tag; the first enclosing tag which has a specific definition handles the command. This pattern, introduced in the TeXmacs editing model, is used for all the hooks below.
The keys of
generic/generic-kbd.scm
call parameterless commands, which call the following hooks on the focus tree:
↩ and ⇧↩ (with kbd-control-enter and kbd-alternate-enter for the modified variants). The default inserts a paragraph break.
The space bar.
⌫ and ⌦; the default removes text, or the selection.
⇥ and ⇧⇥ (tab completion; for labels and citations, completion of the key). kbd-alternate-variant is used for ⌥⇥ and by default inserts a horizontal tab.
Cursor movements with the arrow keys, ↖/↘ and ⇞/⇟; sessions redefine them to move between input fields.
In addition, the unconditional entry points kbd-insert (insertion of a character or shorthand), kbd-backspace and kbd-delete may be redefined under a mode, as the semantic math mode does.
These hooks implement the ⌃…, ⌘⌃… and ⌥… shortcuts and the corresponding icons of the focus bar:
Insert or remove a column, a row, an argument, a branch, … next to the current one. Tables insert and remove columns and rows; switches and overlays insert and remove branches; a subscript and a superscript are completed into a script pair.
Move to the next or previous argument, cell, branch, …
Traversal of the document by structure (for instance from one session input field to the next).
Cycle through the variants of a tag (⌥⇥); the default uses the variant-tag groups (
utils/edit/variants.scm
).
Fold or unfold, toggle between two alternative forms (⌃*); the default uses the pairs declared with define-alternate.
Toggle numbering (⌃#); the default toggles the final * of the tag name.
Change the position or size of the focus (spaces, brackets, table extents, animations; see
generic/format-geometry-edit.scm
).
Gestures.
The predicates structured-horizontal? and structured-vertical? tell the generic code whether the horizontal and vertical commands make sense for a tree; by default the first holds for dynamic tags and tables, the second for tree and tables.
The Focus menu and the focus icon bar are built by standard-focus-menu and standard-focus-icons in
generic/generic-menu.scm
from a number of sub-menus, each of which takes the focus tree as argument and may be redefined for a tag:
Entries for enclosing tags (for instance the document title around an author).
The tag itself: its name, its variants (focus-variant-menu), its toggles (focus-toggle-menu), floats and animations, its preferences and rendering parameters, help and deletion.
Structured movements and insertions; shown only if focus-can-move? resp. focus-can-insert-remove? hold.
Hidden arguments, mode specific extra entries, the label of the tag.
The parallel *-icons menus build the icon bar. A mode may also replace standard-focus-menu for some trees altogether, as
table/table-menu.scm
does for tables.
Several predicates and data functions feed these menus and are redefined per tag: focus-has-variants?, focus-has-toggles?, focus-can-move?, focus-can-insert?, focus-can-remove?, focus-has-geometry?, focus-has-parameters?; and for the style parameters (standard-parameters tag-name) (the environment variables which customize a tag), (customizable-parameters t) (parameters stored in a with around a particular tree) and (parameter-choice-list var) (proposed values for a variable).
To give a new tag my-env its own behaviour, a module typically
adds the tag to suitable groups ((define-group variant-tag …), (define-alternate my-env my-env*), …), so that the generic implementations of variant-circulate and alternate-toggle apply;
defines a context predicate, (tm-define (my-env-context? t) (tree-is? t 'my-env));
redefines the hooks it needs with (:require (my-env-context? t)), for instance kbd-enter or structured-insert-horizontal;
redefines focus-extra-menu and focus-extra-icons with the same condition to add entries to the focus bar.
Since the definitions are tried from the most recent one, the module must be loaded after the generic modules, which is ensured by a :use of (generic generic-edit) and (generic generic-menu).