ERC: Sync version 5.3, release candidate 1.
[bpt/emacs.git] / lisp / image-mode.el
CommitLineData
c7bd5d57
RS
1;;; image-mode.el --- support for visiting image files
2;;
a47d49c0 3;; Copyright (C) 2005, 2006, 2007, 2008 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)
4a2f0e99
JL
44
45;;;###autoload (push '("\\.x[bp]m\\'" . c-mode) auto-mode-alist)
d90e651e 46;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
c7bd5d57 47
4a2f0e99
JL
48;;;###autoload (push '("\\.svgz?\\'" . xml-mode) auto-mode-alist)
49;;;###autoload (push '("\\.svgz?\\'" . image-mode-maybe) auto-mode-alist)
50
bb0cb417
CY
51;;; Image scrolling functions
52
53(defun image-forward-hscroll (&optional n)
54 "Scroll image in current window to the left by N character widths.
55Stop if the right edge of the image is reached."
56 (interactive "p")
57 (cond ((= n 0) nil)
58 ((< n 0)
59 (set-window-hscroll (selected-window)
60 (max 0 (+ (window-hscroll) n))))
61 (t
1a59edfc 62 (let* ((image (get-char-property (point-min) 'display))
bb0cb417
CY
63 (edges (window-inside-edges))
64 (win-width (- (nth 2 edges) (nth 0 edges)))
65 (img-width (ceiling (car (image-size image)))))
66 (set-window-hscroll (selected-window)
67 (min (max 0 (- img-width win-width))
68 (+ n (window-hscroll))))))))
69
70(defun image-backward-hscroll (&optional n)
71 "Scroll image in current window to the right by N character widths.
72Stop if the left edge of the image is reached."
73 (interactive "p")
74 (image-forward-hscroll (- n)))
75
76(defun image-next-line (&optional n)
77 "Scroll image in current window upward by N lines.
78Stop if the bottom edge of the image is reached."
79 (interactive "p")
80 (cond ((= n 0) nil)
81 ((< n 0)
82 (set-window-vscroll (selected-window)
83 (max 0 (+ (window-vscroll) n))))
84 (t
1a59edfc 85 (let* ((image (get-char-property (point-min) 'display))
bb0cb417
CY
86 (edges (window-inside-edges))
87 (win-height (- (nth 3 edges) (nth 1 edges)))
88 (img-height (ceiling (cdr (image-size image)))))
89 (set-window-vscroll (selected-window)
90 (min (max 0 (- img-height win-height))
91 (+ n (window-vscroll))))))))
92
93(defun image-previous-line (&optional n)
94 "Scroll image in current window downward by N lines.
95Stop if the top edge of the image is reached."
96 (interactive "p")
97 (image-next-line (- n)))
98
99(defun image-scroll-up (&optional n)
100 "Scroll image in current window upward by N lines.
101Stop if the bottom edge of the image is reached.
102If ARG is omitted or nil, scroll upward by a near full screen.
103A near full screen is `next-screen-context-lines' less than a full screen.
104Negative ARG means scroll downward.
105If ARG is the atom `-', scroll downward by nearly full screen.
106When calling from a program, supply as argument a number, nil, or `-'."
107 (interactive "P")
108 (cond ((null n)
109 (let* ((edges (window-inside-edges))
110 (win-height (- (nth 3 edges) (nth 1 edges))))
111 (image-next-line
112 (max 0 (- win-height next-screen-context-lines)))))
113 ((eq n '-)
114 (let* ((edges (window-inside-edges))
115 (win-height (- (nth 3 edges) (nth 1 edges))))
116 (image-next-line
117 (min 0 (- next-screen-context-lines win-height)))))
118 (t (image-next-line (prefix-numeric-value n)))))
119
120(defun image-scroll-down (&optional n)
121 "Scroll image in current window downward by N lines
122Stop if the top edge of the image is reached.
123If ARG is omitted or nil, scroll downward by a near full screen.
124A near full screen is `next-screen-context-lines' less than a full screen.
125Negative ARG means scroll upward.
126If ARG is the atom `-', scroll upward by nearly full screen.
127When calling from a program, supply as argument a number, nil, or `-'."
128 (interactive "P")
129 (cond ((null n)
130 (let* ((edges (window-inside-edges))
131 (win-height (- (nth 3 edges) (nth 1 edges))))
132 (image-next-line
133 (min 0 (- next-screen-context-lines win-height)))))
134 ((eq n '-)
135 (let* ((edges (window-inside-edges))
136 (win-height (- (nth 3 edges) (nth 1 edges))))
137 (image-next-line
138 (max 0 (- win-height next-screen-context-lines)))))
139 (t (image-next-line (- (prefix-numeric-value n))))))
140
141(defun image-bol (arg)
142 "Scroll horizontally to the left edge of the image in the current window.
143With argument ARG not nil or 1, move forward ARG - 1 lines first,
144stopping if the top or bottom edge of the image is reached."
145 (interactive "p")
146 (and arg
147 (/= (setq arg (prefix-numeric-value arg)) 1)
148 (image-next-line (- arg 1)))
149 (set-window-hscroll (selected-window) 0))
150
151(defun image-eol (arg)
152 "Scroll horizontally to the right edge of the image in the current window.
153With argument ARG not nil or 1, move forward ARG - 1 lines first,
154stopping if the top or bottom edge of the image is reached."
155 (interactive "p")
156 (and arg
157 (/= (setq arg (prefix-numeric-value arg)) 1)
158 (image-next-line (- arg 1)))
1a59edfc 159 (let* ((image (get-char-property (point-min) 'display))
bb0cb417
CY
160 (edges (window-inside-edges))
161 (win-width (- (nth 2 edges) (nth 0 edges)))
162 (img-width (ceiling (car (image-size image)))))
163 (set-window-hscroll (selected-window)
164 (max 0 (- img-width win-width)))))
165
166(defun image-bob ()
167 "Scroll to the top-left corner of the image in the current window."
168 (interactive)
169 (set-window-hscroll (selected-window) 0)
170 (set-window-vscroll (selected-window) 0))
171
172(defun image-eob ()
173 "Scroll to the bottom-right corner of the image in the current window."
174 (interactive)
1a59edfc 175 (let* ((image (get-char-property (point-min) 'display))
bb0cb417
CY
176 (edges (window-inside-edges))
177 (win-width (- (nth 2 edges) (nth 0 edges)))
178 (img-width (ceiling (car (image-size image))))
179 (win-height (- (nth 3 edges) (nth 1 edges)))
180 (img-height (ceiling (cdr (image-size image)))))
181 (set-window-hscroll (selected-window) (max 0 (- img-width win-width)))
182 (set-window-vscroll (selected-window) (max 0 (- img-height win-height)))))
183
184;;; Image Mode setup
185
d487ca7d
JL
186(defvar image-type nil
187 "Current image type.
188This variable is used to display the current image type in the mode line.")
189(make-variable-buffer-local 'image-type)
190
c7bd5d57 191(defvar image-mode-map
bb0cb417
CY
192 (let ((map (make-sparse-keymap)))
193 (define-key map "\C-c\C-c" 'image-toggle-display)
194 (define-key map [remap forward-char] 'image-forward-hscroll)
195 (define-key map [remap backward-char] 'image-backward-hscroll)
196 (define-key map [remap previous-line] 'image-previous-line)
197 (define-key map [remap next-line] 'image-next-line)
198 (define-key map [remap scroll-up] 'image-scroll-up)
199 (define-key map [remap scroll-down] 'image-scroll-down)
200 (define-key map [remap move-beginning-of-line] 'image-bol)
201 (define-key map [remap move-end-of-line] 'image-eol)
202 (define-key map [remap beginning-of-buffer] 'image-bob)
203 (define-key map [remap end-of-buffer] 'image-eob)
204 map)
205 "Major mode keymap for viewing images in Image mode.")
206
207(defvar image-mode-text-map
c7bd5d57
RS
208 (let ((map (make-sparse-keymap)))
209 (define-key map "\C-c\C-c" 'image-toggle-display)
210 map)
bb0cb417 211 "Major mode keymap for viewing images as text in Image mode.")
c7bd5d57 212
a47d49c0
GM
213(defvar bookmark-make-cell-function)
214
c7bd5d57
RS
215;;;###autoload
216(defun image-mode ()
217 "Major mode for image files.
218You can use \\<image-mode-map>\\[image-toggle-display]
219to toggle between display as an image and display as text."
220 (interactive)
221 (kill-all-local-variables)
d487ca7d 222 (setq mode-name "Image[text]")
c7bd5d57 223 (setq major-mode 'image-mode)
137187c8
TH
224 ;; Use our own bookmarking function for images.
225 (set (make-local-variable 'bookmark-make-cell-function)
226 'image-bookmark-make-cell)
d90e651e 227 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
f992a935 228 (if (and (display-images-p)
1a59edfc 229 (not (get-char-property (point-min) 'display)))
f992a935
CY
230 (image-toggle-display)
231 ;; Set next vars when image is already displayed but local
232 ;; variables were cleared by kill-all-local-variables
bb0cb417 233 (use-local-map image-mode-map)
f992a935 234 (setq cursor-type nil truncate-lines t))
ab145daf
CY
235 (run-mode-hooks 'image-mode-hook)
236 (if (display-images-p)
237 (message "%s" (concat
238 (substitute-command-keys
9f446ee9 239 "Type \\[image-toggle-display] to view as ")
1a59edfc 240 (if (get-char-property (point-min) 'display)
ab145daf 241 "text" "an image") "."))))
d90e651e
JL
242
243;;;###autoload
244(define-minor-mode image-minor-mode
245 "Toggle Image minor mode.
246With arg, turn Image minor mode on if arg is positive, off otherwise.
247See the command `image-mode' for more information on this mode."
d487ca7d 248 nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
d90e651e
JL
249 :group 'image
250 :version "22.1"
90d0be7d
JL
251 (if (not image-minor-mode)
252 (image-toggle-display-text)
1a59edfc 253 (if (get-char-property (point-min) 'display)
d487ca7d
JL
254 (setq cursor-type nil truncate-lines t)
255 (setq image-type "text"))
90d0be7d 256 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
8a26c165 257 (message "%s" (concat (substitute-command-keys
90d0be7d 258 "Type \\[image-toggle-display] to view the image as ")
1a59edfc 259 (if (get-char-property (point-min) 'display)
90d0be7d 260 "text" "an image") "."))))
d90e651e
JL
261
262;;;###autoload
263(defun image-mode-maybe ()
264 "Set major or minor mode for image files.
265Set Image major mode only when there are no other major modes
266associated with a filename in `auto-mode-alist'. When an image
267filename matches another major mode in `auto-mode-alist' then
268set that major mode and Image minor mode.
269
270See commands `image-mode' and `image-minor-mode' for more
271information on these modes."
272 (interactive)
273 (let* ((mode-alist
274 (delq nil (mapcar
275 (lambda (elt)
276 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
277 '(image-mode image-mode-maybe))
278 elt))
279 auto-mode-alist))))
280 (if (assoc-default buffer-file-name mode-alist 'string-match)
9fce2d71
JR
281 (let ((auto-mode-alist mode-alist)
282 (magic-mode-alist nil))
d90e651e
JL
283 (set-auto-mode)
284 (image-minor-mode t))
285 (image-mode))))
286
287(defun image-toggle-display-text ()
288 "Showing the text of the image file."
1a59edfc 289 (if (get-char-property (point-min) 'display)
d90e651e 290 (image-toggle-display)))
c7bd5d57 291
f2920ffe
RS
292(defvar archive-superior-buffer)
293(defvar tar-superior-buffer)
294
c7bd5d57
RS
295(defun image-toggle-display ()
296 "Start or stop displaying an image file as the actual image.
297This command toggles between showing the text of the image file
298and showing the image as an image."
299 (interactive)
1a59edfc 300 (if (get-char-property (point-min) 'display)
c7bd5d57 301 (let ((inhibit-read-only t)
837daa0d
RS
302 (buffer-undo-list t)
303 (modified (buffer-modified-p)))
c7bd5d57
RS
304 (remove-list-of-text-properties (point-min) (point-max)
305 '(display intangible read-nonsticky
306 read-only front-sticky))
837daa0d 307 (set-buffer-modified-p modified)
c7bd5d57
RS
308 (kill-local-variable 'cursor-type)
309 (kill-local-variable 'truncate-lines)
bb0cb417
CY
310 (kill-local-variable 'auto-hscroll-mode)
311 (use-local-map image-mode-text-map)
d487ca7d
JL
312 (setq image-type "text")
313 (if (eq major-mode 'image-mode)
314 (setq mode-name "Image[text]"))
d90e651e
JL
315 (if (called-interactively-p)
316 (message "Repeat this command to go back to displaying the image")))
c7bd5d57
RS
317 ;; Turn the image data into a real image, but only if the whole file
318 ;; was inserted
dc255b24 319 (let* ((filename (buffer-file-name))
d487ca7d
JL
320 (data-p (not (and filename
321 (file-readable-p filename)
322 (not (file-remote-p filename))
323 (not (buffer-modified-p))
324 (not (and (boundp 'archive-superior-buffer)
325 archive-superior-buffer))
326 (not (and (boundp 'tar-superior-buffer)
327 tar-superior-buffer)))))
328 (file-or-data (if data-p
329 (string-make-unibyte
330 (buffer-substring-no-properties (point-min) (point-max)))
331 filename))
332 (type (image-type file-or-data nil data-p))
333 (image (create-image file-or-data type data-p))
c7bd5d57
RS
334 (props
335 `(display ,image
bb0cb417
CY
336 intangible ,image
337 rear-nonsticky (display intangible)
338 read-only t front-sticky (read-only)))
d90e651e 339 (inhibit-read-only t)
837daa0d
RS
340 (buffer-undo-list t)
341 (modified (buffer-modified-p)))
dc255b24 342 (image-refresh image)
c7bd5d57 343 (add-text-properties (point-min) (point-max) props)
837daa0d 344 (set-buffer-modified-p modified)
c7bd5d57
RS
345 ;; Inhibit the cursor when the buffer contains only an image,
346 ;; because cursors look very strange on top of images.
347 (setq cursor-type nil)
348 ;; This just makes the arrow displayed in the right fringe
349 ;; area look correct when the image is wider than the window.
350 (setq truncate-lines t)
bb0cb417
CY
351 ;; Allow navigation of large images
352 (set (make-local-variable 'auto-hscroll-mode) nil)
353 (use-local-map image-mode-map)
d487ca7d
JL
354 (setq image-type type)
355 (if (eq major-mode 'image-mode)
356 (setq mode-name (format "Image[%s]" type)))
d90e651e
JL
357 (if (called-interactively-p)
358 (message "Repeat this command to go back to displaying the file as text")))))
c7bd5d57 359
137187c8
TH
360;;; Support for bookmark.el
361
362(defun image-bookmark-make-cell (annotation &rest args)
363 (let ((the-record
364 `((filename . ,(buffer-file-name))
365 (image-type . ,image-type)
366 (position . ,(point))
367 (handler . image-bookmark-jump))))
368
369 ;; Take no chances with text properties
370 (set-text-properties 0 (length annotation) nil annotation)
371
372 (when annotation
373 (nconc the-record (list (cons 'annotation annotation))))
374
375 ;; Finally, return the completed record.
376 the-record))
377
a47d49c0
GM
378(declare-function bookmark-get-filename "bookmark" (bookmark))
379(declare-function bookmark-get-bookmark-record "bookmark" (bookmark))
380(declare-function bookmark-get-position "bookmark" (bookmark))
381
137187c8
TH
382;;;###autoload
383(defun image-bookmark-jump (bmk)
03e26a79
KF
384 ;; This implements the `handler' function interface for record type
385 ;; returned by `bookmark-make-cell-function', which see.
137187c8
TH
386 (save-window-excursion
387 (let ((filename (bookmark-get-filename bmk))
388 (type (cdr (assq 'image-type (bookmark-get-bookmark-record bmk))))
389 (pos (bookmark-get-position bmk)))
390 (find-file filename)
391 (when (not (string= image-type type))
392 (image-toggle-display))
393 (when (string= image-type "text")
394 (goto-char pos))
03e26a79 395 `((buffer ,(current-buffer)) (position ,(point))))))
137187c8 396
c7bd5d57
RS
397(provide 'image-mode)
398
8f7ee639 399;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
c7bd5d57 400;;; image-mode.el ends here