emacs-pretest-bug
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: python-mode.el doesn't associate python-mode with .PY files


From: Juanma Barranquero
Subject: Re: python-mode.el doesn't associate python-mode with .PY files
Date: Thu, 28 Dec 2006 13:33:20 +0100

On 12/28/06, Juanma Barranquero <address@hidden> wrote:

I think a good approach would be to add Stefan patch, remove the
NOT-ALWAYS stuff and fix `magic-mode-alist' to use a function
specifically designed to detect image types.

(I should've said "to detect image files").

The following is an example patch which removes the NOT-ALWAYS stuff,
decoupling image autodetection from auto-determination of image-mode
for image files.

It uses a separate `image-type-auto-detectable' alist (which makes
more sense IMO) and a new `image-type-auto-detected-p' function for
`magic-mode-alist'. (I'm not particularly fond of the names, BTW.)

                   /L/e/k/t/u



Index: lisp/files.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/files.el,v
retrieving revision 1.870
diff -u -2 -r1.870 files.el
--- lisp/files.el       19 Dec 2006 09:04:30 -0000      1.870
+++ lisp/files.el       28 Dec 2006 11:27:20 -0000
@@ -2116,5 +2116,5 @@

 (defvar magic-mode-alist
-  `((image-type-from-buffer . image-mode)
+  `((image-type-auto-detected-p . image-mode)
    ;; The < comes before the groups (but the first) to reduce backtracking.
    ;; TODO: UTF-16 <?xml may be preceded by a BOM 0xff 0xfe or 0xfe 0xff.
Index: lisp/image.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/image.el,v
retrieving revision 1.65
diff -u -2 -r1.65 image.el
--- lisp/image.el       26 Dec 2006 18:01:40 -0000      1.65
+++ lisp/image.el       28 Dec 2006 12:12:45 -0000
@@ -35,25 +35,18 @@

(defconst image-type-header-regexps
-  '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" xpm)
-    ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+"
pbm)
-    ("\\`GIF8" gif)
-    ("\\`\x89PNG\r\n\x1a\n" png)
-    ("\\`[\t\n\r ]*#define" xbm)
-    ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" tiff)
-    ("\\`[\t\n\r ]*%!PS" postscript t)
-    ("\\`\xff\xd8" (image-jpeg-p . jpeg)))
-  "Alist of (REGEXP IMAGE-TYPE) pairs used to auto-detect image types.
+  '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
+    ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+"
. pbm)
+    ("\\`GIF8" . gif)
+    ("\\`\x89PNG\r\n\x1a\n" . png)
+    ("\\`[\t\n\r ]*#define" . xbm)
+    ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
+    ("\\`[\t\n\r ]*%!PS" . postscript)
+    ("\\`\xff\xd8" . (image-jpeg-p . jpeg)))
+  "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
When the first bytes of an image file match REGEXP, it is assumed to
-be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol.
-
-An element can also be (REGEXP IMAGE-TYPE NOT-ALWAYS).  If
-NOT-ALWAYS is non-nil, that means that REGEXP identifies a
-file that _can_ be treated as an image of type IMAGE-TYPE,
-but such files should not be spontaneously treated as images.
-
-IMAGE-TYPE can be a pair (PREDICATE . TYPE) instead of a
-symbol.  Then PREDICATE is called with one argument, a
-string containing the image data.  If PREDICATE returns a
-non-nil value, TYPE is the image's type.")
+be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol.  If not a symbol,
+IMAGE-TYPE must be a pair (PREDICATE . TYPE).  PREDICATE is called
+with one argument, a string containing the image data.  If PREDICATE returns
+a non-nil value, TYPE is the image's type.")

(defconst image-type-file-name-regexps
@@ -71,4 +64,22 @@
be of image type IMAGE-TYPE.")

+(defconst image-type-auto-detectable
+  '((pbm . t)
+    (xbm . t)
+    (bmp . maybe)
+    (gif . maybe)
+    (png . maybe)
+    (xpm . maybe)
+    (jpeg . maybe)
+    (tiff . maybe)
+    (postscript . nil))
+  "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
+\(See `image-type-auto-detected-p').
+
+AUTODETECT can be
+ - t      always auto-detect.
+ - nil    never auto-detect.
+ - maybe  auto-detect only if the image type is available
+            (see `image-type-available-p').")

 (defvar image-load-path nil
@@ -213,5 +224,5 @@
    (while types
      (let ((regexp (car (car types)))
-           (image-type (nth 1 (car types))))
+           (image-type (cdr (car types))))
        (if (or (and (symbolp image-type)
                     (string-match regexp data))
@@ -226,12 +237,8 @@

;;;###autoload
-(defun image-type-from-buffer (&optional include-maybes)
+(defun image-type-from-buffer ()
  "Determine the image type from data in the current buffer.
-Value is a symbol specifying the image type, or nil if none
-corresponds to the buffer contents.
-
-If INCLUDE-MAYBES is nil (the default), we return nil for
-file types that should not always be treated as images
-even though they can be so treated."
+Value is a symbol specifying the image type or nil if type cannot
+be determined."
  (let ((types image-type-header-regexps)
        type
@@ -240,6 +247,5 @@
    (while types
      (let ((regexp (car (car types)))
-           (image-type (nth 1 (car types)))
-           (not-always (nth 2 (car types)))
+           (image-type (cdr (car types)))
            data)
        (if (or (and (symbolp image-type)
@@ -254,8 +260,5 @@
                                              (+ (point-min) 256))))))
                     (setq image-type (cdr image-type))))
-           ;; If this entry says "not always",
-           ;; treat it as nil, unless INCLUDE-MAYBES is t.
-           (setq type (if (or include-maybes (not not-always))
-                          image-type)
+           (setq type image-type
                  types nil)
          (setq types (cdr types)))))
@@ -277,5 +280,5 @@
         (set-buffer-multibyte nil)
         (insert-file-contents-literally file nil 0 256)
-        (image-type-from-buffer t))))
+        (image-type-from-buffer))))


@@ -333,4 +336,17 @@

;;;###autoload
+(defun image-type-auto-detected-p ()
+  "Return t iff the current buffer contains an auto-detectable image.
+Whether image types are auto-detectable or not depends on the setting
+of the variable `image-type-auto-detectable'.
+
+This function is intended to be used from `magic-mode-alist' (which see)."
+  (let* ((type (image-type-from-buffer))
+        (auto (and type (cdr (assq type image-type-auto-detectable)))))
+    (and auto
+        (or (eq auto t)
+            (image-type-available-p type)))))
+
+;;;###autoload
(defun create-image (file-or-data &optional type data-p &rest props)
  "Create an image.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]