1 ;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*-
3 ;; Copyright (C) 2005-2017 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 ;; hex of the file 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
47 "Alist of windows to window properties.
48 Each element has the form (WINDOW . ALIST).
49 See `image-mode-winprops'.")
51 (defvar image-mode-new-window-functions nil
52 "Special hook run when image data is requested in a new window.
53 It is called with one argument, the initial WINPROPS.")
55 ;; FIXME this doesn't seem mature yet. Document in manual when it is.
56 (defvar image-transform-resize nil
57 "The image resize operation.
58 Its value should be one of the following:
59 - nil, meaning no resizing.
60 - `fit-height', meaning to fit the image to the window height.
61 - `fit-width', meaning to fit the image to the window width.
62 - A number, which is a scale factor (the default size is 1).")
64 (defvar image-transform-scale
1.0
65 "The scale factor of the image being displayed.")
67 (defvar image-transform-rotation
0.0
68 "Rotation angle for the image in the current Image mode buffer.")
70 (defvar image-transform-right-angle-fudge
0.0001
71 "Snap distance to a multiple of a right angle.
72 There's no deep theory behind the default value, it should just
73 be somewhat larger than ImageMagick's MagickEpsilon.")
75 (defun image-mode-winprops (&optional window cleanup
)
76 "Return winprops of WINDOW.
77 A winprops object has the shape (WINDOW . ALIST).
78 WINDOW defaults to `selected-window' if it displays the current buffer, and
79 otherwise it defaults to t, used for times when the buffer is not displayed."
82 (if (eq (current-buffer) (window-buffer)) (selected-window) t
)))
84 ((not (windowp window
))
85 (error "Not a window: %s" window
)))
87 (setq image-mode-winprops-alist
88 (delq nil
(mapcar (lambda (winprop)
89 (let ((w (car-safe winprop
)))
90 (if (or (not (windowp w
)) (window-live-p w
))
92 image-mode-winprops-alist
))))
93 (let ((winprops (assq window image-mode-winprops-alist
)))
94 ;; For new windows, set defaults from the latest.
96 ;; Move window to front.
97 (setq image-mode-winprops-alist
98 (cons winprops
(delq winprops image-mode-winprops-alist
)))
99 (setq winprops
(cons window
100 (copy-alist (cdar image-mode-winprops-alist
))))
101 ;; Add winprops before running the hook, to avoid inf-loops if the hook
102 ;; triggers window-configuration-change-hook.
103 (setq image-mode-winprops-alist
104 (cons winprops image-mode-winprops-alist
))
105 (run-hook-with-args 'image-mode-new-window-functions winprops
))
108 (defun image-mode-window-get (prop &optional winprops
)
109 (declare (gv-setter (lambda (val)
110 `(image-mode-window-put ,prop
,val
,winprops
))))
111 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
112 (cdr (assq prop
(cdr winprops
))))
114 (defun image-mode-window-put (prop val
&optional winprops
)
115 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
116 (unless (eq t
(car winprops
))
117 (image-mode-window-put prop val t
))
118 (setcdr winprops
(cons (cons prop val
)
119 (delq (assq prop
(cdr winprops
)) (cdr winprops
)))))
121 (defun image-set-window-vscroll (vscroll)
122 (setf (image-mode-window-get 'vscroll
) vscroll
)
123 (set-window-vscroll (selected-window) vscroll
))
125 (defun image-set-window-hscroll (ncol)
126 (setf (image-mode-window-get 'hscroll
) ncol
)
127 (set-window-hscroll (selected-window) ncol
))
129 (defun image-mode-reapply-winprops ()
130 ;; When set-window-buffer, set hscroll and vscroll to what they were
131 ;; last time the image was displayed in this window.
132 (when (listp image-mode-winprops-alist
)
133 ;; Beware: this call to image-mode-winprops can't be optimized away,
134 ;; because it not only gets the winprops data but sets it up if needed
135 ;; (e.g. it's used by doc-view to display the image in a new window).
136 (let* ((winprops (image-mode-winprops nil t
))
137 (hscroll (image-mode-window-get 'hscroll winprops
))
138 (vscroll (image-mode-window-get 'vscroll winprops
)))
139 (when (image-get-display-property) ;Only do it if we display an image!
140 (if hscroll
(set-window-hscroll (selected-window) hscroll
))
141 (if vscroll
(set-window-vscroll (selected-window) vscroll
))))))
143 (defun image-mode-setup-winprops ()
144 ;; Record current scroll settings.
145 (unless (listp image-mode-winprops-alist
)
146 (setq image-mode-winprops-alist nil
))
147 (add-hook 'window-configuration-change-hook
148 'image-mode-reapply-winprops nil t
))
150 ;;; Image scrolling functions
152 (defun image-get-display-property ()
153 (get-char-property (point-min) 'display
154 ;; There might be different images for different displays.
155 (if (eq (window-buffer) (current-buffer))
158 (declare-function image-size
"image.c" (spec &optional pixels frame
))
159 (declare-function xwidget-info
"xwidget.c" (xwidget))
160 (declare-function xwidget-at
"xwidget.el" (pos))
162 (defun image-display-size (spec &optional pixels frame
)
163 "Wrapper around `image-size', handling slice display properties.
164 Like `image-size', the return value is (WIDTH . HEIGHT).
165 WIDTH and HEIGHT are in canonical character units if PIXELS is
166 nil, and in pixel units if PIXELS is non-nil.
168 If SPEC is an image display property, this function is equivalent to
169 `image-size'. If SPEC represents an xwidget object, defer to `xwidget-info'.
170 If SPEC is a list of properties containing `image' and `slice' properties,
171 return the display size taking the slice property into account. If the list
172 contains `image' but not `slice', return the `image-size' of the specified
174 (cond ((eq (car spec
) 'xwidget
)
175 (let ((xwi (xwidget-info (xwidget-at (point-min)))))
176 (cons (aref xwi
2) (aref xwi
3))))
177 ((eq (car spec
) 'image
)
178 (image-size spec pixels frame
))
179 (t (let ((image (assoc 'image spec
))
180 (slice (assoc 'slice spec
)))
181 (cond ((and image slice
)
183 (cons (nth 3 slice
) (nth 4 slice
))
184 (cons (/ (float (nth 3 slice
)) (frame-char-width frame
))
185 (/ (float (nth 4 slice
))
186 (frame-char-height frame
)))))
188 (image-size image pixels frame
))
190 (error "Invalid image specification: %s" spec
)))))))
192 (defun image-forward-hscroll (&optional n
)
193 "Scroll image in current window to the left by N character widths.
194 Stop if the right edge of the image is reached."
198 (image-set-window-hscroll (max 0 (+ (window-hscroll) n
))))
200 (let* ((image (image-get-display-property))
201 (edges (window-inside-edges))
202 (win-width (- (nth 2 edges
) (nth 0 edges
)))
203 (img-width (ceiling (car (image-display-size image
)))))
204 (image-set-window-hscroll (min (max 0 (- img-width win-width
))
205 (+ n
(window-hscroll))))))))
207 (defun image-backward-hscroll (&optional n
)
208 "Scroll image in current window to the right by N character widths.
209 Stop if the left edge of the image is reached."
211 (image-forward-hscroll (- n
)))
213 (defun image-next-line (n)
214 "Scroll image in current window upward by N lines.
215 Stop if the bottom edge of the image is reached."
219 (image-set-window-vscroll (max 0 (+ (window-vscroll) n
))))
221 (let* ((image (image-get-display-property))
222 (edges (window-inside-edges))
223 (win-height (- (nth 3 edges
) (nth 1 edges
)))
224 (img-height (ceiling (cdr (image-display-size image
)))))
225 (image-set-window-vscroll (min (max 0 (- img-height win-height
))
226 (+ n
(window-vscroll))))))))
228 (defun image-previous-line (&optional n
)
229 "Scroll image in current window downward by N lines.
230 Stop if the top edge of the image is reached."
232 (image-next-line (- n
)))
234 (defun image-scroll-up (&optional n
)
235 "Scroll image in current window upward by N lines.
236 Stop if the bottom edge of the image is reached.
237 If ARG is omitted or nil, scroll upward by a near full screen.
238 A near full screen is `next-screen-context-lines' less than a full screen.
239 Negative ARG means scroll downward.
240 If ARG is the atom `-', scroll downward by nearly full screen.
241 When calling from a program, supply as argument a number, nil, or `-'."
244 (let* ((edges (window-inside-edges))
245 (win-height (- (nth 3 edges
) (nth 1 edges
))))
247 (max 0 (- win-height next-screen-context-lines
)))))
249 (let* ((edges (window-inside-edges))
250 (win-height (- (nth 3 edges
) (nth 1 edges
))))
252 (min 0 (- next-screen-context-lines win-height
)))))
253 (t (image-next-line (prefix-numeric-value n
)))))
255 (defun image-scroll-down (&optional n
)
256 "Scroll image in current window downward by N lines.
257 Stop if the top edge of the image is reached.
258 If ARG is omitted or nil, scroll downward by a near full screen.
259 A near full screen is `next-screen-context-lines' less than a full screen.
260 Negative ARG means scroll upward.
261 If ARG is the atom `-', scroll upward by nearly full screen.
262 When calling from a program, supply as argument a number, nil, or `-'."
265 (let* ((edges (window-inside-edges))
266 (win-height (- (nth 3 edges
) (nth 1 edges
))))
268 (min 0 (- next-screen-context-lines win-height
)))))
270 (let* ((edges (window-inside-edges))
271 (win-height (- (nth 3 edges
) (nth 1 edges
))))
273 (max 0 (- win-height next-screen-context-lines
)))))
274 (t (image-next-line (- (prefix-numeric-value n
))))))
276 (defun image-scroll-left (&optional n
)
277 "Scroll image in current window leftward by N character widths.
278 Stop if the right edge of the image is reached.
279 If ARG is omitted or nil, scroll leftward by a near full screen.
280 A near full screen is 2 columns less than a full screen.
281 Negative ARG means scroll rightward.
282 If ARG is the atom `-', scroll rightward by nearly full screen.
283 When calling from a program, supply as argument a number, nil, or `-'."
286 (let* ((edges (window-inside-edges))
287 (win-width (- (nth 2 edges
) (nth 0 edges
))))
288 (image-forward-hscroll
289 (max 0 (- win-width
2)))))
291 (let* ((edges (window-inside-edges))
292 (win-width (- (nth 2 edges
) (nth 0 edges
))))
293 (image-forward-hscroll
294 (min 0 (- 2 win-width
)))))
295 (t (image-forward-hscroll (prefix-numeric-value n
)))))
297 (defun image-scroll-right (&optional n
)
298 "Scroll image in current window rightward by N character widths.
299 Stop if the left edge of the image is reached.
300 If ARG is omitted or nil, scroll downward by a near full screen.
301 A near full screen is 2 less than a full screen.
302 Negative ARG means scroll leftward.
303 If ARG is the atom `-', scroll leftward by nearly full screen.
304 When calling from a program, supply as argument a number, nil, or `-'."
307 (let* ((edges (window-inside-edges))
308 (win-width (- (nth 2 edges
) (nth 0 edges
))))
309 (image-forward-hscroll
310 (min 0 (- 2 win-width
)))))
312 (let* ((edges (window-inside-edges))
313 (win-width (- (nth 2 edges
) (nth 0 edges
))))
314 (image-forward-hscroll
315 (max 0 (- win-width
2)))))
316 (t (image-forward-hscroll (- (prefix-numeric-value n
))))))
318 (defun image-bol (arg)
319 "Scroll horizontally to the left edge of the image in the current window.
320 With argument ARG not nil or 1, move forward ARG - 1 lines first,
321 stopping if the top or bottom edge of the image is reached."
324 (/= (setq arg
(prefix-numeric-value arg
)) 1)
325 (image-next-line (- arg
1)))
326 (image-set-window-hscroll 0))
328 (defun image-eol (arg)
329 "Scroll horizontally to the right edge of the image in the current window.
330 With argument ARG not nil or 1, move forward ARG - 1 lines first,
331 stopping if the top or bottom edge of the image is reached."
334 (/= (setq arg
(prefix-numeric-value arg
)) 1)
335 (image-next-line (- arg
1)))
336 (let* ((image (image-get-display-property))
337 (edges (window-inside-edges))
338 (win-width (- (nth 2 edges
) (nth 0 edges
)))
339 (img-width (ceiling (car (image-display-size image
)))))
340 (image-set-window-hscroll (max 0 (- img-width win-width
)))))
343 "Scroll to the top-left corner of the image in the current window."
345 (image-set-window-hscroll 0)
346 (image-set-window-vscroll 0))
349 "Scroll to the bottom-right corner of the image in the current window."
351 (let* ((image (image-get-display-property))
352 (edges (window-inside-edges))
353 (win-width (- (nth 2 edges
) (nth 0 edges
)))
354 (img-width (ceiling (car (image-display-size image
))))
355 (win-height (- (nth 3 edges
) (nth 1 edges
)))
356 (img-height (ceiling (cdr (image-display-size image
)))))
357 (image-set-window-hscroll (max 0 (- img-width win-width
)))
358 (image-set-window-vscroll (max 0 (- img-height win-height
)))))
360 ;; Adjust frame and image size.
362 (defun image-mode-fit-frame (&optional frame toggle
)
363 "Fit FRAME to the current image.
364 If FRAME is omitted or nil, it defaults to the selected frame.
365 All other windows on the frame are deleted.
367 If called interactively, or if TOGGLE is non-nil, toggle between
368 fitting FRAME to the current image and restoring the size and
369 window configuration prior to the last `image-mode-fit-frame'
371 (interactive (list nil t
))
372 (let* ((buffer (current-buffer))
373 (display (image-get-display-property))
374 (size (image-display-size display
))
375 (saved (frame-parameter frame
'image-mode-saved-params
))
376 (window-configuration (current-window-configuration frame
))
377 (width (frame-width frame
))
378 (height (frame-height frame
)))
379 (with-selected-frame (or frame
(selected-frame))
380 (if (and toggle saved
381 (= (caar saved
) width
)
382 (= (cdar saved
) height
))
384 (set-frame-width frame
(car (nth 1 saved
)))
385 (set-frame-height frame
(cdr (nth 1 saved
)))
386 (set-window-configuration (nth 2 saved
))
387 (set-frame-parameter frame
'image-mode-saved-params nil
))
388 (delete-other-windows)
389 (switch-to-buffer buffer t t
)
390 (let* ((edges (window-inside-edges))
391 (inner-width (- (nth 2 edges
) (nth 0 edges
)))
392 (inner-height (- (nth 3 edges
) (nth 1 edges
))))
393 (set-frame-width frame
(+ (ceiling (car size
))
394 width
(- inner-width
)))
395 (set-frame-height frame
(+ (ceiling (cdr size
))
396 height
(- inner-height
)))
397 ;; The frame size after the above `set-frame-*' calls may
398 ;; differ from what we specified, due to window manager
399 ;; interference. We have to call `frame-width' and
400 ;; `frame-height' to get the actual results.
401 (set-frame-parameter frame
'image-mode-saved-params
402 (list (cons (frame-width)
405 window-configuration
)))))))
409 (defvar-local image-type nil
410 "The image type for the current Image mode buffer.")
412 (defvar-local image-multi-frame nil
413 "Non-nil if image for the current Image mode buffer has multiple frames.")
415 (defvar image-mode-previous-major-mode nil
416 "Internal variable to keep the previous non-image major mode.")
418 (defvar image-mode-map
419 (let ((map (make-sparse-keymap)))
420 (define-key map
"\C-c\C-c" 'image-toggle-display
)
421 (define-key map
"\C-c\C-x" 'image-toggle-hex-display
)
422 (define-key map
(kbd "SPC") 'image-scroll-up
)
423 (define-key map
(kbd "S-SPC") 'image-scroll-down
)
424 (define-key map
(kbd "DEL") 'image-scroll-down
)
425 (define-key map
(kbd "RET") 'image-toggle-animation
)
426 (define-key map
"F" 'image-goto-frame
)
427 (define-key map
"f" 'image-next-frame
)
428 (define-key map
"b" 'image-previous-frame
)
429 (define-key map
"n" 'image-next-file
)
430 (define-key map
"p" 'image-previous-file
)
431 (define-key map
"a+" 'image-increase-speed
)
432 (define-key map
"a-" 'image-decrease-speed
)
433 (define-key map
"a0" 'image-reset-speed
)
434 (define-key map
"ar" 'image-reverse-speed
)
435 (define-key map
"k" 'image-kill-buffer
)
436 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
437 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
438 (define-key map
[remap right-char
] 'image-forward-hscroll
)
439 (define-key map
[remap left-char
] 'image-backward-hscroll
)
440 (define-key map
[remap previous-line
] 'image-previous-line
)
441 (define-key map
[remap next-line
] 'image-next-line
)
442 (define-key map
[remap scroll-up
] 'image-scroll-up
)
443 (define-key map
[remap scroll-down
] 'image-scroll-down
)
444 (define-key map
[remap scroll-up-command
] 'image-scroll-up
)
445 (define-key map
[remap scroll-down-command
] 'image-scroll-down
)
446 (define-key map
[remap scroll-left
] 'image-scroll-left
)
447 (define-key map
[remap scroll-right
] 'image-scroll-right
)
448 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
449 (define-key map
[remap move-end-of-line
] 'image-eol
)
450 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
451 (define-key map
[remap end-of-buffer
] 'image-eob
)
452 (easy-menu-define image-mode-menu map
"Menu for Image mode."
454 ["Show as Text" image-toggle-display
:active t
455 :help
"Show image as text"]
456 ["Show as Hex" image-toggle-hex-display
:active t
457 :help
"Show image as hex"]
459 ["Fit to Window Height" image-transform-fit-to-height
460 :visible
(eq image-type
'imagemagick
)
461 :help
"Resize image to match the window height"]
462 ["Fit to Window Width" image-transform-fit-to-width
463 :visible
(eq image-type
'imagemagick
)
464 :help
"Resize image to match the window width"]
465 ["Rotate Image..." image-transform-set-rotation
466 :visible
(eq image-type
'imagemagick
)
467 :help
"Rotate the image"]
468 ["Reset Transformations" image-transform-reset
469 :visible
(eq image-type
'imagemagick
)
470 :help
"Reset all image transformations"]
475 (image-dired default-directory
))
476 :active default-directory
477 :help
"Show thumbnails for all images in this directory"]
478 ["Next Image" image-next-file
:active buffer-file-name
479 :help
"Move to next image in this directory"]
480 ["Previous Image" image-previous-file
:active buffer-file-name
481 :help
"Move to previous image in this directory"]
483 ["Fit Frame to Image" image-mode-fit-frame
:active t
484 :help
"Resize frame to match image"]
486 ["Animate Image" image-toggle-animation
:style toggle
487 :selected
(let ((image (image-get-display-property)))
488 (and image
(image-animate-timer image
)))
489 :active image-multi-frame
490 :help
"Toggle image animation"]
492 (lambda () (interactive)
493 (setq image-animate-loop
(not image-animate-loop
))
494 ;; FIXME this is a hacky way to make it affect a currently
496 (when (let ((image (image-get-display-property)))
497 (and image
(image-animate-timer image
)))
498 (image-toggle-animation)
499 (image-toggle-animation)))
500 :style toggle
:selected image-animate-loop
501 :active image-multi-frame
502 :help
"Animate images once, or forever?"]
503 ["Reverse Animation" image-reverse-speed
504 :style toggle
:selected
(let ((image (image-get-display-property)))
506 (image-animate-get-speed image
)
508 :active image-multi-frame
509 :help
"Reverse direction of this image's animation?"]
510 ["Speed Up Animation" image-increase-speed
511 :active image-multi-frame
512 :help
"Speed up this image's animation"]
513 ["Slow Down Animation" image-decrease-speed
514 :active image-multi-frame
515 :help
"Slow down this image's animation"]
516 ["Reset Animation Speed" image-reset-speed
517 :active image-multi-frame
518 :help
"Reset the speed of this image's animation"]
519 ["Next Frame" image-next-frame
:active image-multi-frame
520 :help
"Show the next frame of this image"]
521 ["Previous Frame" image-previous-frame
:active image-multi-frame
522 :help
"Show the previous frame of this image"]
523 ["Goto Frame..." image-goto-frame
:active image-multi-frame
524 :help
"Show a specific frame of this image"]
526 (make-composed-keymap (list map image-map
) special-mode-map
))
527 "Mode keymap for `image-mode'.")
529 (defvar image-minor-mode-map
530 (let ((map (make-sparse-keymap)))
531 (define-key map
"\C-c\C-c" 'image-toggle-display
)
532 (define-key map
"\C-c\C-x" 'image-toggle-hex-display
)
534 "Mode keymap for `image-minor-mode'.")
536 (defvar bookmark-make-record-function
)
538 (put 'image-mode
'mode-class
'special
)
542 "Major mode for image files.
543 You can use \\<image-mode-map>\\[image-toggle-display] or \\<image-mode-map>\\[image-toggle-hex-display]
544 to toggle between display as an image and display as text or hex.
551 (unless (display-images-p)
552 (error "Display does not support images"))
554 (kill-all-local-variables)
555 (setq major-mode
'image-mode
)
557 (if (not (image-get-display-property))
559 (image-toggle-display-image)
560 ;; If attempt to display the image fails.
561 (if (not (image-get-display-property))
562 (error "Invalid image")))
563 ;; Set next vars when image is already displayed but local
564 ;; variables were cleared by kill-all-local-variables
565 (setq cursor-type nil truncate-lines t
566 image-type
(plist-get (cdr (image-get-display-property)) :type
)))
568 (setq mode-name
(if image-type
(format "Image[%s]" image-type
) "Image"))
569 (use-local-map image-mode-map
)
571 ;; Use our own bookmarking function for images.
572 (setq-local bookmark-make-record-function
573 #'image-bookmark-make-record
)
575 ;; Keep track of [vh]scroll when switching buffers
576 (image-mode-setup-winprops)
578 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
579 (add-hook 'after-revert-hook
'image-after-revert-hook nil t
)
580 (run-mode-hooks 'image-mode-hook
)
581 (let ((image (image-get-display-property))
582 (msg1 (substitute-command-keys
583 "Type \\[image-toggle-display] or \\[image-toggle-hex-display] to view the image as "))
587 (message "%s" (concat msg1
"an image.")))
588 ((setq animated
(image-multi-frame-p image
))
589 (setq image-multi-frame t
595 (1+ (image-current-frame ',image
))
599 mouse-3: Previous frame"
600 'mouse-face
'mode-line-highlight
605 (down-mouse-1 . image-next-frame
)
606 (down-mouse-3 . image-previous-frame
)))))))
608 (concat msg1
"text. This image has multiple frames.")))
609 ;;; (substitute-command-keys
610 ;;; "\\[image-toggle-animation] to animate."))))
612 (message "%s" (concat msg1
"text or hex."))))))
617 (if (called-interactively-p 'any
) 'error
'message
)
618 "Cannot display image: %s" (cdr err
)))))
621 (define-minor-mode image-minor-mode
622 "Toggle Image minor mode in this buffer.
623 With a prefix argument ARG, enable Image minor mode if ARG is
624 positive, and disable it otherwise. If called from Lisp, enable
625 the mode if ARG is omitted or nil.
627 Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
628 to switch back to `image-mode' and display an image file as the
630 nil
(:eval
(if image-type
(format " Image[%s]" image-type
) " Image"))
635 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)))
638 (defun image-mode-to-text ()
639 "Set a non-image mode as major mode in combination with image minor mode.
640 A non-mage major mode found from `auto-mode-alist' or fundamental mode
641 displays an image file as text."
642 ;; image-mode-as-text = normal-mode + image-minor-mode
643 (let ((previous-image-type image-type
)) ; preserve `image-type'
644 (if image-mode-previous-major-mode
645 ;; Restore previous major mode that was already found by this
646 ;; function and cached in `image-mode-previous-major-mode'
647 (funcall image-mode-previous-major-mode
)
648 (let ((auto-mode-alist
651 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
652 '(image-mode image-mode-maybe image-mode-as-text
))
655 (magic-fallback-mode-alist
658 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
659 '(image-mode image-mode-maybe image-mode-as-text
))
661 magic-fallback-mode-alist
))))
663 (setq-local image-mode-previous-major-mode major-mode
)))
664 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
665 (setq image-type previous-image-type
)
666 ;; Enable image minor mode with `C-c C-c'.
668 ;; Show the image file as text.
669 (image-toggle-display-text)))
671 (defun image-mode-as-hex ()
672 "Set a non-image mode as major mode in combination with image minor mode.
673 A non-mage major mode found from `auto-mode-alist' or fundamental mode
674 displays an image file as hex. `image-minor-mode' provides the key
675 \\<image-mode-map>\\[image-toggle-hex-display] to switch back to `image-mode'
676 to display an image file as the actual image.
678 You can use `image-mode-as-hex' in `auto-mode-alist' when you want to
679 to display an image file as hex initially.
681 See commands `image-mode' and `image-minor-mode' for more information
687 (message "%s" (concat
688 (substitute-command-keys
689 "Type \\[image-toggle-hex-display] or \\[image-toggle-display] to view the image as ")
690 (if (image-get-display-property)
691 "hex" "an image or text") ".")))
693 (defun image-mode-as-text ()
694 "Set a non-image mode as major mode in combination with image minor mode.
695 A non-image major mode found from `auto-mode-alist' or Fundamental mode
696 displays an image file as text. `image-minor-mode' provides the key
697 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
698 to display an image file as the actual image.
700 You can use `image-mode-as-text' in `auto-mode-alist' when you want
701 to display an image file as text initially.
703 See commands `image-mode' and `image-minor-mode' for more information
707 (message "%s" (concat
708 (substitute-command-keys
709 "Type \\[image-toggle-display] or \\[image-toggle-hex-display] to view the image as ")
710 (if (image-get-display-property)
711 "text" "an image or hex") ".")))
713 (define-obsolete-function-alias 'image-mode-maybe
'image-mode
"23.2")
715 (defun image-toggle-display-text ()
716 "Show the image file as text.
717 Remove text properties that display the image."
718 (let ((inhibit-read-only t
)
720 (modified (buffer-modified-p)))
721 (remove-list-of-text-properties (point-min) (point-max)
722 '(display read-nonsticky
;; intangible
723 read-only front-sticky
))
724 (set-buffer-modified-p modified
)
725 (if (called-interactively-p 'any
)
726 (message "Repeat this command to go back to displaying the image"))))
728 (defvar archive-superior-buffer
)
729 (defvar tar-superior-buffer
)
730 (declare-function image-flush
"image.c" (spec &optional frame
))
732 (defun image-toggle-display-image ()
733 "Show the image of the image file.
734 Turn the image data into a real image, but only if the whole file
736 (unless (derived-mode-p 'image-mode
)
737 (error "The buffer is not in Image mode"))
738 (let* ((filename (buffer-file-name))
739 (data-p (not (and filename
740 (file-readable-p filename
)
741 (not (file-remote-p filename
))
742 (not (buffer-modified-p))
743 (not (and (boundp 'archive-superior-buffer
)
744 archive-superior-buffer
))
745 (not (and (boundp 'tar-superior-buffer
)
746 tar-superior-buffer
))
747 ;; This means the buffer holds the
748 ;; decrypted content (bug#21870).
749 (not (and (boundp 'epa-file-encrypt-to
)
751 'epa-file-encrypt-to
))))))
752 (file-or-data (if data-p
754 (buffer-substring-no-properties (point-min) (point-max)))
756 ;; If we have a `fit-width' or a `fit-height', don't limit
757 ;; the size of the image to the window size.
758 (edges (and (null image-transform-resize
)
759 (window-inside-pixel-edges
760 (get-buffer-window (current-buffer)))))
761 (type (if (fboundp 'imagemagick-types
)
763 (image-type file-or-data nil data-p
)))
764 (image (if (not edges
)
765 (create-image file-or-data type data-p
)
766 (create-image file-or-data type data-p
767 :max-width
(- (nth 2 edges
) (nth 0 edges
))
768 :max-height
(- (nth 3 edges
) (nth 1 edges
)))))
769 (inhibit-read-only t
)
771 (modified (buffer-modified-p))
774 ;; Discard any stale image data before looking it up again.
776 (setq image
(append image
(image-transform-properties image
)))
780 rear-nonsticky
(display) ;; intangible
781 read-only t front-sticky
(read-only)))
783 (let ((buffer-file-truename nil
)) ; avoid changing dir mtime by lock_file
784 (add-text-properties (point-min) (point-max) props
)
785 (restore-buffer-modified-p modified
))
786 ;; Inhibit the cursor when the buffer contains only an image,
787 ;; because cursors look very strange on top of images.
788 (setq cursor-type nil
)
789 ;; This just makes the arrow displayed in the right fringe
790 ;; area look correct when the image is wider than the window.
791 (setq truncate-lines t
)
792 ;; Disable adding a newline at the end of the image file when it
793 ;; is written with, e.g., C-x C-w.
794 (if (coding-system-equal (coding-system-base buffer-file-coding-system
)
796 (setq-local find-file-literally t
))
797 ;; Allow navigation of large images.
798 (setq-local auto-hscroll-mode nil
)
799 (setq image-type type
)
800 (if (eq major-mode
'image-mode
)
801 (setq mode-name
(format "Image[%s]" type
)))
802 (image-transform-check-size)
803 (if (called-interactively-p 'any
)
804 (message "Repeat this command to go back to displaying the file as text"))))
806 (defun image-toggle-hex-display ()
807 "Toggle between image and hex display."
809 (if (image-get-display-property)
811 (if (eq major-mode
'fundamental-mode
)
815 (defun image-toggle-display ()
816 "Toggle between image and text display.
818 If the current buffer is displaying an image file as an image,
819 call `image-mode-as-text' to switch to text or hex display.
820 Otherwise, display the image by calling `image-mode'"
822 (if (image-get-display-property)
824 (if (eq major-mode
'hexl-mode
)
828 (defun image-kill-buffer ()
829 "Kill the current buffer."
831 (kill-buffer (current-buffer)))
833 (defun image-after-revert-hook ()
835 (when (not (image-get-display-property))
836 (image-toggle-display-image))
837 (when (image-get-display-property)
838 (image-toggle-display-text)
839 ;; Update image display.
840 (mapc (lambda (window) (redraw-frame (window-frame window
)))
841 (get-buffer-window-list (current-buffer) 'nomini
'visible
))
842 (image-toggle-display-image)))
847 (defcustom image-animate-loop nil
848 "Non-nil means animated images loop forever, rather than playing once."
853 (defun image-toggle-animation ()
854 "Start or stop animating the current image.
855 If `image-animate-loop' is non-nil, animation loops forever.
856 Otherwise it plays once, then stops."
858 (let ((image (image-get-display-property))
862 (error "No image is present"))
863 ((null (setq animation
(image-multi-frame-p image
)))
864 (message "No image animation."))
866 (let ((timer (image-animate-timer image
)))
869 (let ((index (plist-get (cdr image
) :index
)))
870 ;; If we're at the end, restart.
872 (>= index
(1- (car animation
)))
874 (image-animate image index
875 (if image-animate-loop t
)))))))))
877 (defun image--set-speed (speed &optional multiply
)
878 "Set speed of an animated image to SPEED.
879 If MULTIPLY is non-nil, treat SPEED as a multiplication factor.
880 If SPEED is `reset', reset the magnitude of the speed to 1."
881 (let ((image (image-get-display-property)))
884 (error "No image is present"))
885 ((null image-multi-frame
)
886 (message "No image animation."))
888 (if (eq speed
'reset
)
889 (setq speed
(if (< (image-animate-get-speed image
) 0)
892 (image-animate-set-speed image speed multiply
)
893 ;; FIXME Hack to refresh an active image.
894 (when (image-animate-timer image
)
895 (image-toggle-animation)
896 (image-toggle-animation))
897 (message "Image speed is now %s" (image-animate-get-speed image
))))))
899 (defun image-increase-speed ()
900 "Increase the speed of current animated image by a factor of 2."
902 (image--set-speed 2 t
))
904 (defun image-decrease-speed ()
905 "Decrease the speed of current animated image by a factor of 2."
907 (image--set-speed 0.5 t
))
909 (defun image-reverse-speed ()
910 "Reverse the animation of the current image."
912 (image--set-speed -
1 t
))
914 (defun image-reset-speed ()
915 "Reset the animation speed of the current image."
917 (image--set-speed 'reset
))
919 (defun image-goto-frame (n &optional relative
)
920 "Show frame N of a multi-frame image.
921 Optional argument OFFSET non-nil means interpret N as relative to the
922 current frame. Frames are indexed from 1."
924 (list (or current-prefix-arg
925 (read-number "Show frame number: "))))
926 (let ((image (image-get-display-property)))
929 (error "No image is present"))
930 ((null image-multi-frame
)
931 (message "No image animation."))
933 (image-show-frame image
935 (+ n
(image-current-frame image
))
938 (defun image-next-frame (&optional n
)
939 "Switch to the next frame of a multi-frame image.
940 With optional argument N, switch to the Nth frame after the current one.
941 If N is negative, switch to the Nth frame before the current one."
943 (image-goto-frame n t
))
945 (defun image-previous-frame (&optional n
)
946 "Switch to the previous frame of a multi-frame image.
947 With optional argument N, switch to the Nth frame before the current one.
948 If N is negative, switch to the Nth frame after the current one."
950 (image-next-frame (- n
)))
953 ;;; Switching to the next/previous image
955 (defun image-next-file (&optional n
)
956 "Visit the next image in the same directory as the current image file.
957 With optional argument N, visit the Nth image file after the
958 current one, in cyclic alphabetical order.
960 This command visits the specified file via `find-alternate-file',
961 replacing the current Image mode buffer."
963 (unless (derived-mode-p 'image-mode
)
964 (error "The buffer is not in Image mode"))
965 (unless buffer-file-name
966 (error "The current image is not associated with a file"))
967 (let* ((file (file-name-nondirectory buffer-file-name
))
968 (images (image-mode--images-in-directory file
))
970 (catch 'image-visit-next-file
973 (throw 'image-visit-next-file
(1+ idx
)))
974 (setq idx
(1+ idx
))))
975 (setq idx
(mod (+ idx
(or n
1)) (length images
)))
976 (find-alternate-file (nth idx images
))))
978 (defun image-previous-file (&optional n
)
979 "Visit the preceding image in the same directory as the current file.
980 With optional argument N, visit the Nth image file preceding the
981 current one, in cyclic alphabetical order.
983 This command visits the specified file via `find-alternate-file',
984 replacing the current Image mode buffer."
986 (image-next-file (- n
)))
988 (defun image-mode--images-in-directory (file)
989 (let* ((dir (file-name-directory buffer-file-name
))
990 (files (directory-files dir nil
991 (image-file-name-regexp) t
)))
992 ;; Add the current file to the list of images if necessary, in
993 ;; case it does not match `image-file-name-regexp'.
994 (unless (member file files
)
996 (sort files
'string-lessp
)))
999 ;;; Support for bookmark.el
1000 (declare-function bookmark-make-record-default
1001 "bookmark" (&optional no-file no-context posn
))
1002 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
1003 (declare-function bookmark-default-handler
"bookmark" (bmk))
1005 (defun image-bookmark-make-record ()
1006 `(,@(bookmark-make-record-default nil
'no-context
0)
1007 (image-type .
,image-type
)
1008 (handler . image-bookmark-jump
)))
1011 (defun image-bookmark-jump (bmk)
1012 ;; This implements the `handler' function interface for record type
1013 ;; returned by `bookmark-make-record-function', which see.
1014 (prog1 (bookmark-default-handler bmk
)
1015 (when (not (string= image-type
(bookmark-prop-get bmk
'image-type
)))
1016 (image-toggle-display))))
1019 ;; Not yet implemented.
1020 ;; (defvar image-transform-minor-mode-map
1021 ;; (let ((map (make-sparse-keymap)))
1022 ;; ;; (define-key map [(control ?+)] 'image-scale-in)
1023 ;; ;; (define-key map [(control ?-)] 'image-scale-out)
1024 ;; ;; (define-key map [(control ?=)] 'image-scale-none)
1025 ;; ;; (define-key map "c f h" 'image-scale-fit-height)
1026 ;; ;; (define-key map "c ]" 'image-rotate-right)
1028 ;; "Minor mode keymap `image-transform-mode'.")
1030 ;; (define-minor-mode image-transform-mode
1031 ;; "Minor mode for scaling and rotating images.
1032 ;; With a prefix argument ARG, enable the mode if ARG is positive,
1033 ;; and disable it otherwise. If called from Lisp, enable the mode
1034 ;; if ARG is omitted or nil. This minor mode requires Emacs to have
1035 ;; been compiled with ImageMagick support."
1036 ;; nil "image-transform" image-transform-minor-mode-map)
1039 (defsubst image-transform-width
(width height
)
1040 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
1041 The rotation angle is the value of `image-transform-rotation' in degrees."
1042 (let ((angle (degrees-to-radians image-transform-rotation
)))
1043 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
1044 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
1045 ;; rotation by the angle A. The projections onto the first axis
1046 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
1047 ;; (h/2) sin A, and the difference between the largest and the
1048 ;; smallest of the four values is the expression below.
1049 (+ (* width
(abs (cos angle
))) (* height
(abs (sin angle
))))))
1051 ;; The following comment and code snippet are from
1052 ;; ImageMagick-6.7.4-4/magick/distort.c
1054 ;; /* Set the output image geometry to calculated 'best fit'.
1055 ;; Yes this tends to 'over do' the file image size, ON PURPOSE!
1056 ;; Do not do this for DePolar which needs to be exact for virtual tiling.
1058 ;; if ( fix_bounds ) {
1059 ;; geometry.x = (ssize_t) floor(min.x-0.5);
1060 ;; geometry.y = (ssize_t) floor(min.y-0.5);
1061 ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
1062 ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
1065 ;; Other parts of the same file show that here the origin is in the
1066 ;; left lower corner of the image rectangle, the center of the
1067 ;; rotation is the center of the rectangle and min.x and max.x
1068 ;; (resp. min.y and max.y) are the smallest and the largest of the
1069 ;; projections of the vertices onto the first (resp. second) axis.
1071 (defun image-transform-fit-width (width height length
)
1072 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
1073 The rotation angle is the value of `image-transform-rotation'.
1074 Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
1075 an \"approximately uniformly\" scaled W x H rectangle, which
1076 currently means that w is one of floor(s W) + {0, 1, -1} and h is
1077 floor(s H), where s can be recovered as the value of `image-transform-scale'.
1078 The value of `image-transform-rotation' may be replaced by
1079 a slightly different angle. Currently this is done for values
1080 close to a multiple of 90, see `image-transform-right-angle-fudge'."
1081 (cond ((< (abs (- (mod (+ image-transform-rotation
90) 180) 90))
1082 image-transform-right-angle-fudge
)
1083 (cl-assert (not (zerop width
)) t
)
1084 (setq image-transform-rotation
1085 (float (round image-transform-rotation
))
1086 image-transform-scale
(/ (float length
) width
))
1088 ((< (abs (- (mod (+ image-transform-rotation
45) 90) 45))
1089 image-transform-right-angle-fudge
)
1090 (cl-assert (not (zerop height
)) t
)
1091 (setq image-transform-rotation
1092 (float (round image-transform-rotation
))
1093 image-transform-scale
(/ (float length
) height
))
1096 (cl-assert (not (and (zerop width
) (zerop height
))) t
)
1097 (setq image-transform-scale
1098 (/ (float (1- length
)) (image-transform-width width height
)))
1099 ;; Assume we have a w x h image and an angle A, and let l =
1100 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
1101 ;; of the bounding box of the rotated image, as calculated by
1102 ;; `image-transform-width'. The code snippet quoted above
1103 ;; means that ImageMagick puts the rotated image in
1104 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
1105 ;; Elementary considerations show that this is equivalent to
1106 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
1107 ;; the given `length' parameter and our job is to determine
1108 ;; reasonable values for w and h which satisfy these
1110 (let ((w (floor (* image-transform-scale width
)))
1111 (h (floor (* image-transform-scale height
))))
1112 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
1113 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
1114 ;; hence l(w, h) > (L-1) - 2 = L-3.
1116 (cond ((= (mod w
2) (mod length
2))
1118 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
1120 ((<= (image-transform-width (1+ w
) h
) (1- length
))
1122 ;; No, it doesn't, but this implies that l(w-1, h) =
1123 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
1124 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
1129 (defun image-transform-check-size ()
1130 "Check that the image exactly fits the width/height of the window.
1132 Do this for an image of type `imagemagick' to make sure that the
1133 elisp code matches the way ImageMagick computes the bounding box
1134 of a rotated image."
1135 (when (and (not (numberp image-transform-resize
))
1136 (boundp 'image-type
)
1137 (eq image-type
'imagemagick
))
1138 (let ((size (image-display-size (image-get-display-property) t
)))
1139 (cond ((eq image-transform-resize
'fit-width
)
1140 (cl-assert (= (car size
)
1141 (- (nth 2 (window-inside-pixel-edges))
1142 (nth 0 (window-inside-pixel-edges))))
1144 ((eq image-transform-resize
'fit-height
)
1145 (cl-assert (= (cdr size
)
1146 (- (nth 3 (window-inside-pixel-edges))
1147 (nth 1 (window-inside-pixel-edges))))
1150 (defun image-transform-properties (spec)
1151 "Return rescaling/rotation properties for image SPEC.
1152 These properties are determined by the Image mode variables
1153 `image-transform-resize' and `image-transform-rotation'. The
1154 return value is suitable for appending to an image spec.
1156 Rescaling and rotation properties only take effect if Emacs is
1157 compiled with ImageMagick support."
1158 (setq image-transform-scale
1.0)
1159 (when (or image-transform-resize
1160 (/= image-transform-rotation
0.0))
1161 ;; Note: `image-size' looks up and thus caches the untransformed
1162 ;; image. There's no easy way to prevent that.
1163 (let* ((size (image-size spec t
))
1166 ((numberp image-transform-resize
)
1167 (unless (= image-transform-resize
1)
1168 (setq image-transform-scale image-transform-resize
)
1169 (cons nil
(floor (* image-transform-resize
(cdr size
))))))
1170 ((eq image-transform-resize
'fit-width
)
1171 (image-transform-fit-width
1172 (car size
) (cdr size
)
1173 (- (nth 2 (window-inside-pixel-edges))
1174 (nth 0 (window-inside-pixel-edges)))))
1175 ((eq image-transform-resize
'fit-height
)
1176 (let ((res (image-transform-fit-width
1177 (cdr size
) (car size
)
1178 (- (nth 3 (window-inside-pixel-edges))
1179 (nth 1 (window-inside-pixel-edges))))))
1180 (cons (cdr res
) (car res
)))))))
1181 `(,@(when (car resized
)
1182 (list :width
(car resized
)))
1183 ,@(when (cdr resized
)
1184 (list :height
(cdr resized
)))
1185 ,@(unless (= 0.0 image-transform-rotation
)
1186 (list :rotation image-transform-rotation
))))))
1188 (defun image-transform-set-scale (scale)
1189 "Prompt for a number, and resize the current image by that amount.
1190 This command has no effect unless Emacs is compiled with
1191 ImageMagick support."
1192 (interactive "nScale: ")
1193 (setq image-transform-resize scale
)
1194 (image-toggle-display-image))
1196 (defun image-transform-fit-to-height ()
1197 "Fit the current image to the height of the current window.
1198 This command has no effect unless Emacs is compiled with
1199 ImageMagick support."
1201 (setq image-transform-resize
'fit-height
)
1202 (image-toggle-display-image))
1204 (defun image-transform-fit-to-width ()
1205 "Fit the current image to the width of the current window.
1206 This command has no effect unless Emacs is compiled with
1207 ImageMagick support."
1209 (setq image-transform-resize
'fit-width
)
1210 (image-toggle-display-image))
1212 (defun image-transform-set-rotation (rotation)
1213 "Prompt for an angle ROTATION, and rotate the image by that amount.
1214 ROTATION should be in degrees. This command has no effect unless
1215 Emacs is compiled with ImageMagick support."
1216 (interactive "nRotation angle (in degrees): ")
1217 (setq image-transform-rotation
(float (mod rotation
360)))
1218 (image-toggle-display-image))
1220 (defun image-transform-reset ()
1221 "Display the current image with the default size and rotation.
1222 This command has no effect unless Emacs is compiled with
1223 ImageMagick support."
1225 (setq image-transform-resize nil
1226 image-transform-rotation
0.0
1227 image-transform-scale
1)
1228 (image-toggle-display-image))
1230 (provide 'image-mode
)
1232 ;;; image-mode.el ends here