Mercurial > vim
annotate runtime/indent/sh.vim @ 11442:d183d629509e
Update runtime files.
commit https://github.com/vim/vim/commit/b4d6c3ea4a59c6d8d4e0e52120596866f0edd510
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat May 27 16:45:17 2017 +0200
Update runtime files.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sat, 27 May 2017 17:00:04 +0200 |
parents | 619a98a67f67 |
children | 444ad56c0cac |
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> |
11442 | 6 " Latest Revision: 2017-05-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: |
11442 | 10 " 20170502: - get rid of buffer-shiftwidth function |
11 " 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
|
12 " 20160627: - detect heredocs correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
13 " 20160213: - detect function definition correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
14 " 20160202: - use shiftwidth() function |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
15 " 20151215: - set b:undo_indent variable |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
16 " 20150728: - add foreach detection for zsh |
7 | 17 |
18 if exists("b:did_indent") | |
19 finish | |
20 endif | |
21 let b:did_indent = 1 | |
22 | |
23 setlocal indentexpr=GetShIndent() | |
7013 | 24 setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;& |
2034 | 25 setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix |
7 | 26 setlocal indentkeys-=:,0# |
2034 | 27 setlocal nosmartindent |
7 | 28 |
8246
f16bfe02cef1
commit https://github.com/vim/vim/commit/f391327adbbffb11180cf6038a92af1ed144e907
Christian Brabandt <cb@256bit.org>
parents:
7924
diff
changeset
|
29 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
|
30 |
7 | 31 if exists("*GetShIndent") |
32 finish | |
33 endif | |
34 | |
375 | 35 let s:cpo_save = &cpo |
36 set cpo&vim | |
7 | 37 |
2034 | 38 let s:sh_indent_defaults = { |
11442 | 39 \ 'default': function('shiftwidth'), |
40 \ 'continuation-line': function('shiftwidth'), | |
41 \ 'case-labels': function('shiftwidth'), | |
42 \ 'case-statements': function('shiftwidth'), | |
2034 | 43 \ 'case-breaks': 0 } |
44 | |
45 function! s:indent_value(option) | |
46 let Value = exists('b:sh_indent_options') | |
47 \ && has_key(b:sh_indent_options, a:option) ? | |
48 \ b:sh_indent_options[a:option] : | |
49 \ s:sh_indent_defaults[a:option] | |
50 if type(Value) == type(function('type')) | |
51 return Value() | |
52 endif | |
53 return Value | |
54 endfunction | |
55 | |
56 function! GetShIndent() | |
7 | 57 let lnum = prevnonblank(v:lnum - 1) |
58 if lnum == 0 | |
59 return 0 | |
60 endif | |
61 | |
2034 | 62 let pnum = prevnonblank(lnum - 1) |
63 | |
7 | 64 let ind = indent(lnum) |
65 let line = getline(lnum) | |
7013 | 66 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' |
67 if line !~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' | |
2034 | 68 let ind += s:indent_value('default') |
69 endif | |
70 elseif s:is_case_label(line, pnum) | |
71 if !s:is_case_ended(line) | |
72 let ind += s:indent_value('case-statements') | |
7 | 73 endif |
8246
f16bfe02cef1
commit https://github.com/vim/vim/commit/f391327adbbffb11180cf6038a92af1ed144e907
Christian Brabandt <cb@256bit.org>
parents:
7924
diff
changeset
|
74 elseif line =~ '^\s*\<\k\+\>\s*()\s*{' || line =~ '^\s*{' || line =~ '^\s*function\s*\w\S\+\s*\%(()\)\?\s*{' |
2034 | 75 if line !~ '}\s*\%(#.*\)\=$' |
76 let ind += s:indent_value('default') | |
77 endif | |
78 elseif s:is_continuation_line(line) | |
79 if pnum == 0 || !s:is_continuation_line(getline(pnum)) | |
80 let ind += s:indent_value('continuation-line') | |
81 endif | |
82 elseif pnum != 0 && s:is_continuation_line(getline(pnum)) | |
83 let ind = indent(s:find_continued_lnum(pnum)) | |
7 | 84 endif |
85 | |
2034 | 86 let pine = line |
7 | 87 let line = getline(v:lnum) |
7013 | 88 if line =~ '^\s*\%(then\|do\|else\|elif\|fi\|done\|end\)\>' || line =~ '^\s*}' |
2034 | 89 let ind -= s:indent_value('default') |
5555 | 90 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
91 let ind -= s:indent_value('default') | |
2034 | 92 elseif line =~ '^\s*esac\>' |
93 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? | |
94 \ 0 : s:indent_value('case-statements')) + | |
95 \ s:indent_value('case-labels') | |
96 if s:is_case_break(pine) | |
97 let ind += s:indent_value('case-breaks') | |
98 endif | |
99 elseif s:is_case_label(line, lnum) | |
100 if s:is_case(pine) | |
101 let ind = indent(lnum) + s:indent_value('case-labels') | |
102 else | |
6153 | 103 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? |
104 \ 0 : s:indent_value('case-statements')) - | |
105 \ s:indent_value('case-breaks') | |
2034 | 106 endif |
107 elseif s:is_case_break(line) | |
108 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
|
109 elseif s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
110 let ind = 0 |
11442 | 111 " statements, executed within a here document. Keep the current indent |
112 elseif match(map(synstack(v:lnum, 1), 'synIDattr(v:val, "name")'), '\c\mheredoc') > -1 | |
113 return indent(v:lnum) | |
7 | 114 endif |
115 | |
116 return ind | |
117 endfunction | |
118 | |
2034 | 119 function! s:is_continuation_line(line) |
120 return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\)$' | |
121 endfunction | |
122 | |
123 function! s:find_continued_lnum(lnum) | |
124 let i = a:lnum | |
125 while i > 1 && s:is_continuation_line(getline(i - 1)) | |
126 let i -= 1 | |
127 endwhile | |
128 return i | |
129 endfunction | |
130 | |
131 function! s:is_case_label(line, pnum) | |
132 if a:line !~ '^\s*(\=.*)' | |
133 return 0 | |
134 endif | |
135 | |
136 if a:pnum > 0 | |
137 let pine = getline(a:pnum) | |
138 if !(s:is_case(pine) || s:is_case_ended(pine)) | |
139 return 0 | |
140 endif | |
141 endif | |
142 | |
143 let suffix = substitute(a:line, '^\s*(\=', "", "") | |
144 let nesting = 0 | |
145 let i = 0 | |
146 let n = strlen(suffix) | |
147 while i < n | |
148 let c = suffix[i] | |
149 let i += 1 | |
150 if c == '\\' | |
151 let i += 1 | |
152 elseif c == '(' | |
153 let nesting += 1 | |
154 elseif c == ')' | |
155 if nesting == 0 | |
156 return 1 | |
157 endif | |
158 let nesting -= 1 | |
159 endif | |
160 endwhile | |
161 return 0 | |
162 endfunction | |
163 | |
164 function! s:is_case(line) | |
165 return a:line =~ '^\s*case\>' | |
166 endfunction | |
167 | |
168 function! s:is_case_break(line) | |
169 return a:line =~ '^\s*;[;&]' | |
170 endfunction | |
171 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
172 function! s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
173 if a:line =~ '^\w\+$' |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
174 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
|
175 return search(here_pat, 'bnW') > 0 |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
176 endif |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
177 return 0 |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
178 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
179 |
2034 | 180 function! s:is_case_ended(line) |
181 return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$' | |
182 endfunction | |
183 | |
5555 | 184 function! s:is_case_empty(line) |
185 if a:line =~ '^\s*$' || a:line =~ '^\s*#' | |
186 return s:is_case_empty(getline(v:lnum - 1)) | |
187 else | |
188 return a:line =~ '^\s*case\>' | |
189 endif | |
190 endfunction | |
191 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
192 function! s:escape(pattern) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
193 return '\V'. escape(a:pattern, '\\') |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
194 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
195 |
375 | 196 let &cpo = s:cpo_save |
197 unlet s:cpo_save |