Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

9552 lines (8824 sloc) 253.262 kB
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* syntax.c: code for syntax highlighting
*/
#include "vim.h"
/*
* Structure that stores information about a highlight group.
* The ID of a highlight group is also called group ID. It is the index in
* the highlight_ga array PLUS ONE.
*/
struct hl_group
{
char_u *sg_name; /* highlight group name */
char_u *sg_name_u; /* uppercase of sg_name */
/* for normal terminals */
int sg_term; /* "term=" highlighting attributes */
char_u *sg_start; /* terminal string for start highl */
char_u *sg_stop; /* terminal string for stop highl */
int sg_term_attr; /* Screen attr for term mode */
/* for color terminals */
int sg_cterm; /* "cterm=" highlighting attr */
int sg_cterm_bold; /* bold attr was set for light color */
int sg_cterm_fg; /* terminal fg color number + 1 */
int sg_cterm_bg; /* terminal bg color number + 1 */
int sg_cterm_attr; /* Screen attr for color term mode */
#ifdef FEAT_GUI
/* for when using the GUI */
guicolor_T sg_gui_fg; /* GUI foreground color handle */
guicolor_T sg_gui_bg; /* GUI background color handle */
guicolor_T sg_gui_sp; /* GUI special color handle */
GuiFont sg_font; /* GUI font handle */
#ifdef FEAT_XFONTSET
GuiFontset sg_fontset; /* GUI fontset handle */
#endif
char_u *sg_font_name; /* GUI font or fontset name */
int sg_gui_attr; /* Screen attr for GUI mode */
#endif
#if defined(FEAT_GUI) || defined(FEAT_EVAL)
/* Store the sp color name for the GUI or synIDattr() */
int sg_gui; /* "gui=" highlighting attributes */
char_u *sg_gui_fg_name;/* GUI foreground color name */
char_u *sg_gui_bg_name;/* GUI background color name */
char_u *sg_gui_sp_name;/* GUI special color name */
#endif
int sg_link; /* link to this highlight group ID */
int sg_set; /* combination of SG_* flags */
#ifdef FEAT_EVAL
scid_T sg_scriptID; /* script in which the group was last set */
#endif
};
#define SG_TERM 1 /* term has been set */
#define SG_CTERM 2 /* cterm has been set */
#define SG_GUI 4 /* gui has been set */
#define SG_LINK 8 /* link has been set */
static garray_T highlight_ga; /* highlight groups for 'highlight' option */
#define HL_TABLE() ((struct hl_group *)((highlight_ga.ga_data)))
#define MAX_HL_ID 20000 /* maximum value for a highlight ID. */
#ifdef FEAT_CMDL_COMPL
/* Flags to indicate an additional string for highlight name completion. */
static int include_none = 0; /* when 1 include "None" */
static int include_default = 0; /* when 1 include "default" */
static int include_link = 0; /* when 2 include "link" and "clear" */
#endif
/*
* The "term", "cterm" and "gui" arguments can be any combination of the
* following names, separated by commas (but no spaces!).
*/
static char *(hl_name_table[]) =
{"bold", "standout", "underline", "undercurl",
"italic", "reverse", "inverse", "NONE"};
static int hl_attr_table[] =
{HL_BOLD, HL_STANDOUT, HL_UNDERLINE, HL_UNDERCURL, HL_ITALIC, HL_INVERSE, HL_INVERSE, 0};
static int get_attr_entry __ARGS((garray_T *table, attrentry_T *aep));
static void syn_unadd_group __ARGS((void));
static void set_hl_attr __ARGS((int idx));
static void highlight_list_one __ARGS((int id));
static int highlight_list_arg __ARGS((int id, int didh, int type, int iarg, char_u *sarg, char *name));
static int syn_add_group __ARGS((char_u *name));
static int syn_list_header __ARGS((int did_header, int outlen, int id));
static int hl_has_settings __ARGS((int idx, int check_link));
static void highlight_clear __ARGS((int idx));
#ifdef FEAT_GUI
static void gui_do_one_color __ARGS((int idx, int do_menu, int do_tooltip));
static int set_group_colors __ARGS((char_u *name, guicolor_T *fgp, guicolor_T *bgp, int do_menu, int use_norm, int do_tooltip));
static guicolor_T color_name2handle __ARGS((char_u *name));
static GuiFont font_name2handle __ARGS((char_u *name));
# ifdef FEAT_XFONTSET
static GuiFontset fontset_name2handle __ARGS((char_u *name, int fixed_width));
# endif
static void hl_do_font __ARGS((int idx, char_u *arg, int do_normal, int do_menu, int do_tooltip));
#endif
/*
* An attribute number is the index in attr_table plus ATTR_OFF.
*/
#define ATTR_OFF (HL_ALL + 1)
#if defined(FEAT_SYN_HL) || defined(PROTO)
#define SYN_NAMELEN 50 /* maximum length of a syntax name */
/* different types of offsets that are possible */
#define SPO_MS_OFF 0 /* match start offset */
#define SPO_ME_OFF 1 /* match end offset */
#define SPO_HS_OFF 2 /* highl. start offset */
#define SPO_HE_OFF 3 /* highl. end offset */
#define SPO_RS_OFF 4 /* region start offset */
#define SPO_RE_OFF 5 /* region end offset */
#define SPO_LC_OFF 6 /* leading context offset */
#define SPO_COUNT 7
static char *(spo_name_tab[SPO_COUNT]) =
{"ms=", "me=", "hs=", "he=", "rs=", "re=", "lc="};
/*
* The patterns that are being searched for are stored in a syn_pattern.
* A match item consists of one pattern.
* A start/end item consists of n start patterns and m end patterns.
* A start/skip/end item consists of n start patterns, one skip pattern and m
* end patterns.
* For the latter two, the patterns are always consecutive: start-skip-end.
*
* A character offset can be given for the matched text (_m_start and _m_end)
* and for the actually highlighted text (_h_start and _h_end).
*/
typedef struct syn_pattern
{
char sp_type; /* see SPTYPE_ defines below */
char sp_syncing; /* this item used for syncing */
int sp_flags; /* see HL_ defines below */
#ifdef FEAT_CONCEAL
int sp_cchar; /* conceal substitute character */
#endif
struct sp_syn sp_syn; /* struct passed to in_id_list() */
short sp_syn_match_id; /* highlight group ID of pattern */
char_u *sp_pattern; /* regexp to match, pattern */
regprog_T *sp_prog; /* regexp to match, program */
int sp_ic; /* ignore-case flag for sp_prog */
short sp_off_flags; /* see below */
int sp_offsets[SPO_COUNT]; /* offsets */
short *sp_cont_list; /* cont. group IDs, if non-zero */
short *sp_next_list; /* next group IDs, if non-zero */
int sp_sync_idx; /* sync item index (syncing only) */
int sp_line_id; /* ID of last line where tried */
int sp_startcol; /* next match in sp_line_id line */
} synpat_T;
/* The sp_off_flags are computed like this:
* offset from the start of the matched text: (1 << SPO_XX_OFF)
* offset from the end of the matched text: (1 << (SPO_XX_OFF + SPO_COUNT))
* When both are present, only one is used.
*/
#define SPTYPE_MATCH 1 /* match keyword with this group ID */
#define SPTYPE_START 2 /* match a regexp, start of item */
#define SPTYPE_END 3 /* match a regexp, end of item */
#define SPTYPE_SKIP 4 /* match a regexp, skip within item */
#define SYN_ITEMS(buf) ((synpat_T *)((buf)->b_syn_patterns.ga_data))
#define NONE_IDX -2 /* value of sp_sync_idx for "NONE" */
/*
* Flags for b_syn_sync_flags:
*/
#define SF_CCOMMENT 0x01 /* sync on a C-style comment */
#define SF_MATCH 0x02 /* sync by matching a pattern */
#define SYN_STATE_P(ssp) ((bufstate_T *)((ssp)->ga_data))
#define MAXKEYWLEN 80 /* maximum length of a keyword */
/*
* The attributes of the syntax item that has been recognized.
*/
static int current_attr = 0; /* attr of current syntax word */
#ifdef FEAT_EVAL
static int current_id = 0; /* ID of current char for syn_get_id() */
static int current_trans_id = 0; /* idem, transparency removed */
#endif
#ifdef FEAT_CONCEAL
static int current_flags = 0;
static int current_seqnr = 0;
static int current_sub_char = 0;
#endif
typedef struct syn_cluster_S
{
char_u *scl_name; /* syntax cluster name */
char_u *scl_name_u; /* uppercase of scl_name */
short *scl_list; /* IDs in this syntax cluster */
} syn_cluster_T;
/*
* Methods of combining two clusters
*/
#define CLUSTER_REPLACE 1 /* replace first list with second */
#define CLUSTER_ADD 2 /* add second list to first */
#define CLUSTER_SUBTRACT 3 /* subtract second list from first */
#define SYN_CLSTR(buf) ((syn_cluster_T *)((buf)->b_syn_clusters.ga_data))
/*
* Syntax group IDs have different types:
* 0 - 19999 normal syntax groups
* 20000 - 20999 ALLBUT indicator (current_syn_inc_tag added)
* 21000 - 21999 TOP indicator (current_syn_inc_tag added)
* 22000 - 22999 CONTAINED indicator (current_syn_inc_tag added)
* 23000 - 32767 cluster IDs (subtract SYNID_CLUSTER for the cluster ID)
*/
#define SYNID_ALLBUT MAX_HL_ID /* syntax group ID for contains=ALLBUT */
#define SYNID_TOP 21000 /* syntax group ID for contains=TOP */
#define SYNID_CONTAINED 22000 /* syntax group ID for contains=CONTAINED */
#define SYNID_CLUSTER 23000 /* first syntax group ID for clusters */
#define MAX_SYN_INC_TAG 999 /* maximum before the above overflow */
#define MAX_CLUSTER_ID (32767 - SYNID_CLUSTER)
/*
* Annoying Hack(TM): ":syn include" needs this pointer to pass to
* expand_filename(). Most of the other syntax commands don't need it, so
* instead of passing it to them, we stow it here.
*/
static char_u **syn_cmdlinep;
/*
* Another Annoying Hack(TM): To prevent rules from other ":syn include"'d
* files from leaking into ALLBUT lists, we assign a unique ID to the
* rules in each ":syn include"'d file.
*/
static int current_syn_inc_tag = 0;
static int running_syn_inc_tag = 0;
/*
* In a hashtable item "hi_key" points to "keyword" in a keyentry.
* This avoids adding a pointer to the hashtable item.
* KE2HIKEY() converts a var pointer to a hashitem key pointer.
* HIKEY2KE() converts a hashitem key pointer to a var pointer.
* HI2KE() converts a hashitem pointer to a var pointer.
*/
static keyentry_T dumkey;
#define KE2HIKEY(kp) ((kp)->keyword)
#define HIKEY2KE(p) ((keyentry_T *)((p) - (dumkey.keyword - (char_u *)&dumkey)))
#define HI2KE(hi) HIKEY2KE((hi)->hi_key)
/*
* To reduce the time spent in keepend(), remember at which level in the state
* stack the first item with "keepend" is present. When "-1", there is no
* "keepend" on the stack.
*/
static int keepend_level = -1;
/*
* For the current state we need to remember more than just the idx.
* When si_m_endpos.lnum is 0, the items other than si_idx are unknown.
* (The end positions have the column number of the next char)
*/
typedef struct state_item
{
int si_idx; /* index of syntax pattern or
KEYWORD_IDX */
int si_id; /* highlight group ID for keywords */
int si_trans_id; /* idem, transparency removed */
int si_m_lnum; /* lnum of the match */
int si_m_startcol; /* starting column of the match */
lpos_T si_m_endpos; /* just after end posn of the match */
lpos_T si_h_startpos; /* start position of the highlighting */
lpos_T si_h_endpos; /* end position of the highlighting */
lpos_T si_eoe_pos; /* end position of end pattern */
int si_end_idx; /* group ID for end pattern or zero */
int si_ends; /* if match ends before si_m_endpos */
int si_attr; /* attributes in this state */
long si_flags; /* HL_HAS_EOL flag in this state, and
* HL_SKIP* for si_next_list */
#ifdef FEAT_CONCEAL
int si_seqnr; /* sequence number */
int si_cchar; /* substitution character for conceal */
#endif
short *si_cont_list; /* list of contained groups */
short *si_next_list; /* nextgroup IDs after this item ends */
reg_extmatch_T *si_extmatch; /* \z(...\) matches from start
* pattern */
} stateitem_T;
#define KEYWORD_IDX -1 /* value of si_idx for keywords */
#define ID_LIST_ALL (short *)-1 /* valid of si_cont_list for containing all
but contained groups */
#ifdef FEAT_CONCEAL
static int next_seqnr = 0; /* value to use for si_seqnr */
#endif
/*
* Struct to reduce the number of arguments to get_syn_options(), it's used
* very often.
*/
typedef struct
{
int flags; /* flags for contained and transparent */
int keyword; /* TRUE for ":syn keyword" */
int *sync_idx; /* syntax item for "grouphere" argument, NULL
if not allowed */
char has_cont_list; /* TRUE if "cont_list" can be used */
short *cont_list; /* group IDs for "contains" argument */
short *cont_in_list; /* group IDs for "containedin" argument */
short *next_list; /* group IDs for "nextgroup" argument */
} syn_opt_arg_T;
/*
* The next possible match in the current line for any pattern is remembered,
* to avoid having to try for a match in each column.
* If next_match_idx == -1, not tried (in this line) yet.
* If next_match_col == MAXCOL, no match found in this line.
* (All end positions have the column of the char after the end)
*/
static int next_match_col; /* column for start of next match */
static lpos_T next_match_m_endpos; /* position for end of next match */
static lpos_T next_match_h_startpos; /* pos. for highl. start of next match */
static lpos_T next_match_h_endpos; /* pos. for highl. end of next match */
static int next_match_idx; /* index of matched item */
static long next_match_flags; /* flags for next match */
static lpos_T next_match_eos_pos; /* end of start pattn (start region) */
static lpos_T next_match_eoe_pos; /* pos. for end of end pattern */
static int next_match_end_idx; /* ID of group for end pattn or zero */
static reg_extmatch_T *next_match_extmatch = NULL;
/*
* A state stack is an array of integers or stateitem_T, stored in a
* garray_T. A state stack is invalid if it's itemsize entry is zero.
*/
#define INVALID_STATE(ssp) ((ssp)->ga_itemsize == 0)
#define VALID_STATE(ssp) ((ssp)->ga_itemsize != 0)
/*
* The current state (within the line) of the recognition engine.
* When current_state.ga_itemsize is 0 the current state is invalid.
*/
static win_T *syn_win; /* current window for highlighting */
static buf_T *syn_buf; /* current buffer for highlighting */
static synblock_T *syn_block; /* current buffer for highlighting */
static linenr_T current_lnum = 0; /* lnum of current state */
static colnr_T current_col = 0; /* column of current state */
static int current_state_stored = 0; /* TRUE if stored current state
* after setting current_finished */
static int current_finished = 0; /* current line has been finished */
static garray_T current_state /* current stack of state_items */
= {0, 0, 0, 0, NULL};
static short *current_next_list = NULL; /* when non-zero, nextgroup list */
static int current_next_flags = 0; /* flags for current_next_list */
static int current_line_id = 0; /* unique number for current line */
#define CUR_STATE(idx) ((stateitem_T *)(current_state.ga_data))[idx]
static void syn_sync __ARGS((win_T *wp, linenr_T lnum, synstate_T *last_valid));
static int syn_match_linecont __ARGS((linenr_T lnum));
static void syn_start_line __ARGS((void));
static void syn_update_ends __ARGS((int startofline));
static void syn_stack_alloc __ARGS((void));
static int syn_stack_cleanup __ARGS((void));
static void syn_stack_free_entry __ARGS((synblock_T *block, synstate_T *p));
static synstate_T *syn_stack_find_entry __ARGS((linenr_T lnum));
static synstate_T *store_current_state __ARGS((void));
static void load_current_state __ARGS((synstate_T *from));
static void invalidate_current_state __ARGS((void));
static int syn_stack_equal __ARGS((synstate_T *sp));
static void validate_current_state __ARGS((void));
static int syn_finish_line __ARGS((int syncing));
static int syn_current_attr __ARGS((int syncing, int displaying, int *can_spell, int keep_state));
static int did_match_already __ARGS((int idx, garray_T *gap));
static stateitem_T *push_next_match __ARGS((stateitem_T *cur_si));
static void check_state_ends __ARGS((void));
static void update_si_attr __ARGS((int idx));
static void check_keepend __ARGS((void));
static void update_si_end __ARGS((stateitem_T *sip, int startcol, int force));
static short *copy_id_list __ARGS((short *list));
static int in_id_list __ARGS((stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained));
static int push_current_state __ARGS((int idx));
static void pop_current_state __ARGS((void));
static void syn_stack_apply_changes_block __ARGS((synblock_T *block, buf_T *buf));
static void find_endpos __ARGS((int idx, lpos_T *startpos, lpos_T *m_endpos, lpos_T *hl_endpos, long *flagsp, lpos_T *end_endpos, int *end_idx, reg_extmatch_T *start_ext));
static void clear_syn_state __ARGS((synstate_T *p));
static void clear_current_state __ARGS((void));
static void limit_pos __ARGS((lpos_T *pos, lpos_T *limit));
static void limit_pos_zero __ARGS((lpos_T *pos, lpos_T *limit));
static void syn_add_end_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
static void syn_add_start_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
static char_u *syn_getcurline __ARGS((void));
static int syn_regexec __ARGS((regmmatch_T *rmp, linenr_T lnum, colnr_T col));
static int check_keyword_id __ARGS((char_u *line, int startcol, int *endcol, long *flags, short **next_list, stateitem_T *cur_si, int *ccharp));
static void syn_cmd_case __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_spell __ARGS((exarg_T *eap, int syncing));
static void syntax_sync_clear __ARGS((void));
static void syn_remove_pattern __ARGS((synblock_T *block, int idx));
static void syn_clear_pattern __ARGS((synblock_T *block, int i));
static void syn_clear_cluster __ARGS((synblock_T *block, int i));
static void syn_cmd_clear __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_conceal __ARGS((exarg_T *eap, int syncing));
static void syn_clear_one __ARGS((int id, int syncing));
static void syn_cmd_on __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_enable __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_reset __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_manual __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_off __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_onoff __ARGS((exarg_T *eap, char *name));
static void syn_cmd_list __ARGS((exarg_T *eap, int syncing));
static void syn_lines_msg __ARGS((void));
static void syn_match_msg __ARGS((void));
static void syn_stack_free_block __ARGS((synblock_T *block));
static void syn_list_one __ARGS((int id, int syncing, int link_only));
static void syn_list_cluster __ARGS((int id));
static void put_id_list __ARGS((char_u *name, short *list, int attr));
static void put_pattern __ARGS((char *s, int c, synpat_T *spp, int attr));
static int syn_list_keywords __ARGS((int id, hashtab_T *ht, int did_header, int attr));
static void syn_clear_keyword __ARGS((int id, hashtab_T *ht));
static void clear_keywtab __ARGS((hashtab_T *ht));
static void add_keyword __ARGS((char_u *name, int id, int flags, short *cont_in_list, short *next_list, int conceal_char));
static char_u *get_group_name __ARGS((char_u *arg, char_u **name_end));
static char_u *get_syn_options __ARGS((char_u *arg, syn_opt_arg_T *opt, int *conceal_char));
static void syn_cmd_include __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_keyword __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_match __ARGS((exarg_T *eap, int syncing));
static void syn_cmd_region __ARGS((exarg_T *eap, int syncing));
#ifdef __BORLANDC__
static int _RTLENTRYF syn_compare_stub __ARGS((const void *v1, const void *v2));
#else
static int syn_compare_stub __ARGS((const void *v1, const void *v2));
#endif
static void syn_cmd_cluster __ARGS((exarg_T *eap, int syncing));
static int syn_scl_name2id __ARGS((char_u *name));
static int syn_scl_namen2id __ARGS((char_u *linep, int len));
static int syn_check_cluster __ARGS((char_u *pp, int len));
static int syn_add_cluster __ARGS((char_u *name));
static void init_syn_patterns __ARGS((void));
static char_u *get_syn_pattern __ARGS((char_u *arg, synpat_T *ci));
static void syn_cmd_sync __ARGS((exarg_T *eap, int syncing));
static int get_id_list __ARGS((char_u **arg, int keylen, short **list));
static void syn_combine_list __ARGS((short **clstr1, short **clstr2, int list_op));
static void syn_incl_toplevel __ARGS((int id, int *flagsp));
/*
* Start the syntax recognition for a line. This function is normally called
* from the screen updating, once for each displayed line.
* The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
* it. Careful: curbuf and curwin are likely to point to another buffer and
* window.
*/
void
syntax_start(wp, lnum)
win_T *wp;
linenr_T lnum;
{
synstate_T *p;
synstate_T *last_valid = NULL;
synstate_T *last_min_valid = NULL;
synstate_T *sp, *prev = NULL;
linenr_T parsed_lnum;
linenr_T first_stored;
int dist;
static int changedtick = 0; /* remember the last change ID */
#ifdef FEAT_CONCEAL
current_sub_char = NUL;
#endif
/*
* After switching buffers, invalidate current_state.
* Also do this when a change was made, the current state may be invalid
* then.
*/
if (syn_block != wp->w_s || changedtick != syn_buf->b_changedtick)
{
invalidate_current_state();
syn_buf = wp->w_buffer;
syn_block = wp->w_s;
}
changedtick = syn_buf->b_changedtick;
syn_win = wp;
/*
* Allocate syntax stack when needed.
*/
syn_stack_alloc();
if (syn_block->b_sst_array == NULL)
return; /* out of memory */
syn_block->b_sst_lasttick = display_tick;
/*
* If the state of the end of the previous line is useful, store it.
*/
if (VALID_STATE(&current_state)
&& current_lnum < lnum
&& current_lnum < syn_buf->b_ml.ml_line_count)
{
(void)syn_finish_line(FALSE);
if (!current_state_stored)
{
++current_lnum;
(void)store_current_state();
}
/*
* If the current_lnum is now the same as "lnum", keep the current
* state (this happens very often!). Otherwise invalidate
* current_state and figure it out below.
*/
if (current_lnum != lnum)
invalidate_current_state();
}
else
invalidate_current_state();
/*
* Try to synchronize from a saved state in b_sst_array[].
* Only do this if lnum is not before and not to far beyond a saved state.
*/
if (INVALID_STATE(&current_state) && syn_block->b_sst_array != NULL)
{
/* Find last valid saved state before start_lnum. */
for (p = syn_block->b_sst_first; p != NULL; p = p->sst_next)
{
if (p->sst_lnum > lnum)
break;
if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
{
last_valid = p;
if (p->sst_lnum >= lnum - syn_block->b_syn_sync_minlines)
last_min_valid = p;
}
}
if (last_min_valid != NULL)
load_current_state(last_min_valid);
}
/*
* If "lnum" is before or far beyond a line with a saved state, need to
* re-synchronize.
*/
if (INVALID_STATE(&current_state))
{
syn_sync(wp, lnum, last_valid);
if (current_lnum == 1)
/* First line is always valid, no matter "minlines". */
first_stored = 1;
else
/* Need to parse "minlines" lines before state can be considered
* valid to store. */
first_stored = current_lnum + syn_block->b_syn_sync_minlines;
}
else
first_stored = current_lnum;
/*
* Advance from the sync point or saved state until the current line.
* Save some entries for syncing with later on.
*/
if (syn_block->b_sst_len <= Rows)
dist = 999999;
else
dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
while (current_lnum < lnum)
{
syn_start_line();
(void)syn_finish_line(FALSE);
++current_lnum;
/* If we parsed at least "minlines" lines or started at a valid
* state, the current state is considered valid. */
if (current_lnum >= first_stored)
{
/* Check if the saved state entry is for the current line and is
* equal to the current state. If so, then validate all saved
* states that depended on a change before the parsed line. */
if (prev == NULL)
prev = syn_stack_find_entry(current_lnum - 1);
if (prev == NULL)
sp = syn_block->b_sst_first;
else
sp = prev;
while (sp != NULL && sp->sst_lnum < current_lnum)
sp = sp->sst_next;
if (sp != NULL
&& sp->sst_lnum == current_lnum
&& syn_stack_equal(sp))
{
parsed_lnum = current_lnum;
prev = sp;
while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
{
if (sp->sst_lnum <= lnum)
/* valid state before desired line, use this one */
prev = sp;
else if (sp->sst_change_lnum == 0)
/* past saved states depending on change, break here. */
break;
sp->sst_change_lnum = 0;
sp = sp->sst_next;
}
load_current_state(prev);
}
/* Store the state at this line when it's the first one, the line
* where we start parsing, or some distance from the previously
* saved state. But only when parsed at least 'minlines'. */
else if (prev == NULL
|| current_lnum == lnum
|| current_lnum >= prev->sst_lnum + dist)
prev = store_current_state();
}
/* This can take a long time: break when CTRL-C pressed. The current
* state will be wrong then. */
line_breakcheck();
if (got_int)
{
current_lnum = lnum;
break;
}
}
syn_start_line();
}
/*
* We cannot simply discard growarrays full of state_items or buf_states; we
* have to manually release their extmatch pointers first.
*/
static void
clear_syn_state(p)
synstate_T *p;
{
int i;
garray_T *gap;
if (p->sst_stacksize > SST_FIX_STATES)
{
gap = &(p->sst_union.sst_ga);
for (i = 0; i < gap->ga_len; i++)
unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
ga_clear(gap);
}
else
{
for (i = 0; i < p->sst_stacksize; i++)
unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
}
}
/*
* Cleanup the current_state stack.
*/
static void
clear_current_state()
{
int i;
stateitem_T *sip;
sip = (stateitem_T *)(current_state.ga_data);
for (i = 0; i < current_state.ga_len; i++)
unref_extmatch(sip[i].si_extmatch);
ga_clear(&current_state);
}
/*
* Try to find a synchronisation point for line "lnum".
*
* This sets current_lnum and the current state. One of three methods is
* used:
* 1. Search backwards for the end of a C-comment.
* 2. Search backwards for given sync patterns.
* 3. Simply start on a given number of lines above "lnum".
*/
static void
syn_sync(wp, start_lnum, last_valid)
win_T *wp;
linenr_T start_lnum;
synstate_T *last_valid;
{
buf_T *curbuf_save;
win_T *curwin_save;
pos_T cursor_save;
int idx;
linenr_T lnum;
linenr_T end_lnum;
linenr_T break_lnum;
int had_sync_point;
stateitem_T *cur_si;
synpat_T *spp;
char_u *line;
int found_flags = 0;
int found_match_idx = 0;
linenr_T found_current_lnum = 0;
int found_current_col= 0;
lpos_T found_m_endpos;
colnr_T prev_current_col;
/*
* Clear any current state that might be hanging around.
*/
invalidate_current_state();
/*
* Start at least "minlines" back. Default starting point for parsing is
* there.
* Start further back, to avoid that scrolling backwards will result in
* resyncing for every line. Now it resyncs only one out of N lines,
* where N is minlines * 1.5, or minlines * 2 if minlines is small.
* Watch out for overflow when minlines is MAXLNUM.
*/
if (syn_block->b_syn_sync_minlines > start_lnum)
start_lnum = 1;
else
{
if (syn_block->b_syn_sync_minlines == 1)
lnum = 1;
else if (syn_block->b_syn_sync_minlines < 10)
lnum = syn_block->b_syn_sync_minlines * 2;
else
lnum = syn_block->b_syn_sync_minlines * 3 / 2;
if (syn_block->b_syn_sync_maxlines != 0
&& lnum > syn_block->b_syn_sync_maxlines)
lnum = syn_block->b_syn_sync_maxlines;
if (lnum >= start_lnum)
start_lnum = 1;
else
start_lnum -= lnum;
}
current_lnum = start_lnum;
/*
* 1. Search backwards for the end of a C-style comment.
*/
if (syn_block->b_syn_sync_flags & SF_CCOMMENT)
{
/* Need to make syn_buf the current buffer for a moment, to be able to
* use find_start_comment(). */
curwin_save = curwin;
curwin = wp;
curbuf_save = curbuf;
curbuf = syn_buf;
/*
* Skip lines that end in a backslash.
*/
for ( ; start_lnum > 1; --start_lnum)
{
line = ml_get(start_lnum - 1);
if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
break;
}
current_lnum = start_lnum;
/* set cursor to start of search */
cursor_save = wp->w_cursor;
wp->w_cursor.lnum = start_lnum;
wp->w_cursor.col = 0;
/*
* If the line is inside a comment, need to find the syntax item that
* defines the comment.
* Restrict the search for the end of a comment to b_syn_sync_maxlines.
*/
if (find_start_comment((int)syn_block->b_syn_sync_maxlines) != NULL)
{
for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
if (SYN_ITEMS(syn_block)[idx].sp_syn.id
== syn_block->b_syn_sync_id
&& SYN_ITEMS(syn_block)[idx].sp_type == SPTYPE_START)
{
validate_current_state();
if (push_current_state(idx) == OK)
update_si_attr(current_state.ga_len - 1);
break;
}
}
/* restore cursor and buffer */
wp->w_cursor = cursor_save;
curwin = curwin_save;
curbuf = curbuf_save;
}
/*
* 2. Search backwards for given sync patterns.
*/
else if (syn_block->b_syn_sync_flags & SF_MATCH)
{
if (syn_block->b_syn_sync_maxlines != 0
&& start_lnum > syn_block->b_syn_sync_maxlines)
break_lnum = start_lnum - syn_block->b_syn_sync_maxlines;
else
break_lnum = 0;
found_m_endpos.lnum = 0;
found_m_endpos.col = 0;
end_lnum = start_lnum;
lnum = start_lnum;
while (--lnum > break_lnum)
{
/* This can take a long time: break when CTRL-C pressed. */
line_breakcheck();
if (got_int)
{
invalidate_current_state();
current_lnum = start_lnum;
break;
}
/* Check if we have run into a valid saved state stack now. */
if (last_valid != NULL && lnum == last_valid->sst_lnum)
{
load_current_state(last_valid);
break;
}
/*
* Check if the previous line has the line-continuation pattern.
*/
if (lnum > 1 && syn_match_linecont(lnum - 1))
continue;
/*
* Start with nothing on the state stack
*/
validate_current_state();
for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
{
syn_start_line();
for (;;)
{
had_sync_point = syn_finish_line(TRUE);
/*
* When a sync point has been found, remember where, and
* continue to look for another one, further on in the line.
*/
if (had_sync_point && current_state.ga_len)
{
cur_si = &CUR_STATE(current_state.ga_len - 1);
if (cur_si->si_m_endpos.lnum > start_lnum)
{
/* ignore match that goes to after where started */
current_lnum = end_lnum;
break;
}
if (cur_si->si_idx < 0)
{
/* Cannot happen? */
found_flags = 0;
found_match_idx = KEYWORD_IDX;
}
else
{
spp = &(SYN_ITEMS(syn_block)[cur_si->si_idx]);
found_flags = spp->sp_flags;
found_match_idx = spp->sp_sync_idx;
}
found_current_lnum = current_lnum;
found_current_col = current_col;
found_m_endpos = cur_si->si_m_endpos;
/*
* Continue after the match (be aware of a zero-length
* match).
*/
if (found_m_endpos.lnum > current_lnum)
{
current_lnum = found_m_endpos.lnum;
current_col = found_m_endpos.col;
if (current_lnum >= end_lnum)
break;
}
else if (found_m_endpos.col > current_col)
current_col = found_m_endpos.col;
else
++current_col;
/* syn_current_attr() will have skipped the check for
* an item that ends here, need to do that now. Be
* careful not to go past the NUL. */
prev_current_col = current_col;
if (syn_getcurline()[current_col] != NUL)
++current_col;
check_state_ends();
current_col = prev_current_col;
}
else
break;
}
}
/*
* If a sync point was encountered, break here.
*/
if (found_flags)
{
/*
* Put the item that was specified by the sync point on the
* state stack. If there was no item specified, make the
* state stack empty.
*/
clear_current_state();
if (found_match_idx >= 0
&& push_current_state(found_match_idx) == OK)
update_si_attr(current_state.ga_len - 1);
/*
* When using "grouphere", continue from the sync point
* match, until the end of the line. Parsing starts at
* the next line.
* For "groupthere" the parsing starts at start_lnum.
*/
if (found_flags & HL_SYNC_HERE)
{
if (current_state.ga_len)
{
cur_si = &CUR_STATE(current_state.ga_len - 1);
cur_si->si_h_startpos.lnum = found_current_lnum;
cur_si->si_h_startpos.col = found_current_col;
update_si_end(cur_si, (int)current_col, TRUE);
check_keepend();
}
current_col = found_m_endpos.col;
current_lnum = found_m_endpos.lnum;
(void)syn_finish_line(FALSE);
++current_lnum;
}
else
current_lnum = start_lnum;
break;
}
end_lnum = lnum;
invalidate_current_state();
}
/* Ran into start of the file or exceeded maximum number of lines */
if (lnum <= break_lnum)
{
invalidate_current_state();
current_lnum = break_lnum + 1;
}
}
validate_current_state();
}
/*
* Return TRUE if the line-continuation pattern matches in line "lnum".
*/
static int
syn_match_linecont(lnum)
linenr_T lnum;
{
regmmatch_T regmatch;
if (syn_block->b_syn_linecont_prog != NULL)
{
regmatch.rmm_ic = syn_block->b_syn_linecont_ic;
regmatch.regprog = syn_block->b_syn_linecont_prog;
return syn_regexec(&regmatch, lnum, (colnr_T)0);
}
return FALSE;
}
/*
* Prepare the current state for the start of a line.
*/
static void
syn_start_line()
{
current_finished = FALSE;
current_col = 0;
/*
* Need to update the end of a start/skip/end that continues from the
* previous line and regions that have "keepend".
*/
if (current_state.ga_len > 0)
{
syn_update_ends(TRUE);
check_state_ends();
}
next_match_idx = -1;
++current_line_id;
}
/*
* Check for items in the stack that need their end updated.
* When "startofline" is TRUE the last item is always updated.
* When "startofline" is FALSE the item with "keepend" is forcefully updated.
*/
static void
syn_update_ends(startofline)
int startofline;
{
stateitem_T *cur_si;
int i;
int seen_keepend;
if (startofline)
{
/* Check for a match carried over from a previous line with a
* contained region. The match ends as soon as the region ends. */
for (i = 0; i < current_state.ga_len; ++i)
{
cur_si = &CUR_STATE(i);
if (cur_si->si_idx >= 0
&& (SYN_ITEMS(syn_block)[cur_si->si_idx]).sp_type
== SPTYPE_MATCH
&& cur_si->si_m_endpos.lnum < current_lnum)
{
cur_si->si_flags |= HL_MATCHCONT;
cur_si->si_m_endpos.lnum = 0;
cur_si->si_m_endpos.col = 0;
cur_si->si_h_endpos = cur_si->si_m_endpos;
cur_si->si_ends = TRUE;
}
}
}
/*
* Need to update the end of a start/skip/end that continues from the
* previous line. And regions that have "keepend", because they may
* influence contained items. If we've just removed "extend"
* (startofline == 0) then we should update ends of normal regions
* contained inside "keepend" because "extend" could have extended
* these "keepend" regions as well as contained normal regions.
* Then check for items ending in column 0.
*/
i = current_state.ga_len - 1;
if (keepend_level >= 0)
for ( ; i > keepend_level; --i)
if (CUR_STATE(i).si_flags & HL_EXTEND)
break;
seen_keepend = FALSE;
for ( ; i < current_state.ga_len; ++i)
{
cur_si = &CUR_STATE(i);
if ((cur_si->si_flags & HL_KEEPEND)
|| (seen_keepend && !startofline)
|| (i == current_state.ga_len - 1 && startofline))
{
cur_si->si_h_startpos.col = 0; /* start highl. in col 0 */
cur_si->si_h_startpos.lnum = current_lnum;
if (!(cur_si->si_flags & HL_MATCHCONT))
update_si_end(cur_si, (int)current_col, !startofline);
if (!startofline && (cur_si->si_flags & HL_KEEPEND))
seen_keepend = TRUE;
}
}
check_keepend();
}
/****************************************
* Handling of the state stack cache.
*/
/*
* EXPLANATION OF THE SYNTAX STATE STACK CACHE
*
* To speed up syntax highlighting, the state stack for the start of some
* lines is cached. These entries can be used to start parsing at that point.
*
* The stack is kept in b_sst_array[] for each buffer. There is a list of
* valid entries. b_sst_first points to the first one, then follow sst_next.
* The entries are sorted on line number. The first entry is often for line 2
* (line 1 always starts with an empty stack).
* There is also a list for free entries. This construction is used to avoid
* having to allocate and free memory blocks too often.
*
* When making changes to the buffer, this is logged in b_mod_*. When calling
* update_screen() to update the display, it will call
* syn_stack_apply_changes() for each displayed buffer to adjust the cached
* entries. The entries which are inside the changed area are removed,
* because they must be recomputed. Entries below the changed have their line
* number adjusted for deleted/inserted lines, and have their sst_change_lnum
* set to indicate that a check must be made if the changed lines would change
* the cached entry.
*
* When later displaying lines, an entry is stored for each line. Displayed
* lines are likely to be displayed again, in which case the state at the
* start of the line is needed.
* For not displayed lines, an entry is stored for every so many lines. These
* entries will be used e.g., when scrolling backwards. The distance between
* entries depends on the number of lines in the buffer. For small buffers
* the distance is fixed at SST_DIST, for large buffers there is a fixed
* number of entries SST_MAX_ENTRIES, and the distance is computed.
*/
static void
syn_stack_free_block(block)
synblock_T *block;
{
synstate_T *p;
if (block->b_sst_array != NULL)
{
for (p = block->b_sst_first; p != NULL; p = p->sst_next)
clear_syn_state(p);
vim_free(block->b_sst_array);
block->b_sst_array = NULL;
block->b_sst_len = 0;
}
}
/*
* Free b_sst_array[] for buffer "buf".
* Used when syntax items changed to force resyncing everywhere.
*/
void
syn_stack_free_all(block)
synblock_T *block;
{
win_T *wp;
syn_stack_free_block(block);
#ifdef FEAT_FOLDING
/* When using "syntax" fold method, must update all folds. */
FOR_ALL_WINDOWS(wp)
{
if (wp->w_s == block && foldmethodIsSyntax(wp))
foldUpdateAll(wp);
}
#endif
}
/*
* Allocate the syntax state stack for syn_buf when needed.
* If the number of entries in b_sst_array[] is much too big or a bit too
* small, reallocate it.
* Also used to allocate b_sst_array[] for the first time.
*/
static void
syn_stack_alloc()
{
long len;
synstate_T *to, *from;
synstate_T *sstp;
len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
if (len < SST_MIN_ENTRIES)
len = SST_MIN_ENTRIES;
else if (len > SST_MAX_ENTRIES)
len = SST_MAX_ENTRIES;
if (syn_block->b_sst_len > len * 2 || syn_block->b_sst_len < len)
{
/* Allocate 50% too much, to avoid reallocating too often. */
len = syn_buf->b_ml.ml_line_count;
len = (len + len / 2) / SST_DIST + Rows * 2;
if (len < SST_MIN_ENTRIES)
len = SST_MIN_ENTRIES;
else if (len > SST_MAX_ENTRIES)
len = SST_MAX_ENTRIES;
if (syn_block->b_sst_array != NULL)
{
/* When shrinking the array, cleanup the existing stack.
* Make sure that all valid entries fit in the new array. */
while (syn_block->b_sst_len - syn_block->b_sst_freecount + 2 > len
&& syn_stack_cleanup())
;
if (len < syn_block->b_sst_len - syn_block->b_sst_freecount + 2)
len = syn_block->b_sst_len - syn_block->b_sst_freecount + 2;
}
sstp = (synstate_T *)alloc_clear((unsigned)(len * sizeof(synstate_T)));
if (sstp == NULL) /* out of memory! */
return;
to = sstp - 1;
if (syn_block->b_sst_array != NULL)
{
/* Move the states from the old array to the new one. */
for (from = syn_block->b_sst_first; from != NULL;
from = from->sst_next)
{
++to;
*to = *from;
to->sst_next = to + 1;
}
}
if (to != sstp - 1)
{
to->sst_next = NULL;
syn_block->b_sst_first = sstp;
syn_block->b_sst_freecount = len - (int)(to - sstp) - 1;
}
else
{
syn_block->b_sst_first = NULL;
syn_block->b_sst_freecount = len;
}
/* Create the list of free entries. */
syn_block->b_sst_firstfree = to + 1;
while (++to < sstp + len)
to->sst_next = to + 1;
(sstp + len - 1)->sst_next = NULL;
vim_free(syn_block->b_sst_array);
syn_block->b_sst_array = sstp;
syn_block->b_sst_len = len;
}
}
/*
* Check for changes in a buffer to affect stored syntax states. Uses the
* b_mod_* fields.
* Called from update_screen(), before screen is being updated, once for each
* displayed buffer.
*/
void
syn_stack_apply_changes(buf)
buf_T *buf;
{
win_T *wp;
syn_stack_apply_changes_block(&buf->b_s, buf);
FOR_ALL_WINDOWS(wp)
{
if ((wp->w_buffer == buf) && (wp->w_s != &buf->b_s))
syn_stack_apply_changes_block(wp->w_s, buf);
}
}
static void
syn_stack_apply_changes_block(block, buf)
synblock_T *block;
buf_T *buf;
{
synstate_T *p, *prev, *np;
linenr_T n;
if (block->b_sst_array == NULL) /* nothing to do */
return;
prev = NULL;
for (p = block->b_sst_first; p != NULL; )
{
if (p->sst_lnum + block->b_syn_sync_linebreaks > buf->b_mod_top)
{
n = p->sst_lnum + buf->b_mod_xlines;
if (n <= buf->b_mod_bot)
{
/* this state is inside the changed area, remove it */
np = p->sst_next;
if (prev == NULL)
block->b_sst_first = np;
else
prev->sst_next = np;
syn_stack_free_entry(block, p);
p = np;
continue;
}
/* This state is below the changed area. Remember the line
* that needs to be parsed before this entry can be made valid
* again. */
if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
{
if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
p->sst_change_lnum += buf->b_mod_xlines;
else
p->sst_change_lnum = buf->b_mod_top;
}
if (p->sst_change_lnum == 0
|| p->sst_change_lnum < buf->b_mod_bot)
p->sst_change_lnum = buf->b_mod_bot;
p->sst_lnum = n;
}
prev = p;
p = p->sst_next;
}
}
/*
* Reduce the number of entries in the state stack for syn_buf.
* Returns TRUE if at least one entry was freed.
*/
static int
syn_stack_cleanup()
{
synstate_T *p, *prev;
disptick_T tick;
int above;
int dist;
int retval = FALSE;
if (syn_block->b_sst_array == NULL || syn_block->b_sst_first == NULL)
return retval;
/* Compute normal distance between non-displayed entries. */
if (syn_block->b_sst_len <= Rows)
dist = 999999;
else
dist = syn_buf->b_ml.ml_line_count / (syn_block->b_sst_len - Rows) + 1;
/*
* Go through the list to find the "tick" for the oldest entry that can
* be removed. Set "above" when the "tick" for the oldest entry is above
* "b_sst_lasttick" (the display tick wraps around).
*/
tick = syn_block->b_sst_lasttick;
above = FALSE;
prev = syn_block->b_sst_first;
for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
{
if (prev->sst_lnum + dist > p->sst_lnum)
{
if (p->sst_tick > syn_block->b_sst_lasttick)
{
if (!above || p->sst_tick < tick)
tick = p->sst_tick;
above = TRUE;
}
else if (!above && p->sst_tick < tick)
tick = p->sst_tick;
}
}
/*
* Go through the list to make the entries for the oldest tick at an
* interval of several lines.
*/
prev = syn_block->b_sst_first;
for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
{
if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
{
/* Move this entry from used list to free list */
prev->sst_next = p->sst_next;
syn_stack_free_entry(syn_block, p);
p = prev;
retval = TRUE;
}
}
return retval;
}
/*
* Free the allocated memory for a syn_state item.
* Move the entry into the free list.
*/
static void
syn_stack_free_entry(block, p)
synblock_T *block;
synstate_T *p;
{
clear_syn_state(p);
p->sst_next = block->b_sst_firstfree;
block->b_sst_firstfree = p;
++block->b_sst_freecount;
}
/*
* Find an entry in the list of state stacks at or before "lnum".
* Returns NULL when there is no entry or the first entry is after "lnum".
*/
static synstate_T *
syn_stack_find_entry(lnum)
linenr_T lnum;
{
synstate_T *p, *prev;
prev = NULL;
for (p = syn_block->b_sst_first; p != NULL; prev = p, p = p->sst_next)
{
if (p->sst_lnum == lnum)
return p;
if (p->sst_lnum > lnum)
break;
}
return prev;
}
/*
* Try saving the current state in b_sst_array[].
* The current state must be valid for the start of the current_lnum line!
*/
static synstate_T *
store_current_state()
{
int i;
synstate_T *p;
bufstate_T *bp;
stateitem_T *cur_si;
synstate_T *sp = syn_stack_find_entry(current_lnum);
/*
* If the current state contains a start or end pattern that continues
* from the previous line, we can't use it. Don't store it then.
*/
for (i = current_state.ga_len - 1; i >= 0; --i)
{
cur_si = &CUR_STATE(i);
if (cur_si->si_h_startpos.lnum >= current_lnum
|| cur_si->si_m_endpos.lnum >= current_lnum
|| cur_si->si_h_endpos.lnum >= current_lnum
|| (cur_si->si_end_idx
&& cur_si->si_eoe_pos.lnum >= current_lnum))
break;
}
if (i >= 0)
{
if (sp != NULL)
{
/* find "sp" in the list and remove it */
if (syn_block->b_sst_first == sp)
/* it's the first entry */
syn_block->b_sst_first = sp->sst_next;
else
{
/* find the entry just before this one to adjust sst_next */
for (p = syn_block->b_sst_first; p != NULL; p = p->sst_next)
if (p->sst_next == sp)
break;
if (p != NULL) /* just in case */
p->sst_next = sp->sst_next;
}
syn_stack_free_entry(syn_block, sp);
sp = NULL;
}
}
else if (sp == NULL || sp->sst_lnum != current_lnum)
{
/*
* Add a new entry
*/
/* If no free items, cleanup the array first. */
if (syn_block->b_sst_freecount == 0)
{
(void)syn_stack_cleanup();
/* "sp" may have been moved to the freelist now */
sp = syn_stack_find_entry(current_lnum);
}
/* Still no free items? Must be a strange problem... */
if (syn_block->b_sst_freecount == 0)
sp = NULL;
else
{
/* Take the first item from the free list and put it in the used
* list, after *sp */
p = syn_block->b_sst_firstfree;
syn_block->b_sst_firstfree = p->sst_next;
--syn_block->b_sst_freecount;
if (sp == NULL)
{
/* Insert in front of the list */
p->sst_next = syn_block->b_sst_first;
syn_block->b_sst_first = p;
}
else
{
/* insert in list after *sp */
p->sst_next = sp->sst_next;
sp->sst_next = p;
}
sp = p;
sp->sst_stacksize = 0;
sp->sst_lnum = current_lnum;
}
}
if (sp != NULL)
{
/* When overwriting an existing state stack, clear it first */
clear_syn_state(sp);
sp->sst_stacksize = current_state.ga_len;
if (current_state.ga_len > SST_FIX_STATES)
{
/* Need to clear it, might be something remaining from when the
* length was less than SST_FIX_STATES. */
ga_init2(&sp->sst_union.sst_ga, (int)sizeof(bufstate_T), 1);
if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
sp->sst_stacksize = 0;
else
sp->sst_union.sst_ga.ga_len = current_state.ga_len;
bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
}
else
bp = sp->sst_union.sst_stack;
for (i = 0; i < sp->sst_stacksize; ++i)
{
bp[i].bs_idx = CUR_STATE(i).si_idx;
bp[i].bs_flags = CUR_STATE(i).si_flags;
#ifdef FEAT_CONCEAL
bp[i].bs_seqnr = CUR_STATE(i).si_seqnr;
bp[i].bs_cchar = CUR_STATE(i).si_cchar;
#endif
bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
}
sp->sst_next_flags = current_next_flags;
sp->sst_next_list = current_next_list;
sp->sst_tick = display_tick;
sp->sst_change_lnum = 0;
}
current_state_stored = TRUE;
return sp;
}
/*
* Copy a state stack from "from" in b_sst_array[] to current_state;
*/
static void
load_current_state(from)
synstate_T *from;
{
int i;
bufstate_T *bp;
clear_current_state();
validate_current_state();
keepend_level = -1;
if (from->sst_stacksize
&& ga_grow(&current_state, from->sst_stacksize) != FAIL)
{
if (from->sst_stacksize > SST_FIX_STATES)
bp = SYN_STATE_P(&(from->sst_union.sst_ga));
else
bp = from->sst_union.sst_stack;
for (i = 0; i < from->sst_stacksize; ++i)
{
CUR_STATE(i).si_idx = bp[i].bs_idx;
CUR_STATE(i).si_flags = bp[i].bs_flags;
#ifdef FEAT_CONCEAL
CUR_STATE(i).si_seqnr = bp[i].bs_seqnr;
CUR_STATE(i).si_cchar = bp[i].bs_cchar;
#endif
CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
keepend_level = i;
CUR_STATE(i).si_ends = FALSE;
CUR_STATE(i).si_m_lnum = 0;
if (CUR_STATE(i).si_idx >= 0)
CUR_STATE(i).si_next_list =
(SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_next_list;
else
CUR_STATE(i).si_next_list = NULL;
update_si_attr(i);
}
current_state.ga_len = from->sst_stacksize;
}
current_next_list = from->sst_next_list;
current_next_flags = from->sst_next_flags;
current_lnum = from->sst_lnum;
}
/*
* Compare saved state stack "*sp" with the current state.
* Return TRUE when they are equal.
*/
static int
syn_stack_equal(sp)
synstate_T *sp;
{
int i, j;
bufstate_T *bp;
reg_extmatch_T *six, *bsx;
/* First a quick check if the stacks have the same size end nextlist. */
if (sp->sst_stacksize == current_state.ga_len
&& sp->sst_next_list == current_next_list)
{
/* Need to compare all states on both stacks. */
if (sp->sst_stacksize > SST_FIX_STATES)
bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
else
bp = sp->sst_union.sst_stack;
for (i = current_state.ga_len; --i >= 0; )
{
/* If the item has another index the state is different. */
if (bp[i].bs_idx != CUR_STATE(i).si_idx)
break;
if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
{
/* When the extmatch pointers are different, the strings in
* them can still be the same. Check if the extmatch
* references are equal. */
bsx = bp[i].bs_extmatch;
six = CUR_STATE(i).si_extmatch;
/* If one of the extmatch pointers is NULL the states are
* different. */
if (bsx == NULL || six == NULL)
break;
for (j = 0; j < NSUBEXP; ++j)
{
/* Check each referenced match string. They must all be
* equal. */
if (bsx->matches[j] != six->matches[j])
{
/* If the pointer is different it can still be the
* same text. Compare the strings, ignore case when
* the start item has the sp_ic flag set. */
if (bsx->matches[j] == NULL
|| six->matches[j] == NULL)
break;
if ((SYN_ITEMS(syn_block)[CUR_STATE(i).si_idx]).sp_ic
? MB_STRICMP(bsx->matches[j],
six->matches[j]) != 0
: STRCMP(bsx->matches[j], six->matches[j]) != 0)
break;
}
}
if (j != NSUBEXP)
break;
}
}
if (i < 0)
return TRUE;
}
return FALSE;
}
/*
* We stop parsing syntax above line "lnum". If the stored state at or below
* this line depended on a change before it, it now depends on the line below
* the last parsed line.
* The window looks like this:
* line which changed
* displayed line
* displayed line
* lnum -> line below window
*/
void
syntax_end_parsing(lnum)
linenr_T lnum;
{
synstate_T *sp;
sp = syn_stack_find_entry(lnum);
if (sp != NULL && sp->sst_lnum < lnum)
sp = sp->sst_next;
if (sp != NULL && sp->sst_change_lnum != 0)
sp->sst_change_lnum = lnum;
}
/*
* End of handling of the state stack.
****************************************/
static void
invalidate_current_state()
{
clear_current_state();
current_state.ga_itemsize = 0; /* mark current_state invalid */
current_next_list = NULL;
keepend_level = -1;
}
static void
validate_current_state()
{
current_state.ga_itemsize = sizeof(stateitem_T);
current_state.ga_growsize = 3;
}
/*
* Return TRUE if the syntax at start of lnum changed since last time.
* This will only be called just after get_syntax_attr() for the previous
* line, to check if the next line needs to be redrawn too.
*/
int
syntax_check_changed(lnum)
linenr_T lnum;
{
int retval = TRUE;
synstate_T *sp;
/*
* Check the state stack when:
* - lnum is just below the previously syntaxed line.
* - lnum is not before the lines with saved states.
* - lnum is not past the lines with saved states.
* - lnum is at or before the last changed line.
*/
if (VALID_STATE(&current_state) && lnum == current_lnum + 1)
{
sp = syn_stack_find_entry(lnum);
if (sp != NULL && sp->sst_lnum == lnum)
{
/*
* finish the previous line (needed when not all of the line was
* drawn)
*/
(void)syn_finish_line(FALSE);
/*
* Compare the current state with the previously saved state of
* the line.
*/
if (syn_stack_equal(sp))
retval = FALSE;
/*
* Store the current state in b_sst_array[] for later use.
*/
++current_lnum;
(void)store_current_state();
}
}
return retval;
}
/*
* Finish the current line.
* This doesn't return any attributes, it only gets the state at the end of
* the line. It can start anywhere in the line, as long as the current state
* is valid.
*/
static int
syn_finish_line(syncing)
int syncing; /* called for syncing */
{
stateitem_T *cur_si;
colnr_T prev_current_col;
if (!current_finished)
{
while (!current_finished)
{
(void)syn_current_attr(syncing, FALSE, NULL, FALSE);
/*
* When syncing, and found some item, need to check the item.
*/
if (syncing && current_state.ga_len)
{
/*
* Check for match with sync item.
*/
cur_si = &CUR_STATE(current_state.ga_len - 1);
if (cur_si->si_idx >= 0
&& (SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags
& (HL_SYNC_HERE|HL_SYNC_THERE)))
return TRUE;
/* syn_current_attr() will have skipped the check for an item
* that ends here, need to do that now. Be careful not to go
* past the NUL. */
prev_current_col = current_col;
if (syn_getcurline()[current_col] != NUL)
++current_col;
check_state_ends();
current_col = prev_current_col;
}
++current_col;
}
}
return FALSE;
}
/*
* Return highlight attributes for next character.
* Must first call syntax_start() once for the line.
* "col" is normally 0 for the first use in a line, and increments by one each
* time. It's allowed to skip characters and to stop before the end of the
* line. But only a "col" after a previously used column is allowed.
* When "can_spell" is not NULL set it to TRUE when spell-checking should be
* done.
*/
int
get_syntax_attr(col, can_spell, keep_state)
colnr_T col;
int *can_spell;
int keep_state; /* keep state of char at "col" */
{
int attr = 0;
if (can_spell != NULL)
/* Default: Only do spelling when there is no @Spell cluster or when
* ":syn spell toplevel" was used. */
*can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
? (syn_block->b_spell_cluster_id == 0)
: (syn_block->b_syn_spell == SYNSPL_TOP);
/* check for out of memory situation */
if (syn_block->b_sst_array == NULL)
return 0;
/* After 'synmaxcol' the attribute is always zero. */
if (syn_buf->b_p_smc > 0 && col >= (colnr_T)syn_buf->b_p_smc)
{
clear_current_state();
#ifdef FEAT_EVAL
current_id = 0;
current_trans_id = 0;
#endif
#ifdef FEAT_CONCEAL
current_flags = 0;
#endif
return 0;
}
/* Make sure current_state is valid */
if (INVALID_STATE(&current_state))
validate_current_state();
/*
* Skip from the current column to "col", get the attributes for "col".
*/
while (current_col <= col)
{
attr = syn_current_attr(FALSE, TRUE, can_spell,
current_col == col ? keep_state : FALSE);
++current_col;
}
return attr;
}
/*
* Get syntax attributes for current_lnum, current_col.
*/
static int
syn_current_attr(syncing, displaying, can_spell, keep_state)
int syncing; /* When 1: called for syncing */
int displaying; /* result will be displayed */
int *can_spell; /* return: do spell checking */
int keep_state; /* keep syntax stack afterwards */
{
int syn_id;
lpos_T endpos; /* was: char_u *endp; */
lpos_T hl_startpos; /* was: int hl_startcol; */
lpos_T hl_endpos;
lpos_T eos_pos; /* end-of-start match (start region) */
lpos_T eoe_pos; /* end-of-end pattern */
int end_idx; /* group ID for end pattern */
int idx;
synpat_T *spp;
stateitem_T *cur_si, *sip = NULL;
int startcol;
int endcol;
long flags;
int cchar;
short *next_list;
int found_match; /* found usable match */
static int try_next_column = FALSE; /* must try in next col */
int do_keywords;
regmmatch_T regmatch;
lpos_T pos;
int lc_col;
reg_extmatch_T *cur_extmatch = NULL;
char_u *line; /* current line. NOTE: becomes invalid after
looking for a pattern match! */
/* variables for zero-width matches that have a "nextgroup" argument */
int keep_next_list;
int zero_width_next_list = FALSE;
garray_T zero_width_next_ga;
/*
* No character, no attributes! Past end of line?
* Do try matching with an empty line (could be the start of a region).
*/
line = syn_getcurline();
if (line[current_col] == NUL && current_col != 0)
{
/*
* If we found a match after the last column, use it.
*/
if (next_match_idx >= 0 && next_match_col >= (int)current_col
&& next_match_col != MAXCOL)
(void)push_next_match(NULL);
current_finished = TRUE;
current_state_stored = FALSE;
return 0;
}
/* if the current or next character is NUL, we will finish the line now */
if (line[current_col] == NUL || line[current_col + 1] == NUL)
{
current_finished = TRUE;
current_state_stored = FALSE;
}
/*
* When in the previous column there was a match but it could not be used
* (empty match or already matched in this column) need to try again in
* the next column.
*/
if (try_next_column)
{
next_match_idx = -1;
try_next_column = FALSE;
}
/* Only check for keywords when not syncing and there are some. */
do_keywords = !syncing
&& (syn_block->b_keywtab.ht_used > 0
|| syn_block->b_keywtab_ic.ht_used > 0);
/* Init the list of zero-width matches with a nextlist. This is used to
* avoid matching the same item in the same position twice. */
ga_init2(&zero_width_next_ga, (int)sizeof(int), 10);
/*
* Repeat matching keywords and patterns, to find contained items at the
* same column. This stops when there are no extra matches at the current
* column.
*/
do
{
found_match = FALSE;
keep_next_list = FALSE;
syn_id = 0;
/*
* 1. Check for a current state.
* Only when there is no current state, or if the current state may
* contain other things, we need to check for keywords and patterns.
* Always need to check for contained items if some item has the
* "containedin" argument (takes extra time!).
*/
if (current_state.ga_len)
cur_si = &CUR_STATE(current_state.ga_len - 1);
else
cur_si = NULL;
if (syn_block->b_syn_containedin || cur_si == NULL
|| cur_si->si_cont_list != NULL)
{
/*
* 2. Check for keywords, if on a keyword char after a non-keyword
* char. Don't do this when syncing.
*/
if (do_keywords)
{
line = syn_getcurline();
if (vim_iswordc_buf(line + current_col, syn_buf)
&& (current_col == 0
|| !vim_iswordc_buf(line + current_col - 1
#ifdef FEAT_MBYTE
- (has_mbyte
? (*mb_head_off)(line, line + current_col - 1)
: 0)
#endif
, syn_buf)))
{
syn_id = check_keyword_id(line, (int)current_col,
&endcol, &flags, &next_list, cur_si,
&cchar);
if (syn_id != 0)
{
if (push_current_state(KEYWORD_IDX) == OK)
{
cur_si = &CUR_STATE(current_state.ga_len - 1);
cur_si->si_m_startcol = current_col;
cur_si->si_h_startpos.lnum = current_lnum;
cur_si->si_h_startpos.col = 0; /* starts right away */
cur_si->si_m_endpos.lnum = current_lnum;
cur_si->si_m_endpos.col = endcol;
cur_si->si_h_endpos.lnum = current_lnum;
cur_si->si_h_endpos.col = endcol;
cur_si->si_ends = TRUE;
cur_si->si_end_idx = 0;
cur_si->si_flags = flags;
#ifdef FEAT_CONCEAL
cur_si->si_seqnr = next_seqnr++;
cur_si->si_cchar = cchar;
if (current_state.ga_len > 1)
cur_si->si_flags |=
CUR_STATE(current_state.ga_len - 2).si_flags
& HL_CONCEAL;
#endif
cur_si->si_id = syn_id;
cur_si->si_trans_id = syn_id;
if (flags & HL_TRANSP)
{
if (current_state.ga_len < 2)
{
cur_si->si_attr = 0;
cur_si->si_trans_id = 0;
}
else
{
cur_si->si_attr = CUR_STATE(
current_state.ga_len - 2).si_attr;
cur_si->si_trans_id = CUR_STATE(
current_state.ga_len - 2).si_trans_id;
}
}
else
cur_si->si_attr = syn_id2attr(syn_id);
cur_si->si_cont_list = NULL;
cur_si->si_next_list = next_list;
check_keepend();
}
else
vim_free(next_list);
}
}
}
/*
* 3. Check for patterns (only if no keyword found).
*/
if (syn_id == 0 && syn_block->b_syn_patterns.ga_len)
{
/*
* If we didn't check for a match yet, or we are past it, check
* for any match with a pattern.
*/
if (next_match_idx < 0 || next_match_col < (int)current_col)
{
/*
* Check all relevant patterns for a match at this
* position. This is complicated, because matching with a
* pattern takes quite a bit of time, thus we want to
* avoid doing it when it's not needed.
*/
next_match_idx = 0; /* no match in this line yet */
next_match_col = MAXCOL;
for (idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; )
{
spp = &(SYN_ITEMS(syn_block)[idx]);
if ( spp->sp_syncing == syncing
&& (displaying || !(spp->sp_flags & HL_DISPLAY))
&& (spp->sp_type == SPTYPE_MATCH
|| spp->sp_type == SPTYPE_START)
&& (current_next_list != NULL
? in_id_list(NULL, current_next_list,
&spp->sp_syn, 0)
: (cur_si == NULL
? !(spp->sp_flags & HL_CONTAINED)
: in_id_list(cur_si,
cur_si->si_cont_list, &spp->sp_syn,
spp->sp_flags & HL_CONTAINED))))
{
/* If we already tried matching in this line, and
* there isn't a match before next_match_col, skip
* this item. */
if (spp->sp_line_id == current_line_id
&& spp->sp_startcol >= next_match_col)
continue;
spp->sp_line_id = current_line_id;
lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
if (lc_col < 0)
lc_col = 0;
regmatch.rmm_ic = spp->sp_ic;
regmatch.regprog = spp->sp_prog;
if (!syn_regexec(&regmatch, current_lnum,
(colnr_T)lc_col))
{
/* no match in this line, try another one */
spp->sp_startcol = MAXCOL;
continue;
}
/*
* Compute the first column of the match.
*/
syn_add_start_off(&pos, &regmatch,
spp, SPO_MS_OFF, -1);
if (pos.lnum > current_lnum)
{
/* must have used end of match in a next line,
* we can't handle that */
spp->sp_startcol = MAXCOL;
continue;
}
startcol = pos.col;
/* remember the next column where this pattern
* matches in the current line */
spp->sp_startcol = startcol;
/*
* If a previously found match starts at a lower
* column number, don't use this one.
*/
if (startcol >= next_match_col)
continue;
/*
* If we matched this pattern at this position
* before, skip it. Must retry in the next
* column, because it may match from there.
*/
if (did_match_already(idx, &zero_width_next_ga))
{
try_next_column = TRUE;
continue;
}
endpos.lnum = regmatch.endpos[0].lnum;
endpos.col = regmatch.endpos[0].col;
/* Compute the highlight start. */
syn_add_start_off(&hl_startpos, &regmatch,
spp, SPO_HS_OFF, -1);
/* Compute the region start. */
/* Default is to use the end of the match. */
syn_add_end_off(&eos_pos, &regmatch,
spp, SPO_RS_OFF, 0);
/*
* Grab the external submatches before they get
* overwritten. Reference count doesn't change.
*/
unref_extmatch(cur_extmatch);
cur_extmatch = re_extmatch_out;
re_extmatch_out = NULL;
flags = 0;
eoe_pos.lnum = 0; /* avoid warning */
eoe_pos.col = 0;
end_idx = 0;
hl_endpos.lnum = 0;
/*
* For a "oneline" the end must be found in the
* same line too. Search for it after the end of
* the match with the start pattern. Set the
* resulting end positions at the same time.
*/
if (spp->sp_type == SPTYPE_START
&& (spp->sp_flags & HL_ONELINE))
{
lpos_T startpos;
startpos = endpos;
find_endpos(idx, &startpos, &endpos, &hl_endpos,
&flags, &eoe_pos, &end_idx, cur_extmatch);
if (endpos.lnum == 0)
continue; /* not found */
}
/*
* For a "match" the size must be > 0 after the
* end offset needs has been added. Except when
* syncing.
*/
else if (spp->sp_type == SPTYPE_MATCH)
{
syn_add_end_off(&hl_endpos, &regmatch, spp,
SPO_HE_OFF, 0);
syn_add_end_off(&endpos, &regmatch, spp,
SPO_ME_OFF, 0);
if (endpos.lnum == current_lnum
&& (int)endpos.col + syncing < startcol)
{
/*
* If an empty string is matched, may need
* to try matching again at next column.
*/
if (regmatch.startpos[0].col
== regmatch.endpos[0].col)
try_next_column = TRUE;
continue;
}
}
/*
* keep the best match so far in next_match_*
*/
/* Highlighting must start after startpos and end
* before endpos. */
if (hl_startpos.lnum == current_lnum
&& (int)hl_startpos.col < startcol)
hl_startpos.col = startcol;
limit_pos_zero(&hl_endpos, &endpos);
next_match_idx = idx;
next_match_col = startcol;
next_match_m_endpos = endpos;
next_match_h_endpos = hl_endpos;
next_match_h_startpos = hl_startpos;
next_match_flags = flags;
next_match_eos_pos = eos_pos;
next_match_eoe_pos = eoe_pos;
next_match_end_idx = end_idx;
unref_extmatch(next_match_extmatch);
next_match_extmatch = cur_extmatch;
cur_extmatch = NULL;
}
}
}
/*
* If we found a match at the current column, use it.
*/
if (next_match_idx >= 0 && next_match_col == (int)current_col)
{
synpat_T *lspp;
/* When a zero-width item matched which has a nextgroup,
* don't push the item but set nextgroup. */
lspp = &(SYN_ITEMS(syn_block)[next_match_idx]);
if (next_match_m_endpos.lnum == current_lnum
&& next_match_m_endpos.col == current_col
&& lspp->sp_next_list != NULL)
{
current_next_list = lspp->sp_next_list;
current_next_flags = lspp->sp_flags;
keep_next_list = TRUE;
zero_width_next_list = TRUE;
/* Add the index to a list, so that we can check
* later that we don't match it again (and cause an
* endless loop). */
if (ga_grow(&zero_width_next_ga, 1) == OK)
{
((int *)(zero_width_next_ga.ga_data))
[zero_width_next_ga.ga_len++] = next_match_idx;
}
next_match_idx = -1;
}
else
cur_si = push_next_match(cur_si);
found_match = TRUE;
}
}
}
/*
* Handle searching for nextgroup match.
*/
if (current_next_list != NULL && !keep_next_list)
{
/*
* If a nextgroup was not found, continue looking for one if:
* - this is an empty line and the "skipempty" option was given
* - we are on white space and the "skipwhite" option was given
*/
if (!found_match)
{
line = syn_getcurline();
if (((current_next_flags & HL_SKIPWHITE)
&& vim_iswhite(line[current_col]))
|| ((current_next_flags & HL_SKIPEMPTY)
&& *line == NUL))
break;
}
/*
* If a nextgroup was found: Use it, and continue looking for
* contained matches.
* If a nextgroup was not found: Continue looking for a normal
* match.
* When did set current_next_list for a zero-width item and no
* match was found don't loop (would get stuck).
*/
current_next_list = NULL;
next_match_idx = -1;
if (!zero_width_next_list)
found_match = TRUE;
}
} while (found_match);
/*
* Use attributes from the current state, if within its highlighting.
* If not, use attributes from the current-but-one state, etc.
*/
current_attr = 0;
#ifdef FEAT_EVAL
current_id = 0;
current_trans_id = 0;
#endif
#ifdef FEAT_CONCEAL
current_flags = 0;
#endif
if (cur_si != NULL)
{
#ifndef FEAT_EVAL
int current_trans_id = 0;
#endif
for (idx = current_state.ga_len - 1; idx >= 0; --idx)
{
sip = &CUR_STATE(idx);
if ((current_lnum > sip->si_h_startpos.lnum
|| (current_lnum == sip->si_h_startpos.lnum
&& current_col >= sip->si_h_startpos.col))
&& (sip->si_h_endpos.lnum == 0
|| current_lnum < sip->si_h_endpos.lnum
|| (current_lnum == sip->si_h_endpos.lnum
&& current_col < sip->si_h_endpos.col)))
{
current_attr = sip->si_attr;
#ifdef FEAT_EVAL
current_id = sip->si_id;
#endif
current_trans_id = sip->si_trans_id;
#ifdef FEAT_CONCEAL
current_flags = sip->si_flags;
current_seqnr = sip->si_seqnr;
current_sub_char = sip->si_cchar;
#endif
break;
}
}
if (can_spell != NULL)
{
struct sp_syn sps;
/*
* set "can_spell" to TRUE if spell checking is supposed to be
* done in the current item.
*/
if (syn_block->b_spell_cluster_id == 0)
{
/* There is no @Spell cluster: Do spelling for items without
* @NoSpell cluster. */
if (syn_block->b_nospell_cluster_id == 0
|| current_trans_id == 0)
*can_spell = (syn_block->b_syn_spell != SYNSPL_NOTOP);
else
{
sps.inc_tag = 0;
sps.id = syn_block->b_nospell_cluster_id;
sps.cont_in_list = NULL;
*can_spell = !in_id_list(sip, sip->si_cont_list, &sps, 0);
}
}
else
{
/* The @Spell cluster is defined: Do spelling in items with
* the @Spell cluster. But not when @NoSpell is also there.
* At the toplevel only spell check when ":syn spell toplevel"
* was used. */
if (current_trans_id == 0)
*can_spell = (syn_block->b_syn_spell == SYNSPL_TOP);
else
{
sps.inc_tag = 0;
sps.id = syn_block->b_spell_cluster_id;
sps.cont_in_list = NULL;
*can_spell = in_id_list(sip, sip->si_cont_list, &sps, 0);
if (syn_block->b_nospell_cluster_id != 0)
{
sps.id = syn_block->b_nospell_cluster_id;
if (in_id_list(sip, sip->si_cont_list, &sps, 0))
*can_spell = FALSE;
}
}
}
}
/*
* Check for end of current state (and the states before it) at the
* next column. Don't do this for syncing, because we would miss a
* single character match.
* First check if the current state ends at the current column. It
* may be for an empty match and a containing item might end in the
* current column.
*/
if (!syncing && !keep_state)
{
check_state_ends();
if (current_state.ga_len > 0
&& syn_getcurline()[current_col] != NUL)
{
++current_col;
check_state_ends();
--current_col;
}
}
}
else if (can_spell != NULL)
/* Default: Only do spelling when there is no @Spell cluster or when
* ":syn spell toplevel" was used. */
*can_spell = syn_block->b_syn_spell == SYNSPL_DEFAULT
? (syn_block->b_spell_cluster_id == 0)
: (syn_block->b_syn_spell == SYNSPL_TOP);
/* nextgroup ends at end of line, unless "skipnl" or "skipempty" present */
if (current_next_list != NULL
&& syn_getcurline()[current_col + 1] == NUL
&& !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
current_next_list = NULL;
if (zero_width_next_ga.ga_len > 0)
ga_clear(&zero_width_next_ga);
/* No longer need external matches. But keep next_match_extmatch. */
unref_extmatch(re_extmatch_out);
re_extmatch_out = NULL;
unref_extmatch(cur_extmatch);
return current_attr;
}
/*
* Check if we already matched pattern "idx" at the current column.
*/
static int
did_match_already(idx, gap)
int idx;
garray_T *gap;
{
int i;
for (i = current_state.ga_len; --i >= 0; )
if (CUR_STATE(i).si_m_startcol == (int)current_col
&& CUR_STATE(i).si_m_lnum == (int)current_lnum
&& CUR_STATE(i).si_idx == idx)
return TRUE;
/* Zero-width matches with a nextgroup argument are not put on the syntax
* stack, and can only be matched once anyway. */
for (i = gap->ga_len; --i >= 0; )
if (((int *)(gap->ga_data))[i] == idx)
return TRUE;
return FALSE;
}
/*
* Push the next match onto the stack.
*/
static stateitem_T *
push_next_match(cur_si)
stateitem_T *cur_si;
{
synpat_T *spp;
#ifdef FEAT_CONCEAL
int save_flags;
#endif
spp = &(SYN_ITEMS(syn_block)[next_match_idx]);
/*
* Push the item in current_state stack;
*/
if (push_current_state(next_match_idx) == OK)
{
/*
* If it's a start-skip-end type that crosses lines, figure out how
* much it continues in this line. Otherwise just fill in the length.
*/
cur_si = &CUR_STATE(current_state.ga_len - 1);
cur_si->si_h_startpos = next_match_h_startpos;
cur_si->si_m_startcol = current_col;
cur_si->si_m_lnum = current_lnum;
cur_si->si_flags = spp->sp_flags;
#ifdef FEAT_CONCEAL
cur_si->si_seqnr = next_seqnr++;
cur_si->si_cchar = spp->sp_cchar;
if (current_state.ga_len > 1)
cur_si->si_flags |=
CUR_STATE(current_state.ga_len - 2).si_flags & HL_CONCEAL;
#endif
cur_si->si_next_list = spp->sp_next_list;
cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
{
/* Try to find the end pattern in the current line */
update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
check_keepend();
}
else
{
cur_si->si_m_endpos = next_match_m_endpos;
cur_si->si_h_endpos = next_match_h_endpos;
cur_si->si_ends = TRUE;
cur_si->si_flags |= next_match_flags;
cur_si->si_eoe_pos = next_match_eoe_pos;
cur_si->si_end_idx = next_match_end_idx;
}
if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
keepend_level = current_state.ga_len - 1;
check_keepend();
update_si_attr(current_state.ga_len - 1);
#ifdef FEAT_CONCEAL
save_flags = cur_si->si_flags & (HL_CONCEAL | HL_CONCEALENDS);
#endif
/*
* If the start pattern has another highlight group, push another item
* on the stack for the start pattern.
*/
if ( spp->sp_type == SPTYPE_START
&& spp->sp_syn_match_id != 0
&& push_current_state(next_match_idx) == OK)
{
cur_si = &CUR_STATE(current_state.ga_len - 1);
cur_si->si_h_startpos = next_match_h_startpos;
cur_si->si_m_startcol = current_col;
cur_si->si_m_lnum = current_lnum;
cur_si->si_m_endpos = next_match_eos_pos;
cur_si->si_h_endpos = next_match_eos_pos;
cur_si->si_ends = TRUE;
cur_si->si_end_idx = 0;
cur_si->si_flags = HL_MATCH;
#ifdef FEAT_CONCEAL
cur_si->si_seqnr = next_seqnr++;
cur_si->si_flags |= save_flags;
if (cur_si->si_flags & HL_CONCEALENDS)
cur_si->si_flags |= HL_CONCEAL;
#endif
cur_si->si_next_list = NULL;
check_keepend();
update_si_attr(current_state.ga_len - 1);
}
}
next_match_idx = -1; /* try other match next time */
return cur_si;
}
/*
* Check for end of current state (and the states before it).
*/
static void
check_state_ends()
{
stateitem_T *cur_si;
int had_extend;
cur_si = &CUR_STATE(current_state.ga_len - 1);
for (;;)
{
if (cur_si->si_ends
&& (cur_si->si_m_endpos.lnum < current_lnum
|| (cur_si->si_m_endpos.lnum == current_lnum
&& cur_si->si_m_endpos.col <= current_col)))
{
/*
* If there is an end pattern group ID, highlight the end pattern
* now. No need to pop the current item from the stack.
* Only do this if the end pattern continues beyond the current
* position.
*/
if (cur_si->si_end_idx
&& (cur_si->si_eoe_pos.lnum > current_lnum
|| (cur_si->si_eoe_pos.lnum == current_lnum
&& cur_si->si_eoe_pos.col > current_col)))
{
cur_si->si_idx = cur_si->si_end_idx;
cur_si->si_end_idx = 0;
cur_si->si_m_endpos = cur_si->si_eoe_pos;
cur_si->si_h_endpos = cur_si->si_eoe_pos;
cur_si->si_flags |= HL_MATCH;
#ifdef FEAT_CONCEAL
cur_si->si_seqnr = next_seqnr++;
if (cur_si->si_flags & HL_CONCEALENDS)
cur_si->si_flags |= HL_CONCEAL;
#endif
update_si_attr(current_state.ga_len - 1);
/* nextgroup= should not match in the end pattern */
current_next_list = NULL;
/* what matches next may be different now, clear it */
next_match_idx = 0;
next_match_col = MAXCOL;
break;
}
else
{
/* handle next_list, unless at end of line and no "skipnl" or
* "skipempty" */
current_next_list = cur_si->si_next_list;
current_next_flags = cur_si->si_flags;
if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
&& syn_getcurline()[current_col] == NUL)
current_next_list = NULL;
/* When the ended item has "extend", another item with
* "keepend" now needs to check for its end. */
had_extend = (cur_si->si_flags & HL_EXTEND);
pop_current_state();
if (current_state.ga_len == 0)
break;
if (had_extend && keepend_level >= 0)
{
syn_update_ends(FALSE);
if (current_state.ga_len == 0)
break;
}
cur_si = &CUR_STATE(current_state.ga_len - 1);
/*
* Only for a region the search for the end continues after
* the end of the contained item. If the contained match
* included the end-of-line, break here, the region continues.
* Don't do this when:
* - "keepend" is used for the contained item
* - not at the end of the line (could be end="x$"me=e-1).
* - "excludenl" is used (HL_HAS_EOL won't be set)
*/
if (cur_si->si_idx >= 0
&& SYN_ITEMS(syn_block)[cur_si->si_idx].sp_type
== SPTYPE_START
&& !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
{
update_si_end(cur_si, (int)current_col, TRUE);
check_keepend();
if ((current_next_flags & HL_HAS_EOL)
&& keepend_level < 0
&& syn_getcurline()[current_col] == NUL)
break;
}
}
}
else
break;
}
}
/*
* Update an entry in the current_state stack for a match or region. This
* fills in si_attr, si_next_list and si_cont_list.
*/
static void
update_si_attr(idx)
int idx;
{
stateitem_T *sip = &CUR_STATE(idx);
synpat_T *spp;
/* This should not happen... */
if (sip->si_idx < 0)
return;
spp = &(SYN_ITEMS(syn_block)[sip->si_idx]);
if (sip->si_flags & HL_MATCH)
sip->si_id = spp->sp_syn_match_id;
else
sip->si_id = spp->sp_syn.id;
sip->si_attr = syn_id2attr(sip->si_id);
sip->si_trans_id = sip->si_id;
if (sip->si_flags & HL_MATCH)
sip->si_cont_list = NULL;
else
sip->si_cont_list = spp->sp_cont_list;
/*
* For transparent items, take attr from outer item.
* Also take cont_list, if there is none.
* Don't do this for the matchgroup of a start or end pattern.
*/
if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
{
if (idx == 0)
{
sip->si_attr = 0;
sip->si_trans_id = 0;
if (sip->si_cont_list == NULL)
sip->si_cont_list = ID_LIST_ALL;
}
else
{
sip->si_attr = CUR_STATE(idx - 1).si_attr;
sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
sip->si_h_startpos = CUR_STATE(idx - 1).si_h_startpos;
sip->si_h_endpos = CUR_STATE(idx - 1).si_h_endpos;
if (sip->si_cont_list == NULL)
{
sip->si_flags |= HL_TRANS_CONT;
sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
}
}
}
}
/*
* Check the current stack for patterns with "keepend" flag.
* Propagate the match-end to contained items, until a "skipend" item is found.
*/
static void
check_keepend()
{
int i;
lpos_T maxpos;
lpos_T maxpos_h;
stateitem_T *sip;
/*
* This check can consume a lot of time; only do it from the level where
* there really is a keepend.
*/
if (keepend_level < 0)
return;
/*
* Find the last index of an "extend" item. "keepend" items before that
* won't do anything. If there is no "extend" item "i" will be
* "keepend_level" and all "keepend" items will work normally.
*/
for (i = current_state.ga_len - 1; i > keepend_level; --i)
if (CUR_STATE(i).si_flags & HL_EXTEND)
break;
maxpos.lnum = 0;
maxpos.col = 0;
maxpos_h.lnum = 0;
maxpos_h.col = 0;
for ( ; i < current_state.ga_len; ++i)
{
sip = &CUR_STATE(i);
if (maxpos.lnum != 0)
{
limit_pos_zero(&sip->si_m_endpos, &maxpos);
limit_pos_zero(&sip->si_h_endpos, &maxpos_h);
limit_pos_zero(&sip->si_eoe_pos, &maxpos);
sip->si_ends = TRUE;
}
if (sip->si_ends && (sip->si_flags & HL_KEEPEND))
{
if (maxpos.lnum == 0
|| maxpos.lnum > sip->si_m_endpos.lnum
|| (maxpos.lnum == sip->si_m_endpos.lnum
&& maxpos.col > sip->si_m_endpos.col))
maxpos = sip->si_m_endpos;
if (maxpos_h.lnum == 0
|| maxpos_h.lnum > sip->si_h_endpos.lnum
|| (maxpos_h.lnum == sip->si_h_endpos.lnum
&& maxpos_h.col > sip->si_h_endpos.col))
maxpos_h = sip->si_h_endpos;
}
}
}
/*
* Update an entry in the current_state stack for a start-skip-end pattern.
* This finds the end of the current item, if it's in the current line.
*
* Return the flags for the matched END.
*/
static void
update_si_end(sip, startcol, force)
stateitem_T *sip;
int startcol; /* where to start searching for the end */
int force; /* when TRUE overrule a previous end */
{
lpos_T startpos;
lpos_T endpos;
lpos_T hl_endpos;
lpos_T end_endpos;
int end_idx;
/* return quickly for a keyword */
if (sip->si_idx < 0)
return;
/* Don't update when it's already done. Can be a match of an end pattern
* that started in a previous line. Watch out: can also be a "keepend"
* from a containing item. */
if (!force && sip->si_m_endpos.lnum >= current_lnum)
return;
/*
* We need to find the end of the region. It may continue in the next
* line.
*/
end_idx = 0;
startpos.lnum = current_lnum;
startpos.col = startcol;
find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
&(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
if (endpos.lnum == 0)
{
/* No end pattern matched. */
if (SYN_ITEMS(syn_block)[sip->si_idx].sp_flags & HL_ONELINE)
{
/* a "oneline" never continues in the next line */
sip->si_ends = TRUE;
sip->si_m_endpos.lnum = current_lnum;
sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
}
else
{
/* continues in the next line */
sip->si_ends = FALSE;
sip->si_m_endpos.lnum = 0;
}
sip->si_h_endpos = sip->si_m_endpos;
}
else
{
/* match within this line */
sip->si_m_endpos = endpos;
sip->si_h_endpos = hl_endpos;
sip->si_eoe_pos = end_endpos;
sip->si_ends = TRUE;
sip->si_end_idx = end_idx;
}
}
/*
* Add a new state to the current state stack.
* It is cleared and the index set to "idx".
* Return FAIL if it's not possible (out of memory).
*/
static int
push_current_state(idx)
int idx;
{
if (ga_grow(&current_state, 1) == FAIL)
return FAIL;
vim_memset(&CUR_STATE(current_state.ga_len), 0, sizeof(stateitem_T));
CUR_STATE(current_state.ga_len).si_idx = idx;
++current_state.ga_len;
return OK;
}
/*
* Remove a state from the current_state stack.
*/
static void
pop_current_state()
{
if (current_state.ga_len)
{
unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
--current_state.ga_len;
}
/* after the end of a pattern, try matching a keyword or pattern */
next_match_idx = -1;
/* if first state with "keepend" is popped, reset keepend_level */
if (keepend_level >= current_state.ga_len)
keepend_level = -1;
}
/*
* Find the end of a start/skip/end syntax region after "startpos".
* Only checks one line.
* Also handles a match item that continued from a previous line.
* If not found, the syntax item continues in the next line. m_endpos->lnum
* will be 0.
* If found, the end of the region and the end of the highlighting is
* computed.
*/
static void
find_endpos(idx, startpos, m_endpos, hl_endpos, flagsp, end_endpos,
end_idx, start_ext)
int idx; /* index of the pattern */
lpos_T *startpos; /* where to start looking for an END match */
lpos_T *m_endpos; /* return: end of match */
lpos_T *hl_endpos; /* return: end of highlighting */
long *flagsp; /* return: flags of matching END */
lpos_T *end_endpos; /* return: end of end pattern match */
int *end_idx; /* return: group ID for end pat. match, or 0 */
reg_extmatch_T *start_ext; /* submatches from the start pattern */
{
colnr_T matchcol;
synpat_T *spp, *spp_skip;
int start_idx;
int best_idx;
regmmatch_T regmatch;
regmmatch_T best_regmatch; /* startpos/endpos of best match */
lpos_T pos;
char_u *line;
int had_match = FALSE;
/* just in case we are invoked for a keyword */
if (idx < 0)
return;
/*
* Check for being called with a START pattern.
* Can happen with a match that continues to the next line, because it
* contained a region.
*/
spp = &(SYN_ITEMS(syn_block)[idx]);
if (spp->sp_type != SPTYPE_START)
{
*hl_endpos = *startpos;
return;
}
/*
* Find the SKIP or first END pattern after the last START pattern.
*/
for (;;)
{
spp = &(SYN_ITEMS(syn_block)[idx]);
if (spp->sp_type != SPTYPE_START)
break;
++idx;
}
/*
* Lookup the SKIP pattern (if present)
*/
if (spp->sp_type == SPTYPE_SKIP)
{
spp_skip = spp;
++idx;
}
else
spp_skip = NULL;
/* Setup external matches for syn_regexec(). */
unref_extmatch(re_extmatch_in);
re_extmatch_in = ref_extmatch(start_ext);
matchcol = startpos->col; /* start looking for a match at sstart */
start_idx = idx; /* remember the first END pattern. */
best_regmatch.startpos[0].col = 0; /* avoid compiler warning */
for (;;)
{
/*
* Find end pattern that matches first after "matchcol".
*/
best_idx = -1;
for (idx = start_idx; idx < syn_block->b_syn_patterns.ga_len; ++idx)
{
int lc_col = matchcol;
spp = &(SYN_ITEMS(syn_block)[idx]);
if (spp->sp_type != SPTYPE_END) /* past last END pattern */
break;
lc_col -= spp->sp_offsets[SPO_LC_OFF];
if (lc_col < 0)
lc_col = 0;
regmatch.rmm_ic = spp->sp_ic;
regmatch.regprog = spp->sp_prog;
if (syn_regexec(&regmatch, startpos->lnum, lc_col))
{
if (best_idx == -1 || regmatch.startpos[0].col
< best_regmatch.startpos[0].col)
{
best_idx = idx;
best_regmatch.startpos[0] = regmatch.startpos[0];
best_regmatch.endpos[0] = regmatch.endpos[0];
}
}
}
/*
* If all end patterns have been tried, and there is no match, the
* item continues until end-of-line.
*/
if (best_idx == -1)
break;
/*
* If the skip pattern matches before the end pattern,
* continue searching after the skip pattern.
*/
if (spp_skip != NULL)
{
int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
if (lc_col < 0)
lc_col = 0;
regmatch.rmm_ic = spp_skip->sp_ic;
regmatch.regprog = spp_skip->sp_prog;
if (syn_regexec(&regmatch, startpos->lnum, lc_col)
&& regmatch.startpos[0].col
<= best_regmatch.startpos[0].col)
{
/* Add offset to skip pattern match */
syn_add_end_off(&pos, &regmatch, spp_skip, SPO_ME_OFF, 1);
/* If the skip pattern goes on to the next line, there is no
* match with an end pattern in this line. */
if (pos.lnum > startpos->lnum)
break;
line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
/* take care of an empty match or negative offset */
if (pos.col <= matchcol)
++matchcol;
else if (pos.col <= regmatch.endpos[0].col)
matchcol = pos.col;
else
/* Be careful not to jump over the NUL at the end-of-line */
for (matchcol = regmatch.endpos[0].col;
line[matchcol] != NUL && matchcol < pos.col;
++matchcol)
;
/* if the skip pattern includes end-of-line, break here */
if (line[matchcol] == NUL)
break;
continue; /* start with first end pattern again */
}
}
/*
* Match from start pattern to end pattern.
* Correct for match and highlight offset of end pattern.
*/
spp = &(SYN_ITEMS(syn_block)[best_idx]);
syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
/* can't end before the start */
if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
m_endpos->col = startpos->col;
syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
/* can't end before the start */
if (end_endpos->lnum == startpos->lnum
&& end_endpos->col < startpos->col)
end_endpos->col = startpos->col;
/* can't end after the match */
limit_pos(end_endpos, m_endpos);
/*
* If the end group is highlighted differently, adjust the pointers.
*/
if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
{
*end_idx = best_idx;
if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
{
hl_endpos->lnum = best_regmatch.endpos[0].lnum;
hl_endpos->col = best_regmatch.endpos[0].col;
}
else
{
hl_endpos->lnum = best_regmatch.startpos[0].lnum;
hl_endpos->col = best_regmatch.startpos[0].col;
}
hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
/* can't end before the start */
if (hl_endpos->lnum == startpos->lnum
&& hl_endpos->col < startpos->col)
hl_endpos->col = startpos->col;
limit_pos(hl_endpos, m_endpos);
/* now the match ends where the highlighting ends, it is turned
* into the matchgroup for the end */
*m_endpos = *hl_endpos;
}
else
{
*end_idx = 0;
*hl_endpos = *end_endpos;
}
*flagsp = spp->sp_flags;
had_match = TRUE;
break;
}
/* no match for an END pattern in this line */
if (!had_match)
m_endpos->lnum = 0;
/* Remove external matches. */
unref_extmatch(re_extmatch_in);
re_extmatch_in = NULL;
}
/*
* Limit "pos" not to be after "limit".
*/
static void
limit_pos(pos, limit)
lpos_T *pos;
lpos_T *limit;
{
if (pos->lnum > limit->lnum)
*pos = *limit;
else if (pos->lnum == limit->lnum && pos->col > limit->col)
pos->col = limit->col;
}
/*
* Limit "pos" not to be after "limit", unless pos->lnum is zero.
*/
static void
limit_pos_zero(pos, limit)
lpos_T *pos;
lpos_T *limit;
{
if (pos->lnum == 0)
*pos = *limit;
else
limit_pos(pos, limit);
}
/*
* Add offset to matched text for end of match or highlight.
*/
static void
syn_add_end_off(result, regmatch, spp, idx, extra)
lpos_T *result; /* returned position */
regmmatch_T *regmatch; /* start/end of match */
synpat_T *spp; /* matched pattern */
int idx; /* index of offset */
int extra; /* extra chars for offset to start */
{
int col;
int off;
char_u *base;
char_u *p;
if (spp->sp_off_flags & (1 << idx))
{
result->lnum = regmatch->startpos[0].lnum;
col = regmatch->startpos[0].col;
off = spp->sp_offsets[idx] + extra;
}
else
{
result->lnum = regmatch->endpos[0].lnum;
col = regmatch->endpos[0].col;
off = spp->sp_offsets[idx];
}
/* Don't go past the end of the line. Matters for "rs=e+2" when there
* is a matchgroup. Watch out for match with last NL in the buffer. */
if (result->lnum > syn_buf->b_ml.ml_line_count)
col = 0;
else if (off != 0)
{
base = ml_get_buf(syn_buf, result->lnum, FALSE);
p = base + col;
if (off > 0)
{
while (off-- > 0 && *p != NUL)
mb_ptr_adv(p);
}
else if (off < 0)
{
while (off++ < 0 && base < p)
mb_ptr_back(base, p);
}
col = (int)(p - base);
}
result->col = col;
}
/*
* Add offset to matched text for start of match or highlight.
* Avoid resulting column to become negative.
*/
static void
syn_add_start_off(result, regmatch, spp, idx, extra)
lpos_T *result; /* returned position */
regmmatch_T *regmatch; /* start/end of match */
synpat_T *spp;
int idx;
int extra; /* extra chars for offset to end */
{
int col;
int off;
char_u *base;
char_u *p;
if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
{
result->lnum = regmatch->endpos[0].lnum;
col = regmatch->endpos[0].col;
off = spp->sp_offsets[idx] + extra;
}
else
{
result->lnum = regmatch->startpos[0].lnum;
col = regmatch->startpos[0].col;
off = spp->sp_offsets[idx];
}
if (result->lnum > syn_buf->b_ml.ml_line_count)
{
/* a "\n" at the end of the pattern may take us below the last line */
result->lnum = syn_buf->b_ml.ml_line_count;
col = (int)STRLEN(ml_get_buf(syn_buf, result->lnum, FALSE));
}
if (off != 0)
{
base = ml_get_buf(syn_buf, result->lnum, FALSE);
p = base + col;
if (off > 0)
{
while (off-- && *p != NUL)
mb_ptr_adv(p);
}
else if (off < 0)
{
while (off++ && base < p)
mb_ptr_back(base, p);
}
col = (int)(p - base);
}
result->col = col;
}
/*
* Get current line in syntax buffer.
*/
static char_u *
syn_getcurline()
{
return ml_get_buf(syn_buf, current_lnum, FALSE);
}
/*
* Call vim_regexec() to find a match with "rmp" in "syn_buf".
* Returns TRUE when there is a match.
*/
static int
syn_regexec(rmp, lnum, col)
regmmatch_T *rmp;
linenr_T lnum;
colnr_T col;
{
rmp->rmm_maxcol = syn_buf->b_p_smc;
if (vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col, NULL) > 0)
{
rmp->startpos[0].lnum += lnum;
rmp->endpos[0].lnum += lnum;
return TRUE;
}
return FALSE;
}
/*
* Check one position in a line for a matching keyword.
* The caller must check if a keyword can start at startcol.
* Return it's ID if found, 0 otherwise.
*/
static int
check_keyword_id(line, startcol, endcolp, flagsp, next_listp, cur_si, ccharp)
char_u *line;
int startcol; /* position in line to check for keyword */
int *endcolp; /* return: character after found keyword */
long *flagsp; /* return: flags of matching keyword */
short **next_listp; /* return: next_list of matching keyword */
stateitem_T *cur_si; /* item at the top of the stack */
int *ccharp UNUSED; /* conceal substitution char */
{
keyentry_T *kp;
char_u *kwp;
int round;
int kwlen;
char_u keyword[MAXKEYWLEN + 1]; /* assume max. keyword len is 80 */
hashtab_T *ht;
hashitem_T *hi;
/* Find first character after the keyword. First character was already
* checked. */
kwp = line + startcol;
kwlen = 0;
do
{
#ifdef FEAT_MBYTE
if (has_mbyte)
kwlen += (*mb_ptr2len)(kwp + kwlen);
else
#endif
++kwlen;
}
while (vim_iswordc_buf(kwp + kwlen, syn_buf));
if (kwlen > MAXKEYWLEN)
return 0;
/*
* Must make a copy of the keyword, so we can add a NUL and make it
* lowercase.
*/
vim_strncpy(keyword, kwp, kwlen);
/*
* Try twice:
* 1. matching case
* 2. ignoring case
*/
for (round = 1; round <= 2; ++round)
{
ht = round == 1 ? &syn_block->b_keywtab : &syn_block->b_keywtab_ic;
if (ht->ht_used == 0)
continue;
if (round == 2) /* ignore case */
(void)str_foldcase(kwp, kwlen, keyword, MAXKEYWLEN + 1);
/*
* Find keywords that match. There can be several with different
* attributes.
* When current_next_list is non-zero accept only that group, otherwise:
* Accept a not-contained keyword at toplevel.
* Accept a keyword at other levels only if it is in the contains list.
*/
hi = hash_find(ht, keyword);
if (!HASHITEM_EMPTY(hi))
for (kp = HI2KE(hi); kp != NULL; kp = kp->ke_next)
{
if (current_next_list != 0
? in_id_list(NULL, current_next_list, &kp->k_syn, 0)
: (cur_si == NULL
? !(kp->flags & HL_CONTAINED)
: in_id_list(cur_si, cur_si->si_cont_list,
&kp->k_syn, kp->flags & HL_CONTAINED)))
{
*endcolp = startcol + kwlen;
*flagsp = kp->flags;
*next_listp = kp->next_list;
#ifdef FEAT_CONCEAL
*ccharp = kp->k_char;
#endif
return kp->k_syn.id;
}
}
}
return 0;
}
/*
* Handle ":syntax conceal" command.
*/
static void
syn_cmd_conceal(eap, syncing)
exarg_T *eap UNUSED;
int syncing UNUSED;
{
#ifdef FEAT_CONCEAL
char_u *arg = eap->arg;
char_u *next;
eap->nextcmd = find_nextcmd(arg);
if (eap->skip)
return;
next = skiptowhite(arg);
if (STRNICMP(arg, "on", 2) == 0 && next - arg == 2)
curwin->w_s->b_syn_conceal = TRUE;
else if (STRNICMP(arg, "off", 3) == 0 && next - arg == 3)
curwin->w_s->b_syn_conceal = FALSE;
else
EMSG2(_("E390: Illegal argument: %s"), arg);
#endif
}
/*
* Handle ":syntax case" command.
*/
static void
syn_cmd_case(eap, syncing)
exarg_T *eap;
int syncing UNUSED;
{
char_u *arg = eap->arg;
char_u *next;
eap->nextcmd = find_nextcmd(arg);
if (eap->skip)
return;
next = skiptowhite(arg);
if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
curwin->w_s->b_syn_ic = FALSE;
else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
curwin->w_s->b_syn_ic = TRUE;
else
EMSG2(_("E390: Illegal argument: %s"), arg);
}
/*
* Handle ":syntax spell" command.
*/
static void
syn_cmd_spell(eap, syncing)
exarg_T *eap;
int syncing UNUSED;
{
char_u *arg = eap->arg;
char_u *next;
eap->nextcmd = find_nextcmd(arg);
if (eap->skip)
return;
next = skiptowhite(arg);
if (STRNICMP(arg, "toplevel", 8) == 0 && next - arg == 8)
curwin->w_s->b_syn_spell = SYNSPL_TOP;
else if (STRNICMP(arg, "notoplevel", 10) == 0 && next - arg == 10)
curwin->w_s->b_syn_spell = SYNSPL_NOTOP;
else if (STRNICMP(arg, "default", 7) == 0 && next - arg == 7)
curwin->w_s->b_syn_spell = SYNSPL_DEFAULT;
else
EMSG2(_("E390: Illegal argument: %s"), arg);
}
/*
* Clear all syntax info for one buffer.
*/
void
syntax_clear(block)
synblock_T *block;
{
int i;
block->b_syn_error = FALSE; /* clear previous error */
block->b_syn_ic = FALSE; /* Use case, by default */
block->b_syn_spell = SYNSPL_DEFAULT; /* default spell checking */
block->b_syn_containedin = FALSE;
/* free the keywords */
clear_keywtab(&block->b_keywtab);
clear_keywtab(&block->b_keywtab_ic);
/* free the syntax patterns */
for (i = block->b_syn_patterns.ga_len; --i >= 0; )
syn_clear_pattern(block, i);
ga_clear(&block->b_syn_patterns);
/* free the syntax clusters */
for (i = block->b_syn_clusters.ga_len; --i >= 0; )
syn_clear_cluster(block, i);
ga_clear(&block->b_syn_clusters);
block->b_spell_cluster_id = 0;
block->b_nospell_cluster_id = 0;
block->b_syn_sync_flags = 0;
block->b_syn_sync_minlines = 0;
block->b_syn_sync_maxlines = 0;
block->b_syn_sync_linebreaks = 0;
vim_free(block->b_syn_linecont_prog);
block->b_syn_linecont_prog = NULL;
vim_free(block->b_syn_linecont_pat);
block->b_syn_linecont_pat = NULL;
#ifdef FEAT_FOLDING
block->b_syn_folditems = 0;
#endif
/* free the stored states */
syn_stack_free_all(block);
invalidate_current_state();
/* Reset the counter for ":syn include" */
running_syn_inc_tag = 0;
}
/*
* Get rid of ownsyntax for window "wp".
*/
void
reset_synblock(wp)
win_T *wp;
{
if (wp->w_s != &wp->w_buffer->b_s)
{
syntax_clear(wp->w_s);
vim_free(wp->w_s);
wp->w_s = &wp->w_buffer->b_s;
}
}
/*
* Clear syncing info for one buffer.
*/
static void
syntax_sync_clear()
{
int i;
/* free the syntax patterns */
for (i = curwin->w_s->b_syn_patterns.ga_len; --i >= 0; )
if (SYN_ITEMS(curwin->w_s)[i].sp_syncing)
syn_remove_pattern(curwin->w_s, i);
curwin->w_s->b_syn_sync_flags = 0;
curwin->w_s->b_syn_sync_minlines = 0;
curwin->w_s->b_syn_sync_maxlines = 0;
curwin->w_s->b_syn_sync_linebreaks = 0;
vim_free(curwin->w_s->b_syn_linecont_prog);
curwin->w_s->b_syn_linecont_prog = NULL;
vim_free(curwin->w_s->b_syn_linecont_pat);
curwin->w_s->b_syn_linecont_pat = NULL;
syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
}
/*
* Remove one pattern from the buffer's pattern list.
*/
static void
syn_remove_pattern(block, idx)
synblock_T *block;
int idx;
{
synpat_T *spp;
spp = &(SYN_ITEMS(block)[idx]);
#ifdef FEAT_FOLDING
if (spp->sp_flags & HL_FOLD)
--block->b_syn_folditems;
#endif
syn_clear_pattern(block, idx);
mch_memmove(spp, spp + 1,
sizeof(synpat_T) * (block->b_syn_patterns.ga_len - idx - 1));
--block->b_syn_patterns.ga_len;
}
/*
* Clear and free one syntax pattern. When clearing all, must be called from
* last to first!
*/
static void
syn_clear_pattern(block, i)
synblock_T *block;
int i;
{
vim_free(SYN_ITEMS(block)[i].sp_pattern);
vim_free(SYN_ITEMS(block)[i].sp_prog);
/* Only free sp_cont_list and sp_next_list of first start pattern */
if (i == 0 || SYN_ITEMS(block)[i - 1].sp_type != SPTYPE_START)
{
vim_free(SYN_ITEMS(block)[i].sp_cont_list);
vim_free(SYN_ITEMS(block)[i].sp_next_list);
vim_free(SYN_ITEMS(block)[i].sp_syn.cont_in_list);
}
}
/*
* Clear and free one syntax cluster.
*/
static void
syn_clear_cluster(block, i)
synblock_T *block;
int i;
{
vim_free(SYN_CLSTR(block)[i].scl_name);
vim_free(SYN_CLSTR(block)[i].scl_name_u);
vim_free(SYN_CLSTR(block)[i].scl_list);
}
/*
* Handle ":syntax clear" command.
*/
static void
syn_cmd_clear(eap, syncing)
exarg_T *eap;
int syncing;
{
char_u *arg = eap->arg;
char_u *arg_end;
int id;
eap->nextcmd = find_nextcmd(arg);
if (eap->skip)
return;
/*
* We have to disable this within ":syn include @group filename",
* because otherwise @group would get deleted.
* Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
* clear".
*/
if (curwin->w_s->b_syn_topgrp != 0)
return;
if (ends_excmd(*arg))
{
/*
* No argument: Clear all syntax items.
*/
if (syncing)
syntax_sync_clear();
else
{
syntax_clear(curwin->w_s);
if (curwin->w_s == &curwin->w_buffer->b_s)
do_unlet((char_u *)"b:current_syntax", TRUE);
do_unlet((char_u *)"w:current_syntax", TRUE);
}
}
else
{
/*
* Clear the group IDs that are in the argument.
*/
while (!ends_excmd(*arg))
{
arg_end = skiptowhite(arg);
if (*arg == '@')
{
id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
if (id == 0)
{
EMSG2(_("E391: No such syntax cluster: %s"), arg);
break;
}
else
{
/*
* We can't physically delete a cluster without changing
* the IDs of other clusters, so we do the next best thing
* and make it empty.
*/
short scl_id = id - SYNID_CLUSTER;
vim_free(SYN_CLSTR(curwin->w_s)[scl_id].scl_list);
SYN_CLSTR(curwin->w_s)[scl_id].scl_list = NULL;
}
}
else
{
id = syn_namen2id(arg, (int)(arg_end - arg));
if (id == 0)
{
EMSG2(_(e_nogroup), arg);
break;
}
else
syn_clear_one(id, syncing);
}
arg = skipwhite(arg_end);
}
}
redraw_curbuf_later(SOME_VALID);
syn_stack_free_all(curwin->w_s); /* Need to recompute all syntax. */
}
/*
* Clear one syntax group for the current buffer.
*/
static void
syn_clear_one(id, syncing)
int id;
int syncing;
{
synpat_T *spp;
int idx;
/* Clear keywords only when not ":syn sync clear group-name" */
if (!syncing)
{
(void)syn_clear_keyword(id, &curwin->w_s->b_keywtab);
(void)syn_clear_keyword(id, &curwin->w_s->b_keywtab_ic);
}
/* clear the patterns for "id" */
for (idx = curwin->w_s->b_syn_patterns.ga_len; --idx >= 0; )
{
spp = &(SYN_ITEMS(curwin->w_s)[idx]);
if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
continue;
syn_remove_pattern(curwin->w_s, idx);
}
}
/*
* Handle ":syntax on" command.
*/
static void
syn_cmd_on(eap, syncing)
exarg_T *eap;
int syncing UNUSED;
{
syn_cmd_onoff(eap, "syntax");
}
/*
* Handle ":syntax enable" command.
*/
static void
syn_cmd_enable(eap, syncing)
exarg_T *eap;
int syncing UNUSED;
{
set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"enable");
syn_cmd_onoff(eap, "syntax");
do_unlet((char_u *)"g:syntax_cmd", TRUE);
}
/*
* Handle ":syntax reset" command.
*/
static void
syn_cmd_reset(eap, syncing)
exarg_T *eap;
int syncing UNUSED;
{
eap->nextcmd = check_nextcmd(eap->arg);
if (!eap->skip)
{
set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"reset");
do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
do_unlet((char_u *)"g:syntax_cmd", TRUE);
}
}
/*
* Handle ":syntax manual" command.
*/
static void
syn_cmd_manual(eap, syncing)
exarg_T *eap;
int syncing UNUSED;
{
syn_cmd_onoff(eap, "manual");
}
/*
* Handle ":syntax off" command.
*/
static void
syn_cmd_off(eap, syncing)
exarg_T *eap;
int syncing UNUSED;
{
syn_cmd_onoff(eap, "nosyntax");
}
static void
syn_cmd_onoff(eap, name)
exarg_T *eap;
char *name;
{
char_u buf[100];
eap->nextcmd = check_nextcmd(eap->arg);
if (!eap->skip)
{
STRCPY(buf, "so ");
vim_snprintf((char *)buf + 3, sizeof(buf) - 3, SYNTAX_FNAME, name);
do_cmdline_cmd(buf);
}
}
/*
* Handle ":syntax [list]" command: list current syntax words.
*/
static void
syn_cmd_list(eap, syncing)
exarg_T *eap;
int syncing; /* when TRUE: list syncing items */
{
char_u *arg = eap->arg;
int id;
char_u *arg_end;
eap->nextcmd = find_nextcmd(arg);
if (eap->skip)
return;
if (!syntax_present(curwin))
{
MSG(_("No Syntax items defined for this buffer"));
return;
}
if (syncing)
{
if (curwin->w_s->b_syn_sync_flags & SF_CCOMMENT)
{
MSG_PUTS(_("syncing on C-style comments"));
syn_lines_msg();
syn_match_msg();
return;
}
else if (!(curwin->w_s->b_syn_sync_flags & SF_MATCH))
{
if (curwin->w_s->b_syn_sync_minlines == 0)
MSG_PUTS(_("no syncing"));
else
{
MSG_PUTS(_("syncing starts "));
msg_outnum(curwin->w_s->b_syn_sync_minlines);
MSG_PUTS(_(" lines before top line"));
syn_match_msg();
}
return;
}
MSG_PUTS_TITLE(_("\n--- Syntax sync items ---"));
if (curwin->w_s->b_syn_sync_minlines > 0
|| curwin->w_s->b_syn_sync_maxlines > 0
|| curwin->w_s->b_syn_sync_linebreaks > 0)
{
MSG_PUTS(_("\nsyncing on items"));
syn_lines_msg();
syn_match_msg();
}
}
else
MSG_PUTS_TITLE(_("\n--- Syntax items ---"));
if (ends_excmd(*arg))
{
/*
* No argument: List all group IDs and all syntax clusters.
*/
for (id = 1; id <= highlight_ga.ga_len && !got_int; ++id)
syn_list_one(id, syncing, FALSE);
for (id = 0; id < curwin->w_s->b_syn_clusters.ga_len && !got_int; ++id)
syn_list_cluster(id);
}
else
{
/*
* List the group IDs and syntax clusters that are in the argument.
*/
while (!ends_excmd(*arg) && !got_int)
{
arg_end = skiptowhite(arg);
if (*arg == '@')
{
id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
if (id == 0)
EMSG2(_("E392: No such syntax cluster: %s"), arg);
else
syn_list_cluster(id - SYNID_CLUSTER);
}
else
{
id = syn_namen2id(arg, (int)(arg_end - arg));
if (id == 0)
EMSG2(_(e_nogroup), arg);
else
syn_list_one(id, syncing, TRUE);
}
arg = skipwhite(arg_end);
}
}
eap->nextcmd = check_nextcmd(arg);
}
static void
syn_lines_msg()
{
if (curwin->w_s->b_syn_sync_maxlines > 0
|| curwin->w_s->b_syn_sync_minlines > 0)
{
MSG_PUTS("; ");
if (curwin->w_s->b_syn_sync_minlines > 0)
{
MSG_PUTS(_("minimal "));
msg_outnum(curwin->w_s->b_syn_sync_minlines);
if (curwin->w_s->b_syn_sync_maxlines)
MSG_PUTS(", ");
}
if (curwin->w_s->b_syn_sync_maxlines > 0)
{
MSG_PUTS(_("maximal "));
msg_outnum(curwin->w_s->b_syn_sync_maxlines);
}
MSG_PUTS(_(" lines before top line"));
}
}
static void
syn_match_msg()
{
if (curwin->w_s->b_syn_sync_linebreaks > 0)
{
MSG_PUTS(_("; match "));
msg_outnum(curwin->w_s->b_syn_sync_linebreaks);
MSG_PUTS(_(" line breaks"));
}
}
static int last_matchgroup;
struct name_list
{
int flag;
char *name;
};
static void syn_list_flags __ARGS((struct name_list *nl, int flags, int attr));
/*
* List one syntax item, for ":syntax" or "syntax list syntax_name".
*/
static void
syn_list_one(id, syncing, link_only)
int id;
int syncing; /* when TRUE: list syncing items */
int link_only; /* when TRUE; list link-only too */
{
int attr;
int idx;
int did_header = FALSE;
synpat_T *spp;
static struct name_list namelist1[] =
{
{HL_DISPLAY, "display"},
{HL_CONTAINED, "contained"},
{HL_ONELINE, "oneline"},
{HL_KEEPEND, "keepend"},
{HL_EXTEND, "extend"},
{HL_EXCLUDENL, "excludenl"},
{HL_TRANSP, "transparent"},
{HL_FOLD, "fold"},
#ifdef FEAT_CONCEAL
{HL_CONCEAL, "conceal"},
{HL_CONCEALENDS, "concealends"},
#endif
{0, NULL}
};
static struct name_list namelist2[] =
{
{HL_SKIPWHITE, "skipwhite"},
{HL_SKIPNL, "skipnl"},
{HL_SKIPEMPTY, "skipempty"},
{0, NULL}
};
attr = hl_attr(HLF_D); /* highlight like directories */
/* list the keywords for "id" */
if (!syncing)
{
did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab, FALSE, attr);
did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab_ic,
did_header, attr);
}
/* list the patterns for "id" */
for (idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len && !got_int; ++idx)
{
spp = &(SYN_ITEMS(curwin->w_s)[idx]);
if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
continue;
(void)syn_list_header(did_header, 999, id);
did_header = TRUE;
last_matchgroup = 0;
if (spp->sp_type == SPTYPE_MATCH)
{
put_pattern("match", ' ', spp, attr);
msg_putchar(' ');
}
else if (spp->sp_type == SPTYPE_START)
{
while (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_START)
put_pattern("start", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
if (SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_SKIP)
put_pattern("skip", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
while (idx < curwin->w_s->b_syn_patterns.ga_len
&& SYN_ITEMS(curwin->w_s)[idx].sp_type == SPTYPE_END)
put_pattern("end", '=', &SYN_ITEMS(curwin->w_s)[idx++], attr);
--idx;
msg_putchar(' ');
}
syn_list_flags(namelist1, spp->sp_flags, attr);
if (spp->sp_cont_list != NULL)
put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
if (spp->sp_syn.cont_in_list != NULL)
put_id_list((char_u *)"containedin",
spp->sp_syn.cont_in_list, attr);
if (spp->sp_next_list != NULL)
{
put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
syn_list_flags(namelist2, spp->sp_flags, attr);
}
if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
{
if (spp->sp_flags & HL_SYNC_HERE)
msg_puts_attr((char_u *)"grouphere", attr);
else
msg_puts_attr((char_u *)"groupthere", attr);
msg_putchar(' ');
if (spp->sp_sync_idx >= 0)
msg_outtrans(HL_TABLE()[SYN_ITEMS(curwin->w_s)
[spp->sp_sync_idx].sp_syn.id - 1].sg_name);
else
MSG_PUTS("NONE");
msg_putchar(' ');
}
}
/* list the link, if there is one */
if (HL_TABLE()[id - 1].sg_link && (did_header || link_only) && !got_int)
{
(void)syn_list_header(did_header, 999, id);
msg_puts_attr((char_u *)"links to", attr);
msg_putchar(' ');
msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
}
}
static void
syn_list_flags(nlist, flags, attr)
struct name_list *nlist;
int flags;
int attr;
{
int i;
for (i = 0; nlist[i].flag != 0; ++i)
if (flags & nlist[i].flag)
{
msg_puts_attr((char_u *)nlist[i].name, attr);
msg_putchar(' ');
}
}
/*
* List one syntax cluster, for ":syntax" or "syntax list syntax_name".
*/
static void
syn_list_cluster(id)
int id;
{
int endcol = 15;
/* slight hack: roughly duplicate the guts of syn_list_header() */
msg_putchar('\n');
msg_outtrans(SYN_CLSTR(curwin->w_s)[id].scl_name);
if (msg_col >= endcol) /* output at least one space */
endcol = msg_col + 1;
if (Columns <= endcol) /* avoid hang for tiny window */
endcol = Columns - 1;
msg_advance(endcol);
if (SYN_CLSTR(curwin->w_s)[id].scl_list != NULL)
{
put_id_list((char_u *)"cluster", SYN_CLSTR(curwin->w_s)[id].scl_list,
hl_attr(HLF_D));
}
else
{
msg_puts_attr((char_u *)"cluster", hl_attr(HLF_D));
msg_puts((char_u *)"=NONE");
}
}
static void
put_id_list(name, list, attr)
char_u *name;
short *list;
int attr;
{
short *p;
msg_puts_attr(name, attr);
msg_putchar('=');
for (p = list; *p; ++p)
{
if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
{
if (p[1])
MSG_PUTS("ALLBUT");
else
MSG_PUTS("ALL");
}
else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
{
MSG_PUTS("TOP");
}
else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
{
MSG_PUTS("CONTAINED");
}
else if (*p >= SYNID_CLUSTER)
{
short scl_id = *p - SYNID_CLUSTER;
msg_putchar('@');
msg_outtrans(SYN_CLSTR(curwin->w_s)[scl_id].scl_name);
}
else
msg_outtrans(HL_TABLE()[*p - 1].sg_name);
if (p[1])
msg_putchar(',');
}
msg_putchar(' ');
}
static void
put_pattern(s, c, spp, attr)
char *s;
int c;
synpat_T *spp;
int attr;
{
long n;
int mask;
int first;
static char *sepchars = "/+=-#@\"|'^&";
int i;
/* May have to write "matchgroup=group" */
if (last_matchgroup != spp->sp_syn_match_id)
{
last_matchgroup = spp->sp_syn_match_id;
msg_puts_attr((char_u *)"matchgroup", attr);
msg_putchar('=');