1 ;;; jka-compr.el --- reading/writing/loading compressed files
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
5 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
9 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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).
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.
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
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
67 ;; jka-compr can coexist with crypt++ 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
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.
92 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
93 ;; Version 18 of Emacs.
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.
103 (defcustom jka-compr-shell
"sh"
104 "*Shell to be used for calling compression programs.
105 The value of this variable only matters if you want to discard the
106 stderr of a compression/decompression program (see the documentation
107 for `jka-compr-compression-info-list')."
111 (defvar jka-compr-use-shell
112 (not (memq system-type
'(ms-dos windows-nt
))))
114 (defvar jka-compr-really-do-compress nil
115 "Non-nil in a buffer whose visited file was uncompressed on visiting it.
116 This means compress the data on writing the file, even if the
117 data appears to be compressed already.")
118 (make-variable-buffer-local 'jka-compr-really-do-compress
)
119 (put 'jka-compr-really-do-compress
'permanent-local t
)
121 ;;; Functions for accessing the return value of jka-compr-get-compression-info
122 (defun jka-compr-info-regexp (info) (aref info
0))
123 (defun jka-compr-info-compress-message (info) (aref info
1))
124 (defun jka-compr-info-compress-program (info) (aref info
2))
125 (defun jka-compr-info-compress-args (info) (aref info
3))
126 (defun jka-compr-info-uncompress-message (info) (aref info
4))
127 (defun jka-compr-info-uncompress-program (info) (aref info
5))
128 (defun jka-compr-info-uncompress-args (info) (aref info
6))
129 (defun jka-compr-info-can-append (info) (aref info
7))
130 (defun jka-compr-info-strip-extension (info) (aref info
8))
131 (defun jka-compr-info-file-magic-bytes (info) (aref info
9))
134 (defun jka-compr-get-compression-info (filename)
135 "Return information about the compression scheme of FILENAME.
136 The determination as to which compression scheme, if any, to use is
137 based on the filename itself and `jka-compr-compression-info-list'."
138 (catch 'compression-info
139 (let ((case-fold-search nil
))
141 (function (lambda (x)
142 (and (string-match (jka-compr-info-regexp x
) filename
)
143 (throw 'compression-info x
))))
144 jka-compr-compression-info-list
)
148 (put 'compression-error
'error-conditions
'(compression-error file-error error
))
151 (defvar jka-compr-acceptable-retval-list
'(0 2 141))
154 (defun jka-compr-error (prog args infile message
&optional errfile
)
156 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
157 (curbuf (current-buffer)))
158 (with-current-buffer errbuf
159 (widen) (erase-buffer)
160 (insert (format "Error while executing \"%s %s < %s\"\n\n"
162 (mapconcat 'identity args
" ")
166 (insert-file-contents errfile
)))
167 (display-buffer errbuf
))
169 (signal 'compression-error
170 (list "Opening input file" (format "error %s" message
) infile
)))
173 (defcustom jka-compr-dd-program
"/bin/dd"
174 "How to invoke `dd'."
179 (defvar jka-compr-dd-blocksize
256)
182 (defun jka-compr-partial-uncompress (prog message args infile beg len
)
183 "Call program PROG with ARGS args taking input from INFILE.
184 Fourth and fifth args, BEG and LEN, specify which part of the output
185 to keep: LEN chars starting BEG chars from the beginning."
186 (let ((start (point))
188 (if (and jka-compr-use-shell jka-compr-dd-program
)
189 ;; Put the uncompression output through dd
190 ;; to discard the part we don't want.
191 (let ((skip (/ beg jka-compr-dd-blocksize
))
192 (err-file (jka-compr-make-temp-name))
194 ;; Update PREFIX based on the text that we won't read in.
195 (setq prefix
(- beg
(* skip jka-compr-dd-blocksize
))
196 count
(and len
(1+ (/ (+ len prefix
) jka-compr-dd-blocksize
))))
198 (or (memq (call-process
199 jka-compr-shell infile t nil
"-c"
201 "%s %s 2> %s | %s bs=%d skip=%d %s 2> %s"
203 (mapconcat 'identity args
" ")
206 jka-compr-dd-blocksize
208 ;; dd seems to be unreliable about
209 ;; providing the last block. So, always
210 ;; read one more than you think you need.
211 (if count
(format "count=%d" (1+ count
)) "")
213 jka-compr-acceptable-retval-list
)
214 (jka-compr-error prog args infile message err-file
))
215 (jka-compr-delete-temp-file err-file
)))
216 ;; Run the uncompression program directly.
217 ;; We get the whole file and must delete what we don't want.
218 (jka-compr-call-process prog message infile t nil args
))
220 ;; Delete the stuff after what we want, if there is any.
223 (< (+ start prefix len
) (point))
224 (delete-region (+ start prefix len
) (point)))
226 ;; Delete the stuff before what we want.
227 (delete-region start
(+ start prefix
))))
230 (defun jka-compr-call-process (prog message infile output temp args
)
231 (if jka-compr-use-shell
233 (let ((err-file (jka-compr-make-temp-name))
234 (coding-system-for-read (or coding-system-for-read
'undecided
))
235 (coding-system-for-write 'no-conversion
))
240 (call-process jka-compr-shell infile
241 (if (stringp output
) nil output
)
244 (format "%s %s 2> %s %s"
246 (mapconcat 'identity args
" ")
251 jka-compr-acceptable-retval-list
)
253 (jka-compr-error prog args infile message err-file
))
255 (jka-compr-delete-temp-file err-file
)))
261 (if (stringp output
) temp output
)
264 (jka-compr-error prog args infile message
))
266 (and (stringp output
)
267 (with-current-buffer temp
268 (write-region (point-min) (point-max) output
)
272 ;;; Support for temp files. Much of this was inspired if not lifted
275 (defcustom jka-compr-temp-name-template
276 (expand-file-name "jka-com" temporary-file-directory
)
277 "Prefix added to all temp files created by jka-compr.
278 There should be no more than seven characters after the final `/'."
282 (defun jka-compr-make-temp-name (&optional local-copy
)
283 "This routine will return the name of a new file."
284 (make-temp-file jka-compr-temp-name-template
))
286 (defalias 'jka-compr-delete-temp-file
'delete-file
)
289 (defun jka-compr-write-region (start end file
&optional append visit
)
290 (let* ((filename (expand-file-name file
))
291 (visit-file (if (stringp visit
) (expand-file-name visit
) filename
))
292 (info (jka-compr-get-compression-info visit-file
))
293 (magic (and info
(jka-compr-info-file-magic-bytes info
))))
295 ;; If START is nil, use the whole buffer.
297 (setq start
1 end
(1+ (buffer-size))))
299 ;; If we uncompressed this file when visiting it,
300 ;; then recompress it when writing it
301 ;; even if the contents look compressed already.
302 (if (and jka-compr-really-do-compress
304 (eq end
(1+ (buffer-size))))
308 ;; If the contents to be written out
309 ;; are properly compressed already,
310 ;; don't try to compress them over again.
312 (equal (if (stringp start
)
313 (substring start
0 (min (length start
)
315 (buffer-substring start
317 (+ start
(length magic
)))))
319 (let ((can-append (jka-compr-info-can-append info
))
320 (compress-program (jka-compr-info-compress-program info
))
321 (compress-message (jka-compr-info-compress-message info
))
322 (compress-args (jka-compr-info-compress-args info
))
323 (base-name (file-name-nondirectory visit-file
))
324 temp-file temp-buffer
325 ;; we need to leave `last-coding-system-used' set to its
326 ;; value after calling write-region the first time, so
327 ;; that `basic-save-buffer' sees the right value.
328 (coding-system-used last-coding-system-used
))
331 (error "No compression program defined"))
333 (setq temp-buffer
(get-buffer-create " *jka-compr-wr-temp*"))
334 (with-current-buffer temp-buffer
335 (widen) (erase-buffer))
339 (file-exists-p filename
))
341 (let* ((local-copy (file-local-copy filename
))
342 (local-file (or local-copy filename
)))
344 (setq temp-file local-file
))
346 (setq temp-file
(jka-compr-make-temp-name)))
350 (message "%s %s..." compress-message base-name
))
352 (jka-compr-run-real-handler 'write-region
353 (list start end temp-file t
'dont
))
354 ;; save value used by the real write-region
355 (setq coding-system-used last-coding-system-used
)
357 ;; Here we must read the output of compress program as is
358 ;; without any code conversion.
359 (let ((coding-system-for-read 'no-conversion
))
360 (jka-compr-call-process compress-program
361 (concat compress-message
368 (with-current-buffer temp-buffer
369 (let ((coding-system-for-write 'no-conversion
))
370 (if (memq system-type
'(ms-dos windows-nt
))
371 (setq buffer-file-type t
) )
372 (jka-compr-run-real-handler 'write-region
373 (list (point-min) (point-max)
375 (and append can-append
) 'dont
))
378 (jka-compr-delete-temp-file temp-file
)
382 (message "%s %s...done" compress-message base-name
))
386 (setq buffer-file-name filename
)
387 (setq jka-compr-really-do-compress t
)
388 (set-visited-file-modtime))
390 (setq buffer-file-name visit
)
391 (let ((buffer-file-name filename
))
392 (set-visited-file-modtime))))
394 (and (or (eq visit t
)
397 (message "Wrote %s" visit-file
))
399 ;; ensure `last-coding-system-used' has an appropriate value
400 (setq last-coding-system-used coding-system-used
)
404 (jka-compr-run-real-handler 'write-region
405 (list start end filename append visit
)))))
408 (defun jka-compr-insert-file-contents (file &optional visit beg end replace
)
409 (barf-if-buffer-read-only)
413 (error "Attempt to visit less than an entire file"))
415 (let* ((filename (expand-file-name file
))
416 (info (jka-compr-get-compression-info filename
)))
420 (let ((uncompress-message (jka-compr-info-uncompress-message info
))
421 (uncompress-program (jka-compr-info-uncompress-program info
))
422 (uncompress-args (jka-compr-info-uncompress-args info
))
423 (base-name (file-name-nondirectory filename
))
426 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))
430 (setq local-file
(or local-copy filename
))
434 (setq buffer-file-name filename
))
436 (unwind-protect ; to make sure local-copy gets deleted
442 (message "%s %s..." uncompress-message base-name
))
444 (condition-case error-code
446 (let ((coding-system-for-read 'no-conversion
))
448 (goto-char (point-min)))
451 (jka-compr-partial-uncompress uncompress-program
452 (concat uncompress-message
460 ;; If visiting, bind off buffer-file-name so that
461 ;; file-locking will not ask whether we should
462 ;; really edit the buffer.
463 (let ((buffer-file-name
464 (if visit nil buffer-file-name
)))
465 (jka-compr-call-process uncompress-program
466 (concat uncompress-message
472 (setq size
(- (point) start
))
474 (delete-region (point) (point-max)))
477 (if (and (eq (car error-code
) 'file-error
)
478 (eq (nth 3 error-code
) local-file
))
480 (setq notfound error-code
)
482 (cons "Opening input file"
483 (nthcdr 2 error-code
))))
484 (signal (car error-code
) (cdr error-code
))))))
488 (file-exists-p local-copy
)
489 (delete-file local-copy
)))
492 (decode-coding-inserted-region
493 (point) (+ (point) size
)
494 (jka-compr-byte-compiler-base-file-name file
)
495 visit beg end replace
))
501 (setq buffer-file-name filename
)
502 (setq jka-compr-really-do-compress t
)
503 (set-visited-file-modtime)))
507 (message "%s %s...done" uncompress-message base-name
))
513 (cons "Opening input file" (nth 2 notfound
))))
515 ;; This is done in insert-file-contents after we return.
516 ;; That is a little weird, but better to go along with it now
517 ;; than to change it now.
519 ;;; ;; Run the functions that insert-file-contents would.
520 ;;; (let ((p after-insert-file-functions)
523 ;;; (setq insval (funcall (car p) size))
526 ;;; (or (integerp insval)
527 ;;; (signal 'wrong-type-argument
528 ;;; (list 'integerp insval)))
529 ;;; (setq size insval)))
530 ;;; (setq p (cdr p))))
532 (or (jka-compr-info-compress-program info
)
533 (message "You can't save this buffer because compression program is not defined"))
535 (list filename size
))
537 (jka-compr-run-real-handler 'insert-file-contents
538 (list file visit beg end replace
)))))
541 (defun jka-compr-file-local-copy (file)
542 (let* ((filename (expand-file-name file
))
543 (info (jka-compr-get-compression-info filename
)))
547 (let ((uncompress-message (jka-compr-info-uncompress-message info
))
548 (uncompress-program (jka-compr-info-uncompress-program info
))
549 (uncompress-args (jka-compr-info-uncompress-args info
))
550 (base-name (file-name-nondirectory filename
))
552 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))
553 (temp-file (jka-compr-make-temp-name t
))
554 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
558 (setq local-file
(or local-copy filename
))
562 (with-current-buffer temp-buffer
566 (message "%s %s..." uncompress-message base-name
))
568 ;; Here we must read the output of uncompress program
569 ;; and write it to TEMP-FILE without any code
570 ;; conversion. An appropriate code conversion (if
571 ;; necessary) is done by the later I/O operation
573 (let ((coding-system-for-read 'no-conversion
)
574 (coding-system-for-write 'no-conversion
))
576 (jka-compr-call-process uncompress-program
577 (concat uncompress-message
586 (message "%s %s...done" uncompress-message base-name
))
589 (point-min) (point-max) temp-file nil
'dont
)))
593 (file-exists-p local-copy
)
594 (delete-file local-copy
))
596 (kill-buffer temp-buffer
))
600 (jka-compr-run-real-handler 'file-local-copy
(list filename
)))))
603 ;;; Support for loading compressed files.
604 (defun jka-compr-load (file &optional noerror nomessage nosuffix
)
605 "Documented as original."
607 (let* ((local-copy (jka-compr-file-local-copy file
))
608 (load-file (or local-copy file
)))
612 (let (inhibit-file-name-operation
613 inhibit-file-name-handlers
)
615 (message "Loading %s..." file
))
617 (let ((load-force-doc-strings t
))
618 (load load-file noerror t t
))
620 (message "Loading %s...done." file
))
621 ;; Fix up the load history to point at the right library.
622 (let ((l (assoc load-file load-history
)))
623 ;; Remove .gz and .elc?.
624 (while (file-name-extension file
)
625 (setq file
(file-name-sans-extension file
)))
628 (jka-compr-delete-temp-file local-copy
))
632 (defun jka-compr-byte-compiler-base-file-name (file)
633 (let ((info (jka-compr-get-compression-info file
)))
634 (if (and info
(jka-compr-info-strip-extension info
))
636 (substring file
0 (string-match (jka-compr-info-regexp info
) file
)))
639 (put 'write-region
'jka-compr
'jka-compr-write-region
)
640 (put 'insert-file-contents
'jka-compr
'jka-compr-insert-file-contents
)
641 (put 'file-local-copy
'jka-compr
'jka-compr-file-local-copy
)
642 (put 'load
'jka-compr
'jka-compr-load
)
643 (put 'byte-compiler-base-file-name
'jka-compr
644 'jka-compr-byte-compiler-base-file-name
)
647 (defvar jka-compr-inhibit nil
648 "Non-nil means inhibit automatic uncompression temporarily.
649 Lisp programs can bind this to t to do that.
650 It is not recommended to set this variable permanently to anything but nil.")
653 (defun jka-compr-handler (operation &rest args
)
655 (let ((jka-op (get operation
'jka-compr
)))
656 (if (and jka-op
(not jka-compr-inhibit
))
658 (jka-compr-run-real-handler operation args
)))))
660 ;; If we are given an operation that we don't handle,
661 ;; call the Emacs primitive for that operation,
662 ;; and manipulate the inhibit variables
663 ;; to prevent the primitive from calling our handler again.
664 (defun jka-compr-run-real-handler (operation args
)
665 (let ((inhibit-file-name-handlers
666 (cons 'jka-compr-handler
667 (and (eq inhibit-file-name-operation operation
)
668 inhibit-file-name-handlers
)))
669 (inhibit-file-name-operation operation
))
670 (apply operation args
)))
673 (defun jka-compr-uninstall ()
674 "Uninstall jka-compr.
675 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
676 and `inhibit-first-line-modes-suffixes' that were added
677 by `jka-compr-installed'."
678 ;; Delete from inhibit-first-line-modes-suffixes
679 ;; what jka-compr-install added.
681 (function (lambda (x)
682 (and (jka-compr-info-strip-extension x
)
683 (setq inhibit-first-line-modes-suffixes
684 (delete (jka-compr-info-regexp x
)
685 inhibit-first-line-modes-suffixes
)))))
686 jka-compr-compression-info-list
)
688 (let* ((fnha (cons nil file-name-handler-alist
))
692 (if (eq (cdr (car (cdr last
))) 'jka-compr-handler
)
693 (setcdr last
(cdr (cdr last
)))
694 (setq last
(cdr last
))))
696 (setq file-name-handler-alist
(cdr fnha
)))
698 (let* ((ama (cons nil auto-mode-alist
))
703 (setq entry
(car (cdr last
)))
704 (if (or (member entry jka-compr-mode-alist-additions
)
705 (and (consp (cdr entry
))
706 (eq (nth 2 entry
) 'jka-compr
)))
707 (setcdr last
(cdr (cdr last
)))
708 (setq last
(cdr last
))))
710 (setq auto-mode-alist
(cdr ama
)))
712 (let* ((ama (cons nil file-coding-system-alist
))
717 (setq entry
(car (cdr last
)))
718 (if (member entry jka-compr-added-to-file-coding-system-alist
)
719 (setcdr last
(cdr (cdr last
)))
720 (setq last
(cdr last
))))
722 (setq file-coding-system-alist
(cdr ama
)))
724 ;; Remove the suffixes that were added by jka-compr.
726 (re (jka-compr-build-file-regexp)))
727 (dolist (suffix load-suffixes
)
728 (unless (string-match re suffix
)
729 (push suffix suffixes
)))
730 (setq load-suffixes
(nreverse suffixes
))))
734 ;;; arch-tag: 3f15b630-e9a7-46c4-a22a-94afdde86ebc
735 ;;; jka-compr.el ends here