elisp: Derived Modes
22.2.4 Defining Derived Modes
-----------------------------
The recommended way to define a new major mode is to derive it from an
existing one using ‘define-derived-mode’. If there is no closely
related mode, you should inherit from either ‘text-mode’,
‘special-mode’, or ‘prog-mode’. Basic Major Modes. If none of
these are suitable, you can inherit from ‘fundamental-mode’ (Major
Modes).
-- Macro: define-derived-mode variant parent name docstring
keyword-args... body...
This macro defines VARIANT as a major mode command, using NAME as
the string form of the mode name. VARIANT and PARENT should be
unquoted symbols.
The new command VARIANT is defined to call the function PARENT,
then override certain aspects of that parent mode:
• The new mode has its own sparse keymap, named ‘VARIANT-map’.
‘define-derived-mode’ makes the parent mode’s keymap the
parent of the new map, unless ‘VARIANT-map’ is already set and
already has a parent.
• The new mode has its own syntax table, kept in the variable
‘VARIANT-syntax-table’, unless you override this using the
‘:syntax-table’ keyword (see below). ‘define-derived-mode’
makes the parent mode’s syntax-table the parent of
‘VARIANT-syntax-table’, unless the latter is already set and
already has a parent different from the standard syntax table.
• The new mode has its own abbrev table, kept in the variable
‘VARIANT-abbrev-table’, unless you override this using the
‘:abbrev-table’ keyword (see below).
• The new mode has its own mode hook, ‘VARIANT-hook’. It runs
this hook, after running the hooks of its ancestor modes, with
‘run-mode-hooks’, as the last thing it does. Mode
Hooks.
In addition, you can specify how to override other aspects of
PARENT with BODY. The command VARIANT evaluates the forms in BODY
after setting up all its usual overrides, just before running the
mode hooks.
If PARENT has a non-‘nil’ ‘mode-class’ symbol property, then
‘define-derived-mode’ sets the ‘mode-class’ property of VARIANT to
the same value. This ensures, for example, that if PARENT is a
special mode, then VARIANT is also a special mode (Major Mode
Conventions).
You can also specify ‘nil’ for PARENT. This gives the new mode no
parent. Then ‘define-derived-mode’ behaves as described above,
but, of course, omits all actions connected with PARENT.
The argument DOCSTRING specifies the documentation string for the
new mode. ‘define-derived-mode’ adds some general information
about the mode’s hook, followed by the mode’s keymap, at the end of
this documentation string. If you omit DOCSTRING,
‘define-derived-mode’ generates a documentation string.
The KEYWORD-ARGS are pairs of keywords and values. The values are
evaluated. The following keywords are currently supported:
‘:syntax-table’
You can use this to explicitly specify a syntax table for the
new mode. If you specify a ‘nil’ value, the new mode uses the
same syntax table as PARENT, or the standard syntax table if
PARENT is ‘nil’. (Note that this does _not_ follow the
convention used for non-keyword arguments that a ‘nil’ value
is equivalent with not specifying the argument.)
‘:abbrev-table’
You can use this to explicitly specify an abbrev table for the
new mode. If you specify a ‘nil’ value, the new mode uses the
same abbrev table as PARENT, or
‘fundamental-mode-abbrev-table’ if PARENT is ‘nil’. (Again, a
‘nil’ value is _not_ equivalent to not specifying this
keyword.)
‘:group’
If this is specified, the value should be the customization
group for this mode. (Not all major modes have one.) The
command ‘customize-mode’ uses this. ‘define-derived-mode’
does _not_ automatically define the specified customization
group.
Here is a hypothetical example:
(defvar hypertext-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [down-mouse-3] 'do-hyper-link)
map))
(define-derived-mode hypertext-mode
text-mode "Hypertext"
"Major mode for hypertext."
(setq-local case-fold-search nil))
Do not write an ‘interactive’ spec in the definition;
‘define-derived-mode’ does that automatically.
-- Function: derived-mode-p &rest modes
This function returns non-‘nil’ if the current major mode is
derived from any of the major modes given by the symbols MODES.