3 " Maintainer: Neil Schemenauer <nas@python.ca>
5 " Added Python 2.4 features 2006 May 4 (Dmitry Vasiliev)
7 " Options to control Python syntax highlighting:
9 " For highlighted numbers:
11 " let python_highlight_numbers = 1
13 " For highlighted builtin functions:
15 " let python_highlight_builtins = 1
17 " For highlighted standard exceptions:
19 " let python_highlight_exceptions = 1
21 " Highlight erroneous whitespace:
23 " let python_highlight_space_errors = 1
25 " If you want all possible Python highlighting (the same as setting the
28 " let python_highlight_all = 1
31 " For version 5.x: Clear all syntax items
32 " For version 6.x: Quit when a syntax file was already loaded
35 elseif exists("b:current_syntax")
40 syn keyword pythonStatement break continue del
41 syn keyword pythonStatement except exec finally
42 syn keyword pythonStatement pass print raise
43 syn keyword pythonStatement return try with
44 syn keyword pythonStatement global assert
45 syn keyword pythonStatement lambda yield
46 syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
47 syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" contained
48 syn keyword pythonRepeat for while
49 syn keyword pythonConditional if elif else
50 syn keyword pythonOperator and in is not or
51 " AS will be a keyword in Python 3
52 syn keyword pythonPreCondit import from as
53 syn match pythonComment "#.*$" contains=pythonTodo,@Spell
54 syn keyword pythonTodo TODO FIXME XXX contained
56 " Decorators (new in Python 2.4)
57 syn match pythonDecoratorName "[[:alpha:]_][[:alnum:]_]*\%(\.[[:alpha:]_][[:alnum:]_]*\)*"
58 syn match pythonDecorator "@" display nextgroup=pythonDecoratorName skipwhite
61 syn region pythonString matchgroup=Normal start=+[uU]\='+ end=+'+ skip=+\\\\\|\\'+ contains=pythonEscape,@Spell
62 syn region pythonString matchgroup=Normal start=+[uU]\="+ end=+"+ skip=+\\\\\|\\"+ contains=pythonEscape,@Spell
63 syn region pythonString matchgroup=Normal start=+[uU]\="""+ end=+"""+ contains=pythonEscape,@Spell
64 syn region pythonString matchgroup=Normal start=+[uU]\='''+ end=+'''+ contains=pythonEscape,@Spell
65 syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]'+ end=+'+ skip=+\\\\\|\\'+ contains=@Spell
66 syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]"+ end=+"+ skip=+\\\\\|\\"+ contains=@Spell
67 syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]"""+ end=+"""+ contains=@Spell
68 syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]'''+ end=+'''+ contains=@Spell
69 syn match pythonEscape +\\[abfnrtv'"\\]+ contained
70 syn match pythonEscape "\\\o\{1,3}" contained
71 syn match pythonEscape "\\x\x\{2}" contained
72 syn match pythonEscape "\(\\u\x\{4}\|\\U\x\{8}\)" contained
73 syn match pythonEscape "\\$"
75 if exists("python_highlight_all")
76 let python_highlight_numbers = 1
77 let python_highlight_builtins = 1
78 let python_highlight_exceptions = 1
79 let python_highlight_space_errors = 1
82 if exists("python_highlight_numbers")
83 " numbers (including longs and complex)
84 syn match pythonNumber "\<0x\x\+[Ll]\=\>"
85 syn match pythonNumber "\<\d\+[LljJ]\=\>"
86 syn match pythonNumber "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
87 syn match pythonNumber "\<\d\+\.\([eE][+-]\=\d\+\)\=[jJ]\=\>"
88 syn match pythonNumber "\<\d\+\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
91 if exists("python_highlight_builtins")
92 " builtin functions, types and objects, not really part of the syntax
93 syn keyword pythonBuiltin True False bool enumerate set frozenset help
94 syn keyword pythonBuiltin reversed sorted sum
95 syn keyword pythonBuiltin Ellipsis None NotImplemented __import__ abs
96 syn keyword pythonBuiltin apply buffer callable chr classmethod cmp
97 syn keyword pythonBuiltin coerce compile complex delattr dict dir divmod
98 syn keyword pythonBuiltin eval execfile file filter float getattr globals
99 syn keyword pythonBuiltin hasattr hash hex id input int intern isinstance
100 syn keyword pythonBuiltin issubclass iter len list locals long map max
101 syn keyword pythonBuiltin min object oct open ord pow property range
102 syn keyword pythonBuiltin raw_input reduce reload repr round setattr
103 syn keyword pythonBuiltin slice staticmethod str super tuple type unichr
104 syn keyword pythonBuiltin unicode vars xrange zip
107 if exists("python_highlight_exceptions")
108 " builtin exceptions and warnings
109 syn keyword pythonException ArithmeticError AssertionError AttributeError
110 syn keyword pythonException DeprecationWarning EOFError EnvironmentError
111 syn keyword pythonException Exception FloatingPointError IOError
112 syn keyword pythonException ImportError IndentationError IndexError
113 syn keyword pythonException KeyError KeyboardInterrupt LookupError
114 syn keyword pythonException MemoryError NameError NotImplementedError
115 syn keyword pythonException OSError OverflowError OverflowWarning
116 syn keyword pythonException ReferenceError RuntimeError RuntimeWarning
117 syn keyword pythonException StandardError StopIteration SyntaxError
118 syn keyword pythonException SyntaxWarning SystemError SystemExit TabError
119 syn keyword pythonException TypeError UnboundLocalError UnicodeError
120 syn keyword pythonException UnicodeEncodeError UnicodeDecodeError
121 syn keyword pythonException UnicodeTranslateError
122 syn keyword pythonException UserWarning ValueError Warning WindowsError
123 syn keyword pythonException ZeroDivisionError
126 if exists("python_highlight_space_errors")
127 " trailing whitespace
128 syn match pythonSpaceError display excludenl "\S\s\+$"ms=s+1
129 " mixed tabs and spaces
130 syn match pythonSpaceError display " \+\t"
131 syn match pythonSpaceError display "\t\+ "
134 " This is fast but code inside triple quoted strings screws it up. It
135 " is impossible to fix because the only way to know if you are inside a
136 " triple quoted string is to start from the beginning of the file. If
137 " you have a fast machine you can try uncommenting the "sync minlines"
138 " and commenting out the rest.
139 syn sync match pythonSync grouphere NONE "):$"
140 syn sync maxlines=200
141 "syn sync minlines=2000
143 if version >= 508 || !exists("did_python_syn_inits")
145 let did_python_syn_inits = 1
146 command -nargs=+ HiLink hi link <args>
148 command -nargs=+ HiLink hi def link <args>
151 " The default methods for highlighting. Can be overridden later
152 HiLink pythonStatement Statement
153 HiLink pythonFunction Function
154 HiLink pythonDecoratorName Function
155 HiLink pythonConditional Conditional
156 HiLink pythonRepeat Repeat
157 HiLink pythonString String
158 HiLink pythonRawString String
159 HiLink pythonEscape Special
160 HiLink pythonOperator Operator
161 HiLink pythonPreCondit PreCondit
162 HiLink pythonComment Comment
163 HiLink pythonTodo Todo
164 HiLink pythonDecorator Define
165 if exists("python_highlight_numbers")
166 HiLink pythonNumber Number
168 if exists("python_highlight_builtins")
169 HiLink pythonBuiltin Function
171 if exists("python_highlight_exceptions")
172 HiLink pythonException Exception
174 if exists("python_highlight_space_errors")
175 HiLink pythonSpaceError Error
181 let b:current_syntax = "python"