![]() |
Selections, the clipboard, search and replace | ![]() |
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.
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.
contents from another application ((extern string)) are converted with the import format, with some care for LaTeX in math mode (dollars are added) and for code in program mode;
TeXmacs contents are inserted with insert_tree, after adapting the mode: text pasted into a formula is refused with an error message, a formula pasted into text is wrapped in math (and conversely), a formula pasted into a program session is converted by the plug-in (plugin-math-input), and a table pasted inside a table is written into the existing table at the cursor with table_write_subtable.
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.
edit_replace_rep (
Edit/Replace/edit_search.cpp
) provides the queries which editing code uses to find its context:
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.
The path of the child of the innermost l ancestor in which the cursor is.
The innermost with setting var to val.
The innermost ancestor whose label is in a tuple of names.
The previous or next accessible tree with a given label in document order.
There are two independent implementations of search and replace.
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:
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;
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;
stores all matches as the alternative selection alternate, which the editor highlights, and selects the next match after the reference position;
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.
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).
The case insensitive search only lowercases the document: search_string (
Data/Tree/tree_search.cpp:256
) compares the lowercased text with the pattern as it is, and the other matching routines (match, match_atomic, used for compound and wildcard patterns) ignore the flag altogether. The search toolbar lowercases the pattern before searching (search-toolbar-search), but the search tool (open-search) does not, so with case-insensitive-match on, a pattern containing capitals typed in the search tool never matches.
compute_selection temporarily switches to source access mode, with a comment in the code explaining that the DRD may be the wrong one when searching from a popup window, which can cause a whole macro to be highlighted instead of the matched text inside it.
The keyboard search mode and the search tools do not share their state: the former stores its last pattern in the clipboard search, the latter in the auxiliary buffer of the tool.