Commit | Line | Data |
---|---|---|
c7bd5d57 RS |
1 | ;;; image-mode.el --- support for visiting image files |
2 | ;; | |
73b0cd50 | 3 | ;; Copyright (C) 2005-2011 Free Software Foundation, Inc. |
c7bd5d57 RS |
4 | ;; |
5 | ;; Author: Richard Stallman <rms@gnu.org> | |
6 | ;; Keywords: multimedia | |
bd78fa1d | 7 | ;; Package: emacs |
c7bd5d57 RS |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c7bd5d57 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
c7bd5d57 RS |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c7bd5d57 RS |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;; Defines a major mode for visiting image files | |
27 | ;; that allows conversion between viewing the text of the file | |
28 | ;; and viewing the file as an image. Viewing the image | |
29 | ;; works by putting a `display' text-property on the | |
30 | ;; image data, with the image-data still present underneath; if the | |
31 | ;; resulting buffer file is saved to another name it will correctly save | |
32 | ;; the image data to the new file. | |
33 | ||
34 | ;;; Code: | |
35 | ||
36 | (require 'image) | |
44e3c7c6 | 37 | (eval-when-compile (require 'cl)) |
c7bd5d57 | 38 | |
44e3c7c6 SM |
39 | ;;; Image mode window-info management. |
40 | ||
41 | (defvar image-mode-winprops-alist t) | |
42 | (make-variable-buffer-local 'image-mode-winprops-alist) | |
43 | ||
44 | (defvar image-mode-new-window-functions nil | |
45 | "Special hook run when image data is requested in a new window. | |
46 | It is called with one argument, the initial WINPROPS.") | |
47 | ||
b81f0bab | 48 | (defun image-mode-winprops (&optional window cleanup) |
44e3c7c6 SM |
49 | "Return winprops of WINDOW. |
50 | A winprops object has the shape (WINDOW . ALIST)." | |
b81f0bab CY |
51 | (cond ((null window) |
52 | (setq window (selected-window))) | |
53 | ((not (windowp window)) | |
54 | (error "Not a window: %s" window))) | |
55 | (when cleanup | |
56 | (setq image-mode-winprops-alist | |
57 | (delq nil (mapcar (lambda (winprop) | |
58 | (if (window-live-p (car-safe winprop)) | |
59 | winprop)) | |
60 | image-mode-winprops-alist)))) | |
44e3c7c6 SM |
61 | (let ((winprops (assq window image-mode-winprops-alist))) |
62 | ;; For new windows, set defaults from the latest. | |
63 | (unless winprops | |
64 | (setq winprops (cons window | |
65 | (copy-alist (cdar image-mode-winprops-alist)))) | |
66 | (run-hook-with-args 'image-mode-new-window-functions winprops)) | |
67 | ;; Move window to front. | |
68 | (setq image-mode-winprops-alist | |
69 | (cons winprops (delq winprops image-mode-winprops-alist))) | |
70 | winprops)) | |
71 | ||
72 | (defun image-mode-window-get (prop &optional winprops) | |
73 | (unless (consp winprops) (setq winprops (image-mode-winprops winprops))) | |
74 | (cdr (assq prop (cdr winprops)))) | |
75 | ||
76 | (defsetf image-mode-window-get (prop &optional winprops) (val) | |
77 | `(image-mode-window-put ,prop ,val ,winprops)) | |
78 | ||
79 | (defun image-mode-window-put (prop val &optional winprops) | |
80 | (unless (consp winprops) (setq winprops (image-mode-winprops winprops))) | |
81 | (setcdr winprops (cons (cons prop val) | |
82 | (delq (assq prop (cdr winprops)) (cdr winprops))))) | |
83 | ||
84 | (defun image-set-window-vscroll (vscroll) | |
85 | (setf (image-mode-window-get 'vscroll) vscroll) | |
86 | (set-window-vscroll (selected-window) vscroll)) | |
87 | ||
88 | (defun image-set-window-hscroll (ncol) | |
60134fd4 | 89 | (setf (image-mode-window-get 'hscroll) ncol) |
44e3c7c6 SM |
90 | (set-window-hscroll (selected-window) ncol)) |
91 | ||
92 | (defun image-mode-reapply-winprops () | |
6d401b4e SM |
93 | ;; When set-window-buffer, set hscroll and vscroll to what they were |
94 | ;; last time the image was displayed in this window. | |
b81f0bab CY |
95 | (when (and (image-get-display-property) |
96 | (listp image-mode-winprops-alist)) | |
97 | (let* ((winprops (image-mode-winprops nil t)) | |
6d401b4e SM |
98 | (hscroll (image-mode-window-get 'hscroll winprops)) |
99 | (vscroll (image-mode-window-get 'vscroll winprops))) | |
c313b5fe SM |
100 | (if hscroll (set-window-hscroll (selected-window) hscroll)) |
101 | (if vscroll (set-window-vscroll (selected-window) vscroll))))) | |
36e1c289 | 102 | |
44e3c7c6 SM |
103 | (defun image-mode-setup-winprops () |
104 | ;; Record current scroll settings. | |
105 | (unless (listp image-mode-winprops-alist) | |
106 | (setq image-mode-winprops-alist nil)) | |
107 | (add-hook 'window-configuration-change-hook | |
108 | 'image-mode-reapply-winprops nil t)) | |
109 | ||
110 | ;;; Image scrolling functions | |
111 | ||
91784462 SM |
112 | (defun image-get-display-property () |
113 | (get-char-property (point-min) 'display | |
114 | ;; There might be different images for different displays. | |
115 | (if (eq (window-buffer) (current-buffer)) | |
116 | (selected-window)))) | |
117 | ||
aa360da1 GM |
118 | (declare-function image-size "image.c" (spec &optional pixels frame)) |
119 | ||
c9088194 SK |
120 | (defun image-display-size (spec &optional pixels frame) |
121 | "Wrapper around `image-size', to handle slice display properties. | |
122 | If SPEC is an image display property, call `image-size' with the | |
123 | given arguments. | |
124 | If SPEC is a list of properties containing `image' and `slice' | |
125 | properties, calculate the display size from the slice property. | |
126 | If SPEC contains `image' but not `slice', call `image-size' with | |
127 | the specified image." | |
128 | (if (eq (car spec) 'image) | |
129 | (image-size spec pixels frame) | |
130 | (let ((image (assoc 'image spec)) | |
131 | (slice (assoc 'slice spec))) | |
132 | (cond ((and image slice) | |
133 | (if pixels | |
134 | (cons (nth 3 slice) (nth 4 slice)) | |
135 | (cons (/ (float (nth 3 slice)) (frame-char-width frame)) | |
136 | (/ (float (nth 4 slice)) (frame-char-height frame))))) | |
137 | (image | |
138 | (image-size image pixels frame)) | |
139 | (t | |
140 | (error "Invalid image specification: %s" spec)))))) | |
141 | ||
bb0cb417 CY |
142 | (defun image-forward-hscroll (&optional n) |
143 | "Scroll image in current window to the left by N character widths. | |
144 | Stop if the right edge of the image is reached." | |
145 | (interactive "p") | |
146 | (cond ((= n 0) nil) | |
147 | ((< n 0) | |
44e3c7c6 | 148 | (image-set-window-hscroll (max 0 (+ (window-hscroll) n)))) |
bb0cb417 | 149 | (t |
91784462 | 150 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
151 | (edges (window-inside-edges)) |
152 | (win-width (- (nth 2 edges) (nth 0 edges))) | |
c9088194 | 153 | (img-width (ceiling (car (image-display-size image))))) |
44e3c7c6 | 154 | (image-set-window-hscroll (min (max 0 (- img-width win-width)) |
36e1c289 | 155 | (+ n (window-hscroll)))))))) |
bb0cb417 CY |
156 | |
157 | (defun image-backward-hscroll (&optional n) | |
158 | "Scroll image in current window to the right by N character widths. | |
159 | Stop if the left edge of the image is reached." | |
160 | (interactive "p") | |
161 | (image-forward-hscroll (- n))) | |
162 | ||
163 | (defun image-next-line (&optional n) | |
164 | "Scroll image in current window upward by N lines. | |
165 | Stop if the bottom edge of the image is reached." | |
166 | (interactive "p") | |
167 | (cond ((= n 0) nil) | |
168 | ((< n 0) | |
44e3c7c6 | 169 | (image-set-window-vscroll (max 0 (+ (window-vscroll) n)))) |
bb0cb417 | 170 | (t |
91784462 | 171 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
172 | (edges (window-inside-edges)) |
173 | (win-height (- (nth 3 edges) (nth 1 edges))) | |
c9088194 | 174 | (img-height (ceiling (cdr (image-display-size image))))) |
44e3c7c6 | 175 | (image-set-window-vscroll (min (max 0 (- img-height win-height)) |
36e1c289 | 176 | (+ n (window-vscroll)))))))) |
bb0cb417 CY |
177 | |
178 | (defun image-previous-line (&optional n) | |
179 | "Scroll image in current window downward by N lines. | |
180 | Stop if the top edge of the image is reached." | |
181 | (interactive "p") | |
182 | (image-next-line (- n))) | |
183 | ||
184 | (defun image-scroll-up (&optional n) | |
185 | "Scroll image in current window upward by N lines. | |
186 | Stop if the bottom edge of the image is reached. | |
187 | If ARG is omitted or nil, scroll upward by a near full screen. | |
188 | A near full screen is `next-screen-context-lines' less than a full screen. | |
189 | Negative ARG means scroll downward. | |
190 | If ARG is the atom `-', scroll downward by nearly full screen. | |
191 | When calling from a program, supply as argument a number, nil, or `-'." | |
192 | (interactive "P") | |
193 | (cond ((null n) | |
194 | (let* ((edges (window-inside-edges)) | |
195 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
196 | (image-next-line | |
197 | (max 0 (- win-height next-screen-context-lines))))) | |
198 | ((eq n '-) | |
199 | (let* ((edges (window-inside-edges)) | |
200 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
201 | (image-next-line | |
202 | (min 0 (- next-screen-context-lines win-height))))) | |
203 | (t (image-next-line (prefix-numeric-value n))))) | |
204 | ||
205 | (defun image-scroll-down (&optional n) | |
3488c456 | 206 | "Scroll image in current window downward by N lines. |
bb0cb417 CY |
207 | Stop if the top edge of the image is reached. |
208 | If ARG is omitted or nil, scroll downward by a near full screen. | |
209 | A near full screen is `next-screen-context-lines' less than a full screen. | |
210 | Negative ARG means scroll upward. | |
211 | If ARG is the atom `-', scroll upward by nearly full screen. | |
212 | When calling from a program, supply as argument a number, nil, or `-'." | |
213 | (interactive "P") | |
214 | (cond ((null n) | |
215 | (let* ((edges (window-inside-edges)) | |
216 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
217 | (image-next-line | |
218 | (min 0 (- next-screen-context-lines win-height))))) | |
219 | ((eq n '-) | |
220 | (let* ((edges (window-inside-edges)) | |
221 | (win-height (- (nth 3 edges) (nth 1 edges)))) | |
222 | (image-next-line | |
223 | (max 0 (- win-height next-screen-context-lines))))) | |
224 | (t (image-next-line (- (prefix-numeric-value n)))))) | |
225 | ||
226 | (defun image-bol (arg) | |
227 | "Scroll horizontally to the left edge of the image in the current window. | |
228 | With argument ARG not nil or 1, move forward ARG - 1 lines first, | |
229 | stopping if the top or bottom edge of the image is reached." | |
230 | (interactive "p") | |
231 | (and arg | |
232 | (/= (setq arg (prefix-numeric-value arg)) 1) | |
233 | (image-next-line (- arg 1))) | |
44e3c7c6 | 234 | (image-set-window-hscroll 0)) |
bb0cb417 CY |
235 | |
236 | (defun image-eol (arg) | |
237 | "Scroll horizontally to the right edge of the image in the current window. | |
238 | With argument ARG not nil or 1, move forward ARG - 1 lines first, | |
239 | stopping if the top or bottom edge of the image is reached." | |
240 | (interactive "p") | |
241 | (and arg | |
242 | (/= (setq arg (prefix-numeric-value arg)) 1) | |
243 | (image-next-line (- arg 1))) | |
91784462 | 244 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
245 | (edges (window-inside-edges)) |
246 | (win-width (- (nth 2 edges) (nth 0 edges))) | |
c9088194 | 247 | (img-width (ceiling (car (image-display-size image))))) |
44e3c7c6 | 248 | (image-set-window-hscroll (max 0 (- img-width win-width))))) |
bb0cb417 CY |
249 | |
250 | (defun image-bob () | |
251 | "Scroll to the top-left corner of the image in the current window." | |
252 | (interactive) | |
44e3c7c6 SM |
253 | (image-set-window-hscroll 0) |
254 | (image-set-window-vscroll 0)) | |
bb0cb417 CY |
255 | |
256 | (defun image-eob () | |
257 | "Scroll to the bottom-right corner of the image in the current window." | |
258 | (interactive) | |
91784462 | 259 | (let* ((image (image-get-display-property)) |
bb0cb417 CY |
260 | (edges (window-inside-edges)) |
261 | (win-width (- (nth 2 edges) (nth 0 edges))) | |
c9088194 | 262 | (img-width (ceiling (car (image-display-size image)))) |
bb0cb417 | 263 | (win-height (- (nth 3 edges) (nth 1 edges))) |
c9088194 | 264 | (img-height (ceiling (cdr (image-display-size image))))) |
44e3c7c6 SM |
265 | (image-set-window-hscroll (max 0 (- img-width win-width))) |
266 | (image-set-window-vscroll (max 0 (- img-height win-height))))) | |
bb0cb417 | 267 | |
bd1d6a63 SM |
268 | ;; Adjust frame and image size. |
269 | ||
270 | (defun image-mode-fit-frame () | |
271 | "Fit the frame to the current image. | |
272 | This function assumes the current frame has only one window." | |
273 | ;; FIXME: This does not take into account decorations like mode-line, | |
274 | ;; minibuffer, header-line, ... | |
275 | (interactive) | |
276 | (let* ((saved (frame-parameter nil 'image-mode-saved-size)) | |
277 | (display (image-get-display-property)) | |
c9088194 | 278 | (size (image-display-size display))) |
bd1d6a63 SM |
279 | (if (and saved |
280 | (eq (caar saved) (frame-width)) | |
281 | (eq (cdar saved) (frame-height))) | |
282 | (progn ;; Toggle back to previous non-fitted size. | |
283 | (set-frame-parameter nil 'image-mode-saved-size nil) | |
284 | (setq size (cdr saved))) | |
285 | ;; Round up size, and save current size so we can toggle back to it. | |
286 | (setcar size (ceiling (car size))) | |
287 | (setcdr size (ceiling (cdr size))) | |
288 | (set-frame-parameter nil 'image-mode-saved-size | |
289 | (cons size (cons (frame-width) (frame-height))))) | |
290 | (set-frame-width (selected-frame) (car size)) | |
291 | (set-frame-height (selected-frame) (cdr size)))) | |
292 | ||
bb0cb417 CY |
293 | ;;; Image Mode setup |
294 | ||
d487ca7d JL |
295 | (defvar image-type nil |
296 | "Current image type. | |
297 | This variable is used to display the current image type in the mode line.") | |
298 | (make-variable-buffer-local 'image-type) | |
299 | ||
9b9debd1 JL |
300 | (defvar image-mode-previous-major-mode nil |
301 | "Internal variable to keep the previous non-image major mode.") | |
302 | ||
c7bd5d57 | 303 | (defvar image-mode-map |
bb0cb417 | 304 | (let ((map (make-sparse-keymap))) |
d2d7e96c SM |
305 | (suppress-keymap map) |
306 | (define-key map "q" 'quit-window) | |
bb0cb417 | 307 | (define-key map "\C-c\C-c" 'image-toggle-display) |
42c27c2a SM |
308 | (define-key map (kbd "SPC") 'image-scroll-up) |
309 | (define-key map (kbd "DEL") 'image-scroll-down) | |
bb0cb417 CY |
310 | (define-key map [remap forward-char] 'image-forward-hscroll) |
311 | (define-key map [remap backward-char] 'image-backward-hscroll) | |
d8b0cddd SM |
312 | (define-key map [remap right-char] 'image-forward-hscroll) |
313 | (define-key map [remap left-char] 'image-backward-hscroll) | |
bb0cb417 CY |
314 | (define-key map [remap previous-line] 'image-previous-line) |
315 | (define-key map [remap next-line] 'image-next-line) | |
316 | (define-key map [remap scroll-up] 'image-scroll-up) | |
317 | (define-key map [remap scroll-down] 'image-scroll-down) | |
32129746 JL |
318 | (define-key map [remap scroll-up-command] 'image-scroll-up) |
319 | (define-key map [remap scroll-down-command] 'image-scroll-down) | |
bb0cb417 CY |
320 | (define-key map [remap move-beginning-of-line] 'image-bol) |
321 | (define-key map [remap move-end-of-line] 'image-eol) | |
322 | (define-key map [remap beginning-of-buffer] 'image-bob) | |
323 | (define-key map [remap end-of-buffer] 'image-eob) | |
324 | map) | |
325 | "Major mode keymap for viewing images in Image mode.") | |
326 | ||
9b9debd1 | 327 | (defvar image-minor-mode-map |
c7bd5d57 RS |
328 | (let ((map (make-sparse-keymap))) |
329 | (define-key map "\C-c\C-c" 'image-toggle-display) | |
330 | map) | |
9b9debd1 | 331 | "Minor mode keymap for viewing images as text in Image mode.") |
c7bd5d57 | 332 | |
e0385bf4 | 333 | (defvar bookmark-make-record-function) |
a47d49c0 | 334 | |
0d17c4b9 JL |
335 | (put 'image-mode 'mode-class 'special) |
336 | ||
c7bd5d57 RS |
337 | ;;;###autoload |
338 | (defun image-mode () | |
339 | "Major mode for image files. | |
340 | You can use \\<image-mode-map>\\[image-toggle-display] | |
341 | to toggle between display as an image and display as text." | |
342 | (interactive) | |
9b9debd1 JL |
343 | (condition-case err |
344 | (progn | |
345 | (unless (display-images-p) | |
346 | (error "Display does not support images")) | |
347 | ||
348 | (kill-all-local-variables) | |
349 | (setq major-mode 'image-mode) | |
350 | ||
351 | (if (not (image-get-display-property)) | |
352 | (progn | |
353 | (image-toggle-display-image) | |
354 | ;; If attempt to display the image fails. | |
355 | (if (not (image-get-display-property)) | |
356 | (error "Invalid image"))) | |
357 | ;; Set next vars when image is already displayed but local | |
358 | ;; variables were cleared by kill-all-local-variables | |
359 | (setq cursor-type nil truncate-lines t | |
360 | image-type (plist-get (cdr (image-get-display-property)) :type))) | |
361 | ||
362 | (setq mode-name (if image-type (format "Image[%s]" image-type) "Image")) | |
bb43424b | 363 | (use-local-map image-mode-map) |
9b9debd1 JL |
364 | |
365 | ;; Use our own bookmarking function for images. | |
366 | (set (make-local-variable 'bookmark-make-record-function) | |
367 | 'image-bookmark-make-record) | |
368 | ||
369 | ;; Keep track of [vh]scroll when switching buffers | |
370 | (image-mode-setup-winprops) | |
371 | ||
372 | (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t) | |
0fb1193d | 373 | (add-hook 'after-revert-hook 'image-after-revert-hook nil t) |
9b9debd1 JL |
374 | (run-mode-hooks 'image-mode-hook) |
375 | (message "%s" (concat | |
376 | (substitute-command-keys | |
377 | "Type \\[image-toggle-display] to view the image as ") | |
378 | (if (image-get-display-property) | |
379 | "text" "an image") "."))) | |
380 | (error | |
381 | (image-mode-as-text) | |
382 | (funcall | |
383 | (if (called-interactively-p 'any) 'error 'message) | |
384 | "Cannot display image: %s" (cdr err))))) | |
d90e651e JL |
385 | |
386 | ;;;###autoload | |
387 | (define-minor-mode image-minor-mode | |
388 | "Toggle Image minor mode. | |
389 | With arg, turn Image minor mode on if arg is positive, off otherwise. | |
9b9debd1 JL |
390 | It provides the key \\<image-mode-map>\\[image-toggle-display] \ |
391 | to switch back to `image-mode' | |
392 | to display an image file as the actual image." | |
393 | nil (:eval (if image-type (format " Image[%s]" image-type) " Image")) | |
394 | image-minor-mode-map | |
d90e651e JL |
395 | :group 'image |
396 | :version "22.1" | |
9b9debd1 JL |
397 | (if image-minor-mode |
398 | (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t))) | |
d90e651e JL |
399 | |
400 | ;;;###autoload | |
9b9debd1 JL |
401 | (defun image-mode-as-text () |
402 | "Set a non-image mode as major mode in combination with image minor mode. | |
403 | A non-image major mode found from `auto-mode-alist' or Fundamental mode | |
404 | displays an image file as text. `image-minor-mode' provides the key | |
405 | \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode' | |
406 | to display an image file as the actual image. | |
407 | ||
408 | You can use `image-mode-as-text' in `auto-mode-alist' when you want | |
316d12fb | 409 | to display an image file as text initially. |
9b9debd1 JL |
410 | |
411 | See commands `image-mode' and `image-minor-mode' for more information | |
412 | on these modes." | |
d90e651e | 413 | (interactive) |
9b9debd1 JL |
414 | ;; image-mode-as-text = normal-mode + image-minor-mode |
415 | (let ((previous-image-type image-type)) ; preserve `image-type' | |
416 | (if image-mode-previous-major-mode | |
417 | ;; Restore previous major mode that was already found by this | |
418 | ;; function and cached in `image-mode-previous-major-mode' | |
419 | (funcall image-mode-previous-major-mode) | |
420 | (let ((auto-mode-alist | |
421 | (delq nil (mapcar | |
422 | (lambda (elt) | |
423 | (unless (memq (or (car-safe (cdr elt)) (cdr elt)) | |
424 | '(image-mode image-mode-maybe image-mode-as-text)) | |
425 | elt)) | |
426 | auto-mode-alist))) | |
427 | (magic-fallback-mode-alist | |
428 | (delq nil (mapcar | |
429 | (lambda (elt) | |
430 | (unless (memq (or (car-safe (cdr elt)) (cdr elt)) | |
431 | '(image-mode image-mode-maybe image-mode-as-text)) | |
432 | elt)) | |
433 | magic-fallback-mode-alist)))) | |
434 | (normal-mode) | |
435 | (set (make-local-variable 'image-mode-previous-major-mode) major-mode))) | |
436 | ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'. | |
437 | (setq image-type previous-image-type) | |
438 | ;; Enable image minor mode with `C-c C-c'. | |
439 | (image-minor-mode 1) | |
440 | ;; Show the image file as text. | |
441 | (image-toggle-display-text) | |
442 | (message "%s" (concat | |
443 | (substitute-command-keys | |
444 | "Type \\[image-toggle-display] to view the image as ") | |
445 | (if (image-get-display-property) | |
446 | "text" "an image") ".")))) | |
447 | ||
448 | (define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2") | |
d90e651e JL |
449 | |
450 | (defun image-toggle-display-text () | |
9b9debd1 JL |
451 | "Show the image file as text. |
452 | Remove text properties that display the image." | |
453 | (let ((inhibit-read-only t) | |
454 | (buffer-undo-list t) | |
455 | (modified (buffer-modified-p))) | |
456 | (remove-list-of-text-properties (point-min) (point-max) | |
457 | '(display intangible read-nonsticky | |
458 | read-only front-sticky)) | |
459 | (set-buffer-modified-p modified) | |
460 | (if (called-interactively-p 'any) | |
461 | (message "Repeat this command to go back to displaying the image")))) | |
c7bd5d57 | 462 | |
f2920ffe RS |
463 | (defvar archive-superior-buffer) |
464 | (defvar tar-superior-buffer) | |
110683ad | 465 | (declare-function image-flush "image.c" (spec &optional frame)) |
f2920ffe | 466 | |
9b9debd1 JL |
467 | (defun image-toggle-display-image () |
468 | "Show the image of the image file. | |
469 | Turn the image data into a real image, but only if the whole file | |
470 | was inserted." | |
471 | (let* ((filename (buffer-file-name)) | |
472 | (data-p (not (and filename | |
473 | (file-readable-p filename) | |
474 | (not (file-remote-p filename)) | |
475 | (not (buffer-modified-p)) | |
476 | (not (and (boundp 'archive-superior-buffer) | |
477 | archive-superior-buffer)) | |
478 | (not (and (boundp 'tar-superior-buffer) | |
479 | tar-superior-buffer))))) | |
480 | (file-or-data (if data-p | |
481 | (string-make-unibyte | |
482 | (buffer-substring-no-properties (point-min) (point-max))) | |
483 | filename)) | |
484 | (type (image-type file-or-data nil data-p)) | |
c7f514b5 JV |
485 | (image0 (create-animated-image file-or-data type data-p)) |
486 | (image (append image0 | |
487 | (image-transform-properties image0) | |
488 | )) | |
9b9debd1 JL |
489 | (props |
490 | `(display ,image | |
491 | intangible ,image | |
492 | rear-nonsticky (display intangible) | |
493 | read-only t front-sticky (read-only))) | |
494 | (inhibit-read-only t) | |
495 | (buffer-undo-list t) | |
496 | (modified (buffer-modified-p))) | |
110683ad | 497 | (image-flush image) |
9b9debd1 JL |
498 | (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file |
499 | (add-text-properties (point-min) (point-max) props) | |
500 | (restore-buffer-modified-p modified)) | |
501 | ;; Inhibit the cursor when the buffer contains only an image, | |
502 | ;; because cursors look very strange on top of images. | |
503 | (setq cursor-type nil) | |
504 | ;; This just makes the arrow displayed in the right fringe | |
505 | ;; area look correct when the image is wider than the window. | |
506 | (setq truncate-lines t) | |
507 | ;; Allow navigation of large images | |
508 | (set (make-local-variable 'auto-hscroll-mode) nil) | |
509 | (setq image-type type) | |
510 | (if (eq major-mode 'image-mode) | |
511 | (setq mode-name (format "Image[%s]" type))) | |
512 | (if (called-interactively-p 'any) | |
513 | (message "Repeat this command to go back to displaying the file as text")))) | |
514 | ||
c7bd5d57 RS |
515 | (defun image-toggle-display () |
516 | "Start or stop displaying an image file as the actual image. | |
9b9debd1 JL |
517 | This command toggles between `image-mode-as-text' showing the text of |
518 | the image file and `image-mode' showing the image as an image." | |
c7bd5d57 | 519 | (interactive) |
91784462 | 520 | (if (image-get-display-property) |
9b9debd1 JL |
521 | (image-mode-as-text) |
522 | (image-mode))) | |
0fb1193d JL |
523 | |
524 | (defun image-after-revert-hook () | |
525 | (when (image-get-display-property) | |
526 | (image-toggle-display-text) | |
527 | ;; Update image display. | |
528 | (redraw-frame (selected-frame)) | |
529 | (image-toggle-display-image))) | |
530 | ||
97666703 | 531 | \f |
137187c8 | 532 | ;;; Support for bookmark.el |
e44fa724 KF |
533 | (declare-function bookmark-make-record-default |
534 | "bookmark" (&optional no-file no-context posn)) | |
43f8b275 SM |
535 | (declare-function bookmark-prop-get "bookmark" (bookmark prop)) |
536 | (declare-function bookmark-default-handler "bookmark" (bmk)) | |
137187c8 | 537 | |
43f8b275 | 538 | (defun image-bookmark-make-record () |
f12492c8 TV |
539 | `(,@(bookmark-make-record-default nil 'no-context 0) |
540 | (image-type . ,image-type) | |
541 | (handler . image-bookmark-jump))) | |
137187c8 | 542 | |
137187c8 TH |
543 | ;;;###autoload |
544 | (defun image-bookmark-jump (bmk) | |
03e26a79 | 545 | ;; This implements the `handler' function interface for record type |
e0385bf4 | 546 | ;; returned by `bookmark-make-record-function', which see. |
43f8b275 SM |
547 | (prog1 (bookmark-default-handler bmk) |
548 | (when (not (string= image-type (bookmark-prop-get bmk 'image-type))) | |
549 | (image-toggle-display)))) | |
97666703 | 550 | \f |
c7f514b5 JV |
551 | |
552 | (defvar image-transform-minor-mode-map | |
553 | (let ((map (make-sparse-keymap))) | |
554 | ; (define-key map [(control ?+)] 'image-scale-in) | |
555 | ; (define-key map [(control ?-)] 'image-scale-out) | |
556 | ; (define-key map [(control ?=)] 'image-scale-none) | |
fe72c5b4 JV |
557 | ;; (define-key map "c f h" 'image-scale-fit-height) |
558 | ;; (define-key map "c ]" 'image-rotate-right) | |
c7f514b5 JV |
559 | map) |
560 | "Minor mode keymap for transforming the view of images Image mode.") | |
561 | ||
562 | (define-minor-mode image-transform-mode | |
563 | "minor mode for scaleing and rotation" | |
564 | nil "image-transform" | |
565 | image-transform-minor-mode-map) | |
566 | ||
fe72c5b4 JV |
567 | (defvar image-transform-resize nil |
568 | "The image resize operation. See the command | |
569 | `image-transform-set-scale' for more information." ) | |
c7f514b5 | 570 | |
c7f514b5 JV |
571 | (defvar image-transform-rotation 0.0) |
572 | ||
c7f514b5 JV |
573 | |
574 | (defun image-transform-properties (display) | |
fe72c5b4 JV |
575 | "Calculate the display properties for transformations; scaling |
576 | and rotation. " | |
c7f514b5 JV |
577 | (let* |
578 | ((size (image-size display t)) | |
579 | (height | |
580 | (cond | |
581 | ((and (numberp image-transform-resize) (eq 100 image-transform-resize)) | |
582 | nil) | |
583 | ((numberp image-transform-resize) | |
584 | (* image-transform-resize (cdr size))) | |
585 | ((eq image-transform-resize 'fit-height) | |
3a50be51 JV |
586 | (- (nth 3 (window-inside-pixel-edges)) (nth 1 (window-inside-pixel-edges)))) |
587 | (t nil))) | |
588 | (width (if (eq image-transform-resize 'fit-width) | |
589 | (- (nth 2 (window-inside-pixel-edges)) (nth 0 (window-inside-pixel-edges)))))) | |
590 | ||
591 | `(,@(if height (list :height height)) | |
592 | ,@(if width (list :width width)) | |
593 | ,@(if (not (equal 0.0 image-transform-rotation)) | |
594 | (list :rotation image-transform-rotation)) | |
fe72c5b4 | 595 | ;;TODO fit-to-* should consider the rotation angle |
3a50be51 | 596 | ))) |
c7f514b5 JV |
597 | |
598 | (defun image-transform-set-scale (scale) | |
fe72c5b4 | 599 | "SCALE sets the scaling for images. " |
c7f514b5 JV |
600 | (interactive "nscale:") |
601 | (image-transform-set-resize (float scale))) | |
602 | ||
603 | (defun image-transform-fit-to-height () | |
fe72c5b4 | 604 | "Fit image height to window height. " |
c7f514b5 JV |
605 | (interactive) |
606 | (image-transform-set-resize 'fit-height)) | |
607 | ||
3a50be51 | 608 | (defun image-transform-fit-to-width () |
fe72c5b4 | 609 | "Fit image width to window width. " |
3a50be51 JV |
610 | (interactive) |
611 | (image-transform-set-resize 'fit-width)) | |
612 | ||
c7f514b5 | 613 | (defun image-transform-set-resize (resize) |
fe72c5b4 JV |
614 | "Set the resize mode for images. The RESIZE value can be the |
615 | symbol fit-height which fits the image to the window height. The | |
616 | symbol fit-width fits the image to the window width. A number | |
617 | indicates a scaling factor. nil indicates scale to 100%. " | |
c7f514b5 | 618 | (setq image-transform-resize resize) |
3a50be51 | 619 | (if (eq 'image-mode major-mode) (image-toggle-display-image))) |
c7f514b5 JV |
620 | |
621 | (defun image-transform-set-rotation (rotation) | |
fe72c5b4 | 622 | "Set the image ROTATION angle. " |
c7f514b5 | 623 | (interactive "nrotation:") |
fe72c5b4 JV |
624 | ;;TODO 0 90 180 270 degrees are the only reasonable angles here |
625 | ;;otherwise combining with rescaling will get very awkward | |
c7f514b5 | 626 | (setq image-transform-rotation (float rotation)) |
fe72c5b4 | 627 | (if (eq major-mode 'image-mode) (image-toggle-display-image))) |
c7f514b5 | 628 | |
c7bd5d57 RS |
629 | (provide 'image-mode) |
630 | ||
631 | ;;; image-mode.el ends here |