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