python.vim 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. " Vim syntax file
  2. " Language: Python
  3. " Maintainer: Neil Schemenauer <nas@python.ca>
  4. " Last Change: 2014 Jul 16
  5. " Credits: Zvezdan Petkovic <zpetkovic@acm.org>
  6. " Neil Schemenauer <nas@python.ca>
  7. " Dmitry Vasiliev
  8. "
  9. " This version is a major rewrite by Zvezdan Petkovic.
  10. "
  11. " - introduced highlighting of doctests
  12. " - updated keywords, built-ins, and exceptions
  13. " - corrected regular expressions for
  14. "
  15. " * functions
  16. " * decorators
  17. " * strings
  18. " * escapes
  19. " * numbers
  20. " * space error
  21. "
  22. " - corrected synchronization
  23. " - more highlighting is ON by default, except
  24. " - space error highlighting is OFF by default
  25. "
  26. " Optional highlighting can be controlled using these variables.
  27. "
  28. " let python_no_builtin_highlight = 1
  29. " let python_no_doctest_code_highlight = 1
  30. " let python_no_doctest_highlight = 1
  31. " let python_no_exception_highlight = 1
  32. " let python_no_number_highlight = 1
  33. " let python_space_error_highlight = 1
  34. " let python_shebang_header_highlight = 1
  35. " let python_coding_header_highlight = 1
  36. "
  37. " All the options above can be switched on together.
  38. "
  39. " let python_highlight_all = 1
  40. "
  41. " For version 5.x: Clear all syntax items.
  42. " For version 6.x: Quit when a syntax file was already loaded.
  43. if version < 600
  44. syntax clear
  45. elseif exists("b:current_syntax")
  46. finish
  47. endif
  48. " We need nocompatible mode in order to continue lines with backslashes.
  49. " Original setting will be restored.
  50. let s:cpo_save = &cpo
  51. set cpo&vim
  52. " Keep Python keywords in alphabetical order inside groups for easy
  53. " comparison with the table in the 'Python Language Reference'
  54. " http://docs.python.org/reference/lexical_analysis.html#keywords.
  55. " Groups are in the order presented in NAMING CONVENTIONS in syntax.txt.
  56. " Exceptions come last at the end of each group (class and def below).
  57. "
  58. " Keywords 'with' and 'as' are new in Python 2.6
  59. " (use 'from __future__ import with_statement' in Python 2.5).
  60. "
  61. " Some compromises had to be made to support both Python 3.0 and 2.6.
  62. " We include Python 3.0 features, but when a definition is duplicated,
  63. " the last definition takes precedence.
  64. "
  65. " - 'False', 'None', and 'True' are keywords in Python 3.0 but they are
  66. " built-ins in 2.6 and will be highlighted as built-ins below.
  67. " - 'exec' is a built-in in Python 3.0 and will be highlighted as
  68. " built-in below.
  69. " - 'nonlocal' is a keyword in Python 3.0 and will be highlighted.
  70. " - 'print' is a built-in in Python 3.0 and will be highlighted as
  71. " built-in below (use 'from __future__ import print_function' in 2.6)
  72. "
  73. syn keyword pythonStatement False, None, True
  74. syn keyword pythonStatement as assert break continue del exec global
  75. syn keyword pythonStatement lambda nonlocal pass print return with yield
  76. syn keyword pythonStatement class def nextgroup=pythonFunction skipwhite
  77. syn keyword pythonConditional elif else if
  78. syn keyword pythonRepeat for while
  79. syn keyword pythonOperator and in is not or
  80. syn keyword pythonException except finally raise try
  81. syn keyword pythonInclude from import
  82. " Decorators (new in Python 2.4)
  83. syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite
  84. " The zero-length non-grouping match before the function name is
  85. " extremely important in pythonFunction. Without it, everything is
  86. " interpreted as a function inside the contained environment of
  87. " doctests.
  88. " A dot must be allowed because of @MyClass.myfunc decorators.
  89. syn match pythonFunction
  90. \ "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained
  91. " Add spelling to comments with some exceptions
  92. syn match pythonComment "#.*$" contains=pythonTodo,@Spell
  93. " Skip for PyLint's in-line comment rules
  94. syn match pythonComment "# pylint:.*$" contains=@NoSpell
  95. " Skip noqa inline comments
  96. syn match pythonComment "# noqa.*$" contains=@NoSpell
  97. syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
  98. " Triple-quoted strings can contain doctests.
  99. syn region pythonString
  100. \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
  101. \ contains=pythonEscape,@Spell
  102. syn region pythonString
  103. \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
  104. \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
  105. syn region pythonRawString
  106. \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
  107. \ contains=@Spell
  108. syn region pythonRawString
  109. \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
  110. \ contains=pythonSpaceError,pythonDoctest,@Spell
  111. syn match pythonEscape +\\[abfnrtv'"\\]+ contained
  112. syn match pythonEscape "\\\o\{1,3}" contained
  113. syn match pythonEscape "\\x\x\{2}" contained
  114. syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
  115. " Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
  116. syn match pythonEscape "\\N{.\{-}}" contained
  117. syn match pythonEscape "\\$"
  118. if exists("python_highlight_all")
  119. if exists("python_no_builtin_highlight")
  120. unlet python_no_builtin_highlight
  121. endif
  122. if exists("python_no_doctest_code_highlight")
  123. unlet python_no_doctest_code_highlight
  124. endif
  125. if exists("python_no_doctest_highlight")
  126. unlet python_no_doctest_highlight
  127. endif
  128. if exists("python_no_exception_highlight")
  129. unlet python_no_exception_highlight
  130. endif
  131. if exists("python_no_number_highlight")
  132. unlet python_no_number_highlight
  133. endif
  134. let python_space_error_highlight = 1
  135. let python_shebang_header_highlight = 1
  136. let python_coding_header_highlight = 1
  137. endif
  138. " It is very important to understand all details before changing the
  139. " regular expressions below or their order.
  140. " The word boundaries are *not* the floating-point number boundaries
  141. " because of a possible leading or trailing decimal point.
  142. " The expressions below ensure that all valid number literals are
  143. " highlighted, and invalid number literals are not. For example,
  144. "
  145. " - a decimal point in '4.' at the end of a line is highlighted,
  146. " - a second dot in 1.0.0 is not highlighted,
  147. " - 08 is not highlighted,
  148. " - 08e0 or 08j are highlighted,
  149. "
  150. " and so on, as specified in the 'Python Language Reference'.
  151. " http://docs.python.org/reference/lexical_analysis.html#numeric-literals
  152. if !exists("python_no_number_highlight")
  153. " numbers (including longs and complex)
  154. syn match pythonNumber "\<0[oO]\=\o\+[Ll]\=\>"
  155. syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>"
  156. syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>"
  157. syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>"
  158. syn match pythonNumber "\<\d\+[jJ]\>"
  159. syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>"
  160. syn match pythonNumber
  161. \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="
  162. syn match pythonNumber
  163. \ "\%(^\|\W\)\@<=\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"
  164. endif
  165. " Group the built-ins in the order in the 'Python Library Reference' for
  166. " easier comparison.
  167. " http://docs.python.org/library/constants.html
  168. " http://docs.python.org/library/functions.html
  169. " http://docs.python.org/library/functions.html#non-essential-built-in-functions
  170. " Python built-in functions are in alphabetical order.
  171. if !exists("python_no_builtin_highlight")
  172. " built-in constants
  173. " 'False', 'True', and 'None' are also reserved words in Python 3.0
  174. syn keyword pythonBuiltin False True None
  175. syn keyword pythonBuiltin NotImplemented Ellipsis __debug__
  176. " built-in functions
  177. syn keyword pythonBuiltin abs all any bin bool chr classmethod
  178. syn keyword pythonBuiltin compile complex delattr dict dir divmod
  179. syn keyword pythonBuiltin enumerate eval filter float format
  180. syn keyword pythonBuiltin frozenset getattr globals hasattr hash
  181. syn keyword pythonBuiltin help hex id input int isinstance
  182. syn keyword pythonBuiltin issubclass iter len list locals map max
  183. syn keyword pythonBuiltin min next object oct open ord pow print
  184. syn keyword pythonBuiltin property range repr reversed round set
  185. syn keyword pythonBuiltin setattr slice sorted staticmethod str
  186. syn keyword pythonBuiltin sum super tuple type vars zip __import__
  187. " Python 2.6 only
  188. syn keyword pythonBuiltin basestring callable cmp execfile file
  189. syn keyword pythonBuiltin long raw_input reduce reload unichr
  190. syn keyword pythonBuiltin unicode xrange
  191. " Python 3.0 only
  192. syn keyword pythonBuiltin ascii bytearray bytes exec memoryview
  193. " non-essential built-in functions; Python 2.6 only
  194. syn keyword pythonBuiltin apply buffer coerce intern
  195. endif
  196. " From the 'Python Library Reference' class hierarchy at the bottom.
  197. " http://docs.python.org/library/exceptions.html
  198. if !exists("python_no_exception_highlight")
  199. " builtin base exceptions (only used as base classes for other exceptions)
  200. syn keyword pythonExceptions BaseException Exception
  201. syn keyword pythonExceptions ArithmeticError EnvironmentError
  202. syn keyword pythonExceptions LookupError
  203. " builtin base exception removed in Python 3.0
  204. syn keyword pythonExceptions StandardError
  205. " builtin exceptions (actually raised)
  206. syn keyword pythonExceptions AssertionError AttributeError BufferError
  207. syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit
  208. syn keyword pythonExceptions IOError ImportError IndentationError
  209. syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt
  210. syn keyword pythonExceptions MemoryError NameError NotImplementedError
  211. syn keyword pythonExceptions OSError OverflowError ReferenceError
  212. syn keyword pythonExceptions RuntimeError StopIteration SyntaxError
  213. syn keyword pythonExceptions SystemError SystemExit TabError TypeError
  214. syn keyword pythonExceptions UnboundLocalError UnicodeError
  215. syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError
  216. syn keyword pythonExceptions UnicodeTranslateError ValueError VMSError
  217. syn keyword pythonExceptions WindowsError ZeroDivisionError
  218. " builtin warnings
  219. syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning
  220. syn keyword pythonExceptions ImportWarning PendingDeprecationWarning
  221. syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning
  222. syn keyword pythonExceptions UserWarning Warning
  223. endif
  224. if exists("python_space_error_highlight")
  225. " trailing whitespace
  226. syn match pythonSpaceError display excludenl "\s\+$"
  227. " mixed tabs and spaces
  228. syn match pythonSpaceError display " \+\t"
  229. syn match pythonSpaceError display "\t\+ "
  230. endif
  231. if exists("python_shebang_header_highlight")
  232. syn match pythonRun "\%^#!.*$" contains=@NoSpell
  233. endif
  234. if exists("python_coding_header_highlight")
  235. syn match pythonCoding "\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$" contains=@NoSpell
  236. endif
  237. " Do not spell doctests inside strings.
  238. " Notice that the end of a string, either ''', or """, will end the contained
  239. " doctest too. Thus, we do *not* need to have it as an end pattern.
  240. if !exists("python_no_doctest_highlight")
  241. if !exists("python_no_doctest_code_highlight")
  242. syn region pythonDoctest
  243. \ start="^\s*>>>\s" end="^\s*$"
  244. \ contained contains=ALLBUT,pythonDoctest,@Spell
  245. syn region pythonDoctestValue
  246. \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
  247. \ contained
  248. else
  249. syn region pythonDoctest
  250. \ start="^\s*>>>" end="^\s*$"
  251. \ contained contains=@NoSpell
  252. endif
  253. endif
  254. " Sync at the beginning of class, function, or method definition.
  255. syn sync match pythonSync grouphere NONE "^\s*\%(def\|class\)\s\+\h\w*\s*("
  256. if version >= 508 || !exists("did_python_syn_inits")
  257. if version <= 508
  258. let did_python_syn_inits = 1
  259. command -nargs=+ HiLink hi link <args>
  260. else
  261. command -nargs=+ HiLink hi def link <args>
  262. endif
  263. " The default highlight links. Can be overridden later.
  264. HiLink pythonStatement Statement
  265. HiLink pythonConditional Conditional
  266. HiLink pythonRepeat Repeat
  267. HiLink pythonOperator Operator
  268. HiLink pythonException Exception
  269. HiLink pythonInclude Include
  270. HiLink pythonDecorator Define
  271. HiLink pythonFunction Function
  272. HiLink pythonComment Comment
  273. if exists("python_shebang_header_highlight")
  274. HiLink pythonRun Special
  275. endif
  276. if exists("python_coding_header_highlight")
  277. HiLink pythonCoding Special
  278. endif
  279. HiLink pythonTodo Todo
  280. HiLink pythonString String
  281. HiLink pythonRawString String
  282. HiLink pythonEscape Special
  283. if !exists("python_no_number_highlight")
  284. HiLink pythonNumber Number
  285. endif
  286. if !exists("python_no_builtin_highlight")
  287. HiLink pythonBuiltin Function
  288. endif
  289. if !exists("python_no_exception_highlight")
  290. HiLink pythonExceptions Structure
  291. endif
  292. if exists("python_space_error_highlight")
  293. HiLink pythonSpaceError Error
  294. endif
  295. if !exists("python_no_doctest_highlight")
  296. HiLink pythonDoctest Special
  297. HiLink pythonDoctestValue Define
  298. endif
  299. delcommand HiLink
  300. endif
  301. let b:current_syntax = "python"
  302. let &cpo = s:cpo_save
  303. unlet s:cpo_save
  304. " vim:set sw=2 sts=2 ts=8 noet: