codekingpro/portable-devtools
114k
1" Vim indent file2" Language: PoV-Ray Scene Description Language3" Maintainer: David Necas (Yeti) <yeti@physics.muni.cz>4" Last Change: 2017 Jun 135" 2022 April: b:undo_indent added by Doug Kearns6" URI: http://trific.ath.cx/Ftp/vim/indent/pov.vim7 8" Only load this indent file when no other was loaded.9if exists("b:did_indent")10 finish11endif12let b:did_indent = 113 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")24 finish25endif26 27" Counts matches of a regexp <rexp> in line number <line>.28" Doesn't count matches inside strings and comments (as defined by current29" syntax).30function! s:MatchCount(line, rexp)31 let str = getline(a:line)32 let i = 033 let n = 034 while i >= 035 let i = matchend(str, a:rexp, i)36 if i >= 0 && synIDattr(synID(a:line, i, 0), "name") !~? "string\|comment"37 let n = n + 138 endif39 endwhile40 return n41endfunction42 43" The main function. Returns indent amount.44function GetPoVRayIndent()45 " If we are inside a comment (may be nested in obscure ways), give up46 if synIDattr(synID(v:lnum, indent(v:lnum)+1, 0), "name") =~? "string\|comment"47 return -148 endif49 50 " Search backwards for the first non-empty, non-comment line.51 let plnum = prevnonblank(v:lnum - 1)52 let plind = indent(plnum)53 while plnum > 0 && synIDattr(synID(plnum, plind+1, 0), "name") =~? "comment"54 let plnum = prevnonblank(plnum - 1)55 let plind = indent(plnum)56 endwhile57 58 " Start indenting from zero59 if plnum == 060 return 061 endif62 63 " Analyse previous nonempty line.64 let chg = 065 let chg = chg + s:MatchCount(plnum, '[[{(]')66 let chg = chg + s:MatchCount(plnum, '#\s*\%(if\|ifdef\|ifndef\|switch\|while\|macro\|else\)\>')67 let chg = chg - s:MatchCount(plnum, '#\s*end\>')68 let chg = chg - s:MatchCount(plnum, '[]})]')69 " Dirty hack for people writing #if and #else on the same line.70 let 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 when72 " chg < 0, we closed groups and this already affected the previous line,73 " so we should not dedent. And when everything else fails, scream.74 let chg = chg > 0 ? chg : 075 76 " Analyse current line77 " FIXME: If we have to dedent, we should try to find the indentation of the78 " opening line.79 let cur = s:MatchCount(v:lnum, '^\s*\%(#\s*\%(end\|else\)\>\|[]})]\)')80 if cur > 081 let final = plind + (chg - cur) * shiftwidth()82 else83 let final = plind + chg * shiftwidth()84 endif85 86 return final < 0 ? 0 : final87endfunction88 