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 | 86ae720 | 2015-07-10 19:31:35 +0200 | [diff] [blame] | 4 | " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org> |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 5 | " Original Author: Nikolai Weibull <now@bitwi.se> |
Bram Moolenaar | b4d6c3e | 2017-05-27 16:45:17 +0200 | [diff] [blame] | 6 | " Latest Revision: 2017-05-02 |
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 | b4d6c3e | 2017-05-27 16:45:17 +0200 | [diff] [blame] | 10 | " 20170502: - get rid of buffer-shiftwidth function |
| 11 | " 20160912: - preserve indentation of here-doc blocks |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 12 | " 20160627: - detect heredocs correctly |
| 13 | " 20160213: - detect function definition correctly |
| 14 | " 20160202: - use shiftwidth() function |
| 15 | " 20151215: - set b:undo_indent variable |
| 16 | " 20150728: - add foreach detection for zsh |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | if exists("b:did_indent") |
| 19 | finish |
| 20 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | let b:did_indent = 1 |
| 22 | |
| 23 | setlocal indentexpr=GetShIndent() |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 24 | 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] | 25 | setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | setlocal indentkeys-=:,0# |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 27 | setlocal nosmartindent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 29 | let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' |
| 30 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 31 | if exists("*GetShIndent") |
| 32 | finish |
| 33 | endif |
| 34 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 35 | let s:cpo_save = &cpo |
| 36 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 38 | let s:sh_indent_defaults = { |
Bram Moolenaar | b4d6c3e | 2017-05-27 16:45:17 +0200 | [diff] [blame] | 39 | \ 'default': function('shiftwidth'), |
| 40 | \ 'continuation-line': function('shiftwidth'), |
| 41 | \ 'case-labels': function('shiftwidth'), |
| 42 | \ 'case-statements': function('shiftwidth'), |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 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() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | let lnum = prevnonblank(v:lnum - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 58 | if lnum == 0 |
| 59 | return 0 |
| 60 | endif |
| 61 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 62 | let pnum = prevnonblank(lnum - 1) |
| 63 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | let ind = indent(lnum) |
| 65 | let line = getline(lnum) |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 66 | if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' |
| 67 | if line !~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 68 | let ind += s:indent_value('default') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | endif |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 70 | elseif s:is_case_label(line, pnum) |
| 71 | if !s:is_case_ended(line) |
| 72 | let ind += s:indent_value('case-statements') |
| 73 | endif |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 74 | elseif line =~ '^\s*\<\k\+\>\s*()\s*{' || line =~ '^\s*{' || line =~ '^\s*function\s*\w\S\+\s*\%(()\)\?\s*{' |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 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)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | endif |
| 85 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 86 | let pine = line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | let line = getline(v:lnum) |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 88 | if line =~ '^\s*\%(then\|do\|else\|elif\|fi\|done\|end\)\>' || line =~ '^\s*}' |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 89 | let ind -= s:indent_value('default') |
Bram Moolenaar | dfb1841 | 2013-12-11 18:53:29 +0100 | [diff] [blame] | 90 | elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
| 91 | let ind -= s:indent_value('default') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 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') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 106 | endif |
| 107 | elseif s:is_case_break(line) |
| 108 | let ind -= s:indent_value('case-breaks') |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 109 | elseif s:is_here_doc(line) |
| 110 | let ind = 0 |
Bram Moolenaar | b4d6c3e | 2017-05-27 16:45:17 +0200 | [diff] [blame] | 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) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | endif |
| 115 | |
| 116 | return ind |
| 117 | endfunction |
| 118 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 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 | |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 172 | function! s:is_here_doc(line) |
| 173 | if a:line =~ '^\w\+$' |
| 174 | let here_pat = '<<-\?'. s:escape(a:line). '\$' |
| 175 | return search(here_pat, 'bnW') > 0 |
| 176 | endif |
| 177 | return 0 |
| 178 | endfunction |
| 179 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 180 | function! s:is_case_ended(line) |
| 181 | return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$' |
| 182 | endfunction |
| 183 | |
Bram Moolenaar | dfb1841 | 2013-12-11 18:53:29 +0100 | [diff] [blame] | 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 | |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 192 | function! s:escape(pattern) |
| 193 | return '\V'. escape(a:pattern, '\\') |
| 194 | endfunction |
| 195 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 196 | let &cpo = s:cpo_save |
| 197 | unlet s:cpo_save |