# 15-212: Principles of Programming

## Configuring GNU Emacs to run SML/NJ

To configure GNU Emacs to run SML/NJ, copy the Elisp files for SML mode from /afs/andrew/course/15/212sp/lib/elisp to your local machine, and add the following lines to the end of your .emacs file (these lines can also be found in the file emacs.additions):

```(autoload 'sml-mode  "sml-mode" "Major mode for editing SML." t)
(setq auto-mode-alist
(cons '("\.sml\$" . sml-mode)
(cons '("\.sig\$" . sml-mode)
auto-mode-alist)))
(setq sml-program-name "/afs/andrew/course/15/212sp/bin/sml")
```

On Windows machines you will probably wish to replace the last two lines above with lines similar to the following:

```(setq load-path (cons "c:/smlnj/esml/" load-path))
(setq sml-program-name "c:/smlnj/bin/sml.bat")
(setq sml-temp-file (make-temp-name "c:/temp/"))
```

The precise pathnames in these last three lines depend on where you have installed the various pieces of the system. The load-path definition assumes that the Elisp files were copied to the directory c:\smlnj\esml. The sml-program-name definition assumes that the SML/NJ system was installed underneath the directory c:\smlnj, invokeable by the indicated .bat file. The sml-temp-file definition assumes that c:\temp is a valid directory where SML/NJ may place temporary files. You may need to experiment a bit with the exact path names.

If you prefer not to run SML/NJ through GNU Emacs, you may also run it by following links from Window's START menu.

On Andrew, you can start GNU Emacs by typing:    `emacs`

## Starting SML within GNU Emacs

To start an SML/NJ within GNU Emacs, one of the following is likely to be successful (it depends a bit on your particular implementation):

• Type    M-x run-sml   at Emacs (and hit ENTER as appropriate, probably twice).
• Type    M-x sml-mode  at Emacs (and hit ENTER as appropriate).   Then type    M-x sml   at Emacs (and hit ENTER as appropriate).
• Read in a file with extension .sml, then type    M-x sml   at Emacs (and hit ENTER as appropriate).
When GNU Emacs starts SML it may be in a different window or in a different buffer. Look around.

## Using the SML mode for GNU Emacs

When running SML under GNU Emacs, you can have emacs "use" the current buffer or region. The Emacs command C-c C-b sends the current buffer to SML/NJ and C-c C-r sends the current Emacs region. You can also have Emacs issue directory change commands with M-x sml-cd.

One of the most useful things Emacs can do for you is to parse error messages and locate their source. After compiling a file, enter C-c ` and Emacs will locate and highlight your first error, as reported by SML/NJ. You may then locate successive errors by repeating the command. Note that if you enter a "use" directly into SML/NJ (by typing "use" on the keyboard), Emacs will not know of it, and will continue parsing errors from the previous compile. You can get Emacs back into synch by directing it to ignore all remaining errors with the M-x sml-skip-errors command.

You may also wish to experiment with the M-| command, which begins a new branch of a case statement or function definition.

A summary of these commands appears below:

 key sequence command name C-c C-b sml-send-buffer C-c C-r sml-send-region M-x sml-cd sml-cd C-c ` sml-next-error M-x sml-skip-errors sml-skip-errors M-| sml-electric-pipe

Free copies of GNU Emacs and installation directions are available here.