Regenerate.
[emacs.git] / lisp / image-mode.el
blobb903d497ec02cd1d9543667ee3078d6f00ed9c18
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)
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)
46 ;;; Image scrolling functions
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))))
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))))))))
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)))
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))))
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))))))))
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)))
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)))))
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))))))
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))
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)))))
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))
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)))))
177 ;;; Image Mode setup
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.")
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.")
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 (display-images-p)
212 (if (not (image-get-display-property))
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 (use-local-map image-mode-text-map))
219 (run-mode-hooks 'image-mode-hook)
220 (if (display-images-p)
221 (message "%s" (concat
222 (substitute-command-keys
223 "Type \\[image-toggle-display] to view as ")
224 (if (get-text-property (point-min) 'display)
225 "text" "an image") "."))))
227 ;;;###autoload
228 (define-minor-mode image-minor-mode
229 "Toggle Image minor mode.
230 With arg, turn Image minor mode on if arg is positive, off otherwise.
231 See the command `image-mode' for more information on this mode."
232 nil " Image" image-mode-text-map
233 :group 'image
234 :version "22.1"
235 (if (not image-minor-mode)
236 (image-toggle-display-text)
237 (if (get-text-property (point-min) 'display)
238 (setq cursor-type nil truncate-lines t))
239 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
240 (message "%s" (concat (substitute-command-keys
241 "Type \\[image-toggle-display] to view the image as ")
242 (if (get-text-property (point-min) 'display)
243 "text" "an image") "."))))
245 ;;;###autoload
246 (defun image-mode-maybe ()
247 "Set major or minor mode for image files.
248 Set Image major mode only when there are no other major modes
249 associated with a filename in `auto-mode-alist'. When an image
250 filename matches another major mode in `auto-mode-alist' then
251 set that major mode and Image minor mode.
253 See commands `image-mode' and `image-minor-mode' for more
254 information on these modes."
255 (interactive)
256 (let* ((mode-alist
257 (delq nil (mapcar
258 (lambda (elt)
259 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
260 '(image-mode image-mode-maybe))
261 elt))
262 auto-mode-alist))))
263 (if (assoc-default buffer-file-name mode-alist 'string-match)
264 (let ((auto-mode-alist mode-alist)
265 (magic-mode-alist nil))
266 (set-auto-mode)
267 (image-minor-mode t))
268 (image-mode))))
270 (defun image-toggle-display-text ()
271 "Showing the text of the image file."
272 (if (get-text-property (point-min) 'display)
273 (image-toggle-display)))
275 (defvar archive-superior-buffer)
276 (defvar tar-superior-buffer)
278 (defun image-toggle-display ()
279 "Start or stop displaying an image file as the actual image.
280 This command toggles between showing the text of the image file
281 and showing the image as an image."
282 (interactive)
283 (if (get-text-property (point-min) 'display)
284 (let ((inhibit-read-only t)
285 (buffer-undo-list t)
286 (modified (buffer-modified-p)))
287 (remove-list-of-text-properties (point-min) (point-max)
288 '(display intangible read-nonsticky
289 read-only front-sticky))
290 (set-buffer-modified-p modified)
291 (kill-local-variable 'cursor-type)
292 (kill-local-variable 'truncate-lines)
293 (kill-local-variable 'auto-hscroll-mode)
294 (use-local-map image-mode-text-map)
295 (if (called-interactively-p)
296 (message "Repeat this command to go back to displaying the image")))
297 ;; Turn the image data into a real image, but only if the whole file
298 ;; was inserted
299 (let* ((filename (buffer-file-name))
300 (image
301 (if (and filename
302 (file-readable-p filename)
303 (not (file-remote-p filename))
304 (not (buffer-modified-p))
305 (not (and (boundp 'archive-superior-buffer)
306 archive-superior-buffer))
307 (not (and (boundp 'tar-superior-buffer)
308 tar-superior-buffer)))
309 (create-image filename)
310 (create-image
311 (string-make-unibyte
312 (buffer-substring-no-properties (point-min) (point-max)))
313 nil t)))
314 (props
315 `(display ,image
316 intangible ,image
317 rear-nonsticky (display intangible)
318 read-only t front-sticky (read-only)))
319 (inhibit-read-only t)
320 (buffer-undo-list t)
321 (modified (buffer-modified-p)))
322 (image-refresh image)
323 (add-text-properties (point-min) (point-max) props)
324 (set-buffer-modified-p modified)
325 ;; Inhibit the cursor when the buffer contains only an image,
326 ;; because cursors look very strange on top of images.
327 (setq cursor-type nil)
328 ;; This just makes the arrow displayed in the right fringe
329 ;; area look correct when the image is wider than the window.
330 (setq truncate-lines t)
331 ;; Allow navigation of large images
332 (set (make-local-variable 'auto-hscroll-mode) nil)
333 (use-local-map image-mode-map)
334 (if (called-interactively-p)
335 (message "Repeat this command to go back to displaying the file as text")))))
337 (provide 'image-mode)
339 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
340 ;;; image-mode.el ends here