python.vim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. " vim: ft=vim:fdm=marker
  2. " DESC: Disable script loading
  3. if !pymode#Option('syntax') || pymode#Default('b:current_syntax', 'python')
  4. finish
  5. endif
  6. " For version 5.x: Clear all syntax items
  7. if version < 600
  8. syntax clear
  9. endif
  10. " Highlight all
  11. call pymode#Default('g:pymode_syntax_all', 1)
  12. " Keywords {{{
  13. " ============
  14. syn keyword pythonStatement break continue del
  15. syn keyword pythonStatement exec return
  16. syn keyword pythonStatement pass raise
  17. syn keyword pythonStatement global assert
  18. syn keyword pythonStatement lambda yield
  19. syn keyword pythonStatement with
  20. syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
  21. syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained
  22. syn keyword pythonRepeat for while
  23. syn keyword pythonConditional if elif else
  24. syn keyword pythonPreCondit import from as
  25. syn keyword pythonException try except finally
  26. syn keyword pythonOperator and in is not or
  27. if !pymode#Default("g:pymode_syntax_print_as_function", 0) || !g:pymode_syntax_print_as_function
  28. syn keyword pythonStatement print
  29. endif
  30. " }}}
  31. " Decorators {{{
  32. " ==============
  33. syn match pythonDecorator "@" display nextgroup=pythonDottedName skipwhite
  34. syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
  35. syn match pythonDot "\." display containedin=pythonDottedName
  36. " }}}
  37. " Comments {{{
  38. " ============
  39. syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
  40. syn match pythonRun "\%^#!.*$"
  41. syn match pythonCoding "\%^.*\(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
  42. syn keyword pythonTodo TODO FIXME XXX contained
  43. " }}}
  44. " Errors {{{
  45. " ==========
  46. syn match pythonError "\<\d\+\D\+\>" display
  47. syn match pythonError "[$?]" display
  48. syn match pythonError "[&|]\{2,}" display
  49. syn match pythonError "[=]\{3,}" display
  50. " Indent errors (mix space and tabs)
  51. if !pymode#Default('g:pymode_syntax_indent_errors', g:pymode_syntax_all) || g:pymode_syntax_indent_errors
  52. syn match pythonIndentError "^\s*\( \t\|\t \)\s*\S"me=e-1 display
  53. endif
  54. " Trailing space errors
  55. if !pymode#Default('g:pymode_syntax_space_errors', g:pymode_syntax_all) || g:pymode_syntax_space_errors
  56. syn match pythonSpaceError "\s\+$" display
  57. endif
  58. " }}}
  59. " Strings {{{
  60. " ===========
  61. syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
  62. syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
  63. syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  64. syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell
  65. syn match pythonEscape +\\[abfnrtv'"\\]+ display contained
  66. syn match pythonEscape "\\\o\o\=\o\=" display contained
  67. syn match pythonEscapeError "\\\o\{,2}[89]" display contained
  68. syn match pythonEscape "\\x\x\{2}" display contained
  69. syn match pythonEscapeError "\\x\x\=\X" display contained
  70. syn match pythonEscape "\\$"
  71. " Unicode
  72. syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
  73. syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell
  74. syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  75. syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  76. syn match pythonUniEscape "\\u\x\{4}" display contained
  77. syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
  78. syn match pythonUniEscape "\\U\x\{8}" display contained
  79. syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
  80. syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
  81. syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
  82. " Raw strings
  83. syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  84. syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  85. syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  86. syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  87. syn match pythonRawEscape +\\['"]+ display transparent contained
  88. " Unicode raw strings
  89. syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  90. syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  91. syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  92. syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell
  93. syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
  94. syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
  95. " String formatting
  96. if !pymode#Default('g:pymode_syntax_string_formatting', g:pymode_syntax_all) || g:pymode_syntax_string_formatting
  97. syn match pythonStrFormatting "%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  98. syn match pythonStrFormatting "%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  99. endif
  100. " Str.format syntax
  101. if !pymode#Default('g:pymode_syntax_string_format', g:pymode_syntax_all) || g:pymode_syntax_string_format
  102. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  103. 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
  104. endif
  105. " String templates
  106. if !pymode#Default('g:pymode_syntax_string_templates', g:pymode_syntax_all) || g:pymode_syntax_string_templates
  107. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  108. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  109. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString
  110. endif
  111. " DocTests
  112. if !pymode#Default('g:pymode_syntax_doctests', g:pymode_syntax_all) || g:pymode_syntax_doctests
  113. syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
  114. syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
  115. endif
  116. " }}}
  117. " Numbers {{{
  118. " ===========
  119. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*[lL]\=\>" display
  120. syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display
  121. syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
  122. syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
  123. syn match pythonNumber "\<\d\+[lLjJ]\=\>" display
  124. syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display
  125. syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
  126. syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display
  127. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
  128. syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
  129. " }}}
  130. " Builtins {{{
  131. " ============
  132. " Builtin objects and types
  133. if !pymode#Default('g:pymode_syntax_builtin_objs', g:pymode_syntax_all) || g:pymode_syntax_builtin_objs
  134. syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented
  135. syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
  136. syn keyword pythonBuiltinObj self
  137. endif
  138. " Builtin functions
  139. if !pymode#Default('g:pymode_syntax_builtin_funcs', g:pymode_syntax_all) || g:pymode_syntax_builtin_funcs
  140. syn keyword pythonBuiltinFunc __import__ abs all any apply
  141. syn keyword pythonBuiltinFunc basestring bin bool buffer bytearray bytes callable
  142. syn keyword pythonBuiltinFunc chr classmethod cmp coerce compile complex
  143. syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval
  144. syn keyword pythonBuiltinFunc execfile file filter float format frozenset getattr
  145. syn keyword pythonBuiltinFunc globals hasattr hash help hex id
  146. syn keyword pythonBuiltinFunc input int intern isinstance
  147. syn keyword pythonBuiltinFunc issubclass iter len list locals long map max
  148. syn keyword pythonBuiltinFunc min next object oct open ord
  149. syn keyword pythonBuiltinFunc pow property range
  150. syn keyword pythonBuiltinFunc raw_input reduce reload repr
  151. syn keyword pythonBuiltinFunc reversed round set setattr
  152. syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple
  153. syn keyword pythonBuiltinFunc type unichr unicode vars xrange zip
  154. if pymode#Default('g:pymode_syntax_print_as_function', 0) && g:pymode_syntax_print_as_function
  155. syn keyword pythonBuiltinFunc print
  156. endif
  157. endif
  158. " Builtin exceptions and warnings
  159. if !pymode#Default('g:pymode_syntax_highlight_exceptions', g:pymode_syntax_all) || g:pymode_syntax_highlight_exceptions
  160. syn keyword pythonExClass BaseException
  161. syn keyword pythonExClass Exception StandardError ArithmeticError
  162. syn keyword pythonExClass LookupError EnvironmentError
  163. syn keyword pythonExClass AssertionError AttributeError BufferError EOFError
  164. syn keyword pythonExClass FloatingPointError GeneratorExit IOError
  165. syn keyword pythonExClass ImportError IndexError KeyError
  166. syn keyword pythonExClass KeyboardInterrupt MemoryError NameError
  167. syn keyword pythonExClass NotImplementedError OSError OverflowError
  168. syn keyword pythonExClass ReferenceError RuntimeError StopIteration
  169. syn keyword pythonExClass SyntaxError IndentationError TabError
  170. syn keyword pythonExClass SystemError SystemExit TypeError
  171. syn keyword pythonExClass UnboundLocalError UnicodeError
  172. syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError
  173. syn keyword pythonExClass UnicodeTranslateError ValueError VMSError
  174. syn keyword pythonExClass WindowsError ZeroDivisionError
  175. syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning
  176. syn keyword pythonExClass PendingDepricationWarning SyntaxWarning
  177. syn keyword pythonExClass RuntimeWarning FutureWarning
  178. syn keyword pythonExClass ImportWarning UnicodeWarning
  179. endif
  180. " }}}
  181. if !pymode#Default('g:pymode_syntax_slow_sync', 0) || g:pymode_syntax_slow_sync
  182. syn sync minlines=2000
  183. else
  184. " This is fast but code inside triple quoted strings screws it up. It
  185. " is impossible to fix because the only way to know if you are inside a
  186. " triple quoted string is to start from the beginning of the file.
  187. syn sync match pythonSync grouphere NONE "):$"
  188. syn sync maxlines=200
  189. endif
  190. " Highlight {{{
  191. " =============
  192. hi def link pythonStatement Statement
  193. hi def link pythonPreCondit Statement
  194. hi def link pythonFunction Function
  195. hi def link pythonConditional Conditional
  196. hi def link pythonRepeat Repeat
  197. hi def link pythonException Exception
  198. hi def link pythonOperator Operator
  199. hi def link pythonDecorator Define
  200. hi def link pythonDottedName Function
  201. hi def link pythonDot Normal
  202. hi def link pythonComment Comment
  203. hi def link pythonCoding Special
  204. hi def link pythonRun Special
  205. hi def link pythonTodo Todo
  206. hi def link pythonError Error
  207. hi def link pythonIndentError Error
  208. hi def link pythonSpaceError Error
  209. hi def link pythonString String
  210. hi def link pythonUniString String
  211. hi def link pythonRawString String
  212. hi def link pythonUniRawString String
  213. hi def link pythonEscape Special
  214. hi def link pythonEscapeError Error
  215. hi def link pythonUniEscape Special
  216. hi def link pythonUniEscapeError Error
  217. hi def link pythonUniRawEscape Special
  218. hi def link pythonUniRawEscapeError Error
  219. hi def link pythonStrFormatting Special
  220. hi def link pythonStrFormat Special
  221. hi def link pythonStrTemplate Special
  222. hi def link pythonDocTest Special
  223. hi def link pythonDocTest2 Special
  224. hi def link pythonNumber Number
  225. hi def link pythonHexNumber Number
  226. hi def link pythonOctNumber Number
  227. hi def link pythonBinNumber Number
  228. hi def link pythonFloat Float
  229. hi def link pythonOctError Error
  230. hi def link pythonHexError Error
  231. hi def link pythonBinError Error
  232. hi def link pythonBuiltinObj Structure
  233. hi def link pythonBuiltinFunc Function
  234. hi def link pythonExClass Structure
  235. " }}}