mma.vim (2367B)
1 " Vim indent file 2 " 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 " 2022 April: b:undo_indent added by Doug Kearns 7 " Source: http://vim.sourceforge.net/scripts/script.php?script_id=1274 8 " http://members.wolfram.com/layland/vim/indent/mma.vim 9 " 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: 17 " o steve hacked this out of a random indent file in the Vim 6.1 18 " distribution that he no longer remembers...sh.vim? Thanks! 19 20 " Only load this indent file when no other was loaded. 21 if exists("b:did_indent") 22 finish 23 endif 24 let b:did_indent = 1 25 26 setlocal indentexpr=GetMmaIndent() 27 setlocal indentkeys+=0[,0],0(,0) 28 setlocal nosi "turn off smart indent so we don't over analyze } blocks 29 30 let b:undo_indent = "setl inde< indk< si<" 31 32 if exists("*GetMmaIndent") 33 finish 34 endif 35 36 function GetMmaIndent() 37 38 " 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) 45 46 " use indenting as a base 47 let ind = indent(v:lnum) 48 let lnum = v:lnum 49 50 " if previous line has an unmatched bracket, or ( indent. 51 " doesn't do multiple parens/blocks/etc... 52 53 " 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\+[\[({]' 56 let ind = ind+shiftwidth() 57 endif 58 59 " if this line had unmatched closing block, 60 " indent to the matching opening block 61 if getline(v:lnum) =~ '[^[]*]\s*$' 62 " move to the closing bracket 63 call search(']','bW') 64 " and find its partner's indent 65 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 78 endfunction