Emacs mode for Mathematica package files

Note: This is an archived copy of the mma.el homepage and source code by Tim Wichmannn. This material was previously located at http://www.itwm.uni-kl.de/as/employees/wichmann/mma.html and is being hosted here by Jason Blevins for historical purposes only. No support can be provided.


The Emacs mode mma.el is a major programming mode for GNU Emacs and XEmacs for writing Mathematica package files (.m-files). It provides:


The source code of mma.el you will find here. The current version of mma.el is 1.32. If you want to be informed about new versions of mma.el or if you have any suggestions, just send me an email.

It works with GNU Emacs 20.4 or later and XEmacs 20.4 or later. It was tested on GNU Emacs 20.4, XEmacs 20.4, and XEmacs 21.3.

To use the mma-mode you should add the following to your local .emacs file:

(autoload 'mma-mode "mma.el" "Mathematica package file mode" t)
(setq auto-mode-alist
      (cons '("\.m\'" . mma-mode) auto-mode-alist))

This automatically turns on mma-mode for files with the suffix .m on loading. To start the mode by hand simply type M-x mma-mode.


For a full description of its features see the documentation strings (especially of the function mma-mode), the comments in the source file, and the customization group mathematica.

The indentation machine works as follows:

If point is on the leading whitespace of the current line, indents as far as the preceding non-empty line and moves point to the first non-whitespace character of the line.

If point is not on the leading whitespace of the current line, indents by steps of mma-indentation.

If called with prefix argument, aligns rest of current line with the next whitespace character position in previous lines (point is denoted by -!-):

---------- Buffer: foo.m ----------
MyOptionName -> MyValue
Another-!--> Value
---------- Buffer: foo.m ----------

Calling with prefix argument yields:

---------- Buffer: foo.m ----------
MyOptionName -> MyValue
Another      -!--> Value
---------- Buffer: foo.m ----------

(Difficult to explain. Just try it and see the result.)

If called with negative prefix argument, aligns with whitespace in following lines.

The variable mma-indent-align-at-eolp specifies if mma-indent-line indents by steps of mma-indentation (value is nil) or aligns to whitespace (value is non-nil) if point is at the end of line.

Activating/Deactivating debug code:

Since Mathematica does not provide a source code debugger, I often find myself writing pieces of code like

Print[ "value1= ", somevariable ];

when I am debugging my Mathematica functions, removing them as soon as the bug is fixed - and inserting them back on if another bug occurs. To simplify this procedure you can mark debug code with a special marker comment like this

(*: DEBUG MyFunction :*)
Print[ "value1= ", somvevariable ];
(*: ENDDEBUG :*)

(Use the function mma-debug-insert to insert a debug code skeleton). Each debug code has a marker, in the above example it is “MyFunction”. The functions mma-debug-on and mma-debug-off activate or deactivate your debug code. Both take a regexp as argument and act only on debug code which marker matches the regexp. Thus, (mma-debug-off "MyFun.*") would turn off the above debug code (i.e., it inserts additional comments surrounding the Print statement). (mma-debug-on ".*") turns on all debug code. The variable mma-debug-fontify determines whether to re-fontify the buffer after a change to the debug code.

Customizing the variables mma-debug-start-1, mma-debug-start-2, and mma-debug-end you can change the special markers for you favorite style.

Note: Mathematica is a registered trademark of Wolfram Research, Inc.