GNU TeXmacsUser manualReferenceAboutEnglish
Selections, the clipboard, search and replace

1.Selections

The selection of an editor is a range_set cur_sel of edit_select_rep (

Edit/Replace/edit_select.cpp

): a flat array of paths \((s_1, e_1, s_2, e_2, \ldots)\), of which normally only the first range is used. The overall organization of the editor state is described in the editor; here are the details which matter for editing operations.

Setting the selection.
select (p1, p2) orders the two paths and stores them. When the common ancestor is not a table or a row and semantic editing is enabled (the preferences semantic editing and semantic selections), it first lets semantic_select enlarge the range to a syntactically meaningful subformula: inside mathematics (or code in the minimal language), the packrat parser of the current language (packrat_select) is asked for the smallest grammatical unit containing the range. select_enlarge and select_enlarge_environmental implement the repeated “select more” commands; selection_set_start, selection_set_end and selection_set_paths (selection-set-start, …) are the Scheme level setters.
Normal and table selections.
A selection is a table selection when both ends are in cells of the same table (is_table_selection); selection_get_subtable then returns the path of the table format and the rectangle of selected cells, enlarged by table_bound to whole spanning cells. selection_active_any, selection_active_normal, selection_active_table and selection_active_small (a normal selection which does not span several paragraphs) are the predicates used by the editing operations to decide whether to wrap the selection.
Reading the selection.
selection_get () (selection-tree) returns the selected material as a tree: a subtable (with its formats) for table selections, otherwise the result of selection_compute on the corrected range, simplified with simplify_correct. The range is first corrected by selection_correct, which moves its ends to positions that make a well-formed selection (and, in source mode, uses source access to the tree). selection_get_cut () returns the selection and deletes it; this is what the math constructors use to wrap a selection.
Cutting.
cut (p1, p2) and its worker raw_cut delete everything between two paths: they recurse through document and concat nodes (deleting whole children in the middle and partial children at the ends, then joining the remainders), empty the cells of a table rectangle (and remove whole rows or columns when complete rows or columns are selected), replace a selected compound tree by the empty string, and remove characters in a string. selection_cut (key) copies the selection to the clipboard key (unless key is "none") and cuts it.
Focus and alternative selections.
The focus (the tree for which the focus menus and toolbars are shown) is computed by focus_get: the manually set focus (manual_focus_set, set-manual-focus-path) if there is one, otherwise the selection or the cursor, after skipping “transparent” nodes such as concat, document, table nodes and a few style specific tags. Named alternative selections (set_alt_selection, set-alt-selection) are additional range sets drawn by the editor; the search tools use alternate for all matches and search-reference for the starting point.

2.The clipboard

Clipboards are identified by names: primary is the ordinary clipboard, mouse the selection of the X11 middle button, and any other name a private clipboard (for instance wrapbuf, used by make to wrap the selection, or nowhere, used when a selection is deleted). The contents of a clipboard are a tuple (texmacs tree mode language), recording the mode and language at the selection, together with string versions for the system clipboard.

Copy.
selection_set (key, t) builds the tuple. For the primary and mouse clipboards it also converts the tree to a string in the export format (selection_export, set with clipboard-set-export): verbatim, html and latex are first expanded with the typesetter (exec_verbatim, exec_html, exec_latex) and then converted with the corresponding -snippet converter; the default is the TeXmacs format, plus, under Qt, a verbatim version for other applications. The result is handed to the GUI with ::set_selection. selection_copy (key) (clipboard-copy) copies the current selection; inside an active graphics, the graphics editor's graphics-copy is used instead.
Paste.
selection_paste (key) (clipboard-paste) retrieves the clipboard through ::get_selection in the import format (clipboard-set-import):

The Scheme commands clipboard-copy, clipboard-cut and clipboard-paste (

utils/library/cpp-wrap.scm

) are overloaded in several contexts (sessions, folding, comments), and

utils/edit/selections.scm

adds commands such as clipboard-copy-export.

3.Structural searches

edit_replace_rep (

Edit/Replace/edit_search.cpp

) provides the queries which editing code uses to find its context:

search_upwards (l), inside (l)

The innermost ancestor of the cursor with the label l (inside?). Note that the search starts at the grandparent of the cursor path, that is, at the innermost tree that contains the cursor position.

search_parent_upwards (l)

The path of the child of the innermost l ancestor in which the cursor is.

search_upwards_with (var, val), inside_with

The innermost with setting var to val.

search_upwards_in_set (t), inside_which

The innermost ancestor whose label is in a tuple of names.

search_previous_compound, search_next_compound

The previous or next accessible tree with a given label in document order.

4.Search and replace

There are two independent implementations of search and replace.

4.1.The search and replace tools

The search and replace commands of the menus and keyboard (interactive-search, interactive-replace) are implemented in Scheme, in

generic/search-widgets.scm

. They open either a toolbar or a search tool with an embedded TeXmacs input field, whose contents is an arbitrary TeXmacs tree, the pattern. After each change of the pattern, perform-search:

  1. calls the glue function tree-search-tree-at, that is, search (t, what, p, pos, limit) in

    Data/Tree/tree_search.cpp

    , on the body of the document being searched, starting near the cursor and with an initial limit of 100 matches;

  2. filters the results by mode and language (filter-search-results): unless the document is in source mode, a match is only kept when the mode and the language of that mode at the match are the same as at the cursor when the search started;

  3. stores all matches as the alternative selection alternate, which the editor highlights, and selects the next match after the reference position;

  4. if the limit was reached, schedules a new search with twice the limit, so that large documents are searched incrementally.

The matcher compares the pattern with the document tree, recursing into accessible children. Its behaviour is controlled by preferences, read at the start of each search (initialize_search): allow-partial-match (a string pattern matches inside a longer string), allow-initial-match (it matches a prefix), allow-blank-match (an empty argument of the pattern matches anything), allow-injective-match (the arguments of a compound pattern only need to match a subsequence of the arguments of the document tree), allow-cascaded-match (an argument of the pattern may match deeper inside the corresponding argument), and case-insensitive-match. When the preference search-and-replace is set (by interactive-replace), the first four are replaced by their stricter allow-*-replace counterparts, which are off by default. Patterns may also contain wildcard tags, and a select-region tag restricts the match to a part of the pattern.

Replacing is also done in Scheme: replace-one and replace-all select the next match, compute the replacement with adjust-by (which substitutes wildcards and preserves the capitalization of the matched text), cut the match and insert the replacement, and search again; replace-all repeats this inside a single start-editing / end-editing pair so that it can be undone at once.

4.2.The keyboard search mode

The C++ class edit_replace_rep still contains an older, keyboard driven implementation. It is no longer bound to the standard shortcuts or menus (which call interactive-search and interactive-replace), but its glue routines are still exported (search-start, replace-start, and replace-start-forward in

utils/library/cursor.scm

). search_start (forward) (search-start) puts the editor in the input mode INPUT_SEARCH; each typed key is then passed by the keyboard-press overload for search-mode? (

generic/generic-edit.scm

) to search_keypress (key-press-search), which extends the pattern and moves to the next match with next_match, a walk through accessible positions in document order (step_horizontal, step_ascend, step_descend). Backspace returns to the previous match and pattern. replace_start and replace_keypress implement the classical “replace (y, n, a)?” dialogue in the footer. Matches are restricted to the mode and language at the starting point, and patterns are compared literally (test_match, where an empty string in a compound pattern acts as a wildcard). The same input-mode mechanism is used by spell checking (spell_start, spell_keypress, see languages, hyphenation and spell checking).

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