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 | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 6 | " Latest Revision: 2019-07-26 |
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 | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 10 | " 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 Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 13 | " 20190428 - De-indent fi correctly when typing with |
| 14 | " https://github.com/chrisbra/vim-sh-indent/issues/15 |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 15 | " 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 Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 22 | " 20180724 - make check for zsh syntax more rigid (needs word-boundaries) |
Bram Moolenaar | eb3dc87 | 2018-05-13 22:34:24 +0200 | [diff] [blame] | 23 | " 20180326 - better support for line continuation |
| 24 | " 20180325 - better detection of function definitions |
| 25 | " 20180127 - better support for zsh complex commands |
Bram Moolenaar | 1ccd8ff | 2017-08-11 19:50:37 +0200 | [diff] [blame] | 26 | " 20170808: - better indent of line continuation |
Bram Moolenaar | b4d6c3e | 2017-05-27 16:45:17 +0200 | [diff] [blame] | 27 | " 20170502: - get rid of buffer-shiftwidth function |
| 28 | " 20160912: - preserve indentation of here-doc blocks |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 29 | " 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | if exists("b:did_indent") |
| 36 | finish |
| 37 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | let b:did_indent = 1 |
| 39 | |
| 40 | setlocal indentexpr=GetShIndent() |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 41 | 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] | 42 | setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | setlocal indentkeys-=:,0# |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 44 | setlocal nosmartindent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 46 | let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' |
| 47 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | if exists("*GetShIndent") |
| 49 | finish |
| 50 | endif |
| 51 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 52 | let s:cpo_save = &cpo |
| 53 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 54 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 55 | let s:sh_indent_defaults = { |
Bram Moolenaar | b4d6c3e | 2017-05-27 16:45:17 +0200 | [diff] [blame] | 56 | \ 'default': function('shiftwidth'), |
| 57 | \ 'continuation-line': function('shiftwidth'), |
| 58 | \ 'case-labels': function('shiftwidth'), |
| 59 | \ 'case-statements': function('shiftwidth'), |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 60 | \ 'case-breaks': 0 } |
| 61 | |
| 62 | function! 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 |
| 71 | endfunction |
| 72 | |
| 73 | function! GetShIndent() |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 74 | let curline = getline(v:lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | let lnum = prevnonblank(v:lnum - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 76 | if lnum == 0 |
| 77 | return 0 |
| 78 | endif |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 79 | let line = getline(lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 81 | let pnum = prevnonblank(lnum - 1) |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 82 | let pline = getline(pnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | let ind = indent(lnum) |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 84 | |
| 85 | " Check contents of previous lines |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 86 | " should not apply to e.g. commented lines |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 87 | if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' || |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 88 | \ (&ft is# 'zsh' && line =~ '^\s*\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>') |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 89 | if !s:is_end_expression(line) |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 90 | let ind += s:indent_value('default') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | endif |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 92 | 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 Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 96 | " function definition |
| 97 | elseif s:is_function_definition(line) |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 98 | if line !~ '}\s*\%(#.*\)\=$' |
| 99 | let ind += s:indent_value('default') |
| 100 | endif |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 101 | " 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 107 | elseif s:is_continuation_line(line) |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 108 | if pnum == 0 || !s:is_continuation_line(pline) |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 109 | let ind += s:indent_value('continuation-line') |
| 110 | endif |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 111 | elseif s:end_block(line) && !s:start_block(line) |
| 112 | let ind -= s:indent_value('default') |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 113 | elseif pnum != 0 && |
| 114 | \ s:is_continuation_line(pline) && |
| 115 | \ !s:end_block(curline) && |
| 116 | \ !s:is_end_expression(curline) |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 117 | " 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | endif |
| 129 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 130 | let pine = line |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 131 | " Check content of current line |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 132 | 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 Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 136 | let ind = indent(v:lnum) |
| 137 | let previous_line = searchpair('\<if\>', '', '\<fi\>\zs', 'bnW', 'synIDattr(synID(line("."),col("."), 1),"name") =~? "comment"') |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 138 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 142 | let ind -= s:indent_value('default') |
Bram Moolenaar | dfb1841 | 2013-12-11 18:53:29 +0100 | [diff] [blame] | 143 | elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
| 144 | let ind -= s:indent_value('default') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 145 | 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 Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 156 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 159 | endif |
| 160 | elseif s:is_case_break(line) |
| 161 | let ind -= s:indent_value('case-breaks') |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 162 | elseif s:is_here_doc(line) |
| 163 | let ind = 0 |
Bram Moolenaar | b4d6c3e | 2017-05-27 16:45:17 +0200 | [diff] [blame] | 164 | " 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 Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 167 | elseif s:is_comment(line) && s:is_empty(getline(v:lnum-1)) |
| 168 | return indent(v:lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 169 | endif |
| 170 | |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 171 | return ind > 0 ? ind : 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | endfunction |
| 173 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 174 | function! s:is_continuation_line(line) |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 175 | " 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 Moolenaar | 1ccd8ff | 2017-08-11 19:50:37 +0200 | [diff] [blame] | 183 | \ '\s*\({\s*\)\=\(#.*\)\=$' |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 184 | endif |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 185 | endfunction |
| 186 | |
| 187 | function! 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 |
| 193 | endfunction |
| 194 | |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 195 | function! 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*{' |
| 199 | endfunction |
| 200 | |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 201 | function! s:is_array(line) |
| 202 | return a:line =~ '^\s*\<\k\+\>=(' |
| 203 | endfunction |
| 204 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 205 | function! 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 |
| 236 | endfunction |
| 237 | |
| 238 | function! s:is_case(line) |
| 239 | return a:line =~ '^\s*case\>' |
| 240 | endfunction |
| 241 | |
| 242 | function! s:is_case_break(line) |
| 243 | return a:line =~ '^\s*;[;&]' |
| 244 | endfunction |
| 245 | |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 246 | function! s:is_here_doc(line) |
| 247 | if a:line =~ '^\w\+$' |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 248 | let here_pat = '<<-\?'. s:escape(a:line). '\$' |
| 249 | return search(here_pat, 'bnW') > 0 |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 250 | endif |
| 251 | return 0 |
| 252 | endfunction |
| 253 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 254 | function! s:is_case_ended(line) |
| 255 | return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$' |
| 256 | endfunction |
| 257 | |
Bram Moolenaar | dfb1841 | 2013-12-11 18:53:29 +0100 | [diff] [blame] | 258 | function! 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 |
| 264 | endfunction |
| 265 | |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 266 | function! s:escape(pattern) |
| 267 | return '\V'. escape(a:pattern, '\\') |
| 268 | endfunction |
| 269 | |
Bram Moolenaar | 7db25fe | 2018-05-13 00:02:36 +0200 | [diff] [blame] | 270 | function! s:is_empty(line) |
| 271 | return a:line =~ '^\s*$' |
| 272 | endfunction |
| 273 | |
| 274 | function! s:end_block(line) |
| 275 | return a:line =~ '^\s*}' |
| 276 | endfunction |
| 277 | |
| 278 | function! s:start_block(line) |
| 279 | return a:line =~ '{\s*\(#.*\)\?$' |
| 280 | endfunction |
| 281 | |
| 282 | function! 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 |
| 288 | endfunction |
| 289 | |
| 290 | function! s:is_comment(line) |
| 291 | return a:line =~ '^\s*#' |
| 292 | endfunction |
| 293 | |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 294 | function! s:is_end_expression(line) |
| 295 | return a:line =~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' |
| 296 | endfunction |
| 297 | |
| 298 | function! s:is_bash() |
| 299 | return get(g:, 'is_bash', 0) || get(b:, 'is_bash', 0) |
| 300 | endfunction |
| 301 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 302 | let &cpo = s:cpo_save |
| 303 | unlet s:cpo_save |