blob: 60077ffffea6cb714a34cbc28d2d8fb4d23539d6 [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 Moolenaarce001a32022-04-27 15:25:03 +01005" 2022 April: b:undo_indent added by Doug Kearns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006" 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")
10 finish
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
Bram Moolenaarce001a32022-04-27 15:25:03 +010020let b:undo_indent = "setl inde< indk< lisp<"
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022" Only define the function once.
23if exists("*GetPoVRayIndent")
24 finish
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)
31 let str = getline(a:line)
32 let i = 0
33 let n = 0
34 while i >= 0
35 let i = matchend(str, a:rexp, i)
36 if i >= 0 && synIDattr(synID(a:line, i, 0), "name") !~? "string\|comment"
37 let n = n + 1
38 endif
39 endwhile
40 return 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
46 if synIDattr(synID(v:lnum, indent(v:lnum)+1, 0), "name") =~? "string\|comment"
47 return -1
48 endif
49
Bram Moolenaar6c391a72021-09-09 21:55:11 +020050 " Search backwards for the first non-empty, non-comment line.
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 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 endwhile
57
58 " Start indenting from zero
59 if plnum == 0
60 return 0
61 endif
62
63 " Analyse previous nonempty line.
64 let chg = 0
65 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 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.
74 let 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.
79 let cur = s:MatchCount(v:lnum, '^\s*\%(#\s*\%(end\|else\)\>\|[]})]\)')
80 if cur > 0
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020081 let final = plind + (chg - cur) * shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020083 let final = plind + chg * shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 endif
85
86 return final < 0 ? 0 : final
87endfunction