Merge from emacs--rel--22
[bpt/emacs.git] / lisp / image-mode.el
CommitLineData
c7bd5d57
RS
1;;; image-mode.el --- support for visiting image files
2;;
d7a0267c 3;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
c7bd5d57
RS
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
b4aa6026 12;; the Free Software Foundation; either version 3, or (at your option)
c7bd5d57
RS
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
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
c7bd5d57
RS
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
d90e651e
JL
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)
c7bd5d57 45
bb0cb417
CY
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.
50Stop 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.
67Stop 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.
73Stop 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.
90Stop 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.
96Stop if the bottom edge of the image is reached.
97If ARG is omitted or nil, scroll upward by a near full screen.
98A near full screen is `next-screen-context-lines' less than a full screen.
99Negative ARG means scroll downward.
100If ARG is the atom `-', scroll downward by nearly full screen.
101When 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
117Stop if the top edge of the image is reached.
118If ARG is omitted or nil, scroll downward by a near full screen.
119A near full screen is `next-screen-context-lines' less than a full screen.
120Negative ARG means scroll upward.
121If ARG is the atom `-', scroll upward by nearly full screen.
122When 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.
138With argument ARG not nil or 1, move forward ARG - 1 lines first,
139stopping 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.
148With argument ARG not nil or 1, move forward ARG - 1 lines first,
149stopping 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
c7bd5d57 181(defvar image-mode-map
bb0cb417
CY
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
c7bd5d57
RS
198 (let ((map (make-sparse-keymap)))
199 (define-key map "\C-c\C-c" 'image-toggle-display)
200 map)
bb0cb417 201 "Major mode keymap for viewing images as text in Image mode.")
c7bd5d57
RS
202
203;;;###autoload
204(defun image-mode ()
205 "Major mode for image files.
206You can use \\<image-mode-map>\\[image-toggle-display]
207to 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)
d90e651e 212 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
f992a935
CY
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
bb0cb417 218 (use-local-map image-mode-map)
f992a935 219 (setq cursor-type nil truncate-lines t))
ab145daf
CY
220 (run-mode-hooks 'image-mode-hook)
221 (if (display-images-p)
222 (message "%s" (concat
223 (substitute-command-keys
9f446ee9 224 "Type \\[image-toggle-display] to view as ")
ab145daf
CY
225 (if (get-text-property (point-min) 'display)
226 "text" "an image") "."))))
d90e651e
JL
227
228;;;###autoload
229(define-minor-mode image-minor-mode
230 "Toggle Image minor mode.
231With arg, turn Image minor mode on if arg is positive, off otherwise.
232See the command `image-mode' for more information on this mode."
233 nil " Image" image-mode-map
234 :group 'image
235 :version "22.1"
90d0be7d
JL
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)
8a26c165 241 (message "%s" (concat (substitute-command-keys
90d0be7d
JL
242 "Type \\[image-toggle-display] to view the image as ")
243 (if (get-text-property (point-min) 'display)
244 "text" "an image") "."))))
d90e651e
JL
245
246;;;###autoload
247(defun image-mode-maybe ()
248 "Set major or minor mode for image files.
249Set Image major mode only when there are no other major modes
250associated with a filename in `auto-mode-alist'. When an image
251filename matches another major mode in `auto-mode-alist' then
252set that major mode and Image minor mode.
253
254See commands `image-mode' and `image-minor-mode' for more
255information 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)
9fce2d71
JR
265 (let ((auto-mode-alist mode-alist)
266 (magic-mode-alist nil))
d90e651e
JL
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)))
c7bd5d57 275
f2920ffe
RS
276(defvar archive-superior-buffer)
277(defvar tar-superior-buffer)
278
c7bd5d57
RS
279(defun image-toggle-display ()
280 "Start or stop displaying an image file as the actual image.
281This command toggles between showing the text of the image file
282and showing the image as an image."
283 (interactive)
284 (if (get-text-property (point-min) 'display)
285 (let ((inhibit-read-only t)
837daa0d
RS
286 (buffer-undo-list t)
287 (modified (buffer-modified-p)))
c7bd5d57
RS
288 (remove-list-of-text-properties (point-min) (point-max)
289 '(display intangible read-nonsticky
290 read-only front-sticky))
837daa0d 291 (set-buffer-modified-p modified)
c7bd5d57
RS
292 (kill-local-variable 'cursor-type)
293 (kill-local-variable 'truncate-lines)
bb0cb417
CY
294 (kill-local-variable 'auto-hscroll-mode)
295 (use-local-map image-mode-text-map)
d90e651e
JL
296 (if (called-interactively-p)
297 (message "Repeat this command to go back to displaying the image")))
c7bd5d57
RS
298 ;; Turn the image data into a real image, but only if the whole file
299 ;; was inserted
dc255b24
CY
300 (let* ((filename (buffer-file-name))
301 (image
302 (if (and filename
303 (file-readable-p filename)
304 (not (file-remote-p filename))
df5b8a53
RS
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)))
dc255b24 310 (create-image filename)
2bd53dc0
CY
311 (create-image
312 (string-make-unibyte
313 (buffer-substring-no-properties (point-min) (point-max)))
314 nil t)))
c7bd5d57
RS
315 (props
316 `(display ,image
bb0cb417
CY
317 intangible ,image
318 rear-nonsticky (display intangible)
319 read-only t front-sticky (read-only)))
d90e651e 320 (inhibit-read-only t)
837daa0d
RS
321 (buffer-undo-list t)
322 (modified (buffer-modified-p)))
dc255b24 323 (image-refresh image)
c7bd5d57 324 (add-text-properties (point-min) (point-max) props)
837daa0d 325 (set-buffer-modified-p modified)
c7bd5d57
RS
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)
bb0cb417
CY
332 ;; Allow navigation of large images
333 (set (make-local-variable 'auto-hscroll-mode) nil)
334 (use-local-map image-mode-map)
d90e651e
JL
335 (if (called-interactively-p)
336 (message "Repeat this command to go back to displaying the file as text")))))
c7bd5d57
RS
337
338(provide 'image-mode)
339
8f7ee639 340;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
c7bd5d57 341;;; image-mode.el ends here