robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
37596fcd29
master
1" Description: Comshare Dimension Definition Language (CDL) 2" Maintainer: Raul Segura Acevedo <raulseguraaceved@netscape.net> (Invalid email address) 3" Doug Kearns <dougkearns@gmail.com> 4" Last Change: 2022 Apr 06 5" 2026 May 04 by Vim Project: fix typo 6 7if exists ( "b:did_indent" ) 8"finish 9endif 10let b: did_indent = 1 11 12setlocal indentexpr = CdlGetIndent (v:lnum) 13setlocal indentkeys & 14setlocal indentkeys += =~else,=~endif,=~then,;,),= 15 16let b: undo_indent = "setl inde< indk<" 17 18" Only define the function once. 19if exists ( "*CdlGetIndent" ) 20"finish 21endif 22 23" find out if an "...=..." expression is an assignment (or a conditional) 24" it scans 'line' first, and then the previous lines 25fun ! CdlAssignment ( lnum , line ) 26let f = -1 27let lnum = a: lnum 28let line = a: line 29while lnum > 0 && f == -1 30" line without members [a] of [b]:[c]... 31let inicio = 0 32while 1 33" keywords that help to decide 34let inicio = matchend ( line , '\c\<\(expr\|\a*if\|and\|or\|not\|else\|then\|memberis\|\k\+of\)\>\|[<>;]' , inicio ) 35if inicio < 0 36break 37endif 38" it's formula if there's a ';', 'elsE', 'theN', 'enDif' or 'expr' 39" conditional if there's a '<', '>', 'elseif', 'if', 'and', 'or', 'not', 40" 'memberis', 'childrenof' and other \k\+of functions 41let f = line [ inicio - 1 ] =~? '[en;]' || strpart ( line , inicio - 4 , 4 ) =~? 'ndif\|expr' 42endw 43let lnum = prevnonblank ( lnum - 1 ) 44let line = substitute ( getline ( lnum ), '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+' , ' ' , 'g' ) 45endw 46" if we hit the start of the file then f = -1, return 1 (formula) 47return f != 0 48endf 49 50fun ! CdlGetIndent ( lnum ) 51let thisline = getline ( a: lnum ) 52if match ( thisline , '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)' ) >= 0 53" it's an attributes line 54return shiftwidth () 55elseif match ( thisline , '^\c\s*\([{}]\|\/[*/]\|dimension\|schedule\|group\|hierarchy\|class\)' ) >= 0 56" it's a header or '{' or '}' or a comment 57return 0 58end 59 60let lnum = prevnonblank ( a: lnum - 1 ) 61" Hit the start of the file, use zero indent. 62if lnum == 0 63return 0 64endif 65 66" PREVIOUS LINE 67let ind = indent ( lnum ) 68let line = getline ( lnum ) 69 70" Whether a '=' is a conditional or an assignment. -1 means we don't know 71" yet. 72" One 'closing' element at the beginning of the line has already reduced the 73" indent, but 'else', 'elseif' & 'then' increment it for the next line. 74" '=' at the beginning already has the right indent (increased for 75" assignments). 76let f = -1 77let inicio = matchend ( line , '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)' ) 78if inicio > 0 79let c = line [ inicio - 1 ] 80" ')' and '=' don't change indent and are useless to set 'f' 81if c == '{' 82return shiftwidth () 83elseif c != ')' && c != '=' 84let f = 1 " all but 'elseif' are followed by a formula 85if c ==? 'n' || c ==? 'e' " 'then', 'else' 86let ind = ind + shiftwidth () 87elseif strpart ( line , inicio - 6 , 6 ) ==? 'elseif' " elseif, set f to conditional 88let ind = ind + shiftwidth () 89let f = 0 90end 91end 92end 93 94" remove members [a] of [b]:[c]... (inicio remains valid) 95let line = substitute ( line , '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+' , ' ' , 'g' ) 96while 1 97" search for the next interesting element 98let inicio = matchend ( line , '\c\<if\|endif\|[()=;]' , inicio ) 99if inicio < 0 100break 101end 102 103let c = line [ inicio - 1 ] 104" 'expr(...)' containing the formula 105if strpart ( line , inicio - 5 , 5 ) ==? 'expr(' 106let ind = 0 107let f = 1 108elseif c == ')' || c == ';' || strpart ( line , inicio - 5 , 5 ) ==? 'endif' 109let ind = ind - shiftwidth () 110elseif c == '(' || c ==? 'f' " '(' or 'if' 111let ind = ind + shiftwidth () 112else " c == '=' 113" if it is an assignment increase indent 114if f == -1 " we don't know yet, find out 115let f = CdlAssignment ( lnum , strpart ( line , 0 , inicio )) 116end 117if f == 1 " formula increase it 118let ind = ind + shiftwidth () 119end 120end 121endw 122 123" CURRENT LINE, if it starts with a closing element, decrease indent 124" or if it starts with '=' (assignment), increase indent 125if match ( thisline , '^\c\s*\(else\|then\|endif\|[);]\)' ) >= 0 126let ind = ind - shiftwidth () 127elseif match ( thisline , '^\s*=' ) >= 0 128if f == -1 " we don't know yet if is an assignment, find out 129let f = CdlAssignment ( lnum , "" ) 130end 131if f == 1 " formula increase it 132let ind = ind + shiftwidth () 133end 134end 135 136return ind 137endfun