* lispref/modes.texi (Region to Refontify): Rename from "Region to Fontify".
[emacs.git] / lisp / image-mode.el
blob9ef4344298063b5d2c4d4ec96b3e638f3b1b6d12
1 ;;; image-mode.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Defines a major mode for visiting image files
26 ;; that allows conversion between viewing the text of the file
27 ;; and viewing the file as an image. Viewing the image
28 ;; works by putting a `display' text-property on the
29 ;; image data, with the image-data still present underneath; if the
30 ;; resulting buffer file is saved to another name it will correctly save
31 ;; the image data to the new file.
33 ;;; Code:
35 (require 'image)
36 (eval-when-compile (require 'cl))
38 ;;;###autoload (push (cons (purecopy "\\.jpe?g\\'") 'image-mode) auto-mode-alist)
39 ;;;###autoload (push (cons (purecopy "\\.png\\'") 'image-mode) auto-mode-alist)
40 ;;;###autoload (push (cons (purecopy "\\.gif\\'") 'image-mode) auto-mode-alist)
41 ;;;###autoload (push (cons (purecopy "\\.tiff?\\'") 'image-mode) auto-mode-alist)
42 ;;;###autoload (push (cons (purecopy "\\.p[bpgn]m\\'") 'image-mode) auto-mode-alist)
44 ;;;###autoload (push (cons (purecopy "\\.x[bp]m\\'") 'c-mode) auto-mode-alist)
45 ;;;###autoload (push (cons (purecopy "\\.x[bp]m\\'") 'image-mode) auto-mode-alist)
47 ;;;###autoload (push (cons (purecopy "\\.svgz?\\'") 'xml-mode) auto-mode-alist)
48 ;;;###autoload (push (cons (purecopy "\\.svgz?\\'") 'image-mode) auto-mode-alist)
50 ;;; Image mode window-info management.
52 (defvar image-mode-winprops-alist t)
53 (make-variable-buffer-local 'image-mode-winprops-alist)
55 (defvar image-mode-new-window-functions nil
56 "Special hook run when image data is requested in a new window.
57 It is called with one argument, the initial WINPROPS.")
59 (defun image-mode-winprops (&optional window cleanup)
60 "Return winprops of WINDOW.
61 A winprops object has the shape (WINDOW . ALIST)."
62 (cond ((null window)
63 (setq window (selected-window)))
64 ((not (windowp window))
65 (error "Not a window: %s" window)))
66 (when cleanup
67 (setq image-mode-winprops-alist
68 (delq nil (mapcar (lambda (winprop)
69 (if (window-live-p (car-safe winprop))
70 winprop))
71 image-mode-winprops-alist))))
72 (let ((winprops (assq window image-mode-winprops-alist)))
73 ;; For new windows, set defaults from the latest.
74 (unless winprops
75 (setq winprops (cons window
76 (copy-alist (cdar image-mode-winprops-alist))))
77 (run-hook-with-args 'image-mode-new-window-functions winprops))
78 ;; Move window to front.
79 (setq image-mode-winprops-alist
80 (cons winprops (delq winprops image-mode-winprops-alist)))
81 winprops))
83 (defun image-mode-window-get (prop &optional winprops)
84 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
85 (cdr (assq prop (cdr winprops))))
87 (defsetf image-mode-window-get (prop &optional winprops) (val)
88 `(image-mode-window-put ,prop ,val ,winprops))
90 (defun image-mode-window-put (prop val &optional winprops)
91 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
92 (setcdr winprops (cons (cons prop val)
93 (delq (assq prop (cdr winprops)) (cdr winprops)))))
95 (defun image-set-window-vscroll (vscroll)
96 (setf (image-mode-window-get 'vscroll) vscroll)
97 (set-window-vscroll (selected-window) vscroll))
99 (defun image-set-window-hscroll (ncol)
100 (setf (image-mode-window-get 'hscroll) ncol)
101 (set-window-hscroll (selected-window) ncol))
103 (defun image-mode-reapply-winprops ()
104 ;; When set-window-buffer, set hscroll and vscroll to what they were
105 ;; last time the image was displayed in this window.
106 (when (and (image-get-display-property)
107 (listp image-mode-winprops-alist))
108 (let* ((winprops (image-mode-winprops nil t))
109 (hscroll (image-mode-window-get 'hscroll winprops))
110 (vscroll (image-mode-window-get 'vscroll winprops)))
111 (if hscroll (set-window-hscroll (selected-window) hscroll))
112 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
114 (defun image-mode-setup-winprops ()
115 ;; Record current scroll settings.
116 (unless (listp image-mode-winprops-alist)
117 (setq image-mode-winprops-alist nil))
118 (add-hook 'window-configuration-change-hook
119 'image-mode-reapply-winprops nil t))
121 ;;; Image scrolling functions
123 (defun image-get-display-property ()
124 (get-char-property (point-min) 'display
125 ;; There might be different images for different displays.
126 (if (eq (window-buffer) (current-buffer))
127 (selected-window))))
129 (declare-function image-size "image.c" (spec &optional pixels frame))
131 (defun image-display-size (spec &optional pixels frame)
132 "Wrapper around `image-size', to handle slice display properties.
133 If SPEC is an image display property, call `image-size' with the
134 given arguments.
135 If SPEC is a list of properties containing `image' and `slice'
136 properties, calculate the display size from the slice property.
137 If SPEC contains `image' but not `slice', call `image-size' with
138 the specified image."
139 (if (eq (car spec) 'image)
140 (image-size spec pixels frame)
141 (let ((image (assoc 'image spec))
142 (slice (assoc 'slice spec)))
143 (cond ((and image slice)
144 (if pixels
145 (cons (nth 3 slice) (nth 4 slice))
146 (cons (/ (float (nth 3 slice)) (frame-char-width frame))
147 (/ (float (nth 4 slice)) (frame-char-height frame)))))
148 (image
149 (image-size image pixels frame))
151 (error "Invalid image specification: %s" spec))))))
153 (defun image-forward-hscroll (&optional n)
154 "Scroll image in current window to the left by N character widths.
155 Stop if the right edge of the image is reached."
156 (interactive "p")
157 (cond ((= n 0) nil)
158 ((< n 0)
159 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
161 (let* ((image (image-get-display-property))
162 (edges (window-inside-edges))
163 (win-width (- (nth 2 edges) (nth 0 edges)))
164 (img-width (ceiling (car (image-display-size image)))))
165 (image-set-window-hscroll (min (max 0 (- img-width win-width))
166 (+ n (window-hscroll))))))))
168 (defun image-backward-hscroll (&optional n)
169 "Scroll image in current window to the right by N character widths.
170 Stop if the left edge of the image is reached."
171 (interactive "p")
172 (image-forward-hscroll (- n)))
174 (defun image-next-line (&optional n)
175 "Scroll image in current window upward by N lines.
176 Stop if the bottom edge of the image is reached."
177 (interactive "p")
178 (cond ((= n 0) nil)
179 ((< n 0)
180 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
182 (let* ((image (image-get-display-property))
183 (edges (window-inside-edges))
184 (win-height (- (nth 3 edges) (nth 1 edges)))
185 (img-height (ceiling (cdr (image-display-size image)))))
186 (image-set-window-vscroll (min (max 0 (- img-height win-height))
187 (+ n (window-vscroll))))))))
189 (defun image-previous-line (&optional n)
190 "Scroll image in current window downward by N lines.
191 Stop if the top edge of the image is reached."
192 (interactive "p")
193 (image-next-line (- n)))
195 (defun image-scroll-up (&optional n)
196 "Scroll image in current window upward by N lines.
197 Stop if the bottom edge of the image is reached.
198 If ARG is omitted or nil, scroll upward by a near full screen.
199 A near full screen is `next-screen-context-lines' less than a full screen.
200 Negative ARG means scroll downward.
201 If ARG is the atom `-', scroll downward by nearly full screen.
202 When calling from a program, supply as argument a number, nil, or `-'."
203 (interactive "P")
204 (cond ((null n)
205 (let* ((edges (window-inside-edges))
206 (win-height (- (nth 3 edges) (nth 1 edges))))
207 (image-next-line
208 (max 0 (- win-height next-screen-context-lines)))))
209 ((eq n '-)
210 (let* ((edges (window-inside-edges))
211 (win-height (- (nth 3 edges) (nth 1 edges))))
212 (image-next-line
213 (min 0 (- next-screen-context-lines win-height)))))
214 (t (image-next-line (prefix-numeric-value n)))))
216 (defun image-scroll-down (&optional n)
217 "Scroll image in current window downward by N lines.
218 Stop if the top edge of the image is reached.
219 If ARG is omitted or nil, scroll downward by a near full screen.
220 A near full screen is `next-screen-context-lines' less than a full screen.
221 Negative ARG means scroll upward.
222 If ARG is the atom `-', scroll upward by nearly full screen.
223 When calling from a program, supply as argument a number, nil, or `-'."
224 (interactive "P")
225 (cond ((null n)
226 (let* ((edges (window-inside-edges))
227 (win-height (- (nth 3 edges) (nth 1 edges))))
228 (image-next-line
229 (min 0 (- next-screen-context-lines win-height)))))
230 ((eq n '-)
231 (let* ((edges (window-inside-edges))
232 (win-height (- (nth 3 edges) (nth 1 edges))))
233 (image-next-line
234 (max 0 (- win-height next-screen-context-lines)))))
235 (t (image-next-line (- (prefix-numeric-value n))))))
237 (defun image-bol (arg)
238 "Scroll horizontally to the left edge of the image in the current window.
239 With argument ARG not nil or 1, move forward ARG - 1 lines first,
240 stopping if the top or bottom edge of the image is reached."
241 (interactive "p")
242 (and arg
243 (/= (setq arg (prefix-numeric-value arg)) 1)
244 (image-next-line (- arg 1)))
245 (image-set-window-hscroll 0))
247 (defun image-eol (arg)
248 "Scroll horizontally to the right edge of the image in the current window.
249 With argument ARG not nil or 1, move forward ARG - 1 lines first,
250 stopping if the top or bottom edge of the image is reached."
251 (interactive "p")
252 (and arg
253 (/= (setq arg (prefix-numeric-value arg)) 1)
254 (image-next-line (- arg 1)))
255 (let* ((image (image-get-display-property))
256 (edges (window-inside-edges))
257 (win-width (- (nth 2 edges) (nth 0 edges)))
258 (img-width (ceiling (car (image-display-size image)))))
259 (image-set-window-hscroll (max 0 (- img-width win-width)))))
261 (defun image-bob ()
262 "Scroll to the top-left corner of the image in the current window."
263 (interactive)
264 (image-set-window-hscroll 0)
265 (image-set-window-vscroll 0))
267 (defun image-eob ()
268 "Scroll to the bottom-right corner of the image in the current window."
269 (interactive)
270 (let* ((image (image-get-display-property))
271 (edges (window-inside-edges))
272 (win-width (- (nth 2 edges) (nth 0 edges)))
273 (img-width (ceiling (car (image-display-size image))))
274 (win-height (- (nth 3 edges) (nth 1 edges)))
275 (img-height (ceiling (cdr (image-display-size image)))))
276 (image-set-window-hscroll (max 0 (- img-width win-width)))
277 (image-set-window-vscroll (max 0 (- img-height win-height)))))
279 ;; Adjust frame and image size.
281 (defun image-mode-fit-frame ()
282 "Fit the frame to the current image.
283 This function assumes the current frame has only one window."
284 ;; FIXME: This does not take into account decorations like mode-line,
285 ;; minibuffer, header-line, ...
286 (interactive)
287 (let* ((saved (frame-parameter nil 'image-mode-saved-size))
288 (display (image-get-display-property))
289 (size (image-display-size display)))
290 (if (and saved
291 (eq (caar saved) (frame-width))
292 (eq (cdar saved) (frame-height)))
293 (progn ;; Toggle back to previous non-fitted size.
294 (set-frame-parameter nil 'image-mode-saved-size nil)
295 (setq size (cdr saved)))
296 ;; Round up size, and save current size so we can toggle back to it.
297 (setcar size (ceiling (car size)))
298 (setcdr size (ceiling (cdr size)))
299 (set-frame-parameter nil 'image-mode-saved-size
300 (cons size (cons (frame-width) (frame-height)))))
301 (set-frame-width (selected-frame) (car size))
302 (set-frame-height (selected-frame) (cdr size))))
304 ;;; Image Mode setup
306 (defvar image-type nil
307 "The image type for the current Image mode buffer.")
308 (make-variable-buffer-local 'image-type)
310 (defvar image-mode-previous-major-mode nil
311 "Internal variable to keep the previous non-image major mode.")
313 (defvar image-mode-map
314 (let ((map (make-sparse-keymap)))
315 (suppress-keymap map)
316 (define-key map "q" 'quit-window)
317 (define-key map "\C-c\C-c" 'image-toggle-display)
318 (define-key map (kbd "SPC") 'image-scroll-up)
319 (define-key map (kbd "DEL") 'image-scroll-down)
320 (define-key map [remap forward-char] 'image-forward-hscroll)
321 (define-key map [remap backward-char] 'image-backward-hscroll)
322 (define-key map [remap previous-line] 'image-previous-line)
323 (define-key map [remap next-line] 'image-next-line)
324 (define-key map [remap scroll-up] 'image-scroll-up)
325 (define-key map [remap scroll-down] 'image-scroll-down)
326 (define-key map [remap move-beginning-of-line] 'image-bol)
327 (define-key map [remap move-end-of-line] 'image-eol)
328 (define-key map [remap beginning-of-buffer] 'image-bob)
329 (define-key map [remap end-of-buffer] 'image-eob)
330 map)
331 "Mode keymap for `image-mode'.")
333 (defvar image-minor-mode-map
334 (let ((map (make-sparse-keymap)))
335 (define-key map "\C-c\C-c" 'image-toggle-display)
336 map)
337 "Mode keymap for `image-minor-mode'.")
339 (defvar bookmark-make-record-function)
341 (put 'image-mode 'mode-class 'special)
343 ;;;###autoload
344 (defun image-mode ()
345 "Major mode for image files.
346 You can use \\<image-mode-map>\\[image-toggle-display]
347 to toggle between display as an image and display as text."
348 (interactive)
349 (condition-case err
350 (progn
351 (unless (display-images-p)
352 (error "Display does not support images"))
354 (kill-all-local-variables)
355 (setq major-mode 'image-mode)
357 (if (not (image-get-display-property))
358 (progn
359 (image-toggle-display-image)
360 ;; If attempt to display the image fails.
361 (if (not (image-get-display-property))
362 (error "Invalid image")))
363 ;; Set next vars when image is already displayed but local
364 ;; variables were cleared by kill-all-local-variables
365 (setq cursor-type nil truncate-lines t
366 image-type (plist-get (cdr (image-get-display-property)) :type)))
368 (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
369 (use-local-map image-mode-map)
371 ;; Use our own bookmarking function for images.
372 (set (make-local-variable 'bookmark-make-record-function)
373 'image-bookmark-make-record)
375 ;; Keep track of [vh]scroll when switching buffers
376 (image-mode-setup-winprops)
378 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
379 (run-mode-hooks 'image-mode-hook)
380 (message "%s" (concat
381 (substitute-command-keys
382 "Type \\[image-toggle-display] to view the image as ")
383 (if (image-get-display-property)
384 "text" "an image") ".")))
385 (error
386 (image-mode-as-text)
387 (funcall
388 (if (called-interactively-p 'any) 'error 'message)
389 "Cannot display image: %s" (cdr err)))))
391 ;;;###autoload
392 (define-minor-mode image-minor-mode
393 "Toggle Image minor mode.
394 With arg, turn Image minor mode on if arg is positive, off otherwise.
395 It provides the key \\<image-mode-map>\\[image-toggle-display] \
396 to switch back to `image-mode'
397 to display an image file as the actual image."
398 nil (:eval (if image-type (format " Image[%s]" image-type) " Image"))
399 image-minor-mode-map
400 :group 'image
401 :version "22.1"
402 (if image-minor-mode
403 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
405 ;;;###autoload
406 (defun image-mode-as-text ()
407 "Set a non-image mode as major mode in combination with image minor mode.
408 A non-image major mode found from `auto-mode-alist' or Fundamental mode
409 displays an image file as text. `image-minor-mode' provides the key
410 \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
411 to display an image file as the actual image.
413 You can use `image-mode-as-text' in `auto-mode-alist' when you want
414 to display an image file as text initially.
416 See commands `image-mode' and `image-minor-mode' for more information
417 on these modes."
418 (interactive)
419 ;; image-mode-as-text = normal-mode + image-minor-mode
420 (let ((previous-image-type image-type)) ; preserve `image-type'
421 (if image-mode-previous-major-mode
422 ;; Restore previous major mode that was already found by this
423 ;; function and cached in `image-mode-previous-major-mode'
424 (funcall image-mode-previous-major-mode)
425 (let ((auto-mode-alist
426 (delq nil (mapcar
427 (lambda (elt)
428 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
429 '(image-mode image-mode-maybe image-mode-as-text))
430 elt))
431 auto-mode-alist)))
432 (magic-fallback-mode-alist
433 (delq nil (mapcar
434 (lambda (elt)
435 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
436 '(image-mode image-mode-maybe image-mode-as-text))
437 elt))
438 magic-fallback-mode-alist))))
439 (normal-mode)
440 (set (make-local-variable 'image-mode-previous-major-mode) major-mode)))
441 ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
442 (setq image-type previous-image-type)
443 ;; Enable image minor mode with `C-c C-c'.
444 (image-minor-mode 1)
445 ;; Show the image file as text.
446 (image-toggle-display-text)
447 (message "%s" (concat
448 (substitute-command-keys
449 "Type \\[image-toggle-display] to view the image as ")
450 (if (image-get-display-property)
451 "text" "an image") "."))))
453 (define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
455 (defun image-toggle-display-text ()
456 "Show the image file as text.
457 Remove text properties that display the image."
458 (let ((inhibit-read-only t)
459 (buffer-undo-list t)
460 (modified (buffer-modified-p)))
461 (remove-list-of-text-properties (point-min) (point-max)
462 '(display intangible read-nonsticky
463 read-only front-sticky))
464 (set-buffer-modified-p modified)
465 (if (called-interactively-p 'any)
466 (message "Repeat this command to go back to displaying the image"))))
468 (defvar archive-superior-buffer)
469 (defvar tar-superior-buffer)
470 (declare-function image-refresh "image.c" (spec &optional frame))
472 (defun image-toggle-display-image ()
473 "Show the image of the image file.
474 Turn the image data into a real image, but only if the whole file
475 was inserted."
476 (let* ((filename (buffer-file-name))
477 (data-p (not (and filename
478 (file-readable-p filename)
479 (not (file-remote-p filename))
480 (not (buffer-modified-p))
481 (not (and (boundp 'archive-superior-buffer)
482 archive-superior-buffer))
483 (not (and (boundp 'tar-superior-buffer)
484 tar-superior-buffer)))))
485 (file-or-data (if data-p
486 (string-make-unibyte
487 (buffer-substring-no-properties (point-min) (point-max)))
488 filename))
489 (type (image-type file-or-data nil data-p))
490 (image (create-image file-or-data type data-p))
491 (props
492 `(display ,image
493 intangible ,image
494 rear-nonsticky (display intangible)
495 read-only t front-sticky (read-only)))
496 (inhibit-read-only t)
497 (buffer-undo-list t)
498 (modified (buffer-modified-p)))
499 (image-refresh image)
500 (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file
501 (add-text-properties (point-min) (point-max) props)
502 (restore-buffer-modified-p modified))
503 ;; Inhibit the cursor when the buffer contains only an image,
504 ;; because cursors look very strange on top of images.
505 (setq cursor-type nil)
506 ;; This just makes the arrow displayed in the right fringe
507 ;; area look correct when the image is wider than the window.
508 (setq truncate-lines t)
509 ;; Disable adding a newline at the end of the image file when it
510 ;; is written with, e.g., C-x C-w.
511 (if (coding-system-equal (coding-system-base buffer-file-coding-system)
512 'no-conversion)
513 (set (make-local-variable 'require-final-newline) nil))
514 ;; Allow navigation of large images
515 (set (make-local-variable 'auto-hscroll-mode) nil)
516 (setq image-type type)
517 (if (eq major-mode 'image-mode)
518 (setq mode-name (format "Image[%s]" type)))
519 (if (called-interactively-p 'any)
520 (message "Repeat this command to go back to displaying the file as text"))))
522 (defun image-toggle-display ()
523 "Toggle between image and text display.
524 If the current buffer is displaying an image file as an image,
525 call `image-mode-as-text' to switch to text. Otherwise, display
526 the image by calling `image-mode'."
527 (interactive)
528 (if (image-get-display-property)
529 (image-mode-as-text)
530 (image-mode)))
532 ;;; Support for bookmark.el
533 (declare-function bookmark-make-record-default "bookmark"
534 (&optional point-only))
535 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
536 (declare-function bookmark-default-handler "bookmark" (bmk))
538 (defun image-bookmark-make-record ()
539 (nconc (bookmark-make-record-default)
540 `((image-type . ,image-type)
541 (handler . image-bookmark-jump))))
543 ;;;###autoload
544 (defun image-bookmark-jump (bmk)
545 ;; This implements the `handler' function interface for record type
546 ;; returned by `bookmark-make-record-function', which see.
547 (prog1 (bookmark-default-handler bmk)
548 (when (not (string= image-type (bookmark-prop-get bmk 'image-type)))
549 (image-toggle-display))))
551 (provide 'image-mode)
553 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
554 ;;; image-mode.el ends here