![]() |
The editor: classes, state, modifications and undo | ![]() |
Every view owns an editor, which holds everything that is specific to one way of looking at a document: the cursor, the selection, the typeset box tree, the undo history and the window decorations which depend on the cursor. This page describes the classes from which the editor is assembled, the state it keeps, and the path which a modification of the document follows: from the elementary tree operations through the observers to the typesetters and cursors of all views on the buffer, and into the undo history.
How the editor receives events and when it retypesets and repaints is the subject of the event loop. The editing operations themselves (text, mathematics, tables, structure, search) are described in structured editing, search and automatic content, and the typesetter which the editor drives in the typesetting algorithm.
An editor is an instance of edit_main_rep, created by new_editor (server_rep* sv, tm_buffer buf) in
Edit/Editor/edit_main.cpp
. Its abstract base class editor_rep (
Edit/editor.hpp
) derives from simple_widget_rep, the widget class for canvases of the GUI back-end (Qt, Cocoa or Widkit), so that an editor is the widget which the GUI displays inside the scrollable canvas of a window. The handle class editor extends widget.
editor_rep declares the complete public interface of the editor as pure virtual functions, grouped by the subclass which implements them, and holds the members shared by all parts:
class editor_rep: public simple_widget_rep {
public:
server_rep* sv; // the underlying texmacs server
widget_rep* cvw; // non reference counted canvas widget
tm_view_rep* mvw; // master view
protected:
tm_buffer buf; // the underlying buffer
drd_info drd; // the drd for the buffer
tree& et; // all TeXmacs trees
box eb; // box translation of tree
path rp; // path to the root of the document in et
path tp; // path of cursor in tree
…
}; |
edit_main_rep combines the implementation classes by multiple inheritance; all of them derive virtually from editor_rep:
(
Edit/Interface/edit_interface.cpp
,
edit_keyboard.cpp
,
edit_mouse.cpp
,
edit_repaint.cpp
,
edit_footer.cpp
,
edit_complete.cpp
) Event handlers, change notification and apply_changes, repainting, the footer, keyboard shortcuts, input modes and completion.
(
Edit/Interface/edit_cursor.cpp
) The cursor and cursor movements.
(
Edit/Interface/edit_graphics.cpp
) Interaction with graphics.
(
Edit/Editor/edit_typeset.cpp
) The link with the typesetter: document data, environment queries and invalidation; see the typesetting algorithm.
(
Edit/Modify/edit_modify.cpp
) Reception of modifications, undo and redo.
(
Edit/Modify/
) Structured editing operations on text, mathematics, tables and markup.
(
Edit/Process/
) Generation of bibliographies, tables of contents, indexes and glossaries.
(
Edit/Replace/edit_select.cpp
) Selections and the clipboard.
(
Edit/Replace/edit_search.cpp
,
edit_spell.cpp
) Searching upwards in the tree, interactive search and replace, spell checking.
edit_main_rep itself adds a table of editor properties (set_property, get_property), printing, some queries (such as the_buffer, the_path, the_buffer_path) and debugging routines (show_tree, show_box, …). Its constructor attaches an edit observer to the root of the buffer and puts the cursor at the start of the document:
edit_main_rep::edit_main_rep (server_rep* sv, tm_buffer buf):
editor_rep (sv, buf), props (UNKNOWN), ed_obs (edit_observer (this))
{
attach_observer (subtree (et, rp), ed_obs);
notify_change (THE_TREE);
tp= correct_cursor (et, rp * 0);
} |
Similarly, the constructor of edit_modify_rep allocates a new author identifier (new_author) and an archiver for the buffer root, which in turn attaches an undo observer to it.
To add a new editing command which is callable from Scheme, one typically declares it as a pure virtual function in editor_rep, implements it in the appropriate edit_*_rep class, and adds an entry to
Scheme/Glue/build-glue-editor.scm
(after which the glue has to be regenerated). Since the glue calls it on get_current_editor (), the command always acts on the current view.
The state of an editor, other than the typesetting state, consists of the following items.
The cursor is the path tp in et; it always starts with the root path rp of the buffer. It is returned by the_path (cursor-path in Scheme); the_buffer_path (buffer-path) returns rp. The graphical cursor cu and the “ghost cursor” mv used during vertical movements are computed from the box tree in edit_cursor_rep. Cursor movements go through go_to (path) and related routines, which update tp, call notify_change (THE_CURSOR) and the Scheme hook notify-cursor-moved. The routine make_cursor_accessible moves the cursor to an accessible position in the document.
The current selection is a range_set cur_sel in edit_select_rep, set by select (start, end) and friends and queried by selection_get_start, selection_get_end and selection_active_any. Additional named selections (alt_sels) are used to highlight search results (alternate), matching brackets (brackets) and spell errors (spell_errors); see set_alt_selection. Copy and paste go through selection_copy, selection_paste and selection_set (key, tree, persistant), which encode the selection as tuple ("texmacs", t, mode, lan) and hand it to the GUI clipboard identified by key (primary, mouse, …), possibly after conversion to the export format.
The focus is the innermost tree which is considered to be edited (used for focus dependent menus and toolbars). It is usually derived from the cursor or selection (focus_get), but can be set manually with manual_focus_set.
The values of environment variables at the cursor, in particular the current mode and language, are obtained from the typesetter with get_env_value, get_env_string and related functions (get-env and get-env-tree in Scheme); the initial values of the document are obtained with get_init_value. They are recomputed lazily and are only valid when the document has been typeset, which is why editing commands occasionally force apply_changes.
input_mode in edit_interface_rep is one of INPUT_NORMAL, INPUT_SEARCH, INPUT_REPLACE, INPUT_SPELL and INPUT_COMPLETE and is changed by set_input_mode. In the non normal modes, the Scheme keyboard handlers forward the keys to routines like key-press-search (search_keypress) instead of inserting them.
The keys typed so far of a multi-key shortcut are kept in sh_s, together with an undo marker sh_mark, so that the effect of a prefix can be undone when the next key completes a longer shortcut (see keyboard events). Input method pre-edit text is handled similarly with pre_edit_s and pre_edit_mark.
The bit set env_change records what has to be recomputed at the next apply_changes (see change notification); last_change, last_update and last_event are time stamps used to decide when menus and the footer are updated.
The left and right footer messages message_l and message_r (see set_message).
The archiver arch and the author identifier author of edit_modify_rep (see below).
The zoom factor zoomf, the magnification magf, the visible rectangle vx1, vy1, vx2, vy2, and got_focus, which tells whether the editor has the keyboard focus. suspend and resume are called when the view is detached from, respectively attached to, a window or loses, respectively gains, the window focus.
Arbitrary Scheme trees can be associated to the editor with set_property and get_property.
Positions which have to survive modifications of the document are represented by position observers: position_new (path) attaches a tree_position observer to the tree at the given path, which is updated by all subsequent modifications; position_get returns the corrected path and position_delete detaches the observer. The editor uses this mechanism itself to preserve the cursor across modifications, and it is exported to Scheme as position-new-path, position-get and so on.
The document is only modified through a small set of elementary modifications (
Kernel/Types/modification.hpp
): MOD_ASSIGN, MOD_INSERT, MOD_REMOVE, MOD_SPLIT, MOD_JOIN, MOD_ASSIGN_NODE, MOD_INSERT_NODE, MOD_REMOVE_NODE and MOD_SET_CURSOR. A modification consists of its kind k, a path p and possibly a tree t. The function apply (tree& ref, modification mod) in
Kernel/Abstractions/observer.cpp
is the single entry point; wrappers like assign (path p, tree t), insert, remove, split, join, assign_node, insert_node, remove_node and set_cursor build the modification and call it. If the modified tree is attached to the_et, apply translates the modification into an absolute one and applies it to the_et with raw_apply. Modifications which are triggered while another modification is being performed (for instance by an observer which mirrors changes to linked trees) are queued in a list of “upcoming” modifications and executed afterwards, unless they concern a path which is already being modified.
The raw_* functions perform the actual change on the tree and notify the observers attached to the modified node, in three steps:
obs->announce (ref, mod) before the change; the default implementation dispatches to announce_assign, announce_insert, and so on;
specific notifications like notify_assign, notify_insert or notify_remove_node, which allow observers to reattach themselves or update positions;
obs->done (ref, mod) after the change.
Several observers can be attached to the same tree (they are combined with list_observer). The following observers matter for this document:
(
Data/Observers/ip_observer.cpp
) Every node of the_et carries an ip observer which knows its inverse path. Announcements are propagated upwards to the observers of the ancestors, with the path of the modification extended accordingly. Hence an observer attached to the root of a buffer is notified of all changes inside the buffer.
(
Data/Observers/edit_observer.cpp
) Attached to the buffer root by each editor. It forwards announcements to edit_announce, completions to edit_done and touch-notifications to edit_touch (in
Edit/Modify/edit_modify.cpp
).
(
Data/Observers/undo_observer.cpp
) Attached to the buffer root by each archiver; records every modification in the undo history through archive_announce.
The Scheme hooks attached through a link repository with a callback (scheme_observer, in
Data/Observers/tree_pointer.cpp
) are called as (callback 'announce tree modification), and similarly with 'done and 'touched. tm_buffer_rep::attach_notifier (Scheme: buffer-attach-notifier) uses this to call buffer-notify on all changes of a buffer, after an initial call of buffer-initialize; this is used for shared buffers and mirrored parts (
part/part-shared.scm
).
tree_position (for cursor positions, see above), tree_pointer, tree addenda (tree_addendum_new, used for instance by animations) and the observers which store syntax highlighting information (highlight_observer, used by the packrat parser).
When a modification inside a buffer is announced to an editor, edit_announce calls one of the editor_rep::notify_* routines (notify_assign, notify_insert, …, notify_set_cursor). For all modifications except cursor settings, edit_modify_rep saves the cursor in a position observer cur_pos and forwards the modification to the typesetter (the global notify_assign (typesetter, path, tree) and so on), which invalidates the corresponding parts of the box tree. After the modification, edit_done calls post_notify:
void
edit_modify_rep::post_notify (path p) {
if (!(rp <= p)) return;
selection_cancel ();
cancel_alt_selections ();
notify_change (THE_TREE);
tp= position_get (cur_pos);
position_delete (cur_pos);
cur_pos= nil_observer;
go_to_correct (tp);
} |
Since every editor on a buffer has its own edit observer on the buffer root, a modification made in one view is automatically propagated to all other views on the same buffer: each of them updates its typesetter and its cursor, cancels its selection and schedules a redraw. No explicit synchronization between views is needed.
Modifications of the kind MOD_SET_CURSOR do not change the tree; they are recorded in the undo history in order to restore cursor positions and selections on undo and redo. notify_set_cursor only acts if the modification carries the author of the editor, in which case it moves the cursor or restores the selection.
Each editor owns an archiver (
Data/History/archiver.hpp
), created with the author identifier of the editor and the root path of the buffer. The archiver stores a patch archive of past changes and a patch current for the changes of the ongoing user action. Each modification received by the undo observer is stored together with its inverse (computed with invert before the modification is applied) by archiver_rep::add, and the archiver is registered as pending.
User actions are delimited by the editor:
Sets the global current author to the author of the editor (set_author). Called at the start of each keyboard and mouse event.
Calls global_confirm (), which confirms all pending archivers: their current changes become one new undo step. Called at the end of each event. cancel_editing (called when an error occurs) calls global_cancel instead.
Records the current cursor and selection as MOD_SET_CURSOR modifications tagged with the author (the tags cursor, cursor-clear, start and end), so that undo returns to the right position.
Delimit a group of changes with a marker obtained from new_marker. mark_cancel undoes the changes since the marker. This is used for multi-key shortcuts and input method pre-edit text.
Confirm the current changes, respectively reopen the last undo step.
undo and redo call archiver_rep::undo and archiver_rep::redo, which apply the inverse patches (with the flag versioning set, so that the resulting modifications are not archived again) and return the path where the cursor should go. Since all editors on a buffer see all modifications, undo steps are tagged with their author; archiver_rep::undo keeps undoing steps until it has undone one of its own author. The whole history of all archivers is cleared with clear_undo_history (global_clear_history).
The history, authors, markers and the modified state are described in detail in undo, redo and the modification history.
The archiver also implements the “modified” status of a buffer. It remembers the depth of the archive at the last save and autosave; after notify_save, conform_save is true as long as the document is in the saved state (in particular after undoing all changes, in which case undo displays “Your document is back in its original state”). The editor exports this as need_save, and require_save forces the modified state. At the buffer level,
buffer_modified (name) is true if some view on the buffer needs to be saved (tm_buffer_rep::needs_to_be_saved); read-only buffers are never modified;
pretend_buffer_saved, pretend_buffer_modified and pretend_buffer_autosaved call notify_save or require_save on all views of the buffer.
This is why delete_window only detaches the view of a window instead of deleting it: a buffer without views would always be reported as unmodified. For the same reason, modifications of a buffer which has no editor are not recorded in any undo history.