Browse Source

Update the python.vim to Vim's 7.4 source

Pedro Algarvio 9 years ago
parent
commit
295f944f67
1 changed files with 250 additions and 499 deletions
  1. 250
    499
      syntax/python.vim

+ 250
- 499
syntax/python.vim View File

@@ -1,475 +1,257 @@
1 1
 " Vim syntax file
2
-" Language:     Python
3
-" Maintainer:   Dmitry Vasiliev <dima at hlabs dot org>
4
-" URL:          https://github.com/hdima/python-syntax
5
-" Last Change:  2013-11-18
6
-" Filenames:    *.py
7
-" Version:      3.3.6
2
+" Language:	Python
3
+" Maintainer:	Neil Schemenauer <nas@python.ca>
4
+" Last Change:	2014 Jul 16
5
+" Credits:	Zvezdan Petkovic <zpetkovic@acm.org>
6
+"		Neil Schemenauer <nas@python.ca>
7
+"		Dmitry Vasiliev
8 8
 "
9
-" Based on python.vim (from Vim 6.1 distribution)
10
-" by Neil Schemenauer <nas at python dot ca>
9
+"		This version is a major rewrite by Zvezdan Petkovic.
11 10
 "
12
-" Please use the following channels for reporting bugs, offering suggestions or
13
-" feedback:
14
-
15
-" - python.vim issue tracker: https://github.com/hdima/python-syntax/issues
16
-" - Email: Dmitry Vasiliev (dima at hlabs.org)
17
-" - Send a message or follow me for updates on Twitter: `@hdima
18
-"   <https://twitter.com/hdima>`__
19
-"
20
-" Contributors
21
-" ============
22
-"
23
-" List of the contributors in alphabetical order:
24
-"
25
-"   Andrea Riciputi
26
-"   Anton Butanaev
27
-"   Caleb Adamantine
28
-"   Elizabeth Myers
29
-"   Jeroen Ruigrok van der Werven
30
-"   John Eikenberry
31
-"   Marc Weber
32
-"   Pedro Algarvio
33
-"   pydave at GitHub
34
-"   Will Gray
35
-"   Yuri Habrusiev
36
-"
37
-" Options
38
-" =======
39
-"
40
-"    :let OPTION_NAME = 1                   Enable option
41
-"    :let OPTION_NAME = 0                   Disable option
42
-"
43
-"
44
-" Option to select Python version
45
-" -------------------------------
11
+"		- introduced highlighting of doctests
12
+"		- updated keywords, built-ins, and exceptions
13
+"		- corrected regular expressions for
46 14
 "
47
-"    python_version_2                       Enable highlighting for Python 2
48
-"                                           (Python 3 highlighting is enabled
49
-"                                           by default). Can also be set as
50
-"                                           a buffer (b:python_version_2)
51
-"                                           variable.
15
+"		  * functions
16
+"		  * decorators
17
+"		  * strings
18
+"		  * escapes
19
+"		  * numbers
20
+"		  * space error
52 21
 "
53
-"    You can also use the following local to buffer commands to switch
54
-"    between two highlighting modes:
22
+"		- corrected synchronization
23
+"		- more highlighting is ON by default, except
24
+"		- space error highlighting is OFF by default
55 25
 "
56
-"    :Python2Syntax                         Switch to Python 2 highlighting
57
-"                                           mode
58
-"    :Python3Syntax                         Switch to Python 3 highlighting
59
-"                                           mode
26
+" Optional highlighting can be controlled using these variables.
60 27
 "
61
-" Option names used by the script
62
-" -------------------------------
28
+"   let python_no_builtin_highlight = 1
29
+"   let python_no_doctest_code_highlight = 1
30
+"   let python_no_doctest_highlight = 1
31
+"   let python_no_exception_highlight = 1
32
+"   let python_no_number_highlight = 1
33
+"   let python_space_error_highlight = 1
63 34
 "
64
-"    python_highlight_builtins              Highlight builtin functions and
65
-"                                           objects
66
-"      python_highlight_builtin_objs        Highlight builtin objects only
67
-"      python_highlight_builtin_funcs       Highlight builtin functions only
68
-"    python_highlight_exceptions            Highlight standard exceptions
69
-"    python_highlight_string_formatting     Highlight % string formatting
70
-"    python_highlight_string_format         Highlight str.format syntax
71
-"    python_highlight_string_templates      Highlight string.Template syntax
72
-"    python_highlight_indent_errors         Highlight indentation errors
73
-"    python_highlight_space_errors          Highlight trailing spaces
74
-"    python_highlight_doctests              Highlight doc-tests
75
-"    python_print_as_function               Highlight 'print' statement as
76
-"                                           function for Python 2
77
-"    python_highlight_file_headers_as_comments
78
-"                                           Highlight shebang and coding
79
-"                                           headers as comments
35
+" All the options above can be switched on together.
80 36
 "
