blob: c35150d0e04d8dd8829a6e2b08a7e14e60715ed6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar6e649222021-10-04 21:32:54 +01002" Language: Tcl
3" Maintainer: Chris Heithoff <chrisheithoff@gmail.com>
Bram Moolenaar30e9b3c2019-09-07 16:24:12 +02004" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
Bram Moolenaar6e649222021-10-04 21:32:54 +01005" Last Change: 24 Sep 2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007if exists("b:did_indent")
8 finish
9endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010let b:did_indent = 1
11
12setlocal indentexpr=GetTclIndent()
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013setlocal indentkeys=0{,0},!^F,o,O,0]
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000014setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
Bram Moolenaar6e649222021-10-04 21:32:54 +010016let b:undo_indent = "setl inde< indk< si<"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018if exists("*GetTclIndent")
19 finish
20endif
21
Bram Moolenaar42eeac32005-06-29 22:40:58 +000022function s:prevnonblanknoncomment(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023 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
32endfunction
33
Bram Moolenaard47d5222018-12-09 20:43:55 +010034function 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
41endfunction
42
Bram Moolenaar42eeac32005-06-29 22:40:58 +000043function s:count_braces(lnum, count_open)
44 let n_open = 0
45 let n_close = 0
46 let line = getline(a:lnum)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000047 let pattern = '[{}]'
Bram Moolenaar42eeac32005-06-29 22:40:58 +000048 let i = match(line, pattern)
49 while i != -1
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000050 if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'tcl\%(Comment\|String\)'
Bram Moolenaar42eeac32005-06-29 22:40:58 +000051 if line[i] == '{'
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000052 let n_open += 1
Bram Moolenaar42eeac32005-06-29 22:40:58 +000053 elseif line[i] == '}'
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000054 if n_open > 0
55 let n_open -= 1
56 else
57 let n_close += 1
58 endif
Bram Moolenaar42eeac32005-06-29 22:40:58 +000059 endif
60 endif
61 let i = match(line, pattern, i + 1)
62 endwhile
63 return a:count_open ? n_open : n_close
64endfunction
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaar42eeac32005-06-29 22:40:58 +000066function GetTclIndent()
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000067 let line = getline(v:lnum)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000068
Bram Moolenaard47d5222018-12-09 20:43:55 +010069 " Get the line number of the previous non-blank or non-comment line.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000070 let pnum = s:prevnonblanknoncomment(v:lnum - 1)
71 if pnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 return 0
73 endif
74
Bram Moolenaard47d5222018-12-09 20:43:55 +010075 " ..and the previous line before the previous line.
76 let pnum2 = s:prevnonblanknoncomment(pnum-1)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000077
Bram Moolenaard47d5222018-12-09 20:43:55 +010078 " 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
Bram Moolenaar42eeac32005-06-29 22:40:58 +000095 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaard47d5222018-12-09 20:43:55 +010097 " 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
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000102 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103endfunction