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