blob: 2b70c6d4513041aaa25fc66fb1c415262e41ae32 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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 */
21struct 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 */
Bram Moolenaare2cc9702005-03-15 22:43:58 +000043 guicolor_T sg_gui_sp; /* GUI special color handle */
44 char_u *sg_gui_sp_name;/* GUI special color name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 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 */
Bram Moolenaar661b1822005-07-28 22:36:45 +000054#ifdef FEAT_EVAL
55 scid_T sg_scriptID; /* script in which the group was last set */
56#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000057};
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
64static 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
Bram Moolenaar4f688582007-07-24 12:34:30 +000069/* Flags to indicate an additional string for highlight name completion. */
70static int include_none = 0; /* when 1 include "None" */
71static int include_default = 0; /* when 1 include "default" */
72static int include_link = 0; /* when 2 include "link" and "clear" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000073#endif
74
75/*
76 * The "term", "cterm" and "gui" arguments can be any combination of the
77 * following names, separated by commas (but no spaces!).
78 */
79static char *(hl_name_table[]) =
Bram Moolenaare2cc9702005-03-15 22:43:58 +000080 {"bold", "standout", "underline", "undercurl",
81 "italic", "reverse", "inverse", "NONE"};
Bram Moolenaar071d4272004-06-13 20:20:40 +000082static int hl_attr_table[] =
Bram Moolenaare2cc9702005-03-15 22:43:58 +000083 {HL_BOLD, HL_STANDOUT, HL_UNDERLINE, HL_UNDERCURL, HL_ITALIC, HL_INVERSE, HL_INVERSE, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
85static int get_attr_entry __ARGS((garray_T *table, attrentry_T *aep));
86static void syn_unadd_group __ARGS((void));
87static void set_hl_attr __ARGS((int idx));
88static void highlight_list_one __ARGS((int id));
89static int highlight_list_arg __ARGS((int id, int didh, int type, int iarg, char_u *sarg, char *name));
90static int syn_add_group __ARGS((char_u *name));
91static int syn_list_header __ARGS((int did_header, int outlen, int id));
92static int hl_has_settings __ARGS((int idx, int check_link));
93static void highlight_clear __ARGS((int idx));
94
95#ifdef FEAT_GUI
96static void gui_do_one_color __ARGS((int idx, int do_menu, int do_tooltip));
97static int set_group_colors __ARGS((char_u *name, guicolor_T *fgp, guicolor_T *bgp, int do_menu, int use_norm, int do_tooltip));
98static guicolor_T color_name2handle __ARGS((char_u *name));
99static GuiFont font_name2handle __ARGS((char_u *name));
100# ifdef FEAT_XFONTSET
101static GuiFontset fontset_name2handle __ARGS((char_u *name, int fixed_width));
102# endif
103static void hl_do_font __ARGS((int idx, char_u *arg, int do_normal, int do_menu, int do_tooltip));
104#endif
105
106/*
107 * An attribute number is the index in attr_table plus ATTR_OFF.
108 */
109#define ATTR_OFF (HL_ALL + 1)
110
111#if defined(FEAT_SYN_HL) || defined(PROTO)
112
113#define SYN_NAMELEN 50 /* maximum length of a syntax name */
114
115/* different types of offsets that are possible */
116#define SPO_MS_OFF 0 /* match start offset */
117#define SPO_ME_OFF 1 /* match end offset */
118#define SPO_HS_OFF 2 /* highl. start offset */
119#define SPO_HE_OFF 3 /* highl. end offset */
120#define SPO_RS_OFF 4 /* region start offset */
121#define SPO_RE_OFF 5 /* region end offset */
122#define SPO_LC_OFF 6 /* leading context offset */
123#define SPO_COUNT 7
124
125static char *(spo_name_tab[SPO_COUNT]) =
126 {"ms=", "me=", "hs=", "he=", "rs=", "re=", "lc="};
127
128/*
129 * The patterns that are being searched for are stored in a syn_pattern.
130 * A match item consists of one pattern.
131 * A start/end item consists of n start patterns and m end patterns.
132 * A start/skip/end item consists of n start patterns, one skip pattern and m
133 * end patterns.
134 * For the latter two, the patterns are always consecutive: start-skip-end.
135 *
136 * A character offset can be given for the matched text (_m_start and _m_end)
137 * and for the actually highlighted text (_h_start and _h_end).
138 */
139typedef struct syn_pattern
140{
141 char sp_type; /* see SPTYPE_ defines below */
142 char sp_syncing; /* this item used for syncing */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200143 int sp_flags; /* see HL_ defines below */
144#ifdef FEAT_CONCEAL
145 int sp_char; /* conceal substitute character */
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 struct sp_syn sp_syn; /* struct passed to in_id_list() */
148 short sp_syn_match_id; /* highlight group ID of pattern */
149 char_u *sp_pattern; /* regexp to match, pattern */
150 regprog_T *sp_prog; /* regexp to match, program */
151 int sp_ic; /* ignore-case flag for sp_prog */
152 short sp_off_flags; /* see below */
153 int sp_offsets[SPO_COUNT]; /* offsets */
154 short *sp_cont_list; /* cont. group IDs, if non-zero */
155 short *sp_next_list; /* next group IDs, if non-zero */
156 int sp_sync_idx; /* sync item index (syncing only) */
157 int sp_line_id; /* ID of last line where tried */
158 int sp_startcol; /* next match in sp_line_id line */
159} synpat_T;
160
161/* The sp_off_flags are computed like this:
162 * offset from the start of the matched text: (1 << SPO_XX_OFF)
163 * offset from the end of the matched text: (1 << (SPO_XX_OFF + SPO_COUNT))
164 * When both are present, only one is used.
165 */
166
167#define SPTYPE_MATCH 1 /* match keyword with this group ID */
168#define SPTYPE_START 2 /* match a regexp, start of item */
169#define SPTYPE_END 3 /* match a regexp, end of item */
170#define SPTYPE_SKIP 4 /* match a regexp, skip within item */
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173#define SYN_ITEMS(buf) ((synpat_T *)((buf)->b_syn_patterns.ga_data))
174
175#define NONE_IDX -2 /* value of sp_sync_idx for "NONE" */
176
177/*
178 * Flags for b_syn_sync_flags:
179 */
180#define SF_CCOMMENT 0x01 /* sync on a C-style comment */
181#define SF_MATCH 0x02 /* sync by matching a pattern */
182
183#define SYN_STATE_P(ssp) ((bufstate_T *)((ssp)->ga_data))
184
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#define MAXKEYWLEN 80 /* maximum length of a keyword */
186
187/*
188 * The attributes of the syntax item that has been recognized.
189 */
190static int current_attr = 0; /* attr of current syntax word */
191#ifdef FEAT_EVAL
192static int current_id = 0; /* ID of current char for syn_get_id() */
Bram Moolenaarf5b63862009-12-16 17:13:44 +0000193static int current_trans_id = 0; /* idem, transparency removed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200195#ifdef FEAT_CONCEAL
196static int current_flags = 0;
197static int current_sub_char = 0;
198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
Bram Moolenaar217ad922005-03-20 22:37:15 +0000200typedef struct syn_cluster_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 char_u *scl_name; /* syntax cluster name */
203 char_u *scl_name_u; /* uppercase of scl_name */
204 short *scl_list; /* IDs in this syntax cluster */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000205} syn_cluster_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
208 * Methods of combining two clusters
209 */
210#define CLUSTER_REPLACE 1 /* replace first list with second */
211#define CLUSTER_ADD 2 /* add second list to first */
212#define CLUSTER_SUBTRACT 3 /* subtract second list from first */
213
Bram Moolenaar217ad922005-03-20 22:37:15 +0000214#define SYN_CLSTR(buf) ((syn_cluster_T *)((buf)->b_syn_clusters.ga_data))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216/*
217 * Syntax group IDs have different types:
218 * 0 - 9999 normal syntax groups
219 * 10000 - 14999 ALLBUT indicator (current_syn_inc_tag added)
220 * 15000 - 19999 TOP indicator (current_syn_inc_tag added)
221 * 20000 - 24999 CONTAINED indicator (current_syn_inc_tag added)
222 * >= 25000 cluster IDs (subtract SYNID_CLUSTER for the cluster ID)
223 */
224#define SYNID_ALLBUT 10000 /* syntax group ID for contains=ALLBUT */
225#define SYNID_TOP 15000 /* syntax group ID for contains=TOP */
226#define SYNID_CONTAINED 20000 /* syntax group ID for contains=CONTAINED */
227#define SYNID_CLUSTER 25000 /* first syntax group ID for clusters */
228
229/*
230 * Annoying Hack(TM): ":syn include" needs this pointer to pass to
231 * expand_filename(). Most of the other syntax commands don't need it, so
232 * instead of passing it to them, we stow it here.
233 */
234static char_u **syn_cmdlinep;
235
236/*
237 * Another Annoying Hack(TM): To prevent rules from other ":syn include"'d
Bram Moolenaar56be9502010-06-06 14:20:26 +0200238 * files from leaking into ALLBUT lists, we assign a unique ID to the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 * rules in each ":syn include"'d file.
240 */
241static int current_syn_inc_tag = 0;
242static int running_syn_inc_tag = 0;
243
244/*
Bram Moolenaardad6b692005-01-25 22:14:34 +0000245 * In a hashtable item "hi_key" points to "keyword" in a keyentry.
246 * This avoids adding a pointer to the hashtable item.
247 * KE2HIKEY() converts a var pointer to a hashitem key pointer.
248 * HIKEY2KE() converts a hashitem key pointer to a var pointer.
249 * HI2KE() converts a hashitem pointer to a var pointer.
250 */
251static keyentry_T dumkey;
252#define KE2HIKEY(kp) ((kp)->keyword)
253#define HIKEY2KE(p) ((keyentry_T *)((p) - (dumkey.keyword - (char_u *)&dumkey)))
254#define HI2KE(hi) HIKEY2KE((hi)->hi_key)
255
256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 * To reduce the time spent in keepend(), remember at which level in the state
258 * stack the first item with "keepend" is present. When "-1", there is no
259 * "keepend" on the stack.
260 */
261static int keepend_level = -1;
262
263/*
264 * For the current state we need to remember more than just the idx.
265 * When si_m_endpos.lnum is 0, the items other than si_idx are unknown.
266 * (The end positions have the column number of the next char)
267 */
268typedef struct state_item
269{
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000270 int si_idx; /* index of syntax pattern or
271 KEYWORD_IDX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 int si_id; /* highlight group ID for keywords */
Bram Moolenaarf5b63862009-12-16 17:13:44 +0000273 int si_trans_id; /* idem, transparency removed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 int si_m_lnum; /* lnum of the match */
275 int si_m_startcol; /* starting column of the match */
276 lpos_T si_m_endpos; /* just after end posn of the match */
277 lpos_T si_h_startpos; /* start position of the highlighting */
278 lpos_T si_h_endpos; /* end position of the highlighting */
279 lpos_T si_eoe_pos; /* end position of end pattern */
280 int si_end_idx; /* group ID for end pattern or zero */
281 int si_ends; /* if match ends before si_m_endpos */
282 int si_attr; /* attributes in this state */
283 long si_flags; /* HL_HAS_EOL flag in this state, and
284 * HL_SKIP* for si_next_list */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200285#ifdef FEAT_CONCEAL
286 int si_char; /* substitution character for conceal */
287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 short *si_cont_list; /* list of contained groups */
289 short *si_next_list; /* nextgroup IDs after this item ends */
290 reg_extmatch_T *si_extmatch; /* \z(...\) matches from start
291 * pattern */
292} stateitem_T;
293
294#define KEYWORD_IDX -1 /* value of si_idx for keywords */
295#define ID_LIST_ALL (short *)-1 /* valid of si_cont_list for containing all
296 but contained groups */
297
298/*
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000299 * Struct to reduce the number of arguments to get_syn_options(), it's used
300 * very often.
301 */
302typedef struct
303{
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000304 int flags; /* flags for contained and transparent */
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000305 int keyword; /* TRUE for ":syn keyword" */
306 int *sync_idx; /* syntax item for "grouphere" argument, NULL
307 if not allowed */
308 char has_cont_list; /* TRUE if "cont_list" can be used */
309 short *cont_list; /* group IDs for "contains" argument */
310 short *cont_in_list; /* group IDs for "containedin" argument */
311 short *next_list; /* group IDs for "nextgroup" argument */
312} syn_opt_arg_T;
313
314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 * The next possible match in the current line for any pattern is remembered,
316 * to avoid having to try for a match in each column.
317 * If next_match_idx == -1, not tried (in this line) yet.
318 * If next_match_col == MAXCOL, no match found in this line.
319 * (All end positions have the column of the char after the end)
320 */
321static int next_match_col; /* column for start of next match */
322static lpos_T next_match_m_endpos; /* position for end of next match */
323static lpos_T next_match_h_startpos; /* pos. for highl. start of next match */
324static lpos_T next_match_h_endpos; /* pos. for highl. end of next match */
325static int next_match_idx; /* index of matched item */
326static long next_match_flags; /* flags for next match */
327static lpos_T next_match_eos_pos; /* end of start pattn (start region) */
328static lpos_T next_match_eoe_pos; /* pos. for end of end pattern */
329static int next_match_end_idx; /* ID of group for end pattn or zero */
330static reg_extmatch_T *next_match_extmatch = NULL;
331
332/*
333 * A state stack is an array of integers or stateitem_T, stored in a
334 * garray_T. A state stack is invalid if it's itemsize entry is zero.
335 */
336#define INVALID_STATE(ssp) ((ssp)->ga_itemsize == 0)
337#define VALID_STATE(ssp) ((ssp)->ga_itemsize != 0)
338
339/*
340 * The current state (within the line) of the recognition engine.
341 * When current_state.ga_itemsize is 0 the current state is invalid.
342 */
343static win_T *syn_win; /* current window for highlighting */
344static buf_T *syn_buf; /* current buffer for highlighting */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200345static synblock_T *syn_block; /* current buffer for highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346static linenr_T current_lnum = 0; /* lnum of current state */
347static colnr_T current_col = 0; /* column of current state */
348static int current_state_stored = 0; /* TRUE if stored current state
349 * after setting current_finished */
350static int current_finished = 0; /* current line has been finished */
351static garray_T current_state /* current stack of state_items */
352 = {0, 0, 0, 0, NULL};
353static short *current_next_list = NULL; /* when non-zero, nextgroup list */
354static int current_next_flags = 0; /* flags for current_next_list */
355static int current_line_id = 0; /* unique number for current line */
356
357#define CUR_STATE(idx) ((stateitem_T *)(current_state.ga_data))[idx]
358
359static void syn_sync __ARGS((win_T *wp, linenr_T lnum, synstate_T *last_valid));
360static int syn_match_linecont __ARGS((linenr_T lnum));
361static void syn_start_line __ARGS((void));
362static void syn_update_ends __ARGS((int startofline));
363static void syn_stack_alloc __ARGS((void));
364static int syn_stack_cleanup __ARGS((void));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200365static void syn_stack_free_entry __ARGS((synblock_T *block, synstate_T *p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366static synstate_T *syn_stack_find_entry __ARGS((linenr_T lnum));
Bram Moolenaardbe31752008-01-13 16:40:19 +0000367static synstate_T *store_current_state __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368static void load_current_state __ARGS((synstate_T *from));
369static void invalidate_current_state __ARGS((void));
370static int syn_stack_equal __ARGS((synstate_T *sp));
371static void validate_current_state __ARGS((void));
372static int syn_finish_line __ARGS((int syncing));
Bram Moolenaar56cefaf2008-01-12 15:47:10 +0000373static int syn_current_attr __ARGS((int syncing, int displaying, int *can_spell, int keep_state));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static int did_match_already __ARGS((int idx, garray_T *gap));
375static stateitem_T *push_next_match __ARGS((stateitem_T *cur_si));
376static void check_state_ends __ARGS((void));
377static void update_si_attr __ARGS((int idx));
378static void check_keepend __ARGS((void));
379static void update_si_end __ARGS((stateitem_T *sip, int startcol, int force));
380static short *copy_id_list __ARGS((short *list));
381static int in_id_list __ARGS((stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained));
382static int push_current_state __ARGS((int idx));
383static void pop_current_state __ARGS((void));
384
Bram Moolenaar860cae12010-06-05 23:22:07 +0200385static void syn_stack_apply_changes_block __ARGS((synblock_T *block, buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static 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));
387static void clear_syn_state __ARGS((synstate_T *p));
388static void clear_current_state __ARGS((void));
389
390static void limit_pos __ARGS((lpos_T *pos, lpos_T *limit));
391static void limit_pos_zero __ARGS((lpos_T *pos, lpos_T *limit));
392static void syn_add_end_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
393static void syn_add_start_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
394static char_u *syn_getcurline __ARGS((void));
395static int syn_regexec __ARGS((regmmatch_T *rmp, linenr_T lnum, colnr_T col));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200396static int check_keyword_id __ARGS((char_u *line, int startcol, int *endcol, long *flags, short **next_list, stateitem_T *cur_si, int *ccharp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static void syn_cmd_case __ARGS((exarg_T *eap, int syncing));
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000398static void syn_cmd_spell __ARGS((exarg_T *eap, int syncing));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399static void syntax_sync_clear __ARGS((void));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200400static void syn_remove_pattern __ARGS((synblock_T *block, int idx));
401static void syn_clear_pattern __ARGS((synblock_T *block, int i));
402static void syn_clear_cluster __ARGS((synblock_T *block, int i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403static void syn_cmd_clear __ARGS((exarg_T *eap, int syncing));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200404static void syn_cmd_conceal __ARGS((exarg_T *eap, int syncing));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405static void syn_clear_one __ARGS((int id, int syncing));
406static void syn_cmd_on __ARGS((exarg_T *eap, int syncing));
407static void syn_cmd_enable __ARGS((exarg_T *eap, int syncing));
408static void syn_cmd_reset __ARGS((exarg_T *eap, int syncing));
409static void syn_cmd_manual __ARGS((exarg_T *eap, int syncing));
410static void syn_cmd_off __ARGS((exarg_T *eap, int syncing));
411static void syn_cmd_onoff __ARGS((exarg_T *eap, char *name));
412static void syn_cmd_list __ARGS((exarg_T *eap, int syncing));
413static void syn_lines_msg __ARGS((void));
414static void syn_match_msg __ARGS((void));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200415static void syn_stack_free_block __ARGS((synblock_T *block));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416static void syn_list_one __ARGS((int id, int syncing, int link_only));
417static void syn_list_cluster __ARGS((int id));
418static void put_id_list __ARGS((char_u *name, short *list, int attr));
419static void put_pattern __ARGS((char *s, int c, synpat_T *spp, int attr));
Bram Moolenaardad6b692005-01-25 22:14:34 +0000420static int syn_list_keywords __ARGS((int id, hashtab_T *ht, int did_header, int attr));
421static void syn_clear_keyword __ARGS((int id, hashtab_T *ht));
422static void clear_keywtab __ARGS((hashtab_T *ht));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200423static void add_keyword __ARGS((char_u *name, int id, int flags, short *cont_in_list, short *next_list, int conceal_char));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static char_u *get_group_name __ARGS((char_u *arg, char_u **name_end));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200425static char_u *get_syn_options __ARGS((char_u *arg, syn_opt_arg_T *opt, int *conceal_char));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426static void syn_cmd_include __ARGS((exarg_T *eap, int syncing));
427static void syn_cmd_keyword __ARGS((exarg_T *eap, int syncing));
428static void syn_cmd_match __ARGS((exarg_T *eap, int syncing));
429static void syn_cmd_region __ARGS((exarg_T *eap, int syncing));
430#ifdef __BORLANDC__
431static int _RTLENTRYF syn_compare_stub __ARGS((const void *v1, const void *v2));
432#else
433static int syn_compare_stub __ARGS((const void *v1, const void *v2));
434#endif
435static void syn_cmd_cluster __ARGS((exarg_T *eap, int syncing));
436static int syn_scl_name2id __ARGS((char_u *name));
437static int syn_scl_namen2id __ARGS((char_u *linep, int len));
438static int syn_check_cluster __ARGS((char_u *pp, int len));
439static int syn_add_cluster __ARGS((char_u *name));
440static void init_syn_patterns __ARGS((void));
441static char_u *get_syn_pattern __ARGS((char_u *arg, synpat_T *ci));
442static void syn_cmd_sync __ARGS((exarg_T *eap, int syncing));
443static int get_id_list __ARGS((char_u **arg, int keylen, short **list));
444static void syn_combine_list __ARGS((short **clstr1, short **clstr2, int list_op));
445static void syn_incl_toplevel __ARGS((int id, int *flagsp));
446
447/*
448 * Start the syntax recognition for a line. This function is normally called
449 * from the screen updating, once for each displayed line.
450 * The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
451 * it. Careful: curbuf and curwin are likely to point to another buffer and
452 * window.
453 */
454 void
455syntax_start(wp, lnum)
456 win_T *wp;
457 linenr_T lnum;
458{
459 synstate_T *p;
460 synstate_T *last_valid = NULL;
461 synstate_T *last_min_valid = NULL;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000462 synstate_T *sp, *prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 linenr_T parsed_lnum;
464 linenr_T first_stored;
465 int dist;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000466 static int changedtick = 0; /* remember the last change ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 /*
469 * After switching buffers, invalidate current_state.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000470 * Also do this when a change was made, the current state may be invalid
471 * then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200473 if (syn_block != wp->w_s || changedtick != syn_buf->b_changedtick)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {
475 invalidate_current_state();
476 syn_buf = wp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200477 syn_block = wp->w_s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000479 changedtick = syn_buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 syn_win = wp;
481
482 /*
483 * Allocate syntax stack when needed.
484 */
485 syn_stack_alloc();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200486 if (syn_block->b_sst_array == NULL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000487 return; /* out of memory */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200488 syn_block->b_sst_lasttick = display_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 /*
491 * If the state of the end of the previous line is useful, store it.
492 */
493 if (VALID_STATE(&current_state)
494 && current_lnum < lnum
495 && current_lnum < syn_buf->b_ml.ml_line_count)
496 {
497 (void)syn_finish_line(FALSE);
498 if (!current_state_stored)
499 {
500 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000501 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503
504 /*
505 * If the current_lnum is now the same as "lnum", keep the current
506 * state (this happens very often!). Otherwise invalidate
507 * current_state and figure it out below.
508 */
509 if (current_lnum != lnum)
510 invalidate_current_state();
511 }
512 else
513 invalidate_current_state();
514
515 /*
516 * Try to synchronize from a saved state in b_sst_array[].
517 * Only do this if lnum is not before and not to far beyond a saved state.
518 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200519 if (INVALID_STATE(&current_state) && syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 {
521 /* Find last valid saved state before start_lnum. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200522 for (p = syn_block->b_sst_first; p != NULL; p = p->sst_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
524 if (p->sst_lnum > lnum)
525 break;
526 if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
527 {
528 last_valid = p;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200529 if (p->sst_lnum >= lnum - syn_block->b_syn_sync_minlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 last_min_valid = p;
531 }
532 }
533 if (last_min_valid != NULL)
534 load_current_state(last_min_valid);
535 }
536
537 /*
538 * If "lnum" is before or far beyond a line with a saved state, need to
539 * re-synchronize.
540 */
541 if (INVALID_STATE(&current_state))
542 {
543 syn_sync(wp, lnum, last_valid);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200544 first_stored = current_lnum + syn_block->b_syn_sync_minlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 else
547 first_stored = current_lnum;
548
549 /*
550 * Advance from the sync point or saved state until the current line.
551 * Save some entries for syncing with later on.
552 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200553 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000554 dist = 999999;
555 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200556 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 while (current_lnum < lnum)
558 {
559 syn_start_line();
560 (void)syn_finish_line(FALSE);
561 ++current_lnum;
562
563 /* If we parsed at least "minlines" lines or started at a valid
564 * state, the current state is considered valid. */
565 if (current_lnum >= first_stored)
566 {
567 /* Check if the saved state entry is for the current line and is
568 * equal to the current state. If so, then validate all saved
569 * states that depended on a change before the parsed line. */
570 if (prev == NULL)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000571 prev = syn_stack_find_entry(current_lnum - 1);
572 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200573 sp = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 else
Bram Moolenaardbe31752008-01-13 16:40:19 +0000575 sp = prev;
576 while (sp != NULL && sp->sst_lnum < current_lnum)
577 sp = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (sp != NULL
579 && sp->sst_lnum == current_lnum
580 && syn_stack_equal(sp))
581 {
582 parsed_lnum = current_lnum;
583 prev = sp;
584 while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
585 {
586 if (sp->sst_lnum <= lnum)
587 /* valid state before desired line, use this one */
588 prev = sp;
589 else if (sp->sst_change_lnum == 0)
590 /* past saved states depending on change, break here. */
591 break;
592 sp->sst_change_lnum = 0;
593 sp = sp->sst_next;
594 }
595 load_current_state(prev);
596 }
597 /* Store the state at this line when it's the first one, the line
598 * where we start parsing, or some distance from the previously
599 * saved state. But only when parsed at least 'minlines'. */
600 else if (prev == NULL
601 || current_lnum == lnum
602 || current_lnum >= prev->sst_lnum + dist)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000603 prev = store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
605
606 /* This can take a long time: break when CTRL-C pressed. The current
607 * state will be wrong then. */
608 line_breakcheck();
609 if (got_int)
610 {
611 current_lnum = lnum;
612 break;
613 }
614 }
615
616 syn_start_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617}
618
619/*
620 * We cannot simply discard growarrays full of state_items or buf_states; we
621 * have to manually release their extmatch pointers first.
622 */
623 static void
624clear_syn_state(p)
625 synstate_T *p;
626{
627 int i;
628 garray_T *gap;
629
630 if (p->sst_stacksize > SST_FIX_STATES)
631 {
632 gap = &(p->sst_union.sst_ga);
633 for (i = 0; i < gap->ga_len; i++)
634 unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
635 ga_clear(gap);
636 }
637 else
638 {
639 for (i = 0; i < p->sst_stacksize; i++)
640 unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
641 }
642}
643
644/*
645 * Cleanup the current_state stack.
646 */
647 static void
648clear_current_state()
649{
650 int i;
651 stateitem_T *sip;
652
653 sip = (stateitem_T *)(current_state.ga_data);
654 for (i = 0; i < current_state.ga_len; i++)
655 unref_extmatch(sip[i].si_extmatch);
656 ga_clear(&current_state);
657}
658
659/*
660 * Try to find a synchronisation point for line "lnum".
661 *
662 * This sets current_lnum and the current state. One of three methods is
663 * used:
664 * 1. Search backwards for the end of a C-comment.
665 * 2. Search backwards for given sync patterns.
666 * 3. Simply start on a given number of lines above "lnum".
667 */
668 static void
669syn_sync(wp, start_lnum, last_valid)
670 win_T *wp;
671 linenr_T start_lnum;
672 synstate_T *last_valid;
673{
674 buf_T *curbuf_save;
675 win_T *curwin_save;
676 pos_T cursor_save;
677 int idx;
678 linenr_T lnum;
679 linenr_T end_lnum;
680 linenr_T break_lnum;
681 int had_sync_point;
682 stateitem_T *cur_si;
683 synpat_T *spp;
684 char_u *line;
685 int found_flags = 0;
686 int found_match_idx = 0;
687 linenr_T found_current_lnum = 0;
688 int found_current_col= 0;
689 lpos_T found_m_endpos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000690 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /*
693 * Clear any current state that might be hanging around.
694 */
695 invalidate_current_state();
696
697 /*
698 * Start at least "minlines" back. Default starting point for parsing is
699 * there.
700 * Start further back, to avoid that scrolling backwards will result in
701 * resyncing for every line. Now it resyncs only one out of N lines,
702 * where N is minlines * 1.5, or minlines * 2 if minlines is small.
703 * Watch out for overflow when minlines is MAXLNUM.
704 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200705 if (syn_block->b_syn_sync_minlines > start_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 start_lnum = 1;
707 else
708 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200709 if (syn_block->b_syn_sync_minlines == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 lnum = 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200711 else if (syn_block->b_syn_sync_minlines < 10)
712 lnum = syn_block->b_syn_sync_minlines * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200714 lnum = syn_block->b_syn_sync_minlines * 3 / 2;
715 if (syn_block->b_syn_sync_maxlines != 0
716 && lnum > syn_block->b_syn_sync_maxlines)
717 lnum = syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (lnum >= start_lnum)
719 start_lnum = 1;
720 else
721 start_lnum -= lnum;
722 }
723 current_lnum = start_lnum;
724
725 /*
726 * 1. Search backwards for the end of a C-style comment.
727 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200728 if (syn_block->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
730 /* Need to make syn_buf the current buffer for a moment, to be able to
731 * use find_start_comment(). */
732 curwin_save = curwin;
733 curwin = wp;
734 curbuf_save = curbuf;
735 curbuf = syn_buf;
736
737 /*
738 * Skip lines that end in a backslash.
739 */
740 for ( ; start_lnum > 1; --start_lnum)
741 {
742 line = ml_get(start_lnum - 1);
743 if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
744 break;
745 }
746 current_lnum = start_lnum;
747
748 /* set cursor to start of search */
749 cursor_save = wp->w_cursor;
750 wp->w_cursor.lnum = start_lnum;
751 wp->w_cursor.col = 0;
752
753 /*
754 * If the line is inside a comment, need to find the syntax item that
755 * defines the comment.
756 * Restrict the search for the end of a comment to b_syn_sync_maxlines.
757 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200758 if (find_start_comment((int)syn_block->b_syn_sync_maxlines) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200760 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
761 if (SYN_ITEMS(syn_block)[idx].sp_syn.id
762 == syn_block->b_syn_sync_id
763 && SYN_ITEMS(syn_block)[idx].sp_type == SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
765 validate_current_state();
766 if (push_current_state(idx) == OK)
767 update_si_attr(current_state.ga_len - 1);
768 break;
769 }
770 }
771
772 /* restore cursor and buffer */
773 wp->w_cursor = cursor_save;
774 curwin = curwin_save;
775 curbuf = curbuf_save;
776 }
777
778 /*
779 * 2. Search backwards for given sync patterns.
780 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200781 else if (syn_block->b_syn_sync_flags & SF_MATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200783 if (syn_block->b_syn_sync_maxlines != 0
784 && start_lnum > syn_block->b_syn_sync_maxlines)
785 break_lnum = start_lnum - syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 else
787 break_lnum = 0;
788
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000789 found_m_endpos.lnum = 0;
790 found_m_endpos.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 end_lnum = start_lnum;
792 lnum = start_lnum;
793 while (--lnum > break_lnum)
794 {
795 /* This can take a long time: break when CTRL-C pressed. */
796 line_breakcheck();
797 if (got_int)
798 {
799 invalidate_current_state();
800 current_lnum = start_lnum;
801 break;
802 }
803
804 /* Check if we have run into a valid saved state stack now. */
805 if (last_valid != NULL && lnum == last_valid->sst_lnum)
806 {
807 load_current_state(last_valid);
808 break;
809 }
810
811 /*
812 * Check if the previous line has the line-continuation pattern.
813 */
814 if (lnum > 1 && syn_match_linecont(lnum - 1))
815 continue;
816
817 /*
818 * Start with nothing on the state stack
819 */
820 validate_current_state();
821
822 for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
823 {
824 syn_start_line();
825 for (;;)
826 {
827 had_sync_point = syn_finish_line(TRUE);
828 /*
829 * When a sync point has been found, remember where, and
830 * continue to look for another one, further on in the line.
831 */
832 if (had_sync_point && current_state.ga_len)
833 {
834 cur_si = &CUR_STATE(current_state.ga_len - 1);
835 if (cur_si->si_m_endpos.lnum > start_lnum)
836 {
837 /* ignore match that goes to after where started */
838 current_lnum = end_lnum;
839 break;
840 }
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000841 if (cur_si->si_idx < 0)
842 {
843 /* Cannot happen? */
844 found_flags = 0;
845 found_match_idx = KEYWORD_IDX;
846 }
847 else
848 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200849 spp = &(SYN_ITEMS(syn_block)[cur_si->si_idx]);
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000850 found_flags = spp->sp_flags;
851 found_match_idx = spp->sp_sync_idx;
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 found_current_lnum = current_lnum;
854 found_current_col = current_col;
855 found_m_endpos = cur_si->si_m_endpos;
856 /*
857 * Continue after the match (be aware of a zero-length
858 * match).
859 */
860 if (found_m_endpos.lnum > current_lnum)
861 {
862 current_lnum = found_m_endpos.lnum;
863 current_col = found_m_endpos.col;
864 if (current_lnum >= end_lnum)
865 break;
866 }
867 else if (found_m_endpos.col > current_col)
868 current_col = found_m_endpos.col;
869 else
870 ++current_col;
871
872 /* syn_current_attr() will have skipped the check for
Bram Moolenaar81366db2005-07-24 21:16:51 +0000873 * an item that ends here, need to do that now. Be
874 * careful not to go past the NUL. */
875 prev_current_col = current_col;
876 if (syn_getcurline()[current_col] != NUL)
877 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +0000879 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 else
882 break;
883 }
884 }
885
886 /*
887 * If a sync point was encountered, break here.
888 */
889 if (found_flags)
890 {
891 /*
892 * Put the item that was specified by the sync point on the
893 * state stack. If there was no item specified, make the
894 * state stack empty.
895 */
896 clear_current_state();
897 if (found_match_idx >= 0
898 && push_current_state(found_match_idx) == OK)
899 update_si_attr(current_state.ga_len - 1);
900
901 /*
902 * When using "grouphere", continue from the sync point
903 * match, until the end of the line. Parsing starts at
904 * the next line.
905 * For "groupthere" the parsing starts at start_lnum.
906 */
907 if (found_flags & HL_SYNC_HERE)
908 {
909 if (current_state.ga_len)
910 {
911 cur_si = &CUR_STATE(current_state.ga_len - 1);
912 cur_si->si_h_startpos.lnum = found_current_lnum;
913 cur_si->si_h_startpos.col = found_current_col;
914 update_si_end(cur_si, (int)current_col, TRUE);
915 check_keepend();
916 }
917 current_col = found_m_endpos.col;
918 current_lnum = found_m_endpos.lnum;
919 (void)syn_finish_line(FALSE);
920 ++current_lnum;
921 }
922 else
923 current_lnum = start_lnum;
924
925 break;
926 }
927
928 end_lnum = lnum;
929 invalidate_current_state();
930 }
931
932 /* Ran into start of the file or exceeded maximum number of lines */
933 if (lnum <= break_lnum)
934 {
935 invalidate_current_state();
936 current_lnum = break_lnum + 1;
937 }
938 }
939
940 validate_current_state();
941}
942
943/*
944 * Return TRUE if the line-continuation pattern matches in line "lnum".
945 */
946 static int
947syn_match_linecont(lnum)
948 linenr_T lnum;
949{
950 regmmatch_T regmatch;
951
Bram Moolenaar860cae12010-06-05 23:22:07 +0200952 if (syn_block->b_syn_linecont_prog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200954 regmatch.rmm_ic = syn_block->b_syn_linecont_ic;
955 regmatch.regprog = syn_block->b_syn_linecont_prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 return syn_regexec(&regmatch, lnum, (colnr_T)0);
957 }
958 return FALSE;
959}
960
961/*
962 * Prepare the current state for the start of a line.
963 */
964 static void
965syn_start_line()
966{
967 current_finished = FALSE;
968 current_col = 0;
969
970 /*
971 * Need to update the end of a start/skip/end that continues from the
972 * previous line and regions that have "keepend".
973 */
974 if (current_state.ga_len > 0)
975 syn_update_ends(TRUE);
976
977 next_match_idx = -1;
978 ++current_line_id;
979}
980
981/*
982 * Check for items in the stack that need their end updated.
983 * When "startofline" is TRUE the last item is always updated.
984 * When "startofline" is FALSE the item with "keepend" is forcefully updated.
985 */
986 static void
987syn_update_ends(startofline)
988 int startofline;
989{
990 stateitem_T *cur_si;
991 int i;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000992 int seen_keepend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 if (startofline)
995 {
996 /* Check for a match carried over from a previous line with a
997 * contained region. The match ends as soon as the region ends. */
998 for (i = 0; i < current_state.ga_len; ++i)
999 {
1000 cur_si = &CUR_STATE(i);
1001 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02001002 && (SYN_ITEMS(syn_block)[cur_si->si_idx]).sp_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 == SPTYPE_MATCH
1004 && cur_si->si_m_endpos.lnum < current_lnum)
1005 {
1006 cur_si->si_flags |= HL_MATCHCONT;
1007 cur_si->si_m_endpos.lnum = 0;
1008 cur_si->si_m_endpos.col = 0;
1009 cur_si->si_h_endpos = cur_si->si_m_endpos;
1010 cur_si->si_ends = TRUE;
1011 }
1012 }
1013 }
1014
1015 /*
1016 * Need to update the end of a start/skip/end that continues from the
1017 * previous line. And regions that have "keepend", because they may
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00001018 * influence contained items. If we've just removed "extend"
1019 * (startofline == 0) then we should update ends of normal regions
1020 * contained inside "keepend" because "extend" could have extended
1021 * these "keepend" regions as well as contained normal regions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 * Then check for items ending in column 0.
1023 */
1024 i = current_state.ga_len - 1;
1025 if (keepend_level >= 0)
1026 for ( ; i > keepend_level; --i)
1027 if (CUR_STATE(i).si_flags & HL_EXTEND)
1028 break;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00001029
1030 seen_keepend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 for ( ; i < current_state.ga_len; ++i)
1032 {
1033 cur_si = &CUR_STATE(i);
1034 if ((cur_si->si_flags & HL_KEEPEND)
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00001035 || (seen_keepend && !startofline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 || (i == current_state.ga_len - 1 && startofline))
1037 {
1038 cur_si->si_h_startpos.col = 0; /* start highl. in col 0 */
1039 cur_si->si_h_startpos.lnum = current_lnum;
1040
1041 if (!(cur_si->si_flags & HL_MATCHCONT))
1042 update_si_end(cur_si, (int)current_col, !startofline);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00001043
1044 if (!startofline && (cur_si->si_flags & HL_KEEPEND))
1045 seen_keepend = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 }
1047 }
1048 check_keepend();
1049 check_state_ends();
1050}
1051
1052/****************************************
1053 * Handling of the state stack cache.
1054 */
1055
1056/*
1057 * EXPLANATION OF THE SYNTAX STATE STACK CACHE
1058 *
1059 * To speed up syntax highlighting, the state stack for the start of some
1060 * lines is cached. These entries can be used to start parsing at that point.
1061 *
1062 * The stack is kept in b_sst_array[] for each buffer. There is a list of
1063 * valid entries. b_sst_first points to the first one, then follow sst_next.
1064 * The entries are sorted on line number. The first entry is often for line 2
1065 * (line 1 always starts with an empty stack).
1066 * There is also a list for free entries. This construction is used to avoid
1067 * having to allocate and free memory blocks too often.
1068 *
1069 * When making changes to the buffer, this is logged in b_mod_*. When calling
1070 * update_screen() to update the display, it will call
1071 * syn_stack_apply_changes() for each displayed buffer to adjust the cached
1072 * entries. The entries which are inside the changed area are removed,
1073 * because they must be recomputed. Entries below the changed have their line
1074 * number adjusted for deleted/inserted lines, and have their sst_change_lnum
1075 * set to indicate that a check must be made if the changed lines would change
1076 * the cached entry.
1077 *
1078 * When later displaying lines, an entry is stored for each line. Displayed
1079 * lines are likely to be displayed again, in which case the state at the
1080 * start of the line is needed.
1081 * For not displayed lines, an entry is stored for every so many lines. These
1082 * entries will be used e.g., when scrolling backwards. The distance between
1083 * entries depends on the number of lines in the buffer. For small buffers
1084 * the distance is fixed at SST_DIST, for large buffers there is a fixed
1085 * number of entries SST_MAX_ENTRIES, and the distance is computed.
1086 */
1087
Bram Moolenaar860cae12010-06-05 23:22:07 +02001088 static void
1089syn_stack_free_block(block)
1090 synblock_T *block;
1091{
1092 synstate_T *p;
1093
1094 if (block->b_sst_array != NULL)
1095 {
1096 for (p = block->b_sst_first; p != NULL; p = p->sst_next)
1097 clear_syn_state(p);
1098 vim_free(block->b_sst_array);
1099 block->b_sst_array = NULL;
1100 block->b_sst_len = 0;
1101 }
1102}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103/*
1104 * Free b_sst_array[] for buffer "buf".
1105 * Used when syntax items changed to force resyncing everywhere.
1106 */
1107 void
Bram Moolenaar860cae12010-06-05 23:22:07 +02001108syn_stack_free_all(block)
1109 synblock_T *block;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 win_T *wp;
1112
Bram Moolenaar860cae12010-06-05 23:22:07 +02001113 syn_stack_free_block(block);
1114
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116#ifdef FEAT_FOLDING
1117 /* When using "syntax" fold method, must update all folds. */
1118 FOR_ALL_WINDOWS(wp)
1119 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001120 if (wp->w_s == block && foldmethodIsSyntax(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 foldUpdateAll(wp);
1122 }
1123#endif
1124}
1125
1126/*
1127 * Allocate the syntax state stack for syn_buf when needed.
1128 * If the number of entries in b_sst_array[] is much too big or a bit too
1129 * small, reallocate it.
1130 * Also used to allocate b_sst_array[] for the first time.
1131 */
1132 static void
1133syn_stack_alloc()
1134{
1135 long len;
1136 synstate_T *to, *from;
1137 synstate_T *sstp;
1138
1139 len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
1140 if (len < SST_MIN_ENTRIES)
1141 len = SST_MIN_ENTRIES;
1142 else if (len > SST_MAX_ENTRIES)
1143 len = SST_MAX_ENTRIES;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001144 if (syn_block->b_sst_len > len * 2 || syn_block->b_sst_len < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 /* Allocate 50% too much, to avoid reallocating too often. */
1147 len = syn_buf->b_ml.ml_line_count;
1148 len = (len + len / 2) / SST_DIST + Rows * 2;
1149 if (len < SST_MIN_ENTRIES)
1150 len = SST_MIN_ENTRIES;
1151 else if (len > SST_MAX_ENTRIES)
1152 len = SST_MAX_ENTRIES;
1153
Bram Moolenaar860cae12010-06-05 23:22:07 +02001154 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
1156 /* When shrinking the array, cleanup the existing stack.
1157 * Make sure that all valid entries fit in the new array. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001158 while (syn_block->b_sst_len - syn_block->b_sst_freecount + 2 > len
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && syn_stack_cleanup())
1160 ;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001161 if (len < syn_block->b_sst_len - syn_block->b_sst_freecount + 2)
1162 len = syn_block->b_sst_len - syn_block->b_sst_freecount + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164
1165 sstp = (synstate_T *)alloc_clear((unsigned)(len * sizeof(synstate_T)));
1166 if (sstp == NULL) /* out of memory! */
1167 return;
1168
1169 to = sstp - 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001170 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 /* Move the states from the old array to the new one. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001173 for (from = syn_block->b_sst_first; from != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 from = from->sst_next)
1175 {
1176 ++to;
1177 *to = *from;
1178 to->sst_next = to + 1;
1179 }
1180 }
1181 if (to != sstp - 1)
1182 {
1183 to->sst_next = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001184 syn_block->b_sst_first = sstp;
1185 syn_block->b_sst_freecount = len - (int)(to - sstp) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 else
1188 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001189 syn_block->b_sst_first = NULL;
1190 syn_block->b_sst_freecount = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192
1193 /* Create the list of free entries. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001194 syn_block->b_sst_firstfree = to + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 while (++to < sstp + len)
1196 to->sst_next = to + 1;
1197 (sstp + len - 1)->sst_next = NULL;
1198
Bram Moolenaar860cae12010-06-05 23:22:07 +02001199 vim_free(syn_block->b_sst_array);
1200 syn_block->b_sst_array = sstp;
1201 syn_block->b_sst_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203}
1204
1205/*
1206 * Check for changes in a buffer to affect stored syntax states. Uses the
1207 * b_mod_* fields.
1208 * Called from update_screen(), before screen is being updated, once for each
1209 * displayed buffer.
1210 */
1211 void
1212syn_stack_apply_changes(buf)
1213 buf_T *buf;
1214{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001215 win_T *wp;
1216
1217 syn_stack_apply_changes_block(&buf->b_s, buf);
1218
1219 FOR_ALL_WINDOWS(wp)
1220 {
1221 if ((wp->w_buffer == buf) && (wp->w_s != &buf->b_s))
1222 syn_stack_apply_changes_block(wp->w_s, buf);
1223 }
1224}
1225
1226 static void
1227syn_stack_apply_changes_block(block, buf)
1228 synblock_T *block;
1229 buf_T *buf;
1230{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 synstate_T *p, *prev, *np;
1232 linenr_T n;
1233
Bram Moolenaar860cae12010-06-05 23:22:07 +02001234 if (block->b_sst_array == NULL) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 return;
1236
1237 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001238 for (p = block->b_sst_first; p != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001240 if (p->sst_lnum + block->b_syn_sync_linebreaks > buf->b_mod_top)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
1242 n = p->sst_lnum + buf->b_mod_xlines;
1243 if (n <= buf->b_mod_bot)
1244 {
1245 /* this state is inside the changed area, remove it */
1246 np = p->sst_next;
1247 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001248 block->b_sst_first = np;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 else
1250 prev->sst_next = np;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 syn_stack_free_entry(block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 p = np;
1253 continue;
1254 }
1255 /* This state is below the changed area. Remember the line
1256 * that needs to be parsed before this entry can be made valid
1257 * again. */
1258 if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
1259 {
1260 if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
1261 p->sst_change_lnum += buf->b_mod_xlines;
1262 else
1263 p->sst_change_lnum = buf->b_mod_top;
1264 }
1265 if (p->sst_change_lnum == 0
1266 || p->sst_change_lnum < buf->b_mod_bot)
1267 p->sst_change_lnum = buf->b_mod_bot;
1268
1269 p->sst_lnum = n;
1270 }
1271 prev = p;
1272 p = p->sst_next;
1273 }
1274}
1275
1276/*
1277 * Reduce the number of entries in the state stack for syn_buf.
1278 * Returns TRUE if at least one entry was freed.
1279 */
1280 static int
1281syn_stack_cleanup()
1282{
1283 synstate_T *p, *prev;
1284 disptick_T tick;
1285 int above;
1286 int dist;
1287 int retval = FALSE;
1288
Bram Moolenaar860cae12010-06-05 23:22:07 +02001289 if (syn_block->b_sst_array == NULL || syn_block->b_sst_first == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 return retval;
1291
1292 /* Compute normal distance between non-displayed entries. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001293 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001294 dist = 999999;
1295 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02001296 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298 /*
Bram Moolenaarf5b63862009-12-16 17:13:44 +00001299 * Go through the list to find the "tick" for the oldest entry that can
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 * be removed. Set "above" when the "tick" for the oldest entry is above
1301 * "b_sst_lasttick" (the display tick wraps around).
1302 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001303 tick = syn_block->b_sst_lasttick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 above = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001305 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1307 {
1308 if (prev->sst_lnum + dist > p->sst_lnum)
1309 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001310 if (p->sst_tick > syn_block->b_sst_lasttick)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
1312 if (!above || p->sst_tick < tick)
1313 tick = p->sst_tick;
1314 above = TRUE;
1315 }
1316 else if (!above && p->sst_tick < tick)
1317 tick = p->sst_tick;
1318 }
1319 }
1320
1321 /*
1322 * Go through the list to make the entries for the oldest tick at an
1323 * interval of several lines.
1324 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001325 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1327 {
1328 if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
1329 {
1330 /* Move this entry from used list to free list */
1331 prev->sst_next = p->sst_next;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001332 syn_stack_free_entry(syn_block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 p = prev;
1334 retval = TRUE;
1335 }
1336 }
1337 return retval;
1338}
1339
1340/*
1341 * Free the allocated memory for a syn_state item.
1342 * Move the entry into the free list.
1343 */
1344 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02001345syn_stack_free_entry(block, p)
1346 synblock_T *block;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 synstate_T *p;
1348{
1349 clear_syn_state(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001350 p->sst_next = block->b_sst_firstfree;
1351 block->b_sst_firstfree = p;
1352 ++block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353}
1354
1355/*
1356 * Find an entry in the list of state stacks at or before "lnum".
1357 * Returns NULL when there is no entry or the first entry is after "lnum".
1358 */
1359 static synstate_T *
1360syn_stack_find_entry(lnum)
1361 linenr_T lnum;
1362{
1363 synstate_T *p, *prev;
1364
1365 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001366 for (p = syn_block->b_sst_first; p != NULL; prev = p, p = p->sst_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
1368 if (p->sst_lnum == lnum)
1369 return p;
1370 if (p->sst_lnum > lnum)
1371 break;
1372 }
1373 return prev;
1374}
1375
1376/*
1377 * Try saving the current state in b_sst_array[].
1378 * The current state must be valid for the start of the current_lnum line!
1379 */
1380 static synstate_T *
Bram Moolenaardbe31752008-01-13 16:40:19 +00001381store_current_state()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 int i;
1384 synstate_T *p;
1385 bufstate_T *bp;
1386 stateitem_T *cur_si;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001387 synstate_T *sp = syn_stack_find_entry(current_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 /*
1390 * If the current state contains a start or end pattern that continues
1391 * from the previous line, we can't use it. Don't store it then.
1392 */
1393 for (i = current_state.ga_len - 1; i >= 0; --i)
1394 {
1395 cur_si = &CUR_STATE(i);
1396 if (cur_si->si_h_startpos.lnum >= current_lnum
1397 || cur_si->si_m_endpos.lnum >= current_lnum
1398 || cur_si->si_h_endpos.lnum >= current_lnum
1399 || (cur_si->si_end_idx
1400 && cur_si->si_eoe_pos.lnum >= current_lnum))
1401 break;
1402 }
1403 if (i >= 0)
1404 {
1405 if (sp != NULL)
1406 {
1407 /* find "sp" in the list and remove it */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001408 if (syn_block->b_sst_first == sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /* it's the first entry */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001410 syn_block->b_sst_first = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 else
1412 {
1413 /* find the entry just before this one to adjust sst_next */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001414 for (p = syn_block->b_sst_first; p != NULL; p = p->sst_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (p->sst_next == sp)
1416 break;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001417 if (p != NULL) /* just in case */
1418 p->sst_next = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001420 syn_stack_free_entry(syn_block, sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 sp = NULL;
1422 }
1423 }
1424 else if (sp == NULL || sp->sst_lnum != current_lnum)
1425 {
1426 /*
1427 * Add a new entry
1428 */
1429 /* If no free items, cleanup the array first. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001430 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
1432 (void)syn_stack_cleanup();
1433 /* "sp" may have been moved to the freelist now */
1434 sp = syn_stack_find_entry(current_lnum);
1435 }
1436 /* Still no free items? Must be a strange problem... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001437 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 sp = NULL;
1439 else
1440 {
1441 /* Take the first item from the free list and put it in the used
1442 * list, after *sp */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001443 p = syn_block->b_sst_firstfree;
1444 syn_block->b_sst_firstfree = p->sst_next;
1445 --syn_block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 if (sp == NULL)
1447 {
1448 /* Insert in front of the list */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001449 p->sst_next = syn_block->b_sst_first;
1450 syn_block->b_sst_first = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 else
1453 {
1454 /* insert in list after *sp */
1455 p->sst_next = sp->sst_next;
1456 sp->sst_next = p;
1457 }
1458 sp = p;
1459 sp->sst_stacksize = 0;
1460 sp->sst_lnum = current_lnum;
1461 }
1462 }
1463 if (sp != NULL)
1464 {
1465 /* When overwriting an existing state stack, clear it first */
1466 clear_syn_state(sp);
1467 sp->sst_stacksize = current_state.ga_len;
1468 if (current_state.ga_len > SST_FIX_STATES)
1469 {
1470 /* Need to clear it, might be something remaining from when the
1471 * length was less than SST_FIX_STATES. */
1472 ga_init2(&sp->sst_union.sst_ga, (int)sizeof(bufstate_T), 1);
1473 if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
1474 sp->sst_stacksize = 0;
1475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 sp->sst_union.sst_ga.ga_len = current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1478 }
1479 else
1480 bp = sp->sst_union.sst_stack;
1481 for (i = 0; i < sp->sst_stacksize; ++i)
1482 {
1483 bp[i].bs_idx = CUR_STATE(i).si_idx;
1484 bp[i].bs_flags = CUR_STATE(i).si_flags;
1485 bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
1486 }
1487 sp->sst_next_flags = current_next_flags;
1488 sp->sst_next_list = current_next_list;
1489 sp->sst_tick = display_tick;
1490 sp->sst_change_lnum = 0;
1491 }
1492 current_state_stored = TRUE;
1493 return sp;
1494}
1495
1496/*
1497 * Copy a state stack from "from" in b_sst_array[] to current_state;
1498 */
1499 static void
1500load_current_state(from)
1501 synstate_T *from;
1502{
1503 int i;
1504 bufstate_T *bp;
1505
1506 clear_current_state();
1507 validate_current_state();
1508 keepend_level = -1;
1509 if (from->sst_stacksize
1510 && ga_grow(&current_state, from->sst_stacksize) != FAIL)
1511 {
1512 if (from->sst_stacksize > SST_FIX_STATES)
1513 bp = SYN_STATE_P(&(from->sst_union.sst_ga));
1514 else
1515 bp = from->sst_union.sst_stack;
1516 for (i = 0; i < from->sst_stacksize; ++i)
1517 {
1518 CUR_STATE(i).si_idx = bp[i].bs_idx;
1519 CUR_STATE(i).si_flags = bp[i].bs_flags;
1520 CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
1521 if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
1522 keepend_level = i;
1523 CUR_STATE(i).si_ends = FALSE;
1524 CUR_STATE(i).si_m_lnum = 0;
1525 if (CUR_STATE(i).si_idx >= 0)
1526 CUR_STATE(i).si_next_list =
Bram Moolenaar860cae12010-06-05 23:22:07 +02001527 (SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 else
1529 CUR_STATE(i).si_next_list = NULL;
1530 update_si_attr(i);
1531 }
1532 current_state.ga_len = from->sst_stacksize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 current_next_list = from->sst_next_list;
1535 current_next_flags = from->sst_next_flags;
1536 current_lnum = from->sst_lnum;
1537}
1538
1539/*
1540 * Compare saved state stack "*sp" with the current state.
1541 * Return TRUE when they are equal.
1542 */
1543 static int
1544syn_stack_equal(sp)
1545 synstate_T *sp;
1546{
1547 int i, j;
1548 bufstate_T *bp;
1549 reg_extmatch_T *six, *bsx;
1550
1551 /* First a quick check if the stacks have the same size end nextlist. */
1552 if (sp->sst_stacksize == current_state.ga_len
1553 && sp->sst_next_list == current_next_list)
1554 {
1555 /* Need to compare all states on both stacks. */
1556 if (sp->sst_stacksize > SST_FIX_STATES)
1557 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1558 else
1559 bp = sp->sst_union.sst_stack;
1560
1561 for (i = current_state.ga_len; --i >= 0; )
1562 {
1563 /* If the item has another index the state is different. */
1564 if (bp[i].bs_idx != CUR_STATE(i).si_idx)
1565 break;
1566 if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
1567 {
1568 /* When the extmatch pointers are different, the strings in
1569 * them can still be the same. Check if the extmatch
1570 * references are equal. */
1571 bsx = bp[i].bs_extmatch;
1572 six = CUR_STATE(i).si_extmatch;
1573 /* If one of the extmatch pointers is NULL the states are
1574 * different. */
1575 if (bsx == NULL || six == NULL)
1576 break;
1577 for (j = 0; j < NSUBEXP; ++j)
1578 {
1579 /* Check each referenced match string. They must all be
1580 * equal. */
1581 if (bsx->matches[j] != six->matches[j])
1582 {
1583 /* If the pointer is different it can still be the
1584 * same text. Compare the strings, ignore case when
1585 * the start item has the sp_ic flag set. */
1586 if (bsx->matches[j] == NULL
1587 || six->matches[j] == NULL)
1588 break;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001589 if ((SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 ? MB_STRICMP(bsx->matches[j],
1591 six->matches[j]) != 0
1592 : STRCMP(bsx->matches[j], six->matches[j]) != 0)
1593 break;
1594 }
1595 }
1596 if (j != NSUBEXP)
1597 break;
1598 }
1599 }
1600 if (i < 0)
1601 return TRUE;
1602 }
1603 return FALSE;
1604}
1605
1606/*
1607 * We stop parsing syntax above line "lnum". If the stored state at or below
1608 * this line depended on a change before it, it now depends on the line below
1609 * the last parsed line.
1610 * The window looks like this:
1611 * line which changed
1612 * displayed line
1613 * displayed line
1614 * lnum -> line below window
1615 */
1616 void
1617syntax_end_parsing(lnum)
1618 linenr_T lnum;
1619{
1620 synstate_T *sp;
1621
1622 sp = syn_stack_find_entry(lnum);
1623 if (sp != NULL && sp->sst_lnum < lnum)
1624 sp = sp->sst_next;
1625
1626 if (sp != NULL && sp->sst_change_lnum != 0)
1627 sp->sst_change_lnum = lnum;
1628}
1629
1630/*
1631 * End of handling of the state stack.
1632 ****************************************/
1633
1634 static void
1635invalidate_current_state()
1636{
1637 clear_current_state();
1638 current_state.ga_itemsize = 0; /* mark current_state invalid */
1639 current_next_list = NULL;
1640 keepend_level = -1;
1641}
1642
1643 static void
1644validate_current_state()
1645{
1646 current_state.ga_itemsize = sizeof(stateitem_T);
1647 current_state.ga_growsize = 3;
1648}
1649
1650/*
1651 * Return TRUE if the syntax at start of lnum changed since last time.
1652 * This will only be called just after get_syntax_attr() for the previous
1653 * line, to check if the next line needs to be redrawn too.
1654 */
1655 int
1656syntax_check_changed(lnum)
1657 linenr_T lnum;
1658{
1659 int retval = TRUE;
1660 synstate_T *sp;
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 /*
1663 * Check the state stack when:
1664 * - lnum is just below the previously syntaxed line.
1665 * - lnum is not before the lines with saved states.
1666 * - lnum is not past the lines with saved states.
1667 * - lnum is at or before the last changed line.
1668 */
1669 if (VALID_STATE(&current_state) && lnum == current_lnum + 1)
1670 {
1671 sp = syn_stack_find_entry(lnum);
1672 if (sp != NULL && sp->sst_lnum == lnum)
1673 {
1674 /*
1675 * finish the previous line (needed when not all of the line was
1676 * drawn)
1677 */
1678 (void)syn_finish_line(FALSE);
1679
1680 /*
1681 * Compare the current state with the previously saved state of
1682 * the line.
1683 */
1684 if (syn_stack_equal(sp))
1685 retval = FALSE;
1686
1687 /*
1688 * Store the current state in b_sst_array[] for later use.
1689 */
1690 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001691 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693 }
1694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 return retval;
1696}
1697
1698/*
1699 * Finish the current line.
1700 * This doesn't return any attributes, it only gets the state at the end of
1701 * the line. It can start anywhere in the line, as long as the current state
1702 * is valid.
1703 */
1704 static int
1705syn_finish_line(syncing)
1706 int syncing; /* called for syncing */
1707{
1708 stateitem_T *cur_si;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001709 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 if (!current_finished)
1712 {
1713 while (!current_finished)
1714 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001715 (void)syn_current_attr(syncing, FALSE, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 /*
1717 * When syncing, and found some item, need to check the item.
1718 */
1719 if (syncing && current_state.ga_len)
1720 {
1721 /*
1722 * Check for match with sync item.
1723 */
1724 cur_si = &CUR_STATE(current_state.ga_len - 1);
1725 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02001726 && (SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 & (HL_SYNC_HERE|HL_SYNC_THERE)))
1728 return TRUE;
1729
1730 /* syn_current_attr() will have skipped the check for an item
Bram Moolenaar81366db2005-07-24 21:16:51 +00001731 * that ends here, need to do that now. Be careful not to go
1732 * past the NUL. */
1733 prev_current_col = current_col;
1734 if (syn_getcurline()[current_col] != NUL)
1735 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +00001737 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
1739 ++current_col;
1740 }
1741 }
1742 return FALSE;
1743}
1744
1745/*
1746 * Return highlight attributes for next character.
1747 * Must first call syntax_start() once for the line.
1748 * "col" is normally 0 for the first use in a line, and increments by one each
1749 * time. It's allowed to skip characters and to stop before the end of the
1750 * line. But only a "col" after a previously used column is allowed.
Bram Moolenaar217ad922005-03-20 22:37:15 +00001751 * When "can_spell" is not NULL set it to TRUE when spell-checking should be
1752 * done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 */
1754 int
Bram Moolenaar860cae12010-06-05 23:22:07 +02001755get_syntax_attr(col, p_flags, can_spell, keep_state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 colnr_T col;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001757 int *p_flags UNUSED;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001758 int *can_spell;
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001759 int keep_state; /* keep state of char at "col" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int attr = 0;
1762
Bram Moolenaar349955a2007-08-14 21:07:36 +00001763 if (can_spell != NULL)
1764 /* Default: Only do spelling when there is no @Spell cluster or when
1765 * ":syn spell toplevel" was used. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001766 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
1767 ? (syn_block->b_spell_cluster_id == 0)
1768 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar349955a2007-08-14 21:07:36 +00001769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 /* check for out of memory situation */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001771 if (syn_block->b_sst_array == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 return 0;
1773
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001774 /* After 'synmaxcol' the attribute is always zero. */
Bram Moolenaar84fb85a2005-07-20 22:02:14 +00001775 if (syn_buf->b_p_smc > 0 && col >= (colnr_T)syn_buf->b_p_smc)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001776 {
1777 clear_current_state();
1778#ifdef FEAT_EVAL
1779 current_id = 0;
1780 current_trans_id = 0;
1781#endif
1782 return 0;
1783 }
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /* Make sure current_state is valid */
1786 if (INVALID_STATE(&current_state))
1787 validate_current_state();
1788
1789 /*
1790 * Skip from the current column to "col", get the attributes for "col".
1791 */
1792 while (current_col <= col)
1793 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001794 attr = syn_current_attr(FALSE, TRUE, can_spell,
1795 current_col == col ? keep_state : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 ++current_col;
1797 }
1798
Bram Moolenaar860cae12010-06-05 23:22:07 +02001799#ifdef FEAT_CONCEAL
1800 if (p_flags != NULL)
1801 *p_flags = current_flags;
1802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 return attr;
1804}
1805
1806/*
1807 * Get syntax attributes for current_lnum, current_col.
1808 */
1809 static int
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001810syn_current_attr(syncing, displaying, can_spell, keep_state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 int syncing; /* When 1: called for syncing */
1812 int displaying; /* result will be displayed */
Bram Moolenaar217ad922005-03-20 22:37:15 +00001813 int *can_spell; /* return: do spell checking */
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001814 int keep_state; /* keep syntax stack afterwards */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
1816 int syn_id;
1817 lpos_T endpos; /* was: char_u *endp; */
1818 lpos_T hl_startpos; /* was: int hl_startcol; */
1819 lpos_T hl_endpos;
1820 lpos_T eos_pos; /* end-of-start match (start region) */
1821 lpos_T eoe_pos; /* end-of-end pattern */
1822 int end_idx; /* group ID for end pattern */
1823 int idx;
1824 synpat_T *spp;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001825 stateitem_T *cur_si, *sip = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 int startcol;
1827 int endcol;
1828 long flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001829 int cchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 short *next_list;
1831 int found_match; /* found usable match */
1832 static int try_next_column = FALSE; /* must try in next col */
1833 int do_keywords;
1834 regmmatch_T regmatch;
1835 lpos_T pos;
1836 int lc_col;
1837 reg_extmatch_T *cur_extmatch = NULL;
1838 char_u *line; /* current line. NOTE: becomes invalid after
1839 looking for a pattern match! */
1840
1841 /* variables for zero-width matches that have a "nextgroup" argument */
1842 int keep_next_list;
1843 int zero_width_next_list = FALSE;
1844 garray_T zero_width_next_ga;
1845
1846 /*
1847 * No character, no attributes! Past end of line?
1848 * Do try matching with an empty line (could be the start of a region).
1849 */
1850 line = syn_getcurline();
1851 if (line[current_col] == NUL && current_col != 0)
1852 {
1853 /*
1854 * If we found a match after the last column, use it.
1855 */
1856 if (next_match_idx >= 0 && next_match_col >= (int)current_col
1857 && next_match_col != MAXCOL)
1858 (void)push_next_match(NULL);
1859
1860 current_finished = TRUE;
1861 current_state_stored = FALSE;
1862 return 0;
1863 }
1864
1865 /* if the current or next character is NUL, we will finish the line now */
1866 if (line[current_col] == NUL || line[current_col + 1] == NUL)
1867 {
1868 current_finished = TRUE;
1869 current_state_stored = FALSE;
1870 }
1871
1872 /*
1873 * When in the previous column there was a match but it could not be used
1874 * (empty match or already matched in this column) need to try again in
1875 * the next column.
1876 */
1877 if (try_next_column)
1878 {
1879 next_match_idx = -1;
1880 try_next_column = FALSE;
1881 }
1882
1883 /* Only check for keywords when not syncing and there are some. */
1884 do_keywords = !syncing
Bram Moolenaar860cae12010-06-05 23:22:07 +02001885 && (syn_block->b_keywtab.ht_used > 0
1886 || syn_block->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888 /* Init the list of zero-width matches with a nextlist. This is used to
1889 * avoid matching the same item in the same position twice. */
1890 ga_init2(&zero_width_next_ga, (int)sizeof(int), 10);
1891
1892 /*
1893 * Repeat matching keywords and patterns, to find contained items at the
1894 * same column. This stops when there are no extra matches at the current
1895 * column.
1896 */
1897 do
1898 {
1899 found_match = FALSE;
1900 keep_next_list = FALSE;
1901 syn_id = 0;
1902
1903 /*
1904 * 1. Check for a current state.
1905 * Only when there is no current state, or if the current state may
1906 * contain other things, we need to check for keywords and patterns.
1907 * Always need to check for contained items if some item has the
1908 * "containedin" argument (takes extra time!).
1909 */
1910 if (current_state.ga_len)
1911 cur_si = &CUR_STATE(current_state.ga_len - 1);
1912 else
1913 cur_si = NULL;
1914
Bram Moolenaar860cae12010-06-05 23:22:07 +02001915 if (syn_block->b_syn_containedin || cur_si == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 || cur_si->si_cont_list != NULL)
1917 {
1918 /*
1919 * 2. Check for keywords, if on a keyword char after a non-keyword
1920 * char. Don't do this when syncing.
1921 */
1922 if (do_keywords)
1923 {
1924 line = syn_getcurline();
1925 if (vim_iswordc_buf(line + current_col, syn_buf)
1926 && (current_col == 0
1927 || !vim_iswordc_buf(line + current_col - 1
1928#ifdef FEAT_MBYTE
1929 - (has_mbyte
1930 ? (*mb_head_off)(line, line + current_col - 1)
1931 : 0)
1932#endif
1933 , syn_buf)))
1934 {
1935 syn_id = check_keyword_id(line, (int)current_col,
Bram Moolenaar860cae12010-06-05 23:22:07 +02001936 &endcol, &flags, &next_list, cur_si,
1937 &cchar);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001938 if (syn_id != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
1940 if (push_current_state(KEYWORD_IDX) == OK)
1941 {
1942 cur_si = &CUR_STATE(current_state.ga_len - 1);
1943 cur_si->si_m_startcol = current_col;
1944 cur_si->si_h_startpos.lnum = current_lnum;
1945 cur_si->si_h_startpos.col = 0; /* starts right away */
1946 cur_si->si_m_endpos.lnum = current_lnum;
1947 cur_si->si_m_endpos.col = endcol;
1948 cur_si->si_h_endpos.lnum = current_lnum;
1949 cur_si->si_h_endpos.col = endcol;
1950 cur_si->si_ends = TRUE;
1951 cur_si->si_end_idx = 0;
1952 cur_si->si_flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001953#ifdef FEAT_CONCEAL
1954 cur_si->si_char = cchar;
1955 if (current_state.ga_len > 1)
1956 cur_si->si_flags |=
1957 CUR_STATE(current_state.ga_len - 2).si_flags
1958 & HL_CONCEAL;
1959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 cur_si->si_id = syn_id;
1961 cur_si->si_trans_id = syn_id;
1962 if (flags & HL_TRANSP)
1963 {
1964 if (current_state.ga_len < 2)
1965 {
1966 cur_si->si_attr = 0;
1967 cur_si->si_trans_id = 0;
1968 }
1969 else
1970 {
1971 cur_si->si_attr = CUR_STATE(
1972 current_state.ga_len - 2).si_attr;
1973 cur_si->si_trans_id = CUR_STATE(
1974 current_state.ga_len - 2).si_trans_id;
1975 }
1976 }
1977 else
1978 cur_si->si_attr = syn_id2attr(syn_id);
1979 cur_si->si_cont_list = NULL;
1980 cur_si->si_next_list = next_list;
1981 check_keepend();
1982 }
1983 else
1984 vim_free(next_list);
1985 }
1986 }
1987 }
1988
1989 /*
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001990 * 3. Check for patterns (only if no keyword found).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001992 if (syn_id == 0 && syn_block->b_syn_patterns.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 /*
1995 * If we didn't check for a match yet, or we are past it, check
1996 * for any match with a pattern.
1997 */
1998 if (next_match_idx < 0 || next_match_col < (int)current_col)
1999 {
2000 /*
2001 * Check all relevant patterns for a match at this
2002 * position. This is complicated, because matching with a
2003 * pattern takes quite a bit of time, thus we want to
2004 * avoid doing it when it's not needed.
2005 */
2006 next_match_idx = 0; /* no match in this line yet */
2007 next_match_col = MAXCOL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002008 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002010 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if ( spp->sp_syncing == syncing
2012 && (displaying || !(spp->sp_flags & HL_DISPLAY))
2013 && (spp->sp_type == SPTYPE_MATCH
2014 || spp->sp_type == SPTYPE_START)
2015 && (current_next_list != NULL
2016 ? in_id_list(NULL, current_next_list,
2017 &spp->sp_syn, 0)
2018 : (cur_si == NULL
2019 ? !(spp->sp_flags & HL_CONTAINED)
2020 : in_id_list(cur_si,
2021 cur_si->si_cont_list, &spp->sp_syn,
2022 spp->sp_flags & HL_CONTAINED))))
2023 {
2024 /* If we already tried matching in this line, and
2025 * there isn't a match before next_match_col, skip
2026 * this item. */
2027 if (spp->sp_line_id == current_line_id
2028 && spp->sp_startcol >= next_match_col)
2029 continue;
2030 spp->sp_line_id = current_line_id;
2031
2032 lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
2033 if (lc_col < 0)
2034 lc_col = 0;
2035
2036 regmatch.rmm_ic = spp->sp_ic;
2037 regmatch.regprog = spp->sp_prog;
2038 if (!syn_regexec(&regmatch, current_lnum,
2039 (colnr_T)lc_col))
2040 {
2041 /* no match in this line, try another one */
2042 spp->sp_startcol = MAXCOL;
2043 continue;
2044 }
2045
2046 /*
2047 * Compute the first column of the match.
2048 */
2049 syn_add_start_off(&pos, &regmatch,
2050 spp, SPO_MS_OFF, -1);
2051 if (pos.lnum > current_lnum)
2052 {
2053 /* must have used end of match in a next line,
2054 * we can't handle that */
2055 spp->sp_startcol = MAXCOL;
2056 continue;
2057 }
2058 startcol = pos.col;
2059
2060 /* remember the next column where this pattern
2061 * matches in the current line */
2062 spp->sp_startcol = startcol;
2063
2064 /*
2065 * If a previously found match starts at a lower
2066 * column number, don't use this one.
2067 */
2068 if (startcol >= next_match_col)
2069 continue;
2070
2071 /*
2072 * If we matched this pattern at this position
2073 * before, skip it. Must retry in the next
2074 * column, because it may match from there.
2075 */
2076 if (did_match_already(idx, &zero_width_next_ga))
2077 {
2078 try_next_column = TRUE;
2079 continue;
2080 }
2081
2082 endpos.lnum = regmatch.endpos[0].lnum;
2083 endpos.col = regmatch.endpos[0].col;
2084
2085 /* Compute the highlight start. */
2086 syn_add_start_off(&hl_startpos, &regmatch,
2087 spp, SPO_HS_OFF, -1);
2088
2089 /* Compute the region start. */
2090 /* Default is to use the end of the match. */
2091 syn_add_end_off(&eos_pos, &regmatch,
2092 spp, SPO_RS_OFF, 0);
2093
2094 /*
2095 * Grab the external submatches before they get
2096 * overwritten. Reference count doesn't change.
2097 */
2098 unref_extmatch(cur_extmatch);
2099 cur_extmatch = re_extmatch_out;
2100 re_extmatch_out = NULL;
2101
2102 flags = 0;
2103 eoe_pos.lnum = 0; /* avoid warning */
2104 eoe_pos.col = 0;
2105 end_idx = 0;
2106 hl_endpos.lnum = 0;
2107
2108 /*
2109 * For a "oneline" the end must be found in the
2110 * same line too. Search for it after the end of
2111 * the match with the start pattern. Set the
2112 * resulting end positions at the same time.
2113 */
2114 if (spp->sp_type == SPTYPE_START
2115 && (spp->sp_flags & HL_ONELINE))
2116 {
2117 lpos_T startpos;
2118
2119 startpos = endpos;
2120 find_endpos(idx, &startpos, &endpos, &hl_endpos,
2121 &flags, &eoe_pos, &end_idx, cur_extmatch);
2122 if (endpos.lnum == 0)
2123 continue; /* not found */
2124 }
2125
2126 /*
2127 * For a "match" the size must be > 0 after the
2128 * end offset needs has been added. Except when
2129 * syncing.
2130 */
2131 else if (spp->sp_type == SPTYPE_MATCH)
2132 {
2133 syn_add_end_off(&hl_endpos, &regmatch, spp,
2134 SPO_HE_OFF, 0);
2135 syn_add_end_off(&endpos, &regmatch, spp,
2136 SPO_ME_OFF, 0);
2137 if (endpos.lnum == current_lnum
2138 && (int)endpos.col + syncing < startcol)
2139 {
2140 /*
2141 * If an empty string is matched, may need
2142 * to try matching again at next column.
2143 */
2144 if (regmatch.startpos[0].col
2145 == regmatch.endpos[0].col)
2146 try_next_column = TRUE;
2147 continue;
2148 }
2149 }
2150
2151 /*
2152 * keep the best match so far in next_match_*
2153 */
2154 /* Highlighting must start after startpos and end
2155 * before endpos. */
2156 if (hl_startpos.lnum == current_lnum
2157 && (int)hl_startpos.col < startcol)
2158 hl_startpos.col = startcol;
2159 limit_pos_zero(&hl_endpos, &endpos);
2160
2161 next_match_idx = idx;
2162 next_match_col = startcol;
2163 next_match_m_endpos = endpos;
2164 next_match_h_endpos = hl_endpos;
2165 next_match_h_startpos = hl_startpos;
2166 next_match_flags = flags;
2167 next_match_eos_pos = eos_pos;
2168 next_match_eoe_pos = eoe_pos;
2169 next_match_end_idx = end_idx;
2170 unref_extmatch(next_match_extmatch);
2171 next_match_extmatch = cur_extmatch;
2172 cur_extmatch = NULL;
2173 }
2174 }
2175 }
2176
2177 /*
2178 * If we found a match at the current column, use it.
2179 */
2180 if (next_match_idx >= 0 && next_match_col == (int)current_col)
2181 {
2182 synpat_T *lspp;
2183
2184 /* When a zero-width item matched which has a nextgroup,
2185 * don't push the item but set nextgroup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002186 lspp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (next_match_m_endpos.lnum == current_lnum
2188 && next_match_m_endpos.col == current_col
2189 && lspp->sp_next_list != NULL)
2190 {
2191 current_next_list = lspp->sp_next_list;
2192 current_next_flags = lspp->sp_flags;
2193 keep_next_list = TRUE;
2194 zero_width_next_list = TRUE;
2195
2196 /* Add the index to a list, so that we can check
2197 * later that we don't match it again (and cause an
2198 * endless loop). */
2199 if (ga_grow(&zero_width_next_ga, 1) == OK)
2200 {
2201 ((int *)(zero_width_next_ga.ga_data))
2202 [zero_width_next_ga.ga_len++] = next_match_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 next_match_idx = -1;
2205 }
2206 else
2207 cur_si = push_next_match(cur_si);
2208 found_match = TRUE;
2209 }
2210 }
2211 }
2212
2213 /*
2214 * Handle searching for nextgroup match.
2215 */
2216 if (current_next_list != NULL && !keep_next_list)
2217 {
2218 /*
2219 * If a nextgroup was not found, continue looking for one if:
2220 * - this is an empty line and the "skipempty" option was given
2221 * - we are on white space and the "skipwhite" option was given
2222 */
2223 if (!found_match)
2224 {
2225 line = syn_getcurline();
2226 if (((current_next_flags & HL_SKIPWHITE)
2227 && vim_iswhite(line[current_col]))
2228 || ((current_next_flags & HL_SKIPEMPTY)
2229 && *line == NUL))
2230 break;
2231 }
2232
2233 /*
2234 * If a nextgroup was found: Use it, and continue looking for
2235 * contained matches.
2236 * If a nextgroup was not found: Continue looking for a normal
2237 * match.
2238 * When did set current_next_list for a zero-width item and no
2239 * match was found don't loop (would get stuck).
2240 */
2241 current_next_list = NULL;
2242 next_match_idx = -1;
2243 if (!zero_width_next_list)
2244 found_match = TRUE;
2245 }
2246
2247 } while (found_match);
2248
2249 /*
2250 * Use attributes from the current state, if within its highlighting.
2251 * If not, use attributes from the current-but-one state, etc.
2252 */
2253 current_attr = 0;
2254#ifdef FEAT_EVAL
2255 current_id = 0;
2256 current_trans_id = 0;
2257#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002258#ifdef FEAT_CONCEAL
2259 current_flags = 0;
2260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (cur_si != NULL)
2262 {
Bram Moolenaar217ad922005-03-20 22:37:15 +00002263#ifndef FEAT_EVAL
2264 int current_trans_id = 0;
2265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 for (idx = current_state.ga_len - 1; idx >= 0; --idx)
2267 {
2268 sip = &CUR_STATE(idx);
2269 if ((current_lnum > sip->si_h_startpos.lnum
2270 || (current_lnum == sip->si_h_startpos.lnum
2271 && current_col >= sip->si_h_startpos.col))
2272 && (sip->si_h_endpos.lnum == 0
2273 || current_lnum < sip->si_h_endpos.lnum
2274 || (current_lnum == sip->si_h_endpos.lnum
2275 && current_col < sip->si_h_endpos.col)))
2276 {
2277 current_attr = sip->si_attr;
2278#ifdef FEAT_EVAL
2279 current_id = sip->si_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002281 current_trans_id = sip->si_trans_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002282#ifdef FEAT_CONCEAL
2283 current_flags = sip->si_flags;
2284 current_sub_char = sip->si_char;
2285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 break;
2287 }
2288 }
2289
Bram Moolenaar217ad922005-03-20 22:37:15 +00002290 if (can_spell != NULL)
2291 {
2292 struct sp_syn sps;
2293
2294 /*
2295 * set "can_spell" to TRUE if spell checking is supposed to be
2296 * done in the current item.
2297 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002298 if (syn_block->b_spell_cluster_id == 0)
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002299 {
Bram Moolenaar3638c682005-06-08 22:05:14 +00002300 /* There is no @Spell cluster: Do spelling for items without
2301 * @NoSpell cluster. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002302 if (syn_block->b_nospell_cluster_id == 0
2303 || current_trans_id == 0)
2304 *can_spell = (syn_block->b_syn_spell != SYNSPL_NOTOP);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002305 else
2306 {
2307 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002308 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002309 sps.cont_in_list = NULL;
2310 *can_spell = !in_id_list(sip, sip->si_cont_list, &sps, 0);
2311 }
2312 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002313 else
2314 {
Bram Moolenaar3638c682005-06-08 22:05:14 +00002315 /* The @Spell cluster is defined: Do spelling in items with
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002316 * the @Spell cluster. But not when @NoSpell is also there.
2317 * At the toplevel only spell check when ":syn spell toplevel"
2318 * was used. */
Bram Moolenaar3638c682005-06-08 22:05:14 +00002319 if (current_trans_id == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002320 *can_spell = (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar3638c682005-06-08 22:05:14 +00002321 else
2322 {
2323 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002324 sps.id = syn_block->b_spell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002325 sps.cont_in_list = NULL;
2326 *can_spell = in_id_list(sip, sip->si_cont_list, &sps, 0);
2327
Bram Moolenaar860cae12010-06-05 23:22:07 +02002328 if (syn_block->b_nospell_cluster_id != 0)
Bram Moolenaar3638c682005-06-08 22:05:14 +00002329 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002330 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002331 if (in_id_list(sip, sip->si_cont_list, &sps, 0))
2332 *can_spell = FALSE;
2333 }
2334 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002335 }
2336 }
2337
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 /*
2340 * Check for end of current state (and the states before it) at the
2341 * next column. Don't do this for syncing, because we would miss a
2342 * single character match.
2343 * First check if the current state ends at the current column. It
2344 * may be for an empty match and a containing item might end in the
2345 * current column.
2346 */
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002347 if (!syncing && !keep_state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +00002350 if (current_state.ga_len > 0
2351 && syn_getcurline()[current_col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 ++current_col;
2354 check_state_ends();
2355 --current_col;
2356 }
2357 }
2358 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002359 else if (can_spell != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002360 /* Default: Only do spelling when there is no @Spell cluster or when
2361 * ":syn spell toplevel" was used. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002362 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
2363 ? (syn_block->b_spell_cluster_id == 0)
2364 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaarf5b63862009-12-16 17:13:44 +00002366 /* nextgroup ends at end of line, unless "skipnl" or "skipempty" present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (current_next_list != NULL
2368 && syn_getcurline()[current_col + 1] == NUL
2369 && !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
2370 current_next_list = NULL;
2371
2372 if (zero_width_next_ga.ga_len > 0)
2373 ga_clear(&zero_width_next_ga);
2374
2375 /* No longer need external matches. But keep next_match_extmatch. */
2376 unref_extmatch(re_extmatch_out);
2377 re_extmatch_out = NULL;
2378 unref_extmatch(cur_extmatch);
2379
2380 return current_attr;
2381}
2382
2383
2384/*
2385 * Check if we already matched pattern "idx" at the current column.
2386 */
2387 static int
2388did_match_already(idx, gap)
2389 int idx;
2390 garray_T *gap;
2391{
2392 int i;
2393
2394 for (i = current_state.ga_len; --i >= 0; )
2395 if (CUR_STATE(i).si_m_startcol == (int)current_col
2396 && CUR_STATE(i).si_m_lnum == (int)current_lnum
2397 && CUR_STATE(i).si_idx == idx)
2398 return TRUE;
2399
2400 /* Zero-width matches with a nextgroup argument are not put on the syntax
2401 * stack, and can only be matched once anyway. */
2402 for (i = gap->ga_len; --i >= 0; )
2403 if (((int *)(gap->ga_data))[i] == idx)
2404 return TRUE;
2405
2406 return FALSE;
2407}
2408
2409/*
2410 * Push the next match onto the stack.
2411 */
2412 static stateitem_T *
2413push_next_match(cur_si)
2414 stateitem_T *cur_si;
2415{
2416 synpat_T *spp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002417#ifdef FEAT_CONCEAL
2418 int save_flags;
2419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaar860cae12010-06-05 23:22:07 +02002421 spp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 /*
2424 * Push the item in current_state stack;
2425 */
2426 if (push_current_state(next_match_idx) == OK)
2427 {
2428 /*
2429 * If it's a start-skip-end type that crosses lines, figure out how
2430 * much it continues in this line. Otherwise just fill in the length.
2431 */
2432 cur_si = &CUR_STATE(current_state.ga_len - 1);
2433 cur_si->si_h_startpos = next_match_h_startpos;
2434 cur_si->si_m_startcol = current_col;
2435 cur_si->si_m_lnum = current_lnum;
2436 cur_si->si_flags = spp->sp_flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002437#ifdef FEAT_CONCEAL
2438 cur_si->si_char = spp->sp_char;
2439 if (current_state.ga_len > 1)
2440 cur_si->si_flags |=
2441 CUR_STATE(current_state.ga_len - 2).si_flags & HL_CONCEAL;
2442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 cur_si->si_next_list = spp->sp_next_list;
2444 cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
2445 if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
2446 {
2447 /* Try to find the end pattern in the current line */
2448 update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
2449 check_keepend();
2450 }
2451 else
2452 {
2453 cur_si->si_m_endpos = next_match_m_endpos;
2454 cur_si->si_h_endpos = next_match_h_endpos;
2455 cur_si->si_ends = TRUE;
2456 cur_si->si_flags |= next_match_flags;
2457 cur_si->si_eoe_pos = next_match_eoe_pos;
2458 cur_si->si_end_idx = next_match_end_idx;
2459 }
2460 if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
2461 keepend_level = current_state.ga_len - 1;
2462 check_keepend();
2463 update_si_attr(current_state.ga_len - 1);
2464
Bram Moolenaar860cae12010-06-05 23:22:07 +02002465#ifdef FEAT_CONCEAL
2466 save_flags = cur_si->si_flags & (HL_CONCEAL | HL_CONCEALENDS);
2467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 /*
2469 * If the start pattern has another highlight group, push another item
2470 * on the stack for the start pattern.
2471 */
2472 if ( spp->sp_type == SPTYPE_START
2473 && spp->sp_syn_match_id != 0
2474 && push_current_state(next_match_idx) == OK)
2475 {
2476 cur_si = &CUR_STATE(current_state.ga_len - 1);
2477 cur_si->si_h_startpos = next_match_h_startpos;
2478 cur_si->si_m_startcol = current_col;
2479 cur_si->si_m_lnum = current_lnum;
2480 cur_si->si_m_endpos = next_match_eos_pos;
2481 cur_si->si_h_endpos = next_match_eos_pos;
2482 cur_si->si_ends = TRUE;
2483 cur_si->si_end_idx = 0;
2484 cur_si->si_flags = HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002485#ifdef FEAT_CONCEAL
2486 cur_si->si_flags |= save_flags;
2487 if (cur_si->si_flags & HL_CONCEALENDS)
2488 cur_si->si_flags |= HL_CONCEAL;
2489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 cur_si->si_next_list = NULL;
2491 check_keepend();
2492 update_si_attr(current_state.ga_len - 1);
2493 }
2494 }
2495
2496 next_match_idx = -1; /* try other match next time */
2497
2498 return cur_si;
2499}
2500
2501/*
2502 * Check for end of current state (and the states before it).
2503 */
2504 static void
2505check_state_ends()
2506{
2507 stateitem_T *cur_si;
2508 int had_extend = FALSE;
2509
2510 cur_si = &CUR_STATE(current_state.ga_len - 1);
2511 for (;;)
2512 {
2513 if (cur_si->si_ends
2514 && (cur_si->si_m_endpos.lnum < current_lnum
2515 || (cur_si->si_m_endpos.lnum == current_lnum
2516 && cur_si->si_m_endpos.col <= current_col)))
2517 {
2518 /*
2519 * If there is an end pattern group ID, highlight the end pattern
2520 * now. No need to pop the current item from the stack.
2521 * Only do this if the end pattern continues beyond the current
2522 * position.
2523 */
2524 if (cur_si->si_end_idx
2525 && (cur_si->si_eoe_pos.lnum > current_lnum
2526 || (cur_si->si_eoe_pos.lnum == current_lnum
2527 && cur_si->si_eoe_pos.col > current_col)))
2528 {
2529 cur_si->si_idx = cur_si->si_end_idx;
2530 cur_si->si_end_idx = 0;
2531 cur_si->si_m_endpos = cur_si->si_eoe_pos;
2532 cur_si->si_h_endpos = cur_si->si_eoe_pos;
2533 cur_si->si_flags |= HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002534#ifdef FEAT_CONCEAL
2535 if (cur_si->si_flags & HL_CONCEALENDS)
2536 cur_si->si_flags |= HL_CONCEAL;
2537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 update_si_attr(current_state.ga_len - 1);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002539
2540 /* what matches next may be different now, clear it */
2541 next_match_idx = 0;
2542 next_match_col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 break;
2544 }
2545 else
2546 {
2547 /* handle next_list, unless at end of line and no "skipnl" or
2548 * "skipempty" */
2549 current_next_list = cur_si->si_next_list;
2550 current_next_flags = cur_si->si_flags;
2551 if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
2552 && syn_getcurline()[current_col] == NUL)
2553 current_next_list = NULL;
2554
2555 /* When the ended item has "extend", another item with
2556 * "keepend" now needs to check for its end. */
2557 if (cur_si->si_flags & HL_EXTEND)
2558 had_extend = TRUE;
2559
2560 pop_current_state();
2561
2562 if (current_state.ga_len == 0)
2563 break;
2564
Bram Moolenaar81993f42008-01-11 20:27:45 +00002565 if (had_extend && keepend_level >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
2567 syn_update_ends(FALSE);
2568 if (current_state.ga_len == 0)
2569 break;
2570 }
2571
2572 cur_si = &CUR_STATE(current_state.ga_len - 1);
2573
2574 /*
2575 * Only for a region the search for the end continues after
2576 * the end of the contained item. If the contained match
2577 * included the end-of-line, break here, the region continues.
2578 * Don't do this when:
2579 * - "keepend" is used for the contained item
2580 * - not at the end of the line (could be end="x$"me=e-1).
2581 * - "excludenl" is used (HL_HAS_EOL won't be set)
2582 */
2583 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02002584 && SYN_ITEMS(syn_block)[cur_si->si_idx].sp_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 == SPTYPE_START
2586 && !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
2587 {
2588 update_si_end(cur_si, (int)current_col, TRUE);
2589 check_keepend();
2590 if ((current_next_flags & HL_HAS_EOL)
2591 && keepend_level < 0
2592 && syn_getcurline()[current_col] == NUL)
2593 break;
2594 }
2595 }
2596 }
2597 else
2598 break;
2599 }
2600}
2601
2602/*
2603 * Update an entry in the current_state stack for a match or region. This
2604 * fills in si_attr, si_next_list and si_cont_list.
2605 */
2606 static void
2607update_si_attr(idx)
2608 int idx;
2609{
2610 stateitem_T *sip = &CUR_STATE(idx);
2611 synpat_T *spp;
2612
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002613 /* This should not happen... */
2614 if (sip->si_idx < 0)
2615 return;
2616
Bram Moolenaar860cae12010-06-05 23:22:07 +02002617 spp = &(SYN_ITEMS(syn_block)[sip->si_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (sip->si_flags & HL_MATCH)
2619 sip->si_id = spp->sp_syn_match_id;
2620 else
2621 sip->si_id = spp->sp_syn.id;
2622 sip->si_attr = syn_id2attr(sip->si_id);
2623 sip->si_trans_id = sip->si_id;
2624 if (sip->si_flags & HL_MATCH)
2625 sip->si_cont_list = NULL;
2626 else
2627 sip->si_cont_list = spp->sp_cont_list;
2628
2629 /*
2630 * For transparent items, take attr from outer item.
2631 * Also take cont_list, if there is none.
2632 * Don't do this for the matchgroup of a start or end pattern.
2633 */
2634 if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
2635 {
2636 if (idx == 0)
2637 {
2638 sip->si_attr = 0;
2639 sip->si_trans_id = 0;
2640 if (sip->si_cont_list == NULL)
2641 sip->si_cont_list = ID_LIST_ALL;
2642 }
2643 else
2644 {
2645 sip->si_attr = CUR_STATE(idx - 1).si_attr;
2646 sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002647 sip->si_h_startpos = CUR_STATE(idx - 1).si_h_startpos;
2648 sip->si_h_endpos = CUR_STATE(idx - 1).si_h_endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (sip->si_cont_list == NULL)
2650 {
2651 sip->si_flags |= HL_TRANS_CONT;
2652 sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
2653 }
2654 }
2655 }
2656}
2657
2658/*
2659 * Check the current stack for patterns with "keepend" flag.
2660 * Propagate the match-end to contained items, until a "skipend" item is found.
2661 */
2662 static void
2663check_keepend()
2664{
2665 int i;
2666 lpos_T maxpos;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002667 lpos_T maxpos_h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 stateitem_T *sip;
2669
2670 /*
2671 * This check can consume a lot of time; only do it from the level where
2672 * there really is a keepend.
2673 */
2674 if (keepend_level < 0)
2675 return;
2676
2677 /*
2678 * Find the last index of an "extend" item. "keepend" items before that
2679 * won't do anything. If there is no "extend" item "i" will be
2680 * "keepend_level" and all "keepend" items will work normally.
2681 */
2682 for (i = current_state.ga_len - 1; i > keepend_level; --i)
2683 if (CUR_STATE(i).si_flags & HL_EXTEND)
2684 break;
2685
2686 maxpos.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002687 maxpos.col = 0;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002688 maxpos_h.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002689 maxpos_h.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 for ( ; i < current_state.ga_len; ++i)
2691 {
2692 sip = &CUR_STATE(i);
2693 if (maxpos.lnum != 0)
2694 {
2695 limit_pos_zero(&sip->si_m_endpos, &maxpos);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002696 limit_pos_zero(&sip->si_h_endpos, &maxpos_h);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 limit_pos_zero(&sip->si_eoe_pos, &maxpos);
2698 sip->si_ends = TRUE;
2699 }
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002700 if (sip->si_ends && (sip->si_flags & HL_KEEPEND))
2701 {
2702 if (maxpos.lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 || maxpos.lnum > sip->si_m_endpos.lnum
2704 || (maxpos.lnum == sip->si_m_endpos.lnum
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002705 && maxpos.col > sip->si_m_endpos.col))
2706 maxpos = sip->si_m_endpos;
2707 if (maxpos_h.lnum == 0
2708 || maxpos_h.lnum > sip->si_h_endpos.lnum
2709 || (maxpos_h.lnum == sip->si_h_endpos.lnum
2710 && maxpos_h.col > sip->si_h_endpos.col))
2711 maxpos_h = sip->si_h_endpos;
2712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714}
2715
2716/*
2717 * Update an entry in the current_state stack for a start-skip-end pattern.
2718 * This finds the end of the current item, if it's in the current line.
2719 *
2720 * Return the flags for the matched END.
2721 */
2722 static void
2723update_si_end(sip, startcol, force)
2724 stateitem_T *sip;
2725 int startcol; /* where to start searching for the end */
2726 int force; /* when TRUE overrule a previous end */
2727{
2728 lpos_T startpos;
2729 lpos_T endpos;
2730 lpos_T hl_endpos;
2731 lpos_T end_endpos;
2732 int end_idx;
2733
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002734 /* return quickly for a keyword */
2735 if (sip->si_idx < 0)
2736 return;
2737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 /* Don't update when it's already done. Can be a match of an end pattern
2739 * that started in a previous line. Watch out: can also be a "keepend"
2740 * from a containing item. */
2741 if (!force && sip->si_m_endpos.lnum >= current_lnum)
2742 return;
2743
2744 /*
2745 * We need to find the end of the region. It may continue in the next
2746 * line.
2747 */
2748 end_idx = 0;
2749 startpos.lnum = current_lnum;
2750 startpos.col = startcol;
2751 find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
2752 &(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
2753
2754 if (endpos.lnum == 0)
2755 {
2756 /* No end pattern matched. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002757 if (SYN_ITEMS(syn_block)[sip->si_idx].sp_flags & HL_ONELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
2759 /* a "oneline" never continues in the next line */
2760 sip->si_ends = TRUE;
2761 sip->si_m_endpos.lnum = current_lnum;
2762 sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
2763 }
2764 else
2765 {
2766 /* continues in the next line */
2767 sip->si_ends = FALSE;
2768 sip->si_m_endpos.lnum = 0;
2769 }
2770 sip->si_h_endpos = sip->si_m_endpos;
2771 }
2772 else
2773 {
2774 /* match within this line */
2775 sip->si_m_endpos = endpos;
2776 sip->si_h_endpos = hl_endpos;
2777 sip->si_eoe_pos = end_endpos;
2778 sip->si_ends = TRUE;
2779 sip->si_end_idx = end_idx;
2780 }
2781}
2782
2783/*
2784 * Add a new state to the current state stack.
2785 * It is cleared and the index set to "idx".
2786 * Return FAIL if it's not possible (out of memory).
2787 */
2788 static int
2789push_current_state(idx)
2790 int idx;
2791{
2792 if (ga_grow(&current_state, 1) == FAIL)
2793 return FAIL;
2794 vim_memset(&CUR_STATE(current_state.ga_len), 0, sizeof(stateitem_T));
2795 CUR_STATE(current_state.ga_len).si_idx = idx;
2796 ++current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 return OK;
2798}
2799
2800/*
2801 * Remove a state from the current_state stack.
2802 */
2803 static void
2804pop_current_state()
2805{
2806 if (current_state.ga_len)
2807 {
2808 unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
2809 --current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811 /* after the end of a pattern, try matching a keyword or pattern */
2812 next_match_idx = -1;
2813
2814 /* if first state with "keepend" is popped, reset keepend_level */
2815 if (keepend_level >= current_state.ga_len)
2816 keepend_level = -1;
2817}
2818
2819/*
2820 * Find the end of a start/skip/end syntax region after "startpos".
2821 * Only checks one line.
2822 * Also handles a match item that continued from a previous line.
2823 * If not found, the syntax item continues in the next line. m_endpos->lnum
2824 * will be 0.
2825 * If found, the end of the region and the end of the highlighting is
2826 * computed.
2827 */
2828 static void
2829find_endpos(idx, startpos, m_endpos, hl_endpos, flagsp, end_endpos,
2830 end_idx, start_ext)
2831 int idx; /* index of the pattern */
2832 lpos_T *startpos; /* where to start looking for an END match */
2833 lpos_T *m_endpos; /* return: end of match */
2834 lpos_T *hl_endpos; /* return: end of highlighting */
2835 long *flagsp; /* return: flags of matching END */
2836 lpos_T *end_endpos; /* return: end of end pattern match */
2837 int *end_idx; /* return: group ID for end pat. match, or 0 */
2838 reg_extmatch_T *start_ext; /* submatches from the start pattern */
2839{
2840 colnr_T matchcol;
2841 synpat_T *spp, *spp_skip;
2842 int start_idx;
2843 int best_idx;
2844 regmmatch_T regmatch;
2845 regmmatch_T best_regmatch; /* startpos/endpos of best match */
2846 lpos_T pos;
2847 char_u *line;
2848 int had_match = FALSE;
2849
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002850 /* just in case we are invoked for a keyword */
2851 if (idx < 0)
2852 return;
2853
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 /*
2855 * Check for being called with a START pattern.
2856 * Can happen with a match that continues to the next line, because it
2857 * contained a region.
2858 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002859 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (spp->sp_type != SPTYPE_START)
2861 {
2862 *hl_endpos = *startpos;
2863 return;
2864 }
2865
2866 /*
2867 * Find the SKIP or first END pattern after the last START pattern.
2868 */
2869 for (;;)
2870 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002871 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (spp->sp_type != SPTYPE_START)
2873 break;
2874 ++idx;
2875 }
2876
2877 /*
2878 * Lookup the SKIP pattern (if present)
2879 */
2880 if (spp->sp_type == SPTYPE_SKIP)
2881 {
2882 spp_skip = spp;
2883 ++idx;
2884 }
2885 else
2886 spp_skip = NULL;
2887
2888 /* Setup external matches for syn_regexec(). */
2889 unref_extmatch(re_extmatch_in);
2890 re_extmatch_in = ref_extmatch(start_ext);
2891
2892 matchcol = startpos->col; /* start looking for a match at sstart */
2893 start_idx = idx; /* remember the first END pattern. */
2894 best_regmatch.startpos[0].col = 0; /* avoid compiler warning */
2895 for (;;)
2896 {
2897 /*
2898 * Find end pattern that matches first after "matchcol".
2899 */
2900 best_idx = -1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901 for (idx = start_idx; idx < syn_block->b_syn_patterns.ga_len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 int lc_col = matchcol;
2904
Bram Moolenaar860cae12010-06-05 23:22:07 +02002905 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (spp->sp_type != SPTYPE_END) /* past last END pattern */
2907 break;
2908 lc_col -= spp->sp_offsets[SPO_LC_OFF];
2909 if (lc_col < 0)
2910 lc_col = 0;
2911
2912 regmatch.rmm_ic = spp->sp_ic;
2913 regmatch.regprog = spp->sp_prog;
2914 if (syn_regexec(&regmatch, startpos->lnum, lc_col))
2915 {
2916 if (best_idx == -1 || regmatch.startpos[0].col
2917 < best_regmatch.startpos[0].col)
2918 {
2919 best_idx = idx;
2920 best_regmatch.startpos[0] = regmatch.startpos[0];
2921 best_regmatch.endpos[0] = regmatch.endpos[0];
2922 }
2923 }
2924 }
2925
2926 /*
2927 * If all end patterns have been tried, and there is no match, the
2928 * item continues until end-of-line.
2929 */
2930 if (best_idx == -1)
2931 break;
2932
2933 /*
2934 * If the skip pattern matches before the end pattern,
2935 * continue searching after the skip pattern.
2936 */
2937 if (spp_skip != NULL)
2938 {
2939 int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
2940
2941 if (lc_col < 0)
2942 lc_col = 0;
2943 regmatch.rmm_ic = spp_skip->sp_ic;
2944 regmatch.regprog = spp_skip->sp_prog;
2945 if (syn_regexec(&regmatch, startpos->lnum, lc_col)
2946 && regmatch.startpos[0].col
2947 <= best_regmatch.startpos[0].col)
2948 {
2949 /* Add offset to skip pattern match */
2950 syn_add_end_off(&pos, &regmatch, spp_skip, SPO_ME_OFF, 1);
2951
2952 /* If the skip pattern goes on to the next line, there is no
2953 * match with an end pattern in this line. */
2954 if (pos.lnum > startpos->lnum)
2955 break;
2956
2957 line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
2958
2959 /* take care of an empty match or negative offset */
2960 if (pos.col <= matchcol)
2961 ++matchcol;
2962 else if (pos.col <= regmatch.endpos[0].col)
2963 matchcol = pos.col;
2964 else
2965 /* Be careful not to jump over the NUL at the end-of-line */
2966 for (matchcol = regmatch.endpos[0].col;
2967 line[matchcol] != NUL && matchcol < pos.col;
2968 ++matchcol)
2969 ;
2970
2971 /* if the skip pattern includes end-of-line, break here */
2972 if (line[matchcol] == NUL)
2973 break;
2974
2975 continue; /* start with first end pattern again */
2976 }
2977 }
2978
2979 /*
2980 * Match from start pattern to end pattern.
2981 * Correct for match and highlight offset of end pattern.
2982 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002983 spp = &(SYN_ITEMS(syn_block)[best_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
2985 /* can't end before the start */
2986 if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
2987 m_endpos->col = startpos->col;
2988
2989 syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
2990 /* can't end before the start */
2991 if (end_endpos->lnum == startpos->lnum
2992 && end_endpos->col < startpos->col)
2993 end_endpos->col = startpos->col;
2994 /* can't end after the match */
2995 limit_pos(end_endpos, m_endpos);
2996
2997 /*
2998 * If the end group is highlighted differently, adjust the pointers.
2999 */
3000 if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
3001 {
3002 *end_idx = best_idx;
3003 if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
3004 {
3005 hl_endpos->lnum = best_regmatch.endpos[0].lnum;
3006 hl_endpos->col = best_regmatch.endpos[0].col;
3007 }
3008 else
3009 {
3010 hl_endpos->lnum = best_regmatch.startpos[0].lnum;
3011 hl_endpos->col = best_regmatch.startpos[0].col;
3012 }
3013 hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
3014
3015 /* can't end before the start */
3016 if (hl_endpos->lnum == startpos->lnum
3017 && hl_endpos->col < startpos->col)
3018 hl_endpos->col = startpos->col;
3019 limit_pos(hl_endpos, m_endpos);
3020
3021 /* now the match ends where the highlighting ends, it is turned
3022 * into the matchgroup for the end */
3023 *m_endpos = *hl_endpos;
3024 }
3025 else
3026 {
3027 *end_idx = 0;
3028 *hl_endpos = *end_endpos;
3029 }
3030
3031 *flagsp = spp->sp_flags;
3032
3033 had_match = TRUE;
3034 break;
3035 }
3036
3037 /* no match for an END pattern in this line */
3038 if (!had_match)
3039 m_endpos->lnum = 0;
3040
3041 /* Remove external matches. */
3042 unref_extmatch(re_extmatch_in);
3043 re_extmatch_in = NULL;
3044}
3045
3046/*
3047 * Limit "pos" not to be after "limit".
3048 */
3049 static void
3050limit_pos(pos, limit)
3051 lpos_T *pos;
3052 lpos_T *limit;
3053{
3054 if (pos->lnum > limit->lnum)
3055 *pos = *limit;
3056 else if (pos->lnum == limit->lnum && pos->col > limit->col)
3057 pos->col = limit->col;
3058}
3059
3060/*
3061 * Limit "pos" not to be after "limit", unless pos->lnum is zero.
3062 */
3063 static void
3064limit_pos_zero(pos, limit)
3065 lpos_T *pos;
3066 lpos_T *limit;
3067{
3068 if (pos->lnum == 0)
3069 *pos = *limit;
3070 else
3071 limit_pos(pos, limit);
3072}
3073
3074/*
3075 * Add offset to matched text for end of match or highlight.
3076 */
3077 static void
3078syn_add_end_off(result, regmatch, spp, idx, extra)
3079 lpos_T *result; /* returned position */
3080 regmmatch_T *regmatch; /* start/end of match */
3081 synpat_T *spp; /* matched pattern */
3082 int idx; /* index of offset */
3083 int extra; /* extra chars for offset to start */
3084{
3085 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086 int off;
3087 char_u *base;
3088 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 if (spp->sp_off_flags & (1 << idx))
3091 {
3092 result->lnum = regmatch->startpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003093 col = regmatch->startpos[0].col;
3094 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 }
3096 else
3097 {
3098 result->lnum = regmatch->endpos[0].lnum;
3099 col = regmatch->endpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003100 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102 /* Don't go past the end of the line. Matters for "rs=e+2" when there
3103 * is a matchgroup. Watch out for match with last NL in the buffer. */
3104 if (result->lnum > syn_buf->b_ml.ml_line_count)
3105 col = 0;
3106 else if (off != 0)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003108 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3109 p = base + col;
3110 if (off > 0)
3111 {
3112 while (off-- > 0 && *p != NUL)
3113 mb_ptr_adv(p);
3114 }
3115 else if (off < 0)
3116 {
3117 while (off++ < 0 && base < p)
3118 mb_ptr_back(base, p);
3119 }
3120 col = (int)(p - base);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003121 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
3125/*
3126 * Add offset to matched text for start of match or highlight.
3127 * Avoid resulting column to become negative.
3128 */
3129 static void
3130syn_add_start_off(result, regmatch, spp, idx, extra)
3131 lpos_T *result; /* returned position */
3132 regmmatch_T *regmatch; /* start/end of match */
3133 synpat_T *spp;
3134 int idx;
3135 int extra; /* extra chars for offset to end */
3136{
3137 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138 int off;
3139 char_u *base;
3140 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
3143 {
3144 result->lnum = regmatch->endpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003145 col = regmatch->endpos[0].col;
3146 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
3148 else
3149 {
3150 result->lnum = regmatch->startpos[0].lnum;
3151 col = regmatch->startpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003154 if (result->lnum > syn_buf->b_ml.ml_line_count)
3155 {
3156 /* a "\n" at the end of the pattern may take us below the last line */
3157 result->lnum = syn_buf->b_ml.ml_line_count;
Bram Moolenaar8b9c05f2010-03-02 17:54:33 +01003158 col = (int)STRLEN(ml_get_buf(syn_buf, result->lnum, FALSE));
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003159 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160 if (off != 0)
3161 {
3162 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3163 p = base + col;
3164 if (off > 0)
3165 {
3166 while (off-- && *p != NUL)
3167 mb_ptr_adv(p);
3168 }
3169 else if (off < 0)
3170 {
3171 while (off++ && base < p)
3172 mb_ptr_back(base, p);
3173 }
3174 col = (int)(p - base);
3175 }
3176 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177}
3178
3179/*
3180 * Get current line in syntax buffer.
3181 */
3182 static char_u *
3183syn_getcurline()
3184{
3185 return ml_get_buf(syn_buf, current_lnum, FALSE);
3186}
3187
3188/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003189 * Call vim_regexec() to find a match with "rmp" in "syn_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * Returns TRUE when there is a match.
3191 */
3192 static int
3193syn_regexec(rmp, lnum, col)
3194 regmmatch_T *rmp;
3195 linenr_T lnum;
3196 colnr_T col;
3197{
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003198 rmp->rmm_maxcol = syn_buf->b_p_smc;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003199 if (vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 rmp->startpos[0].lnum += lnum;
3202 rmp->endpos[0].lnum += lnum;
3203 return TRUE;
3204 }
3205 return FALSE;
3206}
3207
3208/*
3209 * Check one position in a line for a matching keyword.
3210 * The caller must check if a keyword can start at startcol.
3211 * Return it's ID if found, 0 otherwise.
3212 */
3213 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02003214check_keyword_id(line, startcol, endcolp, flagsp, next_listp, cur_si, ccharp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 char_u *line;
3216 int startcol; /* position in line to check for keyword */
3217 int *endcolp; /* return: character after found keyword */
3218 long *flagsp; /* return: flags of matching keyword */
3219 short **next_listp; /* return: next_list of matching keyword */
3220 stateitem_T *cur_si; /* item at the top of the stack */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003221 int *ccharp UNUSED; /* conceal substitution char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaardad6b692005-01-25 22:14:34 +00003223 keyentry_T *kp;
3224 char_u *kwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 int round;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003226 int kwlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 char_u keyword[MAXKEYWLEN + 1]; /* assume max. keyword len is 80 */
Bram Moolenaardad6b692005-01-25 22:14:34 +00003228 hashtab_T *ht;
3229 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 /* Find first character after the keyword. First character was already
3232 * checked. */
Bram Moolenaardad6b692005-01-25 22:14:34 +00003233 kwp = line + startcol;
3234 kwlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 do
3236 {
3237#ifdef FEAT_MBYTE
3238 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003239 kwlen += (*mb_ptr2len)(kwp + kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 else
3241#endif
Bram Moolenaardad6b692005-01-25 22:14:34 +00003242 ++kwlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00003244 while (vim_iswordc_buf(kwp + kwlen, syn_buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Bram Moolenaardad6b692005-01-25 22:14:34 +00003246 if (kwlen > MAXKEYWLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 return 0;
3248
3249 /*
3250 * Must make a copy of the keyword, so we can add a NUL and make it
3251 * lowercase.
3252 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003253 vim_strncpy(keyword, kwp, kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 /*
3256 * Try twice:
3257 * 1. matching case
3258 * 2. ignoring case
3259 */
3260 for (round = 1; round <= 2; ++round)
3261 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003262 ht = round == 1 ? &syn_block->b_keywtab : &syn_block->b_keywtab_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003263 if (ht->ht_used == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 continue;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003265 if (round == 2) /* ignore case */
3266 (void)str_foldcase(kwp, kwlen, keyword, MAXKEYWLEN + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268 /*
Bram Moolenaardad6b692005-01-25 22:14:34 +00003269 * Find keywords that match. There can be several with different
3270 * attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 * When current_next_list is non-zero accept only that group, otherwise:
3272 * Accept a not-contained keyword at toplevel.
3273 * Accept a keyword at other levels only if it is in the contains list.
3274 */
Bram Moolenaardad6b692005-01-25 22:14:34 +00003275 hi = hash_find(ht, keyword);
3276 if (!HASHITEM_EMPTY(hi))
3277 for (kp = HI2KE(hi); kp != NULL; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00003279 if (current_next_list != 0
3280 ? in_id_list(NULL, current_next_list, &kp->k_syn, 0)
3281 : (cur_si == NULL
3282 ? !(kp->flags & HL_CONTAINED)
3283 : in_id_list(cur_si, cur_si->si_cont_list,
3284 &kp->k_syn, kp->flags & HL_CONTAINED)))
3285 {
3286 *endcolp = startcol + kwlen;
3287 *flagsp = kp->flags;
3288 *next_listp = kp->next_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003289#ifdef FEAT_CONCEAL
3290 *ccharp = kp->k_char;
3291#endif
Bram Moolenaardad6b692005-01-25 22:14:34 +00003292 return kp->k_syn.id;
3293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295 }
3296 return 0;
3297}
3298
3299/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02003300 * Handle ":syntax conceal" command.
3301 */
3302 static void
3303syn_cmd_conceal(eap, syncing)
3304 exarg_T *eap UNUSED;
3305 int syncing UNUSED;
3306{
3307#ifdef FEAT_CONCEAL
3308 char_u *arg = eap->arg;
3309 char_u *next;
3310
3311 eap->nextcmd = find_nextcmd(arg);
3312 if (eap->skip)
3313 return;
3314
3315 next = skiptowhite(arg);
3316 if (STRNICMP(arg, "on", 2) == 0 && next - arg == 2)
3317 curwin->w_s->b_syn_conceal = TRUE;
3318 else if (STRNICMP(arg, "off", 3) == 0 && next - arg == 3)
3319 curwin->w_s->b_syn_conceal = FALSE;
3320 else
3321 EMSG2(_("E390: Illegal argument: %s"), arg);
3322#endif
3323}
3324
3325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 * Handle ":syntax case" command.
3327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 static void
3329syn_cmd_case(eap, syncing)
3330 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003331 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 char_u *arg = eap->arg;
3334 char_u *next;
3335
3336 eap->nextcmd = find_nextcmd(arg);
3337 if (eap->skip)
3338 return;
3339
3340 next = skiptowhite(arg);
3341 if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342 curwin->w_s->b_syn_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003344 curwin->w_s->b_syn_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
3346 EMSG2(_("E390: Illegal argument: %s"), arg);
3347}
3348
3349/*
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003350 * Handle ":syntax spell" command.
3351 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003352 static void
3353syn_cmd_spell(eap, syncing)
3354 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003355 int syncing UNUSED;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003356{
3357 char_u *arg = eap->arg;
3358 char_u *next;
3359
3360 eap->nextcmd = find_nextcmd(arg);
3361 if (eap->skip)
3362 return;
3363
3364 next = skiptowhite(arg);
3365 if (STRNICMP(arg, "toplevel", 8) == 0 && next - arg == 8)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003366 curwin->w_s->b_syn_spell = SYNSPL_TOP;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003367 else if (STRNICMP(arg, "notoplevel", 10) == 0 && next - arg == 10)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003368 curwin->w_s->b_syn_spell = SYNSPL_NOTOP;
Bram Moolenaar86ea7642007-02-04 01:48:10 +00003369 else if (STRNICMP(arg, "default", 7) == 0 && next - arg == 7)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003370 curwin->w_s->b_syn_spell = SYNSPL_DEFAULT;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003371 else
3372 EMSG2(_("E390: Illegal argument: %s"), arg);
3373}
3374
3375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 * Clear all syntax info for one buffer.
3377 */
3378 void
Bram Moolenaar860cae12010-06-05 23:22:07 +02003379syntax_clear(block)
3380 synblock_T *block;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 int i;
3383
Bram Moolenaar860cae12010-06-05 23:22:07 +02003384 block->b_syn_error = FALSE; /* clear previous error */
3385 block->b_syn_ic = FALSE; /* Use case, by default */
3386 block->b_syn_spell = SYNSPL_DEFAULT; /* default spell checking */
3387 block->b_syn_containedin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
3389 /* free the keywords */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003390 clear_keywtab(&block->b_keywtab);
3391 clear_keywtab(&block->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 /* free the syntax patterns */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003394 for (i = block->b_syn_patterns.ga_len; --i >= 0; )
3395 syn_clear_pattern(block, i);
3396 ga_clear(&block->b_syn_patterns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
3398 /* free the syntax clusters */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003399 for (i = block->b_syn_clusters.ga_len; --i >= 0; )
3400 syn_clear_cluster(block, i);
3401 ga_clear(&block->b_syn_clusters);
3402 block->b_spell_cluster_id = 0;
3403 block->b_nospell_cluster_id = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar860cae12010-06-05 23:22:07 +02003405 block->b_syn_sync_flags = 0;
3406 block->b_syn_sync_minlines = 0;
3407 block->b_syn_sync_maxlines = 0;
3408 block->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar860cae12010-06-05 23:22:07 +02003410 vim_free(block->b_syn_linecont_prog);
3411 block->b_syn_linecont_prog = NULL;
3412 vim_free(block->b_syn_linecont_pat);
3413 block->b_syn_linecont_pat = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#ifdef FEAT_FOLDING
Bram Moolenaar860cae12010-06-05 23:22:07 +02003415 block->b_syn_folditems = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#endif
3417
3418 /* free the stored states */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003419 syn_stack_free_all(block);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 invalidate_current_state();
3421}
3422
3423/*
3424 * Clear syncing info for one buffer.
3425 */
3426 static void
3427syntax_sync_clear()
3428{
3429 int i;
3430
3431 /* free the syntax patterns */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003432 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
3433 if (SYN_ITEMS(curwin->w_s)[i].sp_syncing)
3434 syn_remove_pattern(curwin->w_s, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar860cae12010-06-05 23:22:07 +02003436 curwin->w_s->b_syn_sync_flags = 0;
3437 curwin->w_s->b_syn_sync_minlines = 0;
3438 curwin->w_s->b_syn_sync_maxlines = 0;
3439 curwin->w_s->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar860cae12010-06-05 23:22:07 +02003441 vim_free(curwin->w_s->b_syn_linecont_prog);
3442 curwin->w_s->b_syn_linecont_prog = NULL;
3443 vim_free(curwin->w_s->b_syn_linecont_pat);
3444 curwin->w_s->b_syn_linecont_pat = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar860cae12010-06-05 23:22:07 +02003446 syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447}
3448
3449/*
3450 * Remove one pattern from the buffer's pattern list.
3451 */
3452 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02003453syn_remove_pattern(block, idx)
3454 synblock_T *block;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int idx;
3456{
3457 synpat_T *spp;
3458
Bram Moolenaar860cae12010-06-05 23:22:07 +02003459 spp = &(SYN_ITEMS(block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460#ifdef FEAT_FOLDING
3461 if (spp->sp_flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003462 --block->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003464 syn_clear_pattern(block, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 mch_memmove(spp, spp + 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003466 sizeof(synpat_T) * (block->b_syn_patterns.ga_len - idx - 1));
3467 --block->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468}
3469
3470/*
3471 * Clear and free one syntax pattern. When clearing all, must be called from
3472 * last to first!
3473 */
3474 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02003475syn_clear_pattern(block, i)
3476 synblock_T *block;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 int i;
3478{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003479 vim_free(SYN_ITEMS(block)[i].sp_pattern);
3480 vim_free(SYN_ITEMS(block)[i].sp_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 /* Only free sp_cont_list and sp_next_list of first start pattern */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003482 if (i == 0 || SYN_ITEMS(block)[i - 1].sp_type != SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003484 vim_free(SYN_ITEMS(block)[i].sp_cont_list);
3485 vim_free(SYN_ITEMS(block)[i].sp_next_list);
3486 vim_free(SYN_ITEMS(block)[i].sp_syn.cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488}
3489
3490/*
3491 * Clear and free one syntax cluster.
3492 */
3493 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02003494syn_clear_cluster(block, i)
3495 synblock_T *block;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 int i;
3497{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003498 vim_free(SYN_CLSTR(block)[i].scl_name);
3499 vim_free(SYN_CLSTR(block)[i].scl_name_u);
3500 vim_free(SYN_CLSTR(block)[i].scl_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502
3503/*
3504 * Handle ":syntax clear" command.
3505 */
3506 static void
3507syn_cmd_clear(eap, syncing)
3508 exarg_T *eap;
3509 int syncing;
3510{
3511 char_u *arg = eap->arg;
3512 char_u *arg_end;
3513 int id;
3514
3515 eap->nextcmd = find_nextcmd(arg);
3516 if (eap->skip)
3517 return;
3518
3519 /*
3520 * We have to disable this within ":syn include @group filename",
3521 * because otherwise @group would get deleted.
3522 * Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
3523 * clear".
3524 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003525 if (curwin->w_s->b_syn_topgrp != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 return;
3527
3528 if (ends_excmd(*arg))
3529 {
3530 /*
3531 * No argument: Clear all syntax items.
3532 */
3533 if (syncing)
3534 syntax_sync_clear();
3535 else
3536 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003537 syntax_clear(curwin->w_s);
3538 if (curwin->w_s == &curwin->w_buffer->b_s)
3539 do_unlet((char_u *)"b:current_syntax", TRUE);
Bram Moolenaar1950c352010-06-06 15:21:10 +02003540 do_unlet((char_u *)"w:current_syntax", TRUE);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543 }
3544 else
3545 {
3546 /*
3547 * Clear the group IDs that are in the argument.
3548 */
3549 while (!ends_excmd(*arg))
3550 {
3551 arg_end = skiptowhite(arg);
3552 if (*arg == '@')
3553 {
3554 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3555 if (id == 0)
3556 {
3557 EMSG2(_("E391: No such syntax cluster: %s"), arg);
3558 break;
3559 }
3560 else
3561 {
3562 /*
3563 * We can't physically delete a cluster without changing
3564 * the IDs of other clusters, so we do the next best thing
3565 * and make it empty.
3566 */
3567 short scl_id = id - SYNID_CLUSTER;
3568
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569 vim_free(SYN_CLSTR(curwin->w_s)[scl_id].scl_list);
3570 SYN_CLSTR(curwin->w_s)[scl_id].scl_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 }
3573 else
3574 {
3575 id = syn_namen2id(arg, (int)(arg_end - arg));
3576 if (id == 0)
3577 {
3578 EMSG2(_(e_nogroup), arg);
3579 break;
3580 }
3581 else
3582 syn_clear_one(id, syncing);
3583 }
3584 arg = skipwhite(arg_end);
3585 }
3586 }
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00003587 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003588 syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
3590
3591/*
3592 * Clear one syntax group for the current buffer.
3593 */
3594 static void
3595syn_clear_one(id, syncing)
3596 int id;
3597 int syncing;
3598{
3599 synpat_T *spp;
3600 int idx;
3601
3602 /* Clear keywords only when not ":syn sync clear group-name" */
3603 if (!syncing)
3604 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003605 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab);
3606 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608
3609 /* clear the patterns for "id" */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003610 for (idx = curwin->w_s->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003612 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3614 continue;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003615 syn_remove_pattern(curwin->w_s, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617}
3618
3619/*
3620 * Handle ":syntax on" command.
3621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 static void
3623syn_cmd_on(eap, syncing)
3624 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003625 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 syn_cmd_onoff(eap, "syntax");
3628}
3629
3630/*
3631 * Handle ":syntax enable" command.
3632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 static void
3634syn_cmd_enable(eap, syncing)
3635 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003636 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
3638 set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"enable");
3639 syn_cmd_onoff(eap, "syntax");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003640 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641}
3642
3643/*
3644 * Handle ":syntax reset" command.
3645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 static void
3647syn_cmd_reset(eap, syncing)
3648 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003649 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 eap->nextcmd = check_nextcmd(eap->arg);
3652 if (!eap->skip)
3653 {
3654 set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"reset");
3655 do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003656 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658}
3659
3660/*
3661 * Handle ":syntax manual" command.
3662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 static void
3664syn_cmd_manual(eap, syncing)
3665 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003666 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 syn_cmd_onoff(eap, "manual");
3669}
3670
3671/*
3672 * Handle ":syntax off" command.
3673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 static void
3675syn_cmd_off(eap, syncing)
3676 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003677 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 syn_cmd_onoff(eap, "nosyntax");
3680}
3681
3682 static void
3683syn_cmd_onoff(eap, name)
3684 exarg_T *eap;
3685 char *name;
3686{
3687 char_u buf[100];
3688
3689 eap->nextcmd = check_nextcmd(eap->arg);
3690 if (!eap->skip)
3691 {
3692 STRCPY(buf, "so ");
Bram Moolenaar555b2802005-05-19 21:08:39 +00003693 vim_snprintf((char *)buf + 3, sizeof(buf) - 3, SYNTAX_FNAME, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 do_cmdline_cmd(buf);
3695 }
3696}
3697
3698/*
3699 * Handle ":syntax [list]" command: list current syntax words.
3700 */
3701 static void
3702syn_cmd_list(eap, syncing)
3703 exarg_T *eap;
3704 int syncing; /* when TRUE: list syncing items */
3705{
3706 char_u *arg = eap->arg;
3707 int id;
3708 char_u *arg_end;
3709
3710 eap->nextcmd = find_nextcmd(arg);
3711 if (eap->skip)
3712 return;
3713
Bram Moolenaar860cae12010-06-05 23:22:07 +02003714 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 MSG(_("No Syntax items defined for this buffer"));
3717 return;
3718 }
3719
3720 if (syncing)
3721 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003722 if (curwin->w_s->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
3724 MSG_PUTS(_("syncing on C-style comments"));
3725 syn_lines_msg();
3726 syn_match_msg();
3727 return;
3728 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003729 else if (!(curwin->w_s->b_syn_sync_flags & SF_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003731 if (curwin->w_s->b_syn_sync_minlines == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 MSG_PUTS(_("no syncing"));
3733 else
3734 {
3735 MSG_PUTS(_("syncing starts "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003736 msg_outnum(curwin->w_s->b_syn_sync_minlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 MSG_PUTS(_(" lines before top line"));
3738 syn_match_msg();
3739 }
3740 return;
3741 }
3742 MSG_PUTS_TITLE(_("\n--- Syntax sync items ---"));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003743 if (curwin->w_s->b_syn_sync_minlines > 0
3744 || curwin->w_s->b_syn_sync_maxlines > 0
3745 || curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
3747 MSG_PUTS(_("\nsyncing on items"));
3748 syn_lines_msg();
3749 syn_match_msg();
3750 }
3751 }
3752 else
3753 MSG_PUTS_TITLE(_("\n--- Syntax items ---"));
3754 if (ends_excmd(*arg))
3755 {
3756 /*
3757 * No argument: List all group IDs and all syntax clusters.
3758 */
3759 for (id = 1; id <= highlight_ga.ga_len && !got_int; ++id)
3760 syn_list_one(id, syncing, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003761 for (id = 0; id < curwin->w_s->b_syn_clusters.ga_len && !got_int; ++id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 syn_list_cluster(id);
3763 }
3764 else
3765 {
3766 /*
3767 * List the group IDs and syntax clusters that are in the argument.
3768 */
3769 while (!ends_excmd(*arg) && !got_int)
3770 {
3771 arg_end = skiptowhite(arg);
3772 if (*arg == '@')
3773 {
3774 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3775 if (id == 0)
3776 EMSG2(_("E392: No such syntax cluster: %s"), arg);
3777 else
3778 syn_list_cluster(id - SYNID_CLUSTER);
3779 }
3780 else
3781 {
3782 id = syn_namen2id(arg, (int)(arg_end - arg));
3783 if (id == 0)
3784 EMSG2(_(e_nogroup), arg);
3785 else
3786 syn_list_one(id, syncing, TRUE);
3787 }
3788 arg = skipwhite(arg_end);
3789 }
3790 }
3791 eap->nextcmd = check_nextcmd(arg);
3792}
3793
3794 static void
3795syn_lines_msg()
3796{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003797 if (curwin->w_s->b_syn_sync_maxlines > 0
3798 || curwin->w_s->b_syn_sync_minlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
3800 MSG_PUTS("; ");
Bram Moolenaar860cae12010-06-05 23:22:07 +02003801 if (curwin->w_s->b_syn_sync_minlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
3803 MSG_PUTS(_("minimal "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003804 msg_outnum(curwin->w_s->b_syn_sync_minlines);
3805 if (curwin->w_s->b_syn_sync_maxlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 MSG_PUTS(", ");
3807 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003808 if (curwin->w_s->b_syn_sync_maxlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
3810 MSG_PUTS(_("maximal "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003811 msg_outnum(curwin->w_s->b_syn_sync_maxlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813 MSG_PUTS(_(" lines before top line"));
3814 }
3815}
3816
3817 static void
3818syn_match_msg()
3819{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003820 if (curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
3822 MSG_PUTS(_("; match "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003823 msg_outnum(curwin->w_s->b_syn_sync_linebreaks);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 MSG_PUTS(_(" line breaks"));
3825 }
3826}
3827
3828static int last_matchgroup;
3829
3830struct name_list
3831{
3832 int flag;
3833 char *name;
3834};
3835
3836static void syn_list_flags __ARGS((struct name_list *nl, int flags, int attr));
3837
3838/*
3839 * List one syntax item, for ":syntax" or "syntax list syntax_name".
3840 */
3841 static void
3842syn_list_one(id, syncing, link_only)
3843 int id;
3844 int syncing; /* when TRUE: list syncing items */
3845 int link_only; /* when TRUE; list link-only too */
3846{
3847 int attr;
3848 int idx;
3849 int did_header = FALSE;
3850 synpat_T *spp;
3851 static struct name_list namelist1[] =
3852 {
3853 {HL_DISPLAY, "display"},
3854 {HL_CONTAINED, "contained"},
3855 {HL_ONELINE, "oneline"},
3856 {HL_KEEPEND, "keepend"},
3857 {HL_EXTEND, "extend"},
3858 {HL_EXCLUDENL, "excludenl"},
3859 {HL_TRANSP, "transparent"},
3860 {HL_FOLD, "fold"},
Bram Moolenaar860cae12010-06-05 23:22:07 +02003861#ifdef FEAT_CONCEAL
3862 {HL_CONCEAL, "conceal"},
3863 {HL_CONCEALENDS, "concealends"},
3864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {0, NULL}
3866 };
3867 static struct name_list namelist2[] =
3868 {
3869 {HL_SKIPWHITE, "skipwhite"},
3870 {HL_SKIPNL, "skipnl"},
3871 {HL_SKIPEMPTY, "skipempty"},
3872 {0, NULL}
3873 };
3874
3875 attr = hl_attr(HLF_D); /* highlight like directories */
3876
3877 /* list the keywords for "id" */
3878 if (!syncing)
3879 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003880 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab, FALSE, attr);
3881 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 did_header, attr);
3883 }
3884
3885 /* list the patterns for "id" */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003886 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len && !got_int; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003888 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3890 continue;
3891
3892 (void)syn_list_header(did_header, 999, id);
3893 did_header = TRUE;
3894 last_matchgroup = 0;
3895 if (spp->sp_type == SPTYPE_MATCH)
3896 {
3897 put_pattern("match", ' ', spp, attr);
3898 msg_putchar(' ');
3899 }
3900 else if (spp->sp_type == SPTYPE_START)
3901 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003902 while (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_START)
3903 put_pattern("start", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
3904 if (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_SKIP)
3905 put_pattern("skip", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
3906 while (idx < curwin->w_s->b_syn_patterns.ga_len
3907 && SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_END)
3908 put_pattern("end", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 --idx;
3910 msg_putchar(' ');
3911 }
3912 syn_list_flags(namelist1, spp->sp_flags, attr);
3913
3914 if (spp->sp_cont_list != NULL)
3915 put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
3916
3917 if (spp->sp_syn.cont_in_list != NULL)
3918 put_id_list((char_u *)"containedin",
3919 spp->sp_syn.cont_in_list, attr);
3920
3921 if (spp->sp_next_list != NULL)
3922 {
3923 put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
3924 syn_list_flags(namelist2, spp->sp_flags, attr);
3925 }
3926 if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
3927 {
3928 if (spp->sp_flags & HL_SYNC_HERE)
3929 msg_puts_attr((char_u *)"grouphere", attr);
3930 else
3931 msg_puts_attr((char_u *)"groupthere", attr);
3932 msg_putchar(' ');
3933 if (spp->sp_sync_idx >= 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003934 msg_outtrans(HL_TABLE()[SYN_ITEMS(curwin->w_s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 [spp->sp_sync_idx].sp_syn.id - 1].sg_name);
3936 else
3937 MSG_PUTS("NONE");
3938 msg_putchar(' ');
3939 }
3940 }
3941
3942 /* list the link, if there is one */
3943 if (HL_TABLE()[id - 1].sg_link && (did_header || link_only) && !got_int)
3944 {
3945 (void)syn_list_header(did_header, 999, id);
3946 msg_puts_attr((char_u *)"links to", attr);
3947 msg_putchar(' ');
3948 msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
3949 }
3950}
3951
3952 static void
3953syn_list_flags(nl, flags, attr)
3954 struct name_list *nl;
3955 int flags;
3956 int attr;
3957{
3958 int i;
3959
3960 for (i = 0; nl[i].flag != 0; ++i)
3961 if (flags & nl[i].flag)
3962 {
3963 msg_puts_attr((char_u *)nl[i].name, attr);
3964 msg_putchar(' ');
3965 }
3966}
3967
3968/*
3969 * List one syntax cluster, for ":syntax" or "syntax list syntax_name".
3970 */
3971 static void
3972syn_list_cluster(id)
3973 int id;
3974{
3975 int endcol = 15;
3976
3977 /* slight hack: roughly duplicate the guts of syn_list_header() */
3978 msg_putchar('\n');
Bram Moolenaar860cae12010-06-05 23:22:07 +02003979 msg_outtrans(SYN_CLSTR(curwin->w_s)[id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981 if (msg_col >= endcol) /* output at least one space */
3982 endcol = msg_col + 1;
3983 if (Columns <= endcol) /* avoid hang for tiny window */
3984 endcol = Columns - 1;
3985
3986 msg_advance(endcol);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003987 if (SYN_CLSTR(curwin->w_s)[id].scl_list != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003989 put_id_list((char_u *)"cluster", SYN_CLSTR(curwin->w_s)[id].scl_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 hl_attr(HLF_D));
3991 }
3992 else
3993 {
3994 msg_puts_attr((char_u *)"cluster", hl_attr(HLF_D));
3995 msg_puts((char_u *)"=NONE");
3996 }
3997}
3998
3999 static void
4000put_id_list(name, list, attr)
4001 char_u *name;
4002 short *list;
4003 int attr;
4004{
4005 short *p;
4006
4007 msg_puts_attr(name, attr);
4008 msg_putchar('=');
4009 for (p = list; *p; ++p)
4010 {
4011 if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
4012 {
4013 if (p[1])
4014 MSG_PUTS("ALLBUT");
4015 else
4016 MSG_PUTS("ALL");
4017 }
4018 else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
4019 {
4020 MSG_PUTS("TOP");
4021 }
4022 else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
4023 {
4024 MSG_PUTS("CONTAINED");
4025 }
4026 else if (*p >= SYNID_CLUSTER)
4027 {
4028 short scl_id = *p - SYNID_CLUSTER;
4029
4030 msg_putchar('@');
Bram Moolenaar860cae12010-06-05 23:22:07 +02004031 msg_outtrans(SYN_CLSTR(curwin->w_s)[scl_id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
4033 else
4034 msg_outtrans(HL_TABLE()[*p - 1].sg_name);
4035 if (p[1])
4036 msg_putchar(',');
4037 }
4038 msg_putchar(' ');
4039}
4040
4041 static void
4042put_pattern(s, c, spp, attr)
4043 char *s;
4044 int c;
4045 synpat_T *spp;
4046 int attr;
4047{
4048 long n;
4049 int mask;
4050 int first;
4051 static char *sepchars = "/+=-#@\"|'^&";
4052 int i;
4053
4054 /* May have to write "matchgroup=group" */
4055 if (last_matchgroup != spp->sp_syn_match_id)
4056 {
4057 last_matchgroup = spp->sp_syn_match_id;
4058 msg_puts_attr((char_u *)"matchgroup", attr);
4059 msg_putchar('=');
4060 if (last_matchgroup == 0)
4061 msg_outtrans((char_u *)"NONE");
4062 else
4063 msg_outtrans(HL_TABLE()[last_matchgroup - 1].sg_name);
4064 msg_putchar(' ');
4065 }
4066
4067 /* output the name of the pattern and an '=' or ' ' */
4068 msg_puts_attr((char_u *)s, attr);
4069 msg_putchar(c);
4070
4071 /* output the pattern, in between a char that is not in the pattern */
4072 for (i = 0; vim_strchr(spp->sp_pattern, sepchars[i]) != NULL; )
4073 if (sepchars[++i] == NUL)
4074 {
4075 i = 0; /* no good char found, just use the first one */
4076 break;
4077 }
4078 msg_putchar(sepchars[i]);
4079 msg_outtrans(spp->sp_pattern);
4080 msg_putchar(sepchars[i]);
4081
4082 /* output any pattern options */
4083 first = TRUE;
4084 for (i = 0; i < SPO_COUNT; ++i)
4085 {
4086 mask = (1 << i);
4087 if (spp->sp_off_flags & (mask + (mask << SPO_COUNT)))
4088 {
4089 if (!first)
4090 msg_putchar(','); /* separate with commas */
4091 msg_puts((char_u *)spo_name_tab[i]);
4092 n = spp->sp_offsets[i];
4093 if (i != SPO_LC_OFF)
4094 {
4095 if (spp->sp_off_flags & mask)
4096 msg_putchar('s');
4097 else
4098 msg_putchar('e');
4099 if (n > 0)
4100 msg_putchar('+');
4101 }
4102 if (n || i == SPO_LC_OFF)
4103 msg_outnum(n);
4104 first = FALSE;
4105 }
4106 }
4107 msg_putchar(' ');
4108}
4109
4110/*
4111 * List or clear the keywords for one syntax group.
4112 * Return TRUE if the header has been printed.
4113 */
4114 static int
Bram Moolenaardad6b692005-01-25 22:14:34 +00004115syn_list_keywords(id, ht, did_header, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 int id;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004117 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 int did_header; /* header has already been printed */
4119 int attr;
4120{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 int outlen;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004122 hashitem_T *hi;
4123 keyentry_T *kp;
4124 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 int prev_contained = 0;
4126 short *prev_next_list = NULL;
4127 short *prev_cont_in_list = NULL;
4128 int prev_skipnl = 0;
4129 int prev_skipwhite = 0;
4130 int prev_skipempty = 0;
4131
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 /*
4133 * Unfortunately, this list of keywords is not sorted on alphabet but on
4134 * hash value...
4135 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004136 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004137 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004139 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004141 --todo;
4142 for (kp = HI2KE(hi); kp != NULL && !got_int; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004144 if (kp->k_syn.id == id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004146 if (prev_contained != (kp->flags & HL_CONTAINED)
4147 || prev_skipnl != (kp->flags & HL_SKIPNL)
4148 || prev_skipwhite != (kp->flags & HL_SKIPWHITE)
4149 || prev_skipempty != (kp->flags & HL_SKIPEMPTY)
4150 || prev_cont_in_list != kp->k_syn.cont_in_list
4151 || prev_next_list != kp->next_list)
4152 outlen = 9999;
4153 else
4154 outlen = (int)STRLEN(kp->keyword);
4155 /* output "contained" and "nextgroup" on each line */
4156 if (syn_list_header(did_header, outlen, id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004158 prev_contained = 0;
4159 prev_next_list = NULL;
4160 prev_cont_in_list = NULL;
4161 prev_skipnl = 0;
4162 prev_skipwhite = 0;
4163 prev_skipempty = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004165 did_header = TRUE;
4166 if (prev_contained != (kp->flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004168 msg_puts_attr((char_u *)"contained", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004170 prev_contained = (kp->flags & HL_CONTAINED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004172 if (kp->k_syn.cont_in_list != prev_cont_in_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004174 put_id_list((char_u *)"containedin",
4175 kp->k_syn.cont_in_list, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004177 prev_cont_in_list = kp->k_syn.cont_in_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004179 if (kp->next_list != prev_next_list)
4180 {
4181 put_id_list((char_u *)"nextgroup", kp->next_list, attr);
4182 msg_putchar(' ');
4183 prev_next_list = kp->next_list;
4184 if (kp->flags & HL_SKIPNL)
4185 {
4186 msg_puts_attr((char_u *)"skipnl", attr);
4187 msg_putchar(' ');
4188 prev_skipnl = (kp->flags & HL_SKIPNL);
4189 }
4190 if (kp->flags & HL_SKIPWHITE)
4191 {
4192 msg_puts_attr((char_u *)"skipwhite", attr);
4193 msg_putchar(' ');
4194 prev_skipwhite = (kp->flags & HL_SKIPWHITE);
4195 }
4196 if (kp->flags & HL_SKIPEMPTY)
4197 {
4198 msg_puts_attr((char_u *)"skipempty", attr);
4199 msg_putchar(' ');
4200 prev_skipempty = (kp->flags & HL_SKIPEMPTY);
4201 }
4202 }
4203 msg_outtrans(kp->keyword);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206 }
4207 }
4208
4209 return did_header;
4210}
4211
4212 static void
Bram Moolenaardad6b692005-01-25 22:14:34 +00004213syn_clear_keyword(id, ht)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int id;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004215 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004217 hashitem_T *hi;
4218 keyentry_T *kp;
4219 keyentry_T *kp_prev;
4220 keyentry_T *kp_next;
4221 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaardad6b692005-01-25 22:14:34 +00004223 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004224 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004225 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004227 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004229 --todo;
4230 kp_prev = NULL;
4231 for (kp = HI2KE(hi); kp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004233 if (kp->k_syn.id == id)
4234 {
4235 kp_next = kp->ke_next;
4236 if (kp_prev == NULL)
4237 {
4238 if (kp_next == NULL)
4239 hash_remove(ht, hi);
4240 else
4241 hi->hi_key = KE2HIKEY(kp_next);
4242 }
4243 else
4244 kp_prev->ke_next = kp_next;
4245 vim_free(kp->next_list);
4246 vim_free(kp->k_syn.cont_in_list);
4247 vim_free(kp);
4248 kp = kp_next;
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
Bram Moolenaardad6b692005-01-25 22:14:34 +00004251 {
4252 kp_prev = kp;
4253 kp = kp->ke_next;
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 }
4257 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004258 hash_unlock(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259}
4260
4261/*
Bram Moolenaardad6b692005-01-25 22:14:34 +00004262 * Clear a whole keyword table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
4264 static void
Bram Moolenaardad6b692005-01-25 22:14:34 +00004265clear_keywtab(ht)
4266 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004268 hashitem_T *hi;
4269 int todo;
4270 keyentry_T *kp;
4271 keyentry_T *kp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004273 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004274 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004276 if (!HASHITEM_EMPTY(hi))
4277 {
4278 --todo;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004279 for (kp = HI2KE(hi); kp != NULL; kp = kp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004281 kp_next = kp->ke_next;
4282 vim_free(kp->next_list);
4283 vim_free(kp->k_syn.cont_in_list);
4284 vim_free(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004288 hash_clear(ht);
4289 hash_init(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290}
4291
4292/*
4293 * Add a keyword to the list of keywords.
4294 */
4295 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004296add_keyword(name, id, flags, cont_in_list, next_list, conceal_char)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u *name; /* name of keyword */
4298 int id; /* group ID for this keyword */
4299 int flags; /* flags for this keyword */
4300 short *cont_in_list; /* containedin for this keyword */
4301 short *next_list; /* nextgroup for this keyword */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004302 int conceal_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004304 keyentry_T *kp;
4305 hashtab_T *ht;
4306 hashitem_T *hi;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004307 char_u *name_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004308 long_u hash;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004309 char_u name_folded[MAXKEYWLEN + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
Bram Moolenaar860cae12010-06-05 23:22:07 +02004311 if (curwin->w_s->b_syn_ic)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004312 name_ic = str_foldcase(name, (int)STRLEN(name),
4313 name_folded, MAXKEYWLEN + 1);
4314 else
4315 name_ic = name;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004316 kp = (keyentry_T *)alloc((int)(sizeof(keyentry_T) + STRLEN(name_ic)));
4317 if (kp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004319 STRCPY(kp->keyword, name_ic);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004320 kp->k_syn.id = id;
4321 kp->k_syn.inc_tag = current_syn_inc_tag;
4322 kp->flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004323 kp->k_char = conceal_char;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004324 kp->k_syn.cont_in_list = copy_id_list(cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004326 curwin->w_s->b_syn_containedin = TRUE;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004327 kp->next_list = copy_id_list(next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
Bram Moolenaar860cae12010-06-05 23:22:07 +02004329 if (curwin->w_s->b_syn_ic)
4330 ht = &curwin->w_s->b_keywtab_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02004332 ht = &curwin->w_s->b_keywtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
Bram Moolenaardad6b692005-01-25 22:14:34 +00004334 hash = hash_hash(kp->keyword);
4335 hi = hash_lookup(ht, kp->keyword, hash);
4336 if (HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004338 /* new keyword, add to hashtable */
4339 kp->ke_next = NULL;
4340 hash_add_item(ht, hi, kp->keyword, hash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004344 /* keyword already exists, prepend to list */
4345 kp->ke_next = HI2KE(hi);
4346 hi->hi_key = KE2HIKEY(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348}
4349
4350/*
4351 * Get the start and end of the group name argument.
4352 * Return a pointer to the first argument.
4353 * Return NULL if the end of the command was found instead of further args.
4354 */
4355 static char_u *
4356get_group_name(arg, name_end)
4357 char_u *arg; /* start of the argument */
4358 char_u **name_end; /* pointer to end of the name */
4359{
4360 char_u *rest;
4361
4362 *name_end = skiptowhite(arg);
4363 rest = skipwhite(*name_end);
4364
4365 /*
4366 * Check if there are enough arguments. The first argument may be a
4367 * pattern, where '|' is allowed, so only check for NUL.
4368 */
4369 if (ends_excmd(*arg) || *rest == NUL)
4370 return NULL;
4371 return rest;
4372}
4373
4374/*
4375 * Check for syntax command option arguments.
4376 * This can be called at any place in the list of arguments, and just picks
4377 * out the arguments that are known. Can be called several times in a row to
4378 * collect all options in between other arguments.
4379 * Return a pointer to the next argument (which isn't an option).
4380 * Return NULL for any error;
4381 */
4382 static char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004383get_syn_options(arg, opt, conceal_char)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004384 char_u *arg; /* next argument to be checked */
4385 syn_opt_arg_T *opt; /* various things */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004386 int *conceal_char UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 char_u *gname_start, *gname;
4389 int syn_id;
4390 int len;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004391 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int i;
4393 int fidx;
4394 static struct flag
4395 {
4396 char *name;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004397 int argtype;
4398 int flags;
4399 } flagtab[] = { {"cCoOnNtTaAiInNeEdD", 0, HL_CONTAINED},
4400 {"oOnNeElLiInNeE", 0, HL_ONELINE},
4401 {"kKeEeEpPeEnNdD", 0, HL_KEEPEND},
4402 {"eExXtTeEnNdD", 0, HL_EXTEND},
4403 {"eExXcClLuUdDeEnNlL", 0, HL_EXCLUDENL},
4404 {"tTrRaAnNsSpPaArReEnNtT", 0, HL_TRANSP},
4405 {"sSkKiIpPnNlL", 0, HL_SKIPNL},
4406 {"sSkKiIpPwWhHiItTeE", 0, HL_SKIPWHITE},
4407 {"sSkKiIpPeEmMpPtTyY", 0, HL_SKIPEMPTY},
4408 {"gGrRoOuUpPhHeErReE", 0, HL_SYNC_HERE},
4409 {"gGrRoOuUpPtThHeErReE", 0, HL_SYNC_THERE},
4410 {"dDiIsSpPlLaAyY", 0, HL_DISPLAY},
4411 {"fFoOlLdD", 0, HL_FOLD},
Bram Moolenaar860cae12010-06-05 23:22:07 +02004412 {"cCoOnNcCeEaAlL", 0, HL_CONCEAL},
4413 {"cCoOnNcCeEaAlLeEnNdDsS", 0, HL_CONCEALENDS},
4414 {"cCcChHaArR", 11, 0},
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004415 {"cCoOnNtTaAiInNsS", 1, 0},
4416 {"cCoOnNtTaAiInNeEdDiInN", 2, 0},
4417 {"nNeExXtTgGrRoOuUpP", 3, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 };
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004419 static char *first_letters = "cCoOkKeEtTsSgGdDfFnN";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 if (arg == NULL) /* already detected error */
4422 return NULL;
4423
Bram Moolenaar860cae12010-06-05 23:22:07 +02004424#ifdef FEAT_CONCEAL
4425 if (curwin->w_s->b_syn_conceal)
4426 opt->flags |= HL_CONCEAL;
4427#endif
4428
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 for (;;)
4430 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004431 /*
4432 * This is used very often when a large number of keywords is defined.
4433 * Need to skip quickly when no option name is found.
4434 * Also avoid tolower(), it's slow.
4435 */
4436 if (strchr(first_letters, *arg) == NULL)
4437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; )
4440 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004441 p = flagtab[fidx].name;
4442 for (i = 0, len = 0; p[i] != NUL; i += 2, ++len)
4443 if (arg[len] != p[i] && arg[len] != p[i + 1])
4444 break;
4445 if (p[i] == NUL && (vim_iswhite(arg[len])
4446 || (flagtab[fidx].argtype > 0
4447 ? arg[len] == '='
4448 : ends_excmd(arg[len]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004450 if (opt->keyword
4451 && (flagtab[fidx].flags == HL_DISPLAY
4452 || flagtab[fidx].flags == HL_FOLD
4453 || flagtab[fidx].flags == HL_EXTEND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 /* treat "display", "fold" and "extend" as a keyword */
4455 fidx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457 }
4458 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004459 if (fidx < 0) /* no match found */
4460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004462 if (flagtab[fidx].argtype == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004464 if (!opt->has_cont_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
4466 EMSG(_("E395: contains argument not accepted here"));
4467 return NULL;
4468 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004469 if (get_id_list(&arg, 8, &opt->cont_list) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 return NULL;
4471 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004472 else if (flagtab[fidx].argtype == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004474#if 0 /* cannot happen */
4475 if (opt->cont_in_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 EMSG(_("E396: containedin argument not accepted here"));
4478 return NULL;
4479 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004480#endif
4481 if (get_id_list(&arg, 11, &opt->cont_in_list) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return NULL;
4483 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004484 else if (flagtab[fidx].argtype == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004486 if (get_id_list(&arg, 9, &opt->next_list) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return NULL;
4488 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004489 else if (flagtab[fidx].argtype == 11 && arg[5] == '=')
4490 {
4491#ifdef FEAT_MBYTE
4492 /* cchar=? */
4493 if (has_mbyte)
4494 {
4495# ifdef FEAT_CONCEAL
4496 *conceal_char = mb_ptr2char(arg + 6);
4497# endif
4498 arg += mb_ptr2len(arg + 6) - 1;
4499 }
4500 else
4501#endif
Bram Moolenaar56be9502010-06-06 14:20:26 +02004502 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004503#ifdef FEAT_CONCEAL
4504 *conceal_char = arg[6];
4505#else
4506 ;
4507#endif
Bram Moolenaar56be9502010-06-06 14:20:26 +02004508 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004509 arg = skipwhite(arg + 7);
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 else
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004512 {
4513 opt->flags |= flagtab[fidx].flags;
4514 arg = skipwhite(arg + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004516 if (flagtab[fidx].flags == HL_SYNC_HERE
4517 || flagtab[fidx].flags == HL_SYNC_THERE)
4518 {
4519 if (opt->sync_idx == NULL)
4520 {
4521 EMSG(_("E393: group[t]here not accepted here"));
4522 return NULL;
4523 }
4524 gname_start = arg;
4525 arg = skiptowhite(arg);
4526 if (gname_start == arg)
4527 return NULL;
4528 gname = vim_strnsave(gname_start, (int)(arg - gname_start));
4529 if (gname == NULL)
4530 return NULL;
4531 if (STRCMP(gname, "NONE") == 0)
4532 *opt->sync_idx = NONE_IDX;
4533 else
4534 {
4535 syn_id = syn_name2id(gname);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004536 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
4537 if (SYN_ITEMS(curwin->w_s)[i].sp_syn.id == syn_id
4538 && SYN_ITEMS(curwin->w_s)[i].sp_type == SPTYPE_START)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004539 {
4540 *opt->sync_idx = i;
4541 break;
4542 }
4543 if (i < 0)
4544 {
4545 EMSG2(_("E394: Didn't find region item for %s"), gname);
4546 vim_free(gname);
4547 return NULL;
4548 }
4549 }
4550
4551 vim_free(gname);
4552 arg = skipwhite(arg);
4553 }
4554#ifdef FEAT_FOLDING
4555 else if (flagtab[fidx].flags == HL_FOLD
4556 && foldmethodIsSyntax(curwin))
4557 /* Need to update folds later. */
4558 foldUpdateAll(curwin);
4559#endif
4560 }
4561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 return arg;
4564}
4565
4566/*
4567 * Adjustments to syntax item when declared in a ":syn include"'d file.
4568 * Set the contained flag, and if the item is not already contained, add it
4569 * to the specified top-level group, if any.
4570 */
4571 static void
4572syn_incl_toplevel(id, flagsp)
4573 int id;
4574 int *flagsp;
4575{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004576 if ((*flagsp & HL_CONTAINED) || curwin->w_s->b_syn_topgrp == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 return;
4578 *flagsp |= HL_CONTAINED;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004579 if (curwin->w_s->b_syn_topgrp >= SYNID_CLUSTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 /* We have to alloc this, because syn_combine_list() will free it. */
4582 short *grp_list = (short *)alloc((unsigned)(2 * sizeof(short)));
Bram Moolenaar860cae12010-06-05 23:22:07 +02004583 int tlg_id = curwin->w_s->b_syn_topgrp - SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
4585 if (grp_list != NULL)
4586 {
4587 grp_list[0] = id;
4588 grp_list[1] = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004589 syn_combine_list(&SYN_CLSTR(curwin->w_s)[tlg_id].scl_list, &grp_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 CLUSTER_ADD);
4591 }
4592 }
4593}
4594
4595/*
4596 * Handle ":syntax include [@{group-name}] filename" command.
4597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 static void
4599syn_cmd_include(eap, syncing)
4600 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004601 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 char_u *arg = eap->arg;
4604 int sgl_id = 1;
4605 char_u *group_name_end;
4606 char_u *rest;
4607 char_u *errormsg = NULL;
4608 int prev_toplvl_grp;
4609 int prev_syn_inc_tag;
4610 int source = FALSE;
4611
4612 eap->nextcmd = find_nextcmd(arg);
4613 if (eap->skip)
4614 return;
4615
4616 if (arg[0] == '@')
4617 {
4618 ++arg;
4619 rest = get_group_name(arg, &group_name_end);
4620 if (rest == NULL)
4621 {
4622 EMSG((char_u *)_("E397: Filename required"));
4623 return;
4624 }
4625 sgl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
4626 /* separate_nextcmd() and expand_filename() depend on this */
4627 eap->arg = rest;
4628 }
4629
4630 /*
4631 * Everything that's left, up to the next command, should be the
4632 * filename to include.
4633 */
4634 eap->argt |= (XFILE | NOSPC);
4635 separate_nextcmd(eap);
4636 if (*eap->arg == '<' || *eap->arg == '$' || mch_isFullName(eap->arg))
4637 {
4638 /* For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
4639 * file. Need to expand the file name first. In other cases
4640 * ":runtime!" is used. */
4641 source = TRUE;
4642 if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL)
4643 {
4644 if (errormsg != NULL)
4645 EMSG(errormsg);
4646 return;
4647 }
4648 }
4649
4650 /*
4651 * Save and restore the existing top-level grouplist id and ":syn
4652 * include" tag around the actual inclusion.
4653 */
4654 prev_syn_inc_tag = current_syn_inc_tag;
4655 current_syn_inc_tag = ++running_syn_inc_tag;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004656 prev_toplvl_grp = curwin->w_s->b_syn_topgrp;
4657 curwin->w_s->b_syn_topgrp = sgl_id;
Bram Moolenaar786b1dc2007-06-28 10:03:45 +00004658 if (source ? do_source(eap->arg, FALSE, DOSO_NONE) == FAIL
4659 : source_runtime(eap->arg, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 EMSG2(_(e_notopen), eap->arg);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004661 curwin->w_s->b_syn_topgrp = prev_toplvl_grp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 current_syn_inc_tag = prev_syn_inc_tag;
4663}
4664
4665/*
4666 * Handle ":syntax keyword {group-name} [{option}] keyword .." command.
4667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 static void
4669syn_cmd_keyword(eap, syncing)
4670 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004671 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 char_u *arg = eap->arg;
4674 char_u *group_name_end;
4675 int syn_id;
4676 char_u *rest;
4677 char_u *keyword_copy;
4678 char_u *p;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004679 char_u *kw;
4680 syn_opt_arg_T syn_opt_arg;
4681 int cnt;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004682 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684 rest = get_group_name(arg, &group_name_end);
4685
4686 if (rest != NULL)
4687 {
4688 syn_id = syn_check_group(arg, (int)(group_name_end - arg));
4689
4690 /* allocate a buffer, for removing the backslashes in the keyword */
4691 keyword_copy = alloc((unsigned)STRLEN(rest) + 1);
4692 if (keyword_copy != NULL)
4693 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004694 syn_opt_arg.flags = 0;
4695 syn_opt_arg.keyword = TRUE;
4696 syn_opt_arg.sync_idx = NULL;
4697 syn_opt_arg.has_cont_list = FALSE;
4698 syn_opt_arg.cont_in_list = NULL;
4699 syn_opt_arg.next_list = NULL;
4700
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 /*
4702 * The options given apply to ALL keywords, so all options must be
4703 * found before keywords can be created.
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004704 * 1: collect the options and copy the keywords to keyword_copy.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004706 cnt = 0;
4707 p = keyword_copy;
4708 for ( ; rest != NULL && !ends_excmd(*rest); rest = skipwhite(rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004710 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004711 if (rest == NULL || ends_excmd(*rest))
4712 break;
4713 /* Copy the keyword, removing backslashes, and add a NUL. */
4714 while (*rest != NUL && !vim_iswhite(*rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004716 if (*rest == '\\' && rest[1] != NUL)
4717 ++rest;
4718 *p++ = *rest++;
4719 }
4720 *p++ = NUL;
4721 ++cnt;
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004724 if (!eap->skip)
4725 {
4726 /* Adjust flags for use of ":syn include". */
4727 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
4728
4729 /*
4730 * 2: Add an entry for each keyword.
4731 */
4732 for (kw = keyword_copy; --cnt >= 0; kw += STRLEN(kw) + 1)
4733 {
4734 for (p = vim_strchr(kw, '['); ; )
4735 {
4736 if (p != NULL)
4737 *p = NUL;
4738 add_keyword(kw, syn_id, syn_opt_arg.flags,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004739 syn_opt_arg.cont_in_list,
4740 syn_opt_arg.next_list, conceal_char);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004741 if (p == NULL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004742 break;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004743 if (p[1] == NUL)
4744 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004745 EMSG2(_("E789: Missing ']': %s"), kw);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004746 kw = p + 2; /* skip over the NUL */
4747 break;
4748 }
4749 if (p[1] == ']')
4750 {
4751 kw = p + 1; /* skip over the "]" */
4752 break;
4753 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004754#ifdef FEAT_MBYTE
4755 if (has_mbyte)
4756 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004757 int l = (*mb_ptr2len)(p + 1);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004758
4759 mch_memmove(p, p + 1, l);
4760 p += l;
4761 }
4762 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#endif
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004764 {
4765 p[0] = p[1];
4766 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 }
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004771
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 vim_free(keyword_copy);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004773 vim_free(syn_opt_arg.cont_in_list);
4774 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 }
4777
4778 if (rest != NULL)
4779 eap->nextcmd = check_nextcmd(rest);
4780 else
4781 EMSG2(_(e_invarg2), arg);
4782
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00004783 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004784 syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785}
4786
4787/*
4788 * Handle ":syntax match {name} [{options}] {pattern} [{options}]".
4789 *
4790 * Also ":syntax sync match {name} [[grouphere | groupthere] {group-name}] .."
4791 */
4792 static void
4793syn_cmd_match(eap, syncing)
4794 exarg_T *eap;
4795 int syncing; /* TRUE for ":syntax sync match .. " */
4796{
4797 char_u *arg = eap->arg;
4798 char_u *group_name_end;
4799 char_u *rest;
4800 synpat_T item; /* the item found in the line */
4801 int syn_id;
4802 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004803 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 int sync_idx = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004805 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /* Isolate the group name, check for validity */
4808 rest = get_group_name(arg, &group_name_end);
4809
4810 /* Get options before the pattern */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004811 syn_opt_arg.flags = 0;
4812 syn_opt_arg.keyword = FALSE;
4813 syn_opt_arg.sync_idx = syncing ? &sync_idx : NULL;
4814 syn_opt_arg.has_cont_list = TRUE;
4815 syn_opt_arg.cont_list = NULL;
4816 syn_opt_arg.cont_in_list = NULL;
4817 syn_opt_arg.next_list = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004818 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 /* get the pattern. */
4821 init_syn_patterns();
4822 vim_memset(&item, 0, sizeof(item));
4823 rest = get_syn_pattern(rest, &item);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004824 if (vim_regcomp_had_eol() && !(syn_opt_arg.flags & HL_EXCLUDENL))
4825 syn_opt_arg.flags |= HL_HAS_EOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
4827 /* Get options after the pattern */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004828 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
4830 if (rest != NULL) /* all arguments are valid */
4831 {
4832 /*
4833 * Check for trailing command and illegal trailing arguments.
4834 */
4835 eap->nextcmd = check_nextcmd(rest);
4836 if (!ends_excmd(*rest) || eap->skip)
4837 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004838 else if (ga_grow(&curwin->w_s->b_syn_patterns, 1) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 && (syn_id = syn_check_group(arg,
4840 (int)(group_name_end - arg))) != 0)
4841 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004842 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /*
4844 * Store the pattern in the syn_items list
4845 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004846 idx = curwin->w_s->b_syn_patterns.ga_len;
4847 SYN_ITEMS(curwin->w_s)[idx] = item;
4848 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
4849 SYN_ITEMS(curwin->w_s)[idx].sp_type = SPTYPE_MATCH;
4850 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
4851 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag = current_syn_inc_tag;
4852 SYN_ITEMS(curwin->w_s)[idx].sp_flags = syn_opt_arg.flags;
4853 SYN_ITEMS(curwin->w_s)[idx].sp_sync_idx = sync_idx;
4854 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list = syn_opt_arg.cont_list;
4855 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004856 syn_opt_arg.cont_in_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004857#ifdef FEAT_CONCEAL
4858 SYN_ITEMS(curwin->w_s)[idx].sp_char = conceal_char;
4859#endif
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004860 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004861 curwin->w_s->b_syn_containedin = TRUE;
4862 SYN_ITEMS(curwin->w_s)[idx].sp_next_list = syn_opt_arg.next_list;
4863 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 /* remember that we found a match for syncing on */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004866 if (syn_opt_arg.flags & (HL_SYNC_HERE|HL_SYNC_THERE))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004867 curwin->w_s->b_syn_sync_flags |= SF_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004869 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004870 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871#endif
4872
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00004873 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004874 syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 return; /* don't free the progs and patterns now */
4876 }
4877 }
4878
4879 /*
4880 * Something failed, free the allocated memory.
4881 */
4882 vim_free(item.sp_prog);
4883 vim_free(item.sp_pattern);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004884 vim_free(syn_opt_arg.cont_list);
4885 vim_free(syn_opt_arg.cont_in_list);
4886 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 if (rest == NULL)
4889 EMSG2(_(e_invarg2), arg);
4890}
4891
4892/*
4893 * Handle ":syntax region {group-name} [matchgroup={group-name}]
4894 * start {start} .. [skip {skip}] end {end} .. [{options}]".
4895 */
4896 static void
4897syn_cmd_region(eap, syncing)
4898 exarg_T *eap;
4899 int syncing; /* TRUE for ":syntax sync region .." */
4900{
4901 char_u *arg = eap->arg;
4902 char_u *group_name_end;
4903 char_u *rest; /* next arg, NULL on error */
4904 char_u *key_end;
4905 char_u *key = NULL;
4906 char_u *p;
4907 int item;
4908#define ITEM_START 0
4909#define ITEM_SKIP 1
4910#define ITEM_END 2
4911#define ITEM_MATCHGROUP 3
4912 struct pat_ptr
4913 {
4914 synpat_T *pp_synp; /* pointer to syn_pattern */
4915 int pp_matchgroup_id; /* matchgroup ID */
4916 struct pat_ptr *pp_next; /* pointer to next pat_ptr */
4917 } *(pat_ptrs[3]);
4918 /* patterns found in the line */
4919 struct pat_ptr *ppp;
4920 struct pat_ptr *ppp_next;
4921 int pat_count = 0; /* nr of syn_patterns found */
4922 int syn_id;
4923 int matchgroup_id = 0;
4924 int not_enough = FALSE; /* not enough arguments */
4925 int illegal = FALSE; /* illegal arguments */
4926 int success = FALSE;
4927 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004928 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004929 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 /* Isolate the group name, check for validity */
4932 rest = get_group_name(arg, &group_name_end);
4933
4934 pat_ptrs[0] = NULL;
4935 pat_ptrs[1] = NULL;
4936 pat_ptrs[2] = NULL;
4937
4938 init_syn_patterns();
4939
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004940 syn_opt_arg.flags = 0;
4941 syn_opt_arg.keyword = FALSE;
4942 syn_opt_arg.sync_idx = NULL;
4943 syn_opt_arg.has_cont_list = TRUE;
4944 syn_opt_arg.cont_list = NULL;
4945 syn_opt_arg.cont_in_list = NULL;
4946 syn_opt_arg.next_list = NULL;
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /*
4949 * get the options, patterns and matchgroup.
4950 */
4951 while (rest != NULL && !ends_excmd(*rest))
4952 {
4953 /* Check for option arguments */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004954 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (rest == NULL || ends_excmd(*rest))
4956 break;
4957
4958 /* must be a pattern or matchgroup then */
4959 key_end = rest;
4960 while (*key_end && !vim_iswhite(*key_end) && *key_end != '=')
4961 ++key_end;
4962 vim_free(key);
4963 key = vim_strnsave_up(rest, (int)(key_end - rest));
4964 if (key == NULL) /* out of memory */
4965 {
4966 rest = NULL;
4967 break;
4968 }
4969 if (STRCMP(key, "MATCHGROUP") == 0)
4970 item = ITEM_MATCHGROUP;
4971 else if (STRCMP(key, "START") == 0)
4972 item = ITEM_START;
4973 else if (STRCMP(key, "END") == 0)
4974 item = ITEM_END;
4975 else if (STRCMP(key, "SKIP") == 0)
4976 {
4977 if (pat_ptrs[ITEM_SKIP] != NULL) /* one skip pattern allowed */
4978 {
4979 illegal = TRUE;
4980 break;
4981 }
4982 item = ITEM_SKIP;
4983 }
4984 else
4985 break;
4986 rest = skipwhite(key_end);
4987 if (*rest != '=')
4988 {
4989 rest = NULL;
4990 EMSG2(_("E398: Missing '=': %s"), arg);
4991 break;
4992 }
4993 rest = skipwhite(rest + 1);
4994 if (*rest == NUL)
4995 {
4996 not_enough = TRUE;
4997 break;
4998 }
4999
5000 if (item == ITEM_MATCHGROUP)
5001 {
5002 p = skiptowhite(rest);
5003 if ((p - rest == 4 && STRNCMP(rest, "NONE", 4) == 0) || eap->skip)
5004 matchgroup_id = 0;
5005 else
5006 {
5007 matchgroup_id = syn_check_group(rest, (int)(p - rest));
5008 if (matchgroup_id == 0)
5009 {
5010 illegal = TRUE;
5011 break;
5012 }
5013 }
5014 rest = skipwhite(p);
5015 }
5016 else
5017 {
5018 /*
5019 * Allocate room for a syn_pattern, and link it in the list of
5020 * syn_patterns for this item, at the start (because the list is
5021 * used from end to start).
5022 */
5023 ppp = (struct pat_ptr *)alloc((unsigned)sizeof(struct pat_ptr));
5024 if (ppp == NULL)
5025 {
5026 rest = NULL;
5027 break;
5028 }
5029 ppp->pp_next = pat_ptrs[item];
5030 pat_ptrs[item] = ppp;
5031 ppp->pp_synp = (synpat_T *)alloc_clear((unsigned)sizeof(synpat_T));
5032 if (ppp->pp_synp == NULL)
5033 {
5034 rest = NULL;
5035 break;
5036 }
5037
5038 /*
5039 * Get the syntax pattern and the following offset(s).
5040 */
5041 /* Enable the appropriate \z specials. */
5042 if (item == ITEM_START)
5043 reg_do_extmatch = REX_SET;
5044 else if (item == ITEM_SKIP || item == ITEM_END)
5045 reg_do_extmatch = REX_USE;
5046 rest = get_syn_pattern(rest, ppp->pp_synp);
5047 reg_do_extmatch = 0;
5048 if (item == ITEM_END && vim_regcomp_had_eol()
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005049 && !(syn_opt_arg.flags & HL_EXCLUDENL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 ppp->pp_synp->sp_flags |= HL_HAS_EOL;
5051 ppp->pp_matchgroup_id = matchgroup_id;
5052 ++pat_count;
5053 }
5054 }
5055 vim_free(key);
5056 if (illegal || not_enough)
5057 rest = NULL;
5058
5059 /*
5060 * Must have a "start" and "end" pattern.
5061 */
5062 if (rest != NULL && (pat_ptrs[ITEM_START] == NULL ||
5063 pat_ptrs[ITEM_END] == NULL))
5064 {
5065 not_enough = TRUE;
5066 rest = NULL;
5067 }
5068
5069 if (rest != NULL)
5070 {
5071 /*
5072 * Check for trailing garbage or command.
5073 * If OK, add the item.
5074 */
5075 eap->nextcmd = check_nextcmd(rest);
5076 if (!ends_excmd(*rest) || eap->skip)
5077 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005078 else if (ga_grow(&(curwin->w_s->b_syn_patterns), pat_count) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 && (syn_id = syn_check_group(arg,
5080 (int)(group_name_end - arg))) != 0)
5081 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005082 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 /*
5084 * Store the start/skip/end in the syn_items list
5085 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005086 idx = curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 for (item = ITEM_START; item <= ITEM_END; ++item)
5088 {
5089 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp->pp_next)
5090 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005091 SYN_ITEMS(curwin->w_s)[idx] = *(ppp->pp_synp);
5092 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
5093 SYN_ITEMS(curwin->w_s)[idx].sp_type =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 (item == ITEM_START) ? SPTYPE_START :
5095 (item == ITEM_SKIP) ? SPTYPE_SKIP : SPTYPE_END;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005096 SYN_ITEMS(curwin->w_s)[idx].sp_flags |= syn_opt_arg.flags;
5097 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
5098 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag = current_syn_inc_tag;
5099 SYN_ITEMS(curwin->w_s)[idx].sp_syn_match_id =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 ppp->pp_matchgroup_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005101#ifdef FEAT_CONCEAL
5102 SYN_ITEMS(curwin->w_s)[idx].sp_char = conceal_char;
5103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (item == ITEM_START)
5105 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005106 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005107 syn_opt_arg.cont_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005108 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005109 syn_opt_arg.cont_in_list;
5110 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005111 curwin->w_s->b_syn_containedin = TRUE;
5112 SYN_ITEMS(curwin->w_s)[idx].sp_next_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005113 syn_opt_arg.next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005115 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 ++idx;
5117#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005118 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005119 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#endif
5121 }
5122 }
5123
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005124 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005125 syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 success = TRUE; /* don't free the progs and patterns now */
5127 }
5128 }
5129
5130 /*
5131 * Free the allocated memory.
5132 */
5133 for (item = ITEM_START; item <= ITEM_END; ++item)
5134 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp_next)
5135 {
5136 if (!success)
5137 {
5138 vim_free(ppp->pp_synp->sp_prog);
5139 vim_free(ppp->pp_synp->sp_pattern);
5140 }
5141 vim_free(ppp->pp_synp);
5142 ppp_next = ppp->pp_next;
5143 vim_free(ppp);
5144 }
5145
5146 if (!success)
5147 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005148 vim_free(syn_opt_arg.cont_list);
5149 vim_free(syn_opt_arg.cont_in_list);
5150 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (not_enough)
5152 EMSG2(_("E399: Not enough arguments: syntax region %s"), arg);
5153 else if (illegal || rest == NULL)
5154 EMSG2(_(e_invarg2), arg);
5155 }
5156}
5157
5158/*
5159 * A simple syntax group ID comparison function suitable for use in qsort()
5160 */
5161 static int
5162#ifdef __BORLANDC__
5163_RTLENTRYF
5164#endif
5165syn_compare_stub(v1, v2)
5166 const void *v1;
5167 const void *v2;
5168{
5169 const short *s1 = v1;
5170 const short *s2 = v2;
5171
5172 return (*s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0);
5173}
5174
5175/*
5176 * Combines lists of syntax clusters.
5177 * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
5178 */
5179 static void
5180syn_combine_list(clstr1, clstr2, list_op)
5181 short **clstr1;
5182 short **clstr2;
5183 int list_op;
5184{
5185 int count1 = 0;
5186 int count2 = 0;
5187 short *g1;
5188 short *g2;
5189 short *clstr = NULL;
5190 int count;
5191 int round;
5192
5193 /*
5194 * Handle degenerate cases.
5195 */
5196 if (*clstr2 == NULL)
5197 return;
5198 if (*clstr1 == NULL || list_op == CLUSTER_REPLACE)
5199 {
5200 if (list_op == CLUSTER_REPLACE)
5201 vim_free(*clstr1);
5202 if (list_op == CLUSTER_REPLACE || list_op == CLUSTER_ADD)
5203 *clstr1 = *clstr2;
5204 else
5205 vim_free(*clstr2);
5206 return;
5207 }
5208
5209 for (g1 = *clstr1; *g1; g1++)
5210 ++count1;
5211 for (g2 = *clstr2; *g2; g2++)
5212 ++count2;
5213
5214 /*
5215 * For speed purposes, sort both lists.
5216 */
5217 qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
5218 qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
5219
5220 /*
5221 * We proceed in two passes; in round 1, we count the elements to place
5222 * in the new list, and in round 2, we allocate and populate the new
5223 * list. For speed, we use a mergesort-like method, adding the smaller
5224 * of the current elements in each list to the new list.
5225 */
5226 for (round = 1; round <= 2; round++)
5227 {
5228 g1 = *clstr1;
5229 g2 = *clstr2;
5230 count = 0;
5231
5232 /*
5233 * First, loop through the lists until one of them is empty.
5234 */
5235 while (*g1 && *g2)
5236 {
5237 /*
5238 * We always want to add from the first list.
5239 */
5240 if (*g1 < *g2)
5241 {
5242 if (round == 2)
5243 clstr[count] = *g1;
5244 count++;
5245 g1++;
5246 continue;
5247 }
5248 /*
5249 * We only want to add from the second list if we're adding the
5250 * lists.
5251 */
5252 if (list_op == CLUSTER_ADD)
5253 {
5254 if (round == 2)
5255 clstr[count] = *g2;
5256 count++;
5257 }
5258 if (*g1 == *g2)
5259 g1++;
5260 g2++;
5261 }
5262
5263 /*
5264 * Now add the leftovers from whichever list didn't get finished
5265 * first. As before, we only want to add from the second list if
5266 * we're adding the lists.
5267 */
5268 for (; *g1; g1++, count++)
5269 if (round == 2)
5270 clstr[count] = *g1;
5271 if (list_op == CLUSTER_ADD)
5272 for (; *g2; g2++, count++)
5273 if (round == 2)
5274 clstr[count] = *g2;
5275
5276 if (round == 1)
5277 {
5278 /*
5279 * If the group ended up empty, we don't need to allocate any
5280 * space for it.
5281 */
5282 if (count == 0)
5283 {
5284 clstr = NULL;
5285 break;
5286 }
5287 clstr = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
5288 if (clstr == NULL)
5289 break;
5290 clstr[count] = 0;
5291 }
5292 }
5293
5294 /*
5295 * Finally, put the new list in place.
5296 */
5297 vim_free(*clstr1);
5298 vim_free(*clstr2);
5299 *clstr1 = clstr;
5300}
5301
5302/*
5303 * Lookup a syntax cluster name and return it's ID.
5304 * If it is not found, 0 is returned.
5305 */
5306 static int
5307syn_scl_name2id(name)
5308 char_u *name;
5309{
5310 int i;
5311 char_u *name_u;
5312
5313 /* Avoid using stricmp() too much, it's slow on some systems */
5314 name_u = vim_strsave_up(name);
5315 if (name_u == NULL)
5316 return 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005317 for (i = curwin->w_s->b_syn_clusters.ga_len; --i >= 0; )
5318 if (SYN_CLSTR(curwin->w_s)[i].scl_name_u != NULL
5319 && STRCMP(name_u, SYN_CLSTR(curwin->w_s)[i].scl_name_u) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 break;
5321 vim_free(name_u);
5322 return (i < 0 ? 0 : i + SYNID_CLUSTER);
5323}
5324
5325/*
5326 * Like syn_scl_name2id(), but take a pointer + length argument.
5327 */
5328 static int
5329syn_scl_namen2id(linep, len)
5330 char_u *linep;
5331 int len;
5332{
5333 char_u *name;
5334 int id = 0;
5335
5336 name = vim_strnsave(linep, len);
5337 if (name != NULL)
5338 {
5339 id = syn_scl_name2id(name);
5340 vim_free(name);
5341 }
5342 return id;
5343}
5344
5345/*
5346 * Find syntax cluster name in the table and return it's ID.
5347 * The argument is a pointer to the name and the length of the name.
5348 * If it doesn't exist yet, a new entry is created.
5349 * Return 0 for failure.
5350 */
5351 static int
5352syn_check_cluster(pp, len)
5353 char_u *pp;
5354 int len;
5355{
5356 int id;
5357 char_u *name;
5358
5359 name = vim_strnsave(pp, len);
5360 if (name == NULL)
5361 return 0;
5362
5363 id = syn_scl_name2id(name);
5364 if (id == 0) /* doesn't exist yet */
5365 id = syn_add_cluster(name);
5366 else
5367 vim_free(name);
5368 return id;
5369}
5370
5371/*
5372 * Add new syntax cluster and return it's ID.
5373 * "name" must be an allocated string, it will be consumed.
5374 * Return 0 for failure.
5375 */
5376 static int
5377syn_add_cluster(name)
Bram Moolenaar217ad922005-03-20 22:37:15 +00005378 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
Bram Moolenaar217ad922005-03-20 22:37:15 +00005380 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
5382 /*
5383 * First call for this growarray: init growing array.
5384 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005385 if (curwin->w_s->b_syn_clusters.ga_data == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005387 curwin->w_s->b_syn_clusters.ga_itemsize = sizeof(syn_cluster_T);
5388 curwin->w_s->b_syn_clusters.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390
5391 /*
5392 * Make room for at least one other cluster entry.
5393 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005394 if (ga_grow(&curwin->w_s->b_syn_clusters, 1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 vim_free(name);
5397 return 0;
5398 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005399 len = curwin->w_s->b_syn_clusters.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar860cae12010-06-05 23:22:07 +02005401 vim_memset(&(SYN_CLSTR(curwin->w_s)[len]), 0, sizeof(syn_cluster_T));
5402 SYN_CLSTR(curwin->w_s)[len].scl_name = name;
5403 SYN_CLSTR(curwin->w_s)[len].scl_name_u = vim_strsave_up(name);
5404 SYN_CLSTR(curwin->w_s)[len].scl_list = NULL;
5405 ++curwin->w_s->b_syn_clusters.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
Bram Moolenaar217ad922005-03-20 22:37:15 +00005407 if (STRICMP(name, "Spell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005408 curwin->w_s->b_spell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005409 if (STRICMP(name, "NoSpell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005410 curwin->w_s->b_nospell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 return len + SYNID_CLUSTER;
5413}
5414
5415/*
5416 * Handle ":syntax cluster {cluster-name} [contains={groupname},..]
5417 * [add={groupname},..] [remove={groupname},..]".
5418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 static void
5420syn_cmd_cluster(eap, syncing)
5421 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005422 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 char_u *arg = eap->arg;
5425 char_u *group_name_end;
5426 char_u *rest;
5427 int scl_id;
5428 short *clstr_list;
5429 int got_clstr = FALSE;
5430 int opt_len;
5431 int list_op;
5432
5433 eap->nextcmd = find_nextcmd(arg);
5434 if (eap->skip)
5435 return;
5436
5437 rest = get_group_name(arg, &group_name_end);
5438
5439 if (rest != NULL)
5440 {
5441 scl_id = syn_check_cluster(arg, (int)(group_name_end - arg))
Bram Moolenaar217ad922005-03-20 22:37:15 +00005442 - SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444 for (;;)
5445 {
5446 if (STRNICMP(rest, "add", 3) == 0
5447 && (vim_iswhite(rest[3]) || rest[3] == '='))
5448 {
5449 opt_len = 3;
5450 list_op = CLUSTER_ADD;
5451 }
5452 else if (STRNICMP(rest, "remove", 6) == 0
5453 && (vim_iswhite(rest[6]) || rest[6] == '='))
5454 {
5455 opt_len = 6;
5456 list_op = CLUSTER_SUBTRACT;
5457 }
5458 else if (STRNICMP(rest, "contains", 8) == 0
5459 && (vim_iswhite(rest[8]) || rest[8] == '='))
5460 {
5461 opt_len = 8;
5462 list_op = CLUSTER_REPLACE;
5463 }
5464 else
5465 break;
5466
5467 clstr_list = NULL;
5468 if (get_id_list(&rest, opt_len, &clstr_list) == FAIL)
5469 {
5470 EMSG2(_(e_invarg2), rest);
5471 break;
5472 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005473 syn_combine_list(&SYN_CLSTR(curwin->w_s)[scl_id].scl_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 &clstr_list, list_op);
5475 got_clstr = TRUE;
5476 }
5477
5478 if (got_clstr)
5479 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005480 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005481 syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483 }
5484
5485 if (!got_clstr)
5486 EMSG(_("E400: No cluster specified"));
5487 if (rest == NULL || !ends_excmd(*rest))
5488 EMSG2(_(e_invarg2), arg);
5489}
5490
5491/*
5492 * On first call for current buffer: Init growing array.
5493 */
5494 static void
5495init_syn_patterns()
5496{
Bram Moolenaar860cae12010-06-05 23:22:07 +02005497 curwin->w_s->b_syn_patterns.ga_itemsize = sizeof(synpat_T);
5498 curwin->w_s->b_syn_patterns.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499}
5500
5501/*
5502 * Get one pattern for a ":syntax match" or ":syntax region" command.
5503 * Stores the pattern and program in a synpat_T.
5504 * Returns a pointer to the next argument, or NULL in case of an error.
5505 */
5506 static char_u *
5507get_syn_pattern(arg, ci)
5508 char_u *arg;
5509 synpat_T *ci;
5510{
5511 char_u *end;
5512 int *p;
5513 int idx;
5514 char_u *cpo_save;
5515
5516 /* need at least three chars */
5517 if (arg == NULL || arg[1] == NUL || arg[2] == NUL)
5518 return NULL;
5519
5520 end = skip_regexp(arg + 1, *arg, TRUE, NULL);
5521 if (*end != *arg) /* end delimiter not found */
5522 {
5523 EMSG2(_("E401: Pattern delimiter not found: %s"), arg);
5524 return NULL;
5525 }
5526 /* store the pattern and compiled regexp program */
5527 if ((ci->sp_pattern = vim_strnsave(arg + 1, (int)(end - arg - 1))) == NULL)
5528 return NULL;
5529
5530 /* Make 'cpoptions' empty, to avoid the 'l' flag */
5531 cpo_save = p_cpo;
5532 p_cpo = (char_u *)"";
5533 ci->sp_prog = vim_regcomp(ci->sp_pattern, RE_MAGIC);
5534 p_cpo = cpo_save;
5535
5536 if (ci->sp_prog == NULL)
5537 return NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005538 ci->sp_ic = curwin->w_s->b_syn_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 /*
5541 * Check for a match, highlight or region offset.
5542 */
5543 ++end;
5544 do
5545 {
5546 for (idx = SPO_COUNT; --idx >= 0; )
5547 if (STRNCMP(end, spo_name_tab[idx], 3) == 0)
5548 break;
5549 if (idx >= 0)
5550 {
5551 p = &(ci->sp_offsets[idx]);
5552 if (idx != SPO_LC_OFF)
5553 switch (end[3])
5554 {
5555 case 's': break;
5556 case 'b': break;
5557 case 'e': idx += SPO_COUNT; break;
5558 default: idx = -1; break;
5559 }
5560 if (idx >= 0)
5561 {
5562 ci->sp_off_flags |= (1 << idx);
5563 if (idx == SPO_LC_OFF) /* lc=99 */
5564 {
5565 end += 3;
5566 *p = getdigits(&end);
5567
5568 /* "lc=" offset automatically sets "ms=" offset */
5569 if (!(ci->sp_off_flags & (1 << SPO_MS_OFF)))
5570 {
5571 ci->sp_off_flags |= (1 << SPO_MS_OFF);
5572 ci->sp_offsets[SPO_MS_OFF] = *p;
5573 }
5574 }
5575 else /* yy=x+99 */
5576 {
5577 end += 4;
5578 if (*end == '+')
5579 {
5580 ++end;
5581 *p = getdigits(&end); /* positive offset */
5582 }
5583 else if (*end == '-')
5584 {
5585 ++end;
5586 *p = -getdigits(&end); /* negative offset */
5587 }
5588 }
5589 if (*end != ',')
5590 break;
5591 ++end;
5592 }
5593 }
5594 } while (idx >= 0);
5595
5596 if (!ends_excmd(*end) && !vim_iswhite(*end))
5597 {
5598 EMSG2(_("E402: Garbage after pattern: %s"), arg);
5599 return NULL;
5600 }
5601 return skipwhite(end);
5602}
5603
5604/*
5605 * Handle ":syntax sync .." command.
5606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 static void
5608syn_cmd_sync(eap, syncing)
5609 exarg_T *eap;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005610 int syncing UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 char_u *arg_start = eap->arg;
5613 char_u *arg_end;
5614 char_u *key = NULL;
5615 char_u *next_arg;
5616 int illegal = FALSE;
5617 int finished = FALSE;
5618 long n;
5619 char_u *cpo_save;
5620
5621 if (ends_excmd(*arg_start))
5622 {
5623 syn_cmd_list(eap, TRUE);
5624 return;
5625 }
5626
5627 while (!ends_excmd(*arg_start))
5628 {
5629 arg_end = skiptowhite(arg_start);
5630 next_arg = skipwhite(arg_end);
5631 vim_free(key);
5632 key = vim_strnsave_up(arg_start, (int)(arg_end - arg_start));
5633 if (STRCMP(key, "CCOMMENT") == 0)
5634 {
5635 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005636 curwin->w_s->b_syn_sync_flags |= SF_CCOMMENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (!ends_excmd(*next_arg))
5638 {
5639 arg_end = skiptowhite(next_arg);
5640 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005641 curwin->w_s->b_syn_sync_id = syn_check_group(next_arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 (int)(arg_end - next_arg));
5643 next_arg = skipwhite(arg_end);
5644 }
5645 else if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005646 curwin->w_s->b_syn_sync_id = syn_name2id((char_u *)"Comment");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648 else if ( STRNCMP(key, "LINES", 5) == 0
5649 || STRNCMP(key, "MINLINES", 8) == 0
5650 || STRNCMP(key, "MAXLINES", 8) == 0
5651 || STRNCMP(key, "LINEBREAKS", 10) == 0)
5652 {
5653 if (key[4] == 'S')
5654 arg_end = key + 6;
5655 else if (key[0] == 'L')
5656 arg_end = key + 11;
5657 else
5658 arg_end = key + 9;
5659 if (arg_end[-1] != '=' || !VIM_ISDIGIT(*arg_end))
5660 {
5661 illegal = TRUE;
5662 break;
5663 }
5664 n = getdigits(&arg_end);
5665 if (!eap->skip)
5666 {
5667 if (key[4] == 'B')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005668 curwin->w_s->b_syn_sync_linebreaks = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 else if (key[1] == 'A')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005670 curwin->w_s->b_syn_sync_maxlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02005672 curwin->w_s->b_syn_sync_minlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 }
5675 else if (STRCMP(key, "FROMSTART") == 0)
5676 {
5677 if (!eap->skip)
5678 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005679 curwin->w_s->b_syn_sync_minlines = MAXLNUM;
5680 curwin->w_s->b_syn_sync_maxlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 }
5683 else if (STRCMP(key, "LINECONT") == 0)
5684 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005685 if (curwin->w_s->b_syn_linecont_pat != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 {
5687 EMSG(_("E403: syntax sync: line continuations pattern specified twice"));
5688 finished = TRUE;
5689 break;
5690 }
5691 arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE, NULL);
5692 if (*arg_end != *next_arg) /* end delimiter not found */
5693 {
5694 illegal = TRUE;
5695 break;
5696 }
5697
5698 if (!eap->skip)
5699 {
5700 /* store the pattern and compiled regexp program */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005701 if ((curwin->w_s->b_syn_linecont_pat = vim_strnsave(next_arg + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 (int)(arg_end - next_arg - 1))) == NULL)
5703 {
5704 finished = TRUE;
5705 break;
5706 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005707 curwin->w_s->b_syn_linecont_ic = curwin->w_s->b_syn_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
5709 /* Make 'cpoptions' empty, to avoid the 'l' flag */
5710 cpo_save = p_cpo;
5711 p_cpo = (char_u *)"";
Bram Moolenaar860cae12010-06-05 23:22:07 +02005712 curwin->w_s->b_syn_linecont_prog =
5713 vim_regcomp(curwin->w_s->b_syn_linecont_pat, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 p_cpo = cpo_save;
5715
Bram Moolenaar860cae12010-06-05 23:22:07 +02005716 if (curwin->w_s->b_syn_linecont_prog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005718 vim_free(curwin->w_s->b_syn_linecont_pat);
5719 curwin->w_s->b_syn_linecont_pat = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 finished = TRUE;
5721 break;
5722 }
5723 }
5724 next_arg = skipwhite(arg_end + 1);
5725 }
5726 else
5727 {
5728 eap->arg = next_arg;
5729 if (STRCMP(key, "MATCH") == 0)
5730 syn_cmd_match(eap, TRUE);
5731 else if (STRCMP(key, "REGION") == 0)
5732 syn_cmd_region(eap, TRUE);
5733 else if (STRCMP(key, "CLEAR") == 0)
5734 syn_cmd_clear(eap, TRUE);
5735 else
5736 illegal = TRUE;
5737 finished = TRUE;
5738 break;
5739 }
5740 arg_start = next_arg;
5741 }
5742 vim_free(key);
5743 if (illegal)
5744 EMSG2(_("E404: Illegal arguments: %s"), arg_start);
5745 else if (!finished)
5746 {
5747 eap->nextcmd = check_nextcmd(arg_start);
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005748 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005749 syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751}
5752
5753/*
5754 * Convert a line of highlight group names into a list of group ID numbers.
5755 * "arg" should point to the "contains" or "nextgroup" keyword.
5756 * "arg" is advanced to after the last group name.
5757 * Careful: the argument is modified (NULs added).
5758 * returns FAIL for some error, OK for success.
5759 */
5760 static int
5761get_id_list(arg, keylen, list)
5762 char_u **arg;
5763 int keylen; /* length of keyword */
5764 short **list; /* where to store the resulting list, if not
5765 NULL, the list is silently skipped! */
5766{
5767 char_u *p = NULL;
5768 char_u *end;
5769 int round;
5770 int count;
5771 int total_count = 0;
5772 short *retval = NULL;
5773 char_u *name;
5774 regmatch_T regmatch;
5775 int id;
5776 int i;
5777 int failed = FALSE;
5778
5779 /*
5780 * We parse the list twice:
5781 * round == 1: count the number of items, allocate the array.
5782 * round == 2: fill the array with the items.
5783 * In round 1 new groups may be added, causing the number of items to
5784 * grow when a regexp is used. In that case round 1 is done once again.
5785 */
5786 for (round = 1; round <= 2; ++round)
5787 {
5788 /*
5789 * skip "contains"
5790 */
5791 p = skipwhite(*arg + keylen);
5792 if (*p != '=')
5793 {
5794 EMSG2(_("E405: Missing equal sign: %s"), *arg);
5795 break;
5796 }
5797 p = skipwhite(p + 1);
5798 if (ends_excmd(*p))
5799 {
5800 EMSG2(_("E406: Empty argument: %s"), *arg);
5801 break;
5802 }
5803
5804 /*
5805 * parse the arguments after "contains"
5806 */
5807 count = 0;
5808 while (!ends_excmd(*p))
5809 {
5810 for (end = p; *end && !vim_iswhite(*end) && *end != ','; ++end)
5811 ;
5812 name = alloc((int)(end - p + 3)); /* leave room for "^$" */
5813 if (name == NULL)
5814 {
5815 failed = TRUE;
5816 break;
5817 }
Bram Moolenaarce0842a2005-07-18 21:58:11 +00005818 vim_strncpy(name + 1, p, end - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if ( STRCMP(name + 1, "ALLBUT") == 0
5820 || STRCMP(name + 1, "ALL") == 0
5821 || STRCMP(name + 1, "TOP") == 0
5822 || STRCMP(name + 1, "CONTAINED") == 0)
5823 {
5824 if (TOUPPER_ASC(**arg) != 'C')
5825 {
5826 EMSG2(_("E407: %s not allowed here"), name + 1);
5827 failed = TRUE;
5828 vim_free(name);
5829 break;
5830 }
5831 if (count != 0)
5832 {
5833 EMSG2(_("E408: %s must be first in contains list"), name + 1);
5834 failed = TRUE;
5835 vim_free(name);
5836 break;
5837 }
5838 if (name[1] == 'A')
5839 id = SYNID_ALLBUT;
5840 else if (name[1] == 'T')
5841 id = SYNID_TOP;
5842 else
5843 id = SYNID_CONTAINED;
5844 id += current_syn_inc_tag;
5845 }
5846 else if (name[1] == '@')
5847 {
5848 id = syn_check_cluster(name + 2, (int)(end - p - 1));
5849 }
5850 else
5851 {
5852 /*
5853 * Handle full group name.
5854 */
5855 if (vim_strpbrk(name + 1, (char_u *)"\\.*^$~[") == NULL)
5856 id = syn_check_group(name + 1, (int)(end - p));
5857 else
5858 {
5859 /*
5860 * Handle match of regexp with group names.
5861 */
5862 *name = '^';
5863 STRCAT(name, "$");
5864 regmatch.regprog = vim_regcomp(name, RE_MAGIC);
5865 if (regmatch.regprog == NULL)
5866 {
5867 failed = TRUE;
5868 vim_free(name);
5869 break;
5870 }
5871
5872 regmatch.rm_ic = TRUE;
5873 id = 0;
5874 for (i = highlight_ga.ga_len; --i >= 0; )
5875 {
5876 if (vim_regexec(&regmatch, HL_TABLE()[i].sg_name,
5877 (colnr_T)0))
5878 {
5879 if (round == 2)
5880 {
5881 /* Got more items than expected; can happen
5882 * when adding items that match:
5883 * "contains=a.*b,axb".
5884 * Go back to first round */
5885 if (count >= total_count)
5886 {
5887 vim_free(retval);
5888 round = 1;
5889 }
5890 else
5891 retval[count] = i + 1;
5892 }
5893 ++count;
5894 id = -1; /* remember that we found one */
5895 }
5896 }
5897 vim_free(regmatch.regprog);
5898 }
5899 }
5900 vim_free(name);
5901 if (id == 0)
5902 {
5903 EMSG2(_("E409: Unknown group name: %s"), p);
5904 failed = TRUE;
5905 break;
5906 }
5907 if (id > 0)
5908 {
5909 if (round == 2)
5910 {
5911 /* Got more items than expected, go back to first round */
5912 if (count >= total_count)
5913 {
5914 vim_free(retval);
5915 round = 1;
5916 }
5917 else
5918 retval[count] = id;
5919 }
5920 ++count;
5921 }
5922 p = skipwhite(end);
5923 if (*p != ',')
5924 break;
5925 p = skipwhite(p + 1); /* skip comma in between arguments */
5926 }
5927 if (failed)
5928 break;
5929 if (round == 1)
5930 {
5931 retval = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
5932 if (retval == NULL)
5933 break;
5934 retval[count] = 0; /* zero means end of the list */
5935 total_count = count;
5936 }
5937 }
5938
5939 *arg = p;
5940 if (failed || retval == NULL)
5941 {
5942 vim_free(retval);
5943 return FAIL;
5944 }
5945
5946 if (*list == NULL)
5947 *list = retval;
5948 else
5949 vim_free(retval); /* list already found, don't overwrite it */
5950
5951 return OK;
5952}
5953
5954/*
5955 * Make a copy of an ID list.
5956 */
5957 static short *
5958copy_id_list(list)
5959 short *list;
5960{
5961 int len;
5962 int count;
5963 short *retval;
5964
5965 if (list == NULL)
5966 return NULL;
5967
5968 for (count = 0; list[count]; ++count)
5969 ;
5970 len = (count + 1) * sizeof(short);
5971 retval = (short *)alloc((unsigned)len);
5972 if (retval != NULL)
5973 mch_memmove(retval, list, (size_t)len);
5974
5975 return retval;
5976}
5977
5978/*
5979 * Check if syntax group "ssp" is in the ID list "list" of "cur_si".
5980 * "cur_si" can be NULL if not checking the "containedin" list.
5981 * Used to check if a syntax item is in the "contains" or "nextgroup" list of
5982 * the current item.
5983 * This function is called very often, keep it fast!!
5984 */
5985 static int
5986in_id_list(cur_si, list, ssp, contained)
5987 stateitem_T *cur_si; /* current item or NULL */
5988 short *list; /* id list */
5989 struct sp_syn *ssp; /* group id and ":syn include" tag of group */
5990 int contained; /* group id is contained */
5991{
5992 int retval;
5993 short *scl_list;
5994 short item;
5995 short id = ssp->id;
5996 static int depth = 0;
5997 int r;
5998
5999 /* If spp has a "containedin" list and "cur_si" is in it, return TRUE. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006000 if (cur_si != NULL && ssp->cont_in_list != NULL
6001 && !(cur_si->si_flags & HL_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 {
6003 /* Ignore transparent items without a contains argument. Double check
6004 * that we don't go back past the first one. */
6005 while ((cur_si->si_flags & HL_TRANS_CONT)
6006 && cur_si > (stateitem_T *)(current_state.ga_data))
6007 --cur_si;
6008 /* cur_si->si_idx is -1 for keywords, these never contain anything. */
6009 if (cur_si->si_idx >= 0 && in_id_list(NULL, ssp->cont_in_list,
Bram Moolenaar860cae12010-06-05 23:22:07 +02006010 &(SYN_ITEMS(syn_block)[cur_si->si_idx].sp_syn),
6011 SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 return TRUE;
6013 }
6014
6015 if (list == NULL)
6016 return FALSE;
6017
6018 /*
6019 * If list is ID_LIST_ALL, we are in a transparent item that isn't
6020 * inside anything. Only allow not-contained groups.
6021 */
6022 if (list == ID_LIST_ALL)
6023 return !contained;
6024
6025 /*
6026 * If the first item is "ALLBUT", return TRUE if "id" is NOT in the
6027 * contains list. We also require that "id" is at the same ":syn include"
6028 * level as the list.
6029 */
6030 item = *list;
6031 if (item >= SYNID_ALLBUT && item < SYNID_CLUSTER)
6032 {
6033 if (item < SYNID_TOP)
6034 {
6035 /* ALL or ALLBUT: accept all groups in the same file */
6036 if (item - SYNID_ALLBUT != ssp->inc_tag)
6037 return FALSE;
6038 }
6039 else if (item < SYNID_CONTAINED)
6040 {
6041 /* TOP: accept all not-contained groups in the same file */
6042 if (item - SYNID_TOP != ssp->inc_tag || contained)
6043 return FALSE;
6044 }
6045 else
6046 {
6047 /* CONTAINED: accept all contained groups in the same file */
6048 if (item - SYNID_CONTAINED != ssp->inc_tag || !contained)
6049 return FALSE;
6050 }
6051 item = *++list;
6052 retval = FALSE;
6053 }
6054 else
6055 retval = TRUE;
6056
6057 /*
6058 * Return "retval" if id is in the contains list.
6059 */
6060 while (item != 0)
6061 {
6062 if (item == id)
6063 return retval;
6064 if (item >= SYNID_CLUSTER)
6065 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006066 scl_list = SYN_CLSTR(syn_block)[item - SYNID_CLUSTER].scl_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 /* restrict recursiveness to 30 to avoid an endless loop for a
6068 * cluster that includes itself (indirectly) */
6069 if (scl_list != NULL && depth < 30)
6070 {
6071 ++depth;
6072 r = in_id_list(NULL, scl_list, ssp, contained);
6073 --depth;
6074 if (r)
6075 return retval;
6076 }
6077 }
6078 item = *++list;
6079 }
6080 return !retval;
6081}
6082
6083struct subcommand
6084{
6085 char *name; /* subcommand name */
6086 void (*func)__ARGS((exarg_T *, int)); /* function to call */
6087};
6088
6089static struct subcommand subcommands[] =
6090{
6091 {"case", syn_cmd_case},
6092 {"clear", syn_cmd_clear},
6093 {"cluster", syn_cmd_cluster},
Bram Moolenaar860cae12010-06-05 23:22:07 +02006094 {"conceal", syn_cmd_conceal},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {"enable", syn_cmd_enable},
6096 {"include", syn_cmd_include},
6097 {"keyword", syn_cmd_keyword},
6098 {"list", syn_cmd_list},
6099 {"manual", syn_cmd_manual},
6100 {"match", syn_cmd_match},
6101 {"on", syn_cmd_on},
6102 {"off", syn_cmd_off},
6103 {"region", syn_cmd_region},
6104 {"reset", syn_cmd_reset},
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006105 {"spell", syn_cmd_spell},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {"sync", syn_cmd_sync},
6107 {"", syn_cmd_list},
6108 {NULL, NULL}
6109};
6110
6111/*
6112 * ":syntax".
6113 * This searches the subcommands[] table for the subcommand name, and calls a
6114 * syntax_subcommand() function to do the rest.
6115 */
6116 void
6117ex_syntax(eap)
6118 exarg_T *eap;
6119{
6120 char_u *arg = eap->arg;
6121 char_u *subcmd_end;
6122 char_u *subcmd_name;
6123 int i;
6124
6125 syn_cmdlinep = eap->cmdlinep;
6126
6127 /* isolate subcommand name */
6128 for (subcmd_end = arg; ASCII_ISALPHA(*subcmd_end); ++subcmd_end)
6129 ;
6130 subcmd_name = vim_strnsave(arg, (int)(subcmd_end - arg));
6131 if (subcmd_name != NULL)
6132 {
6133 if (eap->skip) /* skip error messages for all subcommands */
6134 ++emsg_skip;
6135 for (i = 0; ; ++i)
6136 {
6137 if (subcommands[i].name == NULL)
6138 {
6139 EMSG2(_("E410: Invalid :syntax subcommand: %s"), subcmd_name);
6140 break;
6141 }
6142 if (STRCMP(subcmd_name, (char_u *)subcommands[i].name) == 0)
6143 {
6144 eap->arg = skipwhite(subcmd_end);
6145 (subcommands[i].func)(eap, FALSE);
6146 break;
6147 }
6148 }
6149 vim_free(subcmd_name);
6150 if (eap->skip)
6151 --emsg_skip;
6152 }
6153}
6154
Bram Moolenaar860cae12010-06-05 23:22:07 +02006155 void
6156ex_ownsyntax(eap)
6157 exarg_T *eap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
Bram Moolenaar1950c352010-06-06 15:21:10 +02006159 char_u *old_value;
6160 char_u *new_value;
6161
Bram Moolenaar860cae12010-06-05 23:22:07 +02006162 if (curwin->w_s == &curwin->w_buffer->b_s)
6163 {
6164 curwin->w_s = (synblock_T *)alloc(sizeof(synblock_T));
6165 memset(curwin->w_s, 0, sizeof(synblock_T));
6166#ifdef FEAT_SPELL
6167 curwin->w_p_spell = FALSE; /* No spell checking */
6168 clear_string_option(&curwin->w_s->b_p_spc);
6169 clear_string_option(&curwin->w_s->b_p_spf);
6170 vim_free(curwin->w_s->b_cap_prog);
6171 curwin->w_s->b_cap_prog = NULL;
6172 clear_string_option(&curwin->w_s->b_p_spl);
6173#endif
6174 }
Bram Moolenaar1950c352010-06-06 15:21:10 +02006175
6176 /* save value of b:current_syntax */
6177 old_value = get_var_value((char_u *)"b:current_syntax");
6178 if (old_value != NULL)
6179 old_value = vim_strsave(old_value);
6180
6181 /* Apply the "syntax" autocommand event, this finds and loads the syntax
6182 * file. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006183 apply_autocmds(EVENT_SYNTAX, eap->arg, curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1950c352010-06-06 15:21:10 +02006184
6185 /* move value of b:current_syntax to w:current_syntax */
6186 new_value = get_var_value((char_u *)"b:current_syntax");
6187 set_internal_string_var((char_u *)"w:current_syntax", new_value);
6188
6189 /* restore value of b:current_syntax */
6190 if (old_value != NULL)
6191 {
6192 set_internal_string_var((char_u *)"b:current_syntax", old_value);
6193 vim_free(old_value);
6194 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006195}
6196
6197 int
6198syntax_present(win)
6199 win_T *win;
6200{
6201 return (win->w_s->b_syn_patterns.ga_len != 0
6202 || win->w_s->b_syn_clusters.ga_len != 0
6203 || win->w_s->b_keywtab.ht_used > 0
6204 || win->w_s->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205}
6206
6207#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6208
6209static enum
6210{
6211 EXP_SUBCMD, /* expand ":syn" sub-commands */
6212 EXP_CASE /* expand ":syn case" arguments */
6213} expand_what;
6214
Bram Moolenaar4f688582007-07-24 12:34:30 +00006215/*
6216 * Reset include_link, include_default, include_none to 0.
6217 * Called when we are done expanding.
6218 */
6219 void
6220reset_expand_highlight()
6221{
6222 include_link = include_default = include_none = 0;
6223}
6224
6225/*
6226 * Handle command line completion for :match and :echohl command: Add "None"
6227 * as highlight group.
6228 */
6229 void
6230set_context_in_echohl_cmd(xp, arg)
6231 expand_T *xp;
6232 char_u *arg;
6233{
6234 xp->xp_context = EXPAND_HIGHLIGHT;
6235 xp->xp_pattern = arg;
6236 include_none = 1;
6237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
6239/*
6240 * Handle command line completion for :syntax command.
6241 */
6242 void
6243set_context_in_syntax_cmd(xp, arg)
6244 expand_T *xp;
6245 char_u *arg;
6246{
6247 char_u *p;
6248
6249 /* Default: expand subcommands */
6250 xp->xp_context = EXPAND_SYNTAX;
6251 expand_what = EXP_SUBCMD;
6252 xp->xp_pattern = arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00006253 include_link = 0;
6254 include_default = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
6256 /* (part of) subcommand already typed */
6257 if (*arg != NUL)
6258 {
6259 p = skiptowhite(arg);
6260 if (*p != NUL) /* past first word */
6261 {
6262 xp->xp_pattern = skipwhite(p);
6263 if (*skiptowhite(xp->xp_pattern) != NUL)
6264 xp->xp_context = EXPAND_NOTHING;
6265 else if (STRNICMP(arg, "case", p - arg) == 0)
6266 expand_what = EXP_CASE;
6267 else if ( STRNICMP(arg, "keyword", p - arg) == 0
6268 || STRNICMP(arg, "region", p - arg) == 0
6269 || STRNICMP(arg, "match", p - arg) == 0
6270 || STRNICMP(arg, "list", p - arg) == 0)
6271 xp->xp_context = EXPAND_HIGHLIGHT;
6272 else
6273 xp->xp_context = EXPAND_NOTHING;
6274 }
6275 }
6276}
6277
6278static char *(case_args[]) = {"match", "ignore", NULL};
6279
6280/*
6281 * Function given to ExpandGeneric() to obtain the list syntax names for
6282 * expansion.
6283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 char_u *
6285get_syntax_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006286 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 int idx;
6288{
6289 if (expand_what == EXP_SUBCMD)
6290 return (char_u *)subcommands[idx].name;
6291 return (char_u *)case_args[idx];
6292}
6293
6294#endif /* FEAT_CMDL_COMPL */
6295
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296/*
6297 * Function called for expression evaluation: get syntax ID at file position.
6298 */
6299 int
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006300syn_get_id(wp, lnum, col, trans, spellp, keep_state)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006301 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 long lnum;
Bram Moolenaar9d0ec2e2005-04-20 19:45:58 +00006303 colnr_T col;
Bram Moolenaarf5b63862009-12-16 17:13:44 +00006304 int trans; /* remove transparency */
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006305 int *spellp; /* return: can do spell checking */
6306 int keep_state; /* keep state of char at "col" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 /* When the position is not after the current position and in the same
6309 * line of the same buffer, need to restart parsing. */
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006310 if (wp->w_buffer != syn_buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 || lnum != current_lnum
Bram Moolenaar9d0ec2e2005-04-20 19:45:58 +00006312 || col < current_col)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006313 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
Bram Moolenaar860cae12010-06-05 23:22:07 +02006315 (void)get_syntax_attr(col, NULL, spellp, keep_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317 return (trans ? current_trans_id : current_id);
6318}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
Bram Moolenaar860cae12010-06-05 23:22:07 +02006320#if defined(FEAT_CONCEAL) || defined(PROTO)
6321/*
6322 * Return conceal substitution character
6323 */
6324 int
6325syn_get_sub_char()
6326{
6327 return current_sub_char;
6328}
6329#endif
6330
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006331#if defined(FEAT_EVAL) || defined(PROTO)
6332/*
6333 * Return the syntax ID at position "i" in the current stack.
6334 * The caller must have called syn_get_id() before to fill the stack.
6335 * Returns -1 when "i" is out of range.
6336 */
6337 int
6338syn_get_stack_item(i)
6339 int i;
6340{
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006341 if (i >= current_state.ga_len)
6342 {
6343 /* Need to invalidate the state, because we didn't properly finish it
6344 * for the last character, "keep_state" was TRUE. */
6345 invalidate_current_state();
6346 current_col = MAXCOL;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006347 return -1;
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006348 }
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006349 return CUR_STATE(i).si_id;
6350}
6351#endif
6352
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353#if defined(FEAT_FOLDING) || defined(PROTO)
6354/*
6355 * Function called to get folding level for line "lnum" in window "wp".
6356 */
6357 int
6358syn_get_foldlevel(wp, lnum)
6359 win_T *wp;
6360 long lnum;
6361{
6362 int level = 0;
6363 int i;
6364
6365 /* Return quickly when there are no fold items at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006366 if (wp->w_s->b_syn_folditems != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
6368 syntax_start(wp, lnum);
6369
6370 for (i = 0; i < current_state.ga_len; ++i)
6371 if (CUR_STATE(i).si_flags & HL_FOLD)
6372 ++level;
6373 }
6374 if (level > wp->w_p_fdn)
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 level = wp->w_p_fdn;
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006377 if (level < 0)
6378 level = 0;
6379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 return level;
6381}
6382#endif
6383
6384#endif /* FEAT_SYN_HL */
6385
6386
6387/**************************************
6388 * Highlighting stuff *
6389 **************************************/
6390
6391/*
6392 * The default highlight groups. These are compiled-in for fast startup and
6393 * they still work when the runtime files can't be found.
6394 * When making changes here, also change runtime/colors/default.vim!
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006395 * The #ifdefs are needed to reduce the amount of static data. Helps to make
6396 * the 16 bit DOS (museum) version compile.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006398#ifdef FEAT_GUI
6399# define CENT(a, b) b
6400#else
6401# define CENT(a, b) a
6402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403static char *(highlight_init_both[]) =
6404 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006405 CENT("ErrorMsg term=standout ctermbg=DarkRed ctermfg=White",
6406 "ErrorMsg term=standout ctermbg=DarkRed ctermfg=White guibg=Red guifg=White"),
6407 CENT("IncSearch term=reverse cterm=reverse",
6408 "IncSearch term=reverse cterm=reverse gui=reverse"),
6409 CENT("ModeMsg term=bold cterm=bold",
6410 "ModeMsg term=bold cterm=bold gui=bold"),
6411 CENT("NonText term=bold ctermfg=Blue",
6412 "NonText term=bold ctermfg=Blue gui=bold guifg=Blue"),
6413 CENT("StatusLine term=reverse,bold cterm=reverse,bold",
6414 "StatusLine term=reverse,bold cterm=reverse,bold gui=reverse,bold"),
6415 CENT("StatusLineNC term=reverse cterm=reverse",
6416 "StatusLineNC term=reverse cterm=reverse gui=reverse"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006417#ifdef FEAT_VERTSPLIT
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006418 CENT("VertSplit term=reverse cterm=reverse",
6419 "VertSplit term=reverse cterm=reverse gui=reverse"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006420#endif
6421#ifdef FEAT_CLIPBOARD
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006422 CENT("VisualNOS term=underline,bold cterm=underline,bold",
6423 "VisualNOS term=underline,bold cterm=underline,bold gui=underline,bold"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006424#endif
6425#ifdef FEAT_DIFF
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006426 CENT("DiffText term=reverse cterm=bold ctermbg=Red",
6427 "DiffText term=reverse cterm=bold ctermbg=Red gui=bold guibg=Red"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006428#endif
6429#ifdef FEAT_INS_EXPAND
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006430 CENT("PmenuThumb cterm=reverse",
6431 "PmenuThumb cterm=reverse gui=reverse"),
6432 CENT("PmenuSbar ctermbg=Grey",
6433 "PmenuSbar ctermbg=Grey guibg=Grey"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006434#endif
6435#ifdef FEAT_WINDOWS
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006436 CENT("TabLineSel term=bold cterm=bold",
6437 "TabLineSel term=bold cterm=bold gui=bold"),
6438 CENT("TabLineFill term=reverse cterm=reverse",
6439 "TabLineFill term=reverse cterm=reverse gui=reverse"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441#ifdef FEAT_GUI
6442 "Cursor guibg=fg guifg=bg",
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006443 "lCursor guibg=fg guifg=bg", /* should be different, but what? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 NULL
6446 };
6447
6448static char *(highlight_init_light[]) =
6449 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006450 CENT("Directory term=bold ctermfg=DarkBlue",
6451 "Directory term=bold ctermfg=DarkBlue guifg=Blue"),
6452 CENT("LineNr term=underline ctermfg=Brown",
6453 "LineNr term=underline ctermfg=Brown guifg=Brown"),
6454 CENT("MoreMsg term=bold ctermfg=DarkGreen",
6455 "MoreMsg term=bold ctermfg=DarkGreen gui=bold guifg=SeaGreen"),
6456 CENT("Question term=standout ctermfg=DarkGreen",
6457 "Question term=standout ctermfg=DarkGreen gui=bold guifg=SeaGreen"),
6458 CENT("Search term=reverse ctermbg=Yellow ctermfg=NONE",
6459 "Search term=reverse ctermbg=Yellow ctermfg=NONE guibg=Yellow guifg=NONE"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006460#ifdef FEAT_SPELL
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006461 CENT("SpellBad term=reverse ctermbg=LightRed",
6462 "SpellBad term=reverse ctermbg=LightRed guisp=Red gui=undercurl"),
6463 CENT("SpellCap term=reverse ctermbg=LightBlue",
6464 "SpellCap term=reverse ctermbg=LightBlue guisp=Blue gui=undercurl"),
6465 CENT("SpellRare term=reverse ctermbg=LightMagenta",
6466 "SpellRare term=reverse ctermbg=LightMagenta guisp=Magenta gui=undercurl"),
6467 CENT("SpellLocal term=underline ctermbg=Cyan",
6468 "SpellLocal term=underline ctermbg=Cyan guisp=DarkCyan gui=undercurl"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006469#endif
6470#ifdef FEAT_INS_EXPAND
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006471 CENT("Pmenu ctermbg=LightMagenta",
6472 "Pmenu ctermbg=LightMagenta guibg=LightMagenta"),
6473 CENT("PmenuSel ctermbg=LightGrey",
6474 "PmenuSel ctermbg=LightGrey guibg=Grey"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006475#endif
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006476 CENT("SpecialKey term=bold ctermfg=DarkBlue",
6477 "SpecialKey term=bold ctermfg=DarkBlue guifg=Blue"),
6478 CENT("Title term=bold ctermfg=DarkMagenta",
6479 "Title term=bold ctermfg=DarkMagenta gui=bold guifg=Magenta"),
6480 CENT("WarningMsg term=standout ctermfg=DarkRed",
6481 "WarningMsg term=standout ctermfg=DarkRed guifg=Red"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006482#ifdef FEAT_WILDMENU
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006483 CENT("WildMenu term=standout ctermbg=Yellow ctermfg=Black",
6484 "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006485#endif
6486#ifdef FEAT_FOLDING
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006487 CENT("Folded term=standout ctermbg=Grey ctermfg=DarkBlue",
6488 "Folded term=standout ctermbg=Grey ctermfg=DarkBlue guibg=LightGrey guifg=DarkBlue"),
6489 CENT("FoldColumn term=standout ctermbg=Grey ctermfg=DarkBlue",
6490 "FoldColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006491#endif
6492#ifdef FEAT_SIGNS
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006493 CENT("SignColumn term=standout ctermbg=Grey ctermfg=DarkBlue",
6494 "SignColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006495#endif
6496#ifdef FEAT_VISUAL
Bram Moolenaarc1e37902006-04-18 21:55:01 +00006497 CENT("Visual term=reverse",
6498 "Visual term=reverse guibg=LightGrey"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006499#endif
6500#ifdef FEAT_DIFF
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006501 CENT("DiffAdd term=bold ctermbg=LightBlue",
6502 "DiffAdd term=bold ctermbg=LightBlue guibg=LightBlue"),
6503 CENT("DiffChange term=bold ctermbg=LightMagenta",
6504 "DiffChange term=bold ctermbg=LightMagenta guibg=LightMagenta"),
6505 CENT("DiffDelete term=bold ctermfg=Blue ctermbg=LightCyan",
6506 "DiffDelete term=bold ctermfg=Blue ctermbg=LightCyan gui=bold guifg=Blue guibg=LightCyan"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006507#endif
6508#ifdef FEAT_WINDOWS
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006509 CENT("TabLine term=underline cterm=underline ctermfg=black ctermbg=LightGrey",
6510 "TabLine term=underline cterm=underline ctermfg=black ctermbg=LightGrey gui=underline guibg=LightGrey"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006511#endif
6512#ifdef FEAT_SYN_HL
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006513 CENT("CursorColumn term=reverse ctermbg=LightGrey",
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006514 "CursorColumn term=reverse ctermbg=LightGrey guibg=Grey90"),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006515 CENT("CursorLine term=underline cterm=underline",
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006516 "CursorLine term=underline cterm=underline guibg=Grey90"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006517#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006518#ifdef FEAT_CONCEAL
6519 CENT("Conceal ctermbg=DarkGrey ctermfg=LightGrey",
6520 "Conceal ctermbg=DarkGrey ctermfg=LightGrey guibg=DarkGrey guifg=LightGrey"),
6521#endif
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006522#ifdef FEAT_AUTOCMD
6523 CENT("MatchParen term=reverse ctermbg=Cyan",
6524 "MatchParen term=reverse ctermbg=Cyan guibg=Cyan"),
6525#endif
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006526#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 "Normal gui=NONE",
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 NULL
6530 };
6531
6532static char *(highlight_init_dark[]) =
6533 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006534 CENT("Directory term=bold ctermfg=LightCyan",
6535 "Directory term=bold ctermfg=LightCyan guifg=Cyan"),
6536 CENT("LineNr term=underline ctermfg=Yellow",
6537 "LineNr term=underline ctermfg=Yellow guifg=Yellow"),
6538 CENT("MoreMsg term=bold ctermfg=LightGreen",
6539 "MoreMsg term=bold ctermfg=LightGreen gui=bold guifg=SeaGreen"),
6540 CENT("Question term=standout ctermfg=LightGreen",
6541 "Question term=standout ctermfg=LightGreen gui=bold guifg=Green"),
6542 CENT("Search term=reverse ctermbg=Yellow ctermfg=Black",
6543 "Search term=reverse ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black"),
6544 CENT("SpecialKey term=bold ctermfg=LightBlue",
6545 "SpecialKey term=bold ctermfg=LightBlue guifg=Cyan"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006546#ifdef FEAT_SPELL
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006547 CENT("SpellBad term=reverse ctermbg=Red",
6548 "SpellBad term=reverse ctermbg=Red guisp=Red gui=undercurl"),
6549 CENT("SpellCap term=reverse ctermbg=Blue",
6550 "SpellCap term=reverse ctermbg=Blue guisp=Blue gui=undercurl"),
6551 CENT("SpellRare term=reverse ctermbg=Magenta",
6552 "SpellRare term=reverse ctermbg=Magenta guisp=Magenta gui=undercurl"),
6553 CENT("SpellLocal term=underline ctermbg=Cyan",
6554 "SpellLocal term=underline ctermbg=Cyan guisp=Cyan gui=undercurl"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006555#endif
6556#ifdef FEAT_INS_EXPAND
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006557 CENT("Pmenu ctermbg=Magenta",
6558 "Pmenu ctermbg=Magenta guibg=Magenta"),
6559 CENT("PmenuSel ctermbg=DarkGrey",
6560 "PmenuSel ctermbg=DarkGrey guibg=DarkGrey"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006561#endif
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006562 CENT("Title term=bold ctermfg=LightMagenta",
6563 "Title term=bold ctermfg=LightMagenta gui=bold guifg=Magenta"),
6564 CENT("WarningMsg term=standout ctermfg=LightRed",
6565 "WarningMsg term=standout ctermfg=LightRed guifg=Red"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006566#ifdef FEAT_WILDMENU
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006567 CENT("WildMenu term=standout ctermbg=Yellow ctermfg=Black",
6568 "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006569#endif
6570#ifdef FEAT_FOLDING
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006571 CENT("Folded term=standout ctermbg=DarkGrey ctermfg=Cyan",
6572 "Folded term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=DarkGrey guifg=Cyan"),
6573 CENT("FoldColumn term=standout ctermbg=DarkGrey ctermfg=Cyan",
6574 "FoldColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006575#endif
6576#ifdef FEAT_SIGNS
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006577 CENT("SignColumn term=standout ctermbg=DarkGrey ctermfg=Cyan",
6578 "SignColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006579#endif
6580#ifdef FEAT_VISUAL
Bram Moolenaarc1e37902006-04-18 21:55:01 +00006581 CENT("Visual term=reverse",
6582 "Visual term=reverse guibg=DarkGrey"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006583#endif
6584#ifdef FEAT_DIFF
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006585 CENT("DiffAdd term=bold ctermbg=DarkBlue",
6586 "DiffAdd term=bold ctermbg=DarkBlue guibg=DarkBlue"),
6587 CENT("DiffChange term=bold ctermbg=DarkMagenta",
6588 "DiffChange term=bold ctermbg=DarkMagenta guibg=DarkMagenta"),
6589 CENT("DiffDelete term=bold ctermfg=Blue ctermbg=DarkCyan",
6590 "DiffDelete term=bold ctermfg=Blue ctermbg=DarkCyan gui=bold guifg=Blue guibg=DarkCyan"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006591#endif
6592#ifdef FEAT_WINDOWS
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006593 CENT("TabLine term=underline cterm=underline ctermfg=white ctermbg=DarkGrey",
6594 "TabLine term=underline cterm=underline ctermfg=white ctermbg=DarkGrey gui=underline guibg=DarkGrey"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006595#endif
6596#ifdef FEAT_SYN_HL
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006597 CENT("CursorColumn term=reverse ctermbg=DarkGrey",
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006598 "CursorColumn term=reverse ctermbg=DarkGrey guibg=Grey40"),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006599 CENT("CursorLine term=underline cterm=underline",
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006600 "CursorLine term=underline cterm=underline guibg=Grey40"),
6601#endif
6602#ifdef FEAT_AUTOCMD
6603 CENT("MatchParen term=reverse ctermbg=DarkCyan",
6604 "MatchParen term=reverse ctermbg=DarkCyan guibg=DarkCyan"),
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006605#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006606#ifdef FEAT_CONCEAL
6607 CENT("Conceal ctermbg=DarkGrey ctermfg=LightGrey",
6608 "Conceal ctermbg=DarkGrey ctermfg=LightGrey guibg=DarkGrey guifg=LightGrey"),
6609#endif
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006610#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 "Normal gui=NONE",
Bram Moolenaar42bbef42006-03-25 22:02:07 +00006612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 NULL
6614 };
6615
6616 void
6617init_highlight(both, reset)
6618 int both; /* include groups where 'bg' doesn't matter */
6619 int reset; /* clear group first */
6620{
6621 int i;
6622 char **pp;
6623 static int had_both = FALSE;
6624#ifdef FEAT_EVAL
6625 char_u *p;
6626
6627 /*
6628 * Try finding the color scheme file. Used when a color file was loaded
6629 * and 'background' or 't_Co' is changed.
6630 */
6631 p = get_var_value((char_u *)"g:colors_name");
6632 if (p != NULL && load_colors(p) == OK)
6633 return;
6634#endif
6635
6636 /*
6637 * Didn't use a color file, use the compiled-in colors.
6638 */
6639 if (both)
6640 {
6641 had_both = TRUE;
6642 pp = highlight_init_both;
6643 for (i = 0; pp[i] != NULL; ++i)
6644 do_highlight((char_u *)pp[i], reset, TRUE);
6645 }
6646 else if (!had_both)
6647 /* Don't do anything before the call with both == TRUE from main().
6648 * Not everything has been setup then, and that call will overrule
6649 * everything anyway. */
6650 return;
6651
6652 if (*p_bg == 'l')
6653 pp = highlight_init_light;
6654 else
6655 pp = highlight_init_dark;
6656 for (i = 0; pp[i] != NULL; ++i)
6657 do_highlight((char_u *)pp[i], reset, TRUE);
6658
Bram Moolenaarc1e37902006-04-18 21:55:01 +00006659 /* Reverse looks ugly, but grey may not work for 8 colors. Thus let it
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006660 * depend on the number of colors available.
6661 * With 8 colors brown is equal to yellow, need to use black for Search fg
Bram Moolenaarda40c852008-08-06 13:28:57 +00006662 * to avoid Statement highlighted text disappears.
6663 * Clear the attributes, needed when changing the t_Co value. */
Bram Moolenaarab194812005-09-14 21:40:12 +00006664 if (t_colors > 8)
Bram Moolenaarda40c852008-08-06 13:28:57 +00006665 do_highlight((char_u *)(*p_bg == 'l'
6666 ? "Visual cterm=NONE ctermbg=LightGrey"
6667 : "Visual cterm=NONE ctermbg=DarkGrey"), FALSE, TRUE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00006668 else
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006669 {
Bram Moolenaarda40c852008-08-06 13:28:57 +00006670 do_highlight((char_u *)"Visual cterm=reverse ctermbg=NONE",
6671 FALSE, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006672 if (*p_bg == 'l')
6673 do_highlight((char_u *)"Search ctermfg=black", FALSE, TRUE);
6674 }
Bram Moolenaarab194812005-09-14 21:40:12 +00006675
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676#ifdef FEAT_SYN_HL
6677 /*
6678 * If syntax highlighting is enabled load the highlighting for it.
6679 */
6680 if (get_var_value((char_u *)"g:syntax_on") != NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006681 {
6682 static int recursive = 0;
6683
6684 if (recursive >= 5)
6685 EMSG(_("E679: recursive loop loading syncolor.vim"));
6686 else
6687 {
6688 ++recursive;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00006689 (void)source_runtime((char_u *)"syntax/syncolor.vim", TRUE);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006690 --recursive;
6691 }
6692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693#endif
6694}
6695
6696/*
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006697 * Load color file "name".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 * Return OK for success, FAIL for failure.
6699 */
6700 int
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006701load_colors(name)
6702 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 char_u *buf;
6705 int retval = FAIL;
6706 static int recursive = FALSE;
6707
6708 /* When being called recursively, this is probably because setting
6709 * 'background' caused the highlighting to be reloaded. This means it is
6710 * working, thus we should return OK. */
6711 if (recursive)
6712 return OK;
6713
6714 recursive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006715 buf = alloc((unsigned)(STRLEN(name) + 12));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 if (buf != NULL)
6717 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006718 sprintf((char *)buf, "colors/%s.vim", name);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00006719 retval = source_runtime(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 vim_free(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006721#ifdef FEAT_AUTOCMD
6722 apply_autocmds(EVENT_COLORSCHEME, NULL, NULL, FALSE, curbuf);
6723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
6725 recursive = FALSE;
6726
6727 return retval;
6728}
6729
6730/*
6731 * Handle the ":highlight .." command.
6732 * When using ":hi clear" this is called recursively for each group with
6733 * "forceit" and "init" both TRUE.
6734 */
6735 void
6736do_highlight(line, forceit, init)
6737 char_u *line;
6738 int forceit;
6739 int init; /* TRUE when called for initializing */
6740{
6741 char_u *name_end;
6742 char_u *p;
6743 char_u *linep;
6744 char_u *key_start;
6745 char_u *arg_start;
6746 char_u *key = NULL, *arg = NULL;
6747 long i;
6748 int off;
6749 int len;
6750 int attr;
6751 int id;
6752 int idx;
6753 int dodefault = FALSE;
6754 int doclear = FALSE;
6755 int dolink = FALSE;
6756 int error = FALSE;
6757 int color;
6758 int is_normal_group = FALSE; /* "Normal" group */
6759#ifdef FEAT_GUI_X11
6760 int is_menu_group = FALSE; /* "Menu" group */
6761 int is_scrollbar_group = FALSE; /* "Scrollbar" group */
6762 int is_tooltip_group = FALSE; /* "Tooltip" group */
6763 int do_colors = FALSE; /* need to update colors? */
6764#else
6765# define is_menu_group 0
6766# define is_tooltip_group 0
6767#endif
6768
6769 /*
6770 * If no argument, list current highlighting.
6771 */
6772 if (ends_excmd(*line))
6773 {
6774 for (i = 1; i <= highlight_ga.ga_len && !got_int; ++i)
6775 /* TODO: only call when the group has attributes set */
6776 highlight_list_one((int)i);
6777 return;
6778 }
6779
6780 /*
6781 * Isolate the name.
6782 */
6783 name_end = skiptowhite(line);
6784 linep = skipwhite(name_end);
6785
6786 /*
6787 * Check for "default" argument.
6788 */
6789 if (STRNCMP(line, "default", name_end - line) == 0)
6790 {
6791 dodefault = TRUE;
6792 line = linep;
6793 name_end = skiptowhite(line);
6794 linep = skipwhite(name_end);
6795 }
6796
6797 /*
6798 * Check for "clear" or "link" argument.
6799 */
6800 if (STRNCMP(line, "clear", name_end - line) == 0)
6801 doclear = TRUE;
6802 if (STRNCMP(line, "link", name_end - line) == 0)
6803 dolink = TRUE;
6804
6805 /*
6806 * ":highlight {group-name}": list highlighting for one group.
6807 */
6808 if (!doclear && !dolink && ends_excmd(*linep))
6809 {
6810 id = syn_namen2id(line, (int)(name_end - line));
6811 if (id == 0)
6812 EMSG2(_("E411: highlight group not found: %s"), line);
6813 else
6814 highlight_list_one(id);
6815 return;
6816 }
6817
6818 /*
6819 * Handle ":highlight link {from} {to}" command.
6820 */
6821 if (dolink)
6822 {
6823 char_u *from_start = linep;
6824 char_u *from_end;
6825 char_u *to_start;
6826 char_u *to_end;
6827 int from_id;
6828 int to_id;
6829
6830 from_end = skiptowhite(from_start);
6831 to_start = skipwhite(from_end);
6832 to_end = skiptowhite(to_start);
6833
6834 if (ends_excmd(*from_start) || ends_excmd(*to_start))
6835 {
6836 EMSG2(_("E412: Not enough arguments: \":highlight link %s\""),
6837 from_start);
6838 return;
6839 }
6840
6841 if (!ends_excmd(*skipwhite(to_end)))
6842 {
6843 EMSG2(_("E413: Too many arguments: \":highlight link %s\""), from_start);
6844 return;
6845 }
6846
6847 from_id = syn_check_group(from_start, (int)(from_end - from_start));
6848 if (STRNCMP(to_start, "NONE", 4) == 0)
6849 to_id = 0;
6850 else
6851 to_id = syn_check_group(to_start, (int)(to_end - to_start));
6852
6853 if (from_id > 0 && (!init || HL_TABLE()[from_id - 1].sg_set == 0))
6854 {
6855 /*
6856 * Don't allow a link when there already is some highlighting
6857 * for the group, unless '!' is used
6858 */
6859 if (to_id > 0 && !forceit && !init
6860 && hl_has_settings(from_id - 1, dodefault))
6861 {
6862 if (sourcing_name == NULL && !dodefault)
6863 EMSG(_("E414: group has settings, highlight link ignored"));
6864 }
6865 else
6866 {
6867 if (!init)
6868 HL_TABLE()[from_id - 1].sg_set |= SG_LINK;
6869 HL_TABLE()[from_id - 1].sg_link = to_id;
Bram Moolenaar661b1822005-07-28 22:36:45 +00006870#ifdef FEAT_EVAL
6871 HL_TABLE()[from_id - 1].sg_scriptID = current_SID;
6872#endif
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00006873 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875 }
6876
6877 /* Only call highlight_changed() once, after sourcing a syntax file */
6878 need_highlight_changed = TRUE;
6879
6880 return;
6881 }
6882
6883 if (doclear)
6884 {
6885 /*
6886 * ":highlight clear [group]" command.
6887 */
6888 line = linep;
6889 if (ends_excmd(*line))
6890 {
6891#ifdef FEAT_GUI
6892 /* First, we do not destroy the old values, but allocate the new
6893 * ones and update the display. THEN we destroy the old values.
6894 * If we destroy the old values first, then the old values
6895 * (such as GuiFont's or GuiFontset's) will still be displayed but
6896 * invalid because they were free'd.
6897 */
6898 if (gui.in_use)
6899 {
6900# ifdef FEAT_BEVAL_TIP
6901 gui_init_tooltip_font();
6902# endif
6903# if defined(FEAT_MENU) && (defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MOTIF))
6904 gui_init_menu_font();
6905# endif
6906 }
6907# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_X11)
6908 gui_mch_def_colors();
6909# endif
6910# ifdef FEAT_GUI_X11
6911# ifdef FEAT_MENU
6912
6913 /* This only needs to be done when there is no Menu highlight
6914 * group defined by default, which IS currently the case.
6915 */
6916 gui_mch_new_menu_colors();
6917# endif
6918 if (gui.in_use)
6919 {
6920 gui_new_scrollbar_colors();
6921# ifdef FEAT_BEVAL
6922 gui_mch_new_tooltip_colors();
6923# endif
6924# ifdef FEAT_MENU
6925 gui_mch_new_menu_font();
6926# endif
6927 }
6928# endif
6929
6930 /* Ok, we're done allocating the new default graphics items.
6931 * The screen should already be refreshed at this point.
6932 * It is now Ok to clear out the old data.
6933 */
6934#endif
6935#ifdef FEAT_EVAL
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006936 do_unlet((char_u *)"colors_name", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937#endif
6938 restore_cterm_colors();
6939
6940 /*
6941 * Clear all default highlight groups and load the defaults.
6942 */
6943 for (idx = 0; idx < highlight_ga.ga_len; ++idx)
6944 highlight_clear(idx);
6945 init_highlight(TRUE, TRUE);
6946#ifdef FEAT_GUI
6947 if (gui.in_use)
6948 highlight_gui_started();
6949#endif
6950 highlight_changed();
6951 redraw_later_clear();
6952 return;
6953 }
6954 name_end = skiptowhite(line);
6955 linep = skipwhite(name_end);
6956 }
6957
6958 /*
6959 * Find the group name in the table. If it does not exist yet, add it.
6960 */
6961 id = syn_check_group(line, (int)(name_end - line));
6962 if (id == 0) /* failed (out of memory) */
6963 return;
6964 idx = id - 1; /* index is ID minus one */
6965
6966 /* Return if "default" was used and the group already has settings. */
6967 if (dodefault && hl_has_settings(idx, TRUE))
6968 return;
6969
6970 if (STRCMP(HL_TABLE()[idx].sg_name_u, "NORMAL") == 0)
6971 is_normal_group = TRUE;
6972#ifdef FEAT_GUI_X11
6973 else if (STRCMP(HL_TABLE()[idx].sg_name_u, "MENU") == 0)
6974 is_menu_group = TRUE;
6975 else if (STRCMP(HL_TABLE()[idx].sg_name_u, "SCROLLBAR") == 0)
6976 is_scrollbar_group = TRUE;
6977 else if (STRCMP(HL_TABLE()[idx].sg_name_u, "TOOLTIP") == 0)
6978 is_tooltip_group = TRUE;
6979#endif
6980
6981 /* Clear the highlighting for ":hi clear {group}" and ":hi clear". */
6982 if (doclear || (forceit && init))
6983 {
6984 highlight_clear(idx);
6985 if (!doclear)
6986 HL_TABLE()[idx].sg_set = 0;
6987 }
6988
6989 if (!doclear)
6990 while (!ends_excmd(*linep))
6991 {
6992 key_start = linep;
6993 if (*linep == '=')
6994 {
6995 EMSG2(_("E415: unexpected equal sign: %s"), key_start);
6996 error = TRUE;
6997 break;
6998 }
6999
7000 /*
7001 * Isolate the key ("term", "ctermfg", "ctermbg", "font", "guifg" or
7002 * "guibg").
7003 */
7004 while (*linep && !vim_iswhite(*linep) && *linep != '=')
7005 ++linep;
7006 vim_free(key);
7007 key = vim_strnsave_up(key_start, (int)(linep - key_start));
7008 if (key == NULL)
7009 {
7010 error = TRUE;
7011 break;
7012 }
7013 linep = skipwhite(linep);
7014
7015 if (STRCMP(key, "NONE") == 0)
7016 {
7017 if (!init || HL_TABLE()[idx].sg_set == 0)
7018 {
7019 if (!init)
7020 HL_TABLE()[idx].sg_set |= SG_TERM+SG_CTERM+SG_GUI;
7021 highlight_clear(idx);
7022 }
7023 continue;
7024 }
7025
7026 /*
7027 * Check for the equal sign.
7028 */
7029 if (*linep != '=')
7030 {
7031 EMSG2(_("E416: missing equal sign: %s"), key_start);
7032 error = TRUE;
7033 break;
7034 }
7035 ++linep;
7036
7037 /*
7038 * Isolate the argument.
7039 */
7040 linep = skipwhite(linep);
7041 if (*linep == '\'') /* guifg='color name' */
7042 {
7043 arg_start = ++linep;
7044 linep = vim_strchr(linep, '\'');
7045 if (linep == NULL)
7046 {
7047 EMSG2(_(e_invarg2), key_start);
7048 error = TRUE;
7049 break;
7050 }
7051 }
7052 else
7053 {
7054 arg_start = linep;
7055 linep = skiptowhite(linep);
7056 }
7057 if (linep == arg_start)
7058 {
7059 EMSG2(_("E417: missing argument: %s"), key_start);
7060 error = TRUE;
7061 break;
7062 }
7063 vim_free(arg);
7064 arg = vim_strnsave(arg_start, (int)(linep - arg_start));
7065 if (arg == NULL)
7066 {
7067 error = TRUE;
7068 break;
7069 }
7070 if (*linep == '\'')
7071 ++linep;
7072
7073 /*
7074 * Store the argument.
7075 */
7076 if ( STRCMP(key, "TERM") == 0
7077 || STRCMP(key, "CTERM") == 0
7078 || STRCMP(key, "GUI") == 0)
7079 {
7080 attr = 0;
7081 off = 0;
7082 while (arg[off] != NUL)
7083 {
7084 for (i = sizeof(hl_attr_table) / sizeof(int); --i >= 0; )
7085 {
7086 len = (int)STRLEN(hl_name_table[i]);
7087 if (STRNICMP(arg + off, hl_name_table[i], len) == 0)
7088 {
7089 attr |= hl_attr_table[i];
7090 off += len;
7091 break;
7092 }
7093 }
7094 if (i < 0)
7095 {
7096 EMSG2(_("E418: Illegal value: %s"), arg);
7097 error = TRUE;
7098 break;
7099 }
7100 if (arg[off] == ',') /* another one follows */
7101 ++off;
7102 }
7103 if (error)
7104 break;
7105 if (*key == 'T')
7106 {
7107 if (!init || !(HL_TABLE()[idx].sg_set & SG_TERM))
7108 {
7109 if (!init)
7110 HL_TABLE()[idx].sg_set |= SG_TERM;
7111 HL_TABLE()[idx].sg_term = attr;
7112 }
7113 }
7114 else if (*key == 'C')
7115 {
7116 if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
7117 {
7118 if (!init)
7119 HL_TABLE()[idx].sg_set |= SG_CTERM;
7120 HL_TABLE()[idx].sg_cterm = attr;
7121 HL_TABLE()[idx].sg_cterm_bold = FALSE;
7122 }
7123 }
7124#ifdef FEAT_GUI
7125 else
7126 {
7127 if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
7128 {
7129 if (!init)
7130 HL_TABLE()[idx].sg_set |= SG_GUI;
7131 HL_TABLE()[idx].sg_gui = attr;
7132 }
7133 }
7134#endif
7135 }
7136 else if (STRCMP(key, "FONT") == 0)
7137 {
7138 /* in non-GUI fonts are simply ignored */
7139#ifdef FEAT_GUI
7140 if (!gui.shell_created)
7141 {
7142 /* GUI not started yet, always accept the name. */
7143 vim_free(HL_TABLE()[idx].sg_font_name);
7144 HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
7145 }
7146 else
7147 {
7148 GuiFont temp_sg_font = HL_TABLE()[idx].sg_font;
7149# ifdef FEAT_XFONTSET
7150 GuiFontset temp_sg_fontset = HL_TABLE()[idx].sg_fontset;
7151# endif
7152 /* First, save the current font/fontset.
7153 * Then try to allocate the font/fontset.
7154 * If the allocation fails, HL_TABLE()[idx].sg_font OR
7155 * sg_fontset will be set to NOFONT or NOFONTSET respectively.
7156 */
7157
7158 HL_TABLE()[idx].sg_font = NOFONT;
7159# ifdef FEAT_XFONTSET
7160 HL_TABLE()[idx].sg_fontset = NOFONTSET;
7161# endif
7162 hl_do_font(idx, arg, is_normal_group, is_menu_group,
7163 is_tooltip_group);
7164
7165# ifdef FEAT_XFONTSET
7166 if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
7167 {
7168 /* New fontset was accepted. Free the old one, if there was
7169 * one.
7170 */
7171 gui_mch_free_fontset(temp_sg_fontset);
7172 vim_free(HL_TABLE()[idx].sg_font_name);
7173 HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
7174 }
7175 else
7176 HL_TABLE()[idx].sg_fontset = temp_sg_fontset;
7177# endif
7178 if (HL_TABLE()[idx].sg_font != NOFONT)
7179 {
7180 /* New font was accepted. Free the old one, if there was
7181 * one.
7182 */
7183 gui_mch_free_font(temp_sg_font);
7184 vim_free(HL_TABLE()[idx].sg_font_name);
7185 HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
7186 }
7187 else
7188 HL_TABLE()[idx].sg_font = temp_sg_font;
7189 }
7190#endif
7191 }
7192 else if (STRCMP(key, "CTERMFG") == 0 || STRCMP(key, "CTERMBG") == 0)
7193 {
7194 if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
7195 {
7196 if (!init)
7197 HL_TABLE()[idx].sg_set |= SG_CTERM;
7198
7199 /* When setting the foreground color, and previously the "bold"
7200 * flag was set for a light color, reset it now */
7201 if (key[5] == 'F' && HL_TABLE()[idx].sg_cterm_bold)
7202 {
7203 HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
7204 HL_TABLE()[idx].sg_cterm_bold = FALSE;
7205 }
7206
7207 if (VIM_ISDIGIT(*arg))
7208 color = atoi((char *)arg);
7209 else if (STRICMP(arg, "fg") == 0)
7210 {
7211 if (cterm_normal_fg_color)
7212 color = cterm_normal_fg_color - 1;
7213 else
7214 {
7215 EMSG(_("E419: FG color unknown"));
7216 error = TRUE;
7217 break;
7218 }
7219 }
7220 else if (STRICMP(arg, "bg") == 0)
7221 {
7222 if (cterm_normal_bg_color > 0)
7223 color = cterm_normal_bg_color - 1;
7224 else
7225 {
7226 EMSG(_("E420: BG color unknown"));
7227 error = TRUE;
7228 break;
7229 }
7230 }
7231 else
7232 {
7233 static char *(color_names[28]) = {
7234 "Black", "DarkBlue", "DarkGreen", "DarkCyan",
7235 "DarkRed", "DarkMagenta", "Brown", "DarkYellow",
7236 "Gray", "Grey",
7237 "LightGray", "LightGrey", "DarkGray", "DarkGrey",
7238 "Blue", "LightBlue", "Green", "LightGreen",
7239 "Cyan", "LightCyan", "Red", "LightRed", "Magenta",
7240 "LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
7241 static int color_numbers_16[28] = {0, 1, 2, 3,
7242 4, 5, 6, 6,
7243 7, 7,
7244 7, 7, 8, 8,
7245 9, 9, 10, 10,
7246 11, 11, 12, 12, 13,
7247 13, 14, 14, 15, -1};
7248 /* for xterm with 88 colors... */
7249 static int color_numbers_88[28] = {0, 4, 2, 6,
7250 1, 5, 32, 72,
7251 84, 84,
7252 7, 7, 82, 82,
7253 12, 43, 10, 61,
7254 14, 63, 9, 74, 13,
7255 75, 11, 78, 15, -1};
7256 /* for xterm with 256 colors... */
7257 static int color_numbers_256[28] = {0, 4, 2, 6,
7258 1, 5, 130, 130,
7259 248, 248,
7260 7, 7, 242, 242,
7261 12, 81, 10, 121,
7262 14, 159, 9, 224, 13,
7263 225, 11, 229, 15, -1};
7264 /* for terminals with less than 16 colors... */
7265 static int color_numbers_8[28] = {0, 4, 2, 6,
7266 1, 5, 3, 3,
7267 7, 7,
7268 7, 7, 0+8, 0+8,
7269 4+8, 4+8, 2+8, 2+8,
7270 6+8, 6+8, 1+8, 1+8, 5+8,
7271 5+8, 3+8, 3+8, 7+8, -1};
7272#if defined(__QNXNTO__)
7273 static int *color_numbers_8_qansi = color_numbers_8;
7274 /* On qnx, the 8 & 16 color arrays are the same */
7275 if (STRNCMP(T_NAME, "qansi", 5) == 0)
7276 color_numbers_8_qansi = color_numbers_16;
7277#endif
7278
7279 /* reduce calls to STRICMP a bit, it can be slow */
7280 off = TOUPPER_ASC(*arg);
7281 for (i = (sizeof(color_names) / sizeof(char *)); --i >= 0; )
7282 if (off == color_names[i][0]
7283 && STRICMP(arg + 1, color_names[i] + 1) == 0)
7284 break;
7285 if (i < 0)
7286 {
7287 EMSG2(_("E421: Color name or number not recognized: %s"), key_start);
7288 error = TRUE;
7289 break;
7290 }
7291
7292 /* Use the _16 table to check if its a valid color name. */
7293 color = color_numbers_16[i];
7294 if (color >= 0)
7295 {
7296 if (t_colors == 8)
7297 {
7298 /* t_Co is 8: use the 8 colors table */
7299#if defined(__QNXNTO__)
7300 color = color_numbers_8_qansi[i];
7301#else
7302 color = color_numbers_8[i];
7303#endif
7304 if (key[5] == 'F')
7305 {
7306 /* set/reset bold attribute to get light foreground
7307 * colors (on some terminals, e.g. "linux") */
7308 if (color & 8)
7309 {
7310 HL_TABLE()[idx].sg_cterm |= HL_BOLD;
7311 HL_TABLE()[idx].sg_cterm_bold = TRUE;
7312 }
7313 else
7314 HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
7315 }
7316 color &= 7; /* truncate to 8 colors */
7317 }
7318 else if (t_colors == 16 || t_colors == 88
7319 || t_colors == 256)
7320 {
7321 /*
7322 * Guess: if the termcap entry ends in 'm', it is
7323 * probably an xterm-like terminal. Use the changed
7324 * order for colors.
7325 */
7326 if (*T_CAF != NUL)
7327 p = T_CAF;
7328 else
7329 p = T_CSF;
7330 if (*p != NUL && *(p + STRLEN(p) - 1) == 'm')
7331 switch (t_colors)
7332 {
7333 case 16:
7334 color = color_numbers_8[i];
7335 break;
7336 case 88:
7337 color = color_numbers_88[i];
7338 break;
7339 case 256:
7340 color = color_numbers_256[i];
7341 break;
7342 }
7343 }
7344 }
7345 }
Bram Moolenaarccbab932010-05-13 15:40:30 +02007346 /* Add one to the argument, to avoid zero. Zero is used for
7347 * "NONE", then "color" is -1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 if (key[5] == 'F')
7349 {
7350 HL_TABLE()[idx].sg_cterm_fg = color + 1;
7351 if (is_normal_group)
7352 {
7353 cterm_normal_fg_color = color + 1;
7354 cterm_normal_fg_bold = (HL_TABLE()[idx].sg_cterm & HL_BOLD);
7355#ifdef FEAT_GUI
7356 /* Don't do this if the GUI is used. */
7357 if (!gui.in_use && !gui.starting)
7358#endif
7359 {
7360 must_redraw = CLEAR;
Bram Moolenaarccbab932010-05-13 15:40:30 +02007361 if (termcap_active && color >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 term_fg_color(color);
7363 }
7364 }
7365 }
7366 else
7367 {
7368 HL_TABLE()[idx].sg_cterm_bg = color + 1;
7369 if (is_normal_group)
7370 {
7371 cterm_normal_bg_color = color + 1;
7372#ifdef FEAT_GUI
7373 /* Don't mess with 'background' if the GUI is used. */
7374 if (!gui.in_use && !gui.starting)
7375#endif
7376 {
7377 must_redraw = CLEAR;
Bram Moolenaarccbab932010-05-13 15:40:30 +02007378 if (color >= 0)
7379 {
7380 if (termcap_active)
7381 term_bg_color(color);
7382 if (t_colors < 16)
7383 i = (color == 0 || color == 4);
7384 else
7385 i = (color < 7 || color == 8);
7386 /* Set the 'background' option if the value is
7387 * wrong. */
7388 if (i != (*p_bg == 'd'))
7389 set_option_value((char_u *)"bg", 0L,
7390 i ? (char_u *)"dark"
7391 : (char_u *)"light", 0);
7392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 }
7394 }
7395 }
7396 }
7397 }
7398 else if (STRCMP(key, "GUIFG") == 0)
7399 {
7400#ifdef FEAT_GUI /* in non-GUI guifg colors are simply ignored */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007401 if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007403 if (!init)
7404 HL_TABLE()[idx].sg_set |= SG_GUI;
7405
7406 i = color_name2handle(arg);
7407 if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
7408 {
7409 HL_TABLE()[idx].sg_gui_fg = i;
7410 vim_free(HL_TABLE()[idx].sg_gui_fg_name);
7411 if (STRCMP(arg, "NONE"))
7412 HL_TABLE()[idx].sg_gui_fg_name = vim_strsave(arg);
7413 else
7414 HL_TABLE()[idx].sg_gui_fg_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415# ifdef FEAT_GUI_X11
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007416 if (is_menu_group)
7417 gui.menu_fg_pixel = i;
7418 if (is_scrollbar_group)
7419 gui.scroll_fg_pixel = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420# ifdef FEAT_BEVAL
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007421 if (is_tooltip_group)
7422 gui.tooltip_fg_pixel = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423# endif
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007424 do_colors = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425# endif
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428#endif
7429 }
7430 else if (STRCMP(key, "GUIBG") == 0)
7431 {
7432#ifdef FEAT_GUI /* in non-GUI guibg colors are simply ignored */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007433 if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007435 if (!init)
7436 HL_TABLE()[idx].sg_set |= SG_GUI;
7437
7438 i = color_name2handle(arg);
7439 if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
7440 {
7441 HL_TABLE()[idx].sg_gui_bg = i;
7442 vim_free(HL_TABLE()[idx].sg_gui_bg_name);
7443 if (STRCMP(arg, "NONE") != 0)
7444 HL_TABLE()[idx].sg_gui_bg_name = vim_strsave(arg);
7445 else
7446 HL_TABLE()[idx].sg_gui_bg_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447# ifdef FEAT_GUI_X11
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007448 if (is_menu_group)
7449 gui.menu_bg_pixel = i;
7450 if (is_scrollbar_group)
7451 gui.scroll_bg_pixel = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452# ifdef FEAT_BEVAL
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007453 if (is_tooltip_group)
7454 gui.tooltip_bg_pixel = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455# endif
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007456 do_colors = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457# endif
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007460#endif
7461 }
7462 else if (STRCMP(key, "GUISP") == 0)
7463 {
7464#ifdef FEAT_GUI /* in non-GUI guisp colors are simply ignored */
7465 if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
7466 {
7467 if (!init)
7468 HL_TABLE()[idx].sg_set |= SG_GUI;
7469
7470 i = color_name2handle(arg);
7471 if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
7472 {
7473 HL_TABLE()[idx].sg_gui_sp = i;
7474 vim_free(HL_TABLE()[idx].sg_gui_sp_name);
7475 if (STRCMP(arg, "NONE") != 0)
7476 HL_TABLE()[idx].sg_gui_sp_name = vim_strsave(arg);
7477 else
7478 HL_TABLE()[idx].sg_gui_sp_name = NULL;
7479 }
7480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481#endif
7482 }
7483 else if (STRCMP(key, "START") == 0 || STRCMP(key, "STOP") == 0)
7484 {
7485 char_u buf[100];
7486 char_u *tname;
7487
7488 if (!init)
7489 HL_TABLE()[idx].sg_set |= SG_TERM;
7490
7491 /*
7492 * The "start" and "stop" arguments can be a literal escape
Bram Moolenaarb8017e72007-05-10 18:59:07 +00007493 * sequence, or a comma separated list of terminal codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 */
7495 if (STRNCMP(arg, "t_", 2) == 0)
7496 {
7497 off = 0;
7498 buf[0] = 0;
7499 while (arg[off] != NUL)
7500 {
7501 /* Isolate one termcap name */
7502 for (len = 0; arg[off + len] &&
7503 arg[off + len] != ','; ++len)
7504 ;
7505 tname = vim_strnsave(arg + off, len);
7506 if (tname == NULL) /* out of memory */
7507 {
7508 error = TRUE;
7509 break;
7510 }
7511 /* lookup the escape sequence for the item */
7512 p = get_term_code(tname);
7513 vim_free(tname);
7514 if (p == NULL) /* ignore non-existing things */
7515 p = (char_u *)"";
7516
7517 /* Append it to the already found stuff */
7518 if ((int)(STRLEN(buf) + STRLEN(p)) >= 99)
7519 {
7520 EMSG2(_("E422: terminal code too long: %s"), arg);
7521 error = TRUE;
7522 break;
7523 }
7524 STRCAT(buf, p);
7525
7526 /* Advance to the next item */
7527 off += len;
7528 if (arg[off] == ',') /* another one follows */
7529 ++off;
7530 }
7531 }
7532 else
7533 {
7534 /*
7535 * Copy characters from arg[] to buf[], translating <> codes.
7536 */
7537 for (p = arg, off = 0; off < 100 && *p; )
7538 {
7539 len = trans_special(&p, buf + off, FALSE);
7540 if (len) /* recognized special char */
7541 off += len;
7542 else /* copy as normal char */
7543 buf[off++] = *p++;
7544 }
7545 buf[off] = NUL;
7546 }
7547 if (error)
7548 break;
7549
7550 if (STRCMP(buf, "NONE") == 0) /* resetting the value */
7551 p = NULL;
7552 else
7553 p = vim_strsave(buf);
7554 if (key[2] == 'A')
7555 {
7556 vim_free(HL_TABLE()[idx].sg_start);
7557 HL_TABLE()[idx].sg_start = p;
7558 }
7559 else
7560 {
7561 vim_free(HL_TABLE()[idx].sg_stop);
7562 HL_TABLE()[idx].sg_stop = p;
7563 }
7564 }
7565 else
7566 {
7567 EMSG2(_("E423: Illegal argument: %s"), key_start);
7568 error = TRUE;
7569 break;
7570 }
7571
7572 /*
7573 * When highlighting has been given for a group, don't link it.
7574 */
7575 if (!init || !(HL_TABLE()[idx].sg_set & SG_LINK))
7576 HL_TABLE()[idx].sg_link = 0;
7577
7578 /*
7579 * Continue with next argument.
7580 */
7581 linep = skipwhite(linep);
7582 }
7583
7584 /*
7585 * If there is an error, and it's a new entry, remove it from the table.
7586 */
7587 if (error && idx == highlight_ga.ga_len)
7588 syn_unadd_group();
7589 else
7590 {
7591 if (is_normal_group)
7592 {
7593 HL_TABLE()[idx].sg_term_attr = 0;
7594 HL_TABLE()[idx].sg_cterm_attr = 0;
7595#ifdef FEAT_GUI
7596 HL_TABLE()[idx].sg_gui_attr = 0;
7597 /*
7598 * Need to update all groups, because they might be using "bg"
7599 * and/or "fg", which have been changed now.
7600 */
7601 if (gui.in_use)
7602 highlight_gui_started();
7603#endif
7604 }
7605#ifdef FEAT_GUI_X11
7606# ifdef FEAT_MENU
7607 else if (is_menu_group)
7608 {
7609 if (gui.in_use && do_colors)
7610 gui_mch_new_menu_colors();
7611 }
7612# endif
7613 else if (is_scrollbar_group)
7614 {
7615 if (gui.in_use && do_colors)
7616 gui_new_scrollbar_colors();
7617 }
7618# ifdef FEAT_BEVAL
7619 else if (is_tooltip_group)
7620 {
7621 if (gui.in_use && do_colors)
7622 gui_mch_new_tooltip_colors();
7623 }
7624# endif
7625#endif
7626 else
7627 set_hl_attr(idx);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007628#ifdef FEAT_EVAL
7629 HL_TABLE()[idx].sg_scriptID = current_SID;
7630#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007631 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
7633 vim_free(key);
7634 vim_free(arg);
7635
7636 /* Only call highlight_changed() once, after sourcing a syntax file */
7637 need_highlight_changed = TRUE;
7638}
7639
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007640#if defined(EXITFREE) || defined(PROTO)
7641 void
7642free_highlight()
7643{
7644 int i;
7645
7646 for (i = 0; i < highlight_ga.ga_len; ++i)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007647 {
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007648 highlight_clear(i);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007649 vim_free(HL_TABLE()[i].sg_name);
7650 vim_free(HL_TABLE()[i].sg_name_u);
7651 }
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007652 ga_clear(&highlight_ga);
7653}
7654#endif
7655
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656/*
7657 * Reset the cterm colors to what they were before Vim was started, if
7658 * possible. Otherwise reset them to zero.
7659 */
7660 void
7661restore_cterm_colors()
7662{
7663#if defined(MSDOS) || (defined(WIN3264) && !defined(FEAT_GUI_W32))
7664 /* Since t_me has been set, this probably means that the user
7665 * wants to use this as default colors. Need to reset default
7666 * background/foreground colors. */
7667 mch_set_normal_colors();
7668#else
7669 cterm_normal_fg_color = 0;
7670 cterm_normal_fg_bold = 0;
7671 cterm_normal_bg_color = 0;
7672#endif
7673}
7674
7675/*
7676 * Return TRUE if highlight group "idx" has any settings.
7677 * When "check_link" is TRUE also check for an existing link.
7678 */
7679 static int
7680hl_has_settings(idx, check_link)
7681 int idx;
7682 int check_link;
7683{
7684 return ( HL_TABLE()[idx].sg_term_attr != 0
7685 || HL_TABLE()[idx].sg_cterm_attr != 0
7686#ifdef FEAT_GUI
7687 || HL_TABLE()[idx].sg_gui_attr != 0
7688#endif
7689 || (check_link && (HL_TABLE()[idx].sg_set & SG_LINK)));
7690}
7691
7692/*
7693 * Clear highlighting for one group.
7694 */
7695 static void
7696highlight_clear(idx)
7697 int idx;
7698{
7699 HL_TABLE()[idx].sg_term = 0;
7700 vim_free(HL_TABLE()[idx].sg_start);
7701 HL_TABLE()[idx].sg_start = NULL;
7702 vim_free(HL_TABLE()[idx].sg_stop);
7703 HL_TABLE()[idx].sg_stop = NULL;
7704 HL_TABLE()[idx].sg_term_attr = 0;
7705 HL_TABLE()[idx].sg_cterm = 0;
7706 HL_TABLE()[idx].sg_cterm_bold = FALSE;
7707 HL_TABLE()[idx].sg_cterm_fg = 0;
7708 HL_TABLE()[idx].sg_cterm_bg = 0;
7709 HL_TABLE()[idx].sg_cterm_attr = 0;
7710#ifdef FEAT_GUI /* in non-GUI fonts are simply ignored */
7711 HL_TABLE()[idx].sg_gui = 0;
7712 HL_TABLE()[idx].sg_gui_fg = INVALCOLOR;
7713 vim_free(HL_TABLE()[idx].sg_gui_fg_name);
7714 HL_TABLE()[idx].sg_gui_fg_name = NULL;
7715 HL_TABLE()[idx].sg_gui_bg = INVALCOLOR;
7716 vim_free(HL_TABLE()[idx].sg_gui_bg_name);
7717 HL_TABLE()[idx].sg_gui_bg_name = NULL;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007718 HL_TABLE()[idx].sg_gui_sp = INVALCOLOR;
7719 vim_free(HL_TABLE()[idx].sg_gui_sp_name);
7720 HL_TABLE()[idx].sg_gui_sp_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 gui_mch_free_font(HL_TABLE()[idx].sg_font);
7722 HL_TABLE()[idx].sg_font = NOFONT;
7723# ifdef FEAT_XFONTSET
7724 gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
7725 HL_TABLE()[idx].sg_fontset = NOFONTSET;
7726# endif
7727 vim_free(HL_TABLE()[idx].sg_font_name);
7728 HL_TABLE()[idx].sg_font_name = NULL;
7729 HL_TABLE()[idx].sg_gui_attr = 0;
7730#endif
Bram Moolenaar661b1822005-07-28 22:36:45 +00007731#ifdef FEAT_EVAL
7732 /* Clear the script ID only when there is no link, since that is not
7733 * cleared. */
7734 if (HL_TABLE()[idx].sg_link == 0)
7735 HL_TABLE()[idx].sg_scriptID = 0;
7736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737}
7738
7739#if defined(FEAT_GUI) || defined(PROTO)
7740/*
7741 * Set the normal foreground and background colors according to the "Normal"
Bram Moolenaarf5b63862009-12-16 17:13:44 +00007742 * highlighting group. For X11 also set "Menu", "Scrollbar", and
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 * "Tooltip" colors.
7744 */
7745 void
7746set_normal_colors()
7747{
7748 if (set_group_colors((char_u *)"Normal",
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007749 &gui.norm_pixel, &gui.back_pixel,
7750 FALSE, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 gui_mch_new_colors();
7753 must_redraw = CLEAR;
7754 }
7755#ifdef FEAT_GUI_X11
7756 if (set_group_colors((char_u *)"Menu",
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007757 &gui.menu_fg_pixel, &gui.menu_bg_pixel,
7758 TRUE, FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {
7760# ifdef FEAT_MENU
7761 gui_mch_new_menu_colors();
7762# endif
7763 must_redraw = CLEAR;
7764 }
7765# ifdef FEAT_BEVAL
7766 if (set_group_colors((char_u *)"Tooltip",
7767 &gui.tooltip_fg_pixel, &gui.tooltip_bg_pixel,
7768 FALSE, FALSE, TRUE))
7769 {
7770# ifdef FEAT_TOOLBAR
7771 gui_mch_new_tooltip_colors();
7772# endif
7773 must_redraw = CLEAR;
7774 }
7775#endif
7776 if (set_group_colors((char_u *)"Scrollbar",
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007777 &gui.scroll_fg_pixel, &gui.scroll_bg_pixel,
7778 FALSE, FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {
7780 gui_new_scrollbar_colors();
7781 must_redraw = CLEAR;
7782 }
7783#endif
7784}
7785
7786/*
7787 * Set the colors for "Normal", "Menu", "Tooltip" or "Scrollbar".
7788 */
7789 static int
7790set_group_colors(name, fgp, bgp, do_menu, use_norm, do_tooltip)
7791 char_u *name;
7792 guicolor_T *fgp;
7793 guicolor_T *bgp;
7794 int do_menu;
7795 int use_norm;
7796 int do_tooltip;
7797{
7798 int idx;
7799
7800 idx = syn_name2id(name) - 1;
7801 if (idx >= 0)
7802 {
7803 gui_do_one_color(idx, do_menu, do_tooltip);
7804
7805 if (HL_TABLE()[idx].sg_gui_fg != INVALCOLOR)
7806 *fgp = HL_TABLE()[idx].sg_gui_fg;
7807 else if (use_norm)
7808 *fgp = gui.def_norm_pixel;
7809 if (HL_TABLE()[idx].sg_gui_bg != INVALCOLOR)
7810 *bgp = HL_TABLE()[idx].sg_gui_bg;
7811 else if (use_norm)
7812 *bgp = gui.def_back_pixel;
7813 return TRUE;
7814 }
7815 return FALSE;
7816}
7817
7818/*
7819 * Get the font of the "Normal" group.
7820 * Returns "" when it's not found or not set.
7821 */
7822 char_u *
7823hl_get_font_name()
7824{
7825 int id;
7826 char_u *s;
7827
7828 id = syn_name2id((char_u *)"Normal");
7829 if (id > 0)
7830 {
7831 s = HL_TABLE()[id - 1].sg_font_name;
7832 if (s != NULL)
7833 return s;
7834 }
7835 return (char_u *)"";
7836}
7837
7838/*
7839 * Set font for "Normal" group. Called by gui_mch_init_font() when a font has
7840 * actually chosen to be used.
7841 */
7842 void
7843hl_set_font_name(font_name)
7844 char_u *font_name;
7845{
7846 int id;
7847
7848 id = syn_name2id((char_u *)"Normal");
7849 if (id > 0)
7850 {
7851 vim_free(HL_TABLE()[id - 1].sg_font_name);
7852 HL_TABLE()[id - 1].sg_font_name = vim_strsave(font_name);
7853 }
7854}
7855
7856/*
7857 * Set background color for "Normal" group. Called by gui_set_bg_color()
7858 * when the color is known.
7859 */
7860 void
7861hl_set_bg_color_name(name)
7862 char_u *name; /* must have been allocated */
7863{
7864 int id;
7865
7866 if (name != NULL)
7867 {
7868 id = syn_name2id((char_u *)"Normal");
7869 if (id > 0)
7870 {
7871 vim_free(HL_TABLE()[id - 1].sg_gui_bg_name);
7872 HL_TABLE()[id - 1].sg_gui_bg_name = name;
7873 }
7874 }
7875}
7876
7877/*
7878 * Set foreground color for "Normal" group. Called by gui_set_fg_color()
7879 * when the color is known.
7880 */
7881 void
7882hl_set_fg_color_name(name)
7883 char_u *name; /* must have been allocated */
7884{
7885 int id;
7886
7887 if (name != NULL)
7888 {
7889 id = syn_name2id((char_u *)"Normal");
7890 if (id > 0)
7891 {
7892 vim_free(HL_TABLE()[id - 1].sg_gui_fg_name);
7893 HL_TABLE()[id - 1].sg_gui_fg_name = name;
7894 }
7895 }
7896}
7897
7898/*
7899 * Return the handle for a color name.
7900 * Returns INVALCOLOR when failed.
7901 */
7902 static guicolor_T
7903color_name2handle(name)
7904 char_u *name;
7905{
7906 if (STRCMP(name, "NONE") == 0)
7907 return INVALCOLOR;
7908
7909 if (STRICMP(name, "fg") == 0 || STRICMP(name, "foreground") == 0)
7910 return gui.norm_pixel;
7911 if (STRICMP(name, "bg") == 0 || STRICMP(name, "background") == 0)
7912 return gui.back_pixel;
7913
7914 return gui_get_color(name);
7915}
7916
7917/*
7918 * Return the handle for a font name.
7919 * Returns NOFONT when failed.
7920 */
7921 static GuiFont
7922font_name2handle(name)
7923 char_u *name;
7924{
7925 if (STRCMP(name, "NONE") == 0)
7926 return NOFONT;
7927
7928 return gui_mch_get_font(name, TRUE);
7929}
7930
7931# ifdef FEAT_XFONTSET
7932/*
7933 * Return the handle for a fontset name.
7934 * Returns NOFONTSET when failed.
7935 */
7936 static GuiFontset
7937fontset_name2handle(name, fixed_width)
7938 char_u *name;
7939 int fixed_width;
7940{
7941 if (STRCMP(name, "NONE") == 0)
7942 return NOFONTSET;
7943
7944 return gui_mch_get_fontset(name, TRUE, fixed_width);
7945}
7946# endif
7947
7948/*
7949 * Get the font or fontset for one highlight group.
7950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 static void
7952hl_do_font(idx, arg, do_normal, do_menu, do_tooltip)
7953 int idx;
7954 char_u *arg;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007955 int do_normal; /* set normal font */
7956 int do_menu UNUSED; /* set menu font */
7957 int do_tooltip UNUSED; /* set tooltip font */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959# ifdef FEAT_XFONTSET
7960 /* If 'guifontset' is not empty, first try using the name as a
7961 * fontset. If that doesn't work, use it as a font name. */
7962 if (*p_guifontset != NUL
7963# ifdef FONTSET_ALWAYS
7964 || do_menu
7965# endif
7966# ifdef FEAT_BEVAL_TIP
7967 /* In Athena & Motif, the Tooltip highlight group is always a fontset */
7968 || do_tooltip
7969# endif
7970 )
7971 HL_TABLE()[idx].sg_fontset = fontset_name2handle(arg, 0
7972# ifdef FONTSET_ALWAYS
7973 || do_menu
7974# endif
7975# ifdef FEAT_BEVAL_TIP
7976 || do_tooltip
7977# endif
7978 );
7979 if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
7980 {
7981 /* If it worked and it's the Normal group, use it as the
7982 * normal fontset. Same for the Menu group. */
7983 if (do_normal)
7984 gui_init_font(arg, TRUE);
7985# if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
7986 if (do_menu)
7987 {
7988# ifdef FONTSET_ALWAYS
7989 gui.menu_fontset = HL_TABLE()[idx].sg_fontset;
7990# else
7991 /* YIKES! This is a bug waiting to crash the program */
7992 gui.menu_font = HL_TABLE()[idx].sg_fontset;
7993# endif
7994 gui_mch_new_menu_font();
7995 }
7996# ifdef FEAT_BEVAL
7997 if (do_tooltip)
7998 {
7999 /* The Athena widget set cannot currently handle switching between
8000 * displaying a single font and a fontset.
8001 * If the XtNinternational resource is set to True at widget
Bram Moolenaarb8017e72007-05-10 18:59:07 +00008002 * creation, then a fontset is always used, otherwise an
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 * XFontStruct is used.
8004 */
8005 gui.tooltip_fontset = (XFontSet)HL_TABLE()[idx].sg_fontset;
8006 gui_mch_new_tooltip_font();
8007 }
8008# endif
8009# endif
8010 }
8011 else
8012# endif
8013 {
8014 HL_TABLE()[idx].sg_font = font_name2handle(arg);
8015 /* If it worked and it's the Normal group, use it as the
8016 * normal font. Same for the Menu group. */
8017 if (HL_TABLE()[idx].sg_font != NOFONT)
8018 {
8019 if (do_normal)
8020 gui_init_font(arg, FALSE);
8021#ifndef FONTSET_ALWAYS
8022# if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
8023 if (do_menu)
8024 {
8025 gui.menu_font = HL_TABLE()[idx].sg_font;
8026 gui_mch_new_menu_font();
8027 }
8028# endif
8029#endif
8030 }
8031 }
8032}
8033
8034#endif /* FEAT_GUI */
8035
8036/*
8037 * Table with the specifications for an attribute number.
8038 * Note that this table is used by ALL buffers. This is required because the
8039 * GUI can redraw at any time for any buffer.
8040 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00008041static garray_T term_attr_table = {0, 0, 0, 0, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042
8043#define TERM_ATTR_ENTRY(idx) ((attrentry_T *)term_attr_table.ga_data)[idx]
8044
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00008045static garray_T cterm_attr_table = {0, 0, 0, 0, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
8047#define CTERM_ATTR_ENTRY(idx) ((attrentry_T *)cterm_attr_table.ga_data)[idx]
8048
8049#ifdef FEAT_GUI
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00008050static garray_T gui_attr_table = {0, 0, 0, 0, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
8052#define GUI_ATTR_ENTRY(idx) ((attrentry_T *)gui_attr_table.ga_data)[idx]
8053#endif
8054
8055/*
8056 * Return the attr number for a set of colors and font.
8057 * Add a new entry to the term_attr_table, cterm_attr_table or gui_attr_table
8058 * if the combination is new.
8059 * Return 0 for error (no more room).
8060 */
8061 static int
8062get_attr_entry(table, aep)
8063 garray_T *table;
8064 attrentry_T *aep;
8065{
8066 int i;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008067 attrentry_T *taep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 static int recursive = FALSE;
8069
8070 /*
8071 * Init the table, in case it wasn't done yet.
8072 */
8073 table->ga_itemsize = sizeof(attrentry_T);
8074 table->ga_growsize = 7;
8075
8076 /*
8077 * Try to find an entry with the same specifications.
8078 */
8079 for (i = 0; i < table->ga_len; ++i)
8080 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008081 taep = &(((attrentry_T *)table->ga_data)[i]);
8082 if ( aep->ae_attr == taep->ae_attr
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 && (
8084#ifdef FEAT_GUI
8085 (table == &gui_attr_table
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008086 && (aep->ae_u.gui.fg_color == taep->ae_u.gui.fg_color
8087 && aep->ae_u.gui.bg_color
8088 == taep->ae_u.gui.bg_color
8089 && aep->ae_u.gui.sp_color
8090 == taep->ae_u.gui.sp_color
8091 && aep->ae_u.gui.font == taep->ae_u.gui.font
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092# ifdef FEAT_XFONTSET
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008093 && aep->ae_u.gui.fontset == taep->ae_u.gui.fontset
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094# endif
8095 ))
8096 ||
8097#endif
8098 (table == &term_attr_table
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008099 && (aep->ae_u.term.start == NULL)
8100 == (taep->ae_u.term.start == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 && (aep->ae_u.term.start == NULL
8102 || STRCMP(aep->ae_u.term.start,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008103 taep->ae_u.term.start) == 0)
8104 && (aep->ae_u.term.stop == NULL)
8105 == (taep->ae_u.term.stop == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 && (aep->ae_u.term.stop == NULL
8107 || STRCMP(aep->ae_u.term.stop,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008108 taep->ae_u.term.stop) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 || (table == &cterm_attr_table
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008110 && aep->ae_u.cterm.fg_color
8111 == taep->ae_u.cterm.fg_color
8112 && aep->ae_u.cterm.bg_color
8113 == taep->ae_u.cterm.bg_color)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 ))
8115
8116 return i + ATTR_OFF;
8117 }
8118
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008119 if (table->ga_len + ATTR_OFF > MAX_TYPENR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {
8121 /*
8122 * Running out of attribute entries! remove all attributes, and
8123 * compute new ones for all groups.
8124 * When called recursively, we are really out of numbers.
8125 */
8126 if (recursive)
8127 {
8128 EMSG(_("E424: Too many different highlighting attributes in use"));
8129 return 0;
8130 }
8131 recursive = TRUE;
8132
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008133 clear_hl_tables();
8134
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 must_redraw = CLEAR;
8136
8137 for (i = 0; i < highlight_ga.ga_len; ++i)
8138 set_hl_attr(i);
8139
8140 recursive = FALSE;
8141 }
8142
8143 /*
8144 * This is a new combination of colors and font, add an entry.
8145 */
8146 if (ga_grow(table, 1) == FAIL)
8147 return 0;
8148
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008149 taep = &(((attrentry_T *)table->ga_data)[table->ga_len]);
8150 vim_memset(taep, 0, sizeof(attrentry_T));
8151 taep->ae_attr = aep->ae_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#ifdef FEAT_GUI
8153 if (table == &gui_attr_table)
8154 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008155 taep->ae_u.gui.fg_color = aep->ae_u.gui.fg_color;
8156 taep->ae_u.gui.bg_color = aep->ae_u.gui.bg_color;
8157 taep->ae_u.gui.sp_color = aep->ae_u.gui.sp_color;
8158 taep->ae_u.gui.font = aep->ae_u.gui.font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159# ifdef FEAT_XFONTSET
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008160 taep->ae_u.gui.fontset = aep->ae_u.gui.fontset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161# endif
8162 }
8163#endif
8164 if (table == &term_attr_table)
8165 {
8166 if (aep->ae_u.term.start == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008167 taep->ae_u.term.start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 else
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008169 taep->ae_u.term.start = vim_strsave(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 if (aep->ae_u.term.stop == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008171 taep->ae_u.term.stop = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 else
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008173 taep->ae_u.term.stop = vim_strsave(aep->ae_u.term.stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 }
8175 else if (table == &cterm_attr_table)
8176 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008177 taep->ae_u.cterm.fg_color = aep->ae_u.cterm.fg_color;
8178 taep->ae_u.cterm.bg_color = aep->ae_u.cterm.bg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
8180 ++table->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 return (table->ga_len - 1 + ATTR_OFF);
8182}
8183
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00008184/*
8185 * Clear all highlight tables.
8186 */
8187 void
8188clear_hl_tables()
8189{
8190 int i;
8191 attrentry_T *taep;
8192
8193#ifdef FEAT_GUI
8194 ga_clear(&gui_attr_table);
8195#endif
8196 for (i = 0; i < term_attr_table.ga_len; ++i)
8197 {
8198 taep = &(((attrentry_T *)term_attr_table.ga_data)[i]);
8199 vim_free(taep->ae_u.term.start);
8200 vim_free(taep->ae_u.term.stop);
8201 }
8202 ga_clear(&term_attr_table);
8203 ga_clear(&cterm_attr_table);
8204}
8205
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00008206#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar217ad922005-03-20 22:37:15 +00008207/*
Bram Moolenaar30abd282005-06-22 22:35:10 +00008208 * Combine special attributes (e.g., for spelling) with other attributes
8209 * (e.g., for syntax highlighting).
8210 * "prim_attr" overrules "char_attr".
Bram Moolenaar217ad922005-03-20 22:37:15 +00008211 * This creates a new group when required.
8212 * Since we expect there to be few spelling mistakes we don't cache the
8213 * result.
8214 * Return the resulting attributes.
8215 */
8216 int
Bram Moolenaar30abd282005-06-22 22:35:10 +00008217hl_combine_attr(char_attr, prim_attr)
Bram Moolenaar217ad922005-03-20 22:37:15 +00008218 int char_attr;
Bram Moolenaar30abd282005-06-22 22:35:10 +00008219 int prim_attr;
Bram Moolenaar217ad922005-03-20 22:37:15 +00008220{
8221 attrentry_T *char_aep = NULL;
8222 attrentry_T *spell_aep;
8223 attrentry_T new_en;
8224
8225 if (char_attr == 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00008226 return prim_attr;
8227 if (char_attr <= HL_ALL && prim_attr <= HL_ALL)
8228 return char_attr | prim_attr;
Bram Moolenaar217ad922005-03-20 22:37:15 +00008229#ifdef FEAT_GUI
8230 if (gui.in_use)
8231 {
8232 if (char_attr > HL_ALL)
8233 char_aep = syn_gui_attr2entry(char_attr);
8234 if (char_aep != NULL)
8235 new_en = *char_aep;
8236 else
8237 {
8238 vim_memset(&new_en, 0, sizeof(new_en));
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00008239 new_en.ae_u.gui.fg_color = INVALCOLOR;
8240 new_en.ae_u.gui.bg_color = INVALCOLOR;
8241 new_en.ae_u.gui.sp_color = INVALCOLOR;
Bram Moolenaar217ad922005-03-20 22:37:15 +00008242 if (char_attr <= HL_ALL)
8243 new_en.ae_attr = char_attr;
8244 }
8245
Bram Moolenaar30abd282005-06-22 22:35:10 +00008246 if (prim_attr <= HL_ALL)
8247 new_en.ae_attr |= prim_attr;
Bram Moolenaar217ad922005-03-20 22:37:15 +00008248 else
8249 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00008250 spell_aep = syn_gui_attr2entry(prim_attr);
Bram Moolenaar217ad922005-03-20 22:37:15 +00008251 if (spell_aep != NULL)
8252 {
8253 new_en.ae_attr |= spell_aep->ae_attr;
8254 if (spell_aep->ae_u.gui.fg_color != INVALCOLOR)
8255 new_en.ae_u.gui.fg_color = spell_aep->ae_u.gui.fg_color;
8256 if (spell_aep->ae_u.gui.bg_color != INVALCOLOR)
8257 new_en.ae_u.gui.bg_color = spell_aep->ae_u.gui.bg_color;
8258 if (spell_aep->ae_u.gui.sp_color != INVALCOLOR)
8259 new_en.ae_u.gui.sp_color = spell_aep->ae_u.gui.sp_color;
8260 if (spell_aep->ae_u.gui.font != NOFONT)
8261 new_en.ae_u.gui.font = spell_aep->ae_u.gui.font;
8262# ifdef FEAT_XFONTSET
8263 if (spell_aep->ae_u.gui.fontset != NOFONTSET)
8264 new_en.ae_u.gui.fontset = spell_aep->ae_u.gui.fontset;
8265# endif
8266 }
8267 }
8268 return get_attr_entry(&gui_attr_table, &new_en);
8269 }
8270#endif
8271
8272 if (t_colors > 1)
8273 {
8274 if (char_attr > HL_ALL)
8275 char_aep = syn_cterm_attr2entry(char_attr);
8276 if (char_aep != NULL)
8277 new_en = *char_aep;
8278 else
8279 {
8280 vim_memset(&new_en, 0, sizeof(new_en));
8281 if (char_attr <= HL_ALL)
8282 new_en.ae_attr = char_attr;
8283 }
8284
Bram Moolenaar30abd282005-06-22 22:35:10 +00008285 if (prim_attr <= HL_ALL)
8286 new_en.ae_attr |= prim_attr;
Bram Moolenaar217ad922005-03-20 22:37:15 +00008287 else
8288 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00008289 spell_aep = syn_cterm_attr2entry(prim_attr);
Bram Moolenaar217ad922005-03-20 22:37:15 +00008290 if (spell_aep != NULL)
8291 {
8292 new_en.ae_attr |= spell_aep->ae_attr;
8293 if (spell_aep->ae_u.cterm.fg_color > 0)
8294 new_en.ae_u.cterm.fg_color = spell_aep->ae_u.cterm.fg_color;
8295 if (spell_aep->ae_u.cterm.bg_color > 0)
8296 new_en.ae_u.cterm.bg_color = spell_aep->ae_u.cterm.bg_color;
8297 }
8298 }
8299 return get_attr_entry(&cterm_attr_table, &new_en);
8300 }
8301
8302 if (char_attr > HL_ALL)
8303 char_aep = syn_term_attr2entry(char_attr);
8304 if (char_aep != NULL)
8305 new_en = *char_aep;
8306 else
8307 {
8308 vim_memset(&new_en, 0, sizeof(new_en));
8309 if (char_attr <= HL_ALL)
8310 new_en.ae_attr = char_attr;
8311 }
8312
Bram Moolenaar30abd282005-06-22 22:35:10 +00008313 if (prim_attr <= HL_ALL)
8314 new_en.ae_attr |= prim_attr;
Bram Moolenaar217ad922005-03-20 22:37:15 +00008315 else
8316 {
Bram Moolenaar3b181812005-12-17 22:10:02 +00008317 spell_aep = syn_term_attr2entry(prim_attr);
Bram Moolenaar217ad922005-03-20 22:37:15 +00008318 if (spell_aep != NULL)
8319 {
8320 new_en.ae_attr |= spell_aep->ae_attr;
8321 if (spell_aep->ae_u.term.start != NULL)
8322 {
8323 new_en.ae_u.term.start = spell_aep->ae_u.term.start;
8324 new_en.ae_u.term.stop = spell_aep->ae_u.term.stop;
8325 }
8326 }
8327 }
8328 return get_attr_entry(&term_attr_table, &new_en);
8329}
8330#endif
8331
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332#ifdef FEAT_GUI
8333
8334 attrentry_T *
8335syn_gui_attr2entry(attr)
8336 int attr;
8337{
8338 attr -= ATTR_OFF;
8339 if (attr >= gui_attr_table.ga_len) /* did ":syntax clear" */
8340 return NULL;
8341 return &(GUI_ATTR_ENTRY(attr));
8342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343#endif /* FEAT_GUI */
8344
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00008345/*
8346 * Get the highlight attributes (HL_BOLD etc.) from an attribute nr.
8347 * Only to be used when "attr" > HL_ALL.
8348 */
8349 int
8350syn_attr2attr(attr)
8351 int attr;
8352{
8353 attrentry_T *aep;
8354
8355#ifdef FEAT_GUI
8356 if (gui.in_use)
8357 aep = syn_gui_attr2entry(attr);
8358 else
8359#endif
8360 if (t_colors > 1)
8361 aep = syn_cterm_attr2entry(attr);
8362 else
8363 aep = syn_term_attr2entry(attr);
8364
8365 if (aep == NULL) /* highlighting not set */
8366 return 0;
8367 return aep->ae_attr;
8368}
8369
8370
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 attrentry_T *
8372syn_term_attr2entry(attr)
8373 int attr;
8374{
8375 attr -= ATTR_OFF;
8376 if (attr >= term_attr_table.ga_len) /* did ":syntax clear" */
8377 return NULL;
8378 return &(TERM_ATTR_ENTRY(attr));
8379}
8380
8381 attrentry_T *
8382syn_cterm_attr2entry(attr)
8383 int attr;
8384{
8385 attr -= ATTR_OFF;
8386 if (attr >= cterm_attr_table.ga_len) /* did ":syntax clear" */
8387 return NULL;
8388 return &(CTERM_ATTR_ENTRY(attr));
8389}
8390
8391#define LIST_ATTR 1
8392#define LIST_STRING 2
8393#define LIST_INT 3
8394
8395 static void
8396highlight_list_one(id)
8397 int id;
8398{
8399 struct hl_group *sgp;
8400 int didh = FALSE;
8401
8402 sgp = &HL_TABLE()[id - 1]; /* index is ID minus one */
8403
8404 didh = highlight_list_arg(id, didh, LIST_ATTR,
8405 sgp->sg_term, NULL, "term");
8406 didh = highlight_list_arg(id, didh, LIST_STRING,
8407 0, sgp->sg_start, "start");
8408 didh = highlight_list_arg(id, didh, LIST_STRING,
8409 0, sgp->sg_stop, "stop");
8410
8411 didh = highlight_list_arg(id, didh, LIST_ATTR,
8412 sgp->sg_cterm, NULL, "cterm");
8413 didh = highlight_list_arg(id, didh, LIST_INT,
8414 sgp->sg_cterm_fg, NULL, "ctermfg");
8415 didh = highlight_list_arg(id, didh, LIST_INT,
8416 sgp->sg_cterm_bg, NULL, "ctermbg");
8417
8418#ifdef FEAT_GUI
8419 didh = highlight_list_arg(id, didh, LIST_ATTR,
8420 sgp->sg_gui, NULL, "gui");
8421 didh = highlight_list_arg(id, didh, LIST_STRING,
8422 0, sgp->sg_gui_fg_name, "guifg");
8423 didh = highlight_list_arg(id, didh, LIST_STRING,
8424 0, sgp->sg_gui_bg_name, "guibg");
8425 didh = highlight_list_arg(id, didh, LIST_STRING,
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008426 0, sgp->sg_gui_sp_name, "guisp");
8427 didh = highlight_list_arg(id, didh, LIST_STRING,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 0, sgp->sg_font_name, "font");
8429#endif
8430
Bram Moolenaar661b1822005-07-28 22:36:45 +00008431 if (sgp->sg_link && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {
8433 (void)syn_list_header(didh, 9999, id);
Bram Moolenaar661b1822005-07-28 22:36:45 +00008434 didh = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 msg_puts_attr((char_u *)"links to", hl_attr(HLF_D));
8436 msg_putchar(' ');
8437 msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
8438 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008439
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008440 if (!didh)
8441 highlight_list_arg(id, didh, LIST_STRING, 0, (char_u *)"cleared", "");
Bram Moolenaar661b1822005-07-28 22:36:45 +00008442#ifdef FEAT_EVAL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008443 if (p_verbose > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008444 last_set_msg(sgp->sg_scriptID);
8445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446}
8447
8448 static int
8449highlight_list_arg(id, didh, type, iarg, sarg, name)
8450 int id;
8451 int didh;
8452 int type;
8453 int iarg;
8454 char_u *sarg;
8455 char *name;
8456{
8457 char_u buf[100];
8458 char_u *ts;
8459 int i;
8460
Bram Moolenaar661b1822005-07-28 22:36:45 +00008461 if (got_int)
8462 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 if (type == LIST_STRING ? (sarg != NULL) : (iarg != 0))
8464 {
8465 ts = buf;
8466 if (type == LIST_INT)
8467 sprintf((char *)buf, "%d", iarg - 1);
8468 else if (type == LIST_STRING)
8469 ts = sarg;
8470 else /* type == LIST_ATTR */
8471 {
8472 buf[0] = NUL;
8473 for (i = 0; hl_attr_table[i] != 0; ++i)
8474 {
8475 if (iarg & hl_attr_table[i])
8476 {
8477 if (buf[0] != NUL)
8478 STRCAT(buf, ",");
8479 STRCAT(buf, hl_name_table[i]);
8480 iarg &= ~hl_attr_table[i]; /* don't want "inverse" */
8481 }
8482 }
8483 }
8484
8485 (void)syn_list_header(didh,
8486 (int)(vim_strsize(ts) + STRLEN(name) + 1), id);
8487 didh = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +00008488 if (!got_int)
8489 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008490 if (*name != NUL)
8491 {
8492 MSG_PUTS_ATTR(name, hl_attr(HLF_D));
8493 MSG_PUTS_ATTR("=", hl_attr(HLF_D));
8494 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008495 msg_outtrans(ts);
8496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498 return didh;
8499}
8500
8501#if (((defined(FEAT_EVAL) || defined(FEAT_PRINTER))) && defined(FEAT_SYN_HL)) || defined(PROTO)
8502/*
8503 * Return "1" if highlight group "id" has attribute "flag".
8504 * Return NULL otherwise.
8505 */
8506 char_u *
8507highlight_has_attr(id, flag, modec)
8508 int id;
8509 int flag;
8510 int modec; /* 'g' for GUI, 'c' for cterm, 't' for term */
8511{
8512 int attr;
8513
8514 if (id <= 0 || id > highlight_ga.ga_len)
8515 return NULL;
8516
8517#ifdef FEAT_GUI
8518 if (modec == 'g')
8519 attr = HL_TABLE()[id - 1].sg_gui;
8520 else
8521#endif
8522 if (modec == 'c')
8523 attr = HL_TABLE()[id - 1].sg_cterm;
8524 else
8525 attr = HL_TABLE()[id - 1].sg_term;
8526
8527 if (attr & flag)
8528 return (char_u *)"1";
8529 return NULL;
8530}
8531#endif
8532
8533#if (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) || defined(PROTO)
8534/*
8535 * Return color name of highlight group "id".
8536 */
8537 char_u *
8538highlight_color(id, what, modec)
8539 int id;
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008540 char_u *what; /* "font", "fg", "bg", "sp", "fg#", "bg#" or "sp#" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 int modec; /* 'g' for GUI, 'c' for cterm, 't' for term */
8542{
8543 static char_u name[20];
8544 int n;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008545 int fg = FALSE;
8546# ifdef FEAT_GUI
8547 int sp = FALSE;
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008548 int font = FALSE;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550
8551 if (id <= 0 || id > highlight_ga.ga_len)
8552 return NULL;
8553
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008554 if (TOLOWER_ASC(what[0]) == 'f' && TOLOWER_ASC(what[1]) == 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 fg = TRUE;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008556# ifdef FEAT_GUI
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008557 else if (TOLOWER_ASC(what[0]) == 'f' && TOLOWER_ASC(what[1]) == 'o'
8558 && TOLOWER_ASC(what[2]) == 'n' && TOLOWER_ASC(what[3]) == 't')
8559 font = TRUE;
8560 else if (TOLOWER_ASC(what[0]) == 's' && TOLOWER_ASC(what[1]) == 'p')
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008561 sp = TRUE;
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008562 else if (!(TOLOWER_ASC(what[0]) == 'b' && TOLOWER_ASC(what[1]) == 'g'))
8563 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 if (modec == 'g')
8565 {
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008566 /* return font name */
8567 if (font)
8568 return HL_TABLE()[id - 1].sg_font_name;
8569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 /* return #RRGGBB form (only possible when GUI is running) */
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008571 if (gui.in_use && what[2] == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 {
8573 guicolor_T color;
8574 long_u rgb;
8575 static char_u buf[10];
8576
8577 if (fg)
8578 color = HL_TABLE()[id - 1].sg_gui_fg;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008579 else if (sp)
8580 color = HL_TABLE()[id - 1].sg_gui_sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 else
8582 color = HL_TABLE()[id - 1].sg_gui_bg;
8583 if (color == INVALCOLOR)
8584 return NULL;
8585 rgb = gui_mch_get_rgb(color);
8586 sprintf((char *)buf, "#%02x%02x%02x",
8587 (unsigned)(rgb >> 16),
8588 (unsigned)(rgb >> 8) & 255,
8589 (unsigned)rgb & 255);
8590 return buf;
8591 }
8592 if (fg)
8593 return (HL_TABLE()[id - 1].sg_gui_fg_name);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008594 if (sp)
8595 return (HL_TABLE()[id - 1].sg_gui_sp_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 return (HL_TABLE()[id - 1].sg_gui_bg_name);
8597 }
Bram Moolenaar12682fd2010-03-10 13:43:49 +01008598 if (font || sp)
8599 return NULL;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008600# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 if (modec == 'c')
8602 {
8603 if (fg)
8604 n = HL_TABLE()[id - 1].sg_cterm_fg - 1;
8605 else
8606 n = HL_TABLE()[id - 1].sg_cterm_bg - 1;
8607 sprintf((char *)name, "%d", n);
8608 return name;
8609 }
8610 /* term doesn't have color */
8611 return NULL;
8612}
8613#endif
8614
8615#if (defined(FEAT_SYN_HL) && defined(FEAT_GUI) && defined(FEAT_PRINTER)) \
8616 || defined(PROTO)
8617/*
8618 * Return color name of highlight group "id" as RGB value.
8619 */
8620 long_u
8621highlight_gui_color_rgb(id, fg)
8622 int id;
8623 int fg; /* TRUE = fg, FALSE = bg */
8624{
8625 guicolor_T color;
8626
8627 if (id <= 0 || id > highlight_ga.ga_len)
8628 return 0L;
8629
8630 if (fg)
8631 color = HL_TABLE()[id - 1].sg_gui_fg;
8632 else
8633 color = HL_TABLE()[id - 1].sg_gui_bg;
8634
8635 if (color == INVALCOLOR)
8636 return 0L;
8637
8638 return gui_mch_get_rgb(color);
8639}
8640#endif
8641
8642/*
8643 * Output the syntax list header.
8644 * Return TRUE when started a new line.
8645 */
8646 static int
8647syn_list_header(did_header, outlen, id)
8648 int did_header; /* did header already */
8649 int outlen; /* length of string that comes */
8650 int id; /* highlight group id */
8651{
8652 int endcol = 19;
8653 int newline = TRUE;
8654
8655 if (!did_header)
8656 {
8657 msg_putchar('\n');
Bram Moolenaar661b1822005-07-28 22:36:45 +00008658 if (got_int)
8659 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 msg_outtrans(HL_TABLE()[id - 1].sg_name);
8661 endcol = 15;
8662 }
8663 else if (msg_col + outlen + 1 >= Columns)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 msg_putchar('\n');
Bram Moolenaar661b1822005-07-28 22:36:45 +00008666 if (got_int)
8667 return TRUE;
8668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 else
8670 {
8671 if (msg_col >= endcol) /* wrap around is like starting a new line */
8672 newline = FALSE;
8673 }
8674
8675 if (msg_col >= endcol) /* output at least one space */
8676 endcol = msg_col + 1;
8677 if (Columns <= endcol) /* avoid hang for tiny window */
8678 endcol = Columns - 1;
8679
8680 msg_advance(endcol);
8681
8682 /* Show "xxx" with the attributes. */
8683 if (!did_header)
8684 {
8685 msg_puts_attr((char_u *)"xxx", syn_id2attr(id));
8686 msg_putchar(' ');
8687 }
8688
8689 return newline;
8690}
8691
8692/*
8693 * Set the attribute numbers for a highlight group.
8694 * Called after one of the attributes has changed.
8695 */
8696 static void
8697set_hl_attr(idx)
8698 int idx; /* index in array */
8699{
8700 attrentry_T at_en;
8701 struct hl_group *sgp = HL_TABLE() + idx;
8702
8703 /* The "Normal" group doesn't need an attribute number */
8704 if (sgp->sg_name_u != NULL && STRCMP(sgp->sg_name_u, "NORMAL") == 0)
8705 return;
8706
8707#ifdef FEAT_GUI
8708 /*
8709 * For the GUI mode: If there are other than "normal" highlighting
8710 * attributes, need to allocate an attr number.
8711 */
8712 if (sgp->sg_gui_fg == INVALCOLOR
8713 && sgp->sg_gui_bg == INVALCOLOR
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008714 && sgp->sg_gui_sp == INVALCOLOR
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 && sgp->sg_font == NOFONT
8716# ifdef FEAT_XFONTSET
8717 && sgp->sg_fontset == NOFONTSET
8718# endif
8719 )
8720 {
8721 sgp->sg_gui_attr = sgp->sg_gui;
8722 }
8723 else
8724 {
8725 at_en.ae_attr = sgp->sg_gui;
8726 at_en.ae_u.gui.fg_color = sgp->sg_gui_fg;
8727 at_en.ae_u.gui.bg_color = sgp->sg_gui_bg;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008728 at_en.ae_u.gui.sp_color = sgp->sg_gui_sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 at_en.ae_u.gui.font = sgp->sg_font;
8730# ifdef FEAT_XFONTSET
8731 at_en.ae_u.gui.fontset = sgp->sg_fontset;
8732# endif
8733 sgp->sg_gui_attr = get_attr_entry(&gui_attr_table, &at_en);
8734 }
8735#endif
8736 /*
8737 * For the term mode: If there are other than "normal" highlighting
8738 * attributes, need to allocate an attr number.
8739 */
8740 if (sgp->sg_start == NULL && sgp->sg_stop == NULL)
8741 sgp->sg_term_attr = sgp->sg_term;
8742 else
8743 {
8744 at_en.ae_attr = sgp->sg_term;
8745 at_en.ae_u.term.start = sgp->sg_start;
8746 at_en.ae_u.term.stop = sgp->sg_stop;
8747 sgp->sg_term_attr = get_attr_entry(&term_attr_table, &at_en);
8748 }
8749
8750 /*
8751 * For the color term mode: If there are other than "normal"
8752 * highlighting attributes, need to allocate an attr number.
8753 */
8754 if (sgp->sg_cterm_fg == 0 && sgp->sg_cterm_bg == 0)
8755 sgp->sg_cterm_attr = sgp->sg_cterm;
8756 else
8757 {
8758 at_en.ae_attr = sgp->sg_cterm;
8759 at_en.ae_u.cterm.fg_color = sgp->sg_cterm_fg;
8760 at_en.ae_u.cterm.bg_color = sgp->sg_cterm_bg;
8761 sgp->sg_cterm_attr = get_attr_entry(&cterm_attr_table, &at_en);
8762 }
8763}
8764
8765/*
8766 * Lookup a highlight group name and return it's ID.
8767 * If it is not found, 0 is returned.
8768 */
8769 int
8770syn_name2id(name)
8771 char_u *name;
8772{
8773 int i;
8774 char_u name_u[200];
8775
8776 /* Avoid using stricmp() too much, it's slow on some systems */
8777 /* Avoid alloc()/free(), these are slow too. ID names over 200 chars
8778 * don't deserve to be found! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008779 vim_strncpy(name_u, name, 199);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 vim_strup(name_u);
8781 for (i = highlight_ga.ga_len; --i >= 0; )
8782 if (HL_TABLE()[i].sg_name_u != NULL
8783 && STRCMP(name_u, HL_TABLE()[i].sg_name_u) == 0)
8784 break;
8785 return i + 1;
8786}
8787
8788#if defined(FEAT_EVAL) || defined(PROTO)
8789/*
8790 * Return TRUE if highlight group "name" exists.
8791 */
8792 int
8793highlight_exists(name)
8794 char_u *name;
8795{
8796 return (syn_name2id(name) > 0);
8797}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008798
8799# if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8800/*
8801 * Return the name of highlight group "id".
8802 * When not a valid ID return an empty string.
8803 */
8804 char_u *
8805syn_id2name(id)
8806 int id;
8807{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008808 if (id <= 0 || id > highlight_ga.ga_len)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008809 return (char_u *)"";
8810 return HL_TABLE()[id - 1].sg_name;
8811}
8812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813#endif
8814
8815/*
8816 * Like syn_name2id(), but take a pointer + length argument.
8817 */
8818 int
8819syn_namen2id(linep, len)
8820 char_u *linep;
8821 int len;
8822{
8823 char_u *name;
8824 int id = 0;
8825
8826 name = vim_strnsave(linep, len);
8827 if (name != NULL)
8828 {
8829 id = syn_name2id(name);
8830 vim_free(name);
8831 }
8832 return id;
8833}
8834
8835/*
8836 * Find highlight group name in the table and return it's ID.
8837 * The argument is a pointer to the name and the length of the name.
8838 * If it doesn't exist yet, a new entry is created.
8839 * Return 0 for failure.
8840 */
8841 int
8842syn_check_group(pp, len)
8843 char_u *pp;
8844 int len;
8845{
8846 int id;
8847 char_u *name;
8848
8849 name = vim_strnsave(pp, len);
8850 if (name == NULL)
8851 return 0;
8852
8853 id = syn_name2id(name);
8854 if (id == 0) /* doesn't exist yet */
8855 id = syn_add_group(name);
8856 else
8857 vim_free(name);
8858 return id;
8859}
8860
8861/*
8862 * Add new highlight group and return it's ID.
8863 * "name" must be an allocated string, it will be consumed.
8864 * Return 0 for failure.
8865 */
8866 static int
8867syn_add_group(name)
8868 char_u *name;
8869{
8870 char_u *p;
8871
8872 /* Check that the name is ASCII letters, digits and underscore. */
8873 for (p = name; *p != NUL; ++p)
8874 {
8875 if (!vim_isprintc(*p))
8876 {
8877 EMSG(_("E669: Unprintable character in group name"));
Bram Moolenaarf5b63862009-12-16 17:13:44 +00008878 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 return 0;
8880 }
8881 else if (!ASCII_ISALNUM(*p) && *p != '_')
8882 {
8883 /* This is an error, but since there previously was no check only
8884 * give a warning. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00008885 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 MSG(_("W18: Invalid character in group name"));
8887 break;
8888 }
8889 }
8890
8891 /*
8892 * First call for this growarray: init growing array.
8893 */
8894 if (highlight_ga.ga_data == NULL)
8895 {
8896 highlight_ga.ga_itemsize = sizeof(struct hl_group);
8897 highlight_ga.ga_growsize = 10;
8898 }
8899
8900 /*
8901 * Make room for at least one other syntax_highlight entry.
8902 */
8903 if (ga_grow(&highlight_ga, 1) == FAIL)
8904 {
8905 vim_free(name);
8906 return 0;
8907 }
8908
8909 vim_memset(&(HL_TABLE()[highlight_ga.ga_len]), 0, sizeof(struct hl_group));
8910 HL_TABLE()[highlight_ga.ga_len].sg_name = name;
8911 HL_TABLE()[highlight_ga.ga_len].sg_name_u = vim_strsave_up(name);
8912#ifdef FEAT_GUI
8913 HL_TABLE()[highlight_ga.ga_len].sg_gui_bg = INVALCOLOR;
8914 HL_TABLE()[highlight_ga.ga_len].sg_gui_fg = INVALCOLOR;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008915 HL_TABLE()[highlight_ga.ga_len].sg_gui_sp = INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916#endif
8917 ++highlight_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918
8919 return highlight_ga.ga_len; /* ID is index plus one */
8920}
8921
8922/*
8923 * When, just after calling syn_add_group(), an error is discovered, this
8924 * function deletes the new name.
8925 */
8926 static void
8927syn_unadd_group()
8928{
8929 --highlight_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name);
8931 vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name_u);
8932}
8933
8934/*
8935 * Translate a group ID to highlight attributes.
8936 */
8937 int
8938syn_id2attr(hl_id)
8939 int hl_id;
8940{
8941 int attr;
8942 struct hl_group *sgp;
8943
8944 hl_id = syn_get_final_id(hl_id);
8945 sgp = &HL_TABLE()[hl_id - 1]; /* index is ID minus one */
8946
8947#ifdef FEAT_GUI
8948 /*
8949 * Only use GUI attr when the GUI is being used.
8950 */
8951 if (gui.in_use)
8952 attr = sgp->sg_gui_attr;
8953 else
8954#endif
8955 if (t_colors > 1)
8956 attr = sgp->sg_cterm_attr;
8957 else
8958 attr = sgp->sg_term_attr;
8959
8960 return attr;
8961}
8962
8963#ifdef FEAT_GUI
8964/*
8965 * Get the GUI colors and attributes for a group ID.
8966 * NOTE: the colors will be INVALCOLOR when not set, the color otherwise.
8967 */
8968 int
8969syn_id2colors(hl_id, fgp, bgp)
8970 int hl_id;
8971 guicolor_T *fgp;
8972 guicolor_T *bgp;
8973{
8974 struct hl_group *sgp;
8975
8976 hl_id = syn_get_final_id(hl_id);
8977 sgp = &HL_TABLE()[hl_id - 1]; /* index is ID minus one */
8978
8979 *fgp = sgp->sg_gui_fg;
8980 *bgp = sgp->sg_gui_bg;
8981 return sgp->sg_gui;
8982}
8983#endif
8984
8985/*
8986 * Translate a group ID to the final group ID (following links).
8987 */
8988 int
8989syn_get_final_id(hl_id)
8990 int hl_id;
8991{
8992 int count;
8993 struct hl_group *sgp;
8994
8995 if (hl_id > highlight_ga.ga_len || hl_id < 1)
8996 return 0; /* Can be called from eval!! */
8997
8998 /*
8999 * Follow links until there is no more.
9000 * Look out for loops! Break after 100 links.
9001 */
9002 for (count = 100; --count >= 0; )
9003 {
9004 sgp = &HL_TABLE()[hl_id - 1]; /* index is ID minus one */
9005 if (sgp->sg_link == 0 || sgp->sg_link > highlight_ga.ga_len)
9006 break;
9007 hl_id = sgp->sg_link;
9008 }
9009
9010 return hl_id;
9011}
9012
9013#ifdef FEAT_GUI
9014/*
9015 * Call this function just after the GUI has started.
9016 * It finds the font and color handles for the highlighting groups.
9017 */
9018 void
9019highlight_gui_started()
9020{
9021 int idx;
9022
9023 /* First get the colors from the "Normal" and "Menu" group, if set */
9024 set_normal_colors();
9025
9026 for (idx = 0; idx < highlight_ga.ga_len; ++idx)
9027 gui_do_one_color(idx, FALSE, FALSE);
9028
9029 highlight_changed();
9030}
9031
9032 static void
9033gui_do_one_color(idx, do_menu, do_tooltip)
9034 int idx;
9035 int do_menu; /* TRUE: might set the menu font */
9036 int do_tooltip; /* TRUE: might set the tooltip font */
9037{
9038 int didit = FALSE;
9039
9040 if (HL_TABLE()[idx].sg_font_name != NULL)
9041 {
9042 hl_do_font(idx, HL_TABLE()[idx].sg_font_name, FALSE, do_menu,
9043 do_tooltip);
9044 didit = TRUE;
9045 }
9046 if (HL_TABLE()[idx].sg_gui_fg_name != NULL)
9047 {
9048 HL_TABLE()[idx].sg_gui_fg =
9049 color_name2handle(HL_TABLE()[idx].sg_gui_fg_name);
9050 didit = TRUE;
9051 }
9052 if (HL_TABLE()[idx].sg_gui_bg_name != NULL)
9053 {
9054 HL_TABLE()[idx].sg_gui_bg =
9055 color_name2handle(HL_TABLE()[idx].sg_gui_bg_name);
9056 didit = TRUE;
9057 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009058 if (HL_TABLE()[idx].sg_gui_sp_name != NULL)
9059 {
9060 HL_TABLE()[idx].sg_gui_sp =
9061 color_name2handle(HL_TABLE()[idx].sg_gui_sp_name);
9062 didit = TRUE;
9063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 if (didit) /* need to get a new attr number */
9065 set_hl_attr(idx);
9066}
9067
9068#endif
9069
9070/*
9071 * Translate the 'highlight' option into attributes in highlight_attr[] and
9072 * set up the user highlights User1..9. If FEAT_STL_OPT is in use, a set of
9073 * corresponding highlights to use on top of HLF_SNC is computed.
9074 * Called only when the 'highlight' option has been changed and upon first
9075 * screen redraw after any :highlight command.
9076 * Return FAIL when an invalid flag is found in 'highlight'. OK otherwise.
9077 */
9078 int
9079highlight_changed()
9080{
9081 int hlf;
9082 int i;
9083 char_u *p;
9084 int attr;
9085 char_u *end;
9086 int id;
9087#ifdef USER_HIGHLIGHT
9088 char_u userhl[10];
9089# ifdef FEAT_STL_OPT
9090 int id_SNC = -1;
9091 int id_S = -1;
9092 int hlcnt;
9093# endif
9094#endif
9095 static int hl_flags[HLF_COUNT] = HL_FLAGS;
9096
9097 need_highlight_changed = FALSE;
9098
9099 /*
9100 * Clear all attributes.
9101 */
9102 for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
9103 highlight_attr[hlf] = 0;
9104
9105 /*
9106 * First set all attributes to their default value.
9107 * Then use the attributes from the 'highlight' option.
9108 */
9109 for (i = 0; i < 2; ++i)
9110 {
9111 if (i)
9112 p = p_hl;
9113 else
9114 p = get_highlight_default();
9115 if (p == NULL) /* just in case */
9116 continue;
9117
9118 while (*p)
9119 {
9120 for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
9121 if (hl_flags[hlf] == *p)
9122 break;
9123 ++p;
9124 if (hlf == (int)HLF_COUNT || *p == NUL)
9125 return FAIL;
9126
9127 /*
9128 * Allow several hl_flags to be combined, like "bu" for
9129 * bold-underlined.
9130 */
9131 attr = 0;
9132 for ( ; *p && *p != ','; ++p) /* parse upto comma */
9133 {
9134 if (vim_iswhite(*p)) /* ignore white space */
9135 continue;
9136
9137 if (attr > HL_ALL) /* Combination with ':' is not allowed. */
9138 return FAIL;
9139
9140 switch (*p)
9141 {
9142 case 'b': attr |= HL_BOLD;
9143 break;
9144 case 'i': attr |= HL_ITALIC;
9145 break;
9146 case '-':
9147 case 'n': /* no highlighting */
9148 break;
9149 case 'r': attr |= HL_INVERSE;
9150 break;
9151 case 's': attr |= HL_STANDOUT;
9152 break;
9153 case 'u': attr |= HL_UNDERLINE;
9154 break;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009155 case 'c': attr |= HL_UNDERCURL;
9156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 case ':': ++p; /* highlight group name */
9158 if (attr || *p == NUL) /* no combinations */
9159 return FAIL;
9160 end = vim_strchr(p, ',');
9161 if (end == NULL)
9162 end = p + STRLEN(p);
9163 id = syn_check_group(p, (int)(end - p));
9164 if (id == 0)
9165 return FAIL;
9166 attr = syn_id2attr(id);
9167 p = end - 1;
9168#if defined(FEAT_STL_OPT) && defined(USER_HIGHLIGHT)
9169 if (hlf == (int)HLF_SNC)
9170 id_SNC = syn_get_final_id(id);
9171 else if (hlf == (int)HLF_S)
9172 id_S = syn_get_final_id(id);
9173#endif
9174 break;
9175 default: return FAIL;
9176 }
9177 }
9178 highlight_attr[hlf] = attr;
9179
9180 p = skip_to_option_part(p); /* skip comma and spaces */
9181 }
9182 }
9183
9184#ifdef USER_HIGHLIGHT
9185 /* Setup the user highlights
9186 *
9187 * Temporarily utilize 10 more hl entries. Have to be in there
9188 * simultaneously in case of table overflows in get_attr_entry()
9189 */
9190# ifdef FEAT_STL_OPT
9191 if (ga_grow(&highlight_ga, 10) == FAIL)
9192 return FAIL;
9193 hlcnt = highlight_ga.ga_len;
9194 if (id_S == 0)
9195 { /* Make sure id_S is always valid to simplify code below */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02009196 vim_memset(&HL_TABLE()[hlcnt + 9], 0, sizeof(struct hl_group));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 HL_TABLE()[hlcnt + 9].sg_term = highlight_attr[HLF_S];
9198 id_S = hlcnt + 10;
9199 }
9200# endif
9201 for (i = 0; i < 9; i++)
9202 {
9203 sprintf((char *)userhl, "User%d", i + 1);
9204 id = syn_name2id(userhl);
9205 if (id == 0)
9206 {
9207 highlight_user[i] = 0;
9208# ifdef FEAT_STL_OPT
9209 highlight_stlnc[i] = 0;
9210# endif
9211 }
9212 else
9213 {
9214# ifdef FEAT_STL_OPT
9215 struct hl_group *hlt = HL_TABLE();
9216# endif
9217
9218 highlight_user[i] = syn_id2attr(id);
9219# ifdef FEAT_STL_OPT
9220 if (id_SNC == 0)
9221 {
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02009222 vim_memset(&hlt[hlcnt + i], 0, sizeof(struct hl_group));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 hlt[hlcnt + i].sg_term = highlight_attr[HLF_SNC];
9224 hlt[hlcnt + i].sg_cterm = highlight_attr[HLF_SNC];
9225# ifdef FEAT_GUI
9226 hlt[hlcnt + i].sg_gui = highlight_attr[HLF_SNC];
9227# endif
9228 }
9229 else
9230 mch_memmove(&hlt[hlcnt + i],
9231 &hlt[id_SNC - 1],
9232 sizeof(struct hl_group));
9233 hlt[hlcnt + i].sg_link = 0;
9234
9235 /* Apply difference between UserX and HLF_S to HLF_SNC */
9236 hlt[hlcnt + i].sg_term ^=
9237 hlt[id - 1].sg_term ^ hlt[id_S - 1].sg_term;
9238 if (hlt[id - 1].sg_start != hlt[id_S - 1].sg_start)
9239 hlt[hlcnt + i].sg_start = hlt[id - 1].sg_start;
9240 if (hlt[id - 1].sg_stop != hlt[id_S - 1].sg_stop)
9241 hlt[hlcnt + i].sg_stop = hlt[id - 1].sg_stop;
9242 hlt[hlcnt + i].sg_cterm ^=
9243 hlt[id - 1].sg_cterm ^ hlt[id_S - 1].sg_cterm;
9244 if (hlt[id - 1].sg_cterm_fg != hlt[id_S - 1].sg_cterm_fg)
9245 hlt[hlcnt + i].sg_cterm_fg = hlt[id - 1].sg_cterm_fg;
9246 if (hlt[id - 1].sg_cterm_bg != hlt[id_S - 1].sg_cterm_bg)
9247 hlt[hlcnt + i].sg_cterm_bg = hlt[id - 1].sg_cterm_bg;
9248# ifdef FEAT_GUI
9249 hlt[hlcnt + i].sg_gui ^=
9250 hlt[id - 1].sg_gui ^ hlt[id_S - 1].sg_gui;
9251 if (hlt[id - 1].sg_gui_fg != hlt[id_S - 1].sg_gui_fg)
9252 hlt[hlcnt + i].sg_gui_fg = hlt[id - 1].sg_gui_fg;
9253 if (hlt[id - 1].sg_gui_bg != hlt[id_S - 1].sg_gui_bg)
9254 hlt[hlcnt + i].sg_gui_bg = hlt[id - 1].sg_gui_bg;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009255 if (hlt[id - 1].sg_gui_sp != hlt[id_S - 1].sg_gui_sp)
9256 hlt[hlcnt + i].sg_gui_sp = hlt[id - 1].sg_gui_sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 if (hlt[id - 1].sg_font != hlt[id_S - 1].sg_font)
9258 hlt[hlcnt + i].sg_font = hlt[id - 1].sg_font;
9259# ifdef FEAT_XFONTSET
9260 if (hlt[id - 1].sg_fontset != hlt[id_S - 1].sg_fontset)
9261 hlt[hlcnt + i].sg_fontset = hlt[id - 1].sg_fontset;
9262# endif
9263# endif
9264 highlight_ga.ga_len = hlcnt + i + 1;
9265 set_hl_attr(hlcnt + i); /* At long last we can apply */
9266 highlight_stlnc[i] = syn_id2attr(hlcnt + i + 1);
9267# endif
9268 }
9269 }
9270# ifdef FEAT_STL_OPT
9271 highlight_ga.ga_len = hlcnt;
9272# endif
9273
9274#endif /* USER_HIGHLIGHT */
9275
9276 return OK;
9277}
9278
Bram Moolenaar4f688582007-07-24 12:34:30 +00009279#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280
9281static void highlight_list __ARGS((void));
9282static void highlight_list_two __ARGS((int cnt, int attr));
9283
9284/*
9285 * Handle command line completion for :highlight command.
9286 */
9287 void
9288set_context_in_highlight_cmd(xp, arg)
9289 expand_T *xp;
9290 char_u *arg;
9291{
9292 char_u *p;
9293
9294 /* Default: expand group names */
9295 xp->xp_context = EXPAND_HIGHLIGHT;
9296 xp->xp_pattern = arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00009297 include_link = 2;
9298 include_default = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
9300 /* (part of) subcommand already typed */
9301 if (*arg != NUL)
9302 {
9303 p = skiptowhite(arg);
9304 if (*p != NUL) /* past "default" or group name */
9305 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00009306 include_default = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 if (STRNCMP("default", arg, p - arg) == 0)
9308 {
9309 arg = skipwhite(p);
9310 xp->xp_pattern = arg;
9311 p = skiptowhite(arg);
9312 }
9313 if (*p != NUL) /* past group name */
9314 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00009315 include_link = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 if (arg[1] == 'i' && arg[0] == 'N')
9317 highlight_list();
9318 if (STRNCMP("link", arg, p - arg) == 0
9319 || STRNCMP("clear", arg, p - arg) == 0)
9320 {
9321 xp->xp_pattern = skipwhite(p);
9322 p = skiptowhite(xp->xp_pattern);
9323 if (*p != NUL) /* past first group name */
9324 {
9325 xp->xp_pattern = skipwhite(p);
9326 p = skiptowhite(xp->xp_pattern);
9327 }
9328 }
9329 if (*p != NUL) /* past group name(s) */
9330 xp->xp_context = EXPAND_NOTHING;
9331 }
9332 }
9333 }
9334}
9335
9336/*
9337 * List highlighting matches in a nice way.
9338 */
9339 static void
9340highlight_list()
9341{
9342 int i;
9343
9344 for (i = 10; --i >= 0; )
9345 highlight_list_two(i, hl_attr(HLF_D));
9346 for (i = 40; --i >= 0; )
9347 highlight_list_two(99, 0);
9348}
9349
9350 static void
9351highlight_list_two(cnt, attr)
9352 int cnt;
9353 int attr;
9354{
9355 msg_puts_attr((char_u *)("N \bI \b! \b" + cnt / 11), attr);
9356 msg_clr_eos();
9357 out_flush();
9358 ui_delay(cnt == 99 ? 40L : (long)cnt * 50L, FALSE);
9359}
9360
9361#endif /* FEAT_CMDL_COMPL */
9362
9363#if defined(FEAT_CMDL_COMPL) || (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) \
9364 || defined(FEAT_SIGNS) || defined(PROTO)
9365/*
9366 * Function given to ExpandGeneric() to obtain the list of group names.
9367 * Also used for synIDattr() function.
9368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 char_u *
9370get_highlight_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009371 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 int idx;
9373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374#ifdef FEAT_CMDL_COMPL
Bram Moolenaar4f688582007-07-24 12:34:30 +00009375 if (idx == highlight_ga.ga_len && include_none != 0)
9376 return (char_u *)"none";
9377 if (idx == highlight_ga.ga_len + include_none && include_default != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 return (char_u *)"default";
Bram Moolenaar4f688582007-07-24 12:34:30 +00009379 if (idx == highlight_ga.ga_len + include_none + include_default
9380 && include_link != 0)
9381 return (char_u *)"link";
9382 if (idx == highlight_ga.ga_len + include_none + include_default + 1
9383 && include_link != 0)
9384 return (char_u *)"clear";
9385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 if (idx < 0 || idx >= highlight_ga.ga_len)
9387 return NULL;
9388 return HL_TABLE()[idx].sg_name;
9389}
9390#endif
9391
Bram Moolenaar4f688582007-07-24 12:34:30 +00009392#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393/*
9394 * Free all the highlight group fonts.
9395 * Used when quitting for systems which need it.
9396 */
9397 void
9398free_highlight_fonts()
9399{
9400 int idx;
9401
9402 for (idx = 0; idx < highlight_ga.ga_len; ++idx)
9403 {
9404 gui_mch_free_font(HL_TABLE()[idx].sg_font);
9405 HL_TABLE()[idx].sg_font = NOFONT;
9406# ifdef FEAT_XFONTSET
9407 gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
9408 HL_TABLE()[idx].sg_fontset = NOFONTSET;
9409# endif
9410 }
9411
9412 gui_mch_free_font(gui.norm_font);
9413# ifdef FEAT_XFONTSET
9414 gui_mch_free_fontset(gui.fontset);
9415# endif
9416# ifndef HAVE_GTK2
9417 gui_mch_free_font(gui.bold_font);
9418 gui_mch_free_font(gui.ital_font);
9419 gui_mch_free_font(gui.boldital_font);
9420# endif
9421}
9422#endif
9423
9424/**************************************
9425 * End of Highlighting stuff *
9426 **************************************/