Update years in copyright notice; nfc.
[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,
aaef169d 4;; 2005, 2006 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
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 2, or (at your option)
14;; any later version.
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
22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
5859e61a
MB
25
26;;; Commentary:
27
28;; Defines a file-name-handler hook that transforms visited (or
112d54c1 29;; inserted) image files so that they are displayed by Emacs as
5859e61a
MB
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.
34
35;;; Code:
36
37(require 'image)
38
75e5b373 39
1c7e37a9 40;;;###autoload
75e5b373 41(defcustom image-file-name-extensions
e0e6e9b8 42 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm" "xpm" "pbm" "pgm" "ppm" "pnm")
75e5b373
MB
43 "*A list of image-file filename extensions.
44Filenames having one of these extensions are considered image files,
45in addition to those matching `image-file-name-regexps'.
46
47See `auto-image-file-mode'; if `auto-image-file-mode' is enabled,
48setting this variable directly does not take effect unless
228b0b9a
DL
49`auto-image-file-mode' is re-enabled; this happens automatically when
50the variable is set using \\[customize]."
75e5b373
MB
51 :type '(repeat string)
52 :set (lambda (sym val)
53 (set-default sym val)
54 (when auto-image-file-mode
55 ;; Re-initialize the image-file handler
56 (auto-image-file-mode t)))
5859e61a 57 :initialize 'custom-initialize-default
5859e61a
MB
58 :group 'image)
59
1c7e37a9 60;;;###autoload
75e5b373 61(defcustom image-file-name-regexps nil
34a19346 62 "*List of regexps matching image-file filenames.
75e5b373
MB
63Filenames matching one of these regexps are considered image files,
64in addition to those with an extension in `image-file-name-extensions'.
5859e61a 65
db623cef
DL
66See function `auto-image-file-mode'; if `auto-image-file-mode' is
67enabled, setting this variable directly does not take effect unless
228b0b9a
DL
68`auto-image-file-mode' is re-enabled; this happens automatically when
69the variable is set using \\[customize]."
5859e61a
MB
70 :type '(repeat regexp)
71 :set (lambda (sym val)
72 (set-default sym val)
75e5b373 73 (when auto-image-file-mode
5859e61a 74 ;; Re-initialize the image-file handler
75e5b373 75 (auto-image-file-mode t)))
5859e61a
MB
76 :initialize 'custom-initialize-default
77 :group 'image)
78
75e5b373
MB
79
80;;;###autoload
81(defun image-file-name-regexp ()
34a19346 82 "Return a regular expression matching image-file filenames."
75e5b373
MB
83 (let ((exts-regexp
84 (and image-file-name-extensions
85 (concat "\\."
b170205b
MB
86 (regexp-opt (nconc (mapcar #'upcase
87 image-file-name-extensions)
88 image-file-name-extensions)
89 t)
75e5b373
MB
90 "\\'"))))
91 (if image-file-name-regexps
92 (mapconcat 'identity
93 (if exts-regexp
a23ccdf2
DL
94 (cons exts-regexp image-file-name-regexps)
95 image-file-name-regexps)
75e5b373
MB
96 "\\|")
97 exts-regexp)))
98
99
5859e61a
MB
100;;;###autoload
101(defun insert-image-file (file &optional visit beg end replace)
102 "Insert the image file FILE into the current buffer.
103Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for
104the command `insert-file-contents'."
105 (let ((rval
106 (image-file-call-underlying #'insert-file-contents-literally
107 'insert-file-contents
108 file visit beg end replace)))
75e5b373
MB
109 ;; Turn the image data into a real image, but only if the whole file
110 ;; was inserted
111 (when (and (or (null beg) (zerop beg)) (null end))
5859e61a
MB
112 (let* ((ibeg (point))
113 (iend (+ (point) (cadr rval)))
33155ffb 114 (visitingp (and visit (= ibeg (point-min)) (= iend (point-max))))
5859e61a 115 (data
75e5b373
MB
116 (string-make-unibyte
117 (buffer-substring-no-properties ibeg iend)))
5859e61a
MB
118 (image
119 (create-image data nil t))
120 (props
121 `(display ,image
eab48795
KS
122 yank-handler
123 (image-file-yank-handler nil t)
5859e61a 124 intangible ,image
a02b5bb4 125 rear-nonsticky (display intangible)
5859e61a 126 ;; This a cheap attempt to make the whole buffer
a02b5bb4
MB
127 ;; read-only when we're visiting the file (as
128 ;; opposed to just inserting it).
33155ffb 129 ,@(and visitingp
5859e61a 130 '(read-only t front-sticky (read-only))))))
33155ffb
MB
131 (add-text-properties ibeg iend props)
132 (when visitingp
133 ;; Inhibit the cursor when the buffer contains only an image,
134 ;; because cursors look very strange on top of images.
65e70fc4
MB
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))))
5859e61a
MB
139 rval))
140
6ed554f2
KS
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."
eab48795
KS
146 (let ((len (length string))
147 (image (get-text-property 0 'display string)))
148 (remove-text-properties 0 len yank-excluded-properties string)
6ed554f2 149 (if (consp image)
eab48795
KS
150 (add-text-properties 0
151 (or (next-single-property-change 0 'image-counter string)
152 (length string))
153 `(display
154 ,(cons (car image) (cdr image))
155 yank-handler
156 ,(cons 'image-file-yank-handler '(nil t)))
157 string))
6ed554f2
KS
158 (insert string)))
159
15a420bd 160(put 'image-file-handler 'safe-magic t)
5859e61a 161(defun image-file-handler (operation &rest args)
34a19346 162 "Filename handler for inserting image files.
5859e61a
MB
163OPERATION is the operation to perform, on ARGS.
164See `file-name-handler-alist' for details."
75e5b373
MB
165 (if (and (eq operation 'insert-file-contents)
166 auto-image-file-mode)
5859e61a
MB
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)))
170
171(defun image-file-call-underlying (function operation &rest args)
172 "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
173Optional 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)))
180
5859e61a 181
33c7a00c
MB
182;;;###autoload
183(define-minor-mode auto-image-file-mode
184 "Toggle visiting of image files as images.
185With prefix argument ARG, turn on if positive, otherwise off.
186Returns non-nil if the new state is enabled.
187
188Image files are those whose name has an extension in
189`image-file-name-extensions', or matches a regexp in
190`image-file-name-regexps'."
33c7a00c
MB
191 :global t
192 :group 'image
193 ;; Remove existing handler
194 (let ((existing-entry
195 (rassq 'image-file-handler file-name-handler-alist)))
196 (when existing-entry
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)))
203
204
5859e61a
MB
205(provide 'image-file)
206
ab5796a9 207;;; arch-tag: 04cafe36-f7ba-4c80-9f47-4cb656520ce1
5859e61a 208;;; image-file.el ends here