robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
75157d2572
master
1" Vim indent file 2" Language: bst 3" Author: Tim Pope <vimNOSPAM@tpope.info> 4" Last Change: 2022 Mar 15 5 6if exists ( "b:did_indent" ) 7finish 8endif 9let b: did_indent = 1 10 11setlocal indentexpr = GetBstIndent (v:lnum) 12setlocal cinkeys & 13setlocal cinkeys -= 0# 14setlocal indentkeys & 15let b: undo_indent = 'setlocal indentexpr< cinkeys< indentkeys<' 16 17" Only define the function once. 18if exists ( "*GetBstIndent" ) 19finish 20endif 21 22function ! s: prevgood ( lnum ) 23" Find a non-blank line above the current line. 24" Skip over comments. 25let lnum = a: lnum 26while lnum > 0 27let lnum = prevnonblank ( lnum - 1 ) 28if getline ( lnum ) !~ '^\s*%.*$' 29break 30endif 31endwhile 32return lnum 33endfunction 34 35function ! s: strip ( lnum ) 36let line = getline ( a: lnum ) 37let line = substitute ( line , '"[^"]*"' , '""' , 'g' ) 38let line = substitute ( line , '%.*' , '' , '' ) 39let line = substitute ( line , '^\s\+' , '' , '' ) 40return line 41endfunction 42 43function ! s: count ( string , char ) 44let str = substitute ( a: string , '[^' . a: char . ']' , '' , 'g' ) 45return strlen ( str ) 46endfunction 47 48function ! GetBstIndent ( lnum ) abort 49if a: lnum == 1 50return 0 51endif 52let lnum = s: prevgood ( a: lnum ) 53if lnum <= 0 54return indent ( a: lnum - 1 ) 55endif 56let line = s: strip ( lnum ) 57let cline = s: strip ( a: lnum ) 58if cline =~ '^}' && exists ( "b:current_syntax" ) 59call cursor ( a: lnum , indent ( a: lnum )) 60if searchpair ( '{' , '' , '}' , 'bW' , "synIDattr(synID(line('.'),col('.'),1),'name') =~? 'comment\\|string'" ) 61if col ( '.' ) + 1 == col ( '$' ) 62return indent ( '.' ) 63else 64return virtcol ( '.' ) - 1 65endif 66endif 67endif 68let fakeline = substitute ( line , '^}' , '' , '' ). matchstr ( cline , '^}' ) 69let ind = indent ( lnum ) 70let ind = ind + shiftwidth () * s: count ( line , '{' ) 71let ind = ind - shiftwidth () * s: count ( fakeline , '}' ) 72return ind 73endfunction