GNU TeXmacsUser manualReferenceAboutEnglish
Colors, preferences and themes

1.Three kinds of color specifications

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:

Environment variables

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.

Color classes and preferences

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.

Literal colors

The R highlighter returns fixed colors (#8020c0, dark green, …), and the R and Mathemagix highlighters use the fixed color COLOR_MARKUP for markup.

2.Color classes

The color classes form a fixed vocabulary, defined in initialize_color_encodings (

System/Language/language.cpp

):

int encode_color (string s)
(number of a color class)

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.

string decode_color (string lan, int c)
(color for a code in a language)

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.

void initialize_color_decodings (string lan)
((re)read color preferences)

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:

Class Code Default
comment 1 brown
error 3 dark red
preprocessor 4 #004000
preprocessor_directive 5 #20a000
constant, constant_identifier, constant_function, constant_type, constant_category, constant_module 10–15 #4040c0
constant_number 16 #3030b0
constant_string 17 dark grey
constant_char 18 #333333
variable 20 #606060
variable_identifier 21 #204080
variable_function 22 #606060
variable_type, variable_category, variable_module 23–25 #00c000
variable_ioarg 26 #00b000
declare, declare_identifier, declare_function, declare_type 30–33 #0000c0
declare_category 34 #d030d0
declare_module 35 #0000c0
operator 40 #8b008b
operator_openclose 41 #B02020
operator_field 42 #888888
operator_special 43 orange
keyword, keyword_conditional 50, 51 #309090
keyword_control 52 #000080
(unknown) \(- 1\) red

Table 1. Color classes for syntax highlighting.

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.

3.Preferences

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:

4.Environment variables and themes

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

:

Variable Default Use
keyword-color #8020c0 keywords
constant-color #2060c0 constants
number-color #2060c0 numbers
string-color #a06040 strings
operator-color (empty) operators
comment-color brown comments
preprocessor-color #400040 preprocessor directives
modifier-color #8020c0 declaration modifiers
declaration-color #0000e0 declarations
macro-color #00A0A0 macro declarations
function-color #606060 functions
type-color dark green types
defined-color #204080 defined identifiers
misc-lexeme-color (empty) miscellaneous lexemes
alt-keyword-color #309090 alternative keywords
alt-constant-color #800080 alternative constants

Table 2. Environment variables for syntax highlighting.

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.

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