blob: 47e3058810390797615e9678e4707adfa140efa6 [file] [log] [blame]
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001" Vim indent file
2" Language: bst
3" Author: Tim Pope <vimNOSPAM@tpope.info>
Bram Moolenaar5c736222010-01-06 20:54:52 +01004" $Id: bst.vim,v 1.1 2007/05/05 18:11:12 vimboss Exp $
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00005
6if exists("b:did_indent")
7 finish
8endif
9let b:did_indent = 1
10
11setlocal expandtab
12setlocal indentexpr=GetBstIndent(v:lnum)
13"setlocal smartindent
14setlocal cinkeys&
15setlocal cinkeys-=0#
16setlocal indentkeys&
17"setlocal indentkeys+=0%
18
19" Only define the function once.
20if exists("*GetBstIndent")
21 finish
22endif
23
24function! 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
35endfunction
36
37function! 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
43endfunction
44
45function! s:count(string,char)
46 let str = substitute(a:string,'[^'.a:char.']','','g')
47 return strlen(str)
48endfunction
49
50function! 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)
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020072 let ind = ind + shiftwidth() * s:count(line,'{')
73 let ind = ind - shiftwidth() * s:count(fakeline,'}')
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000074 return ind
75endfunction