robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ae7ab90159
master
1" VHDL indent ('93 syntax) 2" Language: VHDL 3" Maintainer: Gerald Lai <laigera+vim?gmail.com> 4" Version: 1.62 5" Last Change: 2017 Oct 17 6" 2023 Aug 28 by Vim Project (undo_indent) 7" URL: http://www.vim.org/scripts/script.php?script_id=1450 8 9" only load this indent file when no other was loaded 10if exists ( "b:did_indent" ) 11finish 12endif 13let b: did_indent = 1 14 15" setup indent options for local VHDL buffer 16setlocal indentexpr = GetVHDLindent () 17setlocal indentkeys = !^F,o,O,0(,0) 18setlocal indentkeys += =~begin,=~end\ ,=~end\ ,=~is,=~select,=~when 19setlocal indentkeys += =~if,=~then,=~elsif,=~else 20setlocal indentkeys += =~case,=~loop,=~for,=~generate,=~record,=~units,=~process,=~block,=~function,=~component,=~procedure 21setlocal indentkeys += =~architecture,=~configuration,=~entity,=~package 22 23let b: undo_indent = "setlocal indentexpr< indentkeys<" 24 25" constants 26" not a comment 27let s: NC = '\%(--.*\)\@<!' 28" end of string 29let s: ES = '\s*\%(--.*\)\=$' 30" no "end" keyword in front 31let s: NE = '\%(\<end\s\+\)\@<!' 32 33" option to disable alignment of generic/port mappings 34if ! exists ( "g:vhdl_indent_genportmap" ) 35let g: vhdl_indent_genportmap = 1 36endif 37 38" option to disable alignment of right-hand side assignment "<=" statements 39if ! exists ( "g:vhdl_indent_rhsassign" ) 40let g: vhdl_indent_rhsassign = 1 41endif 42 43" only define indent function once 44if exists ( "*GetVHDLindent" ) 45finish 46endif 47 48function GetVHDLindent () 49" store current line & string 50let curn = v: lnum 51let curs = getline ( curn ) 52 53" find previous line that is not a comment 54let prevn = prevnonblank ( curn - 1 ) 55let prevs = getline ( prevn ) 56while prevn > 0 && prevs =~ '^\s*--' 57let prevn = prevnonblank ( prevn - 1 ) 58let prevs = getline ( prevn ) 59endwhile 60let prevs_noi = substitute ( prevs , '^\s*' , '' , '' ) 61 62" default indent starts as previous non-comment line's indent 63let ind = prevn > 0 ? indent ( prevn ) : 0 64" backup default 65let ind2 = ind 66 67" indent: special; kill string so it would not affect other filters 68" keywords: "report" + string 69" where: anywhere in current or previous line 70let s0 = s: NC . '\<report\>\s*".*"' 71if curs =~? s0 72let curs = "" 73endif 74if prevs =~? s0 75let prevs = "" 76endif 77 78" indent: previous line's comment position, otherwise follow next non-comment line if possible 79" keyword: "--" 80" where: start of current line 81if curs =~ '^\s*--' 82let pn = curn - 1 83let ps = getline ( pn ) 84if curs =~ '^\s*--\s' && ps =~ '--' 85return indent ( pn ) + stridx ( substitute ( ps , '^\s*' , '' , '' ), '--' ) 86else 87" find nextnonblank line that is not a comment 88let nn = nextnonblank ( curn + 1 ) 89let ns = getline ( nn ) 90while nn > 0 && ns =~ '^\s*--' 91let nn = nextnonblank ( nn + 1 ) 92let ns = getline ( nn ) 93endwhile 94let n = indent ( nn ) 95return n != -1 ? n : ind 96endif 97endif 98 99" **************************************************************************************** 100" indent: align generic variables & port names 101" keywords: "procedure" + name, "generic", "map", "port" + "(", provided current line is part of mapping 102" where: anywhere in previous 2 lines 103" find following previous non-comment line 104let pn = prevnonblank ( prevn - 1 ) 105let ps = getline ( pn ) 106while pn > 0 && ps =~ '^\s*--' 107let pn = prevnonblank ( pn - 1 ) 108let ps = getline ( pn ) 109endwhile 110if ( 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 "(" 112if curs =~ '^\s*)' 113return ind2 + stridx ( prevs_noi , '(' ) 114endif 115let m = matchend ( prevs_noi , '(\s*\ze\w' ) 116if m != -1 117return ind2 + m 118else 119if g: vhdl_indent_genportmap 120return ind2 + stridx ( prevs_noi , '(' ) + shiftwidth () 121else 122return ind2 + shiftwidth () 123endif 124endif 125endif 126 127" indent: align conditional/select statement 128" keywords: variable + "<=" without ";" ending 129" where: start of previous line 130if prevs =~? '^\s*\S\+\s*<=[^;]*' . s: ES 131if g: vhdl_indent_rhsassign 132return ind2 + matchend ( prevs_noi , '<=\s*\ze.' ) 133else 134return ind2 + shiftwidth () 135endif 136endif 137 138" indent: backtrace previous non-comment lines for next smaller or equal size indent 139" keywords: "end" + "record", "units" 140" where: start of previous line 141" keyword: ")" 142" where: start of previous line 143" keyword: without "<=" + ";" ending 144" where: anywhere in previous line 145" keyword: "=>" + ")" ending, provided current line does not begin with ")" 146" where: anywhere in previous line 147" _note_: indent allowed to leave this filter 148let m = 0 149if prevs =~? '^\s*end\s\+\%(record\|units\)\>' 150let m = 3 151elseif prevs =~ '^\s*)' 152let m = 1 153elseif prevs =~ s: NC . '\%(<=.*\)\@<!;' . s: ES || ( curs !~ '^\s*)' && prevs =~ s: NC . '=>.*' . s: NC . ')' . s: ES ) 154let m = 2 155endif 156 157if m > 0 158let pn = prevnonblank ( prevn - 1 ) 159let ps = getline ( pn ) 160while pn > 0 161let t = indent ( pn ) 162if ps !~ '^\s*--' && ( t < ind || ( t == ind && m == 3 )) 163" make sure one of these is true 164" keywords: variable + "<=" without ";" ending 165" where: start of previous non-comment line 166" keywords: "procedure", "generic", "map", "port" 167" where: anywhere in previous non-comment line 168" keyword: "(" 169" where: start of previous non-comment line 170if m < 3 && ps !~? '^\s*\S\+\s*<=[^;]*' . s: ES 171if ps =~? s: NC . '\<\%(procedure\|generic\|map\|port\)\>' || ps =~ '^\s*(' 172let ind = t 173endif 174break 175endif 176let ind = t 177if m > 1 178" find following previous non-comment line 179let ppn = prevnonblank ( pn - 1 ) 180let pps = getline ( ppn ) 181while ppn > 0 && pps =~ '^\s*--' 182let ppn = prevnonblank ( ppn - 1 ) 183let pps = getline ( ppn ) 184endwhile 185" indent: follow 186" keyword: "select" 187" where: end of following previous non-comment line 188" keyword: "type" 189" where: start of following previous non-comment line 190if m == 2 191let s1 = s: NC . '\<select' . s: ES 192if ps !~? s1 && pps =~? s1 193let ind = indent ( ppn ) 194endif 195elseif m == 3 196let s1 = '^\s*type\>' 197if ps !~? s1 && pps =~? s1 198let ind = indent ( ppn ) 199endif 200endif 201endif 202break 203endif 204let pn = prevnonblank ( pn - 1 ) 205let ps = getline ( pn ) 206endwhile 207endif 208 209" indent: follow indent of previous opening statement, otherwise -sw 210" keyword: "begin" 211" where: anywhere in current line 212if curs =~? s: NC . '\<begin\>' 213" find previous opening statement of 214" keywords: "architecture", "block", "entity", "function", "generate", "procedure", "process" 215let s2 = s: NC . s: NE . '\<\%(architecture\|block\|entity\|function\|generate\|procedure\|process\)\>' 216 217let pn = prevnonblank ( curn - 1 ) 218let ps = getline ( pn ) 219while pn > 0 && ( ps =~ '^\s*--' || ps !~? s2 ) 220let pn = prevnonblank ( pn - 1 ) 221let ps = getline ( pn ) 222 223if ( ps =~? s: NC . '\<begin\>' ) 224return indent ( pn ) - shiftwidth () 225endif 226endwhile 227 228if ( pn == 0 ) 229return ind - shiftwidth () 230else 231return indent ( pn ) 232endif 233endif 234 235" indent: +sw if previous line is previous opening statement 236" keywords: "record", "units" 237" where: anywhere in current line 238if curs =~? s: NC . s: NE . '\<\%(record\|units\)\>' 239" find previous opening statement of 240" keyword: "type" 241let s3 = s: NC . s: NE . '\<type\>' 242if curs !~? s3 . '.*' . s: NC . '\<\%(record\|units\)\>.*' . s: ES && prevs =~? s3 243let ind = ind + shiftwidth () 244endif 245return ind 246endif 247 248" **************************************************************************************** 249" indent: 0 250" keywords: "architecture", "configuration", "entity", "library", "package" 251" where: start of current line 252if curs =~? '^\s*\%(architecture\|configuration\|entity\|library\|package\)\>' 253return 0 254endif 255 256" indent: maintain indent of previous opening statement 257" keyword: "is" 258" where: start of current line 259" find previous opening statement of 260" keywords: "architecture", "block", "configuration", "entity", "function", "package", "procedure", "process", "type" 261if curs =~? '^\s*\<is\>' && prevs =~? s: NC . s: NE . '\<\%(architecture\|block\|configuration\|entity\|function\|package\|procedure\|process\|type\)\>' 262return ind2 263endif 264 265" indent: maintain indent of previous opening statement 266" keyword: "then" 267" where: start of current line 268" find previous opening statement of 269" keywords: "elsif", "if" 270if curs =~? '^\s*\<then\>' && prevs =~? s: NC . '\%(\<elsif\>\|' . s: NE . '\<if\>\)' 271return ind2 272endif 273 274" indent: maintain indent of previous opening statement 275" keyword: "generate" 276" where: start of current line 277" find previous opening statement of 278" keywords: "for", "if" 279if curs =~? '^\s*\<generate\>' && prevs =~? s: NC . s: NE . '\%(\%(\<wait\s\+\)\@<!\<for\|\<if\)\>' 280return ind2 281endif 282 283" indent: +sw 284" keywords: "block", "process" 285" removed: "begin", "case", "elsif", "if", "loop", "record", "units", "while" 286" where: anywhere in previous line 287if prevs =~? s: NC . s: NE . '\<\%(block\|process\)\>' 288return ind + shiftwidth () 289endif 290 291" indent: +sw 292" keywords: "architecture", "configuration", "entity", "package" 293" removed: "component", "for", "when", "with" 294" where: start of previous line 295if prevs =~? '^\s*\%(architecture\|configuration\|entity\|package\)\>' 296return ind + shiftwidth () 297endif 298 299" indent: +sw 300" keyword: "select" 301" removed: "generate", "is", "=>" 302" where: end of previous line 303if prevs =~? s: NC . '\<select' . s: ES 304return ind + shiftwidth () 305endif 306 307" indent: +sw 308" keyword: "begin", "loop", "record", "units" 309" where: anywhere in previous line 310" keyword: "component", "else", "for" 311" where: start of previous line 312" keyword: "generate", "is", "then", "=>" 313" where: end of previous line 314" _note_: indent allowed to leave this filter 315if prevs =~? s: NC . '\%(\<begin\>\|' . s: NE . '\<\%(loop\|record\|units\)\>\)' || prevs =~? '^\s*\%(component\|else\|for\)\>' || prevs =~? s: NC . '\%(' . s: NE . '\<generate\|\<\%(is\|then\)\|=>\)' . s: ES 316let ind = ind + shiftwidth () 317endif 318 319" **************************************************************************************** 320" indent: -sw 321" keywords: "when", provided previous line does not begin with "when", does not end with "is" 322" where: start of current line 323let s4 = '^\s*when\>' 324if curs =~? s4 325if prevs =~? s: NC . '\<is' . s: ES 326return ind 327elseif prevs !~? s4 328return ind - shiftwidth () 329else 330return ind2 331endif 332endif 333 334" indent: -sw 335" keywords: "else", "elsif", "end" + "block", "for", "function", "generate", "if", "loop", "procedure", "process", "record", "units" 336" where: start of current line 337let s5 = 'block\|for\|function\|generate\|if\|loop\|procedure\|process\|record\|units' 338if curs =~? '^\s*\%(else\|elsif\|end\s\+\%(' . s5 . '\)\)\>' 339if prevs =~? '^\s*\%(elsif\|' . s5 . '\)' 340return ind 341else 342return ind - shiftwidth () 343endif 344endif 345 346" indent: backtrace previous non-comment lines 347" keyword: "end" + "case", "component" 348" where: start of current line 349let m = 0 350if curs =~? '^\s*end\s\+case\>' 351let m = 1 352elseif curs =~? '^\s*end\s\+component\>' 353let m = 2 354endif 355 356if m > 0 357" find following previous non-comment line 358let pn = prevn 359let ps = getline ( pn ) 360while pn > 0 361if ps !~ '^\s*--' 362"indent: -2sw 363"keywords: "end" + "case" 364"where: start of previous non-comment line 365"indent: -sw 366"keywords: "when" 367"where: start of previous non-comment line 368"indent: follow 369"keywords: "case" 370"where: start of previous non-comment line 371if m == 1 372if ps =~? '^\s*end\s\+case\>' 373return indent ( pn ) - 2 * shiftwidth () 374elseif ps =~? '^\s*when\>' 375return indent ( pn ) - shiftwidth () 376elseif ps =~? '^\s*case\>' 377return indent ( pn ) 378endif 379"indent: follow 380"keyword: "component" 381"where: start of previous non-comment line 382elseif m == 2 383if ps =~? '^\s*component\>' 384return indent ( pn ) 385endif 386endif 387endif 388let pn = prevnonblank ( pn - 1 ) 389let ps = getline ( pn ) 390endwhile 391return ind - shiftwidth () 392endif 393 394" indent: -sw 395" keyword: ")" 396" where: start of current line 397if curs =~ '^\s*)' 398return ind - shiftwidth () 399endif 400 401" indent: 0 402" keywords: "end" + "architecture", "configuration", "entity", "package" 403" where: start of current line 404if curs =~? '^\s*end\s\+\%(architecture\|configuration\|entity\|package\)\>' 405return 0 406endif 407 408" indent: -sw 409" keywords: "end" + identifier, ";" 410" where: start of current line 411"if curs =~? '^\s*end\s\+\w\+\>' 412if curs =~? '^\s*end\%(\s\|;' . s: ES . '\)' 413return ind - shiftwidth () 414endif 415 416" **************************************************************************************** 417" indent: maintain indent of previous opening statement 418" keywords: without "procedure", "generic", "map", "port" + ":" but not ":=" + "in", "out", "inout", "buffer", "linkage", variable & ":=" 419" where: start of current line 420if curs =~? '^\s*\%(\<\%(procedure\|generic\|map\|port\)\>.*\)\@<!\w\+\s*\w*\s*:[^=]\@=\s*\%(\%(in\|out\|inout\|buffer\|linkage\)\>\|\w\+\s\+:=\)' 421return ind2 422endif 423 424" **************************************************************************************** 425" indent: maintain indent of previous opening statement, corner case which 426" does not end in ;, but is part of a mapping 427" keywords: without "procedure", "generic", "map", "port" + ":" but not ":=", never + ;$ and 428" prevline without "procedure", "generic", "map", "port" + ":" but not ":=" + eventually ;$ 429" where: start of current line 430if curs =~? '^\s*\%(\<\%(procedure\|generic\|map\|port\)\>.*\)\@<!\w\+\s*\w*\s*:[^=].*[^;].*$' 431if prevs =~? '^\s*\%(\<\%(procedure\|generic\|map\|port\)\>.*\)\@<!\w\+\s*\w*\s*:[^=].*;.*$' 432return ind2 433endif 434endif 435 436" return leftover filtered indent 437return ind 438endfunction