Call imagemagick-register-types automatically.
authorChong Yidong <cyd@gnu.org>
Mon, 16 Apr 2012 03:47:43 +0000 (11:47 +0800)
committerChong Yidong <cyd@gnu.org>
Mon, 16 Apr 2012 03:47:43 +0000 (11:47 +0800)
* lisp/image.el (imagemagick--extension-regexp): New variable.
(imagemagick-register-types): Use it.
(imagemagick-types-inhibit): Add :set function.  Allow new value
of t to inhibit all types.

* lisp/loadup.el (fboundp): Preload regexp-opt, needed by
imagemagick-register-types.

* lisp/emacs-lisp/regexp-opt.el (regexp-opt-charset): Avoid cl macros,
so we can preload it.

etc/NEWS
lisp/ChangeLog
lisp/emacs-lisp/regexp-opt.el
lisp/image.el
lisp/loadup.el

index 56a0c9b..03ec540 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -35,6 +35,14 @@ been adding them there, put them somewhere else, eg site-lisp.
 ** If your Emacs was built from a bzr checkout, the new variable
 `emacs-bzr-version' contains information about which bzr revision was used.
 
+** ImageMagick support, if available, is automatically enabled.
+It is no longer necessary to call `imagemagick-register-types'
+explicitly to install ImageMagick image types; that function is called
+automatically when setting `imagemagick-types-inhibit'.
+
+*** Setting `imagemagick-types-inhibit' to t now disables the use of
+ImageMagick to view images, set
+
 \f
 * Editing Changes in Emacs 24.2
 
index a21d584..f50af02 100644 (file)
@@ -1,3 +1,16 @@
+2012-04-16  Chong Yidong  <cyd@gnu.org>
+
+       * image.el (imagemagick--extension-regexp): New variable.
+       (imagemagick-register-types): Use it.
+       (imagemagick-types-inhibit): Add :set function.  Allow new value
+       of t to inhibit all types.
+
+       * emacs-lisp/regexp-opt.el (regexp-opt-charset): Avoid cl macros,
+       so we can preload it.
+
+       * loadup.el (fboundp): Preload regexp-opt, needed by
+       imagemagick-register-types.
+
 2012-04-15  Chong Yidong  <cyd@gnu.org>
 
        * frame.el (scrolling): Remove nearly unused customization group.
index 6d12fe1..72e3c39 100644 (file)
@@ -136,9 +136,6 @@ This means the number of non-shy regexp grouping constructs
 \f
 ;;; Workhorse functions.
 
-(eval-when-compile
-  (require 'cl))
-
 (defun regexp-opt-group (strings &optional paren lax)
   "Return a regexp to match a string in the sorted list STRINGS.
 If PAREN non-nil, output regexp parentheses around returned regexp.
@@ -248,15 +245,15 @@ Merges keywords to avoid backtracking in Emacs's regexp matcher."
     ;;
     ;; Make a character map but extract character set meta characters.
     (dolist (char chars)
-      (case char
-       (?\]
-        (setq bracket "]"))
-       (?^
-        (setq caret "^"))
-       (?-
-        (setq dash "-"))
-       (otherwise
-        (aset charmap char t))))
+      (cond
+       ((eq char ?\])
+       (setq bracket "]"))
+       ((eq char ?^)
+       (setq caret "^"))
+       ((eq char ?-)
+       (setq dash "-"))
+       (t
+       (aset charmap char t))))
     ;;
     ;; Make a character set from the map using ranges where applicable.
     (map-char-table
@@ -268,14 +265,14 @@ Merges keywords to avoid backtracking in Emacs's regexp matcher."
                   (setq charset (format "%s%c-%c" charset start end))
                 (while (>= end start)
                   (setq charset (format "%s%c" charset start))
-                  (incf start)))
+                  (setq start (1+ start))))
               (setq start (car c) end (cdr c)))
           (if (= (1- c) end) (setq end c)
             (if (> end (+ start 2))
               (setq charset (format "%s%c-%c" charset start end))
             (while (>= end start)
               (setq charset (format "%s%c" charset start))
-              (incf start)))
+              (setq start (1+ start))))
             (setq start c end c)))))
      charmap)
     (when (>= end start)
@@ -283,7 +280,7 @@ Merges keywords to avoid backtracking in Emacs's regexp matcher."
          (setq charset (format "%s%c-%c" charset start end))
        (while (>= end start)
          (setq charset (format "%s%c" charset start))
-         (incf start))))
+         (setq start (1+ start)))))
     ;;
     ;; Make sure a caret is not first and a dash is first or last.
     (if (and (string-equal charset "") (string-equal bracket ""))
index b094f24..348c208 100644 (file)
@@ -685,26 +685,16 @@ The minimum delay between successive frames is 0.01s."
                      image n count time-elapsed limit))))
 
 \f
