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