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