blob: 0a6dbc17acbacac6d67bac0bbfcc895c89b76dc1 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Vim script
3" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaardc27ac12005-07-06 22:35:45 +00004" Last Change: 2005 Jul 06
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
6" Only load this indent file when no other was loaded.
7if exists("b:did_indent")
8 finish
9endif
10let b:did_indent = 1
11
12setlocal indentexpr=GetVimIndent()
13setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\
14
15" Only define the function once.
16if exists("*GetVimIndent")
17 finish
18endif
19
20function GetVimIndent()
21 " Find a non-blank line above the current line.
22 let lnum = prevnonblank(v:lnum - 1)
23
24 " If the current line doesn't start with '\' and below a line that starts
25 " with '\', use the indent of the line above it.
26 if getline(v:lnum) !~ '^\s*\\'
27 while lnum > 0 && getline(lnum) =~ '^\s*\\'
28 let lnum = lnum - 1
29 endwhile
30 endif
31
32 " At the start of the file use zero indent.
33 if lnum == 0
34 return 0
35 endif
36
37 " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
38 " and :else. Add it three times for a line that starts with '\' after
Bram Moolenaard4755bb2004-09-02 19:12:26 +000039 " a line that doesn't (or g:vim_indent_cont if it exists).
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 let ind = indent(lnum)
41 if getline(v:lnum) =~ '^\s*\\' && v:lnum > 1 && getline(lnum) !~ '^\s*\\'
Bram Moolenaard4755bb2004-09-02 19:12:26 +000042 if exists("g:vim_indent_cont")
43 let ind = ind + g:vim_indent_cont
44 else
45 let ind = ind + &sw * 3
46 endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000047 elseif getline(lnum) =~ '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 let ind = ind + &sw
49 elseif getline(lnum) =~ '^\s*aug\%[roup]' && getline(lnum) !~ '^\s*aug\%[roup]\s*!\=\s\+END'
50 let ind = ind + &sw
51 endif
52
53 " If the previous line contains an "end" after a pipe, but not in an ":au"
Bram Moolenaar520470a2005-06-16 21:59:56 +000054 " command. And not when there is a backslash before the pipe.
Bram Moolenaardc27ac12005-07-06 22:35:45 +000055 " And when syntax HL is enabled avoid a match inside a string.
56 let line = getline(lnum)
57 let i = match(line, '[^\\]|\s*\(ene\@!\)')
58 if i > 0 && line !~ '^\s*au\%[tocmd]'
59 if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
60 let ind = ind - &sw
61 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 endif
63
64
65 " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
66 " :endfun, :else and :augroup END.
67 if getline(v:lnum) =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s*!\=\s\+END\)'
68 let ind = ind - &sw
69 endif
70
71 return ind
72endfunction
73
74" vim:sw=2