blob: 8281d94c09100bb303cbb8bfce90b08a5ba94d76 [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 Moolenaar8e52a592012-05-18 21:49:28 +02004" Last Change: 2012 May 18
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
Bram Moolenaar8e52a592012-05-18 21:49:28 +020019let s:keepcpo= &cpo
20set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22function GetVimIndent()
23 " Find a non-blank line above the current line.
24 let lnum = prevnonblank(v:lnum - 1)
25
26 " If the current line doesn't start with '\' and below a line that starts
27 " with '\', use the indent of the line above it.
28 if getline(v:lnum) !~ '^\s*\\'
29 while lnum > 0 && getline(lnum) =~ '^\s*\\'
30 let lnum = lnum - 1
31 endwhile
32 endif
33
34 " At the start of the file use zero indent.
35 if lnum == 0
36 return 0
37 endif
38
39 " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
40 " and :else. Add it three times for a line that starts with '\' after
Bram Moolenaard4755bb2004-09-02 19:12:26 +000041 " a line that doesn't (or g:vim_indent_cont if it exists).
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 let ind = indent(lnum)
43 if getline(v:lnum) =~ '^\s*\\' && v:lnum > 1 && getline(lnum) !~ '^\s*\\'
Bram Moolenaard4755bb2004-09-02 19:12:26 +000044 if exists("g:vim_indent_cont")
45 let ind = ind + g:vim_indent_cont
46 else
47 let ind = ind + &sw * 3
48 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 elseif getline(lnum) =~ '^\s*aug\%[roup]' && getline(lnum) !~ '^\s*aug\%[roup]\s*!\=\s\+END'
50 let ind = ind + &sw
Bram Moolenaarcab49df2011-03-22 17:40:10 +010051 else
52 let line = getline(lnum)
53 let i = match(line, '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>')
54 if i >= 0
55 let ind += &sw
56 if strpart(line, i, 1) == '|' && has('syntax_items')
57 \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$'
58 let ind -= &sw
59 endif
60 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 endif
62
63 " If the previous line contains an "end" after a pipe, but not in an ":au"
Bram Moolenaar520470a2005-06-16 21:59:56 +000064 " command. And not when there is a backslash before the pipe.
Bram Moolenaardc27ac12005-07-06 22:35:45 +000065 " And when syntax HL is enabled avoid a match inside a string.
66 let line = getline(lnum)
67 let i = match(line, '[^\\]|\s*\(ene\@!\)')
68 if i > 0 && line !~ '^\s*au\%[tocmd]'
69 if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
70 let ind = ind - &sw
71 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 endif
73
74
75 " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
76 " :endfun, :else and :augroup END.
77 if getline(v:lnum) =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s*!\=\s\+END\)'
78 let ind = ind - &sw
79 endif
80
81 return ind
82endfunction
83
Bram Moolenaar8e52a592012-05-18 21:49:28 +020084let &cpo = s:keepcpo
85unlet s:keepcpo
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087" vim:sw=2