Function: set-auto-mode-0

set-auto-mode-0 is a byte-compiled function defined in files.el.gz.

Signature

(set-auto-mode-0 MODE &optional KEEP-MODE-IF-SAME)

Documentation

Apply MODE and return it.

If optional arg KEEP-MODE-IF-SAME is non-nil, MODE is chased of any aliases and compared to current major mode. If they are the same, do nothing and return :keep. Return nil if MODE could not be applied.

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
;; When `keep-mode-if-same' is set, we are working on behalf of
;; set-visited-file-name.  In that case, if the major mode specified is the
;; same one we already have, don't actually reset it.  We don't want to lose
;; minor modes such as Font Lock.
(defun set-auto-mode-0 (mode &optional keep-mode-if-same)
  "Apply MODE and return it.
If optional arg KEEP-MODE-IF-SAME is non-nil, MODE is chased of
any aliases and compared to current major mode.  If they are the
same, do nothing and return `:keep'.
Return nil if MODE could not be applied."
  (when mode
    (if (and keep-mode-if-same
	     (or (eq (indirect-function mode)
		     (indirect-function major-mode))
		 (and set-auto-mode--last
		      (eq mode (car set-auto-mode--last))
		      (eq major-mode (cdr set-auto-mode--last)))))
	:keep
      (let ((modefun (major-mode-remap mode)))
        (if (not (functionp modefun))
            (progn
              (message "Ignoring unknown mode `%s'%s" mode
                       (if (eq mode modefun) ""
                         (format " (remapped to `%S')" modefun)))
              nil)
          (funcall modefun)
          (unless (or (eq mode major-mode) ;`set-auto-mode--last' is overkill.
                      ;; `modefun' is something like a minor mode.
                      (local-variable-p 'set-auto-mode--last))
            (setq set-auto-mode--last (cons mode major-mode)))
          mode)))))