xref: /vim-8.2.3635/runtime/indent/mma.vim (revision 6e649224)
1" Vim indent file
2" Language:	Mathematica
3" Maintainer:	Steve Layland <[email protected]> (Invalid email address)
4" 		Doug Kearns <[email protected]>
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
8"
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:
16" o steve hacked this out of a random indent file in the Vim 6.1
17"   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()
34
35    " 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)
42
43    " use indenting as a base
44    let ind = indent(v:lnum)
45    let lnum = v:lnum
46
47    " if previous line has an unmatched bracket, or ( indent.
48    " doesn't do multiple parens/blocks/etc...
49
50    " 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\+[\[({]'
53        let ind = ind+shiftwidth()
54    endif
55
56    " if this line had unmatched closing block,
57    " indent to the matching opening block
58    if getline(v:lnum) =~ '[^[]*]\s*$'
59        " move to the closing bracket
60        call search(']','bW')
61        " and find its partner's indent
62        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
77