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