7
|
1 " Vim indent file
|
7013
|
2 " Language: Shell Script
|
|
3 " Maintainer: Christian Brabandt <cb@256bit.org>
|
6918
|
4 " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org>
|
7013
|
5 " Original Author: Nikolai Weibull <now@bitwi.se>
|
|
6 " Latest Revision: 2015-07-28
|
|
7 " License: Vim (see :h license)
|
|
8 " Repository: https://github.com/chrisbra/vim-sh-indent
|
7
|
9
|
|
10 if exists("b:did_indent")
|
|
11 finish
|
|
12 endif
|
|
13 let b:did_indent = 1
|
|
14
|
|
15 setlocal indentexpr=GetShIndent()
|
7013
|
16 setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;&
|
2034
|
17 setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix
|
7
|
18 setlocal indentkeys-=:,0#
|
2034
|
19 setlocal nosmartindent
|
7
|
20
|
|
21 if exists("*GetShIndent")
|
|
22 finish
|
|
23 endif
|
|
24
|
375
|
25 let s:cpo_save = &cpo
|
|
26 set cpo&vim
|
7
|
27
|
2034
|
28 function s:buffer_shiftwidth()
|
|
29 return &shiftwidth
|
|
30 endfunction
|
|
31
|
|
32 let s:sh_indent_defaults = {
|
|
33 \ 'default': function('s:buffer_shiftwidth'),
|
|
34 \ 'continuation-line': function('s:buffer_shiftwidth'),
|
|
35 \ 'case-labels': function('s:buffer_shiftwidth'),
|
|
36 \ 'case-statements': function('s:buffer_shiftwidth'),
|
|
37 \ 'case-breaks': 0 }
|
|
38
|
|
39 function! s:indent_value(option)
|
|
40 let Value = exists('b:sh_indent_options')
|
|
41 \ && has_key(b:sh_indent_options, a:option) ?
|
|
42 \ b:sh_indent_options[a:option] :
|
|
43 \ s:sh_indent_defaults[a:option]
|
|
44 if type(Value) == type(function('type'))
|
|
45 return Value()
|
|
46 endif
|
|
47 return Value
|
|
48 endfunction
|
|
49
|
|
50 function! GetShIndent()
|
7
|
51 let lnum = prevnonblank(v:lnum - 1)
|
|
52 if lnum == 0
|
|
53 return 0
|
|
54 endif
|
|
55
|
2034
|
56 let pnum = prevnonblank(lnum - 1)
|
|
57
|
7
|
58 let ind = indent(lnum)
|
|
59 let line = getline(lnum)
|
7013
|
60 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>'
|
|
61 if line !~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$'
|
2034
|
62 let ind += s:indent_value('default')
|
|
63 endif
|
|
64 elseif s:is_case_label(line, pnum)
|
|
65 if !s:is_case_ended(line)
|
|
66 let ind += s:indent_value('case-statements')
|
7
|
67 endif
|
2034
|
68 elseif line =~ '^\s*\<\k\+\>\s*()\s*{' || line =~ '^\s*{'
|
|
69 if line !~ '}\s*\%(#.*\)\=$'
|
|
70 let ind += s:indent_value('default')
|
|
71 endif
|
|
72 elseif s:is_continuation_line(line)
|
|
73 if pnum == 0 || !s:is_continuation_line(getline(pnum))
|
|
74 let ind += s:indent_value('continuation-line')
|
|
75 endif
|
|
76 elseif pnum != 0 && s:is_continuation_line(getline(pnum))
|
|
77 let ind = indent(s:find_continued_lnum(pnum))
|
7
|
78 endif
|
|
79
|
2034
|
80 let pine = line
|
7
|
81 let line = getline(v:lnum)
|
7013
|
82 if line =~ '^\s*\%(then\|do\|else\|elif\|fi\|done\|end\)\>' || line =~ '^\s*}'
|
2034
|
83 let ind -= s:indent_value('default')
|
5555
|
84 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1))
|
|
85 let ind -= s:indent_value('default')
|
2034
|
86 elseif line =~ '^\s*esac\>'
|
|
87 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
|
|
88 \ 0 : s:indent_value('case-statements')) +
|
|
89 \ s:indent_value('case-labels')
|
|
90 if s:is_case_break(pine)
|
|
91 let ind += s:indent_value('case-breaks')
|
|
92 endif
|
|
93 elseif s:is_case_label(line, lnum)
|
|
94 if s:is_case(pine)
|
|
95 let ind = indent(lnum) + s:indent_value('case-labels')
|
|
96 else
|
6153
|
97 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
|
|
98 \ 0 : s:indent_value('case-statements')) -
|
|
99 \ s:indent_value('case-breaks')
|
2034
|
100 endif
|
|
101 elseif s:is_case_break(line)
|
|
102 let ind -= s:indent_value('case-breaks')
|
7
|
103 endif
|
|
104
|
|
105 return ind
|
|
106 endfunction
|
|
107
|
2034
|
108 function! s:is_continuation_line(line)
|
|
109 return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\)$'
|
|
110 endfunction
|
|
111
|
|
112 function! s:find_continued_lnum(lnum)
|
|
113 let i = a:lnum
|
|
114 while i > 1 && s:is_continuation_line(getline(i - 1))
|
|
115 let i -= 1
|
|
116 endwhile
|
|
117 return i
|
|
118 endfunction
|
|
119
|
|
120 function! s:is_case_label(line, pnum)
|
|
121 if a:line !~ '^\s*(\=.*)'
|
|
122 return 0
|
|
123 endif
|
|
124
|
|
125 if a:pnum > 0
|
|
126 let pine = getline(a:pnum)
|
|
127 if !(s:is_case(pine) || s:is_case_ended(pine))
|
|
128 return 0
|
|
129 endif
|
|
130 endif
|
|
131
|
|
132 let suffix = substitute(a:line, '^\s*(\=', "", "")
|
|
133 let nesting = 0
|
|
134 let i = 0
|
|
135 let n = strlen(suffix)
|
|
136 while i < n
|
|
137 let c = suffix[i]
|
|
138 let i += 1
|
|
139 if c == '\\'
|
|
140 let i += 1
|
|
141 elseif c == '('
|
|
142 let nesting += 1
|
|
143 elseif c == ')'
|
|
144 if nesting == 0
|
|
145 return 1
|
|
146 endif
|
|
147 let nesting -= 1
|
|
148 endif
|
|
149 endwhile
|
|
150 return 0
|
|
151 endfunction
|
|
152
|
|
153 function! s:is_case(line)
|
|
154 return a:line =~ '^\s*case\>'
|
|
155 endfunction
|
|
156
|
|
157 function! s:is_case_break(line)
|
|
158 return a:line =~ '^\s*;[;&]'
|
|
159 endfunction
|
|
160
|
|
161 function! s:is_case_ended(line)
|
|
162 return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$'
|
|
163 endfunction
|
|
164
|
5555
|
165 function! s:is_case_empty(line)
|
|
166 if a:line =~ '^\s*$' || a:line =~ '^\s*#'
|
|
167 return s:is_case_empty(getline(v:lnum - 1))
|
|
168 else
|
|
169 return a:line =~ '^\s*case\>'
|
|
170 endif
|
|
171 endfunction
|
|
172
|
375
|
173 let &cpo = s:cpo_save
|
|
174 unlet s:cpo_save
|