3 " Maintainer: Dmitry Vasiliev <dima@hlabs.spb.ru>
4 " URL: http://www.hlabs.spb.ru/vim/python.vim
5 " Last Change: 2010-04-09
9 " Based on python.vim (from Vim 6.1 distribution)
10 " by Neil Schemenauer <nas@python.ca>
14 " Jeroen Ruigrok van der Werven
15 " for the idea to highlight erroneous operators
17 " for the patch to enable spell checking only for the right spots
18 " (strings and comments)
20 " for the patch fixing small typo
22 " for the patch fixing highlighting for decorators
24 " for the patch with new configuration options
29 " For set option do: let OPTION_NAME = 1
30 " For clear option do: let OPTION_NAME = 0
34 " For highlight builtin functions and objects:
35 " python_highlight_builtins
37 " For highlight builtin objects:
38 " python_highlight_builtin_objs
40 " For highlight builtin funtions:
41 " python_highlight_builtin_funcs
43 " For highlight standard exceptions:
44 " python_highlight_exceptions
46 " For highlight string formatting:
47 " python_highlight_string_formatting
49 " For highlight str.format syntax:
50 " python_highlight_string_format
52 " For highlight string.Template syntax:
53 " python_highlight_string_templates
55 " For highlight indentation errors:
56 " python_highlight_indent_errors
58 " For highlight trailing spaces:
59 " python_highlight_space_errors
61 " For highlight doc-tests:
62 " python_highlight_doctests
64 " If you want all Python highlightings above:
65 " python_highlight_all
66 " (This option not override previously set options)
71 " For "print" builtin as function:
72 " python_print_as_function
74 " For version 5.x: Clear all syntax items
75 " For version 6.x: Quit when a syntax file was already loaded
78 elseif exists("b:current_syntax")
82 if exists("python_highlight_all") && python_highlight_all != 0
83 " Not override previously set options
84 if !exists("python_highlight_builtins")
85 if !exists("python_highlight_builtin_objs")
86 let python_highlight_builtin_objs = 1
88 if !exists("python_highlight_builtin_funcs")
89 let python_highlight_builtin_funcs = 1
92 if !exists("python_highlight_exceptions")
93 let python_highlight_exceptions = 1
95 if !exists("python_highlight_string_formatting")
96 let python_highlight_string_formatting = 1
98 if !exists("python_highlight_string_format")
99 let python_highlight_string_format = 1
101 if !exists("python_highlight_string_templates")
102 let python_highlight_string_templates = 1
104 if !exists("python_highlight_indent_errors")
105 let python_highlight_indent_errors = 1
107 if !exists("python_highlight_space_errors")
108 let python_highlight_space_errors = 1
110 if !exists("python_highlight_doctests")
111 let python_highlight_doctests = 1
116 syn keyword pythonStatement break continue del
117 syn keyword pythonStatement exec return
118 syn keyword pythonStatement pass raise
119 syn keyword pythonStatement global assert
120 syn keyword pythonStatement lambda yield
121 syn keyword pythonStatement with
122 syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
123 syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained
124 syn keyword pythonRepeat for while
125 syn keyword pythonConditional if elif else
126 syn keyword pythonPreCondit import from as
127 syn keyword pythonException try except finally
128 syn keyword pythonOperator and in is not or
130 if !exists("python_print_as_function") || python_print_as_function == 0
131 syn keyword pythonStatement print
134 " Decorators (new in Python 2.4)
135 syn match pythonDecorator "@" display nextgroup=pythonDottedName skipwhite
136 syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
137 syn match pythonDot "\." display containedin=pythonDottedName
140 syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
141 syn match pythonRun "\%^#!.*$"
142 syn match pythonCoding "\%^.*\(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
143 syn keyword pythonTodo TODO FIXME XXX contained
146 syn match pythonError "\<\d\+\D\+\>" display
147 syn match pythonError "[$?]" display
148 syn match pythonError "[&|]\{2,}" display
149 syn match pythonError "[=]\{3,}" display
151 " TODO: Mixing spaces and tabs also may be used for pretty formatting multiline
152 " statements. For now I don't know how to work around this.
153 if exists("python_highlight_indent_errors") && python_highlight_indent_errors != 0
154 syn match pythonIndentError "^\s*\( \t\|\t \)\s*\S"me=e-1 display
157 " Trailing space errors
158 if exists("python_highlight_space_errors") && python_highlight_space_errors != 0
159 syn match pythonSpaceError "\s\+$" display
163 syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
164 syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
165 syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell
166 syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell
168 syn match pythonEscape +\\[abfnrtv'"\\]+ display contained
169 syn match pythonEscape "\\\o\o\=\o\=" display contained
170 syn match pythonEscapeError "\\\o\{,2}[89]" display contained
171 syn match pythonEscape "\\x\x\{2}" display contained
172 syn match pythonEscapeError "\\x\x\=\X" display contained
173 syn match pythonEscape "\\$"
176 syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
177 syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
178 syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
179 syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
181 syn match pythonUniEscape "\\u\x\{4}" display contained
182 syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
183 syn match pythonUniEscape "\\U\x\{8}" display contained
184 syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
185 syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
186 syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
189 syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
190 syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
191 syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
192 syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
194 syn match pythonRawEscape +\\['"]+ display transparent contained
196 " Unicode raw strings
197 syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
198 syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
199 syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell
200 syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell
202 syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
203 syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
205 if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
207 syn match pythonStrFormatting "%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
208 syn match pythonStrFormatting "%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
211 if exists("python_highlight_string_format") && python_highlight_string_format != 0
213 syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
214 syn match pythonStrFormat "{\([a-zA-Z_][a-zA-Z0-9_]*\|\d\+\)\(\.[a-zA-Z_][a-zA-Z0-9_]*\|\[\(\d\+\|[^!:\}]\+\)\]\)*\(![rs]\)\=\(:\({\([a-zA-Z_][a-zA-Z0-9_]*\|\d\+\)}\|\([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*\(\.\d\+\)\=[bcdeEfFgGnoxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
217 if exists("python_highlight_string_templates") && python_highlight_string_templates != 0
219 syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
220 syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
221 syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
224 if exists("python_highlight_doctests") && python_highlight_doctests != 0
226 syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
227 syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
230 " Numbers (ints, longs, floats, complex)
231 syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*[lL]\=\>" display
233 syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display
234 syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
235 syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
237 syn match pythonNumber "\<\d\+[lLjJ]\=\>" display
239 syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display
240 syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
241 syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display
243 syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
244 syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
246 if exists("python_highlight_builtin_objs") && python_highlight_builtin_objs != 0
247 " Builtin objects and types
248 syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented
249 syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
252 if exists("python_highlight_builtin_funcs") && python_highlight_builtin_funcs != 0
254 syn keyword pythonBuiltinFunc __import__ abs all any apply
255 syn keyword pythonBuiltinFunc basestring bin bool buffer bytearray bytes callable
256 syn keyword pythonBuiltinFunc chr classmethod cmp coerce compile complex
257 syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval
258 syn keyword pythonBuiltinFunc execfile file filter float format frozenset getattr
259 syn keyword pythonBuiltinFunc globals hasattr hash help hex id
260 syn keyword pythonBuiltinFunc input int intern isinstance
261 syn keyword pythonBuiltinFunc issubclass iter len list locals long map max
262 syn keyword pythonBuiltinFunc min next object oct open ord
263 syn keyword pythonBuiltinFunc pow property range
264 syn keyword pythonBuiltinFunc raw_input reduce reload repr
265 syn keyword pythonBuiltinFunc reversed round set setattr
266 syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple
267 syn keyword pythonBuiltinFunc type unichr unicode vars xrange zip
269 if exists("python_print_as_function") && python_print_as_function != 0
270 syn keyword pythonBuiltinFunc print
274 if exists("python_highlight_exceptions") && python_highlight_exceptions != 0
275 " Builtin exceptions and warnings
276 syn keyword pythonExClass BaseException
277 syn keyword pythonExClass Exception StandardError ArithmeticError
278 syn keyword pythonExClass LookupError EnvironmentError
280 syn keyword pythonExClass AssertionError AttributeError BufferError EOFError
281 syn keyword pythonExClass FloatingPointError GeneratorExit IOError
282 syn keyword pythonExClass ImportError IndexError KeyError
283 syn keyword pythonExClass KeyboardInterrupt MemoryError NameError
284 syn keyword pythonExClass NotImplementedError OSError OverflowError
285 syn keyword pythonExClass ReferenceError RuntimeError StopIteration
286 syn keyword pythonExClass SyntaxError IndentationError TabError
287 syn keyword pythonExClass SystemError SystemExit TypeError
288 syn keyword pythonExClass UnboundLocalError UnicodeError
289 syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError
290 syn keyword pythonExClass UnicodeTranslateError ValueError VMSError
291 syn keyword pythonExClass WindowsError ZeroDivisionError
293 syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning
294 syn keyword pythonExClass PendingDepricationWarning SyntaxWarning
295 syn keyword pythonExClass RuntimeWarning FutureWarning
296 syn keyword pythonExClass ImportWarning UnicodeWarning
299 if exists("python_slow_sync") && python_slow_sync != 0
300 syn sync minlines=2000
302 " This is fast but code inside triple quoted strings screws it up. It
303 " is impossible to fix because the only way to know if you are inside a
304 " triple quoted string is to start from the beginning of the file.
305 syn sync match pythonSync grouphere NONE "):$"
306 syn sync maxlines=200
309 if version >= 508 || !exists("did_python_syn_inits")
311 let did_python_syn_inits = 1
312 command -nargs=+ HiLink hi link <args>
314 command -nargs=+ HiLink hi def link <args>
317 HiLink pythonStatement Statement
318 HiLink pythonPreCondit Statement
319 HiLink pythonFunction Function
320 HiLink pythonConditional Conditional
321 HiLink pythonRepeat Repeat
322 HiLink pythonException Exception
323 HiLink pythonOperator Operator
325 HiLink pythonDecorator Define
326 HiLink pythonDottedName Function
327 HiLink pythonDot Normal
329 HiLink pythonComment Comment
330 HiLink pythonCoding Special
331 HiLink pythonRun Special
332 HiLink pythonTodo Todo
334 HiLink pythonError Error
335 HiLink pythonIndentError Error
336 HiLink pythonSpaceError Error
338 HiLink pythonString String
339 HiLink pythonUniString String
340 HiLink pythonRawString String
341 HiLink pythonUniRawString String
343 HiLink pythonEscape Special
344 HiLink pythonEscapeError Error
345 HiLink pythonUniEscape Special
346 HiLink pythonUniEscapeError Error
347 HiLink pythonUniRawEscape Special
348 HiLink pythonUniRawEscapeError Error
350 HiLink pythonStrFormatting Special
351 HiLink pythonStrFormat Special
352 HiLink pythonStrTemplate Special
354 HiLink pythonDocTest Special
355 HiLink pythonDocTest2 Special
357 HiLink pythonNumber Number
358 HiLink pythonHexNumber Number
359 HiLink pythonOctNumber Number
360 HiLink pythonBinNumber Number
361 HiLink pythonFloat Float
362 HiLink pythonOctError Error
363 HiLink pythonHexError Error
364 HiLink pythonBinError Error
366 HiLink pythonBuiltinObj Structure
367 HiLink pythonBuiltinFunc Function
369 HiLink pythonExClass Structure
374 let b:current_syntax = "python"