3 " Maintainer: Dmitry Vasiliev <dima@hlabs.spb.ru>
4 " URL: http://www.hlabs.spb.ru/vim/python.vim
5 " Last Change: 2008-09-29
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
25 " For set option do: let OPTION_NAME = 1
26 " For clear option do: let OPTION_NAME = 0
30 " For highlight builtin functions:
31 " python_highlight_builtins
33 " For highlight standard exceptions:
34 " python_highlight_exceptions
36 " For highlight string formatting:
37 " python_highlight_string_formatting
39 " For highlight str.format syntax:
40 " python_highlight_string_format
42 " For highlight string.Template syntax:
43 " python_highlight_string_templates
45 " For highlight indentation errors:
46 " python_highlight_indent_errors
48 " For highlight trailing spaces:
49 " python_highlight_space_errors
51 " For highlight doc-tests:
52 " python_highlight_doctests
54 " If you want all Python highlightings above:
55 " python_highlight_all
56 " (This option not override previously set options)
61 " For "print" builtin as function:
62 " python_print_as_function
64 " For version 5.x: Clear all syntax items
65 " For version 6.x: Quit when a syntax file was already loaded
68 elseif exists("b:current_syntax")
72 if exists("python_highlight_all") && python_highlight_all != 0
73 " Not override previously set options
74 if !exists("python_highlight_builtins")
75 let python_highlight_builtins = 1
77 if !exists("python_highlight_exceptions")
78 let python_highlight_exceptions = 1
80 if !exists("python_highlight_string_formatting")
81 let python_highlight_string_formatting = 1
83 if !exists("python_highlight_string_format")
84 let python_highlight_string_format = 1
86 if !exists("python_highlight_string_templates")
87 let python_highlight_string_templates = 1
89 if !exists("python_highlight_indent_errors")
90 let python_highlight_indent_errors = 1
92 if !exists("python_highlight_space_errors")
93 let python_highlight_space_errors = 1
95 if !exists("python_highlight_doctests")
96 let python_highlight_doctests = 1
101 syn keyword pythonStatement break continue del
102 syn keyword pythonStatement exec return
103 syn keyword pythonStatement pass raise
104 syn keyword pythonStatement global assert
105 syn keyword pythonStatement lambda yield
106 syn keyword pythonStatement with
107 syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
108 syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained
109 syn keyword pythonRepeat for while
110 syn keyword pythonConditional if elif else
111 syn keyword pythonImport import from as
112 syn keyword pythonException try except finally
113 syn keyword pythonOperator and in is not or
115 if !exists("python_print_as_function") || python_print_as_function == 0
116 syn keyword pythonStatement print
119 " Decorators (new in Python 2.4)
120 syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite
123 syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
124 syn match pythonRun "\%^#!.*$"
125 syn match pythonCoding "\%^.*\(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
126 syn keyword pythonTodo TODO FIXME XXX contained
129 syn match pythonError "\<\d\+\D\+\>" display
130 syn match pythonError "[$?]" display
131 syn match pythonError "[&|]\{2,}" display
132 syn match pythonError "[=]\{3,}" display
134 " TODO: Mixing spaces and tabs also may be used for pretty formatting multiline
135 " statements. For now I don't know how to work around this.
136 if exists("python_highlight_indent_errors") && python_highlight_indent_errors != 0
137 syn match pythonIndentError "^\s*\( \t\|\t \)\s*\S"me=e-1 display
140 " Trailing space errors
141 if exists("python_highlight_space_errors") && python_highlight_space_errors != 0
142 syn match pythonSpaceError "\s\+$" display
146 syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
147 syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
148 syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell
149 syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell
151 syn match pythonEscape +\\[abfnrtv'"\\]+ display contained
152 syn match pythonEscape "\\\o\o\=\o\=" display contained
153 syn match pythonEscapeError "\\\o\{,2}[89]" display contained
154 syn match pythonEscape "\\x\x\{2}" display contained
155 syn match pythonEscapeError "\\x\x\=\X" display contained
156 syn match pythonEscape "\\$"
159 syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
160 syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
161 syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
162 syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
164 syn match pythonUniEscape "\\u\x\{4}" display contained
165 syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
166 syn match pythonUniEscape "\\U\x\{8}" display contained
167 syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
168 syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
169 syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
172 syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
173 syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
174 syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
175 syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
177 syn match pythonRawEscape +\\['"]+ display transparent contained
179 " Unicode raw strings
180 syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
181 syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
182 syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell
183 syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell
185 syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
186 syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
188 if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
190 syn match pythonStrFormatting "%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
191 syn match pythonStrFormatting "%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
194 if exists("python_highlight_string_format") && python_highlight_string_format != 0
196 syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
197 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
200 if exists("python_highlight_string_templates") && python_highlight_string_templates != 0
202 syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
203 syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
204 syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
207 if exists("python_highlight_doctests") && python_highlight_doctests != 0
209 syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
210 syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
213 " Numbers (ints, longs, floats, complex)
214 syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*[lL]\=\>" display
216 syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display
217 syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
218 syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
220 syn match pythonNumber "\<\d\+[lLjJ]\=\>" display
222 syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display
223 syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
224 syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display
226 syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
227 syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
229 if exists("python_highlight_builtins") && python_highlight_builtins != 0
230 " Builtin functions, types and objects
231 syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented
232 syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
234 syn keyword pythonBuiltinFunc __import__ abs all any apply
235 syn keyword pythonBuiltinFunc basestring bin bool buffer bytearray bytes callable
236 syn keyword pythonBuiltinFunc chr classmethod cmp coerce compile complex
237 syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval
238 syn keyword pythonBuiltinFunc execfile file filter float format frozenset getattr
239 syn keyword pythonBuiltinFunc globals hasattr hash help hex id
240 syn keyword pythonBuiltinFunc input int intern isinstance
241 syn keyword pythonBuiltinFunc issubclass iter len list locals long map max
242 syn keyword pythonBuiltinFunc min next object oct open ord
243 syn keyword pythonBuiltinFunc pow property range
244 syn keyword pythonBuiltinFunc raw_input reduce reload repr
245 syn keyword pythonBuiltinFunc reversed round set setattr
246 syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple
247 syn keyword pythonBuiltinFunc type unichr unicode vars xrange zip
249 if exists("python_print_as_function") && python_print_as_function != 0
250 syn keyword pythonBuiltinFunc print
254 if exists("python_highlight_exceptions") && python_highlight_exceptions != 0
255 " Builtin exceptions and warnings
256 syn keyword pythonExClass BaseException
257 syn keyword pythonExClass Exception StandardError ArithmeticError
258 syn keyword pythonExClass LookupError EnvironmentError
260 syn keyword pythonExClass AssertionError AttributeError BufferError EOFError
261 syn keyword pythonExClass FloatingPointError GeneratorExit IOError
262 syn keyword pythonExClass ImportError IndexError KeyError
263 syn keyword pythonExClass KeyboardInterrupt MemoryError NameError
264 syn keyword pythonExClass NotImplementedError OSError OverflowError
265 syn keyword pythonExClass ReferenceError RuntimeError StopIteration
266 syn keyword pythonExClass SyntaxError IndentationError TabError
267 syn keyword pythonExClass SystemError SystemExit TypeError
268 syn keyword pythonExClass UnboundLocalError UnicodeError
269 syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError
270 syn keyword pythonExClass UnicodeTranslateError ValueError VMSError
271 syn keyword pythonExClass WindowsError ZeroDivisionError
273 syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning
274 syn keyword pythonExClass PendingDepricationWarning SyntaxWarning
275 syn keyword pythonExClass RuntimeWarning FutureWarning
276 syn keyword pythonExClass ImportWarning UnicodeWarning
279 if exists("python_slow_sync") && python_slow_sync != 0
280 syn sync minlines=2000
282 " This is fast but code inside triple quoted strings screws it up. It
283 " is impossible to fix because the only way to know if you are inside a
284 " triple quoted string is to start from the beginning of the file.
285 syn sync match pythonSync grouphere NONE "):$"
286 syn sync maxlines=200
289 if version >= 508 || !exists("did_python_syn_inits")
291 let did_python_syn_inits = 1
292 command -nargs=+ HiLink hi link <args>
294 command -nargs=+ HiLink hi def link <args>
297 HiLink pythonStatement Statement
298 HiLink pythonImport Statement
299 HiLink pythonFunction Function
300 HiLink pythonConditional Conditional
301 HiLink pythonRepeat Repeat
302 HiLink pythonException Exception
303 HiLink pythonOperator Operator
305 HiLink pythonDecorator Define
307 HiLink pythonComment Comment
308 HiLink pythonCoding Special
309 HiLink pythonRun Special
310 HiLink pythonTodo Todo
312 HiLink pythonError Error
313 HiLink pythonIndentError Error
314 HiLink pythonSpaceError Error
316 HiLink pythonString String
317 HiLink pythonUniString String
318 HiLink pythonRawString String
319 HiLink pythonUniRawString String
321 HiLink pythonEscape Special
322 HiLink pythonEscapeError Error
323 HiLink pythonUniEscape Special
324 HiLink pythonUniEscapeError Error
325 HiLink pythonUniRawEscape Special
326 HiLink pythonUniRawEscapeError Error
328 HiLink pythonStrFormatting Special
329 HiLink pythonStrFormat Special
330 HiLink pythonStrTemplate Special
332 HiLink pythonDocTest Special
333 HiLink pythonDocTest2 Special
335 HiLink pythonNumber Number
336 HiLink pythonHexNumber Number
337 HiLink pythonOctNumber Number
338 HiLink pythonBinNumber Number
339 HiLink pythonFloat Float
340 HiLink pythonOctError Error
341 HiLink pythonHexError Error
342 HiLink pythonBinError Error
344 HiLink pythonBuiltinObj Structure
345 HiLink pythonBuiltinFunc Function
347 HiLink pythonExClass Structure
352 let b:current_syntax = "python"