* lisp/image-mode.el (image-mode-winprops): Don't throw away the fallback
[bpt/emacs.git] / lisp / image-mode.el
CommitLineData
83600dc8 1;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*-
c7bd5d57 2;;
ab422c4d 3;; Copyright (C) 2005-2013 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
83600dc8
SM
34;; Todo:
35
36;; Consolidate with doc-view to make them work on directories of images or on
37;; image files containing various "pages".
38
c7bd5d57
RS
39;;; Code:
40
41(require 'image)
f58e0fd5 42(eval-when-compile (require 'cl-lib))
c7bd5d57 43
44e3c7c6
SM
44;;; Image mode window-info management.
45
83600dc8 46(defvar-local image-mode-winprops-alist t)
44e3c7c6
SM
47
48(defvar image-mode-new-window-functions nil
49 "Special hook run when image data is requested in a new window.
50It is called with one argument, the initial WINPROPS.")
51
b81f0bab 52(defun image-mode-winprops (&optional window cleanup)
44e3c7c6 53 "Return winprops of WINDOW.
83600dc8
SM
54A winprops object has the shape (WINDOW . ALIST).
55WINDOW defaults to `selected-window' if it displays the current buffer, and
56otherwise it defaults to t, used for times when the buffer is not displayed."
b81f0bab 57 (cond ((null window)
83600dc8
SM
58 (setq window
59 (if (eq (current-buffer) (window-buffer)) (selected-window) t)))
60 ((eq window t))
b81f0bab
CY
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)
08ce64e6
SM
66 (let ((w (car-safe winprop)))
67 (if (or (not (windowp w)) (window-live-p w))
68 winprop)))
b81f0bab 69 image-mode-winprops-alist))))
44e3c7c6
SM
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))
80
81(defun image-mode-window-get (prop &optional winprops)
f58e0fd5
SM
82 (declare (gv-setter (lambda (val)
83 `(image-mode-window-put ,prop ,val ,winprops))))
44e3c7c6
SM
84 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
85 (cdr (assq prop (cdr winprops))))
86
44e3c7c6
SM
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)))))
91
92(defun image-set-window-vscroll (vscroll)
93 (setf (image-mode-window-get 'vscroll) vscroll)
94 (set-window-vscroll (selected-window) vscroll))
95
96(defun image-set-window-hscroll (ncol)
60134fd4 97 (setf (image-mode-window-get 'hscroll) ncol)
44e3c7c6
SM
98 (set-window-hscroll (selected-window) ncol))
99
100(defun image-mode-reapply-winprops ()
6d401b4e
SM
101 ;; When set-window-buffer, set hscroll and vscroll to what they were
102 ;; last time the image was displayed in this window.
b81f0bab
CY
103 (when (and (image-get-display-property)
104 (listp image-mode-winprops-alist))
105 (let* ((winprops (image-mode-winprops nil t))
6d401b4e
SM
106 (hscroll (image-mode-window-get 'hscroll winprops))
107 (vscroll (image-mode-window-get 'vscroll winprops)))
c313b5fe
SM
108 (if hscroll (set-window-hscroll (selected-window) hscroll))
109 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
36e1c289 110
44e3c7c6
SM
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))
117
118;;; Image scrolling functions
119
91784462
SM
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))))
125
aa360da1
GM
126(declare-function image-size "image.c" (spec &optional pixels frame))
127
c9088194 128(defun image-display-size (spec &optional pixels frame)
2f224f0b
CY
129 "Wrapper around `image-size', handling slice display properties.
130Like `image-size', the return value is (WIDTH . HEIGHT).
131WIDTH and HEIGHT are in canonical character units if PIXELS is
132nil, and in pixel units if PIXELS is non-nil.
133
134If SPEC is an image display property, this function is equivalent
135to `image-size'. If SPEC is a list of properties containing
136`image' and `slice' properties, return the display size taking
137the slice property into account. If the list contains `image'
138but not `slice', return the `image-size' of the specified image."
c9088194
SK
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))
150 (t
151 (error "Invalid image specification: %s" spec))))))
152
bb0cb417
CY
153(defun image-forward-hscroll (&optional n)
154 "Scroll image in current window to the left by N character widths.
155Stop if the right edge of the image is reached."
156 (interactive "p")
157 (cond ((= n 0) nil)
158 ((< n 0)
44e3c7c6 159 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
bb0cb417 160 (t
91784462 161 (let* ((image (image-get-display-property))
bb0cb417
CY
162 (edges (window-inside-edges))
163 (win-width (- (nth 2 edges) (nth 0 edges)))
c9088194 164 (img-width (ceiling (car (image-display-size image)))))
44e3c7c6 165 (image-set-window-hscroll (min (max 0 (- img-width win-width))
36e1c289 166 (+ n (window-hscroll))))))))
bb0cb417
CY
167
168(defun image-backward-hscroll (&optional n)
169 "Scroll image in current window to the right by N character widths.
170Stop if the left edge of the image is reached."
171 (interactive "p")
172 (image-forward-hscroll (- n)))
173
77549ea8 174(defun image-next-line (n)
bb0cb417
CY
175 "Scroll image in current window upward by N lines.
176Stop if the bottom edge of the image is reached."
177 (interactive "p")
178 (cond ((= n 0) nil)
179 ((< n 0)
44e3c7c6 180 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
bb0cb417 181 (t
91784462 182 (let* ((image (image-get-display-property))
bb0cb417
CY
183 (edges (window-inside-edges))
184 (win-height (- (nth 3 edges) (nth 1 edges)))
c9088194 185 (img-height (ceiling (cdr (image-display-size image)))))
44e3c7c6 186 (image-set-window-vscroll (min (max 0 (- img-height win-height))
36e1c289 187 (+ n (window-vscroll))))))))
bb0cb417
CY
188
189(defun image-previous-line (&optional n)
190 "Scroll image in current window downward by N lines.
191Stop if the top edge of the image is reached."
192 (interactive "p")
193 (image-next-line (- n)))
194
195(defun image-scroll-up (&optional n)
196 "Scroll image in current window upward by N lines.
197Stop if the bottom edge of the image is reached.
198If ARG is omitted or nil, scroll upward by a near full screen.
199A near full screen is `next-screen-context-lines' less than a full screen.
200Negative ARG means scroll downward.
201If ARG is the atom `-', scroll downward by nearly full screen.
202When 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)))))
215
216(defun image-scroll-down (&optional n)
3488c456 217 "Scroll image in current window downward by N lines.
bb0cb417
CY
218Stop if the top edge of the image is reached.
219If ARG is omitted or nil, scroll downward by a near full screen.
220A near full screen is `next-screen-context-lines' less than a full screen.
221Negative ARG means scroll upward.
222If ARG is the atom `-', scroll upward by nearly full screen.
223When 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))))))
236
237(defun image-bol (arg)
238 "Scroll horizontally to the left edge of the image in the current window.
239With argument ARG not nil or 1, move forward ARG - 1 lines first,
240stopping 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)))
44e3c7c6 245 (image-set-window-hscroll 0))
bb0cb417
CY
246
247(defun image-eol (arg)
248 "Scroll horizontally to the right edge of the image in the current window.
249With argument ARG not nil or 1, move forward ARG - 1 lines first,
250stopping 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)))
91784462 255 (let* ((image (image-get-display-property))
bb0cb417
CY
256 (edges (window-inside-edges))
257 (win-width (- (nth 2 edges) (nth 0 edges)))
c9088194 258 (img-width (ceiling (car (image-display-size image)))))
44e3c7c6 259 (image-set-window-hscroll (max 0 (- img-width win-width)))))
bb0cb417
CY
260
261(defun image-bob ()
262 "Scroll to the top-left corner of the image in the current window."
263 (interactive)
44e3c7c6
SM
264 (image-set-window-hscroll 0)
265 (image-set-window-vscroll 0))
bb0cb417
CY
266
267(defun image-eob ()
268 "Scroll to the bottom-right corner of the image in the current window."
269 (interactive)
91784462 270 (let* ((image (image-get-display-property))
bb0cb417
CY
271 (edges (window-inside-edges))
272 (win-width (- (nth 2 edges) (nth 0 edges)))
c9088194 273 (img-width (ceiling (car (image-display-size image))))
bb0cb417 274 (win-height (- (nth 3 edges) (nth 1 edges)))
c9088194 275 (img-height (ceiling (cdr (image-display-size image)))))
44e3c7c6
SM
276 (image-set-window-hscroll (max 0 (- img-width win-width)))
277 (image-set-window-vscroll (max 0 (- img-height win-height)))))
bb0cb417 278
bd1d6a63
SM
279;; Adjust frame and image size.
280
281(defun image-mode-fit-frame ()
5b2d4a66 282 "Toggle whether to fit the frame to the current image.
bd1d6a63
SM
283This 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))
c9088194 289 (size (image-display-size display)))
bd1d6a63
SM
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))))
303
bb0cb417
CY
304;;; Image Mode setup
305
d487ca7d 306(defvar image-type nil
71d73c9c 307 "The image type for the current Image mode buffer.")
d487ca7d
JL
308(make-variable-buffer-local 'image-type)
309
9b9debd1
JL
310(defvar image-mode-previous-major-mode nil
311 "Internal variable to keep the previous non-image major mode.")
312
c7bd5d57 313(defvar image-mode-map
bb0cb417 314 (let ((map (make-sparse-keymap)))
abef340a 315 (set-keymap-parent map special-mode-map)
bb0cb417 316 (define-key map "\C-c\C-c" 'image-toggle-display)
42c27c2a
SM
317 (define-key map (kbd "SPC") 'image-scroll-up)
318 (define-key map (kbd "DEL") 'image-scroll-down)
18af70d0 319 (define-key map (kbd "RET") 'image-toggle-animation)
bb0cb417
CY
320 (define-key map [remap forward-char] 'image-forward-hscroll)
321 (define-key map [remap backward-char] 'image-backward-hscroll)
d8b0cddd
SM
322 (define-key map [remap right-char] 'image-forward-hscroll)
323 (define-key map [remap left-char] 'image-backward-hscroll)
bb0cb417
CY
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)
32129746
JL
328 (define-key map [remap scroll-up-command] 'image-scroll-up)
329 (define-key map [remap scroll-down-command] 'image-scroll-down)
bb0cb417
CY
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)
71d73c9c 335 "Mode keymap for `image-mode'.")
bb0cb417 336
9b9debd1 337(defvar image-minor-mode-map
c7bd5d57
RS
338 (let ((map (make-sparse-keymap)))
339 (define-key map "\C-c\C-c" 'image-toggle-display)
340 map)
71d73c9c 341 "Mode keymap for `image-minor-mode'.")
c7bd5d57 342
e0385bf4 343(defvar bookmark-make-record-function)
a47d49c0 344
0d17c4b9
JL
345(put 'image-mode 'mode-class 'special)
346
c7bd5d57
RS
347;;;###autoload
348(defun image-mode ()
349 "Major mode for image files.
350You can use \\<image-mode-map>\\[image-toggle-display]
351to toggle between display as an image and display as text."
352 (interactive)
9b9debd1
JL
353 (condition-case err
354 (progn
355 (unless (display-images-p)
356 (error "Display does not support images"))
357
358 (kill-all-local-variables)
359 (setq major-mode 'image-mode)
360
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)))
371
372 (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
bb43424b 373 (use-local-map image-mode-map)
9b9debd1
JL
374
375 ;; Use our own bookmarking function for images.
376 (set (make-local-variable 'bookmark-make-record-function)
377 'image-bookmark-make-record)
378
379 ;; Keep track of [vh]scroll when switching buffers
380 (image-mode-setup-winprops)
381
382 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
0fb1193d 383 (add-hook 'after-revert-hook 'image-after-revert-hook nil t)
9b9debd1 384 (run-mode-hooks 'image-mode-hook)
18af70d0
CY
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."))))
396 (t
397 (message "%s" (concat msg1 "text."))))))
398
9b9debd1
JL
399 (error
400 (image-mode-as-text)
401 (funcall
402 (if (called-interactively-p 'any) 'error 'message)
403 "Cannot display image: %s" (cdr err)))))
18af70d0 404
d90e651e
JL
405;;;###autoload
406(define-minor-mode image-minor-mode
06e21633
CY
407 "Toggle Image minor mode in this buffer.
408With a prefix argument ARG, enable Image minor mode if ARG is
409positive, and disable it otherwise. If called from Lisp, enable
410the mode if ARG is omitted or nil.
411
412Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
413to switch back to `image-mode' and display an image file as the
414actual image."
9b9debd1
JL
415 nil (:eval (if image-type (format " Image[%s]" image-type) " Image"))
416 image-minor-mode-map
d90e651e
JL
417 :group 'image
418 :version "22.1"
9b9debd1
JL
419 (if image-minor-mode
420 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
d90e651e
JL
421
422;;;###autoload
9b9debd1
JL
423(defun image-mode-as-text ()
424 "Set a non-image mode as major mode in combination with image minor mode.
425A non-image major mode found from `auto-mode-alist' or Fundamental mode
426displays an image file as text. `image-minor-mode' provides the key
427\\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
428to display an image file as the actual image.
429
430You can use `image-mode-as-text' in `auto-mode-alist' when you want
316d12fb 431to display an image file as text initially.
9b9debd1
JL
432
433See commands `image-mode' and `image-minor-mode' for more information
434on these modes."
d90e651e 435 (interactive)
9b9debd1
JL
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") "."))))
469
470(define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
d90e651e
JL
471
472(defun image-toggle-display-text ()
9b9debd1
JL
473 "Show the image file as text.
474Remove 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)
345083b2 479 '(display read-nonsticky ;; intangible
9b9debd1
JL
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"))))
c7bd5d57 484
f2920ffe
RS
485(defvar archive-superior-buffer)
486(defvar tar-superior-buffer)
110683ad 487(declare-function image-flush "image.c" (spec &optional frame))
f2920ffe 488
9b9debd1
JL
489(defun image-toggle-display-image ()
490 "Show the image of the image file.
491Turn the image data into a real image, but only if the whole file
492was inserted."
953a8c3b 493 (unless (derived-mode-p 'image-mode)
a32d4040 494 (error "The buffer is not in Image mode"))
9b9debd1
JL
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))
e8cbec34
CY
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)
514
18af70d0
CY
515 ;; Discard any stale image data before looking it up again.
516 (image-flush image)
e8cbec34
CY
517 (setq image (append image (image-transform-properties image)))
518 (setq props
9b9debd1 519 `(display ,image
345083b2
SM
520 ;; intangible ,image
521 rear-nonsticky (display) ;; intangible
9b9debd1 522 read-only t front-sticky (read-only)))
e8cbec34 523
9b9debd1
JL
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)
4114ed61
EZ
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))
9b9debd1
JL
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)))
2d21d7f6 543 (image-transform-check-size)
9b9debd1
JL
544 (if (called-interactively-p 'any)
545 (message "Repeat this command to go back to displaying the file as text"))))
546
c7bd5d57 547(defun image-toggle-display ()
71d73c9c
CY
548 "Toggle between image and text display.
549If the current buffer is displaying an image file as an image,
550call `image-mode-as-text' to switch to text. Otherwise, display
551the image by calling `image-mode'."
c7bd5d57 552 (interactive)
91784462 553 (if (image-get-display-property)
9b9debd1
JL
554 (image-mode-as-text)
555 (image-mode)))
0fb1193d
JL
556
557(defun image-after-revert-hook ()
558 (when (image-get-display-property)
559 (image-toggle-display-text)
560 ;; Update image display.
637d46ca
GM
561 (mapc (lambda (window) (redraw-frame (window-frame window)))
562 (get-buffer-window-list (current-buffer) 'nomini 'visible))
0fb1193d
JL
563 (image-toggle-display-image)))
564
97666703 565\f
18af70d0
CY
566;;; Animated images
567
568(defcustom image-animate-loop nil
eea14f31 569 "Non-nil means animated images loop forever, rather than playing once."
18af70d0
CY
570 :type 'boolean
571 :version "24.1"
572 :group 'image)
573
574(defun image-toggle-animation ()
eea14f31
GM
575 "Start or stop animating the current image.
576If `image-animate-loop' is non-nil, animation loops forever.
577Otherwise it plays once, then stops."
18af70d0
CY
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."))
586 (t
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)))))))))
597
598\f
137187c8 599;;; Support for bookmark.el
e44fa724
KF
600(declare-function bookmark-make-record-default
601 "bookmark" (&optional no-file no-context posn))
43f8b275
SM
602(declare-function bookmark-prop-get "bookmark" (bookmark prop))
603(declare-function bookmark-default-handler "bookmark" (bmk))
137187c8 604
43f8b275 605(defun image-bookmark-make-record ()
f12492c8
TV
606 `(,@(bookmark-make-record-default nil 'no-context 0)
607 (image-type . ,image-type)
608 (handler . image-bookmark-jump)))
137187c8 609
137187c8
TH
610;;;###autoload
611(defun image-bookmark-jump (bmk)
03e26a79 612 ;; This implements the `handler' function interface for record type
e0385bf4 613 ;; returned by `bookmark-make-record-function', which see.
43f8b275
SM
614 (prog1 (bookmark-default-handler bmk)
615 (when (not (string= image-type (bookmark-prop-get bmk 'image-type)))
616 (image-toggle-display))))
97666703 617\f
c7f514b5 618
b4ac6e8c 619;; Not yet implemented.
7102e6d0
WJ
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'.")
629;;
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)
b4ac6e8c
GM
637
638
639;; FIXME this doesn't seem mature yet. Document in manual when it is.
a32d4040
CY
640(defvar image-transform-resize nil
641 "The image resize operation.
642Its 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.
7102e6d0 646 - A number, which is a scale factor (the default size is 1).")
c7f514b5 647
2d21d7f6
WJ
648(defvar image-transform-scale 1.0
649 "The scale factor of the image being displayed.")
650
18af70d0
CY
651(defvar image-transform-rotation 0.0
652 "Rotation angle for the image in the current Image mode buffer.")
c7f514b5 653
2d21d7f6
WJ
654(defvar image-transform-right-angle-fudge 0.0001
655 "Snap distance to a multiple of a right angle.
656There's no deep theory behind the default value, it should just
657be somewhat larger than ImageMagick's MagickEpsilon.")
658
659(defsubst image-transform-width (width height)
660 "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle.
661The 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))))))
670
671;; The following comment and code snippet are from
672;; ImageMagick-6.7.4-4/magick/distort.c
673
c846da43 674;; /* Set the output image geometry to calculated 'best fit'.
2d21d7f6
WJ
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;; }
684
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.
690
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.
693The rotation angle is the value of `image-transform-rotation'.
694Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is
695an \"approximately uniformly\" scaled W x H rectangle, which
696currently means that w is one of floor(s W) + {0, 1, -1} and h is
697floor(s H), where s can be recovered as the value of `image-transform-scale'.
698The value of `image-transform-rotation' may be replaced by
699a slightly different angle. Currently this is done for values
700close 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)
f58e0fd5 703 (cl-assert (not (zerop width)) t)
2d21d7f6
WJ
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)
f58e0fd5 710 (cl-assert (not (zerop height)) t)
2d21d7f6
WJ
711 (setq image-transform-rotation
712 (float (round image-transform-rotation))
713 image-transform-scale (/ (float length) height))
714 (cons nil length))
715 (t
f58e0fd5 716 (cl-assert (not (and (zerop width) (zerop height))) t)
2d21d7f6
WJ
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))
737 w)
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.
745 (t
746 (1- w)))
747 h)))))
748
749(defun image-transform-check-size ()
977f9325
WJ
750 "Check that the image exactly fits the width/height of the window.
751
752Do this for an image of type `imagemagick' to make sure that the
753elisp code matches the way ImageMagick computes the bounding box
754of a rotated image."
755 (when (and (not (numberp image-transform-resize))
756 (boundp 'image-type)
757 (eq image-type 'imagemagick))
2d21d7f6
WJ
758 (let ((size (image-display-size (image-get-display-property) t)))
759 (cond ((eq image-transform-resize 'fit-width)
f58e0fd5 760 (cl-assert (= (car size)
2d21d7f6
WJ
761 (- (nth 2 (window-inside-pixel-edges))
762 (nth 0 (window-inside-pixel-edges))))
763 t))
764 ((eq image-transform-resize 'fit-height)
f58e0fd5 765 (cl-assert (= (cdr size)
2d21d7f6
WJ
766 (- (nth 3 (window-inside-pixel-edges))
767 (nth 1 (window-inside-pixel-edges))))
768 t))))))
769
18af70d0
CY
770(defun image-transform-properties (spec)
771 "Return rescaling/rotation properties for image SPEC.
772These properties are determined by the Image mode variables
773`image-transform-resize' and `image-transform-rotation'. The
774return value is suitable for appending to an image spec.
e8cbec34 775
09e80d9f 776Rescaling and rotation properties only take effect if Emacs is
e8cbec34 777compiled with ImageMagick support."
2d21d7f6 778 (setq image-transform-scale 1.0)
18af70d0 779 (when (or image-transform-resize
2d21d7f6 780 (/= image-transform-rotation 0.0))
18af70d0
CY
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))
2d21d7f6 784 (resized
18af70d0
CY
785 (cond
786 ((numberp image-transform-resize)
7102e6d0 787 (unless (= image-transform-resize 1)
2d21d7f6
WJ
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)))))
18af70d0 795 ((eq image-transform-resize 'fit-height)
2d21d7f6
WJ
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))))))
c7f514b5
JV
807
808(defun image-transform-set-scale (scale)
a32d4040
CY
809 "Prompt for a number, and resize the current image by that amount.
810This command has no effect unless Emacs is compiled with
811ImageMagick support."
812 (interactive "nScale: ")
c183f693 813 (setq image-transform-resize scale)
a32d4040 814 (image-toggle-display-image))
c7f514b5
JV
815
816(defun image-transform-fit-to-height ()
a32d4040
CY
817 "Fit the current image to the height of the current window.
818This command has no effect unless Emacs is compiled with
819ImageMagick support."
c7f514b5 820 (interactive)
a32d4040
CY
821 (setq image-transform-resize 'fit-height)
822 (image-toggle-display-image))
c7f514b5 823
3a50be51 824(defun image-transform-fit-to-width ()
a32d4040
CY
825 "Fit the current image to the width of the current window.
826This command has no effect unless Emacs is compiled with
827ImageMagick support."
3a50be51 828 (interactive)
a32d4040
CY
829 (setq image-transform-resize 'fit-width)
830 (image-toggle-display-image))
c7f514b5
JV
831
832(defun image-transform-set-rotation (rotation)
a32d4040
CY
833 "Prompt for an angle ROTATION, and rotate the image by that amount.
834ROTATION should be in degrees. This command has no effect unless
835Emacs is compiled with ImageMagick support."
836 (interactive "nRotation angle (in degrees): ")
2d21d7f6 837 (setq image-transform-rotation (float (mod rotation 360)))
a32d4040 838 (image-toggle-display-image))
c7f514b5 839
c7bd5d57
RS
840(provide 'image-mode)
841
842;;; image-mode.el ends here