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