blob: b6ce774595b1974877cac329521d6cac1b5a6493 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002" Language: Shell Script
3" Maintainer: Nikolai Weibull <nikolai+work.vim@bitwi.se>
4" Latest Revision: 2005-06-29
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006if exists("b:did_indent")
7 finish
8endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009let b:did_indent = 1
10
11setlocal indentexpr=GetShIndent()
12setlocal indentkeys+==then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done
13setlocal indentkeys-=:,0#
14
Bram Moolenaar071d4272004-06-13 20:20:40 +000015if exists("*GetShIndent")
16 finish
17endif
18
Bram Moolenaar42eeac32005-06-29 22:40:58 +000019let s:cpo_save = &cpo
20set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22function GetShIndent()
Bram Moolenaar071d4272004-06-13 20:20:40 +000023 let lnum = prevnonblank(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 if lnum == 0
25 return 0
26 endif
27
28 " Add a 'shiftwidth' after if, while, else, case, until, for, function()
29 " Skip if the line also contains the closure for the above
30 let ind = indent(lnum)
31 let line = getline(lnum)
32 if line =~ '^\s*\(if\|then\|do\|else\|elif\|case\|while\|until\|for\)\>'
Bram Moolenaar42eeac32005-06-29 22:40:58 +000033 \ || line =~ '^\s*\<\k\+\>\s*()\s*{'
34 \ || line =~ '^\s*{'
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 if line !~ '\(esac\|fi\|done\)\>\s*$' && line !~ '}\s*$'
36 let ind = ind + &sw
37 endif
38 endif
39
40 " Subtract a 'shiftwidth' on a then, do, else, esac, fi, done
41 " Retain the indentation level if line matches fin (for find)
42 let line = getline(v:lnum)
43 if (line =~ '^\s*\(then\|do\|else\|elif\|esac\|fi\|done\)\>' || line =~ '^\s*}')
Bram Moolenaar42eeac32005-06-29 22:40:58 +000044 \ && line !~ '^\s*fi[ln]\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 let ind = ind - &sw
46 endif
47
48 return ind
49endfunction
50
Bram Moolenaar42eeac32005-06-29 22:40:58 +000051let &cpo = s:cpo_save
52unlet s:cpo_save