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