24.2 Major Modes

Major modes specialize Emacs for editing or interacting with particular kinds of text. Each buffer has exactly one major mode at a time. Every major mode is associated with a major mode command, whose name should end in ‘-mode’. This command takes care of switching to that mode in the current buffer, by setting various buffer-local variables such as a local keymap. See Major Mode Conventions. Note that unlike minor modes there is no way to “turn off” a major mode, instead the buffer must be switched to a different one. However, you can temporarily suspend a major mode and later restore the suspended mode, see below.

The least specialized major mode is called Fundamental mode, which has no mode-specific definitions or variable settings.

Command: fundamental-mode

This is the major mode command for Fundamental mode. Unlike other mode commands, it does not run any mode hooks (see Major Mode Conventions), since you are not supposed to customize this mode.

Function: major-mode-suspend

This function works like fundamental-mode, in that it kills all buffer-local variables, but it also records the major mode in effect, so that it could subsequently be restored. This function and major-mode-restore (described next) are useful when you need to put a buffer under some specialized mode other than the one Emacs chooses for it automatically (see How Emacs Chooses a Major Mode), but would also like to be able to switch back to the original mode later.

Function: major-mode-restore &optional avoided-modes

This function restores the major mode recorded by major-mode-suspend. If no major mode was recorded, this function calls normal-mode (see normal-mode), but tries to force it not to choose any modes in avoided-modes, if that argument is non-nil.

Function: clean-mode

Changing the major mode clears out most local variables, but it doesn’t remove all artifacts in the buffer (like text properties and overlays). It’s rare to change a buffer from one major mode to another (except from fundamental-mode to everything else), so this is usually not a concern. It can sometimes be convenient (mostly when debugging a problem in a buffer) to do a “full reset” of the buffer, and that’s what the clean-mode major mode offers. It will kill all local variables (even the permanently local ones), and also removes all overlays and text properties.

The easiest way to write a major mode is to use the macro define-derived-mode, which sets up the new mode as a variant of an existing major mode. See Defining Derived Modes. We recommend using define-derived-mode even if the new mode is not an obvious derivative of another mode, as it automatically enforces many coding conventions for you. See Basic Major Modes, for common modes to derive from.

The standard GNU Emacs Lisp directory tree contains the code for several major modes, in files such as text-mode.el, texinfo.el, lisp-mode.el, and rmail.el. You can study these libraries to see how modes are written.

User Option: major-mode

The buffer-local value of this variable holds the symbol for the current major mode. Its default value holds the default major mode for new buffers. The standard default value is fundamental-mode.

If the default value is nil, then whenever Emacs creates a new buffer via a command such as C-x b (switch-to-buffer), the new buffer is put in the major mode of the previously current buffer. As an exception, if the major mode of the previous buffer has a mode-class symbol property with value special, the new buffer is put in Fundamental mode (see Major Mode Conventions).