Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
syntax.c File Reference
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/api/private/helpers.h"
#include "nvim/syntax.h"
#include "nvim/charset.h"
#include "nvim/cursor_shape.h"
#include "nvim/eval.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/fileio.h"
#include "nvim/fold.h"
#include "nvim/hashtab.h"
#include "nvim/highlight.h"
#include "nvim/indent_c.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
#include "nvim/keymap.h"
#include "nvim/garray.h"
#include "nvim/option.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
#include "nvim/macros.h"
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/sign.h"
#include "nvim/strings.h"
#include "nvim/syntax_defs.h"
#include "nvim/terminal.h"
#include "nvim/ui.h"
#include "nvim/os/os.h"
#include "nvim/os/time.h"
#include "nvim/buffer.h"

Data Structures

struct  hl_group
 
struct  syn_pattern
 
struct  syn_cluster_S
 
struct  state_item
 
struct  syn_opt_arg_T
 
struct  time_entry_T
 
struct  name_list
 
struct  subcommand
 

Macros

#define SG_CTERM   2
 
#define SG_GUI   4
 
#define SG_LINK   8
 
#define MAX_HL_ID   20000 /* maximum value for a highlight ID. */
 
#define SPO_MS_OFF   0 /* match start offset */
 
#define SPO_ME_OFF   1 /* match end offset */
 
#define SPO_HS_OFF   2 /* highl. start offset */
 
#define SPO_HE_OFF   3 /* highl. end offset */
 
#define SPO_RS_OFF   4 /* region start offset */
 
#define SPO_RE_OFF   5 /* region end offset */
 
#define SPO_LC_OFF   6 /* leading context offset */
 
#define SPO_COUNT   7
 
#define SPTYPE_MATCH   1 /* match keyword with this group ID */
 
#define SPTYPE_START   2 /* match a regexp, start of item */
 
#define SPTYPE_END   3 /* match a regexp, end of item */
 
#define SPTYPE_SKIP   4 /* match a regexp, skip within item */
 
#define SYN_ITEMS(buf)   ((synpat_T *)((buf)->b_syn_patterns.ga_data))
 
#define NONE_IDX   -2 /* value of sp_sync_idx for "NONE" */
 
#define SF_CCOMMENT   0x01 /* sync on a C-style comment */
 
#define SF_MATCH   0x02 /* sync by matching a pattern */
 
#define SYN_STATE_P(ssp)   ((bufstate_T *)((ssp)->ga_data))
 
#define MAXKEYWLEN   80 /* maximum length of a keyword */
 
#define CLUSTER_REPLACE   1 /* replace first list with second */
 
#define CLUSTER_ADD   2 /* add second list to first */
 
#define CLUSTER_SUBTRACT   3 /* subtract second list from first */
 
#define SYN_CLSTR(buf)   ((syn_cluster_T *)((buf)->b_syn_clusters.ga_data))
 
#define SYNID_ALLBUT   MAX_HL_ID /* syntax group ID for contains=ALLBUT */
 
#define SYNID_TOP   21000 /* syntax group ID for contains=TOP */
 
#define SYNID_CONTAINED   22000 /* syntax group ID for contains=CONTAINED */
 
#define SYNID_CLUSTER   23000 /* first syntax group ID for clusters */
 
#define MAX_SYN_INC_TAG   999 /* maximum before the above overflow */
 
#define MAX_CLUSTER_ID   (32767 - SYNID_CLUSTER)
 
#define KE2HIKEY(kp)   ((kp)->keyword)
 
#define HIKEY2KE(p)   ((keyentry_T *)((p) - (dumkey.keyword - (char_u *)&dumkey)))
 
#define HI2KE(hi)   HIKEY2KE((hi)->hi_key)
 
#define KEYWORD_IDX   -1
 
#define ID_LIST_ALL   (int16_t *)-1
 
#define INVALID_STATE(ssp)   ((ssp)->ga_itemsize == 0)
 
#define VALID_STATE(ssp)   ((ssp)->ga_itemsize != 0)
 
#define CUR_STATE(idx)   ((stateitem_T *)(current_state.ga_data))[idx]
 
#define IF_SYN_TIME(p)   (p)
 
#define UNREF_BUFSTATE_EXTMATCH(bs)   unref_extmatch((bs)->bs_extmatch)
 
