]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - syntax/python.vim
3f739af28ac2febef1feefe44af2a332da1246f4
[config/dotfiles.git] / 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:  2008-09-29
6 " Filenames:    *.py
7 " Version:      2.6.3
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
22 "
23 " Options:
24 "
25 "    For set option do: let OPTION_NAME = 1
26 "    For clear option do: let OPTION_NAME = 0
27 "
28 " Option names:
29 "
30 "    For highlight builtin functions:
31 "       python_highlight_builtins
32 "
33 "    For highlight standard exceptions:
34 "       python_highlight_exceptions
35 "
36 "    For highlight string formatting:
37 "       python_highlight_string_formatting
38 "
39 "    For highlight str.format syntax:
40 "       python_highlight_string_format
41 "
42 "    For highlight string.Template syntax:
43 "       python_highlight_string_templates
44 "
45 "    For highlight indentation errors:
46 "       python_highlight_indent_errors
47 "
48 "    For highlight trailing spaces:
49 "       python_highlight_space_errors
50 "
51 "    For highlight doc-tests:
52 "       python_highlight_doctests
53 "
54 "    If you want all Python highlightings above:
55 "       python_highlight_all
56 "    (This option not override previously set options)
57 "
58 "    For fast machines:
59 "       python_slow_sync
60 "
61 "    For "print" builtin as function:
62 "       python_print_as_function
63
64 " For version 5.x: Clear all syntax items
65 " For version 6.x: Quit when a syntax file was already loaded
66 if version < 600
67   syntax clear
68 elseif exists("b:current_syntax")
69   finish
70 endif
71
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
76   endif
77   if !exists("python_highlight_exceptions")
78     let python_highlight_exceptions = 1
79   endif
80   if !exists("python_highlight_string_formatting")
81     let python_highlight_string_formatting = 1
82   endif
83   if !exists("python_highlight_string_format")
84     let python_highlight_string_format = 1
85   endif
86   if !exists("python_highlight_string_templates")
87     let python_highlight_string_templates = 1
88   endif
89   if !exists("python_highlight_indent_errors")
90     let python_highlight_indent_errors = 1
91   endif
92   if !exists("python_highlight_space_errors")
93     let python_highlight_space_errors = 1
94   endif
95   if !exists("python_highlight_doctests")
96     let python_highlight_doctests = 1
97   endif
98 endif
99
100 " Keywords
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
114
115 if !exists("python_print_as_function") || python_print_as_function == 0
116   syn keyword pythonStatement print
117 endif
118
119 " Decorators (new in Python 2.4)
120 syn match   pythonDecorator     "@" display nextgroup=pythonFunction skipwhite
121
122 " Comments
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
127
128 " Errors
129 syn match pythonError           "\<\d\+\D\+\>" display
130 syn match pythonError           "[$?]" display
131 syn match pythonError           "[&|]\{2,}" display
132 syn match pythonError           "[=]\{3,}" display
133
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
138 endif
139
140 " Trailing space errors
141 if exists("python_highlight_space_errors") && python_highlight_space_errors != 0
142   syn match pythonSpaceError    "\s\+$" display
143 endif
144
145 " Strings
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
150
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         "\\$"
157
158 " Unicode strings
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
163
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
170
171 " Raw strings
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
176
177 syn match pythonRawEscape       +\\['"]+ display transparent contained
178
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
184
185 syn match  pythonUniRawEscape           "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
186 syn match  pythonUniRawEscapeError      "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
187
188 if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
189   " String formatting
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
192 endif
193
194 if exists("python_highlight_string_format") && python_highlight_string_format != 0
195   " str.format syntax
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
198 endif
199
200 if exists("python_highlight_string_templates") && python_highlight_string_templates != 0
201   " String templates
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
205 endif
206
207 if exists("python_highlight_doctests") && python_highlight_doctests != 0
208   " DocTests
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
211 endif
212
213 " Numbers (ints, longs, floats, complex)
214 syn match   pythonHexError      "\<0[xX]\x*[g-zG-Z]\x*[lL]\=\>" display
215
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
219
220 syn match   pythonNumber        "\<\d\+[lLjJ]\=\>" display
221
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
225
226 syn match   pythonOctError      "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
227 syn match   pythonBinError      "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
228
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__
233
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
248
249   if exists("python_print_as_function") && python_print_as_function != 0
250       syn keyword pythonBuiltinFunc     print
251   endif
252 endif
253
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
259
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
272
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
277 endif
278
279 if exists("python_slow_sync") && python_slow_sync != 0
280   syn sync minlines=2000
281 else
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
287 endif
288
289 if version >= 508 || !exists("did_python_syn_inits")
290   if version <= 508
291     let did_python_syn_inits = 1
292     command -nargs=+ HiLink hi link <args>
293   else
294     command -nargs=+ HiLink hi def link <args>
295   endif
296
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
304
305   HiLink pythonDecorator        Define
306
307   HiLink pythonComment          Comment
308   HiLink pythonCoding           Special
309   HiLink pythonRun              Special
310   HiLink pythonTodo             Todo
311
312   HiLink pythonError            Error
313   HiLink pythonIndentError      Error
314   HiLink pythonSpaceError       Error
315
316   HiLink pythonString           String
317   HiLink pythonUniString        String
318   HiLink pythonRawString        String
319   HiLink pythonUniRawString     String
320
321   HiLink pythonEscape                   Special
322   HiLink pythonEscapeError              Error
323   HiLink pythonUniEscape                Special
324   HiLink pythonUniEscapeError           Error
325   HiLink pythonUniRawEscape             Special
326   HiLink pythonUniRawEscapeError        Error
327
328   HiLink pythonStrFormatting    Special
329   HiLink pythonStrFormat        Special
330   HiLink pythonStrTemplate          Special
331
332   HiLink pythonDocTest          Special
333   HiLink pythonDocTest2         Special
334
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
343
344   HiLink pythonBuiltinObj       Structure
345   HiLink pythonBuiltinFunc      Function
346
347   HiLink pythonExClass  Structure
348
349   delcommand HiLink
350 endif
351
352 let b:current_syntax = "python"