Commit | Line | Data |
---|---|---|
be010748 RS |
1 | ;;; jka-compr.el --- reading/writing/loading compressed files |
2 | ||
0d30b337 TTN |
3 | ;; Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 2002, 2003, |
4 | ;; 2004, 2005 Free Software Foundation, Inc. | |
acd622cc RS |
5 | |
6 | ;; Author: jka@ece.cmu.edu (Jay K. Adams) | |
4228277d | 7 | ;; Maintainer: FSF |
acd622cc RS |
8 | ;; Keywords: data |
9 | ||
f4454a14 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
f4454a14 | 26 | |
55535639 | 27 | ;;; Commentary: |
acd622cc | 28 | |
b578f267 EN |
29 | ;; This package implements low-level support for reading, writing, |
30 | ;; and loading compressed files. It hooks into the low-level file | |
31 | ;; I/O functions (including write-region and insert-file-contents) so | |
32 | ;; that they automatically compress or uncompress a file if the file | |
33 | ;; appears to need it (based on the extension of the file name). | |
34 | ;; Packages like Rmail, VM, GNUS, and Info should be able to work | |
35 | ;; with compressed files without modification. | |
36 | ||
37 | ||
38 | ;; INSTRUCTIONS: | |
39 | ;; | |
7391213d MB |
40 | ;; To use jka-compr, invoke the command `auto-compression-mode' (which |
41 | ;; see), or customize the variable of the same name. Its operation | |
42 | ;; should be transparent to the user (except for messages appearing when | |
43 | ;; a file is being compressed or uncompressed). | |
b578f267 EN |
44 | ;; |
45 | ;; The variable, jka-compr-compression-info-list can be used to | |
46 | ;; customize jka-compr to work with other compression programs. | |
47 | ;; The default value of this variable allows jka-compr to work with | |
48 | ;; Unix compress and gzip. | |
49 | ;; | |
50 | ;; If you are concerned about the stderr output of gzip and other | |
51 | ;; compression/decompression programs showing up in your buffers, you | |
52 | ;; should set the discard-error flag in the compression-info-list. | |
53 | ;; This will cause the stderr of all programs to be discarded. | |
54 | ;; However, it also causes emacs to call compression/uncompression | |
55 | ;; programs through a shell (which is specified by jka-compr-shell). | |
56 | ;; This may be a drag if, on your system, starting up a shell is | |
57 | ;; slow. | |
58 | ;; | |
59 | ;; If you don't want messages about compressing and decompressing | |
60 | ;; to show up in the echo area, you can set the compress-name and | |
61 | ;; decompress-name fields of the jka-compr-compression-info-list to | |
62 | ;; nil. | |
63 | ||
64 | ||
65 | ;; APPLICATION NOTES: | |
66 | ;; | |
67 | ;; crypt++ | |
5f320490 | 68 | ;; jka-compr can coexist with crypt++ if you take all the decompression |
b578f267 EN |
69 | ;; entries out of the crypt-encoding-list. Clearly problems will arise if |
70 | ;; you have two programs trying to compress/decompress files. jka-compr | |
71 | ;; will not "work with" crypt++ in the following sense: you won't be able to | |
72 | ;; decode encrypted compressed files--that is, files that have been | |
73 | ;; compressed then encrypted (in that order). Theoretically, crypt++ and | |
74 | ;; jka-compr could properly handle a file that has been encrypted then | |
75 | ;; compressed, but there is little point in trying to compress an encrypted | |
76 | ;; file. | |
77 | ;; | |
78 | ||
79 | ||
80 | ;; ACKNOWLEDGMENTS | |
f1180544 | 81 | ;; |
b578f267 | 82 | ;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people |
f1180544 | 83 | ;; have made helpful suggestions, reported bugs, and even fixed bugs in |
b578f267 EN |
84 | ;; jka-compr. I recall the following people as being particularly helpful. |
85 | ;; | |
86 | ;; Jean-loup Gailly | |
87 | ;; David Hughes | |
88 | ;; Richard Pieri | |
89 | ;; Daniel Quinlan | |
90 | ;; Chris P. Ross | |
91 | ;; Rick Sladkey | |
92 | ;; | |
93 | ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for | |
94 | ;; Version 18 of Emacs. | |
95 | ;; | |
96 | ;; After I had made progress on the original jka-compr for V18, I learned of a | |
97 | ;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly | |
98 | ;; what I was trying to do. I looked over the jam-zcat source code and | |
99 | ;; probably got some ideas from it. | |
100 | ;; | |
acd622cc RS |
101 | |
102 | ;;; Code: | |
103 | ||
bbf5eb28 | 104 | (defcustom jka-compr-shell "sh" |
acd622cc RS |
105 | "*Shell to be used for calling compression programs. |
106 | The value of this variable only matters if you want to discard the | |
107 | stderr of a compression/decompression program (see the documentation | |
bbf5eb28 RS |
108 | for `jka-compr-compression-info-list')." |
109 | :type 'string | |
110 | :group 'jka-compr) | |
acd622cc | 111 | |
f1180544 | 112 | (defvar jka-compr-use-shell |
eb915452 | 113 | (not (memq system-type '(ms-dos windows-nt)))) |
acd622cc | 114 | |
e073a356 | 115 | (defvar jka-compr-really-do-compress nil |
dd83d95a RS |
116 | "Non-nil in a buffer whose visited file was uncompressed on visiting it. |
117 | This means compress the data on writing the file, even if the | |
118 | data appears to be compressed already.") | |
119 | (make-variable-buffer-local 'jka-compr-really-do-compress) | |
e073a356 | 120 | (put 'jka-compr-really-do-compress 'permanent-local t) |
555235e6 | 121 | \f |
43a3bdcc | 122 | ;;; Functions for accessing the return value of jka-compr-get-compression-info |
acd622cc RS |
123 | (defun jka-compr-info-regexp (info) (aref info 0)) |
124 | (defun jka-compr-info-compress-message (info) (aref info 1)) | |
125 | (defun jka-compr-info-compress-program (info) (aref info 2)) | |
126 | (defun jka-compr-info-compress-args (info) (aref info 3)) | |
127 | (defun jka-compr-info-uncompress-message (info) (aref info 4)) | |
128 | (defun jka-compr-info-uncompress-program (info) (aref info 5)) | |
129 | (defun jka-compr-info-uncompress-args (info) (aref info 6)) | |
130 | (defun jka-compr-info-can-append (info) (aref info 7)) | |
131 | (defun jka-compr-info-strip-extension (info) (aref info 8)) | |
e073a356 | 132 | (defun jka-compr-info-file-magic-bytes (info) (aref info 9)) |
acd622cc RS |
133 | |
134 | ||
135 | (defun jka-compr-get-compression-info (filename) | |
136 | "Return information about the compression scheme of FILENAME. | |
137 | The determination as to which compression scheme, if any, to use is | |
99bee6a4 | 138 | based on the filename itself and `jka-compr-compression-info-list'." |
acd622cc RS |
139 | (catch 'compression-info |
140 | (let ((case-fold-search nil)) | |
141 | (mapcar | |
142 | (function (lambda (x) | |
143 | (and (string-match (jka-compr-info-regexp x) filename) | |
144 | (throw 'compression-info x)))) | |
145 | jka-compr-compression-info-list) | |
146 | nil))) | |
147 | ||
148 | ||
149 | (put 'compression-error 'error-conditions '(compression-error file-error error)) | |
150 | ||
151 | ||
30c78e11 | 152 | (defvar jka-compr-acceptable-retval-list '(0 2 141)) |
acd622cc RS |
153 | |
154 | ||
155 | (defun jka-compr-error (prog args infile message &optional errfile) | |
156 | ||
157 | (let ((errbuf (get-buffer-create " *jka-compr-error*")) | |
158 | (curbuf (current-buffer))) | |
75e9c107 RS |
159 | (with-current-buffer errbuf |
160 | (widen) (erase-buffer) | |
161 | (insert (format "Error while executing \"%s %s < %s\"\n\n" | |
162 | prog | |
163 | (mapconcat 'identity args " ") | |
164 | infile)) | |
165 | ||
166 | (and errfile | |
167 | (insert-file-contents errfile))) | |
acd622cc RS |
168 | (display-buffer errbuf)) |
169 | ||
75e9c107 RS |
170 | (signal 'compression-error |
171 | (list "Opening input file" (format "error %s" message) infile))) | |
f1180544 JB |
172 | |
173 | ||
242d2673 RS |
174 | (defcustom jka-compr-dd-program "/bin/dd" |
175 | "How to invoke `dd'." | |
176 | :type 'string | |
177 | :group 'jka-compr) | |
acd622cc RS |
178 | |
179 | ||
dfe05fac | 180 | (defvar jka-compr-dd-blocksize 256) |
acd622cc RS |
181 | |
182 | ||
183 | (defun jka-compr-partial-uncompress (prog message args infile beg len) | |
184 | "Call program PROG with ARGS args taking input from INFILE. | |
185 | Fourth and fifth args, BEG and LEN, specify which part of the output | |
ee139ed3 | 186 | to keep: LEN chars starting BEG chars from the beginning." |
242d2673 RS |
187 | (let ((start (point)) |
188 | (prefix beg)) | |
189 | (if (and jka-compr-use-shell jka-compr-dd-program) | |
190 | ;; Put the uncompression output through dd | |
191 | ;; to discard the part we don't want. | |
192 | (let ((skip (/ beg jka-compr-dd-blocksize)) | |
193 | (err-file (jka-compr-make-temp-name)) | |
194 | count) | |
195 | ;; Update PREFIX based on the text that we won't read in. | |
196 | (setq prefix (- beg (* skip jka-compr-dd-blocksize)) | |
197 | count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize)))) | |
198 | (unwind-protect | |
199 | (or (memq (call-process | |
200 | jka-compr-shell infile t nil "-c" | |
201 | (format | |
152e2693 | 202 | "%s %s 2> %s | %s bs=%d skip=%d %s 2> %s" |
242d2673 RS |
203 | prog |
204 | (mapconcat 'identity args " ") | |
205 | err-file | |
206 | jka-compr-dd-program | |
207 | jka-compr-dd-blocksize | |
208 | skip | |
209 | ;; dd seems to be unreliable about | |
210 | ;; providing the last block. So, always | |
211 | ;; read one more than you think you need. | |
152e2693 EZ |
212 | (if count (format "count=%d" (1+ count)) "") |
213 | null-device)) | |
242d2673 RS |
214 | jka-compr-acceptable-retval-list) |
215 | (jka-compr-error prog args infile message err-file)) | |
216 | (jka-compr-delete-temp-file err-file))) | |
217 | ;; Run the uncompression program directly. | |
218 | ;; We get the whole file and must delete what we don't want. | |
219 | (jka-compr-call-process prog message infile t nil args)) | |
acd622cc | 220 | |
ee139ed3 | 221 | ;; Delete the stuff after what we want, if there is any. |
acd622cc | 222 | (and |
dfe05fac | 223 | len |
ee139ed3 | 224 | (< (+ start prefix len) (point)) |
dfe05fac | 225 | (delete-region (+ start prefix len) (point))) |
acd622cc | 226 | |
ee139ed3 | 227 | ;; Delete the stuff before what we want. |
acd622cc RS |
228 | (delete-region start (+ start prefix)))) |
229 | ||
230 | ||
231 | (defun jka-compr-call-process (prog message infile output temp args) | |
232 | (if jka-compr-use-shell | |
233 | ||
a81635fc | 234 | (let ((err-file (jka-compr-make-temp-name)) |
baefb016 | 235 | (coding-system-for-read (or coding-system-for-read 'undecided)) |
70c7850e | 236 | (coding-system-for-write 'no-conversion)) |
a81635fc | 237 | |
acd622cc RS |
238 | (unwind-protect |
239 | ||
240 | (or (memq | |
241 | (call-process jka-compr-shell infile | |
242 | (if (stringp output) nil output) | |
243 | nil | |
244 | "-c" | |
245 | (format "%s %s 2> %s %s" | |
246 | prog | |
247 | (mapconcat 'identity args " ") | |
248 | err-file | |
249 | (if (stringp output) | |
250 | (concat "> " output) | |
251 | ""))) | |
252 | jka-compr-acceptable-retval-list) | |
253 | ||
254 | (jka-compr-error prog args infile message err-file)) | |
255 | ||
256 | (jka-compr-delete-temp-file err-file))) | |
257 | ||
15502042 | 258 | (or (eq 0 |
acd622cc RS |
259 | (apply 'call-process |
260 | prog | |
261 | infile | |
262 | (if (stringp output) temp output) | |
263 | nil | |
264 | args)) | |
265 | (jka-compr-error prog args infile message)) | |
266 | ||
267 | (and (stringp output) | |
75e9c107 | 268 | (with-current-buffer temp |
acd622cc | 269 | (write-region (point-min) (point-max) output) |
75e9c107 | 270 | (erase-buffer))))) |
acd622cc RS |
271 | |
272 | ||
273 | ;;; Support for temp files. Much of this was inspired if not lifted | |
274 | ;;; from ange-ftp. | |
275 | ||
bbf5eb28 | 276 | (defcustom jka-compr-temp-name-template |
11757e2f | 277 | (expand-file-name "jka-com" temporary-file-directory) |
362b539a | 278 | "Prefix added to all temp files created by jka-compr. |
bbf5eb28 RS |
279 | There should be no more than seven characters after the final `/'." |
280 | :type 'string | |
281 | :group 'jka-compr) | |
acd622cc | 282 | |
acd622cc RS |
283 | (defun jka-compr-make-temp-name (&optional local-copy) |
284 | "This routine will return the name of a new file." | |
767d12f2 SM |
285 | (make-temp-file jka-compr-temp-name-template)) |
286 | ||
287 | (defalias 'jka-compr-delete-temp-file 'delete-file) | |
acd622cc RS |
288 | |
289 | ||
290 | (defun jka-compr-write-region (start end file &optional append visit) | |
acd622cc RS |
291 | (let* ((filename (expand-file-name file)) |
292 | (visit-file (if (stringp visit) (expand-file-name visit) filename)) | |
e073a356 RS |
293 | (info (jka-compr-get-compression-info visit-file)) |
294 | (magic (and info (jka-compr-info-file-magic-bytes info)))) | |
295 | ||
b6e8d238 RS |
296 | ;; If START is nil, use the whole buffer. |
297 | (if (null start) | |
298 | (setq start 1 end (1+ (buffer-size)))) | |
299 | ||
e073a356 RS |
300 | ;; If we uncompressed this file when visiting it, |
301 | ;; then recompress it when writing it | |
302 | ;; even if the contents look compressed already. | |
303 | (if (and jka-compr-really-do-compress | |
304 | (eq start 1) | |
305 | (eq end (1+ (buffer-size)))) | |
306 | (setq magic nil)) | |
307 | ||
308 | (if (and info | |
309 | ;; If the contents to be written out | |
310 | ;; are properly compressed already, | |
311 | ;; don't try to compress them over again. | |
312 | (not (and magic | |
313 | (equal (if (stringp start) | |
314 | (substring start 0 (min (length start) | |
315 | (length magic))) | |
316 | (buffer-substring start | |
317 | (min end | |
318 | (+ start (length magic))))) | |
319 | magic)))) | |
320 | (let ((can-append (jka-compr-info-can-append info)) | |
321 | (compress-program (jka-compr-info-compress-program info)) | |
322 | (compress-message (jka-compr-info-compress-message info)) | |
e073a356 | 323 | (compress-args (jka-compr-info-compress-args info)) |
e073a356 RS |
324 | (base-name (file-name-nondirectory visit-file)) |
325 | temp-file temp-buffer | |
326 | ;; we need to leave `last-coding-system-used' set to its | |
327 | ;; value after calling write-region the first time, so | |
328 | ;; that `basic-save-buffer' sees the right value. | |
329 | (coding-system-used last-coding-system-used)) | |
330 | ||
cc8b577e JL |
331 | (or compress-program |
332 | (error "No compression program defined")) | |
333 | ||
e073a356 RS |
334 | (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*")) |
335 | (with-current-buffer temp-buffer | |
336 | (widen) (erase-buffer)) | |
337 | ||
338 | (if (and append | |
339 | (not can-append) | |
340 | (file-exists-p filename)) | |
341 | ||
342 | (let* ((local-copy (file-local-copy filename)) | |
343 | (local-file (or local-copy filename))) | |
344 | ||
345 | (setq temp-file local-file)) | |
346 | ||
347 | (setq temp-file (jka-compr-make-temp-name))) | |
348 | ||
f1180544 | 349 | (and |
e073a356 RS |
350 | compress-message |
351 | (message "%s %s..." compress-message base-name)) | |
352 | ||
353 | (jka-compr-run-real-handler 'write-region | |
354 | (list start end temp-file t 'dont)) | |
355 | ;; save value used by the real write-region | |
356 | (setq coding-system-used last-coding-system-used) | |
357 | ||
358 | ;; Here we must read the output of compress program as is | |
359 | ;; without any code conversion. | |
360 | (let ((coding-system-for-read 'no-conversion)) | |
361 | (jka-compr-call-process compress-program | |
362 | (concat compress-message | |
363 | " " base-name) | |
364 | temp-file | |
365 | temp-buffer | |
366 | nil | |
367 | compress-args)) | |
368 | ||
369 | (with-current-buffer temp-buffer | |
370 | (let ((coding-system-for-write 'no-conversion)) | |
371 | (if (memq system-type '(ms-dos windows-nt)) | |
372 | (setq buffer-file-type t) ) | |
373 | (jka-compr-run-real-handler 'write-region | |
374 | (list (point-min) (point-max) | |
375 | filename | |
376 | (and append can-append) 'dont)) | |
377 | (erase-buffer)) ) | |
378 | ||
379 | (jka-compr-delete-temp-file temp-file) | |
acd622cc | 380 | |
e073a356 RS |
381 | (and |
382 | compress-message | |
383 | (message "%s %s...done" compress-message base-name)) | |
384 | ||
385 | (cond | |
386 | ((eq visit t) | |
387 | (setq buffer-file-name filename) | |
388 | (setq jka-compr-really-do-compress t) | |
389 | (set-visited-file-modtime)) | |
390 | ((stringp visit) | |
391 | (setq buffer-file-name visit) | |
392 | (let ((buffer-file-name filename)) | |
393 | (set-visited-file-modtime)))) | |
394 | ||
395 | (and (or (eq visit t) | |
396 | (eq visit nil) | |
397 | (stringp visit)) | |
398 | (message "Wrote %s" visit-file)) | |
399 | ||
400 | ;; ensure `last-coding-system-used' has an appropriate value | |
401 | (setq last-coding-system-used coding-system-used) | |
402 | ||
403 | nil) | |
f1180544 | 404 | |
e073a356 RS |
405 | (jka-compr-run-real-handler 'write-region |
406 | (list start end filename append visit))))) | |
acd622cc RS |
407 | |
408 | ||
54b2aa5c | 409 | (defun jka-compr-insert-file-contents (file &optional visit beg end replace) |
acd622cc RS |
410 | (barf-if-buffer-read-only) |
411 | ||
412 | (and (or beg end) | |
413 | visit | |
414 | (error "Attempt to visit less than an entire file")) | |
415 | ||
416 | (let* ((filename (expand-file-name file)) | |
417 | (info (jka-compr-get-compression-info filename))) | |
418 | ||
419 | (if info | |
420 | ||
421 | (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
422 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
423 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
424 | (base-name (file-name-nondirectory filename)) | |
425 | (notfound nil) | |
8fb1a583 RS |
426 | (local-copy |
427 | (jka-compr-run-real-handler 'file-local-copy (list filename))) | |
acd622cc | 428 | local-file |
f54a7168 | 429 | size start) |
acd622cc RS |
430 | |
431 | (setq local-file (or local-copy filename)) | |
432 | ||
433 | (and | |
434 | visit | |
435 | (setq buffer-file-name filename)) | |
436 | ||
437 | (unwind-protect ; to make sure local-copy gets deleted | |
438 | ||
439 | (progn | |
f1180544 | 440 | |
acd622cc RS |
441 | (and |
442 | uncompress-message | |
443 | (message "%s %s..." uncompress-message base-name)) | |
444 | ||
445 | (condition-case error-code | |
446 | ||
f54a7168 | 447 | (let ((coding-system-for-read 'no-conversion)) |
094cf604 RS |
448 | (if replace |
449 | (goto-char (point-min))) | |
acd622cc RS |
450 | (setq start (point)) |
451 | (if (or beg end) | |
452 | (jka-compr-partial-uncompress uncompress-program | |
453 | (concat uncompress-message | |
454 | " " base-name) | |
455 | uncompress-args | |
456 | local-file | |
457 | (or beg 0) | |
458 | (if (and beg end) | |
459 | (- end beg) | |
460 | end)) | |
ae849784 RS |
461 | ;; If visiting, bind off buffer-file-name so that |
462 | ;; file-locking will not ask whether we should | |
463 | ;; really edit the buffer. | |
464 | (let ((buffer-file-name | |
465 | (if visit nil buffer-file-name))) | |
466 | (jka-compr-call-process uncompress-program | |
467 | (concat uncompress-message | |
468 | " " base-name) | |
469 | local-file | |
470 | t | |
471 | nil | |
472 | uncompress-args))) | |
acd622cc | 473 | (setq size (- (point) start)) |
094cf604 | 474 | (if replace |
31b55e80 | 475 | (delete-region (point) (point-max))) |
094cf604 | 476 | (goto-char start)) |
acd622cc | 477 | (error |
7bbae30c RS |
478 | ;; If the file we wanted to uncompress does not exist, |
479 | ;; handle that according to VISIT as `insert-file-contents' | |
480 | ;; would, maybe signaling the same error it normally would. | |
acd622cc RS |
481 | (if (and (eq (car error-code) 'file-error) |
482 | (eq (nth 3 error-code) local-file)) | |
483 | (if visit | |
484 | (setq notfound error-code) | |
f1180544 | 485 | (signal 'file-error |
acd622cc RS |
486 | (cons "Opening input file" |
487 | (nthcdr 2 error-code)))) | |
7bbae30c RS |
488 | ;; If the uncompression program can't be found, |
489 | ;; signal that as a non-file error | |
490 | ;; so that find-file-noselect-1 won't handle it. | |
491 | (if (and (eq (car error-code) 'file-error) | |
492 | (equal (cadr error-code) "Searching for program")) | |
493 | (error "Uncompression program `%s' not found" | |
494 | (nth 3 error-code))) | |
acd622cc RS |
495 | (signal (car error-code) (cdr error-code)))))) |
496 | ||
497 | (and | |
498 | local-copy | |
499 | (file-exists-p local-copy) | |
500 | (delete-file local-copy))) | |
501 | ||
8290faa3 AS |
502 | (unless notfound |
503 | (decode-coding-inserted-region | |
504 | (point) (+ (point) size) | |
505 | (jka-compr-byte-compiler-base-file-name file) | |
506 | visit beg end replace)) | |
f54a7168 | 507 | |
acd622cc RS |
508 | (and |
509 | visit | |
510 | (progn | |
8fb1a583 | 511 | (unlock-buffer) |
acd622cc | 512 | (setq buffer-file-name filename) |
e073a356 | 513 | (setq jka-compr-really-do-compress t) |
acd622cc | 514 | (set-visited-file-modtime))) |
f1180544 | 515 | |
acd622cc RS |
516 | (and |
517 | uncompress-message | |
518 | (message "%s %s...done" uncompress-message base-name)) | |
519 | ||
520 | (and | |
521 | visit | |
522 | notfound | |
523 | (signal 'file-error | |
524 | (cons "Opening input file" (nth 2 notfound)))) | |
525 | ||
5c6f2f2a RS |
526 | ;; This is done in insert-file-contents after we return. |
527 | ;; That is a little weird, but better to go along with it now | |
528 | ;; than to change it now. | |
529 | ||
530 | ;;; ;; Run the functions that insert-file-contents would. | |
531 | ;;; (let ((p after-insert-file-functions) | |
532 | ;;; (insval size)) | |
533 | ;;; (while p | |
534 | ;;; (setq insval (funcall (car p) size)) | |
535 | ;;; (if insval | |
536 | ;;; (progn | |
537 | ;;; (or (integerp insval) | |
538 | ;;; (signal 'wrong-type-argument | |
539 | ;;; (list 'integerp insval))) | |
540 | ;;; (setq size insval))) | |
541 | ;;; (setq p (cdr p)))) | |
094cf604 | 542 | |
cc8b577e JL |
543 | (or (jka-compr-info-compress-program info) |
544 | (message "You can't save this buffer because compression program is not defined")) | |
545 | ||
acd622cc RS |
546 | (list filename size)) |
547 | ||
8fb1a583 RS |
548 | (jka-compr-run-real-handler 'insert-file-contents |
549 | (list file visit beg end replace))))) | |
acd622cc RS |
550 | |
551 | ||
552 | (defun jka-compr-file-local-copy (file) | |
acd622cc RS |
553 | (let* ((filename (expand-file-name file)) |
554 | (info (jka-compr-get-compression-info filename))) | |
555 | ||
556 | (if info | |
557 | ||
558 | (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
559 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
560 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
561 | (base-name (file-name-nondirectory filename)) | |
8fb1a583 RS |
562 | (local-copy |
563 | (jka-compr-run-real-handler 'file-local-copy (list filename))) | |
acd622cc | 564 | (temp-file (jka-compr-make-temp-name t)) |
30c78e11 | 565 | (temp-buffer (get-buffer-create " *jka-compr-flc-temp*")) |
acd622cc | 566 | (notfound nil) |
acd622cc RS |
567 | local-file) |
568 | ||
569 | (setq local-file (or local-copy filename)) | |
570 | ||
571 | (unwind-protect | |
572 | ||
75e9c107 | 573 | (with-current-buffer temp-buffer |
f1180544 | 574 | |
acd622cc RS |
575 | (and |
576 | uncompress-message | |
577 | (message "%s %s..." uncompress-message base-name)) | |
f1180544 | 578 | |
baefb016 KH |
579 | ;; Here we must read the output of uncompress program |
580 | ;; and write it to TEMP-FILE without any code | |
581 | ;; conversion. An appropriate code conversion (if | |
582 | ;; necessary) is done by the later I/O operation | |
583 | ;; (e.g. load). | |
584 | (let ((coding-system-for-read 'no-conversion) | |
585 | (coding-system-for-write 'no-conversion)) | |
586 | ||
587 | (jka-compr-call-process uncompress-program | |
588 | (concat uncompress-message | |
589 | " " base-name) | |
590 | local-file | |
591 | t | |
592 | nil | |
593 | uncompress-args) | |
594 | ||
595 | (and | |
596 | uncompress-message | |
597 | (message "%s %s...done" uncompress-message base-name)) | |
598 | ||
599 | (write-region | |
600 | (point-min) (point-max) temp-file nil 'dont))) | |
acd622cc RS |
601 | |
602 | (and | |
603 | local-copy | |
604 | (file-exists-p local-copy) | |
605 | (delete-file local-copy)) | |
606 | ||
acd622cc RS |
607 | (kill-buffer temp-buffer)) |
608 | ||
609 | temp-file) | |
f1180544 | 610 | |
8fb1a583 | 611 | (jka-compr-run-real-handler 'file-local-copy (list filename))))) |
acd622cc RS |
612 | |
613 | ||
614 | ;;; Support for loading compressed files. | |
615 | (defun jka-compr-load (file &optional noerror nomessage nosuffix) | |
616 | "Documented as original." | |
617 | ||
618 | (let* ((local-copy (jka-compr-file-local-copy file)) | |
619 | (load-file (or local-copy file))) | |
620 | ||
621 | (unwind-protect | |
622 | ||
8fb1a583 RS |
623 | (let (inhibit-file-name-operation |
624 | inhibit-file-name-handlers) | |
acd622cc RS |
625 | (or nomessage |
626 | (message "Loading %s..." file)) | |
627 | ||
9b37d8a9 RS |
628 | (let ((load-force-doc-strings t)) |
629 | (load load-file noerror t t)) | |
acd622cc | 630 | (or nomessage |
e645e77b DL |
631 | (message "Loading %s...done." file)) |
632 | ;; Fix up the load history to point at the right library. | |
633 | (let ((l (assoc load-file load-history))) | |
634 | ;; Remove .gz and .elc?. | |
635 | (while (file-name-extension file) | |
636 | (setq file (file-name-sans-extension file))) | |
637 | (setcar l file))) | |
acd622cc | 638 | |
acd622cc RS |
639 | (jka-compr-delete-temp-file local-copy)) |
640 | ||
641 | t)) | |
3068998d RS |
642 | |
643 | (defun jka-compr-byte-compiler-base-file-name (file) | |
644 | (let ((info (jka-compr-get-compression-info file))) | |
645 | (if (and info (jka-compr-info-strip-extension info)) | |
646 | (save-match-data | |
647 | (substring file 0 (string-match (jka-compr-info-regexp info) file))) | |
648 | file))) | |
8fb1a583 RS |
649 | \f |
650 | (put 'write-region 'jka-compr 'jka-compr-write-region) | |
651 | (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents) | |
652 | (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy) | |
653 | (put 'load 'jka-compr 'jka-compr-load) | |
3068998d RS |
654 | (put 'byte-compiler-base-file-name 'jka-compr |
655 | 'jka-compr-byte-compiler-base-file-name) | |
acd622cc | 656 | |
0e2846fb | 657 | ;;;###autoload |
9fdf055b KH |
658 | (defvar jka-compr-inhibit nil |
659 | "Non-nil means inhibit automatic uncompression temporarily. | |
660 | Lisp programs can bind this to t to do that. | |
661 | It is not recommended to set this variable permanently to anything but nil.") | |
662 | ||
0e2846fb | 663 | ;;;###autoload |
acd622cc | 664 | (defun jka-compr-handler (operation &rest args) |
8fb1a583 RS |
665 | (save-match-data |
666 | (let ((jka-op (get operation 'jka-compr))) | |
9fdf055b | 667 | (if (and jka-op (not jka-compr-inhibit)) |
8fb1a583 RS |
668 | (apply jka-op args) |
669 | (jka-compr-run-real-handler operation args))))) | |
acd622cc | 670 | |
99bee6a4 RS |
671 | ;; If we are given an operation that we don't handle, |
672 | ;; call the Emacs primitive for that operation, | |
673 | ;; and manipulate the inhibit variables | |
674 | ;; to prevent the primitive from calling our handler again. | |
675 | (defun jka-compr-run-real-handler (operation args) | |
676 | (let ((inhibit-file-name-handlers | |
677 | (cons 'jka-compr-handler | |
678 | (and (eq inhibit-file-name-operation operation) | |
679 | inhibit-file-name-handlers))) | |
680 | (inhibit-file-name-operation operation)) | |
681 | (apply operation args))) | |
682 | ||
233c955d | 683 | ;;;###autoload |
acd622cc RS |
684 | (defun jka-compr-uninstall () |
685 | "Uninstall jka-compr. | |
99bee6a4 | 686 | This removes the entries in `file-name-handler-alist' and `auto-mode-alist' |
919a07bb RS |
687 | and `inhibit-first-line-modes-suffixes' that were added |
688 | by `jka-compr-installed'." | |
689 | ;; Delete from inhibit-first-line-modes-suffixes | |
690 | ;; what jka-compr-install added. | |
691 | (mapcar | |
692 | (function (lambda (x) | |
693 | (and (jka-compr-info-strip-extension x) | |
694 | (setq inhibit-first-line-modes-suffixes | |
695 | (delete (jka-compr-info-regexp x) | |
696 | inhibit-first-line-modes-suffixes))))) | |
697 | jka-compr-compression-info-list) | |
acd622cc RS |
698 | |
699 | (let* ((fnha (cons nil file-name-handler-alist)) | |
700 | (last fnha)) | |
701 | ||
702 | (while (cdr last) | |
703 | (if (eq (cdr (car (cdr last))) 'jka-compr-handler) | |
704 | (setcdr last (cdr (cdr last))) | |
705 | (setq last (cdr last)))) | |
706 | ||
707 | (setq file-name-handler-alist (cdr fnha))) | |
708 | ||
709 | (let* ((ama (cons nil auto-mode-alist)) | |
710 | (last ama) | |
711 | entry) | |
712 | ||
713 | (while (cdr last) | |
714 | (setq entry (car (cdr last))) | |
74b2c737 RS |
715 | (if (or (member entry jka-compr-mode-alist-additions) |
716 | (and (consp (cdr entry)) | |
717 | (eq (nth 2 entry) 'jka-compr))) | |
acd622cc RS |
718 | (setcdr last (cdr (cdr last))) |
719 | (setq last (cdr last)))) | |
f1180544 | 720 | |
4eec33ae RS |
721 | (setq auto-mode-alist (cdr ama))) |
722 | ||
723 | (let* ((ama (cons nil file-coding-system-alist)) | |
724 | (last ama) | |
725 | entry) | |
726 | ||
727 | (while (cdr last) | |
728 | (setq entry (car (cdr last))) | |
729 | (if (member entry jka-compr-added-to-file-coding-system-alist) | |
730 | (setcdr last (cdr (cdr last))) | |
731 | (setq last (cdr last)))) | |
f1180544 | 732 | |
aab8a6e3 SM |
733 | (setq file-coding-system-alist (cdr ama))) |
734 | ||
735 | ;; Remove the suffixes that were added by jka-compr. | |
736 | (let ((suffixes nil) | |
737 | (re (jka-compr-build-file-regexp))) | |
738 | (dolist (suffix load-suffixes) | |
739 | (unless (string-match re suffix) | |
740 | (push suffix suffixes))) | |
741 | (setq load-suffixes (nreverse suffixes)))) | |
acd622cc | 742 | |
acd622cc RS |
743 | (provide 'jka-compr) |
744 | ||
ab5796a9 | 745 | ;;; arch-tag: 3f15b630-e9a7-46c4-a22a-94afdde86ebc |
55535639 | 746 | ;;; jka-compr.el ends here |