codekingpro/portable-devtools
114k
1" VHDL indent ('93 syntax)2" Language: VHDL3" Maintainer: Gerald Lai <laigera+vim?gmail.com>4" Version: 1.625" Last Change: 2017 Oct 176" 2023 Aug 28 by Vim Project (undo_indent)7" URL: http://www.vim.org/scripts/script.php?script_id=14508 9" only load this indent file when no other was loaded10if exists("b:did_indent")11 finish12endif13let b:did_indent = 114 15" setup indent options for local VHDL buffer16setlocal indentexpr=GetVHDLindent()17setlocal indentkeys=!^F,o,O,0(,0)18setlocal indentkeys+==~begin,=~end\ ,=~end\ ,=~is,=~select,=~when19setlocal indentkeys+==~if,=~then,=~elsif,=~else20setlocal indentkeys+==~case,=~loop,=~for,=~generate,=~record,=~units,=~process,=~block,=~function,=~component,=~procedure21setlocal indentkeys+==~architecture,=~configuration,=~entity,=~package22 23let b:undo_indent = "setlocal indentexpr< indentkeys<"24 25" constants26" not a comment27let s:NC = '\%(--.*\)\@<!'28" end of string29let s:ES = '\s*\%(--.*\)\=$'30" no "end" keyword in front31let s:NE = '\%(\<end\s\+\)\@<!'32 33" option to disable alignment of generic/port mappings34if !exists("g:vhdl_indent_genportmap")35 let g:vhdl_indent_genportmap = 136endif37 38" option to disable alignment of right-hand side assignment "<=" statements39if !exists("g:vhdl_indent_rhsassign")40 let g:vhdl_indent_rhsassign = 141endif42 43" only define indent function once44if exists("*GetVHDLindent")45 finish46endif47 48function GetVHDLindent()49 " store current line & string50 let curn = v:lnum51 let curs = getline(curn)52 53 " find previous line that is not a comment54 let prevn = prevnonblank(curn - 1)55 let prevs = getline(prevn)56 while prevn > 0 && prevs =~ '^\s*--'57 let prevn = prevnonblank(prevn - 1)58 let prevs = getline(prevn)59 endwhile60 let prevs_noi = substitute(prevs, '^\s*', '', '')61 62 " default indent starts as previous non-comment line's indent63 let ind = prevn > 0 ? indent(prevn) : 064 " backup default65 let ind2 = ind66 67 " indent: special; kill string so it would not affect other filters68 " keywords: "report" + string69 " where: anywhere in current or previous line70 let s0 = s:NC.'\<report\>\s*".*"'71 if curs =~? s072 let curs = ""73 endif74 if prevs =~? s075 let prevs = ""76 endif77 78 " indent: previous line's comment position, otherwise follow next non-comment line if possible79 " keyword: "--"80 " where: start of current line81 if curs =~ '^\s*--'82 let pn = curn - 183 let ps = getline(pn)84 if curs =~ '^\s*--\s' && ps =~ '--'85 return indent(pn) + stridx(substitute(ps, '^\s*', '', ''), '--')86 else87 " find nextnonblank line that is not a comment88 let nn = nextnonblank(curn + 1)89 let ns = getline(nn)90 while nn > 0 && ns =~ '^\s*--'91 let nn = nextnonblank(nn + 1)92 let ns = getline(nn)93 endwhile94 let n = indent(nn)95 return n != -1 ? n : ind96 endif97 endif98 99 " ****************************************************************************************100 " indent: align generic variables & port names101 " keywords: "procedure" + name, "generic", "map", "port" + "(", provided current line is part of mapping102 " where: anywhere in previous 2 lines103 " find following previous non-comment line104 let pn = prevnonblank(prevn - 1)105 let ps = getline(pn)106 while pn > 0 && ps =~ '^\s*--'107 let pn = prevnonblank(pn - 1)108 let ps = getline(pn)109 endwhile110 if (curs =~ '^\s*)' || curs =~? '^\s*\%(\<\%(procedure\|generic\|map\|port\)\>.*\)\@<!\w\+\s*\w*\s*\((.*)\)*\s*\%(=>\s*\S\+\|:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\s\+\)\)') && (prevs =~? s:NC.'\<\%(procedure\s\+\S\+\|generic\|map\|port\)\s*(\%(\s*\w\)\=' || (ps =~? s:NC.'\<\%(procedure\|generic\|map\|port\)'.s:ES && prevs =~ '^\s*('))111 " align closing ")" with opening "("112 if curs =~ '^\s*)'113 return ind2 + stridx(prevs_noi, '(')114 endif115 let m = matchend(prevs_noi, '(\s*\ze\w')116 if m != -1117 return ind2 + m118 else119 if g:vhdl_indent_genportmap120 return ind2 + stridx(prevs_noi, '(') + shiftwidth()121 else122 return ind2 + shiftwidth()123 endif124 endif125 endif126 127 " indent: align conditional/select statement128 " keywords: variable + "<=" without ";" ending129 " where: start of previous line130 if prevs =~? '^\s*\S\+\s*<=[^;]*'.s:ES131 if g:vhdl_indent_rhsassign132 return ind2 + matchend(prevs_noi, '<=\s*\ze.')133 else134 return ind2 + shiftwidth()135 endif136 endif137 138 " indent: backtrace previous non-comment lines for next smaller or equal size indent139 " keywords: "end" + "record", "units"140 " where: start of previous line141 " keyword: ")"142 " where: start of previous line143 " keyword: without "<=" + ";" ending144 " where: anywhere in previous line145 " keyword: "=>" + ")" ending, provided current line does not begin with ")"146 " where: anywhere in previous line147 " _note_: indent allowed to leave this filter148 let m = 0149 if prevs =~? '^\s*end\s\+\%(record\|units\)\>'150 let m = 3151 elseif prevs =~ '^\s*)'152 let m = 1153 elseif prevs =~ s:NC.'\%(<=.*\)\@<!;'.s:ES || (curs !~ '^\s*)' && prevs =~ s:NC.'=>.*'.s:NC.')'.s:ES)154 let m = 2155 endif156 157 if m > 0158 let pn = prevnonblank(prevn - 1)159 let ps = getline(pn)160 while pn > 0161 let t = indent(pn)162 if ps !~ '^\s*--' && (t < ind || (t == ind && m == 3))163 " make sure one of these is true164 " keywords: variable + "<=" without ";" ending165 " where: start of previous non-comment line166 " keywords: "procedure", "generic", "map", "port"167 " where: anywhere in previous non-comment line168 " keyword: "("169 " where: start of previous non-comment line170 if m < 3 && ps !~? '^\s*\S\+\s*<=[^;]*'.s:ES171 if ps =~? s:NC.'\<\%(procedure\|generic\|map\|port\)\>' || ps =~ '^\s*('172 let ind = t173 endif174 break175 endif176 let ind = t177 if m > 1178 " find following previous non-comment line179 let ppn = prevnonblank(pn - 1)180 let pps = getline(ppn)181 while ppn > 0 && pps =~ '^\s*--'182 let ppn = prevnonblank(ppn - 1)183 let pps = getline(ppn)184 endwhile185 " indent: follow186 " keyword: "select"187 " where: end of following previous non-comment line188 " keyword: "type"189 " where: start of following previous non-comment line190 if m == 2191 let s1 = s:NC.'\<select'.s:ES192 if ps !~? s1 && pps =~? s1193 let ind = indent(ppn)194 endif195 elseif m == 3196 let s1 = '^\s*type\>'197 if ps !~? s1 && pps =~? s1198 let ind = indent(ppn)199 endif200 endif201 endif202 break203 endif204 let pn = prevnonblank(pn - 1)205 let ps = getline(pn)206 endwhile207 endif208 209 " indent: follow indent of previous opening statement, otherwise -sw210 " keyword: "begin"211 " where: anywhere in current line212 if curs =~? s:NC.'\<begin\>'213 " find previous opening statement of214 " keywords: "architecture", "block", "entity", "function", "generate", "procedure", "process"215 let s2 = s:NC.s:NE.'\<\%(architecture\|block\|entity\|function\|generate\|procedure\|process\)\>'216 217 let pn = prevnonblank(curn - 1)218 let ps = getline(pn)219 while pn > 0 && (ps =~ '^\s*--' || ps !~? s2)220 let pn = prevnonblank(pn - 1)221 let ps = getline(pn)222 223 if (ps =~? s:NC.'\<begin\>')224 return indent(pn) - shiftwidth()225 endif226 endwhile227 228 if (pn == 0)229 return ind - shiftwidth()230 else231 return indent(pn)232 endif233 endif234 235 " indent: +sw if previous line is previous opening statement236 " keywords: "record", "units"237 " where: anywhere in current line238 if curs =~? s:NC.s:NE.'\<\%(record\|units\)\>'239 " find previous opening statement of240 " keyword: "type"241 let s3 = s:NC.s:NE.'\<type\>'242 if curs !~? s3.'.*'.s:NC.'\<\%(record\|units\)\>.*'.s:ES && prevs =~? s3243 let ind = ind + shiftwidth()244 endif245 return ind246 endif247 248 " ****************************************************************************************249 " indent: 0250 " keywords: "architecture", "configuration", "entity", "library", "package"251 " where: start of current line252 if curs =~? '^\s*\%(architecture\|configuration\|entity\|library\|package\)\>'253 return 0254 endif255 256 " indent: maintain indent of previous opening statement257 " keyword: "is"258 " where: start of current line259 " find previous opening statement of260 " keywords: "architecture", "block", "configuration", "entity", "function", "package", "procedure", "process", "type"261 if curs =~? '^\s*\<is\>' && prevs =~? s:NC.s:NE.'\<\%(architecture\|block\|configuration\|entity\|function\|package\|procedure\|process\|type\)\>'262 return ind2263 endif264 265 " indent: maintain indent of previous opening statement266 " keyword: "then"267 " where: start of current line268 " find previous opening statement of269 " keywords: "elsif", "if"270 if curs =~? '^\s*\<then\>' && prevs =~? s:NC.'\%(\<elsif\>\|'.s:NE.'\<if\>\)'271 return ind2272 endif273 274 " indent: maintain indent of previous opening statement275 " keyword: "generate"276 " where: start of current line277 " find previous opening statement of278 " keywords: "for", "if"279 if curs =~? '^\s*\<generate\>' && prevs =~? s:NC.s:NE.'\%(\%(\<wait\s\+\)\@<!\<for\|\<if\)\>'280 return ind2281 endif282 283 " indent: +sw284 " keywords: "block", "process"285 " removed: "begin", "case", "elsif", "if", "loop", "record", "units", "while"286 " where: anywhere in previous line287 if prevs =~? s:NC.s:NE.'\<\%(block\|process\)\>'288 return ind + shiftwidth()289 endif290 291 " indent: +sw292 " keywords: "architecture", "configuration", "entity", "package"293 " removed: "component", "for", "when", "with"294 " where: start of previous line295 if prevs =~? '^\s*\%(architecture\|configuration\|entity\|package\)\>'296 return ind + shiftwidth()297 endif298 299 " indent: +sw300 " keyword: "select"301 " removed: "generate", "is", "=>"302 " where: end of previous line303 if prevs =~? s:NC.'\<select'.s:ES304 return ind + shiftwidth()305 endif306 307 " indent: +sw308 " keyword: "begin", "loop", "record", "units"309 " where: anywhere in previous line310 " keyword: "component", "else", "for"311 " where: start of previous line312 " keyword: "generate", "is", "then", "=>"313 " where: end of previous line314 " _note_: indent allowed to leave this filter315 if prevs =~? s:NC.'\%(\<begin\>\|'.s:NE.'\<\%(loop\|record\|units\)\>\)' || prevs =~? '^\s*\%(component\|else\|for\)\>' || prevs =~? s:NC.'\%('.s:NE.'\<generate\|\<\%(is\|then\)\|=>\)'.s:ES316 let ind = ind + shiftwidth()317 endif318 319 " ****************************************************************************************320 " indent: -sw321 " keywords: "when", provided previous line does not begin with "when", does not end with "is"322 " where: start of current line323 let s4 = '^\s*when\>'324 if curs =~? s4325 if prevs =~? s:NC.'\<is'.s:ES326 return ind327 elseif prevs !~? s4328 return ind - shiftwidth()329 else330 return ind2331 endif332 endif333 334 " indent: -sw335 " keywords: "else", "elsif", "end" + "block", "for", "function", "generate", "if", "loop", "procedure", "process", "record", "units"336 " where: start of current line337 let s5 = 'block\|for\|function\|generate\|if\|loop\|procedure\|process\|record\|units'338 if curs =~? '^\s*\%(else\|elsif\|end\s\+\%('.s5.'\)\)\>'339 if prevs =~? '^\s*\%(elsif\|'.s5.'\)'340 return ind341 else342 return ind - shiftwidth()343 endif344 endif345 346 " indent: backtrace previous non-comment lines347 " keyword: "end" + "case", "component"348 " where: start of current line349 let m = 0350 if curs =~? '^\s*end\s\+case\>'351 let m = 1352 elseif curs =~? '^\s*end\s\+component\>'353 let m = 2354 endif355 356 if m > 0357 " find following previous non-comment line358 let pn = prevn359 let ps = getline(pn)360 while pn > 0361 if ps !~ '^\s*--'362 "indent: -2sw363 "keywords: "end" + "case"364 "where: start of previous non-comment line365 "indent: -sw366 "keywords: "when"367 "where: start of previous non-comment line368 "indent: follow369 "keywords: "case"370 "where: start of previous non-comment line371 if m == 1372 if ps =~? '^\s*end\s\+case\>'373 return indent(pn) - 2 * shiftwidth()374 elseif ps =~? '^\s*when\>'375 return indent(pn) - shiftwidth()376 elseif ps =~? '^\s*case\>'377 return indent(pn)378 endif379 "indent: follow380 "keyword: "component"381 "where: start of previous non-comment line382 elseif m == 2383 if ps =~? '^\s*component\>'384 return indent(pn)385 endif386 endif387 endif388 let pn = prevnonblank(pn - 1)389 let ps = getline(pn)390 endwhile391 return ind - shiftwidth()392 endif393 394 " indent: -sw395 " keyword: ")"396 " where: start of current line397 if curs =~ '^\s*)'398 return ind - shiftwidth()399 endif400 401 " indent: 0402 " keywords: "end" + "architecture", "configuration", "entity", "package"403 " where: start of current line404 if curs =~? '^\s*end\s\+\%(architecture\|configuration\|entity\|package\)\>'405 return 0406 endif407 408 " indent: -sw409 " keywords: "end" + identifier, ";"410 " where: start of current line411 "if curs =~? '^\s*end\s\+\w\+\>'412 if curs =~? '^\s*end\%(\s\|;'.s:ES.'\)'413 return ind - shiftwidth()414 endif415 416 " ****************************************************************************************417 " indent: maintain indent of previous opening statement418 " keywords: without "procedure", "generic", "map", "port" + ":" but not ":=" + "in", "out", "inout", "buffer", "linkage", variable & ":="419 " where: start of current line420 if curs =~? '^\s*\%(\<\%(procedure\|generic\|map\|port\)\>.*\)\@<!\w\+\s*\w*\s*:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\w\+\s\+:=\)'421 return ind2422 endif423 424 " ****************************************************************************************425 " indent: maintain indent of previous opening statement, corner case which426 " does not end in ;, but is part of a mapping427 " keywords: without "procedure", "generic", "map", "port" + ":" but not ":=", never + ;$ and428 " prevline without "procedure", "generic", "map", "port" + ":" but not ":=" + eventually ;$429 " where: start of current line430 if curs =~? '^\s*\%(\<\%(procedure\|generic\|map\|port\)\>.*\)\@<!\w\+\s*\w*\s*:[^=].*[^;].*$'431 if prevs =~? '^\s*\%(\<\%(procedure\|generic\|map\|port\)\>.*\)\@<!\w\+\s*\w*\s*:[^=].*;.*$'432 return ind2433 endif434 endif435 436 " return leftover filtered indent437 return ind438endfunction439 