Next: , Previous: , Up: Modes   [Contents][Index]


33.1 Major Modes

Major modes specialize SXEmacs for editing particular kinds of text. Each buffer has only one major mode at a time. For each major mode there is a function to switch to that mode in the current buffer; its name should end in ‘-mode’. These functions work by setting buffer-local variable bindings and other data associated with the buffer, such as a local keymap. The effect lasts until you switch to another major mode in the same buffer.

The least specialized major mode is called Fundamental mode. This mode has no mode-specific definitions or variable settings, so each SXEmacs 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 LFD (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 Derived Modes).

Even if the new mode is not an obvious derivative of any other mode, it is convenient to use define-derived-mode with a nil parent argument, since it automatically enforces the most important coding conventions for you.

Rmail Edit mode is an example of a case where one piece of text is put temporarily into a different major mode so it can be edited in a different way (with ordinary SXEmacs commands rather than Rmail). In such cases, the temporary major mode usually has 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 alternative major modes avoids this limitation. See Recursive Editing.

The standard SXEmacs Lisp library directory contains the code for several major modes, in files including text-mode.el, texinfo.el, lisp-mode.el, c-mode.el, and rmail.el. You can look at 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.


Next: , Previous: , Up: Modes   [Contents][Index]