blob: 4acd42e3c84a25f61c35a13d32d5d450343e2d11 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: occam
3" Maintainer: Mario Schweigler <ms44@kent.ac.uk>
4" Last Change: 23 April 2003
5
6" Only load this indent file when no other was loaded.
7if exists("b:did_indent")
8 finish
9endif
10let b:did_indent = 1
11
12"{{{ Settings
13" Set the occam indent function
14setlocal indentexpr=GetOccamIndent()
15" Indent after new line and after initial colon
16setlocal indentkeys=o,O,0=:
17"}}}
18
19" Only define the function once
20if exists("*GetOccamIndent")
21 finish
22endif
Bram Moolenaar8e52a592012-05-18 21:49:28 +020023let s:keepcpo= &cpo
24set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26"{{{ Indent definitions
27" Define carriage return indent
28let s:FirstLevelIndent = '^\C\s*\(IF\|ALT\|PRI\s\+ALT\|PAR\|SEQ\|PRI\s\+PAR\|WHILE\|VALOF\|CLAIM\|FORKING\)\>\|\(--.*\)\@<!\(\<PROC\>\|??\|\<CASE\>\s*\(--.*\)\=\_$\)'
29let s:FirstLevelNonColonEndIndent = '^\C\s*PROTOCOL\>\|\(--.*\)\@<!\<\(\(CHAN\|DATA\)\s\+TYPE\|FUNCTION\)\>'
30let s:SecondLevelIndent = '^\C\s*\(IF\|ALT\|PRI\s\+ALT\)\>\|\(--.*\)\@<!?\s*\<CASE\>\s*\(--.*\)\=\_$'
31let s:SecondLevelNonColonEndIndent = '\(--.*\)\@<!\<\(CHAN\|DATA\)\s\+TYPE\>'
32
33" Define colon indent
34let s:ColonIndent = '\(--.*\)\@<!\<PROC\>'
35let s:ColonNonColonEndIndent = '^\C\s*PROTOCOL\>\|\(--.*\)\@<!\<\(\(CHAN\|DATA\)\s\+TYPE\|FUNCTION\)\>'
36
37let s:ColonEnd = '\(--.*\)\@<!:\s*\(--.*\)\=$'
38let s:ColonStart = '^\s*:\s*\(--.*\)\=$'
39
40" Define comment
41let s:CommentLine = '^\s*--'
42"}}}
43
44"{{{ function GetOccamIndent()
45" Auxiliary function to get the correct indent for a line of occam code
46function GetOccamIndent()
47
48 " Ensure magic is on
49 let save_magic = &magic
50 setlocal magic
51
52 " Get reference line number
53 let linenum = prevnonblank(v:lnum - 1)
54 while linenum > 0 && getline(linenum) =~ s:CommentLine
55 let linenum = prevnonblank(linenum - 1)
56 endwhile
57
58 " Get current indent
59 let curindent = indent(linenum)
60
61 " Get current line
62 let line = getline(linenum)
63
64 " Get previous line number
65 let prevlinenum = prevnonblank(linenum - 1)
66 while prevlinenum > 0 && getline(prevlinenum) =~ s:CommentLine
67 let prevlinenum = prevnonblank(prevlinenum - 1)
68 endwhile
69
70 " Get previous line
71 let prevline = getline(prevlinenum)
72
73 " Colon indent
74 if getline(v:lnum) =~ s:ColonStart
75
76 let found = 0
77
78 while found < 1
79
80 if line =~ s:ColonStart
81 let found = found - 1
82 elseif line =~ s:ColonIndent || (line =~ s:ColonNonColonEndIndent && line !~ s:ColonEnd)
83 let found = found + 1
84 endif
85
86 if found < 1
87 let linenum = prevnonblank(linenum - 1)
88 if linenum > 0
89 let line = getline(linenum)
90 else
91 let found = 1
92 endif
93 endif
94
95 endwhile
96
97 if linenum > 0
98 let curindent = indent(linenum)
99 else
100 let colonline = getline(v:lnum)
101 let tabstr = ''
102 while strlen(tabstr) < &tabstop
103 let tabstr = ' ' . tabstr
104 endwhile
105 let colonline = substitute(colonline, '\t', tabstr, 'g')
106 let curindent = match(colonline, ':')
107 endif
108
109 " Restore magic
110 if !save_magic|setlocal nomagic|endif
111
112 return curindent
113 endif
114
115 if getline(v:lnum) =~ '^\s*:'
116 let colonline = getline(v:lnum)
117 let tabstr = ''
118 while strlen(tabstr) < &tabstop
119 let tabstr = ' ' . tabstr
120 endwhile
121 let colonline = substitute(colonline, '\t', tabstr, 'g')
122 let curindent = match(colonline, ':')
123
124 " Restore magic
125 if !save_magic|setlocal nomagic|endif
126
127 return curindent
128 endif
129
130 " Carriage return indenat
131 if line =~ s:FirstLevelIndent || (line =~ s:FirstLevelNonColonEndIndent && line !~ s:ColonEnd)
132 \ || (line !~ s:ColonStart && (prevline =~ s:SecondLevelIndent
133 \ || (prevline =~ s:SecondLevelNonColonEndIndent && prevline !~ s:ColonEnd)))
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200134 let curindent = curindent + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 " Restore magic
137 if !save_magic|setlocal nomagic|endif
138
139 return curindent
140 endif
141
142 " Commented line
143 if getline(prevnonblank(v:lnum - 1)) =~ s:CommentLine
144
145 " Restore magic
146 if !save_magic|setlocal nomagic|endif
147
148 return indent(prevnonblank(v:lnum - 1))
149 endif
150
151 " Look for previous second level IF / ALT / PRI ALT
152 let found = 0
153
154 while !found
155
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200156 if indent(prevlinenum) == curindent - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 let found = 1
158 endif
159
160 if !found
161 let prevlinenum = prevnonblank(prevlinenum - 1)
162 while prevlinenum > 0 && getline(prevlinenum) =~ s:CommentLine
163 let prevlinenum = prevnonblank(prevlinenum - 1)
164 endwhile
165 if prevlinenum == 0
166 let found = 1
167 endif
168 endif
169
170 endwhile
171
172 if prevlinenum > 0
173 if getline(prevlinenum) =~ s:SecondLevelIndent
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200174 let curindent = curindent + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 endif
176 endif
177
178 " Restore magic
179 if !save_magic|setlocal nomagic|endif
180
181 return curindent
182
183endfunction
184"}}}
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200185
186let &cpo = s:keepcpo
187unlet s:keepcpo