blob: 679b99d534772d4e278f33c2cd2e37f9c63b0ef1 [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
kylo252ae6f1d82022-02-16 19:24:07 +000084#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
kylo252ae6f1d82022-02-16 19:24:07 +0000210#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
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100212 // 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 Moolenaar0d6f5d92019-12-05 21:33:15 +0100269static linenr_T current_lnum = 0; // lnum of current state
270static colnr_T current_col = 0; // column of current state
271static int current_state_stored = 0; // TRUE if stored current state
272 // after setting current_finished
273static int current_finished = 0; // current line has been finished
274static garray_T current_state // current stack of state_items
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 = {0, 0, 0, 0, NULL};
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100276static short *current_next_list = NULL; // when non-zero, nextgroup list
277static int current_next_flags = 0; // flags for current_next_list
278static int current_line_id = 0; // unique number for current line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280#define CUR_STATE(idx) ((stateitem_T *)(current_state.ga_data))[idx]
281
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100282static void syn_sync(win_T *wp, linenr_T lnum, synstate_T *last_valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100283static int syn_match_linecont(linenr_T lnum);
284static void syn_start_line(void);
285static void syn_update_ends(int startofline);
286static void syn_stack_alloc(void);
287static int syn_stack_cleanup(void);
288static void syn_stack_free_entry(synblock_T *block, synstate_T *p);
289static synstate_T *syn_stack_find_entry(linenr_T lnum);
290static synstate_T *store_current_state(void);
291static void load_current_state(synstate_T *from);
292static void invalidate_current_state(void);
293static int syn_stack_equal(synstate_T *sp);
294static void validate_current_state(void);
295static int syn_finish_line(int syncing);
296static int syn_current_attr(int syncing, int displaying, int *can_spell, int keep_state);
297static int did_match_already(int idx, garray_T *gap);
298static stateitem_T *push_next_match(stateitem_T *cur_si);
299static void check_state_ends(void);
300static void update_si_attr(int idx);
301static void check_keepend(void);
302static void update_si_end(stateitem_T *sip, int startcol, int force);
303static short *copy_id_list(short *list);
304static int in_id_list(stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained);
305static int push_current_state(int idx);
306static void pop_current_state(void);
Bram Moolenaarf7512552013-06-06 14:55:19 +0200307#ifdef FEAT_PROFILE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100308static void syn_clear_time(syn_time_T *tt);
309static void syntime_clear(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100310static void syntime_report(void);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200311static int syn_time_on = FALSE;
312# define IF_SYN_TIME(p) (p)
313#else
314# define IF_SYN_TIME(p) NULL
315typedef int syn_time_T;
316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100318static void syn_stack_apply_changes_block(synblock_T *block, buf_T *buf);
319static 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 +0000320
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100321static void limit_pos(lpos_T *pos, lpos_T *limit);
322static void limit_pos_zero(lpos_T *pos, lpos_T *limit);
323static void syn_add_end_off(lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra);
324static void syn_add_start_off(lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra);
325static char_u *syn_getcurline(void);
326static int syn_regexec(regmmatch_T *rmp, linenr_T lnum, colnr_T col, syn_time_T *st);
327static 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 +0100328static void syn_remove_pattern(synblock_T *block, int idx);
329static void syn_clear_pattern(synblock_T *block, int i);
330static void syn_clear_cluster(synblock_T *block, int i);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100331static void syn_clear_one(int id, int syncing);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100332static void syn_cmd_onoff(exarg_T *eap, char *name);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100333static void syn_lines_msg(void);
334static void syn_match_msg(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100335static void syn_list_one(int id, int syncing, int link_only);
336static void syn_list_cluster(int id);
337static void put_id_list(char_u *name, short *list, int attr);
338static void put_pattern(char *s, int c, synpat_T *spp, int attr);
339static int syn_list_keywords(int id, hashtab_T *ht, int did_header, int attr);
340static void syn_clear_keyword(int id, hashtab_T *ht);
341static void clear_keywtab(hashtab_T *ht);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100342static int syn_scl_namen2id(char_u *linep, int len);
343static int syn_check_cluster(char_u *pp, int len);
344static int syn_add_cluster(char_u *name);
345static void init_syn_patterns(void);
346static char_u *get_syn_pattern(char_u *arg, synpat_T *ci);
Bram Moolenaarde318c52017-01-17 16:27:10 +0100347static int get_id_list(char_u **arg, int keylen, short **list, int skip);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100348static void syn_combine_list(short **clstr1, short **clstr2, int list_op);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
350/*
351 * Start the syntax recognition for a line. This function is normally called
352 * from the screen updating, once for each displayed line.
353 * The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
354 * it. Careful: curbuf and curwin are likely to point to another buffer and
355 * window.
356 */
357 void
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200358syntax_start(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
360 synstate_T *p;
361 synstate_T *last_valid = NULL;
362 synstate_T *last_min_valid = NULL;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000363 synstate_T *sp, *prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 linenr_T parsed_lnum;
365 linenr_T first_stored;
366 int dist;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100367 static varnumber_T changedtick = 0; // remember the last change ID
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200369#ifdef FEAT_CONCEAL
370 current_sub_char = NUL;
371#endif
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 /*
374 * After switching buffers, invalidate current_state.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000375 * Also do this when a change was made, the current state may be invalid
376 * then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 */
Bram Moolenaarb681be12016-03-31 23:02:16 +0200378 if (syn_block != wp->w_s
379 || syn_buf != wp->w_buffer
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100380 || changedtick != CHANGEDTICK(syn_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {
382 invalidate_current_state();
383 syn_buf = wp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200384 syn_block = wp->w_s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100386 changedtick = CHANGEDTICK(syn_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 syn_win = wp;
388
389 /*
390 * Allocate syntax stack when needed.
391 */
392 syn_stack_alloc();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200393 if (syn_block->b_sst_array == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100394 return; // out of memory
Bram Moolenaar860cae12010-06-05 23:22:07 +0200395 syn_block->b_sst_lasttick = display_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
397 /*
398 * If the state of the end of the previous line is useful, store it.
399 */
400 if (VALID_STATE(&current_state)
401 && current_lnum < lnum
402 && current_lnum < syn_buf->b_ml.ml_line_count)
403 {
404 (void)syn_finish_line(FALSE);
405 if (!current_state_stored)
406 {
407 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000408 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410
411 /*
412 * If the current_lnum is now the same as "lnum", keep the current
413 * state (this happens very often!). Otherwise invalidate
414 * current_state and figure it out below.
415 */
416 if (current_lnum != lnum)
417 invalidate_current_state();
418 }
419 else
420 invalidate_current_state();
421
422 /*
423 * Try to synchronize from a saved state in b_sst_array[].
424 * Only do this if lnum is not before and not to far beyond a saved state.
425 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200426 if (INVALID_STATE(&current_state) && syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100428 // Find last valid saved state before start_lnum.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200429 FOR_ALL_SYNSTATES(syn_block, p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {
431 if (p->sst_lnum > lnum)
432 break;
433 if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
434 {
435 last_valid = p;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200436 if (p->sst_lnum >= lnum - syn_block->b_syn_sync_minlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 last_min_valid = p;
438 }
439 }
440 if (last_min_valid != NULL)
441 load_current_state(last_min_valid);
442 }
443
444 /*
445 * If "lnum" is before or far beyond a line with a saved state, need to
446 * re-synchronize.
447 */
448 if (INVALID_STATE(&current_state))
449 {
450 syn_sync(wp, lnum, last_valid);
Bram Moolenaard6761c32011-06-19 04:54:21 +0200451 if (current_lnum == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100452 // First line is always valid, no matter "minlines".
Bram Moolenaard6761c32011-06-19 04:54:21 +0200453 first_stored = 1;
454 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100455 // Need to parse "minlines" lines before state can be considered
456 // valid to store.
Bram Moolenaard6761c32011-06-19 04:54:21 +0200457 first_stored = current_lnum + syn_block->b_syn_sync_minlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 }
459 else
460 first_stored = current_lnum;
461
462 /*
463 * Advance from the sync point or saved state until the current line.
464 * Save some entries for syncing with later on.
465 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200466 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000467 dist = 999999;
468 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200469 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 while (current_lnum < lnum)
471 {
472 syn_start_line();
473 (void)syn_finish_line(FALSE);
474 ++current_lnum;
475
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100476 // If we parsed at least "minlines" lines or started at a valid
477 // state, the current state is considered valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (current_lnum >= first_stored)
479 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 // Check if the saved state entry is for the current line and is
481 // equal to the current state. If so, then validate all saved
482 // states that depended on a change before the parsed line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (prev == NULL)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000484 prev = syn_stack_find_entry(current_lnum - 1);
485 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200486 sp = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 else
Bram Moolenaardbe31752008-01-13 16:40:19 +0000488 sp = prev;
489 while (sp != NULL && sp->sst_lnum < current_lnum)
490 sp = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 if (sp != NULL
492 && sp->sst_lnum == current_lnum
493 && syn_stack_equal(sp))
494 {
495 parsed_lnum = current_lnum;
496 prev = sp;
497 while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
498 {
499 if (sp->sst_lnum <= lnum)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100500 // valid state before desired line, use this one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 prev = sp;
502 else if (sp->sst_change_lnum == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // past saved states depending on change, break here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 break;
505 sp->sst_change_lnum = 0;
506 sp = sp->sst_next;
507 }
508 load_current_state(prev);
509 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100510 // Store the state at this line when it's the first one, the line
511 // where we start parsing, or some distance from the previously
512 // saved state. But only when parsed at least 'minlines'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 else if (prev == NULL
514 || current_lnum == lnum
515 || current_lnum >= prev->sst_lnum + dist)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000516 prev = store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100519 // This can take a long time: break when CTRL-C pressed. The current
520 // state will be wrong then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 line_breakcheck();
522 if (got_int)
523 {
524 current_lnum = lnum;
525 break;
526 }
527 }
528
529 syn_start_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530}
531
532/*
533 * We cannot simply discard growarrays full of state_items or buf_states; we
534 * have to manually release their extmatch pointers first.
535 */
536 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100537clear_syn_state(synstate_T *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539 int i;
540 garray_T *gap;
541
542 if (p->sst_stacksize > SST_FIX_STATES)
543 {
544 gap = &(p->sst_union.sst_ga);
545 for (i = 0; i < gap->ga_len; i++)
546 unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
547 ga_clear(gap);
548 }
549 else
550 {
551 for (i = 0; i < p->sst_stacksize; i++)
552 unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
553 }
554}
555
556/*
557 * Cleanup the current_state stack.
558 */
559 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100560clear_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
562 int i;
563 stateitem_T *sip;
564
565 sip = (stateitem_T *)(current_state.ga_data);
566 for (i = 0; i < current_state.ga_len; i++)
567 unref_extmatch(sip[i].si_extmatch);
568 ga_clear(&current_state);
569}
570
571/*
572 * Try to find a synchronisation point for line "lnum".
573 *
574 * This sets current_lnum and the current state. One of three methods is
575 * used:
576 * 1. Search backwards for the end of a C-comment.
577 * 2. Search backwards for given sync patterns.
578 * 3. Simply start on a given number of lines above "lnum".
579 */
580 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100581syn_sync(
582 win_T *wp,
583 linenr_T start_lnum,
584 synstate_T *last_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 buf_T *curbuf_save;
587 win_T *curwin_save;
588 pos_T cursor_save;
589 int idx;
590 linenr_T lnum;
591 linenr_T end_lnum;
592 linenr_T break_lnum;
593 int had_sync_point;
594 stateitem_T *cur_si;
595 synpat_T *spp;
596 char_u *line;
597 int found_flags = 0;
598 int found_match_idx = 0;
599 linenr_T found_current_lnum = 0;
600 int found_current_col= 0;
601 lpos_T found_m_endpos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000602 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
604 /*
605 * Clear any current state that might be hanging around.
606 */
607 invalidate_current_state();
608
609 /*
610 * Start at least "minlines" back. Default starting point for parsing is
611 * there.
612 * Start further back, to avoid that scrolling backwards will result in
613 * resyncing for every line. Now it resyncs only one out of N lines,
614 * where N is minlines * 1.5, or minlines * 2 if minlines is small.
615 * Watch out for overflow when minlines is MAXLNUM.
616 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200617 if (syn_block->b_syn_sync_minlines > start_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 start_lnum = 1;
619 else
620 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200621 if (syn_block->b_syn_sync_minlines == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 lnum = 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200623 else if (syn_block->b_syn_sync_minlines < 10)
624 lnum = syn_block->b_syn_sync_minlines * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200626 lnum = syn_block->b_syn_sync_minlines * 3 / 2;
627 if (syn_block->b_syn_sync_maxlines != 0
628 && lnum > syn_block->b_syn_sync_maxlines)
629 lnum = syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (lnum >= start_lnum)
631 start_lnum = 1;
632 else
633 start_lnum -= lnum;
634 }
635 current_lnum = start_lnum;
636
637 /*
638 * 1. Search backwards for the end of a C-style comment.
639 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 if (syn_block->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100642 // Need to make syn_buf the current buffer for a moment, to be able to
643 // use find_start_comment().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 curwin_save = curwin;
645 curwin = wp;
646 curbuf_save = curbuf;
647 curbuf = syn_buf;
648
649 /*
650 * Skip lines that end in a backslash.
651 */
652 for ( ; start_lnum > 1; --start_lnum)
653 {
654 line = ml_get(start_lnum - 1);
655 if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
656 break;
657 }
658 current_lnum = start_lnum;
659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100660 // set cursor to start of search
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 cursor_save = wp->w_cursor;
662 wp->w_cursor.lnum = start_lnum;
663 wp->w_cursor.col = 0;
664
665 /*
666 * If the line is inside a comment, need to find the syntax item that
667 * defines the comment.
668 * Restrict the search for the end of a comment to b_syn_sync_maxlines.
669 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200670 if (find_start_comment((int)syn_block->b_syn_sync_maxlines) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200672 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
673 if (SYN_ITEMS(syn_block)[idx].sp_syn.id
674 == syn_block->b_syn_sync_id
675 && SYN_ITEMS(syn_block)[idx].sp_type == SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
677 validate_current_state();
678 if (push_current_state(idx) == OK)
679 update_si_attr(current_state.ga_len - 1);
680 break;
681 }
682 }
683
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100684 // restore cursor and buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 wp->w_cursor = cursor_save;
686 curwin = curwin_save;
687 curbuf = curbuf_save;
688 }
689
690 /*
691 * 2. Search backwards for given sync patterns.
692 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200693 else if (syn_block->b_syn_sync_flags & SF_MATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200695 if (syn_block->b_syn_sync_maxlines != 0
696 && start_lnum > syn_block->b_syn_sync_maxlines)
697 break_lnum = start_lnum - syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 else
699 break_lnum = 0;
700
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000701 found_m_endpos.lnum = 0;
702 found_m_endpos.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 end_lnum = start_lnum;
704 lnum = start_lnum;
705 while (--lnum > break_lnum)
706 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100707 // This can take a long time: break when CTRL-C pressed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 line_breakcheck();
709 if (got_int)
710 {
711 invalidate_current_state();
712 current_lnum = start_lnum;
713 break;
714 }
715
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100716 // Check if we have run into a valid saved state stack now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (last_valid != NULL && lnum == last_valid->sst_lnum)
718 {
719 load_current_state(last_valid);
720 break;
721 }
722
723 /*
724 * Check if the previous line has the line-continuation pattern.
725 */
726 if (lnum > 1 && syn_match_linecont(lnum - 1))
727 continue;
728
729 /*
730 * Start with nothing on the state stack
731 */
732 validate_current_state();
733
734 for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
735 {
736 syn_start_line();
737 for (;;)
738 {
739 had_sync_point = syn_finish_line(TRUE);
740 /*
741 * When a sync point has been found, remember where, and
742 * continue to look for another one, further on in the line.
743 */
744 if (had_sync_point && current_state.ga_len)
745 {
746 cur_si = &CUR_STATE(current_state.ga_len - 1);
747 if (cur_si->si_m_endpos.lnum > start_lnum)
748 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100749 // ignore match that goes to after where started
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 current_lnum = end_lnum;
751 break;
752 }
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000753 if (cur_si->si_idx < 0)
754 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100755 // Cannot happen?
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000756 found_flags = 0;
757 found_match_idx = KEYWORD_IDX;
758 }
759 else
760 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200761 spp = &(SYN_ITEMS(syn_block)[cur_si->si_idx]);
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000762 found_flags = spp->sp_flags;
763 found_match_idx = spp->sp_sync_idx;
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 found_current_lnum = current_lnum;
766 found_current_col = current_col;
767 found_m_endpos = cur_si->si_m_endpos;
768 /*
769 * Continue after the match (be aware of a zero-length
770 * match).
771 */
772 if (found_m_endpos.lnum > current_lnum)
773 {
774 current_lnum = found_m_endpos.lnum;
775 current_col = found_m_endpos.col;
776 if (current_lnum >= end_lnum)
777 break;
778 }
779 else if (found_m_endpos.col > current_col)
780 current_col = found_m_endpos.col;
781 else
782 ++current_col;
783
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100784 // syn_current_attr() will have skipped the check for
785 // an item that ends here, need to do that now. Be
786 // careful not to go past the NUL.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000787 prev_current_col = current_col;
788 if (syn_getcurline()[current_col] != NUL)
789 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +0000791 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 else
794 break;
795 }
796 }
797
798 /*
799 * If a sync point was encountered, break here.
800 */
801 if (found_flags)
802 {
803 /*
804 * Put the item that was specified by the sync point on the
805 * state stack. If there was no item specified, make the
806 * state stack empty.
807 */
808 clear_current_state();
809 if (found_match_idx >= 0
810 && push_current_state(found_match_idx) == OK)
811 update_si_attr(current_state.ga_len - 1);
812
813 /*
814 * When using "grouphere", continue from the sync point
815 * match, until the end of the line. Parsing starts at
816 * the next line.
817 * For "groupthere" the parsing starts at start_lnum.
818 */
819 if (found_flags & HL_SYNC_HERE)
820 {
821 if (current_state.ga_len)
822 {
823 cur_si = &CUR_STATE(current_state.ga_len - 1);
824 cur_si->si_h_startpos.lnum = found_current_lnum;
825 cur_si->si_h_startpos.col = found_current_col;
826 update_si_end(cur_si, (int)current_col, TRUE);
827 check_keepend();
828 }
829 current_col = found_m_endpos.col;
830 current_lnum = found_m_endpos.lnum;
831 (void)syn_finish_line(FALSE);
832 ++current_lnum;
833 }
834 else
835 current_lnum = start_lnum;
836
837 break;
838 }
839
840 end_lnum = lnum;
841 invalidate_current_state();
842 }
843
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100844 // Ran into start of the file or exceeded maximum number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (lnum <= break_lnum)
846 {
847 invalidate_current_state();
848 current_lnum = break_lnum + 1;
849 }
850 }
851
852 validate_current_state();
853}
854
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100855 static void
856save_chartab(char_u *chartab)
857{
858 if (syn_block->b_syn_isk != empty_option)
859 {
860 mch_memmove(chartab, syn_buf->b_chartab, (size_t)32);
861 mch_memmove(syn_buf->b_chartab, syn_win->w_s->b_syn_chartab,
862 (size_t)32);
863 }
864}
865
866 static void
867restore_chartab(char_u *chartab)
868{
869 if (syn_win->w_s->b_syn_isk != empty_option)
870 mch_memmove(syn_buf->b_chartab, chartab, (size_t)32);
871}
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873/*
874 * Return TRUE if the line-continuation pattern matches in line "lnum".
875 */
876 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100877syn_match_linecont(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 regmmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100880 int r;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100881 char_u buf_chartab[32]; // chartab array for syn iskyeyword
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883 if (syn_block->b_syn_linecont_prog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100885 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100886 save_chartab(buf_chartab);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200887 regmatch.rmm_ic = syn_block->b_syn_linecont_ic;
888 regmatch.regprog = syn_block->b_syn_linecont_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100889 r = syn_regexec(&regmatch, lnum, (colnr_T)0,
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200890 IF_SYN_TIME(&syn_block->b_syn_linecont_time));
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100891 syn_block->b_syn_linecont_prog = regmatch.regprog;
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100892 restore_chartab(buf_chartab);
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100893 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895 return FALSE;
896}
897
898/*
899 * Prepare the current state for the start of a line.
900 */
901 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100902syn_start_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
904 current_finished = FALSE;
905 current_col = 0;
906
907 /*
908 * Need to update the end of a start/skip/end that continues from the
909 * previous line and regions that have "keepend".
910 */
911 if (current_state.ga_len > 0)
Bram Moolenaar6fa46362011-05-25 17:56:27 +0200912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 syn_update_ends(TRUE);
Bram Moolenaar6fa46362011-05-25 17:56:27 +0200914 check_state_ends();
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 next_match_idx = -1;
918 ++current_line_id;
Bram Moolenaarea20de82017-06-24 22:52:24 +0200919#ifdef FEAT_CONCEAL
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200920 next_seqnr = 1;
Bram Moolenaarea20de82017-06-24 22:52:24 +0200921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922}
923
924/*
925 * Check for items in the stack that need their end updated.
926 * When "startofline" is TRUE the last item is always updated.
927 * When "startofline" is FALSE the item with "keepend" is forcefully updated.
928 */
929 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100930syn_update_ends(int startofline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 stateitem_T *cur_si;
933 int i;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000934 int seen_keepend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
936 if (startofline)
937 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100938 // Check for a match carried over from a previous line with a
939 // contained region. The match ends as soon as the region ends.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 for (i = 0; i < current_state.ga_len; ++i)
941 {
942 cur_si = &CUR_STATE(i);
943 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +0200944 && (SYN_ITEMS(syn_block)[cur_si->si_idx]).sp_type
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 == SPTYPE_MATCH
946 && cur_si->si_m_endpos.lnum < current_lnum)
947 {
948 cur_si->si_flags |= HL_MATCHCONT;
949 cur_si->si_m_endpos.lnum = 0;
950 cur_si->si_m_endpos.col = 0;
951 cur_si->si_h_endpos = cur_si->si_m_endpos;
952 cur_si->si_ends = TRUE;
953 }
954 }
955 }
956
957 /*
958 * Need to update the end of a start/skip/end that continues from the
959 * previous line. And regions that have "keepend", because they may
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000960 * influence contained items. If we've just removed "extend"
961 * (startofline == 0) then we should update ends of normal regions
962 * contained inside "keepend" because "extend" could have extended
963 * these "keepend" regions as well as contained normal regions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 * Then check for items ending in column 0.
965 */
966 i = current_state.ga_len - 1;
967 if (keepend_level >= 0)
968 for ( ; i > keepend_level; --i)
969 if (CUR_STATE(i).si_flags & HL_EXTEND)
970 break;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000971
972 seen_keepend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 for ( ; i < current_state.ga_len; ++i)
974 {
975 cur_si = &CUR_STATE(i);
976 if ((cur_si->si_flags & HL_KEEPEND)
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000977 || (seen_keepend && !startofline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 || (i == current_state.ga_len - 1 && startofline))
979 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100980 cur_si->si_h_startpos.col = 0; // start highl. in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 cur_si->si_h_startpos.lnum = current_lnum;
982
983 if (!(cur_si->si_flags & HL_MATCHCONT))
984 update_si_end(cur_si, (int)current_col, !startofline);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000985
986 if (!startofline && (cur_si->si_flags & HL_KEEPEND))
987 seen_keepend = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989 }
990 check_keepend();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100993/////////////////////////////////////////
994// Handling of the state stack cache.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996/*
997 * EXPLANATION OF THE SYNTAX STATE STACK CACHE
998 *
999 * To speed up syntax highlighting, the state stack for the start of some
1000 * lines is cached. These entries can be used to start parsing at that point.
1001 *
1002 * The stack is kept in b_sst_array[] for each buffer. There is a list of
1003 * valid entries. b_sst_first points to the first one, then follow sst_next.
1004 * The entries are sorted on line number. The first entry is often for line 2
1005 * (line 1 always starts with an empty stack).
1006 * There is also a list for free entries. This construction is used to avoid
1007 * having to allocate and free memory blocks too often.
1008 *
1009 * When making changes to the buffer, this is logged in b_mod_*. When calling
1010 * update_screen() to update the display, it will call
1011 * syn_stack_apply_changes() for each displayed buffer to adjust the cached
1012 * entries. The entries which are inside the changed area are removed,
1013 * because they must be recomputed. Entries below the changed have their line
1014 * number adjusted for deleted/inserted lines, and have their sst_change_lnum
1015 * set to indicate that a check must be made if the changed lines would change
1016 * the cached entry.
1017 *
1018 * When later displaying lines, an entry is stored for each line. Displayed
1019 * lines are likely to be displayed again, in which case the state at the
1020 * start of the line is needed.
1021 * For not displayed lines, an entry is stored for every so many lines. These
1022 * entries will be used e.g., when scrolling backwards. The distance between
1023 * entries depends on the number of lines in the buffer. For small buffers
1024 * the distance is fixed at SST_DIST, for large buffers there is a fixed
1025 * number of entries SST_MAX_ENTRIES, and the distance is computed.
1026 */
1027
Bram Moolenaar860cae12010-06-05 23:22:07 +02001028 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001029syn_stack_free_block(synblock_T *block)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001030{
1031 synstate_T *p;
1032
1033 if (block->b_sst_array != NULL)
1034 {
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001035 FOR_ALL_SYNSTATES(block, p)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001036 clear_syn_state(p);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001037 VIM_CLEAR(block->b_sst_array);
Bram Moolenaar95892c22018-09-28 22:26:54 +02001038 block->b_sst_first = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001039 block->b_sst_len = 0;
1040 }
1041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042/*
1043 * Free b_sst_array[] for buffer "buf".
1044 * Used when syntax items changed to force resyncing everywhere.
1045 */
1046 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001047syn_stack_free_all(synblock_T *block)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaara6c07602017-03-05 21:18:27 +01001049#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 win_T *wp;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar860cae12010-06-05 23:22:07 +02001053 syn_stack_free_block(block);
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#ifdef FEAT_FOLDING
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001056 // When using "syntax" fold method, must update all folds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 FOR_ALL_WINDOWS(wp)
1058 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001059 if (wp->w_s == block && foldmethodIsSyntax(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 foldUpdateAll(wp);
1061 }
1062#endif
1063}
1064
1065/*
1066 * Allocate the syntax state stack for syn_buf when needed.
1067 * If the number of entries in b_sst_array[] is much too big or a bit too
1068 * small, reallocate it.
1069 * Also used to allocate b_sst_array[] for the first time.
1070 */
1071 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001072syn_stack_alloc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073{
1074 long len;
1075 synstate_T *to, *from;
1076 synstate_T *sstp;
1077
1078 len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
1079 if (len < SST_MIN_ENTRIES)
1080 len = SST_MIN_ENTRIES;
1081 else if (len > SST_MAX_ENTRIES)
1082 len = SST_MAX_ENTRIES;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001083 if (syn_block->b_sst_len > len * 2 || syn_block->b_sst_len < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001085 // Allocate 50% too much, to avoid reallocating too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 len = syn_buf->b_ml.ml_line_count;
1087 len = (len + len / 2) / SST_DIST + Rows * 2;
1088 if (len < SST_MIN_ENTRIES)
1089 len = SST_MIN_ENTRIES;
1090 else if (len > SST_MAX_ENTRIES)
1091 len = SST_MAX_ENTRIES;
1092
Bram Moolenaar860cae12010-06-05 23:22:07 +02001093 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001095 // When shrinking the array, cleanup the existing stack.
1096 // Make sure that all valid entries fit in the new array.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001097 while (syn_block->b_sst_len - syn_block->b_sst_freecount + 2 > len
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 && syn_stack_cleanup())
1099 ;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001100 if (len < syn_block->b_sst_len - syn_block->b_sst_freecount + 2)
1101 len = syn_block->b_sst_len - syn_block->b_sst_freecount + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001104 sstp = ALLOC_CLEAR_MULT(synstate_T, len);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001105 if (sstp == NULL) // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 return;
1107
1108 to = sstp - 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001109 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001111 // Move the states from the old array to the new one.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001112 for (from = syn_block->b_sst_first; from != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 from = from->sst_next)
1114 {
1115 ++to;
1116 *to = *from;
1117 to->sst_next = to + 1;
1118 }
1119 }
1120 if (to != sstp - 1)
1121 {
1122 to->sst_next = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001123 syn_block->b_sst_first = sstp;
1124 syn_block->b_sst_freecount = len - (int)(to - sstp) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 else
1127 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001128 syn_block->b_sst_first = NULL;
1129 syn_block->b_sst_freecount = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001132 // Create the list of free entries.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001133 syn_block->b_sst_firstfree = to + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 while (++to < sstp + len)
1135 to->sst_next = to + 1;
1136 (sstp + len - 1)->sst_next = NULL;
1137
Bram Moolenaar860cae12010-06-05 23:22:07 +02001138 vim_free(syn_block->b_sst_array);
1139 syn_block->b_sst_array = sstp;
1140 syn_block->b_sst_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 }
1142}
1143
1144/*
1145 * Check for changes in a buffer to affect stored syntax states. Uses the
1146 * b_mod_* fields.
1147 * Called from update_screen(), before screen is being updated, once for each
1148 * displayed buffer.
1149 */
1150 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001151syn_stack_apply_changes(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001153 win_T *wp;
1154
1155 syn_stack_apply_changes_block(&buf->b_s, buf);
1156
1157 FOR_ALL_WINDOWS(wp)
1158 {
1159 if ((wp->w_buffer == buf) && (wp->w_s != &buf->b_s))
1160 syn_stack_apply_changes_block(wp->w_s, buf);
1161 }
1162}
1163
1164 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001165syn_stack_apply_changes_block(synblock_T *block, buf_T *buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 synstate_T *p, *prev, *np;
1168 linenr_T n;
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001171 for (p = block->b_sst_first; p != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001173 if (p->sst_lnum + block->b_syn_sync_linebreaks > buf->b_mod_top)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
1175 n = p->sst_lnum + buf->b_mod_xlines;
1176 if (n <= buf->b_mod_bot)
1177 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001178 // this state is inside the changed area, remove it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 np = p->sst_next;
1180 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001181 block->b_sst_first = np;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 else
1183 prev->sst_next = np;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001184 syn_stack_free_entry(block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 p = np;
1186 continue;
1187 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001188 // This state is below the changed area. Remember the line
1189 // that needs to be parsed before this entry can be made valid
1190 // again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
1192 {
1193 if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
1194 p->sst_change_lnum += buf->b_mod_xlines;
1195 else
1196 p->sst_change_lnum = buf->b_mod_top;
1197 }
1198 if (p->sst_change_lnum == 0
1199 || p->sst_change_lnum < buf->b_mod_bot)
1200 p->sst_change_lnum = buf->b_mod_bot;
1201
1202 p->sst_lnum = n;
1203 }
1204 prev = p;
1205 p = p->sst_next;
1206 }
1207}
1208
1209/*
1210 * Reduce the number of entries in the state stack for syn_buf.
1211 * Returns TRUE if at least one entry was freed.
1212 */
1213 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001214syn_stack_cleanup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 synstate_T *p, *prev;
1217 disptick_T tick;
1218 int above;
1219 int dist;
1220 int retval = FALSE;
1221
Bram Moolenaar95892c22018-09-28 22:26:54 +02001222 if (syn_block->b_sst_first == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 return retval;
1224
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001225 // Compute normal distance between non-displayed entries.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001226 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001227 dist = 999999;
1228 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02001229 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231 /*
Bram Moolenaarf5b63862009-12-16 17:13:44 +00001232 * Go through the list to find the "tick" for the oldest entry that can
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 * be removed. Set "above" when the "tick" for the oldest entry is above
1234 * "b_sst_lasttick" (the display tick wraps around).
1235 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001236 tick = syn_block->b_sst_lasttick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 above = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001238 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1240 {
1241 if (prev->sst_lnum + dist > p->sst_lnum)
1242 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001243 if (p->sst_tick > syn_block->b_sst_lasttick)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {
1245 if (!above || p->sst_tick < tick)
1246 tick = p->sst_tick;
1247 above = TRUE;
1248 }
1249 else if (!above && p->sst_tick < tick)
1250 tick = p->sst_tick;
1251 }
1252 }
1253
1254 /*
1255 * Go through the list to make the entries for the oldest tick at an
1256 * interval of several lines.
1257 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001258 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1260 {
1261 if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
1262 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001263 // Move this entry from used list to free list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 prev->sst_next = p->sst_next;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001265 syn_stack_free_entry(syn_block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 p = prev;
1267 retval = TRUE;
1268 }
1269 }
1270 return retval;
1271}
1272
1273/*
1274 * Free the allocated memory for a syn_state item.
1275 * Move the entry into the free list.
1276 */
1277 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001278syn_stack_free_entry(synblock_T *block, synstate_T *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279{
1280 clear_syn_state(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001281 p->sst_next = block->b_sst_firstfree;
1282 block->b_sst_firstfree = p;
1283 ++block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284}
1285
1286/*
1287 * Find an entry in the list of state stacks at or before "lnum".
1288 * Returns NULL when there is no entry or the first entry is after "lnum".
1289 */
1290 static synstate_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001291syn_stack_find_entry(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 synstate_T *p, *prev;
1294
1295 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001296 for (p = syn_block->b_sst_first; p != NULL; prev = p, p = p->sst_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {
1298 if (p->sst_lnum == lnum)
1299 return p;
1300 if (p->sst_lnum > lnum)
1301 break;
1302 }
1303 return prev;
1304}
1305
1306/*
1307 * Try saving the current state in b_sst_array[].
1308 * The current state must be valid for the start of the current_lnum line!
1309 */
1310 static synstate_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001311store_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 int i;
1314 synstate_T *p;
1315 bufstate_T *bp;
1316 stateitem_T *cur_si;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001317 synstate_T *sp = syn_stack_find_entry(current_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319 /*
1320 * If the current state contains a start or end pattern that continues
1321 * from the previous line, we can't use it. Don't store it then.
1322 */
1323 for (i = current_state.ga_len - 1; i >= 0; --i)
1324 {
1325 cur_si = &CUR_STATE(i);
1326 if (cur_si->si_h_startpos.lnum >= current_lnum
1327 || cur_si->si_m_endpos.lnum >= current_lnum
1328 || cur_si->si_h_endpos.lnum >= current_lnum
1329 || (cur_si->si_end_idx
1330 && cur_si->si_eoe_pos.lnum >= current_lnum))
1331 break;
1332 }
1333 if (i >= 0)
1334 {
1335 if (sp != NULL)
1336 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001337 // find "sp" in the list and remove it
Bram Moolenaar860cae12010-06-05 23:22:07 +02001338 if (syn_block->b_sst_first == sp)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001339 // it's the first entry
Bram Moolenaar860cae12010-06-05 23:22:07 +02001340 syn_block->b_sst_first = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 else
1342 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001343 // find the entry just before this one to adjust sst_next
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001344 FOR_ALL_SYNSTATES(syn_block, p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (p->sst_next == sp)
1346 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001347 if (p != NULL) // just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001348 p->sst_next = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001350 syn_stack_free_entry(syn_block, sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 sp = NULL;
1352 }
1353 }
1354 else if (sp == NULL || sp->sst_lnum != current_lnum)
1355 {
1356 /*
1357 * Add a new entry
1358 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001359 // If no free items, cleanup the array first.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001360 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
1362 (void)syn_stack_cleanup();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001363 // "sp" may have been moved to the freelist now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 sp = syn_stack_find_entry(current_lnum);
1365 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001366 // Still no free items? Must be a strange problem...
Bram Moolenaar860cae12010-06-05 23:22:07 +02001367 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 sp = NULL;
1369 else
1370 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001371 // Take the first item from the free list and put it in the used
1372 // list, after *sp
Bram Moolenaar860cae12010-06-05 23:22:07 +02001373 p = syn_block->b_sst_firstfree;
1374 syn_block->b_sst_firstfree = p->sst_next;
1375 --syn_block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (sp == NULL)
1377 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001378 // Insert in front of the list
Bram Moolenaar860cae12010-06-05 23:22:07 +02001379 p->sst_next = syn_block->b_sst_first;
1380 syn_block->b_sst_first = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 else
1383 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001384 // insert in list after *sp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 p->sst_next = sp->sst_next;
1386 sp->sst_next = p;
1387 }
1388 sp = p;
1389 sp->sst_stacksize = 0;
1390 sp->sst_lnum = current_lnum;
1391 }
1392 }
1393 if (sp != NULL)
1394 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001395 // When overwriting an existing state stack, clear it first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 clear_syn_state(sp);
1397 sp->sst_stacksize = current_state.ga_len;
1398 if (current_state.ga_len > SST_FIX_STATES)
1399 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001400 // Need to clear it, might be something remaining from when the
1401 // length was less than SST_FIX_STATES.
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001402 ga_init2(&sp->sst_union.sst_ga, sizeof(bufstate_T), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
1404 sp->sst_stacksize = 0;
1405 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 sp->sst_union.sst_ga.ga_len = current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1408 }
1409 else
1410 bp = sp->sst_union.sst_stack;
1411 for (i = 0; i < sp->sst_stacksize; ++i)
1412 {
1413 bp[i].bs_idx = CUR_STATE(i).si_idx;
1414 bp[i].bs_flags = CUR_STATE(i).si_flags;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001415#ifdef FEAT_CONCEAL
1416 bp[i].bs_seqnr = CUR_STATE(i).si_seqnr;
1417 bp[i].bs_cchar = CUR_STATE(i).si_cchar;
1418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
1420 }
1421 sp->sst_next_flags = current_next_flags;
1422 sp->sst_next_list = current_next_list;
1423 sp->sst_tick = display_tick;
1424 sp->sst_change_lnum = 0;
1425 }
1426 current_state_stored = TRUE;
1427 return sp;
1428}
1429
1430/*
1431 * Copy a state stack from "from" in b_sst_array[] to current_state;
1432 */
1433 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001434load_current_state(synstate_T *from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
1436 int i;
1437 bufstate_T *bp;
1438
1439 clear_current_state();
1440 validate_current_state();
1441 keepend_level = -1;
1442 if (from->sst_stacksize
1443 && ga_grow(&current_state, from->sst_stacksize) != FAIL)
1444 {
1445 if (from->sst_stacksize > SST_FIX_STATES)
1446 bp = SYN_STATE_P(&(from->sst_union.sst_ga));
1447 else
1448 bp = from->sst_union.sst_stack;
1449 for (i = 0; i < from->sst_stacksize; ++i)
1450 {
1451 CUR_STATE(i).si_idx = bp[i].bs_idx;
1452 CUR_STATE(i).si_flags = bp[i].bs_flags;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001453#ifdef FEAT_CONCEAL
1454 CUR_STATE(i).si_seqnr = bp[i].bs_seqnr;
1455 CUR_STATE(i).si_cchar = bp[i].bs_cchar;
1456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
1458 if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
1459 keepend_level = i;
1460 CUR_STATE(i).si_ends = FALSE;
1461 CUR_STATE(i).si_m_lnum = 0;
1462 if (CUR_STATE(i).si_idx >= 0)
1463 CUR_STATE(i).si_next_list =
Bram Moolenaar860cae12010-06-05 23:22:07 +02001464 (SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 else
1466 CUR_STATE(i).si_next_list = NULL;
1467 update_si_attr(i);
1468 }
1469 current_state.ga_len = from->sst_stacksize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471 current_next_list = from->sst_next_list;
1472 current_next_flags = from->sst_next_flags;
1473 current_lnum = from->sst_lnum;
1474}
1475
1476/*
1477 * Compare saved state stack "*sp" with the current state.
1478 * Return TRUE when they are equal.
1479 */
1480 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001481syn_stack_equal(synstate_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 int i, j;
1484 bufstate_T *bp;
1485 reg_extmatch_T *six, *bsx;
1486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001487 // First a quick check if the stacks have the same size end nextlist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (sp->sst_stacksize == current_state.ga_len
1489 && sp->sst_next_list == current_next_list)
1490 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001491 // Need to compare all states on both stacks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (sp->sst_stacksize > SST_FIX_STATES)
1493 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1494 else
1495 bp = sp->sst_union.sst_stack;
1496
1497 for (i = current_state.ga_len; --i >= 0; )
1498 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001499 // If the item has another index the state is different.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (bp[i].bs_idx != CUR_STATE(i).si_idx)
1501 break;
1502 if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
1503 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001504 // When the extmatch pointers are different, the strings in
1505 // them can still be the same. Check if the extmatch
1506 // references are equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 bsx = bp[i].bs_extmatch;
1508 six = CUR_STATE(i).si_extmatch;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001509 // If one of the extmatch pointers is NULL the states are
1510 // different.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (bsx == NULL || six == NULL)
1512 break;
1513 for (j = 0; j < NSUBEXP; ++j)
1514 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001515 // Check each referenced match string. They must all be
1516 // equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 if (bsx->matches[j] != six->matches[j])
1518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001519 // If the pointer is different it can still be the
1520 // same text. Compare the strings, ignore case when
1521 // the start item has the sp_ic flag set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (bsx->matches[j] == NULL
1523 || six->matches[j] == NULL)
1524 break;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001525 if ((SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 ? MB_STRICMP(bsx->matches[j],
1527 six->matches[j]) != 0
1528 : STRCMP(bsx->matches[j], six->matches[j]) != 0)
1529 break;
1530 }
1531 }
1532 if (j != NSUBEXP)
1533 break;
1534 }
1535 }
1536 if (i < 0)
1537 return TRUE;
1538 }
1539 return FALSE;
1540}
1541
1542/*
1543 * We stop parsing syntax above line "lnum". If the stored state at or below
1544 * this line depended on a change before it, it now depends on the line below
1545 * the last parsed line.
1546 * The window looks like this:
1547 * line which changed
1548 * displayed line
1549 * displayed line
1550 * lnum -> line below window
1551 */
1552 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001553syntax_end_parsing(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 synstate_T *sp;
1556
1557 sp = syn_stack_find_entry(lnum);
1558 if (sp != NULL && sp->sst_lnum < lnum)
1559 sp = sp->sst_next;
1560
1561 if (sp != NULL && sp->sst_change_lnum != 0)
1562 sp->sst_change_lnum = lnum;
1563}
1564
1565/*
1566 * End of handling of the state stack.
1567 ****************************************/
1568
1569 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001570invalidate_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 clear_current_state();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001573 current_state.ga_itemsize = 0; // mark current_state invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 current_next_list = NULL;
1575 keepend_level = -1;
1576}
1577
1578 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001579validate_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 current_state.ga_itemsize = sizeof(stateitem_T);
1582 current_state.ga_growsize = 3;
1583}
1584
1585/*
1586 * Return TRUE if the syntax at start of lnum changed since last time.
1587 * This will only be called just after get_syntax_attr() for the previous
1588 * line, to check if the next line needs to be redrawn too.
1589 */
1590 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001591syntax_check_changed(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 int retval = TRUE;
1594 synstate_T *sp;
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /*
1597 * Check the state stack when:
1598 * - lnum is just below the previously syntaxed line.
1599 * - lnum is not before the lines with saved states.
1600 * - lnum is not past the lines with saved states.
1601 * - lnum is at or before the last changed line.
1602 */
1603 if (VALID_STATE(&current_state) && lnum == current_lnum + 1)
1604 {
1605 sp = syn_stack_find_entry(lnum);
1606 if (sp != NULL && sp->sst_lnum == lnum)
1607 {
1608 /*
1609 * finish the previous line (needed when not all of the line was
1610 * drawn)
1611 */
1612 (void)syn_finish_line(FALSE);
1613
1614 /*
1615 * Compare the current state with the previously saved state of
1616 * the line.
1617 */
1618 if (syn_stack_equal(sp))
1619 retval = FALSE;
1620
1621 /*
1622 * Store the current state in b_sst_array[] for later use.
1623 */
1624 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001625 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627 }
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 return retval;
1630}
1631
1632/*
1633 * Finish the current line.
1634 * This doesn't return any attributes, it only gets the state at the end of
1635 * the line. It can start anywhere in the line, as long as the current state
1636 * is valid.
1637 */
1638 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001639syn_finish_line(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001640 int syncing) // called for syncing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 stateitem_T *cur_si;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001643 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001645 while (!current_finished)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001647 (void)syn_current_attr(syncing, FALSE, NULL, FALSE);
1648 /*
1649 * When syncing, and found some item, need to check the item.
1650 */
1651 if (syncing && current_state.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 /*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001654 * Check for match with sync item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 */
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001656 cur_si = &CUR_STATE(current_state.ga_len - 1);
1657 if (cur_si->si_idx >= 0
1658 && (SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags
1659 & (HL_SYNC_HERE|HL_SYNC_THERE)))
1660 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001662 // syn_current_attr() will have skipped the check for an item
1663 // that ends here, need to do that now. Be careful not to go
1664 // past the NUL.
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001665 prev_current_col = current_col;
1666 if (syn_getcurline()[current_col] != NUL)
1667 ++current_col;
1668 check_state_ends();
1669 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001671 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673 return FALSE;
1674}
1675
1676/*
1677 * Return highlight attributes for next character.
1678 * Must first call syntax_start() once for the line.
1679 * "col" is normally 0 for the first use in a line, and increments by one each
1680 * time. It's allowed to skip characters and to stop before the end of the
1681 * line. But only a "col" after a previously used column is allowed.
Bram Moolenaar217ad922005-03-20 22:37:15 +00001682 * When "can_spell" is not NULL set it to TRUE when spell-checking should be
1683 * done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
1685 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001686get_syntax_attr(
1687 colnr_T col,
1688 int *can_spell,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001689 int keep_state) // keep state of char at "col"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 int attr = 0;
1692
Bram Moolenaar349955a2007-08-14 21:07:36 +00001693 if (can_spell != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001694 // Default: Only do spelling when there is no @Spell cluster or when
1695 // ":syn spell toplevel" was used.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001696 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
1697 ? (syn_block->b_spell_cluster_id == 0)
1698 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar349955a2007-08-14 21:07:36 +00001699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001700 // check for out of memory situation
Bram Moolenaar860cae12010-06-05 23:22:07 +02001701 if (syn_block->b_sst_array == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 return 0;
1703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001704 // After 'synmaxcol' the attribute is always zero.
Bram Moolenaar84fb85a2005-07-20 22:02:14 +00001705 if (syn_buf->b_p_smc > 0 && col >= (colnr_T)syn_buf->b_p_smc)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001706 {
1707 clear_current_state();
1708#ifdef FEAT_EVAL
1709 current_id = 0;
1710 current_trans_id = 0;
1711#endif
Bram Moolenaar7510fe72010-07-25 12:46:44 +02001712#ifdef FEAT_CONCEAL
1713 current_flags = 0;
Bram Moolenaarcc0750d2017-06-24 22:29:24 +02001714 current_seqnr = 0;
Bram Moolenaar7510fe72010-07-25 12:46:44 +02001715#endif
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001716 return 0;
1717 }
1718
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001719 // Make sure current_state is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (INVALID_STATE(&current_state))
1721 validate_current_state();
1722
1723 /*
1724 * Skip from the current column to "col", get the attributes for "col".
1725 */
1726 while (current_col <= col)
1727 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001728 attr = syn_current_attr(FALSE, TRUE, can_spell,
1729 current_col == col ? keep_state : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 ++current_col;
1731 }
1732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 return attr;
1734}
1735
1736/*
1737 * Get syntax attributes for current_lnum, current_col.
1738 */
1739 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001740syn_current_attr(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001741 int syncing, // When 1: called for syncing
1742 int displaying, // result will be displayed
1743 int *can_spell, // return: do spell checking
1744 int keep_state) // keep syntax stack afterwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 int syn_id;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001747 lpos_T endpos; // was: char_u *endp;
1748 lpos_T hl_startpos; // was: int hl_startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 lpos_T hl_endpos;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001750 lpos_T eos_pos; // end-of-start match (start region)
1751 lpos_T eoe_pos; // end-of-end pattern
1752 int end_idx; // group ID for end pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 int idx;
1754 synpat_T *spp;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001755 stateitem_T *cur_si, *sip = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 int startcol;
1757 int endcol;
1758 long flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001759 int cchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 short *next_list;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001761 int found_match; // found usable match
1762 static int try_next_column = FALSE; // must try in next col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 int do_keywords;
1764 regmmatch_T regmatch;
1765 lpos_T pos;
1766 int lc_col;
1767 reg_extmatch_T *cur_extmatch = NULL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001768 char_u buf_chartab[32]; // chartab array for syn iskyeyword
1769 char_u *line; // current line. NOTE: becomes invalid after
1770 // looking for a pattern match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001772 // variables for zero-width matches that have a "nextgroup" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 int keep_next_list;
1774 int zero_width_next_list = FALSE;
1775 garray_T zero_width_next_ga;
1776
1777 /*
1778 * No character, no attributes! Past end of line?
1779 * Do try matching with an empty line (could be the start of a region).
1780 */
1781 line = syn_getcurline();
1782 if (line[current_col] == NUL && current_col != 0)
1783 {
1784 /*
1785 * If we found a match after the last column, use it.
1786 */
1787 if (next_match_idx >= 0 && next_match_col >= (int)current_col
1788 && next_match_col != MAXCOL)
1789 (void)push_next_match(NULL);
1790
1791 current_finished = TRUE;
1792 current_state_stored = FALSE;
1793 return 0;
1794 }
1795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001796 // if the current or next character is NUL, we will finish the line now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (line[current_col] == NUL || line[current_col + 1] == NUL)
1798 {
1799 current_finished = TRUE;
1800 current_state_stored = FALSE;
1801 }
1802
1803 /*
1804 * When in the previous column there was a match but it could not be used
1805 * (empty match or already matched in this column) need to try again in
1806 * the next column.
1807 */
1808 if (try_next_column)
1809 {
1810 next_match_idx = -1;
1811 try_next_column = FALSE;
1812 }
1813
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001814 // Only check for keywords when not syncing and there are some.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 do_keywords = !syncing
Bram Moolenaar860cae12010-06-05 23:22:07 +02001816 && (syn_block->b_keywtab.ht_used > 0
1817 || syn_block->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001819 // Init the list of zero-width matches with a nextlist. This is used to
1820 // avoid matching the same item in the same position twice.
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001821 ga_init2(&zero_width_next_ga, sizeof(int), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001823 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001824 save_chartab(buf_chartab);
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 /*
1827 * Repeat matching keywords and patterns, to find contained items at the
1828 * same column. This stops when there are no extra matches at the current
1829 * column.
1830 */
1831 do
1832 {
1833 found_match = FALSE;
1834 keep_next_list = FALSE;
1835 syn_id = 0;
1836
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 /*
1839 * 1. Check for a current state.
1840 * Only when there is no current state, or if the current state may
1841 * contain other things, we need to check for keywords and patterns.
1842 * Always need to check for contained items if some item has the
1843 * "containedin" argument (takes extra time!).
1844 */
1845 if (current_state.ga_len)
1846 cur_si = &CUR_STATE(current_state.ga_len - 1);
1847 else
1848 cur_si = NULL;
1849
Bram Moolenaar860cae12010-06-05 23:22:07 +02001850 if (syn_block->b_syn_containedin || cur_si == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 || cur_si->si_cont_list != NULL)
1852 {
1853 /*
1854 * 2. Check for keywords, if on a keyword char after a non-keyword
1855 * char. Don't do this when syncing.
1856 */
1857 if (do_keywords)
1858 {
1859 line = syn_getcurline();
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01001860 if (vim_iswordp_buf(line + current_col, syn_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 && (current_col == 0
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01001862 || !vim_iswordp_buf(line + current_col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 - (has_mbyte
1864 ? (*mb_head_off)(line, line + current_col - 1)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001865 : 0) , syn_buf)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
1867 syn_id = check_keyword_id(line, (int)current_col,
Bram Moolenaar860cae12010-06-05 23:22:07 +02001868 &endcol, &flags, &next_list, cur_si,
1869 &cchar);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001870 if (syn_id != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
1872 if (push_current_state(KEYWORD_IDX) == OK)
1873 {
1874 cur_si = &CUR_STATE(current_state.ga_len - 1);
1875 cur_si->si_m_startcol = current_col;
1876 cur_si->si_h_startpos.lnum = current_lnum;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001877 cur_si->si_h_startpos.col = 0; // starts right away
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 cur_si->si_m_endpos.lnum = current_lnum;
1879 cur_si->si_m_endpos.col = endcol;
1880 cur_si->si_h_endpos.lnum = current_lnum;
1881 cur_si->si_h_endpos.col = endcol;
1882 cur_si->si_ends = TRUE;
1883 cur_si->si_end_idx = 0;
1884 cur_si->si_flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001885#ifdef FEAT_CONCEAL
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02001886 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001887 cur_si->si_cchar = cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001888 if (current_state.ga_len > 1)
1889 cur_si->si_flags |=
1890 CUR_STATE(current_state.ga_len - 2).si_flags
1891 & HL_CONCEAL;
1892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 cur_si->si_id = syn_id;
1894 cur_si->si_trans_id = syn_id;
1895 if (flags & HL_TRANSP)
1896 {
1897 if (current_state.ga_len < 2)
1898 {
1899 cur_si->si_attr = 0;
1900 cur_si->si_trans_id = 0;
1901 }
1902 else
1903 {
1904 cur_si->si_attr = CUR_STATE(
1905 current_state.ga_len - 2).si_attr;
1906 cur_si->si_trans_id = CUR_STATE(
1907 current_state.ga_len - 2).si_trans_id;
1908 }
1909 }
1910 else
1911 cur_si->si_attr = syn_id2attr(syn_id);
1912 cur_si->si_cont_list = NULL;
1913 cur_si->si_next_list = next_list;
1914 check_keepend();
1915 }
1916 else
1917 vim_free(next_list);
1918 }
1919 }
1920 }
1921
1922 /*
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001923 * 3. Check for patterns (only if no keyword found).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001925 if (syn_id == 0 && syn_block->b_syn_patterns.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 /*
1928 * If we didn't check for a match yet, or we are past it, check
1929 * for any match with a pattern.
1930 */
1931 if (next_match_idx < 0 || next_match_col < (int)current_col)
1932 {
1933 /*
1934 * Check all relevant patterns for a match at this
1935 * position. This is complicated, because matching with a
1936 * pattern takes quite a bit of time, thus we want to
1937 * avoid doing it when it's not needed.
1938 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001939 next_match_idx = 0; // no match in this line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 next_match_col = MAXCOL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001941 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001943 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if ( spp->sp_syncing == syncing
1945 && (displaying || !(spp->sp_flags & HL_DISPLAY))
1946 && (spp->sp_type == SPTYPE_MATCH
1947 || spp->sp_type == SPTYPE_START)
1948 && (current_next_list != NULL
1949 ? in_id_list(NULL, current_next_list,
1950 &spp->sp_syn, 0)
1951 : (cur_si == NULL
1952 ? !(spp->sp_flags & HL_CONTAINED)
1953 : in_id_list(cur_si,
1954 cur_si->si_cont_list, &spp->sp_syn,
1955 spp->sp_flags & HL_CONTAINED))))
1956 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001957 int r;
1958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001959 // If we already tried matching in this line, and
1960 // there isn't a match before next_match_col, skip
1961 // this item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (spp->sp_line_id == current_line_id
1963 && spp->sp_startcol >= next_match_col)
1964 continue;
1965 spp->sp_line_id = current_line_id;
1966
1967 lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
1968 if (lc_col < 0)
1969 lc_col = 0;
1970
1971 regmatch.rmm_ic = spp->sp_ic;
1972 regmatch.regprog = spp->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001973 r = syn_regexec(&regmatch,
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02001974 current_lnum,
1975 (colnr_T)lc_col,
Bram Moolenaard23a8232018-02-10 18:45:26 +01001976 IF_SYN_TIME(&spp->sp_time));
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001977 spp->sp_prog = regmatch.regprog;
1978 if (!r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001980 // no match in this line, try another one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 spp->sp_startcol = MAXCOL;
1982 continue;
1983 }
1984
1985 /*
1986 * Compute the first column of the match.
1987 */
1988 syn_add_start_off(&pos, &regmatch,
1989 spp, SPO_MS_OFF, -1);
1990 if (pos.lnum > current_lnum)
1991 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001992 // must have used end of match in a next line,
1993 // we can't handle that
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 spp->sp_startcol = MAXCOL;
1995 continue;
1996 }
1997 startcol = pos.col;
1998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001999 // remember the next column where this pattern
2000 // matches in the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 spp->sp_startcol = startcol;
2002
2003 /*
2004 * If a previously found match starts at a lower
2005 * column number, don't use this one.
2006 */
2007 if (startcol >= next_match_col)
2008 continue;
2009
2010 /*
2011 * If we matched this pattern at this position
2012 * before, skip it. Must retry in the next
2013 * column, because it may match from there.
2014 */
2015 if (did_match_already(idx, &zero_width_next_ga))
2016 {
2017 try_next_column = TRUE;
2018 continue;
2019 }
2020
2021 endpos.lnum = regmatch.endpos[0].lnum;
2022 endpos.col = regmatch.endpos[0].col;
2023
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002024 // Compute the highlight start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 syn_add_start_off(&hl_startpos, &regmatch,
2026 spp, SPO_HS_OFF, -1);
2027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002028 // Compute the region start.
2029 // Default is to use the end of the match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 syn_add_end_off(&eos_pos, &regmatch,
2031 spp, SPO_RS_OFF, 0);
2032
2033 /*
2034 * Grab the external submatches before they get
2035 * overwritten. Reference count doesn't change.
2036 */
2037 unref_extmatch(cur_extmatch);
2038 cur_extmatch = re_extmatch_out;
2039 re_extmatch_out = NULL;
2040
2041 flags = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 eoe_pos.lnum = 0; // avoid warning
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 eoe_pos.col = 0;
2044 end_idx = 0;
2045 hl_endpos.lnum = 0;
2046
2047 /*
2048 * For a "oneline" the end must be found in the
2049 * same line too. Search for it after the end of
2050 * the match with the start pattern. Set the
2051 * resulting end positions at the same time.
2052 */
2053 if (spp->sp_type == SPTYPE_START
2054 && (spp->sp_flags & HL_ONELINE))
2055 {
2056 lpos_T startpos;
2057
2058 startpos = endpos;
2059 find_endpos(idx, &startpos, &endpos, &hl_endpos,
2060 &flags, &eoe_pos, &end_idx, cur_extmatch);
2061 if (endpos.lnum == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002062 continue; // not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064
2065 /*
2066 * For a "match" the size must be > 0 after the
2067 * end offset needs has been added. Except when
2068 * syncing.
2069 */
2070 else if (spp->sp_type == SPTYPE_MATCH)
2071 {
2072 syn_add_end_off(&hl_endpos, &regmatch, spp,
2073 SPO_HE_OFF, 0);
2074 syn_add_end_off(&endpos, &regmatch, spp,
2075 SPO_ME_OFF, 0);
2076 if (endpos.lnum == current_lnum
2077 && (int)endpos.col + syncing < startcol)
2078 {
2079 /*
2080 * If an empty string is matched, may need
2081 * to try matching again at next column.
2082 */
2083 if (regmatch.startpos[0].col
2084 == regmatch.endpos[0].col)
2085 try_next_column = TRUE;
2086 continue;
2087 }
2088 }
2089
2090 /*
2091 * keep the best match so far in next_match_*
2092 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002093 // Highlighting must start after startpos and end
2094 // before endpos.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 if (hl_startpos.lnum == current_lnum
2096 && (int)hl_startpos.col < startcol)
2097 hl_startpos.col = startcol;
2098 limit_pos_zero(&hl_endpos, &endpos);
2099
2100 next_match_idx = idx;
2101 next_match_col = startcol;
2102 next_match_m_endpos = endpos;
2103 next_match_h_endpos = hl_endpos;
2104 next_match_h_startpos = hl_startpos;
2105 next_match_flags = flags;
2106 next_match_eos_pos = eos_pos;
2107 next_match_eoe_pos = eoe_pos;
2108 next_match_end_idx = end_idx;
2109 unref_extmatch(next_match_extmatch);
2110 next_match_extmatch = cur_extmatch;
2111 cur_extmatch = NULL;
2112 }
2113 }
2114 }
2115
2116 /*
2117 * If we found a match at the current column, use it.
2118 */
2119 if (next_match_idx >= 0 && next_match_col == (int)current_col)
2120 {
2121 synpat_T *lspp;
2122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002123 // When a zero-width item matched which has a nextgroup,
2124 // don't push the item but set nextgroup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002125 lspp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (next_match_m_endpos.lnum == current_lnum
2127 && next_match_m_endpos.col == current_col
2128 && lspp->sp_next_list != NULL)
2129 {
2130 current_next_list = lspp->sp_next_list;
2131 current_next_flags = lspp->sp_flags;
2132 keep_next_list = TRUE;
2133 zero_width_next_list = TRUE;
2134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 // Add the index to a list, so that we can check
2136 // later that we don't match it again (and cause an
2137 // endless loop).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (ga_grow(&zero_width_next_ga, 1) == OK)
2139 {
2140 ((int *)(zero_width_next_ga.ga_data))
2141 [zero_width_next_ga.ga_len++] = next_match_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 next_match_idx = -1;
2144 }
2145 else
2146 cur_si = push_next_match(cur_si);
2147 found_match = TRUE;
2148 }
2149 }
2150 }
2151
2152 /*
2153 * Handle searching for nextgroup match.
2154 */
2155 if (current_next_list != NULL && !keep_next_list)
2156 {
2157 /*
2158 * If a nextgroup was not found, continue looking for one if:
2159 * - this is an empty line and the "skipempty" option was given
2160 * - we are on white space and the "skipwhite" option was given
2161 */
2162 if (!found_match)
2163 {
2164 line = syn_getcurline();
2165 if (((current_next_flags & HL_SKIPWHITE)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002166 && VIM_ISWHITE(line[current_col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 || ((current_next_flags & HL_SKIPEMPTY)
2168 && *line == NUL))
2169 break;
2170 }
2171
2172 /*
2173 * If a nextgroup was found: Use it, and continue looking for
2174 * contained matches.
2175 * If a nextgroup was not found: Continue looking for a normal
2176 * match.
2177 * When did set current_next_list for a zero-width item and no
2178 * match was found don't loop (would get stuck).
2179 */
2180 current_next_list = NULL;
2181 next_match_idx = -1;
2182 if (!zero_width_next_list)
2183 found_match = TRUE;
2184 }
2185
2186 } while (found_match);
2187
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002188 restore_chartab(buf_chartab);
2189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 /*
2191 * Use attributes from the current state, if within its highlighting.
2192 * If not, use attributes from the current-but-one state, etc.
2193 */
2194 current_attr = 0;
2195#ifdef FEAT_EVAL
2196 current_id = 0;
2197 current_trans_id = 0;
2198#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002199#ifdef FEAT_CONCEAL
2200 current_flags = 0;
Bram Moolenaarcc0750d2017-06-24 22:29:24 +02002201 current_seqnr = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (cur_si != NULL)
2204 {
Bram Moolenaar217ad922005-03-20 22:37:15 +00002205#ifndef FEAT_EVAL
2206 int current_trans_id = 0;
2207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 for (idx = current_state.ga_len - 1; idx >= 0; --idx)
2209 {
2210 sip = &CUR_STATE(idx);
2211 if ((current_lnum > sip->si_h_startpos.lnum
2212 || (current_lnum == sip->si_h_startpos.lnum
2213 && current_col >= sip->si_h_startpos.col))
2214 && (sip->si_h_endpos.lnum == 0
2215 || current_lnum < sip->si_h_endpos.lnum
2216 || (current_lnum == sip->si_h_endpos.lnum
2217 && current_col < sip->si_h_endpos.col)))
2218 {
2219 current_attr = sip->si_attr;
2220#ifdef FEAT_EVAL
2221 current_id = sip->si_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002223 current_trans_id = sip->si_trans_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002224#ifdef FEAT_CONCEAL
2225 current_flags = sip->si_flags;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002226 current_seqnr = sip->si_seqnr;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02002227 current_sub_char = sip->si_cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 break;
2230 }
2231 }
2232
Bram Moolenaar217ad922005-03-20 22:37:15 +00002233 if (can_spell != NULL)
2234 {
2235 struct sp_syn sps;
2236
2237 /*
2238 * set "can_spell" to TRUE if spell checking is supposed to be
2239 * done in the current item.
2240 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002241 if (syn_block->b_spell_cluster_id == 0)
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002242 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002243 // There is no @Spell cluster: Do spelling for items without
2244 // @NoSpell cluster.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002245 if (syn_block->b_nospell_cluster_id == 0
2246 || current_trans_id == 0)
2247 *can_spell = (syn_block->b_syn_spell != SYNSPL_NOTOP);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002248 else
2249 {
2250 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002251 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002252 sps.cont_in_list = NULL;
2253 *can_spell = !in_id_list(sip, sip->si_cont_list, &sps, 0);
2254 }
2255 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002256 else
2257 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002258 // The @Spell cluster is defined: Do spelling in items with
2259 // the @Spell cluster. But not when @NoSpell is also there.
2260 // At the toplevel only spell check when ":syn spell toplevel"
2261 // was used.
Bram Moolenaar3638c682005-06-08 22:05:14 +00002262 if (current_trans_id == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002263 *can_spell = (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar3638c682005-06-08 22:05:14 +00002264 else
2265 {
2266 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002267 sps.id = syn_block->b_spell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002268 sps.cont_in_list = NULL;
2269 *can_spell = in_id_list(sip, sip->si_cont_list, &sps, 0);
2270
Bram Moolenaar860cae12010-06-05 23:22:07 +02002271 if (syn_block->b_nospell_cluster_id != 0)
Bram Moolenaar3638c682005-06-08 22:05:14 +00002272 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002273 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002274 if (in_id_list(sip, sip->si_cont_list, &sps, 0))
2275 *can_spell = FALSE;
2276 }
2277 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002278 }
2279 }
2280
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 /*
2283 * Check for end of current state (and the states before it) at the
2284 * next column. Don't do this for syncing, because we would miss a
2285 * single character match.
2286 * First check if the current state ends at the current column. It
2287 * may be for an empty match and a containing item might end in the
2288 * current column.
2289 */
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002290 if (!syncing && !keep_state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +00002293 if (current_state.ga_len > 0
2294 && syn_getcurline()[current_col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 ++current_col;
2297 check_state_ends();
2298 --current_col;
2299 }
2300 }
2301 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002302 else if (can_spell != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002303 // Default: Only do spelling when there is no @Spell cluster or when
2304 // ":syn spell toplevel" was used.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002305 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
2306 ? (syn_block->b_spell_cluster_id == 0)
2307 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002309 // nextgroup ends at end of line, unless "skipnl" or "skipempty" present
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (current_next_list != NULL
Bram Moolenaar069dafc2018-03-03 20:02:19 +01002311 && (line = syn_getcurline())[current_col] != NUL
2312 && line[current_col + 1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 && !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
2314 current_next_list = NULL;
2315
2316 if (zero_width_next_ga.ga_len > 0)
2317 ga_clear(&zero_width_next_ga);
2318
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002319 // No longer need external matches. But keep next_match_extmatch.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 unref_extmatch(re_extmatch_out);
2321 re_extmatch_out = NULL;
2322 unref_extmatch(cur_extmatch);
2323
2324 return current_attr;
2325}
2326
2327
2328/*
2329 * Check if we already matched pattern "idx" at the current column.
2330 */
2331 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002332did_match_already(int idx, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 int i;
2335
2336 for (i = current_state.ga_len; --i >= 0; )
2337 if (CUR_STATE(i).si_m_startcol == (int)current_col
2338 && CUR_STATE(i).si_m_lnum == (int)current_lnum
2339 && CUR_STATE(i).si_idx == idx)
2340 return TRUE;
2341
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002342 // Zero-width matches with a nextgroup argument are not put on the syntax
2343 // stack, and can only be matched once anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 for (i = gap->ga_len; --i >= 0; )
2345 if (((int *)(gap->ga_data))[i] == idx)
2346 return TRUE;
2347
2348 return FALSE;
2349}
2350
2351/*
2352 * Push the next match onto the stack.
2353 */
2354 static stateitem_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002355push_next_match(stateitem_T *cur_si)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
2357 synpat_T *spp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002358#ifdef FEAT_CONCEAL
2359 int save_flags;
2360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaar860cae12010-06-05 23:22:07 +02002362 spp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 /*
2365 * Push the item in current_state stack;
2366 */
2367 if (push_current_state(next_match_idx) == OK)
2368 {
2369 /*
2370 * If it's a start-skip-end type that crosses lines, figure out how
2371 * much it continues in this line. Otherwise just fill in the length.
2372 */
2373 cur_si = &CUR_STATE(current_state.ga_len - 1);
2374 cur_si->si_h_startpos = next_match_h_startpos;
2375 cur_si->si_m_startcol = current_col;
2376 cur_si->si_m_lnum = current_lnum;
2377 cur_si->si_flags = spp->sp_flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002378#ifdef FEAT_CONCEAL
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002379 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02002380 cur_si->si_cchar = spp->sp_cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002381 if (current_state.ga_len > 1)
2382 cur_si->si_flags |=
2383 CUR_STATE(current_state.ga_len - 2).si_flags & HL_CONCEAL;
2384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 cur_si->si_next_list = spp->sp_next_list;
2386 cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
2387 if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
2388 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002389 // Try to find the end pattern in the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
2391 check_keepend();
2392 }
2393 else
2394 {
2395 cur_si->si_m_endpos = next_match_m_endpos;
2396 cur_si->si_h_endpos = next_match_h_endpos;
2397 cur_si->si_ends = TRUE;
2398 cur_si->si_flags |= next_match_flags;
2399 cur_si->si_eoe_pos = next_match_eoe_pos;
2400 cur_si->si_end_idx = next_match_end_idx;
2401 }
2402 if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
2403 keepend_level = current_state.ga_len - 1;
2404 check_keepend();
2405 update_si_attr(current_state.ga_len - 1);
2406
Bram Moolenaar860cae12010-06-05 23:22:07 +02002407#ifdef FEAT_CONCEAL
2408 save_flags = cur_si->si_flags & (HL_CONCEAL | HL_CONCEALENDS);
2409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 /*
2411 * If the start pattern has another highlight group, push another item
2412 * on the stack for the start pattern.
2413 */
2414 if ( spp->sp_type == SPTYPE_START
2415 && spp->sp_syn_match_id != 0
2416 && push_current_state(next_match_idx) == OK)
2417 {
2418 cur_si = &CUR_STATE(current_state.ga_len - 1);
2419 cur_si->si_h_startpos = next_match_h_startpos;
2420 cur_si->si_m_startcol = current_col;
2421 cur_si->si_m_lnum = current_lnum;
2422 cur_si->si_m_endpos = next_match_eos_pos;
2423 cur_si->si_h_endpos = next_match_eos_pos;
2424 cur_si->si_ends = TRUE;
2425 cur_si->si_end_idx = 0;
2426 cur_si->si_flags = HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002427#ifdef FEAT_CONCEAL
Bram Moolenaar3b953892010-07-27 20:47:25 +02002428 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002429 cur_si->si_flags |= save_flags;
2430 if (cur_si->si_flags & HL_CONCEALENDS)
2431 cur_si->si_flags |= HL_CONCEAL;
2432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 cur_si->si_next_list = NULL;
2434 check_keepend();
2435 update_si_attr(current_state.ga_len - 1);
2436 }
2437 }
2438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002439 next_match_idx = -1; // try other match next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 return cur_si;
2442}
2443
2444/*
2445 * Check for end of current state (and the states before it).
2446 */
2447 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002448check_state_ends(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450 stateitem_T *cur_si;
Bram Moolenaar6fa46362011-05-25 17:56:27 +02002451 int had_extend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453 cur_si = &CUR_STATE(current_state.ga_len - 1);
2454 for (;;)
2455 {
2456 if (cur_si->si_ends
2457 && (cur_si->si_m_endpos.lnum < current_lnum
2458 || (cur_si->si_m_endpos.lnum == current_lnum
2459 && cur_si->si_m_endpos.col <= current_col)))
2460 {
2461 /*
2462 * If there is an end pattern group ID, highlight the end pattern
2463 * now. No need to pop the current item from the stack.
2464 * Only do this if the end pattern continues beyond the current
2465 * position.
2466 */
2467 if (cur_si->si_end_idx
2468 && (cur_si->si_eoe_pos.lnum > current_lnum
2469 || (cur_si->si_eoe_pos.lnum == current_lnum
2470 && cur_si->si_eoe_pos.col > current_col)))
2471 {
2472 cur_si->si_idx = cur_si->si_end_idx;
2473 cur_si->si_end_idx = 0;
2474 cur_si->si_m_endpos = cur_si->si_eoe_pos;
2475 cur_si->si_h_endpos = cur_si->si_eoe_pos;
2476 cur_si->si_flags |= HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002477#ifdef FEAT_CONCEAL
Bram Moolenaar3b953892010-07-27 20:47:25 +02002478 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002479 if (cur_si->si_flags & HL_CONCEALENDS)
2480 cur_si->si_flags |= HL_CONCEAL;
2481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 update_si_attr(current_state.ga_len - 1);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002483
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002484 // nextgroup= should not match in the end pattern
Bram Moolenaar3a7d8c32011-05-19 12:14:10 +02002485 current_next_list = NULL;
2486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002487 // what matches next may be different now, clear it
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002488 next_match_idx = 0;
2489 next_match_col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 break;
2491 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01002492
2493 // handle next_list, unless at end of line and no "skipnl" or
2494 // "skipempty"
2495 current_next_list = cur_si->si_next_list;
2496 current_next_flags = cur_si->si_flags;
2497 if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
2498 && syn_getcurline()[current_col] == NUL)
2499 current_next_list = NULL;
2500
2501 // When the ended item has "extend", another item with
2502 // "keepend" now needs to check for its end.
2503 had_extend = (cur_si->si_flags & HL_EXTEND);
2504
2505 pop_current_state();
2506
2507 if (current_state.ga_len == 0)
2508 break;
2509
2510 if (had_extend && keepend_level >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01002512 syn_update_ends(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (current_state.ga_len == 0)
2514 break;
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01002515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01002517 cur_si = &CUR_STATE(current_state.ga_len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01002519 /*
2520 * Only for a region the search for the end continues after
2521 * the end of the contained item. If the contained match
2522 * included the end-of-line, break here, the region continues.
2523 * Don't do this when:
2524 * - "keepend" is used for the contained item
2525 * - not at the end of the line (could be end="x$"me=e-1).
2526 * - "excludenl" is used (HL_HAS_EOL won't be set)
2527 */
2528 if (cur_si->si_idx >= 0
2529 && SYN_ITEMS(syn_block)[cur_si->si_idx].sp_type
2530 == SPTYPE_START
2531 && !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
2532 {
2533 update_si_end(cur_si, (int)current_col, TRUE);
2534 check_keepend();
2535 if ((current_next_flags & HL_HAS_EOL)
2536 && keepend_level < 0
2537 && syn_getcurline()[current_col] == NUL)
2538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540 }
2541 else
2542 break;
2543 }
2544}
2545
2546/*
2547 * Update an entry in the current_state stack for a match or region. This
2548 * fills in si_attr, si_next_list and si_cont_list.
2549 */
2550 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002551update_si_attr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 stateitem_T *sip = &CUR_STATE(idx);
2554 synpat_T *spp;
2555
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002556 // This should not happen...
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002557 if (sip->si_idx < 0)
2558 return;
2559
Bram Moolenaar860cae12010-06-05 23:22:07 +02002560 spp = &(SYN_ITEMS(syn_block)[sip->si_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (sip->si_flags & HL_MATCH)
2562 sip->si_id = spp->sp_syn_match_id;
2563 else
2564 sip->si_id = spp->sp_syn.id;
2565 sip->si_attr = syn_id2attr(sip->si_id);
2566 sip->si_trans_id = sip->si_id;
2567 if (sip->si_flags & HL_MATCH)
2568 sip->si_cont_list = NULL;
2569 else
2570 sip->si_cont_list = spp->sp_cont_list;
2571
2572 /*
2573 * For transparent items, take attr from outer item.
2574 * Also take cont_list, if there is none.
2575 * Don't do this for the matchgroup of a start or end pattern.
2576 */
2577 if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
2578 {
2579 if (idx == 0)
2580 {
2581 sip->si_attr = 0;
2582 sip->si_trans_id = 0;
2583 if (sip->si_cont_list == NULL)
2584 sip->si_cont_list = ID_LIST_ALL;
2585 }
2586 else
2587 {
2588 sip->si_attr = CUR_STATE(idx - 1).si_attr;
2589 sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
2590 if (sip->si_cont_list == NULL)
2591 {
2592 sip->si_flags |= HL_TRANS_CONT;
2593 sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
2594 }
2595 }
2596 }
2597}
2598
2599/*
2600 * Check the current stack for patterns with "keepend" flag.
2601 * Propagate the match-end to contained items, until a "skipend" item is found.
2602 */
2603 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002604check_keepend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 int i;
2607 lpos_T maxpos;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002608 lpos_T maxpos_h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 stateitem_T *sip;
2610
2611 /*
2612 * This check can consume a lot of time; only do it from the level where
2613 * there really is a keepend.
2614 */
2615 if (keepend_level < 0)
2616 return;
2617
2618 /*
2619 * Find the last index of an "extend" item. "keepend" items before that
2620 * won't do anything. If there is no "extend" item "i" will be
2621 * "keepend_level" and all "keepend" items will work normally.
2622 */
2623 for (i = current_state.ga_len - 1; i > keepend_level; --i)
2624 if (CUR_STATE(i).si_flags & HL_EXTEND)
2625 break;
2626
2627 maxpos.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002628 maxpos.col = 0;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002629 maxpos_h.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002630 maxpos_h.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 for ( ; i < current_state.ga_len; ++i)
2632 {
2633 sip = &CUR_STATE(i);
2634 if (maxpos.lnum != 0)
2635 {
2636 limit_pos_zero(&sip->si_m_endpos, &maxpos);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002637 limit_pos_zero(&sip->si_h_endpos, &maxpos_h);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 limit_pos_zero(&sip->si_eoe_pos, &maxpos);
2639 sip->si_ends = TRUE;
2640 }
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002641 if (sip->si_ends && (sip->si_flags & HL_KEEPEND))
2642 {
2643 if (maxpos.lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 || maxpos.lnum > sip->si_m_endpos.lnum
2645 || (maxpos.lnum == sip->si_m_endpos.lnum
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002646 && maxpos.col > sip->si_m_endpos.col))
2647 maxpos = sip->si_m_endpos;
2648 if (maxpos_h.lnum == 0
2649 || maxpos_h.lnum > sip->si_h_endpos.lnum
2650 || (maxpos_h.lnum == sip->si_h_endpos.lnum
2651 && maxpos_h.col > sip->si_h_endpos.col))
2652 maxpos_h = sip->si_h_endpos;
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
2655}
2656
2657/*
2658 * Update an entry in the current_state stack for a start-skip-end pattern.
2659 * This finds the end of the current item, if it's in the current line.
2660 *
2661 * Return the flags for the matched END.
2662 */
2663 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002664update_si_end(
2665 stateitem_T *sip,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002666 int startcol, // where to start searching for the end
2667 int force) // when TRUE overrule a previous end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 lpos_T startpos;
2670 lpos_T endpos;
2671 lpos_T hl_endpos;
2672 lpos_T end_endpos;
2673 int end_idx;
2674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002675 // return quickly for a keyword
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002676 if (sip->si_idx < 0)
2677 return;
2678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002679 // Don't update when it's already done. Can be a match of an end pattern
2680 // that started in a previous line. Watch out: can also be a "keepend"
2681 // from a containing item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (!force && sip->si_m_endpos.lnum >= current_lnum)
2683 return;
2684
2685 /*
2686 * We need to find the end of the region. It may continue in the next
2687 * line.
2688 */
2689 end_idx = 0;
2690 startpos.lnum = current_lnum;
2691 startpos.col = startcol;
2692 find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
2693 &(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
2694
2695 if (endpos.lnum == 0)
2696 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // No end pattern matched.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002698 if (SYN_ITEMS(syn_block)[sip->si_idx].sp_flags & HL_ONELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // a "oneline" never continues in the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 sip->si_ends = TRUE;
2702 sip->si_m_endpos.lnum = current_lnum;
2703 sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
2704 }
2705 else
2706 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // continues in the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 sip->si_ends = FALSE;
2709 sip->si_m_endpos.lnum = 0;
2710 }
2711 sip->si_h_endpos = sip->si_m_endpos;
2712 }
2713 else
2714 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002715 // match within this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 sip->si_m_endpos = endpos;
2717 sip->si_h_endpos = hl_endpos;
2718 sip->si_eoe_pos = end_endpos;
2719 sip->si_ends = TRUE;
2720 sip->si_end_idx = end_idx;
2721 }
2722}
2723
2724/*
2725 * Add a new state to the current state stack.
2726 * It is cleared and the index set to "idx".
2727 * Return FAIL if it's not possible (out of memory).
2728 */
2729 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002730push_current_state(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 if (ga_grow(&current_state, 1) == FAIL)
2733 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02002734 CLEAR_POINTER(&CUR_STATE(current_state.ga_len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 CUR_STATE(current_state.ga_len).si_idx = idx;
2736 ++current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 return OK;
2738}
2739
2740/*
2741 * Remove a state from the current_state stack.
2742 */
2743 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002744pop_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 if (current_state.ga_len)
2747 {
2748 unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
2749 --current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002751 // after the end of a pattern, try matching a keyword or pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 next_match_idx = -1;
2753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002754 // if first state with "keepend" is popped, reset keepend_level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (keepend_level >= current_state.ga_len)
2756 keepend_level = -1;
2757}
2758
2759/*
2760 * Find the end of a start/skip/end syntax region after "startpos".
2761 * Only checks one line.
2762 * Also handles a match item that continued from a previous line.
2763 * If not found, the syntax item continues in the next line. m_endpos->lnum
2764 * will be 0.
2765 * If found, the end of the region and the end of the highlighting is
2766 * computed.
2767 */
2768 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002769find_endpos(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002770 int idx, // index of the pattern
2771 lpos_T *startpos, // where to start looking for an END match
2772 lpos_T *m_endpos, // return: end of match
2773 lpos_T *hl_endpos, // return: end of highlighting
2774 long *flagsp, // return: flags of matching END
2775 lpos_T *end_endpos, // return: end of end pattern match
2776 int *end_idx, // return: group ID for end pat. match, or 0
2777 reg_extmatch_T *start_ext) // submatches from the start pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 colnr_T matchcol;
2780 synpat_T *spp, *spp_skip;
2781 int start_idx;
2782 int best_idx;
2783 regmmatch_T regmatch;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002784 regmmatch_T best_regmatch; // startpos/endpos of best match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 lpos_T pos;
2786 char_u *line;
2787 int had_match = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002788 char_u buf_chartab[32]; // chartab array for syn option iskyeyword
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002790 // just in case we are invoked for a keyword
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002791 if (idx < 0)
2792 return;
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 /*
2795 * Check for being called with a START pattern.
2796 * Can happen with a match that continues to the next line, because it
2797 * contained a region.
2798 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002799 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (spp->sp_type != SPTYPE_START)
2801 {
2802 *hl_endpos = *startpos;
2803 return;
2804 }
2805
2806 /*
2807 * Find the SKIP or first END pattern after the last START pattern.
2808 */
2809 for (;;)
2810 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002811 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (spp->sp_type != SPTYPE_START)
2813 break;
2814 ++idx;
2815 }
2816
2817 /*
2818 * Lookup the SKIP pattern (if present)
2819 */
2820 if (spp->sp_type == SPTYPE_SKIP)
2821 {
2822 spp_skip = spp;
2823 ++idx;
2824 }
2825 else
2826 spp_skip = NULL;
2827
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002828 // Setup external matches for syn_regexec().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 unref_extmatch(re_extmatch_in);
2830 re_extmatch_in = ref_extmatch(start_ext);
2831
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002832 matchcol = startpos->col; // start looking for a match at sstart
2833 start_idx = idx; // remember the first END pattern.
2834 best_regmatch.startpos[0].col = 0; // avoid compiler warning
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002835
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002836 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002837 save_chartab(buf_chartab);
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 for (;;)
2840 {
2841 /*
2842 * Find end pattern that matches first after "matchcol".
2843 */
2844 best_idx = -1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002845 for (idx = start_idx; idx < syn_block->b_syn_patterns.ga_len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
2847 int lc_col = matchcol;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002848 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaar860cae12010-06-05 23:22:07 +02002850 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002851 if (spp->sp_type != SPTYPE_END) // past last END pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 break;
2853 lc_col -= spp->sp_offsets[SPO_LC_OFF];
2854 if (lc_col < 0)
2855 lc_col = 0;
2856
2857 regmatch.rmm_ic = spp->sp_ic;
2858 regmatch.regprog = spp->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002859 r = syn_regexec(&regmatch, startpos->lnum, lc_col,
2860 IF_SYN_TIME(&spp->sp_time));
2861 spp->sp_prog = regmatch.regprog;
2862 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
2864 if (best_idx == -1 || regmatch.startpos[0].col
2865 < best_regmatch.startpos[0].col)
2866 {
2867 best_idx = idx;
2868 best_regmatch.startpos[0] = regmatch.startpos[0];
2869 best_regmatch.endpos[0] = regmatch.endpos[0];
2870 }
2871 }
2872 }
2873
2874 /*
2875 * If all end patterns have been tried, and there is no match, the
2876 * item continues until end-of-line.
2877 */
2878 if (best_idx == -1)
2879 break;
2880
2881 /*
2882 * If the skip pattern matches before the end pattern,
2883 * continue searching after the skip pattern.
2884 */
2885 if (spp_skip != NULL)
2886 {
2887 int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002888 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
2890 if (lc_col < 0)
2891 lc_col = 0;
2892 regmatch.rmm_ic = spp_skip->sp_ic;
2893 regmatch.regprog = spp_skip->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002894 r = syn_regexec(&regmatch, startpos->lnum, lc_col,
2895 IF_SYN_TIME(&spp_skip->sp_time));
2896 spp_skip->sp_prog = regmatch.regprog;
2897 if (r && regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 <= best_regmatch.startpos[0].col)
2899 {
Bram Moolenaar04bff882016-01-05 20:46:16 +01002900 int line_len;
2901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002902 // Add offset to skip pattern match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 syn_add_end_off(&pos, &regmatch, spp_skip, SPO_ME_OFF, 1);
2904
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002905 // If the skip pattern goes on to the next line, there is no
2906 // match with an end pattern in this line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 if (pos.lnum > startpos->lnum)
2908 break;
2909
2910 line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
Bram Moolenaar04bff882016-01-05 20:46:16 +01002911 line_len = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002913 // take care of an empty match or negative offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (pos.col <= matchcol)
2915 ++matchcol;
2916 else if (pos.col <= regmatch.endpos[0].col)
2917 matchcol = pos.col;
2918 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002919 // Be careful not to jump over the NUL at the end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 for (matchcol = regmatch.endpos[0].col;
Bram Moolenaar04bff882016-01-05 20:46:16 +01002921 matchcol < line_len && matchcol < pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 ++matchcol)
2923 ;
2924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002925 // if the skip pattern includes end-of-line, break here
Bram Moolenaar04bff882016-01-05 20:46:16 +01002926 if (matchcol >= line_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 break;
2928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002929 continue; // start with first end pattern again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931 }
2932
2933 /*
2934 * Match from start pattern to end pattern.
2935 * Correct for match and highlight offset of end pattern.
2936 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002937 spp = &(SYN_ITEMS(syn_block)[best_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002939 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
2941 m_endpos->col = startpos->col;
2942
2943 syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002944 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 if (end_endpos->lnum == startpos->lnum
2946 && end_endpos->col < startpos->col)
2947 end_endpos->col = startpos->col;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002948 // can't end after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 limit_pos(end_endpos, m_endpos);
2950
2951 /*
2952 * If the end group is highlighted differently, adjust the pointers.
2953 */
2954 if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
2955 {
2956 *end_idx = best_idx;
2957 if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
2958 {
2959 hl_endpos->lnum = best_regmatch.endpos[0].lnum;
2960 hl_endpos->col = best_regmatch.endpos[0].col;
2961 }
2962 else
2963 {
2964 hl_endpos->lnum = best_regmatch.startpos[0].lnum;
2965 hl_endpos->col = best_regmatch.startpos[0].col;
2966 }
2967 hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
2968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002969 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (hl_endpos->lnum == startpos->lnum
2971 && hl_endpos->col < startpos->col)
2972 hl_endpos->col = startpos->col;
2973 limit_pos(hl_endpos, m_endpos);
2974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002975 // now the match ends where the highlighting ends, it is turned
2976 // into the matchgroup for the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 *m_endpos = *hl_endpos;
2978 }
2979 else
2980 {
2981 *end_idx = 0;
2982 *hl_endpos = *end_endpos;
2983 }
2984
2985 *flagsp = spp->sp_flags;
2986
2987 had_match = TRUE;
2988 break;
2989 }
2990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002991 // no match for an END pattern in this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (!had_match)
2993 m_endpos->lnum = 0;
2994
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002995 restore_chartab(buf_chartab);
2996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002997 // Remove external matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 unref_extmatch(re_extmatch_in);
2999 re_extmatch_in = NULL;
3000}
3001
3002/*
3003 * Limit "pos" not to be after "limit".
3004 */
3005 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003006limit_pos(lpos_T *pos, lpos_T *limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
3008 if (pos->lnum > limit->lnum)
3009 *pos = *limit;
3010 else if (pos->lnum == limit->lnum && pos->col > limit->col)
3011 pos->col = limit->col;
3012}
3013
3014/*
3015 * Limit "pos" not to be after "limit", unless pos->lnum is zero.
3016 */
3017 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003018limit_pos_zero(
3019 lpos_T *pos,
3020 lpos_T *limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 if (pos->lnum == 0)
3023 *pos = *limit;
3024 else
3025 limit_pos(pos, limit);
3026}
3027
3028/*
3029 * Add offset to matched text for end of match or highlight.
3030 */
3031 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003032syn_add_end_off(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003033 lpos_T *result, // returned position
3034 regmmatch_T *regmatch, // start/end of match
3035 synpat_T *spp, // matched pattern
3036 int idx, // index of offset
3037 int extra) // extra chars for offset to start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003040 int off;
3041 char_u *base;
3042 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 if (spp->sp_off_flags & (1 << idx))
3045 {
3046 result->lnum = regmatch->startpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 col = regmatch->startpos[0].col;
3048 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 else
3051 {
3052 result->lnum = regmatch->endpos[0].lnum;
3053 col = regmatch->endpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003054 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003056 // Don't go past the end of the line. Matters for "rs=e+2" when there
3057 // is a matchgroup. Watch out for match with last NL in the buffer.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058 if (result->lnum > syn_buf->b_ml.ml_line_count)
3059 col = 0;
3060 else if (off != 0)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003061 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003062 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3063 p = base + col;
3064 if (off > 0)
3065 {
3066 while (off-- > 0 && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003067 MB_PTR_ADV(p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003068 }
3069 else if (off < 0)
3070 {
3071 while (off++ < 0 && base < p)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003072 MB_PTR_BACK(base, p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073 }
3074 col = (int)(p - base);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003075 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077}
3078
3079/*
3080 * Add offset to matched text for start of match or highlight.
3081 * Avoid resulting column to become negative.
3082 */
3083 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003084syn_add_start_off(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003085 lpos_T *result, // returned position
3086 regmmatch_T *regmatch, // start/end of match
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003087 synpat_T *spp,
3088 int idx,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003089 int extra) // extra chars for offset to end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092 int off;
3093 char_u *base;
3094 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
3097 {
3098 result->lnum = regmatch->endpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003099 col = regmatch->endpos[0].col;
3100 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
3102 else
3103 {
3104 result->lnum = regmatch->startpos[0].lnum;
3105 col = regmatch->startpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003106 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003108 if (result->lnum > syn_buf->b_ml.ml_line_count)
3109 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003110 // a "\n" at the end of the pattern may take us below the last line
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003111 result->lnum = syn_buf->b_ml.ml_line_count;
Bram Moolenaar8b9c05f2010-03-02 17:54:33 +01003112 col = (int)STRLEN(ml_get_buf(syn_buf, result->lnum, FALSE));
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003113 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003114 if (off != 0)
3115 {
3116 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3117 p = base + col;
3118 if (off > 0)
3119 {
3120 while (off-- && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003121 MB_PTR_ADV(p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122 }
3123 else if (off < 0)
3124 {
3125 while (off++ && base < p)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003126 MB_PTR_BACK(base, p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127 }
3128 col = (int)(p - base);
3129 }
3130 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
3133/*
3134 * Get current line in syntax buffer.
3135 */
3136 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003137syn_getcurline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
3139 return ml_get_buf(syn_buf, current_lnum, FALSE);
3140}
3141
3142/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003143 * Call vim_regexec() to find a match with "rmp" in "syn_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Returns TRUE when there is a match.
3145 */
3146 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003147syn_regexec(
3148 regmmatch_T *rmp,
3149 linenr_T lnum,
3150 colnr_T col,
3151 syn_time_T *st UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003153 int r;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003154 int timed_out = FALSE;
Bram Moolenaarf7512552013-06-06 14:55:19 +02003155#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003156 proftime_T pt;
3157
3158 if (syn_time_on)
3159 profile_start(&pt);
3160#endif
3161
Bram Moolenaarbcf94422018-06-23 14:21:42 +02003162 if (rmp->regprog == NULL)
3163 // This can happen if a previous call to vim_regexec_multi() tried to
3164 // use the NFA engine, which resulted in NFA_TOO_EXPENSIVE, and
3165 // compiling the pattern with the other engine fails.
3166 return FALSE;
3167
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003168 rmp->rmm_maxcol = syn_buf->b_p_smc;
Paul Ollis65745772022-06-05 16:55:54 +01003169 r = vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col, &timed_out);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003170
Bram Moolenaarf7512552013-06-06 14:55:19 +02003171#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003172 if (syn_time_on)
3173 {
3174 profile_end(&pt);
3175 profile_add(&st->total, &pt);
3176 if (profile_cmp(&pt, &st->slowest) < 0)
3177 st->slowest = pt;
3178 ++st->count;
3179 if (r > 0)
3180 ++st->match;
3181 }
3182#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003183#ifdef FEAT_RELTIME
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003184 if (timed_out && !syn_win->w_s->b_syn_slow)
3185 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003186 syn_win->w_s->b_syn_slow = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003187 msg(_("'redrawtime' exceeded, syntax highlighting disabled"));
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003188 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003189#endif
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003190
3191 if (r > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 rmp->startpos[0].lnum += lnum;
3194 rmp->endpos[0].lnum += lnum;
3195 return TRUE;
3196 }
3197 return FALSE;
3198}
3199
3200/*
3201 * Check one position in a line for a matching keyword.
3202 * The caller must check if a keyword can start at startcol.
Bram Moolenaaraab93b12017-03-18 21:37:28 +01003203 * Return its ID if found, 0 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 */
3205 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003206check_keyword_id(
3207 char_u *line,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003208 int startcol, // position in line to check for keyword
3209 int *endcolp, // return: character after found keyword
3210 long *flagsp, // return: flags of matching keyword
3211 short **next_listp, // return: next_list of matching keyword
3212 stateitem_T *cur_si, // item at the top of the stack
3213 int *ccharp UNUSED) // conceal substitution char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
Bram Moolenaardad6b692005-01-25 22:14:34 +00003215 keyentry_T *kp;
3216 char_u *kwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 int round;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003218 int kwlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003219 char_u keyword[MAXKEYWLEN + 1]; // assume max. keyword len is 80
Bram Moolenaardad6b692005-01-25 22:14:34 +00003220 hashtab_T *ht;
3221 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003223 // Find first character after the keyword. First character was already
3224 // checked.
Bram Moolenaardad6b692005-01-25 22:14:34 +00003225 kwp = line + startcol;
3226 kwlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 do
3228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003230 kwlen += (*mb_ptr2len)(kwp + kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 else
Bram Moolenaardad6b692005-01-25 22:14:34 +00003232 ++kwlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01003234 while (vim_iswordp_buf(kwp + kwlen, syn_buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
Bram Moolenaardad6b692005-01-25 22:14:34 +00003236 if (kwlen > MAXKEYWLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 return 0;
3238
3239 /*
3240 * Must make a copy of the keyword, so we can add a NUL and make it
3241 * lowercase.
3242 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003243 vim_strncpy(keyword, kwp, kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 /*
3246 * Try twice:
3247 * 1. matching case
3248 * 2. ignoring case
3249 */
3250 for (round = 1; round <= 2; ++round)
3251 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003252 ht = round == 1 ? &syn_block->b_keywtab : &syn_block->b_keywtab_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003253 if (ht->ht_used == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003255 if (round == 2) // ignore case
Bram Moolenaardad6b692005-01-25 22:14:34 +00003256 (void)str_foldcase(kwp, kwlen, keyword, MAXKEYWLEN + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 /*
Bram Moolenaardad6b692005-01-25 22:14:34 +00003259 * Find keywords that match. There can be several with different
3260 * attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 * When current_next_list is non-zero accept only that group, otherwise:
3262 * Accept a not-contained keyword at toplevel.
3263 * Accept a keyword at other levels only if it is in the contains list.
3264 */
Bram Moolenaardad6b692005-01-25 22:14:34 +00003265 hi = hash_find(ht, keyword);
3266 if (!HASHITEM_EMPTY(hi))
3267 for (kp = HI2KE(hi); kp != NULL; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00003269 if (current_next_list != 0
3270 ? in_id_list(NULL, current_next_list, &kp->k_syn, 0)
3271 : (cur_si == NULL
3272 ? !(kp->flags & HL_CONTAINED)
3273 : in_id_list(cur_si, cur_si->si_cont_list,
3274 &kp->k_syn, kp->flags & HL_CONTAINED)))
3275 {
3276 *endcolp = startcol + kwlen;
3277 *flagsp = kp->flags;
3278 *next_listp = kp->next_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003279#ifdef FEAT_CONCEAL
3280 *ccharp = kp->k_char;
3281#endif
Bram Moolenaardad6b692005-01-25 22:14:34 +00003282 return kp->k_syn.id;
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285 }
3286 return 0;
3287}
3288
3289/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02003290 * Handle ":syntax conceal" command.
3291 */
3292 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003293syn_cmd_conceal(exarg_T *eap UNUSED, int syncing UNUSED)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003294{
3295#ifdef FEAT_CONCEAL
3296 char_u *arg = eap->arg;
3297 char_u *next;
3298
3299 eap->nextcmd = find_nextcmd(arg);
3300 if (eap->skip)
3301 return;
3302
3303 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003304 if (*arg == NUL)
3305 {
3306 if (curwin->w_s->b_syn_conceal)
Bram Moolenaar0c1550d2022-02-06 11:41:57 +00003307 msg("syntax conceal on");
Bram Moolenaarde318c52017-01-17 16:27:10 +01003308 else
Bram Moolenaar0c1550d2022-02-06 11:41:57 +00003309 msg("syntax conceal off");
Bram Moolenaarde318c52017-01-17 16:27:10 +01003310 }
3311 else if (STRNICMP(arg, "on", 2) == 0 && next - arg == 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003312 curwin->w_s->b_syn_conceal = TRUE;
3313 else if (STRNICMP(arg, "off", 3) == 0 && next - arg == 3)
3314 curwin->w_s->b_syn_conceal = FALSE;
3315 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003316 semsg(_(e_illegal_argument_str_2), arg);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003317#endif
3318}
3319
3320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 * Handle ":syntax case" command.
3322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003324syn_cmd_case(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 char_u *arg = eap->arg;
3327 char_u *next;
3328
3329 eap->nextcmd = find_nextcmd(arg);
3330 if (eap->skip)
3331 return;
3332
3333 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003334 if (*arg == NUL)
3335 {
3336 if (curwin->w_s->b_syn_ic)
Bram Moolenaar0c1550d2022-02-06 11:41:57 +00003337 msg("syntax case ignore");
Bram Moolenaarde318c52017-01-17 16:27:10 +01003338 else
Bram Moolenaar0c1550d2022-02-06 11:41:57 +00003339 msg("syntax case match");
Bram Moolenaarde318c52017-01-17 16:27:10 +01003340 }
3341 else if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342 curwin->w_s->b_syn_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003344 curwin->w_s->b_syn_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003346 semsg(_(e_illegal_argument_str_2), arg);
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003347}
3348
3349/*
3350 * Handle ":syntax foldlevel" command.
3351 */
3352 static void
3353syn_cmd_foldlevel(exarg_T *eap, int syncing UNUSED)
3354{
3355 char_u *arg = eap->arg;
3356 char_u *arg_end;
3357
3358 eap->nextcmd = find_nextcmd(arg);
3359 if (eap->skip)
3360 return;
3361
3362 if (*arg == NUL)
3363 {
3364 switch (curwin->w_s->b_syn_foldlevel)
3365 {
Dominique Pellecd53eed2022-02-05 18:53:06 +00003366 case SYNFLD_START: msg("syntax foldlevel start"); break;
3367 case SYNFLD_MINIMUM: msg("syntax foldlevel minimum"); break;
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003368 default: break;
3369 }
3370 return;
3371 }
3372
3373 arg_end = skiptowhite(arg);
3374 if (STRNICMP(arg, "start", 5) == 0 && arg_end - arg == 5)
3375 curwin->w_s->b_syn_foldlevel = SYNFLD_START;
3376 else if (STRNICMP(arg, "minimum", 7) == 0 && arg_end - arg == 7)
3377 curwin->w_s->b_syn_foldlevel = SYNFLD_MINIMUM;
3378 else
3379 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003380 semsg(_(e_illegal_argument_str_2), arg);
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003381 return;
3382 }
3383
3384 arg = skipwhite(arg_end);
3385 if (*arg != NUL)
3386 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003387 semsg(_(e_illegal_argument_str_2), arg);
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389}
3390
3391/*
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003392 * Handle ":syntax spell" command.
3393 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003394 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003395syn_cmd_spell(exarg_T *eap, int syncing UNUSED)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003396{
3397 char_u *arg = eap->arg;
3398 char_u *next;
3399
3400 eap->nextcmd = find_nextcmd(arg);
3401 if (eap->skip)
3402 return;
3403
3404 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003405 if (*arg == NUL)
3406 {
3407 if (curwin->w_s->b_syn_spell == SYNSPL_TOP)
Dominique Pellecd53eed2022-02-05 18:53:06 +00003408 msg("syntax spell toplevel");
Bram Moolenaarde318c52017-01-17 16:27:10 +01003409 else if (curwin->w_s->b_syn_spell == SYNSPL_NOTOP)
Dominique Pellecd53eed2022-02-05 18:53:06 +00003410 msg("syntax spell notoplevel");
Bram Moolenaarde318c52017-01-17 16:27:10 +01003411 else
Dominique Pellecd53eed2022-02-05 18:53:06 +00003412 msg("syntax spell default");
Bram Moolenaarde318c52017-01-17 16:27:10 +01003413 }
3414 else if (STRNICMP(arg, "toplevel", 8) == 0 && next - arg == 8)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003415 curwin->w_s->b_syn_spell = SYNSPL_TOP;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003416 else if (STRNICMP(arg, "notoplevel", 10) == 0 && next - arg == 10)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003417 curwin->w_s->b_syn_spell = SYNSPL_NOTOP;
Bram Moolenaar86ea7642007-02-04 01:48:10 +00003418 else if (STRNICMP(arg, "default", 7) == 0 && next - arg == 7)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003419 curwin->w_s->b_syn_spell = SYNSPL_DEFAULT;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003420 else
Bram Moolenaar5081d202015-06-25 18:36:26 +02003421 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003422 semsg(_(e_illegal_argument_str_2), arg);
Bram Moolenaar5081d202015-06-25 18:36:26 +02003423 return;
3424 }
3425
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003426 // assume spell checking changed, force a redraw
Bram Moolenaar5081d202015-06-25 18:36:26 +02003427 redraw_win_later(curwin, NOT_VALID);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003428}
3429
3430/*
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003431 * Handle ":syntax iskeyword" command.
3432 */
3433 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003434syn_cmd_iskeyword(exarg_T *eap, int syncing UNUSED)
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003435{
3436 char_u *arg = eap->arg;
3437 char_u save_chartab[32];
3438 char_u *save_isk;
3439
3440 if (eap->skip)
3441 return;
3442
3443 arg = skipwhite(arg);
3444 if (*arg == NUL)
3445 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003446 msg_puts("\n");
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003447 if (curwin->w_s->b_syn_isk != empty_option)
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003448 {
Bram Moolenaar0c1550d2022-02-06 11:41:57 +00003449 msg_puts("syntax iskeyword ");
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003450 msg_outtrans(curwin->w_s->b_syn_isk);
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003451 }
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003452 else
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003453 msg_outtrans((char_u *)_("syntax iskeyword not set"));
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003454 }
3455 else
3456 {
3457 if (STRNICMP(arg, "clear", 5) == 0)
3458 {
3459 mch_memmove(curwin->w_s->b_syn_chartab, curbuf->b_chartab,
3460 (size_t)32);
3461 clear_string_option(&curwin->w_s->b_syn_isk);
3462 }
3463 else
3464 {
3465 mch_memmove(save_chartab, curbuf->b_chartab, (size_t)32);
3466 save_isk = curbuf->b_p_isk;
3467 curbuf->b_p_isk = vim_strsave(arg);
3468
3469 buf_init_chartab(curbuf, FALSE);
3470 mch_memmove(curwin->w_s->b_syn_chartab, curbuf->b_chartab,
3471 (size_t)32);
3472 mch_memmove(curbuf->b_chartab, save_chartab, (size_t)32);
3473 clear_string_option(&curwin->w_s->b_syn_isk);
3474 curwin->w_s->b_syn_isk = curbuf->b_p_isk;
3475 curbuf->b_p_isk = save_isk;
3476 }
3477 }
3478 redraw_win_later(curwin, NOT_VALID);
3479}
3480
3481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * Clear all syntax info for one buffer.
3483 */
3484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003485syntax_clear(synblock_T *block)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 int i;
3488
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003489 block->b_syn_error = FALSE; // clear previous error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003490#ifdef FEAT_RELTIME
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003491 block->b_syn_slow = FALSE; // clear previous timeout
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003492#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003493 block->b_syn_ic = FALSE; // Use case, by default
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003494 block->b_syn_foldlevel = SYNFLD_START;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003495 block->b_syn_spell = SYNSPL_DEFAULT; // default spell checking
Bram Moolenaar860cae12010-06-05 23:22:07 +02003496 block->b_syn_containedin = FALSE;
Bram Moolenaarde318c52017-01-17 16:27:10 +01003497#ifdef FEAT_CONCEAL
3498 block->b_syn_conceal = FALSE;
3499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003501 // free the keywords
Bram Moolenaar860cae12010-06-05 23:22:07 +02003502 clear_keywtab(&block->b_keywtab);
3503 clear_keywtab(&block->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003505 // free the syntax patterns
Bram Moolenaar860cae12010-06-05 23:22:07 +02003506 for (i = block->b_syn_patterns.ga_len; --i >= 0; )
3507 syn_clear_pattern(block, i);
3508 ga_clear(&block->b_syn_patterns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003510 // free the syntax clusters
Bram Moolenaar860cae12010-06-05 23:22:07 +02003511 for (i = block->b_syn_clusters.ga_len; --i >= 0; )
3512 syn_clear_cluster(block, i);
3513 ga_clear(&block->b_syn_clusters);
3514 block->b_spell_cluster_id = 0;
3515 block->b_nospell_cluster_id = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar860cae12010-06-05 23:22:07 +02003517 block->b_syn_sync_flags = 0;
3518 block->b_syn_sync_minlines = 0;
3519 block->b_syn_sync_maxlines = 0;
3520 block->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar473de612013-06-08 18:19:48 +02003522 vim_regfree(block->b_syn_linecont_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003523 block->b_syn_linecont_prog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003524 VIM_CLEAR(block->b_syn_linecont_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#ifdef FEAT_FOLDING
Bram Moolenaar860cae12010-06-05 23:22:07 +02003526 block->b_syn_folditems = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527#endif
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003528 clear_string_option(&block->b_syn_isk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003530 // free the stored states
Bram Moolenaar860cae12010-06-05 23:22:07 +02003531 syn_stack_free_all(block);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 invalidate_current_state();
Bram Moolenaar42431a72011-04-01 14:44:59 +02003533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003534 // Reset the counter for ":syn include"
Bram Moolenaar42431a72011-04-01 14:44:59 +02003535 running_syn_inc_tag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536}
3537
3538/*
Bram Moolenaarfd29f462010-06-06 16:11:09 +02003539 * Get rid of ownsyntax for window "wp".
3540 */
3541 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003542reset_synblock(win_T *wp)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02003543{
3544 if (wp->w_s != &wp->w_buffer->b_s)
3545 {
3546 syntax_clear(wp->w_s);
3547 vim_free(wp->w_s);
3548 wp->w_s = &wp->w_buffer->b_s;
3549 }
3550}
3551
3552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 * Clear syncing info for one buffer.
3554 */
3555 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003556syntax_sync_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557{
3558 int i;
3559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003560 // free the syntax patterns
Bram Moolenaar860cae12010-06-05 23:22:07 +02003561 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
3562 if (SYN_ITEMS(curwin->w_s)[i].sp_syncing)
3563 syn_remove_pattern(curwin->w_s, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar860cae12010-06-05 23:22:07 +02003565 curwin->w_s->b_syn_sync_flags = 0;
3566 curwin->w_s->b_syn_sync_minlines = 0;
3567 curwin->w_s->b_syn_sync_maxlines = 0;
3568 curwin->w_s->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar473de612013-06-08 18:19:48 +02003570 vim_regfree(curwin->w_s->b_syn_linecont_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003571 curwin->w_s->b_syn_linecont_prog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003572 VIM_CLEAR(curwin->w_s->b_syn_linecont_pat);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003573 clear_string_option(&curwin->w_s->b_syn_isk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003575 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576}
3577
3578/*
3579 * Remove one pattern from the buffer's pattern list.
3580 */
3581 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003582syn_remove_pattern(
3583 synblock_T *block,
3584 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
3586 synpat_T *spp;
3587
Bram Moolenaar860cae12010-06-05 23:22:07 +02003588 spp = &(SYN_ITEMS(block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589#ifdef FEAT_FOLDING
3590 if (spp->sp_flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 --block->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593 syn_clear_pattern(block, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 mch_memmove(spp, spp + 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595 sizeof(synpat_T) * (block->b_syn_patterns.ga_len - idx - 1));
3596 --block->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597}
3598
3599/*
3600 * Clear and free one syntax pattern. When clearing all, must be called from
3601 * last to first!
3602 */
3603 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003604syn_clear_pattern(synblock_T *block, int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 vim_free(SYN_ITEMS(block)[i].sp_pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02003607 vim_regfree(SYN_ITEMS(block)[i].sp_prog);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003608 // Only free sp_cont_list and sp_next_list of first start pattern
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 if (i == 0 || SYN_ITEMS(block)[i - 1].sp_type != SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003611 vim_free(SYN_ITEMS(block)[i].sp_cont_list);
3612 vim_free(SYN_ITEMS(block)[i].sp_next_list);
3613 vim_free(SYN_ITEMS(block)[i].sp_syn.cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
3615}
3616
3617/*
3618 * Clear and free one syntax cluster.
3619 */
3620 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003621syn_clear_cluster(synblock_T *block, int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003623 vim_free(SYN_CLSTR(block)[i].scl_name);
3624 vim_free(SYN_CLSTR(block)[i].scl_name_u);
3625 vim_free(SYN_CLSTR(block)[i].scl_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626}
3627
3628/*
3629 * Handle ":syntax clear" command.
3630 */
3631 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003632syn_cmd_clear(exarg_T *eap, int syncing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 char_u *arg = eap->arg;
3635 char_u *arg_end;
3636 int id;
3637
3638 eap->nextcmd = find_nextcmd(arg);
3639 if (eap->skip)
3640 return;
3641
3642 /*
3643 * We have to disable this within ":syn include @group filename",
3644 * because otherwise @group would get deleted.
3645 * Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
3646 * clear".
3647 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003648 if (curwin->w_s->b_syn_topgrp != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return;
3650
Bram Moolenaar1966c242020-04-20 22:42:32 +02003651 if (ends_excmd2(eap->cmd, arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 /*
3654 * No argument: Clear all syntax items.
3655 */
3656 if (syncing)
3657 syntax_sync_clear();
3658 else
3659 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003660 syntax_clear(curwin->w_s);
3661 if (curwin->w_s == &curwin->w_buffer->b_s)
3662 do_unlet((char_u *)"b:current_syntax", TRUE);
Bram Moolenaar1950c352010-06-06 15:21:10 +02003663 do_unlet((char_u *)"w:current_syntax", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665 }
3666 else
3667 {
3668 /*
3669 * Clear the group IDs that are in the argument.
3670 */
Bram Moolenaar1966c242020-04-20 22:42:32 +02003671 while (!ends_excmd2(eap->cmd, arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 arg_end = skiptowhite(arg);
3674 if (*arg == '@')
3675 {
3676 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3677 if (id == 0)
3678 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003679 semsg(_(e_no_such_syntax_cluster_1), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 break;
3681 }
3682 else
3683 {
3684 /*
3685 * We can't physically delete a cluster without changing
3686 * the IDs of other clusters, so we do the next best thing
3687 * and make it empty.
3688 */
3689 short scl_id = id - SYNID_CLUSTER;
3690
Bram Moolenaard23a8232018-02-10 18:45:26 +01003691 VIM_CLEAR(SYN_CLSTR(curwin->w_s)[scl_id].scl_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
3693 }
3694 else
3695 {
3696 id = syn_namen2id(arg, (int)(arg_end - arg));
3697 if (id == 0)
3698 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003699 semsg(_(e_no_such_highlight_group_name_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 break;
3701 }
3702 else
3703 syn_clear_one(id, syncing);
3704 }
3705 arg = skipwhite(arg_end);
3706 }
3707 }
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00003708 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003709 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710}
3711
3712/*
3713 * Clear one syntax group for the current buffer.
3714 */
3715 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003716syn_clear_one(int id, int syncing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
3718 synpat_T *spp;
3719 int idx;
3720
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003721 // Clear keywords only when not ":syn sync clear group-name"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (!syncing)
3723 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003724 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab);
3725 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003728 // clear the patterns for "id"
Bram Moolenaar860cae12010-06-05 23:22:07 +02003729 for (idx = curwin->w_s->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003731 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3733 continue;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003734 syn_remove_pattern(curwin->w_s, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736}
3737
3738/*
3739 * Handle ":syntax on" command.
3740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003742syn_cmd_on(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 syn_cmd_onoff(eap, "syntax");
3745}
3746
3747/*
3748 * Handle ":syntax enable" command.
3749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003751syn_cmd_enable(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
Bram Moolenaarc8a9fe52021-11-20 19:50:59 +00003753 set_internal_string_var((char_u *)"g:syntax_cmd", (char_u *)"enable");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 syn_cmd_onoff(eap, "syntax");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003755 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756}
3757
3758/*
3759 * Handle ":syntax reset" command.
Bram Moolenaar8bc189e2016-04-02 19:01:58 +02003760 * It actually resets highlighting, not syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003763syn_cmd_reset(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
Bram Moolenaar63b91732021-08-05 20:40:03 +02003765 set_nextcmd(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (!eap->skip)
3767 {
Bram Moolenaarc8a9fe52021-11-20 19:50:59 +00003768 set_internal_string_var((char_u *)"g:syntax_cmd", (char_u *)"reset");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003770 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772}
3773
3774/*
3775 * Handle ":syntax manual" command.
3776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003778syn_cmd_manual(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 syn_cmd_onoff(eap, "manual");
3781}
3782
3783/*
3784 * Handle ":syntax off" command.
3785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003787syn_cmd_off(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788{
3789 syn_cmd_onoff(eap, "nosyntax");
3790}
3791
3792 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003793syn_cmd_onoff(exarg_T *eap, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
3795 char_u buf[100];
3796
Bram Moolenaar63b91732021-08-05 20:40:03 +02003797 set_nextcmd(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (!eap->skip)
3799 {
3800 STRCPY(buf, "so ");
Bram Moolenaar555b2802005-05-19 21:08:39 +00003801 vim_snprintf((char *)buf + 3, sizeof(buf) - 3, SYNTAX_FNAME, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 do_cmdline_cmd(buf);
3803 }
3804}
3805
3806/*
3807 * Handle ":syntax [list]" command: list current syntax words.
3808 */
3809 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003810syn_cmd_list(
3811 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003812 int syncing) // when TRUE: list syncing items
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813{
3814 char_u *arg = eap->arg;
3815 int id;
3816 char_u *arg_end;
3817
3818 eap->nextcmd = find_nextcmd(arg);
3819 if (eap->skip)
3820 return;
3821
Bram Moolenaar860cae12010-06-05 23:22:07 +02003822 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003824 msg(_(msg_no_items));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return;
3826 }
3827
3828 if (syncing)
3829 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003830 if (curwin->w_s->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003832 msg_puts(_("syncing on C-style comments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 syn_lines_msg();
3834 syn_match_msg();
3835 return;
3836 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003837 else if (!(curwin->w_s->b_syn_sync_flags & SF_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003839 if (curwin->w_s->b_syn_sync_minlines == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003840 msg_puts(_("no syncing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 else
3842 {
Bram Moolenaar99502802020-11-18 16:53:23 +01003843 if (curwin->w_s->b_syn_sync_minlines == MAXLNUM)
3844 msg_puts(_("syncing starts at the first line"));
3845 else
3846 {
3847 msg_puts(_("syncing starts "));
3848 msg_outnum(curwin->w_s->b_syn_sync_minlines);
3849 msg_puts(_(" lines before top line"));
3850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 syn_match_msg();
3852 }
3853 return;
3854 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003855 msg_puts_title(_("\n--- Syntax sync items ---"));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003856 if (curwin->w_s->b_syn_sync_minlines > 0
3857 || curwin->w_s->b_syn_sync_maxlines > 0
3858 || curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003860 msg_puts(_("\nsyncing on items"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 syn_lines_msg();
3862 syn_match_msg();
3863 }
3864 }
3865 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003866 msg_puts_title(_("\n--- Syntax items ---"));
Bram Moolenaar1966c242020-04-20 22:42:32 +02003867 if (ends_excmd2(eap->cmd, arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 /*
3870 * No argument: List all group IDs and all syntax clusters.
3871 */
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02003872 for (id = 1; id <= highlight_num_groups() && !got_int; ++id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 syn_list_one(id, syncing, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003874 for (id = 0; id < curwin->w_s->b_syn_clusters.ga_len && !got_int; ++id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 syn_list_cluster(id);
3876 }
3877 else
3878 {
3879 /*
3880 * List the group IDs and syntax clusters that are in the argument.
3881 */
Bram Moolenaar1966c242020-04-20 22:42:32 +02003882 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884 arg_end = skiptowhite(arg);
3885 if (*arg == '@')
3886 {
3887 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3888 if (id == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003889 semsg(_(e_no_such_syntax_cluster_2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 else
3891 syn_list_cluster(id - SYNID_CLUSTER);
3892 }
3893 else
3894 {
3895 id = syn_namen2id(arg, (int)(arg_end - arg));
3896 if (id == 0)
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003897 semsg(_(e_no_such_highlight_group_name_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
3899 syn_list_one(id, syncing, TRUE);
3900 }
3901 arg = skipwhite(arg_end);
3902 }
3903 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003904 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905}
3906
3907 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003908syn_lines_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003910 if (curwin->w_s->b_syn_sync_maxlines > 0
3911 || curwin->w_s->b_syn_sync_minlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003913 msg_puts("; ");
Bram Moolenaar99502802020-11-18 16:53:23 +01003914 if (curwin->w_s->b_syn_sync_minlines == MAXLNUM)
3915 msg_puts(_("from the first line"));
3916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar99502802020-11-18 16:53:23 +01003918 if (curwin->w_s->b_syn_sync_minlines > 0)
3919 {
3920 msg_puts(_("minimal "));
3921 msg_outnum(curwin->w_s->b_syn_sync_minlines);
3922 if (curwin->w_s->b_syn_sync_maxlines)
3923 msg_puts(", ");
3924 }
3925 if (curwin->w_s->b_syn_sync_maxlines > 0)
3926 {
3927 msg_puts(_("maximal "));
3928 msg_outnum(curwin->w_s->b_syn_sync_maxlines);
3929 }
3930 msg_puts(_(" lines before top line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933}
3934
3935 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003936syn_match_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003938 if (curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003940 msg_puts(_("; match "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003941 msg_outnum(curwin->w_s->b_syn_sync_linebreaks);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003942 msg_puts(_(" line breaks"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944}
3945
3946static int last_matchgroup;
3947
3948struct name_list
3949{
3950 int flag;
3951 char *name;
3952};
3953
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003954static void syn_list_flags(struct name_list *nl, int flags, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
3956/*
3957 * List one syntax item, for ":syntax" or "syntax list syntax_name".
3958 */
3959 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003960syn_list_one(
3961 int id,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003962 int syncing, // when TRUE: list syncing items
3963 int link_only) // when TRUE; list link-only too
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 int attr;
3966 int idx;
3967 int did_header = FALSE;
3968 synpat_T *spp;
3969 static struct name_list namelist1[] =
3970 {
3971 {HL_DISPLAY, "display"},
3972 {HL_CONTAINED, "contained"},
3973 {HL_ONELINE, "oneline"},
3974 {HL_KEEPEND, "keepend"},
3975 {HL_EXTEND, "extend"},
3976 {HL_EXCLUDENL, "excludenl"},
3977 {HL_TRANSP, "transparent"},
3978 {HL_FOLD, "fold"},
Bram Moolenaar860cae12010-06-05 23:22:07 +02003979#ifdef FEAT_CONCEAL
3980 {HL_CONCEAL, "conceal"},
3981 {HL_CONCEALENDS, "concealends"},
3982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {0, NULL}
3984 };
3985 static struct name_list namelist2[] =
3986 {
3987 {HL_SKIPWHITE, "skipwhite"},
3988 {HL_SKIPNL, "skipnl"},
3989 {HL_SKIPEMPTY, "skipempty"},
3990 {0, NULL}
3991 };
3992
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003993 attr = HL_ATTR(HLF_D); // highlight like directories
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003995 // list the keywords for "id"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (!syncing)
3997 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003998 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab, FALSE, attr);
3999 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 did_header, attr);
4001 }
4002
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004003 // list the patterns for "id"
Bram Moolenaar860cae12010-06-05 23:22:07 +02004004 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len && !got_int; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004006 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
4008 continue;
4009
4010 (void)syn_list_header(did_header, 999, id);
4011 did_header = TRUE;
4012 last_matchgroup = 0;
4013 if (spp->sp_type == SPTYPE_MATCH)
4014 {
4015 put_pattern("match", ' ', spp, attr);
4016 msg_putchar(' ');
4017 }
4018 else if (spp->sp_type == SPTYPE_START)
4019 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004020 while (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_START)
4021 put_pattern("start", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
4022 if (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_SKIP)
4023 put_pattern("skip", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
4024 while (idx < curwin->w_s->b_syn_patterns.ga_len
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004025 && SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_END)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004026 put_pattern("end", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 --idx;
4028 msg_putchar(' ');
4029 }
4030 syn_list_flags(namelist1, spp->sp_flags, attr);
4031
4032 if (spp->sp_cont_list != NULL)
4033 put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
4034
4035 if (spp->sp_syn.cont_in_list != NULL)
4036 put_id_list((char_u *)"containedin",
4037 spp->sp_syn.cont_in_list, attr);
4038
4039 if (spp->sp_next_list != NULL)
4040 {
4041 put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
4042 syn_list_flags(namelist2, spp->sp_flags, attr);
4043 }
4044 if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
4045 {
4046 if (spp->sp_flags & HL_SYNC_HERE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004047 msg_puts_attr("grouphere", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004049 msg_puts_attr("groupthere", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 msg_putchar(' ');
4051 if (spp->sp_sync_idx >= 0)
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004052 msg_outtrans(highlight_group_name(SYN_ITEMS(curwin->w_s)
4053 [spp->sp_sync_idx].sp_syn.id - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004055 msg_puts("NONE");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 msg_putchar(' ');
4057 }
4058 }
4059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004060 // list the link, if there is one
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004061 if (highlight_link_id(id - 1) && (did_header || link_only) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
4063 (void)syn_list_header(did_header, 999, id);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004064 msg_puts_attr("links to", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 msg_putchar(' ');
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004066 msg_outtrans(highlight_group_name(highlight_link_id(id - 1) - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068}
4069
4070 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004071syn_list_flags(struct name_list *nlist, int flags, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
4073 int i;
4074
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004075 for (i = 0; nlist[i].flag != 0; ++i)
4076 if (flags & nlist[i].flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004078 msg_puts_attr(nlist[i].name, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 msg_putchar(' ');
4080 }
4081}
4082
4083/*
4084 * List one syntax cluster, for ":syntax" or "syntax list syntax_name".
4085 */
4086 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004087syn_list_cluster(int id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
4089 int endcol = 15;
4090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004091 // slight hack: roughly duplicate the guts of syn_list_header()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 msg_putchar('\n');
Bram Moolenaar860cae12010-06-05 23:22:07 +02004093 msg_outtrans(SYN_CLSTR(curwin->w_s)[id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004095 if (msg_col >= endcol) // output at least one space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 endcol = msg_col + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004097 if (Columns <= endcol) // avoid hang for tiny window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 endcol = Columns - 1;
4099
4100 msg_advance(endcol);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004101 if (SYN_CLSTR(curwin->w_s)[id].scl_list != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004103 put_id_list((char_u *)"cluster", SYN_CLSTR(curwin->w_s)[id].scl_list,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004104 HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 else
4107 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004108 msg_puts_attr("cluster", HL_ATTR(HLF_D));
4109 msg_puts("=NONE");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111}
4112
4113 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004114put_id_list(char_u *name, short *list, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 short *p;
4117
Bram Moolenaar32526b32019-01-19 17:43:09 +01004118 msg_puts_attr((char *)name, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 msg_putchar('=');
4120 for (p = list; *p; ++p)
4121 {
4122 if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
4123 {
4124 if (p[1])
Bram Moolenaar32526b32019-01-19 17:43:09 +01004125 msg_puts("ALLBUT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004127 msg_puts("ALL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
4130 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004131 msg_puts("TOP");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
4134 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004135 msg_puts("CONTAINED");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 else if (*p >= SYNID_CLUSTER)
4138 {
4139 short scl_id = *p - SYNID_CLUSTER;
4140
4141 msg_putchar('@');
Bram Moolenaar860cae12010-06-05 23:22:07 +02004142 msg_outtrans(SYN_CLSTR(curwin->w_s)[scl_id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 else
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004145 msg_outtrans(highlight_group_name(*p - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 if (p[1])
4147 msg_putchar(',');
4148 }
4149 msg_putchar(' ');
4150}
4151
4152 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004153put_pattern(
4154 char *s,
4155 int c,
4156 synpat_T *spp,
4157 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
4159 long n;
4160 int mask;
4161 int first;
4162 static char *sepchars = "/+=-#@\"|'^&";
4163 int i;
4164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004165 // May have to write "matchgroup=group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (last_matchgroup != spp->sp_syn_match_id)
4167 {
4168 last_matchgroup = spp->sp_syn_match_id;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004169 msg_puts_attr("matchgroup", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 msg_putchar('=');
4171 if (last_matchgroup == 0)
4172 msg_outtrans((char_u *)"NONE");
4173 else
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004174 msg_outtrans(highlight_group_name(last_matchgroup - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 msg_putchar(' ');
4176 }
4177
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004178 // output the name of the pattern and an '=' or ' '
Bram Moolenaar32526b32019-01-19 17:43:09 +01004179 msg_puts_attr(s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 msg_putchar(c);
4181
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004182 // output the pattern, in between a char that is not in the pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 for (i = 0; vim_strchr(spp->sp_pattern, sepchars[i]) != NULL; )
4184 if (sepchars[++i] == NUL)
4185 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004186 i = 0; // no good char found, just use the first one
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 break;
4188 }
4189 msg_putchar(sepchars[i]);
4190 msg_outtrans(spp->sp_pattern);
4191 msg_putchar(sepchars[i]);
4192
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004193 // output any pattern options
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 first = TRUE;
4195 for (i = 0; i < SPO_COUNT; ++i)
4196 {
4197 mask = (1 << i);
4198 if (spp->sp_off_flags & (mask + (mask << SPO_COUNT)))
4199 {
4200 if (!first)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004201 msg_putchar(','); // separate with commas
Bram Moolenaar32526b32019-01-19 17:43:09 +01004202 msg_puts(spo_name_tab[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 n = spp->sp_offsets[i];
4204 if (i != SPO_LC_OFF)
4205 {
4206 if (spp->sp_off_flags & mask)
4207 msg_putchar('s');
4208 else
4209 msg_putchar('e');
4210 if (n > 0)
4211 msg_putchar('+');
4212 }
4213 if (n || i == SPO_LC_OFF)
4214 msg_outnum(n);
4215 first = FALSE;
4216 }
4217 }
4218 msg_putchar(' ');
4219}
4220
4221/*
4222 * List or clear the keywords for one syntax group.
4223 * Return TRUE if the header has been printed.
4224 */
4225 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004226syn_list_keywords(
4227 int id,
4228 hashtab_T *ht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004229 int did_header, // header has already been printed
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004230 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 int outlen;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004233 hashitem_T *hi;
4234 keyentry_T *kp;
4235 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 int prev_contained = 0;
4237 short *prev_next_list = NULL;
4238 short *prev_cont_in_list = NULL;
4239 int prev_skipnl = 0;
4240 int prev_skipwhite = 0;
4241 int prev_skipempty = 0;
4242
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 /*
4244 * Unfortunately, this list of keywords is not sorted on alphabet but on
4245 * hash value...
4246 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004248 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004250 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004252 --todo;
4253 for (kp = HI2KE(hi); kp != NULL && !got_int; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004255 if (kp->k_syn.id == id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004257 if (prev_contained != (kp->flags & HL_CONTAINED)
4258 || prev_skipnl != (kp->flags & HL_SKIPNL)
4259 || prev_skipwhite != (kp->flags & HL_SKIPWHITE)
4260 || prev_skipempty != (kp->flags & HL_SKIPEMPTY)
4261 || prev_cont_in_list != kp->k_syn.cont_in_list
4262 || prev_next_list != kp->next_list)
4263 outlen = 9999;
4264 else
4265 outlen = (int)STRLEN(kp->keyword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004266 // output "contained" and "nextgroup" on each line
Bram Moolenaardad6b692005-01-25 22:14:34 +00004267 if (syn_list_header(did_header, outlen, id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004269 prev_contained = 0;
4270 prev_next_list = NULL;
4271 prev_cont_in_list = NULL;
4272 prev_skipnl = 0;
4273 prev_skipwhite = 0;
4274 prev_skipempty = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004276 did_header = TRUE;
4277 if (prev_contained != (kp->flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004279 msg_puts_attr("contained", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004281 prev_contained = (kp->flags & HL_CONTAINED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004283 if (kp->k_syn.cont_in_list != prev_cont_in_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004285 put_id_list((char_u *)"containedin",
4286 kp->k_syn.cont_in_list, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004288 prev_cont_in_list = kp->k_syn.cont_in_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004290 if (kp->next_list != prev_next_list)
4291 {
4292 put_id_list((char_u *)"nextgroup", kp->next_list, attr);
4293 msg_putchar(' ');
4294 prev_next_list = kp->next_list;
4295 if (kp->flags & HL_SKIPNL)
4296 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004297 msg_puts_attr("skipnl", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004298 msg_putchar(' ');
4299 prev_skipnl = (kp->flags & HL_SKIPNL);
4300 }
4301 if (kp->flags & HL_SKIPWHITE)
4302 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004303 msg_puts_attr("skipwhite", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004304 msg_putchar(' ');
4305 prev_skipwhite = (kp->flags & HL_SKIPWHITE);
4306 }
4307 if (kp->flags & HL_SKIPEMPTY)
4308 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004309 msg_puts_attr("skipempty", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004310 msg_putchar(' ');
4311 prev_skipempty = (kp->flags & HL_SKIPEMPTY);
4312 }
4313 }
4314 msg_outtrans(kp->keyword);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 }
4318 }
4319
4320 return did_header;
4321}
4322
4323 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004324syn_clear_keyword(int id, hashtab_T *ht)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004326 hashitem_T *hi;
4327 keyentry_T *kp;
4328 keyentry_T *kp_prev;
4329 keyentry_T *kp_next;
4330 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
Bram Moolenaardad6b692005-01-25 22:14:34 +00004332 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004333 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004334 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004336 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004338 --todo;
4339 kp_prev = NULL;
4340 for (kp = HI2KE(hi); kp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004342 if (kp->k_syn.id == id)
4343 {
4344 kp_next = kp->ke_next;
4345 if (kp_prev == NULL)
4346 {
4347 if (kp_next == NULL)
4348 hash_remove(ht, hi);
4349 else
4350 hi->hi_key = KE2HIKEY(kp_next);
4351 }
4352 else
4353 kp_prev->ke_next = kp_next;
4354 vim_free(kp->next_list);
4355 vim_free(kp->k_syn.cont_in_list);
4356 vim_free(kp);
4357 kp = kp_next;
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 else
Bram Moolenaardad6b692005-01-25 22:14:34 +00004360 {
4361 kp_prev = kp;
4362 kp = kp->ke_next;
4363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 }
4366 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004367 hash_unlock(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368}
4369
4370/*
Bram Moolenaardad6b692005-01-25 22:14:34 +00004371 * Clear a whole keyword table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004374clear_keywtab(hashtab_T *ht)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004376 hashitem_T *hi;
4377 int todo;
4378 keyentry_T *kp;
4379 keyentry_T *kp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004381 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004382 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004384 if (!HASHITEM_EMPTY(hi))
4385 {
4386 --todo;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004387 for (kp = HI2KE(hi); kp != NULL; kp = kp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004389 kp_next = kp->ke_next;
4390 vim_free(kp->next_list);
4391 vim_free(kp->k_syn.cont_in_list);
4392 vim_free(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004396 hash_clear(ht);
4397 hash_init(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398}
4399
4400/*
4401 * Add a keyword to the list of keywords.
4402 */
4403 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004404add_keyword(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004405 char_u *name, // name of keyword
4406 int id, // group ID for this keyword
4407 int flags, // flags for this keyword
4408 short *cont_in_list, // containedin for this keyword
4409 short *next_list, // nextgroup for this keyword
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004410 int conceal_char)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004412 keyentry_T *kp;
4413 hashtab_T *ht;
4414 hashitem_T *hi;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004415 char_u *name_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004416 long_u hash;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004417 char_u name_folded[MAXKEYWLEN + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar860cae12010-06-05 23:22:07 +02004419 if (curwin->w_s->b_syn_ic)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004420 name_ic = str_foldcase(name, (int)STRLEN(name),
4421 name_folded, MAXKEYWLEN + 1);
4422 else
4423 name_ic = name;
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004424 kp = alloc(offsetof(keyentry_T, keyword) + STRLEN(name_ic) + 1);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004425 if (kp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 return;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004427 STRCPY(kp->keyword, name_ic);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004428 kp->k_syn.id = id;
4429 kp->k_syn.inc_tag = current_syn_inc_tag;
4430 kp->flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004431 kp->k_char = conceal_char;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004432 kp->k_syn.cont_in_list = copy_id_list(cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 if (cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004434 curwin->w_s->b_syn_containedin = TRUE;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004435 kp->next_list = copy_id_list(next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Bram Moolenaar860cae12010-06-05 23:22:07 +02004437 if (curwin->w_s->b_syn_ic)
4438 ht = &curwin->w_s->b_keywtab_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02004440 ht = &curwin->w_s->b_keywtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaardad6b692005-01-25 22:14:34 +00004442 hash = hash_hash(kp->keyword);
4443 hi = hash_lookup(ht, kp->keyword, hash);
4444 if (HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004446 // new keyword, add to hashtable
Bram Moolenaardad6b692005-01-25 22:14:34 +00004447 kp->ke_next = NULL;
4448 hash_add_item(ht, hi, kp->keyword, hash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004452 // keyword already exists, prepend to list
Bram Moolenaardad6b692005-01-25 22:14:34 +00004453 kp->ke_next = HI2KE(hi);
4454 hi->hi_key = KE2HIKEY(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456}
4457
4458/*
4459 * Get the start and end of the group name argument.
4460 * Return a pointer to the first argument.
4461 * Return NULL if the end of the command was found instead of further args.
4462 */
4463 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004464get_group_name(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004465 char_u *arg, // start of the argument
4466 char_u **name_end) // pointer to end of the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
4468 char_u *rest;
4469
4470 *name_end = skiptowhite(arg);
4471 rest = skipwhite(*name_end);
4472
4473 /*
4474 * Check if there are enough arguments. The first argument may be a
4475 * pattern, where '|' is allowed, so only check for NUL.
4476 */
4477 if (ends_excmd(*arg) || *rest == NUL)
4478 return NULL;
4479 return rest;
4480}
4481
4482/*
4483 * Check for syntax command option arguments.
4484 * This can be called at any place in the list of arguments, and just picks
4485 * out the arguments that are known. Can be called several times in a row to
4486 * collect all options in between other arguments.
4487 * Return a pointer to the next argument (which isn't an option).
4488 * Return NULL for any error;
4489 */
4490 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004491get_syn_options(
Bram Moolenaar1966c242020-04-20 22:42:32 +02004492 char_u *start, // next argument to be checked
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004493 syn_opt_arg_T *opt, // various things
Bram Moolenaarde318c52017-01-17 16:27:10 +01004494 int *conceal_char UNUSED,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004495 int skip) // TRUE if skipping over command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
Bram Moolenaar1966c242020-04-20 22:42:32 +02004497 char_u *arg = start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 char_u *gname_start, *gname;
4499 int syn_id;
4500 int len;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004501 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 int i;
4503 int fidx;
4504 static struct flag
4505 {
4506 char *name;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004507 int argtype;
4508 int flags;
4509 } flagtab[] = { {"cCoOnNtTaAiInNeEdD", 0, HL_CONTAINED},
4510 {"oOnNeElLiInNeE", 0, HL_ONELINE},
4511 {"kKeEeEpPeEnNdD", 0, HL_KEEPEND},
4512 {"eExXtTeEnNdD", 0, HL_EXTEND},
4513 {"eExXcClLuUdDeEnNlL", 0, HL_EXCLUDENL},
4514 {"tTrRaAnNsSpPaArReEnNtT", 0, HL_TRANSP},
4515 {"sSkKiIpPnNlL", 0, HL_SKIPNL},
4516 {"sSkKiIpPwWhHiItTeE", 0, HL_SKIPWHITE},
4517 {"sSkKiIpPeEmMpPtTyY", 0, HL_SKIPEMPTY},
4518 {"gGrRoOuUpPhHeErReE", 0, HL_SYNC_HERE},
4519 {"gGrRoOuUpPtThHeErReE", 0, HL_SYNC_THERE},
4520 {"dDiIsSpPlLaAyY", 0, HL_DISPLAY},
4521 {"fFoOlLdD", 0, HL_FOLD},
Bram Moolenaar860cae12010-06-05 23:22:07 +02004522 {"cCoOnNcCeEaAlL", 0, HL_CONCEAL},
4523 {"cCoOnNcCeEaAlLeEnNdDsS", 0, HL_CONCEALENDS},
4524 {"cCcChHaArR", 11, 0},
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004525 {"cCoOnNtTaAiInNsS", 1, 0},
4526 {"cCoOnNtTaAiInNeEdDiInN", 2, 0},
4527 {"nNeExXtTgGrRoOuUpP", 3, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 };
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004529 static char *first_letters = "cCoOkKeEtTsSgGdDfFnN";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004531 if (arg == NULL) // already detected error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 return NULL;
4533
Bram Moolenaar860cae12010-06-05 23:22:07 +02004534#ifdef FEAT_CONCEAL
4535 if (curwin->w_s->b_syn_conceal)
4536 opt->flags |= HL_CONCEAL;
4537#endif
4538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 for (;;)
4540 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004541 /*
4542 * This is used very often when a large number of keywords is defined.
4543 * Need to skip quickly when no option name is found.
4544 * Also avoid tolower(), it's slow.
4545 */
4546 if (strchr(first_letters, *arg) == NULL)
4547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
K.Takataeeec2542021-06-02 13:28:16 +02004549 for (fidx = ARRAY_LENGTH(flagtab); --fidx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004551 p = flagtab[fidx].name;
4552 for (i = 0, len = 0; p[i] != NUL; i += 2, ++len)
4553 if (arg[len] != p[i] && arg[len] != p[i + 1])
4554 break;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004555 if (p[i] == NUL && (VIM_ISWHITE(arg[len])
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004556 || (flagtab[fidx].argtype > 0
4557 ? arg[len] == '='
Bram Moolenaar1966c242020-04-20 22:42:32 +02004558 : ends_excmd2(start, arg + len))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004560 if (opt->keyword
4561 && (flagtab[fidx].flags == HL_DISPLAY
4562 || flagtab[fidx].flags == HL_FOLD
4563 || flagtab[fidx].flags == HL_EXTEND))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004564 // treat "display", "fold" and "extend" as a keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 fidx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567 }
4568 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004569 if (fidx < 0) // no match found
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004572 if (flagtab[fidx].argtype == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004574 if (!opt->has_cont_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004576 emsg(_(e_contains_argument_not_accepted_here));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 return NULL;
4578 }
Bram Moolenaarde318c52017-01-17 16:27:10 +01004579 if (get_id_list(&arg, 8, &opt->cont_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return NULL;
4581 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004582 else if (flagtab[fidx].argtype == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004584 if (get_id_list(&arg, 11, &opt->cont_in_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 return NULL;
4586 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004587 else if (flagtab[fidx].argtype == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004589 if (get_id_list(&arg, 9, &opt->next_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 return NULL;
4591 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004592 else if (flagtab[fidx].argtype == 11 && arg[5] == '=')
4593 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004594 // cchar=?
Bram Moolenaar860cae12010-06-05 23:22:07 +02004595 if (has_mbyte)
4596 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004597#ifdef FEAT_CONCEAL
Bram Moolenaar860cae12010-06-05 23:22:07 +02004598 *conceal_char = mb_ptr2char(arg + 6);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004599#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004600 arg += mb_ptr2len(arg + 6) - 1;
4601 }
4602 else
Bram Moolenaar56be9502010-06-06 14:20:26 +02004603 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004604#ifdef FEAT_CONCEAL
4605 *conceal_char = arg[6];
4606#else
4607 ;
4608#endif
Bram Moolenaar56be9502010-06-06 14:20:26 +02004609 }
Bram Moolenaar4124e722011-01-22 00:58:20 +01004610#ifdef FEAT_CONCEAL
4611 if (!vim_isprintc_strict(*conceal_char))
4612 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004613 emsg(_(e_invalid_cchar_value));
Bram Moolenaar4124e722011-01-22 00:58:20 +01004614 return NULL;
4615 }
4616#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004617 arg = skipwhite(arg + 7);
4618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 else
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004620 {
4621 opt->flags |= flagtab[fidx].flags;
4622 arg = skipwhite(arg + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004624 if (flagtab[fidx].flags == HL_SYNC_HERE
4625 || flagtab[fidx].flags == HL_SYNC_THERE)
4626 {
4627 if (opt->sync_idx == NULL)
4628 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004629 emsg(_(e_groupthere_not_accepted_here));
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004630 return NULL;
4631 }
4632 gname_start = arg;
4633 arg = skiptowhite(arg);
4634 if (gname_start == arg)
4635 return NULL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004636 gname = vim_strnsave(gname_start, arg - gname_start);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004637 if (gname == NULL)
4638 return NULL;
4639 if (STRCMP(gname, "NONE") == 0)
4640 *opt->sync_idx = NONE_IDX;
4641 else
4642 {
4643 syn_id = syn_name2id(gname);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004644 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
4645 if (SYN_ITEMS(curwin->w_s)[i].sp_syn.id == syn_id
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004646 && SYN_ITEMS(curwin->w_s)[i].sp_type
4647 == SPTYPE_START)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004648 {
4649 *opt->sync_idx = i;
4650 break;
4651 }
4652 if (i < 0)
4653 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004654 semsg(_(e_didnt_find_region_item_for_str), gname);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004655 vim_free(gname);
4656 return NULL;
4657 }
4658 }
4659
4660 vim_free(gname);
4661 arg = skipwhite(arg);
4662 }
4663#ifdef FEAT_FOLDING
4664 else if (flagtab[fidx].flags == HL_FOLD
4665 && foldmethodIsSyntax(curwin))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004666 // Need to update folds later.
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004667 foldUpdateAll(curwin);
4668#endif
4669 }
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 return arg;
4673}
4674
4675/*
4676 * Adjustments to syntax item when declared in a ":syn include"'d file.
4677 * Set the contained flag, and if the item is not already contained, add it
4678 * to the specified top-level group, if any.
4679 */
4680 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004681syn_incl_toplevel(int id, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004683 if ((*flagsp & HL_CONTAINED) || curwin->w_s->b_syn_topgrp == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 return;
4685 *flagsp |= HL_CONTAINED;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004686 if (curwin->w_s->b_syn_topgrp >= SYNID_CLUSTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004688 // We have to alloc this, because syn_combine_list() will free it.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004689 short *grp_list = ALLOC_MULT(short, 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004690 int tlg_id = curwin->w_s->b_syn_topgrp - SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
4692 if (grp_list != NULL)
4693 {
4694 grp_list[0] = id;
4695 grp_list[1] = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004696 syn_combine_list(&SYN_CLSTR(curwin->w_s)[tlg_id].scl_list,
4697 &grp_list, CLUSTER_ADD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
4699 }
4700}
4701
4702/*
4703 * Handle ":syntax include [@{group-name}] filename" command.
4704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004706syn_cmd_include(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
4708 char_u *arg = eap->arg;
4709 int sgl_id = 1;
4710 char_u *group_name_end;
4711 char_u *rest;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004712 char *errormsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 int prev_toplvl_grp;
4714 int prev_syn_inc_tag;
4715 int source = FALSE;
4716
4717 eap->nextcmd = find_nextcmd(arg);
4718 if (eap->skip)
4719 return;
4720
4721 if (arg[0] == '@')
4722 {
4723 ++arg;
4724 rest = get_group_name(arg, &group_name_end);
4725 if (rest == NULL)
4726 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004727 emsg(_(e_filename_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return;
4729 }
4730 sgl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004731 if (sgl_id == 0)
4732 return;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004733 // separate_nextcmd() and expand_filename() depend on this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 eap->arg = rest;
4735 }
4736
4737 /*
4738 * Everything that's left, up to the next command, should be the
4739 * filename to include.
4740 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004741 eap->argt |= (EX_XFILE | EX_NOSPC);
Bram Moolenaarac485062022-03-23 19:45:01 +00004742 separate_nextcmd(eap, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (*eap->arg == '<' || *eap->arg == '$' || mch_isFullName(eap->arg))
4744 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004745 // For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
4746 // file. Need to expand the file name first. In other cases
4747 // ":runtime!" is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 source = TRUE;
4749 if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL)
4750 {
4751 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004752 emsg(errormsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 return;
4754 }
4755 }
4756
4757 /*
4758 * Save and restore the existing top-level grouplist id and ":syn
4759 * include" tag around the actual inclusion.
4760 */
Bram Moolenaar42431a72011-04-01 14:44:59 +02004761 if (running_syn_inc_tag >= MAX_SYN_INC_TAG)
4762 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004763 emsg(_(e_too_many_syntax_includes));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004764 return;
4765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 prev_syn_inc_tag = current_syn_inc_tag;
4767 current_syn_inc_tag = ++running_syn_inc_tag;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004768 prev_toplvl_grp = curwin->w_s->b_syn_topgrp;
4769 curwin->w_s->b_syn_topgrp = sgl_id;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 if (source ? do_source(eap->arg, FALSE, DOSO_NONE, NULL) == FAIL
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004771 : source_runtime(eap->arg, DIP_ALL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004772 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004773 curwin->w_s->b_syn_topgrp = prev_toplvl_grp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 current_syn_inc_tag = prev_syn_inc_tag;
4775}
4776
4777/*
4778 * Handle ":syntax keyword {group-name} [{option}] keyword .." command.
4779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004781syn_cmd_keyword(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 char_u *arg = eap->arg;
4784 char_u *group_name_end;
4785 int syn_id;
4786 char_u *rest;
Bram Moolenaar42431a72011-04-01 14:44:59 +02004787 char_u *keyword_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 char_u *p;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004789 char_u *kw;
4790 syn_opt_arg_T syn_opt_arg;
4791 int cnt;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004792 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 rest = get_group_name(arg, &group_name_end);
4795
4796 if (rest != NULL)
4797 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004798 if (eap->skip)
4799 syn_id = -1;
4800 else
4801 syn_id = syn_check_group(arg, (int)(group_name_end - arg));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004802 if (syn_id != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004803 // allocate a buffer, for removing backslashes in the keyword
Bram Moolenaar964b3742019-05-24 18:54:09 +02004804 keyword_copy = alloc(STRLEN(rest) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (keyword_copy != NULL)
4806 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004807 syn_opt_arg.flags = 0;
4808 syn_opt_arg.keyword = TRUE;
4809 syn_opt_arg.sync_idx = NULL;
4810 syn_opt_arg.has_cont_list = FALSE;
4811 syn_opt_arg.cont_in_list = NULL;
4812 syn_opt_arg.next_list = NULL;
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /*
4815 * The options given apply to ALL keywords, so all options must be
4816 * found before keywords can be created.
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004817 * 1: collect the options and copy the keywords to keyword_copy.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004819 cnt = 0;
4820 p = keyword_copy;
Bram Moolenaar1966c242020-04-20 22:42:32 +02004821 for ( ; rest != NULL && !ends_excmd2(eap->arg, rest);
4822 rest = skipwhite(rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004824 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char,
4825 eap->skip);
Bram Moolenaar1966c242020-04-20 22:42:32 +02004826 if (rest == NULL || ends_excmd2(eap->arg, rest))
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004827 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004828 // Copy the keyword, removing backslashes, and add a NUL.
Bram Moolenaar1c465442017-03-12 20:10:05 +01004829 while (*rest != NUL && !VIM_ISWHITE(*rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004831 if (*rest == '\\' && rest[1] != NUL)
4832 ++rest;
4833 *p++ = *rest++;
4834 }
4835 *p++ = NUL;
4836 ++cnt;
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004839 if (!eap->skip)
4840 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004841 // Adjust flags for use of ":syn include".
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004842 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
4843
4844 /*
4845 * 2: Add an entry for each keyword.
4846 */
4847 for (kw = keyword_copy; --cnt >= 0; kw += STRLEN(kw) + 1)
4848 {
4849 for (p = vim_strchr(kw, '['); ; )
4850 {
4851 if (p != NULL)
4852 *p = NUL;
4853 add_keyword(kw, syn_id, syn_opt_arg.flags,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004854 syn_opt_arg.cont_in_list,
4855 syn_opt_arg.next_list, conceal_char);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004856 if (p == NULL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004857 break;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004858 if (p[1] == NUL)
4859 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00004860 semsg(_(e_error_missing_rsb_str), kw);
Bram Moolenaar1560d072015-08-13 22:53:29 +02004861 goto error;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004862 }
4863 if (p[1] == ']')
4864 {
Bram Moolenaar1560d072015-08-13 22:53:29 +02004865 if (p[2] != NUL)
4866 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004867 semsg(_(e_trailing_char_after_rsb_str_str),
4868 kw, &p[2]);
Bram Moolenaar1560d072015-08-13 22:53:29 +02004869 goto error;
4870 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004871 kw = p + 1; // skip over the "]"
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004872 break;
4873 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004874 if (has_mbyte)
4875 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004876 int l = (*mb_ptr2len)(p + 1);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004877
4878 mch_memmove(p, p + 1, l);
4879 p += l;
4880 }
4881 else
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004882 {
4883 p[0] = p[1];
4884 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886 }
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
Bram Moolenaar1560d072015-08-13 22:53:29 +02004889error:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 vim_free(keyword_copy);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004891 vim_free(syn_opt_arg.cont_in_list);
4892 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 }
4895
4896 if (rest != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02004897 set_nextcmd(eap, rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004899 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00004901 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004902 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903}
4904
4905/*
4906 * Handle ":syntax match {name} [{options}] {pattern} [{options}]".
4907 *
4908 * Also ":syntax sync match {name} [[grouphere | groupthere] {group-name}] .."
4909 */
4910 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004911syn_cmd_match(
4912 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004913 int syncing) // TRUE for ":syntax sync match .. "
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 char_u *arg = eap->arg;
4916 char_u *group_name_end;
4917 char_u *rest;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004918 synpat_T item; // the item found in the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 int syn_id;
4920 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004921 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 int sync_idx = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004923 int conceal_char = NUL;
Bram Moolenaar1966c242020-04-20 22:42:32 +02004924 int orig_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004926 // Isolate the group name, check for validity
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 rest = get_group_name(arg, &group_name_end);
4928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004929 // Get options before the pattern
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004930 syn_opt_arg.flags = 0;
4931 syn_opt_arg.keyword = FALSE;
4932 syn_opt_arg.sync_idx = syncing ? &sync_idx : NULL;
4933 syn_opt_arg.has_cont_list = TRUE;
4934 syn_opt_arg.cont_list = NULL;
4935 syn_opt_arg.cont_in_list = NULL;
4936 syn_opt_arg.next_list = NULL;
Bram Moolenaarde318c52017-01-17 16:27:10 +01004937 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004939 // get the pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 init_syn_patterns();
Bram Moolenaara80faa82020-04-12 19:37:17 +02004941 CLEAR_FIELD(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 rest = get_syn_pattern(rest, &item);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004943 if (vim_regcomp_had_eol() && !(syn_opt_arg.flags & HL_EXCLUDENL))
4944 syn_opt_arg.flags |= HL_HAS_EOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004946 // Get options after the pattern
Bram Moolenaarde318c52017-01-17 16:27:10 +01004947 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004949 if (rest != NULL) // all arguments are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 /*
4952 * Check for trailing command and illegal trailing arguments.
4953 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02004954 set_nextcmd(eap, rest);
Bram Moolenaar1966c242020-04-20 22:42:32 +02004955 if (!ends_excmd2(eap->cmd, rest) || eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004957 else if (ga_grow(&curwin->w_s->b_syn_patterns, 1) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 && (syn_id = syn_check_group(arg,
4959 (int)(group_name_end - arg))) != 0)
4960 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004961 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 /*
4963 * Store the pattern in the syn_items list
4964 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004965 idx = curwin->w_s->b_syn_patterns.ga_len;
4966 SYN_ITEMS(curwin->w_s)[idx] = item;
4967 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
4968 SYN_ITEMS(curwin->w_s)[idx].sp_type = SPTYPE_MATCH;
4969 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
4970 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag = current_syn_inc_tag;
4971 SYN_ITEMS(curwin->w_s)[idx].sp_flags = syn_opt_arg.flags;
4972 SYN_ITEMS(curwin->w_s)[idx].sp_sync_idx = sync_idx;
4973 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list = syn_opt_arg.cont_list;
4974 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004975 syn_opt_arg.cont_in_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004976#ifdef FEAT_CONCEAL
Bram Moolenaar6e202e52010-07-28 18:14:45 +02004977 SYN_ITEMS(curwin->w_s)[idx].sp_cchar = conceal_char;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004978#endif
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004979 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004980 curwin->w_s->b_syn_containedin = TRUE;
4981 SYN_ITEMS(curwin->w_s)[idx].sp_next_list = syn_opt_arg.next_list;
4982 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004984 // remember that we found a match for syncing on
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004985 if (syn_opt_arg.flags & (HL_SYNC_HERE|HL_SYNC_THERE))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004986 curwin->w_s->b_syn_sync_flags |= SF_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004988 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004989 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990#endif
4991
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00004992 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004993 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
4994 return; // don't free the progs and patterns now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 }
4997
4998 /*
4999 * Something failed, free the allocated memory.
5000 */
Bram Moolenaar473de612013-06-08 18:19:48 +02005001 vim_regfree(item.sp_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 vim_free(item.sp_pattern);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005003 vim_free(syn_opt_arg.cont_list);
5004 vim_free(syn_opt_arg.cont_in_list);
5005 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
Bram Moolenaar1966c242020-04-20 22:42:32 +02005007 if (rest == NULL && called_emsg == orig_called_emsg)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005008 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009}
5010
5011/*
5012 * Handle ":syntax region {group-name} [matchgroup={group-name}]
5013 * start {start} .. [skip {skip}] end {end} .. [{options}]".
5014 */
5015 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005016syn_cmd_region(
5017 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005018 int syncing) // TRUE for ":syntax sync region .."
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
5020 char_u *arg = eap->arg;
5021 char_u *group_name_end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005022 char_u *rest; // next arg, NULL on error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_u *key_end;
5024 char_u *key = NULL;
5025 char_u *p;
5026 int item;
5027#define ITEM_START 0
5028#define ITEM_SKIP 1
5029#define ITEM_END 2
5030#define ITEM_MATCHGROUP 3
5031 struct pat_ptr
5032 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005033 synpat_T *pp_synp; // pointer to syn_pattern
5034 int pp_matchgroup_id; // matchgroup ID
5035 struct pat_ptr *pp_next; // pointer to next pat_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 } *(pat_ptrs[3]);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005037 // patterns found in the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 struct pat_ptr *ppp;
5039 struct pat_ptr *ppp_next;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005040 int pat_count = 0; // nr of syn_patterns found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 int syn_id;
5042 int matchgroup_id = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005043 int not_enough = FALSE; // not enough arguments
5044 int illegal = FALSE; // illegal arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 int success = FALSE;
5046 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005047 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005048 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005050 // Isolate the group name, check for validity
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 rest = get_group_name(arg, &group_name_end);
5052
5053 pat_ptrs[0] = NULL;
5054 pat_ptrs[1] = NULL;
5055 pat_ptrs[2] = NULL;
5056
5057 init_syn_patterns();
5058
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005059 syn_opt_arg.flags = 0;
5060 syn_opt_arg.keyword = FALSE;
5061 syn_opt_arg.sync_idx = NULL;
5062 syn_opt_arg.has_cont_list = TRUE;
5063 syn_opt_arg.cont_list = NULL;
5064 syn_opt_arg.cont_in_list = NULL;
5065 syn_opt_arg.next_list = NULL;
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 /*
5068 * get the options, patterns and matchgroup.
5069 */
Bram Moolenaar1966c242020-04-20 22:42:32 +02005070 while (rest != NULL && !ends_excmd2(eap->cmd, rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005072 // Check for option arguments
Bram Moolenaarde318c52017-01-17 16:27:10 +01005073 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar1966c242020-04-20 22:42:32 +02005074 if (rest == NULL || ends_excmd2(eap->cmd, rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 break;
5076
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005077 // must be a pattern or matchgroup then
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 key_end = rest;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005079 while (*key_end && !VIM_ISWHITE(*key_end) && *key_end != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 ++key_end;
5081 vim_free(key);
Bram Moolenaardf44a272020-06-07 20:49:05 +02005082 key = vim_strnsave_up(rest, key_end - rest);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005083 if (key == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 rest = NULL;
5086 break;
5087 }
5088 if (STRCMP(key, "MATCHGROUP") == 0)
5089 item = ITEM_MATCHGROUP;
5090 else if (STRCMP(key, "START") == 0)
5091 item = ITEM_START;
5092 else if (STRCMP(key, "END") == 0)
5093 item = ITEM_END;
5094 else if (STRCMP(key, "SKIP") == 0)
5095 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005096 if (pat_ptrs[ITEM_SKIP] != NULL) // one skip pattern allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 illegal = TRUE;
5099 break;
5100 }
5101 item = ITEM_SKIP;
5102 }
5103 else
5104 break;
5105 rest = skipwhite(key_end);
5106 if (*rest != '=')
5107 {
5108 rest = NULL;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005109 semsg(_(e_missing_equal_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 break;
5111 }
5112 rest = skipwhite(rest + 1);
5113 if (*rest == NUL)
5114 {
5115 not_enough = TRUE;
5116 break;
5117 }
5118
5119 if (item == ITEM_MATCHGROUP)
5120 {
5121 p = skiptowhite(rest);
5122 if ((p - rest == 4 && STRNCMP(rest, "NONE", 4) == 0) || eap->skip)
5123 matchgroup_id = 0;
5124 else
5125 {
5126 matchgroup_id = syn_check_group(rest, (int)(p - rest));
5127 if (matchgroup_id == 0)
5128 {
5129 illegal = TRUE;
5130 break;
5131 }
5132 }
5133 rest = skipwhite(p);
5134 }
5135 else
5136 {
5137 /*
5138 * Allocate room for a syn_pattern, and link it in the list of
5139 * syn_patterns for this item, at the start (because the list is
5140 * used from end to start).
5141 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005142 ppp = ALLOC_ONE(struct pat_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (ppp == NULL)
5144 {
5145 rest = NULL;
5146 break;
5147 }
5148 ppp->pp_next = pat_ptrs[item];
5149 pat_ptrs[item] = ppp;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005150 ppp->pp_synp = ALLOC_CLEAR_ONE(synpat_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (ppp->pp_synp == NULL)
5152 {
5153 rest = NULL;
5154 break;
5155 }
5156
5157 /*
5158 * Get the syntax pattern and the following offset(s).
5159 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005160 // Enable the appropriate \z specials.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (item == ITEM_START)
5162 reg_do_extmatch = REX_SET;
5163 else if (item == ITEM_SKIP || item == ITEM_END)
5164 reg_do_extmatch = REX_USE;
5165 rest = get_syn_pattern(rest, ppp->pp_synp);
5166 reg_do_extmatch = 0;
5167 if (item == ITEM_END && vim_regcomp_had_eol()
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005168 && !(syn_opt_arg.flags & HL_EXCLUDENL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 ppp->pp_synp->sp_flags |= HL_HAS_EOL;
5170 ppp->pp_matchgroup_id = matchgroup_id;
5171 ++pat_count;
5172 }
5173 }
5174 vim_free(key);
5175 if (illegal || not_enough)
5176 rest = NULL;
5177
5178 /*
5179 * Must have a "start" and "end" pattern.
5180 */
5181 if (rest != NULL && (pat_ptrs[ITEM_START] == NULL ||
5182 pat_ptrs[ITEM_END] == NULL))
5183 {
5184 not_enough = TRUE;
5185 rest = NULL;
5186 }
5187
5188 if (rest != NULL)
5189 {
5190 /*
5191 * Check for trailing garbage or command.
5192 * If OK, add the item.
5193 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02005194 set_nextcmd(eap, rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if (!ends_excmd(*rest) || eap->skip)
5196 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005197 else if (ga_grow(&(curwin->w_s->b_syn_patterns), pat_count) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 && (syn_id = syn_check_group(arg,
5199 (int)(group_name_end - arg))) != 0)
5200 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005201 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 /*
5203 * Store the start/skip/end in the syn_items list
5204 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005205 idx = curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 for (item = ITEM_START; item <= ITEM_END; ++item)
5207 {
5208 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp->pp_next)
5209 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005210 SYN_ITEMS(curwin->w_s)[idx] = *(ppp->pp_synp);
5211 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
5212 SYN_ITEMS(curwin->w_s)[idx].sp_type =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 (item == ITEM_START) ? SPTYPE_START :
5214 (item == ITEM_SKIP) ? SPTYPE_SKIP : SPTYPE_END;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215 SYN_ITEMS(curwin->w_s)[idx].sp_flags |= syn_opt_arg.flags;
5216 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
Bram Moolenaar42431a72011-04-01 14:44:59 +02005217 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag =
5218 current_syn_inc_tag;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005219 SYN_ITEMS(curwin->w_s)[idx].sp_syn_match_id =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 ppp->pp_matchgroup_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005221#ifdef FEAT_CONCEAL
Bram Moolenaar6e202e52010-07-28 18:14:45 +02005222 SYN_ITEMS(curwin->w_s)[idx].sp_cchar = conceal_char;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (item == ITEM_START)
5225 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005226 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005227 syn_opt_arg.cont_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005228 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005229 syn_opt_arg.cont_in_list;
5230 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005231 curwin->w_s->b_syn_containedin = TRUE;
5232 SYN_ITEMS(curwin->w_s)[idx].sp_next_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005233 syn_opt_arg.next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005235 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 ++idx;
5237#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005238 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005239 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240#endif
5241 }
5242 }
5243
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005244 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005245 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
5246 success = TRUE; // don't free the progs and patterns now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 }
5249
5250 /*
5251 * Free the allocated memory.
5252 */
5253 for (item = ITEM_START; item <= ITEM_END; ++item)
5254 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp_next)
5255 {
Bram Moolenaar4bbfb0f2019-08-31 15:28:02 +02005256 if (!success && ppp->pp_synp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
Bram Moolenaar473de612013-06-08 18:19:48 +02005258 vim_regfree(ppp->pp_synp->sp_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 vim_free(ppp->pp_synp->sp_pattern);
5260 }
5261 vim_free(ppp->pp_synp);
5262 ppp_next = ppp->pp_next;
5263 vim_free(ppp);
5264 }
5265
5266 if (!success)
5267 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005268 vim_free(syn_opt_arg.cont_list);
5269 vim_free(syn_opt_arg.cont_in_list);
5270 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 if (not_enough)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005272 semsg(_(e_not_enough_arguments_syntax_region_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 else if (illegal || rest == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005274 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276}
5277
5278/*
5279 * A simple syntax group ID comparison function suitable for use in qsort()
5280 */
5281 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005282syn_compare_stub(const void *v1, const void *v2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
5284 const short *s1 = v1;
5285 const short *s2 = v2;
5286
5287 return (*s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0);
5288}
5289
5290/*
5291 * Combines lists of syntax clusters.
5292 * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
5293 */
5294 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005295syn_combine_list(short **clstr1, short **clstr2, int list_op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297 int count1 = 0;
5298 int count2 = 0;
5299 short *g1;
5300 short *g2;
5301 short *clstr = NULL;
5302 int count;
5303 int round;
5304
5305 /*
5306 * Handle degenerate cases.
5307 */
5308 if (*clstr2 == NULL)
5309 return;
5310 if (*clstr1 == NULL || list_op == CLUSTER_REPLACE)
5311 {
5312 if (list_op == CLUSTER_REPLACE)
5313 vim_free(*clstr1);
5314 if (list_op == CLUSTER_REPLACE || list_op == CLUSTER_ADD)
5315 *clstr1 = *clstr2;
5316 else
5317 vim_free(*clstr2);
5318 return;
5319 }
5320
5321 for (g1 = *clstr1; *g1; g1++)
5322 ++count1;
5323 for (g2 = *clstr2; *g2; g2++)
5324 ++count2;
5325
5326 /*
5327 * For speed purposes, sort both lists.
5328 */
5329 qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
5330 qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
5331
5332 /*
5333 * We proceed in two passes; in round 1, we count the elements to place
5334 * in the new list, and in round 2, we allocate and populate the new
5335 * list. For speed, we use a mergesort-like method, adding the smaller
5336 * of the current elements in each list to the new list.
5337 */
5338 for (round = 1; round <= 2; round++)
5339 {
5340 g1 = *clstr1;
5341 g2 = *clstr2;
5342 count = 0;
5343
5344 /*
5345 * First, loop through the lists until one of them is empty.
5346 */
5347 while (*g1 && *g2)
5348 {
5349 /*
5350 * We always want to add from the first list.
5351 */
5352 if (*g1 < *g2)
5353 {
5354 if (round == 2)
5355 clstr[count] = *g1;
5356 count++;
5357 g1++;
5358 continue;
5359 }
5360 /*
5361 * We only want to add from the second list if we're adding the
5362 * lists.
5363 */
5364 if (list_op == CLUSTER_ADD)
5365 {
5366 if (round == 2)
5367 clstr[count] = *g2;
5368 count++;
5369 }
5370 if (*g1 == *g2)
5371 g1++;
5372 g2++;
5373 }
5374
5375 /*
5376 * Now add the leftovers from whichever list didn't get finished
5377 * first. As before, we only want to add from the second list if
5378 * we're adding the lists.
5379 */
5380 for (; *g1; g1++, count++)
5381 if (round == 2)
5382 clstr[count] = *g1;
5383 if (list_op == CLUSTER_ADD)
5384 for (; *g2; g2++, count++)
5385 if (round == 2)
5386 clstr[count] = *g2;
5387
5388 if (round == 1)
5389 {
5390 /*
5391 * If the group ended up empty, we don't need to allocate any
5392 * space for it.
5393 */
5394 if (count == 0)
5395 {
5396 clstr = NULL;
5397 break;
5398 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005399 clstr = ALLOC_MULT(short, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 if (clstr == NULL)
5401 break;
5402 clstr[count] = 0;
5403 }
5404 }
5405
5406 /*
5407 * Finally, put the new list in place.
5408 */
5409 vim_free(*clstr1);
5410 vim_free(*clstr2);
5411 *clstr1 = clstr;
5412}
5413
5414/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005415 * Lookup a syntax cluster name and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 * If it is not found, 0 is returned.
5417 */
5418 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005419syn_scl_name2id(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 int i;
5422 char_u *name_u;
5423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005424 // Avoid using stricmp() too much, it's slow on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 name_u = vim_strsave_up(name);
5426 if (name_u == NULL)
5427 return 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005428 for (i = curwin->w_s->b_syn_clusters.ga_len; --i >= 0; )
5429 if (SYN_CLSTR(curwin->w_s)[i].scl_name_u != NULL
5430 && STRCMP(name_u, SYN_CLSTR(curwin->w_s)[i].scl_name_u) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 break;
5432 vim_free(name_u);
5433 return (i < 0 ? 0 : i + SYNID_CLUSTER);
5434}
5435
5436/*
5437 * Like syn_scl_name2id(), but take a pointer + length argument.
5438 */
5439 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005440syn_scl_namen2id(char_u *linep, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 char_u *name;
5443 int id = 0;
5444
5445 name = vim_strnsave(linep, len);
5446 if (name != NULL)
5447 {
5448 id = syn_scl_name2id(name);
5449 vim_free(name);
5450 }
5451 return id;
5452}
5453
5454/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005455 * Find syntax cluster name in the table and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 * The argument is a pointer to the name and the length of the name.
5457 * If it doesn't exist yet, a new entry is created.
5458 * Return 0 for failure.
5459 */
5460 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005461syn_check_cluster(char_u *pp, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 int id;
5464 char_u *name;
5465
5466 name = vim_strnsave(pp, len);
5467 if (name == NULL)
5468 return 0;
5469
5470 id = syn_scl_name2id(name);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005471 if (id == 0) // doesn't exist yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 id = syn_add_cluster(name);
5473 else
5474 vim_free(name);
5475 return id;
5476}
5477
5478/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005479 * Add new syntax cluster and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 * "name" must be an allocated string, it will be consumed.
5481 * Return 0 for failure.
5482 */
5483 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005484syn_add_cluster(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
Bram Moolenaar217ad922005-03-20 22:37:15 +00005486 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488 /*
5489 * First call for this growarray: init growing array.
5490 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005491 if (curwin->w_s->b_syn_clusters.ga_data == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005493 curwin->w_s->b_syn_clusters.ga_itemsize = sizeof(syn_cluster_T);
5494 curwin->w_s->b_syn_clusters.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496
Bram Moolenaar42431a72011-04-01 14:44:59 +02005497 len = curwin->w_s->b_syn_clusters.ga_len;
5498 if (len >= MAX_CLUSTER_ID)
5499 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005500 emsg(_(e_too_many_syntax_clusters));
Bram Moolenaar42431a72011-04-01 14:44:59 +02005501 vim_free(name);
5502 return 0;
5503 }
5504
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 /*
5506 * Make room for at least one other cluster entry.
5507 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005508 if (ga_grow(&curwin->w_s->b_syn_clusters, 1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
5510 vim_free(name);
5511 return 0;
5512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
Bram Moolenaara80faa82020-04-12 19:37:17 +02005514 CLEAR_POINTER(&(SYN_CLSTR(curwin->w_s)[len]));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005515 SYN_CLSTR(curwin->w_s)[len].scl_name = name;
5516 SYN_CLSTR(curwin->w_s)[len].scl_name_u = vim_strsave_up(name);
5517 SYN_CLSTR(curwin->w_s)[len].scl_list = NULL;
5518 ++curwin->w_s->b_syn_clusters.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
Bram Moolenaar217ad922005-03-20 22:37:15 +00005520 if (STRICMP(name, "Spell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005521 curwin->w_s->b_spell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005522 if (STRICMP(name, "NoSpell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005523 curwin->w_s->b_nospell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005524
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return len + SYNID_CLUSTER;
5526}
5527
5528/*
5529 * Handle ":syntax cluster {cluster-name} [contains={groupname},..]
5530 * [add={groupname},..] [remove={groupname},..]".
5531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005533syn_cmd_cluster(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 char_u *arg = eap->arg;
5536 char_u *group_name_end;
5537 char_u *rest;
5538 int scl_id;
5539 short *clstr_list;
5540 int got_clstr = FALSE;
5541 int opt_len;
5542 int list_op;
5543
5544 eap->nextcmd = find_nextcmd(arg);
5545 if (eap->skip)
5546 return;
5547
5548 rest = get_group_name(arg, &group_name_end);
5549
5550 if (rest != NULL)
5551 {
Bram Moolenaar42431a72011-04-01 14:44:59 +02005552 scl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
5553 if (scl_id == 0)
5554 return;
5555 scl_id -= SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 for (;;)
5558 {
5559 if (STRNICMP(rest, "add", 3) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005560 && (VIM_ISWHITE(rest[3]) || rest[3] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 opt_len = 3;
5563 list_op = CLUSTER_ADD;
5564 }
5565 else if (STRNICMP(rest, "remove", 6) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005566 && (VIM_ISWHITE(rest[6]) || rest[6] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
5568 opt_len = 6;
5569 list_op = CLUSTER_SUBTRACT;
5570 }
5571 else if (STRNICMP(rest, "contains", 8) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005572 && (VIM_ISWHITE(rest[8]) || rest[8] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
5574 opt_len = 8;
5575 list_op = CLUSTER_REPLACE;
5576 }
5577 else
5578 break;
5579
5580 clstr_list = NULL;
Bram Moolenaarde318c52017-01-17 16:27:10 +01005581 if (get_id_list(&rest, opt_len, &clstr_list, eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005583 semsg(_(e_invalid_argument_str), rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 break;
5585 }
Bram Moolenaarde318c52017-01-17 16:27:10 +01005586 if (scl_id >= 0)
5587 syn_combine_list(&SYN_CLSTR(curwin->w_s)[scl_id].scl_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 &clstr_list, list_op);
Bram Moolenaard7a96152017-01-22 15:28:55 +01005589 else
5590 vim_free(clstr_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 got_clstr = TRUE;
5592 }
5593
5594 if (got_clstr)
5595 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005596 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005597 syn_stack_free_all(curwin->w_s); // Need to recompute all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 }
5600
5601 if (!got_clstr)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005602 emsg(_(e_no_cluster_specified));
Bram Moolenaar1966c242020-04-20 22:42:32 +02005603 if (rest == NULL || !ends_excmd2(eap->cmd, rest))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005604 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605}
5606
5607/*
5608 * On first call for current buffer: Init growing array.
5609 */
5610 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005611init_syn_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
Bram Moolenaar860cae12010-06-05 23:22:07 +02005613 curwin->w_s->b_syn_patterns.ga_itemsize = sizeof(synpat_T);
5614 curwin->w_s->b_syn_patterns.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616
5617/*
5618 * Get one pattern for a ":syntax match" or ":syntax region" command.
5619 * Stores the pattern and program in a synpat_T.
5620 * Returns a pointer to the next argument, or NULL in case of an error.
5621 */
5622 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005623get_syn_pattern(char_u *arg, synpat_T *ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 char_u *end;
5626 int *p;
5627 int idx;
5628 char_u *cpo_save;
5629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005630 // need at least three chars
Bram Moolenaar38219782015-08-11 15:27:13 +02005631 if (arg == NULL || arg[0] == NUL || arg[1] == NUL || arg[2] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 return NULL;
5633
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005634 end = skip_regexp(arg + 1, *arg, TRUE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005635 if (*end != *arg) // end delimiter not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005637 semsg(_(e_pattern_delimiter_not_found_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 return NULL;
5639 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005640 // store the pattern and compiled regexp program
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005641 if ((ci->sp_pattern = vim_strnsave(arg + 1, end - arg - 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 return NULL;
5643
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005644 // Make 'cpoptions' empty, to avoid the 'l' flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 cpo_save = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01005646 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 ci->sp_prog = vim_regcomp(ci->sp_pattern, RE_MAGIC);
5648 p_cpo = cpo_save;
5649
5650 if (ci->sp_prog == NULL)
5651 return NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005652 ci->sp_ic = curwin->w_s->b_syn_ic;
Bram Moolenaarf7512552013-06-06 14:55:19 +02005653#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005654 syn_clear_time(&ci->sp_time);
5655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 /*
5658 * Check for a match, highlight or region offset.
5659 */
5660 ++end;
5661 do
5662 {
5663 for (idx = SPO_COUNT; --idx >= 0; )
5664 if (STRNCMP(end, spo_name_tab[idx], 3) == 0)
5665 break;
5666 if (idx >= 0)
5667 {
5668 p = &(ci->sp_offsets[idx]);
5669 if (idx != SPO_LC_OFF)
5670 switch (end[3])
5671 {
5672 case 's': break;
5673 case 'b': break;
5674 case 'e': idx += SPO_COUNT; break;
5675 default: idx = -1; break;
5676 }
5677 if (idx >= 0)
5678 {
5679 ci->sp_off_flags |= (1 << idx);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005680 if (idx == SPO_LC_OFF) // lc=99
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
5682 end += 3;
5683 *p = getdigits(&end);
5684
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005685 // "lc=" offset automatically sets "ms=" offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (!(ci->sp_off_flags & (1 << SPO_MS_OFF)))
5687 {
5688 ci->sp_off_flags |= (1 << SPO_MS_OFF);
5689 ci->sp_offsets[SPO_MS_OFF] = *p;
5690 }
5691 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005692 else // yy=x+99
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694 end += 4;
5695 if (*end == '+')
5696 {
5697 ++end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005698 *p = getdigits(&end); // positive offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 else if (*end == '-')
5701 {
5702 ++end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005703 *p = -getdigits(&end); // negative offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
5705 }
5706 if (*end != ',')
5707 break;
5708 ++end;
5709 }
5710 }
5711 } while (idx >= 0);
5712
Bram Moolenaar1966c242020-04-20 22:42:32 +02005713 if (!ends_excmd2(arg, end) && !VIM_ISWHITE(*end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005715 semsg(_(e_garbage_after_pattern_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 return NULL;
5717 }
5718 return skipwhite(end);
5719}
5720
5721/*
5722 * Handle ":syntax sync .." command.
5723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005725syn_cmd_sync(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 char_u *arg_start = eap->arg;
5728 char_u *arg_end;
5729 char_u *key = NULL;
5730 char_u *next_arg;
5731 int illegal = FALSE;
5732 int finished = FALSE;
5733 long n;
5734 char_u *cpo_save;
5735
Bram Moolenaar1966c242020-04-20 22:42:32 +02005736 if (ends_excmd2(eap->cmd, arg_start))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
5738 syn_cmd_list(eap, TRUE);
5739 return;
5740 }
5741
Bram Moolenaar1966c242020-04-20 22:42:32 +02005742 while (!ends_excmd2(eap->cmd, arg_start))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
5744 arg_end = skiptowhite(arg_start);
5745 next_arg = skipwhite(arg_end);
5746 vim_free(key);
Bram Moolenaardf44a272020-06-07 20:49:05 +02005747 key = vim_strnsave_up(arg_start, arg_end - arg_start);
Bram Moolenaar58bb61c2020-07-10 20:30:12 +02005748 if (key == NULL)
5749 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (STRCMP(key, "CCOMMENT") == 0)
5751 {
5752 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005753 curwin->w_s->b_syn_sync_flags |= SF_CCOMMENT;
Bram Moolenaar1966c242020-04-20 22:42:32 +02005754 if (!ends_excmd2(eap->cmd, next_arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
5756 arg_end = skiptowhite(next_arg);
5757 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005758 curwin->w_s->b_syn_sync_id = syn_check_group(next_arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 (int)(arg_end - next_arg));
5760 next_arg = skipwhite(arg_end);
5761 }
5762 else if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005763 curwin->w_s->b_syn_sync_id = syn_name2id((char_u *)"Comment");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 else if ( STRNCMP(key, "LINES", 5) == 0
5766 || STRNCMP(key, "MINLINES", 8) == 0
5767 || STRNCMP(key, "MAXLINES", 8) == 0
5768 || STRNCMP(key, "LINEBREAKS", 10) == 0)
5769 {
5770 if (key[4] == 'S')
5771 arg_end = key + 6;
5772 else if (key[0] == 'L')
5773 arg_end = key + 11;
5774 else
5775 arg_end = key + 9;
5776 if (arg_end[-1] != '=' || !VIM_ISDIGIT(*arg_end))
5777 {
5778 illegal = TRUE;
5779 break;
5780 }
5781 n = getdigits(&arg_end);
5782 if (!eap->skip)
5783 {
5784 if (key[4] == 'B')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005785 curwin->w_s->b_syn_sync_linebreaks = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 else if (key[1] == 'A')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005787 curwin->w_s->b_syn_sync_maxlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02005789 curwin->w_s->b_syn_sync_minlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 }
5792 else if (STRCMP(key, "FROMSTART") == 0)
5793 {
5794 if (!eap->skip)
5795 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005796 curwin->w_s->b_syn_sync_minlines = MAXLNUM;
5797 curwin->w_s->b_syn_sync_maxlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
5799 }
5800 else if (STRCMP(key, "LINECONT") == 0)
5801 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005802 if (*next_arg == NUL) // missing pattern
Bram Moolenaar2795e212016-01-05 22:04:49 +01005803 {
5804 illegal = TRUE;
5805 break;
5806 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005807 if (curwin->w_s->b_syn_linecont_pat != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005809 emsg(_(e_syntax_sync_line_continuations_pattern_specified_twice));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 finished = TRUE;
5811 break;
5812 }
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005813 arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005814 if (*arg_end != *next_arg) // end delimiter not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 {
5816 illegal = TRUE;
5817 break;
5818 }
5819
5820 if (!eap->skip)
5821 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005822 // store the pattern and compiled regexp program
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005823 if ((curwin->w_s->b_syn_linecont_pat =
5824 vim_strnsave(next_arg + 1,
5825 arg_end - next_arg - 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
5827 finished = TRUE;
5828 break;
5829 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005830 curwin->w_s->b_syn_linecont_ic = curwin->w_s->b_syn_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005832 // Make 'cpoptions' empty, to avoid the 'l' flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 cpo_save = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01005834 p_cpo = empty_option;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005835 curwin->w_s->b_syn_linecont_prog =
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005836 vim_regcomp(curwin->w_s->b_syn_linecont_pat, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 p_cpo = cpo_save;
Bram Moolenaarf7512552013-06-06 14:55:19 +02005838#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005839 syn_clear_time(&curwin->w_s->b_syn_linecont_time);
5840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
Bram Moolenaar860cae12010-06-05 23:22:07 +02005842 if (curwin->w_s->b_syn_linecont_prog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005844 VIM_CLEAR(curwin->w_s->b_syn_linecont_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 finished = TRUE;
5846 break;
5847 }
5848 }
5849 next_arg = skipwhite(arg_end + 1);
5850 }
5851 else
5852 {
5853 eap->arg = next_arg;
5854 if (STRCMP(key, "MATCH") == 0)
5855 syn_cmd_match(eap, TRUE);
5856 else if (STRCMP(key, "REGION") == 0)
5857 syn_cmd_region(eap, TRUE);
5858 else if (STRCMP(key, "CLEAR") == 0)
5859 syn_cmd_clear(eap, TRUE);
5860 else
5861 illegal = TRUE;
5862 finished = TRUE;
5863 break;
5864 }
5865 arg_start = next_arg;
5866 }
5867 vim_free(key);
5868 if (illegal)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005869 semsg(_(e_illegal_arguments_str), arg_start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 else if (!finished)
5871 {
Bram Moolenaar63b91732021-08-05 20:40:03 +02005872 set_nextcmd(eap, arg_start);
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005873 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005874 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 }
5876}
5877
5878/*
5879 * Convert a line of highlight group names into a list of group ID numbers.
5880 * "arg" should point to the "contains" or "nextgroup" keyword.
5881 * "arg" is advanced to after the last group name.
5882 * Careful: the argument is modified (NULs added).
5883 * returns FAIL for some error, OK for success.
5884 */
5885 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005886get_id_list(
5887 char_u **arg,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005888 int keylen, // length of keyword
5889 short **list, // where to store the resulting list, if not
5890 // NULL, the list is silently skipped!
Bram Moolenaarde318c52017-01-17 16:27:10 +01005891 int skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
5893 char_u *p = NULL;
5894 char_u *end;
5895 int round;
5896 int count;
5897 int total_count = 0;
5898 short *retval = NULL;
5899 char_u *name;
5900 regmatch_T regmatch;
5901 int id;
5902 int i;
5903 int failed = FALSE;
5904
5905 /*
5906 * We parse the list twice:
5907 * round == 1: count the number of items, allocate the array.
5908 * round == 2: fill the array with the items.
5909 * In round 1 new groups may be added, causing the number of items to
5910 * grow when a regexp is used. In that case round 1 is done once again.
5911 */
5912 for (round = 1; round <= 2; ++round)
5913 {
5914 /*
5915 * skip "contains"
5916 */
5917 p = skipwhite(*arg + keylen);
5918 if (*p != '=')
5919 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005920 semsg(_(e_missing_equal_sign_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 break;
5922 }
5923 p = skipwhite(p + 1);
Bram Moolenaar1966c242020-04-20 22:42:32 +02005924 if (ends_excmd2(*arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005926 semsg(_(e_empty_argument_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 break;
5928 }
5929
5930 /*
5931 * parse the arguments after "contains"
5932 */
5933 count = 0;
Bram Moolenaar1966c242020-04-20 22:42:32 +02005934 while (!ends_excmd2(*arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005936 for (end = p; *end && !VIM_ISWHITE(*end) && *end != ','; ++end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 ;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005938 name = alloc(end - p + 3); // leave room for "^$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 if (name == NULL)
5940 {
5941 failed = TRUE;
5942 break;
5943 }
Bram Moolenaarce0842a2005-07-18 21:58:11 +00005944 vim_strncpy(name + 1, p, end - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 if ( STRCMP(name + 1, "ALLBUT") == 0
5946 || STRCMP(name + 1, "ALL") == 0
5947 || STRCMP(name + 1, "TOP") == 0
5948 || STRCMP(name + 1, "CONTAINED") == 0)
5949 {
5950 if (TOUPPER_ASC(**arg) != 'C')
5951 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005952 semsg(_(e_str_not_allowed_here), name + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 failed = TRUE;
5954 vim_free(name);
5955 break;
5956 }
5957 if (count != 0)
5958 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005959 semsg(_(e_str_must_be_first_in_contains_list), name + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 failed = TRUE;
5961 vim_free(name);
5962 break;
5963 }
5964 if (name[1] == 'A')
Bram Moolenaar2e240bd2021-04-14 11:15:08 +02005965 id = SYNID_ALLBUT + current_syn_inc_tag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 else if (name[1] == 'T')
Bram Moolenaar2e240bd2021-04-14 11:15:08 +02005967 {
5968 if (curwin->w_s->b_syn_topgrp >= SYNID_CLUSTER)
5969 id = curwin->w_s->b_syn_topgrp;
5970 else
5971 id = SYNID_TOP + current_syn_inc_tag;
5972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 else
Bram Moolenaar2e240bd2021-04-14 11:15:08 +02005974 id = SYNID_CONTAINED + current_syn_inc_tag;
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 }
5977 else if (name[1] == '@')
5978 {
Bram Moolenaareb46f8f2017-01-17 19:48:53 +01005979 if (skip)
5980 id = -1;
5981 else
Bram Moolenaarde318c52017-01-17 16:27:10 +01005982 id = syn_check_cluster(name + 2, (int)(end - p - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 }
5984 else
5985 {
5986 /*
5987 * Handle full group name.
5988 */
5989 if (vim_strpbrk(name + 1, (char_u *)"\\.*^$~[") == NULL)
5990 id = syn_check_group(name + 1, (int)(end - p));
5991 else
5992 {
5993 /*
5994 * Handle match of regexp with group names.
5995 */
5996 *name = '^';
5997 STRCAT(name, "$");
5998 regmatch.regprog = vim_regcomp(name, RE_MAGIC);
5999 if (regmatch.regprog == NULL)
6000 {
6001 failed = TRUE;
6002 vim_free(name);
6003 break;
6004 }
6005
6006 regmatch.rm_ic = TRUE;
6007 id = 0;
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02006008 for (i = highlight_num_groups(); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02006010 if (vim_regexec(&regmatch, highlight_group_name(i),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 (colnr_T)0))
6012 {
6013 if (round == 2)
6014 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006015 // Got more items than expected; can happen
6016 // when adding items that match:
6017 // "contains=a.*b,axb".
6018 // Go back to first round
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (count >= total_count)
6020 {
6021 vim_free(retval);
6022 round = 1;
6023 }
6024 else
6025 retval[count] = i + 1;
6026 }
6027 ++count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006028 id = -1; // remember that we found one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 }
6030 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006031 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 }
6033 }
6034 vim_free(name);
6035 if (id == 0)
6036 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006037 semsg(_(e_unknown_group_name_str), p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 failed = TRUE;
6039 break;
6040 }
6041 if (id > 0)
6042 {
6043 if (round == 2)
6044 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006045 // Got more items than expected, go back to first round
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 if (count >= total_count)
6047 {
6048 vim_free(retval);
6049 round = 1;
6050 }
6051 else
6052 retval[count] = id;
6053 }
6054 ++count;
6055 }
6056 p = skipwhite(end);
6057 if (*p != ',')
6058 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006059 p = skipwhite(p + 1); // skip comma in between arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061 if (failed)
6062 break;
6063 if (round == 1)
6064 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006065 retval = ALLOC_MULT(short, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (retval == NULL)
6067 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006068 retval[count] = 0; // zero means end of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 total_count = count;
6070 }
6071 }
6072
6073 *arg = p;
6074 if (failed || retval == NULL)
6075 {
6076 vim_free(retval);
6077 return FAIL;
6078 }
6079
6080 if (*list == NULL)
6081 *list = retval;
6082 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006083 vim_free(retval); // list already found, don't overwrite it
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
6085 return OK;
6086}
6087
6088/*
6089 * Make a copy of an ID list.
6090 */
6091 static short *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006092copy_id_list(short *list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
6094 int len;
6095 int count;
6096 short *retval;
6097
6098 if (list == NULL)
6099 return NULL;
6100
6101 for (count = 0; list[count]; ++count)
6102 ;
6103 len = (count + 1) * sizeof(short);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006104 retval = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (retval != NULL)
6106 mch_memmove(retval, list, (size_t)len);
6107
6108 return retval;
6109}
6110
6111/*
6112 * Check if syntax group "ssp" is in the ID list "list" of "cur_si".
6113 * "cur_si" can be NULL if not checking the "containedin" list.
6114 * Used to check if a syntax item is in the "contains" or "nextgroup" list of
6115 * the current item.
6116 * This function is called very often, keep it fast!!
6117 */
6118 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006119in_id_list(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006120 stateitem_T *cur_si, // current item or NULL
6121 short *list, // id list
6122 struct sp_syn *ssp, // group id and ":syn include" tag of group
6123 int contained) // group id is contained
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 int retval;
6126 short *scl_list;
6127 short item;
6128 short id = ssp->id;
6129 static int depth = 0;
6130 int r;
6131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006132 // If ssp has a "containedin" list and "cur_si" is in it, return TRUE.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006133 if (cur_si != NULL && ssp->cont_in_list != NULL
6134 && !(cur_si->si_flags & HL_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006136 // Ignore transparent items without a contains argument. Double check
6137 // that we don't go back past the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 while ((cur_si->si_flags & HL_TRANS_CONT)
6139 && cur_si > (stateitem_T *)(current_state.ga_data))
6140 --cur_si;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006141 // cur_si->si_idx is -1 for keywords, these never contain anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (cur_si->si_idx >= 0 && in_id_list(NULL, ssp->cont_in_list,
Bram Moolenaar860cae12010-06-05 23:22:07 +02006143 &(SYN_ITEMS(syn_block)[cur_si->si_idx].sp_syn),
6144 SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 return TRUE;
6146 }
6147
6148 if (list == NULL)
6149 return FALSE;
6150
6151 /*
6152 * If list is ID_LIST_ALL, we are in a transparent item that isn't
6153 * inside anything. Only allow not-contained groups.
6154 */
6155 if (list == ID_LIST_ALL)
6156 return !contained;
6157
6158 /*
6159 * If the first item is "ALLBUT", return TRUE if "id" is NOT in the
6160 * contains list. We also require that "id" is at the same ":syn include"
6161 * level as the list.
6162 */
6163 item = *list;
6164 if (item >= SYNID_ALLBUT && item < SYNID_CLUSTER)
6165 {
6166 if (item < SYNID_TOP)
6167 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006168 // ALL or ALLBUT: accept all groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (item - SYNID_ALLBUT != ssp->inc_tag)
6170 return FALSE;
6171 }
6172 else if (item < SYNID_CONTAINED)
6173 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006174 // TOP: accept all not-contained groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (item - SYNID_TOP != ssp->inc_tag || contained)
6176 return FALSE;
6177 }
6178 else
6179 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006180 // CONTAINED: accept all contained groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (item - SYNID_CONTAINED != ssp->inc_tag || !contained)
6182 return FALSE;
6183 }
6184 item = *++list;
6185 retval = FALSE;
6186 }
6187 else
6188 retval = TRUE;
6189
6190 /*
6191 * Return "retval" if id is in the contains list.
6192 */
6193 while (item != 0)
6194 {
6195 if (item == id)
6196 return retval;
6197 if (item >= SYNID_CLUSTER)
6198 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006199 scl_list = SYN_CLSTR(syn_block)[item - SYNID_CLUSTER].scl_list;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006200 // restrict recursiveness to 30 to avoid an endless loop for a
6201 // cluster that includes itself (indirectly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (scl_list != NULL && depth < 30)
6203 {
6204 ++depth;
6205 r = in_id_list(NULL, scl_list, ssp, contained);
6206 --depth;
6207 if (r)
6208 return retval;
6209 }
6210 }
6211 item = *++list;
6212 }
6213 return !retval;
6214}
6215
6216struct subcommand
6217{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006218 char *name; // subcommand name
6219 void (*func)(exarg_T *, int); // function to call
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220};
6221
6222static struct subcommand subcommands[] =
6223{
6224 {"case", syn_cmd_case},
6225 {"clear", syn_cmd_clear},
6226 {"cluster", syn_cmd_cluster},
Bram Moolenaar860cae12010-06-05 23:22:07 +02006227 {"conceal", syn_cmd_conceal},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {"enable", syn_cmd_enable},
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006229 {"foldlevel", syn_cmd_foldlevel},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {"include", syn_cmd_include},
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006231 {"iskeyword", syn_cmd_iskeyword},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {"keyword", syn_cmd_keyword},
6233 {"list", syn_cmd_list},
6234 {"manual", syn_cmd_manual},
6235 {"match", syn_cmd_match},
6236 {"on", syn_cmd_on},
6237 {"off", syn_cmd_off},
6238 {"region", syn_cmd_region},
6239 {"reset", syn_cmd_reset},
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006240 {"spell", syn_cmd_spell},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {"sync", syn_cmd_sync},
6242 {"", syn_cmd_list},
6243 {NULL, NULL}
6244};
6245
6246/*
6247 * ":syntax".
6248 * This searches the subcommands[] table for the subcommand name, and calls a
6249 * syntax_subcommand() function to do the rest.
6250 */
6251 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006252ex_syntax(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 char_u *arg = eap->arg;
6255 char_u *subcmd_end;
6256 char_u *subcmd_name;
6257 int i;
6258
6259 syn_cmdlinep = eap->cmdlinep;
6260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006261 // isolate subcommand name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 for (subcmd_end = arg; ASCII_ISALPHA(*subcmd_end); ++subcmd_end)
6263 ;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006264 subcmd_name = vim_strnsave(arg, subcmd_end - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (subcmd_name != NULL)
6266 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006267 if (eap->skip) // skip error messages for all subcommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 ++emsg_skip;
6269 for (i = 0; ; ++i)
6270 {
6271 if (subcommands[i].name == NULL)
6272 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006273 semsg(_(e_invalid_syntax_subcommand_str), subcmd_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 break;
6275 }
6276 if (STRCMP(subcmd_name, (char_u *)subcommands[i].name) == 0)
6277 {
6278 eap->arg = skipwhite(subcmd_end);
6279 (subcommands[i].func)(eap, FALSE);
6280 break;
6281 }
6282 }
6283 vim_free(subcmd_name);
6284 if (eap->skip)
6285 --emsg_skip;
6286 }
6287}
6288
Bram Moolenaar860cae12010-06-05 23:22:07 +02006289 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006290ex_ownsyntax(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaar1950c352010-06-06 15:21:10 +02006292 char_u *old_value;
6293 char_u *new_value;
6294
Bram Moolenaar860cae12010-06-05 23:22:07 +02006295 if (curwin->w_s == &curwin->w_buffer->b_s)
6296 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006297 curwin->w_s = ALLOC_ONE(synblock_T);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006298 memset(curwin->w_s, 0, sizeof(synblock_T));
Bram Moolenaar670acbc2015-08-25 11:58:36 +02006299 hash_init(&curwin->w_s->b_keywtab);
6300 hash_init(&curwin->w_s->b_keywtab_ic);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006301#ifdef FEAT_SPELL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006302 // TODO: keep the spell checking as it was.
6303 curwin->w_p_spell = FALSE; // No spell checking
Bram Moolenaard1f76af2020-09-13 22:37:34 +02006304 // make sure option values are "empty_option" instead of NULL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006305 clear_string_option(&curwin->w_s->b_p_spc);
6306 clear_string_option(&curwin->w_s->b_p_spf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006307 clear_string_option(&curwin->w_s->b_p_spl);
Bram Moolenaard1f76af2020-09-13 22:37:34 +02006308 clear_string_option(&curwin->w_s->b_p_spo);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006309#endif
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006310 clear_string_option(&curwin->w_s->b_syn_isk);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006311 }
Bram Moolenaar1950c352010-06-06 15:21:10 +02006312
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006313 // save value of b:current_syntax
Bram Moolenaar1950c352010-06-06 15:21:10 +02006314 old_value = get_var_value((char_u *)"b:current_syntax");
6315 if (old_value != NULL)
6316 old_value = vim_strsave(old_value);
6317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006318 // Apply the "syntax" autocommand event, this finds and loads the syntax
6319 // file.
Bram Moolenaar860cae12010-06-05 23:22:07 +02006320 apply_autocmds(EVENT_SYNTAX, eap->arg, curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1950c352010-06-06 15:21:10 +02006321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006322 // move value of b:current_syntax to w:current_syntax
Bram Moolenaar1950c352010-06-06 15:21:10 +02006323 new_value = get_var_value((char_u *)"b:current_syntax");
Bram Moolenaare0c6a652010-06-06 23:10:19 +02006324 if (new_value != NULL)
6325 set_internal_string_var((char_u *)"w:current_syntax", new_value);
Bram Moolenaar1950c352010-06-06 15:21:10 +02006326
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006327 // restore value of b:current_syntax
Bram Moolenaare0c6a652010-06-06 23:10:19 +02006328 if (old_value == NULL)
6329 do_unlet((char_u *)"b:current_syntax", TRUE);
6330 else
Bram Moolenaar1950c352010-06-06 15:21:10 +02006331 {
6332 set_internal_string_var((char_u *)"b:current_syntax", old_value);
6333 vim_free(old_value);
6334 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006335}
6336
6337 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006338syntax_present(win_T *win)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006339{
6340 return (win->w_s->b_syn_patterns.ga_len != 0
6341 || win->w_s->b_syn_clusters.ga_len != 0
6342 || win->w_s->b_keywtab.ht_used > 0
6343 || win->w_s->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344}
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347static enum
6348{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006349 EXP_SUBCMD, // expand ":syn" sub-commands
6350 EXP_CASE, // expand ":syn case" arguments
6351 EXP_SPELL, // expand ":syn spell" arguments
6352 EXP_SYNC // expand ":syn sync" arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353} expand_what;
6354
Bram Moolenaar4f688582007-07-24 12:34:30 +00006355/*
6356 * Reset include_link, include_default, include_none to 0.
6357 * Called when we are done expanding.
6358 */
6359 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006360reset_expand_highlight(void)
Bram Moolenaar4f688582007-07-24 12:34:30 +00006361{
6362 include_link = include_default = include_none = 0;
6363}
6364
6365/*
6366 * Handle command line completion for :match and :echohl command: Add "None"
6367 * as highlight group.
6368 */
6369 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006370set_context_in_echohl_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar4f688582007-07-24 12:34:30 +00006371{
6372 xp->xp_context = EXPAND_HIGHLIGHT;
6373 xp->xp_pattern = arg;
6374 include_none = 1;
6375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
6377/*
6378 * Handle command line completion for :syntax command.
6379 */
6380 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006381set_context_in_syntax_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 char_u *p;
6384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006385 // Default: expand subcommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 xp->xp_context = EXPAND_SYNTAX;
6387 expand_what = EXP_SUBCMD;
6388 xp->xp_pattern = arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00006389 include_link = 0;
6390 include_default = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006392 // (part of) subcommand already typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (*arg != NUL)
6394 {
6395 p = skiptowhite(arg);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006396 if (*p != NUL) // past first word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 {
6398 xp->xp_pattern = skipwhite(p);
6399 if (*skiptowhite(xp->xp_pattern) != NUL)
6400 xp->xp_context = EXPAND_NOTHING;
6401 else if (STRNICMP(arg, "case", p - arg) == 0)
6402 expand_what = EXP_CASE;
Bram Moolenaar2d028392017-01-08 18:28:22 +01006403 else if (STRNICMP(arg, "spell", p - arg) == 0)
6404 expand_what = EXP_SPELL;
6405 else if (STRNICMP(arg, "sync", p - arg) == 0)
6406 expand_what = EXP_SYNC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 else if ( STRNICMP(arg, "keyword", p - arg) == 0
6408 || STRNICMP(arg, "region", p - arg) == 0
6409 || STRNICMP(arg, "match", p - arg) == 0
6410 || STRNICMP(arg, "list", p - arg) == 0)
6411 xp->xp_context = EXPAND_HIGHLIGHT;
6412 else
6413 xp->xp_context = EXPAND_NOTHING;
6414 }
6415 }
6416}
6417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418/*
6419 * Function given to ExpandGeneric() to obtain the list syntax names for
6420 * expansion.
6421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006423get_syntax_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaar2d028392017-01-08 18:28:22 +01006425 switch (expand_what)
6426 {
6427 case EXP_SUBCMD:
6428 return (char_u *)subcommands[idx].name;
6429 case EXP_CASE:
6430 {
6431 static char *case_args[] = {"match", "ignore", NULL};
6432 return (char_u *)case_args[idx];
6433 }
6434 case EXP_SPELL:
6435 {
6436 static char *spell_args[] =
6437 {"toplevel", "notoplevel", "default", NULL};
6438 return (char_u *)spell_args[idx];
6439 }
6440 case EXP_SYNC:
6441 {
6442 static char *sync_args[] =
6443 {"ccomment", "clear", "fromstart",
6444 "linebreaks=", "linecont", "lines=", "match",
6445 "maxlines=", "minlines=", "region", NULL};
6446 return (char_u *)sync_args[idx];
6447 }
6448 }
6449 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450}
6451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453/*
6454 * Function called for expression evaluation: get syntax ID at file position.
6455 */
6456 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006457syn_get_id(
6458 win_T *wp,
6459 long lnum,
6460 colnr_T col,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006461 int trans, // remove transparency
6462 int *spellp, // return: can do spell checking
6463 int keep_state) // keep state of char at "col"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006465 // When the position is not after the current position and in the same
zeertzjqca7e86c2022-04-16 16:49:24 +01006466 // line of the same window with the same buffer, need to restart parsing.
6467 if (wp != syn_win
6468 || wp->w_buffer != syn_buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 || lnum != current_lnum
Bram Moolenaar9d0ec2e2005-04-20 19:45:58 +00006470 || col < current_col)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02006471 syntax_start(wp, lnum);
Bram Moolenaar6773a342016-01-19 20:52:44 +01006472 else if (wp->w_buffer == syn_buf
6473 && lnum == current_lnum
6474 && col > current_col)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006475 // next_match may not be correct when moving around, e.g. with the
6476 // "skip" expression in searchpair()
Bram Moolenaar6773a342016-01-19 20:52:44 +01006477 next_match_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006479 (void)get_syntax_attr(col, spellp, keep_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
6481 return (trans ? current_trans_id : current_id);
6482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaar860cae12010-06-05 23:22:07 +02006484#if defined(FEAT_CONCEAL) || defined(PROTO)
6485/*
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006486 * Get extra information about the syntax item. Must be called right after
6487 * get_syntax_attr().
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02006488 * Stores the current item sequence nr in "*seqnrp".
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006489 * Returns the current flags.
6490 */
6491 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006492get_syntax_info(int *seqnrp)
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006493{
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02006494 *seqnrp = current_seqnr;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006495 return current_flags;
6496}
6497
6498/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02006499 * Return conceal substitution character
6500 */
6501 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006502syn_get_sub_char(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006503{
6504 return current_sub_char;
6505}
6506#endif
6507
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006508#if defined(FEAT_EVAL) || defined(PROTO)
6509/*
6510 * Return the syntax ID at position "i" in the current stack.
6511 * The caller must have called syn_get_id() before to fill the stack.
6512 * Returns -1 when "i" is out of range.
6513 */
6514 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006515syn_get_stack_item(int i)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006516{
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006517 if (i >= current_state.ga_len)
6518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006519 // Need to invalidate the state, because we didn't properly finish it
6520 // for the last character, "keep_state" was TRUE.
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006521 invalidate_current_state();
6522 current_col = MAXCOL;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006523 return -1;
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006524 }
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006525 return CUR_STATE(i).si_id;
6526}
6527#endif
6528
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006530 static int
6531syn_cur_foldlevel(void)
6532{
6533 int level = 0;
6534 int i;
6535
6536 for (i = 0; i < current_state.ga_len; ++i)
6537 if (CUR_STATE(i).si_flags & HL_FOLD)
6538 ++level;
6539 return level;
6540}
6541
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542/*
6543 * Function called to get folding level for line "lnum" in window "wp".
6544 */
6545 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006546syn_get_foldlevel(win_T *wp, long lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 int level = 0;
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006549 int low_level;
6550 int cur_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006552 // Return quickly when there are no fold items at all.
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02006553 if (wp->w_s->b_syn_folditems != 0
6554 && !wp->w_s->b_syn_error
6555# ifdef SYN_TIME_LIMIT
6556 && !wp->w_s->b_syn_slow
6557# endif
6558 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02006560 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006562 // Start with the fold level at the start of the line.
6563 level = syn_cur_foldlevel();
6564
6565 if (wp->w_s->b_syn_foldlevel == SYNFLD_MINIMUM)
6566 {
6567 // Find the lowest fold level that is followed by a higher one.
6568 cur_level = level;
6569 low_level = cur_level;
6570 while (!current_finished)
6571 {
6572 (void)syn_current_attr(FALSE, FALSE, NULL, FALSE);
6573 cur_level = syn_cur_foldlevel();
6574 if (cur_level < low_level)
6575 low_level = cur_level;
6576 else if (cur_level > low_level)
6577 level = low_level;
6578 ++current_col;
6579 }
6580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 }
6582 if (level > wp->w_p_fdn)
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 level = wp->w_p_fdn;
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006585 if (level < 0)
6586 level = 0;
6587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 return level;
6589}
6590#endif
6591
Bram Moolenaar01615492015-02-03 13:00:38 +01006592#if defined(FEAT_PROFILE) || defined(PROTO)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006593/*
6594 * ":syntime".
6595 */
6596 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006597ex_syntime(exarg_T *eap)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006598{
6599 if (STRCMP(eap->arg, "on") == 0)
6600 syn_time_on = TRUE;
6601 else if (STRCMP(eap->arg, "off") == 0)
6602 syn_time_on = FALSE;
6603 else if (STRCMP(eap->arg, "clear") == 0)
6604 syntime_clear();
6605 else if (STRCMP(eap->arg, "report") == 0)
6606 syntime_report();
6607 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006608 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006609}
6610
6611 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006612syn_clear_time(syn_time_T *st)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006613{
6614 profile_zero(&st->total);
6615 profile_zero(&st->slowest);
6616 st->count = 0;
6617 st->match = 0;
6618}
6619
6620/*
6621 * Clear the syntax timing for the current buffer.
6622 */
6623 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006624syntime_clear(void)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006625{
6626 int idx;
6627 synpat_T *spp;
6628
6629 if (!syntax_present(curwin))
6630 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006631 msg(_(msg_no_items));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006632 return;
6633 }
6634 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx)
6635 {
6636 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
6637 syn_clear_time(&spp->sp_time);
6638 }
6639}
6640
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006641/*
6642 * Function given to ExpandGeneric() to obtain the possible arguments of the
6643 * ":syntime {on,off,clear,report}" command.
6644 */
6645 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006646get_syntime_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006647{
6648 switch (idx)
6649 {
6650 case 0: return (char_u *)"on";
6651 case 1: return (char_u *)"off";
6652 case 2: return (char_u *)"clear";
6653 case 3: return (char_u *)"report";
6654 }
6655 return NULL;
6656}
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006657
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006658typedef struct
6659{
6660 proftime_T total;
6661 int count;
6662 int match;
6663 proftime_T slowest;
6664 proftime_T average;
6665 int id;
6666 char_u *pattern;
6667} time_entry_T;
6668
6669 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006670syn_compare_syntime(const void *v1, const void *v2)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006671{
6672 const time_entry_T *s1 = v1;
6673 const time_entry_T *s2 = v2;
6674
6675 return profile_cmp(&s1->total, &s2->total);
6676}
6677
6678/*
6679 * Clear the syntax timing for the current buffer.
6680 */
6681 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006682syntime_report(void)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006683{
6684 int idx;
6685 synpat_T *spp;
Bram Moolenaar81319672020-11-25 11:47:39 +01006686# if defined(FEAT_RELTIME) && defined(FEAT_FLOAT)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006687 proftime_T tm;
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006688# endif
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006689 int len;
6690 proftime_T total_total;
6691 int total_count = 0;
6692 garray_T ga;
6693 time_entry_T *p;
6694
6695 if (!syntax_present(curwin))
6696 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006697 msg(_(msg_no_items));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006698 return;
6699 }
6700
6701 ga_init2(&ga, sizeof(time_entry_T), 50);
6702 profile_zero(&total_total);
6703 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx)
6704 {
6705 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
6706 if (spp->sp_time.count > 0)
6707 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02006708 (void)ga_grow(&ga, 1);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006709 p = ((time_entry_T *)ga.ga_data) + ga.ga_len;
6710 p->total = spp->sp_time.total;
6711 profile_add(&total_total, &spp->sp_time.total);
6712 p->count = spp->sp_time.count;
6713 p->match = spp->sp_time.match;
6714 total_count += spp->sp_time.count;
6715 p->slowest = spp->sp_time.slowest;
Bram Moolenaar81319672020-11-25 11:47:39 +01006716# if defined(FEAT_RELTIME) && defined(FEAT_FLOAT)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006717 profile_divide(&spp->sp_time.total, spp->sp_time.count, &tm);
6718 p->average = tm;
6719# endif
6720 p->id = spp->sp_syn.id;
6721 p->pattern = spp->sp_pattern;
6722 ++ga.ga_len;
6723 }
6724 }
6725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006726 // Sort on total time. Skip if there are no items to avoid passing NULL
6727 // pointer to qsort().
Bram Moolenaara2162552017-01-08 17:46:20 +01006728 if (ga.ga_len > 1)
6729 qsort(ga.ga_data, (size_t)ga.ga_len, sizeof(time_entry_T),
Bram Moolenaar4e312962013-06-06 21:19:51 +02006730 syn_compare_syntime);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006731
Bram Moolenaar32526b32019-01-19 17:43:09 +01006732 msg_puts_title(_(" TOTAL COUNT MATCH SLOWEST AVERAGE NAME PATTERN"));
6733 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006734 for (idx = 0; idx < ga.ga_len && !got_int; ++idx)
6735 {
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006736 p = ((time_entry_T *)ga.ga_data) + idx;
6737
Bram Moolenaar32526b32019-01-19 17:43:09 +01006738 msg_puts(profile_msg(&p->total));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006739 msg_puts(" "); // make sure there is always a separating space
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006740 msg_advance(13);
6741 msg_outnum(p->count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006742 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006743 msg_advance(20);
6744 msg_outnum(p->match);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006745 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006746 msg_advance(26);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006747 msg_puts(profile_msg(&p->slowest));
6748 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006749 msg_advance(38);
6750# ifdef FEAT_FLOAT
Bram Moolenaar32526b32019-01-19 17:43:09 +01006751 msg_puts(profile_msg(&p->average));
6752 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006753# endif
6754 msg_advance(50);
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02006755 msg_outtrans(highlight_group_name(p->id - 1));
Bram Moolenaar32526b32019-01-19 17:43:09 +01006756 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006757
6758 msg_advance(69);
6759 if (Columns < 80)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006760 len = 20; // will wrap anyway
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006761 else
6762 len = Columns - 70;
6763 if (len > (int)STRLEN(p->pattern))
6764 len = (int)STRLEN(p->pattern);
6765 msg_outtrans_len(p->pattern, len);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006766 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006767 }
Bram Moolenaar45fc5392013-06-07 19:48:39 +02006768 ga_clear(&ga);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006769 if (!got_int)
6770 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006771 msg_puts("\n");
6772 msg_puts(profile_msg(&total_total));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006773 msg_advance(13);
6774 msg_outnum(total_count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006775 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006776 }
6777}
6778#endif
6779
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006780#endif // FEAT_SYN_HL