codekingpro/portable-devtools
114k
1" Vim indent file2" Language: Tcl3" Maintainer: Chris Heithoff <chrisheithoff@gmail.com>4" Previous Maintainer: Nikolai Weibull <now@bitwi.se>5" Last Change: 24 Sep 20216 7if exists("b:did_indent")8 finish9endif10let b:did_indent = 111 12setlocal indentexpr=GetTclIndent()13setlocal indentkeys=0{,0},!^F,o,O,0]14setlocal nosmartindent15 16let b:undo_indent = "setl inde< indk< si<"17 18if exists("*GetTclIndent")19 finish20endif21 22function s:prevnonblanknoncomment(lnum)23 let lnum = prevnonblank(a:lnum)24 while lnum > 025 let line = getline(lnum)26 if line !~ '^\s*\(#\|$\)'27 break28 endif29 let lnum = prevnonblank(lnum - 1)30 endwhile31 return lnum32endfunction33 34function s:ends_with_backslash(lnum)35 let line = getline(a:lnum)36 if line =~ '\\\s*$'37 return 138 else39 return 040 endif41endfunction 42 43function s:count_braces(lnum, count_open)44 let n_open = 045 let n_close = 046 let line = getline(a:lnum)47 let pattern = '[{}]'48 let i = match(line, pattern)49 while i != -150 if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'tcl\%(Comment\|String\)'51 if line[i] == '{'52 let n_open += 153 elseif line[i] == '}'54 if n_open > 055 let n_open -= 156 else57 let n_close += 158 endif59 endif60 endif61 let i = match(line, pattern, i + 1)62 endwhile63 return a:count_open ? n_open : n_close64endfunction65 66function GetTclIndent()67 let line = getline(v:lnum)68 69 " Get the line number of the previous non-blank or non-comment line.70 let pnum = s:prevnonblanknoncomment(v:lnum - 1)71 if pnum == 072 return 073 endif74 75 " ..and the previous line before the previous line.76 let pnum2 = s:prevnonblanknoncomment(pnum-1)77 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 indentation82 if s:count_braces(pnum, 1) > 083 let ind += shiftwidth()84 else85 " 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 && !slash289 " If the previous line begins a line continuation.90 let ind += shiftwidth()91 elseif !slash1 && slash292 " If two lines ago was the end of a line continuation group of lines.93 let ind -= shiftwidth()94 endif95 endif96 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 endif101 102 return ind103endfunction104 