1" Vim indent file
2" Language:     ChaiScript
3" Maintainer:	Jason Turner <lefticus 'at' gmail com>
4
5" Only load this indent file when no other was loaded.
6if exists("b:did_indent")
7  finish
8endif
9let b:did_indent = 1
10
11setlocal indentexpr=GetChaiScriptIndent()
12setlocal autoindent
13
14" Only define the function once.
15if exists("*GetChaiScriptIndent")
16  finish
17endif
18
19function! GetChaiScriptIndent()
20  " Find a non-blank line above the current line.
21  let lnum = prevnonblank(v:lnum - 1)
22
23  " Hit the start of the file, use zero indent.
24  if lnum == 0
25    return 0
26  endif
27
28  " Add a 'shiftwidth' after lines that start a block:
29  " lines containing a {
30  let ind = indent(lnum)
31  let flag = 0
32  let prevline = getline(lnum)
33  if prevline =~ '^.*{.*'
34    let ind = ind + &shiftwidth
35    let flag = 1
36  endif
37
38  " Subtract a 'shiftwidth' after lines containing a { followed by a }
39  " to keep it balanced
40  if flag == 1 && prevline =~ '.*{.*}.*'
41    let ind = ind - &shiftwidth
42  endif
43
44  " Subtract a 'shiftwidth' on lines ending with }
45  if getline(v:lnum) =~ '^\s*\%(}\)'
46    let ind = ind - &shiftwidth
47  endif
48
49  return ind
50endfunction
51