blob: 3df6abbf9779e9400339767fd22ec3f6238895cd [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +02002" Language: Shell Script
3" Maintainer: Christian Brabandt <cb@256bit.org>
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +02004" Original Author: Nikolai Weibull <now@bitwi.se>
Bram Moolenaareb3dc872018-05-13 22:34:24 +02005" Previous Maintainer: Peter Aronoff <telemachus@arpinum.org>
Bram Moolenaar54775062019-07-31 21:07:14 +02006" Latest Revision: 2019-07-26
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +02007" License: Vim (see :h license)
8" Repository: https://github.com/chrisbra/vim-sh-indent
Bram Moolenaare18dbe82016-07-02 21:42:23 +02009" Changelog:
Bram Moolenaar54775062019-07-31 21:07:14 +020010" 20190726 - Correctly skip if keywords in syntax comments
11" (issue #17)
12" 20190603 - Do not indent in zsh filetypes with an `if` in comments
Bram Moolenaara6c27c42019-05-09 19:16:22 +020013" 20190428 - De-indent fi correctly when typing with
14" https://github.com/chrisbra/vim-sh-indent/issues/15
Bram Moolenaar62e1bb42019-04-08 16:25:07 +020015" 20190325 - Indent fi; correctly
16" https://github.com/chrisbra/vim-sh-indent/issues/14
17" 20190319 - Indent arrays (only zsh and bash)
18" https://github.com/chrisbra/vim-sh-indent/issues/13
19" 20190316 - Make use of searchpairpos for nested if sections
20" fixes https://github.com/chrisbra/vim-sh-indent/issues/11
21" 20190201 - Better check for closing if sections
Bram Moolenaar91f84f62018-07-29 15:07:52 +020022" 20180724 - make check for zsh syntax more rigid (needs word-boundaries)
Bram Moolenaareb3dc872018-05-13 22:34:24 +020023" 20180326 - better support for line continuation
24" 20180325 - better detection of function definitions
25" 20180127 - better support for zsh complex commands
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +020026" 20170808: - better indent of line continuation
Bram Moolenaarb4d6c3e2017-05-27 16:45:17 +020027" 20170502: - get rid of buffer-shiftwidth function
28" 20160912: - preserve indentation of here-doc blocks
Bram Moolenaare18dbe82016-07-02 21:42:23 +020029" 20160627: - detect heredocs correctly
30" 20160213: - detect function definition correctly
31" 20160202: - use shiftwidth() function
32" 20151215: - set b:undo_indent variable
33" 20150728: - add foreach detection for zsh
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar071d4272004-06-13 20:20:40 +000035if exists("b:did_indent")
36 finish
37endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038let b:did_indent = 1
39
40setlocal indentexpr=GetShIndent()
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +020041setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;&
Bram Moolenaar5c736222010-01-06 20:54:52 +010042setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix
Bram Moolenaar071d4272004-06-13 20:20:40 +000043setlocal indentkeys-=:,0#
Bram Moolenaar5c736222010-01-06 20:54:52 +010044setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000045
Bram Moolenaarf3913272016-02-25 00:00:01 +010046let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<'
47
Bram Moolenaar071d4272004-06-13 20:20:40 +000048if exists("*GetShIndent")
49 finish
50endif
51
Bram Moolenaar42eeac32005-06-29 22:40:58 +000052let s:cpo_save = &cpo
53set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaar5c736222010-01-06 20:54:52 +010055let s:sh_indent_defaults = {
Bram Moolenaarb4d6c3e2017-05-27 16:45:17 +020056 \ 'default': function('shiftwidth'),
57 \ 'continuation-line': function('shiftwidth'),
58 \ 'case-labels': function('shiftwidth'),
59 \ 'case-statements': function('shiftwidth'),
Bram Moolenaar5c736222010-01-06 20:54:52 +010060 \ 'case-breaks': 0 }
61
62function! s:indent_value(option)
63 let Value = exists('b:sh_indent_options')
64 \ && has_key(b:sh_indent_options, a:option) ?
65 \ b:sh_indent_options[a:option] :
66 \ s:sh_indent_defaults[a:option]
67 if type(Value) == type(function('type'))
68 return Value()
69 endif
70 return Value
71endfunction
72
73function! GetShIndent()
Bram Moolenaar62e1bb42019-04-08 16:25:07 +020074 let curline = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 let lnum = prevnonblank(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 if lnum == 0
77 return 0
78 endif
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020079 let line = getline(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar5c736222010-01-06 20:54:52 +010081 let pnum = prevnonblank(lnum - 1)
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020082 let pline = getline(pnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 let ind = indent(lnum)
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020084
85 " Check contents of previous lines
Bram Moolenaar54775062019-07-31 21:07:14 +020086 " should not apply to e.g. commented lines
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020087 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' ||
Bram Moolenaar54775062019-07-31 21:07:14 +020088 \ (&ft is# 'zsh' && line =~ '^\s*\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>')
Bram Moolenaar62e1bb42019-04-08 16:25:07 +020089 if !s:is_end_expression(line)
Bram Moolenaar5c736222010-01-06 20:54:52 +010090 let ind += s:indent_value('default')
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010092 elseif s:is_case_label(line, pnum)
93 if !s:is_case_ended(line)
94 let ind += s:indent_value('case-statements')
95 endif
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020096 " function definition
97 elseif s:is_function_definition(line)
Bram Moolenaar5c736222010-01-06 20:54:52 +010098 if line !~ '}\s*\%(#.*\)\=$'
99 let ind += s:indent_value('default')
100 endif
Bram Moolenaar62e1bb42019-04-08 16:25:07 +0200101 " array (only works for zsh or bash)
102 elseif s:is_array(line) && line !~ ')\s*$' && (&ft is# 'zsh' || s:is_bash())
103 let ind += s:indent_value('continuation-line')
104 " end of array
105 elseif curline =~ '^\s*)$'
106 let ind -= s:indent_value('continuation-line')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100107 elseif s:is_continuation_line(line)
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200108 if pnum == 0 || !s:is_continuation_line(pline)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100109 let ind += s:indent_value('continuation-line')
110 endif
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200111 elseif s:end_block(line) && !s:start_block(line)
112 let ind -= s:indent_value('default')
Bram Moolenaar62e1bb42019-04-08 16:25:07 +0200113 elseif pnum != 0 &&
114 \ s:is_continuation_line(pline) &&
115 \ !s:end_block(curline) &&
116 \ !s:is_end_expression(curline)
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200117 " only add indent, if line and pline is in the same block
118 let i = v:lnum
119 let ind2 = indent(s:find_continued_lnum(pnum))
120 while !s:is_empty(getline(i)) && i > pnum
121 let i -= 1
122 endw
123 if i == pnum
124 let ind += ind2
125 else
126 let ind = ind2
127 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 endif
129
Bram Moolenaar5c736222010-01-06 20:54:52 +0100130 let pine = line
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200131 " Check content of current line
Bram Moolenaar62e1bb42019-04-08 16:25:07 +0200132 let line = curline
133 " Current line is a endif line, so get indent from start of "if condition" line
134 " TODO: should we do the same for other "end" lines?
135 if curline =~ '^\s*\%(fi\);\?\s*\%(#.*\)\=$'
Bram Moolenaar54775062019-07-31 21:07:14 +0200136 let ind = indent(v:lnum)
137 let previous_line = searchpair('\<if\>', '', '\<fi\>\zs', 'bnW', 'synIDattr(synID(line("."),col("."), 1),"name") =~? "comment"')
Bram Moolenaar62e1bb42019-04-08 16:25:07 +0200138 if previous_line > 0
139 let ind = indent(previous_line)
140 endif
141 elseif line =~ '^\s*\%(then\|do\|else\|elif\|done\|end\)\>' || s:end_block(line)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100142 let ind -= s:indent_value('default')
Bram Moolenaardfb18412013-12-11 18:53:29 +0100143 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1))
144 let ind -= s:indent_value('default')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100145 elseif line =~ '^\s*esac\>'
146 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
147 \ 0 : s:indent_value('case-statements')) +
148 \ s:indent_value('case-labels')
149 if s:is_case_break(pine)
150 let ind += s:indent_value('case-breaks')
151 endif
152 elseif s:is_case_label(line, lnum)
153 if s:is_case(pine)
154 let ind = indent(lnum) + s:indent_value('case-labels')
155 else
Bram Moolenaarfb539272014-08-22 19:21:47 +0200156 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
157 \ 0 : s:indent_value('case-statements')) -
158 \ s:indent_value('case-breaks')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100159 endif
160 elseif s:is_case_break(line)
161 let ind -= s:indent_value('case-breaks')
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200162 elseif s:is_here_doc(line)
163 let ind = 0
Bram Moolenaarb4d6c3e2017-05-27 16:45:17 +0200164 " statements, executed within a here document. Keep the current indent
165 elseif match(map(synstack(v:lnum, 1), 'synIDattr(v:val, "name")'), '\c\mheredoc') > -1
166 return indent(v:lnum)
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200167 elseif s:is_comment(line) && s:is_empty(getline(v:lnum-1))
168 return indent(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 endif
170
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200171 return ind > 0 ? ind : 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172endfunction
173
Bram Moolenaar5c736222010-01-06 20:54:52 +0100174function! s:is_continuation_line(line)
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200175 " Comment, cannot be a line continuation
176 if a:line =~ '^\s*#'
177 return 0
178 else
179 " start-of-line
180 " \\ or && or || or |
181 " followed optionally by { or #
182 return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\||\)' .
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +0200183 \ '\s*\({\s*\)\=\(#.*\)\=$'
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200184 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100185endfunction
186
187function! s:find_continued_lnum(lnum)
188 let i = a:lnum
189 while i > 1 && s:is_continuation_line(getline(i - 1))
190 let i -= 1
191 endwhile
192 return i
193endfunction
194
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200195function! s:is_function_definition(line)
196 return a:line =~ '^\s*\<\k\+\>\s*()\s*{' ||
197 \ a:line =~ '^\s*{' ||
198 \ a:line =~ '^\s*function\s*\w\S\+\s*\%(()\)\?\s*{'
199endfunction
200
Bram Moolenaar62e1bb42019-04-08 16:25:07 +0200201function! s:is_array(line)
202 return a:line =~ '^\s*\<\k\+\>=('
203endfunction
204
Bram Moolenaar5c736222010-01-06 20:54:52 +0100205function! s:is_case_label(line, pnum)
206 if a:line !~ '^\s*(\=.*)'
207 return 0
208 endif
209
210 if a:pnum > 0
211 let pine = getline(a:pnum)
212 if !(s:is_case(pine) || s:is_case_ended(pine))
213 return 0
214 endif
215 endif
216
217 let suffix = substitute(a:line, '^\s*(\=', "", "")
218 let nesting = 0
219 let i = 0
220 let n = strlen(suffix)
221 while i < n
222 let c = suffix[i]
223 let i += 1
224 if c == '\\'
225 let i += 1
226 elseif c == '('
227 let nesting += 1
228 elseif c == ')'
229 if nesting == 0
230 return 1
231 endif
232 let nesting -= 1
233 endif
234 endwhile
235 return 0
236endfunction
237
238function! s:is_case(line)
239 return a:line =~ '^\s*case\>'
240endfunction
241
242function! s:is_case_break(line)
243 return a:line =~ '^\s*;[;&]'
244endfunction
245
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200246function! s:is_here_doc(line)
247 if a:line =~ '^\w\+$'
Bram Moolenaar62e1bb42019-04-08 16:25:07 +0200248 let here_pat = '<<-\?'. s:escape(a:line). '\$'
249 return search(here_pat, 'bnW') > 0
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200250 endif
251 return 0
252endfunction
253
Bram Moolenaar5c736222010-01-06 20:54:52 +0100254function! s:is_case_ended(line)
255 return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$'
256endfunction
257
Bram Moolenaardfb18412013-12-11 18:53:29 +0100258function! s:is_case_empty(line)
259 if a:line =~ '^\s*$' || a:line =~ '^\s*#'
260 return s:is_case_empty(getline(v:lnum - 1))
261 else
262 return a:line =~ '^\s*case\>'
263 endif
264endfunction
265
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200266function! s:escape(pattern)
267 return '\V'. escape(a:pattern, '\\')
268endfunction
269
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200270function! s:is_empty(line)
271 return a:line =~ '^\s*$'
272endfunction
273
274function! s:end_block(line)
275 return a:line =~ '^\s*}'
276endfunction
277
278function! s:start_block(line)
279 return a:line =~ '{\s*\(#.*\)\?$'
280endfunction
281
282function! s:find_start_block(lnum)
283 let i = a:lnum
284 while i > 1 && !s:start_block(getline(i))
285 let i -= 1
286 endwhile
287 return i
288endfunction
289
290function! s:is_comment(line)
291 return a:line =~ '^\s*#'
292endfunction
293
Bram Moolenaar62e1bb42019-04-08 16:25:07 +0200294function! s:is_end_expression(line)
295 return a:line =~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$'
296endfunction
297
298function! s:is_bash()
299 return get(g:, 'is_bash', 0) || get(b:, 'is_bash', 0)
300endfunction
301
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000302let &cpo = s:cpo_save
303unlet s:cpo_save