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