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