81
-"    python_highlight_all                   Enable all the options above
82
-"                                           NOTE: This option don't override
83
-"                                           any previously set options
84
-"
85
-"    python_slow_sync                       Can be set to 0 for slow machines
37
+"   let python_highlight_all = 1
86 38
 "
87 39
 
88
-" For version 5.x: Clear all syntax items
89
-" For versions greater than 6.x: Quit when a syntax file was already loaded
40
+" For version 5.x: Clear all syntax items.
41
+" For version 6.x: Quit when a syntax file was already loaded.
90 42
 if version < 600
91 43
   syntax clear
92 44
 elseif exists("b:current_syntax")
93 45
   finish
94 46
 endif
95 47
 
96
-"
97
-" Commands
98
-"
99
-command! -buffer Python2Syntax let b:python_version_2 = 1 | let &syntax=&syntax
100
-command! -buffer Python3Syntax let b:python_version_2 = 0 | let &syntax=&syntax
48
+" We need nocompatible mode in order to continue lines with backslashes.
49
+" Original setting will be restored.
50
+let s:cpo_save = &cpo
51
+set cpo&vim
52
+
53
+" Keep Python keywords in alphabetical order inside groups for easy
54
+" comparison with the table in the 'Python Language Reference'
55
+" http://docs.python.org/reference/lexical_analysis.html#keywords.
56
+" Groups are in the order presented in NAMING CONVENTIONS in syntax.txt.
57
+" Exceptions come last at the end of each group (class and def below).
58
+"
59
+" Keywords 'with' and 'as' are new in Python 2.6
60
+" (use 'from __future__ import with_statement' in Python 2.5).
61
+"
62
+" Some compromises had to be made to support both Python 3.0 and 2.6.
63
+" We include Python 3.0 features, but when a definition is duplicated,
64
+" the last definition takes precedence.
65
+"
66
+" - 'False', 'None', and 'True' are keywords in Python 3.0 but they are
67
+"   built-ins in 2.6 and will be highlighted as built-ins below.
68
+" - 'exec' is a built-in in Python 3.0 and will be highlighted as
69
+"   built-in below.
70
+" - 'nonlocal' is a keyword in Python 3.0 and will be highlighted.
71
+" - 'print' is a built-in in Python 3.0 and will be highlighted as
72
+"   built-in below (use 'from __future__ import print_function' in 2.6)
73
+"
74
+syn keyword pythonStatement	False, None, True
75
+syn keyword pythonStatement	as assert break continue del exec global
76
+syn keyword pythonStatement	lambda nonlocal pass print return with yield
77
+syn keyword pythonStatement	class def nextgroup=pythonFunction skipwhite
78
+syn keyword pythonConditional	elif else if
79
+syn keyword pythonRepeat	for while
80
+syn keyword pythonOperator	and in is not or
81
+syn keyword pythonException	except finally raise try
82
+syn keyword pythonInclude	from import
101 83
 
