Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: bst |
| 3 | " Author: Tim Pope <vimNOSPAM@tpope.info> |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 4 | " $Id: bst.vim,v 1.1 2007/05/05 18:11:12 vimboss Exp $ |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 5 | |
| 6 | if exists("b:did_indent") |
| 7 | finish |
| 8 | endif |
| 9 | let b:did_indent = 1 |
| 10 | |
| 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% |
| 18 | |
| 19 | " Only define the function once. |
| 20 | if exists("*GetBstIndent") |
| 21 | finish |
| 22 | endif |
| 23 | |
| 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 |
| 36 | |
| 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 |
| 44 | |
| 45 | function! s:count(string,char) |
| 46 | let str = substitute(a:string,'[^'.a:char.']','','g') |
| 47 | return strlen(str) |
| 48 | endfunction |
| 49 | |
| 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 |