1 " VHDL indent ('93 syntax)
3 " Maintainer: Gerald Lai <laigera+vim?gmail.com>
5 " Last Change: 2007 Aug 17
6 " URL: http://www.vim.org/scripts/script.php?script_id=1450
8 " only load this indent file when no other was loaded
9 if exists("b:did_indent")
14 " setup indent options for local VHDL buffer
15 setlocal indentexpr=GetVHDLindent()
16 setlocal indentkeys=!^F,o,O,0(,0)
17 setlocal indentkeys+==~begin,=~end\ ,=~end\ ,=~is,=~select,=~when
18 setlocal indentkeys+==~if,=~then,=~elsif,=~else
19 setlocal indentkeys+==~case,=~loop,=~for,=~generate,=~record,=~units,=~process,=~block,=~function,=~component,=~procedure
20 setlocal indentkeys+==~architecture,=~configuration,=~entity,=~package
24 let s:NC = '\%(--.*\)\@<!'
26 let s:ES = '\s*\%(--.*\)\=$'
27 " no "end" keyword in front
28 let s:NE = '\%(\<end\s\+\)\@<!'
30 " option to disable alignment of generic/port mappings
31 if !exists("g:vhdl_indent_genportmap")
32 let g:vhdl_indent_genportmap = 1
35 " option to disable alignment of right-hand side assignment "<=" statements
36 if !exists("g:vhdl_indent_rhsassign")
37 let g:vhdl_indent_rhsassign = 1
40 " only define indent function once
41 if exists("*GetVHDLindent")
45 function GetVHDLindent()
46 " store current line & string
48 let curs = getline(curn)
50 " find previous line that is not a comment
51 let prevn = prevnonblank(curn - 1)
52 let prevs = getline(prevn)
53 while prevn > 0 && prevs =~ '^\s*--'
54 let prevn = prevnonblank(prevn - 1)
55 let prevs = getline(prevn)
57 let prevs_noi = substitute(prevs, '^\s*', '', '')
59 " default indent starts as previous non-comment line's indent
60 let ind = prevn > 0 ? indent(prevn) : 0
64 " indent: special; kill string so it would not affect other filters
65 " keywords: "report" + string
66 " where: anywhere in current or previous line
67 let s0 = s:NC.'\<report\>\s*".*"'
75 " indent: previous line's comment position, otherwise follow next non-comment line if possible
77 " where: start of current line
81 if curs =~ '^\s*--\s' && ps =~ '--'
82 return indent(pn) + stridx(substitute(ps, '^\s*', '', ''), '--')
84 " find nextnonblank line that is not a comment
85 let nn = nextnonblank(curn + 1)
87 while nn > 0 && ns =~ '^\s*--'
88 let nn = nextnonblank(nn + 1)
92 return n != -1 ? n : ind
96 " ****************************************************************************************
97 " indent: align generic variables & port names
98 " keywords: "generic", "map", "port" + "(", provided current line is part of mapping
99 " where: anywhere in previous 2 lines
100 " find following previous non-comment line
101 let pn = prevnonblank(prevn - 1)
103 while pn > 0 && ps =~ '^\s*--'
104 let pn = prevnonblank(pn - 1)
107 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*('))
108 " align closing ")" with opening "("
110 return ind2 + stridx(prevs_noi, '(')
112 let m = matchend(prevs_noi, '(\s*\ze\w')
116 if g:vhdl_indent_genportmap
117 return ind2 + stridx(prevs_noi, '(') + &sw
124 " indent: align conditional/select statement
125 " keywords: variable + "<=" without ";" ending
126 " where: start of previous line
127 if prevs =~? '^\s*\S\+\s*<=[^;]*'.s:ES
128 if g:vhdl_indent_rhsassign
129 return ind2 + matchend(prevs_noi, '<=\s*\ze.')
135 " indent: backtrace previous non-comment lines for next smaller or equal size indent
136 " keywords: "end" + "record", "units"
137 " where: start of previous line
139 " where: start of previous line
140 " keyword: without "<=" + ";" ending
141 " where: anywhere in previous line
142 " keyword: "=>" + ")" ending, provided current line does not begin with ")"
143 " where: anywhere in previous line
144 " _note_: indent allowed to leave this filter
146 if prevs =~? '^\s*end\s\+\%(record\|units\)\>'
148 elseif prevs =~ '^\s*)'
150 elseif prevs =~ s:NC.'\%(<=.*\)\@<!;'.s:ES || (curs !~ '^\s*)' && prevs =~ s:NC.'=>.*'.s:NC.')'.s:ES)
155 let pn = prevnonblank(prevn - 1)
159 if ps !~ '^\s*--' && t < ind
160 " make sure one of these is true
161 " keywords: variable + "<=" without ";" ending
162 " where: start of previous non-comment line
163 " keywords: "generic", "map", "port"
164 " where: anywhere in previous non-comment line
166 " where: start of previous non-comment line
167 if m < 3 && ps !~? '^\s*\S\+\s*<=[^;]*'.s:ES
168 if ps =~? s:NC.'\<\%(generic\|map\|port\)\>' || ps =~ '^\s*('
175 " find following previous non-comment line
176 let ppn = prevnonblank(pn - 1)
177 let pps = getline(ppn)
178 while ppn > 0 && pps =~ '^\s*--'
179 let ppn = prevnonblank(ppn - 1)
180 let pps = getline(ppn)
184 " where: end of following previous non-comment line
186 " where: start of following previous non-comment line
188 let s1 = s:NC.'\<select'.s:ES
189 if ps !~? s1 && pps =~? s1
190 let ind = indent(ppn)
193 let s1 = '^\s*type\>'
194 if ps !~? s1 && pps =~? s1
195 let ind = indent(ppn)
201 let pn = prevnonblank(pn - 1)
206 " indent: follow indent of previous opening statement, otherwise -sw
208 " where: anywhere in current line
209 if curs =~? s:NC.'\<begin\>'
211 " find previous opening statement of
212 " keywords: "architecture", "block", "entity", "function", "generate", "procedure", "process"
213 let s2 = s:NC.s:NE.'\<\%(architecture\|block\|entity\|function\|generate\|procedure\|process\)\>'
214 if (curs !~? s2.'.*'.s:NC.'\<begin\>.*'.s:ES && prevs =~? s2) || m == 1
220 " indent: +sw if previous line is previous opening statement
221 " keywords: "record", "units"
222 " where: anywhere in current line
223 if curs =~? s:NC.s:NE.'\<\%(record\|units\)\>'
224 " find previous opening statement of
226 let s3 = s:NC.s:NE.'\<type\>'
227 if curs !~? s3.'.*'.s:NC.'\<\%(record\|units\)\>.*'.s:ES && prevs =~? s3
233 " ****************************************************************************************
235 " keywords: "architecture", "configuration", "entity", "library", "package"
236 " where: start of current line
237 if curs =~? '^\s*\%(architecture\|configuration\|entity\|library\|package\)\>'
241 " indent: maintain indent of previous opening statement
243 " where: start of current line
244 " find previous opening statement of
245 " keywords: "architecture", "block", "configuration", "entity", "function", "package", "procedure", "process", "type"
246 if curs =~? '^\s*\<is\>' && prevs =~? s:NC.s:NE.'\<\%(architecture\|block\|configuration\|entity\|function\|package\|procedure\|process\|type\)\>'
250 " indent: maintain indent of previous opening statement
252 " where: start of current line
253 " find previous opening statement of
254 " keywords: "elsif", "if"
255 if curs =~? '^\s*\<then\>' && prevs =~? s:NC.'\%(\<elsif\>\|'.s:NE.'\<if\>\)'
259 " indent: maintain indent of previous opening statement
260 " keyword: "generate"
261 " where: start of current line
262 " find previous opening statement of
263 " keywords: "for", "if"
264 if curs =~? '^\s*\<generate\>' && prevs =~? s:NC.s:NE.'\%(\%(\<wait\s\+\)\@<!\<for\|\<if\)\>'
269 " keywords: "block", "process"
270 " removed: "begin", "case", "elsif", "if", "loop", "record", "units", "while"
271 " where: anywhere in previous line
272 if prevs =~? s:NC.s:NE.'\<\%(block\|process\)\>'
277 " keywords: "architecture", "configuration", "entity", "package"
278 " removed: "component", "for", "when", "with"
279 " where: start of previous line
280 if prevs =~? '^\s*\%(architecture\|configuration\|entity\|package\)\>'
286 " removed: "generate", "is", "=>"
287 " where: end of previous line
288 if prevs =~? s:NC.'\<select'.s:ES
293 " keyword: "begin", "loop", "record", "units"
294 " where: anywhere in previous line
295 " keyword: "component", "else", "for"
296 " where: start of previous line
297 " keyword: "generate", "is", "then", "=>"
298 " where: end of previous line
299 " _note_: indent allowed to leave this filter
300 if prevs =~? s:NC.'\%(\<begin\>\|'.s:NE.'\<\%(loop\|record\|units\)\>\)' || prevs =~? '^\s*\%(component\|else\|for\)\>' || prevs =~? s:NC.'\%('.s:NE.'\<generate\|\<\%(is\|then\)\|=>\)'.s:ES
304 " ****************************************************************************************
306 " keywords: "when", provided previous line does not begin with "when", does not end with "is"
307 " where: start of current line
308 let s4 = '^\s*when\>'
310 if prevs =~? s:NC.'\<is'.s:ES
320 " keywords: "else", "elsif", "end" + "block", "for", "function", "generate", "if", "loop", "procedure", "process", "record", "units"
321 " where: start of current line
322 if curs =~? '^\s*\%(else\|elsif\|end\s\+\%(block\|for\|function\|generate\|if\|loop\|procedure\|process\|record\|units\)\)\>'
326 " indent: backtrace previous non-comment lines
327 " keyword: "end" + "case", "component"
328 " where: start of current line
330 if curs =~? '^\s*end\s\+case\>'
332 elseif curs =~? '^\s*end\s\+component\>'
337 " find following previous non-comment line
343 "keywords: "end" + "case"
344 "where: start of previous non-comment line
347 "where: start of previous non-comment line
350 "where: start of previous non-comment line
352 if ps =~? '^\s*end\s\+case\>'
353 return indent(pn) - 2 * &sw
354 elseif ps =~? '^\s*when\>'
355 return indent(pn) - &sw
356 elseif ps =~? '^\s*case\>'
360 "keyword: "component"
361 "where: start of previous non-comment line
363 if ps =~? '^\s*component\>'
368 let pn = prevnonblank(pn - 1)
376 " where: start of current line
382 " keywords: "end" + "architecture", "configuration", "entity", "package"
383 " where: start of current line
384 if curs =~? '^\s*end\s\+\%(architecture\|configuration\|entity\|package\)\>'
389 " keywords: "end" + identifier
390 " where: start of current line
391 "if curs =~? '^\s*end\s\+\w\+\>'
392 if curs =~? '^\s*end\s'
396 " ****************************************************************************************
397 " indent: maintain indent of previous opening statement
398 " keywords: without "generic", "map", "port" + ":" but not ":=" + "in", "out", "inout", "buffer", "linkage", variable & ":="
399 " where: start of current line
400 if curs =~? '^\s*\%(\<\%(generic\|map\|port\)\>.*\)\@<!\S\+\s*:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\w\+\s\+:=\)'
404 " return leftover filtered indent