(match): Use yellow background on light-bg terminals.
[emacs.git] / lisp / image-mode.el
blobeb08de1d6bbb12c7d0ba64d7be96a3296cdb42bf
1 ;;; image-mode.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2005, 2006, 2007 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., 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)
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)
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.")
52 ;;;###autoload
53 (defun image-mode ()
54 "Major mode for image files.
55 You can use \\<image-mode-map>\\[image-toggle-display]
56 to 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)
62 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
63 (if (and (display-images-p)
64 (not (get-text-property (point-min) 'display)))
65 (image-toggle-display)
66 ;; Set next vars when image is already displayed but local
67 ;; variables were cleared by kill-all-local-variables
68 (setq cursor-type nil truncate-lines t))
69 (run-mode-hooks 'image-mode-hook)
70 (if (display-images-p)
71 (message "%s" (concat
72 (substitute-command-keys
73 "Type \\[image-toggle-display] to view as ")
74 (if (get-text-property (point-min) 'display)
75 "text" "an image") "."))))
77 ;;;###autoload
78 (define-minor-mode image-minor-mode
79 "Toggle Image minor mode.
80 With arg, turn Image minor mode on if arg is positive, off otherwise.
81 See the command `image-mode' for more information on this mode."
82 nil " Image" image-mode-map
83 :group 'image
84 :version "22.1"
85 (if (not image-minor-mode)
86 (image-toggle-display-text)
87 (if (get-text-property (point-min) 'display)
88 (setq cursor-type nil truncate-lines t))
89 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
90 (message "%s" (concat (substitute-command-keys
91 "Type \\[image-toggle-display] to view the image as ")
92 (if (get-text-property (point-min) 'display)
93 "text" "an image") "."))))
95 ;;;###autoload
96 (defun image-mode-maybe ()
97 "Set major or minor mode for image files.
98 Set Image major mode only when there are no other major modes
99 associated with a filename in `auto-mode-alist'. When an image
100 filename matches another major mode in `auto-mode-alist' then
101 set that major mode and Image minor mode.
103 See commands `image-mode' and `image-minor-mode' for more
104 information on these modes."
105 (interactive)
106 (let* ((mode-alist
107 (delq nil (mapcar
108 (lambda (elt)
109 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
110 '(image-mode image-mode-maybe))
111 elt))
112 auto-mode-alist))))
113 (if (assoc-default buffer-file-name mode-alist 'string-match)
114 (let ((auto-mode-alist mode-alist)
115 (magic-mode-alist nil))
116 (set-auto-mode)
117 (image-minor-mode t))
118 (image-mode))))
120 (defun image-toggle-display-text ()
121 "Showing the text of the image file."
122 (if (get-text-property (point-min) 'display)
123 (image-toggle-display)))
125 (defvar archive-superior-buffer)
126 (defvar tar-superior-buffer)
128 (defun image-toggle-display ()
129 "Start or stop displaying an image file as the actual image.
130 This command toggles between showing the text of the image file
131 and showing the image as an image."
132 (interactive)
133 (if (get-text-property (point-min) 'display)
134 (let ((inhibit-read-only t)
135 (buffer-undo-list t)
136 (modified (buffer-modified-p)))
137 (remove-list-of-text-properties (point-min) (point-max)
138 '(display intangible read-nonsticky
139 read-only front-sticky))
140 (set-buffer-modified-p modified)
141 (kill-local-variable 'cursor-type)
142 (kill-local-variable 'truncate-lines)
143 (if (called-interactively-p)
144 (message "Repeat this command to go back to displaying the image")))
145 ;; Turn the image data into a real image, but only if the whole file
146 ;; was inserted
147 (let* ((image
148 (if (and (buffer-file-name)
149 (not (file-remote-p (buffer-file-name)))
150 (not (buffer-modified-p))
151 (not (and (boundp 'archive-superior-buffer)
152 archive-superior-buffer))
153 (not (and (boundp 'tar-superior-buffer)
154 tar-superior-buffer)))
155 (progn (clear-image-cache)
156 (create-image (buffer-file-name)))
157 (create-image
158 (string-make-unibyte
159 (buffer-substring-no-properties (point-min) (point-max)))
160 nil t)))
161 (props
162 `(display ,image
163 intangible ,image
164 rear-nonsticky (display intangible)
165 ;; This a cheap attempt to make the whole buffer
166 ;; read-only when we're visiting the file (as
167 ;; opposed to just inserting it).
168 read-only t front-sticky (read-only)))
169 (inhibit-read-only t)
170 (buffer-undo-list t)
171 (modified (buffer-modified-p)))
172 (add-text-properties (point-min) (point-max) props)
173 (set-buffer-modified-p modified)
174 ;; Inhibit the cursor when the buffer contains only an image,
175 ;; because cursors look very strange on top of images.
176 (setq cursor-type nil)
177 ;; This just makes the arrow displayed in the right fringe
178 ;; area look correct when the image is wider than the window.
179 (setq truncate-lines t)
180 (if (called-interactively-p)
181 (message "Repeat this command to go back to displaying the file as text")))))
183 (provide 'image-mode)
185 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
186 ;;; image-mode.el ends here