comparison lisp/files.el @ 74740:46f8a3942acb

(magic-mode-alist): Allow matching file type by calling a function at bob. Check for image types by calling image-type-from-buffer. Suggested by Juanma Barranquero. (set-auto-mode): Do it.
author Kim F. Storm <storm@cua.dk>
date Tue, 19 Dec 2006 09:04:30 +0000
parents 2a54a5dd0df4
children 7f729944fa45
comparison
equal deleted inserted replaced
74739:fbc3c1505544 74740:46f8a3942acb
2113 is assumed to be interpreted by the interpreter matched by the second group 2113 is assumed to be interpreted by the interpreter matched by the second group
2114 of the regular expression. The mode is then determined as the mode 2114 of the regular expression. The mode is then determined as the mode
2115 associated with that interpreter in `interpreter-mode-alist'.") 2115 associated with that interpreter in `interpreter-mode-alist'.")
2116 2116
2117 (defvar magic-mode-alist 2117 (defvar magic-mode-alist
2118 `(;; The < comes before the groups (but the first) to reduce backtracking. 2118 `((image-type-from-buffer . image-mode)
2119 ;; The < comes before the groups (but the first) to reduce backtracking.
2119 ;; TODO: UTF-16 <?xml may be preceded by a BOM 0xff 0xfe or 0xfe 0xff. 2120 ;; TODO: UTF-16 <?xml may be preceded by a BOM 0xff 0xfe or 0xfe 0xff.
2120 ;; We use [ \t\n] instead of `\\s ' to make regex overflow less likely. 2121 ;; We use [ \t\n] instead of `\\s ' to make regex overflow less likely.
2121 (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)") 2122 (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
2122 (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\n]*<\\)"))) 2123 (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\n]*<\\)")))
2123 (concat "\\(?:<\\?xml[ \t\n]+[^>]*>\\)?[ \t\n]*<" 2124 (concat "\\(?:<\\?xml[ \t\n]+[^>]*>\\)?[ \t\n]*<"
2132 (concat "[ \t\n]*<" comment-re "*!DOCTYPE ")) 2133 (concat "[ \t\n]*<" comment-re "*!DOCTYPE "))
2133 . sgml-mode) 2134 . sgml-mode)
2134 ("%![^V]" . ps-mode) 2135 ("%![^V]" . ps-mode)
2135 ("# xmcd " . conf-unix-mode)) 2136 ("# xmcd " . conf-unix-mode))
2136 "Alist of buffer beginnings vs. corresponding major mode functions. 2137 "Alist of buffer beginnings vs. corresponding major mode functions.
2137 Each element looks like (REGEXP . FUNCTION). After visiting a file, 2138 Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION).
2138 if REGEXP matches the text at the beginning of the buffer, 2139 After visiting a file, if REGEXP matches the text at the beginning of the
2139 `normal-mode' will call FUNCTION rather than allowing `auto-mode-alist' 2140 buffer, or calling MATCH-FUNCTION returns non-nil, `normal-mode' will
2140 to decide the buffer's major mode. 2141 call FUNCTION rather than allowing `auto-mode-alist' to decide the buffer's
2142 major mode.
2141 2143
2142 If FUNCTION is nil, then it is not called. (That is a way of saying 2144 If FUNCTION is nil, then it is not called. (That is a way of saying
2143 \"allow `auto-mode-alist' to decide for these files.\")") 2145 \"allow `auto-mode-alist' to decide for these files.\")")
2144 (put 'magic-mode-alist 'risky-local-variable t) 2146 (put 'magic-mode-alist 'risky-local-variable t)
2145 2147
2223 (narrow-to-region (point-min) 2225 (narrow-to-region (point-min)
2224 (min (point-max) 2226 (min (point-max)
2225 (+ (point-min) magic-mode-regexp-match-limit))) 2227 (+ (point-min) magic-mode-regexp-match-limit)))
2226 (assoc-default nil magic-mode-alist 2228 (assoc-default nil magic-mode-alist
2227 (lambda (re dummy) 2229 (lambda (re dummy)
2228 (looking-at re)))))) 2230 (if (functionp re)
2231 (funcall re)
2232 (looking-at re)))))))
2229 (set-auto-mode-0 done keep-mode-if-same) 2233 (set-auto-mode-0 done keep-mode-if-same)
2230 ;; Compare the filename against the entries in auto-mode-alist. 2234 ;; Compare the filename against the entries in auto-mode-alist.
2231 (if buffer-file-name 2235 (if buffer-file-name
2232 (let ((name buffer-file-name)) 2236 (let ((name buffer-file-name))
2233 ;; Remove backup-suffixes from file name. 2237 ;; Remove backup-suffixes from file name.