summaryrefslogtreecommitdiff
path: root/lisp/image-mode.el
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2019-10-29 21:42:33 +0100
committerLars Ingebrigtsen <larsi@gnus.org>2019-10-29 21:42:39 +0100
commit19c98f762092adab01bf35d4b0c958af7d4ea59e (patch)
tree7740857748d7b0d916593181762f26db1719e62d /lisp/image-mode.el
parent1997e3b80f1046d789c4120d50e0f1dde05e7a74 (diff)
downloademacs-19c98f762092adab01bf35d4b0c958af7d4ea59e.tar.gz
emacs-19c98f762092adab01bf35d4b0c958af7d4ea59e.tar.bz2
emacs-19c98f762092adab01bf35d4b0c958af7d4ea59e.zip
Default exotic image formats (like .webp) to image-mode
* doc/lispref/errors.texi (Standard Errors): Mention the new error. * lisp/files.el (auto-mode-alist): Add a bunch of image suffixes to the list (bug#37972) based on the output from "gm convert -list format" (i.e., graphicsmagick). * lisp/image-mode.el (image-mode): Rewrite to possibly notify the user about image-use-external-converter. (image-mode--setup-mode): Factor out into own function and don't run under `condition-case' as there's nothing here that should error. * lisp/image.el (unknown-image-type): New error. (image-type): Signal that error so that image-mode can offer sensible feedback to the user.
Diffstat (limited to 'lisp/image-mode.el')
-rw-r--r--lisp/image-mode.el144
1 files changed, 75 insertions, 69 deletions
diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 342102568ca..db6864649d0 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -552,76 +552,82 @@ to toggle between display as an image and display as text or hex.
Key bindings:
\\{image-mode-map}"
(interactive)
- (condition-case err
+ (unless (display-images-p)
+ (error "Display does not support images"))
+
+ (major-mode-suspend)
+ (setq major-mode 'image-mode)
+
+ (if (not (image-get-display-property))
(progn
- (unless (display-images-p)
- (error "Display does not support images"))
-
- (major-mode-suspend)
- (setq major-mode 'image-mode)
-
- (if (not (image-get-display-property))
- (progn
- (image-toggle-display-image)
- ;; If attempt to display the image fails.
- (if (not (image-get-display-property))
- (error "Invalid image")))
- ;; Set next vars when image is already displayed but local
- ;; variables were cleared by kill-all-local-variables
- (setq cursor-type nil truncate-lines t
- image-type (plist-get (cdr (image-get-display-property)) :type)))
-
- (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
- (use-local-map image-mode-map)
-
- ;; Use our own bookmarking function for images.
- (setq-local bookmark-make-record-function
- #'image-bookmark-make-record)
-
- ;; Keep track of [vh]scroll when switching buffers
- (image-mode-setup-winprops)
-
- (add-hook 'change-major-mode-hook #'image-toggle-display-text nil t)
- (add-hook 'after-revert-hook #'image-after-revert-hook nil t)
- (run-mode-hooks 'image-mode-hook)
- (let ((image (image-get-display-property))
- (msg1 (substitute-command-keys
- "Type \\[image-toggle-display] or \\[image-toggle-hex-display] to view the image as "))
- animated)
- (cond
- ((null image)
- (message "%s" (concat msg1 "an image.")))
- ((setq animated (image-multi-frame-p image))
- (setq image-multi-frame t
- mode-line-process
- `(:eval
- (concat " "
- (propertize
- (format "[%s/%s]"
- (1+ (image-current-frame ',image))
- ,(car animated))
- 'help-echo "Frames
-mouse-1: Next frame
-mouse-3: Previous frame"
- 'mouse-face 'mode-line-highlight
- 'local-map
- '(keymap
- (mode-line
- keymap
- (down-mouse-1 . image-next-frame)
- (down-mouse-3 . image-previous-frame)))))))
- (message "%s"
- (concat msg1 "text. This image has multiple frames.")))
-;;; (substitute-command-keys
-;;; "\\[image-toggle-animation] to animate."))))
- (t
- (message "%s" (concat msg1 "text or hex."))))))
-
- (error
- (image-mode-as-text)
- (funcall
- (if (called-interactively-p 'any) 'error 'message)
- "Cannot display image: %s" (cdr err)))))
+ (when (condition-case err
+ (progn
+ (image-toggle-display-image)
+ t)
+ (unknown-image-type
+ (image-mode-as-text)
+ (funcall
+ (if (called-interactively-p 'any) 'error 'message)
+ "Unknown image type; consider switching `image-use-external-converter' on")
+ nil)
+ (error
+ (image-mode-as-text)
+ (funcall
+ (if (called-interactively-p 'any) 'error 'message)
+ "Cannot display image: %s" (cdr err))
+ nil))
+ ;; If attempt to display the image fails.
+ (if (not (image-get-display-property))
+ (error "Invalid image"))
+ (image-mode--setup-mode)))
+ ;; Set next vars when image is already displayed but local
+ ;; variables were cleared by kill-all-local-variables
+ (setq cursor-type nil truncate-lines t
+ image-type (plist-get (cdr (image-get-display-property)) :type))
+ (image-mode--setup-mode)))
+
+(defun image-mode--setup-mode ()
+ (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
+ (use-local-map image-mode-map)
+
+ ;; Use our own bookmarking function for images.
+ (setq-local bookmark-make-record-function
+ #'image-bookmark-make-record)
+
+ ;; Keep track of [vh]scroll when switching buffers
+ (image-mode-setup-winprops)
+
+ (add-hook 'change-major-mode-hook #'image-toggle-display-text nil t)
+ (add-hook 'after-revert-hook #'image-after-revert-hook nil t)
+ (run-mode-hooks 'image-mode-hook)
+ (let ((image (image-get-display-property))
+ (msg1 (substitute-command-keys
+ "Type \\[image-toggle-display] or \\[image-toggle-hex-display] to view the image as "))
+ animated)
+ (cond
+ ((null image)
+ (message "%s" (concat msg1 "an image.")))
+ ((setq animated (image-multi-frame-p image))
+ (setq image-multi-frame t
+ mode-line-process
+ `(:eval
+ (concat " "
+ (propertize
+ (format "[%s/%s]"
+ (1+ (image-current-frame ',image))
+ ,(car animated))
+ 'help-echo "Frames\nmouse-1: Next frame\nmouse-3: Previous frame"
+ 'mouse-face 'mode-line-highlight
+ 'local-map
+ '(keymap
+ (mode-line
+ keymap
+ (down-mouse-1 . image-next-frame)
+ (down-mouse-3 . image-previous-frame)))))))
+ (message "%s"
+ (concat msg1 "text. This image has multiple frames.")))
+ (t
+ (message "%s" (concat msg1 "text or hex."))))))
;;;###autoload
(define-minor-mode image-minor-mode