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


10.3 File Modes

When you visit a file, Emacs chooses a major mode automatically. Normally, it makes the choice based on the file name. Sometimes it chooses the major mode based on special text in the file. This special text can also be used to enable buffer-local minor modes.

  1. First, Emacs checks whether the file contains file-local mode variables.
    • If there is a file-local variable that specifies a major mode, then Emacs uses that major mode, ignoring all other criteria.
    • There are several methods to specify a major mode using a file-local variable;
      • ; -*-Lisp-*-
      • ; -*- mode: Lisp; -*-
    • You can also use file-local variables to specify buffer-local minor modes, by using eval specifications.
      • ; -*- mode: Lisp; eval: (auto-fill-mode 1); -*-
    • Note, however, that it is usually inappropriate to enable minor modes this way, since most minor modes represent individual user preferences. If you personally want to use a minor mode for a particular file type, it is better to enable the minor mode via a major mode hook.
  2. Second, if there is no file variable specifying a major mode, Emacs checks whether the file’s contents begin with ‘#!’.
    • If so, that indicates that the file can serve as an executable shell command, which works by running an interpreter named on the file’s first line (the rest of the file is used as input to the interpreter).
    • Emacs tries to use the interpreter name to choose a mode.
    • The variable interpreter-mode-alist specifies the correspondence between interpreter program names and major modes.
    • When the first line starts with ‘#!’, you usually cannot use the ‘-*-’ feature on the first line, because the system would get confused when running the interpreter. So Emacs looks for ‘-*-’ on the second line in such files as well as on the first line.
  3. Third, Emacs tries to determine the major mode by looking at the text at the start of the buffer, based on the variable magic-mode-alist.
  4. Fourth it looks at the file’s name. The correspondence between file names and major modes is controlled by the variable auto-mode-alist.
  5. Finally, if Emacs still hasn’t found a major mode to use, it compares the text at the start of the buffer to the variable magic-fallback-mode-alist.
    • This variable works like magic-mode-alist, described above, except that it is consulted only after auto-mode-alist.
    • By default, magic-fallback-mode-alist contains forms that check for image files, HTML/XML/SGML files, PostScript files, and Unix style Conf files.

interpreter-mode-alist

Variable: interpreter-mode-alist

Alist mapping interpreter names to major modes.

See also auto-mode-alist.

auto-mode-alist

Variable: auto-mode-alist

Alist of filename patterns vs corresponding major mode functions.

Each element looks like

(REGEXP  . FUNCTION)

or

(REGEXP FUNCTION NON-NIL).

(‘NON-NIL’ stands for anything that is not ‘nil’; the value does not matter.)

Visiting a file whose name matches ‘REGEXP’ specifies ‘FUNCTION’ as the mode function to use. ‘FUNCTION’ will be called, unless it is ‘nil’.

If the element has the form

(REGEXP FUNCTION NON-NIL)

then after calling ‘FUNCTION’ (if it’s not nil), we delete the suffix that matched ‘REGEXP’ and search the list again for another match.

See also magic-mode-alist.

magic-mode-alist

Variable: magic-mode-alist

Alist of buffer beginnings vs. corresponding major mode functions. Each element looks like

(REGEXP  . FUNCTION)

or

(MATCH-FUNCTION  . FUNCTION).

After visiting a file, if ‘REGEXP’ matches the text at the beginning of the buffer (case-sensitively), or calling ‘MATCH-FUNCTION’ returns non-‘nil’, ‘normal-mode’ will call ‘FUNCTION’ rather than allowing auto-mode-alist to decide the buffer’s major mode.

If ‘FUNCTION’ is ‘nil’, then it is not called. (That is a way of saying "allow ‘auto-mode-alist’ to decide for these files.")

It’s default value is ‘nil’.

magic-fallback-mode-alist

Variable: magic-fallback-mode-alist

Like ‘magic-mode-alist’ but has lower priority than auto-mode-alist.


Next: Text Mode, Previous: Minor Modes, Up: Modes   [Index]