GNU TeXmacsUser manualReferenceAboutEnglish
Programming languages and syntax highlighting

1.Introduction

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 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/

.

2.Overview of the data flow

The following chain of events turns a block of Python code in a document into colored text.

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

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

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

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

    ).

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

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

3.Main source files

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.

Data/Parser/

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.scm
and 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.

4.Contents of this chapter

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