1 ;;; image-mode.el --- support for visiting image files
3 ;; Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Richard Stallman <rms@gnu.org>
6 ;; Keywords: multimedia
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Defines a major mode for visiting image files
26 ;; that allows conversion between viewing the text of the file
27 ;; and viewing the file as an image. Viewing the image
28 ;; works by putting a `display' text-property on the
29 ;; image data, with the image-data still present underneath; if the
30 ;; resulting buffer file is saved to another name it will correctly save
31 ;; the image data to the new file.
36 (eval-when-compile (require 'cl
))
38 ;;;###autoload (push '("\\.jpe?g\\'" . image-mode) auto-mode-alist)
39 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
40 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
41 ;;;###autoload (push '("\\.tiff?\\'" . image-mode) auto-mode-alist)
42 ;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist)
44 ;;;###autoload (push '("\\.x[bp]m\\'" . c-mode) auto-mode-alist)
45 ;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
47 ;;;###autoload (push '("\\.svgz?\\'" . xml-mode) auto-mode-alist)
48 ;;;###autoload (push '("\\.svgz?\\'" . image-mode-maybe) auto-mode-alist)
50 ;;; Image mode window-info management.
52 (defvar image-mode-winprops-alist t
)
53 (make-variable-buffer-local 'image-mode-winprops-alist
)
55 (defvar image-mode-new-window-functions nil
56 "Special hook run when image data is requested in a new window.
57 It is called with one argument, the initial WINPROPS.")
59 (defun image-mode-winprops (&optional window cleanup
)
60 "Return winprops of WINDOW.
61 A winprops object has the shape (WINDOW . ALIST)."
63 (setq window
(selected-window)))
64 ((not (windowp window
))
65 (error "Not a window: %s" window
)))
67 (setq image-mode-winprops-alist
68 (delq nil
(mapcar (lambda (winprop)
69 (if (window-live-p (car-safe winprop
))
71 image-mode-winprops-alist
))))
72 (let ((winprops (assq window image-mode-winprops-alist
)))
73 ;; For new windows, set defaults from the latest.
75 (setq winprops
(cons window
76 (copy-alist (cdar image-mode-winprops-alist
))))
77 (run-hook-with-args 'image-mode-new-window-functions winprops
))
78 ;; Move window to front.
79 (setq image-mode-winprops-alist
80 (cons winprops
(delq winprops image-mode-winprops-alist
)))
83 (defun image-mode-window-get (prop &optional winprops
)
84 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
85 (cdr (assq prop
(cdr winprops
))))
87 (defsetf image-mode-window-get
(prop &optional winprops
) (val)
88 `(image-mode-window-put ,prop
,val
,winprops
))
90 (defun image-mode-window-put (prop val
&optional winprops
)
91 (unless (consp winprops
) (setq winprops
(image-mode-winprops winprops
)))
92 (setcdr winprops
(cons (cons prop val
)
93 (delq (assq prop
(cdr winprops
)) (cdr winprops
)))))
95 (defun image-set-window-vscroll (vscroll)
96 (setf (image-mode-window-get 'vscroll
) vscroll
)
97 (set-window-vscroll (selected-window) vscroll
))
99 (defun image-set-window-hscroll (ncol)
100 (setf (image-mode-window-get 'hscroll
) ncol
)
101 (set-window-hscroll (selected-window) ncol
))
103 (defun image-mode-reapply-winprops ()
104 ;; When set-window-buffer, set hscroll and vscroll to what they were
105 ;; last time the image was displayed in this window.
106 (when (and (image-get-display-property)
107 (listp image-mode-winprops-alist
))
108 (let* ((winprops (image-mode-winprops nil t
))
109 (hscroll (image-mode-window-get 'hscroll winprops
))
110 (vscroll (image-mode-window-get 'vscroll winprops
)))
111 (if hscroll
(set-window-hscroll (selected-window) hscroll
))
112 (if vscroll
(set-window-vscroll (selected-window) vscroll
)))))
114 (defun image-mode-setup-winprops ()
115 ;; Record current scroll settings.
116 (unless (listp image-mode-winprops-alist
)
117 (setq image-mode-winprops-alist nil
))
118 (add-hook 'window-configuration-change-hook
119 'image-mode-reapply-winprops nil t
))
121 ;;; Image scrolling functions
123 (defun image-get-display-property ()
124 (get-char-property (point-min) 'display
125 ;; There might be different images for different displays.
126 (if (eq (window-buffer) (current-buffer))
129 (defun image-forward-hscroll (&optional n
)
130 "Scroll image in current window to the left by N character widths.
131 Stop if the right edge of the image is reached."
135 (image-set-window-hscroll (max 0 (+ (window-hscroll) n
))))
137 (let* ((image (image-get-display-property))
138 (edges (window-inside-edges))
139 (win-width (- (nth 2 edges
) (nth 0 edges
)))
140 (img-width (ceiling (car (image-size image
)))))
141 (image-set-window-hscroll (min (max 0 (- img-width win-width
))
142 (+ n
(window-hscroll))))))))
144 (defun image-backward-hscroll (&optional n
)
145 "Scroll image in current window to the right by N character widths.
146 Stop if the left edge of the image is reached."
148 (image-forward-hscroll (- n
)))
150 (defun image-next-line (&optional n
)
151 "Scroll image in current window upward by N lines.
152 Stop if the bottom edge of the image is reached."
156 (image-set-window-vscroll (max 0 (+ (window-vscroll) n
))))
158 (let* ((image (image-get-display-property))
159 (edges (window-inside-edges))
160 (win-height (- (nth 3 edges
) (nth 1 edges
)))
161 (img-height (ceiling (cdr (image-size image
)))))
162 (image-set-window-vscroll (min (max 0 (- img-height win-height
))
163 (+ n
(window-vscroll))))))))
165 (defun image-previous-line (&optional n
)
166 "Scroll image in current window downward by N lines.
167 Stop if the top edge of the image is reached."
169 (image-next-line (- n
)))
171 (defun image-scroll-up (&optional n
)
172 "Scroll image in current window upward by N lines.
173 Stop if the bottom edge of the image is reached.
174 If ARG is omitted or nil, scroll upward by a near full screen.
175 A near full screen is `next-screen-context-lines' less than a full screen.
176 Negative ARG means scroll downward.
177 If ARG is the atom `-', scroll downward by nearly full screen.
178 When calling from a program, supply as argument a number, nil, or `-'."
181 (let* ((edges (window-inside-edges))
182 (win-height (- (nth 3 edges
) (nth 1 edges
))))
184 (max 0 (- win-height next-screen-context-lines
)))))
186 (let* ((edges (window-inside-edges))
187 (win-height (- (nth 3 edges
) (nth 1 edges
))))
189 (min 0 (- next-screen-context-lines win-height
)))))
190 (t (image-next-line (prefix-numeric-value n
)))))
192 (defun image-scroll-down (&optional n
)
193 "Scroll image in current window downward by N lines.
194 Stop if the top edge of the image is reached.
195 If ARG is omitted or nil, scroll downward by a near full screen.
196 A near full screen is `next-screen-context-lines' less than a full screen.
197 Negative ARG means scroll upward.
198 If ARG is the atom `-', scroll upward by nearly full screen.
199 When calling from a program, supply as argument a number, nil, or `-'."
202 (let* ((edges (window-inside-edges))
203 (win-height (- (nth 3 edges
) (nth 1 edges
))))
205 (min 0 (- next-screen-context-lines win-height
)))))
207 (let* ((edges (window-inside-edges))
208 (win-height (- (nth 3 edges
) (nth 1 edges
))))
210 (max 0 (- win-height next-screen-context-lines
)))))
211 (t (image-next-line (- (prefix-numeric-value n
))))))
213 (defun image-bol (arg)
214 "Scroll horizontally to the left edge of the image in the current window.
215 With argument ARG not nil or 1, move forward ARG - 1 lines first,
216 stopping if the top or bottom edge of the image is reached."
219 (/= (setq arg
(prefix-numeric-value arg
)) 1)
220 (image-next-line (- arg
1)))
221 (image-set-window-hscroll 0))
223 (defun image-eol (arg)
224 "Scroll horizontally to the right edge of the image in the current window.
225 With argument ARG not nil or 1, move forward ARG - 1 lines first,
226 stopping if the top or bottom edge of the image is reached."
229 (/= (setq arg
(prefix-numeric-value arg
)) 1)
230 (image-next-line (- arg
1)))
231 (let* ((image (image-get-display-property))
232 (edges (window-inside-edges))
233 (win-width (- (nth 2 edges
) (nth 0 edges
)))
234 (img-width (ceiling (car (image-size image
)))))
235 (image-set-window-hscroll (max 0 (- img-width win-width
)))))
238 "Scroll to the top-left corner of the image in the current window."
240 (image-set-window-hscroll 0)
241 (image-set-window-vscroll 0))
244 "Scroll to the bottom-right corner of the image in the current window."
246 (let* ((image (image-get-display-property))
247 (edges (window-inside-edges))
248 (win-width (- (nth 2 edges
) (nth 0 edges
)))
249 (img-width (ceiling (car (image-size image
))))
250 (win-height (- (nth 3 edges
) (nth 1 edges
)))
251 (img-height (ceiling (cdr (image-size image
)))))
252 (image-set-window-hscroll (max 0 (- img-width win-width
)))
253 (image-set-window-vscroll (max 0 (- img-height win-height
)))))
255 ;; Adjust frame and image size.
257 (defun image-mode-fit-frame ()
258 "Fit the frame to the current image.
259 This function assumes the current frame has only one window."
260 ;; FIXME: This does not take into account decorations like mode-line,
261 ;; minibuffer, header-line, ...
263 (let* ((saved (frame-parameter nil
'image-mode-saved-size
))
264 (display (image-get-display-property))
265 (size (image-size display
)))
267 (eq (caar saved
) (frame-width))
268 (eq (cdar saved
) (frame-height)))
269 (progn ;; Toggle back to previous non-fitted size.
270 (set-frame-parameter nil
'image-mode-saved-size nil
)
271 (setq size
(cdr saved
)))
272 ;; Round up size, and save current size so we can toggle back to it.
273 (setcar size
(ceiling (car size
)))
274 (setcdr size
(ceiling (cdr size
)))
275 (set-frame-parameter nil
'image-mode-saved-size
276 (cons size
(cons (frame-width) (frame-height)))))
277 (set-frame-width (selected-frame) (car size
))
278 (set-frame-height (selected-frame) (cdr size
))))
282 (defvar image-type nil
284 This variable is used to display the current image type in the mode line.")
285 (make-variable-buffer-local 'image-type
)
287 (defvar image-mode-map
288 (let ((map (make-sparse-keymap)))
289 (define-key map
"\C-c\C-c" 'image-toggle-display
)
290 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
291 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
292 (define-key map
[remap previous-line
] 'image-previous-line
)
293 (define-key map
[remap next-line
] 'image-next-line
)
294 (define-key map
[remap scroll-up
] 'image-scroll-up
)
295 (define-key map
[remap scroll-down
] 'image-scroll-down
)
296 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
297 (define-key map
[remap move-end-of-line
] 'image-eol
)
298 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
299 (define-key map
[remap end-of-buffer
] 'image-eob
)
301 "Major mode keymap for viewing images in Image mode.")
303 (defvar image-mode-text-map
304 (let ((map (make-sparse-keymap)))
305 (define-key map
"\C-c\C-c" 'image-toggle-display
)
307 "Major mode keymap for viewing images as text in Image mode.")
309 (defvar bookmark-make-record-function
)
313 "Major mode for image files.
314 You can use \\<image-mode-map>\\[image-toggle-display]
315 to toggle between display as an image and display as text."
317 (kill-all-local-variables)
318 (setq mode-name
"Image[text]")
319 (setq major-mode
'image-mode
)
320 ;; Use our own bookmarking function for images.
321 (set (make-local-variable 'bookmark-make-record-function
)
322 'image-bookmark-make-record
)
324 ;; Keep track of [vh]scroll when switching buffers
325 (image-mode-setup-winprops)
327 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
328 (if (and (display-images-p)
329 (not (image-get-display-property)))
330 (image-toggle-display)
331 ;; Set next vars when image is already displayed but local
332 ;; variables were cleared by kill-all-local-variables
333 (use-local-map image-mode-map
)
334 (setq cursor-type nil truncate-lines t
))
335 (run-mode-hooks 'image-mode-hook
)
336 (if (display-images-p)
337 (message "%s" (concat
338 (substitute-command-keys
339 "Type \\[image-toggle-display] to view as ")
340 (if (image-get-display-property)
341 "text" "an image") "."))))
344 (define-minor-mode image-minor-mode
345 "Toggle Image minor mode.
346 With arg, turn Image minor mode on if arg is positive, off otherwise.
347 See the command `image-mode' for more information on this mode."
348 nil
(:eval
(format " Image[%s]" image-type
)) image-mode-text-map
351 (if (not image-minor-mode
)
352 (image-toggle-display-text)
353 (if (image-get-display-property)
354 (setq cursor-type nil truncate-lines t
)
355 (setq image-type
"text"))
356 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)
357 (message "%s" (concat (substitute-command-keys
358 "Type \\[image-toggle-display] to view the image as ")
359 (if (image-get-display-property)
360 "text" "an image") "."))))
363 (defun image-mode-maybe ()
364 "Set major or minor mode for image files.
365 Set Image major mode only when there are no other major modes
366 associated with a filename in `auto-mode-alist'. When an image
367 filename matches another major mode in `auto-mode-alist' then
368 set that major mode and Image minor mode.
370 See commands `image-mode' and `image-minor-mode' for more
371 information on these modes."
376 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
377 '(image-mode image-mode-maybe
))
380 (if (assoc-default buffer-file-name mode-alist
'string-match
)
381 (let ((auto-mode-alist mode-alist
)
382 (magic-mode-alist nil
))
384 (image-minor-mode t
))
387 (defun image-toggle-display-text ()
388 "Showing the text of the image file."
389 (if (image-get-display-property)
390 (image-toggle-display)))
392 (defvar archive-superior-buffer
)
393 (defvar tar-superior-buffer
)
395 (defun image-toggle-display ()
396 "Start or stop displaying an image file as the actual image.
397 This command toggles between showing the text of the image file
398 and showing the image as an image."
400 (if (image-get-display-property)
401 (let ((inhibit-read-only t
)
403 (modified (buffer-modified-p)))
404 (remove-list-of-text-properties (point-min) (point-max)
405 '(display intangible read-nonsticky
406 read-only front-sticky
))
407 (set-buffer-modified-p modified
)
408 (kill-local-variable 'cursor-type
)
409 (kill-local-variable 'truncate-lines
)
410 (kill-local-variable 'auto-hscroll-mode
)
411 (use-local-map image-mode-text-map
)
412 (setq image-type
"text")
413 (if (eq major-mode
'image-mode
)
414 (setq mode-name
"Image[text]"))
415 (if (called-interactively-p)
416 (message "Repeat this command to go back to displaying the image")))
417 ;; Turn the image data into a real image, but only if the whole file
419 (let* ((filename (buffer-file-name))
420 (data-p (not (and filename
421 (file-readable-p filename
)
422 (not (file-remote-p filename
))
423 (not (buffer-modified-p))
424 (not (and (boundp 'archive-superior-buffer
)
425 archive-superior-buffer
))
426 (not (and (boundp 'tar-superior-buffer
)
427 tar-superior-buffer
)))))
428 (file-or-data (if data-p
430 (buffer-substring-no-properties (point-min) (point-max)))
432 (type (image-type file-or-data nil data-p
))
433 (image (create-image file-or-data type data-p
))
437 rear-nonsticky
(display intangible
)
438 read-only t front-sticky
(read-only)))
439 (inhibit-read-only t
)
441 (modified (buffer-modified-p)))
442 (image-refresh image
)
443 (add-text-properties (point-min) (point-max) props
)
444 (set-buffer-modified-p modified
)
445 ;; Inhibit the cursor when the buffer contains only an image,
446 ;; because cursors look very strange on top of images.
447 (setq cursor-type nil
)
448 ;; This just makes the arrow displayed in the right fringe
449 ;; area look correct when the image is wider than the window.
450 (setq truncate-lines t
)
451 ;; Allow navigation of large images
452 (set (make-local-variable 'auto-hscroll-mode
) nil
)
453 (use-local-map image-mode-map
)
454 (setq image-type type
)
455 (if (eq major-mode
'image-mode
)
456 (setq mode-name
(format "Image[%s]" type
)))
457 (if (called-interactively-p)
458 (message "Repeat this command to go back to displaying the file as text")))))
460 ;;; Support for bookmark.el
462 (defun image-bookmark-make-record (annotation)
464 `((filename .
,(buffer-file-name))
465 (image-type .
,image-type
)
466 (position .
,(point))
467 (handler . image-bookmark-jump
))))
469 ;; Take no chances with text properties
470 (set-text-properties 0 (length annotation
) nil annotation
)
473 (nconc the-record
(list (cons 'annotation annotation
))))
475 ;; Finally, return the completed record.
478 (declare-function bookmark-get-filename
"bookmark" (bookmark))
479 (declare-function bookmark-get-bookmark-record
"bookmark" (bookmark))
480 (declare-function bookmark-get-position
"bookmark" (bookmark))
483 (defun image-bookmark-jump (bmk)
484 ;; This implements the `handler' function interface for record type
485 ;; returned by `bookmark-make-record-function', which see.
486 (save-window-excursion
487 (let ((filename (bookmark-get-filename bmk
))
488 (type (cdr (assq 'image-type
(bookmark-get-bookmark-record bmk
))))
489 (pos (bookmark-get-position bmk
)))
491 (when (not (string= image-type type
))
492 (image-toggle-display))
493 (when (string= image-type
"text")
495 `((buffer ,(current-buffer)) (position ,(point))))))
497 (provide 'image-mode
)
499 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
500 ;;; image-mode.el ends here