www.delorie.com/gnu/docs/elisp-manual-21/elisp_348.html   search  
Buy the book!

GNU Emacs Lisp Reference Manual

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

23.1 Major Modes

Major modes specialize Emacs for editing particular kinds of text. Each buffer has only one major mode at a time.

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 define a derived mode (see section 23.1.5 Defining Derived Modes). For example, Rmail Edit mode, which is in `emacs/lisp/mail/rmailedit.el', 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, it can be convenient to define it as a derivative of fundamental-mode, so that define-derived-mode can automatically enforce the most important coding conventions for you.

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 section 21.12 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.

23.1.1 Major Mode Conventions  Coding conventions for keymaps, etc.
23.1.2 Major Mode Examples  Text mode and Lisp modes.
23.1.3 How Emacs Chooses a Major Mode  How Emacs chooses the major mode automatically.
23.1.4 Getting Help about a Major Mode  Finding out how to use a mode.
23.1.5 Defining Derived Modes  Defining a new major mode based on another major mode.

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

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003