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

23.2.3 Defining Minor Modes

The macro define-minor-mode offers a convenient way of implementing a mode in one self-contained definition. It supports only buffer-local minor modes, not global ones.

Macro: define-minor-mode mode doc &optional init-value mode-indicator keymap body...
This macro defines a new minor mode whose name is mode (a symbol). It defines a command named mode to toggle the minor mode, with doc as its documentation string. It also defines a variable named mode, which is set to t or nil by enabling or disabling the mode. The variable is initialized to init-value.

The command named mode finishes by executing the body forms, if any, after it has performed the standard actions such as setting the variable named mode.

The string mode-indicator says what to display in the mode line when the mode is enabled; if it is nil, the mode is not displayed in the mode line.

The optional argument keymap specifies the keymap for the minor mode. It can be a variable name, whose value is the keymap, or it can be an alist specifying bindings in this form:

 
(key-sequence . definition)

Here is an example of using define-minor-mode:

 
(define-minor-mode hungry-mode
  "Toggle Hungry mode.
With no argument, this command toggles the mode. 
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode.

When Hungry mode is enabled, the control delete key
gobbles all preceding whitespace except the last.
See the command \\[hungry-electric-delete]."
 ;; The initial value.
 nil
 ;; The indicator for the mode line.
 " Hungry"
 ;; The minor mode bindings.
 '(("\C-\^?" . hungry-electric-delete)
   ("\C-\M-\^?"
    . (lambda () 
        (interactive)
        (hungry-electric-delete t)))))

This defines a minor mode named "Hungry mode", a command named hungry-mode to toggle it, a variable named hungry-mode which indicates whether the mode is enabled, and a variable named hungry-mode-map which holds the keymap that is active when the mode is enabled. It initializes the keymap with key bindings for C-DEL and C-M-DEL.

The name easy-mmode-define-minor-mode is an alias for this macro.


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

This document was generated on May 2, 2002 using texi2html