1" Vim indent file
2" Language:	    Tcl
3" Maintainer:	    Nikolai Weibull <now@bitwi.se>
4" Latest Revision:  2006-12-20
5
6if exists("b:did_indent")
7  finish
8endif
9let b:did_indent = 1
10
11setlocal indentexpr=GetTclIndent()
12setlocal indentkeys=0{,0},!^F,o,O,0]
13setlocal nosmartindent
14
15if exists("*GetTclIndent")
16  finish
17endif
18
19function s:prevnonblanknoncomment(lnum)
20  let lnum = prevnonblank(a:lnum)
21  while lnum > 0
22    let line = getline(lnum)
23    if line !~ '^\s*\(#\|$\)'
24      break
25    endif
26    let lnum = prevnonblank(lnum - 1)
27  endwhile
28  return lnum
29endfunction
30
31function s:count_braces(lnum, count_open)
32  let n_open = 0
33  let n_close = 0
34  let line = getline(a:lnum)
35  let pattern = '[{}]'
36  let i = match(line, pattern)
37  while i != -1
38    if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'tcl\%(Comment\|String\)'
39      if line[i] == '{'
40        let n_open += 1
41      elseif line[i] == '}'
42        if n_open > 0
43          let n_open -= 1
44        else
45          let n_close += 1
46        endif
47      endif
48    endif
49    let i = match(line, pattern, i + 1)
50  endwhile
51  return a:count_open ? n_open : n_close
52endfunction
53
54function GetTclIndent()
55  let line = getline(v:lnum)
56  if line =~ '^\s*\*'
57    return cindent(v:lnum)
58  elseif line =~ '^\s*}'
59    return indent(v:lnum) - &sw
60  endif
61
62  let pnum = s:prevnonblanknoncomment(v:lnum - 1)
63  if pnum == 0
64    return 0
65  endif
66
67  let ind = indent(pnum) + s:count_braces(pnum, 1) * &sw
68
69  let pline = getline(pnum)
70  if pline =~ '}\s*$'
71    let ind -= (s:count_braces(pnum, 0) - (pline =~ '^\s*}' ? 1 : 0)) * &sw
72  endif
73
74  return ind
75endfunction
76