Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / image-file.el
CommitLineData
112d54c1 1;;; image-file.el --- support for visiting image files
5859e61a 2;;
0d30b337 3;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
49f70d46 4;; 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5859e61a
MB
5;;
6;; Author: Miles Bader <miles@gnu.org>
7;; Keywords: multimedia
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
5859e61a 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
5859e61a
MB
15
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5859e61a
MB
23
24;;; Commentary:
25
26;; Defines a file-name-handler hook that transforms visited (or
112d54c1 27;; inserted) image files so that they are displayed by Emacs as
5859e61a
MB
28;; images. This is done by putting a `display' text-property on the
29;; image data, with the image-data still present underneath; if the
30;; resulting buffer file is saved to another name it will correctly save
31;; the image data to the new file.
32
33;;; Code:
34
35(require 'image)
36
75e5b373 37
1c7e37a9 38;;;###autoload
75e5b373 39(defcustom image-file-name-extensions
1e8780b1 40 (purecopy '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm" "xpm" "pbm" "pgm" "ppm" "pnm" "svg"))
9201cc28 41 "A list of image-file filename extensions.
75e5b373
MB
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
228b0b9a
DL
47`auto-image-file-mode' is re-enabled; this happens automatically when
48the variable is set using \\[customize]."
75e5b373
MB
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
1c7e37a9 58;;;###autoload
75e5b373 59(defcustom image-file-name-regexps nil
9201cc28 60 "List of regexps matching image-file filenames.
75e5b373
MB
61Filenames matching one of these regexps are considered image files,
62in addition to those with an extension in `image-file-name-extensions'.
5859e61a 63
db623cef
DL
64See function `auto-image-file-mode'; if `auto-image-file-mode' is
65enabled, setting this variable directly does not take effect unless
228b0b9a
DL
66`auto-image-file-mode' is re-enabled; this happens automatically when
67the variable is set using \\[customize]."
5859e61a
MB
68 :type '(repeat regexp)
69 :set (lambda (sym val)
70 (set-default sym val)
75e5b373 71 (when auto-image-file-mode
5859e61a 72 ;; Re-initialize the image-file handler
75e5b373 73 (auto-image-file-mode t)))
5859e61a
MB
74 :initialize 'custom-initialize-default
75 :group 'image)
76
75e5b373
MB
77
78;;;###autoload
79(defun image-file-name-regexp ()
34a19346 80 "Return a regular expression matching image-file filenames."
75e5b373
MB
81 (let ((exts-regexp
82 (and image-file-name-extensions
83 (concat "\\."
b170205b
MB
84 (regexp-opt (nconc (mapcar #'upcase
85 image-file-name-extensions)
86 image-file-name-extensions)
87 t)
75e5b373
MB
88 "\\'"))))
89 (if image-file-name-regexps
90 (mapconcat 'identity
91 (if exts-regexp
a23ccdf2
DL
92 (cons exts-regexp image-file-name-regexps)
93 image-file-name-regexps)
75e5b373
MB
94 "\\|")
95 exts-regexp)))
96
97
5859e61a
MB
98;;;###autoload
99(defun insert-image-file (file &optional visit beg end replace)
100 "Insert the image file FILE into the current buffer.
101Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for
102the command `insert-file-contents'."
103 (let ((rval
104 (image-file-call-underlying #'insert-file-contents-literally
105 'insert-file-contents
106 file visit beg end replace)))
75e5b373
MB
107 ;; Turn the image data into a real image, but only if the whole file
108 ;; was inserted
109 (when (and (or (null beg) (zerop beg)) (null end))
5859e61a
MB
110 (let* ((ibeg (point))
111 (iend (+ (point) (cadr rval)))
33155ffb 112 (visitingp (and visit (= ibeg (point-min)) (= iend (point-max))))
5859e61a 113 (data
75e5b373
MB
114 (string-make-unibyte
115 (buffer-substring-no-properties ibeg iend)))
5859e61a
MB
116 (image
117 (create-image data nil t))
118 (props
119 `(display ,image
eab48795
KS
120 yank-handler
121 (image-file-yank-handler nil t)
5859e61a 122 intangible ,image
a02b5bb4 123 rear-nonsticky (display intangible)
5859e61a 124 ;; This a cheap attempt to make the whole buffer
a02b5bb4
MB
125 ;; read-only when we're visiting the file (as
126 ;; opposed to just inserting it).
33155ffb 127 ,@(and visitingp
5859e61a 128 '(read-only t front-sticky (read-only))))))
33155ffb
MB
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.
65e70fc4
MB
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))))
5859e61a
MB
137 rval))
138
6ed554f2
KS
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."
eab48795
KS
144 (let ((len (length string))
145 (image (get-text-property 0 'display string)))
146 (remove-text-properties 0 len yank-excluded-properties string)
6ed554f2 147 (if (consp image)
eab48795
KS
148 (add-text-properties 0
149 (or (next-single-property-change 0 'image-counter string)
150 (length string))
151 `(display
152 ,(cons (car image) (cdr image))
153 yank-handler
154 ,(cons 'image-file-yank-handler '(nil t)))
155 string))
6ed554f2
KS
156 (insert string)))
157
15a420bd 158(put 'image-file-handler 'safe-magic t)
5859e61a 159(defun image-file-handler (operation &rest args)
34a19346 160 "Filename handler for inserting image files.
5859e61a
MB
161OPERATION is the operation to perform, on ARGS.
162See `file-name-handler-alist' for details."
75e5b373
MB
163 (if (and (eq operation 'insert-file-contents)
164 auto-image-file-mode)
5859e61a
MB
165 (apply #'insert-image-file args)
166 ;; We don't handle OPERATION, use another handler or the default
167 (apply #'image-file-call-underlying operation operation args)))
168
169(defun image-file-call-underlying (function operation &rest args)
170 "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
171Optional argument ARGS are the arguments to call FUNCTION with."
172 (let ((inhibit-file-name-handlers
173 (cons 'image-file-handler
174 (and (eq inhibit-file-name-operation operation)
175 inhibit-file-name-handlers)))
176 (inhibit-file-name-operation operation))
177 (apply function args)))
178
5859e61a 179
33c7a00c
MB
180;;;###autoload
181(define-minor-mode auto-image-file-mode
182 "Toggle visiting of image files as images.
183With prefix argument ARG, turn on if positive, otherwise off.
184Returns non-nil if the new state is enabled.
185
186Image files are those whose name has an extension in
187`image-file-name-extensions', or matches a regexp in
188`image-file-name-regexps'."
33c7a00c
MB
189 :global t
190 :group 'image
191 ;; Remove existing handler
192 (let ((existing-entry
193 (rassq 'image-file-handler file-name-handler-alist)))
194 (when existing-entry
195 (setq file-name-handler-alist
196 (delq existing-entry file-name-handler-alist))))
197 ;; Add new handler, if enabled
198 (when auto-image-file-mode
199 (push (cons (image-file-name-regexp) 'image-file-handler)
200 file-name-handler-alist)))
201
202
5859e61a
MB
203(provide 'image-file)
204
cbee283d 205;; arch-tag: 04cafe36-f7ba-4c80-9f47-4cb656520ce1
5859e61a 206;;; image-file.el ends here