home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim60.zip / vim60rt2.zip / vim / vim60 / indent / python.vim < prev    next >
Text File  |  2001-09-26  |  2KB  |  74 lines

  1. " Python indent file
  2. " Language:    Python
  3. " Maintainer:    David Bustos <bustos@caltech.edu>
  4. " Last Change:    July 22, 2001
  5.  
  6. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8.   finish
  9. endif
  10. let b:did_indent = 1
  11.  
  12. " Some preliminary settings
  13. setlocal nolisp        " Make sure lisp indenting doesn't supersede us
  14.  
  15. setlocal indentexpr=GetPythonIndent(v:lnum)
  16. setlocal indentkeys+=<:>,=elif
  17.  
  18. " Only define the function once.
  19. if exists("*GetPythonIndent")
  20.   finish
  21. endif
  22.  
  23. function GetPythonIndent(lnum)
  24.   " Give up if this line is explicitly joined.
  25.   if getline(a:lnum - 1) =~ '\\$'
  26.     return -1
  27.   endif
  28.  
  29.   " Search backwards for the frist non-empty line.
  30.   let plnum = prevnonblank(v:lnum - 1)
  31.  
  32.   if plnum == 0
  33.     " This is the first non-empty line, use zero indent.
  34.     return 0
  35.   endif
  36.  
  37.   " If the previous line ended with a colon, indent this line
  38.   if getline(plnum) =~ '^[^#]*:\s*\(#.*\)\=$'
  39.     return indent(plnum) + &sw
  40.  
  41.   " If the previous line was a stop-execution statement...
  42.   elseif getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\)\>'
  43.     " See if the user has already dedented
  44.     if indent(a:lnum) > indent(plnum) - &sw
  45.       " If not, recommend one dedent
  46.       return indent(plnum) - &sw
  47.     else
  48.       " Otherwise, trust the user
  49.       return -1
  50.     endif
  51.  
  52.   " If the current line begins with a header keyword, dedent
  53.   elseif getline(a:lnum) =~ '^\s*\(elif\|else\|except\|finaly\)\>'
  54.  
  55.     " Unless the previous line was a one-liner
  56.     if getline(plnum) =~ '^\s*\(for\|if\|try\)\>'
  57.       return indent(plnum)
  58.     endif
  59.  
  60.     " Or the user has already dedented
  61.     if indent(a:lnum) <= indent(plnum) - &sw
  62.       return -1
  63.     endif
  64.  
  65.     return indent(plnum) - &sw
  66.  
  67.   else
  68.     return -1
  69.  
  70.   endif
  71. endfunction
  72.  
  73. " vim:sw=2
  74.