codekingpro/portable-devtools
114k
1" Vim indent file2" Language: MATLAB3" Maintainer: Axel Forsman <axelsfor@gmail.com>4" Previous maintainer: Christophe Poucet <christophe.poucet@pandora.be>5" Last Update: 2021-10-016 7" Only load if no other indent file is loaded8if exists('b:did_indent') | finish | endif9let b:did_indent = 110 11setlocal indentexpr=GetMatlabIndent()12setlocal indentkeys=!,o,O,e,0=end,0=elseif,0=case,0=otherwise,0=catch,0=function,0=elsei13let b:undo_indent = "setlocal indentexpr< indentkeys<"14 15" The value of the Function indenting format in16" MATLAB Editor/Debugger Language Preferences.17" The possible values are 0 for Classic, 1 for Indent nested functions18" and 2 for Indent all functions (default).19let b:MATLAB_function_indent = get(g:, 'MATLAB_function_indent', 2)20" The previous value of b:changedtick21let b:MATLAB_lasttick = -122" The previously indented line23let b:MATLAB_lastline = -124" Whether the line above was a line continuation25let b:MATLAB_waslc = 026let b:MATLAB_bracketlevel = 027 28" Only define the function once29if exists("*GetMatlabIndent") | finish | endif30 31let s:keepcpo = &cpo32set cpo&vim33 34let s:end = '\<end\>\%([^({]*[)}]\)\@!' " Array indexing heuristic35let 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)43 return synIDattr(synID(a:lnum, a:col, 1), "name") =~# 'matlabComment\|matlabMultilineComment\|matlabCellComment\|matlabString'44endfunction45 46" Returns whether the specified line continues on the next line.47function! s:IsLineContinuation(lnum)48 let l = getline(a:lnum) | let c = -349 while 150 let c = match(l, '\.\{3}', c + 3)51 if c == -1 | return 052 elseif !s:IsCommentOrString(a:lnum, c) | return 1 | endif53 endwhile54endfunction55 56function! s:SubmatchCount(lnum, pattern, ...)57 let endcol = a:0 >= 1 ? a:1 : 1 / 0 | let x = [0, 0, 0, 0]58 call cursor(a:lnum, 1)59 while 160 let [lnum, c, submatch] = searchpos(a:pattern, 'cpe' . s:zflag, a:lnum)61 if !submatch || c >= endcol | break | endif62 if !s:IsCommentOrString(lnum, c) | let x[submatch - 2] += 1 | endif63 if cursor(0, c + 1) == -1 || col('.') == c | break | endif64 endwhile65 return x66endfunction67 68function! s:GetOpenCloseCount(lnum, pattern, ...)69 let counts = call('s:SubmatchCount', [a:lnum, a:pattern] + a:000)70 return counts[0] - counts[1]71endfunction72 73function! GetMatlabIndent()74 let prevlnum = prevnonblank(v:lnum - 1)75 76 if b:MATLAB_lasttick != b:changedtick || b:MATLAB_lastline != prevlnum77 " Recalculate bracket count (only have to check same block and line above)78 let b:MATLAB_bracketlevel = 079 let previndent = indent(prevlnum) | let l = prevlnum80 while 181 let l = prevnonblank(l - 1) | let indent = indent(l)82 if l <= 0 || previndent < indent | break | endif83 let b:MATLAB_bracketlevel += s:GetOpenCloseCount(l, s:bracket_pair_pat)84 if previndent != indent | break | endif85 endwhile86 87 let b:MATLAB_waslc = s:IsLineContinuation(prevlnum - 1)88 endif89 " If line above was blank it can impossibly have been a LC90 let above_lc = b:MATLAB_lasttick == b:changedtick && prevlnum != v:lnum - 1 && b:MATLAB_lastline == prevlnum ? 0 : s:IsLineContinuation(v:lnum - 1)91 92 let 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 . '\)'97 let [open, close, b_open, b_close] = prevlnum ? s:SubmatchCount(prevlnum,98 \ pair_pat . '\|' . s:bracket_pair_pat) : [0, 0, 0, 0]99 let curbracketlevel = b:MATLAB_bracketlevel + b_open - b_close100 101 call cursor(v:lnum, 1)102 let submatch = search(s:dedent_pat, 'cp' . s:zflag, v:lnum)103 if submatch && !s:IsCommentOrString(v:lnum, col('.'))104 " Align end, et cetera with start of block105 let [lnum, col] = searchpairpos(s:start_pat, '', '\C' . s:end, 'bW', 's:IsCommentOrString(line("."), col("."))')106 let result = lnum ? indent(lnum) + shiftwidth() * (s:GetOpenCloseCount(lnum, pair_pat, col) + submatch == 2) : 0107 else108 " Count how many blocks the previous line opens/closes109 " Line continuations/brackets indent once per statement110 let result = (prevlnum > 0) * indent(prevlnum) + shiftwidth() * (open - close111 \ + (b:MATLAB_bracketlevel ? -!curbracketlevel : !!curbracketlevel)112 \ + (curbracketlevel <= 0) * (above_lc - b:MATLAB_waslc))113 endif114 115 let b:MATLAB_waslc = above_lc116 let b:MATLAB_bracketlevel = curbracketlevel117 let b:MATLAB_lasttick = b:changedtick118 let b:MATLAB_lastline = v:lnum119 return result120endfunction121 122let &cpo = s:keepcpo123unlet s:keepcpo124 