Mercurial > vim
annotate runtime/indent/sh.vim @ 13912:a9fdf01085a8
Update runtime files.
commit https://github.com/vim/vim/commit/7db25fed5de1be922b8cbb0328149469606a0424
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun May 13 00:02:36 2018 +0200
Update runtime files.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 13 May 2018 00:15:05 +0200 |
parents | 444ad56c0cac |
children | cd513458728c |
rev | line source |
---|---|
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> |
13912 | 6 " Latest Revision: 2018-05-12 |
7013 | 7 " License: Vim (see :h license) |
8 " Repository: https://github.com/chrisbra/vim-sh-indent | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
9 " Changelog: |
12045 | 10 " 20170808: - better indent of line continuation |
11442 | 11 " 20170502: - get rid of buffer-shiftwidth function |
12 " 20160912: - preserve indentation of here-doc blocks | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
13 " 20160627: - detect heredocs correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
14 " 20160213: - detect function definition correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
15 " 20160202: - use shiftwidth() function |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
16 " 20151215: - set b:undo_indent variable |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
17 " 20150728: - add foreach detection for zsh |
7 | 18 |
19 if exists("b:did_indent") | |
20 finish | |
21 endif | |
22 let b:did_indent = 1 | |
23 | |
24 setlocal indentexpr=GetShIndent() | |
7013 | 25 setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;& |
2034 | 26 setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix |
7 | 27 setlocal indentkeys-=:,0# |
2034 | 28 setlocal nosmartindent |
7 | 29 |
8246
f16bfe02cef1
commit https://github.com/vim/vim/commit/f391327adbbffb11180cf6038a92af1ed144e907
Christian Brabandt <cb@256bit.org>
parents:
7924
diff
changeset
|
30 let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' |
f16bfe02cef1
commit https://github.com/vim/vim/commit/f391327adbbffb11180cf6038a92af1ed144e907
Christian Brabandt <cb@256bit.org>
parents:
7924
diff
changeset
|
31 |
7 | 32 if exists("*GetShIndent") |
33 finish | |
34 endif | |
35 | |
375 | 36 let s:cpo_save = &cpo |
37 set cpo&vim | |
7 | 38 |
2034 | 39 let s:sh_indent_defaults = { |
11442 | 40 \ 'default': function('shiftwidth'), |
41 \ 'continuation-line': function('shiftwidth'), | |
42 \ 'case-labels': function('shiftwidth'), | |
43 \ 'case-statements': function('shiftwidth'), | |
2034 | 44 \ 'case-breaks': 0 } |
45 | |
46 function! s:indent_value(option) | |
47 let Value = exists('b:sh_indent_options') | |
48 \ && has_key(b:sh_indent_options, a:option) ? | |
49 \ b:sh_indent_options[a:option] : | |
50 \ s:sh_indent_defaults[a:option] | |
51 if type(Value) == type(function('type')) | |
52 return Value() | |
53 endif | |
54 return Value | |
55 endfunction | |
56 | |
57 function! GetShIndent() | |
7 | 58 let lnum = prevnonblank(v:lnum - 1) |
59 if lnum == 0 | |
60 return 0 | |
61 endif | |
13912 | 62 let line = getline(lnum) |
7 | 63 |
2034 | 64 let pnum = prevnonblank(lnum - 1) |
13912 | 65 let pline = getline(pnum) |
7 | 66 let ind = indent(lnum) |
13912 | 67 |
68 " Check contents of previous lines | |
69 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' || | |
70 \ (&ft is# 'zsh' && line =~ '\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>') | |
7013 | 71 if line !~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' |
2034 | 72 let ind += s:indent_value('default') |
73 endif | |
74 elseif s:is_case_label(line, pnum) | |
75 if !s:is_case_ended(line) | |
76 let ind += s:indent_value('case-statements') | |
7 | 77 endif |
13912 | 78 " function definition |
79 elseif s:is_function_definition(line) | |
2034 | 80 if line !~ '}\s*\%(#.*\)\=$' |
81 let ind += s:indent_value('default') | |
82 endif | |
83 elseif s:is_continuation_line(line) | |
13912 | 84 if pnum == 0 || !s:is_continuation_line(pline) |
2034 | 85 let ind += s:indent_value('continuation-line') |
86 endif | |
13912 | 87 elseif s:end_block(line) && !s:start_block(line) |
88 let ind -= s:indent_value('default') | |
89 elseif pnum != 0 && s:is_continuation_line(pline) && !s:end_block(getline(v:lnum)) | |
90 " only add indent, if line and pline is in the same block | |
91 let i = v:lnum | |
92 let ind2 = indent(s:find_continued_lnum(pnum)) | |
93 while !s:is_empty(getline(i)) && i > pnum | |
94 let i -= 1 | |
95 endw | |
96 if i == pnum | |
97 let ind += ind2 | |
98 else | |
99 let ind = ind2 | |
100 endif | |
7 | 101 endif |
102 | |
2034 | 103 let pine = line |
13912 | 104 " Check content of current line |
7 | 105 let line = getline(v:lnum) |
13912 | 106 if line =~ '^\s*\%(then\|do\|else\|elif\|fi\|done\|end\)\>' || s:end_block(line) |
2034 | 107 let ind -= s:indent_value('default') |
5555 | 108 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
109 let ind -= s:indent_value('default') | |
2034 | 110 elseif line =~ '^\s*esac\>' |
111 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? | |
112 \ 0 : s:indent_value('case-statements')) + | |
113 \ s:indent_value('case-labels') | |
114 if s:is_case_break(pine) | |
115 let ind += s:indent_value('case-breaks') | |
116 endif | |
117 elseif s:is_case_label(line, lnum) | |
118 if s:is_case(pine) | |
119 let ind = indent(lnum) + s:indent_value('case-labels') | |
120 else | |
6153 | 121 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? |
122 \ 0 : s:indent_value('case-statements')) - | |
123 \ s:indent_value('case-breaks') | |
2034 | 124 endif |
125 elseif s:is_case_break(line) | |
126 let ind -= s:indent_value('case-breaks') | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
127 elseif s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
128 let ind = 0 |
11442 | 129 " statements, executed within a here document. Keep the current indent |
130 elseif match(map(synstack(v:lnum, 1), 'synIDattr(v:val, "name")'), '\c\mheredoc') > -1 | |
131 return indent(v:lnum) | |
13912 | 132 elseif s:is_comment(line) && s:is_empty(getline(v:lnum-1)) |
133 return indent(v:lnum) | |
7 | 134 endif |
135 | |
13912 | 136 return ind > 0 ? ind : 0 |
7 | 137 endfunction |
138 | |
2034 | 139 function! s:is_continuation_line(line) |
13912 | 140 " Comment, cannot be a line continuation |
141 if a:line =~ '^\s*#' | |
142 return 0 | |
143 else | |
144 " start-of-line | |
145 " \\ or && or || or | | |
146 " followed optionally by { or # | |
147 return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\||\)' . | |
12045 | 148 \ '\s*\({\s*\)\=\(#.*\)\=$' |
13912 | 149 endif |
2034 | 150 endfunction |
151 | |
152 function! s:find_continued_lnum(lnum) | |
153 let i = a:lnum | |
154 while i > 1 && s:is_continuation_line(getline(i - 1)) | |
155 let i -= 1 | |
156 endwhile | |
157 return i | |
158 endfunction | |
159 | |
13912 | 160 function! s:is_function_definition(line) |
161 return a:line =~ '^\s*\<\k\+\>\s*()\s*{' || | |
162 \ a:line =~ '^\s*{' || | |
163 \ a:line =~ '^\s*function\s*\w\S\+\s*\%(()\)\?\s*{' | |
164 endfunction | |
165 | |
2034 | 166 function! s:is_case_label(line, pnum) |
167 if a:line !~ '^\s*(\=.*)' | |
168 return 0 | |
169 endif | |
170 | |
171 if a:pnum > 0 | |
172 let pine = getline(a:pnum) | |
173 if !(s:is_case(pine) || s:is_case_ended(pine)) | |
174 return 0 | |
175 endif | |
176 endif | |
177 | |
178 let suffix = substitute(a:line, '^\s*(\=', "", "") | |
179 let nesting = 0 | |
180 let i = 0 | |
181 let n = strlen(suffix) | |
182 while i < n | |
183 let c = suffix[i] | |
184 let i += 1 | |
185 if c == '\\' | |
186 let i += 1 | |
187 elseif c == '(' | |
188 let nesting += 1 | |
189 elseif c == ')' | |
190 if nesting == 0 | |
191 return 1 | |
192 endif | |
193 let nesting -= 1 | |
194 endif | |
195 endwhile | |
196 return 0 | |
197 endfunction | |
198 | |
199 function! s:is_case(line) | |
200 return a:line =~ '^\s*case\>' | |
201 endfunction | |
202 | |
203 function! s:is_case_break(line) | |
204 return a:line =~ '^\s*;[;&]' | |
205 endfunction | |
206 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
207 function! s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
208 if a:line =~ '^\w\+$' |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
209 let here_pat = '<<-\?'. s:escape(a:line). '\$' |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
210 return search(here_pat, 'bnW') > 0 |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
211 endif |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
212 return 0 |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
213 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
214 |
2034 | 215 function! s:is_case_ended(line) |
216 return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$' | |
217 endfunction | |
218 | |
5555 | 219 function! s:is_case_empty(line) |
220 if a:line =~ '^\s*$' || a:line =~ '^\s*#' | |
221 return s:is_case_empty(getline(v:lnum - 1)) | |
222 else | |
223 return a:line =~ '^\s*case\>' | |
224 endif | |
225 endfunction | |
226 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
227 function! s:escape(pattern) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
228 return '\V'. escape(a:pattern, '\\') |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
229 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
230 |
13912 | 231 function! s:is_empty(line) |
232 return a:line =~ '^\s*$' | |
233 endfunction | |
234 | |
235 function! s:end_block(line) | |
236 return a:line =~ '^\s*}' | |
237 endfunction | |
238 | |
239 function! s:start_block(line) | |
240 return a:line =~ '{\s*\(#.*\)\?$' | |
241 endfunction | |
242 | |
243 function! s:find_start_block(lnum) | |
244 let i = a:lnum | |
245 while i > 1 && !s:start_block(getline(i)) | |
246 let i -= 1 | |
247 endwhile | |
248 return i | |
249 endfunction | |
250 | |
251 function! s:is_comment(line) | |
252 return a:line =~ '^\s*#' | |
253 endfunction | |
254 | |
375 | 255 let &cpo = s:cpo_save |
256 unlet s:cpo_save |