xref: /vim-8.2.3635/src/syntax.c (revision c01140a1)
1 /* vi:set ts=8 sts=4 sw=4:
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 /*
11  * syntax.c: code for syntax highlighting
12  */
13 
14 #include "vim.h"
15 
16 /*
17  * Structure that stores information about a highlight group.
18  * The ID of a highlight group is also called group ID.  It is the index in
19  * the highlight_ga array PLUS ONE.
20  */
21 struct hl_group
22 {
23     char_u	*sg_name;	/* highlight group name */
24     char_u	*sg_name_u;	/* uppercase of sg_name */
25 /* for normal terminals */
26     int		sg_term;	/* "term=" highlighting attributes */
27     char_u	*sg_start;	/* terminal string for start highl */
28     char_u	*sg_stop;	/* terminal string for stop highl */
29     int		sg_term_attr;	/* Screen attr for term mode */
30 /* for color terminals */
31     int		sg_cterm;	/* "cterm=" highlighting attr */
32     int		sg_cterm_bold;	/* bold attr was set for light color */
33     int		sg_cterm_fg;	/* terminal fg color number + 1 */
34     int		sg_cterm_bg;	/* terminal bg color number + 1 */
35     int		sg_cterm_attr;	/* Screen attr for color term mode */
36 #ifdef FEAT_GUI
37 /* for when using the GUI */
38     int		sg_gui;		/* "gui=" highlighting attributes */
39     guicolor_T	sg_gui_fg;	/* GUI foreground color handle */
40     char_u	*sg_gui_fg_name;/* GUI foreground color name */
41     guicolor_T	sg_gui_bg;	/* GUI background color handle */
42     char_u	*sg_gui_bg_name;/* GUI background color name */
43     guicolor_T	sg_gui_sp;	/* GUI special color handle */
44     char_u	*sg_gui_sp_name;/* GUI special color name */
45     GuiFont	sg_font;	/* GUI font handle */
46 #ifdef FEAT_XFONTSET
47     GuiFontset	sg_fontset;	/* GUI fontset handle */
48 #endif
49     char_u	*sg_font_name;  /* GUI font or fontset name */
50     int		sg_gui_attr;    /* Screen attr for GUI mode */
51 #endif
52     int		sg_link;	/* link to this highlight group ID */
53     int		sg_set;		/* combination of SG_* flags */
54 #ifdef FEAT_EVAL
55     scid_T	sg_scriptID;	/* script in which the group was last set */
56 #endif
57 };
58 
59 #define SG_TERM		1	/* term has been set */
60 #define SG_CTERM	2	/* cterm has been set */
61 #define SG_GUI		4	/* gui has been set */
62 #define SG_LINK		8	/* link has been set */
63 
64 static garray_T highlight_ga;	/* highlight groups for 'highlight' option */
65 
66 #define HL_TABLE() ((struct hl_group *)((highlight_ga.ga_data)))
67 
68 #ifdef FEAT_CMDL_COMPL
69 static int include_default = FALSE;	/* include "default" for expansion */
70 static int include_link = FALSE;	/* include "link" for expansion */
71 #endif
72 
73 /*
74  * The "term", "cterm" and "gui" arguments can be any combination of the
75  * following names, separated by commas (but no spaces!).
76  */
77 static char *(hl_name_table[]) =
78     {"bold", "standout", "underline", "undercurl",
79 				      "italic", "reverse", "inverse", "NONE"};
80 static int hl_attr_table[] =
81     {HL_BOLD, HL_STANDOUT, HL_UNDERLINE, HL_UNDERCURL, HL_ITALIC, HL_INVERSE, HL_INVERSE, 0};
82 
83 static int get_attr_entry  __ARGS((garray_T *table, attrentry_T *aep));
84 static void syn_unadd_group __ARGS((void));
85 static void set_hl_attr __ARGS((int idx));
86 static void highlight_list_one __ARGS((int id));
87 static int highlight_list_arg __ARGS((int id, int didh, int type, int iarg, char_u *sarg, char *name));
88 static int syn_add_group __ARGS((char_u *name));
89 static int syn_list_header __ARGS((int did_header, int outlen, int id));
90 static int hl_has_settings __ARGS((int idx, int check_link));
91 static void highlight_clear __ARGS((int idx));
92 
93 #ifdef FEAT_GUI
94 static void gui_do_one_color __ARGS((int idx, int do_menu, int do_tooltip));
95 static int  set_group_colors __ARGS((char_u *name, guicolor_T *fgp, guicolor_T *bgp, int do_menu, int use_norm, int do_tooltip));
96 static guicolor_T color_name2handle __ARGS((char_u *name));
97 static GuiFont font_name2handle __ARGS((char_u *name));
98 # ifdef FEAT_XFONTSET
99 static GuiFontset fontset_name2handle __ARGS((char_u *name, int fixed_width));
100 # endif
101 static void hl_do_font __ARGS((int idx, char_u *arg, int do_normal, int do_menu, int do_tooltip));
102 #endif
103 
104 /*
105  * An attribute number is the index in attr_table plus ATTR_OFF.
106  */
107 #define ATTR_OFF (HL_ALL + 1)
108 
109 #if defined(FEAT_SYN_HL) || defined(PROTO)
110 
111 #define SYN_NAMELEN	50		/* maximum length of a syntax name */
112 
113 /* different types of offsets that are possible */
114 #define SPO_MS_OFF	0	/* match  start offset */
115 #define SPO_ME_OFF	1	/* match  end	offset */
116 #define SPO_HS_OFF	2	/* highl. start offset */
117 #define SPO_HE_OFF	3	/* highl. end	offset */
118 #define SPO_RS_OFF	4	/* region start offset */
119 #define SPO_RE_OFF	5	/* region end	offset */
120 #define SPO_LC_OFF	6	/* leading context offset */
121 #define SPO_COUNT	7
122 
123 static char *(spo_name_tab[SPO_COUNT]) =
124 	    {"ms=", "me=", "hs=", "he=", "rs=", "re=", "lc="};
125 
126 /*
127  * The patterns that are being searched for are stored in a syn_pattern.
128  * A match item consists of one pattern.
129  * A start/end item consists of n start patterns and m end patterns.
130  * A start/skip/end item consists of n start patterns, one skip pattern and m
131  * end patterns.
132  * For the latter two, the patterns are always consecutive: start-skip-end.
133  *
134  * A character offset can be given for the matched text (_m_start and _m_end)
135  * and for the actually highlighted text (_h_start and _h_end).
136  */
137 typedef struct syn_pattern
138 {
139     char	 sp_type;		/* see SPTYPE_ defines below */
140     char	 sp_syncing;		/* this item used for syncing */
141     short	 sp_flags;		/* see HL_ defines below */
142     struct sp_syn sp_syn;		/* struct passed to in_id_list() */
143     short	 sp_syn_match_id;	/* highlight group ID of pattern */
144     char_u	*sp_pattern;		/* regexp to match, pattern */
145     regprog_T	*sp_prog;		/* regexp to match, program */
146     int		 sp_ic;			/* ignore-case flag for sp_prog */
147     short	 sp_off_flags;		/* see below */
148     int		 sp_offsets[SPO_COUNT];	/* offsets */
149     short	*sp_cont_list;		/* cont. group IDs, if non-zero */
150     short	*sp_next_list;		/* next group IDs, if non-zero */
151     int		 sp_sync_idx;		/* sync item index (syncing only) */
152     int		 sp_line_id;		/* ID of last line where tried */
153     int		 sp_startcol;		/* next match in sp_line_id line */
154 } synpat_T;
155 
156 /* The sp_off_flags are computed like this:
157  * offset from the start of the matched text: (1 << SPO_XX_OFF)
158  * offset from the end	 of the matched text: (1 << (SPO_XX_OFF + SPO_COUNT))
159  * When both are present, only one is used.
160  */
161 
162 #define SPTYPE_MATCH	1	/* match keyword with this group ID */
163 #define SPTYPE_START	2	/* match a regexp, start of item */
164 #define SPTYPE_END	3	/* match a regexp, end of item */
165 #define SPTYPE_SKIP	4	/* match a regexp, skip within item */
166 
167 #define HL_CONTAINED	0x01	/* not used on toplevel */
168 #define HL_TRANSP	0x02	/* has no highlighting	*/
169 #define HL_ONELINE	0x04	/* match within one line only */
170 #define HL_HAS_EOL	0x08	/* end pattern that matches with $ */
171 #define HL_SYNC_HERE	0x10	/* sync point after this item (syncing only) */
172 #define HL_SYNC_THERE	0x20	/* sync point at current line (syncing only) */
173 #define HL_MATCH	0x40	/* use match ID instead of item ID */
174 #define HL_SKIPNL	0x80	/* nextgroup can skip newlines */
175 #define HL_SKIPWHITE	0x100	/* nextgroup can skip white space */
176 #define HL_SKIPEMPTY	0x200	/* nextgroup can skip empty lines */
177 #define HL_KEEPEND	0x400	/* end match always kept */
178 #define HL_EXCLUDENL	0x800	/* exclude NL from match */
179 #define HL_DISPLAY	0x1000	/* only used for displaying, not syncing */
180 #define HL_FOLD		0x2000	/* define fold */
181 #define HL_EXTEND	0x4000	/* ignore a keepend */
182 /* These don't fit in a short, thus can't be used for syntax items, only for
183  * si_flags and bs_flags. */
184 #define HL_MATCHCONT	0x8000	/* match continued from previous line */
185 #define HL_TRANS_CONT	0x10000L /* transparent item without contains arg */
186 
187 #define SYN_ITEMS(buf)	((synpat_T *)((buf)->b_syn_patterns.ga_data))
188 
189 #define NONE_IDX	-2	/* value of sp_sync_idx for "NONE" */
190 
191 /*
192  * Flags for b_syn_sync_flags:
193  */
194 #define SF_CCOMMENT	0x01	/* sync on a C-style comment */
195 #define SF_MATCH	0x02	/* sync by matching a pattern */
196 
197 #define SYN_STATE_P(ssp)    ((bufstate_T *)((ssp)->ga_data))
198 
199 #define MAXKEYWLEN	80	    /* maximum length of a keyword */
200 
201 /*
202  * The attributes of the syntax item that has been recognized.
203  */
204 static int current_attr = 0;	    /* attr of current syntax word */
205 #ifdef FEAT_EVAL
206 static int current_id = 0;	    /* ID of current char for syn_get_id() */
207 static int current_trans_id = 0;    /* idem, transparancy removed */
208 #endif
209 
210 typedef struct syn_cluster_S
211 {
212     char_u	    *scl_name;	    /* syntax cluster name */
213     char_u	    *scl_name_u;    /* uppercase of scl_name */
214     short	    *scl_list;	    /* IDs in this syntax cluster */
215 } syn_cluster_T;
216 
217 /*
218  * Methods of combining two clusters
219  */
220 #define CLUSTER_REPLACE	    1	/* replace first list with second */
221 #define CLUSTER_ADD	    2	/* add second list to first */
222 #define CLUSTER_SUBTRACT    3	/* subtract second list from first */
223 
224 #define SYN_CLSTR(buf)	((syn_cluster_T *)((buf)->b_syn_clusters.ga_data))
225 
226 /*
227  * Syntax group IDs have different types:
228  *     0 -  9999  normal syntax groups
229  * 10000 - 14999  ALLBUT indicator (current_syn_inc_tag added)
230  * 15000 - 19999  TOP indicator (current_syn_inc_tag added)
231  * 20000 - 24999  CONTAINED indicator (current_syn_inc_tag added)
232  * >= 25000	  cluster IDs (subtract SYNID_CLUSTER for the cluster ID)
233  */
234 #define SYNID_ALLBUT	10000	    /* syntax group ID for contains=ALLBUT */
235 #define SYNID_TOP	15000	    /* syntax group ID for contains=TOP */
236 #define SYNID_CONTAINED	20000	    /* syntax group ID for contains=CONTAINED */
237 #define SYNID_CLUSTER	25000	    /* first syntax group ID for clusters */
238 
239 /*
240  * Annoying Hack(TM):  ":syn include" needs this pointer to pass to
241  * expand_filename().  Most of the other syntax commands don't need it, so
242  * instead of passing it to them, we stow it here.
243  */
244 static char_u **syn_cmdlinep;
245 
246 /*
247  * Another Annoying Hack(TM):  To prevent rules from other ":syn include"'d
248  * files from from leaking into ALLBUT lists, we assign a unique ID to the
249  * rules in each ":syn include"'d file.
250  */
251 static int current_syn_inc_tag = 0;
252 static int running_syn_inc_tag = 0;
253 
254 /*
255  * In a hashtable item "hi_key" points to "keyword" in a keyentry.
256  * This avoids adding a pointer to the hashtable item.
257  * KE2HIKEY() converts a var pointer to a hashitem key pointer.
258  * HIKEY2KE() converts a hashitem key pointer to a var pointer.
259  * HI2KE() converts a hashitem pointer to a var pointer.
260  */
261 static keyentry_T dumkey;
262 #define KE2HIKEY(kp)  ((kp)->keyword)
263 #define HIKEY2KE(p)   ((keyentry_T *)((p) - (dumkey.keyword - (char_u *)&dumkey)))
264 #define HI2KE(hi)      HIKEY2KE((hi)->hi_key)
265 
266 /*
267  * To reduce the time spent in keepend(), remember at which level in the state
268  * stack the first item with "keepend" is present.  When "-1", there is no
269  * "keepend" on the stack.
270  */
271 static int keepend_level = -1;
272 
273 /*
274  * For the current state we need to remember more than just the idx.
275  * When si_m_endpos.lnum is 0, the items other than si_idx are unknown.
276  * (The end positions have the column number of the next char)
277  */
278 typedef struct state_item
279 {
280     int		si_idx;			/* index of syntax pattern */
281     int		si_id;			/* highlight group ID for keywords */
282     int		si_trans_id;		/* idem, transparancy removed */
283     int		si_m_lnum;		/* lnum of the match */
284     int		si_m_startcol;		/* starting column of the match */
285     lpos_T	si_m_endpos;		/* just after end posn of the match */
286     lpos_T	si_h_startpos;		/* start position of the highlighting */
287     lpos_T	si_h_endpos;		/* end position of the highlighting */
288     lpos_T	si_eoe_pos;		/* end position of end pattern */
289     int		si_end_idx;		/* group ID for end pattern or zero */
290     int		si_ends;		/* if match ends before si_m_endpos */
291     int		si_attr;		/* attributes in this state */
292     long	si_flags;		/* HL_HAS_EOL flag in this state, and
293 					 * HL_SKIP* for si_next_list */
294     short	*si_cont_list;		/* list of contained groups */
295     short	*si_next_list;		/* nextgroup IDs after this item ends */
296     reg_extmatch_T *si_extmatch;	/* \z(...\) matches from start
297 					 * pattern */
298 } stateitem_T;
299 
300 #define KEYWORD_IDX	-1	    /* value of si_idx for keywords */
301 #define ID_LIST_ALL	(short *)-1 /* valid of si_cont_list for containing all
302 				       but contained groups */
303 
304 /*
305  * Struct to reduce the number of arguments to get_syn_options(), it's used
306  * very often.
307  */
308 typedef struct
309 {
310     int		flags;		/* flags for contained and transpartent */
311     int		keyword;	/* TRUE for ":syn keyword" */
312     int		*sync_idx;	/* syntax item for "grouphere" argument, NULL
313 				   if not allowed */
314     char	has_cont_list;	/* TRUE if "cont_list" can be used */
315     short	*cont_list;	/* group IDs for "contains" argument */
316     short	*cont_in_list;	/* group IDs for "containedin" argument */
317     short	*next_list;	/* group IDs for "nextgroup" argument */
318 } syn_opt_arg_T;
319 
320 /*
321  * The next possible match in the current line for any pattern is remembered,
322  * to avoid having to try for a match in each column.
323  * If next_match_idx == -1, not tried (in this line) yet.
324  * If next_match_col == MAXCOL, no match found in this line.
325  * (All end positions have the column of the char after the end)
326  */
327 static int next_match_col;		/* column for start of next match */
328 static lpos_T next_match_m_endpos;	/* position for end of next match */
329 static lpos_T next_match_h_startpos;  /* pos. for highl. start of next match */
330 static lpos_T next_match_h_endpos;	/* pos. for highl. end of next match */
331 static int next_match_idx;		/* index of matched item */
332 static long next_match_flags;		/* flags for next match */
333 static lpos_T next_match_eos_pos;	/* end of start pattn (start region) */
334 static lpos_T next_match_eoe_pos;	/* pos. for end of end pattern */
335 static int next_match_end_idx;		/* ID of group for end pattn or zero */
336 static reg_extmatch_T *next_match_extmatch = NULL;
337 
338 /*
339  * A state stack is an array of integers or stateitem_T, stored in a
340  * garray_T.  A state stack is invalid if it's itemsize entry is zero.
341  */
342 #define INVALID_STATE(ssp)  ((ssp)->ga_itemsize == 0)
343 #define VALID_STATE(ssp)    ((ssp)->ga_itemsize != 0)
344 
345 /*
346  * The current state (within the line) of the recognition engine.
347  * When current_state.ga_itemsize is 0 the current state is invalid.
348  */
349 static win_T	*syn_win;		/* current window for highlighting */
350 static buf_T	*syn_buf;		/* current buffer for highlighting */
351 static linenr_T current_lnum = 0;	/* lnum of current state */
352 static colnr_T	current_col = 0;	/* column of current state */
353 static int	current_state_stored = 0; /* TRUE if stored current state
354 					   * after setting current_finished */
355 static int	current_finished = 0;	/* current line has been finished */
356 static garray_T current_state		/* current stack of state_items */
357 		= {0, 0, 0, 0, NULL};
358 static short	*current_next_list = NULL; /* when non-zero, nextgroup list */
359 static int	current_next_flags = 0; /* flags for current_next_list */
360 static int	current_line_id = 0;	/* unique number for current line */
361 
362 #define CUR_STATE(idx)	((stateitem_T *)(current_state.ga_data))[idx]
363 
364 static void syn_sync __ARGS((win_T *wp, linenr_T lnum, synstate_T *last_valid));
365 static int syn_match_linecont __ARGS((linenr_T lnum));
366 static void syn_start_line __ARGS((void));
367 static void syn_update_ends __ARGS((int startofline));
368 static void syn_stack_alloc __ARGS((void));
369 static int syn_stack_cleanup __ARGS((void));
370 static void syn_stack_free_entry __ARGS((buf_T *buf, synstate_T *p));
371 static synstate_T *syn_stack_find_entry __ARGS((linenr_T lnum));
372 static synstate_T *store_current_state __ARGS((synstate_T *sp));
373 static void load_current_state __ARGS((synstate_T *from));
374 static void invalidate_current_state __ARGS((void));
375 static int syn_stack_equal __ARGS((synstate_T *sp));
376 static void validate_current_state __ARGS((void));
377 static int syn_finish_line __ARGS((int syncing));
378 static int syn_current_attr __ARGS((int syncing, int displaying, int *can_spell));
379 static int did_match_already __ARGS((int idx, garray_T *gap));
380 static stateitem_T *push_next_match __ARGS((stateitem_T *cur_si));
381 static void check_state_ends __ARGS((void));
382 static void update_si_attr __ARGS((int idx));
383 static void check_keepend __ARGS((void));
384 static void update_si_end __ARGS((stateitem_T *sip, int startcol, int force));
385 static short *copy_id_list __ARGS((short *list));
386 static int in_id_list __ARGS((stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained));
387 static int push_current_state __ARGS((int idx));
388 static void pop_current_state __ARGS((void));
389 
390 static void find_endpos __ARGS((int idx, lpos_T *startpos, lpos_T *m_endpos, lpos_T *hl_endpos, long *flagsp, lpos_T *end_endpos, int *end_idx, reg_extmatch_T *start_ext));
391 static void clear_syn_state __ARGS((synstate_T *p));
392 static void clear_current_state __ARGS((void));
393 
394 static void limit_pos __ARGS((lpos_T *pos, lpos_T *limit));
395 static void limit_pos_zero __ARGS((lpos_T *pos, lpos_T *limit));
396 static void syn_add_end_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
397 static void syn_add_start_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
398 static char_u *syn_getcurline __ARGS((void));
399 static int syn_regexec __ARGS((regmmatch_T *rmp, linenr_T lnum, colnr_T col));
400 static int check_keyword_id __ARGS((char_u *line, int startcol, int *endcol, long *flags, short **next_list, stateitem_T *cur_si));
401 static void syn_cmd_case __ARGS((exarg_T *eap, int syncing));
402 static void syn_cmd_spell __ARGS((exarg_T *eap, int syncing));
403 static void syntax_sync_clear __ARGS((void));
404 static void syn_remove_pattern __ARGS((buf_T *buf, int idx));
405 static void syn_clear_pattern __ARGS((buf_T *buf, int i));
406 static void syn_clear_cluster __ARGS((buf_T *buf, int i));
407 static void syn_cmd_clear __ARGS((exarg_T *eap, int syncing));
408 static void syn_clear_one __ARGS((int id, int syncing));
409 static void syn_cmd_on __ARGS((exarg_T *eap, int syncing));
410 static void syn_cmd_enable __ARGS((exarg_T *eap, int syncing));
411 static void syn_cmd_reset __ARGS((exarg_T *eap, int syncing));
412 static void syn_cmd_manual __ARGS((exarg_T *eap, int syncing));
413 static void syn_cmd_off __ARGS((exarg_T *eap, int syncing));
414 static void syn_cmd_onoff __ARGS((exarg_T *eap, char *name));
415 static void syn_cmd_list __ARGS((exarg_T *eap, int syncing));
416 static void syn_lines_msg __ARGS((void));
417 static void syn_match_msg __ARGS((void));
418 static void syn_list_one __ARGS((int id, int syncing, int link_only));
419 static void syn_list_cluster __ARGS((int id));
420 static void put_id_list __ARGS((char_u *name, short *list, int attr));
421 static void put_pattern __ARGS((char *s, int c, synpat_T *spp, int attr));
422 static int syn_list_keywords __ARGS((int id, hashtab_T *ht, int did_header, int attr));
423 static void syn_clear_keyword __ARGS((int id, hashtab_T *ht));
424 static void clear_keywtab __ARGS((hashtab_T *ht));
425 static void add_keyword __ARGS((char_u *name, int id, int flags, short *cont_in_list, short *next_list));
426 static char_u *get_group_name __ARGS((char_u *arg, char_u **name_end));
427 static char_u *get_syn_options __ARGS((char_u *arg, syn_opt_arg_T *opt));
428 static void syn_cmd_include __ARGS((exarg_T *eap, int syncing));
429 static void syn_cmd_keyword __ARGS((exarg_T *eap, int syncing));
430 static void syn_cmd_match __ARGS((exarg_T *eap, int syncing));
431 static void syn_cmd_region __ARGS((exarg_T *eap, int syncing));
432 #ifdef __BORLANDC__
433 static int _RTLENTRYF syn_compare_stub __ARGS((const void *v1, const void *v2));
434 #else
435 static int syn_compare_stub __ARGS((const void *v1, const void *v2));
436 #endif
437 static void syn_cmd_cluster __ARGS((exarg_T *eap, int syncing));
438 static int syn_scl_name2id __ARGS((char_u *name));
439 static int syn_scl_namen2id __ARGS((char_u *linep, int len));
440 static int syn_check_cluster __ARGS((char_u *pp, int len));
441 static int syn_add_cluster __ARGS((char_u *name));
442 static void init_syn_patterns __ARGS((void));
443 static char_u *get_syn_pattern __ARGS((char_u *arg, synpat_T *ci));
444 static void syn_cmd_sync __ARGS((exarg_T *eap, int syncing));
445 static int get_id_list __ARGS((char_u **arg, int keylen, short **list));
446 static void syn_combine_list __ARGS((short **clstr1, short **clstr2, int list_op));
447 static void syn_incl_toplevel __ARGS((int id, int *flagsp));
448 
449 /*
450  * Start the syntax recognition for a line.  This function is normally called
451  * from the screen updating, once for each displayed line.
452  * The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
453  * it.	Careful: curbuf and curwin are likely to point to another buffer and
454  * window.
455  */
456     void
457 syntax_start(wp, lnum)
458     win_T	*wp;
459     linenr_T	lnum;
460 {
461     synstate_T	*p;
462     synstate_T	*last_valid = NULL;
463     synstate_T	*last_min_valid = NULL;
464     synstate_T	*sp, *prev;
465     linenr_T	parsed_lnum;
466     linenr_T	first_stored;
467     int		dist;
468     static int	changedtick = 0;	/* remember the last change ID */
469 
470     /*
471      * After switching buffers, invalidate current_state.
472      * Also do this when a change was made, the current state may be invalid
473      * then.
474      */
475     if (syn_buf != wp->w_buffer || changedtick != syn_buf->b_changedtick)
476     {
477 	invalidate_current_state();
478 	syn_buf = wp->w_buffer;
479     }
480     changedtick = syn_buf->b_changedtick;
481     syn_win = wp;
482 
483     /*
484      * Allocate syntax stack when needed.
485      */
486     syn_stack_alloc();
487     if (syn_buf->b_sst_array == NULL)
488 	return;		/* out of memory */
489     syn_buf->b_sst_lasttick = display_tick;
490 
491     /*
492      * If the state of the end of the previous line is useful, store it.
493      */
494     if (VALID_STATE(&current_state)
495 	    && current_lnum < lnum
496 	    && current_lnum < syn_buf->b_ml.ml_line_count)
497     {
498 	(void)syn_finish_line(FALSE);
499 	if (!current_state_stored)
500 	{
501 	    ++current_lnum;
502 	    (void)store_current_state(NULL);
503 	}
504 
505 	/*
506 	 * If the current_lnum is now the same as "lnum", keep the current
507 	 * state (this happens very often!).  Otherwise invalidate
508 	 * current_state and figure it out below.
509 	 */
510 	if (current_lnum != lnum)
511 	    invalidate_current_state();
512     }
513     else
514 	invalidate_current_state();
515 
516     /*
517      * Try to synchronize from a saved state in b_sst_array[].
518      * Only do this if lnum is not before and not to far beyond a saved state.
519      */
520     if (INVALID_STATE(&current_state) && syn_buf->b_sst_array != NULL)
521     {
522 	/* Find last valid saved state before start_lnum. */
523 	for (p = syn_buf->b_sst_first; p != NULL; p = p->sst_next)
524 	{
525 	    if (p->sst_lnum > lnum)
526 		break;
527 	    if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
528 	    {
529 		last_valid = p;
530 		if (p->sst_lnum >= lnum - syn_buf->b_syn_sync_minlines)
531 		    last_min_valid = p;
532 	    }
533 	}
534 	if (last_min_valid != NULL)
535 	    load_current_state(last_min_valid);
536     }
537 
538     /*
539      * If "lnum" is before or far beyond a line with a saved state, need to
540      * re-synchronize.
541      */
542     if (INVALID_STATE(&current_state))
543     {
544 	syn_sync(wp, lnum, last_valid);
545 	first_stored = current_lnum + syn_buf->b_syn_sync_minlines;
546     }
547     else
548 	first_stored = current_lnum;
549 
550     /*
551      * Advance from the sync point or saved state until the current line.
552      * Save some entries for syncing with later on.
553      */
554     dist = syn_buf->b_ml.ml_line_count / (syn_buf->b_sst_len - Rows) + 1;
555     prev = syn_stack_find_entry(current_lnum);
556     while (current_lnum < lnum)
557     {
558 	syn_start_line();
559 	(void)syn_finish_line(FALSE);
560 	++current_lnum;
561 
562 	/* If we parsed at least "minlines" lines or started at a valid
563 	 * state, the current state is considered valid. */
564 	if (current_lnum >= first_stored)
565 	{
566 	    /* Check if the saved state entry is for the current line and is
567 	     * equal to the current state.  If so, then validate all saved
568 	     * states that depended on a change before the parsed line. */
569 	    if (prev == NULL)
570 		sp = syn_buf->b_sst_first;
571 	    else
572 		sp = prev->sst_next;
573 	    if (sp != NULL
574 		    && sp->sst_lnum == current_lnum
575 		    && syn_stack_equal(sp))
576 	    {
577 		parsed_lnum = current_lnum;
578 		prev = sp;
579 		while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
580 		{
581 		    if (sp->sst_lnum <= lnum)
582 			/* valid state before desired line, use this one */
583 			prev = sp;
584 		    else if (sp->sst_change_lnum == 0)
585 			/* past saved states depending on change, break here. */
586 			break;
587 		    sp->sst_change_lnum = 0;
588 		    sp = sp->sst_next;
589 		}
590 		load_current_state(prev);
591 	    }
592 	    /* Store the state at this line when it's the first one, the line
593 	     * where we start parsing, or some distance from the previously
594 	     * saved state.  But only when parsed at least 'minlines'. */
595 	    else if (prev == NULL
596 			|| current_lnum == lnum
597 			|| current_lnum >= prev->sst_lnum + dist)
598 		prev = store_current_state(prev);
599 	}
600 
601 	/* This can take a long time: break when CTRL-C pressed.  The current
602 	 * state will be wrong then. */
603 	line_breakcheck();
604 	if (got_int)
605 	{
606 	    current_lnum = lnum;
607 	    break;
608 	}
609     }
610 
611     syn_start_line();
612 }
613 
614 /*
615  * We cannot simply discard growarrays full of state_items or buf_states; we
616  * have to manually release their extmatch pointers first.
617  */
618     static void
619 clear_syn_state(p)
620     synstate_T *p;
621 {
622     int		i;
623     garray_T	*gap;
624 
625     if (p->sst_stacksize > SST_FIX_STATES)
626     {
627 	gap = &(p->sst_union.sst_ga);
628 	for (i = 0; i < gap->ga_len; i++)
629 	    unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
630 	ga_clear(gap);
631     }
632     else
633     {
634 	for (i = 0; i < p->sst_stacksize; i++)
635 	    unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
636     }
637 }
638 
639 /*
640  * Cleanup the current_state stack.
641  */
642     static void
643 clear_current_state()
644 {
645     int		i;
646     stateitem_T	*sip;
647 
648     sip = (stateitem_T *)(current_state.ga_data);
649     for (i = 0; i < current_state.ga_len; i++)
650 	unref_extmatch(sip[i].si_extmatch);
651     ga_clear(&current_state);
652 }
653 
654 /*
655  * Try to find a synchronisation point for line "lnum".
656  *
657  * This sets current_lnum and the current state.  One of three methods is
658  * used:
659  * 1. Search backwards for the end of a C-comment.
660  * 2. Search backwards for given sync patterns.
661  * 3. Simply start on a given number of lines above "lnum".
662  */
663     static void
664 syn_sync(wp, start_lnum, last_valid)
665     win_T	*wp;
666     linenr_T	start_lnum;
667     synstate_T	*last_valid;
668 {
669     buf_T	*curbuf_save;
670     win_T	*curwin_save;
671     pos_T	cursor_save;
672     int		idx;
673     linenr_T	lnum;
674     linenr_T	end_lnum;
675     linenr_T	break_lnum;
676     int		had_sync_point;
677     stateitem_T	*cur_si;
678     synpat_T	*spp;
679     char_u	*line;
680     int		found_flags = 0;
681     int		found_match_idx = 0;
682     linenr_T	found_current_lnum = 0;
683     int		found_current_col= 0;
684     lpos_T	found_m_endpos;
685     colnr_T	prev_current_col;
686 
687     /*
688      * Clear any current state that might be hanging around.
689      */
690     invalidate_current_state();
691 
692     /*
693      * Start at least "minlines" back.  Default starting point for parsing is
694      * there.
695      * Start further back, to avoid that scrolling backwards will result in
696      * resyncing for every line.  Now it resyncs only one out of N lines,
697      * where N is minlines * 1.5, or minlines * 2 if minlines is small.
698      * Watch out for overflow when minlines is MAXLNUM.
699      */
700     if (syn_buf->b_syn_sync_minlines > start_lnum)
701 	start_lnum = 1;
702     else
703     {
704 	if (syn_buf->b_syn_sync_minlines == 1)
705 	    lnum = 1;
706 	else if (syn_buf->b_syn_sync_minlines < 10)
707 	    lnum = syn_buf->b_syn_sync_minlines * 2;
708 	else
709 	    lnum = syn_buf->b_syn_sync_minlines * 3 / 2;
710 	if (syn_buf->b_syn_sync_maxlines != 0
711 				       && lnum > syn_buf->b_syn_sync_maxlines)
712 	    lnum = syn_buf->b_syn_sync_maxlines;
713 	if (lnum >= start_lnum)
714 	    start_lnum = 1;
715 	else
716 	    start_lnum -= lnum;
717     }
718     current_lnum = start_lnum;
719 
720     /*
721      * 1. Search backwards for the end of a C-style comment.
722      */
723     if (syn_buf->b_syn_sync_flags & SF_CCOMMENT)
724     {
725 	/* Need to make syn_buf the current buffer for a moment, to be able to
726 	 * use find_start_comment(). */
727 	curwin_save = curwin;
728 	curwin = wp;
729 	curbuf_save = curbuf;
730 	curbuf = syn_buf;
731 
732 	/*
733 	 * Skip lines that end in a backslash.
734 	 */
735 	for ( ; start_lnum > 1; --start_lnum)
736 	{
737 	    line = ml_get(start_lnum - 1);
738 	    if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
739 		break;
740 	}
741 	current_lnum = start_lnum;
742 
743 	/* set cursor to start of search */
744 	cursor_save = wp->w_cursor;
745 	wp->w_cursor.lnum = start_lnum;
746 	wp->w_cursor.col = 0;
747 
748 	/*
749 	 * If the line is inside a comment, need to find the syntax item that
750 	 * defines the comment.
751 	 * Restrict the search for the end of a comment to b_syn_sync_maxlines.
752 	 */
753 	if (find_start_comment((int)syn_buf->b_syn_sync_maxlines) != NULL)
754 	{
755 	    for (idx = syn_buf->b_syn_patterns.ga_len; --idx >= 0; )
756 		if (SYN_ITEMS(syn_buf)[idx].sp_syn.id == syn_buf->b_syn_sync_id
757 			&& SYN_ITEMS(syn_buf)[idx].sp_type == SPTYPE_START)
758 		{
759 		    validate_current_state();
760 		    if (push_current_state(idx) == OK)
761 			update_si_attr(current_state.ga_len - 1);
762 		    break;
763 		}
764 	}
765 
766 	/* restore cursor and buffer */
767 	wp->w_cursor = cursor_save;
768 	curwin = curwin_save;
769 	curbuf = curbuf_save;
770     }
771 
772     /*
773      * 2. Search backwards for given sync patterns.
774      */
775     else if (syn_buf->b_syn_sync_flags & SF_MATCH)
776     {
777 	if (syn_buf->b_syn_sync_maxlines != 0
778 				 && start_lnum > syn_buf->b_syn_sync_maxlines)
779 	    break_lnum = start_lnum - syn_buf->b_syn_sync_maxlines;
780 	else
781 	    break_lnum = 0;
782 
783 	found_m_endpos.lnum = 0;
784 	found_m_endpos.col = 0;
785 	end_lnum = start_lnum;
786 	lnum = start_lnum;
787 	while (--lnum > break_lnum)
788 	{
789 	    /* This can take a long time: break when CTRL-C pressed. */
790 	    line_breakcheck();
791 	    if (got_int)
792 	    {
793 		invalidate_current_state();
794 		current_lnum = start_lnum;
795 		break;
796 	    }
797 
798 	    /* Check if we have run into a valid saved state stack now. */
799 	    if (last_valid != NULL && lnum == last_valid->sst_lnum)
800 	    {
801 		load_current_state(last_valid);
802 		break;
803 	    }
804 
805 	    /*
806 	     * Check if the previous line has the line-continuation pattern.
807 	     */
808 	    if (lnum > 1 && syn_match_linecont(lnum - 1))
809 		continue;
810 
811 	    /*
812 	     * Start with nothing on the state stack
813 	     */
814 	    validate_current_state();
815 
816 	    for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
817 	    {
818 		syn_start_line();
819 		for (;;)
820 		{
821 		    had_sync_point = syn_finish_line(TRUE);
822 		    /*
823 		     * When a sync point has been found, remember where, and
824 		     * continue to look for another one, further on in the line.
825 		     */
826 		    if (had_sync_point && current_state.ga_len)
827 		    {
828 			cur_si = &CUR_STATE(current_state.ga_len - 1);
829 			if (cur_si->si_m_endpos.lnum > start_lnum)
830 			{
831 			    /* ignore match that goes to after where started */
832 			    current_lnum = end_lnum;
833 			    break;
834 			}
835 			spp = &(SYN_ITEMS(syn_buf)[cur_si->si_idx]);
836 			found_flags = spp->sp_flags;
837 			found_match_idx = spp->sp_sync_idx;
838 			found_current_lnum = current_lnum;
839 			found_current_col = current_col;
840 			found_m_endpos = cur_si->si_m_endpos;
841 			/*
842 			 * Continue after the match (be aware of a zero-length
843 			 * match).
844 			 */
845 			if (found_m_endpos.lnum > current_lnum)
846 			{
847 			    current_lnum = found_m_endpos.lnum;
848 			    current_col = found_m_endpos.col;
849 			    if (current_lnum >= end_lnum)
850 				break;
851 			}
852 			else if (found_m_endpos.col > current_col)
853 			    current_col = found_m_endpos.col;
854 			else
855 			    ++current_col;
856 
857 			/* syn_current_attr() will have skipped the check for
858 			 * an item that ends here, need to do that now.  Be
859 			 * careful not to go past the NUL. */
860 			prev_current_col = current_col;
861 			if (syn_getcurline()[current_col] != NUL)
862 			    ++current_col;
863 			check_state_ends();
864 			current_col = prev_current_col;
865 		    }
866 		    else
867 			break;
868 		}
869 	    }
870 
871 	    /*
872 	     * If a sync point was encountered, break here.
873 	     */
874 	    if (found_flags)
875 	    {
876 		/*
877 		 * Put the item that was specified by the sync point on the
878 		 * state stack.  If there was no item specified, make the
879 		 * state stack empty.
880 		 */
881 		clear_current_state();
882 		if (found_match_idx >= 0
883 			&& push_current_state(found_match_idx) == OK)
884 		    update_si_attr(current_state.ga_len - 1);
885 
886 		/*
887 		 * When using "grouphere", continue from the sync point
888 		 * match, until the end of the line.  Parsing starts at
889 		 * the next line.
890 		 * For "groupthere" the parsing starts at start_lnum.
891 		 */
892 		if (found_flags & HL_SYNC_HERE)
893 		{
894 		    if (current_state.ga_len)
895 		    {
896 			cur_si = &CUR_STATE(current_state.ga_len - 1);
897 			cur_si->si_h_startpos.lnum = found_current_lnum;
898 			cur_si->si_h_startpos.col = found_current_col;
899 			update_si_end(cur_si, (int)current_col, TRUE);
900 			check_keepend();
901 		    }
902 		    current_col = found_m_endpos.col;
903 		    current_lnum = found_m_endpos.lnum;
904 		    (void)syn_finish_line(FALSE);
905 		    ++current_lnum;
906 		}
907 		else
908 		    current_lnum = start_lnum;
909 
910 		break;
911 	    }
912 
913 	    end_lnum = lnum;
914 	    invalidate_current_state();
915 	}
916 
917 	/* Ran into start of the file or exceeded maximum number of lines */
918 	if (lnum <= break_lnum)
919 	{
920 	    invalidate_current_state();
921 	    current_lnum = break_lnum + 1;
922 	}
923     }
924 
925     validate_current_state();
926 }
927 
928 /*
929  * Return TRUE if the line-continuation pattern matches in line "lnum".
930  */
931     static int
932 syn_match_linecont(lnum)
933     linenr_T	lnum;
934 {
935     regmmatch_T regmatch;
936 
937     if (syn_buf->b_syn_linecont_prog != NULL)
938     {
939 	regmatch.rmm_ic = syn_buf->b_syn_linecont_ic;
940 	regmatch.regprog = syn_buf->b_syn_linecont_prog;
941 	return syn_regexec(&regmatch, lnum, (colnr_T)0);
942     }
943     return FALSE;
944 }
945 
946 /*
947  * Prepare the current state for the start of a line.
948  */
949     static void
950 syn_start_line()
951 {
952     current_finished = FALSE;
953     current_col = 0;
954 
955     /*
956      * Need to update the end of a start/skip/end that continues from the
957      * previous line and regions that have "keepend".
958      */
959     if (current_state.ga_len > 0)
960 	syn_update_ends(TRUE);
961 
962     next_match_idx = -1;
963     ++current_line_id;
964 }
965 
966 /*
967  * Check for items in the stack that need their end updated.
968  * When "startofline" is TRUE the last item is always updated.
969  * When "startofline" is FALSE the item with "keepend" is forcefully updated.
970  */
971     static void
972 syn_update_ends(startofline)
973     int		startofline;
974 {
975     stateitem_T	*cur_si;
976     int		i;
977 
978     if (startofline)
979     {
980 	/* Check for a match carried over from a previous line with a
981 	 * contained region.  The match ends as soon as the region ends. */
982 	for (i = 0; i < current_state.ga_len; ++i)
983 	{
984 	    cur_si = &CUR_STATE(i);
985 	    if (cur_si->si_idx >= 0
986 		    && (SYN_ITEMS(syn_buf)[cur_si->si_idx]).sp_type
987 							       == SPTYPE_MATCH
988 		    && cur_si->si_m_endpos.lnum < current_lnum)
989 	    {
990 		cur_si->si_flags |= HL_MATCHCONT;
991 		cur_si->si_m_endpos.lnum = 0;
992 		cur_si->si_m_endpos.col = 0;
993 		cur_si->si_h_endpos = cur_si->si_m_endpos;
994 		cur_si->si_ends = TRUE;
995 	    }
996 	}
997     }
998 
999     /*
1000      * Need to update the end of a start/skip/end that continues from the
1001      * previous line.  And regions that have "keepend", because they may
1002      * influence contained items.
1003      * Then check for items ending in column 0.
1004      */
1005     i = current_state.ga_len - 1;
1006     if (keepend_level >= 0)
1007 	for ( ; i > keepend_level; --i)
1008 	    if (CUR_STATE(i).si_flags & HL_EXTEND)
1009 		break;
1010     for ( ; i < current_state.ga_len; ++i)
1011     {
1012 	cur_si = &CUR_STATE(i);
1013 	if ((cur_si->si_flags & HL_KEEPEND)
1014 			    || (i == current_state.ga_len - 1 && startofline))
1015 	{
1016 	    cur_si->si_h_startpos.col = 0;	/* start highl. in col 0 */
1017 	    cur_si->si_h_startpos.lnum = current_lnum;
1018 
1019 	    if (!(cur_si->si_flags & HL_MATCHCONT))
1020 		update_si_end(cur_si, (int)current_col, !startofline);
1021 	}
1022     }
1023     check_keepend();
1024     check_state_ends();
1025 }
1026 
1027 /****************************************
1028  * Handling of the state stack cache.
1029  */
1030 
1031 /*
1032  * EXPLANATION OF THE SYNTAX STATE STACK CACHE
1033  *
1034  * To speed up syntax highlighting, the state stack for the start of some
1035  * lines is cached.  These entries can be used to start parsing at that point.
1036  *
1037  * The stack is kept in b_sst_array[] for each buffer.  There is a list of
1038  * valid entries.  b_sst_first points to the first one, then follow sst_next.
1039  * The entries are sorted on line number.  The first entry is often for line 2
1040  * (line 1 always starts with an empty stack).
1041  * There is also a list for free entries.  This construction is used to avoid
1042  * having to allocate and free memory blocks too often.
1043  *
1044  * When making changes to the buffer, this is logged in b_mod_*.  When calling
1045  * update_screen() to update the display, it will call
1046  * syn_stack_apply_changes() for each displayed buffer to adjust the cached
1047  * entries.  The entries which are inside the changed area are removed,
1048  * because they must be recomputed.  Entries below the changed have their line
1049  * number adjusted for deleted/inserted lines, and have their sst_change_lnum
1050  * set to indicate that a check must be made if the changed lines would change
1051  * the cached entry.
1052  *
1053  * When later displaying lines, an entry is stored for each line.  Displayed
1054  * lines are likely to be displayed again, in which case the state at the
1055  * start of the line is needed.
1056  * For not displayed lines, an entry is stored for every so many lines.  These
1057  * entries will be used e.g., when scrolling backwards.  The distance between
1058  * entries depends on the number of lines in the buffer.  For small buffers
1059  * the distance is fixed at SST_DIST, for large buffers there is a fixed
1060  * number of entries SST_MAX_ENTRIES, and the distance is computed.
1061  */
1062 
1063 /*
1064  * Free b_sst_array[] for buffer "buf".
1065  * Used when syntax items changed to force resyncing everywhere.
1066  */
1067     void
1068 syn_stack_free_all(buf)
1069     buf_T	*buf;
1070 {
1071     synstate_T	*p;
1072     win_T	*wp;
1073 
1074     if (buf->b_sst_array != NULL)
1075     {
1076 	for (p = buf->b_sst_first; p != NULL; p = p->sst_next)
1077 	    clear_syn_state(p);
1078 	vim_free(buf->b_sst_array);
1079 	buf->b_sst_array = NULL;
1080 	buf->b_sst_len = 0;
1081     }
1082 #ifdef FEAT_FOLDING
1083     /* When using "syntax" fold method, must update all folds. */
1084     FOR_ALL_WINDOWS(wp)
1085     {
1086 	if (wp->w_buffer == buf && foldmethodIsSyntax(wp))
1087 	    foldUpdateAll(wp);
1088     }
1089 #endif
1090 }
1091 
1092 /*
1093  * Allocate the syntax state stack for syn_buf when needed.
1094  * If the number of entries in b_sst_array[] is much too big or a bit too
1095  * small, reallocate it.
1096  * Also used to allocate b_sst_array[] for the first time.
1097  */
1098     static void
1099 syn_stack_alloc()
1100 {
1101     long	len;
1102     synstate_T	*to, *from;
1103     synstate_T	*sstp;
1104 
1105     len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
1106     if (len < SST_MIN_ENTRIES)
1107 	len = SST_MIN_ENTRIES;
1108     else if (len > SST_MAX_ENTRIES)
1109 	len = SST_MAX_ENTRIES;
1110     if (syn_buf->b_sst_len > len * 2 || syn_buf->b_sst_len < len)
1111     {
1112 	/* Allocate 50% too much, to avoid reallocating too often. */
1113 	len = syn_buf->b_ml.ml_line_count;
1114 	len = (len + len / 2) / SST_DIST + Rows * 2;
1115 	if (len < SST_MIN_ENTRIES)
1116 	    len = SST_MIN_ENTRIES;
1117 	else if (len > SST_MAX_ENTRIES)
1118 	    len = SST_MAX_ENTRIES;
1119 
1120 	if (syn_buf->b_sst_array != NULL)
1121 	{
1122 	    /* When shrinking the array, cleanup the existing stack.
1123 	     * Make sure that all valid entries fit in the new array. */
1124 	    while (syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2 > len
1125 		    && syn_stack_cleanup())
1126 		;
1127 	    if (len < syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2)
1128 		len = syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2;
1129 	}
1130 
1131 	sstp = (synstate_T *)alloc_clear((unsigned)(len * sizeof(synstate_T)));
1132 	if (sstp == NULL)	/* out of memory! */
1133 	    return;
1134 
1135 	to = sstp - 1;
1136 	if (syn_buf->b_sst_array != NULL)
1137 	{
1138 	    /* Move the states from the old array to the new one. */
1139 	    for (from = syn_buf->b_sst_first; from != NULL;
1140 							from = from->sst_next)
1141 	    {
1142 		++to;
1143 		*to = *from;
1144 		to->sst_next = to + 1;
1145 	    }
1146 	}
1147 	if (to != sstp - 1)
1148 	{
1149 	    to->sst_next = NULL;
1150 	    syn_buf->b_sst_first = sstp;
1151 	    syn_buf->b_sst_freecount = len - (int)(to - sstp) - 1;
1152 	}
1153 	else
1154 	{
1155 	    syn_buf->b_sst_first = NULL;
1156 	    syn_buf->b_sst_freecount = len;
1157 	}
1158 
1159 	/* Create the list of free entries. */
1160 	syn_buf->b_sst_firstfree = to + 1;
1161 	while (++to < sstp + len)
1162 	    to->sst_next = to + 1;
1163 	(sstp + len - 1)->sst_next = NULL;
1164 
1165 	vim_free(syn_buf->b_sst_array);
1166 	syn_buf->b_sst_array = sstp;
1167 	syn_buf->b_sst_len = len;
1168     }
1169 }
1170 
1171 /*
1172  * Check for changes in a buffer to affect stored syntax states.  Uses the
1173  * b_mod_* fields.
1174  * Called from update_screen(), before screen is being updated, once for each
1175  * displayed buffer.
1176  */
1177     void
1178 syn_stack_apply_changes(buf)
1179     buf_T	*buf;
1180 {
1181     synstate_T	*p, *prev, *np;
1182     linenr_T	n;
1183 
1184     if (buf->b_sst_array == NULL)	/* nothing to do */
1185 	return;
1186 
1187     prev = NULL;
1188     for (p = buf->b_sst_first; p != NULL; )
1189     {
1190 	if (p->sst_lnum + buf->b_syn_sync_linebreaks > buf->b_mod_top)
1191 	{
1192 	    n = p->sst_lnum + buf->b_mod_xlines;
1193 	    if (n <= buf->b_mod_bot)
1194 	    {
1195 		/* this state is inside the changed area, remove it */
1196 		np = p->sst_next;
1197 		if (prev == NULL)
1198 		    buf->b_sst_first = np;
1199 		else
1200 		    prev->sst_next = np;
1201 		syn_stack_free_entry(buf, p);
1202 		p = np;
1203 		continue;
1204 	    }
1205 	    /* This state is below the changed area.  Remember the line
1206 	     * that needs to be parsed before this entry can be made valid
1207 	     * again. */
1208 	    if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
1209 	    {
1210 		if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
1211 		    p->sst_change_lnum += buf->b_mod_xlines;
1212 		else
1213 		    p->sst_change_lnum = buf->b_mod_top;
1214 	    }
1215 	    if (p->sst_change_lnum == 0
1216 		    || p->sst_change_lnum < buf->b_mod_bot)
1217 		p->sst_change_lnum = buf->b_mod_bot;
1218 
1219 	    p->sst_lnum = n;
1220 	}
1221 	prev = p;
1222 	p = p->sst_next;
1223     }
1224 }
1225 
1226 /*
1227  * Reduce the number of entries in the state stack for syn_buf.
1228  * Returns TRUE if at least one entry was freed.
1229  */
1230     static int
1231 syn_stack_cleanup()
1232 {
1233     synstate_T	*p, *prev;
1234     disptick_T	tick;
1235     int		above;
1236     int		dist;
1237     int		retval = FALSE;
1238 
1239     if (syn_buf->b_sst_array == NULL || syn_buf->b_sst_first == NULL)
1240 	return retval;
1241 
1242     /* Compute normal distance between non-displayed entries. */
1243     dist = syn_buf->b_ml.ml_line_count / (syn_buf->b_sst_len - Rows) + 1;
1244 
1245     /*
1246      * Go throught the list to find the "tick" for the oldest entry that can
1247      * be removed.  Set "above" when the "tick" for the oldest entry is above
1248      * "b_sst_lasttick" (the display tick wraps around).
1249      */
1250     tick = syn_buf->b_sst_lasttick;
1251     above = FALSE;
1252     prev = syn_buf->b_sst_first;
1253     for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1254     {
1255 	if (prev->sst_lnum + dist > p->sst_lnum)
1256 	{
1257 	    if (p->sst_tick > syn_buf->b_sst_lasttick)
1258 	    {
1259 		if (!above || p->sst_tick < tick)
1260 		    tick = p->sst_tick;
1261 		above = TRUE;
1262 	    }
1263 	    else if (!above && p->sst_tick < tick)
1264 		tick = p->sst_tick;
1265 	}
1266     }
1267 
1268     /*
1269      * Go through the list to make the entries for the oldest tick at an
1270      * interval of several lines.
1271      */
1272     prev = syn_buf->b_sst_first;
1273     for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1274     {
1275 	if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
1276 	{
1277 	    /* Move this entry from used list to free list */
1278 	    prev->sst_next = p->sst_next;
1279 	    syn_stack_free_entry(syn_buf, p);
1280 	    p = prev;
1281 	    retval = TRUE;
1282 	}
1283     }
1284     return retval;
1285 }
1286 
1287 /*
1288  * Free the allocated memory for a syn_state item.
1289  * Move the entry into the free list.
1290  */
1291     static void
1292 syn_stack_free_entry(buf, p)
1293     buf_T	*buf;
1294     synstate_T	*p;
1295 {
1296     clear_syn_state(p);
1297     p->sst_next = buf->b_sst_firstfree;
1298     buf->b_sst_firstfree = p;
1299     ++buf->b_sst_freecount;
1300 }
1301 
1302 /*
1303  * Find an entry in the list of state stacks at or before "lnum".
1304  * Returns NULL when there is no entry or the first entry is after "lnum".
1305  */
1306     static synstate_T *
1307 syn_stack_find_entry(lnum)
1308     linenr_T	lnum;
1309 {
1310     synstate_T	*p, *prev;
1311 
1312     prev = NULL;
1313     for (p = syn_buf->b_sst_first; p != NULL; prev = p, p = p->sst_next)
1314     {
1315 	if (p->sst_lnum == lnum)
1316 	    return p;
1317 	if (p->sst_lnum > lnum)
1318 	    break;
1319     }
1320     return prev;
1321 }
1322 
1323 /*
1324  * Try saving the current state in b_sst_array[].
1325  * The current state must be valid for the start of the current_lnum line!
1326  */
1327     static synstate_T *
1328 store_current_state(sp)
1329     synstate_T	*sp;	/* at or before where state is to be saved or
1330 				   NULL */
1331 {
1332     int		i;
1333     synstate_T	*p;
1334     bufstate_T	*bp;
1335     stateitem_T	*cur_si;
1336 
1337     if (sp == NULL)
1338 	sp = syn_stack_find_entry(current_lnum);
1339 
1340     /*
1341      * If the current state contains a start or end pattern that continues
1342      * from the previous line, we can't use it.  Don't store it then.
1343      */
1344     for (i = current_state.ga_len - 1; i >= 0; --i)
1345     {
1346 	cur_si = &CUR_STATE(i);
1347 	if (cur_si->si_h_startpos.lnum >= current_lnum
1348 		|| cur_si->si_m_endpos.lnum >= current_lnum
1349 		|| cur_si->si_h_endpos.lnum >= current_lnum
1350 		|| (cur_si->si_end_idx
1351 		    && cur_si->si_eoe_pos.lnum >= current_lnum))
1352 	    break;
1353     }
1354     if (i >= 0)
1355     {
1356 	if (sp != NULL)
1357 	{
1358 	    /* find "sp" in the list and remove it */
1359 	    if (syn_buf->b_sst_first == sp)
1360 		/* it's the first entry */
1361 		syn_buf->b_sst_first = sp->sst_next;
1362 	    else
1363 	    {
1364 		/* find the entry just before this one to adjust sst_next */
1365 		for (p = syn_buf->b_sst_first; p != NULL; p = p->sst_next)
1366 		    if (p->sst_next == sp)
1367 			break;
1368 		p->sst_next = sp->sst_next;
1369 	    }
1370 	    syn_stack_free_entry(syn_buf, sp);
1371 	    sp = NULL;
1372 	}
1373     }
1374     else if (sp == NULL || sp->sst_lnum != current_lnum)
1375     {
1376 	/*
1377 	 * Add a new entry
1378 	 */
1379 	/* If no free items, cleanup the array first. */
1380 	if (syn_buf->b_sst_freecount == 0)
1381 	{
1382 	    (void)syn_stack_cleanup();
1383 	    /* "sp" may have been moved to the freelist now */
1384 	    sp = syn_stack_find_entry(current_lnum);
1385 	}
1386 	/* Still no free items?  Must be a strange problem... */
1387 	if (syn_buf->b_sst_freecount == 0)
1388 	    sp = NULL;
1389 	else
1390 	{
1391 	    /* Take the first item from the free list and put it in the used
1392 	     * list, after *sp */
1393 	    p = syn_buf->b_sst_firstfree;
1394 	    syn_buf->b_sst_firstfree = p->sst_next;
1395 	    --syn_buf->b_sst_freecount;
1396 	    if (sp == NULL)
1397 	    {
1398 		/* Insert in front of the list */
1399 		p->sst_next = syn_buf->b_sst_first;
1400 		syn_buf->b_sst_first = p;
1401 	    }
1402 	    else
1403 	    {
1404 		/* insert in list after *sp */
1405 		p->sst_next = sp->sst_next;
1406 		sp->sst_next = p;
1407 	    }
1408 	    sp = p;
1409 	    sp->sst_stacksize = 0;
1410 	    sp->sst_lnum = current_lnum;
1411 	}
1412     }
1413     if (sp != NULL)
1414     {
1415 	/* When overwriting an existing state stack, clear it first */
1416 	clear_syn_state(sp);
1417 	sp->sst_stacksize = current_state.ga_len;
1418 	if (current_state.ga_len > SST_FIX_STATES)
1419 	{
1420 	    /* Need to clear it, might be something remaining from when the
1421 	     * length was less than SST_FIX_STATES. */
1422 	    ga_init2(&sp->sst_union.sst_ga, (int)sizeof(bufstate_T), 1);
1423 	    if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
1424 		sp->sst_stacksize = 0;
1425 	    else
1426 		sp->sst_union.sst_ga.ga_len = current_state.ga_len;
1427 	    bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1428 	}
1429 	else
1430 	    bp = sp->sst_union.sst_stack;
1431 	for (i = 0; i < sp->sst_stacksize; ++i)
1432 	{
1433 	    bp[i].bs_idx = CUR_STATE(i).si_idx;
1434 	    bp[i].bs_flags = CUR_STATE(i).si_flags;
1435 	    bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
1436 	}
1437 	sp->sst_next_flags = current_next_flags;
1438 	sp->sst_next_list = current_next_list;
1439 	sp->sst_tick = display_tick;
1440 	sp->sst_change_lnum = 0;
1441     }
1442     current_state_stored = TRUE;
1443     return sp;
1444 }
1445 
1446 /*
1447  * Copy a state stack from "from" in b_sst_array[] to current_state;
1448  */
1449     static void
1450 load_current_state(from)
1451     synstate_T	*from;
1452 {
1453     int		i;
1454     bufstate_T	*bp;
1455 
1456     clear_current_state();
1457     validate_current_state();
1458     keepend_level = -1;
1459     if (from->sst_stacksize
1460 	    && ga_grow(&current_state, from->sst_stacksize) != FAIL)
1461     {
1462 	if (from->sst_stacksize > SST_FIX_STATES)
1463 	    bp = SYN_STATE_P(&(from->sst_union.sst_ga));
1464 	else
1465 	    bp = from->sst_union.sst_stack;
1466 	for (i = 0; i < from->sst_stacksize; ++i)
1467 	{
1468 	    CUR_STATE(i).si_idx = bp[i].bs_idx;
1469 	    CUR_STATE(i).si_flags = bp[i].bs_flags;
1470 	    CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
1471 	    if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
1472 		keepend_level = i;
1473 	    CUR_STATE(i).si_ends = FALSE;
1474 	    CUR_STATE(i).si_m_lnum = 0;
1475 	    if (CUR_STATE(i).si_idx >= 0)
1476 		CUR_STATE(i).si_next_list =
1477 		       (SYN_ITEMS(syn_buf)[CUR_STATE(i).si_idx]).sp_next_list;
1478 	    else
1479 		CUR_STATE(i).si_next_list = NULL;
1480 	    update_si_attr(i);
1481 	}
1482 	current_state.ga_len = from->sst_stacksize;
1483     }
1484     current_next_list = from->sst_next_list;
1485     current_next_flags = from->sst_next_flags;
1486     current_lnum = from->sst_lnum;
1487 }
1488 
1489 /*
1490  * Compare saved state stack "*sp" with the current state.
1491  * Return TRUE when they are equal.
1492  */
1493     static int
1494 syn_stack_equal(sp)
1495     synstate_T *sp;
1496 {
1497     int		i, j;
1498     bufstate_T	*bp;
1499     reg_extmatch_T	*six, *bsx;
1500 
1501     /* First a quick check if the stacks have the same size end nextlist. */
1502     if (sp->sst_stacksize == current_state.ga_len
1503 	    && sp->sst_next_list == current_next_list)
1504     {
1505 	/* Need to compare all states on both stacks. */
1506 	if (sp->sst_stacksize > SST_FIX_STATES)
1507 	    bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1508 	else
1509 	    bp = sp->sst_union.sst_stack;
1510 
1511 	for (i = current_state.ga_len; --i >= 0; )
1512 	{
1513 	    /* If the item has another index the state is different. */
1514 	    if (bp[i].bs_idx != CUR_STATE(i).si_idx)
1515 		break;
1516 	    if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
1517 	    {
1518 		/* When the extmatch pointers are different, the strings in
1519 		 * them can still be the same.  Check if the extmatch
1520 		 * references are equal. */
1521 		bsx = bp[i].bs_extmatch;
1522 		six = CUR_STATE(i).si_extmatch;
1523 		/* If one of the extmatch pointers is NULL the states are
1524 		 * different. */
1525 		if (bsx == NULL || six == NULL)
1526 		    break;
1527 		for (j = 0; j < NSUBEXP; ++j)
1528 		{
1529 		    /* Check each referenced match string. They must all be
1530 		     * equal. */
1531 		    if (bsx->matches[j] != six->matches[j])
1532 		    {
1533 			/* If the pointer is different it can still be the
1534 			 * same text.  Compare the strings, ignore case when
1535 			 * the start item has the sp_ic flag set. */
1536 			if (bsx->matches[j] == NULL
1537 				|| six->matches[j] == NULL)
1538 			    break;
1539 			if ((SYN_ITEMS(syn_buf)[CUR_STATE(i).si_idx]).sp_ic
1540 				? MB_STRICMP(bsx->matches[j],
1541 							 six->matches[j]) != 0
1542 				: STRCMP(bsx->matches[j], six->matches[j]) != 0)
1543 			    break;
1544 		    }
1545 		}
1546 		if (j != NSUBEXP)
1547 		    break;
1548 	    }
1549 	}
1550 	if (i < 0)
1551 	    return TRUE;
1552     }
1553     return FALSE;
1554 }
1555 
1556 /*
1557  * We stop parsing syntax above line "lnum".  If the stored state at or below
1558  * this line depended on a change before it, it now depends on the line below
1559  * the last parsed line.
1560  * The window looks like this:
1561  *	    line which changed
1562  *	    displayed line
1563  *	    displayed line
1564  * lnum ->  line below window
1565  */
1566     void
1567 syntax_end_parsing(lnum)
1568     linenr_T	lnum;
1569 {
1570     synstate_T	*sp;
1571 
1572     sp = syn_stack_find_entry(lnum);
1573     if (sp != NULL && sp->sst_lnum < lnum)
1574 	sp = sp->sst_next;
1575 
1576     if (sp != NULL && sp->sst_change_lnum != 0)
1577 	sp->sst_change_lnum = lnum;
1578 }
1579 
1580 /*
1581  * End of handling of the state stack.
1582  ****************************************/
1583 
1584     static void
1585 invalidate_current_state()
1586 {
1587     clear_current_state();
1588     current_state.ga_itemsize = 0;	/* mark current_state invalid */
1589     current_next_list = NULL;
1590     keepend_level = -1;
1591 }
1592 
1593     static void
1594 validate_current_state()
1595 {
1596     current_state.ga_itemsize = sizeof(stateitem_T);
1597     current_state.ga_growsize = 3;
1598 }
1599 
1600 /*
1601  * Return TRUE if the syntax at start of lnum changed since last time.
1602  * This will only be called just after get_syntax_attr() for the previous
1603  * line, to check if the next line needs to be redrawn too.
1604  */
1605     int
1606 syntax_check_changed(lnum)
1607     linenr_T	lnum;
1608 {
1609     int		retval = TRUE;
1610     synstate_T	*sp;
1611 
1612     /*
1613      * Check the state stack when:
1614      * - lnum is just below the previously syntaxed line.
1615      * - lnum is not before the lines with saved states.
1616      * - lnum is not past the lines with saved states.
1617      * - lnum is at or before the last changed line.
1618      */
1619     if (VALID_STATE(&current_state) && lnum == current_lnum + 1)
1620     {
1621 	sp = syn_stack_find_entry(lnum);
1622 	if (sp != NULL && sp->sst_lnum == lnum)
1623 	{
1624 	    /*
1625 	     * finish the previous line (needed when not all of the line was
1626 	     * drawn)
1627 	     */
1628 	    (void)syn_finish_line(FALSE);
1629 
1630 	    /*
1631 	     * Compare the current state with the previously saved state of
1632 	     * the line.
1633 	     */
1634 	    if (syn_stack_equal(sp))
1635 		retval = FALSE;
1636 
1637 	    /*
1638 	     * Store the current state in b_sst_array[] for later use.
1639 	     */
1640 	    ++current_lnum;
1641 	    (void)store_current_state(NULL);
1642 	}
1643     }
1644 
1645     return retval;
1646 }
1647 
1648 /*
1649  * Finish the current line.
1650  * This doesn't return any attributes, it only gets the state at the end of
1651  * the line.  It can start anywhere in the line, as long as the current state
1652  * is valid.
1653  */
1654     static int
1655 syn_finish_line(syncing)
1656     int	    syncing;		/* called for syncing */
1657 {
1658     stateitem_T	*cur_si;
1659     colnr_T	prev_current_col;
1660 
1661     if (!current_finished)
1662     {
1663 	while (!current_finished)
1664 	{
1665 	    (void)syn_current_attr(syncing, FALSE, NULL);
1666 	    /*
1667 	     * When syncing, and found some item, need to check the item.
1668 	     */
1669 	    if (syncing && current_state.ga_len)
1670 	    {
1671 		/*
1672 		 * Check for match with sync item.
1673 		 */
1674 		cur_si = &CUR_STATE(current_state.ga_len - 1);
1675 		if (cur_si->si_idx >= 0
1676 			&& (SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_flags
1677 					      & (HL_SYNC_HERE|HL_SYNC_THERE)))
1678 		    return TRUE;
1679 
1680 		/* syn_current_attr() will have skipped the check for an item
1681 		 * that ends here, need to do that now.  Be careful not to go
1682 		 * past the NUL. */
1683 		prev_current_col = current_col;
1684 		if (syn_getcurline()[current_col] != NUL)
1685 		    ++current_col;
1686 		check_state_ends();
1687 		current_col = prev_current_col;
1688 	    }
1689 	    ++current_col;
1690 	}
1691     }
1692     return FALSE;
1693 }
1694 
1695 /*
1696  * Return highlight attributes for next character.
1697  * Must first call syntax_start() once for the line.
1698  * "col" is normally 0 for the first use in a line, and increments by one each
1699  * time.  It's allowed to skip characters and to stop before the end of the
1700  * line.  But only a "col" after a previously used column is allowed.
1701  * When "can_spell" is not NULL set it to TRUE when spell-checking should be
1702  * done.
1703  */
1704     int
1705 get_syntax_attr(col, can_spell)
1706     colnr_T	col;
1707     int		*can_spell;
1708 {
1709     int	    attr = 0;
1710 
1711     /* check for out of memory situation */
1712     if (syn_buf->b_sst_array == NULL)
1713 	return 0;
1714 
1715     /* After 'synmaxcol' the attribute is always zero. */
1716     if (syn_buf->b_p_smc > 0 && col >= (colnr_T)syn_buf->b_p_smc)
1717     {
1718 	clear_current_state();
1719 #ifdef FEAT_EVAL
1720 	current_id = 0;
1721 	current_trans_id = 0;
1722 #endif
1723 	return 0;
1724     }
1725 
1726     /* Make sure current_state is valid */
1727     if (INVALID_STATE(&current_state))
1728 	validate_current_state();
1729 
1730     /*
1731      * Skip from the current column to "col", get the attributes for "col".
1732      */
1733     while (current_col <= col)
1734     {
1735 	attr = syn_current_attr(FALSE, TRUE, can_spell);
1736 	++current_col;
1737     }
1738 
1739     return attr;
1740 }
1741 
1742 /*
1743  * Get syntax attributes for current_lnum, current_col.
1744  */
1745     static int
1746 syn_current_attr(syncing, displaying, can_spell)
1747     int		syncing;		/* When 1: called for syncing */
1748     int		displaying;		/* result will be displayed */
1749     int		*can_spell;		/* return: do spell checking */
1750 {
1751     int		syn_id;
1752     lpos_T	endpos;		/* was: char_u *endp; */
1753     lpos_T	hl_startpos;	/* was: int hl_startcol; */
1754     lpos_T	hl_endpos;
1755     lpos_T	eos_pos;	/* end-of-start match (start region) */
1756     lpos_T	eoe_pos;	/* end-of-end pattern */
1757     int		end_idx;	/* group ID for end pattern */
1758     int		idx;
1759     synpat_T	*spp;
1760     stateitem_T	*cur_si, *sip = NULL;
1761     int		startcol;
1762     int		endcol;
1763     long	flags;
1764     short	*next_list;
1765     int		found_match;		    /* found usable match */
1766     static int	try_next_column = FALSE;    /* must try in next col */
1767     int		do_keywords;
1768     regmmatch_T	regmatch;
1769     lpos_T	pos;
1770     int		lc_col;
1771     reg_extmatch_T *cur_extmatch = NULL;
1772     char_u	*line;		/* current line.  NOTE: becomes invalid after
1773 				   looking for a pattern match! */
1774 
1775     /* variables for zero-width matches that have a "nextgroup" argument */
1776     int		keep_next_list;
1777     int		zero_width_next_list = FALSE;
1778     garray_T	zero_width_next_ga;
1779 
1780     /*
1781      * No character, no attributes!  Past end of line?
1782      * Do try matching with an empty line (could be the start of a region).
1783      */
1784     line = syn_getcurline();
1785     if (line[current_col] == NUL && current_col != 0)
1786     {
1787 	/*
1788 	 * If we found a match after the last column, use it.
1789 	 */
1790 	if (next_match_idx >= 0 && next_match_col >= (int)current_col
1791 						  && next_match_col != MAXCOL)
1792 	    (void)push_next_match(NULL);
1793 
1794 	current_finished = TRUE;
1795 	current_state_stored = FALSE;
1796 	return 0;
1797     }
1798 
1799     /* if the current or next character is NUL, we will finish the line now */
1800     if (line[current_col] == NUL || line[current_col + 1] == NUL)
1801     {
1802 	current_finished = TRUE;
1803 	current_state_stored = FALSE;
1804     }
1805 
1806     /*
1807      * When in the previous column there was a match but it could not be used
1808      * (empty match or already matched in this column) need to try again in
1809      * the next column.
1810      */
1811     if (try_next_column)
1812     {
1813 	next_match_idx = -1;
1814 	try_next_column = FALSE;
1815     }
1816 
1817     /* Only check for keywords when not syncing and there are some. */
1818     do_keywords = !syncing
1819 		    && (syn_buf->b_keywtab.ht_used > 0
1820 			    || syn_buf->b_keywtab_ic.ht_used > 0);
1821 
1822     /* Init the list of zero-width matches with a nextlist.  This is used to
1823      * avoid matching the same item in the same position twice. */
1824     ga_init2(&zero_width_next_ga, (int)sizeof(int), 10);
1825 
1826     /*
1827      * Repeat matching keywords and patterns, to find contained items at the
1828      * same column.  This stops when there are no extra matches at the current
1829      * column.
1830      */
1831     do
1832     {
1833 	found_match = FALSE;
1834 	keep_next_list = FALSE;
1835 	syn_id = 0;
1836 
1837 	/*
1838 	 * 1. Check for a current state.
1839 	 *    Only when there is no current state, or if the current state may
1840 	 *    contain other things, we need to check for keywords and patterns.
1841 	 *    Always need to check for contained items if some item has the
1842 	 *    "containedin" argument (takes extra time!).
1843 	 */
1844 	if (current_state.ga_len)
1845 	    cur_si = &CUR_STATE(current_state.ga_len - 1);
1846 	else
1847 	    cur_si = NULL;
1848 
1849 	if (syn_buf->b_syn_containedin || cur_si == NULL
1850 					      || cur_si->si_cont_list != NULL)
1851 	{
1852 	    /*
1853 	     * 2. Check for keywords, if on a keyword char after a non-keyword
1854 	     *	  char.  Don't do this when syncing.
1855 	     */
1856 	    if (do_keywords)
1857 	    {
1858 	      line = syn_getcurline();
1859 	      if (vim_iswordc_buf(line + current_col, syn_buf)
1860 		      && (current_col == 0
1861 			  || !vim_iswordc_buf(line + current_col - 1
1862 #ifdef FEAT_MBYTE
1863 			      - (has_mbyte
1864 				  ? (*mb_head_off)(line, line + current_col - 1)
1865 				  : 0)
1866 #endif
1867 			       , syn_buf)))
1868 	      {
1869 		syn_id = check_keyword_id(line, (int)current_col,
1870 					 &endcol, &flags, &next_list, cur_si);
1871 		if (syn_id != 0)
1872 		{
1873 		    if (push_current_state(KEYWORD_IDX) == OK)
1874 		    {
1875 			cur_si = &CUR_STATE(current_state.ga_len - 1);
1876 			cur_si->si_m_startcol = current_col;
1877 			cur_si->si_h_startpos.lnum = current_lnum;
1878 			cur_si->si_h_startpos.col = 0;	/* starts right away */
1879 			cur_si->si_m_endpos.lnum = current_lnum;
1880 			cur_si->si_m_endpos.col = endcol;
1881 			cur_si->si_h_endpos.lnum = current_lnum;
1882 			cur_si->si_h_endpos.col = endcol;
1883 			cur_si->si_ends = TRUE;
1884 			cur_si->si_end_idx = 0;
1885 			cur_si->si_flags = flags;
1886 			cur_si->si_id = syn_id;
1887 			cur_si->si_trans_id = syn_id;
1888 			if (flags & HL_TRANSP)
1889 			{
1890 			    if (current_state.ga_len < 2)
1891 			    {
1892 				cur_si->si_attr = 0;
1893 				cur_si->si_trans_id = 0;
1894 			    }
1895 			    else
1896 			    {
1897 				cur_si->si_attr = CUR_STATE(
1898 					current_state.ga_len - 2).si_attr;
1899 				cur_si->si_trans_id = CUR_STATE(
1900 					current_state.ga_len - 2).si_trans_id;
1901 			    }
1902 			}
1903 			else
1904 			    cur_si->si_attr = syn_id2attr(syn_id);
1905 			cur_si->si_cont_list = NULL;
1906 			cur_si->si_next_list = next_list;
1907 			check_keepend();
1908 		    }
1909 		    else
1910 			vim_free(next_list);
1911 		}
1912 	      }
1913 	    }
1914 
1915 	    /*
1916 	     * 3. Check for patterns (only if no keyword found).
1917 	     */
1918 	    if (syn_id == 0 && syn_buf->b_syn_patterns.ga_len)
1919 	    {
1920 		/*
1921 		 * If we didn't check for a match yet, or we are past it, check
1922 		 * for any match with a pattern.
1923 		 */
1924 		if (next_match_idx < 0 || next_match_col < (int)current_col)
1925 		{
1926 		    /*
1927 		     * Check all relevant patterns for a match at this
1928 		     * position.  This is complicated, because matching with a
1929 		     * pattern takes quite a bit of time, thus we want to
1930 		     * avoid doing it when it's not needed.
1931 		     */
1932 		    next_match_idx = 0;		/* no match in this line yet */
1933 		    next_match_col = MAXCOL;
1934 		    for (idx = syn_buf->b_syn_patterns.ga_len; --idx >= 0; )
1935 		    {
1936 			spp = &(SYN_ITEMS(syn_buf)[idx]);
1937 			if (	   spp->sp_syncing == syncing
1938 				&& (displaying || !(spp->sp_flags & HL_DISPLAY))
1939 				&& (spp->sp_type == SPTYPE_MATCH
1940 				    || spp->sp_type == SPTYPE_START)
1941 				&& (current_next_list != NULL
1942 				    ? in_id_list(NULL, current_next_list,
1943 							      &spp->sp_syn, 0)
1944 				    : (cur_si == NULL
1945 					? !(spp->sp_flags & HL_CONTAINED)
1946 					: in_id_list(cur_si,
1947 					    cur_si->si_cont_list, &spp->sp_syn,
1948 					    spp->sp_flags & HL_CONTAINED))))
1949 			{
1950 			    /* If we already tried matching in this line, and
1951 			     * there isn't a match before next_match_col, skip
1952 			     * this item. */
1953 			    if (spp->sp_line_id == current_line_id
1954 				    && spp->sp_startcol >= next_match_col)
1955 				continue;
1956 			    spp->sp_line_id = current_line_id;
1957 
1958 			    lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
1959 			    if (lc_col < 0)
1960 				lc_col = 0;
1961 
1962 			    regmatch.rmm_ic = spp->sp_ic;
1963 			    regmatch.regprog = spp->sp_prog;
1964 			    if (!syn_regexec(&regmatch, current_lnum,
1965 							     (colnr_T)lc_col))
1966 			    {
1967 				/* no match in this line, try another one */
1968 				spp->sp_startcol = MAXCOL;
1969 				continue;
1970 			    }
1971 
1972 			    /*
1973 			     * Compute the first column of the match.
1974 			     */
1975 			    syn_add_start_off(&pos, &regmatch,
1976 							 spp, SPO_MS_OFF, -1);
1977 			    if (pos.lnum > current_lnum)
1978 			    {
1979 				/* must have used end of match in a next line,
1980 				 * we can't handle that */
1981 				spp->sp_startcol = MAXCOL;
1982 				continue;
1983 			    }
1984 			    startcol = pos.col;
1985 
1986 			    /* remember the next column where this pattern
1987 			     * matches in the current line */
1988 			    spp->sp_startcol = startcol;
1989 
1990 			    /*
1991 			     * If a previously found match starts at a lower
1992 			     * column number, don't use this one.
1993 			     */
1994 			    if (startcol >= next_match_col)
1995 				continue;
1996 
1997 			    /*
1998 			     * If we matched this pattern at this position
1999 			     * before, skip it.  Must retry in the next
2000 			     * column, because it may match from there.
2001 			     */
2002 			    if (did_match_already(idx, &zero_width_next_ga))
2003 			    {
2004 				try_next_column = TRUE;
2005 				continue;
2006 			    }
2007 
2008 			    endpos.lnum = regmatch.endpos[0].lnum;
2009 			    endpos.col = regmatch.endpos[0].col;
2010 
2011 			    /* Compute the highlight start. */
2012 			    syn_add_start_off(&hl_startpos, &regmatch,
2013 							 spp, SPO_HS_OFF, -1);
2014 
2015 			    /* Compute the region start. */
2016 			    /* Default is to use the end of the match. */
2017 			    syn_add_end_off(&eos_pos, &regmatch,
2018 							 spp, SPO_RS_OFF, 0);
2019 
2020 			    /*
2021 			     * Grab the external submatches before they get
2022 			     * overwritten.  Reference count doesn't change.
2023 			     */
2024 			    unref_extmatch(cur_extmatch);
2025 			    cur_extmatch = re_extmatch_out;
2026 			    re_extmatch_out = NULL;
2027 
2028 			    flags = 0;
2029 			    eoe_pos.lnum = 0;	/* avoid warning */
2030 			    eoe_pos.col = 0;
2031 			    end_idx = 0;
2032 			    hl_endpos.lnum = 0;
2033 
2034 			    /*
2035 			     * For a "oneline" the end must be found in the
2036 			     * same line too.  Search for it after the end of
2037 			     * the match with the start pattern.  Set the
2038 			     * resulting end positions at the same time.
2039 			     */
2040 			    if (spp->sp_type == SPTYPE_START
2041 					      && (spp->sp_flags & HL_ONELINE))
2042 			    {
2043 				lpos_T	startpos;
2044 
2045 				startpos = endpos;
2046 				find_endpos(idx, &startpos, &endpos, &hl_endpos,
2047 				    &flags, &eoe_pos, &end_idx, cur_extmatch);
2048 				if (endpos.lnum == 0)
2049 				    continue;	    /* not found */
2050 			    }
2051 
2052 			    /*
2053 			     * For a "match" the size must be > 0 after the
2054 			     * end offset needs has been added.  Except when
2055 			     * syncing.
2056 			     */
2057 			    else if (spp->sp_type == SPTYPE_MATCH)
2058 			    {
2059 				syn_add_end_off(&hl_endpos, &regmatch, spp,
2060 							       SPO_HE_OFF, 0);
2061 				syn_add_end_off(&endpos, &regmatch, spp,
2062 							       SPO_ME_OFF, 0);
2063 				if (endpos.lnum == current_lnum
2064 				      && (int)endpos.col + syncing < startcol)
2065 				{
2066 				    /*
2067 				     * If an empty string is matched, may need
2068 				     * to try matching again at next column.
2069 				     */
2070 				    if (regmatch.startpos[0].col
2071 						    == regmatch.endpos[0].col)
2072 					try_next_column = TRUE;
2073 				    continue;
2074 				}
2075 			    }
2076 
2077 			    /*
2078 			     * keep the best match so far in next_match_*
2079 			     */
2080 			    /* Highlighting must start after startpos and end
2081 			     * before endpos. */
2082 			    if (hl_startpos.lnum == current_lnum
2083 					   && (int)hl_startpos.col < startcol)
2084 				hl_startpos.col = startcol;
2085 			    limit_pos_zero(&hl_endpos, &endpos);
2086 
2087 			    next_match_idx = idx;
2088 			    next_match_col = startcol;
2089 			    next_match_m_endpos = endpos;
2090 			    next_match_h_endpos = hl_endpos;
2091 			    next_match_h_startpos = hl_startpos;
2092 			    next_match_flags = flags;
2093 			    next_match_eos_pos = eos_pos;
2094 			    next_match_eoe_pos = eoe_pos;
2095 			    next_match_end_idx = end_idx;
2096 			    unref_extmatch(next_match_extmatch);
2097 			    next_match_extmatch = cur_extmatch;
2098 			    cur_extmatch = NULL;
2099 			}
2100 		    }
2101 		}
2102 
2103 		/*
2104 		 * If we found a match at the current column, use it.
2105 		 */
2106 		if (next_match_idx >= 0 && next_match_col == (int)current_col)
2107 		{
2108 		    synpat_T	*lspp;
2109 
2110 		    /* When a zero-width item matched which has a nextgroup,
2111 		     * don't push the item but set nextgroup. */
2112 		    lspp = &(SYN_ITEMS(syn_buf)[next_match_idx]);
2113 		    if (next_match_m_endpos.lnum == current_lnum
2114 			    && next_match_m_endpos.col == current_col
2115 			    && lspp->sp_next_list != NULL)
2116 		    {
2117 			current_next_list = lspp->sp_next_list;
2118 			current_next_flags = lspp->sp_flags;
2119 			keep_next_list = TRUE;
2120 			zero_width_next_list = TRUE;
2121 
2122 			/* Add the index to a list, so that we can check
2123 			 * later that we don't match it again (and cause an
2124 			 * endless loop). */
2125 			if (ga_grow(&zero_width_next_ga, 1) == OK)
2126 			{
2127 			    ((int *)(zero_width_next_ga.ga_data))
2128 				[zero_width_next_ga.ga_len++] = next_match_idx;
2129 			}
2130 			next_match_idx = -1;
2131 		    }
2132 		    else
2133 			cur_si = push_next_match(cur_si);
2134 		    found_match = TRUE;
2135 		}
2136 	    }
2137 	}
2138 
2139 	/*
2140 	 * Handle searching for nextgroup match.
2141 	 */
2142 	if (current_next_list != NULL && !keep_next_list)
2143 	{
2144 	    /*
2145 	     * If a nextgroup was not found, continue looking for one if:
2146 	     * - this is an empty line and the "skipempty" option was given
2147 	     * - we are on white space and the "skipwhite" option was given
2148 	     */
2149 	    if (!found_match)
2150 	    {
2151 		line = syn_getcurline();
2152 		if (((current_next_flags & HL_SKIPWHITE)
2153 			    && vim_iswhite(line[current_col]))
2154 			|| ((current_next_flags & HL_SKIPEMPTY)
2155 			    && *line == NUL))
2156 		    break;
2157 	    }
2158 
2159 	    /*
2160 	     * If a nextgroup was found: Use it, and continue looking for
2161 	     * contained matches.
2162 	     * If a nextgroup was not found: Continue looking for a normal
2163 	     * match.
2164 	     * When did set current_next_list for a zero-width item and no
2165 	     * match was found don't loop (would get stuck).
2166 	     */
2167 	    current_next_list = NULL;
2168 	    next_match_idx = -1;
2169 	    if (!zero_width_next_list)
2170 		found_match = TRUE;
2171 	}
2172 
2173     } while (found_match);
2174 
2175     /*
2176      * Use attributes from the current state, if within its highlighting.
2177      * If not, use attributes from the current-but-one state, etc.
2178      */
2179     current_attr = 0;
2180 #ifdef FEAT_EVAL
2181     current_id = 0;
2182     current_trans_id = 0;
2183 #endif
2184     if (cur_si != NULL)
2185     {
2186 #ifndef FEAT_EVAL
2187 	int	current_trans_id = 0;
2188 #endif
2189 	for (idx = current_state.ga_len - 1; idx >= 0; --idx)
2190 	{
2191 	    sip = &CUR_STATE(idx);
2192 	    if ((current_lnum > sip->si_h_startpos.lnum
2193 			|| (current_lnum == sip->si_h_startpos.lnum
2194 			    && current_col >= sip->si_h_startpos.col))
2195 		    && (sip->si_h_endpos.lnum == 0
2196 			|| current_lnum < sip->si_h_endpos.lnum
2197 			|| (current_lnum == sip->si_h_endpos.lnum
2198 			    && current_col < sip->si_h_endpos.col)))
2199 	    {
2200 		current_attr = sip->si_attr;
2201 #ifdef FEAT_EVAL
2202 		current_id = sip->si_id;
2203 #endif
2204 		current_trans_id = sip->si_trans_id;
2205 		break;
2206 	    }
2207 	}
2208 
2209 	if (can_spell != NULL)
2210 	{
2211 	    struct sp_syn   sps;
2212 
2213 	    /*
2214 	     * set "can_spell" to TRUE if spell checking is supposed to be
2215 	     * done in the current item.
2216 	     */
2217 	    if (syn_buf->b_spell_cluster_id == 0)
2218 	    {
2219 		/* There is no @Spell cluster: Do spelling for items without
2220 		 * @NoSpell cluster. */
2221 		if (syn_buf->b_nospell_cluster_id == 0 || current_trans_id == 0)
2222 		    *can_spell = (syn_buf->b_syn_spell != SYNSPL_NOTOP);
2223 		else
2224 		{
2225 		    sps.inc_tag = 0;
2226 		    sps.id = syn_buf->b_nospell_cluster_id;
2227 		    sps.cont_in_list = NULL;
2228 		    *can_spell = !in_id_list(sip, sip->si_cont_list, &sps, 0);
2229 		}
2230 	    }
2231 	    else
2232 	    {
2233 		/* The @Spell cluster is defined: Do spelling in items with
2234 		 * the @Spell cluster.  But not when @NoSpell is also there.
2235 		 * At the toplevel only spell check when ":syn spell toplevel"
2236 		 * was used. */
2237 		if (current_trans_id == 0)
2238 		    *can_spell = (syn_buf->b_syn_spell == SYNSPL_TOP);
2239 		else
2240 		{
2241 		    sps.inc_tag = 0;
2242 		    sps.id = syn_buf->b_spell_cluster_id;
2243 		    sps.cont_in_list = NULL;
2244 		    *can_spell = in_id_list(sip, sip->si_cont_list, &sps, 0);
2245 
2246 		    if (syn_buf->b_nospell_cluster_id != 0)
2247 		    {
2248 			sps.id = syn_buf->b_nospell_cluster_id;
2249 			if (in_id_list(sip, sip->si_cont_list, &sps, 0))
2250 			    *can_spell = FALSE;
2251 		    }
2252 		}
2253 	    }
2254 	}
2255 
2256 
2257 	/*
2258 	 * Check for end of current state (and the states before it) at the
2259 	 * next column.  Don't do this for syncing, because we would miss a
2260 	 * single character match.
2261 	 * First check if the current state ends at the current column.  It
2262 	 * may be for an empty match and a containing item might end in the
2263 	 * current column.
2264 	 */
2265 	if (!syncing)
2266 	{
2267 	    check_state_ends();
2268 	    if (current_state.ga_len > 0
2269 				      && syn_getcurline()[current_col] != NUL)
2270 	    {
2271 		++current_col;
2272 		check_state_ends();
2273 		--current_col;
2274 	    }
2275 	}
2276     }
2277     else if (can_spell != NULL)
2278 	/* Default: Only do spelling when there is no @Spell cluster or when
2279 	 * ":syn spell toplevel" was used. */
2280 	*can_spell = syn_buf->b_syn_spell == SYNSPL_DEFAULT
2281 		    ? (syn_buf->b_spell_cluster_id == 0)
2282 		    : (syn_buf->b_syn_spell == SYNSPL_TOP);
2283 
2284     /* nextgroup ends at end of line, unless "skipnl" or "skipemtpy" present */
2285     if (current_next_list != NULL
2286 	    && syn_getcurline()[current_col + 1] == NUL
2287 	    && !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
2288 	current_next_list = NULL;
2289 
2290     if (zero_width_next_ga.ga_len > 0)
2291 	ga_clear(&zero_width_next_ga);
2292 
2293     /* No longer need external matches.  But keep next_match_extmatch. */
2294     unref_extmatch(re_extmatch_out);
2295     re_extmatch_out = NULL;
2296     unref_extmatch(cur_extmatch);
2297 
2298     return current_attr;
2299 }
2300 
2301 
2302 /*
2303  * Check if we already matched pattern "idx" at the current column.
2304  */
2305     static int
2306 did_match_already(idx, gap)
2307     int		idx;
2308     garray_T	*gap;
2309 {
2310     int		i;
2311 
2312     for (i = current_state.ga_len; --i >= 0; )
2313 	if (CUR_STATE(i).si_m_startcol == (int)current_col
2314 		&& CUR_STATE(i).si_m_lnum == (int)current_lnum
2315 		&& CUR_STATE(i).si_idx == idx)
2316 	    return TRUE;
2317 
2318     /* Zero-width matches with a nextgroup argument are not put on the syntax
2319      * stack, and can only be matched once anyway. */
2320     for (i = gap->ga_len; --i >= 0; )
2321 	if (((int *)(gap->ga_data))[i] == idx)
2322 	    return TRUE;
2323 
2324     return FALSE;
2325 }
2326 
2327 /*
2328  * Push the next match onto the stack.
2329  */
2330     static stateitem_T *
2331 push_next_match(cur_si)
2332     stateitem_T	*cur_si;
2333 {
2334     synpat_T	*spp;
2335 
2336     spp = &(SYN_ITEMS(syn_buf)[next_match_idx]);
2337 
2338     /*
2339      * Push the item in current_state stack;
2340      */
2341     if (push_current_state(next_match_idx) == OK)
2342     {
2343 	/*
2344 	 * If it's a start-skip-end type that crosses lines, figure out how
2345 	 * much it continues in this line.  Otherwise just fill in the length.
2346 	 */
2347 	cur_si = &CUR_STATE(current_state.ga_len - 1);
2348 	cur_si->si_h_startpos = next_match_h_startpos;
2349 	cur_si->si_m_startcol = current_col;
2350 	cur_si->si_m_lnum = current_lnum;
2351 	cur_si->si_flags = spp->sp_flags;
2352 	cur_si->si_next_list = spp->sp_next_list;
2353 	cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
2354 	if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
2355 	{
2356 	    /* Try to find the end pattern in the current line */
2357 	    update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
2358 	    check_keepend();
2359 	}
2360 	else
2361 	{
2362 	    cur_si->si_m_endpos = next_match_m_endpos;
2363 	    cur_si->si_h_endpos = next_match_h_endpos;
2364 	    cur_si->si_ends = TRUE;
2365 	    cur_si->si_flags |= next_match_flags;
2366 	    cur_si->si_eoe_pos = next_match_eoe_pos;
2367 	    cur_si->si_end_idx = next_match_end_idx;
2368 	}
2369 	if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
2370 	    keepend_level = current_state.ga_len - 1;
2371 	check_keepend();
2372 	update_si_attr(current_state.ga_len - 1);
2373 
2374 	/*
2375 	 * If the start pattern has another highlight group, push another item
2376 	 * on the stack for the start pattern.
2377 	 */
2378 	if (	   spp->sp_type == SPTYPE_START
2379 		&& spp->sp_syn_match_id != 0
2380 		&& push_current_state(next_match_idx) == OK)
2381 	{
2382 	    cur_si = &CUR_STATE(current_state.ga_len - 1);
2383 	    cur_si->si_h_startpos = next_match_h_startpos;
2384 	    cur_si->si_m_startcol = current_col;
2385 	    cur_si->si_m_lnum = current_lnum;
2386 	    cur_si->si_m_endpos = next_match_eos_pos;
2387 	    cur_si->si_h_endpos = next_match_eos_pos;
2388 	    cur_si->si_ends = TRUE;
2389 	    cur_si->si_end_idx = 0;
2390 	    cur_si->si_flags = HL_MATCH;
2391 	    cur_si->si_next_list = NULL;
2392 	    check_keepend();
2393 	    update_si_attr(current_state.ga_len - 1);
2394 	}
2395     }
2396 
2397     next_match_idx = -1;	/* try other match next time */
2398 
2399     return cur_si;
2400 }
2401 
2402 /*
2403  * Check for end of current state (and the states before it).
2404  */
2405     static void
2406 check_state_ends()
2407 {
2408     stateitem_T	*cur_si;
2409     int		had_extend = FALSE;
2410 
2411     cur_si = &CUR_STATE(current_state.ga_len - 1);
2412     for (;;)
2413     {
2414 	if (cur_si->si_ends
2415 		&& (cur_si->si_m_endpos.lnum < current_lnum
2416 		    || (cur_si->si_m_endpos.lnum == current_lnum
2417 			&& cur_si->si_m_endpos.col <= current_col)))
2418 	{
2419 	    /*
2420 	     * If there is an end pattern group ID, highlight the end pattern
2421 	     * now.  No need to pop the current item from the stack.
2422 	     * Only do this if the end pattern continues beyond the current
2423 	     * position.
2424 	     */
2425 	    if (cur_si->si_end_idx
2426 		    && (cur_si->si_eoe_pos.lnum > current_lnum
2427 			|| (cur_si->si_eoe_pos.lnum == current_lnum
2428 			    && cur_si->si_eoe_pos.col > current_col)))
2429 	    {
2430 		cur_si->si_idx = cur_si->si_end_idx;
2431 		cur_si->si_end_idx = 0;
2432 		cur_si->si_m_endpos = cur_si->si_eoe_pos;
2433 		cur_si->si_h_endpos = cur_si->si_eoe_pos;
2434 		cur_si->si_flags |= HL_MATCH;
2435 		update_si_attr(current_state.ga_len - 1);
2436 
2437 		/* what matches next may be different now, clear it */
2438 		next_match_idx = 0;
2439 		next_match_col = MAXCOL;
2440 		break;
2441 	    }
2442 	    else
2443 	    {
2444 		/* handle next_list, unless at end of line and no "skipnl" or
2445 		 * "skipempty" */
2446 		current_next_list = cur_si->si_next_list;
2447 		current_next_flags = cur_si->si_flags;
2448 		if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
2449 			&& syn_getcurline()[current_col] == NUL)
2450 		    current_next_list = NULL;
2451 
2452 		/* When the ended item has "extend", another item with
2453 		 * "keepend" now needs to check for its end. */
2454 		 if (cur_si->si_flags & HL_EXTEND)
2455 		     had_extend = TRUE;
2456 
2457 		pop_current_state();
2458 
2459 		if (current_state.ga_len == 0)
2460 		    break;
2461 
2462 		if (had_extend)
2463 		{
2464 		    syn_update_ends(FALSE);
2465 		    if (current_state.ga_len == 0)
2466 			break;
2467 		}
2468 
2469 		cur_si = &CUR_STATE(current_state.ga_len - 1);
2470 
2471 		/*
2472 		 * Only for a region the search for the end continues after
2473 		 * the end of the contained item.  If the contained match
2474 		 * included the end-of-line, break here, the region continues.
2475 		 * Don't do this when:
2476 		 * - "keepend" is used for the contained item
2477 		 * - not at the end of the line (could be end="x$"me=e-1).
2478 		 * - "excludenl" is used (HL_HAS_EOL won't be set)
2479 		 */
2480 		if (cur_si->si_idx >= 0
2481 			&& SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_type
2482 							       == SPTYPE_START
2483 			&& !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
2484 		{
2485 		    update_si_end(cur_si, (int)current_col, TRUE);
2486 		    check_keepend();
2487 		    if ((current_next_flags & HL_HAS_EOL)
2488 			    && keepend_level < 0
2489 			    && syn_getcurline()[current_col] == NUL)
2490 			break;
2491 		}
2492 	    }
2493 	}
2494 	else
2495 	    break;
2496     }
2497 }
2498 
2499 /*
2500  * Update an entry in the current_state stack for a match or region.  This
2501  * fills in si_attr, si_next_list and si_cont_list.
2502  */
2503     static void
2504 update_si_attr(idx)
2505     int	    idx;
2506 {
2507     stateitem_T	*sip = &CUR_STATE(idx);
2508     synpat_T	*spp;
2509 
2510     spp = &(SYN_ITEMS(syn_buf)[sip->si_idx]);
2511     if (sip->si_flags & HL_MATCH)
2512 	sip->si_id = spp->sp_syn_match_id;
2513     else
2514 	sip->si_id = spp->sp_syn.id;
2515     sip->si_attr = syn_id2attr(sip->si_id);
2516     sip->si_trans_id = sip->si_id;
2517     if (sip->si_flags & HL_MATCH)
2518 	sip->si_cont_list = NULL;
2519     else
2520 	sip->si_cont_list = spp->sp_cont_list;
2521 
2522     /*
2523      * For transparent items, take attr from outer item.
2524      * Also take cont_list, if there is none.
2525      * Don't do this for the matchgroup of a start or end pattern.
2526      */
2527     if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
2528     {
2529 	if (idx == 0)
2530 	{
2531 	    sip->si_attr = 0;
2532 	    sip->si_trans_id = 0;
2533 	    if (sip->si_cont_list == NULL)
2534 		sip->si_cont_list = ID_LIST_ALL;
2535 	}
2536 	else
2537 	{
2538 	    sip->si_attr = CUR_STATE(idx - 1).si_attr;
2539 	    sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
2540 	    sip->si_h_startpos = CUR_STATE(idx - 1).si_h_startpos;
2541 	    sip->si_h_endpos = CUR_STATE(idx - 1).si_h_endpos;
2542 	    if (sip->si_cont_list == NULL)
2543 	    {
2544 		sip->si_flags |= HL_TRANS_CONT;
2545 		sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
2546 	    }
2547 	}
2548     }
2549 }
2550 
2551 /*
2552  * Check the current stack for patterns with "keepend" flag.
2553  * Propagate the match-end to contained items, until a "skipend" item is found.
2554  */
2555     static void
2556 check_keepend()
2557 {
2558     int		i;
2559     lpos_T	maxpos;
2560     stateitem_T	*sip;
2561 
2562     /*
2563      * This check can consume a lot of time; only do it from the level where
2564      * there really is a keepend.
2565      */
2566     if (keepend_level < 0)
2567 	return;
2568 
2569     /*
2570      * Find the last index of an "extend" item.  "keepend" items before that
2571      * won't do anything.  If there is no "extend" item "i" will be
2572      * "keepend_level" and all "keepend" items will work normally.
2573      */
2574     for (i = current_state.ga_len - 1; i > keepend_level; --i)
2575 	if (CUR_STATE(i).si_flags & HL_EXTEND)
2576 	    break;
2577 
2578     maxpos.lnum = 0;
2579     for ( ; i < current_state.ga_len; ++i)
2580     {
2581 	sip = &CUR_STATE(i);
2582 	if (maxpos.lnum != 0)
2583 	{
2584 	    limit_pos_zero(&sip->si_m_endpos, &maxpos);
2585 	    limit_pos_zero(&sip->si_h_endpos, &maxpos);
2586 	    limit_pos_zero(&sip->si_eoe_pos, &maxpos);
2587 	    sip->si_ends = TRUE;
2588 	}
2589 	if (sip->si_ends
2590 		&& (sip->si_flags & HL_KEEPEND)
2591 		&& (maxpos.lnum == 0
2592 		    || maxpos.lnum > sip->si_m_endpos.lnum
2593 		    || (maxpos.lnum == sip->si_m_endpos.lnum
2594 			&& maxpos.col > sip->si_m_endpos.col)))
2595 	    maxpos = sip->si_m_endpos;
2596     }
2597 }
2598 
2599 /*
2600  * Update an entry in the current_state stack for a start-skip-end pattern.
2601  * This finds the end of the current item, if it's in the current line.
2602  *
2603  * Return the flags for the matched END.
2604  */
2605     static void
2606 update_si_end(sip, startcol, force)
2607     stateitem_T	*sip;
2608     int		startcol;   /* where to start searching for the end */
2609     int		force;	    /* when TRUE overrule a previous end */
2610 {
2611     lpos_T	startpos;
2612     lpos_T	endpos;
2613     lpos_T	hl_endpos;
2614     lpos_T	end_endpos;
2615     int		end_idx;
2616 
2617     /* Don't update when it's already done.  Can be a match of an end pattern
2618      * that started in a previous line.  Watch out: can also be a "keepend"
2619      * from a containing item. */
2620     if (!force && sip->si_m_endpos.lnum >= current_lnum)
2621 	return;
2622 
2623     /*
2624      * We need to find the end of the region.  It may continue in the next
2625      * line.
2626      */
2627     end_idx = 0;
2628     startpos.lnum = current_lnum;
2629     startpos.col = startcol;
2630     find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
2631 		   &(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
2632 
2633     if (endpos.lnum == 0)
2634     {
2635 	/* No end pattern matched. */
2636 	if (SYN_ITEMS(syn_buf)[sip->si_idx].sp_flags & HL_ONELINE)
2637 	{
2638 	    /* a "oneline" never continues in the next line */
2639 	    sip->si_ends = TRUE;
2640 	    sip->si_m_endpos.lnum = current_lnum;
2641 	    sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
2642 	}
2643 	else
2644 	{
2645 	    /* continues in the next line */
2646 	    sip->si_ends = FALSE;
2647 	    sip->si_m_endpos.lnum = 0;
2648 	}
2649 	sip->si_h_endpos = sip->si_m_endpos;
2650     }
2651     else
2652     {
2653 	/* match within this line */
2654 	sip->si_m_endpos = endpos;
2655 	sip->si_h_endpos = hl_endpos;
2656 	sip->si_eoe_pos = end_endpos;
2657 	sip->si_ends = TRUE;
2658 	sip->si_end_idx = end_idx;
2659     }
2660 }
2661 
2662 /*
2663  * Add a new state to the current state stack.
2664  * It is cleared and the index set to "idx".
2665  * Return FAIL if it's not possible (out of memory).
2666  */
2667     static int
2668 push_current_state(idx)
2669     int	    idx;
2670 {
2671     if (ga_grow(&current_state, 1) == FAIL)
2672 	return FAIL;
2673     vim_memset(&CUR_STATE(current_state.ga_len), 0, sizeof(stateitem_T));
2674     CUR_STATE(current_state.ga_len).si_idx = idx;
2675     ++current_state.ga_len;
2676     return OK;
2677 }
2678 
2679 /*
2680  * Remove a state from the current_state stack.
2681  */
2682     static void
2683 pop_current_state()
2684 {
2685     if (current_state.ga_len)
2686     {
2687 	unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
2688 	--current_state.ga_len;
2689     }
2690     /* after the end of a pattern, try matching a keyword or pattern */
2691     next_match_idx = -1;
2692 
2693     /* if first state with "keepend" is popped, reset keepend_level */
2694     if (keepend_level >= current_state.ga_len)
2695 	keepend_level = -1;
2696 }
2697 
2698 /*
2699  * Find the end of a start/skip/end syntax region after "startpos".
2700  * Only checks one line.
2701  * Also handles a match item that continued from a previous line.
2702  * If not found, the syntax item continues in the next line.  m_endpos->lnum
2703  * will be 0.
2704  * If found, the end of the region and the end of the highlighting is
2705  * computed.
2706  */
2707     static void
2708 find_endpos(idx, startpos, m_endpos, hl_endpos, flagsp, end_endpos,
2709 							   end_idx, start_ext)
2710     int		idx;		/* index of the pattern */
2711     lpos_T	*startpos;	/* where to start looking for an END match */
2712     lpos_T	*m_endpos;	/* return: end of match */
2713     lpos_T	*hl_endpos;	/* return: end of highlighting */
2714     long	*flagsp;	/* return: flags of matching END */
2715     lpos_T	*end_endpos;	/* return: end of end pattern match */
2716     int		*end_idx;	/* return: group ID for end pat. match, or 0 */
2717     reg_extmatch_T *start_ext;	/* submatches from the start pattern */
2718 {
2719     colnr_T	matchcol;
2720     synpat_T	*spp, *spp_skip;
2721     int		start_idx;
2722     int		best_idx;
2723     regmmatch_T	regmatch;
2724     regmmatch_T	best_regmatch;	    /* startpos/endpos of best match */
2725     lpos_T	pos;
2726     char_u	*line;
2727     int		had_match = FALSE;
2728 
2729     /*
2730      * Check for being called with a START pattern.
2731      * Can happen with a match that continues to the next line, because it
2732      * contained a region.
2733      */
2734     spp = &(SYN_ITEMS(syn_buf)[idx]);
2735     if (spp->sp_type != SPTYPE_START)
2736     {
2737 	*hl_endpos = *startpos;
2738 	return;
2739     }
2740 
2741     /*
2742      * Find the SKIP or first END pattern after the last START pattern.
2743      */
2744     for (;;)
2745     {
2746 	spp = &(SYN_ITEMS(syn_buf)[idx]);
2747 	if (spp->sp_type != SPTYPE_START)
2748 	    break;
2749 	++idx;
2750     }
2751 
2752     /*
2753      *	Lookup the SKIP pattern (if present)
2754      */
2755     if (spp->sp_type == SPTYPE_SKIP)
2756     {
2757 	spp_skip = spp;
2758 	++idx;
2759     }
2760     else
2761 	spp_skip = NULL;
2762 
2763     /* Setup external matches for syn_regexec(). */
2764     unref_extmatch(re_extmatch_in);
2765     re_extmatch_in = ref_extmatch(start_ext);
2766 
2767     matchcol = startpos->col;	/* start looking for a match at sstart */
2768     start_idx = idx;		/* remember the first END pattern. */
2769     best_regmatch.startpos[0].col = 0;		/* avoid compiler warning */
2770     for (;;)
2771     {
2772 	/*
2773 	 * Find end pattern that matches first after "matchcol".
2774 	 */
2775 	best_idx = -1;
2776 	for (idx = start_idx; idx < syn_buf->b_syn_patterns.ga_len; ++idx)
2777 	{
2778 	    int lc_col = matchcol;
2779 
2780 	    spp = &(SYN_ITEMS(syn_buf)[idx]);
2781 	    if (spp->sp_type != SPTYPE_END)	/* past last END pattern */
2782 		break;
2783 	    lc_col -= spp->sp_offsets[SPO_LC_OFF];
2784 	    if (lc_col < 0)
2785 		lc_col = 0;
2786 
2787 	    regmatch.rmm_ic = spp->sp_ic;
2788 	    regmatch.regprog = spp->sp_prog;
2789 	    if (syn_regexec(&regmatch, startpos->lnum, lc_col))
2790 	    {
2791 		if (best_idx == -1 || regmatch.startpos[0].col
2792 					      < best_regmatch.startpos[0].col)
2793 		{
2794 		    best_idx = idx;
2795 		    best_regmatch.startpos[0] = regmatch.startpos[0];
2796 		    best_regmatch.endpos[0] = regmatch.endpos[0];
2797 		}
2798 	    }
2799 	}
2800 
2801 	/*
2802 	 * If all end patterns have been tried, and there is no match, the
2803 	 * item continues until end-of-line.
2804 	 */
2805 	if (best_idx == -1)
2806 	    break;
2807 
2808 	/*
2809 	 * If the skip pattern matches before the end pattern,
2810 	 * continue searching after the skip pattern.
2811 	 */
2812 	if (spp_skip != NULL)
2813 	{
2814 	    int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
2815 
2816 	    if (lc_col < 0)
2817 		lc_col = 0;
2818 	    regmatch.rmm_ic = spp_skip->sp_ic;
2819 	    regmatch.regprog = spp_skip->sp_prog;
2820 	    if (syn_regexec(&regmatch, startpos->lnum, lc_col)
2821 		    && regmatch.startpos[0].col
2822 					     <= best_regmatch.startpos[0].col)
2823 	    {
2824 		/* Add offset to skip pattern match */
2825 		syn_add_end_off(&pos, &regmatch, spp_skip, SPO_ME_OFF, 1);
2826 
2827 		/* If the skip pattern goes on to the next line, there is no
2828 		 * match with an end pattern in this line. */
2829 		if (pos.lnum > startpos->lnum)
2830 		    break;
2831 
2832 		line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
2833 
2834 		/* take care of an empty match or negative offset */
2835 		if (pos.col <= matchcol)
2836 		    ++matchcol;
2837 		else if (pos.col <= regmatch.endpos[0].col)
2838 		    matchcol = pos.col;
2839 		else
2840 		    /* Be careful not to jump over the NUL at the end-of-line */
2841 		    for (matchcol = regmatch.endpos[0].col;
2842 			    line[matchcol] != NUL && matchcol < pos.col;
2843 								   ++matchcol)
2844 			;
2845 
2846 		/* if the skip pattern includes end-of-line, break here */
2847 		if (line[matchcol] == NUL)
2848 		    break;
2849 
2850 		continue;	    /* start with first end pattern again */
2851 	    }
2852 	}
2853 
2854 	/*
2855 	 * Match from start pattern to end pattern.
2856 	 * Correct for match and highlight offset of end pattern.
2857 	 */
2858 	spp = &(SYN_ITEMS(syn_buf)[best_idx]);
2859 	syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
2860 	/* can't end before the start */
2861 	if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
2862 	    m_endpos->col = startpos->col;
2863 
2864 	syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
2865 	/* can't end before the start */
2866 	if (end_endpos->lnum == startpos->lnum
2867 					   && end_endpos->col < startpos->col)
2868 	    end_endpos->col = startpos->col;
2869 	/* can't end after the match */
2870 	limit_pos(end_endpos, m_endpos);
2871 
2872 	/*
2873 	 * If the end group is highlighted differently, adjust the pointers.
2874 	 */
2875 	if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
2876 	{
2877 	    *end_idx = best_idx;
2878 	    if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
2879 	    {
2880 		hl_endpos->lnum = best_regmatch.endpos[0].lnum;
2881 		hl_endpos->col = best_regmatch.endpos[0].col;
2882 	    }
2883 	    else
2884 	    {
2885 		hl_endpos->lnum = best_regmatch.startpos[0].lnum;
2886 		hl_endpos->col = best_regmatch.startpos[0].col;
2887 	    }
2888 	    hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
2889 
2890 	    /* can't end before the start */
2891 	    if (hl_endpos->lnum == startpos->lnum
2892 					    && hl_endpos->col < startpos->col)
2893 		hl_endpos->col = startpos->col;
2894 	    limit_pos(hl_endpos, m_endpos);
2895 
2896 	    /* now the match ends where the highlighting ends, it is turned
2897 	     * into the matchgroup for the end */
2898 	    *m_endpos = *hl_endpos;
2899 	}
2900 	else
2901 	{
2902 	    *end_idx = 0;
2903 	    *hl_endpos = *end_endpos;
2904 	}
2905 
2906 	*flagsp = spp->sp_flags;
2907 
2908 	had_match = TRUE;
2909 	break;
2910     }
2911 
2912     /* no match for an END pattern in this line */
2913     if (!had_match)
2914 	m_endpos->lnum = 0;
2915 
2916     /* Remove external matches. */
2917     unref_extmatch(re_extmatch_in);
2918     re_extmatch_in = NULL;
2919 }
2920 
2921 /*
2922  * Limit "pos" not to be after "limit".
2923  */
2924     static void
2925 limit_pos(pos, limit)
2926     lpos_T	*pos;
2927     lpos_T	*limit;
2928 {
2929     if (pos->lnum > limit->lnum)
2930 	*pos = *limit;
2931     else if (pos->lnum == limit->lnum && pos->col > limit->col)
2932 	pos->col = limit->col;
2933 }
2934 
2935 /*
2936  * Limit "pos" not to be after "limit", unless pos->lnum is zero.
2937  */
2938     static void
2939 limit_pos_zero(pos, limit)
2940     lpos_T	*pos;
2941     lpos_T	*limit;
2942 {
2943     if (pos->lnum == 0)
2944 	*pos = *limit;
2945     else
2946 	limit_pos(pos, limit);
2947 }
2948 
2949 /*
2950  * Add offset to matched text for end of match or highlight.
2951  */
2952     static void
2953 syn_add_end_off(result, regmatch, spp, idx, extra)
2954     lpos_T	*result;	/* returned position */
2955     regmmatch_T	*regmatch;	/* start/end of match */
2956     synpat_T	*spp;		/* matched pattern */
2957     int		idx;		/* index of offset */
2958     int		extra;		/* extra chars for offset to start */
2959 {
2960     int		col;
2961     int		len;
2962 
2963     if (spp->sp_off_flags & (1 << idx))
2964     {
2965 	result->lnum = regmatch->startpos[0].lnum;
2966 	col = regmatch->startpos[0].col + extra;
2967     }
2968     else
2969     {
2970 	result->lnum = regmatch->endpos[0].lnum;
2971 	col = regmatch->endpos[0].col;
2972     }
2973     col += spp->sp_offsets[idx];
2974     if (col < 0)
2975 	result->col = 0;
2976     else
2977     {
2978 	/* Don't go past the end of the line.  Matters for "rs=e+2" when there
2979 	 * is a matchgroup. Watch out for match with last NL in the buffer. */
2980 	if (result->lnum > syn_buf->b_ml.ml_line_count)
2981 	    len = 0;
2982 	else
2983 	    len = STRLEN(ml_get_buf(syn_buf, result->lnum, FALSE));
2984 	if (col > len)
2985 	    result->col = len;
2986 	else
2987 	    result->col = col;
2988     }
2989 }
2990 
2991 /*
2992  * Add offset to matched text for start of match or highlight.
2993  * Avoid resulting column to become negative.
2994  */
2995     static void
2996 syn_add_start_off(result, regmatch, spp, idx, extra)
2997     lpos_T	*result;	/* returned position */
2998     regmmatch_T	*regmatch;	/* start/end of match */
2999     synpat_T	*spp;
3000     int		idx;
3001     int		extra;	    /* extra chars for offset to end */
3002 {
3003     int		col;
3004 
3005     if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
3006     {
3007 	result->lnum = regmatch->endpos[0].lnum;
3008 	col = regmatch->endpos[0].col + extra;
3009     }
3010     else
3011     {
3012 	result->lnum = regmatch->startpos[0].lnum;
3013 	col = regmatch->startpos[0].col;
3014     }
3015     col += spp->sp_offsets[idx];
3016     if (col < 0)
3017 	result->col = 0;
3018     else
3019 	result->col = col;
3020 }
3021 
3022 /*
3023  * Get current line in syntax buffer.
3024  */
3025     static char_u *
3026 syn_getcurline()
3027 {
3028     return ml_get_buf(syn_buf, current_lnum, FALSE);
3029 }
3030 
3031 /*
3032  * Call vim_regexec() to find a match with "rmp" in "syn_buf".
3033  * Returns TRUE when there is a match.
3034  */
3035     static int
3036 syn_regexec(rmp, lnum, col)
3037     regmmatch_T	*rmp;
3038     linenr_T	lnum;
3039     colnr_T	col;
3040 {
3041     rmp->rmm_maxcol = syn_buf->b_p_smc;
3042     if (vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col) > 0)
3043     {
3044 	rmp->startpos[0].lnum += lnum;
3045 	rmp->endpos[0].lnum += lnum;
3046 	return TRUE;
3047     }
3048     return FALSE;
3049 }
3050 
3051 /*
3052  * Check one position in a line for a matching keyword.
3053  * The caller must check if a keyword can start at startcol.
3054  * Return it's ID if found, 0 otherwise.
3055  */
3056     static int
3057 check_keyword_id(line, startcol, endcolp, flagsp, next_listp, cur_si)
3058     char_u	*line;
3059     int		startcol;	/* position in line to check for keyword */
3060     int		*endcolp;	/* return: character after found keyword */
3061     long	*flagsp;	/* return: flags of matching keyword */
3062     short	**next_listp;	/* return: next_list of matching keyword */
3063     stateitem_T	*cur_si;	/* item at the top of the stack */
3064 {
3065     keyentry_T	*kp;
3066     char_u	*kwp;
3067     int		round;
3068     int		kwlen;
3069     char_u	keyword[MAXKEYWLEN + 1]; /* assume max. keyword len is 80 */
3070     hashtab_T	*ht;
3071     hashitem_T	*hi;
3072 
3073     /* Find first character after the keyword.  First character was already
3074      * checked. */
3075     kwp = line + startcol;
3076     kwlen = 0;
3077     do
3078     {
3079 #ifdef FEAT_MBYTE
3080 	if (has_mbyte)
3081 	    kwlen += (*mb_ptr2len)(kwp + kwlen);
3082 	else
3083 #endif
3084 	    ++kwlen;
3085     }
3086     while (vim_iswordc_buf(kwp + kwlen, syn_buf));
3087 
3088     if (kwlen > MAXKEYWLEN)
3089 	return 0;
3090 
3091     /*
3092      * Must make a copy of the keyword, so we can add a NUL and make it
3093      * lowercase.
3094      */
3095     vim_strncpy(keyword, kwp, kwlen);
3096 
3097     /*
3098      * Try twice:
3099      * 1. matching case
3100      * 2. ignoring case
3101      */
3102     for (round = 1; round <= 2; ++round)
3103     {
3104 	ht = round == 1 ? &syn_buf->b_keywtab : &syn_buf->b_keywtab_ic;
3105 	if (ht->ht_used == 0)
3106 	    continue;
3107 	if (round == 2)	/* ignore case */
3108 	    (void)str_foldcase(kwp, kwlen, keyword, MAXKEYWLEN + 1);
3109 
3110 	/*
3111 	 * Find keywords that match.  There can be several with different
3112 	 * attributes.
3113 	 * When current_next_list is non-zero accept only that group, otherwise:
3114 	 *  Accept a not-contained keyword at toplevel.
3115 	 *  Accept a keyword at other levels only if it is in the contains list.
3116 	 */
3117 	hi = hash_find(ht, keyword);
3118 	if (!HASHITEM_EMPTY(hi))
3119 	    for (kp = HI2KE(hi); kp != NULL; kp = kp->ke_next)
3120 	    {
3121 		if (current_next_list != 0
3122 			? in_id_list(NULL, current_next_list, &kp->k_syn, 0)
3123 			: (cur_si == NULL
3124 			    ? !(kp->flags & HL_CONTAINED)
3125 			    : in_id_list(cur_si, cur_si->si_cont_list,
3126 				      &kp->k_syn, kp->flags & HL_CONTAINED)))
3127 		{
3128 		    *endcolp = startcol + kwlen;
3129 		    *flagsp = kp->flags;
3130 		    *next_listp = kp->next_list;
3131 		    return kp->k_syn.id;
3132 		}
3133 	    }
3134     }
3135     return 0;
3136 }
3137 
3138 /*
3139  * Handle ":syntax case" command.
3140  */
3141 /* ARGSUSED */
3142     static void
3143 syn_cmd_case(eap, syncing)
3144     exarg_T	*eap;
3145     int		syncing;	    /* not used */
3146 {
3147     char_u	*arg = eap->arg;
3148     char_u	*next;
3149 
3150     eap->nextcmd = find_nextcmd(arg);
3151     if (eap->skip)
3152 	return;
3153 
3154     next = skiptowhite(arg);
3155     if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
3156 	curbuf->b_syn_ic = FALSE;
3157     else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
3158 	curbuf->b_syn_ic = TRUE;
3159     else
3160 	EMSG2(_("E390: Illegal argument: %s"), arg);
3161 }
3162 
3163 /*
3164  * Handle ":syntax spell" command.
3165  */
3166 /* ARGSUSED */
3167     static void
3168 syn_cmd_spell(eap, syncing)
3169     exarg_T	*eap;
3170     int		syncing;	    /* not used */
3171 {
3172     char_u	*arg = eap->arg;
3173     char_u	*next;
3174 
3175     eap->nextcmd = find_nextcmd(arg);
3176     if (eap->skip)
3177 	return;
3178 
3179     next = skiptowhite(arg);
3180     if (STRNICMP(arg, "toplevel", 8) == 0 && next - arg == 8)
3181 	curbuf->b_syn_spell = SYNSPL_TOP;
3182     else if (STRNICMP(arg, "notoplevel", 10) == 0 && next - arg == 10)
3183 	curbuf->b_syn_spell = SYNSPL_NOTOP;
3184     else if (STRNICMP(arg, "default", 4) == 0 && next - arg == 4)
3185 	curbuf->b_syn_spell = SYNSPL_DEFAULT;
3186     else
3187 	EMSG2(_("E390: Illegal argument: %s"), arg);
3188 }
3189 
3190 /*
3191  * Clear all syntax info for one buffer.
3192  */
3193     void
3194 syntax_clear(buf)
3195     buf_T	*buf;
3196 {
3197     int i;
3198 
3199     buf->b_syn_error = FALSE;	    /* clear previous error */
3200     buf->b_syn_ic = FALSE;	    /* Use case, by default */
3201     buf->b_syn_spell = SYNSPL_DEFAULT; /* default spell checking */
3202     buf->b_syn_containedin = FALSE;
3203 
3204     /* free the keywords */
3205     clear_keywtab(&buf->b_keywtab);
3206     clear_keywtab(&buf->b_keywtab_ic);
3207 
3208     /* free the syntax patterns */
3209     for (i = buf->b_syn_patterns.ga_len; --i >= 0; )
3210 	syn_clear_pattern(buf, i);
3211     ga_clear(&buf->b_syn_patterns);
3212 
3213     /* free the syntax clusters */
3214     for (i = buf->b_syn_clusters.ga_len; --i >= 0; )
3215 	syn_clear_cluster(buf, i);
3216     ga_clear(&buf->b_syn_clusters);
3217     buf->b_spell_cluster_id = 0;
3218     buf->b_nospell_cluster_id = 0;
3219 
3220     buf->b_syn_sync_flags = 0;
3221     buf->b_syn_sync_minlines = 0;
3222     buf->b_syn_sync_maxlines = 0;
3223     buf->b_syn_sync_linebreaks = 0;
3224 
3225     vim_free(buf->b_syn_linecont_prog);
3226     buf->b_syn_linecont_prog = NULL;
3227     vim_free(buf->b_syn_linecont_pat);
3228     buf->b_syn_linecont_pat = NULL;
3229 #ifdef FEAT_FOLDING
3230     buf->b_syn_folditems = 0;
3231 #endif
3232 
3233     /* free the stored states */
3234     syn_stack_free_all(buf);
3235     invalidate_current_state();
3236 }
3237 
3238 /*
3239  * Clear syncing info for one buffer.
3240  */
3241     static void
3242 syntax_sync_clear()
3243 {
3244     int i;
3245 
3246     /* free the syntax patterns */
3247     for (i = curbuf->b_syn_patterns.ga_len; --i >= 0; )
3248 	if (SYN_ITEMS(curbuf)[i].sp_syncing)
3249 	    syn_remove_pattern(curbuf, i);
3250 
3251     curbuf->b_syn_sync_flags = 0;
3252     curbuf->b_syn_sync_minlines = 0;
3253     curbuf->b_syn_sync_maxlines = 0;
3254     curbuf->b_syn_sync_linebreaks = 0;
3255 
3256     vim_free(curbuf->b_syn_linecont_prog);
3257     curbuf->b_syn_linecont_prog = NULL;
3258     vim_free(curbuf->b_syn_linecont_pat);
3259     curbuf->b_syn_linecont_pat = NULL;
3260 
3261     syn_stack_free_all(curbuf);		/* Need to recompute all syntax. */
3262 }
3263 
3264 /*
3265  * Remove one pattern from the buffer's pattern list.
3266  */
3267     static void
3268 syn_remove_pattern(buf, idx)
3269     buf_T	*buf;
3270     int		idx;
3271 {
3272     synpat_T	*spp;
3273 
3274     spp = &(SYN_ITEMS(buf)[idx]);
3275 #ifdef FEAT_FOLDING
3276     if (spp->sp_flags & HL_FOLD)
3277 	--buf->b_syn_folditems;
3278 #endif
3279     syn_clear_pattern(buf, idx);
3280     mch_memmove(spp, spp + 1,
3281 		   sizeof(synpat_T) * (buf->b_syn_patterns.ga_len - idx - 1));
3282     --buf->b_syn_patterns.ga_len;
3283 }
3284 
3285 /*
3286  * Clear and free one syntax pattern.  When clearing all, must be called from
3287  * last to first!
3288  */
3289     static void
3290 syn_clear_pattern(buf, i)
3291     buf_T	*buf;
3292     int		i;
3293 {
3294     vim_free(SYN_ITEMS(buf)[i].sp_pattern);
3295     vim_free(SYN_ITEMS(buf)[i].sp_prog);
3296     /* Only free sp_cont_list and sp_next_list of first start pattern */
3297     if (i == 0 || SYN_ITEMS(buf)[i - 1].sp_type != SPTYPE_START)
3298     {
3299 	vim_free(SYN_ITEMS(buf)[i].sp_cont_list);
3300 	vim_free(SYN_ITEMS(buf)[i].sp_next_list);
3301     }
3302 }
3303 
3304 /*
3305  * Clear and free one syntax cluster.
3306  */
3307     static void
3308 syn_clear_cluster(buf, i)
3309     buf_T	*buf;
3310     int		i;
3311 {
3312     vim_free(SYN_CLSTR(buf)[i].scl_name);
3313     vim_free(SYN_CLSTR(buf)[i].scl_name_u);
3314     vim_free(SYN_CLSTR(buf)[i].scl_list);
3315 }
3316 
3317 /*
3318  * Handle ":syntax clear" command.
3319  */
3320     static void
3321 syn_cmd_clear(eap, syncing)
3322     exarg_T	*eap;
3323     int		syncing;
3324 {
3325     char_u	*arg = eap->arg;
3326     char_u	*arg_end;
3327     int		id;
3328 
3329     eap->nextcmd = find_nextcmd(arg);
3330     if (eap->skip)
3331 	return;
3332 
3333     /*
3334      * We have to disable this within ":syn include @group filename",
3335      * because otherwise @group would get deleted.
3336      * Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
3337      * clear".
3338      */
3339     if (curbuf->b_syn_topgrp != 0)
3340 	return;
3341 
3342     if (ends_excmd(*arg))
3343     {
3344 	/*
3345 	 * No argument: Clear all syntax items.
3346 	 */
3347 	if (syncing)
3348 	    syntax_sync_clear();
3349 	else
3350 	{
3351 	    syntax_clear(curbuf);
3352 	    do_unlet((char_u *)"b:current_syntax", TRUE);
3353 	}
3354     }
3355     else
3356     {
3357 	/*
3358 	 * Clear the group IDs that are in the argument.
3359 	 */
3360 	while (!ends_excmd(*arg))
3361 	{
3362 	    arg_end = skiptowhite(arg);
3363 	    if (*arg == '@')
3364 	    {
3365 		id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3366 		if (id == 0)
3367 		{
3368 		    EMSG2(_("E391: No such syntax cluster: %s"), arg);
3369 		    break;
3370 		}
3371 		else
3372 		{
3373 		    /*
3374 		     * We can't physically delete a cluster without changing
3375 		     * the IDs of other clusters, so we do the next best thing
3376 		     * and make it empty.
3377 		     */
3378 		    short scl_id = id - SYNID_CLUSTER;
3379 
3380 		    vim_free(SYN_CLSTR(curbuf)[scl_id].scl_list);
3381 		    SYN_CLSTR(curbuf)[scl_id].scl_list = NULL;
3382 		}
3383 	    }
3384 	    else
3385 	    {
3386 		id = syn_namen2id(arg, (int)(arg_end - arg));
3387 		if (id == 0)
3388 		{
3389 		    EMSG2(_(e_nogroup), arg);
3390 		    break;
3391 		}
3392 		else
3393 		    syn_clear_one(id, syncing);
3394 	    }
3395 	    arg = skipwhite(arg_end);
3396 	}
3397     }
3398     redraw_curbuf_later(SOME_VALID);
3399     syn_stack_free_all(curbuf);		/* Need to recompute all syntax. */
3400 }
3401 
3402 /*
3403  * Clear one syntax group for the current buffer.
3404  */
3405     static void
3406 syn_clear_one(id, syncing)
3407     int		id;
3408     int		syncing;
3409 {
3410     synpat_T	*spp;
3411     int		idx;
3412 
3413     /* Clear keywords only when not ":syn sync clear group-name" */
3414     if (!syncing)
3415     {
3416 	(void)syn_clear_keyword(id, &curbuf->b_keywtab);
3417 	(void)syn_clear_keyword(id, &curbuf->b_keywtab_ic);
3418     }
3419 
3420     /* clear the patterns for "id" */
3421     for (idx = curbuf->b_syn_patterns.ga_len; --idx >= 0; )
3422     {
3423 	spp = &(SYN_ITEMS(curbuf)[idx]);
3424 	if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3425 	    continue;
3426 	syn_remove_pattern(curbuf, idx);
3427     }
3428 }
3429 
3430 /*
3431  * Handle ":syntax on" command.
3432  */
3433 /* ARGSUSED */
3434     static void
3435 syn_cmd_on(eap, syncing)
3436     exarg_T	*eap;
3437     int		syncing;	/* not used */
3438 {
3439     syn_cmd_onoff(eap, "syntax");
3440 }
3441 
3442 /*
3443  * Handle ":syntax enable" command.
3444  */
3445 /* ARGSUSED */
3446     static void
3447 syn_cmd_enable(eap, syncing)
3448     exarg_T	*eap;
3449     int		syncing;	/* not used */
3450 {
3451     set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"enable");
3452     syn_cmd_onoff(eap, "syntax");
3453     do_unlet((char_u *)"g:syntax_cmd", TRUE);
3454 }
3455 
3456 /*
3457  * Handle ":syntax reset" command.
3458  */
3459 /* ARGSUSED */
3460     static void
3461 syn_cmd_reset(eap, syncing)
3462     exarg_T	*eap;
3463     int		syncing;	/* not used */
3464 {
3465     eap->nextcmd = check_nextcmd(eap->arg);
3466     if (!eap->skip)
3467     {
3468 	set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"reset");
3469 	do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
3470 	do_unlet((char_u *)"g:syntax_cmd", TRUE);
3471     }
3472 }
3473 
3474 /*
3475  * Handle ":syntax manual" command.
3476  */
3477 /* ARGSUSED */
3478     static void
3479 syn_cmd_manual(eap, syncing)
3480     exarg_T	*eap;
3481     int		syncing;	/* not used */
3482 {
3483     syn_cmd_onoff(eap, "manual");
3484 }
3485 
3486 /*
3487  * Handle ":syntax off" command.
3488  */
3489 /* ARGSUSED */
3490     static void
3491 syn_cmd_off(eap, syncing)
3492     exarg_T	*eap;
3493     int		syncing;	/* not used */
3494 {
3495     syn_cmd_onoff(eap, "nosyntax");
3496 }
3497 
3498     static void
3499 syn_cmd_onoff(eap, name)
3500     exarg_T	*eap;
3501     char	*name;
3502 {
3503     char_u	buf[100];
3504 
3505     eap->nextcmd = check_nextcmd(eap->arg);
3506     if (!eap->skip)
3507     {
3508 	STRCPY(buf, "so ");
3509 	vim_snprintf((char *)buf + 3, sizeof(buf) - 3, SYNTAX_FNAME, name);
3510 	do_cmdline_cmd(buf);
3511     }
3512 }
3513 
3514 /*
3515  * Handle ":syntax [list]" command: list current syntax words.
3516  */
3517     static void
3518 syn_cmd_list(eap, syncing)
3519     exarg_T	*eap;
3520     int		syncing;	    /* when TRUE: list syncing items */
3521 {
3522     char_u	*arg = eap->arg;
3523     int		id;
3524     char_u	*arg_end;
3525 
3526     eap->nextcmd = find_nextcmd(arg);
3527     if (eap->skip)
3528 	return;
3529 
3530     if (!syntax_present(curbuf))
3531     {
3532 	MSG(_("No Syntax items defined for this buffer"));
3533 	return;
3534     }
3535 
3536     if (syncing)
3537     {
3538 	if (curbuf->b_syn_sync_flags & SF_CCOMMENT)
3539 	{
3540 	    MSG_PUTS(_("syncing on C-style comments"));
3541 	    syn_lines_msg();
3542 	    syn_match_msg();
3543 	    return;
3544 	}
3545 	else if (!(curbuf->b_syn_sync_flags & SF_MATCH))
3546 	{
3547 	    if (curbuf->b_syn_sync_minlines == 0)
3548 		MSG_PUTS(_("no syncing"));
3549 	    else
3550 	    {
3551 		MSG_PUTS(_("syncing starts "));
3552 		msg_outnum(curbuf->b_syn_sync_minlines);
3553 		MSG_PUTS(_(" lines before top line"));
3554 		syn_match_msg();
3555 	    }
3556 	    return;
3557 	}
3558 	MSG_PUTS_TITLE(_("\n--- Syntax sync items ---"));
3559 	if (curbuf->b_syn_sync_minlines > 0
3560 		|| curbuf->b_syn_sync_maxlines > 0
3561 		|| curbuf->b_syn_sync_linebreaks > 0)
3562 	{
3563 	    MSG_PUTS(_("\nsyncing on items"));
3564 	    syn_lines_msg();
3565 	    syn_match_msg();
3566 	}
3567     }
3568     else
3569 	MSG_PUTS_TITLE(_("\n--- Syntax items ---"));
3570     if (ends_excmd(*arg))
3571     {
3572 	/*
3573 	 * No argument: List all group IDs and all syntax clusters.
3574 	 */
3575 	for (id = 1; id <= highlight_ga.ga_len && !got_int; ++id)
3576 	    syn_list_one(id, syncing, FALSE);
3577 	for (id = 0; id < curbuf->b_syn_clusters.ga_len && !got_int; ++id)
3578 	    syn_list_cluster(id);
3579     }
3580     else
3581     {
3582 	/*
3583 	 * List the group IDs and syntax clusters that are in the argument.
3584 	 */
3585 	while (!ends_excmd(*arg) && !got_int)
3586 	{
3587 	    arg_end = skiptowhite(arg);
3588 	    if (*arg == '@')
3589 	    {
3590 		id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3591 		if (id == 0)
3592 		    EMSG2(_("E392: No such syntax cluster: %s"), arg);
3593 		else
3594 		    syn_list_cluster(id - SYNID_CLUSTER);
3595 	    }
3596 	    else
3597 	    {
3598 		id = syn_namen2id(arg, (int)(arg_end - arg));
3599 		if (id == 0)
3600 		    EMSG2(_(e_nogroup), arg);
3601 		else
3602 		    syn_list_one(id, syncing, TRUE);
3603 	    }
3604 	    arg = skipwhite(arg_end);
3605 	}
3606     }
3607     eap->nextcmd = check_nextcmd(arg);
3608 }
3609 
3610     static void
3611 syn_lines_msg()
3612 {
3613     if (curbuf->b_syn_sync_maxlines > 0 || curbuf->b_syn_sync_minlines > 0)
3614     {
3615 	MSG_PUTS("; ");
3616 	if (curbuf->b_syn_sync_minlines > 0)
3617 	{
3618 	    MSG_PUTS(_("minimal "));
3619 	    msg_outnum(curbuf->b_syn_sync_minlines);
3620 	    if (curbuf->b_syn_sync_maxlines)
3621 		MSG_PUTS(", ");
3622 	}
3623 	if (curbuf->b_syn_sync_maxlines > 0)
3624 	{
3625 	    MSG_PUTS(_("maximal "));
3626 	    msg_outnum(curbuf->b_syn_sync_maxlines);
3627 	}
3628 	MSG_PUTS(_(" lines before top line"));
3629     }
3630 }
3631 
3632     static void
3633 syn_match_msg()
3634 {
3635     if (curbuf->b_syn_sync_linebreaks > 0)
3636     {
3637 	MSG_PUTS(_("; match "));
3638 	msg_outnum(curbuf->b_syn_sync_linebreaks);
3639 	MSG_PUTS(_(" line breaks"));
3640     }
3641 }
3642 
3643 static int  last_matchgroup;
3644 
3645 struct name_list
3646 {
3647     int		flag;
3648     char	*name;
3649 };
3650 
3651 static void syn_list_flags __ARGS((struct name_list *nl, int flags, int attr));
3652 
3653 /*
3654  * List one syntax item, for ":syntax" or "syntax list syntax_name".
3655  */
3656     static void
3657 syn_list_one(id, syncing, link_only)
3658     int		id;
3659     int		syncing;	    /* when TRUE: list syncing items */
3660     int		link_only;	    /* when TRUE; list link-only too */
3661 {
3662     int		attr;
3663     int		idx;
3664     int		did_header = FALSE;
3665     synpat_T	*spp;
3666     static struct name_list namelist1[] =
3667 		{
3668 		    {HL_DISPLAY, "display"},
3669 		    {HL_CONTAINED, "contained"},
3670 		    {HL_ONELINE, "oneline"},
3671 		    {HL_KEEPEND, "keepend"},
3672 		    {HL_EXTEND, "extend"},
3673 		    {HL_EXCLUDENL, "excludenl"},
3674 		    {HL_TRANSP, "transparent"},
3675 		    {HL_FOLD, "fold"},
3676 		    {0, NULL}
3677 		};
3678     static struct name_list namelist2[] =
3679 		{
3680 		    {HL_SKIPWHITE, "skipwhite"},
3681 		    {HL_SKIPNL, "skipnl"},
3682 		    {HL_SKIPEMPTY, "skipempty"},
3683 		    {0, NULL}
3684 		};
3685 
3686     attr = hl_attr(HLF_D);		/* highlight like directories */
3687 
3688     /* list the keywords for "id" */
3689     if (!syncing)
3690     {
3691 	did_header = syn_list_keywords(id, &curbuf->b_keywtab, FALSE, attr);
3692 	did_header = syn_list_keywords(id, &curbuf->b_keywtab_ic,
3693 							    did_header, attr);
3694     }
3695 
3696     /* list the patterns for "id" */
3697     for (idx = 0; idx < curbuf->b_syn_patterns.ga_len && !got_int; ++idx)
3698     {
3699 	spp = &(SYN_ITEMS(curbuf)[idx]);
3700 	if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3701 	    continue;
3702 
3703 	(void)syn_list_header(did_header, 999, id);
3704 	did_header = TRUE;
3705 	last_matchgroup = 0;
3706 	if (spp->sp_type == SPTYPE_MATCH)
3707 	{
3708 	    put_pattern("match", ' ', spp, attr);
3709 	    msg_putchar(' ');
3710 	}
3711 	else if (spp->sp_type == SPTYPE_START)
3712 	{
3713 	    while (SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_START)
3714 		put_pattern("start", '=', &SYN_ITEMS(curbuf)[idx++], attr);
3715 	    if (SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_SKIP)
3716 		put_pattern("skip", '=', &SYN_ITEMS(curbuf)[idx++], attr);
3717 	    while (idx < curbuf->b_syn_patterns.ga_len
3718 			      && SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_END)
3719 		put_pattern("end", '=', &SYN_ITEMS(curbuf)[idx++], attr);
3720 	    --idx;
3721 	    msg_putchar(' ');
3722 	}
3723 	syn_list_flags(namelist1, spp->sp_flags, attr);
3724 
3725 	if (spp->sp_cont_list != NULL)
3726 	    put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
3727 
3728 	if (spp->sp_syn.cont_in_list != NULL)
3729 	    put_id_list((char_u *)"containedin",
3730 					      spp->sp_syn.cont_in_list, attr);
3731 
3732 	if (spp->sp_next_list != NULL)
3733 	{
3734 	    put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
3735 	    syn_list_flags(namelist2, spp->sp_flags, attr);
3736 	}
3737 	if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
3738 	{
3739 	    if (spp->sp_flags & HL_SYNC_HERE)
3740 		msg_puts_attr((char_u *)"grouphere", attr);
3741 	    else
3742 		msg_puts_attr((char_u *)"groupthere", attr);
3743 	    msg_putchar(' ');
3744 	    if (spp->sp_sync_idx >= 0)
3745 		msg_outtrans(HL_TABLE()[SYN_ITEMS(curbuf)
3746 				   [spp->sp_sync_idx].sp_syn.id - 1].sg_name);
3747 	    else
3748 		MSG_PUTS("NONE");
3749 	    msg_putchar(' ');
3750 	}
3751     }
3752 
3753     /* list the link, if there is one */
3754     if (HL_TABLE()[id - 1].sg_link && (did_header || link_only) && !got_int)
3755     {
3756 	(void)syn_list_header(did_header, 999, id);
3757 	msg_puts_attr((char_u *)"links to", attr);
3758 	msg_putchar(' ');
3759 	msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
3760     }
3761 }
3762 
3763     static void
3764 syn_list_flags(nl, flags, attr)
3765     struct name_list	*nl;
3766     int			flags;
3767     int			attr;
3768 {
3769     int		i;
3770 
3771     for (i = 0; nl[i].flag != 0; ++i)
3772 	if (flags & nl[i].flag)
3773 	{
3774 	    msg_puts_attr((char_u *)nl[i].name, attr);
3775 	    msg_putchar(' ');
3776 	}
3777 }
3778 
3779 /*
3780  * List one syntax cluster, for ":syntax" or "syntax list syntax_name".
3781  */
3782     static void
3783 syn_list_cluster(id)
3784     int id;
3785 {
3786     int	    endcol = 15;
3787 
3788     /* slight hack:  roughly duplicate the guts of syn_list_header() */
3789     msg_putchar('\n');
3790     msg_outtrans(SYN_CLSTR(curbuf)[id].scl_name);
3791 
3792     if (msg_col >= endcol)	/* output at least one space */
3793 	endcol = msg_col + 1;
3794     if (Columns <= endcol)	/* avoid hang for tiny window */
3795 	endcol = Columns - 1;
3796 
3797     msg_advance(endcol);
3798     if (SYN_CLSTR(curbuf)[id].scl_list != NULL)
3799     {
3800 	put_id_list((char_u *)"cluster", SYN_CLSTR(curbuf)[id].scl_list,
3801 		    hl_attr(HLF_D));
3802     }
3803     else
3804     {
3805 	msg_puts_attr((char_u *)"cluster", hl_attr(HLF_D));
3806 	msg_puts((char_u *)"=NONE");
3807     }
3808 }
3809 
3810     static void
3811 put_id_list(name, list, attr)
3812     char_u	*name;
3813     short	*list;
3814     int		attr;
3815 {
3816     short		*p;
3817 
3818     msg_puts_attr(name, attr);
3819     msg_putchar('=');
3820     for (p = list; *p; ++p)
3821     {
3822 	if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
3823 	{
3824 	    if (p[1])
3825 		MSG_PUTS("ALLBUT");
3826 	    else
3827 		MSG_PUTS("ALL");
3828 	}
3829 	else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
3830 	{
3831 	    MSG_PUTS("TOP");
3832 	}
3833 	else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
3834 	{
3835 	    MSG_PUTS("CONTAINED");
3836 	}
3837 	else if (*p >= SYNID_CLUSTER)
3838 	{
3839 	    short scl_id = *p - SYNID_CLUSTER;
3840 
3841 	    msg_putchar('@');
3842 	    msg_outtrans(SYN_CLSTR(curbuf)[scl_id].scl_name);
3843 	}
3844 	else
3845 	    msg_outtrans(HL_TABLE()[*p - 1].sg_name);
3846 	if (p[1])
3847 	    msg_putchar(',');
3848     }
3849     msg_putchar(' ');
3850 }
3851 
3852     static void
3853 put_pattern(s, c, spp, attr)
3854     char	*s;
3855     int		c;
3856     synpat_T	*spp;
3857     int		attr;
3858 {
3859     long	n;
3860     int		mask;
3861     int		first;
3862     static char	*sepchars = "/+=-#@\"|'^&";
3863     int		i;
3864 
3865     /* May have to write "matchgroup=group" */
3866     if (last_matchgroup != spp->sp_syn_match_id)
3867     {
3868 	last_matchgroup = spp->sp_syn_match_id;
3869 	msg_puts_attr((char_u *)"matchgroup", attr);
3870 	msg_putchar('=');
3871 	if (last_matchgroup == 0)
3872 	    msg_outtrans((char_u *)"NONE");
3873 	else
3874 	    msg_outtrans(HL_TABLE()[last_matchgroup - 1].sg_name);
3875 	msg_putchar(' ');
3876     }
3877 
3878     /* output the name of the pattern and an '=' or ' ' */
3879     msg_puts_attr((char_u *)s, attr);
3880     msg_putchar(c);
3881 
3882     /* output the pattern, in between a char that is not in the pattern */
3883     for (i = 0; vim_strchr(spp->sp_pattern, sepchars[i]) != NULL; )
3884 	if (sepchars[++i] == NUL)
3885 	{
3886 	    i = 0;	/* no good char found, just use the first one */
3887 	    break;
3888 	}
3889     msg_putchar(sepchars[i]);
3890     msg_outtrans(spp->sp_pattern);
3891     msg_putchar(sepchars[i]);
3892 
3893     /* output any pattern options */
3894     first = TRUE;
3895     for (i = 0; i < SPO_COUNT; ++i)
3896     {
3897 	mask = (1 << i);
3898 	if (spp->sp_off_flags & (mask + (mask << SPO_COUNT)))
3899 	{
3900 	    if (!first)
3901 		msg_putchar(',');	/* separate with commas */
3902 	    msg_puts((char_u *)spo_name_tab[i]);
3903 	    n = spp->sp_offsets[i];
3904 	    if (i != SPO_LC_OFF)
3905 	    {
3906 		if (spp->sp_off_flags & mask)
3907 		    msg_putchar('s');
3908 		else
3909 		    msg_putchar('e');
3910 		if (n > 0)
3911 		    msg_putchar('+');
3912 	    }
3913 	    if (n || i == SPO_LC_OFF)
3914 		msg_outnum(n);
3915 	    first = FALSE;
3916 	}
3917     }
3918     msg_putchar(' ');
3919 }
3920 
3921 /*
3922  * List or clear the keywords for one syntax group.
3923  * Return TRUE if the header has been printed.
3924  */
3925     static int
3926 syn_list_keywords(id, ht, did_header, attr)
3927     int		id;
3928     hashtab_T	*ht;
3929     int		did_header;		/* header has already been printed */
3930     int		attr;
3931 {
3932     int		outlen;
3933     hashitem_T	*hi;
3934     keyentry_T	*kp;
3935     int		todo;
3936     int		prev_contained = 0;
3937     short	*prev_next_list = NULL;
3938     short	*prev_cont_in_list = NULL;
3939     int		prev_skipnl = 0;
3940     int		prev_skipwhite = 0;
3941     int		prev_skipempty = 0;
3942 
3943     /*
3944      * Unfortunately, this list of keywords is not sorted on alphabet but on
3945      * hash value...
3946      */
3947     todo = ht->ht_used;
3948     for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
3949     {
3950 	if (!HASHITEM_EMPTY(hi))
3951 	{
3952 	    --todo;
3953 	    for (kp = HI2KE(hi); kp != NULL && !got_int; kp = kp->ke_next)
3954 	    {
3955 		if (kp->k_syn.id == id)
3956 		{
3957 		    if (prev_contained != (kp->flags & HL_CONTAINED)
3958 			    || prev_skipnl != (kp->flags & HL_SKIPNL)
3959 			    || prev_skipwhite != (kp->flags & HL_SKIPWHITE)
3960 			    || prev_skipempty != (kp->flags & HL_SKIPEMPTY)
3961 			    || prev_cont_in_list != kp->k_syn.cont_in_list
3962 			    || prev_next_list != kp->next_list)
3963 			outlen = 9999;
3964 		    else
3965 			outlen = (int)STRLEN(kp->keyword);
3966 		    /* output "contained" and "nextgroup" on each line */
3967 		    if (syn_list_header(did_header, outlen, id))
3968 		    {
3969 			prev_contained = 0;
3970 			prev_next_list = NULL;
3971 			prev_cont_in_list = NULL;
3972 			prev_skipnl = 0;
3973 			prev_skipwhite = 0;
3974 			prev_skipempty = 0;
3975 		    }
3976 		    did_header = TRUE;
3977 		    if (prev_contained != (kp->flags & HL_CONTAINED))
3978 		    {
3979 			msg_puts_attr((char_u *)"contained", attr);
3980 			msg_putchar(' ');
3981 			prev_contained = (kp->flags & HL_CONTAINED);
3982 		    }
3983 		    if (kp->k_syn.cont_in_list != prev_cont_in_list)
3984 		    {
3985 			put_id_list((char_u *)"containedin",
3986 						kp->k_syn.cont_in_list, attr);
3987 			msg_putchar(' ');
3988 			prev_cont_in_list = kp->k_syn.cont_in_list;
3989 		    }
3990 		    if (kp->next_list != prev_next_list)
3991 		    {
3992 			put_id_list((char_u *)"nextgroup", kp->next_list, attr);
3993 			msg_putchar(' ');
3994 			prev_next_list = kp->next_list;
3995 			if (kp->flags & HL_SKIPNL)
3996 			{
3997 			    msg_puts_attr((char_u *)"skipnl", attr);
3998 			    msg_putchar(' ');
3999 			    prev_skipnl = (kp->flags & HL_SKIPNL);
4000 			}
4001 			if (kp->flags & HL_SKIPWHITE)
4002 			{
4003 			    msg_puts_attr((char_u *)"skipwhite", attr);
4004 			    msg_putchar(' ');
4005 			    prev_skipwhite = (kp->flags & HL_SKIPWHITE);
4006 			}
4007 			if (kp->flags & HL_SKIPEMPTY)
4008 			{
4009 			    msg_puts_attr((char_u *)"skipempty", attr);
4010 			    msg_putchar(' ');
4011 			    prev_skipempty = (kp->flags & HL_SKIPEMPTY);
4012 			}
4013 		    }
4014 		    msg_outtrans(kp->keyword);
4015 		}
4016 	    }
4017 	}
4018     }
4019 
4020     return did_header;
4021 }
4022 
4023     static void
4024 syn_clear_keyword(id, ht)
4025     int		id;
4026     hashtab_T	*ht;
4027 {
4028     hashitem_T	*hi;
4029     keyentry_T	*kp;
4030     keyentry_T	*kp_prev;
4031     keyentry_T	*kp_next;
4032     int		todo;
4033 
4034     hash_lock(ht);
4035     todo = ht->ht_used;
4036     for (hi = ht->ht_array; todo > 0; ++hi)
4037     {
4038 	if (!HASHITEM_EMPTY(hi))
4039 	{
4040 	    --todo;
4041 	    kp_prev = NULL;
4042 	    for (kp = HI2KE(hi); kp != NULL; )
4043 	    {
4044 		if (kp->k_syn.id == id)
4045 		{
4046 		    kp_next = kp->ke_next;
4047 		    if (kp_prev == NULL)
4048 		    {
4049 			if (kp_next == NULL)
4050 			    hash_remove(ht, hi);
4051 			else
4052 			    hi->hi_key = KE2HIKEY(kp_next);
4053 		    }
4054 		    else
4055 			kp_prev->ke_next = kp_next;
4056 		    vim_free(kp->next_list);
4057 		    vim_free(kp->k_syn.cont_in_list);
4058 		    vim_free(kp);
4059 		    kp = kp_next;
4060 		}
4061 		else
4062 		{
4063 		    kp_prev = kp;
4064 		    kp = kp->ke_next;
4065 		}
4066 	    }
4067 	}
4068     }
4069     hash_unlock(ht);
4070 }
4071 
4072 /*
4073  * Clear a whole keyword table.
4074  */
4075     static void
4076 clear_keywtab(ht)
4077     hashtab_T	*ht;
4078 {
4079     hashitem_T	*hi;
4080     int		todo;
4081     keyentry_T	*kp;
4082     keyentry_T	*kp_next;
4083 
4084     todo = ht->ht_used;
4085     for (hi = ht->ht_array; todo > 0; ++hi)
4086     {
4087 	if (!HASHITEM_EMPTY(hi))
4088 	{
4089 	    --todo;
4090 	    kp = HI2KE(hi);
4091 	    for (kp = HI2KE(hi); kp != NULL; kp = kp_next)
4092 	    {
4093 		kp_next = kp->ke_next;
4094 		vim_free(kp->next_list);
4095 		vim_free(kp->k_syn.cont_in_list);
4096 		vim_free(kp);
4097 	    }
4098 	}
4099     }
4100     hash_clear(ht);
4101     hash_init(ht);
4102 }
4103 
4104 /*
4105  * Add a keyword to the list of keywords.
4106  */
4107     static void
4108 add_keyword(name, id, flags, cont_in_list, next_list)
4109     char_u	*name;	    /* name of keyword */
4110     int		id;	    /* group ID for this keyword */
4111     int		flags;	    /* flags for this keyword */
4112     short	*cont_in_list; /* containedin for this keyword */
4113     short	*next_list; /* nextgroup for this keyword */
4114 {
4115     keyentry_T	*kp;
4116     hashtab_T	*ht;
4117     hashitem_T	*hi;
4118     char_u	*name_ic;
4119     long_u	hash;
4120     char_u	name_folded[MAXKEYWLEN + 1];
4121 
4122     if (curbuf->b_syn_ic)
4123 	name_ic = str_foldcase(name, (int)STRLEN(name),
4124 						 name_folded, MAXKEYWLEN + 1);
4125     else
4126 	name_ic = name;
4127     kp = (keyentry_T *)alloc((int)(sizeof(keyentry_T) + STRLEN(name_ic)));
4128     if (kp == NULL)
4129 	return;
4130     STRCPY(kp->keyword, name_ic);
4131     kp->k_syn.id = id;
4132     kp->k_syn.inc_tag = current_syn_inc_tag;
4133     kp->flags = flags;
4134     kp->k_syn.cont_in_list = copy_id_list(cont_in_list);
4135     if (cont_in_list != NULL)
4136 	curbuf->b_syn_containedin = TRUE;
4137     kp->next_list = copy_id_list(next_list);
4138 
4139     if (curbuf->b_syn_ic)
4140 	ht = &curbuf->b_keywtab_ic;
4141     else
4142 	ht = &curbuf->b_keywtab;
4143 
4144     hash = hash_hash(kp->keyword);
4145     hi = hash_lookup(ht, kp->keyword, hash);
4146     if (HASHITEM_EMPTY(hi))
4147     {
4148 	/* new keyword, add to hashtable */
4149 	kp->ke_next = NULL;
4150 	hash_add_item(ht, hi, kp->keyword, hash);
4151     }
4152     else
4153     {
4154 	/* keyword already exists, prepend to list */
4155 	kp->ke_next = HI2KE(hi);
4156 	hi->hi_key = KE2HIKEY(kp);
4157     }
4158 }
4159 
4160 /*
4161  * Get the start and end of the group name argument.
4162  * Return a pointer to the first argument.
4163  * Return NULL if the end of the command was found instead of further args.
4164  */
4165     static char_u *
4166 get_group_name(arg, name_end)
4167     char_u	*arg;		/* start of the argument */
4168     char_u	**name_end;	/* pointer to end of the name */
4169 {
4170     char_u	*rest;
4171 
4172     *name_end = skiptowhite(arg);
4173     rest = skipwhite(*name_end);
4174 
4175     /*
4176      * Check if there are enough arguments.  The first argument may be a
4177      * pattern, where '|' is allowed, so only check for NUL.
4178      */
4179     if (ends_excmd(*arg) || *rest == NUL)
4180 	return NULL;
4181     return rest;
4182 }
4183 
4184 /*
4185  * Check for syntax command option arguments.
4186  * This can be called at any place in the list of arguments, and just picks
4187  * out the arguments that are known.  Can be called several times in a row to
4188  * collect all options in between other arguments.
4189  * Return a pointer to the next argument (which isn't an option).
4190  * Return NULL for any error;
4191  */
4192     static char_u *
4193 get_syn_options(arg, opt)
4194     char_u	    *arg;		/* next argument to be checked */
4195     syn_opt_arg_T   *opt;		/* various things */
4196 {
4197     char_u	*gname_start, *gname;
4198     int		syn_id;
4199     int		len;
4200     char	*p;
4201     int		i;
4202     int		fidx;
4203     static struct flag
4204     {
4205 	char	*name;
4206 	int	argtype;
4207 	int	flags;
4208     } flagtab[] = { {"cCoOnNtTaAiInNeEdD",	0,	HL_CONTAINED},
4209 		    {"oOnNeElLiInNeE",		0,	HL_ONELINE},
4210 		    {"kKeEeEpPeEnNdD",		0,	HL_KEEPEND},
4211 		    {"eExXtTeEnNdD",		0,	HL_EXTEND},
4212 		    {"eExXcClLuUdDeEnNlL",	0,	HL_EXCLUDENL},
4213 		    {"tTrRaAnNsSpPaArReEnNtT",	0,	HL_TRANSP},
4214 		    {"sSkKiIpPnNlL",		0,	HL_SKIPNL},
4215 		    {"sSkKiIpPwWhHiItTeE",	0,	HL_SKIPWHITE},
4216 		    {"sSkKiIpPeEmMpPtTyY",	0,	HL_SKIPEMPTY},
4217 		    {"gGrRoOuUpPhHeErReE",	0,	HL_SYNC_HERE},
4218 		    {"gGrRoOuUpPtThHeErReE",	0,	HL_SYNC_THERE},
4219 		    {"dDiIsSpPlLaAyY",		0,	HL_DISPLAY},
4220 		    {"fFoOlLdD",		0,	HL_FOLD},
4221 		    {"cCoOnNtTaAiInNsS",	1,	0},
4222 		    {"cCoOnNtTaAiInNeEdDiInN",	2,	0},
4223 		    {"nNeExXtTgGrRoOuUpP",	3,	0},
4224 		};
4225     static char *first_letters = "cCoOkKeEtTsSgGdDfFnN";
4226 
4227     if (arg == NULL)		/* already detected error */
4228 	return NULL;
4229 
4230     for (;;)
4231     {
4232 	/*
4233 	 * This is used very often when a large number of keywords is defined.
4234 	 * Need to skip quickly when no option name is found.
4235 	 * Also avoid tolower(), it's slow.
4236 	 */
4237 	if (strchr(first_letters, *arg) == NULL)
4238 	    break;
4239 
4240 	for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; )
4241 	{
4242 	    p = flagtab[fidx].name;
4243 	    for (i = 0, len = 0; p[i] != NUL; i += 2, ++len)
4244 		if (arg[len] != p[i] && arg[len] != p[i + 1])
4245 		    break;
4246 	    if (p[i] == NUL && (vim_iswhite(arg[len])
4247 				    || (flagtab[fidx].argtype > 0
4248 					 ? arg[len] == '='
4249 					 : ends_excmd(arg[len]))))
4250 	    {
4251 		if (opt->keyword
4252 			&& (flagtab[fidx].flags == HL_DISPLAY
4253 			    || flagtab[fidx].flags == HL_FOLD
4254 			    || flagtab[fidx].flags == HL_EXTEND))
4255 		    /* treat "display", "fold" and "extend" as a keyword */
4256 		    fidx = -1;
4257 		break;
4258 	    }
4259 	}
4260 	if (fidx < 0)	    /* no match found */
4261 	    break;
4262 
4263 	if (flagtab[fidx].argtype == 1)
4264 	{
4265 	    if (!opt->has_cont_list)
4266 	    {
4267 		EMSG(_("E395: contains argument not accepted here"));
4268 		return NULL;
4269 	    }
4270 	    if (get_id_list(&arg, 8, &opt->cont_list) == FAIL)
4271 		return NULL;
4272 	}
4273 	else if (flagtab[fidx].argtype == 2)
4274 	{
4275 #if 0	    /* cannot happen */
4276 	    if (opt->cont_in_list == NULL)
4277 	    {
4278 		EMSG(_("E396: containedin argument not accepted here"));
4279 		return NULL;
4280 	    }
4281 #endif
4282 	    if (get_id_list(&arg, 11, &opt->cont_in_list) == FAIL)
4283 		return NULL;
4284 	}
4285 	else if (flagtab[fidx].argtype == 3)
4286 	{
4287 	    if (get_id_list(&arg, 9, &opt->next_list) == FAIL)
4288 		return NULL;
4289 	}
4290 	else
4291 	{
4292 	    opt->flags |= flagtab[fidx].flags;
4293 	    arg = skipwhite(arg + len);
4294 
4295 	    if (flagtab[fidx].flags == HL_SYNC_HERE
4296 		    || flagtab[fidx].flags == HL_SYNC_THERE)
4297 	    {
4298 		if (opt->sync_idx == NULL)
4299 		{
4300 		    EMSG(_("E393: group[t]here not accepted here"));
4301 		    return NULL;
4302 		}
4303 		gname_start = arg;
4304 		arg = skiptowhite(arg);
4305 		if (gname_start == arg)
4306 		    return NULL;
4307 		gname = vim_strnsave(gname_start, (int)(arg - gname_start));
4308 		if (gname == NULL)
4309 		    return NULL;
4310 		if (STRCMP(gname, "NONE") == 0)
4311 		    *opt->sync_idx = NONE_IDX;
4312 		else
4313 		{
4314 		    syn_id = syn_name2id(gname);
4315 		    for (i = curbuf->b_syn_patterns.ga_len; --i >= 0; )
4316 			if (SYN_ITEMS(curbuf)[i].sp_syn.id == syn_id
4317 			      && SYN_ITEMS(curbuf)[i].sp_type == SPTYPE_START)
4318 			{
4319 			    *opt->sync_idx = i;
4320 			    break;
4321 			}
4322 		    if (i < 0)
4323 		    {
4324 			EMSG2(_("E394: Didn't find region item for %s"), gname);
4325 			vim_free(gname);
4326 			return NULL;
4327 		    }
4328 		}
4329 
4330 		vim_free(gname);
4331 		arg = skipwhite(arg);
4332 	    }
4333 #ifdef FEAT_FOLDING
4334 	    else if (flagtab[fidx].flags == HL_FOLD
4335 						&& foldmethodIsSyntax(curwin))
4336 		/* Need to update folds later. */
4337 		foldUpdateAll(curwin);
4338 #endif
4339 	}
4340     }
4341 
4342     return arg;
4343 }
4344 
4345 /*
4346  * Adjustments to syntax item when declared in a ":syn include"'d file.
4347  * Set the contained flag, and if the item is not already contained, add it
4348  * to the specified top-level group, if any.
4349  */
4350     static void
4351 syn_incl_toplevel(id, flagsp)
4352     int		id;
4353     int		*flagsp;
4354 {
4355     if ((*flagsp & HL_CONTAINED) || curbuf->b_syn_topgrp == 0)
4356 	return;
4357     *flagsp |= HL_CONTAINED;
4358     if (curbuf->b_syn_topgrp >= SYNID_CLUSTER)
4359     {
4360 	/* We have to alloc this, because syn_combine_list() will free it. */
4361 	short	    *grp_list = (short *)alloc((unsigned)(2 * sizeof(short)));
4362 	int	    tlg_id = curbuf->b_syn_topgrp - SYNID_CLUSTER;
4363 
4364 	if (grp_list != NULL)
4365 	{
4366 	    grp_list[0] = id;
4367 	    grp_list[1] = 0;
4368 	    syn_combine_list(&SYN_CLSTR(curbuf)[tlg_id].scl_list, &grp_list,
4369 			 CLUSTER_ADD);
4370 	}
4371     }
4372 }
4373 
4374 /*
4375  * Handle ":syntax include [@{group-name}] filename" command.
4376  */
4377 /* ARGSUSED */
4378     static void
4379 syn_cmd_include(eap, syncing)
4380     exarg_T	*eap;
4381     int		syncing;	    /* not used */
4382 {
4383     char_u	*arg = eap->arg;
4384     int		sgl_id = 1;
4385     char_u	*group_name_end;
4386     char_u	*rest;
4387     char_u	*errormsg = NULL;
4388     int		prev_toplvl_grp;
4389     int		prev_syn_inc_tag;
4390     int		source = FALSE;
4391 
4392     eap->nextcmd = find_nextcmd(arg);
4393     if (eap->skip)
4394 	return;
4395 
4396     if (arg[0] == '@')
4397     {
4398 	++arg;
4399 	rest = get_group_name(arg, &group_name_end);
4400 	if (rest == NULL)
4401 	{
4402 	    EMSG((char_u *)_("E397: Filename required"));
4403 	    return;
4404 	}
4405 	sgl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
4406 	/* separate_nextcmd() and expand_filename() depend on this */
4407 	eap->arg = rest;
4408     }
4409 
4410     /*
4411      * Everything that's left, up to the next command, should be the
4412      * filename to include.
4413      */
4414     eap->argt |= (XFILE | NOSPC);
4415     separate_nextcmd(eap);
4416     if (*eap->arg == '<' || *eap->arg == '$' || mch_isFullName(eap->arg))
4417     {
4418 	/* For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
4419 	 * file.  Need to expand the file name first.  In other cases
4420 	 * ":runtime!" is used. */
4421 	source = TRUE;
4422 	if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL)
4423 	{
4424 	    if (errormsg != NULL)
4425 		EMSG(errormsg);
4426 	    return;
4427 	}
4428     }
4429 
4430     /*
4431      * Save and restore the existing top-level grouplist id and ":syn
4432      * include" tag around the actual inclusion.
4433      */
4434     prev_syn_inc_tag = current_syn_inc_tag;
4435     current_syn_inc_tag = ++running_syn_inc_tag;
4436     prev_toplvl_grp = curbuf->b_syn_topgrp;
4437     curbuf->b_syn_topgrp = sgl_id;
4438     if (source ? do_source(eap->arg, FALSE, FALSE) == FAIL
4439 				     : source_runtime(eap->arg, TRUE) == FAIL)
4440 	EMSG2(_(e_notopen), eap->arg);
4441     curbuf->b_syn_topgrp = prev_toplvl_grp;
4442     current_syn_inc_tag = prev_syn_inc_tag;
4443 }
4444 
4445 /*
4446  * Handle ":syntax keyword {group-name} [{option}] keyword .." command.
4447  */
4448 /* ARGSUSED */
4449     static void
4450 syn_cmd_keyword(eap, syncing)
4451     exarg_T	*eap;
4452     int		syncing;	    /* not used */
4453 {
4454     char_u	*arg = eap->arg;
4455     char_u	*group_name_end;
4456     int		syn_id;
4457     char_u	*rest;
4458     char_u	*keyword_copy;
4459     char_u	*p;
4460     char_u	*kw;
4461     syn_opt_arg_T syn_opt_arg;
4462     int		cnt;
4463 
4464     rest = get_group_name(arg, &group_name_end);
4465 
4466     if (rest != NULL)
4467     {
4468 	syn_id = syn_check_group(arg, (int)(group_name_end - arg));
4469 
4470 	/* allocate a buffer, for removing the backslashes in the keyword */
4471 	keyword_copy = alloc((unsigned)STRLEN(rest) + 1);
4472 	if (keyword_copy != NULL)
4473 	{
4474 	    syn_opt_arg.flags = 0;
4475 	    syn_opt_arg.keyword = TRUE;
4476 	    syn_opt_arg.sync_idx = NULL;
4477 	    syn_opt_arg.has_cont_list = FALSE;
4478 	    syn_opt_arg.cont_in_list = NULL;
4479 	    syn_opt_arg.next_list = NULL;
4480 
4481 	    /*
4482 	     * The options given apply to ALL keywords, so all options must be
4483 	     * found before keywords can be created.
4484 	     * 1: collect the options and copy the keywords to keyword_copy.
4485 	     */
4486 	    cnt = 0;
4487 	    p = keyword_copy;
4488 	    for ( ; rest != NULL && !ends_excmd(*rest); rest = skipwhite(rest))
4489 	    {
4490 		rest = get_syn_options(rest, &syn_opt_arg);
4491 		if (rest == NULL || ends_excmd(*rest))
4492 		    break;
4493 		/* Copy the keyword, removing backslashes, and add a NUL. */
4494 		while (*rest != NUL && !vim_iswhite(*rest))
4495 		{
4496 		    if (*rest == '\\' && rest[1] != NUL)
4497 			++rest;
4498 		    *p++ = *rest++;
4499 		}
4500 		*p++ = NUL;
4501 		++cnt;
4502 	    }
4503 
4504 	    if (!eap->skip)
4505 	    {
4506 		/* Adjust flags for use of ":syn include". */
4507 		syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
4508 
4509 		/*
4510 		 * 2: Add an entry for each keyword.
4511 		 */
4512 		for (kw = keyword_copy; --cnt >= 0; kw += STRLEN(kw) + 1)
4513 		{
4514 		    for (p = vim_strchr(kw, '['); ; )
4515 		    {
4516 			if (p != NULL)
4517 			    *p = NUL;
4518 			add_keyword(kw, syn_id, syn_opt_arg.flags,
4519 						     syn_opt_arg.cont_in_list,
4520 						       syn_opt_arg.next_list);
4521 			if (p == NULL)
4522 			    break;
4523 			if (p[1] == NUL)
4524 			{
4525 			    EMSG2(_("E747: Missing ']': %s"), kw);
4526 			    kw = p + 2;		/* skip over the NUL */
4527 			    break;
4528 			}
4529 			if (p[1] == ']')
4530 			{
4531 			    kw = p + 1;		/* skip over the "]" */
4532 			    break;
4533 			}
4534 #ifdef FEAT_MBYTE
4535 			if (has_mbyte)
4536 			{
4537 			    int l = (*mb_ptr2len)(p + 1);
4538 
4539 			    mch_memmove(p, p + 1, l);
4540 			    p += l;
4541 			}
4542 			else
4543 #endif
4544 			{
4545 			    p[0] = p[1];
4546 			    ++p;
4547 			}
4548 		    }
4549 		}
4550 	    }
4551 
4552 	    vim_free(keyword_copy);
4553 	    vim_free(syn_opt_arg.cont_in_list);
4554 	    vim_free(syn_opt_arg.next_list);
4555 	}
4556     }
4557 
4558     if (rest != NULL)
4559 	eap->nextcmd = check_nextcmd(rest);
4560     else
4561 	EMSG2(_(e_invarg2), arg);
4562 
4563     redraw_curbuf_later(SOME_VALID);
4564     syn_stack_free_all(curbuf);		/* Need to recompute all syntax. */
4565 }
4566 
4567 /*
4568  * Handle ":syntax match {name} [{options}] {pattern} [{options}]".
4569  *
4570  * Also ":syntax sync match {name} [[grouphere | groupthere] {group-name}] .."
4571  */
4572     static void
4573 syn_cmd_match(eap, syncing)
4574     exarg_T	*eap;
4575     int		syncing;	    /* TRUE for ":syntax sync match .. " */
4576 {
4577     char_u	*arg = eap->arg;
4578     char_u	*group_name_end;
4579     char_u	*rest;
4580     synpat_T	item;		/* the item found in the line */
4581     int		syn_id;
4582     int		idx;
4583     syn_opt_arg_T syn_opt_arg;
4584     int		sync_idx = 0;
4585 
4586     /* Isolate the group name, check for validity */
4587     rest = get_group_name(arg, &group_name_end);
4588 
4589     /* Get options before the pattern */
4590     syn_opt_arg.flags = 0;
4591     syn_opt_arg.keyword = FALSE;
4592     syn_opt_arg.sync_idx = syncing ? &sync_idx : NULL;
4593     syn_opt_arg.has_cont_list = TRUE;
4594     syn_opt_arg.cont_list = NULL;
4595     syn_opt_arg.cont_in_list = NULL;
4596     syn_opt_arg.next_list = NULL;
4597     rest = get_syn_options(rest, &syn_opt_arg);
4598 
4599     /* get the pattern. */
4600     init_syn_patterns();
4601     vim_memset(&item, 0, sizeof(item));
4602     rest = get_syn_pattern(rest, &item);
4603     if (vim_regcomp_had_eol() && !(syn_opt_arg.flags & HL_EXCLUDENL))
4604 	syn_opt_arg.flags |= HL_HAS_EOL;
4605 
4606     /* Get options after the pattern */
4607     rest = get_syn_options(rest, &syn_opt_arg);
4608 
4609     if (rest != NULL)		/* all arguments are valid */
4610     {
4611 	/*
4612 	 * Check for trailing command and illegal trailing arguments.
4613 	 */
4614 	eap->nextcmd = check_nextcmd(rest);
4615 	if (!ends_excmd(*rest) || eap->skip)
4616 	    rest = NULL;
4617 	else if (ga_grow(&curbuf->b_syn_patterns, 1) != FAIL
4618 		&& (syn_id = syn_check_group(arg,
4619 					   (int)(group_name_end - arg))) != 0)
4620 	{
4621 	    syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
4622 	    /*
4623 	     * Store the pattern in the syn_items list
4624 	     */
4625 	    idx = curbuf->b_syn_patterns.ga_len;
4626 	    SYN_ITEMS(curbuf)[idx] = item;
4627 	    SYN_ITEMS(curbuf)[idx].sp_syncing = syncing;
4628 	    SYN_ITEMS(curbuf)[idx].sp_type = SPTYPE_MATCH;
4629 	    SYN_ITEMS(curbuf)[idx].sp_syn.id = syn_id;
4630 	    SYN_ITEMS(curbuf)[idx].sp_syn.inc_tag = current_syn_inc_tag;
4631 	    SYN_ITEMS(curbuf)[idx].sp_flags = syn_opt_arg.flags;
4632 	    SYN_ITEMS(curbuf)[idx].sp_sync_idx = sync_idx;
4633 	    SYN_ITEMS(curbuf)[idx].sp_cont_list = syn_opt_arg.cont_list;
4634 	    SYN_ITEMS(curbuf)[idx].sp_syn.cont_in_list =
4635 						     syn_opt_arg.cont_in_list;
4636 	    if (syn_opt_arg.cont_in_list != NULL)
4637 		curbuf->b_syn_containedin = TRUE;
4638 	    SYN_ITEMS(curbuf)[idx].sp_next_list = syn_opt_arg.next_list;
4639 	    ++curbuf->b_syn_patterns.ga_len;
4640 
4641 	    /* remember that we found a match for syncing on */
4642 	    if (syn_opt_arg.flags & (HL_SYNC_HERE|HL_SYNC_THERE))
4643 		curbuf->b_syn_sync_flags |= SF_MATCH;
4644 #ifdef FEAT_FOLDING
4645 	    if (syn_opt_arg.flags & HL_FOLD)
4646 		++curbuf->b_syn_folditems;
4647 #endif
4648 
4649 	    redraw_curbuf_later(SOME_VALID);
4650 	    syn_stack_free_all(curbuf);	/* Need to recompute all syntax. */
4651 	    return;	/* don't free the progs and patterns now */
4652 	}
4653     }
4654 
4655     /*
4656      * Something failed, free the allocated memory.
4657      */
4658     vim_free(item.sp_prog);
4659     vim_free(item.sp_pattern);
4660     vim_free(syn_opt_arg.cont_list);
4661     vim_free(syn_opt_arg.cont_in_list);
4662     vim_free(syn_opt_arg.next_list);
4663 
4664     if (rest == NULL)
4665 	EMSG2(_(e_invarg2), arg);
4666 }
4667 
4668 /*
4669  * Handle ":syntax region {group-name} [matchgroup={group-name}]
4670  *		start {start} .. [skip {skip}] end {end} .. [{options}]".
4671  */
4672     static void
4673 syn_cmd_region(eap, syncing)
4674     exarg_T	*eap;
4675     int		syncing;	    /* TRUE for ":syntax sync region .." */
4676 {
4677     char_u		*arg = eap->arg;
4678     char_u		*group_name_end;
4679     char_u		*rest;			/* next arg, NULL on error */
4680     char_u		*key_end;
4681     char_u		*key = NULL;
4682     char_u		*p;
4683     int			item;
4684 #define ITEM_START	    0
4685 #define ITEM_SKIP	    1
4686 #define ITEM_END	    2
4687 #define ITEM_MATCHGROUP	    3
4688     struct pat_ptr
4689     {
4690 	synpat_T	*pp_synp;		/* pointer to syn_pattern */
4691 	int		pp_matchgroup_id;	/* matchgroup ID */
4692 	struct pat_ptr	*pp_next;		/* pointer to next pat_ptr */
4693     }			*(pat_ptrs[3]);
4694 					/* patterns found in the line */
4695     struct pat_ptr	*ppp;
4696     struct pat_ptr	*ppp_next;
4697     int			pat_count = 0;		/* nr of syn_patterns found */
4698     int			syn_id;
4699     int			matchgroup_id = 0;
4700     int			not_enough = FALSE;	/* not enough arguments */
4701     int			illegal = FALSE;	/* illegal arguments */
4702     int			success = FALSE;
4703     int			idx;
4704     syn_opt_arg_T	syn_opt_arg;
4705 
4706     /* Isolate the group name, check for validity */
4707     rest = get_group_name(arg, &group_name_end);
4708 
4709     pat_ptrs[0] = NULL;
4710     pat_ptrs[1] = NULL;
4711     pat_ptrs[2] = NULL;
4712 
4713     init_syn_patterns();
4714 
4715     syn_opt_arg.flags = 0;
4716     syn_opt_arg.keyword = FALSE;
4717     syn_opt_arg.sync_idx = NULL;
4718     syn_opt_arg.has_cont_list = TRUE;
4719     syn_opt_arg.cont_list = NULL;
4720     syn_opt_arg.cont_in_list = NULL;
4721     syn_opt_arg.next_list = NULL;
4722 
4723     /*
4724      * get the options, patterns and matchgroup.
4725      */
4726     while (rest != NULL && !ends_excmd(*rest))
4727     {
4728 	/* Check for option arguments */
4729 	rest = get_syn_options(rest, &syn_opt_arg);
4730 	if (rest == NULL || ends_excmd(*rest))
4731 	    break;
4732 
4733 	/* must be a pattern or matchgroup then */
4734 	key_end = rest;
4735 	while (*key_end && !vim_iswhite(*key_end) && *key_end != '=')
4736 	    ++key_end;
4737 	vim_free(key);
4738 	key = vim_strnsave_up(rest, (int)(key_end - rest));
4739 	if (key == NULL)			/* out of memory */
4740 	{
4741 	    rest = NULL;
4742 	    break;
4743 	}
4744 	if (STRCMP(key, "MATCHGROUP") == 0)
4745 	    item = ITEM_MATCHGROUP;
4746 	else if (STRCMP(key, "START") == 0)
4747 	    item = ITEM_START;
4748 	else if (STRCMP(key, "END") == 0)
4749 	    item = ITEM_END;
4750 	else if (STRCMP(key, "SKIP") == 0)
4751 	{
4752 	    if (pat_ptrs[ITEM_SKIP] != NULL)	/* one skip pattern allowed */
4753 	    {
4754 		illegal = TRUE;
4755 		break;
4756 	    }
4757 	    item = ITEM_SKIP;
4758 	}
4759 	else
4760 	    break;
4761 	rest = skipwhite(key_end);
4762 	if (*rest != '=')
4763 	{
4764 	    rest = NULL;
4765 	    EMSG2(_("E398: Missing '=': %s"), arg);
4766 	    break;
4767 	}
4768 	rest = skipwhite(rest + 1);
4769 	if (*rest == NUL)
4770 	{
4771 	    not_enough = TRUE;
4772 	    break;
4773 	}
4774 
4775 	if (item == ITEM_MATCHGROUP)
4776 	{
4777 	    p = skiptowhite(rest);
4778 	    if ((p - rest == 4 && STRNCMP(rest, "NONE", 4) == 0) || eap->skip)
4779 		matchgroup_id = 0;
4780 	    else
4781 	    {
4782 		matchgroup_id = syn_check_group(rest, (int)(p - rest));
4783 		if (matchgroup_id == 0)
4784 		{
4785 		    illegal = TRUE;
4786 		    break;
4787 		}
4788 	    }
4789 	    rest = skipwhite(p);
4790 	}
4791 	else
4792 	{
4793 	    /*
4794 	     * Allocate room for a syn_pattern, and link it in the list of
4795 	     * syn_patterns for this item, at the start (because the list is
4796 	     * used from end to start).
4797 	     */
4798 	    ppp = (struct pat_ptr *)alloc((unsigned)sizeof(struct pat_ptr));
4799 	    if (ppp == NULL)
4800 	    {
4801 		rest = NULL;
4802 		break;
4803 	    }
4804 	    ppp->pp_next = pat_ptrs[item];
4805 	    pat_ptrs[item] = ppp;
4806 	    ppp->pp_synp = (synpat_T *)alloc_clear((unsigned)sizeof(synpat_T));
4807 	    if (ppp->pp_synp == NULL)
4808 	    {
4809 		rest = NULL;
4810 		break;
4811 	    }
4812 
4813 	    /*
4814 	     * Get the syntax pattern and the following offset(s).
4815 	     */
4816 	    /* Enable the appropriate \z specials. */
4817 	    if (item == ITEM_START)
4818 		reg_do_extmatch = REX_SET;
4819 	    else if (item == ITEM_SKIP || item == ITEM_END)
4820 		reg_do_extmatch = REX_USE;
4821 	    rest = get_syn_pattern(rest, ppp->pp_synp);
4822 	    reg_do_extmatch = 0;
4823 	    if (item == ITEM_END && vim_regcomp_had_eol()
4824 				       && !(syn_opt_arg.flags & HL_EXCLUDENL))
4825 		ppp->pp_synp->sp_flags |= HL_HAS_EOL;
4826 	    ppp->pp_matchgroup_id = matchgroup_id;
4827 	    ++pat_count;
4828 	}
4829     }
4830     vim_free(key);
4831     if (illegal || not_enough)
4832 	rest = NULL;
4833 
4834     /*
4835      * Must have a "start" and "end" pattern.
4836      */
4837     if (rest != NULL && (pat_ptrs[ITEM_START] == NULL ||
4838 						  pat_ptrs[ITEM_END] == NULL))
4839     {
4840 	not_enough = TRUE;
4841 	rest = NULL;
4842     }
4843 
4844     if (rest != NULL)
4845     {
4846 	/*
4847 	 * Check for trailing garbage or command.
4848 	 * If OK, add the item.
4849 	 */
4850 	eap->nextcmd = check_nextcmd(rest);
4851 	if (!ends_excmd(*rest) || eap->skip)
4852 	    rest = NULL;
4853 	else if (ga_grow(&(curbuf->b_syn_patterns), pat_count) != FAIL
4854 		&& (syn_id = syn_check_group(arg,
4855 					   (int)(group_name_end - arg))) != 0)
4856 	{
4857 	    syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
4858 	    /*
4859 	     * Store the start/skip/end in the syn_items list
4860 	     */
4861 	    idx = curbuf->b_syn_patterns.ga_len;
4862 	    for (item = ITEM_START; item <= ITEM_END; ++item)
4863 	    {
4864 		for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp->pp_next)
4865 		{
4866 		    SYN_ITEMS(curbuf)[idx] = *(ppp->pp_synp);
4867 		    SYN_ITEMS(curbuf)[idx].sp_syncing = syncing;
4868 		    SYN_ITEMS(curbuf)[idx].sp_type =
4869 			    (item == ITEM_START) ? SPTYPE_START :
4870 			    (item == ITEM_SKIP) ? SPTYPE_SKIP : SPTYPE_END;
4871 		    SYN_ITEMS(curbuf)[idx].sp_flags |= syn_opt_arg.flags;
4872 		    SYN_ITEMS(curbuf)[idx].sp_syn.id = syn_id;
4873 		    SYN_ITEMS(curbuf)[idx].sp_syn.inc_tag = current_syn_inc_tag;
4874 		    SYN_ITEMS(curbuf)[idx].sp_syn_match_id =
4875 							ppp->pp_matchgroup_id;
4876 		    if (item == ITEM_START)
4877 		    {
4878 			SYN_ITEMS(curbuf)[idx].sp_cont_list =
4879 							syn_opt_arg.cont_list;
4880 			SYN_ITEMS(curbuf)[idx].sp_syn.cont_in_list =
4881 						     syn_opt_arg.cont_in_list;
4882 			if (syn_opt_arg.cont_in_list != NULL)
4883 			    curbuf->b_syn_containedin = TRUE;
4884 			SYN_ITEMS(curbuf)[idx].sp_next_list =
4885 							syn_opt_arg.next_list;
4886 		    }
4887 		    ++curbuf->b_syn_patterns.ga_len;
4888 		    ++idx;
4889 #ifdef FEAT_FOLDING
4890 		    if (syn_opt_arg.flags & HL_FOLD)
4891 			++curbuf->b_syn_folditems;
4892 #endif
4893 		}
4894 	    }
4895 
4896 	    redraw_curbuf_later(SOME_VALID);
4897 	    syn_stack_free_all(curbuf);	/* Need to recompute all syntax. */
4898 	    success = TRUE;	    /* don't free the progs and patterns now */
4899 	}
4900     }
4901 
4902     /*
4903      * Free the allocated memory.
4904      */
4905     for (item = ITEM_START; item <= ITEM_END; ++item)
4906 	for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp_next)
4907 	{
4908 	    if (!success)
4909 	    {
4910 		vim_free(ppp->pp_synp->sp_prog);
4911 		vim_free(ppp->pp_synp->sp_pattern);
4912 	    }
4913 	    vim_free(ppp->pp_synp);
4914 	    ppp_next = ppp->pp_next;
4915 	    vim_free(ppp);
4916 	}
4917 
4918     if (!success)
4919     {
4920 	vim_free(syn_opt_arg.cont_list);
4921 	vim_free(syn_opt_arg.cont_in_list);
4922 	vim_free(syn_opt_arg.next_list);
4923 	if (not_enough)
4924 	    EMSG2(_("E399: Not enough arguments: syntax region %s"), arg);
4925 	else if (illegal || rest == NULL)
4926 	    EMSG2(_(e_invarg2), arg);
4927     }
4928 }
4929 
4930 /*
4931  * A simple syntax group ID comparison function suitable for use in qsort()
4932  */
4933     static int
4934 #ifdef __BORLANDC__
4935 _RTLENTRYF
4936 #endif
4937 syn_compare_stub(v1, v2)
4938     const void	*v1;
4939     const void	*v2;
4940 {
4941     const short	*s1 = v1;
4942     const short	*s2 = v2;
4943 
4944     return (*s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0);
4945 }
4946 
4947 /*
4948  * Combines lists of syntax clusters.
4949  * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
4950  */
4951     static void
4952 syn_combine_list(clstr1, clstr2, list_op)
4953     short	**clstr1;
4954     short	**clstr2;
4955     int		list_op;
4956 {
4957     int		count1 = 0;
4958     int		count2 = 0;
4959     short	*g1;
4960     short	*g2;
4961     short	*clstr = NULL;
4962     int		count;
4963     int		round;
4964 
4965     /*
4966      * Handle degenerate cases.
4967      */
4968     if (*clstr2 == NULL)
4969 	return;
4970     if (*clstr1 == NULL || list_op == CLUSTER_REPLACE)
4971     {
4972 	if (list_op == CLUSTER_REPLACE)
4973 	    vim_free(*clstr1);
4974 	if (list_op == CLUSTER_REPLACE || list_op == CLUSTER_ADD)
4975 	    *clstr1 = *clstr2;
4976 	else
4977 	    vim_free(*clstr2);
4978 	return;
4979     }
4980 
4981     for (g1 = *clstr1; *g1; g1++)
4982 	++count1;
4983     for (g2 = *clstr2; *g2; g2++)
4984 	++count2;
4985 
4986     /*
4987      * For speed purposes, sort both lists.
4988      */
4989     qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
4990     qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
4991 
4992     /*
4993      * We proceed in two passes; in round 1, we count the elements to place
4994      * in the new list, and in round 2, we allocate and populate the new
4995      * list.  For speed, we use a mergesort-like method, adding the smaller
4996      * of the current elements in each list to the new list.
4997      */
4998     for (round = 1; round <= 2; round++)
4999     {
5000 	g1 = *clstr1;
5001 	g2 = *clstr2;
5002 	count = 0;
5003 
5004 	/*
5005 	 * First, loop through the lists until one of them is empty.
5006 	 */
5007 	while (*g1 && *g2)
5008 	{
5009 	    /*
5010 	     * We always want to add from the first list.
5011 	     */
5012 	    if (*g1 < *g2)
5013 	    {
5014 		if (round == 2)
5015 		    clstr[count] = *g1;
5016 		count++;
5017 		g1++;
5018 		continue;
5019 	    }
5020 	    /*
5021 	     * We only want to add from the second list if we're adding the
5022 	     * lists.
5023 	     */
5024 	    if (list_op == CLUSTER_ADD)
5025 	    {
5026 		if (round == 2)
5027 		    clstr[count] = *g2;
5028 		count++;
5029 	    }
5030 	    if (*g1 == *g2)
5031 		g1++;
5032 	    g2++;
5033 	}
5034 
5035 	/*
5036 	 * Now add the leftovers from whichever list didn't get finished
5037 	 * first.  As before, we only want to add from the second list if
5038 	 * we're adding the lists.
5039 	 */
5040 	for (; *g1; g1++, count++)
5041 	    if (round == 2)
5042 		clstr[count] = *g1;
5043 	if (list_op == CLUSTER_ADD)
5044 	    for (; *g2; g2++, count++)
5045 		if (round == 2)
5046 		    clstr[count] = *g2;
5047 
5048 	if (round == 1)
5049 	{
5050 	    /*
5051 	     * If the group ended up empty, we don't need to allocate any
5052 	     * space for it.
5053 	     */
5054 	    if (count == 0)
5055 	    {
5056 		clstr = NULL;
5057 		break;
5058 	    }
5059 	    clstr = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
5060 	    if (clstr == NULL)
5061 		break;
5062 	    clstr[count] = 0;
5063 	}
5064     }
5065 
5066     /*
5067      * Finally, put the new list in place.
5068      */
5069     vim_free(*clstr1);
5070     vim_free(*clstr2);
5071     *clstr1 = clstr;
5072 }
5073 
5074 /*
5075  * Lookup a syntax cluster name and return it's ID.
5076  * If it is not found, 0 is returned.
5077  */
5078     static int
5079 syn_scl_name2id(name)
5080     char_u	*name;
5081 {
5082     int		i;
5083     char_u	*name_u;
5084 
5085     /* Avoid using stricmp() too much, it's slow on some systems */
5086     name_u = vim_strsave_up(name);
5087     if (name_u == NULL)
5088 	return 0;
5089     for (i = curbuf->b_syn_clusters.ga_len; --i >= 0; )
5090 	if (SYN_CLSTR(curbuf)[i].scl_name_u != NULL
5091 		&& STRCMP(name_u, SYN_CLSTR(curbuf)[i].scl_name_u) == 0)
5092 	    break;
5093     vim_free(name_u);
5094     return (i < 0 ? 0 : i + SYNID_CLUSTER);
5095 }
5096 
5097 /*
5098  * Like syn_scl_name2id(), but take a pointer + length argument.
5099  */
5100     static int
5101 syn_scl_namen2id(linep, len)
5102     char_u  *linep;
5103     int	    len;
5104 {
5105     char_u  *name;
5106     int	    id = 0;
5107 
5108     name = vim_strnsave(linep, len);
5109     if (name != NULL)
5110     {
5111 	id = syn_scl_name2id(name);
5112 	vim_free(name);
5113     }
5114     return id;
5115 }
5116 
5117 /*
5118  * Find syntax cluster name in the table and return it's ID.
5119  * The argument is a pointer to the name and the length of the name.
5120  * If it doesn't exist yet, a new entry is created.
5121  * Return 0 for failure.
5122  */
5123     static int
5124 syn_check_cluster(pp, len)
5125     char_u	*pp;
5126     int		len;
5127 {
5128     int		id;
5129     char_u	*name;
5130 
5131     name = vim_strnsave(pp, len);
5132     if (name == NULL)
5133 	return 0;
5134 
5135     id = syn_scl_name2id(name);
5136     if (id == 0)			/* doesn't exist yet */
5137 	id = syn_add_cluster(name);
5138     else
5139 	vim_free(name);
5140     return id;
5141 }
5142 
5143 /*
5144  * Add new syntax cluster and return it's ID.
5145  * "name" must be an allocated string, it will be consumed.
5146  * Return 0 for failure.
5147  */
5148     static int
5149 syn_add_cluster(name)
5150     char_u	*name;
5151 {
5152     int		len;
5153 
5154     /*
5155      * First call for this growarray: init growing array.
5156      */
5157     if (curbuf->b_syn_clusters.ga_data == NULL)
5158     {
5159 	curbuf->b_syn_clusters.ga_itemsize = sizeof(syn_cluster_T);
5160 	curbuf->b_syn_clusters.ga_growsize = 10;
5161     }
5162 
5163     /*
5164      * Make room for at least one other cluster entry.
5165      */
5166     if (ga_grow(&curbuf->b_syn_clusters, 1) == FAIL)
5167     {
5168 	vim_free(name);
5169 	return 0;
5170     }
5171     len = curbuf->b_syn_clusters.ga_len;
5172 
5173     vim_memset(&(SYN_CLSTR(curbuf)[len]), 0, sizeof(syn_cluster_T));
5174     SYN_CLSTR(curbuf)[len].scl_name = name;
5175     SYN_CLSTR(curbuf)[len].scl_name_u = vim_strsave_up(name);
5176     SYN_CLSTR(curbuf)[len].scl_list = NULL;
5177     ++curbuf->b_syn_clusters.ga_len;
5178 
5179     if (STRICMP(name, "Spell") == 0)
5180 	curbuf->b_spell_cluster_id = len + SYNID_CLUSTER;
5181     if (STRICMP(name, "NoSpell") == 0)
5182 	curbuf->b_nospell_cluster_id = len + SYNID_CLUSTER;
5183 
5184     return len + SYNID_CLUSTER;
5185 }
5186 
5187 /*
5188  * Handle ":syntax cluster {cluster-name} [contains={groupname},..]
5189  *		[add={groupname},..] [remove={groupname},..]".
5190  */
5191 /* ARGSUSED */
5192     static void
5193 syn_cmd_cluster(eap, syncing)
5194     exarg_T	*eap;
5195     int		syncing;	    /* not used */
5196 {
5197     char_u	*arg = eap->arg;
5198     char_u	*group_name_end;
5199     char_u	*rest;
5200     int		scl_id;
5201     short	*clstr_list;
5202     int		got_clstr = FALSE;
5203     int		opt_len;
5204     int		list_op;
5205 
5206     eap->nextcmd = find_nextcmd(arg);
5207     if (eap->skip)
5208 	return;
5209 
5210     rest = get_group_name(arg, &group_name_end);
5211 
5212     if (rest != NULL)
5213     {
5214 	scl_id = syn_check_cluster(arg, (int)(group_name_end - arg))
5215 							      - SYNID_CLUSTER;
5216 
5217 	for (;;)
5218 	{
5219 	    if (STRNICMP(rest, "add", 3) == 0
5220 		    && (vim_iswhite(rest[3]) || rest[3] == '='))
5221 	    {
5222 		opt_len = 3;
5223 		list_op = CLUSTER_ADD;
5224 	    }
5225 	    else if (STRNICMP(rest, "remove", 6) == 0
5226 		    && (vim_iswhite(rest[6]) || rest[6] == '='))
5227 	    {
5228 		opt_len = 6;
5229 		list_op = CLUSTER_SUBTRACT;
5230 	    }
5231 	    else if (STRNICMP(rest, "contains", 8) == 0
5232 			&& (vim_iswhite(rest[8]) || rest[8] == '='))
5233 	    {
5234 		opt_len = 8;
5235 		list_op = CLUSTER_REPLACE;
5236 	    }
5237 	    else
5238 		break;
5239 
5240 	    clstr_list = NULL;
5241 	    if (get_id_list(&rest, opt_len, &clstr_list) == FAIL)
5242 	    {
5243 		EMSG2(_(e_invarg2), rest);
5244 		break;
5245 	    }
5246 	    syn_combine_list(&SYN_CLSTR(curbuf)[scl_id].scl_list,
5247 			     &clstr_list, list_op);
5248 	    got_clstr = TRUE;
5249 	}
5250 
5251 	if (got_clstr)
5252 	{
5253 	    redraw_curbuf_later(SOME_VALID);
5254 	    syn_stack_free_all(curbuf);	/* Need to recompute all syntax. */
5255 	}
5256     }
5257 
5258     if (!got_clstr)
5259 	EMSG(_("E400: No cluster specified"));
5260     if (rest == NULL || !ends_excmd(*rest))
5261 	EMSG2(_(e_invarg2), arg);
5262 }
5263 
5264 /*
5265  * On first call for current buffer: Init growing array.
5266  */
5267     static void
5268 init_syn_patterns()
5269 {
5270     curbuf->b_syn_patterns.ga_itemsize = sizeof(synpat_T);
5271     curbuf->b_syn_patterns.ga_growsize = 10;
5272 }
5273 
5274 /*
5275  * Get one pattern for a ":syntax match" or ":syntax region" command.
5276  * Stores the pattern and program in a synpat_T.
5277  * Returns a pointer to the next argument, or NULL in case of an error.
5278  */
5279     static char_u *
5280 get_syn_pattern(arg, ci)
5281     char_u	*arg;
5282     synpat_T	*ci;
5283 {
5284     char_u	*end;
5285     int		*p;
5286     int		idx;
5287     char_u	*cpo_save;
5288 
5289     /* need at least three chars */
5290     if (arg == NULL || arg[1] == NUL || arg[2] == NUL)
5291 	return NULL;
5292 
5293     end = skip_regexp(arg + 1, *arg, TRUE, NULL);
5294     if (*end != *arg)			    /* end delimiter not found */
5295     {
5296 	EMSG2(_("E401: Pattern delimiter not found: %s"), arg);
5297 	return NULL;
5298     }
5299     /* store the pattern and compiled regexp program */
5300     if ((ci->sp_pattern = vim_strnsave(arg + 1, (int)(end - arg - 1))) == NULL)
5301 	return NULL;
5302 
5303     /* Make 'cpoptions' empty, to avoid the 'l' flag */
5304     cpo_save = p_cpo;
5305     p_cpo = (char_u *)"";
5306     ci->sp_prog = vim_regcomp(ci->sp_pattern, RE_MAGIC);
5307     p_cpo = cpo_save;
5308 
5309     if (ci->sp_prog == NULL)
5310 	return NULL;
5311     ci->sp_ic = curbuf->b_syn_ic;
5312 
5313     /*
5314      * Check for a match, highlight or region offset.
5315      */
5316     ++end;
5317     do
5318     {
5319 	for (idx = SPO_COUNT; --idx >= 0; )
5320 	    if (STRNCMP(end, spo_name_tab[idx], 3) == 0)
5321 		break;
5322 	if (idx >= 0)
5323 	{
5324 	    p = &(ci->sp_offsets[idx]);
5325 	    if (idx != SPO_LC_OFF)
5326 		switch (end[3])
5327 		{
5328 		    case 's':   break;
5329 		    case 'b':   break;
5330 		    case 'e':   idx += SPO_COUNT; break;
5331 		    default:    idx = -1; break;
5332 		}
5333 	    if (idx >= 0)
5334 	    {
5335 		ci->sp_off_flags |= (1 << idx);
5336 		if (idx == SPO_LC_OFF)	    /* lc=99 */
5337 		{
5338 		    end += 3;
5339 		    *p = getdigits(&end);
5340 
5341 		    /* "lc=" offset automatically sets "ms=" offset */
5342 		    if (!(ci->sp_off_flags & (1 << SPO_MS_OFF)))
5343 		    {
5344 			ci->sp_off_flags |= (1 << SPO_MS_OFF);
5345 			ci->sp_offsets[SPO_MS_OFF] = *p;
5346 		    }
5347 		}
5348 		else			    /* yy=x+99 */
5349 		{
5350 		    end += 4;
5351 		    if (*end == '+')
5352 		    {
5353 			++end;
5354 			*p = getdigits(&end);		/* positive offset */
5355 		    }
5356 		    else if (*end == '-')
5357 		    {
5358 			++end;
5359 			*p = -getdigits(&end);		/* negative offset */
5360 		    }
5361 		}
5362 		if (*end != ',')
5363 		    break;
5364 		++end;
5365 	    }
5366 	}
5367     } while (idx >= 0);
5368 
5369     if (!ends_excmd(*end) && !vim_iswhite(*end))
5370     {
5371 	EMSG2(_("E402: Garbage after pattern: %s"), arg);
5372 	return NULL;
5373     }
5374     return skipwhite(end);
5375 }
5376 
5377 /*
5378  * Handle ":syntax sync .." command.
5379  */
5380 /* ARGSUSED */
5381     static void
5382 syn_cmd_sync(eap, syncing)
5383     exarg_T	*eap;
5384     int		syncing;	    /* not used */
5385 {
5386     char_u	*arg_start = eap->arg;
5387     char_u	*arg_end;
5388     char_u	*key = NULL;
5389     char_u	*next_arg;
5390     int		illegal = FALSE;
5391     int		finished = FALSE;
5392     long	n;
5393     char_u	*cpo_save;
5394 
5395     if (ends_excmd(*arg_start))
5396     {
5397 	syn_cmd_list(eap, TRUE);
5398 	return;
5399     }
5400 
5401     while (!ends_excmd(*arg_start))
5402     {
5403 	arg_end = skiptowhite(arg_start);
5404 	next_arg = skipwhite(arg_end);
5405 	vim_free(key);
5406 	key = vim_strnsave_up(arg_start, (int)(arg_end - arg_start));
5407 	if (STRCMP(key, "CCOMMENT") == 0)
5408 	{
5409 	    if (!eap->skip)
5410 		curbuf->b_syn_sync_flags |= SF_CCOMMENT;
5411 	    if (!ends_excmd(*next_arg))
5412 	    {
5413 		arg_end = skiptowhite(next_arg);
5414 		if (!eap->skip)
5415 		    curbuf->b_syn_sync_id = syn_check_group(next_arg,
5416 						   (int)(arg_end - next_arg));
5417 		next_arg = skipwhite(arg_end);
5418 	    }
5419 	    else if (!eap->skip)
5420 		curbuf->b_syn_sync_id = syn_name2id((char_u *)"Comment");
5421 	}
5422 	else if (  STRNCMP(key, "LINES", 5) == 0
5423 		|| STRNCMP(key, "MINLINES", 8) == 0
5424 		|| STRNCMP(key, "MAXLINES", 8) == 0
5425 		|| STRNCMP(key, "LINEBREAKS", 10) == 0)
5426 	{
5427 	    if (key[4] == 'S')
5428 		arg_end = key + 6;
5429 	    else if (key[0] == 'L')
5430 		arg_end = key + 11;
5431 	    else
5432 		arg_end = key + 9;
5433 	    if (arg_end[-1] != '=' || !VIM_ISDIGIT(*arg_end))
5434 	    {
5435 		illegal = TRUE;
5436 		break;
5437 	    }
5438 	    n = getdigits(&arg_end);
5439 	    if (!eap->skip)
5440 	    {
5441 		if (key[4] == 'B')
5442 		    curbuf->b_syn_sync_linebreaks = n;
5443 		else if (key[1] == 'A')
5444 		    curbuf->b_syn_sync_maxlines = n;
5445 		else
5446 		    curbuf->b_syn_sync_minlines = n;
5447 	    }
5448 	}
5449 	else if (STRCMP(key, "FROMSTART") == 0)
5450 	{
5451 	    if (!eap->skip)
5452 	    {
5453 		curbuf->b_syn_sync_minlines = MAXLNUM;
5454 		curbuf->b_syn_sync_maxlines = 0;
5455 	    }
5456 	}
5457 	else if (STRCMP(key, "LINECONT") == 0)
5458 	{
5459 	    if (curbuf->b_syn_linecont_pat != NULL)
5460 	    {
5461 		EMSG(_("E403: syntax sync: line continuations pattern specified twice"));
5462 		finished = TRUE;
5463 		break;
5464 	    }
5465 	    arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE, NULL);
5466 	    if (*arg_end != *next_arg)	    /* end delimiter not found */
5467 	    {
5468 		illegal = TRUE;
5469 		break;
5470 	    }
5471 
5472 	    if (!eap->skip)
5473 	    {
5474 		/* store the pattern and compiled regexp program */
5475 		if ((curbuf->b_syn_linecont_pat = vim_strnsave(next_arg + 1,
5476 				      (int)(arg_end - next_arg - 1))) == NULL)
5477 		{
5478 		    finished = TRUE;
5479 		    break;
5480 		}
5481 		curbuf->b_syn_linecont_ic = curbuf->b_syn_ic;
5482 
5483 		/* Make 'cpoptions' empty, to avoid the 'l' flag */
5484 		cpo_save = p_cpo;
5485 		p_cpo = (char_u *)"";
5486 		curbuf->b_syn_linecont_prog =
5487 			    vim_regcomp(curbuf->b_syn_linecont_pat, RE_MAGIC);
5488 		p_cpo = cpo_save;
5489 
5490 		if (curbuf->b_syn_linecont_prog == NULL)
5491 		{
5492 		    vim_free(curbuf->b_syn_linecont_pat);
5493 		    curbuf->b_syn_linecont_pat = NULL;
5494 		    finished = TRUE;
5495 		    break;
5496 		}
5497 	    }
5498 	    next_arg = skipwhite(arg_end + 1);
5499 	}
5500 	else
5501 	{
5502 	    eap->arg = next_arg;
5503 	    if (STRCMP(key, "MATCH") == 0)
5504 		syn_cmd_match(eap, TRUE);
5505 	    else if (STRCMP(key, "REGION") == 0)
5506 		syn_cmd_region(eap, TRUE);
5507 	    else if (STRCMP(key, "CLEAR") == 0)
5508 		syn_cmd_clear(eap, TRUE);
5509 	    else
5510 		illegal = TRUE;
5511 	    finished = TRUE;
5512 	    break;
5513 	}
5514 	arg_start = next_arg;
5515     }
5516     vim_free(key);
5517     if (illegal)
5518 	EMSG2(_("E404: Illegal arguments: %s"), arg_start);
5519     else if (!finished)
5520     {
5521 	eap->nextcmd = check_nextcmd(arg_start);
5522 	redraw_curbuf_later(SOME_VALID);
5523 	syn_stack_free_all(curbuf);	/* Need to recompute all syntax. */
5524     }
5525 }
5526 
5527 /*
5528  * Convert a line of highlight group names into a list of group ID numbers.
5529  * "arg" should point to the "contains" or "nextgroup" keyword.
5530  * "arg" is advanced to after the last group name.
5531  * Careful: the argument is modified (NULs added).
5532  * returns FAIL for some error, OK for success.
5533  */
5534     static int
5535 get_id_list(arg, keylen, list)
5536     char_u	**arg;
5537     int		keylen;		/* length of keyword */
5538     short	**list;		/* where to store the resulting list, if not
5539 				   NULL, the list is silently skipped! */
5540 {
5541     char_u	*p = NULL;
5542     char_u	*end;
5543     int		round;
5544     int		count;
5545     int		total_count = 0;
5546     short	*retval = NULL;
5547     char_u	*name;
5548     regmatch_T	regmatch;
5549     int		id;
5550     int		i;
5551     int		failed = FALSE;
5552 
5553     /*
5554      * We parse the list twice:
5555      * round == 1: count the number of items, allocate the array.
5556      * round == 2: fill the array with the items.
5557      * In round 1 new groups may be added, causing the number of items to
5558      * grow when a regexp is used.  In that case round 1 is done once again.
5559      */
5560     for (round = 1; round <= 2; ++round)
5561     {
5562 	/*
5563 	 * skip "contains"
5564 	 */
5565 	p = skipwhite(*arg + keylen);
5566 	if (*p != '=')
5567 	{
5568 	    EMSG2(_("E405: Missing equal sign: %s"), *arg);
5569 	    break;
5570 	}
5571 	p = skipwhite(p + 1);
5572 	if (ends_excmd(*p))
5573 	{
5574 	    EMSG2(_("E406: Empty argument: %s"), *arg);
5575 	    break;
5576 	}
5577 
5578 	/*
5579 	 * parse the arguments after "contains"
5580 	 */
5581 	count = 0;
5582 	while (!ends_excmd(*p))
5583 	{
5584 	    for (end = p; *end && !vim_iswhite(*end) && *end != ','; ++end)
5585 		;
5586 	    name = alloc((int)(end - p + 3));	    /* leave room for "^$" */
5587 	    if (name == NULL)
5588 	    {
5589 		failed = TRUE;
5590 		break;
5591 	    }
5592 	    vim_strncpy(name + 1, p, end - p);
5593 	    if (       STRCMP(name + 1, "ALLBUT") == 0
5594 		    || STRCMP(name + 1, "ALL") == 0
5595 		    || STRCMP(name + 1, "TOP") == 0
5596 		    || STRCMP(name + 1, "CONTAINED") == 0)
5597 	    {
5598 		if (TOUPPER_ASC(**arg) != 'C')
5599 		{
5600 		    EMSG2(_("E407: %s not allowed here"), name + 1);
5601 		    failed = TRUE;
5602 		    vim_free(name);
5603 		    break;
5604 		}
5605 		if (count != 0)
5606 		{
5607 		    EMSG2(_("E408: %s must be first in contains list"), name + 1);
5608 		    failed = TRUE;
5609 		    vim_free(name);
5610 		    break;
5611 		}
5612 		if (name[1] == 'A')
5613 		    id = SYNID_ALLBUT;
5614 		else if (name[1] == 'T')
5615 		    id = SYNID_TOP;
5616 		else
5617 		    id = SYNID_CONTAINED;
5618 		id += current_syn_inc_tag;
5619 	    }
5620 	    else if (name[1] == '@')
5621 	    {
5622 		id = syn_check_cluster(name + 2, (int)(end - p - 1));
5623 	    }
5624 	    else
5625 	    {
5626 		/*
5627 		 * Handle full group name.
5628 		 */
5629 		if (vim_strpbrk(name + 1, (char_u *)"\\.*^$~[") == NULL)
5630 		    id = syn_check_group(name + 1, (int)(end - p));
5631 		else
5632 		{
5633 		    /*
5634 		     * Handle match of regexp with group names.
5635 		     */
5636 		    *name = '^';
5637 		    STRCAT(name, "$");
5638 		    regmatch.regprog = vim_regcomp(name, RE_MAGIC);
5639 		    if (regmatch.regprog == NULL)
5640 		    {
5641 			failed = TRUE;
5642 			vim_free(name);
5643 			break;
5644 		    }
5645 
5646 		    regmatch.rm_ic = TRUE;
5647 		    id = 0;
5648 		    for (i = highlight_ga.ga_len; --i >= 0; )
5649 		    {
5650 			if (vim_regexec(&regmatch, HL_TABLE()[i].sg_name,
5651 								  (colnr_T)0))
5652 			{
5653 			    if (round == 2)
5654 			    {
5655 				/* Got more items than expected; can happen
5656 				 * when adding items that match:
5657 				 * "contains=a.*b,axb".
5658 				 * Go back to first round */
5659 				if (count >= total_count)
5660 				{
5661 				    vim_free(retval);
5662 				    round = 1;
5663 				}
5664 				else
5665 				    retval[count] = i + 1;
5666 			    }
5667 			    ++count;
5668 			    id = -1;	    /* remember that we found one */
5669 			}
5670 		    }
5671 		    vim_free(regmatch.regprog);
5672 		}
5673 	    }
5674 	    vim_free(name);
5675 	    if (id == 0)
5676 	    {
5677 		EMSG2(_("E409: Unknown group name: %s"), p);
5678 		failed = TRUE;
5679 		break;
5680 	    }
5681 	    if (id > 0)
5682 	    {
5683 		if (round == 2)
5684 		{
5685 		    /* Got more items than expected, go back to first round */
5686 		    if (count >= total_count)
5687 		    {
5688 			vim_free(retval);
5689 			round = 1;
5690 		    }
5691 		    else
5692 			retval[count] = id;
5693 		}
5694 		++count;
5695 	    }
5696 	    p = skipwhite(end);
5697 	    if (*p != ',')
5698 		break;
5699 	    p = skipwhite(p + 1);	/* skip comma in between arguments */
5700 	}
5701 	if (failed)
5702 	    break;
5703 	if (round == 1)
5704 	{
5705 	    retval = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
5706 	    if (retval == NULL)
5707 		break;
5708 	    retval[count] = 0;	    /* zero means end of the list */
5709 	    total_count = count;
5710 	}
5711     }
5712 
5713     *arg = p;
5714     if (failed || retval == NULL)
5715     {
5716 	vim_free(retval);
5717 	return FAIL;
5718     }
5719 
5720     if (*list == NULL)
5721 	*list = retval;
5722     else
5723 	vim_free(retval);	/* list already found, don't overwrite it */
5724 
5725     return OK;
5726 }
5727 
5728 /*
5729  * Make a copy of an ID list.
5730  */
5731     static short *
5732 copy_id_list(list)
5733     short   *list;
5734 {
5735     int	    len;
5736     int	    count;
5737     short   *retval;
5738 
5739     if (list == NULL)
5740 	return NULL;
5741 
5742     for (count = 0; list[count]; ++count)
5743 	;
5744     len = (count + 1) * sizeof(short);
5745     retval = (short *)alloc((unsigned)len);
5746     if (retval != NULL)
5747 	mch_memmove(retval, list, (size_t)len);
5748 
5749     return retval;
5750 }
5751 
5752 /*
5753  * Check if syntax group "ssp" is in the ID list "list" of "cur_si".
5754  * "cur_si" can be NULL if not checking the "containedin" list.
5755  * Used to check if a syntax item is in the "contains" or "nextgroup" list of
5756  * the current item.
5757  * This function is called very often, keep it fast!!
5758  */
5759     static int
5760 in_id_list(cur_si, list, ssp, contained)
5761     stateitem_T	*cur_si;	/* current item or NULL */
5762     short	*list;		/* id list */
5763     struct sp_syn *ssp;		/* group id and ":syn include" tag of group */
5764     int		contained;	/* group id is contained */
5765 {
5766     int		retval;
5767     short	*scl_list;
5768     short	item;
5769     short	id = ssp->id;
5770     static int	depth = 0;
5771     int		r;
5772 
5773     /* If spp has a "containedin" list and "cur_si" is in it, return TRUE. */
5774     if (cur_si != NULL && ssp->cont_in_list != NULL
5775 					    && !(cur_si->si_flags & HL_MATCH))
5776     {
5777 	/* Ignore transparent items without a contains argument.  Double check
5778 	 * that we don't go back past the first one. */
5779 	while ((cur_si->si_flags & HL_TRANS_CONT)
5780 		&& cur_si > (stateitem_T *)(current_state.ga_data))
5781 	    --cur_si;
5782 	/* cur_si->si_idx is -1 for keywords, these never contain anything. */
5783 	if (cur_si->si_idx >= 0 && in_id_list(NULL, ssp->cont_in_list,
5784 		&(SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_syn),
5785 		  SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_flags & HL_CONTAINED))
5786 	    return TRUE;
5787     }
5788 
5789     if (list == NULL)
5790 	return FALSE;
5791 
5792     /*
5793      * If list is ID_LIST_ALL, we are in a transparent item that isn't
5794      * inside anything.  Only allow not-contained groups.
5795      */
5796     if (list == ID_LIST_ALL)
5797 	return !contained;
5798 
5799     /*
5800      * If the first item is "ALLBUT", return TRUE if "id" is NOT in the
5801      * contains list.  We also require that "id" is at the same ":syn include"
5802      * level as the list.
5803      */
5804     item = *list;
5805     if (item >= SYNID_ALLBUT && item < SYNID_CLUSTER)
5806     {
5807 	if (item < SYNID_TOP)
5808 	{
5809 	    /* ALL or ALLBUT: accept all groups in the same file */
5810 	    if (item - SYNID_ALLBUT != ssp->inc_tag)
5811 		return FALSE;
5812 	}
5813 	else if (item < SYNID_CONTAINED)
5814 	{
5815 	    /* TOP: accept all not-contained groups in the same file */
5816 	    if (item - SYNID_TOP != ssp->inc_tag || contained)
5817 		return FALSE;
5818 	}
5819 	else
5820 	{
5821 	    /* CONTAINED: accept all contained groups in the same file */
5822 	    if (item - SYNID_CONTAINED != ssp->inc_tag || !contained)
5823 		return FALSE;
5824 	}
5825 	item = *++list;
5826 	retval = FALSE;
5827     }
5828     else
5829 	retval = TRUE;
5830 
5831     /*
5832      * Return "retval" if id is in the contains list.
5833      */
5834     while (item != 0)
5835     {
5836 	if (item == id)
5837 	    return retval;
5838 	if (item >= SYNID_CLUSTER)
5839 	{
5840 	    scl_list = SYN_CLSTR(syn_buf)[item - SYNID_CLUSTER].scl_list;
5841 	    /* restrict recursiveness to 30 to avoid an endless loop for a
5842 	     * cluster that includes itself (indirectly) */
5843 	    if (scl_list != NULL && depth < 30)
5844 	    {
5845 		++depth;
5846 		r = in_id_list(NULL, scl_list, ssp, contained);
5847 		--depth;
5848 		if (r)
5849 		    return retval;
5850 	    }
5851 	}
5852 	item = *++list;
5853     }
5854     return !retval;
5855 }
5856 
5857 struct subcommand
5858 {
5859     char    *name;				/* subcommand name */
5860     void    (*func)__ARGS((exarg_T *, int));	/* function to call */
5861 };
5862 
5863 static struct subcommand subcommands[] =
5864 {
5865     {"case",		syn_cmd_case},
5866     {"clear",		syn_cmd_clear},
5867     {"cluster",		syn_cmd_cluster},
5868     {"enable",		syn_cmd_enable},
5869     {"include",		syn_cmd_include},
5870     {"keyword",		syn_cmd_keyword},
5871     {"list",		syn_cmd_list},
5872     {"manual",		syn_cmd_manual},
5873     {"match",		syn_cmd_match},
5874     {"on",		syn_cmd_on},
5875     {"off",		syn_cmd_off},
5876     {"region",		syn_cmd_region},
5877     {"reset",		syn_cmd_reset},
5878     {"spell",		syn_cmd_spell},
5879     {"sync",		syn_cmd_sync},
5880     {"",		syn_cmd_list},
5881     {NULL, NULL}
5882 };
5883 
5884 /*
5885  * ":syntax".
5886  * This searches the subcommands[] table for the subcommand name, and calls a
5887  * syntax_subcommand() function to do the rest.
5888  */
5889     void
5890 ex_syntax(eap)
5891     exarg_T	*eap;
5892 {
5893     char_u	*arg = eap->arg;
5894     char_u	*subcmd_end;
5895     char_u	*subcmd_name;
5896     int		i;
5897 
5898     syn_cmdlinep = eap->cmdlinep;
5899 
5900     /* isolate subcommand name */
5901     for (subcmd_end = arg; ASCII_ISALPHA(*subcmd_end); ++subcmd_end)
5902 	;
5903     subcmd_name = vim_strnsave(arg, (int)(subcmd_end - arg));
5904     if (subcmd_name != NULL)
5905     {
5906 	if (eap->skip)		/* skip error messages for all subcommands */
5907 	    ++emsg_skip;
5908 	for (i = 0; ; ++i)
5909 	{
5910 	    if (subcommands[i].name == NULL)
5911 	    {
5912 		EMSG2(_("E410: Invalid :syntax subcommand: %s"), subcmd_name);
5913 		break;
5914 	    }
5915 	    if (STRCMP(subcmd_name, (char_u *)subcommands[i].name) == 0)
5916 	    {
5917 		eap->arg = skipwhite(subcmd_end);
5918 		(subcommands[i].func)(eap, FALSE);
5919 		break;
5920 	    }
5921 	}
5922 	vim_free(subcmd_name);
5923 	if (eap->skip)
5924 	    --emsg_skip;
5925     }
5926 }
5927 
5928     int
5929 syntax_present(buf)
5930     buf_T	*buf;
5931 {
5932     return (buf->b_syn_patterns.ga_len != 0
5933 	    || buf->b_syn_clusters.ga_len != 0
5934 	    || curbuf->b_keywtab.ht_used > 0
5935 	    || curbuf->b_keywtab_ic.ht_used > 0);
5936 }
5937 
5938 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5939 
5940 static enum
5941 {
5942     EXP_SUBCMD,	    /* expand ":syn" sub-commands */
5943     EXP_CASE	    /* expand ":syn case" arguments */
5944 } expand_what;
5945 
5946 
5947 /*
5948  * Handle command line completion for :syntax command.
5949  */
5950     void
5951 set_context_in_syntax_cmd(xp, arg)
5952     expand_T	*xp;
5953     char_u	*arg;
5954 {
5955     char_u	*p;
5956 
5957     /* Default: expand subcommands */
5958     xp->xp_context = EXPAND_SYNTAX;
5959     expand_what = EXP_SUBCMD;
5960     xp->xp_pattern = arg;
5961     include_link = FALSE;
5962     include_default = FALSE;
5963 
5964     /* (part of) subcommand already typed */
5965     if (*arg != NUL)
5966     {
5967 	p = skiptowhite(arg);
5968 	if (*p != NUL)		    /* past first word */
5969 	{
5970 	    xp->xp_pattern = skipwhite(p);
5971 	    if (*skiptowhite(xp->xp_pattern) != NUL)
5972 		xp->xp_context = EXPAND_NOTHING;
5973 	    else if (STRNICMP(arg, "case", p - arg) == 0)
5974 		expand_what = EXP_CASE;
5975 	    else if (  STRNICMP(arg, "keyword", p - arg) == 0
5976 		    || STRNICMP(arg, "region", p - arg) == 0
5977 		    || STRNICMP(arg, "match", p - arg) == 0
5978 		    || STRNICMP(arg, "list", p - arg) == 0)
5979 		xp->xp_context = EXPAND_HIGHLIGHT;
5980 	    else
5981 		xp->xp_context = EXPAND_NOTHING;
5982 	}
5983     }
5984 }
5985 
5986 static char *(case_args[]) = {"match", "ignore", NULL};
5987 
5988 /*
5989  * Function given to ExpandGeneric() to obtain the list syntax names for
5990  * expansion.
5991  */
5992 /*ARGSUSED*/
5993     char_u *
5994 get_syntax_name(xp, idx)
5995     expand_T	*xp;
5996     int		idx;
5997 {
5998     if (expand_what == EXP_SUBCMD)
5999 	return (char_u *)subcommands[idx].name;
6000     return (char_u *)case_args[idx];
6001 }
6002 
6003 #endif /* FEAT_CMDL_COMPL */
6004 
6005 /*
6006  * Function called for expression evaluation: get syntax ID at file position.
6007  */
6008     int
6009 syn_get_id(wp, lnum, col, trans, spellp)
6010     win_T	*wp;
6011     long	lnum;
6012     colnr_T	col;
6013     int		trans;	    /* remove transparancy */
6014     int		*spellp;    /* return: can do spell checking */
6015 {
6016     /* When the position is not after the current position and in the same
6017      * line of the same buffer, need to restart parsing. */
6018     if (wp->w_buffer != syn_buf
6019 	    || lnum != current_lnum
6020 	    || col < current_col)
6021 	syntax_start(wp, lnum);
6022 
6023     (void)get_syntax_attr(col, spellp);
6024 
6025     return (trans ? current_trans_id : current_id);
6026 }
6027 
6028 #if defined(FEAT_FOLDING) || defined(PROTO)
6029 /*
6030  * Function called to get folding level for line "lnum" in window "wp".
6031  */
6032     int
6033 syn_get_foldlevel(wp, lnum)
6034     win_T	*wp;
6035     long	lnum;
6036 {
6037     int		level = 0;
6038     int		i;
6039 
6040     /* Return quickly when there are no fold items at all. */
6041     if (wp->w_buffer->b_syn_folditems != 0)
6042     {
6043 	syntax_start(wp, lnum);
6044 
6045 	for (i = 0; i < current_state.ga_len; ++i)
6046 	    if (CUR_STATE(i).si_flags & HL_FOLD)
6047 		++level;
6048     }
6049     if (level > wp->w_p_fdn)
6050 	level = wp->w_p_fdn;
6051     return level;
6052 }
6053 #endif
6054 
6055 #endif /* FEAT_SYN_HL */
6056 
6057 
6058 /**************************************
6059  *  Highlighting stuff		      *
6060  **************************************/
6061 
6062 /*
6063  * The default highlight groups.  These are compiled-in for fast startup and
6064  * they still work when the runtime files can't be found.
6065  * When making changes here, also change runtime/colors/default.vim!
6066  */
6067 static char *(highlight_init_both[]) =
6068     {
6069 #ifdef FEAT_GUI
6070 	"Cursor guibg=fg guifg=bg",
6071 	"lCursor guibg=fg guifg=bg",	/* should be different, but what? */
6072 #endif
6073 	"ErrorMsg term=standout ctermbg=DarkRed ctermfg=White guibg=Red guifg=White",
6074 	"IncSearch term=reverse cterm=reverse gui=reverse",
6075 	"ModeMsg term=bold cterm=bold gui=bold",
6076 	"NonText term=bold ctermfg=Blue gui=bold guifg=Blue",
6077 	"StatusLine term=reverse,bold cterm=reverse,bold gui=reverse,bold",
6078 	"StatusLineNC term=reverse cterm=reverse gui=reverse",
6079 	"VertSplit term=reverse cterm=reverse gui=reverse",
6080 	"VisualNOS term=underline,bold cterm=underline,bold gui=underline,bold",
6081 	"DiffText term=reverse cterm=bold ctermbg=Red gui=bold guibg=Red",
6082 	"PmenuThumb cterm=reverse gui=reverse",
6083 	"PmenuSbar ctermbg=Grey guibg=Grey",
6084 	"TabLineSel term=bold cterm=bold gui=bold",
6085 	"TabLineFill term=reverse cterm=reverse gui=reverse",
6086 	"MatchParen term=reverse ctermbg=Cyan guibg=Cyan",
6087 	NULL
6088     };
6089 
6090 static char *(highlight_init_light[]) =
6091     {
6092 	"Directory term=bold ctermfg=DarkBlue guifg=Blue",
6093 	"LineNr term=underline ctermfg=Brown guifg=Brown",
6094 	"MoreMsg term=bold ctermfg=DarkGreen gui=bold guifg=SeaGreen",
6095 	"Normal gui=NONE",
6096 	"Question term=standout ctermfg=DarkGreen gui=bold guifg=SeaGreen",
6097 	"Search term=reverse ctermbg=Yellow ctermfg=NONE guibg=Yellow guifg=NONE",
6098 	"SpellBad term=reverse ctermbg=LightRed guisp=Red gui=undercurl",
6099 	"SpellCap term=reverse ctermbg=LightBlue guisp=Blue gui=undercurl",
6100 	"SpellRare term=reverse ctermbg=LightMagenta guisp=Magenta gui=undercurl",
6101 	"SpellLocal term=underline ctermbg=Cyan guisp=DarkCyan gui=undercurl",
6102 	"Pmenu ctermbg=LightMagenta guibg=LightMagenta",
6103 	"PmenuSel ctermbg=LightGrey guibg=Grey",
6104 	"SpecialKey term=bold ctermfg=DarkBlue guifg=Blue",
6105 	"Title term=bold ctermfg=DarkMagenta gui=bold guifg=Magenta",
6106 	"WarningMsg term=standout ctermfg=DarkRed guifg=Red",
6107 	"WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
6108 	"Folded term=standout ctermbg=Grey ctermfg=DarkBlue guibg=LightGrey guifg=DarkBlue",
6109 	"FoldColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue",
6110 	"SignColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue",
6111 	"Visual term=reverse ctermbg=Magenta guibg=LightGrey",
6112 	"DiffAdd term=bold ctermbg=LightBlue guibg=LightBlue",
6113 	"DiffChange term=bold ctermbg=LightMagenta guibg=LightMagenta",
6114 	"DiffDelete term=bold ctermfg=Blue ctermbg=LightCyan gui=bold guifg=Blue guibg=LightCyan",
6115 	"TabLine term=underline cterm=underline ctermfg=black ctermbg=LightGrey gui=underline guibg=LightGrey",
6116 	"CursorColumn term=reverse ctermbg=LightGrey guibg=LightGrey",
6117 	"CursorLine term=underline cterm=underline guibg=LightGrey",
6118 	NULL
6119     };
6120 
6121 static char *(highlight_init_dark[]) =
6122     {
6123 	"Directory term=bold ctermfg=LightCyan guifg=Cyan",
6124 	"LineNr term=underline ctermfg=Yellow guifg=Yellow",
6125 	"MoreMsg term=bold ctermfg=LightGreen gui=bold guifg=SeaGreen",
6126 	"Normal gui=NONE",
6127 	"Question term=standout ctermfg=LightGreen gui=bold guifg=Green",
6128 	"Search term=reverse ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
6129 	"SpecialKey term=bold ctermfg=LightBlue guifg=Cyan",
6130 	"SpellBad term=reverse ctermbg=Red guisp=Red gui=undercurl",
6131 	"SpellCap term=reverse ctermbg=Blue guisp=Blue gui=undercurl",
6132 	"SpellRare term=reverse ctermbg=Magenta guisp=Magenta gui=undercurl",
6133 	"SpellLocal term=underline ctermbg=Cyan guisp=Cyan gui=undercurl",
6134 	"Pmenu ctermbg=Magenta guibg=Magenta",
6135 	"PmenuSel ctermbg=DarkGrey guibg=DarkGrey",
6136 	"Title term=bold ctermfg=LightMagenta gui=bold guifg=Magenta",
6137 	"WarningMsg term=standout ctermfg=LightRed guifg=Red",
6138 	"WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
6139 	"Folded term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=DarkGrey guifg=Cyan",
6140 	"FoldColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan",
6141 	"SignColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan",
6142 	"Visual term=reverse ctermbg=Magenta guibg=DarkGrey",
6143 	"DiffAdd term=bold ctermbg=DarkBlue guibg=DarkBlue",
6144 	"DiffChange term=bold ctermbg=DarkMagenta guibg=DarkMagenta",
6145 	"DiffDelete term=bold ctermfg=Blue ctermbg=DarkCyan gui=bold guifg=Blue guibg=DarkCyan",
6146 	"TabLine term=underline cterm=underline ctermfg=white ctermbg=DarkGrey gui=underline guibg=DarkGrey",
6147 	"CursorColumn term=reverse ctermbg=DarkGrey guibg=DarkGrey",
6148 	"CursorLine term=underline cterm=underline guibg=DarkGrey",
6149 	NULL
6150     };
6151 
6152     void
6153 init_highlight(both, reset)
6154     int		both;	    /* include groups where 'bg' doesn't matter */
6155     int		reset;	    /* clear group first */
6156 {
6157     int		i;
6158     char	**pp;
6159     static int	had_both = FALSE;
6160 #ifdef FEAT_EVAL
6161     char_u	*p;
6162 
6163     /*
6164      * Try finding the color scheme file.  Used when a color file was loaded
6165      * and 'background' or 't_Co' is changed.
6166      */
6167     p = get_var_value((char_u *)"g:colors_name");
6168     if (p != NULL && load_colors(p) == OK)
6169 	return;
6170 #endif
6171 
6172     /*
6173      * Didn't use a color file, use the compiled-in colors.
6174      */
6175     if (both)
6176     {
6177 	had_both = TRUE;
6178 	pp = highlight_init_both;
6179 	for (i = 0; pp[i] != NULL; ++i)
6180 	    do_highlight((char_u *)pp[i], reset, TRUE);
6181     }
6182     else if (!had_both)
6183 	/* Don't do anything before the call with both == TRUE from main().
6184 	 * Not everything has been setup then, and that call will overrule
6185 	 * everything anyway. */
6186 	return;
6187 
6188     if (*p_bg == 'l')
6189 	pp = highlight_init_light;
6190     else
6191 	pp = highlight_init_dark;
6192     for (i = 0; pp[i] != NULL; ++i)
6193 	do_highlight((char_u *)pp[i], reset, TRUE);
6194 
6195     /* Magenta background looks ugly, but grey may not work for 8 colors.
6196      * Thus let it depend on the number of colors available. */
6197     if (t_colors > 8)
6198 	do_highlight((char_u *)(*p_bg == 'l' ? "Visual ctermbg=LightGrey"
6199 				   : "Visual ctermbg=DarkGrey"), reset, TRUE);
6200 
6201 #ifdef FEAT_SYN_HL
6202     /*
6203      * If syntax highlighting is enabled load the highlighting for it.
6204      */
6205     if (get_var_value((char_u *)"g:syntax_on") != NULL)
6206     {
6207 	static int	recursive = 0;
6208 
6209 	if (recursive >= 5)
6210 	    EMSG(_("E679: recursive loop loading syncolor.vim"));
6211 	else
6212 	{
6213 	    ++recursive;
6214 	    (void)source_runtime((char_u *)"syntax/syncolor.vim", TRUE);
6215 	    --recursive;
6216 	}
6217     }
6218 #endif
6219 }
6220 
6221 /*
6222  * Load color file "name".
6223  * Return OK for success, FAIL for failure.
6224  */
6225     int
6226 load_colors(name)
6227     char_u	*name;
6228 {
6229     char_u	*buf;
6230     int		retval = FAIL;
6231     static int	recursive = FALSE;
6232 
6233     /* When being called recursively, this is probably because setting
6234      * 'background' caused the highlighting to be reloaded.  This means it is
6235      * working, thus we should return OK. */
6236     if (recursive)
6237 	return OK;
6238 
6239     recursive = TRUE;
6240     buf = alloc((unsigned)(STRLEN(name) + 12));
6241     if (buf != NULL)
6242     {
6243 	sprintf((char *)buf, "colors/%s.vim", name);
6244 	retval = source_runtime(buf, FALSE);
6245 	vim_free(buf);
6246 #ifdef FEAT_AUTOCMD
6247 	apply_autocmds(EVENT_COLORSCHEME, NULL, NULL, FALSE, curbuf);
6248 #endif
6249     }
6250     recursive = FALSE;
6251 
6252     return retval;
6253 }
6254 
6255 /*
6256  * Handle the ":highlight .." command.
6257  * When using ":hi clear" this is called recursively for each group with
6258  * "forceit" and "init" both TRUE.
6259  */
6260     void
6261 do_highlight(line, forceit, init)
6262     char_u	*line;
6263     int		forceit;
6264     int		init;	    /* TRUE when called for initializing */
6265 {
6266     char_u	*name_end;
6267     char_u	*p;
6268     char_u	*linep;
6269     char_u	*key_start;
6270     char_u	*arg_start;
6271     char_u	*key = NULL, *arg = NULL;
6272     long	i;
6273     int		off;
6274     int		len;
6275     int		attr;
6276     int		id;
6277     int		idx;
6278     int		dodefault = FALSE;
6279     int		doclear = FALSE;
6280     int		dolink = FALSE;
6281     int		error = FALSE;
6282     int		color;
6283     int		is_normal_group = FALSE;	/* "Normal" group */
6284 #ifdef FEAT_GUI_X11
6285     int		is_menu_group = FALSE;		/* "Menu" group */
6286     int		is_scrollbar_group = FALSE;	/* "Scrollbar" group */
6287     int		is_tooltip_group = FALSE;	/* "Tooltip" group */
6288     int		do_colors = FALSE;		/* need to update colors? */
6289 #else
6290 # define is_menu_group 0
6291 # define is_tooltip_group 0
6292 #endif
6293 
6294     /*
6295      * If no argument, list current highlighting.
6296      */
6297     if (ends_excmd(*line))
6298     {
6299 	for (i = 1; i <= highlight_ga.ga_len && !got_int; ++i)
6300 	    /* TODO: only call when the group has attributes set */
6301 	    highlight_list_one((int)i);
6302 	return;
6303     }
6304 
6305     /*
6306      * Isolate the name.
6307      */
6308     name_end = skiptowhite(line);
6309     linep = skipwhite(name_end);
6310 
6311     /*
6312      * Check for "default" argument.
6313      */
6314     if (STRNCMP(line, "default", name_end - line) == 0)
6315     {
6316 	dodefault = TRUE;
6317 	line = linep;
6318 	name_end = skiptowhite(line);
6319 	linep = skipwhite(name_end);
6320     }
6321 
6322     /*
6323      * Check for "clear" or "link" argument.
6324      */
6325     if (STRNCMP(line, "clear", name_end - line) == 0)
6326 	doclear = TRUE;
6327     if (STRNCMP(line, "link", name_end - line) == 0)
6328 	dolink = TRUE;
6329 
6330     /*
6331      * ":highlight {group-name}": list highlighting for one group.
6332      */
6333     if (!doclear && !dolink && ends_excmd(*linep))
6334     {
6335 	id = syn_namen2id(line, (int)(name_end - line));
6336 	if (id == 0)
6337 	    EMSG2(_("E411: highlight group not found: %s"), line);
6338 	else
6339 	    highlight_list_one(id);
6340 	return;
6341     }
6342 
6343     /*
6344      * Handle ":highlight link {from} {to}" command.
6345      */
6346     if (dolink)
6347     {
6348 	char_u	    *from_start = linep;
6349 	char_u	    *from_end;
6350 	char_u	    *to_start;
6351 	char_u	    *to_end;
6352 	int	    from_id;
6353 	int	    to_id;
6354 
6355 	from_end = skiptowhite(from_start);
6356 	to_start = skipwhite(from_end);
6357 	to_end	 = skiptowhite(to_start);
6358 
6359 	if (ends_excmd(*from_start) || ends_excmd(*to_start))
6360 	{
6361 	    EMSG2(_("E412: Not enough arguments: \":highlight link %s\""),
6362 								  from_start);
6363 	    return;
6364 	}
6365 
6366 	if (!ends_excmd(*skipwhite(to_end)))
6367 	{
6368 	    EMSG2(_("E413: Too many arguments: \":highlight link %s\""), from_start);
6369 	    return;
6370 	}
6371 
6372 	from_id = syn_check_group(from_start, (int)(from_end - from_start));
6373 	if (STRNCMP(to_start, "NONE", 4) == 0)
6374 	    to_id = 0;
6375 	else
6376 	    to_id = syn_check_group(to_start, (int)(to_end - to_start));
6377 
6378 	if (from_id > 0 && (!init || HL_TABLE()[from_id - 1].sg_set == 0))
6379 	{
6380 	    /*
6381 	     * Don't allow a link when there already is some highlighting
6382 	     * for the group, unless '!' is used
6383 	     */
6384 	    if (to_id > 0 && !forceit && !init
6385 				   && hl_has_settings(from_id - 1, dodefault))
6386 	    {
6387 		if (sourcing_name == NULL && !dodefault)
6388 		    EMSG(_("E414: group has settings, highlight link ignored"));
6389 	    }
6390 	    else
6391 	    {
6392 		if (!init)
6393 		    HL_TABLE()[from_id - 1].sg_set |= SG_LINK;
6394 		HL_TABLE()[from_id - 1].sg_link = to_id;
6395 #ifdef FEAT_EVAL
6396 		HL_TABLE()[from_id - 1].sg_scriptID = current_SID;
6397 #endif
6398 		redraw_all_later(SOME_VALID);
6399 	    }
6400 	}
6401 
6402 	/* Only call highlight_changed() once, after sourcing a syntax file */
6403 	need_highlight_changed = TRUE;
6404 
6405 	return;
6406     }
6407 
6408     if (doclear)
6409     {
6410 	/*
6411 	 * ":highlight clear [group]" command.
6412 	 */
6413 	line = linep;
6414 	if (ends_excmd(*line))
6415 	{
6416 #ifdef FEAT_GUI
6417 	    /* First, we do not destroy the old values, but allocate the new
6418 	     * ones and update the display. THEN we destroy the old values.
6419 	     * If we destroy the old values first, then the old values
6420 	     * (such as GuiFont's or GuiFontset's) will still be displayed but
6421 	     * invalid because they were free'd.
6422 	     */
6423 	    if (gui.in_use)
6424 	    {
6425 # ifdef FEAT_BEVAL_TIP
6426 		gui_init_tooltip_font();
6427 # endif
6428 # if defined(FEAT_MENU) && (defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MOTIF))
6429 		gui_init_menu_font();
6430 # endif
6431 	    }
6432 # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_X11)
6433 	    gui_mch_def_colors();
6434 # endif
6435 # ifdef FEAT_GUI_X11
6436 #  ifdef FEAT_MENU
6437 
6438 	    /* This only needs to be done when there is no Menu highlight
6439 	     * group defined by default, which IS currently the case.
6440 	     */
6441 	    gui_mch_new_menu_colors();
6442 #  endif
6443 	    if (gui.in_use)
6444 	    {
6445 		gui_new_scrollbar_colors();
6446 #  ifdef FEAT_BEVAL
6447 		gui_mch_new_tooltip_colors();
6448 #  endif
6449 #  ifdef FEAT_MENU
6450 		gui_mch_new_menu_font();
6451 #  endif
6452 	    }
6453 # endif
6454 
6455 	    /* Ok, we're done allocating the new default graphics items.
6456 	     * The screen should already be refreshed at this point.
6457 	     * It is now Ok to clear out the old data.
6458 	     */
6459 #endif
6460 #ifdef FEAT_EVAL
6461 	    do_unlet((char_u *)"colors_name", TRUE);
6462 #endif
6463 	    restore_cterm_colors();
6464 
6465 	    /*
6466 	     * Clear all default highlight groups and load the defaults.
6467 	     */
6468 	    for (idx = 0; idx < highlight_ga.ga_len; ++idx)
6469 		highlight_clear(idx);
6470 	    init_highlight(TRUE, TRUE);
6471 #ifdef FEAT_GUI
6472 	    if (gui.in_use)
6473 		highlight_gui_started();
6474 #endif
6475 	    highlight_changed();
6476 	    redraw_later_clear();
6477 	    return;
6478 	}
6479 	name_end = skiptowhite(line);
6480 	linep = skipwhite(name_end);
6481     }
6482 
6483     /*
6484      * Find the group name in the table.  If it does not exist yet, add it.
6485      */
6486     id = syn_check_group(line, (int)(name_end - line));
6487     if (id == 0)			/* failed (out of memory) */
6488 	return;
6489     idx = id - 1;			/* index is ID minus one */
6490 
6491     /* Return if "default" was used and the group already has settings. */
6492     if (dodefault && hl_has_settings(idx, TRUE))
6493 	return;
6494 
6495     if (STRCMP(HL_TABLE()[idx].sg_name_u, "NORMAL") == 0)
6496 	is_normal_group = TRUE;
6497 #ifdef FEAT_GUI_X11
6498     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "MENU") == 0)
6499 	is_menu_group = TRUE;
6500     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "SCROLLBAR") == 0)
6501 	is_scrollbar_group = TRUE;
6502     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "TOOLTIP") == 0)
6503 	is_tooltip_group = TRUE;
6504 #endif
6505 
6506     /* Clear the highlighting for ":hi clear {group}" and ":hi clear". */
6507     if (doclear || (forceit && init))
6508     {
6509 	highlight_clear(idx);
6510 	if (!doclear)
6511 	    HL_TABLE()[idx].sg_set = 0;
6512     }
6513 
6514     if (!doclear)
6515       while (!ends_excmd(*linep))
6516       {
6517 	key_start = linep;
6518 	if (*linep == '=')
6519 	{
6520 	    EMSG2(_("E415: unexpected equal sign: %s"), key_start);
6521 	    error = TRUE;
6522 	    break;
6523 	}
6524 
6525 	/*
6526 	 * Isolate the key ("term", "ctermfg", "ctermbg", "font", "guifg" or
6527 	 * "guibg").
6528 	 */
6529 	while (*linep && !vim_iswhite(*linep) && *linep != '=')
6530 	    ++linep;
6531 	vim_free(key);
6532 	key = vim_strnsave_up(key_start, (int)(linep - key_start));
6533 	if (key == NULL)
6534 	{
6535 	    error = TRUE;
6536 	    break;
6537 	}
6538 	linep = skipwhite(linep);
6539 
6540 	if (STRCMP(key, "NONE") == 0)
6541 	{
6542 	    if (!init || HL_TABLE()[idx].sg_set == 0)
6543 	    {
6544 		if (!init)
6545 		    HL_TABLE()[idx].sg_set |= SG_TERM+SG_CTERM+SG_GUI;
6546 		highlight_clear(idx);
6547 	    }
6548 	    continue;
6549 	}
6550 
6551 	/*
6552 	 * Check for the equal sign.
6553 	 */
6554 	if (*linep != '=')
6555 	{
6556 	    EMSG2(_("E416: missing equal sign: %s"), key_start);
6557 	    error = TRUE;
6558 	    break;
6559 	}
6560 	++linep;
6561 
6562 	/*
6563 	 * Isolate the argument.
6564 	 */
6565 	linep = skipwhite(linep);
6566 	if (*linep == '\'')		/* guifg='color name' */
6567 	{
6568 	    arg_start = ++linep;
6569 	    linep = vim_strchr(linep, '\'');
6570 	    if (linep == NULL)
6571 	    {
6572 		EMSG2(_(e_invarg2), key_start);
6573 		error = TRUE;
6574 		break;
6575 	    }
6576 	}
6577 	else
6578 	{
6579 	    arg_start = linep;
6580 	    linep = skiptowhite(linep);
6581 	}
6582 	if (linep == arg_start)
6583 	{
6584 	    EMSG2(_("E417: missing argument: %s"), key_start);
6585 	    error = TRUE;
6586 	    break;
6587 	}
6588 	vim_free(arg);
6589 	arg = vim_strnsave(arg_start, (int)(linep - arg_start));
6590 	if (arg == NULL)
6591 	{
6592 	    error = TRUE;
6593 	    break;
6594 	}
6595 	if (*linep == '\'')
6596 	    ++linep;
6597 
6598 	/*
6599 	 * Store the argument.
6600 	 */
6601 	if (  STRCMP(key, "TERM") == 0
6602 		|| STRCMP(key, "CTERM") == 0
6603 		|| STRCMP(key, "GUI") == 0)
6604 	{
6605 	    attr = 0;
6606 	    off = 0;
6607 	    while (arg[off] != NUL)
6608 	    {
6609 		for (i = sizeof(hl_attr_table) / sizeof(int); --i >= 0; )
6610 		{
6611 		    len = (int)STRLEN(hl_name_table[i]);
6612 		    if (STRNICMP(arg + off, hl_name_table[i], len) == 0)
6613 		    {
6614 			attr |= hl_attr_table[i];
6615 			off += len;
6616 			break;
6617 		    }
6618 		}
6619 		if (i < 0)
6620 		{
6621 		    EMSG2(_("E418: Illegal value: %s"), arg);
6622 		    error = TRUE;
6623 		    break;
6624 		}
6625 		if (arg[off] == ',')		/* another one follows */
6626 		    ++off;
6627 	    }
6628 	    if (error)
6629 		break;
6630 	    if (*key == 'T')
6631 	    {
6632 		if (!init || !(HL_TABLE()[idx].sg_set & SG_TERM))
6633 		{
6634 		    if (!init)
6635 			HL_TABLE()[idx].sg_set |= SG_TERM;
6636 		    HL_TABLE()[idx].sg_term = attr;
6637 		}
6638 	    }
6639 	    else if (*key == 'C')
6640 	    {
6641 		if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
6642 		{
6643 		    if (!init)
6644 			HL_TABLE()[idx].sg_set |= SG_CTERM;
6645 		    HL_TABLE()[idx].sg_cterm = attr;
6646 		    HL_TABLE()[idx].sg_cterm_bold = FALSE;
6647 		}
6648 	    }
6649 #ifdef FEAT_GUI
6650 	    else
6651 	    {
6652 		if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
6653 		{
6654 		    if (!init)
6655 			HL_TABLE()[idx].sg_set |= SG_GUI;
6656 		    HL_TABLE()[idx].sg_gui = attr;
6657 		}
6658 	    }
6659 #endif
6660 	}
6661 	else if (STRCMP(key, "FONT") == 0)
6662 	{
6663 	    /* in non-GUI fonts are simply ignored */
6664 #ifdef FEAT_GUI
6665 	    if (!gui.shell_created)
6666 	    {
6667 		/* GUI not started yet, always accept the name. */
6668 		vim_free(HL_TABLE()[idx].sg_font_name);
6669 		HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
6670 	    }
6671 	    else
6672 	    {
6673 		GuiFont temp_sg_font = HL_TABLE()[idx].sg_font;
6674 # ifdef FEAT_XFONTSET
6675 		GuiFontset temp_sg_fontset = HL_TABLE()[idx].sg_fontset;
6676 # endif
6677 		/* First, save the current font/fontset.
6678 		 * Then try to allocate the font/fontset.
6679 		 * If the allocation fails, HL_TABLE()[idx].sg_font OR
6680 		 * sg_fontset will be set to NOFONT or NOFONTSET respectively.
6681 		 */
6682 
6683 		HL_TABLE()[idx].sg_font = NOFONT;
6684 # ifdef FEAT_XFONTSET
6685 		HL_TABLE()[idx].sg_fontset = NOFONTSET;
6686 # endif
6687 		hl_do_font(idx, arg, is_normal_group, is_menu_group,
6688 							    is_tooltip_group);
6689 
6690 # ifdef FEAT_XFONTSET
6691 		if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
6692 		{
6693 		    /* New fontset was accepted. Free the old one, if there was
6694 		     * one.
6695 		     */
6696 		    gui_mch_free_fontset(temp_sg_fontset);
6697 		    vim_free(HL_TABLE()[idx].sg_font_name);
6698 		    HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
6699 		}
6700 		else
6701 		    HL_TABLE()[idx].sg_fontset = temp_sg_fontset;
6702 # endif
6703 		if (HL_TABLE()[idx].sg_font != NOFONT)
6704 		{
6705 		    /* New font was accepted. Free the old one, if there was
6706 		     * one.
6707 		     */
6708 		    gui_mch_free_font(temp_sg_font);
6709 		    vim_free(HL_TABLE()[idx].sg_font_name);
6710 		    HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
6711 		}
6712 		else
6713 		    HL_TABLE()[idx].sg_font = temp_sg_font;
6714 	    }
6715 #endif
6716 	}
6717 	else if (STRCMP(key, "CTERMFG") == 0 || STRCMP(key, "CTERMBG") == 0)
6718 	{
6719 	  if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
6720 	  {
6721 	    if (!init)
6722 		HL_TABLE()[idx].sg_set |= SG_CTERM;
6723 
6724 	    /* When setting the foreground color, and previously the "bold"
6725 	     * flag was set for a light color, reset it now */
6726 	    if (key[5] == 'F' && HL_TABLE()[idx].sg_cterm_bold)
6727 	    {
6728 		HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
6729 		HL_TABLE()[idx].sg_cterm_bold = FALSE;
6730 	    }
6731 
6732 	    if (VIM_ISDIGIT(*arg))
6733 		color = atoi((char *)arg);
6734 	    else if (STRICMP(arg, "fg") == 0)
6735 	    {
6736 		if (cterm_normal_fg_color)
6737 		    color = cterm_normal_fg_color - 1;
6738 		else
6739 		{
6740 		    EMSG(_("E419: FG color unknown"));
6741 		    error = TRUE;
6742 		    break;
6743 		}
6744 	    }
6745 	    else if (STRICMP(arg, "bg") == 0)
6746 	    {
6747 		if (cterm_normal_bg_color > 0)
6748 		    color = cterm_normal_bg_color - 1;
6749 		else
6750 		{
6751 		    EMSG(_("E420: BG color unknown"));
6752 		    error = TRUE;
6753 		    break;
6754 		}
6755 	    }
6756 	    else
6757 	    {
6758 		static char *(color_names[28]) = {
6759 			    "Black", "DarkBlue", "DarkGreen", "DarkCyan",
6760 			    "DarkRed", "DarkMagenta", "Brown", "DarkYellow",
6761 			    "Gray", "Grey",
6762 			    "LightGray", "LightGrey", "DarkGray", "DarkGrey",
6763 			    "Blue", "LightBlue", "Green", "LightGreen",
6764 			    "Cyan", "LightCyan", "Red", "LightRed", "Magenta",
6765 			    "LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
6766 		static int color_numbers_16[28] = {0, 1, 2, 3,
6767 						 4, 5, 6, 6,
6768 						 7, 7,
6769 						 7, 7, 8, 8,
6770 						 9, 9, 10, 10,
6771 						 11, 11, 12, 12, 13,
6772 						 13, 14, 14, 15, -1};
6773 		/* for xterm with 88 colors... */
6774 		static int color_numbers_88[28] = {0, 4, 2, 6,
6775 						 1, 5, 32, 72,
6776 						 84, 84,
6777 						 7, 7, 82, 82,
6778 						 12, 43, 10, 61,
6779 						 14, 63, 9, 74, 13,
6780 						 75, 11, 78, 15, -1};
6781 		/* for xterm with 256 colors... */
6782 		static int color_numbers_256[28] = {0, 4, 2, 6,
6783 						 1, 5, 130, 130,
6784 						 248, 248,
6785 						 7, 7, 242, 242,
6786 						 12, 81, 10, 121,
6787 						 14, 159, 9, 224, 13,
6788 						 225, 11, 229, 15, -1};
6789 		/* for terminals with less than 16 colors... */
6790 		static int color_numbers_8[28] = {0, 4, 2, 6,
6791 						 1, 5, 3, 3,
6792 						 7, 7,
6793 						 7, 7, 0+8, 0+8,
6794 						 4+8, 4+8, 2+8, 2+8,
6795 						 6+8, 6+8, 1+8, 1+8, 5+8,
6796 						 5+8, 3+8, 3+8, 7+8, -1};
6797 #if defined(__QNXNTO__)
6798 		static int *color_numbers_8_qansi = color_numbers_8;
6799 		/* On qnx, the 8 & 16 color arrays are the same */
6800 		if (STRNCMP(T_NAME, "qansi", 5) == 0)
6801 		    color_numbers_8_qansi = color_numbers_16;
6802 #endif
6803 
6804 		/* reduce calls to STRICMP a bit, it can be slow */
6805 		off = TOUPPER_ASC(*arg);
6806 		for (i = (sizeof(color_names) / sizeof(char *)); --i >= 0; )
6807 		    if (off == color_names[i][0]
6808 				 && STRICMP(arg + 1, color_names[i] + 1) == 0)
6809 			break;
6810 		if (i < 0)
6811 		{
6812 		    EMSG2(_("E421: Color name or number not recognized: %s"), key_start);
6813 		    error = TRUE;
6814 		    break;
6815 		}
6816 
6817 		/* Use the _16 table to check if its a valid color name. */
6818 		color = color_numbers_16[i];
6819 		if (color >= 0)
6820 		{
6821 		    if (t_colors == 8)
6822 		    {
6823 			/* t_Co is 8: use the 8 colors table */
6824 #if defined(__QNXNTO__)
6825 			color = color_numbers_8_qansi[i];
6826 #else
6827 			color = color_numbers_8[i];
6828 #endif
6829 			if (key[5] == 'F')
6830 			{
6831 			    /* set/reset bold attribute to get light foreground
6832 			     * colors (on some terminals, e.g. "linux") */
6833 			    if (color & 8)
6834 			    {
6835 				HL_TABLE()[idx].sg_cterm |= HL_BOLD;
6836 				HL_TABLE()[idx].sg_cterm_bold = TRUE;
6837 			    }
6838 			    else
6839 				HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
6840 			}
6841 			color &= 7;	/* truncate to 8 colors */
6842 		    }
6843 		    else if (t_colors == 16 || t_colors == 88
6844 							   || t_colors == 256)
6845 		    {
6846 			/*
6847 			 * Guess: if the termcap entry ends in 'm', it is
6848 			 * probably an xterm-like terminal.  Use the changed
6849 			 * order for colors.
6850 			 */
6851 			if (*T_CAF != NUL)
6852 			    p = T_CAF;
6853 			else
6854 			    p = T_CSF;
6855 			if (*p != NUL && *(p + STRLEN(p) - 1) == 'm')
6856 			    switch (t_colors)
6857 			    {
6858 				case 16:
6859 				    color = color_numbers_8[i];
6860 				    break;
6861 				case 88:
6862 				    color = color_numbers_88[i];
6863 				    break;
6864 				case 256:
6865 				    color = color_numbers_256[i];
6866 				    break;
6867 			    }
6868 		    }
6869 		}
6870 	    }
6871 	    /* Add one to the argument, to avoid zero */
6872 	    if (key[5] == 'F')
6873 	    {
6874 		HL_TABLE()[idx].sg_cterm_fg = color + 1;
6875 		if (is_normal_group)
6876 		{
6877 		    cterm_normal_fg_color = color + 1;
6878 		    cterm_normal_fg_bold = (HL_TABLE()[idx].sg_cterm & HL_BOLD);
6879 #ifdef FEAT_GUI
6880 		    /* Don't do this if the GUI is used. */
6881 		    if (!gui.in_use && !gui.starting)
6882 #endif
6883 		    {
6884 			must_redraw = CLEAR;
6885 			if (termcap_active)
6886 			    term_fg_color(color);
6887 		    }
6888 		}
6889 	    }
6890 	    else
6891 	    {
6892 		HL_TABLE()[idx].sg_cterm_bg = color + 1;
6893 		if (is_normal_group)
6894 		{
6895 		    cterm_normal_bg_color = color + 1;
6896 #ifdef FEAT_GUI
6897 		    /* Don't mess with 'background' if the GUI is used. */
6898 		    if (!gui.in_use && !gui.starting)
6899 #endif
6900 		    {
6901 			must_redraw = CLEAR;
6902 			if (termcap_active)
6903 			    term_bg_color(color);
6904 			if (t_colors < 16)
6905 			    i = (color == 0 || color == 4);
6906 			else
6907 			    i = (color < 7 || color == 8);
6908 			/* Set the 'background' option if the value is wrong. */
6909 			if (i != (*p_bg == 'd'))
6910 			    set_option_value((char_u *)"bg", 0L,
6911 				 i ? (char_u *)"dark" : (char_u *)"light", 0);
6912 		    }
6913 		}
6914 	    }
6915 	  }
6916 	}
6917 	else if (STRCMP(key, "GUIFG") == 0)
6918 	{
6919 #ifdef FEAT_GUI	    /* in non-GUI guifg colors are simply ignored */
6920 	    if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
6921 	    {
6922 		if (!init)
6923 		    HL_TABLE()[idx].sg_set |= SG_GUI;
6924 
6925 		i = color_name2handle(arg);
6926 		if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
6927 		{
6928 		    HL_TABLE()[idx].sg_gui_fg = i;
6929 		    vim_free(HL_TABLE()[idx].sg_gui_fg_name);
6930 		    if (STRCMP(arg, "NONE"))
6931 			HL_TABLE()[idx].sg_gui_fg_name = vim_strsave(arg);
6932 		    else
6933 			HL_TABLE()[idx].sg_gui_fg_name = NULL;
6934 # ifdef FEAT_GUI_X11
6935 		    if (is_menu_group)
6936 			gui.menu_fg_pixel = i;
6937 		    if (is_scrollbar_group)
6938 			gui.scroll_fg_pixel = i;
6939 #  ifdef FEAT_BEVAL
6940 		    if (is_tooltip_group)
6941 			gui.tooltip_fg_pixel = i;
6942 #  endif
6943 		    do_colors = TRUE;
6944 # endif
6945 		}
6946 	    }
6947 #endif
6948 	}
6949 	else if (STRCMP(key, "GUIBG") == 0)
6950 	{
6951 #ifdef FEAT_GUI	    /* in non-GUI guibg colors are simply ignored */
6952 	    if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
6953 	    {
6954 		if (!init)
6955 		    HL_TABLE()[idx].sg_set |= SG_GUI;
6956 
6957 		i = color_name2handle(arg);
6958 		if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
6959 		{
6960 		    HL_TABLE()[idx].sg_gui_bg = i;
6961 		    vim_free(HL_TABLE()[idx].sg_gui_bg_name);
6962 		    if (STRCMP(arg, "NONE") != 0)
6963 			HL_TABLE()[idx].sg_gui_bg_name = vim_strsave(arg);
6964 		    else
6965 			HL_TABLE()[idx].sg_gui_bg_name = NULL;
6966 # ifdef FEAT_GUI_X11
6967 		    if (is_menu_group)
6968 			gui.menu_bg_pixel = i;
6969 		    if (is_scrollbar_group)
6970 			gui.scroll_bg_pixel = i;
6971 #  ifdef FEAT_BEVAL
6972 		    if (is_tooltip_group)
6973 			gui.tooltip_bg_pixel = i;
6974 #  endif
6975 		    do_colors = TRUE;
6976 # endif
6977 		}
6978 	    }
6979 #endif
6980 	}
6981 	else if (STRCMP(key, "GUISP") == 0)
6982 	{
6983 #ifdef FEAT_GUI	    /* in non-GUI guisp colors are simply ignored */
6984 	    if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
6985 	    {
6986 		if (!init)
6987 		    HL_TABLE()[idx].sg_set |= SG_GUI;
6988 
6989 		i = color_name2handle(arg);
6990 		if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
6991 		{
6992 		    HL_TABLE()[idx].sg_gui_sp = i;
6993 		    vim_free(HL_TABLE()[idx].sg_gui_sp_name);
6994 		    if (STRCMP(arg, "NONE") != 0)
6995 			HL_TABLE()[idx].sg_gui_sp_name = vim_strsave(arg);
6996 		    else
6997 			HL_TABLE()[idx].sg_gui_sp_name = NULL;
6998 		}
6999 	    }
7000 #endif
7001 	}
7002 	else if (STRCMP(key, "START") == 0 || STRCMP(key, "STOP") == 0)
7003 	{
7004 	    char_u	buf[100];
7005 	    char_u	*tname;
7006 
7007 	    if (!init)
7008 		HL_TABLE()[idx].sg_set |= SG_TERM;
7009 
7010 	    /*
7011 	     * The "start" and "stop"  arguments can be a literal escape
7012 	     * sequence, or a comma seperated list of terminal codes.
7013 	     */
7014 	    if (STRNCMP(arg, "t_", 2) == 0)
7015 	    {
7016 		off = 0;
7017 		buf[0] = 0;
7018 		while (arg[off] != NUL)
7019 		{
7020 		    /* Isolate one termcap name */
7021 		    for (len = 0; arg[off + len] &&
7022 						 arg[off + len] != ','; ++len)
7023 			;
7024 		    tname = vim_strnsave(arg + off, len);
7025 		    if (tname == NULL)		/* out of memory */
7026 		    {
7027 			error = TRUE;
7028 			break;
7029 		    }
7030 		    /* lookup the escape sequence for the item */
7031 		    p = get_term_code(tname);
7032 		    vim_free(tname);
7033 		    if (p == NULL)	    /* ignore non-existing things */
7034 			p = (char_u *)"";
7035 
7036 		    /* Append it to the already found stuff */
7037 		    if ((int)(STRLEN(buf) + STRLEN(p)) >= 99)
7038 		    {
7039 			EMSG2(_("E422: terminal code too long: %s"), arg);
7040 			error = TRUE;
7041 			break;
7042 		    }
7043 		    STRCAT(buf, p);
7044 
7045 		    /* Advance to the next item */
7046 		    off += len;
7047 		    if (arg[off] == ',')	    /* another one follows */
7048 			++off;
7049 		}
7050 	    }
7051 	    else
7052 	    {
7053 		/*
7054 		 * Copy characters from arg[] to buf[], translating <> codes.
7055 		 */
7056 		for (p = arg, off = 0; off < 100 && *p; )
7057 		{
7058 		    len = trans_special(&p, buf + off, FALSE);
7059 		    if (len)		    /* recognized special char */
7060 			off += len;
7061 		    else		    /* copy as normal char */
7062 			buf[off++] = *p++;
7063 		}
7064 		buf[off] = NUL;
7065 	    }
7066 	    if (error)
7067 		break;
7068 
7069 	    if (STRCMP(buf, "NONE") == 0)	/* resetting the value */
7070 		p = NULL;
7071 	    else
7072 		p = vim_strsave(buf);
7073 	    if (key[2] == 'A')
7074 	    {
7075 		vim_free(HL_TABLE()[idx].sg_start);
7076 		HL_TABLE()[idx].sg_start = p;
7077 	    }
7078 	    else
7079 	    {
7080 		vim_free(HL_TABLE()[idx].sg_stop);
7081 		HL_TABLE()[idx].sg_stop = p;
7082 	    }
7083 	}
7084 	else
7085 	{
7086 	    EMSG2(_("E423: Illegal argument: %s"), key_start);
7087 	    error = TRUE;
7088 	    break;
7089 	}
7090 
7091 	/*
7092 	 * When highlighting has been given for a group, don't link it.
7093 	 */
7094 	if (!init || !(HL_TABLE()[idx].sg_set & SG_LINK))
7095 	    HL_TABLE()[idx].sg_link = 0;
7096 
7097 	/*
7098 	 * Continue with next argument.
7099 	 */
7100 	linep = skipwhite(linep);
7101       }
7102 
7103     /*
7104      * If there is an error, and it's a new entry, remove it from the table.
7105      */
7106     if (error && idx == highlight_ga.ga_len)
7107 	syn_unadd_group();
7108     else
7109     {
7110 	if (is_normal_group)
7111 	{
7112 	    HL_TABLE()[idx].sg_term_attr = 0;
7113 	    HL_TABLE()[idx].sg_cterm_attr = 0;
7114 #ifdef FEAT_GUI
7115 	    HL_TABLE()[idx].sg_gui_attr = 0;
7116 	    /*
7117 	     * Need to update all groups, because they might be using "bg"
7118 	     * and/or "fg", which have been changed now.
7119 	     */
7120 	    if (gui.in_use)
7121 		highlight_gui_started();
7122 #endif
7123 	}
7124 #ifdef FEAT_GUI_X11
7125 # ifdef FEAT_MENU
7126 	else if (is_menu_group)
7127 	{
7128 	    if (gui.in_use && do_colors)
7129 		gui_mch_new_menu_colors();
7130 	}
7131 # endif
7132 	else if (is_scrollbar_group)
7133 	{
7134 	    if (gui.in_use && do_colors)
7135 		gui_new_scrollbar_colors();
7136 	}
7137 # ifdef FEAT_BEVAL
7138 	else if (is_tooltip_group)
7139 	{
7140 	    if (gui.in_use && do_colors)
7141 		gui_mch_new_tooltip_colors();
7142 	}
7143 # endif
7144 #endif
7145 	else
7146 	    set_hl_attr(idx);
7147 #ifdef FEAT_EVAL
7148 	HL_TABLE()[idx].sg_scriptID = current_SID;
7149 #endif
7150 	redraw_all_later(SOME_VALID);
7151     }
7152     vim_free(key);
7153     vim_free(arg);
7154 
7155     /* Only call highlight_changed() once, after sourcing a syntax file */
7156     need_highlight_changed = TRUE;
7157 }
7158 
7159 #if defined(EXITFREE) || defined(PROTO)
7160     void
7161 free_highlight()
7162 {
7163     int	    i;
7164 
7165     for (i = 0; i < highlight_ga.ga_len; ++i)
7166     {
7167 	highlight_clear(i);
7168 	vim_free(HL_TABLE()[i].sg_name);
7169 	vim_free(HL_TABLE()[i].sg_name_u);
7170     }
7171     ga_clear(&highlight_ga);
7172 }
7173 #endif
7174 
7175 /*
7176  * Reset the cterm colors to what they were before Vim was started, if
7177  * possible.  Otherwise reset them to zero.
7178  */
7179     void
7180 restore_cterm_colors()
7181 {
7182 #if defined(MSDOS) || (defined(WIN3264) && !defined(FEAT_GUI_W32))
7183     /* Since t_me has been set, this probably means that the user
7184      * wants to use this as default colors.  Need to reset default
7185      * background/foreground colors. */
7186     mch_set_normal_colors();
7187 #else
7188     cterm_normal_fg_color = 0;
7189     cterm_normal_fg_bold = 0;
7190     cterm_normal_bg_color = 0;
7191 #endif
7192 }
7193 
7194 /*
7195  * Return TRUE if highlight group "idx" has any settings.
7196  * When "check_link" is TRUE also check for an existing link.
7197  */
7198     static int
7199 hl_has_settings(idx, check_link)
7200     int		idx;
7201     int		check_link;
7202 {
7203     return (   HL_TABLE()[idx].sg_term_attr != 0
7204 	    || HL_TABLE()[idx].sg_cterm_attr != 0
7205 #ifdef FEAT_GUI
7206 	    || HL_TABLE()[idx].sg_gui_attr != 0
7207 #endif
7208 	    || (check_link && (HL_TABLE()[idx].sg_set & SG_LINK)));
7209 }
7210 
7211 /*
7212  * Clear highlighting for one group.
7213  */
7214     static void
7215 highlight_clear(idx)
7216     int idx;
7217 {
7218     HL_TABLE()[idx].sg_term = 0;
7219     vim_free(HL_TABLE()[idx].sg_start);
7220     HL_TABLE()[idx].sg_start = NULL;
7221     vim_free(HL_TABLE()[idx].sg_stop);
7222     HL_TABLE()[idx].sg_stop = NULL;
7223     HL_TABLE()[idx].sg_term_attr = 0;
7224     HL_TABLE()[idx].sg_cterm = 0;
7225     HL_TABLE()[idx].sg_cterm_bold = FALSE;
7226     HL_TABLE()[idx].sg_cterm_fg = 0;
7227     HL_TABLE()[idx].sg_cterm_bg = 0;
7228     HL_TABLE()[idx].sg_cterm_attr = 0;
7229 #ifdef FEAT_GUI	    /* in non-GUI fonts are simply ignored */
7230     HL_TABLE()[idx].sg_gui = 0;
7231     HL_TABLE()[idx].sg_gui_fg = INVALCOLOR;
7232     vim_free(HL_TABLE()[idx].sg_gui_fg_name);
7233     HL_TABLE()[idx].sg_gui_fg_name = NULL;
7234     HL_TABLE()[idx].sg_gui_bg = INVALCOLOR;
7235     vim_free(HL_TABLE()[idx].sg_gui_bg_name);
7236     HL_TABLE()[idx].sg_gui_bg_name = NULL;
7237     HL_TABLE()[idx].sg_gui_sp = INVALCOLOR;
7238     vim_free(HL_TABLE()[idx].sg_gui_sp_name);
7239     HL_TABLE()[idx].sg_gui_sp_name = NULL;
7240     gui_mch_free_font(HL_TABLE()[idx].sg_font);
7241     HL_TABLE()[idx].sg_font = NOFONT;
7242 # ifdef FEAT_XFONTSET
7243     gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
7244     HL_TABLE()[idx].sg_fontset = NOFONTSET;
7245 # endif
7246     vim_free(HL_TABLE()[idx].sg_font_name);
7247     HL_TABLE()[idx].sg_font_name = NULL;
7248     HL_TABLE()[idx].sg_gui_attr = 0;
7249 #endif
7250 #ifdef FEAT_EVAL
7251     /* Clear the script ID only when there is no link, since that is not
7252      * cleared. */
7253     if (HL_TABLE()[idx].sg_link == 0)
7254 	HL_TABLE()[idx].sg_scriptID = 0;
7255 #endif
7256 }
7257 
7258 #if defined(FEAT_GUI) || defined(PROTO)
7259 /*
7260  * Set the normal foreground and background colors according to the "Normal"
7261  * highlighighting group.  For X11 also set "Menu", "Scrollbar", and
7262  * "Tooltip" colors.
7263  */
7264     void
7265 set_normal_colors()
7266 {
7267     if (set_group_colors((char_u *)"Normal",
7268 			     &gui.norm_pixel, &gui.back_pixel,
7269 			     FALSE, TRUE, FALSE))
7270     {
7271 	gui_mch_new_colors();
7272 	must_redraw = CLEAR;
7273     }
7274 #ifdef FEAT_GUI_X11
7275     if (set_group_colors((char_u *)"Menu",
7276 			 &gui.menu_fg_pixel, &gui.menu_bg_pixel,
7277 			 TRUE, FALSE, FALSE))
7278     {
7279 # ifdef FEAT_MENU
7280 	gui_mch_new_menu_colors();
7281 # endif
7282 	must_redraw = CLEAR;
7283     }
7284 # ifdef FEAT_BEVAL
7285     if (set_group_colors((char_u *)"Tooltip",
7286 			 &gui.tooltip_fg_pixel, &gui.tooltip_bg_pixel,
7287 			 FALSE, FALSE, TRUE))
7288     {
7289 # ifdef FEAT_TOOLBAR
7290 	gui_mch_new_tooltip_colors();
7291 # endif
7292 	must_redraw = CLEAR;
7293     }
7294 #endif
7295     if (set_group_colors((char_u *)"Scrollbar",
7296 		    &gui.scroll_fg_pixel, &gui.scroll_bg_pixel,
7297 		    FALSE, FALSE, FALSE))
7298     {
7299 	gui_new_scrollbar_colors();
7300 	must_redraw = CLEAR;
7301     }
7302 #endif
7303 }
7304 
7305 /*
7306  * Set the colors for "Normal", "Menu", "Tooltip" or "Scrollbar".
7307  */
7308     static int
7309 set_group_colors(name, fgp, bgp, do_menu, use_norm, do_tooltip)
7310     char_u	*name;
7311     guicolor_T	*fgp;
7312     guicolor_T	*bgp;
7313     int		do_menu;
7314     int		use_norm;
7315     int		do_tooltip;
7316 {
7317     int		idx;
7318 
7319     idx = syn_name2id(name) - 1;
7320     if (idx >= 0)
7321     {
7322 	gui_do_one_color(idx, do_menu, do_tooltip);
7323 
7324 	if (HL_TABLE()[idx].sg_gui_fg != INVALCOLOR)
7325 	    *fgp = HL_TABLE()[idx].sg_gui_fg;
7326 	else if (use_norm)
7327 	    *fgp = gui.def_norm_pixel;
7328 	if (HL_TABLE()[idx].sg_gui_bg != INVALCOLOR)
7329 	    *bgp = HL_TABLE()[idx].sg_gui_bg;
7330 	else if (use_norm)
7331 	    *bgp = gui.def_back_pixel;
7332 	return TRUE;
7333     }
7334     return FALSE;
7335 }
7336 
7337 /*
7338  * Get the font of the "Normal" group.
7339  * Returns "" when it's not found or not set.
7340  */
7341     char_u *
7342 hl_get_font_name()
7343 {
7344     int		id;
7345     char_u	*s;
7346 
7347     id = syn_name2id((char_u *)"Normal");
7348     if (id > 0)
7349     {
7350 	s = HL_TABLE()[id - 1].sg_font_name;
7351 	if (s != NULL)
7352 	    return s;
7353     }
7354     return (char_u *)"";
7355 }
7356 
7357 /*
7358  * Set font for "Normal" group.  Called by gui_mch_init_font() when a font has
7359  * actually chosen to be used.
7360  */
7361     void
7362 hl_set_font_name(font_name)
7363     char_u	*font_name;
7364 {
7365     int	    id;
7366 
7367     id = syn_name2id((char_u *)"Normal");
7368     if (id > 0)
7369     {
7370 	vim_free(HL_TABLE()[id - 1].sg_font_name);
7371 	HL_TABLE()[id - 1].sg_font_name = vim_strsave(font_name);
7372     }
7373 }
7374 
7375 /*
7376  * Set background color for "Normal" group.  Called by gui_set_bg_color()
7377  * when the color is known.
7378  */
7379     void
7380 hl_set_bg_color_name(name)
7381     char_u  *name;	    /* must have been allocated */
7382 {
7383     int	    id;
7384 
7385     if (name != NULL)
7386     {
7387 	id = syn_name2id((char_u *)"Normal");
7388 	if (id > 0)
7389 	{
7390 	    vim_free(HL_TABLE()[id - 1].sg_gui_bg_name);
7391 	    HL_TABLE()[id - 1].sg_gui_bg_name = name;
7392 	}
7393     }
7394 }
7395 
7396 /*
7397  * Set foreground color for "Normal" group.  Called by gui_set_fg_color()
7398  * when the color is known.
7399  */
7400     void
7401 hl_set_fg_color_name(name)
7402     char_u  *name;	    /* must have been allocated */
7403 {
7404     int	    id;
7405 
7406     if (name != NULL)
7407     {
7408 	id = syn_name2id((char_u *)"Normal");
7409 	if (id > 0)
7410 	{
7411 	    vim_free(HL_TABLE()[id - 1].sg_gui_fg_name);
7412 	    HL_TABLE()[id - 1].sg_gui_fg_name = name;
7413 	}
7414     }
7415 }
7416 
7417 /*
7418  * Return the handle for a color name.
7419  * Returns INVALCOLOR when failed.
7420  */
7421     static guicolor_T
7422 color_name2handle(name)
7423     char_u  *name;
7424 {
7425     if (STRCMP(name, "NONE") == 0)
7426 	return INVALCOLOR;
7427 
7428     if (STRICMP(name, "fg") == 0 || STRICMP(name, "foreground") == 0)
7429 	return gui.norm_pixel;
7430     if (STRICMP(name, "bg") == 0 || STRICMP(name, "background") == 0)
7431 	return gui.back_pixel;
7432 
7433     return gui_get_color(name);
7434 }
7435 
7436 /*
7437  * Return the handle for a font name.
7438  * Returns NOFONT when failed.
7439  */
7440     static GuiFont
7441 font_name2handle(name)
7442     char_u  *name;
7443 {
7444     if (STRCMP(name, "NONE") == 0)
7445 	return NOFONT;
7446 
7447     return gui_mch_get_font(name, TRUE);
7448 }
7449 
7450 # ifdef FEAT_XFONTSET
7451 /*
7452  * Return the handle for a fontset name.
7453  * Returns NOFONTSET when failed.
7454  */
7455     static GuiFontset
7456 fontset_name2handle(name, fixed_width)
7457     char_u	*name;
7458     int		fixed_width;
7459 {
7460     if (STRCMP(name, "NONE") == 0)
7461 	return NOFONTSET;
7462 
7463     return gui_mch_get_fontset(name, TRUE, fixed_width);
7464 }
7465 # endif
7466 
7467 /*
7468  * Get the font or fontset for one highlight group.
7469  */
7470 /*ARGSUSED*/
7471     static void
7472 hl_do_font(idx, arg, do_normal, do_menu, do_tooltip)
7473     int		idx;
7474     char_u	*arg;
7475     int		do_normal;	/* set normal font */
7476     int		do_menu;	/* set menu font */
7477     int		do_tooltip;	/* set tooltip font */
7478 {
7479 # ifdef FEAT_XFONTSET
7480     /* If 'guifontset' is not empty, first try using the name as a
7481      * fontset.  If that doesn't work, use it as a font name. */
7482     if (*p_guifontset != NUL
7483 #  ifdef FONTSET_ALWAYS
7484 	|| do_menu
7485 #  endif
7486 #  ifdef FEAT_BEVAL_TIP
7487 	/* In Athena & Motif, the Tooltip highlight group is always a fontset */
7488 	|| do_tooltip
7489 #  endif
7490 	    )
7491 	HL_TABLE()[idx].sg_fontset = fontset_name2handle(arg, 0
7492 #  ifdef FONTSET_ALWAYS
7493 		|| do_menu
7494 #  endif
7495 #  ifdef FEAT_BEVAL_TIP
7496 		|| do_tooltip
7497 #  endif
7498 		);
7499     if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
7500     {
7501 	/* If it worked and it's the Normal group, use it as the
7502 	 * normal fontset.  Same for the Menu group. */
7503 	if (do_normal)
7504 	    gui_init_font(arg, TRUE);
7505 #   if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
7506 	if (do_menu)
7507 	{
7508 #    ifdef FONTSET_ALWAYS
7509 	    gui.menu_fontset = HL_TABLE()[idx].sg_fontset;
7510 #    else
7511 	    /* YIKES!  This is a bug waiting to crash the program */
7512 	    gui.menu_font = HL_TABLE()[idx].sg_fontset;
7513 #    endif
7514 	    gui_mch_new_menu_font();
7515 	}
7516 #    ifdef FEAT_BEVAL
7517 	if (do_tooltip)
7518 	{
7519 	    /* The Athena widget set cannot currently handle switching between
7520 	     * displaying a single font and a fontset.
7521 	     * If the XtNinternational resource is set to True at widget
7522 	     * creation, then a fontset is always used, othwise an
7523 	     * XFontStruct is used.
7524 	     */
7525 	    gui.tooltip_fontset = (XFontSet)HL_TABLE()[idx].sg_fontset;
7526 	    gui_mch_new_tooltip_font();
7527 	}
7528 #    endif
7529 #   endif
7530     }
7531     else
7532 # endif
7533     {
7534 	HL_TABLE()[idx].sg_font = font_name2handle(arg);
7535 	/* If it worked and it's the Normal group, use it as the
7536 	 * normal font.  Same for the Menu group. */
7537 	if (HL_TABLE()[idx].sg_font != NOFONT)
7538 	{
7539 	    if (do_normal)
7540 		gui_init_font(arg, FALSE);
7541 #ifndef FONTSET_ALWAYS
7542 # if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
7543 	    if (do_menu)
7544 	    {
7545 		gui.menu_font = HL_TABLE()[idx].sg_font;
7546 		gui_mch_new_menu_font();
7547 	    }
7548 # endif
7549 #endif
7550 	}
7551     }
7552 }
7553 
7554 #endif /* FEAT_GUI */
7555 
7556 /*
7557  * Table with the specifications for an attribute number.
7558  * Note that this table is used by ALL buffers.  This is required because the
7559  * GUI can redraw at any time for any buffer.
7560  */
7561 static garray_T	term_attr_table = {0, 0, 0, 0, NULL};
7562 
7563 #define TERM_ATTR_ENTRY(idx) ((attrentry_T *)term_attr_table.ga_data)[idx]
7564 
7565 static garray_T	cterm_attr_table = {0, 0, 0, 0, NULL};
7566 
7567 #define CTERM_ATTR_ENTRY(idx) ((attrentry_T *)cterm_attr_table.ga_data)[idx]
7568 
7569 #ifdef FEAT_GUI
7570 static garray_T	gui_attr_table = {0, 0, 0, 0, NULL};
7571 
7572 #define GUI_ATTR_ENTRY(idx) ((attrentry_T *)gui_attr_table.ga_data)[idx]
7573 #endif
7574 
7575 /*
7576  * Return the attr number for a set of colors and font.
7577  * Add a new entry to the term_attr_table, cterm_attr_table or gui_attr_table
7578  * if the combination is new.
7579  * Return 0 for error (no more room).
7580  */
7581     static int
7582 get_attr_entry(table, aep)
7583     garray_T	*table;
7584     attrentry_T	*aep;
7585 {
7586     int		i;
7587     attrentry_T	*taep;
7588     static int	recursive = FALSE;
7589 
7590     /*
7591      * Init the table, in case it wasn't done yet.
7592      */
7593     table->ga_itemsize = sizeof(attrentry_T);
7594     table->ga_growsize = 7;
7595 
7596     /*
7597      * Try to find an entry with the same specifications.
7598      */
7599     for (i = 0; i < table->ga_len; ++i)
7600     {
7601 	taep = &(((attrentry_T *)table->ga_data)[i]);
7602 	if (	   aep->ae_attr == taep->ae_attr
7603 		&& (
7604 #ifdef FEAT_GUI
7605 		       (table == &gui_attr_table
7606 			&& (aep->ae_u.gui.fg_color == taep->ae_u.gui.fg_color
7607 			    && aep->ae_u.gui.bg_color
7608 						    == taep->ae_u.gui.bg_color
7609 			    && aep->ae_u.gui.sp_color
7610 						    == taep->ae_u.gui.sp_color
7611 			    && aep->ae_u.gui.font == taep->ae_u.gui.font
7612 #  ifdef FEAT_XFONTSET
7613 			    && aep->ae_u.gui.fontset == taep->ae_u.gui.fontset
7614 #  endif
7615 			    ))
7616 		    ||
7617 #endif
7618 		       (table == &term_attr_table
7619 			&& (aep->ae_u.term.start == NULL)
7620 					    == (taep->ae_u.term.start == NULL)
7621 			&& (aep->ae_u.term.start == NULL
7622 			    || STRCMP(aep->ae_u.term.start,
7623 						  taep->ae_u.term.start) == 0)
7624 			&& (aep->ae_u.term.stop == NULL)
7625 					     == (taep->ae_u.term.stop == NULL)
7626 			&& (aep->ae_u.term.stop == NULL
7627 			    || STRCMP(aep->ae_u.term.stop,
7628 						  taep->ae_u.term.stop) == 0))
7629 		    || (table == &cterm_attr_table
7630 			    && aep->ae_u.cterm.fg_color
7631 						  == taep->ae_u.cterm.fg_color
7632 			    && aep->ae_u.cterm.bg_color
7633 						 == taep->ae_u.cterm.bg_color)
7634 		     ))
7635 
7636 	return i + ATTR_OFF;
7637     }
7638 
7639     if (table->ga_len + ATTR_OFF > MAX_TYPENR)
7640     {
7641 	/*
7642 	 * Running out of attribute entries!  remove all attributes, and
7643 	 * compute new ones for all groups.
7644 	 * When called recursively, we are really out of numbers.
7645 	 */
7646 	if (recursive)
7647 	{
7648 	    EMSG(_("E424: Too many different highlighting attributes in use"));
7649 	    return 0;
7650 	}
7651 	recursive = TRUE;
7652 
7653 	clear_hl_tables();
7654 
7655 	must_redraw = CLEAR;
7656 
7657 	for (i = 0; i < highlight_ga.ga_len; ++i)
7658 	    set_hl_attr(i);
7659 
7660 	recursive = FALSE;
7661     }
7662 
7663     /*
7664      * This is a new combination of colors and font, add an entry.
7665      */
7666     if (ga_grow(table, 1) == FAIL)
7667 	return 0;
7668 
7669     taep = &(((attrentry_T *)table->ga_data)[table->ga_len]);
7670     vim_memset(taep, 0, sizeof(attrentry_T));
7671     taep->ae_attr = aep->ae_attr;
7672 #ifdef FEAT_GUI
7673     if (table == &gui_attr_table)
7674     {
7675 	taep->ae_u.gui.fg_color = aep->ae_u.gui.fg_color;
7676 	taep->ae_u.gui.bg_color = aep->ae_u.gui.bg_color;
7677 	taep->ae_u.gui.sp_color = aep->ae_u.gui.sp_color;
7678 	taep->ae_u.gui.font = aep->ae_u.gui.font;
7679 # ifdef FEAT_XFONTSET
7680 	taep->ae_u.gui.fontset = aep->ae_u.gui.fontset;
7681 # endif
7682     }
7683 #endif
7684     if (table == &term_attr_table)
7685     {
7686 	if (aep->ae_u.term.start == NULL)
7687 	    taep->ae_u.term.start = NULL;
7688 	else
7689 	    taep->ae_u.term.start = vim_strsave(aep->ae_u.term.start);
7690 	if (aep->ae_u.term.stop == NULL)
7691 	    taep->ae_u.term.stop = NULL;
7692 	else
7693 	    taep->ae_u.term.stop = vim_strsave(aep->ae_u.term.stop);
7694     }
7695     else if (table == &cterm_attr_table)
7696     {
7697 	taep->ae_u.cterm.fg_color = aep->ae_u.cterm.fg_color;
7698 	taep->ae_u.cterm.bg_color = aep->ae_u.cterm.bg_color;
7699     }
7700     ++table->ga_len;
7701     return (table->ga_len - 1 + ATTR_OFF);
7702 }
7703 
7704 /*
7705  * Clear all highlight tables.
7706  */
7707     void
7708 clear_hl_tables()
7709 {
7710     int		i;
7711     attrentry_T	*taep;
7712 
7713 #ifdef FEAT_GUI
7714     ga_clear(&gui_attr_table);
7715 #endif
7716     for (i = 0; i < term_attr_table.ga_len; ++i)
7717     {
7718 	taep = &(((attrentry_T *)term_attr_table.ga_data)[i]);
7719 	vim_free(taep->ae_u.term.start);
7720 	vim_free(taep->ae_u.term.stop);
7721     }
7722     ga_clear(&term_attr_table);
7723     ga_clear(&cterm_attr_table);
7724 }
7725 
7726 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
7727 /*
7728  * Combine special attributes (e.g., for spelling) with other attributes
7729  * (e.g., for syntax highlighting).
7730  * "prim_attr" overrules "char_attr".
7731  * This creates a new group when required.
7732  * Since we expect there to be few spelling mistakes we don't cache the
7733  * result.
7734  * Return the resulting attributes.
7735  */
7736     int
7737 hl_combine_attr(char_attr, prim_attr)
7738     int	    char_attr;
7739     int	    prim_attr;
7740 {
7741     attrentry_T *char_aep = NULL;
7742     attrentry_T *spell_aep;
7743     attrentry_T new_en;
7744 
7745     if (char_attr == 0)
7746 	return prim_attr;
7747     if (char_attr <= HL_ALL && prim_attr <= HL_ALL)
7748 	return char_attr | prim_attr;
7749 #ifdef FEAT_GUI
7750     if (gui.in_use)
7751     {
7752 	if (char_attr > HL_ALL)
7753 	    char_aep = syn_gui_attr2entry(char_attr);
7754 	if (char_aep != NULL)
7755 	    new_en = *char_aep;
7756 	else
7757 	{
7758 	    vim_memset(&new_en, 0, sizeof(new_en));
7759 	    new_en.ae_u.gui.fg_color = INVALCOLOR;
7760 	    new_en.ae_u.gui.bg_color = INVALCOLOR;
7761 	    new_en.ae_u.gui.sp_color = INVALCOLOR;
7762 	    if (char_attr <= HL_ALL)
7763 		new_en.ae_attr = char_attr;
7764 	}
7765 
7766 	if (prim_attr <= HL_ALL)
7767 	    new_en.ae_attr |= prim_attr;
7768 	else
7769 	{
7770 	    spell_aep = syn_gui_attr2entry(prim_attr);
7771 	    if (spell_aep != NULL)
7772 	    {
7773 		new_en.ae_attr |= spell_aep->ae_attr;
7774 		if (spell_aep->ae_u.gui.fg_color != INVALCOLOR)
7775 		    new_en.ae_u.gui.fg_color = spell_aep->ae_u.gui.fg_color;
7776 		if (spell_aep->ae_u.gui.bg_color != INVALCOLOR)
7777 		    new_en.ae_u.gui.bg_color = spell_aep->ae_u.gui.bg_color;
7778 		if (spell_aep->ae_u.gui.sp_color != INVALCOLOR)
7779 		    new_en.ae_u.gui.sp_color = spell_aep->ae_u.gui.sp_color;
7780 		if (spell_aep->ae_u.gui.font != NOFONT)
7781 		    new_en.ae_u.gui.font = spell_aep->ae_u.gui.font;
7782 # ifdef FEAT_XFONTSET
7783 		if (spell_aep->ae_u.gui.fontset != NOFONTSET)
7784 		    new_en.ae_u.gui.fontset = spell_aep->ae_u.gui.fontset;
7785 # endif
7786 	    }
7787 	}
7788 	return get_attr_entry(&gui_attr_table, &new_en);
7789     }
7790 #endif
7791 
7792     if (t_colors > 1)
7793     {
7794 	if (char_attr > HL_ALL)
7795 	    char_aep = syn_cterm_attr2entry(char_attr);
7796 	if (char_aep != NULL)
7797 	    new_en = *char_aep;
7798 	else
7799 	{
7800 	    vim_memset(&new_en, 0, sizeof(new_en));
7801 	    if (char_attr <= HL_ALL)
7802 		new_en.ae_attr = char_attr;
7803 	}
7804 
7805 	if (prim_attr <= HL_ALL)
7806 	    new_en.ae_attr |= prim_attr;
7807 	else
7808 	{
7809 	    spell_aep = syn_cterm_attr2entry(prim_attr);
7810 	    if (spell_aep != NULL)
7811 	    {
7812 		new_en.ae_attr |= spell_aep->ae_attr;
7813 		if (spell_aep->ae_u.cterm.fg_color > 0)
7814 		    new_en.ae_u.cterm.fg_color = spell_aep->ae_u.cterm.fg_color;
7815 		if (spell_aep->ae_u.cterm.bg_color > 0)
7816 		    new_en.ae_u.cterm.bg_color = spell_aep->ae_u.cterm.bg_color;
7817 	    }
7818 	}
7819 	return get_attr_entry(&cterm_attr_table, &new_en);
7820     }
7821 
7822     if (char_attr > HL_ALL)
7823 	char_aep = syn_term_attr2entry(char_attr);
7824     if (char_aep != NULL)
7825 	new_en = *char_aep;
7826     else
7827     {
7828 	vim_memset(&new_en, 0, sizeof(new_en));
7829 	if (char_attr <= HL_ALL)
7830 	    new_en.ae_attr = char_attr;
7831     }
7832 
7833     if (prim_attr <= HL_ALL)
7834 	new_en.ae_attr |= prim_attr;
7835     else
7836     {
7837 	spell_aep = syn_term_attr2entry(prim_attr);
7838 	if (spell_aep != NULL)
7839 	{
7840 	    new_en.ae_attr |= spell_aep->ae_attr;
7841 	    if (spell_aep->ae_u.term.start != NULL)
7842 	    {
7843 		new_en.ae_u.term.start = spell_aep->ae_u.term.start;
7844 		new_en.ae_u.term.stop = spell_aep->ae_u.term.stop;
7845 	    }
7846 	}
7847     }
7848     return get_attr_entry(&term_attr_table, &new_en);
7849 }
7850 #endif
7851 
7852 #ifdef FEAT_GUI
7853 
7854     attrentry_T *
7855 syn_gui_attr2entry(attr)
7856     int		    attr;
7857 {
7858     attr -= ATTR_OFF;
7859     if (attr >= gui_attr_table.ga_len)	    /* did ":syntax clear" */
7860 	return NULL;
7861     return &(GUI_ATTR_ENTRY(attr));
7862 }
7863 #endif /* FEAT_GUI */
7864 
7865 /*
7866  * Get the highlight attributes (HL_BOLD etc.) from an attribute nr.
7867  * Only to be used when "attr" > HL_ALL.
7868  */
7869     int
7870 syn_attr2attr(attr)
7871     int	    attr;
7872 {
7873     attrentry_T	*aep;
7874 
7875 #ifdef FEAT_GUI
7876     if (gui.in_use)
7877 	aep = syn_gui_attr2entry(attr);
7878     else
7879 #endif
7880 	if (t_colors > 1)
7881 	aep = syn_cterm_attr2entry(attr);
7882     else
7883 	aep = syn_term_attr2entry(attr);
7884 
7885     if (aep == NULL)	    /* highlighting not set */
7886 	return 0;
7887     return aep->ae_attr;
7888 }
7889 
7890 
7891     attrentry_T *
7892 syn_term_attr2entry(attr)
7893     int		    attr;
7894 {
7895     attr -= ATTR_OFF;
7896     if (attr >= term_attr_table.ga_len)	    /* did ":syntax clear" */
7897 	return NULL;
7898     return &(TERM_ATTR_ENTRY(attr));
7899 }
7900 
7901     attrentry_T *
7902 syn_cterm_attr2entry(attr)
7903     int		    attr;
7904 {
7905     attr -= ATTR_OFF;
7906     if (attr >= cterm_attr_table.ga_len)	/* did ":syntax clear" */
7907 	return NULL;
7908     return &(CTERM_ATTR_ENTRY(attr));
7909 }
7910 
7911 #define LIST_ATTR   1
7912 #define LIST_STRING 2
7913 #define LIST_INT    3
7914 
7915     static void
7916 highlight_list_one(id)
7917     int		id;
7918 {
7919     struct hl_group	*sgp;
7920     int			didh = FALSE;
7921 
7922     sgp = &HL_TABLE()[id - 1];	    /* index is ID minus one */
7923 
7924     didh = highlight_list_arg(id, didh, LIST_ATTR,
7925 				    sgp->sg_term, NULL, "term");
7926     didh = highlight_list_arg(id, didh, LIST_STRING,
7927 				    0, sgp->sg_start, "start");
7928     didh = highlight_list_arg(id, didh, LIST_STRING,
7929 				    0, sgp->sg_stop, "stop");
7930 
7931     didh = highlight_list_arg(id, didh, LIST_ATTR,
7932 				    sgp->sg_cterm, NULL, "cterm");
7933     didh = highlight_list_arg(id, didh, LIST_INT,
7934 				    sgp->sg_cterm_fg, NULL, "ctermfg");
7935     didh = highlight_list_arg(id, didh, LIST_INT,
7936 				    sgp->sg_cterm_bg, NULL, "ctermbg");
7937 
7938 #ifdef FEAT_GUI
7939     didh = highlight_list_arg(id, didh, LIST_ATTR,
7940 				    sgp->sg_gui, NULL, "gui");
7941     didh = highlight_list_arg(id, didh, LIST_STRING,
7942 				    0, sgp->sg_gui_fg_name, "guifg");
7943     didh = highlight_list_arg(id, didh, LIST_STRING,
7944 				    0, sgp->sg_gui_bg_name, "guibg");
7945     didh = highlight_list_arg(id, didh, LIST_STRING,
7946 				    0, sgp->sg_gui_sp_name, "guisp");
7947     didh = highlight_list_arg(id, didh, LIST_STRING,
7948 				    0, sgp->sg_font_name, "font");
7949 #endif
7950 
7951     if (sgp->sg_link && !got_int)
7952     {
7953 	(void)syn_list_header(didh, 9999, id);
7954 	didh = TRUE;
7955 	msg_puts_attr((char_u *)"links to", hl_attr(HLF_D));
7956 	msg_putchar(' ');
7957 	msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
7958     }
7959 
7960     if (!didh)
7961 	highlight_list_arg(id, didh, LIST_STRING, 0, (char_u *)"cleared", "");
7962 #ifdef FEAT_EVAL
7963     if (p_verbose > 0)
7964 	last_set_msg(sgp->sg_scriptID);
7965 #endif
7966 }
7967 
7968     static int
7969 highlight_list_arg(id, didh, type, iarg, sarg, name)
7970     int		id;
7971     int		didh;
7972     int		type;
7973     int		iarg;
7974     char_u	*sarg;
7975     char	*name;
7976 {
7977     char_u	buf[100];
7978     char_u	*ts;
7979     int		i;
7980 
7981     if (got_int)
7982 	return FALSE;
7983     if (type == LIST_STRING ? (sarg != NULL) : (iarg != 0))
7984     {
7985 	ts = buf;
7986 	if (type == LIST_INT)
7987 	    sprintf((char *)buf, "%d", iarg - 1);
7988 	else if (type == LIST_STRING)
7989 	    ts = sarg;
7990 	else /* type == LIST_ATTR */
7991 	{
7992 	    buf[0] = NUL;
7993 	    for (i = 0; hl_attr_table[i] != 0; ++i)
7994 	    {
7995 		if (iarg & hl_attr_table[i])
7996 		{
7997 		    if (buf[0] != NUL)
7998 			STRCAT(buf, ",");
7999 		    STRCAT(buf, hl_name_table[i]);
8000 		    iarg &= ~hl_attr_table[i];	    /* don't want "inverse" */
8001 		}
8002 	    }
8003 	}
8004 
8005 	(void)syn_list_header(didh,
8006 			       (int)(vim_strsize(ts) + STRLEN(name) + 1), id);
8007 	didh = TRUE;
8008 	if (!got_int)
8009 	{
8010 	    if (*name != NUL)
8011 	    {
8012 		MSG_PUTS_ATTR(name, hl_attr(HLF_D));
8013 		MSG_PUTS_ATTR("=", hl_attr(HLF_D));
8014 	    }
8015 	    msg_outtrans(ts);
8016 	}
8017     }
8018     return didh;
8019 }
8020 
8021 #if (((defined(FEAT_EVAL) || defined(FEAT_PRINTER))) && defined(FEAT_SYN_HL)) || defined(PROTO)
8022 /*
8023  * Return "1" if highlight group "id" has attribute "flag".
8024  * Return NULL otherwise.
8025  */
8026     char_u *
8027 highlight_has_attr(id, flag, modec)
8028     int		id;
8029     int		flag;
8030     int		modec;	/* 'g' for GUI, 'c' for cterm, 't' for term */
8031 {
8032     int		attr;
8033 
8034     if (id <= 0 || id > highlight_ga.ga_len)
8035 	return NULL;
8036 
8037 #ifdef FEAT_GUI
8038     if (modec == 'g')
8039 	attr = HL_TABLE()[id - 1].sg_gui;
8040     else
8041 #endif
8042 	 if (modec == 'c')
8043 	attr = HL_TABLE()[id - 1].sg_cterm;
8044     else
8045 	attr = HL_TABLE()[id - 1].sg_term;
8046 
8047     if (attr & flag)
8048 	return (char_u *)"1";
8049     return NULL;
8050 }
8051 #endif
8052 
8053 #if (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) || defined(PROTO)
8054 /*
8055  * Return color name of highlight group "id".
8056  */
8057     char_u *
8058 highlight_color(id, what, modec)
8059     int		id;
8060     char_u	*what;	/* "fg", "bg", "sp", "fg#", "bg#" or "sp#" */
8061     int		modec;	/* 'g' for GUI, 'c' for cterm, 't' for term */
8062 {
8063     static char_u	name[20];
8064     int			n;
8065     int			fg = FALSE;
8066 # ifdef FEAT_GUI
8067     int			sp = FALSE;
8068 # endif
8069 
8070     if (id <= 0 || id > highlight_ga.ga_len)
8071 	return NULL;
8072 
8073     if (TOLOWER_ASC(what[0]) == 'f')
8074 	fg = TRUE;
8075 # ifdef FEAT_GUI
8076     else if (TOLOWER_ASC(what[0]) == 's')
8077 	sp = TRUE;
8078     if (modec == 'g')
8079     {
8080 	/* return #RRGGBB form (only possible when GUI is running) */
8081 	if (gui.in_use && what[1] && what[2] == '#')
8082 	{
8083 	    guicolor_T		color;
8084 	    long_u		rgb;
8085 	    static char_u	buf[10];
8086 
8087 	    if (fg)
8088 		color = HL_TABLE()[id - 1].sg_gui_fg;
8089 	    else if (sp)
8090 		color = HL_TABLE()[id - 1].sg_gui_sp;
8091 	    else
8092 		color = HL_TABLE()[id - 1].sg_gui_bg;
8093 	    if (color == INVALCOLOR)
8094 		return NULL;
8095 	    rgb = gui_mch_get_rgb(color);
8096 	    sprintf((char *)buf, "#%02x%02x%02x",
8097 				      (unsigned)(rgb >> 16),
8098 				      (unsigned)(rgb >> 8) & 255,
8099 				      (unsigned)rgb & 255);
8100 	    return buf;
8101 	}
8102 	if (fg)
8103 	    return (HL_TABLE()[id - 1].sg_gui_fg_name);
8104 	if (sp)
8105 	    return (HL_TABLE()[id - 1].sg_gui_sp_name);
8106 	return (HL_TABLE()[id - 1].sg_gui_bg_name);
8107     }
8108 # endif
8109     if (modec == 'c')
8110     {
8111 	if (fg)
8112 	    n = HL_TABLE()[id - 1].sg_cterm_fg - 1;
8113 	else
8114 	    n = HL_TABLE()[id - 1].sg_cterm_bg - 1;
8115 	sprintf((char *)name, "%d", n);
8116 	return name;
8117     }
8118     /* term doesn't have color */
8119     return NULL;
8120 }
8121 #endif
8122 
8123 #if (defined(FEAT_SYN_HL) && defined(FEAT_GUI) && defined(FEAT_PRINTER)) \
8124 	|| defined(PROTO)
8125 /*
8126  * Return color name of highlight group "id" as RGB value.
8127  */
8128     long_u
8129 highlight_gui_color_rgb(id, fg)
8130     int		id;
8131     int		fg;	/* TRUE = fg, FALSE = bg */
8132 {
8133     guicolor_T	color;
8134 
8135     if (id <= 0 || id > highlight_ga.ga_len)
8136 	return 0L;
8137 
8138     if (fg)
8139 	color = HL_TABLE()[id - 1].sg_gui_fg;
8140     else
8141 	color = HL_TABLE()[id - 1].sg_gui_bg;
8142 
8143     if (color == INVALCOLOR)
8144 	return 0L;
8145 
8146     return gui_mch_get_rgb(color);
8147 }
8148 #endif
8149 
8150 /*
8151  * Output the syntax list header.
8152  * Return TRUE when started a new line.
8153  */
8154     static int
8155 syn_list_header(did_header, outlen, id)
8156     int	    did_header;		/* did header already */
8157     int	    outlen;		/* length of string that comes */
8158     int	    id;			/* highlight group id */
8159 {
8160     int	    endcol = 19;
8161     int	    newline = TRUE;
8162 
8163     if (!did_header)
8164     {
8165 	msg_putchar('\n');
8166 	if (got_int)
8167 	    return TRUE;
8168 	msg_outtrans(HL_TABLE()[id - 1].sg_name);
8169 	endcol = 15;
8170     }
8171     else if (msg_col + outlen + 1 >= Columns)
8172     {
8173 	msg_putchar('\n');
8174 	if (got_int)
8175 	    return TRUE;
8176     }
8177     else
8178     {
8179 	if (msg_col >= endcol)	/* wrap around is like starting a new line */
8180 	    newline = FALSE;
8181     }
8182 
8183     if (msg_col >= endcol)	/* output at least one space */
8184 	endcol = msg_col + 1;
8185     if (Columns <= endcol)	/* avoid hang for tiny window */
8186 	endcol = Columns - 1;
8187 
8188     msg_advance(endcol);
8189 
8190     /* Show "xxx" with the attributes. */
8191     if (!did_header)
8192     {
8193 	msg_puts_attr((char_u *)"xxx", syn_id2attr(id));
8194 	msg_putchar(' ');
8195     }
8196 
8197     return newline;
8198 }
8199 
8200 /*
8201  * Set the attribute numbers for a highlight group.
8202  * Called after one of the attributes has changed.
8203  */
8204     static void
8205 set_hl_attr(idx)
8206     int		idx;	    /* index in array */
8207 {
8208     attrentry_T		at_en;
8209     struct hl_group	*sgp = HL_TABLE() + idx;
8210 
8211     /* The "Normal" group doesn't need an attribute number */
8212     if (sgp->sg_name_u != NULL && STRCMP(sgp->sg_name_u, "NORMAL") == 0)
8213 	return;
8214 
8215 #ifdef FEAT_GUI
8216     /*
8217      * For the GUI mode: If there are other than "normal" highlighting
8218      * attributes, need to allocate an attr number.
8219      */
8220     if (sgp->sg_gui_fg == INVALCOLOR
8221 	    && sgp->sg_gui_bg == INVALCOLOR
8222 	    && sgp->sg_gui_sp == INVALCOLOR
8223 	    && sgp->sg_font == NOFONT
8224 # ifdef FEAT_XFONTSET
8225 	    && sgp->sg_fontset == NOFONTSET
8226 # endif
8227 	    )
8228     {
8229 	sgp->sg_gui_attr = sgp->sg_gui;
8230     }
8231     else
8232     {
8233 	at_en.ae_attr = sgp->sg_gui;
8234 	at_en.ae_u.gui.fg_color = sgp->sg_gui_fg;
8235 	at_en.ae_u.gui.bg_color = sgp->sg_gui_bg;
8236 	at_en.ae_u.gui.sp_color = sgp->sg_gui_sp;
8237 	at_en.ae_u.gui.font = sgp->sg_font;
8238 # ifdef FEAT_XFONTSET
8239 	at_en.ae_u.gui.fontset = sgp->sg_fontset;
8240 # endif
8241 	sgp->sg_gui_attr = get_attr_entry(&gui_attr_table, &at_en);
8242     }
8243 #endif
8244     /*
8245      * For the term mode: If there are other than "normal" highlighting
8246      * attributes, need to allocate an attr number.
8247      */
8248     if (sgp->sg_start == NULL && sgp->sg_stop == NULL)
8249 	sgp->sg_term_attr = sgp->sg_term;
8250     else
8251     {
8252 	at_en.ae_attr = sgp->sg_term;
8253 	at_en.ae_u.term.start = sgp->sg_start;
8254 	at_en.ae_u.term.stop = sgp->sg_stop;
8255 	sgp->sg_term_attr = get_attr_entry(&term_attr_table, &at_en);
8256     }
8257 
8258     /*
8259      * For the color term mode: If there are other than "normal"
8260      * highlighting attributes, need to allocate an attr number.
8261      */
8262     if (sgp->sg_cterm_fg == 0 && sgp->sg_cterm_bg == 0)
8263 	sgp->sg_cterm_attr = sgp->sg_cterm;
8264     else
8265     {
8266 	at_en.ae_attr = sgp->sg_cterm;
8267 	at_en.ae_u.cterm.fg_color = sgp->sg_cterm_fg;
8268 	at_en.ae_u.cterm.bg_color = sgp->sg_cterm_bg;
8269 	sgp->sg_cterm_attr = get_attr_entry(&cterm_attr_table, &at_en);
8270     }
8271 }
8272 
8273 /*
8274  * Lookup a highlight group name and return it's ID.
8275  * If it is not found, 0 is returned.
8276  */
8277     int
8278 syn_name2id(name)
8279     char_u	*name;
8280 {
8281     int		i;
8282     char_u	name_u[200];
8283 
8284     /* Avoid using stricmp() too much, it's slow on some systems */
8285     /* Avoid alloc()/free(), these are slow too.  ID names over 200 chars
8286      * don't deserve to be found! */
8287     vim_strncpy(name_u, name, 199);
8288     vim_strup(name_u);
8289     for (i = highlight_ga.ga_len; --i >= 0; )
8290 	if (HL_TABLE()[i].sg_name_u != NULL
8291 		&& STRCMP(name_u, HL_TABLE()[i].sg_name_u) == 0)
8292 	    break;
8293     return i + 1;
8294 }
8295 
8296 #if defined(FEAT_EVAL) || defined(PROTO)
8297 /*
8298  * Return TRUE if highlight group "name" exists.
8299  */
8300     int
8301 highlight_exists(name)
8302     char_u	*name;
8303 {
8304     return (syn_name2id(name) > 0);
8305 }
8306 #endif
8307 
8308 /*
8309  * Like syn_name2id(), but take a pointer + length argument.
8310  */
8311     int
8312 syn_namen2id(linep, len)
8313     char_u  *linep;
8314     int	    len;
8315 {
8316     char_u  *name;
8317     int	    id = 0;
8318 
8319     name = vim_strnsave(linep, len);
8320     if (name != NULL)
8321     {
8322 	id = syn_name2id(name);
8323 	vim_free(name);
8324     }
8325     return id;
8326 }
8327 
8328 /*
8329  * Find highlight group name in the table and return it's ID.
8330  * The argument is a pointer to the name and the length of the name.
8331  * If it doesn't exist yet, a new entry is created.
8332  * Return 0 for failure.
8333  */
8334     int
8335 syn_check_group(pp, len)
8336     char_u		*pp;
8337     int			len;
8338 {
8339     int	    id;
8340     char_u  *name;
8341 
8342     name = vim_strnsave(pp, len);
8343     if (name == NULL)
8344 	return 0;
8345 
8346     id = syn_name2id(name);
8347     if (id == 0)			/* doesn't exist yet */
8348 	id = syn_add_group(name);
8349     else
8350 	vim_free(name);
8351     return id;
8352 }
8353 
8354 /*
8355  * Add new highlight group and return it's ID.
8356  * "name" must be an allocated string, it will be consumed.
8357  * Return 0 for failure.
8358  */
8359     static int
8360 syn_add_group(name)
8361     char_u	*name;
8362 {
8363     char_u	*p;
8364 
8365     /* Check that the name is ASCII letters, digits and underscore. */
8366     for (p = name; *p != NUL; ++p)
8367     {
8368 	if (!vim_isprintc(*p))
8369 	{
8370 	    EMSG(_("E669: Unprintable character in group name"));
8371 	    return 0;
8372 	}
8373 	else if (!ASCII_ISALNUM(*p) && *p != '_')
8374 	{
8375 	    /* This is an error, but since there previously was no check only
8376 	     * give a warning. */
8377 	    msg_source(hl_attr(HLF_W));
8378 	    MSG(_("W18: Invalid character in group name"));
8379 	    break;
8380 	}
8381     }
8382 
8383     /*
8384      * First call for this growarray: init growing array.
8385      */
8386     if (highlight_ga.ga_data == NULL)
8387     {
8388 	highlight_ga.ga_itemsize = sizeof(struct hl_group);
8389 	highlight_ga.ga_growsize = 10;
8390     }
8391 
8392     /*
8393      * Make room for at least one other syntax_highlight entry.
8394      */
8395     if (ga_grow(&highlight_ga, 1) == FAIL)
8396     {
8397 	vim_free(name);
8398 	return 0;
8399     }
8400 
8401     vim_memset(&(HL_TABLE()[highlight_ga.ga_len]), 0, sizeof(struct hl_group));
8402     HL_TABLE()[highlight_ga.ga_len].sg_name = name;
8403     HL_TABLE()[highlight_ga.ga_len].sg_name_u = vim_strsave_up(name);
8404 #ifdef FEAT_GUI
8405     HL_TABLE()[highlight_ga.ga_len].sg_gui_bg = INVALCOLOR;
8406     HL_TABLE()[highlight_ga.ga_len].sg_gui_fg = INVALCOLOR;
8407     HL_TABLE()[highlight_ga.ga_len].sg_gui_sp = INVALCOLOR;
8408 #endif
8409     ++highlight_ga.ga_len;
8410 
8411     return highlight_ga.ga_len;		    /* ID is index plus one */
8412 }
8413 
8414 /*
8415  * When, just after calling syn_add_group(), an error is discovered, this
8416  * function deletes the new name.
8417  */
8418     static void
8419 syn_unadd_group()
8420 {
8421     --highlight_ga.ga_len;
8422     vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name);
8423     vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name_u);
8424 }
8425 
8426 /*
8427  * Translate a group ID to highlight attributes.
8428  */
8429     int
8430 syn_id2attr(hl_id)
8431     int			hl_id;
8432 {
8433     int			attr;
8434     struct hl_group	*sgp;
8435 
8436     hl_id = syn_get_final_id(hl_id);
8437     sgp = &HL_TABLE()[hl_id - 1];	    /* index is ID minus one */
8438 
8439 #ifdef FEAT_GUI
8440     /*
8441      * Only use GUI attr when the GUI is being used.
8442      */
8443     if (gui.in_use)
8444 	attr = sgp->sg_gui_attr;
8445     else
8446 #endif
8447 	if (t_colors > 1)
8448 	    attr = sgp->sg_cterm_attr;
8449 	else
8450 	    attr = sgp->sg_term_attr;
8451 
8452     return attr;
8453 }
8454 
8455 #ifdef FEAT_GUI
8456 /*
8457  * Get the GUI colors and attributes for a group ID.
8458  * NOTE: the colors will be INVALCOLOR when not set, the color otherwise.
8459  */
8460     int
8461 syn_id2colors(hl_id, fgp, bgp)
8462     int		hl_id;
8463     guicolor_T	*fgp;
8464     guicolor_T	*bgp;
8465 {
8466     struct hl_group	*sgp;
8467 
8468     hl_id = syn_get_final_id(hl_id);
8469     sgp = &HL_TABLE()[hl_id - 1];	    /* index is ID minus one */
8470 
8471     *fgp = sgp->sg_gui_fg;
8472     *bgp = sgp->sg_gui_bg;
8473     return sgp->sg_gui;
8474 }
8475 #endif
8476 
8477 /*
8478  * Translate a group ID to the final group ID (following links).
8479  */
8480     int
8481 syn_get_final_id(hl_id)
8482     int			hl_id;
8483 {
8484     int			count;
8485     struct hl_group	*sgp;
8486 
8487     if (hl_id > highlight_ga.ga_len || hl_id < 1)
8488 	return 0;			/* Can be called from eval!! */
8489 
8490     /*
8491      * Follow links until there is no more.
8492      * Look out for loops!  Break after 100 links.
8493      */
8494     for (count = 100; --count >= 0; )
8495     {
8496 	sgp = &HL_TABLE()[hl_id - 1];	    /* index is ID minus one */
8497 	if (sgp->sg_link == 0 || sgp->sg_link > highlight_ga.ga_len)
8498 	    break;
8499 	hl_id = sgp->sg_link;
8500     }
8501 
8502     return hl_id;
8503 }
8504 
8505 #ifdef FEAT_GUI
8506 /*
8507  * Call this function just after the GUI has started.
8508  * It finds the font and color handles for the highlighting groups.
8509  */
8510     void
8511 highlight_gui_started()
8512 {
8513     int	    idx;
8514 
8515     /* First get the colors from the "Normal" and "Menu" group, if set */
8516     set_normal_colors();
8517 
8518     for (idx = 0; idx < highlight_ga.ga_len; ++idx)
8519 	gui_do_one_color(idx, FALSE, FALSE);
8520 
8521     highlight_changed();
8522 }
8523 
8524     static void
8525 gui_do_one_color(idx, do_menu, do_tooltip)
8526     int		idx;
8527     int		do_menu;	/* TRUE: might set the menu font */
8528     int		do_tooltip;	/* TRUE: might set the tooltip font */
8529 {
8530     int		didit = FALSE;
8531 
8532     if (HL_TABLE()[idx].sg_font_name != NULL)
8533     {
8534 	hl_do_font(idx, HL_TABLE()[idx].sg_font_name, FALSE, do_menu,
8535 		   do_tooltip);
8536 	didit = TRUE;
8537     }
8538     if (HL_TABLE()[idx].sg_gui_fg_name != NULL)
8539     {
8540 	HL_TABLE()[idx].sg_gui_fg =
8541 			    color_name2handle(HL_TABLE()[idx].sg_gui_fg_name);
8542 	didit = TRUE;
8543     }
8544     if (HL_TABLE()[idx].sg_gui_bg_name != NULL)
8545     {
8546 	HL_TABLE()[idx].sg_gui_bg =
8547 			    color_name2handle(HL_TABLE()[idx].sg_gui_bg_name);
8548 	didit = TRUE;
8549     }
8550     if (HL_TABLE()[idx].sg_gui_sp_name != NULL)
8551     {
8552 	HL_TABLE()[idx].sg_gui_sp =
8553 			    color_name2handle(HL_TABLE()[idx].sg_gui_sp_name);
8554 	didit = TRUE;
8555     }
8556     if (didit)	/* need to get a new attr number */
8557 	set_hl_attr(idx);
8558 }
8559 
8560 #endif
8561 
8562 /*
8563  * Translate the 'highlight' option into attributes in highlight_attr[] and
8564  * set up the user highlights User1..9.  If FEAT_STL_OPT is in use, a set of
8565  * corresponding highlights to use on top of HLF_SNC is computed.
8566  * Called only when the 'highlight' option has been changed and upon first
8567  * screen redraw after any :highlight command.
8568  * Return FAIL when an invalid flag is found in 'highlight'.  OK otherwise.
8569  */
8570     int
8571 highlight_changed()
8572 {
8573     int		hlf;
8574     int		i;
8575     char_u	*p;
8576     int		attr;
8577     char_u	*end;
8578     int		id;
8579 #ifdef USER_HIGHLIGHT
8580     char_u      userhl[10];
8581 # ifdef FEAT_STL_OPT
8582     int		id_SNC = -1;
8583     int		id_S = -1;
8584     int		hlcnt;
8585 # endif
8586 #endif
8587     static int	hl_flags[HLF_COUNT] = HL_FLAGS;
8588 
8589     need_highlight_changed = FALSE;
8590 
8591     /*
8592      * Clear all attributes.
8593      */
8594     for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
8595 	highlight_attr[hlf] = 0;
8596 
8597     /*
8598      * First set all attributes to their default value.
8599      * Then use the attributes from the 'highlight' option.
8600      */
8601     for (i = 0; i < 2; ++i)
8602     {
8603 	if (i)
8604 	    p = p_hl;
8605 	else
8606 	    p = get_highlight_default();
8607 	if (p == NULL)	    /* just in case */
8608 	    continue;
8609 
8610 	while (*p)
8611 	{
8612 	    for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
8613 		if (hl_flags[hlf] == *p)
8614 		    break;
8615 	    ++p;
8616 	    if (hlf == (int)HLF_COUNT || *p == NUL)
8617 		return FAIL;
8618 
8619 	    /*
8620 	     * Allow several hl_flags to be combined, like "bu" for
8621 	     * bold-underlined.
8622 	     */
8623 	    attr = 0;
8624 	    for ( ; *p && *p != ','; ++p)	    /* parse upto comma */
8625 	    {
8626 		if (vim_iswhite(*p))		    /* ignore white space */
8627 		    continue;
8628 
8629 		if (attr > HL_ALL)  /* Combination with ':' is not allowed. */
8630 		    return FAIL;
8631 
8632 		switch (*p)
8633 		{
8634 		    case 'b':	attr |= HL_BOLD;
8635 				break;
8636 		    case 'i':	attr |= HL_ITALIC;
8637 				break;
8638 		    case '-':
8639 		    case 'n':			    /* no highlighting */
8640 				break;
8641 		    case 'r':	attr |= HL_INVERSE;
8642 				break;
8643 		    case 's':	attr |= HL_STANDOUT;
8644 				break;
8645 		    case 'u':	attr |= HL_UNDERLINE;
8646 				break;
8647 		    case 'c':	attr |= HL_UNDERCURL;
8648 				break;
8649 		    case ':':	++p;		    /* highlight group name */
8650 				if (attr || *p == NUL)	 /* no combinations */
8651 				    return FAIL;
8652 				end = vim_strchr(p, ',');
8653 				if (end == NULL)
8654 				    end = p + STRLEN(p);
8655 				id = syn_check_group(p, (int)(end - p));
8656 				if (id == 0)
8657 				    return FAIL;
8658 				attr = syn_id2attr(id);
8659 				p = end - 1;
8660 #if defined(FEAT_STL_OPT) && defined(USER_HIGHLIGHT)
8661 				if (hlf == (int)HLF_SNC)
8662 				    id_SNC = syn_get_final_id(id);
8663 				else if (hlf == (int)HLF_S)
8664 				    id_S = syn_get_final_id(id);
8665 #endif
8666 				break;
8667 		    default:	return FAIL;
8668 		}
8669 	    }
8670 	    highlight_attr[hlf] = attr;
8671 
8672 	    p = skip_to_option_part(p);	    /* skip comma and spaces */
8673 	}
8674     }
8675 
8676 #ifdef USER_HIGHLIGHT
8677     /* Setup the user highlights
8678      *
8679      * Temporarily  utilize 10 more hl entries.  Have to be in there
8680      * simultaneously in case of table overflows in get_attr_entry()
8681      */
8682 # ifdef FEAT_STL_OPT
8683     if (ga_grow(&highlight_ga, 10) == FAIL)
8684 	return FAIL;
8685     hlcnt = highlight_ga.ga_len;
8686     if (id_S == 0)
8687     {		    /* Make sure id_S is always valid to simplify code below */
8688 	memset(&HL_TABLE()[hlcnt + 9], 0, sizeof(struct hl_group));
8689 	HL_TABLE()[hlcnt + 9].sg_term = highlight_attr[HLF_S];
8690 	id_S = hlcnt + 10;
8691     }
8692 # endif
8693     for (i = 0; i < 9; i++)
8694     {
8695 	sprintf((char *)userhl, "User%d", i + 1);
8696 	id = syn_name2id(userhl);
8697 	if (id == 0)
8698 	{
8699 	    highlight_user[i] = 0;
8700 # ifdef FEAT_STL_OPT
8701 	    highlight_stlnc[i] = 0;
8702 # endif
8703 	}
8704 	else
8705 	{
8706 # ifdef FEAT_STL_OPT
8707 	    struct hl_group *hlt = HL_TABLE();
8708 # endif
8709 
8710 	    highlight_user[i] = syn_id2attr(id);
8711 # ifdef FEAT_STL_OPT
8712 	    if (id_SNC == 0)
8713 	    {
8714 		memset(&hlt[hlcnt + i], 0, sizeof(struct hl_group));
8715 		hlt[hlcnt + i].sg_term = highlight_attr[HLF_SNC];
8716 		hlt[hlcnt + i].sg_cterm = highlight_attr[HLF_SNC];
8717 #  ifdef FEAT_GUI
8718 		hlt[hlcnt + i].sg_gui = highlight_attr[HLF_SNC];
8719 #  endif
8720 	    }
8721 	    else
8722 		mch_memmove(&hlt[hlcnt + i],
8723 			    &hlt[id_SNC - 1],
8724 			    sizeof(struct hl_group));
8725 	    hlt[hlcnt + i].sg_link = 0;
8726 
8727 	    /* Apply difference between UserX and HLF_S to HLF_SNC */
8728 	    hlt[hlcnt + i].sg_term ^=
8729 		hlt[id - 1].sg_term ^ hlt[id_S - 1].sg_term;
8730 	    if (hlt[id - 1].sg_start != hlt[id_S - 1].sg_start)
8731 		hlt[hlcnt + i].sg_start = hlt[id - 1].sg_start;
8732 	    if (hlt[id - 1].sg_stop != hlt[id_S - 1].sg_stop)
8733 		hlt[hlcnt + i].sg_stop = hlt[id - 1].sg_stop;
8734 	    hlt[hlcnt + i].sg_cterm ^=
8735 		hlt[id - 1].sg_cterm ^ hlt[id_S - 1].sg_cterm;
8736 	    if (hlt[id - 1].sg_cterm_fg != hlt[id_S - 1].sg_cterm_fg)
8737 		hlt[hlcnt + i].sg_cterm_fg = hlt[id - 1].sg_cterm_fg;
8738 	    if (hlt[id - 1].sg_cterm_bg != hlt[id_S - 1].sg_cterm_bg)
8739 		hlt[hlcnt + i].sg_cterm_bg = hlt[id - 1].sg_cterm_bg;
8740 #  ifdef FEAT_GUI
8741 	    hlt[hlcnt + i].sg_gui ^=
8742 		hlt[id - 1].sg_gui ^ hlt[id_S - 1].sg_gui;
8743 	    if (hlt[id - 1].sg_gui_fg != hlt[id_S - 1].sg_gui_fg)
8744 		hlt[hlcnt + i].sg_gui_fg = hlt[id - 1].sg_gui_fg;
8745 	    if (hlt[id - 1].sg_gui_bg != hlt[id_S - 1].sg_gui_bg)
8746 		hlt[hlcnt + i].sg_gui_bg = hlt[id - 1].sg_gui_bg;
8747 	    if (hlt[id - 1].sg_gui_sp != hlt[id_S - 1].sg_gui_sp)
8748 		hlt[hlcnt + i].sg_gui_sp = hlt[id - 1].sg_gui_sp;
8749 	    if (hlt[id - 1].sg_font != hlt[id_S - 1].sg_font)
8750 		hlt[hlcnt + i].sg_font = hlt[id - 1].sg_font;
8751 #   ifdef FEAT_XFONTSET
8752 	    if (hlt[id - 1].sg_fontset != hlt[id_S - 1].sg_fontset)
8753 		hlt[hlcnt + i].sg_fontset = hlt[id - 1].sg_fontset;
8754 #   endif
8755 #  endif
8756 	    highlight_ga.ga_len = hlcnt + i + 1;
8757 	    set_hl_attr(hlcnt + i);	/* At long last we can apply */
8758 	    highlight_stlnc[i] = syn_id2attr(hlcnt + i + 1);
8759 # endif
8760 	}
8761     }
8762 # ifdef FEAT_STL_OPT
8763     highlight_ga.ga_len = hlcnt;
8764 # endif
8765 
8766 #endif /* USER_HIGHLIGHT */
8767 
8768     return OK;
8769 }
8770 
8771 #ifdef FEAT_CMDL_COMPL
8772 
8773 static void highlight_list __ARGS((void));
8774 static void highlight_list_two __ARGS((int cnt, int attr));
8775 
8776 /*
8777  * Handle command line completion for :highlight command.
8778  */
8779     void
8780 set_context_in_highlight_cmd(xp, arg)
8781     expand_T	*xp;
8782     char_u	*arg;
8783 {
8784     char_u	*p;
8785 
8786     /* Default: expand group names */
8787     xp->xp_context = EXPAND_HIGHLIGHT;
8788     xp->xp_pattern = arg;
8789     include_link = TRUE;
8790     include_default = TRUE;
8791 
8792     /* (part of) subcommand already typed */
8793     if (*arg != NUL)
8794     {
8795 	p = skiptowhite(arg);
8796 	if (*p != NUL)			/* past "default" or group name */
8797 	{
8798 	    include_default = FALSE;
8799 	    if (STRNCMP("default", arg, p - arg) == 0)
8800 	    {
8801 		arg = skipwhite(p);
8802 		xp->xp_pattern = arg;
8803 		p = skiptowhite(arg);
8804 	    }
8805 	    if (*p != NUL)			/* past group name */
8806 	    {
8807 		include_link = FALSE;
8808 		if (arg[1] == 'i' && arg[0] == 'N')
8809 		    highlight_list();
8810 		if (STRNCMP("link", arg, p - arg) == 0
8811 			|| STRNCMP("clear", arg, p - arg) == 0)
8812 		{
8813 		    xp->xp_pattern = skipwhite(p);
8814 		    p = skiptowhite(xp->xp_pattern);
8815 		    if (*p != NUL)		/* past first group name */
8816 		    {
8817 			xp->xp_pattern = skipwhite(p);
8818 			p = skiptowhite(xp->xp_pattern);
8819 		    }
8820 		}
8821 		if (*p != NUL)			/* past group name(s) */
8822 		    xp->xp_context = EXPAND_NOTHING;
8823 	    }
8824 	}
8825     }
8826 }
8827 
8828 /*
8829  * List highlighting matches in a nice way.
8830  */
8831     static void
8832 highlight_list()
8833 {
8834     int		i;
8835 
8836     for (i = 10; --i >= 0; )
8837 	highlight_list_two(i, hl_attr(HLF_D));
8838     for (i = 40; --i >= 0; )
8839 	highlight_list_two(99, 0);
8840 }
8841 
8842     static void
8843 highlight_list_two(cnt, attr)
8844     int	    cnt;
8845     int	    attr;
8846 {
8847     msg_puts_attr((char_u *)("N \bI \b!  \b" + cnt / 11), attr);
8848     msg_clr_eos();
8849     out_flush();
8850     ui_delay(cnt == 99 ? 40L : (long)cnt * 50L, FALSE);
8851 }
8852 
8853 #endif /* FEAT_CMDL_COMPL */
8854 
8855 #if defined(FEAT_CMDL_COMPL) || (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) \
8856     || defined(FEAT_SIGNS) || defined(PROTO)
8857 /*
8858  * Function given to ExpandGeneric() to obtain the list of group names.
8859  * Also used for synIDattr() function.
8860  */
8861 /*ARGSUSED*/
8862     char_u *
8863 get_highlight_name(xp, idx)
8864     expand_T	*xp;
8865     int		idx;
8866 {
8867     if (idx == highlight_ga.ga_len
8868 #ifdef FEAT_CMDL_COMPL
8869 	    && include_link
8870 #endif
8871 	    )
8872 	return (char_u *)"link";
8873     if (idx == highlight_ga.ga_len + 1
8874 #ifdef FEAT_CMDL_COMPL
8875 	    && include_link
8876 #endif
8877 	    )
8878 	return (char_u *)"clear";
8879     if (idx == highlight_ga.ga_len + 2
8880 #ifdef FEAT_CMDL_COMPL
8881 	    && include_default
8882 #endif
8883 	    )
8884 	return (char_u *)"default";
8885     if (idx < 0 || idx >= highlight_ga.ga_len)
8886 	return NULL;
8887     return HL_TABLE()[idx].sg_name;
8888 }
8889 #endif
8890 
8891 #ifdef FEAT_GUI
8892 /*
8893  * Free all the highlight group fonts.
8894  * Used when quitting for systems which need it.
8895  */
8896     void
8897 free_highlight_fonts()
8898 {
8899     int	    idx;
8900 
8901     for (idx = 0; idx < highlight_ga.ga_len; ++idx)
8902     {
8903 	gui_mch_free_font(HL_TABLE()[idx].sg_font);
8904 	HL_TABLE()[idx].sg_font = NOFONT;
8905 # ifdef FEAT_XFONTSET
8906 	gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
8907 	HL_TABLE()[idx].sg_fontset = NOFONTSET;
8908 # endif
8909     }
8910 
8911     gui_mch_free_font(gui.norm_font);
8912 # ifdef FEAT_XFONTSET
8913     gui_mch_free_fontset(gui.fontset);
8914 # endif
8915 # ifndef HAVE_GTK2
8916     gui_mch_free_font(gui.bold_font);
8917     gui_mch_free_font(gui.ital_font);
8918     gui_mch_free_font(gui.boldital_font);
8919 # endif
8920 }
8921 #endif
8922 
8923 /**************************************
8924  *  End of Highlighting stuff	      *
8925  **************************************/
8926