Merge branch 'vim-with-runtime' into feat/quickfix-title
[vim_extended.git] / runtime / indent / tcl.vim
bloba92f57d67df23b7d269bd16286f2ecc2e17f61b8
1 " Vim indent file
2 " Language:         Tcl
3 " Maintainer:       Nikolai Weibull <now@bitwi.se>
4 " Latest Revision:  2006-12-20
6 if exists("b:did_indent")
7   finish
8 endif
9 let b:did_indent = 1
11 setlocal indentexpr=GetTclIndent()
12 setlocal indentkeys=0{,0},!^F,o,O,0]
13 setlocal nosmartindent
15 if exists("*GetTclIndent")
16   finish
17 endif
19 function 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
29 endfunction
31 function 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
52 endfunction
54 function 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
62   let pnum = s:prevnonblanknoncomment(v:lnum - 1)
63   if pnum == 0
64     return 0
65   endif
67   let ind = indent(pnum) + s:count_braces(pnum, 1) * &sw
69   let pline = getline(pnum)
70   if pline =~ '}\s*$'
71     let ind -= (s:count_braces(pnum, 0) - (pline =~ '^\s*}' ? 1 : 0)) * &sw
72   endif
74   return ind
75 endfunction