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 | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 6 | " Latest Revision: 2016-06-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: |
| 10 | " 20160627: - detect heredocs correctly |
| 11 | " 20160213: - detect function definition correctly |
| 12 | " 20160202: - use shiftwidth() function |
| 13 | " 20151215: - set b:undo_indent variable |
| 14 | " 20150728: - add foreach detection for zsh |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | if exists("b:did_indent") |
| 17 | finish |
| 18 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | let b:did_indent = 1 |
| 20 | |
| 21 | setlocal indentexpr=GetShIndent() |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 22 | 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] | 23 | setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | setlocal indentkeys-=:,0# |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 25 | setlocal nosmartindent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 27 | let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' |
| 28 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | if exists("*GetShIndent") |
| 30 | finish |
| 31 | endif |
| 32 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 33 | let s:cpo_save = &cpo |
| 34 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 36 | function s:buffer_shiftwidth() |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 37 | return shiftwidth() |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 38 | endfunction |
| 39 | |
| 40 | let s:sh_indent_defaults = { |
| 41 | \ 'default': function('s:buffer_shiftwidth'), |
| 42 | \ 'continuation-line': function('s:buffer_shiftwidth'), |
| 43 | \ 'case-labels': function('s:buffer_shiftwidth'), |
| 44 | \ 'case-statements': function('s:buffer_shiftwidth'), |
| 45 | \ 'case-breaks': 0 } |
| 46 | |
| 47 | function! s:indent_value(option) |
| 48 | let Value = exists('b:sh_indent_options') |
| 49 | \ && has_key(b:sh_indent_options, a:option) ? |
| 50 | \ b:sh_indent_options[a:option] : |
| 51 | \ s:sh_indent_defaults[a:option] |
| 52 | if type(Value) == type(function('type')) |
| 53 | return Value() |
| 54 | endif |
| 55 | return Value |
| 56 | endfunction |
| 57 | |
| 58 | function! GetShIndent() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | let lnum = prevnonblank(v:lnum - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | if lnum == 0 |
| 61 | return 0 |
| 62 | endif |
| 63 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 64 | let pnum = prevnonblank(lnum - 1) |
| 65 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | let ind = indent(lnum) |
| 67 | let line = getline(lnum) |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 68 | if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' |
| 69 | if line !~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 70 | let ind += s:indent_value('default') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | endif |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 72 | elseif s:is_case_label(line, pnum) |
| 73 | if !s:is_case_ended(line) |
| 74 | let ind += s:indent_value('case-statements') |
| 75 | endif |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 76 | 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] | 77 | if line !~ '}\s*\%(#.*\)\=$' |
| 78 | let ind += s:indent_value('default') |
| 79 | endif |
| 80 | elseif s:is_continuation_line(line) |
| 81 | if pnum == 0 || !s:is_continuation_line(getline(pnum)) |
| 82 | let ind += s:indent_value('continuation-line') |
| 83 | endif |
| 84 | elseif pnum != 0 && s:is_continuation_line(getline(pnum)) |
| 85 | let ind = indent(s:find_continued_lnum(pnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 86 | endif |
| 87 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 88 | let pine = line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | let line = getline(v:lnum) |
Bram Moolenaar | fc39ecf | 2015-08-11 20:34:49 +0200 | [diff] [blame] | 90 | if line =~ '^\s*\%(then\|do\|else\|elif\|fi\|done\|end\)\>' || line =~ '^\s*}' |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 91 | let ind -= s:indent_value('default') |
Bram Moolenaar | dfb1841 | 2013-12-11 18:53:29 +0100 | [diff] [blame] | 92 | elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
| 93 | let ind -= s:indent_value('default') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 94 | elseif line =~ '^\s*esac\>' |
| 95 | let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? |
| 96 | \ 0 : s:indent_value('case-statements')) + |
| 97 | \ s:indent_value('case-labels') |
| 98 | if s:is_case_break(pine) |
| 99 | let ind += s:indent_value('case-breaks') |
| 100 | endif |
| 101 | elseif s:is_case_label(line, lnum) |
| 102 | if s:is_case(pine) |
| 103 | let ind = indent(lnum) + s:indent_value('case-labels') |
| 104 | else |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 105 | let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? |
| 106 | \ 0 : s:indent_value('case-statements')) - |
| 107 | \ s:indent_value('case-breaks') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 108 | endif |
| 109 | elseif s:is_case_break(line) |
| 110 | let ind -= s:indent_value('case-breaks') |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 111 | elseif s:is_here_doc(line) |
| 112 | let ind = 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | endif |
| 114 | |
| 115 | return ind |
| 116 | endfunction |
| 117 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 118 | function! s:is_continuation_line(line) |
| 119 | return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\)$' |
| 120 | endfunction |
| 121 | |
| 122 | function! s:find_continued_lnum(lnum) |
| 123 | let i = a:lnum |
| 124 | while i > 1 && s:is_continuation_line(getline(i - 1)) |
| 125 | let i -= 1 |
| 126 | endwhile |
| 127 | return i |
| 128 | endfunction |
| 129 | |
| 130 | function! s:is_case_label(line, pnum) |
| 131 | if a:line !~ '^\s*(\=.*)' |
| 132 | return 0 |
| 133 | endif |
| 134 | |
| 135 | if a:pnum > 0 |
| 136 | let pine = getline(a:pnum) |
| 137 | if !(s:is_case(pine) || s:is_case_ended(pine)) |
| 138 | return 0 |
| 139 | endif |
| 140 | endif |
| 141 | |
| 142 | let suffix = substitute(a:line, '^\s*(\=', "", "") |
| 143 | let nesting = 0 |
| 144 | let i = 0 |
| 145 | let n = strlen(suffix) |
| 146 | while i < n |
| 147 | let c = suffix[i] |
| 148 | let i += 1 |
| 149 | if c == '\\' |
| 150 | let i += 1 |
| 151 | elseif c == '(' |
| 152 | let nesting += 1 |
| 153 | elseif c == ')' |
| 154 | if nesting == 0 |
| 155 | return 1 |
| 156 | endif |
| 157 | let nesting -= 1 |
| 158 | endif |
| 159 | endwhile |
| 160 | return 0 |
| 161 | endfunction |
| 162 | |
| 163 | function! s:is_case(line) |
| 164 | return a:line =~ '^\s*case\>' |
| 165 | endfunction |
| 166 | |
| 167 | function! s:is_case_break(line) |
| 168 | return a:line =~ '^\s*;[;&]' |
| 169 | endfunction |
| 170 | |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 171 | function! s:is_here_doc(line) |
| 172 | if a:line =~ '^\w\+$' |
| 173 | let here_pat = '<<-\?'. s:escape(a:line). '\$' |
| 174 | return search(here_pat, 'bnW') > 0 |
| 175 | endif |
| 176 | return 0 |
| 177 | endfunction |
| 178 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 179 | function! s:is_case_ended(line) |
| 180 | return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$' |
| 181 | endfunction |
| 182 | |
Bram Moolenaar | dfb1841 | 2013-12-11 18:53:29 +0100 | [diff] [blame] | 183 | function! s:is_case_empty(line) |
| 184 | if a:line =~ '^\s*$' || a:line =~ '^\s*#' |
| 185 | return s:is_case_empty(getline(v:lnum - 1)) |
| 186 | else |
| 187 | return a:line =~ '^\s*case\>' |
| 188 | endif |
| 189 | endfunction |
| 190 | |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 191 | function! s:escape(pattern) |
| 192 | return '\V'. escape(a:pattern, '\\') |
| 193 | endfunction |
| 194 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 195 | let &cpo = s:cpo_save |
| 196 | unlet s:cpo_save |