blob: 9dbfd74d66ac27f61394fcd7e72c6649e9676753 [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
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +01006" 2022 April: b:undo_indent added by Doug Kearns
Bram Moolenaar6e649222021-10-04 21:32:54 +01007" Source: http://vim.sourceforge.net/scripts/script.php?script_id=1274
8" http://members.wolfram.com/layland/vim/indent/mma.vim
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009"
10" NOTE:
11" Empty .m files will automatically be presumed to be Matlab files
12" unless you have the following in your .vimrc:
13"
14" let filetype_m="mma"
15"
16" Credits:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000017" o steve hacked this out of a random indent file in the Vim 6.1
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018" distribution that he no longer remembers...sh.vim? Thanks!
19
20" Only load this indent file when no other was loaded.
21if exists("b:did_indent")
22 finish
23endif
24let b:did_indent = 1
25
26setlocal indentexpr=GetMmaIndent()
27setlocal indentkeys+=0[,0],0(,0)
28setlocal nosi "turn off smart indent so we don't over analyze } blocks
29
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010030let b:undo_indent = "setl inde< indk< si<"
31
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000032if exists("*GetMmaIndent")
33 finish
34endif
35
36function GetMmaIndent()
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000037
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000038 " Hit the start of the file, use zero indent.
39 if v:lnum == 0
40 return 0
41 endif
42
43 " Find a non-blank line above the current line.
44 let lnum = prevnonblank(v:lnum - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000045
46 " use indenting as a base
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000047 let ind = indent(v:lnum)
48 let lnum = v:lnum
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000049
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000050 " if previous line has an unmatched bracket, or ( indent.
51 " doesn't do multiple parens/blocks/etc...
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000052
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000053 " also, indent only if this line if this line isn't starting a new
54 " block... TODO - fix this with indentkeys?
55 if getline(v:lnum-1) =~ '\\\@<!\%(\[[^\]]*\|([^)]*\|{[^}]*\)$' && getline(v:lnum) !~ '\s\+[\[({]'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020056 let ind = ind+shiftwidth()
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000057 endif
58
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000059 " if this line had unmatched closing block,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000060 " indent to the matching opening block
61 if getline(v:lnum) =~ '[^[]*]\s*$'
62 " move to the closing bracket
63 call search(']','bW')
Bram Moolenaara6c27c42019-05-09 19:16:22 +020064 " and find its partner's indent
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000065 let ind = indent(searchpair('\[','',']','bWn'))
66 " same for ( blocks
67 elseif getline(v:lnum) =~ '[^(]*)$'
68 call search(')','bW')
69 let ind = indent(searchpair('(','',')','bWn'))
70
71 " and finally, close { blocks if si ain't already set
72 elseif getline(v:lnum) =~ '[^{]*}'
73 call search('}','bW')
74 let ind = indent(searchpair('{','','}','bWn'))
75 endif
76
77 return ind
78endfunction
79