1 ;;; image-file.el --- Support for visiting image files
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
5 ;; Author: Miles Bader <miles@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)
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.
27 ;; Defines a file-name-handler hook that transforms visited (or
28 ;; inserted) image files so that they are by displayed as emacs as
29 ;; images. This is done by putting a `display' text-property on the
30 ;; image data, with the image-data still present underneath; if the
31 ;; resulting buffer file is saved to another name it will correctly save
32 ;; the image data to the new file.
39 (defcustom image-file-name-extensions
40 '("png" "jpeg" "jpg" "gif" "tiff" "xbm" "xpm")
41 "*A list of image-file filename extensions.
42 Filenames having one of these extensions are considered image files,
43 in addition to those matching `image-file-name-regexps'.
45 See `auto-image-file-mode'; if `auto-image-file-mode' is enabled,
46 setting this variable directly does not take effect unless
47 `auto-image-file-mode' is re-enabled; this happens automatically the
48 variable is set using \\[customize]."
49 :type
'(repeat string
)
50 :set
(lambda (sym val
)
52 (when auto-image-file-mode
53 ;; Re-initialize the image-file handler
54 (auto-image-file-mode t
)))
55 :initialize
'custom-initialize-default
58 (defcustom image-file-name-regexps nil
59 "*A list of regexps matching image-file filenames.
60 Filenames matching one of these regexps are considered image files,
61 in addition to those with an extension in `image-file-name-extensions'.
63 See `auto-image-file-mode'; if `auto-image-file-mode' is enabled,
64 setting this variable directly does not take effect unless
65 `auto-image-file-mode' is re-enabled; this happens automatically the
66 variable is set using \\[customize]."
67 :type
'(repeat regexp
)
68 :set
(lambda (sym val
)
70 (when auto-image-file-mode
71 ;; Re-initialize the image-file handler
72 (auto-image-file-mode t
)))
73 :initialize
'custom-initialize-default
78 (defun image-file-name-regexp ()
79 "Return a regular expression that matches image-file filenames."
81 (and image-file-name-extensions
83 (regexp-opt image-file-name-extensions t
)
85 (if image-file-name-regexps
88 (cons exts-regexp image-file-regexps
)
95 (define-minor-mode auto-image-file-mode
96 "Toggle visiting of image files as images.
97 With prefix argument ARG, turn on if positive, otherwise off.
98 Returns non-nil if the new state is enabled.
100 Image files are those whose name has an extension in
101 `image-file-name-extensions', or matches a regexp in
102 `image-file-name-regexps'."
108 ;; Remove existing handler
109 (let ((existing-entry
110 (rassq 'image-file-handler file-name-handler-alist
)))
112 (setq file-name-handler-alist
113 (delq existing-entry file-name-handler-alist
))))
114 ;; Add new handler, if enabled
115 (when auto-image-file-mode
116 (push (cons (image-file-name-regexp) 'image-file-handler
)
117 file-name-handler-alist
)))
121 (defun insert-image-file (file &optional visit beg end replace
)
122 "Insert the image file FILE into the current buffer.
123 Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for
124 the command `insert-file-contents'."
126 (image-file-call-underlying #'insert-file-contents-literally
127 'insert-file-contents
128 file visit beg end replace
)))
129 ;; Turn the image data into a real image, but only if the whole file
131 (when (and (or (null beg
) (zerop beg
)) (null end
))
132 (let* ((ibeg (point))
133 (iend (+ (point) (cadr rval
)))
136 (buffer-substring-no-properties ibeg iend
)))
138 (create-image data nil t
))
142 rear-nonsticky
(display)
143 ;; This a cheap attempt to make the whole buffer
144 ;; read-only when we're visiting the file.
148 '(read-only t front-sticky
(read-only))))))
149 (add-text-properties ibeg iend props
)))
152 (defun image-file-handler (operation &rest args
)
153 "File name handler for inserting image files.
154 OPERATION is the operation to perform, on ARGS.
155 See `file-name-handler-alist' for details."
156 (if (and (eq operation
'insert-file-contents
)
157 auto-image-file-mode
)
158 (apply #'insert-image-file args
)
159 ;; We don't handle OPERATION, use another handler or the default
160 (apply #'image-file-call-underlying operation operation args
)))
162 (defun image-file-call-underlying (function operation
&rest args
)
163 "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
164 Optional argument ARGS are the arguments to call FUNCTION with."
165 (let ((inhibit-file-name-handlers
166 (cons 'image-file-handler
167 (and (eq inhibit-file-name-operation operation
)
168 inhibit-file-name-handlers
)))
169 (inhibit-file-name-operation operation
))
170 (apply function args
)))
173 (provide 'image-file
)
175 ;;; image-file.el ends here