blob: 880ad12ce76c7f9dae6f0e2de809d071c4521987 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Shell Script
3" Maintainer: Nikolai Weibull <source@pcppopper.org>
4" URL: http://www.pcppopper.org/vim/indent/pcp/sh/
5" Latest Revision: 2004-04-25
6" arch-tag: 431c7fc1-12a6-4d71-9636-1498ef56b038
7
8" Only load this indent file when no other was loaded.
9if exists("b:did_indent")
10 finish
11endif
12
13let b:did_indent = 1
14
15setlocal indentexpr=GetShIndent()
16setlocal indentkeys+==then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done
17setlocal indentkeys-=:,0#
18
19" Only define the function once.
20if exists("*GetShIndent")
21 finish
22endif
23
24set cpoptions-=C
25
26function GetShIndent()
27 " Find a non-blank line above the current line.
28 let lnum = prevnonblank(v:lnum - 1)
29
30 " Hit the start of the file, use zero indent.
31 if lnum == 0
32 return 0
33 endif
34
35 " Add a 'shiftwidth' after if, while, else, case, until, for, function()
36 " Skip if the line also contains the closure for the above
37 let ind = indent(lnum)
38 let line = getline(lnum)
39 if line =~ '^\s*\(if\|then\|do\|else\|elif\|case\|while\|until\|for\)\>'
40 \ || line =~ '^\s*\<\h\w*\>\s*()\s*{'
41 \ || line =~ '^\s*{'
42 if line !~ '\(esac\|fi\|done\)\>\s*$' && line !~ '}\s*$'
43 let ind = ind + &sw
44 endif
45 endif
46
47 " Subtract a 'shiftwidth' on a then, do, else, esac, fi, done
48 " Retain the indentation level if line matches fin (for find)
49 let line = getline(v:lnum)
50 if (line =~ '^\s*\(then\|do\|else\|elif\|esac\|fi\|done\)\>' || line =~ '^\s*}')
51 \ && line !~ '^\s*fi[ln]\>'
52 let ind = ind - &sw
53 endif
54
55 return ind
56endfunction
57
58" vim: set sts=2 sw=2: