]> code.delx.au - gnu-emacs/blob - lisp/image-file.el
(insert-image-file): Add yank-handler.
[gnu-emacs] / lisp / image-file.el
1 ;;; image-file.el --- support for visiting image files
2 ;;
3 ;; Copyright (C) 2000, 2001 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
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.
33
34 ;;; Code:
35
36 (require 'image)
37
38
39 ;;;###autoload
40 (defcustom image-file-name-extensions
41 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm" "xpm" "pbm" "pgm" "ppm" "pnm")
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'.
45
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 when
49 the variable 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)))
56 :initialize 'custom-initialize-default
57 :group 'image)
58
59 ;;;###autoload
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'.
64
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 when
68 the variable is set using \\[customize]."
69 :type '(repeat regexp)
70 :set (lambda (sym val)
71 (set-default 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
76 :group 'image)
77
78
79 ;;;###autoload
80 (defun image-file-name-regexp ()
81 "Return a regular expression matching image-file filenames."
82 (let ((exts-regexp
83 (and image-file-name-extensions
84 (concat "\\."
85 (regexp-opt (nconc (mapcar #'upcase
86 image-file-name-extensions)
87 image-file-name-extensions)
88 t)
89 "\\'"))))
90 (if image-file-name-regexps
91 (mapconcat 'identity
92 (if exts-regexp
93 (cons exts-regexp image-file-name-regexps)
94 image-file-name-regexps)
95 "\\|")
96 exts-regexp)))
97
98
99 ;;;###autoload
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'."
104 (let ((rval
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
109 ;; was inserted
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))))
114 (data
115 (string-make-unibyte
116 (buffer-substring-no-properties ibeg iend)))
117 (image
118 (create-image data nil t))
119 (props
120 `(display ,image
121 yank-handler (image-file-yank-handler)
122 intangible ,image
123 rear-nonsticky (display intangible)
124 ;; This a cheap attempt to make the whole buffer
125 ;; read-only when we're visiting the file (as
126 ;; opposed to just inserting it).
127 ,@(and visitingp
128 '(read-only t front-sticky (read-only))))))
129 (add-text-properties ibeg iend props)
130 (when visitingp
131 ;; Inhibit the cursor when the buffer contains only an image,
132 ;; because cursors look very strange on top of images.
133 (setq cursor-type nil)
134 ;; This just makes the arrow displayed in the right fringe
135 ;; area look correct when the image is wider than the window.
136 (setq truncate-lines t))))
137 rval))
138
139 ;; We use a yank-handler to make yanked images unique, so that
140 ;; yanking two copies of the same image next to each other are
141 ;; recognized as two different images.
142 (defun image-file-yank-handler (string)
143 "Yank handler for inserting an image into a buffer."
144 (let ((image (get-text-property 0 'display string)))
145 (if (consp image)
146 (put-text-property 0 (length string)
147 'display
148 (cons (car image) (cdr image))
149 string))
150 (insert string)))
151
152 (put 'image-file-handler 'safe-magic t)
153 (defun image-file-handler (operation &rest args)
154 "Filename handler for inserting image files.
155 OPERATION is the operation to perform, on ARGS.
156 See `file-name-handler-alist' for details."
157 (if (and (eq operation 'insert-file-contents)
158 auto-image-file-mode)
159 (apply #'insert-image-file args)
160 ;; We don't handle OPERATION, use another handler or the default
161 (apply #'image-file-call-underlying operation operation args)))
162
163 (defun image-file-call-underlying (function operation &rest args)
164 "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
165 Optional argument ARGS are the arguments to call FUNCTION with."
166 (let ((inhibit-file-name-handlers
167 (cons 'image-file-handler
168 (and (eq inhibit-file-name-operation operation)
169 inhibit-file-name-handlers)))
170 (inhibit-file-name-operation operation))
171 (apply function args)))
172
173
174 ;;;###autoload
175 (define-minor-mode auto-image-file-mode
176 "Toggle visiting of image files as images.
177 With prefix argument ARG, turn on if positive, otherwise off.
178 Returns non-nil if the new state is enabled.
179
180 Image files are those whose name has an extension in
181 `image-file-name-extensions', or matches a regexp in
182 `image-file-name-regexps'."
183 :global t
184 :group 'image
185 ;; Remove existing handler
186 (let ((existing-entry
187 (rassq 'image-file-handler file-name-handler-alist)))
188 (when existing-entry
189 (setq file-name-handler-alist
190 (delq existing-entry file-name-handler-alist))))
191 ;; Add new handler, if enabled
192 (when auto-image-file-mode
193 (push (cons (image-file-name-regexp) 'image-file-handler)
194 file-name-handler-alist)))
195
196
197 (provide 'image-file)
198
199 ;;; arch-tag: 04cafe36-f7ba-4c80-9f47-4cb656520ce1
200 ;;; image-file.el ends here