Makefile.in (echo-info): Don't try to install info files named just ".info"
[bpt/emacs.git] / lisp / image-mode.el
CommitLineData
c7bd5d57
RS
1;;; image-mode.el --- support for visiting image files
2;;
acaf905b 3;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
c7bd5d57
RS
4;;
5;; Author: Richard Stallman <rms@gnu.org>
6;; Keywords: multimedia
bd78fa1d 7;; Package: emacs
c7bd5d57
RS
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
c7bd5d57 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
c7bd5d57
RS
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c7bd5d57
RS
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)
44e3c7c6 37(eval-when-compile (require 'cl))
c7bd5d57 38
44e3c7c6
SM
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.
46It is called with one argument, the initial WINPROPS.")
47
b81f0bab 48(defun image-mode-winprops (&optional window cleanup)
44e3c7c6
SM
49 "Return winprops of WINDOW.
50A winprops object has the shape (WINDOW . ALIST)."
b81f0bab
CY
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))))
44e3c7c6
SM
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)
60134fd4 89 (setf (image-mode-window-get 'hscroll) ncol)
44e3c7c6
SM
90 (set-window-hscroll (selected-window) ncol))
91
92(defun image-mode-reapply-winprops ()
6d401b4e
SM
93 ;; When set-window-buffer, set hscroll and vscroll to what they were
94 ;; last time the image was displayed in this window.
b81f0bab
CY
95 (when (and (image-get-display-property)
96 (listp image-mode-winprops-alist))
97 (let* ((winprops (image-mode-winprops nil t))
6d401b4e
SM
98 (hscroll (image-mode-window-get 'hscroll winprops))
99 (vscroll (image-mode-window-get 'vscroll winprops)))
c313b5fe
SM
100 (if hscroll (set-window-hscroll (selected-window) hscroll))
101 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
36e1c289 102
44e3c7c6
SM
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
91784462
SM
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
aa360da1
GM
118(declare-function image-size "image.c" (spec &optional pixels frame))
119
c9088194 120(defun image-display-size (spec &optional pixels frame)
2f224f0b
CY
121 "Wrapper around `image-size', handling slice display properties.
122Like `image-size', the return value is (WIDTH . HEIGHT).
123WIDTH and HEIGHT are in canonical character units if PIXELS is
124nil, and in pixel units if PIXELS is non-nil.
125
126If SPEC is an image display property, this function is equivalent
127to `image-size'. If SPEC is a list of properties containing
128`image' and `slice' properties, return the display size taking
129the slice property into account. If the list contains `image'
130but not `slice', return the `image-size' of the specified image."
c9088194
SK
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
bb0cb417
CY
145(defun image-forward-hscroll (&optional n)
146 "Scroll image in current window to the left by N character widths.
147Stop if the right edge of the image is reached."
148 (interactive "p")
149 (cond ((= n 0) nil)
150 ((< n 0)
44e3c7c6 151 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
bb0cb417 152 (t
91784462 153 (let* ((image (image-get-display-property))
bb0cb417
CY
154 (edges (window-inside-edges))
155 (win-width (- (nth 2 edges) (nth 0 edges)))
c9088194 156 (img-width (ceiling (car (image-display-size image)))))
44e3c7c6 157 (image-set-window-hscroll (min (max 0 (- img-width win-width))
36e1c289 158 (+ n (window-hscroll))))))))
bb0cb417
CY
159
160(defun image-backward-hscroll (&optional n)
161 "Scroll image in current window to the right by N character widths.
162Stop if the left edge of the image is reached."
163 (interactive "p")
164 (image-forward-hscroll (- n)))
165
77549ea8 166(defun image-next-line (n)
bb0cb417
CY
167 "Scroll image in current window upward by N lines.
168Stop if the bottom edge of the image is reached."
169 (interactive "p")
170 (cond ((= n 0) nil)
171 ((< n 0)
44e3c7c6 172 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
bb0cb417 173 (t
91784462 174 (let* ((image (image-get-display-property))
bb0cb417
CY
175 (edges (window-inside-edges))
176 (win-height (- (nth 3 edges) (nth 1 edges)))
c9088194 177 (img-height (ceiling (cdr (image-display-size image)))))
44e3c7c6 178 (image-set-window-vscroll (min (max 0 (- img-height win-height))
36e1c289 179 (+ n (window-vscroll))))))))
bb0cb417
CY
180
181(defun image-previous-line (&optional n)
182 "Scroll image in current window downward by N lines.
183Stop 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.
189Stop if the bottom edge of the image is reached.
190If ARG is omitted or nil, scroll upward by a near full screen.
191A near full screen is `next-screen-context-lines' less than a full screen.
192Negative ARG means scroll downward.
193If ARG is the atom `-', scroll downward by nearly full screen.
194When 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)
3488c456 209 "Scroll image in current window downward by N lines.
bb0cb417
CY
210Stop if the top edge of the image is reached.
211If ARG is omitted or nil, scroll downward by a near full screen.
212A near full screen is `next-screen-context-lines' less than a full screen.
213Negative ARG means scroll upward.
214If ARG is the atom `-', scroll upward by nearly full screen.
215When 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.
231With argument ARG not nil or 1, move forward ARG - 1 lines first,
232stopping 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)))
44e3c7c6 237 (image-set-window-hscroll 0))
bb0cb417
CY
238
239(defun image-eol (arg)
240 "Scroll horizontally to the right edge of the image in the current window.
241With argument ARG not nil or 1, move forward ARG - 1 lines first,
242stopping 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)))
91784462 247 (let* ((image (image-get-display-property))
bb0cb417
CY
248 (edges (window-inside-edges))
249 (win-width (- (nth 2 edges) (nth 0 edges)))
c9088194 250 (img-width (ceiling (car (image-display-size image)))))
44e3c7c6 251 (image-set-window-hscroll (max 0 (- img-width win-width)))))
bb0cb417
CY
252
253(defun image-bob ()
254 "Scroll to the top-left corner of the image in the current window."
255 (interactive)
44e3c7c6
SM
256 (image-set-window-hscroll 0)
257 (image-set-window-vscroll 0))
bb0cb417
CY
258
259(defun image-eob ()
260 "Scroll to the bottom-right corner of the image in the current window."
261 (interactive)
91784462 262 (let* ((image (image-get-display-property))
bb0cb417
CY
263 (edges (window-inside-edges))
264 (win-width (- (nth 2 edges) (nth 0 edges)))
c9088194 265 (img-width (ceiling (car (image-display-size image))))
bb0cb417 266 (win-height (- (nth 3 edges) (nth 1 edges)))
c9088194 267 (img-height (ceiling (cdr (image-display-size image)))))
44e3c7c6
SM
268 (image-set-window-hscroll (max 0 (- img-width win-width)))
269 (image-set-window-vscroll (max 0 (- img-height win-height)))))
bb0cb417 270
bd1d6a63
SM
271;; Adjust frame and image size.
272
273(defun image-mode-fit-frame ()
5b2d4a66 274 "Toggle whether to fit the frame to the current image.
bd1d6a63
SM
275This 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))
c9088194 281 (size (image-display-size display)))
bd1d6a63
SM
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
bb0cb417
CY
296;;; Image Mode setup
297
d487ca7d 298(defvar image-type nil
71d73c9c 299 "The image type for the current Image mode buffer.")
d487ca7d
JL
300(make-variable-buffer-local 'image-type)
301
9b9debd1
JL
302(defvar image-mode-previous-major-mode nil
303 "Internal variable to keep the previous non-image major mode.")
304
c7bd5d57 305(defvar image-mode-map
bb0cb417 306 (let ((map (make-sparse-keymap)))
abef340a 307 (set-keymap-parent map special-mode-map)
bb0cb417 308 (define-key map "\C-c\C-c" 'image-toggle-display)
42c27c2a
SM
309 (define-key map (kbd "SPC") 'image-scroll-up)
310 (define-key map (kbd "DEL") 'image-scroll-down)
18af70d0 311 (define-key map (kbd "RET") 'image-toggle-animation)
bb0cb417
CY
312 (define-key map [remap forward-char] 'image-forward-hscroll)
313 (define-key map [remap backward-char] 'image-backward-hscroll)
d8b0cddd
SM
314 (define-key map [remap right-char] 'image-forward-hscroll)
315 (define-key map [remap left-char] 'image-backward-hscroll)
bb0cb417
CY
316 (define-key map [remap previous-line] 'image-previous-line)
317 (define-key map [remap next-line] 'image-next-line)
318 (define-key map [remap scroll-up] 'image-scroll-up)
319 (define-key map [remap scroll-down] 'image-scroll-down)
32129746
JL
320 (define-key map [remap scroll-up-command] 'image-scroll-up)
321 (define-key map [remap scroll-down-command] 'image-scroll-down)
bb0cb417
CY
322 (define-key map [remap move-beginning-of-line] 'image-bol)
323 (define-key map [remap move-end-of-line] 'image-eol)
324 (define-key map [remap beginning-of-buffer] 'image-bob)
325 (define-key map [remap end-of-buffer] 'image-eob)
326 map)
71d73c9c 327 "Mode keymap for `image-mode'.")
bb0cb417 328
9b9debd1 329(defvar image-minor-mode-map
c7bd5d57
RS
330 (let ((map (make-sparse-keymap)))
331 (define-key map "\C-c\C-c" 'image-toggle-display)
332 map)
71d73c9c 333 "Mode keymap for `image-minor-mode'.")
c7bd5d57 334
e0385bf4 335(defvar bookmark-make-record-function)
a47d49c0 336
0d17c4b9
JL
337(put 'image-mode 'mode-class 'special)
338
c7bd5d57
RS
339;;;###autoload
340(defun image-mode ()
341 "Major mode for image files.
342You can use \\<image-mode-map>\\[image-toggle-display]
343to toggle between display as an image and display as text."
344 (interactive)
9b9debd1
JL
345 (condition-case err
346 (progn
347 (unless (display-images-p)
348 (error "Display does not support images"))
349
350 (kill-all-local-variables)
351 (setq major-mode 'image-mode)
352
353 (if (not (image-get-display-property))
354 (progn
355 (image-toggle-display-image)
356 ;; If attempt to display the image fails.
357 (if (not (image-get-display-property))
358 (error "Invalid image")))
359 ;; Set next vars when image is already displayed but local
360 ;; variables were cleared by kill-all-local-variables
361 (setq cursor-type nil truncate-lines t
362 image-type (plist-get (cdr (image-get-display-property)) :type)))
363
364 (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
bb43424b 365 (use-local-map image-mode-map)
9b9debd1
JL
366
367 ;; Use our own bookmarking function for images.
368 (set (make-local-variable 'bookmark-make-record-function)
369 'image-bookmark-make-record)
370
371 ;; Keep track of [vh]scroll when switching buffers
372 (image-mode-setup-winprops)
373
374 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
0fb1193d 375 (add-hook 'after-revert-hook 'image-after-revert-hook nil t)
9b9debd1 376 (run-mode-hooks 'image-mode-hook)
18af70d0
CY
377 (let ((image (image-get-display-property))
378 (msg1 (substitute-command-keys
379 "Type \\[image-toggle-display] to view the image as ")))
380 (cond
381 ((null image)
382 (message "%s" (concat msg1 "an image.")))
383 ((image-animated-p image)
384 (message "%s"
385 (concat msg1 "text, or "
386 (substitute-command-keys
387 "\\[image-toggle-animation] to animate."))))
388 (t
389 (message "%s" (concat msg1 "text."))))))
390
9b9debd1
JL
391 (error
392 (image-mode-as-text)
393 (funcall
394 (if (called-interactively-p 'any) 'error 'message)
395 "Cannot display image: %s" (cdr err)))))
18af70d0 396
d90e651e
JL
397;;;###autoload
398(define-minor-mode image-minor-mode
06e21633
CY
399 "Toggle Image minor mode in this buffer.
400With a prefix argument ARG, enable Image minor mode if ARG is
401positive, and disable it otherwise. If called from Lisp, enable
402the mode if ARG is omitted or nil.
403
404Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
405to switch back to `image-mode' and display an image file as the
406actual image."
9b9debd1
JL
407 nil (:eval (if image-type (format " Image[%s]" image-type) " Image"))
408 image-minor-mode-map
d90e651e
JL
409 :group 'image
410 :version "22.1"
9b9debd1
JL
411 (if image-minor-mode
412 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
d90e651e
JL
413
414;;;###autoload
9b9debd1
JL
415(defun image-mode-as-text ()
416 "Set a non-image mode as major mode in combination with image minor mode.
417A non-image major mode found from `auto-mode-alist' or Fundamental mode
418displays an image file as text. `image-minor-mode' provides the key
419\\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
420to display an image file as the actual image.
421
422You can use `image-mode-as-text' in `auto-mode-alist' when you want
316d12fb 423to display an image file as text initially.
9b9debd1
JL
424
425See commands `image-mode' and `image-minor-mode' for more information
426on these modes."
d90e651e 427 (interactive)
9b9debd1
JL
428 ;; image-mode-as-text = normal-mode + image-minor-mode
429 (let ((previous-image-type image-type)) ; preserve `image-type'
430 (if image-mode-previous-major-mode
431 ;; Restore previous major mode that was already found by this
432 ;; function and cached in `image-mode-previous-major-mode'
433 (funcall image-mode-previous-major-mode)
434 (let ((auto-mode-alist
435 (delq nil (mapcar
436 (lambda (elt)
437 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
438 '(image-mode image-mode-maybe image-mode-as-text))
439 elt))
440 auto-mode-alist)))
441 (magic-fallback-mode-alist
442 (delq nil (mapcar
443 (lambda (elt)
444 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
445 '(image-mode image-mode-maybe image-mode-as-text))
446 elt))
447 magic-fallback-mode-alist))))
448 (normal-mode)
449 (set (make-local-variable 'image-mode-previous-major-mode) major-mode)))
450 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
451 (setq image-type previous-image-type)
452 ;; Enable image minor mode with `C-c C-c'.
453 (image-minor-mode 1)
454 ;; Show the image file as text.
455 (image-toggle-display-text)
456 (message "%s" (concat
457 (substitute-command-keys
458 "Type \\[image-toggle-display] to view the image as ")
459 (if (image-get-display-property)
460 "text" "an image") "."))))
461
462(define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
d90e651e
JL
463
464(defun image-toggle-display-text ()
9b9debd1
JL
465 "Show the image file as text.
466Remove text properties that display the image."
467 (let ((inhibit-read-only t)
468 (buffer-undo-list t)
469 (modified (buffer-modified-p)))
470 (remove-list-of-text-properties (point-min) (point-max)
345083b2 471 '(display read-nonsticky ;; intangible
9b9debd1
JL
472 read-only front-sticky))
473 (set-buffer-modified-p modified)
474 (if (called-interactively-p 'any)
475 (message "Repeat this command to go back to displaying the image"))))
c7bd5d57 476
f2920ffe
RS
477(defvar archive-superior-buffer)
478(defvar tar-superior-buffer)
110683ad 479(declare-function image-flush "image.c" (spec &optional frame))
f2920ffe 480
9b9debd1
JL
481(defun image-toggle-display-image ()
482 "Show the image of the image file.
483Turn the image data into a real image, but only if the whole file
484was inserted."
953a8c3b 485 (unless (derived-mode-p 'image-mode)
a32d4040 486 (error "The buffer is not in Image mode"))
9b9debd1
JL
487 (let* ((filename (buffer-file-name))
488 (data-p (not (and filename
489 (file-readable-p filename)
490 (not (file-remote-p filename))
491 (not (buffer-modified-p))
492 (not (and (boundp 'archive-superior-buffer)
493 archive-superior-buffer))
494 (not (and (boundp 'tar-superior-buffer)
495 tar-superior-buffer)))))
496 (file-or-data (if data-p
497 (string-make-unibyte
498 (buffer-substring-no-properties (point-min) (point-max)))
499 filename))
500 (type (image-type file-or-data nil data-p))
e8cbec34
CY
501 (image (create-image file-or-data type data-p))
502 (inhibit-read-only t)
503 (buffer-undo-list t)
504 (modified (buffer-modified-p))
505 props)
506
18af70d0
CY
507 ;; Discard any stale image data before looking it up again.
508 (image-flush image)
e8cbec34
CY
509 (setq image (append image (image-transform-properties image)))
510 (setq props
9b9debd1 511 `(display ,image
345083b2
SM
512 ;; intangible ,image
513 rear-nonsticky (display) ;; intangible
9b9debd1 514 read-only t front-sticky (read-only)))
e8cbec34 515
9b9debd1
JL
516 (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file
517 (add-text-properties (point-min) (point-max) props)
518 (restore-buffer-modified-p modified))
519 ;; Inhibit the cursor when the buffer contains only an image,
520 ;; because cursors look very strange on top of images.
521 (setq cursor-type nil)
522 ;; This just makes the arrow displayed in the right fringe
523 ;; area look correct when the image is wider than the window.
524 (setq truncate-lines t)
4114ed61
EZ
525 ;; Disable adding a newline at the end of the image file when it
526 ;; is written with, e.g., C-x C-w.
527 (if (coding-system-equal (coding-system-base buffer-file-coding-system)
528 'no-conversion)
529 (set (make-local-variable 'find-file-literally) t))
9b9debd1
JL
530 ;; Allow navigation of large images
531 (set (make-local-variable 'auto-hscroll-mode) nil)
532 (setq image-type type)
533 (if (eq major-mode 'image-mode)
534 (setq mode-name (format "Image[%s]" type)))
2d21d7f6 535 (image-transform-check-size)
9b9debd1
JL
536 (if (called-interactively-p 'any)
537 (message "Repeat this command to go back to displaying the file as text"))))
538
c7bd5d57 539(defun image-toggle-display ()
71d73c9c
CY
540 "Toggle between image and text display.
541If the current buffer is displaying an image file as an image,
542call `image-mode-as-text' to switch to text. Otherwise, display
543the image by calling `image-mode'."
c7bd5d57 544 (interactive)
91784462 545 (if (image-get-display-property)
9b9debd1
JL
546 (image-mode-as-text)
547 (image-mode)))
0fb1193d
JL
548
549(defun image-after-revert-hook ()
550 (when (image-get-display-property)
551 (image-toggle-display-text)
552 ;; Update image display.
637d46ca
GM
553 (mapc (lambda (window) (redraw-frame (window-frame window)))
554 (get-buffer-window-list (current-buffer) 'nomini 'visible))
0fb1193d
JL
555 (image-toggle-display-image)))
556
97666703 557\f
18af70d0
CY
558;;; Animated images
559
560(defcustom image-animate-loop nil
eea14f31 561 "Non-nil means animated images loop forever, rather than playing once."
18af70d0
CY
562 :type 'boolean
563 :version "24.1"
564 :group 'image)
565
566(defun image-toggle-animation ()
eea14f31
GM
567 "Start or stop animating the current image.
568If `image-animate-loop' is non-nil, animation loops forever.
569Otherwise it plays once, then stops."
18af70d0
CY
570 (interactive)
571 (let ((image (image-get-display-property))
572 animation)
573 (cond
574 ((null image)
575 (error "No image is present"))
576 ((null (setq animation (image-animated-p image)))
577 (message "No image animation."))
578 (t
579 (let ((timer (image-animate-timer image)))
580 (if timer
581 (cancel-timer timer)
582 (let ((index (plist-get (cdr image) :index)))
583 ;; If we're at the end, restart.
584 (and index
585 (>= index (1- (car animation)))
586 (setq index nil))
587 (image-animate image index
588 (if image-animate-loop t)))))))))
589
590\f
137187c8 591;;; Support for bookmark.el
e44fa724
KF
592(declare-function bookmark-make-record-default
593 "bookmark" (&optional no-file no-context posn))
43f8b275
SM
594(declare-function bookmark-prop-get "bookmark" (bookmark prop))
595(declare-function bookmark-default-handler "bookmark" (bmk))
137187c8 596
43f8b275 597(defun image-bookmark-make-record ()
f12492c8
TV
598 `(,@(bookmark-make-record-default nil 'no-context 0)
599 (image-type . ,image-type)
600 (handler . image-bookmark-jump)))
137187c8 601
137187c8
TH
602;;;###autoload
603(defun image-bookmark-jump (bmk)
03e26a79 604 ;; This implements the `handler' function interface for record type
e0385bf4 605 ;; returned by `bookmark-make-record-function', which see.
43f8b275
SM
606 (prog1 (bookmark-default-handler bmk)
607 (when (not (string= image-type (bookmark-prop-get bmk 'image-type)))
608 (image-toggle-display))))
97666703 609\f
c7f514b5 610
b4ac6e8c 611;; Not yet implemented.
7102e6d0
WJ
612;; (defvar image-transform-minor-mode-map
613;; (let ((map (make-sparse-keymap)))
614;; ;; (define-key map [(control ?+)] 'image-scale-in)
615;; ;; (define-key map [(control ?-)] 'image-scale-out)
616;; ;; (define-key map [(control ?=)] 'image-scale-none)
617;; ;; (define-key map "c f h" 'image-scale-fit-height)
618;; ;; (define-key map "c ]" 'image-rotate-right)
619;; map)
620;; "Minor mode keymap `image-transform-mode'.")
621;;
622;; (define-minor-mode image-transform-mode
623;; "Minor mode for scaling and rotating images.
624;; With a prefix argument ARG, enable the mode if ARG is positive,
625;; and disable it otherwise. If called from Lisp, enable the mode
626;; if ARG is omitted or nil. This minor mode requires Emacs to have
627;; been compiled with ImageMagick support."
628;; nil "image-transform" image-transform-minor-mode-map)
b4ac6e8c
GM
629
630
631;; FIXME this doesn't seem mature yet. Document in manual when it is.
a32d4040
CY
632(defvar image-transform-resize nil
633 "The image resize operation.
634Its value should be one of the following:
635 - nil, meaning no resizing.
636 - `fit-height', meaning to fit the image to the window height.
637 - `fit-width', meaning to fit the image to the window width.
7102e6d0 638 - A number, which is a scale factor (the default size is 1).")
c7f514b5 639
2d21d7f6
WJ
640(defvar image-transform-scale 1.0
641 "The scale factor of the image being displayed.")
642
18af70d0
CY
643(defvar image-transform-rotation 0.0
644 "Rotation angle for the image in the current Image mode buffer.")
c7f514b5 645
2d21d7f6
WJ
646(defvar image-transform-right-angle-fudge 0.0001
647 "Snap distance to a multiple of a right angle.
648There's no deep theory behind the default value, it should just
649be somewhat larger than ImageMagick's MagickEpsilon.")
650
651(defsubst image-transform-width (width height)
652 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
653The rotation angle is the value of `image-transform-rotation' in degrees."
654 (let ((angle (degrees-to-radians image-transform-rotation)))
655 ;; Assume, w.l.o.g., that the vertices of the rectangle have the
656 ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the
657 ;; rotation by the angle A. The projections onto the first axis
658 ;; of the vertices of the rotated rectangle are +- (w/2) cos A +-
659 ;; (h/2) sin A, and the difference between the largest and the
660 ;; smallest of the four values is the expression below.
661 (+ (* width (abs (cos angle))) (* height (abs (sin angle))))))
662
663;; The following comment and code snippet are from
664;; ImageMagick-6.7.4-4/magick/distort.c
665
666;; /* Set the output image geometry to calculated 'bestfit'.
667;; Yes this tends to 'over do' the file image size, ON PURPOSE!
668;; Do not do this for DePolar which needs to be exact for virtual tiling.
669;; */
670;; if ( fix_bounds ) {
671;; geometry.x = (ssize_t) floor(min.x-0.5);
672;; geometry.y = (ssize_t) floor(min.y-0.5);
673;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
674;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
675;; }
676
677;; Other parts of the same file show that here the origin is in the
678;; left lower corner of the image rectangle, the center of the
679;; rotation is the center of the rectangle and min.x and max.x
680;; (resp. min.y and max.y) are the smallest and the largest of the
681;; projections of the vertices onto the first (resp. second) axis.
682
683(defun image-transform-fit-width (width height length)
684 "Return (w . h) so that a rotated w x h image has exactly width LENGTH.
685The rotation angle is the value of `image-transform-rotation'.
686Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
687an \"approximately uniformly\" scaled W x H rectangle, which
688currently means that w is one of floor(s W) + {0, 1, -1} and h is
689floor(s H), where s can be recovered as the value of `image-transform-scale'.
690The value of `image-transform-rotation' may be replaced by
691a slightly different angle. Currently this is done for values
692close to a multiple of 90, see `image-transform-right-angle-fudge'."
693 (cond ((< (abs (- (mod (+ image-transform-rotation 90) 180) 90))
694 image-transform-right-angle-fudge)
695 (assert (not (zerop width)) t)
696 (setq image-transform-rotation
697 (float (round image-transform-rotation))
698 image-transform-scale (/ (float length) width))
699 (cons length nil))
700 ((< (abs (- (mod (+ image-transform-rotation 45) 90) 45))
701 image-transform-right-angle-fudge)
702 (assert (not (zerop height)) t)
703 (setq image-transform-rotation
704 (float (round image-transform-rotation))
705 image-transform-scale (/ (float length) height))
706 (cons nil length))
707 (t
708 (assert (not (and (zerop width) (zerop height))) t)
709 (setq image-transform-scale
710 (/ (float (1- length)) (image-transform-width width height)))
711 ;; Assume we have a w x h image and an angle A, and let l =
712 ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width
713 ;; of the bounding box of the rotated image, as calculated by
714 ;; `image-transform-width'. The code snippet quoted above
715 ;; means that ImageMagick puts the rotated image in
716 ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w.
717 ;; Elementary considerations show that this is equivalent to
718 ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is
719 ;; the given `length' parameter and our job is to determine
720 ;; reasonable values for w and h which satisfy these
721 ;; conditions.
722 (let ((w (floor (* image-transform-scale width)))
723 (h (floor (* image-transform-scale height))))
724 ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H)
725 ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2,
726 ;; hence l(w, h) > (L-1) - 2 = L-3.
727 (cons
728 (cond ((= (mod w 2) (mod length 2))
729 w)
730 ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <=
731 ;; L-1 hold?
732 ((<= (image-transform-width (1+ w) h) (1- length))
733 (1+ w))
734 ;; No, it doesn't, but this implies that l(w-1, h) =
735 ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) -
736 ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1.
737 (t
738 (1- w)))
739 h)))))
740
741(defun image-transform-check-size ()
742 "Check that the image exactly fits the width/height of the window."
743 (unless (numberp image-transform-resize)
744 (let ((size (image-display-size (image-get-display-property) t)))
745 (cond ((eq image-transform-resize 'fit-width)
746 (assert (= (car size)
747 (- (nth 2 (window-inside-pixel-edges))
748 (nth 0 (window-inside-pixel-edges))))
749 t))
750 ((eq image-transform-resize 'fit-height)
751 (assert (= (cdr size)
752 (- (nth 3 (window-inside-pixel-edges))
753 (nth 1 (window-inside-pixel-edges))))
754 t))))))
755
18af70d0
CY
756(defun image-transform-properties (spec)
757 "Return rescaling/rotation properties for image SPEC.
758These properties are determined by the Image mode variables
759`image-transform-resize' and `image-transform-rotation'. The
760return value is suitable for appending to an image spec.
e8cbec34 761
09e80d9f 762Rescaling and rotation properties only take effect if Emacs is
e8cbec34 763compiled with ImageMagick support."
2d21d7f6 764 (setq image-transform-scale 1.0)
18af70d0 765 (when (or image-transform-resize
2d21d7f6 766 (/= image-transform-rotation 0.0))
18af70d0
CY
767 ;; Note: `image-size' looks up and thus caches the untransformed
768 ;; image. There's no easy way to prevent that.
769 (let* ((size (image-size spec t))
2d21d7f6 770 (resized
18af70d0
CY
771 (cond
772 ((numberp image-transform-resize)
7102e6d0 773 (unless (= image-transform-resize 1)
2d21d7f6
WJ
774 (setq image-transform-scale image-transform-resize)
775 (cons nil (floor (* image-transform-resize (cdr size))))))
776 ((eq image-transform-resize 'fit-width)
777 (image-transform-fit-width
778 (car size) (cdr size)
779 (- (nth 2 (window-inside-pixel-edges))
780 (nth 0 (window-inside-pixel-edges)))))
18af70d0 781 ((eq image-transform-resize 'fit-height)
2d21d7f6
WJ
782 (let ((res (image-transform-fit-width
783 (cdr size) (car size)
784 (- (nth 3 (window-inside-pixel-edges))
785 (nth 1 (window-inside-pixel-edges))))))
786 (cons (cdr res) (car res)))))))
787 `(,@(when (car resized)
788 (list :width (car resized)))
789 ,@(when (cdr resized)
790 (list :height (cdr resized)))
791 ,@(unless (= 0.0 image-transform-rotation)
792 (list :rotation image-transform-rotation))))))
c7f514b5
JV
793
794(defun image-transform-set-scale (scale)
a32d4040
CY
795 "Prompt for a number, and resize the current image by that amount.
796This command has no effect unless Emacs is compiled with
797ImageMagick support."
798 (interactive "nScale: ")
c183f693 799 (setq image-transform-resize scale)
a32d4040 800 (image-toggle-display-image))
c7f514b5
JV
801
802(defun image-transform-fit-to-height ()
a32d4040
CY
803 "Fit the current image to the height of the current window.
804This command has no effect unless Emacs is compiled with
805ImageMagick support."
c7f514b5 806 (interactive)
a32d4040
CY
807 (setq image-transform-resize 'fit-height)
808 (image-toggle-display-image))
c7f514b5 809
3a50be51 810(defun image-transform-fit-to-width ()
a32d4040
CY
811 "Fit the current image to the width of the current window.
812This command has no effect unless Emacs is compiled with
813ImageMagick support."
3a50be51 814 (interactive)
a32d4040
CY
815 (setq image-transform-resize 'fit-width)
816 (image-toggle-display-image))
c7f514b5
JV
817
818(defun image-transform-set-rotation (rotation)
a32d4040
CY
819 "Prompt for an angle ROTATION, and rotate the image by that amount.
820ROTATION should be in degrees. This command has no effect unless
821Emacs is compiled with ImageMagick support."
822 (interactive "nRotation angle (in degrees): ")
2d21d7f6 823 (setq image-transform-rotation (float (mod rotation 360)))
a32d4040 824 (image-toggle-display-image))
c7f514b5 825
c7bd5d57
RS
826(provide 'image-mode)
827
828;;; image-mode.el ends here