Next: Mode Help, Previous: Major Mode Conventions, Up: Major Modes
Based on information in the file name or in the file itself, Emacs automatically selects a major mode for the new buffer when a file is visited. It also processes local variables specified in the file text.
Fundamental mode is a major mode that is not specialized for anything in particular. Other major modes are defined in effect by comparison with this one—their definitions say what to change, starting from Fundamental mode. The
fundamental-modefunction does not run any mode hooks; you're not supposed to customize it. (If you want Emacs to behave differently in Fundamental mode, change the global state of Emacs.)
This function establishes the proper major mode and buffer-local variable bindings for the current buffer. First it calls
set-auto-mode(see below), then it runshack-local-variablesto parse, and bind or evaluate as appropriate, the file's local variables (see File Local Variables).If the find-file argument to
normal-modeis non-nil,normal-modeassumes that thefind-filefunction is calling it. In this case, it may process local variables in the `-*-' line or at the end of the file. The variableenable-local-variablescontrols whether to do so. See Local Variables in Files, for the syntax of the local variables section of a file.If you run
normal-modeinteractively, the argument find-file is normallynil. In this case,normal-modeunconditionally processes any file local variables.If
normal-modeprocesses the local variables list and this list specifies a major mode, that mode overrides any mode chosen byset-auto-mode. If neitherset-auto-modenorhack-local-variablesspecify a major mode, the buffer stays in the major mode determined bydefault-major-mode(see below).
normal-modeusescondition-casearound the call to the major mode function, so errors are caught and reported as a `File mode specification error', followed by the original error message.
This function selects the major mode that is appropriate for the current buffer. It bases its decision (in order of precedence) on the `-*-' line, on the `#!' line (using
interpreter-mode-alist), on the text at the beginning of the buffer (usingmagic-mode-alist), and finally on the visited file name (usingauto-mode-alist). See How Major Modes are Chosen. However, this function does not look for the `mode:' local variable near the end of a file; thehack-local-variablesfunction does that. Ifenable-local-variablesisnil,set-auto-modedoes not check the `-*-' line for a mode tag either.If keep-mode-if-same is non-
nil, this function does not call the mode command if the buffer is already in the proper major mode. For instance,set-visited-file-namesets this totto avoid killing buffer local variables that the user may have set.
This variable holds the default major mode for new buffers. The standard value is
fundamental-mode.If the value of
default-major-modeisnil, Emacs uses the (previously) current buffer's major mode as the default major mode of a new buffer. However, if that major mode symbol has amode-classproperty with valuespecial, then it is not used for new buffers; Fundamental mode is used instead. The modes that have this property are those such as Dired and Rmail that are useful only with text that has been specially prepared.
This function sets the major mode of buffer to the value of
default-major-mode; if that variable isnil, it uses the current buffer's major mode (if that is suitable). As an exception, if buffer's name is `*scratch*', it sets the mode toinitial-major-mode.The low-level primitives for creating buffers do not use this function, but medium-level commands such as
switch-to-bufferandfind-file-noselectuse it whenever they create buffers.
The value of this variable determines the major mode of the initial `*scratch*' buffer. The value should be a symbol that is a major mode command. The default value is
lisp-interaction-mode.
This variable specifies major modes to use for scripts that specify a command interpreter in a `#!' line. Its value is an alist with elements of the form
(interpreter.mode); for example,("perl" . perl-mode)is one element present by default. The element says to use mode mode if the file specifies an interpreter which matches interpreter.
This variable's value is an alist with elements of the form
(regexp.function), where regexp is a regular expression and function is a function ornil. After visiting a file,set-auto-modecalls function if the text at the beginning of the buffer matches regexp and function is non-nil; if function isnil,auto-mode-alistgets to decide the mode.
This variable contains an association list of file name patterns (regular expressions) and corresponding major mode commands. Usually, the file name patterns test for suffixes, such as `.el' and `.c', but this need not be the case. An ordinary element of the alist looks like
(regexp.mode-function).For example,
(("\\`/tmp/fol/" . text-mode) ("\\.texinfo\\'" . texinfo-mode) ("\\.texi\\'" . texinfo-mode) ("\\.el\\'" . emacs-lisp-mode) ("\\.c\\'" . c-mode) ("\\.h\\'" . c-mode) ...)When you visit a file whose expanded file name (see File Name Expansion), with version numbers and backup suffixes removed using
file-name-sans-versions(see File Name Components), matches a regexp,set-auto-modecalls the corresponding mode-function. This feature enables Emacs to select the proper major mode for most files.If an element of
auto-mode-alisthas the form(regexp functiont), then after calling function, Emacs searchesauto-mode-alistagain for a match against the portion of the file name that did not match before. This feature is useful for uncompression packages: an entry of the form("\\.gz\\'"functiont)can uncompress the file and then put the uncompressed file in the proper mode according to the name sans `.gz'.Here is an example of how to prepend several pattern pairs to
auto-mode-alist. (You might use this sort of expression in your init file.)(setq auto-mode-alist (append ;; File name (within directory) starts with a dot. '(("/\\.[^/]*\\'" . fundamental-mode) ;; File name has no dot. ("[^\\./]*\\'" . fundamental-mode) ;; File name ends in `.C'. ("\\.C\\'" . c++-mode)) auto-mode-alist))