#define UNREF_STATEITEM_EXTMATCH(si)   unref_extmatch((si)->si_extmatch)
 
#define ITEM_START   0
 
#define ITEM_SKIP   1
 
#define ITEM_END   2
 
#define ITEM_MATCHGROUP   3
 
#define LIST_ATTR   1
 
#define LIST_STRING   2
 
#define LIST_INT   3
 

Typedefs

typedef struct syn_pattern synpat_T
 
typedef struct syn_cluster_S syn_cluster_T
 
typedef struct state_item stateitem_T
 

Enumerations

enum  { EXP_SUBCMD, EXP_CASE, EXP_SPELL, EXP_SYNC }
 

Functions

void syn_set_timeout (proftime_T *tm)
 
void syntax_start (win_T *wp, linenr_T lnum)
 
void syn_stack_free_all (synblock_T *block)
 
void syn_stack_apply_changes (buf_T *buf)
 
void syntax_end_parsing (linenr_T lnum)
 
int syntax_check_changed (linenr_T lnum)
 
int get_syntax_attr (const colnr_T col, bool *const can_spell, const bool keep_state)
 
void syntax_clear (synblock_T *block)
 
void reset_synblock (win_T *wp)
 
void syn_maybe_on (void)
 
void ex_syntax (exarg_T *eap)
 
void ex_ownsyntax (exarg_T *eap)
 
bool syntax_present (win_T *win)
 
void reset_expand_highlight (void)
 
void set_context_in_echohl_cmd (expand_T *xp, const char *arg)
 
void set_context_in_syntax_cmd (expand_T *xp, const char *arg)
 
char_uget_syntax_name (expand_T *xp, int idx)
 
int syn_get_id (win_T *wp, long lnum, colnr_T col, int trans, bool *spellp, int keep_state)
 
int get_syntax_info (int *seqnrp)
 
int syn_get_concealed_id (win_T *wp, linenr_T lnum, colnr_T col)
 
int syn_get_sub_char (void)
 
int syn_get_stack_item (int i)
 
int syn_get_foldlevel (win_T *wp, long lnum)
 
void ex_syntime (exarg_T *eap)
 
char_uget_syntime_arg (expand_T *xp, int idx)
 
void syn_init_cmdline_highlight (bool reset, bool init)
 Create default links for Nvim* highlight groups used for cmdline coloring. More...
 
void init_highlight (bool both, bool reset)
 
int load_colors (char_u *name)
 
int lookup_color (const int idx, const bool foreground, TriState *const boldp)
 
void do_highlight (const char *line, const bool forceit, const bool init) FUNC_ATTR_NONNULL_ALL
 
void restore_cterm_colors (void)
 
const char * highlight_has_attr (const int id, const int flag, const int modec) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
 
const char * highlight_color (const int id, const char *const what, const int modec) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
 
int syn_name2id (const char_u *name) FUNC_ATTR_NONNULL_ALL
 
int syn_name2attr (const char_u *name) FUNC_ATTR_NONNULL_ALL
 
int highlight_exists (const char_u *name)
 
char_usyn_id2name (int id)
 
int syn_namen2id (const char_u *linep, int len)
 
int syn_check_group (const char_u *pp, int len)
 
int syn_id2attr (int hl_id)
 
int syn_get_final_id (int hl_id)
 
void highlight_attr_set_all (void)
 Refresh the color attributes of all highlight groups. More...
 
void highlight_changed (void)
 
void set_context_in_highlight_cmd (expand_T *xp, const char *arg)
 
const char * get_highlight_name (expand_T *const xp, int idx) FUNC_ATTR_WARN_UNUSED_RESULT
 Function given to ExpandGeneric() to obtain the list of group names. More...
 
const char * get_highlight_name_ext (expand_T *xp, int idx, int skip_cleared) FUNC_ATTR_WARN_UNUSED_RESULT
 
RgbValue name_to_color (const char_u *name)
 

Variables

const char *const highlight_init_cmdline []
 Array of highlight definitions, used for unit testing. More...
 
color_name_table_T color_name_table []
 

Macro Definition Documentation

