1 ;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*-
3 ;; Copyright (C) 2005-2015 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
[remap forward-char
] 'image-forward-hscroll
)
384 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
385 (define-key map
[remap right-char
] 'image-forward-hscroll
)
386 (define-key map
[remap left-char
] 'image-backward-hscroll
)
387 (define-key map
[remap previous-line
] 'image-previous-line
)
388 (define-key map
[remap next-line
] 'image-next-line
)
389 (define-key map
[remap scroll-up
] 'image-scroll-up
)
390 (define-key map
[remap scroll-down
] 'image-scroll-down
)
391 (define-key map
[remap scroll-up-command
] 'image-scroll-up
)
392 (define-key map
[remap scroll-down-command
] 'image-scroll-down
)
393 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
394 (define-key map
[remap move-end-of-line
] 'image-eol
)
395 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
396 (define-key map
[remap end-of-buffer
] 'image-eob
)
397 (easy-menu-define image-mode-menu map
"Menu for Image mode."
399 ["Show as Text" image-toggle-display
:active t
400 :help
"Show image as text"]
402 ["Fit to Window Height" image-transform-fit-to-height
403 :visible
(eq image-type
'imagemagick
)
404 :help
"Resize image to match the window height"]
405 ["Fit to Window Width" image-transform-fit-to-width
406 :visible
(eq image-type
'imagemagick
)
407 :help
"Resize image to match the window width"]
408 ["Rotate Image..." image-transform-set-rotation
409 :visible
(eq image-type
'imagemagick
)
410 :help
"Rotate the image"]
411 ["Reset Transformations" image-transform-reset
412 :visible
(eq image-type
'imagemagick
)
413 :help
"Reset all image transformations"]
418 (image-dired default-directory
))
419 :active default-directory
420 :help
"Show thumbnails for all images in this directory"]
421 ["Next Image" image-next-file
:active buffer-file-name
422 :help
"Move to next image in this directory"]
423 ["Previous Image" image-previous-file
:active buffer-file-name
424 :help
"Move to previous image in this directory"]
426 ["Fit Frame to Image" image-mode-fit-frame
:active t
427 :help
"Resize frame to match image"]
429 ["Animate Image" image-toggle-animation
:style toggle
430 :selected
(let ((image (image-get-display-property)))
431 (and image
(image-animate-timer image
)))
432 :active image-multi-frame
433 :help
"Toggle image animation"]
435 (lambda () (interactive)
436 (setq image-animate-loop
(not image-animate-loop
))
437 ;; FIXME this is a hacky way to make it affect a currently
439 (when (let ((image (image-get-display-property)))
440 (and image
(image-animate-timer image
)))
441 (image-toggle-animation)
442 (image-toggle-animation)))
443 :style toggle
:selected image-animate-loop
444 :active image-multi-frame
445 :help
"Animate images once, or forever?"]
446 ["Reverse Animation" image-reverse-speed
447 :style toggle
:selected
(let ((image (image-get-display-property)))
449 (image-animate-get-speed image
)
451 :active image-multi-frame
452 :help
"Reverse direction of this image's animation?"]
453 ["Speed Up Animation" image-increase-speed
454 :active image-multi-frame
455 :help
"Speed up this image's animation"]
456 ["Slow Down Animation" image-decrease-speed
457 :active image-multi-frame
458 :help
"Slow down this image's animation"]
459 ["Reset Animation Speed" image-reset-speed
460 :active image-multi-frame
461 :help
"Reset the speed of this image's animation"]
462 ["Next Frame" image-next-frame
:active image-multi-frame
463 :help
"Show the next frame of this image"]
464 ["Previous Frame" image-previous-frame
:active image-multi-frame
465 :help
"Show the previous frame of this image"]
466 ["Goto Frame..." image-goto-frame
:active image-multi-frame
467 :help
"Show a specific frame of this image"]
470 "Mode keymap for `image-mode'.")
472 (defvar image-minor-mode-map
473 (let ((map (make-sparse-keymap)))
474 (define-key map
"\C-c\C-c" 'image-toggle-display
)
476 "Mode keymap for `image-minor-mode'.")
478 (defvar bookmark-make-record-function
)
480 (put 'image-mode
'mode-class
'special
)
484 "Major mode for image files.
485 You can use \\<image-mode-map>\\[image-toggle-display]
486 to toggle between display as an image and display as text.
493 (unless (display-images-p)
494 (error "Display does not support images"))
496 (kill-all-local-variables)
497 (setq major-mode
'image-mode
)
499 (if (not (image-get-display-property))
501 (image-toggle-display-image)
502 ;; If attempt to display the image fails.
503 (if (not (image-get-display-property))
504 (error "Invalid image")))
505 ;; Set next vars when image is already displayed but local
506 ;; variables were cleared by kill-all-local-variables
507 (setq cursor-type nil truncate-lines t
508 image-type
(plist-get (cdr (image-get-display-property)) :type
)))
510 (setq mode-name
(if image-type
(format "Image[%s]" image-type
) "Image"))
511 (use-local-map image-mode-map
)
513 ;; Use our own bookmarking function for images.
514 (setq-local bookmark-make-record-function
515 #'image-bookmark-make-record
)
517 ;; Keep track of [vh]scroll when switching buffers
518 (image-mode-setup-winprops)
520 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
521 (add-hook 'after-revert-hook
'image-after-revert-hook nil t
)
522 (run-mode-hooks 'image-mode-hook
)
523 (let ((image (image-get-display-property))
524 (msg1 (substitute-command-keys
525 "Type \\[image-toggle-display] to view the image as "))
529 (message "%s" (concat msg1
"an image.")))
530 ((setq animated
(image-multi-frame-p image
))
531 (setq image-multi-frame t
537 (1+ (image-current-frame ',image
))
541 mouse-3: Previous frame"
542 'mouse-face
'mode-line-highlight
547 (down-mouse-1 . image-next-frame
)
548 (down-mouse-3 . image-previous-frame
)))))))
550 (concat msg1
"text. This image has multiple frames.")))
551 ;;; (substitute-command-keys
552 ;;; "\\[image-toggle-animation] to animate."))))
554 (message "%s" (concat msg1
"text."))))))
559 (if (called-interactively-p 'any
) 'error
'message
)
560 "Cannot display image: %s" (cdr err
)))))
563 (define-minor-mode image-minor-mode
564 "Toggle Image minor mode in this buffer.
565 With a prefix argument ARG, enable Image minor mode if ARG is
566 positive, and disable it otherwise. If called from Lisp, enable
567 the mode if ARG is omitted or nil.
569 Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
570 to switch back to `image-mode' and display an image file as the
572 nil
(:eval
(if image-type
(format " Image[%s]" image-type
) " Image"))
577 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)))
580 (defun image-mode-as-text ()
581 "Set a non-image mode as major mode in combination with image minor mode.
582 A non-image major mode found from `auto-mode-alist' or Fundamental mode
583 displays an image file as text. `image-minor-mode' provides the key
584 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
585 to display an image file as the actual image.
587 You can use `image-mode-as-text' in `auto-mode-alist' when you want
588 to display an image file as text initially.
590 See commands `image-mode' and `image-minor-mode' for more information
593 ;; image-mode-as-text = normal-mode + image-minor-mode
594 (let ((previous-image-type image-type
)) ; preserve `image-type'
595 (if image-mode-previous-major-mode
596 ;; Restore previous major mode that was already found by this
597 ;; function and cached in `image-mode-previous-major-mode'
598 (funcall image-mode-previous-major-mode
)
599 (let ((auto-mode-alist
602 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
603 '(image-mode image-mode-maybe image-mode-as-text
))
606 (magic-fallback-mode-alist
609 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
610 '(image-mode image-mode-maybe image-mode-as-text
))
612 magic-fallback-mode-alist
))))
614 (setq-local image-mode-previous-major-mode major-mode
)))
615 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
616 (setq image-type previous-image-type
)
617 ;; Enable image minor mode with `C-c C-c'.
619 ;; Show the image file as text.
620 (image-toggle-display-text)
621 (message "%s" (concat
622 (substitute-command-keys
623 "Type \\[image-toggle-display] to view the image as ")
624 (if (image-get-display-property)
625 "text" "an image") "."))))
627 (define-obsolete-function-alias 'image-mode-maybe
'image-mode
"23.2")
629 (defun image-toggle-display-text ()
630 "Show the image file as text.
631 Remove text properties that display the image."
632 (let ((inhibit-read-only t
)
634 (modified (buffer-modified-p)))
635 (remove-list-of-text-properties (point-min) (point-max)
636 '(display read-nonsticky
;; intangible
637 read-only front-sticky
))
638 (set-buffer-modified-p modified
)
639 (if (called-interactively-p 'any
)
640 (message "Repeat this command to go back to displaying the image"))))
642 (defvar archive-superior-buffer
)
643 (defvar tar-superior-buffer
)
644 (declare-function image-flush
"image.c" (spec &optional frame
))
646 (defun image-toggle-display-image ()
647 "Show the image of the image file.
648 Turn the image data into a real image, but only if the whole file
650 (unless (derived-mode-p 'image-mode
)
651 (error "The buffer is not in Image mode"))
652 (let* ((filename (buffer-file-name))
653 (data-p (not (and filename
654 (file-readable-p filename
)
655 (not (file-remote-p filename
))
656 (not (buffer-modified-p))
657 (not (and (boundp 'archive-superior-buffer
)
658 archive-superior-buffer
))
659 (not (and (boundp 'tar-superior-buffer
)
660 tar-superior-buffer
)))))
661 (file-or-data (if data-p
663 (buffer-substring-no-properties (point-min) (point-max)))
665 ;; If we have a `fit-width' or a `fit-height', don't limit
666 ;; the size of the image to the window size.
667 (edges (and (null image-transform-resize
)
668 (window-inside-pixel-edges
669 (get-buffer-window (current-buffer)))))
670 (type (if (fboundp 'imagemagick-types
)
672 (image-type file-or-data nil data-p
)))
673 (image (if (not edges
)
674 (create-image file-or-data type data-p
)
675 (create-image file-or-data type data-p
676 :max-width
(- (nth 2 edges
) (nth 0 edges
))
677 :max-height
(- (nth 3 edges
) (nth 1 edges
)))))
678 (inhibit-read-only t
)
680 (modified (buffer-modified-p))
683 ;; Discard any stale image data before looking it up again.
685 (setq image
(append image
(image-transform-properties image
)))
689 rear-nonsticky
(display) ;; intangible
690 read-only t front-sticky
(read-only)))
692 (let ((buffer-file-truename nil
)) ; avoid changing dir mtime by lock_file
693 (add-text-properties (point-min) (point-max) props
)
694 (restore-buffer-modified-p modified
))
695 ;; Inhibit the cursor when the buffer contains only an image,
696 ;; because cursors look very strange on top of images.
697 (setq cursor-type nil
)
698 ;; This just makes the arrow displayed in the right fringe
699 ;; area look correct when the image is wider than the window.
700 (setq truncate-lines t
)
701 ;; Disable adding a newline at the end of the image file when it
702 ;; is written with, e.g., C-x C-w.
703 (if (coding-system-equal (coding-system-base buffer-file-coding-system
)
705 (setq-local find-file-literally t
))
706 ;; Allow navigation of large images.
707 (setq-local auto-hscroll-mode nil
)
708 (setq image-type type
)
709 (if (eq major-mode
'image-mode
)
710 (setq mode-name
(format "Image[%s]" type
)))
711 (image-transform-check-size)
712 (if (called-interactively-p 'any
)
713 (message "Repeat this command to go back to displaying the file as text"))))
715 (defun image-toggle-display ()
716 "Toggle between image and text display.
717 If the current buffer is displaying an image file as an image,
718 call `image-mode-as-text' to switch to text. Otherwise, display
719 the image by calling `image-mode'."
721 (if (image-get-display-property)
725 (defun image-after-revert-hook ()
726 (when (image-get-display-property)
727 (image-toggle-display-text)
728 ;; Update image display.
729 (mapc (lambda (window) (redraw-frame (window-frame window
)))
730 (get-buffer-window-list (current-buffer) 'nomini
'visible
))
731 (image-toggle-display-image)))
736 (defcustom image-animate-loop nil
737 "Non-nil means animated images loop forever, rather than playing once."
742 (defun image-toggle-animation ()
743 "Start or stop animating the current image.
744 If `image-animate-loop' is non-nil, animation loops forever.
745 Otherwise it plays once, then stops."
747 (let ((image (image-get-display-property))
751 (error "No image is present"))
752 ((null (setq animation
(image-multi-frame-p image
)))
753 (message "No image animation."))
755 (let ((timer (image-animate-timer image
)))
758 (let ((index (plist-get (cdr image
) :index
)))
759 ;; If we're at the end, restart.
761 (>= index
(1- (car animation
)))
763 (image-animate image index
764 (if image-animate-loop t
)))))))))
766 (defun image--set-speed (speed &optional multiply
)
767 "Set speed of an animated image to SPEED.
768 If MULTIPLY is non-nil, treat SPEED as a multiplication factor.
769 If SPEED is `reset', reset the magnitude of the speed to 1."
770 (let ((image (image-get-display-property)))
773 (error "No image is present"))
774 ((null image-multi-frame
)
775 (message "No image animation."))
777 (if (eq speed
'reset
)
778 (setq speed
(if (< (image-animate-get-speed image
) 0)
781 (image-animate-set-speed image speed multiply
)
782 ;; FIXME Hack to refresh an active image.
783 (when (image-animate-timer image
)
784 (image-toggle-animation)
785 (image-toggle-animation))
786 (message "Image speed is now %s" (image-animate-get-speed image
))))))
788 (defun image-increase-speed ()
789 "Increase the speed of current animated image by a factor of 2."
791 (image--set-speed 2 t
))
793 (defun image-decrease-speed ()
794 "Decrease the speed of current animated image by a factor of 2."
796 (image--set-speed 0.5 t
))
798 (defun image-reverse-speed ()
799 "Reverse the animation of the current image."
801 (image--set-speed -
1 t
))
803 (defun image-reset-speed ()
804 "Reset the animation speed of the current image."
806 (image--set-speed 'reset
))
808 (defun image-goto-frame (n &optional relative
)
809 "Show frame N of a multi-frame image.
810 Optional argument OFFSET non-nil means interpret N as relative to the
811 current frame. Frames are indexed from 1."
813 (list (or current-prefix-arg
814 (read-number "Show frame number: "))))
815 (let ((image (image-get-display-property)))
818 (error "No image is present"))
819 ((null image-multi-frame
)
820 (message "No image animation."))
822 (image-show-frame image
824 (+ n
(image-current-frame image
))
827 (defun image-next-frame (&optional n
)
828 "Switch to the next frame of a multi-frame image.
829 With optional argument N, switch to the Nth frame after the current one.
830 If N is negative, switch to the Nth frame before the current one."
832 (image-goto-frame n t
))
834 (defun image-previous-frame (&optional n
)
835 "Switch to the previous frame of a multi-frame image.
836 With optional argument N, switch to the Nth frame before the current one.
837 If N is negative, switch to the Nth frame after the current one."
839 (image-next-frame (- n
)))
842 ;;; Switching to the next/previous image
844 (defun image-next-file (&optional n
)
845 "Visit the next image in the same directory as the current image file.
846 With optional argument N, visit the Nth image file after the
847 current one, in cyclic alphabetical order.
849 This command visits the specified file via `find-alternate-file',
850 replacing the current Image mode buffer."
852 (unless (derived-mode-p 'image-mode
)
853 (error "The buffer is not in Image mode"))
854 (unless buffer-file-name
855 (error "The current image is not associated with a file"))
856 (let* ((file (file-name-nondirectory buffer-file-name
))
857 (images (image-mode--images-in-directory file
))
859 (catch 'image-visit-next-file
862 (throw 'image-visit-next-file
(1+ idx
)))
863 (setq idx
(1+ idx
))))
864 (setq idx
(mod (+ idx
(or n
1)) (length images
)))
865 (find-alternate-file (nth idx images
))))
867 (defun image-previous-file (&optional n
)
868 "Visit the preceding image in the same directory as the current file.
869 With optional argument N, visit the Nth image file preceding the
870 current one, in cyclic alphabetical order.
872 This command visits the specified file via `find-alternate-file',
873 replacing the current Image mode buffer."
875 (image-next-file (- n
)))
877 (defun image-mode--images-in-directory (file)
878 (let* ((dir (file-name-directory buffer-file-name
))
879 (files (directory-files dir nil
880 (image-file-name-regexp) t
)))
881 ;; Add the current file to the list of images if necessary, in
882 ;; case it does not match `image-file-name-regexp'.
883 (unless (member file files
)
885 (sort files
'string-lessp
)))
888 ;;; Support for bookmark.el
889 (declare-function bookmark-make-record-default
890 "bookmark" (&optional no-file no-context posn
))
891 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
892 (declare-function bookmark-default-handler
"bookmark" (bmk))
894 (defun image-bookmark-make-record ()
895 `(,@(bookmark-make-record-default nil
'no-context
0)
896 (image-type .
,image-type
)
897 (handler . image-bookmark-jump
)))
900 (defun image-bookmark-jump (bmk)
901 ;; This implements the `handler' function interface for record type
902 ;; returned by `bookmark-make-record-function', which see.
903 (prog1 (bookmark-default-handler bmk
)
904 (when (not (string= image-type
(bookmark-prop-get bmk
'image-type
)))
905 (image-toggle-display))))
908 ;; Not yet implemented.
909 ;; (defvar image-transform-minor-mode-map
910 ;; (let ((map (make-sparse-keymap)))
911 ;; ;; (define-key map [(control ?+)] 'image-scale-in)
912 ;; ;; (define-key map [(control ?-)] 'image-scale-out)
913 ;; ;; (define-key map [(control ?=)] 'image-scale-none)
914 ;; ;; (define-key map "c f h" 'image-scale-fit-height)
915 ;; ;; (define-key map "c ]" 'image-rotate-right)
917 ;; "Minor mode keymap `image-transform-mode'.")
919 ;; (define-minor-mode image-transform-mode
920 ;; "Minor mode for scaling and rotating images.
921 ;; With a prefix argument ARG, enable the mode if ARG is positive,
922 ;; and disable it otherwise. If called from Lisp, enable the mode
923 ;; if ARG is omitted or nil. This minor mode requires Emacs to have
924 ;; been compiled with ImageMagick support."
925 ;; nil "image-transform" image-transform-minor-mode-map)
928 (defsubst image-transform-width
(width height
)
929 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
930 The rotation angle is the value of `image-transform-rotation' in degrees."
931 (let ((angle (degrees-to-radians image-transform-rotation
)))
932 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
933 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
934 ;; rotation by the angle A. The projections onto the first axis
935 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
936 ;; (h/2) sin A, and the difference between the largest and the
937 ;; smallest of the four values is the expression below.
938 (+ (* width
(abs (cos angle
))) (* height
(abs (sin angle
))))))
940 ;; The following comment and code snippet are from
941 ;; ImageMagick-6.7.4-4/magick/distort.c
943 ;; /* Set the output image geometry to calculated 'best fit'.
944 ;; Yes this tends to 'over do' the file image size, ON PURPOSE!
945 ;; Do not do this for DePolar which needs to be exact for virtual tiling.
947 ;; if ( fix_bounds ) {
948 ;; geometry.x = (ssize_t) floor(min.x-0.5);
949 ;; geometry.y = (ssize_t) floor(min.y-0.5);
950 ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
951 ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
954 ;; Other parts of the same file show that here the origin is in the
955 ;; left lower corner of the image rectangle, the center of the
956 ;; rotation is the center of the rectangle and min.x and max.x
957 ;; (resp. min.y and max.y) are the smallest and the largest of the
958 ;; projections of the vertices onto the first (resp. second) axis.
960 (defun image-transform-fit-width (width height length
)
961 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
962 The rotation angle is the value of `image-transform-rotation'.
963 Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
964 an \"approximately uniformly\" scaled W x H rectangle, which
965 currently means that w is one of floor(s W) + {0, 1, -1} and h is
966 floor(s H), where s can be recovered as the value of `image-transform-scale'.
967 The value of `image-transform-rotation' may be replaced by
968 a slightly different angle. Currently this is done for values
969 close to a multiple of 90, see `image-transform-right-angle-fudge'."
970 (cond ((< (abs (- (mod (+ image-transform-rotation
90) 180) 90))
971 image-transform-right-angle-fudge
)
972 (cl-assert (not (zerop width
)) t
)
973 (setq image-transform-rotation
974 (float (round image-transform-rotation
))
975 image-transform-scale
(/ (float length
) width
))
977 ((< (abs (- (mod (+ image-transform-rotation
45) 90) 45))
978 image-transform-right-angle-fudge
)
979 (cl-assert (not (zerop height
)) t
)
980 (setq image-transform-rotation
981 (float (round image-transform-rotation
))
982 image-transform-scale
(/ (float length
) height
))
985 (cl-assert (not (and (zerop width
) (zerop height
))) t
)
986 (setq image-transform-scale
987 (/ (float (1- length
)) (image-transform-width width height
)))
988 ;; Assume we have a w x h image and an angle A, and let l =
989 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
990 ;; of the bounding box of the rotated image, as calculated by
991 ;; `image-transform-width'. The code snippet quoted above
992 ;; means that ImageMagick puts the rotated image in
993 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
994 ;; Elementary considerations show that this is equivalent to
995 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
996 ;; the given `length' parameter and our job is to determine
997 ;; reasonable values for w and h which satisfy these
999 (let ((w (floor (* image-transform-scale width
)))
1000 (h (floor (* image-transform-scale height
))))
1001 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
1002 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
1003 ;; hence l(w, h) > (L-1) - 2 = L-3.
1005 (cond ((= (mod w
2) (mod length
2))
1007 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
1009 ((<= (image-transform-width (1+ w
) h
) (1- length
))
1011 ;; No, it doesn't, but this implies that l(w-1, h) =
1012 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
1013 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
1018 (defun image-transform-check-size ()
1019 "Check that the image exactly fits the width/height of the window.
1021 Do this for an image of type `imagemagick' to make sure that the
1022 elisp code matches the way ImageMagick computes the bounding box
1023 of a rotated image."
1024 (when (and (not (numberp image-transform-resize
))
1025 (boundp 'image-type
)
1026 (eq image-type
'imagemagick
))
1027 (let ((size (image-display-size (image-get-display-property) t
)))
1028 (cond ((eq image-transform-resize
'fit-width
)
1029 (cl-assert (= (car size
)
1030 (- (nth 2 (window-inside-pixel-edges))
1031 (nth 0 (window-inside-pixel-edges))))
1033 ((eq image-transform-resize
'fit-height
)
1034 (cl-assert (= (cdr size
)
1035 (- (nth 3 (window-inside-pixel-edges))
1036 (nth 1 (window-inside-pixel-edges))))
1039 (defun image-transform-properties (spec)
1040 "Return rescaling/rotation properties for image SPEC.
1041 These properties are determined by the Image mode variables
1042 `image-transform-resize' and `image-transform-rotation'. The
1043 return value is suitable for appending to an image spec.
1045 Rescaling and rotation properties only take effect if Emacs is
1046 compiled with ImageMagick support."
1047 (setq image-transform-scale
1.0)
1048 (when (or image-transform-resize
1049 (/= image-transform-rotation
0.0))
1050 ;; Note: `image-size' looks up and thus caches the untransformed
1051 ;; image. There's no easy way to prevent that.
1052 (let* ((size (image-size spec t
))
1055 ((numberp image-transform-resize
)
1056 (unless (= image-transform-resize
1)
1057 (setq image-transform-scale image-transform-resize
)
1058 (cons nil
(floor (* image-transform-resize
(cdr size
))))))
1059 ((eq image-transform-resize
'fit-width
)
1060 (image-transform-fit-width
1061 (car size
) (cdr size
)
1062 (- (nth 2 (window-inside-pixel-edges))
1063 (nth 0 (window-inside-pixel-edges)))))
1064 ((eq image-transform-resize
'fit-height
)
1065 (let ((res (image-transform-fit-width
1066 (cdr size
) (car size
)
1067 (- (nth 3 (window-inside-pixel-edges))
1068 (nth 1 (window-inside-pixel-edges))))))
1069 (cons (cdr res
) (car res
)))))))
1070 `(,@(when (car resized
)
1071 (list :width
(car resized
)))
1072 ,@(when (cdr resized
)
1073 (list :height
(cdr resized
)))
1074 ,@(unless (= 0.0 image-transform-rotation
)
1075 (list :rotation image-transform-rotation
))))))
1077 (defun image-transform-set-scale (scale)
1078 "Prompt for a number, and resize the current image by that amount.
1079 This command has no effect unless Emacs is compiled with
1080 ImageMagick support."
1081 (interactive "nScale: ")
1082 (setq image-transform-resize scale
)
1083 (image-toggle-display-image))
1085 (defun image-transform-fit-to-height ()
1086 "Fit the current image to the height of the current window.
1087 This command has no effect unless Emacs is compiled with
1088 ImageMagick support."
1090 (setq image-transform-resize
'fit-height
)
1091 (image-toggle-display-image))
1093 (defun image-transform-fit-to-width ()
1094 "Fit the current image to the width of the current window.
1095 This command has no effect unless Emacs is compiled with
1096 ImageMagick support."
1098 (setq image-transform-resize
'fit-width
)
1099 (image-toggle-display-image))
1101 (defun image-transform-set-rotation (rotation)
1102 "Prompt for an angle ROTATION, and rotate the image by that amount.
1103 ROTATION should be in degrees. This command has no effect unless
1104 Emacs is compiled with ImageMagick support."
1105 (interactive "nRotation angle (in degrees): ")
1106 (setq image-transform-rotation
(float (mod rotation
360)))
1107 (image-toggle-display-image))
1109 (defun image-transform-reset ()
1110 "Display the current image with the default size and rotation.
1111 This command has no effect unless Emacs is compiled with
1112 ImageMagick support."
1114 (setq image-transform-resize nil
1115 image-transform-rotation
0.0
1116 image-transform-scale
1)
1117 (image-toggle-display-image))
1119 (provide 'image-mode
)
1121 ;;; image-mode.el ends here