The least specialized major mode is called Fundamental mode.
This mode has no mode-specific definitions or variable settings, so each
Emacs command behaves in its default manner, and each option is in its
default state. All other major modes redefine various keys and options.
For example, Lisp Interaction mode provides special key bindings for
C-j (eval-print-last-sexp), <TAB>
(lisp-indent-line), and other keys.
When you need to write several editing commands to help you perform a specialized editing task, creating a new major mode is usually a good idea. In practice, writing a major mode is easy (in contrast to writing a minor mode, which is often difficult).
If the new mode is similar to an old one, it is often unwise to
modify the old one to serve two purposes, since it may become harder
to use and maintain. Instead, copy and rename an existing major mode
definition and alter the copy—or use the define-derived-mode
macro to define a derived mode (see Derived Modes). For
example, Rmail Edit mode is a major mode that is very similar to Text
mode except that it provides two additional commands. Its definition
is distinct from that of Text mode, but uses that of Text mode.
Even if the new mode is not an obvious derivative of any other mode,
we recommend to use define-derived-mode, since it automatically
enforces the most important coding conventions for you.
For a very simple programming language major mode that handles
comments and fontification, you can use define-generic-mode.
See Generic Modes.
Rmail Edit mode offers an example of changing the major mode temporarily for a buffer, so it can be edited in a different way (with ordinary Emacs commands rather than Rmail commands). In such cases, the temporary major mode usually provides a command to switch back to the buffer's usual mode (Rmail mode, in this case). You might be tempted to present the temporary redefinitions inside a recursive edit and restore the usual ones when the user exits; but this is a bad idea because it constrains the user's options when it is done in more than one buffer: recursive edits must be exited most-recently-entered first. Using an alternative major mode avoids this limitation. See Recursive Editing.
The standard GNU Emacs Lisp library directory tree contains the code for several major modes, in files such as text-mode.el, texinfo.el, lisp-mode.el, c-mode.el, and rmail.el. They are found in various subdirectories of the lisp directory. You can study these libraries to see how modes are written. Text mode is perhaps the simplest major mode aside from Fundamental mode. Rmail mode is a complicated and specialized mode.
blog comments powered by Disqus