#define CLUSTER_ADD   2 /* add second list to first */
#define CLUSTER_REPLACE   1 /* replace first list with second */
#define CLUSTER_SUBTRACT   3 /* subtract second list from first */
#define CUR_STATE (   idx)    ((stateitem_T *)(current_state.ga_data))[idx]
#define HI2KE (   hi)    HIKEY2KE((hi)->hi_key)
#define HIKEY2KE (   p)    ((keyentry_T *)((p) - (dumkey.keyword - (char_u *)&dumkey)))
#define ID_LIST_ALL   (int16_t *)-1
#define IF_SYN_TIME (   p)    (p)
#define INVALID_STATE (   ssp)    ((ssp)->ga_itemsize == 0)
#define ITEM_END   2
#define ITEM_MATCHGROUP   3
#define ITEM_SKIP   1
#define ITEM_START   0
#define KE2HIKEY (   kp)    ((kp)->keyword)
#define KEYWORD_IDX   -1
#define MAX_CLUSTER_ID   (32767 - SYNID_CLUSTER)
#define MAX_HL_ID   20000 /* maximum value for a highlight ID. */
#define MAX_SYN_INC_TAG   999 /* maximum before the above overflow */
#define MAXKEYWLEN   80 /* maximum length of a keyword */
#define NONE_IDX   -2 /* value of sp_sync_idx for "NONE" */
#define SF_CCOMMENT   0x01 /* sync on a C-style comment */
#define SF_MATCH   0x02 /* sync by matching a pattern */
#define SPO_COUNT   7
#define SPO_HE_OFF   3 /* highl. end offset */
#define SPO_HS_OFF   2 /* highl. start offset */
#define SPO_LC_OFF   6 /* leading context offset */
#define SPO_ME_OFF   1 /* match end offset */
#define SPO_MS_OFF   0 /* match start offset */
#define SPO_RE_OFF   5 /* region end offset */
#define SPO_RS_OFF   4 /* region start offset */
#define SPTYPE_END   3 /* match a regexp, end of item */
#define SPTYPE_MATCH   1 /* match keyword with this group ID */
#define SPTYPE_SKIP   4 /* match a regexp, skip within item */
#define SPTYPE_START   2 /* match a regexp, start of item */
#define SYN_CLSTR (   buf)    ((syn_cluster_T *)((buf)->b_syn_clusters.ga_data))
#define SYN_ITEMS (   buf)    ((synpat_T *)((buf)->b_syn_patterns.ga_data))
#define SYN_STATE_P (   ssp)    ((bufstate_T *)((ssp)->ga_data))
#define SYNID_ALLBUT   MAX_HL_ID /* syntax group ID for contains=ALLBUT */
#define SYNID_CLUSTER   23000 /* first syntax group ID for clusters */
#define SYNID_CONTAINED   22000 /* syntax group ID for contains=CONTAINED */
#define SYNID_TOP   21000 /* syntax group ID for contains=TOP */
#define UNREF_BUFSTATE_EXTMATCH (   bs)    unref_extmatch((bs)->bs_extmatch)
#define UNREF_STATEITEM_EXTMATCH (   si)    unref_extmatch((si)->si_extmatch)
#define VALID_STATE (   ssp)    ((ssp)->ga_itemsize != 0)

Typedef Documentation

typedef struct state_item stateitem_T
typedef struct syn_cluster_S syn_cluster_T
typedef struct syn_pattern synpat_T

Enumeration Type Documentation

anonymous enum
Enumerator
EXP_SUBCMD 
EXP_CASE 
EXP_SPELL 
EXP_SYNC 

Function Documentation

void do_highlight ( const char *  line,
const bool  forceit,
const bool  init 
)

Handle ":highlight" command

When using ":highlight clear" this is called recursively for each group with forceit and init being both true.

Parameters
[in]lineCommand arguments.
[in]forceitTrue when bang is given, allows to link group even if it has its own settings.
[in]initTrue when initializing.
void ex_ownsyntax ( exarg_T eap)
void ex_syntax ( exarg_T eap)
void ex_syntime ( exarg_T eap)
const char* get_highlight_name ( expand_T *const  xp,
int  idx 
)

Function given to ExpandGeneric() to obtain the list of group names.

const char* get_highlight_name_ext ( expand_T xp,
int  idx,
int  skip_cleared 
)

Obtain a highlight group name. When "skip_cleared" is TRUE don't return a cleared entry.

int get_syntax_attr ( const colnr_T  col,
bool *const  can_spell,
const bool  keep_state 
)
int get_syntax_info ( int *  seqnrp)
char_u* get_syntax_name ( expand_T xp,
int  idx 
)
char_u* get_syntime_arg ( expand_T xp,
int  idx 
)
void highlight_attr_set_all ( void  )

