Merge from gnus--devo--0
[emacs.git] / lisp / image-mode.el
bloba331064ecf3d8f82c3ae720a8b7d092992de82a7
1 ;;; image-mode.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 ;;
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/>.
23 ;;; Commentary:
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.
33 ;;; Code:
35 (require 'image)
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)
60 "Return winprops of WINDOW.
61 A winprops object has the shape (WINDOW . ALIST)."
62 (unless window (setq window (selected-window)))
63 (let ((winprops (assq window image-mode-winprops-alist)))
64 ;; For new windows, set defaults from the latest.
65 (unless winprops
66 (setq winprops (cons window
67 (copy-alist (cdar image-mode-winprops-alist))))
68 (run-hook-with-args 'image-mode-new-window-functions winprops))
69 ;; Move window to front.
70 (setq image-mode-winprops-alist
71 (cons winprops (delq winprops image-mode-winprops-alist)))
72 winprops))
74 (defun image-mode-window-get (prop &optional winprops)
75 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
76 (cdr (assq prop (cdr winprops))))
78 (defsetf image-mode-window-get (prop &optional winprops) (val)
79 `(image-mode-window-put ,prop ,val ,winprops))
81 (defun image-mode-window-put (prop val &optional winprops)
82 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
83 (setcdr winprops (cons (cons prop val)
84 (delq (assq prop (cdr winprops)) (cdr winprops)))))
86 (defun image-set-window-vscroll (vscroll)
87 (setf (image-mode-window-get 'vscroll) vscroll)
88 (set-window-vscroll (selected-window) vscroll))
90 (defun image-set-window-hscroll (ncol)
91 (setf (image-mode-window-get 'hscroll) ncol)
92 (set-window-hscroll (selected-window) ncol))
94 (defun image-mode-reapply-winprops ()
95 ;; When set-window-buffer, set hscroll and vscroll to what they were
96 ;; last time the image was displayed in this window.
97 (when (listp image-mode-winprops-alist)
98 (let* ((winprops (image-mode-winprops))
99 (hscroll (image-mode-window-get 'hscroll winprops))
100 (vscroll (image-mode-window-get 'vscroll winprops)))
101 (if hscroll (set-window-hscroll (selected-window) hscroll))
102 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
104 (defun image-mode-setup-winprops ()
105 ;; Record current scroll settings.
106 (unless (listp image-mode-winprops-alist)
107 (setq image-mode-winprops-alist nil))
108 (add-hook 'window-configuration-change-hook
109 'image-mode-reapply-winprops nil t))
111 ;;; Image scrolling functions
113 (defun image-get-display-property ()
114 (get-char-property (point-min) 'display
115 ;; There might be different images for different displays.
116 (if (eq (window-buffer) (current-buffer))
117 (selected-window))))
119 (defun image-forward-hscroll (&optional n)
120 "Scroll image in current window to the left by N character widths.
121 Stop if the right edge of the image is reached."
122 (interactive "p")
123 (cond ((= n 0) nil)
124 ((< n 0)
125 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
127 (let* ((image (image-get-display-property))
128 (edges (window-inside-edges))
129 (win-width (- (nth 2 edges) (nth 0 edges)))
130 (img-width (ceiling (car (image-size image)))))
131 (image-set-window-hscroll (min (max 0 (- img-width win-width))
132 (+ n (window-hscroll))))))))
134 (defun image-backward-hscroll (&optional n)
135 "Scroll image in current window to the right by N character widths.
136 Stop if the left edge of the image is reached."
137 (interactive "p")
138 (image-forward-hscroll (- n)))
140 (defun image-next-line (&optional n)
141 "Scroll image in current window upward by N lines.
142 Stop if the bottom edge of the image is reached."
143 (interactive "p")
144 (cond ((= n 0) nil)
145 ((< n 0)
146 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
148 (let* ((image (image-get-display-property))
149 (edges (window-inside-edges))
150 (win-height (- (nth 3 edges) (nth 1 edges)))
151 (img-height (ceiling (cdr (image-size image)))))
152 (image-set-window-vscroll (min (max 0 (- img-height win-height))
153 (+ n (window-vscroll))))))))
155 (defun image-previous-line (&optional n)
156 "Scroll image in current window downward by N lines.
157 Stop if the top edge of the image is reached."
158 (interactive "p")
159 (image-next-line (- n)))
161 (defun image-scroll-up (&optional n)
162 "Scroll image in current window upward by N lines.
163 Stop if the bottom edge of the image is reached.
164 If ARG is omitted or nil, scroll upward by a near full screen.
165 A near full screen is `next-screen-context-lines' less than a full screen.
166 Negative ARG means scroll downward.
167 If ARG is the atom `-', scroll downward by nearly full screen.
168 When calling from a program, supply as argument a number, nil, or `-'."
169 (interactive "P")
170 (cond ((null n)
171 (let* ((edges (window-inside-edges))
172 (win-height (- (nth 3 edges) (nth 1 edges))))
173 (image-next-line
174 (max 0 (- win-height next-screen-context-lines)))))
175 ((eq n '-)
176 (let* ((edges (window-inside-edges))
177 (win-height (- (nth 3 edges) (nth 1 edges))))
178 (image-next-line
179 (min 0 (- next-screen-context-lines win-height)))))
180 (t (image-next-line (prefix-numeric-value n)))))
182 (defun image-scroll-down (&optional n)
183 "Scroll image in current window downward by N lines.
184 Stop if the top edge of the image is reached.
185 If ARG is omitted or nil, scroll downward by a near full screen.
186 A near full screen is `next-screen-context-lines' less than a full screen.
187 Negative ARG means scroll upward.
188 If ARG is the atom `-', scroll upward by nearly full screen.
189 When calling from a program, supply as argument a number, nil, or `-'."
190 (interactive "P")
191 (cond ((null n)
192 (let* ((edges (window-inside-edges))
193 (win-height (- (nth 3 edges) (nth 1 edges))))
194 (image-next-line
195 (min 0 (- next-screen-context-lines win-height)))))
196 ((eq n '-)
197 (let* ((edges (window-inside-edges))
198 (win-height (- (nth 3 edges) (nth 1 edges))))
199 (image-next-line
200 (max 0 (- win-height next-screen-context-lines)))))
201 (t (image-next-line (- (prefix-numeric-value n))))))
203 (defun image-bol (arg)
204 "Scroll horizontally to the left edge of the image in the current window.
205 With argument ARG not nil or 1, move forward ARG - 1 lines first,
206 stopping if the top or bottom edge of the image is reached."
207 (interactive "p")
208 (and arg
209 (/= (setq arg (prefix-numeric-value arg)) 1)
210 (image-next-line (- arg 1)))
211 (image-set-window-hscroll 0))
213 (defun image-eol (arg)
214 "Scroll horizontally to the right 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."
217 (interactive "p")
218 (and arg
219 (/= (setq arg (prefix-numeric-value arg)) 1)
220 (image-next-line (- arg 1)))
221 (let* ((image (image-get-display-property))
222 (edges (window-inside-edges))
223 (win-width (- (nth 2 edges) (nth 0 edges)))
224 (img-width (ceiling (car (image-size image)))))
225 (image-set-window-hscroll (max 0 (- img-width win-width)))))
227 (defun image-bob ()
228 "Scroll to the top-left corner of the image in the current window."
229 (interactive)
230 (image-set-window-hscroll 0)
231 (image-set-window-vscroll 0))
233 (defun image-eob ()
234 "Scroll to the bottom-right corner of the image in the current window."
235 (interactive)
236 (let* ((image (image-get-display-property))
237 (edges (window-inside-edges))
238 (win-width (- (nth 2 edges) (nth 0 edges)))
239 (img-width (ceiling (car (image-size image))))
240 (win-height (- (nth 3 edges) (nth 1 edges)))
241 (img-height (ceiling (cdr (image-size image)))))
242 (image-set-window-hscroll (max 0 (- img-width win-width)))
243 (image-set-window-vscroll (max 0 (- img-height win-height)))))
245 ;; Adjust frame and image size.
247 (defun image-mode-fit-frame ()
248 "Fit the frame to the current image.
249 This function assumes the current frame has only one window."
250 ;; FIXME: This does not take into account decorations like mode-line,
251 ;; minibuffer, header-line, ...
252 (interactive)
253 (let* ((saved (frame-parameter nil 'image-mode-saved-size))
254 (display (image-get-display-property))
255 (size (image-size display)))
256 (if (and saved
257 (eq (caar saved) (frame-width))
258 (eq (cdar saved) (frame-height)))
259 (progn ;; Toggle back to previous non-fitted size.
260 (set-frame-parameter nil 'image-mode-saved-size nil)
261 (setq size (cdr saved)))
262 ;; Round up size, and save current size so we can toggle back to it.
263 (setcar size (ceiling (car size)))
264 (setcdr size (ceiling (cdr size)))
265 (set-frame-parameter nil 'image-mode-saved-size
266 (cons size (cons (frame-width) (frame-height)))))
267 (set-frame-width (selected-frame) (car size))
268 (set-frame-height (selected-frame) (cdr size))))
270 ;;; Image Mode setup
272 (defvar image-type nil
273 "Current image type.
274 This variable is used to display the current image type in the mode line.")
275 (make-variable-buffer-local 'image-type)
277 (defvar image-mode-map
278 (let ((map (make-sparse-keymap)))
279 (define-key map "\C-c\C-c" 'image-toggle-display)
280 (define-key map [remap forward-char] 'image-forward-hscroll)
281 (define-key map [remap backward-char] 'image-backward-hscroll)
282 (define-key map [remap previous-line] 'image-previous-line)
283 (define-key map [remap next-line] 'image-next-line)
284 (define-key map [remap scroll-up] 'image-scroll-up)
285 (define-key map [remap scroll-down] 'image-scroll-down)
286 (define-key map [remap move-beginning-of-line] 'image-bol)
287 (define-key map [remap move-end-of-line] 'image-eol)
288 (define-key map [remap beginning-of-buffer] 'image-bob)
289 (define-key map [remap end-of-buffer] 'image-eob)
290 map)
291 "Major mode keymap for viewing images in Image mode.")
293 (defvar image-mode-text-map
294 (let ((map (make-sparse-keymap)))
295 (define-key map "\C-c\C-c" 'image-toggle-display)
296 map)
297 "Major mode keymap for viewing images as text in Image mode.")
299 (defvar bookmark-make-record-function)
301 ;;;###autoload
302 (defun image-mode ()
303 "Major mode for image files.
304 You can use \\<image-mode-map>\\[image-toggle-display]
305 to toggle between display as an image and display as text."
306 (interactive)
307 (kill-all-local-variables)
308 (setq mode-name "Image[text]")
309 (setq major-mode 'image-mode)
310 ;; Use our own bookmarking function for images.
311 (set (make-local-variable 'bookmark-make-record-function)
312 'image-bookmark-make-record)
314 ;; Keep track of [vh]scroll when switching buffers
315 (image-mode-setup-winprops)
317 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
318 (if (and (display-images-p)
319 (not (image-get-display-property)))
320 (image-toggle-display)
321 ;; Set next vars when image is already displayed but local
322 ;; variables were cleared by kill-all-local-variables
323 (use-local-map image-mode-map)
324 (setq cursor-type nil truncate-lines t))
325 (run-mode-hooks 'image-mode-hook)
326 (if (display-images-p)
327 (message "%s" (concat
328 (substitute-command-keys
329 "Type \\[image-toggle-display] to view as ")
330 (if (image-get-display-property)
331 "text" "an image") "."))))
333 ;;;###autoload
334 (define-minor-mode image-minor-mode
335 "Toggle Image minor mode.
336 With arg, turn Image minor mode on if arg is positive, off otherwise.
337 See the command `image-mode' for more information on this mode."
338 nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
339 :group 'image
340 :version "22.1"
341 (if (not image-minor-mode)
342 (image-toggle-display-text)
343 (if (image-get-display-property)
344 (setq cursor-type nil truncate-lines t)
345 (setq image-type "text"))
346 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
347 (message "%s" (concat (substitute-command-keys
348 "Type \\[image-toggle-display] to view the image as ")
349 (if (image-get-display-property)
350 "text" "an image") "."))))
352 ;;;###autoload
353 (defun image-mode-maybe ()
354 "Set major or minor mode for image files.
355 Set Image major mode only when there are no other major modes
356 associated with a filename in `auto-mode-alist'. When an image
357 filename matches another major mode in `auto-mode-alist' then
358 set that major mode and Image minor mode.
360 See commands `image-mode' and `image-minor-mode' for more
361 information on these modes."
362 (interactive)
363 (let* ((mode-alist
364 (delq nil (mapcar
365 (lambda (elt)
366 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
367 '(image-mode image-mode-maybe))
368 elt))
369 auto-mode-alist))))
370 (if (assoc-default buffer-file-name mode-alist 'string-match)
371 (let ((auto-mode-alist mode-alist)
372 (magic-mode-alist nil))
373 (set-auto-mode)
374 (image-minor-mode t))
375 (image-mode))))
377 (defun image-toggle-display-text ()
378 "Showing the text of the image file."
379 (if (image-get-display-property)
380 (image-toggle-display)))
382 (defvar archive-superior-buffer)
383 (defvar tar-superior-buffer)
385 (defun image-toggle-display ()
386 "Start or stop displaying an image file as the actual image.
387 This command toggles between showing the text of the image file
388 and showing the image as an image."
389 (interactive)
390 (if (image-get-display-property)
391 (let ((inhibit-read-only t)
392 (buffer-undo-list t)
393 (modified (buffer-modified-p)))
394 (remove-list-of-text-properties (point-min) (point-max)
395 '(display intangible read-nonsticky
396 read-only front-sticky))
397 (set-buffer-modified-p modified)
398 (kill-local-variable 'cursor-type)
399 (kill-local-variable 'truncate-lines)
400 (kill-local-variable 'auto-hscroll-mode)
401 (use-local-map image-mode-text-map)
402 (setq image-type "text")
403 (if (eq major-mode 'image-mode)
404 (setq mode-name "Image[text]"))
405 (if (called-interactively-p)
406 (message "Repeat this command to go back to displaying the image")))
407 ;; Turn the image data into a real image, but only if the whole file
408 ;; was inserted
409 (let* ((filename (buffer-file-name))
410 (data-p (not (and filename
411 (file-readable-p filename)
412 (not (file-remote-p filename))
413 (not (buffer-modified-p))
414 (not (and (boundp 'archive-superior-buffer)
415 archive-superior-buffer))
416 (not (and (boundp 'tar-superior-buffer)
417 tar-superior-buffer)))))
418 (file-or-data (if data-p
419 (string-make-unibyte
420 (buffer-substring-no-properties (point-min) (point-max)))
421 filename))
422 (type (image-type file-or-data nil data-p))
423 (image (create-image file-or-data type data-p))
424 (props
425 `(display ,image
426 intangible ,image
427 rear-nonsticky (display intangible)
428 read-only t front-sticky (read-only)))
429 (inhibit-read-only t)
430 (buffer-undo-list t)
431 (modified (buffer-modified-p)))
432 (image-refresh image)
433 (add-text-properties (point-min) (point-max) props)
434 (set-buffer-modified-p modified)
435 ;; Inhibit the cursor when the buffer contains only an image,
436 ;; because cursors look very strange on top of images.
437 (setq cursor-type nil)
438 ;; This just makes the arrow displayed in the right fringe
439 ;; area look correct when the image is wider than the window.
440 (setq truncate-lines t)
441 ;; Allow navigation of large images
442 (set (make-local-variable 'auto-hscroll-mode) nil)
443 (use-local-map image-mode-map)
444 (setq image-type type)
445 (if (eq major-mode 'image-mode)
446 (setq mode-name (format "Image[%s]" type)))
447 (if (called-interactively-p)
448 (message "Repeat this command to go back to displaying the file as text")))))
450 ;;; Support for bookmark.el
452 (defun image-bookmark-make-record (annotation)
453 (let ((the-record
454 `((filename . ,(buffer-file-name))
455 (image-type . ,image-type)
456 (position . ,(point))
457 (handler . image-bookmark-jump))))
459 ;; Take no chances with text properties
460 (set-text-properties 0 (length annotation) nil annotation)
462 (when annotation
463 (nconc the-record (list (cons 'annotation annotation))))
465 ;; Finally, return the completed record.
466 the-record))
468 (declare-function bookmark-get-filename "bookmark" (bookmark))
469 (declare-function bookmark-get-bookmark-record "bookmark" (bookmark))
470 (declare-function bookmark-get-position "bookmark" (bookmark))
472 ;;;###autoload
473 (defun image-bookmark-jump (bmk)
474 ;; This implements the `handler' function interface for record type
475 ;; returned by `bookmark-make-record-function', which see.
476 (save-window-excursion
477 (let ((filename (bookmark-get-filename bmk))
478 (type (cdr (assq 'image-type (bookmark-get-bookmark-record bmk))))
479 (pos (bookmark-get-position bmk)))
480 (find-file filename)
481 (when (not (string= image-type type))
482 (image-toggle-display))
483 (when (string= image-type "text")
484 (goto-char pos))
485 `((buffer ,(current-buffer)) (position ,(point))))))
487 (provide 'image-mode)
489 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
490 ;;; image-mode.el ends here