(image-type-auto-detectable): Don't autodetect x[pb]m.
[bpt/emacs.git] / lisp / image-mode.el
CommitLineData
c7bd5d57
RS
1;;; image-mode.el --- support for visiting image files
2;;
d7a0267c 3;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
c7bd5d57
RS
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 2, 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
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
c7bd5d57
RS
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
d90e651e
JL
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;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
c7bd5d57
RS
45
46(defvar image-mode-map
47 (let ((map (make-sparse-keymap)))
48 (define-key map "\C-c\C-c" 'image-toggle-display)
49 map)
50 "Major mode keymap for Image mode.")
51
52;;;###autoload
53(defun image-mode ()
54 "Major mode for image files.
55You can use \\<image-mode-map>\\[image-toggle-display]
56to toggle between display as an image and display as text."
57 (interactive)
58 (kill-all-local-variables)
59 (setq mode-name "Image")
60 (setq major-mode 'image-mode)
61 (use-local-map image-mode-map)
d90e651e 62 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
ab145daf
CY
63 (run-mode-hooks 'image-mode-hook)
64 (if (display-images-p)
65 (message "%s" (concat
66 (substitute-command-keys
9f446ee9 67 "Type \\[image-toggle-display] to view as ")
ab145daf
CY
68 (if (get-text-property (point-min) 'display)
69 "text" "an image") "."))))
d90e651e
JL
70
71;;;###autoload
72(define-minor-mode image-minor-mode
73 "Toggle Image minor mode.
74With arg, turn Image minor mode on if arg is positive, off otherwise.
75See the command `image-mode' for more information on this mode."
76 nil " Image" image-mode-map
77 :group 'image
78 :version "22.1"
90d0be7d
JL
79 (if (not image-minor-mode)
80 (image-toggle-display-text)
81 (if (get-text-property (point-min) 'display)
82 (setq cursor-type nil truncate-lines t))
83 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
8a26c165 84 (message "%s" (concat (substitute-command-keys
90d0be7d
JL
85 "Type \\[image-toggle-display] to view the image as ")
86 (if (get-text-property (point-min) 'display)
87 "text" "an image") "."))))
d90e651e
JL
88
89;;;###autoload
90(defun image-mode-maybe ()
91 "Set major or minor mode for image files.
92Set Image major mode only when there are no other major modes
93associated with a filename in `auto-mode-alist'. When an image
94filename matches another major mode in `auto-mode-alist' then
95set that major mode and Image minor mode.
96
97See commands `image-mode' and `image-minor-mode' for more
98information on these modes."
99 (interactive)
100 (let* ((mode-alist
101 (delq nil (mapcar
102 (lambda (elt)
103 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
104 '(image-mode image-mode-maybe))
105 elt))
106 auto-mode-alist))))
107 (if (assoc-default buffer-file-name mode-alist 'string-match)
9fce2d71
JR
108 (let ((auto-mode-alist mode-alist)
109 (magic-mode-alist nil))
d90e651e
JL
110 (set-auto-mode)
111 (image-minor-mode t))
112 (image-mode))))
113
114(defun image-toggle-display-text ()
115 "Showing the text of the image file."
116 (if (get-text-property (point-min) 'display)
117 (image-toggle-display)))
c7bd5d57 118
f2920ffe
RS
119(defvar archive-superior-buffer)
120(defvar tar-superior-buffer)
121
c7bd5d57
RS
122(defun image-toggle-display ()
123 "Start or stop displaying an image file as the actual image.
124This command toggles between showing the text of the image file
125and showing the image as an image."
126 (interactive)
127 (if (get-text-property (point-min) 'display)
128 (let ((inhibit-read-only t)
837daa0d
RS
129 (buffer-undo-list t)
130 (modified (buffer-modified-p)))
c7bd5d57
RS
131 (remove-list-of-text-properties (point-min) (point-max)
132 '(display intangible read-nonsticky
133 read-only front-sticky))
837daa0d 134 (set-buffer-modified-p modified)
c7bd5d57
RS
135 (kill-local-variable 'cursor-type)
136 (kill-local-variable 'truncate-lines)
d90e651e
JL
137 (if (called-interactively-p)
138 (message "Repeat this command to go back to displaying the image")))
c7bd5d57
RS
139 ;; Turn the image data into a real image, but only if the whole file
140 ;; was inserted
2bd53dc0
CY
141 (let* ((image
142 (if (and (buffer-file-name)
4d687160 143 (not (file-remote-p (buffer-file-name)))
df5b8a53
RS
144 (not (buffer-modified-p))
145 (not (and (boundp 'archive-superior-buffer)
146 archive-superior-buffer))
147 (not (and (boundp 'tar-superior-buffer)
148 tar-superior-buffer)))
fcfc4732
CY
149 (progn (clear-image-cache)
150 (create-image (buffer-file-name)))
2bd53dc0
CY
151 (create-image
152 (string-make-unibyte
153 (buffer-substring-no-properties (point-min) (point-max)))
154 nil t)))
c7bd5d57
RS
155 (props
156 `(display ,image
157 intangible ,image
158 rear-nonsticky (display intangible)
159 ;; This a cheap attempt to make the whole buffer
160 ;; read-only when we're visiting the file (as
161 ;; opposed to just inserting it).
162 read-only t front-sticky (read-only)))
d90e651e 163 (inhibit-read-only t)
837daa0d
RS
164 (buffer-undo-list t)
165 (modified (buffer-modified-p)))
c7bd5d57 166 (add-text-properties (point-min) (point-max) props)
837daa0d 167 (set-buffer-modified-p modified)
c7bd5d57
RS
168 ;; Inhibit the cursor when the buffer contains only an image,
169 ;; because cursors look very strange on top of images.
170 (setq cursor-type nil)
171 ;; This just makes the arrow displayed in the right fringe
172 ;; area look correct when the image is wider than the window.
173 (setq truncate-lines t)
d90e651e
JL
174 (if (called-interactively-p)
175 (message "Repeat this command to go back to displaying the file as text")))))
c7bd5d57 176
49f1a2f4
CY
177;; Don't override the setting from .emacs.
178;;;###autoload (put 'image-toggle-display 'disabled t)
179
180(if (get 'image-toggle-display 'disabled)
181 (put 'image-toggle-display 'disabled "\
182
183Warning: Displaying images in Emacs could be a security risk.
184Please ensure that you are using up-to-date image libraries
185and that the images being displayed come from a trusted source."))
fd014ebf 186
c7bd5d57
RS
187(provide 'image-mode)
188
8f7ee639 189;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
c7bd5d57 190;;; image-mode.el ends here