Refresh the color attributes of all highlight groups.

void highlight_changed ( void  )

Translate highlight groups into attributes in highlight_attr[] and set up the user highlights User1..9. A set of corresponding highlights to use on top of HLF_SNC is computed. Called only when nvim starts and upon first screen redraw after any :highlight command.

Translate builtin highlight groups into attributes for quick lookup.

const char* highlight_color ( const int  id,
const char *const  what,
const int  modec 
)

Return color name of the given highlight group

Parameters
[in]idHighlight group to work with.
[in]whatWhat to return: one of "font", "fg", "bg", "sp", "fg#", "bg#" or "sp#".
[in]modec'g' for GUI, 'c' for cterm and 't' for term.
Returns
color name, possibly in a static buffer. Buffer will be overwritten on next highlight_color() call. May return NULL.
int highlight_exists ( const char_u name)
const char* highlight_has_attr ( const int  id,
const int  flag,
const int  modec 
)

Check whether highlight group has attribute

Parameters
[in]idHighlight group to check.
[in]flagAttribute to check.
[in]modec'g' for GUI, 'c' for term.
Returns
"1" if highlight group has attribute, NULL otherwise.
void init_highlight ( bool  both,
bool  reset 
)

Load colors from a file if "g:colors_name" is set, otherwise load builtin colors

Parameters
bothinclude groups where 'bg' doesn't matter
resetclear groups first
int load_colors ( char_u name)
int lookup_color ( const int  idx,
const bool  foreground,
TriState *const  boldp 
)
RgbValue name_to_color ( const char_u name)

Translate to RgbValue if name is an hex value (e.g. #XXXXXX), else look into color_name_table to translate a color name to its hex value

Parameters
[in]namestring value to convert to RGB return the hex value or -1 if could not find a correct value
void reset_expand_highlight ( void  )
void reset_synblock ( win_T wp)
void restore_cterm_colors ( void  )
void set_context_in_echohl_cmd ( expand_T xp,
const char *  arg 
)
void set_context_in_highlight_cmd ( expand_T xp,
const char *  arg 
)
void set_context_in_syntax_cmd ( expand_T xp,
const char *  arg 
)
int syn_check_group ( const char_u pp,
int  len 
)

Find highlight group name in the table and return its ID. If it doesn't exist yet, a new entry is created.

Parameters
ppHighlight group name
lenlength of pp
Returns
0 for failure else the id of the group
int syn_get_concealed_id ( win_T wp,
linenr_T  lnum,
colnr_T  col 
)

Get the sequence number of the concealed file position.

Returns
seqnr if the file position is concealed, 0 otherwise.
int syn_get_final_id ( int  hl_id)
int syn_get_foldlevel ( win_T wp,
long  lnum 
)
int syn_get_id ( win_T wp,
long  lnum,
colnr_T  col,
int  trans,
bool *  spellp,
int  keep_state 
)
int syn_get_stack_item ( int  i)
int syn_get_sub_char ( void  )
int syn_id2attr ( int  hl_id)

Translate a group ID to highlight attributes.

See also
syn_attr2entry
char_u* syn_id2name ( int  id)
void syn_init_cmdline_highlight ( bool  reset,
bool  init 
)

Create default links for Nvim* highlight groups used for cmdline coloring.

void syn_maybe_on ( void  )
int syn_name2attr ( const char_u name)

Lookup a highlight group name and return its attributes. Return zero if not found.

int syn_name2id ( const char_u name)

Lookup a highlight group name and return its ID.

Parameters
highlightname e.g. 'Cursor', 'Normal'
Returns
the highlight id, else 0 if name does not exist
int syn_namen2id ( const char_u linep,
int  len 
)
void syn_set_timeout ( proftime_T tm)
void syn_stack_apply_changes ( buf_T buf)
void syn_stack_free_all ( synblock_T block)
int syntax_check_changed ( linenr_T  lnum)
void syntax_clear ( synblock_T block)
void syntax_end_parsing ( linenr_T  lnum)
bool syntax_present ( win_T win)
void syntax_start ( win_T wp,
linenr_T  lnum 
)

Variable Documentation

color_name_table_T color_name_table[]
const char* const highlight_init_cmdline[]

Array of highlight definitions, used for unit testing.