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