*** empty log message ***
[bpt/emacs.git] / lisp / jka-compr.el
CommitLineData
be010748
RS
1;;; jka-compr.el --- reading/writing/loading compressed files
2
e5e5ef24 3;; Copyright (C) 1993, 1994, 1995, 1997, 1999 Free Software Foundation, Inc.
acd622cc
RS
4
5;; Author: jka@ece.cmu.edu (Jay K. Adams)
4228277d 6;; Maintainer: FSF
acd622cc
RS
7;; Keywords: data
8
f4454a14
RS
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
b578f267
EN
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.
f4454a14 25
acd622cc
RS
26;;; Commentary:
27
b578f267
EN
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;;
acd622cc
RS
100
101;;; Code:
102
bbf5eb28
RS
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"
acd622cc
RS
113 "*Shell to be used for calling compression programs.
114The value of this variable only matters if you want to discard the
115stderr of a compression/decompression program (see the documentation
bbf5eb28
RS
116for `jka-compr-compression-info-list')."
117 :type 'string
118 :group 'jka-compr)
acd622cc 119
eb915452
RS
120(defvar jka-compr-use-shell
121 (not (memq system-type '(ms-dos windows-nt))))
acd622cc
RS
122
123;;; I have this defined so that .Z files are assumed to be in unix
060c3cc9 124;;; compress format; and .gz files, in gzip format, and .bz2 files in bzip fmt.
bbf5eb28 125(defcustom jka-compr-compression-info-list
acd622cc 126 ;;[regexp
ede9c6a8
RS
127 ;; compr-message compr-prog compr-args
128 ;; uncomp-message uncomp-prog uncomp-args
acd622cc 129 ;; can-append auto-mode-flag]
094cf604 130 '(["\\.Z\\(~\\|\\.~[0-9]+~\\)?\\'"
acd622cc
RS
131 "compressing" "compress" ("-c")
132 "uncompressing" "uncompress" ("-c")
133 nil t]
df312987
DL
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".
060c3cc9 137 ["\\.bz2\\'"
df312987
DL
138 "bzip2ing" "bzip2" nil
139 "bunzip2ing" "bzip2" ("-d")
6b10a521 140 nil t]
74b2c737
RS
141 ["\\.tgz\\'"
142 "zipping" "gzip" ("-c" "-q")
143 "unzipping" "gzip" ("-c" "-q" "-d")
144 t nil]
094cf604 145 ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'"
acd622cc
RS
146 "zipping" "gzip" ("-c" "-q")
147 "unzipping" "gzip" ("-c" "-q" "-d")
148 t t])
149
150 "List of vectors that describe available compression techniques.
151Each element, which describes a compression technique, is a vector of
ede9c6a8
RS
152the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS
153UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS
154APPEND-FLAG EXTENSION], where:
acd622cc
RS
155
156 regexp is a regexp that matches filenames that are
157 compressed with this format
158
ede9c6a8
RS
159 compress-msg is the message to issue to the user when doing this
160 type of compression (nil means no message)
161
acd622cc
RS
162 compress-program is a program that performs this compression
163
164 compress-args is a list of args to pass to the compress program
165
ede9c6a8
RS
166 uncompress-msg is the message to issue to the user when doing this
167 type of uncompression (nil means no message)
acd622cc
RS
168
169 uncompress-program is a program that performs this compression
170
171 uncompress-args is a list of args to pass to the uncompress program
172
173 append-flag is non-nil if this compression technique can be
174 appended
175
176 auto-mode flag non-nil means strip the regexp from file names
177 before attempting to set the mode.
178
8fb1a583 179Because of the way `call-process' is defined, discarding the stderr output of
acd622cc 180a program adds the overhead of starting a shell each time the program is
bbf5eb28
RS
181invoked."
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)
acd622cc 196
74b2c737
RS
197(defvar jka-compr-mode-alist-additions
198 (list (cons "\\.tgz\\'" 'tar-mode))
4eec33ae
RS
199 "A list of pairs to add to `auto-mode-alist' when jka-compr is installed.")
200
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)
acd622cc 203
555235e6
RS
204(defvar jka-compr-file-name-handler-entry
205 nil
206 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.")
555235e6 207\f
43a3bdcc 208;;; Functions for accessing the return value of jka-compr-get-compression-info
acd622cc
RS
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))
218
219
220(defun jka-compr-get-compression-info (filename)
221 "Return information about the compression scheme of FILENAME.
222The determination as to which compression scheme, if any, to use is
99bee6a4 223based on the filename itself and `jka-compr-compression-info-list'."
acd622cc
RS
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)))
232
233
234(put 'compression-error 'error-conditions '(compression-error file-error error))
235
236
30c78e11 237(defvar jka-compr-acceptable-retval-list '(0 2 141))
acd622cc
RS
238
239
240(defun jka-compr-error (prog args infile message &optional errfile)
241
242 (let ((errbuf (get-buffer-create " *jka-compr-error*"))
243 (curbuf (current-buffer)))
75e9c107
RS
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))
250
251 (and errfile
252 (insert-file-contents errfile)))
acd622cc
RS
253 (display-buffer errbuf))
254
75e9c107
RS
255 (signal 'compression-error
256 (list "Opening input file" (format "error %s" message) infile)))
acd622cc
RS
257
258
259(defvar jka-compr-dd-program
260 "/bin/dd")
261
262
dfe05fac 263(defvar jka-compr-dd-blocksize 256)
acd622cc
RS
264
265
266(defun jka-compr-partial-uncompress (prog message args infile beg len)
267 "Call program PROG with ARGS args taking input from INFILE.
268Fourth and fifth args, BEG and LEN, specify which part of the output
ee139ed3 269to keep: LEN chars starting BEG chars from the beginning."
acd622cc
RS
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))
acd622cc
RS
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
dfe05fac
RS
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)) ""))))
acd622cc
RS
286
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)
292
293 (jka-compr-error prog args infile message err-file))
294
295 (jka-compr-delete-temp-file err-file))
296
ee139ed3 297 ;; Delete the stuff after what we want, if there is any.
acd622cc 298 (and
dfe05fac 299 len
ee139ed3 300 (< (+ start prefix len) (point))
dfe05fac 301 (delete-region (+ start prefix len) (point)))
acd622cc 302
ee139ed3 303 ;; Delete the stuff before what we want.
acd622cc
RS
304 (delete-region start (+ start prefix))))
305
306
307(defun jka-compr-call-process (prog message infile output temp args)
308 (if jka-compr-use-shell
309
a81635fc 310 (let ((err-file (jka-compr-make-temp-name))
baefb016 311 (coding-system-for-read (or coding-system-for-read 'undecided))
70c7850e 312 (coding-system-for-write 'no-conversion))
a81635fc 313
acd622cc
RS
314 (unwind-protect
315
316 (or (memq
317 (call-process jka-compr-shell infile
318 (if (stringp output) nil output)
319 nil
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)
329
330 (jka-compr-error prog args infile message err-file))
331
332 (jka-compr-delete-temp-file err-file)))
333
334 (or (zerop
335 (apply 'call-process
336 prog
337 infile
338 (if (stringp output) temp output)
339 nil
340 args))
341 (jka-compr-error prog args infile message))
342
343 (and (stringp output)
75e9c107 344 (with-current-buffer temp
acd622cc 345 (write-region (point-min) (point-max) output)
75e9c107 346 (erase-buffer)))))
acd622cc
RS
347
348
349;;; Support for temp files. Much of this was inspired if not lifted
350;;; from ange-ftp.
351
bbf5eb28 352(defcustom jka-compr-temp-name-template
11757e2f 353 (expand-file-name "jka-com" temporary-file-directory)
362b539a 354 "Prefix added to all temp files created by jka-compr.
bbf5eb28
RS
355There should be no more than seven characters after the final `/'."
356 :type 'string
357 :group 'jka-compr)
acd622cc 358
acd622cc
RS
359(defun jka-compr-make-temp-name (&optional local-copy)
360 "This routine will return the name of a new file."
767d12f2
SM
361 (make-temp-file jka-compr-temp-name-template))
362
363(defalias 'jka-compr-delete-temp-file 'delete-file)
acd622cc
RS
364
365
366(defun jka-compr-write-region (start end file &optional append visit)
acd622cc
RS
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)))
370
371 (if info
372
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))
acd622cc 380 (base-name (file-name-nondirectory visit-file))
70c7850e
KH
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))
acd622cc 386
75e9c107
RS
387 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
388 (with-current-buffer temp-buffer
389 (widen) (erase-buffer))
acd622cc 390
30c78e11
RS
391 (if (and append
392 (not can-append)
393 (file-exists-p filename))
394
395 (let* ((local-copy (file-local-copy filename))
396 (local-file (or local-copy filename)))
397
398 (setq temp-file local-file))
399
400 (setq temp-file (jka-compr-make-temp-name)))
acd622cc
RS
401
402 (and
403 compress-message
404 (message "%s %s..." compress-message base-name))
30c78e11 405
8fb1a583
RS
406 (jka-compr-run-real-handler 'write-region
407 (list start end temp-file t 'dont))
70c7850e
KH
408 ;; save value used by the real write-region
409 (setq coding-system-used last-coding-system-used)
acd622cc 410
baefb016
KH
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
419 nil
420 compress-args))
acd622cc 421
75e9c107 422 (with-current-buffer temp-buffer
a81635fc
RS
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)) )
acd622cc
RS
431
432 (jka-compr-delete-temp-file temp-file)
433
434 (and
435 compress-message
436 (message "%s %s...done" compress-message base-name))
437
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))))
446
447 (and (or (eq visit t)
448 (eq visit nil)
449 (stringp visit))
450 (message "Wrote %s" visit-file))
451
70c7850e
KH
452 ;; ensure `last-coding-system-used' has an appropriate value
453 (setq last-coding-system-used coding-system-used)
454
acd622cc
RS
455 nil)
456
8fb1a583
RS
457 (jka-compr-run-real-handler 'write-region
458 (list start end filename append visit)))))
acd622cc
RS
459
460
54b2aa5c 461(defun jka-compr-insert-file-contents (file &optional visit beg end replace)
acd622cc
RS
462 (barf-if-buffer-read-only)
463
464 (and (or beg end)
465 visit
466 (error "Attempt to visit less than an entire file"))
467
468 (let* ((filename (expand-file-name file))
469 (info (jka-compr-get-compression-info filename)))
470
471 (if info
472
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)
8fb1a583
RS
478 (local-copy
479 (jka-compr-run-real-handler 'file-local-copy (list filename)))
acd622cc 480 local-file
a81635fc 481 size start
4eec33ae
RS
482 (coding-system-for-read
483 (or coding-system-for-read
bab00368
RS
484 ;; If multibyte characters are disabled,
485 ;; don't do that conversion.
486 (and (null enable-multibyte-characters)
c352d959 487 (or (auto-coding-alist-lookup
f8d8f627 488 (jka-compr-byte-compiler-base-file-name file))
c352d959 489 'raw-text))
7ec4806e
RS
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)))
4eec33ae 494 'undecided)) )
acd622cc
RS
495
496 (setq local-file (or local-copy filename))
497
498 (and
499 visit
500 (setq buffer-file-name filename))
501
502 (unwind-protect ; to make sure local-copy gets deleted
503
504 (progn
505
506 (and
507 uncompress-message
508 (message "%s %s..." uncompress-message base-name))
509
510 (condition-case error-code
511
512 (progn
094cf604
RS
513 (if replace
514 (goto-char (point-min)))
acd622cc
RS
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))
ae849784
RS
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
535 t
536 nil
537 uncompress-args)))
acd622cc 538 (setq size (- (point) start))
094cf604
RS
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))
acd622cc
RS
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))))))
554
555 (and
556 local-copy
557 (file-exists-p local-copy)
558 (delete-file local-copy)))
559
560 (and
561 visit
562 (progn
8fb1a583 563 (unlock-buffer)
acd622cc
RS
564 (setq buffer-file-name filename)
565 (set-visited-file-modtime)))
566
567 (and
568 uncompress-message
569 (message "%s %s...done" uncompress-message base-name))
570
571 (and
572 visit
573 notfound
574 (signal 'file-error
575 (cons "Opening input file" (nth 2 notfound))))
576
5c6f2f2a
RS
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.
580
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))))
094cf604 593
acd622cc
RS
594 (list filename size))
595
8fb1a583
RS
596 (jka-compr-run-real-handler 'insert-file-contents
597 (list file visit beg end replace)))))
acd622cc
RS
598
599
600(defun jka-compr-file-local-copy (file)
acd622cc
RS
601 (let* ((filename (expand-file-name file))
602 (info (jka-compr-get-compression-info filename)))
603
604 (if info
605
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))
8fb1a583
RS
610 (local-copy
611 (jka-compr-run-real-handler 'file-local-copy (list filename)))
acd622cc 612 (temp-file (jka-compr-make-temp-name t))
30c78e11 613 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
acd622cc 614 (notfound nil)
acd622cc
RS
615 local-file)
616
617 (setq local-file (or local-copy filename))
618
619 (unwind-protect
620
75e9c107 621 (with-current-buffer temp-buffer
acd622cc
RS
622
623 (and
624 uncompress-message
625 (message "%s %s..." uncompress-message base-name))
acd622cc 626
baefb016
KH
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))
634
635 (jka-compr-call-process uncompress-program
636 (concat uncompress-message
637 " " base-name)
638 local-file
639 t
640 nil
641 uncompress-args)
642
643 (and
644 uncompress-message
645 (message "%s %s...done" uncompress-message base-name))
646
647 (write-region
648 (point-min) (point-max) temp-file nil 'dont)))
acd622cc
RS
649
650 (and
651 local-copy
652 (file-exists-p local-copy)
653 (delete-file local-copy))
654
acd622cc
RS
655 (kill-buffer temp-buffer))
656
657 temp-file)
658
8fb1a583 659 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
acd622cc
RS
660
661
662;;; Support for loading compressed files.
663(defun jka-compr-load (file &optional noerror nomessage nosuffix)
664 "Documented as original."
665
666 (let* ((local-copy (jka-compr-file-local-copy file))
667 (load-file (or local-copy file)))
668
669 (unwind-protect
670
8fb1a583
RS
671 (let (inhibit-file-name-operation
672 inhibit-file-name-handlers)
acd622cc
RS
673 (or nomessage
674 (message "Loading %s..." file))
675
9b37d8a9
RS
676 (let ((load-force-doc-strings t))
677 (load load-file noerror t t))
acd622cc
RS
678
679 (or nomessage
680 (message "Loading %s...done." file)))
681
acd622cc
RS
682 (jka-compr-delete-temp-file local-copy))
683
684 t))
3068998d
RS
685
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)))
8fb1a583
RS
692\f
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)
3068998d
RS
697(put 'byte-compiler-base-file-name 'jka-compr
698 'jka-compr-byte-compiler-base-file-name)
acd622cc 699
9fdf055b
KH
700(defvar jka-compr-inhibit nil
701 "Non-nil means inhibit automatic uncompression temporarily.
702Lisp programs can bind this to t to do that.
703It is not recommended to set this variable permanently to anything but nil.")
704
acd622cc 705(defun jka-compr-handler (operation &rest args)
8fb1a583
RS
706 (save-match-data
707 (let ((jka-op (get operation 'jka-compr)))
9fdf055b 708 (if (and jka-op (not jka-compr-inhibit))
8fb1a583
RS
709 (apply jka-op args)
710 (jka-compr-run-real-handler operation args)))))
acd622cc 711
99bee6a4
RS
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)))
723
9699776c
DL
724;;;###autoload
725(defcustom auto-compression-mode nil
726 "Toggle automatic file compression and uncompression.
727Setting this variable directly does not take effect;
728use 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)
736
b6dca218 737;;;###autoload(defun auto-compression-mode (&optional arg)
e5220563
RS
738;;;###autoload "\
739;;;###autoloadToggle automatic file compression and uncompression.
b6dca218
RS
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)."
e5220563 742;;;###autoload (interactive "P")
b6dca218
RS
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)))
e5220563 748;;;###autoload (toggle-auto-compression arg t))
b6dca218 749
e5220563 750(defun toggle-auto-compression (&optional arg message)
61793fbf 751 "Toggle automatic file compression and uncompression.
acd622cc 752With prefix argument ARG, turn auto compression on if positive, else off.
e5220563
RS
753Returns the new status of auto compression (non-nil means on).
754If the argument MESSAGE is non-nil, it means to print a message
755saying whether the mode is now on or off."
756 (interactive "P\np")
acd622cc
RS
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))))))
761
762 (cond
763 ((and flag installed) t) ; already installed
764
765 ((and (not flag) (not installed)) nil) ; already not installed
766
767 (flag
768 (jka-compr-install))
769
770 (t
771 (jka-compr-uninstall)))
772
773
e5220563 774 (and message
acd622cc
RS
775 (if flag
776 (message "Automatic file (de)compression is now ON.")
777 (message "Automatic file (de)compression is now OFF.")))
778
779 flag))
acd622cc
RS
780
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 "\\)"))
789
790
791(defun jka-compr-install ()
792 "Install jka-compr.
919a07bb
RS
793This adds entries to `file-name-handler-alist' and `auto-mode-alist'
794and `inhibit-first-line-modes-suffixes'."
acd622cc
RS
795
796 (setq jka-compr-file-name-handler-entry
797 (cons (jka-compr-build-file-regexp) 'jka-compr-handler))
798
799 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry
800 file-name-handler-alist))
801
4eec33ae
RS
802 (setq jka-compr-added-to-file-coding-system-alist nil)
803
acd622cc
RS
804 (mapcar
805 (function (lambda (x)
4eec33ae
RS
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)))
813
469f4e8c
RS
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)))))
74b2c737
RS
829 jka-compr-compression-info-list)
830 (setq auto-mode-alist
831 (append auto-mode-alist jka-compr-mode-alist-additions)))
acd622cc
RS
832
833
834(defun jka-compr-uninstall ()
835 "Uninstall jka-compr.
99bee6a4 836This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
919a07bb
RS
837and `inhibit-first-line-modes-suffixes' that were added
838by `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)
acd622cc
RS
848
849 (let* ((fnha (cons nil file-name-handler-alist))
850 (last fnha))
851
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))))
856
857 (setq file-name-handler-alist (cdr fnha)))
858
859 (let* ((ama (cons nil auto-mode-alist))
860 (last ama)
861 entry)
862
863 (while (cdr last)
864 (setq entry (car (cdr last)))
74b2c737
RS
865 (if (or (member entry jka-compr-mode-alist-additions)
866 (and (consp (cdr entry))
867 (eq (nth 2 entry) 'jka-compr)))
acd622cc
RS
868 (setcdr last (cdr (cdr last)))
869 (setq last (cdr last))))
870
4eec33ae
RS
871 (setq auto-mode-alist (cdr ama)))
872
873 (let* ((ama (cons nil file-coding-system-alist))
874 (last ama)
875 entry)
876
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))))
882
883 (setq file-coding-system-alist (cdr ama))))
acd622cc
RS
884
885
886(defun jka-compr-installed-p ()
887 "Return non-nil if jka-compr is installed.
99bee6a4 888The return value is the entry in `file-name-handler-alist' for jka-compr."
acd622cc
RS
889
890 (let ((fnha file-name-handler-alist)
891 (installed nil))
892
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)))
897
898 installed))
899
900
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))
906
907(jka-compr-install)
908
909
910(provide 'jka-compr)
911
912;; jka-compr.el ends here.