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