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