python.vim 15KB

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