1 ;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*-
3 ;; Copyright (C) 2005-2014 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 (defun image-mode-winprops (&optional window cleanup
)
53 "Return winprops of WINDOW.
54 A winprops object has the shape (WINDOW . ALIST).
55 WINDOW defaults to `selected-window' if it displays the current buffer, and
56 otherwise it defaults to t, used for times when the buffer is not displayed."
59 (if (eq (current-buffer) (window-buffer)) (selected-window) t
)))
61 ((not (windowp window
))
62 (error "Not a window: %s" window
)))
64 (setq image-mode-winprops-alist
65 (delq nil
(mapcar (lambda (winprop)
66 (let ((w (car-safe winprop
)))
67 (if (or (not (windowp w
)) (window-live-p w
))
69 image-mode-winprops-alist
))))
70 (let ((winprops (assq window image-mode-winprops-alist
)))
71 ;; For new windows, set defaults from the latest.
73 ;; Move window to front.
74 (setq image-mode-winprops-alist
75 (cons winprops
(delq winprops image-mode-winprops-alist
)))
76 (setq winprops
(cons window
77 (copy-alist (cdar image-mode-winprops-alist
))))
78 ;; Add winprops before running the hook, to avoid inf-loops if the hook
79 ;; triggers window-configuration-change-hook.
80 (setq image-mode-winprops-alist
81 (cons winprops image-mode-winprops-alist
))
82 (run-hook-with-args 'image-mode-new-window-functions winprops
))
85 (defun image-mode-window-get (prop &optional winprops
)
86 (declare (gv-setter (lambda (val)
87 `(image-mode-window-put ,prop
,val
,winprops
))))
88 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
89 (cdr (assq prop
(cdr winprops
))))
91 (defun image-mode-window-put (prop val
&optional winprops
)
92 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
93 (unless (eq t
(car winprops
))
94 (image-mode-window-put prop val t
))
95 (setcdr winprops
(cons (cons prop val
)
96 (delq (assq prop
(cdr winprops
)) (cdr winprops
)))))
98 (defun image-set-window-vscroll (vscroll)
99 (setf (image-mode-window-get 'vscroll
) vscroll
)
100 (set-window-vscroll (selected-window) vscroll
))
102 (defun image-set-window-hscroll (ncol)
103 (setf (image-mode-window-get 'hscroll
) ncol
)
104 (set-window-hscroll (selected-window) ncol
))
106 (defun image-mode-reapply-winprops ()
107 ;; When set-window-buffer, set hscroll and vscroll to what they were
108 ;; last time the image was displayed in this window.
109 (when (listp image-mode-winprops-alist
)
110 ;; Beware: this call to image-mode-winprops can't be optimized away,
111 ;; because it not only gets the winprops data but sets it up if needed
112 ;; (e.g. it's used by doc-view to display the image in a new window).
113 (let* ((winprops (image-mode-winprops nil t
))
114 (hscroll (image-mode-window-get 'hscroll winprops
))
115 (vscroll (image-mode-window-get 'vscroll winprops
)))
116 (when (image-get-display-property) ;Only do it if we display an image!
117 (if hscroll
(set-window-hscroll (selected-window) hscroll
))
118 (if vscroll
(set-window-vscroll (selected-window) vscroll
))))))
120 (defun image-mode-setup-winprops ()
121 ;; Record current scroll settings.
122 (unless (listp image-mode-winprops-alist
)
123 (setq image-mode-winprops-alist nil
))
124 (add-hook 'window-configuration-change-hook
125 'image-mode-reapply-winprops nil t
))
127 ;;; Image scrolling functions
129 (defun image-get-display-property ()
130 (get-char-property (point-min) 'display
131 ;; There might be different images for different displays.
132 (if (eq (window-buffer) (current-buffer))
135 (declare-function image-size
"image.c" (spec &optional pixels frame
))
137 (defun image-display-size (spec &optional pixels frame
)
138 "Wrapper around `image-size', handling slice display properties.
139 Like `image-size', the return value is (WIDTH . HEIGHT).
140 WIDTH and HEIGHT are in canonical character units if PIXELS is
141 nil, and in pixel units if PIXELS is non-nil.
143 If SPEC is an image display property, this function is equivalent
144 to `image-size'. If SPEC is a list of properties containing
145 `image' and `slice' properties, return the display size taking
146 the slice property into account. If the list contains `image'
147 but not `slice', return the `image-size' of the specified image."
148 (if (eq (car spec
) 'image
)
149 (image-size spec pixels frame
)
150 (let ((image (assoc 'image spec
))
151 (slice (assoc 'slice spec
)))
152 (cond ((and image slice
)
154 (cons (nth 3 slice
) (nth 4 slice
))
155 (cons (/ (float (nth 3 slice
)) (frame-char-width frame
))
156 (/ (float (nth 4 slice
)) (frame-char-height frame
)))))
158 (image-size image pixels frame
))
160 (error "Invalid image specification: %s" spec
))))))
162 (defun image-forward-hscroll (&optional n
)
163 "Scroll image in current window to the left by N character widths.
164 Stop if the right edge of the image is reached."
168 (image-set-window-hscroll (max 0 (+ (window-hscroll) n
))))
170 (let* ((image (image-get-display-property))
171 (edges (window-inside-edges))
172 (win-width (- (nth 2 edges
) (nth 0 edges
)))
173 (img-width (ceiling (car (image-display-size image
)))))
174 (image-set-window-hscroll (min (max 0 (- img-width win-width
))
175 (+ n
(window-hscroll))))))))
177 (defun image-backward-hscroll (&optional n
)
178 "Scroll image in current window to the right by N character widths.
179 Stop if the left edge of the image is reached."
181 (image-forward-hscroll (- n
)))
183 (defun image-next-line (n)
184 "Scroll image in current window upward by N lines.
185 Stop if the bottom edge of the image is reached."
189 (image-set-window-vscroll (max 0 (+ (window-vscroll) n
))))
191 (let* ((image (image-get-display-property))
192 (edges (window-inside-edges))
193 (win-height (- (nth 3 edges
) (nth 1 edges
)))
194 (img-height (ceiling (cdr (image-display-size image
)))))
195 (image-set-window-vscroll (min (max 0 (- img-height win-height
))
196 (+ n
(window-vscroll))))))))
198 (defun image-previous-line (&optional n
)
199 "Scroll image in current window downward by N lines.
200 Stop if the top edge of the image is reached."
202 (image-next-line (- n
)))
204 (defun image-scroll-up (&optional n
)
205 "Scroll image in current window upward by N lines.
206 Stop if the bottom edge of the image is reached.
207 If ARG is omitted or nil, scroll upward by a near full screen.
208 A near full screen is `next-screen-context-lines' less than a full screen.
209 Negative ARG means scroll downward.
210 If ARG is the atom `-', scroll downward by nearly full screen.
211 When calling from a program, supply as argument a number, nil, or `-'."
214 (let* ((edges (window-inside-edges))
215 (win-height (- (nth 3 edges
) (nth 1 edges
))))
217 (max 0 (- win-height next-screen-context-lines
)))))
219 (let* ((edges (window-inside-edges))
220 (win-height (- (nth 3 edges
) (nth 1 edges
))))
222 (min 0 (- next-screen-context-lines win-height
)))))
223 (t (image-next-line (prefix-numeric-value n
)))))
225 (defun image-scroll-down (&optional n
)
226 "Scroll image in current window downward by N lines.
227 Stop if the top edge of the image is reached.
228 If ARG is omitted or nil, scroll downward by a near full screen.
229 A near full screen is `next-screen-context-lines' less than a full screen.
230 Negative ARG means scroll upward.
231 If ARG is the atom `-', scroll upward by nearly full screen.
232 When calling from a program, supply as argument a number, nil, or `-'."
235 (let* ((edges (window-inside-edges))
236 (win-height (- (nth 3 edges
) (nth 1 edges
))))
238 (min 0 (- next-screen-context-lines win-height
)))))
240 (let* ((edges (window-inside-edges))
241 (win-height (- (nth 3 edges
) (nth 1 edges
))))
243 (max 0 (- win-height next-screen-context-lines
)))))
244 (t (image-next-line (- (prefix-numeric-value n
))))))
246 (defun image-bol (arg)
247 "Scroll horizontally to the left edge of the image in the current window.
248 With argument ARG not nil or 1, move forward ARG - 1 lines first,
249 stopping if the top or bottom edge of the image is reached."
252 (/= (setq arg
(prefix-numeric-value arg
)) 1)
253 (image-next-line (- arg
1)))
254 (image-set-window-hscroll 0))
256 (defun image-eol (arg)
257 "Scroll horizontally to the right edge of the image in the current window.
258 With argument ARG not nil or 1, move forward ARG - 1 lines first,
259 stopping if the top or bottom edge of the image is reached."
262 (/= (setq arg
(prefix-numeric-value arg
)) 1)
263 (image-next-line (- arg
1)))
264 (let* ((image (image-get-display-property))
265 (edges (window-inside-edges))
266 (win-width (- (nth 2 edges
) (nth 0 edges
)))
267 (img-width (ceiling (car (image-display-size image
)))))
268 (image-set-window-hscroll (max 0 (- img-width win-width
)))))
271 "Scroll to the top-left corner of the image in the current window."
273 (image-set-window-hscroll 0)
274 (image-set-window-vscroll 0))
277 "Scroll to the bottom-right corner of the image in the current window."
279 (let* ((image (image-get-display-property))
280 (edges (window-inside-edges))
281 (win-width (- (nth 2 edges
) (nth 0 edges
)))
282 (img-width (ceiling (car (image-display-size image
))))
283 (win-height (- (nth 3 edges
) (nth 1 edges
)))
284 (img-height (ceiling (cdr (image-display-size image
)))))
285 (image-set-window-hscroll (max 0 (- img-width win-width
)))
286 (image-set-window-vscroll (max 0 (- img-height win-height
)))))
288 ;; Adjust frame and image size.
290 (defun image-mode-fit-frame (&optional frame toggle
)
291 "Fit FRAME to the current image.
292 If FRAME is omitted or nil, it defaults to the selected frame.
293 All other windows on the frame are deleted.
295 If called interactively, or if TOGGLE is non-nil, toggle between
296 fitting FRAME to the current image and restoring the size and
297 window configuration prior to the last `image-mode-fit-frame'
299 (interactive (list nil t
))
300 (let* ((buffer (current-buffer))
301 (display (image-get-display-property))
302 (size (image-display-size display
))
303 (saved (frame-parameter frame
'image-mode-saved-params
))
304 (window-configuration (current-window-configuration frame
))
305 (width (frame-width frame
))
306 (height (frame-height frame
)))
307 (with-selected-frame (or frame
(selected-frame))
308 (if (and toggle saved
309 (= (caar saved
) width
)
310 (= (cdar saved
) height
))
312 (set-frame-width frame
(car (nth 1 saved
)))
313 (set-frame-height frame
(cdr (nth 1 saved
)))
314 (set-window-configuration (nth 2 saved
))
315 (set-frame-parameter frame
'image-mode-saved-params nil
))
316 (delete-other-windows)
317 (switch-to-buffer buffer t t
)
318 (let* ((edges (window-inside-edges))
319 (inner-width (- (nth 2 edges
) (nth 0 edges
)))
320 (inner-height (- (nth 3 edges
) (nth 1 edges
))))
321 (set-frame-width frame
(+ (ceiling (car size
))
322 width
(- inner-width
)))
323 (set-frame-height frame
(+ (ceiling (cdr size
))
324 height
(- inner-height
)))
325 ;; The frame size after the above `set-frame-*' calls may
326 ;; differ from what we specified, due to window manager
327 ;; interference. We have to call `frame-width' and
328 ;; `frame-height' to get the actual results.
329 (set-frame-parameter frame
'image-mode-saved-params
330 (list (cons (frame-width)
333 window-configuration
)))))))
337 (defvar-local image-type nil
338 "The image type for the current Image mode buffer.")
340 (defvar-local image-multi-frame nil
341 "Non-nil if image for the current Image mode buffer has multiple frames.")
343 (defvar image-mode-previous-major-mode nil
344 "Internal variable to keep the previous non-image major mode.")
346 (defvar image-mode-map
347 (let ((map (make-sparse-keymap)))
348 (set-keymap-parent map special-mode-map
)
349 (define-key map
"\C-c\C-c" 'image-toggle-display
)
350 (define-key map
(kbd "SPC") 'image-scroll-up
)
351 (define-key map
(kbd "S-SPC") 'image-scroll-down
)
352 (define-key map
(kbd "DEL") 'image-scroll-down
)
353 (define-key map
(kbd "RET") 'image-toggle-animation
)
354 (define-key map
"F" 'image-goto-frame
)
355 (define-key map
"f" 'image-next-frame
)
356 (define-key map
"b" 'image-previous-frame
)
357 (define-key map
"n" 'image-next-file
)
358 (define-key map
"p" 'image-previous-file
)
359 (define-key map
"a+" 'image-increase-speed
)
360 (define-key map
"a-" 'image-decrease-speed
)
361 (define-key map
"a0" 'image-reset-speed
)
362 (define-key map
"ar" 'image-reverse-speed
)
363 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
364 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
365 (define-key map
[remap right-char
] 'image-forward-hscroll
)
366 (define-key map
[remap left-char
] 'image-backward-hscroll
)
367 (define-key map
[remap previous-line
] 'image-previous-line
)
368 (define-key map
[remap next-line
] 'image-next-line
)
369 (define-key map
[remap scroll-up
] 'image-scroll-up
)
370 (define-key map
[remap scroll-down
] 'image-scroll-down
)
371 (define-key map
[remap scroll-up-command
] 'image-scroll-up
)
372 (define-key map
[remap scroll-down-command
] 'image-scroll-down
)
373 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
374 (define-key map
[remap move-end-of-line
] 'image-eol
)
375 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
376 (define-key map
[remap end-of-buffer
] 'image-eob
)
377 (easy-menu-define image-mode-menu map
"Menu for Image mode."
379 ["Show as Text" image-toggle-display
:active t
380 :help
"Show image as text"]
382 ["Fit Frame to Image" image-mode-fit-frame
:active t
383 :help
"Resize frame to match image"]
384 ["Fit to Window Height" image-transform-fit-to-height
385 :visible
(eq image-type
'imagemagick
)
386 :help
"Resize image to match the window height"]
387 ["Fit to Window Width" image-transform-fit-to-width
388 :visible
(eq image-type
'imagemagick
)
389 :help
"Resize image to match the window width"]
390 ["Rotate Image..." image-transform-set-rotation
391 :visible
(eq image-type
'imagemagick
)
392 :help
"Rotate the image"]
397 (image-dired default-directory
))
398 :active default-directory
399 :help
"Show thumbnails for all images in this directory"]
400 ["Next Image" image-next-file
:active buffer-file-name
401 :help
"Move to next image in this directory"]
402 ["Previous Image" image-previous-file
:active buffer-file-name
403 :help
"Move to previous image in this directory"]
405 ["Animate Image" image-toggle-animation
:style toggle
406 :selected
(let ((image (image-get-display-property)))
407 (and image
(image-animate-timer image
)))
408 :active image-multi-frame
409 :help
"Toggle image animation"]
411 (lambda () (interactive)
412 (setq image-animate-loop
(not image-animate-loop
))
413 ;; FIXME this is a hacky way to make it affect a currently
415 (when (let ((image (image-get-display-property)))
416 (and image
(image-animate-timer image
)))
417 (image-toggle-animation)
418 (image-toggle-animation)))
419 :style toggle
:selected image-animate-loop
420 :active image-multi-frame
421 :help
"Animate images once, or forever?"]
422 ["Reverse Animation" image-reverse-speed
423 :style toggle
:selected
(let ((image (image-get-display-property)))
425 (image-animate-get-speed image
)
427 :active image-multi-frame
428 :help
"Reverse direction of this image's animation?"]
429 ["Speed Up Animation" image-increase-speed
430 :active image-multi-frame
431 :help
"Speed up this image's animation"]
432 ["Slow Down Animation" image-decrease-speed
433 :active image-multi-frame
434 :help
"Slow down this image's animation"]
435 ["Reset Animation Speed" image-reset-speed
436 :active image-multi-frame
437 :help
"Reset the speed of this image's animation"]
438 ["Next Frame" image-next-frame
:active image-multi-frame
439 :help
"Show the next frame of this image"]
440 ["Previous Frame" image-previous-frame
:active image-multi-frame
441 :help
"Show the previous frame of this image"]
442 ["Goto Frame..." image-goto-frame
:active image-multi-frame
443 :help
"Show a specific frame of this image"]
446 "Mode keymap for `image-mode'.")
448 (defvar image-minor-mode-map
449 (let ((map (make-sparse-keymap)))
450 (define-key map
"\C-c\C-c" 'image-toggle-display
)
452 "Mode keymap for `image-minor-mode'.")
454 (defvar bookmark-make-record-function
)
456 (put 'image-mode
'mode-class
'special
)
460 "Major mode for image files.
461 You can use \\<image-mode-map>\\[image-toggle-display]
462 to toggle between display as an image and display as text.
469 (unless (display-images-p)
470 (error "Display does not support images"))
472 (kill-all-local-variables)
473 (setq major-mode
'image-mode
)
475 (if (not (image-get-display-property))
477 (image-toggle-display-image)
478 ;; If attempt to display the image fails.
479 (if (not (image-get-display-property))
480 (error "Invalid image")))
481 ;; Set next vars when image is already displayed but local
482 ;; variables were cleared by kill-all-local-variables
483 (setq cursor-type nil truncate-lines t
484 image-type
(plist-get (cdr (image-get-display-property)) :type
)))
486 (setq mode-name
(if image-type
(format "Image[%s]" image-type
) "Image"))
487 (use-local-map image-mode-map
)
489 ;; Use our own bookmarking function for images.
490 (setq-local bookmark-make-record-function
491 #'image-bookmark-make-record
)
493 ;; Keep track of [vh]scroll when switching buffers
494 (image-mode-setup-winprops)
496 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
497 (add-hook 'after-revert-hook
'image-after-revert-hook nil t
)
498 (run-mode-hooks 'image-mode-hook
)
499 (let ((image (image-get-display-property))
500 (msg1 (substitute-command-keys
501 "Type \\[image-toggle-display] to view the image as "))
505 (message "%s" (concat msg1
"an image.")))
506 ((setq animated
(image-multi-frame-p image
))
507 (setq image-multi-frame t
513 (1+ (image-current-frame ',image
))
517 mouse-3: Previous frame"
518 'mouse-face
'mode-line-highlight
523 (down-mouse-1 . image-next-frame
)
524 (down-mouse-3 . image-previous-frame
)))))))
526 (concat msg1
"text. This image has multiple frames.")))
527 ;;; (substitute-command-keys
528 ;;; "\\[image-toggle-animation] to animate."))))
530 (message "%s" (concat msg1
"text."))))))
535 (if (called-interactively-p 'any
) 'error
'message
)
536 "Cannot display image: %s" (cdr err
)))))
539 (define-minor-mode image-minor-mode
540 "Toggle Image minor mode in this buffer.
541 With a prefix argument ARG, enable Image minor mode if ARG is
542 positive, and disable it otherwise. If called from Lisp, enable
543 the mode if ARG is omitted or nil.
545 Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
546 to switch back to `image-mode' and display an image file as the
548 nil
(:eval
(if image-type
(format " Image[%s]" image-type
) " Image"))
553 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)))
556 (defun image-mode-as-text ()
557 "Set a non-image mode as major mode in combination with image minor mode.
558 A non-image major mode found from `auto-mode-alist' or Fundamental mode
559 displays an image file as text. `image-minor-mode' provides the key
560 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
561 to display an image file as the actual image.
563 You can use `image-mode-as-text' in `auto-mode-alist' when you want
564 to display an image file as text initially.
566 See commands `image-mode' and `image-minor-mode' for more information
569 ;; image-mode-as-text = normal-mode + image-minor-mode
570 (let ((previous-image-type image-type
)) ; preserve `image-type'
571 (if image-mode-previous-major-mode
572 ;; Restore previous major mode that was already found by this
573 ;; function and cached in `image-mode-previous-major-mode'
574 (funcall image-mode-previous-major-mode
)
575 (let ((auto-mode-alist
578 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
579 '(image-mode image-mode-maybe image-mode-as-text
))
582 (magic-fallback-mode-alist
585 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
586 '(image-mode image-mode-maybe image-mode-as-text
))
588 magic-fallback-mode-alist
))))
590 (setq-local image-mode-previous-major-mode major-mode
)))
591 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
592 (setq image-type previous-image-type
)
593 ;; Enable image minor mode with `C-c C-c'.
595 ;; Show the image file as text.
596 (image-toggle-display-text)
597 (message "%s" (concat
598 (substitute-command-keys
599 "Type \\[image-toggle-display] to view the image as ")
600 (if (image-get-display-property)
601 "text" "an image") "."))))
603 (define-obsolete-function-alias 'image-mode-maybe
'image-mode
"23.2")
605 (defun image-toggle-display-text ()
606 "Show the image file as text.
607 Remove text properties that display the image."
608 (let ((inhibit-read-only t
)
610 (modified (buffer-modified-p)))
611 (remove-list-of-text-properties (point-min) (point-max)
612 '(display read-nonsticky
;; intangible
613 read-only front-sticky
))
614 (set-buffer-modified-p modified
)
615 (if (called-interactively-p 'any
)
616 (message "Repeat this command to go back to displaying the image"))))
618 (defvar archive-superior-buffer
)
619 (defvar tar-superior-buffer
)
620 (declare-function image-flush
"image.c" (spec &optional frame
))
622 (defun image-toggle-display-image ()
623 "Show the image of the image file.
624 Turn the image data into a real image, but only if the whole file
626 (unless (derived-mode-p 'image-mode
)
627 (error "The buffer is not in Image mode"))
628 (let* ((filename (buffer-file-name))
629 (data-p (not (and filename
630 (file-readable-p filename
)
631 (not (file-remote-p filename
))
632 (not (buffer-modified-p))
633 (not (and (boundp 'archive-superior-buffer
)
634 archive-superior-buffer
))
635 (not (and (boundp 'tar-superior-buffer
)
636 tar-superior-buffer
)))))
637 (file-or-data (if data-p
639 (buffer-substring-no-properties (point-min) (point-max)))
641 (type (image-type file-or-data nil data-p
))
642 (image (create-image file-or-data type data-p
))
643 (inhibit-read-only t
)
645 (modified (buffer-modified-p))
648 ;; Discard any stale image data before looking it up again.
650 (setq image
(append image
(image-transform-properties image
)))
654 rear-nonsticky
(display) ;; intangible
655 read-only t front-sticky
(read-only)))
657 (let ((buffer-file-truename nil
)) ; avoid changing dir mtime by lock_file
658 (add-text-properties (point-min) (point-max) props
)
659 (restore-buffer-modified-p modified
))
660 ;; Inhibit the cursor when the buffer contains only an image,
661 ;; because cursors look very strange on top of images.
662 (setq cursor-type nil
)
663 ;; This just makes the arrow displayed in the right fringe
664 ;; area look correct when the image is wider than the window.
665 (setq truncate-lines t
)
666 ;; Disable adding a newline at the end of the image file when it
667 ;; is written with, e.g., C-x C-w.
668 (if (coding-system-equal (coding-system-base buffer-file-coding-system
)
670 (setq-local find-file-literally t
))
671 ;; Allow navigation of large images.
672 (setq-local auto-hscroll-mode nil
)
673 (setq image-type type
)
674 (if (eq major-mode
'image-mode
)
675 (setq mode-name
(format "Image[%s]" type
)))
676 (image-transform-check-size)
677 (if (called-interactively-p 'any
)
678 (message "Repeat this command to go back to displaying the file as text"))))
680 (defun image-toggle-display ()
681 "Toggle between image and text display.
682 If the current buffer is displaying an image file as an image,
683 call `image-mode-as-text' to switch to text. Otherwise, display
684 the image by calling `image-mode'."
686 (if (image-get-display-property)
690 (defun image-after-revert-hook ()
691 (when (image-get-display-property)
692 (image-toggle-display-text)
693 ;; Update image display.
694 (mapc (lambda (window) (redraw-frame (window-frame window
)))
695 (get-buffer-window-list (current-buffer) 'nomini
'visible
))
696 (image-toggle-display-image)))
701 (defcustom image-animate-loop nil
702 "Non-nil means animated images loop forever, rather than playing once."
707 (defun image-toggle-animation ()
708 "Start or stop animating the current image.
709 If `image-animate-loop' is non-nil, animation loops forever.
710 Otherwise it plays once, then stops."
712 (let ((image (image-get-display-property))
716 (error "No image is present"))
717 ((null (setq animation
(image-multi-frame-p image
)))
718 (message "No image animation."))
720 (let ((timer (image-animate-timer image
)))
723 (let ((index (plist-get (cdr image
) :index
)))
724 ;; If we're at the end, restart.
726 (>= index
(1- (car animation
)))
728 (image-animate image index
729 (if image-animate-loop t
)))))))))
731 (defun image--set-speed (speed &optional multiply
)
732 "Set speed of an animated image to SPEED.
733 If MULTIPLY is non-nil, treat SPEED as a multiplication factor.
734 If SPEED is `reset', reset the magnitude of the speed to 1."
735 (let ((image (image-get-display-property)))
738 (error "No image is present"))
739 ((null image-multi-frame
)
740 (message "No image animation."))
742 (if (eq speed
'reset
)
743 (setq speed
(if (< (image-animate-get-speed image
) 0)
746 (image-animate-set-speed image speed multiply
)
747 ;; FIXME Hack to refresh an active image.
748 (when (image-animate-timer image
)
749 (image-toggle-animation)
750 (image-toggle-animation))
751 (message "Image speed is now %s" (image-animate-get-speed image
))))))
753 (defun image-increase-speed ()
754 "Increase the speed of current animated image by a factor of 2."
756 (image--set-speed 2 t
))
758 (defun image-decrease-speed ()
759 "Decrease the speed of current animated image by a factor of 2."
761 (image--set-speed 0.5 t
))
763 (defun image-reverse-speed ()
764 "Reverse the animation of the current image."
766 (image--set-speed -
1 t
))
768 (defun image-reset-speed ()
769 "Reset the animation speed of the current image."
771 (image--set-speed 'reset
))
773 (defun image-goto-frame (n &optional relative
)
774 "Show frame N of a multi-frame image.
775 Optional argument OFFSET non-nil means interpret N as relative to the
776 current frame. Frames are indexed from 1."
778 (list (or current-prefix-arg
779 (read-number "Show frame number: "))))
780 (let ((image (image-get-display-property)))
783 (error "No image is present"))
784 ((null image-multi-frame
)
785 (message "No image animation."))
787 (image-show-frame image
789 (+ n
(image-current-frame image
))
792 (defun image-next-frame (&optional n
)
793 "Switch to the next frame of a multi-frame image.
794 With optional argument N, switch to the Nth frame after the current one.
795 If N is negative, switch to the Nth frame before the current one."
797 (image-goto-frame n t
))
799 (defun image-previous-frame (&optional n
)
800 "Switch to the previous frame of a multi-frame image.
801 With optional argument N, switch to the Nth frame before the current one.
802 If N is negative, switch to the Nth frame after the current one."
804 (image-next-frame (- n
)))
807 ;;; Switching to the next/previous image
809 (defun image-next-file (&optional n
)
810 "Visit the next image in the same directory as the current image file.
811 With optional argument N, visit the Nth image file after the
812 current one, in cyclic alphabetical order.
814 This command visits the specified file via `find-alternate-file',
815 replacing the current Image mode buffer."
817 (unless (derived-mode-p 'image-mode
)
818 (error "The buffer is not in Image mode"))
819 (unless buffer-file-name
820 (error "The current image is not associated with a file"))
821 (let* ((file (file-name-nondirectory buffer-file-name
))
822 (images (image-mode--images-in-directory file
))
824 (catch 'image-visit-next-file
827 (throw 'image-visit-next-file
(1+ idx
)))
828 (setq idx
(1+ idx
))))
829 (setq idx
(mod (+ idx
(or n
1)) (length images
)))
830 (find-alternate-file (nth idx images
))))
832 (defun image-previous-file (&optional n
)
833 "Visit the preceding image in the same directory as the current file.
834 With optional argument N, visit the Nth image file preceding the
835 current one, in cyclic alphabetical order.
837 This command visits the specified file via `find-alternate-file',
838 replacing the current Image mode buffer."
840 (image-next-file (- n
)))
842 (defun image-mode--images-in-directory (file)
843 (let* ((dir (file-name-directory buffer-file-name
))
844 (files (directory-files dir nil
845 (image-file-name-regexp) t
)))
846 ;; Add the current file to the list of images if necessary, in
847 ;; case it does not match `image-file-name-regexp'.
848 (unless (member file files
)
850 (sort files
'string-lessp
)))
853 ;;; Support for bookmark.el
854 (declare-function bookmark-make-record-default
855 "bookmark" (&optional no-file no-context posn
))
856 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
857 (declare-function bookmark-default-handler
"bookmark" (bmk))
859 (defun image-bookmark-make-record ()
860 `(,@(bookmark-make-record-default nil
'no-context
0)
861 (image-type .
,image-type
)
862 (handler . image-bookmark-jump
)))
865 (defun image-bookmark-jump (bmk)
866 ;; This implements the `handler' function interface for record type
867 ;; returned by `bookmark-make-record-function', which see.
868 (prog1 (bookmark-default-handler bmk
)
869 (when (not (string= image-type
(bookmark-prop-get bmk
'image-type
)))
870 (image-toggle-display))))
873 ;; Not yet implemented.
874 ;; (defvar image-transform-minor-mode-map
875 ;; (let ((map (make-sparse-keymap)))
876 ;; ;; (define-key map [(control ?+)] 'image-scale-in)
877 ;; ;; (define-key map [(control ?-)] 'image-scale-out)
878 ;; ;; (define-key map [(control ?=)] 'image-scale-none)
879 ;; ;; (define-key map "c f h" 'image-scale-fit-height)
880 ;; ;; (define-key map "c ]" 'image-rotate-right)
882 ;; "Minor mode keymap `image-transform-mode'.")
884 ;; (define-minor-mode image-transform-mode
885 ;; "Minor mode for scaling and rotating images.
886 ;; With a prefix argument ARG, enable the mode if ARG is positive,
887 ;; and disable it otherwise. If called from Lisp, enable the mode
888 ;; if ARG is omitted or nil. This minor mode requires Emacs to have
889 ;; been compiled with ImageMagick support."
890 ;; nil "image-transform" image-transform-minor-mode-map)
893 ;; FIXME this doesn't seem mature yet. Document in manual when it is.
894 (defvar image-transform-resize nil
895 "The image resize operation.
896 Its value should be one of the following:
897 - nil, meaning no resizing.
898 - `fit-height', meaning to fit the image to the window height.
899 - `fit-width', meaning to fit the image to the window width.
900 - A number, which is a scale factor (the default size is 1).")
902 (defvar image-transform-scale
1.0
903 "The scale factor of the image being displayed.")
905 (defvar image-transform-rotation
0.0
906 "Rotation angle for the image in the current Image mode buffer.")
908 (defvar image-transform-right-angle-fudge
0.0001
909 "Snap distance to a multiple of a right angle.
910 There's no deep theory behind the default value, it should just
911 be somewhat larger than ImageMagick's MagickEpsilon.")
913 (defsubst image-transform-width
(width height
)
914 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
915 The rotation angle is the value of `image-transform-rotation' in degrees."
916 (let ((angle (degrees-to-radians image-transform-rotation
)))
917 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
918 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
919 ;; rotation by the angle A. The projections onto the first axis
920 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
921 ;; (h/2) sin A, and the difference between the largest and the
922 ;; smallest of the four values is the expression below.
923 (+ (* width
(abs (cos angle
))) (* height
(abs (sin angle
))))))
925 ;; The following comment and code snippet are from
926 ;; ImageMagick-6.7.4-4/magick/distort.c
928 ;; /* Set the output image geometry to calculated 'best fit'.
929 ;; Yes this tends to 'over do' the file image size, ON PURPOSE!
930 ;; Do not do this for DePolar which needs to be exact for virtual tiling.
932 ;; if ( fix_bounds ) {
933 ;; geometry.x = (ssize_t) floor(min.x-0.5);
934 ;; geometry.y = (ssize_t) floor(min.y-0.5);
935 ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
936 ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
939 ;; Other parts of the same file show that here the origin is in the
940 ;; left lower corner of the image rectangle, the center of the
941 ;; rotation is the center of the rectangle and min.x and max.x
942 ;; (resp. min.y and max.y) are the smallest and the largest of the
943 ;; projections of the vertices onto the first (resp. second) axis.
945 (defun image-transform-fit-width (width height length
)
946 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
947 The rotation angle is the value of `image-transform-rotation'.
948 Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
949 an \"approximately uniformly\" scaled W x H rectangle, which
950 currently means that w is one of floor(s W) + {0, 1, -1} and h is
951 floor(s H), where s can be recovered as the value of `image-transform-scale'.
952 The value of `image-transform-rotation' may be replaced by
953 a slightly different angle. Currently this is done for values
954 close to a multiple of 90, see `image-transform-right-angle-fudge'."
955 (cond ((< (abs (- (mod (+ image-transform-rotation
90) 180) 90))
956 image-transform-right-angle-fudge
)
957 (cl-assert (not (zerop width
)) t
)
958 (setq image-transform-rotation
959 (float (round image-transform-rotation
))
960 image-transform-scale
(/ (float length
) width
))
962 ((< (abs (- (mod (+ image-transform-rotation
45) 90) 45))
963 image-transform-right-angle-fudge
)
964 (cl-assert (not (zerop height
)) t
)
965 (setq image-transform-rotation
966 (float (round image-transform-rotation
))
967 image-transform-scale
(/ (float length
) height
))
970 (cl-assert (not (and (zerop width
) (zerop height
))) t
)
971 (setq image-transform-scale
972 (/ (float (1- length
)) (image-transform-width width height
)))
973 ;; Assume we have a w x h image and an angle A, and let l =
974 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
975 ;; of the bounding box of the rotated image, as calculated by
976 ;; `image-transform-width'. The code snippet quoted above
977 ;; means that ImageMagick puts the rotated image in
978 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
979 ;; Elementary considerations show that this is equivalent to
980 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
981 ;; the given `length' parameter and our job is to determine
982 ;; reasonable values for w and h which satisfy these
984 (let ((w (floor (* image-transform-scale width
)))
985 (h (floor (* image-transform-scale height
))))
986 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
987 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
988 ;; hence l(w, h) > (L-1) - 2 = L-3.
990 (cond ((= (mod w
2) (mod length
2))
992 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
994 ((<= (image-transform-width (1+ w
) h
) (1- length
))
996 ;; No, it doesn't, but this implies that l(w-1, h) =
997 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
998 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
1003 (defun image-transform-check-size ()
1004 "Check that the image exactly fits the width/height of the window.
1006 Do this for an image of type `imagemagick' to make sure that the
1007 elisp code matches the way ImageMagick computes the bounding box
1008 of a rotated image."
1009 (when (and (not (numberp image-transform-resize
))
1010 (boundp 'image-type
)
1011 (eq image-type
'imagemagick
))
1012 (let ((size (image-display-size (image-get-display-property) t
)))
1013 (cond ((eq image-transform-resize
'fit-width
)
1014 (cl-assert (= (car size
)
1015 (- (nth 2 (window-inside-pixel-edges))
1016 (nth 0 (window-inside-pixel-edges))))
1018 ((eq image-transform-resize
'fit-height
)
1019 (cl-assert (= (cdr size
)
1020 (- (nth 3 (window-inside-pixel-edges))
1021 (nth 1 (window-inside-pixel-edges))))
1024 (defun image-transform-properties (spec)
1025 "Return rescaling/rotation properties for image SPEC.
1026 These properties are determined by the Image mode variables
1027 `image-transform-resize' and `image-transform-rotation'. The
1028 return value is suitable for appending to an image spec.
1030 Rescaling and rotation properties only take effect if Emacs is
1031 compiled with ImageMagick support."
1032 (setq image-transform-scale
1.0)
1033 (when (or image-transform-resize
1034 (/= image-transform-rotation
0.0))
1035 ;; Note: `image-size' looks up and thus caches the untransformed
1036 ;; image. There's no easy way to prevent that.
1037 (let* ((size (image-size spec t
))
1040 ((numberp image-transform-resize
)
1041 (unless (= image-transform-resize
1)
1042 (setq image-transform-scale image-transform-resize
)
1043 (cons nil
(floor (* image-transform-resize
(cdr size
))))))
1044 ((eq image-transform-resize
'fit-width
)
1045 (image-transform-fit-width
1046 (car size
) (cdr size
)
1047 (- (nth 2 (window-inside-pixel-edges))
1048 (nth 0 (window-inside-pixel-edges)))))
1049 ((eq image-transform-resize
'fit-height
)
1050 (let ((res (image-transform-fit-width
1051 (cdr size
) (car size
)
1052 (- (nth 3 (window-inside-pixel-edges))
1053 (nth 1 (window-inside-pixel-edges))))))
1054 (cons (cdr res
) (car res
)))))))
1055 `(,@(when (car resized
)
1056 (list :width
(car resized
)))
1057 ,@(when (cdr resized
)
1058 (list :height
(cdr resized
)))
1059 ,@(unless (= 0.0 image-transform-rotation
)
1060 (list :rotation image-transform-rotation
))))))
1062 (defun image-transform-set-scale (scale)
1063 "Prompt for a number, and resize the current image by that amount.
1064 This command has no effect unless Emacs is compiled with
1065 ImageMagick support."
1066 (interactive "nScale: ")
1067 (setq image-transform-resize scale
)
1068 (image-toggle-display-image))
1070 (defun image-transform-fit-to-height ()
1071 "Fit the current image to the height of the current window.
1072 This command has no effect unless Emacs is compiled with
1073 ImageMagick support."
1075 (setq image-transform-resize
'fit-height
)
1076 (image-toggle-display-image))
1078 (defun image-transform-fit-to-width ()
1079 "Fit the current image to the width of the current window.
1080 This command has no effect unless Emacs is compiled with
1081 ImageMagick support."
1083 (setq image-transform-resize
'fit-width
)
1084 (image-toggle-display-image))
1086 (defun image-transform-set-rotation (rotation)
1087 "Prompt for an angle ROTATION, and rotate the image by that amount.
1088 ROTATION should be in degrees. This command has no effect unless
1089 Emacs is compiled with ImageMagick support."
1090 (interactive "nRotation angle (in degrees): ")
1091 (setq image-transform-rotation
(float (mod rotation
360)))
1092 (image-toggle-display-image))
1094 (provide 'image-mode
)
1096 ;;; image-mode.el ends here