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