1 ;;; jka-compr.el - reading/writing/loading compressed files.
2 ;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
4 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
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).
15 ;;; Packages like Rmail, VM, GNUS, and Info should be able to work
16 ;;; with compressed files without modification.
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
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.
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
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
46 ;;; APPLICATION NOTES:
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.
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
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:
72 ;;; ["\\.trz\\'" "\037\213"
73 ;;; "zip" "gzip" nil ("-q")
74 ;;; "unzip" "gzip" nil ("-q" "-d")
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
83 ;;; Finally, to make tar-mode start up automatically, you have to add an
84 ;;; entry to auto-mode-alist that looks like this
86 ;;; ("\\.trz\\'" . tar-mode)
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.
103 ;;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
104 ;;; Version 18 of Emacs.
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.
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
118 for `jka-compr-compression-info-list').")
121 (defvar jka-compr-use-shell t
)
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
128 ;; compr-message compr-prog compr-args
129 ;; uncomp-message uncomp-prog uncomp-args
130 ;; can-append auto-mode-flag]
132 "compressing" "compress" ("-c")
133 "uncompressing" "uncompress" ("-c")
136 "zipping" "gzip" ("-c" "-q")
137 "unzipping" "gzip" ("-c" "-q" "-d")
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 COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS
143 UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS
144 APPEND-FLAG EXTENSION], where:
146 regexp is a regexp that matches filenames that are
147 compressed with this format
149 compress-msg is the message to issue to the user when doing this
150 type of compression (nil means no message)
152 compress-program is a program that performs this compression
154 compress-args is a list of args to pass to the compress program
156 uncompress-msg is the message to issue to the user when doing this
157 type of uncompression (nil means no message)
159 uncompress-program is a program that performs this compression
161 uncompress-args is a list of args to pass to the uncompress program
163 append-flag is non-nil if this compression technique can be
166 auto-mode flag non-nil means strip the regexp from file names
167 before attempting to set the mode.
169 Because of the way `call-process' is defined, discarding the stderr output of
170 a program adds the overhead of starting a shell each time the program is
174 (defvar jka-compr-file-name-handler-entry
176 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.")
178 ;;; Functions for accessing the return value of jka-get-compression-info
179 (defun jka-compr-info-regexp (info) (aref info
0))
180 (defun jka-compr-info-compress-message (info) (aref info
1))
181 (defun jka-compr-info-compress-program (info) (aref info
2))
182 (defun jka-compr-info-compress-args (info) (aref info
3))
183 (defun jka-compr-info-uncompress-message (info) (aref info
4))
184 (defun jka-compr-info-uncompress-program (info) (aref info
5))
185 (defun jka-compr-info-uncompress-args (info) (aref info
6))
186 (defun jka-compr-info-can-append (info) (aref info
7))
187 (defun jka-compr-info-strip-extension (info) (aref info
8))
190 (defun jka-compr-get-compression-info (filename)
191 "Return information about the compression scheme of FILENAME.
192 The determination as to which compression scheme, if any, to use is
193 based on the filename itself and `jka-compr-compression-info-list'."
194 (catch 'compression-info
195 (let ((case-fold-search nil
))
197 (function (lambda (x)
198 (and (string-match (jka-compr-info-regexp x
) filename
)
199 (throw 'compression-info x
))))
200 jka-compr-compression-info-list
)
204 (put 'compression-error
'error-conditions
'(compression-error file-error error
))
207 (defvar jka-compr-acceptable-retval-list
'(0 141))
210 (defun jka-compr-error (prog args infile message
&optional errfile
)
212 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
213 (curbuf (current-buffer)))
215 (widen) (erase-buffer)
216 (insert (format "Error while executing \"%s %s < %s\"\n\n"
218 (mapconcat 'identity args
" ")
222 (insert-file-contents errfile
))
225 (display-buffer errbuf
))
227 (signal 'compression-error
(list "Opening input file" (format "error %s" message
) infile
)))
230 (defvar jka-compr-dd-program
234 (defvar jka-compr-dd-blocksize
256)
237 (defun jka-compr-partial-uncompress (prog message args infile beg len
)
238 "Call program PROG with ARGS args taking input from INFILE.
239 Fourth and fifth args, BEG and LEN, specify which part of the output
240 to keep: LEN chars starting BEG chars from the beginning."
241 (let* ((skip (/ beg jka-compr-dd-blocksize
))
242 (prefix (- beg
(* skip jka-compr-dd-blocksize
)))
243 (count (and len
(1+ (/ (+ len prefix
) jka-compr-dd-blocksize
))))
245 (err-file (jka-compr-make-temp-name))
246 (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null"
248 (mapconcat 'identity args
" ")
251 jka-compr-dd-blocksize
253 ;; dd seems to be unreliable about
254 ;; providing the last block. So, always
255 ;; read one more than you think you need.
256 (if count
(concat "count=" (1+ count
)) ""))))
259 (or (memq (call-process jka-compr-shell
262 jka-compr-acceptable-retval-list
)
264 (jka-compr-error prog args infile message err-file
))
266 (jka-compr-delete-temp-file err-file
))
268 ;; Delete the stuff after what we want, if there is any.
271 (< (+ start prefix len
) (point))
272 (delete-region (+ start prefix len
) (point)))
274 ;; Delete the stuff before what we want.
275 (delete-region start
(+ start prefix
))))
278 (defun jka-compr-call-process (prog message infile output temp args
)
279 (if jka-compr-use-shell
281 (let ((err-file (jka-compr-make-temp-name)))
286 (call-process jka-compr-shell infile
287 (if (stringp output
) nil output
)
290 (format "%s %s 2> %s %s"
292 (mapconcat 'identity args
" ")
297 jka-compr-acceptable-retval-list
)
299 (jka-compr-error prog args infile message err-file
))
301 (jka-compr-delete-temp-file err-file
)))
307 (if (stringp output
) temp output
)
310 (jka-compr-error prog args infile message
))
312 (and (stringp output
)
313 (let ((cbuf (current-buffer)))
315 (write-region (point-min) (point-max) output
)
317 (set-buffer cbuf
)))))
320 ;;; Support for temp files. Much of this was inspired if not lifted
323 (defvar jka-compr-temp-name-template
325 "Prefix added to all temp files created by jka-compr.
326 There should be no more than seven characters after the final `/'")
328 (defvar jka-compr-temp-name-table
(make-vector 31 nil
))
330 (defun jka-compr-make-temp-name (&optional local-copy
)
331 "This routine will return the name of a new file."
334 (template (concat jka-compr-temp-name-template
"aa"))
335 (lastpos (1- (length template
)))
341 (aset template lastpos lastchar
)
342 (setq file
(concat (make-temp-name template
) "#"))
343 (setq entry
(intern file jka-compr-temp-name-table
))
344 (if (or (get entry
'active
)
345 (file-exists-p file
))
348 (setq lastchar
(1+ lastchar
))
351 (setq prevchar
(1+ prevchar
))
354 (error "Can't allocate temp file.")
355 (aset template
(1- lastpos
) prevchar
)))))
357 (put entry
'active
(not local-copy
))
358 (setq not-done nil
)))
363 (defun jka-compr-delete-temp-file (temp)
365 (put (intern temp jka-compr-temp-name-table
)
373 (defun jka-compr-write-region (start end file
&optional append visit
)
374 (let* ((filename (expand-file-name file
))
375 (visit-file (if (stringp visit
) (expand-file-name visit
) filename
))
376 (info (jka-compr-get-compression-info visit-file
)))
380 (let ((can-append (jka-compr-info-can-append info
))
381 (compress-program (jka-compr-info-compress-program info
))
382 (compress-message (jka-compr-info-compress-message info
))
383 (uncompress-program (jka-compr-info-uncompress-program info
))
384 (uncompress-message (jka-compr-info-uncompress-message info
))
385 (compress-args (jka-compr-info-compress-args info
))
386 (uncompress-args (jka-compr-info-uncompress-args info
))
387 (temp-file (jka-compr-make-temp-name))
388 (base-name (file-name-nondirectory visit-file
))
391 (setq cbuf
(current-buffer)
392 temp-buffer
(get-buffer-create " *jka-compr-temp*"))
393 (set-buffer temp-buffer
)
394 (widen) (erase-buffer)
399 (file-exists-p filename
)
400 (let* ((local-copy (file-local-copy filename
))
401 (local-file (or local-copy filename
)))
409 (message "%s %s..." uncompress-message base-name
))
411 (jka-compr-call-process uncompress-program
412 (concat uncompress-message
420 (message "%s %s...done" uncompress-message base-name
)))
424 (file-exists-p local-copy
)
425 (delete-file local-copy
)))))
429 (message "%s %s..." compress-message base-name
))
431 (jka-compr-run-real-handler 'write-region
432 (list start end temp-file t
'dont
))
434 (jka-compr-call-process compress-program
435 (concat compress-message
442 (set-buffer temp-buffer
)
443 (jka-compr-run-real-handler 'write-region
444 (list (point-min) (point-max)
446 (and append can-append
) 'dont
))
450 (jka-compr-delete-temp-file temp-file
)
454 (message "%s %s...done" compress-message base-name
))
458 (setq buffer-file-name filename
)
459 (set-visited-file-modtime))
461 (setq buffer-file-name visit
)
462 (let ((buffer-file-name filename
))
463 (set-visited-file-modtime))))
465 (and (or (eq visit t
)
468 (message "Wrote %s" visit-file
))
472 (jka-compr-run-real-handler 'write-region
473 (list start end filename append visit
)))))
476 (defun jka-compr-insert-file-contents (file &optional visit beg end replace
)
477 (barf-if-buffer-read-only)
481 (error "Attempt to visit less than an entire file"))
483 (let* ((filename (expand-file-name file
))
484 (info (jka-compr-get-compression-info filename
)))
488 (let ((uncompress-message (jka-compr-info-uncompress-message info
))
489 (uncompress-program (jka-compr-info-uncompress-program info
))
490 (uncompress-args (jka-compr-info-uncompress-args info
))
491 (base-name (file-name-nondirectory filename
))
494 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))
498 (setq local-file
(or local-copy filename
))
502 (setq buffer-file-name filename
))
504 (unwind-protect ; to make sure local-copy gets deleted
510 (message "%s %s..." uncompress-message base-name
))
512 (condition-case error-code
517 (jka-compr-partial-uncompress uncompress-program
518 (concat uncompress-message
526 (jka-compr-call-process uncompress-program
527 (concat uncompress-message
533 (setq size
(- (point) start
))
538 (if (and (eq (car error-code
) 'file-error
)
539 (eq (nth 3 error-code
) local-file
))
541 (setq notfound error-code
)
543 (cons "Opening input file"
544 (nthcdr 2 error-code
))))
545 (signal (car error-code
) (cdr error-code
))))))
549 (file-exists-p local-copy
)
550 (delete-file local-copy
)))
556 (setq buffer-file-name filename
)
557 (set-visited-file-modtime)))
561 (message "%s %s...done" uncompress-message base-name
))
567 (cons "Opening input file" (nth 2 notfound
))))
569 (list filename size
))
571 (jka-compr-run-real-handler 'insert-file-contents
572 (list file visit beg end replace
)))))
575 (defun jka-compr-file-local-copy (file)
576 (let* ((filename (expand-file-name file
))
577 (info (jka-compr-get-compression-info filename
)))
581 (let ((uncompress-message (jka-compr-info-uncompress-message info
))
582 (uncompress-program (jka-compr-info-uncompress-program info
))
583 (uncompress-args (jka-compr-info-uncompress-args info
))
584 (base-name (file-name-nondirectory filename
))
586 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))
587 (temp-file (jka-compr-make-temp-name t
))
588 (temp-buffer (get-buffer-create " *jka-compr-temp*"))
590 (cbuf (current-buffer))
593 (setq local-file
(or local-copy filename
))
601 (message "%s %s..." uncompress-message base-name
))
603 (set-buffer temp-buffer
)
605 (jka-compr-call-process uncompress-program
606 (concat uncompress-message
615 (message "%s %s...done" uncompress-message base-name
))
618 (point-min) (point-max) temp-file nil
'dont
))
622 (file-exists-p local-copy
)
623 (delete-file local-copy
))
626 (kill-buffer temp-buffer
))
630 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))))
633 ;;; Support for loading compressed files.
634 (defun jka-compr-load (file &optional noerror nomessage nosuffix
)
635 "Documented as original."
637 (let* ((local-copy (jka-compr-file-local-copy file
))
638 (load-file (or local-copy file
)))
642 (let (inhibit-file-name-operation
643 inhibit-file-name-handlers
)
645 (message "Loading %s..." file
))
647 (load load-file noerror t t
)
650 (message "Loading %s...done." file
)))
652 (jka-compr-delete-temp-file local-copy
))
656 (put 'write-region
'jka-compr
'jka-compr-write-region
)
657 (put 'insert-file-contents
'jka-compr
'jka-compr-insert-file-contents
)
658 (put 'file-local-copy
'jka-compr
'jka-compr-file-local-copy
)
659 (put 'load
'jka-compr
'jka-compr-load
)
661 (defun jka-compr-handler (operation &rest args
)
663 (let ((jka-op (get operation
'jka-compr
)))
666 (jka-compr-run-real-handler operation args
)))))
668 ;; If we are given an operation that we don't handle,
669 ;; call the Emacs primitive for that operation,
670 ;; and manipulate the inhibit variables
671 ;; to prevent the primitive from calling our handler again.
672 (defun jka-compr-run-real-handler (operation args
)
673 (let ((inhibit-file-name-handlers
674 (cons 'jka-compr-handler
675 (and (eq inhibit-file-name-operation operation
)
676 inhibit-file-name-handlers
)))
677 (inhibit-file-name-operation operation
))
678 (apply operation args
)))
680 (defun toggle-auto-compression (arg)
681 "Toggle automatic file compression and decompression.
682 With prefix argument ARG, turn auto compression on if positive, else off.
683 Returns the new status of auto compression (non-nil means on)."
685 (let* ((installed (jka-compr-installed-p))
688 (or (eq arg t
) (listp arg
) (and (integerp arg
) (> arg
0))))))
691 ((and flag installed
) t
) ; already installed
693 ((and (not flag
) (not installed
)) nil
) ; already not installed
699 (jka-compr-uninstall)))
704 (message "Automatic file (de)compression is now ON.")
705 (message "Automatic file (de)compression is now OFF.")))
710 (defun jka-compr-build-file-regexp ()
714 'jka-compr-info-regexp
715 jka-compr-compression-info-list
720 (defun jka-compr-install ()
722 This adds entries to `file-name-handler-alist' and `auto-mode-alist'."
724 (setq jka-compr-file-name-handler-entry
725 (cons (jka-compr-build-file-regexp) 'jka-compr-handler
))
727 (setq file-name-handler-alist
(cons jka-compr-file-name-handler-entry
728 file-name-handler-alist
))
730 ;; Make entries in auto-mode-alist so that modes are chosen right
731 ;; according to the file names sans `.gz'.
733 (function (lambda (x)
735 (jka-compr-info-strip-extension x
)
736 (setq auto-mode-alist
(cons (list (jka-compr-info-regexp x
)
740 jka-compr-compression-info-list
))
743 (defun jka-compr-uninstall ()
744 "Uninstall jka-compr.
745 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
746 that were created by `jka-compr-installed'."
748 (let* ((fnha (cons nil file-name-handler-alist
))
752 (if (eq (cdr (car (cdr last
))) 'jka-compr-handler
)
753 (setcdr last
(cdr (cdr last
)))
754 (setq last
(cdr last
))))
756 (setq file-name-handler-alist
(cdr fnha
)))
758 (let* ((ama (cons nil auto-mode-alist
))
763 (setq entry
(car (cdr last
)))
764 (if (and (consp (cdr entry
))
765 (eq (nth 2 entry
) 'jka-compr
))
766 (setcdr last
(cdr (cdr last
)))
767 (setq last
(cdr last
))))
769 (setq auto-mode-alist
(cdr ama
))))
772 (defun jka-compr-installed-p ()
773 "Return non-nil if jka-compr is installed.
774 The return value is the entry in `file-name-handler-alist' for jka-compr."
776 (let ((fnha file-name-handler-alist
)
779 (while (and fnha
(not installed
))
780 (and (eq (cdr (car fnha
)) 'jka-compr-handler
)
781 (setq installed
(car fnha
)))
782 (setq fnha
(cdr fnha
)))
787 ;;; Add the file I/O hook if it does not already exist.
788 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
789 ;;; entry for jka-compr in file-name-handler-alist.
790 (and (jka-compr-installed-p)
791 (jka-compr-uninstall))
798 ;; jka-compr.el ends here.