]> code.delx.au - gnu-emacs/blob - lisp/jka-compr.el
* files.el (auto-save-file-name-transforms): Put remote files in
[gnu-emacs] / lisp / jka-compr.el
1 ;;; jka-compr.el --- reading/writing/loading compressed files
2
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000 Free Software Foundation, Inc.
4
5 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
6 ;; Maintainer: FSF
7 ;; Keywords: data
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This package implements low-level support for reading, writing,
29 ;; and loading compressed files. It hooks into the low-level file
30 ;; I/O functions (including write-region and insert-file-contents) so
31 ;; that they automatically compress or uncompress a file if the file
32 ;; appears to need it (based on the extension of the file name).
33 ;; Packages like Rmail, VM, GNUS, and Info should be able to work
34 ;; with compressed files without modification.
35
36
37 ;; INSTRUCTIONS:
38 ;;
39 ;; To use jka-compr, invoke the command `auto-compression-mode' (which
40 ;; see), or customize the variable of the same name. Its operation
41 ;; should be transparent to the user (except for messages appearing when
42 ;; a file is being compressed or uncompressed).
43 ;;
44 ;; The variable, jka-compr-compression-info-list can be used to
45 ;; customize jka-compr to work with other compression programs.
46 ;; The default value of this variable allows jka-compr to work with
47 ;; Unix compress and gzip.
48 ;;
49 ;; If you are concerned about the stderr output of gzip and other
50 ;; compression/decompression programs showing up in your buffers, you
51 ;; should set the discard-error flag in the compression-info-list.
52 ;; This will cause the stderr of all programs to be discarded.
53 ;; However, it also causes emacs to call compression/uncompression
54 ;; programs through a shell (which is specified by jka-compr-shell).
55 ;; This may be a drag if, on your system, starting up a shell is
56 ;; slow.
57 ;;
58 ;; If you don't want messages about compressing and decompressing
59 ;; to show up in the echo area, you can set the compress-name and
60 ;; decompress-name fields of the jka-compr-compression-info-list to
61 ;; nil.
62
63
64 ;; APPLICATION NOTES:
65 ;;
66 ;; crypt++
67 ;; jka-compr can coexist with crpyt++ if you take all the decompression
68 ;; entries out of the crypt-encoding-list. Clearly problems will arise if
69 ;; you have two programs trying to compress/decompress files. jka-compr
70 ;; will not "work with" crypt++ in the following sense: you won't be able to
71 ;; decode encrypted compressed files--that is, files that have been
72 ;; compressed then encrypted (in that order). Theoretically, crypt++ and
73 ;; jka-compr could properly handle a file that has been encrypted then
74 ;; compressed, but there is little point in trying to compress an encrypted
75 ;; file.
76 ;;
77
78
79 ;; ACKNOWLEDGMENTS
80 ;;
81 ;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people
82 ;; have made helpful suggestions, reported bugs, and even fixed bugs in
83 ;; jka-compr. I recall the following people as being particularly helpful.
84 ;;
85 ;; Jean-loup Gailly
86 ;; David Hughes
87 ;; Richard Pieri
88 ;; Daniel Quinlan
89 ;; Chris P. Ross
90 ;; Rick Sladkey
91 ;;
92 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
93 ;; Version 18 of Emacs.
94 ;;
95 ;; After I had made progress on the original jka-compr for V18, I learned of a
96 ;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly
97 ;; what I was trying to do. I looked over the jam-zcat source code and
98 ;; probably got some ideas from it.
99 ;;
100
101 ;;; Code:
102
103 (defgroup compression nil
104 "Data compression utilities"
105 :group 'data)
106
107 (defgroup jka-compr nil
108 "jka-compr customization"
109 :group 'compression)
110
111
112 (defcustom jka-compr-shell "sh"
113 "*Shell to be used for calling compression programs.
114 The value of this variable only matters if you want to discard the
115 stderr of a compression/decompression program (see the documentation
116 for `jka-compr-compression-info-list')."
117 :type 'string
118 :group 'jka-compr)
119
120 (defvar jka-compr-use-shell
121 (not (memq system-type '(ms-dos windows-nt))))
122
123 ;;; I have this defined so that .Z files are assumed to be in unix
124 ;;; compress format; and .gz files, in gzip format, and .bz2 files in bzip fmt.
125 (defcustom jka-compr-compression-info-list
126 ;;[regexp
127 ;; compr-message compr-prog compr-args
128 ;; uncomp-message uncomp-prog uncomp-args
129 ;; can-append auto-mode-flag strip-extension-flag file-magic-bytes]
130 '(["\\.Z\\(~\\|\\.~[0-9]+~\\)?\\'"
131 "compressing" "compress" ("-c")
132 "uncompressing" "uncompress" ("-c")
133 nil t "\037\235"]
134 ;; Formerly, these had an additional arg "-c", but that fails with
135 ;; "Version 0.1pl2, 29-Aug-97." (RedHat 5.1 GNU/Linux) and
136 ;; "Version 0.9.0b, 9-Sept-98".
137 ["\\.bz2\\'"
138 "bzip2ing" "bzip2" nil
139 "bunzip2ing" "bzip2" ("-d")
140 nil t "BZh"]
141 ["\\.tgz\\'"
142 "zipping" "gzip" ("-c" "-q")
143 "unzipping" "gzip" ("-c" "-q" "-d")
144 t nil "\037\213"]
145 ["\\.g?z\\(~\\|\\.~[0-9]+~\\)?\\'"
146 "zipping" "gzip" ("-c" "-q")
147 "unzipping" "gzip" ("-c" "-q" "-d")
148 t t "\037\213"])
149
150 "List of vectors that describe available compression techniques.
151 Each element, which describes a compression technique, is a vector of
152 the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS
153 UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS
154 APPEND-FLAG STRIP-EXTENSION-FLAG FILE-MAGIC-CHARS], where:
155
156 regexp is a regexp that matches filenames that are
157 compressed with this format
158
159 compress-msg is the message to issue to the user when doing this
160 type of compression (nil means no message)
161
162 compress-program is a program that performs this compression
163
164 compress-args is a list of args to pass to the compress program
165
166 uncompress-msg is the message to issue to the user when doing this
167 type of uncompression (nil means no message)
168
169 uncompress-program is a program that performs this compression
170
171 uncompress-args is a list of args to pass to the uncompress program
172
173 append-flag is non-nil if this compression technique can be
174 appended
175
176 strip-extension-flag non-nil means strip the regexp from file names
177 before attempting to set the mode.
178
179 file-magic-chars is a string of characters that you would find
180 at the beginning of a file compressed in this way.
181
182 Because of the way `call-process' is defined, discarding the stderr output of
183 a program adds the overhead of starting a shell each time the program is
184 invoked."
185 :type '(repeat (vector regexp
186 (choice :tag "Compress Message"
187 (string :format "%v")
188 (const :tag "No Message" nil))
189 (string :tag "Compress Program")
190 (repeat :tag "Compress Arguments" string)
191 (choice :tag "Uncompress Message"
192 (string :format "%v")
193 (const :tag "No Message" nil))
194 (string :tag "Uncompress Program")
195 (repeat :tag "Uncompress Arguments" string)
196 (boolean :tag "Append")
197 (boolean :tag "Strip Extension")
198 (string :tag "Magic Bytes")))
199 :group 'jka-compr)
200
201 (defcustom jka-compr-mode-alist-additions
202 (list (cons "\\.tgz\\'" 'tar-mode))
203 "A list of pairs to add to `auto-mode-alist' when jka-compr is installed."
204 :type '(repeat (cons string symbol))
205 :group 'jka-compr)
206
207 (defcustom jka-compr-load-suffixes '(".gz")
208 "List of suffixes to try when loading files."
209 :type '(repeat string)
210 :group 'jka-compr)
211
212 ;; List of all the elements we actually added to file-coding-system-alist.
213 (defvar jka-compr-added-to-file-coding-system-alist nil)
214
215 (defvar jka-compr-file-name-handler-entry
216 nil
217 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.")
218
219 (defvar jka-compr-really-do-compress nil
220 "Non-nil in a buffer whose visited file was uncompressed on visiting it.")
221 (put 'jka-compr-really-do-compress 'permanent-local t)
222 \f
223 ;;; Functions for accessing the return value of jka-compr-get-compression-info
224 (defun jka-compr-info-regexp (info) (aref info 0))
225 (defun jka-compr-info-compress-message (info) (aref info 1))
226 (defun jka-compr-info-compress-program (info) (aref info 2))
227 (defun jka-compr-info-compress-args (info) (aref info 3))
228 (defun jka-compr-info-uncompress-message (info) (aref info 4))
229 (defun jka-compr-info-uncompress-program (info) (aref info 5))
230 (defun jka-compr-info-uncompress-args (info) (aref info 6))
231 (defun jka-compr-info-can-append (info) (aref info 7))
232 (defun jka-compr-info-strip-extension (info) (aref info 8))
233 (defun jka-compr-info-file-magic-bytes (info) (aref info 9))
234
235
236 (defun jka-compr-get-compression-info (filename)
237 "Return information about the compression scheme of FILENAME.
238 The determination as to which compression scheme, if any, to use is
239 based on the filename itself and `jka-compr-compression-info-list'."
240 (catch 'compression-info
241 (let ((case-fold-search nil))
242 (mapcar
243 (function (lambda (x)
244 (and (string-match (jka-compr-info-regexp x) filename)
245 (throw 'compression-info x))))
246 jka-compr-compression-info-list)
247 nil)))
248
249
250 (put 'compression-error 'error-conditions '(compression-error file-error error))
251
252
253 (defvar jka-compr-acceptable-retval-list '(0 2 141))
254
255
256 (defun jka-compr-error (prog args infile message &optional errfile)
257
258 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
259 (curbuf (current-buffer)))
260 (with-current-buffer errbuf
261 (widen) (erase-buffer)
262 (insert (format "Error while executing \"%s %s < %s\"\n\n"
263 prog
264 (mapconcat 'identity args " ")
265 infile))
266
267 (and errfile
268 (insert-file-contents errfile)))
269 (display-buffer errbuf))
270
271 (signal 'compression-error
272 (list "Opening input file" (format "error %s" message) infile)))
273
274
275 (defcustom jka-compr-dd-program "/bin/dd"
276 "How to invoke `dd'."
277 :type 'string
278 :group 'jka-compr)
279
280
281 (defvar jka-compr-dd-blocksize 256)
282
283
284 (defun jka-compr-partial-uncompress (prog message args infile beg len)
285 "Call program PROG with ARGS args taking input from INFILE.
286 Fourth and fifth args, BEG and LEN, specify which part of the output
287 to keep: LEN chars starting BEG chars from the beginning."
288 (let ((start (point))
289 (prefix beg))
290 (if (and jka-compr-use-shell jka-compr-dd-program)
291 ;; Put the uncompression output through dd
292 ;; to discard the part we don't want.
293 (let ((skip (/ beg jka-compr-dd-blocksize))
294 (err-file (jka-compr-make-temp-name))
295 count)
296 ;; Update PREFIX based on the text that we won't read in.
297 (setq prefix (- beg (* skip jka-compr-dd-blocksize))
298 count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize))))
299 (unwind-protect
300 (or (memq (call-process
301 jka-compr-shell infile t nil "-c"
302 (format
303 "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null"
304 prog
305 (mapconcat 'identity args " ")
306 err-file
307 jka-compr-dd-program
308 jka-compr-dd-blocksize
309 skip
310 ;; dd seems to be unreliable about
311 ;; providing the last block. So, always
312 ;; read one more than you think you need.
313 (if count (format "count=%d" (1+ count)) "")))
314 jka-compr-acceptable-retval-list)
315 (jka-compr-error prog args infile message err-file))
316 (jka-compr-delete-temp-file err-file)))
317 ;; Run the uncompression program directly.
318 ;; We get the whole file and must delete what we don't want.
319 (jka-compr-call-process prog message infile t nil args))
320
321 ;; Delete the stuff after what we want, if there is any.
322 (and
323 len
324 (< (+ start prefix len) (point))
325 (delete-region (+ start prefix len) (point)))
326
327 ;; Delete the stuff before what we want.
328 (delete-region start (+ start prefix))))
329
330
331 (defun jka-compr-call-process (prog message infile output temp args)
332 (if jka-compr-use-shell
333
334 (let ((err-file (jka-compr-make-temp-name))
335 (coding-system-for-read (or coding-system-for-read 'undecided))
336 (coding-system-for-write 'no-conversion))
337
338 (unwind-protect
339
340 (or (memq
341 (call-process jka-compr-shell infile
342 (if (stringp output) nil output)
343 nil
344 "-c"
345 (format "%s %s 2> %s %s"
346 prog
347 (mapconcat 'identity args " ")
348 err-file
349 (if (stringp output)
350 (concat "> " output)
351 "")))
352 jka-compr-acceptable-retval-list)
353
354 (jka-compr-error prog args infile message err-file))
355
356 (jka-compr-delete-temp-file err-file)))
357
358 (or (zerop
359 (apply 'call-process
360 prog
361 infile
362 (if (stringp output) temp output)
363 nil
364 args))
365 (jka-compr-error prog args infile message))
366
367 (and (stringp output)
368 (with-current-buffer temp
369 (write-region (point-min) (point-max) output)
370 (erase-buffer)))))
371
372
373 ;;; Support for temp files. Much of this was inspired if not lifted
374 ;;; from ange-ftp.
375
376 (defcustom jka-compr-temp-name-template
377 (expand-file-name "jka-com" temporary-file-directory)
378 "Prefix added to all temp files created by jka-compr.
379 There should be no more than seven characters after the final `/'."
380 :type 'string
381 :group 'jka-compr)
382
383 (defun jka-compr-make-temp-name (&optional local-copy)
384 "This routine will return the name of a new file."
385 (make-temp-file jka-compr-temp-name-template))
386
387 (defalias 'jka-compr-delete-temp-file 'delete-file)
388
389
390 (defun jka-compr-write-region (start end file &optional append visit)
391 (let* ((filename (expand-file-name file))
392 (visit-file (if (stringp visit) (expand-file-name visit) filename))
393 (info (jka-compr-get-compression-info visit-file))
394 (magic (and info (jka-compr-info-file-magic-bytes info))))
395
396 ;; If we uncompressed this file when visiting it,
397 ;; then recompress it when writing it
398 ;; even if the contents look compressed already.
399 (if (and jka-compr-really-do-compress
400 (eq start 1)
401 (eq end (1+ (buffer-size))))
402 (setq magic nil))
403
404 (if (and info
405 ;; If the contents to be written out
406 ;; are properly compressed already,
407 ;; don't try to compress them over again.
408 (not (and magic
409 (equal (if (stringp start)
410 (substring start 0 (min (length start)
411 (length magic)))
412 (buffer-substring start
413 (min end
414 (+ start (length magic)))))
415 magic))))
416 (let ((can-append (jka-compr-info-can-append info))
417 (compress-program (jka-compr-info-compress-program info))
418 (compress-message (jka-compr-info-compress-message info))
419 (uncompress-program (jka-compr-info-uncompress-program info))
420 (uncompress-message (jka-compr-info-uncompress-message info))
421 (compress-args (jka-compr-info-compress-args info))
422 (uncompress-args (jka-compr-info-uncompress-args info))
423 (base-name (file-name-nondirectory visit-file))
424 temp-file temp-buffer
425 ;; we need to leave `last-coding-system-used' set to its
426 ;; value after calling write-region the first time, so
427 ;; that `basic-save-buffer' sees the right value.
428 (coding-system-used last-coding-system-used))
429
430 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
431 (with-current-buffer temp-buffer
432 (widen) (erase-buffer))
433
434 (if (and append
435 (not can-append)
436 (file-exists-p filename))
437
438 (let* ((local-copy (file-local-copy filename))
439 (local-file (or local-copy filename)))
440
441 (setq temp-file local-file))
442
443 (setq temp-file (jka-compr-make-temp-name)))
444
445 (and
446 compress-message
447 (message "%s %s..." compress-message base-name))
448
449 (jka-compr-run-real-handler 'write-region
450 (list start end temp-file t 'dont))
451 ;; save value used by the real write-region
452 (setq coding-system-used last-coding-system-used)
453
454 ;; Here we must read the output of compress program as is
455 ;; without any code conversion.
456 (let ((coding-system-for-read 'no-conversion))
457 (jka-compr-call-process compress-program
458 (concat compress-message
459 " " base-name)
460 temp-file
461 temp-buffer
462 nil
463 compress-args))
464
465 (with-current-buffer temp-buffer
466 (let ((coding-system-for-write 'no-conversion))
467 (if (memq system-type '(ms-dos windows-nt))
468 (setq buffer-file-type t) )
469 (jka-compr-run-real-handler 'write-region
470 (list (point-min) (point-max)
471 filename
472 (and append can-append) 'dont))
473 (erase-buffer)) )
474
475 (jka-compr-delete-temp-file temp-file)
476
477 (and
478 compress-message
479 (message "%s %s...done" compress-message base-name))
480
481 (cond
482 ((eq visit t)
483 (setq buffer-file-name filename)
484 (setq jka-compr-really-do-compress t)
485 (set-visited-file-modtime))
486 ((stringp visit)
487 (setq buffer-file-name visit)
488 (let ((buffer-file-name filename))
489 (set-visited-file-modtime))))
490
491 (and (or (eq visit t)
492 (eq visit nil)
493 (stringp visit))
494 (message "Wrote %s" visit-file))
495
496 ;; ensure `last-coding-system-used' has an appropriate value
497 (setq last-coding-system-used coding-system-used)
498
499 nil)
500
501 (jka-compr-run-real-handler 'write-region
502 (list start end filename append visit)))))
503
504
505 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
506 (barf-if-buffer-read-only)
507
508 (and (or beg end)
509 visit
510 (error "Attempt to visit less than an entire file"))
511
512 (let* ((filename (expand-file-name file))
513 (info (jka-compr-get-compression-info filename)))
514
515 (if info
516
517 (let ((uncompress-message (jka-compr-info-uncompress-message info))
518 (uncompress-program (jka-compr-info-uncompress-program info))
519 (uncompress-args (jka-compr-info-uncompress-args info))
520 (base-name (file-name-nondirectory filename))
521 (notfound nil)
522 (local-copy
523 (jka-compr-run-real-handler 'file-local-copy (list filename)))
524 local-file
525 size start
526 (coding-system-for-read
527 (or coding-system-for-read
528 ;; If multibyte characters are disabled,
529 ;; don't do that conversion.
530 (and (null enable-multibyte-characters)
531 (or (auto-coding-alist-lookup
532 (jka-compr-byte-compiler-base-file-name file))
533 'raw-text))
534 (let ((coding (find-operation-coding-system
535 'insert-file-contents
536 (jka-compr-byte-compiler-base-file-name file))))
537 (and (consp coding) (car coding)))
538 'undecided)) )
539
540 (setq local-file (or local-copy filename))
541
542 (and
543 visit
544 (setq buffer-file-name filename))
545
546 (unwind-protect ; to make sure local-copy gets deleted
547
548 (progn
549
550 (and
551 uncompress-message
552 (message "%s %s..." uncompress-message base-name))
553
554 (condition-case error-code
555
556 (progn
557 (if replace
558 (goto-char (point-min)))
559 (setq start (point))
560 (if (or beg end)
561 (jka-compr-partial-uncompress uncompress-program
562 (concat uncompress-message
563 " " base-name)
564 uncompress-args
565 local-file
566 (or beg 0)
567 (if (and beg end)
568 (- end beg)
569 end))
570 ;; If visiting, bind off buffer-file-name so that
571 ;; file-locking will not ask whether we should
572 ;; really edit the buffer.
573 (let ((buffer-file-name
574 (if visit nil buffer-file-name)))
575 (jka-compr-call-process uncompress-program
576 (concat uncompress-message
577 " " base-name)
578 local-file
579 t
580 nil
581 uncompress-args)))
582 (setq size (- (point) start))
583 (if replace
584 (let* ((del-beg (point))
585 (del-end (+ del-beg size)))
586 (delete-region del-beg
587 (min del-end (point-max)))))
588 (goto-char start))
589 (error
590 (if (and (eq (car error-code) 'file-error)
591 (eq (nth 3 error-code) local-file))
592 (if visit
593 (setq notfound error-code)
594 (signal 'file-error
595 (cons "Opening input file"
596 (nthcdr 2 error-code))))
597 (signal (car error-code) (cdr error-code))))))
598
599 (and
600 local-copy
601 (file-exists-p local-copy)
602 (delete-file local-copy)))
603
604 (and
605 visit
606 (progn
607 (unlock-buffer)
608 (setq buffer-file-name filename)
609 (setq jka-compr-really-do-compress t)
610 (set-visited-file-modtime)))
611
612 (and
613 uncompress-message
614 (message "%s %s...done" uncompress-message base-name))
615
616 (and
617 visit
618 notfound
619 (signal 'file-error
620 (cons "Opening input file" (nth 2 notfound))))
621
622 ;; This is done in insert-file-contents after we return.
623 ;; That is a little weird, but better to go along with it now
624 ;; than to change it now.
625
626 ;;; ;; Run the functions that insert-file-contents would.
627 ;;; (let ((p after-insert-file-functions)
628 ;;; (insval size))
629 ;;; (while p
630 ;;; (setq insval (funcall (car p) size))
631 ;;; (if insval
632 ;;; (progn
633 ;;; (or (integerp insval)
634 ;;; (signal 'wrong-type-argument
635 ;;; (list 'integerp insval)))
636 ;;; (setq size insval)))
637 ;;; (setq p (cdr p))))
638
639 (list filename size))
640
641 (jka-compr-run-real-handler 'insert-file-contents
642 (list file visit beg end replace)))))
643
644
645 (defun jka-compr-file-local-copy (file)
646 (let* ((filename (expand-file-name file))
647 (info (jka-compr-get-compression-info filename)))
648
649 (if info
650
651 (let ((uncompress-message (jka-compr-info-uncompress-message info))
652 (uncompress-program (jka-compr-info-uncompress-program info))
653 (uncompress-args (jka-compr-info-uncompress-args info))
654 (base-name (file-name-nondirectory filename))
655 (local-copy
656 (jka-compr-run-real-handler 'file-local-copy (list filename)))
657 (temp-file (jka-compr-make-temp-name t))
658 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
659 (notfound nil)
660 local-file)
661
662 (setq local-file (or local-copy filename))
663
664 (unwind-protect
665
666 (with-current-buffer temp-buffer
667
668 (and
669 uncompress-message
670 (message "%s %s..." uncompress-message base-name))
671
672 ;; Here we must read the output of uncompress program
673 ;; and write it to TEMP-FILE without any code
674 ;; conversion. An appropriate code conversion (if
675 ;; necessary) is done by the later I/O operation
676 ;; (e.g. load).
677 (let ((coding-system-for-read 'no-conversion)
678 (coding-system-for-write 'no-conversion))
679
680 (jka-compr-call-process uncompress-program
681 (concat uncompress-message
682 " " base-name)
683 local-file
684 t
685 nil
686 uncompress-args)
687
688 (and
689 uncompress-message
690 (message "%s %s...done" uncompress-message base-name))
691
692 (write-region
693 (point-min) (point-max) temp-file nil 'dont)))
694
695 (and
696 local-copy
697 (file-exists-p local-copy)
698 (delete-file local-copy))
699
700 (kill-buffer temp-buffer))
701
702 temp-file)
703
704 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
705
706
707 ;;; Support for loading compressed files.
708 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
709 "Documented as original."
710
711 (let* ((local-copy (jka-compr-file-local-copy file))
712 (load-file (or local-copy file)))
713
714 (unwind-protect
715
716 (let (inhibit-file-name-operation
717 inhibit-file-name-handlers)
718 (or nomessage
719 (message "Loading %s..." file))
720
721 (let ((load-force-doc-strings t))
722 (load load-file noerror t t))
723 (or nomessage
724 (message "Loading %s...done." file))
725 ;; Fix up the load history to point at the right library.
726 (let ((l (assoc load-file load-history)))
727 ;; Remove .gz and .elc?.
728 (while (file-name-extension file)
729 (setq file (file-name-sans-extension file)))
730 (setcar l file)))
731
732 (jka-compr-delete-temp-file local-copy))
733
734 t))
735
736 (defun jka-compr-byte-compiler-base-file-name (file)
737 (let ((info (jka-compr-get-compression-info file)))
738 (if (and info (jka-compr-info-strip-extension info))
739 (save-match-data
740 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
741 file)))
742 \f
743 (put 'write-region 'jka-compr 'jka-compr-write-region)
744 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
745 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
746 (put 'load 'jka-compr 'jka-compr-load)
747 (put 'byte-compiler-base-file-name 'jka-compr
748 'jka-compr-byte-compiler-base-file-name)
749
750 (defvar jka-compr-inhibit nil
751 "Non-nil means inhibit automatic uncompression temporarily.
752 Lisp programs can bind this to t to do that.
753 It is not recommended to set this variable permanently to anything but nil.")
754
755 (defun jka-compr-handler (operation &rest args)
756 (save-match-data
757 (let ((jka-op (get operation 'jka-compr)))
758 (if (and jka-op (not jka-compr-inhibit))
759 (apply jka-op args)
760 (jka-compr-run-real-handler operation args)))))
761
762 ;; If we are given an operation that we don't handle,
763 ;; call the Emacs primitive for that operation,
764 ;; and manipulate the inhibit variables
765 ;; to prevent the primitive from calling our handler again.
766 (defun jka-compr-run-real-handler (operation args)
767 (let ((inhibit-file-name-handlers
768 (cons 'jka-compr-handler
769 (and (eq inhibit-file-name-operation operation)
770 inhibit-file-name-handlers)))
771 (inhibit-file-name-operation operation))
772 (apply operation args)))
773
774
775 (defun jka-compr-build-file-regexp ()
776 (mapconcat
777 'jka-compr-info-regexp
778 jka-compr-compression-info-list
779 "\\|"))
780
781
782 (defun jka-compr-install ()
783 "Install jka-compr.
784 This adds entries to `file-name-handler-alist' and `auto-mode-alist'
785 and `inhibit-first-line-modes-suffixes'."
786
787 (setq jka-compr-file-name-handler-entry
788 (cons (jka-compr-build-file-regexp) 'jka-compr-handler))
789
790 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry
791 file-name-handler-alist))
792
793 (setq jka-compr-added-to-file-coding-system-alist nil)
794
795 (mapcar
796 (function (lambda (x)
797 ;; Don't do multibyte encoding on the compressed files.
798 (let ((elt (cons (jka-compr-info-regexp x)
799 '(no-conversion . no-conversion))))
800 (setq file-coding-system-alist
801 (cons elt file-coding-system-alist))
802 (setq jka-compr-added-to-file-coding-system-alist
803 (cons elt jka-compr-added-to-file-coding-system-alist)))
804
805 (and (jka-compr-info-strip-extension x)
806 ;; Make entries in auto-mode-alist so that modes
807 ;; are chosen right according to the file names
808 ;; sans `.gz'.
809 (setq auto-mode-alist
810 (cons (list (jka-compr-info-regexp x)
811 nil 'jka-compr)
812 auto-mode-alist))
813 ;; Also add these regexps to
814 ;; inhibit-first-line-modes-suffixes, so that a
815 ;; -*- line in the first file of a compressed tar
816 ;; file doesn't override tar-mode.
817 (setq inhibit-first-line-modes-suffixes
818 (cons (jka-compr-info-regexp x)
819 inhibit-first-line-modes-suffixes)))))
820 jka-compr-compression-info-list)
821 (setq auto-mode-alist
822 (append auto-mode-alist jka-compr-mode-alist-additions))
823
824 ;; Make sure that (load "foo") will find /bla/foo.el.gz.
825 (setq load-suffixes
826 (apply 'append
827 (mapcar (lambda (suffix)
828 (cons suffix
829 (mapcar (lambda (ext) (concat suffix ext))
830 jka-compr-load-suffixes)))
831 load-suffixes))))
832
833
834 (defun jka-compr-uninstall ()
835 "Uninstall jka-compr.
836 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
837 and `inhibit-first-line-modes-suffixes' that were added
838 by `jka-compr-installed'."
839 ;; Delete from inhibit-first-line-modes-suffixes
840 ;; what jka-compr-install added.
841 (mapcar
842 (function (lambda (x)
843 (and (jka-compr-info-strip-extension x)
844 (setq inhibit-first-line-modes-suffixes
845 (delete (jka-compr-info-regexp x)
846 inhibit-first-line-modes-suffixes)))))
847 jka-compr-compression-info-list)
848
849 (let* ((fnha (cons nil file-name-handler-alist))
850 (last fnha))
851
852 (while (cdr last)
853 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
854 (setcdr last (cdr (cdr last)))
855 (setq last (cdr last))))
856
857 (setq file-name-handler-alist (cdr fnha)))
858
859 (let* ((ama (cons nil auto-mode-alist))
860 (last ama)
861 entry)
862
863 (while (cdr last)
864 (setq entry (car (cdr last)))
865 (if (or (member entry jka-compr-mode-alist-additions)
866 (and (consp (cdr entry))
867 (eq (nth 2 entry) 'jka-compr)))
868 (setcdr last (cdr (cdr last)))
869 (setq last (cdr last))))
870
871 (setq auto-mode-alist (cdr ama)))
872
873 (let* ((ama (cons nil file-coding-system-alist))
874 (last ama)
875 entry)
876
877 (while (cdr last)
878 (setq entry (car (cdr last)))
879 (if (member entry jka-compr-added-to-file-coding-system-alist)
880 (setcdr last (cdr (cdr last)))
881 (setq last (cdr last))))
882
883 (setq file-coding-system-alist (cdr ama)))
884
885 ;; Remove the suffixes that were added by jka-compr.
886 (let ((suffixes nil)
887 (re (jka-compr-build-file-regexp)))
888 (dolist (suffix load-suffixes)
889 (unless (string-match re suffix)
890 (push suffix suffixes)))
891 (setq load-suffixes (nreverse suffixes))))
892
893
894 (defun jka-compr-installed-p ()
895 "Return non-nil if jka-compr is installed.
896 The return value is the entry in `file-name-handler-alist' for jka-compr."
897
898 (let ((fnha file-name-handler-alist)
899 (installed nil))
900
901 (while (and fnha (not installed))
902 (and (eq (cdr (car fnha)) 'jka-compr-handler)
903 (setq installed (car fnha)))
904 (setq fnha (cdr fnha)))
905
906 installed))
907
908
909 ;;; Add the file I/O hook if it does not already exist.
910 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
911 ;;; entry for jka-compr in file-name-handler-alist.
912 (and (jka-compr-installed-p)
913 (jka-compr-uninstall))
914
915
916 ;;;###autoload
917 (define-minor-mode auto-compression-mode
918 "Toggle automatic file compression and uncompression.
919 With prefix argument ARG, turn auto compression on if positive, else off.
920 Returns the new status of auto compression (non-nil means on)."
921 :global t :group 'jka-compr
922 (let* ((installed (jka-compr-installed-p))
923 (flag auto-compression-mode))
924 (cond
925 ((and flag installed) t) ; already installed
926 ((and (not flag) (not installed)) nil) ; already not installed
927 (flag (jka-compr-install))
928 (t (jka-compr-uninstall)))))
929
930
931 ;;;###autoload
932 (defmacro with-auto-compression-mode (&rest body)
933 "Evalute BODY with automatic file compression and uncompression enabled."
934 (let ((already-installed (make-symbol "already-installed")))
935 `(let ((,already-installed (jka-compr-installed-p)))
936 (unwind-protect
937 (progn
938 (unless ,already-installed
939 (jka-compr-install))
940 ,@body)
941 (unless ,already-installed
942 (jka-compr-uninstall))))))
943 (put 'with-auto-compression-mode 'lisp-indent-function 0)
944
945
946 (provide 'jka-compr)
947
948 ;;; jka-compr.el ends here