blob: b4d3afc5d72b9a245c8b2d840db5db68f2bf128f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#if defined(FEAT_SYN_HL) || defined(PROTO)
17
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010018#define SYN_NAMELEN 50 // maximum length of a syntax name
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010020// different types of offsets that are possible
21#define SPO_MS_OFF 0 // match start offset
22#define SPO_ME_OFF 1 // match end offset
23#define SPO_HS_OFF 2 // highl. start offset
24#define SPO_HE_OFF 3 // highl. end offset
25#define SPO_RS_OFF 4 // region start offset
26#define SPO_RE_OFF 5 // region end offset
27#define SPO_LC_OFF 6 // leading context offset
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#define SPO_COUNT 7
29
30static char *(spo_name_tab[SPO_COUNT]) =
31 {"ms=", "me=", "hs=", "he=", "rs=", "re=", "lc="};
32
33/*
34 * The patterns that are being searched for are stored in a syn_pattern.
35 * A match item consists of one pattern.
36 * A start/end item consists of n start patterns and m end patterns.
37 * A start/skip/end item consists of n start patterns, one skip pattern and m
38 * end patterns.
39 * For the latter two, the patterns are always consecutive: start-skip-end.
40 *
41 * A character offset can be given for the matched text (_m_start and _m_end)
42 * and for the actually highlighted text (_h_start and _h_end).
Bram Moolenaar36f92302018-02-24 21:36:34 +010043 *
44 * Note that ordering of members is optimized to reduce padding.
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 */
46typedef struct syn_pattern
47{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010048 char sp_type; // see SPTYPE_ defines below
49 char sp_syncing; // this item used for syncing
50 short sp_syn_match_id; // highlight group ID of pattern
51 short sp_off_flags; // see below
52 int sp_offsets[SPO_COUNT]; // offsets
53 int sp_flags; // see HL_ defines below
Bram Moolenaar860cae12010-06-05 23:22:07 +020054#ifdef FEAT_CONCEAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010055 int sp_cchar; // conceal substitute character
Bram Moolenaar860cae12010-06-05 23:22:07 +020056#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010057 int sp_ic; // ignore-case flag for sp_prog
58 int sp_sync_idx; // sync item index (syncing only)
59 int sp_line_id; // ID of last line where tried
60 int sp_startcol; // next match in sp_line_id line
61 short *sp_cont_list; // cont. group IDs, if non-zero
62 short *sp_next_list; // next group IDs, if non-zero
63 struct sp_syn sp_syn; // struct passed to in_id_list()
64 char_u *sp_pattern; // regexp to match, pattern
65 regprog_T *sp_prog; // regexp to match, program
Bram Moolenaarf7512552013-06-06 14:55:19 +020066#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +020067 syn_time_T sp_time;
68#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069} synpat_T;
70
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010071// The sp_off_flags are computed like this:
72// offset from the start of the matched text: (1 << SPO_XX_OFF)
73// offset from the end of the matched text: (1 << (SPO_XX_OFF + SPO_COUNT))
74// When both are present, only one is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010076#define SPTYPE_MATCH 1 // match keyword with this group ID
77#define SPTYPE_START 2 // match a regexp, start of item
78#define SPTYPE_END 3 // match a regexp, end of item
79#define SPTYPE_SKIP 4 // match a regexp, skip within item
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
82#define SYN_ITEMS(buf) ((synpat_T *)((buf)->b_syn_patterns.ga_data))
83
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010084#define NONE_IDX -2 // value of sp_sync_idx for "NONE"
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
86/*
87 * Flags for b_syn_sync_flags:
88 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010089#define SF_CCOMMENT 0x01 // sync on a C-style comment
90#define SF_MATCH 0x02 // sync by matching a pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92#define SYN_STATE_P(ssp) ((bufstate_T *)((ssp)->ga_data))
93
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010094#define MAXKEYWLEN 80 // maximum length of a keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
96/*
97 * The attributes of the syntax item that has been recognized.
98 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010099static int current_attr = 0; // attr of current syntax word
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_EVAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100101static int current_id = 0; // ID of current char for syn_get_id()
102static int current_trans_id = 0; // idem, transparency removed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200104#ifdef FEAT_CONCEAL
105static int current_flags = 0;
Bram Moolenaar6e202e52010-07-28 18:14:45 +0200106static int current_seqnr = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200107static int current_sub_char = 0;
108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar217ad922005-03-20 22:37:15 +0000110typedef struct syn_cluster_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100112 char_u *scl_name; // syntax cluster name
113 char_u *scl_name_u; // uppercase of scl_name
114 short *scl_list; // IDs in this syntax cluster
Bram Moolenaar217ad922005-03-20 22:37:15 +0000115} syn_cluster_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
117/*
118 * Methods of combining two clusters
119 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100120#define CLUSTER_REPLACE 1 // replace first list with second
121#define CLUSTER_ADD 2 // add second list to first
122#define CLUSTER_SUBTRACT 3 // subtract second list from first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
Bram Moolenaar217ad922005-03-20 22:37:15 +0000124#define SYN_CLSTR(buf) ((syn_cluster_T *)((buf)->b_syn_clusters.ga_data))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
126/*
127 * Syntax group IDs have different types:
Bram Moolenaar42431a72011-04-01 14:44:59 +0200128 * 0 - 19999 normal syntax groups
129 * 20000 - 20999 ALLBUT indicator (current_syn_inc_tag added)
130 * 21000 - 21999 TOP indicator (current_syn_inc_tag added)
131 * 22000 - 22999 CONTAINED indicator (current_syn_inc_tag added)
132 * 23000 - 32767 cluster IDs (subtract SYNID_CLUSTER for the cluster ID)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100134#define SYNID_ALLBUT MAX_HL_ID // syntax group ID for contains=ALLBUT
135#define SYNID_TOP 21000 // syntax group ID for contains=TOP
136#define SYNID_CONTAINED 22000 // syntax group ID for contains=CONTAINED
137#define SYNID_CLUSTER 23000 // first syntax group ID for clusters
Bram Moolenaar42431a72011-04-01 14:44:59 +0200138
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100139#define MAX_SYN_INC_TAG 999 // maximum before the above overflow
Bram Moolenaar42431a72011-04-01 14:44:59 +0200140#define MAX_CLUSTER_ID (32767 - SYNID_CLUSTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142/*
143 * Annoying Hack(TM): ":syn include" needs this pointer to pass to
144 * expand_filename(). Most of the other syntax commands don't need it, so
145 * instead of passing it to them, we stow it here.
146 */
147static char_u **syn_cmdlinep;
148
149/*
150 * Another Annoying Hack(TM): To prevent rules from other ":syn include"'d
Bram Moolenaar56be9502010-06-06 14:20:26 +0200151 * files from leaking into ALLBUT lists, we assign a unique ID to the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 * rules in each ":syn include"'d file.
153 */
154static int current_syn_inc_tag = 0;
155static int running_syn_inc_tag = 0;
156
157/*
Bram Moolenaardad6b692005-01-25 22:14:34 +0000158 * In a hashtable item "hi_key" points to "keyword" in a keyentry.
159 * This avoids adding a pointer to the hashtable item.
160 * KE2HIKEY() converts a var pointer to a hashitem key pointer.
161 * HIKEY2KE() converts a hashitem key pointer to a var pointer.
162 * HI2KE() converts a hashitem pointer to a var pointer.
163 */
164static keyentry_T dumkey;
165#define KE2HIKEY(kp) ((kp)->keyword)
166#define HIKEY2KE(p) ((keyentry_T *)((p) - (dumkey.keyword - (char_u *)&dumkey)))
167#define HI2KE(hi) HIKEY2KE((hi)->hi_key)
168
169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 * To reduce the time spent in keepend(), remember at which level in the state
171 * stack the first item with "keepend" is present. When "-1", there is no
172 * "keepend" on the stack.
173 */
174static int keepend_level = -1;
175
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200176static char msg_no_items[] = N_("No Syntax items defined for this buffer");
177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178/*
179 * For the current state we need to remember more than just the idx.
180 * When si_m_endpos.lnum is 0, the items other than si_idx are unknown.
181 * (The end positions have the column number of the next char)
182 */
183typedef struct state_item
184{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100185 int si_idx; // index of syntax pattern or
186 // KEYWORD_IDX
187 int si_id; // highlight group ID for keywords
188 int si_trans_id; // idem, transparency removed
189 int si_m_lnum; // lnum of the match
190 int si_m_startcol; // starting column of the match
191 lpos_T si_m_endpos; // just after end posn of the match
192 lpos_T si_h_startpos; // start position of the highlighting
193 lpos_T si_h_endpos; // end position of the highlighting
194 lpos_T si_eoe_pos; // end position of end pattern
195 int si_end_idx; // group ID for end pattern or zero
196 int si_ends; // if match ends before si_m_endpos
197 int si_attr; // attributes in this state
198 long si_flags; // HL_HAS_EOL flag in this state, and
199 // HL_SKIP* for si_next_list
Bram Moolenaar860cae12010-06-05 23:22:07 +0200200#ifdef FEAT_CONCEAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100201 int si_seqnr; // sequence number
202 int si_cchar; // substitution character for conceal
Bram Moolenaar860cae12010-06-05 23:22:07 +0200203#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204 short *si_cont_list; // list of contained groups
205 short *si_next_list; // nextgroup IDs after this item ends
206 reg_extmatch_T *si_extmatch; // \z(...\) matches from start
207 // pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208} stateitem_T;
209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100210#define KEYWORD_IDX -1 // value of si_idx for keywords
211#define ID_LIST_ALL (short *)-1 // valid of si_cont_list for containing all
212 // but contained groups
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaarffbbcb52010-07-24 17:29:03 +0200214#ifdef FEAT_CONCEAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100215static int next_seqnr = 1; // value to use for si_seqnr
Bram Moolenaarffbbcb52010-07-24 17:29:03 +0200216#endif
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/*
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000219 * Struct to reduce the number of arguments to get_syn_options(), it's used
220 * very often.
221 */
222typedef struct
223{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100224 int flags; // flags for contained and transparent
225 int keyword; // TRUE for ":syn keyword"
226 int *sync_idx; // syntax item for "grouphere" argument, NULL
227 // if not allowed
228 char has_cont_list; // TRUE if "cont_list" can be used
229 short *cont_list; // group IDs for "contains" argument
230 short *cont_in_list; // group IDs for "containedin" argument
231 short *next_list; // group IDs for "nextgroup" argument
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000232} syn_opt_arg_T;
233
234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 * The next possible match in the current line for any pattern is remembered,
236 * to avoid having to try for a match in each column.
237 * If next_match_idx == -1, not tried (in this line) yet.
238 * If next_match_col == MAXCOL, no match found in this line.
239 * (All end positions have the column of the char after the end)
240 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100241static int next_match_col; // column for start of next match
242static lpos_T next_match_m_endpos; // position for end of next match
243static lpos_T next_match_h_startpos; // pos. for highl. start of next match
244static lpos_T next_match_h_endpos; // pos. for highl. end of next match
245static int next_match_idx; // index of matched item
246static long next_match_flags; // flags for next match
247static lpos_T next_match_eos_pos; // end of start pattn (start region)
248static lpos_T next_match_eoe_pos; // pos. for end of end pattern
249static int next_match_end_idx; // ID of group for end pattn or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250static reg_extmatch_T *next_match_extmatch = NULL;
251
252/*
253 * A state stack is an array of integers or stateitem_T, stored in a
Bram Moolenaarf86db782018-10-25 13:31:37 +0200254 * garray_T. A state stack is invalid if its itemsize entry is zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 */
256#define INVALID_STATE(ssp) ((ssp)->ga_itemsize == 0)
257#define VALID_STATE(ssp) ((ssp)->ga_itemsize != 0)
258
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200259#define FOR_ALL_SYNSTATES(sb, sst) \
260 for ((sst) = (sb)->b_sst_first; (sst) != NULL; (sst) = (sst)->sst_next)
261
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262/*
263 * The current state (within the line) of the recognition engine.
264 * When current_state.ga_itemsize is 0 the current state is invalid.
265 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100266static win_T *syn_win; // current window for highlighting
267static buf_T *syn_buf; // current buffer for highlighting
268static synblock_T *syn_block; // current buffer for highlighting
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200269#ifdef FEAT_RELTIME
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100270static proftime_T *syn_tm; // timeout limit
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200271#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100272static linenr_T current_lnum = 0; // lnum of current state
273static colnr_T current_col = 0; // column of current state
274static int current_state_stored = 0; // TRUE if stored current state
275 // after setting current_finished
276static int current_finished = 0; // current line has been finished
277static garray_T current_state // current stack of state_items
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 = {0, 0, 0, 0, NULL};
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100279static short *current_next_list = NULL; // when non-zero, nextgroup list
280static int current_next_flags = 0; // flags for current_next_list
281static int current_line_id = 0; // unique number for current line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283#define CUR_STATE(idx) ((stateitem_T *)(current_state.ga_data))[idx]
284
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100285static void syn_sync(win_T *wp, linenr_T lnum, synstate_T *last_valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100286static int syn_match_linecont(linenr_T lnum);
287static void syn_start_line(void);
288static void syn_update_ends(int startofline);
289static void syn_stack_alloc(void);
290static int syn_stack_cleanup(void);
291static void syn_stack_free_entry(synblock_T *block, synstate_T *p);
292static synstate_T *syn_stack_find_entry(linenr_T lnum);
293static synstate_T *store_current_state(void);
294static void load_current_state(synstate_T *from);
295static void invalidate_current_state(void);
296static int syn_stack_equal(synstate_T *sp);
297static void validate_current_state(void);
298static int syn_finish_line(int syncing);
299static int syn_current_attr(int syncing, int displaying, int *can_spell, int keep_state);
300static int did_match_already(int idx, garray_T *gap);
301static stateitem_T *push_next_match(stateitem_T *cur_si);
302static void check_state_ends(void);
303static void update_si_attr(int idx);
304static void check_keepend(void);
305static void update_si_end(stateitem_T *sip, int startcol, int force);
306static short *copy_id_list(short *list);
307static int in_id_list(stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained);
308static int push_current_state(int idx);
309static void pop_current_state(void);
Bram Moolenaarf7512552013-06-06 14:55:19 +0200310#ifdef FEAT_PROFILE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100311static void syn_clear_time(syn_time_T *tt);
312static void syntime_clear(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100313static void syntime_report(void);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200314static int syn_time_on = FALSE;
315# define IF_SYN_TIME(p) (p)
316#else
317# define IF_SYN_TIME(p) NULL
318typedef int syn_time_T;
319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100321static void syn_stack_apply_changes_block(synblock_T *block, buf_T *buf);
322static void find_endpos(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);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100324static void limit_pos(lpos_T *pos, lpos_T *limit);
325static void limit_pos_zero(lpos_T *pos, lpos_T *limit);
326static void syn_add_end_off(lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra);
327static void syn_add_start_off(lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra);
328static char_u *syn_getcurline(void);
329static int syn_regexec(regmmatch_T *rmp, linenr_T lnum, colnr_T col, syn_time_T *st);
330static int check_keyword_id(char_u *line, int startcol, int *endcol, long *flags, short **next_list, stateitem_T *cur_si, int *ccharp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100331static void syn_remove_pattern(synblock_T *block, int idx);
332static void syn_clear_pattern(synblock_T *block, int i);
333static void syn_clear_cluster(synblock_T *block, int i);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100334static void syn_clear_one(int id, int syncing);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100335static void syn_cmd_onoff(exarg_T *eap, char *name);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100336static void syn_lines_msg(void);
337static void syn_match_msg(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100338static void syn_list_one(int id, int syncing, int link_only);
339static void syn_list_cluster(int id);
340static void put_id_list(char_u *name, short *list, int attr);
341static void put_pattern(char *s, int c, synpat_T *spp, int attr);
342static int syn_list_keywords(int id, hashtab_T *ht, int did_header, int attr);
343static void syn_clear_keyword(int id, hashtab_T *ht);
344static void clear_keywtab(hashtab_T *ht);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100345static int syn_scl_namen2id(char_u *linep, int len);
346static int syn_check_cluster(char_u *pp, int len);
347static int syn_add_cluster(char_u *name);
348static void init_syn_patterns(void);
349static char_u *get_syn_pattern(char_u *arg, synpat_T *ci);
Bram Moolenaarde318c52017-01-17 16:27:10 +0100350static int get_id_list(char_u **arg, int keylen, short **list, int skip);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100351static void syn_combine_list(short **clstr1, short **clstr2, int list_op);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200353#if defined(FEAT_RELTIME) || defined(PROTO)
354/*
355 * Set the timeout used for syntax highlighting.
356 * Use NULL to reset, no timeout.
357 */
358 void
359syn_set_timeout(proftime_T *tm)
360{
361 syn_tm = tm;
362}
363#endif
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365/*
366 * Start the syntax recognition for a line. This function is normally called
367 * from the screen updating, once for each displayed line.
368 * The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
369 * it. Careful: curbuf and curwin are likely to point to another buffer and
370 * window.
371 */
372 void
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200373syntax_start(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374{
375 synstate_T *p;
376 synstate_T *last_valid = NULL;
377 synstate_T *last_min_valid = NULL;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000378 synstate_T *sp, *prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 linenr_T parsed_lnum;
380 linenr_T first_stored;
381 int dist;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100382 static varnumber_T changedtick = 0; // remember the last change ID
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200384#ifdef FEAT_CONCEAL
385 current_sub_char = NUL;
386#endif
387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 /*
389 * After switching buffers, invalidate current_state.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000390 * Also do this when a change was made, the current state may be invalid
391 * then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 */
Bram Moolenaarb681be12016-03-31 23:02:16 +0200393 if (syn_block != wp->w_s
394 || syn_buf != wp->w_buffer
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100395 || changedtick != CHANGEDTICK(syn_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {
397 invalidate_current_state();
398 syn_buf = wp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200399 syn_block = wp->w_s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100401 changedtick = CHANGEDTICK(syn_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 syn_win = wp;
403
404 /*
405 * Allocate syntax stack when needed.
406 */
407 syn_stack_alloc();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200408 if (syn_block->b_sst_array == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100409 return; // out of memory
Bram Moolenaar860cae12010-06-05 23:22:07 +0200410 syn_block->b_sst_lasttick = display_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411
412 /*
413 * If the state of the end of the previous line is useful, store it.
414 */
415 if (VALID_STATE(&current_state)
416 && current_lnum < lnum
417 && current_lnum < syn_buf->b_ml.ml_line_count)
418 {
419 (void)syn_finish_line(FALSE);
420 if (!current_state_stored)
421 {
422 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000423 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425
426 /*
427 * If the current_lnum is now the same as "lnum", keep the current
428 * state (this happens very often!). Otherwise invalidate
429 * current_state and figure it out below.
430 */
431 if (current_lnum != lnum)
432 invalidate_current_state();
433 }
434 else
435 invalidate_current_state();
436
437 /*
438 * Try to synchronize from a saved state in b_sst_array[].
439 * Only do this if lnum is not before and not to far beyond a saved state.
440 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200441 if (INVALID_STATE(&current_state) && syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100443 // Find last valid saved state before start_lnum.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200444 FOR_ALL_SYNSTATES(syn_block, p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {
446 if (p->sst_lnum > lnum)
447 break;
448 if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
449 {
450 last_valid = p;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200451 if (p->sst_lnum >= lnum - syn_block->b_syn_sync_minlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 last_min_valid = p;
453 }
454 }
455 if (last_min_valid != NULL)
456 load_current_state(last_min_valid);
457 }
458
459 /*
460 * If "lnum" is before or far beyond a line with a saved state, need to
461 * re-synchronize.
462 */
463 if (INVALID_STATE(&current_state))
464 {
465 syn_sync(wp, lnum, last_valid);
Bram Moolenaard6761c32011-06-19 04:54:21 +0200466 if (current_lnum == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100467 // First line is always valid, no matter "minlines".
Bram Moolenaard6761c32011-06-19 04:54:21 +0200468 first_stored = 1;
469 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100470 // Need to parse "minlines" lines before state can be considered
471 // valid to store.
Bram Moolenaard6761c32011-06-19 04:54:21 +0200472 first_stored = current_lnum + syn_block->b_syn_sync_minlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 }
474 else
475 first_stored = current_lnum;
476
477 /*
478 * Advance from the sync point or saved state until the current line.
479 * Save some entries for syncing with later on.
480 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200481 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000482 dist = 999999;
483 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200484 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 while (current_lnum < lnum)
486 {
487 syn_start_line();
488 (void)syn_finish_line(FALSE);
489 ++current_lnum;
490
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100491 // If we parsed at least "minlines" lines or started at a valid
492 // state, the current state is considered valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 if (current_lnum >= first_stored)
494 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100495 // Check if the saved state entry is for the current line and is
496 // equal to the current state. If so, then validate all saved
497 // states that depended on a change before the parsed line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (prev == NULL)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000499 prev = syn_stack_find_entry(current_lnum - 1);
500 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200501 sp = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 else
Bram Moolenaardbe31752008-01-13 16:40:19 +0000503 sp = prev;
504 while (sp != NULL && sp->sst_lnum < current_lnum)
505 sp = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (sp != NULL
507 && sp->sst_lnum == current_lnum
508 && syn_stack_equal(sp))
509 {
510 parsed_lnum = current_lnum;
511 prev = sp;
512 while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
513 {
514 if (sp->sst_lnum <= lnum)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100515 // valid state before desired line, use this one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 prev = sp;
517 else if (sp->sst_change_lnum == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100518 // past saved states depending on change, break here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 break;
520 sp->sst_change_lnum = 0;
521 sp = sp->sst_next;
522 }
523 load_current_state(prev);
524 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100525 // Store the state at this line when it's the first one, the line
526 // where we start parsing, or some distance from the previously
527 // saved state. But only when parsed at least 'minlines'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 else if (prev == NULL
529 || current_lnum == lnum
530 || current_lnum >= prev->sst_lnum + dist)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000531 prev = store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100534 // This can take a long time: break when CTRL-C pressed. The current
535 // state will be wrong then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 line_breakcheck();
537 if (got_int)
538 {
539 current_lnum = lnum;
540 break;
541 }
542 }
543
544 syn_start_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545}
546
547/*
548 * We cannot simply discard growarrays full of state_items or buf_states; we
549 * have to manually release their extmatch pointers first.
550 */
551 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100552clear_syn_state(synstate_T *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 int i;
555 garray_T *gap;
556
557 if (p->sst_stacksize > SST_FIX_STATES)
558 {
559 gap = &(p->sst_union.sst_ga);
560 for (i = 0; i < gap->ga_len; i++)
561 unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
562 ga_clear(gap);
563 }
564 else
565 {
566 for (i = 0; i < p->sst_stacksize; i++)
567 unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
568 }
569}
570
571/*
572 * Cleanup the current_state stack.
573 */
574 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100575clear_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
577 int i;
578 stateitem_T *sip;
579
580 sip = (stateitem_T *)(current_state.ga_data);
581 for (i = 0; i < current_state.ga_len; i++)
582 unref_extmatch(sip[i].si_extmatch);
583 ga_clear(&current_state);
584}
585
586/*
587 * Try to find a synchronisation point for line "lnum".
588 *
589 * This sets current_lnum and the current state. One of three methods is
590 * used:
591 * 1. Search backwards for the end of a C-comment.
592 * 2. Search backwards for given sync patterns.
593 * 3. Simply start on a given number of lines above "lnum".
594 */
595 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100596syn_sync(
597 win_T *wp,
598 linenr_T start_lnum,
599 synstate_T *last_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 buf_T *curbuf_save;
602 win_T *curwin_save;
603 pos_T cursor_save;
604 int idx;
605 linenr_T lnum;
606 linenr_T end_lnum;
607 linenr_T break_lnum;
608 int had_sync_point;
609 stateitem_T *cur_si;
610 synpat_T *spp;
611 char_u *line;
612 int found_flags = 0;
613 int found_match_idx = 0;
614 linenr_T found_current_lnum = 0;
615 int found_current_col= 0;
616 lpos_T found_m_endpos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000617 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 /*
620 * Clear any current state that might be hanging around.
621 */
622 invalidate_current_state();
623
624 /*
625 * Start at least "minlines" back. Default starting point for parsing is
626 * there.
627 * Start further back, to avoid that scrolling backwards will result in
628 * resyncing for every line. Now it resyncs only one out of N lines,
629 * where N is minlines * 1.5, or minlines * 2 if minlines is small.
630 * Watch out for overflow when minlines is MAXLNUM.
631 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200632 if (syn_block->b_syn_sync_minlines > start_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 start_lnum = 1;
634 else
635 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200636 if (syn_block->b_syn_sync_minlines == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 lnum = 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200638 else if (syn_block->b_syn_sync_minlines < 10)
639 lnum = syn_block->b_syn_sync_minlines * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200641 lnum = syn_block->b_syn_sync_minlines * 3 / 2;
642 if (syn_block->b_syn_sync_maxlines != 0
643 && lnum > syn_block->b_syn_sync_maxlines)
644 lnum = syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (lnum >= start_lnum)
646 start_lnum = 1;
647 else
648 start_lnum -= lnum;
649 }
650 current_lnum = start_lnum;
651
652 /*
653 * 1. Search backwards for the end of a C-style comment.
654 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200655 if (syn_block->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100657 // Need to make syn_buf the current buffer for a moment, to be able to
658 // use find_start_comment().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 curwin_save = curwin;
660 curwin = wp;
661 curbuf_save = curbuf;
662 curbuf = syn_buf;
663
664 /*
665 * Skip lines that end in a backslash.
666 */
667 for ( ; start_lnum > 1; --start_lnum)
668 {
669 line = ml_get(start_lnum - 1);
670 if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
671 break;
672 }
673 current_lnum = start_lnum;
674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100675 // set cursor to start of search
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 cursor_save = wp->w_cursor;
677 wp->w_cursor.lnum = start_lnum;
678 wp->w_cursor.col = 0;
679
680 /*
681 * If the line is inside a comment, need to find the syntax item that
682 * defines the comment.
683 * Restrict the search for the end of a comment to b_syn_sync_maxlines.
684 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200685 if (find_start_comment((int)syn_block->b_syn_sync_maxlines) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200687 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
688 if (SYN_ITEMS(syn_block)[idx].sp_syn.id
689 == syn_block->b_syn_sync_id
690 && SYN_ITEMS(syn_block)[idx].sp_type == SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
692 validate_current_state();
693 if (push_current_state(idx) == OK)
694 update_si_attr(current_state.ga_len - 1);
695 break;
696 }
697 }
698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100699 // restore cursor and buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 wp->w_cursor = cursor_save;
701 curwin = curwin_save;
702 curbuf = curbuf_save;
703 }
704
705 /*
706 * 2. Search backwards for given sync patterns.
707 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200708 else if (syn_block->b_syn_sync_flags & SF_MATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200710 if (syn_block->b_syn_sync_maxlines != 0
711 && start_lnum > syn_block->b_syn_sync_maxlines)
712 break_lnum = start_lnum - syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 else
714 break_lnum = 0;
715
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000716 found_m_endpos.lnum = 0;
717 found_m_endpos.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 end_lnum = start_lnum;
719 lnum = start_lnum;
720 while (--lnum > break_lnum)
721 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100722 // This can take a long time: break when CTRL-C pressed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 line_breakcheck();
724 if (got_int)
725 {
726 invalidate_current_state();
727 current_lnum = start_lnum;
728 break;
729 }
730
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100731 // Check if we have run into a valid saved state stack now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (last_valid != NULL && lnum == last_valid->sst_lnum)
733 {
734 load_current_state(last_valid);
735 break;
736 }
737
738 /*
739 * Check if the previous line has the line-continuation pattern.
740 */
741 if (lnum > 1 && syn_match_linecont(lnum - 1))
742 continue;
743
744 /*
745 * Start with nothing on the state stack
746 */
747 validate_current_state();
748
749 for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
750 {
751 syn_start_line();
752 for (;;)
753 {
754 had_sync_point = syn_finish_line(TRUE);
755 /*
756 * When a sync point has been found, remember where, and
757 * continue to look for another one, further on in the line.
758 */
759 if (had_sync_point && current_state.ga_len)
760 {
761 cur_si = &CUR_STATE(current_state.ga_len - 1);
762 if (cur_si->si_m_endpos.lnum > start_lnum)
763 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100764 // ignore match that goes to after where started
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 current_lnum = end_lnum;
766 break;
767 }
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000768 if (cur_si->si_idx < 0)
769 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100770 // Cannot happen?
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000771 found_flags = 0;
772 found_match_idx = KEYWORD_IDX;
773 }
774 else
775 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200776 spp = &(SYN_ITEMS(syn_block)[cur_si->si_idx]);
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000777 found_flags = spp->sp_flags;
778 found_match_idx = spp->sp_sync_idx;
779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 found_current_lnum = current_lnum;
781 found_current_col = current_col;
782 found_m_endpos = cur_si->si_m_endpos;
783 /*
784 * Continue after the match (be aware of a zero-length
785 * match).
786 */
787 if (found_m_endpos.lnum > current_lnum)
788 {
789 current_lnum = found_m_endpos.lnum;
790 current_col = found_m_endpos.col;
791 if (current_lnum >= end_lnum)
792 break;
793 }
794 else if (found_m_endpos.col > current_col)
795 current_col = found_m_endpos.col;
796 else
797 ++current_col;
798
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100799 // syn_current_attr() will have skipped the check for
800 // an item that ends here, need to do that now. Be
801 // careful not to go past the NUL.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000802 prev_current_col = current_col;
803 if (syn_getcurline()[current_col] != NUL)
804 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +0000806 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
808 else
809 break;
810 }
811 }
812
813 /*
814 * If a sync point was encountered, break here.
815 */
816 if (found_flags)
817 {
818 /*
819 * Put the item that was specified by the sync point on the
820 * state stack. If there was no item specified, make the
821 * state stack empty.
822 */
823 clear_current_state();
824 if (found_match_idx >= 0
825 && push_current_state(found_match_idx) == OK)
826 update_si_attr(current_state.ga_len - 1);
827
828 /*
829 * When using "grouphere", continue from the sync point
830 * match, until the end of the line. Parsing starts at
831 * the next line.
832 * For "groupthere" the parsing starts at start_lnum.
833 */
834 if (found_flags & HL_SYNC_HERE)
835 {
836 if (current_state.ga_len)
837 {
838 cur_si = &CUR_STATE(current_state.ga_len - 1);
839 cur_si->si_h_startpos.lnum = found_current_lnum;
840 cur_si->si_h_startpos.col = found_current_col;
841 update_si_end(cur_si, (int)current_col, TRUE);
842 check_keepend();
843 }
844 current_col = found_m_endpos.col;
845 current_lnum = found_m_endpos.lnum;
846 (void)syn_finish_line(FALSE);
847 ++current_lnum;
848 }
849 else
850 current_lnum = start_lnum;
851
852 break;
853 }
854
855 end_lnum = lnum;
856 invalidate_current_state();
857 }
858
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100859 // Ran into start of the file or exceeded maximum number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (lnum <= break_lnum)
861 {
862 invalidate_current_state();
863 current_lnum = break_lnum + 1;
864 }
865 }
866
867 validate_current_state();
868}
869
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100870 static void
871save_chartab(char_u *chartab)
872{
873 if (syn_block->b_syn_isk != empty_option)
874 {
875 mch_memmove(chartab, syn_buf->b_chartab, (size_t)32);
876 mch_memmove(syn_buf->b_chartab, syn_win->w_s->b_syn_chartab,
877 (size_t)32);
878 }
879}
880
881 static void
882restore_chartab(char_u *chartab)
883{
884 if (syn_win->w_s->b_syn_isk != empty_option)
885 mch_memmove(syn_buf->b_chartab, chartab, (size_t)32);
886}
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888/*
889 * Return TRUE if the line-continuation pattern matches in line "lnum".
890 */
891 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100892syn_match_linecont(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 regmmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100895 int r;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100896 char_u buf_chartab[32]; // chartab array for syn iskyeyword
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaar860cae12010-06-05 23:22:07 +0200898 if (syn_block->b_syn_linecont_prog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100900 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100901 save_chartab(buf_chartab);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200902 regmatch.rmm_ic = syn_block->b_syn_linecont_ic;
903 regmatch.regprog = syn_block->b_syn_linecont_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100904 r = syn_regexec(&regmatch, lnum, (colnr_T)0,
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200905 IF_SYN_TIME(&syn_block->b_syn_linecont_time));
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100906 syn_block->b_syn_linecont_prog = regmatch.regprog;
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100907 restore_chartab(buf_chartab);
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100908 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910 return FALSE;
911}
912
913/*
914 * Prepare the current state for the start of a line.
915 */
916 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100917syn_start_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 current_finished = FALSE;
920 current_col = 0;
921
922 /*
923 * Need to update the end of a start/skip/end that continues from the
924 * previous line and regions that have "keepend".
925 */
926 if (current_state.ga_len > 0)
Bram Moolenaar6fa46362011-05-25 17:56:27 +0200927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 syn_update_ends(TRUE);
Bram Moolenaar6fa46362011-05-25 17:56:27 +0200929 check_state_ends();
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 next_match_idx = -1;
933 ++current_line_id;
Bram Moolenaarea20de82017-06-24 22:52:24 +0200934#ifdef FEAT_CONCEAL
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200935 next_seqnr = 1;
Bram Moolenaarea20de82017-06-24 22:52:24 +0200936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
939/*
940 * Check for items in the stack that need their end updated.
941 * When "startofline" is TRUE the last item is always updated.
942 * When "startofline" is FALSE the item with "keepend" is forcefully updated.
943 */
944 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100945syn_update_ends(int startofline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 stateitem_T *cur_si;
948 int i;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000949 int seen_keepend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
951 if (startofline)
952 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100953 // Check for a match carried over from a previous line with a
954 // contained region. The match ends as soon as the region ends.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 for (i = 0; i < current_state.ga_len; ++i)
956 {
957 cur_si = &CUR_STATE(i);
958 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +0200959 && (SYN_ITEMS(syn_block)[cur_si->si_idx]).sp_type
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 == SPTYPE_MATCH
961 && cur_si->si_m_endpos.lnum < current_lnum)
962 {
963 cur_si->si_flags |= HL_MATCHCONT;
964 cur_si->si_m_endpos.lnum = 0;
965 cur_si->si_m_endpos.col = 0;
966 cur_si->si_h_endpos = cur_si->si_m_endpos;
967 cur_si->si_ends = TRUE;
968 }
969 }
970 }
971
972 /*
973 * Need to update the end of a start/skip/end that continues from the
974 * previous line. And regions that have "keepend", because they may
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000975 * influence contained items. If we've just removed "extend"
976 * (startofline == 0) then we should update ends of normal regions
977 * contained inside "keepend" because "extend" could have extended
978 * these "keepend" regions as well as contained normal regions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * Then check for items ending in column 0.
980 */
981 i = current_state.ga_len - 1;
982 if (keepend_level >= 0)
983 for ( ; i > keepend_level; --i)
984 if (CUR_STATE(i).si_flags & HL_EXTEND)
985 break;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000986
987 seen_keepend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 for ( ; i < current_state.ga_len; ++i)
989 {
990 cur_si = &CUR_STATE(i);
991 if ((cur_si->si_flags & HL_KEEPEND)
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000992 || (seen_keepend && !startofline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 || (i == current_state.ga_len - 1 && startofline))
994 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100995 cur_si->si_h_startpos.col = 0; // start highl. in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 cur_si->si_h_startpos.lnum = current_lnum;
997
998 if (!(cur_si->si_flags & HL_MATCHCONT))
999 update_si_end(cur_si, (int)current_col, !startofline);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00001000
1001 if (!startofline && (cur_si->si_flags & HL_KEEPEND))
1002 seen_keepend = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 }
1005 check_keepend();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
1007
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001008/////////////////////////////////////////
1009// Handling of the state stack cache.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011/*
1012 * EXPLANATION OF THE SYNTAX STATE STACK CACHE
1013 *
1014 * To speed up syntax highlighting, the state stack for the start of some
1015 * lines is cached. These entries can be used to start parsing at that point.
1016 *
1017 * The stack is kept in b_sst_array[] for each buffer. There is a list of
1018 * valid entries. b_sst_first points to the first one, then follow sst_next.
1019 * The entries are sorted on line number. The first entry is often for line 2
1020 * (line 1 always starts with an empty stack).
1021 * There is also a list for free entries. This construction is used to avoid
1022 * having to allocate and free memory blocks too often.
1023 *
1024 * When making changes to the buffer, this is logged in b_mod_*. When calling
1025 * update_screen() to update the display, it will call
1026 * syn_stack_apply_changes() for each displayed buffer to adjust the cached
1027 * entries. The entries which are inside the changed area are removed,
1028 * because they must be recomputed. Entries below the changed have their line
1029 * number adjusted for deleted/inserted lines, and have their sst_change_lnum
1030 * set to indicate that a check must be made if the changed lines would change
1031 * the cached entry.
1032 *
1033 * When later displaying lines, an entry is stored for each line. Displayed
1034 * lines are likely to be displayed again, in which case the state at the
1035 * start of the line is needed.
1036 * For not displayed lines, an entry is stored for every so many lines. These
1037 * entries will be used e.g., when scrolling backwards. The distance between
1038 * entries depends on the number of lines in the buffer. For small buffers
1039 * the distance is fixed at SST_DIST, for large buffers there is a fixed
1040 * number of entries SST_MAX_ENTRIES, and the distance is computed.
1041 */
1042
Bram Moolenaar860cae12010-06-05 23:22:07 +02001043 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001044syn_stack_free_block(synblock_T *block)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001045{
1046 synstate_T *p;
1047
1048 if (block->b_sst_array != NULL)
1049 {
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001050 FOR_ALL_SYNSTATES(block, p)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001051 clear_syn_state(p);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001052 VIM_CLEAR(block->b_sst_array);
Bram Moolenaar95892c22018-09-28 22:26:54 +02001053 block->b_sst_first = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001054 block->b_sst_len = 0;
1055 }
1056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057/*
1058 * Free b_sst_array[] for buffer "buf".
1059 * Used when syntax items changed to force resyncing everywhere.
1060 */
1061 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001062syn_stack_free_all(synblock_T *block)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
Bram Moolenaara6c07602017-03-05 21:18:27 +01001064#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 win_T *wp;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
Bram Moolenaar860cae12010-06-05 23:22:07 +02001068 syn_stack_free_block(block);
1069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#ifdef FEAT_FOLDING
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001071 // When using "syntax" fold method, must update all folds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 FOR_ALL_WINDOWS(wp)
1073 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001074 if (wp->w_s == block && foldmethodIsSyntax(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 foldUpdateAll(wp);
1076 }
1077#endif
1078}
1079
1080/*
1081 * Allocate the syntax state stack for syn_buf when needed.
1082 * If the number of entries in b_sst_array[] is much too big or a bit too
1083 * small, reallocate it.
1084 * Also used to allocate b_sst_array[] for the first time.
1085 */
1086 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001087syn_stack_alloc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 long len;
1090 synstate_T *to, *from;
1091 synstate_T *sstp;
1092
1093 len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
1094 if (len < SST_MIN_ENTRIES)
1095 len = SST_MIN_ENTRIES;
1096 else if (len > SST_MAX_ENTRIES)
1097 len = SST_MAX_ENTRIES;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001098 if (syn_block->b_sst_len > len * 2 || syn_block->b_sst_len < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001100 // Allocate 50% too much, to avoid reallocating too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 len = syn_buf->b_ml.ml_line_count;
1102 len = (len + len / 2) / SST_DIST + Rows * 2;
1103 if (len < SST_MIN_ENTRIES)
1104 len = SST_MIN_ENTRIES;
1105 else if (len > SST_MAX_ENTRIES)
1106 len = SST_MAX_ENTRIES;
1107
Bram Moolenaar860cae12010-06-05 23:22:07 +02001108 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001110 // When shrinking the array, cleanup the existing stack.
1111 // Make sure that all valid entries fit in the new array.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001112 while (syn_block->b_sst_len - syn_block->b_sst_freecount + 2 > len
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 && syn_stack_cleanup())
1114 ;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001115 if (len < syn_block->b_sst_len - syn_block->b_sst_freecount + 2)
1116 len = syn_block->b_sst_len - syn_block->b_sst_freecount + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001119 sstp = ALLOC_CLEAR_MULT(synstate_T, len);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001120 if (sstp == NULL) // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 return;
1122
1123 to = sstp - 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001124 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001126 // Move the states from the old array to the new one.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001127 for (from = syn_block->b_sst_first; from != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 from = from->sst_next)
1129 {
1130 ++to;
1131 *to = *from;
1132 to->sst_next = to + 1;
1133 }
1134 }
1135 if (to != sstp - 1)
1136 {
1137 to->sst_next = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001138 syn_block->b_sst_first = sstp;
1139 syn_block->b_sst_freecount = len - (int)(to - sstp) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
1141 else
1142 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001143 syn_block->b_sst_first = NULL;
1144 syn_block->b_sst_freecount = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001147 // Create the list of free entries.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001148 syn_block->b_sst_firstfree = to + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 while (++to < sstp + len)
1150 to->sst_next = to + 1;
1151 (sstp + len - 1)->sst_next = NULL;
1152
Bram Moolenaar860cae12010-06-05 23:22:07 +02001153 vim_free(syn_block->b_sst_array);
1154 syn_block->b_sst_array = sstp;
1155 syn_block->b_sst_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157}
1158
1159/*
1160 * Check for changes in a buffer to affect stored syntax states. Uses the
1161 * b_mod_* fields.
1162 * Called from update_screen(), before screen is being updated, once for each
1163 * displayed buffer.
1164 */
1165 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001166syn_stack_apply_changes(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001168 win_T *wp;
1169
1170 syn_stack_apply_changes_block(&buf->b_s, buf);
1171
1172 FOR_ALL_WINDOWS(wp)
1173 {
1174 if ((wp->w_buffer == buf) && (wp->w_s != &buf->b_s))
1175 syn_stack_apply_changes_block(wp->w_s, buf);
1176 }
1177}
1178
1179 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001180syn_stack_apply_changes_block(synblock_T *block, buf_T *buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001181{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 synstate_T *p, *prev, *np;
1183 linenr_T n;
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001186 for (p = block->b_sst_first; p != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001188 if (p->sst_lnum + block->b_syn_sync_linebreaks > buf->b_mod_top)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
1190 n = p->sst_lnum + buf->b_mod_xlines;
1191 if (n <= buf->b_mod_bot)
1192 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001193 // this state is inside the changed area, remove it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 np = p->sst_next;
1195 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001196 block->b_sst_first = np;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 else
1198 prev->sst_next = np;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001199 syn_stack_free_entry(block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 p = np;
1201 continue;
1202 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001203 // This state is below the changed area. Remember the line
1204 // that needs to be parsed before this entry can be made valid
1205 // again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
1207 {
1208 if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
1209 p->sst_change_lnum += buf->b_mod_xlines;
1210 else
1211 p->sst_change_lnum = buf->b_mod_top;
1212 }
1213 if (p->sst_change_lnum == 0
1214 || p->sst_change_lnum < buf->b_mod_bot)
1215 p->sst_change_lnum = buf->b_mod_bot;
1216
1217 p->sst_lnum = n;
1218 }
1219 prev = p;
1220 p = p->sst_next;
1221 }
1222}
1223
1224/*
1225 * Reduce the number of entries in the state stack for syn_buf.
1226 * Returns TRUE if at least one entry was freed.
1227 */
1228 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001229syn_stack_cleanup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
1231 synstate_T *p, *prev;
1232 disptick_T tick;
1233 int above;
1234 int dist;
1235 int retval = FALSE;
1236
Bram Moolenaar95892c22018-09-28 22:26:54 +02001237 if (syn_block->b_sst_first == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 return retval;
1239
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001240 // Compute normal distance between non-displayed entries.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001241 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001242 dist = 999999;
1243 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02001244 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 /*
Bram Moolenaarf5b63862009-12-16 17:13:44 +00001247 * Go through the list to find the "tick" for the oldest entry that can
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 * be removed. Set "above" when the "tick" for the oldest entry is above
1249 * "b_sst_lasttick" (the display tick wraps around).
1250 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 tick = syn_block->b_sst_lasttick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 above = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1255 {
1256 if (prev->sst_lnum + dist > p->sst_lnum)
1257 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001258 if (p->sst_tick > syn_block->b_sst_lasttick)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
1260 if (!above || p->sst_tick < tick)
1261 tick = p->sst_tick;
1262 above = TRUE;
1263 }
1264 else if (!above && p->sst_tick < tick)
1265 tick = p->sst_tick;
1266 }
1267 }
1268
1269 /*
1270 * Go through the list to make the entries for the oldest tick at an
1271 * interval of several lines.
1272 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001273 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1275 {
1276 if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
1277 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001278 // Move this entry from used list to free list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 prev->sst_next = p->sst_next;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001280 syn_stack_free_entry(syn_block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 p = prev;
1282 retval = TRUE;
1283 }
1284 }
1285 return retval;
1286}
1287
1288/*
1289 * Free the allocated memory for a syn_state item.
1290 * Move the entry into the free list.
1291 */
1292 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001293syn_stack_free_entry(synblock_T *block, synstate_T *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 clear_syn_state(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001296 p->sst_next = block->b_sst_firstfree;
1297 block->b_sst_firstfree = p;
1298 ++block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299}
1300
1301/*
1302 * Find an entry in the list of state stacks at or before "lnum".
1303 * Returns NULL when there is no entry or the first entry is after "lnum".
1304 */
1305 static synstate_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001306syn_stack_find_entry(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 synstate_T *p, *prev;
1309
1310 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001311 for (p = syn_block->b_sst_first; p != NULL; prev = p, p = p->sst_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 if (p->sst_lnum == lnum)
1314 return p;
1315 if (p->sst_lnum > lnum)
1316 break;
1317 }
1318 return prev;
1319}
1320
1321/*
1322 * Try saving the current state in b_sst_array[].
1323 * The current state must be valid for the start of the current_lnum line!
1324 */
1325 static synstate_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001326store_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 int i;
1329 synstate_T *p;
1330 bufstate_T *bp;
1331 stateitem_T *cur_si;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001332 synstate_T *sp = syn_stack_find_entry(current_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334 /*
1335 * If the current state contains a start or end pattern that continues
1336 * from the previous line, we can't use it. Don't store it then.
1337 */
1338 for (i = current_state.ga_len - 1; i >= 0; --i)
1339 {
1340 cur_si = &CUR_STATE(i);
1341 if (cur_si->si_h_startpos.lnum >= current_lnum
1342 || cur_si->si_m_endpos.lnum >= current_lnum
1343 || cur_si->si_h_endpos.lnum >= current_lnum
1344 || (cur_si->si_end_idx
1345 && cur_si->si_eoe_pos.lnum >= current_lnum))
1346 break;
1347 }
1348 if (i >= 0)
1349 {
1350 if (sp != NULL)
1351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001352 // find "sp" in the list and remove it
Bram Moolenaar860cae12010-06-05 23:22:07 +02001353 if (syn_block->b_sst_first == sp)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001354 // it's the first entry
Bram Moolenaar860cae12010-06-05 23:22:07 +02001355 syn_block->b_sst_first = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 else
1357 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001358 // find the entry just before this one to adjust sst_next
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001359 FOR_ALL_SYNSTATES(syn_block, p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (p->sst_next == sp)
1361 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001362 if (p != NULL) // just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001363 p->sst_next = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001365 syn_stack_free_entry(syn_block, sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 sp = NULL;
1367 }
1368 }
1369 else if (sp == NULL || sp->sst_lnum != current_lnum)
1370 {
1371 /*
1372 * Add a new entry
1373 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001374 // If no free items, cleanup the array first.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001375 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
1377 (void)syn_stack_cleanup();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001378 // "sp" may have been moved to the freelist now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 sp = syn_stack_find_entry(current_lnum);
1380 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001381 // Still no free items? Must be a strange problem...
Bram Moolenaar860cae12010-06-05 23:22:07 +02001382 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 sp = NULL;
1384 else
1385 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001386 // Take the first item from the free list and put it in the used
1387 // list, after *sp
Bram Moolenaar860cae12010-06-05 23:22:07 +02001388 p = syn_block->b_sst_firstfree;
1389 syn_block->b_sst_firstfree = p->sst_next;
1390 --syn_block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (sp == NULL)
1392 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001393 // Insert in front of the list
Bram Moolenaar860cae12010-06-05 23:22:07 +02001394 p->sst_next = syn_block->b_sst_first;
1395 syn_block->b_sst_first = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 else
1398 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001399 // insert in list after *sp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 p->sst_next = sp->sst_next;
1401 sp->sst_next = p;
1402 }
1403 sp = p;
1404 sp->sst_stacksize = 0;
1405 sp->sst_lnum = current_lnum;
1406 }
1407 }
1408 if (sp != NULL)
1409 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001410 // When overwriting an existing state stack, clear it first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 clear_syn_state(sp);
1412 sp->sst_stacksize = current_state.ga_len;
1413 if (current_state.ga_len > SST_FIX_STATES)
1414 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001415 // Need to clear it, might be something remaining from when the
1416 // length was less than SST_FIX_STATES.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 ga_init2(&sp->sst_union.sst_ga, (int)sizeof(bufstate_T), 1);
1418 if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
1419 sp->sst_stacksize = 0;
1420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 sp->sst_union.sst_ga.ga_len = current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1423 }
1424 else
1425 bp = sp->sst_union.sst_stack;
1426 for (i = 0; i < sp->sst_stacksize; ++i)
1427 {
1428 bp[i].bs_idx = CUR_STATE(i).si_idx;
1429 bp[i].bs_flags = CUR_STATE(i).si_flags;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001430#ifdef FEAT_CONCEAL
1431 bp[i].bs_seqnr = CUR_STATE(i).si_seqnr;
1432 bp[i].bs_cchar = CUR_STATE(i).si_cchar;
1433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
1435 }
1436 sp->sst_next_flags = current_next_flags;
1437 sp->sst_next_list = current_next_list;
1438 sp->sst_tick = display_tick;
1439 sp->sst_change_lnum = 0;
1440 }
1441 current_state_stored = TRUE;
1442 return sp;
1443}
1444
1445/*
1446 * Copy a state stack from "from" in b_sst_array[] to current_state;
1447 */
1448 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001449load_current_state(synstate_T *from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 int i;
1452 bufstate_T *bp;
1453
1454 clear_current_state();
1455 validate_current_state();
1456 keepend_level = -1;
1457 if (from->sst_stacksize
1458 && ga_grow(&current_state, from->sst_stacksize) != FAIL)
1459 {
1460 if (from->sst_stacksize > SST_FIX_STATES)
1461 bp = SYN_STATE_P(&(from->sst_union.sst_ga));
1462 else
1463 bp = from->sst_union.sst_stack;
1464 for (i = 0; i < from->sst_stacksize; ++i)
1465 {
1466 CUR_STATE(i).si_idx = bp[i].bs_idx;
1467 CUR_STATE(i).si_flags = bp[i].bs_flags;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001468#ifdef FEAT_CONCEAL
1469 CUR_STATE(i).si_seqnr = bp[i].bs_seqnr;
1470 CUR_STATE(i).si_cchar = bp[i].bs_cchar;
1471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
1473 if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
1474 keepend_level = i;
1475 CUR_STATE(i).si_ends = FALSE;
1476 CUR_STATE(i).si_m_lnum = 0;
1477 if (CUR_STATE(i).si_idx >= 0)
1478 CUR_STATE(i).si_next_list =
Bram Moolenaar860cae12010-06-05 23:22:07 +02001479 (SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 else
1481 CUR_STATE(i).si_next_list = NULL;
1482 update_si_attr(i);
1483 }
1484 current_state.ga_len = from->sst_stacksize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
1486 current_next_list = from->sst_next_list;
1487 current_next_flags = from->sst_next_flags;
1488 current_lnum = from->sst_lnum;
1489}
1490
1491/*
1492 * Compare saved state stack "*sp" with the current state.
1493 * Return TRUE when they are equal.
1494 */
1495 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001496syn_stack_equal(synstate_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 int i, j;
1499 bufstate_T *bp;
1500 reg_extmatch_T *six, *bsx;
1501
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001502 // First a quick check if the stacks have the same size end nextlist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (sp->sst_stacksize == current_state.ga_len
1504 && sp->sst_next_list == current_next_list)
1505 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001506 // Need to compare all states on both stacks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (sp->sst_stacksize > SST_FIX_STATES)
1508 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1509 else
1510 bp = sp->sst_union.sst_stack;
1511
1512 for (i = current_state.ga_len; --i >= 0; )
1513 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001514 // If the item has another index the state is different.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if (bp[i].bs_idx != CUR_STATE(i).si_idx)
1516 break;
1517 if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
1518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001519 // When the extmatch pointers are different, the strings in
1520 // them can still be the same. Check if the extmatch
1521 // references are equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 bsx = bp[i].bs_extmatch;
1523 six = CUR_STATE(i).si_extmatch;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001524 // If one of the extmatch pointers is NULL the states are
1525 // different.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (bsx == NULL || six == NULL)
1527 break;
1528 for (j = 0; j < NSUBEXP; ++j)
1529 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001530 // Check each referenced match string. They must all be
1531 // equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if (bsx->matches[j] != six->matches[j])
1533 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001534 // If the pointer is different it can still be the
1535 // same text. Compare the strings, ignore case when
1536 // the start item has the sp_ic flag set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (bsx->matches[j] == NULL
1538 || six->matches[j] == NULL)
1539 break;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001540 if ((SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 ? MB_STRICMP(bsx->matches[j],
1542 six->matches[j]) != 0
1543 : STRCMP(bsx->matches[j], six->matches[j]) != 0)
1544 break;
1545 }
1546 }
1547 if (j != NSUBEXP)
1548 break;
1549 }
1550 }
1551 if (i < 0)
1552 return TRUE;
1553 }
1554 return FALSE;
1555}
1556
1557/*
1558 * We stop parsing syntax above line "lnum". If the stored state at or below
1559 * this line depended on a change before it, it now depends on the line below
1560 * the last parsed line.
1561 * The window looks like this:
1562 * line which changed
1563 * displayed line
1564 * displayed line
1565 * lnum -> line below window
1566 */
1567 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001568syntax_end_parsing(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 synstate_T *sp;
1571
1572 sp = syn_stack_find_entry(lnum);
1573 if (sp != NULL && sp->sst_lnum < lnum)
1574 sp = sp->sst_next;
1575
1576 if (sp != NULL && sp->sst_change_lnum != 0)
1577 sp->sst_change_lnum = lnum;
1578}
1579
1580/*
1581 * End of handling of the state stack.
1582 ****************************************/
1583
1584 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001585invalidate_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 clear_current_state();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001588 current_state.ga_itemsize = 0; // mark current_state invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 current_next_list = NULL;
1590 keepend_level = -1;
1591}
1592
1593 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001594validate_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 current_state.ga_itemsize = sizeof(stateitem_T);
1597 current_state.ga_growsize = 3;
1598}
1599
1600/*
1601 * Return TRUE if the syntax at start of lnum changed since last time.
1602 * This will only be called just after get_syntax_attr() for the previous
1603 * line, to check if the next line needs to be redrawn too.
1604 */
1605 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001606syntax_check_changed(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 int retval = TRUE;
1609 synstate_T *sp;
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 /*
1612 * Check the state stack when:
1613 * - lnum is just below the previously syntaxed line.
1614 * - lnum is not before the lines with saved states.
1615 * - lnum is not past the lines with saved states.
1616 * - lnum is at or before the last changed line.
1617 */
1618 if (VALID_STATE(&current_state) && lnum == current_lnum + 1)
1619 {
1620 sp = syn_stack_find_entry(lnum);
1621 if (sp != NULL && sp->sst_lnum == lnum)
1622 {
1623 /*
1624 * finish the previous line (needed when not all of the line was
1625 * drawn)
1626 */
1627 (void)syn_finish_line(FALSE);
1628
1629 /*
1630 * Compare the current state with the previously saved state of
1631 * the line.
1632 */
1633 if (syn_stack_equal(sp))
1634 retval = FALSE;
1635
1636 /*
1637 * Store the current state in b_sst_array[] for later use.
1638 */
1639 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001640 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 }
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 return retval;
1645}
1646
1647/*
1648 * Finish the current line.
1649 * This doesn't return any attributes, it only gets the state at the end of
1650 * the line. It can start anywhere in the line, as long as the current state
1651 * is valid.
1652 */
1653 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001654syn_finish_line(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001655 int syncing) // called for syncing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 stateitem_T *cur_si;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001658 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001660 while (!current_finished)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001662 (void)syn_current_attr(syncing, FALSE, NULL, FALSE);
1663 /*
1664 * When syncing, and found some item, need to check the item.
1665 */
1666 if (syncing && current_state.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 /*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001669 * Check for match with sync item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001671 cur_si = &CUR_STATE(current_state.ga_len - 1);
1672 if (cur_si->si_idx >= 0
1673 && (SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags
1674 & (HL_SYNC_HERE|HL_SYNC_THERE)))
1675 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001677 // syn_current_attr() will have skipped the check for an item
1678 // that ends here, need to do that now. Be careful not to go
1679 // past the NUL.
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001680 prev_current_col = current_col;
1681 if (syn_getcurline()[current_col] != NUL)
1682 ++current_col;
1683 check_state_ends();
1684 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001686 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688 return FALSE;
1689}
1690
1691/*
1692 * Return highlight attributes for next character.
1693 * Must first call syntax_start() once for the line.
1694 * "col" is normally 0 for the first use in a line, and increments by one each
1695 * time. It's allowed to skip characters and to stop before the end of the
1696 * line. But only a "col" after a previously used column is allowed.
Bram Moolenaar217ad922005-03-20 22:37:15 +00001697 * When "can_spell" is not NULL set it to TRUE when spell-checking should be
1698 * done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 */
1700 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001701get_syntax_attr(
1702 colnr_T col,
1703 int *can_spell,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001704 int keep_state) // keep state of char at "col"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 int attr = 0;
1707
Bram Moolenaar349955a2007-08-14 21:07:36 +00001708 if (can_spell != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001709 // Default: Only do spelling when there is no @Spell cluster or when
1710 // ":syn spell toplevel" was used.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001711 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
1712 ? (syn_block->b_spell_cluster_id == 0)
1713 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar349955a2007-08-14 21:07:36 +00001714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // check for out of memory situation
Bram Moolenaar860cae12010-06-05 23:22:07 +02001716 if (syn_block->b_sst_array == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 return 0;
1718
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001719 // After 'synmaxcol' the attribute is always zero.
Bram Moolenaar84fb85a2005-07-20 22:02:14 +00001720 if (syn_buf->b_p_smc > 0 && col >= (colnr_T)syn_buf->b_p_smc)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001721 {
1722 clear_current_state();
1723#ifdef FEAT_EVAL
1724 current_id = 0;
1725 current_trans_id = 0;
1726#endif
Bram Moolenaar7510fe72010-07-25 12:46:44 +02001727#ifdef FEAT_CONCEAL
1728 current_flags = 0;
Bram Moolenaarcc0750d2017-06-24 22:29:24 +02001729 current_seqnr = 0;
Bram Moolenaar7510fe72010-07-25 12:46:44 +02001730#endif
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001731 return 0;
1732 }
1733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001734 // Make sure current_state is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (INVALID_STATE(&current_state))
1736 validate_current_state();
1737
1738 /*
1739 * Skip from the current column to "col", get the attributes for "col".
1740 */
1741 while (current_col <= col)
1742 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001743 attr = syn_current_attr(FALSE, TRUE, can_spell,
1744 current_col == col ? keep_state : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 ++current_col;
1746 }
1747
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 return attr;
1749}
1750
1751/*
1752 * Get syntax attributes for current_lnum, current_col.
1753 */
1754 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001755syn_current_attr(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001756 int syncing, // When 1: called for syncing
1757 int displaying, // result will be displayed
1758 int *can_spell, // return: do spell checking
1759 int keep_state) // keep syntax stack afterwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int syn_id;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001762 lpos_T endpos; // was: char_u *endp;
1763 lpos_T hl_startpos; // was: int hl_startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 lpos_T hl_endpos;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001765 lpos_T eos_pos; // end-of-start match (start region)
1766 lpos_T eoe_pos; // end-of-end pattern
1767 int end_idx; // group ID for end pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 int idx;
1769 synpat_T *spp;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001770 stateitem_T *cur_si, *sip = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 int startcol;
1772 int endcol;
1773 long flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001774 int cchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 short *next_list;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001776 int found_match; // found usable match
1777 static int try_next_column = FALSE; // must try in next col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int do_keywords;
1779 regmmatch_T regmatch;
1780 lpos_T pos;
1781 int lc_col;
1782 reg_extmatch_T *cur_extmatch = NULL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001783 char_u buf_chartab[32]; // chartab array for syn iskyeyword
1784 char_u *line; // current line. NOTE: becomes invalid after
1785 // looking for a pattern match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001787 // variables for zero-width matches that have a "nextgroup" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 int keep_next_list;
1789 int zero_width_next_list = FALSE;
1790 garray_T zero_width_next_ga;
1791
1792 /*
1793 * No character, no attributes! Past end of line?
1794 * Do try matching with an empty line (could be the start of a region).
1795 */
1796 line = syn_getcurline();
1797 if (line[current_col] == NUL && current_col != 0)
1798 {
1799 /*
1800 * If we found a match after the last column, use it.
1801 */
1802 if (next_match_idx >= 0 && next_match_col >= (int)current_col
1803 && next_match_col != MAXCOL)
1804 (void)push_next_match(NULL);
1805
1806 current_finished = TRUE;
1807 current_state_stored = FALSE;
1808 return 0;
1809 }
1810
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001811 // if the current or next character is NUL, we will finish the line now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 if (line[current_col] == NUL || line[current_col + 1] == NUL)
1813 {
1814 current_finished = TRUE;
1815 current_state_stored = FALSE;
1816 }
1817
1818 /*
1819 * When in the previous column there was a match but it could not be used
1820 * (empty match or already matched in this column) need to try again in
1821 * the next column.
1822 */
1823 if (try_next_column)
1824 {
1825 next_match_idx = -1;
1826 try_next_column = FALSE;
1827 }
1828
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001829 // Only check for keywords when not syncing and there are some.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 do_keywords = !syncing
Bram Moolenaar860cae12010-06-05 23:22:07 +02001831 && (syn_block->b_keywtab.ht_used > 0
1832 || syn_block->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001834 // Init the list of zero-width matches with a nextlist. This is used to
1835 // avoid matching the same item in the same position twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 ga_init2(&zero_width_next_ga, (int)sizeof(int), 10);
1837
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001838 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001839 save_chartab(buf_chartab);
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 /*
1842 * Repeat matching keywords and patterns, to find contained items at the
1843 * same column. This stops when there are no extra matches at the current
1844 * column.
1845 */
1846 do
1847 {
1848 found_match = FALSE;
1849 keep_next_list = FALSE;
1850 syn_id = 0;
1851
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 /*
1854 * 1. Check for a current state.
1855 * Only when there is no current state, or if the current state may
1856 * contain other things, we need to check for keywords and patterns.
1857 * Always need to check for contained items if some item has the
1858 * "containedin" argument (takes extra time!).
1859 */
1860 if (current_state.ga_len)
1861 cur_si = &CUR_STATE(current_state.ga_len - 1);
1862 else
1863 cur_si = NULL;
1864
Bram Moolenaar860cae12010-06-05 23:22:07 +02001865 if (syn_block->b_syn_containedin || cur_si == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 || cur_si->si_cont_list != NULL)
1867 {
1868 /*
1869 * 2. Check for keywords, if on a keyword char after a non-keyword
1870 * char. Don't do this when syncing.
1871 */
1872 if (do_keywords)
1873 {
1874 line = syn_getcurline();
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01001875 if (vim_iswordp_buf(line + current_col, syn_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 && (current_col == 0
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01001877 || !vim_iswordp_buf(line + current_col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 - (has_mbyte
1879 ? (*mb_head_off)(line, line + current_col - 1)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001880 : 0) , syn_buf)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 syn_id = check_keyword_id(line, (int)current_col,
Bram Moolenaar860cae12010-06-05 23:22:07 +02001883 &endcol, &flags, &next_list, cur_si,
1884 &cchar);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001885 if (syn_id != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 if (push_current_state(KEYWORD_IDX) == OK)
1888 {
1889 cur_si = &CUR_STATE(current_state.ga_len - 1);
1890 cur_si->si_m_startcol = current_col;
1891 cur_si->si_h_startpos.lnum = current_lnum;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001892 cur_si->si_h_startpos.col = 0; // starts right away
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 cur_si->si_m_endpos.lnum = current_lnum;
1894 cur_si->si_m_endpos.col = endcol;
1895 cur_si->si_h_endpos.lnum = current_lnum;
1896 cur_si->si_h_endpos.col = endcol;
1897 cur_si->si_ends = TRUE;
1898 cur_si->si_end_idx = 0;
1899 cur_si->si_flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001900#ifdef FEAT_CONCEAL
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02001901 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001902 cur_si->si_cchar = cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001903 if (current_state.ga_len > 1)
1904 cur_si->si_flags |=
1905 CUR_STATE(current_state.ga_len - 2).si_flags
1906 & HL_CONCEAL;
1907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 cur_si->si_id = syn_id;
1909 cur_si->si_trans_id = syn_id;
1910 if (flags & HL_TRANSP)
1911 {
1912 if (current_state.ga_len < 2)
1913 {
1914 cur_si->si_attr = 0;
1915 cur_si->si_trans_id = 0;
1916 }
1917 else
1918 {
1919 cur_si->si_attr = CUR_STATE(
1920 current_state.ga_len - 2).si_attr;
1921 cur_si->si_trans_id = CUR_STATE(
1922 current_state.ga_len - 2).si_trans_id;
1923 }
1924 }
1925 else
1926 cur_si->si_attr = syn_id2attr(syn_id);
1927 cur_si->si_cont_list = NULL;
1928 cur_si->si_next_list = next_list;
1929 check_keepend();
1930 }
1931 else
1932 vim_free(next_list);
1933 }
1934 }
1935 }
1936
1937 /*
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001938 * 3. Check for patterns (only if no keyword found).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001940 if (syn_id == 0 && syn_block->b_syn_patterns.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 /*
1943 * If we didn't check for a match yet, or we are past it, check
1944 * for any match with a pattern.
1945 */
1946 if (next_match_idx < 0 || next_match_col < (int)current_col)
1947 {
1948 /*
1949 * Check all relevant patterns for a match at this
1950 * position. This is complicated, because matching with a
1951 * pattern takes quite a bit of time, thus we want to
1952 * avoid doing it when it's not needed.
1953 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001954 next_match_idx = 0; // no match in this line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 next_match_col = MAXCOL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001956 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001958 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 if ( spp->sp_syncing == syncing
1960 && (displaying || !(spp->sp_flags & HL_DISPLAY))
1961 && (spp->sp_type == SPTYPE_MATCH
1962 || spp->sp_type == SPTYPE_START)
1963 && (current_next_list != NULL
1964 ? in_id_list(NULL, current_next_list,
1965 &spp->sp_syn, 0)
1966 : (cur_si == NULL
1967 ? !(spp->sp_flags & HL_CONTAINED)
1968 : in_id_list(cur_si,
1969 cur_si->si_cont_list, &spp->sp_syn,
1970 spp->sp_flags & HL_CONTAINED))))
1971 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001972 int r;
1973
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001974 // If we already tried matching in this line, and
1975 // there isn't a match before next_match_col, skip
1976 // this item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (spp->sp_line_id == current_line_id
1978 && spp->sp_startcol >= next_match_col)
1979 continue;
1980 spp->sp_line_id = current_line_id;
1981
1982 lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
1983 if (lc_col < 0)
1984 lc_col = 0;
1985
1986 regmatch.rmm_ic = spp->sp_ic;
1987 regmatch.regprog = spp->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001988 r = syn_regexec(&regmatch,
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02001989 current_lnum,
1990 (colnr_T)lc_col,
Bram Moolenaard23a8232018-02-10 18:45:26 +01001991 IF_SYN_TIME(&spp->sp_time));
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001992 spp->sp_prog = regmatch.regprog;
1993 if (!r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001995 // no match in this line, try another one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 spp->sp_startcol = MAXCOL;
1997 continue;
1998 }
1999
2000 /*
2001 * Compute the first column of the match.
2002 */
2003 syn_add_start_off(&pos, &regmatch,
2004 spp, SPO_MS_OFF, -1);
2005 if (pos.lnum > current_lnum)
2006 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002007 // must have used end of match in a next line,
2008 // we can't handle that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 spp->sp_startcol = MAXCOL;
2010 continue;
2011 }
2012 startcol = pos.col;
2013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002014 // remember the next column where this pattern
2015 // matches in the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 spp->sp_startcol = startcol;
2017
2018 /*
2019 * If a previously found match starts at a lower
2020 * column number, don't use this one.
2021 */
2022 if (startcol >= next_match_col)
2023 continue;
2024
2025 /*
2026 * If we matched this pattern at this position
2027 * before, skip it. Must retry in the next
2028 * column, because it may match from there.
2029 */
2030 if (did_match_already(idx, &zero_width_next_ga))
2031 {
2032 try_next_column = TRUE;
2033 continue;
2034 }
2035
2036 endpos.lnum = regmatch.endpos[0].lnum;
2037 endpos.col = regmatch.endpos[0].col;
2038
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002039 // Compute the highlight start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 syn_add_start_off(&hl_startpos, &regmatch,
2041 spp, SPO_HS_OFF, -1);
2042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002043 // Compute the region start.
2044 // Default is to use the end of the match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 syn_add_end_off(&eos_pos, &regmatch,
2046 spp, SPO_RS_OFF, 0);
2047
2048 /*
2049 * Grab the external submatches before they get
2050 * overwritten. Reference count doesn't change.
2051 */
2052 unref_extmatch(cur_extmatch);
2053 cur_extmatch = re_extmatch_out;
2054 re_extmatch_out = NULL;
2055
2056 flags = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002057 eoe_pos.lnum = 0; // avoid warning
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 eoe_pos.col = 0;
2059 end_idx = 0;
2060 hl_endpos.lnum = 0;
2061
2062 /*
2063 * For a "oneline" the end must be found in the
2064 * same line too. Search for it after the end of
2065 * the match with the start pattern. Set the
2066 * resulting end positions at the same time.
2067 */
2068 if (spp->sp_type == SPTYPE_START
2069 && (spp->sp_flags & HL_ONELINE))
2070 {
2071 lpos_T startpos;
2072
2073 startpos = endpos;
2074 find_endpos(idx, &startpos, &endpos, &hl_endpos,
2075 &flags, &eoe_pos, &end_idx, cur_extmatch);
2076 if (endpos.lnum == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002077 continue; // not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079
2080 /*
2081 * For a "match" the size must be > 0 after the
2082 * end offset needs has been added. Except when
2083 * syncing.
2084 */
2085 else if (spp->sp_type == SPTYPE_MATCH)
2086 {
2087 syn_add_end_off(&hl_endpos, &regmatch, spp,
2088 SPO_HE_OFF, 0);
2089 syn_add_end_off(&endpos, &regmatch, spp,
2090 SPO_ME_OFF, 0);
2091 if (endpos.lnum == current_lnum
2092 && (int)endpos.col + syncing < startcol)
2093 {
2094 /*
2095 * If an empty string is matched, may need
2096 * to try matching again at next column.
2097 */
2098 if (regmatch.startpos[0].col
2099 == regmatch.endpos[0].col)
2100 try_next_column = TRUE;
2101 continue;
2102 }
2103 }
2104
2105 /*
2106 * keep the best match so far in next_match_*
2107 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002108 // Highlighting must start after startpos and end
2109 // before endpos.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (hl_startpos.lnum == current_lnum
2111 && (int)hl_startpos.col < startcol)
2112 hl_startpos.col = startcol;
2113 limit_pos_zero(&hl_endpos, &endpos);
2114
2115 next_match_idx = idx;
2116 next_match_col = startcol;
2117 next_match_m_endpos = endpos;
2118 next_match_h_endpos = hl_endpos;
2119 next_match_h_startpos = hl_startpos;
2120 next_match_flags = flags;
2121 next_match_eos_pos = eos_pos;
2122 next_match_eoe_pos = eoe_pos;
2123 next_match_end_idx = end_idx;
2124 unref_extmatch(next_match_extmatch);
2125 next_match_extmatch = cur_extmatch;
2126 cur_extmatch = NULL;
2127 }
2128 }
2129 }
2130
2131 /*
2132 * If we found a match at the current column, use it.
2133 */
2134 if (next_match_idx >= 0 && next_match_col == (int)current_col)
2135 {
2136 synpat_T *lspp;
2137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002138 // When a zero-width item matched which has a nextgroup,
2139 // don't push the item but set nextgroup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002140 lspp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (next_match_m_endpos.lnum == current_lnum
2142 && next_match_m_endpos.col == current_col
2143 && lspp->sp_next_list != NULL)
2144 {
2145 current_next_list = lspp->sp_next_list;
2146 current_next_flags = lspp->sp_flags;
2147 keep_next_list = TRUE;
2148 zero_width_next_list = TRUE;
2149
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002150 // Add the index to a list, so that we can check
2151 // later that we don't match it again (and cause an
2152 // endless loop).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (ga_grow(&zero_width_next_ga, 1) == OK)
2154 {
2155 ((int *)(zero_width_next_ga.ga_data))
2156 [zero_width_next_ga.ga_len++] = next_match_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 next_match_idx = -1;
2159 }
2160 else
2161 cur_si = push_next_match(cur_si);
2162 found_match = TRUE;
2163 }
2164 }
2165 }
2166
2167 /*
2168 * Handle searching for nextgroup match.
2169 */
2170 if (current_next_list != NULL && !keep_next_list)
2171 {
2172 /*
2173 * If a nextgroup was not found, continue looking for one if:
2174 * - this is an empty line and the "skipempty" option was given
2175 * - we are on white space and the "skipwhite" option was given
2176 */
2177 if (!found_match)
2178 {
2179 line = syn_getcurline();
2180 if (((current_next_flags & HL_SKIPWHITE)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002181 && VIM_ISWHITE(line[current_col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 || ((current_next_flags & HL_SKIPEMPTY)
2183 && *line == NUL))
2184 break;
2185 }
2186
2187 /*
2188 * If a nextgroup was found: Use it, and continue looking for
2189 * contained matches.
2190 * If a nextgroup was not found: Continue looking for a normal
2191 * match.
2192 * When did set current_next_list for a zero-width item and no
2193 * match was found don't loop (would get stuck).
2194 */
2195 current_next_list = NULL;
2196 next_match_idx = -1;
2197 if (!zero_width_next_list)
2198 found_match = TRUE;
2199 }
2200
2201 } while (found_match);
2202
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002203 restore_chartab(buf_chartab);
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /*
2206 * Use attributes from the current state, if within its highlighting.
2207 * If not, use attributes from the current-but-one state, etc.
2208 */
2209 current_attr = 0;
2210#ifdef FEAT_EVAL
2211 current_id = 0;
2212 current_trans_id = 0;
2213#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002214#ifdef FEAT_CONCEAL
2215 current_flags = 0;
Bram Moolenaarcc0750d2017-06-24 22:29:24 +02002216 current_seqnr = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (cur_si != NULL)
2219 {
Bram Moolenaar217ad922005-03-20 22:37:15 +00002220#ifndef FEAT_EVAL
2221 int current_trans_id = 0;
2222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 for (idx = current_state.ga_len - 1; idx >= 0; --idx)
2224 {
2225 sip = &CUR_STATE(idx);
2226 if ((current_lnum > sip->si_h_startpos.lnum
2227 || (current_lnum == sip->si_h_startpos.lnum
2228 && current_col >= sip->si_h_startpos.col))
2229 && (sip->si_h_endpos.lnum == 0
2230 || current_lnum < sip->si_h_endpos.lnum
2231 || (current_lnum == sip->si_h_endpos.lnum
2232 && current_col < sip->si_h_endpos.col)))
2233 {
2234 current_attr = sip->si_attr;
2235#ifdef FEAT_EVAL
2236 current_id = sip->si_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002238 current_trans_id = sip->si_trans_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002239#ifdef FEAT_CONCEAL
2240 current_flags = sip->si_flags;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002241 current_seqnr = sip->si_seqnr;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02002242 current_sub_char = sip->si_cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 break;
2245 }
2246 }
2247
Bram Moolenaar217ad922005-03-20 22:37:15 +00002248 if (can_spell != NULL)
2249 {
2250 struct sp_syn sps;
2251
2252 /*
2253 * set "can_spell" to TRUE if spell checking is supposed to be
2254 * done in the current item.
2255 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002256 if (syn_block->b_spell_cluster_id == 0)
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002257 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002258 // There is no @Spell cluster: Do spelling for items without
2259 // @NoSpell cluster.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002260 if (syn_block->b_nospell_cluster_id == 0
2261 || current_trans_id == 0)
2262 *can_spell = (syn_block->b_syn_spell != SYNSPL_NOTOP);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002263 else
2264 {
2265 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002266 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002267 sps.cont_in_list = NULL;
2268 *can_spell = !in_id_list(sip, sip->si_cont_list, &sps, 0);
2269 }
2270 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002271 else
2272 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002273 // The @Spell cluster is defined: Do spelling in items with
2274 // the @Spell cluster. But not when @NoSpell is also there.
2275 // At the toplevel only spell check when ":syn spell toplevel"
2276 // was used.
Bram Moolenaar3638c682005-06-08 22:05:14 +00002277 if (current_trans_id == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002278 *can_spell = (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar3638c682005-06-08 22:05:14 +00002279 else
2280 {
2281 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002282 sps.id = syn_block->b_spell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002283 sps.cont_in_list = NULL;
2284 *can_spell = in_id_list(sip, sip->si_cont_list, &sps, 0);
2285
Bram Moolenaar860cae12010-06-05 23:22:07 +02002286 if (syn_block->b_nospell_cluster_id != 0)
Bram Moolenaar3638c682005-06-08 22:05:14 +00002287 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002288 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002289 if (in_id_list(sip, sip->si_cont_list, &sps, 0))
2290 *can_spell = FALSE;
2291 }
2292 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002293 }
2294 }
2295
2296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 /*
2298 * Check for end of current state (and the states before it) at the
2299 * next column. Don't do this for syncing, because we would miss a
2300 * single character match.
2301 * First check if the current state ends at the current column. It
2302 * may be for an empty match and a containing item might end in the
2303 * current column.
2304 */
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002305 if (!syncing && !keep_state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +00002308 if (current_state.ga_len > 0
2309 && syn_getcurline()[current_col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 ++current_col;
2312 check_state_ends();
2313 --current_col;
2314 }
2315 }
2316 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002317 else if (can_spell != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002318 // Default: Only do spelling when there is no @Spell cluster or when
2319 // ":syn spell toplevel" was used.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002320 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
2321 ? (syn_block->b_spell_cluster_id == 0)
2322 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002324 // nextgroup ends at end of line, unless "skipnl" or "skipempty" present
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (current_next_list != NULL
Bram Moolenaar069dafc2018-03-03 20:02:19 +01002326 && (line = syn_getcurline())[current_col] != NUL
2327 && line[current_col + 1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 && !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
2329 current_next_list = NULL;
2330
2331 if (zero_width_next_ga.ga_len > 0)
2332 ga_clear(&zero_width_next_ga);
2333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002334 // No longer need external matches. But keep next_match_extmatch.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 unref_extmatch(re_extmatch_out);
2336 re_extmatch_out = NULL;
2337 unref_extmatch(cur_extmatch);
2338
2339 return current_attr;
2340}
2341
2342
2343/*
2344 * Check if we already matched pattern "idx" at the current column.
2345 */
2346 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002347did_match_already(int idx, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 int i;
2350
2351 for (i = current_state.ga_len; --i >= 0; )
2352 if (CUR_STATE(i).si_m_startcol == (int)current_col
2353 && CUR_STATE(i).si_m_lnum == (int)current_lnum
2354 && CUR_STATE(i).si_idx == idx)
2355 return TRUE;
2356
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002357 // Zero-width matches with a nextgroup argument are not put on the syntax
2358 // stack, and can only be matched once anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 for (i = gap->ga_len; --i >= 0; )
2360 if (((int *)(gap->ga_data))[i] == idx)
2361 return TRUE;
2362
2363 return FALSE;
2364}
2365
2366/*
2367 * Push the next match onto the stack.
2368 */
2369 static stateitem_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002370push_next_match(stateitem_T *cur_si)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 synpat_T *spp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002373#ifdef FEAT_CONCEAL
2374 int save_flags;
2375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
Bram Moolenaar860cae12010-06-05 23:22:07 +02002377 spp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 /*
2380 * Push the item in current_state stack;
2381 */
2382 if (push_current_state(next_match_idx) == OK)
2383 {
2384 /*
2385 * If it's a start-skip-end type that crosses lines, figure out how
2386 * much it continues in this line. Otherwise just fill in the length.
2387 */
2388 cur_si = &CUR_STATE(current_state.ga_len - 1);
2389 cur_si->si_h_startpos = next_match_h_startpos;
2390 cur_si->si_m_startcol = current_col;
2391 cur_si->si_m_lnum = current_lnum;
2392 cur_si->si_flags = spp->sp_flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002393#ifdef FEAT_CONCEAL
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002394 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02002395 cur_si->si_cchar = spp->sp_cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002396 if (current_state.ga_len > 1)
2397 cur_si->si_flags |=
2398 CUR_STATE(current_state.ga_len - 2).si_flags & HL_CONCEAL;
2399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 cur_si->si_next_list = spp->sp_next_list;
2401 cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
2402 if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
2403 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002404 // Try to find the end pattern in the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
2406 check_keepend();
2407 }
2408 else
2409 {
2410 cur_si->si_m_endpos = next_match_m_endpos;
2411 cur_si->si_h_endpos = next_match_h_endpos;
2412 cur_si->si_ends = TRUE;
2413 cur_si->si_flags |= next_match_flags;
2414 cur_si->si_eoe_pos = next_match_eoe_pos;
2415 cur_si->si_end_idx = next_match_end_idx;
2416 }
2417 if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
2418 keepend_level = current_state.ga_len - 1;
2419 check_keepend();
2420 update_si_attr(current_state.ga_len - 1);
2421
Bram Moolenaar860cae12010-06-05 23:22:07 +02002422#ifdef FEAT_CONCEAL
2423 save_flags = cur_si->si_flags & (HL_CONCEAL | HL_CONCEALENDS);
2424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 /*
2426 * If the start pattern has another highlight group, push another item
2427 * on the stack for the start pattern.
2428 */
2429 if ( spp->sp_type == SPTYPE_START
2430 && spp->sp_syn_match_id != 0
2431 && push_current_state(next_match_idx) == OK)
2432 {
2433 cur_si = &CUR_STATE(current_state.ga_len - 1);
2434 cur_si->si_h_startpos = next_match_h_startpos;
2435 cur_si->si_m_startcol = current_col;
2436 cur_si->si_m_lnum = current_lnum;
2437 cur_si->si_m_endpos = next_match_eos_pos;
2438 cur_si->si_h_endpos = next_match_eos_pos;
2439 cur_si->si_ends = TRUE;
2440 cur_si->si_end_idx = 0;
2441 cur_si->si_flags = HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002442#ifdef FEAT_CONCEAL
Bram Moolenaar3b953892010-07-27 20:47:25 +02002443 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002444 cur_si->si_flags |= save_flags;
2445 if (cur_si->si_flags & HL_CONCEALENDS)
2446 cur_si->si_flags |= HL_CONCEAL;
2447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 cur_si->si_next_list = NULL;
2449 check_keepend();
2450 update_si_attr(current_state.ga_len - 1);
2451 }
2452 }
2453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002454 next_match_idx = -1; // try other match next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 return cur_si;
2457}
2458
2459/*
2460 * Check for end of current state (and the states before it).
2461 */
2462 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002463check_state_ends(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 stateitem_T *cur_si;
Bram Moolenaar6fa46362011-05-25 17:56:27 +02002466 int had_extend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468 cur_si = &CUR_STATE(current_state.ga_len - 1);
2469 for (;;)
2470 {
2471 if (cur_si->si_ends
2472 && (cur_si->si_m_endpos.lnum < current_lnum
2473 || (cur_si->si_m_endpos.lnum == current_lnum
2474 && cur_si->si_m_endpos.col <= current_col)))
2475 {
2476 /*
2477 * If there is an end pattern group ID, highlight the end pattern
2478 * now. No need to pop the current item from the stack.
2479 * Only do this if the end pattern continues beyond the current
2480 * position.
2481 */
2482 if (cur_si->si_end_idx
2483 && (cur_si->si_eoe_pos.lnum > current_lnum
2484 || (cur_si->si_eoe_pos.lnum == current_lnum
2485 && cur_si->si_eoe_pos.col > current_col)))
2486 {
2487 cur_si->si_idx = cur_si->si_end_idx;
2488 cur_si->si_end_idx = 0;
2489 cur_si->si_m_endpos = cur_si->si_eoe_pos;
2490 cur_si->si_h_endpos = cur_si->si_eoe_pos;
2491 cur_si->si_flags |= HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002492#ifdef FEAT_CONCEAL
Bram Moolenaar3b953892010-07-27 20:47:25 +02002493 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002494 if (cur_si->si_flags & HL_CONCEALENDS)
2495 cur_si->si_flags |= HL_CONCEAL;
2496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 update_si_attr(current_state.ga_len - 1);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002499 // nextgroup= should not match in the end pattern
Bram Moolenaar3a7d8c32011-05-19 12:14:10 +02002500 current_next_list = NULL;
2501
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002502 // what matches next may be different now, clear it
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002503 next_match_idx = 0;
2504 next_match_col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 break;
2506 }
2507 else
2508 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002509 // handle next_list, unless at end of line and no "skipnl" or
2510 // "skipempty"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 current_next_list = cur_si->si_next_list;
2512 current_next_flags = cur_si->si_flags;
2513 if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
2514 && syn_getcurline()[current_col] == NUL)
2515 current_next_list = NULL;
2516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002517 // When the ended item has "extend", another item with
2518 // "keepend" now needs to check for its end.
Bram Moolenaar6fa46362011-05-25 17:56:27 +02002519 had_extend = (cur_si->si_flags & HL_EXTEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521 pop_current_state();
2522
2523 if (current_state.ga_len == 0)
2524 break;
2525
Bram Moolenaar81993f42008-01-11 20:27:45 +00002526 if (had_extend && keepend_level >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
2528 syn_update_ends(FALSE);
2529 if (current_state.ga_len == 0)
2530 break;
2531 }
2532
2533 cur_si = &CUR_STATE(current_state.ga_len - 1);
2534
2535 /*
2536 * Only for a region the search for the end continues after
2537 * the end of the contained item. If the contained match
2538 * included the end-of-line, break here, the region continues.
2539 * Don't do this when:
2540 * - "keepend" is used for the contained item
2541 * - not at the end of the line (could be end="x$"me=e-1).
2542 * - "excludenl" is used (HL_HAS_EOL won't be set)
2543 */
2544 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02002545 && SYN_ITEMS(syn_block)[cur_si->si_idx].sp_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 == SPTYPE_START
2547 && !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
2548 {
2549 update_si_end(cur_si, (int)current_col, TRUE);
2550 check_keepend();
2551 if ((current_next_flags & HL_HAS_EOL)
2552 && keepend_level < 0
2553 && syn_getcurline()[current_col] == NUL)
2554 break;
2555 }
2556 }
2557 }
2558 else
2559 break;
2560 }
2561}
2562
2563/*
2564 * Update an entry in the current_state stack for a match or region. This
2565 * fills in si_attr, si_next_list and si_cont_list.
2566 */
2567 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002568update_si_attr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 stateitem_T *sip = &CUR_STATE(idx);
2571 synpat_T *spp;
2572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002573 // This should not happen...
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002574 if (sip->si_idx < 0)
2575 return;
2576
Bram Moolenaar860cae12010-06-05 23:22:07 +02002577 spp = &(SYN_ITEMS(syn_block)[sip->si_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (sip->si_flags & HL_MATCH)
2579 sip->si_id = spp->sp_syn_match_id;
2580 else
2581 sip->si_id = spp->sp_syn.id;
2582 sip->si_attr = syn_id2attr(sip->si_id);
2583 sip->si_trans_id = sip->si_id;
2584 if (sip->si_flags & HL_MATCH)
2585 sip->si_cont_list = NULL;
2586 else
2587 sip->si_cont_list = spp->sp_cont_list;
2588
2589 /*
2590 * For transparent items, take attr from outer item.
2591 * Also take cont_list, if there is none.
2592 * Don't do this for the matchgroup of a start or end pattern.
2593 */
2594 if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
2595 {
2596 if (idx == 0)
2597 {
2598 sip->si_attr = 0;
2599 sip->si_trans_id = 0;
2600 if (sip->si_cont_list == NULL)
2601 sip->si_cont_list = ID_LIST_ALL;
2602 }
2603 else
2604 {
2605 sip->si_attr = CUR_STATE(idx - 1).si_attr;
2606 sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002607 sip->si_h_startpos = CUR_STATE(idx - 1).si_h_startpos;
2608 sip->si_h_endpos = CUR_STATE(idx - 1).si_h_endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (sip->si_cont_list == NULL)
2610 {
2611 sip->si_flags |= HL_TRANS_CONT;
2612 sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
2613 }
2614 }
2615 }
2616}
2617
2618/*
2619 * Check the current stack for patterns with "keepend" flag.
2620 * Propagate the match-end to contained items, until a "skipend" item is found.
2621 */
2622 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002623check_keepend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 int i;
2626 lpos_T maxpos;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002627 lpos_T maxpos_h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 stateitem_T *sip;
2629
2630 /*
2631 * This check can consume a lot of time; only do it from the level where
2632 * there really is a keepend.
2633 */
2634 if (keepend_level < 0)
2635 return;
2636
2637 /*
2638 * Find the last index of an "extend" item. "keepend" items before that
2639 * won't do anything. If there is no "extend" item "i" will be
2640 * "keepend_level" and all "keepend" items will work normally.
2641 */
2642 for (i = current_state.ga_len - 1; i > keepend_level; --i)
2643 if (CUR_STATE(i).si_flags & HL_EXTEND)
2644 break;
2645
2646 maxpos.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002647 maxpos.col = 0;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002648 maxpos_h.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002649 maxpos_h.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 for ( ; i < current_state.ga_len; ++i)
2651 {
2652 sip = &CUR_STATE(i);
2653 if (maxpos.lnum != 0)
2654 {
2655 limit_pos_zero(&sip->si_m_endpos, &maxpos);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002656 limit_pos_zero(&sip->si_h_endpos, &maxpos_h);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 limit_pos_zero(&sip->si_eoe_pos, &maxpos);
2658 sip->si_ends = TRUE;
2659 }
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002660 if (sip->si_ends && (sip->si_flags & HL_KEEPEND))
2661 {
2662 if (maxpos.lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 || maxpos.lnum > sip->si_m_endpos.lnum
2664 || (maxpos.lnum == sip->si_m_endpos.lnum
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002665 && maxpos.col > sip->si_m_endpos.col))
2666 maxpos = sip->si_m_endpos;
2667 if (maxpos_h.lnum == 0
2668 || maxpos_h.lnum > sip->si_h_endpos.lnum
2669 || (maxpos_h.lnum == sip->si_h_endpos.lnum
2670 && maxpos_h.col > sip->si_h_endpos.col))
2671 maxpos_h = sip->si_h_endpos;
2672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674}
2675
2676/*
2677 * Update an entry in the current_state stack for a start-skip-end pattern.
2678 * This finds the end of the current item, if it's in the current line.
2679 *
2680 * Return the flags for the matched END.
2681 */
2682 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002683update_si_end(
2684 stateitem_T *sip,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002685 int startcol, // where to start searching for the end
2686 int force) // when TRUE overrule a previous end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 lpos_T startpos;
2689 lpos_T endpos;
2690 lpos_T hl_endpos;
2691 lpos_T end_endpos;
2692 int end_idx;
2693
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002694 // return quickly for a keyword
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002695 if (sip->si_idx < 0)
2696 return;
2697
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002698 // Don't update when it's already done. Can be a match of an end pattern
2699 // that started in a previous line. Watch out: can also be a "keepend"
2700 // from a containing item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (!force && sip->si_m_endpos.lnum >= current_lnum)
2702 return;
2703
2704 /*
2705 * We need to find the end of the region. It may continue in the next
2706 * line.
2707 */
2708 end_idx = 0;
2709 startpos.lnum = current_lnum;
2710 startpos.col = startcol;
2711 find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
2712 &(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
2713
2714 if (endpos.lnum == 0)
2715 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002716 // No end pattern matched.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002717 if (SYN_ITEMS(syn_block)[sip->si_idx].sp_flags & HL_ONELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002719 // a "oneline" never continues in the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 sip->si_ends = TRUE;
2721 sip->si_m_endpos.lnum = current_lnum;
2722 sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
2723 }
2724 else
2725 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002726 // continues in the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 sip->si_ends = FALSE;
2728 sip->si_m_endpos.lnum = 0;
2729 }
2730 sip->si_h_endpos = sip->si_m_endpos;
2731 }
2732 else
2733 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002734 // match within this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 sip->si_m_endpos = endpos;
2736 sip->si_h_endpos = hl_endpos;
2737 sip->si_eoe_pos = end_endpos;
2738 sip->si_ends = TRUE;
2739 sip->si_end_idx = end_idx;
2740 }
2741}
2742
2743/*
2744 * Add a new state to the current state stack.
2745 * It is cleared and the index set to "idx".
2746 * Return FAIL if it's not possible (out of memory).
2747 */
2748 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749push_current_state(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 if (ga_grow(&current_state, 1) == FAIL)
2752 return FAIL;
2753 vim_memset(&CUR_STATE(current_state.ga_len), 0, sizeof(stateitem_T));
2754 CUR_STATE(current_state.ga_len).si_idx = idx;
2755 ++current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 return OK;
2757}
2758
2759/*
2760 * Remove a state from the current_state stack.
2761 */
2762 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002763pop_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 if (current_state.ga_len)
2766 {
2767 unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
2768 --current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002770 // after the end of a pattern, try matching a keyword or pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 next_match_idx = -1;
2772
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002773 // if first state with "keepend" is popped, reset keepend_level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (keepend_level >= current_state.ga_len)
2775 keepend_level = -1;
2776}
2777
2778/*
2779 * Find the end of a start/skip/end syntax region after "startpos".
2780 * Only checks one line.
2781 * Also handles a match item that continued from a previous line.
2782 * If not found, the syntax item continues in the next line. m_endpos->lnum
2783 * will be 0.
2784 * If found, the end of the region and the end of the highlighting is
2785 * computed.
2786 */
2787 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002788find_endpos(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002789 int idx, // index of the pattern
2790 lpos_T *startpos, // where to start looking for an END match
2791 lpos_T *m_endpos, // return: end of match
2792 lpos_T *hl_endpos, // return: end of highlighting
2793 long *flagsp, // return: flags of matching END
2794 lpos_T *end_endpos, // return: end of end pattern match
2795 int *end_idx, // return: group ID for end pat. match, or 0
2796 reg_extmatch_T *start_ext) // submatches from the start pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 colnr_T matchcol;
2799 synpat_T *spp, *spp_skip;
2800 int start_idx;
2801 int best_idx;
2802 regmmatch_T regmatch;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002803 regmmatch_T best_regmatch; // startpos/endpos of best match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 lpos_T pos;
2805 char_u *line;
2806 int had_match = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002807 char_u buf_chartab[32]; // chartab array for syn option iskyeyword
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 // just in case we are invoked for a keyword
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002810 if (idx < 0)
2811 return;
2812
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 /*
2814 * Check for being called with a START pattern.
2815 * Can happen with a match that continues to the next line, because it
2816 * contained a region.
2817 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002818 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (spp->sp_type != SPTYPE_START)
2820 {
2821 *hl_endpos = *startpos;
2822 return;
2823 }
2824
2825 /*
2826 * Find the SKIP or first END pattern after the last START pattern.
2827 */
2828 for (;;)
2829 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002830 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 if (spp->sp_type != SPTYPE_START)
2832 break;
2833 ++idx;
2834 }
2835
2836 /*
2837 * Lookup the SKIP pattern (if present)
2838 */
2839 if (spp->sp_type == SPTYPE_SKIP)
2840 {
2841 spp_skip = spp;
2842 ++idx;
2843 }
2844 else
2845 spp_skip = NULL;
2846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002847 // Setup external matches for syn_regexec().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 unref_extmatch(re_extmatch_in);
2849 re_extmatch_in = ref_extmatch(start_ext);
2850
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002851 matchcol = startpos->col; // start looking for a match at sstart
2852 start_idx = idx; // remember the first END pattern.
2853 best_regmatch.startpos[0].col = 0; // avoid compiler warning
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002854
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002855 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002856 save_chartab(buf_chartab);
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 for (;;)
2859 {
2860 /*
2861 * Find end pattern that matches first after "matchcol".
2862 */
2863 best_idx = -1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002864 for (idx = start_idx; idx < syn_block->b_syn_patterns.ga_len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
2866 int lc_col = matchcol;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002867 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
Bram Moolenaar860cae12010-06-05 23:22:07 +02002869 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002870 if (spp->sp_type != SPTYPE_END) // past last END pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 break;
2872 lc_col -= spp->sp_offsets[SPO_LC_OFF];
2873 if (lc_col < 0)
2874 lc_col = 0;
2875
2876 regmatch.rmm_ic = spp->sp_ic;
2877 regmatch.regprog = spp->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002878 r = syn_regexec(&regmatch, startpos->lnum, lc_col,
2879 IF_SYN_TIME(&spp->sp_time));
2880 spp->sp_prog = regmatch.regprog;
2881 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
2883 if (best_idx == -1 || regmatch.startpos[0].col
2884 < best_regmatch.startpos[0].col)
2885 {
2886 best_idx = idx;
2887 best_regmatch.startpos[0] = regmatch.startpos[0];
2888 best_regmatch.endpos[0] = regmatch.endpos[0];
2889 }
2890 }
2891 }
2892
2893 /*
2894 * If all end patterns have been tried, and there is no match, the
2895 * item continues until end-of-line.
2896 */
2897 if (best_idx == -1)
2898 break;
2899
2900 /*
2901 * If the skip pattern matches before the end pattern,
2902 * continue searching after the skip pattern.
2903 */
2904 if (spp_skip != NULL)
2905 {
2906 int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002907 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
2909 if (lc_col < 0)
2910 lc_col = 0;
2911 regmatch.rmm_ic = spp_skip->sp_ic;
2912 regmatch.regprog = spp_skip->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002913 r = syn_regexec(&regmatch, startpos->lnum, lc_col,
2914 IF_SYN_TIME(&spp_skip->sp_time));
2915 spp_skip->sp_prog = regmatch.regprog;
2916 if (r && regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 <= best_regmatch.startpos[0].col)
2918 {
Bram Moolenaar04bff882016-01-05 20:46:16 +01002919 int line_len;
2920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002921 // Add offset to skip pattern match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 syn_add_end_off(&pos, &regmatch, spp_skip, SPO_ME_OFF, 1);
2923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002924 // If the skip pattern goes on to the next line, there is no
2925 // match with an end pattern in this line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (pos.lnum > startpos->lnum)
2927 break;
2928
2929 line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
Bram Moolenaar04bff882016-01-05 20:46:16 +01002930 line_len = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002932 // take care of an empty match or negative offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (pos.col <= matchcol)
2934 ++matchcol;
2935 else if (pos.col <= regmatch.endpos[0].col)
2936 matchcol = pos.col;
2937 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002938 // Be careful not to jump over the NUL at the end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 for (matchcol = regmatch.endpos[0].col;
Bram Moolenaar04bff882016-01-05 20:46:16 +01002940 matchcol < line_len && matchcol < pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 ++matchcol)
2942 ;
2943
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002944 // if the skip pattern includes end-of-line, break here
Bram Moolenaar04bff882016-01-05 20:46:16 +01002945 if (matchcol >= line_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 break;
2947
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002948 continue; // start with first end pattern again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 }
2951
2952 /*
2953 * Match from start pattern to end pattern.
2954 * Correct for match and highlight offset of end pattern.
2955 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002956 spp = &(SYN_ITEMS(syn_block)[best_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002958 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
2960 m_endpos->col = startpos->col;
2961
2962 syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002963 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (end_endpos->lnum == startpos->lnum
2965 && end_endpos->col < startpos->col)
2966 end_endpos->col = startpos->col;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002967 // can't end after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 limit_pos(end_endpos, m_endpos);
2969
2970 /*
2971 * If the end group is highlighted differently, adjust the pointers.
2972 */
2973 if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
2974 {
2975 *end_idx = best_idx;
2976 if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
2977 {
2978 hl_endpos->lnum = best_regmatch.endpos[0].lnum;
2979 hl_endpos->col = best_regmatch.endpos[0].col;
2980 }
2981 else
2982 {
2983 hl_endpos->lnum = best_regmatch.startpos[0].lnum;
2984 hl_endpos->col = best_regmatch.startpos[0].col;
2985 }
2986 hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
2987
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002988 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 if (hl_endpos->lnum == startpos->lnum
2990 && hl_endpos->col < startpos->col)
2991 hl_endpos->col = startpos->col;
2992 limit_pos(hl_endpos, m_endpos);
2993
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002994 // now the match ends where the highlighting ends, it is turned
2995 // into the matchgroup for the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 *m_endpos = *hl_endpos;
2997 }
2998 else
2999 {
3000 *end_idx = 0;
3001 *hl_endpos = *end_endpos;
3002 }
3003
3004 *flagsp = spp->sp_flags;
3005
3006 had_match = TRUE;
3007 break;
3008 }
3009
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003010 // no match for an END pattern in this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (!had_match)
3012 m_endpos->lnum = 0;
3013
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003014 restore_chartab(buf_chartab);
3015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003016 // Remove external matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 unref_extmatch(re_extmatch_in);
3018 re_extmatch_in = NULL;
3019}
3020
3021/*
3022 * Limit "pos" not to be after "limit".
3023 */
3024 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003025limit_pos(lpos_T *pos, lpos_T *limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 if (pos->lnum > limit->lnum)
3028 *pos = *limit;
3029 else if (pos->lnum == limit->lnum && pos->col > limit->col)
3030 pos->col = limit->col;
3031}
3032
3033/*
3034 * Limit "pos" not to be after "limit", unless pos->lnum is zero.
3035 */
3036 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003037limit_pos_zero(
3038 lpos_T *pos,
3039 lpos_T *limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 if (pos->lnum == 0)
3042 *pos = *limit;
3043 else
3044 limit_pos(pos, limit);
3045}
3046
3047/*
3048 * Add offset to matched text for end of match or highlight.
3049 */
3050 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003051syn_add_end_off(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003052 lpos_T *result, // returned position
3053 regmmatch_T *regmatch, // start/end of match
3054 synpat_T *spp, // matched pattern
3055 int idx, // index of offset
3056 int extra) // extra chars for offset to start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003059 int off;
3060 char_u *base;
3061 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 if (spp->sp_off_flags & (1 << idx))
3064 {
3065 result->lnum = regmatch->startpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066 col = regmatch->startpos[0].col;
3067 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
3069 else
3070 {
3071 result->lnum = regmatch->endpos[0].lnum;
3072 col = regmatch->endpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003075 // Don't go past the end of the line. Matters for "rs=e+2" when there
3076 // is a matchgroup. Watch out for match with last NL in the buffer.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077 if (result->lnum > syn_buf->b_ml.ml_line_count)
3078 col = 0;
3079 else if (off != 0)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003080 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003081 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3082 p = base + col;
3083 if (off > 0)
3084 {
3085 while (off-- > 0 && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003086 MB_PTR_ADV(p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087 }
3088 else if (off < 0)
3089 {
3090 while (off++ < 0 && base < p)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003091 MB_PTR_BACK(base, p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092 }
3093 col = (int)(p - base);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003094 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096}
3097
3098/*
3099 * Add offset to matched text for start of match or highlight.
3100 * Avoid resulting column to become negative.
3101 */
3102 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003103syn_add_start_off(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003104 lpos_T *result, // returned position
3105 regmmatch_T *regmatch, // start/end of match
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003106 synpat_T *spp,
3107 int idx,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003108 int extra) // extra chars for offset to end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 int off;
3112 char_u *base;
3113 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
3116 {
3117 result->lnum = regmatch->endpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118 col = regmatch->endpos[0].col;
3119 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121 else
3122 {
3123 result->lnum = regmatch->startpos[0].lnum;
3124 col = regmatch->startpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003127 if (result->lnum > syn_buf->b_ml.ml_line_count)
3128 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003129 // a "\n" at the end of the pattern may take us below the last line
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003130 result->lnum = syn_buf->b_ml.ml_line_count;
Bram Moolenaar8b9c05f2010-03-02 17:54:33 +01003131 col = (int)STRLEN(ml_get_buf(syn_buf, result->lnum, FALSE));
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003132 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133 if (off != 0)
3134 {
3135 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3136 p = base + col;
3137 if (off > 0)
3138 {
3139 while (off-- && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003140 MB_PTR_ADV(p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141 }
3142 else if (off < 0)
3143 {
3144 while (off++ && base < p)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003145 MB_PTR_BACK(base, p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146 }
3147 col = (int)(p - base);
3148 }
3149 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150}
3151
3152/*
3153 * Get current line in syntax buffer.
3154 */
3155 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003156syn_getcurline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 return ml_get_buf(syn_buf, current_lnum, FALSE);
3159}
3160
3161/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003162 * Call vim_regexec() to find a match with "rmp" in "syn_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 * Returns TRUE when there is a match.
3164 */
3165 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003166syn_regexec(
3167 regmmatch_T *rmp,
3168 linenr_T lnum,
3169 colnr_T col,
3170 syn_time_T *st UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003172 int r;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003173#ifdef FEAT_RELTIME
3174 int timed_out = FALSE;
3175#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +02003176#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003177 proftime_T pt;
3178
3179 if (syn_time_on)
3180 profile_start(&pt);
3181#endif
3182
Bram Moolenaarbcf94422018-06-23 14:21:42 +02003183 if (rmp->regprog == NULL)
3184 // This can happen if a previous call to vim_regexec_multi() tried to
3185 // use the NFA engine, which resulted in NFA_TOO_EXPENSIVE, and
3186 // compiling the pattern with the other engine fails.
3187 return FALSE;
3188
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003189 rmp->rmm_maxcol = syn_buf->b_p_smc;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003190 r = vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col,
3191#ifdef FEAT_RELTIME
3192 syn_tm, &timed_out
3193#else
3194 NULL, NULL
3195#endif
3196 );
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003197
Bram Moolenaarf7512552013-06-06 14:55:19 +02003198#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003199 if (syn_time_on)
3200 {
3201 profile_end(&pt);
3202 profile_add(&st->total, &pt);
3203 if (profile_cmp(&pt, &st->slowest) < 0)
3204 st->slowest = pt;
3205 ++st->count;
3206 if (r > 0)
3207 ++st->match;
3208 }
3209#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003210#ifdef FEAT_RELTIME
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003211 if (timed_out && !syn_win->w_s->b_syn_slow)
3212 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003213 syn_win->w_s->b_syn_slow = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003214 msg(_("'redrawtime' exceeded, syntax highlighting disabled"));
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003215 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003216#endif
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003217
3218 if (r > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 rmp->startpos[0].lnum += lnum;
3221 rmp->endpos[0].lnum += lnum;
3222 return TRUE;
3223 }
3224 return FALSE;
3225}
3226
3227/*
3228 * Check one position in a line for a matching keyword.
3229 * The caller must check if a keyword can start at startcol.
Bram Moolenaaraab93b12017-03-18 21:37:28 +01003230 * Return its ID if found, 0 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 */
3232 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003233check_keyword_id(
3234 char_u *line,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003235 int startcol, // position in line to check for keyword
3236 int *endcolp, // return: character after found keyword
3237 long *flagsp, // return: flags of matching keyword
3238 short **next_listp, // return: next_list of matching keyword
3239 stateitem_T *cur_si, // item at the top of the stack
3240 int *ccharp UNUSED) // conceal substitution char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
Bram Moolenaardad6b692005-01-25 22:14:34 +00003242 keyentry_T *kp;
3243 char_u *kwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 int round;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003245 int kwlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003246 char_u keyword[MAXKEYWLEN + 1]; // assume max. keyword len is 80
Bram Moolenaardad6b692005-01-25 22:14:34 +00003247 hashtab_T *ht;
3248 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003250 // Find first character after the keyword. First character was already
3251 // checked.
Bram Moolenaardad6b692005-01-25 22:14:34 +00003252 kwp = line + startcol;
3253 kwlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 do
3255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003257 kwlen += (*mb_ptr2len)(kwp + kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 else
Bram Moolenaardad6b692005-01-25 22:14:34 +00003259 ++kwlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 }
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01003261 while (vim_iswordp_buf(kwp + kwlen, syn_buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
Bram Moolenaardad6b692005-01-25 22:14:34 +00003263 if (kwlen > MAXKEYWLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 return 0;
3265
3266 /*
3267 * Must make a copy of the keyword, so we can add a NUL and make it
3268 * lowercase.
3269 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003270 vim_strncpy(keyword, kwp, kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
3272 /*
3273 * Try twice:
3274 * 1. matching case
3275 * 2. ignoring case
3276 */
3277 for (round = 1; round <= 2; ++round)
3278 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003279 ht = round == 1 ? &syn_block->b_keywtab : &syn_block->b_keywtab_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003280 if (ht->ht_used == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003282 if (round == 2) // ignore case
Bram Moolenaardad6b692005-01-25 22:14:34 +00003283 (void)str_foldcase(kwp, kwlen, keyword, MAXKEYWLEN + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
3285 /*
Bram Moolenaardad6b692005-01-25 22:14:34 +00003286 * Find keywords that match. There can be several with different
3287 * attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * When current_next_list is non-zero accept only that group, otherwise:
3289 * Accept a not-contained keyword at toplevel.
3290 * Accept a keyword at other levels only if it is in the contains list.
3291 */
Bram Moolenaardad6b692005-01-25 22:14:34 +00003292 hi = hash_find(ht, keyword);
3293 if (!HASHITEM_EMPTY(hi))
3294 for (kp = HI2KE(hi); kp != NULL; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00003296 if (current_next_list != 0
3297 ? in_id_list(NULL, current_next_list, &kp->k_syn, 0)
3298 : (cur_si == NULL
3299 ? !(kp->flags & HL_CONTAINED)
3300 : in_id_list(cur_si, cur_si->si_cont_list,
3301 &kp->k_syn, kp->flags & HL_CONTAINED)))
3302 {
3303 *endcolp = startcol + kwlen;
3304 *flagsp = kp->flags;
3305 *next_listp = kp->next_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003306#ifdef FEAT_CONCEAL
3307 *ccharp = kp->k_char;
3308#endif
Bram Moolenaardad6b692005-01-25 22:14:34 +00003309 return kp->k_syn.id;
3310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312 }
3313 return 0;
3314}
3315
3316/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02003317 * Handle ":syntax conceal" command.
3318 */
3319 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003320syn_cmd_conceal(exarg_T *eap UNUSED, int syncing UNUSED)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003321{
3322#ifdef FEAT_CONCEAL
3323 char_u *arg = eap->arg;
3324 char_u *next;
3325
3326 eap->nextcmd = find_nextcmd(arg);
3327 if (eap->skip)
3328 return;
3329
3330 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003331 if (*arg == NUL)
3332 {
3333 if (curwin->w_s->b_syn_conceal)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003334 msg(_("syntax conceal on"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003335 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003336 msg(_("syntax conceal off"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003337 }
3338 else if (STRNICMP(arg, "on", 2) == 0 && next - arg == 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003339 curwin->w_s->b_syn_conceal = TRUE;
3340 else if (STRNICMP(arg, "off", 3) == 0 && next - arg == 3)
3341 curwin->w_s->b_syn_conceal = FALSE;
3342 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003343 semsg(_("E390: Illegal argument: %s"), arg);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003344#endif
3345}
3346
3347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 * Handle ":syntax case" command.
3349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003351syn_cmd_case(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 char_u *arg = eap->arg;
3354 char_u *next;
3355
3356 eap->nextcmd = find_nextcmd(arg);
3357 if (eap->skip)
3358 return;
3359
3360 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003361 if (*arg == NUL)
3362 {
3363 if (curwin->w_s->b_syn_ic)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003364 msg(_("syntax case ignore"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003365 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003366 msg(_("syntax case match"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003367 }
3368 else if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003369 curwin->w_s->b_syn_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003371 curwin->w_s->b_syn_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003373 semsg(_("E390: Illegal argument: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374}
3375
3376/*
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003377 * Handle ":syntax spell" command.
3378 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003379 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003380syn_cmd_spell(exarg_T *eap, int syncing UNUSED)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003381{
3382 char_u *arg = eap->arg;
3383 char_u *next;
3384
3385 eap->nextcmd = find_nextcmd(arg);
3386 if (eap->skip)
3387 return;
3388
3389 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003390 if (*arg == NUL)
3391 {
3392 if (curwin->w_s->b_syn_spell == SYNSPL_TOP)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003393 msg(_("syntax spell toplevel"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003394 else if (curwin->w_s->b_syn_spell == SYNSPL_NOTOP)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003395 msg(_("syntax spell notoplevel"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003396 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003397 msg(_("syntax spell default"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003398 }
3399 else if (STRNICMP(arg, "toplevel", 8) == 0 && next - arg == 8)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003400 curwin->w_s->b_syn_spell = SYNSPL_TOP;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003401 else if (STRNICMP(arg, "notoplevel", 10) == 0 && next - arg == 10)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003402 curwin->w_s->b_syn_spell = SYNSPL_NOTOP;
Bram Moolenaar86ea7642007-02-04 01:48:10 +00003403 else if (STRNICMP(arg, "default", 7) == 0 && next - arg == 7)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003404 curwin->w_s->b_syn_spell = SYNSPL_DEFAULT;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003405 else
Bram Moolenaar5081d202015-06-25 18:36:26 +02003406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003407 semsg(_("E390: Illegal argument: %s"), arg);
Bram Moolenaar5081d202015-06-25 18:36:26 +02003408 return;
3409 }
3410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003411 // assume spell checking changed, force a redraw
Bram Moolenaar5081d202015-06-25 18:36:26 +02003412 redraw_win_later(curwin, NOT_VALID);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003413}
3414
3415/*
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003416 * Handle ":syntax iskeyword" command.
3417 */
3418 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003419syn_cmd_iskeyword(exarg_T *eap, int syncing UNUSED)
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003420{
3421 char_u *arg = eap->arg;
3422 char_u save_chartab[32];
3423 char_u *save_isk;
3424
3425 if (eap->skip)
3426 return;
3427
3428 arg = skipwhite(arg);
3429 if (*arg == NUL)
3430 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003431 msg_puts("\n");
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003432 if (curwin->w_s->b_syn_isk != empty_option)
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003433 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003434 msg_puts(_("syntax iskeyword "));
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003435 msg_outtrans(curwin->w_s->b_syn_isk);
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003436 }
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003437 else
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003438 msg_outtrans((char_u *)_("syntax iskeyword not set"));
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003439 }
3440 else
3441 {
3442 if (STRNICMP(arg, "clear", 5) == 0)
3443 {
3444 mch_memmove(curwin->w_s->b_syn_chartab, curbuf->b_chartab,
3445 (size_t)32);
3446 clear_string_option(&curwin->w_s->b_syn_isk);
3447 }
3448 else
3449 {
3450 mch_memmove(save_chartab, curbuf->b_chartab, (size_t)32);
3451 save_isk = curbuf->b_p_isk;
3452 curbuf->b_p_isk = vim_strsave(arg);
3453
3454 buf_init_chartab(curbuf, FALSE);
3455 mch_memmove(curwin->w_s->b_syn_chartab, curbuf->b_chartab,
3456 (size_t)32);
3457 mch_memmove(curbuf->b_chartab, save_chartab, (size_t)32);
3458 clear_string_option(&curwin->w_s->b_syn_isk);
3459 curwin->w_s->b_syn_isk = curbuf->b_p_isk;
3460 curbuf->b_p_isk = save_isk;
3461 }
3462 }
3463 redraw_win_later(curwin, NOT_VALID);
3464}
3465
3466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 * Clear all syntax info for one buffer.
3468 */
3469 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003470syntax_clear(synblock_T *block)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
3472 int i;
3473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003474 block->b_syn_error = FALSE; // clear previous error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003475#ifdef FEAT_RELTIME
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003476 block->b_syn_slow = FALSE; // clear previous timeout
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003477#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003478 block->b_syn_ic = FALSE; // Use case, by default
3479 block->b_syn_spell = SYNSPL_DEFAULT; // default spell checking
Bram Moolenaar860cae12010-06-05 23:22:07 +02003480 block->b_syn_containedin = FALSE;
Bram Moolenaarde318c52017-01-17 16:27:10 +01003481#ifdef FEAT_CONCEAL
3482 block->b_syn_conceal = FALSE;
3483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003485 // free the keywords
Bram Moolenaar860cae12010-06-05 23:22:07 +02003486 clear_keywtab(&block->b_keywtab);
3487 clear_keywtab(&block->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003489 // free the syntax patterns
Bram Moolenaar860cae12010-06-05 23:22:07 +02003490 for (i = block->b_syn_patterns.ga_len; --i >= 0; )
3491 syn_clear_pattern(block, i);
3492 ga_clear(&block->b_syn_patterns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003494 // free the syntax clusters
Bram Moolenaar860cae12010-06-05 23:22:07 +02003495 for (i = block->b_syn_clusters.ga_len; --i >= 0; )
3496 syn_clear_cluster(block, i);
3497 ga_clear(&block->b_syn_clusters);
3498 block->b_spell_cluster_id = 0;
3499 block->b_nospell_cluster_id = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaar860cae12010-06-05 23:22:07 +02003501 block->b_syn_sync_flags = 0;
3502 block->b_syn_sync_minlines = 0;
3503 block->b_syn_sync_maxlines = 0;
3504 block->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
Bram Moolenaar473de612013-06-08 18:19:48 +02003506 vim_regfree(block->b_syn_linecont_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003507 block->b_syn_linecont_prog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003508 VIM_CLEAR(block->b_syn_linecont_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#ifdef FEAT_FOLDING
Bram Moolenaar860cae12010-06-05 23:22:07 +02003510 block->b_syn_folditems = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#endif
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003512 clear_string_option(&block->b_syn_isk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003514 // free the stored states
Bram Moolenaar860cae12010-06-05 23:22:07 +02003515 syn_stack_free_all(block);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 invalidate_current_state();
Bram Moolenaar42431a72011-04-01 14:44:59 +02003517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003518 // Reset the counter for ":syn include"
Bram Moolenaar42431a72011-04-01 14:44:59 +02003519 running_syn_inc_tag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520}
3521
3522/*
Bram Moolenaarfd29f462010-06-06 16:11:09 +02003523 * Get rid of ownsyntax for window "wp".
3524 */
3525 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003526reset_synblock(win_T *wp)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02003527{
3528 if (wp->w_s != &wp->w_buffer->b_s)
3529 {
3530 syntax_clear(wp->w_s);
3531 vim_free(wp->w_s);
3532 wp->w_s = &wp->w_buffer->b_s;
3533 }
3534}
3535
3536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 * Clear syncing info for one buffer.
3538 */
3539 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003540syntax_sync_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542 int i;
3543
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003544 // free the syntax patterns
Bram Moolenaar860cae12010-06-05 23:22:07 +02003545 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
3546 if (SYN_ITEMS(curwin->w_s)[i].sp_syncing)
3547 syn_remove_pattern(curwin->w_s, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaar860cae12010-06-05 23:22:07 +02003549 curwin->w_s->b_syn_sync_flags = 0;
3550 curwin->w_s->b_syn_sync_minlines = 0;
3551 curwin->w_s->b_syn_sync_maxlines = 0;
3552 curwin->w_s->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaar473de612013-06-08 18:19:48 +02003554 vim_regfree(curwin->w_s->b_syn_linecont_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555 curwin->w_s->b_syn_linecont_prog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003556 VIM_CLEAR(curwin->w_s->b_syn_linecont_pat);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003557 clear_string_option(&curwin->w_s->b_syn_isk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003559 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
3562/*
3563 * Remove one pattern from the buffer's pattern list.
3564 */
3565 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003566syn_remove_pattern(
3567 synblock_T *block,
3568 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 synpat_T *spp;
3571
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572 spp = &(SYN_ITEMS(block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573#ifdef FEAT_FOLDING
3574 if (spp->sp_flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003575 --block->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577 syn_clear_pattern(block, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 mch_memmove(spp, spp + 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579 sizeof(synpat_T) * (block->b_syn_patterns.ga_len - idx - 1));
3580 --block->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583/*
3584 * Clear and free one syntax pattern. When clearing all, must be called from
3585 * last to first!
3586 */
3587 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003588syn_clear_pattern(synblock_T *block, int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 vim_free(SYN_ITEMS(block)[i].sp_pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02003591 vim_regfree(SYN_ITEMS(block)[i].sp_prog);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003592 // Only free sp_cont_list and sp_next_list of first start pattern
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593 if (i == 0 || SYN_ITEMS(block)[i - 1].sp_type != SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595 vim_free(SYN_ITEMS(block)[i].sp_cont_list);
3596 vim_free(SYN_ITEMS(block)[i].sp_next_list);
3597 vim_free(SYN_ITEMS(block)[i].sp_syn.cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599}
3600
3601/*
3602 * Clear and free one syntax cluster.
3603 */
3604 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003605syn_clear_cluster(synblock_T *block, int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607 vim_free(SYN_CLSTR(block)[i].scl_name);
3608 vim_free(SYN_CLSTR(block)[i].scl_name_u);
3609 vim_free(SYN_CLSTR(block)[i].scl_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610}
3611
3612/*
3613 * Handle ":syntax clear" command.
3614 */
3615 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003616syn_cmd_clear(exarg_T *eap, int syncing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 char_u *arg = eap->arg;
3619 char_u *arg_end;
3620 int id;
3621
3622 eap->nextcmd = find_nextcmd(arg);
3623 if (eap->skip)
3624 return;
3625
3626 /*
3627 * We have to disable this within ":syn include @group filename",
3628 * because otherwise @group would get deleted.
3629 * Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
3630 * clear".
3631 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003632 if (curwin->w_s->b_syn_topgrp != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 return;
3634
3635 if (ends_excmd(*arg))
3636 {
3637 /*
3638 * No argument: Clear all syntax items.
3639 */
3640 if (syncing)
3641 syntax_sync_clear();
3642 else
3643 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003644 syntax_clear(curwin->w_s);
3645 if (curwin->w_s == &curwin->w_buffer->b_s)
3646 do_unlet((char_u *)"b:current_syntax", TRUE);
Bram Moolenaar1950c352010-06-06 15:21:10 +02003647 do_unlet((char_u *)"w:current_syntax", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
3649 }
3650 else
3651 {
3652 /*
3653 * Clear the group IDs that are in the argument.
3654 */
3655 while (!ends_excmd(*arg))
3656 {
3657 arg_end = skiptowhite(arg);
3658 if (*arg == '@')
3659 {
3660 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3661 if (id == 0)
3662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003663 semsg(_("E391: No such syntax cluster: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 break;
3665 }
3666 else
3667 {
3668 /*
3669 * We can't physically delete a cluster without changing
3670 * the IDs of other clusters, so we do the next best thing
3671 * and make it empty.
3672 */
3673 short scl_id = id - SYNID_CLUSTER;
3674
Bram Moolenaard23a8232018-02-10 18:45:26 +01003675 VIM_CLEAR(SYN_CLSTR(curwin->w_s)[scl_id].scl_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 }
3678 else
3679 {
3680 id = syn_namen2id(arg, (int)(arg_end - arg));
3681 if (id == 0)
3682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003683 semsg(_(e_nogroup), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 break;
3685 }
3686 else
3687 syn_clear_one(id, syncing);
3688 }
3689 arg = skipwhite(arg_end);
3690 }
3691 }
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00003692 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003693 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694}
3695
3696/*
3697 * Clear one syntax group for the current buffer.
3698 */
3699 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003700syn_clear_one(int id, int syncing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 synpat_T *spp;
3703 int idx;
3704
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003705 // Clear keywords only when not ":syn sync clear group-name"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (!syncing)
3707 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003708 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab);
3709 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003712 // clear the patterns for "id"
Bram Moolenaar860cae12010-06-05 23:22:07 +02003713 for (idx = curwin->w_s->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003715 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3717 continue;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003718 syn_remove_pattern(curwin->w_s, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720}
3721
3722/*
3723 * Handle ":syntax on" command.
3724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003726syn_cmd_on(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
3728 syn_cmd_onoff(eap, "syntax");
3729}
3730
3731/*
3732 * Handle ":syntax enable" command.
3733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003735syn_cmd_enable(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"enable");
3738 syn_cmd_onoff(eap, "syntax");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003739 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740}
3741
3742/*
3743 * Handle ":syntax reset" command.
Bram Moolenaar8bc189e2016-04-02 19:01:58 +02003744 * It actually resets highlighting, not syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003747syn_cmd_reset(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 eap->nextcmd = check_nextcmd(eap->arg);
3750 if (!eap->skip)
3751 {
3752 set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"reset");
3753 do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003754 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756}
3757
3758/*
3759 * Handle ":syntax manual" command.
3760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003762syn_cmd_manual(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 syn_cmd_onoff(eap, "manual");
3765}
3766
3767/*
3768 * Handle ":syntax off" command.
3769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003771syn_cmd_off(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 syn_cmd_onoff(eap, "nosyntax");
3774}
3775
3776 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003777syn_cmd_onoff(exarg_T *eap, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 char_u buf[100];
3780
3781 eap->nextcmd = check_nextcmd(eap->arg);
3782 if (!eap->skip)
3783 {
3784 STRCPY(buf, "so ");
Bram Moolenaar555b2802005-05-19 21:08:39 +00003785 vim_snprintf((char *)buf + 3, sizeof(buf) - 3, SYNTAX_FNAME, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 do_cmdline_cmd(buf);
3787 }
3788}
3789
3790/*
3791 * Handle ":syntax [list]" command: list current syntax words.
3792 */
3793 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003794syn_cmd_list(
3795 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003796 int syncing) // when TRUE: list syncing items
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
3798 char_u *arg = eap->arg;
3799 int id;
3800 char_u *arg_end;
3801
3802 eap->nextcmd = find_nextcmd(arg);
3803 if (eap->skip)
3804 return;
3805
Bram Moolenaar860cae12010-06-05 23:22:07 +02003806 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003808 msg(_(msg_no_items));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 return;
3810 }
3811
3812 if (syncing)
3813 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003814 if (curwin->w_s->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003816 msg_puts(_("syncing on C-style comments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 syn_lines_msg();
3818 syn_match_msg();
3819 return;
3820 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003821 else if (!(curwin->w_s->b_syn_sync_flags & SF_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003823 if (curwin->w_s->b_syn_sync_minlines == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003824 msg_puts(_("no syncing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 else
3826 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003827 msg_puts(_("syncing starts "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003828 msg_outnum(curwin->w_s->b_syn_sync_minlines);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003829 msg_puts(_(" lines before top line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 syn_match_msg();
3831 }
3832 return;
3833 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003834 msg_puts_title(_("\n--- Syntax sync items ---"));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003835 if (curwin->w_s->b_syn_sync_minlines > 0
3836 || curwin->w_s->b_syn_sync_maxlines > 0
3837 || curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003839 msg_puts(_("\nsyncing on items"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 syn_lines_msg();
3841 syn_match_msg();
3842 }
3843 }
3844 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003845 msg_puts_title(_("\n--- Syntax items ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (ends_excmd(*arg))
3847 {
3848 /*
3849 * No argument: List all group IDs and all syntax clusters.
3850 */
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02003851 for (id = 1; id <= highlight_num_groups() && !got_int; ++id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 syn_list_one(id, syncing, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003853 for (id = 0; id < curwin->w_s->b_syn_clusters.ga_len && !got_int; ++id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 syn_list_cluster(id);
3855 }
3856 else
3857 {
3858 /*
3859 * List the group IDs and syntax clusters that are in the argument.
3860 */
3861 while (!ends_excmd(*arg) && !got_int)
3862 {
3863 arg_end = skiptowhite(arg);
3864 if (*arg == '@')
3865 {
3866 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3867 if (id == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003868 semsg(_("E392: No such syntax cluster: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
3870 syn_list_cluster(id - SYNID_CLUSTER);
3871 }
3872 else
3873 {
3874 id = syn_namen2id(arg, (int)(arg_end - arg));
3875 if (id == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003876 semsg(_(e_nogroup), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 else
3878 syn_list_one(id, syncing, TRUE);
3879 }
3880 arg = skipwhite(arg_end);
3881 }
3882 }
3883 eap->nextcmd = check_nextcmd(arg);
3884}
3885
3886 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003887syn_lines_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003889 if (curwin->w_s->b_syn_sync_maxlines > 0
3890 || curwin->w_s->b_syn_sync_minlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003892 msg_puts("; ");
Bram Moolenaar860cae12010-06-05 23:22:07 +02003893 if (curwin->w_s->b_syn_sync_minlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003895 msg_puts(_("minimal "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003896 msg_outnum(curwin->w_s->b_syn_sync_minlines);
3897 if (curwin->w_s->b_syn_sync_maxlines)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003898 msg_puts(", ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003900 if (curwin->w_s->b_syn_sync_maxlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003902 msg_puts(_("maximal "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003903 msg_outnum(curwin->w_s->b_syn_sync_maxlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003905 msg_puts(_(" lines before top line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907}
3908
3909 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003910syn_match_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003912 if (curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003914 msg_puts(_("; match "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003915 msg_outnum(curwin->w_s->b_syn_sync_linebreaks);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003916 msg_puts(_(" line breaks"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918}
3919
3920static int last_matchgroup;
3921
3922struct name_list
3923{
3924 int flag;
3925 char *name;
3926};
3927
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003928static void syn_list_flags(struct name_list *nl, int flags, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930/*
3931 * List one syntax item, for ":syntax" or "syntax list syntax_name".
3932 */
3933 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003934syn_list_one(
3935 int id,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003936 int syncing, // when TRUE: list syncing items
3937 int link_only) // when TRUE; list link-only too
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 int attr;
3940 int idx;
3941 int did_header = FALSE;
3942 synpat_T *spp;
3943 static struct name_list namelist1[] =
3944 {
3945 {HL_DISPLAY, "display"},
3946 {HL_CONTAINED, "contained"},
3947 {HL_ONELINE, "oneline"},
3948 {HL_KEEPEND, "keepend"},
3949 {HL_EXTEND, "extend"},
3950 {HL_EXCLUDENL, "excludenl"},
3951 {HL_TRANSP, "transparent"},
3952 {HL_FOLD, "fold"},
Bram Moolenaar860cae12010-06-05 23:22:07 +02003953#ifdef FEAT_CONCEAL
3954 {HL_CONCEAL, "conceal"},
3955 {HL_CONCEALENDS, "concealends"},
3956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {0, NULL}
3958 };
3959 static struct name_list namelist2[] =
3960 {
3961 {HL_SKIPWHITE, "skipwhite"},
3962 {HL_SKIPNL, "skipnl"},
3963 {HL_SKIPEMPTY, "skipempty"},
3964 {0, NULL}
3965 };
3966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003967 attr = HL_ATTR(HLF_D); // highlight like directories
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003969 // list the keywords for "id"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (!syncing)
3971 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003972 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab, FALSE, attr);
3973 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 did_header, attr);
3975 }
3976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003977 // list the patterns for "id"
Bram Moolenaar860cae12010-06-05 23:22:07 +02003978 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len && !got_int; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003980 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3982 continue;
3983
3984 (void)syn_list_header(did_header, 999, id);
3985 did_header = TRUE;
3986 last_matchgroup = 0;
3987 if (spp->sp_type == SPTYPE_MATCH)
3988 {
3989 put_pattern("match", ' ', spp, attr);
3990 msg_putchar(' ');
3991 }
3992 else if (spp->sp_type == SPTYPE_START)
3993 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003994 while (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_START)
3995 put_pattern("start", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
3996 if (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_SKIP)
3997 put_pattern("skip", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
3998 while (idx < curwin->w_s->b_syn_patterns.ga_len
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02003999 && SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_END)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004000 put_pattern("end", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 --idx;
4002 msg_putchar(' ');
4003 }
4004 syn_list_flags(namelist1, spp->sp_flags, attr);
4005
4006 if (spp->sp_cont_list != NULL)
4007 put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
4008
4009 if (spp->sp_syn.cont_in_list != NULL)
4010 put_id_list((char_u *)"containedin",
4011 spp->sp_syn.cont_in_list, attr);
4012
4013 if (spp->sp_next_list != NULL)
4014 {
4015 put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
4016 syn_list_flags(namelist2, spp->sp_flags, attr);
4017 }
4018 if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
4019 {
4020 if (spp->sp_flags & HL_SYNC_HERE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004021 msg_puts_attr("grouphere", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004023 msg_puts_attr("groupthere", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 msg_putchar(' ');
4025 if (spp->sp_sync_idx >= 0)
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004026 msg_outtrans(highlight_group_name(SYN_ITEMS(curwin->w_s)
4027 [spp->sp_sync_idx].sp_syn.id - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004029 msg_puts("NONE");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 msg_putchar(' ');
4031 }
4032 }
4033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004034 // list the link, if there is one
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004035 if (highlight_link_id(id - 1) && (did_header || link_only) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
4037 (void)syn_list_header(did_header, 999, id);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004038 msg_puts_attr("links to", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 msg_putchar(' ');
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004040 msg_outtrans(highlight_group_name(highlight_link_id(id - 1) - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 }
4042}
4043
4044 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004045syn_list_flags(struct name_list *nlist, int flags, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 int i;
4048
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004049 for (i = 0; nlist[i].flag != 0; ++i)
4050 if (flags & nlist[i].flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004052 msg_puts_attr(nlist[i].name, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 msg_putchar(' ');
4054 }
4055}
4056
4057/*
4058 * List one syntax cluster, for ":syntax" or "syntax list syntax_name".
4059 */
4060 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004061syn_list_cluster(int id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
4063 int endcol = 15;
4064
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004065 // slight hack: roughly duplicate the guts of syn_list_header()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 msg_putchar('\n');
Bram Moolenaar860cae12010-06-05 23:22:07 +02004067 msg_outtrans(SYN_CLSTR(curwin->w_s)[id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004069 if (msg_col >= endcol) // output at least one space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 endcol = msg_col + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004071 if (Columns <= endcol) // avoid hang for tiny window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 endcol = Columns - 1;
4073
4074 msg_advance(endcol);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004075 if (SYN_CLSTR(curwin->w_s)[id].scl_list != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004077 put_id_list((char_u *)"cluster", SYN_CLSTR(curwin->w_s)[id].scl_list,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004078 HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080 else
4081 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004082 msg_puts_attr("cluster", HL_ATTR(HLF_D));
4083 msg_puts("=NONE");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085}
4086
4087 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004088put_id_list(char_u *name, short *list, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
4090 short *p;
4091
Bram Moolenaar32526b32019-01-19 17:43:09 +01004092 msg_puts_attr((char *)name, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 msg_putchar('=');
4094 for (p = list; *p; ++p)
4095 {
4096 if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
4097 {
4098 if (p[1])
Bram Moolenaar32526b32019-01-19 17:43:09 +01004099 msg_puts("ALLBUT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004101 msg_puts("ALL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
4104 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004105 msg_puts("TOP");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
4108 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004109 msg_puts("CONTAINED");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 else if (*p >= SYNID_CLUSTER)
4112 {
4113 short scl_id = *p - SYNID_CLUSTER;
4114
4115 msg_putchar('@');
Bram Moolenaar860cae12010-06-05 23:22:07 +02004116 msg_outtrans(SYN_CLSTR(curwin->w_s)[scl_id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 else
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004119 msg_outtrans(highlight_group_name(*p - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (p[1])
4121 msg_putchar(',');
4122 }
4123 msg_putchar(' ');
4124}
4125
4126 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004127put_pattern(
4128 char *s,
4129 int c,
4130 synpat_T *spp,
4131 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 long n;
4134 int mask;
4135 int first;
4136 static char *sepchars = "/+=-#@\"|'^&";
4137 int i;
4138
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004139 // May have to write "matchgroup=group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 if (last_matchgroup != spp->sp_syn_match_id)
4141 {
4142 last_matchgroup = spp->sp_syn_match_id;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004143 msg_puts_attr("matchgroup", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 msg_putchar('=');
4145 if (last_matchgroup == 0)
4146 msg_outtrans((char_u *)"NONE");
4147 else
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004148 msg_outtrans(highlight_group_name(last_matchgroup - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 msg_putchar(' ');
4150 }
4151
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004152 // output the name of the pattern and an '=' or ' '
Bram Moolenaar32526b32019-01-19 17:43:09 +01004153 msg_puts_attr(s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 msg_putchar(c);
4155
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004156 // output the pattern, in between a char that is not in the pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 for (i = 0; vim_strchr(spp->sp_pattern, sepchars[i]) != NULL; )
4158 if (sepchars[++i] == NUL)
4159 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004160 i = 0; // no good char found, just use the first one
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162 }
4163 msg_putchar(sepchars[i]);
4164 msg_outtrans(spp->sp_pattern);
4165 msg_putchar(sepchars[i]);
4166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004167 // output any pattern options
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 first = TRUE;
4169 for (i = 0; i < SPO_COUNT; ++i)
4170 {
4171 mask = (1 << i);
4172 if (spp->sp_off_flags & (mask + (mask << SPO_COUNT)))
4173 {
4174 if (!first)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004175 msg_putchar(','); // separate with commas
Bram Moolenaar32526b32019-01-19 17:43:09 +01004176 msg_puts(spo_name_tab[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 n = spp->sp_offsets[i];
4178 if (i != SPO_LC_OFF)
4179 {
4180 if (spp->sp_off_flags & mask)
4181 msg_putchar('s');
4182 else
4183 msg_putchar('e');
4184 if (n > 0)
4185 msg_putchar('+');
4186 }
4187 if (n || i == SPO_LC_OFF)
4188 msg_outnum(n);
4189 first = FALSE;
4190 }
4191 }
4192 msg_putchar(' ');
4193}
4194
4195/*
4196 * List or clear the keywords for one syntax group.
4197 * Return TRUE if the header has been printed.
4198 */
4199 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004200syn_list_keywords(
4201 int id,
4202 hashtab_T *ht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004203 int did_header, // header has already been printed
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004204 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int outlen;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004207 hashitem_T *hi;
4208 keyentry_T *kp;
4209 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 int prev_contained = 0;
4211 short *prev_next_list = NULL;
4212 short *prev_cont_in_list = NULL;
4213 int prev_skipnl = 0;
4214 int prev_skipwhite = 0;
4215 int prev_skipempty = 0;
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 /*
4218 * Unfortunately, this list of keywords is not sorted on alphabet but on
4219 * hash value...
4220 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004221 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004222 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004224 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004226 --todo;
4227 for (kp = HI2KE(hi); kp != NULL && !got_int; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004229 if (kp->k_syn.id == id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004231 if (prev_contained != (kp->flags & HL_CONTAINED)
4232 || prev_skipnl != (kp->flags & HL_SKIPNL)
4233 || prev_skipwhite != (kp->flags & HL_SKIPWHITE)
4234 || prev_skipempty != (kp->flags & HL_SKIPEMPTY)
4235 || prev_cont_in_list != kp->k_syn.cont_in_list
4236 || prev_next_list != kp->next_list)
4237 outlen = 9999;
4238 else
4239 outlen = (int)STRLEN(kp->keyword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004240 // output "contained" and "nextgroup" on each line
Bram Moolenaardad6b692005-01-25 22:14:34 +00004241 if (syn_list_header(did_header, outlen, id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004243 prev_contained = 0;
4244 prev_next_list = NULL;
4245 prev_cont_in_list = NULL;
4246 prev_skipnl = 0;
4247 prev_skipwhite = 0;
4248 prev_skipempty = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004250 did_header = TRUE;
4251 if (prev_contained != (kp->flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004253 msg_puts_attr("contained", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004255 prev_contained = (kp->flags & HL_CONTAINED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004257 if (kp->k_syn.cont_in_list != prev_cont_in_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004259 put_id_list((char_u *)"containedin",
4260 kp->k_syn.cont_in_list, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004262 prev_cont_in_list = kp->k_syn.cont_in_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004264 if (kp->next_list != prev_next_list)
4265 {
4266 put_id_list((char_u *)"nextgroup", kp->next_list, attr);
4267 msg_putchar(' ');
4268 prev_next_list = kp->next_list;
4269 if (kp->flags & HL_SKIPNL)
4270 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004271 msg_puts_attr("skipnl", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004272 msg_putchar(' ');
4273 prev_skipnl = (kp->flags & HL_SKIPNL);
4274 }
4275 if (kp->flags & HL_SKIPWHITE)
4276 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004277 msg_puts_attr("skipwhite", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004278 msg_putchar(' ');
4279 prev_skipwhite = (kp->flags & HL_SKIPWHITE);
4280 }
4281 if (kp->flags & HL_SKIPEMPTY)
4282 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004283 msg_puts_attr("skipempty", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004284 msg_putchar(' ');
4285 prev_skipempty = (kp->flags & HL_SKIPEMPTY);
4286 }
4287 }
4288 msg_outtrans(kp->keyword);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292 }
4293
4294 return did_header;
4295}
4296
4297 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004298syn_clear_keyword(int id, hashtab_T *ht)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004300 hashitem_T *hi;
4301 keyentry_T *kp;
4302 keyentry_T *kp_prev;
4303 keyentry_T *kp_next;
4304 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Bram Moolenaardad6b692005-01-25 22:14:34 +00004306 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004307 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004308 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004310 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004312 --todo;
4313 kp_prev = NULL;
4314 for (kp = HI2KE(hi); kp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004316 if (kp->k_syn.id == id)
4317 {
4318 kp_next = kp->ke_next;
4319 if (kp_prev == NULL)
4320 {
4321 if (kp_next == NULL)
4322 hash_remove(ht, hi);
4323 else
4324 hi->hi_key = KE2HIKEY(kp_next);
4325 }
4326 else
4327 kp_prev->ke_next = kp_next;
4328 vim_free(kp->next_list);
4329 vim_free(kp->k_syn.cont_in_list);
4330 vim_free(kp);
4331 kp = kp_next;
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 else
Bram Moolenaardad6b692005-01-25 22:14:34 +00004334 {
4335 kp_prev = kp;
4336 kp = kp->ke_next;
4337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 }
4340 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004341 hash_unlock(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342}
4343
4344/*
Bram Moolenaardad6b692005-01-25 22:14:34 +00004345 * Clear a whole keyword table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 */
4347 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004348clear_keywtab(hashtab_T *ht)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004350 hashitem_T *hi;
4351 int todo;
4352 keyentry_T *kp;
4353 keyentry_T *kp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004355 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004356 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004358 if (!HASHITEM_EMPTY(hi))
4359 {
4360 --todo;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004361 for (kp = HI2KE(hi); kp != NULL; kp = kp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004363 kp_next = kp->ke_next;
4364 vim_free(kp->next_list);
4365 vim_free(kp->k_syn.cont_in_list);
4366 vim_free(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004370 hash_clear(ht);
4371 hash_init(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372}
4373
4374/*
4375 * Add a keyword to the list of keywords.
4376 */
4377 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004378add_keyword(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004379 char_u *name, // name of keyword
4380 int id, // group ID for this keyword
4381 int flags, // flags for this keyword
4382 short *cont_in_list, // containedin for this keyword
4383 short *next_list, // nextgroup for this keyword
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004384 int conceal_char)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004386 keyentry_T *kp;
4387 hashtab_T *ht;
4388 hashitem_T *hi;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004389 char_u *name_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004390 long_u hash;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004391 char_u name_folded[MAXKEYWLEN + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
Bram Moolenaar860cae12010-06-05 23:22:07 +02004393 if (curwin->w_s->b_syn_ic)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004394 name_ic = str_foldcase(name, (int)STRLEN(name),
4395 name_folded, MAXKEYWLEN + 1);
4396 else
4397 name_ic = name;
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004398 kp = alloc(offsetof(keyentry_T, keyword) + STRLEN(name_ic) + 1);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004399 if (kp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004401 STRCPY(kp->keyword, name_ic);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004402 kp->k_syn.id = id;
4403 kp->k_syn.inc_tag = current_syn_inc_tag;
4404 kp->flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004405 kp->k_char = conceal_char;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004406 kp->k_syn.cont_in_list = copy_id_list(cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004408 curwin->w_s->b_syn_containedin = TRUE;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004409 kp->next_list = copy_id_list(next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaar860cae12010-06-05 23:22:07 +02004411 if (curwin->w_s->b_syn_ic)
4412 ht = &curwin->w_s->b_keywtab_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02004414 ht = &curwin->w_s->b_keywtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
Bram Moolenaardad6b692005-01-25 22:14:34 +00004416 hash = hash_hash(kp->keyword);
4417 hi = hash_lookup(ht, kp->keyword, hash);
4418 if (HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004420 // new keyword, add to hashtable
Bram Moolenaardad6b692005-01-25 22:14:34 +00004421 kp->ke_next = NULL;
4422 hash_add_item(ht, hi, kp->keyword, hash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004426 // keyword already exists, prepend to list
Bram Moolenaardad6b692005-01-25 22:14:34 +00004427 kp->ke_next = HI2KE(hi);
4428 hi->hi_key = KE2HIKEY(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430}
4431
4432/*
4433 * Get the start and end of the group name argument.
4434 * Return a pointer to the first argument.
4435 * Return NULL if the end of the command was found instead of further args.
4436 */
4437 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004438get_group_name(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004439 char_u *arg, // start of the argument
4440 char_u **name_end) // pointer to end of the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442 char_u *rest;
4443
4444 *name_end = skiptowhite(arg);
4445 rest = skipwhite(*name_end);
4446
4447 /*
4448 * Check if there are enough arguments. The first argument may be a
4449 * pattern, where '|' is allowed, so only check for NUL.
4450 */
4451 if (ends_excmd(*arg) || *rest == NUL)
4452 return NULL;
4453 return rest;
4454}
4455
4456/*
4457 * Check for syntax command option arguments.
4458 * This can be called at any place in the list of arguments, and just picks
4459 * out the arguments that are known. Can be called several times in a row to
4460 * collect all options in between other arguments.
4461 * Return a pointer to the next argument (which isn't an option).
4462 * Return NULL for any error;
4463 */
4464 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004465get_syn_options(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004466 char_u *arg, // next argument to be checked
4467 syn_opt_arg_T *opt, // various things
Bram Moolenaarde318c52017-01-17 16:27:10 +01004468 int *conceal_char UNUSED,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004469 int skip) // TRUE if skipping over command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 char_u *gname_start, *gname;
4472 int syn_id;
4473 int len;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004474 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 int i;
4476 int fidx;
4477 static struct flag
4478 {
4479 char *name;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004480 int argtype;
4481 int flags;
4482 } flagtab[] = { {"cCoOnNtTaAiInNeEdD", 0, HL_CONTAINED},
4483 {"oOnNeElLiInNeE", 0, HL_ONELINE},
4484 {"kKeEeEpPeEnNdD", 0, HL_KEEPEND},
4485 {"eExXtTeEnNdD", 0, HL_EXTEND},
4486 {"eExXcClLuUdDeEnNlL", 0, HL_EXCLUDENL},
4487 {"tTrRaAnNsSpPaArReEnNtT", 0, HL_TRANSP},
4488 {"sSkKiIpPnNlL", 0, HL_SKIPNL},
4489 {"sSkKiIpPwWhHiItTeE", 0, HL_SKIPWHITE},
4490 {"sSkKiIpPeEmMpPtTyY", 0, HL_SKIPEMPTY},
4491 {"gGrRoOuUpPhHeErReE", 0, HL_SYNC_HERE},
4492 {"gGrRoOuUpPtThHeErReE", 0, HL_SYNC_THERE},
4493 {"dDiIsSpPlLaAyY", 0, HL_DISPLAY},
4494 {"fFoOlLdD", 0, HL_FOLD},
Bram Moolenaar860cae12010-06-05 23:22:07 +02004495 {"cCoOnNcCeEaAlL", 0, HL_CONCEAL},
4496 {"cCoOnNcCeEaAlLeEnNdDsS", 0, HL_CONCEALENDS},
4497 {"cCcChHaArR", 11, 0},
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004498 {"cCoOnNtTaAiInNsS", 1, 0},
4499 {"cCoOnNtTaAiInNeEdDiInN", 2, 0},
4500 {"nNeExXtTgGrRoOuUpP", 3, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 };
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004502 static char *first_letters = "cCoOkKeEtTsSgGdDfFnN";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004504 if (arg == NULL) // already detected error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 return NULL;
4506
Bram Moolenaar860cae12010-06-05 23:22:07 +02004507#ifdef FEAT_CONCEAL
4508 if (curwin->w_s->b_syn_conceal)
4509 opt->flags |= HL_CONCEAL;
4510#endif
4511
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 for (;;)
4513 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004514 /*
4515 * This is used very often when a large number of keywords is defined.
4516 * Need to skip quickly when no option name is found.
4517 * Also avoid tolower(), it's slow.
4518 */
4519 if (strchr(first_letters, *arg) == NULL)
4520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; )
4523 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004524 p = flagtab[fidx].name;
4525 for (i = 0, len = 0; p[i] != NUL; i += 2, ++len)
4526 if (arg[len] != p[i] && arg[len] != p[i + 1])
4527 break;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004528 if (p[i] == NUL && (VIM_ISWHITE(arg[len])
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004529 || (flagtab[fidx].argtype > 0
4530 ? arg[len] == '='
4531 : ends_excmd(arg[len]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004533 if (opt->keyword
4534 && (flagtab[fidx].flags == HL_DISPLAY
4535 || flagtab[fidx].flags == HL_FOLD
4536 || flagtab[fidx].flags == HL_EXTEND))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004537 // treat "display", "fold" and "extend" as a keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 fidx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 break;
4540 }
4541 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004542 if (fidx < 0) // no match found
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004545 if (flagtab[fidx].argtype == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004547 if (!opt->has_cont_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004549 emsg(_("E395: contains argument not accepted here"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return NULL;
4551 }
Bram Moolenaarde318c52017-01-17 16:27:10 +01004552 if (get_id_list(&arg, 8, &opt->cont_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 return NULL;
4554 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004555 else if (flagtab[fidx].argtype == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004557 if (get_id_list(&arg, 11, &opt->cont_in_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 return NULL;
4559 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004560 else if (flagtab[fidx].argtype == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004562 if (get_id_list(&arg, 9, &opt->next_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return NULL;
4564 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004565 else if (flagtab[fidx].argtype == 11 && arg[5] == '=')
4566 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004567 // cchar=?
Bram Moolenaar860cae12010-06-05 23:22:07 +02004568 if (has_mbyte)
4569 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004570#ifdef FEAT_CONCEAL
Bram Moolenaar860cae12010-06-05 23:22:07 +02004571 *conceal_char = mb_ptr2char(arg + 6);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004572#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004573 arg += mb_ptr2len(arg + 6) - 1;
4574 }
4575 else
Bram Moolenaar56be9502010-06-06 14:20:26 +02004576 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004577#ifdef FEAT_CONCEAL
4578 *conceal_char = arg[6];
4579#else
4580 ;
4581#endif
Bram Moolenaar56be9502010-06-06 14:20:26 +02004582 }
Bram Moolenaar4124e722011-01-22 00:58:20 +01004583#ifdef FEAT_CONCEAL
4584 if (!vim_isprintc_strict(*conceal_char))
4585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004586 emsg(_("E844: invalid cchar value"));
Bram Moolenaar4124e722011-01-22 00:58:20 +01004587 return NULL;
4588 }
4589#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004590 arg = skipwhite(arg + 7);
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 else
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004593 {
4594 opt->flags |= flagtab[fidx].flags;
4595 arg = skipwhite(arg + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004597 if (flagtab[fidx].flags == HL_SYNC_HERE
4598 || flagtab[fidx].flags == HL_SYNC_THERE)
4599 {
4600 if (opt->sync_idx == NULL)
4601 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004602 emsg(_("E393: group[t]here not accepted here"));
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004603 return NULL;
4604 }
4605 gname_start = arg;
4606 arg = skiptowhite(arg);
4607 if (gname_start == arg)
4608 return NULL;
4609 gname = vim_strnsave(gname_start, (int)(arg - gname_start));
4610 if (gname == NULL)
4611 return NULL;
4612 if (STRCMP(gname, "NONE") == 0)
4613 *opt->sync_idx = NONE_IDX;
4614 else
4615 {
4616 syn_id = syn_name2id(gname);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004617 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
4618 if (SYN_ITEMS(curwin->w_s)[i].sp_syn.id == syn_id
4619 && SYN_ITEMS(curwin->w_s)[i].sp_type == SPTYPE_START)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004620 {
4621 *opt->sync_idx = i;
4622 break;
4623 }
4624 if (i < 0)
4625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004626 semsg(_("E394: Didn't find region item for %s"), gname);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004627 vim_free(gname);
4628 return NULL;
4629 }
4630 }
4631
4632 vim_free(gname);
4633 arg = skipwhite(arg);
4634 }
4635#ifdef FEAT_FOLDING
4636 else if (flagtab[fidx].flags == HL_FOLD
4637 && foldmethodIsSyntax(curwin))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004638 // Need to update folds later.
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004639 foldUpdateAll(curwin);
4640#endif
4641 }
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 return arg;
4645}
4646
4647/*
4648 * Adjustments to syntax item when declared in a ":syn include"'d file.
4649 * Set the contained flag, and if the item is not already contained, add it
4650 * to the specified top-level group, if any.
4651 */
4652 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004653syn_incl_toplevel(int id, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004655 if ((*flagsp & HL_CONTAINED) || curwin->w_s->b_syn_topgrp == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 return;
4657 *flagsp |= HL_CONTAINED;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004658 if (curwin->w_s->b_syn_topgrp >= SYNID_CLUSTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004660 // We have to alloc this, because syn_combine_list() will free it.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004661 short *grp_list = ALLOC_MULT(short, 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004662 int tlg_id = curwin->w_s->b_syn_topgrp - SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 if (grp_list != NULL)
4665 {
4666 grp_list[0] = id;
4667 grp_list[1] = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004668 syn_combine_list(&SYN_CLSTR(curwin->w_s)[tlg_id].scl_list,
4669 &grp_list, CLUSTER_ADD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671 }
4672}
4673
4674/*
4675 * Handle ":syntax include [@{group-name}] filename" command.
4676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004678syn_cmd_include(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 char_u *arg = eap->arg;
4681 int sgl_id = 1;
4682 char_u *group_name_end;
4683 char_u *rest;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 char *errormsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 int prev_toplvl_grp;
4686 int prev_syn_inc_tag;
4687 int source = FALSE;
4688
4689 eap->nextcmd = find_nextcmd(arg);
4690 if (eap->skip)
4691 return;
4692
4693 if (arg[0] == '@')
4694 {
4695 ++arg;
4696 rest = get_group_name(arg, &group_name_end);
4697 if (rest == NULL)
4698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004699 emsg(_("E397: Filename required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return;
4701 }
4702 sgl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004703 if (sgl_id == 0)
4704 return;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004705 // separate_nextcmd() and expand_filename() depend on this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 eap->arg = rest;
4707 }
4708
4709 /*
4710 * Everything that's left, up to the next command, should be the
4711 * filename to include.
4712 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004713 eap->argt |= (EX_XFILE | EX_NOSPC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 separate_nextcmd(eap);
4715 if (*eap->arg == '<' || *eap->arg == '$' || mch_isFullName(eap->arg))
4716 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004717 // For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
4718 // file. Need to expand the file name first. In other cases
4719 // ":runtime!" is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 source = TRUE;
4721 if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL)
4722 {
4723 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004724 emsg(errormsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 return;
4726 }
4727 }
4728
4729 /*
4730 * Save and restore the existing top-level grouplist id and ":syn
4731 * include" tag around the actual inclusion.
4732 */
Bram Moolenaar42431a72011-04-01 14:44:59 +02004733 if (running_syn_inc_tag >= MAX_SYN_INC_TAG)
4734 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004735 emsg(_("E847: Too many syntax includes"));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004736 return;
4737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 prev_syn_inc_tag = current_syn_inc_tag;
4739 current_syn_inc_tag = ++running_syn_inc_tag;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004740 prev_toplvl_grp = curwin->w_s->b_syn_topgrp;
4741 curwin->w_s->b_syn_topgrp = sgl_id;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 if (source ? do_source(eap->arg, FALSE, DOSO_NONE, NULL) == FAIL
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004743 : source_runtime(eap->arg, DIP_ALL) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004744 semsg(_(e_notopen), eap->arg);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004745 curwin->w_s->b_syn_topgrp = prev_toplvl_grp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 current_syn_inc_tag = prev_syn_inc_tag;
4747}
4748
4749/*
4750 * Handle ":syntax keyword {group-name} [{option}] keyword .." command.
4751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004753syn_cmd_keyword(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 char_u *arg = eap->arg;
4756 char_u *group_name_end;
4757 int syn_id;
4758 char_u *rest;
Bram Moolenaar42431a72011-04-01 14:44:59 +02004759 char_u *keyword_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 char_u *p;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004761 char_u *kw;
4762 syn_opt_arg_T syn_opt_arg;
4763 int cnt;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004764 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 rest = get_group_name(arg, &group_name_end);
4767
4768 if (rest != NULL)
4769 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004770 if (eap->skip)
4771 syn_id = -1;
4772 else
4773 syn_id = syn_check_group(arg, (int)(group_name_end - arg));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004774 if (syn_id != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004775 // allocate a buffer, for removing backslashes in the keyword
Bram Moolenaar964b3742019-05-24 18:54:09 +02004776 keyword_copy = alloc(STRLEN(rest) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if (keyword_copy != NULL)
4778 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004779 syn_opt_arg.flags = 0;
4780 syn_opt_arg.keyword = TRUE;
4781 syn_opt_arg.sync_idx = NULL;
4782 syn_opt_arg.has_cont_list = FALSE;
4783 syn_opt_arg.cont_in_list = NULL;
4784 syn_opt_arg.next_list = NULL;
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /*
4787 * The options given apply to ALL keywords, so all options must be
4788 * found before keywords can be created.
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004789 * 1: collect the options and copy the keywords to keyword_copy.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004791 cnt = 0;
4792 p = keyword_copy;
4793 for ( ; rest != NULL && !ends_excmd(*rest); rest = skipwhite(rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004795 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char,
4796 eap->skip);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004797 if (rest == NULL || ends_excmd(*rest))
4798 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004799 // Copy the keyword, removing backslashes, and add a NUL.
Bram Moolenaar1c465442017-03-12 20:10:05 +01004800 while (*rest != NUL && !VIM_ISWHITE(*rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004802 if (*rest == '\\' && rest[1] != NUL)
4803 ++rest;
4804 *p++ = *rest++;
4805 }
4806 *p++ = NUL;
4807 ++cnt;
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004810 if (!eap->skip)
4811 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004812 // Adjust flags for use of ":syn include".
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004813 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
4814
4815 /*
4816 * 2: Add an entry for each keyword.
4817 */
4818 for (kw = keyword_copy; --cnt >= 0; kw += STRLEN(kw) + 1)
4819 {
4820 for (p = vim_strchr(kw, '['); ; )
4821 {
4822 if (p != NULL)
4823 *p = NUL;
4824 add_keyword(kw, syn_id, syn_opt_arg.flags,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004825 syn_opt_arg.cont_in_list,
4826 syn_opt_arg.next_list, conceal_char);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004827 if (p == NULL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004828 break;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004829 if (p[1] == NUL)
4830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004831 semsg(_("E789: Missing ']': %s"), kw);
Bram Moolenaar1560d072015-08-13 22:53:29 +02004832 goto error;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004833 }
4834 if (p[1] == ']')
4835 {
Bram Moolenaar1560d072015-08-13 22:53:29 +02004836 if (p[2] != NUL)
4837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004838 semsg(_("E890: trailing char after ']': %s]%s"),
Bram Moolenaar1560d072015-08-13 22:53:29 +02004839 kw, &p[2]);
4840 goto error;
4841 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004842 kw = p + 1; // skip over the "]"
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004843 break;
4844 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004845 if (has_mbyte)
4846 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004847 int l = (*mb_ptr2len)(p + 1);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004848
4849 mch_memmove(p, p + 1, l);
4850 p += l;
4851 }
4852 else
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004853 {
4854 p[0] = p[1];
4855 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 }
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
Bram Moolenaar1560d072015-08-13 22:53:29 +02004860error:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 vim_free(keyword_copy);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004862 vim_free(syn_opt_arg.cont_in_list);
4863 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
4866
4867 if (rest != NULL)
4868 eap->nextcmd = check_nextcmd(rest);
4869 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004870 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00004872 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004873 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874}
4875
4876/*
4877 * Handle ":syntax match {name} [{options}] {pattern} [{options}]".
4878 *
4879 * Also ":syntax sync match {name} [[grouphere | groupthere] {group-name}] .."
4880 */
4881 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004882syn_cmd_match(
4883 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004884 int syncing) // TRUE for ":syntax sync match .. "
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 char_u *arg = eap->arg;
4887 char_u *group_name_end;
4888 char_u *rest;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004889 synpat_T item; // the item found in the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 int syn_id;
4891 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004892 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 int sync_idx = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004894 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004896 // Isolate the group name, check for validity
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 rest = get_group_name(arg, &group_name_end);
4898
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004899 // Get options before the pattern
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004900 syn_opt_arg.flags = 0;
4901 syn_opt_arg.keyword = FALSE;
4902 syn_opt_arg.sync_idx = syncing ? &sync_idx : NULL;
4903 syn_opt_arg.has_cont_list = TRUE;
4904 syn_opt_arg.cont_list = NULL;
4905 syn_opt_arg.cont_in_list = NULL;
4906 syn_opt_arg.next_list = NULL;
Bram Moolenaarde318c52017-01-17 16:27:10 +01004907 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004909 // get the pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 init_syn_patterns();
4911 vim_memset(&item, 0, sizeof(item));
4912 rest = get_syn_pattern(rest, &item);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004913 if (vim_regcomp_had_eol() && !(syn_opt_arg.flags & HL_EXCLUDENL))
4914 syn_opt_arg.flags |= HL_HAS_EOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004916 // Get options after the pattern
Bram Moolenaarde318c52017-01-17 16:27:10 +01004917 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004919 if (rest != NULL) // all arguments are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
4921 /*
4922 * Check for trailing command and illegal trailing arguments.
4923 */
4924 eap->nextcmd = check_nextcmd(rest);
4925 if (!ends_excmd(*rest) || eap->skip)
4926 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004927 else if (ga_grow(&curwin->w_s->b_syn_patterns, 1) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 && (syn_id = syn_check_group(arg,
4929 (int)(group_name_end - arg))) != 0)
4930 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004931 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 /*
4933 * Store the pattern in the syn_items list
4934 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004935 idx = curwin->w_s->b_syn_patterns.ga_len;
4936 SYN_ITEMS(curwin->w_s)[idx] = item;
4937 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
4938 SYN_ITEMS(curwin->w_s)[idx].sp_type = SPTYPE_MATCH;
4939 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
4940 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag = current_syn_inc_tag;
4941 SYN_ITEMS(curwin->w_s)[idx].sp_flags = syn_opt_arg.flags;
4942 SYN_ITEMS(curwin->w_s)[idx].sp_sync_idx = sync_idx;
4943 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list = syn_opt_arg.cont_list;
4944 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004945 syn_opt_arg.cont_in_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004946#ifdef FEAT_CONCEAL
Bram Moolenaar6e202e52010-07-28 18:14:45 +02004947 SYN_ITEMS(curwin->w_s)[idx].sp_cchar = conceal_char;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004948#endif
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004949 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004950 curwin->w_s->b_syn_containedin = TRUE;
4951 SYN_ITEMS(curwin->w_s)[idx].sp_next_list = syn_opt_arg.next_list;
4952 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004954 // remember that we found a match for syncing on
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004955 if (syn_opt_arg.flags & (HL_SYNC_HERE|HL_SYNC_THERE))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004956 curwin->w_s->b_syn_sync_flags |= SF_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004958 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004959 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#endif
4961
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00004962 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004963 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
4964 return; // don't free the progs and patterns now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966 }
4967
4968 /*
4969 * Something failed, free the allocated memory.
4970 */
Bram Moolenaar473de612013-06-08 18:19:48 +02004971 vim_regfree(item.sp_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 vim_free(item.sp_pattern);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004973 vim_free(syn_opt_arg.cont_list);
4974 vim_free(syn_opt_arg.cont_in_list);
4975 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 if (rest == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004978 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979}
4980
4981/*
4982 * Handle ":syntax region {group-name} [matchgroup={group-name}]
4983 * start {start} .. [skip {skip}] end {end} .. [{options}]".
4984 */
4985 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004986syn_cmd_region(
4987 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004988 int syncing) // TRUE for ":syntax sync region .."
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 char_u *arg = eap->arg;
4991 char_u *group_name_end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004992 char_u *rest; // next arg, NULL on error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 char_u *key_end;
4994 char_u *key = NULL;
4995 char_u *p;
4996 int item;
4997#define ITEM_START 0
4998#define ITEM_SKIP 1
4999#define ITEM_END 2
5000#define ITEM_MATCHGROUP 3
5001 struct pat_ptr
5002 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005003 synpat_T *pp_synp; // pointer to syn_pattern
5004 int pp_matchgroup_id; // matchgroup ID
5005 struct pat_ptr *pp_next; // pointer to next pat_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 } *(pat_ptrs[3]);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005007 // patterns found in the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 struct pat_ptr *ppp;
5009 struct pat_ptr *ppp_next;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005010 int pat_count = 0; // nr of syn_patterns found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 int syn_id;
5012 int matchgroup_id = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005013 int not_enough = FALSE; // not enough arguments
5014 int illegal = FALSE; // illegal arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 int success = FALSE;
5016 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005017 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005018 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005020 // Isolate the group name, check for validity
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 rest = get_group_name(arg, &group_name_end);
5022
5023 pat_ptrs[0] = NULL;
5024 pat_ptrs[1] = NULL;
5025 pat_ptrs[2] = NULL;
5026
5027 init_syn_patterns();
5028
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005029 syn_opt_arg.flags = 0;
5030 syn_opt_arg.keyword = FALSE;
5031 syn_opt_arg.sync_idx = NULL;
5032 syn_opt_arg.has_cont_list = TRUE;
5033 syn_opt_arg.cont_list = NULL;
5034 syn_opt_arg.cont_in_list = NULL;
5035 syn_opt_arg.next_list = NULL;
5036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 /*
5038 * get the options, patterns and matchgroup.
5039 */
5040 while (rest != NULL && !ends_excmd(*rest))
5041 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005042 // Check for option arguments
Bram Moolenaarde318c52017-01-17 16:27:10 +01005043 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (rest == NULL || ends_excmd(*rest))
5045 break;
5046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005047 // must be a pattern or matchgroup then
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 key_end = rest;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005049 while (*key_end && !VIM_ISWHITE(*key_end) && *key_end != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 ++key_end;
5051 vim_free(key);
5052 key = vim_strnsave_up(rest, (int)(key_end - rest));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005053 if (key == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 rest = NULL;
5056 break;
5057 }
5058 if (STRCMP(key, "MATCHGROUP") == 0)
5059 item = ITEM_MATCHGROUP;
5060 else if (STRCMP(key, "START") == 0)
5061 item = ITEM_START;
5062 else if (STRCMP(key, "END") == 0)
5063 item = ITEM_END;
5064 else if (STRCMP(key, "SKIP") == 0)
5065 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005066 if (pat_ptrs[ITEM_SKIP] != NULL) // one skip pattern allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
5068 illegal = TRUE;
5069 break;
5070 }
5071 item = ITEM_SKIP;
5072 }
5073 else
5074 break;
5075 rest = skipwhite(key_end);
5076 if (*rest != '=')
5077 {
5078 rest = NULL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005079 semsg(_("E398: Missing '=': %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 break;
5081 }
5082 rest = skipwhite(rest + 1);
5083 if (*rest == NUL)
5084 {
5085 not_enough = TRUE;
5086 break;
5087 }
5088
5089 if (item == ITEM_MATCHGROUP)
5090 {
5091 p = skiptowhite(rest);
5092 if ((p - rest == 4 && STRNCMP(rest, "NONE", 4) == 0) || eap->skip)
5093 matchgroup_id = 0;
5094 else
5095 {
5096 matchgroup_id = syn_check_group(rest, (int)(p - rest));
5097 if (matchgroup_id == 0)
5098 {
5099 illegal = TRUE;
5100 break;
5101 }
5102 }
5103 rest = skipwhite(p);
5104 }
5105 else
5106 {
5107 /*
5108 * Allocate room for a syn_pattern, and link it in the list of
5109 * syn_patterns for this item, at the start (because the list is
5110 * used from end to start).
5111 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005112 ppp = ALLOC_ONE(struct pat_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (ppp == NULL)
5114 {
5115 rest = NULL;
5116 break;
5117 }
5118 ppp->pp_next = pat_ptrs[item];
5119 pat_ptrs[item] = ppp;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005120 ppp->pp_synp = ALLOC_CLEAR_ONE(synpat_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (ppp->pp_synp == NULL)
5122 {
5123 rest = NULL;
5124 break;
5125 }
5126
5127 /*
5128 * Get the syntax pattern and the following offset(s).
5129 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005130 // Enable the appropriate \z specials.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (item == ITEM_START)
5132 reg_do_extmatch = REX_SET;
5133 else if (item == ITEM_SKIP || item == ITEM_END)
5134 reg_do_extmatch = REX_USE;
5135 rest = get_syn_pattern(rest, ppp->pp_synp);
5136 reg_do_extmatch = 0;
5137 if (item == ITEM_END && vim_regcomp_had_eol()
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005138 && !(syn_opt_arg.flags & HL_EXCLUDENL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 ppp->pp_synp->sp_flags |= HL_HAS_EOL;
5140 ppp->pp_matchgroup_id = matchgroup_id;
5141 ++pat_count;
5142 }
5143 }
5144 vim_free(key);
5145 if (illegal || not_enough)
5146 rest = NULL;
5147
5148 /*
5149 * Must have a "start" and "end" pattern.
5150 */
5151 if (rest != NULL && (pat_ptrs[ITEM_START] == NULL ||
5152 pat_ptrs[ITEM_END] == NULL))
5153 {
5154 not_enough = TRUE;
5155 rest = NULL;
5156 }
5157
5158 if (rest != NULL)
5159 {
5160 /*
5161 * Check for trailing garbage or command.
5162 * If OK, add the item.
5163 */
5164 eap->nextcmd = check_nextcmd(rest);
5165 if (!ends_excmd(*rest) || eap->skip)
5166 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167 else if (ga_grow(&(curwin->w_s->b_syn_patterns), pat_count) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 && (syn_id = syn_check_group(arg,
5169 (int)(group_name_end - arg))) != 0)
5170 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005171 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 /*
5173 * Store the start/skip/end in the syn_items list
5174 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005175 idx = curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 for (item = ITEM_START; item <= ITEM_END; ++item)
5177 {
5178 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp->pp_next)
5179 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005180 SYN_ITEMS(curwin->w_s)[idx] = *(ppp->pp_synp);
5181 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
5182 SYN_ITEMS(curwin->w_s)[idx].sp_type =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 (item == ITEM_START) ? SPTYPE_START :
5184 (item == ITEM_SKIP) ? SPTYPE_SKIP : SPTYPE_END;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005185 SYN_ITEMS(curwin->w_s)[idx].sp_flags |= syn_opt_arg.flags;
5186 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
Bram Moolenaar42431a72011-04-01 14:44:59 +02005187 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag =
5188 current_syn_inc_tag;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005189 SYN_ITEMS(curwin->w_s)[idx].sp_syn_match_id =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 ppp->pp_matchgroup_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005191#ifdef FEAT_CONCEAL
Bram Moolenaar6e202e52010-07-28 18:14:45 +02005192 SYN_ITEMS(curwin->w_s)[idx].sp_cchar = conceal_char;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (item == ITEM_START)
5195 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005196 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005197 syn_opt_arg.cont_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005198 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005199 syn_opt_arg.cont_in_list;
5200 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005201 curwin->w_s->b_syn_containedin = TRUE;
5202 SYN_ITEMS(curwin->w_s)[idx].sp_next_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005203 syn_opt_arg.next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005205 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ++idx;
5207#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005208 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005209 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#endif
5211 }
5212 }
5213
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005214 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005215 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
5216 success = TRUE; // don't free the progs and patterns now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218 }
5219
5220 /*
5221 * Free the allocated memory.
5222 */
5223 for (item = ITEM_START; item <= ITEM_END; ++item)
5224 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp_next)
5225 {
Bram Moolenaar4bbfb0f2019-08-31 15:28:02 +02005226 if (!success && ppp->pp_synp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
Bram Moolenaar473de612013-06-08 18:19:48 +02005228 vim_regfree(ppp->pp_synp->sp_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 vim_free(ppp->pp_synp->sp_pattern);
5230 }
5231 vim_free(ppp->pp_synp);
5232 ppp_next = ppp->pp_next;
5233 vim_free(ppp);
5234 }
5235
5236 if (!success)
5237 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005238 vim_free(syn_opt_arg.cont_list);
5239 vim_free(syn_opt_arg.cont_in_list);
5240 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (not_enough)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005242 semsg(_("E399: Not enough arguments: syntax region %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else if (illegal || rest == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005244 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246}
5247
5248/*
5249 * A simple syntax group ID comparison function suitable for use in qsort()
5250 */
5251 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005252syn_compare_stub(const void *v1, const void *v2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 const short *s1 = v1;
5255 const short *s2 = v2;
5256
5257 return (*s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0);
5258}
5259
5260/*
5261 * Combines lists of syntax clusters.
5262 * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
5263 */
5264 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005265syn_combine_list(short **clstr1, short **clstr2, int list_op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 int count1 = 0;
5268 int count2 = 0;
5269 short *g1;
5270 short *g2;
5271 short *clstr = NULL;
5272 int count;
5273 int round;
5274
5275 /*
5276 * Handle degenerate cases.
5277 */
5278 if (*clstr2 == NULL)
5279 return;
5280 if (*clstr1 == NULL || list_op == CLUSTER_REPLACE)
5281 {
5282 if (list_op == CLUSTER_REPLACE)
5283 vim_free(*clstr1);
5284 if (list_op == CLUSTER_REPLACE || list_op == CLUSTER_ADD)
5285 *clstr1 = *clstr2;
5286 else
5287 vim_free(*clstr2);
5288 return;
5289 }
5290
5291 for (g1 = *clstr1; *g1; g1++)
5292 ++count1;
5293 for (g2 = *clstr2; *g2; g2++)
5294 ++count2;
5295
5296 /*
5297 * For speed purposes, sort both lists.
5298 */
5299 qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
5300 qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
5301
5302 /*
5303 * We proceed in two passes; in round 1, we count the elements to place
5304 * in the new list, and in round 2, we allocate and populate the new
5305 * list. For speed, we use a mergesort-like method, adding the smaller
5306 * of the current elements in each list to the new list.
5307 */
5308 for (round = 1; round <= 2; round++)
5309 {
5310 g1 = *clstr1;
5311 g2 = *clstr2;
5312 count = 0;
5313
5314 /*
5315 * First, loop through the lists until one of them is empty.
5316 */
5317 while (*g1 && *g2)
5318 {
5319 /*
5320 * We always want to add from the first list.
5321 */
5322 if (*g1 < *g2)
5323 {
5324 if (round == 2)
5325 clstr[count] = *g1;
5326 count++;
5327 g1++;
5328 continue;
5329 }
5330 /*
5331 * We only want to add from the second list if we're adding the
5332 * lists.
5333 */
5334 if (list_op == CLUSTER_ADD)
5335 {
5336 if (round == 2)
5337 clstr[count] = *g2;
5338 count++;
5339 }
5340 if (*g1 == *g2)
5341 g1++;
5342 g2++;
5343 }
5344
5345 /*
5346 * Now add the leftovers from whichever list didn't get finished
5347 * first. As before, we only want to add from the second list if
5348 * we're adding the lists.
5349 */
5350 for (; *g1; g1++, count++)
5351 if (round == 2)
5352 clstr[count] = *g1;
5353 if (list_op == CLUSTER_ADD)
5354 for (; *g2; g2++, count++)
5355 if (round == 2)
5356 clstr[count] = *g2;
5357
5358 if (round == 1)
5359 {
5360 /*
5361 * If the group ended up empty, we don't need to allocate any
5362 * space for it.
5363 */
5364 if (count == 0)
5365 {
5366 clstr = NULL;
5367 break;
5368 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005369 clstr = ALLOC_MULT(short, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (clstr == NULL)
5371 break;
5372 clstr[count] = 0;
5373 }
5374 }
5375
5376 /*
5377 * Finally, put the new list in place.
5378 */
5379 vim_free(*clstr1);
5380 vim_free(*clstr2);
5381 *clstr1 = clstr;
5382}
5383
5384/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005385 * Lookup a syntax cluster name and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 * If it is not found, 0 is returned.
5387 */
5388 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005389syn_scl_name2id(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391 int i;
5392 char_u *name_u;
5393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005394 // Avoid using stricmp() too much, it's slow on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 name_u = vim_strsave_up(name);
5396 if (name_u == NULL)
5397 return 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005398 for (i = curwin->w_s->b_syn_clusters.ga_len; --i >= 0; )
5399 if (SYN_CLSTR(curwin->w_s)[i].scl_name_u != NULL
5400 && STRCMP(name_u, SYN_CLSTR(curwin->w_s)[i].scl_name_u) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 break;
5402 vim_free(name_u);
5403 return (i < 0 ? 0 : i + SYNID_CLUSTER);
5404}
5405
5406/*
5407 * Like syn_scl_name2id(), but take a pointer + length argument.
5408 */
5409 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005410syn_scl_namen2id(char_u *linep, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 char_u *name;
5413 int id = 0;
5414
5415 name = vim_strnsave(linep, len);
5416 if (name != NULL)
5417 {
5418 id = syn_scl_name2id(name);
5419 vim_free(name);
5420 }
5421 return id;
5422}
5423
5424/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005425 * Find syntax cluster name in the table and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 * The argument is a pointer to the name and the length of the name.
5427 * If it doesn't exist yet, a new entry is created.
5428 * Return 0 for failure.
5429 */
5430 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005431syn_check_cluster(char_u *pp, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 int id;
5434 char_u *name;
5435
5436 name = vim_strnsave(pp, len);
5437 if (name == NULL)
5438 return 0;
5439
5440 id = syn_scl_name2id(name);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005441 if (id == 0) // doesn't exist yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 id = syn_add_cluster(name);
5443 else
5444 vim_free(name);
5445 return id;
5446}
5447
5448/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005449 * Add new syntax cluster and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 * "name" must be an allocated string, it will be consumed.
5451 * Return 0 for failure.
5452 */
5453 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005454syn_add_cluster(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
Bram Moolenaar217ad922005-03-20 22:37:15 +00005456 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458 /*
5459 * First call for this growarray: init growing array.
5460 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005461 if (curwin->w_s->b_syn_clusters.ga_data == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005463 curwin->w_s->b_syn_clusters.ga_itemsize = sizeof(syn_cluster_T);
5464 curwin->w_s->b_syn_clusters.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466
Bram Moolenaar42431a72011-04-01 14:44:59 +02005467 len = curwin->w_s->b_syn_clusters.ga_len;
5468 if (len >= MAX_CLUSTER_ID)
5469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005470 emsg(_("E848: Too many syntax clusters"));
Bram Moolenaar42431a72011-04-01 14:44:59 +02005471 vim_free(name);
5472 return 0;
5473 }
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 /*
5476 * Make room for at least one other cluster entry.
5477 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005478 if (ga_grow(&curwin->w_s->b_syn_clusters, 1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
5480 vim_free(name);
5481 return 0;
5482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaar860cae12010-06-05 23:22:07 +02005484 vim_memset(&(SYN_CLSTR(curwin->w_s)[len]), 0, sizeof(syn_cluster_T));
5485 SYN_CLSTR(curwin->w_s)[len].scl_name = name;
5486 SYN_CLSTR(curwin->w_s)[len].scl_name_u = vim_strsave_up(name);
5487 SYN_CLSTR(curwin->w_s)[len].scl_list = NULL;
5488 ++curwin->w_s->b_syn_clusters.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
Bram Moolenaar217ad922005-03-20 22:37:15 +00005490 if (STRICMP(name, "Spell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005491 curwin->w_s->b_spell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005492 if (STRICMP(name, "NoSpell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005493 curwin->w_s->b_nospell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 return len + SYNID_CLUSTER;
5496}
5497
5498/*
5499 * Handle ":syntax cluster {cluster-name} [contains={groupname},..]
5500 * [add={groupname},..] [remove={groupname},..]".
5501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005503syn_cmd_cluster(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 char_u *arg = eap->arg;
5506 char_u *group_name_end;
5507 char_u *rest;
5508 int scl_id;
5509 short *clstr_list;
5510 int got_clstr = FALSE;
5511 int opt_len;
5512 int list_op;
5513
5514 eap->nextcmd = find_nextcmd(arg);
5515 if (eap->skip)
5516 return;
5517
5518 rest = get_group_name(arg, &group_name_end);
5519
5520 if (rest != NULL)
5521 {
Bram Moolenaar42431a72011-04-01 14:44:59 +02005522 scl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
5523 if (scl_id == 0)
5524 return;
5525 scl_id -= SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 for (;;)
5528 {
5529 if (STRNICMP(rest, "add", 3) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005530 && (VIM_ISWHITE(rest[3]) || rest[3] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 opt_len = 3;
5533 list_op = CLUSTER_ADD;
5534 }
5535 else if (STRNICMP(rest, "remove", 6) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005536 && (VIM_ISWHITE(rest[6]) || rest[6] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 opt_len = 6;
5539 list_op = CLUSTER_SUBTRACT;
5540 }
5541 else if (STRNICMP(rest, "contains", 8) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005542 && (VIM_ISWHITE(rest[8]) || rest[8] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
5544 opt_len = 8;
5545 list_op = CLUSTER_REPLACE;
5546 }
5547 else
5548 break;
5549
5550 clstr_list = NULL;
Bram Moolenaarde318c52017-01-17 16:27:10 +01005551 if (get_id_list(&rest, opt_len, &clstr_list, eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005553 semsg(_(e_invarg2), rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 break;
5555 }
Bram Moolenaarde318c52017-01-17 16:27:10 +01005556 if (scl_id >= 0)
5557 syn_combine_list(&SYN_CLSTR(curwin->w_s)[scl_id].scl_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 &clstr_list, list_op);
Bram Moolenaard7a96152017-01-22 15:28:55 +01005559 else
5560 vim_free(clstr_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 got_clstr = TRUE;
5562 }
5563
5564 if (got_clstr)
5565 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005566 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005567 syn_stack_free_all(curwin->w_s); // Need to recompute all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 }
5570
5571 if (!got_clstr)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005572 emsg(_("E400: No cluster specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (rest == NULL || !ends_excmd(*rest))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005574 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575}
5576
5577/*
5578 * On first call for current buffer: Init growing array.
5579 */
5580 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005581init_syn_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
Bram Moolenaar860cae12010-06-05 23:22:07 +02005583 curwin->w_s->b_syn_patterns.ga_itemsize = sizeof(synpat_T);
5584 curwin->w_s->b_syn_patterns.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
5587/*
5588 * Get one pattern for a ":syntax match" or ":syntax region" command.
5589 * Stores the pattern and program in a synpat_T.
5590 * Returns a pointer to the next argument, or NULL in case of an error.
5591 */
5592 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005593get_syn_pattern(char_u *arg, synpat_T *ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 char_u *end;
5596 int *p;
5597 int idx;
5598 char_u *cpo_save;
5599
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005600 // need at least three chars
Bram Moolenaar38219782015-08-11 15:27:13 +02005601 if (arg == NULL || arg[0] == NUL || arg[1] == NUL || arg[2] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 return NULL;
5603
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005604 end = skip_regexp(arg + 1, *arg, TRUE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005605 if (*end != *arg) // end delimiter not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005607 semsg(_("E401: Pattern delimiter not found: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 return NULL;
5609 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005610 // store the pattern and compiled regexp program
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if ((ci->sp_pattern = vim_strnsave(arg + 1, (int)(end - arg - 1))) == NULL)
5612 return NULL;
5613
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005614 // Make 'cpoptions' empty, to avoid the 'l' flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 cpo_save = p_cpo;
5616 p_cpo = (char_u *)"";
5617 ci->sp_prog = vim_regcomp(ci->sp_pattern, RE_MAGIC);
5618 p_cpo = cpo_save;
5619
5620 if (ci->sp_prog == NULL)
5621 return NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005622 ci->sp_ic = curwin->w_s->b_syn_ic;
Bram Moolenaarf7512552013-06-06 14:55:19 +02005623#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005624 syn_clear_time(&ci->sp_time);
5625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 /*
5628 * Check for a match, highlight or region offset.
5629 */
5630 ++end;
5631 do
5632 {
5633 for (idx = SPO_COUNT; --idx >= 0; )
5634 if (STRNCMP(end, spo_name_tab[idx], 3) == 0)
5635 break;
5636 if (idx >= 0)
5637 {
5638 p = &(ci->sp_offsets[idx]);
5639 if (idx != SPO_LC_OFF)
5640 switch (end[3])
5641 {
5642 case 's': break;
5643 case 'b': break;
5644 case 'e': idx += SPO_COUNT; break;
5645 default: idx = -1; break;
5646 }
5647 if (idx >= 0)
5648 {
5649 ci->sp_off_flags |= (1 << idx);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005650 if (idx == SPO_LC_OFF) // lc=99
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
5652 end += 3;
5653 *p = getdigits(&end);
5654
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005655 // "lc=" offset automatically sets "ms=" offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (!(ci->sp_off_flags & (1 << SPO_MS_OFF)))
5657 {
5658 ci->sp_off_flags |= (1 << SPO_MS_OFF);
5659 ci->sp_offsets[SPO_MS_OFF] = *p;
5660 }
5661 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 else // yy=x+99
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
5664 end += 4;
5665 if (*end == '+')
5666 {
5667 ++end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005668 *p = getdigits(&end); // positive offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670 else if (*end == '-')
5671 {
5672 ++end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005673 *p = -getdigits(&end); // negative offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 }
5676 if (*end != ',')
5677 break;
5678 ++end;
5679 }
5680 }
5681 } while (idx >= 0);
5682
Bram Moolenaar1c465442017-03-12 20:10:05 +01005683 if (!ends_excmd(*end) && !VIM_ISWHITE(*end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005685 semsg(_("E402: Garbage after pattern: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 return NULL;
5687 }
5688 return skipwhite(end);
5689}
5690
5691/*
5692 * Handle ":syntax sync .." command.
5693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005695syn_cmd_sync(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 char_u *arg_start = eap->arg;
5698 char_u *arg_end;
5699 char_u *key = NULL;
5700 char_u *next_arg;
5701 int illegal = FALSE;
5702 int finished = FALSE;
5703 long n;
5704 char_u *cpo_save;
5705
5706 if (ends_excmd(*arg_start))
5707 {
5708 syn_cmd_list(eap, TRUE);
5709 return;
5710 }
5711
5712 while (!ends_excmd(*arg_start))
5713 {
5714 arg_end = skiptowhite(arg_start);
5715 next_arg = skipwhite(arg_end);
5716 vim_free(key);
5717 key = vim_strnsave_up(arg_start, (int)(arg_end - arg_start));
5718 if (STRCMP(key, "CCOMMENT") == 0)
5719 {
5720 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005721 curwin->w_s->b_syn_sync_flags |= SF_CCOMMENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 if (!ends_excmd(*next_arg))
5723 {
5724 arg_end = skiptowhite(next_arg);
5725 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005726 curwin->w_s->b_syn_sync_id = syn_check_group(next_arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 (int)(arg_end - next_arg));
5728 next_arg = skipwhite(arg_end);
5729 }
5730 else if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005731 curwin->w_s->b_syn_sync_id = syn_name2id((char_u *)"Comment");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else if ( STRNCMP(key, "LINES", 5) == 0
5734 || STRNCMP(key, "MINLINES", 8) == 0
5735 || STRNCMP(key, "MAXLINES", 8) == 0
5736 || STRNCMP(key, "LINEBREAKS", 10) == 0)
5737 {
5738 if (key[4] == 'S')
5739 arg_end = key + 6;
5740 else if (key[0] == 'L')
5741 arg_end = key + 11;
5742 else
5743 arg_end = key + 9;
5744 if (arg_end[-1] != '=' || !VIM_ISDIGIT(*arg_end))
5745 {
5746 illegal = TRUE;
5747 break;
5748 }
5749 n = getdigits(&arg_end);
5750 if (!eap->skip)
5751 {
5752 if (key[4] == 'B')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005753 curwin->w_s->b_syn_sync_linebreaks = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 else if (key[1] == 'A')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005755 curwin->w_s->b_syn_sync_maxlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02005757 curwin->w_s->b_syn_sync_minlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759 }
5760 else if (STRCMP(key, "FROMSTART") == 0)
5761 {
5762 if (!eap->skip)
5763 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005764 curwin->w_s->b_syn_sync_minlines = MAXLNUM;
5765 curwin->w_s->b_syn_sync_maxlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 }
5768 else if (STRCMP(key, "LINECONT") == 0)
5769 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005770 if (*next_arg == NUL) // missing pattern
Bram Moolenaar2795e212016-01-05 22:04:49 +01005771 {
5772 illegal = TRUE;
5773 break;
5774 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005775 if (curwin->w_s->b_syn_linecont_pat != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005777 emsg(_("E403: syntax sync: line continuations pattern specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 finished = TRUE;
5779 break;
5780 }
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005781 arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005782 if (*arg_end != *next_arg) // end delimiter not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
5784 illegal = TRUE;
5785 break;
5786 }
5787
5788 if (!eap->skip)
5789 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005790 // store the pattern and compiled regexp program
Bram Moolenaar860cae12010-06-05 23:22:07 +02005791 if ((curwin->w_s->b_syn_linecont_pat = vim_strnsave(next_arg + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 (int)(arg_end - next_arg - 1))) == NULL)
5793 {
5794 finished = TRUE;
5795 break;
5796 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005797 curwin->w_s->b_syn_linecont_ic = curwin->w_s->b_syn_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005799 // Make 'cpoptions' empty, to avoid the 'l' flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 cpo_save = p_cpo;
5801 p_cpo = (char_u *)"";
Bram Moolenaar860cae12010-06-05 23:22:07 +02005802 curwin->w_s->b_syn_linecont_prog =
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005803 vim_regcomp(curwin->w_s->b_syn_linecont_pat, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 p_cpo = cpo_save;
Bram Moolenaarf7512552013-06-06 14:55:19 +02005805#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005806 syn_clear_time(&curwin->w_s->b_syn_linecont_time);
5807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808
Bram Moolenaar860cae12010-06-05 23:22:07 +02005809 if (curwin->w_s->b_syn_linecont_prog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005811 VIM_CLEAR(curwin->w_s->b_syn_linecont_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 finished = TRUE;
5813 break;
5814 }
5815 }
5816 next_arg = skipwhite(arg_end + 1);
5817 }
5818 else
5819 {
5820 eap->arg = next_arg;
5821 if (STRCMP(key, "MATCH") == 0)
5822 syn_cmd_match(eap, TRUE);
5823 else if (STRCMP(key, "REGION") == 0)
5824 syn_cmd_region(eap, TRUE);
5825 else if (STRCMP(key, "CLEAR") == 0)
5826 syn_cmd_clear(eap, TRUE);
5827 else
5828 illegal = TRUE;
5829 finished = TRUE;
5830 break;
5831 }
5832 arg_start = next_arg;
5833 }
5834 vim_free(key);
5835 if (illegal)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005836 semsg(_("E404: Illegal arguments: %s"), arg_start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 else if (!finished)
5838 {
5839 eap->nextcmd = check_nextcmd(arg_start);
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005840 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005841 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843}
5844
5845/*
5846 * Convert a line of highlight group names into a list of group ID numbers.
5847 * "arg" should point to the "contains" or "nextgroup" keyword.
5848 * "arg" is advanced to after the last group name.
5849 * Careful: the argument is modified (NULs added).
5850 * returns FAIL for some error, OK for success.
5851 */
5852 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005853get_id_list(
5854 char_u **arg,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005855 int keylen, // length of keyword
5856 short **list, // where to store the resulting list, if not
5857 // NULL, the list is silently skipped!
Bram Moolenaarde318c52017-01-17 16:27:10 +01005858 int skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 char_u *p = NULL;
5861 char_u *end;
5862 int round;
5863 int count;
5864 int total_count = 0;
5865 short *retval = NULL;
5866 char_u *name;
5867 regmatch_T regmatch;
5868 int id;
5869 int i;
5870 int failed = FALSE;
5871
5872 /*
5873 * We parse the list twice:
5874 * round == 1: count the number of items, allocate the array.
5875 * round == 2: fill the array with the items.
5876 * In round 1 new groups may be added, causing the number of items to
5877 * grow when a regexp is used. In that case round 1 is done once again.
5878 */
5879 for (round = 1; round <= 2; ++round)
5880 {
5881 /*
5882 * skip "contains"
5883 */
5884 p = skipwhite(*arg + keylen);
5885 if (*p != '=')
5886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005887 semsg(_("E405: Missing equal sign: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 break;
5889 }
5890 p = skipwhite(p + 1);
5891 if (ends_excmd(*p))
5892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005893 semsg(_("E406: Empty argument: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 break;
5895 }
5896
5897 /*
5898 * parse the arguments after "contains"
5899 */
5900 count = 0;
5901 while (!ends_excmd(*p))
5902 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005903 for (end = p; *end && !VIM_ISWHITE(*end) && *end != ','; ++end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 ;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005905 name = alloc(end - p + 3); // leave room for "^$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (name == NULL)
5907 {
5908 failed = TRUE;
5909 break;
5910 }
Bram Moolenaarce0842a2005-07-18 21:58:11 +00005911 vim_strncpy(name + 1, p, end - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 if ( STRCMP(name + 1, "ALLBUT") == 0
5913 || STRCMP(name + 1, "ALL") == 0
5914 || STRCMP(name + 1, "TOP") == 0
5915 || STRCMP(name + 1, "CONTAINED") == 0)
5916 {
5917 if (TOUPPER_ASC(**arg) != 'C')
5918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005919 semsg(_("E407: %s not allowed here"), name + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 failed = TRUE;
5921 vim_free(name);
5922 break;
5923 }
5924 if (count != 0)
5925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005926 semsg(_("E408: %s must be first in contains list"),
Bram Moolenaard7a96152017-01-22 15:28:55 +01005927 name + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 failed = TRUE;
5929 vim_free(name);
5930 break;
5931 }
5932 if (name[1] == 'A')
5933 id = SYNID_ALLBUT;
5934 else if (name[1] == 'T')
5935 id = SYNID_TOP;
5936 else
5937 id = SYNID_CONTAINED;
5938 id += current_syn_inc_tag;
5939 }
5940 else if (name[1] == '@')
5941 {
Bram Moolenaareb46f8f2017-01-17 19:48:53 +01005942 if (skip)
5943 id = -1;
5944 else
Bram Moolenaarde318c52017-01-17 16:27:10 +01005945 id = syn_check_cluster(name + 2, (int)(end - p - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 }
5947 else
5948 {
5949 /*
5950 * Handle full group name.
5951 */
5952 if (vim_strpbrk(name + 1, (char_u *)"\\.*^$~[") == NULL)
5953 id = syn_check_group(name + 1, (int)(end - p));
5954 else
5955 {
5956 /*
5957 * Handle match of regexp with group names.
5958 */
5959 *name = '^';
5960 STRCAT(name, "$");
5961 regmatch.regprog = vim_regcomp(name, RE_MAGIC);
5962 if (regmatch.regprog == NULL)
5963 {
5964 failed = TRUE;
5965 vim_free(name);
5966 break;
5967 }
5968
5969 regmatch.rm_ic = TRUE;
5970 id = 0;
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02005971 for (i = highlight_num_groups(); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02005973 if (vim_regexec(&regmatch, highlight_group_name(i),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 (colnr_T)0))
5975 {
5976 if (round == 2)
5977 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005978 // Got more items than expected; can happen
5979 // when adding items that match:
5980 // "contains=a.*b,axb".
5981 // Go back to first round
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 if (count >= total_count)
5983 {
5984 vim_free(retval);
5985 round = 1;
5986 }
5987 else
5988 retval[count] = i + 1;
5989 }
5990 ++count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005991 id = -1; // remember that we found one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
5993 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005994 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996 }
5997 vim_free(name);
5998 if (id == 0)
5999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006000 semsg(_("E409: Unknown group name: %s"), p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 failed = TRUE;
6002 break;
6003 }
6004 if (id > 0)
6005 {
6006 if (round == 2)
6007 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006008 // Got more items than expected, go back to first round
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (count >= total_count)
6010 {
6011 vim_free(retval);
6012 round = 1;
6013 }
6014 else
6015 retval[count] = id;
6016 }
6017 ++count;
6018 }
6019 p = skipwhite(end);
6020 if (*p != ',')
6021 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006022 p = skipwhite(p + 1); // skip comma in between arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
6024 if (failed)
6025 break;
6026 if (round == 1)
6027 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006028 retval = ALLOC_MULT(short, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 if (retval == NULL)
6030 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006031 retval[count] = 0; // zero means end of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 total_count = count;
6033 }
6034 }
6035
6036 *arg = p;
6037 if (failed || retval == NULL)
6038 {
6039 vim_free(retval);
6040 return FAIL;
6041 }
6042
6043 if (*list == NULL)
6044 *list = retval;
6045 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006046 vim_free(retval); // list already found, don't overwrite it
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 return OK;
6049}
6050
6051/*
6052 * Make a copy of an ID list.
6053 */
6054 static short *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006055copy_id_list(short *list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056{
6057 int len;
6058 int count;
6059 short *retval;
6060
6061 if (list == NULL)
6062 return NULL;
6063
6064 for (count = 0; list[count]; ++count)
6065 ;
6066 len = (count + 1) * sizeof(short);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006067 retval = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (retval != NULL)
6069 mch_memmove(retval, list, (size_t)len);
6070
6071 return retval;
6072}
6073
6074/*
6075 * Check if syntax group "ssp" is in the ID list "list" of "cur_si".
6076 * "cur_si" can be NULL if not checking the "containedin" list.
6077 * Used to check if a syntax item is in the "contains" or "nextgroup" list of
6078 * the current item.
6079 * This function is called very often, keep it fast!!
6080 */
6081 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006082in_id_list(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006083 stateitem_T *cur_si, // current item or NULL
6084 short *list, // id list
6085 struct sp_syn *ssp, // group id and ":syn include" tag of group
6086 int contained) // group id is contained
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
6088 int retval;
6089 short *scl_list;
6090 short item;
6091 short id = ssp->id;
6092 static int depth = 0;
6093 int r;
6094
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006095 // If ssp has a "containedin" list and "cur_si" is in it, return TRUE.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006096 if (cur_si != NULL && ssp->cont_in_list != NULL
6097 && !(cur_si->si_flags & HL_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006099 // Ignore transparent items without a contains argument. Double check
6100 // that we don't go back past the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 while ((cur_si->si_flags & HL_TRANS_CONT)
6102 && cur_si > (stateitem_T *)(current_state.ga_data))
6103 --cur_si;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006104 // cur_si->si_idx is -1 for keywords, these never contain anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (cur_si->si_idx >= 0 && in_id_list(NULL, ssp->cont_in_list,
Bram Moolenaar860cae12010-06-05 23:22:07 +02006106 &(SYN_ITEMS(syn_block)[cur_si->si_idx].sp_syn),
6107 SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 return TRUE;
6109 }
6110
6111 if (list == NULL)
6112 return FALSE;
6113
6114 /*
6115 * If list is ID_LIST_ALL, we are in a transparent item that isn't
6116 * inside anything. Only allow not-contained groups.
6117 */
6118 if (list == ID_LIST_ALL)
6119 return !contained;
6120
6121 /*
6122 * If the first item is "ALLBUT", return TRUE if "id" is NOT in the
6123 * contains list. We also require that "id" is at the same ":syn include"
6124 * level as the list.
6125 */
6126 item = *list;
6127 if (item >= SYNID_ALLBUT && item < SYNID_CLUSTER)
6128 {
6129 if (item < SYNID_TOP)
6130 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006131 // ALL or ALLBUT: accept all groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (item - SYNID_ALLBUT != ssp->inc_tag)
6133 return FALSE;
6134 }
6135 else if (item < SYNID_CONTAINED)
6136 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006137 // TOP: accept all not-contained groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (item - SYNID_TOP != ssp->inc_tag || contained)
6139 return FALSE;
6140 }
6141 else
6142 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006143 // CONTAINED: accept all contained groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (item - SYNID_CONTAINED != ssp->inc_tag || !contained)
6145 return FALSE;
6146 }
6147 item = *++list;
6148 retval = FALSE;
6149 }
6150 else
6151 retval = TRUE;
6152
6153 /*
6154 * Return "retval" if id is in the contains list.
6155 */
6156 while (item != 0)
6157 {
6158 if (item == id)
6159 return retval;
6160 if (item >= SYNID_CLUSTER)
6161 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006162 scl_list = SYN_CLSTR(syn_block)[item - SYNID_CLUSTER].scl_list;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006163 // restrict recursiveness to 30 to avoid an endless loop for a
6164 // cluster that includes itself (indirectly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (scl_list != NULL && depth < 30)
6166 {
6167 ++depth;
6168 r = in_id_list(NULL, scl_list, ssp, contained);
6169 --depth;
6170 if (r)
6171 return retval;
6172 }
6173 }
6174 item = *++list;
6175 }
6176 return !retval;
6177}
6178
6179struct subcommand
6180{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006181 char *name; // subcommand name
6182 void (*func)(exarg_T *, int); // function to call
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183};
6184
6185static struct subcommand subcommands[] =
6186{
6187 {"case", syn_cmd_case},
6188 {"clear", syn_cmd_clear},
6189 {"cluster", syn_cmd_cluster},
Bram Moolenaar860cae12010-06-05 23:22:07 +02006190 {"conceal", syn_cmd_conceal},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 {"enable", syn_cmd_enable},
6192 {"include", syn_cmd_include},
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006193 {"iskeyword", syn_cmd_iskeyword},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {"keyword", syn_cmd_keyword},
6195 {"list", syn_cmd_list},
6196 {"manual", syn_cmd_manual},
6197 {"match", syn_cmd_match},
6198 {"on", syn_cmd_on},
6199 {"off", syn_cmd_off},
6200 {"region", syn_cmd_region},
6201 {"reset", syn_cmd_reset},
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006202 {"spell", syn_cmd_spell},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {"sync", syn_cmd_sync},
6204 {"", syn_cmd_list},
6205 {NULL, NULL}
6206};
6207
6208/*
6209 * ":syntax".
6210 * This searches the subcommands[] table for the subcommand name, and calls a
6211 * syntax_subcommand() function to do the rest.
6212 */
6213 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006214ex_syntax(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 char_u *arg = eap->arg;
6217 char_u *subcmd_end;
6218 char_u *subcmd_name;
6219 int i;
6220
6221 syn_cmdlinep = eap->cmdlinep;
6222
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006223 // isolate subcommand name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 for (subcmd_end = arg; ASCII_ISALPHA(*subcmd_end); ++subcmd_end)
6225 ;
6226 subcmd_name = vim_strnsave(arg, (int)(subcmd_end - arg));
6227 if (subcmd_name != NULL)
6228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006229 if (eap->skip) // skip error messages for all subcommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 ++emsg_skip;
6231 for (i = 0; ; ++i)
6232 {
6233 if (subcommands[i].name == NULL)
6234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006235 semsg(_("E410: Invalid :syntax subcommand: %s"), subcmd_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 break;
6237 }
6238 if (STRCMP(subcmd_name, (char_u *)subcommands[i].name) == 0)
6239 {
6240 eap->arg = skipwhite(subcmd_end);
6241 (subcommands[i].func)(eap, FALSE);
6242 break;
6243 }
6244 }
6245 vim_free(subcmd_name);
6246 if (eap->skip)
6247 --emsg_skip;
6248 }
6249}
6250
Bram Moolenaar860cae12010-06-05 23:22:07 +02006251 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006252ex_ownsyntax(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
Bram Moolenaar1950c352010-06-06 15:21:10 +02006254 char_u *old_value;
6255 char_u *new_value;
6256
Bram Moolenaar860cae12010-06-05 23:22:07 +02006257 if (curwin->w_s == &curwin->w_buffer->b_s)
6258 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006259 curwin->w_s = ALLOC_ONE(synblock_T);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006260 memset(curwin->w_s, 0, sizeof(synblock_T));
Bram Moolenaar670acbc2015-08-25 11:58:36 +02006261 hash_init(&curwin->w_s->b_keywtab);
6262 hash_init(&curwin->w_s->b_keywtab_ic);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006263#ifdef FEAT_SPELL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006264 // TODO: keep the spell checking as it was.
6265 curwin->w_p_spell = FALSE; // No spell checking
Bram Moolenaar860cae12010-06-05 23:22:07 +02006266 clear_string_option(&curwin->w_s->b_p_spc);
6267 clear_string_option(&curwin->w_s->b_p_spf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006268 clear_string_option(&curwin->w_s->b_p_spl);
6269#endif
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006270 clear_string_option(&curwin->w_s->b_syn_isk);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006271 }
Bram Moolenaar1950c352010-06-06 15:21:10 +02006272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006273 // save value of b:current_syntax
Bram Moolenaar1950c352010-06-06 15:21:10 +02006274 old_value = get_var_value((char_u *)"b:current_syntax");
6275 if (old_value != NULL)
6276 old_value = vim_strsave(old_value);
6277
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006278 // Apply the "syntax" autocommand event, this finds and loads the syntax
6279 // file.
Bram Moolenaar860cae12010-06-05 23:22:07 +02006280 apply_autocmds(EVENT_SYNTAX, eap->arg, curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1950c352010-06-06 15:21:10 +02006281
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006282 // move value of b:current_syntax to w:current_syntax
Bram Moolenaar1950c352010-06-06 15:21:10 +02006283 new_value = get_var_value((char_u *)"b:current_syntax");
Bram Moolenaare0c6a652010-06-06 23:10:19 +02006284 if (new_value != NULL)
6285 set_internal_string_var((char_u *)"w:current_syntax", new_value);
Bram Moolenaar1950c352010-06-06 15:21:10 +02006286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006287 // restore value of b:current_syntax
Bram Moolenaare0c6a652010-06-06 23:10:19 +02006288 if (old_value == NULL)
6289 do_unlet((char_u *)"b:current_syntax", TRUE);
6290 else
Bram Moolenaar1950c352010-06-06 15:21:10 +02006291 {
6292 set_internal_string_var((char_u *)"b:current_syntax", old_value);
6293 vim_free(old_value);
6294 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006295}
6296
6297 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006298syntax_present(win_T *win)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006299{
6300 return (win->w_s->b_syn_patterns.ga_len != 0
6301 || win->w_s->b_syn_clusters.ga_len != 0
6302 || win->w_s->b_keywtab.ht_used > 0
6303 || win->w_s->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304}
6305
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
6307static enum
6308{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006309 EXP_SUBCMD, // expand ":syn" sub-commands
6310 EXP_CASE, // expand ":syn case" arguments
6311 EXP_SPELL, // expand ":syn spell" arguments
6312 EXP_SYNC // expand ":syn sync" arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313} expand_what;
6314
Bram Moolenaar4f688582007-07-24 12:34:30 +00006315/*
6316 * Reset include_link, include_default, include_none to 0.
6317 * Called when we are done expanding.
6318 */
6319 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006320reset_expand_highlight(void)
Bram Moolenaar4f688582007-07-24 12:34:30 +00006321{
6322 include_link = include_default = include_none = 0;
6323}
6324
6325/*
6326 * Handle command line completion for :match and :echohl command: Add "None"
6327 * as highlight group.
6328 */
6329 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006330set_context_in_echohl_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar4f688582007-07-24 12:34:30 +00006331{
6332 xp->xp_context = EXPAND_HIGHLIGHT;
6333 xp->xp_pattern = arg;
6334 include_none = 1;
6335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337/*
6338 * Handle command line completion for :syntax command.
6339 */
6340 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006341set_context_in_syntax_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 char_u *p;
6344
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006345 // Default: expand subcommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 xp->xp_context = EXPAND_SYNTAX;
6347 expand_what = EXP_SUBCMD;
6348 xp->xp_pattern = arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00006349 include_link = 0;
6350 include_default = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006352 // (part of) subcommand already typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 if (*arg != NUL)
6354 {
6355 p = skiptowhite(arg);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006356 if (*p != NUL) // past first word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358 xp->xp_pattern = skipwhite(p);
6359 if (*skiptowhite(xp->xp_pattern) != NUL)
6360 xp->xp_context = EXPAND_NOTHING;
6361 else if (STRNICMP(arg, "case", p - arg) == 0)
6362 expand_what = EXP_CASE;
Bram Moolenaar2d028392017-01-08 18:28:22 +01006363 else if (STRNICMP(arg, "spell", p - arg) == 0)
6364 expand_what = EXP_SPELL;
6365 else if (STRNICMP(arg, "sync", p - arg) == 0)
6366 expand_what = EXP_SYNC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 else if ( STRNICMP(arg, "keyword", p - arg) == 0
6368 || STRNICMP(arg, "region", p - arg) == 0
6369 || STRNICMP(arg, "match", p - arg) == 0
6370 || STRNICMP(arg, "list", p - arg) == 0)
6371 xp->xp_context = EXPAND_HIGHLIGHT;
6372 else
6373 xp->xp_context = EXPAND_NOTHING;
6374 }
6375 }
6376}
6377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378/*
6379 * Function given to ExpandGeneric() to obtain the list syntax names for
6380 * expansion.
6381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006383get_syntax_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
Bram Moolenaar2d028392017-01-08 18:28:22 +01006385 switch (expand_what)
6386 {
6387 case EXP_SUBCMD:
6388 return (char_u *)subcommands[idx].name;
6389 case EXP_CASE:
6390 {
6391 static char *case_args[] = {"match", "ignore", NULL};
6392 return (char_u *)case_args[idx];
6393 }
6394 case EXP_SPELL:
6395 {
6396 static char *spell_args[] =
6397 {"toplevel", "notoplevel", "default", NULL};
6398 return (char_u *)spell_args[idx];
6399 }
6400 case EXP_SYNC:
6401 {
6402 static char *sync_args[] =
6403 {"ccomment", "clear", "fromstart",
6404 "linebreaks=", "linecont", "lines=", "match",
6405 "maxlines=", "minlines=", "region", NULL};
6406 return (char_u *)sync_args[idx];
6407 }
6408 }
6409 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413/*
6414 * Function called for expression evaluation: get syntax ID at file position.
6415 */
6416 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006417syn_get_id(
6418 win_T *wp,
6419 long lnum,
6420 colnr_T col,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006421 int trans, // remove transparency
6422 int *spellp, // return: can do spell checking
6423 int keep_state) // keep state of char at "col"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006425 // When the position is not after the current position and in the same
6426 // line of the same buffer, need to restart parsing.
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006427 if (wp->w_buffer != syn_buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 || lnum != current_lnum
Bram Moolenaar9d0ec2e2005-04-20 19:45:58 +00006429 || col < current_col)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02006430 syntax_start(wp, lnum);
Bram Moolenaar6773a342016-01-19 20:52:44 +01006431 else if (wp->w_buffer == syn_buf
6432 && lnum == current_lnum
6433 && col > current_col)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006434 // next_match may not be correct when moving around, e.g. with the
6435 // "skip" expression in searchpair()
Bram Moolenaar6773a342016-01-19 20:52:44 +01006436 next_match_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006438 (void)get_syntax_attr(col, spellp, keep_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
6440 return (trans ? current_trans_id : current_id);
6441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
Bram Moolenaar860cae12010-06-05 23:22:07 +02006443#if defined(FEAT_CONCEAL) || defined(PROTO)
6444/*
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006445 * Get extra information about the syntax item. Must be called right after
6446 * get_syntax_attr().
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02006447 * Stores the current item sequence nr in "*seqnrp".
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006448 * Returns the current flags.
6449 */
6450 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006451get_syntax_info(int *seqnrp)
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006452{
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02006453 *seqnrp = current_seqnr;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006454 return current_flags;
6455}
6456
6457/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02006458 * Return conceal substitution character
6459 */
6460 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006461syn_get_sub_char(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006462{
6463 return current_sub_char;
6464}
6465#endif
6466
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006467#if defined(FEAT_EVAL) || defined(PROTO)
6468/*
6469 * Return the syntax ID at position "i" in the current stack.
6470 * The caller must have called syn_get_id() before to fill the stack.
6471 * Returns -1 when "i" is out of range.
6472 */
6473 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006474syn_get_stack_item(int i)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006475{
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006476 if (i >= current_state.ga_len)
6477 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006478 // Need to invalidate the state, because we didn't properly finish it
6479 // for the last character, "keep_state" was TRUE.
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006480 invalidate_current_state();
6481 current_col = MAXCOL;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006482 return -1;
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006483 }
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006484 return CUR_STATE(i).si_id;
6485}
6486#endif
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488#if defined(FEAT_FOLDING) || defined(PROTO)
6489/*
6490 * Function called to get folding level for line "lnum" in window "wp".
6491 */
6492 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006493syn_get_foldlevel(win_T *wp, long lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 int level = 0;
6496 int i;
6497
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006498 // Return quickly when there are no fold items at all.
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02006499 if (wp->w_s->b_syn_folditems != 0
6500 && !wp->w_s->b_syn_error
6501# ifdef SYN_TIME_LIMIT
6502 && !wp->w_s->b_syn_slow
6503# endif
6504 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02006506 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 for (i = 0; i < current_state.ga_len; ++i)
6509 if (CUR_STATE(i).si_flags & HL_FOLD)
6510 ++level;
6511 }
6512 if (level > wp->w_p_fdn)
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 level = wp->w_p_fdn;
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006515 if (level < 0)
6516 level = 0;
6517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 return level;
6519}
6520#endif
6521
Bram Moolenaar01615492015-02-03 13:00:38 +01006522#if defined(FEAT_PROFILE) || defined(PROTO)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006523/*
6524 * ":syntime".
6525 */
6526 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006527ex_syntime(exarg_T *eap)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006528{
6529 if (STRCMP(eap->arg, "on") == 0)
6530 syn_time_on = TRUE;
6531 else if (STRCMP(eap->arg, "off") == 0)
6532 syn_time_on = FALSE;
6533 else if (STRCMP(eap->arg, "clear") == 0)
6534 syntime_clear();
6535 else if (STRCMP(eap->arg, "report") == 0)
6536 syntime_report();
6537 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006538 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006539}
6540
6541 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006542syn_clear_time(syn_time_T *st)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006543{
6544 profile_zero(&st->total);
6545 profile_zero(&st->slowest);
6546 st->count = 0;
6547 st->match = 0;
6548}
6549
6550/*
6551 * Clear the syntax timing for the current buffer.
6552 */
6553 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006554syntime_clear(void)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006555{
6556 int idx;
6557 synpat_T *spp;
6558
6559 if (!syntax_present(curwin))
6560 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006561 msg(_(msg_no_items));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006562 return;
6563 }
6564 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx)
6565 {
6566 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
6567 syn_clear_time(&spp->sp_time);
6568 }
6569}
6570
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006571/*
6572 * Function given to ExpandGeneric() to obtain the possible arguments of the
6573 * ":syntime {on,off,clear,report}" command.
6574 */
6575 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006576get_syntime_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006577{
6578 switch (idx)
6579 {
6580 case 0: return (char_u *)"on";
6581 case 1: return (char_u *)"off";
6582 case 2: return (char_u *)"clear";
6583 case 3: return (char_u *)"report";
6584 }
6585 return NULL;
6586}
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006587
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006588typedef struct
6589{
6590 proftime_T total;
6591 int count;
6592 int match;
6593 proftime_T slowest;
6594 proftime_T average;
6595 int id;
6596 char_u *pattern;
6597} time_entry_T;
6598
6599 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006600syn_compare_syntime(const void *v1, const void *v2)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006601{
6602 const time_entry_T *s1 = v1;
6603 const time_entry_T *s2 = v2;
6604
6605 return profile_cmp(&s1->total, &s2->total);
6606}
6607
6608/*
6609 * Clear the syntax timing for the current buffer.
6610 */
6611 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006612syntime_report(void)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006613{
6614 int idx;
6615 synpat_T *spp;
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006616# ifdef FEAT_FLOAT
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006617 proftime_T tm;
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006618# endif
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006619 int len;
6620 proftime_T total_total;
6621 int total_count = 0;
6622 garray_T ga;
6623 time_entry_T *p;
6624
6625 if (!syntax_present(curwin))
6626 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006627 msg(_(msg_no_items));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006628 return;
6629 }
6630
6631 ga_init2(&ga, sizeof(time_entry_T), 50);
6632 profile_zero(&total_total);
6633 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx)
6634 {
6635 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
6636 if (spp->sp_time.count > 0)
6637 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02006638 (void)ga_grow(&ga, 1);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006639 p = ((time_entry_T *)ga.ga_data) + ga.ga_len;
6640 p->total = spp->sp_time.total;
6641 profile_add(&total_total, &spp->sp_time.total);
6642 p->count = spp->sp_time.count;
6643 p->match = spp->sp_time.match;
6644 total_count += spp->sp_time.count;
6645 p->slowest = spp->sp_time.slowest;
6646# ifdef FEAT_FLOAT
6647 profile_divide(&spp->sp_time.total, spp->sp_time.count, &tm);
6648 p->average = tm;
6649# endif
6650 p->id = spp->sp_syn.id;
6651 p->pattern = spp->sp_pattern;
6652 ++ga.ga_len;
6653 }
6654 }
6655
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006656 // Sort on total time. Skip if there are no items to avoid passing NULL
6657 // pointer to qsort().
Bram Moolenaara2162552017-01-08 17:46:20 +01006658 if (ga.ga_len > 1)
6659 qsort(ga.ga_data, (size_t)ga.ga_len, sizeof(time_entry_T),
Bram Moolenaar4e312962013-06-06 21:19:51 +02006660 syn_compare_syntime);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006661
Bram Moolenaar32526b32019-01-19 17:43:09 +01006662 msg_puts_title(_(" TOTAL COUNT MATCH SLOWEST AVERAGE NAME PATTERN"));
6663 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006664 for (idx = 0; idx < ga.ga_len && !got_int; ++idx)
6665 {
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006666 p = ((time_entry_T *)ga.ga_data) + idx;
6667
Bram Moolenaar32526b32019-01-19 17:43:09 +01006668 msg_puts(profile_msg(&p->total));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006669 msg_puts(" "); // make sure there is always a separating space
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006670 msg_advance(13);
6671 msg_outnum(p->count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006672 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006673 msg_advance(20);
6674 msg_outnum(p->match);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006675 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006676 msg_advance(26);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006677 msg_puts(profile_msg(&p->slowest));
6678 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006679 msg_advance(38);
6680# ifdef FEAT_FLOAT
Bram Moolenaar32526b32019-01-19 17:43:09 +01006681 msg_puts(profile_msg(&p->average));
6682 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006683# endif
6684 msg_advance(50);
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02006685 msg_outtrans(highlight_group_name(p->id - 1));
Bram Moolenaar32526b32019-01-19 17:43:09 +01006686 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006687
6688 msg_advance(69);
6689 if (Columns < 80)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006690 len = 20; // will wrap anyway
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006691 else
6692 len = Columns - 70;
6693 if (len > (int)STRLEN(p->pattern))
6694 len = (int)STRLEN(p->pattern);
6695 msg_outtrans_len(p->pattern, len);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006696 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006697 }
Bram Moolenaar45fc5392013-06-07 19:48:39 +02006698 ga_clear(&ga);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006699 if (!got_int)
6700 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006701 msg_puts("\n");
6702 msg_puts(profile_msg(&total_total));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006703 msg_advance(13);
6704 msg_outnum(total_count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006705 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006706 }
6707}
6708#endif
6709
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006710#endif // FEAT_SYN_HL