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