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 | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 4 | " Last Change: 2022 Mar 15 |
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 | |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 11 | setlocal indentexpr=GetBstIndent(v:lnum) |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 12 | setlocal cinkeys& |
| 13 | setlocal cinkeys-=0# |
| 14 | setlocal indentkeys& |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 15 | let b:undo_indent = 'setlocal indentexpr< cinkeys< indentkeys<' |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 16 | |
| 17 | " Only define the function once. |
| 18 | if exists("*GetBstIndent") |
| 19 | finish |
| 20 | endif |
| 21 | |
| 22 | function! s:prevgood(lnum) |
| 23 | " Find a non-blank line above the current line. |
| 24 | " Skip over comments. |
| 25 | let lnum = a:lnum |
| 26 | while lnum > 0 |
| 27 | let lnum = prevnonblank(lnum - 1) |
| 28 | if getline(lnum) !~ '^\s*%.*$' |
| 29 | break |
| 30 | endif |
| 31 | endwhile |
| 32 | return lnum |
| 33 | endfunction |
| 34 | |
| 35 | function! s:strip(lnum) |
| 36 | let line = getline(a:lnum) |
| 37 | let line = substitute(line,'"[^"]*"','""','g') |
| 38 | let line = substitute(line,'%.*','','') |
| 39 | let line = substitute(line,'^\s\+','','') |
| 40 | return line |
| 41 | endfunction |
| 42 | |
| 43 | function! s:count(string,char) |
| 44 | let str = substitute(a:string,'[^'.a:char.']','','g') |
| 45 | return strlen(str) |
| 46 | endfunction |
| 47 | |
| 48 | function! GetBstIndent(lnum) abort |
| 49 | if a:lnum == 1 |
| 50 | return 0 |
| 51 | endif |
| 52 | let lnum = s:prevgood(a:lnum) |
| 53 | if lnum <= 0 |
| 54 | return indent(a:lnum - 1) |
| 55 | endif |
| 56 | let line = s:strip(lnum) |
| 57 | let cline = s:strip(a:lnum) |
| 58 | if cline =~ '^}' && exists("b:current_syntax") |
| 59 | call cursor(a:lnum,indent(a:lnum)) |
| 60 | if searchpair('{','','}','bW',"synIDattr(synID(line('.'),col('.'),1),'name') =~? 'comment\\|string'") |
| 61 | if col('.')+1 == col('$') |
| 62 | return indent('.') |
| 63 | else |
| 64 | return virtcol('.')-1 |
| 65 | endif |
| 66 | endif |
| 67 | endif |
| 68 | let fakeline = substitute(line,'^}','','').matchstr(cline,'^}') |
| 69 | let ind = indent(lnum) |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 70 | let ind = ind + shiftwidth() * s:count(line,'{') |
| 71 | let ind = ind - shiftwidth() * s:count(fakeline,'}') |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 72 | return ind |
| 73 | endfunction |