Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 24: Installing Emacs Add-Ons |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

## Hooks

Hooks are often provided for add-on Lisp files. Hooks are aptly named--they are functions that an Emacs process can "hook" into in order for a user to change the default behavior. An example might make this clearer:

    (add-hook 'text-mode-hook 'turn-on-auto-fill)

By default, Emacs doesn't automatically wrap lines in text- mode. Adding the preceding lines to your .emacs file causes the function turn-on-auto-fill to be automatically loaded along with text-mode. When this hook is active, lines are automatically wrapped as you type. To try out a hook without including it in your .emacs file, type the line (complete with parentheses) into the Scratch buffer and then type C-x C-e, which is the keybinding for Evaluate LastS-Expression. The hook is then active until you exit Emacs.

Hooks are also used when you want to load two modes simultaneously. For example, if you want outline-minor-mode to be loaded along with AucTeX when you are editing TeX files, the following line is needed in your .emacs file:

    (add-hook 'LaTeX-mode-hook '(lambda () (outline-minor-mode1)))

Using the lambda statement is a method of creating a nameless function (see Hour 22, "Learning Lisp Basics"); in this case, it causes outline-minor-mode to be loaded along with AucTeX's LaTeX mode. You didn't need to use lambda in the earlier examples because turn-on-auto-fill is already a function, whereas outline-minor-mode is a mode. In order for outline-minor-mode to be used in an add-hook statement, it is necessary to wrap it in a function definition.

Hooks can also be used to choose between several available options. CC-mode is a major mode that is used for editing C and C++ source-code files. The mode can be set to use one of several different coding styles with the following statement:

    (add-hook 'c-mode-hook
              ' (lambda ()
                  (c-set-style "gnu")))

Many modes have their own built-in hooks, which you normally don't want to disable. The add-hook statement leaves existing hooks active while adding an additional hook.

### Binding Keys With Hooks

Hook statements are often used to change the keybindings of a mode, most of which have predefined keymaps. Everyone has different editing habits and needs, so the most convenient and comfortable keys differ from user to user. Using mode-hooks ensures that the new keybindings only take effect when the particular mode is in use. Following are some examples:

    (add-hook 'notes-mode-hook
      ' (lambda ()
        (define-key notes-mode-map [f5] 'notes-follow-prev-link)
        (define-key notes-mode-map [f6] 'notes-follow-next-link)))

This example changes two multiple-key bindings to an adjacent pair of unused function keys. This speeds up two often-used commands in notes-mode. This mode is briefly described at the end of the hour:

      (setq mail-mode-hook
        ' (lambda ()
         (define-key mail-mode-map "\C-cm" 'mail-send-and-exit)
         (define-key mail-mode-map "\C-cc" 'mail-cc)
         ('turn-on-auto-fill)
         (setq fill-column 65)))

In this example, two keybindings have been changed to shorter key sequences, auto-fill has been turned on, and the fill column has been narrowed to 65. Mail-mode doesn't have a predefined hook, so the setq statement is used to create one. Any mail-mode hook statements that follow this one in the .emacs file can use the add-hook function rather than setq.

Some add-ons have special provisions for changing the keybindings. For example, Folding-mode includes a function called fold-default-keys-function, which simplifies the alteration of keybindings. A function such as this in your .emacs file changes just one of the keybindings:

    defun my-fold-bind-keys ()
     "My favorite fold key settings"
     ;;  - I want the default ones, but I want fold-whole-buffer and
     ;;    fold-open-buffer to be close together, instead of the
     ;;    default
     ;;  - read them all.
     ;;
     (fold-default-keys)
     ;;  - first disable old bind and define new one.
     ;;
     (define-key folding-mode-map "\C-c\C-w" nil)
     (define-key folding-mode-map "\C-c\C-p" 'fold-whole-buffer)
     )

This function first sets the keymap to the default with the (fold-default-keys) statement. Next, the default keybinding for fold-whole-buffer is disabled and a new binding (C-c C-p) is substituted.

The following line assigns the new my-fold-bind-keys function as the new default:

    setq fold-default-keys-function 'my-fold-bind-keys)

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 24: Installing Emacs Add-Ons |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

© Copyright Macmillan USA. All rights reserved.