Vim 7.2 released!
[MacVim.git] / runtime / indent / bst.vim
blob3d4b8932293c5b191e08b29051a84cf863480593
1 " Vim indent file
2 " Language:     bst
3 " Author:       Tim Pope <vimNOSPAM@tpope.info>
4 " $Id: bst.vim,v 1.9 2008/08/09 17:52:31 vimboss Exp $
6 if exists("b:did_indent")
7     finish
8 endif
9 let b:did_indent = 1
11 setlocal expandtab
12 setlocal indentexpr=GetBstIndent(v:lnum)
13 "setlocal smartindent
14 setlocal cinkeys&
15 setlocal cinkeys-=0#
16 setlocal indentkeys&
17 "setlocal indentkeys+=0%
19 " Only define the function once.
20 if exists("*GetBstIndent")
21     finish
22 endif
24 function! s:prevgood(lnum)
25     " Find a non-blank line above the current line.
26     " Skip over comments.
27     let lnum = a:lnum
28     while lnum > 0
29         let lnum = prevnonblank(lnum - 1)
30         if getline(lnum) !~ '^\s*%.*$'
31             break
32         endif
33     endwhile
34     return lnum
35 endfunction
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\+','','')
42     return line
43 endfunction
45 function! s:count(string,char)
46     let str = substitute(a:string,'[^'.a:char.']','','g')
47     return strlen(str)
48 endfunction
50 function! GetBstIndent(lnum) abort
51     if a:lnum == 1
52         return 0
53     endif
54     let lnum = s:prevgood(a:lnum)
55     if lnum <= 0
56         return indent(a:lnum - 1)
57     endif
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('$')
64                 return indent('.')
65             else
66                 return virtcol('.')-1
67             endif
68         endif
69     endif
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,'}')
74     return ind
75 endfunction