3 " Maintainer: Bram Moolenaar <Bram@vim.org>
4 " Last Change: 2001 May 17
6 " For version 5.x: Clear all syntax items
7 " For version 6.x: Quit when a syntax file was already loaded
10 elseif exists("b:current_syntax")
14 " A bunch of useful C keywords
15 syn keyword cStatement goto break return continue asm
16 syn keyword cLabel case default
17 syn keyword cConditional if else switch
18 syn keyword cRepeat while for do
20 syn keyword cTodo contained TODO FIXME XXX
22 " cCommentGroup allows adding matches for special things in comments
23 syn cluster cCommentGroup contains=cTodo
25 " String and Character constants
26 " Highlight special characters (those which have a backslash) differently
27 syn match cSpecial display contained "\\\(x\x\+\|\o\{1,3}\|.\|$\)"
28 if !exists("c_no_utf")
29 syn match cSpecial display contained "\\\(u\x\{4}\|U\x\{8}\)"
31 if exists("c_no_cformat")
32 syn region cString start=+L\="+ skip=+\\\\\|\\"+ end=+"+ contains=cSpecial
33 " cCppString: same as cString, but ends at end of line
34 syn region cCppString start=+L\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end='$' contains=cSpecial
36 syn match cFormat display "%\(\d\+\$\)\=[-+' #0*]*\(\d*\|\*\|\*\d\+\$\)\(\.\(\d*\|\*\|\*\d\+\$\)\)\=\([hlL]\|ll\)\=\([diuoxXfeEgGcCsSpn]\|\[\^\=.[^]]*\]\)" contained
37 syn match cFormat display "%%" contained
38 syn region cString start=+L\="+ skip=+\\\\\|\\"+ end=+"+ contains=cSpecial,cFormat
39 " cCppString: same as cString, but ends at end of line
40 syn region cCppString start=+L\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end='$' contains=cSpecial,cFormat
43 syn match cCharacter "L\='[^\\]'"
44 syn match cCharacter "L'[^']*'" contains=cSpecial
46 syn match cSpecialError "L\='\\[^'\"?\\abefnrtv]'"
47 syn match cSpecialCharacter "L\='\\['\"?\\abefnrtv]'"
49 syn match cSpecialError "L\='\\[^'\"?\\abfnrtv]'"
50 syn match cSpecialCharacter "L\='\\['\"?\\abfnrtv]'"
52 syn match cSpecialCharacter display "L\='\\\o\{1,3}'"
53 syn match cSpecialCharacter display "'\\x\x\{1,2}'"
54 syn match cSpecialCharacter display "L'\\x\x\+'"
56 "when wanted, highlight trailing white space
57 if exists("c_space_errors")
58 if !exists("c_no_trail_space_error")
59 syn match cSpaceError display excludenl "\s\+$"
61 if !exists("c_no_tab_space_error")
62 syn match cSpaceError display " \+\t"me=e-1
66 "catch errors caused by wrong parenthesis and brackets
67 syn cluster cParenGroup contains=cParenError,cIncluded,cSpecial,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cUserCont,cUserLabel,cBitField,cCommentSkip,cOctalZero,cCppOut,cCppOut2,cCppSkip,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom
68 if exists("c_no_bracket_error")
69 syn region cParen transparent start='(' end=')' contains=ALLBUT,@cParenGroup,cCppParen,cCppString
70 " cCppParen: same as cParen but ends at end-of-line; used in cDefine
71 syn region cCppParen transparent start='(' skip='\\$' excludenl end=')' end='$' contained contains=ALLBUT,@cParenGroup,cParen,cString
72 syn match cParenError display ")"
73 syn match cErrInParen display contained "[{}]"
75 syn region cParen transparent start='(' end=')' contains=ALLBUT,@cParenGroup,cCppParen,cErrInBracket,cCppBracket,cCppString
76 " cCppParen: same as cParen but ends at end-of-line; used in cDefine
77 syn region cCppParen transparent start='(' skip='\\$' excludenl end=')' end='$' contained contains=ALLBUT,@cParenGroup,cErrInBracket,cParen,cBracket,cString
78 syn match cParenError display "[\])]"
79 syn match cErrInParen display contained "[\]{}]"
80 syn region cBracket transparent start='\[' end=']' contains=ALLBUT,@cParenGroup,cErrInParen,cCppParen,cCppBracket,cCppString
81 " cCppBracket: same as cParen but ends at end-of-line; used in cDefine
82 syn region cCppBracket transparent start='\[' skip='\\$' excludenl end=']' end='$' contained contains=ALLBUT,@cParenGroup,cErrInParen,cParen,cBracket,cString
83 syn match cErrInBracket display contained "[);{}]"
86 "integer number, or floating point number without a dot and with "f".
88 syn match cNumbers display transparent "\<\d\|\.\d" contains=cNumber,cFloat,cOctalError,cOctal
89 " Same, but without octal error (for comments)
90 syn match cNumbersCom display contained transparent "\<\d\|\.\d" contains=cNumber,cFloat,cOctal
91 syn match cNumber display contained "\d\+\(u\=l\{0,2}\|ll\=u\)\>"
93 syn match cNumber display contained "0x\x\+\(u\=l\{0,2}\|ll\=u\)\>"
94 " Flag the first zero of an octal number as something special
95 syn match cOctal display contained "0\o\+\(u\=l\{0,2}\|ll\=u\)\>" contains=cOctalZero
96 syn match cOctalZero display contained "\<0"
97 syn match cFloat display contained "\d\+f"
98 "floating point number, with dot, optional exponent
99 syn match cFloat display contained "\d\+\.\d*\(e[-+]\=\d\+\)\=[fl]\="
100 "floating point number, starting with a dot, optional exponent
101 syn match cFloat display contained "\.\d\+\(e[-+]\=\d\+\)\=[fl]\=\>"
102 "floating point number, without dot, with exponent
103 syn match cFloat display contained "\d\+e[-+]\=\d\+[fl]\=\>"
104 " flag an octal number with wrong digits
105 syn match cOctalError display contained "0\o*[89]\d*"
108 if exists("c_comment_strings")
109 " A comment can contain cString, cCharacter and cNumber.
110 " But a "*/" inside a cString in a cComment DOES end the comment! So we
111 " need to use a special type of cString: cCommentString, which also ends on
112 " "*/", and sees a "*" at the start of the line as comment again.
113 " Unfortunately this doesn't very well work for // type of comments :-(
114 syntax match cCommentSkip contained "^\s*\*\($\|\s\+\)"
115 syntax region cCommentString contained start=+L\="+ skip=+\\\\\|\\"+ end=+"+ end=+\*/+me=s-1 contains=cSpecial,cCommentSkip
116 syntax region cComment2String contained start=+L\="+ skip=+\\\\\|\\"+ end=+"+ end="$" contains=cSpecial
117 syntax region cCommentL start="//" skip="\\$" end="$" keepend contains=@cCommentGroup,cComment2String,cCharacter,cNumbersCom,cSpaceError
118 syntax region cComment matchgroup=cCommentStart start="/\*" matchgroup=NONE end="\*/" contains=@cCommentGroup,cCommentStartError,cCommentString,cCharacter,cNumbersCom,cSpaceError
120 syn region cCommentL start="//" skip="\\$" end="$" keepend contains=@cCommentGroup,cSpaceError
121 syn region cComment matchgroup=cCommentStart start="/\*" matchgroup=NONE end="\*/" contains=@cCommentGroup,cCommentStartError,cSpaceError
123 " keep a // comment separately, it terminates a preproc. conditional
124 syntax match cCommentError display "\*/"
125 syntax match cCommentStartError display "/\*"me=e-1 contained
127 syn keyword cOperator sizeof
129 syn keyword cOperator typeof __real__ __imag__
131 syn keyword cType int long short char void
132 syn keyword cType signed unsigned float double
133 if !exists("c_no_ansi") || exists("c_ansi_typedefs")
134 syn keyword cType size_t wchar_t ptrdiff_t sig_atomic_t fpos_t
135 syn keyword cType clock_t time_t va_list jmp_buf FILE DIR div_t ldiv_t
138 syn keyword cType __label__ __complex__
141 syn keyword cStructure struct union enum typedef
142 syn keyword cStorageClass static register auto volatile extern const
144 syn keyword cStorageClass inline __attribute__
147 if !exists("c_no_ansi") || exists("c_ansi_constants") || exists("c_gnu")
149 syn keyword cConstant __GNUC__ __FUNCTION__ __PRETTY_FUNCTION__
151 syn keyword cConstant __LINE__ __FILE__ __DATE__ __TIME__ __STDC__
152 syn keyword cConstant __STDC_VERSION__
153 syn keyword cConstant CHAR_BIT MB_LEN_MAX MB_CUR_MAX
154 syn keyword cConstant UCHAR_MAX UINT_MAX ULONG_MAX USHRT_MAX
155 syn keyword cConstant CHAR_MIN INT_MIN LONG_MIN SHRT_MIN
156 syn keyword cConstant CHAR_MAX INT_MAX LONG_MAX SHRT_MAX
157 syn keyword cConstant SCHAR_MIN SINT_MIN SLONG_MIN SSHRT_MIN
158 syn keyword cConstant SCHAR_MAX SINT_MAX SLONG_MAX SSHRT_MAX
159 syn keyword cConstant FLT_RADIX FLT_ROUNDS
160 syn keyword cConstant FLT_DIG FLT_MANT_DIG FLT_EPSILON
161 syn keyword cConstant DBL_DIG DBL_MANT_DIG DBL_EPSILON
162 syn keyword cConstant LDBL_DIG LDBL_MANT_DIG LDBL_EPSILON
163 syn keyword cConstant FLT_MIN FLT_MAX FLT_MIN_EXP FLT_MAX_EXP
164 syn keyword cConstant FLT_MIN_10_EXP FLT_MAX_10_EXP
165 syn keyword cConstant DBL_MIN DBL_MAX DBL_MIN_EXP DBL_MAX_EXP
166 syn keyword cConstant DBL_MIN_10_EXP DBL_MAX_10_EXP
167 syn keyword cConstant LDBL_MIN LDBL_MAX LDBL_MIN_EXP LDBL_MAX_EXP
168 syn keyword cConstant LDBL_MIN_10_EXP LDBL_MAX_10_EXP
169 syn keyword cConstant HUGE_VAL EDOM ERANGE CLOCKS_PER_SEC NULL
170 syn keyword cConstant LC_ALL LC_COLLATE LC_CTYPE LC_MONETARY
171 syn keyword cConstant LC_NUMERIC LC_TIME
172 syn keyword cConstant SIG_DFL SIG_ERR SIG_IGN
173 syn keyword cConstant SIGABRT SIGFPE SIGILL SIGHUP SIGINT SIGSEGV SIGTERM
174 " Add POSIX signals as well...
175 syn keyword cConstant SIGQUIT SIGTRAP SIGKILL SIGUSR1 SIGUSR2 SIGPIPE
176 syn keyword cConstant SIGALRM SIGCHLD SIGCONT SIGSTOP SIGTSTP SIGTTIN SIGTTOU
177 syn keyword cConstant _IOFBF _IOLBF _IONBF BUFSIZ EOF
178 syn keyword cConstant FOPEN_MAX FILENAME_MAX L_tmpnam
179 syn keyword cConstant SEEK_CUR SEEK_END SEEK_SET
180 syn keyword cConstant TMP_MAX stderr stdin stdout
181 syn keyword cConstant EXIT_FAILURE EXIT_SUCCESS RAND_MAX
183 syn keyword cConstant M_E M_LOG2E M_LOG10E M_LN2 M_LN10 M_PI M_PI_2 M_PI_4
184 syn keyword cConstant M_1_PI M_2_PI M_2_SQRTPI M_SQRT2 M_SQRT1_2
187 syn region cPreCondit start="^\s*#\s*\(if\|ifdef\|ifndef\|elif\)\>" skip="\\$" end="$" end="//"me=s-1 contains=cComment,cCppString,cCharacter,cCppParen,cParenError,cNumbers,cCommentError,cSpaceError
188 syn match cPreCondit display "^\s*#\s*\(else\|endif\)\>"
189 if !exists("c_no_if0")
190 syn region cCppOut start="^\s*#\s*if\s\+0\+\>" end=".\|$" contains=cCppOut2
191 syn region cCppOut2 contained start="0" end="^\s*#\s*\(endif\>\|else\>\|elif\>\)" contains=cSpaceError,cCppSkip
192 syn region cCppSkip contained start="^\s*#\s*\(if\>\|ifdef\>\|ifndef\>\)" skip="\\$" end="^\s*#\s*endif\>" contains=cSpaceError,cCppSkip
194 syn region cIncluded display contained start=+"+ skip=+\\\\\|\\"+ end=+"+
195 syn match cIncluded display contained "<[^>]*>"
196 syn match cInclude display "^\s*#\s*include\>\s*["<]" contains=cIncluded
197 "syn match cLineSkip "\\$"
198 syn cluster cPreProcGroup contains=cPreCondit,cIncluded,cInclude,cDefine,cErrInParen,cErrInBracket,cUserLabel,cSpecial,cOctalZero,cCppOut,cCppOut2,cCppSkip,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom,cString,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cParen,cBracket,cMulti
199 syn region cDefine start="^\s*#\s*\(define\|undef\)\>" skip="\\$" end="$" end="//"me=s-1 contains=ALLBUT,@cPreProcGroup
200 syn region cPreProc start="^\s*#\s*\(pragma\>\|line\>\|warning\>\|warn\>\|error\>\)" skip="\\$" end="$" keepend contains=ALLBUT,@cPreProcGroup
202 " Highlight User Labels
203 syn cluster cMultiGroup contains=cIncluded,cSpecial,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cUserCont,cUserLabel,cBitField,cOctalZero,cCppOut,cCppOut2,cCppSkip,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom,cCppParen,cCppBracket,cCppString
204 syn region cMulti transparent start='?' skip='::' end=':' contains=ALLBUT,@cMultiGroup
205 " Avoid matching foo::bar() in C++ by requiring that the next char is not ':'
206 syn cluster cLabelGroup contains=cUserLabel
207 syn match cUserCont display "^\s*\I\i*\s*:$" contains=@cLabelGroup
208 syn match cUserCont display ";\s*\I\i*\s*:$" contains=@cLabelGroup
209 syn match cUserCont display "^\s*\I\i*\s*:[^:]"me=e-1 contains=@cLabelGroup
210 syn match cUserCont display ";\s*\I\i*\s*:[^:]"me=e-1 contains=@cLabelGroup
212 syn match cUserLabel display "\I\i*" contained
214 " Avoid recognizing most bitfields as labels
215 syn match cBitField display "^\s*\I\i*\s*:\s*[1-9]"me=e-1
216 syn match cBitField display ";\s*\I\i*\s*:\s*[1-9]"me=e-1
218 if exists("c_minlines")
219 let b:c_minlines = c_minlines
221 if !exists("c_no_if0")
222 let b:c_minlines = 50 " #if 0 constructs can be long
224 let b:c_minlines = 15 " mostly for () constructs
227 exec "syn sync ccomment cComment minlines=" . b:c_minlines
229 " Define the default highlighting.
230 " For version 5.7 and earlier: only when not done already
231 " For version 5.8 and later: only when an item doesn't have highlighting yet
232 if version >= 508 || !exists("did_c_syn_inits")
234 let did_c_syn_inits = 1
235 command -nargs=+ HiLink hi link <args>
237 command -nargs=+ HiLink hi def link <args>
240 HiLink cFormat cSpecial
241 HiLink cCppString cString
242 HiLink cCommentL cComment
243 HiLink cCommentStart cComment
245 HiLink cUserLabel Label
246 HiLink cConditional Conditional
247 HiLink cRepeat Repeat
248 HiLink cCharacter Character
249 HiLink cSpecialCharacter cSpecial
250 HiLink cNumber Number
252 HiLink cOctalZero PreProc " link this to Error if you want
254 HiLink cOctalError cError
255 HiLink cParenError cError
256 HiLink cErrInParen cError
257 HiLink cErrInBracket cError
258 HiLink cCommentError cError
259 HiLink cCommentStartError cError
260 HiLink cSpaceError cError
261 HiLink cSpecialError cError
262 HiLink cOperator Operator
263 HiLink cStructure Structure
264 HiLink cStorageClass StorageClass
265 HiLink cInclude Include
266 HiLink cPreProc PreProc
268 HiLink cIncluded cString
270 HiLink cStatement Statement
271 HiLink cPreCondit PreCondit
273 HiLink cConstant Constant
274 HiLink cCommentString cString
275 HiLink cComment2String cString
276 HiLink cCommentSkip cComment
277 HiLink cString String
278 HiLink cComment Comment
279 HiLink cSpecial SpecialChar
281 HiLink cCppSkip cCppOut
282 HiLink cCppOut2 cCppOut
283 HiLink cCppOut Comment
288 let b:current_syntax = "c"