1 ;;; jka-compr.el --- reading/writing/loading compressed files
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This package implements low-level support for reading, writing,
30 ;; and loading compressed files. It hooks into the low-level file
31 ;; I/O functions (including write-region and insert-file-contents) so
32 ;; that they automatically compress or uncompress a file if the file
33 ;; appears to need it (based on the extension of the file name).
34 ;; Packages like Rmail, VM, GNUS, and Info should be able to work
35 ;; with compressed files without modification.
40 ;; To use jka-compr, invoke the command `auto-compression-mode' (which
41 ;; see), or customize the variable of the same name. Its operation
42 ;; should be transparent to the user (except for messages appearing when
43 ;; a file is being compressed or uncompressed).
45 ;; The variable, jka-compr-compression-info-list can be used to
46 ;; customize jka-compr to work with other compression programs.
47 ;; The default value of this variable allows jka-compr to work with
48 ;; Unix compress and gzip.
50 ;; If you are concerned about the stderr output of gzip and other
51 ;; compression/decompression programs showing up in your buffers, you
52 ;; should set the discard-error flag in the compression-info-list.
53 ;; This will cause the stderr of all programs to be discarded.
54 ;; However, it also causes emacs to call compression/uncompression
55 ;; programs through a shell (which is specified by jka-compr-shell).
56 ;; This may be a drag if, on your system, starting up a shell is
59 ;; If you don't want messages about compressing and decompressing
60 ;; to show up in the echo area, you can set the compress-name and
61 ;; decompress-name fields of the jka-compr-compression-info-list to
68 ;; jka-compr can coexist with crypt++ if you take all the decompression
69 ;; entries out of the crypt-encoding-list. Clearly problems will arise if
70 ;; you have two programs trying to compress/decompress files. jka-compr
71 ;; will not "work with" crypt++ in the following sense: you won't be able to
72 ;; decode encrypted compressed files--that is, files that have been
73 ;; compressed then encrypted (in that order). Theoretically, crypt++ and
74 ;; jka-compr could properly handle a file that has been encrypted then
75 ;; compressed, but there is little point in trying to compress an encrypted
82 ;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people
83 ;; have made helpful suggestions, reported bugs, and even fixed bugs in
84 ;; jka-compr. I recall the following people as being particularly helpful.
93 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
94 ;; Version 18 of Emacs.
96 ;; After I had made progress on the original jka-compr for V18, I learned of a
97 ;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly
98 ;; what I was trying to do. I looked over the jam-zcat source code and
99 ;; probably got some ideas from it.
104 (defcustom jka-compr-shell
"sh"
105 "*Shell to be used for calling compression programs.
106 The value of this variable only matters if you want to discard the
107 stderr of a compression/decompression program (see the documentation
108 for `jka-compr-compression-info-list')."
112 (defvar jka-compr-use-shell
113 (not (memq system-type
'(ms-dos windows-nt
))))
115 (defvar jka-compr-really-do-compress nil
116 "Non-nil in a buffer whose visited file was uncompressed on visiting it.
117 This means compress the data on writing the file, even if the
118 data appears to be compressed already.")
119 (make-variable-buffer-local 'jka-compr-really-do-compress
)
120 (put 'jka-compr-really-do-compress
'permanent-local t
)
122 ;;; Functions for accessing the return value of jka-compr-get-compression-info
123 (defun jka-compr-info-regexp (info) (aref info
0))
124 (defun jka-compr-info-compress-message (info) (aref info
1))
125 (defun jka-compr-info-compress-program (info) (aref info
2))
126 (defun jka-compr-info-compress-args (info) (aref info
3))
127 (defun jka-compr-info-uncompress-message (info) (aref info
4))
128 (defun jka-compr-info-uncompress-program (info) (aref info
5))
129 (defun jka-compr-info-uncompress-args (info) (aref info
6))
130 (defun jka-compr-info-can-append (info) (aref info
7))
131 (defun jka-compr-info-strip-extension (info) (aref info
8))
132 (defun jka-compr-info-file-magic-bytes (info) (aref info
9))
135 (defun jka-compr-get-compression-info (filename)
136 "Return information about the compression scheme of FILENAME.
137 The determination as to which compression scheme, if any, to use is
138 based on the filename itself and `jka-compr-compression-info-list'."
139 (catch 'compression-info
140 (let ((case-fold-search nil
))
142 (function (lambda (x)
143 (and (string-match (jka-compr-info-regexp x
) filename
)
144 (throw 'compression-info x
))))
145 jka-compr-compression-info-list
)
149 (put 'compression-error
'error-conditions
'(compression-error file-error error
))
152 (defvar jka-compr-acceptable-retval-list
'(0 2 141))
155 (defun jka-compr-error (prog args infile message
&optional errfile
)
157 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
158 (curbuf (current-buffer)))
159 (with-current-buffer errbuf
160 (widen) (erase-buffer)
161 (insert (format "Error while executing \"%s %s < %s\"\n\n"
163 (mapconcat 'identity args
" ")
167 (insert-file-contents errfile
)))
168 (display-buffer errbuf
))
170 (signal 'compression-error
171 (list "Opening input file" (format "error %s" message
) infile
)))
174 (defcustom jka-compr-dd-program
"/bin/dd"
175 "How to invoke `dd'."
180 (defvar jka-compr-dd-blocksize
256)
183 (defun jka-compr-partial-uncompress (prog message args infile beg len
)
184 "Call program PROG with ARGS args taking input from INFILE.
185 Fourth and fifth args, BEG and LEN, specify which part of the output
186 to keep: LEN chars starting BEG chars from the beginning."
187 (let ((start (point))
189 (if (and jka-compr-use-shell jka-compr-dd-program
)
190 ;; Put the uncompression output through dd
191 ;; to discard the part we don't want.
192 (let ((skip (/ beg jka-compr-dd-blocksize
))
193 (err-file (jka-compr-make-temp-name))
195 ;; Update PREFIX based on the text that we won't read in.
196 (setq prefix
(- beg
(* skip jka-compr-dd-blocksize
))
197 count
(and len
(1+ (/ (+ len prefix
) jka-compr-dd-blocksize
))))
199 (or (memq (call-process
200 jka-compr-shell infile t nil
"-c"
202 "%s %s 2> %s | %s bs=%d skip=%d %s 2> %s"
204 (mapconcat 'identity args
" ")
207 jka-compr-dd-blocksize
209 ;; dd seems to be unreliable about
210 ;; providing the last block. So, always
211 ;; read one more than you think you need.
212 (if count
(format "count=%d" (1+ count
)) "")
214 jka-compr-acceptable-retval-list
)
215 (jka-compr-error prog args infile message err-file
))
216 (jka-compr-delete-temp-file err-file
)))
217 ;; Run the uncompression program directly.
218 ;; We get the whole file and must delete what we don't want.
219 (jka-compr-call-process prog message infile t nil args
))
221 ;; Delete the stuff after what we want, if there is any.
224 (< (+ start prefix len
) (point))
225 (delete-region (+ start prefix len
) (point)))
227 ;; Delete the stuff before what we want.
228 (delete-region start
(+ start prefix
))))
231 (defun jka-compr-call-process (prog message infile output temp args
)
232 (if jka-compr-use-shell
234 (let ((err-file (jka-compr-make-temp-name))
235 (coding-system-for-read (or coding-system-for-read
'undecided
))
236 (coding-system-for-write 'no-conversion
))
241 (call-process jka-compr-shell infile
242 (if (stringp output
) nil output
)
245 (format "%s %s 2> %s %s"
247 (mapconcat 'identity args
" ")
252 jka-compr-acceptable-retval-list
)
254 (jka-compr-error prog args infile message err-file
))
256 (jka-compr-delete-temp-file err-file
)))
262 (if (stringp output
) temp output
)
265 (jka-compr-error prog args infile message
))
267 (and (stringp output
)
268 (with-current-buffer temp
269 (write-region (point-min) (point-max) output
)
273 ;;; Support for temp files. Much of this was inspired if not lifted
276 (defcustom jka-compr-temp-name-template
277 (expand-file-name "jka-com" temporary-file-directory
)
278 "Prefix added to all temp files created by jka-compr.
279 There should be no more than seven characters after the final `/'."
283 (defun jka-compr-make-temp-name (&optional local-copy
)
284 "This routine will return the name of a new file."
285 (make-temp-file jka-compr-temp-name-template
))
287 (defalias 'jka-compr-delete-temp-file
'delete-file
)
290 (defun jka-compr-write-region (start end file
&optional append visit
)
291 (let* ((filename (expand-file-name file
))
292 (visit-file (if (stringp visit
) (expand-file-name visit
) filename
))
293 (info (jka-compr-get-compression-info visit-file
))
294 (magic (and info
(jka-compr-info-file-magic-bytes info
))))
296 ;; If START is nil, use the whole buffer.
298 (setq start
1 end
(1+ (buffer-size))))
300 ;; If we uncompressed this file when visiting it,
301 ;; then recompress it when writing it
302 ;; even if the contents look compressed already.
303 (if (and jka-compr-really-do-compress
305 (eq end
(1+ (buffer-size))))
309 ;; If the contents to be written out
310 ;; are properly compressed already,
311 ;; don't try to compress them over again.
313 (equal (if (stringp start
)
314 (substring start
0 (min (length start
)
316 (buffer-substring start
318 (+ start
(length magic
)))))
320 (let ((can-append (jka-compr-info-can-append info
))
321 (compress-program (jka-compr-info-compress-program info
))
322 (compress-message (jka-compr-info-compress-message info
))
323 (compress-args (jka-compr-info-compress-args info
))
324 (base-name (file-name-nondirectory visit-file
))
325 temp-file temp-buffer
326 ;; we need to leave `last-coding-system-used' set to its
327 ;; value after calling write-region the first time, so
328 ;; that `basic-save-buffer' sees the right value.
329 (coding-system-used last-coding-system-used
))
332 (error "No compression program defined"))
334 (setq temp-buffer
(get-buffer-create " *jka-compr-wr-temp*"))
335 (with-current-buffer temp-buffer
336 (widen) (erase-buffer))
340 (file-exists-p filename
))
342 (let* ((local-copy (file-local-copy filename
))
343 (local-file (or local-copy filename
)))
345 (setq temp-file local-file
))
347 (setq temp-file
(jka-compr-make-temp-name)))
351 (message "%s %s..." compress-message base-name
))
353 (jka-compr-run-real-handler 'write-region
354 (list start end temp-file t
'dont
))
355 ;; save value used by the real write-region
356 (setq coding-system-used last-coding-system-used
)
358 ;; Here we must read the output of compress program as is
359 ;; without any code conversion.
360 (let ((coding-system-for-read 'no-conversion
))
361 (jka-compr-call-process compress-program
362 (concat compress-message
369 (with-current-buffer temp-buffer
370 (let ((coding-system-for-write 'no-conversion
))
371 (if (memq system-type
'(ms-dos windows-nt
))
372 (setq buffer-file-type t
) )
373 (jka-compr-run-real-handler 'write-region
374 (list (point-min) (point-max)
376 (and append can-append
) 'dont
))
379 (jka-compr-delete-temp-file temp-file
)
383 (message "%s %s...done" compress-message base-name
))
387 (setq buffer-file-name filename
)
388 (setq jka-compr-really-do-compress t
)
389 (set-visited-file-modtime))
391 (setq buffer-file-name visit
)
392 (let ((buffer-file-name filename
))
393 (set-visited-file-modtime))))
395 (and (or (eq visit t
)
398 (message "Wrote %s" visit-file
))
400 ;; ensure `last-coding-system-used' has an appropriate value
401 (setq last-coding-system-used coding-system-used
)
405 (jka-compr-run-real-handler 'write-region
406 (list start end filename append visit
)))))
409 (defun jka-compr-insert-file-contents (file &optional visit beg end replace
)
410 (barf-if-buffer-read-only)
414 (error "Attempt to visit less than an entire file"))
416 (let* ((filename (expand-file-name file
))
417 (info (jka-compr-get-compression-info filename
)))
421 (let ((uncompress-message (jka-compr-info-uncompress-message info
))
422 (uncompress-program (jka-compr-info-uncompress-program info
))
423 (uncompress-args (jka-compr-info-uncompress-args info
))
424 (base-name (file-name-nondirectory filename
))
427 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))
431 (setq local-file
(or local-copy filename
))
435 (setq buffer-file-name filename
))
437 (unwind-protect ; to make sure local-copy gets deleted
443 (message "%s %s..." uncompress-message base-name
))
445 (condition-case error-code
447 (let ((coding-system-for-read 'no-conversion
))
449 (goto-char (point-min)))
452 (jka-compr-partial-uncompress uncompress-program
453 (concat uncompress-message
461 ;; If visiting, bind off buffer-file-name so that
462 ;; file-locking will not ask whether we should
463 ;; really edit the buffer.
464 (let ((buffer-file-name
465 (if visit nil buffer-file-name
)))
466 (jka-compr-call-process uncompress-program
467 (concat uncompress-message
473 (setq size
(- (point) start
))
475 (delete-region (point) (point-max)))
478 ;; If the file we wanted to uncompress does not exist,
479 ;; handle that according to VISIT as `insert-file-contents'
480 ;; would, maybe signaling the same error it normally would.
481 (if (and (eq (car error-code
) 'file-error
)
482 (eq (nth 3 error-code
) local-file
))
484 (setq notfound error-code
)
486 (cons "Opening input file"
487 (nthcdr 2 error-code
))))
488 ;; If the uncompression program can't be found,
489 ;; signal that as a non-file error
490 ;; so that find-file-noselect-1 won't handle it.
491 (if (and (eq (car error-code
) 'file-error
)
492 (equal (cadr error-code
) "Searching for program"))
493 (error "Uncompression program `%s' not found"
495 (signal (car error-code
) (cdr error-code
))))))
499 (file-exists-p local-copy
)
500 (delete-file local-copy
)))
503 (decode-coding-inserted-region
504 (point) (+ (point) size
)
505 (jka-compr-byte-compiler-base-file-name file
)
506 visit beg end replace
))
512 (setq buffer-file-name filename
)
513 (setq jka-compr-really-do-compress t
)
514 (set-visited-file-modtime)))
518 (message "%s %s...done" uncompress-message base-name
))
524 (cons "Opening input file" (nth 2 notfound
))))
526 ;; This is done in insert-file-contents after we return.
527 ;; That is a little weird, but better to go along with it now
528 ;; than to change it now.
530 ;;; ;; Run the functions that insert-file-contents would.
531 ;;; (let ((p after-insert-file-functions)
534 ;;; (setq insval (funcall (car p) size))
537 ;;; (or (integerp insval)
538 ;;; (signal 'wrong-type-argument
539 ;;; (list 'integerp insval)))
540 ;;; (setq size insval)))
541 ;;; (setq p (cdr p))))
543 (or (jka-compr-info-compress-program info
)
544 (message "You can't save this buffer because compression program is not defined"))
546 (list filename size
))
548 (jka-compr-run-real-handler 'insert-file-contents
549 (list file visit beg end replace
)))))
552 (defun jka-compr-file-local-copy (file)
553 (let* ((filename (expand-file-name file
))
554 (info (jka-compr-get-compression-info filename
)))
558 (let ((uncompress-message (jka-compr-info-uncompress-message info
))
559 (uncompress-program (jka-compr-info-uncompress-program info
))
560 (uncompress-args (jka-compr-info-uncompress-args info
))
561 (base-name (file-name-nondirectory filename
))
563 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))
564 (temp-file (jka-compr-make-temp-name t
))
565 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
569 (setq local-file
(or local-copy filename
))
573 (with-current-buffer temp-buffer
577 (message "%s %s..." uncompress-message base-name
))
579 ;; Here we must read the output of uncompress program
580 ;; and write it to TEMP-FILE without any code
581 ;; conversion. An appropriate code conversion (if
582 ;; necessary) is done by the later I/O operation
584 (let ((coding-system-for-read 'no-conversion
)
585 (coding-system-for-write 'no-conversion
))
587 (jka-compr-call-process uncompress-program
588 (concat uncompress-message
597 (message "%s %s...done" uncompress-message base-name
))
600 (point-min) (point-max) temp-file nil
'dont
)))
604 (file-exists-p local-copy
)
605 (delete-file local-copy
))
607 (kill-buffer temp-buffer
))
611 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))))
614 ;;; Support for loading compressed files.
615 (defun jka-compr-load (file &optional noerror nomessage nosuffix
)
616 "Documented as original."
618 (let* ((local-copy (jka-compr-file-local-copy file
))
619 (load-file (or local-copy file
)))
623 (let (inhibit-file-name-operation
624 inhibit-file-name-handlers
)
626 (message "Loading %s..." file
))
628 (let ((load-force-doc-strings t
))
629 (load load-file noerror t t
))
631 (message "Loading %s...done." file
))
632 ;; Fix up the load history to point at the right library.
633 (let ((l (assoc load-file load-history
)))
634 ;; Remove .gz and .elc?.
635 (while (file-name-extension file
)
636 (setq file
(file-name-sans-extension file
)))
639 (jka-compr-delete-temp-file local-copy
))
643 (defun jka-compr-byte-compiler-base-file-name (file)
644 (let ((info (jka-compr-get-compression-info file
)))
645 (if (and info
(jka-compr-info-strip-extension info
))
647 (substring file
0 (string-match (jka-compr-info-regexp info
) file
)))
650 (put 'write-region
'jka-compr
'jka-compr-write-region
)
651 (put 'insert-file-contents
'jka-compr
'jka-compr-insert-file-contents
)
652 (put 'file-local-copy
'jka-compr
'jka-compr-file-local-copy
)
653 (put 'load
'jka-compr
'jka-compr-load
)
654 (put 'byte-compiler-base-file-name
'jka-compr
655 'jka-compr-byte-compiler-base-file-name
)
658 (defvar jka-compr-inhibit nil
659 "Non-nil means inhibit automatic uncompression temporarily.
660 Lisp programs can bind this to t to do that.
661 It is not recommended to set this variable permanently to anything but nil.")
664 (defun jka-compr-handler (operation &rest args
)
666 (let ((jka-op (get operation
'jka-compr
)))
667 (if (and jka-op
(not jka-compr-inhibit
))
669 (jka-compr-run-real-handler operation args
)))))
671 ;; If we are given an operation that we don't handle,
672 ;; call the Emacs primitive for that operation,
673 ;; and manipulate the inhibit variables
674 ;; to prevent the primitive from calling our handler again.
675 (defun jka-compr-run-real-handler (operation args
)
676 (let ((inhibit-file-name-handlers
677 (cons 'jka-compr-handler
678 (and (eq inhibit-file-name-operation operation
)
679 inhibit-file-name-handlers
)))
680 (inhibit-file-name-operation operation
))
681 (apply operation args
)))
684 (defun jka-compr-uninstall ()
685 "Uninstall jka-compr.
686 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
687 and `inhibit-first-line-modes-suffixes' that were added
688 by `jka-compr-installed'."
689 ;; Delete from inhibit-first-line-modes-suffixes
690 ;; what jka-compr-install added.
692 (function (lambda (x)
693 (and (jka-compr-info-strip-extension x
)
694 (setq inhibit-first-line-modes-suffixes
695 (delete (jka-compr-info-regexp x
)
696 inhibit-first-line-modes-suffixes
)))))
697 jka-compr-compression-info-list
)
699 (let* ((fnha (cons nil file-name-handler-alist
))
703 (if (eq (cdr (car (cdr last
))) 'jka-compr-handler
)
704 (setcdr last
(cdr (cdr last
)))
705 (setq last
(cdr last
))))
707 (setq file-name-handler-alist
(cdr fnha
)))
709 (let* ((ama (cons nil auto-mode-alist
))
714 (setq entry
(car (cdr last
)))
715 (if (or (member entry jka-compr-mode-alist-additions
)
716 (and (consp (cdr entry
))
717 (eq (nth 2 entry
) 'jka-compr
)))
718 (setcdr last
(cdr (cdr last
)))
719 (setq last
(cdr last
))))
721 (setq auto-mode-alist
(cdr ama
)))
723 (let* ((ama (cons nil file-coding-system-alist
))
728 (setq entry
(car (cdr last
)))
729 (if (member entry jka-compr-added-to-file-coding-system-alist
)
730 (setcdr last
(cdr (cdr last
)))
731 (setq last
(cdr last
))))
733 (setq file-coding-system-alist
(cdr ama
)))
735 ;; Remove the suffixes that were added by jka-compr.
737 (re (jka-compr-build-file-regexp)))
738 (dolist (suffix load-suffixes
)
739 (unless (string-match re suffix
)
740 (push suffix suffixes
)))
741 (setq load-suffixes
(nreverse suffixes
))))
745 ;;; arch-tag: 3f15b630-e9a7-46c4-a22a-94afdde86ebc
746 ;;; jka-compr.el ends here