(require 'image)
(defvar image-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-c" 'image-toggle-display)
map)
"Major mode keymap for Image mode.")
(defun image-mode ()
"Major mode for image files.
You can use \\<image-mode-map>\\[image-toggle-display]
to toggle between display as an image and display as text."
(interactive)
(kill-all-local-variables)
(setq mode-name "Image")
(setq major-mode 'image-mode)
(use-local-map image-mode-map)
(add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
(if (and (display-images-p)
(not (get-text-property (point-min) 'display)))
(image-toggle-display)
(setq cursor-type nil truncate-lines t))
(run-mode-hooks 'image-mode-hook)
(if (display-images-p)
(message "%s" (concat
(substitute-command-keys
"Type \\[image-toggle-display] to view as ")
(if (get-text-property (point-min) 'display)
"text" "an image") "."))))
(define-minor-mode image-minor-mode
"Toggle Image minor mode.
With arg, turn Image minor mode on if arg is positive, off otherwise.
See the command `image-mode' for more information on this mode."
nil " Image" image-mode-map
:group 'image
:version "22.1"
(if (not image-minor-mode)
(image-toggle-display-text)
(if (get-text-property (point-min) 'display)
(setq cursor-type nil truncate-lines t))
(add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
(message "%s" (concat (substitute-command-keys
"Type \\[image-toggle-display] to view the image as ")
(if (get-text-property (point-min) 'display)
"text" "an image") "."))))
(defun image-mode-maybe ()
"Set major or minor mode for image files.
Set Image major mode only when there are no other major modes
associated with a filename in `auto-mode-alist'. When an image
filename matches another major mode in `auto-mode-alist' then
set that major mode and Image minor mode.
See commands `image-mode' and `image-minor-mode' for more
information on these modes."
(interactive)
(let* ((mode-alist
(delq nil (mapcar
(lambda (elt)
(unless (memq (or (car-safe (cdr elt)) (cdr elt))
'(image-mode image-mode-maybe))
elt))
auto-mode-alist))))
(if (assoc-default buffer-file-name mode-alist 'string-match)
(let ((auto-mode-alist mode-alist)
(magic-mode-alist nil))
(set-auto-mode)
(image-minor-mode t))
(image-mode))))
(defun image-toggle-display-text ()
"Showing the text of the image file."
(if (get-text-property (point-min) 'display)
(image-toggle-display)))
(defvar archive-superior-buffer)
(defvar tar-superior-buffer)
(defun image-toggle-display ()
"Start or stop displaying an image file as the actual image.
This command toggles between showing the text of the image file
and showing the image as an image."
(interactive)
(if (get-text-property (point-min) 'display)
(let ((inhibit-read-only t)
(buffer-undo-list t)
(modified (buffer-modified-p)))
(remove-list-of-text-properties (point-min) (point-max)
'(display intangible read-nonsticky
read-only front-sticky))
(set-buffer-modified-p modified)
(kill-local-variable 'cursor-type)
(kill-local-variable 'truncate-lines)
(if (called-interactively-p)
(message "Repeat this command to go back to displaying the image")))
(let* ((image
(if (and (buffer-file-name)
(not (file-remote-p (buffer-file-name)))
(not (buffer-modified-p))
(not (and (boundp 'archive-superior-buffer)
archive-superior-buffer))
(not (and (boundp 'tar-superior-buffer)
tar-superior-buffer)))
(progn (clear-image-cache)
(create-image (buffer-file-name)))
(create-image
(string-make-unibyte
(buffer-substring-no-properties (point-min) (point-max)))
nil t)))
(props
`(display ,image
intangible ,image
rear-nonsticky (display intangible)
read-only t front-sticky (read-only)))
(inhibit-read-only t)
(buffer-undo-list t)
(modified (buffer-modified-p)))
(add-text-properties (point-min) (point-max) props)
(set-buffer-modified-p modified)
(setq cursor-type nil)
(setq truncate-lines t)
(if (called-interactively-p)
(message "Repeat this command to go back to displaying the file as text")))))
(provide 'image-mode)