Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | cbaff5e | 2022-04-08 17:45:08 +0100 | [diff] [blame] | 6 | " 2022 April: b:undo_indent added by Doug Kearns |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 7 | " Source: http://vim.sourceforge.net/scripts/script.php?script_id=1274 |
| 8 | " http://members.wolfram.com/layland/vim/indent/mma.vim |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 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: |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 17 | " 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] | 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 | |
Bram Moolenaar | cbaff5e | 2022-04-08 17:45:08 +0100 | [diff] [blame] | 30 | let b:undo_indent = "setl inde< indk< si<" |
| 31 | |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 32 | if exists("*GetMmaIndent") |
| 33 | finish |
| 34 | endif |
| 35 | |
| 36 | function GetMmaIndent() |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 37 | |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 45 | |
| 46 | " use indenting as a base |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 47 | let ind = indent(v:lnum) |
| 48 | let lnum = v:lnum |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 49 | |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 50 | " if previous line has an unmatched bracket, or ( indent. |
| 51 | " doesn't do multiple parens/blocks/etc... |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 52 | |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 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\+[\[({]' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 56 | let ind = ind+shiftwidth() |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 57 | endif |
| 58 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 59 | " if this line had unmatched closing block, |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 60 | " indent to the matching opening block |
| 61 | if getline(v:lnum) =~ '[^[]*]\s*$' |
| 62 | " move to the closing bracket |
| 63 | call search(']','bW') |
Bram Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 64 | " and find its partner's indent |
Bram Moolenaar | 34cdc3e | 2005-05-18 22:24:46 +0000 | [diff] [blame] | 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 |
| 79 | |