From c505aaeb00a1c4f01e79f6e82216f83e33a77426 Mon Sep 17 00:00:00 2001 From: Chong Yidong Date: Mon, 16 Apr 2012 11:47:43 +0800 Subject: [PATCH] Call imagemagick-register-types automatically. * 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 | 8 ++++ lisp/ChangeLog | 13 +++++++ lisp/emacs-lisp/regexp-opt.el | 27 ++++++-------- lisp/image.el | 69 +++++++++++++++++++++++------------ lisp/loadup.el | 2 + 5 files changed, 80 insertions(+), 39 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 56a0c9bf95d..03ec5405803 100644 --- 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 + * Editing Changes in Emacs 24.2 diff --git a/lisp/ChangeLog b/lisp/ChangeLog index a21d5841346..f50af02a239 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,16 @@ +2012-04-16 Chong Yidong + + * 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 * frame.el (scrolling): Remove nearly unused customization group. diff --git a/lisp/emacs-lisp/regexp-opt.el b/lisp/emacs-lisp/regexp-opt.el index 6d12fe19277..72e3c398dc0 100644 --- a/lisp/emacs-lisp/regexp-opt.el +++ b/lisp/emacs-lisp/regexp-opt.el @@ -136,9 +136,6 @@ This means the number of non-shy regexp grouping constructs ;;; 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 "")) diff --git a/lisp/image.el b/lisp/image.el index b094f2464ec..348c208781e 100644 --- a/lisp/image.el +++ b/lisp/image.el @@ -685,26 +685,16 @@ The minimum delay between successive frames is 0.01s." image n count time-elapsed limit)))) -(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) diff --git a/lisp/loadup.el b/lisp/loadup.el index 2ba24c8fe37..f7ffa27a9ed 100644 --- a/lisp/loadup.el +++ b/lisp/loadup.el @@ -193,6 +193,8 @@ (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") -- 2.39.2