1 ;;; image-mode.el --- support for visiting image files
3 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Richard Stallman <rms@gnu.org>
6 ;; Keywords: multimedia
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Defines a major mode for visiting image files
26 ;; that allows conversion between viewing the text of the file
27 ;; and viewing the file as an image. Viewing the image
28 ;; works by putting a `display' text-property on the
29 ;; image data, with the image-data still present underneath; if the
30 ;; resulting buffer file is saved to another name it will correctly save
31 ;; the image data to the new file.
36 (eval-when-compile (require 'cl
))
38 ;;;###autoload (push (cons (purecopy "\\.jpe?g\\'") 'image-mode) auto-mode-alist)
39 ;;;###autoload (push (cons (purecopy "\\.png\\'") 'image-mode) auto-mode-alist)
40 ;;;###autoload (push (cons (purecopy "\\.gif\\'") 'image-mode) auto-mode-alist)
41 ;;;###autoload (push (cons (purecopy "\\.tiff?\\'") 'image-mode) auto-mode-alist)
42 ;;;###autoload (push (cons (purecopy "\\.p[bpgn]m\\'") 'image-mode) auto-mode-alist)
44 ;;;###autoload (push (cons (purecopy "\\.x[bp]m\\'") 'c-mode) auto-mode-alist)
45 ;;;###autoload (push (cons (purecopy "\\.x[bp]m\\'") 'image-mode) auto-mode-alist)
47 ;;;###autoload (push (cons (purecopy "\\.svgz?\\'") 'xml-mode) auto-mode-alist)
48 ;;;###autoload (push (cons (purecopy "\\.svgz?\\'") 'image-mode) auto-mode-alist)
50 ;;; Image mode window-info management.
52 (defvar image-mode-winprops-alist t
)
53 (make-variable-buffer-local 'image-mode-winprops-alist
)
55 (defvar image-mode-new-window-functions nil
56 "Special hook run when image data is requested in a new window.
57 It is called with one argument, the initial WINPROPS.")
59 (defun image-mode-winprops (&optional window cleanup
)
60 "Return winprops of WINDOW.
61 A winprops object has the shape (WINDOW . ALIST)."
63 (setq window
(selected-window)))
64 ((not (windowp window
))
65 (error "Not a window: %s" window
)))
67 (setq image-mode-winprops-alist
68 (delq nil
(mapcar (lambda (winprop)
69 (if (window-live-p (car-safe winprop
))
71 image-mode-winprops-alist
))))
72 (let ((winprops (assq window image-mode-winprops-alist
)))
73 ;; For new windows, set defaults from the latest.
75 (setq winprops
(cons window
76 (copy-alist (cdar image-mode-winprops-alist
))))
77 (run-hook-with-args 'image-mode-new-window-functions winprops
))
78 ;; Move window to front.
79 (setq image-mode-winprops-alist
80 (cons winprops
(delq winprops image-mode-winprops-alist
)))
83 (defun image-mode-window-get (prop &optional winprops
)
84 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
85 (cdr (assq prop
(cdr winprops
))))
87 (defsetf image-mode-window-get
(prop &optional winprops
) (val)
88 `(image-mode-window-put ,prop
,val
,winprops
))
90 (defun image-mode-window-put (prop val
&optional winprops
)
91 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
92 (setcdr winprops
(cons (cons prop val
)
93 (delq (assq prop
(cdr winprops
)) (cdr winprops
)))))
95 (defun image-set-window-vscroll (vscroll)
96 (setf (image-mode-window-get 'vscroll
) vscroll
)
97 (set-window-vscroll (selected-window) vscroll
))
99 (defun image-set-window-hscroll (ncol)
100 (setf (image-mode-window-get 'hscroll
) ncol
)
101 (set-window-hscroll (selected-window) ncol
))
103 (defun image-mode-reapply-winprops ()
104 ;; When set-window-buffer, set hscroll and vscroll to what they were
105 ;; last time the image was displayed in this window.
106 (when (and (image-get-display-property)
107 (listp image-mode-winprops-alist
))
108 (let* ((winprops (image-mode-winprops nil t
))
109 (hscroll (image-mode-window-get 'hscroll winprops
))
110 (vscroll (image-mode-window-get 'vscroll winprops
)))
111 (if hscroll
(set-window-hscroll (selected-window) hscroll
))
112 (if vscroll
(set-window-vscroll (selected-window) vscroll
)))))
114 (defun image-mode-setup-winprops ()
115 ;; Record current scroll settings.
116 (unless (listp image-mode-winprops-alist
)
117 (setq image-mode-winprops-alist nil
))
118 (add-hook 'window-configuration-change-hook
119 'image-mode-reapply-winprops nil t
))
121 ;;; Image scrolling functions
123 (defun image-get-display-property ()
124 (get-char-property (point-min) 'display
125 ;; There might be different images for different displays.
126 (if (eq (window-buffer) (current-buffer))
129 (declare-function image-size
"image.c" (spec &optional pixels frame
))
131 (defun image-forward-hscroll (&optional n
)
132 "Scroll image in current window to the left by N character widths.
133 Stop if the right edge of the image is reached."
137 (image-set-window-hscroll (max 0 (+ (window-hscroll) n
))))
139 (let* ((image (image-get-display-property))
140 (edges (window-inside-edges))
141 (win-width (- (nth 2 edges
) (nth 0 edges
)))
142 (img-width (ceiling (car (image-size image
)))))
143 (image-set-window-hscroll (min (max 0 (- img-width win-width
))
144 (+ n
(window-hscroll))))))))
146 (defun image-backward-hscroll (&optional n
)
147 "Scroll image in current window to the right by N character widths.
148 Stop if the left edge of the image is reached."
150 (image-forward-hscroll (- n
)))
152 (defun image-next-line (&optional n
)
153 "Scroll image in current window upward by N lines.
154 Stop if the bottom edge of the image is reached."
158 (image-set-window-vscroll (max 0 (+ (window-vscroll) n
))))
160 (let* ((image (image-get-display-property))
161 (edges (window-inside-edges))
162 (win-height (- (nth 3 edges
) (nth 1 edges
)))
163 (img-height (ceiling (cdr (image-size image
)))))
164 (image-set-window-vscroll (min (max 0 (- img-height win-height
))
165 (+ n
(window-vscroll))))))))
167 (defun image-previous-line (&optional n
)
168 "Scroll image in current window downward by N lines.
169 Stop if the top edge of the image is reached."
171 (image-next-line (- n
)))
173 (defun image-scroll-up (&optional n
)
174 "Scroll image in current window upward by N lines.
175 Stop if the bottom edge of the image is reached.
176 If ARG is omitted or nil, scroll upward by a near full screen.
177 A near full screen is `next-screen-context-lines' less than a full screen.
178 Negative ARG means scroll downward.
179 If ARG is the atom `-', scroll downward by nearly full screen.
180 When calling from a program, supply as argument a number, nil, or `-'."
183 (let* ((edges (window-inside-edges))
184 (win-height (- (nth 3 edges
) (nth 1 edges
))))
186 (max 0 (- win-height next-screen-context-lines
)))))
188 (let* ((edges (window-inside-edges))
189 (win-height (- (nth 3 edges
) (nth 1 edges
))))
191 (min 0 (- next-screen-context-lines win-height
)))))
192 (t (image-next-line (prefix-numeric-value n
)))))
194 (defun image-scroll-down (&optional n
)
195 "Scroll image in current window downward by N lines.
196 Stop if the top edge of the image is reached.
197 If ARG is omitted or nil, scroll downward by a near full screen.
198 A near full screen is `next-screen-context-lines' less than a full screen.
199 Negative ARG means scroll upward.
200 If ARG is the atom `-', scroll upward by nearly full screen.
201 When calling from a program, supply as argument a number, nil, or `-'."
204 (let* ((edges (window-inside-edges))
205 (win-height (- (nth 3 edges
) (nth 1 edges
))))
207 (min 0 (- next-screen-context-lines win-height
)))))
209 (let* ((edges (window-inside-edges))
210 (win-height (- (nth 3 edges
) (nth 1 edges
))))
212 (max 0 (- win-height next-screen-context-lines
)))))
213 (t (image-next-line (- (prefix-numeric-value n
))))))
215 (defun image-bol (arg)
216 "Scroll horizontally to the left edge of the image in the current window.
217 With argument ARG not nil or 1, move forward ARG - 1 lines first,
218 stopping if the top or bottom edge of the image is reached."
221 (/= (setq arg
(prefix-numeric-value arg
)) 1)
222 (image-next-line (- arg
1)))
223 (image-set-window-hscroll 0))
225 (defun image-eol (arg)
226 "Scroll horizontally to the right edge of the image in the current window.
227 With argument ARG not nil or 1, move forward ARG - 1 lines first,
228 stopping if the top or bottom edge of the image is reached."
231 (/= (setq arg
(prefix-numeric-value arg
)) 1)
232 (image-next-line (- arg
1)))
233 (let* ((image (image-get-display-property))
234 (edges (window-inside-edges))
235 (win-width (- (nth 2 edges
) (nth 0 edges
)))
236 (img-width (ceiling (car (image-size image
)))))
237 (image-set-window-hscroll (max 0 (- img-width win-width
)))))
240 "Scroll to the top-left corner of the image in the current window."
242 (image-set-window-hscroll 0)
243 (image-set-window-vscroll 0))
246 "Scroll to the bottom-right corner of the image in the current window."
248 (let* ((image (image-get-display-property))
249 (edges (window-inside-edges))
250 (win-width (- (nth 2 edges
) (nth 0 edges
)))
251 (img-width (ceiling (car (image-size image
))))
252 (win-height (- (nth 3 edges
) (nth 1 edges
)))
253 (img-height (ceiling (cdr (image-size image
)))))
254 (image-set-window-hscroll (max 0 (- img-width win-width
)))
255 (image-set-window-vscroll (max 0 (- img-height win-height
)))))
257 ;; Adjust frame and image size.
259 (defun image-mode-fit-frame ()
260 "Fit the frame to the current image.
261 This function assumes the current frame has only one window."
262 ;; FIXME: This does not take into account decorations like mode-line,
263 ;; minibuffer, header-line, ...
265 (let* ((saved (frame-parameter nil
'image-mode-saved-size
))
266 (display (image-get-display-property))
267 (size (image-size display
)))
269 (eq (caar saved
) (frame-width))
270 (eq (cdar saved
) (frame-height)))
271 (progn ;; Toggle back to previous non-fitted size.
272 (set-frame-parameter nil
'image-mode-saved-size nil
)
273 (setq size
(cdr saved
)))
274 ;; Round up size, and save current size so we can toggle back to it.
275 (setcar size
(ceiling (car size
)))
276 (setcdr size
(ceiling (cdr size
)))
277 (set-frame-parameter nil
'image-mode-saved-size
278 (cons size
(cons (frame-width) (frame-height)))))
279 (set-frame-width (selected-frame) (car size
))
280 (set-frame-height (selected-frame) (cdr size
))))
284 (defvar image-type nil
286 This variable is used to display the current image type in the mode line.")
287 (make-variable-buffer-local 'image-type
)
289 (defvar image-mode-previous-major-mode nil
290 "Internal variable to keep the previous non-image major mode.")
292 (defvar image-mode-map
293 (let ((map (make-sparse-keymap)))
294 (suppress-keymap map
)
295 (define-key map
"q" 'quit-window
)
296 (define-key map
"\C-c\C-c" 'image-toggle-display
)
297 (define-key map
(kbd "SPC") 'image-scroll-up
)
298 (define-key map
(kbd "DEL") 'image-scroll-down
)
299 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
300 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
301 (define-key map
[remap previous-line
] 'image-previous-line
)
302 (define-key map
[remap next-line
] 'image-next-line
)
303 (define-key map
[remap scroll-up
] 'image-scroll-up
)
304 (define-key map
[remap scroll-down
] 'image-scroll-down
)
305 (define-key map
[remap scroll-up-command
] 'image-scroll-up
)
306 (define-key map
[remap scroll-down-command
] 'image-scroll-down
)
307 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
308 (define-key map
[remap move-end-of-line
] 'image-eol
)
309 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
310 (define-key map
[remap end-of-buffer
] 'image-eob
)
312 "Major mode keymap for viewing images in Image mode.")
314 (defvar image-minor-mode-map
315 (let ((map (make-sparse-keymap)))
316 (define-key map
"\C-c\C-c" 'image-toggle-display
)
318 "Minor mode keymap for viewing images as text in Image mode.")
320 (defvar bookmark-make-record-function
)
322 (put 'image-mode
'mode-class
'special
)
326 "Major mode for image files.
327 You can use \\<image-mode-map>\\[image-toggle-display]
328 to toggle between display as an image and display as text."
332 (unless (display-images-p)
333 (error "Display does not support images"))
335 (kill-all-local-variables)
336 (setq major-mode
'image-mode
)
338 (if (not (image-get-display-property))
340 (image-toggle-display-image)
341 ;; If attempt to display the image fails.
342 (if (not (image-get-display-property))
343 (error "Invalid image")))
344 ;; Set next vars when image is already displayed but local
345 ;; variables were cleared by kill-all-local-variables
346 (setq cursor-type nil truncate-lines t
347 image-type
(plist-get (cdr (image-get-display-property)) :type
)))
349 (setq mode-name
(if image-type
(format "Image[%s]" image-type
) "Image"))
350 (use-local-map image-mode-map
)
352 ;; Use our own bookmarking function for images.
353 (set (make-local-variable 'bookmark-make-record-function
)
354 'image-bookmark-make-record
)
356 ;; Keep track of [vh]scroll when switching buffers
357 (image-mode-setup-winprops)
359 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
360 (add-hook 'after-revert-hook
'image-after-revert-hook nil t
)
361 (run-mode-hooks 'image-mode-hook
)
362 (message "%s" (concat
363 (substitute-command-keys
364 "Type \\[image-toggle-display] to view the image as ")
365 (if (image-get-display-property)
366 "text" "an image") ".")))
370 (if (called-interactively-p 'any
) 'error
'message
)
371 "Cannot display image: %s" (cdr err
)))))
374 (define-minor-mode image-minor-mode
375 "Toggle Image minor mode.
376 With arg, turn Image minor mode on if arg is positive, off otherwise.
377 It provides the key \\<image-mode-map>\\[image-toggle-display] \
378 to switch back to `image-mode'
379 to display an image file as the actual image."
380 nil
(:eval
(if image-type
(format " Image[%s]" image-type
) " Image"))
385 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)))
388 (defun image-mode-as-text ()
389 "Set a non-image mode as major mode in combination with image minor mode.
390 A non-image major mode found from `auto-mode-alist' or Fundamental mode
391 displays an image file as text. `image-minor-mode' provides the key
392 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
393 to display an image file as the actual image.
395 You can use `image-mode-as-text' in `auto-mode-alist' when you want
396 to display an image file as text initially.
398 See commands `image-mode' and `image-minor-mode' for more information
401 ;; image-mode-as-text = normal-mode + image-minor-mode
402 (let ((previous-image-type image-type
)) ; preserve `image-type'
403 (if image-mode-previous-major-mode
404 ;; Restore previous major mode that was already found by this
405 ;; function and cached in `image-mode-previous-major-mode'
406 (funcall image-mode-previous-major-mode
)
407 (let ((auto-mode-alist
410 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
411 '(image-mode image-mode-maybe image-mode-as-text
))
414 (magic-fallback-mode-alist
417 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
418 '(image-mode image-mode-maybe image-mode-as-text
))
420 magic-fallback-mode-alist
))))
422 (set (make-local-variable 'image-mode-previous-major-mode
) major-mode
)))
423 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
424 (setq image-type previous-image-type
)
425 ;; Enable image minor mode with `C-c C-c'.
427 ;; Show the image file as text.
428 (image-toggle-display-text)
429 (message "%s" (concat
430 (substitute-command-keys
431 "Type \\[image-toggle-display] to view the image as ")
432 (if (image-get-display-property)
433 "text" "an image") "."))))
435 (define-obsolete-function-alias 'image-mode-maybe
'image-mode
"23.2")
437 (defun image-toggle-display-text ()
438 "Show the image file as text.
439 Remove text properties that display the image."
440 (let ((inhibit-read-only t
)
442 (modified (buffer-modified-p)))
443 (remove-list-of-text-properties (point-min) (point-max)
444 '(display intangible read-nonsticky
445 read-only front-sticky
))
446 (set-buffer-modified-p modified
)
447 (if (called-interactively-p 'any
)
448 (message "Repeat this command to go back to displaying the image"))))
450 (defvar archive-superior-buffer
)
451 (defvar tar-superior-buffer
)
452 (declare-function image-flush
"image.c" (spec &optional frame
))
454 (defun image-toggle-display-image ()
455 "Show the image of the image file.
456 Turn the image data into a real image, but only if the whole file
458 (let* ((filename (buffer-file-name))
459 (data-p (not (and filename
460 (file-readable-p filename
)
461 (not (file-remote-p filename
))
462 (not (buffer-modified-p))
463 (not (and (boundp 'archive-superior-buffer
)
464 archive-superior-buffer
))
465 (not (and (boundp 'tar-superior-buffer
)
466 tar-superior-buffer
)))))
467 (file-or-data (if data-p
469 (buffer-substring-no-properties (point-min) (point-max)))
471 (type (image-type file-or-data nil data-p
))
472 (image (create-animated-image file-or-data type data-p
))
476 rear-nonsticky
(display intangible
)
477 read-only t front-sticky
(read-only)))
478 (inhibit-read-only t
)
480 (modified (buffer-modified-p)))
482 (let ((buffer-file-truename nil
)) ; avoid changing dir mtime by lock_file
483 (add-text-properties (point-min) (point-max) props
)
484 (restore-buffer-modified-p modified
))
485 ;; Inhibit the cursor when the buffer contains only an image,
486 ;; because cursors look very strange on top of images.
487 (setq cursor-type nil
)
488 ;; This just makes the arrow displayed in the right fringe
489 ;; area look correct when the image is wider than the window.
490 (setq truncate-lines t
)
491 ;; Allow navigation of large images
492 (set (make-local-variable 'auto-hscroll-mode
) nil
)
493 (setq image-type type
)
494 (if (eq major-mode
'image-mode
)
495 (setq mode-name
(format "Image[%s]" type
)))
496 (if (called-interactively-p 'any
)
497 (message "Repeat this command to go back to displaying the file as text"))))
499 (defun image-toggle-display ()
500 "Start or stop displaying an image file as the actual image.
501 This command toggles between `image-mode-as-text' showing the text of
502 the image file and `image-mode' showing the image as an image."
504 (if (image-get-display-property)
508 (defun image-after-revert-hook ()
509 (when (image-get-display-property)
510 (image-toggle-display-text)
511 ;; Update image display.
512 (redraw-frame (selected-frame))
513 (image-toggle-display-image)))
516 ;;; Support for bookmark.el
517 (declare-function bookmark-make-record-default
"bookmark"
518 (&optional point-only
))
519 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
520 (declare-function bookmark-default-handler
"bookmark" (bmk))
522 (defun image-bookmark-make-record ()
523 (nconc (bookmark-make-record-default)
524 `((image-type .
,image-type
)
525 (handler . image-bookmark-jump
))))
528 (defun image-bookmark-jump (bmk)
529 ;; This implements the `handler' function interface for record type
530 ;; returned by `bookmark-make-record-function', which see.
531 (prog1 (bookmark-default-handler bmk
)
532 (when (not (string= image-type
(bookmark-prop-get bmk
'image-type
)))
533 (image-toggle-display))))
535 (provide 'image-mode
)
537 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
538 ;;; image-mode.el ends here