(select-safe-coding-system): If FROM is string, show it in *Warning*
[bpt/emacs.git] / lisp / image-file.el
CommitLineData
5859e61a
MB
1;;; image-file.el --- Support for visiting image files
2;;
3;; Copyright (C) 2000 Free Software Foundation, Inc.
4;;
5;; Author: Miles Bader <miles@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
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; Defines a file-name-handler hook that transforms visited (or
db623cef 28;; inserted) image files so that they are displayed by emacs as
5859e61a
MB
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.
33
34;;; Code:
35
36(require 'image)
37
75e5b373
MB
38
39(defcustom image-file-name-extensions
db623cef 40 '("png" "jpeg" "jpg" "gif" "tiff" "xbm" "xpm" "pbm")
75e5b373
MB
41 "*A list of image-file filename extensions.
42Filenames having one of these extensions are considered image files,
43in addition to those matching `image-file-name-regexps'.
44
45See `auto-image-file-mode'; if `auto-image-file-mode' is enabled,
46setting this variable directly does not take effect unless
47`auto-image-file-mode' is re-enabled; this happens automatically the
48variable is set using \\[customize]."
49 :type '(repeat string)
50 :set (lambda (sym val)
51 (set-default sym val)
52 (when auto-image-file-mode
53 ;; Re-initialize the image-file handler
54 (auto-image-file-mode t)))
5859e61a 55 :initialize 'custom-initialize-default
5859e61a
MB
56 :group 'image)
57
75e5b373 58(defcustom image-file-name-regexps nil
34a19346 59 "*List of regexps matching image-file filenames.
75e5b373
MB
60Filenames matching one of these regexps are considered image files,
61in addition to those with an extension in `image-file-name-extensions'.
5859e61a 62
db623cef
DL
63See function `auto-image-file-mode'; if `auto-image-file-mode' is
64enabled, setting this variable directly does not take effect unless
75e5b373
MB
65`auto-image-file-mode' is re-enabled; this happens automatically the
66variable is set using \\[customize]."
5859e61a
MB
67 :type '(repeat regexp)
68 :set (lambda (sym val)
69 (set-default sym val)
75e5b373 70 (when auto-image-file-mode
5859e61a 71 ;; Re-initialize the image-file handler
75e5b373 72 (auto-image-file-mode t)))
5859e61a
MB
73 :initialize 'custom-initialize-default
74 :group 'image)
75
75e5b373
MB
76
77;;;###autoload
78(defun image-file-name-regexp ()
34a19346 79 "Return a regular expression matching image-file filenames."
75e5b373
MB
80 (let ((exts-regexp
81 (and image-file-name-extensions
82 (concat "\\."
83 (regexp-opt image-file-name-extensions t)
84 "\\'"))))
85 (if image-file-name-regexps
86 (mapconcat 'identity
87 (if exts-regexp
a23ccdf2
DL
88 (cons exts-regexp image-file-name-regexps)
89 image-file-name-regexps)
75e5b373
MB
90 "\\|")
91 exts-regexp)))
92
93
5859e61a
MB
94;;;###autoload
95(defun insert-image-file (file &optional visit beg end replace)
96 "Insert the image file FILE into the current buffer.
97Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for
98the command `insert-file-contents'."
99 (let ((rval
100 (image-file-call-underlying #'insert-file-contents-literally
101 'insert-file-contents
102 file visit beg end replace)))
75e5b373
MB
103 ;; Turn the image data into a real image, but only if the whole file
104 ;; was inserted
105 (when (and (or (null beg) (zerop beg)) (null end))
5859e61a
MB
106 (let* ((ibeg (point))
107 (iend (+ (point) (cadr rval)))
108 (data
75e5b373
MB
109 (string-make-unibyte
110 (buffer-substring-no-properties ibeg iend)))
5859e61a
MB
111 (image
112 (create-image data nil t))
113 (props
114 `(display ,image
115 intangible ,image
116 rear-nonsticky (display)
117 ;; This a cheap attempt to make the whole buffer
118 ;; read-only when we're visiting the file.
119 ,@(and visit
120 (= ibeg (point-min))
121 (= iend (point-max))
122 '(read-only t front-sticky (read-only))))))
123 (add-text-properties ibeg iend props)))
124 rval))
125
126(defun image-file-handler (operation &rest args)
34a19346 127 "Filename handler for inserting image files.
5859e61a
MB
128OPERATION is the operation to perform, on ARGS.
129See `file-name-handler-alist' for details."
75e5b373
MB
130 (if (and (eq operation 'insert-file-contents)
131 auto-image-file-mode)
5859e61a
MB
132 (apply #'insert-image-file args)
133 ;; We don't handle OPERATION, use another handler or the default
134 (apply #'image-file-call-underlying operation operation args)))
135
136(defun image-file-call-underlying (function operation &rest args)
137 "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
138Optional argument ARGS are the arguments to call FUNCTION with."
139 (let ((inhibit-file-name-handlers
140 (cons 'image-file-handler
141 (and (eq inhibit-file-name-operation operation)
142 inhibit-file-name-handlers)))
143 (inhibit-file-name-operation operation))
144 (apply function args)))
145
5859e61a 146
33c7a00c
MB
147;;; Note this definition must be at the end of the file, because
148;;; `define-minor-mode' actually calls the mode-function if the
149;;; associated variable is non-nil, which requires that all needed
150;;; functions be already defined. [This is arguably a bug in d-m-m]
151;;;###autoload
152(define-minor-mode auto-image-file-mode
153 "Toggle visiting of image files as images.
154With prefix argument ARG, turn on if positive, otherwise off.
155Returns non-nil if the new state is enabled.
156
157Image files are those whose name has an extension in
158`image-file-name-extensions', or matches a regexp in
159`image-file-name-regexps'."
160 nil
161 nil
162 nil
163 :global t
164 :group 'image
165 ;; Remove existing handler
166 (let ((existing-entry
167 (rassq 'image-file-handler file-name-handler-alist)))
168 (when existing-entry
169 (setq file-name-handler-alist
170 (delq existing-entry file-name-handler-alist))))
171 ;; Add new handler, if enabled
172 (when auto-image-file-mode
173 (push (cons (image-file-name-regexp) 'image-file-handler)
174 file-name-handler-alist)))
175
176
5859e61a
MB
177(provide 'image-file)
178
179;;; image-file.el ends here