Commit | Line | Data |
---|---|---|
83600dc8 | 1 | ;;; image-mode.el --- support for visiting image files -*- lexical-binding: t -*- |
c7bd5d57 | 2 | ;; |
ba318903 | 3 | ;; Copyright (C) 2005-2014 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) | |
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. | |
50105835 SM |
72 | (if winprops |
73 | ;; Move window to front. | |
74 | (setq image-mode-winprops-alist | |
75 | (cons winprops (delq winprops image-mode-winprops-alist))) | |
44e3c7c6 SM |
76 | (setq winprops (cons window |
77 | (copy-alist (cdar image-mode-winprops-alist)))) | |
50105835 SM |
78 | ;; Add winprops before running the hook, to avoid inf-loops if the hook |
79 | ;; triggers window-configuration-change-hook. | |
80 | (setq image-mode-winprops-alist | |
81 | (cons winprops image-mode-winprops-alist)) | |
44e3c7c6 | 82 | (run-hook-with-args 'image-mode-new-window-functions winprops)) |
44e3c7c6 SM |
83 | winprops)) |
84 | ||
85 | (defun image-mode-window-get (prop &optional winprops) | |
f58e0fd5 SM |
86 | (declare (gv-setter (lambda (val) |
87 | `(image-mode-window-put ,prop ,val ,winprops)))) | |
44e3c7c6 SM |
88 | (unless (consp winprops) (setq winprops (image-mode-winprops winprops))) |
89 | (cdr (assq prop (cdr winprops)))) | |
90 | ||
44e3c7c6 SM |
91 | (defun image-mode-window-put (prop val &optional winprops) |
92 | (unless (consp winprops) (setq winprops (image-mode-winprops winprops))) | |
9253f7af SM |
93 | (unless (eq t (car winprops)) |
94 | (image-mode-window-put prop val t)) | |
44e3c7c6 SM |
95 | (setcdr winprops (cons (cons prop val) |
96 | (delq (assq prop (cdr winprops)) (cdr winprops))))) | |
97 | ||
98 | (defun image-set-window-vscroll (vscroll) | |
99 | (setf (image-mode-window-get 'vscroll) vscroll) | |
100 | (set-window-vscroll (selected-window) vscroll)) | |
101 | ||
102 | (defun image-set-window-hscroll (ncol) | |
60134fd4 | 103 | (setf (image-mode-window-get 'hscroll) ncol) |
44e3c7c6 SM |
104 | (set-window-hscroll (selected-window) ncol)) |
105 | ||
106 | (defun image-mode-reapply-winprops () | |
6d401b4e SM |
107 | ;; When set-window-buffer, set hscroll and vscroll to what they were |
108 | ;; last time the image was displayed in this window. | |
4fd996b3 SM |
109 | (when (listp image-mode-winprops-alist) |
110 | ;; Beware: this call to image-mode-winprops can't be optimized away, | |
111 | ;; because it not only gets the winprops data but sets it up if needed | |
112 | ;; (e.g. it's used by doc-view to display the image in a new window). | |
b81f0bab | 113 | (let* ((winprops (image-mode-winprops nil t)) |
6d401b4e SM |
114 | (hscroll (image-mode-window-get 'hscroll winprops)) |
115 | (vscroll (image-mode-window-get 'vscroll winprops))) | |
4fd996b3 SM |
116 | (when (image-get-display-property) ;Only do it if we display an image! |
117 | (if hscroll (set-window-hscroll (selected-window) hscroll)) | |
118 | (if vscroll (set-window-vscroll (selected-window) vscroll)))))) | |
36e1c289 | 119 | |
44e3c7c6 SM |
120 | (defun image-mode-setup-winprops () |
121 | ;; Record current scroll settings. | |
122 | (unless (listp image-mode-winprops-alist) | |
123 | (setq image-mode-winprops-alist nil)) | |
124 | (add-hook 'window-configuration-change-hook | |
125 | 'image-mode-reapply-winprops nil t)) | |
126 | ||
127 | ;;; Image scrolling functions | |
128 | ||
91784462 SM |
129 | (defun image-get-display-property () |
130 | (get-char-property (point-min) 'display | |
131 | ;; There might be different images for different displays. | |
132 | (if (eq (window-buffer) (current-buffer)) | |
133 | (selected-window)))) | |
134 | ||
aa360da1 GM |
135 | (declare-function image-size "image.c" (spec &optional pixels frame)) |
136 | ||
c9088194 | 137 | (defun image-display-size (spec &optional pixels frame) |
2f224f0b CY |
138 | "Wrapper around `image-size', handling slice display properties. |
139 | Like `image-size', the return value is (WIDTH . HEIGHT). | |
140 | WIDTH and HEIGHT are in canonical character units if PIXELS is | |
141 | nil, and in pixel units if PIXELS is non-nil. | |
142 | ||
143 | If SPEC is an image display property, this function is equivalent | |
144 | to `image-size'. If SPEC is a list of properties containing | |
145 | `image' and `slice' properties, return the display size taking | |
146 | the slice property into account. If the list contains `image' | |
147 | but not `slice', return the `image-size' of the specified image." | |
c9088194 SK |
148 | (if (eq (car spec) 'image) |
149 | (image-size spec pixels frame) | |
150 | (let ((image (assoc 'image spec)) | |
151 | (slice (assoc 'slice spec))) | |
152 | (cond ((and image slice) | |
153 | (if pixels | |
154 | (cons (nth 3 slice) (nth 4 slice)) | |
155 | (cons (/ (float (nth 3 slice)) (frame-char-width frame)) | |
156 | (/ (float (nth 4 slice)) (frame-char-height frame))))) | |
157 | (image | |
158 | (image-size image pixels frame)) | |
159 | (t | |
160 | (error "Invalid image specification: %s" spec)))))) | |
161 | ||
bb0cb417 CY |
162 | (defun image-forward-hscroll (&optional n) |
163 | "Scroll image in current window to the left by N character widths. | |
164 | Stop if the right edge of the image is reached." | |
165 | (interactive "p") | |
166 | (cond ((= n 0) nil) | |
167 | ((< n 0) | |
44e3c7c6 | 168 | (image-set-window-hscroll (max 0 (+ (window-hscroll) n)))) |
bb0cb417 | 169 | (t |
91784462 | 170 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
171 | (edges (window-inside-edges)) |
172 | (win-width (- (nth 2 edges) (nth 0 edges))) | |
c9088194 | 173 | (img-width (ceiling (car (image-display-size image))))) |
44e3c7c6 | 174 | (image-set-window-hscroll (min (max 0 (- img-width win-width)) |
36e1c289 | 175 | (+ n (window-hscroll)))))))) |
bb0cb417 CY |
176 | |
177 | (defun image-backward-hscroll (&optional n) | |
178 | "Scroll image in current window to the right by N character widths. | |
179 | Stop if the left edge of the image is reached." | |
180 | (interactive "p") | |
181 | (image-forward-hscroll (- n))) | |
182 | ||
77549ea8 | 183 | (defun image-next-line (n) |
bb0cb417 CY |
184 | "Scroll image in current window upward by N lines. |
185 | Stop if the bottom edge of the image is reached." | |
186 | (interactive "p") | |
187 | (cond ((= n 0) nil) | |
188 | ((< n 0) | |
44e3c7c6 | 189 | (image-set-window-vscroll (max 0 (+ (window-vscroll) n)))) |
bb0cb417 | 190 | (t |
91784462 | 191 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
192 | (edges (window-inside-edges)) |
193 | (win-height (- (nth 3 edges) (nth 1 edges))) | |
c9088194 | 194 | (img-height (ceiling (cdr (image-display-size image))))) |
44e3c7c6 | 195 | (image-set-window-vscroll (min (max 0 (- img-height win-height)) |
36e1c289 | 196 | (+ n (window-vscroll)))))))) |
bb0cb417 CY |
197 | |
198 | (defun image-previous-line (&optional n) | |
199 | "Scroll image in current window downward by N lines. | |
200 | Stop if the top edge of the image is reached." | |
201 | (interactive "p") | |
202 | (image-next-line (- n))) | |
203 | ||
204 | (defun image-scroll-up (&optional n) | |
205 | "Scroll image in current window upward by N lines. | |
206 | Stop if the bottom edge of the image is reached. | |
207 | If ARG is omitted or nil, scroll upward by a near full screen. | |
208 | A near full screen is `next-screen-context-lines' less than a full screen. | |
209 | Negative ARG means scroll downward. | |
210 | If ARG is the atom `-', scroll downward by nearly full screen. | |
211 | When calling from a program, supply as argument a number, nil, or `-'." | |
212 | (interactive "P") | |
213 | (cond ((null n) | |
214 | (let* ((edges (window-inside-edges)) | |
215 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
216 | (image-next-line | |
217 | (max 0 (- win-height next-screen-context-lines))))) | |
218 | ((eq n '-) | |
219 | (let* ((edges (window-inside-edges)) | |
220 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
221 | (image-next-line | |
222 | (min 0 (- next-screen-context-lines win-height))))) | |
223 | (t (image-next-line (prefix-numeric-value n))))) | |
224 | ||
225 | (defun image-scroll-down (&optional n) | |
3488c456 | 226 | "Scroll image in current window downward by N lines. |
bb0cb417 CY |
227 | Stop if the top edge of the image is reached. |
228 | If ARG is omitted or nil, scroll downward by a near full screen. | |
229 | A near full screen is `next-screen-context-lines' less than a full screen. | |
230 | Negative ARG means scroll upward. | |
231 | If ARG is the atom `-', scroll upward by nearly full screen. | |
232 | When calling from a program, supply as argument a number, nil, or `-'." | |
233 | (interactive "P") | |
234 | (cond ((null n) | |
235 | (let* ((edges (window-inside-edges)) | |
236 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
237 | (image-next-line | |
238 | (min 0 (- next-screen-context-lines win-height))))) | |
239 | ((eq n '-) | |
240 | (let* ((edges (window-inside-edges)) | |
241 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
242 | (image-next-line | |
243 | (max 0 (- win-height next-screen-context-lines))))) | |
244 | (t (image-next-line (- (prefix-numeric-value n)))))) | |
245 | ||
246 | (defun image-bol (arg) | |
247 | "Scroll horizontally to the left 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))) | |
44e3c7c6 | 254 | (image-set-window-hscroll 0)) |
bb0cb417 CY |
255 | |
256 | (defun image-eol (arg) | |
257 | "Scroll horizontally to the right edge of the image in the current window. | |
258 | With argument ARG not nil or 1, move forward ARG - 1 lines first, | |
259 | stopping if the top or bottom edge of the image is reached." | |
260 | (interactive "p") | |
261 | (and arg | |
262 | (/= (setq arg (prefix-numeric-value arg)) 1) | |
263 | (image-next-line (- arg 1))) | |
91784462 | 264 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
265 | (edges (window-inside-edges)) |
266 | (win-width (- (nth 2 edges) (nth 0 edges))) | |
c9088194 | 267 | (img-width (ceiling (car (image-display-size image))))) |
44e3c7c6 | 268 | (image-set-window-hscroll (max 0 (- img-width win-width))))) |
bb0cb417 CY |
269 | |
270 | (defun image-bob () | |
271 | "Scroll to the top-left corner of the image in the current window." | |
272 | (interactive) | |
44e3c7c6 SM |
273 | (image-set-window-hscroll 0) |
274 | (image-set-window-vscroll 0)) | |
bb0cb417 CY |
275 | |
276 | (defun image-eob () | |
277 | "Scroll to the bottom-right corner of the image in the current window." | |
278 | (interactive) | |
91784462 | 279 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
280 | (edges (window-inside-edges)) |
281 | (win-width (- (nth 2 edges) (nth 0 edges))) | |
c9088194 | 282 | (img-width (ceiling (car (image-display-size image)))) |
bb0cb417 | 283 | (win-height (- (nth 3 edges) (nth 1 edges))) |
c9088194 | 284 | (img-height (ceiling (cdr (image-display-size image))))) |
44e3c7c6 SM |
285 | (image-set-window-hscroll (max 0 (- img-width win-width))) |
286 | (image-set-window-vscroll (max 0 (- img-height win-height))))) | |
bb0cb417 | 287 | |
bd1d6a63 SM |
288 | ;; Adjust frame and image size. |
289 | ||
2a43515a CY |
290 | (defun image-mode-fit-frame (&optional frame toggle) |
291 | "Fit FRAME to the current image. | |
292 | If FRAME is omitted or nil, it defaults to the selected frame. | |
293 | All other windows on the frame are deleted. | |
294 | ||
295 | If called interactively, or if TOGGLE is non-nil, toggle between | |
296 | fitting FRAME to the current image and restoring the size and | |
297 | window configuration prior to the last `image-mode-fit-frame' | |
298 | call." | |
299 | (interactive (list nil t)) | |
300 | (let* ((buffer (current-buffer)) | |
bd1d6a63 | 301 | (display (image-get-display-property)) |
2a43515a CY |
302 | (size (image-display-size display)) |
303 | (saved (frame-parameter frame 'image-mode-saved-params)) | |
304 | (window-configuration (current-window-configuration frame)) | |
305 | (width (frame-width frame)) | |
306 | (height (frame-height frame))) | |
307 | (with-selected-frame (or frame (selected-frame)) | |
308 | (if (and toggle saved | |
309 | (= (caar saved) width) | |
310 | (= (cdar saved) height)) | |
311 | (progn | |
312 | (set-frame-width frame (car (nth 1 saved))) | |
313 | (set-frame-height frame (cdr (nth 1 saved))) | |
314 | (set-window-configuration (nth 2 saved)) | |
315 | (set-frame-parameter frame 'image-mode-saved-params nil)) | |
316 | (delete-other-windows) | |
317 | (switch-to-buffer buffer t t) | |
318 | (let* ((edges (window-inside-edges)) | |
319 | (inner-width (- (nth 2 edges) (nth 0 edges))) | |
320 | (inner-height (- (nth 3 edges) (nth 1 edges)))) | |
321 | (set-frame-width frame (+ (ceiling (car size)) | |
322 | width (- inner-width))) | |
323 | (set-frame-height frame (+ (ceiling (cdr size)) | |
324 | height (- inner-height))) | |
325 | ;; The frame size after the above `set-frame-*' calls may | |
326 | ;; differ from what we specified, due to window manager | |
327 | ;; interference. We have to call `frame-width' and | |
328 | ;; `frame-height' to get the actual results. | |
329 | (set-frame-parameter frame 'image-mode-saved-params | |
330 | (list (cons (frame-width) | |
331 | (frame-height)) | |
332 | (cons width height) | |
333 | window-configuration))))))) | |
bd1d6a63 | 334 | |
bb0cb417 CY |
335 | ;;; Image Mode setup |
336 | ||
4fd996b3 | 337 | (defvar-local image-type nil |
71d73c9c | 338 | "The image type for the current Image mode buffer.") |
d487ca7d | 339 | |
dc504515 GM |
340 | (defvar-local image-multi-frame nil |
341 | "Non-nil if image for the current Image mode buffer has multiple frames.") | |
342 | ||
9b9debd1 JL |
343 | (defvar image-mode-previous-major-mode nil |
344 | "Internal variable to keep the previous non-image major mode.") | |
345 | ||
c7bd5d57 | 346 | (defvar image-mode-map |
bb0cb417 | 347 | (let ((map (make-sparse-keymap))) |
abef340a | 348 | (set-keymap-parent map special-mode-map) |
bb0cb417 | 349 | (define-key map "\C-c\C-c" 'image-toggle-display) |
42c27c2a | 350 | (define-key map (kbd "SPC") 'image-scroll-up) |
9cec74cf | 351 | (define-key map (kbd "S-SPC") 'image-scroll-down) |
42c27c2a | 352 | (define-key map (kbd "DEL") 'image-scroll-down) |
18af70d0 | 353 | (define-key map (kbd "RET") 'image-toggle-animation) |
c0211c4e GM |
354 | (define-key map "F" 'image-goto-frame) |
355 | (define-key map "f" 'image-next-frame) | |
356 | (define-key map "b" 'image-previous-frame) | |
20de6ab6 CW |
357 | (define-key map "n" 'image-next-file) |
358 | (define-key map "p" 'image-previous-file) | |
3a2ddc2d GM |
359 | (define-key map "a+" 'image-increase-speed) |
360 | (define-key map "a-" 'image-decrease-speed) | |
361 | (define-key map "a0" 'image-reset-speed) | |
362 | (define-key map "ar" 'image-reverse-speed) | |
bb0cb417 CY |
363 | (define-key map [remap forward-char] 'image-forward-hscroll) |
364 | (define-key map [remap backward-char] 'image-backward-hscroll) | |
d8b0cddd SM |
365 | (define-key map [remap right-char] 'image-forward-hscroll) |
366 | (define-key map [remap left-char] 'image-backward-hscroll) | |
bb0cb417 CY |
367 | (define-key map [remap previous-line] 'image-previous-line) |
368 | (define-key map [remap next-line] 'image-next-line) | |
369 | (define-key map [remap scroll-up] 'image-scroll-up) | |
370 | (define-key map [remap scroll-down] 'image-scroll-down) | |
32129746 JL |
371 | (define-key map [remap scroll-up-command] 'image-scroll-up) |
372 | (define-key map [remap scroll-down-command] 'image-scroll-down) | |
bb0cb417 CY |
373 | (define-key map [remap move-beginning-of-line] 'image-bol) |
374 | (define-key map [remap move-end-of-line] 'image-eol) | |
375 | (define-key map [remap beginning-of-buffer] 'image-bob) | |
376 | (define-key map [remap end-of-buffer] 'image-eob) | |
783b7b75 GM |
377 | (easy-menu-define image-mode-menu map "Menu for Image mode." |
378 | '("Image" | |
379 | ["Show as Text" image-toggle-display :active t | |
380 | :help "Show image as text"] | |
381 | "--" | |
382 | ["Fit Frame to Image" image-mode-fit-frame :active t | |
383 | :help "Resize frame to match image"] | |
384 | ["Fit to Window Height" image-transform-fit-to-height | |
385 | :visible (eq image-type 'imagemagick) | |
386 | :help "Resize image to match the window height"] | |
387 | ["Fit to Window Width" image-transform-fit-to-width | |
388 | :visible (eq image-type 'imagemagick) | |
389 | :help "Resize image to match the window width"] | |
390 | ["Rotate Image..." image-transform-set-rotation | |
391 | :visible (eq image-type 'imagemagick) | |
392 | :help "Rotate the image"] | |
393 | "--" | |
f05e2ff2 GM |
394 | ["Show Thumbnails" |
395 | (lambda () | |
396 | (interactive) | |
397 | (image-dired default-directory)) | |
398 | :active default-directory | |
399 | :help "Show thumbnails for all images in this directory"] | |
400 | ["Next Image" image-next-file :active buffer-file-name | |
783b7b75 | 401 | :help "Move to next image in this directory"] |
f05e2ff2 | 402 | ["Previous Image" image-previous-file :active buffer-file-name |
783b7b75 GM |
403 | :help "Move to previous image in this directory"] |
404 | "--" | |
405 | ["Animate Image" image-toggle-animation :style toggle | |
406 | :selected (let ((image (image-get-display-property))) | |
407 | (and image (image-animate-timer image))) | |
dc504515 | 408 | :active image-multi-frame |
783b7b75 GM |
409 | :help "Toggle image animation"] |
410 | ["Loop Animation" | |
411 | (lambda () (interactive) | |
783b7b75 GM |
412 | (setq image-animate-loop (not image-animate-loop)) |
413 | ;; FIXME this is a hacky way to make it affect a currently | |
414 | ;; animating image. | |
415 | (when (let ((image (image-get-display-property))) | |
416 | (and image (image-animate-timer image))) | |
417 | (image-toggle-animation) | |
418 | (image-toggle-animation))) | |
419 | :style toggle :selected image-animate-loop | |
dc504515 | 420 | :active image-multi-frame |
3a2ddc2d GM |
421 | :help "Animate images once, or forever?"] |
422 | ["Reverse Animation" image-reverse-speed | |
423 | :style toggle :selected (let ((image (image-get-display-property))) | |
424 | (and image (< | |
425 | (image-animate-get-speed image) | |
426 | 0))) | |
427 | :active image-multi-frame | |
428 | :help "Reverse direction of this image's animation?"] | |
429 | ["Speed Up Animation" image-increase-speed | |
430 | :active image-multi-frame | |
431 | :help "Speed up this image's animation"] | |
432 | ["Slow Down Animation" image-decrease-speed | |
433 | :active image-multi-frame | |
434 | :help "Slow down this image's animation"] | |
435 | ["Reset Animation Speed" image-reset-speed | |
436 | :active image-multi-frame | |
437 | :help "Reset the speed of this image's animation"] | |
dc504515 | 438 | ["Next Frame" image-next-frame :active image-multi-frame |
783b7b75 | 439 | :help "Show the next frame of this image"] |
dc504515 | 440 | ["Previous Frame" image-previous-frame :active image-multi-frame |
783b7b75 | 441 | :help "Show the previous frame of this image"] |
dc504515 | 442 | ["Goto Frame..." image-goto-frame :active image-multi-frame |
783b7b75 GM |
443 | :help "Show a specific frame of this image"] |
444 | )) | |
bb0cb417 | 445 | map) |
71d73c9c | 446 | "Mode keymap for `image-mode'.") |
bb0cb417 | 447 | |
9b9debd1 | 448 | (defvar image-minor-mode-map |
c7bd5d57 RS |
449 | (let ((map (make-sparse-keymap))) |
450 | (define-key map "\C-c\C-c" 'image-toggle-display) | |
451 | map) | |
71d73c9c | 452 | "Mode keymap for `image-minor-mode'.") |
c7bd5d57 | 453 | |
e0385bf4 | 454 | (defvar bookmark-make-record-function) |
a47d49c0 | 455 | |
0d17c4b9 JL |
456 | (put 'image-mode 'mode-class 'special) |
457 | ||
c7bd5d57 RS |
458 | ;;;###autoload |
459 | (defun image-mode () | |
460 | "Major mode for image files. | |
461 | You can use \\<image-mode-map>\\[image-toggle-display] | |
3a2ddc2d GM |
462 | to toggle between display as an image and display as text. |
463 | ||
464 | Key bindings: | |
465 | \\{image-mode-map}" | |
c7bd5d57 | 466 | (interactive) |
9b9debd1 JL |
467 | (condition-case err |
468 | (progn | |
469 | (unless (display-images-p) | |
470 | (error "Display does not support images")) | |
471 | ||
472 | (kill-all-local-variables) | |
473 | (setq major-mode 'image-mode) | |
474 | ||
475 | (if (not (image-get-display-property)) | |
476 | (progn | |
477 | (image-toggle-display-image) | |
478 | ;; If attempt to display the image fails. | |
479 | (if (not (image-get-display-property)) | |
480 | (error "Invalid image"))) | |
481 | ;; Set next vars when image is already displayed but local | |
482 | ;; variables were cleared by kill-all-local-variables | |
483 | (setq cursor-type nil truncate-lines t | |
484 | image-type (plist-get (cdr (image-get-display-property)) :type))) | |
485 | ||
486 | (setq mode-name (if image-type (format "Image[%s]" image-type) "Image")) | |
bb43424b | 487 | (use-local-map image-mode-map) |
9b9debd1 JL |
488 | |
489 | ;; Use our own bookmarking function for images. | |
4fd996b3 SM |
490 | (setq-local bookmark-make-record-function |
491 | #'image-bookmark-make-record) | |
9b9debd1 JL |
492 | |
493 | ;; Keep track of [vh]scroll when switching buffers | |
494 | (image-mode-setup-winprops) | |
495 | ||
496 | (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t) | |
0fb1193d | 497 | (add-hook 'after-revert-hook 'image-after-revert-hook nil t) |
9b9debd1 | 498 | (run-mode-hooks 'image-mode-hook) |
18af70d0 CY |
499 | (let ((image (image-get-display-property)) |
500 | (msg1 (substitute-command-keys | |
bb9dfee1 GM |
501 | "Type \\[image-toggle-display] to view the image as ")) |
502 | animated) | |
18af70d0 CY |
503 | (cond |
504 | ((null image) | |
505 | (message "%s" (concat msg1 "an image."))) | |
ed8d7fca | 506 | ((setq animated (image-multi-frame-p image)) |
dc504515 | 507 | (setq image-multi-frame t |
bb9dfee1 | 508 | mode-line-process |
5db881d0 GM |
509 | `(:eval |
510 | (concat " " | |
511 | (propertize | |
512 | (format "[%s/%s]" | |
513 | (1+ (image-current-frame ',image)) | |
514 | ,(car animated)) | |
515 | 'help-echo "Frames | |
516 | mouse-1: Next frame | |
517 | mouse-3: Previous frame" | |
518 | 'mouse-face 'mode-line-highlight | |
519 | 'local-map | |
520 | '(keymap | |
521 | (mode-line | |
522 | keymap | |
523 | (down-mouse-1 . image-next-frame) | |
524 | (down-mouse-3 . image-previous-frame))))))) | |
18af70d0 | 525 | (message "%s" |
ed8d7fca GM |
526 | (concat msg1 "text. This image has multiple frames."))) |
527 | ;;; (substitute-command-keys | |
528 | ;;; "\\[image-toggle-animation] to animate.")))) | |
18af70d0 CY |
529 | (t |
530 | (message "%s" (concat msg1 "text.")))))) | |
531 | ||
9b9debd1 JL |
532 | (error |
533 | (image-mode-as-text) | |
534 | (funcall | |
535 | (if (called-interactively-p 'any) 'error 'message) | |
536 | "Cannot display image: %s" (cdr err))))) | |
18af70d0 | 537 | |
d90e651e JL |
538 | ;;;###autoload |
539 | (define-minor-mode image-minor-mode | |
06e21633 CY |
540 | "Toggle Image minor mode in this buffer. |
541 | With a prefix argument ARG, enable Image minor mode if ARG is | |
542 | positive, and disable it otherwise. If called from Lisp, enable | |
543 | the mode if ARG is omitted or nil. | |
544 | ||
545 | Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display], | |
546 | to switch back to `image-mode' and display an image file as the | |
547 | actual image." | |
9b9debd1 JL |
548 | nil (:eval (if image-type (format " Image[%s]" image-type) " Image")) |
549 | image-minor-mode-map | |
d90e651e JL |
550 | :group 'image |
551 | :version "22.1" | |
9b9debd1 JL |
552 | (if image-minor-mode |
553 | (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t))) | |
d90e651e JL |
554 | |
555 | ;;;###autoload | |
9b9debd1 JL |
556 | (defun image-mode-as-text () |
557 | "Set a non-image mode as major mode in combination with image minor mode. | |
558 | A non-image major mode found from `auto-mode-alist' or Fundamental mode | |
559 | displays an image file as text. `image-minor-mode' provides the key | |
560 | \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode' | |
561 | to display an image file as the actual image. | |
562 | ||
563 | You can use `image-mode-as-text' in `auto-mode-alist' when you want | |
316d12fb | 564 | to display an image file as text initially. |
9b9debd1 JL |
565 | |
566 | See commands `image-mode' and `image-minor-mode' for more information | |
567 | on these modes." | |
d90e651e | 568 | (interactive) |
9b9debd1 JL |
569 | ;; image-mode-as-text = normal-mode + image-minor-mode |
570 | (let ((previous-image-type image-type)) ; preserve `image-type' | |
571 | (if image-mode-previous-major-mode | |
572 | ;; Restore previous major mode that was already found by this | |
573 | ;; function and cached in `image-mode-previous-major-mode' | |
574 | (funcall image-mode-previous-major-mode) | |
575 | (let ((auto-mode-alist | |
576 | (delq nil (mapcar | |
577 | (lambda (elt) | |
578 | (unless (memq (or (car-safe (cdr elt)) (cdr elt)) | |
579 | '(image-mode image-mode-maybe image-mode-as-text)) | |
580 | elt)) | |
581 | auto-mode-alist))) | |
582 | (magic-fallback-mode-alist | |
583 | (delq nil (mapcar | |
584 | (lambda (elt) | |
585 | (unless (memq (or (car-safe (cdr elt)) (cdr elt)) | |
586 | '(image-mode image-mode-maybe image-mode-as-text)) | |
587 | elt)) | |
588 | magic-fallback-mode-alist)))) | |
589 | (normal-mode) | |
4fd996b3 | 590 | (setq-local image-mode-previous-major-mode major-mode))) |
9b9debd1 JL |
591 | ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'. |
592 | (setq image-type previous-image-type) | |
593 | ;; Enable image minor mode with `C-c C-c'. | |
594 | (image-minor-mode 1) | |
595 | ;; Show the image file as text. | |
596 | (image-toggle-display-text) | |
597 | (message "%s" (concat | |
598 | (substitute-command-keys | |
599 | "Type \\[image-toggle-display] to view the image as ") | |
600 | (if (image-get-display-property) | |
601 | "text" "an image") ".")))) | |
602 | ||
603 | (define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2") | |
d90e651e JL |
604 | |
605 | (defun image-toggle-display-text () | |
9b9debd1 JL |
606 | "Show the image file as text. |
607 | Remove text properties that display the image." | |
608 | (let ((inhibit-read-only t) | |
609 | (buffer-undo-list t) | |
610 | (modified (buffer-modified-p))) | |
611 | (remove-list-of-text-properties (point-min) (point-max) | |
345083b2 | 612 | '(display read-nonsticky ;; intangible |
9b9debd1 JL |
613 | read-only front-sticky)) |
614 | (set-buffer-modified-p modified) | |
615 | (if (called-interactively-p 'any) | |
616 | (message "Repeat this command to go back to displaying the image")))) | |
c7bd5d57 | 617 | |
f2920ffe RS |
618 | (defvar archive-superior-buffer) |
619 | (defvar tar-superior-buffer) | |
110683ad | 620 | (declare-function image-flush "image.c" (spec &optional frame)) |
f2920ffe | 621 | |
9b9debd1 JL |
622 | (defun image-toggle-display-image () |
623 | "Show the image of the image file. | |
624 | Turn the image data into a real image, but only if the whole file | |
625 | was inserted." | |
953a8c3b | 626 | (unless (derived-mode-p 'image-mode) |
a32d4040 | 627 | (error "The buffer is not in Image mode")) |
9b9debd1 JL |
628 | (let* ((filename (buffer-file-name)) |
629 | (data-p (not (and filename | |
630 | (file-readable-p filename) | |
631 | (not (file-remote-p filename)) | |
632 | (not (buffer-modified-p)) | |
633 | (not (and (boundp 'archive-superior-buffer) | |
634 | archive-superior-buffer)) | |
635 | (not (and (boundp 'tar-superior-buffer) | |
636 | tar-superior-buffer))))) | |
637 | (file-or-data (if data-p | |
638 | (string-make-unibyte | |
639 | (buffer-substring-no-properties (point-min) (point-max))) | |
640 | filename)) | |
641 | (type (image-type file-or-data nil data-p)) | |
e8cbec34 CY |
642 | (image (create-image file-or-data type data-p)) |
643 | (inhibit-read-only t) | |
644 | (buffer-undo-list t) | |
645 | (modified (buffer-modified-p)) | |
646 | props) | |
647 | ||
18af70d0 CY |
648 | ;; Discard any stale image data before looking it up again. |
649 | (image-flush image) | |
e8cbec34 CY |
650 | (setq image (append image (image-transform-properties image))) |
651 | (setq props | |
9b9debd1 | 652 | `(display ,image |
345083b2 SM |
653 | ;; intangible ,image |
654 | rear-nonsticky (display) ;; intangible | |
9b9debd1 | 655 | read-only t front-sticky (read-only))) |
e8cbec34 | 656 | |
9b9debd1 JL |
657 | (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file |
658 | (add-text-properties (point-min) (point-max) props) | |
659 | (restore-buffer-modified-p modified)) | |
660 | ;; Inhibit the cursor when the buffer contains only an image, | |
661 | ;; because cursors look very strange on top of images. | |
662 | (setq cursor-type nil) | |
663 | ;; This just makes the arrow displayed in the right fringe | |
664 | ;; area look correct when the image is wider than the window. | |
665 | (setq truncate-lines t) | |
4114ed61 EZ |
666 | ;; Disable adding a newline at the end of the image file when it |
667 | ;; is written with, e.g., C-x C-w. | |
668 | (if (coding-system-equal (coding-system-base buffer-file-coding-system) | |
669 | 'no-conversion) | |
4fd996b3 SM |
670 | (setq-local find-file-literally t)) |
671 | ;; Allow navigation of large images. | |
672 | (setq-local auto-hscroll-mode nil) | |
9b9debd1 JL |
673 | (setq image-type type) |
674 | (if (eq major-mode 'image-mode) | |
675 | (setq mode-name (format "Image[%s]" type))) | |
2d21d7f6 | 676 | (image-transform-check-size) |
9b9debd1 JL |
677 | (if (called-interactively-p 'any) |
678 | (message "Repeat this command to go back to displaying the file as text")))) | |
679 | ||
c7bd5d57 | 680 | (defun image-toggle-display () |
71d73c9c CY |
681 | "Toggle between image and text display. |
682 | If the current buffer is displaying an image file as an image, | |
683 | call `image-mode-as-text' to switch to text. Otherwise, display | |
684 | the image by calling `image-mode'." | |
c7bd5d57 | 685 | (interactive) |
91784462 | 686 | (if (image-get-display-property) |
9b9debd1 JL |
687 | (image-mode-as-text) |
688 | (image-mode))) | |
0fb1193d JL |
689 | |
690 | (defun image-after-revert-hook () | |
691 | (when (image-get-display-property) | |
692 | (image-toggle-display-text) | |
693 | ;; Update image display. | |
637d46ca GM |
694 | (mapc (lambda (window) (redraw-frame (window-frame window))) |
695 | (get-buffer-window-list (current-buffer) 'nomini 'visible)) | |
0fb1193d JL |
696 | (image-toggle-display-image))) |
697 | ||
97666703 | 698 | \f |
18af70d0 CY |
699 | ;;; Animated images |
700 | ||
701 | (defcustom image-animate-loop nil | |
eea14f31 | 702 | "Non-nil means animated images loop forever, rather than playing once." |
18af70d0 CY |
703 | :type 'boolean |
704 | :version "24.1" | |
705 | :group 'image) | |
706 | ||
707 | (defun image-toggle-animation () | |
eea14f31 GM |
708 | "Start or stop animating the current image. |
709 | If `image-animate-loop' is non-nil, animation loops forever. | |
710 | Otherwise it plays once, then stops." | |
18af70d0 CY |
711 | (interactive) |
712 | (let ((image (image-get-display-property)) | |
713 | animation) | |
714 | (cond | |
715 | ((null image) | |
716 | (error "No image is present")) | |
ed8d7fca | 717 | ((null (setq animation (image-multi-frame-p image))) |
18af70d0 CY |
718 | (message "No image animation.")) |
719 | (t | |
720 | (let ((timer (image-animate-timer image))) | |
721 | (if timer | |
722 | (cancel-timer timer) | |
723 | (let ((index (plist-get (cdr image) :index))) | |
724 | ;; If we're at the end, restart. | |
725 | (and index | |
726 | (>= index (1- (car animation))) | |
727 | (setq index nil)) | |
728 | (image-animate image index | |
729 | (if image-animate-loop t))))))))) | |
730 | ||
3a2ddc2d GM |
731 | (defun image--set-speed (speed &optional multiply) |
732 | "Set speed of an animated image to SPEED. | |
733 | If MULTIPLY is non-nil, treat SPEED as a multiplication factor. | |
734 | If SPEED is `reset', reset the magnitude of the speed to 1." | |
735 | (let ((image (image-get-display-property))) | |
736 | (cond | |
737 | ((null image) | |
738 | (error "No image is present")) | |
739 | ((null image-multi-frame) | |
740 | (message "No image animation.")) | |
741 | (t | |
742 | (if (eq speed 'reset) | |
743 | (setq speed (if (< (image-animate-get-speed image) 0) | |
744 | -1 1) | |
745 | multiply nil)) | |
746 | (image-animate-set-speed image speed multiply) | |
747 | ;; FIXME Hack to refresh an active image. | |
748 | (when (image-animate-timer image) | |
749 | (image-toggle-animation) | |
750 | (image-toggle-animation)) | |
751 | (message "Image speed is now %s" (image-animate-get-speed image)))))) | |
752 | ||
753 | (defun image-increase-speed () | |
754 | "Increase the speed of current animated image by a factor of 2." | |
755 | (interactive) | |
756 | (image--set-speed 2 t)) | |
757 | ||
758 | (defun image-decrease-speed () | |
759 | "Decrease the speed of current animated image by a factor of 2." | |
760 | (interactive) | |
761 | (image--set-speed 0.5 t)) | |
762 | ||
763 | (defun image-reverse-speed () | |
764 | "Reverse the animation of the current image." | |
765 | (interactive) | |
766 | (image--set-speed -1 t)) | |
767 | ||
768 | (defun image-reset-speed () | |
769 | "Reset the animation speed of the current image." | |
770 | (interactive) | |
771 | (image--set-speed 'reset)) | |
772 | ||
c0211c4e GM |
773 | (defun image-goto-frame (n &optional relative) |
774 | "Show frame N of a multi-frame image. | |
775 | Optional argument OFFSET non-nil means interpret N as relative to the | |
776 | current frame. Frames are indexed from 1." | |
777 | (interactive | |
778 | (list (or current-prefix-arg | |
779 | (read-number "Show frame number: ")))) | |
783b7b75 | 780 | (let ((image (image-get-display-property))) |
c0211c4e GM |
781 | (cond |
782 | ((null image) | |
783 | (error "No image is present")) | |
dc504515 | 784 | ((null image-multi-frame) |
c0211c4e GM |
785 | (message "No image animation.")) |
786 | (t | |
dc504515 GM |
787 | (image-show-frame image |
788 | (if relative | |
789 | (+ n (image-current-frame image)) | |
790 | (1- n))))))) | |
c0211c4e GM |
791 | |
792 | (defun image-next-frame (&optional n) | |
793 | "Switch to the next frame of a multi-frame image. | |
794 | With optional argument N, switch to the Nth frame after the current one. | |
795 | If N is negative, switch to the Nth frame before the current one." | |
796 | (interactive "p") | |
797 | (image-goto-frame n t)) | |
798 | ||
799 | (defun image-previous-frame (&optional n) | |
800 | "Switch to the previous frame of a multi-frame image. | |
801 | With optional argument N, switch to the Nth frame before the current one. | |
802 | If N is negative, switch to the Nth frame after the current one." | |
803 | (interactive "p") | |
804 | (image-next-frame (- n))) | |
805 | ||
18af70d0 | 806 | \f |
20de6ab6 CW |
807 | ;;; Switching to the next/previous image |
808 | ||
809 | (defun image-next-file (&optional n) | |
810 | "Visit the next image in the same directory as the current image file. | |
811 | With optional argument N, visit the Nth image file after the | |
812 | current one, in cyclic alphabetical order. | |
813 | ||
814 | This command visits the specified file via `find-alternate-file', | |
815 | replacing the current Image mode buffer." | |
816 | (interactive "p") | |
817 | (unless (derived-mode-p 'image-mode) | |
818 | (error "The buffer is not in Image mode")) | |
819 | (unless buffer-file-name | |
820 | (error "The current image is not associated with a file")) | |
821 | (let* ((file (file-name-nondirectory buffer-file-name)) | |
822 | (images (image-mode--images-in-directory file)) | |
823 | (idx 0)) | |
824 | (catch 'image-visit-next-file | |
825 | (dolist (f images) | |
826 | (if (string= f file) | |
827 | (throw 'image-visit-next-file (1+ idx))) | |
828 | (setq idx (1+ idx)))) | |
829 | (setq idx (mod (+ idx (or n 1)) (length images))) | |
830 | (find-alternate-file (nth idx images)))) | |
831 | ||
832 | (defun image-previous-file (&optional n) | |
833 | "Visit the preceding image in the same directory as the current file. | |
834 | With optional argument N, visit the Nth image file preceding the | |
835 | current one, in cyclic alphabetical order. | |
836 | ||
837 | This command visits the specified file via `find-alternate-file', | |
838 | replacing the current Image mode buffer." | |
839 | (interactive "p") | |
840 | (image-next-file (- n))) | |
841 | ||
842 | (defun image-mode--images-in-directory (file) | |
843 | (let* ((dir (file-name-directory buffer-file-name)) | |
844 | (files (directory-files dir nil | |
845 | (image-file-name-regexp) t))) | |
846 | ;; Add the current file to the list of images if necessary, in | |
847 | ;; case it does not match `image-file-name-regexp'. | |
848 | (unless (member file files) | |
849 | (push file files)) | |
850 | (sort files 'string-lessp))) | |
851 | ||
852 | \f | |
137187c8 | 853 | ;;; Support for bookmark.el |
e44fa724 KF |
854 | (declare-function bookmark-make-record-default |
855 | "bookmark" (&optional no-file no-context posn)) | |
43f8b275 SM |
856 | (declare-function bookmark-prop-get "bookmark" (bookmark prop)) |
857 | (declare-function bookmark-default-handler "bookmark" (bmk)) | |
137187c8 | 858 | |
43f8b275 | 859 | (defun image-bookmark-make-record () |
f12492c8 TV |
860 | `(,@(bookmark-make-record-default nil 'no-context 0) |
861 | (image-type . ,image-type) | |
862 | (handler . image-bookmark-jump))) | |
137187c8 | 863 | |
137187c8 TH |
864 | ;;;###autoload |
865 | (defun image-bookmark-jump (bmk) | |
03e26a79 | 866 | ;; This implements the `handler' function interface for record type |
e0385bf4 | 867 | ;; returned by `bookmark-make-record-function', which see. |
43f8b275 SM |
868 | (prog1 (bookmark-default-handler bmk) |
869 | (when (not (string= image-type (bookmark-prop-get bmk 'image-type))) | |
870 | (image-toggle-display)))) | |
97666703 | 871 | \f |
c7f514b5 | 872 | |
b4ac6e8c | 873 | ;; Not yet implemented. |
7102e6d0 WJ |
874 | ;; (defvar image-transform-minor-mode-map |
875 | ;; (let ((map (make-sparse-keymap))) | |
876 | ;; ;; (define-key map [(control ?+)] 'image-scale-in) | |
877 | ;; ;; (define-key map [(control ?-)] 'image-scale-out) | |
878 | ;; ;; (define-key map [(control ?=)] 'image-scale-none) | |
879 | ;; ;; (define-key map "c f h" 'image-scale-fit-height) | |
880 | ;; ;; (define-key map "c ]" 'image-rotate-right) | |
881 | ;; map) | |
882 | ;; "Minor mode keymap `image-transform-mode'.") | |
883 | ;; | |
884 | ;; (define-minor-mode image-transform-mode | |
885 | ;; "Minor mode for scaling and rotating images. | |
886 | ;; With a prefix argument ARG, enable the mode if ARG is positive, | |
887 | ;; and disable it otherwise. If called from Lisp, enable the mode | |
888 | ;; if ARG is omitted or nil. This minor mode requires Emacs to have | |
889 | ;; been compiled with ImageMagick support." | |
890 | ;; nil "image-transform" image-transform-minor-mode-map) | |
b4ac6e8c GM |
891 | |
892 | ||
893 | ;; FIXME this doesn't seem mature yet. Document in manual when it is. | |
a32d4040 CY |
894 | (defvar image-transform-resize nil |
895 | "The image resize operation. | |
896 | Its value should be one of the following: | |
897 | - nil, meaning no resizing. | |
898 | - `fit-height', meaning to fit the image to the window height. | |
899 | - `fit-width', meaning to fit the image to the window width. | |
7102e6d0 | 900 | - A number, which is a scale factor (the default size is 1).") |
c7f514b5 | 901 | |
2d21d7f6 WJ |
902 | (defvar image-transform-scale 1.0 |
903 | "The scale factor of the image being displayed.") | |
904 | ||
18af70d0 CY |
905 | (defvar image-transform-rotation 0.0 |
906 | "Rotation angle for the image in the current Image mode buffer.") | |
c7f514b5 | 907 | |
2d21d7f6 WJ |
908 | (defvar image-transform-right-angle-fudge 0.0001 |
909 | "Snap distance to a multiple of a right angle. | |
910 | There's no deep theory behind the default value, it should just | |
911 | be somewhat larger than ImageMagick's MagickEpsilon.") | |
912 | ||
913 | (defsubst image-transform-width (width height) | |
914 | "Return the bounding box width of a rotated WIDTH x HEIGHT rectangle. | |
915 | The rotation angle is the value of `image-transform-rotation' in degrees." | |
916 | (let ((angle (degrees-to-radians image-transform-rotation))) | |
917 | ;; Assume, w.l.o.g., that the vertices of the rectangle have the | |
918 | ;; coordinates (+-w/2, +-h/2) and that (0, 0) is the center of the | |
919 | ;; rotation by the angle A. The projections onto the first axis | |
920 | ;; of the vertices of the rotated rectangle are +- (w/2) cos A +- | |
921 | ;; (h/2) sin A, and the difference between the largest and the | |
922 | ;; smallest of the four values is the expression below. | |
923 | (+ (* width (abs (cos angle))) (* height (abs (sin angle)))))) | |
924 | ||
925 | ;; The following comment and code snippet are from | |
926 | ;; ImageMagick-6.7.4-4/magick/distort.c | |
927 | ||
c846da43 | 928 | ;; /* Set the output image geometry to calculated 'best fit'. |
2d21d7f6 WJ |
929 | ;; Yes this tends to 'over do' the file image size, ON PURPOSE! |
930 | ;; Do not do this for DePolar which needs to be exact for virtual tiling. | |
931 | ;; */ | |
932 | ;; if ( fix_bounds ) { | |
933 | ;; geometry.x = (ssize_t) floor(min.x-0.5); | |
934 | ;; geometry.y = (ssize_t) floor(min.y-0.5); | |
935 | ;; geometry.width=(size_t) ceil(max.x-geometry.x+0.5); | |
936 | ;; geometry.height=(size_t) ceil(max.y-geometry.y+0.5); | |
937 | ;; } | |
938 | ||
939 | ;; Other parts of the same file show that here the origin is in the | |
940 | ;; left lower corner of the image rectangle, the center of the | |
941 | ;; rotation is the center of the rectangle and min.x and max.x | |
942 | ;; (resp. min.y and max.y) are the smallest and the largest of the | |
943 | ;; projections of the vertices onto the first (resp. second) axis. | |
944 | ||
945 | (defun image-transform-fit-width (width height length) | |
946 | "Return (w . h) so that a rotated w x h image has exactly width LENGTH. | |
947 | The rotation angle is the value of `image-transform-rotation'. | |
948 | Write W for WIDTH and H for HEIGHT. Then the w x h rectangle is | |
949 | an \"approximately uniformly\" scaled W x H rectangle, which | |
950 | currently means that w is one of floor(s W) + {0, 1, -1} and h is | |
951 | floor(s H), where s can be recovered as the value of `image-transform-scale'. | |
952 | The value of `image-transform-rotation' may be replaced by | |
953 | a slightly different angle. Currently this is done for values | |
954 | close to a multiple of 90, see `image-transform-right-angle-fudge'." | |
955 | (cond ((< (abs (- (mod (+ image-transform-rotation 90) 180) 90)) | |
956 | image-transform-right-angle-fudge) | |
f58e0fd5 | 957 | (cl-assert (not (zerop width)) t) |
2d21d7f6 WJ |
958 | (setq image-transform-rotation |
959 | (float (round image-transform-rotation)) | |
960 | image-transform-scale (/ (float length) width)) | |
961 | (cons length nil)) | |
962 | ((< (abs (- (mod (+ image-transform-rotation 45) 90) 45)) | |
963 | image-transform-right-angle-fudge) | |
f58e0fd5 | 964 | (cl-assert (not (zerop height)) t) |
2d21d7f6 WJ |
965 | (setq image-transform-rotation |
966 | (float (round image-transform-rotation)) | |
967 | image-transform-scale (/ (float length) height)) | |
968 | (cons nil length)) | |
969 | (t | |
f58e0fd5 | 970 | (cl-assert (not (and (zerop width) (zerop height))) t) |
2d21d7f6 WJ |
971 | (setq image-transform-scale |
972 | (/ (float (1- length)) (image-transform-width width height))) | |
973 | ;; Assume we have a w x h image and an angle A, and let l = | |
974 | ;; l(w, h) = w |cos A| + h |sin A|, which is the actual width | |
975 | ;; of the bounding box of the rotated image, as calculated by | |
976 | ;; `image-transform-width'. The code snippet quoted above | |
977 | ;; means that ImageMagick puts the rotated image in | |
978 | ;; a bounding box of width L = 2 ceil((w+l+1)/2) - w. | |
979 | ;; Elementary considerations show that this is equivalent to | |
980 | ;; L - w being even and L-3 < l(w, h) <= L-1. In our case, L is | |
981 | ;; the given `length' parameter and our job is to determine | |
982 | ;; reasonable values for w and h which satisfy these | |
983 | ;; conditions. | |
984 | (let ((w (floor (* image-transform-scale width))) | |
985 | (h (floor (* image-transform-scale height)))) | |
986 | ;; Let w and h as bound above. Then l(w, h) <= l(s W, s H) | |
987 | ;; = L-1 < l(w+1, h+1) = l(w, h) + l(1, 1) <= l(w, h) + 2, | |
988 | ;; hence l(w, h) > (L-1) - 2 = L-3. | |
989 | (cons | |
990 | (cond ((= (mod w 2) (mod length 2)) | |
991 | w) | |
992 | ;; l(w+1, h) >= l(w, h) > L-3, but does l(w+1, h) <= | |
993 | ;; L-1 hold? | |
994 | ((<= (image-transform-width (1+ w) h) (1- length)) | |
995 | (1+ w)) | |
996 | ;; No, it doesn't, but this implies that l(w-1, h) = | |
997 | ;; l(w+1, h) - l(2, 0) >= l(w+1, h) - 2 > (L-1) - | |
998 | ;; 2 = L-3. Clearly, l(w-1, h) <= l(w, h) <= L-1. | |
999 | (t | |
1000 | (1- w))) | |
1001 | h))))) | |
1002 | ||
1003 | (defun image-transform-check-size () | |
977f9325 WJ |
1004 | "Check that the image exactly fits the width/height of the window. |
1005 | ||
1006 | Do this for an image of type `imagemagick' to make sure that the | |
1007 | elisp code matches the way ImageMagick computes the bounding box | |
1008 | of a rotated image." | |
1009 | (when (and (not (numberp image-transform-resize)) | |
1010 | (boundp 'image-type) | |
1011 | (eq image-type 'imagemagick)) | |
2d21d7f6 WJ |
1012 | (let ((size (image-display-size (image-get-display-property) t))) |
1013 | (cond ((eq image-transform-resize 'fit-width) | |
f58e0fd5 | 1014 | (cl-assert (= (car size) |
2d21d7f6 WJ |
1015 | (- (nth 2 (window-inside-pixel-edges)) |
1016 | (nth 0 (window-inside-pixel-edges)))) | |
1017 | t)) | |
1018 | ((eq image-transform-resize 'fit-height) | |
f58e0fd5 | 1019 | (cl-assert (= (cdr size) |
2d21d7f6 WJ |
1020 | (- (nth 3 (window-inside-pixel-edges)) |
1021 | (nth 1 (window-inside-pixel-edges)))) | |
1022 | t)))))) | |
1023 | ||
18af70d0 CY |
1024 | (defun image-transform-properties (spec) |
1025 | "Return rescaling/rotation properties for image SPEC. | |
1026 | These properties are determined by the Image mode variables | |
1027 | `image-transform-resize' and `image-transform-rotation'. The | |
1028 | return value is suitable for appending to an image spec. | |
e8cbec34 | 1029 | |
09e80d9f | 1030 | Rescaling and rotation properties only take effect if Emacs is |
e8cbec34 | 1031 | compiled with ImageMagick support." |
2d21d7f6 | 1032 | (setq image-transform-scale 1.0) |
18af70d0 | 1033 | (when (or image-transform-resize |
2d21d7f6 | 1034 | (/= image-transform-rotation 0.0)) |
18af70d0 CY |
1035 | ;; Note: `image-size' looks up and thus caches the untransformed |
1036 | ;; image. There's no easy way to prevent that. | |
1037 | (let* ((size (image-size spec t)) | |
2d21d7f6 | 1038 | (resized |
18af70d0 CY |
1039 | (cond |
1040 | ((numberp image-transform-resize) | |
7102e6d0 | 1041 | (unless (= image-transform-resize 1) |
2d21d7f6 WJ |
1042 | (setq image-transform-scale image-transform-resize) |
1043 | (cons nil (floor (* image-transform-resize (cdr size)))))) | |
1044 | ((eq image-transform-resize 'fit-width) | |
1045 | (image-transform-fit-width | |
1046 | (car size) (cdr size) | |
1047 | (- (nth 2 (window-inside-pixel-edges)) | |
1048 | (nth 0 (window-inside-pixel-edges))))) | |
18af70d0 | 1049 | ((eq image-transform-resize 'fit-height) |
2d21d7f6 WJ |
1050 | (let ((res (image-transform-fit-width |
1051 | (cdr size) (car size) | |
1052 | (- (nth 3 (window-inside-pixel-edges)) | |
1053 | (nth 1 (window-inside-pixel-edges)))))) | |
1054 | (cons (cdr res) (car res))))))) | |
1055 | `(,@(when (car resized) | |
1056 | (list :width (car resized))) | |
1057 | ,@(when (cdr resized) | |
1058 | (list :height (cdr resized))) | |
1059 | ,@(unless (= 0.0 image-transform-rotation) | |
1060 | (list :rotation image-transform-rotation)))))) | |
c7f514b5 JV |
1061 | |
1062 | (defun image-transform-set-scale (scale) | |
a32d4040 CY |
1063 | "Prompt for a number, and resize the current image by that amount. |
1064 | This command has no effect unless Emacs is compiled with | |
1065 | ImageMagick support." | |
1066 | (interactive "nScale: ") | |
c183f693 | 1067 | (setq image-transform-resize scale) |
a32d4040 | 1068 | (image-toggle-display-image)) |
c7f514b5 JV |
1069 | |
1070 | (defun image-transform-fit-to-height () | |
a32d4040 CY |
1071 | "Fit the current image to the height of the current window. |
1072 | This command has no effect unless Emacs is compiled with | |
1073 | ImageMagick support." | |
c7f514b5 | 1074 | (interactive) |
a32d4040 CY |
1075 | (setq image-transform-resize 'fit-height) |
1076 | (image-toggle-display-image)) | |
c7f514b5 | 1077 | |
3a50be51 | 1078 | (defun image-transform-fit-to-width () |
a32d4040 CY |
1079 | "Fit the current image to the width of the current window. |
1080 | This command has no effect unless Emacs is compiled with | |
1081 | ImageMagick support." | |
3a50be51 | 1082 | (interactive) |
a32d4040 CY |
1083 | (setq image-transform-resize 'fit-width) |
1084 | (image-toggle-display-image)) | |
c7f514b5 JV |
1085 | |
1086 | (defun image-transform-set-rotation (rotation) | |
a32d4040 CY |
1087 | "Prompt for an angle ROTATION, and rotate the image by that amount. |
1088 | ROTATION should be in degrees. This command has no effect unless | |
1089 | Emacs is compiled with ImageMagick support." | |
1090 | (interactive "nRotation angle (in degrees): ") | |
2d21d7f6 | 1091 | (setq image-transform-rotation (float (mod rotation 360))) |
a32d4040 | 1092 | (image-toggle-display-image)) |
c7f514b5 | 1093 | |
c7bd5d57 RS |
1094 | (provide 'image-mode) |
1095 | ||
1096 | ;;; image-mode.el ends here |