102
-" Enable option if it's not defined
103
-function! s:EnableByDefault(name)
104
-  if !exists(a:name)
105
-    let {a:name} = 1
106
-  endif
107
-endfunction
108
-
109
-" Check if option is enabled
110
-function! s:Enabled(name)
111
-  return exists(a:name) && {a:name}
112
-endfunction
113
-
114
-" Is it Python 2 syntax?
115
-function! s:Python2Syntax()
116
-  if exists("b:python_version_2")
117
-      return b:python_version_2
118
-  endif
119
-  return s:Enabled("g:python_version_2")
120
-endfunction
121
-
122
-"
123
-" Default options
124
-"
125
-
126
-call s:EnableByDefault("g:python_slow_sync")
127
-
128
-if s:Enabled("g:python_highlight_all")
129
-  call s:EnableByDefault("g:python_highlight_builtins")
130
-  if s:Enabled("g:python_highlight_builtins")
131
-    call s:EnableByDefault("g:python_highlight_builtin_objs")
132
-    call s:EnableByDefault("g:python_highlight_builtin_funcs")
84
+" Decorators (new in Python 2.4)
85
+syn match   pythonDecorator	"@" display nextgroup=pythonFunction skipwhite
86
+" The zero-length non-grouping match before the function name is
87
+" extremely important in pythonFunction.  Without it, everything is
88
+" interpreted as a function inside the contained environment of
89
+" doctests.
90
+" A dot must be allowed because of @MyClass.myfunc decorators.
91
+syn match   pythonFunction
92
+      \ "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained
93
+
94
+syn match   pythonComment	"#.*$" contains=pythonTodo,@Spell
95
+syn keyword pythonTodo		FIXME NOTE NOTES TODO XXX contained
96
+
97
+" Triple-quoted strings can contain doctests.
98
+syn region  pythonString
99
+      \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
100
+      \ contains=pythonEscape,@Spell
101
+syn region  pythonString
102
+      \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
103
+      \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
104
+syn region  pythonRawString
105
+      \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
106
+      \ contains=@Spell
107
+syn region  pythonRawString
108
+      \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
109
+      \ contains=pythonSpaceError,pythonDoctest,@Spell
110
+
111
+syn match   pythonEscape	+\\[abfnrtv'"\\]+ contained
112
+syn match   pythonEscape	"\\\o\{1,3}" contained
113
+syn match   pythonEscape	"\\x\x\{2}" contained
114
+syn match   pythonEscape	"\%(\\u\x\{4}\|\\U\x\{8}\)" contained
115
+" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
116
+syn match   pythonEscape	"\\N{.\{-}}" contained
117
+syn match   pythonEscape	"\\$"
118
+
119
+if exists("python_highlight_all")
120
+  if exists("python_no_builtin_highlight")
121
+    unlet python_no_builtin_highlight
133 122
   endif
134
-  call s:EnableByDefault("g:python_highlight_exceptions")
135
-  call s:EnableByDefault("g:python_highlight_string_formatting")
136
-  call s:EnableByDefault("g:python_highlight_string_format")
137
-  call s:EnableByDefault("g:python_highlight_string_templates")
138
-  call s:EnableByDefault("g:python_highlight_indent_errors")
139
-  call s:EnableByDefault("g:python_highlight_space_errors")
140
-  call s:EnableByDefault("g:python_highlight_doctests")
141
-  call s:EnableByDefault("g:python_print_as_function")
142
-endif
143
-
144
-"
145
-" Keywords
146
-"
147
-
148
-syn keyword pythonStatement     break continue del
149
-syn keyword pythonStatement     exec return
150
-syn keyword pythonStatement     pass raise
151
-syn keyword pythonStatement     global assert
152
-syn keyword pythonStatement     lambda
153
-syn keyword pythonStatement     with
154
-syn keyword pythonStatement     def class nextgroup=pythonFunction skipwhite
155
-syn keyword pythonRepeat        for while
156
-syn keyword pythonConditional   if elif else
157
-syn keyword pythonImport        import
158
-syn keyword pythonException     try except finally
159
-syn keyword pythonOperator      and in is not or
160
-
161
-syn match pythonStatement   "\<yield\>" display
162
-syn match pythonImport      "\<from\>" display
163
-
164
-if s:Python2Syntax()
165
-  if !s:Enabled("g:python_print_as_function")
166
-    syn keyword pythonStatement  print
123
+  if exists("python_no_doctest_code_highlight")
124
+    unlet python_no_doctest_code_highlight
167 125
   endif
168
-  syn keyword pythonImport      as
169
-  syn match   pythonFunction    "[a-zA-Z_][a-zA-Z0-9_]*" display contained
170
-else
171
-  syn keyword pythonStatement   as nonlocal None
172
-  syn match   pythonStatement   "\<yield\s\+from\>" display
173
-  syn keyword pythonBoolean     True False
174
-  syn match   pythonFunction    "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
175
-endif
176
-
177
-"
178
-" Decorators (new in Python 2.4)
179
-"
180
-
181
-syn match   pythonDecorator	"@" display nextgroup=pythonDottedName skipwhite
182
-syn match   pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
183
-syn match   pythonDot        "\." display containedin=pythonDottedName
184
-
185
-"
186
-" Comments
187
-"
188
-
189
-syn match   pythonComment	"#.*$" display contains=pythonTodo,@Spell
190
-if !s:Enabled("g:python_highlight_file_headers_as_comments")
191
-  syn match   pythonRun		"\%^#!.*$"
192
-  syn match   pythonCoding	"\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
193
-endif
194
-syn keyword pythonTodo		TODO FIXME XXX contained
195
-
196
-"
197
-" Errors
198
-"
199
-
200
-syn match pythonError		"\<\d\+\D\+\>" display
201
-syn match pythonError		"[$?]" display
202
-syn match pythonError		"[&|]\{2,}" display
203
-syn match pythonError		"[=]\{3,}" display
204
-
205
-" Mixing spaces and tabs also may be used for pretty formatting multiline
206
-" statements
207
-if s:Enabled("g:python_highlight_indent_errors")
208
-  syn match pythonIndentError	"^\s*\%( \t\|\t \)\s*\S"me=e-1 display
209
-endif
210
-
211
-" Trailing space errors
212
-if s:Enabled("g:python_highlight_space_errors")
213
-  syn match pythonSpaceError	"\s\+$" display
214
-endif
215
-
216
-"
217
-" Strings
218
-"
219
-
220
-if s:Python2Syntax()
221
-  " Python 2 strings
222
-  syn region pythonString   start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
223
-  syn region pythonString   start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
224
-  syn region pythonString   start=+[bB]\="""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
225
-  syn region pythonString   start=+[bB]\='''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
226
-else
227
-  " Python 3 byte strings
228
-  syn region pythonBytes		start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
229
-  syn region pythonBytes		start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
230
-  syn region pythonBytes		start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell
231
-  syn region pythonBytes		start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell
232
-
233
-  syn match pythonBytesError    ".\+" display contained
234
-  syn match pythonBytesContent  "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError
235
-endif
236
-
237
-syn match pythonBytesEscape       +\\[abfnrtv'"\\]+ display contained
238
-syn match pythonBytesEscape       "\\\o\o\=\o\=" display contained
239
-syn match pythonBytesEscapeError  "\\\o\{,2}[89]" display contained
240
-syn match pythonBytesEscape       "\\x\x\{2}" display contained
241
-syn match pythonBytesEscapeError  "\\x\x\=\X" display contained
242
-syn match pythonBytesEscape       "\\$"
243
-
244
-syn match pythonUniEscape         "\\u\x\{4}" display contained
245
-syn match pythonUniEscapeError    "\\u\x\{,3}\X" display contained
246
-syn match pythonUniEscape         "\\U\x\{8}" display contained
247
-syn match pythonUniEscapeError    "\\U\x\{,7}\X" display contained
248
-syn match pythonUniEscape         "\\N{[A-Z ]\+}" display contained
249
-syn match pythonUniEscapeError    "\\N{[^A-Z ]\+}" display contained
250
-
251
-if s:Python2Syntax()
252
-  " Python 2 Unicode strings
253
-  syn region pythonUniString  start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
254
-  syn region pythonUniString  start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
255
-  syn region pythonUniString  start=+[uU]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
256
-  syn region pythonUniString  start=+[uU]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
257
-else
258
-  " Python 3 strings
259
-  syn region pythonString   start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
260
-  syn region pythonString   start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
261
-  syn region pythonString   start=+"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
262
-  syn region pythonString   start=+'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
263
-endif
264
-
265
-if s:Python2Syntax()
266
-  " Python 2 Unicode raw strings
267
-  syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
268
-  syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
269
-  syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell
270
-  syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell
271
-
272
-  syn match  pythonUniRawEscape       "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
273
-  syn match  pythonUniRawEscapeError  "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
274
-endif
275
-
276
-" Python 2/3 raw strings
277
-if s:Python2Syntax()
278
-  syn region pythonRawString  start=+[bB]\=[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
279
-  syn region pythonRawString  start=+[bB]\=[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
280
-  syn region pythonRawString  start=+[bB]\=[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
281
-  syn region pythonRawString  start=+[bB]\=[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
282
-else
283
-  syn region pythonRawString  start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
284
-  syn region pythonRawString  start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
285
-  syn region pythonRawString  start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
286
-  syn region pythonRawString  start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
287
-
288
-  syn region pythonRawBytes  start=+[bB][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
289
-  syn region pythonRawBytes  start=+[bB][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
290
-  syn region pythonRawBytes  start=+[bB][rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
291
-  syn region pythonRawBytes  start=+[bB][rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
292
-endif
293
-
294
-syn match pythonRawEscape +\\['"]+ display transparent contained
295
-
296
-if s:Enabled("g:python_highlight_string_formatting")
297
-  " % operator string formatting
298
-  if s:Python2Syntax()
299
-    syn match pythonStrFormatting	"%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
300
-    syn match pythonStrFormatting	"%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
301
-  else
302
-    syn match pythonStrFormatting	"%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
303
-    syn match pythonStrFormatting	"%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
126
+  if exists("python_no_doctest_highlight")
127
+    unlet python_no_doctest_highlight
304 128
   endif
305
-endif
306
-
307
-if s:Enabled("g:python_highlight_string_format")
308
-  " str.format syntax
309
-  if s:Python2Syntax()
310
-    syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
311
-    syn match pythonStrFormat	"{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
312
-  else
313
-    syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString
314
-    syn match pythonStrFormat	"{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString
129
+  if exists("python_no_exception_highlight")
130
+    unlet python_no_exception_highlight
315 131
   endif
316
-endif
317
-
318
-if s:Enabled("g:python_highlight_string_templates")
319
-  " string.Template format
320
-  if s:Python2Syntax()
321
-    syn match pythonStrTemplate	"\$\$" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
322
-    syn match pythonStrTemplate	"\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
323
-    syn match pythonStrTemplate	"\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
324
-  else
325
-    syn match pythonStrTemplate	"\$\$" contained containedin=pythonString,pythonRawString
326
-    syn match pythonStrTemplate	"\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString
327
-    syn match pythonStrTemplate	"\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString
132
+  if exists("python_no_number_highlight")
133
+    unlet python_no_number_highlight
328 134
   endif
135
+  let python_space_error_highlight = 1
329 136
 endif
330 137
 
331
-if s:Enabled("g:python_highlight_doctests")
332
-  " DocTests
333
-  syn region pythonDocTest	start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
334
-  syn region pythonDocTest2	start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
138
+" It is very important to understand all details before changing the
139
+" regular expressions below or their order.
140
+" The word boundaries are *not* the floating-point number boundaries
141
+" because of a possible leading or trailing decimal point.
142
+" The expressions below ensure that all valid number literals are
143
+" highlighted, and invalid number literals are not.  For example,
144
+"
145
+" - a decimal point in '4.' at the end of a line is highlighted,
146
+" - a second dot in 1.0.0 is not highlighted,
147
+" - 08 is not highlighted,
148
+" - 08e0 or 08j are highlighted,
149
+"
150
+" and so on, as specified in the 'Python Language Reference'.
151
+" http://docs.python.org/reference/lexical_analysis.html#numeric-literals
152
+if !exists("python_no_number_highlight")
153
+  " numbers (including longs and complex)
154
+  syn match   pythonNumber	"\<0[oO]\=\o\+[Ll]\=\>"
155
+  syn match   pythonNumber	"\<0[xX]\x\+[Ll]\=\>"
156
+  syn match   pythonNumber	"\<0[bB][01]\+[Ll]\=\>"
157
+  syn match   pythonNumber	"\<\%([1-9]\d*\|0\)[Ll]\=\>"
158
+  syn match   pythonNumber	"\<\d\+[jJ]\>"
159
+  syn match   pythonNumber	"\<\d\+[eE][+-]\=\d\+[jJ]\=\>"
160
+  syn match   pythonNumber
161
+	\ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="
162
+  syn match   pythonNumber
163
+	\ "\%(^\|\W\)\@<=\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"
335 164
 endif
336 165
 
337
-"
338
-" Numbers (ints, longs, floats, complex)
339
-"
340
-
341
-if s:Python2Syntax()
342
-  syn match   pythonHexError	"\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>" display
343
-  syn match   pythonOctError	"\<0[oO]\=\o*\D\+\d*[lL]\=\>" display
344
-  syn match   pythonBinError	"\<0[bB][01]*\D\+\d*[lL]\=\>" display
345
-
346
-  syn match   pythonHexNumber	"\<0[xX]\x\+[lL]\=\>" display
347
-  syn match   pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
348
-  syn match   pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
349
-
350
-  syn match   pythonNumberError	"\<\d\+\D[lL]\=\>" display
351
-  syn match   pythonNumber	"\<\d[lL]\=\>" display
352
-  syn match   pythonNumber	"\<[0-9]\d\+[lL]\=\>" display
353
-  syn match   pythonNumber	"\<\d\+[lLjJ]\>" display
354
-
355
-  syn match   pythonOctError	"\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
356
-  syn match   pythonBinError	"\<0[bB][01]*[2-9]\d*[lL]\=\>" display
357
-else
358
-  syn match   pythonHexError	"\<0[xX]\x*[g-zG-Z]\x*\>" display
359
-  syn match   pythonOctError	"\<0[oO]\=\o*\D\+\d*\>" display
360
-  syn match   pythonBinError	"\<0[bB][01]*\D\+\d*\>" display
361
-
362
-  syn match   pythonHexNumber	"\<0[xX]\x\+\>" display
363
-  syn match   pythonOctNumber "\<0[oO]\o\+\>" display
364
-  syn match   pythonBinNumber "\<0[bB][01]\+\>" display
365
-
366
-  syn match   pythonNumberError	"\<\d\+\D\>" display
367
-  syn match   pythonNumberError	"\<0\d\+\>" display
368
-  syn match   pythonNumber	"\<\d\>" display
369
-  syn match   pythonNumber	"\<[1-9]\d\+\>" display
370
-  syn match   pythonNumber	"\<\d\+[jJ]\>" display
371
-
372
-  syn match   pythonOctError	"\<0[oO]\=\o*[8-9]\d*\>" display
373
-  syn match   pythonBinError	"\<0[bB][01]*[2-9]\d*\>" display
166
+" Group the built-ins in the order in the 'Python Library Reference' for
167
+" easier comparison.
168
+" http://docs.python.org/library/constants.html
169
+" http://docs.python.org/library/functions.html
170
+" http://docs.python.org/library/functions.html#non-essential-built-in-functions
171
+" Python built-in functions are in alphabetical order.
172
+if !exists("python_no_builtin_highlight")
173
+  " built-in constants
174
+  " 'False', 'True', and 'None' are also reserved words in Python 3.0
175
+  syn keyword pythonBuiltin	False True None
176
+  syn keyword pythonBuiltin	NotImplemented Ellipsis __debug__
177
+  " built-in functions
178
+  syn keyword pythonBuiltin	abs all any bin bool chr classmethod
179
+  syn keyword pythonBuiltin	compile complex delattr dict dir divmod
180
+  syn keyword pythonBuiltin	enumerate eval filter float format
181
+  syn keyword pythonBuiltin	frozenset getattr globals hasattr hash
182
+  syn keyword pythonBuiltin	help hex id input int isinstance
183
+  syn keyword pythonBuiltin	issubclass iter len list locals map max
184
+  syn keyword pythonBuiltin	min next object oct open ord pow print
185
+  syn keyword pythonBuiltin	property range repr reversed round set
186
+  syn keyword pythonBuiltin	setattr slice sorted staticmethod str
187
+  syn keyword pythonBuiltin	sum super tuple type vars zip __import__
188
+  " Python 2.6 only
189
+  syn keyword pythonBuiltin	basestring callable cmp execfile file
190
+  syn keyword pythonBuiltin	long raw_input reduce reload unichr
191
+  syn keyword pythonBuiltin	unicode xrange
192
+  " Python 3.0 only
193
+  syn keyword pythonBuiltin	ascii bytearray bytes exec memoryview
194
+  " non-essential built-in functions; Python 2.6 only
195
+  syn keyword pythonBuiltin	apply buffer coerce intern
374 196
 endif
375 197
 
376
-syn match   pythonFloat		"\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display
377
-syn match   pythonFloat		"\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
378
-syn match   pythonFloat		"\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display
379
-
380
-"
381
-" Builtin objects and types
382
-"
383
-
384
-if s:Enabled("g:python_highlight_builtin_objs")
385
-  if s:Python2Syntax()
386
-    syn keyword pythonBuiltinObj	None
387
-    syn keyword pythonBoolean		True False
388
-  endif
389
-  syn keyword pythonBuiltinObj	Ellipsis NotImplemented
390
-  syn keyword pythonBuiltinObj	__debug__ __doc__ __file__ __name__ __package__
198
+" From the 'Python Library Reference' class hierarchy at the bottom.
199
+" http://docs.python.org/library/exceptions.html
200
+if !exists("python_no_exception_highlight")
201
+  " builtin base exceptions (only used as base classes for other exceptions)
202
+  syn keyword pythonExceptions	BaseException Exception
203
+  syn keyword pythonExceptions	ArithmeticError EnvironmentError
204
+  syn keyword pythonExceptions	LookupError
205
+  " builtin base exception removed in Python 3.0
206
+  syn keyword pythonExceptions	StandardError
207
+  " builtin exceptions (actually raised)
208
+  syn keyword pythonExceptions	AssertionError AttributeError BufferError
209
+  syn keyword pythonExceptions	EOFError FloatingPointError GeneratorExit
210
+  syn keyword pythonExceptions	IOError ImportError IndentationError
211
+  syn keyword pythonExceptions	IndexError KeyError KeyboardInterrupt
212
+  syn keyword pythonExceptions	MemoryError NameError NotImplementedError
213
+  syn keyword pythonExceptions	OSError OverflowError ReferenceError
214
+  syn keyword pythonExceptions	RuntimeError StopIteration SyntaxError
215
+  syn keyword pythonExceptions	SystemError SystemExit TabError TypeError
216
+  syn keyword pythonExceptions	UnboundLocalError UnicodeError
217
+  syn keyword pythonExceptions	UnicodeDecodeError UnicodeEncodeError
218
+  syn keyword pythonExceptions	UnicodeTranslateError ValueError VMSError
219
+  syn keyword pythonExceptions	WindowsError ZeroDivisionError
220
+  " builtin warnings
221
+  syn keyword pythonExceptions	BytesWarning DeprecationWarning FutureWarning
222
+  syn keyword pythonExceptions	ImportWarning PendingDeprecationWarning
223
+  syn keyword pythonExceptions	RuntimeWarning SyntaxWarning UnicodeWarning
224
+  syn keyword pythonExceptions	UserWarning Warning
391 225
 endif
392 226
 
393
-"
394
-" Builtin functions
395
-"
396
-
397
-if s:Enabled("g:python_highlight_builtin_funcs")
398
-  if s:Python2Syntax()
399
-    syn keyword pythonBuiltinFunc	apply basestring buffer callable coerce
400
-    syn keyword pythonBuiltinFunc	execfile file help intern long raw_input
401
-    syn keyword pythonBuiltinFunc	reduce reload unichr unicode xrange
402
-    if s:Enabled("g:python_print_as_function")
403
-      syn keyword pythonBuiltinFunc	print
404
-    endif
405
-  else
406
-    syn keyword pythonBuiltinFunc	ascii exec memoryview print
407
-  endif
408
-  syn keyword pythonBuiltinFunc	__import__ abs all any
409
-  syn keyword pythonBuiltinFunc	bin bool bytearray bytes
410
-  syn keyword pythonBuiltinFunc	chr classmethod cmp compile complex
411
-  syn keyword pythonBuiltinFunc	delattr dict dir divmod enumerate eval
412
-  syn keyword pythonBuiltinFunc	filter float format frozenset getattr
413
-  syn keyword pythonBuiltinFunc	globals hasattr hash hex id
414
-  syn keyword pythonBuiltinFunc	input int isinstance
415
-  syn keyword pythonBuiltinFunc	issubclass iter len list locals map max
416
-  syn keyword pythonBuiltinFunc	min next object oct open ord
417
-  syn keyword pythonBuiltinFunc	pow property range
418
-  syn keyword pythonBuiltinFunc	repr reversed round set setattr
419
-  syn keyword pythonBuiltinFunc	slice sorted staticmethod str sum super tuple
420
-  syn keyword pythonBuiltinFunc	type vars zip
227
+if exists("python_space_error_highlight")
228
+  " trailing whitespace
229
+  syn match   pythonSpaceError	display excludenl "\s\+$"
230
+  " mixed tabs and spaces
231
+  syn match   pythonSpaceError	display " \+\t"
232
+  syn match   pythonSpaceError	display "\t\+ "
421 233
 endif
422 234
 
423
-"
424
-" Builtin exceptions and warnings
425
-"
426
-
427
-if s:Enabled("g:python_highlight_exceptions")
428
-  if s:Python2Syntax()
429
-    syn keyword pythonExClass	StandardError
235
+" Do not spell doctests inside strings.
236
+" Notice that the end of a string, either ''', or """, will end the contained
237
+" doctest too.  Thus, we do *not* need to have it as an end pattern.
238
+if !exists("python_no_doctest_highlight")
239
+  if !exists("python_no_doctest_code_highlight")
240
+    syn region pythonDoctest
241
+	  \ start="^\s*>>>\s" end="^\s*$"
242
+	  \ contained contains=ALLBUT,pythonDoctest,@Spell
243
+    syn region pythonDoctestValue
244
+	  \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
245
+	  \ contained
430 246
   else
431
-    syn keyword pythonExClass	BlockingIOError ChildProcessError
432
-    syn keyword pythonExClass	ConnectionError BrokenPipeError
433
-    syn keyword pythonExClass	ConnectionAbortedError ConnectionRefusedError
434
-    syn keyword pythonExClass	ConnectionResetError FileExistsError
435
-    syn keyword pythonExClass	FileNotFoundError InterruptedError
436
-    syn keyword pythonExClass	IsADirectoryError NotADirectoryError
437
-    syn keyword pythonExClass	PermissionError ProcessLookupError TimeoutError
438
-
439
-    syn keyword pythonExClass	ResourceWarning
247
+    syn region pythonDoctest
248
+	  \ start="^\s*>>>" end="^\s*$"
249
+	  \ contained contains=@NoSpell
440 250
   endif
441
-  syn keyword pythonExClass	BaseException
442
-  syn keyword pythonExClass	Exception ArithmeticError
443
-  syn keyword pythonExClass	LookupError EnvironmentError
444
-
445
-  syn keyword pythonExClass	AssertionError AttributeError BufferError EOFError
446
-  syn keyword pythonExClass	FloatingPointError GeneratorExit IOError
447
-  syn keyword pythonExClass	ImportError IndexError KeyError
448
-  syn keyword pythonExClass	KeyboardInterrupt MemoryError NameError
449
-  syn keyword pythonExClass	NotImplementedError OSError OverflowError
450
-  syn keyword pythonExClass	ReferenceError RuntimeError StopIteration
451
-  syn keyword pythonExClass	SyntaxError IndentationError TabError
452
-  syn keyword pythonExClass	SystemError SystemExit TypeError
453
-  syn keyword pythonExClass	UnboundLocalError UnicodeError
454
-  syn keyword pythonExClass	UnicodeEncodeError UnicodeDecodeError
455
-  syn keyword pythonExClass	UnicodeTranslateError ValueError VMSError
456
-  syn keyword pythonExClass	WindowsError ZeroDivisionError
457
-
458
-  syn keyword pythonExClass	Warning UserWarning BytesWarning DeprecationWarning
459
-  syn keyword pythonExClass	PendingDepricationWarning SyntaxWarning
460
-  syn keyword pythonExClass	RuntimeWarning FutureWarning
461
-  syn keyword pythonExClass	ImportWarning UnicodeWarning
462 251
 endif
463 252
 
464
-if s:Enabled("g:python_slow_sync")
465
-  syn sync minlines=2000
466
-else
467
-  " This is fast but code inside triple quoted strings screws it up. It
468
-  " is impossible to fix because the only way to know if you are inside a
469
-  " triple quoted string is to start from the beginning of the file.
470
-  syn sync match pythonSync grouphere NONE "):$"
471
-  syn sync maxlines=200
472
-endif
253
+" Sync at the beginning of class, function, or method definition.
254
+syn sync match pythonSync grouphere NONE "^\s*\%(def\|class\)\s\+\h\w*\s*("
473 255
 
474 256
 if version >= 508 || !exists("did_python_syn_inits")
475 257
   if version <= 508
@@ -479,74 +261,43 @@ if version >= 508 || !exists("did_python_syn_inits")
479 261
     command -nargs=+ HiLink hi def link <args>
480 262
   endif
481 263
 
482
-  HiLink pythonStatement        Statement
483
-  HiLink pythonImport           Include
484
-  HiLink pythonFunction         Function
485
-  HiLink pythonConditional      Conditional
486
-  HiLink pythonRepeat           Repeat
487
-  HiLink pythonException        Exception
488
-  HiLink pythonOperator         Operator
489
-
490
-  HiLink pythonDecorator        Define
491
-  HiLink pythonDottedName       Function
492
-  HiLink pythonDot              Normal
493
-
494
-  HiLink pythonComment          Comment
495
-  if !s:Enabled("g:python_highlight_file_headers_as_comments")
496
-    HiLink pythonCoding           Special
497
-    HiLink pythonRun              Special
264
+  " The default highlight links.  Can be overridden later.
265
+  HiLink pythonStatement	Statement
266
+  HiLink pythonConditional	Conditional
267
+  HiLink pythonRepeat		Repeat
268
+  HiLink pythonOperator		Operator
269
+  HiLink pythonException	Exception
270
+  HiLink pythonInclude		Include
271
+  HiLink pythonDecorator	Define
272
+  HiLink pythonFunction		Function
273
+  HiLink pythonComment		Comment
274
+  HiLink pythonTodo		Todo
275
+  HiLink pythonString		String
276
+  HiLink pythonRawString	String
277
+  HiLink pythonEscape		Special
278
+  if !exists("python_no_number_highlight")
279
+    HiLink pythonNumber		Number
498 280
   endif
499
-  HiLink pythonTodo             Todo
500
-
501
-  HiLink pythonError            Error
502
-  HiLink pythonIndentError      Error
503
-  HiLink pythonSpaceError       Error
504
-
505
-  HiLink pythonString           String
506
-  HiLink pythonRawString        String
507
-
508
-  HiLink pythonUniEscape        Special
509
-  HiLink pythonUniEscapeError   Error
510
-
511
-  if s:Python2Syntax()
512
-    HiLink pythonUniString          String
513
-    HiLink pythonUniRawString       String
514
-    HiLink pythonUniRawEscape       Special
515
-    HiLink pythonUniRawEscapeError  Error
516
-  else
517
-    HiLink pythonBytes              String
518
-    HiLink pythonRawBytes           String
519
-    HiLink pythonBytesContent       String
520
-    HiLink pythonBytesError         Error
521
-    HiLink pythonBytesEscape        Special
522
-    HiLink pythonBytesEscapeError   Error
281
+  if !exists("python_no_builtin_highlight")
282
+    HiLink pythonBuiltin	Function
283
+  endif
284
+  if !exists("python_no_exception_highlight")
285
+    HiLink pythonExceptions	Structure
286
+  endif
287
+  if exists("python_space_error_highlight")
288
+    HiLink pythonSpaceError	Error
289
+  endif
290
+  if !exists("python_no_doctest_highlight")
291
+    HiLink pythonDoctest	Special
292
+    HiLink pythonDoctestValue	Define
523 293
   endif
524
-
525
-  HiLink pythonStrFormatting    Special
526
-  HiLink pythonStrFormat        Special
527
-  HiLink pythonStrTemplate      Special
528
-
529
-  HiLink pythonDocTest          Special
530
-  HiLink pythonDocTest2         Special
531
-
532
-  HiLink pythonNumber           Number
533
-  HiLink pythonHexNumber        Number
534
-  HiLink pythonOctNumber        Number
535
-  HiLink pythonBinNumber        Number
536
-  HiLink pythonFloat            Float
537
-  HiLink pythonNumberError      Error
538
-  HiLink pythonOctError         Error
539
-  HiLink pythonHexError         Error
540
-  HiLink pythonBinError         Error
541
-
542
-  HiLink pythonBoolean          Boolean
543
-
544
-  HiLink pythonBuiltinObj       Structure
545
-  HiLink pythonBuiltinFunc      Function
546
-
547
-  HiLink pythonExClass          Structure
548 294
 
549 295
   delcommand HiLink
550 296
 endif
551 297
 
552 298
 let b:current_syntax = "python"
299
+
300
+let &cpo = s:cpo_save
301
+unlet s:cpo_save
302
+
303
+" vim:set sw=2 sts=2 ts=8 noet: