7
|
1 " Vim indent file
|
25880
|
2 " Language: Tcl
|
|
3 " Maintainer: Chris Heithoff <chrisheithoff@gmail.com>
|
15131
|
4 " Previous Maintainer: Nikolai Weibull <now@bitwi.se>
|
25880
|
5 " Last Change: 24 Sep 2021
|
7
|
6
|
|
7 if exists("b:did_indent")
|
|
8 finish
|
|
9 endif
|
|
10 let b:did_indent = 1
|
|
11
|
|
12 setlocal indentexpr=GetTclIndent()
|
375
|
13 setlocal indentkeys=0{,0},!^F,o,O,0]
|
1213
|
14 setlocal nosmartindent
|
7
|
15
|
25880
|
16 let b:undo_indent = "setl inde< indk< si<"
|
|
17
|
7
|
18 if exists("*GetTclIndent")
|
|
19 finish
|
|
20 endif
|
|
21
|
375
|
22 function s:prevnonblanknoncomment(lnum)
|
7
|
23 let lnum = prevnonblank(a:lnum)
|
|
24 while lnum > 0
|
|
25 let line = getline(lnum)
|
|
26 if line !~ '^\s*\(#\|$\)'
|
|
27 break
|
|
28 endif
|
|
29 let lnum = prevnonblank(lnum - 1)
|
|
30 endwhile
|
|
31 return lnum
|
|
32 endfunction
|
|
33
|
15131
|
34 function s:ends_with_backslash(lnum)
|
|
35 let line = getline(a:lnum)
|
|
36 if line =~ '\\\s*$'
|
|
37 return 1
|
|
38 else
|
|
39 return 0
|
|
40 endif
|
|
41 endfunction
|
|
42
|
375
|
43 function s:count_braces(lnum, count_open)
|
|
44 let n_open = 0
|
|
45 let n_close = 0
|
|
46 let line = getline(a:lnum)
|
389
|
47 let pattern = '[{}]'
|
375
|
48 let i = match(line, pattern)
|
|
49 while i != -1
|
389
|
50 if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'tcl\%(Comment\|String\)'
|
375
|
51 if line[i] == '{'
|
1213
|
52 let n_open += 1
|
375
|
53 elseif line[i] == '}'
|
1213
|
54 if n_open > 0
|
|
55 let n_open -= 1
|
|
56 else
|
|
57 let n_close += 1
|
|
58 endif
|
375
|
59 endif
|
|
60 endif
|
|
61 let i = match(line, pattern, i + 1)
|
|
62 endwhile
|
|
63 return a:count_open ? n_open : n_close
|
|
64 endfunction
|
7
|
65
|
375
|
66 function GetTclIndent()
|
389
|
67 let line = getline(v:lnum)
|
|
68
|
15131
|
69 " Get the line number of the previous non-blank or non-comment line.
|
375
|
70 let pnum = s:prevnonblanknoncomment(v:lnum - 1)
|
|
71 if pnum == 0
|
7
|
72 return 0
|
|
73 endif
|
|
74
|
15131
|
75 " ..and the previous line before the previous line.
|
|
76 let pnum2 = s:prevnonblanknoncomment(pnum-1)
|
389
|
77
|
15131
|
78 " Default indentation is to preserve the previous indentation.
|
|
79 let ind = indent(pnum)
|
|
80
|
|
81 " ...but if previous line introduces an open brace, then increase current line's indentation
|
|
82 if s:count_braces(pnum, 1) > 0
|
|
83 let ind += shiftwidth()
|
|
84 else
|
|
85 " Look for backslash line continuation on the previous two lines.
|
|
86 let slash1 = s:ends_with_backslash(pnum)
|
|
87 let slash2 = s:ends_with_backslash(pnum2)
|
|
88 if slash1 && !slash2
|
|
89 " If the previous line begins a line continuation.
|
|
90 let ind += shiftwidth()
|
|
91 elseif !slash1 && slash2
|
|
92 " If two lines ago was the end of a line continuation group of lines.
|
|
93 let ind -= shiftwidth()
|
|
94 endif
|
375
|
95 endif
|
7
|
96
|
15131
|
97 " If the current line begins with a closed brace, then decrease the indentation by one.
|
|
98 if line =~ '^\s*}'
|
|
99 let ind -= shiftwidth()
|
|
100 endif
|
|
101
|
389
|
102 return ind
|
7
|
103 endfunction
|