![]() |
Editing support for code | ![]() |
The editing behaviour inside code is implemented entirely in Scheme, in the directory
prog/
. It does not use the C++ language objects of the previous chapters: there is no shared tokenizer between highlighting and editing. The main files are:
prog/prog-edit.scm
Generic routines: access to the lines of a program, preferences for brackets, bracket insertion, highlighting and selection, tab stops, the indentation framework, and copy and paste.
prog/prog-kbd.scm
Keyboard bindings in programming mode and per language. This module is loaded lazily the first time the cursor is in programming mode ((lazy-keyboard (prog prog-kbd) in-prog?) in
init-texmacs.scm
), and it loads all the language specific edit modules.
prog/scheme-edit.scm,
cpp-edit.scm,
python-edit.scm,
fortran-edit.scm,
java-edit.scm,
scala-edit.scm,
dot-edit.scm
Language specific indentation and bracket handling.
prog/scheme-tools.scm,
scheme-autocomplete.scm,
scheme-menu.scm
Extra developer tools for Scheme code: help on symbols, jump to definition, completion, running a Scheme file.
prog/prog-menu.scm
The Format menu and the icon bar in programming mode (loaded lazily as well).
Language specific behaviour is selected with the :mode or :require clauses of tm-define and kbd-map, using predicates defined with texmacs-modes in
kernel/texmacs/tm-modes.scm
:
(in-prog% (and (== (get-env "mode") "prog") (not (in-graphics?))))
…
(in-python% (== (get-env "prog-language") "python"))
(in-prog-python% #t in-prog% in-python%) |
Each name% entry defines a predicate name?. Pairs in-lan? and in-prog-lan? exist for cpp, dot, octave, java, javascript, json, fortran, scala, scheme, python and julia. The predicate in-code? is defined by (in-code% (style-has? "code-style")); it is used for the focus bar in
prog/prog-menu.scm
.
All routines assume that a program is a document whose children are strings, one per line, and that the cursor is inside one of these strings. They fail (or return #f) for code containing markup.
True if the cursor tree is atomic and its parent is a document.
Access to the enclosing document, to its lines and to the cursor position in terms of rows and columns.
Three user preferences control bracket handling; they are mirrored in the Scheme variables prog-auto-close-brackets?, prog-highlight-brackets? and prog-select-brackets?:
Insert the closing bracket or quote together with the opening one (not after the escape character, and around the selection if there is one).
Highlight the matching pair of brackets around the cursor, using the alternative selection named "brackets".
Let the “select enlarge” command (kbd-select-enlarge) successively select larger bracketed regions.
These preferences can be set in the preferences dialog (
texmacs/menus/preferences-widgets.scm
). The core routines are:
Insert the bracket, honouring the preferences above. Each language wraps them (scheme-bracket-open, cpp-bracket-open, python-bracket-open, fortran-bracket-open, dot-bracket-open, …) with "\\" as escape character and binds them to the bracket keys in a kbd-map with the appropriate mode.
The first function finds the innermost pair of brackets around path with the C++ routines find-left-bracket and find-right-bracket (find_left_bracket and find_right_bracket in
Data/String/analyze.cpp
, which count nesting levels and may cross line boundaries within the same document), and sets the alternative selection "brackets". The second is called from language specific overloads of notify-cursor-moved, for instance
(tm-define (notify-cursor-moved status)
(:require prog-highlight-brackets?)
(:mode in-prog-python?)
(select-brackets-after-movement "([{" ")]}" "\\")) |
and highlights the pair adjacent to the cursor, if any.
Used by the language specific overloads of kbd-select-enlarge for Scheme and C++.
Scheme-only helpers string-bracket-forward, string-bracket-backward, string-bracket-level and program-previous-match operate on plain strings and lines.
The tab width is the preference editor:verbatim:tabstop (default 4), returned by (get-tabstop); Java and dot override it to 4 and Scala to 2 through :mode. (insert-tabstop) inserts spaces up to the next multiple of the tab width, and (remove-tabstop) removes one level of indentation. There are no tab characters in programs edited in TeXmacs.
Automatic indentation is organized around one overloadable function:
Returns the number of spaces with which line row of the program doc should be indented. The default returns 0. The current overloads are:
Scheme (
prog/scheme-edit.scm
): looks back for the previous arguments of the enclosing form and uses the indentation arity of the head symbol (indent-get-arity, from the lists nullary-indent, unary-indent, … in
tm-mode.el
) to decide between aligning with the previous argument and indenting by a fixed amount;
Python (
prog/python-edit.scm
): the indentation of the previous line, plus one tab stop if that line ends with : (after a naive removal of # comments);
Fortran (
prog/fortran-edit.scm
): the indentation of the previous line, plus one tab stop if that line starts with a keyword like function, program, subroutine, do or module;
Java, Scala and dot: always one tab stop.
Other languages, including C++, use the default.
Set the indentation of lines according to program-compute-indentation. The flag unindent? is not implemented yet.
In programming mode, insert-return is redefined to insert a raw line break and to re-indent the new line with (program-indent #f). In
prog/prog-kbd.scm
the following keys are bound for all languages: ⌃I and ⌃⇥ re-indent the current line, ⌥⌃⇥ re-indents the whole program, and ␣⇥ inserts a tab stop. Several text mode shortcuts are overridden so that characters such as $, \, " and the quotes are inserted literally.
In
prog/prog-edit.scm
, kbd-cut and kbd-paste are overloaded in programming mode when the selection (or clipboard) is purely textual. They export and import through the converters lan-snippet if they exist, where lan is the value of prog-language, and through verbatim otherwise. For Scheme, kbd-copy exports to the scheme format (
prog/scheme-edit.scm
). The converters for the languages of the code plugin are defined in
src/plugins/code/progs/code-format.scm
; they use texmacs->verbatim with the SourceCode encoding and code->texmacs.
When developer-mode? is on,
prog/prog-kbd.scm
binds ⌥F1 to scheme-popup-help, ⌥⌃F1 to scheme-inbuffer-help and ⌘F1 to scheme-go-to-definition, all applied to the word at the cursor; in a Scheme file, ⌘⇧R runs the file. Completion (kbd-variant, normally bound to ⇥) uses scheme-completions from
prog/scheme-autocomplete.scm
, which collects the glued symbols listed in
prog/glue-symbols.scm
(all-glued-symbols) and the symbols used in Scheme code (all-used-symbols).