python.vim 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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: 2010-04-09
  6. " Filenames: *.py
  7. " Version: 2.6.6
  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. " Caleb Adamantine
  22. " for the patch fixing highlighting for decorators
  23. " Andrea Riciputi
  24. " for the patch with new configuration options
  25. "
  26. " Options:
  27. "
  28. " For set option do: let OPTION_NAME = 1
  29. " For clear option do: let OPTION_NAME = 0
  30. "
  31. " Option names:
  32. "
  33. " For highlight builtin functions and objects:
  34. " python_highlight_builtins
  35. "
  36. " For highlight builtin objects:
  37. " python_highlight_builtin_objs
  38. "
  39. " For highlight builtin funtions:
  40. " python_highlight_builtin_funcs
  41. "
  42. " For highlight standard exceptions:
  43. " python_highlight_exceptions
  44. "
  45. " For highlight string formatting:
  46. " python_highlight_string_formatting
  47. "
  48. " For highlight str.format syntax:
  49. " python_highlight_string_format
  50. "
  51. " For highlight string.Template syntax:
  52. " python_highlight_string_templates
  53. "
  54. " For highlight indentation errors:
  55. " python_highlight_indent_errors
  56. "
  57. " For highlight trailing spaces:
  58. " python_highlight_space_errors
  59. "
  60. " For highlight doc-tests:
  61. " python_highlight_doctests
  62. "
  63. " If you want all Python highlightings above:
  64. " python_highlight_all
  65. " (This option not override previously set options)
  66. "
  67. " For fast machines:
  68. " python_slow_sync
  69. "
  70. " For "print" builtin as function:
  71. " python_print_as_function
  72. " For version 5.x: Clear all syntax items
  73. " For version 6.x: Quit when a syntax file was already loaded
  74. if version < 600
  75. syntax clear
  76. elseif exists("b:current_syntax")
  77. finish
  78. endif
  79. if exists("python_highlight_all") && python_highlight_all != 0
  80. " Not override previously set options
  81. if !exists("python_highlight_builtins")
  82. if !exists("python_highlight_builtin_objs")
  83. let python_highlight_builtin_objs = 1
  84. endif
  85. if !exists("python_highlight_builtin_funcs")
  86. let python_highlight_builtin_funcs = 1
  87. endif
  88. endif
  89. if !exists("python_highlight_exceptions")
  90. let python_highlight_exceptions = 1
  91. endif
  92. if !exists("python_highlight_string_formatting")
  93. let python_highlight_string_formatting = 1
  94. endif
  95. if !exists("python_highlight_string_format")
  96. let python_highlight_string_format = 1
  97. endif
  98. if !exists("python_highlight_string_templates")
  99. let python_highlight_string_templates = 1
  100. endif
  101. if !exists("python_highlight_indent_errors")
  102. let python_highlight_indent_errors = 1
  103. endif
  104. if !exists("python_highlight_space_errors")
  105. let python_highlight_space_errors = 1
  106. endif
  107. if !exists("python_highlight_doctests")
  108. let python_highlight_doctests = 1
  109. endif
  110. endif
  111. " Keywords
  112. syn keyword pythonStatement break continue del
  113. syn keyword pythonStatement exec return
  114. syn keyword pythonStatement pass raise
  115. syn keyword pythonStatement global assert
  116. syn keyword pythonStatement lambda yield
  117. syn keyword pythonStatement with
  118. syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
  119. syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained
  120. syn keyword pythonRepeat for while
  121. syn keyword pythonConditional if elif else
  122. syn keyword pythonPreCondit import from as
  123. syn keyword pythonException try except finally
  124. syn keyword pythonOperator and in is not or
  125. if !exists("python_print_as_function") || python_print_as_function == 0
  126. syn keyword pythonStatement print
  127. endif
  128. " Decorators (new in Python 2.4)
  129. syn match pythonDecorator "@" display nextgroup=pythonDottedName skipwhite
  130. syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
  131. syn match pythonDot "\." display containedin=pythonDottedName
  132. " Comments
  133. syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
  134. syn match pythonRun "\%^#!.*$"
  135. syn match pythonCoding "\%^.*\(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
  136. syn keyword pythonTodo TODO FIXME XXX contained
  137. " Errors
  138. syn match pythonError "\<\d\+\D\+\>" display
  139. syn match pythonError "[$?]" display
  140. syn match pythonError "[&|]\{2,}" display
  141. syn match pythonError "[=]\{3,}" display
  142. " TODO: Mixing spaces and tabs also may be used for pretty formatting multiline
  143. " statements. For now I don't know how to work around this.
  144. if exists("python_highlight_indent_errors") && python_highlight_indent_errors != 0
  145. syn match pythonIndentError "^\s*\( \t\|\t \)\s*\S"me=e-1 display
  146. endif
  147. " Trailing space errors
  148. if exists("python_highlight_space_errors") && python_highlight_space_errors != 0
  149. syn match pythonSpaceError "\s\+$" display
  150. endif
  151. " Strings
  152. syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
  153. syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
  154. syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  155. syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell
  156. syn match pythonEscape +\\[abfnrtv'"\\]+ display contained
  157. syn match pythonEscape "\\\o\o\=\o\=" display contained
  158. syn match pythonEscapeError "\\\o\{,2}[89]" display contained
  159. syn match pythonEscape "\\x\x\{2}" display contained
  160. syn match pythonEscapeError "\\x\x\=\X" display contained
  161. syn match pythonEscape "\\$"
  162. " Unicode strings
  163. syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
  164. syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
  165. syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  166. syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  167. syn match pythonUniEscape "\\u\x\{4}" display contained
  168. syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
  169. syn match pythonUniEscape "\\U\x\{8}" display contained
  170. syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
  171. syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
  172. syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
  173. " Raw strings
  174. syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  175. syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  176. syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  177. syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  178. syn match pythonRawEscape +\\['"]+ display transparent contained
  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. syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
  185. syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
  186. if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
  187. " String formatting
  188. syn match pythonStrFormatting "%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  189. syn match pythonStrFormatting "%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  190. endif
  191. if exists("python_highlight_string_format") && python_highlight_string_format != 0
  192. " str.format syntax
  193. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  194. 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
  195. endif
  196. if exists("python_highlight_string_templates") && python_highlight_string_templates != 0
  197. " String templates
  198. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  199. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  200. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  201. endif
  202. if exists("python_highlight_doctests") && python_highlight_doctests != 0
  203. " DocTests
  204. syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
  205. syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
  206. endif
  207. " Numbers (ints, longs, floats, complex)
  208. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*[lL]\=\>" display
  209. syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display
  210. syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
  211. syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
  212. syn match pythonNumber "\<\d\+[lLjJ]\=\>" display
  213. syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display
  214. syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
  215. syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display
  216. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
  217. syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
  218. if exists("python_highlight_builtin_objs") && python_highlight_builtin_objs != 0
  219. " Builtin objects and types
  220. syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented
  221. syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
  222. endif
  223. if exists("python_highlight_builtin_funcs") && python_highlight_builtin_funcs != 0
  224. " Builtin functions
  225. syn keyword pythonBuiltinFunc __import__ abs all any apply
  226. syn keyword pythonBuiltinFunc basestring bin bool buffer bytearray bytes callable
  227. syn keyword pythonBuiltinFunc chr classmethod cmp coerce compile complex
  228. syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval
  229. syn keyword pythonBuiltinFunc execfile file filter float format frozenset getattr
  230. syn keyword pythonBuiltinFunc globals hasattr hash help hex id
  231. syn keyword pythonBuiltinFunc input int intern isinstance
  232. syn keyword pythonBuiltinFunc issubclass iter len list locals long map max
  233. syn keyword pythonBuiltinFunc min next object oct open ord
  234. syn keyword pythonBuiltinFunc pow property range
  235. syn keyword pythonBuiltinFunc raw_input reduce reload repr
  236. syn keyword pythonBuiltinFunc reversed round set setattr
  237. syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple
  238. syn keyword pythonBuiltinFunc type unichr unicode vars xrange zip
  239. if exists("python_print_as_function") && python_print_as_function != 0
  240. syn keyword pythonBuiltinFunc print
  241. endif
  242. endif
  243. if exists("python_highlight_exceptions") && python_highlight_exceptions != 0
  244. " Builtin exceptions and warnings
  245. syn keyword pythonExClass BaseException
  246. syn keyword pythonExClass Exception StandardError ArithmeticError
  247. syn keyword pythonExClass LookupError EnvironmentError
  248. syn keyword pythonExClass AssertionError AttributeError BufferError EOFError
  249. syn keyword pythonExClass FloatingPointError GeneratorExit IOError
  250. syn keyword pythonExClass ImportError IndexError KeyError
  251. syn keyword pythonExClass KeyboardInterrupt MemoryError NameError
  252. syn keyword pythonExClass NotImplementedError OSError OverflowError
  253. syn keyword pythonExClass ReferenceError RuntimeError StopIteration
  254. syn keyword pythonExClass SyntaxError IndentationError TabError
  255. syn keyword pythonExClass SystemError SystemExit TypeError
  256. syn keyword pythonExClass UnboundLocalError UnicodeError
  257. syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError
  258. syn keyword pythonExClass UnicodeTranslateError ValueError VMSError
  259. syn keyword pythonExClass WindowsError ZeroDivisionError
  260. syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning
  261. syn keyword pythonExClass PendingDepricationWarning SyntaxWarning
  262. syn keyword pythonExClass RuntimeWarning FutureWarning
  263. syn keyword pythonExClass ImportWarning UnicodeWarning
  264. endif
  265. if exists("python_slow_sync") && python_slow_sync != 0
  266. syn sync minlines=2000
  267. else
  268. " This is fast but code inside triple quoted strings screws it up. It
  269. " is impossible to fix because the only way to know if you are inside a
  270. " triple quoted string is to start from the beginning of the file.
  271. syn sync match pythonSync grouphere NONE "):$"
  272. syn sync maxlines=200
  273. endif
  274. if version >= 508 || !exists("did_python_syn_inits")
  275. if version <= 508
  276. let did_python_syn_inits = 1
  277. command -nargs=+ HiLink hi link <args>
  278. else
  279. command -nargs=+ HiLink hi def link <args>
  280. endif
  281. HiLink pythonStatement Statement
  282. HiLink pythonPreCondit Statement
  283. HiLink pythonFunction Function
  284. HiLink pythonConditional Conditional
  285. HiLink pythonRepeat Repeat
  286. HiLink pythonException Exception
  287. HiLink pythonOperator Operator
  288. HiLink pythonDecorator Define
  289. HiLink pythonDottedName Function
  290. HiLink pythonDot Normal
  291. HiLink pythonComment Comment
  292. HiLink pythonCoding Special
  293. HiLink pythonRun Special
  294. HiLink pythonTodo Todo
  295. HiLink pythonError Error
  296. HiLink pythonIndentError Error
  297. HiLink pythonSpaceError Error
  298. HiLink pythonString String
  299. HiLink pythonUniString String
  300. HiLink pythonRawString String
  301. HiLink pythonUniRawString String
  302. HiLink pythonEscape Special
  303. HiLink pythonEscapeError Error
  304. HiLink pythonUniEscape Special
  305. HiLink pythonUniEscapeError Error
  306. HiLink pythonUniRawEscape Special
  307. HiLink pythonUniRawEscapeError Error
  308. HiLink pythonStrFormatting Special
  309. HiLink pythonStrFormat Special
  310. HiLink pythonStrTemplate Special
  311. HiLink pythonDocTest Special
  312. HiLink pythonDocTest2 Special
  313. HiLink pythonNumber Number
  314. HiLink pythonHexNumber Number
  315. HiLink pythonOctNumber Number
  316. HiLink pythonBinNumber Number
  317. HiLink pythonFloat Float
  318. HiLink pythonOctError Error
  319. HiLink pythonHexError Error
  320. HiLink pythonBinError Error
  321. HiLink pythonBuiltinObj Structure
  322. HiLink pythonBuiltinFunc Function
  323. HiLink pythonExClass Structure
  324. delcommand HiLink
  325. endif
  326. let b:current_syntax = "python"