robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
117.4 KiB3256 linesraw
1// highlight_group.c: code for managing highlight groups
2
3#include <assert.h>
4#include <ctype.h>
5#include <stdbool.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include "klib/kvec.h"
12#include "nvim/api/keysets_defs.h"
13#include "nvim/api/private/defs.h"
14#include "nvim/api/private/dispatch.h"
15#include "nvim/api/private/helpers.h"
16#include "nvim/api/private/validate.h"
17#include "nvim/ascii_defs.h"
18#include "nvim/autocmd.h"
19#include "nvim/autocmd_defs.h"
20#include "nvim/buffer_defs.h"
21#include "nvim/charset.h"
22#include "nvim/cmdexpand_defs.h"
23#include "nvim/cursor_shape.h"
24#include "nvim/decoration_provider.h"
25#include "nvim/drawscreen.h"
26#include "nvim/errors.h"
27#include "nvim/eval.h"
28#include "nvim/eval/typval_defs.h"
29#include "nvim/eval/vars.h"
30#include "nvim/ex_docmd.h"
31#include "nvim/garray.h"
32#include "nvim/garray_defs.h"
33#include "nvim/gettext_defs.h"
34#include "nvim/globals.h"
35#include "nvim/grid_defs.h"
36#include "nvim/highlight.h"
37#include "nvim/highlight_group.h"
38#include "nvim/lua/executor.h"
39#include "nvim/macros_defs.h"
40#include "nvim/map_defs.h"
41#include "nvim/memory.h"
42#include "nvim/memory_defs.h"
43#include "nvim/message.h"
44#include "nvim/option.h"
45#include "nvim/option_defs.h"
46#include "nvim/option_vars.h"
47#include "nvim/os/time.h"
48#include "nvim/runtime.h"
49#include "nvim/strings.h"
50#include "nvim/types_defs.h"
51#include "nvim/ui.h"
52#include "nvim/ui_defs.h"
53#include "nvim/vim_defs.h"
54
55/// \addtogroup SG_SET
56/// @{
57enum {
58  SG_CTERM = 2,  // cterm has been set
59  SG_GUI = 4,    // gui has been set
60  SG_LINK = 8,   // link has been set
61};
62/// @}
63
64#define MAX_SYN_NAME 200
65
66// builtin |highlight-groups|
67static garray_T highlight_ga = GA_EMPTY_INIT_VALUE;
68
69// arena for object with same lifetime as highlight_ga (aka hl_table)
70static Arena highlight_arena = ARENA_EMPTY;
71
72static Map(cstr_t, int) highlight_unames = MAP_INIT;
73
74/// The "term", "cterm" and "gui" arguments can be any combination of the
75/// following names, separated by commas (but no spaces!).
76static char *(hl_name_table[]) =
77{ "bold", "standout", "underline",
78  "undercurl", "underdouble", "underdotted", "underdashed",
79  "italic", "reverse", "inverse", "strikethrough", "altfont",
80  "dim", "blink", "conceal", "overline", "nocombine", "NONE" };
81static int hl_attr_table[] =
82{ HL_BOLD, HL_STANDOUT, HL_UNDERLINE,
83  HL_UNDERCURL, HL_UNDERDOUBLE, HL_UNDERDOTTED, HL_UNDERDASHED,
84  HL_ITALIC, HL_INVERSE, HL_INVERSE, HL_STRIKETHROUGH, HL_ALTFONT,
85  HL_DIM, HL_BLINK, HL_CONCEALED, HL_OVERLINE, HL_NOCOMBINE, 0 };
86
87/// Structure that stores information about a highlight group.
88/// The ID of a highlight group is also called group ID.  It is the index in
89/// the highlight_ga array PLUS ONE.
90typedef struct {
91  char *sg_name;                ///< highlight group name
92  char *sg_name_u;              ///< uppercase of sg_name
93  bool sg_cleared;              ///< "hi clear" was used
94  int sg_attr;                  ///< Screen attr @see ATTR_ENTRY
95  int sg_link;                  ///< link to this highlight group ID
96  int sg_deflink;               ///< default link; restored in highlight_clear()
97  int sg_set;                   ///< combination of flags in \ref SG_SET
98  sctx_T sg_deflink_sctx;       ///< script where the default link was set
99  sctx_T sg_script_ctx;         ///< script in which the group was last set for terminal UIs
100  int sg_cterm;                 ///< "cterm=" highlighting attr
101                                ///< (combination of \ref HlAttrFlags)
102  int sg_cterm_fg;              ///< terminal fg color number + 1
103  int sg_cterm_bg;              ///< terminal bg color number + 1
104  bool sg_cterm_bold;           ///< bold attr was set for light color for RGB UIs
105  int sg_gui;                   ///< "gui=" highlighting attributes
106                                ///< (combination of \ref HlAttrFlags)
107  RgbValue sg_rgb_fg;           ///< RGB foreground color
108  RgbValue sg_rgb_bg;           ///< RGB background color
109  RgbValue sg_rgb_sp;           ///< RGB special color
110  int sg_rgb_fg_idx;            ///< RGB foreground color index
111  int sg_rgb_bg_idx;            ///< RGB background color index
112  int sg_rgb_sp_idx;            ///< RGB special color index
113
114  int sg_blend;                 ///< blend level (0-100 inclusive), -1 if unset
115  char *sg_font;                ///< font name, NULL if not set
116
117  int sg_parent;                ///< parent of @nested.group
118} HlGroup;
119
120enum {
121  kColorIdxNone = -1,
122  kColorIdxHex = -2,
123  kColorIdxFg = -3,
124  kColorIdxBg = -4,
125};
126
127#include "highlight_group.c.generated.h"
128
129static const char e_highlight_group_name_not_found_str[]
130  = N_("E411: Highlight group not found: %s");
131static const char e_group_has_settings_highlight_link_ignored[]
132  = N_("E414: Group has settings, highlight link ignored");
133static const char e_unexpected_equal_sign_str[]
134  = N_("E415: Unexpected equal sign: %s");
135static const char e_missing_equal_sign_str_2[]
136  = N_("E416: Missing equal sign: %s");
137static const char e_missing_argument_str[]
138  = N_("E417: Missing argument: %s");
139
140#define hl_table ((HlGroup *)((highlight_ga.ga_data)))
141
142// The default highlight groups.  These are compiled-in for fast startup and
143// they still work when the runtime files can't be found.
144
145static const char *highlight_init_both[] = {
146  "Cursor            guifg=bg      guibg=fg                cterm=reverse",
147  "CursorLineNr      gui=bold      cterm=bold",
148  "Ignore            guifg=bg      ctermfg=0",
149  "PmenuMatch        gui=bold      cterm=bold",
150  "PmenuMatchSel     gui=bold      cterm=bold",
151  "PmenuSel          gui=reverse   cterm=reverse,underline blend=0",
152  "RedrawDebugNormal gui=reverse   cterm=reverse",
153  "TabLineSel        guifg=fg      guibg=bg                gui=bold cterm=nocombine",
154  "TermCursor        gui=reverse   cterm=reverse",
155  "Underlined        gui=underline cterm=underline",
156  "Bold              gui=bold        cterm=bold",
157  "Italic            gui=italic      cterm=italic",
158  "BoldItalic        gui=bold,italic cterm=bold,italic",
159  "lCursor           guifg=bg      guibg=fg                cterm=reverse",
160
161  // UI
162  "default link CursorIM         Cursor",
163  "default link CursorLineFold   FoldColumn",
164  "default link CursorLineSign   SignColumn",
165  "default link DiffTextAdd      DiffText",
166  "default link EndOfBuffer      NonText",
167  "default link FloatBorder      NormalFloat",
168  "default link FloatFooter      FloatTitle",
169  "default link FloatTitle       Title",
170  "default link FoldColumn       SignColumn",
171  "default link IncSearch        CurSearch",
172  "default link LineNrAbove      LineNr",
173  "default link LineNrBelow      LineNr",
174  "default link MsgSeparator     StatusLine",
175  "default link MsgArea          NONE",
176  "default link NormalNC         NONE",
177  "default link PmenuExtra       Pmenu",
178  "default link PmenuExtraSel    PmenuSel",
179  "default link PmenuKind        Pmenu",
180  "default link PmenuKindSel     PmenuSel",
181  "default link PmenuSbar        Pmenu",
182  "default link PmenuBorder        Pmenu",
183  "default link PmenuShadow        FloatShadow",
184  "default link PmenuShadowThrough FloatShadowThrough",
185  "default link PreInsert        Added",
186  "default link ComplMatchIns    NONE",
187  "default link ComplHint        NonText",
188  "default link ComplHintMore    MoreMsg",
189  "default link MCursor          CurSearch",
190  "default link MCursorVisual    Visual",
191  "default link Substitute       Search",
192  "default link StatusLineTerm   StatusLine",
193  "default link StatusLineTermNC StatusLineNC",
194  "default link StderrMsg        ErrorMsg",
195  "default link StdoutMsg        NONE",
196  "default link TabLine          StatusLineNC",
197  "default link TabLineFill      TabLine",
198  "default link VertSplit        WinSeparator",
199  "default link VisualNOS        Visual",
200  "default link Whitespace       NonText",
201  "default link WildMenu         PmenuSel",
202  "default link WinSeparator     Normal",
203
204  // Syntax
205  "default link Character      Constant",
206  "default link Number         Constant",
207  "default link Boolean        Constant",
208  "default link Regexp         Constant",
209  "default link Float          Number",
210  "default link Conditional    Statement",
211  "default link Repeat         Statement",
212  "default link Label          Statement",
213  "default link Keyword        Statement",
214  "default link Exception      Statement",
215  "default link Include        PreProc",
216  "default link Define         PreProc",
217  "default link Macro          PreProc",
218  "default link PreCondit      PreProc",
219  "default link StorageClass   Type",
220  "default link Structure      Type",
221  "default link Typedef        Type",
222  "default link Tag            Special",
223  "default link SpecialChar    Special",
224  "default link SpecialComment Special",
225  "default link Debug          Special",
226  // Used by HLF_8 (very common). None of the HLF_* things use the other Special* groups.
227  "default link SpecialKey     Special",
228  "default link Dimmed         Comment",
229
230  // Built-in LSP
231  "default link LspCodeLens                 NonText",
232  "default link LspCodeLensSeparator        LspCodeLens",
233  "default link LspInlayHint                NonText",
234  "default link LspReferenceRead            LspReferenceText",
235  "default link LspReferenceText            Visual",
236  "default link LspReferenceWrite           LspReferenceText",
237  "default link LspReferenceTarget          LspReferenceText",
238  "default link LspSignatureActiveParameter Visual",
239  "default link SnippetTabstop              Visual",
240  "default link SnippetTabstopActive        SnippetTabstop",
241
242  // Diagnostic
243  "default link DiagnosticFloatingError    DiagnosticError",
244  "default link DiagnosticFloatingWarn     DiagnosticWarn",
245  "default link DiagnosticFloatingInfo     DiagnosticInfo",
246  "default link DiagnosticFloatingHint     DiagnosticHint",
247  "default link DiagnosticFloatingOk       DiagnosticOk",
248  "default link DiagnosticVirtualTextError DiagnosticError",
249  "default link DiagnosticVirtualTextWarn  DiagnosticWarn",
250  "default link DiagnosticVirtualTextInfo  DiagnosticInfo",
251  "default link DiagnosticVirtualTextHint  DiagnosticHint",
252  "default link DiagnosticVirtualTextOk    DiagnosticOk",
253  "default link DiagnosticVirtualLinesError DiagnosticError",
254  "default link DiagnosticVirtualLinesWarn  DiagnosticWarn",
255  "default link DiagnosticVirtualLinesInfo  DiagnosticInfo",
256  "default link DiagnosticVirtualLinesHint  DiagnosticHint",
257  "default link DiagnosticVirtualLinesOk    DiagnosticOk",
258  "default link DiagnosticSignError        DiagnosticError",
259  "default link DiagnosticSignWarn         DiagnosticWarn",
260  "default link DiagnosticSignInfo         DiagnosticInfo",
261  "default link DiagnosticSignHint         DiagnosticHint",
262  "default link DiagnosticSignOk           DiagnosticOk",
263  "default link DiagnosticUnnecessary      Comment",
264
265  // Treesitter standard groups
266  "default link @variable.builtin           Special",
267  "default link @variable.parameter.builtin Special",
268
269  "default link @constant         Constant",
270  "default link @constant.builtin Special",
271
272  "default link @module         Structure",
273  "default link @module.builtin Special",
274  "default link @label          Label",
275
276  "default link @string             String",
277  "default link @string.regexp      @string.special",
278  "default link @string.escape      @string.special",
279  "default link @string.special     SpecialChar",
280  "default link @string.special.url Underlined",
281
282  "default link @character         Character",
283  "default link @character.special SpecialChar",
284
285  "default link @boolean      Boolean",
286  "default link @number       Number",
287  "default link @number.float Float",
288
289  "default link @type         Type",
290  "default link @type.builtin Special",
291
292  "default link @attribute         Macro",
293  "default link @attribute.builtin Special",
294  "default link @property          Identifier",
295
296  "default link @function         Function",
297  "default link @function.builtin Special",
298
299  "default link @constructor Special",
300  "default link @operator    Operator",
301
302  "default link @keyword Keyword",
303
304  "default link @punctuation         Delimiter",  // fallback for subgroups; never used itself
305  "default link @punctuation.special Special",
306
307  "default link @comment Comment",
308
309  "default link @comment.error   DiagnosticError",
310  "default link @comment.warning DiagnosticWarn",
311  "default link @comment.note    DiagnosticInfo",
312  "default link @comment.todo    Todo",
313
314  "@markup.strong        gui=bold          cterm=bold",
315  "@markup.italic        gui=italic        cterm=italic",
316  "@markup.strikethrough gui=strikethrough cterm=strikethrough",
317  "@markup.underline     gui=underline     cterm=underline",
318
319  "default link @markup         Special",  // fallback for subgroups; never used itself
320  "default link @markup.heading Title",
321  "default link @markup.link    Underlined",
322
323  "default link @diff.plus  Added",
324  "default link @diff.minus Removed",
325  "default link @diff.delta Changed",
326
327  "default link @tag         Tag",
328  "default link @tag.builtin Special",
329
330  // :help
331  // Highlight "===" and "---" heading delimiters specially.
332  "default @markup.heading.1.delimiter.vimdoc guibg=bg guifg=bg guisp=fg gui=underdouble,nocombine ctermbg=NONE ctermfg=NONE cterm=underdouble,nocombine",
333  "default @markup.heading.2.delimiter.vimdoc guibg=bg guifg=bg guisp=fg gui=underline,nocombine ctermbg=NONE ctermfg=NONE cterm=underline,nocombine",
334
335  // LSP semantic tokens
336  "default link @lsp.type.class         @type",
337  "default link @lsp.type.comment       @comment",
338  "default link @lsp.type.decorator     @attribute",
339  "default link @lsp.type.enum          @type",
340  "default link @lsp.type.enumMember    @constant",
341  "default link @lsp.type.event         @type",
342  "default link @lsp.type.function      @function",
343  "default link @lsp.type.interface     @type",
344  "default link @lsp.type.keyword       @keyword",
345  "default link @lsp.type.macro         @constant.macro",
346  "default link @lsp.type.method        @function.method",
347  "default link @lsp.type.modifier      @type.qualifier",
348  "default link @lsp.type.namespace     @module",
349  "default link @lsp.type.number        @number",
350  "default link @lsp.type.operator      @operator",
351  "default link @lsp.type.parameter     @variable.parameter",
352  "default link @lsp.type.property      @property",
353  "default link @lsp.type.regexp        @string.regexp",
354  "default link @lsp.type.string        @string",
355  "default link @lsp.type.struct        @type",
356  "default link @lsp.type.type          @type",
357  "default link @lsp.type.typeParameter @type.definition",
358  "default link @lsp.type.variable      @variable",
359
360  "default link @lsp.mod.deprecated DiagnosticDeprecated",
361
362  NULL
363};
364
365// Default colors only used with a light background.
366static const char *highlight_init_light[] = {
367  "Normal guifg=NvimDarkGrey2 guibg=NvimLightGrey2 ctermfg=NONE ctermbg=NONE",
368
369  // UI
370  "Added                guifg=NvimDarkGreen                                  ctermfg=2",
371  "Changed              guifg=NvimDarkCyan                                   ctermfg=6",
372  "ColorColumn                               guibg=NvimLightGrey4            cterm=reverse",
373  "Conceal              guifg=NvimLightGrey4",
374  "CurSearch            guifg=NvimLightGrey1 guibg=NvimDarkYellow            ctermfg=15 ctermbg=3",
375  "CursorColumn                              guibg=NvimLightGrey3",
376  "CursorLine                                guibg=NvimLightGrey3",
377  "DiffAdd              guifg=NvimDarkGrey1  guibg=NvimLightGreen            ctermfg=15 ctermbg=2",
378  "DiffChange           guifg=NvimDarkGrey1  guibg=NvimLightGrey4",
379  "DiffDelete           guifg=NvimDarkRed                          gui=bold  ctermfg=1 cterm=bold",
380  "DiffText             guifg=NvimDarkGrey1  guibg=NvimLightCyan             ctermfg=15 ctermbg=6",
381  "Directory            guifg=NvimDarkCyan                                   ctermfg=6",
382  "ErrorMsg             guifg=NvimDarkRed                                    ctermfg=1",
383  "FloatShadow                               guibg=NvimLightGrey4            ctermbg=0 blend=80",
384  "FloatShadowThrough                        guibg=NvimLightGrey4            ctermbg=0 blend=100",
385  "Folded               guifg=NvimDarkGrey4  guibg=NvimLightGrey1",
386  "LineNr               guifg=NvimLightGrey4",
387  "MatchParen                                guibg=NvimLightGrey4  gui=bold  cterm=bold,underline",
388  "ModeMsg              guifg=NvimDarkGreen                                  ctermfg=2",
389  "MoreMsg              guifg=NvimDarkCyan                                   ctermfg=6",
390  "NonText              guifg=NvimLightGrey4",
391  "NormalFloat                               guibg=NvimLightGrey1",
392  "OkMsg                guifg=NvimDarkGreen                                  ctermfg=2",
393  "Pmenu                                     guibg=NvimLightGrey3            cterm=reverse",
394  "PmenuThumb                                guibg=NvimLightGrey4",
395  "Question             guifg=NvimDarkCyan                                   ctermfg=6",
396  "QuickFixLine         guifg=NvimDarkCyan                                   ctermfg=6",
397  "RedrawDebugClear                          guibg=NvimLightYellow           ctermfg=15 ctermbg=3",
398  "RedrawDebugComposed                       guibg=NvimLightGreen            ctermfg=15 ctermbg=2",
399  "RedrawDebugRecompose                      guibg=NvimLightRed              ctermfg=15 ctermbg=1",
400  "Removed              guifg=NvimDarkRed                                    ctermfg=1",
401  "Search               guifg=NvimDarkGrey1  guibg=NvimLightYellow           ctermfg=15 ctermbg=3",
402  "SignColumn           guifg=NvimLightGrey4",
403  "SpellBad             guisp=NvimDarkRed    gui=undercurl                   cterm=undercurl",
404  "SpellCap             guisp=NvimDarkYellow gui=undercurl                   cterm=undercurl",
405  "SpellLocal           guisp=NvimDarkGreen  gui=undercurl                   cterm=undercurl",
406  "SpellRare            guisp=NvimDarkCyan   gui=undercurl                   cterm=undercurl",
407  "StatusLine           guifg=NvimDarkGrey2  guibg=NvimLightGrey4            cterm=reverse",
408  "StatusLineNC         guifg=NvimDarkGrey3  guibg=NvimLightGrey3            cterm=bold,underline",
409  "Title                guifg=NvimDarkGrey2                        gui=bold  cterm=bold",
410  "Visual                                    guibg=NvimLightGrey4            ctermfg=15 ctermbg=0",
411  "WarningMsg           guifg=NvimDarkYellow                                 ctermfg=3",
412  "WinBar               guifg=NvimDarkGrey4  guibg=NvimLightGrey1  gui=bold  cterm=bold",
413  "WinBarNC             guifg=NvimDarkGrey4  guibg=NvimLightGrey1            cterm=bold",
414
415  // Syntax
416  "Constant   guifg=NvimDarkGrey2",  // Use only `Normal` foreground to be usable on different background
417  "Operator   guifg=NvimDarkGrey2",
418  "PreProc    guifg=NvimDarkGrey2",
419  "Type       guifg=NvimDarkGrey2",
420  "Delimiter  guifg=NvimDarkGrey2",
421
422  "Comment    guifg=NvimDarkGrey4",
423  "String     guifg=NvimDarkGreen                    ctermfg=2",
424  "Identifier guifg=NvimDarkBlue                     ctermfg=4",
425  "Function   guifg=NvimDarkCyan                     ctermfg=6",
426  "Statement  guifg=NvimDarkGrey2 gui=bold           cterm=bold",
427  "Special    guifg=NvimDarkCyan                     ctermfg=6",
428  "Error      guifg=NvimDarkGrey1 guibg=NvimLightRed ctermfg=15 ctermbg=1",
429  "Todo       guifg=NvimDarkGrey2 gui=bold           cterm=bold",
430
431  // Diagnostic
432  "DiagnosticError          guifg=NvimDarkRed                      ctermfg=1",
433  "DiagnosticWarn           guifg=NvimDarkYellow                   ctermfg=3",
434  "DiagnosticInfo           guifg=NvimDarkCyan                     ctermfg=6",
435  "DiagnosticHint           guifg=NvimDarkBlue                     ctermfg=4",
436  "DiagnosticOk             guifg=NvimDarkGreen                    ctermfg=2",
437  "DiagnosticUnderlineError guisp=NvimDarkRed    gui=underline     cterm=underline",
438  "DiagnosticUnderlineWarn  guisp=NvimDarkYellow gui=underline     cterm=underline",
439  "DiagnosticUnderlineInfo  guisp=NvimDarkCyan   gui=underline     cterm=underline",
440  "DiagnosticUnderlineHint  guisp=NvimDarkBlue   gui=underline     cterm=underline",
441  "DiagnosticUnderlineOk    guisp=NvimDarkGreen  gui=underline     cterm=underline",
442  "DiagnosticDeprecated     guisp=NvimDarkRed    gui=strikethrough cterm=strikethrough",
443
444  // Treesitter standard groups
445  "@variable guifg=NvimDarkGrey2",
446  NULL
447};
448
449// Default colors only used with a dark background.
450static const char *highlight_init_dark[] = {
451  "Normal guifg=NvimLightGrey2 guibg=NvimDarkGrey2 ctermfg=NONE ctermbg=NONE",
452
453  // UI
454  "Added                guifg=NvimLightGreen                                ctermfg=10",
455  "Changed              guifg=NvimLightCyan                                 ctermfg=14",
456  "ColorColumn                                guibg=NvimDarkGrey4           cterm=reverse",
457  "Conceal              guifg=NvimDarkGrey4",
458  "CurSearch            guifg=NvimDarkGrey1   guibg=NvimLightYellow         ctermfg=0 ctermbg=11",
459  "CursorColumn                               guibg=NvimDarkGrey3",
460  "CursorLine                                 guibg=NvimDarkGrey3",
461  "DiffAdd              guifg=NvimLightGrey1  guibg=NvimDarkGreen           ctermfg=0 ctermbg=10",
462  "DiffChange           guifg=NvimLightGrey1  guibg=NvimDarkGrey4",
463  "DiffDelete           guifg=NvimLightRed                         gui=bold ctermfg=9 cterm=bold",
464  "DiffText             guifg=NvimLightGrey1  guibg=NvimDarkCyan            ctermfg=0 ctermbg=14",
465  "Directory            guifg=NvimLightCyan                                 ctermfg=14",
466  "ErrorMsg             guifg=NvimLightRed                                  ctermfg=9",
467  "FloatShadow                                guibg=NvimDarkGrey4           ctermbg=0 blend=80",
468  "FloatShadowThrough                         guibg=NvimDarkGrey4           ctermbg=0 blend=100",
469  "Folded               guifg=NvimLightGrey4  guibg=NvimDarkGrey1",
470  "LineNr               guifg=NvimDarkGrey4",
471  "MatchParen                                 guibg=NvimDarkGrey4  gui=bold cterm=bold,underline",
472  "ModeMsg              guifg=NvimLightGreen                                ctermfg=10",
473  "MoreMsg              guifg=NvimLightCyan                                 ctermfg=14",
474  "NonText              guifg=NvimDarkGrey4",
475  "NormalFloat                                guibg=NvimDarkGrey1",
476  "OkMsg                guifg=NvimLightGreen                                ctermfg=10",
477  "Pmenu                                      guibg=NvimDarkGrey3           cterm=reverse",
478  "PmenuThumb                                 guibg=NvimDarkGrey4",
479  "Question             guifg=NvimLightCyan                                 ctermfg=14",
480  "QuickFixLine         guifg=NvimLightCyan                                 ctermfg=14",
481  "RedrawDebugClear                           guibg=NvimDarkYellow          ctermfg=0 ctermbg=11",
482  "RedrawDebugComposed                        guibg=NvimDarkGreen           ctermfg=0 ctermbg=10",
483  "RedrawDebugRecompose                       guibg=NvimDarkRed             ctermfg=0 ctermbg=9",
484  "Removed              guifg=NvimLightRed                                  ctermfg=9",
485  "Search               guifg=NvimLightGrey1  guibg=NvimDarkYellow          ctermfg=0 ctermbg=11",
486  "SignColumn           guifg=NvimDarkGrey4",
487  "SpellBad             guisp=NvimLightRed    gui=undercurl                 cterm=undercurl",
488  "SpellCap             guisp=NvimLightYellow gui=undercurl                 cterm=undercurl",
489  "SpellLocal           guisp=NvimLightGreen  gui=undercurl                 cterm=undercurl",
490  "SpellRare            guisp=NvimLightCyan   gui=undercurl                 cterm=undercurl",
491  "StatusLine           guifg=NvimLightGrey2  guibg=NvimDarkGrey4           cterm=reverse",
492  "StatusLineNC         guifg=NvimLightGrey3  guibg=NvimDarkGrey3           cterm=bold,underline",
493  "Title                guifg=NvimLightGrey2                       gui=bold cterm=bold",
494  "Visual                                     guibg=NvimDarkGrey4           ctermfg=0 ctermbg=15",
495  "WarningMsg           guifg=NvimLightYellow                               ctermfg=11",
496  "WinBar               guifg=NvimLightGrey4  guibg=NvimDarkGrey1  gui=bold cterm=bold",
497  "WinBarNC             guifg=NvimLightGrey4  guibg=NvimDarkGrey1           cterm=bold",
498
499  // Syntax
500  "Constant   guifg=NvimLightGrey2",  // Use only `Normal` foreground to be usable on different background
501  "Operator   guifg=NvimLightGrey2",
502  "PreProc    guifg=NvimLightGrey2",
503  "Type       guifg=NvimLightGrey2",
504  "Delimiter  guifg=NvimLightGrey2",
505
506  "Comment    guifg=NvimLightGrey4",
507  "String     guifg=NvimLightGreen                   ctermfg=10",
508  "Identifier guifg=NvimLightBlue                    ctermfg=12",
509  "Function   guifg=NvimLightCyan                    ctermfg=14",
510  "Statement  guifg=NvimLightGrey2 gui=bold          cterm=bold",
511  "Special    guifg=NvimLightCyan                    ctermfg=14",
512  "Error      guifg=NvimLightGrey1 guibg=NvimDarkRed ctermfg=0 ctermbg=9",
513  "Todo       guifg=NvimLightGrey2 gui=bold          cterm=bold",
514
515  // Diagnostic
516  "DiagnosticError          guifg=NvimLightRed                      ctermfg=9",
517  "DiagnosticWarn           guifg=NvimLightYellow                   ctermfg=11",
518  "DiagnosticInfo           guifg=NvimLightCyan                     ctermfg=14",
519  "DiagnosticHint           guifg=NvimLightBlue                     ctermfg=12",
520  "DiagnosticOk             guifg=NvimLightGreen                    ctermfg=10",
521  "DiagnosticUnderlineError guisp=NvimLightRed    gui=underline     cterm=underline",
522  "DiagnosticUnderlineWarn  guisp=NvimLightYellow gui=underline     cterm=underline",
523  "DiagnosticUnderlineInfo  guisp=NvimLightCyan   gui=underline     cterm=underline",
524  "DiagnosticUnderlineHint  guisp=NvimLightBlue   gui=underline     cterm=underline",
525  "DiagnosticUnderlineOk    guisp=NvimLightGreen  gui=underline     cterm=underline",
526  "DiagnosticDeprecated     guisp=NvimLightRed    gui=strikethrough cterm=strikethrough",
527
528  // Treesitter standard groups
529  "@variable guifg=NvimLightGrey2",
530  NULL
531};
532
533// NOLINTNEXTLINE(misc-use-internal-linkage): used with FFI in unittests
534const char *const highlight_init_cmdline[] = {
535  // XXX When modifying a list modify it in both valid and invalid halves.
536  // TODO(ZyX-I): merge valid and invalid groups via a macros.
537
538  // NvimInternalError should appear only when highlighter has a bug.
539  "NvimInternalError ctermfg=Red ctermbg=Red guifg=Red guibg=Red",
540
541  // Highlight groups (links) used by parser:
542
543  "default link NvimAssignment Operator",
544  "default link NvimPlainAssignment NvimAssignment",
545  "default link NvimAugmentedAssignment NvimAssignment",
546  "default link NvimAssignmentWithAddition NvimAugmentedAssignment",
547  "default link NvimAssignmentWithSubtraction NvimAugmentedAssignment",
548  "default link NvimAssignmentWithConcatenation NvimAugmentedAssignment",
549
550  "default link NvimOperator Operator",
551
552  "default link NvimUnaryOperator NvimOperator",
553  "default link NvimUnaryPlus NvimUnaryOperator",
554  "default link NvimUnaryMinus NvimUnaryOperator",
555  "default link NvimNot NvimUnaryOperator",
556
557  "default link NvimBinaryOperator NvimOperator",
558  "default link NvimComparison NvimBinaryOperator",
559  "default link NvimComparisonModifier NvimComparison",
560  "default link NvimBinaryPlus NvimBinaryOperator",
561  "default link NvimBinaryMinus NvimBinaryOperator",
562  "default link NvimConcat NvimBinaryOperator",
563  "default link NvimConcatOrSubscript NvimConcat",
564  "default link NvimOr NvimBinaryOperator",
565  "default link NvimAnd NvimBinaryOperator",
566  "default link NvimMultiplication NvimBinaryOperator",
567  "default link NvimDivision NvimBinaryOperator",
568  "default link NvimMod NvimBinaryOperator",
569
570  "default link NvimTernary NvimOperator",
571  "default link NvimTernaryColon NvimTernary",
572
573  "default link NvimParenthesis Delimiter",
574  "default link NvimLambda NvimParenthesis",
575  "default link NvimNestingParenthesis NvimParenthesis",
576  "default link NvimCallingParenthesis NvimParenthesis",
577
578  "default link NvimSubscript NvimParenthesis",
579  "default link NvimSubscriptBracket NvimSubscript",
580  "default link NvimSubscriptColon NvimSubscript",
581  "default link NvimCurly NvimSubscript",
582
583  "default link NvimContainer NvimParenthesis",
584  "default link NvimDict NvimContainer",
585  "default link NvimList NvimContainer",
586
587  "default link NvimIdentifier Identifier",
588  "default link NvimIdentifierScope NvimIdentifier",
589  "default link NvimIdentifierScopeDelimiter NvimIdentifier",
590  "default link NvimIdentifierName NvimIdentifier",
591  "default link NvimIdentifierKey NvimIdentifier",
592
593  "default link NvimColon Delimiter",
594  "default link NvimComma Delimiter",
595  "default link NvimArrow Delimiter",
596
597  "default link NvimRegister SpecialChar",
598  "default link NvimNumber Number",
599  "default link NvimFloat NvimNumber",
600  "default link NvimNumberPrefix Type",
601
602  "default link NvimOptionSigil Type",
603  "default link NvimOptionName NvimIdentifier",
604  "default link NvimOptionScope NvimIdentifierScope",
605  "default link NvimOptionScopeDelimiter NvimIdentifierScopeDelimiter",
606
607  "default link NvimEnvironmentSigil NvimOptionSigil",
608  "default link NvimEnvironmentName NvimIdentifier",
609
610  "default link NvimString String",
611  "default link NvimStringBody NvimString",
612  "default link NvimStringQuote NvimString",
613  "default link NvimStringSpecial SpecialChar",
614
615  "default link NvimSingleQuote NvimStringQuote",
616  "default link NvimSingleQuotedBody NvimStringBody",
617  "default link NvimSingleQuotedQuote NvimStringSpecial",
618
619  "default link NvimDoubleQuote NvimStringQuote",
620  "default link NvimDoubleQuotedBody NvimStringBody",
621  "default link NvimDoubleQuotedEscape NvimStringSpecial",
622
623  "default link NvimFigureBrace NvimInternalError",
624  "default link NvimSingleQuotedUnknownEscape NvimInternalError",
625
626  "default link NvimSpacing Normal",
627
628  // NvimInvalid groups:
629
630  "default link NvimInvalidSingleQuotedUnknownEscape NvimInternalError",
631
632  "default link NvimInvalid Error",
633
634  "default link NvimInvalidAssignment NvimInvalid",
635  "default link NvimInvalidPlainAssignment NvimInvalidAssignment",
636  "default link NvimInvalidAugmentedAssignment NvimInvalidAssignment",
637  "default link NvimInvalidAssignmentWithAddition NvimInvalidAugmentedAssignment",
638  "default link NvimInvalidAssignmentWithSubtraction NvimInvalidAugmentedAssignment",
639  "default link NvimInvalidAssignmentWithConcatenation NvimInvalidAugmentedAssignment",
640
641  "default link NvimInvalidOperator NvimInvalid",
642
643  "default link NvimInvalidUnaryOperator NvimInvalidOperator",
644  "default link NvimInvalidUnaryPlus NvimInvalidUnaryOperator",
645  "default link NvimInvalidUnaryMinus NvimInvalidUnaryOperator",
646  "default link NvimInvalidNot NvimInvalidUnaryOperator",
647
648  "default link NvimInvalidBinaryOperator NvimInvalidOperator",
649  "default link NvimInvalidComparison NvimInvalidBinaryOperator",
650  "default link NvimInvalidComparisonModifier NvimInvalidComparison",
651  "default link NvimInvalidBinaryPlus NvimInvalidBinaryOperator",
652  "default link NvimInvalidBinaryMinus NvimInvalidBinaryOperator",
653  "default link NvimInvalidConcat NvimInvalidBinaryOperator",
654  "default link NvimInvalidConcatOrSubscript NvimInvalidConcat",
655  "default link NvimInvalidOr NvimInvalidBinaryOperator",
656  "default link NvimInvalidAnd NvimInvalidBinaryOperator",
657  "default link NvimInvalidMultiplication NvimInvalidBinaryOperator",
658  "default link NvimInvalidDivision NvimInvalidBinaryOperator",
659  "default link NvimInvalidMod NvimInvalidBinaryOperator",
660
661  "default link NvimInvalidTernary NvimInvalidOperator",
662  "default link NvimInvalidTernaryColon NvimInvalidTernary",
663
664  "default link NvimInvalidDelimiter NvimInvalid",
665
666  "default link NvimInvalidParenthesis NvimInvalidDelimiter",
667  "default link NvimInvalidLambda NvimInvalidParenthesis",
668  "default link NvimInvalidNestingParenthesis NvimInvalidParenthesis",
669  "default link NvimInvalidCallingParenthesis NvimInvalidParenthesis",
670
671  "default link NvimInvalidSubscript NvimInvalidParenthesis",
672  "default link NvimInvalidSubscriptBracket NvimInvalidSubscript",
673  "default link NvimInvalidSubscriptColon NvimInvalidSubscript",
674  "default link NvimInvalidCurly NvimInvalidSubscript",
675
676  "default link NvimInvalidContainer NvimInvalidParenthesis",
677  "default link NvimInvalidDict NvimInvalidContainer",
678  "default link NvimInvalidList NvimInvalidContainer",
679
680  "default link NvimInvalidValue NvimInvalid",
681
682  "default link NvimInvalidIdentifier NvimInvalidValue",
683  "default link NvimInvalidIdentifierScope NvimInvalidIdentifier",
684  "default link NvimInvalidIdentifierScopeDelimiter NvimInvalidIdentifier",
685  "default link NvimInvalidIdentifierName NvimInvalidIdentifier",
686  "default link NvimInvalidIdentifierKey NvimInvalidIdentifier",
687
688  "default link NvimInvalidColon NvimInvalidDelimiter",
689  "default link NvimInvalidComma NvimInvalidDelimiter",
690  "default link NvimInvalidArrow NvimInvalidDelimiter",
691
692  "default link NvimInvalidRegister NvimInvalidValue",
693  "default link NvimInvalidNumber NvimInvalidValue",
694  "default link NvimInvalidFloat NvimInvalidNumber",
695  "default link NvimInvalidNumberPrefix NvimInvalidNumber",
696
697  "default link NvimInvalidOptionSigil NvimInvalidIdentifier",
698  "default link NvimInvalidOptionName NvimInvalidIdentifier",
699  "default link NvimInvalidOptionScope NvimInvalidIdentifierScope",
700  "default link NvimInvalidOptionScopeDelimiter NvimInvalidIdentifierScopeDelimiter",
701
702  "default link NvimInvalidEnvironmentSigil NvimInvalidOptionSigil",
703  "default link NvimInvalidEnvironmentName NvimInvalidIdentifier",
704
705  // Invalid string bodies and specials are still highlighted as valid ones to
706  // minimize the red area.
707  "default link NvimInvalidString NvimInvalidValue",
708  "default link NvimInvalidStringBody NvimStringBody",
709  "default link NvimInvalidStringQuote NvimInvalidString",
710  "default link NvimInvalidStringSpecial NvimStringSpecial",
711
712  "default link NvimInvalidSingleQuote NvimInvalidStringQuote",
713  "default link NvimInvalidSingleQuotedBody NvimInvalidStringBody",
714  "default link NvimInvalidSingleQuotedQuote NvimInvalidStringSpecial",
715
716  "default link NvimInvalidDoubleQuote NvimInvalidStringQuote",
717  "default link NvimInvalidDoubleQuotedBody NvimInvalidStringBody",
718  "default link NvimInvalidDoubleQuotedEscape NvimInvalidStringSpecial",
719  "default link NvimInvalidDoubleQuotedUnknownEscape NvimInvalidValue",
720
721  "default link NvimInvalidFigureBrace NvimInvalidDelimiter",
722
723  "default link NvimInvalidSpacing ErrorMsg",
724
725  // Not actually invalid, but we show the user that they are doing something
726  // wrong.
727  "default link NvimDoubleQuotedUnknownEscape NvimInvalidValue",
728  NULL,
729};
730
731/// Returns the number of highlight groups.
732int highlight_num_groups(void)
733{
734  return highlight_ga.ga_len;
735}
736
737/// Returns the name of a highlight group.
738char *highlight_group_name(int id)
739{
740  return hl_table[id].sg_name;
741}
742
743/// Returns the ID of the link to a highlight group.
744int highlight_link_id(int id)
745{
746  return hl_table[id].sg_link;
747}
748
749/// Create default links for Nvim* highlight groups used for cmdline coloring
750void syn_init_cmdline_highlight(bool reset, bool init)
751{
752  for (size_t i = 0; highlight_init_cmdline[i] != NULL; i++) {
753    do_highlight(highlight_init_cmdline[i], reset, init);
754  }
755}
756
757/// Load colors from a file if "g:colors_name" is set, otherwise load builtin
758/// colors
759///
760/// @param both include groups where 'bg' doesn't matter
761/// @param reset clear groups first
762void init_highlight(bool both, bool reset)
763{
764  static bool had_both = false;
765
766  // Try finding the color scheme file.  Used when a color file was loaded
767  // and 'background' or 't_Co' is changed.
768  char *p = get_var_value("g:colors_name");
769  if (p != NULL) {
770    // Value of g:colors_name could be freed in load_colors() and make
771    // p invalid, so copy it.
772    char *copy_p = xstrdup(p);
773    bool okay = load_colors(copy_p);
774    xfree(copy_p);
775    if (okay) {
776      return;
777    }
778  }
779
780  // Didn't use a color file, use the compiled-in colors.
781  if (both) {
782    had_both = true;
783    const char *const *const pp = highlight_init_both;
784    for (size_t i = 0; pp[i] != NULL; i++) {
785      do_highlight(pp[i], reset, true);
786    }
787  } else if (!had_both) {
788    // Don't do anything before the call with both == true from main().
789    // Not everything has been setup then, and that call will overrule
790    // everything anyway.
791    return;
792  }
793
794  const char *const *const pp = ((*p_bg == 'l')
795                                 ? highlight_init_light
796                                 : highlight_init_dark);
797  for (size_t i = 0; pp[i] != NULL; i++) {
798    do_highlight(pp[i], reset, true);
799  }
800
801  syn_init_cmdline_highlight(false, false);
802}
803
804/// Load color file "name".
805///
806/// @return  OK for success, FAIL for failure.
807int load_colors(char *name)
808{
809  static bool recursive = false;
810
811  // When being called recursively, this is probably because setting
812  // 'background' caused the highlighting to be reloaded.  This means it is
813  // working, thus we should return OK.
814  if (recursive) {
815    return OK;
816  }
817
818  recursive = true;
819  size_t buflen = strlen(name) + 12;
820  char *buf = xmalloc(buflen);
821  apply_autocmds(EVENT_COLORSCHEMEPRE, name, curbuf->b_fname, false, curbuf);
822  snprintf(buf, buflen, "colors/%s.*", name);
823  int retval = source_runtime_vim_lua(buf, DIP_START + DIP_OPT);
824  xfree(buf);
825  if (retval == OK) {
826    apply_autocmds(EVENT_COLORSCHEME, name, curbuf->b_fname, false, curbuf);
827  }
828
829  recursive = false;
830
831  return retval;
832}
833
834static char *(color_names[28]) = {
835  "Black", "DarkBlue", "DarkGreen", "DarkCyan",
836  "DarkRed", "DarkMagenta", "Brown", "DarkYellow",
837  "Gray", "Grey", "LightGray", "LightGrey",
838  "DarkGray", "DarkGrey",
839  "Blue", "LightBlue", "Green", "LightGreen",
840  "Cyan", "LightCyan", "Red", "LightRed", "Magenta",
841  "LightMagenta", "Yellow", "LightYellow", "White", "NONE"
842};
843// indices:
844// 0, 1, 2, 3,
845// 4, 5, 6, 7,
846// 8, 9, 10, 11,
847// 12, 13,
848// 14, 15, 16, 17,
849// 18, 19, 20, 21, 22,
850// 23, 24, 25, 26, 27
851static int color_numbers_16[28] = { 0, 1, 2, 3,
852                                    4, 5, 6, 6,
853                                    7, 7, 7, 7,
854                                    8, 8,
855                                    9, 9, 10, 10,
856                                    11, 11, 12, 12, 13,
857                                    13, 14, 14, 15, -1 };
858// for xterm with 88 colors...
859static int color_numbers_88[28] = { 0, 4, 2, 6,
860                                    1, 5, 32, 72,
861                                    84, 84, 7, 7,
862                                    82, 82,
863                                    12, 43, 10, 61,
864                                    14, 63, 9, 74, 13,
865                                    75, 11, 78, 15, -1 };
866// for xterm with 256 colors...
867static int color_numbers_256[28] = { 0, 4, 2, 6,
868                                     1, 5, 130, 3,
869                                     248, 248, 7, 7,
870                                     242, 242,
871                                     12, 81, 10, 121,
872                                     14, 159, 9, 224, 13,
873                                     225, 11, 229, 15, -1 };
874// for terminals with less than 16 colors...
875static int color_numbers_8[28] = { 0, 4, 2, 6,
876                                   1, 5, 3, 3,
877                                   7, 7, 7, 7,
878                                   0 + 8, 0 + 8,
879                                   4 + 8, 4 + 8, 2 + 8, 2 + 8,
880                                   6 + 8, 6 + 8, 1 + 8, 1 + 8, 5 + 8,
881                                   5 + 8, 3 + 8, 3 + 8, 7 + 8, -1 };
882
883// Lookup the "cterm" value to be used for color with index "idx" in
884// color_names[].
885// "boldp" will be set to kTrue or kFalse for a foreground color when using 8
886// colors, otherwise it will be unchanged.
887static int lookup_color(const int idx, const bool foreground, TriState *const boldp)
888{
889  int color = color_numbers_16[idx];
890
891  // Use the _16 table to check if it's a valid color name.
892  if (color < 0) {
893    return -1;
894  }
895
896  if (t_colors == 8) {
897    // t_Co is 8: use the 8 colors table
898    color = color_numbers_8[idx];
899    if (foreground) {
900      // set/reset bold attribute to get light foreground
901      // colors (on some terminals, e.g. "linux")
902      if (color & 8) {
903        *boldp = kTrue;
904      } else {
905        *boldp = kFalse;
906      }
907    }
908    color &= 7;   // truncate to 8 colors
909  } else if (t_colors == 16) {
910    color = color_numbers_8[idx];
911  } else if (t_colors == 88) {
912    color = color_numbers_88[idx];
913  } else if (t_colors >= 256) {
914    color = color_numbers_256[idx];
915  }
916  return color;
917}
918
919void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id)
920{
921  int idx = id - 1;  // Index is ID minus one.
922  bool is_default = attrs.rgb_ae_attr & HL_DEFAULT;
923
924  // Return if "default" was used and the group already has settings
925  if (is_default && hl_has_settings(idx, true) && !dict->force) {
926    return;
927  }
928
929  HlGroup *g = &hl_table[idx];
930  g->sg_cleared = false;
931  int old_link = g->sg_link;
932
933  if (link_id > 0) {
934    g->sg_link = link_id;
935    g->sg_script_ctx = current_sctx;
936    g->sg_script_ctx.sc_lnum += SOURCING_LNUM;
937    nlua_set_sctx(&g->sg_script_ctx);
938    g->sg_set |= SG_LINK;
939    if (is_default) {
940      g->sg_deflink = link_id;
941      g->sg_deflink_sctx = current_sctx;
942      g->sg_deflink_sctx.sc_lnum += SOURCING_LNUM;
943      nlua_set_sctx(&g->sg_deflink_sctx);
944    }
945  } else {
946    g->sg_link = 0;
947  }
948
949  bool update = HAS_KEY(dict, highlight, update) && dict->update;
950  g->sg_gui = attrs.rgb_ae_attr &~HL_DEFAULT;
951
952  g->sg_rgb_fg = attrs.rgb_fg_color;
953  g->sg_rgb_bg = attrs.rgb_bg_color;
954  g->sg_rgb_sp = attrs.rgb_sp_color;
955
956  struct {
957    int *dest; RgbValue val; Object name;
958  } cattrs[] = {
959    { &g->sg_rgb_fg_idx, g->sg_rgb_fg,
960      HAS_KEY(dict, highlight, fg) ? dict->fg : dict->foreground },
961    { &g->sg_rgb_bg_idx, g->sg_rgb_bg,
962      HAS_KEY(dict, highlight, bg) ? dict->bg : dict->background },
963    { &g->sg_rgb_sp_idx, g->sg_rgb_sp, HAS_KEY(dict, highlight, sp) ? dict->sp : dict->special },
964    { NULL, -1, NIL },
965  };
966
967  for (int j = 0; cattrs[j].dest; j++) {
968    if (cattrs[j].name.type != kObjectTypeNil) {
969      if (cattrs[j].val < 0) {
970        *cattrs[j].dest = kColorIdxNone;
971      } else if (cattrs[j].name.type == kObjectTypeString && cattrs[j].name.data.string.size) {
972        name_to_color(cattrs[j].name.data.string.data, cattrs[j].dest);
973      } else {
974        *cattrs[j].dest = kColorIdxHex;
975      }
976    } else if (!update) {
977      *cattrs[j].dest = kColorIdxNone;
978    } else if (old_link > 0 && cattrs[j].val >= 0) {
979      // Copy color indices from the linked group so inherited colors remain visible in :hi output.
980      HlGroup *linked = &hl_table[old_link - 1];
981      int linked_idx = (j == 0) ? linked->sg_rgb_fg_idx
982                                : (j == 1) ? linked->sg_rgb_bg_idx
983                                           : linked->sg_rgb_sp_idx;
984      *cattrs[j].dest = (linked_idx != kColorIdxNone) ? linked_idx : kColorIdxHex;
985    }
986  }
987
988  g->sg_cterm = attrs.cterm_ae_attr &~HL_DEFAULT;
989  g->sg_cterm_bg = attrs.cterm_bg_color;
990  g->sg_cterm_fg = attrs.cterm_fg_color;
991
992  g->sg_cterm_bold = g->sg_cterm & HL_BOLD;
993  if (attrs.hl_blend != -1) {
994    g->sg_blend = attrs.hl_blend;
995  } else if (!update) {
996    g->sg_blend = -1;
997  }
998
999  // Persist the font name so set_hl_attr() can rebuild it after a table reset.
1000  // attrs.font already encodes update-inheritance and an explicit "NONE".
1001  if (attrs.font >= 0) {
1002    xfree(g->sg_font);
1003    g->sg_font = xstrdup(hl_get_font(attrs.font));
1004  } else if (HAS_KEY(dict, highlight, font) || !update) {
1005    XFREE_CLEAR(g->sg_font);
1006  }
1007
1008  g->sg_script_ctx = current_sctx;
1009  g->sg_script_ctx.sc_lnum += SOURCING_LNUM;
1010  nlua_set_sctx(&g->sg_script_ctx);
1011
1012  g->sg_attr = hl_get_syn_attr(0, id, attrs);
1013
1014  // 'Normal' is special
1015  if (strcmp(g->sg_name_u, "NORMAL") == 0) {
1016    cterm_normal_fg_color = g->sg_cterm_fg;
1017    cterm_normal_bg_color = g->sg_cterm_bg;
1018    bool did_changed = false;
1019    if (normal_bg != g->sg_rgb_bg || normal_fg != g->sg_rgb_fg || normal_sp != g->sg_rgb_sp) {
1020      did_changed = true;
1021    }
1022    normal_fg = g->sg_rgb_fg;
1023    normal_bg = g->sg_rgb_bg;
1024    normal_sp = g->sg_rgb_sp;
1025
1026    if (did_changed) {
1027      highlight_attr_set_all();
1028    }
1029    ui_default_colors_set();
1030  } else {
1031    // a cursor style uses this syn_id, make sure its attribute is updated.
1032    if (cursor_mode_uses_syn_id(id)) {
1033      ui_mode_info_set();
1034    }
1035  }
1036
1037  if (!updating_screen) {
1038    redraw_all_later(UPD_NOT_VALID);
1039  }
1040  need_highlight_changed = true;
1041}
1042
1043static bool set_gui_color(int idx, bool init, const char *arg, RgbValue *color, int *color_idx)
1044{
1045  if (init && (hl_table[idx].sg_set & SG_GUI)) {
1046    return false;
1047  }
1048  if (!init) {
1049    hl_table[idx].sg_set |= SG_GUI;
1050  }
1051  RgbValue old_color = *color;
1052  int old_idx = *color_idx;
1053  if (strcmp(arg, "NONE") != 0) {
1054    *color = name_to_color(arg, color_idx);
1055  } else {
1056    *color = -1;
1057    *color_idx = kColorIdxNone;
1058  }
1059  return *color != old_color || *color_idx != old_idx;
1060}
1061
1062/// Handle ":highlight" command
1063///
1064/// When using ":highlight clear" this is called recursively for each group with
1065/// forceit and init being both true.
1066///
1067/// @param[in]  line  Command arguments.
1068/// @param[in]  forceit  True when bang is given, allows to link group even if
1069///                      it has its own settings.
1070/// @param[in]  init  True when initializing.
1071void do_highlight(const char *line, const bool forceit, const bool init)
1072  FUNC_ATTR_NONNULL_ALL
1073{
1074  // If no argument, list current highlighting.
1075  if (!init && ends_excmd((uint8_t)(*line))) {
1076    msg_ext_set_kind("list_cmd");
1077    for (int i = 1; i <= highlight_ga.ga_len && !got_int; i++) {
1078      // TODO(brammool): only call when the group has attributes set
1079      highlight_list_one(i);
1080    }
1081    return;
1082  }
1083
1084  bool dodefault = false;
1085
1086  // Isolate the name.
1087  const char *name_end = skiptowhite(line);
1088  const char *linep = skipwhite(name_end);
1089
1090  // Check for "default" argument.
1091  if (strncmp(line, "default", (size_t)(name_end - line)) == 0) {
1092    dodefault = true;
1093    line = linep;
1094    name_end = skiptowhite(line);
1095    linep = skipwhite(name_end);
1096  }
1097
1098  bool doclear = false;
1099  bool dolink = false;
1100
1101  // Check for "clear" or "link" argument.
1102  if (strncmp(line, "clear", (size_t)(name_end - line)) == 0) {
1103    doclear = true;
1104  } else if (strncmp(line, "link", (size_t)(name_end - line)) == 0) {
1105    dolink = true;
1106  }
1107
1108  // ":highlight {group-name}": list highlighting for one group.
1109  if (!doclear && !dolink && ends_excmd((uint8_t)(*linep))) {
1110    int id = syn_name2id_len(line, (size_t)(name_end - line));
1111    if (id == 0) {
1112      semsg(_(e_highlight_group_name_not_found_str), line);
1113    } else {
1114      msg_ext_set_kind("list_cmd");
1115      highlight_list_one(id);
1116    }
1117    return;
1118  }
1119
1120  // Handle ":highlight link {from} {to}" command.
1121  if (dolink) {
1122    const char *from_start = linep;
1123    int to_id;
1124    HlGroup *hlgroup = NULL;
1125
1126    const char *from_end = skiptowhite(from_start);
1127    const char *to_start = skipwhite(from_end);
1128    const char *to_end = skiptowhite(to_start);
1129
1130    if (ends_excmd((uint8_t)(*from_start))
1131        || ends_excmd((uint8_t)(*to_start))) {
1132      semsg(_("E412: Not enough arguments: \":highlight link %s\""),
1133            from_start);
1134      return;
1135    }
1136
1137    if (!ends_excmd(*skipwhite(to_end))) {
1138      semsg(_("E413: Too many arguments: \":highlight link %s\""), from_start);
1139      return;
1140    }
1141
1142    int from_id = syn_check_group(from_start, (size_t)(from_end - from_start));
1143    if (strncmp(to_start, "NONE", 4) == 0) {
1144      to_id = 0;
1145    } else {
1146      to_id = syn_check_group(to_start, (size_t)(to_end - to_start));
1147    }
1148
1149    if (from_id > 0) {
1150      hlgroup = &hl_table[from_id - 1];
1151      if (dodefault && (forceit || hlgroup->sg_deflink == 0)) {
1152        hlgroup->sg_deflink = to_id;
1153        hlgroup->sg_deflink_sctx = current_sctx;
1154        hlgroup->sg_deflink_sctx.sc_lnum += SOURCING_LNUM;
1155        nlua_set_sctx(&hlgroup->sg_deflink_sctx);
1156      }
1157    }
1158
1159    if (from_id > 0 && (!init || hlgroup->sg_set == 0)) {
1160      // Don't allow a link when there already is some highlighting
1161      // for the group, unless '!' is used
1162      if (to_id > 0 && !forceit && !init
1163          && hl_has_settings(from_id - 1, dodefault)) {
1164        if (SOURCING_NAME == NULL && !dodefault) {
1165          emsg(_(e_group_has_settings_highlight_link_ignored));
1166        }
1167      } else if (hlgroup->sg_link != to_id
1168                 || hlgroup->sg_script_ctx.sc_sid != current_sctx.sc_sid
1169                 || hlgroup->sg_cleared) {
1170        if (!init) {
1171          hlgroup->sg_set |= SG_LINK;
1172        }
1173        hlgroup->sg_link = to_id;
1174        hlgroup->sg_script_ctx = current_sctx;
1175        hlgroup->sg_script_ctx.sc_lnum += SOURCING_LNUM;
1176        nlua_set_sctx(&hlgroup->sg_script_ctx);
1177        hlgroup->sg_cleared = false;
1178        redraw_all_later(UPD_SOME_VALID);
1179
1180        // Only call highlight changed() once after multiple changes
1181        need_highlight_changed = true;
1182      }
1183    }
1184
1185    return;
1186  }
1187
1188  if (doclear) {
1189    // ":highlight clear [group]" command.
1190    line = linep;
1191    if (ends_excmd((uint8_t)(*line))) {
1192      do_unlet(S_LEN("g:colors_name"), true);
1193      restore_cterm_colors();
1194
1195      // Clear all default highlight groups and load the defaults.
1196      for (int j = 0; j < highlight_ga.ga_len; j++) {
1197        highlight_clear(j);
1198      }
1199      init_highlight(true, true);
1200      highlight_changed();
1201      redraw_all_later(UPD_NOT_VALID);
1202      return;
1203    }
1204    name_end = skiptowhite(line);
1205    linep = skipwhite(name_end);
1206  }
1207
1208  // Find the group name in the table.  If it does not exist yet, add it.
1209  int id = syn_check_group(line, (size_t)(name_end - line));
1210  if (id == 0) {  // Failed (out of memory).
1211    return;
1212  }
1213  int idx = id - 1;  // Index is ID minus one.
1214
1215  // Return if "default" was used and the group already has settings
1216  if (dodefault && hl_has_settings(idx, true)) {
1217    return;
1218  }
1219
1220  // Make a copy so we can check if any attribute actually changed
1221  HlGroup item_before = hl_table[idx];
1222  bool is_normal_group = (strcmp(hl_table[idx].sg_name_u, "NORMAL") == 0);
1223
1224  // Clear the highlighting for ":hi clear {group}" and ":hi clear".
1225  if (doclear || (forceit && init)) {
1226    highlight_clear(idx);
1227    if (!doclear) {
1228      hl_table[idx].sg_set = 0;
1229    }
1230  }
1231
1232  bool did_change = false;
1233  bool error = false;
1234
1235  char key[64];
1236  char arg[512];
1237  if (!doclear) {
1238    const char *arg_start;
1239
1240    while (!ends_excmd((uint8_t)(*linep))) {
1241      const char *key_start = linep;
1242      if (*linep == '=') {
1243        semsg(_(e_unexpected_equal_sign_str), key_start);
1244        error = true;
1245        break;
1246      }
1247
1248      // Isolate the key ("term", "ctermfg", "ctermbg", "font", "guifg",
1249      // "guibg" or "guisp").
1250      while (*linep && !ascii_iswhite(*linep) && *linep != '=') {
1251        linep++;
1252      }
1253      size_t key_len = (size_t)(linep - key_start);
1254      if (key_len > sizeof(key) - 1) {
1255        emsg(_("E423: Illegal argument"));
1256        error = true;
1257        break;
1258      }
1259      vim_memcpy_up(key, key_start, key_len);
1260      key[key_len] = NUL;
1261      linep = skipwhite(linep);
1262
1263      if (strcmp(key, "NONE") == 0) {
1264        if (!init || hl_table[idx].sg_set == 0) {
1265          if (!init) {
1266            hl_table[idx].sg_set |= SG_CTERM + SG_GUI;
1267          }
1268          highlight_clear(idx);
1269        }
1270        continue;
1271      }
1272
1273      // Check for the equal sign.
1274      if (*linep != '=') {
1275        semsg(_(e_missing_equal_sign_str_2), key_start);
1276        error = true;
1277        break;
1278      }
1279      linep++;
1280
1281      // Isolate the argument.
1282      linep = skipwhite(linep);
1283      if (*linep == '\'') {  // guifg='color name'
1284        arg_start = ++linep;
1285        linep = strchr(linep, '\'');
1286        if (linep == NULL) {
1287          semsg(_(e_invarg2), key_start);
1288          error = true;
1289          break;
1290        }
1291      } else {
1292        arg_start = linep;
1293        linep = skiptowhite(linep);
1294      }
1295      if (linep == arg_start) {
1296        semsg(_(e_missing_argument_str), key_start);
1297        error = true;
1298        break;
1299      }
1300      size_t arg_len = (size_t)(linep - arg_start);
1301      if (arg_len > sizeof(arg) - 1) {
1302        emsg(_("E423: Illegal argument"));
1303        error = true;
1304        break;
1305      }
1306      memcpy(arg, arg_start, arg_len);
1307      arg[arg_len] = NUL;
1308
1309      if (*linep == '\'') {
1310        linep++;
1311      }
1312
1313      // Store the argument.
1314      if (strcmp(key, "TERM") == 0
1315          || strcmp(key, "CTERM") == 0
1316          || strcmp(key, "GUI") == 0) {
1317        int attr = 0;
1318        int off = 0;
1319        int i;
1320        while (arg[off] != NUL) {
1321          for (i = ARRAY_SIZE(hl_attr_table); --i >= 0;) {
1322            int len = (int)strlen(hl_name_table[i]);
1323            if (STRNICMP(arg + off, hl_name_table[i], (size_t)len) == 0) {
1324              if (hl_attr_table[i] & HL_UNDERLINE_MASK) {
1325                attr &= ~HL_UNDERLINE_MASK;
1326              }
1327              attr |= hl_attr_table[i];
1328              off += len;
1329              break;
1330            }
1331          }
1332          if (i < 0) {
1333            semsg(_("E418: Illegal value: %s"), arg);
1334            error = true;
1335            break;
1336          }
1337          if (arg[off] == ',') {  // Another one follows.
1338            off++;
1339          }
1340        }
1341        if (error) {
1342          break;
1343        }
1344        if (*key == 'C') {
1345          if (!init || !(hl_table[idx].sg_set & SG_CTERM)) {
1346            if (!init) {
1347              hl_table[idx].sg_set |= SG_CTERM;
1348            }
1349            hl_table[idx].sg_cterm = attr;
1350            hl_table[idx].sg_cterm_bold = false;
1351          }
1352        } else if (*key == 'G') {
1353          if (!init || !(hl_table[idx].sg_set & SG_GUI)) {
1354            if (!init) {
1355              hl_table[idx].sg_set |= SG_GUI;
1356            }
1357            hl_table[idx].sg_gui = attr;
1358          }
1359        }
1360      } else if (strcmp(key, "FONT") == 0 && (!init || !(hl_table[idx].sg_set & SG_GUI))) {
1361        if (!init) {
1362          hl_table[idx].sg_set |= SG_GUI;
1363        }
1364        if (hl_table[idx].sg_font != NULL) {
1365          XFREE_CLEAR(hl_table[idx].sg_font);
1366        }
1367        if (strcmp(arg, "NONE") != 0) {
1368          hl_table[idx].sg_font = xstrdup(arg);
1369        }
1370      } else if (strcmp(key, "CTERMFG") == 0 || strcmp(key, "CTERMBG") == 0) {
1371        if (!init || !(hl_table[idx].sg_set & SG_CTERM)) {
1372          if (!init) {
1373            hl_table[idx].sg_set |= SG_CTERM;
1374          }
1375
1376          // When setting the foreground color, and previously the "bold"
1377          // flag was set for a light color, reset it now
1378          if (key[5] == 'F' && hl_table[idx].sg_cterm_bold) {
1379            hl_table[idx].sg_cterm &= ~HL_BOLD;
1380            hl_table[idx].sg_cterm_bold = false;
1381          }
1382
1383          int color;
1384          if (ascii_isdigit(*arg)) {
1385            color = atoi(arg);
1386          } else if (STRICMP(arg, "fg") == 0) {
1387            if (cterm_normal_fg_color) {
1388              color = cterm_normal_fg_color - 1;
1389            } else {
1390              emsg(_("E419: FG color unknown"));
1391              error = true;
1392              break;
1393            }
1394          } else if (STRICMP(arg, "bg") == 0) {
1395            if (cterm_normal_bg_color > 0) {
1396              color = cterm_normal_bg_color - 1;
1397            } else {
1398              emsg(_("E420: BG color unknown"));
1399              error = true;
1400              break;
1401            }
1402          } else {
1403            // Reduce calls to STRICMP a bit, it can be slow.
1404            int off = TOUPPER_ASC(*arg);
1405            int i;
1406            for (i = ARRAY_SIZE(color_names); --i >= 0;) {
1407              if (off == color_names[i][0]
1408                  && STRICMP(arg + 1, color_names[i] + 1) == 0) {
1409                break;
1410              }
1411            }
1412            if (i < 0) {
1413              semsg(_("E421: Color name or number not recognized: %s"),
1414                    key_start);
1415              error = true;
1416              break;
1417            }
1418
1419            TriState bold = kNone;
1420            color = lookup_color(i, key[5] == 'F', &bold);
1421
1422            // set/reset bold attribute to get light foreground
1423            // colors (on some terminals, e.g. "linux")
1424            if (bold == kTrue) {
1425              hl_table[idx].sg_cterm |= HL_BOLD;
1426              hl_table[idx].sg_cterm_bold = true;
1427            } else if (bold == kFalse) {
1428              hl_table[idx].sg_cterm &= ~HL_BOLD;
1429            }
1430          }
1431          // Add one to the argument, to avoid zero.  Zero is used for
1432          // "NONE", then "color" is -1.
1433          if (key[5] == 'F') {
1434            hl_table[idx].sg_cterm_fg = color + 1;
1435            if (is_normal_group) {
1436              cterm_normal_fg_color = color + 1;
1437            }
1438          } else {
1439            hl_table[idx].sg_cterm_bg = color + 1;
1440            if (is_normal_group) {
1441              cterm_normal_bg_color = color + 1;
1442              if (!ui_rgb_attached()) {
1443                if (color >= 0) {
1444                  int dark = -1;
1445
1446                  if (t_colors < 16) {
1447                    dark = (color == 0 || color == 4);
1448                  } else if (color < 16) {
1449                    // Limit the heuristic to the standard 16 colors
1450                    dark = (color < 7 || color == 8);
1451                  }
1452                  // Set the 'background' option if the value is
1453                  // wrong.
1454                  if (dark != -1
1455                      && dark != (*p_bg == 'd')
1456                      && !option_was_set(kOptBackground)) {
1457                    set_option_value_give_err(kOptBackground,
1458                                              CSTR_AS_OBJ(dark ? "dark" : "light"), 0);
1459                    reset_option_was_set(kOptBackground);
1460                  }
1461                }
1462              }
1463            }
1464          }
1465        }
1466      } else if (strcmp(key, "GUIFG") == 0) {
1467        did_change = set_gui_color(idx, init, arg, &hl_table[idx].sg_rgb_fg,
1468                                   &hl_table[idx].sg_rgb_fg_idx);
1469        if (is_normal_group) {
1470          normal_fg = hl_table[idx].sg_rgb_fg;
1471        }
1472      } else if (strcmp(key, "GUIBG") == 0) {
1473        did_change = set_gui_color(idx, init, arg, &hl_table[idx].sg_rgb_bg,
1474                                   &hl_table[idx].sg_rgb_bg_idx);
1475        if (is_normal_group) {
1476          normal_bg = hl_table[idx].sg_rgb_bg;
1477        }
1478      } else if (strcmp(key, "GUISP") == 0) {
1479        did_change = set_gui_color(idx, init, arg, &hl_table[idx].sg_rgb_sp,
1480                                   &hl_table[idx].sg_rgb_sp_idx);
1481        if (is_normal_group) {
1482          normal_sp = hl_table[idx].sg_rgb_sp;
1483        }
1484      } else if (strcmp(key, "START") == 0 || strcmp(key, "STOP") == 0) {
1485        // Ignored for now
1486      } else if (strcmp(key, "BLEND") == 0) {
1487        if (strcmp(arg, "NONE") != 0) {
1488          char *arg_end = arg;
1489          hl_table[idx].sg_blend = getdigits_int(&arg_end, false, 0);
1490        } else {
1491          hl_table[idx].sg_blend = -1;
1492        }
1493      } else {
1494        semsg(_("E423: Illegal argument: %s"), key_start);
1495        error = true;
1496        break;
1497      }
1498      hl_table[idx].sg_cleared = false;
1499
1500      // When highlighting has been given for a group, don't link it.
1501      if (!init || !(hl_table[idx].sg_set & SG_LINK)) {
1502        hl_table[idx].sg_link = 0;
1503      }
1504
1505      // Continue with next argument.
1506      linep = skipwhite(linep);
1507    }
1508  }
1509
1510  bool did_highlight_changed = false;
1511
1512  if (!error && is_normal_group) {
1513    // Need to update all groups, because they might be using "bg" and/or
1514    // "fg", which have been changed now.
1515    highlight_attr_set_all();
1516
1517    if (!ui_has(kUILinegrid) && starting == 0) {
1518      // Older UIs assume that we clear the screen after normal group is
1519      // changed
1520      ui_refresh();
1521    } else {
1522      // TUI and newer UIs will repaint the screen themselves. UPD_NOT_VALID
1523      // redraw below will still handle usages of guibg=fg etc.
1524      ui_default_colors_set();
1525    }
1526    did_highlight_changed = true;
1527    redraw_all_later(UPD_NOT_VALID);
1528  } else {
1529    set_hl_attr(idx);
1530  }
1531  hl_table[idx].sg_script_ctx = current_sctx;
1532  hl_table[idx].sg_script_ctx.sc_lnum += SOURCING_LNUM;
1533  nlua_set_sctx(&hl_table[idx].sg_script_ctx);
1534
1535  // Only call highlight_changed() once, after a sequence of highlight
1536  // commands, and only if an attribute actually changed
1537  if ((did_change
1538       || memcmp(&hl_table[idx], &item_before, sizeof(item_before)) != 0)
1539      && !did_highlight_changed) {
1540    // Do not trigger a redraw when highlighting is changed while
1541    // redrawing.  This may happen when evaluating 'statusline' changes the
1542    // StatusLine group.
1543    if (!updating_screen) {
1544      redraw_all_later(UPD_NOT_VALID);
1545    }
1546    need_highlight_changed = true;
1547  }
1548}
1549
1550#ifdef EXITFREE
1551void free_highlight(void)
1552{
1553  for (int i = 0; i < highlight_ga.ga_len; i++) {
1554    xfree(hl_table[i].sg_font);
1555  }
1556  ga_clear(&highlight_ga);
1557  map_destroy(cstr_t, &highlight_unames);
1558  arena_mem_free(arena_finish(&highlight_arena));
1559}
1560
1561#endif
1562
1563/// Reset the cterm colors to what they were before Vim was started, if
1564/// possible.  Otherwise reset them to zero.
1565void restore_cterm_colors(void)
1566{
1567  normal_fg = -1;
1568  normal_bg = -1;
1569  normal_sp = -1;
1570  cterm_normal_fg_color = 0;
1571  cterm_normal_bg_color = 0;
1572}
1573
1574/// @param check_link  if true also check for an existing link.
1575///
1576/// @return true if highlight group "idx" has any settings.
1577static bool hl_has_settings(int idx, bool check_link)
1578{
1579  return hl_table[idx].sg_cleared == 0
1580         && (hl_table[idx].sg_attr != 0
1581             || hl_table[idx].sg_cterm_fg != 0
1582             || hl_table[idx].sg_cterm_bg != 0
1583             || hl_table[idx].sg_rgb_fg_idx != kColorIdxNone
1584             || hl_table[idx].sg_rgb_bg_idx != kColorIdxNone
1585             || hl_table[idx].sg_rgb_sp_idx != kColorIdxNone
1586             || (check_link && (hl_table[idx].sg_set & SG_LINK)));
1587}
1588
1589/// Clear highlighting for one group.
1590static void highlight_clear(int idx)
1591{
1592  hl_table[idx].sg_cleared = true;
1593
1594  hl_table[idx].sg_attr = 0;
1595  hl_table[idx].sg_cterm = 0;
1596  hl_table[idx].sg_cterm_bold = false;
1597  hl_table[idx].sg_cterm_fg = 0;
1598  hl_table[idx].sg_cterm_bg = 0;
1599  hl_table[idx].sg_gui = 0;
1600  hl_table[idx].sg_rgb_fg = -1;
1601  hl_table[idx].sg_rgb_bg = -1;
1602  hl_table[idx].sg_rgb_sp = -1;
1603  hl_table[idx].sg_rgb_fg_idx = kColorIdxNone;
1604  hl_table[idx].sg_rgb_bg_idx = kColorIdxNone;
1605  hl_table[idx].sg_rgb_sp_idx = kColorIdxNone;
1606  hl_table[idx].sg_blend = -1;
1607  if (hl_table[idx].sg_font != NULL) {
1608    XFREE_CLEAR(hl_table[idx].sg_font);
1609  }
1610  // Restore default link and context if they exist. Otherwise clears.
1611  hl_table[idx].sg_link = hl_table[idx].sg_deflink;
1612  // Since we set the default link, set the location to where the default
1613  // link was set.
1614  hl_table[idx].sg_script_ctx = hl_table[idx].sg_deflink_sctx;
1615}
1616
1617/// \addtogroup LIST_XXX
1618/// @{
1619#define LIST_ATTR   1
1620#define LIST_STRING 2
1621#define LIST_INT    3
1622/// @}
1623
1624static void highlight_list_one(const int id)
1625{
1626  const HlGroup *sgp = &hl_table[id - 1];  // index is ID minus one
1627  bool didh = false;
1628
1629  if (message_filtered(sgp->sg_name)) {
1630    return;
1631  }
1632
1633  // don't list specialized groups if a parent is used instead
1634  if (sgp->sg_parent && sgp->sg_cleared) {
1635    return;
1636  }
1637
1638  didh = highlight_list_arg(id, didh, LIST_ATTR,
1639                            sgp->sg_cterm, NULL, "cterm");
1640  didh = highlight_list_arg(id, didh, LIST_INT,
1641                            sgp->sg_cterm_fg, NULL, "ctermfg");
1642  didh = highlight_list_arg(id, didh, LIST_INT,
1643                            sgp->sg_cterm_bg, NULL, "ctermbg");
1644
1645  didh = highlight_list_arg(id, didh, LIST_ATTR,
1646                            sgp->sg_gui, NULL, "gui");
1647  char hexbuf[8];
1648  didh = highlight_list_arg(id, didh, LIST_STRING, 0,
1649                            coloridx_to_name(sgp->sg_rgb_fg_idx, sgp->sg_rgb_fg, hexbuf), "guifg");
1650  didh = highlight_list_arg(id, didh, LIST_STRING, 0,
1651                            coloridx_to_name(sgp->sg_rgb_bg_idx, sgp->sg_rgb_bg, hexbuf), "guibg");
1652  didh = highlight_list_arg(id, didh, LIST_STRING, 0,
1653                            coloridx_to_name(sgp->sg_rgb_sp_idx, sgp->sg_rgb_sp, hexbuf), "guisp");
1654
1655  didh = highlight_list_arg(id, didh, LIST_INT, sgp->sg_blend + 1, NULL, "blend");
1656
1657  didh = highlight_list_arg(id, didh, LIST_STRING, 0, sgp->sg_font, "font");
1658
1659  if (sgp->sg_link && !got_int) {
1660    syn_list_header(didh, 0, id, true);
1661    didh = true;
1662    msg_puts_hl("links to", HLF_D, false);
1663    msg_putchar(' ');
1664    msg_outtrans(hl_table[hl_table[id - 1].sg_link - 1].sg_name, 0, false);
1665  }
1666
1667  if (!didh) {
1668    highlight_list_arg(id, didh, LIST_STRING, 0, "cleared", "");
1669  }
1670  if (p_verbose > 0) {
1671    last_set_msg(sgp->sg_script_ctx);
1672  }
1673}
1674
1675static bool hlgroup2dict(Dict *hl, NS ns_id, int hl_id, Arena *arena)
1676{
1677  HlGroup *sgp = &hl_table[hl_id - 1];
1678  NS ns = ns_id;
1679  int link = ns_id == 0 ? sgp->sg_link : ns_get_hl(&ns, hl_id, true, sgp->sg_set);
1680  if (link == -1) {
1681    return false;
1682  }
1683  if (ns_id == 0 && sgp->sg_cleared && sgp->sg_set == 0) {
1684    // table entry was created but not ever set
1685    return false;
1686  }
1687  ns = ns_id;
1688  HlAttrs attr = syn_attr2entry(ns_id == 0 ? sgp->sg_attr : ns_get_hl(&ns, hl_id, false,
1689                                                                      sgp->sg_set));
1690  *hl = arena_dict(arena, HLATTRS_DICT_SIZE + 1);
1691  if (attr.rgb_ae_attr & HL_DEFAULT) {
1692    PUT_C(*hl, "default", BOOLEAN_OBJ(true));
1693  }
1694  if (link > 0) {
1695    assert(1 <= link && link <= highlight_ga.ga_len);
1696    PUT_C(*hl, "link", CSTR_AS_OBJ(hl_table[link - 1].sg_name));
1697  }
1698  Dict hl_cterm = arena_dict(arena, HLATTRS_DICT_SIZE);
1699  hlattrs2dict(hl, NULL, attr, true, true);
1700  hlattrs2dict(hl, &hl_cterm, attr, false, true);
1701  if (kv_size(hl_cterm)) {
1702    PUT_C(*hl, "cterm", DICT_OBJ(hl_cterm));
1703  }
1704  return true;
1705}
1706
1707Dict ns_get_hl_defs(NS ns_id, Dict(get_highlight) *opts, Arena *arena, Error *err)
1708{
1709  Boolean link = GET_BOOL_OR_TRUE(opts, get_highlight, link);
1710  int id = -1;
1711  if (HAS_KEY(opts, get_highlight, name)) {
1712    Boolean create = GET_BOOL_OR_TRUE(opts, get_highlight, create);
1713    id = create ? syn_check_group(opts->name.data, opts->name.size)
1714                : syn_name2id_len(opts->name.data, opts->name.size);
1715    if (id == 0 && !create) {
1716      Dict attrs = ARRAY_DICT_INIT;
1717      return attrs;
1718    }
1719  } else if (HAS_KEY(opts, get_highlight, id)) {
1720    id = (int)opts->id;
1721  }
1722
1723  if (id != -1) {
1724    VALIDATE(1 <= id && id <= highlight_ga.ga_len, "%s", "Highlight id out of bounds", {
1725      goto cleanup;
1726    });
1727    Dict attrs = ARRAY_DICT_INIT;
1728    hlgroup2dict(&attrs, ns_id, link ? id : syn_get_final_id(id), arena);
1729    return attrs;
1730  }
1731  if (ERROR_SET(err)) {
1732    goto cleanup;
1733  }
1734
1735  Dict rv = arena_dict(arena, (size_t)highlight_ga.ga_len);
1736  for (int i = 1; i <= highlight_ga.ga_len; i++) {
1737    Dict attrs = ARRAY_DICT_INIT;
1738    if (!hlgroup2dict(&attrs, ns_id, i, arena)) {
1739      continue;
1740    }
1741    PUT_C(rv, hl_table[(link ? i : syn_get_final_id(i)) - 1].sg_name, DICT_OBJ(attrs));
1742  }
1743
1744  return rv;
1745
1746cleanup:
1747  return (Dict)ARRAY_DICT_INIT;
1748}
1749
1750/// Outputs a highlight when doing ":hi MyHighlight"
1751///
1752/// @param type one of \ref LIST_XXX
1753/// @param iarg integer argument used if \p type == LIST_INT
1754/// @param sarg string used if \p type == LIST_STRING
1755static bool highlight_list_arg(const int id, bool didh, const int type, int iarg, const char *sarg,
1756                               const char *const name)
1757{
1758  if (got_int) {
1759    return false;
1760  }
1761
1762  if (type == LIST_STRING ? (sarg == NULL) : (iarg == 0)) {
1763    return didh;
1764  }
1765
1766  char buf[100];
1767  const char *ts = buf;
1768  if (type == LIST_INT) {
1769    snprintf(buf, sizeof(buf), "%d", iarg - 1);
1770  } else if (type == LIST_STRING) {
1771    ts = sarg;
1772  } else {    // type == LIST_ATTR
1773    buf[0] = NUL;
1774    for (int i = 0; hl_attr_table[i] != 0; i++) {
1775      if (((hl_attr_table[i] & HL_UNDERLINE_MASK)
1776           && ((iarg & HL_UNDERLINE_MASK) == hl_attr_table[i]))
1777          || (!(hl_attr_table[i] & HL_UNDERLINE_MASK)
1778              && (iarg & hl_attr_table[i]))) {
1779        if (buf[0] != NUL) {
1780          xstrlcat(buf, ",", 100);
1781        }
1782        xstrlcat(buf, hl_name_table[i], 100);
1783        if (!(hl_attr_table[i] & HL_UNDERLINE_MASK)) {
1784          iarg &= ~hl_attr_table[i];  // don't want "inverse"
1785        }
1786      }
1787    }
1788  }
1789
1790  syn_list_header(didh, vim_strsize(ts) + (int)strlen(name) + 1, id, false);
1791  didh = true;
1792  if (!got_int) {
1793    if (*name != NUL) {
1794      msg_puts_hl(name, HLF_D, false);
1795      msg_puts_hl("=", HLF_D, false);
1796    }
1797    msg_outtrans(ts, 0, false);
1798  }
1799  return didh;
1800}
1801
1802/// Check whether highlight group has attribute
1803///
1804/// @param[in]  id  Highlight group to check.
1805/// @param[in]  flag  Attribute to check.
1806/// @param[in]  modec  'g' for GUI, 'c' for term.
1807///
1808/// @return "1" if highlight group has attribute, NULL otherwise.
1809const char *highlight_has_attr(const int id, const int flag, const int modec)
1810  FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
1811{
1812  if (id <= 0 || id > highlight_ga.ga_len) {
1813    return NULL;
1814  }
1815
1816  int attr;
1817
1818  if (modec == 'g') {
1819    attr = hl_table[id - 1].sg_gui;
1820  } else {
1821    attr = hl_table[id - 1].sg_cterm;
1822  }
1823
1824  if (flag & HL_UNDERLINE_MASK) {
1825    int ul = attr & HL_UNDERLINE_MASK;
1826    return ul == flag ? "1" : NULL;
1827  } else {
1828    return (attr & flag) ? "1" : NULL;
1829  }
1830}
1831
1832/// Return color name of the given highlight group
1833///
1834/// @param[in]  id  Highlight group to work with.
1835/// @param[in]  what  What to return: one of "font", "fg", "bg", "sp", "fg#",
1836///                   "bg#" or "sp#".
1837/// @param[in]  modec  'g' for GUI, 'c' for cterm and 't' for term.
1838///
1839/// @return color name, possibly in a static buffer. Buffer will be overwritten
1840///         on next highlight_color() call. May return NULL.
1841const char *highlight_color(const int id, const char *const what, const int modec)
1842  FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
1843{
1844  static char name[20];
1845  bool fg = false;
1846  bool sp = false;
1847  bool font = false;
1848
1849  if (id <= 0 || id > highlight_ga.ga_len) {
1850    return NULL;
1851  }
1852
1853  if (TOLOWER_ASC(what[0]) == 'f' && TOLOWER_ASC(what[1]) == 'g') {
1854    fg = true;
1855  } else if (TOLOWER_ASC(what[0]) == 'f' && TOLOWER_ASC(what[1]) == 'o'
1856             && TOLOWER_ASC(what[2]) == 'n' && TOLOWER_ASC(what[3]) == 't') {
1857    font = true;
1858  } else if (TOLOWER_ASC(what[0]) == 's' && TOLOWER_ASC(what[1]) == 'p') {
1859    sp = true;
1860  } else if (!(TOLOWER_ASC(what[0]) == 'b' && TOLOWER_ASC(what[1]) == 'g')) {
1861    return NULL;
1862  }
1863
1864  int n;
1865
1866  if (modec == 'g') {
1867    if (what[2] == '#' && ui_rgb_attached()) {
1868      if (fg) {
1869        n = hl_table[id - 1].sg_rgb_fg;
1870      } else if (sp) {
1871        n = hl_table[id - 1].sg_rgb_sp;
1872      } else {
1873        n = hl_table[id - 1].sg_rgb_bg;
1874      }
1875      if (n < 0 || n > 0xffffff) {
1876        return NULL;
1877      }
1878      snprintf(name, sizeof(name), "#%06x", n);
1879      return name;
1880    }
1881    if (fg) {
1882      return coloridx_to_name(hl_table[id - 1].sg_rgb_fg_idx, hl_table[id - 1].sg_rgb_fg, name);
1883    } else if (sp) {
1884      return coloridx_to_name(hl_table[id - 1].sg_rgb_sp_idx, hl_table[id - 1].sg_rgb_sp, name);
1885    } else {
1886      return coloridx_to_name(hl_table[id - 1].sg_rgb_bg_idx, hl_table[id - 1].sg_rgb_bg, name);
1887    }
1888  }
1889  if (font || sp) {
1890    return NULL;
1891  }
1892  if (modec == 'c') {
1893    if (fg) {
1894      n = hl_table[id - 1].sg_cterm_fg - 1;
1895    } else {
1896      n = hl_table[id - 1].sg_cterm_bg - 1;
1897    }
1898    if (n < 0) {
1899      return NULL;
1900    }
1901    snprintf(name, sizeof(name), "%d", n);
1902    return name;
1903  }
1904  // term doesn't have color.
1905  return NULL;
1906}
1907
1908/// Output the syntax list header.
1909///
1910/// @param did_header did header already
1911/// @param outlen length of string that comes
1912/// @param id highlight group id
1913/// @param force_newline always start a new line
1914/// @return true when started a new line.
1915bool syn_list_header(const bool did_header, const int outlen, const int id, bool force_newline)
1916{
1917  int endcol = 19;
1918  bool newline = true;
1919  int name_col = 0;
1920  bool adjust = true;
1921
1922  if (!did_header) {
1923    if (!ui_has(kUIMessages) || msg_col > 0) {
1924      msg_putchar('\n');
1925    }
1926    if (got_int) {
1927      return true;
1928    }
1929    msg_col = name_col = msg_outtrans(hl_table[id - 1].sg_name, 0, false);
1930    endcol = 15;
1931  } else if ((ui_has(kUIMessages) || msg_silent) && !force_newline) {
1932    msg_putchar(' ');
1933    adjust = false;
1934  } else if (msg_col + outlen + 1 >= Columns || force_newline) {
1935    msg_putchar('\n');
1936    if (got_int) {
1937      return true;
1938    }
1939  } else {
1940    if (msg_col >= endcol) {    // wrap around is like starting a new line
1941      newline = false;
1942    }
1943  }
1944
1945  if (adjust) {
1946    if (msg_col >= endcol) {
1947      // output at least one space
1948      endcol = msg_col + 1;
1949    }
1950
1951    msg_advance(endcol);
1952  }
1953
1954  // Show "xxx" with the attributes.
1955  if (!did_header) {
1956    if (endcol == Columns - 1 && endcol <= name_col) {
1957      msg_putchar(' ');
1958    }
1959    msg_puts_hl("xxx", id, false);
1960    msg_putchar(' ');
1961  }
1962
1963  return newline;
1964}
1965
1966/// Set the attribute numbers for a highlight group.
1967/// Called after one of the attributes has changed.
1968/// @param idx corrected highlight index
1969static void set_hl_attr(int idx)
1970{
1971  HlAttrs at_en = HLATTRS_INIT;
1972  HlGroup *sgp = hl_table + idx;
1973
1974  at_en.cterm_ae_attr = (int32_t)sgp->sg_cterm;
1975  at_en.cterm_fg_color = (int16_t)sgp->sg_cterm_fg;
1976  at_en.cterm_bg_color = (int16_t)sgp->sg_cterm_bg;
1977  at_en.rgb_ae_attr = (int32_t)sgp->sg_gui;
1978  // FIXME(tarruda): The "unset value" for rgb is -1, but since hlgroup is
1979  // initialized with 0 (by garray functions), check for sg_rgb_{f,b}g_name
1980  // before setting attr_entry->{f,g}g_color to a other than -1
1981  at_en.rgb_fg_color = sgp->sg_rgb_fg_idx != kColorIdxNone ? sgp->sg_rgb_fg : -1;
1982  at_en.rgb_bg_color = sgp->sg_rgb_bg_idx != kColorIdxNone ? sgp->sg_rgb_bg : -1;
1983  at_en.rgb_sp_color = sgp->sg_rgb_sp_idx != kColorIdxNone ? sgp->sg_rgb_sp : -1;
1984  at_en.hl_blend = sgp->sg_blend;
1985  // Convert font name to index
1986  if (sgp->sg_font != NULL) {
1987    at_en.font = hl_add_font_idx(sgp->sg_font);
1988  }
1989
1990  sgp->sg_attr = hl_get_syn_attr(0, idx + 1, at_en);
1991
1992  // a cursor style uses this syn_id, make sure its attribute is updated.
1993  if (cursor_mode_uses_syn_id(idx + 1)) {
1994    ui_mode_info_set();
1995  }
1996}
1997
1998int syn_name2id(const char *name)
1999  FUNC_ATTR_NONNULL_ALL
2000{
2001  if (name[0] == '@') {
2002    // if we look up @aaa.bbb, we have to consider @aaa as well
2003    return syn_check_group(name, strlen(name));
2004  }
2005  return syn_name2id_len(name, strlen(name));
2006}
2007
2008/// Lookup a highlight group name and return its ID.
2009///
2010/// @param highlight name e.g. 'Cursor', 'Normal'
2011/// @return the highlight id, else 0 if \p name does not exist
2012int syn_name2id_len(const char *name, size_t len)
2013  FUNC_ATTR_NONNULL_ALL
2014{
2015  char name_u[MAX_SYN_NAME + 1];
2016
2017  if (len == 0 || len > MAX_SYN_NAME) {
2018    return 0;
2019  }
2020
2021  // Avoid using stricmp() too much, it's slow on some systems
2022  // Avoid alloc()/free(), these are slow too.
2023  vim_memcpy_up(name_u, name, len);
2024  name_u[len] = NUL;
2025
2026  // map_get(..., int) returns 0 when no key is present, which is
2027  // the expected value for missing highlight group.
2028  return map_get(cstr_t, int)(&highlight_unames, name_u);
2029}
2030
2031/// Lookup a highlight group name and return its attributes.
2032/// Return zero if not found.
2033int syn_name2attr(const char *name)
2034  FUNC_ATTR_NONNULL_ALL
2035{
2036  int id = syn_name2id(name);
2037
2038  if (id != 0) {
2039    return syn_id2attr(id);
2040  }
2041  return 0;
2042}
2043
2044/// Return true if highlight group "name" exists.
2045int highlight_exists(const char *name)
2046{
2047  return syn_name2id(name) > 0;
2048}
2049
2050/// Return the name of highlight group "id".
2051/// When not a valid ID return an empty string.
2052char *syn_id2name(int id)
2053{
2054  if (id <= 0 || id > highlight_ga.ga_len) {
2055    return "";
2056  }
2057  return hl_table[id - 1].sg_name;
2058}
2059
2060/// Find highlight group name in the table and return its ID.
2061/// If it doesn't exist yet, a new entry is created.
2062///
2063/// @param pp Highlight group name
2064/// @param len length of \p pp
2065///
2066/// @return 0 for failure else the id of the group
2067int syn_check_group(const char *name, size_t len)
2068{
2069  if (len > MAX_SYN_NAME) {
2070    emsg(_(e_highlight_group_name_too_long));
2071    return 0;
2072  }
2073  int id = syn_name2id_len(name, len);
2074  if (id == 0) {  // doesn't exist yet
2075    return syn_add_group(name, len);
2076  }
2077  return id;
2078}
2079
2080/// Add new highlight group and return its ID.
2081///
2082/// @param name must be an allocated string, it will be consumed.
2083/// @return 0 for failure, else the allocated group id
2084/// @see syn_check_group
2085static int syn_add_group(const char *name, size_t len)
2086{
2087  // Check that the name is valid (ASCII letters, digits, '_', '.', '@', '-').
2088  for (size_t i = 0; i < len; i++) {
2089    int c = (uint8_t)name[i];
2090    if (!vim_isprintc(c)) {
2091      emsg(_("E669: Unprintable character in group name"));
2092      return 0;
2093    } else if (!ASCII_ISALNUM(c) && c != '_' && c != '.' && c != '@' && c != '-') {
2094      // '.' and '@' are allowed characters for use with treesitter capture names.
2095      msg_source(HLF_W);
2096      emsg(_(e_highlight_group_name_invalid_char));
2097      return 0;
2098    }
2099  }
2100
2101  int scoped_parent = 0;
2102  if (len > 1 && name[0] == '@') {
2103    char *delim = xmemrchr(name, '.', len);
2104    if (delim) {
2105      scoped_parent = syn_check_group(name, (size_t)(delim - name));
2106    }
2107  }
2108
2109  // First call for this growarray: init growing array.
2110  if (highlight_ga.ga_data == NULL) {
2111    highlight_ga.ga_itemsize = sizeof(HlGroup);
2112    ga_set_growsize(&highlight_ga, 10);
2113    // 265 builtin groups, will always be used, plus some space
2114    ga_grow(&highlight_ga, 300);
2115  }
2116
2117  if (highlight_ga.ga_len >= MAX_HL_ID) {
2118    emsg(_("E849: Too many highlight and syntax groups"));
2119    return 0;
2120  }
2121
2122  // Append another syntax_highlight entry.
2123  HlGroup *hlgp = GA_APPEND_VIA_PTR(HlGroup, &highlight_ga);
2124  CLEAR_POINTER(hlgp);
2125  hlgp->sg_name = arena_memdupz(&highlight_arena, name, len);
2126  hlgp->sg_rgb_bg = -1;
2127  hlgp->sg_rgb_fg = -1;
2128  hlgp->sg_rgb_sp = -1;
2129  hlgp->sg_rgb_bg_idx = kColorIdxNone;
2130  hlgp->sg_rgb_fg_idx = kColorIdxNone;
2131  hlgp->sg_rgb_sp_idx = kColorIdxNone;
2132  hlgp->sg_blend = -1;
2133  hlgp->sg_name_u = arena_memdupz(&highlight_arena, name, len);
2134  hlgp->sg_parent = scoped_parent;
2135  // will get set to false by caller if settings are added
2136  hlgp->sg_cleared = true;
2137  vim_strup(hlgp->sg_name_u);
2138
2139  int id = highlight_ga.ga_len;  // ID is index plus one
2140
2141  map_put(cstr_t, int)(&highlight_unames, hlgp->sg_name_u, id);
2142
2143  return id;
2144}
2145
2146/// Translate a group ID to highlight attributes.
2147/// @see syn_attr2entry
2148int syn_id2attr(int hl_id)
2149{
2150  bool optional = false;
2151  return syn_ns_id2attr(-1, hl_id, &optional);
2152}
2153
2154int syn_ns_id2attr(int ns_id, int hl_id, bool *optional)
2155  FUNC_ATTR_NONNULL_ALL
2156{
2157  if (syn_ns_get_final_id(&ns_id, &hl_id)) {
2158    // If the namespace explicitly defines a group to be empty, it is not optional
2159    *optional = false;
2160  }
2161  HlGroup *sgp = &hl_table[hl_id - 1];  // index is ID minus one
2162
2163  int attr = ns_get_hl(&ns_id, hl_id, false, sgp->sg_set);
2164
2165  // if a highlight group is optional, don't use the global value
2166  if (attr >= 0 || (*optional && ns_id > 0)) {
2167    return attr;
2168  }
2169  return sgp->sg_attr;
2170}
2171
2172/// Translate a group ID to the final group ID (following links).
2173int syn_get_final_id(int hl_id)
2174{
2175  int ns_id = curwin->w_ns_hl_active;
2176  syn_ns_get_final_id(&ns_id, &hl_id);
2177  return hl_id;
2178}
2179
2180bool syn_ns_get_final_id(int *ns_id, int *hl_idp)
2181{
2182  int hl_id = *hl_idp;
2183  bool used = false;
2184
2185  if (hl_id > highlight_ga.ga_len || hl_id < 1) {
2186    *hl_idp = 0;
2187    return false;  // Can be called from eval!!
2188  }
2189
2190  // Follow links until there is no more.
2191  // Look out for loops!  Break after 100 links.
2192  for (int count = 100; --count >= 0;) {
2193    HlGroup *sgp = &hl_table[hl_id - 1];  // index is ID minus one
2194
2195    // TODO(bfredl): when using "tmp" attribute (no link) the function might be
2196    // called twice. it needs be smart enough to remember attr only to
2197    // syn_id2attr time
2198    int check = ns_get_hl(ns_id, hl_id, true, sgp->sg_set);
2199    if (check == 0) {
2200      *hl_idp = hl_id;
2201      return true;  // how dare! it broke the link!
2202    } else if (check > 0) {
2203      used = true;
2204      hl_id = check;
2205      continue;
2206    }
2207
2208    if (sgp->sg_link > 0 && sgp->sg_link <= highlight_ga.ga_len) {
2209      hl_id = sgp->sg_link;
2210    } else if (sgp->sg_cleared && sgp->sg_parent > 0) {
2211      hl_id = sgp->sg_parent;
2212    } else {
2213      break;
2214    }
2215  }
2216
2217  *hl_idp = hl_id;
2218  return used;
2219}
2220
2221/// Refresh the color attributes of all highlight groups.
2222void highlight_attr_set_all(void)
2223{
2224  for (int idx = 0; idx < highlight_ga.ga_len; idx++) {
2225    HlGroup *sgp = &hl_table[idx];
2226    if (sgp->sg_rgb_bg_idx == kColorIdxFg) {
2227      sgp->sg_rgb_bg = normal_fg;
2228    } else if (sgp->sg_rgb_bg_idx == kColorIdxBg) {
2229      sgp->sg_rgb_bg = normal_bg;
2230    }
2231    if (sgp->sg_rgb_fg_idx == kColorIdxFg) {
2232      sgp->sg_rgb_fg = normal_fg;
2233    } else if (sgp->sg_rgb_fg_idx == kColorIdxBg) {
2234      sgp->sg_rgb_fg = normal_bg;
2235    }
2236    if (sgp->sg_rgb_sp_idx == kColorIdxFg) {
2237      sgp->sg_rgb_sp = normal_fg;
2238    } else if (sgp->sg_rgb_sp_idx == kColorIdxBg) {
2239      sgp->sg_rgb_sp = normal_bg;
2240    }
2241    set_hl_attr(idx);
2242  }
2243}
2244
2245// Apply difference between User[1-9] and HLF_S to HLF_SNC.
2246static void combine_stl_hlt(int id, int id_S, int id_alt, int hlcnt, int i, int hlf, int *table)
2247  FUNC_ATTR_NONNULL_ALL
2248{
2249  HlGroup *const hlt = hl_table;
2250
2251  if (id_alt == 0) {
2252    CLEAR_POINTER(&hlt[hlcnt + i]);
2253    hlt[hlcnt + i].sg_cterm = highlight_attr[hlf];
2254    hlt[hlcnt + i].sg_gui = highlight_attr[hlf];
2255  } else {
2256    memmove(&hlt[hlcnt + i], &hlt[id_alt - 1], sizeof(HlGroup));
2257  }
2258  hlt[hlcnt + i].sg_link = 0;
2259
2260  hlt[hlcnt + i].sg_cterm ^= hlt[id - 1].sg_cterm ^ hlt[id_S - 1].sg_cterm;
2261  if (hlt[id - 1].sg_cterm_fg != hlt[id_S - 1].sg_cterm_fg) {
2262    hlt[hlcnt + i].sg_cterm_fg = hlt[id - 1].sg_cterm_fg;
2263  }
2264  if (hlt[id - 1].sg_cterm_bg != hlt[id_S - 1].sg_cterm_bg) {
2265    hlt[hlcnt + i].sg_cterm_bg = hlt[id - 1].sg_cterm_bg;
2266  }
2267  hlt[hlcnt + i].sg_gui ^= hlt[id - 1].sg_gui ^ hlt[id_S - 1].sg_gui;
2268  if (hlt[id - 1].sg_rgb_fg != hlt[id_S - 1].sg_rgb_fg) {
2269    hlt[hlcnt + i].sg_rgb_fg = hlt[id - 1].sg_rgb_fg;
2270  }
2271  if (hlt[id - 1].sg_rgb_bg != hlt[id_S - 1].sg_rgb_bg) {
2272    hlt[hlcnt + i].sg_rgb_bg = hlt[id - 1].sg_rgb_bg;
2273  }
2274  if (hlt[id - 1].sg_rgb_sp != hlt[id_S - 1].sg_rgb_sp) {
2275    hlt[hlcnt + i].sg_rgb_sp = hlt[id - 1].sg_rgb_sp;
2276  }
2277  highlight_ga.ga_len = hlcnt + i + 1;
2278  set_hl_attr(hlcnt + i);  // At long last we can apply
2279  table[i] = syn_id2attr(hlcnt + i + 1);
2280}
2281
2282/// Translate highlight groups into attributes in highlight_attr[] and set up
2283/// the user highlights User1..9. A set of corresponding highlights to use on
2284/// top of HLF_SNC is computed.  Called only when nvim starts and upon first
2285/// screen redraw after any :highlight command.
2286void highlight_changed(void)
2287{
2288  char userhl[30];  // use 30 to avoid compiler warning
2289  int id_S = -1;
2290  int id_SNC = 0;
2291
2292  need_highlight_changed = false;
2293
2294  // sentinel value. used when no highlight is active
2295  highlight_attr[HLF_NONE] = 0;
2296
2297  /// Translate builtin highlight groups into attributes for quick lookup.
2298  for (int hlf = 1; hlf < HLF_COUNT; hlf++) {
2299    int id = syn_check_group(hlf_names[hlf], strlen(hlf_names[hlf]));
2300    if (id == 0) {
2301      abort();
2302    }
2303    int ns_id = 0;
2304    int final_id = id;
2305    syn_ns_get_final_id(&ns_id, &final_id);
2306    if (hlf == HLF_SNC) {
2307      id_SNC = final_id;
2308    } else if (hlf == HLF_S) {
2309      id_S = final_id;
2310    }
2311
2312    highlight_attr[hlf] = hl_get_ui_attr(ns_id, hlf, final_id, hlf == HLF_INACTIVE);
2313
2314    if (highlight_attr[hlf] != highlight_attr_last[hlf]) {
2315      if (hlf == HLF_MSG) {
2316        clear_cmdline = true;
2317        HlAttrs attrs = syn_attr2entry(highlight_attr[hlf]);
2318        msg_grid.blending = attrs.hl_blend > -1;
2319      }
2320      ui_call_hl_group_set(cstr_as_string(hlf_names[hlf]),
2321                           highlight_attr[hlf]);
2322      highlight_attr_last[hlf] = highlight_attr[hlf];
2323    }
2324  }
2325
2326  // Setup the user highlights
2327  //
2328  // Temporarily utilize 10 more hl entries:
2329  // 9 for User1-User9 combined with StatusLineNC
2330  // 1 for StatusLine default
2331  // Must to be in there simultaneously in case of table overflows in
2332  // get_attr_entry()
2333  ga_grow(&highlight_ga, 10);
2334  int hlcnt = highlight_ga.ga_len;
2335  if (id_S == -1) {
2336    // Make sure id_S is always valid to simplify code below. Use the last entry
2337    CLEAR_POINTER(&hl_table[hlcnt + 9]);
2338    id_S = hlcnt + 10;
2339  }
2340  for (int i = 0; i < 9; i++) {
2341    snprintf(userhl, sizeof(userhl), "User%d", i + 1);
2342    int id = syn_name2id(userhl);
2343    if (id == 0) {
2344      highlight_user[i] = 0;
2345      highlight_stlnc[i] = 0;
2346    } else {
2347      highlight_user[i] = syn_id2attr(id);
2348      combine_stl_hlt(id, id_S, id_SNC, hlcnt, i, HLF_SNC, highlight_stlnc);
2349    }
2350  }
2351  highlight_ga.ga_len = hlcnt;
2352
2353  decor_provider_invalidate_hl();
2354}
2355
2356/// Handle command line completion for :highlight command.
2357void set_context_in_highlight_cmd(expand_T *xp, const char *arg)
2358{
2359  // Default: expand group names.
2360  xp->xp_context = EXPAND_HIGHLIGHT;
2361  xp->xp_pattern = (char *)arg;
2362  include_link = 2;
2363  include_default = 1;
2364
2365  if (*arg == NUL) {
2366    return;
2367  }
2368
2369  // (part of) subcommand already typed
2370  const char *p = skiptowhite(arg);
2371  if (*p == NUL) {
2372    return;
2373  }
2374
2375  // past "default" or group name
2376  include_default = 0;
2377  if (strncmp("default", arg, (unsigned)(p - arg)) == 0) {
2378    arg = skipwhite(p);
2379    xp->xp_pattern = (char *)arg;
2380    p = skiptowhite(arg);
2381  }
2382  if (*p == NUL) {
2383    return;
2384  }
2385
2386  // past group name
2387  include_link = 0;
2388  if (arg[1] == 'i' && arg[0] == 'N') {
2389    highlight_list();
2390  }
2391  if (strncmp("link", arg, (unsigned)(p - arg)) == 0
2392      || strncmp("clear", arg, (unsigned)(p - arg)) == 0) {
2393    xp->xp_pattern = skipwhite(p);
2394    p = skiptowhite(xp->xp_pattern);
2395    if (*p != NUL) {  // past first group name
2396      xp->xp_pattern = skipwhite(p);
2397      p = skiptowhite(xp->xp_pattern);
2398    }
2399  }
2400  if (*p != NUL) {  // past group name(s)
2401    xp->xp_context = EXPAND_NOTHING;
2402  }
2403}
2404
2405/// List highlighting matches in a nice way.
2406static void highlight_list(void)
2407{
2408  for (int i = 10; --i >= 0;) {
2409    highlight_list_two(i, HLF_D);
2410  }
2411  for (int i = 40; --i >= 0;) {
2412    highlight_list_two(99, 0);
2413  }
2414}
2415
2416static void highlight_list_two(int cnt, int id)
2417{
2418  msg_puts_hl(&("N \bI \b!  \b"[cnt / 11]), id, false);
2419  msg_clr_eos();
2420  ui_flush();
2421  // TODO(justinmk): is this delay needed? ":hi" seems to work without it.
2422  os_delay(cnt == 99 ? 40 : (uint64_t)cnt * 50, false);
2423}
2424
2425/// Function given to ExpandGeneric() to obtain the list of group names.
2426char *get_highlight_name(expand_T *const xp, int idx)
2427  FUNC_ATTR_WARN_UNUSED_RESULT
2428{
2429  return (char *)get_highlight_name_ext(xp, idx, true);
2430}
2431
2432/// Obtain a highlight group name.
2433///
2434/// @param skip_cleared  if true don't return a cleared entry.
2435const char *get_highlight_name_ext(expand_T *xp, int idx, bool skip_cleared)
2436  FUNC_ATTR_WARN_UNUSED_RESULT
2437{
2438  if (idx < 0) {
2439    return NULL;
2440  }
2441
2442  // Items are never removed from the table, skip the ones that were cleared.
2443  if (skip_cleared && idx < highlight_ga.ga_len && hl_table[idx].sg_cleared) {
2444    return "";
2445  }
2446
2447  if (idx == highlight_ga.ga_len && include_none != 0) {
2448    return "none";
2449  } else if (idx == highlight_ga.ga_len + include_none
2450             && include_default != 0) {
2451    return "default";
2452  } else if (idx == highlight_ga.ga_len + include_none + include_default
2453             && include_link != 0) {
2454    return "link";
2455  } else if (idx == highlight_ga.ga_len + include_none + include_default + 1
2456             && include_link != 0) {
2457    return "clear";
2458  } else if (idx >= highlight_ga.ga_len) {
2459    return NULL;
2460  }
2461  return hl_table[idx].sg_name;
2462}
2463
2464color_name_table_T color_name_table[] = {
2465  // Colors from rgb.txt
2466  { "AliceBlue", RGB_(0xf0, 0xf8, 0xff) },
2467  { "AntiqueWhite", RGB_(0xfa, 0xeb, 0xd7) },
2468  { "AntiqueWhite1", RGB_(0xff, 0xef, 0xdb) },
2469  { "AntiqueWhite2", RGB_(0xee, 0xdf, 0xcc) },
2470  { "AntiqueWhite3", RGB_(0xcd, 0xc0, 0xb0) },
2471  { "AntiqueWhite4", RGB_(0x8b, 0x83, 0x78) },
2472  { "Aqua", RGB_(0x00, 0xff, 0xff) },
2473  { "Aquamarine", RGB_(0x7f, 0xff, 0xd4) },
2474  { "Aquamarine1", RGB_(0x7f, 0xff, 0xd4) },
2475  { "Aquamarine2", RGB_(0x76, 0xee, 0xc6) },
2476  { "Aquamarine3", RGB_(0x66, 0xcd, 0xaa) },
2477  { "Aquamarine4", RGB_(0x45, 0x8b, 0x74) },
2478  { "Azure", RGB_(0xf0, 0xff, 0xff) },
2479  { "Azure1", RGB_(0xf0, 0xff, 0xff) },
2480  { "Azure2", RGB_(0xe0, 0xee, 0xee) },
2481  { "Azure3", RGB_(0xc1, 0xcd, 0xcd) },
2482  { "Azure4", RGB_(0x83, 0x8b, 0x8b) },
2483  { "Beige", RGB_(0xf5, 0xf5, 0xdc) },
2484  { "Bisque", RGB_(0xff, 0xe4, 0xc4) },
2485  { "Bisque1", RGB_(0xff, 0xe4, 0xc4) },
2486  { "Bisque2", RGB_(0xee, 0xd5, 0xb7) },
2487  { "Bisque3", RGB_(0xcd, 0xb7, 0x9e) },
2488  { "Bisque4", RGB_(0x8b, 0x7d, 0x6b) },
2489  { "Black", RGB_(0x00, 0x00, 0x00) },
2490  { "BlanchedAlmond", RGB_(0xff, 0xeb, 0xcd) },
2491  { "Blue", RGB_(0x00, 0x00, 0xff) },
2492  { "Blue1", RGB_(0x0, 0x0, 0xff) },
2493  { "Blue2", RGB_(0x0, 0x0, 0xee) },
2494  { "Blue3", RGB_(0x0, 0x0, 0xcd) },
2495  { "Blue4", RGB_(0x0, 0x0, 0x8b) },
2496  { "BlueViolet", RGB_(0x8a, 0x2b, 0xe2) },
2497  { "Brown", RGB_(0xa5, 0x2a, 0x2a) },
2498  { "Brown1", RGB_(0xff, 0x40, 0x40) },
2499  { "Brown2", RGB_(0xee, 0x3b, 0x3b) },
2500  { "Brown3", RGB_(0xcd, 0x33, 0x33) },
2501  { "Brown4", RGB_(0x8b, 0x23, 0x23) },
2502  { "BurlyWood", RGB_(0xde, 0xb8, 0x87) },
2503  { "Burlywood1", RGB_(0xff, 0xd3, 0x9b) },
2504  { "Burlywood2", RGB_(0xee, 0xc5, 0x91) },
2505  { "Burlywood3", RGB_(0xcd, 0xaa, 0x7d) },
2506  { "Burlywood4", RGB_(0x8b, 0x73, 0x55) },
2507  { "CadetBlue", RGB_(0x5f, 0x9e, 0xa0) },
2508  { "CadetBlue1", RGB_(0x98, 0xf5, 0xff) },
2509  { "CadetBlue2", RGB_(0x8e, 0xe5, 0xee) },
2510  { "CadetBlue3", RGB_(0x7a, 0xc5, 0xcd) },
2511  { "CadetBlue4", RGB_(0x53, 0x86, 0x8b) },
2512  { "ChartReuse", RGB_(0x7f, 0xff, 0x00) },
2513  { "Chartreuse1", RGB_(0x7f, 0xff, 0x0) },
2514  { "Chartreuse2", RGB_(0x76, 0xee, 0x0) },
2515  { "Chartreuse3", RGB_(0x66, 0xcd, 0x0) },
2516  { "Chartreuse4", RGB_(0x45, 0x8b, 0x0) },
2517  { "Chocolate", RGB_(0xd2, 0x69, 0x1e) },
2518  { "Chocolate1", RGB_(0xff, 0x7f, 0x24) },
2519  { "Chocolate2", RGB_(0xee, 0x76, 0x21) },
2520  { "Chocolate3", RGB_(0xcd, 0x66, 0x1d) },
2521  { "Chocolate4", RGB_(0x8b, 0x45, 0x13) },
2522  { "Coral", RGB_(0xff, 0x7f, 0x50) },
2523  { "Coral1", RGB_(0xff, 0x72, 0x56) },
2524  { "Coral2", RGB_(0xee, 0x6a, 0x50) },
2525  { "Coral3", RGB_(0xcd, 0x5b, 0x45) },
2526  { "Coral4", RGB_(0x8b, 0x3e, 0x2f) },
2527  { "CornFlowerBlue", RGB_(0x64, 0x95, 0xed) },
2528  { "Cornsilk", RGB_(0xff, 0xf8, 0xdc) },
2529  { "Cornsilk1", RGB_(0xff, 0xf8, 0xdc) },
2530  { "Cornsilk2", RGB_(0xee, 0xe8, 0xcd) },
2531  { "Cornsilk3", RGB_(0xcd, 0xc8, 0xb1) },
2532  { "Cornsilk4", RGB_(0x8b, 0x88, 0x78) },
2533  { "Crimson", RGB_(0xdc, 0x14, 0x3c) },
2534  { "Cyan", RGB_(0x00, 0xff, 0xff) },
2535  { "Cyan1", RGB_(0x0, 0xff, 0xff) },
2536  { "Cyan2", RGB_(0x0, 0xee, 0xee) },
2537  { "Cyan3", RGB_(0x0, 0xcd, 0xcd) },
2538  { "Cyan4", RGB_(0x0, 0x8b, 0x8b) },
2539  { "DarkBlue", RGB_(0x00, 0x00, 0x8b) },
2540  { "DarkCyan", RGB_(0x00, 0x8b, 0x8b) },
2541  { "DarkGoldenrod", RGB_(0xb8, 0x86, 0x0b) },
2542  { "DarkGoldenrod1", RGB_(0xff, 0xb9, 0xf) },
2543  { "DarkGoldenrod2", RGB_(0xee, 0xad, 0xe) },
2544  { "DarkGoldenrod3", RGB_(0xcd, 0x95, 0xc) },
2545  { "DarkGoldenrod4", RGB_(0x8b, 0x65, 0x8) },
2546  { "DarkGray", RGB_(0xa9, 0xa9, 0xa9) },
2547  { "DarkGreen", RGB_(0x00, 0x64, 0x00) },
2548  { "DarkGrey", RGB_(0xa9, 0xa9, 0xa9) },
2549  { "DarkKhaki", RGB_(0xbd, 0xb7, 0x6b) },
2550  { "DarkMagenta", RGB_(0x8b, 0x00, 0x8b) },
2551  { "DarkOliveGreen", RGB_(0x55, 0x6b, 0x2f) },
2552  { "DarkOliveGreen1", RGB_(0xca, 0xff, 0x70) },
2553  { "DarkOliveGreen2", RGB_(0xbc, 0xee, 0x68) },
2554  { "DarkOliveGreen3", RGB_(0xa2, 0xcd, 0x5a) },
2555  { "DarkOliveGreen4", RGB_(0x6e, 0x8b, 0x3d) },
2556  { "DarkOrange", RGB_(0xff, 0x8c, 0x00) },
2557  { "DarkOrange1", RGB_(0xff, 0x7f, 0x0) },
2558  { "DarkOrange2", RGB_(0xee, 0x76, 0x0) },
2559  { "DarkOrange3", RGB_(0xcd, 0x66, 0x0) },
2560  { "DarkOrange4", RGB_(0x8b, 0x45, 0x0) },
2561  { "DarkOrchid", RGB_(0x99, 0x32, 0xcc) },
2562  { "DarkOrchid1", RGB_(0xbf, 0x3e, 0xff) },
2563  { "DarkOrchid2", RGB_(0xb2, 0x3a, 0xee) },
2564  { "DarkOrchid3", RGB_(0x9a, 0x32, 0xcd) },
2565  { "DarkOrchid4", RGB_(0x68, 0x22, 0x8b) },
2566  { "DarkRed", RGB_(0x8b, 0x00, 0x00) },
2567  { "DarkSalmon", RGB_(0xe9, 0x96, 0x7a) },
2568  { "DarkSeaGreen", RGB_(0x8f, 0xbc, 0x8f) },
2569  { "DarkSeaGreen1", RGB_(0xc1, 0xff, 0xc1) },
2570  { "DarkSeaGreen2", RGB_(0xb4, 0xee, 0xb4) },
2571  { "DarkSeaGreen3", RGB_(0x9b, 0xcd, 0x9b) },
2572  { "DarkSeaGreen4", RGB_(0x69, 0x8b, 0x69) },
2573  { "DarkSlateBlue", RGB_(0x48, 0x3d, 0x8b) },
2574  { "DarkSlateGray", RGB_(0x2f, 0x4f, 0x4f) },
2575  { "DarkSlateGray1", RGB_(0x97, 0xff, 0xff) },
2576  { "DarkSlateGray2", RGB_(0x8d, 0xee, 0xee) },
2577  { "DarkSlateGray3", RGB_(0x79, 0xcd, 0xcd) },
2578  { "DarkSlateGray4", RGB_(0x52, 0x8b, 0x8b) },
2579  { "DarkSlateGrey", RGB_(0x2f, 0x4f, 0x4f) },
2580  { "DarkTurquoise", RGB_(0x00, 0xce, 0xd1) },
2581  { "DarkViolet", RGB_(0x94, 0x00, 0xd3) },
2582  { "DarkYellow", RGB_(0xbb, 0xbb, 0x00) },
2583  { "DeepPink", RGB_(0xff, 0x14, 0x93) },
2584  { "DeepPink1", RGB_(0xff, 0x14, 0x93) },
2585  { "DeepPink2", RGB_(0xee, 0x12, 0x89) },
2586  { "DeepPink3", RGB_(0xcd, 0x10, 0x76) },
2587  { "DeepPink4", RGB_(0x8b, 0xa, 0x50) },
2588  { "DeepSkyBlue", RGB_(0x00, 0xbf, 0xff) },
2589  { "DeepSkyBlue1", RGB_(0x0, 0xbf, 0xff) },
2590  { "DeepSkyBlue2", RGB_(0x0, 0xb2, 0xee) },
2591  { "DeepSkyBlue3", RGB_(0x0, 0x9a, 0xcd) },
2592  { "DeepSkyBlue4", RGB_(0x0, 0x68, 0x8b) },
2593  { "DimGray", RGB_(0x69, 0x69, 0x69) },
2594  { "DimGrey", RGB_(0x69, 0x69, 0x69) },
2595  { "DodgerBlue", RGB_(0x1e, 0x90, 0xff) },
2596  { "DodgerBlue1", RGB_(0x1e, 0x90, 0xff) },
2597  { "DodgerBlue2", RGB_(0x1c, 0x86, 0xee) },
2598  { "DodgerBlue3", RGB_(0x18, 0x74, 0xcd) },
2599  { "DodgerBlue4", RGB_(0x10, 0x4e, 0x8b) },
2600  { "Firebrick", RGB_(0xb2, 0x22, 0x22) },
2601  { "Firebrick1", RGB_(0xff, 0x30, 0x30) },
2602  { "Firebrick2", RGB_(0xee, 0x2c, 0x2c) },
2603  { "Firebrick3", RGB_(0xcd, 0x26, 0x26) },
2604  { "Firebrick4", RGB_(0x8b, 0x1a, 0x1a) },
2605  { "FloralWhite", RGB_(0xff, 0xfa, 0xf0) },
2606  { "ForestGreen", RGB_(0x22, 0x8b, 0x22) },
2607  { "Fuchsia", RGB_(0xff, 0x00, 0xff) },
2608  { "Gainsboro", RGB_(0xdc, 0xdc, 0xdc) },
2609  { "GhostWhite", RGB_(0xf8, 0xf8, 0xff) },
2610  { "Gold", RGB_(0xff, 0xd7, 0x00) },
2611  { "Gold1", RGB_(0xff, 0xd7, 0x0) },
2612  { "Gold2", RGB_(0xee, 0xc9, 0x0) },
2613  { "Gold3", RGB_(0xcd, 0xad, 0x0) },
2614  { "Gold4", RGB_(0x8b, 0x75, 0x0) },
2615  { "Goldenrod", RGB_(0xda, 0xa5, 0x20) },
2616  { "Goldenrod1", RGB_(0xff, 0xc1, 0x25) },
2617  { "Goldenrod2", RGB_(0xee, 0xb4, 0x22) },
2618  { "Goldenrod3", RGB_(0xcd, 0x9b, 0x1d) },
2619  { "Goldenrod4", RGB_(0x8b, 0x69, 0x14) },
2620  { "Gray", RGB_(0x80, 0x80, 0x80) },
2621  { "Gray0", RGB_(0x0, 0x0, 0x0) },
2622  { "Gray1", RGB_(0x3, 0x3, 0x3) },
2623  { "Gray10", RGB_(0x1a, 0x1a, 0x1a) },
2624  { "Gray100", RGB_(0xff, 0xff, 0xff) },
2625  { "Gray11", RGB_(0x1c, 0x1c, 0x1c) },
2626  { "Gray12", RGB_(0x1f, 0x1f, 0x1f) },
2627  { "Gray13", RGB_(0x21, 0x21, 0x21) },
2628  { "Gray14", RGB_(0x24, 0x24, 0x24) },
2629  { "Gray15", RGB_(0x26, 0x26, 0x26) },
2630  { "Gray16", RGB_(0x29, 0x29, 0x29) },
2631  { "Gray17", RGB_(0x2b, 0x2b, 0x2b) },
2632  { "Gray18", RGB_(0x2e, 0x2e, 0x2e) },
2633  { "Gray19", RGB_(0x30, 0x30, 0x30) },
2634  { "Gray2", RGB_(0x5, 0x5, 0x5) },
2635  { "Gray20", RGB_(0x33, 0x33, 0x33) },
2636  { "Gray21", RGB_(0x36, 0x36, 0x36) },
2637  { "Gray22", RGB_(0x38, 0x38, 0x38) },
2638  { "Gray23", RGB_(0x3b, 0x3b, 0x3b) },
2639  { "Gray24", RGB_(0x3d, 0x3d, 0x3d) },
2640  { "Gray25", RGB_(0x40, 0x40, 0x40) },
2641  { "Gray26", RGB_(0x42, 0x42, 0x42) },
2642  { "Gray27", RGB_(0x45, 0x45, 0x45) },
2643  { "Gray28", RGB_(0x47, 0x47, 0x47) },
2644  { "Gray29", RGB_(0x4a, 0x4a, 0x4a) },
2645  { "Gray3", RGB_(0x8, 0x8, 0x8) },
2646  { "Gray30", RGB_(0x4d, 0x4d, 0x4d) },
2647  { "Gray31", RGB_(0x4f, 0x4f, 0x4f) },
2648  { "Gray32", RGB_(0x52, 0x52, 0x52) },
2649  { "Gray33", RGB_(0x54, 0x54, 0x54) },
2650  { "Gray34", RGB_(0x57, 0x57, 0x57) },
2651  { "Gray35", RGB_(0x59, 0x59, 0x59) },
2652  { "Gray36", RGB_(0x5c, 0x5c, 0x5c) },
2653  { "Gray37", RGB_(0x5e, 0x5e, 0x5e) },
2654  { "Gray38", RGB_(0x61, 0x61, 0x61) },
2655  { "Gray39", RGB_(0x63, 0x63, 0x63) },
2656  { "Gray4", RGB_(0xa, 0xa, 0xa) },
2657  { "Gray40", RGB_(0x66, 0x66, 0x66) },
2658  { "Gray41", RGB_(0x69, 0x69, 0x69) },
2659  { "Gray42", RGB_(0x6b, 0x6b, 0x6b) },
2660  { "Gray43", RGB_(0x6e, 0x6e, 0x6e) },
2661  { "Gray44", RGB_(0x70, 0x70, 0x70) },
2662  { "Gray45", RGB_(0x73, 0x73, 0x73) },
2663  { "Gray46", RGB_(0x75, 0x75, 0x75) },
2664  { "Gray47", RGB_(0x78, 0x78, 0x78) },
2665  { "Gray48", RGB_(0x7a, 0x7a, 0x7a) },
2666  { "Gray49", RGB_(0x7d, 0x7d, 0x7d) },
2667  { "Gray5", RGB_(0xd, 0xd, 0xd) },
2668  { "Gray50", RGB_(0x7f, 0x7f, 0x7f) },
2669  { "Gray51", RGB_(0x82, 0x82, 0x82) },
2670  { "Gray52", RGB_(0x85, 0x85, 0x85) },
2671  { "Gray53", RGB_(0x87, 0x87, 0x87) },
2672  { "Gray54", RGB_(0x8a, 0x8a, 0x8a) },
2673  { "Gray55", RGB_(0x8c, 0x8c, 0x8c) },
2674  { "Gray56", RGB_(0x8f, 0x8f, 0x8f) },
2675  { "Gray57", RGB_(0x91, 0x91, 0x91) },
2676  { "Gray58", RGB_(0x94, 0x94, 0x94) },
2677  { "Gray59", RGB_(0x96, 0x96, 0x96) },
2678  { "Gray6", RGB_(0xf, 0xf, 0xf) },
2679  { "Gray60", RGB_(0x99, 0x99, 0x99) },
2680  { "Gray61", RGB_(0x9c, 0x9c, 0x9c) },
2681  { "Gray62", RGB_(0x9e, 0x9e, 0x9e) },
2682  { "Gray63", RGB_(0xa1, 0xa1, 0xa1) },
2683  { "Gray64", RGB_(0xa3, 0xa3, 0xa3) },
2684  { "Gray65", RGB_(0xa6, 0xa6, 0xa6) },
2685  { "Gray66", RGB_(0xa8, 0xa8, 0xa8) },
2686  { "Gray67", RGB_(0xab, 0xab, 0xab) },
2687  { "Gray68", RGB_(0xad, 0xad, 0xad) },
2688  { "Gray69", RGB_(0xb0, 0xb0, 0xb0) },
2689  { "Gray7", RGB_(0x12, 0x12, 0x12) },
2690  { "Gray70", RGB_(0xb3, 0xb3, 0xb3) },
2691  { "Gray71", RGB_(0xb5, 0xb5, 0xb5) },
2692  { "Gray72", RGB_(0xb8, 0xb8, 0xb8) },
2693  { "Gray73", RGB_(0xba, 0xba, 0xba) },
2694  { "Gray74", RGB_(0xbd, 0xbd, 0xbd) },
2695  { "Gray75", RGB_(0xbf, 0xbf, 0xbf) },
2696  { "Gray76", RGB_(0xc2, 0xc2, 0xc2) },
2697  { "Gray77", RGB_(0xc4, 0xc4, 0xc4) },
2698  { "Gray78", RGB_(0xc7, 0xc7, 0xc7) },
2699  { "Gray79", RGB_(0xc9, 0xc9, 0xc9) },
2700  { "Gray8", RGB_(0x14, 0x14, 0x14) },
2701  { "Gray80", RGB_(0xcc, 0xcc, 0xcc) },
2702  { "Gray81", RGB_(0xcf, 0xcf, 0xcf) },
2703  { "Gray82", RGB_(0xd1, 0xd1, 0xd1) },
2704  { "Gray83", RGB_(0xd4, 0xd4, 0xd4) },
2705  { "Gray84", RGB_(0xd6, 0xd6, 0xd6) },
2706  { "Gray85", RGB_(0xd9, 0xd9, 0xd9) },
2707  { "Gray86", RGB_(0xdb, 0xdb, 0xdb) },
2708  { "Gray87", RGB_(0xde, 0xde, 0xde) },
2709  { "Gray88", RGB_(0xe0, 0xe0, 0xe0) },
2710  { "Gray89", RGB_(0xe3, 0xe3, 0xe3) },
2711  { "Gray9", RGB_(0x17, 0x17, 0x17) },
2712  { "Gray90", RGB_(0xe5, 0xe5, 0xe5) },
2713  { "Gray91", RGB_(0xe8, 0xe8, 0xe8) },
2714  { "Gray92", RGB_(0xeb, 0xeb, 0xeb) },
2715  { "Gray93", RGB_(0xed, 0xed, 0xed) },
2716  { "Gray94", RGB_(0xf0, 0xf0, 0xf0) },
2717  { "Gray95", RGB_(0xf2, 0xf2, 0xf2) },
2718  { "Gray96", RGB_(0xf5, 0xf5, 0xf5) },
2719  { "Gray97", RGB_(0xf7, 0xf7, 0xf7) },
2720  { "Gray98", RGB_(0xfa, 0xfa, 0xfa) },
2721  { "Gray99", RGB_(0xfc, 0xfc, 0xfc) },
2722  { "Green", RGB_(0x00, 0x80, 0x00) },
2723  { "Green1", RGB_(0x0, 0xff, 0x0) },
2724  { "Green2", RGB_(0x0, 0xee, 0x0) },
2725  { "Green3", RGB_(0x0, 0xcd, 0x0) },
2726  { "Green4", RGB_(0x0, 0x8b, 0x0) },
2727  { "GreenYellow", RGB_(0xad, 0xff, 0x2f) },
2728  { "Grey", RGB_(0x80, 0x80, 0x80) },
2729  { "Grey0", RGB_(0x0, 0x0, 0x0) },
2730  { "Grey1", RGB_(0x3, 0x3, 0x3) },
2731  { "Grey10", RGB_(0x1a, 0x1a, 0x1a) },
2732  { "Grey100", RGB_(0xff, 0xff, 0xff) },
2733  { "Grey11", RGB_(0x1c, 0x1c, 0x1c) },
2734  { "Grey12", RGB_(0x1f, 0x1f, 0x1f) },
2735  { "Grey13", RGB_(0x21, 0x21, 0x21) },
2736  { "Grey14", RGB_(0x24, 0x24, 0x24) },
2737  { "Grey15", RGB_(0x26, 0x26, 0x26) },
2738  { "Grey16", RGB_(0x29, 0x29, 0x29) },
2739  { "Grey17", RGB_(0x2b, 0x2b, 0x2b) },
2740  { "Grey18", RGB_(0x2e, 0x2e, 0x2e) },
2741  { "Grey19", RGB_(0x30, 0x30, 0x30) },
2742  { "Grey2", RGB_(0x5, 0x5, 0x5) },
2743  { "Grey20", RGB_(0x33, 0x33, 0x33) },
2744  { "Grey21", RGB_(0x36, 0x36, 0x36) },
2745  { "Grey22", RGB_(0x38, 0x38, 0x38) },
2746  { "Grey23", RGB_(0x3b, 0x3b, 0x3b) },
2747  { "Grey24", RGB_(0x3d, 0x3d, 0x3d) },
2748  { "Grey25", RGB_(0x40, 0x40, 0x40) },
2749  { "Grey26", RGB_(0x42, 0x42, 0x42) },
2750  { "Grey27", RGB_(0x45, 0x45, 0x45) },
2751  { "Grey28", RGB_(0x47, 0x47, 0x47) },
2752  { "Grey29", RGB_(0x4a, 0x4a, 0x4a) },
2753  { "Grey3", RGB_(0x8, 0x8, 0x8) },
2754  { "Grey30", RGB_(0x4d, 0x4d, 0x4d) },
2755  { "Grey31", RGB_(0x4f, 0x4f, 0x4f) },
2756  { "Grey32", RGB_(0x52, 0x52, 0x52) },
2757  { "Grey33", RGB_(0x54, 0x54, 0x54) },
2758  { "Grey34", RGB_(0x57, 0x57, 0x57) },
2759  { "Grey35", RGB_(0x59, 0x59, 0x59) },
2760  { "Grey36", RGB_(0x5c, 0x5c, 0x5c) },
2761  { "Grey37", RGB_(0x5e, 0x5e, 0x5e) },
2762  { "Grey38", RGB_(0x61, 0x61, 0x61) },
2763  { "Grey39", RGB_(0x63, 0x63, 0x63) },
2764  { "Grey4", RGB_(0xa, 0xa, 0xa) },
2765  { "Grey40", RGB_(0x66, 0x66, 0x66) },
2766  { "Grey41", RGB_(0x69, 0x69, 0x69) },
2767  { "Grey42", RGB_(0x6b, 0x6b, 0x6b) },
2768  { "Grey43", RGB_(0x6e, 0x6e, 0x6e) },
2769  { "Grey44", RGB_(0x70, 0x70, 0x70) },
2770  { "Grey45", RGB_(0x73, 0x73, 0x73) },
2771  { "Grey46", RGB_(0x75, 0x75, 0x75) },
2772  { "Grey47", RGB_(0x78, 0x78, 0x78) },
2773  { "Grey48", RGB_(0x7a, 0x7a, 0x7a) },
2774  { "Grey49", RGB_(0x7d, 0x7d, 0x7d) },
2775  { "Grey5", RGB_(0xd, 0xd, 0xd) },
2776  { "Grey50", RGB_(0x7f, 0x7f, 0x7f) },
2777  { "Grey51", RGB_(0x82, 0x82, 0x82) },
2778  { "Grey52", RGB_(0x85, 0x85, 0x85) },
2779  { "Grey53", RGB_(0x87, 0x87, 0x87) },
2780  { "Grey54", RGB_(0x8a, 0x8a, 0x8a) },
2781  { "Grey55", RGB_(0x8c, 0x8c, 0x8c) },
2782  { "Grey56", RGB_(0x8f, 0x8f, 0x8f) },
2783  { "Grey57", RGB_(0x91, 0x91, 0x91) },
2784  { "Grey58", RGB_(0x94, 0x94, 0x94) },
2785  { "Grey59", RGB_(0x96, 0x96, 0x96) },
2786  { "Grey6", RGB_(0xf, 0xf, 0xf) },
2787  { "Grey60", RGB_(0x99, 0x99, 0x99) },
2788  { "Grey61", RGB_(0x9c, 0x9c, 0x9c) },
2789  { "Grey62", RGB_(0x9e, 0x9e, 0x9e) },
2790  { "Grey63", RGB_(0xa1, 0xa1, 0xa1) },
2791  { "Grey64", RGB_(0xa3, 0xa3, 0xa3) },
2792  { "Grey65", RGB_(0xa6, 0xa6, 0xa6) },
2793  { "Grey66", RGB_(0xa8, 0xa8, 0xa8) },
2794  { "Grey67", RGB_(0xab, 0xab, 0xab) },
2795  { "Grey68", RGB_(0xad, 0xad, 0xad) },
2796  { "Grey69", RGB_(0xb0, 0xb0, 0xb0) },
2797  { "Grey7", RGB_(0x12, 0x12, 0x12) },
2798  { "Grey70", RGB_(0xb3, 0xb3, 0xb3) },
2799  { "Grey71", RGB_(0xb5, 0xb5, 0xb5) },
2800  { "Grey72", RGB_(0xb8, 0xb8, 0xb8) },
2801  { "Grey73", RGB_(0xba, 0xba, 0xba) },
2802  { "Grey74", RGB_(0xbd, 0xbd, 0xbd) },
2803  { "Grey75", RGB_(0xbf, 0xbf, 0xbf) },
2804  { "Grey76", RGB_(0xc2, 0xc2, 0xc2) },
2805  { "Grey77", RGB_(0xc4, 0xc4, 0xc4) },
2806  { "Grey78", RGB_(0xc7, 0xc7, 0xc7) },
2807  { "Grey79", RGB_(0xc9, 0xc9, 0xc9) },
2808  { "Grey8", RGB_(0x14, 0x14, 0x14) },
2809  { "Grey80", RGB_(0xcc, 0xcc, 0xcc) },
2810  { "Grey81", RGB_(0xcf, 0xcf, 0xcf) },
2811  { "Grey82", RGB_(0xd1, 0xd1, 0xd1) },
2812  { "Grey83", RGB_(0xd4, 0xd4, 0xd4) },
2813  { "Grey84", RGB_(0xd6, 0xd6, 0xd6) },
2814  { "Grey85", RGB_(0xd9, 0xd9, 0xd9) },
2815  { "Grey86", RGB_(0xdb, 0xdb, 0xdb) },
2816  { "Grey87", RGB_(0xde, 0xde, 0xde) },
2817  { "Grey88", RGB_(0xe0, 0xe0, 0xe0) },
2818  { "Grey89", RGB_(0xe3, 0xe3, 0xe3) },
2819  { "Grey9", RGB_(0x17, 0x17, 0x17) },
2820  { "Grey90", RGB_(0xe5, 0xe5, 0xe5) },
2821  { "Grey91", RGB_(0xe8, 0xe8, 0xe8) },
2822  { "Grey92", RGB_(0xeb, 0xeb, 0xeb) },
2823  { "Grey93", RGB_(0xed, 0xed, 0xed) },
2824  { "Grey94", RGB_(0xf0, 0xf0, 0xf0) },
2825  { "Grey95", RGB_(0xf2, 0xf2, 0xf2) },
2826  { "Grey96", RGB_(0xf5, 0xf5, 0xf5) },
2827  { "Grey97", RGB_(0xf7, 0xf7, 0xf7) },
2828  { "Grey98", RGB_(0xfa, 0xfa, 0xfa) },
2829  { "Grey99", RGB_(0xfc, 0xfc, 0xfc) },
2830  { "Honeydew", RGB_(0xf0, 0xff, 0xf0) },
2831  { "Honeydew1", RGB_(0xf0, 0xff, 0xf0) },
2832  { "Honeydew2", RGB_(0xe0, 0xee, 0xe0) },
2833  { "Honeydew3", RGB_(0xc1, 0xcd, 0xc1) },
2834  { "Honeydew4", RGB_(0x83, 0x8b, 0x83) },
2835  { "HotPink", RGB_(0xff, 0x69, 0xb4) },
2836  { "HotPink1", RGB_(0xff, 0x6e, 0xb4) },
2837  { "HotPink2", RGB_(0xee, 0x6a, 0xa7) },
2838  { "HotPink3", RGB_(0xcd, 0x60, 0x90) },
2839  { "HotPink4", RGB_(0x8b, 0x3a, 0x62) },
2840  { "IndianRed", RGB_(0xcd, 0x5c, 0x5c) },
2841  { "IndianRed1", RGB_(0xff, 0x6a, 0x6a) },
2842  { "IndianRed2", RGB_(0xee, 0x63, 0x63) },
2843  { "IndianRed3", RGB_(0xcd, 0x55, 0x55) },
2844  { "IndianRed4", RGB_(0x8b, 0x3a, 0x3a) },
2845  { "Indigo", RGB_(0x4b, 0x00, 0x82) },
2846  { "Ivory", RGB_(0xff, 0xff, 0xf0) },
2847  { "Ivory1", RGB_(0xff, 0xff, 0xf0) },
2848  { "Ivory2", RGB_(0xee, 0xee, 0xe0) },
2849  { "Ivory3", RGB_(0xcd, 0xcd, 0xc1) },
2850  { "Ivory4", RGB_(0x8b, 0x8b, 0x83) },
2851  { "Khaki", RGB_(0xf0, 0xe6, 0x8c) },
2852  { "Khaki1", RGB_(0xff, 0xf6, 0x8f) },
2853  { "Khaki2", RGB_(0xee, 0xe6, 0x85) },
2854  { "Khaki3", RGB_(0xcd, 0xc6, 0x73) },
2855  { "Khaki4", RGB_(0x8b, 0x86, 0x4e) },
2856  { "Lavender", RGB_(0xe6, 0xe6, 0xfa) },
2857  { "LavenderBlush", RGB_(0xff, 0xf0, 0xf5) },
2858  { "LavenderBlush1", RGB_(0xff, 0xf0, 0xf5) },
2859  { "LavenderBlush2", RGB_(0xee, 0xe0, 0xe5) },
2860  { "LavenderBlush3", RGB_(0xcd, 0xc1, 0xc5) },
2861  { "LavenderBlush4", RGB_(0x8b, 0x83, 0x86) },
2862  { "LawnGreen", RGB_(0x7c, 0xfc, 0x00) },
2863  { "LemonChiffon", RGB_(0xff, 0xfa, 0xcd) },
2864  { "LemonChiffon1", RGB_(0xff, 0xfa, 0xcd) },
2865  { "LemonChiffon2", RGB_(0xee, 0xe9, 0xbf) },
2866  { "LemonChiffon3", RGB_(0xcd, 0xc9, 0xa5) },
2867  { "LemonChiffon4", RGB_(0x8b, 0x89, 0x70) },
2868  { "LightBlue", RGB_(0xad, 0xd8, 0xe6) },
2869  { "LightBlue1", RGB_(0xbf, 0xef, 0xff) },
2870  { "LightBlue2", RGB_(0xb2, 0xdf, 0xee) },
2871  { "LightBlue3", RGB_(0x9a, 0xc0, 0xcd) },
2872  { "LightBlue4", RGB_(0x68, 0x83, 0x8b) },
2873  { "LightCoral", RGB_(0xf0, 0x80, 0x80) },
2874  { "LightCyan", RGB_(0xe0, 0xff, 0xff) },
2875  { "LightCyan1", RGB_(0xe0, 0xff, 0xff) },
2876  { "LightCyan2", RGB_(0xd1, 0xee, 0xee) },
2877  { "LightCyan3", RGB_(0xb4, 0xcd, 0xcd) },
2878  { "LightCyan4", RGB_(0x7a, 0x8b, 0x8b) },
2879  { "LightGoldenrod", RGB_(0xee, 0xdd, 0x82) },
2880  { "LightGoldenrod1", RGB_(0xff, 0xec, 0x8b) },
2881  { "LightGoldenrod2", RGB_(0xee, 0xdc, 0x82) },
2882  { "LightGoldenrod3", RGB_(0xcd, 0xbe, 0x70) },
2883  { "LightGoldenrod4", RGB_(0x8b, 0x81, 0x4c) },
2884  { "LightGoldenrodYellow", RGB_(0xfa, 0xfa, 0xd2) },
2885  { "LightGray", RGB_(0xd3, 0xd3, 0xd3) },
2886  { "LightGreen", RGB_(0x90, 0xee, 0x90) },
2887  { "LightGrey", RGB_(0xd3, 0xd3, 0xd3) },
2888  { "LightMagenta", RGB_(0xff, 0xbb, 0xff) },
2889  { "LightPink", RGB_(0xff, 0xb6, 0xc1) },
2890  { "LightPink1", RGB_(0xff, 0xae, 0xb9) },
2891  { "LightPink2", RGB_(0xee, 0xa2, 0xad) },
2892  { "LightPink3", RGB_(0xcd, 0x8c, 0x95) },
2893  { "LightPink4", RGB_(0x8b, 0x5f, 0x65) },
2894  { "LightRed", RGB_(0xff, 0xbb, 0xbb) },
2895  { "LightSalmon", RGB_(0xff, 0xa0, 0x7a) },
2896  { "LightSalmon1", RGB_(0xff, 0xa0, 0x7a) },
2897  { "LightSalmon2", RGB_(0xee, 0x95, 0x72) },
2898  { "LightSalmon3", RGB_(0xcd, 0x81, 0x62) },
2899  { "LightSalmon4", RGB_(0x8b, 0x57, 0x42) },
2900  { "LightSeaGreen", RGB_(0x20, 0xb2, 0xaa) },
2901  { "LightSkyBlue", RGB_(0x87, 0xce, 0xfa) },
2902  { "LightSkyBlue1", RGB_(0xb0, 0xe2, 0xff) },
2903  { "LightSkyBlue2", RGB_(0xa4, 0xd3, 0xee) },
2904  { "LightSkyBlue3", RGB_(0x8d, 0xb6, 0xcd) },
2905  { "LightSkyBlue4", RGB_(0x60, 0x7b, 0x8b) },
2906  { "LightSlateBlue", RGB_(0x84, 0x70, 0xff) },
2907  { "LightSlateGray", RGB_(0x77, 0x88, 0x99) },
2908  { "LightSlateGrey", RGB_(0x77, 0x88, 0x99) },
2909  { "LightSteelBlue", RGB_(0xb0, 0xc4, 0xde) },
2910  { "LightSteelBlue1", RGB_(0xca, 0xe1, 0xff) },
2911  { "LightSteelBlue2", RGB_(0xbc, 0xd2, 0xee) },
2912  { "LightSteelBlue3", RGB_(0xa2, 0xb5, 0xcd) },
2913  { "LightSteelBlue4", RGB_(0x6e, 0x7b, 0x8b) },
2914  { "LightYellow", RGB_(0xff, 0xff, 0xe0) },
2915  { "LightYellow1", RGB_(0xff, 0xff, 0xe0) },
2916  { "LightYellow2", RGB_(0xee, 0xee, 0xd1) },
2917  { "LightYellow3", RGB_(0xcd, 0xcd, 0xb4) },
2918  { "LightYellow4", RGB_(0x8b, 0x8b, 0x7a) },
2919  { "Lime", RGB_(0x00, 0xff, 0x00) },
2920  { "LimeGreen", RGB_(0x32, 0xcd, 0x32) },
2921  { "Linen", RGB_(0xfa, 0xf0, 0xe6) },
2922  { "Magenta", RGB_(0xff, 0x00, 0xff) },
2923  { "Magenta1", RGB_(0xff, 0x0, 0xff) },
2924  { "Magenta2", RGB_(0xee, 0x0, 0xee) },
2925  { "Magenta3", RGB_(0xcd, 0x0, 0xcd) },
2926  { "Magenta4", RGB_(0x8b, 0x0, 0x8b) },
2927  { "Maroon", RGB_(0x80, 0x00, 0x00) },
2928  { "Maroon1", RGB_(0xff, 0x34, 0xb3) },
2929  { "Maroon2", RGB_(0xee, 0x30, 0xa7) },
2930  { "Maroon3", RGB_(0xcd, 0x29, 0x90) },
2931  { "Maroon4", RGB_(0x8b, 0x1c, 0x62) },
2932  { "MediumAquamarine", RGB_(0x66, 0xcd, 0xaa) },
2933  { "MediumBlue", RGB_(0x00, 0x00, 0xcd) },
2934  { "MediumOrchid", RGB_(0xba, 0x55, 0xd3) },
2935  { "MediumOrchid1", RGB_(0xe0, 0x66, 0xff) },
2936  { "MediumOrchid2", RGB_(0xd1, 0x5f, 0xee) },
2937  { "MediumOrchid3", RGB_(0xb4, 0x52, 0xcd) },
2938  { "MediumOrchid4", RGB_(0x7a, 0x37, 0x8b) },
2939  { "MediumPurple", RGB_(0x93, 0x70, 0xdb) },
2940  { "MediumPurple1", RGB_(0xab, 0x82, 0xff) },
2941  { "MediumPurple2", RGB_(0x9f, 0x79, 0xee) },
2942  { "MediumPurple3", RGB_(0x89, 0x68, 0xcd) },
2943  { "MediumPurple4", RGB_(0x5d, 0x47, 0x8b) },
2944  { "MediumSeaGreen", RGB_(0x3c, 0xb3, 0x71) },
2945  { "MediumSlateBlue", RGB_(0x7b, 0x68, 0xee) },
2946  { "MediumSpringGreen", RGB_(0x00, 0xfa, 0x9a) },
2947  { "MediumTurquoise", RGB_(0x48, 0xd1, 0xcc) },
2948  { "MediumVioletRed", RGB_(0xc7, 0x15, 0x85) },
2949  { "MidnightBlue", RGB_(0x19, 0x19, 0x70) },
2950  { "MintCream", RGB_(0xf5, 0xff, 0xfa) },
2951  { "MistyRose", RGB_(0xff, 0xe4, 0xe1) },
2952  { "MistyRose1", RGB_(0xff, 0xe4, 0xe1) },
2953  { "MistyRose2", RGB_(0xee, 0xd5, 0xd2) },
2954  { "MistyRose3", RGB_(0xcd, 0xb7, 0xb5) },
2955  { "MistyRose4", RGB_(0x8b, 0x7d, 0x7b) },
2956  { "Moccasin", RGB_(0xff, 0xe4, 0xb5) },
2957  { "NavajoWhite", RGB_(0xff, 0xde, 0xad) },
2958  { "NavajoWhite1", RGB_(0xff, 0xde, 0xad) },
2959  { "NavajoWhite2", RGB_(0xee, 0xcf, 0xa1) },
2960  { "NavajoWhite3", RGB_(0xcd, 0xb3, 0x8b) },
2961  { "NavajoWhite4", RGB_(0x8b, 0x79, 0x5e) },
2962  { "Navy", RGB_(0x00, 0x00, 0x80) },
2963  { "NavyBlue", RGB_(0x0, 0x0, 0x80) },
2964  // Default Neovim palettes.
2965  // Dark/light palette is used for background in dark/light color scheme and
2966  // for foreground in light/dark color scheme.
2967  { "NvimDarkBlue", RGB_(0x00, 0x4c, 0x73) },
2968  { "NvimDarkCyan", RGB_(0x00, 0x73, 0x73) },
2969  { "NvimDarkGray1", RGB_(0x07, 0x08, 0x0d) },
2970  { "NvimDarkGray2", RGB_(0x14, 0x16, 0x1b) },
2971  { "NvimDarkGray3", RGB_(0x2c, 0x2e, 0x33) },
2972  { "NvimDarkGray4", RGB_(0x4f, 0x52, 0x58) },
2973  { "NvimDarkGreen", RGB_(0x00, 0x55, 0x23) },
2974  { "NvimDarkGrey1", RGB_(0x07, 0x08, 0x0d) },
2975  { "NvimDarkGrey2", RGB_(0x14, 0x16, 0x1b) },
2976  { "NvimDarkGrey3", RGB_(0x2c, 0x2e, 0x33) },
2977  { "NvimDarkGrey4", RGB_(0x4f, 0x52, 0x58) },
2978  { "NvimDarkMagenta", RGB_(0x47, 0x00, 0x45) },
2979  { "NvimDarkRed", RGB_(0x59, 0x00, 0x08) },
2980  { "NvimDarkYellow", RGB_(0x6b, 0x53, 0x00) },
2981  { "NvimLightBlue", RGB_(0xa6, 0xdb, 0xff) },
2982  { "NvimLightCyan", RGB_(0x8c, 0xf8, 0xf7) },
2983  { "NvimLightGray1", RGB_(0xee, 0xf1, 0xf8) },
2984  { "NvimLightGray2", RGB_(0xe0, 0xe2, 0xea) },
2985  { "NvimLightGray3", RGB_(0xc4, 0xc6, 0xcd) },
2986  { "NvimLightGray4", RGB_(0x9b, 0x9e, 0xa4) },
2987  { "NvimLightGreen", RGB_(0xb3, 0xf6, 0xc0) },
2988  { "NvimLightGrey1", RGB_(0xee, 0xf1, 0xf8) },
2989  { "NvimLightGrey2", RGB_(0xe0, 0xe2, 0xea) },
2990  { "NvimLightGrey3", RGB_(0xc4, 0xc6, 0xcd) },
2991  { "NvimLightGrey4", RGB_(0x9b, 0x9e, 0xa4) },
2992  { "NvimLightMagenta", RGB_(0xff, 0xca, 0xff) },
2993  { "NvimLightRed", RGB_(0xff, 0xc0, 0xb9) },
2994  { "NvimLightYellow", RGB_(0xfc, 0xe0, 0x94) },
2995  { "OldLace", RGB_(0xfd, 0xf5, 0xe6) },
2996  { "Olive", RGB_(0x80, 0x80, 0x00) },
2997  { "OliveDrab", RGB_(0x6b, 0x8e, 0x23) },
2998  { "OliveDrab1", RGB_(0xc0, 0xff, 0x3e) },
2999  { "OliveDrab2", RGB_(0xb3, 0xee, 0x3a) },
3000  { "OliveDrab3", RGB_(0x9a, 0xcd, 0x32) },
3001  { "OliveDrab4", RGB_(0x69, 0x8b, 0x22) },
3002  { "Orange", RGB_(0xff, 0xa5, 0x00) },
3003  { "Orange1", RGB_(0xff, 0xa5, 0x0) },
3004  { "Orange2", RGB_(0xee, 0x9a, 0x0) },
3005  { "Orange3", RGB_(0xcd, 0x85, 0x0) },
3006  { "Orange4", RGB_(0x8b, 0x5a, 0x0) },
3007  { "OrangeRed", RGB_(0xff, 0x45, 0x00) },
3008  { "OrangeRed1", RGB_(0xff, 0x45, 0x0) },
3009  { "OrangeRed2", RGB_(0xee, 0x40, 0x0) },
3010  { "OrangeRed3", RGB_(0xcd, 0x37, 0x0) },
3011  { "OrangeRed4", RGB_(0x8b, 0x25, 0x0) },
3012  { "Orchid", RGB_(0xda, 0x70, 0xd6) },
3013  { "Orchid1", RGB_(0xff, 0x83, 0xfa) },
3014  { "Orchid2", RGB_(0xee, 0x7a, 0xe9) },
3015  { "Orchid3", RGB_(0xcd, 0x69, 0xc9) },
3016  { "Orchid4", RGB_(0x8b, 0x47, 0x89) },
3017  { "PaleGoldenrod", RGB_(0xee, 0xe8, 0xaa) },
3018  { "PaleGreen", RGB_(0x98, 0xfb, 0x98) },
3019  { "PaleGreen1", RGB_(0x9a, 0xff, 0x9a) },
3020  { "PaleGreen2", RGB_(0x90, 0xee, 0x90) },
3021  { "PaleGreen3", RGB_(0x7c, 0xcd, 0x7c) },
3022  { "PaleGreen4", RGB_(0x54, 0x8b, 0x54) },
3023  { "PaleTurquoise", RGB_(0xaf, 0xee, 0xee) },
3024  { "PaleTurquoise1", RGB_(0xbb, 0xff, 0xff) },
3025  { "PaleTurquoise2", RGB_(0xae, 0xee, 0xee) },
3026  { "PaleTurquoise3", RGB_(0x96, 0xcd, 0xcd) },
3027  { "PaleTurquoise4", RGB_(0x66, 0x8b, 0x8b) },
3028  { "PaleVioletRed", RGB_(0xdb, 0x70, 0x93) },
3029  { "PaleVioletRed1", RGB_(0xff, 0x82, 0xab) },
3030  { "PaleVioletRed2", RGB_(0xee, 0x79, 0x9f) },
3031  { "PaleVioletRed3", RGB_(0xcd, 0x68, 0x89) },
3032  { "PaleVioletRed4", RGB_(0x8b, 0x47, 0x5d) },
3033  { "PapayaWhip", RGB_(0xff, 0xef, 0xd5) },
3034  { "PeachPuff", RGB_(0xff, 0xda, 0xb9) },
3035  { "PeachPuff1", RGB_(0xff, 0xda, 0xb9) },
3036  { "PeachPuff2", RGB_(0xee, 0xcb, 0xad) },
3037  { "PeachPuff3", RGB_(0xcd, 0xaf, 0x95) },
3038  { "PeachPuff4", RGB_(0x8b, 0x77, 0x65) },
3039  { "Peru", RGB_(0xcd, 0x85, 0x3f) },
3040  { "Pink", RGB_(0xff, 0xc0, 0xcb) },
3041  { "Pink1", RGB_(0xff, 0xb5, 0xc5) },
3042  { "Pink2", RGB_(0xee, 0xa9, 0xb8) },
3043  { "Pink3", RGB_(0xcd, 0x91, 0x9e) },
3044  { "Pink4", RGB_(0x8b, 0x63, 0x6c) },
3045  { "Plum", RGB_(0xdd, 0xa0, 0xdd) },
3046  { "Plum1", RGB_(0xff, 0xbb, 0xff) },
3047  { "Plum2", RGB_(0xee, 0xae, 0xee) },
3048  { "Plum3", RGB_(0xcd, 0x96, 0xcd) },
3049  { "Plum4", RGB_(0x8b, 0x66, 0x8b) },
3050  { "PowderBlue", RGB_(0xb0, 0xe0, 0xe6) },
3051  { "Purple", RGB_(0x80, 0x00, 0x80) },
3052  { "Purple1", RGB_(0x9b, 0x30, 0xff) },
3053  { "Purple2", RGB_(0x91, 0x2c, 0xee) },
3054  { "Purple3", RGB_(0x7d, 0x26, 0xcd) },
3055  { "Purple4", RGB_(0x55, 0x1a, 0x8b) },
3056  { "RebeccaPurple", RGB_(0x66, 0x33, 0x99) },
3057  { "Red", RGB_(0xff, 0x00, 0x00) },
3058  { "Red1", RGB_(0xff, 0x0, 0x0) },
3059  { "Red2", RGB_(0xee, 0x0, 0x0) },
3060  { "Red3", RGB_(0xcd, 0x0, 0x0) },
3061  { "Red4", RGB_(0x8b, 0x0, 0x0) },
3062  { "RosyBrown", RGB_(0xbc, 0x8f, 0x8f) },
3063  { "RosyBrown1", RGB_(0xff, 0xc1, 0xc1) },
3064  { "RosyBrown2", RGB_(0xee, 0xb4, 0xb4) },
3065  { "RosyBrown3", RGB_(0xcd, 0x9b, 0x9b) },
3066  { "RosyBrown4", RGB_(0x8b, 0x69, 0x69) },
3067  { "RoyalBlue", RGB_(0x41, 0x69, 0xe1) },
3068  { "RoyalBlue1", RGB_(0x48, 0x76, 0xff) },
3069  { "RoyalBlue2", RGB_(0x43, 0x6e, 0xee) },
3070  { "RoyalBlue3", RGB_(0x3a, 0x5f, 0xcd) },
3071  { "RoyalBlue4", RGB_(0x27, 0x40, 0x8b) },
3072  { "SaddleBrown", RGB_(0x8b, 0x45, 0x13) },
3073  { "Salmon", RGB_(0xfa, 0x80, 0x72) },
3074  { "Salmon1", RGB_(0xff, 0x8c, 0x69) },
3075  { "Salmon2", RGB_(0xee, 0x82, 0x62) },
3076  { "Salmon3", RGB_(0xcd, 0x70, 0x54) },
3077  { "Salmon4", RGB_(0x8b, 0x4c, 0x39) },
3078  { "SandyBrown", RGB_(0xf4, 0xa4, 0x60) },
3079  { "SeaGreen", RGB_(0x2e, 0x8b, 0x57) },
3080  { "SeaGreen1", RGB_(0x54, 0xff, 0x9f) },
3081  { "SeaGreen2", RGB_(0x4e, 0xee, 0x94) },
3082  { "SeaGreen3", RGB_(0x43, 0xcd, 0x80) },
3083  { "SeaGreen4", RGB_(0x2e, 0x8b, 0x57) },
3084  { "SeaShell", RGB_(0xff, 0xf5, 0xee) },
3085  { "Seashell1", RGB_(0xff, 0xf5, 0xee) },
3086  { "Seashell2", RGB_(0xee, 0xe5, 0xde) },
3087  { "Seashell3", RGB_(0xcd, 0xc5, 0xbf) },
3088  { "Seashell4", RGB_(0x8b, 0x86, 0x82) },
3089  { "Sienna", RGB_(0xa0, 0x52, 0x2d) },
3090  { "Sienna1", RGB_(0xff, 0x82, 0x47) },
3091  { "Sienna2", RGB_(0xee, 0x79, 0x42) },
3092  { "Sienna3", RGB_(0xcd, 0x68, 0x39) },
3093  { "Sienna4", RGB_(0x8b, 0x47, 0x26) },
3094  { "Silver", RGB_(0xc0, 0xc0, 0xc0) },
3095  { "SkyBlue", RGB_(0x87, 0xce, 0xeb) },
3096  { "SkyBlue1", RGB_(0x87, 0xce, 0xff) },
3097  { "SkyBlue2", RGB_(0x7e, 0xc0, 0xee) },
3098  { "SkyBlue3", RGB_(0x6c, 0xa6, 0xcd) },
3099  { "SkyBlue4", RGB_(0x4a, 0x70, 0x8b) },
3100  { "SlateBlue", RGB_(0x6a, 0x5a, 0xcd) },
3101  { "SlateBlue1", RGB_(0x83, 0x6f, 0xff) },
3102  { "SlateBlue2", RGB_(0x7a, 0x67, 0xee) },
3103  { "SlateBlue3", RGB_(0x69, 0x59, 0xcd) },
3104  { "SlateBlue4", RGB_(0x47, 0x3c, 0x8b) },
3105  { "SlateGray", RGB_(0x70, 0x80, 0x90) },
3106  { "SlateGray1", RGB_(0xc6, 0xe2, 0xff) },
3107  { "SlateGray2", RGB_(0xb9, 0xd3, 0xee) },
3108  { "SlateGray3", RGB_(0x9f, 0xb6, 0xcd) },
3109  { "SlateGray4", RGB_(0x6c, 0x7b, 0x8b) },
3110  { "SlateGrey", RGB_(0x70, 0x80, 0x90) },
3111  { "Snow", RGB_(0xff, 0xfa, 0xfa) },
3112  { "Snow1", RGB_(0xff, 0xfa, 0xfa) },
3113  { "Snow2", RGB_(0xee, 0xe9, 0xe9) },
3114  { "Snow3", RGB_(0xcd, 0xc9, 0xc9) },
3115  { "Snow4", RGB_(0x8b, 0x89, 0x89) },
3116  { "SpringGreen", RGB_(0x00, 0xff, 0x7f) },
3117  { "SpringGreen1", RGB_(0x0, 0xff, 0x7f) },
3118  { "SpringGreen2", RGB_(0x0, 0xee, 0x76) },
3119  { "SpringGreen3", RGB_(0x0, 0xcd, 0x66) },
3120  { "SpringGreen4", RGB_(0x0, 0x8b, 0x45) },
3121  { "SteelBlue", RGB_(0x46, 0x82, 0xb4) },
3122  { "SteelBlue1", RGB_(0x63, 0xb8, 0xff) },
3123  { "SteelBlue2", RGB_(0x5c, 0xac, 0xee) },
3124  { "SteelBlue3", RGB_(0x4f, 0x94, 0xcd) },
3125  { "SteelBlue4", RGB_(0x36, 0x64, 0x8b) },
3126  { "Tan", RGB_(0xd2, 0xb4, 0x8c) },
3127  { "Tan1", RGB_(0xff, 0xa5, 0x4f) },
3128  { "Tan2", RGB_(0xee, 0x9a, 0x49) },
3129  { "Tan3", RGB_(0xcd, 0x85, 0x3f) },
3130  { "Tan4", RGB_(0x8b, 0x5a, 0x2b) },
3131  { "Teal", RGB_(0x00, 0x80, 0x80) },
3132  { "Thistle", RGB_(0xd8, 0xbf, 0xd8) },
3133  { "Thistle1", RGB_(0xff, 0xe1, 0xff) },
3134  { "Thistle2", RGB_(0xee, 0xd2, 0xee) },
3135  { "Thistle3", RGB_(0xcd, 0xb5, 0xcd) },
3136  { "Thistle4", RGB_(0x8b, 0x7b, 0x8b) },
3137  { "Tomato", RGB_(0xff, 0x63, 0x47) },
3138  { "Tomato1", RGB_(0xff, 0x63, 0x47) },
3139  { "Tomato2", RGB_(0xee, 0x5c, 0x42) },
3140  { "Tomato3", RGB_(0xcd, 0x4f, 0x39) },
3141  { "Tomato4", RGB_(0x8b, 0x36, 0x26) },
3142  { "Turquoise", RGB_(0x40, 0xe0, 0xd0) },
3143  { "Turquoise1", RGB_(0x0, 0xf5, 0xff) },
3144  { "Turquoise2", RGB_(0x0, 0xe5, 0xee) },
3145  { "Turquoise3", RGB_(0x0, 0xc5, 0xcd) },
3146  { "Turquoise4", RGB_(0x0, 0x86, 0x8b) },
3147  { "Violet", RGB_(0xee, 0x82, 0xee) },
3148  { "VioletRed", RGB_(0xd0, 0x20, 0x90) },
3149  { "VioletRed1", RGB_(0xff, 0x3e, 0x96) },
3150  { "VioletRed2", RGB_(0xee, 0x3a, 0x8c) },
3151  { "VioletRed3", RGB_(0xcd, 0x32, 0x78) },
3152  { "VioletRed4", RGB_(0x8b, 0x22, 0x52) },
3153  { "WebGray", RGB_(0x80, 0x80, 0x80) },
3154  { "WebGreen", RGB_(0x0, 0x80, 0x0) },
3155  { "WebGrey", RGB_(0x80, 0x80, 0x80) },
3156  { "WebMaroon", RGB_(0x80, 0x0, 0x0) },
3157  { "WebPurple", RGB_(0x80, 0x0, 0x80) },
3158  { "Wheat", RGB_(0xf5, 0xde, 0xb3) },
3159  { "Wheat1", RGB_(0xff, 0xe7, 0xba) },
3160  { "Wheat2", RGB_(0xee, 0xd8, 0xae) },
3161  { "Wheat3", RGB_(0xcd, 0xba, 0x96) },
3162  { "Wheat4", RGB_(0x8b, 0x7e, 0x66) },
3163  { "White", RGB_(0xff, 0xff, 0xff) },
3164  { "WhiteSmoke", RGB_(0xf5, 0xf5, 0xf5) },
3165  { "X11Gray", RGB_(0xbe, 0xbe, 0xbe) },
3166  { "X11Green", RGB_(0x0, 0xff, 0x0) },
3167  { "X11Grey", RGB_(0xbe, 0xbe, 0xbe) },
3168  { "X11Maroon", RGB_(0xb0, 0x30, 0x60) },
3169  { "X11Purple", RGB_(0xa0, 0x20, 0xf0) },
3170  { "Yellow", RGB_(0xff, 0xff, 0x00) },
3171  { "Yellow1", RGB_(0xff, 0xff, 0x0) },
3172  { "Yellow2", RGB_(0xee, 0xee, 0x0) },
3173  { "Yellow3", RGB_(0xcd, 0xcd, 0x0) },
3174  { "Yellow4", RGB_(0x8b, 0x8b, 0x0) },
3175  { "YellowGreen", RGB_(0x9a, 0xcd, 0x32) },
3176  { NULL, 0 },
3177};
3178
3179/// Translate to RgbValue if \p name is an hex value (e.g. #XXXXXX),
3180/// else look into color_name_table to translate a color name to  its
3181/// hex value
3182///
3183/// @param[in] name string value to convert to RGB
3184/// @param[out] idx index in color table or special value
3185/// return the hex value or -1 if could not find a correct value
3186RgbValue name_to_color(const char *name, int *idx)
3187{
3188  if (name[0] == '#' && isxdigit((uint8_t)name[1]) && isxdigit((uint8_t)name[2])
3189      && isxdigit((uint8_t)name[3]) && isxdigit((uint8_t)name[4]) && isxdigit((uint8_t)name[5])
3190      && isxdigit((uint8_t)name[6]) && name[7] == NUL) {
3191    // rgb hex string
3192    *idx = kColorIdxHex;
3193    return (RgbValue)strtol(name + 1, NULL, 16);  // NOLINT(runtime/deprecated)
3194  } else if (!STRICMP(name, "bg") || !STRICMP(name, "background")) {
3195    *idx = kColorIdxBg;
3196    return normal_bg;
3197  } else if (!STRICMP(name, "fg") || !STRICMP(name, "foreground")) {
3198    *idx = kColorIdxFg;
3199    return normal_fg;
3200  }
3201
3202  int lo = 0;
3203  int hi = ARRAY_SIZE(color_name_table) - 1;  // don't count NULL element
3204  while (lo < hi) {
3205    int m = (lo + hi) / 2;
3206    int cmp = STRICMP(name, color_name_table[m].name);
3207    if (cmp < 0) {
3208      hi = m;
3209    } else if (cmp > 0) {
3210      lo = m + 1;
3211    } else {  // found match
3212      *idx = m;
3213      return color_name_table[m].color;
3214    }
3215  }
3216
3217  *idx = kColorIdxNone;
3218  return -1;
3219}
3220
3221const char *coloridx_to_name(int idx, int val, char hexbuf[8])
3222{
3223  if (idx >= 0) {
3224    return color_name_table[idx].name;
3225  }
3226  switch (idx) {
3227  case kColorIdxNone:
3228    return NULL;
3229  case kColorIdxFg:
3230    return "fg";
3231  case kColorIdxBg:
3232    return "bg";
3233  case kColorIdxHex:
3234    snprintf(hexbuf, 7 + 1, "#%06x", val);
3235    return hexbuf;
3236  default:
3237    abort();
3238  }
3239}
3240
3241int name_to_ctermcolor(const char *name)
3242{
3243  int i;
3244  int off = TOUPPER_ASC(*name);
3245  for (i = ARRAY_SIZE(color_names); --i >= 0;) {
3246    if (off == color_names[i][0]
3247        && STRICMP(name + 1, color_names[i] + 1) == 0) {
3248      break;
3249    }
3250  }
3251  if (i < 0) {
3252    return -1;
3253  }
3254  TriState bold = kNone;
3255  return lookup_color(i, false, &bold);
3256}