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