new version
[emacs.git] / lisp / jka-compr.el
blob0f02f74df8a8beed5e9f558e3c17e833268f7b58
1 ;;; jka-compr.el --- reading/writing/loading compressed files
3 ;; Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
5 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
6 ;; Maintainer: FSF
7 ;; Keywords: data
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)
14 ;; any later version.
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.
26 ;;; Commentary:
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.
37 ;; INSTRUCTIONS:
39 ;; To use jka-compr, simply load this package, and edit as usual.
40 ;; Its operation should be transparent to the user (except for
41 ;; messages appearing when a file is being compressed or
42 ;; 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
56 ;; slow.
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
61 ;; nil.
64 ;; APPLICATION NOTES:
66 ;; crypt++
67 ;; jka-compr can coexist with crpyt++ 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
75 ;; file.
79 ;; ACKNOWLEDGMENTS
80 ;;
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.
85 ;; Jean-loup Gailly
86 ;; David Hughes
87 ;; Richard Pieri
88 ;; Daniel Quinlan
89 ;; Chris P. Ross
90 ;; Rick Sladkey
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.
101 ;;; Code:
103 (defgroup compression nil
104 "Data compression utilities"
105 :group 'data)
107 (defgroup jka-compr nil
108 "jka-compr customization"
109 :group 'compression)
112 (defcustom jka-compr-shell "sh"
113 "*Shell to be used for calling compression programs.
114 The value of this variable only matters if you want to discard the
115 stderr of a compression/decompression program (see the documentation
116 for `jka-compr-compression-info-list')."
117 :type 'string
118 :group 'jka-compr)
120 (defvar jka-compr-use-shell
121 (not (memq system-type '(ms-dos windows-nt))))
123 ;;; I have this defined so that .Z files are assumed to be in unix
124 ;;; compress format; and .gz files, in gzip format, and .bz2 files in bzip fmt.
125 (defcustom jka-compr-compression-info-list
126 ;;[regexp
127 ;; compr-message compr-prog compr-args
128 ;; uncomp-message uncomp-prog uncomp-args
129 ;; can-append auto-mode-flag]
130 '(["\\.Z\\(~\\|\\.~[0-9]+~\\)?\\'"
131 "compressing" "compress" ("-c")
132 "uncompressing" "uncompress" ("-c")
133 nil t]
134 ["\\.bz2\\'"
135 "bzip2ing" "bzip2" ("")
136 "bunzip2ing" "bzip2" ("-d")
137 nil t]
138 ["\\.tgz\\'"
139 "zipping" "gzip" ("-c" "-q")
140 "unzipping" "gzip" ("-c" "-q" "-d")
141 t nil]
142 ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'"
143 "zipping" "gzip" ("-c" "-q")
144 "unzipping" "gzip" ("-c" "-q" "-d")
145 t t])
147 "List of vectors that describe available compression techniques.
148 Each element, which describes a compression technique, is a vector of
149 the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS
150 UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS
151 APPEND-FLAG EXTENSION], where:
153 regexp is a regexp that matches filenames that are
154 compressed with this format
156 compress-msg is the message to issue to the user when doing this
157 type of compression (nil means no message)
159 compress-program is a program that performs this compression
161 compress-args is a list of args to pass to the compress program
163 uncompress-msg is the message to issue to the user when doing this
164 type of uncompression (nil means no message)
166 uncompress-program is a program that performs this compression
168 uncompress-args is a list of args to pass to the uncompress program
170 append-flag is non-nil if this compression technique can be
171 appended
173 auto-mode flag non-nil means strip the regexp from file names
174 before attempting to set the mode.
176 Because of the way `call-process' is defined, discarding the stderr output of
177 a program adds the overhead of starting a shell each time the program is
178 invoked."
179 :type '(repeat (vector regexp
180 (choice :tag "Compress Message"
181 (string :format "%v")
182 (const :tag "No Message" nil))
183 (string :tag "Compress Program")
184 (repeat :tag "Compress Arguments" string)
185 (choice :tag "Uncompress Message"
186 (string :format "%v")
187 (const :tag "No Message" nil))
188 (string :tag "Uncompress Program")
189 (repeat :tag "Uncompress Arguments" string)
190 (boolean :tag "Append")
191 (boolean :tag "Auto Mode")))
192 :group 'jka-compr)
194 (defvar jka-compr-mode-alist-additions
195 (list (cons "\\.tgz\\'" 'tar-mode))
196 "A list of pairs to add to `auto-mode-alist' when jka-compr is installed.")
198 ;; List of all the elements we actually added to file-coding-system-alist.
199 (defvar jka-compr-added-to-file-coding-system-alist nil)
201 (defvar jka-compr-file-name-handler-entry
203 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.")
205 ;;; Functions for accessing the return value of jka-compr-get-compression-info
206 (defun jka-compr-info-regexp (info) (aref info 0))
207 (defun jka-compr-info-compress-message (info) (aref info 1))
208 (defun jka-compr-info-compress-program (info) (aref info 2))
209 (defun jka-compr-info-compress-args (info) (aref info 3))
210 (defun jka-compr-info-uncompress-message (info) (aref info 4))
211 (defun jka-compr-info-uncompress-program (info) (aref info 5))
212 (defun jka-compr-info-uncompress-args (info) (aref info 6))
213 (defun jka-compr-info-can-append (info) (aref info 7))
214 (defun jka-compr-info-strip-extension (info) (aref info 8))
217 (defun jka-compr-get-compression-info (filename)
218 "Return information about the compression scheme of FILENAME.
219 The determination as to which compression scheme, if any, to use is
220 based on the filename itself and `jka-compr-compression-info-list'."
221 (catch 'compression-info
222 (let ((case-fold-search nil))
223 (mapcar
224 (function (lambda (x)
225 (and (string-match (jka-compr-info-regexp x) filename)
226 (throw 'compression-info x))))
227 jka-compr-compression-info-list)
228 nil)))
231 (put 'compression-error 'error-conditions '(compression-error file-error error))
234 (defvar jka-compr-acceptable-retval-list '(0 2 141))
237 (defun jka-compr-error (prog args infile message &optional errfile)
239 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
240 (curbuf (current-buffer)))
241 (with-current-buffer errbuf
242 (widen) (erase-buffer)
243 (insert (format "Error while executing \"%s %s < %s\"\n\n"
244 prog
245 (mapconcat 'identity args " ")
246 infile))
248 (and errfile
249 (insert-file-contents errfile)))
250 (display-buffer errbuf))
252 (signal 'compression-error
253 (list "Opening input file" (format "error %s" message) infile)))
256 (defvar jka-compr-dd-program
257 "/bin/dd")
260 (defvar jka-compr-dd-blocksize 256)
263 (defun jka-compr-partial-uncompress (prog message args infile beg len)
264 "Call program PROG with ARGS args taking input from INFILE.
265 Fourth and fifth args, BEG and LEN, specify which part of the output
266 to keep: LEN chars starting BEG chars from the beginning."
267 (let* ((skip (/ beg jka-compr-dd-blocksize))
268 (prefix (- beg (* skip jka-compr-dd-blocksize)))
269 (count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize))))
270 (start (point))
271 (err-file (jka-compr-make-temp-name))
272 (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null"
273 prog
274 (mapconcat 'identity args " ")
275 err-file
276 jka-compr-dd-program
277 jka-compr-dd-blocksize
278 skip
279 ;; dd seems to be unreliable about
280 ;; providing the last block. So, always
281 ;; read one more than you think you need.
282 (if count (concat "count=" (1+ count)) ""))))
284 (unwind-protect
285 (or (memq (call-process jka-compr-shell
286 infile t nil "-c"
287 run-string)
288 jka-compr-acceptable-retval-list)
290 (jka-compr-error prog args infile message err-file))
292 (jka-compr-delete-temp-file err-file))
294 ;; Delete the stuff after what we want, if there is any.
295 (and
297 (< (+ start prefix len) (point))
298 (delete-region (+ start prefix len) (point)))
300 ;; Delete the stuff before what we want.
301 (delete-region start (+ start prefix))))
304 (defun jka-compr-call-process (prog message infile output temp args)
305 (if jka-compr-use-shell
307 (let ((err-file (jka-compr-make-temp-name))
308 (coding-system-for-read (or coding-system-for-read 'undecided))
309 (coding-system-for-write 'no-conversion) )
311 (unwind-protect
313 (or (memq
314 (call-process jka-compr-shell infile
315 (if (stringp output) nil output)
317 "-c"
318 (format "%s %s 2> %s %s"
319 prog
320 (mapconcat 'identity args " ")
321 err-file
322 (if (stringp output)
323 (concat "> " output)
324 "")))
325 jka-compr-acceptable-retval-list)
327 (jka-compr-error prog args infile message err-file))
329 (jka-compr-delete-temp-file err-file)))
331 (or (zerop
332 (apply 'call-process
333 prog
334 infile
335 (if (stringp output) temp output)
337 args))
338 (jka-compr-error prog args infile message))
340 (and (stringp output)
341 (with-current-buffer temp
342 (write-region (point-min) (point-max) output)
343 (erase-buffer)))))
346 ;;; Support for temp files. Much of this was inspired if not lifted
347 ;;; from ange-ftp.
349 (defcustom jka-compr-temp-name-template
350 (expand-file-name "jka-com"
351 (if (memq system-type '(ms-dos windows-nt))
352 (concat (or (getenv "TEMP") (getenv "TMP") "c:/temp")
353 "/")
354 (or (getenv "TMPDIR") "/tmp/")))
355 "Prefix added to all temp files created by jka-compr.
356 There should be no more than seven characters after the final `/'."
357 :type 'string
358 :group 'jka-compr)
360 (defvar jka-compr-temp-name-table (make-vector 31 nil))
362 (defun jka-compr-make-temp-name (&optional local-copy)
363 "This routine will return the name of a new file."
364 (let* ((lastchar ?a)
365 (prevchar ?a)
366 (template (concat jka-compr-temp-name-template "aa"))
367 (lastpos (1- (length template)))
368 (not-done t)
369 file
370 entry)
372 (while not-done
373 (aset template lastpos lastchar)
374 (setq file (concat (make-temp-name template) "#"))
375 (setq entry (intern file jka-compr-temp-name-table))
376 (if (or (get entry 'active)
377 (file-exists-p file))
379 (progn
380 (setq lastchar (1+ lastchar))
381 (if (> lastchar ?z)
382 (progn
383 (setq prevchar (1+ prevchar))
384 (setq lastchar ?a)
385 (if (> prevchar ?z)
386 (error "Can't allocate temp file.")
387 (aset template (1- lastpos) prevchar)))))
389 (put entry 'active (not local-copy))
390 (setq not-done nil)))
392 file))
395 (defun jka-compr-delete-temp-file (temp)
397 (put (intern temp jka-compr-temp-name-table)
398 'active nil)
400 (condition-case ()
401 (delete-file temp)
402 (error nil)))
405 (defun jka-compr-write-region (start end file &optional append visit)
406 (let* ((filename (expand-file-name file))
407 (visit-file (if (stringp visit) (expand-file-name visit) filename))
408 (info (jka-compr-get-compression-info visit-file)))
410 (if info
412 (let ((can-append (jka-compr-info-can-append info))
413 (compress-program (jka-compr-info-compress-program info))
414 (compress-message (jka-compr-info-compress-message info))
415 (uncompress-program (jka-compr-info-uncompress-program info))
416 (uncompress-message (jka-compr-info-uncompress-message info))
417 (compress-args (jka-compr-info-compress-args info))
418 (uncompress-args (jka-compr-info-uncompress-args info))
419 (base-name (file-name-nondirectory visit-file))
420 temp-file temp-buffer)
422 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
423 (with-current-buffer temp-buffer
424 (widen) (erase-buffer))
426 (if (and append
427 (not can-append)
428 (file-exists-p filename))
430 (let* ((local-copy (file-local-copy filename))
431 (local-file (or local-copy filename)))
433 (setq temp-file local-file))
435 (setq temp-file (jka-compr-make-temp-name)))
437 (and
438 compress-message
439 (message "%s %s..." compress-message base-name))
441 (jka-compr-run-real-handler 'write-region
442 (list start end temp-file t 'dont))
444 ;; Here we must read the output of compress program as is
445 ;; without any code conversion.
446 (let ((coding-system-for-read 'no-conversion))
447 (jka-compr-call-process compress-program
448 (concat compress-message
449 " " base-name)
450 temp-file
451 temp-buffer
453 compress-args))
455 (with-current-buffer temp-buffer
456 (let ((coding-system-for-write 'no-conversion))
457 (if (memq system-type '(ms-dos windows-nt))
458 (setq buffer-file-type t) )
459 (jka-compr-run-real-handler 'write-region
460 (list (point-min) (point-max)
461 filename
462 (and append can-append) 'dont))
463 (erase-buffer)) )
465 (jka-compr-delete-temp-file temp-file)
467 (and
468 compress-message
469 (message "%s %s...done" compress-message base-name))
471 (cond
472 ((eq visit t)
473 (setq buffer-file-name filename)
474 (set-visited-file-modtime))
475 ((stringp visit)
476 (setq buffer-file-name visit)
477 (let ((buffer-file-name filename))
478 (set-visited-file-modtime))))
480 (and (or (eq visit t)
481 (eq visit nil)
482 (stringp visit))
483 (message "Wrote %s" visit-file))
485 nil)
487 (jka-compr-run-real-handler 'write-region
488 (list start end filename append visit)))))
491 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
492 (barf-if-buffer-read-only)
494 (and (or beg end)
495 visit
496 (error "Attempt to visit less than an entire file"))
498 (let* ((filename (expand-file-name file))
499 (info (jka-compr-get-compression-info filename)))
501 (if info
503 (let ((uncompress-message (jka-compr-info-uncompress-message info))
504 (uncompress-program (jka-compr-info-uncompress-program info))
505 (uncompress-args (jka-compr-info-uncompress-args info))
506 (base-name (file-name-nondirectory filename))
507 (notfound nil)
508 (local-copy
509 (jka-compr-run-real-handler 'file-local-copy (list filename)))
510 local-file
511 size start
512 (coding-system-for-read
513 (or coding-system-for-read
514 ;; If multibyte characters are disabled,
515 ;; don't do that conversion.
516 (and (null enable-multibyte-characters)
517 'raw-text)
518 (let ((tail file-coding-system-alist)
519 (newfile
520 (jka-compr-byte-compiler-base-file-name file))
521 result)
522 (while tail
523 (if (string-match (car (car tail)) newfile)
524 (setq result (car (cdr (car tail)))
525 tail nil))
526 (setq tail (cdr tail)))
527 result)
528 'undecided)) )
530 (setq local-file (or local-copy filename))
532 (and
533 visit
534 (setq buffer-file-name filename))
536 (unwind-protect ; to make sure local-copy gets deleted
538 (progn
540 (and
541 uncompress-message
542 (message "%s %s..." uncompress-message base-name))
544 (condition-case error-code
546 (progn
547 (if replace
548 (goto-char (point-min)))
549 (setq start (point))
550 (if (or beg end)
551 (jka-compr-partial-uncompress uncompress-program
552 (concat uncompress-message
553 " " base-name)
554 uncompress-args
555 local-file
556 (or beg 0)
557 (if (and beg end)
558 (- end beg)
559 end))
560 ;; If visiting, bind off buffer-file-name so that
561 ;; file-locking will not ask whether we should
562 ;; really edit the buffer.
563 (let ((buffer-file-name
564 (if visit nil buffer-file-name)))
565 (jka-compr-call-process uncompress-program
566 (concat uncompress-message
567 " " base-name)
568 local-file
571 uncompress-args)))
572 (setq size (- (point) start))
573 (if replace
574 (let* ((del-beg (point))
575 (del-end (+ del-beg size)))
576 (delete-region del-beg
577 (min del-end (point-max)))))
578 (goto-char start))
579 (error
580 (if (and (eq (car error-code) 'file-error)
581 (eq (nth 3 error-code) local-file))
582 (if visit
583 (setq notfound error-code)
584 (signal 'file-error
585 (cons "Opening input file"
586 (nthcdr 2 error-code))))
587 (signal (car error-code) (cdr error-code))))))
589 (and
590 local-copy
591 (file-exists-p local-copy)
592 (delete-file local-copy)))
594 (and
595 visit
596 (progn
597 (unlock-buffer)
598 (setq buffer-file-name filename)
599 (set-visited-file-modtime)))
601 (and
602 uncompress-message
603 (message "%s %s...done" uncompress-message base-name))
605 (and
606 visit
607 notfound
608 (signal 'file-error
609 (cons "Opening input file" (nth 2 notfound))))
611 ;; Run the functions that insert-file-contents would.
612 (let ((p after-insert-file-functions)
613 (insval size))
614 (while p
615 (setq insval (funcall (car p) size))
616 (if insval
617 (progn
618 (or (integerp insval)
619 (signal 'wrong-type-argument
620 (list 'integerp insval)))
621 (setq size insval)))
622 (setq p (cdr p))))
624 (list filename size))
626 (jka-compr-run-real-handler 'insert-file-contents
627 (list file visit beg end replace)))))
630 (defun jka-compr-file-local-copy (file)
631 (let* ((filename (expand-file-name file))
632 (info (jka-compr-get-compression-info filename)))
634 (if info
636 (let ((uncompress-message (jka-compr-info-uncompress-message info))
637 (uncompress-program (jka-compr-info-uncompress-program info))
638 (uncompress-args (jka-compr-info-uncompress-args info))
639 (base-name (file-name-nondirectory filename))
640 (local-copy
641 (jka-compr-run-real-handler 'file-local-copy (list filename)))
642 (temp-file (jka-compr-make-temp-name t))
643 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
644 (notfound nil)
645 local-file)
647 (setq local-file (or local-copy filename))
649 (unwind-protect
651 (with-current-buffer temp-buffer
653 (and
654 uncompress-message
655 (message "%s %s..." uncompress-message base-name))
657 ;; Here we must read the output of uncompress program
658 ;; and write it to TEMP-FILE without any code
659 ;; conversion. An appropriate code conversion (if
660 ;; necessary) is done by the later I/O operation
661 ;; (e.g. load).
662 (let ((coding-system-for-read 'no-conversion)
663 (coding-system-for-write 'no-conversion))
665 (jka-compr-call-process uncompress-program
666 (concat uncompress-message
667 " " base-name)
668 local-file
671 uncompress-args)
673 (and
674 uncompress-message
675 (message "%s %s...done" uncompress-message base-name))
677 (write-region
678 (point-min) (point-max) temp-file nil 'dont)))
680 (and
681 local-copy
682 (file-exists-p local-copy)
683 (delete-file local-copy))
685 (kill-buffer temp-buffer))
687 temp-file)
689 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
692 ;;; Support for loading compressed files.
693 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
694 "Documented as original."
696 (let* ((local-copy (jka-compr-file-local-copy file))
697 (load-file (or local-copy file)))
699 (unwind-protect
701 (let (inhibit-file-name-operation
702 inhibit-file-name-handlers)
703 (or nomessage
704 (message "Loading %s..." file))
706 (let ((load-force-doc-strings t))
707 (load load-file noerror t t))
709 (or nomessage
710 (message "Loading %s...done." file)))
712 (jka-compr-delete-temp-file local-copy))
716 (defun jka-compr-byte-compiler-base-file-name (file)
717 (let ((info (jka-compr-get-compression-info file)))
718 (if (and info (jka-compr-info-strip-extension info))
719 (save-match-data
720 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
721 file)))
723 (put 'write-region 'jka-compr 'jka-compr-write-region)
724 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
725 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
726 (put 'load 'jka-compr 'jka-compr-load)
727 (put 'byte-compiler-base-file-name 'jka-compr
728 'jka-compr-byte-compiler-base-file-name)
730 (defvar jka-compr-inhibit nil
731 "Non-nil means inhibit automatic uncompression temporarily.
732 Lisp programs can bind this to t to do that.
733 It is not recommended to set this variable permanently to anything but nil.")
735 (defun jka-compr-handler (operation &rest args)
736 (save-match-data
737 (let ((jka-op (get operation 'jka-compr)))
738 (if (and jka-op (not jka-compr-inhibit))
739 (apply jka-op args)
740 (jka-compr-run-real-handler operation args)))))
742 ;; If we are given an operation that we don't handle,
743 ;; call the Emacs primitive for that operation,
744 ;; and manipulate the inhibit variables
745 ;; to prevent the primitive from calling our handler again.
746 (defun jka-compr-run-real-handler (operation args)
747 (let ((inhibit-file-name-handlers
748 (cons 'jka-compr-handler
749 (and (eq inhibit-file-name-operation operation)
750 inhibit-file-name-handlers)))
751 (inhibit-file-name-operation operation))
752 (apply operation args)))
754 ;;;###autoload(defun auto-compression-mode (&optional arg)
755 ;;;###autoload "\
756 ;;;###autoloadToggle automatic file compression and uncompression.
757 ;;;###autoloadWith prefix argument ARG, turn auto compression on if positive, else off.
758 ;;;###autoloadReturns the new status of auto compression (non-nil means on)."
759 ;;;###autoload (interactive "P")
760 ;;;###autoload (if (not (fboundp 'jka-compr-installed-p))
761 ;;;###autoload (progn
762 ;;;###autoload (require 'jka-compr)
763 ;;;###autoload ;; That turned the mode on, so make it initially off.
764 ;;;###autoload (toggle-auto-compression)))
765 ;;;###autoload (toggle-auto-compression arg t))
767 (defun toggle-auto-compression (&optional arg message)
768 "Toggle automatic file compression and uncompression.
769 With prefix argument ARG, turn auto compression on if positive, else off.
770 Returns the new status of auto compression (non-nil means on).
771 If the argument MESSAGE is non-nil, it means to print a message
772 saying whether the mode is now on or off."
773 (interactive "P\np")
774 (let* ((installed (jka-compr-installed-p))
775 (flag (if (null arg)
776 (not installed)
777 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))))
779 (cond
780 ((and flag installed) t) ; already installed
782 ((and (not flag) (not installed)) nil) ; already not installed
784 (flag
785 (jka-compr-install))
788 (jka-compr-uninstall)))
791 (and message
792 (if flag
793 (message "Automatic file (de)compression is now ON.")
794 (message "Automatic file (de)compression is now OFF.")))
796 flag))
798 (defun jka-compr-build-file-regexp ()
799 (concat
800 "\\("
801 (mapconcat
802 'jka-compr-info-regexp
803 jka-compr-compression-info-list
804 "\\)\\|\\(")
805 "\\)"))
808 (defun jka-compr-install ()
809 "Install jka-compr.
810 This adds entries to `file-name-handler-alist' and `auto-mode-alist'
811 and `inhibit-first-line-modes-suffixes'."
813 (setq jka-compr-file-name-handler-entry
814 (cons (jka-compr-build-file-regexp) 'jka-compr-handler))
816 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry
817 file-name-handler-alist))
819 (setq jka-compr-added-to-file-coding-system-alist nil)
821 (mapcar
822 (function (lambda (x)
823 ;; Don't do multibyte encoding on the compressed files.
824 (let ((elt (cons (jka-compr-info-regexp x)
825 '(no-conversion . no-conversion))))
826 (setq file-coding-system-alist
827 (cons elt file-coding-system-alist))
828 (setq jka-compr-added-to-file-coding-system-alist
829 (cons elt jka-compr-added-to-file-coding-system-alist)))
831 (and (jka-compr-info-strip-extension x)
832 ;; Make entries in auto-mode-alist so that modes
833 ;; are chosen right according to the file names
834 ;; sans `.gz'.
835 (setq auto-mode-alist
836 (cons (list (jka-compr-info-regexp x)
837 nil 'jka-compr)
838 auto-mode-alist))
839 ;; Also add these regexps to
840 ;; inhibit-first-line-modes-suffixes, so that a
841 ;; -*- line in the first file of a compressed tar
842 ;; file doesn't override tar-mode.
843 (setq inhibit-first-line-modes-suffixes
844 (cons (jka-compr-info-regexp x)
845 inhibit-first-line-modes-suffixes)))))
846 jka-compr-compression-info-list)
847 (setq auto-mode-alist
848 (append auto-mode-alist jka-compr-mode-alist-additions)))
851 (defun jka-compr-uninstall ()
852 "Uninstall jka-compr.
853 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
854 and `inhibit-first-line-modes-suffixes' that were added
855 by `jka-compr-installed'."
856 ;; Delete from inhibit-first-line-modes-suffixes
857 ;; what jka-compr-install added.
858 (mapcar
859 (function (lambda (x)
860 (and (jka-compr-info-strip-extension x)
861 (setq inhibit-first-line-modes-suffixes
862 (delete (jka-compr-info-regexp x)
863 inhibit-first-line-modes-suffixes)))))
864 jka-compr-compression-info-list)
866 (let* ((fnha (cons nil file-name-handler-alist))
867 (last fnha))
869 (while (cdr last)
870 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
871 (setcdr last (cdr (cdr last)))
872 (setq last (cdr last))))
874 (setq file-name-handler-alist (cdr fnha)))
876 (let* ((ama (cons nil auto-mode-alist))
877 (last ama)
878 entry)
880 (while (cdr last)
881 (setq entry (car (cdr last)))
882 (if (or (member entry jka-compr-mode-alist-additions)
883 (and (consp (cdr entry))
884 (eq (nth 2 entry) 'jka-compr)))
885 (setcdr last (cdr (cdr last)))
886 (setq last (cdr last))))
888 (setq auto-mode-alist (cdr ama)))
890 (let* ((ama (cons nil file-coding-system-alist))
891 (last ama)
892 entry)
894 (while (cdr last)
895 (setq entry (car (cdr last)))
896 (if (member entry jka-compr-added-to-file-coding-system-alist)
897 (setcdr last (cdr (cdr last)))
898 (setq last (cdr last))))
900 (setq file-coding-system-alist (cdr ama))))
903 (defun jka-compr-installed-p ()
904 "Return non-nil if jka-compr is installed.
905 The return value is the entry in `file-name-handler-alist' for jka-compr."
907 (let ((fnha file-name-handler-alist)
908 (installed nil))
910 (while (and fnha (not installed))
911 (and (eq (cdr (car fnha)) 'jka-compr-handler)
912 (setq installed (car fnha)))
913 (setq fnha (cdr fnha)))
915 installed))
918 ;;; Add the file I/O hook if it does not already exist.
919 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
920 ;;; entry for jka-compr in file-name-handler-alist.
921 (and (jka-compr-installed-p)
922 (jka-compr-uninstall))
924 (jka-compr-install)
927 (provide 'jka-compr)
929 ;; jka-compr.el ends here.