blob: c6f01988dad94763a4c6ea32fed0431c76e5661e [file] [log] [blame]
Bram Moolenaar280f1262006-01-30 00:14:18 +00001" VHDL indent ('93 syntax)
Bram Moolenaar51156d52006-01-26 22:17:47 +00002" Language: VHDL
3" Maintainer: Gerald Lai <laigera+vim?gmail.com>
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004" Version: 1.36
5" Last Change: 2006 Apr 12
Bram Moolenaar280f1262006-01-30 00:14:18 +00006" URL: http://www.vim.org/scripts/script.php?script_id=1450
Bram Moolenaar51156d52006-01-26 22:17:47 +00007
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" setup indent options for local VHDL buffer
15setlocal indentexpr=GetVHDLindent()
16setlocal indentkeys=!^F,o,O,e,0(,0)
17setlocal indentkeys+==~if,=~then,=~elsif,=~else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000018setlocal indentkeys+==~begin,=~is,=~select
Bram Moolenaar51156d52006-01-26 22:17:47 +000019
Bram Moolenaar51156d52006-01-26 22:17:47 +000020" constants
21" not a comment
22let s:NC = '\%(--.*\)\@<!'
23" end of string
24let s:ES = '\s*\%(--.*\)\=$'
25" no "end" keyword in front
26let s:NE = '\%(\<end\s\+\)\@<!'
27
Bram Moolenaar51156d52006-01-26 22:17:47 +000028" only define indent function once
29if exists("*GetVHDLindent")
30 finish
31endif
32
33function GetVHDLindent()
34 " store current line & string
35 let curn = v:lnum
36 let curs = getline(curn)
37
38 " find previous line that is not a comment
39 let prevn = prevnonblank(curn - 1)
40 let prevs = getline(prevn)
41 while prevn > 0 && prevs =~ '^\s*--'
42 let prevn = prevnonblank(prevn - 1)
43 let prevs = getline(prevn)
44 endwhile
45
46 " default indent starts as previous non-comment line's indent
47 let ind = prevn > 0 ? indent(prevn) : 0
48 " backup default
49 let ind2 = ind
50
Bram Moolenaar280f1262006-01-30 00:14:18 +000051 " indent: special; kill string so it would not affect other filters
52 " keywords: "report" + string
53 " where: anywhere in current or previous line
54 let s0 = s:NC.'\<report\>\s*".*"'
55 if curs =~? s0
56 let curs = ""
57 endif
58 if prevs =~? s0
59 let prevs = ""
60 endif
61
Bram Moolenaar51156d52006-01-26 22:17:47 +000062 " indent: previous line's comment position, otherwise follow next non-comment line if possible
63 " keyword: "--"
64 " where: start of current line
65 if curs =~ '^\s*--'
66 let pn = curn - 1
67 let ps = getline(pn)
68 if ps =~ '--'
69 return stridx(ps, '--')
70 else
71 " find nextnonblank line that is not a comment
72 let nn = nextnonblank(curn + 1)
73 let ns = getline(nn)
74 while nn > 0 && ns =~ '^\s*--'
75 let nn = nextnonblank(nn + 1)
76 let ns = getline(nn)
77 endwhile
78 let n = indent(nn)
79 return n != -1 ? n : ind
80 endif
81 endif
82
83 " ****************************************************************************************
84 " indent: align generic variables & port names
85 " keywords: "generic", "map", "port" + "(", provided current line is part of mapping
86 " where: anywhere in previous 2 lines
87 " find following previous non-comment line
88 let pn = prevnonblank(prevn - 1)
89 let ps = getline(pn)
90 while pn > 0 && ps =~ '^\s*--'
91 let pn = prevnonblank(pn - 1)
92 let ps = getline(pn)
93 endwhile
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000094 if (curs =~ '^\s*)' || curs =~? '^\s*\%(\<\%(generic\|map\|port\)\>.*\)\@<!\S\+\s*\%(=>\s*\S\+\|:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\w\+\s\+:=\)\)') && (prevs =~? s:NC.'\<\%(generic\|map\|port\)\s*(\%(\s*\w\)\=' || (ps =~? s:NC.'\<\%(generic\|map\|port\)'.s:ES && prevs =~ '^\s*('))
Bram Moolenaar51156d52006-01-26 22:17:47 +000095 " align closing ")" with opening "("
96 if curs =~ '^\s*)'
97 return stridx(prevs, '(')
98 endif
99 let m = matchend(prevs, '(\s*\ze\w')
100 if m != -1
101 return m
102 else
103 return stridx(prevs, '(') + &sw
104 endif
105 endif
106
107 " indent: align conditional/select statement
Bram Moolenaar280f1262006-01-30 00:14:18 +0000108 " keywords: variable + "<=" without ";" ending
109 " where: start of previous line
110 if prevs =~? '^\s*\S\+\s*<=[^;]*'.s:ES
Bram Moolenaar51156d52006-01-26 22:17:47 +0000111 return matchend(prevs, '<=\s*\ze.')
112 endif
113
114 " indent: backtrace previous non-comment lines for next smaller or equal size indent
115 " keywords: "end" + "record", "units"
116 " where: start of previous line
117 " keyword: ")"
118 " where: start of previous line
119 " keyword: without "<=" + ";" ending
120 " where: anywhere in previous line
121 " keyword: "=>" + ")" ending, provided current line does not begin with ")"
122 " where: anywhere in previous line
123 " _note_: indent allowed to leave this filter
124 let m = 0
125 if prevs =~? '^\s*end\s\+\%(record\|units\)\>'
126 let m = 3
127 elseif prevs =~ '^\s*)'
128 let m = 1
129 elseif prevs =~ s:NC.'\%(<=.*\)\@<!;'.s:ES || (curs !~ '^\s*)' && prevs =~ s:NC.'=>.*'.s:NC.')'.s:ES)
130 let m = 2
131 endif
132
133 if m > 0
134 let pn = prevnonblank(prevn - 1)
135 let ps = getline(pn)
136 while pn > 0
137 let t = indent(pn)
138 if ps !~ '^\s*--' && t < ind
139 " make sure one of these is true
Bram Moolenaar280f1262006-01-30 00:14:18 +0000140 " keywords: variable + "<=" without ";" ending
141 " where: start of previous non-comment line
Bram Moolenaar51156d52006-01-26 22:17:47 +0000142 " keywords: "generic", "map", "port"
143 " where: anywhere in previous non-comment line
144 " keyword: "("
145 " where: start of previous non-comment line
Bram Moolenaar280f1262006-01-30 00:14:18 +0000146 if m < 3 && ps !~? '^\s*\S\+\s*<=[^;]*'.s:ES
Bram Moolenaar51156d52006-01-26 22:17:47 +0000147 if ps =~? s:NC.'\<\%(generic\|map\|port\)\>' || ps =~ '^\s*('
148 let ind = t
149 endif
150 break
151 endif
152 let ind = t
153 if m > 1
154 " find following previous non-comment line
155 let ppn = prevnonblank(pn - 1)
156 let pps = getline(ppn)
157 while ppn > 0 && pps =~ '^\s*--'
158 let ppn = prevnonblank(ppn - 1)
159 let pps = getline(ppn)
160 endwhile
161 " indent: follow
162 " keyword: "select"
163 " where: end of following previous non-comment line
164 " keyword: "type"
165 " where: start of following previous non-comment line
166 if m == 2
167 let s1 = s:NC.'\<select'.s:ES
168 if ps !~? s1 && pps =~? s1
169 let ind = indent(ppn)
170 endif
171 elseif m == 3
172 let s1 = '^\s*type\>'
173 if ps !~? s1 && pps =~? s1
174 let ind = indent(ppn)
175 endif
176 endif
177 endif
178 break
179 endif
180 let pn = prevnonblank(pn - 1)
181 let ps = getline(pn)
182 endwhile
183 endif
184
185 " indent: follow indent of previous opening statement, otherwise -sw
186 " keyword: "begin"
187 " where: anywhere in current line
188 if curs =~? s:NC.'\<begin\>'
189 let ind = ind - &sw
190 " find previous opening statement of
191 " keywords: "architecture", "block", "entity", "function", "generate", "procedure", "process"
192 let s2 = s:NC.s:NE.'\<\%(architecture\|block\|entity\|function\|generate\|procedure\|process\)\>'
193 if curs !~? s2.'.*'.s:NC.'\<begin\>.*'.s:ES && prevs =~? s2
194 let ind = ind + &sw
195 endif
196 return ind
197 endif
198
199 " indent: +sw if previous line is previous opening statement
200 " keywords: "record", "units"
201 " where: anywhere in current line
202 if curs =~? s:NC.s:NE.'\<\%(record\|units\)\>'
203 " find previous opening statement of
204 " keyword: "type"
205 let s3 = s:NC.s:NE.'\<type\>'
206 if curs !~? s3.'.*'.s:NC.'\<\%(record\|units\)\>.*'.s:ES && prevs =~? s3
207 let ind = ind + &sw
208 endif
209 return ind
210 endif
211
212 " ****************************************************************************************
213 " indent: 0
214 " keywords: "architecture", "configuration", "entity", "library", "package"
215 " where: start of current line
216 if curs =~? '^\s*\%(architecture\|configuration\|entity\|library\|package\)\>'
217 return 0
218 endif
219
Bram Moolenaar280f1262006-01-30 00:14:18 +0000220 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000221 " keyword: "is"
222 " where: start of current line
223 " find previous opening statement of
224 " keywords: "architecture", "block", "configuration", "entity", "function", "package", "procedure", "process", "type"
225 if curs =~? '^\s*\<is\>' && prevs =~? s:NC.s:NE.'\<\%(architecture\|block\|configuration\|entity\|function\|package\|procedure\|process\|type\)\>'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000226 return ind2
Bram Moolenaar51156d52006-01-26 22:17:47 +0000227 endif
228
Bram Moolenaar280f1262006-01-30 00:14:18 +0000229 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000230 " keyword: "then"
231 " where: start of current line
232 " find previous opening statement of
233 " keywords: "elsif", "if"
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000234 if curs =~? '^\s*\<then\>' && prevs =~? s:NC.'\%(\<elsif\>\|'.s:NE.'\<if\>\)'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000235 return ind2
Bram Moolenaar51156d52006-01-26 22:17:47 +0000236 endif
237
Bram Moolenaar280f1262006-01-30 00:14:18 +0000238 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000239 " keyword: "generate"
240 " where: start of current line
241 " find previous opening statement of
242 " keywords: "for", "if"
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000243 if curs =~? '^\s*\<generate\>' && prevs =~? s:NC.s:NE.'\%(\%(\<wait\s\+\)\@<!\<for\>\|\<if\>\)'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000244 return ind2
Bram Moolenaar51156d52006-01-26 22:17:47 +0000245 endif
246
247 " indent: +sw
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000248 " keywords: "begin", "block", "loop", "process", "record", "units"
249 " removed: "case", "elsif", "if", "while"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000250 " where: anywhere in previous line
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000251 if prevs =~? s:NC.'\%(\<begin\>\|'.s:NE.'\<\%(block\|loop\|process\|record\|units\)\>\)'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000252 return ind + &sw
253 endif
254
255 " indent: +sw
Bram Moolenaar280f1262006-01-30 00:14:18 +0000256 " keywords: "architecture", "component", "configuration", "entity", "for", "package"
257 " removed: "when", "with"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000258 " where: start of previous line
Bram Moolenaar280f1262006-01-30 00:14:18 +0000259 if prevs =~? '^\s*\%(architecture\|component\|configuration\|entity\|for\|package\)\>'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000260 return ind + &sw
261 endif
262
263 " indent: +sw
264 " keyword: "generate", "is", "select", "=>"
265 " where: end of previous line
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000266 if prevs =~? s:NC.'\%(\%('.s:NE.'\<generate\|\<is\|\<select\)\|=>\)'.s:ES
Bram Moolenaar51156d52006-01-26 22:17:47 +0000267 return ind + &sw
268 endif
269
270 " indent: +sw
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000271 " keyword: "else"
272 " where: start of previous line
273 " keyword: "then"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000274 " where: end of previous line
275 " _note_: indent allowed to leave this filter
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000276 if prevs =~? '^\s*else\>' || prevs =~? s:NC.'\<then'.s:ES
Bram Moolenaar51156d52006-01-26 22:17:47 +0000277 let ind = ind + &sw
278 endif
279
280 " ****************************************************************************************
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000281 " indent: -sw
282 " keywords: "when", provided previous line does not begin with "when"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000283 " where: start of current line
284 let s4 = '^\s*when\>'
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000285 if curs =~? s4
286 if prevs !~? s4
287 return ind - &sw
288 else
289 return ind2
290 endif
Bram Moolenaar51156d52006-01-26 22:17:47 +0000291 endif
292
293 " indent: -sw
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000294 " keywords: "else", "elsif", provided previous line does not contain "then"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000295 " where: start of current line
296 if curs =~? '^\s*\%(else\|elsif\)\>'
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000297 if prevs !~? s:NC.'\<then\>'
298 return ind - &sw
299 else
300 return ind2
301 endif
Bram Moolenaar51156d52006-01-26 22:17:47 +0000302 endif
303
304 " indent: -sw
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000305 " keywords: "end" + "if", provided previous line does not begin with "else", not contain "then"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000306 " where: start of current line
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000307 if curs =~? '^\s*end\s\+if\>'
308 if prevs !~? '^\s*else\>' && prevs !~? s:NC.'\<then\>'
309 return ind - &sw
310 else
311 return ind2
312 endif
313 endif
314
315 " indent: -sw
316 " keywords: "end" + "function", "procedure", provided previous line does not contain "begin"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000317 " where: start of current line
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000318 if curs =~? '^\s*end\s\+\%(function\|procedure\)\>'
319 if prevs !~? s:NC.'\<begin\>'
320 return ind - &sw
321 else
322 return ind2
323 endif
324 endif
325
326 " indent: -sw
327 " keywords: "end" + "block", "for", "generate", "loop", "process", "record", "units"
328 " where: start of current line
329 if curs =~? '^\s*end\s\+\%(block\|for\|generate\|loop\|process\|record\|units\)\>'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000330 return ind - &sw
331 endif
332
Bram Moolenaar280f1262006-01-30 00:14:18 +0000333 " indent: backtrace previous non-comment lines
334 " keyword: "end" + "case", "component"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000335 " where: start of current line
Bram Moolenaar280f1262006-01-30 00:14:18 +0000336 let m = 0
Bram Moolenaar51156d52006-01-26 22:17:47 +0000337 if curs =~? '^\s*end\s\+case\>'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000338 let m = 1
339 elseif curs =~? '^\s*end\s\+component\>'
340 let m = 2
341 endif
342
343 if m > 0
Bram Moolenaar51156d52006-01-26 22:17:47 +0000344 " find following previous non-comment line
345 let pn = prevn
346 let ps = getline(pn)
347 while pn > 0
348 if ps !~ '^\s*--'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000349 "indent: -2sw
350 "keywords: "end" + "case"
351 "where: start of previous non-comment line
352 "indent: -sw
353 "keywords: "when"
354 "where: start of previous non-comment line
355 "indent: follow
356 "keywords: "case"
357 "where: start of previous non-comment line
358 if m == 1
359 if ps =~? '^\s*end\s\+case\>'
360 return indent(pn) - 2 * &sw
361 elseif ps =~? '^\s*when\>'
362 return indent(pn) - &sw
363 elseif ps =~? '^\s*case\>'
364 return indent(pn)
365 endif
366 "indent: follow
367 "keyword: "component"
368 "where: anywhere in previous non-comment line
369 elseif m == 2
370 if ps =~? s:NC.s:NE.'\<component\>'
371 return indent(pn)
372 endif
Bram Moolenaar51156d52006-01-26 22:17:47 +0000373 endif
374 endif
375 let pn = prevnonblank(pn - 1)
376 let ps = getline(pn)
377 endwhile
Bram Moolenaar280f1262006-01-30 00:14:18 +0000378 return ind - &sw
Bram Moolenaar51156d52006-01-26 22:17:47 +0000379 endif
380
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000381 " indent: -sw
382 " keyword: ")"
383 " where: start of current line
384 if curs =~ '^\s*)'
385 return ind - &sw
386 endif
387
Bram Moolenaar51156d52006-01-26 22:17:47 +0000388 " indent: 0
389 " keywords: "end" + "architecture", "configuration", "entity", "package"
390 " where: start of current line
391 if curs =~? '^\s*end\s\+\%(architecture\|configuration\|entity\|package\)\>'
392 return 0
393 endif
394
395 " indent: -sw
396 " keywords: "end" + identifier
397 " where: start of current line
398 if curs =~? '^\s*end\s\+\w\+\>'
399 return ind - &sw
400 endif
401
402 " ****************************************************************************************
Bram Moolenaar280f1262006-01-30 00:14:18 +0000403 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000404 " keywords: without "generic", "map", "port" + ":" but not ":=" + "in", "out", "inout", "buffer", "linkage", variable & ":="
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000405 " where: start of current line
406 if curs =~? '^\s*\%(\<\%(generic\|map\|port\)\>.*\)\@<!\S\+\s*:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\w\+\s\+:=\)'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000407 return ind2
408 endif
409
410 " return leftover filtered indent
411 return ind
412endfunction