blob: d288be4ceb113e9d4415609e21e4b4c841aaf847 [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 Moolenaar9964e462007-05-05 17:54:07 +00004" Version: 1.50
5" Last Change: 2007 Jan 29
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()
Bram Moolenaar9964e462007-05-05 17:54:07 +000016setlocal indentkeys=!^F,o,O,0(,0)
17setlocal indentkeys+==~begin,=~end\ ,=~end\ ,=~is,=~select,=~when
Bram Moolenaar51156d52006-01-26 22:17:47 +000018setlocal indentkeys+==~if,=~then,=~elsif,=~else
Bram Moolenaar9964e462007-05-05 17:54:07 +000019setlocal indentkeys+==~case,=~loop,=~for,=~generate,=~record,=~units,=~process,=~block,=~function,=~component,=~procedure
20setlocal indentkeys+==~architecture,=~configuration,=~entity,=~package
Bram Moolenaar51156d52006-01-26 22:17:47 +000021
Bram Moolenaar51156d52006-01-26 22:17:47 +000022" constants
23" not a comment
24let s:NC = '\%(--.*\)\@<!'
25" end of string
26let s:ES = '\s*\%(--.*\)\=$'
27" no "end" keyword in front
28let s:NE = '\%(\<end\s\+\)\@<!'
29
Bram Moolenaar9964e462007-05-05 17:54:07 +000030" option to disable alignment of generic/port mappings
31if !exists("g:vhdl_align_genportmap")
32 let g:vhdl_align_genportmap = 1
33endif
34
35" option to disable alignment of right-hand side assignment "<=" statements
36if !exists("g:vhdl_align_rhsassign")
37 let g:vhdl_align_rhsassign = 1
38endif
39
Bram Moolenaar51156d52006-01-26 22:17:47 +000040" only define indent function once
41if exists("*GetVHDLindent")
42 finish
43endif
44
45function GetVHDLindent()
46 " store current line & string
47 let curn = v:lnum
48 let curs = getline(curn)
49
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)
56 endwhile
57
58 " default indent starts as previous non-comment line's indent
59 let ind = prevn > 0 ? indent(prevn) : 0
60 " backup default
61 let ind2 = ind
62
Bram Moolenaar280f1262006-01-30 00:14:18 +000063 " 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*".*"'
67 if curs =~? s0
68 let curs = ""
69 endif
70 if prevs =~? s0
71 let prevs = ""
72 endif
73
Bram Moolenaar51156d52006-01-26 22:17:47 +000074 " indent: previous line's comment position, otherwise follow next non-comment line if possible
75 " keyword: "--"
76 " where: start of current line
77 if curs =~ '^\s*--'
78 let pn = curn - 1
79 let ps = getline(pn)
80 if ps =~ '--'
81 return stridx(ps, '--')
82 else
83 " find nextnonblank line that is not a comment
84 let nn = nextnonblank(curn + 1)
85 let ns = getline(nn)
86 while nn > 0 && ns =~ '^\s*--'
87 let nn = nextnonblank(nn + 1)
88 let ns = getline(nn)
89 endwhile
90 let n = indent(nn)
91 return n != -1 ? n : ind
92 endif
93 endif
94
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)
101 let ps = getline(pn)
102 while pn > 0 && ps =~ '^\s*--'
103 let pn = prevnonblank(pn - 1)
104 let ps = getline(pn)
105 endwhile
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000106 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 +0000107 " align closing ")" with opening "("
108 if curs =~ '^\s*)'
109 return stridx(prevs, '(')
110 endif
111 let m = matchend(prevs, '(\s*\ze\w')
112 if m != -1
113 return m
114 else
Bram Moolenaar9964e462007-05-05 17:54:07 +0000115 if g:vhdl_align_genportmap
116 return stridx(prevs, '(') + &sw
117 else
118 return ind2 + &sw
119 endif
Bram Moolenaar51156d52006-01-26 22:17:47 +0000120 endif
121 endif
122
123 " indent: align conditional/select statement
Bram Moolenaar280f1262006-01-30 00:14:18 +0000124 " keywords: variable + "<=" without ";" ending
125 " where: start of previous line
126 if prevs =~? '^\s*\S\+\s*<=[^;]*'.s:ES
Bram Moolenaar9964e462007-05-05 17:54:07 +0000127 if g:vhdl_align_rhsassign
128 return matchend(prevs, '<=\s*\ze.')
129 else
130 return ind2 + &sw
131 endif
Bram Moolenaar51156d52006-01-26 22:17:47 +0000132 endif
133
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
137 " keyword: ")"
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
144 let m = 0
145 if prevs =~? '^\s*end\s\+\%(record\|units\)\>'
146 let m = 3
147 elseif prevs =~ '^\s*)'
148 let m = 1
149 elseif prevs =~ s:NC.'\%(<=.*\)\@<!;'.s:ES || (curs !~ '^\s*)' && prevs =~ s:NC.'=>.*'.s:NC.')'.s:ES)
150 let m = 2
151 endif
152
153 if m > 0
154 let pn = prevnonblank(prevn - 1)
155 let ps = getline(pn)
156 while pn > 0
157 let t = indent(pn)
158 if ps !~ '^\s*--' && t < ind
159 " make sure one of these is true
Bram Moolenaar280f1262006-01-30 00:14:18 +0000160 " keywords: variable + "<=" without ";" ending
161 " where: start of previous non-comment line
Bram Moolenaar51156d52006-01-26 22:17:47 +0000162 " keywords: "generic", "map", "port"
163 " where: anywhere in previous non-comment line
164 " keyword: "("
165 " where: start of previous non-comment line
Bram Moolenaar280f1262006-01-30 00:14:18 +0000166 if m < 3 && ps !~? '^\s*\S\+\s*<=[^;]*'.s:ES
Bram Moolenaar51156d52006-01-26 22:17:47 +0000167 if ps =~? s:NC.'\<\%(generic\|map\|port\)\>' || ps =~ '^\s*('
168 let ind = t
169 endif
170 break
171 endif
172 let ind = t
173 if m > 1
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)
180 endwhile
181 " indent: follow
182 " keyword: "select"
183 " where: end of following previous non-comment line
184 " keyword: "type"
185 " where: start of following previous non-comment line
186 if m == 2
187 let s1 = s:NC.'\<select'.s:ES
188 if ps !~? s1 && pps =~? s1
189 let ind = indent(ppn)
190 endif
191 elseif m == 3
192 let s1 = '^\s*type\>'
193 if ps !~? s1 && pps =~? s1
194 let ind = indent(ppn)
195 endif
196 endif
197 endif
198 break
199 endif
200 let pn = prevnonblank(pn - 1)
201 let ps = getline(pn)
202 endwhile
203 endif
204
205 " indent: follow indent of previous opening statement, otherwise -sw
206 " keyword: "begin"
207 " where: anywhere in current line
208 if curs =~? s:NC.'\<begin\>'
209 let ind = ind - &sw
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\)\>'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000213 if (curs !~? s2.'.*'.s:NC.'\<begin\>.*'.s:ES && prevs =~? s2) || m == 1
Bram Moolenaar51156d52006-01-26 22:17:47 +0000214 let ind = ind + &sw
215 endif
216 return ind
217 endif
218
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
224 " keyword: "type"
225 let s3 = s:NC.s:NE.'\<type\>'
226 if curs !~? s3.'.*'.s:NC.'\<\%(record\|units\)\>.*'.s:ES && prevs =~? s3
227 let ind = ind + &sw
228 endif
229 return ind
230 endif
231
232 " ****************************************************************************************
233 " indent: 0
234 " keywords: "architecture", "configuration", "entity", "library", "package"
235 " where: start of current line
236 if curs =~? '^\s*\%(architecture\|configuration\|entity\|library\|package\)\>'
237 return 0
238 endif
239
Bram Moolenaar280f1262006-01-30 00:14:18 +0000240 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000241 " keyword: "is"
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\)\>'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000246 return ind2
Bram Moolenaar51156d52006-01-26 22:17:47 +0000247 endif
248
Bram Moolenaar280f1262006-01-30 00:14:18 +0000249 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000250 " keyword: "then"
251 " where: start of current line
252 " find previous opening statement of
253 " keywords: "elsif", "if"
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000254 if curs =~? '^\s*\<then\>' && prevs =~? s:NC.'\%(\<elsif\>\|'.s:NE.'\<if\>\)'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000255 return ind2
Bram Moolenaar51156d52006-01-26 22:17:47 +0000256 endif
257
Bram Moolenaar280f1262006-01-30 00:14:18 +0000258 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000259 " keyword: "generate"
260 " where: start of current line
261 " find previous opening statement of
262 " keywords: "for", "if"
Bram Moolenaar9964e462007-05-05 17:54:07 +0000263 if curs =~? '^\s*\<generate\>' && prevs =~? s:NC.s:NE.'\%(\%(\<wait\s\+\)\@<!\<for\|\<if\)\>'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000264 return ind2
Bram Moolenaar51156d52006-01-26 22:17:47 +0000265 endif
266
267 " indent: +sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000268 " keywords: "block", "process"
269 " removed: "begin", "case", "elsif", "if", "loop", "record", "units", "while"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000270 " where: anywhere in previous line
Bram Moolenaar9964e462007-05-05 17:54:07 +0000271 if prevs =~? s:NC.s:NE.'\<\%(block\|process\)\>'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000272 return ind + &sw
273 endif
274
275 " indent: +sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000276 " keywords: "architecture", "configuration", "entity", "package"
277 " removed: "component", "for", "when", "with"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000278 " where: start of previous line
Bram Moolenaar9964e462007-05-05 17:54:07 +0000279 if prevs =~? '^\s*\%(architecture\|configuration\|entity\|package\)\>'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000280 return ind + &sw
281 endif
282
283 " indent: +sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000284 " keyword: "select"
285 " removed: "generate", "is", "=>"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000286 " where: end of previous line
Bram Moolenaar9964e462007-05-05 17:54:07 +0000287 if prevs =~? s:NC.'\<select'.s:ES
Bram Moolenaar51156d52006-01-26 22:17:47 +0000288 return ind + &sw
289 endif
290
291 " indent: +sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000292 " keyword: "begin", "loop", "record", "units"
293 " where: anywhere in previous line
294 " keyword: "component", "else", "for"
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000295 " where: start of previous line
Bram Moolenaar9964e462007-05-05 17:54:07 +0000296 " keyword: "generate", "is", "then", "=>"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000297 " where: end of previous line
298 " _note_: indent allowed to leave this filter
Bram Moolenaar9964e462007-05-05 17:54:07 +0000299 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
Bram Moolenaar51156d52006-01-26 22:17:47 +0000300 let ind = ind + &sw
301 endif
302
303 " ****************************************************************************************
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000304 " indent: -sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000305 " keywords: "when", provided previous line does not begin with "when", does not end with "is"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000306 " where: start of current line
307 let s4 = '^\s*when\>'
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000308 if curs =~? s4
Bram Moolenaar9964e462007-05-05 17:54:07 +0000309 if prevs =~? s:NC.'\<is'.s:ES
310 return ind
311 elseif prevs !~? s4
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000312 return ind - &sw
313 else
314 return ind2
315 endif
Bram Moolenaar51156d52006-01-26 22:17:47 +0000316 endif
317
318 " indent: -sw
Bram Moolenaar9964e462007-05-05 17:54:07 +0000319 " keywords: "else", "elsif", "end" + "block", "for", "function", "generate", "if", "loop", "procedure", "process", "record", "units"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000320 " where: start of current line
Bram Moolenaar9964e462007-05-05 17:54:07 +0000321 if curs =~? '^\s*\%(else\|elsif\|end\s\+\%(block\|for\|function\|generate\|if\|loop\|procedure\|process\|record\|units\)\)\>'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000322 return ind - &sw
323 endif
324
Bram Moolenaar280f1262006-01-30 00:14:18 +0000325 " indent: backtrace previous non-comment lines
326 " keyword: "end" + "case", "component"
Bram Moolenaar51156d52006-01-26 22:17:47 +0000327 " where: start of current line
Bram Moolenaar280f1262006-01-30 00:14:18 +0000328 let m = 0
Bram Moolenaar51156d52006-01-26 22:17:47 +0000329 if curs =~? '^\s*end\s\+case\>'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000330 let m = 1
331 elseif curs =~? '^\s*end\s\+component\>'
332 let m = 2
333 endif
334
335 if m > 0
Bram Moolenaar51156d52006-01-26 22:17:47 +0000336 " find following previous non-comment line
337 let pn = prevn
338 let ps = getline(pn)
339 while pn > 0
340 if ps !~ '^\s*--'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000341 "indent: -2sw
342 "keywords: "end" + "case"
343 "where: start of previous non-comment line
344 "indent: -sw
345 "keywords: "when"
346 "where: start of previous non-comment line
347 "indent: follow
348 "keywords: "case"
349 "where: start of previous non-comment line
350 if m == 1
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\>'
356 return indent(pn)
357 endif
358 "indent: follow
359 "keyword: "component"
Bram Moolenaar9964e462007-05-05 17:54:07 +0000360 "where: start of previous non-comment line
Bram Moolenaar280f1262006-01-30 00:14:18 +0000361 elseif m == 2
Bram Moolenaar9964e462007-05-05 17:54:07 +0000362 if ps =~? '^\s*component\>'
Bram Moolenaar280f1262006-01-30 00:14:18 +0000363 return indent(pn)
364 endif
Bram Moolenaar51156d52006-01-26 22:17:47 +0000365 endif
366 endif
367 let pn = prevnonblank(pn - 1)
368 let ps = getline(pn)
369 endwhile
Bram Moolenaar280f1262006-01-30 00:14:18 +0000370 return ind - &sw
Bram Moolenaar51156d52006-01-26 22:17:47 +0000371 endif
372
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000373 " indent: -sw
374 " keyword: ")"
375 " where: start of current line
376 if curs =~ '^\s*)'
377 return ind - &sw
378 endif
379
Bram Moolenaar51156d52006-01-26 22:17:47 +0000380 " indent: 0
381 " keywords: "end" + "architecture", "configuration", "entity", "package"
382 " where: start of current line
383 if curs =~? '^\s*end\s\+\%(architecture\|configuration\|entity\|package\)\>'
384 return 0
385 endif
386
387 " indent: -sw
388 " keywords: "end" + identifier
389 " where: start of current line
Bram Moolenaar9964e462007-05-05 17:54:07 +0000390 "if curs =~? '^\s*end\s\+\w\+\>'
391 if curs =~? '^\s*end\s'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000392 return ind - &sw
393 endif
394
395 " ****************************************************************************************
Bram Moolenaar280f1262006-01-30 00:14:18 +0000396 " indent: maintain indent of previous opening statement
Bram Moolenaar51156d52006-01-26 22:17:47 +0000397 " keywords: without "generic", "map", "port" + ":" but not ":=" + "in", "out", "inout", "buffer", "linkage", variable & ":="
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000398 " where: start of current line
399 if curs =~? '^\s*\%(\<\%(generic\|map\|port\)\>.*\)\@<!\S\+\s*:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\w\+\s\+:=\)'
Bram Moolenaar51156d52006-01-26 22:17:47 +0000400 return ind2
401 endif
402
403 " return leftover filtered indent
404 return ind
405endfunction