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