7
|
1 " Vim indent file
|
375
|
2 " Language: Tcl
|
|
3 " Maintainer: Nikolai Weibull <nikolai+work.vim@bitwi.se>
|
|
4 " Latest Revision: 2005-06-29
|
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)
|
|
34 let pattern = '\\\@<![{}]'
|
|
35 let i = match(line, pattern)
|
|
36 while i != -1
|
|
37 if synIDattr(synID(a:lnum, i + 1, 1), 'name') !~ 'tcl\%(Comment\|String\)'
|
|
38 if line[i] == '{'
|
|
39 let n_open += 1
|
|
40 elseif line[i] == '}'
|
|
41 if n_open > 0
|
|
42 let n_open -= 1
|
|
43 else
|
|
44 let n_close += 1
|
|
45 endif
|
|
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()
|
|
54 let pnum = s:prevnonblanknoncomment(v:lnum - 1)
|
|
55 if pnum == 0
|
7
|
56 return 0
|
|
57 endif
|
|
58
|
375
|
59 let ind = indent(pnum) + s:count_braces(pnum, 1) * &sw
|
|
60 if getline(pnum) =~ '\\$'
|
|
61 let ind += &sw
|
|
62 endif
|
7
|
63
|
375
|
64 let pnum = s:prevnonblanknoncomment(pnum - 1)
|
|
65 if pnum > 0 && getline(pnum) =~ '\\$'
|
|
66 let ind -= &sw
|
|
67 endif
|
7
|
68
|
375
|
69 return ind - s:count_braces(v:lnum, 0) * &sw
|
7
|
70 endfunction
|