Merge from emacs--rel--22
[emacs.git] / lisp / image-mode.el
blob23d9aff737d2be7ef1db7630555ee5f395263817
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, or (at your option)
13 ;; 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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; Defines a major mode for visiting image files
28 ;; that allows conversion between viewing the text of the file
29 ;; and viewing the file as an image. Viewing the image
30 ;; works by putting a `display' text-property on the
31 ;; image data, with the image-data still present underneath; if the
32 ;; resulting buffer file is saved to another name it will correctly save
33 ;; the image data to the new file.
35 ;;; Code:
37 (require 'image)
38 (eval-when-compile (require 'cl))
40 ;;;###autoload (push '("\\.jpe?g\\'" . image-mode) auto-mode-alist)
41 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
42 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
43 ;;;###autoload (push '("\\.tiff?\\'" . image-mode) auto-mode-alist)
44 ;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist)
46 ;;;###autoload (push '("\\.x[bp]m\\'" . c-mode) auto-mode-alist)
47 ;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
49 ;;;###autoload (push '("\\.svgz?\\'" . xml-mode) auto-mode-alist)
50 ;;;###autoload (push '("\\.svgz?\\'" . image-mode-maybe) auto-mode-alist)
52 ;;; Image mode window-info management.
54 (defvar image-mode-winprops-alist t)
55 (make-variable-buffer-local 'image-mode-winprops-alist)
57 (defvar image-mode-new-window-functions nil
58 "Special hook run when image data is requested in a new window.
59 It is called with one argument, the initial WINPROPS.")
61 (defun image-mode-winprops (&optional window)
62 "Return winprops of WINDOW.
63 A winprops object has the shape (WINDOW . ALIST)."
64 (unless window (setq window (selected-window)))
65 (let ((winprops (assq window image-mode-winprops-alist)))
66 ;; For new windows, set defaults from the latest.
67 (unless winprops
68 (setq winprops (cons window
69 (copy-alist (cdar image-mode-winprops-alist))))
70 (run-hook-with-args 'image-mode-new-window-functions winprops))
71 ;; Move window to front.
72 (setq image-mode-winprops-alist
73 (cons winprops (delq winprops image-mode-winprops-alist)))
74 winprops))
76 (defun image-mode-window-get (prop &optional winprops)
77 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
78 (cdr (assq prop (cdr winprops))))
80 (defsetf image-mode-window-get (prop &optional winprops) (val)
81 `(image-mode-window-put ,prop ,val ,winprops))
83 (defun image-mode-window-put (prop val &optional winprops)
84 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
85 (setcdr winprops (cons (cons prop val)
86 (delq (assq prop (cdr winprops)) (cdr winprops)))))
88 (defun image-set-window-vscroll (vscroll)
89 (setf (image-mode-window-get 'vscroll) vscroll)
90 (set-window-vscroll (selected-window) vscroll))
92 (defun image-set-window-hscroll (ncol)
93 (setf (image-mode-window-get 'hscroll) ncol)
94 (set-window-hscroll (selected-window) ncol))
96 (defun image-mode-reapply-winprops ()
97 (walk-windows
98 (lambda (win)
99 (with-current-buffer (window-buffer win)
100 ;; When set-window-buffer, set hscroll and vscroll to what they were
101 ;; last time the image was displayed in this window.
102 (when (listp image-mode-winprops-alist)
103 (let* ((winprops (image-mode-winprops win))
104 (hscroll (image-mode-window-get 'hscroll winprops))
105 (vscroll (image-mode-window-get 'vscroll winprops)))
106 (if hscroll (set-window-hscroll win hscroll))
107 (if vscroll (set-window-vscroll win vscroll))))))
108 'nomini
109 (selected-frame)))
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))
118 ;;; Image scrolling functions
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))))
126 (defun image-forward-hscroll (&optional n)
127 "Scroll image in current window to the left by N character widths.
128 Stop if the right edge of the image is reached."
129 (interactive "p")
130 (cond ((= n 0) nil)
131 ((< n 0)
132 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
134 (let* ((image (image-get-display-property))
135 (edges (window-inside-edges))
136 (win-width (- (nth 2 edges) (nth 0 edges)))
137 (img-width (ceiling (car (image-size image)))))
138 (image-set-window-hscroll (min (max 0 (- img-width win-width))
139 (+ n (window-hscroll))))))))
141 (defun image-backward-hscroll (&optional n)
142 "Scroll image in current window to the right by N character widths.
143 Stop if the left edge of the image is reached."
144 (interactive "p")
145 (image-forward-hscroll (- n)))
147 (defun image-next-line (&optional n)
148 "Scroll image in current window upward by N lines.
149 Stop if the bottom edge of the image is reached."
150 (interactive "p")
151 (cond ((= n 0) nil)
152 ((< n 0)
153 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
155 (let* ((image (image-get-display-property))
156 (edges (window-inside-edges))
157 (win-height (- (nth 3 edges) (nth 1 edges)))
158 (img-height (ceiling (cdr (image-size image)))))
159 (image-set-window-vscroll (min (max 0 (- img-height win-height))
160 (+ n (window-vscroll))))))))
162 (defun image-previous-line (&optional n)
163 "Scroll image in current window downward by N lines.
164 Stop if the top edge of the image is reached."
165 (interactive "p")
166 (image-next-line (- n)))
168 (defun image-scroll-up (&optional n)
169 "Scroll image in current window upward by N lines.
170 Stop if the bottom edge of the image is reached.
171 If ARG is omitted or nil, scroll upward by a near full screen.
172 A near full screen is `next-screen-context-lines' less than a full screen.
173 Negative ARG means scroll downward.
174 If ARG is the atom `-', scroll downward by nearly full screen.
175 When calling from a program, supply as argument a number, nil, or `-'."
176 (interactive "P")
177 (cond ((null n)
178 (let* ((edges (window-inside-edges))
179 (win-height (- (nth 3 edges) (nth 1 edges))))
180 (image-next-line
181 (max 0 (- win-height next-screen-context-lines)))))
182 ((eq n '-)
183 (let* ((edges (window-inside-edges))
184 (win-height (- (nth 3 edges) (nth 1 edges))))
185 (image-next-line
186 (min 0 (- next-screen-context-lines win-height)))))
187 (t (image-next-line (prefix-numeric-value n)))))
189 (defun image-scroll-down (&optional n)
190 "Scroll image in current window downward by N lines.
191 Stop if the top edge of the image is reached.
192 If ARG is omitted or nil, scroll downward by a near full screen.
193 A near full screen is `next-screen-context-lines' less than a full screen.
194 Negative ARG means scroll upward.
195 If ARG is the atom `-', scroll upward by nearly full screen.
196 When calling from a program, supply as argument a number, nil, or `-'."
197 (interactive "P")
198 (cond ((null 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 ((eq n '-)
204 (let* ((edges (window-inside-edges))
205 (win-height (- (nth 3 edges) (nth 1 edges))))
206 (image-next-line
207 (max 0 (- win-height next-screen-context-lines)))))
208 (t (image-next-line (- (prefix-numeric-value n))))))
210 (defun image-bol (arg)
211 "Scroll horizontally to the left edge of the image in the current window.
212 With argument ARG not nil or 1, move forward ARG - 1 lines first,
213 stopping if the top or bottom edge of the image is reached."
214 (interactive "p")
215 (and arg
216 (/= (setq arg (prefix-numeric-value arg)) 1)
217 (image-next-line (- arg 1)))
218 (image-set-window-hscroll 0))
220 (defun image-eol (arg)
221 "Scroll horizontally to the right edge of the image in the current window.
222 With argument ARG not nil or 1, move forward ARG - 1 lines first,
223 stopping if the top or bottom edge of the image is reached."
224 (interactive "p")
225 (and arg
226 (/= (setq arg (prefix-numeric-value arg)) 1)
227 (image-next-line (- arg 1)))
228 (let* ((image (image-get-display-property))
229 (edges (window-inside-edges))
230 (win-width (- (nth 2 edges) (nth 0 edges)))
231 (img-width (ceiling (car (image-size image)))))
232 (image-set-window-hscroll (max 0 (- img-width win-width)))))
234 (defun image-bob ()
235 "Scroll to the top-left corner of the image in the current window."
236 (interactive)
237 (image-set-window-hscroll 0)
238 (image-set-window-vscroll 0))
240 (defun image-eob ()
241 "Scroll to the bottom-right corner of the image in the current window."
242 (interactive)
243 (let* ((image (image-get-display-property))
244 (edges (window-inside-edges))
245 (win-width (- (nth 2 edges) (nth 0 edges)))
246 (img-width (ceiling (car (image-size image))))
247 (win-height (- (nth 3 edges) (nth 1 edges)))
248 (img-height (ceiling (cdr (image-size image)))))
249 (image-set-window-hscroll (max 0 (- img-width win-width)))
250 (image-set-window-vscroll (max 0 (- img-height win-height)))))
252 ;;; Image Mode setup
254 (defvar image-type nil
255 "Current image type.
256 This variable is used to display the current image type in the mode line.")
257 (make-variable-buffer-local 'image-type)
259 (defvar image-mode-map
260 (let ((map (make-sparse-keymap)))
261 (define-key map "\C-c\C-c" 'image-toggle-display)
262 (define-key map [remap forward-char] 'image-forward-hscroll)
263 (define-key map [remap backward-char] 'image-backward-hscroll)
264 (define-key map [remap previous-line] 'image-previous-line)
265 (define-key map [remap next-line] 'image-next-line)
266 (define-key map [remap scroll-up] 'image-scroll-up)
267 (define-key map [remap scroll-down] 'image-scroll-down)
268 (define-key map [remap move-beginning-of-line] 'image-bol)
269 (define-key map [remap move-end-of-line] 'image-eol)
270 (define-key map [remap beginning-of-buffer] 'image-bob)
271 (define-key map [remap end-of-buffer] 'image-eob)
272 map)
273 "Major mode keymap for viewing images in Image mode.")
275 (defvar image-mode-text-map
276 (let ((map (make-sparse-keymap)))
277 (define-key map "\C-c\C-c" 'image-toggle-display)
278 map)
279 "Major mode keymap for viewing images as text in Image mode.")
281 (defvar bookmark-make-cell-function)
283 ;;;###autoload
284 (defun image-mode ()
285 "Major mode for image files.
286 You can use \\<image-mode-map>\\[image-toggle-display]
287 to toggle between display as an image and display as text."
288 (interactive)
289 (kill-all-local-variables)
290 (setq mode-name "Image[text]")
291 (setq major-mode 'image-mode)
292 ;; Use our own bookmarking function for images.
293 (set (make-local-variable 'bookmark-make-cell-function)
294 'image-bookmark-make-cell)
296 ;; Keep track of [vh]scroll when switching buffers
297 (image-mode-setup-winprops)
299 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
300 (if (and (display-images-p)
301 (not (image-get-display-property)))
302 (image-toggle-display)
303 ;; Set next vars when image is already displayed but local
304 ;; variables were cleared by kill-all-local-variables
305 (use-local-map image-mode-map)
306 (setq cursor-type nil truncate-lines t))
307 (run-mode-hooks 'image-mode-hook)
308 (if (display-images-p)
309 (message "%s" (concat
310 (substitute-command-keys
311 "Type \\[image-toggle-display] to view as ")
312 (if (image-get-display-property)
313 "text" "an image") "."))))
315 ;;;###autoload
316 (define-minor-mode image-minor-mode
317 "Toggle Image minor mode.
318 With arg, turn Image minor mode on if arg is positive, off otherwise.
319 See the command `image-mode' for more information on this mode."
320 nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
321 :group 'image
322 :version "22.1"
323 (if (not image-minor-mode)
324 (image-toggle-display-text)
325 (if (image-get-display-property)
326 (setq cursor-type nil truncate-lines t)
327 (setq image-type "text"))
328 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
329 (message "%s" (concat (substitute-command-keys
330 "Type \\[image-toggle-display] to view the image as ")
331 (if (image-get-display-property)
332 "text" "an image") "."))))
334 ;;;###autoload
335 (defun image-mode-maybe ()
336 "Set major or minor mode for image files.
337 Set Image major mode only when there are no other major modes
338 associated with a filename in `auto-mode-alist'. When an image
339 filename matches another major mode in `auto-mode-alist' then
340 set that major mode and Image minor mode.
342 See commands `image-mode' and `image-minor-mode' for more
343 information on these modes."
344 (interactive)
345 (let* ((mode-alist
346 (delq nil (mapcar
347 (lambda (elt)
348 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
349 '(image-mode image-mode-maybe))
350 elt))
351 auto-mode-alist))))
352 (if (assoc-default buffer-file-name mode-alist 'string-match)
353 (let ((auto-mode-alist mode-alist)
354 (magic-mode-alist nil))
355 (set-auto-mode)
356 (image-minor-mode t))
357 (image-mode))))
359 (defun image-toggle-display-text ()
360 "Showing the text of the image file."
361 (if (image-get-display-property)
362 (image-toggle-display)))
364 (defvar archive-superior-buffer)
365 (defvar tar-superior-buffer)
367 (defun image-toggle-display ()
368 "Start or stop displaying an image file as the actual image.
369 This command toggles between showing the text of the image file
370 and showing the image as an image."
371 (interactive)
372 (if (image-get-display-property)
373 (let ((inhibit-read-only t)
374 (buffer-undo-list t)
375 (modified (buffer-modified-p)))
376 (remove-list-of-text-properties (point-min) (point-max)
377 '(display intangible read-nonsticky
378 read-only front-sticky))
379 (set-buffer-modified-p modified)
380 (kill-local-variable 'cursor-type)
381 (kill-local-variable 'truncate-lines)
382 (kill-local-variable 'auto-hscroll-mode)
383 (use-local-map image-mode-text-map)
384 (setq image-type "text")
385 (if (eq major-mode 'image-mode)
386 (setq mode-name "Image[text]"))
387 (if (called-interactively-p)
388 (message "Repeat this command to go back to displaying the image")))
389 ;; Turn the image data into a real image, but only if the whole file
390 ;; was inserted
391 (let* ((filename (buffer-file-name))
392 (data-p (not (and filename
393 (file-readable-p filename)
394 (not (file-remote-p filename))
395 (not (buffer-modified-p))
396 (not (and (boundp 'archive-superior-buffer)
397 archive-superior-buffer))
398 (not (and (boundp 'tar-superior-buffer)
399 tar-superior-buffer)))))
400 (file-or-data (if data-p
401 (string-make-unibyte
402 (buffer-substring-no-properties (point-min) (point-max)))
403 filename))
404 (type (image-type file-or-data nil data-p))
405 (image (create-image file-or-data type data-p))
406 (props
407 `(display ,image
408 intangible ,image
409 rear-nonsticky (display intangible)
410 read-only t front-sticky (read-only)))
411 (inhibit-read-only t)
412 (buffer-undo-list t)
413 (modified (buffer-modified-p)))
414 (image-refresh image)
415 (add-text-properties (point-min) (point-max) props)
416 (set-buffer-modified-p modified)
417 ;; Inhibit the cursor when the buffer contains only an image,
418 ;; because cursors look very strange on top of images.
419 (setq cursor-type nil)
420 ;; This just makes the arrow displayed in the right fringe
421 ;; area look correct when the image is wider than the window.
422 (setq truncate-lines t)
423 ;; Allow navigation of large images
424 (set (make-local-variable 'auto-hscroll-mode) nil)
425 (use-local-map image-mode-map)
426 (setq image-type type)
427 (if (eq major-mode 'image-mode)
428 (setq mode-name (format "Image[%s]" type)))
429 (if (called-interactively-p)
430 (message "Repeat this command to go back to displaying the file as text")))))
432 ;;; Support for bookmark.el
434 (defun image-bookmark-make-cell (annotation &rest args)
435 (let ((the-record
436 `((filename . ,(buffer-file-name))
437 (image-type . ,image-type)
438 (position . ,(point))
439 (handler . image-bookmark-jump))))
441 ;; Take no chances with text properties
442 (set-text-properties 0 (length annotation) nil annotation)
444 (when annotation
445 (nconc the-record (list (cons 'annotation annotation))))
447 ;; Finally, return the completed record.
448 the-record))
450 (declare-function bookmark-get-filename "bookmark" (bookmark))
451 (declare-function bookmark-get-bookmark-record "bookmark" (bookmark))
452 (declare-function bookmark-get-position "bookmark" (bookmark))
454 ;;;###autoload
455 (defun image-bookmark-jump (bmk)
456 ;; This implements the `handler' function interface for record type
457 ;; returned by `bookmark-make-cell-function', which see.
458 (save-window-excursion
459 (let ((filename (bookmark-get-filename bmk))
460 (type (cdr (assq 'image-type (bookmark-get-bookmark-record bmk))))
461 (pos (bookmark-get-position bmk)))
462 (find-file filename)
463 (when (not (string= image-type type))
464 (image-toggle-display))
465 (when (string= image-type "text")
466 (goto-char pos))
467 `((buffer ,(current-buffer)) (position ,(point))))))
469 (provide 'image-mode)
471 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
472 ;;; image-mode.el ends here