[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6. Configuration Summary

This (sort of pedagogic) section gives more information on how to configure SML mode: menus, key bindings, hooks and highlighting are discussed, along with a few other random topics.

6.1 Hooks  Creating them
6.2 Key bindings  Binding commands to keys
6.3 Syntax colouring  
6.4 Advanced Topics  You may need to speak Emacs Lisp

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.1 Hooks

One way to set SML mode variables (see section Indentation Defaults), and other defaults, is through the sml-mode-hook in your `.emacs'. A simple example:

(defun my-sml-mode-hook () "Local defaults for SML mode"
  (setq sml-indent-level 2)        ; conserve on horizontal space
  (setq words-include-escape t)    ; \ loses word break status
  (setq indent-tabs-mode nil))     ; never ever indent with tabs
(add-hook 'sml-mode-hook 'my-sml-mode-hook)
The body of my-sml-mode-hook is a sequence of assignments. In this case it is not really necessary to set sml-indent-level in a hook because this variable is global (most SML mode variables are). With similar effect:

(setq sml-indent-level 2)
anywhere in your `.emacs' file. The variable indent-tabs-mode is automatically made local to the current buffer whenever it is set explicitly, so it must be set in a hook if you always want SML mode to behave like this.

Another hook is inferior-sml-mode-hook. This can be used to control the behaviour of the interaction buffer through various variables meaningful to `comint'-based packages:

(defun my-inf-sml-mode-hook () "Local defaults for inferior SML mode"
  (add-hook 'comint-output-filter-functions 'comint-truncate-buffer)
  (setq      comint-scroll-show-maximum-output t)
  (setq      comint-input-autoexpand nil))
(add-hook 'inferior-sml-mode-hook 'my-inf-sml-mode-hook)
Again, the body is a sequence of assignments. Unless you run several ML compilers simultaneously under one Emacs, this hook will normally only get run once. You might want to look up the documentation (C-h v and C-h f) for these buffer-local comint things.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.2 Key bindings

Customisation (in Emacs) usually entails putting favourite commands on easily remembered keys. Two `keymaps' are defined in SML mode: one is effective in program text buffers (sml-mode-map) and the other is effective in interaction buffers (inferior-sml-mode-map). The initial design ensures that (many of) the default key bindings from the former keymap will also be available in the latter (e.g., C-c`).

Type C-h m in an SML mode buffer to find the default key bindings (and similarly in an ML interaction buffer), and use the hooks provided to install your preferred key bindings. Given that the keymaps are global (variables):

(defun my-sml-mode-hook () "Global defaults for SML mode"
  (define-key sml-mode-map "\C-cd" 'sml-cd))
(add-hook 'sml-mode-hook 'my-sml-mode-hook)
This has the effect of binding sml-cd to the key C-c d. If you want the same behaviour from C-c d in the ML buffer:

(defun my-inf-sml-mode-hook () "Global defaults for inferior SML mode"
  (define-key inferior-sml-mode-map "\C-cd" 'sml-cd)
  ;; NB. for SML/NJ '96
  (setq sml-cd-command "OS.FileSys.chDir \"%s\""))
(add-hook 'inferior-sml-mode-hook 'my-inf-sml-mode-hook)

There is nothing to stop you rebuilding the entire keymap for SML mode and the ML interaction buffer in your `.emacs' of course: SML mode won't define sml-mode-map or inferior-sml-mode-map if you have already done so.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.3 Syntax colouring

Highlighting is very handy for picking out keywords in the program text, spotting misspelled kewyords, and, if you have Emacs' `ps-print' package installed (you usually do these days), obtaining pretty, even colourful code listings--quite properly for your colourful ML programs.

The indentation scheme (strangely enough) also relies on the highlighting code to properly handle nested comments, which is yet another reason to turn on highlighting. To turn on highlighting, use either of:

M-x font-lock-mode
(add-hook 'sml-mode-hook 'turn-on-font-lock)
(global-font-lock-mode 1)

The first will turn it on in the current buffer. The second will turn it on in all sml-mode buffers. The last will turn it on everywhere. This is valid for Emacs but maybe not for XEmacs. Check font-lock documentation if you encounter problems.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.4 Advanced Topics

These forms are bloody useless; can't we have better ones?

You can indeed. sml-insert-form is extensible so all you need to do is create the macros yourself. Define a keybord macro (C-x ( <something> C-x )) and give it a suitable name: sml-addto-forms-alist prompts for a name, say NAME, and binds the macro sml-form-NAME. Thereafter C-c RET NAME will insert the macro at point, and C-u C-c RET NAME will insert the macro after a newline-and-indent. If you want to keep your macros from one editing session to the next, go to your `.emacs' file and call insert-kbd-macro; you'll need to add NAME to sml-forms-alist permanently yourself:

(defun my-sml-mode-hook () "Global defaults for SML mode"
  ;; whatever else you do
  (add-to-list 'sml-forms-alist '("NAME" . FUNCTION)))

If you want to create templates like `case' that prompt for parameters you'll have to do some Lisp programming. The skeleton package is a good stating point. Better yet, you can reuse the wrappers used by sml-mode itself in your sml-mode-hook:

(add-hook 'sml-mode-hook
  (lambda ()
    (sml-def-skeleton "case" "Case expr: "
      str " of" \n _ " => ")))

This will redefine `case' in order to leave the `of' on the first line. See the documentation of skeleton-insert to get a better understanding of how this works.

I hate that indentation algorithm; can't I tweak it?

Ah, yes, of course, but this manual will not tell you how.

Can SML mode handle more than one compiler running at once?

Sure, just rename the `*sml*' buffer and then use run-sml as usual.

What needs to be done to support other ML compilers?

Not much really. Just add the right regular expressions to sml-error-regexp-alist and that should be all.

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs Webmaster on October, 2 2007 using texi2html