robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
1" Vim indent file 2" Language: PoV-Ray Scene Description Language 3" Maintainer: David Necas (Yeti) <yeti@physics.muni.cz> 4" Last Change: 2017 Jun 13 5" 2022 April: b:undo_indent added by Doug Kearns 6" URI: http://trific.ath.cx/Ftp/vim/indent/pov.vim 7 8" Only load this indent file when no other was loaded. 9if exists ( "b:did_indent" ) 10finish 11endif 12let b: did_indent = 1 13 14" Some preliminary settings. 15setlocal nolisp " Make sure lisp indenting doesn't supersede us. 16 17setlocal indentexpr = GetPoVRayIndent () 18setlocal indentkeys += =else,=end,0] 19 20let b: undo_indent = "setl inde< indk< lisp<" 21 22" Only define the function once. 23if exists ( "*GetPoVRayIndent" ) 24finish 25endif 26 27" Counts matches of a regexp <rexp> in line number <line>. 28" Doesn't count matches inside strings and comments (as defined by current 29" syntax). 30function ! s: MatchCount ( line , rexp ) 31let str = getline ( a: line ) 32let i = 0 33let n = 0 34while i >= 0 35let i = matchend ( str , a: rexp , i ) 36if i >= 0 && synIDattr ( synID ( a: line , i , 0 ), "name" ) !~? "string\|comment" 37let n = n + 1 38endif 39endwhile 40return n 41endfunction 42 43" The main function. Returns indent amount. 44function GetPoVRayIndent () 45" If we are inside a comment (may be nested in obscure ways), give up 46if synIDattr ( synID ( v: lnum , indent ( v: lnum ) + 1 , 0 ), "name" ) =~? "string\|comment" 47return -1 48endif 49 50" Search backwards for the first non-empty, non-comment line. 51let plnum = prevnonblank ( v: lnum - 1 ) 52let plind = indent ( plnum ) 53while plnum > 0 && synIDattr ( synID ( plnum , plind + 1 , 0 ), "name" ) =~? "comment" 54let plnum = prevnonblank ( plnum - 1 ) 55let plind = indent ( plnum ) 56endwhile 57 58" Start indenting from zero 59if plnum == 0 60return 0 61endif 62 63" Analyse previous nonempty line. 64let chg = 0 65let chg = chg + s: MatchCount ( plnum , '[[{(]' ) 66let chg = chg + s: MatchCount ( plnum , '#\s*\%(if\|ifdef\|ifndef\|switch\|while\|macro\|else\)\>' ) 67let chg = chg - s: MatchCount ( plnum , '#\s*end\>' ) 68let chg = chg - s: MatchCount ( plnum , '[]})]' ) 69" Dirty hack for people writing #if and #else on the same line. 70let chg = chg - s: MatchCount ( plnum , '#\s*\%(if\|ifdef\|ifndef\|switch\)\>.*#\s*else\>' ) 71" When chg > 0, then we opened groups and we should indent more, but when 72" chg < 0, we closed groups and this already affected the previous line, 73" so we should not dedent. And when everything else fails, scream. 74let chg = chg > 0 ? chg : 0 75 76" Analyse current line 77" FIXME: If we have to dedent, we should try to find the indentation of the 78" opening line. 79let cur = s: MatchCount ( v: lnum , '^\s*\%(#\s*\%(end\|else\)\>\|[]})]\)' ) 80if cur > 0 81let final = plind + ( chg - cur ) * shiftwidth () 82else 83let final = plind + chg * shiftwidth () 84endif 85 86return final < 0 ? 0 : final 87endfunction