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
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Defines a major mode for visiting image files
27 ;; that allows conversion between viewing the text of the file
28 ;; and viewing the file as an image. Viewing the image
29 ;; works by putting a `display' text-property on the
30 ;; image data, with the image-data still present underneath; if the
31 ;; resulting buffer file is saved to another name it will correctly save
32 ;; the image data to the new file.
37 (eval-when-compile (require 'cl
))
39 ;;; Image mode window-info management.
41 (defvar image-mode-winprops-alist t
)
42 (make-variable-buffer-local 'image-mode-winprops-alist
)
44 (defvar image-mode-new-window-functions nil
45 "Special hook run when image data is requested in a new window.
46 It is called with one argument, the initial WINPROPS.")
48 (defun image-mode-winprops (&optional window cleanup
)
49 "Return winprops of WINDOW.
50 A winprops object has the shape (WINDOW . ALIST)."
52 (setq window
(selected-window)))
53 ((not (windowp window
))
54 (error "Not a window: %s" window
)))
56 (setq image-mode-winprops-alist
57 (delq nil
(mapcar (lambda (winprop)
58 (if (window-live-p (car-safe winprop
))
60 image-mode-winprops-alist
))))
61 (let ((winprops (assq window image-mode-winprops-alist
)))
62 ;; For new windows, set defaults from the latest.
64 (setq winprops
(cons window
65 (copy-alist (cdar image-mode-winprops-alist
))))
66 (run-hook-with-args 'image-mode-new-window-functions winprops
))
67 ;; Move window to front.
68 (setq image-mode-winprops-alist
69 (cons winprops
(delq winprops image-mode-winprops-alist
)))
72 (defun image-mode-window-get (prop &optional winprops
)
73 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
74 (cdr (assq prop
(cdr winprops
))))
76 (defsetf image-mode-window-get
(prop &optional winprops
) (val)
77 `(image-mode-window-put ,prop
,val
,winprops
))
79 (defun image-mode-window-put (prop val
&optional winprops
)
80 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
81 (setcdr winprops
(cons (cons prop val
)
82 (delq (assq prop
(cdr winprops
)) (cdr winprops
)))))
84 (defun image-set-window-vscroll (vscroll)
85 (setf (image-mode-window-get 'vscroll
) vscroll
)
86 (set-window-vscroll (selected-window) vscroll
))
88 (defun image-set-window-hscroll (ncol)
89 (setf (image-mode-window-get 'hscroll
) ncol
)
90 (set-window-hscroll (selected-window) ncol
))
92 (defun image-mode-reapply-winprops ()
93 ;; When set-window-buffer, set hscroll and vscroll to what they were
94 ;; last time the image was displayed in this window.
95 (when (and (image-get-display-property)
96 (listp image-mode-winprops-alist
))
97 (let* ((winprops (image-mode-winprops nil t
))
98 (hscroll (image-mode-window-get 'hscroll winprops
))
99 (vscroll (image-mode-window-get 'vscroll winprops
)))
100 (if hscroll
(set-window-hscroll (selected-window) hscroll
))
101 (if vscroll
(set-window-vscroll (selected-window) vscroll
)))))
103 (defun image-mode-setup-winprops ()
104 ;; Record current scroll settings.
105 (unless (listp image-mode-winprops-alist
)
106 (setq image-mode-winprops-alist nil
))
107 (add-hook 'window-configuration-change-hook
108 'image-mode-reapply-winprops nil t
))
110 ;;; Image scrolling functions
112 (defun image-get-display-property ()
113 (get-char-property (point-min) 'display
114 ;; There might be different images for different displays.
115 (if (eq (window-buffer) (current-buffer))
118 (declare-function image-size
"image.c" (spec &optional pixels frame
))
120 (defun image-display-size (spec &optional pixels frame
)
121 "Wrapper around `image-size', to handle slice display properties.
122 If SPEC is an image display property, call `image-size' with the
124 If SPEC is a list of properties containing `image' and `slice'
125 properties, calculate the display size from the slice property.
126 If SPEC contains `image' but not `slice', call `image-size' with
127 the specified image."
128 (if (eq (car spec
) 'image
)
129 (image-size spec pixels frame
)
130 (let ((image (assoc 'image spec
))
131 (slice (assoc 'slice spec
)))
132 (cond ((and image slice
)
134 (cons (nth 3 slice
) (nth 4 slice
))
135 (cons (/ (float (nth 3 slice
)) (frame-char-width frame
))
136 (/ (float (nth 4 slice
)) (frame-char-height frame
)))))
138 (image-size image pixels frame
))
140 (error "Invalid image specification: %s" spec
))))))
142 (defun image-forward-hscroll (&optional n
)
143 "Scroll image in current window to the left by N character widths.
144 Stop if the right edge of the image is reached."
148 (image-set-window-hscroll (max 0 (+ (window-hscroll) n
))))
150 (let* ((image (image-get-display-property))
151 (edges (window-inside-edges))
152 (win-width (- (nth 2 edges
) (nth 0 edges
)))
153 (img-width (ceiling (car (image-display-size image
)))))
154 (image-set-window-hscroll (min (max 0 (- img-width win-width
))
155 (+ n
(window-hscroll))))))))
157 (defun image-backward-hscroll (&optional n
)
158 "Scroll image in current window to the right by N character widths.
159 Stop if the left edge of the image is reached."
161 (image-forward-hscroll (- n
)))
163 (defun image-next-line (&optional n
)
164 "Scroll image in current window upward by N lines.
165 Stop if the bottom edge of the image is reached."
169 (image-set-window-vscroll (max 0 (+ (window-vscroll) n
))))
171 (let* ((image (image-get-display-property))
172 (edges (window-inside-edges))
173 (win-height (- (nth 3 edges
) (nth 1 edges
)))
174 (img-height (ceiling (cdr (image-display-size image
)))))
175 (image-set-window-vscroll (min (max 0 (- img-height win-height
))
176 (+ n
(window-vscroll))))))))
178 (defun image-previous-line (&optional n
)
179 "Scroll image in current window downward by N lines.
180 Stop if the top edge of the image is reached."
182 (image-next-line (- n
)))
184 (defun image-scroll-up (&optional n
)
185 "Scroll image in current window upward by N lines.
186 Stop if the bottom edge of the image is reached.
187 If ARG is omitted or nil, scroll upward by a near full screen.
188 A near full screen is `next-screen-context-lines' less than a full screen.
189 Negative ARG means scroll downward.
190 If ARG is the atom `-', scroll downward by nearly full screen.
191 When calling from a program, supply as argument a number, nil, or `-'."
194 (let* ((edges (window-inside-edges))
195 (win-height (- (nth 3 edges
) (nth 1 edges
))))
197 (max 0 (- win-height next-screen-context-lines
)))))
199 (let* ((edges (window-inside-edges))
200 (win-height (- (nth 3 edges
) (nth 1 edges
))))
202 (min 0 (- next-screen-context-lines win-height
)))))
203 (t (image-next-line (prefix-numeric-value n
)))))
205 (defun image-scroll-down (&optional n
)
206 "Scroll image in current window downward by N lines.
207 Stop if the top edge of the image is reached.
208 If ARG is omitted or nil, scroll downward by a near full screen.
209 A near full screen is `next-screen-context-lines' less than a full screen.
210 Negative ARG means scroll upward.
211 If ARG is the atom `-', scroll upward by nearly full screen.
212 When calling from a program, supply as argument a number, nil, or `-'."
215 (let* ((edges (window-inside-edges))
216 (win-height (- (nth 3 edges
) (nth 1 edges
))))
218 (min 0 (- next-screen-context-lines win-height
)))))
220 (let* ((edges (window-inside-edges))
221 (win-height (- (nth 3 edges
) (nth 1 edges
))))
223 (max 0 (- win-height next-screen-context-lines
)))))
224 (t (image-next-line (- (prefix-numeric-value n
))))))
226 (defun image-bol (arg)
227 "Scroll horizontally to the left edge of the image in the current window.
228 With argument ARG not nil or 1, move forward ARG - 1 lines first,
229 stopping if the top or bottom edge of the image is reached."
232 (/= (setq arg
(prefix-numeric-value arg
)) 1)
233 (image-next-line (- arg
1)))
234 (image-set-window-hscroll 0))
236 (defun image-eol (arg)
237 "Scroll horizontally to the right edge of the image in the current window.
238 With argument ARG not nil or 1, move forward ARG - 1 lines first,
239 stopping if the top or bottom edge of the image is reached."
242 (/= (setq arg
(prefix-numeric-value arg
)) 1)
243 (image-next-line (- arg
1)))
244 (let* ((image (image-get-display-property))
245 (edges (window-inside-edges))
246 (win-width (- (nth 2 edges
) (nth 0 edges
)))
247 (img-width (ceiling (car (image-display-size image
)))))
248 (image-set-window-hscroll (max 0 (- img-width win-width
)))))
251 "Scroll to the top-left corner of the image in the current window."
253 (image-set-window-hscroll 0)
254 (image-set-window-vscroll 0))
257 "Scroll to the bottom-right corner of the image in the current window."
259 (let* ((image (image-get-display-property))
260 (edges (window-inside-edges))
261 (win-width (- (nth 2 edges
) (nth 0 edges
)))
262 (img-width (ceiling (car (image-display-size image
))))
263 (win-height (- (nth 3 edges
) (nth 1 edges
)))
264 (img-height (ceiling (cdr (image-display-size image
)))))
265 (image-set-window-hscroll (max 0 (- img-width win-width
)))
266 (image-set-window-vscroll (max 0 (- img-height win-height
)))))
268 ;; Adjust frame and image size.
270 (defun image-mode-fit-frame ()
271 "Fit the frame to the current image.
272 This function assumes the current frame has only one window."
273 ;; FIXME: This does not take into account decorations like mode-line,
274 ;; minibuffer, header-line, ...
276 (let* ((saved (frame-parameter nil
'image-mode-saved-size
))
277 (display (image-get-display-property))
278 (size (image-display-size display
)))
280 (eq (caar saved
) (frame-width))
281 (eq (cdar saved
) (frame-height)))
282 (progn ;; Toggle back to previous non-fitted size.
283 (set-frame-parameter nil
'image-mode-saved-size nil
)
284 (setq size
(cdr saved
)))
285 ;; Round up size, and save current size so we can toggle back to it.
286 (setcar size
(ceiling (car size
)))
287 (setcdr size
(ceiling (cdr size
)))
288 (set-frame-parameter nil
'image-mode-saved-size
289 (cons size
(cons (frame-width) (frame-height)))))
290 (set-frame-width (selected-frame) (car size
))
291 (set-frame-height (selected-frame) (cdr size
))))
295 (defvar image-type nil
297 This variable is used to display the current image type in the mode line.")
298 (make-variable-buffer-local 'image-type
)
300 (defvar image-mode-previous-major-mode nil
301 "Internal variable to keep the previous non-image major mode.")
303 (defvar image-mode-map
304 (let ((map (make-sparse-keymap)))
305 (suppress-keymap map
)
306 (define-key map
"q" 'quit-window
)
307 (define-key map
"\C-c\C-c" 'image-toggle-display
)
308 (define-key map
(kbd "SPC") 'image-scroll-up
)
309 (define-key map
(kbd "DEL") 'image-scroll-down
)
310 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
311 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
312 (define-key map
[remap right-char
] 'image-forward-hscroll
)
313 (define-key map
[remap left-char
] 'image-backward-hscroll
)
314 (define-key map
[remap previous-line
] 'image-previous-line
)
315 (define-key map
[remap next-line
] 'image-next-line
)
316 (define-key map
[remap scroll-up
] 'image-scroll-up
)
317 (define-key map
[remap scroll-down
] 'image-scroll-down
)
318 (define-key map
[remap scroll-up-command
] 'image-scroll-up
)
319 (define-key map
[remap scroll-down-command
] 'image-scroll-down
)
320 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
321 (define-key map
[remap move-end-of-line
] 'image-eol
)
322 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
323 (define-key map
[remap end-of-buffer
] 'image-eob
)
325 "Major mode keymap for viewing images in Image mode.")
327 (defvar image-minor-mode-map
328 (let ((map (make-sparse-keymap)))
329 (define-key map
"\C-c\C-c" 'image-toggle-display
)
331 "Minor mode keymap for viewing images as text in Image mode.")
333 (defvar bookmark-make-record-function
)
335 (put 'image-mode
'mode-class
'special
)
339 "Major mode for image files.
340 You can use \\<image-mode-map>\\[image-toggle-display]
341 to toggle between display as an image and display as text."
345 (unless (display-images-p)
346 (error "Display does not support images"))
348 (kill-all-local-variables)
349 (setq major-mode
'image-mode
)
351 (if (not (image-get-display-property))
353 (image-toggle-display-image)
354 ;; If attempt to display the image fails.
355 (if (not (image-get-display-property))
356 (error "Invalid image")))
357 ;; Set next vars when image is already displayed but local
358 ;; variables were cleared by kill-all-local-variables
359 (setq cursor-type nil truncate-lines t
360 image-type
(plist-get (cdr (image-get-display-property)) :type
)))
362 (setq mode-name
(if image-type
(format "Image[%s]" image-type
) "Image"))
363 (use-local-map image-mode-map
)
365 ;; Use our own bookmarking function for images.
366 (set (make-local-variable 'bookmark-make-record-function
)
367 'image-bookmark-make-record
)
369 ;; Keep track of [vh]scroll when switching buffers
370 (image-mode-setup-winprops)
372 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
373 (add-hook 'after-revert-hook
'image-after-revert-hook nil t
)
374 (run-mode-hooks 'image-mode-hook
)
375 (message "%s" (concat
376 (substitute-command-keys
377 "Type \\[image-toggle-display] to view the image as ")
378 (if (image-get-display-property)
379 "text" "an image") ".")))
383 (if (called-interactively-p 'any
) 'error
'message
)
384 "Cannot display image: %s" (cdr err
)))))
387 (define-minor-mode image-minor-mode
388 "Toggle Image minor mode.
389 With arg, turn Image minor mode on if arg is positive, off otherwise.
390 It provides the key \\<image-mode-map>\\[image-toggle-display] \
391 to switch back to `image-mode'
392 to display an image file as the actual image."
393 nil
(:eval
(if image-type
(format " Image[%s]" image-type
) " Image"))
398 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)))
401 (defun image-mode-as-text ()
402 "Set a non-image mode as major mode in combination with image minor mode.
403 A non-image major mode found from `auto-mode-alist' or Fundamental mode
404 displays an image file as text. `image-minor-mode' provides the key
405 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
406 to display an image file as the actual image.
408 You can use `image-mode-as-text' in `auto-mode-alist' when you want
409 to display an image file as text initially.
411 See commands `image-mode' and `image-minor-mode' for more information
414 ;; image-mode-as-text = normal-mode + image-minor-mode
415 (let ((previous-image-type image-type
)) ; preserve `image-type'
416 (if image-mode-previous-major-mode
417 ;; Restore previous major mode that was already found by this
418 ;; function and cached in `image-mode-previous-major-mode'
419 (funcall image-mode-previous-major-mode
)
420 (let ((auto-mode-alist
423 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
424 '(image-mode image-mode-maybe image-mode-as-text
))
427 (magic-fallback-mode-alist
430 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
431 '(image-mode image-mode-maybe image-mode-as-text
))
433 magic-fallback-mode-alist
))))
435 (set (make-local-variable 'image-mode-previous-major-mode
) major-mode
)))
436 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
437 (setq image-type previous-image-type
)
438 ;; Enable image minor mode with `C-c C-c'.
440 ;; Show the image file as text.
441 (image-toggle-display-text)
442 (message "%s" (concat
443 (substitute-command-keys
444 "Type \\[image-toggle-display] to view the image as ")
445 (if (image-get-display-property)
446 "text" "an image") "."))))
448 (define-obsolete-function-alias 'image-mode-maybe
'image-mode
"23.2")
450 (defun image-toggle-display-text ()
451 "Show the image file as text.
452 Remove text properties that display the image."
453 (let ((inhibit-read-only t
)
455 (modified (buffer-modified-p)))
456 (remove-list-of-text-properties (point-min) (point-max)
457 '(display intangible read-nonsticky
458 read-only front-sticky
))
459 (set-buffer-modified-p modified
)
460 (if (called-interactively-p 'any
)
461 (message "Repeat this command to go back to displaying the image"))))
463 (defvar archive-superior-buffer
)
464 (defvar tar-superior-buffer
)
465 (declare-function image-flush
"image.c" (spec &optional frame
))
467 (defun image-toggle-display-image ()
468 "Show the image of the image file.
469 Turn the image data into a real image, but only if the whole file
471 (let* ((filename (buffer-file-name))
472 (data-p (not (and filename
473 (file-readable-p filename
)
474 (not (file-remote-p filename
))
475 (not (buffer-modified-p))
476 (not (and (boundp 'archive-superior-buffer
)
477 archive-superior-buffer
))
478 (not (and (boundp 'tar-superior-buffer
)
479 tar-superior-buffer
)))))
480 (file-or-data (if data-p
482 (buffer-substring-no-properties (point-min) (point-max)))
484 (type (image-type file-or-data nil data-p
))
485 (image0 (create-animated-image file-or-data type data-p
))
486 (image (append image0
487 (image-transform-properties image0
)
492 rear-nonsticky
(display intangible
)
493 read-only t front-sticky
(read-only)))
494 (inhibit-read-only t
)
496 (modified (buffer-modified-p)))
498 (let ((buffer-file-truename nil
)) ; avoid changing dir mtime by lock_file
499 (add-text-properties (point-min) (point-max) props
)
500 (restore-buffer-modified-p modified
))
501 ;; Inhibit the cursor when the buffer contains only an image,
502 ;; because cursors look very strange on top of images.
503 (setq cursor-type nil
)
504 ;; This just makes the arrow displayed in the right fringe
505 ;; area look correct when the image is wider than the window.
506 (setq truncate-lines t
)
507 ;; Allow navigation of large images
508 (set (make-local-variable 'auto-hscroll-mode
) nil
)
509 (setq image-type type
)
510 (if (eq major-mode
'image-mode
)
511 (setq mode-name
(format "Image[%s]" type
)))
512 (if (called-interactively-p 'any
)
513 (message "Repeat this command to go back to displaying the file as text"))))
515 (defun image-toggle-display ()
516 "Start or stop displaying an image file as the actual image.
517 This command toggles between `image-mode-as-text' showing the text of
518 the image file and `image-mode' showing the image as an image."
520 (if (image-get-display-property)
524 (defun image-after-revert-hook ()
525 (when (image-get-display-property)
526 (image-toggle-display-text)
527 ;; Update image display.
528 (redraw-frame (selected-frame))
529 (image-toggle-display-image)))
532 ;;; Support for bookmark.el
533 (declare-function bookmark-make-record-default
534 "bookmark" (&optional no-file no-context posn
))
535 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
536 (declare-function bookmark-default-handler
"bookmark" (bmk))
538 (defun image-bookmark-make-record ()
539 `(,@(bookmark-make-record-default nil
'no-context
0)
540 (image-type .
,image-type
)
541 (handler . image-bookmark-jump
)))
544 (defun image-bookmark-jump (bmk)
545 ;; This implements the `handler' function interface for record type
546 ;; returned by `bookmark-make-record-function', which see.
547 (prog1 (bookmark-default-handler bmk
)
548 (when (not (string= image-type
(bookmark-prop-get bmk
'image-type
)))
549 (image-toggle-display))))
552 (defvar image-transform-minor-mode-map
553 (let ((map (make-sparse-keymap)))
554 ; (define-key map [(control ?+)] 'image-scale-in)
555 ; (define-key map [(control ?-)] 'image-scale-out)
556 ; (define-key map [(control ?=)] 'image-scale-none)
557 ;; (define-key map "c f h" 'image-scale-fit-height)
558 ;; (define-key map "c ]" 'image-rotate-right)
560 "Minor mode keymap for transforming the view of images Image mode.")
562 (define-minor-mode image-transform-mode
563 "minor mode for scaleing and rotation"
564 nil
"image-transform"
565 image-transform-minor-mode-map
)
567 (defvar image-transform-resize nil
568 "The image resize operation. See the command
569 `image-transform-set-scale' for more information." )
571 (defvar image-transform-rotation
0.0)
574 (defun image-transform-properties (display)
575 "Calculate the display properties for transformations; scaling
578 ((size (image-size display t
))
581 ((and (numberp image-transform-resize
) (eq 100 image-transform-resize
))
583 ((numberp image-transform-resize
)
584 (* image-transform-resize
(cdr size
)))
585 ((eq image-transform-resize
'fit-height
)
586 (- (nth 3 (window-inside-pixel-edges)) (nth 1 (window-inside-pixel-edges))))
588 (width (if (eq image-transform-resize
'fit-width
)
589 (- (nth 2 (window-inside-pixel-edges)) (nth 0 (window-inside-pixel-edges))))))
591 `(,@(if height
(list :height height
))
592 ,@(if width
(list :width width
))
593 ,@(if (not (equal 0.0 image-transform-rotation
))
594 (list :rotation image-transform-rotation
))
595 ;;TODO fit-to-* should consider the rotation angle
598 (defun image-transform-set-scale (scale)
599 "SCALE sets the scaling for images. "
600 (interactive "nscale:")
601 (image-transform-set-resize (float scale
)))
603 (defun image-transform-fit-to-height ()
604 "Fit image height to window height. "
606 (image-transform-set-resize 'fit-height
))
608 (defun image-transform-fit-to-width ()
609 "Fit image width to window width. "
611 (image-transform-set-resize 'fit-width
))
613 (defun image-transform-set-resize (resize)
614 "Set the resize mode for images. The RESIZE value can be the
615 symbol fit-height which fits the image to the window height. The
616 symbol fit-width fits the image to the window width. A number
617 indicates a scaling factor. nil indicates scale to 100%. "
618 (setq image-transform-resize resize
)
619 (if (eq 'image-mode major-mode
) (image-toggle-display-image)))
621 (defun image-transform-set-rotation (rotation)
622 "Set the image ROTATION angle. "
623 (interactive "nrotation:")
624 ;;TODO 0 90 180 270 degrees are the only reasonable angles here
625 ;;otherwise combining with rescaling will get very awkward
626 (setq image-transform-rotation
(float rotation
))
627 (if (eq major-mode
'image-mode
) (image-toggle-display-image)))
629 (provide 'image-mode
)
631 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
632 ;;; image-mode.el ends here