1 ;;; jka-compr.el --- reading/writing/loading compressed files
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
6 ;; Maintainer: XEmacs Development Team
9 ;; This file is part of XEmacs.
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 ;; Synched up with: FSF 20.3.10.1
30 ;; This package implements low-level support for reading, writing,
31 ;; and loading compressed files. It hooks into the low-level file
32 ;; I/O functions (including write-region and insert-file-contents) so
33 ;; that they automatically compress or uncompress a file if the file
34 ;; appears to need it (based on the extension of the file name).
35 ;; Packages like Rmail, VM, GNUS, and Info should be able to work
36 ;; with compressed files without modification.
41 ;; To use jka-compr, simply load this package, and edit as usual.
42 ;; Its operation should be transparent to the user (except for
43 ;; messages appearing when a file is being compressed or
46 ;; The variable, jka-compr-compression-info-list can be used to
47 ;; customize jka-compr to work with other compression programs.
48 ;; The default value of this variable allows jka-compr to work with
49 ;; Unix compress and gzip.
51 ;; If you are concerned about the stderr output of gzip and other
52 ;; compression/decompression programs showing up in your buffers, you
53 ;; should set the discard-error flag in the compression-info-list.
54 ;; This will cause the stderr of all programs to be discarded.
55 ;; However, it also causes emacs to call compression/uncompression
56 ;; programs through a shell (which is specified by jka-compr-shell).
57 ;; This may be a drag if, on your system, starting up a shell is
60 ;; If you don't want messages about compressing and decompressing
61 ;; to show up in the echo area, you can set the compress-name and
62 ;; decompress-name fields of the jka-compr-compression-info-list to
69 ;; jka-compr can coexist with crypt++ if you take all the decompression
70 ;; entries out of the crypt-encoding-list. Clearly problems will arise if
71 ;; you have two programs trying to compress/decompress files. jka-compr
72 ;; will not "work with" crypt++ in the following sense: you won't be able to
73 ;; decode encrypted compressed files--that is, files that have been
74 ;; compressed then encrypted (in that order). Theoretically, crypt++ and
75 ;; jka-compr could properly handle a file that has been encrypted then
76 ;; compressed, but there is little point in trying to compress an encrypted
83 ;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people
84 ;; have made helpful suggestions, reported bugs, and even fixed bugs in
85 ;; jka-compr. I recall the following people as being particularly helpful.
94 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
95 ;; Version 18 of Emacs.
97 ;; After I had made progress on the original jka-compr for V18, I learned of a
98 ;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly
99 ;; what I was trying to do. I looked over the jam-zcat source code and
100 ;; probably got some ideas from it.
105 (defgroup compression nil
106 "Data compression utilities"
109 (defgroup jka-compr nil
110 "jka-compr customization"
114 (defcustom 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')."
122 (defvar jka-compr-use-shell
123 (not (memq system-type '(ms-dos windows-nt))))
125 ;;; I have this defined so that .Z files are assumed to be in unix
126 ;;; compress format; and .gz files, in gzip format, and .bz2 files in bzip fmt.
127 (defcustom jka-compr-compression-info-list
129 ;; compr-message compr-prog compr-args
130 ;; uncomp-message uncomp-prog uncomp-args
131 ;; can-append auto-mode-flag]
132 '(["\\.Z\\(~\\|\\.~[0-9]+~\\)?\\'"
133 "compressing" "compress" ("-c")
134 "uncompressing" "uncompress" ("-c")
136 ["\\.bz2\\(~\\|\\.~[0-9]+~\\)?\\'" ; XEmacs change
137 "bzip2ing" "bzip2" nil
138 "unbzip2ing" "bzip2" ("-d")
141 "zipping" "gzip" ("-c" "-q")
142 "unzipping" "gzip" ("-c" "-q" "-d")
144 ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'"
145 "zipping" "gzip" ("-c" "-q")
146 "unzipping" "gzip" ("-c" "-q" "-d")
149 "List of vectors that describe available compression techniques.
150 Each element, which describes a compression technique, is a vector of
151 the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS
152 UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS
153 APPEND-FLAG EXTENSION], where:
155 regexp is a regexp that matches filenames that are
156 compressed with this format
158 compress-msg is the message to issue to the user when doing this
159 type of compression (nil means no message)
161 compress-program is a program that performs this compression
163 compress-args is a list of args to pass to the compress program
165 uncompress-msg is the message to issue to the user when doing this
166 type of uncompression (nil means no message)
168 uncompress-program is a program that performs this compression
170 uncompress-args is a list of args to pass to the uncompress program
172 append-flag is non-nil if this compression technique can be
175 auto-mode flag non-nil means strip the regexp from file names
176 before attempting to set the mode.
178 Because of the way `call-process' is defined, discarding the stderr output of
179 a program adds the overhead of starting a shell each time the program is
181 :type '(repeat (vector :tag "Compression Technique" ; XEmacs change
183 (choice :tag "Compress Message"
184 (string :format "%v")
185 (const :tag "No Message" nil))
186 (string :tag "Compress Program")
187 (repeat :tag "Compress Arguments" string)
188 (choice :tag "Uncompress Message"
189 (string :format "%v")
190 (const :tag "No Message" nil))
191 (string :tag "Uncompress Program")
192 (repeat :tag "Uncompress Arguments" string)
193 (boolean :tag "Append")
194 (boolean :tag "Auto Mode")))
197 (defvar jka-compr-mode-alist-additions
198 (list (cons "\\.tgz\\'" 'tar-mode))
199 "A list of pairs to add to `auto-mode-alist' when jka-compr is installed.")
201 ;; List of all the elements we actually added to file-coding-system-alist.
202 (defvar jka-compr-added-to-file-coding-system-alist nil)
204 (defvar jka-compr-file-name-handler-entry
206 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.")
208 ;;; Functions for accessing the return value of jka-compr-get-compression-info
209 (defun jka-compr-info-regexp (info) (aref info 0))
210 (defun jka-compr-info-compress-message (info) (aref info 1))
211 (defun jka-compr-info-compress-program (info) (aref info 2))
212 (defun jka-compr-info-compress-args (info) (aref info 3))
213 (defun jka-compr-info-uncompress-message (info) (aref info 4))
214 (defun jka-compr-info-uncompress-program (info) (aref info 5))
215 (defun jka-compr-info-uncompress-args (info) (aref info 6))
216 (defun jka-compr-info-can-append (info) (aref info 7))
217 (defun jka-compr-info-strip-extension (info) (aref info 8))
220 (defun jka-compr-get-compression-info (filename)
221 "Return information about the compression scheme of FILENAME.
222 The determination as to which compression scheme, if any, to use is
223 based on the filename itself and `jka-compr-compression-info-list'."
224 (catch 'compression-info
225 (let ((case-fold-search nil))
227 (function (lambda (x)
228 (and (string-match (jka-compr-info-regexp x) filename)
229 (throw 'compression-info x))))
230 jka-compr-compression-info-list)
235 (defmacro jka-value-if-bound (symbol)
236 `(if (boundp (quote ,symbol)) ,symbol))
239 (define-error 'compression-error "Compression error" 'file-error)
241 (defvar jka-compr-acceptable-retval-list '(0 2 141))
244 (defun jka-compr-error (prog args infile message &optional errfile)
246 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
247 (curbuf (current-buffer)))
248 (with-current-buffer errbuf
249 (widen) (erase-buffer)
250 (insert (format "Error while executing \"%s %s < %s\"\n\n"
252 (mapconcat 'identity args " ")
256 (insert-file-contents errfile)))
257 (display-buffer errbuf))
259 (signal 'compression-error
260 (list "Opening input file" (format "error %s" message) infile)))
263 (defvar jka-compr-dd-program
267 (defvar jka-compr-dd-blocksize 256)
270 (defun jka-compr-partial-uncompress (prog message args infile beg len)
271 "Call program PROG with ARGS args taking input from INFILE.
272 Fourth and fifth args, BEG and LEN, specify which part of the output
273 to keep: LEN chars starting BEG chars from the beginning."
274 (let* ((skip (/ beg jka-compr-dd-blocksize))
275 (prefix (- beg (* skip jka-compr-dd-blocksize)))
276 (count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize))))
278 (err-file (jka-compr-make-temp-name))
279 (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null"
281 (mapconcat 'identity args " ")
284 jka-compr-dd-blocksize
286 ;; dd seems to be unreliable about
287 ;; providing the last block. So, always
288 ;; read one more than you think you need.
289 ;; XEmacs change -- de-ebolify
291 (concat "count=" (number-to-string (1+ count)))
295 (or (memq (call-process jka-compr-shell
298 jka-compr-acceptable-retval-list)
300 (jka-compr-error prog args infile message err-file))
302 (jka-compr-delete-temp-file err-file))
304 ;; Delete the stuff after what we want, if there is any.
307 (< (+ start prefix len) (point))
308 (delete-region (+ start prefix len) (point)))
310 ;; Delete the stuff before what we want.
311 (delete-region start (+ start prefix))))
314 (defun jka-compr-call-process (prog message infile output temp args)
315 (if jka-compr-use-shell
317 (let ((err-file (jka-compr-make-temp-name))
318 (coding-system-for-read (or
321 coding-system-for-read)
323 (coding-system-for-write 'binary))
328 (call-process jka-compr-shell infile
329 (if (stringp output) nil output)
332 (format "%s %s 2> %s %s"
334 (mapconcat 'identity args " ")
339 jka-compr-acceptable-retval-list)
341 (jka-compr-error prog args infile message err-file))
343 (jka-compr-delete-temp-file err-file)))
349 (if (stringp output) temp output)
352 (jka-compr-error prog args infile message))
354 (and (stringp output)
355 (with-current-buffer temp
356 (write-region (point-min) (point-max) output)
360 ;;; Support for temp files. Much of this was inspired if not lifted
363 (defcustom jka-compr-temp-name-template
364 (expand-file-name "jka-com" (temp-directory))
365 "Prefix added to all temp files created by jka-compr.
366 There should be no more than seven characters after the final `/'."
370 (defvar jka-compr-temp-name-table (make-vector 31 nil))
372 (defun jka-compr-make-temp-name (&optional local-copy)
373 "This routine will return the name of a new file."
376 (template (concat jka-compr-temp-name-template "aa"))
377 (lastpos (1- (length template)))
383 (aset template lastpos lastchar)
384 (setq file (concat (make-temp-name template) "#"))
385 (setq entry (intern file jka-compr-temp-name-table))
386 (if (or (get entry 'active)
387 (file-exists-p file))
390 (setq lastchar (1+ lastchar))
393 (setq prevchar (1+ prevchar))
396 (error "Can't allocate temp file.")
397 (aset template (1- lastpos) prevchar)))))
399 (put entry 'active (not local-copy))
400 (setq not-done nil)))
405 (defun jka-compr-delete-temp-file (temp)
407 (put (intern temp jka-compr-temp-name-table)
415 ;;; Now receives both `lockname' and `codesys' from Fwrite_region_internal
416 ;;; what makes it compatible with write-region
417 (defun jka-compr-write-region (start end file &optional append visit lockname coding-system)
418 (let* ((filename (expand-file-name file))
419 (visit-file (if (stringp visit) (expand-file-name visit) filename))
420 (info (jka-compr-get-compression-info visit-file)))
424 (let ((can-append (jka-compr-info-can-append info))
425 (compress-program (jka-compr-info-compress-program info))
426 (compress-message (jka-compr-info-compress-message info))
427 (uncompress-program (jka-compr-info-uncompress-program info))
428 (uncompress-message (jka-compr-info-uncompress-message info))
429 (compress-args (jka-compr-info-compress-args info))
430 (uncompress-args (jka-compr-info-uncompress-args info))
431 (base-name (file-name-nondirectory visit-file))
432 temp-file temp-buffer
433 ;; we need to leave `last-coding-system-used' set to its
434 ;; value after calling write-region the first time, so
435 ;; that `basic-save-buffer' sees the right value.
436 ;; XEmacs change: we don't have `last-coding-system-used'.
437 ;; (coding-system-used last-coding-system-used)
440 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
441 (with-current-buffer temp-buffer
442 (widen) (erase-buffer))
446 (file-exists-p filename))
448 (let* ((local-copy (file-local-copy filename))
449 (local-file (or local-copy filename)))
451 (setq temp-file local-file))
453 (setq temp-file (jka-compr-make-temp-name)))
457 (message "%s %s..." compress-message base-name))
459 (jka-compr-run-real-handler 'write-region
460 ;; XEmacs change: add lockname, c-s
461 (list start end temp-file t 'dont
462 lockname coding-system
465 ;; save value used by the real write-region
466 ;; without any code conversion.
467 (let ((coding-system-for-read 'binary))
468 (jka-compr-call-process compress-program
469 (concat compress-message
476 (with-current-buffer temp-buffer
477 (let ((coding-system-for-write 'binary))
478 (if (memq system-type '(ms-dos windows-nt))
479 (setq buffer-file-type t) )
480 (jka-compr-run-real-handler 'write-region
481 (list (point-min) (point-max)
483 (and append can-append) 'dont
484 lockname coding-system
487 (jka-compr-delete-temp-file temp-file)
491 (message "%s %s...done" compress-message base-name))
495 (setq buffer-file-name filename)
496 (set-visited-file-modtime))
498 (setq buffer-file-name visit)
499 (let ((buffer-file-name filename))
500 (set-visited-file-modtime))))
502 (and (or (eq visit t)
505 (message "Wrote %s" visit-file))
507 ;; ensure `last-coding-system-used' has an appropriate value
508 ;; XEmacs change: don't have `last-coding-system-used'
509 ;; (setq last-coding-system-used coding-system-used)
513 (jka-compr-run-real-handler 'write-region
514 (list start end filename append visit
516 lockname coding-system
520 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
521 "Insert contents of FILE in current buffer after point.
522 Returns list of absolute file name and length of data inserted.
524 If second argument VISIT is non-nil, the buffer's visited filename
525 and last save file modtime are set, and it is marked unmodified.
526 If visiting and the file does not exist, visiting is completed
527 before the error is signaled.
529 The optional third and fourth arguments BEG and END
530 specify what portion of the file to insert.
531 If VISIT is non-nil, BEG and END must be nil.
532 If optional fifth argument REPLACE is non-nil,
533 it means replace the current buffer contents (in the accessible portion)
534 with the file contents. This is better than simply deleting and inserting
535 the whole thing because (1) it preserves some marker positions
536 and (2) it puts less data in the undo list. (#### Is (2) true?)
538 Coding system determination differs from `insert-file-contents'. It is set
539 to 'undecided unless `coding-system-for-read' is bound. WARNING: That means
540 this function is likely broken under Mule. Use jka-compr at your own risk
542 (barf-if-buffer-read-only)
546 (error "Attempt to visit less than an entire file"))
548 (let* ((filename (expand-file-name file))
549 (info (jka-compr-get-compression-info filename)))
553 (let ((uncompress-message (jka-compr-info-uncompress-message info))
554 (uncompress-program (jka-compr-info-uncompress-program info))
555 (uncompress-args (jka-compr-info-uncompress-args info))
556 (base-name (file-name-nondirectory filename))
559 (jka-compr-run-real-handler 'file-local-copy (list filename)))
562 (coding-system-for-read
565 (jka-value-if-bound coding-system-for-read)
567 ;; XEmacs change; skip multibyte-mode crap
568 ; (and (null enable-multibyte-characters)
569 ; (or (auto-coding-alist-lookup
570 ; (jka-compr-byte-compiler-base-file-name file))
572 ; (let ((coding (find-operation-coding-system
573 ; 'insert-file-contents
574 ; (jka-compr-byte-compiler-base-file-name file))))
575 ; (and (consp coding) (car coding)))
577 (setq local-file (or local-copy filename))
581 (setq buffer-file-name filename))
583 (unwind-protect ; to make sure local-copy gets deleted
589 (message "%s %s..." uncompress-message base-name))
591 (condition-case error-code
595 (goto-char (point-min)))
598 (jka-compr-partial-uncompress uncompress-program
599 (concat uncompress-message
607 ;; If visiting, bind off buffer-file-name so that
608 ;; file-locking will not ask whether we should
609 ;; really edit the buffer.
610 (let ((buffer-file-name
611 (if visit nil buffer-file-name)))
612 (jka-compr-call-process uncompress-program
613 (concat uncompress-message
619 (setq size (- (point) start))
621 (delete-region (point) (point-max)))
624 (if (and (eq (car error-code) 'file-error)
625 (eq (nth 3 error-code) local-file))
627 (setq notfound error-code)
629 (cons "Opening input file"
630 (nthcdr 2 error-code))))
631 (signal (car error-code) (cdr error-code))))))
635 (file-exists-p local-copy)
636 (delete-file local-copy)))
642 (setq buffer-file-name filename)
643 (set-visited-file-modtime)))
647 (message "%s %s...done" uncompress-message base-name))
653 (cons "Opening input file" (nth 2 notfound))))
655 ;; This is done in insert-file-contents after we return.
656 ;; That is a little weird, but better to go along with it now
657 ;; than to change it now.
659 ;; Run the functions that insert-file-contents would.
660 ; (let ((p after-insert-file-functions)
663 ; (setq insval (funcall (car p) size))
666 ; (or (integerp insval)
667 ; (signal 'wrong-type-argument
668 ; (list 'integerp insval)))
669 ; (setq size insval)))
672 (list filename size))
674 (jka-compr-run-real-handler 'insert-file-contents
675 (list file visit beg end replace)))))
678 (defun jka-compr-file-local-copy (file)
679 (let* ((filename (expand-file-name file))
680 (info (jka-compr-get-compression-info filename)))
684 (let ((uncompress-message (jka-compr-info-uncompress-message info))
685 (uncompress-program (jka-compr-info-uncompress-program info))
686 (uncompress-args (jka-compr-info-uncompress-args info))
687 (base-name (file-name-nondirectory filename))
689 (jka-compr-run-real-handler 'file-local-copy (list filename)))
690 (temp-file (jka-compr-make-temp-name t))
691 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
695 (setq local-file (or local-copy filename))
699 (with-current-buffer temp-buffer
703 (message "%s %s..." uncompress-message base-name))
705 ;; Here we must read the output of uncompress program
706 ;; and write it to TEMP-FILE without any code
707 ;; conversion. An appropriate code conversion (if
708 ;; necessary) is done by the later I/O operation
710 (let ((coding-system-for-read 'binary)
711 (coding-system-for-write 'binary))
712 (jka-compr-call-process uncompress-program
713 (concat uncompress-message
722 (message "%s %s...done" uncompress-message base-name))
725 (point-min) (point-max) temp-file nil 'dont)))
729 (file-exists-p local-copy)
730 (delete-file local-copy))
732 (kill-buffer temp-buffer))
736 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
739 ;;; Support for loading compressed files.
740 ;;; XEmacs: autoload this function
742 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
743 "Documented as original."
745 (let* ((local-copy (jka-compr-file-local-copy file))
746 (load-file (or local-copy file)))
750 (let (inhibit-file-name-operation
751 inhibit-file-name-handlers)
753 (message "Loading %s..." file))
755 (let ((load-force-doc-strings t))
756 (load load-file noerror t t))
759 (message "Loading %s...done." file)))
761 (jka-compr-delete-temp-file local-copy))
765 (defun jka-compr-byte-compiler-base-file-name (file)
766 (let ((info (jka-compr-get-compression-info file)))
767 (if (and info (jka-compr-info-strip-extension info))
769 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
772 (put 'write-region 'jka-compr 'jka-compr-write-region)
773 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
774 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
775 (put 'load 'jka-compr 'jka-compr-load)
776 (put 'byte-compiler-base-file-name 'jka-compr
777 'jka-compr-byte-compiler-base-file-name)
779 (defvar jka-compr-inhibit nil
780 "Non-nil means inhibit automatic uncompression temporarily.
781 Lisp programs can bind this to t to do that.
782 It is not recommended to set this variable permanently to anything but nil.")
784 (defun jka-compr-handler (operation &rest args)
786 (let ((jka-op (get operation 'jka-compr)))
787 (if (and jka-op (not jka-compr-inhibit))
789 (jka-compr-run-real-handler operation args)))))
791 ;; If we are given an operation that we don't handle,
792 ;; call the Emacs primitive for that operation,
793 ;; and manipulate the inhibit variables
794 ;; to prevent the primitive from calling our handler again.
795 (defun jka-compr-run-real-handler (operation args)
796 (let ((inhibit-file-name-handlers
797 (cons 'jka-compr-handler
798 (and (eq inhibit-file-name-operation operation)
799 inhibit-file-name-handlers)))
800 (inhibit-file-name-operation operation))
801 (apply operation args)))
803 ;;;###autoload(defun auto-compression-mode (&optional arg)
805 ;;;###autoloadToggle automatic file compression and uncompression.
806 ;;;###autoloadWith prefix argument ARG, turn auto compression on if positive, else off.
807 ;;;###autoloadReturns the new status of auto compression (non-nil means on)."
808 ;;;###autoload (interactive "P")
809 ;;;###autoload (if (not (fboundp 'jka-compr-installed-p))
810 ;;;###autoload (require 'jka-compr))
811 ;;;###autoload (toggle-auto-compression arg t))
813 ;; XEmacs: autoload this function
815 (defun toggle-auto-compression (&optional arg message)
816 "Toggle automatic file compression and uncompression.
817 With prefix argument ARG, turn auto compression on if positive, else off.
818 Returns the new status of auto compression (non-nil means on).
819 If the argument MESSAGE is non-nil, it means to print a message
820 saying whether the mode is now on or off."
822 (let* ((installed (jka-compr-installed-p))
825 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))))
828 ((and flag installed) t) ; already installed
830 ((and (not flag) (not installed)) nil) ; already not installed
836 (jka-compr-uninstall)))
841 (message "Automatic file (de)compression is now ON.")
842 (message "Automatic file (de)compression is now OFF.")))
846 (defun jka-compr-build-file-regexp ()
850 'jka-compr-info-regexp
851 jka-compr-compression-info-list
857 (defun jka-compr-install ()
859 This adds entries to `file-name-handler-alist' and `auto-mode-alist'
860 and `inhibit-first-line-modes-suffixes'."
862 (setq jka-compr-file-name-handler-entry
863 (cons (jka-compr-build-file-regexp) 'jka-compr-handler))
865 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry
866 file-name-handler-alist))
868 (when (boundp 'file-coding-system-alist)
869 (setq jka-compr-added-to-file-coding-system-alist nil))
872 (function (lambda (x)
873 ;; Don't do multibyte encoding on the compressed files.
874 (when (boundp 'file-coding-system-alist)
875 (let ((elt (cons (jka-compr-info-regexp x)
876 '(binary . binary))))
877 (setq file-coding-system-alist
878 (cons elt file-coding-system-alist))
879 (setq jka-compr-added-to-file-coding-system-alist
880 (cons elt jka-compr-added-to-file-coding-system-alist)))
882 (and (jka-compr-info-strip-extension x)
883 ;; Make entries in auto-mode-alist so that modes
884 ;; are chosen right according to the file names
886 (setq auto-mode-alist
887 (cons (list (jka-compr-info-regexp x)
890 ;; Also add these regexps to
891 ;; inhibit-first-line-modes-suffixes, so that a
892 ;; -*- line in the first file of a compressed tar
893 ;; file doesn't override tar-mode.
894 ;; XEmacs: the (now)superfluous conditional doesn't hurt
895 (and (boundp 'inhibit-first-line-modes-suffixes)
896 (setq inhibit-first-line-modes-suffixes
897 (cons (jka-compr-info-regexp x)
898 inhibit-first-line-modes-suffixes))))))
899 jka-compr-compression-info-list)
900 (setq auto-mode-alist
901 (append auto-mode-alist jka-compr-mode-alist-additions)))
904 (defun jka-compr-uninstall ()
905 "Uninstall jka-compr.
906 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
907 and `inhibit-first-line-modes-suffixes' that were added
908 by `jka-compr-installed'."
909 ;; Delete from inhibit-first-line-modes-suffixes
910 ;; what jka-compr-install added.
912 (function (lambda (x)
913 (and (jka-compr-info-strip-extension x)
914 ;; XEmacs: the (now)superfluous conditional doesn't hurt
915 (and (boundp 'inhibit-first-line-modes-suffixes)
916 (setq inhibit-first-line-modes-suffixes
917 (delete (jka-compr-info-regexp x)
918 inhibit-first-line-modes-suffixes)))))
920 jka-compr-compression-info-list)
922 (let* ((fnha (cons nil file-name-handler-alist))
926 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
927 (setcdr last (cdr (cdr last)))
928 (setq last (cdr last))))
930 (setq file-name-handler-alist (cdr fnha)))
932 (let* ((ama (cons nil auto-mode-alist))
937 (setq entry (car (cdr last)))
938 (if (or (member entry jka-compr-mode-alist-additions)
939 (and (consp (cdr entry))
940 (eq (nth 2 entry) 'jka-compr)))
941 (setcdr last (cdr (cdr last)))
942 (setq last (cdr last))))
944 (setq auto-mode-alist (cdr ama)))
946 (when (boundp 'file-coding-system-alist)
947 (let* ((ama (cons nil file-coding-system-alist))
952 (setq entry (car (cdr last)))
953 (if (member entry jka-compr-added-to-file-coding-system-alist)
954 (setcdr last (cdr (cdr last)))
955 (setq last (cdr last))))
957 (setq file-coding-system-alist (cdr ama))))
960 (defun jka-compr-installed-p ()
961 "Return non-nil if jka-compr is installed.
962 The return value is the entry in `file-name-handler-alist' for jka-compr."
964 (let ((fnha file-name-handler-alist)
967 (while (and fnha (not installed))
968 (and (eq (cdr (car fnha)) 'jka-compr-handler)
969 (setq installed (car fnha)))
970 (setq fnha (cdr fnha)))
975 ;;; Add the file I/O hook if it does not already exist.
976 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
977 ;;; entry for jka-compr in file-name-handler-alist.
979 ;(and (jka-compr-installed-p)
980 ; (jka-compr-uninstall))
989 ;; jka-compr.el ends here.