2 " Language: Shell Script
3 " Maintainer: Nikolai Weibull <now@bitwi.se>
4 " Latest Revision: 2010-01-06
6 if exists("b:did_indent")
11 setlocal indentexpr=GetShIndent()
12 setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,),0=;;,0=;&
13 setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix
14 setlocal indentkeys-=:,0#
15 setlocal nosmartindent
17 if exists("*GetShIndent")
24 function s:buffer_shiftwidth()
28 let s:sh_indent_defaults = {
29 \ 'default': function('s:buffer_shiftwidth'),
30 \ 'continuation-line': function('s:buffer_shiftwidth'),
31 \ 'case-labels': function('s:buffer_shiftwidth'),
32 \ 'case-statements': function('s:buffer_shiftwidth'),
35 function! s:indent_value(option)
36 let Value = exists('b:sh_indent_options')
37 \ && has_key(b:sh_indent_options, a:option) ?
38 \ b:sh_indent_options[a:option] :
39 \ s:sh_indent_defaults[a:option]
40 if type(Value) == type(function('type'))
46 function! GetShIndent()
47 let lnum = prevnonblank(v:lnum - 1)
52 let pnum = prevnonblank(lnum - 1)
54 let ind = indent(lnum)
55 let line = getline(lnum)
56 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\)\>'
57 if line !~ '\<\%(fi\|esac\|done\)\>\s*\%(#.*\)\=$'
58 let ind += s:indent_value('default')
60 elseif s:is_case_label(line, pnum)
61 if !s:is_case_ended(line)
62 let ind += s:indent_value('case-statements')
64 elseif line =~ '^\s*\<\k\+\>\s*()\s*{' || line =~ '^\s*{'
65 if line !~ '}\s*\%(#.*\)\=$'
66 let ind += s:indent_value('default')
68 elseif s:is_continuation_line(line)
69 if pnum == 0 || !s:is_continuation_line(getline(pnum))
70 let ind += s:indent_value('continuation-line')
72 elseif pnum != 0 && s:is_continuation_line(getline(pnum))
73 let ind = indent(s:find_continued_lnum(pnum))
77 let line = getline(v:lnum)
78 if line =~ '^\s*\%(then\|do\|else\|elif\|fi\|done\)\>' || line =~ '^\s*}'
79 let ind -= s:indent_value('default')
80 elseif line =~ '^\s*esac\>'
81 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
82 \ 0 : s:indent_value('case-statements')) +
83 \ s:indent_value('case-labels')
84 if s:is_case_break(pine)
85 let ind += s:indent_value('case-breaks')
87 elseif s:is_case_label(line, lnum)
89 let ind = indent(lnum) + s:indent_value('case-labels')
91 let ind -= s:indent_value('case-statements') - s:indent_value('case-breaks')
93 elseif s:is_case_break(line)
94 let ind -= s:indent_value('case-breaks')
100 function! s:is_continuation_line(line)
101 return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\)$'
104 function! s:find_continued_lnum(lnum)
106 while i > 1 && s:is_continuation_line(getline(i - 1))
112 function! s:is_case_label(line, pnum)
113 if a:line !~ '^\s*(\=.*)'
118 let pine = getline(a:pnum)
119 if !(s:is_case(pine) || s:is_case_ended(pine))
124 let suffix = substitute(a:line, '^\s*(\=', "", "")
127 let n = strlen(suffix)
145 function! s:is_case(line)
146 return a:line =~ '^\s*case\>'
149 function! s:is_case_break(line)
150 return a:line =~ '^\s*;[;&]'
153 function! s:is_case_ended(line)
154 return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$'
157 let &cpo = s:cpo_save