Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 1 | " 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 Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 15 | " o steve hacked this out of a random indent file in the Vim 6.1 |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 16 | " distribution that he no longer remembers...sh.vim? Thanks! |
| 17 | |
| 18 | " Only load this indent file when no other was loaded. |
| 19 | if exists("b:did_indent") |
| 20 | finish |
| 21 | endif |
| 22 | let b:did_indent = 1 |
| 23 | |
| 24 | setlocal indentexpr=GetMmaIndent() |
| 25 | setlocal indentkeys+=0[,0],0(,0) |
| 26 | setlocal nosi "turn off smart indent so we don't over analyze } blocks |
| 27 | |
| 28 | if exists("*GetMmaIndent") |
| 29 | finish |
| 30 | endif |
| 31 | |
| 32 | function GetMmaIndent() |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 33 | |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 34 | " 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 Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 41 | |
| 42 | " use indenting as a base |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 43 | let ind = indent(v:lnum) |
| 44 | let lnum = v:lnum |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 45 | |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 46 | " if previous line has an unmatched bracket, or ( indent. |
| 47 | " doesn't do multiple parens/blocks/etc... |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 48 | |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 49 | " 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 Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 52 | let ind = ind+shiftwidth() |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 53 | endif |
| 54 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 55 | " if this line had unmatched closing block, |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 56 | " 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 |
| 74 | endfunction |
| 75 | |