Attachment 'python.vim'

Download

   1 " enforce python PEP 0008
   2 let python_slow_sync = 1
   3 
   4 if exists("b:current_syntax")
   5   finish
   6 endif
   7 
   8 if !exists("python_highlight_builtins")
   9     let python_highlight_builtins = 1
  10 endif
  11 if !exists("python_highlight_string_formatting")
  12     let python_highlight_string_formatting = 1
  13 endif
  14 if !exists("python_highlight_doctests")
  15     let python_highlight_doctests = 1
  16 endif
  17 if !exists("python_79_char_line_limit")
  18     let python_79_char_line_limit = 0
  19 endif
  20 if !exists("python_highlight_whitespace")
  21     let python_highlight_whitespace = 1
  22 endif
  23 
  24 " Keywords
  25 syn keyword pythonStatement	break continue del exec return pass
  26 syn keyword pythonStatement	print raise global assert yield
  27 syn keyword pythonStatement	def nextgroup=pythonFunction skipwhite
  28 syn keyword pythonStatement	class nextgroup=pythonClass skipwhite
  29 syn match   pythonFunction	"\h\w*" display contained nextgroup=pythonFuncDecl skipwhite
  30 syn match   pythonClass         "\h\w*" display contained nextgroup=pythonFuncDecl skipwhite
  31 syn keyword pythonRepeat	for while
  32 syn keyword pythonConditional	if elif else
  33 syn keyword pythonImport	import from as
  34 syn keyword pythonException	try except finally
  35 syn keyword pythonOperator	and in is not or lambda for
  36 
  37 " Comments
  38 syn match   pythonComment	"#.*$" display contains=pythonTodo
  39 syn match   pythonRun		"\%^#!.*$"
  40 syn match   pythonCoding	"\%^.*\(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
  41 syn keyword pythonTodo		TODO FIXME XXX RFC contained
  42 
  43 " when outside of parenthesis, = must have space on either side
  44 syn match pythonUncontainedError "\>=" display
  45 syn match pythonUncontainedError "=[^ =$\\]"me=e-1 display
  46 
  47 " erroneous whitespace
  48 if exists("python_highlight_whitespace") && python_highlight_whitespace != 0
  49   syn match pythonWhiteSpaceError " $" display
  50   syn match pythonWhiteSpaceError "[^ \t]  \+[^ #]"ms=s+2,me=e-1 display
  51 endif
  52 
  53 " illegal characters
  54 syn match pythonError "[@$?\t]" display
  55 syn match pythonError ";\ze *$" display
  56 
  57 " erroneous whitespace around operators
  58 syn match pythonError "\.\zs " display
  59 syn match pythonError " [:,]\@=" display
  60 
  61 " missing whitespace after operator
  62 syn match pythonError "[<>%]\ze[^ =$\\]" display
  63 syn match pythonError "[<>!+*/%\-]=\ze[^ $\\]" display
  64 "syn match pythonUncontainedError "[*]\ze[^ =$\\]e" display
  65 " comma must be followed by a space
  66 syn match pythonError ",\ze[^ )=$\\]" display
  67 
  68 " missing whitespace before operator
  69 syn match pythonError " \@<![<>%]" display
  70 syn match pythonError " \@<![+\-*/=<>!]=" display
  71 "syn match pythonUncontainedError " \@<![*]" display
  72 
  73 " deprecated or invalid constructions
  74 syn match pythonError "\<string\.\(join\|split\)" display
  75 "
  76 syn match pythonError "[!=]= \?\(None\|True\|False\|type\>\|dict\>\|list\>\|tuple\>\|str\>\|unicode\>\)" display
  77 syn keyword pythonError throw catch l O I <> elsif
  78 
  79 "--contained in parenthesis-- 
  80 " TODO these two need to leave out the actual '='
  81 syn match pythonError " \ze=[^=]" contained containedin=@pythonContainers,pythonFuncDecl
  82 syn match pythonError "=\zs " contained containedin=@pythonContainers,pythonFuncDecl
  83 syn match ok " [!=+/\-*<>]= " contained containedin=@pythonContainers
  84 syn match pythonError "\\" contained containedin=@pythonContainers,pythonFuncDecl
  85 
  86 
  87 " Strings
  88 syn region pythonString		start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError
  89 syn region pythonString		start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError
  90 syn region pythonString		start=+"""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2
  91 syn region pythonString		matchgroup=pythonError start=+'''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest
  92 
  93 
  94 syn match  pythonEscape		+\\[abfnrtv'"\\]+ display contained
  95 syn match  pythonEscapeError	+\\[^abfnrtv'"\\]+ display contained
  96 syn match  pythonEscape		"\\\o\o\=\o\=" display contained
  97 syn match  pythonEscapeError	"\\\o\{,2}[89]" display contained
  98 syn match  pythonEscape		"\\x\x\{2}" display contained
  99 syn match  pythonEscapeError	"\\x\x\=\X" display contained
 100 syn match  pythonEscape		"\\$"
 101 
 102 " Unicode strings
 103 syn region pythonUniString	start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError
 104 syn region pythonUniString	start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError
 105 syn region pythonUniString	start=+[uU]"""+ end=+"""+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest2
 106 syn region pythonUniString	start=+[uU]'''+ end=+'''+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest
 107 
 108 syn match  pythonUniEscape	"\\u\x\{4}" display contained
 109 syn match  pythonUniEscapeError	"\\u\x\{,3}\X" display contained
 110 syn match  pythonUniEscape	"\\U\x\{8}" display contained
 111 syn match  pythonUniEscapeError	"\\U\x\{,7}\X" display contained
 112 syn match  pythonUniEscape	"\\N{[A-Z ]\+}" display contained
 113 syn match  pythonUniEscapeError	"\\N{[^A-Z ]\+}" display contained
 114 
 115 " Raw strings
 116 syn region pythonRawString	start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape
 117 syn region pythonRawString	start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape
 118 syn region pythonRawString	start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2
 119 syn region pythonRawString	start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest
 120 
 121 syn match pythonRawEscape	+\\['"]+ display transparent contained
 122 
 123 " Unicode raw strings
 124 syn region pythonUniRawString	start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError
 125 syn region pythonUniRawString	start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError
 126 syn region pythonUniRawString	start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2
 127 syn region pythonUniRawString	start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest
 128 
 129 syn match  pythonUniRawEscape		"\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
 130 syn match  pythonUniRawEscapeError	"\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
 131 
 132 if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
 133   " String formatting
 134   syn match pythonStrFormat	"%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString
 135   syn match pythonStrFormat	"%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString
 136 endif
 137 
 138 if exists("python_highlight_doctests") && python_highlight_doctests != 0
 139   " DocTests
 140   syn region pythonDocTest	start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
 141   syn region pythonDocTest2	start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
 142 endif
 143 
 144 " Numbers (ints, longs, floats, complex)
 145 syn match   pythonNumber	"\<0[xX]\x\+[lL]\=\>" display
 146 syn match   pythonNumber	"\<\d\+[lLjJ]\=\>" display
 147 syn match   pythonFloat		"\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display
 148 syn match   pythonFloat		"\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
 149 syn match   pythonFloat		"\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display
 150 syn match   pythonOctalError	"\<0\o*[89]\d*[lLjJ]\=\>" display
 151 
 152 
 153 syn keyword pythonBuiltinObj	True False Ellipsis None NotImplemented
 154 
 155 if exists("python_highlight_builtins") && python_highlight_builtins != 0
 156   " Builtin functions, types and objects, not really part of the syntax
 157   syn keyword pythonBuiltinFunc	__import__ abs all any apply
 158   syn keyword pythonBuiltinFunc	basestring bool buffer callable
 159   syn keyword pythonBuiltinFunc	chr classmethod cmp coerce compile complex
 160   syn keyword pythonBuiltinFunc	delattr dict dir divmod enumerate eval
 161   syn keyword pythonBuiltinFunc	execfile file filter float frozenset getattr
 162   syn keyword pythonBuiltinfunc globals hasattr hash help hex id 
 163   syn keyword pythonBuiltinFunc	input int intern isinstance
 164   syn keyword pythonBuiltinFunc	issubclass iter len list locals long map max
 165   syn keyword pythonBuiltinFunc	min object oct open ord pow property range
 166   syn keyword pythonBuiltinFunc	raw_input reduce reload repr
 167   syn keyword pythonBuiltinFunc reversed round set setattr
 168   syn keyword pythonBuiltinFunc	slice sorted staticmethod str sum super tuple
 169   syn keyword pythonBuiltinFunc	type unichr unicode vars xrange zip
 170 endif
 171 
 172 " don't match builtins if they are methods
 173 syn match ok "\.\h\w*"
 174 
 175 " container regions
 176 syn region pythonFuncDecl       matchgroup=pythonFuncDeclParens start="(" end=")" contained contains=@pythonExpression,pythonFuncDeclError,@pythonContainers
 177 syn region Parenthesis		matchgroup=pythonParens start="(" end=")" matchgroup=pythonError end="[}\]]" contains=@pythonExpression,@pythonContainers
 178 syn region BlockParens		start="\[" end="\]" matchgroup=pythonError end="[)}]" contains=@pythonExpression,@pythonContainers
 179 syn region CurlyParens		start="{" end="}" matchgroup=pythonError end="[\])]" contains=@pythonExpression,@pythonContainers
 180 syn cluster pythonContainers    contains=Parenthesis,BlockParens,CurlyParens
 181 syn cluster pythonExpression    contains=pythonOperator,pythonString,pythonUniString,pythonRawString,pythonUniRawString,pythonNumber,pythonFloat,pythonOctalError,pythonBuiltinObj,pythonBuiltinFunc,pythonError,pythonComment,pythonWhiteSpaceError
 182 syn match pythonFuncDeclError "{}" contained containedin=pythonFuncDecl display
 183 syn match pythonFuncDeclError "\[]" contained containedin=pythonFuncDecl display
 184 syn match pythonUncontainedError "[\]})]"
 185 
 186 syn match pythonError "\[\@<= "
 187 syn match pythonError "(\@<= "
 188 syn match pythonError "{\@<= "
 189 "syn match pythonError " \ze[)\]}]"
 190 
 191 "syn region pythonIndent matchgroup=pythonConditional start="[ ^]if " end="\ze: *[#$]" matchgroup=pythonError end=".$" contains=@pythonExpression
 192 
 193 if exists("python_slow_sync") && python_slow_sync != 0
 194   syn sync minlines=2000
 195 else
 196   " This is fast but code inside triple quoted strings screws it up. It
 197   " is impossible to fix because the only way to know if you are inside a
 198   " triple quoted string is to start from the beginning of the file.
 199   syn sync match pythonSync grouphere NONE "):$"
 200   syn sync maxlines=200
 201 endif
 202 
 203 if exists("python_79_char_line_limit") && python_79_char_line_limit != 0
 204   "syn region LineTooLong start="\%80c" end="$" excludenl containedin=ALL
 205   " THIS IS TOO SLOW!
 206   syn match LineTooLong -\%>79c[^{([\])}'"]\+- containedin=ALL display
 207 endif
 208 
 209 if version >= 508 || !exists("did_python_syn_inits")
 210   if version <= 508
 211     let did_python_syn_inits = 1
 212     command -nargs=+ HiLink hi link <args>
 213   else
 214     command -nargs=+ HiLink hi def link <args>
 215   endif
 216 
 217   HiLink pythonStatement	Statement
 218   HiLink pythonImport		Include
 219   HiLink pythonFunction		Function
 220   HiLink pythonClass		Function
 221   HiLink pythonConditional	Conditional
 222   HiLink pythonRepeat		Repeat
 223   HiLink pythonException	Exception
 224   HiLink pythonOperator		Operator
 225 
 226   HiLink pythonComment		Comment
 227   HiLink pythonCoding		Special
 228   HiLink pythonRun		Special
 229   HiLink pythonTodo		Todo
 230   
 231   HiLink pythonUncontainedError Error
 232   HiLink pythonError		Error
 233 
 234   HiLink pythonString		String
 235   HiLink pythonUniString	String
 236   HiLink pythonRawString	String
 237   HiLink pythonUniRawString	String
 238 
 239   HiLink pythonEscape			Special
 240   HiLink pythonEscapeError		Error
 241   HiLink pythonUniEscape		Special
 242   HiLink pythonUniEscapeError		Error
 243   HiLink pythonUniRawEscape		Special
 244   HiLink pythonUniRawEscapeError	Error
 245 
 246   HiLink LineTooLong            Error
 247   if exists("python_highlight_whitespace") && python_highlight_whitespace != 0
 248     HiLink pythonWhiteSpaceError  Error
 249   endif
 250   HiLink pythonFuncDeclError    Error
 251   HiLink pythonFuncDeclParens   Special
 252   HiLink pythonParens           Special
 253 
 254   if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
 255     HiLink pythonStrFormat	Special
 256   endif
 257 
 258   if exists("python_highlight_doctests") && python_highlight_doctests != 0
 259     HiLink pythonDocTest	Special
 260     HiLink pythonDocTest2	Special
 261   endif
 262 
 263   HiLink pythonNumber		Number
 264   HiLink pythonFloat		Float
 265   HiLink pythonOctalError	Error
 266 
 267   if exists("python_highlight_builtins") && python_highlight_builtins != 0
 268     HiLink pythonBuiltinObj	Structure
 269     HiLink pythonBuiltinFunc	Function
 270   endif
 271 
 272   delcommand HiLink
 273 endif
 274 
 275 let b:current_syntax = "python"

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-07-05 18:21:25, 12.4 KB) [[attachment:python.vim]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.