robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0c2a7aa5f7
master
1" Vim indent file 2" Language: MATLAB 3" Maintainer: Axel Forsman <axelsfor@gmail.com> 4" Previous maintainer: Christophe Poucet <christophe.poucet@pandora.be> 5" Last Update: 2021-10-01 6 7" Only load if no other indent file is loaded 8if exists ( 'b:did_indent' ) |finish |endif 9let b: did_indent = 1 10 11setlocal indentexpr = GetMatlabIndent () 12setlocal indentkeys = !,o,O,e,0=end,0=elseif,0=case,0=otherwise,0=catch,0=function,0=elsei 13let b: undo_indent = "setlocal indentexpr< indentkeys<" 14 15" The value of the Function indenting format in 16" MATLAB Editor/Debugger Language Preferences. 17" The possible values are 0 for Classic, 1 for Indent nested functions 18" and 2 for Indent all functions (default). 19let b: MATLAB_function_indent = get ( g:, 'MATLAB_function_indent' , 2 ) 20" The previous value of b:changedtick 21let b: MATLAB_lasttick = -1 22" The previously indented line 23let b: MATLAB_lastline = -1 24" Whether the line above was a line continuation 25let b: MATLAB_waslc = 0 26let b: MATLAB_bracketlevel = 0 27 28" Only define the function once 29if exists ( "*GetMatlabIndent" ) |finish |endif 30 31let s: keepcpo = & cpo 32set cpo & vim 33 34let s: end = '\<end\>\%([^({]*[)}]\)\@!' " Array indexing heuristic 35let s: open_pat = 'for\|if\|parfor\|spmd\|switch\|try\|while\|classdef\|properties\|methods\|events\|enumeration' 36let s: dedent_pat = '\C^\s*\zs\<\%(end\|else\|elseif\|catch\|\(case\|otherwise\|function\)\)\>' 37let s: start_pat = '\C\<\%(function\|' . s: open_pat . '\)\>' 38let s: bracket_pair_pat = '\(\[\|{\)\|\(\]\|}\)' 39let s: zflag = has ( 'patch-7.4.984' ) ? 'z' : '' 40 41" Returns whether a comment or string envelops the specified column. 42function ! s: IsCommentOrString ( lnum , col ) 43return synIDattr ( synID ( a: lnum , a: col , 1 ), "name" ) =~# 'matlabComment\|matlabMultilineComment\|matlabCellComment\|matlabString' 44endfunction 45 46" Returns whether the specified line continues on the next line. 47function ! s: IsLineContinuation ( lnum ) 48let l = getline ( a: lnum ) |let c = -3 49while 1 50let c = match ( l , '\.\{3}' , c + 3 ) 51if c == -1 |return 0 52elseif ! s: IsCommentOrString ( a: lnum , c ) |return 1 |endif 53endwhile 54endfunction 55 56function ! s: SubmatchCount ( lnum , pattern , ...) 57let endcol = a: 0 >= 1 ? a: 1 : 1 / 0 |let x = [ 0 , 0 , 0 , 0 ] 58call cursor ( a: lnum , 1 ) 59while 1 60let [ lnum , c , submatch ] = searchpos ( a: pattern , 'cpe' . s: zflag , a: lnum ) 61if ! submatch || c >= endcol |break |endif 62if ! s: IsCommentOrString ( lnum , c ) |let x [ submatch - 2 ] += 1 |endif 63if cursor ( 0 , c + 1 ) == -1 || col ( '.' ) == c |break |endif 64endwhile 65return x 66endfunction 67 68function ! s: GetOpenCloseCount ( lnum , pattern , ...) 69let counts = call ( 's:SubmatchCount' , [ a: lnum , a: pattern ] + a: 000 ) 70return counts [ 0 ] - counts [ 1 ] 71endfunction 72 73function ! GetMatlabIndent () 74let prevlnum = prevnonblank ( v: lnum - 1 ) 75 76if b: MATLAB_lasttick != b: changedtick || b: MATLAB_lastline != prevlnum 77" Recalculate bracket count (only have to check same block and line above) 78let b: MATLAB_bracketlevel = 0 79let previndent = indent ( prevlnum ) |let l = prevlnum 80while 1 81let l = prevnonblank ( l - 1 ) |let indent = indent ( l ) 82if l <= 0 || previndent < indent |break |endif 83let b: MATLAB_bracketlevel += s: GetOpenCloseCount ( l , s: bracket_pair_pat ) 84if previndent != indent |break |endif 85endwhile 86 87let b: MATLAB_waslc = s: IsLineContinuation ( prevlnum - 1 ) 88endif 89" If line above was blank it can impossibly have been a LC 90let above_lc = b: MATLAB_lasttick == b: changedtick && prevlnum != v: lnum - 1 && b: MATLAB_lastline == prevlnum ? 0 : s: IsLineContinuation ( v: lnum - 1 ) 91 92let pair_pat = '\C\<\(' . s: open_pat . '\|' 93 \. ( b: MATLAB_function_indent == 1 ? '^\@<!' : '' ) 94 \. ( b: MATLAB_function_indent >= 1 ? 'function\|' : '' ) 95 \. '\|\%(^\s*\)\@<=\%(else\|elseif\|case\|otherwise\|catch\)\)\>' 96 \. '\|\S\s*\zs\(' . s: end . '\)' 97let [ open , close , b_open , b_close ] = prevlnum ? s: SubmatchCount ( prevlnum , 98 \pair_pat . '\|' . s: bracket_pair_pat ) : [ 0 , 0 , 0 , 0 ] 99let curbracketlevel = b: MATLAB_bracketlevel + b_open - b_close 100 101call cursor ( v: lnum , 1 ) 102let submatch = search ( s: dedent_pat , 'cp' . s: zflag , v: lnum ) 103if submatch && ! s: IsCommentOrString ( v: lnum , col ( '.' )) 104" Align end, et cetera with start of block 105let [ lnum , col ] = searchpairpos ( s: start_pat , '' , '\C' . s: end , 'bW' , 's:IsCommentOrString(line("."), col("."))' ) 106let result = lnum ? indent ( lnum ) + shiftwidth () * ( s: GetOpenCloseCount ( lnum , pair_pat , col ) + submatch == 2 ) : 0 107else 108" Count how many blocks the previous line opens/closes 109" Line continuations/brackets indent once per statement 110let result = ( prevlnum > 0 ) * indent ( prevlnum ) + shiftwidth () * ( open - close 111 \+ ( b: MATLAB_bracketlevel ? -! curbracketlevel : !! curbracketlevel ) 112 \+ ( curbracketlevel <= 0 ) * ( above_lc - b: MATLAB_waslc )) 113endif 114 115let b: MATLAB_waslc = above_lc 116let b: MATLAB_bracketlevel = curbracketlevel 117let b: MATLAB_lasttick = b: changedtick 118let b: MATLAB_lastline = v: lnum 119return result 120endfunction 121 122let & cpo = s: keepcpo 123unlet s: keepcpo