Merge from emacs--rel--22
[bpt/emacs.git] / lisp / image-mode.el
1 ;;; image-mode.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Richard Stallman <rms@gnu.org>
6 ;; Keywords: multimedia
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
34
35 ;;; Code:
36
37 (require 'image)
38
39 ;;;###autoload (push '("\\.jpe?g\\'" . image-mode) auto-mode-alist)
40 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
41 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
42 ;;;###autoload (push '("\\.tiff?\\'" . image-mode) auto-mode-alist)
43 ;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist)
44 ;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
45
46 ;;; Image scrolling functions
47
48 (defun image-forward-hscroll (&optional n)
49 "Scroll image in current window to the left by N character widths.
50 Stop if the right edge of the image is reached."
51 (interactive "p")
52 (cond ((= n 0) nil)
53 ((< n 0)
54 (set-window-hscroll (selected-window)
55 (max 0 (+ (window-hscroll) n))))
56 (t
57 (let* ((image (get-text-property 1 'display))
58 (edges (window-inside-edges))
59 (win-width (- (nth 2 edges) (nth 0 edges)))
60 (img-width (ceiling (car (image-size image)))))
61 (set-window-hscroll (selected-window)
62 (min (max 0 (- img-width win-width))
63 (+ n (window-hscroll))))))))
64
65 (defun image-backward-hscroll (&optional n)
66 "Scroll image in current window to the right by N character widths.
67 Stop if the left edge of the image is reached."
68 (interactive "p")
69 (image-forward-hscroll (- n)))
70
71 (defun image-next-line (&optional n)
72 "Scroll image in current window upward by N lines.
73 Stop if the bottom edge of the image is reached."
74 (interactive "p")
75 (cond ((= n 0) nil)
76 ((< n 0)
77 (set-window-vscroll (selected-window)
78 (max 0 (+ (window-vscroll) n))))
79 (t
80 (let* ((image (get-text-property 1 'display))
81 (edges (window-inside-edges))
82 (win-height (- (nth 3 edges) (nth 1 edges)))
83 (img-height (ceiling (cdr (image-size image)))))
84 (set-window-vscroll (selected-window)
85 (min (max 0 (- img-height win-height))
86 (+ n (window-vscroll))))))))
87
88 (defun image-previous-line (&optional n)
89 "Scroll image in current window downward by N lines.
90 Stop if the top edge of the image is reached."
91 (interactive "p")
92 (image-next-line (- n)))
93
94 (defun image-scroll-up (&optional n)
95 "Scroll image in current window upward by N lines.
96 Stop if the bottom edge of the image is reached.
97 If ARG is omitted or nil, scroll upward by a near full screen.
98 A near full screen is `next-screen-context-lines' less than a full screen.
99 Negative ARG means scroll downward.
100 If ARG is the atom `-', scroll downward by nearly full screen.
101 When calling from a program, supply as argument a number, nil, or `-'."
102 (interactive "P")
103 (cond ((null n)
104 (let* ((edges (window-inside-edges))
105 (win-height (- (nth 3 edges) (nth 1 edges))))
106 (image-next-line
107 (max 0 (- win-height next-screen-context-lines)))))
108 ((eq n '-)
109 (let* ((edges (window-inside-edges))
110 (win-height (- (nth 3 edges) (nth 1 edges))))
111 (image-next-line
112 (min 0 (- next-screen-context-lines win-height)))))
113 (t (image-next-line (prefix-numeric-value n)))))
114
115 (defun image-scroll-down (&optional n)
116 "Scroll image in current window downward by N lines
117 Stop if the top edge of the image is reached.
118 If ARG is omitted or nil, scroll downward by a near full screen.
119 A near full screen is `next-screen-context-lines' less than a full screen.
120 Negative ARG means scroll upward.
121 If ARG is the atom `-', scroll upward by nearly full screen.
122 When calling from a program, supply as argument a number, nil, or `-'."
123 (interactive "P")
124 (cond ((null n)
125 (let* ((edges (window-inside-edges))
126 (win-height (- (nth 3 edges) (nth 1 edges))))
127 (image-next-line
128 (min 0 (- next-screen-context-lines win-height)))))
129 ((eq n '-)
130 (let* ((edges (window-inside-edges))
131 (win-height (- (nth 3 edges) (nth 1 edges))))
132 (image-next-line
133 (max 0 (- win-height next-screen-context-lines)))))
134 (t (image-next-line (- (prefix-numeric-value n))))))
135
136 (defun image-bol (arg)
137 "Scroll horizontally to the left edge of the image in the current window.
138 With argument ARG not nil or 1, move forward ARG - 1 lines first,
139 stopping if the top or bottom edge of the image is reached."
140 (interactive "p")
141 (and arg
142 (/= (setq arg (prefix-numeric-value arg)) 1)
143 (image-next-line (- arg 1)))
144 (set-window-hscroll (selected-window) 0))
145
146 (defun image-eol (arg)
147 "Scroll horizontally to the right edge of the image in the current window.
148 With argument ARG not nil or 1, move forward ARG - 1 lines first,
149 stopping if the top or bottom edge of the image is reached."
150 (interactive "p")
151 (and arg
152 (/= (setq arg (prefix-numeric-value arg)) 1)
153 (image-next-line (- arg 1)))
154 (let* ((image (get-text-property 1 'display))
155 (edges (window-inside-edges))
156 (win-width (- (nth 2 edges) (nth 0 edges)))
157 (img-width (ceiling (car (image-size image)))))
158 (set-window-hscroll (selected-window)
159 (max 0 (- img-width win-width)))))
160
161 (defun image-bob ()
162 "Scroll to the top-left corner of the image in the current window."
163 (interactive)
164 (set-window-hscroll (selected-window) 0)
165 (set-window-vscroll (selected-window) 0))
166
167 (defun image-eob ()
168 "Scroll to the bottom-right corner of the image in the current window."
169 (interactive)
170 (let* ((image (get-text-property 1 'display))
171 (edges (window-inside-edges))
172 (win-width (- (nth 2 edges) (nth 0 edges)))
173 (img-width (ceiling (car (image-size image))))
174 (win-height (- (nth 3 edges) (nth 1 edges)))
175 (img-height (ceiling (cdr (image-size image)))))
176 (set-window-hscroll (selected-window) (max 0 (- img-width win-width)))
177 (set-window-vscroll (selected-window) (max 0 (- img-height win-height)))))
178
179 ;;; Image Mode setup
180
181 (defvar image-mode-map
182 (let ((map (make-sparse-keymap)))
183 (define-key map "\C-c\C-c" 'image-toggle-display)
184 (define-key map [remap forward-char] 'image-forward-hscroll)
185 (define-key map [remap backward-char] 'image-backward-hscroll)
186 (define-key map [remap previous-line] 'image-previous-line)
187 (define-key map [remap next-line] 'image-next-line)
188 (define-key map [remap scroll-up] 'image-scroll-up)
189 (define-key map [remap scroll-down] 'image-scroll-down)
190 (define-key map [remap move-beginning-of-line] 'image-bol)
191 (define-key map [remap move-end-of-line] 'image-eol)
192 (define-key map [remap beginning-of-buffer] 'image-bob)
193 (define-key map [remap end-of-buffer] 'image-eob)
194 map)
195 "Major mode keymap for viewing images in Image mode.")
196
197 (defvar image-mode-text-map
198 (let ((map (make-sparse-keymap)))
199 (define-key map "\C-c\C-c" 'image-toggle-display)
200 map)
201 "Major mode keymap for viewing images as text in Image mode.")
202
203 ;;;###autoload
204 (defun image-mode ()
205 "Major mode for image files.
206 You can use \\<image-mode-map>\\[image-toggle-display]
207 to toggle between display as an image and display as text."
208 (interactive)
209 (kill-all-local-variables)
210 (setq mode-name "Image")
211 (setq major-mode 'image-mode)
212 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
213 (if (and (display-images-p)
214 (not (get-text-property (point-min) 'display)))
215 (image-toggle-display)
216 ;; Set next vars when image is already displayed but local
217 ;; variables were cleared by kill-all-local-variables
218 (use-local-map image-mode-map)
219 (setq cursor-type nil truncate-lines t))
220 (run-mode-hooks 'image-mode-hook)
221 (if (display-images-p)
222 (message "%s" (concat
223 (substitute-command-keys
224 "Type \\[image-toggle-display] to view as ")
225 (if (get-text-property (point-min) 'display)
226 "text" "an image") "."))))
227
228 ;;;###autoload
229 (define-minor-mode image-minor-mode
230 "Toggle Image minor mode.
231 With arg, turn Image minor mode on if arg is positive, off otherwise.
232 See the command `image-mode' for more information on this mode."
233 nil " Image" image-mode-map
234 :group 'image
235 :version "22.1"
236 (if (not image-minor-mode)
237 (image-toggle-display-text)
238 (if (get-text-property (point-min) 'display)
239 (setq cursor-type nil truncate-lines t))
240 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
241 (message "%s" (concat (substitute-command-keys
242 "Type \\[image-toggle-display] to view the image as ")
243 (if (get-text-property (point-min) 'display)
244 "text" "an image") "."))))
245
246 ;;;###autoload
247 (defun image-mode-maybe ()
248 "Set major or minor mode for image files.
249 Set Image major mode only when there are no other major modes
250 associated with a filename in `auto-mode-alist'. When an image
251 filename matches another major mode in `auto-mode-alist' then
252 set that major mode and Image minor mode.
253
254 See commands `image-mode' and `image-minor-mode' for more
255 information on these modes."
256 (interactive)
257 (let* ((mode-alist
258 (delq nil (mapcar
259 (lambda (elt)
260 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
261 '(image-mode image-mode-maybe))
262 elt))
263 auto-mode-alist))))
264 (if (assoc-default buffer-file-name mode-alist 'string-match)
265 (let ((auto-mode-alist mode-alist)
266 (magic-mode-alist nil))
267 (set-auto-mode)
268 (image-minor-mode t))
269 (image-mode))))
270
271 (defun image-toggle-display-text ()
272 "Showing the text of the image file."
273 (if (get-text-property (point-min) 'display)
274 (image-toggle-display)))
275
276 (defvar archive-superior-buffer)
277 (defvar tar-superior-buffer)
278
279 (defun image-toggle-display ()
280 "Start or stop displaying an image file as the actual image.
281 This command toggles between showing the text of the image file
282 and showing the image as an image."
283 (interactive)
284 (if (get-text-property (point-min) 'display)
285 (let ((inhibit-read-only t)
286 (buffer-undo-list t)
287 (modified (buffer-modified-p)))
288 (remove-list-of-text-properties (point-min) (point-max)
289 '(display intangible read-nonsticky
290 read-only front-sticky))
291 (set-buffer-modified-p modified)
292 (kill-local-variable 'cursor-type)
293 (kill-local-variable 'truncate-lines)
294 (kill-local-variable 'auto-hscroll-mode)
295 (use-local-map image-mode-text-map)
296 (if (called-interactively-p)
297 (message "Repeat this command to go back to displaying the image")))
298 ;; Turn the image data into a real image, but only if the whole file
299 ;; was inserted
300 (let* ((filename (buffer-file-name))
301 (image
302 (if (and filename
303 (file-readable-p filename)
304 (not (file-remote-p filename))
305 (not (buffer-modified-p))
306 (not (and (boundp 'archive-superior-buffer)
307 archive-superior-buffer))
308 (not (and (boundp 'tar-superior-buffer)
309 tar-superior-buffer)))
310 (create-image filename)
311 (create-image
312 (string-make-unibyte
313 (buffer-substring-no-properties (point-min) (point-max)))
314 nil t)))
315 (props
316 `(display ,image
317 intangible ,image
318 rear-nonsticky (display intangible)
319 read-only t front-sticky (read-only)))
320 (inhibit-read-only t)
321 (buffer-undo-list t)
322 (modified (buffer-modified-p)))
323 (image-refresh image)
324 (add-text-properties (point-min) (point-max) props)
325 (set-buffer-modified-p modified)
326 ;; Inhibit the cursor when the buffer contains only an image,
327 ;; because cursors look very strange on top of images.
328 (setq cursor-type nil)
329 ;; This just makes the arrow displayed in the right fringe
330 ;; area look correct when the image is wider than the window.
331 (setq truncate-lines t)
332 ;; Allow navigation of large images
333 (set (make-local-variable 'auto-hscroll-mode) nil)
334 (use-local-map image-mode-map)
335 (if (called-interactively-p)
336 (message "Repeat this command to go back to displaying the file as text")))))
337
338 (provide 'image-mode)
339
340 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
341 ;;; image-mode.el ends here