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
))
156 (declare-function xwidget-info
"xwidget.c" (xwidget))
157 (declare-function xwidget-at
"xwidget.el" (pos))
159 (defun image-display-size (spec &optional pixels frame
)
160 "Wrapper around `image-size', handling slice display properties.
161 Like `image-size', the return value is (WIDTH . HEIGHT).
162 WIDTH and HEIGHT are in canonical character units if PIXELS is
163 nil, and in pixel units if PIXELS is non-nil.
165 If SPEC is an image display property, this function is equivalent to
166 `image-size'. If SPEC represents an xwidget object, defer to `xwidget-info'.
167 If SPEC is a list of properties containing `image' and `slice' properties,
168 return the display size taking the slice property into account. If the list
169 contains `image' but not `slice', return the `image-size' of the specified
171 (cond ((eq (car spec
) 'xwidget
)
172 (let ((xwi (xwidget-info (xwidget-at (point-min)))))
173 (cons (aref xwi
2) (aref xwi
3))))
174 ((eq (car spec
) 'image
)
175 (image-size spec pixels frame
))
176 (t (let ((image (assoc 'image spec
))
177 (slice (assoc 'slice spec
)))
178 (cond ((and image slice
)
180 (cons (nth 3 slice
) (nth 4 slice
))
181 (cons (/ (float (nth 3 slice
)) (frame-char-width frame
))
182 (/ (float (nth 4 slice
))
183 (frame-char-height frame
)))))
185 (image-size image pixels frame
))
187 (error "Invalid image specification: %s" spec
)))))))
189 (defun image-forward-hscroll (&optional n
)
190 "Scroll image in current window to the left by N character widths.
191 Stop if the right edge of the image is reached."
195 (image-set-window-hscroll (max 0 (+ (window-hscroll) n
))))
197 (let* ((image (image-get-display-property))
198 (edges (window-inside-edges))
199 (win-width (- (nth 2 edges
) (nth 0 edges
)))
200 (img-width (ceiling (car (image-display-size image
)))))
201 (image-set-window-hscroll (min (max 0 (- img-width win-width
))
202 (+ n
(window-hscroll))))))))
204 (defun image-backward-hscroll (&optional n
)
205 "Scroll image in current window to the right by N character widths.
206 Stop if the left edge of the image is reached."
208 (image-forward-hscroll (- n
)))
210 (defun image-next-line (n)
211 "Scroll image in current window upward by N lines.
212 Stop if the bottom edge of the image is reached."
216 (image-set-window-vscroll (max 0 (+ (window-vscroll) n
))))
218 (let* ((image (image-get-display-property))
219 (edges (window-inside-edges))
220 (win-height (- (nth 3 edges
) (nth 1 edges
)))
221 (img-height (ceiling (cdr (image-display-size image
)))))
222 (image-set-window-vscroll (min (max 0 (- img-height win-height
))
223 (+ n
(window-vscroll))))))))
225 (defun image-previous-line (&optional n
)
226 "Scroll image in current window downward by N lines.
227 Stop if the top edge of the image is reached."
229 (image-next-line (- n
)))
231 (defun image-scroll-up (&optional n
)
232 "Scroll image in current window upward by N lines.
233 Stop if the bottom edge of the image is reached.
234 If ARG is omitted or nil, scroll upward by a near full screen.
235 A near full screen is `next-screen-context-lines' less than a full screen.
236 Negative ARG means scroll downward.
237 If ARG is the atom `-', scroll downward by nearly full screen.
238 When calling from a program, supply as argument a number, nil, or `-'."
241 (let* ((edges (window-inside-edges))
242 (win-height (- (nth 3 edges
) (nth 1 edges
))))
244 (max 0 (- win-height next-screen-context-lines
)))))
246 (let* ((edges (window-inside-edges))
247 (win-height (- (nth 3 edges
) (nth 1 edges
))))
249 (min 0 (- next-screen-context-lines win-height
)))))
250 (t (image-next-line (prefix-numeric-value n
)))))
252 (defun image-scroll-down (&optional n
)
253 "Scroll image in current window downward by N lines.
254 Stop if the top edge of the image is reached.
255 If ARG is omitted or nil, scroll downward by a near full screen.
256 A near full screen is `next-screen-context-lines' less than a full screen.
257 Negative ARG means scroll upward.
258 If ARG is the atom `-', scroll upward by nearly full screen.
259 When calling from a program, supply as argument a number, nil, or `-'."
262 (let* ((edges (window-inside-edges))
263 (win-height (- (nth 3 edges
) (nth 1 edges
))))
265 (min 0 (- next-screen-context-lines win-height
)))))
267 (let* ((edges (window-inside-edges))
268 (win-height (- (nth 3 edges
) (nth 1 edges
))))
270 (max 0 (- win-height next-screen-context-lines
)))))
271 (t (image-next-line (- (prefix-numeric-value n
))))))
273 (defun image-bol (arg)
274 "Scroll horizontally to the left edge of the image in the current window.
275 With argument ARG not nil or 1, move forward ARG - 1 lines first,
276 stopping if the top or bottom edge of the image is reached."
279 (/= (setq arg
(prefix-numeric-value arg
)) 1)
280 (image-next-line (- arg
1)))
281 (image-set-window-hscroll 0))
283 (defun image-eol (arg)
284 "Scroll horizontally to the right edge of the image in the current window.
285 With argument ARG not nil or 1, move forward ARG - 1 lines first,
286 stopping if the top or bottom edge of the image is reached."
289 (/= (setq arg
(prefix-numeric-value arg
)) 1)
290 (image-next-line (- arg
1)))
291 (let* ((image (image-get-display-property))
292 (edges (window-inside-edges))
293 (win-width (- (nth 2 edges
) (nth 0 edges
)))
294 (img-width (ceiling (car (image-display-size image
)))))
295 (image-set-window-hscroll (max 0 (- img-width win-width
)))))
298 "Scroll to the top-left corner of the image in the current window."
300 (image-set-window-hscroll 0)
301 (image-set-window-vscroll 0))
304 "Scroll to the bottom-right corner of the image in the current window."
306 (let* ((image (image-get-display-property))
307 (edges (window-inside-edges))
308 (win-width (- (nth 2 edges
) (nth 0 edges
)))
309 (img-width (ceiling (car (image-display-size image
))))
310 (win-height (- (nth 3 edges
) (nth 1 edges
)))
311 (img-height (ceiling (cdr (image-display-size image
)))))
312 (image-set-window-hscroll (max 0 (- img-width win-width
)))
313 (image-set-window-vscroll (max 0 (- img-height win-height
)))))
315 ;; Adjust frame and image size.
317 (defun image-mode-fit-frame (&optional frame toggle
)
318 "Fit FRAME to the current image.
319 If FRAME is omitted or nil, it defaults to the selected frame.
320 All other windows on the frame are deleted.
322 If called interactively, or if TOGGLE is non-nil, toggle between
323 fitting FRAME to the current image and restoring the size and
324 window configuration prior to the last `image-mode-fit-frame'
326 (interactive (list nil t
))
327 (let* ((buffer (current-buffer))
328 (display (image-get-display-property))
329 (size (image-display-size display
))
330 (saved (frame-parameter frame
'image-mode-saved-params
))
331 (window-configuration (current-window-configuration frame
))
332 (width (frame-width frame
))
333 (height (frame-height frame
)))
334 (with-selected-frame (or frame
(selected-frame))
335 (if (and toggle saved
336 (= (caar saved
) width
)
337 (= (cdar saved
) height
))
339 (set-frame-width frame
(car (nth 1 saved
)))
340 (set-frame-height frame
(cdr (nth 1 saved
)))
341 (set-window-configuration (nth 2 saved
))
342 (set-frame-parameter frame
'image-mode-saved-params nil
))
343 (delete-other-windows)
344 (switch-to-buffer buffer t t
)
345 (let* ((edges (window-inside-edges))
346 (inner-width (- (nth 2 edges
) (nth 0 edges
)))
347 (inner-height (- (nth 3 edges
) (nth 1 edges
))))
348 (set-frame-width frame
(+ (ceiling (car size
))
349 width
(- inner-width
)))
350 (set-frame-height frame
(+ (ceiling (cdr size
))
351 height
(- inner-height
)))
352 ;; The frame size after the above `set-frame-*' calls may
353 ;; differ from what we specified, due to window manager
354 ;; interference. We have to call `frame-width' and
355 ;; `frame-height' to get the actual results.
356 (set-frame-parameter frame
'image-mode-saved-params
357 (list (cons (frame-width)
360 window-configuration
)))))))
364 (defvar-local image-type nil
365 "The image type for the current Image mode buffer.")
367 (defvar-local image-multi-frame nil
368 "Non-nil if image for the current Image mode buffer has multiple frames.")
370 (defvar image-mode-previous-major-mode nil
371 "Internal variable to keep the previous non-image major mode.")
373 (defvar image-mode-map
374 (let ((map (make-sparse-keymap)))
375 (set-keymap-parent map special-mode-map
)
376 (define-key map
"\C-c\C-c" 'image-toggle-display
)
377 (define-key map
(kbd "SPC") 'image-scroll-up
)
378 (define-key map
(kbd "S-SPC") 'image-scroll-down
)
379 (define-key map
(kbd "DEL") 'image-scroll-down
)
380 (define-key map
(kbd "RET") 'image-toggle-animation
)
381 (define-key map
"F" 'image-goto-frame
)
382 (define-key map
"f" 'image-next-frame
)
383 (define-key map
"b" 'image-previous-frame
)
384 (define-key map
"n" 'image-next-file
)
385 (define-key map
"p" 'image-previous-file
)
386 (define-key map
"a+" 'image-increase-speed
)
387 (define-key map
"a-" 'image-decrease-speed
)
388 (define-key map
"a0" 'image-reset-speed
)
389 (define-key map
"ar" 'image-reverse-speed
)
390 (define-key map
"k" 'image-kill-buffer
)
391 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
392 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
393 (define-key map
[remap right-char
] 'image-forward-hscroll
)
394 (define-key map
[remap left-char
] 'image-backward-hscroll
)
395 (define-key map
[remap previous-line
] 'image-previous-line
)
396 (define-key map
[remap next-line
] 'image-next-line
)
397 (define-key map
[remap scroll-up
] 'image-scroll-up
)
398 (define-key map
[remap scroll-down
] 'image-scroll-down
)
399 (define-key map
[remap scroll-up-command
] 'image-scroll-up
)
400 (define-key map
[remap scroll-down-command
] 'image-scroll-down
)
401 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
402 (define-key map
[remap move-end-of-line
] 'image-eol
)
403 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
404 (define-key map
[remap end-of-buffer
] 'image-eob
)
405 (easy-menu-define image-mode-menu map
"Menu for Image mode."
407 ["Show as Text" image-toggle-display
:active t
408 :help
"Show image as text"]
410 ["Fit to Window Height" image-transform-fit-to-height
411 :visible
(eq image-type
'imagemagick
)
412 :help
"Resize image to match the window height"]
413 ["Fit to Window Width" image-transform-fit-to-width
414 :visible
(eq image-type
'imagemagick
)
415 :help
"Resize image to match the window width"]
416 ["Rotate Image..." image-transform-set-rotation
417 :visible
(eq image-type
'imagemagick
)
418 :help
"Rotate the image"]
419 ["Reset Transformations" image-transform-reset
420 :visible
(eq image-type
'imagemagick
)
421 :help
"Reset all image transformations"]
426 (image-dired default-directory
))
427 :active default-directory
428 :help
"Show thumbnails for all images in this directory"]
429 ["Next Image" image-next-file
:active buffer-file-name
430 :help
"Move to next image in this directory"]
431 ["Previous Image" image-previous-file
:active buffer-file-name
432 :help
"Move to previous image in this directory"]
434 ["Fit Frame to Image" image-mode-fit-frame
:active t
435 :help
"Resize frame to match image"]
437 ["Animate Image" image-toggle-animation
:style toggle
438 :selected
(let ((image (image-get-display-property)))
439 (and image
(image-animate-timer image
)))
440 :active image-multi-frame
441 :help
"Toggle image animation"]
443 (lambda () (interactive)
444 (setq image-animate-loop
(not image-animate-loop
))
445 ;; FIXME this is a hacky way to make it affect a currently
447 (when (let ((image (image-get-display-property)))
448 (and image
(image-animate-timer image
)))
449 (image-toggle-animation)
450 (image-toggle-animation)))
451 :style toggle
:selected image-animate-loop
452 :active image-multi-frame
453 :help
"Animate images once, or forever?"]
454 ["Reverse Animation" image-reverse-speed
455 :style toggle
:selected
(let ((image (image-get-display-property)))
457 (image-animate-get-speed image
)
459 :active image-multi-frame
460 :help
"Reverse direction of this image's animation?"]
461 ["Speed Up Animation" image-increase-speed
462 :active image-multi-frame
463 :help
"Speed up this image's animation"]
464 ["Slow Down Animation" image-decrease-speed
465 :active image-multi-frame
466 :help
"Slow down this image's animation"]
467 ["Reset Animation Speed" image-reset-speed
468 :active image-multi-frame
469 :help
"Reset the speed of this image's animation"]
470 ["Next Frame" image-next-frame
:active image-multi-frame
471 :help
"Show the next frame of this image"]
472 ["Previous Frame" image-previous-frame
:active image-multi-frame
473 :help
"Show the previous frame of this image"]
474 ["Goto Frame..." image-goto-frame
:active image-multi-frame
475 :help
"Show a specific frame of this image"]
478 "Mode keymap for `image-mode'.")
480 (defvar image-minor-mode-map
481 (let ((map (make-sparse-keymap)))
482 (define-key map
"\C-c\C-c" 'image-toggle-display
)
484 "Mode keymap for `image-minor-mode'.")
486 (defvar bookmark-make-record-function
)
488 (put 'image-mode
'mode-class
'special
)
492 "Major mode for image files.
493 You can use \\<image-mode-map>\\[image-toggle-display]
494 to toggle between display as an image and display as text.
501 (unless (display-images-p)
502 (error "Display does not support images"))
504 (kill-all-local-variables)
505 (setq major-mode
'image-mode
)
507 (if (not (image-get-display-property))
509 (image-toggle-display-image)
510 ;; If attempt to display the image fails.
511 (if (not (image-get-display-property))
512 (error "Invalid image")))
513 ;; Set next vars when image is already displayed but local
514 ;; variables were cleared by kill-all-local-variables
515 (setq cursor-type nil truncate-lines t
516 image-type
(plist-get (cdr (image-get-display-property)) :type
)))
518 (setq mode-name
(if image-type
(format "Image[%s]" image-type
) "Image"))
519 (use-local-map image-mode-map
)
521 ;; Use our own bookmarking function for images.
522 (setq-local bookmark-make-record-function
523 #'image-bookmark-make-record
)
525 ;; Keep track of [vh]scroll when switching buffers
526 (image-mode-setup-winprops)
528 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
529 (add-hook 'after-revert-hook
'image-after-revert-hook nil t
)
530 (run-mode-hooks 'image-mode-hook
)
531 (let ((image (image-get-display-property))
532 (msg1 (substitute-command-keys
533 "Type \\[image-toggle-display] to view the image as "))
537 (message "%s" (concat msg1
"an image.")))
538 ((setq animated
(image-multi-frame-p image
))
539 (setq image-multi-frame t
545 (1+ (image-current-frame ',image
))
549 mouse-3: Previous frame"
550 'mouse-face
'mode-line-highlight
555 (down-mouse-1 . image-next-frame
)
556 (down-mouse-3 . image-previous-frame
)))))))
558 (concat msg1
"text. This image has multiple frames.")))
559 ;;; (substitute-command-keys
560 ;;; "\\[image-toggle-animation] to animate."))))
562 (message "%s" (concat msg1
"text."))))))
567 (if (called-interactively-p 'any
) 'error
'message
)
568 "Cannot display image: %s" (cdr err
)))))
571 (define-minor-mode image-minor-mode
572 "Toggle Image minor mode in this buffer.
573 With a prefix argument ARG, enable Image minor mode if ARG is
574 positive, and disable it otherwise. If called from Lisp, enable
575 the mode if ARG is omitted or nil.
577 Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
578 to switch back to `image-mode' and display an image file as the
580 nil
(:eval
(if image-type
(format " Image[%s]" image-type
) " Image"))
585 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)))
588 (defun image-mode-as-text ()
589 "Set a non-image mode as major mode in combination with image minor mode.
590 A non-image major mode found from `auto-mode-alist' or Fundamental mode
591 displays an image file as text. `image-minor-mode' provides the key
592 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
593 to display an image file as the actual image.
595 You can use `image-mode-as-text' in `auto-mode-alist' when you want
596 to display an image file as text initially.
598 See commands `image-mode' and `image-minor-mode' for more information
601 ;; image-mode-as-text = normal-mode + image-minor-mode
602 (let ((previous-image-type image-type
)) ; preserve `image-type'
603 (if image-mode-previous-major-mode
604 ;; Restore previous major mode that was already found by this
605 ;; function and cached in `image-mode-previous-major-mode'
606 (funcall image-mode-previous-major-mode
)
607 (let ((auto-mode-alist
610 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
611 '(image-mode image-mode-maybe image-mode-as-text
))
614 (magic-fallback-mode-alist
617 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
618 '(image-mode image-mode-maybe image-mode-as-text
))
620 magic-fallback-mode-alist
))))
622 (setq-local image-mode-previous-major-mode major-mode
)))
623 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
624 (setq image-type previous-image-type
)
625 ;; Enable image minor mode with `C-c C-c'.
627 ;; Show the image file as text.
628 (image-toggle-display-text)
629 (message "%s" (concat
630 (substitute-command-keys
631 "Type \\[image-toggle-display] to view the image as ")
632 (if (image-get-display-property)
633 "text" "an image") "."))))
635 (define-obsolete-function-alias 'image-mode-maybe
'image-mode
"23.2")
637 (defun image-toggle-display-text ()
638 "Show the image file as text.
639 Remove text properties that display the image."
640 (let ((inhibit-read-only t
)
642 (modified (buffer-modified-p)))
643 (remove-list-of-text-properties (point-min) (point-max)
644 '(display read-nonsticky
;; intangible
645 read-only front-sticky
))
646 (set-buffer-modified-p modified
)
647 (if (called-interactively-p 'any
)
648 (message "Repeat this command to go back to displaying the image"))))
650 (defvar archive-superior-buffer
)
651 (defvar tar-superior-buffer
)
652 (declare-function image-flush
"image.c" (spec &optional frame
))
654 (defun image-toggle-display-image ()
655 "Show the image of the image file.
656 Turn the image data into a real image, but only if the whole file
658 (unless (derived-mode-p 'image-mode
)
659 (error "The buffer is not in Image mode"))
660 (let* ((filename (buffer-file-name))
661 (data-p (not (and filename
662 (file-readable-p filename
)
663 (not (file-remote-p filename
))
664 (not (buffer-modified-p))
665 (not (and (boundp 'archive-superior-buffer
)
666 archive-superior-buffer
))
667 (not (and (boundp 'tar-superior-buffer
)
668 tar-superior-buffer
))
669 ;; This means the buffer holds the
670 ;; decrypted content (bug#21870).
671 (not (and (boundp 'epa-file-encrypt-to
)
673 'epa-file-encrypt-to
))))))
674 (file-or-data (if data-p
676 (buffer-substring-no-properties (point-min) (point-max)))
678 ;; If we have a `fit-width' or a `fit-height', don't limit
679 ;; the size of the image to the window size.
680 (edges (and (null image-transform-resize
)
681 (window-inside-pixel-edges
682 (get-buffer-window (current-buffer)))))
683 (type (if (fboundp 'imagemagick-types
)
685 (image-type file-or-data nil data-p
)))
686 (image (if (not edges
)
687 (create-image file-or-data type data-p
)
688 (create-image file-or-data type data-p
689 :max-width
(- (nth 2 edges
) (nth 0 edges
))
690 :max-height
(- (nth 3 edges
) (nth 1 edges
)))))
691 (inhibit-read-only t
)
693 (modified (buffer-modified-p))
696 ;; Discard any stale image data before looking it up again.
698 (setq image
(append image
(image-transform-properties image
)))
702 rear-nonsticky
(display) ;; intangible
703 read-only t front-sticky
(read-only)))
705 (let ((buffer-file-truename nil
)) ; avoid changing dir mtime by lock_file
706 (add-text-properties (point-min) (point-max) props
)
707 (restore-buffer-modified-p modified
))
708 ;; Inhibit the cursor when the buffer contains only an image,
709 ;; because cursors look very strange on top of images.
710 (setq cursor-type nil
)
711 ;; This just makes the arrow displayed in the right fringe
712 ;; area look correct when the image is wider than the window.
713 (setq truncate-lines t
)
714 ;; Disable adding a newline at the end of the image file when it
715 ;; is written with, e.g., C-x C-w.
716 (if (coding-system-equal (coding-system-base buffer-file-coding-system
)
718 (setq-local find-file-literally t
))
719 ;; Allow navigation of large images.
720 (setq-local auto-hscroll-mode nil
)
721 (setq image-type type
)
722 (if (eq major-mode
'image-mode
)
723 (setq mode-name
(format "Image[%s]" type
)))
724 (image-transform-check-size)
725 (if (called-interactively-p 'any
)
726 (message "Repeat this command to go back to displaying the file as text"))))
728 (defun image-toggle-display ()
729 "Toggle between image and text display.
730 If the current buffer is displaying an image file as an image,
731 call `image-mode-as-text' to switch to text. Otherwise, display
732 the image by calling `image-mode'."
734 (if (image-get-display-property)
738 (defun image-kill-buffer ()
739 "Kill the current buffer."
741 (kill-buffer (current-buffer)))
743 (defun image-after-revert-hook ()
744 (when (image-get-display-property)
745 (image-toggle-display-text)
746 ;; Update image display.
747 (mapc (lambda (window) (redraw-frame (window-frame window
)))
748 (get-buffer-window-list (current-buffer) 'nomini
'visible
))
749 (image-toggle-display-image)))
754 (defcustom image-animate-loop nil
755 "Non-nil means animated images loop forever, rather than playing once."
760 (defun image-toggle-animation ()
761 "Start or stop animating the current image.
762 If `image-animate-loop' is non-nil, animation loops forever.
763 Otherwise it plays once, then stops."
765 (let ((image (image-get-display-property))
769 (error "No image is present"))
770 ((null (setq animation
(image-multi-frame-p image
)))
771 (message "No image animation."))
773 (let ((timer (image-animate-timer image
)))
776 (let ((index (plist-get (cdr image
) :index
)))
777 ;; If we're at the end, restart.
779 (>= index
(1- (car animation
)))
781 (image-animate image index
782 (if image-animate-loop t
)))))))))
784 (defun image--set-speed (speed &optional multiply
)
785 "Set speed of an animated image to SPEED.
786 If MULTIPLY is non-nil, treat SPEED as a multiplication factor.
787 If SPEED is `reset', reset the magnitude of the speed to 1."
788 (let ((image (image-get-display-property)))
791 (error "No image is present"))
792 ((null image-multi-frame
)
793 (message "No image animation."))
795 (if (eq speed
'reset
)
796 (setq speed
(if (< (image-animate-get-speed image
) 0)
799 (image-animate-set-speed image speed multiply
)
800 ;; FIXME Hack to refresh an active image.
801 (when (image-animate-timer image
)
802 (image-toggle-animation)
803 (image-toggle-animation))
804 (message "Image speed is now %s" (image-animate-get-speed image
))))))
806 (defun image-increase-speed ()
807 "Increase the speed of current animated image by a factor of 2."
809 (image--set-speed 2 t
))
811 (defun image-decrease-speed ()
812 "Decrease the speed of current animated image by a factor of 2."
814 (image--set-speed 0.5 t
))
816 (defun image-reverse-speed ()
817 "Reverse the animation of the current image."
819 (image--set-speed -
1 t
))
821 (defun image-reset-speed ()
822 "Reset the animation speed of the current image."
824 (image--set-speed 'reset
))
826 (defun image-goto-frame (n &optional relative
)
827 "Show frame N of a multi-frame image.
828 Optional argument OFFSET non-nil means interpret N as relative to the
829 current frame. Frames are indexed from 1."
831 (list (or current-prefix-arg
832 (read-number "Show frame number: "))))
833 (let ((image (image-get-display-property)))
836 (error "No image is present"))
837 ((null image-multi-frame
)
838 (message "No image animation."))
840 (image-show-frame image
842 (+ n
(image-current-frame image
))
845 (defun image-next-frame (&optional n
)
846 "Switch to the next frame of a multi-frame image.
847 With optional argument N, switch to the Nth frame after the current one.
848 If N is negative, switch to the Nth frame before the current one."
850 (image-goto-frame n t
))
852 (defun image-previous-frame (&optional n
)
853 "Switch to the previous frame of a multi-frame image.
854 With optional argument N, switch to the Nth frame before the current one.
855 If N is negative, switch to the Nth frame after the current one."
857 (image-next-frame (- n
)))
860 ;;; Switching to the next/previous image
862 (defun image-next-file (&optional n
)
863 "Visit the next image in the same directory as the current image file.
864 With optional argument N, visit the Nth image file after the
865 current one, in cyclic alphabetical order.
867 This command visits the specified file via `find-alternate-file',
868 replacing the current Image mode buffer."
870 (unless (derived-mode-p 'image-mode
)
871 (error "The buffer is not in Image mode"))
872 (unless buffer-file-name
873 (error "The current image is not associated with a file"))
874 (let* ((file (file-name-nondirectory buffer-file-name
))
875 (images (image-mode--images-in-directory file
))
877 (catch 'image-visit-next-file
880 (throw 'image-visit-next-file
(1+ idx
)))
881 (setq idx
(1+ idx
))))
882 (setq idx
(mod (+ idx
(or n
1)) (length images
)))
883 (find-alternate-file (nth idx images
))))
885 (defun image-previous-file (&optional n
)
886 "Visit the preceding image in the same directory as the current file.
887 With optional argument N, visit the Nth image file preceding the
888 current one, in cyclic alphabetical order.
890 This command visits the specified file via `find-alternate-file',
891 replacing the current Image mode buffer."
893 (image-next-file (- n
)))
895 (defun image-mode--images-in-directory (file)
896 (let* ((dir (file-name-directory buffer-file-name
))
897 (files (directory-files dir nil
898 (image-file-name-regexp) t
)))
899 ;; Add the current file to the list of images if necessary, in
900 ;; case it does not match `image-file-name-regexp'.
901 (unless (member file files
)
903 (sort files
'string-lessp
)))
906 ;;; Support for bookmark.el
907 (declare-function bookmark-make-record-default
908 "bookmark" (&optional no-file no-context posn
))
909 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
910 (declare-function bookmark-default-handler
"bookmark" (bmk))
912 (defun image-bookmark-make-record ()
913 `(,@(bookmark-make-record-default nil
'no-context
0)
914 (image-type .
,image-type
)
915 (handler . image-bookmark-jump
)))
918 (defun image-bookmark-jump (bmk)
919 ;; This implements the `handler' function interface for record type
920 ;; returned by `bookmark-make-record-function', which see.
921 (prog1 (bookmark-default-handler bmk
)
922 (when (not (string= image-type
(bookmark-prop-get bmk
'image-type
)))
923 (image-toggle-display))))
926 ;; Not yet implemented.
927 ;; (defvar image-transform-minor-mode-map
928 ;; (let ((map (make-sparse-keymap)))
929 ;; ;; (define-key map [(control ?+)] 'image-scale-in)
930 ;; ;; (define-key map [(control ?-)] 'image-scale-out)
931 ;; ;; (define-key map [(control ?=)] 'image-scale-none)
932 ;; ;; (define-key map "c f h" 'image-scale-fit-height)
933 ;; ;; (define-key map "c ]" 'image-rotate-right)
935 ;; "Minor mode keymap `image-transform-mode'.")
937 ;; (define-minor-mode image-transform-mode
938 ;; "Minor mode for scaling and rotating images.
939 ;; With a prefix argument ARG, enable the mode if ARG is positive,
940 ;; and disable it otherwise. If called from Lisp, enable the mode
941 ;; if ARG is omitted or nil. This minor mode requires Emacs to have
942 ;; been compiled with ImageMagick support."
943 ;; nil "image-transform" image-transform-minor-mode-map)
946 (defsubst image-transform-width
(width height
)
947 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
948 The rotation angle is the value of `image-transform-rotation' in degrees."
949 (let ((angle (degrees-to-radians image-transform-rotation
)))
950 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
951 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
952 ;; rotation by the angle A. The projections onto the first axis
953 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
954 ;; (h/2) sin A, and the difference between the largest and the
955 ;; smallest of the four values is the expression below.
956 (+ (* width
(abs (cos angle
))) (* height
(abs (sin angle
))))))
958 ;; The following comment and code snippet are from
959 ;; ImageMagick-6.7.4-4/magick/distort.c
961 ;; /* Set the output image geometry to calculated 'best fit'.
962 ;; Yes this tends to 'over do' the file image size, ON PURPOSE!
963 ;; Do not do this for DePolar which needs to be exact for virtual tiling.
965 ;; if ( fix_bounds ) {
966 ;; geometry.x = (ssize_t) floor(min.x-0.5);
967 ;; geometry.y = (ssize_t) floor(min.y-0.5);
968 ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
969 ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
972 ;; Other parts of the same file show that here the origin is in the
973 ;; left lower corner of the image rectangle, the center of the
974 ;; rotation is the center of the rectangle and min.x and max.x
975 ;; (resp. min.y and max.y) are the smallest and the largest of the
976 ;; projections of the vertices onto the first (resp. second) axis.
978 (defun image-transform-fit-width (width height length
)
979 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
980 The rotation angle is the value of `image-transform-rotation'.
981 Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
982 an \"approximately uniformly\" scaled W x H rectangle, which
983 currently means that w is one of floor(s W) + {0, 1, -1} and h is
984 floor(s H), where s can be recovered as the value of `image-transform-scale'.
985 The value of `image-transform-rotation' may be replaced by
986 a slightly different angle. Currently this is done for values
987 close to a multiple of 90, see `image-transform-right-angle-fudge'."
988 (cond ((< (abs (- (mod (+ image-transform-rotation
90) 180) 90))
989 image-transform-right-angle-fudge
)
990 (cl-assert (not (zerop width
)) t
)
991 (setq image-transform-rotation
992 (float (round image-transform-rotation
))
993 image-transform-scale
(/ (float length
) width
))
995 ((< (abs (- (mod (+ image-transform-rotation
45) 90) 45))
996 image-transform-right-angle-fudge
)
997 (cl-assert (not (zerop height
)) t
)
998 (setq image-transform-rotation
999 (float (round image-transform-rotation
))
1000 image-transform-scale
(/ (float length
) height
))
1003 (cl-assert (not (and (zerop width
) (zerop height
))) t
)
1004 (setq image-transform-scale
1005 (/ (float (1- length
)) (image-transform-width width height
)))
1006 ;; Assume we have a w x h image and an angle A, and let l =
1007 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
1008 ;; of the bounding box of the rotated image, as calculated by
1009 ;; `image-transform-width'. The code snippet quoted above
1010 ;; means that ImageMagick puts the rotated image in
1011 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
1012 ;; Elementary considerations show that this is equivalent to
1013 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
1014 ;; the given `length' parameter and our job is to determine
1015 ;; reasonable values for w and h which satisfy these
1017 (let ((w (floor (* image-transform-scale width
)))
1018 (h (floor (* image-transform-scale height
))))
1019 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
1020 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
1021 ;; hence l(w, h) > (L-1) - 2 = L-3.
1023 (cond ((= (mod w
2) (mod length
2))
1025 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
1027 ((<= (image-transform-width (1+ w
) h
) (1- length
))
1029 ;; No, it doesn't, but this implies that l(w-1, h) =
1030 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
1031 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
1036 (defun image-transform-check-size ()
1037 "Check that the image exactly fits the width/height of the window.
1039 Do this for an image of type `imagemagick' to make sure that the
1040 elisp code matches the way ImageMagick computes the bounding box
1041 of a rotated image."
1042 (when (and (not (numberp image-transform-resize
))
1043 (boundp 'image-type
)
1044 (eq image-type
'imagemagick
))
1045 (let ((size (image-display-size (image-get-display-property) t
)))
1046 (cond ((eq image-transform-resize
'fit-width
)
1047 (cl-assert (= (car size
)
1048 (- (nth 2 (window-inside-pixel-edges))
1049 (nth 0 (window-inside-pixel-edges))))
1051 ((eq image-transform-resize
'fit-height
)
1052 (cl-assert (= (cdr size
)
1053 (- (nth 3 (window-inside-pixel-edges))
1054 (nth 1 (window-inside-pixel-edges))))
1057 (defun image-transform-properties (spec)
1058 "Return rescaling/rotation properties for image SPEC.
1059 These properties are determined by the Image mode variables
1060 `image-transform-resize' and `image-transform-rotation'. The
1061 return value is suitable for appending to an image spec.
1063 Rescaling and rotation properties only take effect if Emacs is
1064 compiled with ImageMagick support."
1065 (setq image-transform-scale
1.0)
1066 (when (or image-transform-resize
1067 (/= image-transform-rotation
0.0))
1068 ;; Note: `image-size' looks up and thus caches the untransformed
1069 ;; image. There's no easy way to prevent that.
1070 (let* ((size (image-size spec t
))
1073 ((numberp image-transform-resize
)
1074 (unless (= image-transform-resize
1)
1075 (setq image-transform-scale image-transform-resize
)
1076 (cons nil
(floor (* image-transform-resize
(cdr size
))))))
1077 ((eq image-transform-resize
'fit-width
)
1078 (image-transform-fit-width
1079 (car size
) (cdr size
)
1080 (- (nth 2 (window-inside-pixel-edges))
1081 (nth 0 (window-inside-pixel-edges)))))
1082 ((eq image-transform-resize
'fit-height
)
1083 (let ((res (image-transform-fit-width
1084 (cdr size
) (car size
)
1085 (- (nth 3 (window-inside-pixel-edges))
1086 (nth 1 (window-inside-pixel-edges))))))
1087 (cons (cdr res
) (car res
)))))))
1088 `(,@(when (car resized
)
1089 (list :width
(car resized
)))
1090 ,@(when (cdr resized
)
1091 (list :height
(cdr resized
)))
1092 ,@(unless (= 0.0 image-transform-rotation
)
1093 (list :rotation image-transform-rotation
))))))
1095 (defun image-transform-set-scale (scale)
1096 "Prompt for a number, and resize the current image by that amount.
1097 This command has no effect unless Emacs is compiled with
1098 ImageMagick support."
1099 (interactive "nScale: ")
1100 (setq image-transform-resize scale
)
1101 (image-toggle-display-image))
1103 (defun image-transform-fit-to-height ()
1104 "Fit the current image to the height of the current window.
1105 This command has no effect unless Emacs is compiled with
1106 ImageMagick support."
1108 (setq image-transform-resize
'fit-height
)
1109 (image-toggle-display-image))
1111 (defun image-transform-fit-to-width ()
1112 "Fit the current image to the width of the current window.
1113 This command has no effect unless Emacs is compiled with
1114 ImageMagick support."
1116 (setq image-transform-resize
'fit-width
)
1117 (image-toggle-display-image))
1119 (defun image-transform-set-rotation (rotation)
1120 "Prompt for an angle ROTATION, and rotate the image by that amount.
1121 ROTATION should be in degrees. This command has no effect unless
1122 Emacs is compiled with ImageMagick support."
1123 (interactive "nRotation angle (in degrees): ")
1124 (setq image-transform-rotation
(float (mod rotation
360)))
1125 (image-toggle-display-image))
1127 (defun image-transform-reset ()
1128 "Display the current image with the default size and rotation.
1129 This command has no effect unless Emacs is compiled with
1130 ImageMagick support."
1132 (setq image-transform-resize nil
1133 image-transform-rotation
0.0
1134 image-transform-scale
1)
1135 (image-toggle-display-image))
1137 (provide 'image-mode
)
1139 ;;; image-mode.el ends here