Added gud-speedbar-buttons, and support for GDB buttons.
[emacs.git] / lisp / jka-compr.el
blobf66b84291c2b0f453321d20dbadd94ecf7fc2e03
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 t)
123 ;;; I have this defined so that .Z files are assumed to be in unix
124 ;;; compress format; and .gz files, in gzip format.
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 ["\\.tgz\\'"
135 "zipping" "gzip" ("-c" "-q")
136 "unzipping" "gzip" ("-c" "-q" "-d")
137 t nil]
138 ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'"
139 "zipping" "gzip" ("-c" "-q")
140 "unzipping" "gzip" ("-c" "-q" "-d")
141 t t])
143 "List of vectors that describe available compression techniques.
144 Each element, which describes a compression technique, is a vector of
145 the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS
146 UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS
147 APPEND-FLAG EXTENSION], where:
149 regexp is a regexp that matches filenames that are
150 compressed with this format
152 compress-msg is the message to issue to the user when doing this
153 type of compression (nil means no message)
155 compress-program is a program that performs this compression
157 compress-args is a list of args to pass to the compress program
159 uncompress-msg is the message to issue to the user when doing this
160 type of uncompression (nil means no message)
162 uncompress-program is a program that performs this compression
164 uncompress-args is a list of args to pass to the uncompress program
166 append-flag is non-nil if this compression technique can be
167 appended
169 auto-mode flag non-nil means strip the regexp from file names
170 before attempting to set the mode.
172 Because of the way `call-process' is defined, discarding the stderr output of
173 a program adds the overhead of starting a shell each time the program is
174 invoked."
175 :type '(repeat (vector regexp
176 (choice :tag "Compress Message"
177 (string :format "%v")
178 (const :tag "No Message" nil))
179 (string :tag "Compress Program")
180 (repeat :tag "Compress Arguments" string)
181 (choice :tag "Uncompress Message"
182 (string :format "%v")
183 (const :tag "No Message" nil))
184 (string :tag "Uncompress Program")
185 (repeat :tag "Uncompress Arguments" string)
186 (boolean :tag "Append")
187 (boolean :tag "Auto Mode")))
188 :group 'jka-compr)
190 (defvar jka-compr-mode-alist-additions
191 (list (cons "\\.tgz\\'" 'tar-mode))
192 "A list of pairs to add to `auto-mode-alist' when jka-compr is installed.")
194 ;; List of all the elements we actually added to file-coding-system-alist.
195 (defvar jka-compr-added-to-file-coding-system-alist nil)
197 (defvar jka-compr-file-name-handler-entry
199 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.")
201 ;;; Functions for accessing the return value of jka-compr-get-compression-info
202 (defun jka-compr-info-regexp (info) (aref info 0))
203 (defun jka-compr-info-compress-message (info) (aref info 1))
204 (defun jka-compr-info-compress-program (info) (aref info 2))
205 (defun jka-compr-info-compress-args (info) (aref info 3))
206 (defun jka-compr-info-uncompress-message (info) (aref info 4))
207 (defun jka-compr-info-uncompress-program (info) (aref info 5))
208 (defun jka-compr-info-uncompress-args (info) (aref info 6))
209 (defun jka-compr-info-can-append (info) (aref info 7))
210 (defun jka-compr-info-strip-extension (info) (aref info 8))
213 (defun jka-compr-get-compression-info (filename)
214 "Return information about the compression scheme of FILENAME.
215 The determination as to which compression scheme, if any, to use is
216 based on the filename itself and `jka-compr-compression-info-list'."
217 (catch 'compression-info
218 (let ((case-fold-search nil))
219 (mapcar
220 (function (lambda (x)
221 (and (string-match (jka-compr-info-regexp x) filename)
222 (throw 'compression-info x))))
223 jka-compr-compression-info-list)
224 nil)))
227 (put 'compression-error 'error-conditions '(compression-error file-error error))
230 (defvar jka-compr-acceptable-retval-list '(0 2 141))
233 (defun jka-compr-error (prog args infile message &optional errfile)
235 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
236 (curbuf (current-buffer)))
237 (with-current-buffer errbuf
238 (widen) (erase-buffer)
239 (insert (format "Error while executing \"%s %s < %s\"\n\n"
240 prog
241 (mapconcat 'identity args " ")
242 infile))
244 (and errfile
245 (insert-file-contents errfile)))
246 (display-buffer errbuf))
248 (signal 'compression-error
249 (list "Opening input file" (format "error %s" message) infile)))
252 (defvar jka-compr-dd-program
253 "/bin/dd")
256 (defvar jka-compr-dd-blocksize 256)
259 (defun jka-compr-partial-uncompress (prog message args infile beg len)
260 "Call program PROG with ARGS args taking input from INFILE.
261 Fourth and fifth args, BEG and LEN, specify which part of the output
262 to keep: LEN chars starting BEG chars from the beginning."
263 (let* ((skip (/ beg jka-compr-dd-blocksize))
264 (prefix (- beg (* skip jka-compr-dd-blocksize)))
265 (count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize))))
266 (start (point))
267 (err-file (jka-compr-make-temp-name))
268 (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null"
269 prog
270 (mapconcat 'identity args " ")
271 err-file
272 jka-compr-dd-program
273 jka-compr-dd-blocksize
274 skip
275 ;; dd seems to be unreliable about
276 ;; providing the last block. So, always
277 ;; read one more than you think you need.
278 (if count (concat "count=" (1+ count)) ""))))
280 (unwind-protect
281 (or (memq (call-process jka-compr-shell
282 infile t nil "-c"
283 run-string)
284 jka-compr-acceptable-retval-list)
286 (jka-compr-error prog args infile message err-file))
288 (jka-compr-delete-temp-file err-file))
290 ;; Delete the stuff after what we want, if there is any.
291 (and
293 (< (+ start prefix len) (point))
294 (delete-region (+ start prefix len) (point)))
296 ;; Delete the stuff before what we want.
297 (delete-region start (+ start prefix))))
300 (defun jka-compr-call-process (prog message infile output temp args)
301 (if jka-compr-use-shell
303 (let ((err-file (jka-compr-make-temp-name))
304 (coding-system-for-read (or coding-system-for-read 'undecided))
305 (coding-system-for-write 'no-conversion) )
307 (unwind-protect
309 (or (memq
310 (call-process jka-compr-shell infile
311 (if (stringp output) nil output)
313 "-c"
314 (format "%s %s 2> %s %s"
315 prog
316 (mapconcat 'identity args " ")
317 err-file
318 (if (stringp output)
319 (concat "> " output)
320 "")))
321 jka-compr-acceptable-retval-list)
323 (jka-compr-error prog args infile message err-file))
325 (jka-compr-delete-temp-file err-file)))
327 (or (zerop
328 (apply 'call-process
329 prog
330 infile
331 (if (stringp output) temp output)
333 args))
334 (jka-compr-error prog args infile message))
336 (and (stringp output)
337 (with-current-buffer temp
338 (write-region (point-min) (point-max) output)
339 (erase-buffer)))))
342 ;;; Support for temp files. Much of this was inspired if not lifted
343 ;;; from ange-ftp.
345 (defcustom jka-compr-temp-name-template
346 (expand-file-name "jka-com"
347 (or (getenv "TMPDIR") "/tmp/"))
348 "Prefix added to all temp files created by jka-compr.
349 There should be no more than seven characters after the final `/'."
350 :type 'string
351 :group 'jka-compr)
353 (defvar jka-compr-temp-name-table (make-vector 31 nil))
355 (defun jka-compr-make-temp-name (&optional local-copy)
356 "This routine will return the name of a new file."
357 (let* ((lastchar ?a)
358 (prevchar ?a)
359 (template (concat jka-compr-temp-name-template "aa"))
360 (lastpos (1- (length template)))
361 (not-done t)
362 file
363 entry)
365 (while not-done
366 (aset template lastpos lastchar)
367 (setq file (concat (make-temp-name template) "#"))
368 (setq entry (intern file jka-compr-temp-name-table))
369 (if (or (get entry 'active)
370 (file-exists-p file))
372 (progn
373 (setq lastchar (1+ lastchar))
374 (if (> lastchar ?z)
375 (progn
376 (setq prevchar (1+ prevchar))
377 (setq lastchar ?a)
378 (if (> prevchar ?z)
379 (error "Can't allocate temp file.")
380 (aset template (1- lastpos) prevchar)))))
382 (put entry 'active (not local-copy))
383 (setq not-done nil)))
385 file))
388 (defun jka-compr-delete-temp-file (temp)
390 (put (intern temp jka-compr-temp-name-table)
391 'active nil)
393 (condition-case ()
394 (delete-file temp)
395 (error nil)))
398 (defun jka-compr-write-region (start end file &optional append visit)
399 (let* ((filename (expand-file-name file))
400 (visit-file (if (stringp visit) (expand-file-name visit) filename))
401 (info (jka-compr-get-compression-info visit-file)))
403 (if info
405 (let ((can-append (jka-compr-info-can-append info))
406 (compress-program (jka-compr-info-compress-program info))
407 (compress-message (jka-compr-info-compress-message info))
408 (uncompress-program (jka-compr-info-uncompress-program info))
409 (uncompress-message (jka-compr-info-uncompress-message info))
410 (compress-args (jka-compr-info-compress-args info))
411 (uncompress-args (jka-compr-info-uncompress-args info))
412 (base-name (file-name-nondirectory visit-file))
413 temp-file temp-buffer)
415 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
416 (with-current-buffer temp-buffer
417 (widen) (erase-buffer))
419 (if (and append
420 (not can-append)
421 (file-exists-p filename))
423 (let* ((local-copy (file-local-copy filename))
424 (local-file (or local-copy filename)))
426 (setq temp-file local-file))
428 (setq temp-file (jka-compr-make-temp-name)))
430 (and
431 compress-message
432 (message "%s %s..." compress-message base-name))
434 (jka-compr-run-real-handler 'write-region
435 (list start end temp-file t 'dont))
437 ;; Here we must read the output of compress program as is
438 ;; without any code conversion.
439 (let ((coding-system-for-read 'no-conversion))
440 (jka-compr-call-process compress-program
441 (concat compress-message
442 " " base-name)
443 temp-file
444 temp-buffer
446 compress-args))
448 (with-current-buffer temp-buffer
449 (let ((coding-system-for-write 'no-conversion))
450 (if (memq system-type '(ms-dos windows-nt))
451 (setq buffer-file-type t) )
452 (jka-compr-run-real-handler 'write-region
453 (list (point-min) (point-max)
454 filename
455 (and append can-append) 'dont))
456 (erase-buffer)) )
458 (jka-compr-delete-temp-file temp-file)
460 (and
461 compress-message
462 (message "%s %s...done" compress-message base-name))
464 (cond
465 ((eq visit t)
466 (setq buffer-file-name filename)
467 (set-visited-file-modtime))
468 ((stringp visit)
469 (setq buffer-file-name visit)
470 (let ((buffer-file-name filename))
471 (set-visited-file-modtime))))
473 (and (or (eq visit t)
474 (eq visit nil)
475 (stringp visit))
476 (message "Wrote %s" visit-file))
478 nil)
480 (jka-compr-run-real-handler 'write-region
481 (list start end filename append visit)))))
484 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
485 (barf-if-buffer-read-only)
487 (and (or beg end)
488 visit
489 (error "Attempt to visit less than an entire file"))
491 (let* ((filename (expand-file-name file))
492 (info (jka-compr-get-compression-info filename)))
494 (if info
496 (let ((uncompress-message (jka-compr-info-uncompress-message info))
497 (uncompress-program (jka-compr-info-uncompress-program info))
498 (uncompress-args (jka-compr-info-uncompress-args info))
499 (base-name (file-name-nondirectory filename))
500 (notfound nil)
501 (local-copy
502 (jka-compr-run-real-handler 'file-local-copy (list filename)))
503 local-file
504 size start
505 (coding-system-for-read
506 (or coding-system-for-read
507 ;; If multibyte characters are disabled,
508 ;; don't do that conversion.
509 (and (null enable-multibyte-characters)
510 'raw-text)
511 (let ((tail file-coding-system-alist)
512 (newfile
513 (jka-compr-byte-compiler-base-file-name file))
514 result)
515 (while tail
516 (if (string-match (car (car tail)) newfile)
517 (setq result (car (cdr (car tail)))
518 tail nil))
519 (setq tail (cdr tail)))
520 result)
521 'undecided)) )
523 (setq local-file (or local-copy filename))
525 (and
526 visit
527 (setq buffer-file-name filename))
529 (unwind-protect ; to make sure local-copy gets deleted
531 (progn
533 (and
534 uncompress-message
535 (message "%s %s..." uncompress-message base-name))
537 (condition-case error-code
539 (progn
540 (if replace
541 (goto-char (point-min)))
542 (setq start (point))
543 (if (or beg end)
544 (jka-compr-partial-uncompress uncompress-program
545 (concat uncompress-message
546 " " base-name)
547 uncompress-args
548 local-file
549 (or beg 0)
550 (if (and beg end)
551 (- end beg)
552 end))
553 ;; If visiting, bind off buffer-file-name so that
554 ;; file-locking will not ask whether we should
555 ;; really edit the buffer.
556 (let ((buffer-file-name
557 (if visit nil buffer-file-name)))
558 (jka-compr-call-process uncompress-program
559 (concat uncompress-message
560 " " base-name)
561 local-file
564 uncompress-args)))
565 (setq size (- (point) start))
566 (if replace
567 (let* ((del-beg (point))
568 (del-end (+ del-beg size)))
569 (delete-region del-beg
570 (min del-end (point-max)))))
571 (goto-char start))
572 (error
573 (if (and (eq (car error-code) 'file-error)
574 (eq (nth 3 error-code) local-file))
575 (if visit
576 (setq notfound error-code)
577 (signal 'file-error
578 (cons "Opening input file"
579 (nthcdr 2 error-code))))
580 (signal (car error-code) (cdr error-code))))))
582 (and
583 local-copy
584 (file-exists-p local-copy)
585 (delete-file local-copy)))
587 (and
588 visit
589 (progn
590 (unlock-buffer)
591 (setq buffer-file-name filename)
592 (set-visited-file-modtime)))
594 (and
595 uncompress-message
596 (message "%s %s...done" uncompress-message base-name))
598 (and
599 visit
600 notfound
601 (signal 'file-error
602 (cons "Opening input file" (nth 2 notfound))))
604 ;; Run the functions that insert-file-contents would.
605 (let ((p after-insert-file-functions)
606 (insval size))
607 (while p
608 (setq insval (funcall (car p) size))
609 (if insval
610 (progn
611 (or (integerp insval)
612 (signal 'wrong-type-argument
613 (list 'integerp insval)))
614 (setq size insval)))
615 (setq p (cdr p))))
617 (list filename size))
619 (jka-compr-run-real-handler 'insert-file-contents
620 (list file visit beg end replace)))))
623 (defun jka-compr-file-local-copy (file)
624 (let* ((filename (expand-file-name file))
625 (info (jka-compr-get-compression-info filename)))
627 (if info
629 (let ((uncompress-message (jka-compr-info-uncompress-message info))
630 (uncompress-program (jka-compr-info-uncompress-program info))
631 (uncompress-args (jka-compr-info-uncompress-args info))
632 (base-name (file-name-nondirectory filename))
633 (local-copy
634 (jka-compr-run-real-handler 'file-local-copy (list filename)))
635 (temp-file (jka-compr-make-temp-name t))
636 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
637 (notfound nil)
638 local-file)
640 (setq local-file (or local-copy filename))
642 (unwind-protect
644 (with-current-buffer temp-buffer
646 (and
647 uncompress-message
648 (message "%s %s..." uncompress-message base-name))
650 ;; Here we must read the output of uncompress program
651 ;; and write it to TEMP-FILE without any code
652 ;; conversion. An appropriate code conversion (if
653 ;; necessary) is done by the later I/O operation
654 ;; (e.g. load).
655 (let ((coding-system-for-read 'no-conversion)
656 (coding-system-for-write 'no-conversion))
658 (jka-compr-call-process uncompress-program
659 (concat uncompress-message
660 " " base-name)
661 local-file
664 uncompress-args)
666 (and
667 uncompress-message
668 (message "%s %s...done" uncompress-message base-name))
670 (write-region
671 (point-min) (point-max) temp-file nil 'dont)))
673 (and
674 local-copy
675 (file-exists-p local-copy)
676 (delete-file local-copy))
678 (kill-buffer temp-buffer))
680 temp-file)
682 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
685 ;;; Support for loading compressed files.
686 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
687 "Documented as original."
689 (let* ((local-copy (jka-compr-file-local-copy file))
690 (load-file (or local-copy file)))
692 (unwind-protect
694 (let (inhibit-file-name-operation
695 inhibit-file-name-handlers)
696 (or nomessage
697 (message "Loading %s..." file))
699 (let ((load-force-doc-strings t))
700 (load load-file noerror t t))
702 (or nomessage
703 (message "Loading %s...done." file)))
705 (jka-compr-delete-temp-file local-copy))
709 (defun jka-compr-byte-compiler-base-file-name (file)
710 (let ((info (jka-compr-get-compression-info file)))
711 (if (and info (jka-compr-info-strip-extension info))
712 (save-match-data
713 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
714 file)))
716 (put 'write-region 'jka-compr 'jka-compr-write-region)
717 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
718 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
719 (put 'load 'jka-compr 'jka-compr-load)
720 (put 'byte-compiler-base-file-name 'jka-compr
721 'jka-compr-byte-compiler-base-file-name)
723 (defvar jka-compr-inhibit nil
724 "Non-nil means inhibit automatic uncompression temporarily.
725 Lisp programs can bind this to t to do that.
726 It is not recommended to set this variable permanently to anything but nil.")
728 (defun jka-compr-handler (operation &rest args)
729 (save-match-data
730 (let ((jka-op (get operation 'jka-compr)))
731 (if (and jka-op (not jka-compr-inhibit))
732 (apply jka-op args)
733 (jka-compr-run-real-handler operation args)))))
735 ;; If we are given an operation that we don't handle,
736 ;; call the Emacs primitive for that operation,
737 ;; and manipulate the inhibit variables
738 ;; to prevent the primitive from calling our handler again.
739 (defun jka-compr-run-real-handler (operation args)
740 (let ((inhibit-file-name-handlers
741 (cons 'jka-compr-handler
742 (and (eq inhibit-file-name-operation operation)
743 inhibit-file-name-handlers)))
744 (inhibit-file-name-operation operation))
745 (apply operation args)))
747 ;;;###autoload(defun auto-compression-mode (&optional arg)
748 ;;;###autoload "\
749 ;;;###autoloadToggle automatic file compression and uncompression.
750 ;;;###autoloadWith prefix argument ARG, turn auto compression on if positive, else off.
751 ;;;###autoloadReturns the new status of auto compression (non-nil means on)."
752 ;;;###autoload (interactive "P")
753 ;;;###autoload (if (not (fboundp 'jka-compr-installed-p))
754 ;;;###autoload (progn
755 ;;;###autoload (require 'jka-compr)
756 ;;;###autoload ;; That turned the mode on, so make it initially off.
757 ;;;###autoload (toggle-auto-compression)))
758 ;;;###autoload (toggle-auto-compression arg t))
760 (defun toggle-auto-compression (&optional arg message)
761 "Toggle automatic file compression and uncompression.
762 With prefix argument ARG, turn auto compression on if positive, else off.
763 Returns the new status of auto compression (non-nil means on).
764 If the argument MESSAGE is non-nil, it means to print a message
765 saying whether the mode is now on or off."
766 (interactive "P\np")
767 (let* ((installed (jka-compr-installed-p))
768 (flag (if (null arg)
769 (not installed)
770 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))))
772 (cond
773 ((and flag installed) t) ; already installed
775 ((and (not flag) (not installed)) nil) ; already not installed
777 (flag
778 (jka-compr-install))
781 (jka-compr-uninstall)))
784 (and message
785 (if flag
786 (message "Automatic file (de)compression is now ON.")
787 (message "Automatic file (de)compression is now OFF.")))
789 flag))
791 (defun jka-compr-build-file-regexp ()
792 (concat
793 "\\("
794 (mapconcat
795 'jka-compr-info-regexp
796 jka-compr-compression-info-list
797 "\\)\\|\\(")
798 "\\)"))
801 (defun jka-compr-install ()
802 "Install jka-compr.
803 This adds entries to `file-name-handler-alist' and `auto-mode-alist'
804 and `inhibit-first-line-modes-suffixes'."
806 (setq jka-compr-file-name-handler-entry
807 (cons (jka-compr-build-file-regexp) 'jka-compr-handler))
809 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry
810 file-name-handler-alist))
812 (setq jka-compr-added-to-file-coding-system-alist nil)
814 (mapcar
815 (function (lambda (x)
816 ;; Don't do multibyte encoding on the compressed files.
817 (let ((elt (cons (jka-compr-info-regexp x)
818 '(no-conversion . no-conversion))))
819 (setq file-coding-system-alist
820 (cons elt file-coding-system-alist))
821 (setq jka-compr-added-to-file-coding-system-alist
822 (cons elt jka-compr-added-to-file-coding-system-alist)))
824 (and (jka-compr-info-strip-extension x)
825 ;; Make entries in auto-mode-alist so that modes
826 ;; are chosen right according to the file names
827 ;; sans `.gz'.
828 (setq auto-mode-alist
829 (cons (list (jka-compr-info-regexp x)
830 nil 'jka-compr)
831 auto-mode-alist))
832 ;; Also add these regexps to
833 ;; inhibit-first-line-modes-suffixes, so that a
834 ;; -*- line in the first file of a compressed tar
835 ;; file doesn't override tar-mode.
836 (setq inhibit-first-line-modes-suffixes
837 (cons (jka-compr-info-regexp x)
838 inhibit-first-line-modes-suffixes)))))
839 jka-compr-compression-info-list)
840 (setq auto-mode-alist
841 (append auto-mode-alist jka-compr-mode-alist-additions)))
844 (defun jka-compr-uninstall ()
845 "Uninstall jka-compr.
846 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
847 and `inhibit-first-line-modes-suffixes' that were added
848 by `jka-compr-installed'."
849 ;; Delete from inhibit-first-line-modes-suffixes
850 ;; what jka-compr-install added.
851 (mapcar
852 (function (lambda (x)
853 (and (jka-compr-info-strip-extension x)
854 (setq inhibit-first-line-modes-suffixes
855 (delete (jka-compr-info-regexp x)
856 inhibit-first-line-modes-suffixes)))))
857 jka-compr-compression-info-list)
859 (let* ((fnha (cons nil file-name-handler-alist))
860 (last fnha))
862 (while (cdr last)
863 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
864 (setcdr last (cdr (cdr last)))
865 (setq last (cdr last))))
867 (setq file-name-handler-alist (cdr fnha)))
869 (let* ((ama (cons nil auto-mode-alist))
870 (last ama)
871 entry)
873 (while (cdr last)
874 (setq entry (car (cdr last)))
875 (if (or (member entry jka-compr-mode-alist-additions)
876 (and (consp (cdr entry))
877 (eq (nth 2 entry) 'jka-compr)))
878 (setcdr last (cdr (cdr last)))
879 (setq last (cdr last))))
881 (setq auto-mode-alist (cdr ama)))
883 (let* ((ama (cons nil file-coding-system-alist))
884 (last ama)
885 entry)
887 (while (cdr last)
888 (setq entry (car (cdr last)))
889 (if (member entry jka-compr-added-to-file-coding-system-alist)
890 (setcdr last (cdr (cdr last)))
891 (setq last (cdr last))))
893 (setq file-coding-system-alist (cdr ama))))
896 (defun jka-compr-installed-p ()
897 "Return non-nil if jka-compr is installed.
898 The return value is the entry in `file-name-handler-alist' for jka-compr."
900 (let ((fnha file-name-handler-alist)
901 (installed nil))
903 (while (and fnha (not installed))
904 (and (eq (cdr (car fnha)) 'jka-compr-handler)
905 (setq installed (car fnha)))
906 (setq fnha (cdr fnha)))
908 installed))
911 ;;; Add the file I/O hook if it does not already exist.
912 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
913 ;;; entry for jka-compr in file-name-handler-alist.
914 (and (jka-compr-installed-p)
915 (jka-compr-uninstall))
917 (jka-compr-install)
920 (provide 'jka-compr)
922 ;; jka-compr.el ends here.