annotate runtime/indent/pov.vim @ 24518:cf334a353c30 v8.2.2799

patch 8.2.2799: Vim9: type casts don't fully work at the script level Commit: https://github.com/vim/vim/commit/459fbdbf9216bc7b4721fc192e08b35039036caa Author: Bram Moolenaar <Bram@vim.org> Date: Wed Apr 21 17:57:26 2021 +0200 patch 8.2.2799: Vim9: type casts don't fully work at the script level Problem: Vim9: type casts don't fully work at the script level. Solution: Implement the missing piece.
author Bram Moolenaar <Bram@vim.org>
date Wed, 21 Apr 2021 18:00:05 +0200
parents 63b0b7b79b25
children 11b656e74444
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 " Vim indent file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 " Language: PoV-Ray Scene Description Language
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 " Maintainer: David Necas (Yeti) <yeti@physics.muni.cz>
11518
63b0b7b79b25 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
4 " Last Change: 2017 Jun 13
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 " URI: http://trific.ath.cx/Ftp/vim/indent/pov.vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 " Only load this indent file when no other was loaded.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 if exists("b:did_indent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 let b:did_indent = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 " Some preliminary settings.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 setlocal nolisp " Make sure lisp indenting doesn't supersede us.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 setlocal indentexpr=GetPoVRayIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 setlocal indentkeys+==else,=end,0]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 " Only define the function once.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 if exists("*GetPoVRayIndent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 " Counts matches of a regexp <rexp> in line number <line>.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 " Doesn't count matches inside strings and comments (as defined by current
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 " syntax).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 function! s:MatchCount(line, rexp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 let str = getline(a:line)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 let i = 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 let n = 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 while i >= 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 let i = matchend(str, a:rexp, i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 if i >= 0 && synIDattr(synID(a:line, i, 0), "name") !~? "string\|comment"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 let n = n + 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 return n
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 " The main function. Returns indent amount.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 function GetPoVRayIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 " If we are inside a comment (may be nested in obscure ways), give up
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 if synIDattr(synID(v:lnum, indent(v:lnum)+1, 0), "name") =~? "string\|comment"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 return -1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 " Search backwards for the frist non-empty, non-comment line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 let plnum = prevnonblank(v:lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 let plind = indent(plnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 while plnum > 0 && synIDattr(synID(plnum, plind+1, 0), "name") =~? "comment"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 let plnum = prevnonblank(plnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 let plind = indent(plnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 " Start indenting from zero
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 if plnum == 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 return 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 " Analyse previous nonempty line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 let chg = 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 let chg = chg + s:MatchCount(plnum, '[[{(]')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 let chg = chg + s:MatchCount(plnum, '#\s*\%(if\|ifdef\|ifndef\|switch\|while\|macro\|else\)\>')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 let chg = chg - s:MatchCount(plnum, '#\s*end\>')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 let chg = chg - s:MatchCount(plnum, '[]})]')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 " Dirty hack for people writing #if and #else on the same line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 let chg = chg - s:MatchCount(plnum, '#\s*\%(if\|ifdef\|ifndef\|switch\)\>.*#\s*else\>')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 " When chg > 0, then we opened groups and we should indent more, but when
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 " chg < 0, we closed groups and this already affected the previous line,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 " so we should not dedent. And when everything else fails, scream.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 let chg = chg > 0 ? chg : 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 " Analyse current line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 " FIXME: If we have to dedent, we should try to find the indentation of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 " opening line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 let cur = s:MatchCount(v:lnum, '^\s*\%(#\s*\%(end\|else\)\>\|[]})]\)')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 if cur > 0
11518
63b0b7b79b25 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
78 let final = plind + (chg - cur) * shiftwidth()
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 else
11518
63b0b7b79b25 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
80 let final = plind + chg * shiftwidth()
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 return final < 0 ? 0 : final
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 endfunction