(mac_cg_color_space_rgb) [USE_CG_DRAWING]:
[emacs.git] / lisp / image-file.el
blob6a29b4da0fef322fe5f77f62c8ff47ec35177348
1 ;;; image-file.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Miles Bader <miles@gnu.org>
7 ;; Keywords: multimedia
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Defines a file-name-handler hook that transforms visited (or
29 ;; inserted) image files so that they are displayed by Emacs as
30 ;; images. This is done 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)
40 ;;;###autoload
41 (defcustom image-file-name-extensions
42 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm" "xpm" "pbm" "pgm" "ppm" "pnm")
43 "*A list of image-file filename extensions.
44 Filenames having one of these extensions are considered image files,
45 in addition to those matching `image-file-name-regexps'.
47 See `auto-image-file-mode'; if `auto-image-file-mode' is enabled,
48 setting this variable directly does not take effect unless
49 `auto-image-file-mode' is re-enabled; this happens automatically when
50 the variable is set using \\[customize]."
51 :type '(repeat string)
52 :set (lambda (sym val)
53 (set-default sym val)
54 (when auto-image-file-mode
55 ;; Re-initialize the image-file handler
56 (auto-image-file-mode t)))
57 :initialize 'custom-initialize-default
58 :group 'image)
60 ;;;###autoload
61 (defcustom image-file-name-regexps nil
62 "*List of regexps matching image-file filenames.
63 Filenames matching one of these regexps are considered image files,
64 in addition to those with an extension in `image-file-name-extensions'.
66 See function `auto-image-file-mode'; if `auto-image-file-mode' is
67 enabled, setting this variable directly does not take effect unless
68 `auto-image-file-mode' is re-enabled; this happens automatically when
69 the variable is set using \\[customize]."
70 :type '(repeat regexp)
71 :set (lambda (sym val)
72 (set-default sym val)
73 (when auto-image-file-mode
74 ;; Re-initialize the image-file handler
75 (auto-image-file-mode t)))
76 :initialize 'custom-initialize-default
77 :group 'image)
80 ;;;###autoload
81 (defun image-file-name-regexp ()
82 "Return a regular expression matching image-file filenames."
83 (let ((exts-regexp
84 (and image-file-name-extensions
85 (concat "\\."
86 (regexp-opt (nconc (mapcar #'upcase
87 image-file-name-extensions)
88 image-file-name-extensions)
90 "\\'"))))
91 (if image-file-name-regexps
92 (mapconcat 'identity
93 (if exts-regexp
94 (cons exts-regexp image-file-name-regexps)
95 image-file-name-regexps)
96 "\\|")
97 exts-regexp)))
100 ;;;###autoload
101 (defun insert-image-file (file &optional visit beg end replace)
102 "Insert the image file FILE into the current buffer.
103 Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for
104 the command `insert-file-contents'."
105 (let ((rval
106 (image-file-call-underlying #'insert-file-contents-literally
107 'insert-file-contents
108 file visit beg end replace)))
109 ;; Turn the image data into a real image, but only if the whole file
110 ;; was inserted
111 (when (and (or (null beg) (zerop beg)) (null end))
112 (let* ((ibeg (point))
113 (iend (+ (point) (cadr rval)))
114 (visitingp (and visit (= ibeg (point-min)) (= iend (point-max))))
115 (data
116 (string-make-unibyte
117 (buffer-substring-no-properties ibeg iend)))
118 (image
119 (create-image data nil t))
120 (props
121 `(display ,image
122 yank-handler
123 (image-file-yank-handler nil t)
124 intangible ,image
125 rear-nonsticky (display intangible)
126 ;; This a cheap attempt to make the whole buffer
127 ;; read-only when we're visiting the file (as
128 ;; opposed to just inserting it).
129 ,@(and visitingp
130 '(read-only t front-sticky (read-only))))))
131 (add-text-properties ibeg iend props)
132 (when visitingp
133 ;; Inhibit the cursor when the buffer contains only an image,
134 ;; because cursors look very strange on top of images.
135 (setq cursor-type nil)
136 ;; This just makes the arrow displayed in the right fringe
137 ;; area look correct when the image is wider than the window.
138 (setq truncate-lines t))))
139 rval))
141 ;; We use a yank-handler to make yanked images unique, so that
142 ;; yanking two copies of the same image next to each other are
143 ;; recognized as two different images.
144 (defun image-file-yank-handler (string)
145 "Yank handler for inserting an image into a buffer."
146 (let ((len (length string))
147 (image (get-text-property 0 'display string)))
148 (remove-text-properties 0 len yank-excluded-properties string)
149 (if (consp image)
150 (add-text-properties 0
151 (or (next-single-property-change 0 'image-counter string)
152 (length string))
153 `(display
154 ,(cons (car image) (cdr image))
155 yank-handler
156 ,(cons 'image-file-yank-handler '(nil t)))
157 string))
158 (insert string)))
160 (put 'image-file-handler 'safe-magic t)
161 (defun image-file-handler (operation &rest args)
162 "Filename handler for inserting image files.
163 OPERATION is the operation to perform, on ARGS.
164 See `file-name-handler-alist' for details."
165 (if (and (eq operation 'insert-file-contents)
166 auto-image-file-mode)
167 (apply #'insert-image-file args)
168 ;; We don't handle OPERATION, use another handler or the default
169 (apply #'image-file-call-underlying operation operation args)))
171 (defun image-file-call-underlying (function operation &rest args)
172 "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
173 Optional argument ARGS are the arguments to call FUNCTION with."
174 (let ((inhibit-file-name-handlers
175 (cons 'image-file-handler
176 (and (eq inhibit-file-name-operation operation)
177 inhibit-file-name-handlers)))
178 (inhibit-file-name-operation operation))
179 (apply function args)))
182 ;;;###autoload
183 (define-minor-mode auto-image-file-mode
184 "Toggle visiting of image files as images.
185 With prefix argument ARG, turn on if positive, otherwise off.
186 Returns non-nil if the new state is enabled.
188 Image files are those whose name has an extension in
189 `image-file-name-extensions', or matches a regexp in
190 `image-file-name-regexps'."
191 :global t
192 :group 'image
193 ;; Remove existing handler
194 (let ((existing-entry
195 (rassq 'image-file-handler file-name-handler-alist)))
196 (when existing-entry
197 (setq file-name-handler-alist
198 (delq existing-entry file-name-handler-alist))))
199 ;; Add new handler, if enabled
200 (when auto-image-file-mode
201 (push (cons (image-file-name-regexp) 'image-file-handler)
202 file-name-handler-alist)))
205 (provide 'image-file)
207 ;;; arch-tag: 04cafe36-f7ba-4c80-9f47-4cb656520ce1
208 ;;; image-file.el ends here