]> code.delx.au - gnu-emacs/blob - lisp/image-mode.el
Merge from emacs-23; up to 2010-06-09T17:54:28Z!albinus@detlef.
[gnu-emacs] / lisp / image-mode.el
1 ;;; image-mode.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2005-2011 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Richard Stallman <rms@gnu.org>
6 ;; Keywords: multimedia
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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.
33
34 ;;; Code:
35
36 (require 'image)
37 (eval-when-compile (require 'cl))
38
39 ;;; Image mode window-info management.
40
41 (defvar image-mode-winprops-alist t)
42 (make-variable-buffer-local 'image-mode-winprops-alist)
43
44 (defvar image-mode-new-window-functions nil
45 "Special hook run when image data is requested in a new window.
46 It is called with one argument, the initial WINPROPS.")
47
48 (defun image-mode-winprops (&optional window cleanup)
49 "Return winprops of WINDOW.
50 A winprops object has the shape (WINDOW . ALIST)."
51 (cond ((null window)
52 (setq window (selected-window)))
53 ((not (windowp window))
54 (error "Not a window: %s" window)))
55 (when cleanup
56 (setq image-mode-winprops-alist
57 (delq nil (mapcar (lambda (winprop)
58 (if (window-live-p (car-safe winprop))
59 winprop))
60 image-mode-winprops-alist))))
61 (let ((winprops (assq window image-mode-winprops-alist)))
62 ;; For new windows, set defaults from the latest.
63 (unless winprops
64 (setq winprops (cons window
65 (copy-alist (cdar image-mode-winprops-alist))))
66 (run-hook-with-args 'image-mode-new-window-functions winprops))
67 ;; Move window to front.
68 (setq image-mode-winprops-alist
69 (cons winprops (delq winprops image-mode-winprops-alist)))
70 winprops))
71
72 (defun image-mode-window-get (prop &optional winprops)
73 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
74 (cdr (assq prop (cdr winprops))))
75
76 (defsetf image-mode-window-get (prop &optional winprops) (val)
77 `(image-mode-window-put ,prop ,val ,winprops))
78
79 (defun image-mode-window-put (prop val &optional winprops)
80 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
81 (setcdr winprops (cons (cons prop val)
82 (delq (assq prop (cdr winprops)) (cdr winprops)))))
83
84 (defun image-set-window-vscroll (vscroll)
85 (setf (image-mode-window-get 'vscroll) vscroll)
86 (set-window-vscroll (selected-window) vscroll))
87
88 (defun image-set-window-hscroll (ncol)
89 (setf (image-mode-window-get 'hscroll) ncol)
90 (set-window-hscroll (selected-window) ncol))
91
92 (defun image-mode-reapply-winprops ()
93 ;; When set-window-buffer, set hscroll and vscroll to what they were
94 ;; last time the image was displayed in this window.
95 (when (and (image-get-display-property)
96 (listp image-mode-winprops-alist))
97 (let* ((winprops (image-mode-winprops nil t))
98 (hscroll (image-mode-window-get 'hscroll winprops))
99 (vscroll (image-mode-window-get 'vscroll winprops)))
100 (if hscroll (set-window-hscroll (selected-window) hscroll))
101 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
102
103 (defun image-mode-setup-winprops ()
104 ;; Record current scroll settings.
105 (unless (listp image-mode-winprops-alist)
106 (setq image-mode-winprops-alist nil))
107 (add-hook 'window-configuration-change-hook
108 'image-mode-reapply-winprops nil t))
109
110 ;;; Image scrolling functions
111
112 (defun image-get-display-property ()
113 (get-char-property (point-min) 'display
114 ;; There might be different images for different displays.
115 (if (eq (window-buffer) (current-buffer))
116 (selected-window))))
117
118 (declare-function image-size "image.c" (spec &optional pixels frame))
119
120 (defun image-display-size (spec &optional pixels frame)
121 "Wrapper around `image-size', handling slice display properties.
122 Like `image-size', the return value is (WIDTH . HEIGHT).
123 WIDTH and HEIGHT are in canonical character units if PIXELS is
124 nil, and in pixel units if PIXELS is non-nil.
125
126 If SPEC is an image display property, this function is equivalent
127 to `image-size'. If SPEC is a list of properties containing
128 `image' and `slice' properties, return the display size taking
129 the slice property into account. If the list contains `image'
130 but not `slice', return the `image-size' of the specified image."
131 (if (eq (car spec) 'image)
132 (image-size spec pixels frame)
133 (let ((image (assoc 'image spec))
134 (slice (assoc 'slice spec)))
135 (cond ((and image slice)
136 (if pixels
137 (cons (nth 3 slice) (nth 4 slice))
138 (cons (/ (float (nth 3 slice)) (frame-char-width frame))
139 (/ (float (nth 4 slice)) (frame-char-height frame)))))
140 (image
141 (image-size image pixels frame))
142 (t
143 (error "Invalid image specification: %s" spec))))))
144
145 (defun image-forward-hscroll (&optional n)
146 "Scroll image in current window to the left by N character widths.
147 Stop if the right edge of the image is reached."
148 (interactive "p")
149 (cond ((= n 0) nil)
150 ((< n 0)
151 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
152 (t
153 (let* ((image (image-get-display-property))
154 (edges (window-inside-edges))
155 (win-width (- (nth 2 edges) (nth 0 edges)))
156 (img-width (ceiling (car (image-display-size image)))))
157 (image-set-window-hscroll (min (max 0 (- img-width win-width))
158 (+ n (window-hscroll))))))))
159
160 (defun image-backward-hscroll (&optional n)
161 "Scroll image in current window to the right by N character widths.
162 Stop if the left edge of the image is reached."
163 (interactive "p")
164 (image-forward-hscroll (- n)))
165
166 (defun image-next-line (&optional n)
167 "Scroll image in current window upward by N lines.
168 Stop if the bottom edge of the image is reached."
169 (interactive "p")
170 (cond ((= n 0) nil)
171 ((< n 0)
172 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
173 (t
174 (let* ((image (image-get-display-property))
175 (edges (window-inside-edges))
176 (win-height (- (nth 3 edges) (nth 1 edges)))
177 (img-height (ceiling (cdr (image-display-size image)))))
178 (image-set-window-vscroll (min (max 0 (- img-height win-height))
179 (+ n (window-vscroll))))))))
180
181 (defun image-previous-line (&optional n)
182 "Scroll image in current window downward by N lines.
183 Stop if the top edge of the image is reached."
184 (interactive "p")
185 (image-next-line (- n)))
186
187 (defun image-scroll-up (&optional n)
188 "Scroll image in current window upward by N lines.
189 Stop if the bottom edge of the image is reached.
190 If ARG is omitted or nil, scroll upward by a near full screen.
191 A near full screen is `next-screen-context-lines' less than a full screen.
192 Negative ARG means scroll downward.
193 If ARG is the atom `-', scroll downward by nearly full screen.
194 When calling from a program, supply as argument a number, nil, or `-'."
195 (interactive "P")
196 (cond ((null n)
197 (let* ((edges (window-inside-edges))
198 (win-height (- (nth 3 edges) (nth 1 edges))))
199 (image-next-line
200 (max 0 (- win-height next-screen-context-lines)))))
201 ((eq n '-)
202 (let* ((edges (window-inside-edges))
203 (win-height (- (nth 3 edges) (nth 1 edges))))
204 (image-next-line
205 (min 0 (- next-screen-context-lines win-height)))))
206 (t (image-next-line (prefix-numeric-value n)))))
207
208 (defun image-scroll-down (&optional n)
209 "Scroll image in current window downward by N lines.
210 Stop if the top edge of the image is reached.
211 If ARG is omitted or nil, scroll downward by a near full screen.
212 A near full screen is `next-screen-context-lines' less than a full screen.
213 Negative ARG means scroll upward.
214 If ARG is the atom `-', scroll upward by nearly full screen.
215 When calling from a program, supply as argument a number, nil, or `-'."
216 (interactive "P")
217 (cond ((null n)
218 (let* ((edges (window-inside-edges))
219 (win-height (- (nth 3 edges) (nth 1 edges))))
220 (image-next-line
221 (min 0 (- next-screen-context-lines win-height)))))
222 ((eq n '-)
223 (let* ((edges (window-inside-edges))
224 (win-height (- (nth 3 edges) (nth 1 edges))))
225 (image-next-line
226 (max 0 (- win-height next-screen-context-lines)))))
227 (t (image-next-line (- (prefix-numeric-value n))))))
228
229 (defun image-bol (arg)
230 "Scroll horizontally to the left edge of the image in the current window.
231 With argument ARG not nil or 1, move forward ARG - 1 lines first,
232 stopping if the top or bottom edge of the image is reached."
233 (interactive "p")
234 (and arg
235 (/= (setq arg (prefix-numeric-value arg)) 1)
236 (image-next-line (- arg 1)))
237 (image-set-window-hscroll 0))
238
239 (defun image-eol (arg)
240 "Scroll horizontally to the right edge of the image in the current window.
241 With argument ARG not nil or 1, move forward ARG - 1 lines first,
242 stopping if the top or bottom edge of the image is reached."
243 (interactive "p")
244 (and arg
245 (/= (setq arg (prefix-numeric-value arg)) 1)
246 (image-next-line (- arg 1)))
247 (let* ((image (image-get-display-property))
248 (edges (window-inside-edges))
249 (win-width (- (nth 2 edges) (nth 0 edges)))
250 (img-width (ceiling (car (image-display-size image)))))
251 (image-set-window-hscroll (max 0 (- img-width win-width)))))
252
253 (defun image-bob ()
254 "Scroll to the top-left corner of the image in the current window."
255 (interactive)
256 (image-set-window-hscroll 0)
257 (image-set-window-vscroll 0))
258
259 (defun image-eob ()
260 "Scroll to the bottom-right corner of the image in the current window."
261 (interactive)
262 (let* ((image (image-get-display-property))
263 (edges (window-inside-edges))
264 (win-width (- (nth 2 edges) (nth 0 edges)))
265 (img-width (ceiling (car (image-display-size image))))
266 (win-height (- (nth 3 edges) (nth 1 edges)))
267 (img-height (ceiling (cdr (image-display-size image)))))
268 (image-set-window-hscroll (max 0 (- img-width win-width)))
269 (image-set-window-vscroll (max 0 (- img-height win-height)))))
270
271 ;; Adjust frame and image size.
272
273 (defun image-mode-fit-frame ()
274 "Fit the frame to the current image.
275 This function assumes the current frame has only one window."
276 ;; FIXME: This does not take into account decorations like mode-line,
277 ;; minibuffer, header-line, ...
278 (interactive)
279 (let* ((saved (frame-parameter nil 'image-mode-saved-size))
280 (display (image-get-display-property))
281 (size (image-display-size display)))
282 (if (and saved
283 (eq (caar saved) (frame-width))
284 (eq (cdar saved) (frame-height)))
285 (progn ;; Toggle back to previous non-fitted size.
286 (set-frame-parameter nil 'image-mode-saved-size nil)
287 (setq size (cdr saved)))
288 ;; Round up size, and save current size so we can toggle back to it.
289 (setcar size (ceiling (car size)))
290 (setcdr size (ceiling (cdr size)))
291 (set-frame-parameter nil 'image-mode-saved-size
292 (cons size (cons (frame-width) (frame-height)))))
293 (set-frame-width (selected-frame) (car size))
294 (set-frame-height (selected-frame) (cdr size))))
295
296 ;;; Image Mode setup
297
298 (defvar image-type nil
299 "The image type for the current Image mode buffer.")
300 (make-variable-buffer-local 'image-type)
301
302 (defvar image-mode-previous-major-mode nil
303 "Internal variable to keep the previous non-image major mode.")
304
305 (defvar image-mode-map
306 (let ((map (make-sparse-keymap)))
307 (set-keymap-parent map special-mode-map)
308 (define-key map "\C-c\C-c" 'image-toggle-display)
309 (define-key map (kbd "SPC") 'image-scroll-up)
310 (define-key map (kbd "DEL") 'image-scroll-down)
311 (define-key map [remap forward-char] 'image-forward-hscroll)
312 (define-key map [remap backward-char] 'image-backward-hscroll)
313 (define-key map [remap right-char] 'image-forward-hscroll)
314 (define-key map [remap left-char] 'image-backward-hscroll)
315 (define-key map [remap previous-line] 'image-previous-line)
316 (define-key map [remap next-line] 'image-next-line)
317 (define-key map [remap scroll-up] 'image-scroll-up)
318 (define-key map [remap scroll-down] 'image-scroll-down)
319 (define-key map [remap scroll-up-command] 'image-scroll-up)
320 (define-key map [remap scroll-down-command] 'image-scroll-down)
321 (define-key map [remap move-beginning-of-line] 'image-bol)
322 (define-key map [remap move-end-of-line] 'image-eol)
323 (define-key map [remap beginning-of-buffer] 'image-bob)
324 (define-key map [remap end-of-buffer] 'image-eob)
325 map)
326 "Mode keymap for `image-mode'.")
327
328 (defvar image-minor-mode-map
329 (let ((map (make-sparse-keymap)))
330 (define-key map "\C-c\C-c" 'image-toggle-display)
331 map)
332 "Mode keymap for `image-minor-mode'.")
333
334 (defvar bookmark-make-record-function)
335
336 (put 'image-mode 'mode-class 'special)
337
338 ;;;###autoload
339 (defun image-mode ()
340 "Major mode for image files.
341 You can use \\<image-mode-map>\\[image-toggle-display]
342 to toggle between display as an image and display as text."
343 (interactive)
344 (condition-case err
345 (progn
346 (unless (display-images-p)
347 (error "Display does not support images"))
348
349 (kill-all-local-variables)
350 (setq major-mode 'image-mode)
351
352 (if (not (image-get-display-property))
353 (progn
354 (image-toggle-display-image)
355 ;; If attempt to display the image fails.
356 (if (not (image-get-display-property))
357 (error "Invalid image")))
358 ;; Set next vars when image is already displayed but local
359 ;; variables were cleared by kill-all-local-variables
360 (setq cursor-type nil truncate-lines t
361 image-type (plist-get (cdr (image-get-display-property)) :type)))
362
363 (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
364 (use-local-map image-mode-map)
365
366 ;; Use our own bookmarking function for images.
367 (set (make-local-variable 'bookmark-make-record-function)
368 'image-bookmark-make-record)
369
370 ;; Keep track of [vh]scroll when switching buffers
371 (image-mode-setup-winprops)
372
373 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
374 (add-hook 'after-revert-hook 'image-after-revert-hook nil t)
375 (run-mode-hooks 'image-mode-hook)
376 (message "%s" (concat
377 (substitute-command-keys
378 "Type \\[image-toggle-display] to view the image as ")
379 (if (image-get-display-property)
380 "text" "an image") ".")))
381 (error
382 (image-mode-as-text)
383 (funcall
384 (if (called-interactively-p 'any) 'error 'message)
385 "Cannot display image: %s" (cdr err)))))
386 ;;;###autoload
387 (define-minor-mode image-minor-mode
388 "Toggle Image minor mode.
389 With arg, turn Image minor mode on if arg is positive, off otherwise.
390 It provides the key \\<image-mode-map>\\[image-toggle-display] \
391 to switch back to `image-mode'
392 to display an image file as the actual image."
393 nil (:eval (if image-type (format " Image[%s]" image-type) " Image"))
394 image-minor-mode-map
395 :group 'image
396 :version "22.1"
397 (if image-minor-mode
398 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
399
400 ;;;###autoload
401 (defun image-mode-as-text ()
402 "Set a non-image mode as major mode in combination with image minor mode.
403 A non-image major mode found from `auto-mode-alist' or Fundamental mode
404 displays an image file as text. `image-minor-mode' provides the key
405 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
406 to display an image file as the actual image.
407
408 You can use `image-mode-as-text' in `auto-mode-alist' when you want
409 to display an image file as text initially.
410
411 See commands `image-mode' and `image-minor-mode' for more information
412 on these modes."
413 (interactive)
414 ;; image-mode-as-text = normal-mode + image-minor-mode
415 (let ((previous-image-type image-type)) ; preserve `image-type'
416 (if image-mode-previous-major-mode
417 ;; Restore previous major mode that was already found by this
418 ;; function and cached in `image-mode-previous-major-mode'
419 (funcall image-mode-previous-major-mode)
420 (let ((auto-mode-alist
421 (delq nil (mapcar
422 (lambda (elt)
423 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
424 '(image-mode image-mode-maybe image-mode-as-text))
425 elt))
426 auto-mode-alist)))
427 (magic-fallback-mode-alist
428 (delq nil (mapcar
429 (lambda (elt)
430 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
431 '(image-mode image-mode-maybe image-mode-as-text))
432 elt))
433 magic-fallback-mode-alist))))
434 (normal-mode)
435 (set (make-local-variable 'image-mode-previous-major-mode) major-mode)))
436 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
437 (setq image-type previous-image-type)
438 ;; Enable image minor mode with `C-c C-c'.
439 (image-minor-mode 1)
440 ;; Show the image file as text.
441 (image-toggle-display-text)
442 (message "%s" (concat
443 (substitute-command-keys
444 "Type \\[image-toggle-display] to view the image as ")
445 (if (image-get-display-property)
446 "text" "an image") "."))))
447
448 (define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
449
450 (defun image-toggle-display-text ()
451 "Show the image file as text.
452 Remove text properties that display the image."
453 (let ((inhibit-read-only t)
454 (buffer-undo-list t)
455 (modified (buffer-modified-p)))
456 (remove-list-of-text-properties (point-min) (point-max)
457 '(display intangible read-nonsticky
458 read-only front-sticky))
459 (set-buffer-modified-p modified)
460 (if (called-interactively-p 'any)
461 (message "Repeat this command to go back to displaying the image"))))
462
463 (defvar archive-superior-buffer)
464 (defvar tar-superior-buffer)
465 (declare-function image-flush "image.c" (spec &optional frame))
466
467 (defun image-toggle-display-image ()
468 "Show the image of the image file.
469 Turn the image data into a real image, but only if the whole file
470 was inserted."
471 (unless (derived-mode-p 'image-mode major-mode)
472 (error "The buffer is not in Image mode"))
473 (let* ((filename (buffer-file-name))
474 (data-p (not (and filename
475 (file-readable-p filename)
476 (not (file-remote-p filename))
477 (not (buffer-modified-p))
478 (not (and (boundp 'archive-superior-buffer)
479 archive-superior-buffer))
480 (not (and (boundp 'tar-superior-buffer)
481 tar-superior-buffer)))))
482 (file-or-data (if data-p
483 (string-make-unibyte
484 (buffer-substring-no-properties (point-min) (point-max)))
485 filename))
486 (type (image-type file-or-data nil data-p))
487 (image0 (create-animated-image file-or-data type data-p))
488 (image (append image0
489 (image-transform-properties image0)))
490 (props
491 `(display ,image
492 intangible ,image
493 rear-nonsticky (display intangible)
494 read-only t front-sticky (read-only)))
495 (inhibit-read-only t)
496 (buffer-undo-list t)
497 (modified (buffer-modified-p)))
498 (image-flush image)
499 (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file
500 (add-text-properties (point-min) (point-max) props)
501 (restore-buffer-modified-p modified))
502 ;; Inhibit the cursor when the buffer contains only an image,
503 ;; because cursors look very strange on top of images.
504 (setq cursor-type nil)
505 ;; This just makes the arrow displayed in the right fringe
506 ;; area look correct when the image is wider than the window.
507 (setq truncate-lines t)
508 ;; Disable adding a newline at the end of the image file when it
509 ;; is written with, e.g., C-x C-w.
510 (if (coding-system-equal (coding-system-base buffer-file-coding-system)
511 'no-conversion)
512 (set (make-local-variable 'find-file-literally) t))
513 ;; Allow navigation of large images
514 (set (make-local-variable 'auto-hscroll-mode) nil)
515 (setq image-type type)
516 (if (eq major-mode 'image-mode)
517 (setq mode-name (format "Image[%s]" type)))
518 (if (called-interactively-p 'any)
519 (message "Repeat this command to go back to displaying the file as text"))))
520
521 (defun image-toggle-display ()
522 "Toggle between image and text display.
523 If the current buffer is displaying an image file as an image,
524 call `image-mode-as-text' to switch to text. Otherwise, display
525 the image by calling `image-mode'."
526 (interactive)
527 (if (image-get-display-property)
528 (image-mode-as-text)
529 (image-mode)))
530
531 (defun image-after-revert-hook ()
532 (when (image-get-display-property)
533 (image-toggle-display-text)
534 ;; Update image display.
535 (redraw-frame (selected-frame))
536 (image-toggle-display-image)))
537
538 \f
539 ;;; Support for bookmark.el
540 (declare-function bookmark-make-record-default
541 "bookmark" (&optional no-file no-context posn))
542 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
543 (declare-function bookmark-default-handler "bookmark" (bmk))
544
545 (defun image-bookmark-make-record ()
546 `(,@(bookmark-make-record-default nil 'no-context 0)
547 (image-type . ,image-type)
548 (handler . image-bookmark-jump)))
549
550 ;;;###autoload
551 (defun image-bookmark-jump (bmk)
552 ;; This implements the `handler' function interface for record type
553 ;; returned by `bookmark-make-record-function', which see.
554 (prog1 (bookmark-default-handler bmk)
555 (when (not (string= image-type (bookmark-prop-get bmk 'image-type)))
556 (image-toggle-display))))
557 \f
558
559 (defvar image-transform-minor-mode-map
560 (let ((map (make-sparse-keymap)))
561 ;; (define-key map [(control ?+)] 'image-scale-in)
562 ;; (define-key map [(control ?-)] 'image-scale-out)
563 ;; (define-key map [(control ?=)] 'image-scale-none)
564 ;; (define-key map "c f h" 'image-scale-fit-height)
565 ;; (define-key map "c ]" 'image-rotate-right)
566 map)
567 "Minor mode keymap `image-transform-mode'.")
568
569 (define-minor-mode image-transform-mode
570 "Minor mode for scaling and rotating images.
571 This minor mode has no effect unless Emacs is compiled with
572 ImageMagick support."
573 nil "image-transform" image-transform-minor-mode-map)
574
575 (defvar image-transform-resize nil
576 "The image resize operation.
577 Its value should be one of the following:
578 - nil, meaning no resizing.
579 - `fit-height', meaning to fit the image to the window height.
580 - `fit-width', meaning to fit the image to the window width.
581 - A number, which is a scale factor (the default size is 100).")
582
583 (defvar image-transform-rotation 0.0)
584
585 (defun image-transform-properties (display)
586 "Rescale and/or rotate the current image.
587 The scale factor and rotation angle are given by the variables
588 `image-transform-resize' and `image-transform-rotation'. This
589 takes effect only if Emacs is compiled with ImageMagick support."
590 (let* ((size (image-size display t))
591 (height
592 (cond
593 ((numberp image-transform-resize)
594 (unless (= image-transform-resize 100)
595 (* image-transform-resize (cdr size))))
596 ((eq image-transform-resize 'fit-height)
597 (- (nth 3 (window-inside-pixel-edges))
598 (nth 1 (window-inside-pixel-edges))))))
599 (width (if (eq image-transform-resize 'fit-width)
600 (- (nth 2 (window-inside-pixel-edges))
601 (nth 0 (window-inside-pixel-edges))))))
602 ;;TODO fit-to-* should consider the rotation angle
603 `(,@(if height (list :height height))
604 ,@(if width (list :width width))
605 ,@(if (not (equal 0.0 image-transform-rotation))
606 (list :rotation image-transform-rotation)))))
607
608 (defun image-transform-set-scale (scale)
609 "Prompt for a number, and resize the current image by that amount.
610 This command has no effect unless Emacs is compiled with
611 ImageMagick support."
612 (interactive "nScale: ")
613 (setq image-transform-resize scale)
614 (image-toggle-display-image))
615
616 (defun image-transform-fit-to-height ()
617 "Fit the current image to the height of the current window.
618 This command has no effect unless Emacs is compiled with
619 ImageMagick support."
620 (interactive)
621 (setq image-transform-resize 'fit-height)
622 (image-toggle-display-image))
623
624 (defun image-transform-fit-to-width ()
625 "Fit the current image to the width of the current window.
626 This command has no effect unless Emacs is compiled with
627 ImageMagick support."
628 (interactive)
629 (setq image-transform-resize 'fit-width)
630 (image-toggle-display-image))
631
632 (defun image-transform-set-rotation (rotation)
633 "Prompt for an angle ROTATION, and rotate the image by that amount.
634 ROTATION should be in degrees. This command has no effect unless
635 Emacs is compiled with ImageMagick support."
636 (interactive "nRotation angle (in degrees): ")
637 ;;TODO 0 90 180 270 degrees are the only reasonable angles here
638 ;;otherwise combining with rescaling will get very awkward
639 (setq image-transform-rotation (float rotation))
640 (image-toggle-display-image))
641
642 (provide 'image-mode)
643
644 ;;; image-mode.el ends here