-(defcustom imagemagick-types-inhibit
-  '(C HTML HTM TXT PDF)
-  "ImageMagick types that should not be visited in Image mode.
-This should be a list of symbols, each of which should be one of
-the ImageMagick types listed in `imagemagick-types'.  These image
-types are not registered by `imagemagick-register-types'.
-
-If Emacs is compiled without ImageMagick support, this variable
-has no effect."
-  :type '(choice (const :tag "Let ImageMagick handle all types it can" nil)
-                (repeat symbol))
-  ;; Ideally, would have a :set function that checks if we already did
-  ;; imagemagick-register-types, and if so undoes it, then redoes it.
-  :version "24.1"
-  :group 'image)
+(defvar imagemagick--file-regexp nil
+  "File extension regexp for ImageMagick files, if any.
+This is the extension installed into `auto-mode-alist' and
+`image-type-file-name-regexps' by `imagemagick-register-types'.")
 
 ;;;###autoload
 (defun imagemagick-register-types ()
   "Register file types that can be handled by ImageMagick.
-This registers the ImageMagick types listed in `imagemagick-types',
+This function is called at startup, after loading the init file.
+It registers the ImageMagick types listed in `imagemagick-types',
 excluding those listed in `imagemagick-types-inhibit'.
 
 Registered image types are added to `auto-mode-alist', so that
@@ -714,14 +704,45 @@ recognizes these files as having image type `imagemagick'.
 
 If Emacs is compiled without ImageMagick support, do nothing."
   (when (fboundp 'imagemagick-types)
-    (let ((im-types '()))
-      (dolist (im-type (imagemagick-types))
-        (unless (memq im-type imagemagick-types-inhibit)
-          (push (downcase (symbol-name im-type)) im-types)))
-      (let ((extension (concat "\\." (regexp-opt im-types) "\\'")))
-        (push (cons extension 'image-mode) auto-mode-alist)
-        (push (cons extension 'imagemagick)
-              image-type-file-name-regexps)))))
+    (let ((re (if (eq imagemagick-types-inhibit t)
+                 ;; Use a bogus regexp to inhibit matches.
+                 "\\'a"
+               (let ((types))
+                 (dolist (type (imagemagick-types))
+                   (unless (memq type imagemagick-types-inhibit)
+                     (push (downcase (symbol-name type)) types)))
+                 (concat "\\." (regexp-opt types) "\\'"))))
+         (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode)
+                               auto-mode-alist)))
+         (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick)
+                                 image-type-file-name-regexps))))
+      (if ama-elt
+         (setcar ama-elt re)
+       (push (cons re 'image-mode) auto-mode-alist))
+      (if itfnr-elt
+         (setcar itfnr-elt re)
+       (push (cons re 'imagemagick) image-type-file-name-regexps))
+      (setq imagemagick--file-regexp re))))
+
+(defcustom imagemagick-types-inhibit
+  '(C HTML HTM TXT PDF)
+  "List of ImageMagick types that should not be treated as images.
+This should be a list of symbols, each of which should be one of
+the ImageMagick types listed in `imagemagick-types'.  The listed
+image types are not registered by `imagemagick-register-types'.
+
+If the value is t, inhibit the use of ImageMagick for images.
+
+If Emacs is compiled without ImageMagick support, this variable
+has no effect."
+  :type '(choice (const :tag "Support all ImageMagick types" nil)
+                (const :tag "Disable all ImageMagick types" t)
+                (repeat symbol))
+  :set (lambda (symbol value)
+        (set-default symbol value)
+        (imagemagick-register-types))
+  :version "24.1"
+  :group 'image)
 
 (provide 'image)
 
index 2ba24c8..f7ffa27 100644 (file)
 (if (fboundp 'x-create-frame)
     (progn
       (load "fringe")
+      ;; Needed by `imagemagick-register-types'
+      (load "emacs-lisp/regexp-opt")
       (load "image")
       (load "international/fontset")
       (load "dnd")