Mercurial > emacs
annotate lisp/image-file.el @ 33813:03ae5e62feb4
comment
author | Dave Love <fx@gnu.org> |
---|---|
date | Thu, 23 Nov 2000 16:05:05 +0000 |
parents | 242c7cbf0a6b |
children | 89fcace178df |
rev | line source |
---|---|
31934 | 1 ;;; image-file.el --- Support for visiting image files |
2 ;; | |
3 ;; Copyright (C) 2000 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., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; Defines a file-name-handler hook that transforms visited (or | |
32220
3c7e4ccb6fee
(image-file-name-extensions): Add pbm.
Dave Love <fx@gnu.org>
parents:
32218
diff
changeset
|
28 ;; inserted) image files so that they are displayed by emacs as |
31934 | 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 | |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
38 |
32440
4e98e54082d2
(image-file-name-extensions, image-file-name-regexps): Add autoload cookies.
Miles Bader <miles@gnu.org>
parents:
32316
diff
changeset
|
39 ;;;###autoload |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
40 (defcustom image-file-name-extensions |
32220
3c7e4ccb6fee
(image-file-name-extensions): Add pbm.
Dave Love <fx@gnu.org>
parents:
32218
diff
changeset
|
41 '("png" "jpeg" "jpg" "gif" "tiff" "xbm" "xpm" "pbm") |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
42 "*A list of image-file filename extensions. |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
43 Filenames having one of these extensions are considered image files, |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
44 in addition to those matching `image-file-name-regexps'. |
31934 | 45 |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
46 See `auto-image-file-mode'; if `auto-image-file-mode' is enabled, |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
47 setting this variable directly does not take effect unless |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
48 `auto-image-file-mode' is re-enabled; this happens automatically the |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
49 variable is set using \\[customize]." |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
50 :type '(repeat string) |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
51 :set (lambda (sym val) |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
52 (set-default sym val) |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
53 (when auto-image-file-mode |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
54 ;; Re-initialize the image-file handler |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
55 (auto-image-file-mode t))) |
31934 | 56 :initialize 'custom-initialize-default |
57 :group 'image) | |
58 | |
32440
4e98e54082d2
(image-file-name-extensions, image-file-name-regexps): Add autoload cookies.
Miles Bader <miles@gnu.org>
parents:
32316
diff
changeset
|
59 ;;;###autoload |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
60 (defcustom image-file-name-regexps nil |
32185
f7460e78c02a
Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32003
diff
changeset
|
61 "*List of regexps matching image-file filenames. |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
62 Filenames matching one of these regexps are considered image files, |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
63 in addition to those with an extension in `image-file-name-extensions'. |
31934 | 64 |
32220
3c7e4ccb6fee
(image-file-name-extensions): Add pbm.
Dave Love <fx@gnu.org>
parents:
32218
diff
changeset
|
65 See function `auto-image-file-mode'; if `auto-image-file-mode' is |
3c7e4ccb6fee
(image-file-name-extensions): Add pbm.
Dave Love <fx@gnu.org>
parents:
32218
diff
changeset
|
66 enabled, setting this variable directly does not take effect unless |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
67 `auto-image-file-mode' is re-enabled; this happens automatically the |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
68 variable is set using \\[customize]." |
31934 | 69 :type '(repeat regexp) |
70 :set (lambda (sym val) | |
71 (set-default sym val) | |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
72 (when auto-image-file-mode |
31934 | 73 ;; Re-initialize the image-file handler |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
74 (auto-image-file-mode t))) |
31934 | 75 :initialize 'custom-initialize-default |
76 :group 'image) | |
77 | |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
78 |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
79 ;;;###autoload |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
80 (defun image-file-name-regexp () |
32185
f7460e78c02a
Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32003
diff
changeset
|
81 "Return a regular expression matching image-file filenames." |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
82 (let ((exts-regexp |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
83 (and image-file-name-extensions |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
84 (concat "\\." |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
85 (regexp-opt image-file-name-extensions t) |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
86 "\\'")))) |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
87 (if image-file-name-regexps |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
88 (mapconcat 'identity |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
89 (if exts-regexp |
32218
65d06b88701d
(image-file-name-regexp): image-file-regexps -> image-file-name-regexps.
Dave Love <fx@gnu.org>
parents:
32185
diff
changeset
|
90 (cons exts-regexp image-file-name-regexps) |
65d06b88701d
(image-file-name-regexp): image-file-regexps -> image-file-name-regexps.
Dave Love <fx@gnu.org>
parents:
32185
diff
changeset
|
91 image-file-name-regexps) |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
92 "\\|") |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
93 exts-regexp))) |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
94 |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
95 |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
96 ;;;###autoload |
31934 | 97 (defun insert-image-file (file &optional visit beg end replace) |
98 "Insert the image file FILE into the current buffer. | |
99 Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for | |
100 the command `insert-file-contents'." | |
101 (let ((rval | |
102 (image-file-call-underlying #'insert-file-contents-literally | |
103 'insert-file-contents | |
104 file visit beg end replace))) | |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
105 ;; Turn the image data into a real image, but only if the whole file |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
106 ;; was inserted |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
107 (when (and (or (null beg) (zerop beg)) (null end)) |
31934 | 108 (let* ((ibeg (point)) |
109 (iend (+ (point) (cadr rval))) | |
110 (data | |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
111 (string-make-unibyte |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
112 (buffer-substring-no-properties ibeg iend))) |
31934 | 113 (image |
114 (create-image data nil t)) | |
115 (props | |
116 `(display ,image | |
117 intangible ,image | |
33727
242c7cbf0a6b
(insert-image-file): Don't make `read-only' property rear-nonsticky.
Miles Bader <miles@gnu.org>
parents:
33608
diff
changeset
|
118 rear-nonsticky (display intangible) |
31934 | 119 ;; This a cheap attempt to make the whole buffer |
33727
242c7cbf0a6b
(insert-image-file): Don't make `read-only' property rear-nonsticky.
Miles Bader <miles@gnu.org>
parents:
33608
diff
changeset
|
120 ;; read-only when we're visiting the file (as |
242c7cbf0a6b
(insert-image-file): Don't make `read-only' property rear-nonsticky.
Miles Bader <miles@gnu.org>
parents:
33608
diff
changeset
|
121 ;; opposed to just inserting it). |
31934 | 122 ,@(and visit |
123 (= ibeg (point-min)) | |
124 (= iend (point-max)) | |
125 '(read-only t front-sticky (read-only)))))) | |
126 (add-text-properties ibeg iend props))) | |
127 rval)) | |
128 | |
129 (defun image-file-handler (operation &rest args) | |
32185
f7460e78c02a
Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32003
diff
changeset
|
130 "Filename handler for inserting image files. |
31934 | 131 OPERATION is the operation to perform, on ARGS. |
132 See `file-name-handler-alist' for details." | |
32003
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
133 (if (and (eq operation 'insert-file-contents) |
ff2dfe1f1df9
(image-file-name-extensions): New variable.
Miles Bader <miles@gnu.org>
parents:
31934
diff
changeset
|
134 auto-image-file-mode) |
31934 | 135 (apply #'insert-image-file args) |
136 ;; We don't handle OPERATION, use another handler or the default | |
137 (apply #'image-file-call-underlying operation operation args))) | |
138 | |
139 (defun image-file-call-underlying (function operation &rest args) | |
140 "Call FUNCTION with `image-file-handler' and OPERATION inhibited. | |
141 Optional argument ARGS are the arguments to call FUNCTION with." | |
142 (let ((inhibit-file-name-handlers | |
143 (cons 'image-file-handler | |
144 (and (eq inhibit-file-name-operation operation) | |
145 inhibit-file-name-handlers))) | |
146 (inhibit-file-name-operation operation)) | |
147 (apply function args))) | |
148 | |
149 | |
32316
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
150 ;;; Note this definition must be at the end of the file, because |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
151 ;;; `define-minor-mode' actually calls the mode-function if the |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
152 ;;; associated variable is non-nil, which requires that all needed |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
153 ;;; functions be already defined. [This is arguably a bug in d-m-m] |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
154 ;;;###autoload |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
155 (define-minor-mode auto-image-file-mode |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
156 "Toggle visiting of image files as images. |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
157 With prefix argument ARG, turn on if positive, otherwise off. |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
158 Returns non-nil if the new state is enabled. |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
159 |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
160 Image files are those whose name has an extension in |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
161 `image-file-name-extensions', or matches a regexp in |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
162 `image-file-name-regexps'." |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
163 :global t |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
164 :group 'image |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
165 ;; Remove existing handler |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
166 (let ((existing-entry |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
167 (rassq 'image-file-handler file-name-handler-alist))) |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
168 (when existing-entry |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
169 (setq file-name-handler-alist |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
170 (delq existing-entry file-name-handler-alist)))) |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
171 ;; Add new handler, if enabled |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
172 (when auto-image-file-mode |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
173 (push (cons (image-file-name-regexp) 'image-file-handler) |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
174 file-name-handler-alist))) |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
175 |
b930d561cd53
(auto-image-file-mode): Move to the end of the file, because
Miles Bader <miles@gnu.org>
parents:
32220
diff
changeset
|
176 |
31934 | 177 (provide 'image-file) |
178 | |
179 ;;; image-file.el ends here |