How and where are the Python keywords assigned a syntax highlighting type in blender? Blender has a colour assigned for Builtins, Symbol, Special, Preprocessor, Reserved, Comment, String and Numbers.


The text editor theme for syntax highlighting

While testing importing Vim colorschemes to blender I noticed there is quite some variance in keyword assignment.

Vim uses a syntax file to highlight colors from the colorscheme

Snip from vim syntax file for python python.vim

"
" Keywords
"

syn keyword pythonStatement     break continue del
syn keyword pythonStatement     exec return
syn keyword pythonStatement     pass raise
syn keyword pythonStatement     global assert
syn keyword pythonStatement     lambda
syn keyword pythonStatement     with
syn keyword pythonStatement     def class nextgroup=pythonFunction skipwhite
syn keyword pythonRepeat        for while
syn keyword pythonConditional   if elif else
syn keyword pythonImport        import
syn keyword pythonException     try except finally
syn keyword pythonOperator      and in is not or



My current result for importing the github colorscheme

How and where are the python keywords assigned to syntax type in blender.  Can they be modified using python?
Providing syntax highlighting in the editor requires parsers for the programming languages in order to identify keywords. These parsers can be found in source/blender/editors/space_text. Currently implement are text_format_lua.c, text_format_osl.c, text_format_pov.c, text_format_pov_ini.c and text_format_py.c. Each of these implementation provides a functions to map strings to internal identifier types which can later be used to give these a distinct color, e.g. in case of the Python:

static void txtfmt_py_format_line(SpaceText *st, TextLine *line, const bool do_next)


Every format implementation provide a function to register the parser as well as the formatter. They are called ED_text_format_register_ followed by the language suffix. Ultimately they all call ED_text_format_register to register the callback functions. The registration using ED_text_format_register_ functions is performed once in source/blender/editors/space_text/space_text.c by calling: 

void ED_spacetype_text(void)


Once this is completed the text can be formatted and syntax highlighted by

void draw_text_main(SpaceText *st, ARegion *ar)


which can be found in source/blender/editors/space_text/text_draw.c. This function retrieves the pointers to formatting callbacks for a given text using tft = ED_text_format_get(text);. The return value can then be used to call the callback with tft-&gt;format_line(st, tmp, false);. In case the current language is Python that would result in a call to txtfmt_py_format_line. It stores identifiers / format character in tmp which can then be used to highlight keywords by one of the drawing functions text_draw, text_draw_wrapped and draw_suggestion_list. Each of them will call the function:

static void format_draw_color(const TextDrawContext *tdc, char formatchar)


This functions contains a switch statement that identifies which format character is given and applies the matching color from the theme. For example if the format character would be FMT_TYPE_KEYWORD it would set the color with UI_FontThemeColor(tdc-&gt;font_id, TH_SYNTAX_B). The parameter TH_SYNTAX_B determines the color.

In source/blender/editors/interface/resource.c is the function that is parsing this color id TH_SYNTAX_B to retrieve the actual color.

const uchar *UI_ThemeGetColorPtr(bTheme *btheme, int spacetype, int colorid)


This function use ts-&gt;syntaxb when the color id is TH_SYNTAX_B where ts is a pointer to a ThemeSpace. 

The place where syntaxb is assigned its value is in release/datafiles/userdef/userdef_default_theme.c. The value is given in .space_text as

.syntaxb = RGBA(0xff1961ff),


For this syntaxb we have a matching RNA property definition in source/blender/makesrna/intern/rna_userdef.c

  prop = RNA_def_property(srna, "syntax_builtin", PROP_FLOAT, PROP_COLOR_GAMMA);
  RNA_def_property_float_sdna(prop, NULL, "syntaxb");
  RNA_def_property_array(prop, 3);
  RNA_def_property_ui_text(prop, "Syntax Built-in", "");
  RNA_def_property_update(prop, 0, "rna_userdef_theme_update"); 


The property syntax_builtin can be set in the Blender theme. For instance release/scripts/presets/interface_theme/blender_light.xml sets syntax_builtin to:

syntax_builtin="#ff1961"