Make `n' in image-mode work more reliably with external formats

* lisp/image-mode.el (image-mode): Init the external machinery so
that commands like `n' work for those files.
* lisp/image/image-converter.el (image-converter-initialize):
Factored out into own function.
(image-convert-p): Use it.
(image-convert): Ditto.
This commit is contained in:
Lars Ingebrigtsen 2022-03-21 18:05:18 +01:00
parent 61d34c6a50
commit d74cd0cf1f
2 changed files with 16 additions and 7 deletions

View file

@ -625,6 +625,8 @@ image as text, when opening such images in `image-mode'."
(put 'image-mode 'mode-class 'special)
(declare-function image-converter-initialize "image-converter.el")
;;;###autoload
(defun image-mode ()
"Major mode for image files.
@ -650,7 +652,12 @@ Key bindings:
"Empty file"
"(New file)")
"Empty buffer"))
(image-mode--display)))
(image-mode--display)
;; Ensure that we recognize externally parsed image formats in
;; commands like `n'.
(when image-use-external-converter
(require 'image-converter)
(image-converter-initialize))))
(defun image-mode--display ()
(if (not (image-get-display-property))

View file

@ -68,15 +68,19 @@ not, conversion will fail."
(imagemagick :command "convert" :probe ("-list" "format")))
"List of supported image converters to try.")
(defun image-converter-initialize ()
"Determine the external image converter to be used.
This also determines which external formats we can parse."
(unless image-converter
(image-converter--find-converter)))
(defun image-convert-p (source &optional data-p)
"Return `image-convert' if SOURCE is an image that can be converted.
SOURCE can either be a file name or a string containing image
data. In the latter case, DATA-P should be non-nil. If DATA-P
is a string, it should be a MIME format string like
\"image/gif\"."
;; Find an installed image converter.
(unless image-converter
(image-converter--find-converter))
(image-converter-initialize)
;; When image-converter was customized
(when (and image-converter (not image-converter-regexp))
(when-let ((formats (image-converter--probe image-converter)))
@ -111,9 +115,7 @@ IMAGE can also be an image object as returned by `create-image'.
This function converts the image the preferred format, and the
converted image data is returned as a string."
;; Find an installed image converter.
(unless image-converter
(image-converter--find-converter))
(image-converter-initialize)
(unless image-converter
(error "No external image converters available"))
(when (and image-format