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