GNU TeXmacsUser manualReferenceAboutEnglish
The generic structured editing hooks

1.The focus tree and outward recursion

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

):

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.

2.The hooks

2.1.Keyboard

The keys of

generic/generic-kbd.scm

call parameterless commands, which call the following hooks on the focus tree:

(kbd-enter t shift?)

↩ and ⇧↩ (with kbd-control-enter and kbd-alternate-enter for the modified variants). The default inserts a paragraph break.

(kbd-space-bar t shift?)

The space bar.

(kbd-remove t forwards?)

⌫ and ⌦; the default removes text, or the selection.

(kbd-variant t forwards?)

⇥ and ⇧⇥ (tab completion; for labels and citations, completion of the key). kbd-alternate-variant is used for ⌥⇥ and by default inserts a horizontal tab.

kbd-horizontal, kbd-vertical, kbd-extremal, kbd-incremental

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.

2.2.Structured insertion, removal and movement

These hooks implement the ⌃…, ⌘⌃… and ⌥… shortcuts and the corresponding icons of the focus bar:

(structured-insert-horizontal t forwards?), structured-insert-vertical, structured-remove-horizontal, structured-remove-vertical

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.

structured-horizontal, structured-vertical, structured-inner-extremal, …

Move to the next or previous argument, cell, branch, …

traverse-horizontal, traverse-vertical, traverse-incremental, traverse-extremal

Traversal of the document by structure (for instance from one session input field to the next).

(variant-circulate t forward?)

Cycle through the variants of a tag (⌥⇥); the default uses the variant-tag groups (

utils/edit/variants.scm

).

(alternate-toggle t)

Fold or unfold, toggle between two alternative forms (⌃*); the default uses the pairs declared with define-alternate.

(numbered-toggle t)

Toggle numbering (⌃#); the default toggles the final * of the tag name.

geometry-horizontal, geometry-vertical, geometry-default, …

Change the position or size of the focus (spaces, brackets, table extents, animations; see

generic/format-geometry-edit.scm

).

structured-maximize, structured-minimize, swipe-horizontal, swipe-vertical

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.

2.3.Focus menus and focus icons

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:

focus-ancestor-menu, focus-ancestor-icons

Entries for enclosing tags (for instance the document title around an author).

focus-tag-menu, focus-tag-icons

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.

focus-move-menu, focus-insert-menu

Structured movements and insertions; shown only if focus-can-move? resp. focus-can-insert-remove? hold.

focus-hidden-menu, focus-extra-menu, focus-label-menu

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

3.Adding behaviour for a new tag

To give a new tag my-env its own behaviour, a module typically

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

  2. defines a context predicate, (tm-define (my-env-context? t) (tree-is? t 'my-env));

  3. redefines the hooks it needs with (:require (my-env-context? t)), for instance kbd-enter or structured-insert-horizontal;

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

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