GNU TeXmacsUser manualReferenceAboutEnglish
Editing support for code

1.Overview

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

2.Mode predicates

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

.

3.Programs as documents of lines

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.

(inside-program?)
(cursor in a line of a program?)

True if the cursor tree is atomic and its parent is a document.

(program-tree)
(the whole program)
(program-row row)
(the string of a line)
(program-row-number)
(current line)
(program-column-number)
(current column)
(program-go-to row col)
(move the cursor)

Access to the enclosing document, to its lines and to the cursor position in terms of rows and columns.

4.Brackets

Three user preferences control bracket handling; they are mirrored in the Scheme variables prog-auto-close-brackets?, prog-highlight-brackets? and prog-select-brackets?:

prog:automatic 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).

prog:highlight brackets

Highlight the matching pair of brackets around the cursor, using the alternative selection named "brackets".

prog:select 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:

(bracket-open lb rb esc)
(insert an opening bracket)
(bracket-close lb rb esc)
(insert a closing bracket)

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.

(select-brackets path lb rb)
(highlight matching brackets)
(select-brackets-after-movement lbs rbs esc)
(highlight after cursor movement)

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.

(program-select-enlarge lb rb)
(enlarge the selection to the next brackets)

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.

5.Tab stops and indentation

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:

(program-compute-indentation doc row col)
(indentation of a line)

Returns the number of spaces with which line row of the program doc should be indented. The default returns 0. The current overloads are:

Other languages, including C++, use the default.

(program-indent-line doc row unindent?)
(re-indent one line)
(program-indent unindent?)
(re-indent the current line)
(program-indent-all unindent?)
(re-indent all lines)

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.

6.Copy and paste

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.

7.Scheme specific tools

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

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