Require generic again.
[emacs.git] / lisp / image-mode.el
blob0d2b221ee8b45718c12d4f3b4d519caa44dc6fe3
1 ;;; image-mode.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2005 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 2, 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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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)
39 ;;;###autoload (push '("\\.jpg\\'" . image-mode) auto-mode-alist)
40 ;;;###autoload (push '("\\.jpeg\\'" . image-mode) auto-mode-alist)
41 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
42 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
43 ;;;###autoload (push '("\\.tiff\\'" . image-mode) auto-mode-alist)
44 ;;;###autoload (push '("\\.tif\\'" . image-mode) auto-mode-alist)
45 ;;;###autoload (push '("\\.xbm\\'" . image-mode) auto-mode-alist)
46 ;;;###autoload (push '("\\.xpm\\'" . image-mode) auto-mode-alist)
47 ;;;###autoload (push '("\\.pbm\\'" . image-mode) auto-mode-alist)
48 ;;;###autoload (push '("\\.pgm\\'" . image-mode) auto-mode-alist)
49 ;;;###autoload (push '("\\.ppm\\'" . image-mode) auto-mode-alist)
50 ;;;###autoload (push '("\\.pnm\\'" . image-mode) auto-mode-alist)
52 (defvar image-mode-map
53 (let ((map (make-sparse-keymap)))
54 (define-key map "\C-c\C-c" 'image-toggle-display)
55 map)
56 "Major mode keymap for Image mode.")
58 ;;;###autoload
59 (defun image-mode ()
60 "Major mode for image files.
61 You can use \\<image-mode-map>\\[image-toggle-display]
62 to toggle between display as an image and display as text."
63 (interactive)
64 (kill-all-local-variables)
65 (setq mode-name "Image")
66 (setq major-mode 'image-mode)
67 (use-local-map image-mode-map)
68 (run-mode-hooks 'image-mode-hook)
69 (message (substitute-command-keys
70 "Type \\[image-toggle-display] to view the image as an image.")))
72 (defun image-toggle-display ()
73 "Start or stop displaying an image file as the actual image.
74 This command toggles between showing the text of the image file
75 and showing the image as an image."
76 (interactive)
77 (if (get-text-property (point-min) 'display)
78 (let ((inhibit-read-only t)
79 (buffer-undo-list t)
80 (modified (buffer-modified-p)))
81 (remove-list-of-text-properties (point-min) (point-max)
82 '(display intangible read-nonsticky
83 read-only front-sticky))
84 (set-buffer-modified-p modified)
85 (kill-local-variable 'cursor-type)
86 (kill-local-variable 'truncate-lines)
87 (message "Repeat this command to go back to displaying the image"))
88 ;; Turn the image data into a real image, but only if the whole file
89 ;; was inserted
90 (let* ((data
91 (string-make-unibyte
92 (buffer-substring-no-properties (point-min) (point-max))))
93 (image
94 (create-image data nil t))
95 (props
96 `(display ,image
97 intangible ,image
98 rear-nonsticky (display intangible)
99 ;; This a cheap attempt to make the whole buffer
100 ;; read-only when we're visiting the file (as
101 ;; opposed to just inserting it).
102 read-only t front-sticky (read-only)))
103 (buffer-undo-list t)
104 (modified (buffer-modified-p)))
105 (add-text-properties (point-min) (point-max) props)
106 (set-buffer-modified-p modified)
107 ;; Inhibit the cursor when the buffer contains only an image,
108 ;; because cursors look very strange on top of images.
109 (setq cursor-type nil)
110 ;; This just makes the arrow displayed in the right fringe
111 ;; area look correct when the image is wider than the window.
112 (setq truncate-lines t)
113 (message "Repeat this command to go back to displaying the file as text"))))
115 (provide 'image-mode)
117 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
118 ;;; image-mode.el ends here