1 ;;; image-file.el --- support for visiting image files
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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)
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.
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.
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
)
54 (when auto-image-file-mode
55 ;; Re-initialize the image-file handler
56 (auto-image-file-mode t
)))
57 :initialize
'custom-initialize-default
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
)
73 (when auto-image-file-mode
74 ;; Re-initialize the image-file handler
75 (auto-image-file-mode t
)))
76 :initialize
'custom-initialize-default
81 (defun image-file-name-regexp ()
82 "Return a regular expression matching image-file filenames."
84 (and image-file-name-extensions
86 (regexp-opt (nconc (mapcar #'upcase
87 image-file-name-extensions
)
88 image-file-name-extensions
)
91 (if image-file-name-regexps
94 (cons exts-regexp image-file-name-regexps
)
95 image-file-name-regexps
)
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'."
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
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))))
117 (buffer-substring-no-properties ibeg iend
)))
119 (create-image data nil t
))
123 (image-file-yank-handler nil t
)
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).
130 '(read-only t front-sticky
(read-only))))))
131 (add-text-properties ibeg iend props
)
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
))))
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
)
150 (add-text-properties 0
151 (or (next-single-property-change 0 'image-counter string
)
154 ,(cons (car image
) (cdr image
))
156 ,(cons 'image-file-yank-handler
'(nil t
)))
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
)))
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'."
193 ;; Remove existing handler
194 (let ((existing-entry
195 (rassq 'image-file-handler file-name-handler-alist
)))
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