Use make-temp-file.
[emacs.git] / lisp / jka-compr.el
blob234fb8375d845c190d491bb538055d4211b48500
1 ;;; jka-compr.el --- reading/writing/loading compressed files
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 1999 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 ;; Formerly, these had an additional arg "-c", but that fails with
135 ;; "Version 0.1pl2, 29-Aug-97." (RedHat 5.1 GNU/Linux) and
136 ;; "Version 0.9.0b, 9-Sept-98".
137 ["\\.bz2\\'"
138 "bzip2ing" "bzip2" nil
139 "bunzip2ing" "bzip2" ("-d")
140 nil t]
141 ["\\.tgz\\'"
142 "zipping" "gzip" ("-c" "-q")
143 "unzipping" "gzip" ("-c" "-q" "-d")
144 t nil]
145 ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'"
146 "zipping" "gzip" ("-c" "-q")
147 "unzipping" "gzip" ("-c" "-q" "-d")
148 t t])
150 "List of vectors that describe available compression techniques.
151 Each element, which describes a compression technique, is a vector of
152 the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS
153 UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS
154 APPEND-FLAG EXTENSION], where:
156 regexp is a regexp that matches filenames that are
157 compressed with this format
159 compress-msg is the message to issue to the user when doing this
160 type of compression (nil means no message)
162 compress-program is a program that performs this compression
164 compress-args is a list of args to pass to the compress program
166 uncompress-msg is the message to issue to the user when doing this
167 type of uncompression (nil means no message)
169 uncompress-program is a program that performs this compression
171 uncompress-args is a list of args to pass to the uncompress program
173 append-flag is non-nil if this compression technique can be
174 appended
176 auto-mode flag non-nil means strip the regexp from file names
177 before attempting to set the mode.
179 Because of the way `call-process' is defined, discarding the stderr output of
180 a program adds the overhead of starting a shell each time the program is
181 invoked."
182 :type '(repeat (vector regexp
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")))
195 :group 'jka-compr)
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))
226 (mapcar
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)
231 nil)))
234 (put 'compression-error 'error-conditions '(compression-error file-error error))
237 (defvar jka-compr-acceptable-retval-list '(0 2 141))
240 (defun jka-compr-error (prog args infile message &optional errfile)
242 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
243 (curbuf (current-buffer)))
244 (with-current-buffer errbuf
245 (widen) (erase-buffer)
246 (insert (format "Error while executing \"%s %s < %s\"\n\n"
247 prog
248 (mapconcat 'identity args " ")
249 infile))
251 (and errfile
252 (insert-file-contents errfile)))
253 (display-buffer errbuf))
255 (signal 'compression-error
256 (list "Opening input file" (format "error %s" message) infile)))
259 (defvar jka-compr-dd-program
260 "/bin/dd")
263 (defvar jka-compr-dd-blocksize 256)
266 (defun jka-compr-partial-uncompress (prog message args infile beg len)
267 "Call program PROG with ARGS args taking input from INFILE.
268 Fourth and fifth args, BEG and LEN, specify which part of the output
269 to keep: LEN chars starting BEG chars from the beginning."
270 (let* ((skip (/ beg jka-compr-dd-blocksize))
271 (prefix (- beg (* skip jka-compr-dd-blocksize)))
272 (count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize))))
273 (start (point))
274 (err-file (jka-compr-make-temp-name))
275 (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null"
276 prog
277 (mapconcat 'identity args " ")
278 err-file
279 jka-compr-dd-program
280 jka-compr-dd-blocksize
281 skip
282 ;; dd seems to be unreliable about
283 ;; providing the last block. So, always
284 ;; read one more than you think you need.
285 (if count (concat "count=" (1+ count)) ""))))
287 (unwind-protect
288 (or (memq (call-process jka-compr-shell
289 infile t nil "-c"
290 run-string)
291 jka-compr-acceptable-retval-list)
293 (jka-compr-error prog args infile message err-file))
295 (jka-compr-delete-temp-file err-file))
297 ;; Delete the stuff after what we want, if there is any.
298 (and
300 (< (+ start prefix len) (point))
301 (delete-region (+ start prefix len) (point)))
303 ;; Delete the stuff before what we want.
304 (delete-region start (+ start prefix))))
307 (defun jka-compr-call-process (prog message infile output temp args)
308 (if jka-compr-use-shell
310 (let ((err-file (jka-compr-make-temp-name))
311 (coding-system-for-read (or coding-system-for-read 'undecided))
312 (coding-system-for-write 'no-conversion))
314 (unwind-protect
316 (or (memq
317 (call-process jka-compr-shell infile
318 (if (stringp output) nil output)
320 "-c"
321 (format "%s %s 2> %s %s"
322 prog
323 (mapconcat 'identity args " ")
324 err-file
325 (if (stringp output)
326 (concat "> " output)
327 "")))
328 jka-compr-acceptable-retval-list)
330 (jka-compr-error prog args infile message err-file))
332 (jka-compr-delete-temp-file err-file)))
334 (or (zerop
335 (apply 'call-process
336 prog
337 infile
338 (if (stringp output) temp output)
340 args))
341 (jka-compr-error prog args infile message))
343 (and (stringp output)
344 (with-current-buffer temp
345 (write-region (point-min) (point-max) output)
346 (erase-buffer)))))
349 ;;; Support for temp files. Much of this was inspired if not lifted
350 ;;; from ange-ftp.
352 (defcustom jka-compr-temp-name-template
353 (expand-file-name "jka-com" temporary-file-directory)
354 "Prefix added to all temp files created by jka-compr.
355 There should be no more than seven characters after the final `/'."
356 :type 'string
357 :group 'jka-compr)
359 (defun jka-compr-make-temp-name (&optional local-copy)
360 "This routine will return the name of a new file."
361 (make-temp-file jka-compr-temp-name-template))
363 (defalias 'jka-compr-delete-temp-file 'delete-file)
366 (defun jka-compr-write-region (start end file &optional append visit)
367 (let* ((filename (expand-file-name file))
368 (visit-file (if (stringp visit) (expand-file-name visit) filename))
369 (info (jka-compr-get-compression-info visit-file)))
371 (if info
373 (let ((can-append (jka-compr-info-can-append info))
374 (compress-program (jka-compr-info-compress-program info))
375 (compress-message (jka-compr-info-compress-message info))
376 (uncompress-program (jka-compr-info-uncompress-program info))
377 (uncompress-message (jka-compr-info-uncompress-message info))
378 (compress-args (jka-compr-info-compress-args info))
379 (uncompress-args (jka-compr-info-uncompress-args info))
380 (base-name (file-name-nondirectory visit-file))
381 temp-file temp-buffer
382 ;; we need to leave `last-coding-system-used' set to its
383 ;; value after calling write-region the first time, so
384 ;; that `basic-save-buffer' sees the right value.
385 (coding-system-used last-coding-system-used))
387 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
388 (with-current-buffer temp-buffer
389 (widen) (erase-buffer))
391 (if (and append
392 (not can-append)
393 (file-exists-p filename))
395 (let* ((local-copy (file-local-copy filename))
396 (local-file (or local-copy filename)))
398 (setq temp-file local-file))
400 (setq temp-file (jka-compr-make-temp-name)))
402 (and
403 compress-message
404 (message "%s %s..." compress-message base-name))
406 (jka-compr-run-real-handler 'write-region
407 (list start end temp-file t 'dont))
408 ;; save value used by the real write-region
409 (setq coding-system-used last-coding-system-used)
411 ;; Here we must read the output of compress program as is
412 ;; without any code conversion.
413 (let ((coding-system-for-read 'no-conversion))
414 (jka-compr-call-process compress-program
415 (concat compress-message
416 " " base-name)
417 temp-file
418 temp-buffer
420 compress-args))
422 (with-current-buffer temp-buffer
423 (let ((coding-system-for-write 'no-conversion))
424 (if (memq system-type '(ms-dos windows-nt))
425 (setq buffer-file-type t) )
426 (jka-compr-run-real-handler 'write-region
427 (list (point-min) (point-max)
428 filename
429 (and append can-append) 'dont))
430 (erase-buffer)) )
432 (jka-compr-delete-temp-file temp-file)
434 (and
435 compress-message
436 (message "%s %s...done" compress-message base-name))
438 (cond
439 ((eq visit t)
440 (setq buffer-file-name filename)
441 (set-visited-file-modtime))
442 ((stringp visit)
443 (setq buffer-file-name visit)
444 (let ((buffer-file-name filename))
445 (set-visited-file-modtime))))
447 (and (or (eq visit t)
448 (eq visit nil)
449 (stringp visit))
450 (message "Wrote %s" visit-file))
452 ;; ensure `last-coding-system-used' has an appropriate value
453 (setq last-coding-system-used coding-system-used)
455 nil)
457 (jka-compr-run-real-handler 'write-region
458 (list start end filename append visit)))))
461 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
462 (barf-if-buffer-read-only)
464 (and (or beg end)
465 visit
466 (error "Attempt to visit less than an entire file"))
468 (let* ((filename (expand-file-name file))
469 (info (jka-compr-get-compression-info filename)))
471 (if info
473 (let ((uncompress-message (jka-compr-info-uncompress-message info))
474 (uncompress-program (jka-compr-info-uncompress-program info))
475 (uncompress-args (jka-compr-info-uncompress-args info))
476 (base-name (file-name-nondirectory filename))
477 (notfound nil)
478 (local-copy
479 (jka-compr-run-real-handler 'file-local-copy (list filename)))
480 local-file
481 size start
482 (coding-system-for-read
483 (or coding-system-for-read
484 ;; If multibyte characters are disabled,
485 ;; don't do that conversion.
486 (and (null enable-multibyte-characters)
487 (or (auto-coding-alist-lookup
488 (jka-compr-byte-compiler-base-file-name file))
489 'raw-text))
490 (let ((coding (find-operation-coding-system
491 'insert-file-contents
492 (jka-compr-byte-compiler-base-file-name file))))
493 (and (consp coding) (car coding)))
494 'undecided)) )
496 (setq local-file (or local-copy filename))
498 (and
499 visit
500 (setq buffer-file-name filename))
502 (unwind-protect ; to make sure local-copy gets deleted
504 (progn
506 (and
507 uncompress-message
508 (message "%s %s..." uncompress-message base-name))
510 (condition-case error-code
512 (progn
513 (if replace
514 (goto-char (point-min)))
515 (setq start (point))
516 (if (or beg end)
517 (jka-compr-partial-uncompress uncompress-program
518 (concat uncompress-message
519 " " base-name)
520 uncompress-args
521 local-file
522 (or beg 0)
523 (if (and beg end)
524 (- end beg)
525 end))
526 ;; If visiting, bind off buffer-file-name so that
527 ;; file-locking will not ask whether we should
528 ;; really edit the buffer.
529 (let ((buffer-file-name
530 (if visit nil buffer-file-name)))
531 (jka-compr-call-process uncompress-program
532 (concat uncompress-message
533 " " base-name)
534 local-file
537 uncompress-args)))
538 (setq size (- (point) start))
539 (if replace
540 (let* ((del-beg (point))
541 (del-end (+ del-beg size)))
542 (delete-region del-beg
543 (min del-end (point-max)))))
544 (goto-char start))
545 (error
546 (if (and (eq (car error-code) 'file-error)
547 (eq (nth 3 error-code) local-file))
548 (if visit
549 (setq notfound error-code)
550 (signal 'file-error
551 (cons "Opening input file"
552 (nthcdr 2 error-code))))
553 (signal (car error-code) (cdr error-code))))))
555 (and
556 local-copy
557 (file-exists-p local-copy)
558 (delete-file local-copy)))
560 (and
561 visit
562 (progn
563 (unlock-buffer)
564 (setq buffer-file-name filename)
565 (set-visited-file-modtime)))
567 (and
568 uncompress-message
569 (message "%s %s...done" uncompress-message base-name))
571 (and
572 visit
573 notfound
574 (signal 'file-error
575 (cons "Opening input file" (nth 2 notfound))))
577 ;; This is done in insert-file-contents after we return.
578 ;; That is a little weird, but better to go along with it now
579 ;; than to change it now.
581 ;;; ;; Run the functions that insert-file-contents would.
582 ;;; (let ((p after-insert-file-functions)
583 ;;; (insval size))
584 ;;; (while p
585 ;;; (setq insval (funcall (car p) size))
586 ;;; (if insval
587 ;;; (progn
588 ;;; (or (integerp insval)
589 ;;; (signal 'wrong-type-argument
590 ;;; (list 'integerp insval)))
591 ;;; (setq size insval)))
592 ;;; (setq p (cdr p))))
594 (list filename size))
596 (jka-compr-run-real-handler 'insert-file-contents
597 (list file visit beg end replace)))))
600 (defun jka-compr-file-local-copy (file)
601 (let* ((filename (expand-file-name file))
602 (info (jka-compr-get-compression-info filename)))
604 (if info
606 (let ((uncompress-message (jka-compr-info-uncompress-message info))
607 (uncompress-program (jka-compr-info-uncompress-program info))
608 (uncompress-args (jka-compr-info-uncompress-args info))
609 (base-name (file-name-nondirectory filename))
610 (local-copy
611 (jka-compr-run-real-handler 'file-local-copy (list filename)))
612 (temp-file (jka-compr-make-temp-name t))
613 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
614 (notfound nil)
615 local-file)
617 (setq local-file (or local-copy filename))
619 (unwind-protect
621 (with-current-buffer temp-buffer
623 (and
624 uncompress-message
625 (message "%s %s..." uncompress-message base-name))
627 ;; Here we must read the output of uncompress program
628 ;; and write it to TEMP-FILE without any code
629 ;; conversion. An appropriate code conversion (if
630 ;; necessary) is done by the later I/O operation
631 ;; (e.g. load).
632 (let ((coding-system-for-read 'no-conversion)
633 (coding-system-for-write 'no-conversion))
635 (jka-compr-call-process uncompress-program
636 (concat uncompress-message
637 " " base-name)
638 local-file
641 uncompress-args)
643 (and
644 uncompress-message
645 (message "%s %s...done" uncompress-message base-name))
647 (write-region
648 (point-min) (point-max) temp-file nil 'dont)))
650 (and
651 local-copy
652 (file-exists-p local-copy)
653 (delete-file local-copy))
655 (kill-buffer temp-buffer))
657 temp-file)
659 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
662 ;;; Support for loading compressed files.
663 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
664 "Documented as original."
666 (let* ((local-copy (jka-compr-file-local-copy file))
667 (load-file (or local-copy file)))
669 (unwind-protect
671 (let (inhibit-file-name-operation
672 inhibit-file-name-handlers)
673 (or nomessage
674 (message "Loading %s..." file))
676 (let ((load-force-doc-strings t))
677 (load load-file noerror t t))
679 (or nomessage
680 (message "Loading %s...done." file)))
682 (jka-compr-delete-temp-file local-copy))
686 (defun jka-compr-byte-compiler-base-file-name (file)
687 (let ((info (jka-compr-get-compression-info file)))
688 (if (and info (jka-compr-info-strip-extension info))
689 (save-match-data
690 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
691 file)))
693 (put 'write-region 'jka-compr 'jka-compr-write-region)
694 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
695 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
696 (put 'load 'jka-compr 'jka-compr-load)
697 (put 'byte-compiler-base-file-name 'jka-compr
698 'jka-compr-byte-compiler-base-file-name)
700 (defvar jka-compr-inhibit nil
701 "Non-nil means inhibit automatic uncompression temporarily.
702 Lisp programs can bind this to t to do that.
703 It is not recommended to set this variable permanently to anything but nil.")
705 (defun jka-compr-handler (operation &rest args)
706 (save-match-data
707 (let ((jka-op (get operation 'jka-compr)))
708 (if (and jka-op (not jka-compr-inhibit))
709 (apply jka-op args)
710 (jka-compr-run-real-handler operation args)))))
712 ;; If we are given an operation that we don't handle,
713 ;; call the Emacs primitive for that operation,
714 ;; and manipulate the inhibit variables
715 ;; to prevent the primitive from calling our handler again.
716 (defun jka-compr-run-real-handler (operation args)
717 (let ((inhibit-file-name-handlers
718 (cons 'jka-compr-handler
719 (and (eq inhibit-file-name-operation operation)
720 inhibit-file-name-handlers)))
721 (inhibit-file-name-operation operation))
722 (apply operation args)))
724 ;;;###autoload
725 (defcustom auto-compression-mode nil
726 "Toggle automatic file compression and uncompression.
727 Setting this variable directly does not take effect;
728 use either \\[customize] or the function `auto-compression-mode'."
729 :set (lambda (symbol value)
730 (auto-compression-mode (or value 0)))
731 :initialize 'custom-initialize-default
732 :group 'jka-compr
733 :version "21.1"
734 :type 'boolean
735 :require 'jka-compr)
737 ;;;###autoload(defun auto-compression-mode (&optional arg)
738 ;;;###autoload "\
739 ;;;###autoloadToggle automatic file compression and uncompression.
740 ;;;###autoloadWith prefix argument ARG, turn auto compression on if positive, else off.
741 ;;;###autoloadReturns the new status of auto compression (non-nil means on)."
742 ;;;###autoload (interactive "P")
743 ;;;###autoload (if (not (fboundp 'jka-compr-installed-p))
744 ;;;###autoload (progn
745 ;;;###autoload (require 'jka-compr)
746 ;;;###autoload ;; That turned the mode on, so make it initially off.
747 ;;;###autoload (toggle-auto-compression)))
748 ;;;###autoload (toggle-auto-compression arg t))
750 (defun toggle-auto-compression (&optional arg message)
751 "Toggle automatic file compression and uncompression.
752 With prefix argument ARG, turn auto compression on if positive, else off.
753 Returns the new status of auto compression (non-nil means on).
754 If the argument MESSAGE is non-nil, it means to print a message
755 saying whether the mode is now on or off."
756 (interactive "P\np")
757 (let* ((installed (jka-compr-installed-p))
758 (flag (if (null arg)
759 (not installed)
760 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))))
762 (cond
763 ((and flag installed) t) ; already installed
765 ((and (not flag) (not installed)) nil) ; already not installed
767 (flag
768 (jka-compr-install))
771 (jka-compr-uninstall)))
774 (and message
775 (if flag
776 (message "Automatic file (de)compression is now ON.")
777 (message "Automatic file (de)compression is now OFF.")))
779 flag))
781 (defun jka-compr-build-file-regexp ()
782 (concat
783 "\\("
784 (mapconcat
785 'jka-compr-info-regexp
786 jka-compr-compression-info-list
787 "\\)\\|\\(")
788 "\\)"))
791 (defun jka-compr-install ()
792 "Install jka-compr.
793 This adds entries to `file-name-handler-alist' and `auto-mode-alist'
794 and `inhibit-first-line-modes-suffixes'."
796 (setq jka-compr-file-name-handler-entry
797 (cons (jka-compr-build-file-regexp) 'jka-compr-handler))
799 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry
800 file-name-handler-alist))
802 (setq jka-compr-added-to-file-coding-system-alist nil)
804 (mapcar
805 (function (lambda (x)
806 ;; Don't do multibyte encoding on the compressed files.
807 (let ((elt (cons (jka-compr-info-regexp x)
808 '(no-conversion . no-conversion))))
809 (setq file-coding-system-alist
810 (cons elt file-coding-system-alist))
811 (setq jka-compr-added-to-file-coding-system-alist
812 (cons elt jka-compr-added-to-file-coding-system-alist)))
814 (and (jka-compr-info-strip-extension x)
815 ;; Make entries in auto-mode-alist so that modes
816 ;; are chosen right according to the file names
817 ;; sans `.gz'.
818 (setq auto-mode-alist
819 (cons (list (jka-compr-info-regexp x)
820 nil 'jka-compr)
821 auto-mode-alist))
822 ;; Also add these regexps to
823 ;; inhibit-first-line-modes-suffixes, so that a
824 ;; -*- line in the first file of a compressed tar
825 ;; file doesn't override tar-mode.
826 (setq inhibit-first-line-modes-suffixes
827 (cons (jka-compr-info-regexp x)
828 inhibit-first-line-modes-suffixes)))))
829 jka-compr-compression-info-list)
830 (setq auto-mode-alist
831 (append auto-mode-alist jka-compr-mode-alist-additions)))
834 (defun jka-compr-uninstall ()
835 "Uninstall jka-compr.
836 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
837 and `inhibit-first-line-modes-suffixes' that were added
838 by `jka-compr-installed'."
839 ;; Delete from inhibit-first-line-modes-suffixes
840 ;; what jka-compr-install added.
841 (mapcar
842 (function (lambda (x)
843 (and (jka-compr-info-strip-extension x)
844 (setq inhibit-first-line-modes-suffixes
845 (delete (jka-compr-info-regexp x)
846 inhibit-first-line-modes-suffixes)))))
847 jka-compr-compression-info-list)
849 (let* ((fnha (cons nil file-name-handler-alist))
850 (last fnha))
852 (while (cdr last)
853 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
854 (setcdr last (cdr (cdr last)))
855 (setq last (cdr last))))
857 (setq file-name-handler-alist (cdr fnha)))
859 (let* ((ama (cons nil auto-mode-alist))
860 (last ama)
861 entry)
863 (while (cdr last)
864 (setq entry (car (cdr last)))
865 (if (or (member entry jka-compr-mode-alist-additions)
866 (and (consp (cdr entry))
867 (eq (nth 2 entry) 'jka-compr)))
868 (setcdr last (cdr (cdr last)))
869 (setq last (cdr last))))
871 (setq auto-mode-alist (cdr ama)))
873 (let* ((ama (cons nil file-coding-system-alist))
874 (last ama)
875 entry)
877 (while (cdr last)
878 (setq entry (car (cdr last)))
879 (if (member entry jka-compr-added-to-file-coding-system-alist)
880 (setcdr last (cdr (cdr last)))
881 (setq last (cdr last))))
883 (setq file-coding-system-alist (cdr ama))))
886 (defun jka-compr-installed-p ()
887 "Return non-nil if jka-compr is installed.
888 The return value is the entry in `file-name-handler-alist' for jka-compr."
890 (let ((fnha file-name-handler-alist)
891 (installed nil))
893 (while (and fnha (not installed))
894 (and (eq (cdr (car fnha)) 'jka-compr-handler)
895 (setq installed (car fnha)))
896 (setq fnha (cdr fnha)))
898 installed))
901 ;;; Add the file I/O hook if it does not already exist.
902 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
903 ;;; entry for jka-compr in file-name-handler-alist.
904 (and (jka-compr-installed-p)
905 (jka-compr-uninstall))
907 (jka-compr-install)
910 (provide 'jka-compr)
912 ;; jka-compr.el ends here.