blob: 6a316243898c487a28b58f75d5992cc3f35a7833 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Matlab indent file
2" Language: Matlab
3" Maintainer: Christophe Poucet <christophe.poucet@pandora.be>
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
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020047 elseif indent(v:lnum) > curind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 " If not, recommend one dedent
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020049 let curind = curind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 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
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020059 if indent(v:lnum) < curind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 "If not, recommend indent
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020061 let curind = curind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 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