blob: 8298ad98cdf2cb45a1a43e9709c1a376e5ff490c [file] [log] [blame]
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001" Vim indent file
2" Language: Mathematica
3" Author: steve layland <layland@wolfram.com>
4" Last Change: Sat May 10 18:56:22 CDT 2005
5" Source: http://vim.sourceforge.net/scripts/script.php?script_id=1274
6" http://members.wolfram.com/layland/vim/indent/mma.vim
7"
8" NOTE:
9" Empty .m files will automatically be presumed to be Matlab files
10" unless you have the following in your .vimrc:
11"
12" let filetype_m="mma"
13"
14" Credits:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000015" o steve hacked this out of a random indent file in the Vim 6.1
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016" distribution that he no longer remembers...sh.vim? Thanks!
17
18" Only load this indent file when no other was loaded.
19if exists("b:did_indent")
20 finish
21endif
22let b:did_indent = 1
23
24setlocal indentexpr=GetMmaIndent()
25setlocal indentkeys+=0[,0],0(,0)
26setlocal nosi "turn off smart indent so we don't over analyze } blocks
27
28if exists("*GetMmaIndent")
29 finish
30endif
31
32function GetMmaIndent()
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000033
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000034 " Hit the start of the file, use zero indent.
35 if v:lnum == 0
36 return 0
37 endif
38
39 " Find a non-blank line above the current line.
40 let lnum = prevnonblank(v:lnum - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000041
42 " use indenting as a base
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000043 let ind = indent(v:lnum)
44 let lnum = v:lnum
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000045
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000046 " if previous line has an unmatched bracket, or ( indent.
47 " doesn't do multiple parens/blocks/etc...
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000048
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000049 " also, indent only if this line if this line isn't starting a new
50 " block... TODO - fix this with indentkeys?
51 if getline(v:lnum-1) =~ '\\\@<!\%(\[[^\]]*\|([^)]*\|{[^}]*\)$' && getline(v:lnum) !~ '\s\+[\[({]'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020052 let ind = ind+shiftwidth()
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000053 endif
54
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000055 " if this line had unmatched closing block,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000056 " indent to the matching opening block
57 if getline(v:lnum) =~ '[^[]*]\s*$'
58 " move to the closing bracket
59 call search(']','bW')
60 " and find it's partner's indent
61 let ind = indent(searchpair('\[','',']','bWn'))
62 " same for ( blocks
63 elseif getline(v:lnum) =~ '[^(]*)$'
64 call search(')','bW')
65 let ind = indent(searchpair('(','',')','bWn'))
66
67 " and finally, close { blocks if si ain't already set
68 elseif getline(v:lnum) =~ '[^{]*}'
69 call search('}','bW')
70 let ind = indent(searchpair('{','','}','bWn'))
71 endif
72
73 return ind
74endfunction
75