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