1 ;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*-
3 ;; Copyright (C) 2005-2016 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.
36 ;; Consolidate with doc-view to make them work on directories of images or on
37 ;; image files containing various "pages".
42 (eval-when-compile (require 'cl-lib
))
44 ;;; Image mode window-info management.
46 (defvar-local image-mode-winprops-alist t
)
48 (defvar image-mode-new-window-functions nil
49 "Special hook run when image data is requested in a new window.
50 It is called with one argument, the initial WINPROPS.")
52 ;; FIXME this doesn't seem mature yet. Document in manual when it is.
53 (defvar image-transform-resize nil
54 "The image resize operation.
55 Its value should be one of the following:
56 - nil, meaning no resizing.
57 - `fit-height', meaning to fit the image to the window height.
58 - `fit-width', meaning to fit the image to the window width.
59 - A number, which is a scale factor (the default size is 1).")
61 (defvar image-transform-scale
1.0
62 "The scale factor of the image being displayed.")
64 (defvar image-transform-rotation
0.0
65 "Rotation angle for the image in the current Image mode buffer.")
67 (defvar image-transform-right-angle-fudge
0.0001
68 "Snap distance to a multiple of a right angle.
69 There's no deep theory behind the default value, it should just
70 be somewhat larger than ImageMagick's MagickEpsilon.")
72 (defun image-mode-winprops (&optional window cleanup
)
73 "Return winprops of WINDOW.
74 A winprops object has the shape (WINDOW . ALIST).
75 WINDOW defaults to `selected-window' if it displays the current buffer, and
76 otherwise it defaults to t, used for times when the buffer is not displayed."
79 (if (eq (current-buffer) (window-buffer)) (selected-window) t
)))
81 ((not (windowp window
))
82 (error "Not a window: %s" window
)))
84 (setq image-mode-winprops-alist
85 (delq nil
(mapcar (lambda (winprop)
86 (let ((w (car-safe winprop
)))
87 (if (or (not (windowp w
)) (window-live-p w
))
89 image-mode-winprops-alist
))))
90 (let ((winprops (assq window image-mode-winprops-alist
)))
91 ;; For new windows, set defaults from the latest.
93 ;; Move window to front.
94 (setq image-mode-winprops-alist
95 (cons winprops
(delq winprops image-mode-winprops-alist
)))
96 (setq winprops
(cons window
97 (copy-alist (cdar image-mode-winprops-alist
))))
98 ;; Add winprops before running the hook, to avoid inf-loops if the hook
99 ;; triggers window-configuration-change-hook.
100 (setq image-mode-winprops-alist
101 (cons winprops image-mode-winprops-alist
))
102 (run-hook-with-args 'image-mode-new-window-functions winprops
))
105 (defun image-mode-window-get (prop &optional winprops
)
106 (declare (gv-setter (lambda (val)
107 `(image-mode-window-put ,prop
,val
,winprops
))))
108 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
109 (cdr (assq prop
(cdr winprops
))))
111 (defun image-mode-window-put (prop val
&optional winprops
)
112 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
113 (unless (eq t
(car winprops
))
114 (image-mode-window-put prop val t
))
115 (setcdr winprops
(cons (cons prop val
)
116 (delq (assq prop
(cdr winprops
)) (cdr winprops
)))))
118 (defun image-set-window-vscroll (vscroll)
119 (setf (image-mode-window-get 'vscroll
) vscroll
)
120 (set-window-vscroll (selected-window) vscroll
))
122 (defun image-set-window-hscroll (ncol)
123 (setf (image-mode-window-get 'hscroll
) ncol
)
124 (set-window-hscroll (selected-window) ncol
))
126 (defun image-mode-reapply-winprops ()
127 ;; When set-window-buffer, set hscroll and vscroll to what they were
128 ;; last time the image was displayed in this window.
129 (when (listp image-mode-winprops-alist
)
130 ;; Beware: this call to image-mode-winprops can't be optimized away,
131 ;; because it not only gets the winprops data but sets it up if needed
132 ;; (e.g. it's used by doc-view to display the image in a new window).
133 (let* ((winprops (image-mode-winprops nil t
))
134 (hscroll (image-mode-window-get 'hscroll winprops
))
135 (vscroll (image-mode-window-get 'vscroll winprops
)))
136 (when (image-get-display-property) ;Only do it if we display an image!
137 (if hscroll
(set-window-hscroll (selected-window) hscroll
))
138 (if vscroll
(set-window-vscroll (selected-window) vscroll
))))))
140 (defun image-mode-setup-winprops ()
141 ;; Record current scroll settings.
142 (unless (listp image-mode-winprops-alist
)
143 (setq image-mode-winprops-alist nil
))
144 (add-hook 'window-configuration-change-hook
145 'image-mode-reapply-winprops nil t
))
147 ;;; Image scrolling functions
149 (defun image-get-display-property ()
150 (get-char-property (point-min) 'display
151 ;; There might be different images for different displays.
152 (if (eq (window-buffer) (current-buffer))
155 (declare-function image-size
"image.c" (spec &optional pixels frame
))
157 (defun image-display-size (spec &optional pixels frame
)
158 "Wrapper around `image-size', handling slice display properties.
159 Like `image-size', the return value is (WIDTH . HEIGHT).
160 WIDTH and HEIGHT are in canonical character units if PIXELS is
161 nil, and in pixel units if PIXELS is non-nil.
163 If SPEC is an image display property, this function is equivalent
164 to `image-size'. If SPEC is a list of properties containing
165 `image' and `slice' properties, return the display size taking
166 the slice property into account. If the list contains `image'
167 but not `slice', return the `image-size' of the specified image."
168 (if (eq (car spec
) 'image
)
169 (image-size spec pixels frame
)
170 (let ((image (assoc 'image spec
))
171 (slice (assoc 'slice spec
)))
172 (cond ((and image slice
)
174 (cons (nth 3 slice
) (nth 4 slice
))
175 (cons (/ (float (nth 3 slice
)) (frame-char-width frame
))
176 (/ (float (nth 4 slice
)) (frame-char-height frame
)))))
178 (image-size image pixels frame
))
180 (error "Invalid image specification: %s" spec
))))))
182 (defun image-forward-hscroll (&optional n
)
183 "Scroll image in current window to the left by N character widths.
184 Stop if the right edge of the image is reached."
188 (image-set-window-hscroll (max 0 (+ (window-hscroll) n
))))
190 (let* ((image (image-get-display-property))
191 (edges (window-inside-edges))
192 (win-width (- (nth 2 edges
) (nth 0 edges
)))
193 (img-width (ceiling (car (image-display-size image
)))))
194 (image-set-window-hscroll (min (max 0 (- img-width win-width
))
195 (+ n
(window-hscroll))))))))
197 (defun image-backward-hscroll (&optional n
)
198 "Scroll image in current window to the right by N character widths.
199 Stop if the left edge of the image is reached."
201 (image-forward-hscroll (- n
)))
203 (defun image-next-line (n)
204 "Scroll image in current window upward by N lines.
205 Stop if the bottom edge of the image is reached."
209 (image-set-window-vscroll (max 0 (+ (window-vscroll) n
))))
211 (let* ((image (image-get-display-property))
212 (edges (window-inside-edges))
213 (win-height (- (nth 3 edges
) (nth 1 edges
)))
214 (img-height (ceiling (cdr (image-display-size image
)))))
215 (image-set-window-vscroll (min (max 0 (- img-height win-height
))
216 (+ n
(window-vscroll))))))))
218 (defun image-previous-line (&optional n
)
219 "Scroll image in current window downward by N lines.
220 Stop if the top edge of the image is reached."
222 (image-next-line (- n
)))
224 (defun image-scroll-up (&optional n
)
225 "Scroll image in current window upward by N lines.
226 Stop if the bottom edge of the image is reached.
227 If ARG is omitted or nil, scroll upward by a near full screen.
228 A near full screen is `next-screen-context-lines' less than a full screen.
229 Negative ARG means scroll downward.
230 If ARG is the atom `-', scroll downward by nearly full screen.
231 When calling from a program, supply as argument a number, nil, or `-'."
234 (let* ((edges (window-inside-edges))
235 (win-height (- (nth 3 edges
) (nth 1 edges
))))
237 (max 0 (- win-height next-screen-context-lines
)))))
239 (let* ((edges (window-inside-edges))
240 (win-height (- (nth 3 edges
) (nth 1 edges
))))
242 (min 0 (- next-screen-context-lines win-height
)))))
243 (t (image-next-line (prefix-numeric-value n
)))))
245 (defun image-scroll-down (&optional n
)
246 "Scroll image in current window downward by N lines.
247 Stop if the top edge of the image is reached.
248 If ARG is omitted or nil, scroll downward by a near full screen.
249 A near full screen is `next-screen-context-lines' less than a full screen.
250 Negative ARG means scroll upward.
251 If ARG is the atom `-', scroll upward by nearly full screen.
252 When calling from a program, supply as argument a number, nil, or `-'."
255 (let* ((edges (window-inside-edges))
256 (win-height (- (nth 3 edges
) (nth 1 edges
))))
258 (min 0 (- next-screen-context-lines win-height
)))))
260 (let* ((edges (window-inside-edges))
261 (win-height (- (nth 3 edges
) (nth 1 edges
))))
263 (max 0 (- win-height next-screen-context-lines
)))))
264 (t (image-next-line (- (prefix-numeric-value n
))))))
266 (defun image-bol (arg)
267 "Scroll horizontally to the left edge of the image in the current window.
268 With argument ARG not nil or 1, move forward ARG - 1 lines first,
269 stopping if the top or bottom edge of the image is reached."
272 (/= (setq arg
(prefix-numeric-value arg
)) 1)
273 (image-next-line (- arg
1)))
274 (image-set-window-hscroll 0))
276 (defun image-eol (arg)
277 "Scroll horizontally to the right edge of the image in the current window.
278 With argument ARG not nil or 1, move forward ARG - 1 lines first,
279 stopping if the top or bottom edge of the image is reached."
282 (/= (setq arg
(prefix-numeric-value arg
)) 1)
283 (image-next-line (- arg
1)))
284 (let* ((image (image-get-display-property))
285 (edges (window-inside-edges))
286 (win-width (- (nth 2 edges
) (nth 0 edges
)))
287 (img-width (ceiling (car (image-display-size image
)))))
288 (image-set-window-hscroll (max 0 (- img-width win-width
)))))
291 "Scroll to the top-left corner of the image in the current window."
293 (image-set-window-hscroll 0)
294 (image-set-window-vscroll 0))
297 "Scroll to the bottom-right corner of the image in the current window."
299 (let* ((image (image-get-display-property))
300 (edges (window-inside-edges))
301 (win-width (- (nth 2 edges
) (nth 0 edges
)))
302 (img-width (ceiling (car (image-display-size image
))))
303 (win-height (- (nth 3 edges
) (nth 1 edges
)))
304 (img-height (ceiling (cdr (image-display-size image
)))))
305 (image-set-window-hscroll (max 0 (- img-width win-width
)))
306 (image-set-window-vscroll (max 0 (- img-height win-height
)))))
308 ;; Adjust frame and image size.
310 (defun image-mode-fit-frame (&optional frame toggle
)
311 "Fit FRAME to the current image.
312 If FRAME is omitted or nil, it defaults to the selected frame.
313 All other windows on the frame are deleted.
315 If called interactively, or if TOGGLE is non-nil, toggle between
316 fitting FRAME to the current image and restoring the size and
317 window configuration prior to the last `image-mode-fit-frame'
319 (interactive (list nil t
))
320 (let* ((buffer (current-buffer))
321 (display (image-get-display-property))
322 (size (image-display-size display
))
323 (saved (frame-parameter frame
'image-mode-saved-params
))
324 (window-configuration (current-window-configuration frame
))
325 (width (frame-width frame
))
326 (height (frame-height frame
)))
327 (with-selected-frame (or frame
(selected-frame))
328 (if (and toggle saved
329 (= (caar saved
) width
)
330 (= (cdar saved
) height
))
332 (set-frame-width frame
(car (nth 1 saved
)))
333 (set-frame-height frame
(cdr (nth 1 saved
)))
334 (set-window-configuration (nth 2 saved
))
335 (set-frame-parameter frame
'image-mode-saved-params nil
))
336 (delete-other-windows)
337 (switch-to-buffer buffer t t
)
338 (let* ((edges (window-inside-edges))
339 (inner-width (- (nth 2 edges
) (nth 0 edges
)))
340 (inner-height (- (nth 3 edges
) (nth 1 edges
))))
341 (set-frame-width frame
(+ (ceiling (car size
))
342 width
(- inner-width
)))
343 (set-frame-height frame
(+ (ceiling (cdr size
))
344 height
(- inner-height
)))
345 ;; The frame size after the above `set-frame-*' calls may
346 ;; differ from what we specified, due to window manager
347 ;; interference. We have to call `frame-width' and
348 ;; `frame-height' to get the actual results.
349 (set-frame-parameter frame
'image-mode-saved-params
350 (list (cons (frame-width)
353 window-configuration
)))))))
357 (defvar-local image-type nil
358 "The image type for the current Image mode buffer.")
360 (defvar-local image-multi-frame nil
361 "Non-nil if image for the current Image mode buffer has multiple frames.")
363 (defvar image-mode-previous-major-mode nil
364 "Internal variable to keep the previous non-image major mode.")
366 (defvar image-mode-map
367 (let ((map (make-sparse-keymap)))
368 (set-keymap-parent map special-mode-map
)
369 (define-key map
"\C-c\C-c" 'image-toggle-display
)
370 (define-key map
(kbd "SPC") 'image-scroll-up
)
371 (define-key map
(kbd "S-SPC") 'image-scroll-down
)
372 (define-key map
(kbd "DEL") 'image-scroll-down
)
373 (define-key map
(kbd "RET") 'image-toggle-animation
)
374 (define-key map
"F" 'image-goto-frame
)
375 (define-key map
"f" 'image-next-frame
)
376 (define-key map
"b" 'image-previous-frame
)
377 (define-key map
"n" 'image-next-file
)
378 (define-key map
"p" 'image-previous-file
)
379 (define-key map
"a+" 'image-increase-speed
)
380 (define-key map
"a-" 'image-decrease-speed
)
381 (define-key map
"a0" 'image-reset-speed
)
382 (define-key map
"ar" 'image-reverse-speed
)
383 (define-key map
"k" 'image-kill-buffer
)
384 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
385 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
386 (define-key map
[remap right-char
] 'image-forward-hscroll
)
387 (define-key map
[remap left-char
] 'image-backward-hscroll
)
388 (define-key map
[remap previous-line
] 'image-previous-line
)
389 (define-key map
[remap next-line
] 'image-next-line
)
390 (define-key map
[remap scroll-up
] 'image-scroll-up
)
391 (define-key map
[remap scroll-down
] 'image-scroll-down
)
392 (define-key map
[remap scroll-up-command
] 'image-scroll-up
)
393 (define-key map
[remap scroll-down-command
] 'image-scroll-down
)
394 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
395 (define-key map
[remap move-end-of-line
] 'image-eol
)
396 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
397 (define-key map
[remap end-of-buffer
] 'image-eob
)
398 (easy-menu-define image-mode-menu map
"Menu for Image mode."
400 ["Show as Text" image-toggle-display
:active t
401 :help
"Show image as text"]
403 ["Fit to Window Height" image-transform-fit-to-height
404 :visible
(eq image-type
'imagemagick
)
405 :help
"Resize image to match the window height"]
406 ["Fit to Window Width" image-transform-fit-to-width
407 :visible
(eq image-type
'imagemagick
)
408 :help
"Resize image to match the window width"]
409 ["Rotate Image..." image-transform-set-rotation
410 :visible
(eq image-type
'imagemagick
)
411 :help
"Rotate the image"]
412 ["Reset Transformations" image-transform-reset
413 :visible
(eq image-type
'imagemagick
)
414 :help
"Reset all image transformations"]
419 (image-dired default-directory
))
420 :active default-directory
421 :help
"Show thumbnails for all images in this directory"]
422 ["Next Image" image-next-file
:active buffer-file-name
423 :help
"Move to next image in this directory"]
424 ["Previous Image" image-previous-file
:active buffer-file-name
425 :help
"Move to previous image in this directory"]
427 ["Fit Frame to Image" image-mode-fit-frame
:active t
428 :help
"Resize frame to match image"]
430 ["Animate Image" image-toggle-animation
:style toggle
431 :selected
(let ((image (image-get-display-property)))
432 (and image
(image-animate-timer image
)))
433 :active image-multi-frame
434 :help
"Toggle image animation"]
436 (lambda () (interactive)
437 (setq image-animate-loop
(not image-animate-loop
))
438 ;; FIXME this is a hacky way to make it affect a currently
440 (when (let ((image (image-get-display-property)))
441 (and image
(image-animate-timer image
)))
442 (image-toggle-animation)
443 (image-toggle-animation)))
444 :style toggle
:selected image-animate-loop
445 :active image-multi-frame
446 :help
"Animate images once, or forever?"]
447 ["Reverse Animation" image-reverse-speed
448 :style toggle
:selected
(let ((image (image-get-display-property)))
450 (image-animate-get-speed image
)
452 :active image-multi-frame
453 :help
"Reverse direction of this image's animation?"]
454 ["Speed Up Animation" image-increase-speed
455 :active image-multi-frame
456 :help
"Speed up this image's animation"]
457 ["Slow Down Animation" image-decrease-speed
458 :active image-multi-frame
459 :help
"Slow down this image's animation"]
460 ["Reset Animation Speed" image-reset-speed
461 :active image-multi-frame
462 :help
"Reset the speed of this image's animation"]
463 ["Next Frame" image-next-frame
:active image-multi-frame
464 :help
"Show the next frame of this image"]
465 ["Previous Frame" image-previous-frame
:active image-multi-frame
466 :help
"Show the previous frame of this image"]
467 ["Goto Frame..." image-goto-frame
:active image-multi-frame
468 :help
"Show a specific frame of this image"]
471 "Mode keymap for `image-mode'.")
473 (defvar image-minor-mode-map
474 (let ((map (make-sparse-keymap)))
475 (define-key map
"\C-c\C-c" 'image-toggle-display
)
477 "Mode keymap for `image-minor-mode'.")
479 (defvar bookmark-make-record-function
)
481 (put 'image-mode
'mode-class
'special
)
485 "Major mode for image files.
486 You can use \\<image-mode-map>\\[image-toggle-display]
487 to toggle between display as an image and display as text.
494 (unless (display-images-p)
495 (error "Display does not support images"))
497 (kill-all-local-variables)
498 (setq major-mode
'image-mode
)
500 (if (not (image-get-display-property))
502 (image-toggle-display-image)
503 ;; If attempt to display the image fails.
504 (if (not (image-get-display-property))
505 (error "Invalid image")))
506 ;; Set next vars when image is already displayed but local
507 ;; variables were cleared by kill-all-local-variables
508 (setq cursor-type nil truncate-lines t
509 image-type
(plist-get (cdr (image-get-display-property)) :type
)))
511 (setq mode-name
(if image-type
(format "Image[%s]" image-type
) "Image"))
512 (use-local-map image-mode-map
)
514 ;; Use our own bookmarking function for images.
515 (setq-local bookmark-make-record-function
516 #'image-bookmark-make-record
)
518 ;; Keep track of [vh]scroll when switching buffers
519 (image-mode-setup-winprops)
521 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
522 (add-hook 'after-revert-hook
'image-after-revert-hook nil t
)
523 (run-mode-hooks 'image-mode-hook
)
524 (let ((image (image-get-display-property))
525 (msg1 (substitute-command-keys
526 "Type \\[image-toggle-display] to view the image as "))
530 (message "%s" (concat msg1
"an image.")))
531 ((setq animated
(image-multi-frame-p image
))
532 (setq image-multi-frame t
538 (1+ (image-current-frame ',image
))
542 mouse-3: Previous frame"
543 'mouse-face
'mode-line-highlight
548 (down-mouse-1 . image-next-frame
)
549 (down-mouse-3 . image-previous-frame
)))))))
551 (concat msg1
"text. This image has multiple frames.")))
552 ;;; (substitute-command-keys
553 ;;; "\\[image-toggle-animation] to animate."))))
555 (message "%s" (concat msg1
"text."))))))
560 (if (called-interactively-p 'any
) 'error
'message
)
561 "Cannot display image: %s" (cdr err
)))))
564 (define-minor-mode image-minor-mode
565 "Toggle Image minor mode in this buffer.
566 With a prefix argument ARG, enable Image minor mode if ARG is
567 positive, and disable it otherwise. If called from Lisp, enable
568 the mode if ARG is omitted or nil.
570 Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
571 to switch back to `image-mode' and display an image file as the
573 nil
(:eval
(if image-type
(format " Image[%s]" image-type
) " Image"))
578 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)))
581 (defun image-mode-as-text ()
582 "Set a non-image mode as major mode in combination with image minor mode.
583 A non-image major mode found from `auto-mode-alist' or Fundamental mode
584 displays an image file as text. `image-minor-mode' provides the key
585 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
586 to display an image file as the actual image.
588 You can use `image-mode-as-text' in `auto-mode-alist' when you want
589 to display an image file as text initially.
591 See commands `image-mode' and `image-minor-mode' for more information
594 ;; image-mode-as-text = normal-mode + image-minor-mode
595 (let ((previous-image-type image-type
)) ; preserve `image-type'
596 (if image-mode-previous-major-mode
597 ;; Restore previous major mode that was already found by this
598 ;; function and cached in `image-mode-previous-major-mode'
599 (funcall image-mode-previous-major-mode
)
600 (let ((auto-mode-alist
603 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
604 '(image-mode image-mode-maybe image-mode-as-text
))
607 (magic-fallback-mode-alist
610 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
611 '(image-mode image-mode-maybe image-mode-as-text
))
613 magic-fallback-mode-alist
))))
615 (setq-local image-mode-previous-major-mode major-mode
)))
616 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
617 (setq image-type previous-image-type
)
618 ;; Enable image minor mode with `C-c C-c'.
620 ;; Show the image file as text.
621 (image-toggle-display-text)
622 (message "%s" (concat
623 (substitute-command-keys
624 "Type \\[image-toggle-display] to view the image as ")
625 (if (image-get-display-property)
626 "text" "an image") "."))))
628 (define-obsolete-function-alias 'image-mode-maybe
'image-mode
"23.2")
630 (defun image-toggle-display-text ()
631 "Show the image file as text.
632 Remove text properties that display the image."
633 (let ((inhibit-read-only t
)
635 (modified (buffer-modified-p)))
636 (remove-list-of-text-properties (point-min) (point-max)
637 '(display read-nonsticky
;; intangible
638 read-only front-sticky
))
639 (set-buffer-modified-p modified
)
640 (if (called-interactively-p 'any
)
641 (message "Repeat this command to go back to displaying the image"))))
643 (defvar archive-superior-buffer
)
644 (defvar tar-superior-buffer
)
645 (declare-function image-flush
"image.c" (spec &optional frame
))
647 (defun image-toggle-display-image ()
648 "Show the image of the image file.
649 Turn the image data into a real image, but only if the whole file
651 (unless (derived-mode-p 'image-mode
)
652 (error "The buffer is not in Image mode"))
653 (let* ((filename (buffer-file-name))
654 (data-p (not (and filename
655 (file-readable-p filename
)
656 (not (file-remote-p filename
))
657 (not (buffer-modified-p))
658 (not (and (boundp 'archive-superior-buffer
)
659 archive-superior-buffer
))
660 (not (and (boundp 'tar-superior-buffer
)
661 tar-superior-buffer
))
662 ;; This means the buffer holds the
663 ;; decrypted content (bug#21870).
664 (not (and (boundp 'epa-file-encrypt-to
)
666 'epa-file-encrypt-to
))))))
667 (file-or-data (if data-p
669 (buffer-substring-no-properties (point-min) (point-max)))
671 ;; If we have a `fit-width' or a `fit-height', don't limit
672 ;; the size of the image to the window size.
673 (edges (and (null image-transform-resize
)
674 (window-inside-pixel-edges
675 (get-buffer-window (current-buffer)))))
676 (type (if (fboundp 'imagemagick-types
)
678 (image-type file-or-data nil data-p
)))
679 (image (if (not edges
)
680 (create-image file-or-data type data-p
)
681 (create-image file-or-data type data-p
682 :max-width
(- (nth 2 edges
) (nth 0 edges
))
683 :max-height
(- (nth 3 edges
) (nth 1 edges
)))))
684 (inhibit-read-only t
)
686 (modified (buffer-modified-p))
689 ;; Discard any stale image data before looking it up again.
691 (setq image
(append image
(image-transform-properties image
)))
695 rear-nonsticky
(display) ;; intangible
696 read-only t front-sticky
(read-only)))
698 (let ((buffer-file-truename nil
)) ; avoid changing dir mtime by lock_file
699 (add-text-properties (point-min) (point-max) props
)
700 (restore-buffer-modified-p modified
))
701 ;; Inhibit the cursor when the buffer contains only an image,
702 ;; because cursors look very strange on top of images.
703 (setq cursor-type nil
)
704 ;; This just makes the arrow displayed in the right fringe
705 ;; area look correct when the image is wider than the window.
706 (setq truncate-lines t
)
707 ;; Disable adding a newline at the end of the image file when it
708 ;; is written with, e.g., C-x C-w.
709 (if (coding-system-equal (coding-system-base buffer-file-coding-system
)
711 (setq-local find-file-literally t
))
712 ;; Allow navigation of large images.
713 (setq-local auto-hscroll-mode nil
)
714 (setq image-type type
)
715 (if (eq major-mode
'image-mode
)
716 (setq mode-name
(format "Image[%s]" type
)))
717 (image-transform-check-size)
718 (if (called-interactively-p 'any
)
719 (message "Repeat this command to go back to displaying the file as text"))))
721 (defun image-toggle-display ()
722 "Toggle between image and text display.
723 If the current buffer is displaying an image file as an image,
724 call `image-mode-as-text' to switch to text. Otherwise, display
725 the image by calling `image-mode'."
727 (if (image-get-display-property)
731 (defun image-kill-buffer ()
732 "Kill the current buffer."
734 (kill-buffer (current-buffer)))
736 (defun image-after-revert-hook ()
737 (when (image-get-display-property)
738 (image-toggle-display-text)
739 ;; Update image display.
740 (mapc (lambda (window) (redraw-frame (window-frame window
)))
741 (get-buffer-window-list (current-buffer) 'nomini
'visible
))
742 (image-toggle-display-image)))
747 (defcustom image-animate-loop nil
748 "Non-nil means animated images loop forever, rather than playing once."
753 (defun image-toggle-animation ()
754 "Start or stop animating the current image.
755 If `image-animate-loop' is non-nil, animation loops forever.
756 Otherwise it plays once, then stops."
758 (let ((image (image-get-display-property))
762 (error "No image is present"))
763 ((null (setq animation
(image-multi-frame-p image
)))
764 (message "No image animation."))
766 (let ((timer (image-animate-timer image
)))
769 (let ((index (plist-get (cdr image
) :index
)))
770 ;; If we're at the end, restart.
772 (>= index
(1- (car animation
)))
774 (image-animate image index
775 (if image-animate-loop t
)))))))))
777 (defun image--set-speed (speed &optional multiply
)
778 "Set speed of an animated image to SPEED.
779 If MULTIPLY is non-nil, treat SPEED as a multiplication factor.
780 If SPEED is `reset', reset the magnitude of the speed to 1."
781 (let ((image (image-get-display-property)))
784 (error "No image is present"))
785 ((null image-multi-frame
)
786 (message "No image animation."))
788 (if (eq speed
'reset
)
789 (setq speed
(if (< (image-animate-get-speed image
) 0)
792 (image-animate-set-speed image speed multiply
)
793 ;; FIXME Hack to refresh an active image.
794 (when (image-animate-timer image
)
795 (image-toggle-animation)
796 (image-toggle-animation))
797 (message "Image speed is now %s" (image-animate-get-speed image
))))))
799 (defun image-increase-speed ()
800 "Increase the speed of current animated image by a factor of 2."
802 (image--set-speed 2 t
))
804 (defun image-decrease-speed ()
805 "Decrease the speed of current animated image by a factor of 2."
807 (image--set-speed 0.5 t
))
809 (defun image-reverse-speed ()
810 "Reverse the animation of the current image."
812 (image--set-speed -
1 t
))
814 (defun image-reset-speed ()
815 "Reset the animation speed of the current image."
817 (image--set-speed 'reset
))
819 (defun image-goto-frame (n &optional relative
)
820 "Show frame N of a multi-frame image.
821 Optional argument OFFSET non-nil means interpret N as relative to the
822 current frame. Frames are indexed from 1."
824 (list (or current-prefix-arg
825 (read-number "Show frame number: "))))
826 (let ((image (image-get-display-property)))
829 (error "No image is present"))
830 ((null image-multi-frame
)
831 (message "No image animation."))
833 (image-show-frame image
835 (+ n
(image-current-frame image
))
838 (defun image-next-frame (&optional n
)
839 "Switch to the next frame of a multi-frame image.
840 With optional argument N, switch to the Nth frame after the current one.
841 If N is negative, switch to the Nth frame before the current one."
843 (image-goto-frame n t
))
845 (defun image-previous-frame (&optional n
)
846 "Switch to the previous frame of a multi-frame image.
847 With optional argument N, switch to the Nth frame before the current one.
848 If N is negative, switch to the Nth frame after the current one."
850 (image-next-frame (- n
)))
853 ;;; Switching to the next/previous image
855 (defun image-next-file (&optional n
)
856 "Visit the next image in the same directory as the current image file.
857 With optional argument N, visit the Nth image file after the
858 current one, in cyclic alphabetical order.
860 This command visits the specified file via `find-alternate-file',
861 replacing the current Image mode buffer."
863 (unless (derived-mode-p 'image-mode
)
864 (error "The buffer is not in Image mode"))
865 (unless buffer-file-name
866 (error "The current image is not associated with a file"))
867 (let* ((file (file-name-nondirectory buffer-file-name
))
868 (images (image-mode--images-in-directory file
))
870 (catch 'image-visit-next-file
873 (throw 'image-visit-next-file
(1+ idx
)))
874 (setq idx
(1+ idx
))))
875 (setq idx
(mod (+ idx
(or n
1)) (length images
)))
876 (find-alternate-file (nth idx images
))))
878 (defun image-previous-file (&optional n
)
879 "Visit the preceding image in the same directory as the current file.
880 With optional argument N, visit the Nth image file preceding the
881 current one, in cyclic alphabetical order.
883 This command visits the specified file via `find-alternate-file',
884 replacing the current Image mode buffer."
886 (image-next-file (- n
)))
888 (defun image-mode--images-in-directory (file)
889 (let* ((dir (file-name-directory buffer-file-name
))
890 (files (directory-files dir nil
891 (image-file-name-regexp) t
)))
892 ;; Add the current file to the list of images if necessary, in
893 ;; case it does not match `image-file-name-regexp'.
894 (unless (member file files
)
896 (sort files
'string-lessp
)))
899 ;;; Support for bookmark.el
900 (declare-function bookmark-make-record-default
901 "bookmark" (&optional no-file no-context posn
))
902 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
903 (declare-function bookmark-default-handler
"bookmark" (bmk))
905 (defun image-bookmark-make-record ()
906 `(,@(bookmark-make-record-default nil
'no-context
0)
907 (image-type .
,image-type
)
908 (handler . image-bookmark-jump
)))
911 (defun image-bookmark-jump (bmk)
912 ;; This implements the `handler' function interface for record type
913 ;; returned by `bookmark-make-record-function', which see.
914 (prog1 (bookmark-default-handler bmk
)
915 (when (not (string= image-type
(bookmark-prop-get bmk
'image-type
)))
916 (image-toggle-display))))
919 ;; Not yet implemented.
920 ;; (defvar image-transform-minor-mode-map
921 ;; (let ((map (make-sparse-keymap)))
922 ;; ;; (define-key map [(control ?+)] 'image-scale-in)
923 ;; ;; (define-key map [(control ?-)] 'image-scale-out)
924 ;; ;; (define-key map [(control ?=)] 'image-scale-none)
925 ;; ;; (define-key map "c f h" 'image-scale-fit-height)
926 ;; ;; (define-key map "c ]" 'image-rotate-right)
928 ;; "Minor mode keymap `image-transform-mode'.")
930 ;; (define-minor-mode image-transform-mode
931 ;; "Minor mode for scaling and rotating images.
932 ;; With a prefix argument ARG, enable the mode if ARG is positive,
933 ;; and disable it otherwise. If called from Lisp, enable the mode
934 ;; if ARG is omitted or nil. This minor mode requires Emacs to have
935 ;; been compiled with ImageMagick support."
936 ;; nil "image-transform" image-transform-minor-mode-map)
939 (defsubst image-transform-width
(width height
)
940 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
941 The rotation angle is the value of `image-transform-rotation' in degrees."
942 (let ((angle (degrees-to-radians image-transform-rotation
)))
943 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
944 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
945 ;; rotation by the angle A. The projections onto the first axis
946 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
947 ;; (h/2) sin A, and the difference between the largest and the
948 ;; smallest of the four values is the expression below.
949 (+ (* width
(abs (cos angle
))) (* height
(abs (sin angle
))))))
951 ;; The following comment and code snippet are from
952 ;; ImageMagick-6.7.4-4/magick/distort.c
954 ;; /* Set the output image geometry to calculated 'best fit'.
955 ;; Yes this tends to 'over do' the file image size, ON PURPOSE!
956 ;; Do not do this for DePolar which needs to be exact for virtual tiling.
958 ;; if ( fix_bounds ) {
959 ;; geometry.x = (ssize_t) floor(min.x-0.5);
960 ;; geometry.y = (ssize_t) floor(min.y-0.5);
961 ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
962 ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
965 ;; Other parts of the same file show that here the origin is in the
966 ;; left lower corner of the image rectangle, the center of the
967 ;; rotation is the center of the rectangle and min.x and max.x
968 ;; (resp. min.y and max.y) are the smallest and the largest of the
969 ;; projections of the vertices onto the first (resp. second) axis.
971 (defun image-transform-fit-width (width height length
)
972 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
973 The rotation angle is the value of `image-transform-rotation'.
974 Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
975 an \"approximately uniformly\" scaled W x H rectangle, which
976 currently means that w is one of floor(s W) + {0, 1, -1} and h is
977 floor(s H), where s can be recovered as the value of `image-transform-scale'.
978 The value of `image-transform-rotation' may be replaced by
979 a slightly different angle. Currently this is done for values
980 close to a multiple of 90, see `image-transform-right-angle-fudge'."
981 (cond ((< (abs (- (mod (+ image-transform-rotation
90) 180) 90))
982 image-transform-right-angle-fudge
)
983 (cl-assert (not (zerop width
)) t
)
984 (setq image-transform-rotation
985 (float (round image-transform-rotation
))
986 image-transform-scale
(/ (float length
) width
))
988 ((< (abs (- (mod (+ image-transform-rotation
45) 90) 45))
989 image-transform-right-angle-fudge
)
990 (cl-assert (not (zerop height
)) t
)
991 (setq image-transform-rotation
992 (float (round image-transform-rotation
))
993 image-transform-scale
(/ (float length
) height
))
996 (cl-assert (not (and (zerop width
) (zerop height
))) t
)
997 (setq image-transform-scale
998 (/ (float (1- length
)) (image-transform-width width height
)))
999 ;; Assume we have a w x h image and an angle A, and let l =
1000 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
1001 ;; of the bounding box of the rotated image, as calculated by
1002 ;; `image-transform-width'. The code snippet quoted above
1003 ;; means that ImageMagick puts the rotated image in
1004 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
1005 ;; Elementary considerations show that this is equivalent to
1006 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
1007 ;; the given `length' parameter and our job is to determine
1008 ;; reasonable values for w and h which satisfy these
1010 (let ((w (floor (* image-transform-scale width
)))
1011 (h (floor (* image-transform-scale height
))))
1012 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
1013 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
1014 ;; hence l(w, h) > (L-1) - 2 = L-3.
1016 (cond ((= (mod w
2) (mod length
2))
1018 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
1020 ((<= (image-transform-width (1+ w
) h
) (1- length
))
1022 ;; No, it doesn't, but this implies that l(w-1, h) =
1023 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
1024 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
1029 (defun image-transform-check-size ()
1030 "Check that the image exactly fits the width/height of the window.
1032 Do this for an image of type `imagemagick' to make sure that the
1033 elisp code matches the way ImageMagick computes the bounding box
1034 of a rotated image."
1035 (when (and (not (numberp image-transform-resize
))
1036 (boundp 'image-type
)
1037 (eq image-type
'imagemagick
))
1038 (let ((size (image-display-size (image-get-display-property) t
)))
1039 (cond ((eq image-transform-resize
'fit-width
)
1040 (cl-assert (= (car size
)
1041 (- (nth 2 (window-inside-pixel-edges))
1042 (nth 0 (window-inside-pixel-edges))))
1044 ((eq image-transform-resize
'fit-height
)
1045 (cl-assert (= (cdr size
)
1046 (- (nth 3 (window-inside-pixel-edges))
1047 (nth 1 (window-inside-pixel-edges))))
1050 (defun image-transform-properties (spec)
1051 "Return rescaling/rotation properties for image SPEC.
1052 These properties are determined by the Image mode variables
1053 `image-transform-resize' and `image-transform-rotation'. The
1054 return value is suitable for appending to an image spec.
1056 Rescaling and rotation properties only take effect if Emacs is
1057 compiled with ImageMagick support."
1058 (setq image-transform-scale
1.0)
1059 (when (or image-transform-resize
1060 (/= image-transform-rotation
0.0))
1061 ;; Note: `image-size' looks up and thus caches the untransformed
1062 ;; image. There's no easy way to prevent that.
1063 (let* ((size (image-size spec t
))
1066 ((numberp image-transform-resize
)
1067 (unless (= image-transform-resize
1)
1068 (setq image-transform-scale image-transform-resize
)
1069 (cons nil
(floor (* image-transform-resize
(cdr size
))))))
1070 ((eq image-transform-resize
'fit-width
)
1071 (image-transform-fit-width
1072 (car size
) (cdr size
)
1073 (- (nth 2 (window-inside-pixel-edges))
1074 (nth 0 (window-inside-pixel-edges)))))
1075 ((eq image-transform-resize
'fit-height
)
1076 (let ((res (image-transform-fit-width
1077 (cdr size
) (car size
)
1078 (- (nth 3 (window-inside-pixel-edges))
1079 (nth 1 (window-inside-pixel-edges))))))
1080 (cons (cdr res
) (car res
)))))))
1081 `(,@(when (car resized
)
1082 (list :width
(car resized
)))
1083 ,@(when (cdr resized
)
1084 (list :height
(cdr resized
)))
1085 ,@(unless (= 0.0 image-transform-rotation
)
1086 (list :rotation image-transform-rotation
))))))
1088 (defun image-transform-set-scale (scale)
1089 "Prompt for a number, and resize the current image by that amount.
1090 This command has no effect unless Emacs is compiled with
1091 ImageMagick support."
1092 (interactive "nScale: ")
1093 (setq image-transform-resize scale
)
1094 (image-toggle-display-image))
1096 (defun image-transform-fit-to-height ()
1097 "Fit the current image to the height of the current window.
1098 This command has no effect unless Emacs is compiled with
1099 ImageMagick support."
1101 (setq image-transform-resize
'fit-height
)
1102 (image-toggle-display-image))
1104 (defun image-transform-fit-to-width ()
1105 "Fit the current image to the width of the current window.
1106 This command has no effect unless Emacs is compiled with
1107 ImageMagick support."
1109 (setq image-transform-resize
'fit-width
)
1110 (image-toggle-display-image))
1112 (defun image-transform-set-rotation (rotation)
1113 "Prompt for an angle ROTATION, and rotate the image by that amount.
1114 ROTATION should be in degrees. This command has no effect unless
1115 Emacs is compiled with ImageMagick support."
1116 (interactive "nRotation angle (in degrees): ")
1117 (setq image-transform-rotation
(float (mod rotation
360)))
1118 (image-toggle-display-image))
1120 (defun image-transform-reset ()
1121 "Display the current image with the default size and rotation.
1122 This command has no effect unless Emacs is compiled with
1123 ImageMagick support."
1125 (setq image-transform-resize nil
1126 image-transform-rotation
0.0
1127 image-transform-scale
1)
1128 (image-toggle-display-image))
1130 (provide 'image-mode
)
1132 ;;; image-mode.el ends here