![]() |
Programming languages and syntax highlighting | ![]() |
TeXmacs can display fragments of computer programs inside documents: inline code such as cpp or python, blocks of code such as cpp-code or python-code, the input fields of interactive sessions, and whole source files (a .scm or .py file opened in TeXmacs). In all these cases the text is typeset in a special programming mode (the environment variable mode equals prog), and the programming language is given by the environment variable prog-language. While a string is typeset in programming mode, a language object chops it into lexical tokens and assigns a color to each of them. This is what users perceive as syntax highlighting.
This chapter describes how this works inside the C++ kernel and the Scheme layer. It covers:
the path from a code environment in a document to colored text boxes, through the typesetting environment and the typesetter;
the language registry, i.e. how the name stored in prog-language is mapped to a C++ object of class language_rep, and the various implementations of this class;
the small library of hand-written parsers in Data/Parser/, the generic language class prog_language_rep which is configured from Scheme, and the Scheme definitions (parser-feature) by which a language is described;
the alternative highlighting mechanism based on packrat grammars and the define-language macro;
how colors are assigned, how they can be customized through preferences and how they are adapted by themes such as the dark theme;
editing support for code: keyboard modes, automatic brackets, bracket highlighting, indentation, copy and paste;
a complete walk-through for adding a new language, performance considerations and known pitfalls.
The languages which have a grammar of tree-sitter (most of them, see the field grammar of
tm-program-languages.scm
) are coloured, cut into tokens and indented by their syntax trees instead, in a build with tree-sitter: their language objects are wrapped by those of tree-sitter, which use the ones described here for the lines which the syntax tree does not colour (syntax trees of program code). What this chapter describes is what the other languages, and all of them in a build without tree-sitter, do.
All C++ file names below are relative to
src/src/
, and all Scheme file names are relative to
src/TeXmacs/progs/
, unless stated otherwise. The language definitions which live in plugins are found in the source tree in
src/plugins/
(for instance
src/plugins/code/progs/cpp-lang.scm
); they are installed into $TEXMACS_PATH/plugins/ by the build. Style packages are relative to
src/TeXmacs/packages/
.
The following chain of events turns a block of Python code in a document into colored text.
The document contains python-code whose body is a document, one string per line. The macro python-code (
environment/env-program.ts
) expands to python, which sets mode to prog and prog-language to python.
When the typesetter executes this with, the environment notices that a variable of type Env_Mode or Env_Language was modified and calls edit_env_rep::update_language (
Typeset/Env/env_semantics.cpp
). In programming mode this sets env->lan= prog_language ("python").
prog_language (
System/Language/prog_language.cpp
) looks up the language object in a global cache. The first time, it creates a prog_language_rep, whose constructor loads the Scheme module (python-lang) and asks it, through parser-feature, for keywords, operators, number and string syntax and comment delimiters.
Each line of the program is an atomic tree. The concatenator (
Typeset/Concat/concater.cpp
) sees that env->mode == 3 and calls concater_rep::typeset_prog_string (
Typeset/Concat/concat_text.cpp
).
typeset_prog_string repeatedly calls env->lan->advance (t, pos), which advances pos over one token, and then env->lan->get_color (t, start, pos), which returns a color specification for the token.
concater_rep::typeset_colored_substring turns this specification into a color: if it names an environment variable (such as keyword-color) the value of that variable is used, otherwise the string is interpreted as a color name. A text box with this color is appended to the line.
Editing commands (indentation, bracket handling, copy and paste) do not use the language object; they are implemented in Scheme in the directory
prog/
and dispatched on prog-language through the mode predicates in-prog-python?, in-prog-cpp?, and so on.
System/Language/language.hpp,
language.cpp
The abstract class language_rep, text properties, the registry functions and the encoding and decoding of syntax colors.
System/Language/impl_language.hpp,
impl_language.cpp
Declarations of the concrete language classes for programming languages, and shared helpers for multi-line comments.
System/Language/prog_language.cpp
The generic prog_language_rep configured from Scheme, and the dispatcher prog_language.
System/Language/scheme_language.cpp,
cpp_language.cpp,
mathemagix_language.cpp,
r_language.cpp,
scilab_language.cpp,
fortran_language.cpp
Hand-written highlighters for specific languages.
System/Language/verb_language.cpp
The fallback language, which also implements highlighting through packrat grammars.
Small reusable parsers: blanks, identifiers, keywords, operators, numbers, strings, escaped characters, inline comments and preprocessor directives.
Typeset/Concat/concat_text.cpp
The typesetting of strings in programming mode (typeset_prog_string).
kernel/texmacs/tm-language.scm
The define-language macro for packrat grammars.
prog/default-lang.scmand the *-lang.scm files
The default and the per-language parser-feature definitions.
prog/prog-edit.scm,
prog/prog-kbd.scm, prog/*-edit.scm
Editing support for code.
environment/env-program.ts
The markup for inline code and blocks of code.
themes/base/base-colors.ts,
themes/dark/dark-scene.ts
The theme for highlighting colors.