Function: set-auto-mode--apply-alist

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

Signature

(set-auto-mode--apply-alist ALIST KEEP-MODE-IF-SAME DIR-LOCAL)

Documentation

Helper function for set-auto-mode.

This function takes an alist of the same form as auto-mode-alist. It then tries to find the appropriate match in the alist for the current buffer; setting the mode if possible. Return non-nil if the mode was set, nil otherwise. DIR-LOCAL non-nil means this call is via directory-locals, and extra checks should be done.

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun set-auto-mode--apply-alist (alist keep-mode-if-same dir-local)
  "Helper function for `set-auto-mode'.
This function takes an alist of the same form as
`auto-mode-alist'.  It then tries to find the appropriate match
in the alist for the current buffer; setting the mode if
possible.
Return non-nil if the mode was set, nil otherwise.
DIR-LOCAL non-nil means this call is via directory-locals, and
extra checks should be done."
  (if buffer-file-name
      (let (mode
            (name buffer-file-name)
            (remote-id (file-remote-p buffer-file-name))
            (case-insensitive-p (file-name-case-insensitive-p
                                 buffer-file-name)))
        ;; Remove backup-suffixes from file name.
        (setq name (file-name-sans-versions name))
        ;; Remove remote file name identification.
        (when (and (stringp remote-id)
                   (string-match (regexp-quote remote-id) name))
          (setq name (substring name (match-end 0))))
        (while name
          ;; Find first matching alist entry.
          (setq mode
                (if case-insensitive-p
                    ;; Filesystem is case-insensitive.
                    (let ((case-fold-search t))
                      (assoc-default name alist 'string-match))
                  ;; Filesystem is case-sensitive.
                  (or
                   ;; First match case-sensitively.
                   (let ((case-fold-search nil))
                     (assoc-default name alist 'string-match))
                   ;; Fallback to case-insensitive match.
                   (and auto-mode-case-fold
                        (let ((case-fold-search t))
                          (assoc-default name alist 'string-match))))))
          (if (and mode
                   (consp mode)
                   (cadr mode))
              (setq mode (car mode)
                    name (substring name 0 (match-beginning 0)))
            (setq name nil)))
        (when (and dir-local mode
                   (not (set-auto-mode--dir-local-valid-p mode)))
          (message "Ignoring invalid mode `%s'" mode)
          (setq mode nil))
        (when mode
          (set-auto-mode-0 mode keep-mode-if-same)
          t))))