1 " VHDL indent ('93 syntax)
3 " Maintainer: Gerald Lai <laigera+vim?gmail.com>
5 " Last Change: 2007 Jan 29
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_align_genportmap")
32 let g:vhdl_align_genportmap = 1
35 " option to disable alignment of right-hand side assignment "<=" statements
36 if !exists("g:vhdl_align_rhsassign")
37 let g:vhdl_align_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)
58 " default indent starts as previous non-comment line's indent
59 let ind = prevn > 0 ? indent(prevn) : 0
63 " indent: special; kill string so it would not affect other filters
64 " keywords: "report" + string
65 " where: anywhere in current or previous line
66 let s0 = s:NC.'\<report\>\s*".*"'
74 " indent: previous line's comment position, otherwise follow next non-comment line if possible
76 " where: start of current line
81 return stridx(ps, '--')
83 " find nextnonblank line that is not a comment
84 let nn = nextnonblank(curn + 1)
86 while nn > 0 && ns =~ '^\s*--'
87 let nn = nextnonblank(nn + 1)
91 return n != -1 ? n : ind
95 " ****************************************************************************************
96 " indent: align generic variables & port names
97 " keywords: "generic", "map", "port" + "(", provided current line is part of mapping
98 " where: anywhere in previous 2 lines
99 " find following previous non-comment line
100 let pn = prevnonblank(prevn - 1)
102 while pn > 0 && ps =~ '^\s*--'
103 let pn = prevnonblank(pn - 1)
106 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*('))
107 " align closing ")" with opening "("
109 return stridx(prevs, '(')
111 let m = matchend(prevs, '(\s*\ze\w')
115 if g:vhdl_align_genportmap
116 return stridx(prevs, '(') + &sw
123 " indent: align conditional/select statement
124 " keywords: variable + "<=" without ";" ending
125 " where: start of previous line
126 if prevs =~? '^\s*\S\+\s*<=[^;]*'.s:ES
127 if g:vhdl_align_rhsassign
128 return matchend(prevs, '<=\s*\ze.')
134 " indent: backtrace previous non-comment lines for next smaller or equal size indent
135 " keywords: "end" + "record", "units"
136 " where: start of previous line
138 " where: start of previous line
139 " keyword: without "<=" + ";" ending
140 " where: anywhere in previous line
141 " keyword: "=>" + ")" ending, provided current line does not begin with ")"
142 " where: anywhere in previous line
143 " _note_: indent allowed to leave this filter
145 if prevs =~? '^\s*end\s\+\%(record\|units\)\>'
147 elseif prevs =~ '^\s*)'
149 elseif prevs =~ s:NC.'\%(<=.*\)\@<!;'.s:ES || (curs !~ '^\s*)' && prevs =~ s:NC.'=>.*'.s:NC.')'.s:ES)
154 let pn = prevnonblank(prevn - 1)
158 if ps !~ '^\s*--' && t < ind
159 " make sure one of these is true
160 " keywords: variable + "<=" without ";" ending
161 " where: start of previous non-comment line
162 " keywords: "generic", "map", "port"
163 " where: anywhere in previous non-comment line
165 " where: start of previous non-comment line
166 if m < 3 && ps !~? '^\s*\S\+\s*<=[^;]*'.s:ES
167 if ps =~? s:NC.'\<\%(generic\|map\|port\)\>' || ps =~ '^\s*('
174 " find following previous non-comment line
175 let ppn = prevnonblank(pn - 1)
176 let pps = getline(ppn)
177 while ppn > 0 && pps =~ '^\s*--'
178 let ppn = prevnonblank(ppn - 1)
179 let pps = getline(ppn)
183 " where: end of following previous non-comment line
185 " where: start of following previous non-comment line
187 let s1 = s:NC.'\<select'.s:ES
188 if ps !~? s1 && pps =~? s1
189 let ind = indent(ppn)
192 let s1 = '^\s*type\>'
193 if ps !~? s1 && pps =~? s1
194 let ind = indent(ppn)
200 let pn = prevnonblank(pn - 1)
205 " indent: follow indent of previous opening statement, otherwise -sw
207 " where: anywhere in current line
208 if curs =~? s:NC.'\<begin\>'
210 " find previous opening statement of
211 " keywords: "architecture", "block", "entity", "function", "generate", "procedure", "process"
212 let s2 = s:NC.s:NE.'\<\%(architecture\|block\|entity\|function\|generate\|procedure\|process\)\>'
213 if (curs !~? s2.'.*'.s:NC.'\<begin\>.*'.s:ES && prevs =~? s2) || m == 1
219 " indent: +sw if previous line is previous opening statement
220 " keywords: "record", "units"
221 " where: anywhere in current line
222 if curs =~? s:NC.s:NE.'\<\%(record\|units\)\>'
223 " find previous opening statement of
225 let s3 = s:NC.s:NE.'\<type\>'
226 if curs !~? s3.'.*'.s:NC.'\<\%(record\|units\)\>.*'.s:ES && prevs =~? s3
232 " ****************************************************************************************
234 " keywords: "architecture", "configuration", "entity", "library", "package"
235 " where: start of current line
236 if curs =~? '^\s*\%(architecture\|configuration\|entity\|library\|package\)\>'
240 " indent: maintain indent of previous opening statement
242 " where: start of current line
243 " find previous opening statement of
244 " keywords: "architecture", "block", "configuration", "entity", "function", "package", "procedure", "process", "type"
245 if curs =~? '^\s*\<is\>' && prevs =~? s:NC.s:NE.'\<\%(architecture\|block\|configuration\|entity\|function\|package\|procedure\|process\|type\)\>'
249 " indent: maintain indent of previous opening statement
251 " where: start of current line
252 " find previous opening statement of
253 " keywords: "elsif", "if"
254 if curs =~? '^\s*\<then\>' && prevs =~? s:NC.'\%(\<elsif\>\|'.s:NE.'\<if\>\)'
258 " indent: maintain indent of previous opening statement
259 " keyword: "generate"
260 " where: start of current line
261 " find previous opening statement of
262 " keywords: "for", "if"
263 if curs =~? '^\s*\<generate\>' && prevs =~? s:NC.s:NE.'\%(\%(\<wait\s\+\)\@<!\<for\|\<if\)\>'
268 " keywords: "block", "process"
269 " removed: "begin", "case", "elsif", "if", "loop", "record", "units", "while"
270 " where: anywhere in previous line
271 if prevs =~? s:NC.s:NE.'\<\%(block\|process\)\>'
276 " keywords: "architecture", "configuration", "entity", "package"
277 " removed: "component", "for", "when", "with"
278 " where: start of previous line
279 if prevs =~? '^\s*\%(architecture\|configuration\|entity\|package\)\>'
285 " removed: "generate", "is", "=>"
286 " where: end of previous line
287 if prevs =~? s:NC.'\<select'.s:ES
292 " keyword: "begin", "loop", "record", "units"
293 " where: anywhere in previous line
294 " keyword: "component", "else", "for"
295 " where: start of previous line
296 " keyword: "generate", "is", "then", "=>"
297 " where: end of previous line
298 " _note_: indent allowed to leave this filter
299 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
303 " ****************************************************************************************
305 " keywords: "when", provided previous line does not begin with "when", does not end with "is"
306 " where: start of current line
307 let s4 = '^\s*when\>'
309 if prevs =~? s:NC.'\<is'.s:ES
319 " keywords: "else", "elsif", "end" + "block", "for", "function", "generate", "if", "loop", "procedure", "process", "record", "units"
320 " where: start of current line
321 if curs =~? '^\s*\%(else\|elsif\|end\s\+\%(block\|for\|function\|generate\|if\|loop\|procedure\|process\|record\|units\)\)\>'
325 " indent: backtrace previous non-comment lines
326 " keyword: "end" + "case", "component"
327 " where: start of current line
329 if curs =~? '^\s*end\s\+case\>'
331 elseif curs =~? '^\s*end\s\+component\>'
336 " find following previous non-comment line
342 "keywords: "end" + "case"
343 "where: start of previous non-comment line
346 "where: start of previous non-comment line
349 "where: start of previous non-comment line
351 if ps =~? '^\s*end\s\+case\>'
352 return indent(pn) - 2 * &sw
353 elseif ps =~? '^\s*when\>'
354 return indent(pn) - &sw
355 elseif ps =~? '^\s*case\>'
359 "keyword: "component"
360 "where: start of previous non-comment line
362 if ps =~? '^\s*component\>'
367 let pn = prevnonblank(pn - 1)
375 " where: start of current line
381 " keywords: "end" + "architecture", "configuration", "entity", "package"
382 " where: start of current line
383 if curs =~? '^\s*end\s\+\%(architecture\|configuration\|entity\|package\)\>'
388 " keywords: "end" + identifier
389 " where: start of current line
390 "if curs =~? '^\s*end\s\+\w\+\>'
391 if curs =~? '^\s*end\s'
395 " ****************************************************************************************
396 " indent: maintain indent of previous opening statement
397 " keywords: without "generic", "map", "port" + ":" but not ":=" + "in", "out", "inout", "buffer", "linkage", variable & ":="
398 " where: start of current line
399 if curs =~? '^\s*\%(\<\%(generic\|map\|port\)\>.*\)\@<!\S\+\s*:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\w\+\s\+:=\)'
403 " return leftover filtered indent