[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The code for existing major modes follows various coding conventions, including conventions for local keymap and syntax table initialization, global names, and hooks. Please follow these conventions when you define a new major mode.
This list of conventions is only partial, because each major mode should aim for consistency in general with other Emacs major modes. This makes Emacs as a whole more coherent. It is impossible to list here all the possible points where this issue might come up; if the Emacs developers point out an area where your major mode deviates from the usual conventions, please make it compatible.
describe-mode
) in your mode will display this string.
The documentation string may include the special documentation substrings, `\[command]', `\{keymap}', and `\<keymap>', which enable the documentation to adapt automatically to the user's own key bindings. See section 24.3 Substituting Key Bindings in Documentation.
kill-all-local-variables
. This is what gets rid of the
buffer-local variables of the major mode previously in effect.
major-mode
to the
major mode command symbol. This is how describe-mode
discovers
which documentation to print.
mode-name
to the
"pretty" name of the mode, as a string. This string appears in the
mode line.
indent-line-function
to a suitable function, and probably customize other variables
for indentation.
use-local-map
to install this local map. See section 22.6 Active Keymaps, for more information.
This keymap should be stored permanently in a global variable named
modename-mode-map
. Normally the library that defines the
mode sets this variable.
See section 11.6 Tips for Defining Variables Robustly, for advice about how to write the code to set up the mode's keymap variable.
It is reasonable for a major mode to rebind a key sequence with a standard meaning, if it implements a command that does "the same job" in a way that fits the major mode better. For example, a major mode for editing a programming language might redefine C-M-a to "move to the beginning of a function" in a way that works better for that language.
Major modes such as Dired or Rmail that do not allow self-insertion of text can reasonably redefine letters and other printing characters as editing commands. Dired and Rmail both do this.
modename-mode-syntax-table
. See section 35. Syntax Tables.
modename-mode-abbrev-table
. See section 36.2 Abbrev Tables.
font-lock-defaults
(see section 23.5 Font Lock Mode).
imenu-generic-expression
or
imenu-create-index-function
(see section 23.4 Imenu).
defvar
or defcustom
to set mode-related variables, so
that they are not reinitialized if they already have a value. (Such
reinitialization could discard customizations made by the user.)
make-local-variable
in the major mode command, not
make-variable-buffer-local
. The latter function would make the
variable local to every buffer in which it is subsequently set, which
would affect buffers that do not use this mode. It is undesirable for a
mode to have such global effects. See section 11.10 Buffer-Local Variables.
With rare exceptions, the only reasonable way to use
make-variable-buffer-local
in a Lisp package is for a variable
which is used only within that package. Using it on a variable used by
other packages would interfere with them.
modename-mode-hook
. The major mode command should run that
hook, with run-hooks
, as the very last thing it
does. See section 23.6 Hooks.
indented-text-mode
runs text-mode-hook
as
well as indented-text-mode-hook
. It may run these other hooks
immediately before the mode's own hook (that is, after everything else),
or it may run them earlier.
change-major-mode-hook
(see section 11.10.2 Creating and Deleting Buffer-Local Bindings).
mode-class
with value special
, put on as follows:
(put 'funny-mode 'mode-class 'special) |
This tells Emacs that new buffers created while the current buffer is in Funny mode should not inherit Funny mode. Modes such as Dired, Rmail, and Buffer List use this feature.
auto-mode-alist
to select
the mode for those file names. If you define the mode command to
autoload, you should add this element in the same file that calls
autoload
. Otherwise, it is sufficient to add the element in the
file that contains the mode definition. See section 23.1.3 How Emacs Chooses a Major Mode.
autoload
form
and an example of how to add to auto-mode-alist
, that users can
include in their init files (see section 40.1.2 The Init File, `.emacs').
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |