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