home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / runtime / indent / vim.vim < prev    next >
Encoding:
Text File  |  2012-05-31  |  2.6 KB  |  90 lines

  1. " Vim indent file
  2. " Language:    Vim script
  3. " Maintainer:    Bram Moolenaar <Bram@vim.org>
  4. " Last Change:    2012 May 20
  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. setlocal indentexpr=GetVimIndent()
  13. setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\
  14.  
  15. let b:undo_indent = "setl indentkeys< indentexpr<"
  16.  
  17. " Only define the function once.
  18. if exists("*GetVimIndent")
  19.   finish
  20. endif
  21. let s:keepcpo= &cpo
  22. set cpo&vim
  23.  
  24. function GetVimIndent()
  25.   " Find a non-blank line above the current line.
  26.   let lnum = prevnonblank(v:lnum - 1)
  27.  
  28.   " If the current line doesn't start with '\' and below a line that starts
  29.   " with '\', use the indent of the line above it.
  30.   if getline(v:lnum) !~ '^\s*\\'
  31.     while lnum > 0 && getline(lnum) =~ '^\s*\\'
  32.       let lnum = lnum - 1
  33.     endwhile
  34.   endif
  35.  
  36.   " At the start of the file use zero indent.
  37.   if lnum == 0
  38.     return 0
  39.   endif
  40.  
  41.   " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
  42.   " and :else.  Add it three times for a line that starts with '\' after
  43.   " a line that doesn't (or g:vim_indent_cont if it exists).
  44.   let ind = indent(lnum)
  45.   if getline(v:lnum) =~ '^\s*\\' && v:lnum > 1 && getline(lnum) !~ '^\s*\\'
  46.     if exists("g:vim_indent_cont")
  47.       let ind = ind + g:vim_indent_cont
  48.     else
  49.       let ind = ind + &sw * 3
  50.     endif
  51.   elseif getline(lnum) =~ '^\s*aug\%[roup]' && getline(lnum) !~ '^\s*aug\%[roup]\s*!\=\s\+END'
  52.     let ind = ind + &sw
  53.   else
  54.     let line = getline(lnum)
  55.     let i = match(line, '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>')
  56.     if i >= 0
  57.       let ind += &sw
  58.       if strpart(line, i, 1) == '|' && has('syntax_items')
  59.             \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$'
  60.         let ind -= &sw
  61.       endif
  62.     endif
  63.   endif
  64.  
  65.   " If the previous line contains an "end" after a pipe, but not in an ":au"
  66.   " command.  And not when there is a backslash before the pipe.
  67.   " And when syntax HL is enabled avoid a match inside a string.
  68.   let line = getline(lnum)
  69.   let i = match(line, '[^\\]|\s*\(ene\@!\)')
  70.   if i > 0 && line !~ '^\s*au\%[tocmd]'
  71.     if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
  72.       let ind = ind - &sw
  73.     endif
  74.   endif
  75.  
  76.  
  77.   " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
  78.   " :endfun, :else and :augroup END.
  79.   if getline(v:lnum) =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s*!\=\s\+END\)'
  80.     let ind = ind - &sw
  81.   endif
  82.  
  83.   return ind
  84. endfunction
  85.  
  86. let &cpo = s:keepcpo
  87. unlet s:keepcpo
  88.  
  89. " vim:sw=2
  90.