Function: normal-mode

normal-mode is an interactive and byte-compiled function defined in files.el.gz.

Signature

(normal-mode &optional FIND-FILE)

Documentation

Choose the major mode for this buffer automatically.

Also sets up any specified local variables of the file or its directory. Uses the visited file name, the -*- line, and the local variables spec.

This function is called automatically from find-file. In that case, we may set up the file-specified mode and local variables, depending on the value of enable-local-variables. In addition, if local-enable-local-variables is nil, we do not set local variables (though we do notice a mode specified with -*-.)

enable-local-variables is ignored if you run normal-mode interactively, or from Lisp without specifying the optional argument FIND-FILE; in that case, this function acts as if enable-local-variables were t.

If invoked in a buffer that doesn't visit a file, this function processes only the major mode specification in the -*- line and the local variables spec.

View in manual

Probably introduced at or before Emacs version 17.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun normal-mode (&optional find-file)
  "Choose the major mode for this buffer automatically.
Also sets up any specified local variables of the file or its directory.
Uses the visited file name, the -*- line, and the local variables spec.

This function is called automatically from `find-file'.  In that case,
we may set up the file-specified mode and local variables,
depending on the value of `enable-local-variables'.
In addition, if `local-enable-local-variables' is nil, we do
not set local variables (though we do notice a mode specified with -*-.)

`enable-local-variables' is ignored if you run `normal-mode' interactively,
or from Lisp without specifying the optional argument FIND-FILE;
in that case, this function acts as if `enable-local-variables' were t.

If invoked in a buffer that doesn't visit a file, this function
processes only the major mode specification in the -*- line and
the local variables spec."
  (interactive)
  (kill-all-local-variables)
  (unless delay-mode-hooks
    (run-hooks 'change-major-mode-after-body-hook
               'after-change-major-mode-hook))
  (let ((enable-local-variables (or (not find-file) enable-local-variables)))
    ;; FIXME this is less efficient than it could be, since both
    ;; s-a-m and h-l-v may parse the same regions, looking for "mode:".
    (with-demoted-errors "File mode specification error: %s"
      (set-auto-mode))
    ;; `delay-mode-hooks' being non-nil will have prevented the major
    ;; mode's call to `run-mode-hooks' from calling
    ;; `hack-local-variables'.  In that case, call it now.
    (when delay-mode-hooks
      (with-demoted-errors "File local-variables error: %s"
        (hack-local-variables 'no-mode))))
  ;; Turn font lock off and on, to make sure it takes account of
  ;; whatever file local variables are relevant to it.
  (when (and font-lock-mode
             ;; Font-lock-mode (now in font-core.el) can be ON when
             ;; font-lock.el still hasn't been loaded.
             (boundp 'font-lock-keywords)
             (eq (car font-lock-keywords) t))
    (setq font-lock-keywords (cadr font-lock-keywords))
    (font-lock-mode 1)))