mma.vim 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. " Only load this indent file when no other was loaded.
  20. if exists("b:did_indent")
  21. finish
  22. endif
  23. let b:did_indent = 1
  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. let b:undo_indent = "setl inde< indk< si<"
  28. if exists("*GetMmaIndent")
  29. finish
  30. endif
  31. function GetMmaIndent()
  32. " Hit the start of the file, use zero indent.
  33. if v:lnum == 0
  34. return 0
  35. endif
  36. " Find a non-blank line above the current line.
  37. let lnum = prevnonblank(v:lnum - 1)
  38. " use indenting as a base
  39. let ind = indent(v:lnum)
  40. let lnum = v:lnum
  41. " if previous line has an unmatched bracket, or ( indent.
  42. " doesn't do multiple parens/blocks/etc...
  43. " also, indent only if this line if this line isn't starting a new
  44. " block... TODO - fix this with indentkeys?
  45. if getline(v:lnum-1) =~ '\\\@<!\%(\[[^\]]*\|([^)]*\|{[^}]*\)$' && getline(v:lnum) !~ '\s\+[\[({]'
  46. let ind = ind+shiftwidth()
  47. endif
  48. " if this line had unmatched closing block,
  49. " indent to the matching opening block
  50. if getline(v:lnum) =~ '[^[]*]\s*$'
  51. " move to the closing bracket
  52. call search(']','bW')
  53. " and find its partner's indent
  54. let ind = indent(searchpair('\[','',']','bWn'))
  55. " same for ( blocks
  56. elseif getline(v:lnum) =~ '[^(]*)$'
  57. call search(')','bW')
  58. let ind = indent(searchpair('(','',')','bWn'))
  59. " and finally, close { blocks if si ain't already set
  60. elseif getline(v:lnum) =~ '[^{]*}'
  61. call search('}','bW')
  62. let ind = indent(searchpair('{','','}','bWn'))
  63. endif
  64. return ind
  65. endfunction