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