1" Matlab indent file 2" Language: Matlab 3" Maintainer: Christophe Poucet <[email protected]> 4" Last Change: 6 January, 2001 5 6" Only load this indent file when no other was loaded. 7if exists("b:did_indent") 8 finish 9endif 10let b:did_indent = 1 11 12" Some preliminary setting 13setlocal indentkeys=!,o,O=end,=case,=else,=elseif,=otherwise,=catch 14 15 16setlocal indentexpr=GetMatlabIndent(v:lnum) 17 18" Only define the function once. 19if exists("*GetMatlabIndent") 20 finish 21endif 22 23function GetMatlabIndent(lnum) 24 " Give up if this line is explicitly joined. 25 if getline(a:lnum - 1) =~ '\\$' 26 return -1 27 endif 28 29 " Search backwards for the first non-empty line. 30 let plnum = a:lnum - 1 31 while plnum > 0 && getline(plnum) =~ '^\s*$' 32 let plnum = plnum - 1 33 endwhile 34 35 if plnum == 0 36 " This is the first non-empty line, use zero indent. 37 return 0 38 endif 39 40 let curind = indent(plnum) 41 42 " If the current line is a stop-block statement... 43 if getline(v:lnum) =~ '^\s*\(end\|else\|elseif\|case\|otherwise\|catch\)\>' 44 " See if this line does not follow the line right after an openblock 45 if getline(plnum) =~ '^\s*\(for\|if\|else\|elseif\|case\|while\|switch\|try\|otherwise\|catch\)\>' 46 " See if the user has already dedented 47 elseif indent(v:lnum) > curind - shiftwidth() 48 " If not, recommend one dedent 49 let curind = curind - shiftwidth() 50 else 51 " Otherwise, trust the user 52 return -1 53 endif 54" endif 55 56 " If the previous line opened a block 57 elseif getline(plnum) =~ '^\s*\(for\|if\|else\|elseif\|case\|while\|switch\|try\|otherwise\|catch\)\>' 58 " See if the user has already indented 59 if indent(v:lnum) < curind + shiftwidth() 60 "If not, recommend indent 61 let curind = curind + shiftwidth() 62 else 63 " Otherwise, trust the user 64 return -1 65 endif 66 endif 67 68 69 70 " If we got to here, it means that the user takes the standardversion, so we return it 71 return curind 72endfunction 73 74" vim:sw=2 75