Mercurial > vim
annotate runtime/indent/sh.vim @ 16267:b471858040bc
Update runtime files.
commit https://github.com/vim/vim/commit/62e1bb4a111e7ce570c30965f40a68a07a9da5b0
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Apr 8 16:25:07 2019 +0200
Update runtime files.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 08 Apr 2019 16:30:06 +0200 |
parents | a23c883685cb |
children | 1eaf34420bb3 |
rev | line source |
---|---|
7 | 1 " Vim indent file |
7013 | 2 " Language: Shell Script |
3 " Maintainer: Christian Brabandt <cb@256bit.org> | |
13937 | 4 " Original Author: Nikolai Weibull <now@bitwi.se> |
6918 | 5 " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org> |
16267 | 6 " Latest Revision: 2019-03-25 |
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: |
16267 | 10 " 20190325 - Indent fi; correctly |
11 " https://github.com/chrisbra/vim-sh-indent/issues/14 | |
12 " 20190319 - Indent arrays (only zsh and bash) | |
13 " https://github.com/chrisbra/vim-sh-indent/issues/13 | |
14 " 20190316 - Make use of searchpairpos for nested if sections | |
15 " fixes https://github.com/chrisbra/vim-sh-indent/issues/11 | |
16 " 20190201 - Better check for closing if sections | |
14421 | 17 " 20180724 - make check for zsh syntax more rigid (needs word-boundaries) |
13937 | 18 " 20180326 - better support for line continuation |
19 " 20180325 - better detection of function definitions | |
20 " 20180127 - better support for zsh complex commands | |
12045 | 21 " 20170808: - better indent of line continuation |
11442 | 22 " 20170502: - get rid of buffer-shiftwidth function |
23 " 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
|
24 " 20160627: - detect heredocs correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
25 " 20160213: - detect function definition correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
26 " 20160202: - use shiftwidth() function |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
27 " 20151215: - set b:undo_indent variable |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
28 " 20150728: - add foreach detection for zsh |
7 | 29 |
30 if exists("b:did_indent") | |
31 finish | |
32 endif | |
33 let b:did_indent = 1 | |
34 | |
35 setlocal indentexpr=GetShIndent() | |
7013 | 36 setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;& |
2034 | 37 setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix |
7 | 38 setlocal indentkeys-=:,0# |
2034 | 39 setlocal nosmartindent |
7 | 40 |
8246
f16bfe02cef1
commit https://github.com/vim/vim/commit/f391327adbbffb11180cf6038a92af1ed144e907
Christian Brabandt <cb@256bit.org>
parents:
7924
diff
changeset
|
41 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
|
42 |
7 | 43 if exists("*GetShIndent") |
44 finish | |
45 endif | |
46 | |
375 | 47 let s:cpo_save = &cpo |
48 set cpo&vim | |
7 | 49 |
2034 | 50 let s:sh_indent_defaults = { |
11442 | 51 \ 'default': function('shiftwidth'), |
52 \ 'continuation-line': function('shiftwidth'), | |
53 \ 'case-labels': function('shiftwidth'), | |
54 \ 'case-statements': function('shiftwidth'), | |
2034 | 55 \ 'case-breaks': 0 } |
56 | |
57 function! s:indent_value(option) | |
58 let Value = exists('b:sh_indent_options') | |
59 \ && has_key(b:sh_indent_options, a:option) ? | |
60 \ b:sh_indent_options[a:option] : | |
61 \ s:sh_indent_defaults[a:option] | |
62 if type(Value) == type(function('type')) | |
63 return Value() | |
64 endif | |
65 return Value | |
66 endfunction | |
67 | |
68 function! GetShIndent() | |
16267 | 69 let curline = getline(v:lnum) |
7 | 70 let lnum = prevnonblank(v:lnum - 1) |
71 if lnum == 0 | |
72 return 0 | |
73 endif | |
13912 | 74 let line = getline(lnum) |
7 | 75 |
2034 | 76 let pnum = prevnonblank(lnum - 1) |
13912 | 77 let pline = getline(pnum) |
7 | 78 let ind = indent(lnum) |
13912 | 79 |
80 " Check contents of previous lines | |
81 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' || | |
14421 | 82 \ (&ft is# 'zsh' && line =~ '\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>') |
16267 | 83 if !s:is_end_expression(line) |
2034 | 84 let ind += s:indent_value('default') |
85 endif | |
86 elseif s:is_case_label(line, pnum) | |
87 if !s:is_case_ended(line) | |
88 let ind += s:indent_value('case-statements') | |
7 | 89 endif |
13912 | 90 " function definition |
91 elseif s:is_function_definition(line) | |
2034 | 92 if line !~ '}\s*\%(#.*\)\=$' |
93 let ind += s:indent_value('default') | |
94 endif | |
16267 | 95 " array (only works for zsh or bash) |
96 elseif s:is_array(line) && line !~ ')\s*$' && (&ft is# 'zsh' || s:is_bash()) | |
97 let ind += s:indent_value('continuation-line') | |
98 " end of array | |
99 elseif curline =~ '^\s*)$' | |
100 let ind -= s:indent_value('continuation-line') | |
2034 | 101 elseif s:is_continuation_line(line) |
13912 | 102 if pnum == 0 || !s:is_continuation_line(pline) |
2034 | 103 let ind += s:indent_value('continuation-line') |
104 endif | |
13912 | 105 elseif s:end_block(line) && !s:start_block(line) |
106 let ind -= s:indent_value('default') | |
16267 | 107 elseif pnum != 0 && |
108 \ s:is_continuation_line(pline) && | |
109 \ !s:end_block(curline) && | |
110 \ !s:is_end_expression(curline) | |
13912 | 111 " only add indent, if line and pline is in the same block |
112 let i = v:lnum | |
113 let ind2 = indent(s:find_continued_lnum(pnum)) | |
114 while !s:is_empty(getline(i)) && i > pnum | |
115 let i -= 1 | |
116 endw | |
117 if i == pnum | |
118 let ind += ind2 | |
119 else | |
120 let ind = ind2 | |
121 endif | |
7 | 122 endif |
123 | |
2034 | 124 let pine = line |
13912 | 125 " Check content of current line |
16267 | 126 let line = curline |
127 " Current line is a endif line, so get indent from start of "if condition" line | |
128 " TODO: should we do the same for other "end" lines? | |
129 if curline =~ '^\s*\%(fi\);\?\s*\%(#.*\)\=$' | |
130 let previous_line = searchpair('\<if\>', '', '\<fi\>', 'bnW') | |
131 if previous_line > 0 | |
132 let ind = indent(previous_line) | |
133 endif | |
134 elseif line =~ '^\s*\%(then\|do\|else\|elif\|done\|end\)\>' || s:end_block(line) | |
2034 | 135 let ind -= s:indent_value('default') |
5555 | 136 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
137 let ind -= s:indent_value('default') | |
2034 | 138 elseif line =~ '^\s*esac\>' |
139 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? | |
140 \ 0 : s:indent_value('case-statements')) + | |
141 \ s:indent_value('case-labels') | |
142 if s:is_case_break(pine) | |
143 let ind += s:indent_value('case-breaks') | |
144 endif | |
145 elseif s:is_case_label(line, lnum) | |
146 if s:is_case(pine) | |
147 let ind = indent(lnum) + s:indent_value('case-labels') | |
148 else | |
6153 | 149 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? |
150 \ 0 : s:indent_value('case-statements')) - | |
151 \ s:indent_value('case-breaks') | |
2034 | 152 endif |
153 elseif s:is_case_break(line) | |
154 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
|
155 elseif s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
156 let ind = 0 |
11442 | 157 " statements, executed within a here document. Keep the current indent |
158 elseif match(map(synstack(v:lnum, 1), 'synIDattr(v:val, "name")'), '\c\mheredoc') > -1 | |
159 return indent(v:lnum) | |
13912 | 160 elseif s:is_comment(line) && s:is_empty(getline(v:lnum-1)) |
161 return indent(v:lnum) | |
7 | 162 endif |
163 | |
13912 | 164 return ind > 0 ? ind : 0 |
7 | 165 endfunction |
166 | |
2034 | 167 function! s:is_continuation_line(line) |
13912 | 168 " Comment, cannot be a line continuation |
169 if a:line =~ '^\s*#' | |
170 return 0 | |
171 else | |
172 " start-of-line | |
173 " \\ or && or || or | | |
174 " followed optionally by { or # | |
175 return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\||\)' . | |
12045 | 176 \ '\s*\({\s*\)\=\(#.*\)\=$' |
13912 | 177 endif |
2034 | 178 endfunction |
179 | |
180 function! s:find_continued_lnum(lnum) | |
181 let i = a:lnum | |
182 while i > 1 && s:is_continuation_line(getline(i - 1)) | |
183 let i -= 1 | |
184 endwhile | |
185 return i | |
186 endfunction | |
187 | |
13912 | 188 function! s:is_function_definition(line) |
189 return a:line =~ '^\s*\<\k\+\>\s*()\s*{' || | |
190 \ a:line =~ '^\s*{' || | |
191 \ a:line =~ '^\s*function\s*\w\S\+\s*\%(()\)\?\s*{' | |
192 endfunction | |
193 | |
16267 | 194 function! s:is_array(line) |
195 return a:line =~ '^\s*\<\k\+\>=(' | |
196 endfunction | |
197 | |
2034 | 198 function! s:is_case_label(line, pnum) |
199 if a:line !~ '^\s*(\=.*)' | |
200 return 0 | |
201 endif | |
202 | |
203 if a:pnum > 0 | |
204 let pine = getline(a:pnum) | |
205 if !(s:is_case(pine) || s:is_case_ended(pine)) | |
206 return 0 | |
207 endif | |
208 endif | |
209 | |
210 let suffix = substitute(a:line, '^\s*(\=', "", "") | |
211 let nesting = 0 | |
212 let i = 0 | |
213 let n = strlen(suffix) | |
214 while i < n | |
215 let c = suffix[i] | |
216 let i += 1 | |
217 if c == '\\' | |
218 let i += 1 | |
219 elseif c == '(' | |
220 let nesting += 1 | |
221 elseif c == ')' | |
222 if nesting == 0 | |
223 return 1 | |
224 endif | |
225 let nesting -= 1 | |
226 endif | |
227 endwhile | |
228 return 0 | |
229 endfunction | |
230 | |
231 function! s:is_case(line) | |
232 return a:line =~ '^\s*case\>' | |
233 endfunction | |
234 | |
235 function! s:is_case_break(line) | |
236 return a:line =~ '^\s*;[;&]' | |
237 endfunction | |
238 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
239 function! s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
240 if a:line =~ '^\w\+$' |
16267 | 241 let here_pat = '<<-\?'. s:escape(a:line). '\$' |
242 return search(here_pat, 'bnW') > 0 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
243 endif |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
244 return 0 |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
245 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
246 |
2034 | 247 function! s:is_case_ended(line) |
248 return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$' | |
249 endfunction | |
250 | |
5555 | 251 function! s:is_case_empty(line) |
252 if a:line =~ '^\s*$' || a:line =~ '^\s*#' | |
253 return s:is_case_empty(getline(v:lnum - 1)) | |
254 else | |
255 return a:line =~ '^\s*case\>' | |
256 endif | |
257 endfunction | |
258 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
259 function! s:escape(pattern) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
260 return '\V'. escape(a:pattern, '\\') |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
261 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
262 |
13912 | 263 function! s:is_empty(line) |
264 return a:line =~ '^\s*$' | |
265 endfunction | |
266 | |
267 function! s:end_block(line) | |
268 return a:line =~ '^\s*}' | |
269 endfunction | |
270 | |
271 function! s:start_block(line) | |
272 return a:line =~ '{\s*\(#.*\)\?$' | |
273 endfunction | |
274 | |
275 function! s:find_start_block(lnum) | |
276 let i = a:lnum | |
277 while i > 1 && !s:start_block(getline(i)) | |
278 let i -= 1 | |
279 endwhile | |
280 return i | |
281 endfunction | |
282 | |
283 function! s:is_comment(line) | |
284 return a:line =~ '^\s*#' | |
285 endfunction | |
286 | |
16267 | 287 function! s:is_end_expression(line) |
288 return a:line =~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' | |
289 endfunction | |
290 | |
291 function! s:is_bash() | |
292 return get(g:, 'is_bash', 0) || get(b:, 'is_bash', 0) | |
293 endfunction | |
294 | |
375 | 295 let &cpo = s:cpo_save |
296 unlet s:cpo_save |