Commit | Line | Data |
---|---|---|
acd622cc RS |
1 | ;;; jka-compr.el - reading/writing/loading compressed files. |
2 | ;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc. | |
3 | ||
4 | ;; Author: jka@ece.cmu.edu (Jay K. Adams) | |
dfe05fac | 5 | ;; Version: 0.11 |
acd622cc RS |
6 | ;; Keywords: data |
7 | ||
8 | ;;; Commentary: | |
9 | ||
10 | ;;; This package implements low-level support for reading, writing, | |
11 | ;;; and loading compressed files. It hooks into the low-level file | |
12 | ;;; I/O functions (including write-region and insert-file-contents) so | |
13 | ;;; that they automatically compress or uncompress a file if the file | |
14 | ;;; appears to need it (based on the extension of the file name). | |
dfe05fac | 15 | ;;; Packages like Rmail, VM, GNUS, and Info should be able to work |
acd622cc RS |
16 | ;;; with compressed files without modification. |
17 | ||
18 | ||
19 | ;;; INSTRUCTIONS: | |
20 | ;;; | |
21 | ;;; To use jka-compr, simply load this package, and edit as usual. | |
22 | ;;; Its operation should be transparent to the user (except for | |
23 | ;;; messages appearing when a file is being compressed or | |
24 | ;;; uncompressed). | |
25 | ;;; | |
26 | ;;; The variable, jka-compr-compression-info-list can be used to | |
27 | ;;; customize jka-compr to work with other compression programs. | |
28 | ;;; The default value of this variable allows jka-compr to work with | |
29 | ;;; Unix compress and gzip. | |
30 | ;;; | |
31 | ;;; If you are concerned about the stderr output of gzip and other | |
32 | ;;; compression/decompression programs showing up in your buffers, you | |
33 | ;;; should set the discard-error flag in the compression-info-list. | |
34 | ;;; This will cause the stderr of all programs to be discarded. | |
35 | ;;; However, it also causes emacs to call compression/uncompression | |
36 | ;;; programs through a shell (which is specified by jka-compr-shell). | |
37 | ;;; This may be a drag if, on your system, starting up a shell is | |
38 | ;;; slow. | |
39 | ;;; | |
40 | ;;; If you don't want messages about compressing and decompressing | |
41 | ;;; to show up in the echo area, you can set the compress-name and | |
42 | ;;; decompress-name fields of the jka-compr-compression-info-list to | |
43 | ;;; nil. | |
44 | ||
45 | ||
46 | ;;; APPLICATION NOTES: | |
47 | ;;; | |
48 | ;;; rmail, vm, gnus, etc. | |
49 | ;;; To use compressed mail folders, .newsrc files, etc., you need | |
50 | ;;; only compress the file. Since jka-compr searches for .gz | |
51 | ;;; versions of the files it's finding, you need not change | |
52 | ;;; variables within rmail, gnus, etc. | |
53 | ;;; | |
54 | ;;; | |
55 | ;;; crypt++ | |
56 | ;;; jka-compr can coexist with crpyt++ if you take all the decompression | |
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 | ;;; tar-mode | |
68 | ;;; Some people like to use extensions like .trz for compressed tar files. | |
69 | ;;; To handle these sorts of files, you have to add an entry to | |
70 | ;;; jka-compr-compression-info-list that looks something like this: | |
71 | ;;; | |
72 | ;;; ["\\.trz\\'" "\037\213" | |
73 | ;;; "zip" "gzip" nil ("-q") | |
74 | ;;; "unzip" "gzip" nil ("-q" "-d") | |
75 | ;;; t | |
76 | ;;; nil] | |
77 | ;;; | |
78 | ;;; The last nil in the vector (the "extension" field) prevents jka-compr | |
79 | ;;; from attempting to add .trz to an ordinary file name when it is looking | |
80 | ;;; for a compressed version of that file (i.e. don't look for things like | |
81 | ;;; foobar.c.trz). | |
82 | ;;; | |
83 | ;;; Finally, to make tar-mode start up automatically, you have to add an | |
84 | ;;; entry to auto-mode-alist that looks like this | |
85 | ;;; | |
86 | ;;; ("\\.trz\\'" . tar-mode) | |
87 | ;;; | |
88 | ||
89 | ||
90 | ;;; ACKNOWLEDGMENTS | |
91 | ;;; | |
92 | ;;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people | |
93 | ;;; have made helpful suggestions, reported bugs, and even fixed bugs in | |
94 | ;;; jka-compr. I recall the following people as being particularly helpful. | |
95 | ;;; | |
96 | ;;; Jean-loup Gailly | |
97 | ;;; David Hughes | |
98 | ;;; Richard Pieri | |
99 | ;;; Daniel Quinlan | |
100 | ;;; Chris P. Ross | |
101 | ;;; Rick Sladkey | |
102 | ;;; | |
103 | ;;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for | |
104 | ;;; Version 18 of Emacs. | |
105 | ;;; | |
106 | ;;; After I had made progress on the original jka-compr for V18, I learned of a | |
107 | ;;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly | |
108 | ;;; what I was trying to do. I looked over the jam-zcat source code and | |
109 | ;;; probably got some ideas from it. | |
110 | ;;; | |
111 | ||
112 | ;;; Code: | |
113 | ||
114 | (defvar jka-compr-shell "sh" | |
115 | "*Shell to be used for calling compression programs. | |
116 | The value of this variable only matters if you want to discard the | |
117 | stderr of a compression/decompression program (see the documentation | |
99bee6a4 | 118 | for `jka-compr-compression-info-list').") |
acd622cc RS |
119 | |
120 | ||
121 | (defvar jka-compr-use-shell t) | |
122 | ||
123 | ||
124 | ;;; I have this defined so that .Z files are assumed to be in unix | |
125 | ;;; compress format; and .gz files, in gzip format. | |
126 | (defvar jka-compr-compression-info-list | |
127 | ;;[regexp | |
128 | ;; compr-message compr-prog compr-discard compr-args | |
129 | ;; uncomp-message uncomp-prog uncomp-discard uncomp-args | |
130 | ;; can-append auto-mode-flag] | |
131 | '(["\\.Z~?\\'" | |
132 | "compressing" "compress" ("-c") | |
133 | "uncompressing" "uncompress" ("-c") | |
134 | nil t] | |
135 | ["\\.gz~?\\'" | |
136 | "zipping" "gzip" ("-c" "-q") | |
137 | "unzipping" "gzip" ("-c" "-q" "-d") | |
138 | t t]) | |
139 | ||
140 | "List of vectors that describe available compression techniques. | |
141 | Each element, which describes a compression technique, is a vector of | |
142 | the form [regexp magic compress-name compress-program compress-discard-err | |
143 | compress-args uncompress-name uncompress-program uncompress-discard-err | |
144 | uncompress-args append-flag extension] where: | |
145 | ||
146 | regexp is a regexp that matches filenames that are | |
147 | compressed with this format | |
148 | ||
149 | compress-program is a program that performs this compression | |
150 | ||
151 | compress-args is a list of args to pass to the compress program | |
152 | ||
153 | uncompress-message is the message to issue to the user when this | |
154 | type of uncompression is taking place (nil | |
155 | means don't issue any message) | |
156 | ||
157 | uncompress-program is a program that performs this compression | |
158 | ||
159 | uncompress-args is a list of args to pass to the uncompress program | |
160 | ||
161 | append-flag is non-nil if this compression technique can be | |
162 | appended | |
163 | ||
164 | auto-mode flag non-nil means strip the regexp from file names | |
165 | before attempting to set the mode. | |
166 | ||
167 | Because of the way call-process is defined, discarding the stderr output of | |
168 | a program adds the overhead of starting a shell each time the program is | |
169 | invoked.") | |
170 | ||
171 | ||
172 | ;;; Functions for accessing the return value of jka-get-compression-info | |
173 | (defun jka-compr-info-regexp (info) (aref info 0)) | |
174 | (defun jka-compr-info-compress-message (info) (aref info 1)) | |
175 | (defun jka-compr-info-compress-program (info) (aref info 2)) | |
176 | (defun jka-compr-info-compress-args (info) (aref info 3)) | |
177 | (defun jka-compr-info-uncompress-message (info) (aref info 4)) | |
178 | (defun jka-compr-info-uncompress-program (info) (aref info 5)) | |
179 | (defun jka-compr-info-uncompress-args (info) (aref info 6)) | |
180 | (defun jka-compr-info-can-append (info) (aref info 7)) | |
181 | (defun jka-compr-info-strip-extension (info) (aref info 8)) | |
182 | ||
183 | ||
184 | (defun jka-compr-get-compression-info (filename) | |
185 | "Return information about the compression scheme of FILENAME. | |
186 | The determination as to which compression scheme, if any, to use is | |
99bee6a4 | 187 | based on the filename itself and `jka-compr-compression-info-list'." |
acd622cc RS |
188 | (catch 'compression-info |
189 | (let ((case-fold-search nil)) | |
190 | (mapcar | |
191 | (function (lambda (x) | |
192 | (and (string-match (jka-compr-info-regexp x) filename) | |
193 | (throw 'compression-info x)))) | |
194 | jka-compr-compression-info-list) | |
195 | nil))) | |
196 | ||
197 | ||
198 | (put 'compression-error 'error-conditions '(compression-error file-error error)) | |
199 | ||
200 | ||
201 | (defvar jka-compr-acceptable-retval-list '(0 141)) | |
202 | ||
203 | ||
204 | (defun jka-compr-error (prog args infile message &optional errfile) | |
205 | ||
206 | (let ((errbuf (get-buffer-create " *jka-compr-error*")) | |
207 | (curbuf (current-buffer))) | |
208 | (set-buffer errbuf) | |
209 | (widen) (erase-buffer) | |
210 | (insert (format "Error while executing \"%s %s < %s\"\n\n" | |
211 | prog | |
212 | (mapconcat 'identity args " ") | |
213 | infile)) | |
214 | ||
215 | (and errfile | |
216 | (insert-file-contents errfile)) | |
217 | ||
218 | (set-buffer curbuf) | |
219 | (display-buffer errbuf)) | |
220 | ||
221 | (signal 'compression-error (list "Opening input file" (format "error %s" message) infile))) | |
222 | ||
223 | ||
224 | (defvar jka-compr-dd-program | |
225 | "/bin/dd") | |
226 | ||
227 | ||
dfe05fac | 228 | (defvar jka-compr-dd-blocksize 256) |
acd622cc RS |
229 | |
230 | ||
231 | (defun jka-compr-partial-uncompress (prog message args infile beg len) | |
232 | "Call program PROG with ARGS args taking input from INFILE. | |
233 | Fourth and fifth args, BEG and LEN, specify which part of the output | |
234 | to discard. All output is discarded unless it comes within LEN chars after | |
235 | the BEGth char." | |
236 | ||
237 | (let* ((skip (/ beg jka-compr-dd-blocksize)) | |
238 | (prefix (- beg (* skip jka-compr-dd-blocksize))) | |
239 | (count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize)))) | |
240 | (start (point)) | |
acd622cc RS |
241 | (err-file (jka-compr-make-temp-name)) |
242 | (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null" | |
243 | prog | |
244 | (mapconcat 'identity args " ") | |
245 | err-file | |
246 | jka-compr-dd-program | |
247 | jka-compr-dd-blocksize | |
248 | skip | |
dfe05fac RS |
249 | ;; dd seems to be unreliable about |
250 | ;; providing the last block. So, always | |
251 | ;; read one more than you think you need. | |
252 | (if count (concat "count=" (1+ count)) "")))) | |
acd622cc RS |
253 | |
254 | (unwind-protect | |
255 | (or (memq (call-process jka-compr-shell | |
256 | infile t nil "-c" | |
257 | run-string) | |
258 | jka-compr-acceptable-retval-list) | |
259 | ||
260 | (jka-compr-error prog args infile message err-file)) | |
261 | ||
262 | (jka-compr-delete-temp-file err-file)) | |
263 | ||
264 | (and | |
dfe05fac RS |
265 | len |
266 | (delete-region (+ start prefix len) (point))) | |
acd622cc RS |
267 | |
268 | (delete-region start (+ start prefix)))) | |
269 | ||
270 | ||
271 | (defun jka-compr-call-process (prog message infile output temp args) | |
272 | (if jka-compr-use-shell | |
273 | ||
274 | (let ((err-file (jka-compr-make-temp-name))) | |
275 | ||
276 | (unwind-protect | |
277 | ||
278 | (or (memq | |
279 | (call-process jka-compr-shell infile | |
280 | (if (stringp output) nil output) | |
281 | nil | |
282 | "-c" | |
283 | (format "%s %s 2> %s %s" | |
284 | prog | |
285 | (mapconcat 'identity args " ") | |
286 | err-file | |
287 | (if (stringp output) | |
288 | (concat "> " output) | |
289 | ""))) | |
290 | jka-compr-acceptable-retval-list) | |
291 | ||
292 | (jka-compr-error prog args infile message err-file)) | |
293 | ||
294 | (jka-compr-delete-temp-file err-file))) | |
295 | ||
296 | (or (zerop | |
297 | (apply 'call-process | |
298 | prog | |
299 | infile | |
300 | (if (stringp output) temp output) | |
301 | nil | |
302 | args)) | |
303 | (jka-compr-error prog args infile message)) | |
304 | ||
305 | (and (stringp output) | |
306 | (let ((cbuf (current-buffer))) | |
307 | (set-buffer temp) | |
308 | (write-region (point-min) (point-max) output) | |
309 | (erase-buffer) | |
310 | (set-buffer cbuf))))) | |
311 | ||
312 | ||
313 | ;;; Support for temp files. Much of this was inspired if not lifted | |
314 | ;;; from ange-ftp. | |
315 | ||
316 | (defvar jka-compr-temp-name-template | |
317 | "/tmp/jka-com" | |
318 | "Prefix added to all temp files created by jka-compr. | |
99bee6a4 | 319 | There should be no more than seven characters after the final `/'") |
acd622cc RS |
320 | |
321 | (defvar jka-compr-temp-name-table (make-vector 31 nil)) | |
322 | ||
323 | (defun jka-compr-make-temp-name (&optional local-copy) | |
324 | "This routine will return the name of a new file." | |
325 | (let* ((lastchar ?a) | |
326 | (prevchar ?a) | |
327 | (template (concat jka-compr-temp-name-template "aa")) | |
328 | (lastpos (1- (length template))) | |
329 | (not-done t) | |
330 | file | |
331 | entry) | |
332 | ||
333 | (while not-done | |
334 | (aset template lastpos lastchar) | |
335 | (setq file (concat (make-temp-name template) "#")) | |
336 | (setq entry (intern file jka-compr-temp-name-table)) | |
337 | (if (or (get entry 'active) | |
338 | (file-exists-p file)) | |
339 | ||
340 | (progn | |
341 | (setq lastchar (1+ lastchar)) | |
342 | (if (> lastchar ?z) | |
343 | (progn | |
344 | (setq prevchar (1+ prevchar)) | |
345 | (setq lastchar ?a) | |
346 | (if (> prevchar ?z) | |
347 | (error "Can't allocate temp file.") | |
348 | (aset template (1- lastpos) prevchar))))) | |
349 | ||
350 | (put entry 'active (not local-copy)) | |
351 | (setq not-done nil))) | |
352 | ||
353 | file)) | |
354 | ||
355 | ||
356 | (defun jka-compr-delete-temp-file (temp) | |
357 | ||
358 | (put (intern temp jka-compr-temp-name-table) | |
359 | 'active nil) | |
360 | ||
361 | (condition-case () | |
362 | (delete-file temp) | |
363 | (error nil))) | |
364 | ||
365 | ||
366 | (defun jka-compr-write-region (start end file &optional append visit) | |
367 | "Documented as original." | |
368 | (interactive "r\nFWrite region to file: ") | |
369 | ||
370 | (let* ((filename (expand-file-name file)) | |
371 | (visit-file (if (stringp visit) (expand-file-name visit) filename)) | |
372 | (info (jka-compr-get-compression-info visit-file))) | |
373 | ||
374 | (if info | |
375 | ||
376 | (let ((can-append (jka-compr-info-can-append info)) | |
377 | (compress-program (jka-compr-info-compress-program info)) | |
378 | (compress-message (jka-compr-info-compress-message info)) | |
379 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
380 | (uncompress-message (jka-compr-info-uncompress-message info)) | |
381 | (compress-args (jka-compr-info-compress-args info)) | |
382 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
383 | (temp-file (jka-compr-make-temp-name)) | |
384 | (base-name (file-name-nondirectory visit-file)) | |
385 | cbuf temp-buffer) | |
386 | ||
387 | (setq cbuf (current-buffer) | |
388 | temp-buffer (get-buffer-create " *jka-compr-temp*")) | |
389 | (set-buffer temp-buffer) | |
390 | (widen) (erase-buffer) | |
391 | (set-buffer cbuf) | |
392 | ||
393 | (and append | |
394 | (not can-append) | |
395 | (file-exists-p filename) | |
396 | (let* ((local-copy (file-local-copy filename)) | |
397 | (local-file (or local-copy filename))) | |
398 | ||
399 | (unwind-protect | |
400 | ||
401 | (progn | |
402 | ||
403 | (and | |
404 | uncompress-message | |
405 | (message "%s %s..." uncompress-message base-name)) | |
406 | ||
407 | (jka-compr-call-process uncompress-program | |
408 | (concat uncompress-message | |
409 | " " base-name) | |
410 | local-file | |
411 | temp-file | |
412 | temp-buffer | |
413 | uncompress-args) | |
414 | (and | |
415 | uncompress-message | |
416 | (message "%s %s...done" uncompress-message base-name))) | |
417 | ||
418 | (and | |
419 | local-copy | |
420 | (file-exists-p local-copy) | |
421 | (delete-file local-copy))))) | |
422 | ||
423 | (and | |
424 | compress-message | |
425 | (message "%s %s..." compress-message base-name)) | |
426 | ||
427 | (write-region start end temp-file t 'dont) | |
428 | ||
429 | (jka-compr-call-process compress-program | |
430 | (concat compress-message | |
431 | " " base-name) | |
432 | temp-file | |
433 | temp-buffer | |
434 | nil | |
435 | compress-args) | |
436 | ||
437 | (set-buffer temp-buffer) | |
438 | (write-region (point-min) (point-max) | |
439 | filename (and append can-append) 'dont) | |
440 | (erase-buffer) | |
441 | (set-buffer cbuf) | |
442 | ||
443 | (jka-compr-delete-temp-file temp-file) | |
444 | ||
445 | (and | |
446 | compress-message | |
447 | (message "%s %s...done" compress-message base-name)) | |
448 | ||
449 | (cond | |
450 | ((eq visit t) | |
451 | (setq buffer-file-name filename) | |
452 | (set-visited-file-modtime)) | |
453 | ((stringp visit) | |
454 | (setq buffer-file-name visit) | |
455 | (let ((buffer-file-name filename)) | |
456 | (set-visited-file-modtime)))) | |
457 | ||
458 | (and (or (eq visit t) | |
459 | (eq visit nil) | |
460 | (stringp visit)) | |
461 | (message "Wrote %s" visit-file)) | |
462 | ||
463 | nil) | |
464 | ||
465 | (write-region start end filename append visit)))) | |
466 | ||
467 | ||
54b2aa5c | 468 | (defun jka-compr-insert-file-contents (file &optional visit beg end replace) |
acd622cc RS |
469 | "Documented as original." |
470 | ||
471 | (barf-if-buffer-read-only) | |
472 | ||
473 | (and (or beg end) | |
474 | visit | |
475 | (error "Attempt to visit less than an entire file")) | |
476 | ||
477 | (let* ((filename (expand-file-name file)) | |
478 | (info (jka-compr-get-compression-info filename))) | |
479 | ||
480 | (if info | |
481 | ||
482 | (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
483 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
484 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
485 | (base-name (file-name-nondirectory filename)) | |
486 | (notfound nil) | |
487 | (local-copy (file-local-copy filename)) | |
488 | local-file | |
489 | size start) | |
490 | ||
491 | (setq local-file (or local-copy filename)) | |
492 | ||
493 | (and | |
494 | visit | |
495 | (setq buffer-file-name filename)) | |
496 | ||
497 | (unwind-protect ; to make sure local-copy gets deleted | |
498 | ||
499 | (progn | |
500 | ||
501 | (and | |
502 | uncompress-message | |
503 | (message "%s %s..." uncompress-message base-name)) | |
504 | ||
505 | (condition-case error-code | |
506 | ||
507 | (progn | |
508 | (setq start (point)) | |
509 | (if (or beg end) | |
510 | (jka-compr-partial-uncompress uncompress-program | |
511 | (concat uncompress-message | |
512 | " " base-name) | |
513 | uncompress-args | |
514 | local-file | |
515 | (or beg 0) | |
516 | (if (and beg end) | |
517 | (- end beg) | |
518 | end)) | |
519 | (jka-compr-call-process uncompress-program | |
520 | (concat uncompress-message | |
521 | " " base-name) | |
522 | local-file | |
523 | t | |
524 | nil | |
525 | uncompress-args)) | |
526 | (setq size (- (point) start)) | |
527 | (goto-char start)) | |
528 | ||
529 | ||
530 | (error | |
531 | (if (and (eq (car error-code) 'file-error) | |
532 | (eq (nth 3 error-code) local-file)) | |
533 | (if visit | |
534 | (setq notfound error-code) | |
535 | (signal 'file-error | |
536 | (cons "Opening input file" | |
537 | (nthcdr 2 error-code)))) | |
538 | (signal (car error-code) (cdr error-code)))))) | |
539 | ||
540 | (and | |
541 | local-copy | |
542 | (file-exists-p local-copy) | |
543 | (delete-file local-copy))) | |
544 | ||
545 | (and | |
546 | visit | |
547 | (progn | |
548 | (setq buffer-file-name filename) | |
549 | (set-visited-file-modtime))) | |
550 | ||
551 | (and | |
552 | uncompress-message | |
553 | (message "%s %s...done" uncompress-message base-name)) | |
554 | ||
555 | (and | |
556 | visit | |
557 | notfound | |
558 | (signal 'file-error | |
559 | (cons "Opening input file" (nth 2 notfound)))) | |
560 | ||
561 | (list filename size)) | |
562 | ||
54b2aa5c | 563 | (insert-file-contents file visit beg end replace)))) |
acd622cc RS |
564 | |
565 | ||
566 | (defun jka-compr-file-local-copy (file) | |
567 | "Documented as original." | |
568 | ||
569 | (let* ((filename (expand-file-name file)) | |
570 | (info (jka-compr-get-compression-info filename))) | |
571 | ||
572 | (if info | |
573 | ||
574 | (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
575 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
576 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
577 | (base-name (file-name-nondirectory filename)) | |
578 | (local-copy (file-local-copy filename)) | |
579 | (temp-file (jka-compr-make-temp-name t)) | |
580 | (temp-buffer (get-buffer-create " *jka-compr-temp*")) | |
581 | (notfound nil) | |
582 | (cbuf (current-buffer)) | |
583 | local-file) | |
584 | ||
585 | (setq local-file (or local-copy filename)) | |
586 | ||
587 | (unwind-protect | |
588 | ||
589 | (progn | |
590 | ||
591 | (and | |
592 | uncompress-message | |
593 | (message "%s %s..." uncompress-message base-name)) | |
594 | ||
595 | (set-buffer temp-buffer) | |
596 | ||
597 | (jka-compr-call-process uncompress-program | |
598 | (concat uncompress-message | |
599 | " " base-name) | |
600 | local-file | |
601 | t | |
602 | nil | |
603 | uncompress-args) | |
604 | ||
605 | (and | |
606 | uncompress-message | |
607 | (message "%s %s...done" uncompress-message base-name)) | |
608 | ||
609 | (write-region | |
610 | (point-min) (point-max) temp-file nil 'dont)) | |
611 | ||
612 | (and | |
613 | local-copy | |
614 | (file-exists-p local-copy) | |
615 | (delete-file local-copy)) | |
616 | ||
617 | (set-buffer cbuf) | |
618 | (kill-buffer temp-buffer)) | |
619 | ||
620 | temp-file) | |
621 | ||
622 | (file-local-copy filename)))) | |
623 | ||
624 | ||
625 | ;;; Support for loading compressed files. | |
626 | (defun jka-compr-load (file &optional noerror nomessage nosuffix) | |
627 | "Documented as original." | |
628 | ||
629 | (let* ((local-copy (jka-compr-file-local-copy file)) | |
630 | (load-file (or local-copy file))) | |
631 | ||
632 | (unwind-protect | |
633 | ||
634 | (progn | |
635 | ||
636 | (setq file-name-handler-alist | |
637 | (cons jka-compr-file-name-handler-entry | |
638 | file-name-handler-alist)) | |
639 | ||
640 | (or nomessage | |
641 | (message "Loading %s..." file)) | |
642 | ||
643 | (load load-file noerror t t) | |
644 | ||
645 | (or nomessage | |
646 | (message "Loading %s...done." file))) | |
647 | ||
648 | (setq file-name-handler-alist | |
649 | (delq jka-compr-file-name-handler-entry | |
650 | file-name-handler-alist)) | |
651 | ||
652 | (jka-compr-delete-temp-file local-copy)) | |
653 | ||
654 | t)) | |
655 | ||
656 | ||
657 | (defvar jka-compr-file-name-handler-entry | |
658 | nil | |
99bee6a4 | 659 | "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.") |
acd622cc RS |
660 | |
661 | ||
662 | (defun jka-compr-handler (operation &rest args) | |
663 | ||
1cb46df6 KH |
664 | (let ((jka-op (intern-soft (symbol-name operation) jka-compr-op-table)) |
665 | (match-data (match-data))) | |
acd622cc RS |
666 | |
667 | (unwind-protect | |
668 | (progn | |
669 | (setq file-name-handler-alist | |
670 | (delq jka-compr-file-name-handler-entry | |
671 | file-name-handler-alist)) | |
672 | (if jka-op | |
673 | (apply jka-op args) | |
99bee6a4 | 674 | (jka-compr-run-real-handler operation args))) |
acd622cc RS |
675 | |
676 | (setq file-name-handler-alist | |
677 | (cons jka-compr-file-name-handler-entry | |
1cb46df6 KH |
678 | file-name-handler-alist)) |
679 | (store-match-data match-data)))) | |
acd622cc | 680 | |
99bee6a4 RS |
681 | ;; If we are given an operation that we don't handle, |
682 | ;; call the Emacs primitive for that operation, | |
683 | ;; and manipulate the inhibit variables | |
684 | ;; to prevent the primitive from calling our handler again. | |
685 | (defun jka-compr-run-real-handler (operation args) | |
686 | (let ((inhibit-file-name-handlers | |
687 | (cons 'jka-compr-handler | |
688 | (and (eq inhibit-file-name-operation operation) | |
689 | inhibit-file-name-handlers))) | |
690 | (inhibit-file-name-operation operation)) | |
691 | (apply operation args))) | |
692 | ||
acd622cc RS |
693 | |
694 | (defvar jka-compr-op-table | |
695 | (make-vector 127 0) | |
dfe05fac | 696 | "Hash table of operations supported by jka-compr.") |
acd622cc RS |
697 | |
698 | ||
699 | (defun jka-compr-intern-operation (op) | |
700 | (let ((opsym (intern (symbol-name op) jka-compr-op-table)) | |
701 | (jka-fn (intern (concat "jka-compr-" (symbol-name op))))) | |
702 | (fset opsym jka-fn))) | |
703 | ||
704 | ||
705 | (defvar jka-compr-operation-list | |
706 | '( | |
707 | write-region | |
708 | insert-file-contents | |
709 | file-local-copy | |
710 | load | |
711 | ) | |
712 | "List of file operations implemented by jka-compr.") | |
713 | ||
714 | ||
715 | (mapcar | |
716 | (function | |
717 | (lambda (fn) | |
718 | (jka-compr-intern-operation fn))) | |
719 | jka-compr-operation-list) | |
720 | ||
721 | ||
722 | (defun toggle-auto-compression (arg) | |
723 | "Toggle automatic file compression and decompression. | |
724 | With prefix argument ARG, turn auto compression on if positive, else off. | |
725 | Returns the new status of auto compression (non-nil means on)." | |
726 | (interactive "P") | |
727 | (let* ((installed (jka-compr-installed-p)) | |
728 | (flag (if (null arg) | |
729 | (not installed) | |
730 | (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))))) | |
731 | ||
732 | (cond | |
733 | ((and flag installed) t) ; already installed | |
734 | ||
735 | ((and (not flag) (not installed)) nil) ; already not installed | |
736 | ||
737 | (flag | |
738 | (jka-compr-install)) | |
739 | ||
740 | (t | |
741 | (jka-compr-uninstall))) | |
742 | ||
743 | ||
744 | (and (interactive-p) | |
745 | (if flag | |
746 | (message "Automatic file (de)compression is now ON.") | |
747 | (message "Automatic file (de)compression is now OFF."))) | |
748 | ||
749 | flag)) | |
750 | ||
751 | ||
752 | (defun jka-compr-build-file-regexp () | |
753 | (concat | |
754 | "\\(" | |
755 | (mapconcat | |
756 | 'jka-compr-info-regexp | |
757 | jka-compr-compression-info-list | |
758 | "\\)\\|\\(") | |
759 | "\\)")) | |
760 | ||
761 | ||
762 | (defun jka-compr-install () | |
763 | "Install jka-compr. | |
99bee6a4 | 764 | This adds entries to `file-name-handler-alist' and `auto-mode-alist'." |
acd622cc RS |
765 | |
766 | (setq jka-compr-file-name-handler-entry | |
767 | (cons (jka-compr-build-file-regexp) 'jka-compr-handler)) | |
768 | ||
769 | (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry | |
770 | file-name-handler-alist)) | |
771 | ||
772 | (mapcar | |
773 | (function (lambda (x) | |
774 | (and | |
775 | (jka-compr-info-strip-extension x) | |
776 | (setq auto-mode-alist (cons (list (jka-compr-info-regexp x) | |
777 | nil 'jka-compr) | |
778 | auto-mode-alist))))) | |
779 | ||
780 | jka-compr-compression-info-list)) | |
781 | ||
782 | ||
783 | (defun jka-compr-uninstall () | |
784 | "Uninstall jka-compr. | |
99bee6a4 RS |
785 | This removes the entries in `file-name-handler-alist' and `auto-mode-alist' |
786 | that were created by `jka-compr-installed'." | |
acd622cc RS |
787 | |
788 | (let* ((fnha (cons nil file-name-handler-alist)) | |
789 | (last fnha)) | |
790 | ||
791 | (while (cdr last) | |
792 | (if (eq (cdr (car (cdr last))) 'jka-compr-handler) | |
793 | (setcdr last (cdr (cdr last))) | |
794 | (setq last (cdr last)))) | |
795 | ||
796 | (setq file-name-handler-alist (cdr fnha))) | |
797 | ||
798 | (let* ((ama (cons nil auto-mode-alist)) | |
799 | (last ama) | |
800 | entry) | |
801 | ||
802 | (while (cdr last) | |
803 | (setq entry (car (cdr last))) | |
804 | (if (and (consp (cdr entry)) | |
805 | (eq (nth 2 entry) 'jka-compr)) | |
806 | (setcdr last (cdr (cdr last))) | |
807 | (setq last (cdr last)))) | |
808 | ||
809 | (setq auto-mode-alist (cdr ama)))) | |
810 | ||
811 | ||
812 | (defun jka-compr-installed-p () | |
813 | "Return non-nil if jka-compr is installed. | |
99bee6a4 | 814 | The return value is the entry in `file-name-handler-alist' for jka-compr." |
acd622cc RS |
815 | |
816 | (let ((fnha file-name-handler-alist) | |
817 | (installed nil)) | |
818 | ||
819 | (while (and fnha (not installed)) | |
820 | (and (eq (cdr (car fnha)) 'jka-compr-handler) | |
821 | (setq installed (car fnha))) | |
822 | (setq fnha (cdr fnha))) | |
823 | ||
824 | installed)) | |
825 | ||
826 | ||
827 | ;;; Add the file I/O hook if it does not already exist. | |
828 | ;;; Make sure that jka-compr-file-name-handler-entry is eq to the | |
829 | ;;; entry for jka-compr in file-name-handler-alist. | |
830 | (and (jka-compr-installed-p) | |
831 | (jka-compr-uninstall)) | |
832 | ||
833 | (jka-compr-install) | |
834 | ||
835 | ||
836 | (provide 'jka-compr) | |
837 | ||
838 | ;; jka-compr.el ends here. |