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