1 ;;; image-file.el --- Support for visiting image files
3 ;; Copyright (C) 2000, 2001 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 displayed by 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.
40 (defcustom image-file-name-extensions
41 '("png" "jpeg" "jpg" "gif" "tiff" "xbm" "xpm" "pbm" "pgm" "ppm")
42 "*A list of image-file filename extensions.
43 Filenames having one of these extensions are considered image files,
44 in addition to those matching `image-file-name-regexps'.
46 See `auto-image-file-mode'; if `auto-image-file-mode' is enabled,
47 setting this variable directly does not take effect unless
48 `auto-image-file-mode' is re-enabled; this happens automatically the
49 variable is set using \\[customize]."
50 :type
'(repeat string
)
51 :set
(lambda (sym val
)
53 (when auto-image-file-mode
54 ;; Re-initialize the image-file handler
55 (auto-image-file-mode t
)))
56 :initialize
'custom-initialize-default
60 (defcustom image-file-name-regexps nil
61 "*List of regexps matching image-file filenames.
62 Filenames matching one of these regexps are considered image files,
63 in addition to those with an extension in `image-file-name-extensions'.
65 See function `auto-image-file-mode'; if `auto-image-file-mode' is
66 enabled, setting this variable directly does not take effect unless
67 `auto-image-file-mode' is re-enabled; this happens automatically the
68 variable is set using \\[customize]."
69 :type
'(repeat regexp
)
70 :set
(lambda (sym val
)
72 (when auto-image-file-mode
73 ;; Re-initialize the image-file handler
74 (auto-image-file-mode t
)))
75 :initialize
'custom-initialize-default
80 (defun image-file-name-regexp ()
81 "Return a regular expression matching image-file filenames."
83 (and image-file-name-extensions
85 (regexp-opt (nconc (mapcar #'upcase
86 image-file-name-extensions
)
87 image-file-name-extensions
)
90 (if image-file-name-regexps
93 (cons exts-regexp image-file-name-regexps
)
94 image-file-name-regexps
)
100 (defun insert-image-file (file &optional visit beg end replace
)
101 "Insert the image file FILE into the current buffer.
102 Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for
103 the command `insert-file-contents'."
105 (image-file-call-underlying #'insert-file-contents-literally
106 'insert-file-contents
107 file visit beg end replace
)))
108 ;; Turn the image data into a real image, but only if the whole file
110 (when (and (or (null beg
) (zerop beg
)) (null end
))
111 (let* ((ibeg (point))
112 (iend (+ (point) (cadr rval
)))
113 (visitingp (and visit
(= ibeg
(point-min)) (= iend
(point-max))))
116 (buffer-substring-no-properties ibeg iend
)))
118 (create-image data nil t
))
122 rear-nonsticky
(display intangible
)
123 ;; This a cheap attempt to make the whole buffer
124 ;; read-only when we're visiting the file (as
125 ;; opposed to just inserting it).
127 '(read-only t front-sticky
(read-only))))))
128 (add-text-properties ibeg iend props
)
130 ;; Inhibit the cursor when the buffer contains only an image,
131 ;; because cursors look very strange on top of images.
132 (setq cursor-type nil
)
133 ;; This just makes the arrow displayed in the right fringe
134 ;; area look correct when the image is wider than the window.
135 (setq truncate-lines t
))))
138 (defun image-file-handler (operation &rest args
)
139 "Filename handler for inserting image files.
140 OPERATION is the operation to perform, on ARGS.
141 See `file-name-handler-alist' for details."
142 (if (and (eq operation
'insert-file-contents
)
143 auto-image-file-mode
)
144 (apply #'insert-image-file args
)
145 ;; We don't handle OPERATION, use another handler or the default
146 (apply #'image-file-call-underlying operation operation args
)))
148 (defun image-file-call-underlying (function operation
&rest args
)
149 "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
150 Optional argument ARGS are the arguments to call FUNCTION with."
151 (let ((inhibit-file-name-handlers
152 (cons 'image-file-handler
153 (and (eq inhibit-file-name-operation operation
)
154 inhibit-file-name-handlers
)))
155 (inhibit-file-name-operation operation
))
156 (apply function args
)))
159 ;;; Note this definition must be at the end of the file, because
160 ;;; `define-minor-mode' actually calls the mode-function if the
161 ;;; associated variable is non-nil, which requires that all needed
162 ;;; functions be already defined. [This is arguably a bug in d-m-m]
164 (define-minor-mode auto-image-file-mode
165 "Toggle visiting of image files as images.
166 With prefix argument ARG, turn on if positive, otherwise off.
167 Returns non-nil if the new state is enabled.
169 Image files are those whose name has an extension in
170 `image-file-name-extensions', or matches a regexp in
171 `image-file-name-regexps'."
174 ;; Remove existing handler
175 (let ((existing-entry
176 (rassq 'image-file-handler file-name-handler-alist
)))
178 (setq file-name-handler-alist
179 (delq existing-entry file-name-handler-alist
))))
180 ;; Add new handler, if enabled
181 (when auto-image-file-mode
182 (push (cons (image-file-name-regexp) 'image-file-handler
)
183 file-name-handler-alist
)))
186 (provide 'image-file
)
188 ;;; image-file.el ends here