blob: 10d84602aff61daeb323187a23262a1be5bd6185 [file] [log] [blame]
Bram Moolenaar9d87a372018-12-18 21:41:50 +01001" Vim indent file
2" Language: MATLAB
3" Maintainer: Axel Forsman <axelsfor@gmail.com>
4" Previous maintainer: Christophe Poucet <christophe.poucet@pandora.be>
Bram Moolenaar6e649222021-10-04 21:32:54 +01005" Last Update: 2021-10-01
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
Bram Moolenaar9d87a372018-12-18 21:41:50 +01007" Only load if no other indent file is loaded
8if exists('b:did_indent') | finish | endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009let b:did_indent = 1
10
Bram Moolenaar9d87a372018-12-18 21:41:50 +010011setlocal indentexpr=GetMatlabIndent()
12setlocal indentkeys=!,o,O,e,0=end,0=elseif,0=case,0=otherwise,0=catch,0=function,0=elsei
Bram Moolenaar6e649222021-10-04 21:32:54 +010013let b:undo_indent = "setlocal indentexpr< indentkeys<"
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
Bram Moolenaar9d87a372018-12-18 21:41:50 +010015" 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
Bram Moolenaar9d87a372018-12-18 21:41:50 +010028" Only define the function once
29if exists("*GetMatlabIndent") | finish | endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar9d87a372018-12-18 21:41:50 +010031let s:keepcpo = &cpo
32set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
Bram Moolenaar32b364f2019-12-08 15:07:48 +010034let s:end = '\<end\>\%([^({]*[)}]\)\@!' " Array indexing heuristic
Bram Moolenaar9d87a372018-12-18 21:41:50 +010035let 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' : ''
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar9d87a372018-12-18 21:41:50 +010041" Returns whether a comment or string envelops the specified column.
42function! s:IsCommentOrString(lnum, col)
Bram Moolenaar32b364f2019-12-08 15:07:48 +010043 return synIDattr(synID(a:lnum, a:col, 1), "name") =~# 'matlabComment\|matlabMultilineComment\|matlabCellComment\|matlabString'
Bram Moolenaar071d4272004-06-13 20:20:40 +000044endfunction
45
Bram Moolenaar9d87a372018-12-18 21:41:50 +010046" Returns whether the specified line continues on the next line.
47function! s:IsLineContinuation(lnum)
48 let l = getline(a:lnum) | let c = -3
49 while 1
50 let c = match(l, '\.\{3}', c + 3)
51 if c == -1 | return 0
52 elseif !s:IsCommentOrString(a:lnum, c) | return 1 | endif
53 endwhile
54endfunction
55
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 1
60 let [lnum, c, submatch] = searchpos(a:pattern, 'cpe' . s:zflag, a:lnum)
61 if !submatch || c >= endcol | break | endif
62 if !s:IsCommentOrString(lnum, c) | let x[submatch - 2] += 1 | endif
63 if cursor(0, c + 1) == -1 || col('.') == c | break | endif
64 endwhile
65 return x
66endfunction
67
68function! s:GetOpenCloseCount(lnum, pattern, ...)
69 let counts = call('s:SubmatchCount', [a:lnum, a:pattern] + a:000)
70 return counts[0] - counts[1]
71endfunction
72
73function! GetMatlabIndent()
74 let prevlnum = prevnonblank(v:lnum - 1)
75
76 if b:MATLAB_lasttick != b:changedtick || b:MATLAB_lastline != prevlnum
77 " Recalculate bracket count (only have to check same block and line above)
78 let b:MATLAB_bracketlevel = 0
79 let previndent = indent(prevlnum) | let l = prevlnum
80 while 1
81 let l = prevnonblank(l - 1) | let indent = indent(l)
82 if l <= 0 || previndent < indent | break | endif
83 let b:MATLAB_bracketlevel += s:GetOpenCloseCount(l, s:bracket_pair_pat)
84 if previndent != indent | break | endif
85 endwhile
86
87 let b:MATLAB_waslc = s:IsLineContinuation(prevlnum - 1)
88 endif
89 " If line above was blank it can impossibly have been a LC
90 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_close
100
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 block
105 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) : 0
107 else
108 " Count how many blocks the previous line opens/closes
109 " Line continuations/brackets indent once per statement
Bram Moolenaar32b364f2019-12-08 15:07:48 +0100110 let result = (prevlnum > 0) * indent(prevlnum) + shiftwidth() * (open - close
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100111 \ + (b:MATLAB_bracketlevel ? -!curbracketlevel : !!curbracketlevel)
112 \ + (curbracketlevel <= 0) * (above_lc - b:MATLAB_waslc))
113 endif
114
115 let b:MATLAB_waslc = above_lc
116 let b:MATLAB_bracketlevel = curbracketlevel
117 let b:MATLAB_lasttick = b:changedtick
118 let b:MATLAB_lastline = v:lnum
119 return result
120endfunction
121
122let &cpo = s:keepcpo
123unlet s:keepcpo