blob: a4b2d48d2208378e477bff29082ec100b6bb8b5d [file] [log] [blame]
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +020011 * Highlighting stuff.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +020012 */
13
14#include "vim.h"
15
16#define SG_TERM 1 // term has been set
17#define SG_CTERM 2 // cterm has been set
18#define SG_GUI 4 // gui has been set
19#define SG_LINK 8 // link has been set
20
erw7f7f7aaf2021-12-07 21:29:20 +000021#define MAX_SYN_NAME 200
22
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +020023/*
24 * The "term", "cterm" and "gui" arguments can be any combination of the
25 * following names, separated by commas (but no spaces!).
26 */
John Marriott34f00dd2024-04-08 23:28:12 +020027// must be sorted by the 'value' field because it is used by bsearch()!
28// note: inverse and reverse use the same key
29static keyvalue_T highlight_tab[] = {
30 KEYVALUE_ENTRY(HL_BOLD, "bold"), // index 0
31 KEYVALUE_ENTRY(HL_INVERSE, "inverse"), // index 1
32 KEYVALUE_ENTRY(HL_ITALIC, "italic"), // index 2
33 KEYVALUE_ENTRY(HL_NOCOMBINE, "nocombine"), // index 3
34 KEYVALUE_ENTRY(HL_NORMAL, "NONE"), // index 4
35 KEYVALUE_ENTRY(HL_INVERSE, "reverse"), // index 5
36 KEYVALUE_ENTRY(HL_STANDOUT, "standout"), // index 6
37 KEYVALUE_ENTRY(HL_STRIKETHROUGH, "strikethrough"), // index 7
38 KEYVALUE_ENTRY(HL_UNDERCURL, "undercurl"), // index 8
39 KEYVALUE_ENTRY(HL_UNDERDASHED, "underdashed"), // index 9
40 KEYVALUE_ENTRY(HL_UNDERDOTTED, "underdotted"), // index 10
41 KEYVALUE_ENTRY(HL_UNDERDOUBLE, "underdouble"), // index 11
42 KEYVALUE_ENTRY(HL_UNDERLINE, "underline") // index 12
43};
44
45// this table is used to display highlight names in the "correct" sequence.
46// keep this in sync with highlight_tab[].
47static keyvalue_T *highlight_index_tab[] = {
48 &highlight_tab[0], // HL_BOLD
49 &highlight_tab[6], // HL_STANDOUT
50 &highlight_tab[12], // HL_UNDERLINE
51 &highlight_tab[8], // HL_UNDERCURL
52 &highlight_tab[11], // HL_UNDERDOUBLE
53 &highlight_tab[10], // HL_UNDERDOTTED
54 &highlight_tab[9], // HL_UNDERDASHED
55 &highlight_tab[2], // HL_ITALIC
56 &highlight_tab[5], // HL_REVERSE
57 &highlight_tab[1], // HL_INVERSE
58 &highlight_tab[3], // HL_NOCOMBINE
59 &highlight_tab[7], // HL_STRIKETHROUGH
60 &highlight_tab[4] // HL_NORMAL
61};
62
Bram Moolenaar84f54632022-06-29 18:39:11 +010063// length of all attribute names, plus commas, together (and a bit more)
64#define MAX_ATTR_LEN 120
65
kylo252ae6f1d82022-02-16 19:24:07 +000066#define ATTR_COMBINE(attr_a, attr_b) ((((attr_b) & HL_NOCOMBINE) ? (attr_b) : (attr_a)) | (attr_b))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +020067
John Marriott34f00dd2024-04-08 23:28:12 +020068enum {
69 BLACK = 0,
70 DARKBLUE,
71 DARKGREEN,
72 DARKCYAN,
73 DARKRED,
74 DARKMAGENTA,
75 BROWN,
76 DARKYELLOW,
77 GRAY,
78 GREY,
79 LIGHTGRAY,
80 LIGHTGREY,
81 DARKGRAY,
82 DARKGREY,
83 BLUE,
84 LIGHTBLUE,
85 GREEN,
86 LIGHTGREEN,
87 CYAN,
88 LIGHTCYAN,
89 RED,
90 LIGHTRED,
91 MAGENTA,
92 LIGHTMAGENTA,
93 YELLOW,
94 LIGHTYELLOW,
95 WHITE,
96 NONE
97};
98
99// must be sorted by the 'value' field because it is used by bsearch()!
100static keyvalue_T color_name_tab[] = {
101 KEYVALUE_ENTRY(BLACK, "Black"),
102 KEYVALUE_ENTRY(BLUE, "Blue"),
103 KEYVALUE_ENTRY(BROWN, "Brown"),
104 KEYVALUE_ENTRY(CYAN, "Cyan"),
105 KEYVALUE_ENTRY(DARKBLUE, "DarkBlue"),
106 KEYVALUE_ENTRY(DARKCYAN, "DarkCyan"),
107 KEYVALUE_ENTRY(DARKGRAY, "DarkGray"),
108 KEYVALUE_ENTRY(DARKGREEN, "DarkGreen"),
109 KEYVALUE_ENTRY(DARKGREY, "DarkGrey"),
110 KEYVALUE_ENTRY(DARKMAGENTA, "DarkMagenta"),
111 KEYVALUE_ENTRY(DARKRED, "DarkRed"),
112 KEYVALUE_ENTRY(DARKYELLOW, "DarkYellow"),
113 KEYVALUE_ENTRY(GRAY, "Gray"),
114 KEYVALUE_ENTRY(GREEN, "Green"),
115 KEYVALUE_ENTRY(GREY, "Grey"),
116 KEYVALUE_ENTRY(LIGHTBLUE, "LightBlue"),
117 KEYVALUE_ENTRY(LIGHTCYAN, "LightCyan"),
118 KEYVALUE_ENTRY(LIGHTGRAY, "LightGray"),
119 KEYVALUE_ENTRY(LIGHTGREEN, "LightGreen"),
120 KEYVALUE_ENTRY(LIGHTGREY, "LightGrey"),
121 KEYVALUE_ENTRY(LIGHTMAGENTA, "LightMagenta"),
122 KEYVALUE_ENTRY(LIGHTRED, "LightRed"),
123 KEYVALUE_ENTRY(LIGHTYELLOW, "LightYellow"),
124 KEYVALUE_ENTRY(MAGENTA, "Magenta"),
125 KEYVALUE_ENTRY(NONE, "NONE"),
126 KEYVALUE_ENTRY(RED, "Red"),
127 KEYVALUE_ENTRY(WHITE, "White"),
128 KEYVALUE_ENTRY(YELLOW, "Yellow")
129};
130
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200131/*
132 * Structure that stores information about a highlight group.
133 * The ID of a highlight group is also called group ID. It is the index in
134 * the highlight_ga array PLUS ONE.
135 */
136typedef struct
137{
138 char_u *sg_name; // highlight group name
139 char_u *sg_name_u; // uppercase of sg_name
140 int sg_cleared; // "hi clear" was used
141// for normal terminals
142 int sg_term; // "term=" highlighting attributes
143 char_u *sg_start; // terminal string for start highl
144 char_u *sg_stop; // terminal string for stop highl
145 int sg_term_attr; // Screen attr for term mode
146// for color terminals
147 int sg_cterm; // "cterm=" highlighting attr
148 int sg_cterm_bold; // bold attr was set for light color
149 int sg_cterm_fg; // terminal fg color number + 1
150 int sg_cterm_bg; // terminal bg color number + 1
Bram Moolenaare023e882020-05-31 16:42:30 +0200151 int sg_cterm_ul; // terminal ul color number + 1
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200152 int sg_cterm_attr; // Screen attr for color term mode
PMuncha606f3a2023-11-15 15:35:49 +0100153 int sg_cterm_font; // terminal alternative font (0 for normal)
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200154// for when using the GUI
155#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
156 guicolor_T sg_gui_fg; // GUI foreground color handle
157 guicolor_T sg_gui_bg; // GUI background color handle
Bram Moolenaare023e882020-05-31 16:42:30 +0200158 guicolor_T sg_gui_sp; // GUI special color handle
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200159#endif
160#ifdef FEAT_GUI
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200161 GuiFont sg_font; // GUI font handle
162#ifdef FEAT_XFONTSET
163 GuiFontset sg_fontset; // GUI fontset handle
164#endif
165 char_u *sg_font_name; // GUI font or fontset name
166 int sg_gui_attr; // Screen attr for GUI mode
167#endif
168#if defined(FEAT_GUI) || defined(FEAT_EVAL)
169// Store the sp color name for the GUI or synIDattr()
170 int sg_gui; // "gui=" highlighting attributes
171 char_u *sg_gui_fg_name;// GUI foreground color name
172 char_u *sg_gui_bg_name;// GUI background color name
173 char_u *sg_gui_sp_name;// GUI special color name
174#endif
175 int sg_link; // link to this highlight group ID
Bram Moolenaar213da552020-09-17 19:59:26 +0200176 int sg_deflink; // default link; restored in highlight_clear()
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200177 int sg_set; // combination of SG_* flags
178#ifdef FEAT_EVAL
Bram Moolenaare8df0102020-09-18 19:40:45 +0200179 sctx_T sg_deflink_sctx; // script where the default link was set
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200180 sctx_T sg_script_ctx; // script in which the group was last set
181#endif
182} hl_group_T;
183
184// highlight groups for 'highlight' option
185static garray_T highlight_ga;
186#define HL_TABLE() ((hl_group_T *)((highlight_ga.ga_data)))
187
188/*
189 * An attribute number is the index in attr_table plus ATTR_OFF.
190 */
191#define ATTR_OFF (HL_ALL + 1)
192
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200193static void syn_unadd_group(void);
194static void set_hl_attr(int idx);
195static void highlight_list_one(int id);
196static int highlight_list_arg(int id, int didh, int type, int iarg, char_u *sarg, char *name);
197static int syn_add_group(char_u *name);
198static int hl_has_settings(int idx, int check_link);
199static void highlight_clear(int idx);
200
201#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
202static void gui_do_one_color(int idx, int do_menu, int do_tooltip);
203#endif
204#ifdef FEAT_GUI
205static int set_group_colors(char_u *name, guicolor_T *fgp, guicolor_T *bgp, int do_menu, int use_norm, int do_tooltip);
206static void hl_do_font(int idx, char_u *arg, int do_normal, int do_menu, int do_tooltip, int free_font);
207#endif
208
209/*
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200210 * The default highlight groups. These are compiled-in for fast startup and
211 * they still work when the runtime files can't be found.
212 * When making changes here, also change runtime/colors/default.vim!
213 * The #ifdefs are needed to reduce the amount of static data. Helps to make
214 * the 16 bit DOS (museum) version compile.
215 */
216#if defined(FEAT_GUI) || defined(FEAT_EVAL)
217# define CENT(a, b) b
218#else
219# define CENT(a, b) a
220#endif
221static char *(highlight_init_both[]) = {
222 CENT("ErrorMsg term=standout ctermbg=DarkRed ctermfg=White",
223 "ErrorMsg term=standout ctermbg=DarkRed ctermfg=White guibg=Red guifg=White"),
224 CENT("IncSearch term=reverse cterm=reverse",
225 "IncSearch term=reverse cterm=reverse gui=reverse"),
226 CENT("ModeMsg term=bold cterm=bold",
227 "ModeMsg term=bold cterm=bold gui=bold"),
228 CENT("NonText term=bold ctermfg=Blue",
229 "NonText term=bold ctermfg=Blue gui=bold guifg=Blue"),
230 CENT("StatusLine term=reverse,bold cterm=reverse,bold",
231 "StatusLine term=reverse,bold cterm=reverse,bold gui=reverse,bold"),
232 CENT("StatusLineNC term=reverse cterm=reverse",
233 "StatusLineNC term=reverse cterm=reverse gui=reverse"),
234 "default link EndOfBuffer NonText",
235 CENT("VertSplit term=reverse cterm=reverse",
236 "VertSplit term=reverse cterm=reverse gui=reverse"),
237#ifdef FEAT_CLIPBOARD
238 CENT("VisualNOS term=underline,bold cterm=underline,bold",
239 "VisualNOS term=underline,bold cterm=underline,bold gui=underline,bold"),
240#endif
241#ifdef FEAT_DIFF
242 CENT("DiffText term=reverse cterm=bold ctermbg=Red",
243 "DiffText term=reverse cterm=bold ctermbg=Red gui=bold guibg=Red"),
244#endif
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200245 CENT("PmenuSbar ctermbg=Grey",
246 "PmenuSbar ctermbg=Grey guibg=Grey"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200247 CENT("TabLineSel term=bold cterm=bold",
248 "TabLineSel term=bold cterm=bold gui=bold"),
249 CENT("TabLineFill term=reverse cterm=reverse",
250 "TabLineFill term=reverse cterm=reverse gui=reverse"),
251#ifdef FEAT_GUI
252 "Cursor guibg=fg guifg=bg",
253 "lCursor guibg=fg guifg=bg", // should be different, but what?
254#endif
255 "default link QuickFixLine Search",
Bram Moolenaare413ea02021-11-24 16:20:13 +0000256 "default link CursorLineSign SignColumn",
257 "default link CursorLineFold FoldColumn",
LemonBoya4399382022-04-09 21:04:08 +0100258 "default link CurSearch Search",
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000259 "default link PmenuKind Pmenu",
260 "default link PmenuKindSel PmenuSel",
glepnir40c1c332024-06-11 19:37:04 +0200261 "default link PmenuMatch Pmenu",
262 "default link PmenuMatchSel PmenuSel",
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000263 "default link PmenuExtra Pmenu",
264 "default link PmenuExtraSel PmenuSel",
glepnir6a38aff2024-12-16 21:56:16 +0100265 "default link ComplMatchIns Normal",
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200266 CENT("Normal cterm=NONE", "Normal gui=NONE"),
267 NULL
268};
269
Bram Moolenaar1f164b12019-07-24 19:00:36 +0200270// Default colors only used with a light background.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200271static char *(highlight_init_light[]) = {
272 CENT("Directory term=bold ctermfg=DarkBlue",
273 "Directory term=bold ctermfg=DarkBlue guifg=Blue"),
274 CENT("LineNr term=underline ctermfg=Brown",
275 "LineNr term=underline ctermfg=Brown guifg=Brown"),
Bram Moolenaar017ba072019-09-14 21:01:23 +0200276 CENT("CursorLineNr term=bold cterm=underline ctermfg=Brown",
277 "CursorLineNr term=bold cterm=underline ctermfg=Brown gui=bold guifg=Brown"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200278 CENT("MoreMsg term=bold ctermfg=DarkGreen",
279 "MoreMsg term=bold ctermfg=DarkGreen gui=bold guifg=SeaGreen"),
280 CENT("Question term=standout ctermfg=DarkGreen",
281 "Question term=standout ctermfg=DarkGreen gui=bold guifg=SeaGreen"),
282 CENT("Search term=reverse ctermbg=Yellow ctermfg=NONE",
283 "Search term=reverse ctermbg=Yellow ctermfg=NONE guibg=Yellow guifg=NONE"),
284#ifdef FEAT_SPELL
285 CENT("SpellBad term=reverse ctermbg=LightRed",
286 "SpellBad term=reverse ctermbg=LightRed guisp=Red gui=undercurl"),
287 CENT("SpellCap term=reverse ctermbg=LightBlue",
288 "SpellCap term=reverse ctermbg=LightBlue guisp=Blue gui=undercurl"),
289 CENT("SpellRare term=reverse ctermbg=LightMagenta",
290 "SpellRare term=reverse ctermbg=LightMagenta guisp=Magenta gui=undercurl"),
291 CENT("SpellLocal term=underline ctermbg=Cyan",
292 "SpellLocal term=underline ctermbg=Cyan guisp=DarkCyan gui=undercurl"),
293#endif
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200294 CENT("PmenuThumb ctermbg=Black",
295 "PmenuThumb ctermbg=Black guibg=Black"),
296 CENT("Pmenu ctermbg=LightMagenta ctermfg=Black",
297 "Pmenu ctermbg=LightMagenta ctermfg=Black guibg=LightMagenta"),
298 CENT("PmenuSel ctermbg=LightGrey ctermfg=Black",
299 "PmenuSel ctermbg=LightGrey ctermfg=Black guibg=Grey"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200300 CENT("SpecialKey term=bold ctermfg=DarkBlue",
301 "SpecialKey term=bold ctermfg=DarkBlue guifg=Blue"),
302 CENT("Title term=bold ctermfg=DarkMagenta",
303 "Title term=bold ctermfg=DarkMagenta gui=bold guifg=Magenta"),
304 CENT("WarningMsg term=standout ctermfg=DarkRed",
305 "WarningMsg term=standout ctermfg=DarkRed guifg=Red"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200306 CENT("WildMenu term=standout ctermbg=Yellow ctermfg=Black",
307 "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200308#ifdef FEAT_FOLDING
309 CENT("Folded term=standout ctermbg=Grey ctermfg=DarkBlue",
310 "Folded term=standout ctermbg=Grey ctermfg=DarkBlue guibg=LightGrey guifg=DarkBlue"),
311 CENT("FoldColumn term=standout ctermbg=Grey ctermfg=DarkBlue",
312 "FoldColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue"),
313#endif
314#ifdef FEAT_SIGNS
315 CENT("SignColumn term=standout ctermbg=Grey ctermfg=DarkBlue",
316 "SignColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue"),
317#endif
Maxim Kim59bafc82024-02-01 21:07:51 +0100318 CENT("Visual ctermbg=Grey ctermfg=Black",
Maxim Kim34e4a052024-02-14 20:28:17 +0100319 "Visual ctermbg=Grey ctermfg=Black guibg=LightGrey guifg=Black"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200320#ifdef FEAT_DIFF
321 CENT("DiffAdd term=bold ctermbg=LightBlue",
322 "DiffAdd term=bold ctermbg=LightBlue guibg=LightBlue"),
323 CENT("DiffChange term=bold ctermbg=LightMagenta",
324 "DiffChange term=bold ctermbg=LightMagenta guibg=LightMagenta"),
325 CENT("DiffDelete term=bold ctermfg=Blue ctermbg=LightCyan",
326 "DiffDelete term=bold ctermfg=Blue ctermbg=LightCyan gui=bold guifg=Blue guibg=LightCyan"),
327#endif
328 CENT("TabLine term=underline cterm=underline ctermfg=black ctermbg=LightGrey",
329 "TabLine term=underline cterm=underline ctermfg=black ctermbg=LightGrey gui=underline guibg=LightGrey"),
330#ifdef FEAT_SYN_HL
331 CENT("CursorColumn term=reverse ctermbg=LightGrey",
332 "CursorColumn term=reverse ctermbg=LightGrey guibg=Grey90"),
333 CENT("CursorLine term=underline cterm=underline",
334 "CursorLine term=underline cterm=underline guibg=Grey90"),
335 CENT("ColorColumn term=reverse ctermbg=LightRed",
336 "ColorColumn term=reverse ctermbg=LightRed guibg=LightRed"),
337#endif
338#ifdef FEAT_CONCEAL
339 CENT("Conceal ctermbg=DarkGrey ctermfg=LightGrey",
340 "Conceal ctermbg=DarkGrey ctermfg=LightGrey guibg=DarkGrey guifg=LightGrey"),
341#endif
342 CENT("MatchParen term=reverse ctermbg=Cyan",
343 "MatchParen term=reverse ctermbg=Cyan guibg=Cyan"),
344#ifdef FEAT_TERMINAL
345 CENT("StatusLineTerm term=reverse,bold cterm=bold ctermfg=White ctermbg=DarkGreen",
346 "StatusLineTerm term=reverse,bold cterm=bold ctermfg=White ctermbg=DarkGreen gui=bold guifg=bg guibg=DarkGreen"),
347 CENT("StatusLineTermNC term=reverse ctermfg=White ctermbg=DarkGreen",
348 "StatusLineTermNC term=reverse ctermfg=White ctermbg=DarkGreen guifg=bg guibg=DarkGreen"),
349#endif
350#ifdef FEAT_MENU
351 CENT("ToolbarLine term=underline ctermbg=LightGrey",
352 "ToolbarLine term=underline ctermbg=LightGrey guibg=LightGrey"),
353 CENT("ToolbarButton cterm=bold ctermfg=White ctermbg=DarkGrey",
354 "ToolbarButton cterm=bold ctermfg=White ctermbg=DarkGrey gui=bold guifg=White guibg=Grey40"),
355#endif
356 NULL
357};
358
Bram Moolenaar1f164b12019-07-24 19:00:36 +0200359// Default colors only used with a dark background.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200360static char *(highlight_init_dark[]) = {
361 CENT("Directory term=bold ctermfg=LightCyan",
362 "Directory term=bold ctermfg=LightCyan guifg=Cyan"),
363 CENT("LineNr term=underline ctermfg=Yellow",
364 "LineNr term=underline ctermfg=Yellow guifg=Yellow"),
Bram Moolenaar017ba072019-09-14 21:01:23 +0200365 CENT("CursorLineNr term=bold cterm=underline ctermfg=Yellow",
366 "CursorLineNr term=bold cterm=underline ctermfg=Yellow gui=bold guifg=Yellow"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200367 CENT("MoreMsg term=bold ctermfg=LightGreen",
368 "MoreMsg term=bold ctermfg=LightGreen gui=bold guifg=SeaGreen"),
369 CENT("Question term=standout ctermfg=LightGreen",
370 "Question term=standout ctermfg=LightGreen gui=bold guifg=Green"),
371 CENT("Search term=reverse ctermbg=Yellow ctermfg=Black",
372 "Search term=reverse ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black"),
373 CENT("SpecialKey term=bold ctermfg=LightBlue",
374 "SpecialKey term=bold ctermfg=LightBlue guifg=Cyan"),
375#ifdef FEAT_SPELL
376 CENT("SpellBad term=reverse ctermbg=Red",
377 "SpellBad term=reverse ctermbg=Red guisp=Red gui=undercurl"),
378 CENT("SpellCap term=reverse ctermbg=Blue",
379 "SpellCap term=reverse ctermbg=Blue guisp=Blue gui=undercurl"),
380 CENT("SpellRare term=reverse ctermbg=Magenta",
381 "SpellRare term=reverse ctermbg=Magenta guisp=Magenta gui=undercurl"),
382 CENT("SpellLocal term=underline ctermbg=Cyan",
383 "SpellLocal term=underline ctermbg=Cyan guisp=Cyan gui=undercurl"),
384#endif
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200385 CENT("PmenuThumb ctermbg=White",
386 "PmenuThumb ctermbg=White guibg=White"),
387 CENT("Pmenu ctermbg=Magenta ctermfg=Black",
388 "Pmenu ctermbg=Magenta ctermfg=Black guibg=Magenta"),
389 CENT("PmenuSel ctermbg=Black ctermfg=DarkGrey",
390 "PmenuSel ctermbg=Black ctermfg=DarkGrey guibg=DarkGrey"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200391 CENT("Title term=bold ctermfg=LightMagenta",
392 "Title term=bold ctermfg=LightMagenta gui=bold guifg=Magenta"),
393 CENT("WarningMsg term=standout ctermfg=LightRed",
394 "WarningMsg term=standout ctermfg=LightRed guifg=Red"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200395 CENT("WildMenu term=standout ctermbg=Yellow ctermfg=Black",
396 "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200397#ifdef FEAT_FOLDING
398 CENT("Folded term=standout ctermbg=DarkGrey ctermfg=Cyan",
399 "Folded term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=DarkGrey guifg=Cyan"),
400 CENT("FoldColumn term=standout ctermbg=DarkGrey ctermfg=Cyan",
401 "FoldColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan"),
402#endif
403#ifdef FEAT_SIGNS
404 CENT("SignColumn term=standout ctermbg=DarkGrey ctermfg=Cyan",
405 "SignColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan"),
406#endif
Christian Brabandte6d8b462024-01-28 23:33:29 +0100407 CENT("Visual ctermbg=Grey ctermfg=Black",
Maxim Kim34e4a052024-02-14 20:28:17 +0100408 "Visual ctermbg=Grey ctermfg=Black guibg=#575757 guifg=LightGrey"),
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200409#ifdef FEAT_DIFF
410 CENT("DiffAdd term=bold ctermbg=DarkBlue",
411 "DiffAdd term=bold ctermbg=DarkBlue guibg=DarkBlue"),
412 CENT("DiffChange term=bold ctermbg=DarkMagenta",
413 "DiffChange term=bold ctermbg=DarkMagenta guibg=DarkMagenta"),
414 CENT("DiffDelete term=bold ctermfg=Blue ctermbg=DarkCyan",
415 "DiffDelete term=bold ctermfg=Blue ctermbg=DarkCyan gui=bold guifg=Blue guibg=DarkCyan"),
416#endif
417 CENT("TabLine term=underline cterm=underline ctermfg=white ctermbg=DarkGrey",
418 "TabLine term=underline cterm=underline ctermfg=white ctermbg=DarkGrey gui=underline guibg=DarkGrey"),
419#ifdef FEAT_SYN_HL
420 CENT("CursorColumn term=reverse ctermbg=DarkGrey",
421 "CursorColumn term=reverse ctermbg=DarkGrey guibg=Grey40"),
422 CENT("CursorLine term=underline cterm=underline",
423 "CursorLine term=underline cterm=underline guibg=Grey40"),
424 CENT("ColorColumn term=reverse ctermbg=DarkRed",
425 "ColorColumn term=reverse ctermbg=DarkRed guibg=DarkRed"),
426#endif
427 CENT("MatchParen term=reverse ctermbg=DarkCyan",
428 "MatchParen term=reverse ctermbg=DarkCyan guibg=DarkCyan"),
429#ifdef FEAT_CONCEAL
430 CENT("Conceal ctermbg=DarkGrey ctermfg=LightGrey",
431 "Conceal ctermbg=DarkGrey ctermfg=LightGrey guibg=DarkGrey guifg=LightGrey"),
432#endif
433#ifdef FEAT_TERMINAL
434 CENT("StatusLineTerm term=reverse,bold cterm=bold ctermfg=Black ctermbg=LightGreen",
435 "StatusLineTerm term=reverse,bold cterm=bold ctermfg=Black ctermbg=LightGreen gui=bold guifg=bg guibg=LightGreen"),
436 CENT("StatusLineTermNC term=reverse ctermfg=Black ctermbg=LightGreen",
437 "StatusLineTermNC term=reverse ctermfg=Black ctermbg=LightGreen guifg=bg guibg=LightGreen"),
438#endif
439#ifdef FEAT_MENU
440 CENT("ToolbarLine term=underline ctermbg=DarkGrey",
441 "ToolbarLine term=underline ctermbg=DarkGrey guibg=Grey50"),
442 CENT("ToolbarButton cterm=bold ctermfg=Black ctermbg=LightGrey",
443 "ToolbarButton cterm=bold ctermfg=Black ctermbg=LightGrey gui=bold guifg=Black guibg=LightGrey"),
444#endif
445 NULL
446};
447
Dominique Pelle748b3082022-01-08 12:41:16 +0000448#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200449/*
450 * Returns the number of highlight groups.
451 */
452 int
453highlight_num_groups(void)
454{
455 return highlight_ga.ga_len;
456}
457
458/*
459 * Returns the name of a highlight group.
460 */
461 char_u *
462highlight_group_name(int id)
463{
464 return HL_TABLE()[id].sg_name;
465}
466
467/*
468 * Returns the ID of the link to a highlight group.
469 */
470 int
471highlight_link_id(int id)
472{
473 return HL_TABLE()[id].sg_link;
474}
Dominique Pelle748b3082022-01-08 12:41:16 +0000475#endif
Bram Moolenaar2ac6e822019-07-15 22:40:22 +0200476
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200477 void
478init_highlight(
479 int both, // include groups where 'bg' doesn't matter
480 int reset) // clear group first
481{
482 int i;
483 char **pp;
484 static int had_both = FALSE;
485#ifdef FEAT_EVAL
486 char_u *p;
487
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100488 // Try finding the color scheme file. Used when a color file was loaded
489 // and 'background' or 't_Co' is changed.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200490 p = get_var_value((char_u *)"g:colors_name");
491 if (p != NULL)
492 {
493 // The value of g:colors_name could be freed when sourcing the script,
494 // making "p" invalid, so copy it.
495 char_u *copy_p = vim_strsave(p);
496 int r;
497
498 if (copy_p != NULL)
499 {
500 r = load_colors(copy_p);
501 vim_free(copy_p);
502 if (r == OK)
503 return;
504 }
505 }
506
507#endif
508
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100509 // Didn't use a color file, use the compiled-in colors.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200510 if (both)
511 {
512 had_both = TRUE;
513 pp = highlight_init_both;
514 for (i = 0; pp[i] != NULL; ++i)
515 do_highlight((char_u *)pp[i], reset, TRUE);
516 }
517 else if (!had_both)
518 // Don't do anything before the call with both == TRUE from main().
519 // Not everything has been setup then, and that call will overrule
520 // everything anyway.
521 return;
522
523 if (*p_bg == 'l')
524 pp = highlight_init_light;
525 else
526 pp = highlight_init_dark;
527 for (i = 0; pp[i] != NULL; ++i)
528 do_highlight((char_u *)pp[i], reset, TRUE);
529
Maxim Kim59bafc82024-02-01 21:07:51 +0100530 // Reverse looks ugly, but grey may not work for less than 8 colors. Thus
531 // let it depend on the number of colors available.
532 if (t_colors < 8)
533 do_highlight((char_u *)"Visual term=reverse cterm=reverse ctermbg=NONE ctermfg=NONE",
534 FALSE, TRUE);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200535 // With 8 colors brown is equal to yellow, need to use black for Search fg
536 // to avoid Statement highlighted text disappears.
537 // Clear the attributes, needed when changing the t_Co value.
Christian Brabandte6d8b462024-01-28 23:33:29 +0100538 if (t_colors <= 8)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200539 {
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200540 if (*p_bg == 'l')
541 do_highlight((char_u *)"Search ctermfg=black", FALSE, TRUE);
542 }
543
544#ifdef FEAT_SYN_HL
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100545 // If syntax highlighting is enabled load the highlighting for it.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200546 if (get_var_value((char_u *)"g:syntax_on") != NULL)
547 {
548 static int recursive = 0;
549
550 if (recursive >= 5)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000551 emsg(_(e_recursive_loop_loading_syncolor_vim));
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200552 else
553 {
554 ++recursive;
555 (void)source_runtime((char_u *)"syntax/syncolor.vim", DIP_ALL);
556 --recursive;
557 }
558 }
559#endif
560}
561
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000562#if defined(FEAT_EVAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
563/*
564 * Load a default color list. Intended to support legacy color names but allows
565 * the user to override the color values. Only loaded once.
566 */
567 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000568load_default_colors_lists(void)
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000569{
570 // Lacking a default color list isn't the end of the world but it is likely
571 // an inconvenience so users should know when it is missing.
572 if (source_runtime((char_u *)"colors/lists/default.vim", DIP_ALL) != OK)
573 msg("failed to load colors/lists/default.vim");
574}
575#endif
576
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200577/*
578 * Load color file "name".
579 * Return OK for success, FAIL for failure.
580 */
581 int
582load_colors(char_u *name)
583{
584 char_u *buf;
585 int retval = FAIL;
586 static int recursive = FALSE;
587
588 // When being called recursively, this is probably because setting
589 // 'background' caused the highlighting to be reloaded. This means it is
590 // working, thus we should return OK.
591 if (recursive)
592 return OK;
593
594 recursive = TRUE;
595 buf = alloc(STRLEN(name) + 12);
596 if (buf != NULL)
597 {
Bram Moolenaar2a521962021-10-25 10:30:14 +0100598#if defined(FEAT_EVAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Drew Vogele30d1022021-10-24 20:35:07 +0100599 load_default_colors_lists();
600#endif
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200601 apply_autocmds(EVENT_COLORSCHEMEPRE, name,
602 curbuf->b_fname, FALSE, curbuf);
603 sprintf((char *)buf, "colors/%s.vim", name);
604 retval = source_runtime(buf, DIP_START + DIP_OPT);
605 vim_free(buf);
Bram Moolenaar5d09a402022-08-31 21:17:10 +0100606 if (retval == OK)
607 apply_autocmds(EVENT_COLORSCHEME, name, curbuf->b_fname,
608 FALSE, curbuf);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200609 }
610 recursive = FALSE;
611
612 return retval;
613}
614
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200615static int color_numbers_16[28] = {0, 1, 2, 3,
616 4, 5, 6, 6,
617 7, 7, 7, 7,
618 8, 8,
619 9, 9, 10, 10,
620 11, 11, 12, 12, 13,
621 13, 14, 14, 15, -1};
622// for xterm with 88 colors...
623static int color_numbers_88[28] = {0, 4, 2, 6,
624 1, 5, 32, 72,
625 84, 84, 7, 7,
626 82, 82,
627 12, 43, 10, 61,
628 14, 63, 9, 74, 13,
629 75, 11, 78, 15, -1};
630// for xterm with 256 colors...
631static int color_numbers_256[28] = {0, 4, 2, 6,
Bram Moolenaare93c9682020-04-25 15:35:32 +0200632 1, 5, 130, 3,
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200633 248, 248, 7, 7,
634 242, 242,
635 12, 81, 10, 121,
636 14, 159, 9, 224, 13,
637 225, 11, 229, 15, -1};
638// for terminals with less than 16 colors...
639static int color_numbers_8[28] = {0, 4, 2, 6,
640 1, 5, 3, 3,
641 7, 7, 7, 7,
642 0+8, 0+8,
643 4+8, 4+8, 2+8, 2+8,
644 6+8, 6+8, 1+8, 1+8, 5+8,
645 5+8, 3+8, 3+8, 7+8, -1};
646
647/*
648 * Lookup the "cterm" value to be used for color with index "idx" in
John Marriott34f00dd2024-04-08 23:28:12 +0200649 * color_name_tab[].
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200650 * "boldp" will be set to TRUE or FALSE for a foreground color when using 8
651 * colors, otherwise it will be unchanged.
652 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100653 static int
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200654lookup_color(int idx, int foreground, int *boldp)
655{
656 int color = color_numbers_16[idx];
657 char_u *p;
658
659 // Use the _16 table to check if it's a valid color name.
660 if (color < 0)
661 return -1;
662
663 if (t_colors == 8)
664 {
665 // t_Co is 8: use the 8 colors table
666#if defined(__QNXNTO__)
Bram Moolenaarc95e8d62019-12-05 18:35:44 +0100667 // On qnx, the 8 & 16 color arrays are the same
668 if (STRNCMP(T_NAME, "qansi", 5) == 0)
669 color = color_numbers_16[idx];
670 else
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200671#endif
Bram Moolenaarc95e8d62019-12-05 18:35:44 +0100672 color = color_numbers_8[idx];
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200673 if (foreground)
674 {
675 // set/reset bold attribute to get light foreground
676 // colors (on some terminals, e.g. "linux")
677 if (color & 8)
678 *boldp = TRUE;
679 else
680 *boldp = FALSE;
681 }
682 color &= 7; // truncate to 8 colors
683 }
684 else if (t_colors == 16 || t_colors == 88
685 || t_colors >= 256)
686 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100687 // Guess: if the termcap entry ends in 'm', it is
688 // probably an xterm-like terminal. Use the changed
689 // order for colors.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +0200690 if (*T_CAF != NUL)
691 p = T_CAF;
692 else
693 p = T_CSF;
694 if (*p != NUL && (t_colors > 256
695 || *(p + STRLEN(p) - 1) == 'm'))
696 {
697 if (t_colors == 88)
698 color = color_numbers_88[idx];
699 else if (t_colors >= 256)
700 color = color_numbers_256[idx];
701 else
702 color = color_numbers_8[idx];
703 }
704#ifdef FEAT_TERMRESPONSE
705 if (t_colors >= 256 && color == 15 && is_mac_terminal)
706 // Terminal.app has a bug: 15 is light grey. Use white
707 // from the color cube instead.
708 color = 231;
709#endif
710 }
711 return color;
712}
713
714/*
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100715 * Link highlight group 'from_hg' to 'to_hg'.
716 * 'dodefault' is set to TRUE for ":highlight default link".
dundargocc57b5bc2022-11-02 13:30:51 +0000717 * 'forceit' is set to TRUE for ":highlight! link"
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100718 * 'init' is set to TRUE when initializing all the highlight groups.
719 */
720 static void
721highlight_group_link(
722 char_u *from_hg,
723 int from_len,
724 char_u *to_hg,
725 int to_len,
726 int dodefault,
727 int forceit,
728 int init)
729{
730 int from_id;
731 int to_id;
732 hl_group_T *hlgroup = NULL;
733
734 from_id = syn_check_group(from_hg, from_len);
735 if (STRNCMP(to_hg, "NONE", 4) == 0)
736 to_id = 0;
737 else
738 to_id = syn_check_group(to_hg, to_len);
739
740 if (from_id > 0)
741 {
742 hlgroup = &HL_TABLE()[from_id - 1];
743 if (dodefault && (forceit || hlgroup->sg_deflink == 0))
744 {
745 hlgroup->sg_deflink = to_id;
746#ifdef FEAT_EVAL
747 hlgroup->sg_deflink_sctx = current_sctx;
748 hlgroup->sg_deflink_sctx.sc_lnum += SOURCING_LNUM;
749#endif
750 }
751 }
752
753 if (from_id > 0 && (!init || hlgroup->sg_set == 0))
754 {
755 // Don't allow a link when there already is some highlighting
756 // for the group, unless '!' is used
757 if (to_id > 0 && !forceit && !init
758 && hl_has_settings(from_id - 1, dodefault))
759 {
760 if (SOURCING_NAME == NULL && !dodefault)
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000761 emsg(_(e_group_has_settings_highlight_link_ignored));
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100762 }
763 else if (hlgroup->sg_link != to_id
764#ifdef FEAT_EVAL
765 || hlgroup->sg_script_ctx.sc_sid != current_sctx.sc_sid
766#endif
767 || hlgroup->sg_cleared)
768 {
769 if (!init)
770 hlgroup->sg_set |= SG_LINK;
771 hlgroup->sg_link = to_id;
772#ifdef FEAT_EVAL
773 hlgroup->sg_script_ctx = current_sctx;
774 hlgroup->sg_script_ctx.sc_lnum += SOURCING_LNUM;
775#endif
776 hlgroup->sg_cleared = FALSE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100777 redraw_all_later(UPD_SOME_VALID);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100778
779 // Only call highlight_changed() once after multiple changes.
780 need_highlight_changed = TRUE;
781 }
782 }
783
784}
785
786/*
787 * Reset all highlighting to the defaults. Removes all highlighting for the
788 * groups added by the user.
789 */
790 static void
791highlight_reset_all(void)
792{
793 int idx;
794
795#ifdef FEAT_GUI
796 // First, we do not destroy the old values, but allocate the new
797 // ones and update the display. THEN we destroy the old values.
798 // If we destroy the old values first, then the old values
799 // (such as GuiFont's or GuiFontset's) will still be displayed but
800 // invalid because they were free'd.
801 if (gui.in_use)
802 {
803# ifdef FEAT_BEVAL_TIP
804 gui_init_tooltip_font();
805# endif
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100806# if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100807 gui_init_menu_font();
808# endif
809 }
810# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_X11)
811 gui_mch_def_colors();
812# endif
813# ifdef FEAT_GUI_X11
814# ifdef FEAT_MENU
815
816 // This only needs to be done when there is no Menu highlight
817 // group defined by default, which IS currently the case.
818 gui_mch_new_menu_colors();
819# endif
820 if (gui.in_use)
821 {
822 gui_new_scrollbar_colors();
823# ifdef FEAT_BEVAL_GUI
824 gui_mch_new_tooltip_colors();
825# endif
826# ifdef FEAT_MENU
827 gui_mch_new_menu_font();
828# endif
829 }
830# endif
831
832 // Ok, we're done allocating the new default graphics items.
833 // The screen should already be refreshed at this point.
834 // It is now Ok to clear out the old data.
835#endif
836#ifdef FEAT_EVAL
837 do_unlet((char_u *)"g:colors_name", TRUE);
838#endif
839 restore_cterm_colors();
840
841 // Clear all default highlight groups and load the defaults.
842 for (idx = 0; idx < highlight_ga.ga_len; ++idx)
843 highlight_clear(idx);
844 init_highlight(TRUE, TRUE);
845#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
846 if (USE_24BIT)
847 highlight_gui_started();
848 else
849#endif
850 highlight_changed();
851 redraw_later_clear();
852}
853
854/*
855 * Set the 'term' or 'cterm' or 'gui' attributes for the highlight group at
856 * index 'idx'.
857 * 'key' is one of 'TERM' or 'CTERM' or 'GUI'
858 * 'arg' is the list of attribute names separated by comma.
859 * 'init' is set to TRUE when initializing all the highlight groups.
860 * Returns TRUE if the attributes are set.
861 */
862 static int
863highlight_set_termgui_attr(int idx, char_u *key, char_u *arg, int init)
864{
865 int attr;
Mike Williams72a156b2024-04-09 22:04:54 +0200866 size_t off;
John Marriott34f00dd2024-04-08 23:28:12 +0200867 keyvalue_T target;
868 keyvalue_T *entry;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100869
870 attr = 0;
871 off = 0;
John Marriott34f00dd2024-04-08 23:28:12 +0200872 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +0100873 target.value.length = 0; // not used, see cmp_keyvalue_value_ni()
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100874 while (arg[off] != NUL)
875 {
John Marriott8d4477e2024-11-02 15:59:01 +0100876 target.value.string = arg + off;
877 entry = (keyvalue_T *)bsearch(&target, &highlight_tab,
878 ARRAY_LENGTH(highlight_tab), sizeof(highlight_tab[0]),
879 cmp_keyvalue_value_ni);
John Marriott34f00dd2024-04-08 23:28:12 +0200880 if (entry == NULL)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100881 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000882 semsg(_(e_illegal_value_str), arg);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100883 return FALSE;
884 }
John Marriott34f00dd2024-04-08 23:28:12 +0200885
886 attr |= entry->key;
John Marriott8d4477e2024-11-02 15:59:01 +0100887 off += entry->value.length;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +0100888 if (arg[off] == ',') // another one follows
889 ++off;
890 }
891 if (*key == 'T')
892 {
893 if (!init || !(HL_TABLE()[idx].sg_set & SG_TERM))
894 {
895 if (!init)
896 HL_TABLE()[idx].sg_set |= SG_TERM;
897 HL_TABLE()[idx].sg_term = attr;
898 }
899 }
900 else if (*key == 'C')
901 {
902 if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
903 {
904 if (!init)
905 HL_TABLE()[idx].sg_set |= SG_CTERM;
906 HL_TABLE()[idx].sg_cterm = attr;
907 HL_TABLE()[idx].sg_cterm_bold = FALSE;
908 }
909 }
910#if defined(FEAT_GUI) || defined(FEAT_EVAL)
911 else
912 {
913 if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
914 {
915 if (!init)
916 HL_TABLE()[idx].sg_set |= SG_GUI;
917 HL_TABLE()[idx].sg_gui = attr;
918 }
919 }
920#endif
921
922 return TRUE;
923}
924
925#ifdef FEAT_GUI
926/*
927 * Set the font for the highlight group at 'idx'.
928 * 'arg' is the font name.
929 * Returns TRUE if the font is changed.
930 */
931 static int
932highlight_set_font(
933 int idx,
934 char_u *arg,
935 int is_normal_group,
936 int is_menu_group,
937 int is_tooltip_group)
938{
939 int did_change = FALSE;
940
941 // in non-GUI fonts are simply ignored
942 if (HL_TABLE()[idx].sg_font_name != NULL
943 && STRCMP(HL_TABLE()[idx].sg_font_name, arg) == 0)
944 {
945 // Font name didn't change, ignore.
946 }
947 else if (!gui.shell_created)
948 {
949 // GUI not started yet, always accept the name.
950 vim_free(HL_TABLE()[idx].sg_font_name);
951 HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
952 did_change = TRUE;
953 }
954 else
955 {
956 GuiFont temp_sg_font = HL_TABLE()[idx].sg_font;
957# ifdef FEAT_XFONTSET
958 GuiFontset temp_sg_fontset = HL_TABLE()[idx].sg_fontset;
959# endif
960 // First, save the current font/fontset.
961 // Then try to allocate the font/fontset.
962 // If the allocation fails, HL_TABLE()[idx].sg_font OR
963 // sg_fontset will be set to NOFONT or NOFONTSET respectively.
964
965 HL_TABLE()[idx].sg_font = NOFONT;
966# ifdef FEAT_XFONTSET
967 HL_TABLE()[idx].sg_fontset = NOFONTSET;
968# endif
969 hl_do_font(idx, arg, is_normal_group, is_menu_group,
970 is_tooltip_group, FALSE);
971
972# ifdef FEAT_XFONTSET
973 if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
974 {
975 // New fontset was accepted. Free the old one, if there
976 // was one.
977 gui_mch_free_fontset(temp_sg_fontset);
978 vim_free(HL_TABLE()[idx].sg_font_name);
979 HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
980 did_change = TRUE;
981 }
982 else
983 HL_TABLE()[idx].sg_fontset = temp_sg_fontset;
984# endif
985 if (HL_TABLE()[idx].sg_font != NOFONT)
986 {
987 // New font was accepted. Free the old one, if there was
988 // one.
989 gui_mch_free_font(temp_sg_font);
990 vim_free(HL_TABLE()[idx].sg_font_name);
991 HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
992 did_change = TRUE;
993 }
994 else
995 HL_TABLE()[idx].sg_font = temp_sg_font;
996 }
997
998 return did_change;
999}
1000#endif
1001
1002/*
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001003 * Set the cterm foreground color for the Normal highlight group to "color" and
1004 * the bold attribute to "bold".
1005 */
1006 static void
1007hl_set_ctermfg_normal_group(int color, int bold)
1008{
1009 cterm_normal_fg_color = color + 1;
1010 cterm_normal_fg_bold = bold;
1011#ifdef FEAT_GUI
1012 // Don't do this if the GUI is used.
1013 if (!gui.in_use && !gui.starting)
1014#endif
1015 {
1016 set_must_redraw(UPD_CLEAR);
1017 if (termcap_active && color >= 0)
1018 term_fg_color(color);
1019 }
1020}
1021
1022/*
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001023 * Set the cterm foreground color for the highlight group at 'idx' to 'color'.
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001024 */
1025 static void
1026highlight_set_ctermfg(int idx, int color, int is_normal_group)
1027{
1028 HL_TABLE()[idx].sg_cterm_fg = color + 1;
1029 if (is_normal_group)
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001030 hl_set_ctermfg_normal_group(color,
1031 (HL_TABLE()[idx].sg_cterm & HL_BOLD));
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001032}
1033
1034/*
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001035 * Set the cterm background color for the Normal highlight group to "color".
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001036 */
1037 static void
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001038hl_set_ctermbg_normal_group(int color)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001039{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001040 cterm_normal_bg_color = color + 1;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001041#ifdef FEAT_GUI
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001042 // Don't mess with 'background' if the GUI is used.
1043 if (!gui.in_use && !gui.starting)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001044#endif
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001045 {
1046 set_must_redraw(UPD_CLEAR);
1047 if (color >= 0)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001048 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001049 int dark = -1;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001050
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001051 if (termcap_active)
1052 term_bg_color(color);
1053 if (t_colors < 16)
1054 dark = (color == 0 || color == 4);
1055 // Limit the heuristic to the standard 16 colors
1056 else if (color < 16)
1057 dark = (color < 7 || color == 8);
1058 // Set the 'background' option if the value is
1059 // wrong.
1060 if (dark != -1
1061 && dark != (*p_bg == 'd')
1062 && !option_was_set((char_u *)"bg"))
1063 {
1064 set_option_value_give_err((char_u *)"bg",
1065 0L, (char_u *)(dark ? "dark" : "light"), 0);
1066 reset_option_was_set((char_u *)"bg");
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001067 }
1068 }
1069 }
1070}
1071
1072/*
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001073 * Set the cterm background color for the highlight group at 'idx' to 'color'.
1074 */
1075 static void
1076highlight_set_ctermbg(int idx, int color, int is_normal_group)
1077{
1078 HL_TABLE()[idx].sg_cterm_bg = color + 1;
1079 if (is_normal_group)
1080 hl_set_ctermbg_normal_group(color);
1081}
1082
1083/*
1084 * Set the cterm underline color for the Normal highlight group to "color".
1085 */
1086 static void
1087hl_set_ctermul_normal_group(int color)
1088{
1089 cterm_normal_ul_color = color + 1;
1090#ifdef FEAT_GUI
1091 // Don't do this if the GUI is used.
1092 if (!gui.in_use && !gui.starting)
1093#endif
1094 {
1095 set_must_redraw(UPD_CLEAR);
1096 if (termcap_active && color >= 0)
1097 term_ul_color(color);
1098 }
1099}
1100
1101/*
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001102 * Set the cterm underline color for the highlight group at 'idx' to 'color'.
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001103 */
1104 static void
1105highlight_set_ctermul(int idx, int color, int is_normal_group)
1106{
1107 HL_TABLE()[idx].sg_cterm_ul = color + 1;
1108 if (is_normal_group)
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001109 hl_set_ctermul_normal_group(color);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001110}
1111
1112/*
PMuncha606f3a2023-11-15 15:35:49 +01001113 * Set the cterm font for the highlight group at 'idx'.
1114 * 'arg' is the color name or the numeric value as a string.
1115 * 'init' is set to TRUE when initializing highlighting.
1116 * Called for the ":highlight" command and the "hlset()" function.
1117 *
1118 * Returns TRUE if the font is set.
1119 */
1120 static int
1121highlight_set_cterm_font(
1122 int idx,
1123 char_u *arg,
1124 int init)
1125{
1126 int font;
1127
1128 if (init && (HL_TABLE()[idx].sg_set & SG_CTERM))
1129 return FALSE;
1130
1131 if (!init)
1132 HL_TABLE()[idx].sg_set |= SG_CTERM;
1133
1134 if (VIM_ISDIGIT(*arg))
1135 font = atoi((char *)arg);
1136 else if (STRICMP(arg, "NONE") == 0)
1137 font = -1;
1138 else
1139 return FALSE;
1140
1141 HL_TABLE()[idx].sg_cterm_font = font + 1;
1142 return TRUE;
1143}
1144
1145/*
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001146 * Set the cterm fg/bg/ul color for the highlight group at 'idx'.
1147 * 'key' is one of 'CTERMFG' or 'CTERMBG' or 'CTERMUL'.
1148 * 'keystart' is the color name/value.
1149 * 'arg' is the color name or the numeric value as a string.
1150 * 'is_normal_group' is set if the highlight group is 'NORMAL'
1151 * 'init' is set to TRUE when initializing highlighting.
1152 * Called for the ":highlight" command and the "hlset()" function.
1153 *
1154 * Returns TRUE if the color is set.
1155 */
1156 static int
1157highlight_set_cterm_color(
1158 int idx,
1159 char_u *key,
1160 char_u *key_start,
1161 char_u *arg,
1162 int is_normal_group,
1163 int init)
1164{
1165 int color;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001166
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001167 if (init && (HL_TABLE()[idx].sg_set & SG_CTERM))
1168 return FALSE;
1169
1170 if (!init)
1171 HL_TABLE()[idx].sg_set |= SG_CTERM;
1172
1173 // When setting the foreground color, and previously the "bold"
1174 // flag was set for a light color, reset it now
1175 if (key[5] == 'F' && HL_TABLE()[idx].sg_cterm_bold)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001176 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001177 HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
1178 HL_TABLE()[idx].sg_cterm_bold = FALSE;
1179 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001180
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001181 if (VIM_ISDIGIT(*arg))
1182 color = atoi((char *)arg);
1183 else if (STRICMP(arg, "fg") == 0)
1184 {
1185 if (cterm_normal_fg_color)
1186 color = cterm_normal_fg_color - 1;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001187 else
1188 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001189 emsg(_(e_fg_color_unknown));
1190 return FALSE;
1191 }
1192 }
1193 else if (STRICMP(arg, "bg") == 0)
1194 {
1195 if (cterm_normal_bg_color > 0)
1196 color = cterm_normal_bg_color - 1;
1197 else
1198 {
1199 emsg(_(e_bg_color_unknown));
1200 return FALSE;
1201 }
1202 }
1203 else if (STRICMP(arg, "ul") == 0)
1204 {
1205 if (cterm_normal_ul_color > 0)
1206 color = cterm_normal_ul_color - 1;
1207 else
1208 {
1209 emsg(_(e_ul_color_unknown));
1210 return FALSE;
1211 }
1212 }
1213 else
1214 {
1215 int bold = MAYBE;
John Marriott34f00dd2024-04-08 23:28:12 +02001216 keyvalue_T target;
1217 keyvalue_T *entry;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001218
John Marriott34f00dd2024-04-08 23:28:12 +02001219 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01001220 target.value.string = arg;
1221 target.value.length = 0; // not used, see cmp_keyvalue_value_i()
1222 entry = (keyvalue_T *)bsearch(&target, &color_name_tab,
1223 ARRAY_LENGTH(color_name_tab), sizeof(color_name_tab[0]),
1224 cmp_keyvalue_value_i);
John Marriott34f00dd2024-04-08 23:28:12 +02001225 if (entry == NULL)
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001226 {
1227 semsg(_(e_color_name_or_number_not_recognized_str), key_start);
1228 return FALSE;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001229 }
1230
John Marriott34f00dd2024-04-08 23:28:12 +02001231 color = lookup_color(entry->key, key[5] == 'F', &bold);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001232
1233 // set/reset bold attribute to get light foreground
1234 // colors (on some terminals, e.g. "linux")
1235 if (bold == TRUE)
1236 {
1237 HL_TABLE()[idx].sg_cterm |= HL_BOLD;
1238 HL_TABLE()[idx].sg_cterm_bold = TRUE;
1239 }
1240 else if (bold == FALSE)
1241 HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001242 }
1243
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001244 // Add one to the argument, to avoid zero. Zero is used for
1245 // "NONE", then "color" is -1.
1246 if (key[5] == 'F')
1247 highlight_set_ctermfg(idx, color, is_normal_group);
1248 else if (key[5] == 'B')
1249 highlight_set_ctermbg(idx, color, is_normal_group);
1250 else // ctermul
1251 highlight_set_ctermul(idx, color, is_normal_group);
1252
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001253 return TRUE;
1254}
1255
1256#if defined(FEAT_GUI) || defined(FEAT_EVAL)
1257/*
1258 * Set the GUI foreground color for the highlight group at 'idx'.
1259 * Returns TRUE if the color is set.
1260 */
1261 static int
1262highlight_set_guifg(
1263 int idx,
1264 char_u *arg,
1265 int is_menu_group UNUSED,
1266 int is_scrollbar_group UNUSED,
1267 int is_tooltip_group UNUSED,
1268 int *do_colors UNUSED,
1269 int init)
1270{
Bram Moolenaar731fba12021-10-19 20:24:34 +01001271# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001272 long i;
Bram Moolenaar731fba12021-10-19 20:24:34 +01001273# endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001274 char_u **namep;
1275 int did_change = FALSE;
1276
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001277 if (init && (HL_TABLE()[idx].sg_set & SG_GUI))
1278 return FALSE;
1279
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001280 namep = &HL_TABLE()[idx].sg_gui_fg_name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001281 if (!init)
1282 HL_TABLE()[idx].sg_set |= SG_GUI;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001283
1284# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001285 // In GUI guifg colors are only used when recognized
1286 i = color_name2handle(arg);
1287 if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !USE_24BIT)
1288 {
1289 HL_TABLE()[idx].sg_gui_fg = i;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001290# endif
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001291 if (*namep == NULL || STRCMP(*namep, arg) != 0)
1292 {
1293 vim_free(*namep);
1294 if (STRCMP(arg, "NONE") != 0)
1295 *namep = vim_strsave(arg);
1296 else
1297 *namep = NULL;
1298 did_change = TRUE;
1299 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001300# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1301# ifdef FEAT_GUI_X11
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001302 if (is_menu_group && gui.menu_fg_pixel != i)
1303 {
1304 gui.menu_fg_pixel = i;
1305 *do_colors = TRUE;
1306 }
1307 if (is_scrollbar_group && gui.scroll_fg_pixel != i)
1308 {
1309 gui.scroll_fg_pixel = i;
1310 *do_colors = TRUE;
1311 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001312# ifdef FEAT_BEVAL_GUI
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001313 if (is_tooltip_group && gui.tooltip_fg_pixel != i)
1314 {
1315 gui.tooltip_fg_pixel = i;
1316 *do_colors = TRUE;
1317 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001318# endif
1319# endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001320 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001321# endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001322
1323 return did_change;
1324}
1325
1326/*
1327 * Set the GUI background color for the highlight group at 'idx'.
1328 * Returns TRUE if the color is set.
1329 */
1330 static int
1331highlight_set_guibg(
1332 int idx,
1333 char_u *arg,
1334 int is_menu_group UNUSED,
1335 int is_scrollbar_group UNUSED,
1336 int is_tooltip_group UNUSED,
1337 int *do_colors UNUSED,
1338 int init)
1339{
Bram Moolenaar731fba12021-10-19 20:24:34 +01001340# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001341 int i;
Bram Moolenaar731fba12021-10-19 20:24:34 +01001342# endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001343 char_u **namep;
1344 int did_change = FALSE;
1345
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001346 if (init && (HL_TABLE()[idx].sg_set & SG_GUI))
1347 return FALSE;
1348
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001349 namep = &HL_TABLE()[idx].sg_gui_bg_name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001350 if (!init)
1351 HL_TABLE()[idx].sg_set |= SG_GUI;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001352
1353# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001354 // In GUI guibg colors are only used when recognized
1355 i = color_name2handle(arg);
1356 if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !USE_24BIT)
1357 {
1358 HL_TABLE()[idx].sg_gui_bg = i;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001359# endif
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001360 if (*namep == NULL || STRCMP(*namep, arg) != 0)
1361 {
1362 vim_free(*namep);
1363 if (STRCMP(arg, "NONE") != 0)
1364 *namep = vim_strsave(arg);
1365 else
1366 *namep = NULL;
1367 did_change = TRUE;
1368 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001369# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1370# ifdef FEAT_GUI_X11
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001371 if (is_menu_group && gui.menu_bg_pixel != i)
1372 {
1373 gui.menu_bg_pixel = i;
1374 *do_colors = TRUE;
1375 }
1376 if (is_scrollbar_group && gui.scroll_bg_pixel != i)
1377 {
1378 gui.scroll_bg_pixel = i;
1379 *do_colors = TRUE;
1380 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001381# ifdef FEAT_BEVAL_GUI
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001382 if (is_tooltip_group && gui.tooltip_bg_pixel != i)
1383 {
1384 gui.tooltip_bg_pixel = i;
1385 *do_colors = TRUE;
1386 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001387# endif
1388# endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001389 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001390# endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001391
1392 return did_change;
1393}
1394
1395/*
1396 * Set the GUI undercurl/strikethrough color for the highlight group at 'idx'.
1397 * Returns TRUE if the color is set.
1398 */
1399 static int
1400highlight_set_guisp(int idx, char_u *arg, int init)
1401{
1402# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1403 int i;
1404# endif
1405 int did_change = FALSE;
1406 char_u **namep;
1407
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001408 if (init && (HL_TABLE()[idx].sg_set & SG_GUI))
1409 return FALSE;
1410
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001411 namep = &HL_TABLE()[idx].sg_gui_sp_name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001412 if (!init)
1413 HL_TABLE()[idx].sg_set |= SG_GUI;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001414
1415# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001416 // In GUI guisp colors are only used when recognized
1417 i = color_name2handle(arg);
1418 if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !USE_24BIT)
1419 {
1420 HL_TABLE()[idx].sg_gui_sp = i;
1421# endif
1422 if (*namep == NULL || STRCMP(*namep, arg) != 0)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001423 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001424 vim_free(*namep);
1425 if (STRCMP(arg, "NONE") != 0)
1426 *namep = vim_strsave(arg);
1427 else
1428 *namep = NULL;
1429 did_change = TRUE;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001430 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001431# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001432 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001433# endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001434
1435 return did_change;
1436}
1437#endif
1438
1439/*
1440 * Set the start/stop terminal codes for a highlight group.
1441 * Returns TRUE if the terminal code is set.
1442 */
1443 static int
1444highlight_set_startstop_termcode(int idx, char_u *key, char_u *arg, int init)
1445{
1446 int off;
1447 char_u buf[100];
1448 int len;
1449 char_u *tname;
1450 char_u *p;
1451
1452 if (!init)
1453 HL_TABLE()[idx].sg_set |= SG_TERM;
1454
1455 // The "start" and "stop" arguments can be a literal escape
1456 // sequence, or a comma separated list of terminal codes.
1457 if (STRNCMP(arg, "t_", 2) == 0)
1458 {
1459 off = 0;
1460 buf[0] = 0;
1461 while (arg[off] != NUL)
1462 {
1463 // Isolate one termcap name
1464 for (len = 0; arg[off + len] &&
1465 arg[off + len] != ','; ++len)
1466 ;
1467 tname = vim_strnsave(arg + off, len);
1468 if (tname == NULL) // out of memory
1469 return FALSE;
1470 // lookup the escape sequence for the item
1471 p = get_term_code(tname);
1472 vim_free(tname);
1473 if (p == NULL) // ignore non-existing things
1474 p = (char_u *)"";
1475
1476 // Append it to the already found stuff
1477 if ((int)(STRLEN(buf) + STRLEN(p)) >= 99)
1478 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001479 semsg(_(e_terminal_code_too_long_str), arg);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001480 return FALSE;
1481 }
1482 STRCAT(buf, p);
1483
1484 // Advance to the next item
1485 off += len;
1486 if (arg[off] == ',') // another one follows
1487 ++off;
1488 }
1489 }
1490 else
1491 {
1492 // Copy characters from arg[] to buf[], translating <> codes.
1493 for (p = arg, off = 0; off < 100 - 6 && *p; )
1494 {
zeertzjqdb088872022-05-02 22:53:45 +01001495 len = trans_special(&p, buf + off, FSK_SIMPLIFY, FALSE, NULL);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001496 if (len > 0) // recognized special char
1497 off += len;
1498 else // copy as normal char
1499 buf[off++] = *p++;
1500 }
1501 buf[off] = NUL;
1502 }
1503
1504 if (STRCMP(buf, "NONE") == 0) // resetting the value
1505 p = NULL;
1506 else
1507 p = vim_strsave(buf);
1508 if (key[2] == 'A')
1509 {
1510 vim_free(HL_TABLE()[idx].sg_start);
1511 HL_TABLE()[idx].sg_start = p;
1512 }
1513 else
1514 {
1515 vim_free(HL_TABLE()[idx].sg_stop);
1516 HL_TABLE()[idx].sg_stop = p;
1517 }
1518 return TRUE;
1519}
1520
1521/*
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001522 * Handle the ":highlight .." command.
1523 * When using ":hi clear" this is called recursively for each group with
1524 * "forceit" and "init" both TRUE.
1525 */
1526 void
1527do_highlight(
1528 char_u *line,
1529 int forceit,
1530 int init) // TRUE when called for initializing
1531{
1532 char_u *name_end;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001533 char_u *linep;
1534 char_u *key_start;
1535 char_u *arg_start;
1536 char_u *key = NULL, *arg = NULL;
1537 long i;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001538 int id;
1539 int idx;
1540 hl_group_T item_before;
1541 int did_change = FALSE;
1542 int dodefault = FALSE;
1543 int doclear = FALSE;
1544 int dolink = FALSE;
1545 int error = FALSE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001546 int is_normal_group = FALSE; // "Normal" group
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001547#ifdef FEAT_GUI_X11
1548 int is_menu_group = FALSE; // "Menu" group
1549 int is_scrollbar_group = FALSE; // "Scrollbar" group
1550 int is_tooltip_group = FALSE; // "Tooltip" group
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001551#else
1552# define is_menu_group 0
1553# define is_tooltip_group 0
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001554# define is_scrollbar_group 0
1555#endif
1556#if defined(FEAT_GUI) || defined(FEAT_EVAL)
1557 int do_colors = FALSE; // need to update colors?
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001558#endif
1559#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1560 int did_highlight_changed = FALSE;
1561#endif
1562
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001563 // If no argument, list current highlighting.
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02001564 if (!init && ends_excmd2(line - 1, line))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001565 {
1566 for (i = 1; i <= highlight_ga.ga_len && !got_int; ++i)
1567 // TODO: only call when the group has attributes set
1568 highlight_list_one((int)i);
1569 return;
1570 }
1571
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001572 // Isolate the name.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001573 name_end = skiptowhite(line);
1574 linep = skipwhite(name_end);
1575
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001576 // Check for "default" argument.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001577 if (STRNCMP(line, "default", name_end - line) == 0)
1578 {
1579 dodefault = TRUE;
1580 line = linep;
1581 name_end = skiptowhite(line);
1582 linep = skipwhite(name_end);
1583 }
1584
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001585 // Check for "clear" or "link" argument.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001586 if (STRNCMP(line, "clear", name_end - line) == 0)
1587 doclear = TRUE;
1588 if (STRNCMP(line, "link", name_end - line) == 0)
1589 dolink = TRUE;
1590
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001591 // ":highlight {group-name}": list highlighting for one group.
Bram Moolenaar1966c242020-04-20 22:42:32 +02001592 if (!doclear && !dolink && ends_excmd2(line, linep))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001593 {
1594 id = syn_namen2id(line, (int)(name_end - line));
1595 if (id == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001596 semsg(_(e_highlight_group_name_not_found_str), line);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001597 else
1598 highlight_list_one(id);
1599 return;
1600 }
1601
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001602 // Handle ":highlight link {from} {to}" command.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001603 if (dolink)
1604 {
1605 char_u *from_start = linep;
1606 char_u *from_end;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001607 int from_len;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001608 char_u *to_start;
1609 char_u *to_end;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001610 int to_len;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001611
1612 from_end = skiptowhite(from_start);
1613 to_start = skipwhite(from_end);
1614 to_end = skiptowhite(to_start);
1615
Bram Moolenaar1966c242020-04-20 22:42:32 +02001616 if (ends_excmd2(line, from_start) || ends_excmd2(line, to_start))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001617 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001618 semsg(_(e_not_enough_arguments_highlight_link_str), from_start);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001619 return;
1620 }
1621
Bram Moolenaar1966c242020-04-20 22:42:32 +02001622 if (!ends_excmd2(line, skipwhite(to_end)))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001623 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001624 semsg(_(e_too_many_arguments_highlight_link_str), from_start);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001625 return;
1626 }
1627
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001628 from_len = (int)(from_end - from_start);
1629 to_len = (int)(to_end - to_start);
1630 highlight_group_link(from_start, from_len, to_start, to_len,
1631 dodefault, forceit, init);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001632 return;
1633 }
1634
1635 if (doclear)
1636 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001637 // ":highlight clear [group]" command.
Bram Moolenaar1966c242020-04-20 22:42:32 +02001638 if (ends_excmd2(line, linep))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001639 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001640 // ":highlight clear" without group name
1641 highlight_reset_all();
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001642 return;
1643 }
Bram Moolenaar1966c242020-04-20 22:42:32 +02001644 line = linep;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001645 name_end = skiptowhite(line);
1646 linep = skipwhite(name_end);
1647 }
1648
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001649 // Find the group name in the table. If it does not exist yet, add it.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001650 id = syn_check_group(line, (int)(name_end - line));
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001651 if (id == 0) // failed (out of memory)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001652 return;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001653 idx = id - 1; // index is ID minus one
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001654
1655 // Return if "default" was used and the group already has settings.
1656 if (dodefault && hl_has_settings(idx, TRUE))
1657 return;
1658
1659 // Make a copy so we can check if any attribute actually changed.
1660 item_before = HL_TABLE()[idx];
1661
1662 if (STRCMP(HL_TABLE()[idx].sg_name_u, "NORMAL") == 0)
1663 is_normal_group = TRUE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001664#ifdef FEAT_GUI_X11
1665 else if (STRCMP(HL_TABLE()[idx].sg_name_u, "MENU") == 0)
1666 is_menu_group = TRUE;
1667 else if (STRCMP(HL_TABLE()[idx].sg_name_u, "SCROLLBAR") == 0)
1668 is_scrollbar_group = TRUE;
1669 else if (STRCMP(HL_TABLE()[idx].sg_name_u, "TOOLTIP") == 0)
1670 is_tooltip_group = TRUE;
1671#endif
1672
1673 // Clear the highlighting for ":hi clear {group}" and ":hi clear".
1674 if (doclear || (forceit && init))
1675 {
1676 highlight_clear(idx);
1677 if (!doclear)
1678 HL_TABLE()[idx].sg_set = 0;
1679 }
1680
1681 if (!doclear)
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001682 while (!ends_excmd2(line, linep))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001683 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001684 key_start = linep;
1685 if (*linep == '=')
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001686 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001687 semsg(_(e_unexpected_equal_sign_str), key_start);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001688 error = TRUE;
1689 break;
1690 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001691
1692 // Isolate the key ("term", "ctermfg", "ctermbg", "font", "guifg"
1693 // or "guibg").
1694 while (*linep && !VIM_ISWHITE(*linep) && *linep != '=')
1695 ++linep;
1696 vim_free(key);
1697 key = vim_strnsave_up(key_start, linep - key_start);
1698 if (key == NULL)
1699 {
1700 error = TRUE;
1701 break;
1702 }
1703 linep = skipwhite(linep);
1704
1705 if (STRCMP(key, "NONE") == 0)
1706 {
1707 if (!init || HL_TABLE()[idx].sg_set == 0)
1708 {
1709 if (!init)
1710 HL_TABLE()[idx].sg_set |= SG_TERM+SG_CTERM+SG_GUI;
1711 highlight_clear(idx);
1712 }
1713 continue;
1714 }
1715
1716 // Check for the equal sign.
1717 if (*linep != '=')
1718 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001719 semsg(_(e_missing_equal_sign_str_2), key_start);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001720 error = TRUE;
1721 break;
1722 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001723 ++linep;
1724
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001725 // Isolate the argument.
1726 linep = skipwhite(linep);
1727 if (*linep == '\'') // guifg='color name'
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001728 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001729 arg_start = ++linep;
1730 linep = vim_strchr(linep, '\'');
1731 if (linep == NULL)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001732 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001733 semsg(_(e_invalid_argument_str), key_start);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001734 error = TRUE;
1735 break;
1736 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001737 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001738 else
1739 {
1740 arg_start = linep;
1741 linep = skiptowhite(linep);
1742 }
1743 if (linep == arg_start)
1744 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001745 semsg(_(e_missing_argument_str), key_start);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001746 error = TRUE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001747 break;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001748 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001749 vim_free(arg);
1750 arg = vim_strnsave(arg_start, linep - arg_start);
1751 if (arg == NULL)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001752 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001753 error = TRUE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001754 break;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001755 }
1756 if (*linep == '\'')
1757 ++linep;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001758
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001759 // Store the argument.
1760 if (STRCMP(key, "TERM") == 0
1761 || STRCMP(key, "CTERM") == 0
1762 || STRCMP(key, "GUI") == 0)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001763 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001764 if (!highlight_set_termgui_attr(idx, key, arg, init))
1765 {
1766 error = TRUE;
1767 break;
1768 }
1769 }
1770 else if (STRCMP(key, "FONT") == 0)
1771 {
1772 // in non-GUI fonts are simply ignored
1773#ifdef FEAT_GUI
1774 if (highlight_set_font(idx, arg, is_normal_group,
1775 is_menu_group, is_tooltip_group))
1776 did_change = TRUE;
1777#endif
1778 }
1779 else if (STRCMP(key, "CTERMFG") == 0
1780 || STRCMP(key, "CTERMBG") == 0
1781 || STRCMP(key, "CTERMUL") == 0)
1782 {
1783 if (!highlight_set_cterm_color(idx, key, key_start, arg,
1784 is_normal_group, init))
1785 {
1786 error = TRUE;
1787 break;
1788 }
1789 }
PMuncha606f3a2023-11-15 15:35:49 +01001790 else if (STRCMP(key, "CTERMFONT") == 0)
1791 {
1792 if (!highlight_set_cterm_font(idx, arg, init))
1793 {
1794 error = TRUE;
1795 break;
1796 }
1797 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001798 else if (STRCMP(key, "GUIFG") == 0)
1799 {
1800#if defined(FEAT_GUI) || defined(FEAT_EVAL)
1801 if (highlight_set_guifg(idx, arg, is_menu_group,
1802 is_scrollbar_group, is_tooltip_group,
1803 &do_colors, init))
1804 did_change = TRUE;
1805#endif
1806 }
1807 else if (STRCMP(key, "GUIBG") == 0)
1808 {
1809#if defined(FEAT_GUI) || defined(FEAT_EVAL)
1810 if (highlight_set_guibg(idx, arg, is_menu_group,
1811 is_scrollbar_group, is_tooltip_group,
1812 &do_colors, init))
1813 did_change = TRUE;
1814#endif
1815 }
1816 else if (STRCMP(key, "GUISP") == 0)
1817 {
1818#if defined(FEAT_GUI) || defined(FEAT_EVAL)
1819 if (highlight_set_guisp(idx, arg, init))
1820 did_change = TRUE;
1821#endif
1822 }
1823 else if (STRCMP(key, "START") == 0 || STRCMP(key, "STOP") == 0)
1824 {
1825 if (!highlight_set_startstop_termcode(idx, key, arg, init))
1826 {
1827 error = TRUE;
1828 break;
1829 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001830 }
1831 else
1832 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001833 semsg(_(e_illegal_argument_str_3), key_start);
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001834 error = TRUE;
1835 break;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001836 }
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001837 HL_TABLE()[idx].sg_cleared = FALSE;
1838
1839 // When highlighting has been given for a group, don't link it.
1840 if (!init || !(HL_TABLE()[idx].sg_set & SG_LINK))
1841 HL_TABLE()[idx].sg_link = 0;
1842
1843 // Continue with next argument.
1844 linep = skipwhite(linep);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001845 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001846
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001847 // If there is an error, and it's a new entry, remove it from the table.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001848 if (error && idx == highlight_ga.ga_len)
1849 syn_unadd_group();
1850 else
1851 {
1852 if (is_normal_group)
1853 {
1854 HL_TABLE()[idx].sg_term_attr = 0;
1855 HL_TABLE()[idx].sg_cterm_attr = 0;
1856#ifdef FEAT_GUI
1857 HL_TABLE()[idx].sg_gui_attr = 0;
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01001858 // Need to update all groups, because they might be using "bg"
1859 // and/or "fg", which have been changed now.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001860#endif
1861#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1862 if (USE_24BIT)
1863 {
1864 highlight_gui_started();
1865 did_highlight_changed = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001866 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001867 }
1868#endif
Bram Moolenaara8bd3492020-03-23 21:45:29 +01001869#ifdef FEAT_VTP
1870 control_console_color_rgb();
1871#endif
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001872 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001873#ifdef FEAT_GUI_X11
1874# ifdef FEAT_MENU
1875 else if (is_menu_group)
1876 {
1877 if (gui.in_use && do_colors)
1878 gui_mch_new_menu_colors();
1879 }
1880# endif
1881 else if (is_scrollbar_group)
1882 {
1883 if (gui.in_use && do_colors)
1884 gui_new_scrollbar_colors();
1885 else
1886 set_hl_attr(idx);
1887 }
1888# ifdef FEAT_BEVAL_GUI
1889 else if (is_tooltip_group)
1890 {
1891 if (gui.in_use && do_colors)
1892 gui_mch_new_tooltip_colors();
1893 }
1894# endif
1895#endif
1896 else
1897 set_hl_attr(idx);
1898#ifdef FEAT_EVAL
1899 HL_TABLE()[idx].sg_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001900 HL_TABLE()[idx].sg_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001901#endif
1902 }
1903
1904 vim_free(key);
1905 vim_free(arg);
1906
1907 // Only call highlight_changed() once, after a sequence of highlight
1908 // commands, and only if an attribute actually changed.
1909 if ((did_change
1910 || memcmp(&HL_TABLE()[idx], &item_before, sizeof(item_before)) != 0)
1911#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1912 && !did_highlight_changed
1913#endif
1914 )
1915 {
1916 // Do not trigger a redraw when highlighting is changed while
1917 // redrawing. This may happen when evaluating 'statusline' changes the
1918 // StatusLine group.
1919 if (!updating_screen)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001920 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001921 need_highlight_changed = TRUE;
1922 }
1923}
1924
1925#if defined(EXITFREE) || defined(PROTO)
1926 void
1927free_highlight(void)
1928{
1929 int i;
1930
1931 for (i = 0; i < highlight_ga.ga_len; ++i)
1932 {
1933 highlight_clear(i);
1934 vim_free(HL_TABLE()[i].sg_name);
1935 vim_free(HL_TABLE()[i].sg_name_u);
1936 }
1937 ga_clear(&highlight_ga);
1938}
1939#endif
1940
1941/*
1942 * Reset the cterm colors to what they were before Vim was started, if
1943 * possible. Otherwise reset them to zero.
1944 */
1945 void
1946restore_cterm_colors(void)
1947{
1948#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
1949 // Since t_me has been set, this probably means that the user
1950 // wants to use this as default colors. Need to reset default
1951 // background/foreground colors.
1952 mch_set_normal_colors();
1953#else
1954# ifdef VIMDLL
1955 if (!gui.in_use)
1956 {
1957 mch_set_normal_colors();
1958 return;
1959 }
1960# endif
1961 cterm_normal_fg_color = 0;
1962 cterm_normal_fg_bold = 0;
1963 cterm_normal_bg_color = 0;
1964# ifdef FEAT_TERMGUICOLORS
1965 cterm_normal_fg_gui_color = INVALCOLOR;
1966 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaare023e882020-05-31 16:42:30 +02001967 cterm_normal_ul_gui_color = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001968# endif
1969#endif
1970}
1971
1972/*
1973 * Return TRUE if highlight group "idx" has any settings.
1974 * When "check_link" is TRUE also check for an existing link.
1975 */
1976 static int
1977hl_has_settings(int idx, int check_link)
1978{
Bram Moolenaar05eb5b92020-09-16 15:43:21 +02001979 return HL_TABLE()[idx].sg_cleared == 0
1980 && ( HL_TABLE()[idx].sg_term_attr != 0
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001981 || HL_TABLE()[idx].sg_cterm_attr != 0
1982 || HL_TABLE()[idx].sg_cterm_fg != 0
1983 || HL_TABLE()[idx].sg_cterm_bg != 0
PMuncha606f3a2023-11-15 15:35:49 +01001984 || HL_TABLE()[idx].sg_cterm_font != 0
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02001985#ifdef FEAT_GUI
1986 || HL_TABLE()[idx].sg_gui_attr != 0
1987 || HL_TABLE()[idx].sg_gui_fg_name != NULL
1988 || HL_TABLE()[idx].sg_gui_bg_name != NULL
1989 || HL_TABLE()[idx].sg_gui_sp_name != NULL
1990 || HL_TABLE()[idx].sg_font_name != NULL
1991#endif
1992 || (check_link && (HL_TABLE()[idx].sg_set & SG_LINK)));
1993}
1994
1995/*
1996 * Clear highlighting for one group.
1997 */
1998 static void
1999highlight_clear(int idx)
2000{
2001 HL_TABLE()[idx].sg_cleared = TRUE;
2002
2003 HL_TABLE()[idx].sg_term = 0;
2004 VIM_CLEAR(HL_TABLE()[idx].sg_start);
2005 VIM_CLEAR(HL_TABLE()[idx].sg_stop);
2006 HL_TABLE()[idx].sg_term_attr = 0;
2007 HL_TABLE()[idx].sg_cterm = 0;
2008 HL_TABLE()[idx].sg_cterm_bold = FALSE;
2009 HL_TABLE()[idx].sg_cterm_fg = 0;
2010 HL_TABLE()[idx].sg_cterm_bg = 0;
2011 HL_TABLE()[idx].sg_cterm_attr = 0;
PMuncha606f3a2023-11-15 15:35:49 +01002012 HL_TABLE()[idx].sg_cterm_font = 0;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002013#if defined(FEAT_GUI) || defined(FEAT_EVAL)
2014 HL_TABLE()[idx].sg_gui = 0;
2015 VIM_CLEAR(HL_TABLE()[idx].sg_gui_fg_name);
2016 VIM_CLEAR(HL_TABLE()[idx].sg_gui_bg_name);
2017 VIM_CLEAR(HL_TABLE()[idx].sg_gui_sp_name);
2018#endif
2019#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
2020 HL_TABLE()[idx].sg_gui_fg = INVALCOLOR;
2021 HL_TABLE()[idx].sg_gui_bg = INVALCOLOR;
Bram Moolenaare023e882020-05-31 16:42:30 +02002022 HL_TABLE()[idx].sg_gui_sp = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002023#endif
2024#ifdef FEAT_GUI
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002025 gui_mch_free_font(HL_TABLE()[idx].sg_font);
2026 HL_TABLE()[idx].sg_font = NOFONT;
2027# ifdef FEAT_XFONTSET
2028 gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
2029 HL_TABLE()[idx].sg_fontset = NOFONTSET;
2030# endif
2031 VIM_CLEAR(HL_TABLE()[idx].sg_font_name);
2032 HL_TABLE()[idx].sg_gui_attr = 0;
2033#endif
Bram Moolenaare8df0102020-09-18 19:40:45 +02002034 // Restore default link and context if they exist. Otherwise clears.
Bram Moolenaar213da552020-09-17 19:59:26 +02002035 HL_TABLE()[idx].sg_link = HL_TABLE()[idx].sg_deflink;
Bram Moolenaare8df0102020-09-18 19:40:45 +02002036#ifdef FEAT_EVAL
2037 // Since we set the default link, set the location to where the default
2038 // link was set.
2039 HL_TABLE()[idx].sg_script_ctx = HL_TABLE()[idx].sg_deflink_sctx;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002040#endif
2041}
2042
2043#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
2044/*
2045 * Set the normal foreground and background colors according to the "Normal"
2046 * highlighting group. For X11 also set "Menu", "Scrollbar", and
2047 * "Tooltip" colors.
2048 */
2049 void
2050set_normal_colors(void)
2051{
2052# ifdef FEAT_GUI
2053# ifdef FEAT_TERMGUICOLORS
2054 if (gui.in_use)
2055# endif
2056 {
2057 if (set_group_colors((char_u *)"Normal",
2058 &gui.norm_pixel, &gui.back_pixel,
2059 FALSE, TRUE, FALSE))
2060 {
2061 gui_mch_new_colors();
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002062 set_must_redraw(UPD_CLEAR);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002063 }
2064# ifdef FEAT_GUI_X11
2065 if (set_group_colors((char_u *)"Menu",
2066 &gui.menu_fg_pixel, &gui.menu_bg_pixel,
2067 TRUE, FALSE, FALSE))
2068 {
2069# ifdef FEAT_MENU
2070 gui_mch_new_menu_colors();
2071# endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002072 set_must_redraw(UPD_CLEAR);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002073 }
2074# ifdef FEAT_BEVAL_GUI
2075 if (set_group_colors((char_u *)"Tooltip",
2076 &gui.tooltip_fg_pixel, &gui.tooltip_bg_pixel,
2077 FALSE, FALSE, TRUE))
2078 {
2079# ifdef FEAT_TOOLBAR
2080 gui_mch_new_tooltip_colors();
2081# endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002082 set_must_redraw(UPD_CLEAR);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002083 }
2084# endif
2085 if (set_group_colors((char_u *)"Scrollbar",
2086 &gui.scroll_fg_pixel, &gui.scroll_bg_pixel,
2087 FALSE, FALSE, FALSE))
2088 {
2089 gui_new_scrollbar_colors();
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002090 set_must_redraw(UPD_CLEAR);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002091 }
2092# endif
2093 }
2094# endif
2095# ifdef FEAT_TERMGUICOLORS
2096# ifdef FEAT_GUI
2097 else
2098# endif
2099 {
2100 int idx;
2101
2102 idx = syn_name2id((char_u *)"Normal") - 1;
2103 if (idx >= 0)
2104 {
2105 gui_do_one_color(idx, FALSE, FALSE);
2106
2107 // If the normal fg or bg color changed a complete redraw is
2108 // required.
2109 if (cterm_normal_fg_gui_color != HL_TABLE()[idx].sg_gui_fg
2110 || cterm_normal_bg_gui_color != HL_TABLE()[idx].sg_gui_bg)
2111 {
2112 // if the GUI color is INVALCOLOR then we use the default cterm
2113 // color
2114 cterm_normal_fg_gui_color = HL_TABLE()[idx].sg_gui_fg;
2115 cterm_normal_bg_gui_color = HL_TABLE()[idx].sg_gui_bg;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002116 set_must_redraw(UPD_CLEAR);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002117 }
2118 }
2119 }
2120# endif
2121}
2122#endif
2123
2124#if defined(FEAT_GUI) || defined(PROTO)
2125/*
2126 * Set the colors for "Normal", "Menu", "Tooltip" or "Scrollbar".
2127 */
2128 static int
2129set_group_colors(
2130 char_u *name,
2131 guicolor_T *fgp,
2132 guicolor_T *bgp,
2133 int do_menu,
2134 int use_norm,
2135 int do_tooltip)
2136{
2137 int idx;
2138
2139 idx = syn_name2id(name) - 1;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002140 if (idx < 0)
2141 return FALSE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002142
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002143 gui_do_one_color(idx, do_menu, do_tooltip);
2144
2145 if (HL_TABLE()[idx].sg_gui_fg != INVALCOLOR)
2146 *fgp = HL_TABLE()[idx].sg_gui_fg;
2147 else if (use_norm)
2148 *fgp = gui.def_norm_pixel;
2149 if (HL_TABLE()[idx].sg_gui_bg != INVALCOLOR)
2150 *bgp = HL_TABLE()[idx].sg_gui_bg;
2151 else if (use_norm)
2152 *bgp = gui.def_back_pixel;
2153 return TRUE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002154}
2155
2156/*
2157 * Get the font of the "Normal" group.
2158 * Returns "" when it's not found or not set.
2159 */
2160 char_u *
2161hl_get_font_name(void)
2162{
2163 int id;
2164 char_u *s;
2165
2166 id = syn_name2id((char_u *)"Normal");
2167 if (id > 0)
2168 {
2169 s = HL_TABLE()[id - 1].sg_font_name;
2170 if (s != NULL)
2171 return s;
2172 }
2173 return (char_u *)"";
2174}
2175
2176/*
2177 * Set font for "Normal" group. Called by gui_mch_init_font() when a font has
2178 * actually chosen to be used.
2179 */
2180 void
2181hl_set_font_name(char_u *font_name)
2182{
2183 int id;
2184
2185 id = syn_name2id((char_u *)"Normal");
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002186 if (id <= 0)
2187 return;
2188
2189 vim_free(HL_TABLE()[id - 1].sg_font_name);
2190 HL_TABLE()[id - 1].sg_font_name = vim_strsave(font_name);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002191}
2192
2193/*
2194 * Set background color for "Normal" group. Called by gui_set_bg_color()
2195 * when the color is known.
2196 */
2197 void
2198hl_set_bg_color_name(
2199 char_u *name) // must have been allocated
2200{
2201 int id;
2202
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002203 if (name == NULL)
2204 return;
2205
2206 id = syn_name2id((char_u *)"Normal");
2207 if (id <= 0)
2208 return;
2209
2210 vim_free(HL_TABLE()[id - 1].sg_gui_bg_name);
2211 HL_TABLE()[id - 1].sg_gui_bg_name = name;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002212}
2213
2214/*
2215 * Set foreground color for "Normal" group. Called by gui_set_fg_color()
2216 * when the color is known.
2217 */
2218 void
2219hl_set_fg_color_name(
2220 char_u *name) // must have been allocated
2221{
2222 int id;
2223
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002224 if (name == NULL)
2225 return;
2226
2227 id = syn_name2id((char_u *)"Normal");
2228 if (id <= 0)
2229 return;
2230
2231 vim_free(HL_TABLE()[id - 1].sg_gui_fg_name);
2232 HL_TABLE()[id - 1].sg_gui_fg_name = name;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002233}
2234
2235/*
2236 * Return the handle for a font name.
2237 * Returns NOFONT when failed.
2238 */
2239 static GuiFont
2240font_name2handle(char_u *name)
2241{
2242 if (STRCMP(name, "NONE") == 0)
2243 return NOFONT;
2244
2245 return gui_mch_get_font(name, TRUE);
2246}
2247
2248# ifdef FEAT_XFONTSET
2249/*
2250 * Return the handle for a fontset name.
2251 * Returns NOFONTSET when failed.
2252 */
2253 static GuiFontset
2254fontset_name2handle(char_u *name, int fixed_width)
2255{
2256 if (STRCMP(name, "NONE") == 0)
2257 return NOFONTSET;
2258
2259 return gui_mch_get_fontset(name, TRUE, fixed_width);
2260}
2261# endif
2262
2263/*
2264 * Get the font or fontset for one highlight group.
2265 */
2266 static void
2267hl_do_font(
2268 int idx,
2269 char_u *arg,
2270 int do_normal, // set normal font
2271 int do_menu UNUSED, // set menu font
2272 int do_tooltip UNUSED, // set tooltip font
2273 int free_font) // free current font/fontset
2274{
2275# ifdef FEAT_XFONTSET
2276 // If 'guifontset' is not empty, first try using the name as a
2277 // fontset. If that doesn't work, use it as a font name.
2278 if (*p_guifontset != NUL
2279# ifdef FONTSET_ALWAYS
2280 || do_menu
2281# endif
2282# ifdef FEAT_BEVAL_TIP
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002283 // In Motif, the Tooltip highlight group is always a fontset
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002284 || do_tooltip
2285# endif
2286 )
2287 {
2288 if (free_font)
2289 gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
2290 HL_TABLE()[idx].sg_fontset = fontset_name2handle(arg, 0
2291# ifdef FONTSET_ALWAYS
2292 || do_menu
2293# endif
2294# ifdef FEAT_BEVAL_TIP
2295 || do_tooltip
2296# endif
2297 );
2298 }
2299 if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
2300 {
2301 // If it worked and it's the Normal group, use it as the normal
2302 // fontset. Same for the Menu group.
2303 if (do_normal)
2304 gui_init_font(arg, TRUE);
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002305# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002306 if (do_menu)
2307 {
2308# ifdef FONTSET_ALWAYS
2309 gui.menu_fontset = HL_TABLE()[idx].sg_fontset;
2310# else
2311 // YIKES! This is a bug waiting to crash the program
2312 gui.menu_font = HL_TABLE()[idx].sg_fontset;
2313# endif
2314 gui_mch_new_menu_font();
2315 }
2316# ifdef FEAT_BEVAL_GUI
2317 if (do_tooltip)
2318 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002319 // The Athena widget set could not handle switching between
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002320 // displaying a single font and a fontset.
2321 // If the XtNinternational resource is set to True at widget
2322 // creation, then a fontset is always used, otherwise an
2323 // XFontStruct is used.
2324 gui.tooltip_fontset = (XFontSet)HL_TABLE()[idx].sg_fontset;
2325 gui_mch_new_tooltip_font();
2326 }
2327# endif
2328# endif
2329 }
2330 else
2331# endif
2332 {
2333 if (free_font)
2334 gui_mch_free_font(HL_TABLE()[idx].sg_font);
2335 HL_TABLE()[idx].sg_font = font_name2handle(arg);
2336 // If it worked and it's the Normal group, use it as the
2337 // normal font. Same for the Menu group.
2338 if (HL_TABLE()[idx].sg_font != NOFONT)
2339 {
2340 if (do_normal)
2341 gui_init_font(arg, FALSE);
2342#ifndef FONTSET_ALWAYS
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002343# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002344 if (do_menu)
2345 {
2346 gui.menu_font = HL_TABLE()[idx].sg_font;
2347 gui_mch_new_menu_font();
2348 }
2349# endif
2350#endif
2351 }
2352 }
2353}
2354
2355#endif // FEAT_GUI
2356
2357#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
2358/*
2359 * Return the handle for a color name.
2360 * Returns INVALCOLOR when failed.
2361 */
2362 guicolor_T
2363color_name2handle(char_u *name)
2364{
2365 if (STRCMP(name, "NONE") == 0)
2366 return INVALCOLOR;
2367
2368 if (STRICMP(name, "fg") == 0 || STRICMP(name, "foreground") == 0)
2369 {
2370#if defined(FEAT_TERMGUICOLORS) && defined(FEAT_GUI)
2371 if (gui.in_use)
2372#endif
2373#ifdef FEAT_GUI
2374 return gui.norm_pixel;
2375#endif
2376#ifdef FEAT_TERMGUICOLORS
2377 if (cterm_normal_fg_gui_color != INVALCOLOR)
2378 return cterm_normal_fg_gui_color;
2379 // Guess that the foreground is black or white.
2380 return GUI_GET_COLOR((char_u *)(*p_bg == 'l' ? "black" : "white"));
2381#endif
2382 }
2383 if (STRICMP(name, "bg") == 0 || STRICMP(name, "background") == 0)
2384 {
2385#if defined(FEAT_TERMGUICOLORS) && defined(FEAT_GUI)
2386 if (gui.in_use)
2387#endif
2388#ifdef FEAT_GUI
2389 return gui.back_pixel;
2390#endif
2391#ifdef FEAT_TERMGUICOLORS
2392 if (cterm_normal_bg_gui_color != INVALCOLOR)
2393 return cterm_normal_bg_gui_color;
2394 // Guess that the background is white or black.
2395 return GUI_GET_COLOR((char_u *)(*p_bg == 'l' ? "white" : "black"));
2396#endif
2397 }
2398
2399 return GUI_GET_COLOR(name);
2400}
Drew Vogele30d1022021-10-24 20:35:07 +01002401
2402// On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
2403// values as used by the MS-Windows GDI api. It should be used only for
2404// MS-Windows GDI builds.
2405# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
2406# undef RGB
2407# endif
2408# ifndef RGB
kylo252ae6f1d82022-02-16 19:24:07 +00002409# define RGB(r, g, b) (((r)<<16) | ((g)<<8) | (b))
Drew Vogele30d1022021-10-24 20:35:07 +01002410# endif
2411
2412# ifdef VIMDLL
2413 static guicolor_T
2414gui_adjust_rgb(guicolor_T c)
2415{
2416 if (gui.in_use)
2417 return c;
2418 else
2419 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
2420}
2421# else
2422# define gui_adjust_rgb(c) (c)
2423# endif
2424
2425 static int
2426hex_digit(int c)
2427{
Keith Thompson184f71c2024-01-04 21:19:04 +01002428 if (SAFE_isdigit(c))
Drew Vogele30d1022021-10-24 20:35:07 +01002429 return c - '0';
2430 c = TOLOWER_ASC(c);
2431 if (c >= 'a' && c <= 'f')
2432 return c - 'a' + 10;
2433 return 0x1ffffff;
2434}
2435
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00002436 static guicolor_T
Drew Vogele30d1022021-10-24 20:35:07 +01002437decode_hex_color(char_u *hex)
2438{
2439 guicolor_T color;
2440
2441 if (hex[0] != '#' || STRLEN(hex) != 7)
2442 return INVALCOLOR;
2443
2444 // Name is in "#rrggbb" format
2445 color = RGB(((hex_digit(hex[1]) << 4) + hex_digit(hex[2])),
2446 ((hex_digit(hex[3]) << 4) + hex_digit(hex[4])),
2447 ((hex_digit(hex[5]) << 4) + hex_digit(hex[6])));
2448 if (color > 0xffffff)
2449 return INVALCOLOR;
2450 return gui_adjust_rgb(color);
2451}
2452
Bram Moolenaar2a521962021-10-25 10:30:14 +01002453#ifdef FEAT_EVAL
Drew Vogele30d1022021-10-24 20:35:07 +01002454// Returns the color currently mapped to the given name or INVALCOLOR if no
2455// such name exists in the color table. The convention is to use lowercase for
2456// all keys in the v:colornames dictionary. The value can be either a string in
2457// the form #rrggbb or a number, either of which is converted to a guicolor_T.
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00002458 static guicolor_T
Drew Vogele30d1022021-10-24 20:35:07 +01002459colorname2rgb(char_u *name)
2460{
2461 dict_T *colornames_table = get_vim_var_dict(VV_COLORNAMES);
2462 char_u *lc_name;
2463 dictitem_T *colentry;
2464 char_u *colstr;
2465 varnumber_T colnum;
2466
2467 lc_name = strlow_save(name);
2468 if (lc_name == NULL)
2469 return INVALCOLOR;
2470
2471 colentry = dict_find(colornames_table, lc_name, -1);
2472 vim_free(lc_name);
2473 if (colentry == NULL)
2474 return INVALCOLOR;
2475
2476 if (colentry->di_tv.v_type == VAR_STRING)
2477 {
2478 colstr = tv_get_string_strict(&colentry->di_tv);
2479 if ((STRLEN(colstr) == 7) && (*colstr == '#'))
2480 {
2481 return decode_hex_color(colstr);
2482 }
2483 else
2484 {
2485 semsg(_(e_bad_color_string_str), colstr);
2486 return INVALCOLOR;
2487 }
2488 }
2489
2490 if (colentry->di_tv.v_type == VAR_NUMBER)
2491 {
2492 colnum = tv_get_number(&colentry->di_tv);
2493 return (guicolor_T)colnum;
2494 }
2495
2496 return INVALCOLOR;
2497}
2498
Drew Vogele30d1022021-10-24 20:35:07 +01002499#endif
2500
2501 guicolor_T
2502gui_get_color_cmn(char_u *name)
2503{
Drew Vogele30d1022021-10-24 20:35:07 +01002504 guicolor_T color;
Drew Vogele30d1022021-10-24 20:35:07 +01002505 // Only non X11 colors (not present in rgb.txt) and colors in
John Marriott34f00dd2024-04-08 23:28:12 +02002506 // color_name_tab[], useful when $VIMRUNTIME is not found,.
2507 // must be sorted by the 'value' field because it is used by bsearch()!
2508 static keyvalue_T rgb_tab[] = {
2509 KEYVALUE_ENTRY(RGB(0x00, 0x00, 0x00), "black"),
2510 KEYVALUE_ENTRY(RGB(0x00, 0x00, 0xFF), "blue"),
2511 KEYVALUE_ENTRY(RGB(0xA5, 0x2A, 0x2A), "brown"),
2512 KEYVALUE_ENTRY(RGB(0x00, 0xFF, 0xFF), "cyan"),
2513 KEYVALUE_ENTRY(RGB(0x00, 0x00, 0x8B), "darkblue"),
2514 KEYVALUE_ENTRY(RGB(0x00, 0x8B, 0x8B), "darkcyan"),
2515 KEYVALUE_ENTRY(RGB(0xA9, 0xA9, 0xA9), "darkgray"),
2516 KEYVALUE_ENTRY(RGB(0x00, 0x64, 0x00), "darkgreen"),
2517 KEYVALUE_ENTRY(RGB(0xA9, 0xA9, 0xA9), "darkgrey"),
2518 KEYVALUE_ENTRY(RGB(0x8B, 0x00, 0x8B), "darkmagenta"),
2519 KEYVALUE_ENTRY(RGB(0x8B, 0x00, 0x00), "darkred"),
2520 KEYVALUE_ENTRY(RGB(0x8B, 0x8B, 0x00), "darkyellow"), // No X11
2521 KEYVALUE_ENTRY(RGB(0xBE, 0xBE, 0xBE), "gray"),
2522 KEYVALUE_ENTRY(RGB(0x00, 0xFF, 0x00), "green"),
2523 KEYVALUE_ENTRY(RGB(0xBE, 0xBE, 0xBE), "grey"),
2524 KEYVALUE_ENTRY(RGB(0x66, 0x66, 0x66), "grey40"),
2525 KEYVALUE_ENTRY(RGB(0x7F, 0x7F, 0x7F), "grey50"),
2526 KEYVALUE_ENTRY(RGB(0xE5, 0xE5, 0xE5), "grey90"),
2527 KEYVALUE_ENTRY(RGB(0xAD, 0xD8, 0xE6), "lightblue"),
2528 KEYVALUE_ENTRY(RGB(0xE0, 0xFF, 0xFF), "lightcyan"),
2529 KEYVALUE_ENTRY(RGB(0xD3, 0xD3, 0xD3), "lightgray"),
2530 KEYVALUE_ENTRY(RGB(0x90, 0xEE, 0x90), "lightgreen"),
2531 KEYVALUE_ENTRY(RGB(0xD3, 0xD3, 0xD3), "lightgrey"),
2532 KEYVALUE_ENTRY(RGB(0xFF, 0x8B, 0xFF), "lightmagenta"), // No XX
2533 KEYVALUE_ENTRY(RGB(0xFF, 0x8B, 0x8B), "lightred"), // No XX
2534 KEYVALUE_ENTRY(RGB(0xFF, 0xFF, 0xE0), "lightyellow"),
2535 KEYVALUE_ENTRY(RGB(0xFF, 0x00, 0xFF), "magenta"),
2536 KEYVALUE_ENTRY(RGB(0xFF, 0x00, 0x00), "red"),
2537 KEYVALUE_ENTRY(RGB(0x2E, 0x8B, 0x57), "seagreen"),
2538 KEYVALUE_ENTRY(RGB(0xFF, 0xFF, 0xFF), "white"),
2539 KEYVALUE_ENTRY(RGB(0xFF, 0xFF, 0x00), "yellow")
Drew Vogele30d1022021-10-24 20:35:07 +01002540 };
John Marriott34f00dd2024-04-08 23:28:12 +02002541 keyvalue_T target;
2542 keyvalue_T *entry;
Drew Vogele30d1022021-10-24 20:35:07 +01002543
2544 color = decode_hex_color(name);
2545 if (color != INVALCOLOR)
2546 return color;
2547
John Marriott34f00dd2024-04-08 23:28:12 +02002548 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01002549 target.value.string = name;
2550 target.value.length = 0; // not used, see cmp_keyvalue_value_i()
2551 entry = (keyvalue_T *)bsearch(&target, &rgb_tab, ARRAY_LENGTH(rgb_tab),
2552 sizeof(rgb_tab[0]), cmp_keyvalue_value_i);
John Marriott34f00dd2024-04-08 23:28:12 +02002553 if (entry != NULL)
John Marriott34f00dd2024-04-08 23:28:12 +02002554 return gui_adjust_rgb((guicolor_T)entry->key);
Drew Vogele30d1022021-10-24 20:35:07 +01002555
2556#if defined(FEAT_EVAL)
2557 /*
2558 * Not a traditional color. Load additional color aliases and then consult the alias table.
2559 */
2560
2561 color = colorname2rgb(name);
2562 if (color == INVALCOLOR)
2563 {
2564 load_default_colors_lists();
2565 color = colorname2rgb(name);
2566 }
2567
2568 return color;
2569#else
2570 return INVALCOLOR;
2571#endif
2572}
2573
2574 guicolor_T
2575gui_get_rgb_color_cmn(int r, int g, int b)
2576{
2577 guicolor_T color = RGB(r, g, b);
2578
2579 if (color > 0xffffff)
2580 return INVALCOLOR;
2581 return gui_adjust_rgb(color);
2582}
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002583#endif
2584
2585/*
2586 * Table with the specifications for an attribute number.
2587 * Note that this table is used by ALL buffers. This is required because the
2588 * GUI can redraw at any time for any buffer.
2589 */
2590static garray_T term_attr_table = {0, 0, 0, 0, NULL};
2591
2592#define TERM_ATTR_ENTRY(idx) ((attrentry_T *)term_attr_table.ga_data)[idx]
2593
2594static garray_T cterm_attr_table = {0, 0, 0, 0, NULL};
2595
2596#define CTERM_ATTR_ENTRY(idx) ((attrentry_T *)cterm_attr_table.ga_data)[idx]
2597
2598#ifdef FEAT_GUI
2599static garray_T gui_attr_table = {0, 0, 0, 0, NULL};
2600
2601#define GUI_ATTR_ENTRY(idx) ((attrentry_T *)gui_attr_table.ga_data)[idx]
2602#endif
2603
2604/*
2605 * Return the attr number for a set of colors and font.
2606 * Add a new entry to the term_attr_table, cterm_attr_table or gui_attr_table
2607 * if the combination is new.
2608 * Return 0 for error (no more room).
2609 */
2610 static int
2611get_attr_entry(garray_T *table, attrentry_T *aep)
2612{
2613 int i;
2614 attrentry_T *taep;
2615 static int recursive = FALSE;
2616
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01002617 // Init the table, in case it wasn't done yet.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002618 table->ga_itemsize = sizeof(attrentry_T);
2619 table->ga_growsize = 7;
2620
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01002621 // Try to find an entry with the same specifications.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002622 for (i = 0; i < table->ga_len; ++i)
2623 {
2624 taep = &(((attrentry_T *)table->ga_data)[i]);
2625 if ( aep->ae_attr == taep->ae_attr
2626 && (
2627#ifdef FEAT_GUI
2628 (table == &gui_attr_table
2629 && (aep->ae_u.gui.fg_color == taep->ae_u.gui.fg_color
2630 && aep->ae_u.gui.bg_color
2631 == taep->ae_u.gui.bg_color
2632 && aep->ae_u.gui.sp_color
2633 == taep->ae_u.gui.sp_color
2634 && aep->ae_u.gui.font == taep->ae_u.gui.font
2635# ifdef FEAT_XFONTSET
2636 && aep->ae_u.gui.fontset == taep->ae_u.gui.fontset
2637# endif
2638 ))
2639 ||
2640#endif
2641 (table == &term_attr_table
2642 && (aep->ae_u.term.start == NULL)
2643 == (taep->ae_u.term.start == NULL)
2644 && (aep->ae_u.term.start == NULL
2645 || STRCMP(aep->ae_u.term.start,
2646 taep->ae_u.term.start) == 0)
2647 && (aep->ae_u.term.stop == NULL)
2648 == (taep->ae_u.term.stop == NULL)
2649 && (aep->ae_u.term.stop == NULL
2650 || STRCMP(aep->ae_u.term.stop,
2651 taep->ae_u.term.stop) == 0))
2652 || (table == &cterm_attr_table
2653 && aep->ae_u.cterm.fg_color
2654 == taep->ae_u.cterm.fg_color
2655 && aep->ae_u.cterm.bg_color
2656 == taep->ae_u.cterm.bg_color
Bram Moolenaare023e882020-05-31 16:42:30 +02002657 && aep->ae_u.cterm.ul_color
2658 == taep->ae_u.cterm.ul_color
PMuncha606f3a2023-11-15 15:35:49 +01002659 && aep->ae_u.cterm.font
2660 == taep->ae_u.cterm.font
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002661#ifdef FEAT_TERMGUICOLORS
2662 && aep->ae_u.cterm.fg_rgb
2663 == taep->ae_u.cterm.fg_rgb
2664 && aep->ae_u.cterm.bg_rgb
2665 == taep->ae_u.cterm.bg_rgb
Bram Moolenaare023e882020-05-31 16:42:30 +02002666 && aep->ae_u.cterm.ul_rgb
2667 == taep->ae_u.cterm.ul_rgb
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002668#endif
2669 )))
2670
2671 return i + ATTR_OFF;
2672 }
2673
2674 if (table->ga_len + ATTR_OFF > MAX_TYPENR)
2675 {
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01002676 // Running out of attribute entries! remove all attributes, and
2677 // compute new ones for all groups.
2678 // When called recursively, we are really out of numbers.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002679 if (recursive)
2680 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002681 emsg(_(e_too_many_different_highlighting_attributes_in_use));
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002682 return 0;
2683 }
2684 recursive = TRUE;
2685
2686 clear_hl_tables();
2687
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002688 set_must_redraw(UPD_CLEAR);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002689
2690 for (i = 0; i < highlight_ga.ga_len; ++i)
2691 set_hl_attr(i);
2692
2693 recursive = FALSE;
2694 }
2695
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01002696 // This is a new combination of colors and font, add an entry.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002697 if (ga_grow(table, 1) == FAIL)
2698 return 0;
2699
2700 taep = &(((attrentry_T *)table->ga_data)[table->ga_len]);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002701 CLEAR_POINTER(taep);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002702 taep->ae_attr = aep->ae_attr;
2703#ifdef FEAT_GUI
2704 if (table == &gui_attr_table)
2705 {
2706 taep->ae_u.gui.fg_color = aep->ae_u.gui.fg_color;
2707 taep->ae_u.gui.bg_color = aep->ae_u.gui.bg_color;
2708 taep->ae_u.gui.sp_color = aep->ae_u.gui.sp_color;
2709 taep->ae_u.gui.font = aep->ae_u.gui.font;
2710# ifdef FEAT_XFONTSET
2711 taep->ae_u.gui.fontset = aep->ae_u.gui.fontset;
2712# endif
2713 }
2714#endif
2715 if (table == &term_attr_table)
2716 {
2717 if (aep->ae_u.term.start == NULL)
2718 taep->ae_u.term.start = NULL;
2719 else
2720 taep->ae_u.term.start = vim_strsave(aep->ae_u.term.start);
2721 if (aep->ae_u.term.stop == NULL)
2722 taep->ae_u.term.stop = NULL;
2723 else
2724 taep->ae_u.term.stop = vim_strsave(aep->ae_u.term.stop);
2725 }
2726 else if (table == &cterm_attr_table)
2727 {
2728 taep->ae_u.cterm.fg_color = aep->ae_u.cterm.fg_color;
2729 taep->ae_u.cterm.bg_color = aep->ae_u.cterm.bg_color;
Bram Moolenaare023e882020-05-31 16:42:30 +02002730 taep->ae_u.cterm.ul_color = aep->ae_u.cterm.ul_color;
PMuncha606f3a2023-11-15 15:35:49 +01002731 taep->ae_u.cterm.font = aep->ae_u.cterm.font;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002732#ifdef FEAT_TERMGUICOLORS
2733 taep->ae_u.cterm.fg_rgb = aep->ae_u.cterm.fg_rgb;
2734 taep->ae_u.cterm.bg_rgb = aep->ae_u.cterm.bg_rgb;
Bram Moolenaare023e882020-05-31 16:42:30 +02002735 taep->ae_u.cterm.ul_rgb = aep->ae_u.cterm.ul_rgb;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002736#endif
2737 }
2738 ++table->ga_len;
2739 return (table->ga_len - 1 + ATTR_OFF);
2740}
2741
2742#if defined(FEAT_TERMINAL) || defined(PROTO)
2743/*
2744 * Get an attribute index for a cterm entry.
2745 * Uses an existing entry when possible or adds one when needed.
2746 */
2747 int
2748get_cterm_attr_idx(int attr, int fg, int bg)
2749{
2750 attrentry_T at_en;
2751
Bram Moolenaara80faa82020-04-12 19:37:17 +02002752 CLEAR_FIELD(at_en);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002753#ifdef FEAT_TERMGUICOLORS
2754 at_en.ae_u.cterm.fg_rgb = INVALCOLOR;
2755 at_en.ae_u.cterm.bg_rgb = INVALCOLOR;
Bram Moolenaar1e5f8f62020-06-02 23:18:24 +02002756 at_en.ae_u.cterm.ul_rgb = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002757#endif
2758 at_en.ae_attr = attr;
2759 at_en.ae_u.cterm.fg_color = fg;
2760 at_en.ae_u.cterm.bg_color = bg;
Bram Moolenaarcc836552020-06-03 10:04:49 +02002761 at_en.ae_u.cterm.ul_color = 0;
PMuncha606f3a2023-11-15 15:35:49 +01002762 at_en.ae_u.cterm.font = 0;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002763 return get_attr_entry(&cterm_attr_table, &at_en);
2764}
2765#endif
2766
2767#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMGUICOLORS)) || defined(PROTO)
2768/*
2769 * Get an attribute index for a 'termguicolors' entry.
2770 * Uses an existing entry when possible or adds one when needed.
2771 */
2772 int
2773get_tgc_attr_idx(int attr, guicolor_T fg, guicolor_T bg)
2774{
2775 attrentry_T at_en;
2776
Bram Moolenaara80faa82020-04-12 19:37:17 +02002777 CLEAR_FIELD(at_en);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002778 at_en.ae_attr = attr;
2779 if (fg == INVALCOLOR && bg == INVALCOLOR)
2780 {
2781 // If both GUI colors are not set fall back to the cterm colors. Helps
2782 // if the GUI only has an attribute, such as undercurl.
2783 at_en.ae_u.cterm.fg_rgb = CTERMCOLOR;
2784 at_en.ae_u.cterm.bg_rgb = CTERMCOLOR;
2785 }
2786 else
2787 {
2788 at_en.ae_u.cterm.fg_rgb = fg;
2789 at_en.ae_u.cterm.bg_rgb = bg;
2790 }
Bram Moolenaar1e5f8f62020-06-02 23:18:24 +02002791 at_en.ae_u.cterm.ul_rgb = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002792 return get_attr_entry(&cterm_attr_table, &at_en);
2793}
2794#endif
2795
2796#if (defined(FEAT_TERMINAL) && defined(FEAT_GUI)) || defined(PROTO)
2797/*
2798 * Get an attribute index for a cterm entry.
2799 * Uses an existing entry when possible or adds one when needed.
2800 */
2801 int
2802get_gui_attr_idx(int attr, guicolor_T fg, guicolor_T bg)
2803{
2804 attrentry_T at_en;
2805
Bram Moolenaara80faa82020-04-12 19:37:17 +02002806 CLEAR_FIELD(at_en);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002807 at_en.ae_attr = attr;
2808 at_en.ae_u.gui.fg_color = fg;
2809 at_en.ae_u.gui.bg_color = bg;
2810 return get_attr_entry(&gui_attr_table, &at_en);
2811}
2812#endif
2813
2814/*
2815 * Clear all highlight tables.
2816 */
2817 void
2818clear_hl_tables(void)
2819{
2820 int i;
2821 attrentry_T *taep;
2822
2823#ifdef FEAT_GUI
2824 ga_clear(&gui_attr_table);
2825#endif
2826 for (i = 0; i < term_attr_table.ga_len; ++i)
2827 {
2828 taep = &(((attrentry_T *)term_attr_table.ga_data)[i]);
2829 vim_free(taep->ae_u.term.start);
2830 vim_free(taep->ae_u.term.stop);
2831 }
2832 ga_clear(&term_attr_table);
2833 ga_clear(&cterm_attr_table);
2834}
2835
2836/*
2837 * Combine special attributes (e.g., for spelling) with other attributes
2838 * (e.g., for syntax highlighting).
2839 * "prim_attr" overrules "char_attr".
2840 * This creates a new group when required.
2841 * Since we expect there to be few spelling mistakes we don't cache the
2842 * result.
2843 * Return the resulting attributes.
2844 */
2845 int
2846hl_combine_attr(int char_attr, int prim_attr)
2847{
2848 attrentry_T *char_aep = NULL;
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002849 attrentry_T *prim_aep;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002850 attrentry_T new_en;
2851
2852 if (char_attr == 0)
2853 return prim_attr;
2854 if (char_attr <= HL_ALL && prim_attr <= HL_ALL)
2855 return ATTR_COMBINE(char_attr, prim_attr);
2856#ifdef FEAT_GUI
2857 if (gui.in_use)
2858 {
2859 if (char_attr > HL_ALL)
2860 char_aep = syn_gui_attr2entry(char_attr);
2861 if (char_aep != NULL)
2862 new_en = *char_aep;
2863 else
2864 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002865 CLEAR_FIELD(new_en);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002866 new_en.ae_u.gui.fg_color = INVALCOLOR;
2867 new_en.ae_u.gui.bg_color = INVALCOLOR;
2868 new_en.ae_u.gui.sp_color = INVALCOLOR;
2869 if (char_attr <= HL_ALL)
2870 new_en.ae_attr = char_attr;
2871 }
2872
2873 if (prim_attr <= HL_ALL)
2874 new_en.ae_attr = ATTR_COMBINE(new_en.ae_attr, prim_attr);
2875 else
2876 {
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002877 prim_aep = syn_gui_attr2entry(prim_attr);
2878 if (prim_aep != NULL)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002879 {
2880 new_en.ae_attr = ATTR_COMBINE(new_en.ae_attr,
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002881 prim_aep->ae_attr);
2882 if (prim_aep->ae_u.gui.fg_color != INVALCOLOR)
2883 new_en.ae_u.gui.fg_color = prim_aep->ae_u.gui.fg_color;
2884 if (prim_aep->ae_u.gui.bg_color != INVALCOLOR)
2885 new_en.ae_u.gui.bg_color = prim_aep->ae_u.gui.bg_color;
2886 if (prim_aep->ae_u.gui.sp_color != INVALCOLOR)
2887 new_en.ae_u.gui.sp_color = prim_aep->ae_u.gui.sp_color;
2888 if (prim_aep->ae_u.gui.font != NOFONT)
2889 new_en.ae_u.gui.font = prim_aep->ae_u.gui.font;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002890# ifdef FEAT_XFONTSET
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002891 if (prim_aep->ae_u.gui.fontset != NOFONTSET)
2892 new_en.ae_u.gui.fontset = prim_aep->ae_u.gui.fontset;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002893# endif
2894 }
2895 }
2896 return get_attr_entry(&gui_attr_table, &new_en);
2897 }
2898#endif
2899
2900 if (IS_CTERM)
2901 {
2902 if (char_attr > HL_ALL)
2903 char_aep = syn_cterm_attr2entry(char_attr);
2904 if (char_aep != NULL)
2905 new_en = *char_aep;
2906 else
2907 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002908 CLEAR_FIELD(new_en);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002909#ifdef FEAT_TERMGUICOLORS
2910 new_en.ae_u.cterm.bg_rgb = INVALCOLOR;
2911 new_en.ae_u.cterm.fg_rgb = INVALCOLOR;
Bram Moolenaare023e882020-05-31 16:42:30 +02002912 new_en.ae_u.cterm.ul_rgb = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002913#endif
2914 if (char_attr <= HL_ALL)
2915 new_en.ae_attr = char_attr;
2916 }
2917
2918 if (prim_attr <= HL_ALL)
2919 new_en.ae_attr = ATTR_COMBINE(new_en.ae_attr, prim_attr);
2920 else
2921 {
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002922 prim_aep = syn_cterm_attr2entry(prim_attr);
2923 if (prim_aep != NULL)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002924 {
2925 new_en.ae_attr = ATTR_COMBINE(new_en.ae_attr,
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002926 prim_aep->ae_attr);
2927 if (prim_aep->ae_u.cterm.fg_color > 0)
2928 new_en.ae_u.cterm.fg_color = prim_aep->ae_u.cterm.fg_color;
2929 if (prim_aep->ae_u.cterm.bg_color > 0)
2930 new_en.ae_u.cterm.bg_color = prim_aep->ae_u.cterm.bg_color;
2931 if (prim_aep->ae_u.cterm.ul_color > 0)
2932 new_en.ae_u.cterm.ul_color = prim_aep->ae_u.cterm.ul_color;
PMuncha606f3a2023-11-15 15:35:49 +01002933 if (prim_aep->ae_u.cterm.font > 0)
2934 new_en.ae_u.cterm.font = prim_aep->ae_u.cterm.font;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002935#ifdef FEAT_TERMGUICOLORS
2936 // If both fg and bg are not set fall back to cterm colors.
2937 // Helps for SpellBad which uses undercurl in the GUI.
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002938 if (COLOR_INVALID(prim_aep->ae_u.cterm.fg_rgb)
2939 && COLOR_INVALID(prim_aep->ae_u.cterm.bg_rgb))
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002940 {
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002941 if (prim_aep->ae_u.cterm.fg_color > 0)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002942 new_en.ae_u.cterm.fg_rgb = CTERMCOLOR;
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002943 if (prim_aep->ae_u.cterm.bg_color > 0)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002944 new_en.ae_u.cterm.bg_rgb = CTERMCOLOR;
2945 }
2946 else
2947 {
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002948 if (prim_aep->ae_u.cterm.fg_rgb != INVALCOLOR)
2949 new_en.ae_u.cterm.fg_rgb = prim_aep->ae_u.cterm.fg_rgb;
2950 if (prim_aep->ae_u.cterm.bg_rgb != INVALCOLOR)
2951 new_en.ae_u.cterm.bg_rgb = prim_aep->ae_u.cterm.bg_rgb;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002952 }
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002953 if (prim_aep->ae_u.cterm.ul_rgb != INVALCOLOR)
2954 new_en.ae_u.cterm.ul_rgb = prim_aep->ae_u.cterm.ul_rgb;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002955#endif
2956 }
2957 }
2958 return get_attr_entry(&cterm_attr_table, &new_en);
2959 }
2960
2961 if (char_attr > HL_ALL)
2962 char_aep = syn_term_attr2entry(char_attr);
2963 if (char_aep != NULL)
2964 new_en = *char_aep;
2965 else
2966 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002967 CLEAR_FIELD(new_en);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002968 if (char_attr <= HL_ALL)
2969 new_en.ae_attr = char_attr;
2970 }
2971
2972 if (prim_attr <= HL_ALL)
2973 new_en.ae_attr = ATTR_COMBINE(new_en.ae_attr, prim_attr);
2974 else
2975 {
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002976 prim_aep = syn_term_attr2entry(prim_attr);
2977 if (prim_aep != NULL)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002978 {
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002979 new_en.ae_attr = ATTR_COMBINE(new_en.ae_attr, prim_aep->ae_attr);
2980 if (prim_aep->ae_u.term.start != NULL)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002981 {
Bram Moolenaarc9b65702022-08-13 21:37:29 +01002982 new_en.ae_u.term.start = prim_aep->ae_u.term.start;
2983 new_en.ae_u.term.stop = prim_aep->ae_u.term.stop;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02002984 }
2985 }
2986 }
2987 return get_attr_entry(&term_attr_table, &new_en);
2988}
2989
2990#ifdef FEAT_GUI
2991 attrentry_T *
2992syn_gui_attr2entry(int attr)
2993{
2994 attr -= ATTR_OFF;
2995 if (attr >= gui_attr_table.ga_len) // did ":syntax clear"
2996 return NULL;
2997 return &(GUI_ATTR_ENTRY(attr));
2998}
2999#endif
3000
3001/*
3002 * Get the highlight attributes (HL_BOLD etc.) from an attribute nr.
3003 * Only to be used when "attr" > HL_ALL.
3004 */
3005 int
3006syn_attr2attr(int attr)
3007{
3008 attrentry_T *aep;
3009
3010#ifdef FEAT_GUI
3011 if (gui.in_use)
3012 aep = syn_gui_attr2entry(attr);
3013 else
3014#endif
3015 if (IS_CTERM)
3016 aep = syn_cterm_attr2entry(attr);
3017 else
3018 aep = syn_term_attr2entry(attr);
3019
3020 if (aep == NULL) // highlighting not set
3021 return 0;
3022 return aep->ae_attr;
3023}
3024
3025
3026 attrentry_T *
3027syn_term_attr2entry(int attr)
3028{
3029 attr -= ATTR_OFF;
3030 if (attr >= term_attr_table.ga_len) // did ":syntax clear"
3031 return NULL;
3032 return &(TERM_ATTR_ENTRY(attr));
3033}
3034
3035 attrentry_T *
3036syn_cterm_attr2entry(int attr)
3037{
3038 attr -= ATTR_OFF;
3039 if (attr >= cterm_attr_table.ga_len) // did ":syntax clear"
3040 return NULL;
3041 return &(CTERM_ATTR_ENTRY(attr));
3042}
3043
3044#define LIST_ATTR 1
3045#define LIST_STRING 2
3046#define LIST_INT 3
3047
3048 static void
3049highlight_list_one(int id)
3050{
3051 hl_group_T *sgp;
3052 int didh = FALSE;
3053
3054 sgp = &HL_TABLE()[id - 1]; // index is ID minus one
3055
3056 if (message_filtered(sgp->sg_name))
3057 return;
3058
3059 didh = highlight_list_arg(id, didh, LIST_ATTR,
3060 sgp->sg_term, NULL, "term");
3061 didh = highlight_list_arg(id, didh, LIST_STRING,
3062 0, sgp->sg_start, "start");
3063 didh = highlight_list_arg(id, didh, LIST_STRING,
3064 0, sgp->sg_stop, "stop");
3065
3066 didh = highlight_list_arg(id, didh, LIST_ATTR,
3067 sgp->sg_cterm, NULL, "cterm");
3068 didh = highlight_list_arg(id, didh, LIST_INT,
3069 sgp->sg_cterm_fg, NULL, "ctermfg");
3070 didh = highlight_list_arg(id, didh, LIST_INT,
3071 sgp->sg_cterm_bg, NULL, "ctermbg");
Bram Moolenaare023e882020-05-31 16:42:30 +02003072 didh = highlight_list_arg(id, didh, LIST_INT,
3073 sgp->sg_cterm_ul, NULL, "ctermul");
PMuncha606f3a2023-11-15 15:35:49 +01003074 didh = highlight_list_arg(id, didh, LIST_INT,
3075 sgp->sg_cterm_font, NULL, "ctermfont");
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003076
3077#if defined(FEAT_GUI) || defined(FEAT_EVAL)
3078 didh = highlight_list_arg(id, didh, LIST_ATTR,
3079 sgp->sg_gui, NULL, "gui");
3080 didh = highlight_list_arg(id, didh, LIST_STRING,
3081 0, sgp->sg_gui_fg_name, "guifg");
3082 didh = highlight_list_arg(id, didh, LIST_STRING,
3083 0, sgp->sg_gui_bg_name, "guibg");
3084 didh = highlight_list_arg(id, didh, LIST_STRING,
3085 0, sgp->sg_gui_sp_name, "guisp");
3086#endif
3087#ifdef FEAT_GUI
3088 didh = highlight_list_arg(id, didh, LIST_STRING,
3089 0, sgp->sg_font_name, "font");
3090#endif
3091
3092 if (sgp->sg_link && !got_int)
3093 {
3094 (void)syn_list_header(didh, 9999, id);
3095 didh = TRUE;
3096 msg_puts_attr("links to", HL_ATTR(HLF_D));
3097 msg_putchar(' ');
3098 msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
3099 }
3100
3101 if (!didh)
3102 highlight_list_arg(id, didh, LIST_STRING, 0, (char_u *)"cleared", "");
3103#ifdef FEAT_EVAL
3104 if (p_verbose > 0)
3105 last_set_msg(sgp->sg_script_ctx);
3106#endif
3107}
3108
3109 static int
3110highlight_list_arg(
3111 int id,
3112 int didh,
3113 int type,
3114 int iarg,
3115 char_u *sarg,
3116 char *name)
3117{
Bram Moolenaar84f54632022-06-29 18:39:11 +01003118 char_u buf[MAX_ATTR_LEN];
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003119 char_u *ts;
3120 int i;
3121
3122 if (got_int)
3123 return FALSE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003124
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003125 if (type == LIST_STRING ? (sarg == NULL) : (iarg == 0))
3126 return didh;
3127
3128 ts = buf;
3129 if (type == LIST_INT)
3130 sprintf((char *)buf, "%d", iarg - 1);
3131 else if (type == LIST_STRING)
3132 ts = sarg;
3133 else // type == LIST_ATTR
3134 {
John Marriott34f00dd2024-04-08 23:28:12 +02003135 size_t buflen;
3136
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003137 buf[0] = NUL;
John Marriott34f00dd2024-04-08 23:28:12 +02003138 buflen = 0;
3139 for (i = 0; i < (int)ARRAY_LENGTH(highlight_index_tab); ++i)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003140 {
John Marriott34f00dd2024-04-08 23:28:12 +02003141 if (iarg & highlight_index_tab[i]->key)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003142 {
John Marriott34f00dd2024-04-08 23:28:12 +02003143 if (buflen > 0)
3144 {
3145 STRCPY(buf + buflen, (char_u *)",");
3146 ++buflen;
3147 }
John Marriott8d4477e2024-11-02 15:59:01 +01003148 STRCPY(buf + buflen, highlight_index_tab[i]->value.string);
3149 buflen += highlight_index_tab[i]->value.length;
John Marriott34f00dd2024-04-08 23:28:12 +02003150 iarg &= ~highlight_index_tab[i]->key; // don't want "inverse"/"reverse"
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003151 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003152 }
3153 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003154
3155 (void)syn_list_header(didh,
3156 (int)(vim_strsize(ts) + STRLEN(name) + 1), id);
3157 didh = TRUE;
3158 if (!got_int)
3159 {
3160 if (*name != NUL)
3161 {
3162 msg_puts_attr(name, HL_ATTR(HLF_D));
3163 msg_puts_attr("=", HL_ATTR(HLF_D));
3164 }
3165 msg_outtrans(ts);
3166 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003167 return didh;
3168}
3169
3170#if (((defined(FEAT_EVAL) || defined(FEAT_PRINTER))) && defined(FEAT_SYN_HL)) || defined(PROTO)
3171/*
3172 * Return "1" if highlight group "id" has attribute "flag".
3173 * Return NULL otherwise.
3174 */
3175 char_u *
3176highlight_has_attr(
3177 int id,
3178 int flag,
3179 int modec) // 'g' for GUI, 'c' for cterm, 't' for term
3180{
3181 int attr;
3182
3183 if (id <= 0 || id > highlight_ga.ga_len)
3184 return NULL;
3185
3186#if defined(FEAT_GUI) || defined(FEAT_EVAL)
3187 if (modec == 'g')
3188 attr = HL_TABLE()[id - 1].sg_gui;
3189 else
3190#endif
Yegappan Lakshmanand43d8e22021-10-19 13:44:52 +01003191 {
3192 if (modec == 'c')
3193 attr = HL_TABLE()[id - 1].sg_cterm;
3194 else
3195 attr = HL_TABLE()[id - 1].sg_term;
3196 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003197
3198 if (attr & flag)
3199 return (char_u *)"1";
3200 return NULL;
3201}
3202#endif
3203
3204#if (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) || defined(PROTO)
3205/*
3206 * Return color name of highlight group "id".
3207 */
3208 char_u *
3209highlight_color(
3210 int id,
Bram Moolenaar391c3622020-09-29 20:59:17 +02003211 char_u *what, // "font", "fg", "bg", "sp", "ul", "fg#", "bg#" or "sp#"
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003212 int modec) // 'g' for GUI, 'c' for cterm, 't' for term
3213{
3214 static char_u name[20];
3215 int n;
3216 int fg = FALSE;
3217 int sp = FALSE;
Bram Moolenaar391c3622020-09-29 20:59:17 +02003218 int ul = FALSE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003219 int font = FALSE;
3220
3221 if (id <= 0 || id > highlight_ga.ga_len)
3222 return NULL;
3223
3224 if (TOLOWER_ASC(what[0]) == 'f' && TOLOWER_ASC(what[1]) == 'g')
3225 fg = TRUE;
3226 else if (TOLOWER_ASC(what[0]) == 'f' && TOLOWER_ASC(what[1]) == 'o'
3227 && TOLOWER_ASC(what[2]) == 'n' && TOLOWER_ASC(what[3]) == 't')
3228 font = TRUE;
3229 else if (TOLOWER_ASC(what[0]) == 's' && TOLOWER_ASC(what[1]) == 'p')
3230 sp = TRUE;
Bram Moolenaar391c3622020-09-29 20:59:17 +02003231 else if (TOLOWER_ASC(what[0]) == 'u' && TOLOWER_ASC(what[1]) == 'l')
3232 ul = TRUE;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003233 else if (!(TOLOWER_ASC(what[0]) == 'b' && TOLOWER_ASC(what[1]) == 'g'))
3234 return NULL;
3235 if (modec == 'g')
3236 {
3237# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3238# ifdef FEAT_GUI
3239 // return font name
3240 if (font)
3241 return HL_TABLE()[id - 1].sg_font_name;
3242# endif
3243
3244 // return #RRGGBB form (only possible when GUI is running)
3245 if ((USE_24BIT) && what[2] == '#')
3246 {
3247 guicolor_T color;
3248 long_u rgb;
3249 static char_u buf[10];
3250
3251 if (fg)
3252 color = HL_TABLE()[id - 1].sg_gui_fg;
3253 else if (sp)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003254 color = HL_TABLE()[id - 1].sg_gui_sp;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003255 else
3256 color = HL_TABLE()[id - 1].sg_gui_bg;
3257 if (color == INVALCOLOR)
3258 return NULL;
3259 rgb = (long_u)GUI_MCH_GET_RGB(color);
3260 sprintf((char *)buf, "#%02x%02x%02x",
3261 (unsigned)(rgb >> 16),
3262 (unsigned)(rgb >> 8) & 255,
3263 (unsigned)rgb & 255);
3264 return buf;
3265 }
3266# endif
3267 if (fg)
3268 return (HL_TABLE()[id - 1].sg_gui_fg_name);
3269 if (sp)
3270 return (HL_TABLE()[id - 1].sg_gui_sp_name);
3271 return (HL_TABLE()[id - 1].sg_gui_bg_name);
3272 }
PMuncha606f3a2023-11-15 15:35:49 +01003273 if (sp)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003274 return NULL;
3275 if (modec == 'c')
3276 {
3277 if (fg)
3278 n = HL_TABLE()[id - 1].sg_cterm_fg - 1;
Bram Moolenaar391c3622020-09-29 20:59:17 +02003279 else if (ul)
3280 n = HL_TABLE()[id - 1].sg_cterm_ul - 1;
PMuncha606f3a2023-11-15 15:35:49 +01003281 else if (font)
3282 n = HL_TABLE()[id - 1].sg_cterm_font - 1;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003283 else
3284 n = HL_TABLE()[id - 1].sg_cterm_bg - 1;
3285 if (n < 0)
3286 return NULL;
3287 sprintf((char *)name, "%d", n);
3288 return name;
3289 }
3290 // term doesn't have color
3291 return NULL;
3292}
3293#endif
3294
3295#if (defined(FEAT_SYN_HL) \
3296 && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)) \
3297 && defined(FEAT_PRINTER)) || defined(PROTO)
3298/*
3299 * Return color name of highlight group "id" as RGB value.
3300 */
3301 long_u
3302highlight_gui_color_rgb(
3303 int id,
3304 int fg) // TRUE = fg, FALSE = bg
3305{
3306 guicolor_T color;
3307
3308 if (id <= 0 || id > highlight_ga.ga_len)
3309 return 0L;
3310
3311 if (fg)
3312 color = HL_TABLE()[id - 1].sg_gui_fg;
3313 else
3314 color = HL_TABLE()[id - 1].sg_gui_bg;
3315
3316 if (color == INVALCOLOR)
3317 return 0L;
3318
3319 return GUI_MCH_GET_RGB(color);
3320}
3321#endif
3322
3323/*
3324 * Output the syntax list header.
3325 * Return TRUE when started a new line.
3326 */
3327 int
3328syn_list_header(
3329 int did_header, // did header already
3330 int outlen, // length of string that comes
3331 int id) // highlight group id
3332{
3333 int endcol = 19;
3334 int newline = TRUE;
3335 int name_col = 0;
3336
3337 if (!did_header)
3338 {
3339 msg_putchar('\n');
3340 if (got_int)
3341 return TRUE;
3342 msg_outtrans(HL_TABLE()[id - 1].sg_name);
3343 name_col = msg_col;
3344 endcol = 15;
3345 }
3346 else if (msg_col + outlen + 1 >= Columns)
3347 {
3348 msg_putchar('\n');
3349 if (got_int)
3350 return TRUE;
3351 }
3352 else
3353 {
3354 if (msg_col >= endcol) // wrap around is like starting a new line
3355 newline = FALSE;
3356 }
3357
3358 if (msg_col >= endcol) // output at least one space
3359 endcol = msg_col + 1;
Christian Brabandt220474d2024-07-20 13:26:44 +02003360 if (Columns <= (long)endcol) // avoid hang for tiny window
3361 endcol = (int)(Columns - 1);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003362
3363 msg_advance(endcol);
3364
3365 // Show "xxx" with the attributes.
3366 if (!did_header)
3367 {
3368 if (endcol == Columns - 1 && endcol <= name_col)
3369 msg_putchar(' ');
3370 msg_puts_attr("xxx", syn_id2attr(id));
3371 msg_putchar(' ');
3372 }
3373
3374 return newline;
3375}
3376
3377/*
3378 * Set the attribute numbers for a highlight group.
3379 * Called after one of the attributes has changed.
3380 */
3381 static void
3382set_hl_attr(
3383 int idx) // index in array
3384{
3385 attrentry_T at_en;
3386 hl_group_T *sgp = HL_TABLE() + idx;
3387
3388 // The "Normal" group doesn't need an attribute number
3389 if (sgp->sg_name_u != NULL && STRCMP(sgp->sg_name_u, "NORMAL") == 0)
3390 return;
3391
3392#ifdef FEAT_GUI
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003393 // For the GUI mode: If there are other than "normal" highlighting
3394 // attributes, need to allocate an attr number.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003395 if (sgp->sg_gui_fg == INVALCOLOR
3396 && sgp->sg_gui_bg == INVALCOLOR
3397 && sgp->sg_gui_sp == INVALCOLOR
3398 && sgp->sg_font == NOFONT
3399# ifdef FEAT_XFONTSET
3400 && sgp->sg_fontset == NOFONTSET
3401# endif
3402 )
3403 {
3404 sgp->sg_gui_attr = sgp->sg_gui;
3405 }
3406 else
3407 {
3408 at_en.ae_attr = sgp->sg_gui;
3409 at_en.ae_u.gui.fg_color = sgp->sg_gui_fg;
3410 at_en.ae_u.gui.bg_color = sgp->sg_gui_bg;
3411 at_en.ae_u.gui.sp_color = sgp->sg_gui_sp;
3412 at_en.ae_u.gui.font = sgp->sg_font;
3413# ifdef FEAT_XFONTSET
3414 at_en.ae_u.gui.fontset = sgp->sg_fontset;
3415# endif
3416 sgp->sg_gui_attr = get_attr_entry(&gui_attr_table, &at_en);
3417 }
3418#endif
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003419 // For the term mode: If there are other than "normal" highlighting
3420 // attributes, need to allocate an attr number.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003421 if (sgp->sg_start == NULL && sgp->sg_stop == NULL)
3422 sgp->sg_term_attr = sgp->sg_term;
3423 else
3424 {
3425 at_en.ae_attr = sgp->sg_term;
3426 at_en.ae_u.term.start = sgp->sg_start;
3427 at_en.ae_u.term.stop = sgp->sg_stop;
3428 sgp->sg_term_attr = get_attr_entry(&term_attr_table, &at_en);
3429 }
3430
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003431 // For the color term mode: If there are other than "normal"
3432 // highlighting attributes, need to allocate an attr number.
PMuncha606f3a2023-11-15 15:35:49 +01003433 if (sgp->sg_cterm_fg == 0 && sgp->sg_cterm_bg == 0 &&
3434 sgp->sg_cterm_ul == 0 && sgp->sg_cterm_font == 0
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003435# ifdef FEAT_TERMGUICOLORS
3436 && sgp->sg_gui_fg == INVALCOLOR
3437 && sgp->sg_gui_bg == INVALCOLOR
Bram Moolenaare023e882020-05-31 16:42:30 +02003438 && sgp->sg_gui_sp == INVALCOLOR
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003439# endif
3440 )
3441 sgp->sg_cterm_attr = sgp->sg_cterm;
3442 else
3443 {
3444 at_en.ae_attr = sgp->sg_cterm;
3445 at_en.ae_u.cterm.fg_color = sgp->sg_cterm_fg;
3446 at_en.ae_u.cterm.bg_color = sgp->sg_cterm_bg;
Bram Moolenaare023e882020-05-31 16:42:30 +02003447 at_en.ae_u.cterm.ul_color = sgp->sg_cterm_ul;
PMuncha606f3a2023-11-15 15:35:49 +01003448 at_en.ae_u.cterm.font = sgp->sg_cterm_font;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003449# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003450 at_en.ae_u.cterm.fg_rgb = GUI_MCH_GET_RGB2(sgp->sg_gui_fg);
3451 at_en.ae_u.cterm.bg_rgb = GUI_MCH_GET_RGB2(sgp->sg_gui_bg);
Bram Moolenaarea563cc2020-06-05 19:36:57 +02003452 // Only use the underline/undercurl color when used, it may clear the
3453 // background color if not supported.
Bram Moolenaar84f54632022-06-29 18:39:11 +01003454 if (sgp->sg_cterm & (HL_UNDERLINE | HL_UNDERCURL
3455 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED))
Bram Moolenaarea563cc2020-06-05 19:36:57 +02003456 at_en.ae_u.cterm.ul_rgb = GUI_MCH_GET_RGB2(sgp->sg_gui_sp);
3457 else
3458 at_en.ae_u.cterm.ul_rgb = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003459 if (at_en.ae_u.cterm.fg_rgb == INVALCOLOR
3460 && at_en.ae_u.cterm.bg_rgb == INVALCOLOR)
3461 {
3462 // If both fg and bg are invalid fall back to the cterm colors.
3463 // Helps when the GUI only uses an attribute, e.g. undercurl.
3464 at_en.ae_u.cterm.fg_rgb = CTERMCOLOR;
3465 at_en.ae_u.cterm.bg_rgb = CTERMCOLOR;
3466 }
3467# endif
3468 sgp->sg_cterm_attr = get_attr_entry(&cterm_attr_table, &at_en);
3469 }
3470}
3471
3472/*
3473 * Lookup a highlight group name and return its ID.
3474 * If it is not found, 0 is returned.
3475 */
3476 int
3477syn_name2id(char_u *name)
3478{
3479 int i;
erw7f7f7aaf2021-12-07 21:29:20 +00003480 char_u name_u[MAX_SYN_NAME + 1];
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003481
3482 // Avoid using stricmp() too much, it's slow on some systems
3483 // Avoid alloc()/free(), these are slow too. ID names over 200 chars
3484 // don't deserve to be found!
erw7f7f7aaf2021-12-07 21:29:20 +00003485 vim_strncpy(name_u, name, MAX_SYN_NAME);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003486 vim_strup(name_u);
3487 for (i = highlight_ga.ga_len; --i >= 0; )
3488 if (HL_TABLE()[i].sg_name_u != NULL
3489 && STRCMP(name_u, HL_TABLE()[i].sg_name_u) == 0)
3490 break;
3491 return i + 1;
3492}
3493
3494/*
3495 * Lookup a highlight group name and return its attributes.
3496 * Return zero if not found.
3497 */
3498 int
3499syn_name2attr(char_u *name)
3500{
3501 int id = syn_name2id(name);
3502
3503 if (id != 0)
3504 return syn_id2attr(id);
3505 return 0;
3506}
3507
3508#if defined(FEAT_EVAL) || defined(PROTO)
3509/*
3510 * Return TRUE if highlight group "name" exists.
3511 */
3512 int
3513highlight_exists(char_u *name)
3514{
3515 return (syn_name2id(name) > 0);
3516}
3517
3518# if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3519/*
3520 * Return the name of highlight group "id".
3521 * When not a valid ID return an empty string.
3522 */
3523 char_u *
3524syn_id2name(int id)
3525{
3526 if (id <= 0 || id > highlight_ga.ga_len)
3527 return (char_u *)"";
3528 return HL_TABLE()[id - 1].sg_name;
3529}
3530# endif
3531#endif
3532
3533/*
3534 * Like syn_name2id(), but take a pointer + length argument.
3535 */
3536 int
3537syn_namen2id(char_u *linep, int len)
3538{
3539 char_u *name;
3540 int id = 0;
3541
3542 name = vim_strnsave(linep, len);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003543 if (name == NULL)
3544 return 0;
3545
3546 id = syn_name2id(name);
3547 vim_free(name);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003548 return id;
3549}
3550
3551/*
3552 * Find highlight group name in the table and return its ID.
3553 * The argument is a pointer to the name and the length of the name.
3554 * If it doesn't exist yet, a new entry is created.
3555 * Return 0 for failure.
3556 */
3557 int
3558syn_check_group(char_u *pp, int len)
3559{
3560 int id;
3561 char_u *name;
3562
erw7f7f7aaf2021-12-07 21:29:20 +00003563 if (len > MAX_SYN_NAME)
3564 {
3565 emsg(_(e_highlight_group_name_too_long));
3566 return 0;
3567 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003568 name = vim_strnsave(pp, len);
3569 if (name == NULL)
3570 return 0;
3571
3572 id = syn_name2id(name);
3573 if (id == 0) // doesn't exist yet
3574 id = syn_add_group(name);
3575 else
3576 vim_free(name);
3577 return id;
3578}
3579
3580/*
3581 * Add new highlight group and return its ID.
3582 * "name" must be an allocated string, it will be consumed.
3583 * Return 0 for failure.
3584 */
3585 static int
3586syn_add_group(char_u *name)
3587{
3588 char_u *p;
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003589 char_u *name_up;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003590
Gregory Andersd4376dc2023-08-20 19:14:03 +02003591 // Check that the name is valid (ASCII letters, digits, underscores, dots, or hyphens).
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003592 for (p = name; *p != NUL; ++p)
3593 {
3594 if (!vim_isprintc(*p))
3595 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003596 emsg(_(e_unprintable_character_in_group_name));
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003597 vim_free(name);
3598 return 0;
3599 }
Gregory Andersd4376dc2023-08-20 19:14:03 +02003600 else if (!ASCII_ISALNUM(*p) && *p != '_' && *p != '.' && *p != '-')
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003601 {
3602 // This is an error, but since there previously was no check only
3603 // give a warning.
3604 msg_source(HL_ATTR(HLF_W));
3605 msg(_("W18: Invalid character in group name"));
3606 break;
3607 }
3608 }
3609
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003610 // First call for this growarray: init growing array.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003611 if (highlight_ga.ga_data == NULL)
3612 {
3613 highlight_ga.ga_itemsize = sizeof(hl_group_T);
3614 highlight_ga.ga_growsize = 10;
3615 }
3616
3617 if (highlight_ga.ga_len >= MAX_HL_ID)
3618 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003619 emsg(_(e_too_many_highlight_and_syntax_groups));
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003620 vim_free(name);
3621 return 0;
3622 }
3623
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003624 // Make room for at least one other syntax_highlight entry.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003625 if (ga_grow(&highlight_ga, 1) == FAIL)
3626 {
3627 vim_free(name);
3628 return 0;
3629 }
3630
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003631 name_up = vim_strsave_up(name);
3632 if (name_up == NULL)
3633 {
3634 vim_free(name);
3635 return 0;
3636 }
3637
Bram Moolenaara80faa82020-04-12 19:37:17 +02003638 CLEAR_POINTER(&(HL_TABLE()[highlight_ga.ga_len]));
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003639 HL_TABLE()[highlight_ga.ga_len].sg_name = name;
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003640 HL_TABLE()[highlight_ga.ga_len].sg_name_u = name_up;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003641#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3642 HL_TABLE()[highlight_ga.ga_len].sg_gui_bg = INVALCOLOR;
3643 HL_TABLE()[highlight_ga.ga_len].sg_gui_fg = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003644 HL_TABLE()[highlight_ga.ga_len].sg_gui_sp = INVALCOLOR;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003645#endif
3646 ++highlight_ga.ga_len;
3647
3648 return highlight_ga.ga_len; // ID is index plus one
3649}
3650
3651/*
3652 * When, just after calling syn_add_group(), an error is discovered, this
3653 * function deletes the new name.
3654 */
3655 static void
3656syn_unadd_group(void)
3657{
3658 --highlight_ga.ga_len;
3659 vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name);
3660 vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name_u);
3661}
3662
3663/*
3664 * Translate a group ID to highlight attributes.
Bram Moolenaar87f3a2c2022-08-10 20:50:23 +01003665 * "hl_id" must be valid: > 0, caller must check.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003666 */
3667 int
3668syn_id2attr(int hl_id)
3669{
3670 int attr;
3671 hl_group_T *sgp;
3672
3673 hl_id = syn_get_final_id(hl_id);
3674 sgp = &HL_TABLE()[hl_id - 1]; // index is ID minus one
3675
3676#ifdef FEAT_GUI
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003677 // Only use GUI attr when the GUI is being used.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003678 if (gui.in_use)
3679 attr = sgp->sg_gui_attr;
3680 else
3681#endif
3682 if (IS_CTERM)
3683 attr = sgp->sg_cterm_attr;
3684 else
3685 attr = sgp->sg_term_attr;
3686
3687 return attr;
3688}
3689
3690#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
3691/*
3692 * Get the GUI colors and attributes for a group ID.
3693 * NOTE: the colors will be INVALCOLOR when not set, the color otherwise.
3694 */
3695 int
3696syn_id2colors(int hl_id, guicolor_T *fgp, guicolor_T *bgp)
3697{
3698 hl_group_T *sgp;
3699
3700 hl_id = syn_get_final_id(hl_id);
3701 sgp = &HL_TABLE()[hl_id - 1]; // index is ID minus one
3702
3703 *fgp = sgp->sg_gui_fg;
3704 *bgp = sgp->sg_gui_bg;
3705 return sgp->sg_gui;
3706}
3707#endif
3708
3709#if (defined(MSWIN) \
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003710 && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) \
3711 && defined(FEAT_TERMGUICOLORS)) \
3712 || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003713 void
3714syn_id2cterm_bg(int hl_id, int *fgp, int *bgp)
3715{
3716 hl_group_T *sgp;
3717
3718 hl_id = syn_get_final_id(hl_id);
3719 sgp = &HL_TABLE()[hl_id - 1]; // index is ID minus one
3720 *fgp = sgp->sg_cterm_fg - 1;
3721 *bgp = sgp->sg_cterm_bg - 1;
3722}
3723#endif
3724
3725/*
3726 * Translate a group ID to the final group ID (following links).
3727 */
3728 int
3729syn_get_final_id(int hl_id)
3730{
3731 int count;
3732 hl_group_T *sgp;
3733
3734 if (hl_id > highlight_ga.ga_len || hl_id < 1)
3735 return 0; // Can be called from eval!!
3736
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003737 // Follow links until there is no more.
3738 // Look out for loops! Break after 100 links.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003739 for (count = 100; --count >= 0; )
3740 {
3741 sgp = &HL_TABLE()[hl_id - 1]; // index is ID minus one
3742 if (sgp->sg_link == 0 || sgp->sg_link > highlight_ga.ga_len)
3743 break;
3744 hl_id = sgp->sg_link;
3745 }
3746
3747 return hl_id;
3748}
3749
3750#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
3751/*
3752 * Call this function just after the GUI has started.
3753 * Also called when 'termguicolors' was set, gui.in_use will be FALSE then.
3754 * It finds the font and color handles for the highlighting groups.
3755 */
3756 void
3757highlight_gui_started(void)
3758{
3759 int idx;
3760
3761 // First get the colors from the "Normal" and "Menu" group, if set
3762 if (USE_24BIT)
3763 set_normal_colors();
3764
3765 for (idx = 0; idx < highlight_ga.ga_len; ++idx)
3766 gui_do_one_color(idx, FALSE, FALSE);
3767
3768 highlight_changed();
3769}
3770
3771 static void
3772gui_do_one_color(
3773 int idx,
3774 int do_menu UNUSED, // TRUE: might set the menu font
3775 int do_tooltip UNUSED) // TRUE: might set the tooltip font
3776{
3777 int didit = FALSE;
3778
3779# ifdef FEAT_GUI
3780# ifdef FEAT_TERMGUICOLORS
3781 if (gui.in_use)
3782# endif
3783 if (HL_TABLE()[idx].sg_font_name != NULL)
3784 {
3785 hl_do_font(idx, HL_TABLE()[idx].sg_font_name, FALSE, do_menu,
3786 do_tooltip, TRUE);
3787 didit = TRUE;
3788 }
3789# endif
3790 if (HL_TABLE()[idx].sg_gui_fg_name != NULL)
3791 {
3792 HL_TABLE()[idx].sg_gui_fg =
3793 color_name2handle(HL_TABLE()[idx].sg_gui_fg_name);
3794 didit = TRUE;
3795 }
3796 if (HL_TABLE()[idx].sg_gui_bg_name != NULL)
3797 {
3798 HL_TABLE()[idx].sg_gui_bg =
3799 color_name2handle(HL_TABLE()[idx].sg_gui_bg_name);
3800 didit = TRUE;
3801 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003802 if (HL_TABLE()[idx].sg_gui_sp_name != NULL)
3803 {
3804 HL_TABLE()[idx].sg_gui_sp =
3805 color_name2handle(HL_TABLE()[idx].sg_gui_sp_name);
3806 didit = TRUE;
3807 }
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003808 if (didit) // need to get a new attr number
3809 set_hl_attr(idx);
3810}
3811#endif
3812
3813#if defined(USER_HIGHLIGHT) && defined(FEAT_STL_OPT)
3814/*
3815 * Apply difference between User[1-9] and HLF_S to HLF_SNC, HLF_ST or HLF_STNC.
3816 */
3817 static void
3818combine_stl_hlt(
3819 int id,
3820 int id_S,
3821 int id_alt,
3822 int hlcnt,
3823 int i,
3824 int hlf,
3825 int *table)
3826{
3827 hl_group_T *hlt = HL_TABLE();
3828
3829 if (id_alt == 0)
3830 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003831 CLEAR_POINTER(&hlt[hlcnt + i]);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003832 hlt[hlcnt + i].sg_term = highlight_attr[hlf];
3833 hlt[hlcnt + i].sg_cterm = highlight_attr[hlf];
3834# if defined(FEAT_GUI) || defined(FEAT_EVAL)
3835 hlt[hlcnt + i].sg_gui = highlight_attr[hlf];
3836# endif
3837 }
3838 else
3839 mch_memmove(&hlt[hlcnt + i],
3840 &hlt[id_alt - 1],
3841 sizeof(hl_group_T));
3842 hlt[hlcnt + i].sg_link = 0;
3843
3844 hlt[hlcnt + i].sg_term ^=
3845 hlt[id - 1].sg_term ^ hlt[id_S - 1].sg_term;
3846 if (hlt[id - 1].sg_start != hlt[id_S - 1].sg_start)
3847 hlt[hlcnt + i].sg_start = hlt[id - 1].sg_start;
3848 if (hlt[id - 1].sg_stop != hlt[id_S - 1].sg_stop)
3849 hlt[hlcnt + i].sg_stop = hlt[id - 1].sg_stop;
3850 hlt[hlcnt + i].sg_cterm ^=
3851 hlt[id - 1].sg_cterm ^ hlt[id_S - 1].sg_cterm;
3852 if (hlt[id - 1].sg_cterm_fg != hlt[id_S - 1].sg_cterm_fg)
3853 hlt[hlcnt + i].sg_cterm_fg = hlt[id - 1].sg_cterm_fg;
3854 if (hlt[id - 1].sg_cterm_bg != hlt[id_S - 1].sg_cterm_bg)
3855 hlt[hlcnt + i].sg_cterm_bg = hlt[id - 1].sg_cterm_bg;
PMuncha606f3a2023-11-15 15:35:49 +01003856 if (hlt[id - 1].sg_cterm_font != hlt[id_S - 1].sg_cterm_font)
3857 hlt[hlcnt + i].sg_cterm_font = hlt[id - 1].sg_cterm_font;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003858# if defined(FEAT_GUI) || defined(FEAT_EVAL)
3859 hlt[hlcnt + i].sg_gui ^=
3860 hlt[id - 1].sg_gui ^ hlt[id_S - 1].sg_gui;
3861# endif
3862# ifdef FEAT_GUI
3863 if (hlt[id - 1].sg_gui_fg != hlt[id_S - 1].sg_gui_fg)
3864 hlt[hlcnt + i].sg_gui_fg = hlt[id - 1].sg_gui_fg;
3865 if (hlt[id - 1].sg_gui_bg != hlt[id_S - 1].sg_gui_bg)
3866 hlt[hlcnt + i].sg_gui_bg = hlt[id - 1].sg_gui_bg;
3867 if (hlt[id - 1].sg_gui_sp != hlt[id_S - 1].sg_gui_sp)
3868 hlt[hlcnt + i].sg_gui_sp = hlt[id - 1].sg_gui_sp;
3869 if (hlt[id - 1].sg_font != hlt[id_S - 1].sg_font)
3870 hlt[hlcnt + i].sg_font = hlt[id - 1].sg_font;
3871# ifdef FEAT_XFONTSET
3872 if (hlt[id - 1].sg_fontset != hlt[id_S - 1].sg_fontset)
3873 hlt[hlcnt + i].sg_fontset = hlt[id - 1].sg_fontset;
3874# endif
3875# endif
3876 highlight_ga.ga_len = hlcnt + i + 1;
3877 set_hl_attr(hlcnt + i); // At long last we can apply
3878 table[i] = syn_id2attr(hlcnt + i + 1);
3879}
3880#endif
3881
3882/*
3883 * Translate the 'highlight' option into attributes in highlight_attr[] and
3884 * set up the user highlights User1..9. If FEAT_STL_OPT is in use, a set of
3885 * corresponding highlights to use on top of HLF_SNC is computed.
3886 * Called only when the 'highlight' option has been changed and upon first
3887 * screen redraw after any :highlight command.
3888 * Return FAIL when an invalid flag is found in 'highlight'. OK otherwise.
3889 */
3890 int
3891highlight_changed(void)
3892{
3893 int hlf;
3894 int i;
3895 char_u *p;
3896 int attr;
3897 char_u *end;
3898 int id;
3899#ifdef USER_HIGHLIGHT
3900 char_u userhl[30]; // use 30 to avoid compiler warning
3901# ifdef FEAT_STL_OPT
3902 int id_S = -1;
3903 int id_SNC = 0;
3904# ifdef FEAT_TERMINAL
3905 int id_ST = 0;
3906 int id_STNC = 0;
3907# endif
3908 int hlcnt;
3909# endif
3910#endif
3911 static int hl_flags[HLF_COUNT] = HL_FLAGS;
3912
3913 need_highlight_changed = FALSE;
3914
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003915#ifdef FEAT_TERMINAL
3916 term_update_colors_all();
3917 term_update_wincolor_all();
3918#endif
3919
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003920 // Clear all attributes.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003921 for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
3922 highlight_attr[hlf] = 0;
3923
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003924 // First set all attributes to their default value.
3925 // Then use the attributes from the 'highlight' option.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003926 for (i = 0; i < 2; ++i)
3927 {
3928 if (i)
3929 p = p_hl;
3930 else
3931 p = get_highlight_default();
3932 if (p == NULL) // just in case
3933 continue;
3934
3935 while (*p)
3936 {
3937 for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
3938 if (hl_flags[hlf] == *p)
3939 break;
3940 ++p;
3941 if (hlf == (int)HLF_COUNT || *p == NUL)
3942 return FAIL;
3943
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01003944 // Allow several hl_flags to be combined, like "bu" for
3945 // bold-underlined.
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003946 attr = 0;
Bram Moolenaarc95e8d62019-12-05 18:35:44 +01003947 for ( ; *p && *p != ','; ++p) // parse up to comma
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003948 {
3949 if (VIM_ISWHITE(*p)) // ignore white space
3950 continue;
3951
3952 if (attr > HL_ALL) // Combination with ':' is not allowed.
3953 return FAIL;
3954
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003955 // Note: Keep this in sync with expand_set_highlight().
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003956 switch (*p)
3957 {
3958 case 'b': attr |= HL_BOLD;
3959 break;
3960 case 'i': attr |= HL_ITALIC;
3961 break;
3962 case '-':
3963 case 'n': // no highlighting
3964 break;
3965 case 'r': attr |= HL_INVERSE;
3966 break;
3967 case 's': attr |= HL_STANDOUT;
3968 break;
3969 case 'u': attr |= HL_UNDERLINE;
3970 break;
3971 case 'c': attr |= HL_UNDERCURL;
3972 break;
Bram Moolenaar84f54632022-06-29 18:39:11 +01003973 case '2': attr |= HL_UNDERDOUBLE;
3974 break;
3975 case 'd': attr |= HL_UNDERDOTTED;
3976 break;
3977 case '=': attr |= HL_UNDERDASHED;
3978 break;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02003979 case 't': attr |= HL_STRIKETHROUGH;
3980 break;
3981 case ':': ++p; // highlight group name
3982 if (attr || *p == NUL) // no combinations
3983 return FAIL;
3984 end = vim_strchr(p, ',');
3985 if (end == NULL)
3986 end = p + STRLEN(p);
3987 id = syn_check_group(p, (int)(end - p));
3988 if (id == 0)
3989 return FAIL;
3990 attr = syn_id2attr(id);
3991 p = end - 1;
3992#if defined(FEAT_STL_OPT) && defined(USER_HIGHLIGHT)
3993 if (hlf == (int)HLF_SNC)
3994 id_SNC = syn_get_final_id(id);
3995# ifdef FEAT_TERMINAL
3996 else if (hlf == (int)HLF_ST)
3997 id_ST = syn_get_final_id(id);
3998 else if (hlf == (int)HLF_STNC)
3999 id_STNC = syn_get_final_id(id);
4000# endif
4001 else if (hlf == (int)HLF_S)
4002 id_S = syn_get_final_id(id);
4003#endif
4004 break;
4005 default: return FAIL;
4006 }
4007 }
4008 highlight_attr[hlf] = attr;
4009
4010 p = skip_to_option_part(p); // skip comma and spaces
4011 }
4012 }
4013
4014#ifdef USER_HIGHLIGHT
Yegappan Lakshmananad6b90c2021-10-18 22:13:57 +01004015 // Setup the user highlights
4016 //
4017 // Temporarily utilize 28 more hl entries:
4018 // 9 for User1-User9 combined with StatusLineNC
4019 // 9 for User1-User9 combined with StatusLineTerm
4020 // 9 for User1-User9 combined with StatusLineTermNC
4021 // 1 for StatusLine default
4022 // Have to be in there simultaneously in case of table overflows in
4023 // get_attr_entry()
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004024# ifdef FEAT_STL_OPT
4025 if (ga_grow(&highlight_ga, 28) == FAIL)
4026 return FAIL;
4027 hlcnt = highlight_ga.ga_len;
4028 if (id_S == -1)
4029 {
4030 // Make sure id_S is always valid to simplify code below. Use the last
4031 // entry.
Bram Moolenaara80faa82020-04-12 19:37:17 +02004032 CLEAR_POINTER(&HL_TABLE()[hlcnt + 27]);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004033 HL_TABLE()[hlcnt + 18].sg_term = highlight_attr[HLF_S];
4034 id_S = hlcnt + 19;
4035 }
4036# endif
4037 for (i = 0; i < 9; i++)
4038 {
4039 sprintf((char *)userhl, "User%d", i + 1);
4040 id = syn_name2id(userhl);
4041 if (id == 0)
4042 {
4043 highlight_user[i] = 0;
4044# ifdef FEAT_STL_OPT
4045 highlight_stlnc[i] = 0;
4046# ifdef FEAT_TERMINAL
4047 highlight_stlterm[i] = 0;
4048 highlight_stltermnc[i] = 0;
4049# endif
4050# endif
4051 }
4052 else
4053 {
4054 highlight_user[i] = syn_id2attr(id);
4055# ifdef FEAT_STL_OPT
4056 combine_stl_hlt(id, id_S, id_SNC, hlcnt, i,
4057 HLF_SNC, highlight_stlnc);
4058# ifdef FEAT_TERMINAL
4059 combine_stl_hlt(id, id_S, id_ST, hlcnt + 9, i,
4060 HLF_ST, highlight_stlterm);
4061 combine_stl_hlt(id, id_S, id_STNC, hlcnt + 18, i,
4062 HLF_STNC, highlight_stltermnc);
4063# endif
4064# endif
4065 }
4066 }
4067# ifdef FEAT_STL_OPT
4068 highlight_ga.ga_len = hlcnt;
4069# endif
4070
4071#endif // USER_HIGHLIGHT
4072
4073 return OK;
4074}
4075
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004076static void highlight_list(void);
4077static void highlight_list_two(int cnt, int attr);
4078
4079/*
4080 * Handle command line completion for :highlight command.
4081 */
4082 void
4083set_context_in_highlight_cmd(expand_T *xp, char_u *arg)
4084{
4085 char_u *p;
4086
4087 // Default: expand group names
4088 xp->xp_context = EXPAND_HIGHLIGHT;
4089 xp->xp_pattern = arg;
4090 include_link = 2;
4091 include_default = 1;
4092
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004093 if (*arg == NUL)
4094 return;
4095
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004096 // (part of) subcommand already typed
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004097 p = skiptowhite(arg);
4098 if (*p == NUL)
4099 return;
4100
4101 // past "default" or group name
4102 include_default = 0;
4103 if (STRNCMP("default", arg, p - arg) == 0)
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004104 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004105 arg = skipwhite(p);
4106 xp->xp_pattern = arg;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004107 p = skiptowhite(arg);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004108 }
4109 if (*p == NUL)
4110 return;
4111
4112 // past group name
4113 include_link = 0;
4114 if (arg[1] == 'i' && arg[0] == 'N')
4115 highlight_list();
4116 if (STRNCMP("link", arg, p - arg) == 0
4117 || STRNCMP("clear", arg, p - arg) == 0)
4118 {
4119 xp->xp_pattern = skipwhite(p);
4120 p = skiptowhite(xp->xp_pattern);
4121 if (*p != NUL) // past first group name
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004122 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004123 xp->xp_pattern = skipwhite(p);
4124 p = skiptowhite(xp->xp_pattern);
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004125 }
4126 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004127 if (*p != NUL) // past group name(s)
4128 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004129}
4130
4131/*
4132 * List highlighting matches in a nice way.
4133 */
4134 static void
4135highlight_list(void)
4136{
4137 int i;
4138
4139 for (i = 10; --i >= 0; )
4140 highlight_list_two(i, HL_ATTR(HLF_D));
4141 for (i = 40; --i >= 0; )
4142 highlight_list_two(99, 0);
4143}
4144
4145 static void
4146highlight_list_two(int cnt, int attr)
4147{
4148 msg_puts_attr(&("N \bI \b! \b"[cnt / 11]), attr);
4149 msg_clr_eos();
4150 out_flush();
4151 ui_delay(cnt == 99 ? 40L : (long)cnt * 50L, FALSE);
4152}
4153
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004154/*
4155 * Function given to ExpandGeneric() to obtain the list of group names.
4156 */
4157 char_u *
4158get_highlight_name(expand_T *xp UNUSED, int idx)
4159{
4160 return get_highlight_name_ext(xp, idx, TRUE);
4161}
4162
4163/*
4164 * Obtain a highlight group name.
4165 * When "skip_cleared" is TRUE don't return a cleared entry.
4166 */
4167 char_u *
4168get_highlight_name_ext(expand_T *xp UNUSED, int idx, int skip_cleared)
4169{
4170 if (idx < 0)
4171 return NULL;
4172
4173 // Items are never removed from the table, skip the ones that were
4174 // cleared.
4175 if (skip_cleared && idx < highlight_ga.ga_len && HL_TABLE()[idx].sg_cleared)
4176 return (char_u *)"";
4177
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004178 if (idx == highlight_ga.ga_len && include_none != 0)
4179 return (char_u *)"none";
4180 if (idx == highlight_ga.ga_len + include_none && include_default != 0)
4181 return (char_u *)"default";
4182 if (idx == highlight_ga.ga_len + include_none + include_default
4183 && include_link != 0)
4184 return (char_u *)"link";
4185 if (idx == highlight_ga.ga_len + include_none + include_default + 1
4186 && include_link != 0)
4187 return (char_u *)"clear";
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004188 if (idx >= highlight_ga.ga_len)
4189 return NULL;
4190 return HL_TABLE()[idx].sg_name;
4191}
Bram Moolenaarf9cc9f22019-07-14 21:29:22 +02004192
4193#if defined(FEAT_GUI) || defined(PROTO)
4194/*
4195 * Free all the highlight group fonts.
4196 * Used when quitting for systems which need it.
4197 */
4198 void
4199free_highlight_fonts(void)
4200{
4201 int idx;
4202
4203 for (idx = 0; idx < highlight_ga.ga_len; ++idx)
4204 {
4205 gui_mch_free_font(HL_TABLE()[idx].sg_font);
4206 HL_TABLE()[idx].sg_font = NOFONT;
4207# ifdef FEAT_XFONTSET
4208 gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
4209 HL_TABLE()[idx].sg_fontset = NOFONTSET;
4210# endif
4211 }
4212
4213 gui_mch_free_font(gui.norm_font);
4214# ifdef FEAT_XFONTSET
4215 gui_mch_free_fontset(gui.fontset);
4216# endif
4217# ifndef FEAT_GUI_GTK
4218 gui_mch_free_font(gui.bold_font);
4219 gui_mch_free_font(gui.ital_font);
4220 gui_mch_free_font(gui.boldital_font);
4221# endif
4222}
4223#endif
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004224
4225#if defined(FEAT_EVAL) || defined(PROTO)
4226/*
4227 * Convert each of the highlight attribute bits (bold, standout, underline,
4228 * etc.) set in 'hlattr' into a separate boolean item in a Dictionary with
4229 * the attribute name as the key.
4230 */
4231 static dict_T *
4232highlight_get_attr_dict(int hlattr)
4233{
4234 dict_T *dict;
4235 int i;
4236
4237 dict = dict_alloc();
4238 if (dict == NULL)
4239 return NULL;
4240
John Marriott34f00dd2024-04-08 23:28:12 +02004241 for (i = 0; i < (int)ARRAY_LENGTH(highlight_index_tab); ++i)
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004242 {
John Marriott34f00dd2024-04-08 23:28:12 +02004243 if (hlattr & highlight_index_tab[i]->key)
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004244 {
John Marriott8d4477e2024-11-02 15:59:01 +01004245 dict_add_bool(dict, (char *)highlight_index_tab[i]->value.string,
4246 VVAL_TRUE);
4247 // don't want "inverse"/"reverse"
4248 hlattr &= ~highlight_index_tab[i]->key;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004249 }
4250 }
4251
4252 return dict;
4253}
4254
4255/*
4256 * Return the attributes of the highlight group at index 'hl_idx' as a
4257 * Dictionary. If 'resolve_link' is TRUE, then resolves the highlight group
4258 * links recursively.
4259 */
4260 static dict_T *
4261highlight_get_info(int hl_idx, int resolve_link)
4262{
4263 dict_T *dict;
4264 hl_group_T *sgp;
4265 dict_T *attr_dict;
4266 int hlgid;
4267
4268 dict = dict_alloc();
4269 if (dict == NULL)
4270 return dict;
4271
4272 sgp = &HL_TABLE()[hl_idx];
4273 // highlight group id is 1-based
4274 hlgid = hl_idx + 1;
4275
4276 if (dict_add_string(dict, "name", sgp->sg_name) == FAIL)
4277 goto error;
4278 if (dict_add_number(dict, "id", hlgid) == FAIL)
4279 goto error;
4280
4281 if (sgp->sg_link && resolve_link)
4282 {
4283 // resolve the highlight group link recursively
4284 while (sgp->sg_link)
4285 {
4286 hlgid = sgp->sg_link;
4287 sgp = &HL_TABLE()[sgp->sg_link - 1];
4288 }
4289 }
4290
4291 if (sgp->sg_term != 0)
4292 {
4293 attr_dict = highlight_get_attr_dict(sgp->sg_term);
4294 if (attr_dict != NULL)
4295 if (dict_add_dict(dict, "term", attr_dict) == FAIL)
4296 goto error;
4297 }
4298 if (sgp->sg_start != NULL)
4299 if (dict_add_string(dict, "start", sgp->sg_start) == FAIL)
4300 goto error;
4301 if (sgp->sg_stop != NULL)
4302 if (dict_add_string(dict, "stop", sgp->sg_stop) == FAIL)
4303 goto error;
4304 if (sgp->sg_cterm != 0)
4305 {
4306 attr_dict = highlight_get_attr_dict(sgp->sg_cterm);
4307 if (attr_dict != NULL)
4308 if (dict_add_dict(dict, "cterm", attr_dict) == FAIL)
4309 goto error;
4310 }
4311 if (sgp->sg_cterm_fg != 0)
4312 if (dict_add_string(dict, "ctermfg",
4313 highlight_color(hlgid, (char_u *)"fg", 'c')) == FAIL)
4314 goto error;
4315 if (sgp->sg_cterm_bg != 0)
4316 if (dict_add_string(dict, "ctermbg",
4317 highlight_color(hlgid, (char_u *)"bg", 'c')) == FAIL)
4318 goto error;
4319 if (sgp->sg_cterm_ul != 0)
4320 if (dict_add_string(dict, "ctermul",
4321 highlight_color(hlgid, (char_u *)"ul", 'c')) == FAIL)
4322 goto error;
PMuncha606f3a2023-11-15 15:35:49 +01004323 if (sgp->sg_cterm_font != 0)
4324 if (dict_add_string(dict, "ctermfont",
4325 highlight_color(hlgid, (char_u *)"font", 'c')) == FAIL)
4326 goto error;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004327 if (sgp->sg_gui != 0)
4328 {
4329 attr_dict = highlight_get_attr_dict(sgp->sg_gui);
4330 if (attr_dict != NULL)
4331 if (dict_add_dict(dict, "gui", attr_dict) == FAIL)
4332 goto error;
4333 }
4334 if (sgp->sg_gui_fg_name != NULL)
4335 if (dict_add_string(dict, "guifg",
4336 highlight_color(hlgid, (char_u *)"fg", 'g')) == FAIL)
4337 goto error;
4338 if (sgp->sg_gui_bg_name != NULL)
4339 if (dict_add_string(dict, "guibg",
4340 highlight_color(hlgid, (char_u *)"bg", 'g')) == FAIL)
4341 goto error;
4342 if (sgp->sg_gui_sp_name != NULL)
4343 if (dict_add_string(dict, "guisp",
4344 highlight_color(hlgid, (char_u *)"sp", 'g')) == FAIL)
4345 goto error;
4346# ifdef FEAT_GUI
4347 if (sgp->sg_font_name != NULL)
4348 if (dict_add_string(dict, "font", sgp->sg_font_name) == FAIL)
4349 goto error;
4350# endif
4351 if (sgp->sg_link)
4352 {
4353 char_u *link;
4354
4355 link = HL_TABLE()[sgp->sg_link - 1].sg_name;
4356 if (link != NULL && dict_add_string(dict, "linksto", link) == FAIL)
4357 goto error;
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004358
4359 if (sgp->sg_deflink)
4360 dict_add_bool(dict, "default", VVAL_TRUE);
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004361 }
4362 if (dict_len(dict) == 2)
4363 // If only 'name' is present, then the highlight group is cleared.
4364 dict_add_bool(dict, "cleared", VVAL_TRUE);
4365
4366 return dict;
4367
4368error:
4369 vim_free(dict);
4370 return NULL;
4371}
4372
4373/*
4374 * "hlget([name])" function
4375 * Return the attributes of a specific highlight group (if specified) or all
4376 * the highlight groups.
4377 */
4378 void
4379f_hlget(typval_T *argvars, typval_T *rettv)
4380{
4381 list_T *list;
4382 dict_T *dict;
4383 int i;
4384 char_u *hlarg = NULL;
4385 int resolve_link = FALSE;
4386
4387 if (rettv_list_alloc(rettv) == FAIL)
4388 return;
4389
4390 if (check_for_opt_string_arg(argvars, 0) == FAIL
4391 || (argvars[0].v_type != VAR_UNKNOWN
4392 && check_for_opt_bool_arg(argvars, 1) == FAIL))
4393 return;
4394
4395 if (argvars[0].v_type != VAR_UNKNOWN)
4396 {
4397 // highlight group name supplied
4398 hlarg = tv_get_string_chk(&argvars[0]);
4399 if (hlarg == NULL)
4400 return;
4401
4402 if (argvars[1].v_type != VAR_UNKNOWN)
4403 {
4404 int error = FALSE;
4405
4406 resolve_link = tv_get_bool_chk(&argvars[1], &error);
4407 if (error)
4408 return;
4409 }
4410 }
4411
4412 list = rettv->vval.v_list;
4413 for (i = 0; i < highlight_ga.ga_len && !got_int; ++i)
4414 {
4415 if (hlarg == NULL || STRICMP(hlarg, HL_TABLE()[i].sg_name) == 0)
4416 {
4417 dict = highlight_get_info(i, resolve_link);
4418 if (dict != NULL)
4419 list_append_dict(list, dict);
4420 }
4421 }
4422}
4423
4424/*
4425 * Returns the string value at 'dict[key]'. Returns NULL, if 'key' is not in
4426 * 'dict' or the value is not a string type. If the value is not a string type
4427 * or is NULL, then 'error' is set to TRUE.
4428 */
4429 static char_u *
4430hldict_get_string(dict_T *dict, char_u *key, int *error)
4431{
4432 dictitem_T *di;
4433
4434 *error = FALSE;
4435 di = dict_find(dict, key, -1);
4436 if (di == NULL)
4437 return NULL;
4438
4439 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
4440 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004441 emsg(_(e_string_required));
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004442 *error = TRUE;
4443 return NULL;
4444 }
4445
4446 return di->di_tv.vval.v_string;
4447}
4448
4449/*
4450 * Convert the highlight attribute Dictionary at 'dict[key]' into a string
4451 * value in 'attr_str' of length 'len'. Returns FALSE if 'dict[key]' is not a
4452 * Dictionary or is NULL.
4453 */
4454 static int
4455hldict_attr_to_str(
4456 dict_T *dict,
4457 char_u *key,
4458 char_u *attr_str,
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004459 size_t len)
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004460{
4461 dictitem_T *di;
4462 dict_T *attrdict;
4463 int i;
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004464 char_u *p;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004465
4466 attr_str[0] = NUL;
4467 di = dict_find(dict, key, -1);
4468 if (di == NULL)
4469 return TRUE;
4470
4471 if (di->di_tv.v_type != VAR_DICT || di->di_tv.vval.v_dict == NULL)
4472 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004473 emsg(_(e_dictionary_required));
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004474 return FALSE;
4475 }
4476
4477 attrdict = di->di_tv.vval.v_dict;
4478
4479 // If the attribute dict is empty, then return NONE to clear the attributes
4480 if (dict_len(attrdict) == 0)
4481 {
4482 vim_strcat(attr_str, (char_u *)"NONE", len);
4483 return TRUE;
4484 }
4485
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004486 p = attr_str;
John Marriott34f00dd2024-04-08 23:28:12 +02004487 for (i = 0; i < (int)ARRAY_LENGTH(highlight_tab); ++i)
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004488 {
John Marriott8d4477e2024-11-02 15:59:01 +01004489 if (dict_get_bool(attrdict, (char *)highlight_tab[i].value.string,
4490 VVAL_FALSE) == VVAL_TRUE)
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004491 {
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004492 if (p != attr_str && (size_t)(p - attr_str + 2) < len)
4493 STRCPY(p, (char_u *)",");
John Marriott8d4477e2024-11-02 15:59:01 +01004494 if (p - attr_str + highlight_tab[i].value.length + 1 < len)
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004495 {
John Marriott8d4477e2024-11-02 15:59:01 +01004496 STRCPY(p, highlight_tab[i].value.string);
4497 p += highlight_tab[i].value.length;
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004498 }
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004499 }
4500 }
4501
4502 return TRUE;
4503}
4504
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004505// Temporary buffer used to store the command string produced by hlset().
4506// IObuff cannot be used for this as the error messages produced by hlset()
4507// internally use IObuff.
4508#define HLSETBUFSZ 512
4509static char_u hlsetBuf[HLSETBUFSZ + 1];
4510
4511/*
4512 * Add the highlight attribute "attr" of length "attrlen" and "value" at
4513 * "dptr", which points into "hlsetBuf".
4514 * Returns the updated pointer.
4515 */
4516 static char_u *
4517add_attr_and_value(char_u *dptr, char_u *attr, int attrlen, char_u *value)
4518{
4519 size_t vallen;
4520
4521 // Do nothing if the value is not specified or is empty
4522 if (value == NULL || *value == NUL)
4523 return dptr;
4524
4525 vallen = STRLEN(value);
4526 if (dptr + attrlen + vallen + 1 < hlsetBuf + HLSETBUFSZ)
4527 {
4528 STRCPY(dptr, attr);
4529 dptr += attrlen;
4530 STRCPY(dptr, value);
4531 dptr += vallen;
4532 }
4533
4534 return dptr;
4535}
4536
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004537/*
4538 * Add or update a highlight group using 'dict' items. Returns TRUE if
4539 * successfully updated the highlight group.
4540 */
4541 static int
4542hlg_add_or_update(dict_T *dict)
4543{
4544 char_u *name;
4545 int error;
Bram Moolenaar84f54632022-06-29 18:39:11 +01004546 char_u term_attr[MAX_ATTR_LEN];
4547 char_u cterm_attr[MAX_ATTR_LEN];
4548 char_u gui_attr[MAX_ATTR_LEN];
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004549 char_u *start;
4550 char_u *stop;
4551 char_u *ctermfg;
4552 char_u *ctermbg;
4553 char_u *ctermul;
PMuncha606f3a2023-11-15 15:35:49 +01004554 char_u *ctermfont;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004555 char_u *guifg;
4556 char_u *guibg;
4557 char_u *guisp;
4558# ifdef FEAT_GUI
4559 char_u *font;
4560# endif
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004561 int forceit = FALSE;
4562 int dodefault = FALSE;
4563 int done = FALSE;
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004564 char_u *p;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004565
4566 name = hldict_get_string(dict, (char_u *)"name", &error);
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004567 if (name == NULL || *name == NUL || error)
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004568 return FALSE;
4569
Bram Moolenaard61efa52022-07-23 09:52:04 +01004570 if (dict_get_bool(dict, "force", VVAL_FALSE) == VVAL_TRUE)
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004571 forceit = TRUE;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004572
Bram Moolenaard61efa52022-07-23 09:52:04 +01004573 if (dict_get_bool(dict, "default", VVAL_FALSE) == VVAL_TRUE)
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004574 dodefault = TRUE;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004575
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004576 if (dict_has_key(dict, "cleared"))
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004577 {
4578 varnumber_T cleared;
4579
4580 // clear a highlight group
Bram Moolenaard61efa52022-07-23 09:52:04 +01004581 cleared = dict_get_bool(dict, "cleared", FALSE);
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004582 if (cleared == TRUE)
4583 {
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004584 vim_snprintf((char *)hlsetBuf, HLSETBUFSZ, "clear %s", name);
4585 do_highlight(hlsetBuf, forceit, FALSE);
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004586 done = TRUE;
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004587 }
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004588 }
4589
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004590 if (dict_has_key(dict, "linksto"))
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004591 {
4592 char_u *linksto;
4593
4594 // link highlight groups
4595 linksto = hldict_get_string(dict, (char_u *)"linksto", &error);
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004596 if (linksto == NULL || *linksto == NUL || error)
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004597 return FALSE;
4598
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004599 vim_snprintf((char *)hlsetBuf, HLSETBUFSZ, "%slink %s %s",
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004600 dodefault ? "default " : "", name, linksto);
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004601 do_highlight(hlsetBuf, forceit, FALSE);
Yegappan Lakshmanan2a16dc62021-11-16 17:19:30 +00004602
4603 done = TRUE;
4604 }
4605
4606 // If 'cleared' or 'linksto' are specified, then don't process the other
4607 // attributes.
4608 if (done)
4609 return TRUE;
4610
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004611 start = hldict_get_string(dict, (char_u *)"start", &error);
4612 if (error)
4613 return FALSE;
4614
4615 stop = hldict_get_string(dict, (char_u *)"stop", &error);
4616 if (error)
4617 return FALSE;
4618
4619 if (!hldict_attr_to_str(dict, (char_u *)"term", term_attr,
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004620 sizeof(term_attr)))
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004621 return FALSE;
4622
4623 if (!hldict_attr_to_str(dict, (char_u *)"cterm", cterm_attr,
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004624 sizeof(cterm_attr)))
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004625 return FALSE;
4626
4627 ctermfg = hldict_get_string(dict, (char_u *)"ctermfg", &error);
4628 if (error)
4629 return FALSE;
4630
4631 ctermbg = hldict_get_string(dict, (char_u *)"ctermbg", &error);
4632 if (error)
4633 return FALSE;
4634
4635 ctermul = hldict_get_string(dict, (char_u *)"ctermul", &error);
4636 if (error)
4637 return FALSE;
4638
PMuncha606f3a2023-11-15 15:35:49 +01004639 ctermfont = hldict_get_string(dict, (char_u *)"ctermfont", &error);
4640 if (error)
4641 return FALSE;
4642
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004643 if (!hldict_attr_to_str(dict, (char_u *)"gui", gui_attr, sizeof(gui_attr)))
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004644 return FALSE;
4645
4646 guifg = hldict_get_string(dict, (char_u *)"guifg", &error);
4647 if (error)
4648 return FALSE;
4649
4650 guibg = hldict_get_string(dict, (char_u *)"guibg", &error);
4651 if (error)
4652 return FALSE;
4653
4654 guisp = hldict_get_string(dict, (char_u *)"guisp", &error);
4655 if (error)
4656 return FALSE;
4657
4658# ifdef FEAT_GUI
4659 font = hldict_get_string(dict, (char_u *)"font", &error);
4660 if (error)
4661 return FALSE;
4662# endif
4663
4664 // If none of the attributes are specified, then do nothing.
4665 if (term_attr[0] == NUL && start == NULL && stop == NULL
4666 && cterm_attr[0] == NUL && ctermfg == NULL && ctermbg == NULL
PMuncha606f3a2023-11-15 15:35:49 +01004667 && ctermul == NULL && ctermfont == NULL && gui_attr[0] == NUL
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004668# ifdef FEAT_GUI
4669 && font == NULL
4670# endif
4671 && guifg == NULL && guibg == NULL && guisp == NULL
4672 )
4673 return TRUE;
4674
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004675 hlsetBuf[0] = NUL;
4676 p = hlsetBuf;
4677 if (dodefault)
4678 p = add_attr_and_value(p, (char_u *)"default", 7, (char_u *)" ");
4679 p = add_attr_and_value(p, (char_u *)"", 0, name);
4680 p = add_attr_and_value(p, (char_u *)" term=", 6, term_attr);
4681 p = add_attr_and_value(p, (char_u *)" start=", 7, start);
4682 p = add_attr_and_value(p, (char_u *)" stop=", 6, stop);
4683 p = add_attr_and_value(p, (char_u *)" cterm=", 7, cterm_attr);
4684 p = add_attr_and_value(p, (char_u *)" ctermfg=", 9, ctermfg);
4685 p = add_attr_and_value(p, (char_u *)" ctermbg=", 9, ctermbg);
4686 p = add_attr_and_value(p, (char_u *)" ctermul=", 9, ctermul);
PMuncha606f3a2023-11-15 15:35:49 +01004687 p = add_attr_and_value(p, (char_u *)" ctermfont=", 9, ctermfont);
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004688 p = add_attr_and_value(p, (char_u *)" gui=", 5, gui_attr);
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004689# ifdef FEAT_GUI
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004690 p = add_attr_and_value(p, (char_u *)" font=", 6, font);
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004691# endif
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004692 p = add_attr_and_value(p, (char_u *)" guifg=", 7, guifg);
4693 p = add_attr_and_value(p, (char_u *)" guibg=", 7, guibg);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01004694 (void)add_attr_and_value(p, (char_u *)" guisp=", 7, guisp);
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004695
Yegappan Lakshmananbb277fd2021-11-24 20:28:31 +00004696 do_highlight(hlsetBuf, forceit, FALSE);
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004697
4698 return TRUE;
4699}
4700
4701/*
4702 * "hlset([{highlight_attr}])" function
4703 * Add or modify highlight groups
4704 */
4705 void
4706f_hlset(typval_T *argvars, typval_T *rettv)
4707{
4708 listitem_T *li;
4709 dict_T *dict;
4710
4711 rettv->vval.v_number = -1;
4712
4713 if (check_for_list_arg(argvars, 0) == FAIL)
4714 return;
4715
4716 FOR_ALL_LIST_ITEMS(argvars->vval.v_list, li)
4717 {
4718 if (li->li_tv.v_type != VAR_DICT)
4719 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004720 emsg(_(e_dictionary_required));
Yegappan Lakshmanand1a8d652021-11-03 21:56:45 +00004721 return;
4722 }
4723
4724 dict = li->li_tv.vval.v_dict;
4725 if (!hlg_add_or_update(dict))
4726 return;
4727 }
4728
4729 rettv->vval.v_number = 0;
4730}
4731#endif