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

  1. " Vim indent file
  2. " Language:    Ruby
  3. " Maintainer:    Matt Armstrong <matt@lickey.com>
  4. " Last Change:    2001 Jul 28
  5. " URL: http://www.lickey.com/env/vim/indent/ruby.vim
  6.  
  7. " Only load this indent file when no other was loaded.
  8. if exists("b:did_indent")
  9.   finish
  10. endif
  11. let b:did_indent = 1
  12.  
  13. setlocal indentexpr=GetRubyIndent()
  14. setlocal nolisp
  15. setlocal nosmartindent
  16. setlocal autoindent
  17. setlocal indentkeys+==end,=else,=elsif,=when,=ensure,=rescue
  18.  
  19. " Only define the function once.
  20. if exists("*GetRubyIndent")
  21.   finish
  22. endif
  23.  
  24. function GetRubyIndent()
  25.   " Find a non-blank line above the current line.
  26.   let lnum = prevnonblank(v:lnum - 1)
  27.  
  28.   " At the start of the file use zero indent.
  29.   if lnum == 0
  30.     return 0
  31.   endif
  32.  
  33.   " If the line trailed with a *, +, comma, . or (, trust the user
  34.   if getline(lnum) =~ '\(\*\|\.\|+\|,\|(\)\(\s*#.*\)\=$'
  35.     return -1
  36.   endif
  37.  
  38.   " Add a 'shiftwidth' after lines beginning with:
  39.   " module, class, dev, if, for, while, until, else, elsif, case, when, {
  40.   let ind = indent(lnum)
  41.   let flag = 0
  42.   if getline(lnum) =~ '^\s*\(module\>\|class\>\|def\>\|if\>\|for\>\|while\>\|until\>\|else\>\|elsif\>\|case\>\|when\>\|unless\|begin\|ensure\>\|rescue\>\)' || getline(lnum) =~ '{\s*$' || getline(lnum) =~ '\({\|\<do\>\).*|.*|\s*$' || getline(lnum) =~ '\<do\>\(\s*#.*\)\=$'
  43.     let ind = ind + &sw
  44.     let flag = 1
  45.   endif
  46.  
  47.   " Subtract a 'shiftwidth' after lines ending with
  48.   " "end" when they begin with while, if, for, until
  49.   if flag == 1 && getline(lnum) =~ '\<end\>\(\s*#.*\)\=$'
  50.     let ind = ind - &sw
  51.   endif
  52.  
  53.   " Subtract a 'shiftwidth' on end, else and, elsif, when and }
  54.   if getline(v:lnum) =~ '^\s*\(end\>\|else\>\|elsif\>\|when\>\|ensure\>\|rescue\>\|}\)'
  55.     let ind = ind - &sw
  56.   endif
  57.  
  58.   return ind
  59. endfunction
  60.  
  61. " vim:sw=2
  62.