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-discard compr-args
129 ;; uncomp-message uncomp-prog uncomp-discard 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 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:
146 regexp is a regexp that matches filenames that are
147 compressed with this format
149 compress-program is a program that performs this compression
151 compress-args is a list of args to pass to the compress program
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)
157 uncompress-program is a program that performs this compression
159 uncompress-args is a list of args to pass to the uncompress program
161 append-flag is non-nil if this compression technique can be
164 auto-mode flag non-nil means strip the regexp from file names
165 before attempting to set the mode.
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
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))
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
187 based on the filename itself and jka-compr-compression-info-list."
188 (catch 'compression-info
189 (let ((case-fold-search nil
))
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
)
198 (put 'compression-error
'error-conditions
'(compression-error file-error error
))
201 (defvar jka-compr-acceptable-retval-list
'(0 141))
204 (defun jka-compr-error (prog args infile message
&optional errfile
)
206 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
207 (curbuf (current-buffer)))
209 (widen) (erase-buffer)
210 (insert (format "Error while executing \"%s %s < %s\"\n\n"
212 (mapconcat 'identity args
" ")
216 (insert-file-contents errfile
))
219 (display-buffer errbuf
))
221 (signal 'compression-error
(list "Opening input file" (format "error %s" message
) infile
)))
224 (defvar jka-compr-dd-program
228 (defvar jka-compr-dd-blocksize
256)
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
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
))))
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"
244 (mapconcat 'identity args
" ")
247 jka-compr-dd-blocksize
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
)) ""))))
255 (or (memq (call-process jka-compr-shell
258 jka-compr-acceptable-retval-list
)
260 (jka-compr-error prog args infile message err-file
))
262 (jka-compr-delete-temp-file err-file
))
266 (delete-region (+ start prefix len
) (point)))
268 (delete-region start
(+ start prefix
))))
271 (defun jka-compr-call-process (prog message infile output temp args
)
272 (if jka-compr-use-shell
274 (let ((err-file (jka-compr-make-temp-name)))
279 (call-process jka-compr-shell infile
280 (if (stringp output
) nil output
)
283 (format "%s %s 2> %s %s"
285 (mapconcat 'identity args
" ")
290 jka-compr-acceptable-retval-list
)
292 (jka-compr-error prog args infile message err-file
))
294 (jka-compr-delete-temp-file err-file
)))
300 (if (stringp output
) temp output
)
303 (jka-compr-error prog args infile message
))
305 (and (stringp output
)
306 (let ((cbuf (current-buffer)))
308 (write-region (point-min) (point-max) output
)
310 (set-buffer cbuf
)))))
313 ;;; Support for temp files. Much of this was inspired if not lifted
316 (defvar jka-compr-temp-name-template
318 "Prefix added to all temp files created by jka-compr.
319 There should be no more than seven characters after the final '/'")
321 (defvar jka-compr-temp-name-table
(make-vector 31 nil
))
323 (defun jka-compr-make-temp-name (&optional local-copy
)
324 "This routine will return the name of a new file."
327 (template (concat jka-compr-temp-name-template
"aa"))
328 (lastpos (1- (length template
)))
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
))
341 (setq lastchar
(1+ lastchar
))
344 (setq prevchar
(1+ prevchar
))
347 (error "Can't allocate temp file.")
348 (aset template
(1- lastpos
) prevchar
)))))
350 (put entry
'active
(not local-copy
))
351 (setq not-done nil
)))
356 (defun jka-compr-delete-temp-file (temp)
358 (put (intern temp jka-compr-temp-name-table
)
366 (defun jka-compr-write-region (start end file
&optional append visit
)
367 "Documented as original."
368 (interactive "r\nFWrite region to file: ")
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
)))
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
))
387 (setq cbuf
(current-buffer)
388 temp-buffer
(get-buffer-create " *jka-compr-temp*"))
389 (set-buffer temp-buffer
)
390 (widen) (erase-buffer)
395 (file-exists-p filename
)
396 (let* ((local-copy (file-local-copy filename
))
397 (local-file (or local-copy filename
)))
405 (message "%s %s..." uncompress-message base-name
))
407 (jka-compr-call-process uncompress-program
408 (concat uncompress-message
416 (message "%s %s...done" uncompress-message base-name
)))
420 (file-exists-p local-copy
)
421 (delete-file local-copy
)))))
425 (message "%s %s..." compress-message base-name
))
427 (write-region start end temp-file t
'dont
)
429 (jka-compr-call-process compress-program
430 (concat compress-message
437 (set-buffer temp-buffer
)
438 (write-region (point-min) (point-max)
439 filename
(and append can-append
) 'dont
)
443 (jka-compr-delete-temp-file temp-file
)
447 (message "%s %s...done" compress-message base-name
))
451 (setq buffer-file-name filename
)
452 (set-visited-file-modtime))
454 (setq buffer-file-name visit
)
455 (let ((buffer-file-name filename
))
456 (set-visited-file-modtime))))
458 (and (or (eq visit t
)
461 (message "Wrote %s" visit-file
))
465 (write-region start end filename append visit
))))
468 (defun jka-compr-insert-file-contents (file &optional visit beg end replace
)
469 "Documented as original."
471 (barf-if-buffer-read-only)
475 (error "Attempt to visit less than an entire file"))
477 (let* ((filename (expand-file-name file
))
478 (info (jka-compr-get-compression-info filename
)))
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
))
487 (local-copy (file-local-copy filename
))
491 (setq local-file
(or local-copy filename
))
495 (setq buffer-file-name filename
))
497 (unwind-protect ; to make sure local-copy gets deleted
503 (message "%s %s..." uncompress-message base-name
))
505 (condition-case error-code
510 (jka-compr-partial-uncompress uncompress-program
511 (concat uncompress-message
519 (jka-compr-call-process uncompress-program
520 (concat uncompress-message
526 (setq size
(- (point) start
))
531 (if (and (eq (car error-code
) 'file-error
)
532 (eq (nth 3 error-code
) local-file
))
534 (setq notfound error-code
)
536 (cons "Opening input file"
537 (nthcdr 2 error-code
))))
538 (signal (car error-code
) (cdr error-code
))))))
542 (file-exists-p local-copy
)
543 (delete-file local-copy
)))
548 (setq buffer-file-name filename
)
549 (set-visited-file-modtime)))
553 (message "%s %s...done" uncompress-message base-name
))
559 (cons "Opening input file" (nth 2 notfound
))))
561 (list filename size
))
563 (insert-file-contents file visit beg end replace
))))
566 (defun jka-compr-file-local-copy (file)
567 "Documented as original."
569 (let* ((filename (expand-file-name file
))
570 (info (jka-compr-get-compression-info filename
)))
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*"))
582 (cbuf (current-buffer))
585 (setq local-file
(or local-copy filename
))
593 (message "%s %s..." uncompress-message base-name
))
595 (set-buffer temp-buffer
)
597 (jka-compr-call-process uncompress-program
598 (concat uncompress-message
607 (message "%s %s...done" uncompress-message base-name
))
610 (point-min) (point-max) temp-file nil
'dont
))
614 (file-exists-p local-copy
)
615 (delete-file local-copy
))
618 (kill-buffer temp-buffer
))
622 (file-local-copy filename
))))
625 ;;; Support for loading compressed files.
626 (defun jka-compr-load (file &optional noerror nomessage nosuffix
)
627 "Documented as original."
629 (let* ((local-copy (jka-compr-file-local-copy file
))
630 (load-file (or local-copy file
)))
636 (setq file-name-handler-alist
637 (cons jka-compr-file-name-handler-entry
638 file-name-handler-alist
))
641 (message "Loading %s..." file
))
643 (load load-file noerror t t
)
646 (message "Loading %s...done." file
)))
648 (setq file-name-handler-alist
649 (delq jka-compr-file-name-handler-entry
650 file-name-handler-alist
))
652 (jka-compr-delete-temp-file local-copy
))
657 (defvar jka-compr-file-name-handler-entry
659 "The entry in file-name-handler-alist used by the jka-compr I/O functions.")
662 (defun jka-compr-handler (operation &rest args
)
664 (let ((jka-op (intern-soft (symbol-name operation
) jka-compr-op-table
))
665 (match-data (match-data)))
669 (setq file-name-handler-alist
670 (delq jka-compr-file-name-handler-entry
671 file-name-handler-alist
))
674 (apply operation args
)))
676 (setq file-name-handler-alist
677 (cons jka-compr-file-name-handler-entry
678 file-name-handler-alist
))
679 (store-match-data match-data
))))
682 (defvar jka-compr-op-table
684 "Hash table of operations supported by jka-compr.")
687 (defun jka-compr-intern-operation (op)
688 (let ((opsym (intern (symbol-name op
) jka-compr-op-table
))
689 (jka-fn (intern (concat "jka-compr-" (symbol-name op
)))))
690 (fset opsym jka-fn
)))
693 (defvar jka-compr-operation-list
700 "List of file operations implemented by jka-compr.")
706 (jka-compr-intern-operation fn
)))
707 jka-compr-operation-list
)
710 (defun toggle-auto-compression (arg)
711 "Toggle automatic file compression and decompression.
712 With prefix argument ARG, turn auto compression on if positive, else off.
713 Returns the new status of auto compression (non-nil means on)."
715 (let* ((installed (jka-compr-installed-p))
718 (or (eq arg t
) (listp arg
) (and (integerp arg
) (> arg
0))))))
721 ((and flag installed
) t
) ; already installed
723 ((and (not flag
) (not installed
)) nil
) ; already not installed
729 (jka-compr-uninstall)))
734 (message "Automatic file (de)compression is now ON.")
735 (message "Automatic file (de)compression is now OFF.")))
740 (defun jka-compr-build-file-regexp ()
744 'jka-compr-info-regexp
745 jka-compr-compression-info-list
750 (defun jka-compr-install ()
752 Appropriate entries are added to file-name-handler-alist and auto-mode-alist."
754 (setq jka-compr-file-name-handler-entry
755 (cons (jka-compr-build-file-regexp) 'jka-compr-handler
))
757 (setq file-name-handler-alist
(cons jka-compr-file-name-handler-entry
758 file-name-handler-alist
))
761 (function (lambda (x)
763 (jka-compr-info-strip-extension x
)
764 (setq auto-mode-alist
(cons (list (jka-compr-info-regexp x
)
768 jka-compr-compression-info-list
))
771 (defun jka-compr-uninstall ()
772 "Uninstall jka-compr.
773 Entries in file-name-handler-alist and auto-mode-alist that were created by
774 jka-compr-installed are removed."
776 (let* ((fnha (cons nil file-name-handler-alist
))
780 (if (eq (cdr (car (cdr last
))) 'jka-compr-handler
)
781 (setcdr last
(cdr (cdr last
)))
782 (setq last
(cdr last
))))
784 (setq file-name-handler-alist
(cdr fnha
)))
786 (let* ((ama (cons nil auto-mode-alist
))
791 (setq entry
(car (cdr last
)))
792 (if (and (consp (cdr entry
))
793 (eq (nth 2 entry
) 'jka-compr
))
794 (setcdr last
(cdr (cdr last
)))
795 (setq last
(cdr last
))))
797 (setq auto-mode-alist
(cdr ama
))))
800 (defun jka-compr-installed-p ()
801 "Return non-nil if jka-compr is installed.
802 The return value is the entry in file-name-handler-alist for jka-compr."
804 (let ((fnha file-name-handler-alist
)
807 (while (and fnha
(not installed
))
808 (and (eq (cdr (car fnha
)) 'jka-compr-handler
)
809 (setq installed
(car fnha
)))
810 (setq fnha
(cdr fnha
)))
815 ;;; Add the file I/O hook if it does not already exist.
816 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
817 ;;; entry for jka-compr in file-name-handler-alist.
818 (and (jka-compr-installed-p)
819 (jka-compr-uninstall))
826 ;; jka-compr.el ends here.