Next: , Previous: Hooks, Up: Modes

23.2 Major Modes

Major modes specialize Emacs for editing particular kinds of text. Each buffer has 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.

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.

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 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).