123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. " Vim syntax file
  2. " Language: Python
  3. " Maintainer: Neil Schemenauer <nas at python dot ca>
  4. " Updated: $Date: 2003/01/12 14:17:34 $
  5. " Updated by: Dmitry Vasiliev <dima at hlabs dot org>
  6. " Filenames: *.py
  7. " $Revision: 1.13 $
  8. "
  9. " Options:
  10. " For folded functions and classes:
  11. "
  12. " let python_folding = 1
  13. "
  14. " For highlighted builtin functions:
  15. "
  16. " let python_highlight_builtins = 1
  17. "
  18. " For highlighted standard exceptions:
  19. "
  20. " let python_highlight_exceptions = 1
  21. "
  22. " For highlighted string formatting:
  23. "
  24. " let python_highlight_string_formatting = 1
  25. "
  26. " If you want all possible Python highlighting:
  27. "
  28. " let python_highlight_all = 1
  29. "
  30. " TODO: Check more errors?
  31. " For version 5.x: Clear all syntax items
  32. " For version 6.x: Quit when a syntax file was already loaded
  33. if version < 600
  34. syntax clear
  35. elseif exists("b:current_syntax")
  36. finish
  37. endif
  38. if exists("python_highlight_all")
  39. let python_folding = 1
  40. let python_highlight_builtins = 1
  41. let python_highlight_exceptions = 1
  42. let python_highlight_string_formatting = 1
  43. endif
  44. " Keywords
  45. syn keyword pythonStatement break continue del
  46. syn keyword pythonStatement exec return
  47. syn keyword pythonStatement pass print raise
  48. syn keyword pythonStatement global assert
  49. syn keyword pythonStatement lambda yield
  50. if exists("python_folding") && has("folding")
  51. syn match pythonStatement "\<\(def\|class\)\>" display nextgroup=pythonFunction skipwhite
  52. else
  53. syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
  54. endif
  55. syn match pythonFunction "\h\w*" display contained
  56. syn keyword pythonRepeat for while
  57. syn keyword pythonConditional if elif else
  58. syn keyword pythonImport import from as
  59. syn keyword pythonException try except finally
  60. syn keyword pythonOperator and in is not or
  61. " Comments
  62. syn match pythonComment "#.*$" display contains=pythonTodo
  63. syn keyword pythonTodo TODO FIXME XXX contained
  64. " Erroneous characters that cannont be in a python program
  65. syn match pythonError "[@$?]" display
  66. " Mixing spaces and tabs is bad
  67. syn match pythonIndentError "^\s*\(\t \| \t\)\s*" display
  68. " Strings
  69. syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonEscape,pythonEscapeError
  70. syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonEscape,pythonEscapeError
  71. syn region pythonString start=+"""+ end=+"""+ contains=pythonEscape,pythonEscapeError
  72. syn region pythonString start=+'''+ end=+'''+ contains=pythonEscape,pythonEscapeError
  73. syn match pythonEscape +\\[abfnrtv'"\\]+ display contained
  74. syn match pythonEscapeError +\\[^abfnrtv'"\\]+ display contained
  75. syn match pythonEscape "\\\o\o\=\o\=" display contained
  76. syn match pythonEscapeError "\\\o\{,2}[89]" display contained
  77. syn match pythonEscape "\\x\x\{2}" display contained
  78. syn match pythonEscapeError "\\x\x\=\X" display contained
  79. syn match pythonEscape "\\$"
  80. " Unicode strings
  81. syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError
  82. syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError
  83. syn region pythonUniString start=+[uU]"""+ end=+"""+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError
  84. syn region pythonUniString start=+[uU]'''+ end=+'''+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError
  85. syn match pythonUniEscape "\\u\x\{4}" display contained
  86. syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
  87. syn match pythonUniEscape "\\U\x\{8}" display contained
  88. syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
  89. syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
  90. syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
  91. " Raw strings
  92. syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonRawEscape
  93. syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonRawEscape
  94. syn region pythonRawString start=+[rR]"""+ end=+"""+
  95. syn region pythonRawString start=+[rR]'''+ end=+'''+
  96. syn match pythonRawEscape +\\['"]+ display transparent contained
  97. " Unicode raw strings
  98. syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError
  99. syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError
  100. syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ contains=pythonUniRawEscape,pythonUniRawEscapeError
  101. syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ contains=pythonUniRawEscape,pythonUniRawEscapeError
  102. syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
  103. syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
  104. if exists("python_highlight_string_formatting")
  105. " String formatting
  106. syn match pythonStrFormat "%\(([^)]\+)\)\=[-#0 +]\=\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString
  107. syn match pythonStrFormat "%[-#0 +]\=\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString
  108. endif
  109. " Numbers (ints, longs, floats, complex)
  110. syn match pythonNumber "\<0[xX]\x\+[lL]\=\>" display
  111. syn match pythonNumber "\<\d\+[lLjJ]\=\>" display
  112. syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display
  113. syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
  114. syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display
  115. syn match pythonOctalError "\<0\o*[89]\d*[lLjJ]\=\>" display
  116. if exists("python_highlight_builtins")
  117. " Builtin functions, types and objects, not really part of the syntax
  118. syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented
  119. syn keyword pythonBuiltinFunc bool __import__ abs
  120. syn keyword pythonBuiltinFunc apply buffer callable chr classmethod cmp
  121. syn keyword pythonBuiltinFunc coerce compile complex delattr dict dir divmod
  122. syn keyword pythonBuiltinFunc eval execfile file filter float getattr globals
  123. syn keyword pythonBuiltinFunc hasattr hash hex id input int intern isinstance
  124. syn keyword pythonBuiltinFunc issubclass iter len list locals long map max
  125. syn keyword pythonBuiltinFunc min object oct open ord pow property range
  126. syn keyword pythonBuiltinFunc raw_input reduce reload repr round setattr
  127. syn keyword pythonBuiltinFunc slice staticmethod str super tuple type unichr
  128. syn keyword pythonBuiltinFunc unicode vars xrange zip
  129. endif
  130. if exists("python_highlight_exceptions")
  131. " Builtin exceptions and warnings
  132. syn keyword pythonExClass ArithmeticError AssertionError AttributeError
  133. syn keyword pythonExClass DeprecationWarning EOFError EnvironmentError
  134. syn keyword pythonExClass Exception FloatingPointError IOError
  135. syn keyword pythonExClass ImportError IndentiationError IndexError
  136. syn keyword pythonExClass KeyError KeyboardInterrupt LookupError
  137. syn keyword pythonExClass MemoryError NameError NotImplementedError
  138. syn keyword pythonExClass OSError OverflowError OverflowWarning
  139. syn keyword pythonExClass ReferenceError RuntimeError RuntimeWarning
  140. syn keyword pythonExClass StandardError StopIteration SyntaxError
  141. syn keyword pythonExClass SyntaxWarning SystemError SystemExit TabError
  142. syn keyword pythonExClass TypeError UnboundLocalError UnicodeError
  143. syn keyword pythonExClass UserWarning ValueError Warning WindowsError
  144. syn keyword pythonExClass ZeroDivisionError
  145. endif
  146. syn sync clear
  147. if exists("python_folding") && has("folding")
  148. syn sync fromstart
  149. "syn match pythonFold "^\(\s*\)\(class\|def\)\s.*\(\(\n\s*\)*\n\1\s\+\S.*\)\+" transparent fold
  150. syn region pythonFold start="^\z(\s*\)\(class\|def\)\s" skip="^\z1\s\+\S" end="^\s*\S"me=s-1 transparent fold
  151. syn region pythonFold start="{" end="}" transparent fold
  152. syn region pythonFold start="\[" end="\]" transparent fold
  153. else
  154. " This is fast but code inside triple quoted strings screws it up. It
  155. " is impossible to fix because the only way to know if you are inside a
  156. " triple quoted string is to start from the beginning of the file. If
  157. " you have a fast machine you can try uncommenting the "sync minlines"
  158. " and commenting out the rest.
  159. syn sync match pythonSync grouphere NONE "):$"
  160. syn sync maxlines=200
  161. "syn sync minlines=2000
  162. endif
  163. if version >= 508 || !exists("did_python_syn_inits")
  164. if version <= 508
  165. let did_python_syn_inits = 1
  166. command -nargs=+ HiLink hi link <args>
  167. else
  168. command -nargs=+ HiLink hi def link <args>
  169. endif
  170. HiLink pythonStatement Statement
  171. HiLink pythonImport Statement
  172. HiLink pythonFunction Function
  173. HiLink pythonConditional Conditional
  174. HiLink pythonRepeat Repeat
  175. HiLink pythonException Exception
  176. HiLink pythonOperator Operator
  177. HiLink pythonComment Comment
  178. HiLink pythonTodo Todo
  179. HiLink pythonError Error
  180. HiLink pythonIndentError Error
  181. HiLink pythonString String
  182. HiLink pythonUniString String
  183. HiLink pythonRawString String
  184. HiLink pythonUniRawString String
  185. HiLink pythonEscape Special
  186. HiLink pythonEscapeError Error
  187. HiLink pythonUniEscape Special
  188. HiLink pythonUniEscapeError Error
  189. HiLink pythonUniRawEscape Special
  190. HiLink pythonUniRawEscapeError Error
  191. if exists("python_highlight_string_formatting")
  192. HiLink pythonStrFormat Special
  193. endif
  194. HiLink pythonNumber Number
  195. HiLink pythonFloat Float
  196. HiLink pythonOctalError Error
  197. if exists("python_highlight_builtins")
  198. HiLink pythonBuiltinObj Structure
  199. HiLink pythonBuiltinFunc Function
  200. endif
  201. if exists("python_highlight_exceptions")
  202. HiLink pythonExClass Structure
  203. endif
  204. delcommand HiLink
  205. endif
  206. let b:current_syntax = "python"