Fix bug #13515 with processing DBCS file names on MS-Windows.
[emacs.git] / lisp / image-mode.el
bloba95dde1d999cb13175d649b6a1f20ab802b57d0a
1 ;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright (C) 2005-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Richard Stallman <rms@gnu.org>
6 ;; Keywords: multimedia
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
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.
34 ;; Todo:
36 ;; Consolidate with doc-view to make them work on directories of images or on
37 ;; image files containing various "pages".
39 ;;; Code:
41 (require 'image)
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."
57 (cond ((null window)
58 (setq window
59 (if (eq (current-buffer) (window-buffer)) (selected-window) t)))
60 ((eq window t))
61 ((not (windowp window))
62 (error "Not a window: %s" window)))
63 (when cleanup
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))
68 winprop)))
69 image-mode-winprops-alist))))
70 (let ((winprops (assq window image-mode-winprops-alist)))
71 ;; For new windows, set defaults from the latest.
72 (unless winprops
73 (setq winprops (cons window
74 (copy-alist (cdar image-mode-winprops-alist))))
75 (run-hook-with-args 'image-mode-new-window-functions winprops))
76 ;; Move window to front.
77 (setq image-mode-winprops-alist
78 (cons winprops (delq winprops image-mode-winprops-alist)))
79 winprops))
81 (defun image-mode-window-get (prop &optional winprops)
82 (declare (gv-setter (lambda (val)
83 `(image-mode-window-put ,prop ,val ,winprops))))
84 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
85 (cdr (assq prop (cdr winprops))))
87 (defun image-mode-window-put (prop val &optional winprops)
88 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
89 (setcdr winprops (cons (cons prop val)
90 (delq (assq prop (cdr winprops)) (cdr winprops)))))
92 (defun image-set-window-vscroll (vscroll)
93 (setf (image-mode-window-get 'vscroll) vscroll)
94 (set-window-vscroll (selected-window) vscroll))
96 (defun image-set-window-hscroll (ncol)
97 (setf (image-mode-window-get 'hscroll) ncol)
98 (set-window-hscroll (selected-window) ncol))
100 (defun image-mode-reapply-winprops ()
101 ;; When set-window-buffer, set hscroll and vscroll to what they were
102 ;; last time the image was displayed in this window.
103 (when (and (image-get-display-property)
104 (listp image-mode-winprops-alist))
105 (let* ((winprops (image-mode-winprops nil t))
106 (hscroll (image-mode-window-get 'hscroll winprops))
107 (vscroll (image-mode-window-get 'vscroll winprops)))
108 (if hscroll (set-window-hscroll (selected-window) hscroll))
109 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
111 (defun image-mode-setup-winprops ()
112 ;; Record current scroll settings.
113 (unless (listp image-mode-winprops-alist)
114 (setq image-mode-winprops-alist nil))
115 (add-hook 'window-configuration-change-hook
116 'image-mode-reapply-winprops nil t))
118 ;;; Image scrolling functions
120 (defun image-get-display-property ()
121 (get-char-property (point-min) 'display
122 ;; There might be different images for different displays.
123 (if (eq (window-buffer) (current-buffer))
124 (selected-window))))
126 (declare-function image-size "image.c" (spec &optional pixels frame))
128 (defun image-display-size (spec &optional pixels frame)
129 "Wrapper around `image-size', handling slice display properties.
130 Like `image-size', the return value is (WIDTH . HEIGHT).
131 WIDTH and HEIGHT are in canonical character units if PIXELS is
132 nil, and in pixel units if PIXELS is non-nil.
134 If SPEC is an image display property, this function is equivalent
135 to `image-size'. If SPEC is a list of properties containing
136 `image' and `slice' properties, return the display size taking
137 the slice property into account. If the list contains `image'
138 but not `slice', return the `image-size' of the specified image."
139 (if (eq (car spec) 'image)
140 (image-size spec pixels frame)
141 (let ((image (assoc 'image spec))
142 (slice (assoc 'slice spec)))
143 (cond ((and image slice)
144 (if pixels
145 (cons (nth 3 slice) (nth 4 slice))
146 (cons (/ (float (nth 3 slice)) (frame-char-width frame))
147 (/ (float (nth 4 slice)) (frame-char-height frame)))))
148 (image
149 (image-size image pixels frame))
151 (error "Invalid image specification: %s" spec))))))
153 (defun image-forward-hscroll (&optional n)
154 "Scroll image in current window to the left by N character widths.
155 Stop if the right edge of the image is reached."
156 (interactive "p")
157 (cond ((= n 0) nil)
158 ((< n 0)
159 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
161 (let* ((image (image-get-display-property))
162 (edges (window-inside-edges))
163 (win-width (- (nth 2 edges) (nth 0 edges)))
164 (img-width (ceiling (car (image-display-size image)))))
165 (image-set-window-hscroll (min (max 0 (- img-width win-width))
166 (+ n (window-hscroll))))))))
168 (defun image-backward-hscroll (&optional n)
169 "Scroll image in current window to the right by N character widths.
170 Stop if the left edge of the image is reached."
171 (interactive "p")
172 (image-forward-hscroll (- n)))
174 (defun image-next-line (n)
175 "Scroll image in current window upward by N lines.
176 Stop if the bottom edge of the image is reached."
177 (interactive "p")
178 (cond ((= n 0) nil)
179 ((< n 0)
180 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
182 (let* ((image (image-get-display-property))
183 (edges (window-inside-edges))
184 (win-height (- (nth 3 edges) (nth 1 edges)))
185 (img-height (ceiling (cdr (image-display-size image)))))
186 (image-set-window-vscroll (min (max 0 (- img-height win-height))
187 (+ n (window-vscroll))))))))
189 (defun image-previous-line (&optional n)
190 "Scroll image in current window downward by N lines.
191 Stop if the top edge of the image is reached."
192 (interactive "p")
193 (image-next-line (- n)))
195 (defun image-scroll-up (&optional n)
196 "Scroll image in current window upward by N lines.
197 Stop if the bottom edge of the image is reached.
198 If ARG is omitted or nil, scroll upward by a near full screen.
199 A near full screen is `next-screen-context-lines' less than a full screen.
200 Negative ARG means scroll downward.
201 If ARG is the atom `-', scroll downward by nearly full screen.
202 When calling from a program, supply as argument a number, nil, or `-'."
203 (interactive "P")
204 (cond ((null n)
205 (let* ((edges (window-inside-edges))
206 (win-height (- (nth 3 edges) (nth 1 edges))))
207 (image-next-line
208 (max 0 (- win-height next-screen-context-lines)))))
209 ((eq n '-)
210 (let* ((edges (window-inside-edges))
211 (win-height (- (nth 3 edges) (nth 1 edges))))
212 (image-next-line
213 (min 0 (- next-screen-context-lines win-height)))))
214 (t (image-next-line (prefix-numeric-value n)))))
216 (defun image-scroll-down (&optional n)
217 "Scroll image in current window downward by N lines.
218 Stop if the top edge of the image is reached.
219 If ARG is omitted or nil, scroll downward by a near full screen.
220 A near full screen is `next-screen-context-lines' less than a full screen.
221 Negative ARG means scroll upward.
222 If ARG is the atom `-', scroll upward by nearly full screen.
223 When calling from a program, supply as argument a number, nil, or `-'."
224 (interactive "P")
225 (cond ((null n)
226 (let* ((edges (window-inside-edges))
227 (win-height (- (nth 3 edges) (nth 1 edges))))
228 (image-next-line
229 (min 0 (- next-screen-context-lines win-height)))))
230 ((eq n '-)
231 (let* ((edges (window-inside-edges))
232 (win-height (- (nth 3 edges) (nth 1 edges))))
233 (image-next-line
234 (max 0 (- win-height next-screen-context-lines)))))
235 (t (image-next-line (- (prefix-numeric-value n))))))
237 (defun image-bol (arg)
238 "Scroll horizontally to the left edge of the image in the current window.
239 With argument ARG not nil or 1, move forward ARG - 1 lines first,
240 stopping if the top or bottom edge of the image is reached."
241 (interactive "p")
242 (and arg
243 (/= (setq arg (prefix-numeric-value arg)) 1)
244 (image-next-line (- arg 1)))
245 (image-set-window-hscroll 0))
247 (defun image-eol (arg)
248 "Scroll horizontally to the right edge of the image in the current window.
249 With argument ARG not nil or 1, move forward ARG - 1 lines first,
250 stopping if the top or bottom edge of the image is reached."
251 (interactive "p")
252 (and arg
253 (/= (setq arg (prefix-numeric-value arg)) 1)
254 (image-next-line (- arg 1)))
255 (let* ((image (image-get-display-property))
256 (edges (window-inside-edges))
257 (win-width (- (nth 2 edges) (nth 0 edges)))
258 (img-width (ceiling (car (image-display-size image)))))
259 (image-set-window-hscroll (max 0 (- img-width win-width)))))
261 (defun image-bob ()
262 "Scroll to the top-left corner of the image in the current window."
263 (interactive)
264 (image-set-window-hscroll 0)
265 (image-set-window-vscroll 0))
267 (defun image-eob ()
268 "Scroll to the bottom-right corner of the image in the current window."
269 (interactive)
270 (let* ((image (image-get-display-property))
271 (edges (window-inside-edges))
272 (win-width (- (nth 2 edges) (nth 0 edges)))
273 (img-width (ceiling (car (image-display-size image))))
274 (win-height (- (nth 3 edges) (nth 1 edges)))
275 (img-height (ceiling (cdr (image-display-size image)))))
276 (image-set-window-hscroll (max 0 (- img-width win-width)))
277 (image-set-window-vscroll (max 0 (- img-height win-height)))))
279 ;; Adjust frame and image size.
281 (defun image-mode-fit-frame ()
282 "Toggle whether to fit the frame to the current image.
283 This function assumes the current frame has only one window."
284 ;; FIXME: This does not take into account decorations like mode-line,
285 ;; minibuffer, header-line, ...
286 (interactive)
287 (let* ((saved (frame-parameter nil 'image-mode-saved-size))
288 (display (image-get-display-property))
289 (size (image-display-size display)))
290 (if (and saved
291 (eq (caar saved) (frame-width))
292 (eq (cdar saved) (frame-height)))
293 (progn ;; Toggle back to previous non-fitted size.
294 (set-frame-parameter nil 'image-mode-saved-size nil)
295 (setq size (cdr saved)))
296 ;; Round up size, and save current size so we can toggle back to it.
297 (setcar size (ceiling (car size)))
298 (setcdr size (ceiling (cdr size)))
299 (set-frame-parameter nil 'image-mode-saved-size
300 (cons size (cons (frame-width) (frame-height)))))
301 (set-frame-width (selected-frame) (car size))
302 (set-frame-height (selected-frame) (cdr size))))
304 ;;; Image Mode setup
306 (defvar image-type nil
307 "The image type for the current Image mode buffer.")
308 (make-variable-buffer-local 'image-type)
310 (defvar image-mode-previous-major-mode nil
311 "Internal variable to keep the previous non-image major mode.")
313 (defvar image-mode-map
314 (let ((map (make-sparse-keymap)))
315 (set-keymap-parent map special-mode-map)
316 (define-key map "\C-c\C-c" 'image-toggle-display)
317 (define-key map (kbd "SPC") 'image-scroll-up)
318 (define-key map (kbd "DEL") 'image-scroll-down)
319 (define-key map (kbd "RET") 'image-toggle-animation)
320 (define-key map [remap forward-char] 'image-forward-hscroll)
321 (define-key map [remap backward-char] 'image-backward-hscroll)
322 (define-key map [remap right-char] 'image-forward-hscroll)
323 (define-key map [remap left-char] 'image-backward-hscroll)
324 (define-key map [remap previous-line] 'image-previous-line)
325 (define-key map [remap next-line] 'image-next-line)
326 (define-key map [remap scroll-up] 'image-scroll-up)
327 (define-key map [remap scroll-down] 'image-scroll-down)
328 (define-key map [remap scroll-up-command] 'image-scroll-up)
329 (define-key map [remap scroll-down-command] 'image-scroll-down)
330 (define-key map [remap move-beginning-of-line] 'image-bol)
331 (define-key map [remap move-end-of-line] 'image-eol)
332 (define-key map [remap beginning-of-buffer] 'image-bob)
333 (define-key map [remap end-of-buffer] 'image-eob)
334 map)
335 "Mode keymap for `image-mode'.")
337 (defvar image-minor-mode-map
338 (let ((map (make-sparse-keymap)))
339 (define-key map "\C-c\C-c" 'image-toggle-display)
340 map)
341 "Mode keymap for `image-minor-mode'.")
343 (defvar bookmark-make-record-function)
345 (put 'image-mode 'mode-class 'special)
347 ;;;###autoload
348 (defun image-mode ()
349 "Major mode for image files.
350 You can use \\<image-mode-map>\\[image-toggle-display]
351 to toggle between display as an image and display as text."
352 (interactive)
353 (condition-case err
354 (progn
355 (unless (display-images-p)
356 (error "Display does not support images"))
358 (kill-all-local-variables)
359 (setq major-mode 'image-mode)
361 (if (not (image-get-display-property))
362 (progn
363 (image-toggle-display-image)
364 ;; If attempt to display the image fails.
365 (if (not (image-get-display-property))
366 (error "Invalid image")))
367 ;; Set next vars when image is already displayed but local
368 ;; variables were cleared by kill-all-local-variables
369 (setq cursor-type nil truncate-lines t
370 image-type (plist-get (cdr (image-get-display-property)) :type)))
372 (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
373 (use-local-map image-mode-map)
375 ;; Use our own bookmarking function for images.
376 (set (make-local-variable 'bookmark-make-record-function)
377 'image-bookmark-make-record)
379 ;; Keep track of [vh]scroll when switching buffers
380 (image-mode-setup-winprops)
382 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
383 (add-hook 'after-revert-hook 'image-after-revert-hook nil t)
384 (run-mode-hooks 'image-mode-hook)
385 (let ((image (image-get-display-property))
386 (msg1 (substitute-command-keys
387 "Type \\[image-toggle-display] to view the image as ")))
388 (cond
389 ((null image)
390 (message "%s" (concat msg1 "an image.")))
391 ((image-animated-p image)
392 (message "%s"
393 (concat msg1 "text, or "
394 (substitute-command-keys
395 "\\[image-toggle-animation] to animate."))))
397 (message "%s" (concat msg1 "text."))))))
399 (error
400 (image-mode-as-text)
401 (funcall
402 (if (called-interactively-p 'any) 'error 'message)
403 "Cannot display image: %s" (cdr err)))))
405 ;;;###autoload
406 (define-minor-mode image-minor-mode
407 "Toggle Image minor mode in this buffer.
408 With a prefix argument ARG, enable Image minor mode if ARG is
409 positive, and disable it otherwise. If called from Lisp, enable
410 the mode if ARG is omitted or nil.
412 Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
413 to switch back to `image-mode' and display an image file as the
414 actual image."
415 nil (:eval (if image-type (format " Image[%s]" image-type) " Image"))
416 image-minor-mode-map
417 :group 'image
418 :version "22.1"
419 (if image-minor-mode
420 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
422 ;;;###autoload
423 (defun image-mode-as-text ()
424 "Set a non-image mode as major mode in combination with image minor mode.
425 A non-image major mode found from `auto-mode-alist' or Fundamental mode
426 displays an image file as text. `image-minor-mode' provides the key
427 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
428 to display an image file as the actual image.
430 You can use `image-mode-as-text' in `auto-mode-alist' when you want
431 to display an image file as text initially.
433 See commands `image-mode' and `image-minor-mode' for more information
434 on these modes."
435 (interactive)
436 ;; image-mode-as-text = normal-mode + image-minor-mode
437 (let ((previous-image-type image-type)) ; preserve `image-type'
438 (if image-mode-previous-major-mode
439 ;; Restore previous major mode that was already found by this
440 ;; function and cached in `image-mode-previous-major-mode'
441 (funcall image-mode-previous-major-mode)
442 (let ((auto-mode-alist
443 (delq nil (mapcar
444 (lambda (elt)
445 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
446 '(image-mode image-mode-maybe image-mode-as-text))
447 elt))
448 auto-mode-alist)))
449 (magic-fallback-mode-alist
450 (delq nil (mapcar
451 (lambda (elt)
452 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
453 '(image-mode image-mode-maybe image-mode-as-text))
454 elt))
455 magic-fallback-mode-alist))))
456 (normal-mode)
457 (set (make-local-variable 'image-mode-previous-major-mode) major-mode)))
458 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
459 (setq image-type previous-image-type)
460 ;; Enable image minor mode with `C-c C-c'.
461 (image-minor-mode 1)
462 ;; Show the image file as text.
463 (image-toggle-display-text)
464 (message "%s" (concat
465 (substitute-command-keys
466 "Type \\[image-toggle-display] to view the image as ")
467 (if (image-get-display-property)
468 "text" "an image") "."))))
470 (define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
472 (defun image-toggle-display-text ()
473 "Show the image file as text.
474 Remove text properties that display the image."
475 (let ((inhibit-read-only t)
476 (buffer-undo-list t)
477 (modified (buffer-modified-p)))
478 (remove-list-of-text-properties (point-min) (point-max)
479 '(display read-nonsticky ;; intangible
480 read-only front-sticky))
481 (set-buffer-modified-p modified)
482 (if (called-interactively-p 'any)
483 (message "Repeat this command to go back to displaying the image"))))
485 (defvar archive-superior-buffer)
486 (defvar tar-superior-buffer)
487 (declare-function image-flush "image.c" (spec &optional frame))
489 (defun image-toggle-display-image ()
490 "Show the image of the image file.
491 Turn the image data into a real image, but only if the whole file
492 was inserted."
493 (unless (derived-mode-p 'image-mode)
494 (error "The buffer is not in Image mode"))
495 (let* ((filename (buffer-file-name))
496 (data-p (not (and filename
497 (file-readable-p filename)
498 (not (file-remote-p filename))
499 (not (buffer-modified-p))
500 (not (and (boundp 'archive-superior-buffer)
501 archive-superior-buffer))
502 (not (and (boundp 'tar-superior-buffer)
503 tar-superior-buffer)))))
504 (file-or-data (if data-p
505 (string-make-unibyte
506 (buffer-substring-no-properties (point-min) (point-max)))
507 filename))
508 (type (image-type file-or-data nil data-p))
509 (image (create-image file-or-data type data-p))
510 (inhibit-read-only t)
511 (buffer-undo-list t)
512 (modified (buffer-modified-p))
513 props)
515 ;; Discard any stale image data before looking it up again.
516 (image-flush image)
517 (setq image (append image (image-transform-properties image)))
518 (setq props
519 `(display ,image
520 ;; intangible ,image
521 rear-nonsticky (display) ;; intangible
522 read-only t front-sticky (read-only)))
524 (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file
525 (add-text-properties (point-min) (point-max) props)
526 (restore-buffer-modified-p modified))
527 ;; Inhibit the cursor when the buffer contains only an image,
528 ;; because cursors look very strange on top of images.
529 (setq cursor-type nil)
530 ;; This just makes the arrow displayed in the right fringe
531 ;; area look correct when the image is wider than the window.
532 (setq truncate-lines t)
533 ;; Disable adding a newline at the end of the image file when it
534 ;; is written with, e.g., C-x C-w.
535 (if (coding-system-equal (coding-system-base buffer-file-coding-system)
536 'no-conversion)
537 (set (make-local-variable 'find-file-literally) t))
538 ;; Allow navigation of large images
539 (set (make-local-variable 'auto-hscroll-mode) nil)
540 (setq image-type type)
541 (if (eq major-mode 'image-mode)
542 (setq mode-name (format "Image[%s]" type)))
543 (image-transform-check-size)
544 (if (called-interactively-p 'any)
545 (message "Repeat this command to go back to displaying the file as text"))))
547 (defun image-toggle-display ()
548 "Toggle between image and text display.
549 If the current buffer is displaying an image file as an image,
550 call `image-mode-as-text' to switch to text. Otherwise, display
551 the image by calling `image-mode'."
552 (interactive)
553 (if (image-get-display-property)
554 (image-mode-as-text)
555 (image-mode)))
557 (defun image-after-revert-hook ()
558 (when (image-get-display-property)
559 (image-toggle-display-text)
560 ;; Update image display.
561 (mapc (lambda (window) (redraw-frame (window-frame window)))
562 (get-buffer-window-list (current-buffer) 'nomini 'visible))
563 (image-toggle-display-image)))
566 ;;; Animated images
568 (defcustom image-animate-loop nil
569 "Non-nil means animated images loop forever, rather than playing once."
570 :type 'boolean
571 :version "24.1"
572 :group 'image)
574 (defun image-toggle-animation ()
575 "Start or stop animating the current image.
576 If `image-animate-loop' is non-nil, animation loops forever.
577 Otherwise it plays once, then stops."
578 (interactive)
579 (let ((image (image-get-display-property))
580 animation)
581 (cond
582 ((null image)
583 (error "No image is present"))
584 ((null (setq animation (image-animated-p image)))
585 (message "No image animation."))
587 (let ((timer (image-animate-timer image)))
588 (if timer
589 (cancel-timer timer)
590 (let ((index (plist-get (cdr image) :index)))
591 ;; If we're at the end, restart.
592 (and index
593 (>= index (1- (car animation)))
594 (setq index nil))
595 (image-animate image index
596 (if image-animate-loop t)))))))))
599 ;;; Support for bookmark.el
600 (declare-function bookmark-make-record-default
601 "bookmark" (&optional no-file no-context posn))
602 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
603 (declare-function bookmark-default-handler "bookmark" (bmk))
605 (defun image-bookmark-make-record ()
606 `(,@(bookmark-make-record-default nil 'no-context 0)
607 (image-type . ,image-type)
608 (handler . image-bookmark-jump)))
610 ;;;###autoload
611 (defun image-bookmark-jump (bmk)
612 ;; This implements the `handler' function interface for record type
613 ;; returned by `bookmark-make-record-function', which see.
614 (prog1 (bookmark-default-handler bmk)
615 (when (not (string= image-type (bookmark-prop-get bmk 'image-type)))
616 (image-toggle-display))))
619 ;; Not yet implemented.
620 ;; (defvar image-transform-minor-mode-map
621 ;; (let ((map (make-sparse-keymap)))
622 ;; ;; (define-key map [(control ?+)] 'image-scale-in)
623 ;; ;; (define-key map [(control ?-)] 'image-scale-out)
624 ;; ;; (define-key map [(control ?=)] 'image-scale-none)
625 ;; ;; (define-key map "c f h" 'image-scale-fit-height)
626 ;; ;; (define-key map "c ]" 'image-rotate-right)
627 ;; map)
628 ;; "Minor mode keymap `image-transform-mode'.")
630 ;; (define-minor-mode image-transform-mode
631 ;; "Minor mode for scaling and rotating images.
632 ;; With a prefix argument ARG, enable the mode if ARG is positive,
633 ;; and disable it otherwise. If called from Lisp, enable the mode
634 ;; if ARG is omitted or nil. This minor mode requires Emacs to have
635 ;; been compiled with ImageMagick support."
636 ;; nil "image-transform" image-transform-minor-mode-map)
639 ;; FIXME this doesn't seem mature yet. Document in manual when it is.
640 (defvar image-transform-resize nil
641 "The image resize operation.
642 Its value should be one of the following:
643 - nil, meaning no resizing.
644 - `fit-height', meaning to fit the image to the window height.
645 - `fit-width', meaning to fit the image to the window width.
646 - A number, which is a scale factor (the default size is 1).")
648 (defvar image-transform-scale 1.0
649 "The scale factor of the image being displayed.")
651 (defvar image-transform-rotation 0.0
652 "Rotation angle for the image in the current Image mode buffer.")
654 (defvar image-transform-right-angle-fudge 0.0001
655 "Snap distance to a multiple of a right angle.
656 There's no deep theory behind the default value, it should just
657 be somewhat larger than ImageMagick's MagickEpsilon.")
659 (defsubst image-transform-width (width height)
660 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
661 The rotation angle is the value of `image-transform-rotation' in degrees."
662 (let ((angle (degrees-to-radians image-transform-rotation)))
663 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
664 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
665 ;; rotation by the angle A. The projections onto the first axis
666 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
667 ;; (h/2) sin A, and the difference between the largest and the
668 ;; smallest of the four values is the expression below.
669 (+ (* width (abs (cos angle))) (* height (abs (sin angle))))))
671 ;; The following comment and code snippet are from
672 ;; ImageMagick-6.7.4-4/magick/distort.c
674 ;; /* Set the output image geometry to calculated 'best fit'.
675 ;; Yes this tends to 'over do' the file image size, ON PURPOSE!
676 ;; Do not do this for DePolar which needs to be exact for virtual tiling.
677 ;; */
678 ;; if ( fix_bounds ) {
679 ;; geometry.x = (ssize_t) floor(min.x-0.5);
680 ;; geometry.y = (ssize_t) floor(min.y-0.5);
681 ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
682 ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
683 ;; }
685 ;; Other parts of the same file show that here the origin is in the
686 ;; left lower corner of the image rectangle, the center of the
687 ;; rotation is the center of the rectangle and min.x and max.x
688 ;; (resp. min.y and max.y) are the smallest and the largest of the
689 ;; projections of the vertices onto the first (resp. second) axis.
691 (defun image-transform-fit-width (width height length)
692 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
693 The rotation angle is the value of `image-transform-rotation'.
694 Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
695 an \"approximately uniformly\" scaled W x H rectangle, which
696 currently means that w is one of floor(s W) + {0, 1, -1} and h is
697 floor(s H), where s can be recovered as the value of `image-transform-scale'.
698 The value of `image-transform-rotation' may be replaced by
699 a slightly different angle. Currently this is done for values
700 close to a multiple of 90, see `image-transform-right-angle-fudge'."
701 (cond ((< (abs (- (mod (+ image-transform-rotation 90) 180) 90))
702 image-transform-right-angle-fudge)
703 (cl-assert (not (zerop width)) t)
704 (setq image-transform-rotation
705 (float (round image-transform-rotation))
706 image-transform-scale (/ (float length) width))
707 (cons length nil))
708 ((< (abs (- (mod (+ image-transform-rotation 45) 90) 45))
709 image-transform-right-angle-fudge)
710 (cl-assert (not (zerop height)) t)
711 (setq image-transform-rotation
712 (float (round image-transform-rotation))
713 image-transform-scale (/ (float length) height))
714 (cons nil length))
716 (cl-assert (not (and (zerop width) (zerop height))) t)
717 (setq image-transform-scale
718 (/ (float (1- length)) (image-transform-width width height)))
719 ;; Assume we have a w x h image and an angle A, and let l =
720 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
721 ;; of the bounding box of the rotated image, as calculated by
722 ;; `image-transform-width'. The code snippet quoted above
723 ;; means that ImageMagick puts the rotated image in
724 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
725 ;; Elementary considerations show that this is equivalent to
726 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
727 ;; the given `length' parameter and our job is to determine
728 ;; reasonable values for w and h which satisfy these
729 ;; conditions.
730 (let ((w (floor (* image-transform-scale width)))
731 (h (floor (* image-transform-scale height))))
732 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
733 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
734 ;; hence l(w, h) > (L-1) - 2 = L-3.
735 (cons
736 (cond ((= (mod w 2) (mod length 2))
738 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
739 ;; L-1 hold?
740 ((<= (image-transform-width (1+ w) h) (1- length))
741 (1+ w))
742 ;; No, it doesn't, but this implies that l(w-1, h) =
743 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
744 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
746 (1- w)))
747 h)))))
749 (defun image-transform-check-size ()
750 "Check that the image exactly fits the width/height of the window.
752 Do this for an image of type `imagemagick' to make sure that the
753 elisp code matches the way ImageMagick computes the bounding box
754 of a rotated image."
755 (when (and (not (numberp image-transform-resize))
756 (boundp 'image-type)
757 (eq image-type 'imagemagick))
758 (let ((size (image-display-size (image-get-display-property) t)))
759 (cond ((eq image-transform-resize 'fit-width)
760 (cl-assert (= (car size)
761 (- (nth 2 (window-inside-pixel-edges))
762 (nth 0 (window-inside-pixel-edges))))
764 ((eq image-transform-resize 'fit-height)
765 (cl-assert (= (cdr size)
766 (- (nth 3 (window-inside-pixel-edges))
767 (nth 1 (window-inside-pixel-edges))))
768 t))))))
770 (defun image-transform-properties (spec)
771 "Return rescaling/rotation properties for image SPEC.
772 These properties are determined by the Image mode variables
773 `image-transform-resize' and `image-transform-rotation'. The
774 return value is suitable for appending to an image spec.
776 Rescaling and rotation properties only take effect if Emacs is
777 compiled with ImageMagick support."
778 (setq image-transform-scale 1.0)
779 (when (or image-transform-resize
780 (/= image-transform-rotation 0.0))
781 ;; Note: `image-size' looks up and thus caches the untransformed
782 ;; image. There's no easy way to prevent that.
783 (let* ((size (image-size spec t))
784 (resized
785 (cond
786 ((numberp image-transform-resize)
787 (unless (= image-transform-resize 1)
788 (setq image-transform-scale image-transform-resize)
789 (cons nil (floor (* image-transform-resize (cdr size))))))
790 ((eq image-transform-resize 'fit-width)
791 (image-transform-fit-width
792 (car size) (cdr size)
793 (- (nth 2 (window-inside-pixel-edges))
794 (nth 0 (window-inside-pixel-edges)))))
795 ((eq image-transform-resize 'fit-height)
796 (let ((res (image-transform-fit-width
797 (cdr size) (car size)
798 (- (nth 3 (window-inside-pixel-edges))
799 (nth 1 (window-inside-pixel-edges))))))
800 (cons (cdr res) (car res)))))))
801 `(,@(when (car resized)
802 (list :width (car resized)))
803 ,@(when (cdr resized)
804 (list :height (cdr resized)))
805 ,@(unless (= 0.0 image-transform-rotation)
806 (list :rotation image-transform-rotation))))))
808 (defun image-transform-set-scale (scale)
809 "Prompt for a number, and resize the current image by that amount.
810 This command has no effect unless Emacs is compiled with
811 ImageMagick support."
812 (interactive "nScale: ")
813 (setq image-transform-resize scale)
814 (image-toggle-display-image))
816 (defun image-transform-fit-to-height ()
817 "Fit the current image to the height of the current window.
818 This command has no effect unless Emacs is compiled with
819 ImageMagick support."
820 (interactive)
821 (setq image-transform-resize 'fit-height)
822 (image-toggle-display-image))
824 (defun image-transform-fit-to-width ()
825 "Fit the current image to the width of the current window.
826 This command has no effect unless Emacs is compiled with
827 ImageMagick support."
828 (interactive)
829 (setq image-transform-resize 'fit-width)
830 (image-toggle-display-image))
832 (defun image-transform-set-rotation (rotation)
833 "Prompt for an angle ROTATION, and rotate the image by that amount.
834 ROTATION should be in degrees. This command has no effect unless
835 Emacs is compiled with ImageMagick support."
836 (interactive "nRotation angle (in degrees): ")
837 (setq image-transform-rotation (float (mod rotation 360)))
838 (image-toggle-display-image))
840 (provide 'image-mode)
842 ;;; image-mode.el ends here