![]() |
Colors, preferences and themes | ![]() |
As explained in the chapter on the typesetting of code, language_rep::get_color returns a string which concater_rep::typeset_colored_substring interprets either as the name of an environment variable or as a color. Historically, the various highlighters have made different choices, so that there are three families:
The Scheme, C++ and Mathemagix highlighters return names such as keyword-color or comment-color. The actual color is the value of that variable at the place where the code is typeset. These colors can be changed by style files, themes and with, and they adapt to dark backgrounds.
The generic highlighter prog_language_rep (Python, Java, Scala, Julia, JSON, …), the Fortran and Scilab highlighters and the packrat based highlighting of verb_language_rep compute a color class such as keyword_conditional or constant_string, encode it as an integer, and decode it into a color with user preferences. These colors are global for each language; they do not depend on the document or its theme.
The R highlighter returns fixed colors (#8020c0, dark green, …), and the R and Mathemagix highlighters use the fixed color COLOR_MARKUP for markup.
The color classes form a fixed vocabulary, defined in initialize_color_encodings (
System/Language/language.cpp
):
Returns the integer code of the class s, or \(- 1\) if the class is unknown. The table language_rep::color_encoding is static and shared by all languages; it is initialized on first use.
Returns the color associated to the code c for the language named lan (as given by prog_language (lan)), or "" if there is none. The per-language table color_decoding is filled by initialize_color_decodings the first time it is needed.
For each class c, reads the preference syntax:lan:c with get_preference, falling back to a built-in default, and stores the result in the decoding table of the language. The code \(- 1\) (unknown class) is decoded with the preference syntax:lan:none. This function is exported to Scheme as (syntax-read-preferences lan) (
Scheme/Glue/build-glue-basic.scm
).
The classes, their codes and the built-in defaults are:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Code 0 means “no color”; it is the initial value in the highlight arrays of the packrat mechanism. Note that a group name which is not in this table, such as operator_decoration used by
python-lang.scm
and
julia-lang.scm
, is encoded as \(- 1\) and therefore drawn in the none color, which is red by default.
The preferences syntax:lan:class are ordinary user preferences. Language modules declare defaults for them with define-preferences, together with a notification function which re-reads the decoding table when the user changes a value:
(define (notify-python-syntax var val)
(syntax-read-preferences "python"))
(define-preferences
("syntax:python:none" "red" notify-python-syntax)
("syntax:python:comment" "brown" notify-python-syntax)
("syntax:python:keyword" "#309090" notify-python-syntax)
…) |
Such declarations exist for python (in
src/plugins/python/progs/python-lang.scm
), julia and cpp (in
src/plugins/code/progs/
), scheme (in
prog/scheme-edit.scm
) and fortran (in
prog/fortran-edit.scm
). The Scheme and C++ highlighters do not use the decoding tables, so their syntax:* preferences currently have no visible effect. Other languages (Java, Scala, JSON, …) simply use the built-in defaults of the table above unless the user sets the preferences by hand.
Two practical consequences:
The decoding table is read once per language and session. After a change of preference, the notification refreshes the table, but boxes which were already typeset keep their colors until the corresponding text is typeset again (for instance after an edit or an explicit re-typesetting of the document).
Since these colors are global, a document which is shown with a dark background keeps the light-background colors of the preferences. Only the environment variable based highlighters adapt automatically. The themes dark-mode and programmer set the environment variable adapt-dark-colors: then concater_rep::typeset_colored_substring lightens each literal color of the other highlighters, until its contrast with bg-color is at least 4.5 (WCAG), and keeps hue and the colors which already have that contrast. Other documents, also those with the other dark themes (Dark vador, Blackboard), are not changed.
The environment variables used by the Scheme, C++ and Mathemagix highlighters are declared in
Data/Drd/vars.cpp
(C++ constants KEYWORD_COLOR, CONSTANT_COLOR, …), get the DRD type TYPE_COLOR in
Data/Drd/drd_std.cpp
, and have the following defaults in
Typeset/Env/env_default.cpp
:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
These variables are also documented from the user's point of view among the miscellaneous environment variables.
The theme mechanism groups them. In
themes/base/base-colors.ts
:
<new-theme|highlight-colors|keyword-color|constant-color|number-color|string-color|operator-color|comment-color|preprocessor-color|modifier-color|declaration-color|macro-color|function-color|type-color|defined-color|misc-lexeme-color|alt-keyword-color|alt-constant-color> <copy-theme|all-colors|colors|gui-colors|highlight-colors|session-colors> |
The primitive new-theme (edit_env_rep::exec_new_theme in
Typeset/Env/env_exec.cpp
) creates, for each listed variable v, a variable highlight-colors-v initialized with the current value of v, and a macro with-highlight-colors which sets each v to the value of highlight-colors-v. copy-theme builds a new theme from existing ones in the same way. A concrete theme then overrides the prefixed variables; for instance
themes/dark/dark-scene.ts
starts with <copy-theme|dark-scene|all-colors> and sets dark-scene-keyword-color to #d070f0, dark-scene-comment-color to #d06030, and so on. Wherever the theme is applied (through with-dark-scene, apply-theme or select-theme), code typeset by the environment variable based highlighters automatically gets colors which are readable on a dark background.
To make a highlighter theme-aware, it is therefore enough to return variable names from get_color. Conversely, any new environment variable used in this way should be added to the highlight-colors theme and to the dark themes.