robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3222f0ad00
master
1" ABB Rapid Command indent file for Vim 2" Language: ABB Rapid Command 3" Maintainer: Patrick Meiser-Knosowski <knosowski@graeffrobotics.de> 4" Version: 2.2.7 5" Last Change: 12. May 2023 6" Credits: Based on indent/vim.vim 7" 8" Suggestions of improvement are very welcome. Please email me! 9" 10" Known bugs: ../doc/rapid.txt 11" 12" TODO 13" * indent wrapped lines which do not end with an ; or special key word, 14" maybe this is a better idea, but then () and [] has to be changed as 15" well 16" 17 18if exists ( "g:rapidNoSpaceIndent" ) 19if ! exists ( "g:rapidSpaceIndent" ) 20let g: rapidSpaceIndent = ! g: rapidNoSpaceIndent 21endif 22unlet g: rapidNoSpaceIndent 23endif 24 25" Only load this indent file when no other was loaded. 26if exists ( "b:did_indent" ) || get ( g:, 'rapidNoIndent' , 0 ) 27finish 28endif 29let b: did_indent = 1 30 31setlocal nolisp 32setlocal nosmartindent 33setlocal autoindent 34setlocal indentexpr = GetRapidIndent () 35if get ( g:, 'rapidNewStyleIndent' , 0 ) 36setlocal indentkeys = !^F,o,O,0=~endmodule,0=~error,0=~undo,0=~backward,0=~endproc,0=~endrecord,0=~endtrap,0=~endfunc,0=~else,0=~endif,0=~endtest,0=~endfor,0=~endwhile,:,<[>,<]>,<(>,<)> 37else 38setlocal indentkeys = !^F,o,O,0=~endmodule,0=~error,0=~undo,0=~backward,0=~endproc,0=~endrecord,0=~endtrap,0=~endfunc,0=~else,0=~endif,0=~endtest,0=~endfor,0=~endwhile,: 39endif 40let b: undo_indent = "setlocal lisp< si< ai< inde< indk<" 41 42if get ( g:, 'rapidSpaceIndent' , 1 ) 43" Use spaces for indention, 2 is enough. 44" More or even tabs wastes space on the teach pendant. 45setlocal softtabstop = 2 46setlocal shiftwidth = 2 47setlocal expandtab 48setlocal shiftround 49let b: undo_indent = b: undo_indent . " sts< sw< et< sr<" 50endif 51 52" Only define the function once. 53if exists ( "*GetRapidIndent" ) 54finish 55endif 56 57let s: keepcpo = & cpo 58set cpo & vim 59 60function GetRapidIndent () 61let ignorecase_save = & ignorecase 62try 63let & ignorecase = 0 64return s: GetRapidIndentIntern () 65finally 66let & ignorecase = ignorecase_save 67endtry 68endfunction 69 70function s: GetRapidIndentIntern () abort 71 72let l: currentLineNum = v: lnum 73let l: currentLine = getline ( l: currentLineNum ) 74 75if l: currentLine =~ '^!' && ! get ( g:, 'rapidCommentIndent' , 0 ) 76" If current line is ! line comment, do not change indent 77" This may be useful if code is commented out at the first column. 78return 0 79endif 80 81" Find a non-blank line above the current line. 82let l: preNoneBlankLineNum = s: RapidPreNoneBlank ( v: lnum - 1 ) 83if l: preNoneBlankLineNum == 0 84" At the start of the file use zero indent. 85return 0 86endif 87 88let l: preNoneBlankLine = getline ( l: preNoneBlankLineNum ) 89let l: ind = indent ( l: preNoneBlankLineNum ) 90 91" Define add a 'shiftwidth' pattern 92let l: addShiftwidthPattern = '\c\v^\s*(' 93let l: addShiftwidthPattern .= '((local|task)\s+)?(module|record|proc|func|trap)\s+\k' 94let l: addShiftwidthPattern .= '|(backward|error|undo)>' 95let l: addShiftwidthPattern .= ')' 96" 97" Define Subtract 'shiftwidth' pattern 98let l: subtractShiftwidthPattern = '\c\v^\s*(' 99let l: subtractShiftwidthPattern .= 'end(module|record|proc|func|trap)>' 100let l: subtractShiftwidthPattern .= '|(backward|error|undo)>' 101let l: subtractShiftwidthPattern .= ')' 102 103" Add shiftwidth 104if l: preNoneBlankLine =~ l: addShiftwidthPattern 105 \|| s: RapidLenTilStr ( l: preNoneBlankLineNum , "then" , 0 ) >= 0 106 \|| s: RapidLenTilStr ( l: preNoneBlankLineNum , "else" , 0 ) >= 0 107 \|| s: RapidLenTilStr ( l: preNoneBlankLineNum , "do" , 0 ) >= 0 108 \|| s: RapidLenTilStr ( l: preNoneBlankLineNum , "case" , 0 ) >= 0 109 \|| s: RapidLenTilStr ( l: preNoneBlankLineNum , "default" , 0 ) >= 0 110let l: ind += & sw 111endif 112 113" Subtract shiftwidth 114if l: currentLine =~ l: subtractShiftwidthPattern 115 \|| s: RapidLenTilStr ( l: currentLineNum , "endif" , 0 ) >= 0 116 \|| s: RapidLenTilStr ( l: currentLineNum , "endfor" , 0 ) >= 0 117 \|| s: RapidLenTilStr ( l: currentLineNum , "endwhile" , 0 ) >= 0 118 \|| s: RapidLenTilStr ( l: currentLineNum , "endtest" , 0 ) >= 0 119 \|| s: RapidLenTilStr ( l: currentLineNum , "else" , 0 ) >= 0 120 \|| s: RapidLenTilStr ( l: currentLineNum , "elseif" , 0 ) >= 0 121 \|| s: RapidLenTilStr ( l: currentLineNum , "case" , 0 ) >= 0 122 \|| s: RapidLenTilStr ( l: currentLineNum , "default" , 0 ) >= 0 123let l: ind = l: ind - & sw 124endif 125 126" First case (or default) after a test gets the indent of the test. 127if ( s: RapidLenTilStr ( l: currentLineNum , "case" , 0 ) >= 0 || s: RapidLenTilStr ( l: currentLineNum , "default" , 0 ) >= 0 ) && s: RapidLenTilStr ( l: preNoneBlankLineNum , "test" , 0 ) >= 0 128let l: ind += & sw 129endif 130 131" continued lines with () or [] 132let l: OpenSum = s: RapidLoneParen ( l: preNoneBlankLineNum , "(" ) + s: RapidLoneParen ( l: preNoneBlankLineNum , "[" ) 133if get ( g:, 'rapidNewStyleIndent' , 0 ) 134let l: CloseSum = s: RapidLoneParen ( l: preNoneBlankLineNum , ")" ) + s: RapidLoneParen ( l: currentLineNum , "]" ) 135else 136let l: CloseSum = s: RapidLoneParen ( l: preNoneBlankLineNum , ")" ) + s: RapidLoneParen ( l: preNoneBlankLineNum , "]" ) 137endif 138if l: OpenSum > l: CloseSum 139let l: ind += ( l: OpenSum * 4 * & sw ) 140elseif l: OpenSum < l: CloseSum 141let l: ind -= ( l: CloseSum * 4 * & sw ) 142endif 143 144return l: ind 145endfunction 146 147" Returns the length of the line until a:str occur outside a string or 148" comment. Search starts at string index a:startIdx. 149" If a:str is a word also add word boundaries and case insensitivity. 150" Note: rapidTodoComment and rapidDebugComment are not taken into account. 151function s: RapidLenTilStr ( lnum , str , startIdx ) abort 152 153let l: line = getline ( a: lnum ) 154let l: len = strlen ( l: line ) 155let l: idx = a: startIdx 156let l: str = a: str 157if l: str =~ '^\k\+$' 158let l: str = '\c\<' . l: str . '\>' 159endif 160 161while l: len > l: idx 162let l: idx = match ( l: line , l: str , l: idx ) 163if l: idx < 0 164" a:str not found 165return -1 166endif 167let l: synName = synIDattr ( synID ( a: lnum , l: idx + 1 , 0 ), "name" ) 168if l: synName != "rapidString" 169 \&&l: synName != "rapidConcealableString" 170 \&& (l: synName != "rapidComment" || l: str =~ '^!' ) 171" a:str found outside string or line comment 172return l: idx 173endif 174" a:str is part of string or line comment 175let l: idx += 1 " continue search for a:str 176endwhile 177 178" a:str not found or l:len <= a:startIdx 179return -1 180endfunction 181 182" a:lchar should be one of (, ), [, ], { or } 183" returns the number of opening/closing parentheses which have no 184" closing/opening match in getline(a:lnum) 185function s: RapidLoneParen ( lnum , lchar ) abort 186if a: lchar == "(" || a: lchar == ")" 187let l: opnParChar = "(" 188let l: clsParChar = ")" 189elseif a: lchar == "[" || a: lchar == "]" 190let l: opnParChar = "[" 191let l: clsParChar = "]" 192elseif a: lchar == "{" || a: lchar == "}" 193let l: opnParChar = "{" 194let l: clsParChar = "}" 195else 196return 0 197endif 198 199let l: line = getline ( a: lnum ) 200 201" look for the first ! which is not part of a string 202let l: len = s: RapidLenTilStr ( a: lnum , "!" , 0 ) 203if l: len == 0 204return 0 " first char is !; ignored 205endif 206 207let l: opnParen = 0 208" count opening brackets 209let l: i = 0 210while l: i >= 0 211let l: i = s: RapidLenTilStr ( a: lnum , l: opnParChar , l: i ) 212if l: i >= 0 213let l: opnParen += 1 214let l: i += 1 215endif 216endwhile 217 218let l: clsParen = 0 219" count closing brackets 220let l: i = 0 221while l: i >= 0 222let l: i = s: RapidLenTilStr ( a: lnum , l: clsParChar , l: i ) 223if l: i >= 0 224let l: clsParen += 1 225let l: i += 1 226endif 227endwhile 228 229if ( a: lchar == "(" || a: lchar == "[" || a: lchar == "{" ) && l: opnParen > l: clsParen 230return ( l: opnParen - l: clsParen ) 231elseif ( a: lchar == ")" || a: lchar == "]" || a: lchar == "}" ) && l: clsParen > l: opnParen 232return ( l: clsParen - l: opnParen ) 233endif 234 235return 0 236endfunction 237 238" This function works almost like prevnonblank() but handles %%%-headers and 239" comments like blank lines 240function s: RapidPreNoneBlank ( lnum ) abort 241 242let nPreNoneBlank = prevnonblank ( a: lnum ) 243 244while nPreNoneBlank > 0 && getline ( nPreNoneBlank ) =~ '\v\c^\s*(\%\%\%|!)' 245" Previous none blank line irrelevant. Look further aback. 246let nPreNoneBlank = prevnonblank ( nPreNoneBlank - 1 ) 247endwhile 248 249return nPreNoneBlank 250endfunction 251 252let & cpo = s: keepcpo 253unlet s: keepcpo 254 255" vim:sw=2 sts=2 et