mma.vim 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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:
  15. " o steve hacked this out of a random indent file in the Vim 6.1
  16. " distribution that he no longer remembers...sh.vim? Thanks!
  17. " Only load this indent file when no other was loaded.
  18. if exists("b:did_indent")
  19. finish
  20. endif
  21. let b:did_indent = 1
  22. setlocal indentexpr=GetMmaIndent()
  23. setlocal indentkeys+=0[,0],0(,0)
  24. setlocal nosi "turn off smart indent so we don't over analyze } blocks
  25. if exists("*GetMmaIndent")
  26. finish
  27. endif
  28. function GetMmaIndent()
  29. " Hit the start of the file, use zero indent.
  30. if v:lnum == 0
  31. return 0
  32. endif
  33. " Find a non-blank line above the current line.
  34. let lnum = prevnonblank(v:lnum - 1)
  35. " use indenting as a base
  36. let ind = indent(v:lnum)
  37. let lnum = v:lnum
  38. " if previous line has an unmatched bracket, or ( indent.
  39. " doesn't do multiple parens/blocks/etc...
  40. " also, indent only if this line if this line isn't starting a new
  41. " block... TODO - fix this with indentkeys?
  42. if getline(v:lnum-1) =~ '\\\@<!\%(\[[^\]]*\|([^)]*\|{[^}]*\)$' && getline(v:lnum) !~ '\s\+[\[({]'
  43. let ind = ind+shiftwidth()
  44. endif
  45. " if this line had unmatched closing block,
  46. " indent to the matching opening block
  47. if getline(v:lnum) =~ '[^[]*]\s*$'
  48. " move to the closing bracket
  49. call search(']','bW')
  50. " and find its partner's indent
  51. let ind = indent(searchpair('\[','',']','bWn'))
  52. " same for ( blocks
  53. elseif getline(v:lnum) =~ '[^(]*)$'
  54. call search(')','bW')
  55. let ind = indent(searchpair('(','',')','bWn'))
  56. " and finally, close { blocks if si ain't already set
  57. elseif getline(v:lnum) =~ '[^{]*}'
  58. call search('}','bW')
  59. let ind = indent(searchpair('{','','}','bWn'))
  60. endif
  61. return ind
  62. endfunction