diff --git a/build b/build index 20fea26..b0ef1da 100755 --- a/build +++ b/build @@ -99,7 +99,7 @@ PACKS=" php:StanAngeloff/php.vim puppet:ajf/puppet-vim protobuf:uarun/vim-protobuf - python:vim-scripts/python.vim--Vasiliev + python:mitsuhiko/vim-python-combined r-lang:vim-scripts/R.vim rspec:sheerun/rspec.vim ruby:vim-ruby/vim-ruby diff --git a/indent/python.vim b/indent/python.vim new file mode 100644 index 0000000..24ffd88 --- /dev/null +++ b/indent/python.vim @@ -0,0 +1,213 @@ +" PEP8 compatible Python indent file +" Only load this indent file when no other was loaded. +if exists("b:did_indent") + finish +endif +let b:did_indent = 1 + +setlocal expandtab +setlocal nolisp +setlocal autoindent +setlocal indentexpr=GetPythonPEPIndent(v:lnum) +setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except + +let s:maxoff = 50 + +" Find backwards the closest open parenthesis/bracket/brace. +function! s:SearchParensPair() + let line = line('.') + let col = col('.') + + " Skip strings and comments and don't look too far + let skip = "line('.') < " . (line - s:maxoff) . " ? dummy :" . + \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' . + \ '"string\\|comment"' + + " Search for parentheses + call cursor(line, col) + let parlnum = searchpair('(', '', ')', 'bW', skip) + let parcol = col('.') + + " Search for brackets + call cursor(line, col) + let par2lnum = searchpair('\[', '', '\]', 'bW', skip) + let par2col = col('.') + + " Search for braces + call cursor(line, col) + let par3lnum = searchpair('{', '', '}', 'bW', skip) + let par3col = col('.') + + " Get the closest match + if par2lnum > parlnum || (par2lnum == parlnum && par2col > parcol) + let parlnum = par2lnum + let parcol = par2col + endif + if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol) + let parlnum = par3lnum + let parcol = par3col + endif + + " Put the cursor on the match + if parlnum > 0 + call cursor(parlnum, parcol) + endif + return parlnum +endfunction + +" Find the start of a multi-line statement +function! s:StatementStart(lnum) + let lnum = a:lnum + while 1 + if getline(lnum - 1) =~ '\\$' + let lnum = lnum - 1 + else + call cursor(lnum, 1) + let maybe_lnum = s:SearchParensPair() + if maybe_lnum < 1 + return lnum + else + let lnum = maybe_lnum + endif + endif + endwhile +endfunction + +" Find the block starter that matches the current line +function! s:BlockStarter(lnum, block_start_re) + let lnum = a:lnum + let maxindent = 10000 " whatever + while lnum > 1 + let lnum = prevnonblank(lnum - 1) + if indent(lnum) < maxindent + if getline(lnum) =~ a:block_start_re + return lnum + else + let maxindent = indent(lnum) + " It's not worth going further if we reached the top level + if maxindent == 0 + return -1 + endif + endif + endif + endwhile + return -1 +endfunction + +function! GetPythonPEPIndent(lnum) + let scol = col('.') + + " First line has indent 0 + if a:lnum == 1 + return 0 + endif + + " If we can find an open parenthesis/bracket/brace, line up with it. + call cursor(a:lnum, 1) + let parlnum = s:SearchParensPair() + if parlnum > 0 + let parcol = col('.') + let matches = matchlist(getline(a:lnum), '^\(\s*\)[])}]') + if len(matches) == 0 + let closing_paren = 0 + let closing_paren_pos = 0 + else + let closing_paren = 1 + let closing_paren_pos = len(matches[1]) + endif + if match(getline(parlnum), '[([{]\s*$', parcol - 1) != -1 + if closing_paren + return indent(parlnum) + else + return indent(parlnum) + &shiftwidth + endif + elseif a:lnum - 1 != parlnum + if closing_paren && closing_paren_pos > scol + return indent(parlnum) + else + let lastindent = match(getline(a:lnum - 1), '\S') + if lastindent != -1 && lastindent < parcol + return lastindent + endif + endif + endif + + " If we line up with an opening column there is a special case + " we want to handle: a docstring as argument. In that case we + " don't want to line up with the paren but with the statement + " imagine foo(doc=""" as example + echo getline(parlnum) + if match(getline(parlnum), '\("""\|' . "'''" . '\)\s*$') != -1 + return indent(parlnum) + endif + + return parcol + endif + + " Examine this line + let thisline = getline(a:lnum) + let thisindent = indent(a:lnum) + + " If the line starts with 'elif' or 'else', line up with 'if' or 'elif' + if thisline =~ '^\s*\(elif\|else\)\>' + let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>') + if bslnum > 0 + return indent(bslnum) + else + return -1 + endif + endif + + " If the line starts with 'except' or 'finally', line up with 'try' + " or 'except' + if thisline =~ '^\s*\(except\|finally\)\>' + let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>') + if bslnum > 0 + return indent(bslnum) + else + return -1 + endif + endif + + " Examine previous line + let plnum = a:lnum - 1 + let pline = getline(plnum) + let sslnum = s:StatementStart(plnum) + + " If the previous line is blank, keep the same indentation + if pline =~ '^\s*$' + return -1 + endif + + " If this line is explicitly joined, try to find an indentation that looks + " good. + if pline =~ '\\$' + let compound_statement = '^\s*\(if\|while\|from\|import\|for\s.*\sin\|except\)\s*' + let maybe_indent = matchend(getline(sslnum), compound_statement) + if maybe_indent != -1 + return maybe_indent + else + return indent(sslnum) + &sw * 2 + endif + endif + + " If the previous line ended with a colon and is not a comment, indent + " relative to statement start. + if pline =~ '^[^#]*:\s*\(#.*\)\?$' + return indent(sslnum) + &sw + endif + + " If the previous line was a stop-execution statement or a pass + if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' + " See if the user has already dedented + if indent(a:lnum) > indent(sslnum) - &sw + " If not, recommend one dedent + return indent(sslnum) - &sw + endif + " Otherwise, trust the user + return -1 + endif + + " In all other cases, line up with the start of the previous statement. + return indent(sslnum) +endfunction diff --git a/syntax/python.vim b/syntax/python.vim index 0e0bb12..7482d07 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -1,27 +1,20 @@ " Vim syntax file -" Language: Python -" Maintainer: Dmitry Vasiliev -" URL: http://www.hlabs.spb.ru/vim/python.vim -" Last Change: 2010-04-09 -" Filenames: *.py -" Version: 2.6.6 " " Based on python.vim (from Vim 6.1 distribution) " by Neil Schemenauer " +" Notes Armin: +" +" This version of the syntax file works better for 2.x and 3.x without +" having to switch modes. +" " Thanks: " " Jeroen Ruigrok van der Werven -" for the idea to highlight erroneous operators +" for the idea of highlighting for erroneous operators " Pedro Algarvio " for the patch to enable spell checking only for the right spots " (strings and comments) -" John Eikenberry -" for the patch fixing small typo -" Caleb Adamantine -" for the patch fixing highlighting for decorators -" Andrea Riciputi -" for the patch with new configuration options " " Options: @@ -31,27 +24,15 @@ " " Option names: " -" For highlight builtin functions and objects: +" For highlight builtin functions: " python_highlight_builtins " -" For highlight builtin objects: -" python_highlight_builtin_objs -" -" For highlight builtin funtions: -" python_highlight_builtin_funcs -" " For highlight standard exceptions: " python_highlight_exceptions " " For highlight string formatting: " python_highlight_string_formatting " -" For highlight str.format syntax: -" python_highlight_string_format -" -" For highlight string.Template syntax: -" python_highlight_string_templates -" " For highlight indentation errors: " python_highlight_indent_errors " @@ -61,15 +42,13 @@ " For highlight doc-tests: " python_highlight_doctests " -" If you want all Python highlightings above: -" python_highlight_all +" If you want all possible Python highlighting: " (This option not override previously set options) +" python_highlight_all " " For fast machines: " python_slow_sync " -" For "print" builtin as function: -" python_print_as_function " For version 5.x: Clear all syntax items " For version 6.x: Quit when a syntax file was already loaded @@ -82,12 +61,7 @@ endif if exists("python_highlight_all") && python_highlight_all != 0 " Not override previously set options if !exists("python_highlight_builtins") - if !exists("python_highlight_builtin_objs") - let python_highlight_builtin_objs = 1 - endif - if !exists("python_highlight_builtin_funcs") - let python_highlight_builtin_funcs = 1 - endif + let python_highlight_builtins = 1 endif if !exists("python_highlight_exceptions") let python_highlight_exceptions = 1 @@ -95,12 +69,6 @@ if exists("python_highlight_all") && python_highlight_all != 0 if !exists("python_highlight_string_formatting") let python_highlight_string_formatting = 1 endif - if !exists("python_highlight_string_format") - let python_highlight_string_format = 1 - endif - if !exists("python_highlight_string_templates") - let python_highlight_string_templates = 1 - endif if !exists("python_highlight_indent_errors") let python_highlight_indent_errors = 1 endif @@ -118,23 +86,20 @@ syn keyword pythonStatement exec return syn keyword pythonStatement pass raise syn keyword pythonStatement global assert syn keyword pythonStatement lambda yield -syn keyword pythonStatement with +syn keyword pythonStatement with nonlocal True False None syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained syn keyword pythonRepeat for while syn keyword pythonConditional if elif else -syn keyword pythonPreCondit import from as +syn keyword pythonImport import from as syn keyword pythonException try except finally syn keyword pythonOperator and in is not or -if !exists("python_print_as_function") || python_print_as_function == 0 - syn keyword pythonStatement print -endif +" Print keyword but only if not used as function +syn match pythonStatement "\\((\)\@!" display " Decorators (new in Python 2.4) -syn match pythonDecorator "@" display nextgroup=pythonDottedName skipwhite -syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained -syn match pythonDot "\." display containedin=pythonDottedName +syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite " Comments syn match pythonComment "#.*$" display contains=pythonTodo,@Spell @@ -145,7 +110,7 @@ syn keyword pythonTodo TODO FIXME XXX contained " Errors syn match pythonError "\<\d\+\D\+\>" display syn match pythonError "[$?]" display -syn match pythonError "[&|]\{2,}" display +syn match pythonError "[-+&|]\{2,}" display syn match pythonError "[=]\{3,}" display " TODO: Mixing spaces and tabs also may be used for pretty formatting multiline @@ -160,10 +125,10 @@ if exists("python_highlight_space_errors") && python_highlight_space_errors != 0 endif " Strings -syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell -syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell -syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell -syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell +syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell +syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell +syn region pythonString start=+"""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell +syn region pythonString start=+'''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell syn match pythonEscape +\\[abfnrtv'"\\]+ display contained syn match pythonEscape "\\\o\o\=\o\=" display contained @@ -172,6 +137,19 @@ syn match pythonEscape "\\x\x\{2}" display contained syn match pythonEscapeError "\\x\x\=\X" display contained syn match pythonEscape "\\$" +" Byte-Strings +syn region pythonBString start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBEscape,pythonBEscapeError,@Spell +syn region pythonBString start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBEscape,pythonBEscapeError,@Spell +syn region pythonBString start=+[bB]"""+ end=+"""+ keepend contains=pythonBEscape,pythonBEscapeError,pythonDocTest2,pythonSpaceError,@Spell +syn region pythonBString start=+[bB]'''+ end=+'''+ keepend contains=pythonBEscape,pythonBEscapeError,pythonDocTest,pythonSpaceError,@Spell + +syn match pythonBEscape +\\[abfnrtv'"\\]+ display contained +syn match pythonBEscape "\\\o\o\=\o\=" display contained +syn match pythonBEscapeError "\\\o\{,2}[89]" display contained +syn match pythonBEscape "\\x\x\{2}" display contained +syn match pythonBEscapeError "\\x\x\=\X" display contained +syn match pythonBEscape "\\$" + " Unicode strings syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell @@ -204,21 +182,8 @@ syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0 " String formatting - syn match pythonStrFormatting "%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString - syn match pythonStrFormatting "%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString -endif - -if exists("python_highlight_string_format") && python_highlight_string_format != 0 - " str.format syntax - syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString - 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 -endif - -if exists("python_highlight_string_templates") && python_highlight_string_templates != 0 - " String templates - syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString - syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString - syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString + syn match pythonStrFormat "%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonBString,pythonUniString,pythonRawString,pythonUniRawString + syn match pythonStrFormat "%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonBString,pythonUniString,pythonRawString,pythonUniRawString endif if exists("python_highlight_doctests") && python_highlight_doctests != 0 @@ -228,47 +193,33 @@ if exists("python_highlight_doctests") && python_highlight_doctests != 0 endif " Numbers (ints, longs, floats, complex) -syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*[lL]\=\>" display - syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display -syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display -syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display - +syn match pythonHexNumber "\<0[xX]\>" display syn match pythonNumber "\<\d\+[lLjJ]\=\>" display - syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display -syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display -syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display +syn match pythonOctalError "\<0\o*[89]\d*[lL]\=\>" display +syn match pythonHexError "\<0[xX]\X\+[lL]\=\>" display -if exists("python_highlight_builtin_objs") && python_highlight_builtin_objs != 0 - " Builtin objects and types - syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented - syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__ -endif +if exists("python_highlight_builtins") && python_highlight_builtins != 0 + " Builtin functions, types and objects + syn keyword pythonBuiltinObj Ellipsis NotImplemented -if exists("python_highlight_builtin_funcs") && python_highlight_builtin_funcs != 0 - " Builtin functions syn keyword pythonBuiltinFunc __import__ abs all any apply - syn keyword pythonBuiltinFunc basestring bin bool buffer bytearray bytes callable + syn keyword pythonBuiltinFunc basestring bool buffer callable syn keyword pythonBuiltinFunc chr classmethod cmp coerce compile complex syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval - syn keyword pythonBuiltinFunc execfile file filter float format frozenset getattr - syn keyword pythonBuiltinFunc globals hasattr hash help hex id + syn keyword pythonBuiltinFunc execfile file filter float frozenset getattr + syn keyword pythonBuiltinfunc globals hasattr hash help hex id syn keyword pythonBuiltinFunc input int intern isinstance syn keyword pythonBuiltinFunc issubclass iter len list locals long map max - syn keyword pythonBuiltinFunc min next object oct open ord - syn keyword pythonBuiltinFunc pow property range + syn keyword pythonBuiltinFunc min object oct open ord pow property range syn keyword pythonBuiltinFunc raw_input reduce reload repr - syn keyword pythonBuiltinFunc reversed round set setattr + syn keyword pythonBuiltinFunc reversed round set setattr syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple syn keyword pythonBuiltinFunc type unichr unicode vars xrange zip - - if exists("python_print_as_function") && python_print_as_function != 0 - syn keyword pythonBuiltinFunc print - endif endif if exists("python_highlight_exceptions") && python_highlight_exceptions != 0 @@ -277,7 +228,7 @@ if exists("python_highlight_exceptions") && python_highlight_exceptions != 0 syn keyword pythonExClass Exception StandardError ArithmeticError syn keyword pythonExClass LookupError EnvironmentError - syn keyword pythonExClass AssertionError AttributeError BufferError EOFError + syn keyword pythonExClass AssertionError AttributeError EOFError syn keyword pythonExClass FloatingPointError GeneratorExit IOError syn keyword pythonExClass ImportError IndexError KeyError syn keyword pythonExClass KeyboardInterrupt MemoryError NameError @@ -287,12 +238,12 @@ if exists("python_highlight_exceptions") && python_highlight_exceptions != 0 syn keyword pythonExClass SystemError SystemExit TypeError syn keyword pythonExClass UnboundLocalError UnicodeError syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError - syn keyword pythonExClass UnicodeTranslateError ValueError VMSError + syn keyword pythonExClass UnicodeTranslateError ValueError syn keyword pythonExClass WindowsError ZeroDivisionError - syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning + syn keyword pythonExClass Warning UserWarning DeprecationWarning syn keyword pythonExClass PendingDepricationWarning SyntaxWarning - syn keyword pythonExClass RuntimeWarning FutureWarning + syn keyword pythonExClass RuntimeWarning FutureWarning OverflowWarning syn keyword pythonExClass ImportWarning UnicodeWarning endif @@ -315,7 +266,7 @@ if version >= 508 || !exists("did_python_syn_inits") endif HiLink pythonStatement Statement - HiLink pythonPreCondit Statement + HiLink pythonImport Statement HiLink pythonFunction Function HiLink pythonConditional Conditional HiLink pythonRepeat Repeat @@ -323,8 +274,6 @@ if version >= 508 || !exists("did_python_syn_inits") HiLink pythonOperator Operator HiLink pythonDecorator Define - HiLink pythonDottedName Function - HiLink pythonDot Normal HiLink pythonComment Comment HiLink pythonCoding Special @@ -336,32 +285,30 @@ if version >= 508 || !exists("did_python_syn_inits") HiLink pythonSpaceError Error HiLink pythonString String + HiLink pythonBString String HiLink pythonUniString String HiLink pythonRawString String HiLink pythonUniRawString String HiLink pythonEscape Special + HiLink pythonBEscape Special HiLink pythonEscapeError Error + HiLink pythonBEscapeError Error HiLink pythonUniEscape Special HiLink pythonUniEscapeError Error HiLink pythonUniRawEscape Special HiLink pythonUniRawEscapeError Error - HiLink pythonStrFormatting Special - HiLink pythonStrFormat Special - HiLink pythonStrTemplate Special + HiLink pythonStrFormat Special HiLink pythonDocTest Special HiLink pythonDocTest2 Special HiLink pythonNumber Number HiLink pythonHexNumber Number - HiLink pythonOctNumber Number - HiLink pythonBinNumber Number HiLink pythonFloat Float - HiLink pythonOctError Error + HiLink pythonOctalError Error HiLink pythonHexError Error - HiLink pythonBinError Error HiLink pythonBuiltinObj Structure HiLink pythonBuiltinFunc Function