(lispref.info)Auto Major Mode
Next: Mode Help Prev: Example Major Modes Up: Major Modes
How Emacs Chooses a Major Mode
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
- Command: fundamental-mode
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-mode' function
does *not* run any hooks, so it is not readily customizable.
- Command: normal-mode &optional FIND-FILE
This function establishes the proper major mode and local variable
bindings for the current buffer. First it calls `set-auto-mode',
then it runs `hack-local-variables' to parse, and bind or evaluate
as appropriate, any local variables.
If the FIND-FILE argument to `normal-mode' is non-`nil',
`normal-mode' assumes that the `find-file' function is calling it.
In this case, it may process a local variables list at the end of
the file. The variable `enable-local-variables' controls whether
to do so.
If you run `normal-mode' yourself, the argument FIND-FILE is
normally `nil'. In this case, `normal-mode' unconditionally
processes any local variables list. Note: Local Variables in
Files, for the syntax of the local
variables section of a file.
`normal-mode' uses `condition-case' around 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.
- User Option: enable-local-variables
This variable controls processing of local variables lists in files
being visited. A value of `t' means process the local variables
lists unconditionally; `nil' means ignore them; anything else means
ask the user what to do for each file. The default value is `t'.
- User Option: enable-local-eval
This variable controls processing of `Eval:' in local variables
lists in files being visited. A value of `t' means process them
unconditionally; `nil' means ignore them; anything else means ask
the user what to do for each file. The default value is `maybe'.
- Function: set-auto-mode
This function selects the major mode that is appropriate for the
current buffer. It may base its decision on the value of the `-*-'
line, on the visited file name (using `auto-mode-alist'), or on the
value of a local variable). However, this function does not look
for the `mode:' local variable near the end of a file; the
`hack-local-variables' function does that. Note: How Major Modes
- User Option: default-major-mode
This variable holds the default major mode for new buffers. The
standard value is `fundamental-mode'.
If the value of `default-major-mode' is `nil', Emacs uses the
(previously) current buffer's major mode for the major mode of a
new buffer. However, if the major mode symbol has a `mode-class'
property with value `special', 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.
- Variable: initial-major-mode
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 name. The default value is `lisp-interaction-mode'.
- Variable: auto-mode-alist
This variable contains an association list of file name patterns
(regular expressions; Note: Regular Expressions.) and
corresponding major mode functions. Usually, the file name
patterns test for suffixes, such as `.el' and `.c', but this need
not be the case. Each element of the alist looks like `(REGEXP .
(("^/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 (Note: File Name
Expansion.) matches a REGEXP, `set-auto-mode' calls the
corresponding MODE-FUNCTION. This feature enables Emacs to select
the proper major mode for most files.
Here is an example of how to prepend several pattern pairs to
`auto-mode-alist'. (You might use this sort of expression in your
;; Filename starts with a dot.
'(("/\\.[^/]*$" . fundamental-mode)
;; Filename has no dot.
("[^\\./]*$" . fundamental-mode)
("\\.C$" . c++-mode))
- Function: hack-local-variables &optional FORCE
This function parses, and binds or evaluates as appropriate, any
local variables for the current buffer.
The handling of `enable-local-variables' documented for
`normal-mode' actually takes place here. The argument FORCE
reflects the argument FIND-FILE given to `normal-mode'.
automatically generated by info2www