home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / syntax.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-05-02  |  213.7 KB  |  8,396 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  * See README.txt for an overview of the Vim source code.
  8.  */
  9.  
  10. /*
  11.  * syntax.c: code for syntax highlighting
  12.  */
  13.  
  14. #include "vim.h"
  15.  
  16. /*
  17.  * Structure that stores information about a highlight group.
  18.  * The ID of a highlight group is also called group ID.  It is the index in
  19.  * the highlight_ga array PLUS ONE.
  20.  */
  21. struct hl_group
  22. {
  23.     char_u    *sg_name;    /* highlight group name */
  24.     char_u    *sg_name_u;    /* uppercase of sg_name */
  25. /* for normal terminals */
  26.     int        sg_term;    /* "term=" highlighting attributes */
  27.     char_u    *sg_start;    /* terminal string for start highl */
  28.     char_u    *sg_stop;    /* terminal string for stop highl */
  29.     int        sg_term_attr;    /* Screen attr for term mode */
  30. /* for color terminals */
  31.     int        sg_cterm;    /* "cterm=" highlighting attr */
  32.     int        sg_cterm_bold;    /* bold attr was set for light color */
  33.     int        sg_cterm_fg;    /* terminal fg color number + 1 */
  34.     int        sg_cterm_bg;    /* terminal bg color number + 1 */
  35.     int        sg_cterm_attr;    /* Screen attr for color term mode */
  36. #ifdef FEAT_GUI
  37. /* for when using the GUI */
  38.     int        sg_gui;        /* "gui=" highlighting attributes */
  39.     guicolor_T    sg_gui_fg;    /* GUI foreground color handle */
  40.     char_u    *sg_gui_fg_name;/* GUI foreground color name */
  41.     guicolor_T    sg_gui_bg;    /* GUI background color handle */
  42.     char_u    *sg_gui_bg_name;/* GUI background color name */
  43.     GuiFont    sg_font;    /* GUI font handle */
  44. #ifdef FEAT_XFONTSET
  45.     GuiFontset    sg_fontset;    /* GUI fontset handle */
  46. #endif
  47.     char_u    *sg_font_name;  /* GUI font or fontset name */
  48.     int        sg_gui_attr;    /* Screen attr for GUI mode */
  49. #endif
  50.     int        sg_link;    /* link to this highlight group ID */
  51.     int        sg_set;        /* combination of SG_* flags */
  52. };
  53.  
  54. #define SG_TERM        1    /* term has been set */
  55. #define SG_CTERM    2    /* cterm has been set */
  56. #define SG_GUI        4    /* gui has been set */
  57. #define SG_LINK        8    /* link has been set */
  58.  
  59. static garray_T highlight_ga;    /* highlight groups for 'highlight' option */
  60.  
  61. #define HL_TABLE() ((struct hl_group *)((highlight_ga.ga_data)))
  62.  
  63. #ifdef FEAT_CMDL_COMPL
  64. static int include_default = FALSE;    /* include "default" for expansion */
  65. static int include_link = FALSE;    /* include "link" for expansion */
  66. #endif
  67.  
  68. /*
  69.  * The "term", "cterm" and "gui" arguments can be any combination of the
  70.  * following names, separated by commas (but no spaces!).
  71.  */
  72. static char *(hl_name_table[]) =
  73.     {"bold", "standout", "underline", "italic", "reverse", "inverse", "NONE"};
  74. static int hl_attr_table[] =
  75.     {HL_BOLD, HL_STANDOUT, HL_UNDERLINE, HL_ITALIC, HL_INVERSE, HL_INVERSE, 0};
  76.  
  77. static int get_attr_entry  __ARGS((garray_T *table, attrentry_T *aep));
  78. static void syn_unadd_group __ARGS((void));
  79. static void set_hl_attr __ARGS((int idx));
  80. static void highlight_list_one __ARGS((int id));
  81. static int highlight_list_arg __ARGS((int id, int didh, int type, int iarg, char_u *sarg, char *name));
  82. static int syn_add_group __ARGS((char_u *name));
  83. static int syn_list_header __ARGS((int did_header, int outlen, int id));
  84. static int hl_has_settings __ARGS((int idx, int check_link));
  85. static void highlight_clear __ARGS((int idx));
  86.  
  87. #ifdef FEAT_GUI
  88. static void gui_do_one_color __ARGS((int idx, int do_menu, int do_tooltip));
  89. static int  set_group_colors __ARGS((char_u *name, guicolor_T *fgp, guicolor_T *bgp, int do_menu, int use_norm, int do_tooltip));
  90. static guicolor_T color_name2handle __ARGS((char_u *name));
  91. static GuiFont font_name2handle __ARGS((char_u *name));
  92. # ifdef FEAT_XFONTSET
  93. static GuiFontset fontset_name2handle __ARGS((char_u *name, int fixed_width));
  94. # endif
  95. static void hl_do_font __ARGS((int idx, char_u *arg, int do_normal, int do_menu, int do_tooltip));
  96. #endif
  97.  
  98. /*
  99.  * An attribute number is the index in attr_table plus ATTR_OFF.
  100.  */
  101. #define ATTR_OFF (HL_ALL + 1)
  102.  
  103. #if defined(FEAT_SYN_HL) || defined(PROTO)
  104.  
  105. #define SYN_NAMELEN    50        /* maximum length of a syntax name */
  106.  
  107. /* different types of offsets that are possible */
  108. #define SPO_MS_OFF    0    /* match  start offset */
  109. #define SPO_ME_OFF    1    /* match  end    offset */
  110. #define SPO_HS_OFF    2    /* highl. start offset */
  111. #define SPO_HE_OFF    3    /* highl. end    offset */
  112. #define SPO_RS_OFF    4    /* region start offset */
  113. #define SPO_RE_OFF    5    /* region end    offset */
  114. #define SPO_LC_OFF    6    /* leading context offset */
  115. #define SPO_COUNT    7
  116.  
  117. static char *(spo_name_tab[SPO_COUNT]) =
  118.         {"ms=", "me=", "hs=", "he=", "rs=", "re=", "lc="};
  119.  
  120. /*
  121.  * The patterns that are being searched for are stored in a syn_pattern.
  122.  * A match item consists of one pattern.
  123.  * A start/end item consists of n start patterns and m end patterns.
  124.  * A start/skip/end item consists of n start patterns, one skip pattern and m
  125.  * end patterns.
  126.  * For the latter two, the patterns are always consecutive: start-skip-end.
  127.  *
  128.  * A character offset can be given for the matched text (_m_start and _m_end)
  129.  * and for the actually highlighted text (_h_start and _h_end).
  130.  */
  131. typedef struct syn_pattern
  132. {
  133.     char     sp_type;        /* see SPTYPE_ defines below */
  134.     char     sp_syncing;        /* this item used for syncing */
  135.     short     sp_flags;        /* see HL_ defines below */
  136.     struct sp_syn sp_syn;        /* struct passed to in_id_list() */
  137.     short     sp_syn_match_id;    /* highlight group ID of pattern */
  138.     char_u    *sp_pattern;        /* regexp to match, pattern */
  139.     regprog_T    *sp_prog;        /* regexp to match, program */
  140.     int         sp_ic;            /* ignore-case flag for sp_prog */
  141.     short     sp_off_flags;        /* see below */
  142.     int         sp_offsets[SPO_COUNT];    /* offsets */
  143.     short    *sp_cont_list;        /* cont. group IDs, if non-zero */
  144.     short    *sp_next_list;        /* next group IDs, if non-zero */
  145.     int         sp_sync_idx;        /* sync item index (syncing only) */
  146.     int         sp_line_id;        /* ID of last line where tried */
  147.     int         sp_startcol;        /* next match in sp_line_id line */
  148. } synpat_T;
  149.  
  150. /* The sp_off_flags are computed like this:
  151.  * offset from the start of the matched text: (1 << SPO_XX_OFF)
  152.  * offset from the end     of the matched text: (1 << (SPO_XX_OFF + SPO_COUNT))
  153.  * When both are present, only one is used.
  154.  */
  155.  
  156. #define SPTYPE_MATCH    1    /* match keyword with this group ID */
  157. #define SPTYPE_START    2    /* match a regexp, start of item */
  158. #define SPTYPE_END    3    /* match a regexp, end of item */
  159. #define SPTYPE_SKIP    4    /* match a regexp, skip within item */
  160.  
  161. #define HL_CONTAINED    0x01    /* not used on toplevel */
  162. #define HL_TRANSP    0x02    /* has no highlighting    */
  163. #define HL_ONELINE    0x04    /* match within one line only */
  164. #define HL_HAS_EOL    0x08    /* end pattern that matches with $ */
  165. #define HL_SYNC_HERE    0x10    /* sync point after this item (syncing only) */
  166. #define HL_SYNC_THERE    0x20    /* sync point at current line (syncing only) */
  167. #define HL_MATCH    0x40    /* use match ID instead of item ID */
  168. #define HL_SKIPNL    0x80    /* nextgroup can skip newlines */
  169. #define HL_SKIPWHITE    0x100    /* nextgroup can skip white space */
  170. #define HL_SKIPEMPTY    0x200    /* nextgroup can skip empty lines */
  171. #define HL_KEEPEND    0x400    /* end match always kept */
  172. #define HL_EXCLUDENL    0x800    /* exclude NL from match */
  173. #define HL_DISPLAY    0x1000    /* only used for displaying, not syncing */
  174. #define HL_FOLD        0x2000    /* define fold */
  175. #define HL_EXTEND    0x4000    /* ignore a keepend */
  176. /* These don't fit in a short, thus can't be used for syntax items, only for
  177.  * si_flags and bs_flags. */
  178. #define HL_MATCHCONT    0x8000    /* match continued from previous line */
  179. #define HL_TRANS_CONT    0x10000L /* transparent item without contains arg */
  180.  
  181. #define SYN_ITEMS(buf)    ((synpat_T *)((buf)->b_syn_patterns.ga_data))
  182.  
  183. #define NONE_IDX    -2    /* value of sp_sync_idx for "NONE" */
  184.  
  185. /*
  186.  * Flags for b_syn_sync_flags:
  187.  */
  188. #define SF_CCOMMENT    0x01    /* sync on a C-style comment */
  189. #define SF_MATCH    0x02    /* sync by matching a pattern */
  190.  
  191. #define SYN_STATE_P(ssp)    ((bufstate_T *)((ssp)->ga_data))
  192.  
  193. /*
  194.  * Settings for keyword hash table.  It uses a simplistic hash function: add
  195.  * all characters together, modulo KHASH_SIZE.
  196.  */
  197. #define KHASH_SIZE    512
  198. #define KHASH_MASK    (KHASH_SIZE - 1)
  199. #define MAXKEYWLEN    80        /* maximum length of a keyword */
  200.  
  201. /*
  202.  * The attributes of the syntax item that has been recognized.
  203.  */
  204. static int current_attr = 0;        /* attr of current syntax word */
  205. #ifdef FEAT_EVAL
  206. static int current_id = 0;        /* ID of current char for syn_get_id() */
  207. static int current_trans_id = 0;    /* idem, transparancy removed */
  208. #endif
  209.  
  210. struct syn_cluster
  211. {
  212.     char_u        *scl_name;        /* syntax cluster name */
  213.     char_u        *scl_name_u;    /* uppercase of scl_name */
  214.     short        *scl_list;        /* IDs in this syntax cluster */
  215. };
  216.  
  217. /*
  218.  * Methods of combining two clusters
  219.  */
  220. #define CLUSTER_REPLACE        1    /* replace first list with second */
  221. #define CLUSTER_ADD        2    /* add second list to first */
  222. #define CLUSTER_SUBTRACT    3    /* subtract second list from first */
  223.  
  224. #define SYN_CLSTR(buf)    ((struct syn_cluster *)((buf)->b_syn_clusters.ga_data))
  225.  
  226. /*
  227.  * Syntax group IDs have different types:
  228.  *     0 -  9999  normal syntax groups
  229.  * 10000 - 14999  ALLBUT indicator (current_syn_inc_tag added)
  230.  * 15000 - 19999  TOP indicator (current_syn_inc_tag added)
  231.  * 20000 - 24999  CONTAINED indicator (current_syn_inc_tag added)
  232.  * >= 25000      cluster IDs (subtract SYNID_CLUSTER for the cluster ID)
  233.  */
  234. #define SYNID_ALLBUT    10000        /* syntax group ID for contains=ALLBUT */
  235. #define SYNID_TOP    15000        /* syntax group ID for contains=TOP */
  236. #define SYNID_CONTAINED    20000        /* syntax group ID for contains=CONTAINED */
  237. #define SYNID_CLUSTER    25000        /* first syntax group ID for clusters */
  238.  
  239. /*
  240.  * Annoying Hack(TM):  ":syn include" needs this pointer to pass to
  241.  * expand_filename().  Most of the other syntax commands don't need it, so
  242.  * instead of passing it to them, we stow it here.
  243.  */
  244. static char_u **syn_cmdlinep;
  245.  
  246. /*
  247.  * Another Annoying Hack(TM):  To prevent rules from other ":syn include"'d
  248.  * files from from leaking into ALLBUT lists, we assign a unique ID to the
  249.  * rules in each ":syn include"'d file.
  250.  */
  251. static int current_syn_inc_tag = 0;
  252. static int running_syn_inc_tag = 0;
  253.  
  254. /*
  255.  * To reduce the time spent in keepend(), remember at which level in the state
  256.  * stack the first item with "keepend" is present.  When "-1", there is no
  257.  * "keepend" on the stack.
  258.  */
  259. static int keepend_level = -1;
  260.  
  261. /*
  262.  * For the current state we need to remember more than just the idx.
  263.  * When si_m_endpos.lnum is 0, the items other than si_idx are unknown.
  264.  * (The end positions have the column number of the next char)
  265.  */
  266. typedef struct state_item
  267. {
  268.     int        si_idx;            /* index of syntax pattern */
  269.     int        si_id;            /* highlight group ID for keywords */
  270.     int        si_trans_id;        /* idem, transparancy removed */
  271.     int        si_m_lnum;        /* lnum of the match */
  272.     int        si_m_startcol;        /* starting column of the match */
  273.     lpos_T    si_m_endpos;        /* just after end posn of the match */
  274.     lpos_T    si_h_startpos;        /* start position of the highlighting */
  275.     lpos_T    si_h_endpos;        /* end position of the highlighting */
  276.     lpos_T    si_eoe_pos;        /* end position of end pattern */
  277.     int        si_end_idx;        /* group ID for end pattern or zero */
  278.     int        si_ends;        /* if match ends before si_m_endpos */
  279.     int        si_attr;        /* attributes in this state */
  280.     long    si_flags;        /* HL_HAS_EOL flag in this state, and
  281.                      * HL_SKIP* for si_next_list */
  282.     short    *si_cont_list;        /* list of contained groups */
  283.     short    *si_next_list;        /* nextgroup IDs after this item ends */
  284.     reg_extmatch_T *si_extmatch;    /* \z(...\) matches from start
  285.                      * pattern */
  286. } stateitem_T;
  287.  
  288. #define KEYWORD_IDX    -1        /* value of si_idx for keywords */
  289. #define ID_LIST_ALL    (short *)-1 /* valid of si_cont_list for containing all
  290.                        but contained groups */
  291.  
  292. /*
  293.  * The next possible match in the current line for any pattern is remembered,
  294.  * to avoid having to try for a match in each column.
  295.  * If next_match_idx == -1, not tried (in this line) yet.
  296.  * If next_match_col == MAXCOL, no match found in this line.
  297.  * (All end positions have the column of the char after the end)
  298.  */
  299. static int next_match_col;        /* column for start of next match */
  300. static lpos_T next_match_m_endpos;    /* position for end of next match */
  301. static lpos_T next_match_h_startpos;  /* pos. for highl. start of next match */
  302. static lpos_T next_match_h_endpos;    /* pos. for highl. end of next match */
  303. static int next_match_idx;        /* index of matched item */
  304. static long next_match_flags;        /* flags for next match */
  305. static lpos_T next_match_eos_pos;    /* end of start pattn (start region) */
  306. static lpos_T next_match_eoe_pos;    /* pos. for end of end pattern */
  307. static int next_match_end_idx;        /* ID of group for end pattn or zero */
  308. static reg_extmatch_T *next_match_extmatch = NULL;
  309.  
  310. /*
  311.  * A state stack is an array of integers or stateitem_T, stored in a
  312.  * garray_T.  A state stack is invalid if it's itemsize entry is zero.
  313.  */
  314. #define INVALID_STATE(ssp)  ((ssp)->ga_itemsize == 0)
  315. #define VALID_STATE(ssp)    ((ssp)->ga_itemsize != 0)
  316.  
  317. /*
  318.  * The current state (within the line) of the recognition engine.
  319.  * When current_state.ga_itemsize is 0 the current state is invalid.
  320.  */
  321. static win_T    *syn_win;        /* current window for highlighting */
  322. static buf_T    *syn_buf;        /* current buffer for highlighting */
  323. static linenr_T current_lnum = 0;    /* lnum of current state */
  324. static colnr_T    current_col = 0;    /* column of current state */
  325. static int    current_state_stored = 0; /* TRUE if stored current state
  326.                        * after setting current_finished */
  327. static int    current_finished = 0;    /* current line has been finished */
  328. static garray_T current_state        /* current stack of state_items */
  329.         = {0, 0, 0, 0, NULL};
  330. static short    *current_next_list = NULL; /* when non-zero, nextgroup list */
  331. static int    current_next_flags = 0; /* flags for current_next_list */
  332. static int    current_line_id = 0;    /* unique number for current line */
  333.  
  334. #define CUR_STATE(idx)    ((stateitem_T *)(current_state.ga_data))[idx]
  335.  
  336. static void syn_sync __ARGS((win_T *wp, linenr_T lnum, synstate_T *last_valid));
  337. static int syn_match_linecont __ARGS((linenr_T lnum));
  338. static void syn_start_line __ARGS((void));
  339. static void syn_update_ends __ARGS((int startofline));
  340. static void syn_stack_alloc __ARGS((void));
  341. static int syn_stack_cleanup __ARGS((void));
  342. static void syn_stack_free_entry __ARGS((buf_T *buf, synstate_T *p));
  343. static synstate_T *syn_stack_find_entry __ARGS((linenr_T lnum));
  344. static synstate_T *store_current_state __ARGS((synstate_T *sp));
  345. static void load_current_state __ARGS((synstate_T *from));
  346. static void invalidate_current_state __ARGS((void));
  347. static int syn_stack_equal __ARGS((synstate_T *sp));
  348. static void validate_current_state __ARGS((void));
  349. static int syn_finish_line __ARGS((int syncing));
  350. static int syn_current_attr __ARGS((int syncing, int displaying));
  351. static int did_match_already __ARGS((int idx));
  352. static stateitem_T *push_next_match __ARGS((stateitem_T *cur_si));
  353. static void check_state_ends __ARGS((void));
  354. static void update_si_attr __ARGS((int idx));
  355. static void check_keepend __ARGS((void));
  356. static void update_si_end __ARGS((stateitem_T *sip, int startcol, int force));
  357. static short *copy_id_list __ARGS((short *list));
  358. static int in_id_list __ARGS((stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained));
  359. static int push_current_state __ARGS((int idx));
  360. static void pop_current_state __ARGS((void));
  361.  
  362. 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));
  363. static void clear_syn_state __ARGS((synstate_T *p));
  364. static void clear_current_state __ARGS((void));
  365.  
  366. static void limit_pos __ARGS((lpos_T *pos, lpos_T *limit));
  367. static void limit_pos_zero __ARGS((lpos_T *pos, lpos_T *limit));
  368. static void syn_add_end_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
  369. static void syn_add_start_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
  370. static char_u *syn_getcurline __ARGS((void));
  371. static int syn_regexec __ARGS((regmmatch_T *rmp, linenr_T lnum, colnr_T col));
  372. static int check_keyword_id __ARGS((char_u *line, int startcol, int *endcol, long *flags, short **next_list, stateitem_T *cur_si));
  373. static void syn_cmd_case __ARGS((exarg_T *eap, int syncing));
  374. static void syntax_sync_clear __ARGS((void));
  375. static void syn_remove_pattern __ARGS((buf_T *buf, int idx));
  376. static void syn_clear_pattern __ARGS((buf_T *buf, int i));
  377. static void syn_clear_cluster __ARGS((buf_T *buf, int i));
  378. static void syn_cmd_clear __ARGS((exarg_T *eap, int syncing));
  379. static void syn_clear_one __ARGS((int id, int syncing));
  380. static void syn_cmd_on __ARGS((exarg_T *eap, int syncing));
  381. static void syn_cmd_enable __ARGS((exarg_T *eap, int syncing));
  382. static void syn_cmd_reset __ARGS((exarg_T *eap, int syncing));
  383. static void syn_cmd_manual __ARGS((exarg_T *eap, int syncing));
  384. static void syn_cmd_off __ARGS((exarg_T *eap, int syncing));
  385. static void syn_cmd_onoff __ARGS((exarg_T *eap, char *name));
  386. static void syn_cmd_list __ARGS((exarg_T *eap, int syncing));
  387. static void syn_lines_msg __ARGS((void));
  388. static void syn_match_msg __ARGS((void));
  389. static void syn_list_one __ARGS((int id, int syncing, int link_only));
  390. static void syn_list_cluster __ARGS((int id));
  391. static void put_id_list __ARGS((char_u *name, short *list, int attr));
  392. static void put_pattern __ARGS((char *s, int c, synpat_T *spp, int attr));
  393. static int syn_list_keywords __ARGS((int id, keyentry_T **ktabp, int did_header, int attr));
  394. static void syn_clear_keyword __ARGS((int id, keyentry_T **ktabp));
  395. static void free_keywtab __ARGS((keyentry_T **ktabp));
  396. static void add_keyword __ARGS((char_u *name, int id, int flags, short *cont_in_list, short *next_list));
  397. static int syn_khash __ARGS((char_u *p));
  398. static char_u *get_group_name __ARGS((char_u *arg, char_u **name_end));
  399. static char_u *get_syn_options __ARGS((char_u *arg, int *flagsp, int keyword, int *sync_idx, short **cont_list, short **cont_in_list, short **next_list));
  400. static void syn_cmd_include __ARGS((exarg_T *eap, int syncing));
  401. static void syn_cmd_keyword __ARGS((exarg_T *eap, int syncing));
  402. static void syn_cmd_match __ARGS((exarg_T *eap, int syncing));
  403. static void syn_cmd_region __ARGS((exarg_T *eap, int syncing));
  404. #ifdef __BORLANDC__
  405. static int _RTLENTRYF syn_compare_stub __ARGS((const void *v1, const void *v2));
  406. #else
  407. static int syn_compare_stub __ARGS((const void *v1, const void *v2));
  408. #endif
  409. static void syn_cmd_cluster __ARGS((exarg_T *eap, int syncing));
  410. static int syn_scl_name2id __ARGS((char_u *name));
  411. static int syn_scl_namen2id __ARGS((char_u *linep, int len));
  412. static int syn_check_cluster __ARGS((char_u *pp, int len));
  413. static int syn_add_cluster __ARGS((char_u *name));
  414. static void init_syn_patterns __ARGS((void));
  415. static char_u *get_syn_pattern __ARGS((char_u *arg, synpat_T *ci));
  416. static void syn_cmd_sync __ARGS((exarg_T *eap, int syncing));
  417. static int get_id_list __ARGS((char_u **arg, int keylen, short **list));
  418. static void syn_combine_list __ARGS((short **clstr1, short **clstr2, int list_op));
  419. static void syn_incl_toplevel __ARGS((int id, int *flagsp));
  420.  
  421. /*
  422.  * Start the syntax recognition for a line.  This function is normally called
  423.  * from the screen updating, once for each displayed line.
  424.  * The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
  425.  * it.    Careful: curbuf and curwin are likely to point to another buffer and
  426.  * window.
  427.  */
  428.     void
  429. syntax_start(wp, lnum)
  430.     win_T    *wp;
  431.     linenr_T    lnum;
  432. {
  433.     synstate_T    *p;
  434.     synstate_T    *last_valid = NULL;
  435.     synstate_T    *last_min_valid = NULL;
  436.     synstate_T    *sp, *prev;
  437.     linenr_T    parsed_lnum;
  438.     linenr_T    first_stored;
  439.     int        dist;
  440.  
  441.     reg_syn = TRUE;    /* let vim_regexec() know we're using syntax */
  442.  
  443.     /*
  444.      * After switching buffers, invalidate current_state.
  445.      */
  446.     if (syn_buf != wp->w_buffer)
  447.     {
  448.     invalidate_current_state();
  449.     syn_buf = wp->w_buffer;
  450.     }
  451.     syn_win = wp;
  452.  
  453.     /*
  454.      * Allocate syntax stack when needed.
  455.      */
  456.     syn_stack_alloc();
  457.     if (syn_buf->b_sst_array == NULL)
  458.     goto theend;        /* out of memory */
  459.     syn_buf->b_sst_lasttick = display_tick;
  460.  
  461.     /*
  462.      * If the state of the end of the previous line is useful, store it.
  463.      */
  464.     if (VALID_STATE(¤t_state)
  465.         && current_lnum < lnum
  466.         && current_lnum < syn_buf->b_ml.ml_line_count)
  467.     {
  468.     (void)syn_finish_line(FALSE);
  469.     if (!current_state_stored)
  470.     {
  471.         ++current_lnum;
  472.         (void)store_current_state(NULL);
  473.     }
  474.  
  475.     /*
  476.      * If the current_lnum is now the same as "lnum", keep the current
  477.      * state (this happens very often!).  Otherwise invalidate
  478.      * current_state and figure it out below.
  479.      */
  480.     if (current_lnum != lnum)
  481.         invalidate_current_state();
  482.     }
  483.     else
  484.     invalidate_current_state();
  485.  
  486.     /*
  487.      * Try to synchronize from a saved state in b_sst_array[].
  488.      * Only do this if lnum is not before and not to far beyond a saved state.
  489.      */
  490.     if (INVALID_STATE(¤t_state) && syn_buf->b_sst_array != NULL)
  491.     {
  492.     /* Find last valid saved state before start_lnum. */
  493.     for (p = syn_buf->b_sst_first; p != NULL; p = p->sst_next)
  494.     {
  495.         if (p->sst_lnum > lnum)
  496.         break;
  497.         if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
  498.         {
  499.         last_valid = p;
  500.         if (p->sst_lnum >= lnum - syn_buf->b_syn_sync_minlines)
  501.             last_min_valid = p;
  502.         }
  503.     }
  504.     if (last_min_valid != NULL)
  505.         load_current_state(last_min_valid);
  506.     }
  507.  
  508.     /*
  509.      * If "lnum" is before or far beyond a line with a saved state, need to
  510.      * re-synchronize.
  511.      */
  512.     if (INVALID_STATE(¤t_state))
  513.     {
  514.     syn_sync(wp, lnum, last_valid);
  515.     first_stored = current_lnum + syn_buf->b_syn_sync_minlines;
  516.     }
  517.     else
  518.     first_stored = current_lnum;
  519.  
  520.     /*
  521.      * Advance from the sync point or saved state until the current line.
  522.      * Save some entries for syncing with later on.
  523.      */
  524.     dist = syn_buf->b_ml.ml_line_count / (syn_buf->b_sst_len - Rows) + 1;
  525.     prev = syn_stack_find_entry(current_lnum);
  526.     while (current_lnum < lnum)
  527.     {
  528.     syn_start_line();
  529.     (void)syn_finish_line(FALSE);
  530.     ++current_lnum;
  531.  
  532.     /* If we parsed at least "minlines" lines or started at a valid
  533.      * state, the current state is considered valid. */
  534.     if (current_lnum >= first_stored)
  535.     {
  536.         /* Check if the saved state entry is for the current line and is
  537.          * equal to the current state.  If so, then validate all saved
  538.          * states that depended on a change before the parsed line. */
  539.         if (prev == NULL)
  540.         sp = syn_buf->b_sst_first;
  541.         else
  542.         sp = prev->sst_next;
  543.         if (sp != NULL
  544.             && sp->sst_lnum == current_lnum
  545.             && syn_stack_equal(sp))
  546.         {
  547.         parsed_lnum = current_lnum;
  548.         prev = sp;
  549.         while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
  550.         {
  551.             if (sp->sst_lnum <= lnum)
  552.             /* valid state before desired line, use this one */
  553.             prev = sp;
  554.             else if (sp->sst_change_lnum == 0)
  555.             /* past saved states depending on change, break here. */
  556.             break;
  557.             sp->sst_change_lnum = 0;
  558.             sp = sp->sst_next;
  559.         }
  560.         load_current_state(prev);
  561.         }
  562.         /* Store the state at this line when it's the first one, the line
  563.          * where we start parsing, or some distance from the previously
  564.          * saved state.  But only when parsed at least 'minlines'. */
  565.         else if (prev == NULL
  566.             || current_lnum == lnum
  567.             || current_lnum >= prev->sst_lnum + dist)
  568.         prev = store_current_state(prev);
  569.     }
  570.  
  571.     /* This can take a long time: break when CTRL-C pressed.  The current
  572.      * state will be wrong then. */
  573.     line_breakcheck();
  574.     if (got_int)
  575.     {
  576.         current_lnum = lnum;
  577.         break;
  578.     }
  579.     }
  580.  
  581.     syn_start_line();
  582.  
  583. theend:
  584.     reg_syn = FALSE;
  585. }
  586.  
  587. /*
  588.  * We cannot simply discard growarrays full of state_items or buf_states; we
  589.  * have to manually release their extmatch pointers first.
  590.  */
  591.     static void
  592. clear_syn_state(p)
  593.     synstate_T *p;
  594. {
  595.     int        i;
  596.     garray_T    *gap;
  597.  
  598.     if (p->sst_stacksize > SST_FIX_STATES)
  599.     {
  600.     gap = &(p->sst_union.sst_ga);
  601.     for (i = 0; i < gap->ga_len; i++)
  602.         unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
  603.     ga_clear(gap);
  604.     }
  605.     else
  606.     {
  607.     for (i = 0; i < p->sst_stacksize; i++)
  608.         unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
  609.     }
  610. }
  611.  
  612. /*
  613.  * Cleanup the current_state stack.
  614.  */
  615.     static void
  616. clear_current_state()
  617. {
  618.     int        i;
  619.     stateitem_T    *sip;
  620.  
  621.     sip = (stateitem_T *)(current_state.ga_data);
  622.     for (i = 0; i < current_state.ga_len; i++)
  623.     unref_extmatch(sip[i].si_extmatch);
  624.     ga_clear(¤t_state);
  625. }
  626.  
  627. /*
  628.  * Try to find a synchronisation point for line "lnum".
  629.  *
  630.  * This sets current_lnum and the current state.  One of three methods is
  631.  * used:
  632.  * 1. Search backwards for the end of a C-comment.
  633.  * 2. Search backwards for given sync patterns.
  634.  * 3. Simply start on a given number of lines above "lnum".
  635.  */
  636.     static void
  637. syn_sync(wp, start_lnum, last_valid)
  638.     win_T    *wp;
  639.     linenr_T    start_lnum;
  640.     synstate_T    *last_valid;
  641. {
  642.     buf_T    *curbuf_save;
  643.     win_T    *curwin_save;
  644.     pos_T    cursor_save;
  645.     int        idx;
  646.     linenr_T    lnum;
  647.     linenr_T    end_lnum;
  648.     linenr_T    break_lnum;
  649.     int        had_sync_point;
  650.     stateitem_T    *cur_si;
  651.     synpat_T    *spp;
  652.     char_u    *line;
  653.     int        found_flags = 0;
  654.     int        found_match_idx = 0;
  655.     linenr_T    found_current_lnum = 0;
  656.     int        found_current_col= 0;
  657.     lpos_T    found_m_endpos;
  658.  
  659.     /*
  660.      * Clear any current state that might be hanging around.
  661.      */
  662.     invalidate_current_state();
  663.  
  664.     /*
  665.      * Start at least "minlines" back.  Default starting point for parsing is
  666.      * there.
  667.      * Start further back, to avoid that scrolling backwards will result in
  668.      * resyncing for every line.  Now it resyncs only one out of N lines,
  669.      * where N is minlines * 1.5, or minlines * 2 if minlines is small.
  670.      * Watch out for overflow when minlines is MAXLNUM.
  671.      */
  672.     if (syn_buf->b_syn_sync_minlines > start_lnum)
  673.     start_lnum = 1;
  674.     else
  675.     {
  676.     if (syn_buf->b_syn_sync_minlines == 1)
  677.         lnum = 1;
  678.     else if (syn_buf->b_syn_sync_minlines < 10)
  679.         lnum = syn_buf->b_syn_sync_minlines * 2;
  680.     else
  681.         lnum = syn_buf->b_syn_sync_minlines * 3 / 2;
  682.     if (syn_buf->b_syn_sync_maxlines != 0
  683.                        && lnum > syn_buf->b_syn_sync_maxlines)
  684.         lnum = syn_buf->b_syn_sync_maxlines;
  685.     if (lnum >= start_lnum)
  686.         start_lnum = 1;
  687.     else
  688.         start_lnum -= lnum;
  689.     }
  690.     current_lnum = start_lnum;
  691.  
  692.     /*
  693.      * 1. Search backwards for the end of a C-style comment.
  694.      */
  695.     if (syn_buf->b_syn_sync_flags & SF_CCOMMENT)
  696.     {
  697.     /* Need to make syn_buf the current buffer for a moment, to be able to
  698.      * use find_start_comment(). */
  699.     curwin_save = curwin;
  700.     curwin = wp;
  701.     curbuf_save = curbuf;
  702.     curbuf = syn_buf;
  703.  
  704.     /*
  705.      * Skip lines that end in a backslash.
  706.      */
  707.     for ( ; start_lnum > 1; --start_lnum)
  708.     {
  709.         line = ml_get(start_lnum - 1);
  710.         if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
  711.         break;
  712.     }
  713.     current_lnum = start_lnum;
  714.  
  715.     /* set cursor to start of search */
  716.     cursor_save = wp->w_cursor;
  717.     wp->w_cursor.lnum = start_lnum;
  718.     wp->w_cursor.col = 0;
  719.  
  720.     /*
  721.      * If the line is inside a comment, need to find the syntax item that
  722.      * defines the comment.
  723.      * Restrict the search for the end of a comment to b_syn_sync_maxlines.
  724.      */
  725.     if (find_start_comment((int)syn_buf->b_syn_sync_maxlines) != NULL)
  726.     {
  727.         for (idx = syn_buf->b_syn_patterns.ga_len; --idx >= 0; )
  728.         if (SYN_ITEMS(syn_buf)[idx].sp_syn.id == syn_buf->b_syn_sync_id
  729.             && SYN_ITEMS(syn_buf)[idx].sp_type == SPTYPE_START)
  730.         {
  731.             validate_current_state();
  732.             if (push_current_state(idx) == OK)
  733.             update_si_attr(current_state.ga_len - 1);
  734.             break;
  735.         }
  736.     }
  737.  
  738.     /* restore cursor and buffer */
  739.     wp->w_cursor = cursor_save;
  740.     curwin = curwin_save;
  741.     curbuf = curbuf_save;
  742.     }
  743.  
  744.     /*
  745.      * 2. Search backwards for given sync patterns.
  746.      */
  747.     else if (syn_buf->b_syn_sync_flags & SF_MATCH)
  748.     {
  749.     if (syn_buf->b_syn_sync_maxlines != 0
  750.                  && start_lnum > syn_buf->b_syn_sync_maxlines)
  751.         break_lnum = start_lnum - syn_buf->b_syn_sync_maxlines;
  752.     else
  753.         break_lnum = 0;
  754.  
  755.     end_lnum = start_lnum;
  756.     lnum = start_lnum;
  757.     while (--lnum > break_lnum)
  758.     {
  759.         /* This can take a long time: break when CTRL-C pressed. */
  760.         line_breakcheck();
  761.         if (got_int)
  762.         {
  763.         invalidate_current_state();
  764.         current_lnum = start_lnum;
  765.         break;
  766.         }
  767.  
  768.         /* Check if we have run into a valid saved state stack now. */
  769.         if (last_valid != NULL && lnum == last_valid->sst_lnum)
  770.         {
  771.         load_current_state(last_valid);
  772.         break;
  773.         }
  774.  
  775.         /*
  776.          * Check if the previous line has the line-continuation pattern.
  777.          */
  778.         if (lnum > 1 && syn_match_linecont(lnum - 1))
  779.         continue;
  780.  
  781.         /*
  782.          * Start with nothing on the state stack
  783.          */
  784.         validate_current_state();
  785.  
  786.         for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
  787.         {
  788.         syn_start_line();
  789.         for (;;)
  790.         {
  791.             had_sync_point = syn_finish_line(TRUE);
  792.             /*
  793.              * When a sync point has been found, remember where, and
  794.              * continue to look for another one, further on in the line.
  795.              */
  796.             if (had_sync_point && current_state.ga_len)
  797.             {
  798.             cur_si = &CUR_STATE(current_state.ga_len - 1);
  799.             if (cur_si->si_m_endpos.lnum > start_lnum)
  800.             {
  801.                 /* ignore match that goes to after where started */
  802.                 current_lnum = end_lnum;
  803.                 break;
  804.             }
  805.             spp = &(SYN_ITEMS(syn_buf)[cur_si->si_idx]);
  806.             found_flags = spp->sp_flags;
  807.             found_match_idx = spp->sp_sync_idx;
  808.             found_current_lnum = current_lnum;
  809.             found_current_col = current_col;
  810.             found_m_endpos = cur_si->si_m_endpos;
  811.             /*
  812.              * Continue after the match (be aware of a zero-length
  813.              * match).
  814.              */
  815.             if (found_m_endpos.lnum > current_lnum)
  816.             {
  817.                 current_lnum = found_m_endpos.lnum;
  818.                 current_col = found_m_endpos.col;
  819.                 if (current_lnum >= end_lnum)
  820.                 break;
  821.             }
  822.             else if (found_m_endpos.col > current_col)
  823.                 current_col = found_m_endpos.col;
  824.             else
  825.                 ++current_col;
  826.  
  827.             /* syn_current_attr() will have skipped the check for
  828.              * an item that ends here, need to do that now. */
  829.             ++current_col;
  830.             check_state_ends();
  831.             --current_col;
  832.             }
  833.             else
  834.             break;
  835.         }
  836.         }
  837.  
  838.         /*
  839.          * If a sync point was encountered, break here.
  840.          */
  841.         if (found_flags)
  842.         {
  843.         /*
  844.          * Put the item that was specified by the sync point on the
  845.          * state stack.  If there was no item specified, make the
  846.          * state stack empty.
  847.          */
  848.         clear_current_state();
  849.         if (found_match_idx >= 0
  850.             && push_current_state(found_match_idx) == OK)
  851.             update_si_attr(current_state.ga_len - 1);
  852.  
  853.         /*
  854.          * When using "grouphere", continue from the sync point
  855.          * match, until the end of the line.  Parsing starts at
  856.          * the next line.
  857.          * For "groupthere" the parsing starts at start_lnum.
  858.          */
  859.         if (found_flags & HL_SYNC_HERE)
  860.         {
  861.             if (current_state.ga_len)
  862.             {
  863.             cur_si = &CUR_STATE(current_state.ga_len - 1);
  864.             cur_si->si_h_startpos.lnum = found_current_lnum;
  865.             cur_si->si_h_startpos.col = found_current_col;
  866.             update_si_end(cur_si, (int)current_col, TRUE);
  867.             check_keepend();
  868.             }
  869.             current_col = found_m_endpos.col;
  870.             current_lnum = found_m_endpos.lnum;
  871.             (void)syn_finish_line(FALSE);
  872.             ++current_lnum;
  873.         }
  874.         else
  875.             current_lnum = start_lnum;
  876.  
  877.         break;
  878.         }
  879.  
  880.         end_lnum = lnum;
  881.         invalidate_current_state();
  882.     }
  883.  
  884.     /* Ran into start of the file or exceeded maximum number of lines */
  885.     if (lnum <= break_lnum)
  886.     {
  887.         invalidate_current_state();
  888.         current_lnum = break_lnum + 1;
  889.     }
  890.     }
  891.  
  892.     validate_current_state();
  893. }
  894.  
  895. /*
  896.  * Return TRUE if the line-continuation pattern matches in line "lnum".
  897.  */
  898.     static int
  899. syn_match_linecont(lnum)
  900.     linenr_T    lnum;
  901. {
  902.     regmmatch_T regmatch;
  903.  
  904.     if (syn_buf->b_syn_linecont_prog != NULL)
  905.     {
  906.     regmatch.rmm_ic = syn_buf->b_syn_linecont_ic;
  907.     regmatch.regprog = syn_buf->b_syn_linecont_prog;
  908.     return syn_regexec(®match, lnum, (colnr_T)0);
  909.     }
  910.     return FALSE;
  911. }
  912.  
  913. /*
  914.  * Prepare the current state for the start of a line.
  915.  */
  916.     static void
  917. syn_start_line()
  918. {
  919.     current_finished = FALSE;
  920.     current_col = 0;
  921.  
  922.     /*
  923.      * Need to update the end of a start/skip/end that continues from the
  924.      * previous line and regions that have "keepend".
  925.      */
  926.     if (current_state.ga_len > 0)
  927.     syn_update_ends(TRUE);
  928.  
  929.     next_match_idx = -1;
  930.     ++current_line_id;
  931. }
  932.  
  933. /*
  934.  * Check for items in the stack that need their end updated.
  935.  * When "startofline" is TRUE the last item is always updated.
  936.  * When "startofline" is FALSE the item with "keepend" is forcefully updated.
  937.  */
  938.     static void
  939. syn_update_ends(startofline)
  940.     int        startofline;
  941. {
  942.     stateitem_T    *cur_si;
  943.     int        i;
  944.  
  945.     if (startofline)
  946.     {
  947.     /* Check for a match carried over from a previous line with a
  948.      * contained region.  The match ends as soon as the region ends. */
  949.     for (i = 0; i < current_state.ga_len; ++i)
  950.     {
  951.         cur_si = &CUR_STATE(i);
  952.         if (cur_si->si_idx >= 0
  953.             && (SYN_ITEMS(syn_buf)[cur_si->si_idx]).sp_type
  954.                                    == SPTYPE_MATCH
  955.             && cur_si->si_m_endpos.lnum < current_lnum)
  956.         {
  957.         cur_si->si_flags |= HL_MATCHCONT;
  958.         cur_si->si_m_endpos.lnum = 0;
  959.         cur_si->si_m_endpos.col = 0;
  960.         cur_si->si_h_endpos = cur_si->si_m_endpos;
  961.         cur_si->si_ends = TRUE;
  962.         }
  963.     }
  964.     }
  965.  
  966.     /*
  967.      * Need to update the end of a start/skip/end that continues from the
  968.      * previous line.  And regions that have "keepend", because they may
  969.      * influence contained items.
  970.      * Then check for items ending in column 0.
  971.      */
  972.     i = current_state.ga_len - 1;
  973.     if (keepend_level >= 0)
  974.     for ( ; i > keepend_level; --i)
  975.         if (CUR_STATE(i).si_flags & HL_EXTEND)
  976.         break;
  977.     for ( ; i < current_state.ga_len; ++i)
  978.     {
  979.     cur_si = &CUR_STATE(i);
  980.     if ((cur_si->si_flags & HL_KEEPEND)
  981.                 || (i == current_state.ga_len - 1 && startofline))
  982.     {
  983.         cur_si->si_h_startpos.col = 0;    /* start highl. in col 0 */
  984.         cur_si->si_h_startpos.lnum = current_lnum;
  985.  
  986.         if (!(cur_si->si_flags & HL_MATCHCONT))
  987.         update_si_end(cur_si, (int)current_col, !startofline);
  988.     }
  989.     }
  990.     check_keepend();
  991.     check_state_ends();
  992. }
  993.  
  994. /****************************************
  995.  * Handling of the state stack cache.
  996.  */
  997.  
  998. /*
  999.  * EXPLANATION OF THE SYNTAX STATE STACK CACHE
  1000.  *
  1001.  * To speed up syntax highlighting, the state stack for the start of some
  1002.  * lines is cached.  These entries can be used to start parsing at that point.
  1003.  *
  1004.  * The stack is kept in b_sst_array[] for each buffer.  There is a list of
  1005.  * valid entries.  b_sst_first points to the first one, then follow sst_next.
  1006.  * The entries are sorted on line number.  The first entry is often for line 2
  1007.  * (line 1 always starts with an empty stack).
  1008.  * There is also a list for free entries.  This construction is used to avoid
  1009.  * having to allocate and free memory blocks too often.
  1010.  *
  1011.  * When making changes to the buffer, this is logged in b_mod_*.  When calling
  1012.  * update_screen() to update the display, it will call
  1013.  * syn_stack_apply_changes() for each displayed buffer to adjust the cached
  1014.  * entries.  The entries which are inside the changed area are removed,
  1015.  * because they must be recomputed.  Entries below the changed have their line
  1016.  * number adjusted for deleted/inserted lines, and have their sst_change_lnum
  1017.  * set to indicate that a check must be made if the changed lines would change
  1018.  * the cached entry.
  1019.  *
  1020.  * When later displaying lines, an entry is stored for each line.  Displayed
  1021.  * lines are likely to be displayed again, in which case the state at the
  1022.  * start of the line is needed.
  1023.  * For not displayed lines, an entry is stored for every so many lines.  These
  1024.  * entries will be used e.g., when scrolling backwards.  The distance between
  1025.  * entries depends on the number of lines in the buffer.  For small buffers
  1026.  * the distance is fixed at SST_DIST, for large buffers there is a fixed
  1027.  * number of entries SST_MAX_ENTRIES, and the distance is computed.
  1028.  */
  1029.  
  1030. /*
  1031.  * Free b_sst_array[] for buffer "buf".
  1032.  * Used when syntax items changed to force resyncing everywhere.
  1033.  */
  1034.     void
  1035. syn_stack_free_all(buf)
  1036.     buf_T    *buf;
  1037. {
  1038.     synstate_T    *p;
  1039.     win_T    *wp;
  1040.  
  1041.     if (buf->b_sst_array != NULL)
  1042.     {
  1043.     for (p = buf->b_sst_first; p != NULL; p = p->sst_next)
  1044.         clear_syn_state(p);
  1045.     vim_free(buf->b_sst_array);
  1046.     buf->b_sst_array = NULL;
  1047.     buf->b_sst_len = 0;
  1048.     }
  1049. #ifdef FEAT_FOLDING
  1050.     /* When using "syntax" fold method, must update all folds. */
  1051.     FOR_ALL_WINDOWS(wp)
  1052.     {
  1053.     if (wp->w_buffer == buf && foldmethodIsSyntax(wp))
  1054.         foldUpdateAll(wp);
  1055.     }
  1056. #endif
  1057. }
  1058.  
  1059. /*
  1060.  * Allocate the syntax state stack for syn_buf when needed.
  1061.  * If the number of entries in b_sst_array[] is much too big or a bit too
  1062.  * small, reallocate it.
  1063.  * Also used to allocate b_sst_array[] for the first time.
  1064.  */
  1065.     static void
  1066. syn_stack_alloc()
  1067. {
  1068.     long    len;
  1069.     synstate_T    *to, *from;
  1070.     synstate_T    *sstp;
  1071.  
  1072.     len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
  1073.     if (len < SST_MIN_ENTRIES)
  1074.     len = SST_MIN_ENTRIES;
  1075.     else if (len > SST_MAX_ENTRIES)
  1076.     len = SST_MAX_ENTRIES;
  1077.     if (syn_buf->b_sst_len > len * 2 || syn_buf->b_sst_len < len)
  1078.     {
  1079.     /* Allocate 50% too much, to avoid reallocating too often. */
  1080.     len = syn_buf->b_ml.ml_line_count;
  1081.     len = (len + len / 2) / SST_DIST + Rows * 2;
  1082.     if (len < SST_MIN_ENTRIES)
  1083.         len = SST_MIN_ENTRIES;
  1084.     else if (len > SST_MAX_ENTRIES)
  1085.         len = SST_MAX_ENTRIES;
  1086.  
  1087.     if (syn_buf->b_sst_array != NULL)
  1088.     {
  1089.         /* When shrinking the array, cleanup the existing stack.
  1090.          * Make sure that all valid entries fit in the new array. */
  1091.         while (syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2> len
  1092.             && syn_stack_cleanup())
  1093.         ;
  1094.         if (len < syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2)
  1095.         len = syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2;
  1096.     }
  1097.  
  1098.     sstp = (synstate_T *)alloc_clear((unsigned)(len * sizeof(synstate_T)));
  1099.     if (sstp == NULL)    /* out of memory! */
  1100.         return;
  1101.  
  1102.     to = sstp - 1;
  1103.     if (syn_buf->b_sst_array != NULL)
  1104.     {
  1105.         /* Move the states from the old array to the new one. */
  1106.         for (from = syn_buf->b_sst_first; from != NULL;
  1107.                             from = from->sst_next)
  1108.         {
  1109.         ++to;
  1110.         *to = *from;
  1111.         to->sst_next = to + 1;
  1112.         }
  1113.     }
  1114.     if (to != sstp - 1)
  1115.     {
  1116.         to->sst_next = NULL;
  1117.         syn_buf->b_sst_first = sstp;
  1118.         syn_buf->b_sst_freecount = len - (int)(to - sstp) - 1;
  1119.     }
  1120.     else
  1121.     {
  1122.         syn_buf->b_sst_first = NULL;
  1123.         syn_buf->b_sst_freecount = len;
  1124.     }
  1125.  
  1126.     /* Create the list of free entries. */
  1127.     syn_buf->b_sst_firstfree = to + 1;
  1128.     while (++to < sstp + len)
  1129.         to->sst_next = to + 1;
  1130.     (sstp + len - 1)->sst_next = NULL;
  1131.  
  1132.     vim_free(syn_buf->b_sst_array);
  1133.     syn_buf->b_sst_array = sstp;
  1134.     syn_buf->b_sst_len = len;
  1135.     }
  1136. }
  1137.  
  1138. /*
  1139.  * Check for changes in a buffer to affect stored syntax states.  Uses the
  1140.  * b_mod_* fields.
  1141.  * Called from update_screen(), before screen is being updated, once for each
  1142.  * displayed buffer.
  1143.  */
  1144.     void
  1145. syn_stack_apply_changes(buf)
  1146.     buf_T    *buf;
  1147. {
  1148.     synstate_T    *p, *prev, *np;
  1149.     linenr_T    n;
  1150.  
  1151.     if (buf->b_sst_array == NULL)    /* nothing to do */
  1152.     return;
  1153.  
  1154.     prev = NULL;
  1155.     for (p = buf->b_sst_first; p != NULL; )
  1156.     {
  1157.     if (p->sst_lnum + syn_buf->b_syn_sync_linebreaks > buf->b_mod_top)
  1158.     {
  1159.         n = p->sst_lnum + buf->b_mod_xlines;
  1160.         if (n <= buf->b_mod_bot)
  1161.         {
  1162.         /* this state is inside the changed area, remove it */
  1163.         np = p->sst_next;
  1164.         if (prev == NULL)
  1165.             buf->b_sst_first = np;
  1166.         else
  1167.             prev->sst_next = np;
  1168.         syn_stack_free_entry(buf, p);
  1169.         p = np;
  1170.         continue;
  1171.         }
  1172.         /* This state is below the changed area.  Remember the line
  1173.          * that needs to be parsed before this entry can be made valid
  1174.          * again. */
  1175.         if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
  1176.         {
  1177.         if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
  1178.             p->sst_change_lnum += buf->b_mod_xlines;
  1179.         else
  1180.             p->sst_change_lnum = buf->b_mod_top;
  1181.         }
  1182.         if (p->sst_change_lnum == 0
  1183.             || p->sst_change_lnum < buf->b_mod_bot)
  1184.         p->sst_change_lnum = buf->b_mod_bot;
  1185.  
  1186.         p->sst_lnum = n;
  1187.     }
  1188.     prev = p;
  1189.     p = p->sst_next;
  1190.     }
  1191. }
  1192.  
  1193. /*
  1194.  * Reduce the number of entries in the state stack for syn_buf.
  1195.  * Returns TRUE if at least one entry was freed.
  1196.  */
  1197.     static int
  1198. syn_stack_cleanup()
  1199. {
  1200.     synstate_T    *p, *prev;
  1201.     disptick_T    tick;
  1202.     int        above;
  1203.     int        dist;
  1204.     int        retval = FALSE;
  1205.  
  1206.     if (syn_buf->b_sst_array == NULL || syn_buf->b_sst_first == NULL)
  1207.     return retval;
  1208.  
  1209.     /* Compute normal distance between non-displayed entries. */
  1210.     dist = syn_buf->b_ml.ml_line_count / (syn_buf->b_sst_len - Rows) + 1;
  1211.  
  1212.     /*
  1213.      * Go throught the list to find the "tick" for the oldest entry that can
  1214.      * be removed.  Set "above" when the "tick" for the oldest entry is above
  1215.      * "b_sst_lasttick" (the display tick wraps around).
  1216.      */
  1217.     tick = syn_buf->b_sst_lasttick;
  1218.     above = FALSE;
  1219.     prev = syn_buf->b_sst_first;
  1220.     for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
  1221.     {
  1222.     if (prev->sst_lnum + dist > p->sst_lnum)
  1223.     {
  1224.         if (p->sst_tick > syn_buf->b_sst_lasttick)
  1225.         {
  1226.         if (!above || p->sst_tick < tick)
  1227.             tick = p->sst_tick;
  1228.         above = TRUE;
  1229.         }
  1230.         else if (!above && p->sst_tick < tick)
  1231.         tick = p->sst_tick;
  1232.     }
  1233.     }
  1234.  
  1235.     /*
  1236.      * Go through the list to make the entries for the oldest tick at an
  1237.      * interval of several lines.
  1238.      */
  1239.     prev = syn_buf->b_sst_first;
  1240.     for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
  1241.     {
  1242.     if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
  1243.     {
  1244.         /* Move this entry from used list to free list */
  1245.         prev->sst_next = p->sst_next;
  1246.         syn_stack_free_entry(syn_buf, p);
  1247.         p = prev;
  1248.         retval = TRUE;
  1249.     }
  1250.     }
  1251.     return retval;
  1252. }
  1253.  
  1254. /*
  1255.  * Free the allocated memory for a syn_state item.
  1256.  * Move the entry into the free list.
  1257.  */
  1258.     static void
  1259. syn_stack_free_entry(buf, p)
  1260.     buf_T    *buf;
  1261.     synstate_T    *p;
  1262. {
  1263.     clear_syn_state(p);
  1264.     p->sst_next = buf->b_sst_firstfree;
  1265.     buf->b_sst_firstfree = p;
  1266.     ++buf->b_sst_freecount;
  1267. }
  1268.  
  1269. /*
  1270.  * Find an entry in the list of state stacks at or before "lnum".
  1271.  * Returns NULL when there is no entry or the first entry is after "lnum".
  1272.  */
  1273.     static synstate_T *
  1274. syn_stack_find_entry(lnum)
  1275.     linenr_T    lnum;
  1276. {
  1277.     synstate_T    *p, *prev;
  1278.  
  1279.     prev = NULL;
  1280.     for (p = syn_buf->b_sst_first; p != NULL; prev = p, p = p->sst_next)
  1281.     {
  1282.     if (p->sst_lnum == lnum)
  1283.         return p;
  1284.     if (p->sst_lnum > lnum)
  1285.         break;
  1286.     }
  1287.     return prev;
  1288. }
  1289.  
  1290. /*
  1291.  * Try saving the current state in b_sst_array[].
  1292.  * The current state must be valid for the start of the current_lnum line!
  1293.  */
  1294.     static synstate_T *
  1295. store_current_state(sp)
  1296.     synstate_T    *sp;    /* at or before where state is to be saved or
  1297.                    NULL */
  1298. {
  1299.     int        i;
  1300.     synstate_T    *p;
  1301.     bufstate_T    *bp;
  1302.     stateitem_T    *cur_si;
  1303.  
  1304.     if (sp == NULL)
  1305.     sp = syn_stack_find_entry(current_lnum);
  1306.  
  1307.     /*
  1308.      * If the current state contains a start or end pattern that continues
  1309.      * from the previous line, we can't use it.  Don't store it then.
  1310.      */
  1311.     for (i = current_state.ga_len - 1; i >= 0; --i)
  1312.     {
  1313.     cur_si = &CUR_STATE(i);
  1314.     if (cur_si->si_h_startpos.lnum >= current_lnum
  1315.         || cur_si->si_m_endpos.lnum >= current_lnum
  1316.         || cur_si->si_h_endpos.lnum >= current_lnum
  1317.         || (cur_si->si_end_idx
  1318.             && cur_si->si_eoe_pos.lnum >= current_lnum))
  1319.         break;
  1320.     }
  1321.     if (i >= 0)
  1322.     {
  1323.     if (sp != NULL)
  1324.     {
  1325.         /* find the entry just before this one */
  1326.         for (p = syn_buf->b_sst_first; p != NULL; p = p->sst_next)
  1327.         if (p->sst_next == sp)
  1328.             break;
  1329.         if (p != NULL)
  1330.         p->sst_next = sp->sst_next;
  1331.         else
  1332.         syn_buf->b_sst_first = sp->sst_next;
  1333.         syn_stack_free_entry(syn_buf, sp);
  1334.         sp = NULL;
  1335.     }
  1336.     }
  1337.     else if (sp == NULL || sp->sst_lnum != current_lnum)
  1338.     {
  1339.     /*
  1340.      * Add a new entry
  1341.      */
  1342.     /* If no free items, cleanup the array first. */
  1343.     if (syn_buf->b_sst_freecount == 0)
  1344.         (void)syn_stack_cleanup();
  1345.     /* Still no free items?  Must be a strange problem... */
  1346.     if (syn_buf->b_sst_freecount == 0)
  1347.         sp = NULL;
  1348.     else
  1349.     {
  1350.         /* Take the first item from the free list and put it in the used
  1351.          * list, after *sp */
  1352.         p = syn_buf->b_sst_firstfree;
  1353.         syn_buf->b_sst_firstfree = p->sst_next;
  1354.         --syn_buf->b_sst_freecount;
  1355.         if (sp == NULL)
  1356.         {
  1357.         /* Insert in front of the list */
  1358.         p->sst_next = syn_buf->b_sst_first;
  1359.         syn_buf->b_sst_first = p;
  1360.         }
  1361.         else
  1362.         {
  1363.         /* insert in list after *sp */
  1364.         p->sst_next = sp->sst_next;
  1365.         sp->sst_next = p;
  1366.         }
  1367.         sp = p;
  1368.         sp->sst_stacksize = 0;
  1369.         sp->sst_lnum = current_lnum;
  1370.     }
  1371.     }
  1372.     if (sp != NULL)
  1373.     {
  1374.     /* When overwriting an existing state stack, clear it first */
  1375.     clear_syn_state(sp);
  1376.     sp->sst_stacksize = current_state.ga_len;
  1377.     if (current_state.ga_len > SST_FIX_STATES)
  1378.     {
  1379.         /* Need to clear it, might be something remaining from when the
  1380.          * length was less than SST_FIX_STATES. */
  1381.         ga_init2(&sp->sst_union.sst_ga, (int)sizeof(bufstate_T), 1);
  1382.         if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
  1383.         sp->sst_stacksize = 0;
  1384.         else
  1385.         {
  1386.         sp->sst_union.sst_ga.ga_len = current_state.ga_len;
  1387.         sp->sst_union.sst_ga.ga_room -= current_state.ga_len;
  1388.         }
  1389.         bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
  1390.     }
  1391.     else
  1392.         bp = sp->sst_union.sst_stack;
  1393.     for (i = 0; i < sp->sst_stacksize; ++i)
  1394.     {
  1395.         bp[i].bs_idx = CUR_STATE(i).si_idx;
  1396.         bp[i].bs_flags = CUR_STATE(i).si_flags;
  1397.         bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
  1398.     }
  1399.     sp->sst_next_flags = current_next_flags;
  1400.     sp->sst_next_list = current_next_list;
  1401.     sp->sst_tick = display_tick;
  1402.     sp->sst_change_lnum = 0;
  1403.     }
  1404.     current_state_stored = TRUE;
  1405.     return sp;
  1406. }
  1407.  
  1408. /*
  1409.  * Copy a state stack from "from" in b_sst_array[] to current_state;
  1410.  */
  1411.     static void
  1412. load_current_state(from)
  1413.     synstate_T    *from;
  1414. {
  1415.     int        i;
  1416.     bufstate_T    *bp;
  1417.  
  1418.     clear_current_state();
  1419.     validate_current_state();
  1420.     keepend_level = -1;
  1421.     if (from->sst_stacksize
  1422.         && ga_grow(¤t_state, from->sst_stacksize) != FAIL)
  1423.     {
  1424.     if (from->sst_stacksize > SST_FIX_STATES)
  1425.         bp = SYN_STATE_P(&(from->sst_union.sst_ga));
  1426.     else
  1427.         bp = from->sst_union.sst_stack;
  1428.     for (i = 0; i < from->sst_stacksize; ++i)
  1429.     {
  1430.         CUR_STATE(i).si_idx = bp[i].bs_idx;
  1431.         CUR_STATE(i).si_flags = bp[i].bs_flags;
  1432.         CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
  1433.         if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
  1434.         keepend_level = i;
  1435.         CUR_STATE(i).si_ends = FALSE;
  1436.         CUR_STATE(i).si_m_lnum = 0;
  1437.         if (CUR_STATE(i).si_idx >= 0)
  1438.         CUR_STATE(i).si_next_list =
  1439.                (SYN_ITEMS(syn_buf)[CUR_STATE(i).si_idx]).sp_next_list;
  1440.         else
  1441.         CUR_STATE(i).si_next_list = NULL;
  1442.         update_si_attr(i);
  1443.     }
  1444.     current_state.ga_len = from->sst_stacksize;
  1445.     current_state.ga_room -= current_state.ga_len;
  1446.     }
  1447.     current_next_list = from->sst_next_list;
  1448.     current_next_flags = from->sst_next_flags;
  1449.     current_lnum = from->sst_lnum;
  1450. }
  1451.  
  1452. /*
  1453.  * Compare saved state stack "*sp" with the current state.
  1454.  * Return TRUE when they are equal.
  1455.  */
  1456.     static int
  1457. syn_stack_equal(sp)
  1458.     synstate_T *sp;
  1459. {
  1460.     int        i, j;
  1461.     bufstate_T    *bp;
  1462.     reg_extmatch_T    *six, *bsx;
  1463.  
  1464.     /* First a quick check if the stacks have the same size end nextlist. */
  1465.     if (sp->sst_stacksize == current_state.ga_len
  1466.         && sp->sst_next_list == current_next_list)
  1467.     {
  1468.     /* Need to compare all states on both stacks. */
  1469.     if (sp->sst_stacksize > SST_FIX_STATES)
  1470.         bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
  1471.     else
  1472.         bp = sp->sst_union.sst_stack;
  1473.  
  1474.     for (i = current_state.ga_len; --i >= 0; )
  1475.     {
  1476.         /* If the item has another index the state is different. */
  1477.         if (bp[i].bs_idx != CUR_STATE(i).si_idx)
  1478.         break;
  1479.         if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
  1480.         {
  1481.         /* When the extmatch pointers are different, the strings in
  1482.          * them can still be the same.  Check if the extmatch
  1483.          * references are equal. */
  1484.         bsx = bp[i].bs_extmatch;
  1485.         six = CUR_STATE(i).si_extmatch;
  1486.         /* If one of the extmatch pointers is NULL the states are
  1487.          * different. */
  1488.         if (bsx == NULL || six == NULL)
  1489.             break;
  1490.         for (j = 0; j < NSUBEXP; ++j)
  1491.         {
  1492.             /* Check each referenced match string. They must all be
  1493.              * equal. */
  1494.             if (bsx->matches[j] != six->matches[j])
  1495.             {
  1496.             /* If the pointer is different it can still be the
  1497.              * same text.  Compare the strings, ignore case when
  1498.              * the start item has the sp_ic flag set. */
  1499.             if (bsx->matches[j] == NULL
  1500.                 || six->matches[j] == NULL)
  1501.                 break;
  1502.             if ((SYN_ITEMS(syn_buf)[CUR_STATE(i).si_idx]).sp_ic
  1503.                 ? MB_STRICMP(bsx->matches[j],
  1504.                              six->matches[j]) != 0
  1505.                 : STRCMP(bsx->matches[j], six->matches[j]) != 0)
  1506.                 break;
  1507.             }
  1508.         }
  1509.         if (j != NSUBEXP)
  1510.             break;
  1511.         }
  1512.     }
  1513.     if (i < 0)
  1514.         return TRUE;
  1515.     }
  1516.     return FALSE;
  1517. }
  1518.  
  1519. /*
  1520.  * We stop parsing syntax above line "lnum".  If the stored state at or below
  1521.  * this line depended on a change before it, it now depends on the line below
  1522.  * the last parsed line.
  1523.  * The window looks like this:
  1524.  *        line which changed
  1525.  *        displayed line
  1526.  *        displayed line
  1527.  * lnum ->  line below window
  1528.  */
  1529.     void
  1530. syntax_end_parsing(lnum)
  1531.     linenr_T    lnum;
  1532. {
  1533.     synstate_T    *sp;
  1534.  
  1535.     sp = syn_stack_find_entry(lnum);
  1536.     if (sp != NULL && sp->sst_lnum < lnum)
  1537.     sp = sp->sst_next;
  1538.  
  1539.     if (sp != NULL && sp->sst_change_lnum != 0)
  1540.     sp->sst_change_lnum = lnum;
  1541. }
  1542.  
  1543. /*
  1544.  * End of handling of the state stack.
  1545.  ****************************************/
  1546.  
  1547.     static void
  1548. invalidate_current_state()
  1549. {
  1550.     clear_current_state();
  1551.     current_state.ga_itemsize = 0;    /* mark current_state invalid */
  1552.     current_next_list = NULL;
  1553.     keepend_level = -1;
  1554. }
  1555.  
  1556.     static void
  1557. validate_current_state()
  1558. {
  1559.     current_state.ga_itemsize = sizeof(stateitem_T);
  1560.     current_state.ga_growsize = 3;
  1561. }
  1562.  
  1563. /*
  1564.  * Return TRUE if the syntax at start of lnum changed since last time.
  1565.  * This will only be called just after get_syntax_attr() for the previous
  1566.  * line, to check if the next line needs to be redrawn too.
  1567.  */
  1568.     int
  1569. syntax_check_changed(lnum)
  1570.     linenr_T    lnum;
  1571. {
  1572.     int        retval = TRUE;
  1573.     synstate_T    *sp;
  1574.  
  1575.     reg_syn = TRUE;    /* let vim_regexec() know we're using syntax */
  1576.  
  1577.     /*
  1578.      * Check the state stack when:
  1579.      * - lnum is just below the previously syntaxed line.
  1580.      * - lnum is not before the lines with saved states.
  1581.      * - lnum is not past the lines with saved states.
  1582.      * - lnum is at or before the last changed line.
  1583.      */
  1584.     if (VALID_STATE(¤t_state) && lnum == current_lnum + 1)
  1585.     {
  1586.     sp = syn_stack_find_entry(lnum);
  1587.     if (sp != NULL && sp->sst_lnum == lnum)
  1588.     {
  1589.         /*
  1590.          * finish the previous line (needed when not all of the line was
  1591.          * drawn)
  1592.          */
  1593.         (void)syn_finish_line(FALSE);
  1594.  
  1595.         /*
  1596.          * Compare the current state with the previously saved state of
  1597.          * the line.
  1598.          */
  1599.         if (syn_stack_equal(sp))
  1600.         retval = FALSE;
  1601.  
  1602.         /*
  1603.          * Store the current state in b_sst_array[] for later use.
  1604.          */
  1605.         ++current_lnum;
  1606.         (void)store_current_state(NULL);
  1607.     }
  1608.     }
  1609.  
  1610.     reg_syn = FALSE;
  1611.  
  1612.     return retval;
  1613. }
  1614.  
  1615. /*
  1616.  * Finish the current line.
  1617.  * This doesn't return any attributes, it only gets the state at the end of
  1618.  * the line.  It can start anywhere in the line, as long as the current state
  1619.  * is valid.
  1620.  */
  1621.     static int
  1622. syn_finish_line(syncing)
  1623.     int        syncing;        /* called for syncing */
  1624. {
  1625.     stateitem_T    *cur_si;
  1626.  
  1627.     if (!current_finished)
  1628.     {
  1629.     while (!current_finished)
  1630.     {
  1631.         (void)syn_current_attr(syncing, FALSE);
  1632.         /*
  1633.          * When syncing, and found some item, need to check the item.
  1634.          */
  1635.         if (syncing && current_state.ga_len)
  1636.         {
  1637.         /*
  1638.          * Check for match with sync item.
  1639.          */
  1640.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  1641.         if (cur_si->si_idx >= 0
  1642.             && (SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_flags
  1643.                           & (HL_SYNC_HERE|HL_SYNC_THERE)))
  1644.             return TRUE;
  1645.  
  1646.         /* syn_current_attr() will have skipped the check for an item
  1647.          * that ends here, need to do that now. */
  1648.         ++current_col;
  1649.         check_state_ends();
  1650.         --current_col;
  1651.         }
  1652.         ++current_col;
  1653.     }
  1654.     }
  1655.     return FALSE;
  1656. }
  1657.  
  1658. /*
  1659.  * Return highlight attributes for next character.
  1660.  * Must first call syntax_start() once for the line.
  1661.  * "col" is normally 0 for the first use in a line, and increments by one each
  1662.  * time.  It's allowed to skip characters and to stop before the end of the
  1663.  * line.  But only a "col" after a previously used column is allowed.
  1664.  */
  1665.     int
  1666. get_syntax_attr(col)
  1667.     colnr_T    col;
  1668. {
  1669.     int        attr = 0;
  1670.  
  1671.     /* check for out of memory situation */
  1672.     if (syn_buf->b_sst_array == NULL)
  1673.     return 0;
  1674.  
  1675.     reg_syn = TRUE;    /* let vim_regexec() know we're using syntax */
  1676.  
  1677.     /* Make sure current_state is valid */
  1678.     if (INVALID_STATE(¤t_state))
  1679.     validate_current_state();
  1680.  
  1681.     /*
  1682.      * Skip from the current column to "col", get the attributes for "col".
  1683.      */
  1684.     while (current_col <= col)
  1685.     {
  1686.     attr = syn_current_attr(FALSE, TRUE);
  1687.     ++current_col;
  1688.     }
  1689.  
  1690.     reg_syn = FALSE;
  1691.     return attr;
  1692. }
  1693.  
  1694. /*
  1695.  * Get syntax attributes for current_lnum, current_col.
  1696.  */
  1697.     static int
  1698. syn_current_attr(syncing, displaying)
  1699.     int        syncing;        /* When 1: called for syncing */
  1700.     int        displaying;        /* result will be displayed */
  1701. {
  1702.     int        syn_id;
  1703.     lpos_T    endpos;        /* was: char_u *endp; */
  1704.     lpos_T    hl_startpos;    /* was: int hl_startcol; */
  1705.     lpos_T    hl_endpos;
  1706.     lpos_T    eos_pos;    /* end-of-start match (start region) */
  1707.     lpos_T    eoe_pos;    /* end-of-end pattern */
  1708.     int        end_idx;    /* group ID for end pattern */
  1709.     int        idx;
  1710.     synpat_T    *spp;
  1711.     stateitem_T    *cur_si, *sip;
  1712.     int        startcol;
  1713.     int        endcol;
  1714.     long    flags;
  1715.     short    *next_list;
  1716.     int        found_match;            /* found usable match */
  1717.     static int    try_next_column = FALSE;    /* must try in next col */
  1718.     int        do_keywords;
  1719.     regmmatch_T    regmatch;
  1720.     lpos_T    pos;
  1721.     int        lc_col;
  1722.     reg_extmatch_T *cur_extmatch = NULL;
  1723.     char_u    *line;        /* current line.  NOTE: becomes invalid after
  1724.                    looking for a pattern match! */
  1725.     int        keep_next_list;
  1726.     int        zero_width_next_list = FALSE;
  1727.  
  1728.     /*
  1729.      * No character, no attributes!  Past end of line?
  1730.      * Do try matching with an empty line (could be the start of a region).
  1731.      */
  1732.     line = syn_getcurline();
  1733.     if (line[current_col] == NUL && current_col != 0)
  1734.     {
  1735.     /*
  1736.      * If we found a match after the last column, use it.
  1737.      */
  1738.     if (next_match_idx >= 0 && next_match_col >= (int)current_col
  1739.                           && next_match_col != MAXCOL)
  1740.         (void)push_next_match(NULL);
  1741.  
  1742.     current_finished = TRUE;
  1743.     current_state_stored = FALSE;
  1744.     return 0;
  1745.     }
  1746.  
  1747.     /* if the current or next character is NUL, we will finish the line now */
  1748.     if (line[current_col] == NUL || line[current_col + 1] == NUL)
  1749.     {
  1750.     current_finished = TRUE;
  1751.     current_state_stored = FALSE;
  1752.     }
  1753.  
  1754.     /*
  1755.      * When in the previous column there was a match but it could not be used
  1756.      * (empty match or already matched in this column) need to try again in
  1757.      * the next column.
  1758.      */
  1759.     if (try_next_column)
  1760.     {
  1761.     next_match_idx = -1;
  1762.     try_next_column = FALSE;
  1763.     }
  1764.  
  1765.     /* Only check for keywords when not syncing and there are some. */
  1766.     do_keywords = !syncing
  1767.             && (syn_buf->b_keywtab != NULL
  1768.                 || syn_buf->b_keywtab_ic != NULL);
  1769.  
  1770.     /*
  1771.      * Repeat matching keywords and patterns, to find contained items at the
  1772.      * same column.  This stops when there are no extra matches at the current
  1773.      * column.
  1774.      */
  1775.     do
  1776.     {
  1777.     found_match = FALSE;
  1778.     keep_next_list = FALSE;
  1779.     syn_id = 0;
  1780.  
  1781.     /*
  1782.      * 1. Check for a current state.
  1783.      *    Only when there is no current state, or if the current state may
  1784.      *    contain other things, we need to check for keywords and patterns.
  1785.      *    Always need to check for contained items if some item has the
  1786.      *    "containedin" argument (takes extra time!).
  1787.      */
  1788.     if (current_state.ga_len)
  1789.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  1790.     else
  1791.         cur_si = NULL;
  1792.  
  1793.     if (curbuf->b_syn_containedin || cur_si == NULL
  1794.                           || cur_si->si_cont_list != NULL)
  1795.     {
  1796.         /*
  1797.          * 2. Check for keywords, if on a keyword char after a non-keyword
  1798.          *      char.  Don't do this when syncing.
  1799.          */
  1800.         if (do_keywords)
  1801.         {
  1802.           line = syn_getcurline();
  1803.           if (vim_iswordc_buf(line + current_col, syn_buf)
  1804.               && (current_col == 0
  1805.               || !vim_iswordc_buf(line + current_col - 1
  1806. #ifdef FEAT_MBYTE
  1807.                   - (has_mbyte
  1808.                   ? (*mb_head_off)(line, line + current_col - 1)
  1809.                   : 0)
  1810. #endif
  1811.                    , syn_buf)))
  1812.           {
  1813.         syn_id = check_keyword_id(line, (int)current_col,
  1814.                      &endcol, &flags, &next_list, cur_si);
  1815.         if (syn_id)
  1816.         {
  1817.             if (push_current_state(KEYWORD_IDX) == OK)
  1818.             {
  1819.             cur_si = &CUR_STATE(current_state.ga_len - 1);
  1820.             cur_si->si_m_startcol = current_col;
  1821.             cur_si->si_h_startpos.lnum = current_lnum;
  1822.             cur_si->si_h_startpos.col = 0;    /* starts right away */
  1823.             cur_si->si_m_endpos.lnum = current_lnum;
  1824.             cur_si->si_m_endpos.col = endcol;
  1825.             cur_si->si_h_endpos.lnum = current_lnum;
  1826.             cur_si->si_h_endpos.col = endcol;
  1827.             cur_si->si_ends = TRUE;
  1828.             cur_si->si_end_idx = 0;
  1829.             cur_si->si_flags = flags;
  1830.             cur_si->si_id = syn_id;
  1831.             cur_si->si_trans_id = syn_id;
  1832.             if (flags & HL_TRANSP)
  1833.             {
  1834.                 if (current_state.ga_len < 2)
  1835.                 {
  1836.                 cur_si->si_attr = 0;
  1837.                 cur_si->si_trans_id = 0;
  1838.                 }
  1839.                 else
  1840.                 {
  1841.                 cur_si->si_attr = CUR_STATE(
  1842.                     current_state.ga_len - 2).si_attr;
  1843.                 cur_si->si_trans_id = CUR_STATE(
  1844.                     current_state.ga_len - 2).si_trans_id;
  1845.                 }
  1846.             }
  1847.             else
  1848.                 cur_si->si_attr = syn_id2attr(syn_id);
  1849.             cur_si->si_cont_list = NULL;
  1850.             cur_si->si_next_list = next_list;
  1851.             check_keepend();
  1852.             }
  1853.             else
  1854.             vim_free(next_list);
  1855.         }
  1856.           }
  1857.         }
  1858.  
  1859.         /*
  1860.          * 3. Check for patterns (only if not found a keyword).
  1861.          */
  1862.         if (syn_id == 0 && syn_buf->b_syn_patterns.ga_len)
  1863.         {
  1864.         /*
  1865.          * If we didn't check for a match yet, or we are past it, check
  1866.          * for any match with a pattern.
  1867.          */
  1868.         if (next_match_idx < 0 || next_match_col < (int)current_col)
  1869.         {
  1870.             /*
  1871.              * Check all relevant patterns for a match at this
  1872.              * position.  This is complicated, because matching with a
  1873.              * pattern takes quite a bit of time, thus we want to
  1874.              * avoid doing it when it's not needed.
  1875.              */
  1876.             next_match_idx = 0;        /* no match in this line yet */
  1877.             next_match_col = MAXCOL;
  1878.             for (idx = syn_buf->b_syn_patterns.ga_len; --idx >= 0; )
  1879.             {
  1880.             spp = &(SYN_ITEMS(syn_buf)[idx]);
  1881.             if (       spp->sp_syncing == syncing
  1882.                 && (displaying || !(spp->sp_flags & HL_DISPLAY))
  1883.                 && (spp->sp_type == SPTYPE_MATCH
  1884.                     || spp->sp_type == SPTYPE_START)
  1885.                 && (current_next_list != NULL
  1886.                     ? in_id_list(NULL, current_next_list,
  1887.                                   &spp->sp_syn, 0)
  1888.                     : (cur_si == NULL
  1889.                     ? !(spp->sp_flags & HL_CONTAINED)
  1890.                     : in_id_list(cur_si,
  1891.                         cur_si->si_cont_list, &spp->sp_syn,
  1892.                         spp->sp_flags & HL_CONTAINED))))
  1893.             {
  1894.                 /* If we already tried matching in this line, and
  1895.                  * there isn't a match before next_match_col, skip
  1896.                  * this item. */
  1897.                 if (spp->sp_line_id == current_line_id
  1898.                     && spp->sp_startcol >= next_match_col)
  1899.                 continue;
  1900.                 spp->sp_line_id = current_line_id;
  1901.  
  1902.                 lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
  1903.                 if (lc_col < 0)
  1904.                 lc_col = 0;
  1905.  
  1906.                 regmatch.rmm_ic = spp->sp_ic;
  1907.                 regmatch.regprog = spp->sp_prog;
  1908.                 if (!syn_regexec(®match, current_lnum,
  1909.                                  (colnr_T)lc_col))
  1910.                 {
  1911.                 /* no match in this line, try another one */
  1912.                 spp->sp_startcol = MAXCOL;
  1913.                 continue;
  1914.                 }
  1915.  
  1916.                 /*
  1917.                  * Compute the first column of the match.
  1918.                  */
  1919.                 syn_add_start_off(&pos, ®match,
  1920.                              spp, SPO_MS_OFF, -1);
  1921.                 if (pos.lnum > current_lnum)
  1922.                 {
  1923.                 /* must have used end of match in a next line,
  1924.                  * we can't handle that */
  1925.                 spp->sp_startcol = MAXCOL;
  1926.                 continue;
  1927.                 }
  1928.                 startcol = pos.col;
  1929.  
  1930.                 /* remember the next column where this pattern
  1931.                  * matches in the current line */
  1932.                 spp->sp_startcol = startcol;
  1933.  
  1934.                 /*
  1935.                  * If a previously found match starts at a lower
  1936.                  * column number, don't use this one.
  1937.                  */
  1938.                 if (startcol >= next_match_col)
  1939.                 continue;
  1940.  
  1941.                 /*
  1942.                  * If we matched this pattern at this position
  1943.                  * before, skip it.  Must retry in the next
  1944.                  * column, because it may match from there.
  1945.                  */
  1946.                 if (did_match_already(idx))
  1947.                 {
  1948.                 try_next_column = TRUE;
  1949.                 continue;
  1950.                 }
  1951.  
  1952.                 endpos.lnum = regmatch.endpos[0].lnum;
  1953.                 endpos.col = regmatch.endpos[0].col;
  1954.  
  1955.                 /* Compute the highlight start. */
  1956.                 syn_add_start_off(&hl_startpos, ®match,
  1957.                              spp, SPO_HS_OFF, -1);
  1958.  
  1959.                 /* Compute the region start. */
  1960.                 /* Default is to use the end of the match. */
  1961.                 syn_add_end_off(&eos_pos, ®match,
  1962.                              spp, SPO_RS_OFF, 0);
  1963.  
  1964.                 /*
  1965.                  * Grab the external submatches before they get
  1966.                  * overwritten.  Reference count doesn't change.
  1967.                  */
  1968.                 unref_extmatch(cur_extmatch);
  1969.                 cur_extmatch = re_extmatch_out;
  1970.                 re_extmatch_out = NULL;
  1971.  
  1972.                 flags = 0;
  1973.                 eoe_pos.lnum = 0;    /* avoid warning */
  1974.                 eoe_pos.col = 0;
  1975.                 end_idx = 0;
  1976.                 hl_endpos.lnum = 0;
  1977.  
  1978.                 /*
  1979.                  * For a "oneline" the end must be found in the
  1980.                  * same line too.  Search for it after the end of
  1981.                  * the match with the start pattern.  Set the
  1982.                  * resulting end positions at the same time.
  1983.                  */
  1984.                 if (spp->sp_type == SPTYPE_START
  1985.                           && (spp->sp_flags & HL_ONELINE))
  1986.                 {
  1987.                 lpos_T    startpos;
  1988.  
  1989.                 startpos = endpos;
  1990.                 find_endpos(idx, &startpos, &endpos, &hl_endpos,
  1991.                     &flags, &eoe_pos, &end_idx, cur_extmatch);
  1992.                 if (endpos.lnum == 0)
  1993.                     continue;        /* not found */
  1994.                 }
  1995.  
  1996.                 /*
  1997.                  * For a "match" the size must be > 0 after the
  1998.                  * end offset needs has been added.  Except when
  1999.                  * syncing.
  2000.                  */
  2001.                 else if (spp->sp_type == SPTYPE_MATCH)
  2002.                 {
  2003.                 syn_add_end_off(&hl_endpos, ®match, spp,
  2004.                                    SPO_HE_OFF, 0);
  2005.                 syn_add_end_off(&endpos, ®match, spp,
  2006.                                    SPO_ME_OFF, 0);
  2007.                 if (endpos.lnum == current_lnum
  2008.                       && (int)endpos.col + syncing < startcol)
  2009.                 {
  2010.                     /*
  2011.                      * If an empty string is matched, may need
  2012.                      * to try matching again at next column.
  2013.                      */
  2014.                     if (regmatch.startpos[0].col
  2015.                             == regmatch.endpos[0].col)
  2016.                     try_next_column = TRUE;
  2017.                     continue;
  2018.                 }
  2019.                 }
  2020.  
  2021.                 /*
  2022.                  * keep the best match so far in next_match_*
  2023.                  */
  2024.                 /* Highlighting must start after startpos and end
  2025.                  * before endpos. */
  2026.                 if (hl_startpos.lnum == current_lnum
  2027.                        && (int)hl_startpos.col < startcol)
  2028.                 hl_startpos.col = startcol;
  2029.                 limit_pos_zero(&hl_endpos, &endpos);
  2030.  
  2031.                 next_match_idx = idx;
  2032.                 next_match_col = startcol;
  2033.                 next_match_m_endpos = endpos;
  2034.                 next_match_h_endpos = hl_endpos;
  2035.                 next_match_h_startpos = hl_startpos;
  2036.                 next_match_flags = flags;
  2037.                 next_match_eos_pos = eos_pos;
  2038.                 next_match_eoe_pos = eoe_pos;
  2039.                 next_match_end_idx = end_idx;
  2040.                 unref_extmatch(next_match_extmatch);
  2041.                 next_match_extmatch = cur_extmatch;
  2042.                 cur_extmatch = NULL;
  2043.             }
  2044.             }
  2045.         }
  2046.  
  2047.         /*
  2048.          * If we found a match at the current column, use it.
  2049.          */
  2050.         if (next_match_idx >= 0 && next_match_col == (int)current_col)
  2051.         {
  2052.             synpat_T    *lspp;
  2053.  
  2054.             /* When a zero-width item matched which has a nextgroup,
  2055.              * don't push the item but set nextgroup. */
  2056.             lspp = &(SYN_ITEMS(syn_buf)[next_match_idx]);
  2057.             if (next_match_m_endpos.lnum == current_lnum
  2058.                 && next_match_m_endpos.col == current_col
  2059.                 && lspp->sp_next_list != NULL)
  2060.             {
  2061.             current_next_list = lspp->sp_next_list;
  2062.             current_next_flags = lspp->sp_flags;
  2063.             keep_next_list = TRUE;
  2064.             zero_width_next_list = TRUE;
  2065.             next_match_idx = -1;
  2066.             }
  2067.             else
  2068.             cur_si = push_next_match(cur_si);
  2069.             found_match = TRUE;
  2070.         }
  2071.         }
  2072.     }
  2073.  
  2074.     /*
  2075.      * Handle searching for nextgroup match.
  2076.      */
  2077.     if (current_next_list != NULL && !keep_next_list)
  2078.     {
  2079.         /*
  2080.          * If a nextgroup was not found, continue looking for one if:
  2081.          * - this is an empty line and the "skipempty" option was given
  2082.          * - we are on white space and the "skipwhite" option was given
  2083.          */
  2084.         if (!found_match)
  2085.         {
  2086.         line = syn_getcurline();
  2087.         if (((current_next_flags & HL_SKIPWHITE)
  2088.                 && vim_iswhite(line[current_col]))
  2089.             || ((current_next_flags & HL_SKIPEMPTY)
  2090.                 && *line == NUL))
  2091.             break;
  2092.         }
  2093.  
  2094.         /*
  2095.          * If a nextgroup was found: Use it, and continue looking for
  2096.          * contained matches.
  2097.          * If a nextgroup was not found: Continue looking for a normal
  2098.          * match.
  2099.          * When did set current_next_list for a zero-width item and no
  2100.          * match was found don't loop (would get stuck).
  2101.          */
  2102.         current_next_list = NULL;
  2103.         next_match_idx = -1;
  2104.         if (!zero_width_next_list)
  2105.         found_match = TRUE;
  2106.     }
  2107.  
  2108.     } while (found_match);
  2109.  
  2110.     /*
  2111.      * Use attributes from the current state, if within its highlighting.
  2112.      * If not, use attributes from the current-but-one state, etc.
  2113.      */
  2114.     current_attr = 0;
  2115. #ifdef FEAT_EVAL
  2116.     current_id = 0;
  2117.     current_trans_id = 0;
  2118. #endif
  2119.     if (cur_si != NULL)
  2120.     {
  2121.     for (idx = current_state.ga_len - 1; idx >= 0; --idx)
  2122.     {
  2123.         sip = &CUR_STATE(idx);
  2124.         if ((current_lnum > sip->si_h_startpos.lnum
  2125.             || (current_lnum == sip->si_h_startpos.lnum
  2126.                 && current_col >= sip->si_h_startpos.col))
  2127.             && (sip->si_h_endpos.lnum == 0
  2128.             || current_lnum < sip->si_h_endpos.lnum
  2129.             || (current_lnum == sip->si_h_endpos.lnum
  2130.                 && current_col < sip->si_h_endpos.col)))
  2131.         {
  2132.         current_attr = sip->si_attr;
  2133. #ifdef FEAT_EVAL
  2134.         current_id = sip->si_id;
  2135.         current_trans_id = sip->si_trans_id;
  2136. #endif
  2137.         break;
  2138.         }
  2139.     }
  2140.  
  2141.     /*
  2142.      * Check for end of current state (and the states before it) at the
  2143.      * next column.  Don't do this for syncing, because we would miss a
  2144.      * single character match.
  2145.      * First check if the current state ends at the current column.  It
  2146.      * may be for an empty match and a containing item might end in the
  2147.      * current column.
  2148.      */
  2149.     if (!syncing)
  2150.     {
  2151.         check_state_ends();
  2152.         if (current_state.ga_len > 0)
  2153.         {
  2154.         ++current_col;
  2155.         check_state_ends();
  2156.         --current_col;
  2157.         }
  2158.     }
  2159.     }
  2160.  
  2161.     /* nextgroup ends at end of line, unless "skipnl" or "skipemtpy" present */
  2162.     if (current_next_list != NULL
  2163.         && syn_getcurline()[current_col + 1] == NUL
  2164.         && !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
  2165.     current_next_list = NULL;
  2166.  
  2167.     /* No longer need external matches.  But keep next_match_extmatch. */
  2168.     unref_extmatch(re_extmatch_out);
  2169.     re_extmatch_out = NULL;
  2170.     unref_extmatch(cur_extmatch);
  2171.  
  2172.     return current_attr;
  2173. }
  2174.  
  2175.  
  2176. /*
  2177.  * Check if we already matched pattern "idx" at the current column.
  2178.  */
  2179.     static int
  2180. did_match_already(idx)
  2181.     int        idx;
  2182. {
  2183.     int        i;
  2184.  
  2185.     for (i = current_state.ga_len; --i >= 0; )
  2186.     {
  2187.     if (CUR_STATE(i).si_m_startcol == (int)current_col
  2188.         && CUR_STATE(i).si_m_lnum == (int)current_lnum
  2189.         && CUR_STATE(i).si_idx == idx)
  2190.         return TRUE;
  2191.     }
  2192.     return FALSE;
  2193. }
  2194.  
  2195. /*
  2196.  * Push the next match onto the stack.
  2197.  */
  2198.     static stateitem_T *
  2199. push_next_match(cur_si)
  2200.     stateitem_T    *cur_si;
  2201. {
  2202.     synpat_T    *spp;
  2203.  
  2204.     spp = &(SYN_ITEMS(syn_buf)[next_match_idx]);
  2205.  
  2206.     /*
  2207.      * Push the item in current_state stack;
  2208.      */
  2209.     if (push_current_state(next_match_idx) == OK)
  2210.     {
  2211.     /*
  2212.      * If it's a start-skip-end type that crosses lines, figure out how
  2213.      * much it continues in this line.  Otherwise just fill in the length.
  2214.      */
  2215.     cur_si = &CUR_STATE(current_state.ga_len - 1);
  2216.     cur_si->si_h_startpos = next_match_h_startpos;
  2217.     cur_si->si_m_startcol = current_col;
  2218.     cur_si->si_m_lnum = current_lnum;
  2219.     cur_si->si_flags = spp->sp_flags;
  2220.     cur_si->si_next_list = spp->sp_next_list;
  2221.     cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
  2222.     if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
  2223.     {
  2224.         /* Try to find the end pattern in the current line */
  2225.         update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
  2226.         check_keepend();
  2227.     }
  2228.     else
  2229.     {
  2230.         cur_si->si_m_endpos = next_match_m_endpos;
  2231.         cur_si->si_h_endpos = next_match_h_endpos;
  2232.         cur_si->si_ends = TRUE;
  2233.         cur_si->si_flags |= next_match_flags;
  2234.         cur_si->si_eoe_pos = next_match_eoe_pos;
  2235.         cur_si->si_end_idx = next_match_end_idx;
  2236.     }
  2237.     if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
  2238.         keepend_level = current_state.ga_len - 1;
  2239.     check_keepend();
  2240.     update_si_attr(current_state.ga_len - 1);
  2241.  
  2242.     /*
  2243.      * If the start pattern has another highlight group, push another item
  2244.      * on the stack for the start pattern.
  2245.      */
  2246.     if (       spp->sp_type == SPTYPE_START
  2247.         && spp->sp_syn_match_id != 0
  2248.         && push_current_state(next_match_idx) == OK)
  2249.     {
  2250.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  2251.         cur_si->si_h_startpos = next_match_h_startpos;
  2252.         cur_si->si_m_startcol = current_col;
  2253.         cur_si->si_m_lnum = current_lnum;
  2254.         cur_si->si_m_endpos = next_match_eos_pos;
  2255.         cur_si->si_h_endpos = next_match_eos_pos;
  2256.         cur_si->si_ends = TRUE;
  2257.         cur_si->si_end_idx = 0;
  2258.         cur_si->si_flags = HL_MATCH;
  2259.         cur_si->si_next_list = NULL;
  2260.         check_keepend();
  2261.         update_si_attr(current_state.ga_len - 1);
  2262.     }
  2263.     }
  2264.  
  2265.     next_match_idx = -1;    /* try other match next time */
  2266.  
  2267.     return cur_si;
  2268. }
  2269.  
  2270. /*
  2271.  * Check for end of current state (and the states before it).
  2272.  */
  2273.     static void
  2274. check_state_ends()
  2275. {
  2276.     stateitem_T    *cur_si;
  2277.     int        had_extend = FALSE;
  2278.  
  2279.     cur_si = &CUR_STATE(current_state.ga_len - 1);
  2280.     for (;;)
  2281.     {
  2282.     if (cur_si->si_ends
  2283.         && (cur_si->si_m_endpos.lnum < current_lnum
  2284.             || (cur_si->si_m_endpos.lnum == current_lnum
  2285.             && cur_si->si_m_endpos.col <= current_col)))
  2286.     {
  2287.         /*
  2288.          * If there is an end pattern group ID, highlight the end pattern
  2289.          * now.  No need to pop the current item from the stack.
  2290.          * Only do this if the end pattern continues beyond the current
  2291.          * position.
  2292.          */
  2293.         if (cur_si->si_end_idx
  2294.             && (cur_si->si_eoe_pos.lnum > current_lnum
  2295.             || (cur_si->si_eoe_pos.lnum == current_lnum
  2296.                 && cur_si->si_eoe_pos.col > current_col)))
  2297.         {
  2298.         cur_si->si_idx = cur_si->si_end_idx;
  2299.         cur_si->si_end_idx = 0;
  2300.         cur_si->si_m_endpos = cur_si->si_eoe_pos;
  2301.         cur_si->si_h_endpos = cur_si->si_eoe_pos;
  2302.         cur_si->si_flags |= HL_MATCH;
  2303.         update_si_attr(current_state.ga_len - 1);
  2304.         break;
  2305.         }
  2306.         else
  2307.         {
  2308.         /* handle next_list, unless at end of line and no "skipnl" or
  2309.          * "skipempty" */
  2310.         current_next_list = cur_si->si_next_list;
  2311.         current_next_flags = cur_si->si_flags;
  2312.         if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
  2313.             && syn_getcurline()[current_col] == NUL)
  2314.             current_next_list = NULL;
  2315.  
  2316.         /* When the ended item has "extend", another item with
  2317.          * "keepend" now needs to check for its end. */
  2318.          if (cur_si->si_flags & HL_EXTEND)
  2319.              had_extend = TRUE;
  2320.  
  2321.         pop_current_state();
  2322.  
  2323.         if (current_state.ga_len == 0)
  2324.             break;
  2325.  
  2326.         if (had_extend)
  2327.         {
  2328.             syn_update_ends(FALSE);
  2329.             if (current_state.ga_len == 0)
  2330.             break;
  2331.         }
  2332.  
  2333.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  2334.  
  2335.         /*
  2336.          * Only for a region the search for the end continues after
  2337.          * the end of the contained item.  If the contained match
  2338.          * included the end-of-line, break here, the region continues.
  2339.          * Don't do this when:
  2340.          * - "keepend" is used for the contained item
  2341.          * - not at the end of the line (could be end="x$"me=e-1).
  2342.          * - "excludenl" is used (HL_HAS_EOL won't be set)
  2343.          */
  2344.         if (cur_si->si_idx >= 0
  2345.             && SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_type
  2346.                                    == SPTYPE_START
  2347.             && !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
  2348.         {
  2349.             update_si_end(cur_si, (int)current_col, TRUE);
  2350.             check_keepend();
  2351.             if ((current_next_flags & HL_HAS_EOL)
  2352.                 && keepend_level < 0
  2353.                 && syn_getcurline()[current_col] == NUL)
  2354.             break;
  2355.         }
  2356.         }
  2357.     }
  2358.     else
  2359.         break;
  2360.     }
  2361. }
  2362.  
  2363. /*
  2364.  * Update an entry in the current_state stack for a match or region.  This
  2365.  * fills in si_attr, si_next_list and si_cont_list.
  2366.  */
  2367.     static void
  2368. update_si_attr(idx)
  2369.     int        idx;
  2370. {
  2371.     stateitem_T    *sip = &CUR_STATE(idx);
  2372.     synpat_T    *spp;
  2373.  
  2374.     spp = &(SYN_ITEMS(syn_buf)[sip->si_idx]);
  2375.     if (sip->si_flags & HL_MATCH)
  2376.     sip->si_id = spp->sp_syn_match_id;
  2377.     else
  2378.     sip->si_id = spp->sp_syn.id;
  2379.     sip->si_attr = syn_id2attr(sip->si_id);
  2380.     sip->si_trans_id = sip->si_id;
  2381.     if (sip->si_flags & HL_MATCH)
  2382.     sip->si_cont_list = NULL;
  2383.     else
  2384.     sip->si_cont_list = spp->sp_cont_list;
  2385.  
  2386.     /*
  2387.      * For transparent items, take attr from outer item.
  2388.      * Also take cont_list, if there is none.
  2389.      * Don't do this for the matchgroup of a start or end pattern.
  2390.      */
  2391.     if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
  2392.     {
  2393.     if (idx == 0)
  2394.     {
  2395.         sip->si_attr = 0;
  2396.         sip->si_trans_id = 0;
  2397.         if (sip->si_cont_list == NULL)
  2398.         sip->si_cont_list = ID_LIST_ALL;
  2399.     }
  2400.     else
  2401.     {
  2402.         sip->si_attr = CUR_STATE(idx - 1).si_attr;
  2403.         sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
  2404.         if (sip->si_cont_list == NULL)
  2405.         {
  2406.         sip->si_flags |= HL_TRANS_CONT;
  2407.         sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
  2408.         }
  2409.     }
  2410.     }
  2411. }
  2412.  
  2413. /*
  2414.  * Check the current stack for patterns with "keepend" flag.
  2415.  * Propagate the match-end to contained items, until a "skipend" item is found.
  2416.  */
  2417.     static void
  2418. check_keepend()
  2419. {
  2420.     int        i;
  2421.     lpos_T    maxpos;
  2422.     stateitem_T    *sip;
  2423.  
  2424.     /*
  2425.      * This check can consume a lot of time; only do it from the level where
  2426.      * there really is a keepend.
  2427.      */
  2428.     if (keepend_level < 0)
  2429.     return;
  2430.  
  2431.     /*
  2432.      * Find the last index of an "extend" item.  "keepend" items before that
  2433.      * won't do anything.  If there is no "extend" item "i" will be
  2434.      * "keepend_level" and all "keepend" items will work normally.
  2435.      */
  2436.     for (i = current_state.ga_len - 1; i > keepend_level; --i)
  2437.     if (CUR_STATE(i).si_flags & HL_EXTEND)
  2438.         break;
  2439.  
  2440.     maxpos.lnum = 0;
  2441.     for ( ; i < current_state.ga_len; ++i)
  2442.     {
  2443.     sip = &CUR_STATE(i);
  2444.     if (maxpos.lnum != 0)
  2445.     {
  2446.         limit_pos_zero(&sip->si_m_endpos, &maxpos);
  2447.         limit_pos_zero(&sip->si_h_endpos, &maxpos);
  2448.         limit_pos_zero(&sip->si_eoe_pos, &maxpos);
  2449.         sip->si_ends = TRUE;
  2450.     }
  2451.     if (sip->si_ends
  2452.         && (sip->si_flags & HL_KEEPEND)
  2453.         && (maxpos.lnum == 0
  2454.             || maxpos.lnum > sip->si_m_endpos.lnum
  2455.             || (maxpos.lnum == sip->si_m_endpos.lnum
  2456.             && maxpos.col > sip->si_m_endpos.col)))
  2457.         maxpos = sip->si_m_endpos;
  2458.     }
  2459. }
  2460.  
  2461. /*
  2462.  * Update an entry in the current_state stack for a start-skip-end pattern.
  2463.  * This finds the end of the current item, if it's in the current line.
  2464.  *
  2465.  * Return the flags for the matched END.
  2466.  */
  2467.     static void
  2468. update_si_end(sip, startcol, force)
  2469.     stateitem_T    *sip;
  2470.     int        startcol;   /* where to start searching for the end */
  2471.     int        force;        /* when TRUE overrule a previous end */
  2472. {
  2473.     lpos_T    startpos;
  2474.     lpos_T    endpos;
  2475.     lpos_T    hl_endpos;
  2476.     lpos_T    end_endpos;
  2477.     int        end_idx;
  2478.  
  2479.     /* Don't update when it's already done.  Can be a match of an end pattern
  2480.      * that started in a previous line.  Watch out: can also be a "keepend"
  2481.      * from a containing item. */
  2482.     if (!force && sip->si_m_endpos.lnum >= current_lnum)
  2483.     return;
  2484.  
  2485.     /*
  2486.      * We need to find the end of the region.  It may continue in the next
  2487.      * line.
  2488.      */
  2489.     end_idx = 0;
  2490.     startpos.lnum = current_lnum;
  2491.     startpos.col = startcol;
  2492.     find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
  2493.            &(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
  2494.  
  2495.     if (endpos.lnum == 0)
  2496.     {
  2497.     /* No end pattern matched. */
  2498.     if (SYN_ITEMS(syn_buf)[sip->si_idx].sp_flags & HL_ONELINE)
  2499.     {
  2500.         /* a "oneline" never continues in the next line */
  2501.         sip->si_ends = TRUE;
  2502.         sip->si_m_endpos.lnum = current_lnum;
  2503.         sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
  2504.     }
  2505.     else
  2506.     {
  2507.         /* continues in the next line */
  2508.         sip->si_ends = FALSE;
  2509.         sip->si_m_endpos.lnum = 0;
  2510.     }
  2511.     sip->si_h_endpos = sip->si_m_endpos;
  2512.     }
  2513.     else
  2514.     {
  2515.     /* match within this line */
  2516.     sip->si_m_endpos = endpos;
  2517.     sip->si_h_endpos = hl_endpos;
  2518.     sip->si_eoe_pos = end_endpos;
  2519.     sip->si_ends = TRUE;
  2520.     sip->si_end_idx = end_idx;
  2521.     }
  2522. }
  2523.  
  2524. /*
  2525.  * Add a new state to the current state stack.
  2526.  * It is cleared and the index set to "idx".
  2527.  * Return FAIL if it's not possible (out of memory).
  2528.  */
  2529.     static int
  2530. push_current_state(idx)
  2531.     int        idx;
  2532. {
  2533.     if (ga_grow(¤t_state, 1) == FAIL)
  2534.     return FAIL;
  2535.     vim_memset(&CUR_STATE(current_state.ga_len), 0, sizeof(stateitem_T));
  2536.     CUR_STATE(current_state.ga_len).si_idx = idx;
  2537.     ++current_state.ga_len;
  2538.     --current_state.ga_room;
  2539.     return OK;
  2540. }
  2541.  
  2542. /*
  2543.  * Remove a state from the current_state stack.
  2544.  */
  2545.     static void
  2546. pop_current_state()
  2547. {
  2548.     if (current_state.ga_len)
  2549.     {
  2550.     unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
  2551.     --current_state.ga_len;
  2552.     ++current_state.ga_room;
  2553.     }
  2554.     /* after the end of a pattern, try matching a keyword or pattern */
  2555.     next_match_idx = -1;
  2556.  
  2557.     /* if first state with "keepend" is popped, reset keepend_level */
  2558.     if (keepend_level >= current_state.ga_len)
  2559.     keepend_level = -1;
  2560. }
  2561.  
  2562. /*
  2563.  * Find the end of a start/skip/end syntax region after "startpos".
  2564.  * Only checks one line.
  2565.  * Also handles a match item that continued from a previous line.
  2566.  * If not found, the syntax item continues in the next line.  m_endpos->lnum
  2567.  * will be 0.
  2568.  * If found, the end of the region and the end of the highlighting is
  2569.  * computed.
  2570.  */
  2571.     static void
  2572. find_endpos(idx, startpos, m_endpos, hl_endpos, flagsp, end_endpos,
  2573.                                end_idx, start_ext)
  2574.     int        idx;        /* index of the pattern */
  2575.     lpos_T    *startpos;    /* where to start looking for an END match */
  2576.     lpos_T    *m_endpos;    /* return: end of match */
  2577.     lpos_T    *hl_endpos;    /* return: end of highlighting */
  2578.     long    *flagsp;    /* return: flags of matching END */
  2579.     lpos_T    *end_endpos;    /* return: end of end pattern match */
  2580.     int        *end_idx;    /* return: group ID for end pat. match, or 0 */
  2581.     reg_extmatch_T *start_ext;    /* submatches from the start pattern */
  2582. {
  2583.     colnr_T    matchcol;
  2584.     synpat_T    *spp, *spp_skip;
  2585.     int        start_idx;
  2586.     int        best_idx;
  2587.     regmmatch_T    regmatch;
  2588.     regmmatch_T    best_regmatch;        /* startpos/endpos of best match */
  2589.     lpos_T    pos;
  2590.     char_u    *line;
  2591.     int        had_match = FALSE;
  2592.  
  2593.     /*
  2594.      * Check for being called with a START pattern.
  2595.      * Can happen with a match that continues to the next line, because it
  2596.      * contained a region.
  2597.      */
  2598.     spp = &(SYN_ITEMS(syn_buf)[idx]);
  2599.     if (spp->sp_type != SPTYPE_START)
  2600.     {
  2601.     *hl_endpos = *startpos;
  2602.     return;
  2603.     }
  2604.  
  2605.     /*
  2606.      * Find the SKIP or first END pattern after the last START pattern.
  2607.      */
  2608.     for (;;)
  2609.     {
  2610.     spp = &(SYN_ITEMS(syn_buf)[idx]);
  2611.     if (spp->sp_type != SPTYPE_START)
  2612.         break;
  2613.     ++idx;
  2614.     }
  2615.  
  2616.     /*
  2617.      *    Lookup the SKIP pattern (if present)
  2618.      */
  2619.     if (spp->sp_type == SPTYPE_SKIP)
  2620.     {
  2621.     spp_skip = spp;
  2622.     ++idx;
  2623.     }
  2624.     else
  2625.     spp_skip = NULL;
  2626.  
  2627.     /* Setup external matches for syn_regexec(). */
  2628.     unref_extmatch(re_extmatch_in);
  2629.     re_extmatch_in = ref_extmatch(start_ext);
  2630.  
  2631.     matchcol = startpos->col;    /* start looking for a match at sstart */
  2632.     start_idx = idx;        /* remember the first END pattern. */
  2633.     best_regmatch.startpos[0].col = 0;        /* avoid compiler warning */
  2634.     for (;;)
  2635.     {
  2636.     /*
  2637.      * Find end pattern that matches first after "matchcol".
  2638.      */
  2639.     best_idx = -1;
  2640.     for (idx = start_idx; idx < syn_buf->b_syn_patterns.ga_len; ++idx)
  2641.     {
  2642.         int lc_col = matchcol;
  2643.  
  2644.         spp = &(SYN_ITEMS(syn_buf)[idx]);
  2645.         if (spp->sp_type != SPTYPE_END)    /* past last END pattern */
  2646.         break;
  2647.         lc_col -= spp->sp_offsets[SPO_LC_OFF];
  2648.         if (lc_col < 0)
  2649.         lc_col = 0;
  2650.  
  2651.         regmatch.rmm_ic = spp->sp_ic;
  2652.         regmatch.regprog = spp->sp_prog;
  2653.         if (syn_regexec(®match, startpos->lnum, lc_col))
  2654.         {
  2655.         if (best_idx == -1 || regmatch.startpos[0].col
  2656.                           < best_regmatch.startpos[0].col)
  2657.         {
  2658.             best_idx = idx;
  2659.             best_regmatch.startpos[0] = regmatch.startpos[0];
  2660.             best_regmatch.endpos[0] = regmatch.endpos[0];
  2661.         }
  2662.         }
  2663.     }
  2664.  
  2665.     /*
  2666.      * If all end patterns have been tried, and there is no match, the
  2667.      * item continues until end-of-line.
  2668.      */
  2669.     if (best_idx == -1)
  2670.         break;
  2671.  
  2672.     /*
  2673.      * If the skip pattern matches before the end pattern,
  2674.      * continue searching after the skip pattern.
  2675.      */
  2676.     if (spp_skip != NULL)
  2677.     {
  2678.         int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
  2679.  
  2680.         if (lc_col < 0)
  2681.         lc_col = 0;
  2682.         regmatch.rmm_ic = spp_skip->sp_ic;
  2683.         regmatch.regprog = spp_skip->sp_prog;
  2684.         if (syn_regexec(®match, startpos->lnum, lc_col)
  2685.             && regmatch.startpos[0].col
  2686.                          <= best_regmatch.startpos[0].col)
  2687.         {
  2688.         /* Add offset to skip pattern match */
  2689.         syn_add_end_off(&pos, ®match, spp_skip, SPO_ME_OFF, 1);
  2690.  
  2691.         /* If the skip pattern goes on to the next line, there is no
  2692.          * match with an end pattern in this line. */
  2693.         if (pos.lnum > startpos->lnum)
  2694.             break;
  2695.  
  2696.         line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
  2697.  
  2698.         /* take care of an empty match or negative offset */
  2699.         if (pos.col <= matchcol)
  2700.             ++matchcol;
  2701.         else if (pos.col <= regmatch.endpos[0].col)
  2702.             matchcol = pos.col;
  2703.         else
  2704.             /* Be careful not to jump over the NUL at the end-of-line */
  2705.             for (matchcol = regmatch.endpos[0].col;
  2706.                 line[matchcol] != NUL && matchcol < pos.col;
  2707.                                    ++matchcol)
  2708.             ;
  2709.  
  2710.         /* if the skip pattern includes end-of-line, break here */
  2711.         if (line[matchcol] == NUL)
  2712.             break;
  2713.  
  2714.         continue;        /* start with first end pattern again */
  2715.         }
  2716.     }
  2717.  
  2718.     /*
  2719.      * Match from start pattern to end pattern.
  2720.      * Correct for match and highlight offset of end pattern.
  2721.      */
  2722.     spp = &(SYN_ITEMS(syn_buf)[best_idx]);
  2723.     syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
  2724.     /* can't end before the start */
  2725.     if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
  2726.         m_endpos->col = startpos->col;
  2727.  
  2728.     syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
  2729.     /* can't end before the start */
  2730.     if (end_endpos->lnum == startpos->lnum
  2731.                        && end_endpos->col < startpos->col)
  2732.         end_endpos->col = startpos->col;
  2733.     /* can't end after the match */
  2734.     limit_pos(end_endpos, m_endpos);
  2735.  
  2736.     /*
  2737.      * If the end group is highlighted differently, adjust the pointers.
  2738.      */
  2739.     if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
  2740.     {
  2741.         *end_idx = best_idx;
  2742.         if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
  2743.         {
  2744.         hl_endpos->lnum = best_regmatch.endpos[0].lnum;
  2745.         hl_endpos->col = best_regmatch.endpos[0].col;
  2746.         }
  2747.         else
  2748.         {
  2749.         hl_endpos->lnum = best_regmatch.startpos[0].lnum;
  2750.         hl_endpos->col = best_regmatch.startpos[0].col;
  2751.         }
  2752.         hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
  2753.  
  2754.         /* can't end before the start */
  2755.         if (hl_endpos->lnum == startpos->lnum
  2756.                         && hl_endpos->col < startpos->col)
  2757.         hl_endpos->col = startpos->col;
  2758.         limit_pos(hl_endpos, m_endpos);
  2759.  
  2760.         /* now the match ends where the highlighting ends (why?) */
  2761.         m_endpos->col = hl_endpos->col;
  2762.     }
  2763.     else
  2764.     {
  2765.         *end_idx = 0;
  2766.         *hl_endpos = *end_endpos;
  2767.     }
  2768.  
  2769.     *flagsp = spp->sp_flags;
  2770.  
  2771.     had_match = TRUE;
  2772.     break;
  2773.     }
  2774.  
  2775.     /* no match for an END pattern in this line */
  2776.     if (!had_match)
  2777.     m_endpos->lnum = 0;
  2778.  
  2779.     /* Remove external matches. */
  2780.     unref_extmatch(re_extmatch_in);
  2781.     re_extmatch_in = NULL;
  2782. }
  2783.  
  2784. /*
  2785.  * Limit "pos" not to be after "limit".
  2786.  */
  2787.     static void
  2788. limit_pos(pos, limit)
  2789.     lpos_T    *pos;
  2790.     lpos_T    *limit;
  2791. {
  2792.     if (pos->lnum > limit->lnum)
  2793.     *pos = *limit;
  2794.     else if (pos->lnum == limit->lnum && pos->col > limit->col)
  2795.     pos->col = limit->col;
  2796. }
  2797.  
  2798. /*
  2799.  * Limit "pos" not to be after "limit", unless pos->lnum is zero.
  2800.  */
  2801.     static void
  2802. limit_pos_zero(pos, limit)
  2803.     lpos_T    *pos;
  2804.     lpos_T    *limit;
  2805. {
  2806.     if (pos->lnum == 0)
  2807.     *pos = *limit;
  2808.     else
  2809.     limit_pos(pos, limit);
  2810. }
  2811.  
  2812. /*
  2813.  * Add offset to matched text for end of match or highlight.
  2814.  */
  2815.     static void
  2816. syn_add_end_off(result, regmatch, spp, idx, extra)
  2817.     lpos_T    *result;    /* returned position */
  2818.     regmmatch_T    *regmatch;    /* start/end of match */
  2819.     synpat_T    *spp;        /* matched pattern */
  2820.     int        idx;        /* index of offset */
  2821.     int        extra;        /* extra chars for offset to start */
  2822. {
  2823.     int        col;
  2824.  
  2825.     if (spp->sp_off_flags & (1 << idx))
  2826.     {
  2827.     result->lnum = regmatch->startpos[0].lnum;
  2828.     col = regmatch->startpos[0].col + extra;
  2829.     }
  2830.     else
  2831.     {
  2832.     result->lnum = regmatch->endpos[0].lnum;
  2833.     col = regmatch->endpos[0].col;
  2834.     }
  2835.     col += spp->sp_offsets[idx];
  2836.     if (col < 0)
  2837.     result->col = 0;
  2838.     else
  2839.     result->col = col;
  2840. }
  2841.  
  2842. /*
  2843.  * Add offset to matched text for start of match or highlight.
  2844.  * Avoid resulting column to become negative.
  2845.  */
  2846.     static void
  2847. syn_add_start_off(result, regmatch, spp, idx, extra)
  2848.     lpos_T    *result;    /* returned position */
  2849.     regmmatch_T    *regmatch;    /* start/end of match */
  2850.     synpat_T    *spp;
  2851.     int        idx;
  2852.     int        extra;        /* extra chars for offset to end */
  2853. {
  2854.     int        col;
  2855.  
  2856.     if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
  2857.     {
  2858.     result->lnum = regmatch->endpos[0].lnum;
  2859.     col = regmatch->endpos[0].col + extra;
  2860.     }
  2861.     else
  2862.     {
  2863.     result->lnum = regmatch->startpos[0].lnum;
  2864.     col = regmatch->startpos[0].col;
  2865.     }
  2866.     col += spp->sp_offsets[idx];
  2867.     if (col < 0)
  2868.     result->col = 0;
  2869.     else
  2870.     result->col = col;
  2871. }
  2872.  
  2873. /*
  2874.  * Get current line in syntax buffer.
  2875.  */
  2876.     static char_u *
  2877. syn_getcurline()
  2878. {
  2879.     return ml_get_buf(syn_buf, current_lnum, FALSE);
  2880. }
  2881.  
  2882. /*
  2883.  * Call vim_regexec() to match in syn_buf.
  2884.  * Returns TRUE when there is a match.
  2885.  */
  2886.     static int
  2887. syn_regexec(rmp, lnum, col)
  2888.     regmmatch_T    *rmp;
  2889.     linenr_T    lnum;
  2890.     colnr_T    col;
  2891. {
  2892.     if (vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col) > 0)
  2893.     {
  2894.     rmp->startpos[0].lnum += lnum;
  2895.     rmp->endpos[0].lnum += lnum;
  2896.     return TRUE;
  2897.     }
  2898.     return FALSE;
  2899. }
  2900.  
  2901. /*
  2902.  * Check one position in a line for a matching keyword.
  2903.  * The caller must check if a keyword can start at startcol.
  2904.  * Return it's ID if found, 0 otherwise.
  2905.  */
  2906.     static int
  2907. check_keyword_id(line, startcol, endcolp, flagsp, next_listp, cur_si)
  2908.     char_u    *line;
  2909.     int        startcol;    /* position in line to check for keyword */
  2910.     int        *endcolp;    /* return: character after found keyword */
  2911.     long    *flagsp;    /* return: flags of matching keyword */
  2912.     short    **next_listp;    /* return: next_list of matching keyword */
  2913.     stateitem_T    *cur_si;    /* item at the top of the stack */
  2914. {
  2915.     keyentry_T    *ktab;
  2916.     char_u    *p;
  2917.     int        round;
  2918.     int        len;
  2919.     char_u    keyword[MAXKEYWLEN + 1]; /* assume max. keyword len is 80 */
  2920.  
  2921.     /* Find first character after the keyword.  First character was already
  2922.      * checked. */
  2923.     p = line + startcol;
  2924.     len = 0;
  2925.     do
  2926.     {
  2927. #ifdef FEAT_MBYTE
  2928.     if (has_mbyte)
  2929.         len += (*mb_ptr2len_check)(p + len);
  2930.     else
  2931. #endif
  2932.         ++len;
  2933.     }
  2934.     while (vim_iswordc_buf(p + len, syn_buf));
  2935.  
  2936.     if (len > MAXKEYWLEN)
  2937.     return 0;
  2938.  
  2939.     /*
  2940.      * Must make a copy of the keyword, so we can add a NUL and make it
  2941.      * lowercase.
  2942.      */
  2943.     STRNCPY(keyword, p, len);
  2944.     keyword[len] = NUL;
  2945.  
  2946.     /*
  2947.      * Try twice:
  2948.      * 1. matching case
  2949.      * 2. ignoring case
  2950.      */
  2951.     for (round = 1; round <= 2; ++round)
  2952.     {
  2953.     if ((round == 1 ? syn_buf->b_keywtab : syn_buf->b_keywtab_ic) == NULL)
  2954.         continue;
  2955.     if (round == 1)    /* match case */
  2956.         ktab = syn_buf->b_keywtab[syn_khash(keyword)];
  2957.     else /* round == 2, ignore case */
  2958.     {
  2959.         p = str_foldcase(keyword, (int)STRLEN(keyword));
  2960.         if (p != NULL)
  2961.         {
  2962.         STRNCPY(keyword, p, MAXKEYWLEN);
  2963.         keyword[MAXKEYWLEN] = NUL;
  2964.         vim_free(p);
  2965.         }
  2966.         ktab = syn_buf->b_keywtab_ic[syn_khash(keyword)];
  2967.     }
  2968.  
  2969.     /*
  2970.      * Find keywords that match.
  2971.      * When current_next_list is non-zero accept only that group, otherwise:
  2972.      *  Accept a not-contained keyword at toplevel.
  2973.      *  Accept a keyword at other levels only if it is in the contains list.
  2974.      */
  2975.     for ( ; ktab != NULL; ktab = ktab->next)
  2976.         if (   STRCMP(keyword, ktab->keyword) == 0
  2977.         && (current_next_list != 0
  2978.             ? in_id_list(NULL, current_next_list, &ktab->k_syn, 0)
  2979.             : (cur_si == NULL
  2980.             ? !(ktab->flags & HL_CONTAINED)
  2981.             : in_id_list(cur_si, cur_si->si_cont_list,
  2982.                   &ktab->k_syn, ktab->flags & HL_CONTAINED))))
  2983.         {
  2984.         *endcolp = startcol + len;
  2985.         *flagsp = ktab->flags;
  2986.         *next_listp = ktab->next_list;
  2987.         return ktab->k_syn.id;
  2988.         }
  2989.     }
  2990.     return 0;
  2991. }
  2992.  
  2993. /*
  2994.  * Handle ":syntax case" command.
  2995.  */
  2996. /* ARGSUSED */
  2997.     static void
  2998. syn_cmd_case(eap, syncing)
  2999.     exarg_T    *eap;
  3000.     int        syncing;        /* not used */
  3001. {
  3002.     char_u    *arg = eap->arg;
  3003.     char_u    *next;
  3004.  
  3005.     eap->nextcmd = find_nextcmd(arg);
  3006.     if (eap->skip)
  3007.     return;
  3008.  
  3009.     next = skiptowhite(arg);
  3010.     if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
  3011.     curbuf->b_syn_ic = FALSE;
  3012.     else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
  3013.     curbuf->b_syn_ic = TRUE;
  3014.     else
  3015.     EMSG2(_("E390: Illegal argument: %s"), arg);
  3016. }
  3017.  
  3018. /*
  3019.  * Clear all syntax info for one buffer.
  3020.  */
  3021.     void
  3022. syntax_clear(buf)
  3023.     buf_T    *buf;
  3024. {
  3025.     int i;
  3026.  
  3027.     curbuf->b_syn_ic = FALSE;        /* Use case, by default */
  3028.     curbuf->b_syn_containedin = FALSE;
  3029.  
  3030.     /* free the keywords */
  3031.     free_keywtab(buf->b_keywtab);
  3032.     buf->b_keywtab = NULL;
  3033.     free_keywtab(buf->b_keywtab_ic);
  3034.     buf->b_keywtab_ic = NULL;
  3035.  
  3036.     /* free the syntax patterns */
  3037.     for (i = buf->b_syn_patterns.ga_len; --i >= 0; )
  3038.     syn_clear_pattern(buf, i);
  3039.     ga_clear(&buf->b_syn_patterns);
  3040.  
  3041.     /* free the syntax clusters */
  3042.     for (i = buf->b_syn_clusters.ga_len; --i >= 0; )
  3043.     syn_clear_cluster(buf, i);
  3044.     ga_clear(&buf->b_syn_clusters);
  3045.  
  3046.     buf->b_syn_sync_flags = 0;
  3047.     buf->b_syn_sync_minlines = 0;
  3048.     buf->b_syn_sync_maxlines = 0;
  3049.     buf->b_syn_sync_linebreaks = 0;
  3050.  
  3051.     vim_free(buf->b_syn_linecont_prog);
  3052.     buf->b_syn_linecont_prog = NULL;
  3053.     vim_free(buf->b_syn_linecont_pat);
  3054.     buf->b_syn_linecont_pat = NULL;
  3055. #ifdef FEAT_FOLDING
  3056.     buf->b_syn_folditems = 0;
  3057. #endif
  3058.  
  3059.     /* free the stored states */
  3060.     syn_stack_free_all(buf);
  3061.     invalidate_current_state();
  3062. }
  3063.  
  3064. /*
  3065.  * Clear syncing info for one buffer.
  3066.  */
  3067.     static void
  3068. syntax_sync_clear()
  3069. {
  3070.     int i;
  3071.  
  3072.     /* free the syntax patterns */
  3073.     for (i = curbuf->b_syn_patterns.ga_len; --i >= 0; )
  3074.     if (SYN_ITEMS(curbuf)[i].sp_syncing)
  3075.         syn_remove_pattern(curbuf, i);
  3076.  
  3077.     curbuf->b_syn_sync_flags = 0;
  3078.     curbuf->b_syn_sync_minlines = 0;
  3079.     curbuf->b_syn_sync_maxlines = 0;
  3080.     curbuf->b_syn_sync_linebreaks = 0;
  3081.  
  3082.     vim_free(curbuf->b_syn_linecont_prog);
  3083.     curbuf->b_syn_linecont_prog = NULL;
  3084.     vim_free(curbuf->b_syn_linecont_pat);
  3085.     curbuf->b_syn_linecont_pat = NULL;
  3086.  
  3087.     syn_stack_free_all(curbuf);        /* Need to recompute all syntax. */
  3088. }
  3089.  
  3090. /*
  3091.  * Remove one pattern from the buffer's pattern list.
  3092.  */
  3093.     static void
  3094. syn_remove_pattern(buf, idx)
  3095.     buf_T    *buf;
  3096.     int        idx;
  3097. {
  3098.     synpat_T    *spp;
  3099.  
  3100.     spp = &(SYN_ITEMS(buf)[idx]);
  3101. #ifdef FEAT_FOLDING
  3102.     if (spp->sp_flags & HL_FOLD)
  3103.     --buf->b_syn_folditems;
  3104. #endif
  3105.     syn_clear_pattern(buf, idx);
  3106.     mch_memmove(spp, spp + 1,
  3107.            sizeof(synpat_T) * (buf->b_syn_patterns.ga_len - idx - 1));
  3108.     --buf->b_syn_patterns.ga_len;
  3109.     --buf->b_syn_patterns.ga_room;
  3110. }
  3111.  
  3112. /*
  3113.  * Clear and free one syntax pattern.  When clearing all, must be called from
  3114.  * last to first!
  3115.  */
  3116.     static void
  3117. syn_clear_pattern(buf, i)
  3118.     buf_T    *buf;
  3119.     int        i;
  3120. {
  3121.     vim_free(SYN_ITEMS(buf)[i].sp_pattern);
  3122.     vim_free(SYN_ITEMS(buf)[i].sp_prog);
  3123.     /* Only free sp_cont_list and sp_next_list of first start pattern */
  3124.     if (i == 0 || SYN_ITEMS(buf)[i - 1].sp_type != SPTYPE_START)
  3125.     {
  3126.     vim_free(SYN_ITEMS(buf)[i].sp_cont_list);
  3127.     vim_free(SYN_ITEMS(buf)[i].sp_next_list);
  3128.     }
  3129. }
  3130.  
  3131. /*
  3132.  * Clear and free one syntax cluster.
  3133.  */
  3134.     static void
  3135. syn_clear_cluster(buf, i)
  3136.     buf_T    *buf;
  3137.     int        i;
  3138. {
  3139.     vim_free(SYN_CLSTR(buf)[i].scl_name);
  3140.     vim_free(SYN_CLSTR(buf)[i].scl_name_u);
  3141.     vim_free(SYN_CLSTR(buf)[i].scl_list);
  3142. }
  3143.  
  3144. /*
  3145.  * Handle ":syntax clear" command.
  3146.  */
  3147.     static void
  3148. syn_cmd_clear(eap, syncing)
  3149.     exarg_T    *eap;
  3150.     int        syncing;
  3151. {
  3152.     char_u    *arg = eap->arg;
  3153.     char_u    *arg_end;
  3154.     int        id;
  3155.  
  3156.     eap->nextcmd = find_nextcmd(arg);
  3157.     if (eap->skip)
  3158.     return;
  3159.  
  3160.     /*
  3161.      * We have to disable this within ":syn include @group filename",
  3162.      * because otherwise @group would get deleted.
  3163.      * Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
  3164.      * clear".
  3165.      */
  3166.     if (curbuf->b_syn_topgrp != 0)
  3167.     return;
  3168.  
  3169.     if (ends_excmd(*arg))
  3170.     {
  3171.     /*
  3172.      * No argument: Clear all syntax items.
  3173.      */
  3174.     if (syncing)
  3175.         syntax_sync_clear();
  3176.     else
  3177.     {
  3178.         syntax_clear(curbuf);
  3179.         do_unlet((char_u *)"b:current_syntax");
  3180.     }
  3181.     }
  3182.     else
  3183.     {
  3184.     /*
  3185.      * Clear the group IDs that are in the argument.
  3186.      */
  3187.     while (!ends_excmd(*arg))
  3188.     {
  3189.         arg_end = skiptowhite(arg);
  3190.         if (*arg == '@')
  3191.         {
  3192.         id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
  3193.         if (id == 0)
  3194.         {
  3195.             EMSG2(_("E391: No such syntax cluster: %s"), arg);
  3196.             break;
  3197.         }
  3198.         else
  3199.         {
  3200.             /*
  3201.              * We can't physically delete a cluster without changing
  3202.              * the IDs of other clusters, so we do the next best thing
  3203.              * and make it empty.
  3204.              */
  3205.             short scl_id = id - SYNID_CLUSTER;
  3206.  
  3207.             vim_free(SYN_CLSTR(curbuf)[scl_id].scl_list);
  3208.             SYN_CLSTR(curbuf)[scl_id].scl_list = NULL;
  3209.         }
  3210.         }
  3211.         else
  3212.         {
  3213.         id = syn_namen2id(arg, (int)(arg_end - arg));
  3214.         if (id == 0)
  3215.         {
  3216.             EMSG2(_(e_nogroup), arg);
  3217.             break;
  3218.         }
  3219.         else
  3220.             syn_clear_one(id, syncing);
  3221.         }
  3222.         arg = skipwhite(arg_end);
  3223.     }
  3224.     }
  3225.     redraw_curbuf_later(NOT_VALID);
  3226.     syn_stack_free_all(curbuf);        /* Need to recompute all syntax. */
  3227. }
  3228.  
  3229. /*
  3230.  * Clear one syntax group for the current buffer.
  3231.  */
  3232.     static void
  3233. syn_clear_one(id, syncing)
  3234.     int        id;
  3235.     int        syncing;
  3236. {
  3237.     synpat_T    *spp;
  3238.     int        idx;
  3239.  
  3240.     /* Clear keywords only when not ":syn sync clear group-name" */
  3241.     if (!syncing)
  3242.     {
  3243.     (void)syn_clear_keyword(id, curbuf->b_keywtab);
  3244.     (void)syn_clear_keyword(id, curbuf->b_keywtab_ic);
  3245.     }
  3246.  
  3247.     /* clear the patterns for "id" */
  3248.     for (idx = curbuf->b_syn_patterns.ga_len; --idx >= 0; )
  3249.     {
  3250.     spp = &(SYN_ITEMS(curbuf)[idx]);
  3251.     if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
  3252.         continue;
  3253.     syn_remove_pattern(curbuf, idx);
  3254.     }
  3255. }
  3256.  
  3257. /*
  3258.  * Handle ":syntax on" command.
  3259.  */
  3260. /* ARGSUSED */
  3261.     static void
  3262. syn_cmd_on(eap, syncing)
  3263.     exarg_T    *eap;
  3264.     int        syncing;    /* not used */
  3265. {
  3266.     syn_cmd_onoff(eap, "syntax");
  3267. }
  3268.  
  3269. /*
  3270.  * Handle ":syntax enable" command.
  3271.  */
  3272. /* ARGSUSED */
  3273.     static void
  3274. syn_cmd_enable(eap, syncing)
  3275.     exarg_T    *eap;
  3276.     int        syncing;    /* not used */
  3277. {
  3278.     set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"enable");
  3279.     syn_cmd_onoff(eap, "syntax");
  3280.     do_unlet((char_u *)"g:syntax_cmd");
  3281. }
  3282.  
  3283. /*
  3284.  * Handle ":syntax reset" command.
  3285.  */
  3286. /* ARGSUSED */
  3287.     static void
  3288. syn_cmd_reset(eap, syncing)
  3289.     exarg_T    *eap;
  3290.     int        syncing;    /* not used */
  3291. {
  3292.     eap->nextcmd = check_nextcmd(eap->arg);
  3293.     if (!eap->skip)
  3294.     {
  3295.     set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"reset");
  3296.     do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
  3297.     do_unlet((char_u *)"g:syntax_cmd");
  3298.     }
  3299. }
  3300.  
  3301. /*
  3302.  * Handle ":syntax manual" command.
  3303.  */
  3304. /* ARGSUSED */
  3305.     static void
  3306. syn_cmd_manual(eap, syncing)
  3307.     exarg_T    *eap;
  3308.     int        syncing;    /* not used */
  3309. {
  3310.     syn_cmd_onoff(eap, "manual");
  3311. }
  3312.  
  3313. /*
  3314.  * Handle ":syntax off" command.
  3315.  */
  3316. /* ARGSUSED */
  3317.     static void
  3318. syn_cmd_off(eap, syncing)
  3319.     exarg_T    *eap;
  3320.     int        syncing;    /* not used */
  3321. {
  3322.     syn_cmd_onoff(eap, "nosyntax");
  3323. }
  3324.  
  3325.     static void
  3326. syn_cmd_onoff(eap, name)
  3327.     exarg_T    *eap;
  3328.     char    *name;
  3329. {
  3330.     char_u    buf[100];
  3331.  
  3332.     eap->nextcmd = check_nextcmd(eap->arg);
  3333.     if (!eap->skip)
  3334.     {
  3335.     STRCPY(buf, "so ");
  3336.     sprintf((char *)buf + 3, SYNTAX_FNAME, name);
  3337.     do_cmdline_cmd(buf);
  3338.     }
  3339. }
  3340.  
  3341. /*
  3342.  * Handle ":syntax [list]" command: list current syntax words.
  3343.  */
  3344.     static void
  3345. syn_cmd_list(eap, syncing)
  3346.     exarg_T    *eap;
  3347.     int        syncing;        /* when TRUE: list syncing items */
  3348. {
  3349.     char_u    *arg = eap->arg;
  3350.     int        id;
  3351.     char_u    *arg_end;
  3352.  
  3353.     eap->nextcmd = find_nextcmd(arg);
  3354.     if (eap->skip)
  3355.     return;
  3356.  
  3357.     if (!syntax_present(curbuf))
  3358.     {
  3359.     MSG(_("No Syntax items defined for this buffer"));
  3360.     return;
  3361.     }
  3362.  
  3363.     if (syncing)
  3364.     {
  3365.     if (curbuf->b_syn_sync_flags & SF_CCOMMENT)
  3366.     {
  3367.         MSG_PUTS(_("syncing on C-style comments"));
  3368.         syn_lines_msg();
  3369.         syn_match_msg();
  3370.         return;
  3371.     }
  3372.     else if (!(curbuf->b_syn_sync_flags & SF_MATCH))
  3373.     {
  3374.         if (curbuf->b_syn_sync_minlines == 0)
  3375.         MSG_PUTS(_("no syncing"));
  3376.         else
  3377.         {
  3378.         MSG_PUTS(_("syncing starts "));
  3379.         msg_outnum(curbuf->b_syn_sync_minlines);
  3380.         MSG_PUTS(_(" lines before top line"));
  3381.         syn_match_msg();
  3382.         }
  3383.         return;
  3384.     }
  3385.     MSG_PUTS_TITLE(_("\n--- Syntax sync items ---"));
  3386.     if (curbuf->b_syn_sync_minlines > 0
  3387.         || curbuf->b_syn_sync_maxlines > 0
  3388.         || curbuf->b_syn_sync_linebreaks > 0)
  3389.     {
  3390.         MSG_PUTS(_("\nsyncing on items"));
  3391.         syn_lines_msg();
  3392.         syn_match_msg();
  3393.     }
  3394.     }
  3395.     else
  3396.     MSG_PUTS_TITLE(_("\n--- Syntax items ---"));
  3397.     if (ends_excmd(*arg))
  3398.     {
  3399.     /*
  3400.      * No argument: List all group IDs and all syntax clusters.
  3401.      */
  3402.     for (id = 1; id <= highlight_ga.ga_len && !got_int; ++id)
  3403.         syn_list_one(id, syncing, FALSE);
  3404.     for (id = 0; id < curbuf->b_syn_clusters.ga_len && !got_int; ++id)
  3405.         syn_list_cluster(id);
  3406.     }
  3407.     else
  3408.     {
  3409.     /*
  3410.      * List the group IDs and syntax clusters that are in the argument.
  3411.      */
  3412.     while (!ends_excmd(*arg) && !got_int)
  3413.     {
  3414.         arg_end = skiptowhite(arg);
  3415.         if (*arg == '@')
  3416.         {
  3417.         id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
  3418.         if (id == 0)
  3419.             EMSG2(_("E392: No such syntax cluster: %s"), arg);
  3420.         else
  3421.             syn_list_cluster(id - SYNID_CLUSTER);
  3422.         }
  3423.         else
  3424.         {
  3425.         id = syn_namen2id(arg, (int)(arg_end - arg));
  3426.         if (id == 0)
  3427.             EMSG2(_(e_nogroup), arg);
  3428.         else
  3429.             syn_list_one(id, syncing, TRUE);
  3430.         }
  3431.         arg = skipwhite(arg_end);
  3432.     }
  3433.     }
  3434.     eap->nextcmd = check_nextcmd(arg);
  3435. }
  3436.  
  3437.     static void
  3438. syn_lines_msg()
  3439. {
  3440.     if (curbuf->b_syn_sync_maxlines > 0 || curbuf->b_syn_sync_minlines > 0)
  3441.     {
  3442.     MSG_PUTS("; ");
  3443.     if (curbuf->b_syn_sync_minlines > 0)
  3444.     {
  3445.         MSG_PUTS(_("minimal "));
  3446.         msg_outnum(curbuf->b_syn_sync_minlines);
  3447.         if (curbuf->b_syn_sync_maxlines)
  3448.         MSG_PUTS(", ");
  3449.     }
  3450.     if (curbuf->b_syn_sync_maxlines > 0)
  3451.     {
  3452.         MSG_PUTS(_("maximal "));
  3453.         msg_outnum(curbuf->b_syn_sync_maxlines);
  3454.     }
  3455.     MSG_PUTS(_(" lines before top line"));
  3456.     }
  3457. }
  3458.  
  3459.     static void
  3460. syn_match_msg()
  3461. {
  3462.     if (curbuf->b_syn_sync_linebreaks > 0)
  3463.     {
  3464.     MSG_PUTS(_("; match "));
  3465.     msg_outnum(curbuf->b_syn_sync_linebreaks);
  3466.     MSG_PUTS(_(" line breaks"));
  3467.     }
  3468. }
  3469.  
  3470. static int  last_matchgroup;
  3471.  
  3472. struct name_list
  3473. {
  3474.     int        flag;
  3475.     char    *name;
  3476. };
  3477.  
  3478. static void syn_list_flags __ARGS((struct name_list *nl, int flags, int attr));
  3479.  
  3480. /*
  3481.  * List one syntax item, for ":syntax" or "syntax list syntax_name".
  3482.  */
  3483.     static void
  3484. syn_list_one(id, syncing, link_only)
  3485.     int        id;
  3486.     int        syncing;        /* when TRUE: list syncing items */
  3487.     int        link_only;        /* when TRUE; list link-only too */
  3488. {
  3489.     int        attr;
  3490.     int        idx;
  3491.     int        did_header = FALSE;
  3492.     synpat_T    *spp;
  3493.     static struct name_list namelist1[] =
  3494.         {
  3495.             {HL_DISPLAY, "display"},
  3496.             {HL_CONTAINED, "contained"},
  3497.             {HL_ONELINE, "oneline"},
  3498.             {HL_KEEPEND, "keepend"},
  3499.             {HL_EXTEND, "extend"},
  3500.             {HL_EXCLUDENL, "excludenl"},
  3501.             {HL_TRANSP, "transparent"},
  3502.             {HL_FOLD, "fold"},
  3503.             {0, NULL}
  3504.         };
  3505.     static struct name_list namelist2[] =
  3506.         {
  3507.             {HL_SKIPWHITE, "skipwhite"},
  3508.             {HL_SKIPNL, "skipnl"},
  3509.             {HL_SKIPEMPTY, "skipempty"},
  3510.             {0, NULL}
  3511.         };
  3512.  
  3513.     attr = hl_attr(HLF_D);        /* highlight like directories */
  3514.  
  3515.     /* list the keywords for "id" */
  3516.     if (!syncing)
  3517.     {
  3518.     did_header = syn_list_keywords(id, curbuf->b_keywtab, FALSE, attr);
  3519.     did_header = syn_list_keywords(id, curbuf->b_keywtab_ic,
  3520.                                 did_header, attr);
  3521.     }
  3522.  
  3523.     /* list the patterns for "id" */
  3524.     for (idx = 0; idx < curbuf->b_syn_patterns.ga_len && !got_int; ++idx)
  3525.     {
  3526.     spp = &(SYN_ITEMS(curbuf)[idx]);
  3527.     if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
  3528.         continue;
  3529.  
  3530.     (void)syn_list_header(did_header, 999, id);
  3531.     did_header = TRUE;
  3532.     last_matchgroup = 0;
  3533.     if (spp->sp_type == SPTYPE_MATCH)
  3534.     {
  3535.         put_pattern("match", ' ', spp, attr);
  3536.         msg_putchar(' ');
  3537.     }
  3538.     else if (spp->sp_type == SPTYPE_START)
  3539.     {
  3540.         while (SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_START)
  3541.         put_pattern("start", '=', &SYN_ITEMS(curbuf)[idx++], attr);
  3542.         if (SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_SKIP)
  3543.         put_pattern("skip", '=', &SYN_ITEMS(curbuf)[idx++], attr);
  3544.         while (idx < curbuf->b_syn_patterns.ga_len
  3545.                   && SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_END)
  3546.         put_pattern("end", '=', &SYN_ITEMS(curbuf)[idx++], attr);
  3547.         --idx;
  3548.         msg_putchar(' ');
  3549.     }
  3550.     syn_list_flags(namelist1, spp->sp_flags, attr);
  3551.  
  3552.     if (spp->sp_cont_list != NULL)
  3553.         put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
  3554.  
  3555.     if (spp->sp_syn.cont_in_list != NULL)
  3556.         put_id_list((char_u *)"containedin",
  3557.                           spp->sp_syn.cont_in_list, attr);
  3558.  
  3559.     if (spp->sp_next_list != NULL)
  3560.     {
  3561.         put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
  3562.         syn_list_flags(namelist2, spp->sp_flags, attr);
  3563.     }
  3564.     if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
  3565.     {
  3566.         if (spp->sp_flags & HL_SYNC_HERE)
  3567.         msg_puts_attr((char_u *)"grouphere", attr);
  3568.         else
  3569.         msg_puts_attr((char_u *)"groupthere", attr);
  3570.         msg_putchar(' ');
  3571.         if (spp->sp_sync_idx >= 0)
  3572.         msg_outtrans(HL_TABLE()[SYN_ITEMS(curbuf)
  3573.                    [spp->sp_sync_idx].sp_syn.id - 1].sg_name);
  3574.         else
  3575.         MSG_PUTS("NONE");
  3576.         msg_putchar(' ');
  3577.     }
  3578.     }
  3579.  
  3580.     /* list the link, if there is one */
  3581.     if (HL_TABLE()[id - 1].sg_link && (did_header || link_only) && !got_int)
  3582.     {
  3583.     (void)syn_list_header(did_header, 999, id);
  3584.     msg_puts_attr((char_u *)"links to", attr);
  3585.     msg_putchar(' ');
  3586.     msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
  3587.     }
  3588. }
  3589.  
  3590.     static void
  3591. syn_list_flags(nl, flags, attr)
  3592.     struct name_list    *nl;
  3593.     int            flags;
  3594.     int            attr;
  3595. {
  3596.     int        i;
  3597.  
  3598.     for (i = 0; nl[i].flag != 0; ++i)
  3599.     if (flags & nl[i].flag)
  3600.     {
  3601.         msg_puts_attr((char_u *)nl[i].name, attr);
  3602.         msg_putchar(' ');
  3603.     }
  3604. }
  3605.  
  3606. /*
  3607.  * List one syntax cluster, for ":syntax" or "syntax list syntax_name".
  3608.  */
  3609.     static void
  3610. syn_list_cluster(id)
  3611.     int id;
  3612. {
  3613.     int        endcol = 15;
  3614.  
  3615.     /* slight hack:  roughly duplicate the guts of syn_list_header() */
  3616.     msg_putchar('\n');
  3617.     msg_outtrans(SYN_CLSTR(curbuf)[id].scl_name);
  3618.  
  3619.     if (msg_col >= endcol)    /* output at least one space */
  3620.     endcol = msg_col + 1;
  3621.     if (Columns <= endcol)    /* avoid hang for tiny window */
  3622.     endcol = Columns - 1;
  3623.  
  3624.     msg_advance(endcol);
  3625.     if (SYN_CLSTR(curbuf)[id].scl_list != NULL)
  3626.     {
  3627.     put_id_list((char_u *)"cluster", SYN_CLSTR(curbuf)[id].scl_list,
  3628.             hl_attr(HLF_D));
  3629.     }
  3630.     else
  3631.     {
  3632.     msg_puts_attr((char_u *)"cluster", hl_attr(HLF_D));
  3633.     msg_puts((char_u *)"=NONE");
  3634.     }
  3635. }
  3636.  
  3637.     static void
  3638. put_id_list(name, list, attr)
  3639.     char_u    *name;
  3640.     short    *list;
  3641.     int        attr;
  3642. {
  3643.     short        *p;
  3644.  
  3645.     msg_puts_attr(name, attr);
  3646.     msg_putchar('=');
  3647.     for (p = list; *p; ++p)
  3648.     {
  3649.     if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
  3650.     {
  3651.         if (p[1])
  3652.         MSG_PUTS("ALLBUT");
  3653.         else
  3654.         MSG_PUTS("ALL");
  3655.     }
  3656.     else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
  3657.     {
  3658.         MSG_PUTS("TOP");
  3659.     }
  3660.     else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
  3661.     {
  3662.         MSG_PUTS("CONTAINED");
  3663.     }
  3664.     else if (*p >= SYNID_CLUSTER)
  3665.     {
  3666.         short scl_id = *p - SYNID_CLUSTER;
  3667.  
  3668.         msg_putchar('@');
  3669.         msg_outtrans(SYN_CLSTR(curbuf)[scl_id].scl_name);
  3670.     }
  3671.     else
  3672.         msg_outtrans(HL_TABLE()[*p - 1].sg_name);
  3673.     if (p[1])
  3674.         msg_putchar(',');
  3675.     }
  3676.     msg_putchar(' ');
  3677. }
  3678.  
  3679.     static void
  3680. put_pattern(s, c, spp, attr)
  3681.     char    *s;
  3682.     int        c;
  3683.     synpat_T    *spp;
  3684.     int        attr;
  3685. {
  3686.     long    n;
  3687.     int        mask;
  3688.     int        first;
  3689.     static char    *sepchars = "/+=-#@\"|'^&";
  3690.     int        i;
  3691.  
  3692.     /* May have to write "matchgroup=group" */
  3693.     if (last_matchgroup != spp->sp_syn_match_id)
  3694.     {
  3695.     last_matchgroup = spp->sp_syn_match_id;
  3696.     msg_puts_attr((char_u *)"matchgroup", attr);
  3697.     msg_putchar('=');
  3698.     if (last_matchgroup == 0)
  3699.         msg_outtrans((char_u *)"NONE");
  3700.     else
  3701.         msg_outtrans(HL_TABLE()[last_matchgroup - 1].sg_name);
  3702.     msg_putchar(' ');
  3703.     }
  3704.  
  3705.     /* output the name of the pattern and an '=' or ' ' */
  3706.     msg_puts_attr((char_u *)s, attr);
  3707.     msg_putchar(c);
  3708.  
  3709.     /* output the pattern, in between a char that is not in the pattern */
  3710.     for (i = 0; vim_strchr(spp->sp_pattern, sepchars[i]) != NULL; )
  3711.     if (sepchars[++i] == NUL)
  3712.     {
  3713.         i = 0;    /* no good char found, just use the first one */
  3714.         break;
  3715.     }
  3716.     msg_putchar(sepchars[i]);
  3717.     msg_outtrans(spp->sp_pattern);
  3718.     msg_putchar(sepchars[i]);
  3719.  
  3720.     /* output any pattern options */
  3721.     first = TRUE;
  3722.     for (i = 0; i < SPO_COUNT; ++i)
  3723.     {
  3724.     mask = (1 << i);
  3725.     if (spp->sp_off_flags & (mask + (mask << SPO_COUNT)))
  3726.     {
  3727.         if (!first)
  3728.         msg_putchar(',');    /* separate with commas */
  3729.         msg_puts((char_u *)spo_name_tab[i]);
  3730.         n = spp->sp_offsets[i];
  3731.         if (i != SPO_LC_OFF)
  3732.         {
  3733.         if (spp->sp_off_flags & mask)
  3734.             msg_putchar('s');
  3735.         else
  3736.             msg_putchar('e');
  3737.         if (n > 0)
  3738.             msg_putchar('+');
  3739.         }
  3740.         if (n || i == SPO_LC_OFF)
  3741.         msg_outnum(n);
  3742.         first = FALSE;
  3743.     }
  3744.     }
  3745.     msg_putchar(' ');
  3746. }
  3747.  
  3748. /*
  3749.  * List or clear the keywords for one syntax group.
  3750.  * Return TRUE if the header has been printed.
  3751.  */
  3752.     static int
  3753. syn_list_keywords(id, ktabp, did_header, attr)
  3754.     int        id;
  3755.     keyentry_T    **ktabp;
  3756.     int        did_header;        /* header has already been printed */
  3757.     int        attr;
  3758. {
  3759.     int        i;
  3760.     int        outlen;
  3761.     keyentry_T    *ktab;
  3762.     int        prev_contained = 0;
  3763.     short    *prev_next_list = NULL;
  3764.     short    *prev_cont_in_list = NULL;
  3765.     int        prev_skipnl = 0;
  3766.     int        prev_skipwhite = 0;
  3767.     int        prev_skipempty = 0;
  3768.  
  3769.     if (ktabp == NULL)
  3770.     return did_header;
  3771.  
  3772.     /*
  3773.      * Unfortunately, this list of keywords is not sorted on alphabet but on
  3774.      * hash value...
  3775.      */
  3776.     for (i = 0; i < KHASH_SIZE; ++i)
  3777.     {
  3778.     for (ktab = ktabp[i]; ktab != NULL && !got_int; ktab = ktab->next)
  3779.     {
  3780.         if (ktab->k_syn.id == id)
  3781.         {
  3782.         if (prev_contained != (ktab->flags & HL_CONTAINED)
  3783.             || prev_skipnl != (ktab->flags & HL_SKIPNL)
  3784.             || prev_skipwhite != (ktab->flags & HL_SKIPWHITE)
  3785.             || prev_skipempty != (ktab->flags & HL_SKIPEMPTY)
  3786.             || prev_cont_in_list != ktab->k_syn.cont_in_list
  3787.             || prev_next_list != ktab->next_list)
  3788.             outlen = 9999;
  3789.         else
  3790.             outlen = (int)STRLEN(ktab->keyword);
  3791.         /* output "contained" and "nextgroup" on each line */
  3792.         if (syn_list_header(did_header, outlen, id))
  3793.         {
  3794.             prev_contained = 0;
  3795.             prev_next_list = NULL;
  3796.             prev_cont_in_list = NULL;
  3797.             prev_skipnl = 0;
  3798.             prev_skipwhite = 0;
  3799.             prev_skipempty = 0;
  3800.         }
  3801.         did_header = TRUE;
  3802.         if (prev_contained != (ktab->flags & HL_CONTAINED))
  3803.         {
  3804.             msg_puts_attr((char_u *)"contained", attr);
  3805.             msg_putchar(' ');
  3806.             prev_contained = (ktab->flags & HL_CONTAINED);
  3807.         }
  3808.         if (ktab->k_syn.cont_in_list != prev_cont_in_list)
  3809.         {
  3810.             put_id_list((char_u *)"containedin",
  3811.                           ktab->k_syn.cont_in_list, attr);
  3812.             msg_putchar(' ');
  3813.             prev_cont_in_list = ktab->k_syn.cont_in_list;
  3814.         }
  3815.         if (ktab->next_list != prev_next_list)
  3816.         {
  3817.             put_id_list((char_u *)"nextgroup", ktab->next_list, attr);
  3818.             msg_putchar(' ');
  3819.             prev_next_list = ktab->next_list;
  3820.             if (ktab->flags & HL_SKIPNL)
  3821.             {
  3822.             msg_puts_attr((char_u *)"skipnl", attr);
  3823.             msg_putchar(' ');
  3824.             prev_skipnl = (ktab->flags & HL_SKIPNL);
  3825.             }
  3826.             if (ktab->flags & HL_SKIPWHITE)
  3827.             {
  3828.             msg_puts_attr((char_u *)"skipwhite", attr);
  3829.             msg_putchar(' ');
  3830.             prev_skipwhite = (ktab->flags & HL_SKIPWHITE);
  3831.             }
  3832.             if (ktab->flags & HL_SKIPEMPTY)
  3833.             {
  3834.             msg_puts_attr((char_u *)"skipempty", attr);
  3835.             msg_putchar(' ');
  3836.             prev_skipempty = (ktab->flags & HL_SKIPEMPTY);
  3837.             }
  3838.         }
  3839.         msg_outtrans(ktab->keyword);
  3840.         }
  3841.     }
  3842.     }
  3843.  
  3844.     return did_header;
  3845. }
  3846.  
  3847.     static void
  3848. syn_clear_keyword(id, ktabp)
  3849.     int        id;
  3850.     keyentry_T    **ktabp;
  3851. {
  3852.     int        i;
  3853.     keyentry_T    *ktab;
  3854.     keyentry_T    *ktab_prev;
  3855.     keyentry_T    *ktab_next;
  3856.  
  3857.     if (ktabp == NULL)        /* no keywords present */
  3858.     return;
  3859.  
  3860.     for (i = 0; i < KHASH_SIZE; ++i)
  3861.     {
  3862.     ktab_prev = NULL;
  3863.     for (ktab = ktabp[i]; ktab != NULL; )
  3864.     {
  3865.         if (ktab->k_syn.id == id)
  3866.         {
  3867.         ktab_next = ktab->next;
  3868.         if (ktab_prev == NULL)
  3869.             ktabp[i] = ktab_next;
  3870.         else
  3871.             ktab_prev->next = ktab_next;
  3872.         vim_free(ktab);
  3873.         ktab = ktab_next;
  3874.         }
  3875.         else
  3876.         {
  3877.         ktab_prev = ktab;
  3878.         ktab = ktab->next;
  3879.         }
  3880.     }
  3881.     }
  3882. }
  3883.  
  3884. /*
  3885.  * Recursive function to free() a branch of a kwordtab.
  3886.  */
  3887.     static void
  3888. free_keywtab(ktabp)
  3889.     keyentry_T    **ktabp;
  3890. {
  3891.     int        i;
  3892.     keyentry_T    *ktab;
  3893.     keyentry_T    *ktab_next;
  3894.  
  3895.     if (ktabp != NULL)
  3896.     {
  3897.     for (i = 0; i < KHASH_SIZE; ++i)
  3898.         for (ktab = ktabp[i]; ktab != NULL; ktab = ktab_next)
  3899.         {
  3900.         ktab_next = ktab->next;
  3901.         vim_free(ktab->next_list);
  3902.         vim_free(ktab->k_syn.cont_in_list);
  3903.         vim_free(ktab);
  3904.         }
  3905.     vim_free(ktabp);
  3906.     }
  3907. }
  3908.  
  3909. /*
  3910.  * Add a keyword to the list of keywords.
  3911.  */
  3912.     static void
  3913. add_keyword(name, id, flags, cont_in_list, next_list)
  3914.     char_u    *name;        /* name of keyword */
  3915.     int        id;        /* group ID for this keyword */
  3916.     int        flags;        /* flags for this keyword */
  3917.     short    *cont_in_list; /* containedin for this keyword */
  3918.     short    *next_list; /* nextgroup for this keyword */
  3919. {
  3920.     keyentry_T    *ktab;
  3921.     keyentry_T    ***ktabpp;
  3922.     int        hash;
  3923.     char_u    *name_ic = name;
  3924.  
  3925.     if (curbuf->b_syn_ic)
  3926.     {
  3927.     name_ic = str_foldcase(name, (int)STRLEN(name));
  3928.     if (name_ic == NULL)
  3929.         name_ic = name;
  3930.     }
  3931.     ktab = (keyentry_T *)alloc((int)(sizeof(keyentry_T) + STRLEN(name_ic)));
  3932.     if (ktab == NULL)
  3933.     return;
  3934.     STRCPY(ktab->keyword, name_ic);
  3935.     if (name_ic != name)
  3936.     vim_free(name_ic);
  3937.     ktab->k_syn.id = id;
  3938.     ktab->k_syn.inc_tag = current_syn_inc_tag;
  3939.     ktab->flags = flags;
  3940.     ktab->k_syn.cont_in_list = copy_id_list(cont_in_list);
  3941.     if (cont_in_list != NULL)
  3942.     curbuf->b_syn_containedin = TRUE;
  3943.     ktab->next_list = copy_id_list(next_list);
  3944.  
  3945.     if (curbuf->b_syn_ic)
  3946.     ktabpp = &curbuf->b_keywtab_ic;
  3947.     else
  3948.     ktabpp = &curbuf->b_keywtab;
  3949.  
  3950.     if (*ktabpp == NULL)
  3951.     {
  3952.     *ktabpp = (keyentry_T **)alloc_clear(
  3953.                     (int)(sizeof(keyentry_T *) * KHASH_SIZE));
  3954.     if (*ktabpp == NULL)
  3955.         return;
  3956.     }
  3957.  
  3958.     hash = syn_khash(ktab->keyword);
  3959.     ktab->next = (*ktabpp)[hash];
  3960.     (*ktabpp)[hash] = ktab;
  3961. }
  3962.  
  3963. /*
  3964.  * Compute a hash value for a keyword.  Uses the ElfHash algorithm, which is
  3965.  * supposed to have an even distribution (suggested by Charles Campbell).
  3966.  */
  3967.     static int
  3968. syn_khash(p)
  3969.     char_u    *p;
  3970. {
  3971.     long_u    hash = 0;
  3972.     long_u    g;
  3973.  
  3974.     while (*p != NUL)
  3975.     {
  3976.     hash = (hash << 4) + *p++;    /* clear low 4 bits of hash, add char */
  3977.     g = hash & 0xf0000000L;        /* g has high 4 bits of hash only */
  3978.     if (g != 0)
  3979.         hash ^= g >> 24;        /* xor g's high 4 bits into hash */
  3980.     }
  3981.  
  3982.     return (int)(hash & KHASH_MASK);
  3983. }
  3984.  
  3985. /*
  3986.  * Get the start and end of the group name argument.
  3987.  * Return a pointer to the first argument.
  3988.  * Return NULL if the end of the command was found instead of further args.
  3989.  */
  3990.     static char_u *
  3991. get_group_name(arg, name_end)
  3992.     char_u    *arg;        /* start of the argument */
  3993.     char_u    **name_end;    /* pointer to end of the name */
  3994. {
  3995.     char_u    *rest;
  3996.  
  3997.     *name_end = skiptowhite(arg);
  3998.     rest = skipwhite(*name_end);
  3999.  
  4000.     /*
  4001.      * Check if there are enough arguments.  The first argument may be a
  4002.      * pattern, where '|' is allowed, so only check for NUL.
  4003.      */
  4004.     if (ends_excmd(*arg) || *rest == NUL)
  4005.     return NULL;
  4006.     return rest;
  4007. }
  4008.  
  4009. /*
  4010.  * Check for syntax command option arguments.
  4011.  * This can be called at any place in the list of arguments, and just picks
  4012.  * out the arguments that are known.  Can be called several times in a row to
  4013.  * collect all options in between other arguments.
  4014.  * Return a pointer to the next argument (which isn't an option).
  4015.  * Return NULL for any error;
  4016.  */
  4017.     static char_u *
  4018. get_syn_options(arg, flagsp, keyword, sync_idx, cont_list,
  4019.                               cont_in_list, next_list)
  4020.     char_u    *arg;        /* next argument */
  4021.     int        *flagsp;    /* flags for contained and transpartent */
  4022.     int        keyword;    /* TRUE for ":syn keyword" */
  4023.     int        *sync_idx;    /* syntax item for "grouphere" argument, NULL
  4024.                    if not allowed */
  4025.     short    **cont_list;    /* group IDs for "contains" argument, NULL if
  4026.                    not allowed */
  4027.     short    **cont_in_list;    /* group IDs for "containedin" argument, NULL
  4028.                    if not allowed */
  4029.     short    **next_list;    /* group IDs for "nextgroup" argument */
  4030. {
  4031.     int        flags;
  4032.     char_u    *gname_start, *gname;
  4033.     int        syn_id;
  4034.     int        len;
  4035.     int        i;
  4036.     int        fidx;
  4037.     static struct flag
  4038.     {
  4039.     char    *name;
  4040.     int    len;
  4041.     int    val;
  4042.     } flagtab[] = { {"contained",   9,    HL_CONTAINED},
  4043.             {"oneline",        7,    HL_ONELINE},
  4044.             {"keepend",        7,    HL_KEEPEND},
  4045.             {"extend",        6,    HL_EXTEND},
  4046.             {"excludenl",   9,    HL_EXCLUDENL},
  4047.             {"transparent", 11, HL_TRANSP},
  4048.             {"skipnl",        6,    HL_SKIPNL},
  4049.             {"skipwhite",   9,    HL_SKIPWHITE},
  4050.             {"skipempty",   9,    HL_SKIPEMPTY},
  4051.             {"grouphere",   9,    HL_SYNC_HERE},
  4052.             {"groupthere",  10,    HL_SYNC_THERE},
  4053.             {"display",        7,    HL_DISPLAY},
  4054.             {"fold",        4,    HL_FOLD},
  4055.         };
  4056. #define MLEN 12
  4057.     char    lowname[MLEN];
  4058.     int        llen;
  4059.  
  4060.     if (arg == NULL)        /* already detected error */
  4061.     return NULL;
  4062.  
  4063.     flags = *flagsp;
  4064.     for (;;)
  4065.     {
  4066.     /* STRNICMP() is a bit slow, change arg to lowercase first and use
  4067.      * STRNCMP() */
  4068.     for (llen = 0; llen < MLEN; ++llen)
  4069.     {
  4070.         if (!isalpha(arg[llen]))
  4071.         break;
  4072.         lowname[llen] = TOLOWER_ASC(arg[llen]);
  4073.     }
  4074.  
  4075.     for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; )
  4076.     {
  4077.         len = flagtab[fidx].len;
  4078.         if (len == llen
  4079.             && STRNCMP(lowname, flagtab[fidx].name, len) == 0
  4080.             && (ends_excmd(arg[len]) || vim_iswhite(arg[len])))
  4081.         {
  4082.         if (keyword
  4083.             && (flagtab[fidx].val == HL_DISPLAY
  4084.                 || flagtab[fidx].val == HL_FOLD
  4085.                 || flagtab[fidx].val == HL_EXTEND))
  4086.         {
  4087.             /* treat "display", "fold" and "extend" as a keyword */
  4088.             fidx = -1;
  4089.             break;
  4090.         }
  4091.  
  4092.         flags |= flagtab[fidx].val;
  4093.         arg = skipwhite(arg + len);
  4094.  
  4095.         if (flagtab[fidx].val == HL_SYNC_HERE
  4096.             || flagtab[fidx].val == HL_SYNC_THERE)
  4097.         {
  4098.             if (sync_idx == NULL)
  4099.             {
  4100.             EMSG(_("E393: group[t]here not accepted here"));
  4101.             return NULL;
  4102.             }
  4103.             gname_start = arg;
  4104.             arg = skiptowhite(arg);
  4105.             if (gname_start == arg)
  4106.             return NULL;
  4107.             gname = vim_strnsave(gname_start, (int)(arg - gname_start));
  4108.             if (gname == NULL)
  4109.             return NULL;
  4110.             if (STRCMP(gname, "NONE") == 0)
  4111.             *sync_idx = NONE_IDX;
  4112.             else
  4113.             {
  4114.             syn_id = syn_name2id(gname);
  4115.             for (i = curbuf->b_syn_patterns.ga_len; --i >= 0; )
  4116.                 if (SYN_ITEMS(curbuf)[i].sp_syn.id == syn_id
  4117.                 && SYN_ITEMS(curbuf)[i].sp_type == SPTYPE_START)
  4118.                 {
  4119.                 *sync_idx = i;
  4120.                 break;
  4121.                 }
  4122.             if (i < 0)
  4123.             {
  4124.                 EMSG2(_("E394: Didn't find region item for %s"), gname);
  4125.                 vim_free(gname);
  4126.                 return NULL;
  4127.             }
  4128.             }
  4129.  
  4130.             vim_free(gname);
  4131.             arg = skipwhite(arg);
  4132.         }
  4133. #ifdef FEAT_FOLDING
  4134.         else if (flagtab[fidx].val == HL_FOLD
  4135.                         && foldmethodIsSyntax(curwin))
  4136.         {
  4137.             /* Need to update folds later. */
  4138.             foldUpdateAll(curwin);
  4139.         }
  4140. #endif
  4141.         break;
  4142.         }
  4143.     }
  4144.     if (fidx >= 0)
  4145.         continue;
  4146.  
  4147.     if (llen == 8 && STRNCMP(lowname, "contains", 8) == 0
  4148.         && (vim_iswhite(arg[8]) || arg[8] == '='))
  4149.     {
  4150.         if (cont_list == NULL)
  4151.         {
  4152.         EMSG(_("E395: contains argument not accepted here"));
  4153.         return NULL;
  4154.         }
  4155.         if (get_id_list(&arg, 8, cont_list) == FAIL)
  4156.         return NULL;
  4157.     }
  4158.     else if (llen == 11 && STRNCMP(lowname, "containedin", 11) == 0
  4159.         && (vim_iswhite(arg[11]) || arg[11] == '='))
  4160.     {
  4161.         if (cont_in_list == NULL)
  4162.         {
  4163.         EMSG(_("E396: containedin argument not accepted here"));
  4164.         return NULL;
  4165.         }
  4166.         if (get_id_list(&arg, 11, cont_in_list) == FAIL)
  4167.         return NULL;
  4168.     }
  4169.     else if (llen == 9 && STRNCMP(lowname, "nextgroup", 9) == 0
  4170.         && (vim_iswhite(arg[9]) || arg[9] == '='))
  4171.     {
  4172.         if (get_id_list(&arg, 9, next_list) == FAIL)
  4173.         return NULL;
  4174.     }
  4175.     else
  4176.         break;
  4177.     }
  4178.  
  4179.     *flagsp = flags;
  4180.  
  4181.     return arg;
  4182. }
  4183.  
  4184. /*
  4185.  * Adjustments to syntax item when declared in a ":syn include"'d file.
  4186.  * Set the contained flag, and if the item is not already contained, add it
  4187.  * to the specified top-level group, if any.
  4188.  */
  4189.     static void
  4190. syn_incl_toplevel(id, flagsp)
  4191.     int        id;
  4192.     int        *flagsp;
  4193. {
  4194.     if ((*flagsp & HL_CONTAINED) || curbuf->b_syn_topgrp == 0)
  4195.     return;
  4196.     *flagsp |= HL_CONTAINED;
  4197.     if (curbuf->b_syn_topgrp >= SYNID_CLUSTER)
  4198.     {
  4199.     /* We have to alloc this, because syn_combine_list() will free it. */
  4200.     short        *grp_list = (short *)alloc((unsigned)(2 * sizeof(short)));
  4201.     int        tlg_id = curbuf->b_syn_topgrp - SYNID_CLUSTER;
  4202.  
  4203.     if (grp_list != NULL)
  4204.     {
  4205.         grp_list[0] = id;
  4206.         grp_list[1] = 0;
  4207.         syn_combine_list(&SYN_CLSTR(curbuf)[tlg_id].scl_list, &grp_list,
  4208.              CLUSTER_ADD);
  4209.     }
  4210.     }
  4211. }
  4212.  
  4213. /*
  4214.  * Handle ":syntax include [@{group-name}] filename" command.
  4215.  */
  4216. /* ARGSUSED */
  4217.     static void
  4218. syn_cmd_include(eap, syncing)
  4219.     exarg_T    *eap;
  4220.     int        syncing;        /* not used */
  4221. {
  4222.     char_u    *arg = eap->arg;
  4223.     int        sgl_id = 1;
  4224.     char_u    *group_name_end;
  4225.     char_u    *rest;
  4226.     char_u    *errormsg = NULL;
  4227.     int        prev_toplvl_grp;
  4228.     int        prev_syn_inc_tag;
  4229.     int        source = FALSE;
  4230.  
  4231.     eap->nextcmd = find_nextcmd(arg);
  4232.     if (eap->skip)
  4233.     return;
  4234.  
  4235.     if (arg[0] == '@')
  4236.     {
  4237.     ++arg;
  4238.     rest = get_group_name(arg, &group_name_end);
  4239.     if (rest == NULL)
  4240.     {
  4241.         EMSG((char_u *)_("E397: Filename required"));
  4242.         return;
  4243.     }
  4244.     sgl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
  4245.     /* separate_nextcmd() and expand_filename() depend on this */
  4246.     eap->arg = rest;
  4247.     }
  4248.  
  4249.     /*
  4250.      * Everything that's left, up to the next command, should be the
  4251.      * filename to include.
  4252.      */
  4253.     eap->argt |= (XFILE | NOSPC);
  4254.     separate_nextcmd(eap);
  4255.     if (*eap->arg == '<' || *eap->arg == '$' || mch_isFullName(eap->arg))
  4256.     {
  4257.     /* For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
  4258.      * file.  Need to expand the file name first.  In other cases
  4259.      * ":runtime!" is used. */
  4260.     source = TRUE;
  4261.     if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL)
  4262.     {
  4263.         if (errormsg != NULL)
  4264.         EMSG(errormsg);
  4265.         return;
  4266.     }
  4267.     }
  4268.  
  4269.     /*
  4270.      * Save and restore the existing top-level grouplist id and ":syn
  4271.      * include" tag around the actual inclusion.
  4272.      */
  4273.     prev_syn_inc_tag = current_syn_inc_tag;
  4274.     current_syn_inc_tag = ++running_syn_inc_tag;
  4275.     prev_toplvl_grp = curbuf->b_syn_topgrp;
  4276.     curbuf->b_syn_topgrp = sgl_id;
  4277.     if (source ? do_source(eap->arg, FALSE, FALSE) == FAIL
  4278.                     : cmd_runtime(eap->arg, TRUE) == FAIL)
  4279.     EMSG2(_(e_notopen), eap->arg);
  4280.     curbuf->b_syn_topgrp = prev_toplvl_grp;
  4281.     current_syn_inc_tag = prev_syn_inc_tag;
  4282. }
  4283.  
  4284. /*
  4285.  * Handle ":syntax keyword {group-name} [{option}] keyword .." command.
  4286.  */
  4287. /* ARGSUSED */
  4288.     static void
  4289. syn_cmd_keyword(eap, syncing)
  4290.     exarg_T    *eap;
  4291.     int        syncing;        /* not used */
  4292. {
  4293.     char_u    *arg = eap->arg;
  4294.     char_u    *group_name_end;
  4295.     int        syn_id;
  4296.     char_u    *rest;
  4297.     char_u    *keyword_copy;
  4298.     char_u    *p;
  4299.     char_u    *first_arg;
  4300.     int        round;
  4301.     int        flags = 0;
  4302.     short    *next_list = NULL;
  4303.     short    *cont_in_list = NULL;
  4304.  
  4305.     rest = get_group_name(arg, &group_name_end);
  4306.  
  4307.     if (rest != NULL)
  4308.     {
  4309.     syn_id = syn_check_group(arg, (int)(group_name_end - arg));
  4310.  
  4311.     /* allocate a buffer, for removing the backslashes in the keyword */
  4312.     keyword_copy = alloc((unsigned)STRLEN(rest) + 1);
  4313.     if (keyword_copy != NULL)
  4314.     {
  4315.         /*
  4316.          * The options given apply to ALL keywords, so all options must be
  4317.          * found before keywords can be created.
  4318.          * round 1: collect the options.
  4319.          * round 2: create the keywords.
  4320.          */
  4321.         first_arg = rest;
  4322.         for (round = 1; round <= 2; ++round)
  4323.         {
  4324.         /*
  4325.          * Isolate each keyword and add an entry for it.
  4326.          */
  4327.         for (rest = first_arg; rest != NULL && !ends_excmd(*rest);
  4328.                                rest = skipwhite(rest))
  4329.         {
  4330.             rest = get_syn_options(rest, &flags, TRUE, NULL,
  4331.                          NULL, &cont_in_list, &next_list);
  4332.             if (rest == NULL || ends_excmd(*rest))
  4333.             break;
  4334.             p = keyword_copy;
  4335.             while (*rest != 0 && !vim_iswhite(*rest))
  4336.             {
  4337.             if (*rest == '\\' && rest[1] != NUL)
  4338.                 ++rest;
  4339.             *p++ = *rest++;
  4340.             }
  4341.             *p = NUL;
  4342.             if (round == 2 && !eap->skip)
  4343.             {
  4344.             for (p = vim_strchr(keyword_copy, '['); ; )
  4345.             {
  4346.                 if (p != NULL)
  4347.                 *p = NUL;
  4348.                 add_keyword(keyword_copy, syn_id, flags,
  4349.                              cont_in_list, next_list);
  4350.                 if (p == NULL || p[1] == NUL || p[1] == ']')
  4351.                 break;
  4352. #ifdef FEAT_MBYTE
  4353.                 if (has_mbyte)
  4354.                 {
  4355.                 int l = (*mb_ptr2len_check)(p + 1);
  4356.  
  4357.                 mch_memmove(p, p + 1, l);
  4358.                 p += l;
  4359.                 }
  4360.                 else
  4361. #endif
  4362.                 {
  4363.                 p[0] = p[1];
  4364.                 ++p;
  4365.                 }
  4366.             }
  4367.             }
  4368.         }
  4369.         if (round == 1)
  4370.             syn_incl_toplevel(syn_id, &flags);
  4371.         }
  4372.         vim_free(keyword_copy);
  4373.     }
  4374.     }
  4375.  
  4376.     if (rest != NULL)
  4377.     eap->nextcmd = check_nextcmd(rest);
  4378.     else
  4379.     EMSG2(_(e_invarg2), arg);
  4380.  
  4381.     vim_free(cont_in_list);
  4382.     vim_free(next_list);
  4383.     redraw_curbuf_later(NOT_VALID);
  4384.     syn_stack_free_all(curbuf);        /* Need to recompute all syntax. */
  4385. }
  4386.  
  4387. /*
  4388.  * Handle ":syntax match {name} [{options}] {pattern} [{options}]".
  4389.  *
  4390.  * Also ":syntax sync match {name} [[grouphere | groupthere] {group-name}] .."
  4391.  */
  4392.     static void
  4393. syn_cmd_match(eap, syncing)
  4394.     exarg_T    *eap;
  4395.     int        syncing;        /* TRUE for ":syntax sync match .. " */
  4396. {
  4397.     char_u    *arg = eap->arg;
  4398.     char_u    *group_name_end;
  4399.     char_u    *rest;
  4400.     synpat_T    item;        /* the item found in the line */
  4401.     int        syn_id;
  4402.     int        idx;
  4403.     int        flags = 0;
  4404.     int        sync_idx = 0;
  4405.     short    *cont_list = NULL;
  4406.     short    *cont_in_list = NULL;
  4407.     short    *next_list = NULL;
  4408.  
  4409.     /* Isolate the group name, check for validity */
  4410.     rest = get_group_name(arg, &group_name_end);
  4411.  
  4412.     /* Get options before the pattern */
  4413.     rest = get_syn_options(rest, &flags, FALSE,
  4414.        syncing ? &sync_idx : NULL, &cont_list, &cont_in_list, &next_list);
  4415.  
  4416.     /* get the pattern. */
  4417.     init_syn_patterns();
  4418.     vim_memset(&item, 0, sizeof(item));
  4419.     rest = get_syn_pattern(rest, &item);
  4420.     if (vim_regcomp_had_eol() && !(flags & HL_EXCLUDENL))
  4421.     flags |= HL_HAS_EOL;
  4422.  
  4423.     /* Get options after the pattern */
  4424.     rest = get_syn_options(rest, &flags, FALSE,
  4425.        syncing ? &sync_idx : NULL, &cont_list, &cont_in_list, &next_list);
  4426.  
  4427.     if (rest != NULL)        /* all arguments are valid */
  4428.     {
  4429.     /*
  4430.      * Check for trailing command and illegal trailing arguments.
  4431.      */
  4432.     eap->nextcmd = check_nextcmd(rest);
  4433.     if (!ends_excmd(*rest) || eap->skip)
  4434.         rest = NULL;
  4435.     else if (ga_grow(&curbuf->b_syn_patterns, 1) != FAIL
  4436.         && (syn_id = syn_check_group(arg,
  4437.                        (int)(group_name_end - arg))) != 0)
  4438.     {
  4439.         syn_incl_toplevel(syn_id, &flags);
  4440.         /*
  4441.          * Store the pattern in the syn_items list
  4442.          */
  4443.         idx = curbuf->b_syn_patterns.ga_len;
  4444.         SYN_ITEMS(curbuf)[idx] = item;
  4445.         SYN_ITEMS(curbuf)[idx].sp_syncing = syncing;
  4446.         SYN_ITEMS(curbuf)[idx].sp_type = SPTYPE_MATCH;
  4447.         SYN_ITEMS(curbuf)[idx].sp_syn.id = syn_id;
  4448.         SYN_ITEMS(curbuf)[idx].sp_syn.inc_tag = current_syn_inc_tag;
  4449.         SYN_ITEMS(curbuf)[idx].sp_flags = flags;
  4450.         SYN_ITEMS(curbuf)[idx].sp_sync_idx = sync_idx;
  4451.         SYN_ITEMS(curbuf)[idx].sp_cont_list = cont_list;
  4452.         SYN_ITEMS(curbuf)[idx].sp_syn.cont_in_list = cont_in_list;
  4453.         if (cont_in_list != NULL)
  4454.         curbuf->b_syn_containedin = TRUE;
  4455.         SYN_ITEMS(curbuf)[idx].sp_next_list = next_list;
  4456.         ++curbuf->b_syn_patterns.ga_len;
  4457.         --curbuf->b_syn_patterns.ga_room;
  4458.  
  4459.         /* remember that we found a match for syncing on */
  4460.         if (flags & (HL_SYNC_HERE|HL_SYNC_THERE))
  4461.         curbuf->b_syn_sync_flags |= SF_MATCH;
  4462. #ifdef FEAT_FOLDING
  4463.         if (flags & HL_FOLD)
  4464.         ++curbuf->b_syn_folditems;
  4465. #endif
  4466.  
  4467.         redraw_curbuf_later(NOT_VALID);
  4468.         syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  4469.         return;    /* don't free the progs and patterns now */
  4470.     }
  4471.     }
  4472.  
  4473.     /*
  4474.      * Something failed, free the allocated memory.
  4475.      */
  4476.     vim_free(item.sp_prog);
  4477.     vim_free(item.sp_pattern);
  4478.     vim_free(cont_list);
  4479.     vim_free(cont_in_list);
  4480.     vim_free(next_list);
  4481.  
  4482.     if (rest == NULL)
  4483.     EMSG2(_(e_invarg2), arg);
  4484. }
  4485.  
  4486. /*
  4487.  * Handle ":syntax region {group-name} [matchgroup={group-name}]
  4488.  *        start {start} .. [skip {skip}] end {end} .. [{options}]".
  4489.  */
  4490.     static void
  4491. syn_cmd_region(eap, syncing)
  4492.     exarg_T    *eap;
  4493.     int        syncing;        /* TRUE for ":syntax sync region .." */
  4494. {
  4495.     char_u        *arg = eap->arg;
  4496.     char_u        *group_name_end;
  4497.     char_u        *rest;            /* next arg, NULL on error */
  4498.     char_u        *key_end;
  4499.     char_u        *key = NULL;
  4500.     char_u        *p;
  4501.     int            item;
  4502. #define ITEM_START        0
  4503. #define ITEM_SKIP        1
  4504. #define ITEM_END        2
  4505. #define ITEM_MATCHGROUP        3
  4506.     struct pat_ptr
  4507.     {
  4508.     synpat_T    *pp_synp;        /* pointer to syn_pattern */
  4509.     int        pp_matchgroup_id;    /* matchgroup ID */
  4510.     struct pat_ptr    *pp_next;        /* pointer to next pat_ptr */
  4511.     }            *(pat_ptrs[3]);
  4512.                     /* patterns found in the line */
  4513.     struct pat_ptr    *ppp;
  4514.     struct pat_ptr    *ppp_next;
  4515.     int            pat_count = 0;        /* nr of syn_patterns found */
  4516.     int            syn_id;
  4517.     int            matchgroup_id = 0;
  4518.     int            not_enough = FALSE;    /* not enough arguments */
  4519.     int            illegal = FALSE;    /* illegal arguments */
  4520.     int            success = FALSE;
  4521.     int            idx;
  4522.     int            flags = 0;
  4523.     short        *cont_list = NULL;
  4524.     short        *cont_in_list = NULL;
  4525.     short        *next_list = NULL;
  4526.  
  4527.     /* Isolate the group name, check for validity */
  4528.     rest = get_group_name(arg, &group_name_end);
  4529.  
  4530.     pat_ptrs[0] = NULL;
  4531.     pat_ptrs[1] = NULL;
  4532.     pat_ptrs[2] = NULL;
  4533.  
  4534.     init_syn_patterns();
  4535.  
  4536.     /*
  4537.      * get the options, patterns and matchgroup.
  4538.      */
  4539.     while (rest != NULL && !ends_excmd(*rest))
  4540.     {
  4541.     /* Check for option arguments */
  4542.     rest = get_syn_options(rest, &flags, FALSE, NULL,
  4543.                        &cont_list, &cont_in_list, &next_list);
  4544.     if (rest == NULL || ends_excmd(*rest))
  4545.         break;
  4546.  
  4547.     /* must be a pattern or matchgroup then */
  4548.     key_end = rest;
  4549.     while (*key_end && !vim_iswhite(*key_end) && *key_end != '=')
  4550.         ++key_end;
  4551.     vim_free(key);
  4552.     key = vim_strnsave_up(rest, (int)(key_end - rest));
  4553.     if (key == NULL)            /* out of memory */
  4554.     {
  4555.         rest = NULL;
  4556.         break;
  4557.     }
  4558.     if (STRCMP(key, "MATCHGROUP") == 0)
  4559.         item = ITEM_MATCHGROUP;
  4560.     else if (STRCMP(key, "START") == 0)
  4561.         item = ITEM_START;
  4562.     else if (STRCMP(key, "END") == 0)
  4563.         item = ITEM_END;
  4564.     else if (STRCMP(key, "SKIP") == 0)
  4565.     {
  4566.         if (pat_ptrs[ITEM_SKIP] != NULL)    /* one skip pattern allowed */
  4567.         {
  4568.         illegal = TRUE;
  4569.         break;
  4570.         }
  4571.         item = ITEM_SKIP;
  4572.     }
  4573.     else
  4574.         break;
  4575.     rest = skipwhite(key_end);
  4576.     if (*rest != '=')
  4577.     {
  4578.         rest = NULL;
  4579.         EMSG2(_("E398: Missing '=': %s"), arg);
  4580.         break;
  4581.     }
  4582.     rest = skipwhite(rest + 1);
  4583.     if (*rest == NUL)
  4584.     {
  4585.         not_enough = TRUE;
  4586.         break;
  4587.     }
  4588.  
  4589.     if (item == ITEM_MATCHGROUP)
  4590.     {
  4591.         p = skiptowhite(rest);
  4592.         if ((p - rest == 4 && STRNCMP(rest, "NONE", 4) == 0) || eap->skip)
  4593.         matchgroup_id = 0;
  4594.         else
  4595.         {
  4596.         matchgroup_id = syn_check_group(rest, (int)(p - rest));
  4597.         if (matchgroup_id == 0)
  4598.         {
  4599.             illegal = TRUE;
  4600.             break;
  4601.         }
  4602.         }
  4603.         rest = skipwhite(p);
  4604.     }
  4605.     else
  4606.     {
  4607.         /*
  4608.          * Allocate room for a syn_pattern, and link it in the list of
  4609.          * syn_patterns for this item, at the start (because the list is
  4610.          * used from end to start).
  4611.          */
  4612.         ppp = (struct pat_ptr *)alloc((unsigned)sizeof(struct pat_ptr));
  4613.         if (ppp == NULL)
  4614.         {
  4615.         rest = NULL;
  4616.         break;
  4617.         }
  4618.         ppp->pp_next = pat_ptrs[item];
  4619.         pat_ptrs[item] = ppp;
  4620.         ppp->pp_synp = (synpat_T *)alloc_clear((unsigned)sizeof(synpat_T));
  4621.         if (ppp->pp_synp == NULL)
  4622.         {
  4623.         rest = NULL;
  4624.         break;
  4625.         }
  4626.  
  4627.         /*
  4628.          * Get the syntax pattern and the following offset(s).
  4629.          */
  4630.         /* Enable the appropriate \z specials. */
  4631.         if (item == ITEM_START)
  4632.         reg_do_extmatch = REX_SET;
  4633.         else if (item == ITEM_SKIP || item == ITEM_END)
  4634.         reg_do_extmatch = REX_USE;
  4635.         rest = get_syn_pattern(rest, ppp->pp_synp);
  4636.         reg_do_extmatch = 0;
  4637.         if (item == ITEM_END && vim_regcomp_had_eol()
  4638.                            && !(flags & HL_EXCLUDENL))
  4639.         ppp->pp_synp->sp_flags |= HL_HAS_EOL;
  4640.         ppp->pp_matchgroup_id = matchgroup_id;
  4641.         ++pat_count;
  4642.     }
  4643.     }
  4644.     vim_free(key);
  4645.     if (illegal || not_enough)
  4646.     rest = NULL;
  4647.  
  4648.     /*
  4649.      * Must have a "start" and "end" pattern.
  4650.      */
  4651.     if (rest != NULL && (pat_ptrs[ITEM_START] == NULL ||
  4652.                           pat_ptrs[ITEM_END] == NULL))
  4653.     {
  4654.     not_enough = TRUE;
  4655.     rest = NULL;
  4656.     }
  4657.  
  4658.     if (rest != NULL)
  4659.     {
  4660.     /*
  4661.      * Check for trailing garbage or command.
  4662.      * If OK, add the item.
  4663.      */
  4664.     eap->nextcmd = check_nextcmd(rest);
  4665.     if (!ends_excmd(*rest) || eap->skip)
  4666.         rest = NULL;
  4667.     else if (ga_grow(&(curbuf->b_syn_patterns), pat_count) != FAIL
  4668.         && (syn_id = syn_check_group(arg,
  4669.                        (int)(group_name_end - arg))) != 0)
  4670.     {
  4671.         syn_incl_toplevel(syn_id, &flags);
  4672.         /*
  4673.          * Store the start/skip/end in the syn_items list
  4674.          */
  4675.         idx = curbuf->b_syn_patterns.ga_len;
  4676.         for (item = ITEM_START; item <= ITEM_END; ++item)
  4677.         {
  4678.         for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp->pp_next)
  4679.         {
  4680.             SYN_ITEMS(curbuf)[idx] = *(ppp->pp_synp);
  4681.             SYN_ITEMS(curbuf)[idx].sp_syncing = syncing;
  4682.             SYN_ITEMS(curbuf)[idx].sp_type =
  4683.                 (item == ITEM_START) ? SPTYPE_START :
  4684.                 (item == ITEM_SKIP) ? SPTYPE_SKIP : SPTYPE_END;
  4685.             SYN_ITEMS(curbuf)[idx].sp_flags |= flags;
  4686.             SYN_ITEMS(curbuf)[idx].sp_syn.id = syn_id;
  4687.             SYN_ITEMS(curbuf)[idx].sp_syn.inc_tag = current_syn_inc_tag;
  4688.             SYN_ITEMS(curbuf)[idx].sp_syn_match_id =
  4689.                             ppp->pp_matchgroup_id;
  4690.             if (item == ITEM_START)
  4691.             {
  4692.             SYN_ITEMS(curbuf)[idx].sp_cont_list = cont_list;
  4693.             SYN_ITEMS(curbuf)[idx].sp_syn.cont_in_list =
  4694.                                  cont_in_list;
  4695.             if (cont_in_list != NULL)
  4696.                 curbuf->b_syn_containedin = TRUE;
  4697.             SYN_ITEMS(curbuf)[idx].sp_next_list = next_list;
  4698.             }
  4699.             ++curbuf->b_syn_patterns.ga_len;
  4700.             --curbuf->b_syn_patterns.ga_room;
  4701.             ++idx;
  4702. #ifdef FEAT_FOLDING
  4703.             if (flags & HL_FOLD)
  4704.             ++curbuf->b_syn_folditems;
  4705. #endif
  4706.         }
  4707.         }
  4708.  
  4709.         redraw_curbuf_later(NOT_VALID);
  4710.         syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  4711.         success = TRUE;        /* don't free the progs and patterns now */
  4712.     }
  4713.     }
  4714.  
  4715.     /*
  4716.      * Free the allocated memory.
  4717.      */
  4718.     for (item = ITEM_START; item <= ITEM_END; ++item)
  4719.     for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp_next)
  4720.     {
  4721.         if (!success)
  4722.         {
  4723.         vim_free(ppp->pp_synp->sp_prog);
  4724.         vim_free(ppp->pp_synp->sp_pattern);
  4725.         }
  4726.         vim_free(ppp->pp_synp);
  4727.         ppp_next = ppp->pp_next;
  4728.         vim_free(ppp);
  4729.     }
  4730.  
  4731.     if (!success)
  4732.     {
  4733.     vim_free(cont_list);
  4734.     vim_free(cont_in_list);
  4735.     vim_free(next_list);
  4736.     if (not_enough)
  4737.         EMSG2(_("E399: Not enough arguments: syntax region %s"), arg);
  4738.     else if (illegal || rest == NULL)
  4739.         EMSG2(_(e_invarg2), arg);
  4740.     }
  4741. }
  4742.  
  4743. /*
  4744.  * A simple syntax group ID comparison function suitable for use in qsort()
  4745.  */
  4746.     static int
  4747. #ifdef __BORLANDC__
  4748. _RTLENTRYF
  4749. #endif
  4750. syn_compare_stub(v1, v2)
  4751.     const void    *v1;
  4752.     const void    *v2;
  4753. {
  4754.     const short    *s1 = v1;
  4755.     const short    *s2 = v2;
  4756.  
  4757.     return (*s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0);
  4758. }
  4759.  
  4760. /*
  4761.  * Combines lists of syntax clusters.
  4762.  * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
  4763.  */
  4764.     static void
  4765. syn_combine_list(clstr1, clstr2, list_op)
  4766.     short    **clstr1;
  4767.     short    **clstr2;
  4768.     int        list_op;
  4769. {
  4770.     int        count1 = 0;
  4771.     int        count2 = 0;
  4772.     short    *g1;
  4773.     short    *g2;
  4774.     short    *clstr = NULL;
  4775.     int        count;
  4776.     int        round;
  4777.  
  4778.     /*
  4779.      * Handle degenerate cases.
  4780.      */
  4781.     if (*clstr2 == NULL)
  4782.     return;
  4783.     if (*clstr1 == NULL || list_op == CLUSTER_REPLACE)
  4784.     {
  4785.     if (list_op == CLUSTER_REPLACE)
  4786.         vim_free(*clstr1);
  4787.     if (list_op == CLUSTER_REPLACE || list_op == CLUSTER_ADD)
  4788.         *clstr1 = *clstr2;
  4789.     else
  4790.         vim_free(*clstr2);
  4791.     return;
  4792.     }
  4793.  
  4794.     for (g1 = *clstr1; *g1; g1++)
  4795.     ++count1;
  4796.     for (g2 = *clstr2; *g2; g2++)
  4797.     ++count2;
  4798.  
  4799.     /*
  4800.      * For speed purposes, sort both lists.
  4801.      */
  4802.     qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
  4803.     qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
  4804.  
  4805.     /*
  4806.      * We proceed in two passes; in round 1, we count the elements to place
  4807.      * in the new list, and in round 2, we allocate and populate the new
  4808.      * list.  For speed, we use a mergesort-like method, adding the smaller
  4809.      * of the current elements in each list to the new list.
  4810.      */
  4811.     for (round = 1; round <= 2; round++)
  4812.     {
  4813.     g1 = *clstr1;
  4814.     g2 = *clstr2;
  4815.     count = 0;
  4816.  
  4817.     /*
  4818.      * First, loop through the lists until one of them is empty.
  4819.      */
  4820.     while (*g1 && *g2)
  4821.     {
  4822.         /*
  4823.          * We always want to add from the first list.
  4824.          */
  4825.         if (*g1 < *g2)
  4826.         {
  4827.         if (round == 2)
  4828.             clstr[count] = *g1;
  4829.         count++;
  4830.         g1++;
  4831.         continue;
  4832.         }
  4833.         /*
  4834.          * We only want to add from the second list if we're adding the
  4835.          * lists.
  4836.          */
  4837.         if (list_op == CLUSTER_ADD)
  4838.         {
  4839.         if (round == 2)
  4840.             clstr[count] = *g2;
  4841.         count++;
  4842.         }
  4843.         if (*g1 == *g2)
  4844.         g1++;
  4845.         g2++;
  4846.     }
  4847.  
  4848.     /*
  4849.      * Now add the leftovers from whichever list didn't get finished
  4850.      * first.  As before, we only want to add from the second list if
  4851.      * we're adding the lists.
  4852.      */
  4853.     for (; *g1; g1++, count++)
  4854.         if (round == 2)
  4855.         clstr[count] = *g1;
  4856.     if (list_op == CLUSTER_ADD)
  4857.         for (; *g2; g2++, count++)
  4858.         if (round == 2)
  4859.             clstr[count] = *g2;
  4860.  
  4861.     if (round == 1)
  4862.     {
  4863.         /*
  4864.          * If the group ended up empty, we don't need to allocate any
  4865.          * space for it.
  4866.          */
  4867.         if (count == 0)
  4868.         {
  4869.         clstr = NULL;
  4870.         break;
  4871.         }
  4872.         clstr = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
  4873.         if (clstr == NULL)
  4874.         break;
  4875.         clstr[count] = 0;
  4876.     }
  4877.     }
  4878.  
  4879.     /*
  4880.      * Finally, put the new list in place.
  4881.      */
  4882.     vim_free(*clstr1);
  4883.     vim_free(*clstr2);
  4884.     *clstr1 = clstr;
  4885. }
  4886.  
  4887. /*
  4888.  * Lookup a syntax cluster name and return it's ID.
  4889.  * If it is not found, 0 is returned.
  4890.  */
  4891.     static int
  4892. syn_scl_name2id(name)
  4893.     char_u    *name;
  4894. {
  4895.     int        i;
  4896.     char_u    *name_u;
  4897.  
  4898.     /* Avoid using stricmp() too much, it's slow on some systems */
  4899.     name_u = vim_strsave_up(name);
  4900.     if (name_u == NULL)
  4901.     return 0;
  4902.     for (i = curbuf->b_syn_clusters.ga_len; --i >= 0; )
  4903.     if (SYN_CLSTR(curbuf)[i].scl_name_u != NULL
  4904.         && STRCMP(name_u, SYN_CLSTR(curbuf)[i].scl_name_u) == 0)
  4905.         break;
  4906.     vim_free(name_u);
  4907.     return (i < 0 ? 0 : i + SYNID_CLUSTER);
  4908. }
  4909.  
  4910. /*
  4911.  * Like syn_scl_name2id(), but take a pointer + length argument.
  4912.  */
  4913.     static int
  4914. syn_scl_namen2id(linep, len)
  4915.     char_u  *linep;
  4916.     int        len;
  4917. {
  4918.     char_u  *name;
  4919.     int        id = 0;
  4920.  
  4921.     name = vim_strnsave(linep, len);
  4922.     if (name != NULL)
  4923.     {
  4924.     id = syn_scl_name2id(name);
  4925.     vim_free(name);
  4926.     }
  4927.     return id;
  4928. }
  4929.  
  4930. /*
  4931.  * Find syntax cluster name in the table and return it's ID.
  4932.  * The argument is a pointer to the name and the length of the name.
  4933.  * If it doesn't exist yet, a new entry is created.
  4934.  * Return 0 for failure.
  4935.  */
  4936.     static int
  4937. syn_check_cluster(pp, len)
  4938.     char_u    *pp;
  4939.     int        len;
  4940. {
  4941.     int        id;
  4942.     char_u    *name;
  4943.  
  4944.     name = vim_strnsave(pp, len);
  4945.     if (name == NULL)
  4946.     return 0;
  4947.  
  4948.     id = syn_scl_name2id(name);
  4949.     if (id == 0)            /* doesn't exist yet */
  4950.     id = syn_add_cluster(name);
  4951.     else
  4952.     vim_free(name);
  4953.     return id;
  4954. }
  4955.  
  4956. /*
  4957.  * Add new syntax cluster and return it's ID.
  4958.  * "name" must be an allocated string, it will be consumed.
  4959.  * Return 0 for failure.
  4960.  */
  4961.     static int
  4962. syn_add_cluster(name)
  4963.     char_u        *name;
  4964. {
  4965.     int len;
  4966.  
  4967.     /*
  4968.      * First call for this growarray: init growing array.
  4969.      */
  4970.     if (curbuf->b_syn_clusters.ga_data == NULL)
  4971.     {
  4972.     curbuf->b_syn_clusters.ga_itemsize = sizeof(struct syn_cluster);
  4973.     curbuf->b_syn_clusters.ga_growsize = 10;
  4974.     }
  4975.  
  4976.     /*
  4977.      * Make room for at least one other cluster entry.
  4978.      */
  4979.     if (ga_grow(&curbuf->b_syn_clusters, 1) == FAIL)
  4980.     {
  4981.     vim_free(name);
  4982.     return 0;
  4983.     }
  4984.     len = curbuf->b_syn_clusters.ga_len;
  4985.  
  4986.     vim_memset(&(SYN_CLSTR(curbuf)[len]), 0, sizeof(struct syn_cluster));
  4987.     SYN_CLSTR(curbuf)[len].scl_name = name;
  4988.     SYN_CLSTR(curbuf)[len].scl_name_u = vim_strsave_up(name);
  4989.     SYN_CLSTR(curbuf)[len].scl_list = NULL;
  4990.     ++curbuf->b_syn_clusters.ga_len;
  4991.     --curbuf->b_syn_clusters.ga_room;
  4992.  
  4993.     return len + SYNID_CLUSTER;
  4994. }
  4995.  
  4996. /*
  4997.  * Handle ":syntax cluster {cluster-name} [contains={groupname},..]
  4998.  *        [add={groupname},..] [remove={groupname},..]".
  4999.  */
  5000. /* ARGSUSED */
  5001.     static void
  5002. syn_cmd_cluster(eap, syncing)
  5003.     exarg_T    *eap;
  5004.     int        syncing;        /* not used */
  5005. {
  5006.     char_u    *arg = eap->arg;
  5007.     char_u    *group_name_end;
  5008.     char_u    *rest;
  5009.     int        scl_id;
  5010.     short    *clstr_list;
  5011.     int        got_clstr = FALSE;
  5012.     int        opt_len;
  5013.     int        list_op;
  5014.  
  5015.     eap->nextcmd = find_nextcmd(arg);
  5016.     if (eap->skip)
  5017.     return;
  5018.  
  5019.     rest = get_group_name(arg, &group_name_end);
  5020.  
  5021.     if (rest != NULL)
  5022.     {
  5023.     scl_id = syn_check_cluster(arg, (int)(group_name_end - arg))
  5024.             - SYNID_CLUSTER;
  5025.  
  5026.     for (;;)
  5027.     {
  5028.         if (STRNICMP(rest, "add", 3) == 0
  5029.             && (vim_iswhite(rest[3]) || rest[3] == '='))
  5030.         {
  5031.         opt_len = 3;
  5032.         list_op = CLUSTER_ADD;
  5033.         }
  5034.         else if (STRNICMP(rest, "remove", 6) == 0
  5035.             && (vim_iswhite(rest[6]) || rest[6] == '='))
  5036.         {
  5037.         opt_len = 6;
  5038.         list_op = CLUSTER_SUBTRACT;
  5039.         }
  5040.         else if (STRNICMP(rest, "contains", 8) == 0
  5041.             && (vim_iswhite(rest[8]) || rest[8] == '='))
  5042.         {
  5043.         opt_len = 8;
  5044.         list_op = CLUSTER_REPLACE;
  5045.         }
  5046.         else
  5047.         break;
  5048.  
  5049.         clstr_list = NULL;
  5050.         if (get_id_list(&rest, opt_len, &clstr_list) == FAIL)
  5051.         {
  5052.         EMSG2(_(e_invarg2), rest);
  5053.         break;
  5054.         }
  5055.         syn_combine_list(&SYN_CLSTR(curbuf)[scl_id].scl_list,
  5056.                  &clstr_list, list_op);
  5057.         got_clstr = TRUE;
  5058.     }
  5059.  
  5060.     if (got_clstr)
  5061.     {
  5062.         redraw_curbuf_later(NOT_VALID);
  5063.         syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  5064.     }
  5065.     }
  5066.  
  5067.     if (!got_clstr)
  5068.     EMSG(_("E400: No cluster specified"));
  5069.     if (rest == NULL || !ends_excmd(*rest))
  5070.     EMSG2(_(e_invarg2), arg);
  5071. }
  5072.  
  5073. /*
  5074.  * On first call for current buffer: Init growing array.
  5075.  */
  5076.     static void
  5077. init_syn_patterns()
  5078. {
  5079.     curbuf->b_syn_patterns.ga_itemsize = sizeof(synpat_T);
  5080.     curbuf->b_syn_patterns.ga_growsize = 10;
  5081. }
  5082.  
  5083. /*
  5084.  * Get one pattern for a ":syntax match" or ":syntax region" command.
  5085.  * Stores the pattern and program in a synpat_T.
  5086.  * Returns a pointer to the next argument, or NULL in case of an error.
  5087.  */
  5088.     static char_u *
  5089. get_syn_pattern(arg, ci)
  5090.     char_u    *arg;
  5091.     synpat_T    *ci;
  5092. {
  5093.     char_u    *end;
  5094.     int        *p;
  5095.     int        idx;
  5096.     char_u    *cpo_save;
  5097.  
  5098.     /* need at least three chars */
  5099.     if (arg == NULL || arg[1] == NUL || arg[2] == NUL)
  5100.     return NULL;
  5101.  
  5102.     end = skip_regexp(arg + 1, *arg, TRUE, NULL);
  5103.     if (*end != *arg)                /* end delimiter not found */
  5104.     {
  5105.     EMSG2(_("E401: Pattern delimiter not found: %s"), arg);
  5106.     return NULL;
  5107.     }
  5108.     /* store the pattern and compiled regexp program */
  5109.     if ((ci->sp_pattern = vim_strnsave(arg + 1, (int)(end - arg - 1))) == NULL)
  5110.     return NULL;
  5111.  
  5112.     /* Make 'cpoptions' empty, to avoid the 'l' flag */
  5113.     cpo_save = p_cpo;
  5114.     p_cpo = (char_u *)"";
  5115.     ci->sp_prog = vim_regcomp(ci->sp_pattern, RE_MAGIC);
  5116.     p_cpo = cpo_save;
  5117.  
  5118.     if (ci->sp_prog == NULL)
  5119.     return NULL;
  5120.     ci->sp_ic = curbuf->b_syn_ic;
  5121.  
  5122.     /*
  5123.      * Check for a match, highlight or region offset.
  5124.      */
  5125.     ++end;
  5126.     do
  5127.     {
  5128.     for (idx = SPO_COUNT; --idx >= 0; )
  5129.         if (STRNCMP(end, spo_name_tab[idx], 3) == 0)
  5130.         break;
  5131.     if (idx >= 0)
  5132.     {
  5133.         p = &(ci->sp_offsets[idx]);
  5134.         if (idx != SPO_LC_OFF)
  5135.         switch (end[3])
  5136.         {
  5137.             case 's':   break;
  5138.             case 'b':   break;
  5139.             case 'e':   idx += SPO_COUNT; break;
  5140.             default:    idx = -1; break;
  5141.         }
  5142.         if (idx >= 0)
  5143.         {
  5144.         ci->sp_off_flags |= (1 << idx);
  5145.         if (idx == SPO_LC_OFF)        /* lc=99 */
  5146.         {
  5147.             end += 3;
  5148.             *p = getdigits(&end);
  5149.  
  5150.             /* "lc=" offset automatically sets "ms=" offset */
  5151.             if (!(ci->sp_off_flags & (1 << SPO_MS_OFF)))
  5152.             {
  5153.             ci->sp_off_flags |= (1 << SPO_MS_OFF);
  5154.             ci->sp_offsets[SPO_MS_OFF] = *p;
  5155.             }
  5156.         }
  5157.         else                /* yy=x+99 */
  5158.         {
  5159.             end += 4;
  5160.             if (*end == '+')
  5161.             {
  5162.             ++end;
  5163.             *p = getdigits(&end);        /* positive offset */
  5164.             }
  5165.             else if (*end == '-')
  5166.             {
  5167.             ++end;
  5168.             *p = -getdigits(&end);        /* negative offset */
  5169.             }
  5170.         }
  5171.         if (*end != ',')
  5172.             break;
  5173.         ++end;
  5174.         }
  5175.     }
  5176.     } while (idx >= 0);
  5177.  
  5178.     if (!ends_excmd(*end) && !vim_iswhite(*end))
  5179.     {
  5180.     EMSG2(_("E402: Garbage after pattern: %s"), arg);
  5181.     return NULL;
  5182.     }
  5183.     return skipwhite(end);
  5184. }
  5185.  
  5186. /*
  5187.  * Handle ":syntax sync .." command.
  5188.  */
  5189. /* ARGSUSED */
  5190.     static void
  5191. syn_cmd_sync(eap, syncing)
  5192.     exarg_T    *eap;
  5193.     int        syncing;        /* not used */
  5194. {
  5195.     char_u    *arg_start = eap->arg;
  5196.     char_u    *arg_end;
  5197.     char_u    *key = NULL;
  5198.     char_u    *next_arg;
  5199.     int        illegal = FALSE;
  5200.     int        finished = FALSE;
  5201.     long    n;
  5202.     char_u    *cpo_save;
  5203.  
  5204.     if (ends_excmd(*arg_start))
  5205.     {
  5206.     syn_cmd_list(eap, TRUE);
  5207.     return;
  5208.     }
  5209.  
  5210.     while (!ends_excmd(*arg_start))
  5211.     {
  5212.     arg_end = skiptowhite(arg_start);
  5213.     next_arg = skipwhite(arg_end);
  5214.     vim_free(key);
  5215.     key = vim_strnsave_up(arg_start, (int)(arg_end - arg_start));
  5216.     if (STRCMP(key, "CCOMMENT") == 0)
  5217.     {
  5218.         if (!eap->skip)
  5219.         curbuf->b_syn_sync_flags |= SF_CCOMMENT;
  5220.         if (!ends_excmd(*next_arg))
  5221.         {
  5222.         arg_end = skiptowhite(next_arg);
  5223.         if (!eap->skip)
  5224.             curbuf->b_syn_sync_id = syn_check_group(next_arg,
  5225.                            (int)(arg_end - next_arg));
  5226.         next_arg = skipwhite(arg_end);
  5227.         }
  5228.         else if (!eap->skip)
  5229.         curbuf->b_syn_sync_id = syn_name2id((char_u *)"Comment");
  5230.     }
  5231.     else if (  STRNCMP(key, "LINES", 5) == 0
  5232.         || STRNCMP(key, "MINLINES", 8) == 0
  5233.         || STRNCMP(key, "MAXLINES", 8) == 0
  5234.         || STRNCMP(key, "LINEBREAKS", 10) == 0)
  5235.     {
  5236.         if (key[4] == 'S')
  5237.         arg_end = key + 6;
  5238.         else if (key[0] == 'L')
  5239.         arg_end = key + 11;
  5240.         else
  5241.         arg_end = key + 9;
  5242.         if (arg_end[-1] != '=' || !isdigit(*arg_end))
  5243.         {
  5244.         illegal = TRUE;
  5245.         break;
  5246.         }
  5247.         n = getdigits(&arg_end);
  5248.         if (!eap->skip)
  5249.         {
  5250.         if (key[4] == 'B')
  5251.             curbuf->b_syn_sync_linebreaks = n;
  5252.         else if (key[1] == 'A')
  5253.             curbuf->b_syn_sync_maxlines = n;
  5254.         else
  5255.             curbuf->b_syn_sync_minlines = n;
  5256.         }
  5257.     }
  5258.     else if (STRCMP(key, "FROMSTART") == 0)
  5259.     {
  5260.         if (!eap->skip)
  5261.         {
  5262.         curbuf->b_syn_sync_minlines = MAXLNUM;
  5263.         curbuf->b_syn_sync_maxlines = 0;
  5264.         }
  5265.     }
  5266.     else if (STRCMP(key, "LINECONT") == 0)
  5267.     {
  5268.         if (curbuf->b_syn_linecont_pat != NULL)
  5269.         {
  5270.         EMSG(_("E403: syntax sync: line continuations pattern specified twice"));
  5271.         finished = TRUE;
  5272.         break;
  5273.         }
  5274.         arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE, NULL);
  5275.         if (*arg_end != *next_arg)        /* end delimiter not found */
  5276.         {
  5277.         illegal = TRUE;
  5278.         break;
  5279.         }
  5280.  
  5281.         if (!eap->skip)
  5282.         {
  5283.         /* store the pattern and compiled regexp program */
  5284.         if ((curbuf->b_syn_linecont_pat = vim_strnsave(next_arg + 1,
  5285.                       (int)(arg_end - next_arg - 1))) == NULL)
  5286.         {
  5287.             finished = TRUE;
  5288.             break;
  5289.         }
  5290.         curbuf->b_syn_linecont_ic = curbuf->b_syn_ic;
  5291.  
  5292.         /* Make 'cpoptions' empty, to avoid the 'l' flag */
  5293.         cpo_save = p_cpo;
  5294.         p_cpo = (char_u *)"";
  5295.         curbuf->b_syn_linecont_prog =
  5296.                 vim_regcomp(curbuf->b_syn_linecont_pat, RE_MAGIC);
  5297.         p_cpo = cpo_save;
  5298.  
  5299.         if (curbuf->b_syn_linecont_prog == NULL)
  5300.         {
  5301.             vim_free(curbuf->b_syn_linecont_pat);
  5302.             curbuf->b_syn_linecont_pat = NULL;
  5303.             finished = TRUE;
  5304.             break;
  5305.         }
  5306.         }
  5307.         next_arg = skipwhite(arg_end + 1);
  5308.     }
  5309.     else
  5310.     {
  5311.         eap->arg = next_arg;
  5312.         if (STRCMP(key, "MATCH") == 0)
  5313.         syn_cmd_match(eap, TRUE);
  5314.         else if (STRCMP(key, "REGION") == 0)
  5315.         syn_cmd_region(eap, TRUE);
  5316.         else if (STRCMP(key, "CLEAR") == 0)
  5317.         syn_cmd_clear(eap, TRUE);
  5318.         else
  5319.         illegal = TRUE;
  5320.         finished = TRUE;
  5321.         break;
  5322.     }
  5323.     arg_start = next_arg;
  5324.     }
  5325.     vim_free(key);
  5326.     if (illegal)
  5327.     EMSG2(_("E404: Illegal arguments: %s"), arg_start);
  5328.     else if (!finished)
  5329.     {
  5330.     eap->nextcmd = check_nextcmd(arg_start);
  5331.     redraw_curbuf_later(NOT_VALID);
  5332.     syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  5333.     }
  5334. }
  5335.  
  5336. /*
  5337.  * Convert a line of highlight group names into a list of group ID numbers.
  5338.  * "arg" should point to the "contains" or "nextgroup" keyword.
  5339.  * "arg" is advanced to after the last group name.
  5340.  * Careful: the argument is modified (NULs added).
  5341.  * returns FAIL for some error, OK for success.
  5342.  */
  5343.     static int
  5344. get_id_list(arg, keylen, list)
  5345.     char_u    **arg;
  5346.     int        keylen;        /* length of keyword */
  5347.     short    **list;        /* where to store the resulting list, if not
  5348.                    NULL, the list is silently skipped! */
  5349. {
  5350.     char_u    *p = NULL;
  5351.     char_u    *end;
  5352.     int        round;
  5353.     int        count;
  5354.     int        total_count = 0;
  5355.     short    *retval = NULL;
  5356.     char_u    *name;
  5357.     regmatch_T    regmatch;
  5358.     int        id;
  5359.     int        i;
  5360.     int        failed = FALSE;
  5361.  
  5362.     /*
  5363.      * We parse the list twice:
  5364.      * round == 1: count the number of items, allocate the array.
  5365.      * round == 2: fill the array with the items.
  5366.      * In round 1 new groups may be added, causing the number of items to
  5367.      * grow when a regexp is used.  In that case round 1 is done once again.
  5368.      */
  5369.     for (round = 1; round <= 2; ++round)
  5370.     {
  5371.     /*
  5372.      * skip "contains"
  5373.      */
  5374.     p = skipwhite(*arg + keylen);
  5375.     if (*p != '=')
  5376.     {
  5377.         EMSG2(_("E405: Missing equal sign: %s"), *arg);
  5378.         break;
  5379.     }
  5380.     p = skipwhite(p + 1);
  5381.     if (ends_excmd(*p))
  5382.     {
  5383.         EMSG2(_("E406: Empty argument: %s"), *arg);
  5384.         break;
  5385.     }
  5386.  
  5387.     /*
  5388.      * parse the arguments after "contains"
  5389.      */
  5390.     count = 0;
  5391.     while (!ends_excmd(*p))
  5392.     {
  5393.         for (end = p; *end && !vim_iswhite(*end) && *end != ','; ++end)
  5394.         ;
  5395.         name = alloc((int)(end - p + 3));        /* leave room for "^$" */
  5396.         if (name == NULL)
  5397.         {
  5398.         failed = TRUE;
  5399.         break;
  5400.         }
  5401.         STRNCPY(name + 1, p, end - p);
  5402.         name[end - p + 1] = NUL;
  5403.         if (       STRCMP(name + 1, "ALLBUT") == 0
  5404.             || STRCMP(name + 1, "ALL") == 0
  5405.             || STRCMP(name + 1, "TOP") == 0
  5406.             || STRCMP(name + 1, "CONTAINED") == 0)
  5407.         {
  5408.         if (TOUPPER_ASC(**arg) != 'C')
  5409.         {
  5410.             EMSG2(_("E407: %s not allowed here"), name + 1);
  5411.             failed = TRUE;
  5412.             vim_free(name);
  5413.             break;
  5414.         }
  5415.         if (count != 0)
  5416.         {
  5417.             EMSG2(_("E408: %s must be first in contains list"), name + 1);
  5418.             failed = TRUE;
  5419.             vim_free(name);
  5420.             break;
  5421.         }
  5422.         if (name[1] == 'A')
  5423.             id = SYNID_ALLBUT;
  5424.         else if (name[1] == 'T')
  5425.             id = SYNID_TOP;
  5426.         else
  5427.             id = SYNID_CONTAINED;
  5428.         id += current_syn_inc_tag;
  5429.         }
  5430.         else if (name[1] == '@')
  5431.         {
  5432.         id = syn_check_cluster(name + 2, (int)(end - p - 1));
  5433.         }
  5434.         else
  5435.         {
  5436.         /*
  5437.          * Handle full group name.
  5438.          */
  5439.         if (vim_strpbrk(name + 1, (char_u *)"\\.*^$~[") == NULL)
  5440.             id = syn_check_group(name + 1, (int)(end - p));
  5441.         else
  5442.         {
  5443.             /*
  5444.              * Handle match of regexp with group names.
  5445.              */
  5446.             *name = '^';
  5447.             STRCAT(name, "$");
  5448.             regmatch.regprog = vim_regcomp(name, RE_MAGIC);
  5449.             if (regmatch.regprog == NULL)
  5450.             {
  5451.             failed = TRUE;
  5452.             vim_free(name);
  5453.             break;
  5454.             }
  5455.  
  5456.             regmatch.rm_ic = TRUE;
  5457.             id = 0;
  5458.             for (i = highlight_ga.ga_len; --i >= 0; )
  5459.             {
  5460.             if (vim_regexec(®match, HL_TABLE()[i].sg_name,
  5461.                                   (colnr_T)0))
  5462.             {
  5463.                 if (round == 2)
  5464.                 {
  5465.                 /* Got more items than expected; can happen
  5466.                  * when adding items that match:
  5467.                  * "contains=a.*b,axb".
  5468.                  * Go back to first round */
  5469.                 if (count >= total_count)
  5470.                 {
  5471.                     vim_free(retval);
  5472.                     round = 1;
  5473.                 }
  5474.                 else
  5475.                     retval[count] = i + 1;
  5476.                 }
  5477.                 ++count;
  5478.                 id = -1;        /* remember that we found one */
  5479.             }
  5480.             }
  5481.             vim_free(regmatch.regprog);
  5482.         }
  5483.         }
  5484.         vim_free(name);
  5485.         if (id == 0)
  5486.         {
  5487.         EMSG2(_("E409: Unknown group name: %s"), p);
  5488.         failed = TRUE;
  5489.         break;
  5490.         }
  5491.         if (id > 0)
  5492.         {
  5493.         if (round == 2)
  5494.         {
  5495.             /* Got more items than expected, go back to first round */
  5496.             if (count >= total_count)
  5497.             {
  5498.             vim_free(retval);
  5499.             round = 1;
  5500.             }
  5501.             else
  5502.             retval[count] = id;
  5503.         }
  5504.         ++count;
  5505.         }
  5506.         p = skipwhite(end);
  5507.         if (*p != ',')
  5508.         break;
  5509.         p = skipwhite(p + 1);    /* skip comma in between arguments */
  5510.     }
  5511.     if (failed)
  5512.         break;
  5513.     if (round == 1)
  5514.     {
  5515.         retval = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
  5516.         if (retval == NULL)
  5517.         break;
  5518.         retval[count] = 0;        /* zero means end of the list */
  5519.         total_count = count;
  5520.     }
  5521.     }
  5522.  
  5523.     *arg = p;
  5524.     if (failed || retval == NULL)
  5525.     {
  5526.     vim_free(retval);
  5527.     return FAIL;
  5528.     }
  5529.  
  5530.     if (*list == NULL)
  5531.     *list = retval;
  5532.     else
  5533.     vim_free(retval);    /* list already found, don't overwrite it */
  5534.  
  5535.     return OK;
  5536. }
  5537.  
  5538. /*
  5539.  * Make a copy of an ID list.
  5540.  */
  5541.     static short *
  5542. copy_id_list(list)
  5543.     short   *list;
  5544. {
  5545.     int        len;
  5546.     int        count;
  5547.     short   *retval;
  5548.  
  5549.     if (list == NULL)
  5550.     return NULL;
  5551.  
  5552.     for (count = 0; list[count]; ++count)
  5553.     ;
  5554.     len = (count + 1) * sizeof(short);
  5555.     retval = (short *)alloc((unsigned)len);
  5556.     if (retval != NULL)
  5557.     mch_memmove(retval, list, (size_t)len);
  5558.  
  5559.     return retval;
  5560. }
  5561.  
  5562. /*
  5563.  * Check if syntax group "ssp" is in the ID list "list" of "cur_si".
  5564.  * "cur_si" can be NULL if not checking the "containedin" list.
  5565.  * Used to check if a syntax item is in the "contains" or "nextgroup" list of
  5566.  * the current item.
  5567.  * This function is called very often, keep it fast!!
  5568.  */
  5569.     static int
  5570. in_id_list(cur_si, list, ssp, contained)
  5571.     stateitem_T    *cur_si;    /* current item or NULL */
  5572.     short    *list;        /* id list */
  5573.     struct sp_syn *ssp;        /* group id and ":syn include" tag of group */
  5574.     int        contained;    /* group id is contained */
  5575. {
  5576.     int        retval;
  5577.     short    *scl_list;
  5578.     short    item;
  5579.     short    id = ssp->id;
  5580.     static int    depth = 0;
  5581.     int        r;
  5582.  
  5583.     /* If spp has a "containedin" list and "cur_si" is in it, return TRUE. */
  5584.     if (cur_si != NULL && ssp->cont_in_list != NULL)
  5585.     {
  5586.     /* Ignore transparent items without a contains argument.  Double check
  5587.      * that we don't go back past the first one. */
  5588.     while ((cur_si->si_flags & HL_TRANS_CONT)
  5589.         && cur_si > (stateitem_T *)(current_state.ga_data))
  5590.         --cur_si;
  5591.     /* cur_si->si_idx is -1 for keywords, these never contain anything. */
  5592.     if (cur_si->si_idx >= 0 && in_id_list(NULL, ssp->cont_in_list,
  5593.         &(SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_syn),
  5594.           SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_flags & HL_CONTAINED))
  5595.         return TRUE;
  5596.     }
  5597.  
  5598.     if (list == NULL)
  5599.     return FALSE;
  5600.  
  5601.     /*
  5602.      * If list is ID_LIST_ALL, we are in a transparent item that isn't
  5603.      * inside anything.  Only allow not-contained groups.
  5604.      */
  5605.     if (list == ID_LIST_ALL)
  5606.     return !contained;
  5607.  
  5608.     /*
  5609.      * If the first item is "ALLBUT", return TRUE if "id" is NOT in the
  5610.      * contains list.  We also require that "id" is at the same ":syn include"
  5611.      * level as the list.
  5612.      */
  5613.     item = *list;
  5614.     if (item >= SYNID_ALLBUT && item < SYNID_CLUSTER)
  5615.     {
  5616.     if (item < SYNID_TOP)
  5617.     {
  5618.         /* ALL or ALLBUT: accept all groups in the same file */
  5619.         if (item - SYNID_ALLBUT != ssp->inc_tag)
  5620.         return FALSE;
  5621.     }
  5622.     else if (item < SYNID_CONTAINED)
  5623.     {
  5624.         /* TOP: accept all not-contained groups in the same file */
  5625.         if (item - SYNID_TOP != ssp->inc_tag || contained)
  5626.         return FALSE;
  5627.     }
  5628.     else
  5629.     {
  5630.         /* CONTAINED: accept all contained groups in the same file */
  5631.         if (item - SYNID_CONTAINED != ssp->inc_tag || !contained)
  5632.         return FALSE;
  5633.     }
  5634.     item = *++list;
  5635.     retval = FALSE;
  5636.     }
  5637.     else
  5638.     retval = TRUE;
  5639.  
  5640.     /*
  5641.      * Return "retval" if id is in the contains list.
  5642.      */
  5643.     while (item != 0)
  5644.     {
  5645.     if (item == id)
  5646.         return retval;
  5647.     if (item >= SYNID_CLUSTER)
  5648.     {
  5649.         scl_list = SYN_CLSTR(syn_buf)[item - SYNID_CLUSTER].scl_list;
  5650.         /* restrict recursiveness to 30 to avoid an endless loop for a
  5651.          * cluster that includes itself (indirectly) */
  5652.         if (scl_list != NULL && depth < 30)
  5653.         {
  5654.         ++depth;
  5655.         r = in_id_list(NULL, scl_list, ssp, contained);
  5656.         --depth;
  5657.         if (r)
  5658.             return retval;
  5659.         }
  5660.     }
  5661.     item = *++list;
  5662.     }
  5663.     return !retval;
  5664. }
  5665.  
  5666. struct subcommand
  5667. {
  5668.     char    *name;                /* subcommand name */
  5669.     void    (*func)__ARGS((exarg_T *, int));    /* function to call */
  5670. };
  5671.  
  5672. static struct subcommand subcommands[] =
  5673. {
  5674.     {"case",        syn_cmd_case},
  5675.     {"clear",        syn_cmd_clear},
  5676.     {"cluster",        syn_cmd_cluster},
  5677.     {"enable",        syn_cmd_enable},
  5678.     {"include",        syn_cmd_include},
  5679.     {"keyword",        syn_cmd_keyword},
  5680.     {"list",        syn_cmd_list},
  5681.     {"manual",        syn_cmd_manual},
  5682.     {"match",        syn_cmd_match},
  5683.     {"on",        syn_cmd_on},
  5684.     {"off",        syn_cmd_off},
  5685.     {"region",        syn_cmd_region},
  5686.     {"reset",        syn_cmd_reset},
  5687.     {"sync",        syn_cmd_sync},
  5688.     {"",        syn_cmd_list},
  5689.     {NULL, NULL}
  5690. };
  5691.  
  5692. /*
  5693.  * ":syntax".
  5694.  * This searches the subcommands[] table for the subcommand name, and calls a
  5695.  * syntax_subcommand() function to do the rest.
  5696.  */
  5697.     void
  5698. ex_syntax(eap)
  5699.     exarg_T    *eap;
  5700. {
  5701.     char_u    *arg = eap->arg;
  5702.     char_u    *subcmd_end;
  5703.     char_u    *subcmd_name;
  5704.     int        i;
  5705.  
  5706.     syn_cmdlinep = eap->cmdlinep;
  5707.  
  5708.     /* isolate subcommand name */
  5709.     for (subcmd_end = arg; ASCII_ISALPHA(*subcmd_end); ++subcmd_end)
  5710.     ;
  5711.     subcmd_name = vim_strnsave(arg, (int)(subcmd_end - arg));
  5712.     if (subcmd_name != NULL)
  5713.     {
  5714.     if (eap->skip)        /* skip error messages for all subcommands */
  5715.         ++emsg_skip;
  5716.     for (i = 0; ; ++i)
  5717.     {
  5718.         if (subcommands[i].name == NULL)
  5719.         {
  5720.         EMSG2(_("E410: Invalid :syntax subcommand: %s"), subcmd_name);
  5721.         break;
  5722.         }
  5723.         if (STRCMP(subcmd_name, (char_u *)subcommands[i].name) == 0)
  5724.         {
  5725.         eap->arg = skipwhite(subcmd_end);
  5726.         (subcommands[i].func)(eap, FALSE);
  5727.         break;
  5728.         }
  5729.     }
  5730.     vim_free(subcmd_name);
  5731.     if (eap->skip)
  5732.         --emsg_skip;
  5733.     }
  5734. }
  5735.  
  5736.     int
  5737. syntax_present(buf)
  5738.     buf_T    *buf;
  5739. {
  5740.     return (buf->b_syn_patterns.ga_len != 0
  5741.         || buf->b_syn_clusters.ga_len != 0
  5742.         || curbuf->b_keywtab != NULL
  5743.         || curbuf->b_keywtab_ic != NULL);
  5744. }
  5745.  
  5746. #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
  5747.  
  5748. static enum
  5749. {
  5750.     EXP_SUBCMD,        /* expand ":syn" sub-commands */
  5751.     EXP_CASE        /* expand ":syn case" arguments */
  5752. } expand_what;
  5753.  
  5754.  
  5755. /*
  5756.  * Handle command line completion for :syntax command.
  5757.  */
  5758.     void
  5759. set_context_in_syntax_cmd(xp, arg)
  5760.     expand_T    *xp;
  5761.     char_u    *arg;
  5762. {
  5763.     char_u    *p;
  5764.  
  5765.     /* Default: expand subcommands */
  5766.     xp->xp_context = EXPAND_SYNTAX;
  5767.     expand_what = EXP_SUBCMD;
  5768.     xp->xp_pattern = arg;
  5769.     include_link = FALSE;
  5770.     include_default = FALSE;
  5771.  
  5772.     /* (part of) subcommand already typed */
  5773.     if (*arg != NUL)
  5774.     {
  5775.     p = skiptowhite(arg);
  5776.     if (*p != NUL)            /* past first word */
  5777.     {
  5778.         xp->xp_pattern = skipwhite(p);
  5779.         if (*skiptowhite(xp->xp_pattern) != NUL)
  5780.         xp->xp_context = EXPAND_NOTHING;
  5781.         else if (STRNICMP(arg, "case", p - arg) == 0)
  5782.         expand_what = EXP_CASE;
  5783.         else if (  STRNICMP(arg, "keyword", p - arg) == 0
  5784.             || STRNICMP(arg, "region", p - arg) == 0
  5785.             || STRNICMP(arg, "match", p - arg) == 0
  5786.             || STRNICMP(arg, "list", p - arg) == 0)
  5787.         xp->xp_context = EXPAND_HIGHLIGHT;
  5788.         else
  5789.         xp->xp_context = EXPAND_NOTHING;
  5790.     }
  5791.     }
  5792. }
  5793.  
  5794. static char *(case_args[]) = {"match", "ignore", NULL};
  5795.  
  5796. /*
  5797.  * Function given to ExpandGeneric() to obtain the list syntax names for
  5798.  * expansion.
  5799.  */
  5800. /*ARGSUSED*/
  5801.     char_u *
  5802. get_syntax_name(xp, idx)
  5803.     expand_T    *xp;
  5804.     int        idx;
  5805. {
  5806.     if (expand_what == EXP_SUBCMD)
  5807.     return (char_u *)subcommands[idx].name;
  5808.     return (char_u *)case_args[idx];
  5809. }
  5810.  
  5811. #endif /* FEAT_CMDL_COMPL */
  5812.  
  5813. #if defined(FEAT_EVAL) || defined(FEAT_PRINTER) || defined(PROTO)
  5814. /*
  5815.  * Function called for expression evaluation: get syntax ID at file position.
  5816.  */
  5817.     int
  5818. syn_get_id(lnum, col, trans)
  5819.     long    lnum;
  5820.     long    col;
  5821.     int        trans;        /* remove transparancy */
  5822. {
  5823.     /* When the position is not after the current position and in the same
  5824.      * line of the same buffer, need to restart parsing. */
  5825.     if (curwin->w_buffer != syn_buf
  5826.         || lnum != current_lnum
  5827.         || col < (long)current_col)
  5828.     syntax_start(curwin, lnum);
  5829.  
  5830.     (void)get_syntax_attr((colnr_T)col);
  5831.  
  5832.     return (trans ? current_trans_id : current_id);
  5833. }
  5834. #endif
  5835.  
  5836. #if defined(FEAT_FOLDING) || defined(PROTO)
  5837. /*
  5838.  * Function called to get folding level for line "lnum" in window "wp".
  5839.  */
  5840.     int
  5841. syn_get_foldlevel(wp, lnum)
  5842.     win_T    *wp;
  5843.     long    lnum;
  5844. {
  5845.     int        level = 0;
  5846.     int        i;
  5847.  
  5848.     /* Return quickly when there are no fold items at all. */
  5849.     if (wp->w_buffer->b_syn_folditems != 0)
  5850.     {
  5851.     syntax_start(wp, lnum);
  5852.  
  5853.     for (i = 0; i < current_state.ga_len; ++i)
  5854.         if (CUR_STATE(i).si_flags & HL_FOLD)
  5855.         ++level;
  5856.     }
  5857.     if (level > wp->w_p_fdn)
  5858.     level = wp->w_p_fdn;
  5859.     return level;
  5860. }
  5861. #endif
  5862.  
  5863. #endif /* FEAT_SYN_HL */
  5864.  
  5865.  
  5866. /**************************************
  5867.  *  Highlighting stuff              *
  5868.  **************************************/
  5869.  
  5870. /*
  5871.  * The default highlight groups.  These are compiled-in for fast startup and
  5872.  * they still work when the runtime files can't be found.
  5873.  * When making changes here, also change runtime/colors/default.vim!
  5874.  */
  5875. static char *(highlight_init_both[]) =
  5876.     {
  5877. #ifdef FEAT_GUI
  5878.     "Cursor guibg=fg guifg=bg",
  5879.     "lCursor guibg=fg guifg=bg",    /* should be different, but what? */
  5880. #endif
  5881.     "ErrorMsg term=standout ctermbg=DarkRed ctermfg=White guibg=Red guifg=White",
  5882.     "IncSearch term=reverse cterm=reverse gui=reverse",
  5883.     "ModeMsg term=bold cterm=bold gui=bold",
  5884.     "NonText term=bold ctermfg=Blue gui=bold guifg=Blue",
  5885.     "StatusLine term=reverse,bold cterm=reverse,bold gui=reverse,bold",
  5886.     "StatusLineNC term=reverse cterm=reverse gui=reverse",
  5887.     "VertSplit term=reverse cterm=reverse gui=reverse",
  5888.     "Visual term=reverse cterm=reverse gui=reverse guifg=Grey guibg=fg",
  5889.     "VisualNOS term=underline,bold cterm=underline,bold gui=underline,bold",
  5890.     "DiffText term=reverse cterm=bold ctermbg=Red gui=bold guibg=Red",
  5891.     NULL
  5892.     };
  5893.  
  5894. static char *(highlight_init_light[]) =
  5895.     {
  5896.     "Directory term=bold ctermfg=DarkBlue guifg=Blue",
  5897.     "LineNr term=underline ctermfg=Brown guifg=Brown",
  5898.     "MoreMsg term=bold ctermfg=DarkGreen gui=bold guifg=SeaGreen",
  5899.     "Normal gui=NONE",
  5900.     "Question term=standout ctermfg=DarkGreen gui=bold guifg=SeaGreen",
  5901.     "Search term=reverse ctermbg=Yellow ctermfg=NONE guibg=Yellow guifg=NONE",
  5902.     "SpecialKey term=bold ctermfg=DarkBlue guifg=Blue",
  5903.     "Title term=bold ctermfg=DarkMagenta gui=bold guifg=Magenta",
  5904.     "WarningMsg term=standout ctermfg=DarkRed guifg=Red",
  5905.     "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
  5906.     "Folded term=standout ctermbg=Grey ctermfg=DarkBlue guibg=LightGrey guifg=DarkBlue",
  5907.     "FoldColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue",
  5908.     "DiffAdd term=bold ctermbg=LightBlue guibg=LightBlue",
  5909.     "DiffChange term=bold ctermbg=LightMagenta guibg=LightMagenta",
  5910.     "DiffDelete term=bold ctermfg=Blue ctermbg=LightCyan gui=bold guifg=Blue guibg=LightCyan",
  5911.     NULL
  5912.     };
  5913.  
  5914. static char *(highlight_init_dark[]) =
  5915.     {
  5916.     "Directory term=bold ctermfg=LightCyan guifg=Cyan",
  5917.     "LineNr term=underline ctermfg=Yellow guifg=Yellow",
  5918.     "MoreMsg term=bold ctermfg=LightGreen gui=bold guifg=SeaGreen",
  5919.     "Normal gui=NONE",
  5920.     "Question term=standout ctermfg=LightGreen gui=bold guifg=Green",
  5921.     "Search term=reverse ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
  5922.     "SpecialKey term=bold ctermfg=LightBlue guifg=Cyan",
  5923.     "Title term=bold ctermfg=LightMagenta gui=bold guifg=Magenta",
  5924.     "WarningMsg term=standout ctermfg=LightRed guifg=Red",
  5925.     "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
  5926.     "Folded term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=DarkGrey guifg=Cyan",
  5927.     "FoldColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan",
  5928.     "DiffAdd term=bold ctermbg=DarkBlue guibg=DarkBlue",
  5929.     "DiffChange term=bold ctermbg=DarkMagenta guibg=DarkMagenta",
  5930.     "DiffDelete term=bold ctermfg=Blue ctermbg=DarkCyan gui=bold guifg=Blue guibg=DarkCyan",
  5931.     NULL
  5932.     };
  5933.  
  5934.     void
  5935. init_highlight(both, reset)
  5936.     int        both;        /* include groups where 'bg' doesn't matter */
  5937.     int        reset;        /* clear group first */
  5938. {
  5939.     int        i;
  5940.     char    **pp;
  5941.     static int    had_both = FALSE;
  5942. #ifdef FEAT_EVAL
  5943.     char_u    *p;
  5944.  
  5945.     /*
  5946.      * Try finding the color scheme file.  Used when a color file was loaded
  5947.      * and 'background' or 't_Co' is changed.
  5948.      */
  5949.     p = get_var_value((char_u *)"g:colors_name");
  5950.     if (p != NULL && load_colors(p) == OK)
  5951.     return;
  5952. #endif
  5953.  
  5954.     /*
  5955.      * Didn't use a color file, use the compiled-in colors.
  5956.      */
  5957.     if (both)
  5958.     {
  5959.     had_both = TRUE;
  5960.     pp = highlight_init_both;
  5961.     for (i = 0; pp[i] != NULL; ++i)
  5962.         do_highlight((char_u *)pp[i], reset, TRUE);
  5963.     }
  5964.     else if (!had_both)
  5965.     /* Don't do anything before the call with both == TRUE from main().
  5966.      * Not everything has been setup then, and that call will overrule
  5967.      * everything anyway. */
  5968.     return;
  5969.  
  5970.     if (*p_bg == 'l')
  5971.     pp = highlight_init_light;
  5972.     else
  5973.     pp = highlight_init_dark;
  5974.     for (i = 0; pp[i] != NULL; ++i)
  5975.     do_highlight((char_u *)pp[i], reset, TRUE);
  5976.  
  5977. #ifdef FEAT_SYN_HL
  5978.     /*
  5979.      * If syntax highlighting is enabled load the highlighting for it.
  5980.      */
  5981.     if (get_var_value((char_u *)"g:syntax_on") != NULL)
  5982.     (void)cmd_runtime((char_u *)"syntax/syncolor.vim", TRUE);
  5983. #endif
  5984. }
  5985.  
  5986. /*
  5987.  * Load color file "p".
  5988.  * Return OK for success, FAIL for failure.
  5989.  */
  5990.     int
  5991. load_colors(p)
  5992.     char_u    *p;
  5993. {
  5994.     char_u    *buf;
  5995.     int        retval = FAIL;
  5996.     static int    recursive = FALSE;
  5997.  
  5998.     /* When being called recursively, this is probably because setting
  5999.      * 'background' caused the highlighting to be reloaded.  This means it is
  6000.      * working, thus we should return OK. */
  6001.     if (recursive)
  6002.     return OK;
  6003.  
  6004.     recursive = TRUE;
  6005.     buf = alloc((unsigned)(STRLEN(p) + 12));
  6006.     if (buf != NULL)
  6007.     {
  6008.     sprintf((char *)buf, "colors/%s.vim", p);
  6009.     retval = cmd_runtime(buf, FALSE);
  6010.     vim_free(buf);
  6011.     }
  6012.     recursive = FALSE;
  6013.  
  6014.     return retval;
  6015. }
  6016.  
  6017. /*
  6018.  * Handle the ":highlight .." command.
  6019.  * When using ":hi clear" this is called recursively for each group with
  6020.  * "forceit" and "init" both TRUE.
  6021.  */
  6022.     void
  6023. do_highlight(line, forceit, init)
  6024.     char_u    *line;
  6025.     int        forceit;
  6026.     int        init;        /* TRUE when called for initializing */
  6027. {
  6028.     char_u    *name_end;
  6029.     char_u    *p;
  6030.     char_u    *linep;
  6031.     char_u    *key_start;
  6032.     char_u    *arg_start;
  6033.     char_u    *key = NULL, *arg = NULL;
  6034.     long    i;
  6035.     int        off;
  6036.     int        len;
  6037.     int        attr;
  6038.     int        id;
  6039.     int        idx;
  6040.     int        dodefault = FALSE;
  6041.     int        doclear = FALSE;
  6042.     int        dolink = FALSE;
  6043.     int        error = FALSE;
  6044.     int        color;
  6045.     int        is_normal_group = FALSE;    /* "Normal" group */
  6046. #ifdef FEAT_GUI_X11
  6047.     int        is_menu_group = FALSE;        /* "Menu" group */
  6048.     int        is_scrollbar_group = FALSE;    /* "Scrollbar" group */
  6049.     int        is_tooltip_group = FALSE;    /* "Tooltip" group */
  6050.     int        do_colors = FALSE;        /* need to update colors? */
  6051. #else
  6052. # define is_menu_group 0
  6053. # define is_tooltip_group 0
  6054. #endif
  6055.  
  6056.     /*
  6057.      * If no argument, list current highlighting.
  6058.      */
  6059.     if (ends_excmd(*line))
  6060.     {
  6061.     for (i = 1; i <= highlight_ga.ga_len && !got_int; ++i)
  6062.         /* TODO: only call when the group has attributes set */
  6063.         highlight_list_one((int)i);
  6064.     return;
  6065.     }
  6066.  
  6067.     /*
  6068.      * Isolate the name.
  6069.      */
  6070.     name_end = skiptowhite(line);
  6071.     linep = skipwhite(name_end);
  6072.  
  6073.     /*
  6074.      * Check for "default" argument.
  6075.      */
  6076.     if (STRNCMP(line, "default", name_end - line) == 0)
  6077.     {
  6078.     dodefault = TRUE;
  6079.     line = linep;
  6080.     name_end = skiptowhite(line);
  6081.     linep = skipwhite(name_end);
  6082.     }
  6083.  
  6084.     /*
  6085.      * Check for "clear" or "link" argument.
  6086.      */
  6087.     if (STRNCMP(line, "clear", name_end - line) == 0)
  6088.     doclear = TRUE;
  6089.     if (STRNCMP(line, "link", name_end - line) == 0)
  6090.     dolink = TRUE;
  6091.  
  6092.     /*
  6093.      * ":highlight {group-name}": list highlighting for one group.
  6094.      */
  6095.     if (!doclear && !dolink && ends_excmd(*linep))
  6096.     {
  6097.     id = syn_namen2id(line, (int)(name_end - line));
  6098.     if (id == 0)
  6099.         EMSG2(_("E411: highlight group not found: %s"), line);
  6100.     else
  6101.         highlight_list_one(id);
  6102.     return;
  6103.     }
  6104.  
  6105.     /*
  6106.      * Handle ":highlight link {from} {to}" command.
  6107.      */
  6108.     if (dolink)
  6109.     {
  6110.     char_u        *from_start = linep;
  6111.     char_u        *from_end;
  6112.     char_u        *to_start;
  6113.     char_u        *to_end;
  6114.     int        from_id;
  6115.     int        to_id;
  6116.  
  6117.     from_end = skiptowhite(from_start);
  6118.     to_start = skipwhite(from_end);
  6119.     to_end     = skiptowhite(to_start);
  6120.  
  6121.     if (ends_excmd(*from_start) || ends_excmd(*to_start))
  6122.     {
  6123.         EMSG2(_("E412: Not enough arguments: \":highlight link %s\""),
  6124.                                   from_start);
  6125.         return;
  6126.     }
  6127.  
  6128.     if (!ends_excmd(*skipwhite(to_end)))
  6129.     {
  6130.         EMSG2(_("E413: Too many arguments: \":highlight link %s\""), from_start);
  6131.         return;
  6132.     }
  6133.  
  6134.     from_id = syn_check_group(from_start, (int)(from_end - from_start));
  6135.     if (STRNCMP(to_start, "NONE", 4) == 0)
  6136.         to_id = 0;
  6137.     else
  6138.         to_id = syn_check_group(to_start, (int)(to_end - to_start));
  6139.  
  6140.     if (from_id > 0 && (!init || HL_TABLE()[from_id - 1].sg_set == 0))
  6141.     {
  6142.         /*
  6143.          * Don't allow a link when there already is some highlighting
  6144.          * for the group, unless '!' is used
  6145.          */
  6146.         if (to_id > 0 && !forceit && !init
  6147.                    && hl_has_settings(from_id - 1, dodefault))
  6148.         {
  6149.         if (sourcing_name == NULL && !dodefault)
  6150.             EMSG(_("E414: group has settings, highlight link ignored"));
  6151.         }
  6152.         else
  6153.         {
  6154.         if (!init)
  6155.             HL_TABLE()[from_id - 1].sg_set |= SG_LINK;
  6156.         HL_TABLE()[from_id - 1].sg_link = to_id;
  6157.         redraw_all_later(NOT_VALID);
  6158.         }
  6159.     }
  6160.  
  6161.     /* Only call highlight_changed() once, after sourcing a syntax file */
  6162.     need_highlight_changed = TRUE;
  6163.  
  6164.     return;
  6165.     }
  6166.  
  6167.     if (doclear)
  6168.     {
  6169.     /*
  6170.      * ":highlight clear [group]" command.
  6171.      */
  6172.     line = linep;
  6173.     if (ends_excmd(*line))
  6174.     {
  6175. #ifdef FEAT_GUI
  6176.         /* First, we do not destroy the old values, but allocate the new
  6177.          * ones and update the display. THEN we destroy the old values.
  6178.          * If we destroy the old values first, then the old values
  6179.          * (such as GuiFont's or GuiFontset's) will still be displayed but
  6180.          * invalid because they were free'd.
  6181.          */
  6182.         if (gui.in_use)
  6183.         {
  6184. # ifdef FEAT_BEVAL_TIP
  6185.         gui_init_tooltip_font();
  6186. # endif
  6187. # if defined(FEAT_MENU) && (defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MOTIF))
  6188.         gui_init_menu_font();
  6189. # endif
  6190.         }
  6191. # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_X11)
  6192.         gui_mch_def_colors();
  6193. # endif
  6194. # ifdef FEAT_GUI_X11
  6195. #  ifdef FEAT_MENU
  6196.  
  6197.         /* This only needs to be done when there is no Menu highlight
  6198.          * group defined by default, which IS currently the case.
  6199.          */
  6200.         gui_mch_new_menu_colors();
  6201. #  endif
  6202.         if (gui.in_use)
  6203.         {
  6204.         gui_new_scrollbar_colors();
  6205. #  ifdef FEAT_BEVAL
  6206.         gui_mch_new_tooltip_colors();
  6207. #  endif
  6208. #  ifdef FEAT_MENU
  6209.         gui_mch_new_menu_font();
  6210. #  endif
  6211.         }
  6212. # endif
  6213.  
  6214.         /* Ok, we're done allocating the new default graphics items.
  6215.          * The screen should already be refreshed at this point.
  6216.          * It is now Ok to clear out the old data.
  6217.          */
  6218. #endif
  6219. #ifdef FEAT_EVAL
  6220.         do_unlet((char_u *)"colors_name");
  6221. #endif
  6222.         restore_cterm_colors();
  6223.  
  6224.         /*
  6225.          * Clear all default highlight groups and load the defaults.
  6226.          */
  6227.         for (idx = 0; idx < highlight_ga.ga_len; ++idx)
  6228.         highlight_clear(idx);
  6229.         init_highlight(TRUE, TRUE);
  6230. #ifdef FEAT_GUI
  6231.         if (gui.in_use)
  6232.         highlight_gui_started();
  6233. #endif
  6234.         highlight_changed();
  6235.         redraw_later_clear();
  6236.         return;
  6237.     }
  6238.     name_end = skiptowhite(line);
  6239.     linep = skipwhite(name_end);
  6240.     }
  6241.  
  6242.     /*
  6243.      * Find the group name in the table.  If it does not exist yet, add it.
  6244.      */
  6245.     id = syn_check_group(line, (int)(name_end - line));
  6246.     if (id == 0)            /* failed (out of memory) */
  6247.     return;
  6248.     idx = id - 1;            /* index is ID minus one */
  6249.  
  6250.     /* Return if "default" was used and the group already has settings. */
  6251.     if (dodefault && hl_has_settings(idx, TRUE))
  6252.     return;
  6253.  
  6254.     if (STRCMP(HL_TABLE()[idx].sg_name_u, "NORMAL") == 0)
  6255.     is_normal_group = TRUE;
  6256. #ifdef FEAT_GUI_X11
  6257.     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "MENU") == 0)
  6258.     is_menu_group = TRUE;
  6259.     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "SCROLLBAR") == 0)
  6260.     is_scrollbar_group = TRUE;
  6261.     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "TOOLTIP") == 0)
  6262.     is_tooltip_group = TRUE;
  6263. #endif
  6264.  
  6265.     /* Clear the highlighting for ":hi clear {group}" and ":hi clear". */
  6266.     if (doclear || (forceit && init))
  6267.     {
  6268.     highlight_clear(idx);
  6269.     if (!doclear)
  6270.         HL_TABLE()[idx].sg_set = 0;
  6271.     }
  6272.  
  6273.     if (!doclear)
  6274.       while (!ends_excmd(*linep))
  6275.       {
  6276.     key_start = linep;
  6277.     if (*linep == '=')
  6278.     {
  6279.         EMSG2(_("E415: unexpected equal sign: %s"), key_start);
  6280.         error = TRUE;
  6281.         break;
  6282.     }
  6283.  
  6284.     /*
  6285.      * Isolate the key ("term", "ctermfg", "ctermbg", "font", "guifg" or
  6286.      * "guibg").
  6287.      */
  6288.     while (*linep && !vim_iswhite(*linep) && *linep != '=')
  6289.         ++linep;
  6290.     vim_free(key);
  6291.     key = vim_strnsave_up(key_start, (int)(linep - key_start));
  6292.     if (key == NULL)
  6293.     {
  6294.         error = TRUE;
  6295.         break;
  6296.     }
  6297.     linep = skipwhite(linep);
  6298.  
  6299.     if (STRCMP(key, "NONE") == 0)
  6300.     {
  6301.         if (!init || HL_TABLE()[idx].sg_set == 0)
  6302.         {
  6303.         if (!init)
  6304.             HL_TABLE()[idx].sg_set |= SG_TERM+SG_CTERM+SG_GUI;
  6305.         highlight_clear(idx);
  6306.         }
  6307.         continue;
  6308.     }
  6309.  
  6310.     /*
  6311.      * Check for the equal sign.
  6312.      */
  6313.     if (*linep != '=')
  6314.     {
  6315.         EMSG2(_("E416: missing equal sign: %s"), key_start);
  6316.         error = TRUE;
  6317.         break;
  6318.     }
  6319.     ++linep;
  6320.  
  6321.     /*
  6322.      * Isolate the argument.
  6323.      */
  6324.     linep = skipwhite(linep);
  6325.     if (*linep == '\'')        /* guifg='color name' */
  6326.     {
  6327.         arg_start = ++linep;
  6328.         linep = vim_strchr(linep, '\'');
  6329.     }
  6330.     else
  6331.     {
  6332.         arg_start = linep;
  6333.         linep = skiptowhite(linep);
  6334.     }
  6335.     if (linep == arg_start)
  6336.     {
  6337.         EMSG2(_("E417: missing argument: %s"), key_start);
  6338.         error = TRUE;
  6339.         break;
  6340.     }
  6341.     vim_free(arg);
  6342.     arg = vim_strnsave(arg_start, (int)(linep - arg_start));
  6343.     if (arg == NULL)
  6344.     {
  6345.         error = TRUE;
  6346.         break;
  6347.     }
  6348.     if (*linep == '\'')
  6349.         ++linep;
  6350.  
  6351.     /*
  6352.      * Store the argument.
  6353.      */
  6354.     if (  STRCMP(key, "TERM") == 0
  6355.         || STRCMP(key, "CTERM") == 0
  6356.         || STRCMP(key, "GUI") == 0)
  6357.     {
  6358.         attr = 0;
  6359.         off = 0;
  6360.         while (arg[off] != NUL)
  6361.         {
  6362.         for (i = sizeof(hl_attr_table) / sizeof(int); --i >= 0; )
  6363.         {
  6364.             len = (int)STRLEN(hl_name_table[i]);
  6365.             if (STRNICMP(arg + off, hl_name_table[i], len) == 0)
  6366.             {
  6367.             attr |= hl_attr_table[i];
  6368.             off += len;
  6369.             break;
  6370.             }
  6371.         }
  6372.         if (i < 0)
  6373.         {
  6374.             EMSG2(_("E418: Illegal value: %s"), arg);
  6375.             error = TRUE;
  6376.             break;
  6377.         }
  6378.         if (arg[off] == ',')        /* another one follows */
  6379.             ++off;
  6380.         }
  6381.         if (error)
  6382.         break;
  6383.         if (*key == 'T')
  6384.         {
  6385.         if (!init || !(HL_TABLE()[idx].sg_set & SG_TERM))
  6386.         {
  6387.             if (!init)
  6388.             HL_TABLE()[idx].sg_set |= SG_TERM;
  6389.             HL_TABLE()[idx].sg_term = attr;
  6390.         }
  6391.         }
  6392.         else if (*key == 'C')
  6393.         {
  6394.         if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
  6395.         {
  6396.             if (!init)
  6397.             HL_TABLE()[idx].sg_set |= SG_CTERM;
  6398.             HL_TABLE()[idx].sg_cterm = attr;
  6399.             HL_TABLE()[idx].sg_cterm_bold = FALSE;
  6400.         }
  6401.         }
  6402. #ifdef FEAT_GUI
  6403.         else
  6404.         {
  6405.         if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
  6406.         {
  6407.             if (!init)
  6408.             HL_TABLE()[idx].sg_set |= SG_GUI;
  6409.             HL_TABLE()[idx].sg_gui = attr;
  6410.         }
  6411.         }
  6412. #endif
  6413.     }
  6414.     else if (STRCMP(key, "FONT") == 0)
  6415.     {
  6416.         /* in non-GUI fonts are simply ignored */
  6417. #ifdef FEAT_GUI
  6418.         if (!gui.shell_created)
  6419.         {
  6420.         /* GUI not started yet, always accept the name. */
  6421.         vim_free(HL_TABLE()[idx].sg_font_name);
  6422.         HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
  6423.         }
  6424.         else
  6425.         {
  6426.         GuiFont temp_sg_font = HL_TABLE()[idx].sg_font;
  6427. # ifdef FEAT_XFONTSET
  6428.         GuiFontset temp_sg_fontset = HL_TABLE()[idx].sg_fontset;
  6429. # endif
  6430.         /* First, save the current font/fontset.
  6431.          * Then try to allocate the font/fontset.
  6432.          * If the allocation fails, HL_TABLE()[idx].sg_font OR
  6433.          * sg_fontset will be set to NOFONT or NOFONTSET respectively.
  6434.          */
  6435.  
  6436.         HL_TABLE()[idx].sg_font = NOFONT;
  6437. # ifdef FEAT_XFONTSET
  6438.         HL_TABLE()[idx].sg_fontset = NOFONTSET;
  6439. # endif
  6440.         hl_do_font(idx, arg, is_normal_group, is_menu_group,
  6441.                                 is_tooltip_group);
  6442.  
  6443. # ifdef FEAT_XFONTSET
  6444.         if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
  6445.         {
  6446.             /* New fontset was accepted. Free the old one, if there was
  6447.              * one.
  6448.              */
  6449.             gui_mch_free_fontset(temp_sg_fontset);
  6450.             vim_free(HL_TABLE()[idx].sg_font_name);
  6451.             HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
  6452.         }
  6453.         else
  6454.             HL_TABLE()[idx].sg_fontset = temp_sg_fontset;
  6455. # endif
  6456.         if (HL_TABLE()[idx].sg_font != NOFONT)
  6457.         {
  6458.             /* New font was accepted. Free the old one, if there was
  6459.              * one.
  6460.              */
  6461.             gui_mch_free_font(temp_sg_font);
  6462.             vim_free(HL_TABLE()[idx].sg_font_name);
  6463.             HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
  6464.         }
  6465.         else
  6466.             HL_TABLE()[idx].sg_font = temp_sg_font;
  6467.         }
  6468. #endif
  6469.     }
  6470.     else if (STRCMP(key, "CTERMFG") == 0 || STRCMP(key, "CTERMBG") == 0)
  6471.     {
  6472.       if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
  6473.       {
  6474.         if (!init)
  6475.         HL_TABLE()[idx].sg_set |= SG_CTERM;
  6476.  
  6477.         /* When setting the foreground color, and previously the "bold"
  6478.          * flag was set for a light color, reset it now */
  6479.         if (key[5] == 'F' && HL_TABLE()[idx].sg_cterm_bold)
  6480.         {
  6481.         HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
  6482.         HL_TABLE()[idx].sg_cterm_bold = FALSE;
  6483.         }
  6484.  
  6485.         if (isdigit(*arg))
  6486.         color = atoi((char *)arg);
  6487.         else if (STRICMP(arg, "fg") == 0)
  6488.         {
  6489.         if (cterm_normal_fg_color)
  6490.             color = cterm_normal_fg_color - 1;
  6491.         else
  6492.         {
  6493.             EMSG(_("E419: FG color unknown"));
  6494.             error = TRUE;
  6495.             break;
  6496.         }
  6497.         }
  6498.         else if (STRICMP(arg, "bg") == 0)
  6499.         {
  6500.         if (cterm_normal_bg_color > 0)
  6501.             color = cterm_normal_bg_color - 1;
  6502.         else
  6503.         {
  6504.             EMSG(_("E420: BG color unknown"));
  6505.             error = TRUE;
  6506.             break;
  6507.         }
  6508.         }
  6509.         else
  6510.         {
  6511.         static char *(color_names[28]) = {
  6512.                 "Black", "DarkBlue", "DarkGreen", "DarkCyan",
  6513.                 "DarkRed", "DarkMagenta", "Brown", "DarkYellow",
  6514.                 "Gray", "Grey",
  6515.                 "LightGray", "LightGrey", "DarkGray", "DarkGrey",
  6516.                 "Blue", "LightBlue", "Green", "LightGreen",
  6517.                 "Cyan", "LightCyan", "Red", "LightRed", "Magenta",
  6518.                 "LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
  6519.         static int color_numbers_16[28] = {0, 1, 2, 3,
  6520.                          4, 5, 6, 6,
  6521.                          7, 7,
  6522.                          7, 7, 8, 8,
  6523.                          9, 9, 10, 10,
  6524.                          11, 11, 12, 12, 13,
  6525.                          13, 14, 14, 15, -1};
  6526.         /* for xterm with 88 colors... */
  6527.         static int color_numbers_88[28] = {0, 4, 2, 6,
  6528.                          1, 5, 32, 72,
  6529.                          84, 84,
  6530.                          7, 7, 82, 82,
  6531.                          12, 43, 10, 61,
  6532.                          14, 63, 9, 74, 13,
  6533.                          75, 11, 78, 15, -1};
  6534.         /* for xterm with 256 colors... */
  6535.         static int color_numbers_256[28] = {0, 4, 2, 6,
  6536.                          1, 5, 130, 130,
  6537.                          248, 248,
  6538.                          7, 7, 242, 242,
  6539.                          12, 81, 10, 121,
  6540.                          14, 159, 9, 224, 13,
  6541.                          225, 11, 229, 15, -1};
  6542.         /* for terminals with less than 16 colors... */
  6543.         static int color_numbers_8[28] = {0, 4, 2, 6,
  6544.                          1, 5, 3, 3,
  6545.                          7, 7,
  6546.                          7, 7, 0+8, 0+8,
  6547.                          4+8, 4+8, 2+8, 2+8,
  6548.                          6+8, 6+8, 1+8, 1+8, 5+8,
  6549.                          5+8, 3+8, 3+8, 7+8, -1};
  6550. #if defined(__QNXNTO__)
  6551.         static int *color_numbers_8_qansi = color_numbers_8;
  6552.         /* On qnx, the 8 & 16 color arrays are the same */
  6553.         if (STRNCMP(T_NAME, "qansi", 5) == 0)
  6554.             color_numbers_8_qansi = color_numbers_16;
  6555. #endif
  6556.  
  6557.         /* reduce calls to STRICMP a bit, it can be slow */
  6558.         off = TOUPPER_ASC(*arg);
  6559.         for (i = (sizeof(color_names) / sizeof(char *)); --i >= 0; )
  6560.             if (off == color_names[i][0]
  6561.                  && STRICMP(arg + 1, color_names[i] + 1) == 0)
  6562.             break;
  6563.         if (i < 0)
  6564.         {
  6565.             EMSG2(_("E421: Color name or number not recognized: %s"), key_start);
  6566.             error = TRUE;
  6567.             break;
  6568.         }
  6569.  
  6570.         /* Use the _16 table to check if its a valid color name. */
  6571.         color = color_numbers_16[i];
  6572.         if (color >= 0)
  6573.         {
  6574.             if (t_colors == 8)
  6575.             {
  6576.             /* t_Co is 8: use the 8 colors table */
  6577. #if defined(__QNXNTO__)
  6578.             color = color_numbers_8_qansi[i];
  6579. #else
  6580.             color = color_numbers_8[i];
  6581. #endif
  6582.             if (key[5] == 'F')
  6583.             {
  6584.                 /* set/reset bold attribute to get light foreground
  6585.                  * colors (on some terminals, e.g. "linux") */
  6586.                 if (color & 8)
  6587.                 {
  6588.                 HL_TABLE()[idx].sg_cterm |= HL_BOLD;
  6589.                 HL_TABLE()[idx].sg_cterm_bold = TRUE;
  6590.                 }
  6591.                 else
  6592.                 HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
  6593.             }
  6594.             color &= 7;    /* truncate to 8 colors */
  6595.             }
  6596.             else if (t_colors == 16 || t_colors == 88
  6597.                                || t_colors == 256)
  6598.             {
  6599.             /*
  6600.              * Guess: if the termcap entry ends in 'm', it is
  6601.              * probably an xterm-like terminal.  Use the changed
  6602.              * order for colors.
  6603.              */
  6604.             if (*T_CAF != NUL)
  6605.                 p = T_CAF;
  6606.             else
  6607.                 p = T_CSF;
  6608.             if (*p != NUL && *(p + STRLEN(p) - 1) == 'm')
  6609.                 switch (t_colors)
  6610.                 {
  6611.                 case 16:
  6612.                     color = color_numbers_8[i];
  6613.                     break;
  6614.                 case 88:
  6615.                     color = color_numbers_88[i];
  6616.                     break;
  6617.                 case 256:
  6618.                     color = color_numbers_256[i];
  6619.                     break;
  6620.                 }
  6621.             }
  6622.         }
  6623.         }
  6624.         /* Add one to the argument, to avoid zero */
  6625.         if (key[5] == 'F')
  6626.         {
  6627.         HL_TABLE()[idx].sg_cterm_fg = color + 1;
  6628.         if (is_normal_group)
  6629.         {
  6630.             cterm_normal_fg_color = color + 1;
  6631.             cterm_normal_fg_bold = (HL_TABLE()[idx].sg_cterm & HL_BOLD);
  6632. #ifdef FEAT_GUI
  6633.             /* Don't do this if the GUI is used. */
  6634.             if (!gui.in_use && !gui.starting)
  6635. #endif
  6636.             {
  6637.             must_redraw = CLEAR;
  6638.             if (termcap_active)
  6639.                 term_fg_color(color);
  6640.             }
  6641.         }
  6642.         }
  6643.         else
  6644.         {
  6645.         HL_TABLE()[idx].sg_cterm_bg = color + 1;
  6646.         if (is_normal_group)
  6647.         {
  6648.             cterm_normal_bg_color = color + 1;
  6649. #ifdef FEAT_GUI
  6650.             /* Don't mess with 'background' if the GUI is used. */
  6651.             if (!gui.in_use && !gui.starting)
  6652. #endif
  6653.             {
  6654.             must_redraw = CLEAR;
  6655.             if (termcap_active)
  6656.                 term_bg_color(color);
  6657.             if (t_colors < 16)
  6658.                 i = (color == 0 || color == 4);
  6659.             else
  6660.                 i = (color < 7 || color == 8);
  6661.             /* Set the 'background' option if the value is wrong. */
  6662.             if (i != (*p_bg == 'd'))
  6663.                 set_option_value((char_u *)"bg", 0L,
  6664.                  i ? (char_u *)"dark" : (char_u *)"light", 0);
  6665.             }
  6666.         }
  6667.         }
  6668.       }
  6669.     }
  6670.     else if (STRCMP(key, "GUIFG") == 0)
  6671.     {
  6672. #ifdef FEAT_GUI        /* in non-GUI guifg colors are simply ignored */
  6673.       if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
  6674.       {
  6675.         if (!init)
  6676.         HL_TABLE()[idx].sg_set |= SG_GUI;
  6677.  
  6678.         i = color_name2handle(arg);
  6679.         if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
  6680.         {
  6681.         HL_TABLE()[idx].sg_gui_fg = i;
  6682.         vim_free(HL_TABLE()[idx].sg_gui_fg_name);
  6683.         if (STRCMP(arg, "NONE"))
  6684.             HL_TABLE()[idx].sg_gui_fg_name = vim_strsave(arg);
  6685.         else
  6686.             HL_TABLE()[idx].sg_gui_fg_name = NULL;
  6687. # ifdef FEAT_GUI_X11
  6688.         if (is_menu_group)
  6689.             gui.menu_fg_pixel = i;
  6690.         if (is_scrollbar_group)
  6691.             gui.scroll_fg_pixel = i;
  6692. #  ifdef FEAT_BEVAL
  6693.         if (is_tooltip_group)
  6694.             gui.tooltip_fg_pixel = i;
  6695. #  endif
  6696.         do_colors = TRUE;
  6697. # endif
  6698.         }
  6699.       }
  6700. #endif
  6701.     }
  6702.     else if (STRCMP(key, "GUIBG") == 0)
  6703.     {
  6704. #ifdef FEAT_GUI        /* in non-GUI guibg colors are simply ignored */
  6705.       if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
  6706.       {
  6707.         if (!init)
  6708.         HL_TABLE()[idx].sg_set |= SG_GUI;
  6709.  
  6710.         i = color_name2handle(arg);
  6711.         if (i != INVALCOLOR || STRCMP(arg, "NONE") == 0 || !gui.in_use)
  6712.         {
  6713.         HL_TABLE()[idx].sg_gui_bg = i;
  6714.         vim_free(HL_TABLE()[idx].sg_gui_bg_name);
  6715.         if (STRCMP(arg, "NONE"))
  6716.             HL_TABLE()[idx].sg_gui_bg_name = vim_strsave(arg);
  6717.         else
  6718.             HL_TABLE()[idx].sg_gui_bg_name = NULL;
  6719. # ifdef FEAT_GUI_X11
  6720.         if (is_menu_group)
  6721.             gui.menu_bg_pixel = i;
  6722.         if (is_scrollbar_group)
  6723.             gui.scroll_bg_pixel = i;
  6724. #  ifdef FEAT_BEVAL
  6725.         if (is_tooltip_group)
  6726.             gui.tooltip_bg_pixel = i;
  6727. #  endif
  6728.         do_colors = TRUE;
  6729. # endif
  6730.         }
  6731.       }
  6732. #endif
  6733.     }
  6734.     else if (STRCMP(key, "START") == 0 || STRCMP(key, "STOP") == 0)
  6735.     {
  6736.         char_u    buf[100];
  6737.         char_u    *tname;
  6738.  
  6739.         if (!init)
  6740.         HL_TABLE()[idx].sg_set |= SG_TERM;
  6741.  
  6742.         /*
  6743.          * The "start" and "stop"  arguments can be a literal escape
  6744.          * sequence, or a comma seperated list of terminal codes.
  6745.          */
  6746.         if (STRNCMP(arg, "t_", 2) == 0)
  6747.         {
  6748.         off = 0;
  6749.         buf[0] = 0;
  6750.         while (arg[off] != NUL)
  6751.         {
  6752.             /* Isolate one termcap name */
  6753.             for (len = 0; arg[off + len] &&
  6754.                          arg[off + len] != ','; ++len)
  6755.             ;
  6756.             tname = vim_strnsave(arg + off, len);
  6757.             if (tname == NULL)        /* out of memory */
  6758.             {
  6759.             error = TRUE;
  6760.             break;
  6761.             }
  6762.             /* lookup the escape sequence for the item */
  6763.             p = get_term_code(tname);
  6764.             vim_free(tname);
  6765.             if (p == NULL)        /* ignore non-existing things */
  6766.             p = (char_u *)"";
  6767.  
  6768.             /* Append it to the already found stuff */
  6769.             if ((int)(STRLEN(buf) + STRLEN(p)) >= 99)
  6770.             {
  6771.             EMSG2(_("E422: terminal code too long: %s"), arg);
  6772.             error = TRUE;
  6773.             break;
  6774.             }
  6775.             STRCAT(buf, p);
  6776.  
  6777.             /* Advance to the next item */
  6778.             off += len;
  6779.             if (arg[off] == ',')        /* another one follows */
  6780.             ++off;
  6781.         }
  6782.         }
  6783.         else
  6784.         {
  6785.         /*
  6786.          * Copy characters from arg[] to buf[], translating <> codes.
  6787.          */
  6788.         for (p = arg, off = 0; off < 100 && *p; )
  6789.         {
  6790.             len = trans_special(&p, buf + off, FALSE);
  6791.             if (len)            /* recognized special char */
  6792.             off += len;
  6793.             else            /* copy as normal char */
  6794.             buf[off++] = *p++;
  6795.         }
  6796.         buf[off] = NUL;
  6797.         }
  6798.         if (error)
  6799.         break;
  6800.  
  6801.         if (STRCMP(buf, "NONE") == 0)    /* resetting the value */
  6802.         p = NULL;
  6803.         else
  6804.         p = vim_strsave(buf);
  6805.         if (key[2] == 'A')
  6806.         {
  6807.         vim_free(HL_TABLE()[idx].sg_start);
  6808.         HL_TABLE()[idx].sg_start = p;
  6809.         }
  6810.         else
  6811.         {
  6812.         vim_free(HL_TABLE()[idx].sg_stop);
  6813.         HL_TABLE()[idx].sg_stop = p;
  6814.         }
  6815.     }
  6816.     else
  6817.     {
  6818.         EMSG2(_("E423: Illegal argument: %s"), key_start);
  6819.         error = TRUE;
  6820.         break;
  6821.     }
  6822.  
  6823.     /*
  6824.      * When highlighting has been given for a group, don't link it.
  6825.      */
  6826.     if (!init || !(HL_TABLE()[idx].sg_set & SG_LINK))
  6827.         HL_TABLE()[idx].sg_link = 0;
  6828.  
  6829.     /*
  6830.      * Continue with next argument.
  6831.      */
  6832.     linep = skipwhite(linep);
  6833.       }
  6834.  
  6835.     /*
  6836.      * If there is an error, and it's a new entry, remove it from the table.
  6837.      */
  6838.     if (error && idx == highlight_ga.ga_len)
  6839.     syn_unadd_group();
  6840.     else
  6841.     {
  6842.     if (is_normal_group)
  6843.     {
  6844.         HL_TABLE()[idx].sg_term_attr = 0;
  6845.         HL_TABLE()[idx].sg_cterm_attr = 0;
  6846. #ifdef FEAT_GUI
  6847.         HL_TABLE()[idx].sg_gui_attr = 0;
  6848.         /*
  6849.          * Need to update all groups, because they might be using "bg"
  6850.          * and/or "fg", which have been changed now.
  6851.          */
  6852.         if (gui.in_use)
  6853.         highlight_gui_started();
  6854. #endif
  6855.     }
  6856. #ifdef FEAT_GUI_X11
  6857. # ifdef FEAT_MENU
  6858.     else if (is_menu_group)
  6859.     {
  6860.         if (gui.in_use && do_colors)
  6861.         gui_mch_new_menu_colors();
  6862.     }
  6863. # endif
  6864.     else if (is_scrollbar_group)
  6865.     {
  6866.         if (gui.in_use && do_colors)
  6867.         gui_new_scrollbar_colors();
  6868.     }
  6869. # ifdef FEAT_BEVAL
  6870.     else if (is_tooltip_group)
  6871.     {
  6872.         if (gui.in_use && do_colors)
  6873.         gui_mch_new_tooltip_colors();
  6874.     }
  6875. # endif
  6876. #endif
  6877.     else
  6878.         set_hl_attr(idx);
  6879.     redraw_all_later(NOT_VALID);
  6880.     }
  6881.     vim_free(key);
  6882.     vim_free(arg);
  6883.  
  6884.     /* Only call highlight_changed() once, after sourcing a syntax file */
  6885.     need_highlight_changed = TRUE;
  6886. }
  6887.  
  6888. /*
  6889.  * Reset the cterm colors to what they were before Vim was started, if
  6890.  * possible.  Otherwise reset them to zero.
  6891.  */
  6892.     void
  6893. restore_cterm_colors()
  6894. {
  6895. #if defined(MSDOS) || (defined(WIN3264) && !defined(FEAT_GUI_W32))
  6896.     /* Since t_me has been set, this probably means that the user
  6897.      * wants to use this as default colors.  Need to reset default
  6898.      * background/foreground colors. */
  6899.     mch_set_normal_colors();
  6900. #else
  6901.     cterm_normal_fg_color = 0;
  6902.     cterm_normal_fg_bold = 0;
  6903.     cterm_normal_bg_color = 0;
  6904. #endif
  6905. }
  6906.  
  6907. /*
  6908.  * Return TRUE if highlight group "idx" has any settings.
  6909.  * When "check_link" is TRUE also check for an existing link.
  6910.  */
  6911.     static int
  6912. hl_has_settings(idx, check_link)
  6913.     int        idx;
  6914.     int        check_link;
  6915. {
  6916.     return (   HL_TABLE()[idx].sg_term_attr != 0
  6917.         || HL_TABLE()[idx].sg_cterm_attr != 0
  6918. #ifdef FEAT_GUI
  6919.         || HL_TABLE()[idx].sg_gui_attr != 0
  6920. #endif
  6921.         || (check_link && (HL_TABLE()[idx].sg_set & SG_LINK)));
  6922. }
  6923.  
  6924. /*
  6925.  * Clear highlighting for one group.
  6926.  */
  6927.     static void
  6928. highlight_clear(idx)
  6929.     int idx;
  6930. {
  6931.     HL_TABLE()[idx].sg_term = 0;
  6932.     vim_free(HL_TABLE()[idx].sg_start);
  6933.     HL_TABLE()[idx].sg_start = NULL;
  6934.     vim_free(HL_TABLE()[idx].sg_stop);
  6935.     HL_TABLE()[idx].sg_stop = NULL;
  6936.     HL_TABLE()[idx].sg_term_attr = 0;
  6937.     HL_TABLE()[idx].sg_cterm = 0;
  6938.     HL_TABLE()[idx].sg_cterm_bold = FALSE;
  6939.     HL_TABLE()[idx].sg_cterm_fg = 0;
  6940.     HL_TABLE()[idx].sg_cterm_bg = 0;
  6941.     HL_TABLE()[idx].sg_cterm_attr = 0;
  6942. #ifdef FEAT_GUI        /* in non-GUI fonts are simply ignored */
  6943.     HL_TABLE()[idx].sg_gui = 0;
  6944.     HL_TABLE()[idx].sg_gui_fg = INVALCOLOR;
  6945.     vim_free(HL_TABLE()[idx].sg_gui_fg_name);
  6946.     HL_TABLE()[idx].sg_gui_fg_name = NULL;
  6947.     HL_TABLE()[idx].sg_gui_bg = INVALCOLOR;
  6948.     vim_free(HL_TABLE()[idx].sg_gui_bg_name);
  6949.     HL_TABLE()[idx].sg_gui_bg_name = NULL;
  6950.     gui_mch_free_font(HL_TABLE()[idx].sg_font);
  6951.     HL_TABLE()[idx].sg_font = NOFONT;
  6952. # ifdef FEAT_XFONTSET
  6953.     gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
  6954.     HL_TABLE()[idx].sg_fontset = NOFONTSET;
  6955. # endif
  6956.     vim_free(HL_TABLE()[idx].sg_font_name);
  6957.     HL_TABLE()[idx].sg_font_name = NULL;
  6958.     HL_TABLE()[idx].sg_gui_attr = 0;
  6959. #endif
  6960. }
  6961.  
  6962. #if defined(FEAT_GUI) || defined(PROTO)
  6963. /*
  6964.  * Set the normal foreground and background colors according to the "Normal"
  6965.  * highlighighting group.  For X11 also set "Menu", "Scrollbar", and
  6966.  * "Tooltip" colors.
  6967.  */
  6968.     void
  6969. set_normal_colors()
  6970. {
  6971.     if (set_group_colors((char_u *)"Normal",
  6972.                    &gui.norm_pixel, &gui.back_pixel, FALSE, TRUE,
  6973.                    FALSE))
  6974.     {
  6975.     gui_mch_new_colors();
  6976.     must_redraw = CLEAR;
  6977.     }
  6978. #ifdef FEAT_GUI_X11
  6979.     if (set_group_colors((char_u *)"Menu",
  6980.              &gui.menu_fg_pixel, &gui.menu_bg_pixel, TRUE, FALSE,
  6981.              FALSE))
  6982.     {
  6983. # ifdef FEAT_MENU
  6984.     gui_mch_new_menu_colors();
  6985. # endif
  6986.     must_redraw = CLEAR;
  6987.     }
  6988. # ifdef FEAT_BEVAL
  6989.     if (set_group_colors((char_u *)"Tooltip",
  6990.              &gui.tooltip_fg_pixel, &gui.tooltip_bg_pixel,
  6991.              FALSE, FALSE, TRUE))
  6992.     {
  6993. # ifdef FEAT_TOOLBAR
  6994.     gui_mch_new_tooltip_colors();
  6995. # endif
  6996.     must_redraw = CLEAR;
  6997.     }
  6998. #endif
  6999.     if (set_group_colors((char_u *)"Scrollbar",
  7000.             &gui.scroll_fg_pixel, &gui.scroll_bg_pixel, FALSE, FALSE,
  7001.             FALSE))
  7002.     {
  7003.     gui_new_scrollbar_colors();
  7004.     must_redraw = CLEAR;
  7005.     }
  7006. #endif
  7007. }
  7008.  
  7009. /*
  7010.  * Set the colors for "Normal", "Menu", "Tooltip" or "Scrollbar".
  7011.  */
  7012.     static int
  7013. set_group_colors(name, fgp, bgp, do_menu, use_norm, do_tooltip)
  7014.     char_u    *name;
  7015.     guicolor_T    *fgp;
  7016.     guicolor_T    *bgp;
  7017.     int        do_menu;
  7018.     int        use_norm;
  7019.     int        do_tooltip;
  7020. {
  7021.     int        idx;
  7022.  
  7023.     idx = syn_name2id(name) - 1;
  7024.     if (idx >= 0)
  7025.     {
  7026.     gui_do_one_color(idx, do_menu, do_tooltip);
  7027.  
  7028.     if (HL_TABLE()[idx].sg_gui_fg != INVALCOLOR)
  7029.         *fgp = HL_TABLE()[idx].sg_gui_fg;
  7030.     else if (use_norm)
  7031.         *fgp = gui.def_norm_pixel;
  7032.     if (HL_TABLE()[idx].sg_gui_bg != INVALCOLOR)
  7033.         *bgp = HL_TABLE()[idx].sg_gui_bg;
  7034.     else if (use_norm)
  7035.         *bgp = gui.def_back_pixel;
  7036.     return TRUE;
  7037.     }
  7038.     return FALSE;
  7039. }
  7040.  
  7041. /*
  7042.  * Get the font of the "Normal" group.
  7043.  * Returns "" when it's not found or not set.
  7044.  */
  7045.     char_u *
  7046. hl_get_font_name()
  7047. {
  7048.     int        id;
  7049.     char_u    *s;
  7050.  
  7051.     id = syn_name2id((char_u *)"Normal");
  7052.     if (id > 0)
  7053.     {
  7054.     s = HL_TABLE()[id - 1].sg_font_name;
  7055.     if (s != NULL)
  7056.         return s;
  7057.     }
  7058.     return (char_u *)"";
  7059. }
  7060.  
  7061. /*
  7062.  * Set font for "Normal" group.  Called by gui_mch_init_font() when a font has
  7063.  * actually chosen to be used.
  7064.  */
  7065.     void
  7066. hl_set_font_name(font_name)
  7067.     char_u    *font_name;
  7068. {
  7069.     int        id;
  7070.  
  7071.     id = syn_name2id((char_u *)"Normal");
  7072.     if (id > 0)
  7073.     {
  7074.     vim_free(HL_TABLE()[id - 1].sg_font_name);
  7075.     HL_TABLE()[id - 1].sg_font_name = vim_strsave(font_name);
  7076.     }
  7077. }
  7078.  
  7079. /*
  7080.  * Set background color for "Normal" group.  Called by gui_set_bg_color()
  7081.  * when the color is known.
  7082.  */
  7083.     void
  7084. hl_set_bg_color_name(name)
  7085.     char_u  *name;        /* must have been allocated */
  7086. {
  7087.     int        id;
  7088.  
  7089.     if (name != NULL)
  7090.     {
  7091.     id = syn_name2id((char_u *)"Normal");
  7092.     if (id > 0)
  7093.     {
  7094.         vim_free(HL_TABLE()[id - 1].sg_gui_bg_name);
  7095.         HL_TABLE()[id - 1].sg_gui_bg_name = name;
  7096.     }
  7097.     }
  7098. }
  7099.  
  7100. /*
  7101.  * Set foreground color for "Normal" group.  Called by gui_set_fg_color()
  7102.  * when the color is known.
  7103.  */
  7104.     void
  7105. hl_set_fg_color_name(name)
  7106.     char_u  *name;        /* must have been allocated */
  7107. {
  7108.     int        id;
  7109.  
  7110.     if (name != NULL)
  7111.     {
  7112.     id = syn_name2id((char_u *)"Normal");
  7113.     if (id > 0)
  7114.     {
  7115.         vim_free(HL_TABLE()[id - 1].sg_gui_fg_name);
  7116.         HL_TABLE()[id - 1].sg_gui_fg_name = name;
  7117.     }
  7118.     }
  7119. }
  7120.  
  7121. /*
  7122.  * Return the handle for a color name.
  7123.  * Returns INVALCOLOR when failed.
  7124.  */
  7125.     static guicolor_T
  7126. color_name2handle(name)
  7127.     char_u  *name;
  7128. {
  7129.     if (STRCMP(name, "NONE") == 0)
  7130.     return INVALCOLOR;
  7131.  
  7132.     if (STRICMP(name, "fg") == 0 || STRICMP(name, "foreground") == 0)
  7133.     return gui.norm_pixel;
  7134.     if (STRICMP(name, "bg") == 0 || STRICMP(name, "background") == 0)
  7135.     return gui.back_pixel;
  7136.  
  7137.     return gui_get_color(name);
  7138. }
  7139.  
  7140. /*
  7141.  * Return the handle for a font name.
  7142.  * Returns NOFONT when failed.
  7143.  */
  7144.     static GuiFont
  7145. font_name2handle(name)
  7146.     char_u  *name;
  7147. {
  7148.     if (STRCMP(name, "NONE") == 0)
  7149.     return NOFONT;
  7150.  
  7151.     return gui_mch_get_font(name, TRUE);
  7152. }
  7153.  
  7154. # ifdef FEAT_XFONTSET
  7155. /*
  7156.  * Return the handle for a fontset name.
  7157.  * Returns NOFONTSET when failed.
  7158.  */
  7159.     static GuiFontset
  7160. fontset_name2handle(name, fixed_width)
  7161.     char_u    *name;
  7162.     int        fixed_width;
  7163. {
  7164.     if (STRCMP(name, "NONE") == 0)
  7165.     return NOFONTSET;
  7166.  
  7167.     return gui_mch_get_fontset(name, TRUE, fixed_width);
  7168. }
  7169. # endif
  7170.  
  7171. /*
  7172.  * Get the font or fontset for one highlight group.
  7173.  */
  7174. /*ARGSUSED*/
  7175.     static void
  7176. hl_do_font(idx, arg, do_normal, do_menu, do_tooltip)
  7177.     int        idx;
  7178.     char_u    *arg;
  7179.     int        do_normal;    /* set normal font */
  7180.     int        do_menu;    /* set menu font */
  7181.     int        do_tooltip;    /* set tooltip font */
  7182. {
  7183. # ifdef FEAT_XFONTSET
  7184.     /* If 'guifontset' is not empty, first try using the name as a
  7185.      * fontset.  If that doesn't work, use it as a font name. */
  7186.     if (*p_guifontset != NUL
  7187. #  ifdef FONTSET_ALWAYS
  7188.     || do_menu
  7189. #  endif
  7190. #  ifdef FEAT_BEVAL_TIP
  7191.     /* In Athena & Motif, the Tooltip highlight group is always a fontset */
  7192.     || do_tooltip
  7193. #  endif
  7194.         )
  7195.     HL_TABLE()[idx].sg_fontset = fontset_name2handle(arg, 0
  7196. #  ifdef FONTSET_ALWAYS
  7197.         || do_menu
  7198. #  endif
  7199. #  ifdef FEAT_BEVAL_TIP
  7200.         || do_tooltip
  7201. #  endif
  7202.         );
  7203.     if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
  7204.     {
  7205.     /* If it worked and it's the Normal group, use it as the
  7206.      * normal fontset.  Same for the Menu group. */
  7207.     if (do_normal)
  7208.         gui_init_font(arg, TRUE);
  7209. #   if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
  7210.     if (do_menu)
  7211.     {
  7212. #    ifdef FONTSET_ALWAYS
  7213.         gui.menu_fontset = HL_TABLE()[idx].sg_fontset;
  7214. #    else
  7215.         /* YIKES!  This is a bug waiting to crash the program */
  7216.         gui.menu_font = HL_TABLE()[idx].sg_fontset;
  7217. #    endif
  7218.         gui_mch_new_menu_font();
  7219.     }
  7220. #    ifdef FEAT_BEVAL
  7221.     if (do_tooltip)
  7222.     {
  7223.         /* The Athena widget set cannot currently handle switching between
  7224.          * displaying a single font and a fontset.
  7225.          * If the XtNinternational resource is set to True at widget
  7226.          * creation, then a fontset is always used, othwise an
  7227.          * XFontStruct is used.
  7228.          */
  7229.         gui.tooltip_fontset = (XFontSet)HL_TABLE()[idx].sg_fontset;
  7230.         gui_mch_new_tooltip_font();
  7231.     }
  7232. #    endif
  7233. #   endif
  7234.     }
  7235.     else
  7236. # endif
  7237.     {
  7238.     HL_TABLE()[idx].sg_font = font_name2handle(arg);
  7239.     /* If it worked and it's the Normal group, use it as the
  7240.      * normal font.  Same for the Menu group. */
  7241.     if (HL_TABLE()[idx].sg_font != NOFONT)
  7242.     {
  7243.         if (do_normal)
  7244.         gui_init_font(arg, FALSE);
  7245. #ifndef FONTSET_ALWAYS
  7246. # if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
  7247.         if (do_menu)
  7248.         {
  7249.         gui.menu_font = HL_TABLE()[idx].sg_font;
  7250.         gui_mch_new_menu_font();
  7251.         }
  7252. # endif
  7253. #endif
  7254.     }
  7255.     }
  7256. }
  7257.  
  7258. #endif /* FEAT_GUI */
  7259.  
  7260. /*
  7261.  * Table with the specifications for an attribute number.
  7262.  * Note that this table is used by ALL buffers.  This is required because the
  7263.  * GUI can redraw at any time for any buffer.
  7264.  */
  7265. garray_T    term_attr_table = {0, 0, 0, 0, NULL};
  7266.  
  7267. #define TERM_ATTR_ENTRY(idx) ((attrentry_T *)term_attr_table.ga_data)[idx]
  7268.  
  7269. garray_T    cterm_attr_table = {0, 0, 0, 0, NULL};
  7270.  
  7271. #define CTERM_ATTR_ENTRY(idx) ((attrentry_T *)cterm_attr_table.ga_data)[idx]
  7272.  
  7273. #ifdef FEAT_GUI
  7274. garray_T    gui_attr_table = {0, 0, 0, 0, NULL};
  7275.  
  7276. #define GUI_ATTR_ENTRY(idx) ((attrentry_T *)gui_attr_table.ga_data)[idx]
  7277. #endif
  7278.  
  7279. /*
  7280.  * Return the attr number for a set of colors and font.
  7281.  * Add a new entry to the term_attr_table, cterm_attr_table or gui_attr_table
  7282.  * if the combination is new.
  7283.  * Return 0 for error (no more room).
  7284.  */
  7285.     static int
  7286. get_attr_entry(table, aep)
  7287.     garray_T    *table;
  7288.     attrentry_T    *aep;
  7289. {
  7290.     int        i;
  7291.     attrentry_T    *gap;
  7292.     static int    recursive = FALSE;
  7293.  
  7294.     /*
  7295.      * Init the table, in case it wasn't done yet.
  7296.      */
  7297.     table->ga_itemsize = sizeof(attrentry_T);
  7298.     table->ga_growsize = 7;
  7299.  
  7300.     /*
  7301.      * Try to find an entry with the same specifications.
  7302.      */
  7303.     for (i = 0; i < table->ga_len; ++i)
  7304.     {
  7305.     gap = &(((attrentry_T *)table->ga_data)[i]);
  7306.     if (       aep->ae_attr == gap->ae_attr
  7307.         && (
  7308. #ifdef FEAT_GUI
  7309.                (table == &gui_attr_table
  7310.             && (aep->ae_u.gui.fg_color == gap->ae_u.gui.fg_color
  7311.                 && aep->ae_u.gui.bg_color == gap->ae_u.gui.bg_color
  7312.                 && aep->ae_u.gui.font == gap->ae_u.gui.font
  7313. #  ifdef FEAT_XFONTSET
  7314.                 && aep->ae_u.gui.fontset == gap->ae_u.gui.fontset
  7315. #  endif
  7316.                 ))
  7317.             ||
  7318. #endif
  7319.                (table == &term_attr_table
  7320.             && (aep->ae_u.term.start == NULL) ==
  7321.                         (gap->ae_u.term.start == NULL)
  7322.             && (aep->ae_u.term.start == NULL
  7323.                 || STRCMP(aep->ae_u.term.start,
  7324.                            gap->ae_u.term.start) == 0)
  7325.             && (aep->ae_u.term.stop == NULL) ==
  7326.                          (gap->ae_u.term.stop == NULL)
  7327.             && (aep->ae_u.term.stop == NULL
  7328.                 || STRCMP(aep->ae_u.term.stop,
  7329.                            gap->ae_u.term.stop) == 0))
  7330.             || (table == &cterm_attr_table
  7331.             && aep->ae_u.cterm.fg_color == gap->ae_u.cterm.fg_color
  7332.             && aep->ae_u.cterm.bg_color == gap->ae_u.cterm.bg_color)
  7333.              ))
  7334.  
  7335.     return i + ATTR_OFF;
  7336.     }
  7337.  
  7338.     if (table->ga_len + ATTR_OFF == 256)
  7339.     {
  7340.     /*
  7341.      * Running out of attribute entries!  remove all attributes, and
  7342.      * compute new ones for all groups.
  7343.      * When called recursively, we are really out of numbers.
  7344.      */
  7345.     if (recursive)
  7346.     {
  7347.         EMSG(_("E424: Too many different highlighting attributes in use"));
  7348.         return 0;
  7349.     }
  7350.     recursive = TRUE;
  7351.  
  7352. #ifdef FEAT_GUI
  7353.     ga_clear(&gui_attr_table);
  7354. #endif
  7355.     ga_clear(&term_attr_table);
  7356.     ga_clear(&cterm_attr_table);
  7357.     must_redraw = CLEAR;
  7358.  
  7359.     for (i = 0; i < highlight_ga.ga_len; ++i)
  7360.         set_hl_attr(i);
  7361.  
  7362.     recursive = FALSE;
  7363.     }
  7364.  
  7365.     /*
  7366.      * This is a new combination of colors and font, add an entry.
  7367.      */
  7368.     if (ga_grow(table, 1) == FAIL)
  7369.     return 0;
  7370.  
  7371.     gap = &(((attrentry_T *)table->ga_data)[table->ga_len]);
  7372.     vim_memset(gap, 0, sizeof(attrentry_T));
  7373.     gap->ae_attr = aep->ae_attr;
  7374. #ifdef FEAT_GUI
  7375.     if (table == &gui_attr_table)
  7376.     {
  7377.     gap->ae_u.gui.fg_color = aep->ae_u.gui.fg_color;
  7378.     gap->ae_u.gui.bg_color = aep->ae_u.gui.bg_color;
  7379.     gap->ae_u.gui.font = aep->ae_u.gui.font;
  7380. # ifdef FEAT_XFONTSET
  7381.     gap->ae_u.gui.fontset = aep->ae_u.gui.fontset;
  7382. # endif
  7383.     }
  7384. #endif
  7385.     if (table == &term_attr_table)
  7386.     {
  7387.     if (aep->ae_u.term.start == NULL)
  7388.         gap->ae_u.term.start = NULL;
  7389.     else
  7390.         gap->ae_u.term.start = vim_strsave(aep->ae_u.term.start);
  7391.     if (aep->ae_u.term.stop == NULL)
  7392.         gap->ae_u.term.stop = NULL;
  7393.     else
  7394.         gap->ae_u.term.stop = vim_strsave(aep->ae_u.term.stop);
  7395.     }
  7396.     else if (table == &cterm_attr_table)
  7397.     {
  7398.     gap->ae_u.cterm.fg_color = aep->ae_u.cterm.fg_color;
  7399.     gap->ae_u.cterm.bg_color = aep->ae_u.cterm.bg_color;
  7400.     }
  7401.     ++table->ga_len;
  7402.     --table->ga_room;
  7403.     return (table->ga_len - 1 + ATTR_OFF);
  7404. }
  7405.  
  7406. #ifdef FEAT_GUI
  7407.  
  7408.     attrentry_T *
  7409. syn_gui_attr2entry(attr)
  7410.     int            attr;
  7411. {
  7412.     attr -= ATTR_OFF;
  7413.     if (attr >= gui_attr_table.ga_len)        /* did ":syntax clear" */
  7414.     return NULL;
  7415.     return &(GUI_ATTR_ENTRY(attr));
  7416. }
  7417.  
  7418. #endif /* FEAT_GUI */
  7419.  
  7420.     attrentry_T *
  7421. syn_term_attr2entry(attr)
  7422.     int            attr;
  7423. {
  7424.     attr -= ATTR_OFF;
  7425.     if (attr >= term_attr_table.ga_len)        /* did ":syntax clear" */
  7426.     return NULL;
  7427.     return &(TERM_ATTR_ENTRY(attr));
  7428. }
  7429.  
  7430.     attrentry_T *
  7431. syn_cterm_attr2entry(attr)
  7432.     int            attr;
  7433. {
  7434.     attr -= ATTR_OFF;
  7435.     if (attr >= cterm_attr_table.ga_len)    /* did ":syntax clear" */
  7436.     return NULL;
  7437.     return &(CTERM_ATTR_ENTRY(attr));
  7438. }
  7439.  
  7440. #define LIST_ATTR   1
  7441. #define LIST_STRING 2
  7442. #define LIST_INT    3
  7443.  
  7444.     static void
  7445. highlight_list_one(id)
  7446.     int        id;
  7447. {
  7448.     struct hl_group    *sgp;
  7449.     int            didh = FALSE;
  7450.  
  7451.     sgp = &HL_TABLE()[id - 1];        /* index is ID minus one */
  7452.  
  7453.     didh = highlight_list_arg(id, didh, LIST_ATTR,
  7454.                     sgp->sg_term, NULL, "term");
  7455.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7456.                     0, sgp->sg_start, "start");
  7457.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7458.                     0, sgp->sg_stop, "stop");
  7459.  
  7460.     didh = highlight_list_arg(id, didh, LIST_ATTR,
  7461.                     sgp->sg_cterm, NULL, "cterm");
  7462.     didh = highlight_list_arg(id, didh, LIST_INT,
  7463.                     sgp->sg_cterm_fg, NULL, "ctermfg");
  7464.     didh = highlight_list_arg(id, didh, LIST_INT,
  7465.                     sgp->sg_cterm_bg, NULL, "ctermbg");
  7466.  
  7467. #ifdef FEAT_GUI
  7468.     didh = highlight_list_arg(id, didh, LIST_ATTR,
  7469.                     sgp->sg_gui, NULL, "gui");
  7470.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7471.                     0, sgp->sg_gui_fg_name, "guifg");
  7472.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7473.                     0, sgp->sg_gui_bg_name, "guibg");
  7474.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7475.                     0, sgp->sg_font_name, "font");
  7476. #endif
  7477.  
  7478.     if (sgp->sg_link)
  7479.     {
  7480.     (void)syn_list_header(didh, 9999, id);
  7481.     msg_puts_attr((char_u *)"links to", hl_attr(HLF_D));
  7482.     msg_putchar(' ');
  7483.     msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
  7484.     }
  7485. }
  7486.  
  7487.     static int
  7488. highlight_list_arg(id, didh, type, iarg, sarg, name)
  7489.     int        id;
  7490.     int        didh;
  7491.     int        type;
  7492.     int        iarg;
  7493.     char_u    *sarg;
  7494.     char    *name;
  7495. {
  7496.     char_u    buf[100];
  7497.     char_u    *ts;
  7498.     int        i;
  7499.  
  7500.     if (type == LIST_STRING ? (sarg != NULL) : (iarg != 0))
  7501.     {
  7502.     ts = buf;
  7503.     if (type == LIST_INT)
  7504.         sprintf((char *)buf, "%d", iarg - 1);
  7505.     else if (type == LIST_STRING)
  7506.         ts = sarg;
  7507.     else /* type == LIST_ATTR */
  7508.     {
  7509.         buf[0] = NUL;
  7510.         for (i = 0; hl_attr_table[i] != 0; ++i)
  7511.         {
  7512.         if (iarg & hl_attr_table[i])
  7513.         {
  7514.             if (buf[0] != NUL)
  7515.             STRCAT(buf, ",");
  7516.             STRCAT(buf, hl_name_table[i]);
  7517.             iarg &= ~hl_attr_table[i];        /* don't want "inverse" */
  7518.         }
  7519.         }
  7520.     }
  7521.  
  7522.     (void)syn_list_header(didh,
  7523.                    (int)(vim_strsize(ts) + STRLEN(name) + 1), id);
  7524.     didh = TRUE;
  7525.  
  7526.     MSG_PUTS_ATTR(name, hl_attr(HLF_D));
  7527.     MSG_PUTS_ATTR("=", hl_attr(HLF_D));
  7528.     msg_outtrans(ts);
  7529.     }
  7530.     return didh;
  7531. }
  7532.  
  7533. #if (((defined(FEAT_EVAL) || defined(FEAT_PRINTER))) && defined(FEAT_SYN_HL)) || defined(PROTO)
  7534. /*
  7535.  * Return "1" if highlight group "id" has attribute "flag".
  7536.  * Return NULL otherwise.
  7537.  */
  7538.     char_u *
  7539. highlight_has_attr(id, flag, modec)
  7540.     int        id;
  7541.     int        flag;
  7542.     int        modec;    /* 'g' for GUI, 'c' for cterm, 't' for term */
  7543. {
  7544.     int        attr;
  7545.  
  7546.     if (id <= 0 || id > highlight_ga.ga_len)
  7547.     return NULL;
  7548.  
  7549. #ifdef FEAT_GUI
  7550.     if (modec == 'g')
  7551.     attr = HL_TABLE()[id - 1].sg_gui;
  7552.     else
  7553. #endif
  7554.      if (modec == 'c')
  7555.     attr = HL_TABLE()[id - 1].sg_cterm;
  7556.     else
  7557.     attr = HL_TABLE()[id - 1].sg_term;
  7558.  
  7559.     if (attr & flag)
  7560.     return (char_u *)"1";
  7561.     return NULL;
  7562. }
  7563. #endif
  7564.  
  7565. #if (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) || defined(PROTO)
  7566. /*
  7567.  * Return color name of highlight group "id".
  7568.  */
  7569.     char_u *
  7570. highlight_color(id, what, modec)
  7571.     int        id;
  7572.     char_u    *what;    /* "fg", "bg", "fg#" or "bg#" */
  7573.     int        modec;    /* 'g' for GUI, 'c' for cterm, 't' for term */
  7574. {
  7575.     static char_u    name[20];
  7576.     int            n;
  7577.     int            fg;
  7578.  
  7579.     if (id <= 0 || id > highlight_ga.ga_len)
  7580.     return NULL;
  7581.  
  7582.     if (TOLOWER_ASC(what[0]) == 'f')
  7583.     fg = TRUE;
  7584.     else
  7585.     fg = FALSE;
  7586. #ifdef FEAT_GUI
  7587.     if (modec == 'g')
  7588.     {
  7589.     /* return #RRGGBB form (only possible when GUI is running) */
  7590.     if (gui.in_use && what[1] && what[2] == '#')
  7591.     {
  7592.         guicolor_T        color;
  7593.         long_u        rgb;
  7594.         static char_u    buf[10];
  7595.  
  7596.         if (fg)
  7597.         color = HL_TABLE()[id - 1].sg_gui_fg;
  7598.         else
  7599.         color = HL_TABLE()[id - 1].sg_gui_bg;
  7600.         if (color == INVALCOLOR)
  7601.         return NULL;
  7602.         rgb = gui_mch_get_rgb(color);
  7603.         sprintf((char *)buf, "#%02x%02x%02x",
  7604.                       (unsigned)(rgb >> 16),
  7605.                       (unsigned)(rgb >> 8) & 255,
  7606.                       (unsigned)rgb & 255);
  7607.         return buf;
  7608.     }
  7609.     if (fg)
  7610.         return (HL_TABLE()[id - 1].sg_gui_fg_name);
  7611.     return (HL_TABLE()[id - 1].sg_gui_bg_name);
  7612.     }
  7613. #endif
  7614.     if (modec == 'c')
  7615.     {
  7616.     if (fg)
  7617.         n = HL_TABLE()[id - 1].sg_cterm_fg - 1;
  7618.     else
  7619.         n = HL_TABLE()[id - 1].sg_cterm_bg - 1;
  7620.     sprintf((char *)name, "%d", n);
  7621.     return name;
  7622.     }
  7623.     /* term doesn't have color */
  7624.     return NULL;
  7625. }
  7626. #endif
  7627.  
  7628. #if (defined(FEAT_SYN_HL) && defined(FEAT_GUI) && defined(FEAT_PRINTER)) \
  7629.     || defined(PROTO)
  7630. /*
  7631.  * Return color name of highlight group "id" as RGB value.
  7632.  */
  7633.     long_u
  7634. highlight_gui_color_rgb(id, fg)
  7635.     int        id;
  7636.     int        fg;    /* TRUE = fg, FALSE = bg */
  7637. {
  7638.     guicolor_T    color;
  7639.  
  7640.     if (id <= 0 || id > highlight_ga.ga_len)
  7641.     return 0L;
  7642.  
  7643.     if (fg)
  7644.     color = HL_TABLE()[id - 1].sg_gui_fg;
  7645.     else
  7646.     color = HL_TABLE()[id - 1].sg_gui_bg;
  7647.  
  7648.     if (color == INVALCOLOR)
  7649.     return 0L;
  7650.  
  7651.     return gui_mch_get_rgb(color);
  7652. }
  7653. #endif
  7654.  
  7655. /*
  7656.  * Output the syntax list header.
  7657.  * Return TRUE when started a new line.
  7658.  */
  7659.     static int
  7660. syn_list_header(did_header, outlen, id)
  7661.     int        did_header;        /* did header already */
  7662.     int        outlen;        /* length of string that comes */
  7663.     int        id;            /* highlight group id */
  7664. {
  7665.     int        endcol = 19;
  7666.     int        newline = TRUE;
  7667.  
  7668.     if (!did_header)
  7669.     {
  7670.     msg_putchar('\n');
  7671.     msg_outtrans(HL_TABLE()[id - 1].sg_name);
  7672.     endcol = 15;
  7673.     }
  7674.     else if (msg_col + outlen + 1 >= Columns)
  7675.     msg_putchar('\n');
  7676.     else
  7677.     {
  7678.     if (msg_col >= endcol)    /* wrap around is like starting a new line */
  7679.         newline = FALSE;
  7680.     }
  7681.  
  7682.     if (msg_col >= endcol)    /* output at least one space */
  7683.     endcol = msg_col + 1;
  7684.     if (Columns <= endcol)    /* avoid hang for tiny window */
  7685.     endcol = Columns - 1;
  7686.  
  7687.     msg_advance(endcol);
  7688.  
  7689.     /* Show "xxx" with the attributes. */
  7690.     if (!did_header)
  7691.     {
  7692.     msg_puts_attr((char_u *)"xxx", syn_id2attr(id));
  7693.     msg_putchar(' ');
  7694.     }
  7695.  
  7696.     return newline;
  7697. }
  7698.  
  7699. /*
  7700.  * Set the attribute numbers for a highlight group.
  7701.  * Called after one of the attributes has changed.
  7702.  */
  7703.     static void
  7704. set_hl_attr(idx)
  7705.     int        idx;        /* index in array */
  7706. {
  7707.     attrentry_T    at_en;
  7708.  
  7709.     /* The "Normal" group doesn't need an attribute number */
  7710.     if (STRCMP(HL_TABLE()[idx].sg_name_u, "NORMAL") == 0)
  7711.     return;
  7712.  
  7713. #ifdef FEAT_GUI
  7714.     /*
  7715.      * For the GUI mode: If there are other than "normal" highlighting
  7716.      * attributes, need to allocate an attr number.
  7717.      */
  7718.     if (HL_TABLE()[idx].sg_gui_fg == INVALCOLOR
  7719.         && HL_TABLE()[idx].sg_gui_bg == INVALCOLOR
  7720.         && HL_TABLE()[idx].sg_font == NOFONT
  7721. # ifdef FEAT_XFONTSET
  7722.         && HL_TABLE()[idx].sg_fontset == NOFONTSET
  7723. # endif
  7724.         )
  7725.     {
  7726.     HL_TABLE()[idx].sg_gui_attr = HL_TABLE()[idx].sg_gui;
  7727.     }
  7728.     else
  7729.     {
  7730.     at_en.ae_attr = HL_TABLE()[idx].sg_gui;
  7731.     at_en.ae_u.gui.fg_color = HL_TABLE()[idx].sg_gui_fg;
  7732.     at_en.ae_u.gui.bg_color = HL_TABLE()[idx].sg_gui_bg;
  7733.     at_en.ae_u.gui.font = HL_TABLE()[idx].sg_font;
  7734. # ifdef FEAT_XFONTSET
  7735.     at_en.ae_u.gui.fontset = HL_TABLE()[idx].sg_fontset;
  7736. # endif
  7737.     HL_TABLE()[idx].sg_gui_attr = get_attr_entry(&gui_attr_table, &at_en);
  7738.     }
  7739. #endif
  7740.     /*
  7741.      * For the term mode: If there are other than "normal" highlighting
  7742.      * attributes, need to allocate an attr number.
  7743.      */
  7744.     if (HL_TABLE()[idx].sg_start == NULL && HL_TABLE()[idx].sg_stop == NULL)
  7745.     HL_TABLE()[idx].sg_term_attr = HL_TABLE()[idx].sg_term;
  7746.     else
  7747.     {
  7748.     at_en.ae_attr = HL_TABLE()[idx].sg_term;
  7749.     at_en.ae_u.term.start = HL_TABLE()[idx].sg_start;
  7750.     at_en.ae_u.term.stop = HL_TABLE()[idx].sg_stop;
  7751.     HL_TABLE()[idx].sg_term_attr = get_attr_entry(&term_attr_table, &at_en);
  7752.     }
  7753.  
  7754.     /*
  7755.      * For the color term mode: If there are other than "normal"
  7756.      * highlighting attributes, need to allocate an attr number.
  7757.      */
  7758.     if (HL_TABLE()[idx].sg_cterm_fg == 0 && HL_TABLE()[idx].sg_cterm_bg == 0)
  7759.     HL_TABLE()[idx].sg_cterm_attr = HL_TABLE()[idx].sg_cterm;
  7760.     else
  7761.     {
  7762.     at_en.ae_attr = HL_TABLE()[idx].sg_cterm;
  7763.     at_en.ae_u.cterm.fg_color = HL_TABLE()[idx].sg_cterm_fg;
  7764.     at_en.ae_u.cterm.bg_color = HL_TABLE()[idx].sg_cterm_bg;
  7765.     HL_TABLE()[idx].sg_cterm_attr =
  7766.                     get_attr_entry(&cterm_attr_table, &at_en);
  7767.     }
  7768. }
  7769.  
  7770. /*
  7771.  * Lookup a highlight group name and return it's ID.
  7772.  * If it is not found, 0 is returned.
  7773.  */
  7774.     int
  7775. syn_name2id(name)
  7776.     char_u    *name;
  7777. {
  7778.     int        i;
  7779.     char_u    name_u[200];
  7780.  
  7781.     /* Avoid using stricmp() too much, it's slow on some systems */
  7782.     /* Avoid alloc()/free(), these are slow too.  ID names over 200 chars
  7783.      * don't deserve to be found! */
  7784.     STRNCPY(name_u, name, 199);
  7785.     name_u[199] = NUL;
  7786.     vim_strup(name_u);
  7787.     for (i = highlight_ga.ga_len; --i >= 0; )
  7788.     if (HL_TABLE()[i].sg_name_u != NULL
  7789.         && STRCMP(name_u, HL_TABLE()[i].sg_name_u) == 0)
  7790.         break;
  7791.     return i + 1;
  7792. }
  7793.  
  7794. #if defined(FEAT_EVAL) || defined(PROTO)
  7795. /*
  7796.  * Return TRUE if highlight group "name" exists.
  7797.  */
  7798.     int
  7799. highlight_exists(name)
  7800.     char_u    *name;
  7801. {
  7802.     return (syn_name2id(name) > 0);
  7803. }
  7804. #endif
  7805.  
  7806. /*
  7807.  * Like syn_name2id(), but take a pointer + length argument.
  7808.  */
  7809.     int
  7810. syn_namen2id(linep, len)
  7811.     char_u  *linep;
  7812.     int        len;
  7813. {
  7814.     char_u  *name;
  7815.     int        id = 0;
  7816.  
  7817.     name = vim_strnsave(linep, len);
  7818.     if (name != NULL)
  7819.     {
  7820.     id = syn_name2id(name);
  7821.     vim_free(name);
  7822.     }
  7823.     return id;
  7824. }
  7825.  
  7826. /*
  7827.  * Find highlight group name in the table and return it's ID.
  7828.  * The argument is a pointer to the name and the length of the name.
  7829.  * If it doesn't exist yet, a new entry is created.
  7830.  * Return 0 for failure.
  7831.  */
  7832.     int
  7833. syn_check_group(pp, len)
  7834.     char_u        *pp;
  7835.     int            len;
  7836. {
  7837.     int        id;
  7838.     char_u  *name;
  7839.  
  7840.     name = vim_strnsave(pp, len);
  7841.     if (name == NULL)
  7842.     return 0;
  7843.  
  7844.     id = syn_name2id(name);
  7845.     if (id == 0)            /* doesn't exist yet */
  7846.     id = syn_add_group(name);
  7847.     else
  7848.     vim_free(name);
  7849.     return id;
  7850. }
  7851.  
  7852. /*
  7853.  * Add new highlight group and return it's ID.
  7854.  * "name" must be an allocated string, it will be consumed.
  7855.  * Return 0 for failure.
  7856.  */
  7857.     static int
  7858. syn_add_group(name)
  7859.     char_u        *name;
  7860. {
  7861.     /* TODO: check that the name is ASCII letters, digits and underscore. */
  7862.     /*
  7863.      * First call for this growarray: init growing array.
  7864.      */
  7865.     if (highlight_ga.ga_data == NULL)
  7866.     {
  7867.     highlight_ga.ga_itemsize = sizeof(struct hl_group);
  7868.     highlight_ga.ga_growsize = 10;
  7869.     }
  7870.  
  7871.     /*
  7872.      * Make room for at least one other syntax_highlight entry.
  7873.      */
  7874.     if (ga_grow(&highlight_ga, 1) == FAIL)
  7875.     {
  7876.     vim_free(name);
  7877.     return 0;
  7878.     }
  7879.  
  7880.     vim_memset(&(HL_TABLE()[highlight_ga.ga_len]), 0, sizeof(struct hl_group));
  7881.     HL_TABLE()[highlight_ga.ga_len].sg_name = name;
  7882.     HL_TABLE()[highlight_ga.ga_len].sg_name_u = vim_strsave_up(name);
  7883. #ifdef FEAT_GUI
  7884.     HL_TABLE()[highlight_ga.ga_len].sg_gui_bg = INVALCOLOR;
  7885.     HL_TABLE()[highlight_ga.ga_len].sg_gui_fg = INVALCOLOR;
  7886. #endif
  7887.     ++highlight_ga.ga_len;
  7888.     --highlight_ga.ga_room;
  7889.  
  7890.     return highlight_ga.ga_len;            /* ID is index plus one */
  7891. }
  7892.  
  7893. /*
  7894.  * When, just after calling syn_add_group(), an error is discovered, this
  7895.  * function deletes the new name.
  7896.  */
  7897.     static void
  7898. syn_unadd_group()
  7899. {
  7900.     --highlight_ga.ga_len;
  7901.     ++highlight_ga.ga_room;
  7902.     vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name);
  7903.     vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name_u);
  7904. }
  7905.  
  7906. /*
  7907.  * Translate a group ID to highlight attributes.
  7908.  */
  7909.     int
  7910. syn_id2attr(hl_id)
  7911.     int            hl_id;
  7912. {
  7913.     int            attr;
  7914.     struct hl_group    *sgp;
  7915.  
  7916.     hl_id = syn_get_final_id(hl_id);
  7917.     sgp = &HL_TABLE()[hl_id - 1];        /* index is ID minus one */
  7918.  
  7919. #ifdef FEAT_GUI
  7920.     /*
  7921.      * Only use GUI attr when the GUI is being used.
  7922.      */
  7923.     if (gui.in_use)
  7924.     attr = sgp->sg_gui_attr;
  7925.     else
  7926. #endif
  7927.     if (t_colors > 1)
  7928.         attr = sgp->sg_cterm_attr;
  7929.     else
  7930.         attr = sgp->sg_term_attr;
  7931.  
  7932.     return attr;
  7933. }
  7934.  
  7935. #ifdef FEAT_GUI
  7936. /*
  7937.  * Get the GUI colors and attributes for a group ID.
  7938.  * NOTE: the colors will be INVALCOLOR when not set, the color otherwise.
  7939.  */
  7940.     int
  7941. syn_id2colors(hl_id, fgp, bgp)
  7942.     int        hl_id;
  7943.     guicolor_T    *fgp;
  7944.     guicolor_T    *bgp;
  7945. {
  7946.     struct hl_group    *sgp;
  7947.  
  7948.     hl_id = syn_get_final_id(hl_id);
  7949.     sgp = &HL_TABLE()[hl_id - 1];        /* index is ID minus one */
  7950.  
  7951.     *fgp = sgp->sg_gui_fg;
  7952.     *bgp = sgp->sg_gui_bg;
  7953.     return sgp->sg_gui;
  7954. }
  7955. #endif
  7956.  
  7957. /*
  7958.  * Translate a group ID to the final group ID (following links).
  7959.  */
  7960.     int
  7961. syn_get_final_id(hl_id)
  7962.     int            hl_id;
  7963. {
  7964.     int            count;
  7965.     struct hl_group    *sgp;
  7966.  
  7967.     if (hl_id > highlight_ga.ga_len || hl_id < 1)
  7968.     return 0;            /* Can be called from eval!! */
  7969.  
  7970.     /*
  7971.      * Follow links until there is no more.
  7972.      * Look out for loops!  Break after 100 links.
  7973.      */
  7974.     for (count = 100; --count >= 0; )
  7975.     {
  7976.     sgp = &HL_TABLE()[hl_id - 1];        /* index is ID minus one */
  7977.     if (sgp->sg_link == 0 || sgp->sg_link > highlight_ga.ga_len)
  7978.         break;
  7979.     hl_id = sgp->sg_link;
  7980.     }
  7981.  
  7982.     return hl_id;
  7983. }
  7984.  
  7985. #ifdef FEAT_GUI
  7986. /*
  7987.  * Call this function just after the GUI has started.
  7988.  * It finds the font and color handles for the highlighting groups.
  7989.  */
  7990.     void
  7991. highlight_gui_started()
  7992. {
  7993.     int        idx;
  7994.  
  7995.     /* First get the colors from the "Normal" and "Menu" group, if set */
  7996.     set_normal_colors();
  7997.  
  7998.     for (idx = 0; idx < highlight_ga.ga_len; ++idx)
  7999.     gui_do_one_color(idx, FALSE, FALSE);
  8000.  
  8001.     highlight_changed();
  8002. }
  8003.  
  8004.     static void
  8005. gui_do_one_color(idx, do_menu, do_tooltip)
  8006.     int        idx;
  8007.     int        do_menu;    /* TRUE: might set the menu font */
  8008.     int        do_tooltip;    /* TRUE: might set the tooltip font */
  8009. {
  8010.     int        didit = FALSE;
  8011.  
  8012.     if (HL_TABLE()[idx].sg_font_name != NULL)
  8013.     {
  8014.     hl_do_font(idx, HL_TABLE()[idx].sg_font_name, FALSE, do_menu,
  8015.            do_tooltip);
  8016.     didit = TRUE;
  8017.     }
  8018.     if (HL_TABLE()[idx].sg_gui_fg_name != NULL)
  8019.     {
  8020.     HL_TABLE()[idx].sg_gui_fg =
  8021.                 color_name2handle(HL_TABLE()[idx].sg_gui_fg_name);
  8022.     didit = TRUE;
  8023.     }
  8024.     if (HL_TABLE()[idx].sg_gui_bg_name != NULL)
  8025.     {
  8026.     HL_TABLE()[idx].sg_gui_bg =
  8027.                 color_name2handle(HL_TABLE()[idx].sg_gui_bg_name);
  8028.     didit = TRUE;
  8029.     }
  8030.     if (didit)    /* need to get a new attr number */
  8031.     set_hl_attr(idx);
  8032. }
  8033.  
  8034. #endif
  8035.  
  8036. /*
  8037.  * Translate the 'highlight' option into attributes in highlight_attr[] and
  8038.  * set up the user highlights User1..9.  If FEAT_STL_OPT is in use, a set of
  8039.  * corresponding highlights to use on top of HLF_SNC is computed.
  8040.  * Called only when the 'highlight' option has been changed and upon first
  8041.  * screen redraw after any :highlight command.
  8042.  * Return FAIL when an invalid flag is found in 'highlight'.  OK otherwise.
  8043.  */
  8044.     int
  8045. highlight_changed()
  8046. {
  8047.     int        hlf;
  8048.     int        i;
  8049.     char_u    *p;
  8050.     int        attr;
  8051.     char_u    *end;
  8052.     int        id;
  8053. #ifdef USER_HIGHLIGHT
  8054.     char_u      userhl[10];
  8055. # ifdef FEAT_STL_OPT
  8056.     int        id_SNC = -1;
  8057.     int        id_S = -1;
  8058.     int        hlcnt;
  8059. # endif
  8060. #endif
  8061.     static int    hl_flags[HLF_COUNT] = HL_FLAGS;
  8062.  
  8063.     need_highlight_changed = FALSE;
  8064.  
  8065.     /*
  8066.      * Clear all attributes.
  8067.      */
  8068.     for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
  8069.     highlight_attr[hlf] = 0;
  8070.  
  8071.     /*
  8072.      * First set all attributes to their default value.
  8073.      * Then use the attributes from the 'highlight' option.
  8074.      */
  8075.     for (i = 0; i < 2; ++i)
  8076.     {
  8077.     if (i)
  8078.         p = p_hl;
  8079.     else
  8080.         p = get_highlight_default();
  8081.     if (p == NULL)        /* just in case */
  8082.         continue;
  8083.  
  8084.     while (*p)
  8085.     {
  8086.         for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
  8087.         if (hl_flags[hlf] == *p)
  8088.             break;
  8089.         ++p;
  8090.         if (hlf == (int)HLF_COUNT || *p == NUL)
  8091.         return FAIL;
  8092.  
  8093.         /*
  8094.          * Allow several hl_flags to be combined, like "bu" for
  8095.          * bold-underlined.
  8096.          */
  8097.         attr = 0;
  8098.         for ( ; *p && *p != ','; ++p)        /* parse upto comma */
  8099.         {
  8100.         if (vim_iswhite(*p))            /* ignore white space */
  8101.             continue;
  8102.  
  8103.         if (attr > HL_ALL)  /* Combination with ':' is not allowed. */
  8104.             return FAIL;
  8105.  
  8106.         switch (*p)
  8107.         {
  8108.             case 'b':    attr |= HL_BOLD;
  8109.                 break;
  8110.             case 'i':    attr |= HL_ITALIC;
  8111.                 break;
  8112.             case '-':
  8113.             case 'n':                /* no highlighting */
  8114.                 break;
  8115.             case 'r':    attr |= HL_INVERSE;
  8116.                 break;
  8117.             case 's':    attr |= HL_STANDOUT;
  8118.                 break;
  8119.             case 'u':    attr |= HL_UNDERLINE;
  8120.                 break;
  8121.             case ':':    ++p;            /* highlight group name */
  8122.                 if (attr || *p == NUL)     /* no combinations */
  8123.                     return FAIL;
  8124.                 end = vim_strchr(p, ',');
  8125.                 if (end == NULL)
  8126.                     end = p + STRLEN(p);
  8127.                 id = syn_check_group(p, (int)(end - p));
  8128.                 if (id == 0)
  8129.                     return FAIL;
  8130.                 attr = syn_id2attr(id);
  8131.                 p = end - 1;
  8132. #if defined(FEAT_STL_OPT) && defined(USER_HIGHLIGHT)
  8133.                 if (hlf == (int)HLF_SNC)
  8134.                     id_SNC = syn_get_final_id(id);
  8135.                 else if (hlf == (int)HLF_S)
  8136.                     id_S = syn_get_final_id(id);
  8137. #endif
  8138.                 break;
  8139.             default:    return FAIL;
  8140.         }
  8141.         }
  8142.         highlight_attr[hlf] = attr;
  8143.  
  8144.         p = skip_to_option_part(p);        /* skip comma and spaces */
  8145.     }
  8146.     }
  8147.  
  8148. #ifdef USER_HIGHLIGHT
  8149.     /* Setup the user highlights
  8150.      *
  8151.      * Temporarily  utilize 10 more hl entries.  Have to be in there
  8152.      * simultaneously in case of table overflows in get_attr_entry()
  8153.      */
  8154. # ifdef FEAT_STL_OPT
  8155.     if (ga_grow(&highlight_ga, 10) == FAIL)
  8156.     return FAIL;
  8157.     hlcnt = highlight_ga.ga_len;
  8158.     if (id_S == 0)
  8159.     {            /* Make sure id_S is always valid to simplify code below */
  8160.     memset(&HL_TABLE()[hlcnt + 9], 0, sizeof(struct hl_group));
  8161.     HL_TABLE()[hlcnt + 9].sg_term = highlight_attr[HLF_S];
  8162.     id_S = hlcnt + 10;
  8163.     }
  8164. # endif
  8165.     for (i = 0; i < 9; i++)
  8166.     {
  8167.     sprintf((char *)userhl, "User%d", i + 1);
  8168.     id = syn_name2id(userhl);
  8169.     if (id == 0)
  8170.     {
  8171.         highlight_user[i] = 0;
  8172. # ifdef FEAT_STL_OPT
  8173.         highlight_stlnc[i] = 0;
  8174. # endif
  8175.     }
  8176.     else
  8177.     {
  8178. # ifdef FEAT_STL_OPT
  8179.         struct hl_group *hlt = HL_TABLE();
  8180. # endif
  8181.  
  8182.         highlight_user[i] = syn_id2attr(id);
  8183. # ifdef FEAT_STL_OPT
  8184.         if (id_SNC == 0)
  8185.         {
  8186.         memset(&hlt[hlcnt + i], 0, sizeof(struct hl_group));
  8187.         hlt[hlcnt + i].sg_term = highlight_attr[HLF_SNC];
  8188.         hlt[hlcnt + i].sg_cterm = highlight_attr[HLF_SNC];
  8189. #  ifdef FEAT_GUI
  8190.         hlt[hlcnt + i].sg_gui = highlight_attr[HLF_SNC];
  8191. #  endif
  8192.         }
  8193.         else
  8194.         mch_memmove(&hlt[hlcnt + i],
  8195.                 &hlt[id_SNC - 1],
  8196.                 sizeof(struct hl_group));
  8197.         hlt[hlcnt + i].sg_link = 0;
  8198.  
  8199.         /* Apply difference between UserX and HLF_S to HLF_SNC */
  8200.         hlt[hlcnt + i].sg_term ^=
  8201.         hlt[id - 1].sg_term ^ hlt[id_S - 1].sg_term;
  8202.         if (hlt[id - 1].sg_start != hlt[id_S - 1].sg_start)
  8203.         hlt[hlcnt + i].sg_start = hlt[id - 1].sg_start;
  8204.         if (hlt[id - 1].sg_stop != hlt[id_S - 1].sg_stop)
  8205.         hlt[hlcnt + i].sg_stop = hlt[id - 1].sg_stop;
  8206.         hlt[hlcnt + i].sg_cterm ^=
  8207.         hlt[id - 1].sg_cterm ^ hlt[id_S - 1].sg_cterm;
  8208.         if (hlt[id - 1].sg_cterm_fg != hlt[id_S - 1].sg_cterm_fg)
  8209.         hlt[hlcnt + i].sg_cterm_fg = hlt[id - 1].sg_cterm_fg;
  8210.         if (hlt[id - 1].sg_cterm_bg != hlt[id_S - 1].sg_cterm_bg)
  8211.         hlt[hlcnt + i].sg_cterm_bg = hlt[id - 1].sg_cterm_bg;
  8212. #  ifdef FEAT_GUI
  8213.         hlt[hlcnt + i].sg_gui ^=
  8214.         hlt[id - 1].sg_gui ^ hlt[id_S - 1].sg_gui;
  8215.         if (hlt[id - 1].sg_gui_fg != hlt[id_S - 1].sg_gui_fg)
  8216.         hlt[hlcnt + i].sg_gui_fg = hlt[id - 1].sg_gui_fg;
  8217.         if (hlt[id - 1].sg_gui_bg != hlt[id_S - 1].sg_gui_bg)
  8218.         hlt[hlcnt + i].sg_gui_bg = hlt[id - 1].sg_gui_bg;
  8219.         if (hlt[id - 1].sg_font != hlt[id_S - 1].sg_font)
  8220.         hlt[hlcnt + i].sg_font = hlt[id - 1].sg_font;
  8221. #   ifdef FEAT_XFONTSET
  8222.         if (hlt[id - 1].sg_fontset != hlt[id_S - 1].sg_fontset)
  8223.         hlt[hlcnt + i].sg_fontset = hlt[id - 1].sg_fontset;
  8224. #   endif
  8225. #  endif
  8226.         highlight_ga.ga_len = hlcnt + i + 1;
  8227.         set_hl_attr(hlcnt + i);    /* At long last we can apply */
  8228.         highlight_stlnc[i] = syn_id2attr(hlcnt + i + 1);
  8229. # endif
  8230.     }
  8231.     }
  8232. # ifdef FEAT_STL_OPT
  8233.     highlight_ga.ga_len = hlcnt;
  8234. # endif
  8235.  
  8236. #endif /* USER_HIGHLIGHT */
  8237.  
  8238.     return OK;
  8239. }
  8240.  
  8241. #ifdef FEAT_CMDL_COMPL
  8242.  
  8243. static void highlight_list __ARGS((void));
  8244. static void highlight_list_two __ARGS((int cnt, int attr));
  8245.  
  8246. /*
  8247.  * Handle command line completion for :highlight command.
  8248.  */
  8249.     void
  8250. set_context_in_highlight_cmd(xp, arg)
  8251.     expand_T    *xp;
  8252.     char_u    *arg;
  8253. {
  8254.     char_u    *p;
  8255.  
  8256.     /* Default: expand group names */
  8257.     xp->xp_context = EXPAND_HIGHLIGHT;
  8258.     xp->xp_pattern = arg;
  8259.     include_link = TRUE;
  8260.     include_default = TRUE;
  8261.  
  8262.     /* (part of) subcommand already typed */
  8263.     if (*arg != NUL)
  8264.     {
  8265.     p = skiptowhite(arg);
  8266.     if (*p != NUL)            /* past "default" or group name */
  8267.     {
  8268.         include_default = FALSE;
  8269.         if (STRNCMP("default", arg, p - arg) == 0)
  8270.         {
  8271.         arg = skipwhite(p);
  8272.         xp->xp_pattern = arg;
  8273.         p = skiptowhite(arg);
  8274.         }
  8275.         if (*p != NUL)            /* past group name */
  8276.         {
  8277.         include_link = FALSE;
  8278.         if (arg[1] == 'i' && arg[0] == 'N')
  8279.             highlight_list();
  8280.         if (STRNCMP("link", arg, p - arg) == 0
  8281.             || STRNCMP("clear", arg, p - arg) == 0)
  8282.         {
  8283.             xp->xp_pattern = skipwhite(p);
  8284.             p = skiptowhite(xp->xp_pattern);
  8285.             if (*p != NUL)        /* past first group name */
  8286.             {
  8287.             xp->xp_pattern = skipwhite(p);
  8288.             p = skiptowhite(xp->xp_pattern);
  8289.             }
  8290.         }
  8291.         if (*p != NUL)            /* past group name(s) */
  8292.             xp->xp_context = EXPAND_NOTHING;
  8293.         }
  8294.     }
  8295.     }
  8296. }
  8297.  
  8298. /*
  8299.  * List highlighting matches in a nice way.
  8300.  */
  8301.     static void
  8302. highlight_list()
  8303. {
  8304.     int        i;
  8305.  
  8306.     for (i = 10; --i >= 0; )
  8307.     highlight_list_two(i, hl_attr(HLF_D));
  8308.     for (i = 40; --i >= 0; )
  8309.     highlight_list_two(99, 0);
  8310. }
  8311.  
  8312.     static void
  8313. highlight_list_two(cnt, attr)
  8314.     int        cnt;
  8315.     int        attr;
  8316. {
  8317.     msg_puts_attr((char_u *)("N \bI \b!  \b" + cnt / 11), attr);
  8318.     msg_clr_eos();
  8319.     out_flush();
  8320.     ui_delay(cnt == 99 ? 40L : (long)cnt * 50L, FALSE);
  8321. }
  8322.  
  8323. #endif /* FEAT_CMDL_COMPL */
  8324.  
  8325. #if defined(FEAT_CMDL_COMPL) || (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) \
  8326.     || defined(FEAT_SIGNS) || defined(PROTO)
  8327. /*
  8328.  * Function given to ExpandGeneric() to obtain the list of group names.
  8329.  * Also used for synIDattr() function.
  8330.  */
  8331. /*ARGSUSED*/
  8332.     char_u *
  8333. get_highlight_name(xp, idx)
  8334.     expand_T    *xp;
  8335.     int        idx;
  8336. {
  8337.     if (idx == highlight_ga.ga_len
  8338. #ifdef FEAT_CMDL_COMPL
  8339.         && include_link
  8340. #endif
  8341.         )
  8342.     return (char_u *)"link";
  8343.     if (idx == highlight_ga.ga_len + 1
  8344. #ifdef FEAT_CMDL_COMPL
  8345.         && include_link
  8346. #endif
  8347.         )
  8348.     return (char_u *)"clear";
  8349.     if (idx == highlight_ga.ga_len + 2
  8350. #ifdef FEAT_CMDL_COMPL
  8351.         && include_default
  8352. #endif
  8353.         )
  8354.     return (char_u *)"default";
  8355.     if (idx < 0 || idx >= highlight_ga.ga_len)
  8356.     return NULL;
  8357.     return HL_TABLE()[idx].sg_name;
  8358. }
  8359. #endif
  8360.  
  8361. #ifdef FEAT_GUI
  8362. /*
  8363.  * Free all the highlight group fonts.
  8364.  * Used when quitting for systems which need it.
  8365.  */
  8366.     void
  8367. free_highlight_fonts()
  8368. {
  8369.     int        idx;
  8370.  
  8371.     for (idx = 0; idx < highlight_ga.ga_len; ++idx)
  8372.     {
  8373.     gui_mch_free_font(HL_TABLE()[idx].sg_font);
  8374.     HL_TABLE()[idx].sg_font = NOFONT;
  8375. # ifdef FEAT_XFONTSET
  8376.     gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
  8377.     HL_TABLE()[idx].sg_fontset = NOFONTSET;
  8378. # endif
  8379.     }
  8380.  
  8381.     gui_mch_free_font(gui.norm_font);
  8382. # ifdef FEAT_XFONTSET
  8383.     gui_mch_free_fontset(gui.fontset);
  8384. # endif
  8385. # ifndef HAVE_GTK2
  8386.     gui_mch_free_font(gui.bold_font);
  8387.     gui_mch_free_font(gui.ital_font);
  8388.     gui_mch_free_font(gui.boldital_font);
  8389. # endif
  8390. }
  8391. #endif
  8392.  
  8393. /**************************************
  8394.  *  End of Highlighting stuff          *
  8395.  **************************************/
  8396.