python.vim 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. " Vim syntax file
  2. " Language: Python
  3. " Current Maintainer: Dmitry Vasiliev <dima at hlabs dot org>
  4. " Previous Maintainer: Neil Schemenauer <nas at python dot ca>
  5. " URL: https://github.com/hdima/python-syntax
  6. " Last Change: 2015-11-01
  7. " Filenames: *.py
  8. " Version: 3.6.0
  9. "
  10. " Based on python.vim (from Vim 6.1 distribution)
  11. " by Neil Schemenauer <nas at python dot ca>
  12. "
  13. " Please use the following channels for reporting bugs, offering suggestions or
  14. " feedback:
  15. " - python.vim issue tracker: https://github.com/hdima/python-syntax/issues
  16. " - Email: Dmitry Vasiliev (dima at hlabs.org)
  17. " - Send a message or follow me for updates on Twitter: `@hdima
  18. " <https://twitter.com/hdima>`__
  19. "
  20. " Contributors
  21. " ============
  22. "
  23. " List of the contributors in alphabetical order:
  24. "
  25. " Andrea Riciputi
  26. " Anton Butanaev
  27. " Antony Lee
  28. " Caleb Adamantine
  29. " David Briscoe
  30. " Elizabeth Myers
  31. " Ihor Gorobets
  32. " Jeroen Ruigrok van der Werven
  33. " John Eikenberry
  34. " Joongi Kim
  35. " Marc Weber
  36. " Pedro Algarvio
  37. " Victor Salgado
  38. " Will Gray
  39. " Yuri Habrusiev
  40. "
  41. " Options
  42. " =======
  43. "
  44. " :let OPTION_NAME = 1 Enable option
  45. " :let OPTION_NAME = 0 Disable option
  46. "
  47. "
  48. " Option to select Python version
  49. " -------------------------------
  50. "
  51. " python_version_2 Enable highlighting for Python 2
  52. " (Python 3 highlighting is enabled
  53. " by default). Can also be set as
  54. " a buffer (b:python_version_2)
  55. " variable.
  56. "
  57. " You can also use the following local to buffer commands to switch
  58. " between two highlighting modes:
  59. "
  60. " :Python2Syntax Switch to Python 2 highlighting
  61. " mode
  62. " :Python3Syntax Switch to Python 3 highlighting
  63. " mode
  64. "
  65. " Option names used by the script
  66. " -------------------------------
  67. "
  68. " python_highlight_builtins Highlight builtin functions and
  69. " objects
  70. " python_highlight_builtin_objs Highlight builtin objects only
  71. " python_highlight_builtin_funcs Highlight builtin functions only
  72. " python_highlight_exceptions Highlight standard exceptions
  73. " python_highlight_string_formatting Highlight % string formatting
  74. " python_highlight_string_format Highlight str.format syntax
  75. " python_highlight_string_templates Highlight string.Template syntax
  76. " python_highlight_indent_errors Highlight indentation errors
  77. " python_highlight_space_errors Highlight trailing spaces
  78. " python_highlight_doctests Highlight doc-tests
  79. " python_print_as_function Highlight 'print' statement as
  80. " function for Python 2
  81. " python_highlight_file_headers_as_comments
  82. " Highlight shebang and coding
  83. " headers as comments
  84. "
  85. " python_highlight_all Enable all the options above
  86. " NOTE: This option don't override
  87. " any previously set options
  88. "
  89. " python_slow_sync Can be set to 0 for slow machines
  90. "
  91. " For version 5.x: Clear all syntax items
  92. " For versions greater than 6.x: Quit when a syntax file was already loaded
  93. if version < 600
  94. syntax clear
  95. elseif exists("b:current_syntax")
  96. finish
  97. endif
  98. "
  99. " Commands
  100. "
  101. command! -buffer Python2Syntax let b:python_version_2 = 1 | let &syntax=&syntax
  102. command! -buffer Python3Syntax let b:python_version_2 = 0 | let &syntax=&syntax
  103. " Enable option if it's not defined
  104. function! s:EnableByDefault(name)
  105. if !exists(a:name)
  106. let {a:name} = 1
  107. endif
  108. endfunction
  109. " Check if option is enabled
  110. function! s:Enabled(name)
  111. return exists(a:name) && {a:name}
  112. endfunction
  113. " Is it Python 2 syntax?
  114. function! s:Python2Syntax()
  115. if exists("b:python_version_2")
  116. return b:python_version_2
  117. endif
  118. return s:Enabled("g:python_version_2")
  119. endfunction
  120. "
  121. " Default options
  122. "
  123. call s:EnableByDefault("g:python_slow_sync")
  124. if s:Enabled("g:python_highlight_all")
  125. call s:EnableByDefault("g:python_highlight_builtins")
  126. if s:Enabled("g:python_highlight_builtins")
  127. call s:EnableByDefault("g:python_highlight_builtin_objs")
  128. call s:EnableByDefault("g:python_highlight_builtin_funcs")
  129. endif
  130. call s:EnableByDefault("g:python_highlight_type_annotations")
  131. call s:EnableByDefault("g:python_highlight_exceptions")
  132. call s:EnableByDefault("g:python_highlight_string_formatting")
  133. call s:EnableByDefault("g:python_highlight_string_format")
  134. call s:EnableByDefault("g:python_highlight_string_templates")
  135. call s:EnableByDefault("g:python_highlight_indent_errors")
  136. call s:EnableByDefault("g:python_highlight_space_errors")
  137. call s:EnableByDefault("g:python_highlight_doctests")
  138. call s:EnableByDefault("g:python_print_as_function")
  139. endif
  140. "
  141. " Keywords
  142. "
  143. syn keyword pythonStatement break continue del
  144. if s:Python2Syntax()
  145. syn keyword pythonStatement exec
  146. endif
  147. syn keyword pythonStatement return
  148. syn keyword pythonStatement pass raise
  149. syn keyword pythonStatement global assert
  150. syn keyword pythonStatement with
  151. syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
  152. syn keyword pythonRepeat for while
  153. syn keyword pythonConditional if elif else
  154. " The standard pyrex.vim unconditionally removes the pythonInclude group, so
  155. " we provide a dummy group here to avoid crashing pyrex.vim.
  156. syn keyword pythonInclude import
  157. syn keyword pythonImport import
  158. syn match pythonIdentifier "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" nextgroup=pythonFuncArgs,pythonTypeAnno display
  159. syn keyword pythonException try except finally
  160. syn keyword pythonOperator and in is not or
  161. syn match pythonLambdaExpr "\<lambda[^:]*:"he=s+6 contains=@pythonExpression nextgroup=@pythonExpression
  162. syn region pythonDictSetExpr matchgroup=pythonDictSetExpr start='{' end='}' contains=@pythonExpression
  163. syn region pythonListSliceExpr matchgroup=pythonListSliceExpr start='\[' end='\]' contains=@pythonExpression
  164. syn region pythonFuncArgs matchgroup=pythonFuncArgs start='(' end=')' contained contains=pythonTypeAnno,@pythonExpression
  165. if !s:Python2Syntax()
  166. if s:Enabled("g:python_highlight_type_annotations")
  167. syn match pythonTypeAnno @:\s*['"]\?\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*['"]\?@hs=s+1 display contained contains=pythonType,pythonString nextgroup=pythonTypeArgs
  168. syn match pythonTypeAnnoReturn @->\s*['"]\?\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*['"]\?@ display contains=pythonType,pythonString nextgroup=pythonTypeArgs
  169. syn region pythonTypeArgs matchgroup=pythonTypeArgs start='\[' end='\]' display contained contains=pythonType,pythonString,pythonTypeArgs
  170. syn keyword pythonType Any AnyStr Callable ClassVar Tuple Union Optional Type TypeVar None contained
  171. syn keyword pythonType AbstractSet MutableSet Mapping MutableMapping Sequence MutableSequence ByteString Deque List contained
  172. syn keyword pythonType Set FrozenSet MappingView KeysView ItemsView ValuesView Awaitable Coroutine AsyncIterable contained
  173. syn keyword pythonType AsyncIterator ContextManager Dict DefaultDict Generator AsyncGenerator Text NamedTuple contained
  174. syn keyword pythonType Iterable Iterator Reversible SupportsInt SupportsFloat SupportsAbs SupportsRound Container contained
  175. syn keyword pythonType Hashable Sized Collection contained
  176. endif
  177. endif
  178. syn match pythonStatement "\<yield\>" display
  179. syn match pythonImport "\<from\>" display
  180. if s:Python2Syntax()
  181. if !s:Enabled("g:python_print_as_function")
  182. syn keyword pythonStatement print
  183. endif
  184. syn keyword pythonImport as
  185. syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained
  186. else
  187. syn keyword pythonStatement as nonlocal
  188. syn match pythonStatement "\<yield\s\+from\>" display
  189. syn keyword pythonBuiltinObj None True False
  190. syn match pythonFunction "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" nextgroup=pythonFuncArgs display contained
  191. syn keyword pythonStatement await async
  192. syn match pythonStatement "\<async\s\+def\>" display nextgroup=pythonFunction skipwhite
  193. syn match pythonStatement "\<async\s\+with\>" display
  194. syn match pythonStatement "\<async\s\+for\>" display
  195. endif
  196. syn cluster pythonExpression contains=
  197. \ pythonFuncArgs,
  198. \ pythonDictSetExpr,
  199. \ pythonListSliceExpr,
  200. \ pythonLambdaExpr,
  201. \ pythonStatement,
  202. \ pythonRepeat,
  203. \ pythonConditional,
  204. \ pythonComment,
  205. \ pythonOperator,
  206. \ pythonNumber,
  207. \ pythonNumberError,
  208. \ pythonFloat,
  209. \ pythonHexNumber,
  210. \ pythonOctNumber,
  211. \ pythonBytes,
  212. \ pythonString,
  213. \ pythonRawString,
  214. \ pythonUniString,
  215. \ pythonUniRawString,
  216. \ pythonFString,
  217. \ pythonExClass,
  218. \ pythonBuiltinObj,
  219. \ pythonBuiltinFunc
  220. syn cluster pythonFExpression contains=
  221. \ pythonStatement,
  222. \ pythonDictSetExpr,
  223. \ pythonListSliceExpr,
  224. \ pythonLambdaExpr,
  225. \ pythonRepeat,
  226. \ pythonConditional,
  227. \ pythonOperator,
  228. \ pythonNumber,
  229. \ pythonHexNumber,
  230. \ pythonOctNumber,
  231. \ pythonBinNumber,
  232. \ pythonFloat,
  233. \ pythonString,
  234. \ pythonBytes,
  235. \ pythonBuiltinObj,
  236. \ pythonBuiltinFunc
  237. "
  238. " Decorators (new in Python 2.4)
  239. "
  240. syn match pythonDecorator "^\s*\zs@" display nextgroup=pythonDottedName skipwhite
  241. if s:Python2Syntax()
  242. syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
  243. else
  244. syn match pythonDottedName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*" display contained
  245. endif
  246. syn match pythonDot "\." display containedin=pythonDottedName
  247. "
  248. " Comments
  249. "
  250. syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
  251. if !s:Enabled("g:python_highlight_file_headers_as_comments")
  252. syn match pythonRun "\%^#!.*$"
  253. syn match pythonCoding "\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
  254. endif
  255. syn keyword pythonTodo TODO FIXME XXX contained
  256. "
  257. " Errors
  258. "
  259. syn match pythonError "\<\d\+\D\+\>" display
  260. syn match pythonError "[$?]" display
  261. syn match pythonError "[&|]\{2,}" display
  262. syn match pythonError "[=]\{3,}" display
  263. " Mixing spaces and tabs also may be used for pretty formatting multiline
  264. " statements
  265. if s:Enabled("g:python_highlight_indent_errors")
  266. syn match pythonIndentError "^\s*\%( \t\|\t \)\s*\S"me=e-1 display
  267. endif
  268. " Trailing space errors
  269. if s:Enabled("g:python_highlight_space_errors")
  270. syn match pythonSpaceError "\s\+$" display
  271. endif
  272. "
  273. " Strings
  274. "
  275. if s:Python2Syntax()
  276. " Python 2 strings
  277. syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  278. syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  279. syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  280. syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  281. else
  282. " Python 3 byte strings
  283. syn region pythonBytes start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
  284. syn region pythonBytes start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
  285. syn region pythonBytes start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell
  286. syn region pythonBytes start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell
  287. syn match pythonBytesError ".\+" display contained
  288. syn match pythonBytesContent "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError
  289. endif
  290. syn match pythonBytesEscape +\\[abfnrtv'"\\]+ display contained
  291. syn match pythonBytesEscape "\\\o\o\=\o\=" display contained
  292. syn match pythonBytesEscapeError "\\\o\{,2}[89]" display contained
  293. syn match pythonBytesEscape "\\x\x\{2}" display contained
  294. syn match pythonBytesEscapeError "\\x\x\=\X" display contained
  295. syn match pythonBytesEscape "\\$"
  296. syn match pythonUniEscape "\\u\x\{4}" display contained
  297. syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
  298. syn match pythonUniEscape "\\U\x\{8}" display contained
  299. syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
  300. syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
  301. syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
  302. if s:Python2Syntax()
  303. " Python 2 Unicode strings
  304. syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  305. syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  306. syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  307. syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  308. else
  309. " Python 3 strings
  310. syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  311. syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  312. syn region pythonString start=+"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  313. syn region pythonString start=+'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  314. syn region pythonFString start=+[fF]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  315. syn region pythonFString start=+[fF]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  316. syn region pythonFString start=+[fF]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  317. syn region pythonFString start=+[fF]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  318. endif
  319. if s:Python2Syntax()
  320. " Python 2 Unicode raw strings
  321. syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  322. syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  323. syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  324. syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell
  325. syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
  326. syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
  327. endif
  328. " Python 2/3 raw strings
  329. if s:Python2Syntax()
  330. syn region pythonRawString start=+[bB]\=[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  331. syn region pythonRawString start=+[bB]\=[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  332. syn region pythonRawString start=+[bB]\=[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  333. syn region pythonRawString start=+[bB]\=[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  334. else
  335. syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  336. syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  337. syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  338. syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  339. syn region pythonRawBytes start=+[bB][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  340. syn region pythonRawBytes start=+[bB][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  341. syn region pythonRawBytes start=+[bB][rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  342. syn region pythonRawBytes start=+[bB][rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  343. endif
  344. syn match pythonRawEscape +\\['"]+ display transparent contained
  345. if s:Enabled("g:python_highlight_string_formatting")
  346. " % operator string formatting
  347. if s:Python2Syntax()
  348. syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  349. syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  350. else
  351. syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
  352. syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
  353. endif
  354. endif
  355. if s:Enabled("g:python_highlight_string_format")
  356. " str.format syntax
  357. if s:Python2Syntax()
  358. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  359. syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  360. else
  361. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString
  362. syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString
  363. syn region pythonStrInterpRegion matchgroup=pythonStrInterpRegion start="{" end="}" extend contained containedin=pythonFString contains=@pythonFExpression
  364. endif
  365. endif
  366. if s:Enabled("g:python_highlight_string_templates")
  367. " string.Template format
  368. if s:Python2Syntax()
  369. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  370. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  371. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  372. else
  373. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonRawString
  374. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString
  375. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString
  376. endif
  377. endif
  378. if s:Enabled("g:python_highlight_doctests")
  379. " DocTests
  380. syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
  381. syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
  382. endif
  383. "
  384. " Numbers (ints, longs, floats, complex)
  385. "
  386. if s:Python2Syntax()
  387. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>" display
  388. syn match pythonOctError "\<0[oO]\=\o*\D\+\d*[lL]\=\>" display
  389. syn match pythonBinError "\<0[bB][01]*\D\+\d*[lL]\=\>" display
  390. syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display
  391. syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
  392. syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
  393. syn match pythonNumberError "\<\d\+\D[lL]\=\>" display
  394. syn match pythonNumber "\<\d[lL]\=\>" display
  395. syn match pythonNumber "\<[0-9]\d\+[lL]\=\>" display
  396. syn match pythonNumber "\<\d\+[lLjJ]\>" display
  397. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
  398. syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
  399. syn match pythonFloat "\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display
  400. syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
  401. syn match pythonFloat "\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display
  402. else
  403. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*\>" display
  404. syn match pythonOctError "\<0[oO]\=\o*\D\+\d*\>" display
  405. syn match pythonBinError "\<0[bB][01]*\D\+\d*\>" display
  406. syn match pythonHexNumber "\<0[xX][_0-9a-fA-F]*\x\>" display
  407. syn match pythonOctNumber "\<0[oO][_0-7]*\o\>" display
  408. syn match pythonBinNumber "\<0[bB][_01]*[01]\>" display
  409. syn match pythonNumberError "\<\d[_0-9]*\D\>" display
  410. syn match pythonNumberError "\<0[_0-9]\+\>" display
  411. syn match pythonNumberError "\<\d[_0-9]*_\>" display
  412. syn match pythonNumber "\<\d\>" display
  413. syn match pythonNumber "\<[1-9][_0-9]*\d\>" display
  414. syn match pythonNumber "\<\d[jJ]\>" display
  415. syn match pythonNumber "\<[1-9][_0-9]*\d[jJ]\>" display
  416. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*\>" display
  417. syn match pythonBinError "\<0[bB][01]*[2-9]\d*\>" display
  418. syn match pythonFloat "\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>" display
  419. syn match pythonFloat "\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>" display
  420. syn match pythonFloat "\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=" display
  421. endif
  422. "
  423. " Builtin objects and types
  424. "
  425. if s:Enabled("g:python_highlight_builtin_objs")
  426. if s:Python2Syntax()
  427. syn keyword pythonBuiltinObj None False True
  428. endif
  429. syn keyword pythonBuiltinObj Ellipsis NotImplemented self cls
  430. syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
  431. endif
  432. "
  433. " Builtin functions
  434. "
  435. if s:Enabled("g:python_highlight_builtin_funcs")
  436. if s:Python2Syntax()
  437. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(apply|basestring|buffer|callable|coerce)>\ze\(' nextgroup=pythonFuncArgs
  438. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(execfile|file|help|intern|long|raw_input)>\ze\(' nextgroup=pythonFuncArgs
  439. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(reduce|reload|unichr|unicode|xrange)>\ze\(' nextgroup=pythonFuncArgs
  440. if s:Enabled("g:python_print_as_function")
  441. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(print)>\ze\(' nextgroup=pythonFuncArgs
  442. endif
  443. else
  444. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(ascii|exec|memoryview|print)\ze\(' nextgroup=pythonFuncArgs
  445. endif
  446. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(__import__|abs|all|any)>\ze\(' nextgroup=pythonFuncArgs
  447. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(bin|bool|bytearray|bytes)>\ze\(' nextgroup=pythonFuncArgs
  448. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(chr|classmethod|cmp|compile|complex)>\ze\(' nextgroup=pythonFuncArgs
  449. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(delattr|dict|dir|divmod|enumerate|eval)>\ze\(' nextgroup=pythonFuncArgs
  450. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(filter|float|format|frozenset|getattr)>\ze\(' nextgroup=pythonFuncArgs
  451. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(globals|hasattr|hash|hex|id)>\ze\(' nextgroup=pythonFuncArgs
  452. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(input|int|isinstance)>\ze\(' nextgroup=pythonFuncArgs
  453. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(issubclass|iter|len|list|locals|map|max)>\ze\(' nextgroup=pythonFuncArgs
  454. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(min|next|object|oct|open|ord)>\ze\(' nextgroup=pythonFuncArgs
  455. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(pow|property|range)>\ze\(' nextgroup=pythonFuncArgs
  456. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(repr|reversed|round|set|setattr)>\ze\(' nextgroup=pythonFuncArgs
  457. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(slice|sorted|staticmethod|str|sum|super|tuple)>\ze\(' nextgroup=pythonFuncArgs
  458. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(type|vars|zip)>\ze\(' nextgroup=pythonFuncArgs
  459. endif
  460. "
  461. " Builtin exceptions and warnings
  462. "
  463. if s:Enabled("g:python_highlight_exceptions")
  464. if s:Python2Syntax()
  465. syn keyword pythonExClass StandardError nextgroup=pythonFuncArgs
  466. else
  467. syn keyword pythonExClass BlockingIOError ChildProcessError nextgroup=pythonFuncArgs
  468. syn keyword pythonExClass ConnectionError BrokenPipeError nextgroup=pythonFuncArgs
  469. syn keyword pythonExClass ConnectionAbortedError ConnectionRefusedError nextgroup=pythonFuncArgs
  470. syn keyword pythonExClass ConnectionResetError FileExistsError nextgroup=pythonFuncArgs
  471. syn keyword pythonExClass FileNotFoundError InterruptedError nextgroup=pythonFuncArgs
  472. syn keyword pythonExClass IsADirectoryError NotADirectoryError nextgroup=pythonFuncArgs
  473. syn keyword pythonExClass PermissionError ProcessLookupError TimeoutError nextgroup=pythonFuncArgs
  474. syn keyword pythonExClass ResourceWarning nextgroup=pythonFuncArgs
  475. endif
  476. syn keyword pythonExClass BaseException nextgroup=pythonFuncArgs
  477. syn keyword pythonExClass Exception ArithmeticError nextgroup=pythonFuncArgs
  478. syn keyword pythonExClass LookupError EnvironmentError nextgroup=pythonFuncArgs
  479. syn keyword pythonExClass AssertionError AttributeError BufferError EOFError nextgroup=pythonFuncArgs
  480. syn keyword pythonExClass FloatingPointError GeneratorExit IOError nextgroup=pythonFuncArgs
  481. syn keyword pythonExClass ImportError IndexError KeyError nextgroup=pythonFuncArgs
  482. syn keyword pythonExClass KeyboardInterrupt MemoryError NameError nextgroup=pythonFuncArgs
  483. syn keyword pythonExClass NotImplementedError OSError OverflowError nextgroup=pythonFuncArgs
  484. syn keyword pythonExClass ReferenceError RuntimeError StopIteration nextgroup=pythonFuncArgs
  485. syn keyword pythonExClass SyntaxError IndentationError TabError nextgroup=pythonFuncArgs
  486. syn keyword pythonExClass SystemError SystemExit TypeError nextgroup=pythonFuncArgs
  487. syn keyword pythonExClass UnboundLocalError UnicodeError nextgroup=pythonFuncArgs
  488. syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError nextgroup=pythonFuncArgs
  489. syn keyword pythonExClass UnicodeTranslateError ValueError VMSError nextgroup=pythonFuncArgs
  490. syn keyword pythonExClass WindowsError ZeroDivisionError nextgroup=pythonFuncArgs
  491. syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning nextgroup=pythonFuncArgs
  492. syn keyword pythonExClass PendingDepricationWarning SyntaxWarning nextgroup=pythonFuncArgs
  493. syn keyword pythonExClass RuntimeWarning FutureWarning nextgroup=pythonFuncArgs
  494. syn keyword pythonExClass ImportWarning UnicodeWarning nextgroup=pythonFuncArgs
  495. endif
  496. if s:Enabled("g:python_slow_sync")
  497. syn sync minlines=2000
  498. else
  499. " This is fast but code inside triple quoted strings screws it up. It
  500. " is impossible to fix because the only way to know if you are inside a
  501. " triple quoted string is to start from the beginning of the file.
  502. syn sync match pythonSync grouphere NONE "):$"
  503. syn sync maxlines=200
  504. endif
  505. if version >= 508 || !exists("did_python_syn_inits")
  506. if version <= 508
  507. let did_python_syn_inits = 1
  508. command -nargs=+ HiLink hi link <args>
  509. else
  510. command -nargs=+ HiLink hi def link <args>
  511. endif
  512. HiLink pythonStatement Statement
  513. HiLink pythonLambdaExpr Statement
  514. HiLink pythonImport Include
  515. HiLink pythonFunction Function
  516. HiLink pythonConditional Conditional
  517. HiLink pythonRepeat Repeat
  518. HiLink pythonException Exception
  519. HiLink pythonOperator Operator
  520. HiLink pythonDecorator Define
  521. HiLink pythonDottedName Function
  522. HiLink pythonDot Normal
  523. HiLink pythonComment Comment
  524. if !s:Enabled("g:python_highlight_file_headers_as_comments")
  525. HiLink pythonCoding Special
  526. HiLink pythonRun Special
  527. endif
  528. HiLink pythonTodo Todo
  529. HiLink pythonError Error
  530. HiLink pythonIndentError Error
  531. HiLink pythonSpaceError Error
  532. HiLink pythonString String
  533. HiLink pythonRawString String
  534. HiLink pythonUniEscape Special
  535. HiLink pythonUniEscapeError Error
  536. if s:Python2Syntax()
  537. HiLink pythonUniString String
  538. HiLink pythonUniRawString String
  539. HiLink pythonUniRawEscape Special
  540. HiLink pythonUniRawEscapeError Error
  541. else
  542. HiLink pythonBytes String
  543. HiLink pythonRawBytes String
  544. HiLink pythonBytesContent String
  545. HiLink pythonBytesError Error
  546. HiLink pythonBytesEscape Special
  547. HiLink pythonBytesEscapeError Error
  548. HiLink pythonFString String
  549. HiLink pythonStrInterpRegion Special
  550. endif
  551. HiLink pythonStrFormatting Special
  552. HiLink pythonStrFormat Special
  553. HiLink pythonStrTemplate Special
  554. HiLink pythonDocTest Special
  555. HiLink pythonDocTest2 Special
  556. HiLink pythonNumber Number
  557. HiLink pythonHexNumber Number
  558. HiLink pythonOctNumber Number
  559. HiLink pythonBinNumber Number
  560. HiLink pythonFloat Float
  561. HiLink pythonNumberError Error
  562. HiLink pythonOctError Error
  563. HiLink pythonHexError Error
  564. HiLink pythonBinError Error
  565. HiLink pythonBuiltinObj Structure
  566. HiLink pythonBuiltinFunc Function
  567. HiLink pythonExClass Structure
  568. HiLink pythonTypeAnno Optional
  569. HiLink pythonTypeAnnoReturn Optional
  570. HiLink pythonTypeArgs Optional
  571. HiLink pythonType Special
  572. delcommand HiLink
  573. " default style for custom highlight group (may be overriden in colors)
  574. hi Optional gui=italic cterm=italic
  575. endif
  576. let b:current_syntax = "python"