]> code.delx.au - gnu-emacs/blob - lisp/jka-compr.el
(jka-compr-compression-info-list): Fix previous change:
[gnu-emacs] / lisp / jka-compr.el
1 ;;; jka-compr.el --- reading/writing/loading compressed files
2
3 ;; Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
4
5 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
6 ;; Maintainer: FSF
7 ;; Keywords: data
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
35
36
37 ;; INSTRUCTIONS:
38 ;;
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).
43 ;;
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.
48 ;;
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.
57 ;;
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.
62
63
64 ;; APPLICATION NOTES:
65 ;;
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.
76 ;;
77
78
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.
84 ;;
85 ;; Jean-loup Gailly
86 ;; David Hughes
87 ;; Richard Pieri
88 ;; Daniel Quinlan
89 ;; Chris P. Ross
90 ;; Rick Sladkey
91 ;;
92 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
93 ;; Version 18 of Emacs.
94 ;;
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.
99 ;;
100
101 ;;; Code:
102
103 (defgroup compression nil
104 "Data compression utilities"
105 :group 'data)
106
107 (defgroup jka-compr nil
108 "jka-compr customization"
109 :group 'compression)
110
111
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)
119
120 (defvar jka-compr-use-shell t)
121
122
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])
146
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:
152
153 regexp is a regexp that matches filenames that are
154 compressed with this format
155
156 compress-msg is the message to issue to the user when doing this
157 type of compression (nil means no message)
158
159 compress-program is a program that performs this compression
160
161 compress-args is a list of args to pass to the compress program
162
163 uncompress-msg is the message to issue to the user when doing this
164 type of uncompression (nil means no message)
165
166 uncompress-program is a program that performs this compression
167
168 uncompress-args is a list of args to pass to the uncompress program
169
170 append-flag is non-nil if this compression technique can be
171 appended
172
173 auto-mode flag non-nil means strip the regexp from file names
174 before attempting to set the mode.
175
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)
193
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.")
197
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)
200
201 (defvar jka-compr-file-name-handler-entry
202 nil
203 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.")
204 \f
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))
215
216
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)))
229
230
231 (put 'compression-error 'error-conditions '(compression-error file-error error))
232
233
234 (defvar jka-compr-acceptable-retval-list '(0 2 141))
235
236
237 (defun jka-compr-error (prog args infile message &optional errfile)
238
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))
247
248 (and errfile
249 (insert-file-contents errfile)))
250 (display-buffer errbuf))
251
252 (signal 'compression-error
253 (list "Opening input file" (format "error %s" message) infile)))
254
255
256 (defvar jka-compr-dd-program
257 "/bin/dd")
258
259
260 (defvar jka-compr-dd-blocksize 256)
261
262
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)) ""))))
283
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)
289
290 (jka-compr-error prog args infile message err-file))
291
292 (jka-compr-delete-temp-file err-file))
293
294 ;; Delete the stuff after what we want, if there is any.
295 (and
296 len
297 (< (+ start prefix len) (point))
298 (delete-region (+ start prefix len) (point)))
299
300 ;; Delete the stuff before what we want.
301 (delete-region start (+ start prefix))))
302
303
304 (defun jka-compr-call-process (prog message infile output temp args)
305 (if jka-compr-use-shell
306
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) )
310
311 (unwind-protect
312
313 (or (memq
314 (call-process jka-compr-shell infile
315 (if (stringp output) nil output)
316 nil
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)
326
327 (jka-compr-error prog args infile message err-file))
328
329 (jka-compr-delete-temp-file err-file)))
330
331 (or (zerop
332 (apply 'call-process
333 prog
334 infile
335 (if (stringp output) temp output)
336 nil
337 args))
338 (jka-compr-error prog args infile message))
339
340 (and (stringp output)
341 (with-current-buffer temp
342 (write-region (point-min) (point-max) output)
343 (erase-buffer)))))
344
345
346 ;;; Support for temp files. Much of this was inspired if not lifted
347 ;;; from ange-ftp.
348
349 (defcustom jka-compr-temp-name-template
350 (expand-file-name "jka-com"
351 (or (getenv "TMPDIR") "/tmp/"))
352 "Prefix added to all temp files created by jka-compr.
353 There should be no more than seven characters after the final `/'."
354 :type 'string
355 :group 'jka-compr)
356
357 (defvar jka-compr-temp-name-table (make-vector 31 nil))
358
359 (defun jka-compr-make-temp-name (&optional local-copy)
360 "This routine will return the name of a new file."
361 (let* ((lastchar ?a)
362 (prevchar ?a)
363 (template (concat jka-compr-temp-name-template "aa"))
364 (lastpos (1- (length template)))
365 (not-done t)
366 file
367 entry)
368
369 (while not-done
370 (aset template lastpos lastchar)
371 (setq file (concat (make-temp-name template) "#"))
372 (setq entry (intern file jka-compr-temp-name-table))
373 (if (or (get entry 'active)
374 (file-exists-p file))
375
376 (progn
377 (setq lastchar (1+ lastchar))
378 (if (> lastchar ?z)
379 (progn
380 (setq prevchar (1+ prevchar))
381 (setq lastchar ?a)
382 (if (> prevchar ?z)
383 (error "Can't allocate temp file.")
384 (aset template (1- lastpos) prevchar)))))
385
386 (put entry 'active (not local-copy))
387 (setq not-done nil)))
388
389 file))
390
391
392 (defun jka-compr-delete-temp-file (temp)
393
394 (put (intern temp jka-compr-temp-name-table)
395 'active nil)
396
397 (condition-case ()
398 (delete-file temp)
399 (error nil)))
400
401
402 (defun jka-compr-write-region (start end file &optional append visit)
403 (let* ((filename (expand-file-name file))
404 (visit-file (if (stringp visit) (expand-file-name visit) filename))
405 (info (jka-compr-get-compression-info visit-file)))
406
407 (if info
408
409 (let ((can-append (jka-compr-info-can-append info))
410 (compress-program (jka-compr-info-compress-program info))
411 (compress-message (jka-compr-info-compress-message info))
412 (uncompress-program (jka-compr-info-uncompress-program info))
413 (uncompress-message (jka-compr-info-uncompress-message info))
414 (compress-args (jka-compr-info-compress-args info))
415 (uncompress-args (jka-compr-info-uncompress-args info))
416 (base-name (file-name-nondirectory visit-file))
417 temp-file temp-buffer)
418
419 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
420 (with-current-buffer temp-buffer
421 (widen) (erase-buffer))
422
423 (if (and append
424 (not can-append)
425 (file-exists-p filename))
426
427 (let* ((local-copy (file-local-copy filename))
428 (local-file (or local-copy filename)))
429
430 (setq temp-file local-file))
431
432 (setq temp-file (jka-compr-make-temp-name)))
433
434 (and
435 compress-message
436 (message "%s %s..." compress-message base-name))
437
438 (jka-compr-run-real-handler 'write-region
439 (list start end temp-file t 'dont))
440
441 ;; Here we must read the output of compress program as is
442 ;; without any code conversion.
443 (let ((coding-system-for-read 'no-conversion))
444 (jka-compr-call-process compress-program
445 (concat compress-message
446 " " base-name)
447 temp-file
448 temp-buffer
449 nil
450 compress-args))
451
452 (with-current-buffer temp-buffer
453 (let ((coding-system-for-write 'no-conversion))
454 (if (memq system-type '(ms-dos windows-nt))
455 (setq buffer-file-type t) )
456 (jka-compr-run-real-handler 'write-region
457 (list (point-min) (point-max)
458 filename
459 (and append can-append) 'dont))
460 (erase-buffer)) )
461
462 (jka-compr-delete-temp-file temp-file)
463
464 (and
465 compress-message
466 (message "%s %s...done" compress-message base-name))
467
468 (cond
469 ((eq visit t)
470 (setq buffer-file-name filename)
471 (set-visited-file-modtime))
472 ((stringp visit)
473 (setq buffer-file-name visit)
474 (let ((buffer-file-name filename))
475 (set-visited-file-modtime))))
476
477 (and (or (eq visit t)
478 (eq visit nil)
479 (stringp visit))
480 (message "Wrote %s" visit-file))
481
482 nil)
483
484 (jka-compr-run-real-handler 'write-region
485 (list start end filename append visit)))))
486
487
488 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
489 (barf-if-buffer-read-only)
490
491 (and (or beg end)
492 visit
493 (error "Attempt to visit less than an entire file"))
494
495 (let* ((filename (expand-file-name file))
496 (info (jka-compr-get-compression-info filename)))
497
498 (if info
499
500 (let ((uncompress-message (jka-compr-info-uncompress-message info))
501 (uncompress-program (jka-compr-info-uncompress-program info))
502 (uncompress-args (jka-compr-info-uncompress-args info))
503 (base-name (file-name-nondirectory filename))
504 (notfound nil)
505 (local-copy
506 (jka-compr-run-real-handler 'file-local-copy (list filename)))
507 local-file
508 size start
509 (coding-system-for-read
510 (or coding-system-for-read
511 ;; If multibyte characters are disabled,
512 ;; don't do that conversion.
513 (and (null enable-multibyte-characters)
514 'raw-text)
515 (let ((tail file-coding-system-alist)
516 (newfile
517 (jka-compr-byte-compiler-base-file-name file))
518 result)
519 (while tail
520 (if (string-match (car (car tail)) newfile)
521 (setq result (car (cdr (car tail)))
522 tail nil))
523 (setq tail (cdr tail)))
524 result)
525 'undecided)) )
526
527 (setq local-file (or local-copy filename))
528
529 (and
530 visit
531 (setq buffer-file-name filename))
532
533 (unwind-protect ; to make sure local-copy gets deleted
534
535 (progn
536
537 (and
538 uncompress-message
539 (message "%s %s..." uncompress-message base-name))
540
541 (condition-case error-code
542
543 (progn
544 (if replace
545 (goto-char (point-min)))
546 (setq start (point))
547 (if (or beg end)
548 (jka-compr-partial-uncompress uncompress-program
549 (concat uncompress-message
550 " " base-name)
551 uncompress-args
552 local-file
553 (or beg 0)
554 (if (and beg end)
555 (- end beg)
556 end))
557 ;; If visiting, bind off buffer-file-name so that
558 ;; file-locking will not ask whether we should
559 ;; really edit the buffer.
560 (let ((buffer-file-name
561 (if visit nil buffer-file-name)))
562 (jka-compr-call-process uncompress-program
563 (concat uncompress-message
564 " " base-name)
565 local-file
566 t
567 nil
568 uncompress-args)))
569 (setq size (- (point) start))
570 (if replace
571 (let* ((del-beg (point))
572 (del-end (+ del-beg size)))
573 (delete-region del-beg
574 (min del-end (point-max)))))
575 (goto-char start))
576 (error
577 (if (and (eq (car error-code) 'file-error)
578 (eq (nth 3 error-code) local-file))
579 (if visit
580 (setq notfound error-code)
581 (signal 'file-error
582 (cons "Opening input file"
583 (nthcdr 2 error-code))))
584 (signal (car error-code) (cdr error-code))))))
585
586 (and
587 local-copy
588 (file-exists-p local-copy)
589 (delete-file local-copy)))
590
591 (and
592 visit
593 (progn
594 (unlock-buffer)
595 (setq buffer-file-name filename)
596 (set-visited-file-modtime)))
597
598 (and
599 uncompress-message
600 (message "%s %s...done" uncompress-message base-name))
601
602 (and
603 visit
604 notfound
605 (signal 'file-error
606 (cons "Opening input file" (nth 2 notfound))))
607
608 ;; Run the functions that insert-file-contents would.
609 (let ((p after-insert-file-functions)
610 (insval size))
611 (while p
612 (setq insval (funcall (car p) size))
613 (if insval
614 (progn
615 (or (integerp insval)
616 (signal 'wrong-type-argument
617 (list 'integerp insval)))
618 (setq size insval)))
619 (setq p (cdr p))))
620
621 (list filename size))
622
623 (jka-compr-run-real-handler 'insert-file-contents
624 (list file visit beg end replace)))))
625
626
627 (defun jka-compr-file-local-copy (file)
628 (let* ((filename (expand-file-name file))
629 (info (jka-compr-get-compression-info filename)))
630
631 (if info
632
633 (let ((uncompress-message (jka-compr-info-uncompress-message info))
634 (uncompress-program (jka-compr-info-uncompress-program info))
635 (uncompress-args (jka-compr-info-uncompress-args info))
636 (base-name (file-name-nondirectory filename))
637 (local-copy
638 (jka-compr-run-real-handler 'file-local-copy (list filename)))
639 (temp-file (jka-compr-make-temp-name t))
640 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
641 (notfound nil)
642 local-file)
643
644 (setq local-file (or local-copy filename))
645
646 (unwind-protect
647
648 (with-current-buffer temp-buffer
649
650 (and
651 uncompress-message
652 (message "%s %s..." uncompress-message base-name))
653
654 ;; Here we must read the output of uncompress program
655 ;; and write it to TEMP-FILE without any code
656 ;; conversion. An appropriate code conversion (if
657 ;; necessary) is done by the later I/O operation
658 ;; (e.g. load).
659 (let ((coding-system-for-read 'no-conversion)
660 (coding-system-for-write 'no-conversion))
661
662 (jka-compr-call-process uncompress-program
663 (concat uncompress-message
664 " " base-name)
665 local-file
666 t
667 nil
668 uncompress-args)
669
670 (and
671 uncompress-message
672 (message "%s %s...done" uncompress-message base-name))
673
674 (write-region
675 (point-min) (point-max) temp-file nil 'dont)))
676
677 (and
678 local-copy
679 (file-exists-p local-copy)
680 (delete-file local-copy))
681
682 (kill-buffer temp-buffer))
683
684 temp-file)
685
686 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
687
688
689 ;;; Support for loading compressed files.
690 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
691 "Documented as original."
692
693 (let* ((local-copy (jka-compr-file-local-copy file))
694 (load-file (or local-copy file)))
695
696 (unwind-protect
697
698 (let (inhibit-file-name-operation
699 inhibit-file-name-handlers)
700 (or nomessage
701 (message "Loading %s..." file))
702
703 (let ((load-force-doc-strings t))
704 (load load-file noerror t t))
705
706 (or nomessage
707 (message "Loading %s...done." file)))
708
709 (jka-compr-delete-temp-file local-copy))
710
711 t))
712
713 (defun jka-compr-byte-compiler-base-file-name (file)
714 (let ((info (jka-compr-get-compression-info file)))
715 (if (and info (jka-compr-info-strip-extension info))
716 (save-match-data
717 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
718 file)))
719 \f
720 (put 'write-region 'jka-compr 'jka-compr-write-region)
721 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
722 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
723 (put 'load 'jka-compr 'jka-compr-load)
724 (put 'byte-compiler-base-file-name 'jka-compr
725 'jka-compr-byte-compiler-base-file-name)
726
727 (defvar jka-compr-inhibit nil
728 "Non-nil means inhibit automatic uncompression temporarily.
729 Lisp programs can bind this to t to do that.
730 It is not recommended to set this variable permanently to anything but nil.")
731
732 (defun jka-compr-handler (operation &rest args)
733 (save-match-data
734 (let ((jka-op (get operation 'jka-compr)))
735 (if (and jka-op (not jka-compr-inhibit))
736 (apply jka-op args)
737 (jka-compr-run-real-handler operation args)))))
738
739 ;; If we are given an operation that we don't handle,
740 ;; call the Emacs primitive for that operation,
741 ;; and manipulate the inhibit variables
742 ;; to prevent the primitive from calling our handler again.
743 (defun jka-compr-run-real-handler (operation args)
744 (let ((inhibit-file-name-handlers
745 (cons 'jka-compr-handler
746 (and (eq inhibit-file-name-operation operation)
747 inhibit-file-name-handlers)))
748 (inhibit-file-name-operation operation))
749 (apply operation args)))
750
751 ;;;###autoload(defun auto-compression-mode (&optional arg)
752 ;;;###autoload "\
753 ;;;###autoloadToggle automatic file compression and uncompression.
754 ;;;###autoloadWith prefix argument ARG, turn auto compression on if positive, else off.
755 ;;;###autoloadReturns the new status of auto compression (non-nil means on)."
756 ;;;###autoload (interactive "P")
757 ;;;###autoload (if (not (fboundp 'jka-compr-installed-p))
758 ;;;###autoload (progn
759 ;;;###autoload (require 'jka-compr)
760 ;;;###autoload ;; That turned the mode on, so make it initially off.
761 ;;;###autoload (toggle-auto-compression)))
762 ;;;###autoload (toggle-auto-compression arg t))
763
764 (defun toggle-auto-compression (&optional arg message)
765 "Toggle automatic file compression and uncompression.
766 With prefix argument ARG, turn auto compression on if positive, else off.
767 Returns the new status of auto compression (non-nil means on).
768 If the argument MESSAGE is non-nil, it means to print a message
769 saying whether the mode is now on or off."
770 (interactive "P\np")
771 (let* ((installed (jka-compr-installed-p))
772 (flag (if (null arg)
773 (not installed)
774 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))))
775
776 (cond
777 ((and flag installed) t) ; already installed
778
779 ((and (not flag) (not installed)) nil) ; already not installed
780
781 (flag
782 (jka-compr-install))
783
784 (t
785 (jka-compr-uninstall)))
786
787
788 (and message
789 (if flag
790 (message "Automatic file (de)compression is now ON.")
791 (message "Automatic file (de)compression is now OFF.")))
792
793 flag))
794
795 (defun jka-compr-build-file-regexp ()
796 (concat
797 "\\("
798 (mapconcat
799 'jka-compr-info-regexp
800 jka-compr-compression-info-list
801 "\\)\\|\\(")
802 "\\)"))
803
804
805 (defun jka-compr-install ()
806 "Install jka-compr.
807 This adds entries to `file-name-handler-alist' and `auto-mode-alist'
808 and `inhibit-first-line-modes-suffixes'."
809
810 (setq jka-compr-file-name-handler-entry
811 (cons (jka-compr-build-file-regexp) 'jka-compr-handler))
812
813 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry
814 file-name-handler-alist))
815
816 (setq jka-compr-added-to-file-coding-system-alist nil)
817
818 (mapcar
819 (function (lambda (x)
820 ;; Don't do multibyte encoding on the compressed files.
821 (let ((elt (cons (jka-compr-info-regexp x)
822 '(no-conversion . no-conversion))))
823 (setq file-coding-system-alist
824 (cons elt file-coding-system-alist))
825 (setq jka-compr-added-to-file-coding-system-alist
826 (cons elt jka-compr-added-to-file-coding-system-alist)))
827
828 (and (jka-compr-info-strip-extension x)
829 ;; Make entries in auto-mode-alist so that modes
830 ;; are chosen right according to the file names
831 ;; sans `.gz'.
832 (setq auto-mode-alist
833 (cons (list (jka-compr-info-regexp x)
834 nil 'jka-compr)
835 auto-mode-alist))
836 ;; Also add these regexps to
837 ;; inhibit-first-line-modes-suffixes, so that a
838 ;; -*- line in the first file of a compressed tar
839 ;; file doesn't override tar-mode.
840 (setq inhibit-first-line-modes-suffixes
841 (cons (jka-compr-info-regexp x)
842 inhibit-first-line-modes-suffixes)))))
843 jka-compr-compression-info-list)
844 (setq auto-mode-alist
845 (append auto-mode-alist jka-compr-mode-alist-additions)))
846
847
848 (defun jka-compr-uninstall ()
849 "Uninstall jka-compr.
850 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
851 and `inhibit-first-line-modes-suffixes' that were added
852 by `jka-compr-installed'."
853 ;; Delete from inhibit-first-line-modes-suffixes
854 ;; what jka-compr-install added.
855 (mapcar
856 (function (lambda (x)
857 (and (jka-compr-info-strip-extension x)
858 (setq inhibit-first-line-modes-suffixes
859 (delete (jka-compr-info-regexp x)
860 inhibit-first-line-modes-suffixes)))))
861 jka-compr-compression-info-list)
862
863 (let* ((fnha (cons nil file-name-handler-alist))
864 (last fnha))
865
866 (while (cdr last)
867 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
868 (setcdr last (cdr (cdr last)))
869 (setq last (cdr last))))
870
871 (setq file-name-handler-alist (cdr fnha)))
872
873 (let* ((ama (cons nil auto-mode-alist))
874 (last ama)
875 entry)
876
877 (while (cdr last)
878 (setq entry (car (cdr last)))
879 (if (or (member entry jka-compr-mode-alist-additions)
880 (and (consp (cdr entry))
881 (eq (nth 2 entry) 'jka-compr)))
882 (setcdr last (cdr (cdr last)))
883 (setq last (cdr last))))
884
885 (setq auto-mode-alist (cdr ama)))
886
887 (let* ((ama (cons nil file-coding-system-alist))
888 (last ama)
889 entry)
890
891 (while (cdr last)
892 (setq entry (car (cdr last)))
893 (if (member entry jka-compr-added-to-file-coding-system-alist)
894 (setcdr last (cdr (cdr last)))
895 (setq last (cdr last))))
896
897 (setq file-coding-system-alist (cdr ama))))
898
899
900 (defun jka-compr-installed-p ()
901 "Return non-nil if jka-compr is installed.
902 The return value is the entry in `file-name-handler-alist' for jka-compr."
903
904 (let ((fnha file-name-handler-alist)
905 (installed nil))
906
907 (while (and fnha (not installed))
908 (and (eq (cdr (car fnha)) 'jka-compr-handler)
909 (setq installed (car fnha)))
910 (setq fnha (cdr fnha)))
911
912 installed))
913
914
915 ;;; Add the file I/O hook if it does not already exist.
916 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the
917 ;;; entry for jka-compr in file-name-handler-alist.
918 (and (jka-compr-installed-p)
919 (jka-compr-uninstall))
920
921 (jka-compr-install)
922
923
924 (provide 'jka-compr)
925
926 ;; jka-compr.el ends here.