Merge changes made in Gnus trunk
[emacs.git] / lisp / jka-compr.el
blob8a8d7cdbb528df25ad5ad933368bbc1a7c8ff2b4
1 ;;; jka-compr.el --- reading/writing/loading compressed files
3 ;; Copyright (C) 1993-1995, 1997, 1999-2012 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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package implements low-level support for reading, writing,
27 ;; and loading compressed files. It hooks into the low-level file
28 ;; I/O functions (including write-region and insert-file-contents) so
29 ;; that they automatically compress or uncompress a file if the file
30 ;; appears to need it (based on the extension of the file name).
31 ;; Packages like Rmail, VM, GNUS, and Info should be able to work
32 ;; with compressed files without modification.
35 ;; INSTRUCTIONS:
37 ;; To use jka-compr, invoke the command `auto-compression-mode' (which
38 ;; see), or customize the variable of the same name. Its operation
39 ;; should be transparent to the user (except for messages appearing when
40 ;; a file is being compressed or uncompressed).
42 ;; The variable, jka-compr-compression-info-list can be used to
43 ;; customize jka-compr to work with other compression programs.
44 ;; The default value of this variable allows jka-compr to work with
45 ;; Unix compress and gzip.
47 ;; If you don't want messages about compressing and decompressing
48 ;; to show up in the echo area, you can set the compress-msg and
49 ;; decompress-msg fields of the jka-compr-compression-info-list to
50 ;; nil.
53 ;; APPLICATION NOTES:
55 ;; crypt++
56 ;; jka-compr can coexist with crypt++ if you take all the decompression
57 ;; entries out of the crypt-encoding-list. Clearly problems will arise if
58 ;; you have two programs trying to compress/decompress files. jka-compr
59 ;; will not "work with" crypt++ in the following sense: you won't be able to
60 ;; decode encrypted compressed files--that is, files that have been
61 ;; compressed then encrypted (in that order). Theoretically, crypt++ and
62 ;; jka-compr could properly handle a file that has been encrypted then
63 ;; compressed, but there is little point in trying to compress an encrypted
64 ;; file.
68 ;; ACKNOWLEDGMENTS
70 ;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people
71 ;; have made helpful suggestions, reported bugs, and even fixed bugs in
72 ;; jka-compr. I recall the following people as being particularly helpful.
74 ;; Jean-loup Gailly
75 ;; David Hughes
76 ;; Richard Pieri
77 ;; Daniel Quinlan
78 ;; Chris P. Ross
79 ;; Rick Sladkey
81 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
82 ;; Version 18 of Emacs.
84 ;; After I had made progress on the original jka-compr for V18, I learned of a
85 ;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly
86 ;; what I was trying to do. I looked over the jam-zcat source code and
87 ;; probably got some ideas from it.
90 ;;; Code:
92 (require 'jka-cmpr-hook)
94 (defcustom jka-compr-shell "sh"
95 "Shell to be used for calling compression programs.
96 NOTE: Not used in MS-DOS and Windows systems."
97 :type 'string
98 :group 'jka-compr)
100 (defvar jka-compr-use-shell
101 (not (memq system-type '(ms-dos windows-nt))))
103 (defvar jka-compr-really-do-compress nil
104 "Non-nil in a buffer whose visited file was uncompressed on visiting it.
105 This means compress the data on writing the file, even if the
106 data appears to be compressed already.")
107 (make-variable-buffer-local 'jka-compr-really-do-compress)
108 (put 'jka-compr-really-do-compress 'permanent-local t)
111 (put 'compression-error 'error-conditions '(compression-error file-error error))
114 (defvar jka-compr-acceptable-retval-list '(0 2 141))
117 (defun jka-compr-error (prog args infile message &optional errfile)
119 (let ((errbuf (get-buffer-create " *jka-compr-error*")))
120 (with-current-buffer errbuf
121 (widen) (erase-buffer)
122 (insert (format "Error while executing \"%s %s < %s\"\n\n"
123 prog
124 (mapconcat 'identity args " ")
125 infile))
127 (and errfile
128 (insert-file-contents errfile)))
129 (display-buffer errbuf))
131 (signal 'compression-error
132 (list "Opening input file" (format "error %s" message) infile)))
135 (defcustom jka-compr-dd-program "/bin/dd"
136 "How to invoke `dd'."
137 :type 'string
138 :group 'jka-compr)
141 (defvar jka-compr-dd-blocksize 256)
144 (defun jka-compr-partial-uncompress (prog message args infile beg len)
145 "Call program PROG with ARGS args taking input from INFILE.
146 Fourth and fifth args, BEG and LEN, specify which part of the output
147 to keep: LEN chars starting BEG chars from the beginning."
148 (let ((start (point))
149 (prefix beg))
150 (if (and jka-compr-use-shell jka-compr-dd-program)
151 ;; Put the uncompression output through dd
152 ;; to discard the part we don't want.
153 (let ((skip (/ beg jka-compr-dd-blocksize))
154 (err-file (jka-compr-make-temp-name))
155 ;; call-process barfs if default-directory is inaccessible.
156 (default-directory
157 (if (and default-directory
158 (file-accessible-directory-p default-directory))
159 default-directory
160 (file-name-directory infile)))
161 count)
162 ;; Update PREFIX based on the text that we won't read in.
163 (setq prefix (- beg (* skip jka-compr-dd-blocksize))
164 count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize))))
165 (unwind-protect
166 (or (memq (call-process
167 jka-compr-shell infile t nil "-c"
168 ;; Windows shells need the program file name
169 ;; after the pipe symbol be quoted if they use
170 ;; forward slashes as directory separators.
171 (format
172 "%s %s 2> %s | \"%s\" bs=%d skip=%d %s 2> %s"
173 prog
174 (mapconcat 'identity args " ")
175 err-file
176 jka-compr-dd-program
177 jka-compr-dd-blocksize
178 skip
179 ;; dd seems to be unreliable about
180 ;; providing the last block. So, always
181 ;; read one more than you think you need.
182 (if count (format "count=%d" (1+ count)) "")
183 null-device))
184 jka-compr-acceptable-retval-list)
185 (jka-compr-error prog args infile message err-file))
186 (delete-file err-file)))
188 ;; Run the uncompression program directly.
189 ;; We get the whole file and must delete what we don't want.
190 (jka-compr-call-process prog message infile t nil args))
192 ;; Delete the stuff after what we want, if there is any.
193 (and
195 (< (+ start prefix len) (point))
196 (delete-region (+ start prefix len) (point)))
198 ;; Delete the stuff before what we want.
199 (delete-region start (+ start prefix))))
202 (defun jka-compr-call-process (prog message infile output temp args)
203 ;; call-process barfs if default-directory is inaccessible.
204 (let ((default-directory
205 (if (and default-directory
206 (file-accessible-directory-p default-directory))
207 default-directory
208 (file-name-directory infile))))
209 (if jka-compr-use-shell
210 (let ((err-file (jka-compr-make-temp-name))
211 (coding-system-for-read (or coding-system-for-read 'undecided))
212 (coding-system-for-write 'no-conversion))
213 (unwind-protect
214 (or (memq
215 (call-process jka-compr-shell infile
216 (if (stringp output) nil output)
218 "-c"
219 (format "%s %s 2> %s %s"
220 prog
221 (mapconcat 'identity args " ")
222 err-file
223 (if (stringp output)
224 (concat "> " output)
225 "")))
226 jka-compr-acceptable-retval-list)
227 (jka-compr-error prog args infile message err-file))
228 (delete-file err-file)))
229 (or (eq 0
230 (apply 'call-process
231 prog infile (if (stringp output) temp output)
232 nil args))
233 (jka-compr-error prog args infile message))
234 (and (stringp output)
235 (with-current-buffer temp
236 (write-region (point-min) (point-max) output)
237 (erase-buffer))))))
240 ;; Support for temp files. Much of this was inspired if not lifted
241 ;; from ange-ftp.
243 (defcustom jka-compr-temp-name-template
244 (expand-file-name "jka-com" temporary-file-directory)
245 "Prefix added to all temp files created by jka-compr.
246 There should be no more than seven characters after the final `/'."
247 :type 'string
248 :group 'jka-compr)
250 (defun jka-compr-make-temp-name (&optional _local-copy)
251 "This routine will return the name of a new file."
252 (make-temp-file jka-compr-temp-name-template))
254 (defun jka-compr-write-region (start end file &optional append visit)
255 (let* ((filename (expand-file-name file))
256 (visit-file (if (stringp visit) (expand-file-name visit) filename))
257 (info (jka-compr-get-compression-info visit-file))
258 (magic (and info (jka-compr-info-file-magic-bytes info))))
260 ;; If we uncompressed this file when visiting it,
261 ;; then recompress it when writing it
262 ;; even if the contents look compressed already.
263 (if (and jka-compr-really-do-compress
264 (or (null start)
265 (= (- end start) (buffer-size))))
266 (setq magic nil))
268 (if (and info
269 ;; If the contents to be written out
270 ;; are properly compressed already,
271 ;; don't try to compress them over again.
272 (not (and magic
273 (equal (if (stringp start)
274 (substring start 0 (min (length start)
275 (length magic)))
276 (let* ((from (or start (point-min)))
277 (to (min (or end (point-max))
278 (+ from (length magic)))))
279 (buffer-substring from to)))
280 magic))))
281 (let ((can-append (jka-compr-info-can-append info))
282 (compress-program (jka-compr-info-compress-program info))
283 (compress-message (jka-compr-info-compress-message info))
284 (compress-args (jka-compr-info-compress-args info))
285 (base-name (file-name-nondirectory visit-file))
286 temp-file temp-buffer
287 ;; we need to leave `last-coding-system-used' set to its
288 ;; value after calling write-region the first time, so
289 ;; that `basic-save-buffer' sees the right value.
290 (coding-system-used last-coding-system-used))
292 (or compress-program
293 (error "No compression program defined"))
295 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
296 (with-current-buffer temp-buffer
297 (widen) (erase-buffer))
299 (if (and append
300 (not can-append)
301 (file-exists-p filename))
303 (let* ((local-copy (file-local-copy filename))
304 (local-file (or local-copy filename)))
306 (setq temp-file local-file))
308 (setq temp-file (jka-compr-make-temp-name)))
310 (and
311 compress-message
312 jka-compr-verbose
313 (message "%s %s..." compress-message base-name))
315 (jka-compr-run-real-handler 'write-region
316 (list start end temp-file t 'dont))
317 ;; save value used by the real write-region
318 (setq coding-system-used last-coding-system-used)
320 ;; Here we must read the output of compress program as is
321 ;; without any code conversion.
322 (let ((coding-system-for-read 'no-conversion))
323 (jka-compr-call-process compress-program
324 (concat compress-message
325 " " base-name)
326 temp-file
327 temp-buffer
329 compress-args))
331 (with-current-buffer temp-buffer
332 (let ((coding-system-for-write 'no-conversion))
333 (if (memq system-type '(ms-dos windows-nt))
334 (setq buffer-file-type t) )
335 (jka-compr-run-real-handler 'write-region
336 (list (point-min) (point-max)
337 filename
338 (and append can-append) 'dont))
339 (erase-buffer)) )
341 (delete-file temp-file)
343 (and
344 compress-message
345 jka-compr-verbose
346 (message "%s %s...done" compress-message base-name))
348 (cond
349 ((eq visit t)
350 (setq buffer-file-name filename)
351 (setq jka-compr-really-do-compress t)
352 (set-visited-file-modtime))
353 ((stringp visit)
354 (setq buffer-file-name visit)
355 (let ((buffer-file-name filename))
356 (set-visited-file-modtime))))
358 (and (or (eq visit t)
359 (eq visit nil)
360 (stringp visit))
361 (message "Wrote %s" visit-file))
363 ;; ensure `last-coding-system-used' has an appropriate value
364 (setq last-coding-system-used coding-system-used)
366 nil)
368 (jka-compr-run-real-handler 'write-region
369 (list start end filename append visit)))))
372 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
373 (barf-if-buffer-read-only)
375 (and (or beg end)
376 visit
377 (error "Attempt to visit less than an entire file"))
379 (let* ((filename (expand-file-name file))
380 (info (jka-compr-get-compression-info filename)))
382 (if (not info)
384 (jka-compr-run-real-handler 'insert-file-contents
385 (list file visit beg end replace))
387 (let ((uncompress-message (jka-compr-info-uncompress-message info))
388 (uncompress-program (jka-compr-info-uncompress-program info))
389 (uncompress-args (jka-compr-info-uncompress-args info))
390 (base-name (file-name-nondirectory filename))
391 (notfound nil)
392 (local-copy
393 (jka-compr-run-real-handler 'file-local-copy (list filename)))
394 local-file
395 size start)
397 (setq local-file (or local-copy filename))
399 (and
400 visit
401 (setq buffer-file-name filename))
403 (unwind-protect ; to make sure local-copy gets deleted
405 (progn
407 (and
408 uncompress-message
409 jka-compr-verbose
410 (message "%s %s..." uncompress-message base-name))
412 (condition-case error-code
414 (let ((coding-system-for-read 'no-conversion))
415 (if replace
416 (goto-char (point-min)))
417 (setq start (point))
418 (if (or beg end)
419 (jka-compr-partial-uncompress uncompress-program
420 (concat uncompress-message
421 " " base-name)
422 uncompress-args
423 local-file
424 (or beg 0)
425 (if (and beg end)
426 (- end beg)
427 end))
428 ;; If visiting, bind off buffer-file-name so that
429 ;; file-locking will not ask whether we should
430 ;; really edit the buffer.
431 (let ((buffer-file-name
432 (if visit nil buffer-file-name)))
433 (jka-compr-call-process uncompress-program
434 (concat uncompress-message
435 " " base-name)
436 local-file
439 uncompress-args)))
440 (setq size (- (point) start))
441 (if replace
442 (delete-region (point) (point-max)))
443 (goto-char start))
444 (error
445 ;; If the file we wanted to uncompress does not exist,
446 ;; handle that according to VISIT as `insert-file-contents'
447 ;; would, maybe signaling the same error it normally would.
448 (if (and (eq (car error-code) 'file-error)
449 (eq (nth 3 error-code) local-file))
450 (if visit
451 (setq notfound error-code)
452 (signal 'file-error
453 (cons "Opening input file"
454 (nthcdr 2 error-code))))
455 ;; If the uncompression program can't be found,
456 ;; signal that as a non-file error
457 ;; so that find-file-noselect-1 won't handle it.
458 (if (and (eq (car error-code) 'file-error)
459 (equal (cadr error-code) "Searching for program"))
460 (error "Uncompression program `%s' not found"
461 (nth 3 error-code)))
462 (signal (car error-code) (cdr error-code))))))
464 (and
465 local-copy
466 (file-exists-p local-copy)
467 (delete-file local-copy)))
469 (unless notfound
470 (decode-coding-inserted-region
471 (point) (+ (point) size)
472 (jka-compr-byte-compiler-base-file-name file)
473 visit beg end replace))
475 (and
476 visit
477 (progn
478 (unlock-buffer)
479 (setq buffer-file-name filename)
480 (setq jka-compr-really-do-compress t)
481 (set-visited-file-modtime)))
483 (and
484 uncompress-message
485 jka-compr-verbose
486 (message "%s %s...done" uncompress-message base-name))
488 (and
489 visit
490 notfound
491 (signal 'file-error
492 (cons "Opening input file" (nth 2 notfound))))
494 ;; This is done in insert-file-contents after we return.
495 ;; That is a little weird, but better to go along with it now
496 ;; than to change it now.
498 ;; ;; Run the functions that insert-file-contents would.
499 ;; (let ((p after-insert-file-functions)
500 ;; (insval size))
501 ;; (while p
502 ;; (setq insval (funcall (car p) size))
503 ;; (if insval
504 ;; (progn
505 ;; (or (integerp insval)
506 ;; (signal 'wrong-type-argument
507 ;; (list 'integerp insval)))
508 ;; (setq size insval)))
509 ;; (setq p (cdr p))))
511 (or (jka-compr-info-compress-program info)
512 (message "You can't save this buffer because compression program is not defined"))
514 (list filename size)))))
517 (defun jka-compr-file-local-copy (file)
518 (let* ((filename (expand-file-name file))
519 (info (jka-compr-get-compression-info filename)))
521 (if info
523 (let ((uncompress-message (jka-compr-info-uncompress-message info))
524 (uncompress-program (jka-compr-info-uncompress-program info))
525 (uncompress-args (jka-compr-info-uncompress-args info))
526 (base-name (file-name-nondirectory filename))
527 (local-copy
528 (jka-compr-run-real-handler 'file-local-copy (list filename)))
529 (temp-file (jka-compr-make-temp-name t))
530 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
531 local-file)
533 (setq local-file (or local-copy filename))
535 (unwind-protect
537 (with-current-buffer temp-buffer
539 (and
540 uncompress-message
541 jka-compr-verbose
542 (message "%s %s..." uncompress-message base-name))
544 ;; Here we must read the output of uncompress program
545 ;; and write it to TEMP-FILE without any code
546 ;; conversion. An appropriate code conversion (if
547 ;; necessary) is done by the later I/O operation
548 ;; (e.g. load).
549 (let ((coding-system-for-read 'no-conversion)
550 (coding-system-for-write 'no-conversion))
552 (jka-compr-call-process uncompress-program
553 (concat uncompress-message
554 " " base-name)
555 local-file
558 uncompress-args)
560 (and
561 uncompress-message
562 jka-compr-verbose
563 (message "%s %s...done" uncompress-message base-name))
565 (write-region
566 (point-min) (point-max) temp-file nil 'dont)))
568 (and
569 local-copy
570 (file-exists-p local-copy)
571 (delete-file local-copy))
573 (kill-buffer temp-buffer))
575 temp-file)
577 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
580 ;; Support for loading compressed files.
581 (defun jka-compr-load (file &optional noerror nomessage _nosuffix)
582 "Documented as original."
584 (let* ((local-copy (jka-compr-file-local-copy file))
585 (load-file (or local-copy file)))
587 (unwind-protect
589 (let (inhibit-file-name-operation
590 inhibit-file-name-handlers)
591 (or nomessage
592 (message "Loading %s..." file))
594 (let ((load-force-doc-strings t))
595 (load load-file noerror t t))
596 (or nomessage
597 (message "Loading %s...done." file))
598 ;; Fix up the load history to point at the right library.
599 (let ((l (or (assoc load-file load-history)
600 ;; On MS-Windows, if load-file is in
601 ;; temporary-file-directory, it will look like
602 ;; "c:/DOCUME~1/USER/LOCALS~1/foo", whereas
603 ;; readevalloop will record its truename in
604 ;; load-history. Therefore try truename if the
605 ;; original name is not in load-history.
606 (assoc (file-truename load-file) load-history))))
607 ;; Remove .gz and .elc?.
608 (while (file-name-extension file)
609 (setq file (file-name-sans-extension file)))
610 (setcar l file)))
612 (delete-file local-copy))
616 (defun jka-compr-byte-compiler-base-file-name (file)
617 (let ((info (jka-compr-get-compression-info file)))
618 (if (and info (jka-compr-info-strip-extension info))
619 (save-match-data
620 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
621 file)))
623 (put 'write-region 'jka-compr 'jka-compr-write-region)
624 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
625 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
626 (put 'load 'jka-compr 'jka-compr-load)
627 (put 'byte-compiler-base-file-name 'jka-compr
628 'jka-compr-byte-compiler-base-file-name)
630 ;;;###autoload
631 (defvar jka-compr-inhibit nil
632 "Non-nil means inhibit automatic uncompression temporarily.
633 Lisp programs can bind this to t to do that.
634 It is not recommended to set this variable permanently to anything but nil.")
636 ;;;###autoload
637 (defun jka-compr-handler (operation &rest args)
638 (save-match-data
639 (let ((jka-op (get operation 'jka-compr)))
640 (if (and jka-op (not jka-compr-inhibit))
641 (apply jka-op args)
642 (jka-compr-run-real-handler operation args)))))
644 ;; If we are given an operation that we don't handle,
645 ;; call the Emacs primitive for that operation,
646 ;; and manipulate the inhibit variables
647 ;; to prevent the primitive from calling our handler again.
648 (defun jka-compr-run-real-handler (operation args)
649 (let ((inhibit-file-name-handlers
650 (cons 'jka-compr-handler
651 (and (eq inhibit-file-name-operation operation)
652 inhibit-file-name-handlers)))
653 (inhibit-file-name-operation operation))
654 (apply operation args)))
656 ;;;###autoload
657 (defun jka-compr-uninstall ()
658 "Uninstall jka-compr.
659 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
660 and `inhibit-local-variables-suffixes' that were added
661 by `jka-compr-installed'."
662 ;; Delete from inhibit-local-variables-suffixes what jka-compr-install added.
663 (mapc
664 (function (lambda (x)
665 (and (jka-compr-info-strip-extension x)
666 (setq inhibit-local-variables-suffixes
667 (delete (jka-compr-info-regexp x)
668 inhibit-local-variables-suffixes)))))
669 jka-compr-compression-info-list--internal)
671 (let* ((fnha (cons nil file-name-handler-alist))
672 (last fnha))
674 (while (cdr last)
675 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
676 (setcdr last (cdr (cdr last)))
677 (setq last (cdr last))))
679 (setq file-name-handler-alist (cdr fnha)))
681 (let* ((ama (cons nil auto-mode-alist))
682 (last ama)
683 entry)
685 (while (cdr last)
686 (setq entry (car (cdr last)))
687 (if (or (member entry jka-compr-mode-alist-additions--internal)
688 (and (consp (cdr entry))
689 (eq (nth 2 entry) 'jka-compr)))
690 (setcdr last (cdr (cdr last)))
691 (setq last (cdr last))))
693 (setq auto-mode-alist (cdr ama)))
695 (while jka-compr-added-to-file-coding-system-alist
696 (setq file-coding-system-alist
697 (delq (car (member (pop jka-compr-added-to-file-coding-system-alist)
698 file-coding-system-alist))
699 file-coding-system-alist)))
701 ;; Remove the suffixes that were added by jka-compr.
702 (dolist (suff jka-compr-load-suffixes--internal)
703 (setq load-file-rep-suffixes (delete suff load-file-rep-suffixes)))
705 (setq jka-compr-compression-info-list--internal nil
706 jka-compr-mode-alist-additions--internal nil
707 jka-compr-load-suffixes--internal nil))
709 (provide 'jka-compr)
711 ;;; jka-compr.el ends here