blob: 5c9dd0b0225c76591d80637391a213060fed80c8 [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
Bram Moolenaare35a52a2020-05-31 19:48:53 +020033static char e_illegal_arg[] = N_("E390: Illegal argument: %s");
34
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/*
36 * The patterns that are being searched for are stored in a syn_pattern.
37 * A match item consists of one pattern.
38 * A start/end item consists of n start patterns and m end patterns.
39 * A start/skip/end item consists of n start patterns, one skip pattern and m
40 * end patterns.
41 * For the latter two, the patterns are always consecutive: start-skip-end.
42 *
43 * A character offset can be given for the matched text (_m_start and _m_end)
44 * and for the actually highlighted text (_h_start and _h_end).
Bram Moolenaar36f92302018-02-24 21:36:34 +010045 *
46 * Note that ordering of members is optimized to reduce padding.
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 */
48typedef struct syn_pattern
49{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010050 char sp_type; // see SPTYPE_ defines below
51 char sp_syncing; // this item used for syncing
52 short sp_syn_match_id; // highlight group ID of pattern
53 short sp_off_flags; // see below
54 int sp_offsets[SPO_COUNT]; // offsets
55 int sp_flags; // see HL_ defines below
Bram Moolenaar860cae12010-06-05 23:22:07 +020056#ifdef FEAT_CONCEAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010057 int sp_cchar; // conceal substitute character
Bram Moolenaar860cae12010-06-05 23:22:07 +020058#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010059 int sp_ic; // ignore-case flag for sp_prog
60 int sp_sync_idx; // sync item index (syncing only)
61 int sp_line_id; // ID of last line where tried
62 int sp_startcol; // next match in sp_line_id line
63 short *sp_cont_list; // cont. group IDs, if non-zero
64 short *sp_next_list; // next group IDs, if non-zero
65 struct sp_syn sp_syn; // struct passed to in_id_list()
66 char_u *sp_pattern; // regexp to match, pattern
67 regprog_T *sp_prog; // regexp to match, program
Bram Moolenaarf7512552013-06-06 14:55:19 +020068#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +020069 syn_time_T sp_time;
70#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000071} synpat_T;
72
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010073// The sp_off_flags are computed like this:
74// offset from the start of the matched text: (1 << SPO_XX_OFF)
75// offset from the end of the matched text: (1 << (SPO_XX_OFF + SPO_COUNT))
76// When both are present, only one is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010078#define SPTYPE_MATCH 1 // match keyword with this group ID
79#define SPTYPE_START 2 // match a regexp, start of item
80#define SPTYPE_END 3 // match a regexp, end of item
81#define SPTYPE_SKIP 4 // match a regexp, skip within item
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
84#define SYN_ITEMS(buf) ((synpat_T *)((buf)->b_syn_patterns.ga_data))
85
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010086#define NONE_IDX -2 // value of sp_sync_idx for "NONE"
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
88/*
89 * Flags for b_syn_sync_flags:
90 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010091#define SF_CCOMMENT 0x01 // sync on a C-style comment
92#define SF_MATCH 0x02 // sync by matching a pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
94#define SYN_STATE_P(ssp) ((bufstate_T *)((ssp)->ga_data))
95
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010096#define MAXKEYWLEN 80 // maximum length of a keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +000097
98/*
99 * The attributes of the syntax item that has been recognized.
100 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100101static int current_attr = 0; // attr of current syntax word
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#ifdef FEAT_EVAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100103static int current_id = 0; // ID of current char for syn_get_id()
104static int current_trans_id = 0; // idem, transparency removed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200106#ifdef FEAT_CONCEAL
107static int current_flags = 0;
Bram Moolenaar6e202e52010-07-28 18:14:45 +0200108static int current_seqnr = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200109static int current_sub_char = 0;
110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
Bram Moolenaar217ad922005-03-20 22:37:15 +0000112typedef struct syn_cluster_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100114 char_u *scl_name; // syntax cluster name
115 char_u *scl_name_u; // uppercase of scl_name
116 short *scl_list; // IDs in this syntax cluster
Bram Moolenaar217ad922005-03-20 22:37:15 +0000117} syn_cluster_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
120 * Methods of combining two clusters
121 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100122#define CLUSTER_REPLACE 1 // replace first list with second
123#define CLUSTER_ADD 2 // add second list to first
124#define CLUSTER_SUBTRACT 3 // subtract second list from first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
Bram Moolenaar217ad922005-03-20 22:37:15 +0000126#define SYN_CLSTR(buf) ((syn_cluster_T *)((buf)->b_syn_clusters.ga_data))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128/*
129 * Syntax group IDs have different types:
Bram Moolenaar42431a72011-04-01 14:44:59 +0200130 * 0 - 19999 normal syntax groups
131 * 20000 - 20999 ALLBUT indicator (current_syn_inc_tag added)
132 * 21000 - 21999 TOP indicator (current_syn_inc_tag added)
133 * 22000 - 22999 CONTAINED indicator (current_syn_inc_tag added)
134 * 23000 - 32767 cluster IDs (subtract SYNID_CLUSTER for the cluster ID)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100136#define SYNID_ALLBUT MAX_HL_ID // syntax group ID for contains=ALLBUT
137#define SYNID_TOP 21000 // syntax group ID for contains=TOP
138#define SYNID_CONTAINED 22000 // syntax group ID for contains=CONTAINED
139#define SYNID_CLUSTER 23000 // first syntax group ID for clusters
Bram Moolenaar42431a72011-04-01 14:44:59 +0200140
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100141#define MAX_SYN_INC_TAG 999 // maximum before the above overflow
Bram Moolenaar42431a72011-04-01 14:44:59 +0200142#define MAX_CLUSTER_ID (32767 - SYNID_CLUSTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144/*
145 * Annoying Hack(TM): ":syn include" needs this pointer to pass to
146 * expand_filename(). Most of the other syntax commands don't need it, so
147 * instead of passing it to them, we stow it here.
148 */
149static char_u **syn_cmdlinep;
150
151/*
152 * Another Annoying Hack(TM): To prevent rules from other ":syn include"'d
Bram Moolenaar56be9502010-06-06 14:20:26 +0200153 * files from leaking into ALLBUT lists, we assign a unique ID to the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 * rules in each ":syn include"'d file.
155 */
156static int current_syn_inc_tag = 0;
157static int running_syn_inc_tag = 0;
158
159/*
Bram Moolenaardad6b692005-01-25 22:14:34 +0000160 * In a hashtable item "hi_key" points to "keyword" in a keyentry.
161 * This avoids adding a pointer to the hashtable item.
162 * KE2HIKEY() converts a var pointer to a hashitem key pointer.
163 * HIKEY2KE() converts a hashitem key pointer to a var pointer.
164 * HI2KE() converts a hashitem pointer to a var pointer.
165 */
166static keyentry_T dumkey;
167#define KE2HIKEY(kp) ((kp)->keyword)
168#define HIKEY2KE(p) ((keyentry_T *)((p) - (dumkey.keyword - (char_u *)&dumkey)))
169#define HI2KE(hi) HIKEY2KE((hi)->hi_key)
170
171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 * To reduce the time spent in keepend(), remember at which level in the state
173 * stack the first item with "keepend" is present. When "-1", there is no
174 * "keepend" on the stack.
175 */
176static int keepend_level = -1;
177
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200178static char msg_no_items[] = N_("No Syntax items defined for this buffer");
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180/*
181 * For the current state we need to remember more than just the idx.
182 * When si_m_endpos.lnum is 0, the items other than si_idx are unknown.
183 * (The end positions have the column number of the next char)
184 */
185typedef struct state_item
186{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100187 int si_idx; // index of syntax pattern or
188 // KEYWORD_IDX
189 int si_id; // highlight group ID for keywords
190 int si_trans_id; // idem, transparency removed
191 int si_m_lnum; // lnum of the match
192 int si_m_startcol; // starting column of the match
193 lpos_T si_m_endpos; // just after end posn of the match
194 lpos_T si_h_startpos; // start position of the highlighting
195 lpos_T si_h_endpos; // end position of the highlighting
196 lpos_T si_eoe_pos; // end position of end pattern
197 int si_end_idx; // group ID for end pattern or zero
198 int si_ends; // if match ends before si_m_endpos
199 int si_attr; // attributes in this state
200 long si_flags; // HL_HAS_EOL flag in this state, and
201 // HL_SKIP* for si_next_list
Bram Moolenaar860cae12010-06-05 23:22:07 +0200202#ifdef FEAT_CONCEAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100203 int si_seqnr; // sequence number
204 int si_cchar; // substitution character for conceal
Bram Moolenaar860cae12010-06-05 23:22:07 +0200205#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100206 short *si_cont_list; // list of contained groups
207 short *si_next_list; // nextgroup IDs after this item ends
208 reg_extmatch_T *si_extmatch; // \z(...\) matches from start
209 // pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210} stateitem_T;
211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100212#define KEYWORD_IDX -1 // value of si_idx for keywords
213#define ID_LIST_ALL (short *)-1 // valid of si_cont_list for containing all
214 // but contained groups
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
Bram Moolenaarffbbcb52010-07-24 17:29:03 +0200216#ifdef FEAT_CONCEAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100217static int next_seqnr = 1; // value to use for si_seqnr
Bram Moolenaarffbbcb52010-07-24 17:29:03 +0200218#endif
219
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220/*
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000221 * Struct to reduce the number of arguments to get_syn_options(), it's used
222 * very often.
223 */
224typedef struct
225{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100226 int flags; // flags for contained and transparent
227 int keyword; // TRUE for ":syn keyword"
228 int *sync_idx; // syntax item for "grouphere" argument, NULL
229 // if not allowed
230 char has_cont_list; // TRUE if "cont_list" can be used
231 short *cont_list; // group IDs for "contains" argument
232 short *cont_in_list; // group IDs for "containedin" argument
233 short *next_list; // group IDs for "nextgroup" argument
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000234} syn_opt_arg_T;
235
236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 * The next possible match in the current line for any pattern is remembered,
238 * to avoid having to try for a match in each column.
239 * If next_match_idx == -1, not tried (in this line) yet.
240 * If next_match_col == MAXCOL, no match found in this line.
241 * (All end positions have the column of the char after the end)
242 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100243static int next_match_col; // column for start of next match
244static lpos_T next_match_m_endpos; // position for end of next match
245static lpos_T next_match_h_startpos; // pos. for highl. start of next match
246static lpos_T next_match_h_endpos; // pos. for highl. end of next match
247static int next_match_idx; // index of matched item
248static long next_match_flags; // flags for next match
249static lpos_T next_match_eos_pos; // end of start pattn (start region)
250static lpos_T next_match_eoe_pos; // pos. for end of end pattern
251static int next_match_end_idx; // ID of group for end pattn or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252static reg_extmatch_T *next_match_extmatch = NULL;
253
254/*
255 * A state stack is an array of integers or stateitem_T, stored in a
Bram Moolenaarf86db782018-10-25 13:31:37 +0200256 * garray_T. A state stack is invalid if its itemsize entry is zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 */
258#define INVALID_STATE(ssp) ((ssp)->ga_itemsize == 0)
259#define VALID_STATE(ssp) ((ssp)->ga_itemsize != 0)
260
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200261#define FOR_ALL_SYNSTATES(sb, sst) \
262 for ((sst) = (sb)->b_sst_first; (sst) != NULL; (sst) = (sst)->sst_next)
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264/*
265 * The current state (within the line) of the recognition engine.
266 * When current_state.ga_itemsize is 0 the current state is invalid.
267 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100268static win_T *syn_win; // current window for highlighting
269static buf_T *syn_buf; // current buffer for highlighting
270static synblock_T *syn_block; // current buffer for highlighting
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200271#ifdef FEAT_RELTIME
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100272static proftime_T *syn_tm; // timeout limit
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200273#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100274static linenr_T current_lnum = 0; // lnum of current state
275static colnr_T current_col = 0; // column of current state
276static int current_state_stored = 0; // TRUE if stored current state
277 // after setting current_finished
278static int current_finished = 0; // current line has been finished
279static garray_T current_state // current stack of state_items
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 = {0, 0, 0, 0, NULL};
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100281static short *current_next_list = NULL; // when non-zero, nextgroup list
282static int current_next_flags = 0; // flags for current_next_list
283static int current_line_id = 0; // unique number for current line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
285#define CUR_STATE(idx) ((stateitem_T *)(current_state.ga_data))[idx]
286
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100287static void syn_sync(win_T *wp, linenr_T lnum, synstate_T *last_valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100288static int syn_match_linecont(linenr_T lnum);
289static void syn_start_line(void);
290static void syn_update_ends(int startofline);
291static void syn_stack_alloc(void);
292static int syn_stack_cleanup(void);
293static void syn_stack_free_entry(synblock_T *block, synstate_T *p);
294static synstate_T *syn_stack_find_entry(linenr_T lnum);
295static synstate_T *store_current_state(void);
296static void load_current_state(synstate_T *from);
297static void invalidate_current_state(void);
298static int syn_stack_equal(synstate_T *sp);
299static void validate_current_state(void);
300static int syn_finish_line(int syncing);
301static int syn_current_attr(int syncing, int displaying, int *can_spell, int keep_state);
302static int did_match_already(int idx, garray_T *gap);
303static stateitem_T *push_next_match(stateitem_T *cur_si);
304static void check_state_ends(void);
305static void update_si_attr(int idx);
306static void check_keepend(void);
307static void update_si_end(stateitem_T *sip, int startcol, int force);
308static short *copy_id_list(short *list);
309static int in_id_list(stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained);
310static int push_current_state(int idx);
311static void pop_current_state(void);
Bram Moolenaarf7512552013-06-06 14:55:19 +0200312#ifdef FEAT_PROFILE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100313static void syn_clear_time(syn_time_T *tt);
314static void syntime_clear(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100315static void syntime_report(void);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200316static int syn_time_on = FALSE;
317# define IF_SYN_TIME(p) (p)
318#else
319# define IF_SYN_TIME(p) NULL
320typedef int syn_time_T;
321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100323static void syn_stack_apply_changes_block(synblock_T *block, buf_T *buf);
324static 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 +0000325
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100326static void limit_pos(lpos_T *pos, lpos_T *limit);
327static void limit_pos_zero(lpos_T *pos, lpos_T *limit);
328static void syn_add_end_off(lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra);
329static void syn_add_start_off(lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra);
330static char_u *syn_getcurline(void);
331static int syn_regexec(regmmatch_T *rmp, linenr_T lnum, colnr_T col, syn_time_T *st);
332static 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 +0100333static void syn_remove_pattern(synblock_T *block, int idx);
334static void syn_clear_pattern(synblock_T *block, int i);
335static void syn_clear_cluster(synblock_T *block, int i);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100336static void syn_clear_one(int id, int syncing);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100337static void syn_cmd_onoff(exarg_T *eap, char *name);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100338static void syn_lines_msg(void);
339static void syn_match_msg(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100340static void syn_list_one(int id, int syncing, int link_only);
341static void syn_list_cluster(int id);
342static void put_id_list(char_u *name, short *list, int attr);
343static void put_pattern(char *s, int c, synpat_T *spp, int attr);
344static int syn_list_keywords(int id, hashtab_T *ht, int did_header, int attr);
345static void syn_clear_keyword(int id, hashtab_T *ht);
346static void clear_keywtab(hashtab_T *ht);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100347static int syn_scl_namen2id(char_u *linep, int len);
348static int syn_check_cluster(char_u *pp, int len);
349static int syn_add_cluster(char_u *name);
350static void init_syn_patterns(void);
351static char_u *get_syn_pattern(char_u *arg, synpat_T *ci);
Bram Moolenaarde318c52017-01-17 16:27:10 +0100352static int get_id_list(char_u **arg, int keylen, short **list, int skip);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100353static void syn_combine_list(short **clstr1, short **clstr2, int list_op);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200355#if defined(FEAT_RELTIME) || defined(PROTO)
356/*
357 * Set the timeout used for syntax highlighting.
358 * Use NULL to reset, no timeout.
359 */
360 void
361syn_set_timeout(proftime_T *tm)
362{
363 syn_tm = tm;
364}
365#endif
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/*
368 * Start the syntax recognition for a line. This function is normally called
369 * from the screen updating, once for each displayed line.
370 * The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
371 * it. Careful: curbuf and curwin are likely to point to another buffer and
372 * window.
373 */
374 void
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200375syntax_start(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376{
377 synstate_T *p;
378 synstate_T *last_valid = NULL;
379 synstate_T *last_min_valid = NULL;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000380 synstate_T *sp, *prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 linenr_T parsed_lnum;
382 linenr_T first_stored;
383 int dist;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100384 static varnumber_T changedtick = 0; // remember the last change ID
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200386#ifdef FEAT_CONCEAL
387 current_sub_char = NUL;
388#endif
389
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 /*
391 * After switching buffers, invalidate current_state.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000392 * Also do this when a change was made, the current state may be invalid
393 * then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 */
Bram Moolenaarb681be12016-03-31 23:02:16 +0200395 if (syn_block != wp->w_s
396 || syn_buf != wp->w_buffer
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100397 || changedtick != CHANGEDTICK(syn_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {
399 invalidate_current_state();
400 syn_buf = wp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200401 syn_block = wp->w_s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100403 changedtick = CHANGEDTICK(syn_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 syn_win = wp;
405
406 /*
407 * Allocate syntax stack when needed.
408 */
409 syn_stack_alloc();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200410 if (syn_block->b_sst_array == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100411 return; // out of memory
Bram Moolenaar860cae12010-06-05 23:22:07 +0200412 syn_block->b_sst_lasttick = display_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 /*
415 * If the state of the end of the previous line is useful, store it.
416 */
417 if (VALID_STATE(&current_state)
418 && current_lnum < lnum
419 && current_lnum < syn_buf->b_ml.ml_line_count)
420 {
421 (void)syn_finish_line(FALSE);
422 if (!current_state_stored)
423 {
424 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +0000425 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 }
427
428 /*
429 * If the current_lnum is now the same as "lnum", keep the current
430 * state (this happens very often!). Otherwise invalidate
431 * current_state and figure it out below.
432 */
433 if (current_lnum != lnum)
434 invalidate_current_state();
435 }
436 else
437 invalidate_current_state();
438
439 /*
440 * Try to synchronize from a saved state in b_sst_array[].
441 * Only do this if lnum is not before and not to far beyond a saved state.
442 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200443 if (INVALID_STATE(&current_state) && syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100445 // Find last valid saved state before start_lnum.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200446 FOR_ALL_SYNSTATES(syn_block, p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {
448 if (p->sst_lnum > lnum)
449 break;
450 if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
451 {
452 last_valid = p;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200453 if (p->sst_lnum >= lnum - syn_block->b_syn_sync_minlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 last_min_valid = p;
455 }
456 }
457 if (last_min_valid != NULL)
458 load_current_state(last_min_valid);
459 }
460
461 /*
462 * If "lnum" is before or far beyond a line with a saved state, need to
463 * re-synchronize.
464 */
465 if (INVALID_STATE(&current_state))
466 {
467 syn_sync(wp, lnum, last_valid);
Bram Moolenaard6761c32011-06-19 04:54:21 +0200468 if (current_lnum == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100469 // First line is always valid, no matter "minlines".
Bram Moolenaard6761c32011-06-19 04:54:21 +0200470 first_stored = 1;
471 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100472 // Need to parse "minlines" lines before state can be considered
473 // valid to store.
Bram Moolenaard6761c32011-06-19 04:54:21 +0200474 first_stored = current_lnum + syn_block->b_syn_sync_minlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 else
477 first_stored = current_lnum;
478
479 /*
480 * Advance from the sync point or saved state until the current line.
481 * Save some entries for syncing with later on.
482 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200483 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000484 dist = 999999;
485 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200486 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 while (current_lnum < lnum)
488 {
489 syn_start_line();
490 (void)syn_finish_line(FALSE);
491 ++current_lnum;
492
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100493 // If we parsed at least "minlines" lines or started at a valid
494 // state, the current state is considered valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 if (current_lnum >= first_stored)
496 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100497 // Check if the saved state entry is for the current line and is
498 // equal to the current state. If so, then validate all saved
499 // states that depended on a change before the parsed line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 if (prev == NULL)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000501 prev = syn_stack_find_entry(current_lnum - 1);
502 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200503 sp = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 else
Bram Moolenaardbe31752008-01-13 16:40:19 +0000505 sp = prev;
506 while (sp != NULL && sp->sst_lnum < current_lnum)
507 sp = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (sp != NULL
509 && sp->sst_lnum == current_lnum
510 && syn_stack_equal(sp))
511 {
512 parsed_lnum = current_lnum;
513 prev = sp;
514 while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
515 {
516 if (sp->sst_lnum <= lnum)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100517 // valid state before desired line, use this one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 prev = sp;
519 else if (sp->sst_change_lnum == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100520 // past saved states depending on change, break here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 break;
522 sp->sst_change_lnum = 0;
523 sp = sp->sst_next;
524 }
525 load_current_state(prev);
526 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100527 // Store the state at this line when it's the first one, the line
528 // where we start parsing, or some distance from the previously
529 // saved state. But only when parsed at least 'minlines'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 else if (prev == NULL
531 || current_lnum == lnum
532 || current_lnum >= prev->sst_lnum + dist)
Bram Moolenaardbe31752008-01-13 16:40:19 +0000533 prev = store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 }
535
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100536 // This can take a long time: break when CTRL-C pressed. The current
537 // state will be wrong then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 line_breakcheck();
539 if (got_int)
540 {
541 current_lnum = lnum;
542 break;
543 }
544 }
545
546 syn_start_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547}
548
549/*
550 * We cannot simply discard growarrays full of state_items or buf_states; we
551 * have to manually release their extmatch pointers first.
552 */
553 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100554clear_syn_state(synstate_T *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 int i;
557 garray_T *gap;
558
559 if (p->sst_stacksize > SST_FIX_STATES)
560 {
561 gap = &(p->sst_union.sst_ga);
562 for (i = 0; i < gap->ga_len; i++)
563 unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
564 ga_clear(gap);
565 }
566 else
567 {
568 for (i = 0; i < p->sst_stacksize; i++)
569 unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
570 }
571}
572
573/*
574 * Cleanup the current_state stack.
575 */
576 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100577clear_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
579 int i;
580 stateitem_T *sip;
581
582 sip = (stateitem_T *)(current_state.ga_data);
583 for (i = 0; i < current_state.ga_len; i++)
584 unref_extmatch(sip[i].si_extmatch);
585 ga_clear(&current_state);
586}
587
588/*
589 * Try to find a synchronisation point for line "lnum".
590 *
591 * This sets current_lnum and the current state. One of three methods is
592 * used:
593 * 1. Search backwards for the end of a C-comment.
594 * 2. Search backwards for given sync patterns.
595 * 3. Simply start on a given number of lines above "lnum".
596 */
597 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100598syn_sync(
599 win_T *wp,
600 linenr_T start_lnum,
601 synstate_T *last_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
603 buf_T *curbuf_save;
604 win_T *curwin_save;
605 pos_T cursor_save;
606 int idx;
607 linenr_T lnum;
608 linenr_T end_lnum;
609 linenr_T break_lnum;
610 int had_sync_point;
611 stateitem_T *cur_si;
612 synpat_T *spp;
613 char_u *line;
614 int found_flags = 0;
615 int found_match_idx = 0;
616 linenr_T found_current_lnum = 0;
617 int found_current_col= 0;
618 lpos_T found_m_endpos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000619 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 /*
622 * Clear any current state that might be hanging around.
623 */
624 invalidate_current_state();
625
626 /*
627 * Start at least "minlines" back. Default starting point for parsing is
628 * there.
629 * Start further back, to avoid that scrolling backwards will result in
630 * resyncing for every line. Now it resyncs only one out of N lines,
631 * where N is minlines * 1.5, or minlines * 2 if minlines is small.
632 * Watch out for overflow when minlines is MAXLNUM.
633 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200634 if (syn_block->b_syn_sync_minlines > start_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 start_lnum = 1;
636 else
637 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200638 if (syn_block->b_syn_sync_minlines == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 lnum = 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 else if (syn_block->b_syn_sync_minlines < 10)
641 lnum = syn_block->b_syn_sync_minlines * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 else
Bram Moolenaar860cae12010-06-05 23:22:07 +0200643 lnum = syn_block->b_syn_sync_minlines * 3 / 2;
644 if (syn_block->b_syn_sync_maxlines != 0
645 && lnum > syn_block->b_syn_sync_maxlines)
646 lnum = syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 if (lnum >= start_lnum)
648 start_lnum = 1;
649 else
650 start_lnum -= lnum;
651 }
652 current_lnum = start_lnum;
653
654 /*
655 * 1. Search backwards for the end of a C-style comment.
656 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200657 if (syn_block->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100659 // Need to make syn_buf the current buffer for a moment, to be able to
660 // use find_start_comment().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 curwin_save = curwin;
662 curwin = wp;
663 curbuf_save = curbuf;
664 curbuf = syn_buf;
665
666 /*
667 * Skip lines that end in a backslash.
668 */
669 for ( ; start_lnum > 1; --start_lnum)
670 {
671 line = ml_get(start_lnum - 1);
672 if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
673 break;
674 }
675 current_lnum = start_lnum;
676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100677 // set cursor to start of search
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 cursor_save = wp->w_cursor;
679 wp->w_cursor.lnum = start_lnum;
680 wp->w_cursor.col = 0;
681
682 /*
683 * If the line is inside a comment, need to find the syntax item that
684 * defines the comment.
685 * Restrict the search for the end of a comment to b_syn_sync_maxlines.
686 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200687 if (find_start_comment((int)syn_block->b_syn_sync_maxlines) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200689 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
690 if (SYN_ITEMS(syn_block)[idx].sp_syn.id
691 == syn_block->b_syn_sync_id
692 && SYN_ITEMS(syn_block)[idx].sp_type == SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {
694 validate_current_state();
695 if (push_current_state(idx) == OK)
696 update_si_attr(current_state.ga_len - 1);
697 break;
698 }
699 }
700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100701 // restore cursor and buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 wp->w_cursor = cursor_save;
703 curwin = curwin_save;
704 curbuf = curbuf_save;
705 }
706
707 /*
708 * 2. Search backwards for given sync patterns.
709 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200710 else if (syn_block->b_syn_sync_flags & SF_MATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200712 if (syn_block->b_syn_sync_maxlines != 0
713 && start_lnum > syn_block->b_syn_sync_maxlines)
714 break_lnum = start_lnum - syn_block->b_syn_sync_maxlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 else
716 break_lnum = 0;
717
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000718 found_m_endpos.lnum = 0;
719 found_m_endpos.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 end_lnum = start_lnum;
721 lnum = start_lnum;
722 while (--lnum > break_lnum)
723 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100724 // This can take a long time: break when CTRL-C pressed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 line_breakcheck();
726 if (got_int)
727 {
728 invalidate_current_state();
729 current_lnum = start_lnum;
730 break;
731 }
732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100733 // Check if we have run into a valid saved state stack now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 if (last_valid != NULL && lnum == last_valid->sst_lnum)
735 {
736 load_current_state(last_valid);
737 break;
738 }
739
740 /*
741 * Check if the previous line has the line-continuation pattern.
742 */
743 if (lnum > 1 && syn_match_linecont(lnum - 1))
744 continue;
745
746 /*
747 * Start with nothing on the state stack
748 */
749 validate_current_state();
750
751 for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
752 {
753 syn_start_line();
754 for (;;)
755 {
756 had_sync_point = syn_finish_line(TRUE);
757 /*
758 * When a sync point has been found, remember where, and
759 * continue to look for another one, further on in the line.
760 */
761 if (had_sync_point && current_state.ga_len)
762 {
763 cur_si = &CUR_STATE(current_state.ga_len - 1);
764 if (cur_si->si_m_endpos.lnum > start_lnum)
765 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100766 // ignore match that goes to after where started
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 current_lnum = end_lnum;
768 break;
769 }
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000770 if (cur_si->si_idx < 0)
771 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100772 // Cannot happen?
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000773 found_flags = 0;
774 found_match_idx = KEYWORD_IDX;
775 }
776 else
777 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200778 spp = &(SYN_ITEMS(syn_block)[cur_si->si_idx]);
Bram Moolenaar3a36cf72007-08-21 15:29:56 +0000779 found_flags = spp->sp_flags;
780 found_match_idx = spp->sp_sync_idx;
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 found_current_lnum = current_lnum;
783 found_current_col = current_col;
784 found_m_endpos = cur_si->si_m_endpos;
785 /*
786 * Continue after the match (be aware of a zero-length
787 * match).
788 */
789 if (found_m_endpos.lnum > current_lnum)
790 {
791 current_lnum = found_m_endpos.lnum;
792 current_col = found_m_endpos.col;
793 if (current_lnum >= end_lnum)
794 break;
795 }
796 else if (found_m_endpos.col > current_col)
797 current_col = found_m_endpos.col;
798 else
799 ++current_col;
800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100801 // syn_current_attr() will have skipped the check for
802 // an item that ends here, need to do that now. Be
803 // careful not to go past the NUL.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000804 prev_current_col = current_col;
805 if (syn_getcurline()[current_col] != NUL)
806 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +0000808 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810 else
811 break;
812 }
813 }
814
815 /*
816 * If a sync point was encountered, break here.
817 */
818 if (found_flags)
819 {
820 /*
821 * Put the item that was specified by the sync point on the
822 * state stack. If there was no item specified, make the
823 * state stack empty.
824 */
825 clear_current_state();
826 if (found_match_idx >= 0
827 && push_current_state(found_match_idx) == OK)
828 update_si_attr(current_state.ga_len - 1);
829
830 /*
831 * When using "grouphere", continue from the sync point
832 * match, until the end of the line. Parsing starts at
833 * the next line.
834 * For "groupthere" the parsing starts at start_lnum.
835 */
836 if (found_flags & HL_SYNC_HERE)
837 {
838 if (current_state.ga_len)
839 {
840 cur_si = &CUR_STATE(current_state.ga_len - 1);
841 cur_si->si_h_startpos.lnum = found_current_lnum;
842 cur_si->si_h_startpos.col = found_current_col;
843 update_si_end(cur_si, (int)current_col, TRUE);
844 check_keepend();
845 }
846 current_col = found_m_endpos.col;
847 current_lnum = found_m_endpos.lnum;
848 (void)syn_finish_line(FALSE);
849 ++current_lnum;
850 }
851 else
852 current_lnum = start_lnum;
853
854 break;
855 }
856
857 end_lnum = lnum;
858 invalidate_current_state();
859 }
860
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100861 // Ran into start of the file or exceeded maximum number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 if (lnum <= break_lnum)
863 {
864 invalidate_current_state();
865 current_lnum = break_lnum + 1;
866 }
867 }
868
869 validate_current_state();
870}
871
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100872 static void
873save_chartab(char_u *chartab)
874{
875 if (syn_block->b_syn_isk != empty_option)
876 {
877 mch_memmove(chartab, syn_buf->b_chartab, (size_t)32);
878 mch_memmove(syn_buf->b_chartab, syn_win->w_s->b_syn_chartab,
879 (size_t)32);
880 }
881}
882
883 static void
884restore_chartab(char_u *chartab)
885{
886 if (syn_win->w_s->b_syn_isk != empty_option)
887 mch_memmove(syn_buf->b_chartab, chartab, (size_t)32);
888}
889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890/*
891 * Return TRUE if the line-continuation pattern matches in line "lnum".
892 */
893 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100894syn_match_linecont(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 regmmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100897 int r;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100898 char_u buf_chartab[32]; // chartab array for syn iskyeyword
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar860cae12010-06-05 23:22:07 +0200900 if (syn_block->b_syn_linecont_prog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100902 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100903 save_chartab(buf_chartab);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200904 regmatch.rmm_ic = syn_block->b_syn_linecont_ic;
905 regmatch.regprog = syn_block->b_syn_linecont_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100906 r = syn_regexec(&regmatch, lnum, (colnr_T)0,
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200907 IF_SYN_TIME(&syn_block->b_syn_linecont_time));
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100908 syn_block->b_syn_linecont_prog = regmatch.regprog;
Bram Moolenaarb8060fe2016-01-19 22:29:28 +0100909 restore_chartab(buf_chartab);
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100910 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912 return FALSE;
913}
914
915/*
916 * Prepare the current state for the start of a line.
917 */
918 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100919syn_start_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
921 current_finished = FALSE;
922 current_col = 0;
923
924 /*
925 * Need to update the end of a start/skip/end that continues from the
926 * previous line and regions that have "keepend".
927 */
928 if (current_state.ga_len > 0)
Bram Moolenaar6fa46362011-05-25 17:56:27 +0200929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 syn_update_ends(TRUE);
Bram Moolenaar6fa46362011-05-25 17:56:27 +0200931 check_state_ends();
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
934 next_match_idx = -1;
935 ++current_line_id;
Bram Moolenaarea20de82017-06-24 22:52:24 +0200936#ifdef FEAT_CONCEAL
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200937 next_seqnr = 1;
Bram Moolenaarea20de82017-06-24 22:52:24 +0200938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939}
940
941/*
942 * Check for items in the stack that need their end updated.
943 * When "startofline" is TRUE the last item is always updated.
944 * When "startofline" is FALSE the item with "keepend" is forcefully updated.
945 */
946 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100947syn_update_ends(int startofline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949 stateitem_T *cur_si;
950 int i;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000951 int seen_keepend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
953 if (startofline)
954 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100955 // Check for a match carried over from a previous line with a
956 // contained region. The match ends as soon as the region ends.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 for (i = 0; i < current_state.ga_len; ++i)
958 {
959 cur_si = &CUR_STATE(i);
960 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +0200961 && (SYN_ITEMS(syn_block)[cur_si->si_idx]).sp_type
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 == SPTYPE_MATCH
963 && cur_si->si_m_endpos.lnum < current_lnum)
964 {
965 cur_si->si_flags |= HL_MATCHCONT;
966 cur_si->si_m_endpos.lnum = 0;
967 cur_si->si_m_endpos.col = 0;
968 cur_si->si_h_endpos = cur_si->si_m_endpos;
969 cur_si->si_ends = TRUE;
970 }
971 }
972 }
973
974 /*
975 * Need to update the end of a start/skip/end that continues from the
976 * previous line. And regions that have "keepend", because they may
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000977 * influence contained items. If we've just removed "extend"
978 * (startofline == 0) then we should update ends of normal regions
979 * contained inside "keepend" because "extend" could have extended
980 * these "keepend" regions as well as contained normal regions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 * Then check for items ending in column 0.
982 */
983 i = current_state.ga_len - 1;
984 if (keepend_level >= 0)
985 for ( ; i > keepend_level; --i)
986 if (CUR_STATE(i).si_flags & HL_EXTEND)
987 break;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000988
989 seen_keepend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 for ( ; i < current_state.ga_len; ++i)
991 {
992 cur_si = &CUR_STATE(i);
993 if ((cur_si->si_flags & HL_KEEPEND)
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +0000994 || (seen_keepend && !startofline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 || (i == current_state.ga_len - 1 && startofline))
996 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100997 cur_si->si_h_startpos.col = 0; // start highl. in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 cur_si->si_h_startpos.lnum = current_lnum;
999
1000 if (!(cur_si->si_flags & HL_MATCHCONT))
1001 update_si_end(cur_si, (int)current_col, !startofline);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00001002
1003 if (!startofline && (cur_si->si_flags & HL_KEEPEND))
1004 seen_keepend = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 }
1007 check_keepend();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
1009
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001010/////////////////////////////////////////
1011// Handling of the state stack cache.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013/*
1014 * EXPLANATION OF THE SYNTAX STATE STACK CACHE
1015 *
1016 * To speed up syntax highlighting, the state stack for the start of some
1017 * lines is cached. These entries can be used to start parsing at that point.
1018 *
1019 * The stack is kept in b_sst_array[] for each buffer. There is a list of
1020 * valid entries. b_sst_first points to the first one, then follow sst_next.
1021 * The entries are sorted on line number. The first entry is often for line 2
1022 * (line 1 always starts with an empty stack).
1023 * There is also a list for free entries. This construction is used to avoid
1024 * having to allocate and free memory blocks too often.
1025 *
1026 * When making changes to the buffer, this is logged in b_mod_*. When calling
1027 * update_screen() to update the display, it will call
1028 * syn_stack_apply_changes() for each displayed buffer to adjust the cached
1029 * entries. The entries which are inside the changed area are removed,
1030 * because they must be recomputed. Entries below the changed have their line
1031 * number adjusted for deleted/inserted lines, and have their sst_change_lnum
1032 * set to indicate that a check must be made if the changed lines would change
1033 * the cached entry.
1034 *
1035 * When later displaying lines, an entry is stored for each line. Displayed
1036 * lines are likely to be displayed again, in which case the state at the
1037 * start of the line is needed.
1038 * For not displayed lines, an entry is stored for every so many lines. These
1039 * entries will be used e.g., when scrolling backwards. The distance between
1040 * entries depends on the number of lines in the buffer. For small buffers
1041 * the distance is fixed at SST_DIST, for large buffers there is a fixed
1042 * number of entries SST_MAX_ENTRIES, and the distance is computed.
1043 */
1044
Bram Moolenaar860cae12010-06-05 23:22:07 +02001045 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001046syn_stack_free_block(synblock_T *block)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001047{
1048 synstate_T *p;
1049
1050 if (block->b_sst_array != NULL)
1051 {
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001052 FOR_ALL_SYNSTATES(block, p)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001053 clear_syn_state(p);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001054 VIM_CLEAR(block->b_sst_array);
Bram Moolenaar95892c22018-09-28 22:26:54 +02001055 block->b_sst_first = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001056 block->b_sst_len = 0;
1057 }
1058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059/*
1060 * Free b_sst_array[] for buffer "buf".
1061 * Used when syntax items changed to force resyncing everywhere.
1062 */
1063 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001064syn_stack_free_all(synblock_T *block)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaara6c07602017-03-05 21:18:27 +01001066#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 win_T *wp;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Bram Moolenaar860cae12010-06-05 23:22:07 +02001070 syn_stack_free_block(block);
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_FOLDING
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001073 // When using "syntax" fold method, must update all folds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 FOR_ALL_WINDOWS(wp)
1075 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001076 if (wp->w_s == block && foldmethodIsSyntax(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 foldUpdateAll(wp);
1078 }
1079#endif
1080}
1081
1082/*
1083 * Allocate the syntax state stack for syn_buf when needed.
1084 * If the number of entries in b_sst_array[] is much too big or a bit too
1085 * small, reallocate it.
1086 * Also used to allocate b_sst_array[] for the first time.
1087 */
1088 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001089syn_stack_alloc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 long len;
1092 synstate_T *to, *from;
1093 synstate_T *sstp;
1094
1095 len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
1096 if (len < SST_MIN_ENTRIES)
1097 len = SST_MIN_ENTRIES;
1098 else if (len > SST_MAX_ENTRIES)
1099 len = SST_MAX_ENTRIES;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001100 if (syn_block->b_sst_len > len * 2 || syn_block->b_sst_len < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001102 // Allocate 50% too much, to avoid reallocating too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 len = syn_buf->b_ml.ml_line_count;
1104 len = (len + len / 2) / SST_DIST + Rows * 2;
1105 if (len < SST_MIN_ENTRIES)
1106 len = SST_MIN_ENTRIES;
1107 else if (len > SST_MAX_ENTRIES)
1108 len = SST_MAX_ENTRIES;
1109
Bram Moolenaar860cae12010-06-05 23:22:07 +02001110 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001112 // When shrinking the array, cleanup the existing stack.
1113 // Make sure that all valid entries fit in the new array.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001114 while (syn_block->b_sst_len - syn_block->b_sst_freecount + 2 > len
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 && syn_stack_cleanup())
1116 ;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001117 if (len < syn_block->b_sst_len - syn_block->b_sst_freecount + 2)
1118 len = syn_block->b_sst_len - syn_block->b_sst_freecount + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001121 sstp = ALLOC_CLEAR_MULT(synstate_T, len);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001122 if (sstp == NULL) // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 return;
1124
1125 to = sstp - 1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001126 if (syn_block->b_sst_array != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001128 // Move the states from the old array to the new one.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001129 for (from = syn_block->b_sst_first; from != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 from = from->sst_next)
1131 {
1132 ++to;
1133 *to = *from;
1134 to->sst_next = to + 1;
1135 }
1136 }
1137 if (to != sstp - 1)
1138 {
1139 to->sst_next = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001140 syn_block->b_sst_first = sstp;
1141 syn_block->b_sst_freecount = len - (int)(to - sstp) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 else
1144 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001145 syn_block->b_sst_first = NULL;
1146 syn_block->b_sst_freecount = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
1148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001149 // Create the list of free entries.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001150 syn_block->b_sst_firstfree = to + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 while (++to < sstp + len)
1152 to->sst_next = to + 1;
1153 (sstp + len - 1)->sst_next = NULL;
1154
Bram Moolenaar860cae12010-06-05 23:22:07 +02001155 vim_free(syn_block->b_sst_array);
1156 syn_block->b_sst_array = sstp;
1157 syn_block->b_sst_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159}
1160
1161/*
1162 * Check for changes in a buffer to affect stored syntax states. Uses the
1163 * b_mod_* fields.
1164 * Called from update_screen(), before screen is being updated, once for each
1165 * displayed buffer.
1166 */
1167 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001168syn_stack_apply_changes(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001170 win_T *wp;
1171
1172 syn_stack_apply_changes_block(&buf->b_s, buf);
1173
1174 FOR_ALL_WINDOWS(wp)
1175 {
1176 if ((wp->w_buffer == buf) && (wp->w_s != &buf->b_s))
1177 syn_stack_apply_changes_block(wp->w_s, buf);
1178 }
1179}
1180
1181 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001182syn_stack_apply_changes_block(synblock_T *block, buf_T *buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001183{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 synstate_T *p, *prev, *np;
1185 linenr_T n;
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001188 for (p = block->b_sst_first; p != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001190 if (p->sst_lnum + block->b_syn_sync_linebreaks > buf->b_mod_top)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
1192 n = p->sst_lnum + buf->b_mod_xlines;
1193 if (n <= buf->b_mod_bot)
1194 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001195 // this state is inside the changed area, remove it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 np = p->sst_next;
1197 if (prev == NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02001198 block->b_sst_first = np;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else
1200 prev->sst_next = np;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001201 syn_stack_free_entry(block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 p = np;
1203 continue;
1204 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001205 // This state is below the changed area. Remember the line
1206 // that needs to be parsed before this entry can be made valid
1207 // again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
1209 {
1210 if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
1211 p->sst_change_lnum += buf->b_mod_xlines;
1212 else
1213 p->sst_change_lnum = buf->b_mod_top;
1214 }
1215 if (p->sst_change_lnum == 0
1216 || p->sst_change_lnum < buf->b_mod_bot)
1217 p->sst_change_lnum = buf->b_mod_bot;
1218
1219 p->sst_lnum = n;
1220 }
1221 prev = p;
1222 p = p->sst_next;
1223 }
1224}
1225
1226/*
1227 * Reduce the number of entries in the state stack for syn_buf.
1228 * Returns TRUE if at least one entry was freed.
1229 */
1230 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001231syn_stack_cleanup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232{
1233 synstate_T *p, *prev;
1234 disptick_T tick;
1235 int above;
1236 int dist;
1237 int retval = FALSE;
1238
Bram Moolenaar95892c22018-09-28 22:26:54 +02001239 if (syn_block->b_sst_first == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 return retval;
1241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001242 // Compute normal distance between non-displayed entries.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001243 if (syn_block->b_sst_len <= Rows)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001244 dist = 999999;
1245 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02001246 dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 /*
Bram Moolenaarf5b63862009-12-16 17:13:44 +00001249 * Go through the list to find the "tick" for the oldest entry that can
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 * be removed. Set "above" when the "tick" for the oldest entry is above
1251 * "b_sst_lasttick" (the display tick wraps around).
1252 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 tick = syn_block->b_sst_lasttick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 above = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1257 {
1258 if (prev->sst_lnum + dist > p->sst_lnum)
1259 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001260 if (p->sst_tick > syn_block->b_sst_lasttick)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 if (!above || p->sst_tick < tick)
1263 tick = p->sst_tick;
1264 above = TRUE;
1265 }
1266 else if (!above && p->sst_tick < tick)
1267 tick = p->sst_tick;
1268 }
1269 }
1270
1271 /*
1272 * Go through the list to make the entries for the oldest tick at an
1273 * interval of several lines.
1274 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001275 prev = syn_block->b_sst_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
1277 {
1278 if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
1279 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001280 // Move this entry from used list to free list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 prev->sst_next = p->sst_next;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001282 syn_stack_free_entry(syn_block, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 p = prev;
1284 retval = TRUE;
1285 }
1286 }
1287 return retval;
1288}
1289
1290/*
1291 * Free the allocated memory for a syn_state item.
1292 * Move the entry into the free list.
1293 */
1294 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001295syn_stack_free_entry(synblock_T *block, synstate_T *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 clear_syn_state(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001298 p->sst_next = block->b_sst_firstfree;
1299 block->b_sst_firstfree = p;
1300 ++block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
1303/*
1304 * Find an entry in the list of state stacks at or before "lnum".
1305 * Returns NULL when there is no entry or the first entry is after "lnum".
1306 */
1307 static synstate_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001308syn_stack_find_entry(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 synstate_T *p, *prev;
1311
1312 prev = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001313 for (p = syn_block->b_sst_first; p != NULL; prev = p, p = p->sst_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
1315 if (p->sst_lnum == lnum)
1316 return p;
1317 if (p->sst_lnum > lnum)
1318 break;
1319 }
1320 return prev;
1321}
1322
1323/*
1324 * Try saving the current state in b_sst_array[].
1325 * The current state must be valid for the start of the current_lnum line!
1326 */
1327 static synstate_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001328store_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 int i;
1331 synstate_T *p;
1332 bufstate_T *bp;
1333 stateitem_T *cur_si;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001334 synstate_T *sp = syn_stack_find_entry(current_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 /*
1337 * If the current state contains a start or end pattern that continues
1338 * from the previous line, we can't use it. Don't store it then.
1339 */
1340 for (i = current_state.ga_len - 1; i >= 0; --i)
1341 {
1342 cur_si = &CUR_STATE(i);
1343 if (cur_si->si_h_startpos.lnum >= current_lnum
1344 || cur_si->si_m_endpos.lnum >= current_lnum
1345 || cur_si->si_h_endpos.lnum >= current_lnum
1346 || (cur_si->si_end_idx
1347 && cur_si->si_eoe_pos.lnum >= current_lnum))
1348 break;
1349 }
1350 if (i >= 0)
1351 {
1352 if (sp != NULL)
1353 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001354 // find "sp" in the list and remove it
Bram Moolenaar860cae12010-06-05 23:22:07 +02001355 if (syn_block->b_sst_first == sp)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001356 // it's the first entry
Bram Moolenaar860cae12010-06-05 23:22:07 +02001357 syn_block->b_sst_first = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 else
1359 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001360 // find the entry just before this one to adjust sst_next
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001361 FOR_ALL_SYNSTATES(syn_block, p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (p->sst_next == sp)
1363 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001364 if (p != NULL) // just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001365 p->sst_next = sp->sst_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001367 syn_stack_free_entry(syn_block, sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 sp = NULL;
1369 }
1370 }
1371 else if (sp == NULL || sp->sst_lnum != current_lnum)
1372 {
1373 /*
1374 * Add a new entry
1375 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001376 // If no free items, cleanup the array first.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001377 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 {
1379 (void)syn_stack_cleanup();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001380 // "sp" may have been moved to the freelist now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 sp = syn_stack_find_entry(current_lnum);
1382 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001383 // Still no free items? Must be a strange problem...
Bram Moolenaar860cae12010-06-05 23:22:07 +02001384 if (syn_block->b_sst_freecount == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 sp = NULL;
1386 else
1387 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001388 // Take the first item from the free list and put it in the used
1389 // list, after *sp
Bram Moolenaar860cae12010-06-05 23:22:07 +02001390 p = syn_block->b_sst_firstfree;
1391 syn_block->b_sst_firstfree = p->sst_next;
1392 --syn_block->b_sst_freecount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (sp == NULL)
1394 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001395 // Insert in front of the list
Bram Moolenaar860cae12010-06-05 23:22:07 +02001396 p->sst_next = syn_block->b_sst_first;
1397 syn_block->b_sst_first = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 else
1400 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001401 // insert in list after *sp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 p->sst_next = sp->sst_next;
1403 sp->sst_next = p;
1404 }
1405 sp = p;
1406 sp->sst_stacksize = 0;
1407 sp->sst_lnum = current_lnum;
1408 }
1409 }
1410 if (sp != NULL)
1411 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001412 // When overwriting an existing state stack, clear it first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 clear_syn_state(sp);
1414 sp->sst_stacksize = current_state.ga_len;
1415 if (current_state.ga_len > SST_FIX_STATES)
1416 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001417 // Need to clear it, might be something remaining from when the
1418 // length was less than SST_FIX_STATES.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 ga_init2(&sp->sst_union.sst_ga, (int)sizeof(bufstate_T), 1);
1420 if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
1421 sp->sst_stacksize = 0;
1422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 sp->sst_union.sst_ga.ga_len = current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1425 }
1426 else
1427 bp = sp->sst_union.sst_stack;
1428 for (i = 0; i < sp->sst_stacksize; ++i)
1429 {
1430 bp[i].bs_idx = CUR_STATE(i).si_idx;
1431 bp[i].bs_flags = CUR_STATE(i).si_flags;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001432#ifdef FEAT_CONCEAL
1433 bp[i].bs_seqnr = CUR_STATE(i).si_seqnr;
1434 bp[i].bs_cchar = CUR_STATE(i).si_cchar;
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
1437 }
1438 sp->sst_next_flags = current_next_flags;
1439 sp->sst_next_list = current_next_list;
1440 sp->sst_tick = display_tick;
1441 sp->sst_change_lnum = 0;
1442 }
1443 current_state_stored = TRUE;
1444 return sp;
1445}
1446
1447/*
1448 * Copy a state stack from "from" in b_sst_array[] to current_state;
1449 */
1450 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001451load_current_state(synstate_T *from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 int i;
1454 bufstate_T *bp;
1455
1456 clear_current_state();
1457 validate_current_state();
1458 keepend_level = -1;
1459 if (from->sst_stacksize
1460 && ga_grow(&current_state, from->sst_stacksize) != FAIL)
1461 {
1462 if (from->sst_stacksize > SST_FIX_STATES)
1463 bp = SYN_STATE_P(&(from->sst_union.sst_ga));
1464 else
1465 bp = from->sst_union.sst_stack;
1466 for (i = 0; i < from->sst_stacksize; ++i)
1467 {
1468 CUR_STATE(i).si_idx = bp[i].bs_idx;
1469 CUR_STATE(i).si_flags = bp[i].bs_flags;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001470#ifdef FEAT_CONCEAL
1471 CUR_STATE(i).si_seqnr = bp[i].bs_seqnr;
1472 CUR_STATE(i).si_cchar = bp[i].bs_cchar;
1473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
1475 if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
1476 keepend_level = i;
1477 CUR_STATE(i).si_ends = FALSE;
1478 CUR_STATE(i).si_m_lnum = 0;
1479 if (CUR_STATE(i).si_idx >= 0)
1480 CUR_STATE(i).si_next_list =
Bram Moolenaar860cae12010-06-05 23:22:07 +02001481 (SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 else
1483 CUR_STATE(i).si_next_list = NULL;
1484 update_si_attr(i);
1485 }
1486 current_state.ga_len = from->sst_stacksize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488 current_next_list = from->sst_next_list;
1489 current_next_flags = from->sst_next_flags;
1490 current_lnum = from->sst_lnum;
1491}
1492
1493/*
1494 * Compare saved state stack "*sp" with the current state.
1495 * Return TRUE when they are equal.
1496 */
1497 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001498syn_stack_equal(synstate_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 int i, j;
1501 bufstate_T *bp;
1502 reg_extmatch_T *six, *bsx;
1503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001504 // First a quick check if the stacks have the same size end nextlist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (sp->sst_stacksize == current_state.ga_len
1506 && sp->sst_next_list == current_next_list)
1507 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001508 // Need to compare all states on both stacks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (sp->sst_stacksize > SST_FIX_STATES)
1510 bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
1511 else
1512 bp = sp->sst_union.sst_stack;
1513
1514 for (i = current_state.ga_len; --i >= 0; )
1515 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001516 // If the item has another index the state is different.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 if (bp[i].bs_idx != CUR_STATE(i).si_idx)
1518 break;
1519 if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
1520 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001521 // When the extmatch pointers are different, the strings in
1522 // them can still be the same. Check if the extmatch
1523 // references are equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 bsx = bp[i].bs_extmatch;
1525 six = CUR_STATE(i).si_extmatch;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001526 // If one of the extmatch pointers is NULL the states are
1527 // different.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (bsx == NULL || six == NULL)
1529 break;
1530 for (j = 0; j < NSUBEXP; ++j)
1531 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001532 // Check each referenced match string. They must all be
1533 // equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (bsx->matches[j] != six->matches[j])
1535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001536 // If the pointer is different it can still be the
1537 // same text. Compare the strings, ignore case when
1538 // the start item has the sp_ic flag set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (bsx->matches[j] == NULL
1540 || six->matches[j] == NULL)
1541 break;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001542 if ((SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 ? MB_STRICMP(bsx->matches[j],
1544 six->matches[j]) != 0
1545 : STRCMP(bsx->matches[j], six->matches[j]) != 0)
1546 break;
1547 }
1548 }
1549 if (j != NSUBEXP)
1550 break;
1551 }
1552 }
1553 if (i < 0)
1554 return TRUE;
1555 }
1556 return FALSE;
1557}
1558
1559/*
1560 * We stop parsing syntax above line "lnum". If the stored state at or below
1561 * this line depended on a change before it, it now depends on the line below
1562 * the last parsed line.
1563 * The window looks like this:
1564 * line which changed
1565 * displayed line
1566 * displayed line
1567 * lnum -> line below window
1568 */
1569 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001570syntax_end_parsing(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 synstate_T *sp;
1573
1574 sp = syn_stack_find_entry(lnum);
1575 if (sp != NULL && sp->sst_lnum < lnum)
1576 sp = sp->sst_next;
1577
1578 if (sp != NULL && sp->sst_change_lnum != 0)
1579 sp->sst_change_lnum = lnum;
1580}
1581
1582/*
1583 * End of handling of the state stack.
1584 ****************************************/
1585
1586 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001587invalidate_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
1589 clear_current_state();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001590 current_state.ga_itemsize = 0; // mark current_state invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 current_next_list = NULL;
1592 keepend_level = -1;
1593}
1594
1595 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001596validate_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 current_state.ga_itemsize = sizeof(stateitem_T);
1599 current_state.ga_growsize = 3;
1600}
1601
1602/*
1603 * Return TRUE if the syntax at start of lnum changed since last time.
1604 * This will only be called just after get_syntax_attr() for the previous
1605 * line, to check if the next line needs to be redrawn too.
1606 */
1607 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001608syntax_check_changed(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
1610 int retval = TRUE;
1611 synstate_T *sp;
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /*
1614 * Check the state stack when:
1615 * - lnum is just below the previously syntaxed line.
1616 * - lnum is not before the lines with saved states.
1617 * - lnum is not past the lines with saved states.
1618 * - lnum is at or before the last changed line.
1619 */
1620 if (VALID_STATE(&current_state) && lnum == current_lnum + 1)
1621 {
1622 sp = syn_stack_find_entry(lnum);
1623 if (sp != NULL && sp->sst_lnum == lnum)
1624 {
1625 /*
1626 * finish the previous line (needed when not all of the line was
1627 * drawn)
1628 */
1629 (void)syn_finish_line(FALSE);
1630
1631 /*
1632 * Compare the current state with the previously saved state of
1633 * the line.
1634 */
1635 if (syn_stack_equal(sp))
1636 retval = FALSE;
1637
1638 /*
1639 * Store the current state in b_sst_array[] for later use.
1640 */
1641 ++current_lnum;
Bram Moolenaardbe31752008-01-13 16:40:19 +00001642 (void)store_current_state();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 }
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 return retval;
1647}
1648
1649/*
1650 * Finish the current line.
1651 * This doesn't return any attributes, it only gets the state at the end of
1652 * the line. It can start anywhere in the line, as long as the current state
1653 * is valid.
1654 */
1655 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001656syn_finish_line(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001657 int syncing) // called for syncing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 stateitem_T *cur_si;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001660 colnr_T prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001662 while (!current_finished)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001664 (void)syn_current_attr(syncing, FALSE, NULL, FALSE);
1665 /*
1666 * When syncing, and found some item, need to check the item.
1667 */
1668 if (syncing && current_state.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 /*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001671 * Check for match with sync item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 */
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001673 cur_si = &CUR_STATE(current_state.ga_len - 1);
1674 if (cur_si->si_idx >= 0
1675 && (SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags
1676 & (HL_SYNC_HERE|HL_SYNC_THERE)))
1677 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001679 // syn_current_attr() will have skipped the check for an item
1680 // that ends here, need to do that now. Be careful not to go
1681 // past the NUL.
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001682 prev_current_col = current_col;
1683 if (syn_getcurline()[current_col] != NUL)
1684 ++current_col;
1685 check_state_ends();
1686 current_col = prev_current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaaraab93b12017-03-18 21:37:28 +01001688 ++current_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 return FALSE;
1691}
1692
1693/*
1694 * Return highlight attributes for next character.
1695 * Must first call syntax_start() once for the line.
1696 * "col" is normally 0 for the first use in a line, and increments by one each
1697 * time. It's allowed to skip characters and to stop before the end of the
1698 * line. But only a "col" after a previously used column is allowed.
Bram Moolenaar217ad922005-03-20 22:37:15 +00001699 * When "can_spell" is not NULL set it to TRUE when spell-checking should be
1700 * done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 */
1702 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001703get_syntax_attr(
1704 colnr_T col,
1705 int *can_spell,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001706 int keep_state) // keep state of char at "col"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 int attr = 0;
1709
Bram Moolenaar349955a2007-08-14 21:07:36 +00001710 if (can_spell != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001711 // Default: Only do spelling when there is no @Spell cluster or when
1712 // ":syn spell toplevel" was used.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001713 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
1714 ? (syn_block->b_spell_cluster_id == 0)
1715 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar349955a2007-08-14 21:07:36 +00001716
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001717 // check for out of memory situation
Bram Moolenaar860cae12010-06-05 23:22:07 +02001718 if (syn_block->b_sst_array == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return 0;
1720
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001721 // After 'synmaxcol' the attribute is always zero.
Bram Moolenaar84fb85a2005-07-20 22:02:14 +00001722 if (syn_buf->b_p_smc > 0 && col >= (colnr_T)syn_buf->b_p_smc)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001723 {
1724 clear_current_state();
1725#ifdef FEAT_EVAL
1726 current_id = 0;
1727 current_trans_id = 0;
1728#endif
Bram Moolenaar7510fe72010-07-25 12:46:44 +02001729#ifdef FEAT_CONCEAL
1730 current_flags = 0;
Bram Moolenaarcc0750d2017-06-24 22:29:24 +02001731 current_seqnr = 0;
Bram Moolenaar7510fe72010-07-25 12:46:44 +02001732#endif
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001733 return 0;
1734 }
1735
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001736 // Make sure current_state is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (INVALID_STATE(&current_state))
1738 validate_current_state();
1739
1740 /*
1741 * Skip from the current column to "col", get the attributes for "col".
1742 */
1743 while (current_col <= col)
1744 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001745 attr = syn_current_attr(FALSE, TRUE, can_spell,
1746 current_col == col ? keep_state : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 ++current_col;
1748 }
1749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 return attr;
1751}
1752
1753/*
1754 * Get syntax attributes for current_lnum, current_col.
1755 */
1756 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001757syn_current_attr(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001758 int syncing, // When 1: called for syncing
1759 int displaying, // result will be displayed
1760 int *can_spell, // return: do spell checking
1761 int keep_state) // keep syntax stack afterwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 int syn_id;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001764 lpos_T endpos; // was: char_u *endp;
1765 lpos_T hl_startpos; // was: int hl_startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 lpos_T hl_endpos;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001767 lpos_T eos_pos; // end-of-start match (start region)
1768 lpos_T eoe_pos; // end-of-end pattern
1769 int end_idx; // group ID for end pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 int idx;
1771 synpat_T *spp;
Bram Moolenaar217ad922005-03-20 22:37:15 +00001772 stateitem_T *cur_si, *sip = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 int startcol;
1774 int endcol;
1775 long flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001776 int cchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 short *next_list;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001778 int found_match; // found usable match
1779 static int try_next_column = FALSE; // must try in next col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 int do_keywords;
1781 regmmatch_T regmatch;
1782 lpos_T pos;
1783 int lc_col;
1784 reg_extmatch_T *cur_extmatch = NULL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001785 char_u buf_chartab[32]; // chartab array for syn iskyeyword
1786 char_u *line; // current line. NOTE: becomes invalid after
1787 // looking for a pattern match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001789 // variables for zero-width matches that have a "nextgroup" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 int keep_next_list;
1791 int zero_width_next_list = FALSE;
1792 garray_T zero_width_next_ga;
1793
1794 /*
1795 * No character, no attributes! Past end of line?
1796 * Do try matching with an empty line (could be the start of a region).
1797 */
1798 line = syn_getcurline();
1799 if (line[current_col] == NUL && current_col != 0)
1800 {
1801 /*
1802 * If we found a match after the last column, use it.
1803 */
1804 if (next_match_idx >= 0 && next_match_col >= (int)current_col
1805 && next_match_col != MAXCOL)
1806 (void)push_next_match(NULL);
1807
1808 current_finished = TRUE;
1809 current_state_stored = FALSE;
1810 return 0;
1811 }
1812
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001813 // if the current or next character is NUL, we will finish the line now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (line[current_col] == NUL || line[current_col + 1] == NUL)
1815 {
1816 current_finished = TRUE;
1817 current_state_stored = FALSE;
1818 }
1819
1820 /*
1821 * When in the previous column there was a match but it could not be used
1822 * (empty match or already matched in this column) need to try again in
1823 * the next column.
1824 */
1825 if (try_next_column)
1826 {
1827 next_match_idx = -1;
1828 try_next_column = FALSE;
1829 }
1830
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001831 // Only check for keywords when not syncing and there are some.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 do_keywords = !syncing
Bram Moolenaar860cae12010-06-05 23:22:07 +02001833 && (syn_block->b_keywtab.ht_used > 0
1834 || syn_block->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001836 // Init the list of zero-width matches with a nextlist. This is used to
1837 // avoid matching the same item in the same position twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 ga_init2(&zero_width_next_ga, (int)sizeof(int), 10);
1839
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001840 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001841 save_chartab(buf_chartab);
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 /*
1844 * Repeat matching keywords and patterns, to find contained items at the
1845 * same column. This stops when there are no extra matches at the current
1846 * column.
1847 */
1848 do
1849 {
1850 found_match = FALSE;
1851 keep_next_list = FALSE;
1852 syn_id = 0;
1853
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 /*
1856 * 1. Check for a current state.
1857 * Only when there is no current state, or if the current state may
1858 * contain other things, we need to check for keywords and patterns.
1859 * Always need to check for contained items if some item has the
1860 * "containedin" argument (takes extra time!).
1861 */
1862 if (current_state.ga_len)
1863 cur_si = &CUR_STATE(current_state.ga_len - 1);
1864 else
1865 cur_si = NULL;
1866
Bram Moolenaar860cae12010-06-05 23:22:07 +02001867 if (syn_block->b_syn_containedin || cur_si == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 || cur_si->si_cont_list != NULL)
1869 {
1870 /*
1871 * 2. Check for keywords, if on a keyword char after a non-keyword
1872 * char. Don't do this when syncing.
1873 */
1874 if (do_keywords)
1875 {
1876 line = syn_getcurline();
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01001877 if (vim_iswordp_buf(line + current_col, syn_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 && (current_col == 0
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01001879 || !vim_iswordp_buf(line + current_col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 - (has_mbyte
1881 ? (*mb_head_off)(line, line + current_col - 1)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001882 : 0) , syn_buf)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 syn_id = check_keyword_id(line, (int)current_col,
Bram Moolenaar860cae12010-06-05 23:22:07 +02001885 &endcol, &flags, &next_list, cur_si,
1886 &cchar);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001887 if (syn_id != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 if (push_current_state(KEYWORD_IDX) == OK)
1890 {
1891 cur_si = &CUR_STATE(current_state.ga_len - 1);
1892 cur_si->si_m_startcol = current_col;
1893 cur_si->si_h_startpos.lnum = current_lnum;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001894 cur_si->si_h_startpos.col = 0; // starts right away
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 cur_si->si_m_endpos.lnum = current_lnum;
1896 cur_si->si_m_endpos.col = endcol;
1897 cur_si->si_h_endpos.lnum = current_lnum;
1898 cur_si->si_h_endpos.col = endcol;
1899 cur_si->si_ends = TRUE;
1900 cur_si->si_end_idx = 0;
1901 cur_si->si_flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001902#ifdef FEAT_CONCEAL
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02001903 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02001904 cur_si->si_cchar = cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001905 if (current_state.ga_len > 1)
1906 cur_si->si_flags |=
1907 CUR_STATE(current_state.ga_len - 2).si_flags
1908 & HL_CONCEAL;
1909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 cur_si->si_id = syn_id;
1911 cur_si->si_trans_id = syn_id;
1912 if (flags & HL_TRANSP)
1913 {
1914 if (current_state.ga_len < 2)
1915 {
1916 cur_si->si_attr = 0;
1917 cur_si->si_trans_id = 0;
1918 }
1919 else
1920 {
1921 cur_si->si_attr = CUR_STATE(
1922 current_state.ga_len - 2).si_attr;
1923 cur_si->si_trans_id = CUR_STATE(
1924 current_state.ga_len - 2).si_trans_id;
1925 }
1926 }
1927 else
1928 cur_si->si_attr = syn_id2attr(syn_id);
1929 cur_si->si_cont_list = NULL;
1930 cur_si->si_next_list = next_list;
1931 check_keepend();
1932 }
1933 else
1934 vim_free(next_list);
1935 }
1936 }
1937 }
1938
1939 /*
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001940 * 3. Check for patterns (only if no keyword found).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001942 if (syn_id == 0 && syn_block->b_syn_patterns.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
1944 /*
1945 * If we didn't check for a match yet, or we are past it, check
1946 * for any match with a pattern.
1947 */
1948 if (next_match_idx < 0 || next_match_col < (int)current_col)
1949 {
1950 /*
1951 * Check all relevant patterns for a match at this
1952 * position. This is complicated, because matching with a
1953 * pattern takes quite a bit of time, thus we want to
1954 * avoid doing it when it's not needed.
1955 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001956 next_match_idx = 0; // no match in this line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 next_match_col = MAXCOL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001958 for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001960 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if ( spp->sp_syncing == syncing
1962 && (displaying || !(spp->sp_flags & HL_DISPLAY))
1963 && (spp->sp_type == SPTYPE_MATCH
1964 || spp->sp_type == SPTYPE_START)
1965 && (current_next_list != NULL
1966 ? in_id_list(NULL, current_next_list,
1967 &spp->sp_syn, 0)
1968 : (cur_si == NULL
1969 ? !(spp->sp_flags & HL_CONTAINED)
1970 : in_id_list(cur_si,
1971 cur_si->si_cont_list, &spp->sp_syn,
1972 spp->sp_flags & HL_CONTAINED))))
1973 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001974 int r;
1975
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001976 // If we already tried matching in this line, and
1977 // there isn't a match before next_match_col, skip
1978 // this item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (spp->sp_line_id == current_line_id
1980 && spp->sp_startcol >= next_match_col)
1981 continue;
1982 spp->sp_line_id = current_line_id;
1983
1984 lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
1985 if (lc_col < 0)
1986 lc_col = 0;
1987
1988 regmatch.rmm_ic = spp->sp_ic;
1989 regmatch.regprog = spp->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001990 r = syn_regexec(&regmatch,
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02001991 current_lnum,
1992 (colnr_T)lc_col,
Bram Moolenaard23a8232018-02-10 18:45:26 +01001993 IF_SYN_TIME(&spp->sp_time));
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001994 spp->sp_prog = regmatch.regprog;
1995 if (!r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001997 // no match in this line, try another one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 spp->sp_startcol = MAXCOL;
1999 continue;
2000 }
2001
2002 /*
2003 * Compute the first column of the match.
2004 */
2005 syn_add_start_off(&pos, &regmatch,
2006 spp, SPO_MS_OFF, -1);
2007 if (pos.lnum > current_lnum)
2008 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002009 // must have used end of match in a next line,
2010 // we can't handle that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 spp->sp_startcol = MAXCOL;
2012 continue;
2013 }
2014 startcol = pos.col;
2015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002016 // remember the next column where this pattern
2017 // matches in the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 spp->sp_startcol = startcol;
2019
2020 /*
2021 * If a previously found match starts at a lower
2022 * column number, don't use this one.
2023 */
2024 if (startcol >= next_match_col)
2025 continue;
2026
2027 /*
2028 * If we matched this pattern at this position
2029 * before, skip it. Must retry in the next
2030 * column, because it may match from there.
2031 */
2032 if (did_match_already(idx, &zero_width_next_ga))
2033 {
2034 try_next_column = TRUE;
2035 continue;
2036 }
2037
2038 endpos.lnum = regmatch.endpos[0].lnum;
2039 endpos.col = regmatch.endpos[0].col;
2040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002041 // Compute the highlight start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 syn_add_start_off(&hl_startpos, &regmatch,
2043 spp, SPO_HS_OFF, -1);
2044
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002045 // Compute the region start.
2046 // Default is to use the end of the match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 syn_add_end_off(&eos_pos, &regmatch,
2048 spp, SPO_RS_OFF, 0);
2049
2050 /*
2051 * Grab the external submatches before they get
2052 * overwritten. Reference count doesn't change.
2053 */
2054 unref_extmatch(cur_extmatch);
2055 cur_extmatch = re_extmatch_out;
2056 re_extmatch_out = NULL;
2057
2058 flags = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002059 eoe_pos.lnum = 0; // avoid warning
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 eoe_pos.col = 0;
2061 end_idx = 0;
2062 hl_endpos.lnum = 0;
2063
2064 /*
2065 * For a "oneline" the end must be found in the
2066 * same line too. Search for it after the end of
2067 * the match with the start pattern. Set the
2068 * resulting end positions at the same time.
2069 */
2070 if (spp->sp_type == SPTYPE_START
2071 && (spp->sp_flags & HL_ONELINE))
2072 {
2073 lpos_T startpos;
2074
2075 startpos = endpos;
2076 find_endpos(idx, &startpos, &endpos, &hl_endpos,
2077 &flags, &eoe_pos, &end_idx, cur_extmatch);
2078 if (endpos.lnum == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002079 continue; // not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081
2082 /*
2083 * For a "match" the size must be > 0 after the
2084 * end offset needs has been added. Except when
2085 * syncing.
2086 */
2087 else if (spp->sp_type == SPTYPE_MATCH)
2088 {
2089 syn_add_end_off(&hl_endpos, &regmatch, spp,
2090 SPO_HE_OFF, 0);
2091 syn_add_end_off(&endpos, &regmatch, spp,
2092 SPO_ME_OFF, 0);
2093 if (endpos.lnum == current_lnum
2094 && (int)endpos.col + syncing < startcol)
2095 {
2096 /*
2097 * If an empty string is matched, may need
2098 * to try matching again at next column.
2099 */
2100 if (regmatch.startpos[0].col
2101 == regmatch.endpos[0].col)
2102 try_next_column = TRUE;
2103 continue;
2104 }
2105 }
2106
2107 /*
2108 * keep the best match so far in next_match_*
2109 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002110 // Highlighting must start after startpos and end
2111 // before endpos.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 if (hl_startpos.lnum == current_lnum
2113 && (int)hl_startpos.col < startcol)
2114 hl_startpos.col = startcol;
2115 limit_pos_zero(&hl_endpos, &endpos);
2116
2117 next_match_idx = idx;
2118 next_match_col = startcol;
2119 next_match_m_endpos = endpos;
2120 next_match_h_endpos = hl_endpos;
2121 next_match_h_startpos = hl_startpos;
2122 next_match_flags = flags;
2123 next_match_eos_pos = eos_pos;
2124 next_match_eoe_pos = eoe_pos;
2125 next_match_end_idx = end_idx;
2126 unref_extmatch(next_match_extmatch);
2127 next_match_extmatch = cur_extmatch;
2128 cur_extmatch = NULL;
2129 }
2130 }
2131 }
2132
2133 /*
2134 * If we found a match at the current column, use it.
2135 */
2136 if (next_match_idx >= 0 && next_match_col == (int)current_col)
2137 {
2138 synpat_T *lspp;
2139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002140 // When a zero-width item matched which has a nextgroup,
2141 // don't push the item but set nextgroup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002142 lspp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (next_match_m_endpos.lnum == current_lnum
2144 && next_match_m_endpos.col == current_col
2145 && lspp->sp_next_list != NULL)
2146 {
2147 current_next_list = lspp->sp_next_list;
2148 current_next_flags = lspp->sp_flags;
2149 keep_next_list = TRUE;
2150 zero_width_next_list = TRUE;
2151
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 // Add the index to a list, so that we can check
2153 // later that we don't match it again (and cause an
2154 // endless loop).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (ga_grow(&zero_width_next_ga, 1) == OK)
2156 {
2157 ((int *)(zero_width_next_ga.ga_data))
2158 [zero_width_next_ga.ga_len++] = next_match_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
2160 next_match_idx = -1;
2161 }
2162 else
2163 cur_si = push_next_match(cur_si);
2164 found_match = TRUE;
2165 }
2166 }
2167 }
2168
2169 /*
2170 * Handle searching for nextgroup match.
2171 */
2172 if (current_next_list != NULL && !keep_next_list)
2173 {
2174 /*
2175 * If a nextgroup was not found, continue looking for one if:
2176 * - this is an empty line and the "skipempty" option was given
2177 * - we are on white space and the "skipwhite" option was given
2178 */
2179 if (!found_match)
2180 {
2181 line = syn_getcurline();
2182 if (((current_next_flags & HL_SKIPWHITE)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002183 && VIM_ISWHITE(line[current_col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 || ((current_next_flags & HL_SKIPEMPTY)
2185 && *line == NUL))
2186 break;
2187 }
2188
2189 /*
2190 * If a nextgroup was found: Use it, and continue looking for
2191 * contained matches.
2192 * If a nextgroup was not found: Continue looking for a normal
2193 * match.
2194 * When did set current_next_list for a zero-width item and no
2195 * match was found don't loop (would get stuck).
2196 */
2197 current_next_list = NULL;
2198 next_match_idx = -1;
2199 if (!zero_width_next_list)
2200 found_match = TRUE;
2201 }
2202
2203 } while (found_match);
2204
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002205 restore_chartab(buf_chartab);
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /*
2208 * Use attributes from the current state, if within its highlighting.
2209 * If not, use attributes from the current-but-one state, etc.
2210 */
2211 current_attr = 0;
2212#ifdef FEAT_EVAL
2213 current_id = 0;
2214 current_trans_id = 0;
2215#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002216#ifdef FEAT_CONCEAL
2217 current_flags = 0;
Bram Moolenaarcc0750d2017-06-24 22:29:24 +02002218 current_seqnr = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 if (cur_si != NULL)
2221 {
Bram Moolenaar217ad922005-03-20 22:37:15 +00002222#ifndef FEAT_EVAL
2223 int current_trans_id = 0;
2224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 for (idx = current_state.ga_len - 1; idx >= 0; --idx)
2226 {
2227 sip = &CUR_STATE(idx);
2228 if ((current_lnum > sip->si_h_startpos.lnum
2229 || (current_lnum == sip->si_h_startpos.lnum
2230 && current_col >= sip->si_h_startpos.col))
2231 && (sip->si_h_endpos.lnum == 0
2232 || current_lnum < sip->si_h_endpos.lnum
2233 || (current_lnum == sip->si_h_endpos.lnum
2234 && current_col < sip->si_h_endpos.col)))
2235 {
2236 current_attr = sip->si_attr;
2237#ifdef FEAT_EVAL
2238 current_id = sip->si_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002240 current_trans_id = sip->si_trans_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002241#ifdef FEAT_CONCEAL
2242 current_flags = sip->si_flags;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002243 current_seqnr = sip->si_seqnr;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02002244 current_sub_char = sip->si_cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 break;
2247 }
2248 }
2249
Bram Moolenaar217ad922005-03-20 22:37:15 +00002250 if (can_spell != NULL)
2251 {
2252 struct sp_syn sps;
2253
2254 /*
2255 * set "can_spell" to TRUE if spell checking is supposed to be
2256 * done in the current item.
2257 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002258 if (syn_block->b_spell_cluster_id == 0)
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002259 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002260 // There is no @Spell cluster: Do spelling for items without
2261 // @NoSpell cluster.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002262 if (syn_block->b_nospell_cluster_id == 0
2263 || current_trans_id == 0)
2264 *can_spell = (syn_block->b_syn_spell != SYNSPL_NOTOP);
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002265 else
2266 {
2267 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002268 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002269 sps.cont_in_list = NULL;
2270 *can_spell = !in_id_list(sip, sip->si_cont_list, &sps, 0);
2271 }
2272 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002273 else
2274 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002275 // The @Spell cluster is defined: Do spelling in items with
2276 // the @Spell cluster. But not when @NoSpell is also there.
2277 // At the toplevel only spell check when ":syn spell toplevel"
2278 // was used.
Bram Moolenaar3638c682005-06-08 22:05:14 +00002279 if (current_trans_id == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002280 *can_spell = (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar3638c682005-06-08 22:05:14 +00002281 else
2282 {
2283 sps.inc_tag = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002284 sps.id = syn_block->b_spell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002285 sps.cont_in_list = NULL;
2286 *can_spell = in_id_list(sip, sip->si_cont_list, &sps, 0);
2287
Bram Moolenaar860cae12010-06-05 23:22:07 +02002288 if (syn_block->b_nospell_cluster_id != 0)
Bram Moolenaar3638c682005-06-08 22:05:14 +00002289 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002290 sps.id = syn_block->b_nospell_cluster_id;
Bram Moolenaar3638c682005-06-08 22:05:14 +00002291 if (in_id_list(sip, sip->si_cont_list, &sps, 0))
2292 *can_spell = FALSE;
2293 }
2294 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002295 }
2296 }
2297
2298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 /*
2300 * Check for end of current state (and the states before it) at the
2301 * next column. Don't do this for syncing, because we would miss a
2302 * single character match.
2303 * First check if the current state ends at the current column. It
2304 * may be for an empty match and a containing item might end in the
2305 * current column.
2306 */
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002307 if (!syncing && !keep_state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 check_state_ends();
Bram Moolenaar81366db2005-07-24 21:16:51 +00002310 if (current_state.ga_len > 0
2311 && syn_getcurline()[current_col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 ++current_col;
2314 check_state_ends();
2315 --current_col;
2316 }
2317 }
2318 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002319 else if (can_spell != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002320 // Default: Only do spelling when there is no @Spell cluster or when
2321 // ":syn spell toplevel" was used.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002322 *can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
2323 ? (syn_block->b_spell_cluster_id == 0)
2324 : (syn_block->b_syn_spell == SYNSPL_TOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002326 // nextgroup ends at end of line, unless "skipnl" or "skipempty" present
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (current_next_list != NULL
Bram Moolenaar069dafc2018-03-03 20:02:19 +01002328 && (line = syn_getcurline())[current_col] != NUL
2329 && line[current_col + 1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 && !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
2331 current_next_list = NULL;
2332
2333 if (zero_width_next_ga.ga_len > 0)
2334 ga_clear(&zero_width_next_ga);
2335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002336 // No longer need external matches. But keep next_match_extmatch.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 unref_extmatch(re_extmatch_out);
2338 re_extmatch_out = NULL;
2339 unref_extmatch(cur_extmatch);
2340
2341 return current_attr;
2342}
2343
2344
2345/*
2346 * Check if we already matched pattern "idx" at the current column.
2347 */
2348 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002349did_match_already(int idx, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351 int i;
2352
2353 for (i = current_state.ga_len; --i >= 0; )
2354 if (CUR_STATE(i).si_m_startcol == (int)current_col
2355 && CUR_STATE(i).si_m_lnum == (int)current_lnum
2356 && CUR_STATE(i).si_idx == idx)
2357 return TRUE;
2358
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002359 // Zero-width matches with a nextgroup argument are not put on the syntax
2360 // stack, and can only be matched once anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 for (i = gap->ga_len; --i >= 0; )
2362 if (((int *)(gap->ga_data))[i] == idx)
2363 return TRUE;
2364
2365 return FALSE;
2366}
2367
2368/*
2369 * Push the next match onto the stack.
2370 */
2371 static stateitem_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002372push_next_match(stateitem_T *cur_si)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 synpat_T *spp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002375#ifdef FEAT_CONCEAL
2376 int save_flags;
2377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
Bram Moolenaar860cae12010-06-05 23:22:07 +02002379 spp = &(SYN_ITEMS(syn_block)[next_match_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381 /*
2382 * Push the item in current_state stack;
2383 */
2384 if (push_current_state(next_match_idx) == OK)
2385 {
2386 /*
2387 * If it's a start-skip-end type that crosses lines, figure out how
2388 * much it continues in this line. Otherwise just fill in the length.
2389 */
2390 cur_si = &CUR_STATE(current_state.ga_len - 1);
2391 cur_si->si_h_startpos = next_match_h_startpos;
2392 cur_si->si_m_startcol = current_col;
2393 cur_si->si_m_lnum = current_lnum;
2394 cur_si->si_flags = spp->sp_flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002395#ifdef FEAT_CONCEAL
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002396 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar6e202e52010-07-28 18:14:45 +02002397 cur_si->si_cchar = spp->sp_cchar;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002398 if (current_state.ga_len > 1)
2399 cur_si->si_flags |=
2400 CUR_STATE(current_state.ga_len - 2).si_flags & HL_CONCEAL;
2401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 cur_si->si_next_list = spp->sp_next_list;
2403 cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
2404 if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
2405 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 // Try to find the end pattern in the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
2408 check_keepend();
2409 }
2410 else
2411 {
2412 cur_si->si_m_endpos = next_match_m_endpos;
2413 cur_si->si_h_endpos = next_match_h_endpos;
2414 cur_si->si_ends = TRUE;
2415 cur_si->si_flags |= next_match_flags;
2416 cur_si->si_eoe_pos = next_match_eoe_pos;
2417 cur_si->si_end_idx = next_match_end_idx;
2418 }
2419 if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
2420 keepend_level = current_state.ga_len - 1;
2421 check_keepend();
2422 update_si_attr(current_state.ga_len - 1);
2423
Bram Moolenaar860cae12010-06-05 23:22:07 +02002424#ifdef FEAT_CONCEAL
2425 save_flags = cur_si->si_flags & (HL_CONCEAL | HL_CONCEALENDS);
2426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /*
2428 * If the start pattern has another highlight group, push another item
2429 * on the stack for the start pattern.
2430 */
2431 if ( spp->sp_type == SPTYPE_START
2432 && spp->sp_syn_match_id != 0
2433 && push_current_state(next_match_idx) == OK)
2434 {
2435 cur_si = &CUR_STATE(current_state.ga_len - 1);
2436 cur_si->si_h_startpos = next_match_h_startpos;
2437 cur_si->si_m_startcol = current_col;
2438 cur_si->si_m_lnum = current_lnum;
2439 cur_si->si_m_endpos = next_match_eos_pos;
2440 cur_si->si_h_endpos = next_match_eos_pos;
2441 cur_si->si_ends = TRUE;
2442 cur_si->si_end_idx = 0;
2443 cur_si->si_flags = HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002444#ifdef FEAT_CONCEAL
Bram Moolenaar3b953892010-07-27 20:47:25 +02002445 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002446 cur_si->si_flags |= save_flags;
2447 if (cur_si->si_flags & HL_CONCEALENDS)
2448 cur_si->si_flags |= HL_CONCEAL;
2449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 cur_si->si_next_list = NULL;
2451 check_keepend();
2452 update_si_attr(current_state.ga_len - 1);
2453 }
2454 }
2455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002456 next_match_idx = -1; // try other match next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 return cur_si;
2459}
2460
2461/*
2462 * Check for end of current state (and the states before it).
2463 */
2464 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002465check_state_ends(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 stateitem_T *cur_si;
Bram Moolenaar6fa46362011-05-25 17:56:27 +02002468 int had_extend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 cur_si = &CUR_STATE(current_state.ga_len - 1);
2471 for (;;)
2472 {
2473 if (cur_si->si_ends
2474 && (cur_si->si_m_endpos.lnum < current_lnum
2475 || (cur_si->si_m_endpos.lnum == current_lnum
2476 && cur_si->si_m_endpos.col <= current_col)))
2477 {
2478 /*
2479 * If there is an end pattern group ID, highlight the end pattern
2480 * now. No need to pop the current item from the stack.
2481 * Only do this if the end pattern continues beyond the current
2482 * position.
2483 */
2484 if (cur_si->si_end_idx
2485 && (cur_si->si_eoe_pos.lnum > current_lnum
2486 || (cur_si->si_eoe_pos.lnum == current_lnum
2487 && cur_si->si_eoe_pos.col > current_col)))
2488 {
2489 cur_si->si_idx = cur_si->si_end_idx;
2490 cur_si->si_end_idx = 0;
2491 cur_si->si_m_endpos = cur_si->si_eoe_pos;
2492 cur_si->si_h_endpos = cur_si->si_eoe_pos;
2493 cur_si->si_flags |= HL_MATCH;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002494#ifdef FEAT_CONCEAL
Bram Moolenaar3b953892010-07-27 20:47:25 +02002495 cur_si->si_seqnr = next_seqnr++;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002496 if (cur_si->si_flags & HL_CONCEALENDS)
2497 cur_si->si_flags |= HL_CONCEAL;
2498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 update_si_attr(current_state.ga_len - 1);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002500
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002501 // nextgroup= should not match in the end pattern
Bram Moolenaar3a7d8c32011-05-19 12:14:10 +02002502 current_next_list = NULL;
2503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002504 // what matches next may be different now, clear it
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002505 next_match_idx = 0;
2506 next_match_col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 break;
2508 }
2509 else
2510 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002511 // handle next_list, unless at end of line and no "skipnl" or
2512 // "skipempty"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 current_next_list = cur_si->si_next_list;
2514 current_next_flags = cur_si->si_flags;
2515 if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
2516 && syn_getcurline()[current_col] == NUL)
2517 current_next_list = NULL;
2518
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002519 // When the ended item has "extend", another item with
2520 // "keepend" now needs to check for its end.
Bram Moolenaar6fa46362011-05-25 17:56:27 +02002521 had_extend = (cur_si->si_flags & HL_EXTEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 pop_current_state();
2524
2525 if (current_state.ga_len == 0)
2526 break;
2527
Bram Moolenaar81993f42008-01-11 20:27:45 +00002528 if (had_extend && keepend_level >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 syn_update_ends(FALSE);
2531 if (current_state.ga_len == 0)
2532 break;
2533 }
2534
2535 cur_si = &CUR_STATE(current_state.ga_len - 1);
2536
2537 /*
2538 * Only for a region the search for the end continues after
2539 * the end of the contained item. If the contained match
2540 * included the end-of-line, break here, the region continues.
2541 * Don't do this when:
2542 * - "keepend" is used for the contained item
2543 * - not at the end of the line (could be end="x$"me=e-1).
2544 * - "excludenl" is used (HL_HAS_EOL won't be set)
2545 */
2546 if (cur_si->si_idx >= 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02002547 && SYN_ITEMS(syn_block)[cur_si->si_idx].sp_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 == SPTYPE_START
2549 && !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
2550 {
2551 update_si_end(cur_si, (int)current_col, TRUE);
2552 check_keepend();
2553 if ((current_next_flags & HL_HAS_EOL)
2554 && keepend_level < 0
2555 && syn_getcurline()[current_col] == NUL)
2556 break;
2557 }
2558 }
2559 }
2560 else
2561 break;
2562 }
2563}
2564
2565/*
2566 * Update an entry in the current_state stack for a match or region. This
2567 * fills in si_attr, si_next_list and si_cont_list.
2568 */
2569 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002570update_si_attr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 stateitem_T *sip = &CUR_STATE(idx);
2573 synpat_T *spp;
2574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002575 // This should not happen...
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002576 if (sip->si_idx < 0)
2577 return;
2578
Bram Moolenaar860cae12010-06-05 23:22:07 +02002579 spp = &(SYN_ITEMS(syn_block)[sip->si_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (sip->si_flags & HL_MATCH)
2581 sip->si_id = spp->sp_syn_match_id;
2582 else
2583 sip->si_id = spp->sp_syn.id;
2584 sip->si_attr = syn_id2attr(sip->si_id);
2585 sip->si_trans_id = sip->si_id;
2586 if (sip->si_flags & HL_MATCH)
2587 sip->si_cont_list = NULL;
2588 else
2589 sip->si_cont_list = spp->sp_cont_list;
2590
2591 /*
2592 * For transparent items, take attr from outer item.
2593 * Also take cont_list, if there is none.
2594 * Don't do this for the matchgroup of a start or end pattern.
2595 */
2596 if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
2597 {
2598 if (idx == 0)
2599 {
2600 sip->si_attr = 0;
2601 sip->si_trans_id = 0;
2602 if (sip->si_cont_list == NULL)
2603 sip->si_cont_list = ID_LIST_ALL;
2604 }
2605 else
2606 {
2607 sip->si_attr = CUR_STATE(idx - 1).si_attr;
2608 sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002609 sip->si_h_startpos = CUR_STATE(idx - 1).si_h_startpos;
2610 sip->si_h_endpos = CUR_STATE(idx - 1).si_h_endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (sip->si_cont_list == NULL)
2612 {
2613 sip->si_flags |= HL_TRANS_CONT;
2614 sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
2615 }
2616 }
2617 }
2618}
2619
2620/*
2621 * Check the current stack for patterns with "keepend" flag.
2622 * Propagate the match-end to contained items, until a "skipend" item is found.
2623 */
2624 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002625check_keepend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 int i;
2628 lpos_T maxpos;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002629 lpos_T maxpos_h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 stateitem_T *sip;
2631
2632 /*
2633 * This check can consume a lot of time; only do it from the level where
2634 * there really is a keepend.
2635 */
2636 if (keepend_level < 0)
2637 return;
2638
2639 /*
2640 * Find the last index of an "extend" item. "keepend" items before that
2641 * won't do anything. If there is no "extend" item "i" will be
2642 * "keepend_level" and all "keepend" items will work normally.
2643 */
2644 for (i = current_state.ga_len - 1; i > keepend_level; --i)
2645 if (CUR_STATE(i).si_flags & HL_EXTEND)
2646 break;
2647
2648 maxpos.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002649 maxpos.col = 0;
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002650 maxpos_h.lnum = 0;
Bram Moolenaared39e1d2008-08-09 17:55:22 +00002651 maxpos_h.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 for ( ; i < current_state.ga_len; ++i)
2653 {
2654 sip = &CUR_STATE(i);
2655 if (maxpos.lnum != 0)
2656 {
2657 limit_pos_zero(&sip->si_m_endpos, &maxpos);
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002658 limit_pos_zero(&sip->si_h_endpos, &maxpos_h);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 limit_pos_zero(&sip->si_eoe_pos, &maxpos);
2660 sip->si_ends = TRUE;
2661 }
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002662 if (sip->si_ends && (sip->si_flags & HL_KEEPEND))
2663 {
2664 if (maxpos.lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 || maxpos.lnum > sip->si_m_endpos.lnum
2666 || (maxpos.lnum == sip->si_m_endpos.lnum
Bram Moolenaar7bd2cd82006-10-03 15:04:36 +00002667 && maxpos.col > sip->si_m_endpos.col))
2668 maxpos = sip->si_m_endpos;
2669 if (maxpos_h.lnum == 0
2670 || maxpos_h.lnum > sip->si_h_endpos.lnum
2671 || (maxpos_h.lnum == sip->si_h_endpos.lnum
2672 && maxpos_h.col > sip->si_h_endpos.col))
2673 maxpos_h = sip->si_h_endpos;
2674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
2676}
2677
2678/*
2679 * Update an entry in the current_state stack for a start-skip-end pattern.
2680 * This finds the end of the current item, if it's in the current line.
2681 *
2682 * Return the flags for the matched END.
2683 */
2684 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002685update_si_end(
2686 stateitem_T *sip,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002687 int startcol, // where to start searching for the end
2688 int force) // when TRUE overrule a previous end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 lpos_T startpos;
2691 lpos_T endpos;
2692 lpos_T hl_endpos;
2693 lpos_T end_endpos;
2694 int end_idx;
2695
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002696 // return quickly for a keyword
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002697 if (sip->si_idx < 0)
2698 return;
2699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // Don't update when it's already done. Can be a match of an end pattern
2701 // that started in a previous line. Watch out: can also be a "keepend"
2702 // from a containing item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (!force && sip->si_m_endpos.lnum >= current_lnum)
2704 return;
2705
2706 /*
2707 * We need to find the end of the region. It may continue in the next
2708 * line.
2709 */
2710 end_idx = 0;
2711 startpos.lnum = current_lnum;
2712 startpos.col = startcol;
2713 find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
2714 &(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
2715
2716 if (endpos.lnum == 0)
2717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002718 // No end pattern matched.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002719 if (SYN_ITEMS(syn_block)[sip->si_idx].sp_flags & HL_ONELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002721 // a "oneline" never continues in the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 sip->si_ends = TRUE;
2723 sip->si_m_endpos.lnum = current_lnum;
2724 sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
2725 }
2726 else
2727 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002728 // continues in the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 sip->si_ends = FALSE;
2730 sip->si_m_endpos.lnum = 0;
2731 }
2732 sip->si_h_endpos = sip->si_m_endpos;
2733 }
2734 else
2735 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002736 // match within this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 sip->si_m_endpos = endpos;
2738 sip->si_h_endpos = hl_endpos;
2739 sip->si_eoe_pos = end_endpos;
2740 sip->si_ends = TRUE;
2741 sip->si_end_idx = end_idx;
2742 }
2743}
2744
2745/*
2746 * Add a new state to the current state stack.
2747 * It is cleared and the index set to "idx".
2748 * Return FAIL if it's not possible (out of memory).
2749 */
2750 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002751push_current_state(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 if (ga_grow(&current_state, 1) == FAIL)
2754 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02002755 CLEAR_POINTER(&CUR_STATE(current_state.ga_len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 CUR_STATE(current_state.ga_len).si_idx = idx;
2757 ++current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 return OK;
2759}
2760
2761/*
2762 * Remove a state from the current_state stack.
2763 */
2764 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002765pop_current_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 if (current_state.ga_len)
2768 {
2769 unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
2770 --current_state.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002772 // after the end of a pattern, try matching a keyword or pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 next_match_idx = -1;
2774
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002775 // if first state with "keepend" is popped, reset keepend_level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (keepend_level >= current_state.ga_len)
2777 keepend_level = -1;
2778}
2779
2780/*
2781 * Find the end of a start/skip/end syntax region after "startpos".
2782 * Only checks one line.
2783 * Also handles a match item that continued from a previous line.
2784 * If not found, the syntax item continues in the next line. m_endpos->lnum
2785 * will be 0.
2786 * If found, the end of the region and the end of the highlighting is
2787 * computed.
2788 */
2789 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002790find_endpos(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002791 int idx, // index of the pattern
2792 lpos_T *startpos, // where to start looking for an END match
2793 lpos_T *m_endpos, // return: end of match
2794 lpos_T *hl_endpos, // return: end of highlighting
2795 long *flagsp, // return: flags of matching END
2796 lpos_T *end_endpos, // return: end of end pattern match
2797 int *end_idx, // return: group ID for end pat. match, or 0
2798 reg_extmatch_T *start_ext) // submatches from the start pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 colnr_T matchcol;
2801 synpat_T *spp, *spp_skip;
2802 int start_idx;
2803 int best_idx;
2804 regmmatch_T regmatch;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002805 regmmatch_T best_regmatch; // startpos/endpos of best match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 lpos_T pos;
2807 char_u *line;
2808 int had_match = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 char_u buf_chartab[32]; // chartab array for syn option iskyeyword
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002811 // just in case we are invoked for a keyword
Bram Moolenaar3a36cf72007-08-21 15:29:56 +00002812 if (idx < 0)
2813 return;
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 /*
2816 * Check for being called with a START pattern.
2817 * Can happen with a match that continues to the next line, because it
2818 * contained a region.
2819 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002820 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (spp->sp_type != SPTYPE_START)
2822 {
2823 *hl_endpos = *startpos;
2824 return;
2825 }
2826
2827 /*
2828 * Find the SKIP or first END pattern after the last START pattern.
2829 */
2830 for (;;)
2831 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002832 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (spp->sp_type != SPTYPE_START)
2834 break;
2835 ++idx;
2836 }
2837
2838 /*
2839 * Lookup the SKIP pattern (if present)
2840 */
2841 if (spp->sp_type == SPTYPE_SKIP)
2842 {
2843 spp_skip = spp;
2844 ++idx;
2845 }
2846 else
2847 spp_skip = NULL;
2848
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002849 // Setup external matches for syn_regexec().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 unref_extmatch(re_extmatch_in);
2851 re_extmatch_in = ref_extmatch(start_ext);
2852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002853 matchcol = startpos->col; // start looking for a match at sstart
2854 start_idx = idx; // remember the first END pattern.
2855 best_regmatch.startpos[0].col = 0; // avoid compiler warning
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002856
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002857 // use syntax iskeyword option
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002858 save_chartab(buf_chartab);
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 for (;;)
2861 {
2862 /*
2863 * Find end pattern that matches first after "matchcol".
2864 */
2865 best_idx = -1;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002866 for (idx = start_idx; idx < syn_block->b_syn_patterns.ga_len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
2868 int lc_col = matchcol;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002869 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
Bram Moolenaar860cae12010-06-05 23:22:07 +02002871 spp = &(SYN_ITEMS(syn_block)[idx]);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002872 if (spp->sp_type != SPTYPE_END) // past last END pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 break;
2874 lc_col -= spp->sp_offsets[SPO_LC_OFF];
2875 if (lc_col < 0)
2876 lc_col = 0;
2877
2878 regmatch.rmm_ic = spp->sp_ic;
2879 regmatch.regprog = spp->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002880 r = syn_regexec(&regmatch, startpos->lnum, lc_col,
2881 IF_SYN_TIME(&spp->sp_time));
2882 spp->sp_prog = regmatch.regprog;
2883 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 if (best_idx == -1 || regmatch.startpos[0].col
2886 < best_regmatch.startpos[0].col)
2887 {
2888 best_idx = idx;
2889 best_regmatch.startpos[0] = regmatch.startpos[0];
2890 best_regmatch.endpos[0] = regmatch.endpos[0];
2891 }
2892 }
2893 }
2894
2895 /*
2896 * If all end patterns have been tried, and there is no match, the
2897 * item continues until end-of-line.
2898 */
2899 if (best_idx == -1)
2900 break;
2901
2902 /*
2903 * If the skip pattern matches before the end pattern,
2904 * continue searching after the skip pattern.
2905 */
2906 if (spp_skip != NULL)
2907 {
2908 int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002909 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 if (lc_col < 0)
2912 lc_col = 0;
2913 regmatch.rmm_ic = spp_skip->sp_ic;
2914 regmatch.regprog = spp_skip->sp_prog;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002915 r = syn_regexec(&regmatch, startpos->lnum, lc_col,
2916 IF_SYN_TIME(&spp_skip->sp_time));
2917 spp_skip->sp_prog = regmatch.regprog;
2918 if (r && regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 <= best_regmatch.startpos[0].col)
2920 {
Bram Moolenaar04bff882016-01-05 20:46:16 +01002921 int line_len;
2922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002923 // Add offset to skip pattern match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 syn_add_end_off(&pos, &regmatch, spp_skip, SPO_ME_OFF, 1);
2925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002926 // If the skip pattern goes on to the next line, there is no
2927 // match with an end pattern in this line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (pos.lnum > startpos->lnum)
2929 break;
2930
2931 line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
Bram Moolenaar04bff882016-01-05 20:46:16 +01002932 line_len = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002934 // take care of an empty match or negative offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (pos.col <= matchcol)
2936 ++matchcol;
2937 else if (pos.col <= regmatch.endpos[0].col)
2938 matchcol = pos.col;
2939 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002940 // Be careful not to jump over the NUL at the end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 for (matchcol = regmatch.endpos[0].col;
Bram Moolenaar04bff882016-01-05 20:46:16 +01002942 matchcol < line_len && matchcol < pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 ++matchcol)
2944 ;
2945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002946 // if the skip pattern includes end-of-line, break here
Bram Moolenaar04bff882016-01-05 20:46:16 +01002947 if (matchcol >= line_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 break;
2949
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002950 continue; // start with first end pattern again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952 }
2953
2954 /*
2955 * Match from start pattern to end pattern.
2956 * Correct for match and highlight offset of end pattern.
2957 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002958 spp = &(SYN_ITEMS(syn_block)[best_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002960 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
2962 m_endpos->col = startpos->col;
2963
2964 syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002965 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 if (end_endpos->lnum == startpos->lnum
2967 && end_endpos->col < startpos->col)
2968 end_endpos->col = startpos->col;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002969 // can't end after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 limit_pos(end_endpos, m_endpos);
2971
2972 /*
2973 * If the end group is highlighted differently, adjust the pointers.
2974 */
2975 if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
2976 {
2977 *end_idx = best_idx;
2978 if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
2979 {
2980 hl_endpos->lnum = best_regmatch.endpos[0].lnum;
2981 hl_endpos->col = best_regmatch.endpos[0].col;
2982 }
2983 else
2984 {
2985 hl_endpos->lnum = best_regmatch.startpos[0].lnum;
2986 hl_endpos->col = best_regmatch.startpos[0].col;
2987 }
2988 hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
2989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002990 // can't end before the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (hl_endpos->lnum == startpos->lnum
2992 && hl_endpos->col < startpos->col)
2993 hl_endpos->col = startpos->col;
2994 limit_pos(hl_endpos, m_endpos);
2995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002996 // now the match ends where the highlighting ends, it is turned
2997 // into the matchgroup for the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 *m_endpos = *hl_endpos;
2999 }
3000 else
3001 {
3002 *end_idx = 0;
3003 *hl_endpos = *end_endpos;
3004 }
3005
3006 *flagsp = spp->sp_flags;
3007
3008 had_match = TRUE;
3009 break;
3010 }
3011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003012 // no match for an END pattern in this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (!had_match)
3014 m_endpos->lnum = 0;
3015
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003016 restore_chartab(buf_chartab);
3017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003018 // Remove external matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 unref_extmatch(re_extmatch_in);
3020 re_extmatch_in = NULL;
3021}
3022
3023/*
3024 * Limit "pos" not to be after "limit".
3025 */
3026 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003027limit_pos(lpos_T *pos, lpos_T *limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 if (pos->lnum > limit->lnum)
3030 *pos = *limit;
3031 else if (pos->lnum == limit->lnum && pos->col > limit->col)
3032 pos->col = limit->col;
3033}
3034
3035/*
3036 * Limit "pos" not to be after "limit", unless pos->lnum is zero.
3037 */
3038 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003039limit_pos_zero(
3040 lpos_T *pos,
3041 lpos_T *limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 if (pos->lnum == 0)
3044 *pos = *limit;
3045 else
3046 limit_pos(pos, limit);
3047}
3048
3049/*
3050 * Add offset to matched text for end of match or highlight.
3051 */
3052 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003053syn_add_end_off(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003054 lpos_T *result, // returned position
3055 regmmatch_T *regmatch, // start/end of match
3056 synpat_T *spp, // matched pattern
3057 int idx, // index of offset
3058 int extra) // extra chars for offset to start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003061 int off;
3062 char_u *base;
3063 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 if (spp->sp_off_flags & (1 << idx))
3066 {
3067 result->lnum = regmatch->startpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003068 col = regmatch->startpos[0].col;
3069 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071 else
3072 {
3073 result->lnum = regmatch->endpos[0].lnum;
3074 col = regmatch->endpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003075 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003077 // Don't go past the end of the line. Matters for "rs=e+2" when there
3078 // is a matchgroup. Watch out for match with last NL in the buffer.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079 if (result->lnum > syn_buf->b_ml.ml_line_count)
3080 col = 0;
3081 else if (off != 0)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003082 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003083 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3084 p = base + col;
3085 if (off > 0)
3086 {
3087 while (off-- > 0 && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003088 MB_PTR_ADV(p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089 }
3090 else if (off < 0)
3091 {
3092 while (off++ < 0 && base < p)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003093 MB_PTR_BACK(base, p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094 }
3095 col = (int)(p - base);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003096 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
3099
3100/*
3101 * Add offset to matched text for start of match or highlight.
3102 * Avoid resulting column to become negative.
3103 */
3104 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003105syn_add_start_off(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003106 lpos_T *result, // returned position
3107 regmmatch_T *regmatch, // start/end of match
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003108 synpat_T *spp,
3109 int idx,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003110 int extra) // extra chars for offset to end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113 int off;
3114 char_u *base;
3115 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
3118 {
3119 result->lnum = regmatch->endpos[0].lnum;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120 col = regmatch->endpos[0].col;
3121 off = spp->sp_offsets[idx] + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
3123 else
3124 {
3125 result->lnum = regmatch->startpos[0].lnum;
3126 col = regmatch->startpos[0].col;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127 off = spp->sp_offsets[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003129 if (result->lnum > syn_buf->b_ml.ml_line_count)
3130 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003131 // a "\n" at the end of the pattern may take us below the last line
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003132 result->lnum = syn_buf->b_ml.ml_line_count;
Bram Moolenaar8b9c05f2010-03-02 17:54:33 +01003133 col = (int)STRLEN(ml_get_buf(syn_buf, result->lnum, FALSE));
Bram Moolenaar72b73c12010-02-24 17:22:20 +01003134 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135 if (off != 0)
3136 {
3137 base = ml_get_buf(syn_buf, result->lnum, FALSE);
3138 p = base + col;
3139 if (off > 0)
3140 {
3141 while (off-- && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003142 MB_PTR_ADV(p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 }
3144 else if (off < 0)
3145 {
3146 while (off++ && base < p)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003147 MB_PTR_BACK(base, p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003148 }
3149 col = (int)(p - base);
3150 }
3151 result->col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152}
3153
3154/*
3155 * Get current line in syntax buffer.
3156 */
3157 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003158syn_getcurline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 return ml_get_buf(syn_buf, current_lnum, FALSE);
3161}
3162
3163/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003164 * Call vim_regexec() to find a match with "rmp" in "syn_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 * Returns TRUE when there is a match.
3166 */
3167 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003168syn_regexec(
3169 regmmatch_T *rmp,
3170 linenr_T lnum,
3171 colnr_T col,
3172 syn_time_T *st UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003174 int r;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003175#ifdef FEAT_RELTIME
3176 int timed_out = FALSE;
3177#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +02003178#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003179 proftime_T pt;
3180
3181 if (syn_time_on)
3182 profile_start(&pt);
3183#endif
3184
Bram Moolenaarbcf94422018-06-23 14:21:42 +02003185 if (rmp->regprog == NULL)
3186 // This can happen if a previous call to vim_regexec_multi() tried to
3187 // use the NFA engine, which resulted in NFA_TOO_EXPENSIVE, and
3188 // compiling the pattern with the other engine fails.
3189 return FALSE;
3190
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003191 rmp->rmm_maxcol = syn_buf->b_p_smc;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003192 r = vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col,
3193#ifdef FEAT_RELTIME
3194 syn_tm, &timed_out
3195#else
3196 NULL, NULL
3197#endif
3198 );
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003199
Bram Moolenaarf7512552013-06-06 14:55:19 +02003200#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003201 if (syn_time_on)
3202 {
3203 profile_end(&pt);
3204 profile_add(&st->total, &pt);
3205 if (profile_cmp(&pt, &st->slowest) < 0)
3206 st->slowest = pt;
3207 ++st->count;
3208 if (r > 0)
3209 ++st->match;
3210 }
3211#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003212#ifdef FEAT_RELTIME
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003213 if (timed_out && !syn_win->w_s->b_syn_slow)
3214 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003215 syn_win->w_s->b_syn_slow = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003216 msg(_("'redrawtime' exceeded, syntax highlighting disabled"));
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003217 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003218#endif
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02003219
3220 if (r > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
3222 rmp->startpos[0].lnum += lnum;
3223 rmp->endpos[0].lnum += lnum;
3224 return TRUE;
3225 }
3226 return FALSE;
3227}
3228
3229/*
3230 * Check one position in a line for a matching keyword.
3231 * The caller must check if a keyword can start at startcol.
Bram Moolenaaraab93b12017-03-18 21:37:28 +01003232 * Return its ID if found, 0 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 */
3234 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003235check_keyword_id(
3236 char_u *line,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003237 int startcol, // position in line to check for keyword
3238 int *endcolp, // return: character after found keyword
3239 long *flagsp, // return: flags of matching keyword
3240 short **next_listp, // return: next_list of matching keyword
3241 stateitem_T *cur_si, // item at the top of the stack
3242 int *ccharp UNUSED) // conceal substitution char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
Bram Moolenaardad6b692005-01-25 22:14:34 +00003244 keyentry_T *kp;
3245 char_u *kwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 int round;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003247 int kwlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003248 char_u keyword[MAXKEYWLEN + 1]; // assume max. keyword len is 80
Bram Moolenaardad6b692005-01-25 22:14:34 +00003249 hashtab_T *ht;
3250 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003252 // Find first character after the keyword. First character was already
3253 // checked.
Bram Moolenaardad6b692005-01-25 22:14:34 +00003254 kwp = line + startcol;
3255 kwlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 do
3257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003259 kwlen += (*mb_ptr2len)(kwp + kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else
Bram Moolenaardad6b692005-01-25 22:14:34 +00003261 ++kwlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01003263 while (vim_iswordp_buf(kwp + kwlen, syn_buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
Bram Moolenaardad6b692005-01-25 22:14:34 +00003265 if (kwlen > MAXKEYWLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 return 0;
3267
3268 /*
3269 * Must make a copy of the keyword, so we can add a NUL and make it
3270 * lowercase.
3271 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003272 vim_strncpy(keyword, kwp, kwlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 /*
3275 * Try twice:
3276 * 1. matching case
3277 * 2. ignoring case
3278 */
3279 for (round = 1; round <= 2; ++round)
3280 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003281 ht = round == 1 ? &syn_block->b_keywtab : &syn_block->b_keywtab_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00003282 if (ht->ht_used == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003284 if (round == 2) // ignore case
Bram Moolenaardad6b692005-01-25 22:14:34 +00003285 (void)str_foldcase(kwp, kwlen, keyword, MAXKEYWLEN + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287 /*
Bram Moolenaardad6b692005-01-25 22:14:34 +00003288 * Find keywords that match. There can be several with different
3289 * attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 * When current_next_list is non-zero accept only that group, otherwise:
3291 * Accept a not-contained keyword at toplevel.
3292 * Accept a keyword at other levels only if it is in the contains list.
3293 */
Bram Moolenaardad6b692005-01-25 22:14:34 +00003294 hi = hash_find(ht, keyword);
3295 if (!HASHITEM_EMPTY(hi))
3296 for (kp = HI2KE(hi); kp != NULL; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00003298 if (current_next_list != 0
3299 ? in_id_list(NULL, current_next_list, &kp->k_syn, 0)
3300 : (cur_si == NULL
3301 ? !(kp->flags & HL_CONTAINED)
3302 : in_id_list(cur_si, cur_si->si_cont_list,
3303 &kp->k_syn, kp->flags & HL_CONTAINED)))
3304 {
3305 *endcolp = startcol + kwlen;
3306 *flagsp = kp->flags;
3307 *next_listp = kp->next_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003308#ifdef FEAT_CONCEAL
3309 *ccharp = kp->k_char;
3310#endif
Bram Moolenaardad6b692005-01-25 22:14:34 +00003311 return kp->k_syn.id;
3312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 }
3315 return 0;
3316}
3317
3318/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02003319 * Handle ":syntax conceal" command.
3320 */
3321 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003322syn_cmd_conceal(exarg_T *eap UNUSED, int syncing UNUSED)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003323{
3324#ifdef FEAT_CONCEAL
3325 char_u *arg = eap->arg;
3326 char_u *next;
3327
3328 eap->nextcmd = find_nextcmd(arg);
3329 if (eap->skip)
3330 return;
3331
3332 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003333 if (*arg == NUL)
3334 {
3335 if (curwin->w_s->b_syn_conceal)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003336 msg(_("syntax conceal on"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003337 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003338 msg(_("syntax conceal off"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003339 }
3340 else if (STRNICMP(arg, "on", 2) == 0 && next - arg == 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003341 curwin->w_s->b_syn_conceal = TRUE;
3342 else if (STRNICMP(arg, "off", 3) == 0 && next - arg == 3)
3343 curwin->w_s->b_syn_conceal = FALSE;
3344 else
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003345 semsg(_(e_illegal_arg), arg);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003346#endif
3347}
3348
3349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * Handle ":syntax case" command.
3351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003353syn_cmd_case(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 char_u *arg = eap->arg;
3356 char_u *next;
3357
3358 eap->nextcmd = find_nextcmd(arg);
3359 if (eap->skip)
3360 return;
3361
3362 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003363 if (*arg == NUL)
3364 {
3365 if (curwin->w_s->b_syn_ic)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003366 msg(_("syntax case ignore"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003367 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003368 msg(_("syntax case match"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003369 }
3370 else if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003371 curwin->w_s->b_syn_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003373 curwin->w_s->b_syn_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003375 semsg(_(e_illegal_arg), arg);
3376}
3377
3378/*
3379 * Handle ":syntax foldlevel" command.
3380 */
3381 static void
3382syn_cmd_foldlevel(exarg_T *eap, int syncing UNUSED)
3383{
3384 char_u *arg = eap->arg;
3385 char_u *arg_end;
3386
3387 eap->nextcmd = find_nextcmd(arg);
3388 if (eap->skip)
3389 return;
3390
3391 if (*arg == NUL)
3392 {
3393 switch (curwin->w_s->b_syn_foldlevel)
3394 {
3395 case SYNFLD_START: msg(_("syntax foldlevel start")); break;
3396 case SYNFLD_MINIMUM: msg(_("syntax foldlevel minimum")); break;
3397 default: break;
3398 }
3399 return;
3400 }
3401
3402 arg_end = skiptowhite(arg);
3403 if (STRNICMP(arg, "start", 5) == 0 && arg_end - arg == 5)
3404 curwin->w_s->b_syn_foldlevel = SYNFLD_START;
3405 else if (STRNICMP(arg, "minimum", 7) == 0 && arg_end - arg == 7)
3406 curwin->w_s->b_syn_foldlevel = SYNFLD_MINIMUM;
3407 else
3408 {
3409 semsg(_(e_illegal_arg), arg);
3410 return;
3411 }
3412
3413 arg = skipwhite(arg_end);
3414 if (*arg != NUL)
3415 {
3416 semsg(_(e_illegal_arg), arg);
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418}
3419
3420/*
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003421 * Handle ":syntax spell" command.
3422 */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003423 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003424syn_cmd_spell(exarg_T *eap, int syncing UNUSED)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003425{
3426 char_u *arg = eap->arg;
3427 char_u *next;
3428
3429 eap->nextcmd = find_nextcmd(arg);
3430 if (eap->skip)
3431 return;
3432
3433 next = skiptowhite(arg);
Bram Moolenaarde318c52017-01-17 16:27:10 +01003434 if (*arg == NUL)
3435 {
3436 if (curwin->w_s->b_syn_spell == SYNSPL_TOP)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003437 msg(_("syntax spell toplevel"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003438 else if (curwin->w_s->b_syn_spell == SYNSPL_NOTOP)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003439 msg(_("syntax spell notoplevel"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003440 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003441 msg(_("syntax spell default"));
Bram Moolenaarde318c52017-01-17 16:27:10 +01003442 }
3443 else if (STRNICMP(arg, "toplevel", 8) == 0 && next - arg == 8)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003444 curwin->w_s->b_syn_spell = SYNSPL_TOP;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003445 else if (STRNICMP(arg, "notoplevel", 10) == 0 && next - arg == 10)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003446 curwin->w_s->b_syn_spell = SYNSPL_NOTOP;
Bram Moolenaar86ea7642007-02-04 01:48:10 +00003447 else if (STRNICMP(arg, "default", 7) == 0 && next - arg == 7)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003448 curwin->w_s->b_syn_spell = SYNSPL_DEFAULT;
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003449 else
Bram Moolenaar5081d202015-06-25 18:36:26 +02003450 {
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003451 semsg(_(e_illegal_arg), arg);
Bram Moolenaar5081d202015-06-25 18:36:26 +02003452 return;
3453 }
3454
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003455 // assume spell checking changed, force a redraw
Bram Moolenaar5081d202015-06-25 18:36:26 +02003456 redraw_win_later(curwin, NOT_VALID);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003457}
3458
3459/*
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003460 * Handle ":syntax iskeyword" command.
3461 */
3462 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003463syn_cmd_iskeyword(exarg_T *eap, int syncing UNUSED)
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003464{
3465 char_u *arg = eap->arg;
3466 char_u save_chartab[32];
3467 char_u *save_isk;
3468
3469 if (eap->skip)
3470 return;
3471
3472 arg = skipwhite(arg);
3473 if (*arg == NUL)
3474 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003475 msg_puts("\n");
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003476 if (curwin->w_s->b_syn_isk != empty_option)
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003477 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003478 msg_puts(_("syntax iskeyword "));
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003479 msg_outtrans(curwin->w_s->b_syn_isk);
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003480 }
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003481 else
Bram Moolenaar0a6efcd2018-07-20 19:56:10 +02003482 msg_outtrans((char_u *)_("syntax iskeyword not set"));
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003483 }
3484 else
3485 {
3486 if (STRNICMP(arg, "clear", 5) == 0)
3487 {
3488 mch_memmove(curwin->w_s->b_syn_chartab, curbuf->b_chartab,
3489 (size_t)32);
3490 clear_string_option(&curwin->w_s->b_syn_isk);
3491 }
3492 else
3493 {
3494 mch_memmove(save_chartab, curbuf->b_chartab, (size_t)32);
3495 save_isk = curbuf->b_p_isk;
3496 curbuf->b_p_isk = vim_strsave(arg);
3497
3498 buf_init_chartab(curbuf, FALSE);
3499 mch_memmove(curwin->w_s->b_syn_chartab, curbuf->b_chartab,
3500 (size_t)32);
3501 mch_memmove(curbuf->b_chartab, save_chartab, (size_t)32);
3502 clear_string_option(&curwin->w_s->b_syn_isk);
3503 curwin->w_s->b_syn_isk = curbuf->b_p_isk;
3504 curbuf->b_p_isk = save_isk;
3505 }
3506 }
3507 redraw_win_later(curwin, NOT_VALID);
3508}
3509
3510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 * Clear all syntax info for one buffer.
3512 */
3513 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003514syntax_clear(synblock_T *block)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 int i;
3517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003518 block->b_syn_error = FALSE; // clear previous error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003519#ifdef FEAT_RELTIME
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003520 block->b_syn_slow = FALSE; // clear previous timeout
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003521#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003522 block->b_syn_ic = FALSE; // Use case, by default
Bram Moolenaare35a52a2020-05-31 19:48:53 +02003523 block->b_syn_foldlevel = SYNFLD_START;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003524 block->b_syn_spell = SYNSPL_DEFAULT; // default spell checking
Bram Moolenaar860cae12010-06-05 23:22:07 +02003525 block->b_syn_containedin = FALSE;
Bram Moolenaarde318c52017-01-17 16:27:10 +01003526#ifdef FEAT_CONCEAL
3527 block->b_syn_conceal = FALSE;
3528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003530 // free the keywords
Bram Moolenaar860cae12010-06-05 23:22:07 +02003531 clear_keywtab(&block->b_keywtab);
3532 clear_keywtab(&block->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003534 // free the syntax patterns
Bram Moolenaar860cae12010-06-05 23:22:07 +02003535 for (i = block->b_syn_patterns.ga_len; --i >= 0; )
3536 syn_clear_pattern(block, i);
3537 ga_clear(&block->b_syn_patterns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003539 // free the syntax clusters
Bram Moolenaar860cae12010-06-05 23:22:07 +02003540 for (i = block->b_syn_clusters.ga_len; --i >= 0; )
3541 syn_clear_cluster(block, i);
3542 ga_clear(&block->b_syn_clusters);
3543 block->b_spell_cluster_id = 0;
3544 block->b_nospell_cluster_id = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
Bram Moolenaar860cae12010-06-05 23:22:07 +02003546 block->b_syn_sync_flags = 0;
3547 block->b_syn_sync_minlines = 0;
3548 block->b_syn_sync_maxlines = 0;
3549 block->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
Bram Moolenaar473de612013-06-08 18:19:48 +02003551 vim_regfree(block->b_syn_linecont_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003552 block->b_syn_linecont_prog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003553 VIM_CLEAR(block->b_syn_linecont_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#ifdef FEAT_FOLDING
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555 block->b_syn_folditems = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003557 clear_string_option(&block->b_syn_isk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003559 // free the stored states
Bram Moolenaar860cae12010-06-05 23:22:07 +02003560 syn_stack_free_all(block);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 invalidate_current_state();
Bram Moolenaar42431a72011-04-01 14:44:59 +02003562
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003563 // Reset the counter for ":syn include"
Bram Moolenaar42431a72011-04-01 14:44:59 +02003564 running_syn_inc_tag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567/*
Bram Moolenaarfd29f462010-06-06 16:11:09 +02003568 * Get rid of ownsyntax for window "wp".
3569 */
3570 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003571reset_synblock(win_T *wp)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02003572{
3573 if (wp->w_s != &wp->w_buffer->b_s)
3574 {
3575 syntax_clear(wp->w_s);
3576 vim_free(wp->w_s);
3577 wp->w_s = &wp->w_buffer->b_s;
3578 }
3579}
3580
3581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 * Clear syncing info for one buffer.
3583 */
3584 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003585syntax_sync_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
3587 int i;
3588
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003589 // free the syntax patterns
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
3591 if (SYN_ITEMS(curwin->w_s)[i].sp_syncing)
3592 syn_remove_pattern(curwin->w_s, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 curwin->w_s->b_syn_sync_flags = 0;
3595 curwin->w_s->b_syn_sync_minlines = 0;
3596 curwin->w_s->b_syn_sync_maxlines = 0;
3597 curwin->w_s->b_syn_sync_linebreaks = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
Bram Moolenaar473de612013-06-08 18:19:48 +02003599 vim_regfree(curwin->w_s->b_syn_linecont_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 curwin->w_s->b_syn_linecont_prog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003601 VIM_CLEAR(curwin->w_s->b_syn_linecont_pat);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01003602 clear_string_option(&curwin->w_s->b_syn_isk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003604 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605}
3606
3607/*
3608 * Remove one pattern from the buffer's pattern list.
3609 */
3610 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003611syn_remove_pattern(
3612 synblock_T *block,
3613 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
3615 synpat_T *spp;
3616
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 spp = &(SYN_ITEMS(block)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#ifdef FEAT_FOLDING
3619 if (spp->sp_flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003620 --block->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003622 syn_clear_pattern(block, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 mch_memmove(spp, spp + 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02003624 sizeof(synpat_T) * (block->b_syn_patterns.ga_len - idx - 1));
3625 --block->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626}
3627
3628/*
3629 * Clear and free one syntax pattern. When clearing all, must be called from
3630 * last to first!
3631 */
3632 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003633syn_clear_pattern(synblock_T *block, int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003635 vim_free(SYN_ITEMS(block)[i].sp_pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02003636 vim_regfree(SYN_ITEMS(block)[i].sp_prog);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003637 // Only free sp_cont_list and sp_next_list of first start pattern
Bram Moolenaar860cae12010-06-05 23:22:07 +02003638 if (i == 0 || SYN_ITEMS(block)[i - 1].sp_type != SPTYPE_START)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003640 vim_free(SYN_ITEMS(block)[i].sp_cont_list);
3641 vim_free(SYN_ITEMS(block)[i].sp_next_list);
3642 vim_free(SYN_ITEMS(block)[i].sp_syn.cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644}
3645
3646/*
3647 * Clear and free one syntax cluster.
3648 */
3649 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003650syn_clear_cluster(synblock_T *block, int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003652 vim_free(SYN_CLSTR(block)[i].scl_name);
3653 vim_free(SYN_CLSTR(block)[i].scl_name_u);
3654 vim_free(SYN_CLSTR(block)[i].scl_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655}
3656
3657/*
3658 * Handle ":syntax clear" command.
3659 */
3660 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003661syn_cmd_clear(exarg_T *eap, int syncing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 char_u *arg = eap->arg;
3664 char_u *arg_end;
3665 int id;
3666
3667 eap->nextcmd = find_nextcmd(arg);
3668 if (eap->skip)
3669 return;
3670
3671 /*
3672 * We have to disable this within ":syn include @group filename",
3673 * because otherwise @group would get deleted.
3674 * Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
3675 * clear".
3676 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003677 if (curwin->w_s->b_syn_topgrp != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 return;
3679
Bram Moolenaar1966c242020-04-20 22:42:32 +02003680 if (ends_excmd2(eap->cmd, arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
3682 /*
3683 * No argument: Clear all syntax items.
3684 */
3685 if (syncing)
3686 syntax_sync_clear();
3687 else
3688 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003689 syntax_clear(curwin->w_s);
3690 if (curwin->w_s == &curwin->w_buffer->b_s)
3691 do_unlet((char_u *)"b:current_syntax", TRUE);
Bram Moolenaar1950c352010-06-06 15:21:10 +02003692 do_unlet((char_u *)"w:current_syntax", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694 }
3695 else
3696 {
3697 /*
3698 * Clear the group IDs that are in the argument.
3699 */
Bram Moolenaar1966c242020-04-20 22:42:32 +02003700 while (!ends_excmd2(eap->cmd, arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
3702 arg_end = skiptowhite(arg);
3703 if (*arg == '@')
3704 {
3705 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3706 if (id == 0)
3707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003708 semsg(_("E391: No such syntax cluster: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 break;
3710 }
3711 else
3712 {
3713 /*
3714 * We can't physically delete a cluster without changing
3715 * the IDs of other clusters, so we do the next best thing
3716 * and make it empty.
3717 */
3718 short scl_id = id - SYNID_CLUSTER;
3719
Bram Moolenaard23a8232018-02-10 18:45:26 +01003720 VIM_CLEAR(SYN_CLSTR(curwin->w_s)[scl_id].scl_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 }
3723 else
3724 {
3725 id = syn_namen2id(arg, (int)(arg_end - arg));
3726 if (id == 0)
3727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003728 semsg(_(e_nogroup), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 break;
3730 }
3731 else
3732 syn_clear_one(id, syncing);
3733 }
3734 arg = skipwhite(arg_end);
3735 }
3736 }
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00003737 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003738 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739}
3740
3741/*
3742 * Clear one syntax group for the current buffer.
3743 */
3744 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003745syn_clear_one(int id, int syncing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 synpat_T *spp;
3748 int idx;
3749
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003750 // Clear keywords only when not ":syn sync clear group-name"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (!syncing)
3752 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003753 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab);
3754 (void)syn_clear_keyword(id, &curwin->w_s->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003757 // clear the patterns for "id"
Bram Moolenaar860cae12010-06-05 23:22:07 +02003758 for (idx = curwin->w_s->b_syn_patterns.ga_len; --idx >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003760 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
3762 continue;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003763 syn_remove_pattern(curwin->w_s, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765}
3766
3767/*
3768 * Handle ":syntax on" command.
3769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003771syn_cmd_on(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 syn_cmd_onoff(eap, "syntax");
3774}
3775
3776/*
3777 * Handle ":syntax enable" command.
3778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003780syn_cmd_enable(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
3782 set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"enable");
3783 syn_cmd_onoff(eap, "syntax");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003784 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785}
3786
3787/*
3788 * Handle ":syntax reset" command.
Bram Moolenaar8bc189e2016-04-02 19:01:58 +02003789 * It actually resets highlighting, not syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003792syn_cmd_reset(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 eap->nextcmd = check_nextcmd(eap->arg);
3795 if (!eap->skip)
3796 {
3797 set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"reset");
3798 do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003799 do_unlet((char_u *)"g:syntax_cmd", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801}
3802
3803/*
3804 * Handle ":syntax manual" command.
3805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003807syn_cmd_manual(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
3809 syn_cmd_onoff(eap, "manual");
3810}
3811
3812/*
3813 * Handle ":syntax off" command.
3814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003816syn_cmd_off(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
3818 syn_cmd_onoff(eap, "nosyntax");
3819}
3820
3821 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003822syn_cmd_onoff(exarg_T *eap, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 char_u buf[100];
3825
3826 eap->nextcmd = check_nextcmd(eap->arg);
3827 if (!eap->skip)
3828 {
3829 STRCPY(buf, "so ");
Bram Moolenaar555b2802005-05-19 21:08:39 +00003830 vim_snprintf((char *)buf + 3, sizeof(buf) - 3, SYNTAX_FNAME, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 do_cmdline_cmd(buf);
3832 }
3833}
3834
3835/*
3836 * Handle ":syntax [list]" command: list current syntax words.
3837 */
3838 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003839syn_cmd_list(
3840 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003841 int syncing) // when TRUE: list syncing items
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
3843 char_u *arg = eap->arg;
3844 int id;
3845 char_u *arg_end;
3846
3847 eap->nextcmd = find_nextcmd(arg);
3848 if (eap->skip)
3849 return;
3850
Bram Moolenaar860cae12010-06-05 23:22:07 +02003851 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003853 msg(_(msg_no_items));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return;
3855 }
3856
3857 if (syncing)
3858 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003859 if (curwin->w_s->b_syn_sync_flags & SF_CCOMMENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003861 msg_puts(_("syncing on C-style comments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 syn_lines_msg();
3863 syn_match_msg();
3864 return;
3865 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003866 else if (!(curwin->w_s->b_syn_sync_flags & SF_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003868 if (curwin->w_s->b_syn_sync_minlines == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003869 msg_puts(_("no syncing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 else
3871 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003872 msg_puts(_("syncing starts "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003873 msg_outnum(curwin->w_s->b_syn_sync_minlines);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 msg_puts(_(" lines before top line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 syn_match_msg();
3876 }
3877 return;
3878 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003879 msg_puts_title(_("\n--- Syntax sync items ---"));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003880 if (curwin->w_s->b_syn_sync_minlines > 0
3881 || curwin->w_s->b_syn_sync_maxlines > 0
3882 || curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003884 msg_puts(_("\nsyncing on items"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 syn_lines_msg();
3886 syn_match_msg();
3887 }
3888 }
3889 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003890 msg_puts_title(_("\n--- Syntax items ---"));
Bram Moolenaar1966c242020-04-20 22:42:32 +02003891 if (ends_excmd2(eap->cmd, arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
3893 /*
3894 * No argument: List all group IDs and all syntax clusters.
3895 */
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02003896 for (id = 1; id <= highlight_num_groups() && !got_int; ++id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 syn_list_one(id, syncing, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003898 for (id = 0; id < curwin->w_s->b_syn_clusters.ga_len && !got_int; ++id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 syn_list_cluster(id);
3900 }
3901 else
3902 {
3903 /*
3904 * List the group IDs and syntax clusters that are in the argument.
3905 */
Bram Moolenaar1966c242020-04-20 22:42:32 +02003906 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 arg_end = skiptowhite(arg);
3909 if (*arg == '@')
3910 {
3911 id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
3912 if (id == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003913 semsg(_("E392: No such syntax cluster: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 else
3915 syn_list_cluster(id - SYNID_CLUSTER);
3916 }
3917 else
3918 {
3919 id = syn_namen2id(arg, (int)(arg_end - arg));
3920 if (id == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003921 semsg(_(e_nogroup), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 else
3923 syn_list_one(id, syncing, TRUE);
3924 }
3925 arg = skipwhite(arg_end);
3926 }
3927 }
3928 eap->nextcmd = check_nextcmd(arg);
3929}
3930
3931 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003932syn_lines_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003934 if (curwin->w_s->b_syn_sync_maxlines > 0
3935 || curwin->w_s->b_syn_sync_minlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003937 msg_puts("; ");
Bram Moolenaar860cae12010-06-05 23:22:07 +02003938 if (curwin->w_s->b_syn_sync_minlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003940 msg_puts(_("minimal "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003941 msg_outnum(curwin->w_s->b_syn_sync_minlines);
3942 if (curwin->w_s->b_syn_sync_maxlines)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003943 msg_puts(", ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003945 if (curwin->w_s->b_syn_sync_maxlines > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003947 msg_puts(_("maximal "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003948 msg_outnum(curwin->w_s->b_syn_sync_maxlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003950 msg_puts(_(" lines before top line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952}
3953
3954 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003955syn_match_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
Bram Moolenaar860cae12010-06-05 23:22:07 +02003957 if (curwin->w_s->b_syn_sync_linebreaks > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003959 msg_puts(_("; match "));
Bram Moolenaar860cae12010-06-05 23:22:07 +02003960 msg_outnum(curwin->w_s->b_syn_sync_linebreaks);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003961 msg_puts(_(" line breaks"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963}
3964
3965static int last_matchgroup;
3966
3967struct name_list
3968{
3969 int flag;
3970 char *name;
3971};
3972
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003973static void syn_list_flags(struct name_list *nl, int flags, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975/*
3976 * List one syntax item, for ":syntax" or "syntax list syntax_name".
3977 */
3978 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003979syn_list_one(
3980 int id,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003981 int syncing, // when TRUE: list syncing items
3982 int link_only) // when TRUE; list link-only too
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
3984 int attr;
3985 int idx;
3986 int did_header = FALSE;
3987 synpat_T *spp;
3988 static struct name_list namelist1[] =
3989 {
3990 {HL_DISPLAY, "display"},
3991 {HL_CONTAINED, "contained"},
3992 {HL_ONELINE, "oneline"},
3993 {HL_KEEPEND, "keepend"},
3994 {HL_EXTEND, "extend"},
3995 {HL_EXCLUDENL, "excludenl"},
3996 {HL_TRANSP, "transparent"},
3997 {HL_FOLD, "fold"},
Bram Moolenaar860cae12010-06-05 23:22:07 +02003998#ifdef FEAT_CONCEAL
3999 {HL_CONCEAL, "conceal"},
4000 {HL_CONCEALENDS, "concealends"},
4001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {0, NULL}
4003 };
4004 static struct name_list namelist2[] =
4005 {
4006 {HL_SKIPWHITE, "skipwhite"},
4007 {HL_SKIPNL, "skipnl"},
4008 {HL_SKIPEMPTY, "skipempty"},
4009 {0, NULL}
4010 };
4011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004012 attr = HL_ATTR(HLF_D); // highlight like directories
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004014 // list the keywords for "id"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (!syncing)
4016 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004017 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab, FALSE, attr);
4018 did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 did_header, attr);
4020 }
4021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004022 // list the patterns for "id"
Bram Moolenaar860cae12010-06-05 23:22:07 +02004023 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len && !got_int; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004025 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
4027 continue;
4028
4029 (void)syn_list_header(did_header, 999, id);
4030 did_header = TRUE;
4031 last_matchgroup = 0;
4032 if (spp->sp_type == SPTYPE_MATCH)
4033 {
4034 put_pattern("match", ' ', spp, attr);
4035 msg_putchar(' ');
4036 }
4037 else if (spp->sp_type == SPTYPE_START)
4038 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004039 while (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_START)
4040 put_pattern("start", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
4041 if (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_SKIP)
4042 put_pattern("skip", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
4043 while (idx < curwin->w_s->b_syn_patterns.ga_len
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004044 && SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_END)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004045 put_pattern("end", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 --idx;
4047 msg_putchar(' ');
4048 }
4049 syn_list_flags(namelist1, spp->sp_flags, attr);
4050
4051 if (spp->sp_cont_list != NULL)
4052 put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
4053
4054 if (spp->sp_syn.cont_in_list != NULL)
4055 put_id_list((char_u *)"containedin",
4056 spp->sp_syn.cont_in_list, attr);
4057
4058 if (spp->sp_next_list != NULL)
4059 {
4060 put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
4061 syn_list_flags(namelist2, spp->sp_flags, attr);
4062 }
4063 if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
4064 {
4065 if (spp->sp_flags & HL_SYNC_HERE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004066 msg_puts_attr("grouphere", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004068 msg_puts_attr("groupthere", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 msg_putchar(' ');
4070 if (spp->sp_sync_idx >= 0)
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004071 msg_outtrans(highlight_group_name(SYN_ITEMS(curwin->w_s)
4072 [spp->sp_sync_idx].sp_syn.id - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004074 msg_puts("NONE");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 msg_putchar(' ');
4076 }
4077 }
4078
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004079 // list the link, if there is one
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004080 if (highlight_link_id(id - 1) && (did_header || link_only) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
4082 (void)syn_list_header(did_header, 999, id);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004083 msg_puts_attr("links to", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 msg_putchar(' ');
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004085 msg_outtrans(highlight_group_name(highlight_link_id(id - 1) - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087}
4088
4089 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004090syn_list_flags(struct name_list *nlist, int flags, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 int i;
4093
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004094 for (i = 0; nlist[i].flag != 0; ++i)
4095 if (flags & nlist[i].flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004097 msg_puts_attr(nlist[i].name, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 msg_putchar(' ');
4099 }
4100}
4101
4102/*
4103 * List one syntax cluster, for ":syntax" or "syntax list syntax_name".
4104 */
4105 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004106syn_list_cluster(int id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107{
4108 int endcol = 15;
4109
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004110 // slight hack: roughly duplicate the guts of syn_list_header()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 msg_putchar('\n');
Bram Moolenaar860cae12010-06-05 23:22:07 +02004112 msg_outtrans(SYN_CLSTR(curwin->w_s)[id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004114 if (msg_col >= endcol) // output at least one space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 endcol = msg_col + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004116 if (Columns <= endcol) // avoid hang for tiny window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 endcol = Columns - 1;
4118
4119 msg_advance(endcol);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004120 if (SYN_CLSTR(curwin->w_s)[id].scl_list != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004122 put_id_list((char_u *)"cluster", SYN_CLSTR(curwin->w_s)[id].scl_list,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004123 HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 else
4126 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004127 msg_puts_attr("cluster", HL_ATTR(HLF_D));
4128 msg_puts("=NONE");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130}
4131
4132 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004133put_id_list(char_u *name, short *list, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 short *p;
4136
Bram Moolenaar32526b32019-01-19 17:43:09 +01004137 msg_puts_attr((char *)name, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 msg_putchar('=');
4139 for (p = list; *p; ++p)
4140 {
4141 if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
4142 {
4143 if (p[1])
Bram Moolenaar32526b32019-01-19 17:43:09 +01004144 msg_puts("ALLBUT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004146 msg_puts("ALL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
4148 else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
4149 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004150 msg_puts("TOP");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
4153 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004154 msg_puts("CONTAINED");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 else if (*p >= SYNID_CLUSTER)
4157 {
4158 short scl_id = *p - SYNID_CLUSTER;
4159
4160 msg_putchar('@');
Bram Moolenaar860cae12010-06-05 23:22:07 +02004161 msg_outtrans(SYN_CLSTR(curwin->w_s)[scl_id].scl_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 else
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004164 msg_outtrans(highlight_group_name(*p - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (p[1])
4166 msg_putchar(',');
4167 }
4168 msg_putchar(' ');
4169}
4170
4171 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004172put_pattern(
4173 char *s,
4174 int c,
4175 synpat_T *spp,
4176 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 long n;
4179 int mask;
4180 int first;
4181 static char *sepchars = "/+=-#@\"|'^&";
4182 int i;
4183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004184 // May have to write "matchgroup=group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (last_matchgroup != spp->sp_syn_match_id)
4186 {
4187 last_matchgroup = spp->sp_syn_match_id;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004188 msg_puts_attr("matchgroup", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 msg_putchar('=');
4190 if (last_matchgroup == 0)
4191 msg_outtrans((char_u *)"NONE");
4192 else
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02004193 msg_outtrans(highlight_group_name(last_matchgroup - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 msg_putchar(' ');
4195 }
4196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004197 // output the name of the pattern and an '=' or ' '
Bram Moolenaar32526b32019-01-19 17:43:09 +01004198 msg_puts_attr(s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 msg_putchar(c);
4200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004201 // output the pattern, in between a char that is not in the pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 for (i = 0; vim_strchr(spp->sp_pattern, sepchars[i]) != NULL; )
4203 if (sepchars[++i] == NUL)
4204 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004205 i = 0; // no good char found, just use the first one
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 break;
4207 }
4208 msg_putchar(sepchars[i]);
4209 msg_outtrans(spp->sp_pattern);
4210 msg_putchar(sepchars[i]);
4211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004212 // output any pattern options
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 first = TRUE;
4214 for (i = 0; i < SPO_COUNT; ++i)
4215 {
4216 mask = (1 << i);
4217 if (spp->sp_off_flags & (mask + (mask << SPO_COUNT)))
4218 {
4219 if (!first)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004220 msg_putchar(','); // separate with commas
Bram Moolenaar32526b32019-01-19 17:43:09 +01004221 msg_puts(spo_name_tab[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 n = spp->sp_offsets[i];
4223 if (i != SPO_LC_OFF)
4224 {
4225 if (spp->sp_off_flags & mask)
4226 msg_putchar('s');
4227 else
4228 msg_putchar('e');
4229 if (n > 0)
4230 msg_putchar('+');
4231 }
4232 if (n || i == SPO_LC_OFF)
4233 msg_outnum(n);
4234 first = FALSE;
4235 }
4236 }
4237 msg_putchar(' ');
4238}
4239
4240/*
4241 * List or clear the keywords for one syntax group.
4242 * Return TRUE if the header has been printed.
4243 */
4244 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004245syn_list_keywords(
4246 int id,
4247 hashtab_T *ht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004248 int did_header, // header has already been printed
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004249 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 int outlen;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004252 hashitem_T *hi;
4253 keyentry_T *kp;
4254 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 int prev_contained = 0;
4256 short *prev_next_list = NULL;
4257 short *prev_cont_in_list = NULL;
4258 int prev_skipnl = 0;
4259 int prev_skipwhite = 0;
4260 int prev_skipempty = 0;
4261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 /*
4263 * Unfortunately, this list of keywords is not sorted on alphabet but on
4264 * hash value...
4265 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004266 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004267 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004269 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004271 --todo;
4272 for (kp = HI2KE(hi); kp != NULL && !got_int; kp = kp->ke_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004274 if (kp->k_syn.id == id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004276 if (prev_contained != (kp->flags & HL_CONTAINED)
4277 || prev_skipnl != (kp->flags & HL_SKIPNL)
4278 || prev_skipwhite != (kp->flags & HL_SKIPWHITE)
4279 || prev_skipempty != (kp->flags & HL_SKIPEMPTY)
4280 || prev_cont_in_list != kp->k_syn.cont_in_list
4281 || prev_next_list != kp->next_list)
4282 outlen = 9999;
4283 else
4284 outlen = (int)STRLEN(kp->keyword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004285 // output "contained" and "nextgroup" on each line
Bram Moolenaardad6b692005-01-25 22:14:34 +00004286 if (syn_list_header(did_header, outlen, id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004288 prev_contained = 0;
4289 prev_next_list = NULL;
4290 prev_cont_in_list = NULL;
4291 prev_skipnl = 0;
4292 prev_skipwhite = 0;
4293 prev_skipempty = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004295 did_header = TRUE;
4296 if (prev_contained != (kp->flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004298 msg_puts_attr("contained", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004300 prev_contained = (kp->flags & HL_CONTAINED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004302 if (kp->k_syn.cont_in_list != prev_cont_in_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004304 put_id_list((char_u *)"containedin",
4305 kp->k_syn.cont_in_list, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 msg_putchar(' ');
Bram Moolenaardad6b692005-01-25 22:14:34 +00004307 prev_cont_in_list = kp->k_syn.cont_in_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004309 if (kp->next_list != prev_next_list)
4310 {
4311 put_id_list((char_u *)"nextgroup", kp->next_list, attr);
4312 msg_putchar(' ');
4313 prev_next_list = kp->next_list;
4314 if (kp->flags & HL_SKIPNL)
4315 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004316 msg_puts_attr("skipnl", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004317 msg_putchar(' ');
4318 prev_skipnl = (kp->flags & HL_SKIPNL);
4319 }
4320 if (kp->flags & HL_SKIPWHITE)
4321 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004322 msg_puts_attr("skipwhite", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004323 msg_putchar(' ');
4324 prev_skipwhite = (kp->flags & HL_SKIPWHITE);
4325 }
4326 if (kp->flags & HL_SKIPEMPTY)
4327 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004328 msg_puts_attr("skipempty", attr);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004329 msg_putchar(' ');
4330 prev_skipempty = (kp->flags & HL_SKIPEMPTY);
4331 }
4332 }
4333 msg_outtrans(kp->keyword);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336 }
4337 }
4338
4339 return did_header;
4340}
4341
4342 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004343syn_clear_keyword(int id, hashtab_T *ht)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004345 hashitem_T *hi;
4346 keyentry_T *kp;
4347 keyentry_T *kp_prev;
4348 keyentry_T *kp_next;
4349 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
Bram Moolenaardad6b692005-01-25 22:14:34 +00004351 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004352 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004353 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004355 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004357 --todo;
4358 kp_prev = NULL;
4359 for (kp = HI2KE(hi); kp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004361 if (kp->k_syn.id == id)
4362 {
4363 kp_next = kp->ke_next;
4364 if (kp_prev == NULL)
4365 {
4366 if (kp_next == NULL)
4367 hash_remove(ht, hi);
4368 else
4369 hi->hi_key = KE2HIKEY(kp_next);
4370 }
4371 else
4372 kp_prev->ke_next = kp_next;
4373 vim_free(kp->next_list);
4374 vim_free(kp->k_syn.cont_in_list);
4375 vim_free(kp);
4376 kp = kp_next;
4377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 else
Bram Moolenaardad6b692005-01-25 22:14:34 +00004379 {
4380 kp_prev = kp;
4381 kp = kp->ke_next;
4382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 }
4385 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004386 hash_unlock(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387}
4388
4389/*
Bram Moolenaardad6b692005-01-25 22:14:34 +00004390 * Clear a whole keyword table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 */
4392 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004393clear_keywtab(hashtab_T *ht)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004395 hashitem_T *hi;
4396 int todo;
4397 keyentry_T *kp;
4398 keyentry_T *kp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004400 todo = (int)ht->ht_used;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004401 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004403 if (!HASHITEM_EMPTY(hi))
4404 {
4405 --todo;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004406 for (kp = HI2KE(hi); kp != NULL; kp = kp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
Bram Moolenaardad6b692005-01-25 22:14:34 +00004408 kp_next = kp->ke_next;
4409 vim_free(kp->next_list);
4410 vim_free(kp->k_syn.cont_in_list);
4411 vim_free(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004415 hash_clear(ht);
4416 hash_init(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417}
4418
4419/*
4420 * Add a keyword to the list of keywords.
4421 */
4422 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004423add_keyword(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004424 char_u *name, // name of keyword
4425 int id, // group ID for this keyword
4426 int flags, // flags for this keyword
4427 short *cont_in_list, // containedin for this keyword
4428 short *next_list, // nextgroup for this keyword
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004429 int conceal_char)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
Bram Moolenaardad6b692005-01-25 22:14:34 +00004431 keyentry_T *kp;
4432 hashtab_T *ht;
4433 hashitem_T *hi;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004434 char_u *name_ic;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004435 long_u hash;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004436 char_u name_folded[MAXKEYWLEN + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
Bram Moolenaar860cae12010-06-05 23:22:07 +02004438 if (curwin->w_s->b_syn_ic)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004439 name_ic = str_foldcase(name, (int)STRLEN(name),
4440 name_folded, MAXKEYWLEN + 1);
4441 else
4442 name_ic = name;
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004443 kp = alloc(offsetof(keyentry_T, keyword) + STRLEN(name_ic) + 1);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004444 if (kp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 return;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004446 STRCPY(kp->keyword, name_ic);
Bram Moolenaardad6b692005-01-25 22:14:34 +00004447 kp->k_syn.id = id;
4448 kp->k_syn.inc_tag = current_syn_inc_tag;
4449 kp->flags = flags;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004450 kp->k_char = conceal_char;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004451 kp->k_syn.cont_in_list = copy_id_list(cont_in_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004453 curwin->w_s->b_syn_containedin = TRUE;
Bram Moolenaardad6b692005-01-25 22:14:34 +00004454 kp->next_list = copy_id_list(next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaar860cae12010-06-05 23:22:07 +02004456 if (curwin->w_s->b_syn_ic)
4457 ht = &curwin->w_s->b_keywtab_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02004459 ht = &curwin->w_s->b_keywtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaardad6b692005-01-25 22:14:34 +00004461 hash = hash_hash(kp->keyword);
4462 hi = hash_lookup(ht, kp->keyword, hash);
4463 if (HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004465 // new keyword, add to hashtable
Bram Moolenaardad6b692005-01-25 22:14:34 +00004466 kp->ke_next = NULL;
4467 hash_add_item(ht, hi, kp->keyword, hash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
Bram Moolenaardad6b692005-01-25 22:14:34 +00004469 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004471 // keyword already exists, prepend to list
Bram Moolenaardad6b692005-01-25 22:14:34 +00004472 kp->ke_next = HI2KE(hi);
4473 hi->hi_key = KE2HIKEY(kp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475}
4476
4477/*
4478 * Get the start and end of the group name argument.
4479 * Return a pointer to the first argument.
4480 * Return NULL if the end of the command was found instead of further args.
4481 */
4482 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004483get_group_name(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004484 char_u *arg, // start of the argument
4485 char_u **name_end) // pointer to end of the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487 char_u *rest;
4488
4489 *name_end = skiptowhite(arg);
4490 rest = skipwhite(*name_end);
4491
4492 /*
4493 * Check if there are enough arguments. The first argument may be a
4494 * pattern, where '|' is allowed, so only check for NUL.
4495 */
4496 if (ends_excmd(*arg) || *rest == NUL)
4497 return NULL;
4498 return rest;
4499}
4500
4501/*
4502 * Check for syntax command option arguments.
4503 * This can be called at any place in the list of arguments, and just picks
4504 * out the arguments that are known. Can be called several times in a row to
4505 * collect all options in between other arguments.
4506 * Return a pointer to the next argument (which isn't an option).
4507 * Return NULL for any error;
4508 */
4509 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004510get_syn_options(
Bram Moolenaar1966c242020-04-20 22:42:32 +02004511 char_u *start, // next argument to be checked
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004512 syn_opt_arg_T *opt, // various things
Bram Moolenaarde318c52017-01-17 16:27:10 +01004513 int *conceal_char UNUSED,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004514 int skip) // TRUE if skipping over command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
Bram Moolenaar1966c242020-04-20 22:42:32 +02004516 char_u *arg = start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 char_u *gname_start, *gname;
4518 int syn_id;
4519 int len;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004520 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 int i;
4522 int fidx;
4523 static struct flag
4524 {
4525 char *name;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004526 int argtype;
4527 int flags;
4528 } flagtab[] = { {"cCoOnNtTaAiInNeEdD", 0, HL_CONTAINED},
4529 {"oOnNeElLiInNeE", 0, HL_ONELINE},
4530 {"kKeEeEpPeEnNdD", 0, HL_KEEPEND},
4531 {"eExXtTeEnNdD", 0, HL_EXTEND},
4532 {"eExXcClLuUdDeEnNlL", 0, HL_EXCLUDENL},
4533 {"tTrRaAnNsSpPaArReEnNtT", 0, HL_TRANSP},
4534 {"sSkKiIpPnNlL", 0, HL_SKIPNL},
4535 {"sSkKiIpPwWhHiItTeE", 0, HL_SKIPWHITE},
4536 {"sSkKiIpPeEmMpPtTyY", 0, HL_SKIPEMPTY},
4537 {"gGrRoOuUpPhHeErReE", 0, HL_SYNC_HERE},
4538 {"gGrRoOuUpPtThHeErReE", 0, HL_SYNC_THERE},
4539 {"dDiIsSpPlLaAyY", 0, HL_DISPLAY},
4540 {"fFoOlLdD", 0, HL_FOLD},
Bram Moolenaar860cae12010-06-05 23:22:07 +02004541 {"cCoOnNcCeEaAlL", 0, HL_CONCEAL},
4542 {"cCoOnNcCeEaAlLeEnNdDsS", 0, HL_CONCEALENDS},
4543 {"cCcChHaArR", 11, 0},
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004544 {"cCoOnNtTaAiInNsS", 1, 0},
4545 {"cCoOnNtTaAiInNeEdDiInN", 2, 0},
4546 {"nNeExXtTgGrRoOuUpP", 3, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 };
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004548 static char *first_letters = "cCoOkKeEtTsSgGdDfFnN";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004550 if (arg == NULL) // already detected error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 return NULL;
4552
Bram Moolenaar860cae12010-06-05 23:22:07 +02004553#ifdef FEAT_CONCEAL
4554 if (curwin->w_s->b_syn_conceal)
4555 opt->flags |= HL_CONCEAL;
4556#endif
4557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 for (;;)
4559 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004560 /*
4561 * This is used very often when a large number of keywords is defined.
4562 * Need to skip quickly when no option name is found.
4563 * Also avoid tolower(), it's slow.
4564 */
4565 if (strchr(first_letters, *arg) == NULL)
4566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; )
4569 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004570 p = flagtab[fidx].name;
4571 for (i = 0, len = 0; p[i] != NUL; i += 2, ++len)
4572 if (arg[len] != p[i] && arg[len] != p[i + 1])
4573 break;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004574 if (p[i] == NUL && (VIM_ISWHITE(arg[len])
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004575 || (flagtab[fidx].argtype > 0
4576 ? arg[len] == '='
Bram Moolenaar1966c242020-04-20 22:42:32 +02004577 : ends_excmd2(start, arg + len))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004579 if (opt->keyword
4580 && (flagtab[fidx].flags == HL_DISPLAY
4581 || flagtab[fidx].flags == HL_FOLD
4582 || flagtab[fidx].flags == HL_EXTEND))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004583 // treat "display", "fold" and "extend" as a keyword
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 fidx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 break;
4586 }
4587 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004588 if (fidx < 0) // no match found
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004591 if (flagtab[fidx].argtype == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004593 if (!opt->has_cont_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004595 emsg(_("E395: contains argument not accepted here"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 return NULL;
4597 }
Bram Moolenaarde318c52017-01-17 16:27:10 +01004598 if (get_id_list(&arg, 8, &opt->cont_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 return NULL;
4600 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004601 else if (flagtab[fidx].argtype == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004603 if (get_id_list(&arg, 11, &opt->cont_in_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 return NULL;
4605 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004606 else if (flagtab[fidx].argtype == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004608 if (get_id_list(&arg, 9, &opt->next_list, skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 return NULL;
4610 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004611 else if (flagtab[fidx].argtype == 11 && arg[5] == '=')
4612 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004613 // cchar=?
Bram Moolenaar860cae12010-06-05 23:22:07 +02004614 if (has_mbyte)
4615 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004616#ifdef FEAT_CONCEAL
Bram Moolenaar860cae12010-06-05 23:22:07 +02004617 *conceal_char = mb_ptr2char(arg + 6);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004618#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004619 arg += mb_ptr2len(arg + 6) - 1;
4620 }
4621 else
Bram Moolenaar56be9502010-06-06 14:20:26 +02004622 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004623#ifdef FEAT_CONCEAL
4624 *conceal_char = arg[6];
4625#else
4626 ;
4627#endif
Bram Moolenaar56be9502010-06-06 14:20:26 +02004628 }
Bram Moolenaar4124e722011-01-22 00:58:20 +01004629#ifdef FEAT_CONCEAL
4630 if (!vim_isprintc_strict(*conceal_char))
4631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004632 emsg(_("E844: invalid cchar value"));
Bram Moolenaar4124e722011-01-22 00:58:20 +01004633 return NULL;
4634 }
4635#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004636 arg = skipwhite(arg + 7);
4637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 else
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004639 {
4640 opt->flags |= flagtab[fidx].flags;
4641 arg = skipwhite(arg + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004643 if (flagtab[fidx].flags == HL_SYNC_HERE
4644 || flagtab[fidx].flags == HL_SYNC_THERE)
4645 {
4646 if (opt->sync_idx == NULL)
4647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004648 emsg(_("E393: group[t]here not accepted here"));
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004649 return NULL;
4650 }
4651 gname_start = arg;
4652 arg = skiptowhite(arg);
4653 if (gname_start == arg)
4654 return NULL;
4655 gname = vim_strnsave(gname_start, (int)(arg - gname_start));
4656 if (gname == NULL)
4657 return NULL;
4658 if (STRCMP(gname, "NONE") == 0)
4659 *opt->sync_idx = NONE_IDX;
4660 else
4661 {
4662 syn_id = syn_name2id(gname);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004663 for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
4664 if (SYN_ITEMS(curwin->w_s)[i].sp_syn.id == syn_id
4665 && SYN_ITEMS(curwin->w_s)[i].sp_type == SPTYPE_START)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004666 {
4667 *opt->sync_idx = i;
4668 break;
4669 }
4670 if (i < 0)
4671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004672 semsg(_("E394: Didn't find region item for %s"), gname);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004673 vim_free(gname);
4674 return NULL;
4675 }
4676 }
4677
4678 vim_free(gname);
4679 arg = skipwhite(arg);
4680 }
4681#ifdef FEAT_FOLDING
4682 else if (flagtab[fidx].flags == HL_FOLD
4683 && foldmethodIsSyntax(curwin))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004684 // Need to update folds later.
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004685 foldUpdateAll(curwin);
4686#endif
4687 }
4688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 return arg;
4691}
4692
4693/*
4694 * Adjustments to syntax item when declared in a ":syn include"'d file.
4695 * Set the contained flag, and if the item is not already contained, add it
4696 * to the specified top-level group, if any.
4697 */
4698 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004699syn_incl_toplevel(int id, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004701 if ((*flagsp & HL_CONTAINED) || curwin->w_s->b_syn_topgrp == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return;
4703 *flagsp |= HL_CONTAINED;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004704 if (curwin->w_s->b_syn_topgrp >= SYNID_CLUSTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004706 // We have to alloc this, because syn_combine_list() will free it.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004707 short *grp_list = ALLOC_MULT(short, 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004708 int tlg_id = curwin->w_s->b_syn_topgrp - SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
4710 if (grp_list != NULL)
4711 {
4712 grp_list[0] = id;
4713 grp_list[1] = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004714 syn_combine_list(&SYN_CLSTR(curwin->w_s)[tlg_id].scl_list,
4715 &grp_list, CLUSTER_ADD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717 }
4718}
4719
4720/*
4721 * Handle ":syntax include [@{group-name}] filename" command.
4722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004724syn_cmd_include(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
4726 char_u *arg = eap->arg;
4727 int sgl_id = 1;
4728 char_u *group_name_end;
4729 char_u *rest;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004730 char *errormsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 int prev_toplvl_grp;
4732 int prev_syn_inc_tag;
4733 int source = FALSE;
4734
4735 eap->nextcmd = find_nextcmd(arg);
4736 if (eap->skip)
4737 return;
4738
4739 if (arg[0] == '@')
4740 {
4741 ++arg;
4742 rest = get_group_name(arg, &group_name_end);
4743 if (rest == NULL)
4744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004745 emsg(_("E397: Filename required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 return;
4747 }
4748 sgl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004749 if (sgl_id == 0)
4750 return;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004751 // separate_nextcmd() and expand_filename() depend on this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 eap->arg = rest;
4753 }
4754
4755 /*
4756 * Everything that's left, up to the next command, should be the
4757 * filename to include.
4758 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004759 eap->argt |= (EX_XFILE | EX_NOSPC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 separate_nextcmd(eap);
4761 if (*eap->arg == '<' || *eap->arg == '$' || mch_isFullName(eap->arg))
4762 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004763 // For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
4764 // file. Need to expand the file name first. In other cases
4765 // ":runtime!" is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 source = TRUE;
4767 if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL)
4768 {
4769 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004770 emsg(errormsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 return;
4772 }
4773 }
4774
4775 /*
4776 * Save and restore the existing top-level grouplist id and ":syn
4777 * include" tag around the actual inclusion.
4778 */
Bram Moolenaar42431a72011-04-01 14:44:59 +02004779 if (running_syn_inc_tag >= MAX_SYN_INC_TAG)
4780 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004781 emsg(_("E847: Too many syntax includes"));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004782 return;
4783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 prev_syn_inc_tag = current_syn_inc_tag;
4785 current_syn_inc_tag = ++running_syn_inc_tag;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004786 prev_toplvl_grp = curwin->w_s->b_syn_topgrp;
4787 curwin->w_s->b_syn_topgrp = sgl_id;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 if (source ? do_source(eap->arg, FALSE, DOSO_NONE, NULL) == FAIL
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004789 : source_runtime(eap->arg, DIP_ALL) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004790 semsg(_(e_notopen), eap->arg);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004791 curwin->w_s->b_syn_topgrp = prev_toplvl_grp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 current_syn_inc_tag = prev_syn_inc_tag;
4793}
4794
4795/*
4796 * Handle ":syntax keyword {group-name} [{option}] keyword .." command.
4797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004799syn_cmd_keyword(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
4801 char_u *arg = eap->arg;
4802 char_u *group_name_end;
4803 int syn_id;
4804 char_u *rest;
Bram Moolenaar42431a72011-04-01 14:44:59 +02004805 char_u *keyword_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 char_u *p;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004807 char_u *kw;
4808 syn_opt_arg_T syn_opt_arg;
4809 int cnt;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004810 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 rest = get_group_name(arg, &group_name_end);
4813
4814 if (rest != NULL)
4815 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004816 if (eap->skip)
4817 syn_id = -1;
4818 else
4819 syn_id = syn_check_group(arg, (int)(group_name_end - arg));
Bram Moolenaar42431a72011-04-01 14:44:59 +02004820 if (syn_id != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004821 // allocate a buffer, for removing backslashes in the keyword
Bram Moolenaar964b3742019-05-24 18:54:09 +02004822 keyword_copy = alloc(STRLEN(rest) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (keyword_copy != NULL)
4824 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004825 syn_opt_arg.flags = 0;
4826 syn_opt_arg.keyword = TRUE;
4827 syn_opt_arg.sync_idx = NULL;
4828 syn_opt_arg.has_cont_list = FALSE;
4829 syn_opt_arg.cont_in_list = NULL;
4830 syn_opt_arg.next_list = NULL;
4831
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 /*
4833 * The options given apply to ALL keywords, so all options must be
4834 * found before keywords can be created.
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004835 * 1: collect the options and copy the keywords to keyword_copy.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004837 cnt = 0;
4838 p = keyword_copy;
Bram Moolenaar1966c242020-04-20 22:42:32 +02004839 for ( ; rest != NULL && !ends_excmd2(eap->arg, rest);
4840 rest = skipwhite(rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaarde318c52017-01-17 16:27:10 +01004842 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char,
4843 eap->skip);
Bram Moolenaar1966c242020-04-20 22:42:32 +02004844 if (rest == NULL || ends_excmd2(eap->arg, rest))
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004845 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004846 // Copy the keyword, removing backslashes, and add a NUL.
Bram Moolenaar1c465442017-03-12 20:10:05 +01004847 while (*rest != NUL && !VIM_ISWHITE(*rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004849 if (*rest == '\\' && rest[1] != NUL)
4850 ++rest;
4851 *p++ = *rest++;
4852 }
4853 *p++ = NUL;
4854 ++cnt;
4855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004857 if (!eap->skip)
4858 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004859 // Adjust flags for use of ":syn include".
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004860 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
4861
4862 /*
4863 * 2: Add an entry for each keyword.
4864 */
4865 for (kw = keyword_copy; --cnt >= 0; kw += STRLEN(kw) + 1)
4866 {
4867 for (p = vim_strchr(kw, '['); ; )
4868 {
4869 if (p != NULL)
4870 *p = NUL;
4871 add_keyword(kw, syn_id, syn_opt_arg.flags,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004872 syn_opt_arg.cont_in_list,
4873 syn_opt_arg.next_list, conceal_char);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004874 if (p == NULL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004875 break;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004876 if (p[1] == NUL)
4877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004878 semsg(_("E789: Missing ']': %s"), kw);
Bram Moolenaar1560d072015-08-13 22:53:29 +02004879 goto error;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004880 }
4881 if (p[1] == ']')
4882 {
Bram Moolenaar1560d072015-08-13 22:53:29 +02004883 if (p[2] != NUL)
4884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004885 semsg(_("E890: trailing char after ']': %s]%s"),
Bram Moolenaar1560d072015-08-13 22:53:29 +02004886 kw, &p[2]);
4887 goto error;
4888 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004889 kw = p + 1; // skip over the "]"
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004890 break;
4891 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004892 if (has_mbyte)
4893 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004894 int l = (*mb_ptr2len)(p + 1);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004895
4896 mch_memmove(p, p + 1, l);
4897 p += l;
4898 }
4899 else
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004900 {
4901 p[0] = p[1];
4902 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 }
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
Bram Moolenaar1560d072015-08-13 22:53:29 +02004907error:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 vim_free(keyword_copy);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004909 vim_free(syn_opt_arg.cont_in_list);
4910 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 }
4913
4914 if (rest != NULL)
4915 eap->nextcmd = check_nextcmd(rest);
4916 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004917 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00004919 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004920 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921}
4922
4923/*
4924 * Handle ":syntax match {name} [{options}] {pattern} [{options}]".
4925 *
4926 * Also ":syntax sync match {name} [[grouphere | groupthere] {group-name}] .."
4927 */
4928 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004929syn_cmd_match(
4930 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004931 int syncing) // TRUE for ":syntax sync match .. "
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
4933 char_u *arg = eap->arg;
4934 char_u *group_name_end;
4935 char_u *rest;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004936 synpat_T item; // the item found in the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 int syn_id;
4938 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004939 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 int sync_idx = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004941 int conceal_char = NUL;
Bram Moolenaar1966c242020-04-20 22:42:32 +02004942 int orig_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004944 // Isolate the group name, check for validity
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 rest = get_group_name(arg, &group_name_end);
4946
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004947 // Get options before the pattern
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004948 syn_opt_arg.flags = 0;
4949 syn_opt_arg.keyword = FALSE;
4950 syn_opt_arg.sync_idx = syncing ? &sync_idx : NULL;
4951 syn_opt_arg.has_cont_list = TRUE;
4952 syn_opt_arg.cont_list = NULL;
4953 syn_opt_arg.cont_in_list = NULL;
4954 syn_opt_arg.next_list = NULL;
Bram Moolenaarde318c52017-01-17 16:27:10 +01004955 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004957 // get the pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 init_syn_patterns();
Bram Moolenaara80faa82020-04-12 19:37:17 +02004959 CLEAR_FIELD(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 rest = get_syn_pattern(rest, &item);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004961 if (vim_regcomp_had_eol() && !(syn_opt_arg.flags & HL_EXCLUDENL))
4962 syn_opt_arg.flags |= HL_HAS_EOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004964 // Get options after the pattern
Bram Moolenaarde318c52017-01-17 16:27:10 +01004965 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004967 if (rest != NULL) // all arguments are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
4969 /*
4970 * Check for trailing command and illegal trailing arguments.
4971 */
4972 eap->nextcmd = check_nextcmd(rest);
Bram Moolenaar1966c242020-04-20 22:42:32 +02004973 if (!ends_excmd2(eap->cmd, rest) || eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004975 else if (ga_grow(&curwin->w_s->b_syn_patterns, 1) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 && (syn_id = syn_check_group(arg,
4977 (int)(group_name_end - arg))) != 0)
4978 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004979 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 /*
4981 * Store the pattern in the syn_items list
4982 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004983 idx = curwin->w_s->b_syn_patterns.ga_len;
4984 SYN_ITEMS(curwin->w_s)[idx] = item;
4985 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
4986 SYN_ITEMS(curwin->w_s)[idx].sp_type = SPTYPE_MATCH;
4987 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
4988 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag = current_syn_inc_tag;
4989 SYN_ITEMS(curwin->w_s)[idx].sp_flags = syn_opt_arg.flags;
4990 SYN_ITEMS(curwin->w_s)[idx].sp_sync_idx = sync_idx;
4991 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list = syn_opt_arg.cont_list;
4992 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004993 syn_opt_arg.cont_in_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004994#ifdef FEAT_CONCEAL
Bram Moolenaar6e202e52010-07-28 18:14:45 +02004995 SYN_ITEMS(curwin->w_s)[idx].sp_cchar = conceal_char;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004996#endif
Bram Moolenaar6ac54292005-02-02 23:07:25 +00004997 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004998 curwin->w_s->b_syn_containedin = TRUE;
4999 SYN_ITEMS(curwin->w_s)[idx].sp_next_list = syn_opt_arg.next_list;
5000 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005002 // remember that we found a match for syncing on
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005003 if (syn_opt_arg.flags & (HL_SYNC_HERE|HL_SYNC_THERE))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005004 curwin->w_s->b_syn_sync_flags |= SF_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005006 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005007 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008#endif
5009
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005010 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005011 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
5012 return; // don't free the progs and patterns now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 }
5015
5016 /*
5017 * Something failed, free the allocated memory.
5018 */
Bram Moolenaar473de612013-06-08 18:19:48 +02005019 vim_regfree(item.sp_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 vim_free(item.sp_pattern);
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005021 vim_free(syn_opt_arg.cont_list);
5022 vim_free(syn_opt_arg.cont_in_list);
5023 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
Bram Moolenaar1966c242020-04-20 22:42:32 +02005025 if (rest == NULL && called_emsg == orig_called_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005026 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027}
5028
5029/*
5030 * Handle ":syntax region {group-name} [matchgroup={group-name}]
5031 * start {start} .. [skip {skip}] end {end} .. [{options}]".
5032 */
5033 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005034syn_cmd_region(
5035 exarg_T *eap,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005036 int syncing) // TRUE for ":syntax sync region .."
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038 char_u *arg = eap->arg;
5039 char_u *group_name_end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005040 char_u *rest; // next arg, NULL on error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 char_u *key_end;
5042 char_u *key = NULL;
5043 char_u *p;
5044 int item;
5045#define ITEM_START 0
5046#define ITEM_SKIP 1
5047#define ITEM_END 2
5048#define ITEM_MATCHGROUP 3
5049 struct pat_ptr
5050 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005051 synpat_T *pp_synp; // pointer to syn_pattern
5052 int pp_matchgroup_id; // matchgroup ID
5053 struct pat_ptr *pp_next; // pointer to next pat_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 } *(pat_ptrs[3]);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005055 // patterns found in the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 struct pat_ptr *ppp;
5057 struct pat_ptr *ppp_next;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005058 int pat_count = 0; // nr of syn_patterns found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 int syn_id;
5060 int matchgroup_id = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005061 int not_enough = FALSE; // not enough arguments
5062 int illegal = FALSE; // illegal arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 int success = FALSE;
5064 int idx;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005065 syn_opt_arg_T syn_opt_arg;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005066 int conceal_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005068 // Isolate the group name, check for validity
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 rest = get_group_name(arg, &group_name_end);
5070
5071 pat_ptrs[0] = NULL;
5072 pat_ptrs[1] = NULL;
5073 pat_ptrs[2] = NULL;
5074
5075 init_syn_patterns();
5076
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005077 syn_opt_arg.flags = 0;
5078 syn_opt_arg.keyword = FALSE;
5079 syn_opt_arg.sync_idx = NULL;
5080 syn_opt_arg.has_cont_list = TRUE;
5081 syn_opt_arg.cont_list = NULL;
5082 syn_opt_arg.cont_in_list = NULL;
5083 syn_opt_arg.next_list = NULL;
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 /*
5086 * get the options, patterns and matchgroup.
5087 */
Bram Moolenaar1966c242020-04-20 22:42:32 +02005088 while (rest != NULL && !ends_excmd2(eap->cmd, rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005090 // Check for option arguments
Bram Moolenaarde318c52017-01-17 16:27:10 +01005091 rest = get_syn_options(rest, &syn_opt_arg, &conceal_char, eap->skip);
Bram Moolenaar1966c242020-04-20 22:42:32 +02005092 if (rest == NULL || ends_excmd2(eap->cmd, rest))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 break;
5094
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005095 // must be a pattern or matchgroup then
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 key_end = rest;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005097 while (*key_end && !VIM_ISWHITE(*key_end) && *key_end != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 ++key_end;
5099 vim_free(key);
Bram Moolenaardf44a272020-06-07 20:49:05 +02005100 key = vim_strnsave_up(rest, key_end - rest);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005101 if (key == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
5103 rest = NULL;
5104 break;
5105 }
5106 if (STRCMP(key, "MATCHGROUP") == 0)
5107 item = ITEM_MATCHGROUP;
5108 else if (STRCMP(key, "START") == 0)
5109 item = ITEM_START;
5110 else if (STRCMP(key, "END") == 0)
5111 item = ITEM_END;
5112 else if (STRCMP(key, "SKIP") == 0)
5113 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005114 if (pat_ptrs[ITEM_SKIP] != NULL) // one skip pattern allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 illegal = TRUE;
5117 break;
5118 }
5119 item = ITEM_SKIP;
5120 }
5121 else
5122 break;
5123 rest = skipwhite(key_end);
5124 if (*rest != '=')
5125 {
5126 rest = NULL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005127 semsg(_("E398: Missing '=': %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129 }
5130 rest = skipwhite(rest + 1);
5131 if (*rest == NUL)
5132 {
5133 not_enough = TRUE;
5134 break;
5135 }
5136
5137 if (item == ITEM_MATCHGROUP)
5138 {
5139 p = skiptowhite(rest);
5140 if ((p - rest == 4 && STRNCMP(rest, "NONE", 4) == 0) || eap->skip)
5141 matchgroup_id = 0;
5142 else
5143 {
5144 matchgroup_id = syn_check_group(rest, (int)(p - rest));
5145 if (matchgroup_id == 0)
5146 {
5147 illegal = TRUE;
5148 break;
5149 }
5150 }
5151 rest = skipwhite(p);
5152 }
5153 else
5154 {
5155 /*
5156 * Allocate room for a syn_pattern, and link it in the list of
5157 * syn_patterns for this item, at the start (because the list is
5158 * used from end to start).
5159 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005160 ppp = ALLOC_ONE(struct pat_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (ppp == NULL)
5162 {
5163 rest = NULL;
5164 break;
5165 }
5166 ppp->pp_next = pat_ptrs[item];
5167 pat_ptrs[item] = ppp;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005168 ppp->pp_synp = ALLOC_CLEAR_ONE(synpat_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (ppp->pp_synp == NULL)
5170 {
5171 rest = NULL;
5172 break;
5173 }
5174
5175 /*
5176 * Get the syntax pattern and the following offset(s).
5177 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005178 // Enable the appropriate \z specials.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (item == ITEM_START)
5180 reg_do_extmatch = REX_SET;
5181 else if (item == ITEM_SKIP || item == ITEM_END)
5182 reg_do_extmatch = REX_USE;
5183 rest = get_syn_pattern(rest, ppp->pp_synp);
5184 reg_do_extmatch = 0;
5185 if (item == ITEM_END && vim_regcomp_had_eol()
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005186 && !(syn_opt_arg.flags & HL_EXCLUDENL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 ppp->pp_synp->sp_flags |= HL_HAS_EOL;
5188 ppp->pp_matchgroup_id = matchgroup_id;
5189 ++pat_count;
5190 }
5191 }
5192 vim_free(key);
5193 if (illegal || not_enough)
5194 rest = NULL;
5195
5196 /*
5197 * Must have a "start" and "end" pattern.
5198 */
5199 if (rest != NULL && (pat_ptrs[ITEM_START] == NULL ||
5200 pat_ptrs[ITEM_END] == NULL))
5201 {
5202 not_enough = TRUE;
5203 rest = NULL;
5204 }
5205
5206 if (rest != NULL)
5207 {
5208 /*
5209 * Check for trailing garbage or command.
5210 * If OK, add the item.
5211 */
5212 eap->nextcmd = check_nextcmd(rest);
5213 if (!ends_excmd(*rest) || eap->skip)
5214 rest = NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215 else if (ga_grow(&(curwin->w_s->b_syn_patterns), pat_count) != FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 && (syn_id = syn_check_group(arg,
5217 (int)(group_name_end - arg))) != 0)
5218 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005219 syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 /*
5221 * Store the start/skip/end in the syn_items list
5222 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005223 idx = curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 for (item = ITEM_START; item <= ITEM_END; ++item)
5225 {
5226 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp->pp_next)
5227 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005228 SYN_ITEMS(curwin->w_s)[idx] = *(ppp->pp_synp);
5229 SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing;
5230 SYN_ITEMS(curwin->w_s)[idx].sp_type =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 (item == ITEM_START) ? SPTYPE_START :
5232 (item == ITEM_SKIP) ? SPTYPE_SKIP : SPTYPE_END;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005233 SYN_ITEMS(curwin->w_s)[idx].sp_flags |= syn_opt_arg.flags;
5234 SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id;
Bram Moolenaar42431a72011-04-01 14:44:59 +02005235 SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag =
5236 current_syn_inc_tag;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005237 SYN_ITEMS(curwin->w_s)[idx].sp_syn_match_id =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 ppp->pp_matchgroup_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005239#ifdef FEAT_CONCEAL
Bram Moolenaar6e202e52010-07-28 18:14:45 +02005240 SYN_ITEMS(curwin->w_s)[idx].sp_cchar = conceal_char;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 if (item == ITEM_START)
5243 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005244 SYN_ITEMS(curwin->w_s)[idx].sp_cont_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005245 syn_opt_arg.cont_list;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005246 SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005247 syn_opt_arg.cont_in_list;
5248 if (syn_opt_arg.cont_in_list != NULL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005249 curwin->w_s->b_syn_containedin = TRUE;
5250 SYN_ITEMS(curwin->w_s)[idx].sp_next_list =
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005251 syn_opt_arg.next_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005253 ++curwin->w_s->b_syn_patterns.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ++idx;
5255#ifdef FEAT_FOLDING
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005256 if (syn_opt_arg.flags & HL_FOLD)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005257 ++curwin->w_s->b_syn_folditems;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#endif
5259 }
5260 }
5261
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005262 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005263 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
5264 success = TRUE; // don't free the progs and patterns now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 }
5267
5268 /*
5269 * Free the allocated memory.
5270 */
5271 for (item = ITEM_START; item <= ITEM_END; ++item)
5272 for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp_next)
5273 {
Bram Moolenaar4bbfb0f2019-08-31 15:28:02 +02005274 if (!success && ppp->pp_synp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
Bram Moolenaar473de612013-06-08 18:19:48 +02005276 vim_regfree(ppp->pp_synp->sp_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 vim_free(ppp->pp_synp->sp_pattern);
5278 }
5279 vim_free(ppp->pp_synp);
5280 ppp_next = ppp->pp_next;
5281 vim_free(ppp);
5282 }
5283
5284 if (!success)
5285 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00005286 vim_free(syn_opt_arg.cont_list);
5287 vim_free(syn_opt_arg.cont_in_list);
5288 vim_free(syn_opt_arg.next_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (not_enough)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005290 semsg(_("E399: Not enough arguments: syntax region %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 else if (illegal || rest == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005292 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294}
5295
5296/*
5297 * A simple syntax group ID comparison function suitable for use in qsort()
5298 */
5299 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005300syn_compare_stub(const void *v1, const void *v2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
5302 const short *s1 = v1;
5303 const short *s2 = v2;
5304
5305 return (*s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0);
5306}
5307
5308/*
5309 * Combines lists of syntax clusters.
5310 * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
5311 */
5312 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005313syn_combine_list(short **clstr1, short **clstr2, int list_op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 int count1 = 0;
5316 int count2 = 0;
5317 short *g1;
5318 short *g2;
5319 short *clstr = NULL;
5320 int count;
5321 int round;
5322
5323 /*
5324 * Handle degenerate cases.
5325 */
5326 if (*clstr2 == NULL)
5327 return;
5328 if (*clstr1 == NULL || list_op == CLUSTER_REPLACE)
5329 {
5330 if (list_op == CLUSTER_REPLACE)
5331 vim_free(*clstr1);
5332 if (list_op == CLUSTER_REPLACE || list_op == CLUSTER_ADD)
5333 *clstr1 = *clstr2;
5334 else
5335 vim_free(*clstr2);
5336 return;
5337 }
5338
5339 for (g1 = *clstr1; *g1; g1++)
5340 ++count1;
5341 for (g2 = *clstr2; *g2; g2++)
5342 ++count2;
5343
5344 /*
5345 * For speed purposes, sort both lists.
5346 */
5347 qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
5348 qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
5349
5350 /*
5351 * We proceed in two passes; in round 1, we count the elements to place
5352 * in the new list, and in round 2, we allocate and populate the new
5353 * list. For speed, we use a mergesort-like method, adding the smaller
5354 * of the current elements in each list to the new list.
5355 */
5356 for (round = 1; round <= 2; round++)
5357 {
5358 g1 = *clstr1;
5359 g2 = *clstr2;
5360 count = 0;
5361
5362 /*
5363 * First, loop through the lists until one of them is empty.
5364 */
5365 while (*g1 && *g2)
5366 {
5367 /*
5368 * We always want to add from the first list.
5369 */
5370 if (*g1 < *g2)
5371 {
5372 if (round == 2)
5373 clstr[count] = *g1;
5374 count++;
5375 g1++;
5376 continue;
5377 }
5378 /*
5379 * We only want to add from the second list if we're adding the
5380 * lists.
5381 */
5382 if (list_op == CLUSTER_ADD)
5383 {
5384 if (round == 2)
5385 clstr[count] = *g2;
5386 count++;
5387 }
5388 if (*g1 == *g2)
5389 g1++;
5390 g2++;
5391 }
5392
5393 /*
5394 * Now add the leftovers from whichever list didn't get finished
5395 * first. As before, we only want to add from the second list if
5396 * we're adding the lists.
5397 */
5398 for (; *g1; g1++, count++)
5399 if (round == 2)
5400 clstr[count] = *g1;
5401 if (list_op == CLUSTER_ADD)
5402 for (; *g2; g2++, count++)
5403 if (round == 2)
5404 clstr[count] = *g2;
5405
5406 if (round == 1)
5407 {
5408 /*
5409 * If the group ended up empty, we don't need to allocate any
5410 * space for it.
5411 */
5412 if (count == 0)
5413 {
5414 clstr = NULL;
5415 break;
5416 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005417 clstr = ALLOC_MULT(short, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 if (clstr == NULL)
5419 break;
5420 clstr[count] = 0;
5421 }
5422 }
5423
5424 /*
5425 * Finally, put the new list in place.
5426 */
5427 vim_free(*clstr1);
5428 vim_free(*clstr2);
5429 *clstr1 = clstr;
5430}
5431
5432/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005433 * Lookup a syntax cluster name and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 * If it is not found, 0 is returned.
5435 */
5436 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005437syn_scl_name2id(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439 int i;
5440 char_u *name_u;
5441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005442 // Avoid using stricmp() too much, it's slow on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 name_u = vim_strsave_up(name);
5444 if (name_u == NULL)
5445 return 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005446 for (i = curwin->w_s->b_syn_clusters.ga_len; --i >= 0; )
5447 if (SYN_CLSTR(curwin->w_s)[i].scl_name_u != NULL
5448 && STRCMP(name_u, SYN_CLSTR(curwin->w_s)[i].scl_name_u) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 break;
5450 vim_free(name_u);
5451 return (i < 0 ? 0 : i + SYNID_CLUSTER);
5452}
5453
5454/*
5455 * Like syn_scl_name2id(), but take a pointer + length argument.
5456 */
5457 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005458syn_scl_namen2id(char_u *linep, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 char_u *name;
5461 int id = 0;
5462
5463 name = vim_strnsave(linep, len);
5464 if (name != NULL)
5465 {
5466 id = syn_scl_name2id(name);
5467 vim_free(name);
5468 }
5469 return id;
5470}
5471
5472/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005473 * Find syntax cluster name in the table and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 * The argument is a pointer to the name and the length of the name.
5475 * If it doesn't exist yet, a new entry is created.
5476 * Return 0 for failure.
5477 */
5478 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005479syn_check_cluster(char_u *pp, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480{
5481 int id;
5482 char_u *name;
5483
5484 name = vim_strnsave(pp, len);
5485 if (name == NULL)
5486 return 0;
5487
5488 id = syn_scl_name2id(name);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005489 if (id == 0) // doesn't exist yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 id = syn_add_cluster(name);
5491 else
5492 vim_free(name);
5493 return id;
5494}
5495
5496/*
Bram Moolenaaraab93b12017-03-18 21:37:28 +01005497 * Add new syntax cluster and return its ID.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 * "name" must be an allocated string, it will be consumed.
5499 * Return 0 for failure.
5500 */
5501 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005502syn_add_cluster(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
Bram Moolenaar217ad922005-03-20 22:37:15 +00005504 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506 /*
5507 * First call for this growarray: init growing array.
5508 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005509 if (curwin->w_s->b_syn_clusters.ga_data == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005511 curwin->w_s->b_syn_clusters.ga_itemsize = sizeof(syn_cluster_T);
5512 curwin->w_s->b_syn_clusters.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 }
5514
Bram Moolenaar42431a72011-04-01 14:44:59 +02005515 len = curwin->w_s->b_syn_clusters.ga_len;
5516 if (len >= MAX_CLUSTER_ID)
5517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005518 emsg(_("E848: Too many syntax clusters"));
Bram Moolenaar42431a72011-04-01 14:44:59 +02005519 vim_free(name);
5520 return 0;
5521 }
5522
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 /*
5524 * Make room for at least one other cluster entry.
5525 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005526 if (ga_grow(&curwin->w_s->b_syn_clusters, 1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
5528 vim_free(name);
5529 return 0;
5530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Bram Moolenaara80faa82020-04-12 19:37:17 +02005532 CLEAR_POINTER(&(SYN_CLSTR(curwin->w_s)[len]));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005533 SYN_CLSTR(curwin->w_s)[len].scl_name = name;
5534 SYN_CLSTR(curwin->w_s)[len].scl_name_u = vim_strsave_up(name);
5535 SYN_CLSTR(curwin->w_s)[len].scl_list = NULL;
5536 ++curwin->w_s->b_syn_clusters.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaar217ad922005-03-20 22:37:15 +00005538 if (STRICMP(name, "Spell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005539 curwin->w_s->b_spell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005540 if (STRICMP(name, "NoSpell") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005541 curwin->w_s->b_nospell_cluster_id = len + SYNID_CLUSTER;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 return len + SYNID_CLUSTER;
5544}
5545
5546/*
5547 * Handle ":syntax cluster {cluster-name} [contains={groupname},..]
5548 * [add={groupname},..] [remove={groupname},..]".
5549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005551syn_cmd_cluster(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 char_u *arg = eap->arg;
5554 char_u *group_name_end;
5555 char_u *rest;
5556 int scl_id;
5557 short *clstr_list;
5558 int got_clstr = FALSE;
5559 int opt_len;
5560 int list_op;
5561
5562 eap->nextcmd = find_nextcmd(arg);
5563 if (eap->skip)
5564 return;
5565
5566 rest = get_group_name(arg, &group_name_end);
5567
5568 if (rest != NULL)
5569 {
Bram Moolenaar42431a72011-04-01 14:44:59 +02005570 scl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
5571 if (scl_id == 0)
5572 return;
5573 scl_id -= SYNID_CLUSTER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 for (;;)
5576 {
5577 if (STRNICMP(rest, "add", 3) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005578 && (VIM_ISWHITE(rest[3]) || rest[3] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 opt_len = 3;
5581 list_op = CLUSTER_ADD;
5582 }
5583 else if (STRNICMP(rest, "remove", 6) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005584 && (VIM_ISWHITE(rest[6]) || rest[6] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 opt_len = 6;
5587 list_op = CLUSTER_SUBTRACT;
5588 }
5589 else if (STRNICMP(rest, "contains", 8) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005590 && (VIM_ISWHITE(rest[8]) || rest[8] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
5592 opt_len = 8;
5593 list_op = CLUSTER_REPLACE;
5594 }
5595 else
5596 break;
5597
5598 clstr_list = NULL;
Bram Moolenaarde318c52017-01-17 16:27:10 +01005599 if (get_id_list(&rest, opt_len, &clstr_list, eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005601 semsg(_(e_invarg2), rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 break;
5603 }
Bram Moolenaarde318c52017-01-17 16:27:10 +01005604 if (scl_id >= 0)
5605 syn_combine_list(&SYN_CLSTR(curwin->w_s)[scl_id].scl_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 &clstr_list, list_op);
Bram Moolenaard7a96152017-01-22 15:28:55 +01005607 else
5608 vim_free(clstr_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 got_clstr = TRUE;
5610 }
5611
5612 if (got_clstr)
5613 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005614 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005615 syn_stack_free_all(curwin->w_s); // Need to recompute all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
5617 }
5618
5619 if (!got_clstr)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005620 emsg(_("E400: No cluster specified"));
Bram Moolenaar1966c242020-04-20 22:42:32 +02005621 if (rest == NULL || !ends_excmd2(eap->cmd, rest))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005622 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623}
5624
5625/*
5626 * On first call for current buffer: Init growing array.
5627 */
5628 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005629init_syn_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
Bram Moolenaar860cae12010-06-05 23:22:07 +02005631 curwin->w_s->b_syn_patterns.ga_itemsize = sizeof(synpat_T);
5632 curwin->w_s->b_syn_patterns.ga_growsize = 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635/*
5636 * Get one pattern for a ":syntax match" or ":syntax region" command.
5637 * Stores the pattern and program in a synpat_T.
5638 * Returns a pointer to the next argument, or NULL in case of an error.
5639 */
5640 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005641get_syn_pattern(char_u *arg, synpat_T *ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 char_u *end;
5644 int *p;
5645 int idx;
5646 char_u *cpo_save;
5647
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005648 // need at least three chars
Bram Moolenaar38219782015-08-11 15:27:13 +02005649 if (arg == NULL || arg[0] == NUL || arg[1] == NUL || arg[2] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 return NULL;
5651
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005652 end = skip_regexp(arg + 1, *arg, TRUE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005653 if (*end != *arg) // end delimiter not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005655 semsg(_("E401: Pattern delimiter not found: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return NULL;
5657 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005658 // store the pattern and compiled regexp program
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if ((ci->sp_pattern = vim_strnsave(arg + 1, (int)(end - arg - 1))) == NULL)
5660 return NULL;
5661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 // Make 'cpoptions' empty, to avoid the 'l' flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 cpo_save = p_cpo;
5664 p_cpo = (char_u *)"";
5665 ci->sp_prog = vim_regcomp(ci->sp_pattern, RE_MAGIC);
5666 p_cpo = cpo_save;
5667
5668 if (ci->sp_prog == NULL)
5669 return NULL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005670 ci->sp_ic = curwin->w_s->b_syn_ic;
Bram Moolenaarf7512552013-06-06 14:55:19 +02005671#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005672 syn_clear_time(&ci->sp_time);
5673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675 /*
5676 * Check for a match, highlight or region offset.
5677 */
5678 ++end;
5679 do
5680 {
5681 for (idx = SPO_COUNT; --idx >= 0; )
5682 if (STRNCMP(end, spo_name_tab[idx], 3) == 0)
5683 break;
5684 if (idx >= 0)
5685 {
5686 p = &(ci->sp_offsets[idx]);
5687 if (idx != SPO_LC_OFF)
5688 switch (end[3])
5689 {
5690 case 's': break;
5691 case 'b': break;
5692 case 'e': idx += SPO_COUNT; break;
5693 default: idx = -1; break;
5694 }
5695 if (idx >= 0)
5696 {
5697 ci->sp_off_flags |= (1 << idx);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005698 if (idx == SPO_LC_OFF) // lc=99
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
5700 end += 3;
5701 *p = getdigits(&end);
5702
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005703 // "lc=" offset automatically sets "ms=" offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (!(ci->sp_off_flags & (1 << SPO_MS_OFF)))
5705 {
5706 ci->sp_off_flags |= (1 << SPO_MS_OFF);
5707 ci->sp_offsets[SPO_MS_OFF] = *p;
5708 }
5709 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005710 else // yy=x+99
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
5712 end += 4;
5713 if (*end == '+')
5714 {
5715 ++end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005716 *p = getdigits(&end); // positive offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 else if (*end == '-')
5719 {
5720 ++end;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005721 *p = -getdigits(&end); // negative offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 }
5724 if (*end != ',')
5725 break;
5726 ++end;
5727 }
5728 }
5729 } while (idx >= 0);
5730
Bram Moolenaar1966c242020-04-20 22:42:32 +02005731 if (!ends_excmd2(arg, end) && !VIM_ISWHITE(*end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005733 semsg(_("E402: Garbage after pattern: %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 return NULL;
5735 }
5736 return skipwhite(end);
5737}
5738
5739/*
5740 * Handle ":syntax sync .." command.
5741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005743syn_cmd_sync(exarg_T *eap, int syncing UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
5745 char_u *arg_start = eap->arg;
5746 char_u *arg_end;
5747 char_u *key = NULL;
5748 char_u *next_arg;
5749 int illegal = FALSE;
5750 int finished = FALSE;
5751 long n;
5752 char_u *cpo_save;
5753
Bram Moolenaar1966c242020-04-20 22:42:32 +02005754 if (ends_excmd2(eap->cmd, arg_start))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
5756 syn_cmd_list(eap, TRUE);
5757 return;
5758 }
5759
Bram Moolenaar1966c242020-04-20 22:42:32 +02005760 while (!ends_excmd2(eap->cmd, arg_start))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762 arg_end = skiptowhite(arg_start);
5763 next_arg = skipwhite(arg_end);
5764 vim_free(key);
Bram Moolenaardf44a272020-06-07 20:49:05 +02005765 key = vim_strnsave_up(arg_start, arg_end - arg_start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (STRCMP(key, "CCOMMENT") == 0)
5767 {
5768 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005769 curwin->w_s->b_syn_sync_flags |= SF_CCOMMENT;
Bram Moolenaar1966c242020-04-20 22:42:32 +02005770 if (!ends_excmd2(eap->cmd, next_arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772 arg_end = skiptowhite(next_arg);
5773 if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005774 curwin->w_s->b_syn_sync_id = syn_check_group(next_arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 (int)(arg_end - next_arg));
5776 next_arg = skipwhite(arg_end);
5777 }
5778 else if (!eap->skip)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005779 curwin->w_s->b_syn_sync_id = syn_name2id((char_u *)"Comment");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781 else if ( STRNCMP(key, "LINES", 5) == 0
5782 || STRNCMP(key, "MINLINES", 8) == 0
5783 || STRNCMP(key, "MAXLINES", 8) == 0
5784 || STRNCMP(key, "LINEBREAKS", 10) == 0)
5785 {
5786 if (key[4] == 'S')
5787 arg_end = key + 6;
5788 else if (key[0] == 'L')
5789 arg_end = key + 11;
5790 else
5791 arg_end = key + 9;
5792 if (arg_end[-1] != '=' || !VIM_ISDIGIT(*arg_end))
5793 {
5794 illegal = TRUE;
5795 break;
5796 }
5797 n = getdigits(&arg_end);
5798 if (!eap->skip)
5799 {
5800 if (key[4] == 'B')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005801 curwin->w_s->b_syn_sync_linebreaks = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 else if (key[1] == 'A')
Bram Moolenaar860cae12010-06-05 23:22:07 +02005803 curwin->w_s->b_syn_sync_maxlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02005805 curwin->w_s->b_syn_sync_minlines = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807 }
5808 else if (STRCMP(key, "FROMSTART") == 0)
5809 {
5810 if (!eap->skip)
5811 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005812 curwin->w_s->b_syn_sync_minlines = MAXLNUM;
5813 curwin->w_s->b_syn_sync_maxlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
5815 }
5816 else if (STRCMP(key, "LINECONT") == 0)
5817 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005818 if (*next_arg == NUL) // missing pattern
Bram Moolenaar2795e212016-01-05 22:04:49 +01005819 {
5820 illegal = TRUE;
5821 break;
5822 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005823 if (curwin->w_s->b_syn_linecont_pat != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005825 emsg(_("E403: syntax sync: line continuations pattern specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 finished = TRUE;
5827 break;
5828 }
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005829 arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005830 if (*arg_end != *next_arg) // end delimiter not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
5832 illegal = TRUE;
5833 break;
5834 }
5835
5836 if (!eap->skip)
5837 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005838 // store the pattern and compiled regexp program
Bram Moolenaar860cae12010-06-05 23:22:07 +02005839 if ((curwin->w_s->b_syn_linecont_pat = vim_strnsave(next_arg + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 (int)(arg_end - next_arg - 1))) == NULL)
5841 {
5842 finished = TRUE;
5843 break;
5844 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005845 curwin->w_s->b_syn_linecont_ic = curwin->w_s->b_syn_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005847 // Make 'cpoptions' empty, to avoid the 'l' flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 cpo_save = p_cpo;
5849 p_cpo = (char_u *)"";
Bram Moolenaar860cae12010-06-05 23:22:07 +02005850 curwin->w_s->b_syn_linecont_prog =
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005851 vim_regcomp(curwin->w_s->b_syn_linecont_pat, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 p_cpo = cpo_save;
Bram Moolenaarf7512552013-06-06 14:55:19 +02005853#ifdef FEAT_PROFILE
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02005854 syn_clear_time(&curwin->w_s->b_syn_linecont_time);
5855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
Bram Moolenaar860cae12010-06-05 23:22:07 +02005857 if (curwin->w_s->b_syn_linecont_prog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005859 VIM_CLEAR(curwin->w_s->b_syn_linecont_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 finished = TRUE;
5861 break;
5862 }
5863 }
5864 next_arg = skipwhite(arg_end + 1);
5865 }
5866 else
5867 {
5868 eap->arg = next_arg;
5869 if (STRCMP(key, "MATCH") == 0)
5870 syn_cmd_match(eap, TRUE);
5871 else if (STRCMP(key, "REGION") == 0)
5872 syn_cmd_region(eap, TRUE);
5873 else if (STRCMP(key, "CLEAR") == 0)
5874 syn_cmd_clear(eap, TRUE);
5875 else
5876 illegal = TRUE;
5877 finished = TRUE;
5878 break;
5879 }
5880 arg_start = next_arg;
5881 }
5882 vim_free(key);
5883 if (illegal)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005884 semsg(_("E404: Illegal arguments: %s"), arg_start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 else if (!finished)
5886 {
5887 eap->nextcmd = check_nextcmd(arg_start);
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00005888 redraw_curbuf_later(SOME_VALID);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005889 syn_stack_free_all(curwin->w_s); // Need to recompute all syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891}
5892
5893/*
5894 * Convert a line of highlight group names into a list of group ID numbers.
5895 * "arg" should point to the "contains" or "nextgroup" keyword.
5896 * "arg" is advanced to after the last group name.
5897 * Careful: the argument is modified (NULs added).
5898 * returns FAIL for some error, OK for success.
5899 */
5900 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005901get_id_list(
5902 char_u **arg,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005903 int keylen, // length of keyword
5904 short **list, // where to store the resulting list, if not
5905 // NULL, the list is silently skipped!
Bram Moolenaarde318c52017-01-17 16:27:10 +01005906 int skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
5908 char_u *p = NULL;
5909 char_u *end;
5910 int round;
5911 int count;
5912 int total_count = 0;
5913 short *retval = NULL;
5914 char_u *name;
5915 regmatch_T regmatch;
5916 int id;
5917 int i;
5918 int failed = FALSE;
5919
5920 /*
5921 * We parse the list twice:
5922 * round == 1: count the number of items, allocate the array.
5923 * round == 2: fill the array with the items.
5924 * In round 1 new groups may be added, causing the number of items to
5925 * grow when a regexp is used. In that case round 1 is done once again.
5926 */
5927 for (round = 1; round <= 2; ++round)
5928 {
5929 /*
5930 * skip "contains"
5931 */
5932 p = skipwhite(*arg + keylen);
5933 if (*p != '=')
5934 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005935 semsg(_("E405: Missing equal sign: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 break;
5937 }
5938 p = skipwhite(p + 1);
Bram Moolenaar1966c242020-04-20 22:42:32 +02005939 if (ends_excmd2(*arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005941 semsg(_("E406: Empty argument: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 break;
5943 }
5944
5945 /*
5946 * parse the arguments after "contains"
5947 */
5948 count = 0;
Bram Moolenaar1966c242020-04-20 22:42:32 +02005949 while (!ends_excmd2(*arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005951 for (end = p; *end && !VIM_ISWHITE(*end) && *end != ','; ++end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 ;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005953 name = alloc(end - p + 3); // leave room for "^$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 if (name == NULL)
5955 {
5956 failed = TRUE;
5957 break;
5958 }
Bram Moolenaarce0842a2005-07-18 21:58:11 +00005959 vim_strncpy(name + 1, p, end - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if ( STRCMP(name + 1, "ALLBUT") == 0
5961 || STRCMP(name + 1, "ALL") == 0
5962 || STRCMP(name + 1, "TOP") == 0
5963 || STRCMP(name + 1, "CONTAINED") == 0)
5964 {
5965 if (TOUPPER_ASC(**arg) != 'C')
5966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005967 semsg(_("E407: %s not allowed here"), name + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 failed = TRUE;
5969 vim_free(name);
5970 break;
5971 }
5972 if (count != 0)
5973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005974 semsg(_("E408: %s must be first in contains list"),
Bram Moolenaard7a96152017-01-22 15:28:55 +01005975 name + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 failed = TRUE;
5977 vim_free(name);
5978 break;
5979 }
5980 if (name[1] == 'A')
5981 id = SYNID_ALLBUT;
5982 else if (name[1] == 'T')
5983 id = SYNID_TOP;
5984 else
5985 id = SYNID_CONTAINED;
5986 id += current_syn_inc_tag;
5987 }
5988 else if (name[1] == '@')
5989 {
Bram Moolenaareb46f8f2017-01-17 19:48:53 +01005990 if (skip)
5991 id = -1;
5992 else
Bram Moolenaarde318c52017-01-17 16:27:10 +01005993 id = syn_check_cluster(name + 2, (int)(end - p - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 }
5995 else
5996 {
5997 /*
5998 * Handle full group name.
5999 */
6000 if (vim_strpbrk(name + 1, (char_u *)"\\.*^$~[") == NULL)
6001 id = syn_check_group(name + 1, (int)(end - p));
6002 else
6003 {
6004 /*
6005 * Handle match of regexp with group names.
6006 */
6007 *name = '^';
6008 STRCAT(name, "$");
6009 regmatch.regprog = vim_regcomp(name, RE_MAGIC);
6010 if (regmatch.regprog == NULL)
6011 {
6012 failed = TRUE;
6013 vim_free(name);
6014 break;
6015 }
6016
6017 regmatch.rm_ic = TRUE;
6018 id = 0;
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02006019 for (i = highlight_num_groups(); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02006021 if (vim_regexec(&regmatch, highlight_group_name(i),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 (colnr_T)0))
6023 {
6024 if (round == 2)
6025 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006026 // Got more items than expected; can happen
6027 // when adding items that match:
6028 // "contains=a.*b,axb".
6029 // Go back to first round
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (count >= total_count)
6031 {
6032 vim_free(retval);
6033 round = 1;
6034 }
6035 else
6036 retval[count] = i + 1;
6037 }
6038 ++count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006039 id = -1; // remember that we found one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 }
6041 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006042 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 }
6044 }
6045 vim_free(name);
6046 if (id == 0)
6047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006048 semsg(_("E409: Unknown group name: %s"), p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 failed = TRUE;
6050 break;
6051 }
6052 if (id > 0)
6053 {
6054 if (round == 2)
6055 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006056 // Got more items than expected, go back to first round
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (count >= total_count)
6058 {
6059 vim_free(retval);
6060 round = 1;
6061 }
6062 else
6063 retval[count] = id;
6064 }
6065 ++count;
6066 }
6067 p = skipwhite(end);
6068 if (*p != ',')
6069 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006070 p = skipwhite(p + 1); // skip comma in between arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 }
6072 if (failed)
6073 break;
6074 if (round == 1)
6075 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006076 retval = ALLOC_MULT(short, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (retval == NULL)
6078 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006079 retval[count] = 0; // zero means end of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 total_count = count;
6081 }
6082 }
6083
6084 *arg = p;
6085 if (failed || retval == NULL)
6086 {
6087 vim_free(retval);
6088 return FAIL;
6089 }
6090
6091 if (*list == NULL)
6092 *list = retval;
6093 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006094 vim_free(retval); // list already found, don't overwrite it
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
6096 return OK;
6097}
6098
6099/*
6100 * Make a copy of an ID list.
6101 */
6102 static short *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006103copy_id_list(short *list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 int len;
6106 int count;
6107 short *retval;
6108
6109 if (list == NULL)
6110 return NULL;
6111
6112 for (count = 0; list[count]; ++count)
6113 ;
6114 len = (count + 1) * sizeof(short);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006115 retval = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (retval != NULL)
6117 mch_memmove(retval, list, (size_t)len);
6118
6119 return retval;
6120}
6121
6122/*
6123 * Check if syntax group "ssp" is in the ID list "list" of "cur_si".
6124 * "cur_si" can be NULL if not checking the "containedin" list.
6125 * Used to check if a syntax item is in the "contains" or "nextgroup" list of
6126 * the current item.
6127 * This function is called very often, keep it fast!!
6128 */
6129 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006130in_id_list(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006131 stateitem_T *cur_si, // current item or NULL
6132 short *list, // id list
6133 struct sp_syn *ssp, // group id and ":syn include" tag of group
6134 int contained) // group id is contained
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 int retval;
6137 short *scl_list;
6138 short item;
6139 short id = ssp->id;
6140 static int depth = 0;
6141 int r;
6142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006143 // If ssp has a "containedin" list and "cur_si" is in it, return TRUE.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006144 if (cur_si != NULL && ssp->cont_in_list != NULL
6145 && !(cur_si->si_flags & HL_MATCH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006147 // Ignore transparent items without a contains argument. Double check
6148 // that we don't go back past the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 while ((cur_si->si_flags & HL_TRANS_CONT)
6150 && cur_si > (stateitem_T *)(current_state.ga_data))
6151 --cur_si;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006152 // cur_si->si_idx is -1 for keywords, these never contain anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 if (cur_si->si_idx >= 0 && in_id_list(NULL, ssp->cont_in_list,
Bram Moolenaar860cae12010-06-05 23:22:07 +02006154 &(SYN_ITEMS(syn_block)[cur_si->si_idx].sp_syn),
6155 SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags & HL_CONTAINED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 return TRUE;
6157 }
6158
6159 if (list == NULL)
6160 return FALSE;
6161
6162 /*
6163 * If list is ID_LIST_ALL, we are in a transparent item that isn't
6164 * inside anything. Only allow not-contained groups.
6165 */
6166 if (list == ID_LIST_ALL)
6167 return !contained;
6168
6169 /*
6170 * If the first item is "ALLBUT", return TRUE if "id" is NOT in the
6171 * contains list. We also require that "id" is at the same ":syn include"
6172 * level as the list.
6173 */
6174 item = *list;
6175 if (item >= SYNID_ALLBUT && item < SYNID_CLUSTER)
6176 {
6177 if (item < SYNID_TOP)
6178 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006179 // ALL or ALLBUT: accept all groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (item - SYNID_ALLBUT != ssp->inc_tag)
6181 return FALSE;
6182 }
6183 else if (item < SYNID_CONTAINED)
6184 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006185 // TOP: accept all not-contained groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (item - SYNID_TOP != ssp->inc_tag || contained)
6187 return FALSE;
6188 }
6189 else
6190 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006191 // CONTAINED: accept all contained groups in the same file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (item - SYNID_CONTAINED != ssp->inc_tag || !contained)
6193 return FALSE;
6194 }
6195 item = *++list;
6196 retval = FALSE;
6197 }
6198 else
6199 retval = TRUE;
6200
6201 /*
6202 * Return "retval" if id is in the contains list.
6203 */
6204 while (item != 0)
6205 {
6206 if (item == id)
6207 return retval;
6208 if (item >= SYNID_CLUSTER)
6209 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006210 scl_list = SYN_CLSTR(syn_block)[item - SYNID_CLUSTER].scl_list;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006211 // restrict recursiveness to 30 to avoid an endless loop for a
6212 // cluster that includes itself (indirectly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if (scl_list != NULL && depth < 30)
6214 {
6215 ++depth;
6216 r = in_id_list(NULL, scl_list, ssp, contained);
6217 --depth;
6218 if (r)
6219 return retval;
6220 }
6221 }
6222 item = *++list;
6223 }
6224 return !retval;
6225}
6226
6227struct subcommand
6228{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006229 char *name; // subcommand name
6230 void (*func)(exarg_T *, int); // function to call
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231};
6232
6233static struct subcommand subcommands[] =
6234{
6235 {"case", syn_cmd_case},
6236 {"clear", syn_cmd_clear},
6237 {"cluster", syn_cmd_cluster},
Bram Moolenaar860cae12010-06-05 23:22:07 +02006238 {"conceal", syn_cmd_conceal},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {"enable", syn_cmd_enable},
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006240 {"foldlevel", syn_cmd_foldlevel},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {"include", syn_cmd_include},
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006242 {"iskeyword", syn_cmd_iskeyword},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {"keyword", syn_cmd_keyword},
6244 {"list", syn_cmd_list},
6245 {"manual", syn_cmd_manual},
6246 {"match", syn_cmd_match},
6247 {"on", syn_cmd_on},
6248 {"off", syn_cmd_off},
6249 {"region", syn_cmd_region},
6250 {"reset", syn_cmd_reset},
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006251 {"spell", syn_cmd_spell},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {"sync", syn_cmd_sync},
6253 {"", syn_cmd_list},
6254 {NULL, NULL}
6255};
6256
6257/*
6258 * ":syntax".
6259 * This searches the subcommands[] table for the subcommand name, and calls a
6260 * syntax_subcommand() function to do the rest.
6261 */
6262 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006263ex_syntax(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 char_u *arg = eap->arg;
6266 char_u *subcmd_end;
6267 char_u *subcmd_name;
6268 int i;
6269
6270 syn_cmdlinep = eap->cmdlinep;
6271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006272 // isolate subcommand name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 for (subcmd_end = arg; ASCII_ISALPHA(*subcmd_end); ++subcmd_end)
6274 ;
6275 subcmd_name = vim_strnsave(arg, (int)(subcmd_end - arg));
6276 if (subcmd_name != NULL)
6277 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006278 if (eap->skip) // skip error messages for all subcommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 ++emsg_skip;
6280 for (i = 0; ; ++i)
6281 {
6282 if (subcommands[i].name == NULL)
6283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006284 semsg(_("E410: Invalid :syntax subcommand: %s"), subcmd_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 break;
6286 }
6287 if (STRCMP(subcmd_name, (char_u *)subcommands[i].name) == 0)
6288 {
6289 eap->arg = skipwhite(subcmd_end);
6290 (subcommands[i].func)(eap, FALSE);
6291 break;
6292 }
6293 }
6294 vim_free(subcmd_name);
6295 if (eap->skip)
6296 --emsg_skip;
6297 }
6298}
6299
Bram Moolenaar860cae12010-06-05 23:22:07 +02006300 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006301ex_ownsyntax(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
Bram Moolenaar1950c352010-06-06 15:21:10 +02006303 char_u *old_value;
6304 char_u *new_value;
6305
Bram Moolenaar860cae12010-06-05 23:22:07 +02006306 if (curwin->w_s == &curwin->w_buffer->b_s)
6307 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006308 curwin->w_s = ALLOC_ONE(synblock_T);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006309 memset(curwin->w_s, 0, sizeof(synblock_T));
Bram Moolenaar670acbc2015-08-25 11:58:36 +02006310 hash_init(&curwin->w_s->b_keywtab);
6311 hash_init(&curwin->w_s->b_keywtab_ic);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006312#ifdef FEAT_SPELL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006313 // TODO: keep the spell checking as it was.
6314 curwin->w_p_spell = FALSE; // No spell checking
Bram Moolenaar860cae12010-06-05 23:22:07 +02006315 clear_string_option(&curwin->w_s->b_p_spc);
6316 clear_string_option(&curwin->w_s->b_p_spf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006317 clear_string_option(&curwin->w_s->b_p_spl);
6318#endif
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006319 clear_string_option(&curwin->w_s->b_syn_isk);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006320 }
Bram Moolenaar1950c352010-06-06 15:21:10 +02006321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006322 // save value of b:current_syntax
Bram Moolenaar1950c352010-06-06 15:21:10 +02006323 old_value = get_var_value((char_u *)"b:current_syntax");
6324 if (old_value != NULL)
6325 old_value = vim_strsave(old_value);
6326
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006327 // Apply the "syntax" autocommand event, this finds and loads the syntax
6328 // file.
Bram Moolenaar860cae12010-06-05 23:22:07 +02006329 apply_autocmds(EVENT_SYNTAX, eap->arg, curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1950c352010-06-06 15:21:10 +02006330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006331 // move value of b:current_syntax to w:current_syntax
Bram Moolenaar1950c352010-06-06 15:21:10 +02006332 new_value = get_var_value((char_u *)"b:current_syntax");
Bram Moolenaare0c6a652010-06-06 23:10:19 +02006333 if (new_value != NULL)
6334 set_internal_string_var((char_u *)"w:current_syntax", new_value);
Bram Moolenaar1950c352010-06-06 15:21:10 +02006335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006336 // restore value of b:current_syntax
Bram Moolenaare0c6a652010-06-06 23:10:19 +02006337 if (old_value == NULL)
6338 do_unlet((char_u *)"b:current_syntax", TRUE);
6339 else
Bram Moolenaar1950c352010-06-06 15:21:10 +02006340 {
6341 set_internal_string_var((char_u *)"b:current_syntax", old_value);
6342 vim_free(old_value);
6343 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006344}
6345
6346 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006347syntax_present(win_T *win)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006348{
6349 return (win->w_s->b_syn_patterns.ga_len != 0
6350 || win->w_s->b_syn_clusters.ga_len != 0
6351 || win->w_s->b_keywtab.ht_used > 0
6352 || win->w_s->b_keywtab_ic.ht_used > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353}
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356static enum
6357{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006358 EXP_SUBCMD, // expand ":syn" sub-commands
6359 EXP_CASE, // expand ":syn case" arguments
6360 EXP_SPELL, // expand ":syn spell" arguments
6361 EXP_SYNC // expand ":syn sync" arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362} expand_what;
6363
Bram Moolenaar4f688582007-07-24 12:34:30 +00006364/*
6365 * Reset include_link, include_default, include_none to 0.
6366 * Called when we are done expanding.
6367 */
6368 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006369reset_expand_highlight(void)
Bram Moolenaar4f688582007-07-24 12:34:30 +00006370{
6371 include_link = include_default = include_none = 0;
6372}
6373
6374/*
6375 * Handle command line completion for :match and :echohl command: Add "None"
6376 * as highlight group.
6377 */
6378 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006379set_context_in_echohl_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar4f688582007-07-24 12:34:30 +00006380{
6381 xp->xp_context = EXPAND_HIGHLIGHT;
6382 xp->xp_pattern = arg;
6383 include_none = 1;
6384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386/*
6387 * Handle command line completion for :syntax command.
6388 */
6389 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006390set_context_in_syntax_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
6392 char_u *p;
6393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006394 // Default: expand subcommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 xp->xp_context = EXPAND_SYNTAX;
6396 expand_what = EXP_SUBCMD;
6397 xp->xp_pattern = arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00006398 include_link = 0;
6399 include_default = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006401 // (part of) subcommand already typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (*arg != NUL)
6403 {
6404 p = skiptowhite(arg);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006405 if (*p != NUL) // past first word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 {
6407 xp->xp_pattern = skipwhite(p);
6408 if (*skiptowhite(xp->xp_pattern) != NUL)
6409 xp->xp_context = EXPAND_NOTHING;
6410 else if (STRNICMP(arg, "case", p - arg) == 0)
6411 expand_what = EXP_CASE;
Bram Moolenaar2d028392017-01-08 18:28:22 +01006412 else if (STRNICMP(arg, "spell", p - arg) == 0)
6413 expand_what = EXP_SPELL;
6414 else if (STRNICMP(arg, "sync", p - arg) == 0)
6415 expand_what = EXP_SYNC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 else if ( STRNICMP(arg, "keyword", p - arg) == 0
6417 || STRNICMP(arg, "region", p - arg) == 0
6418 || STRNICMP(arg, "match", p - arg) == 0
6419 || STRNICMP(arg, "list", p - arg) == 0)
6420 xp->xp_context = EXPAND_HIGHLIGHT;
6421 else
6422 xp->xp_context = EXPAND_NOTHING;
6423 }
6424 }
6425}
6426
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427/*
6428 * Function given to ExpandGeneric() to obtain the list syntax names for
6429 * expansion.
6430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006432get_syntax_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
Bram Moolenaar2d028392017-01-08 18:28:22 +01006434 switch (expand_what)
6435 {
6436 case EXP_SUBCMD:
6437 return (char_u *)subcommands[idx].name;
6438 case EXP_CASE:
6439 {
6440 static char *case_args[] = {"match", "ignore", NULL};
6441 return (char_u *)case_args[idx];
6442 }
6443 case EXP_SPELL:
6444 {
6445 static char *spell_args[] =
6446 {"toplevel", "notoplevel", "default", NULL};
6447 return (char_u *)spell_args[idx];
6448 }
6449 case EXP_SYNC:
6450 {
6451 static char *sync_args[] =
6452 {"ccomment", "clear", "fromstart",
6453 "linebreaks=", "linecont", "lines=", "match",
6454 "maxlines=", "minlines=", "region", NULL};
6455 return (char_u *)sync_args[idx];
6456 }
6457 }
6458 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459}
6460
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462/*
6463 * Function called for expression evaluation: get syntax ID at file position.
6464 */
6465 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006466syn_get_id(
6467 win_T *wp,
6468 long lnum,
6469 colnr_T col,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006470 int trans, // remove transparency
6471 int *spellp, // return: can do spell checking
6472 int keep_state) // keep state of char at "col"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006474 // When the position is not after the current position and in the same
6475 // line of the same buffer, need to restart parsing.
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006476 if (wp->w_buffer != syn_buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 || lnum != current_lnum
Bram Moolenaar9d0ec2e2005-04-20 19:45:58 +00006478 || col < current_col)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02006479 syntax_start(wp, lnum);
Bram Moolenaar6773a342016-01-19 20:52:44 +01006480 else if (wp->w_buffer == syn_buf
6481 && lnum == current_lnum
6482 && col > current_col)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006483 // next_match may not be correct when moving around, e.g. with the
6484 // "skip" expression in searchpair()
Bram Moolenaar6773a342016-01-19 20:52:44 +01006485 next_match_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006487 (void)get_syntax_attr(col, spellp, keep_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
6489 return (trans ? current_trans_id : current_id);
6490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
Bram Moolenaar860cae12010-06-05 23:22:07 +02006492#if defined(FEAT_CONCEAL) || defined(PROTO)
6493/*
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006494 * Get extra information about the syntax item. Must be called right after
6495 * get_syntax_attr().
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02006496 * Stores the current item sequence nr in "*seqnrp".
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006497 * Returns the current flags.
6498 */
6499 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006500get_syntax_info(int *seqnrp)
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006501{
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02006502 *seqnrp = current_seqnr;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02006503 return current_flags;
6504}
6505
6506/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02006507 * Return conceal substitution character
6508 */
6509 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006510syn_get_sub_char(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006511{
6512 return current_sub_char;
6513}
6514#endif
6515
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006516#if defined(FEAT_EVAL) || defined(PROTO)
6517/*
6518 * Return the syntax ID at position "i" in the current stack.
6519 * The caller must have called syn_get_id() before to fill the stack.
6520 * Returns -1 when "i" is out of range.
6521 */
6522 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006523syn_get_stack_item(int i)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006524{
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006525 if (i >= current_state.ga_len)
6526 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006527 // Need to invalidate the state, because we didn't properly finish it
6528 // for the last character, "keep_state" was TRUE.
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006529 invalidate_current_state();
6530 current_col = MAXCOL;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006531 return -1;
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00006532 }
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00006533 return CUR_STATE(i).si_id;
6534}
6535#endif
6536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006538 static int
6539syn_cur_foldlevel(void)
6540{
6541 int level = 0;
6542 int i;
6543
6544 for (i = 0; i < current_state.ga_len; ++i)
6545 if (CUR_STATE(i).si_flags & HL_FOLD)
6546 ++level;
6547 return level;
6548}
6549
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550/*
6551 * Function called to get folding level for line "lnum" in window "wp".
6552 */
6553 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006554syn_get_foldlevel(win_T *wp, long lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 int level = 0;
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006557 int low_level;
6558 int cur_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006560 // Return quickly when there are no fold items at all.
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02006561 if (wp->w_s->b_syn_folditems != 0
6562 && !wp->w_s->b_syn_error
6563# ifdef SYN_TIME_LIMIT
6564 && !wp->w_s->b_syn_slow
6565# endif
6566 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02006568 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
Bram Moolenaare35a52a2020-05-31 19:48:53 +02006570 // Start with the fold level at the start of the line.
6571 level = syn_cur_foldlevel();
6572
6573 if (wp->w_s->b_syn_foldlevel == SYNFLD_MINIMUM)
6574 {
6575 // Find the lowest fold level that is followed by a higher one.
6576 cur_level = level;
6577 low_level = cur_level;
6578 while (!current_finished)
6579 {
6580 (void)syn_current_attr(FALSE, FALSE, NULL, FALSE);
6581 cur_level = syn_cur_foldlevel();
6582 if (cur_level < low_level)
6583 low_level = cur_level;
6584 else if (cur_level > low_level)
6585 level = low_level;
6586 ++current_col;
6587 }
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590 if (level > wp->w_p_fdn)
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 level = wp->w_p_fdn;
Bram Moolenaar74c596b2006-11-01 11:44:31 +00006593 if (level < 0)
6594 level = 0;
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 return level;
6597}
6598#endif
6599
Bram Moolenaar01615492015-02-03 13:00:38 +01006600#if defined(FEAT_PROFILE) || defined(PROTO)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006601/*
6602 * ":syntime".
6603 */
6604 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006605ex_syntime(exarg_T *eap)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006606{
6607 if (STRCMP(eap->arg, "on") == 0)
6608 syn_time_on = TRUE;
6609 else if (STRCMP(eap->arg, "off") == 0)
6610 syn_time_on = FALSE;
6611 else if (STRCMP(eap->arg, "clear") == 0)
6612 syntime_clear();
6613 else if (STRCMP(eap->arg, "report") == 0)
6614 syntime_report();
6615 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006616 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006617}
6618
6619 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006620syn_clear_time(syn_time_T *st)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006621{
6622 profile_zero(&st->total);
6623 profile_zero(&st->slowest);
6624 st->count = 0;
6625 st->match = 0;
6626}
6627
6628/*
6629 * Clear the syntax timing for the current buffer.
6630 */
6631 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006632syntime_clear(void)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006633{
6634 int idx;
6635 synpat_T *spp;
6636
6637 if (!syntax_present(curwin))
6638 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006639 msg(_(msg_no_items));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006640 return;
6641 }
6642 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx)
6643 {
6644 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
6645 syn_clear_time(&spp->sp_time);
6646 }
6647}
6648
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006649/*
6650 * Function given to ExpandGeneric() to obtain the possible arguments of the
6651 * ":syntime {on,off,clear,report}" command.
6652 */
6653 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006654get_syntime_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006655{
6656 switch (idx)
6657 {
6658 case 0: return (char_u *)"on";
6659 case 1: return (char_u *)"off";
6660 case 2: return (char_u *)"clear";
6661 case 3: return (char_u *)"report";
6662 }
6663 return NULL;
6664}
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006665
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006666typedef struct
6667{
6668 proftime_T total;
6669 int count;
6670 int match;
6671 proftime_T slowest;
6672 proftime_T average;
6673 int id;
6674 char_u *pattern;
6675} time_entry_T;
6676
6677 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006678syn_compare_syntime(const void *v1, const void *v2)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006679{
6680 const time_entry_T *s1 = v1;
6681 const time_entry_T *s2 = v2;
6682
6683 return profile_cmp(&s1->total, &s2->total);
6684}
6685
6686/*
6687 * Clear the syntax timing for the current buffer.
6688 */
6689 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006690syntime_report(void)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006691{
6692 int idx;
6693 synpat_T *spp;
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006694# ifdef FEAT_FLOAT
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006695 proftime_T tm;
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006696# endif
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006697 int len;
6698 proftime_T total_total;
6699 int total_count = 0;
6700 garray_T ga;
6701 time_entry_T *p;
6702
6703 if (!syntax_present(curwin))
6704 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006705 msg(_(msg_no_items));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006706 return;
6707 }
6708
6709 ga_init2(&ga, sizeof(time_entry_T), 50);
6710 profile_zero(&total_total);
6711 for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx)
6712 {
6713 spp = &(SYN_ITEMS(curwin->w_s)[idx]);
6714 if (spp->sp_time.count > 0)
6715 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02006716 (void)ga_grow(&ga, 1);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006717 p = ((time_entry_T *)ga.ga_data) + ga.ga_len;
6718 p->total = spp->sp_time.total;
6719 profile_add(&total_total, &spp->sp_time.total);
6720 p->count = spp->sp_time.count;
6721 p->match = spp->sp_time.match;
6722 total_count += spp->sp_time.count;
6723 p->slowest = spp->sp_time.slowest;
6724# ifdef FEAT_FLOAT
6725 profile_divide(&spp->sp_time.total, spp->sp_time.count, &tm);
6726 p->average = tm;
6727# endif
6728 p->id = spp->sp_syn.id;
6729 p->pattern = spp->sp_pattern;
6730 ++ga.ga_len;
6731 }
6732 }
6733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006734 // Sort on total time. Skip if there are no items to avoid passing NULL
6735 // pointer to qsort().
Bram Moolenaara2162552017-01-08 17:46:20 +01006736 if (ga.ga_len > 1)
6737 qsort(ga.ga_data, (size_t)ga.ga_len, sizeof(time_entry_T),
Bram Moolenaar4e312962013-06-06 21:19:51 +02006738 syn_compare_syntime);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006739
Bram Moolenaar32526b32019-01-19 17:43:09 +01006740 msg_puts_title(_(" TOTAL COUNT MATCH SLOWEST AVERAGE NAME PATTERN"));
6741 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006742 for (idx = 0; idx < ga.ga_len && !got_int; ++idx)
6743 {
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006744 p = ((time_entry_T *)ga.ga_data) + idx;
6745
Bram Moolenaar32526b32019-01-19 17:43:09 +01006746 msg_puts(profile_msg(&p->total));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006747 msg_puts(" "); // make sure there is always a separating space
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006748 msg_advance(13);
6749 msg_outnum(p->count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006750 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006751 msg_advance(20);
6752 msg_outnum(p->match);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006753 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006754 msg_advance(26);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006755 msg_puts(profile_msg(&p->slowest));
6756 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006757 msg_advance(38);
6758# ifdef FEAT_FLOAT
Bram Moolenaar32526b32019-01-19 17:43:09 +01006759 msg_puts(profile_msg(&p->average));
6760 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006761# endif
6762 msg_advance(50);
Bram Moolenaar2ac6e822019-07-15 22:40:22 +02006763 msg_outtrans(highlight_group_name(p->id - 1));
Bram Moolenaar32526b32019-01-19 17:43:09 +01006764 msg_puts(" ");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006765
6766 msg_advance(69);
6767 if (Columns < 80)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006768 len = 20; // will wrap anyway
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006769 else
6770 len = Columns - 70;
6771 if (len > (int)STRLEN(p->pattern))
6772 len = (int)STRLEN(p->pattern);
6773 msg_outtrans_len(p->pattern, len);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006774 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006775 }
Bram Moolenaar45fc5392013-06-07 19:48:39 +02006776 ga_clear(&ga);
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006777 if (!got_int)
6778 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006779 msg_puts("\n");
6780 msg_puts(profile_msg(&total_total));
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006781 msg_advance(13);
6782 msg_outnum(total_count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006783 msg_puts("\n");
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +02006784 }
6785}
6786#endif
6787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006788#endif // FEAT_SYN_HL