Fix merge mistake.
[emacs.git] / lisp / image-mode.el
blobfc38f045ff9e42675a0079df1299125934ce1a02
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)
38 (eval-when-compile (require 'cl))
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)
46 ;;;###autoload (push '("\\.x[bp]m\\'" . c-mode) auto-mode-alist)
47 ;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
49 ;;;###autoload (push '("\\.svgz?\\'" . xml-mode) auto-mode-alist)
50 ;;;###autoload (push '("\\.svgz?\\'" . image-mode-maybe) auto-mode-alist)
52 ;;; Image mode window-info management.
54 (defvar image-mode-winprops-alist t)
55 (make-variable-buffer-local 'image-mode-winprops-alist)
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.")
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))
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))))
80 (defsetf image-mode-window-get (prop &optional winprops) (val)
81 `(image-mode-window-put ,prop ,val ,winprops))
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)))))
88 (defun image-set-window-vscroll (vscroll)
89 (setf (image-mode-window-get 'vscroll) vscroll)
90 (set-window-vscroll (selected-window) vscroll))
92 (defun image-set-window-hscroll (ncol)
93 (setf (image-mode-window-get 'hscroll) ncol)
94 (set-window-hscroll (selected-window) ncol))
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)))))
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))
113 ;;; Image scrolling functions
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))))
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))))
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))))))))
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)))
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))))
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))))))))
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)))
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)))))
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))))))
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))
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)))))
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))
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)))))
247 ;;; Image Mode setup
249 (defvar image-type nil
250 "Current image type.
251 This variable is used to display the current image type in the mode line.")
252 (make-variable-buffer-local 'image-type)
254 (defvar image-mode-map
255 (let ((map (make-sparse-keymap)))
256 (define-key map "\C-c\C-c" 'image-toggle-display)
257 (define-key map [remap forward-char] 'image-forward-hscroll)
258 (define-key map [remap backward-char] 'image-backward-hscroll)
259 (define-key map [remap previous-line] 'image-previous-line)
260 (define-key map [remap next-line] 'image-next-line)
261 (define-key map [remap scroll-up] 'image-scroll-up)
262 (define-key map [remap scroll-down] 'image-scroll-down)
263 (define-key map [remap move-beginning-of-line] 'image-bol)
264 (define-key map [remap move-end-of-line] 'image-eol)
265 (define-key map [remap beginning-of-buffer] 'image-bob)
266 (define-key map [remap end-of-buffer] 'image-eob)
267 map)
268 "Major mode keymap for viewing images in Image mode.")
270 (defvar image-mode-text-map
271 (let ((map (make-sparse-keymap)))
272 (define-key map "\C-c\C-c" 'image-toggle-display)
273 map)
274 "Major mode keymap for viewing images as text in Image mode.")
276 (defvar bookmark-make-record-function)
278 ;;;###autoload
279 (defun image-mode ()
280 "Major mode for image files.
281 You can use \\<image-mode-map>\\[image-toggle-display]
282 to toggle between display as an image and display as text."
283 (interactive)
284 (kill-all-local-variables)
285 (setq mode-name "Image[text]")
286 (setq major-mode 'image-mode)
287 ;; Use our own bookmarking function for images.
288 (set (make-local-variable 'bookmark-make-record-function)
289 'image-bookmark-make-record)
291 ;; Keep track of [vh]scroll when switching buffers
292 (image-mode-setup-winprops)
294 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
295 (if (and (display-images-p)
296 (not (image-get-display-property)))
297 (image-toggle-display)
298 ;; Set next vars when image is already displayed but local
299 ;; variables were cleared by kill-all-local-variables
300 (use-local-map image-mode-map)
301 (setq cursor-type nil truncate-lines t))
302 (run-mode-hooks 'image-mode-hook)
303 (if (display-images-p)
304 (message "%s" (concat
305 (substitute-command-keys
306 "Type \\[image-toggle-display] to view as ")
307 (if (image-get-display-property)
308 "text" "an image") "."))))
310 ;;;###autoload
311 (define-minor-mode image-minor-mode
312 "Toggle Image minor mode.
313 With arg, turn Image minor mode on if arg is positive, off otherwise.
314 See the command `image-mode' for more information on this mode."
315 nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
316 :group 'image
317 :version "22.1"
318 (if (not image-minor-mode)
319 (image-toggle-display-text)
320 (if (image-get-display-property)
321 (setq cursor-type nil truncate-lines t)
322 (setq image-type "text"))
323 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
324 (message "%s" (concat (substitute-command-keys
325 "Type \\[image-toggle-display] to view the image as ")
326 (if (image-get-display-property)
327 "text" "an image") "."))))
329 ;;;###autoload
330 (defun image-mode-maybe ()
331 "Set major or minor mode for image files.
332 Set Image major mode only when there are no other major modes
333 associated with a filename in `auto-mode-alist'. When an image
334 filename matches another major mode in `auto-mode-alist' then
335 set that major mode and Image minor mode.
337 See commands `image-mode' and `image-minor-mode' for more
338 information on these modes."
339 (interactive)
340 (let* ((mode-alist
341 (delq nil (mapcar
342 (lambda (elt)
343 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
344 '(image-mode image-mode-maybe))
345 elt))
346 auto-mode-alist))))
347 (if (assoc-default buffer-file-name mode-alist 'string-match)
348 (let ((auto-mode-alist mode-alist)
349 (magic-mode-alist nil))
350 (set-auto-mode)
351 (image-minor-mode t))
352 (image-mode))))
354 (defun image-toggle-display-text ()
355 "Showing the text of the image file."
356 (if (image-get-display-property)
357 (image-toggle-display)))
359 (defvar archive-superior-buffer)
360 (defvar tar-superior-buffer)
362 (defun image-toggle-display ()
363 "Start or stop displaying an image file as the actual image.
364 This command toggles between showing the text of the image file
365 and showing the image as an image."
366 (interactive)
367 (if (image-get-display-property)
368 (let ((inhibit-read-only t)
369 (buffer-undo-list t)
370 (modified (buffer-modified-p)))
371 (remove-list-of-text-properties (point-min) (point-max)
372 '(display intangible read-nonsticky
373 read-only front-sticky))
374 (set-buffer-modified-p modified)
375 (kill-local-variable 'cursor-type)
376 (kill-local-variable 'truncate-lines)
377 (kill-local-variable 'auto-hscroll-mode)
378 (use-local-map image-mode-text-map)
379 (setq image-type "text")
380 (if (eq major-mode 'image-mode)
381 (setq mode-name "Image[text]"))
382 (if (called-interactively-p)
383 (message "Repeat this command to go back to displaying the image")))
384 ;; Turn the image data into a real image, but only if the whole file
385 ;; was inserted
386 (let* ((filename (buffer-file-name))
387 (data-p (not (and filename
388 (file-readable-p filename)
389 (not (file-remote-p filename))
390 (not (buffer-modified-p))
391 (not (and (boundp 'archive-superior-buffer)
392 archive-superior-buffer))
393 (not (and (boundp 'tar-superior-buffer)
394 tar-superior-buffer)))))
395 (file-or-data (if data-p
396 (string-make-unibyte
397 (buffer-substring-no-properties (point-min) (point-max)))
398 filename))
399 (type (image-type file-or-data nil data-p))
400 (image (create-image file-or-data type data-p))
401 (props
402 `(display ,image
403 intangible ,image
404 rear-nonsticky (display intangible)
405 read-only t front-sticky (read-only)))
406 (inhibit-read-only t)
407 (buffer-undo-list t)
408 (modified (buffer-modified-p)))
409 (image-refresh image)
410 (add-text-properties (point-min) (point-max) props)
411 (set-buffer-modified-p modified)
412 ;; Inhibit the cursor when the buffer contains only an image,
413 ;; because cursors look very strange on top of images.
414 (setq cursor-type nil)
415 ;; This just makes the arrow displayed in the right fringe
416 ;; area look correct when the image is wider than the window.
417 (setq truncate-lines t)
418 ;; Allow navigation of large images
419 (set (make-local-variable 'auto-hscroll-mode) nil)
420 (use-local-map image-mode-map)
421 (setq image-type type)
422 (if (eq major-mode 'image-mode)
423 (setq mode-name (format "Image[%s]" type)))
424 (if (called-interactively-p)
425 (message "Repeat this command to go back to displaying the file as text")))))
427 ;;; Support for bookmark.el
429 (defun image-bookmark-make-record (annotation)
430 (let ((the-record
431 `((filename . ,(buffer-file-name))
432 (image-type . ,image-type)
433 (position . ,(point))
434 (handler . image-bookmark-jump))))
436 ;; Take no chances with text properties
437 (set-text-properties 0 (length annotation) nil annotation)
439 (when annotation
440 (nconc the-record (list (cons 'annotation annotation))))
442 ;; Finally, return the completed record.
443 the-record))
445 (declare-function bookmark-get-filename "bookmark" (bookmark))
446 (declare-function bookmark-get-bookmark-record "bookmark" (bookmark))
447 (declare-function bookmark-get-position "bookmark" (bookmark))
449 ;;;###autoload
450 (defun image-bookmark-jump (bmk)
451 ;; This implements the `handler' function interface for record type
452 ;; returned by `bookmark-make-record-function', which see.
453 (save-window-excursion
454 (let ((filename (bookmark-get-filename bmk))
455 (type (cdr (assq 'image-type (bookmark-get-bookmark-record bmk))))
456 (pos (bookmark-get-position bmk)))
457 (find-file filename)
458 (when (not (string= image-type type))
459 (image-toggle-display))
460 (when (string= image-type "text")
461 (goto-char pos))
462 `((buffer ,(current-buffer)) (position ,(point))))))
464 (provide 'image-mode)
466 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
467 ;;; image-mode.el ends here