]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - vim/vim/syntax/python.vim
vim: syntax: update perl
[config/dotfiles.git] / vim / vim / syntax / python.vim
1 " Vim syntax file
2 " Language:     Python
3 " Maintainer:   Dmitry Vasiliev <dima@hlabs.spb.ru>
4 " URL:          http://www.hlabs.spb.ru/vim/python.vim
5 " Last Change:  2010-04-09
6 " Filenames:    *.py
7 " Version:      2.6.6
8 "
9 " Based on python.vim (from Vim 6.1 distribution)
10 " by Neil Schemenauer <nas@python.ca>
11 "
12 " Thanks:
13 "
14 "    Jeroen Ruigrok van der Werven
15 "        for the idea to highlight erroneous operators
16 "    Pedro Algarvio
17 "        for the patch to enable spell checking only for the right spots
18 "        (strings and comments)
19 "    John Eikenberry
20 "        for the patch fixing small typo
21 "    Caleb Adamantine
22 "        for the patch fixing highlighting for decorators
23 "    Andrea Riciputi
24 "        for the patch with new configuration options
25
26 "
27 " Options:
28 "
29 "    For set option do: let OPTION_NAME = 1
30 "    For clear option do: let OPTION_NAME = 0
31 "
32 " Option names:
33 "
34 "    For highlight builtin functions and objects:
35 "       python_highlight_builtins
36 "
37 "    For highlight builtin objects:
38 "       python_highlight_builtin_objs
39 "
40 "    For highlight builtin funtions:
41 "       python_highlight_builtin_funcs
42 "
43 "    For highlight standard exceptions:
44 "       python_highlight_exceptions
45 "
46 "    For highlight string formatting:
47 "       python_highlight_string_formatting
48 "
49 "    For highlight str.format syntax:
50 "       python_highlight_string_format
51 "
52 "    For highlight string.Template syntax:
53 "       python_highlight_string_templates
54 "
55 "    For highlight indentation errors:
56 "       python_highlight_indent_errors
57 "
58 "    For highlight trailing spaces:
59 "       python_highlight_space_errors
60 "
61 "    For highlight doc-tests:
62 "       python_highlight_doctests
63 "
64 "    If you want all Python highlightings above:
65 "       python_highlight_all
66 "    (This option not override previously set options)
67 "
68 "    For fast machines:
69 "       python_slow_sync
70 "
71 "    For "print" builtin as function:
72 "       python_print_as_function
73
74 " For version 5.x: Clear all syntax items
75 " For version 6.x: Quit when a syntax file was already loaded
76 if version < 600
77   syntax clear
78 elseif exists("b:current_syntax")
79   finish
80 endif
81
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
87     endif
88     if !exists("python_highlight_builtin_funcs")
89       let python_highlight_builtin_funcs = 1
90     endif
91   endif
92   if !exists("python_highlight_exceptions")
93     let python_highlight_exceptions = 1
94   endif
95   if !exists("python_highlight_string_formatting")
96     let python_highlight_string_formatting = 1
97   endif
98   if !exists("python_highlight_string_format")
99     let python_highlight_string_format = 1
100   endif
101   if !exists("python_highlight_string_templates")
102     let python_highlight_string_templates = 1
103   endif
104   if !exists("python_highlight_indent_errors")
105     let python_highlight_indent_errors = 1
106   endif
107   if !exists("python_highlight_space_errors")
108     let python_highlight_space_errors = 1
109   endif
110   if !exists("python_highlight_doctests")
111     let python_highlight_doctests = 1
112   endif
113 endif
114
115 " Keywords
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
129
130 if !exists("python_print_as_function") || python_print_as_function == 0
131   syn keyword pythonStatement print
132 endif
133
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
138
139 " Comments
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
144
145 " Errors
146 syn match pythonError           "\<\d\+\D\+\>" display
147 syn match pythonError           "[$?]" display
148 syn match pythonError           "[&|]\{2,}" display
149 syn match pythonError           "[=]\{3,}" display
150
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
155 endif
156
157 " Trailing space errors
158 if exists("python_highlight_space_errors") && python_highlight_space_errors != 0
159   syn match pythonSpaceError    "\s\+$" display
160 endif
161
162 " Strings
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
167
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         "\\$"
174
175 " Unicode strings
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
180
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
187
188 " Raw strings
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
193
194 syn match pythonRawEscape       +\\['"]+ display transparent contained
195
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
201
202 syn match  pythonUniRawEscape           "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
203 syn match  pythonUniRawEscapeError      "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
204
205 if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
206   " String formatting
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
209 endif
210
211 if exists("python_highlight_string_format") && python_highlight_string_format != 0
212   " str.format syntax
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
215 endif
216
217 if exists("python_highlight_string_templates") && python_highlight_string_templates != 0
218   " String templates
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
222 endif
223
224 if exists("python_highlight_doctests") && python_highlight_doctests != 0
225   " DocTests
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
228 endif
229
230 " Numbers (ints, longs, floats, complex)
231 syn match   pythonHexError      "\<0[xX]\x*[g-zG-Z]\x*[lL]\=\>" display
232
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
236
237 syn match   pythonNumber        "\<\d\+[lLjJ]\=\>" display
238
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
242
243 syn match   pythonOctError      "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
244 syn match   pythonBinError      "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
245
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__
250 endif
251
252 if exists("python_highlight_builtin_funcs") && python_highlight_builtin_funcs != 0
253   " Builtin functions
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
268
269   if exists("python_print_as_function") && python_print_as_function != 0
270       syn keyword pythonBuiltinFunc     print
271   endif
272 endif
273
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
279
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
292
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
297 endif
298
299 if exists("python_slow_sync") && python_slow_sync != 0
300   syn sync minlines=2000
301 else
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
307 endif
308
309 if version >= 508 || !exists("did_python_syn_inits")
310   if version <= 508
311     let did_python_syn_inits = 1
312     command -nargs=+ HiLink hi link <args>
313   else
314     command -nargs=+ HiLink hi def link <args>
315   endif
316
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
324
325   HiLink pythonDecorator        Define
326   HiLink pythonDottedName       Function
327   HiLink pythonDot          Normal
328
329   HiLink pythonComment          Comment
330   HiLink pythonCoding           Special
331   HiLink pythonRun              Special
332   HiLink pythonTodo             Todo
333
334   HiLink pythonError            Error
335   HiLink pythonIndentError      Error
336   HiLink pythonSpaceError       Error
337
338   HiLink pythonString           String
339   HiLink pythonUniString        String
340   HiLink pythonRawString        String
341   HiLink pythonUniRawString     String
342
343   HiLink pythonEscape                   Special
344   HiLink pythonEscapeError              Error
345   HiLink pythonUniEscape                Special
346   HiLink pythonUniEscapeError           Error
347   HiLink pythonUniRawEscape             Special
348   HiLink pythonUniRawEscapeError        Error
349
350   HiLink pythonStrFormatting    Special
351   HiLink pythonStrFormat        Special
352   HiLink pythonStrTemplate          Special
353
354   HiLink pythonDocTest          Special
355   HiLink pythonDocTest2         Special
356
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
365
366   HiLink pythonBuiltinObj       Structure
367   HiLink pythonBuiltinFunc      Function
368
369   HiLink pythonExClass  Structure
370
371   delcommand HiLink
372 endif
373
374 let b:current_syntax = "python"