32061
|
1 " Vim syntax file
|
|
2 " Language: Python 2
|
|
3 " Maintainer: Zvezdan Petkovic <zpetkovic@acm.org>
|
|
4 " Last Change: 2016 Oct 29
|
|
5 " Credits: Neil Schemenauer <nas@python.ca>
|
|
6 " Dmitry Vasiliev
|
|
7 "
|
|
8 " This version is a major rewrite by Zvezdan Petkovic.
|
|
9 "
|
|
10 " - introduced highlighting of doctests
|
|
11 " - updated keywords, built-ins, and exceptions
|
|
12 " - corrected regular expressions for
|
|
13 "
|
|
14 " * functions
|
|
15 " * decorators
|
|
16 " * strings
|
|
17 " * escapes
|
|
18 " * numbers
|
|
19 " * space error
|
|
20 "
|
|
21 " - corrected synchronization
|
|
22 " - more highlighting is ON by default, except
|
|
23 " - space error highlighting is OFF by default
|
|
24 "
|
|
25 " Optional highlighting can be controlled using these variables.
|
|
26 "
|
|
27 " let python_no_builtin_highlight = 1
|
|
28 " let python_no_doctest_code_highlight = 1
|
|
29 " let python_no_doctest_highlight = 1
|
|
30 " let python_no_exception_highlight = 1
|
|
31 " let python_no_number_highlight = 1
|
|
32 " let python_space_error_highlight = 1
|
|
33 "
|
|
34 " All the options above can be switched on together.
|
|
35 "
|
|
36 " let python_highlight_all = 1
|
|
37 "
|
|
38 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|
39 " NOTE: This file is a copy of the last commit of runtime/syntax/python.vim
|
|
40 " that still supported Python 2. There is support for Python 3, up to 3.5,
|
|
41 " and it was kept in the file as is, because it supports the straddling code
|
|
42 " (Python 2 and 3 compatible) better.
|
|
43 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|
44
|
|
45 " quit when a syntax file was already loaded.
|
|
46 if exists("b:current_syntax")
|
|
47 finish
|
|
48 endif
|
|
49
|
|
50 " We need nocompatible mode in order to continue lines with backslashes.
|
|
51 " Original setting will be restored.
|
|
52 let s:cpo_save = &cpo
|
|
53 set cpo&vim
|
|
54
|
|
55 if exists("python_no_doctest_highlight")
|
|
56 let python_no_doctest_code_highlight = 1
|
|
57 endif
|
|
58
|
|
59 if exists("python_highlight_all")
|
|
60 if exists("python_no_builtin_highlight")
|
|
61 unlet python_no_builtin_highlight
|
|
62 endif
|
|
63 if exists("python_no_doctest_code_highlight")
|
|
64 unlet python_no_doctest_code_highlight
|
|
65 endif
|
|
66 if exists("python_no_doctest_highlight")
|
|
67 unlet python_no_doctest_highlight
|
|
68 endif
|
|
69 if exists("python_no_exception_highlight")
|
|
70 unlet python_no_exception_highlight
|
|
71 endif
|
|
72 if exists("python_no_number_highlight")
|
|
73 unlet python_no_number_highlight
|
|
74 endif
|
|
75 let python_space_error_highlight = 1
|
|
76 endif
|
|
77
|
|
78 " Keep Python keywords in alphabetical order inside groups for easy
|
|
79 " comparison with the table in the 'Python Language Reference'
|
|
80 " https://docs.python.org/2/reference/lexical_analysis.html#keywords,
|
|
81 " https://docs.python.org/3/reference/lexical_analysis.html#keywords.
|
|
82 " Groups are in the order presented in NAMING CONVENTIONS in syntax.txt.
|
|
83 " Exceptions come last at the end of each group (class and def below).
|
|
84 "
|
|
85 " Keywords 'with' and 'as' are new in Python 2.6
|
|
86 " (use 'from __future__ import with_statement' in Python 2.5).
|
|
87 "
|
|
88 " Some compromises had to be made to support both Python 3 and 2.
|
|
89 " We include Python 3 features, but when a definition is duplicated,
|
|
90 " the last definition takes precedence.
|
|
91 "
|
|
92 " - 'False', 'None', and 'True' are keywords in Python 3 but they are
|
|
93 " built-ins in 2 and will be highlighted as built-ins below.
|
|
94 " - 'exec' is a built-in in Python 3 and will be highlighted as
|
|
95 " built-in below.
|
|
96 " - 'nonlocal' is a keyword in Python 3 and will be highlighted.
|
|
97 " - 'print' is a built-in in Python 3 and will be highlighted as
|
|
98 " built-in below (use 'from __future__ import print_function' in 2)
|
|
99 " - async and await were added in Python 3.5 and are soft keywords.
|
|
100 "
|
|
101 syn keyword pythonStatement False None True
|
|
102 syn keyword pythonStatement as assert break continue del exec global
|
|
103 syn keyword pythonStatement lambda nonlocal pass print return with yield
|
|
104 syn keyword pythonStatement class def nextgroup=pythonFunction skipwhite
|
|
105 syn keyword pythonConditional elif else if
|
|
106 syn keyword pythonRepeat for while
|
|
107 syn keyword pythonOperator and in is not or
|
|
108 syn keyword pythonException except finally raise try
|
|
109 syn keyword pythonInclude from import
|
|
110 syn keyword pythonAsync async await
|
|
111
|
|
112 " Decorators (new in Python 2.4)
|
|
113 " A dot must be allowed because of @MyClass.myfunc decorators.
|
|
114 syn match pythonDecorator "@" display contained
|
|
115 syn match pythonDecoratorName "@\s*\h\%(\w\|\.\)*" display contains=pythonDecorator
|
|
116
|
|
117 " Python 3.5 introduced the use of the same symbol for matrix multiplication:
|
|
118 " https://www.python.org/dev/peps/pep-0465/. We now have to exclude the
|
|
119 " symbol from highlighting when used in that context.
|
|
120 " Single line multiplication.
|
|
121 syn match pythonMatrixMultiply
|
|
122 \ "\%(\w\|[])]\)\s*@"
|
|
123 \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue
|
|
124 \ transparent
|
|
125 " Multiplication continued on the next line after backslash.
|
|
126 syn match pythonMatrixMultiply
|
|
127 \ "[^\\]\\\s*\n\%(\s*\.\.\.\s\)\=\s\+@"
|
|
128 \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue
|
|
129 \ transparent
|
|
130 " Multiplication in a parenthesized expression over multiple lines with @ at
|
|
131 " the start of each continued line; very similar to decorators and complex.
|
|
132 syn match pythonMatrixMultiply
|
|
133 \ "^\s*\%(\%(>>>\|\.\.\.\)\s\+\)\=\zs\%(\h\|\%(\h\|[[(]\).\{-}\%(\w\|[])]\)\)\s*\n\%(\s*\.\.\.\s\)\=\s\+@\%(.\{-}\n\%(\s*\.\.\.\s\)\=\s\+@\)*"
|
|
134 \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue
|
|
135 \ transparent
|
|
136
|
|
137 syn match pythonFunction "\h\w*" display contained
|
|
138
|
|
139 syn match pythonComment "#.*$" contains=pythonTodo,@Spell
|
|
140 syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
|
|
141
|
|
142 " Triple-quoted strings can contain doctests.
|
|
143 syn region pythonString matchgroup=pythonQuotes
|
|
144 \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
|
|
145 \ contains=pythonEscape,@Spell
|
|
146 syn region pythonString matchgroup=pythonTripleQuotes
|
|
147 \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
|
|
148 \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
|
|
149 syn region pythonRawString matchgroup=pythonQuotes
|
|
150 \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
|
|
151 \ contains=@Spell
|
|
152 syn region pythonRawString matchgroup=pythonTripleQuotes
|
|
153 \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
|
|
154 \ contains=pythonSpaceError,pythonDoctest,@Spell
|
|
155
|
|
156 syn match pythonEscape +\\[abfnrtv'"\\]+ contained
|
|
157 syn match pythonEscape "\\\o\{1,3}" contained
|
|
158 syn match pythonEscape "\\x\x\{2}" contained
|
|
159 syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
|
|
160 " Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
|
|
161 syn match pythonEscape "\\N{\a\+\%(\s\a\+\)*}" contained
|
|
162 syn match pythonEscape "\\$"
|
|
163
|
|
164 " It is very important to understand all details before changing the
|
|
165 " regular expressions below or their order.
|
|
166 " The word boundaries are *not* the floating-point number boundaries
|
|
167 " because of a possible leading or trailing decimal point.
|
|
168 " The expressions below ensure that all valid number literals are
|
|
169 " highlighted, and invalid number literals are not. For example,
|
|
170 "
|
|
171 " - a decimal point in '4.' at the end of a line is highlighted,
|
|
172 " - a second dot in 1.0.0 is not highlighted,
|
|
173 " - 08 is not highlighted,
|
|
174 " - 08e0 or 08j are highlighted,
|
|
175 "
|
|
176 " and so on, as specified in the 'Python Language Reference'.
|
|
177 " https://docs.python.org/2/reference/lexical_analysis.html#numeric-literals
|
|
178 " https://docs.python.org/3/reference/lexical_analysis.html#numeric-literals
|
|
179 if !exists("python_no_number_highlight")
|
|
180 " numbers (including longs and complex)
|
|
181 syn match pythonNumber "\<0[oO]\=\o\+[Ll]\=\>"
|
|
182 syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>"
|
|
183 syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>"
|
|
184 syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>"
|
|
185 syn match pythonNumber "\<\d\+[jJ]\>"
|
|
186 syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>"
|
|
187 syn match pythonNumber
|
|
188 \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="
|
|
189 syn match pythonNumber
|
|
190 \ "\%(^\|\W\)\zs\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"
|
|
191 endif
|
|
192
|
|
193 " Group the built-ins in the order in the 'Python Library Reference' for
|
|
194 " easier comparison.
|
|
195 " https://docs.python.org/2/library/constants.html
|
|
196 " https://docs.python.org/3/library/constants.html
|
|
197 " http://docs.python.org/2/library/functions.html
|
|
198 " http://docs.python.org/3/library/functions.html
|
|
199 " http://docs.python.org/2/library/functions.html#non-essential-built-in-functions
|
|
200 " http://docs.python.org/3/library/functions.html#non-essential-built-in-functions
|
|
201 " Python built-in functions are in alphabetical order.
|
|
202 if !exists("python_no_builtin_highlight")
|
|
203 " built-in constants
|
|
204 " 'False', 'True', and 'None' are also reserved words in Python 3
|
|
205 syn keyword pythonBuiltin False True None
|
|
206 syn keyword pythonBuiltin NotImplemented Ellipsis __debug__
|
|
207 " built-in functions
|
|
208 syn keyword pythonBuiltin abs all any bin bool bytearray callable chr
|
|
209 syn keyword pythonBuiltin classmethod compile complex delattr dict dir
|
|
210 syn keyword pythonBuiltin divmod enumerate eval filter float format
|
|
211 syn keyword pythonBuiltin frozenset getattr globals hasattr hash
|
|
212 syn keyword pythonBuiltin help hex id input int isinstance
|
|
213 syn keyword pythonBuiltin issubclass iter len list locals map max
|
|
214 syn keyword pythonBuiltin memoryview min next object oct open ord pow
|
|
215 syn keyword pythonBuiltin print property range repr reversed round set
|
|
216 syn keyword pythonBuiltin setattr slice sorted staticmethod str
|
|
217 syn keyword pythonBuiltin sum super tuple type vars zip __import__
|
|
218 " Python 2 only
|
|
219 syn keyword pythonBuiltin basestring cmp execfile file
|
|
220 syn keyword pythonBuiltin long raw_input reduce reload unichr
|
|
221 syn keyword pythonBuiltin unicode xrange
|
|
222 " Python 3 only
|
|
223 syn keyword pythonBuiltin ascii bytes exec
|
|
224 " non-essential built-in functions; Python 2 only
|
|
225 syn keyword pythonBuiltin apply buffer coerce intern
|
|
226 " avoid highlighting attributes as builtins
|
|
227 syn match pythonAttribute /\.\h\w*/hs=s+1
|
|
228 \ contains=ALLBUT,pythonBuiltin,pythonFunction,pythonAsync
|
|
229 \ transparent
|
|
230 endif
|
|
231
|
|
232 " From the 'Python Library Reference' class hierarchy at the bottom.
|
|
233 " http://docs.python.org/2/library/exceptions.html
|
|
234 " http://docs.python.org/3/library/exceptions.html
|
|
235 if !exists("python_no_exception_highlight")
|
|
236 " builtin base exceptions (used mostly as base classes for other exceptions)
|
|
237 syn keyword pythonExceptions BaseException Exception
|
|
238 syn keyword pythonExceptions ArithmeticError BufferError
|
|
239 syn keyword pythonExceptions LookupError
|
|
240 " builtin base exceptions removed in Python 3
|
|
241 syn keyword pythonExceptions EnvironmentError StandardError
|
|
242 " builtin exceptions (actually raised)
|
|
243 syn keyword pythonExceptions AssertionError AttributeError
|
|
244 syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit
|
|
245 syn keyword pythonExceptions ImportError IndentationError
|
|
246 syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt
|
|
247 syn keyword pythonExceptions MemoryError NameError NotImplementedError
|
|
248 syn keyword pythonExceptions OSError OverflowError ReferenceError
|
|
249 syn keyword pythonExceptions RuntimeError StopIteration SyntaxError
|
|
250 syn keyword pythonExceptions SystemError SystemExit TabError TypeError
|
|
251 syn keyword pythonExceptions UnboundLocalError UnicodeError
|
|
252 syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError
|
|
253 syn keyword pythonExceptions UnicodeTranslateError ValueError
|
|
254 syn keyword pythonExceptions ZeroDivisionError
|
|
255 " builtin OS exceptions in Python 3
|
|
256 syn keyword pythonExceptions BlockingIOError BrokenPipeError
|
|
257 syn keyword pythonExceptions ChildProcessError ConnectionAbortedError
|
|
258 syn keyword pythonExceptions ConnectionError ConnectionRefusedError
|
|
259 syn keyword pythonExceptions ConnectionResetError FileExistsError
|
|
260 syn keyword pythonExceptions FileNotFoundError InterruptedError
|
|
261 syn keyword pythonExceptions IsADirectoryError NotADirectoryError
|
|
262 syn keyword pythonExceptions PermissionError ProcessLookupError
|
|
263 syn keyword pythonExceptions RecursionError StopAsyncIteration
|
|
264 syn keyword pythonExceptions TimeoutError
|
|
265 " builtin exceptions deprecated/removed in Python 3
|
|
266 syn keyword pythonExceptions IOError VMSError WindowsError
|
|
267 " builtin warnings
|
|
268 syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning
|
|
269 syn keyword pythonExceptions ImportWarning PendingDeprecationWarning
|
|
270 syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning
|
|
271 syn keyword pythonExceptions UserWarning Warning
|
|
272 " builtin warnings in Python 3
|
|
273 syn keyword pythonExceptions ResourceWarning
|
|
274 endif
|
|
275
|
|
276 if exists("python_space_error_highlight")
|
|
277 " trailing whitespace
|
|
278 syn match pythonSpaceError display excludenl "\s\+$"
|
|
279 " mixed tabs and spaces
|
|
280 syn match pythonSpaceError display " \+\t"
|
|
281 syn match pythonSpaceError display "\t\+ "
|
|
282 endif
|
|
283
|
|
284 " Do not spell doctests inside strings.
|
|
285 " Notice that the end of a string, either ''', or """, will end the contained
|
|
286 " doctest too. Thus, we do *not* need to have it as an end pattern.
|
|
287 if !exists("python_no_doctest_highlight")
|
|
288 if !exists("python_no_doctest_code_highlight")
|
|
289 syn region pythonDoctest
|
|
290 \ start="^\s*>>>\s" end="^\s*$"
|
|
291 \ contained contains=ALLBUT,pythonDoctest,pythonFunction,@Spell
|
|
292 syn region pythonDoctestValue
|
|
293 \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
|
|
294 \ contained
|
|
295 else
|
|
296 syn region pythonDoctest
|
|
297 \ start="^\s*>>>" end="^\s*$"
|
|
298 \ contained contains=@NoSpell
|
|
299 endif
|
|
300 endif
|
|
301
|
|
302 " Sync at the beginning of class, function, or method definition.
|
|
303 syn sync match pythonSync grouphere NONE "^\%(def\|class\)\s\+\h\w*\s*[(:]"
|
|
304
|
|
305 " The default highlight links. Can be overridden later.
|
|
306 hi def link pythonStatement Statement
|
|
307 hi def link pythonConditional Conditional
|
|
308 hi def link pythonRepeat Repeat
|
|
309 hi def link pythonOperator Operator
|
|
310 hi def link pythonException Exception
|
|
311 hi def link pythonInclude Include
|
|
312 hi def link pythonAsync Statement
|
|
313 hi def link pythonDecorator Define
|
|
314 hi def link pythonDecoratorName Function
|
|
315 hi def link pythonFunction Function
|
|
316 hi def link pythonComment Comment
|
|
317 hi def link pythonTodo Todo
|
|
318 hi def link pythonString String
|
|
319 hi def link pythonRawString String
|
|
320 hi def link pythonQuotes String
|
|
321 hi def link pythonTripleQuotes pythonQuotes
|
|
322 hi def link pythonEscape Special
|
|
323 if !exists("python_no_number_highlight")
|
|
324 hi def link pythonNumber Number
|
|
325 endif
|
|
326 if !exists("python_no_builtin_highlight")
|
|
327 hi def link pythonBuiltin Function
|
|
328 endif
|
|
329 if !exists("python_no_exception_highlight")
|
|
330 hi def link pythonExceptions Structure
|
|
331 endif
|
|
332 if exists("python_space_error_highlight")
|
|
333 hi def link pythonSpaceError Error
|
|
334 endif
|
|
335 if !exists("python_no_doctest_highlight")
|
|
336 hi def link pythonDoctest Special
|
|
337 hi def link pythonDoctestValue Define
|
|
338 endif
|
|
339
|
|
340 let b:current_syntax = "python"
|
|
341
|
|
342 let &cpo = s:cpo_save
|
|
343 unlet s:cpo_save
|
|
344
|
|
345 " vim:set sw=2 sts=2 ts=8 noet:
|