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