3 " Author: Tim Pope <vimNOSPAM@tpope.info>
4 " $Id: bst.vim,v 1.4 2007/05/12 15:03:23 vimboss Exp $
6 if exists("b:did_indent")
12 setlocal indentexpr=GetBstIndent(v:lnum)
17 "setlocal indentkeys+=0%
19 " Only define the function once.
20 if exists("*GetBstIndent")
24 function! s:prevgood(lnum)
25 " Find a non-blank line above the current line.
29 let lnum = prevnonblank(lnum - 1)
30 if getline(lnum) !~ '^\s*%.*$'
37 function! s:strip(lnum)
38 let line = getline(a:lnum)
39 let line = substitute(line,'"[^"]*"','""','g')
40 let line = substitute(line,'%.*','','')
41 let line = substitute(line,'^\s\+','','')
45 function! s:count(string,char)
46 let str = substitute(a:string,'[^'.a:char.']','','g')
50 function! GetBstIndent(lnum) abort
54 let lnum = s:prevgood(a:lnum)
56 return indent(a:lnum - 1)
58 let line = s:strip(lnum)
59 let cline = s:strip(a:lnum)
60 if cline =~ '^}' && exists("b:current_syntax")
61 call cursor(a:lnum,indent(a:lnum))
62 if searchpair('{','','}','bW',"synIDattr(synID(line('.'),col('.'),1),'name') =~? 'comment\\|string'")
63 if col('.')+1 == col('$')
70 let fakeline = substitute(line,'^}','','').matchstr(cline,'^}')
71 let ind = indent(lnum)
72 let ind = ind + &sw * s:count(line,'{')
73 let ind = ind - &sw * s:count(fakeline,'}')