Commit | Line | Data |
---|---|---|
5859e61a MB |
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 | |
28 | ;; inserted) image files so that they are by displayed as 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 | ;;;###autoload | |
39 | (defcustom image-file-handler-enabled nil | |
40 | "True if visiting an image file will actually display the image. | |
41 | A file is considered an image file if its filename matches one of the | |
42 | regexps in `image-file-regexps'. | |
43 | ||
44 | Setting this variable directly does not take effect; | |
45 | use either \\[customize] or the function `set-image-file-handler-enabled'." | |
46 | :type 'boolean | |
47 | :set (lambda (sym val) (set-image-file-handler-enabled val)) | |
48 | :initialize 'custom-initialize-default | |
49 | :require 'image-file | |
50 | :group 'image) | |
51 | ||
52 | (defcustom image-file-regexps | |
53 | '("\\.png$" "\\.jpeg$" "\\.jpg$" "\\.gif$" "\\.tiff$" "\\.x[bp]m$") | |
54 | "*A list of regexps matching files that should be displayed as images. | |
55 | ||
56 | Setting this variable directly does not take effect until the next time | |
57 | `set-image-file-handler-enabled' is called (which happens automatically | |
58 | when using \\[customize]." | |
59 | :type '(repeat regexp) | |
60 | :set (lambda (sym val) | |
61 | (set-default sym val) | |
62 | (when image-file-handler-enabled | |
63 | ;; Re-initialize the image-file handler | |
64 | (set-image-file-handler-enabled t))) | |
65 | :initialize 'custom-initialize-default | |
66 | :group 'image) | |
67 | ||
68 | ;;;###autoload | |
69 | (defun insert-image-file (file &optional visit beg end replace) | |
70 | "Insert the image file FILE into the current buffer. | |
71 | Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for | |
72 | the command `insert-file-contents'." | |
73 | (let ((rval | |
74 | (image-file-call-underlying #'insert-file-contents-literally | |
75 | 'insert-file-contents | |
76 | file visit beg end replace))) | |
77 | (when (and image-file-handler-enabled (or (null beg) (zerop beg)) (null end)) | |
78 | ;; Make image into a picture, but only if the whole file was inserted | |
79 | (let* ((ibeg (point)) | |
80 | (iend (+ (point) (cadr rval))) | |
81 | (data | |
82 | (string-make-unibyte (buffer-substring-no-properties ibeg iend))) | |
83 | (image | |
84 | (create-image data nil t)) | |
85 | (props | |
86 | `(display ,image | |
87 | intangible ,image | |
88 | rear-nonsticky (display) | |
89 | ;; This a cheap attempt to make the whole buffer | |
90 | ;; read-only when we're visiting the file. | |
91 | ,@(and visit | |
92 | (= ibeg (point-min)) | |
93 | (= iend (point-max)) | |
94 | '(read-only t front-sticky (read-only)))))) | |
95 | (add-text-properties ibeg iend props))) | |
96 | rval)) | |
97 | ||
98 | (defun image-file-handler (operation &rest args) | |
99 | "File name handler for inserting image files. | |
100 | OPERATION is the operation to perform, on ARGS. | |
101 | See `file-name-handler-alist' for details." | |
102 | (if (eq operation 'insert-file-contents) | |
103 | (apply #'insert-image-file args) | |
104 | ;; We don't handle OPERATION, use another handler or the default | |
105 | (apply #'image-file-call-underlying operation operation args))) | |
106 | ||
107 | (defun image-file-call-underlying (function operation &rest args) | |
108 | "Call FUNCTION with `image-file-handler' and OPERATION inhibited. | |
109 | Optional argument ARGS are the arguments to call FUNCTION with." | |
110 | (let ((inhibit-file-name-handlers | |
111 | (cons 'image-file-handler | |
112 | (and (eq inhibit-file-name-operation operation) | |
113 | inhibit-file-name-handlers))) | |
114 | (inhibit-file-name-operation operation)) | |
115 | (apply function args))) | |
116 | ||
117 | ;;;###autoload | |
118 | (defun set-image-file-handler-enabled (enabled) | |
119 | "Enable or disable visiting image files as real images, as per ENABLED. | |
120 | The regexp in `image-file-regexp' is used to determine which filenames are | |
121 | considered image files." | |
122 | ;; Remove existing handler | |
123 | (let ((existing-entry (rassq 'image-file-handler file-name-handler-alist))) | |
124 | (when existing-entry | |
125 | (setq file-name-handler-alist | |
126 | (delq existing-entry file-name-handler-alist)))) | |
127 | ;; Add new handler | |
128 | (when enabled | |
129 | (let ((regexp | |
130 | (concat "\\(" | |
131 | (mapconcat 'identity image-file-regexps "\\|") | |
132 | "\\)"))) | |
133 | (setq file-name-handler-alist | |
134 | (cons (cons regexp 'image-file-handler) file-name-handler-alist)))) | |
135 | (setq-default image-file-handler-enabled enabled)) | |
136 | ||
137 | ;;;###autoload | |
138 | (defun enable-image-file-handler () | |
139 | "Enable visiting image files as real images. | |
140 | The regexp in `image-file-regexp' is used to determine which filenames are | |
141 | considered image files." | |
142 | (interactive) | |
143 | (set-image-file-handler-enabled t)) | |
144 | ||
145 | (defun disable-image-file-handler () | |
146 | "Disable visiting image files as real images." | |
147 | (interactive) | |
148 | (set-image-file-handler-enabled nil)) | |
149 | ||
150 | (provide 'image-file) | |
151 | ||
152 | ;;; image-file.el ends here |