python.vim 12KB

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