Go to the first, previous, next, last section, table of contents.


Installing Modes

Before a major mode can be used to edit a buffer with it must be installed in that buffer. The most straightforward method of doing this is simply to invoke the mode's command which does this (i.e. c-mode).

It could be a bit annoying to have to this every time a new buffer is created so the mode-alist variable allows major modes to be installed automatically, when the buffer is opened.

Function: init-mode buffer &optional string
This function attempts to install a major mode into buffer. If the major-mode variable is non-nil it defines the function to call to install the mode; this function will be called.

Otherwise the mode-alist variable is searched; each regular expression is matched against a string, when a match occurs the associated function is called to install the mode.

The string matched against is defined by the first of the following choices which is not nil or undefined.

  1. The value of the optional string argument.
  2. The word specified on the first line of the buffer bracketed by the string `-*-'. For example if the first line contained the string `-*-Text-*-' the string `Text' would be used.
  3. The value of the variable mode-name.
  4. The name of the file being edited in the buffer.

Note that each match is case-insensitive.

Variable: mode-alist
An association list (see section Association Lists) defining regular expressions which associate with a particular major mode.

When the init-mode function matches a regular expression to the string it is using to find the mode for the buffer the associated mode is installed.

For example, mode-alist could be,

(("\\.(c|h)$|^c(|-mode)$" . c-mode)
 ("\\.jl$|^lisp(|-mode)$" . lisp-mode)
 ("\\.(text|doc|txt|article|letter)$" . text-mode)
 ("^(text(|-mode)|(.*/|)draft)$" . text-mode)
 ("^indented-text(|-mode)$" . indented-text-mode)
 ("\\.[s]$|^asm(|-mode)$" . asm-mode)
 ("\\.[S]$|^asm-cpp(|-mode)$" . asm-cpp-mode)
 ("\\.texi(|nfo)|^texinfo(|-mode)$" . texinfo-mode))

Function: kill-mode &optional buffer
This function removes the major mode currently installed in the specified buffer.


Go to the first, previous, next, last section, table of contents.