* customize.texi (Composite Types): Move alist/plist from Simple Types (Bug#7545).
[emacs.git] / lisp / jka-compr.el
blob32f3d6d1f588b04919422efb47dbe1827685d8f6
1 ;;; jka-compr.el --- reading/writing/loading compressed files
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
7 ;; Maintainer: FSF
8 ;; Keywords: data
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package implements low-level support for reading, writing,
28 ;; and loading compressed files. It hooks into the low-level file
29 ;; I/O functions (including write-region and insert-file-contents) so
30 ;; that they automatically compress or uncompress a file if the file
31 ;; appears to need it (based on the extension of the file name).
32 ;; Packages like Rmail, VM, GNUS, and Info should be able to work
33 ;; with compressed files without modification.
36 ;; INSTRUCTIONS:
38 ;; To use jka-compr, invoke the command `auto-compression-mode' (which
39 ;; see), or customize the variable of the same name. Its operation
40 ;; should be transparent to the user (except for messages appearing when
41 ;; a file is being compressed or uncompressed).
43 ;; The variable, jka-compr-compression-info-list can be used to
44 ;; customize jka-compr to work with other compression programs.
45 ;; The default value of this variable allows jka-compr to work with
46 ;; Unix compress and gzip.
48 ;; If you don't want messages about compressing and decompressing
49 ;; to show up in the echo area, you can set the compress-msg and
50 ;; decompress-msg fields of the jka-compr-compression-info-list to
51 ;; nil.
54 ;; APPLICATION NOTES:
56 ;; crypt++
57 ;; jka-compr can coexist with crypt++ if you take all the decompression
58 ;; entries out of the crypt-encoding-list. Clearly problems will arise if
59 ;; you have two programs trying to compress/decompress files. jka-compr
60 ;; will not "work with" crypt++ in the following sense: you won't be able to
61 ;; decode encrypted compressed files--that is, files that have been
62 ;; compressed then encrypted (in that order). Theoretically, crypt++ and
63 ;; jka-compr could properly handle a file that has been encrypted then
64 ;; compressed, but there is little point in trying to compress an encrypted
65 ;; file.
69 ;; ACKNOWLEDGMENTS
71 ;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people
72 ;; have made helpful suggestions, reported bugs, and even fixed bugs in
73 ;; jka-compr. I recall the following people as being particularly helpful.
75 ;; Jean-loup Gailly
76 ;; David Hughes
77 ;; Richard Pieri
78 ;; Daniel Quinlan
79 ;; Chris P. Ross
80 ;; Rick Sladkey
82 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
83 ;; Version 18 of Emacs.
85 ;; After I had made progress on the original jka-compr for V18, I learned of a
86 ;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly
87 ;; what I was trying to do. I looked over the jam-zcat source code and
88 ;; probably got some ideas from it.
91 ;;; Code:
93 (require 'jka-cmpr-hook)
95 (defcustom jka-compr-shell "sh"
96 "Shell to be used for calling compression programs.
97 NOTE: Not used in MS-DOS and Windows systems."
98 :type 'string
99 :group 'jka-compr)
101 (defvar jka-compr-use-shell
102 (not (memq system-type '(ms-dos windows-nt))))
104 (defvar jka-compr-really-do-compress nil
105 "Non-nil in a buffer whose visited file was uncompressed on visiting it.
106 This means compress the data on writing the file, even if the
107 data appears to be compressed already.")
108 (make-variable-buffer-local 'jka-compr-really-do-compress)
109 (put 'jka-compr-really-do-compress 'permanent-local t)
112 (put 'compression-error 'error-conditions '(compression-error file-error error))
115 (defvar jka-compr-acceptable-retval-list '(0 2 141))
118 (defun jka-compr-error (prog args infile message &optional errfile)
120 (let ((errbuf (get-buffer-create " *jka-compr-error*")))
121 (with-current-buffer errbuf
122 (widen) (erase-buffer)
123 (insert (format "Error while executing \"%s %s < %s\"\n\n"
124 prog
125 (mapconcat 'identity args " ")
126 infile))
128 (and errfile
129 (insert-file-contents errfile)))
130 (display-buffer errbuf))
132 (signal 'compression-error
133 (list "Opening input file" (format "error %s" message) infile)))
136 (defcustom jka-compr-dd-program "/bin/dd"
137 "How to invoke `dd'."
138 :type 'string
139 :group 'jka-compr)
142 (defvar jka-compr-dd-blocksize 256)
145 (defun jka-compr-partial-uncompress (prog message args infile beg len)
146 "Call program PROG with ARGS args taking input from INFILE.
147 Fourth and fifth args, BEG and LEN, specify which part of the output
148 to keep: LEN chars starting BEG chars from the beginning."
149 (let ((start (point))
150 (prefix beg))
151 (if (and jka-compr-use-shell jka-compr-dd-program)
152 ;; Put the uncompression output through dd
153 ;; to discard the part we don't want.
154 (let ((skip (/ beg jka-compr-dd-blocksize))
155 (err-file (jka-compr-make-temp-name))
156 ;; call-process barfs if default-directory is inaccessible.
157 (default-directory
158 (if (and default-directory
159 (file-accessible-directory-p default-directory))
160 default-directory
161 (file-name-directory infile)))
162 count)
163 ;; Update PREFIX based on the text that we won't read in.
164 (setq prefix (- beg (* skip jka-compr-dd-blocksize))
165 count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize))))
166 (unwind-protect
167 (or (memq (call-process
168 jka-compr-shell infile t nil "-c"
169 ;; Windows shells need the program file name
170 ;; after the pipe symbol be quoted if they use
171 ;; forward slashes as directory separators.
172 (format
173 "%s %s 2> %s | \"%s\" bs=%d skip=%d %s 2> %s"
174 prog
175 (mapconcat 'identity args " ")
176 err-file
177 jka-compr-dd-program
178 jka-compr-dd-blocksize
179 skip
180 ;; dd seems to be unreliable about
181 ;; providing the last block. So, always
182 ;; read one more than you think you need.
183 (if count (format "count=%d" (1+ count)) "")
184 null-device))
185 jka-compr-acceptable-retval-list)
186 (jka-compr-error prog args infile message err-file))
187 (jka-compr-delete-temp-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 (jka-compr-delete-temp-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 (defalias 'jka-compr-delete-temp-file 'delete-file)
257 (defun jka-compr-write-region (start end file &optional append visit)
258 (let* ((filename (expand-file-name file))
259 (visit-file (if (stringp visit) (expand-file-name visit) filename))
260 (info (jka-compr-get-compression-info visit-file))
261 (magic (and info (jka-compr-info-file-magic-bytes info))))
263 ;; If we uncompressed this file when visiting it,
264 ;; then recompress it when writing it
265 ;; even if the contents look compressed already.
266 (if (and jka-compr-really-do-compress
267 (or (null start)
268 (= (- end start) (buffer-size))))
269 (setq magic nil))
271 (if (and info
272 ;; If the contents to be written out
273 ;; are properly compressed already,
274 ;; don't try to compress them over again.
275 (not (and magic
276 (equal (if (stringp start)
277 (substring start 0 (min (length start)
278 (length magic)))
279 (let* ((from (or start (point-min)))
280 (to (min (or end (point-max))
281 (+ from (length magic)))))
282 (buffer-substring from to)))
283 magic))))
284 (let ((can-append (jka-compr-info-can-append info))
285 (compress-program (jka-compr-info-compress-program info))
286 (compress-message (jka-compr-info-compress-message info))
287 (compress-args (jka-compr-info-compress-args info))
288 (base-name (file-name-nondirectory visit-file))
289 temp-file temp-buffer
290 ;; we need to leave `last-coding-system-used' set to its
291 ;; value after calling write-region the first time, so
292 ;; that `basic-save-buffer' sees the right value.
293 (coding-system-used last-coding-system-used))
295 (or compress-program
296 (error "No compression program defined"))
298 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
299 (with-current-buffer temp-buffer
300 (widen) (erase-buffer))
302 (if (and append
303 (not can-append)
304 (file-exists-p filename))
306 (let* ((local-copy (file-local-copy filename))
307 (local-file (or local-copy filename)))
309 (setq temp-file local-file))
311 (setq temp-file (jka-compr-make-temp-name)))
313 (and
314 compress-message
315 (message "%s %s..." compress-message base-name))
317 (jka-compr-run-real-handler 'write-region
318 (list start end temp-file t 'dont))
319 ;; save value used by the real write-region
320 (setq coding-system-used last-coding-system-used)
322 ;; Here we must read the output of compress program as is
323 ;; without any code conversion.
324 (let ((coding-system-for-read 'no-conversion))
325 (jka-compr-call-process compress-program
326 (concat compress-message
327 " " base-name)
328 temp-file
329 temp-buffer
331 compress-args))
333 (with-current-buffer temp-buffer
334 (let ((coding-system-for-write 'no-conversion))
335 (if (memq system-type '(ms-dos windows-nt))
336 (setq buffer-file-type t) )
337 (jka-compr-run-real-handler 'write-region
338 (list (point-min) (point-max)
339 filename
340 (and append can-append) 'dont))
341 (erase-buffer)) )
343 (jka-compr-delete-temp-file temp-file)
345 (and
346 compress-message
347 (message "%s %s...done" compress-message base-name))
349 (cond
350 ((eq visit t)
351 (setq buffer-file-name filename)
352 (setq jka-compr-really-do-compress t)
353 (set-visited-file-modtime))
354 ((stringp visit)
355 (setq buffer-file-name visit)
356 (let ((buffer-file-name filename))
357 (set-visited-file-modtime))))
359 (and (or (eq visit t)
360 (eq visit nil)
361 (stringp visit))
362 (message "Wrote %s" visit-file))
364 ;; ensure `last-coding-system-used' has an appropriate value
365 (setq last-coding-system-used coding-system-used)
367 nil)
369 (jka-compr-run-real-handler 'write-region
370 (list start end filename append visit)))))
373 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
374 (barf-if-buffer-read-only)
376 (and (or beg end)
377 visit
378 (error "Attempt to visit less than an entire file"))
380 (let* ((filename (expand-file-name file))
381 (info (jka-compr-get-compression-info filename)))
383 (if (not info)
385 (jka-compr-run-real-handler 'insert-file-contents
386 (list file visit beg end replace))
388 (let ((uncompress-message (jka-compr-info-uncompress-message info))
389 (uncompress-program (jka-compr-info-uncompress-program info))
390 (uncompress-args (jka-compr-info-uncompress-args info))
391 (base-name (file-name-nondirectory filename))
392 (notfound nil)
393 (local-copy
394 (jka-compr-run-real-handler 'file-local-copy (list filename)))
395 local-file
396 size start)
398 (setq local-file (or local-copy filename))
400 (and
401 visit
402 (setq buffer-file-name filename))
404 (unwind-protect ; to make sure local-copy gets deleted
406 (progn
408 (and
409 uncompress-message
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 (message "%s %s...done" uncompress-message base-name))
487 (and
488 visit
489 notfound
490 (signal 'file-error
491 (cons "Opening input file" (nth 2 notfound))))
493 ;; This is done in insert-file-contents after we return.
494 ;; That is a little weird, but better to go along with it now
495 ;; than to change it now.
497 ;; ;; Run the functions that insert-file-contents would.
498 ;; (let ((p after-insert-file-functions)
499 ;; (insval size))
500 ;; (while p
501 ;; (setq insval (funcall (car p) size))
502 ;; (if insval
503 ;; (progn
504 ;; (or (integerp insval)
505 ;; (signal 'wrong-type-argument
506 ;; (list 'integerp insval)))
507 ;; (setq size insval)))
508 ;; (setq p (cdr p))))
510 (or (jka-compr-info-compress-program info)
511 (message "You can't save this buffer because compression program is not defined"))
513 (list filename size)))))
516 (defun jka-compr-file-local-copy (file)
517 (let* ((filename (expand-file-name file))
518 (info (jka-compr-get-compression-info filename)))
520 (if info
522 (let ((uncompress-message (jka-compr-info-uncompress-message info))
523 (uncompress-program (jka-compr-info-uncompress-program info))
524 (uncompress-args (jka-compr-info-uncompress-args info))
525 (base-name (file-name-nondirectory filename))
526 (local-copy
527 (jka-compr-run-real-handler 'file-local-copy (list filename)))
528 (temp-file (jka-compr-make-temp-name t))
529 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
530 local-file)
532 (setq local-file (or local-copy filename))
534 (unwind-protect
536 (with-current-buffer temp-buffer
538 (and
539 uncompress-message
540 (message "%s %s..." uncompress-message base-name))
542 ;; Here we must read the output of uncompress program
543 ;; and write it to TEMP-FILE without any code
544 ;; conversion. An appropriate code conversion (if
545 ;; necessary) is done by the later I/O operation
546 ;; (e.g. load).
547 (let ((coding-system-for-read 'no-conversion)
548 (coding-system-for-write 'no-conversion))
550 (jka-compr-call-process uncompress-program
551 (concat uncompress-message
552 " " base-name)
553 local-file
556 uncompress-args)
558 (and
559 uncompress-message
560 (message "%s %s...done" uncompress-message base-name))
562 (write-region
563 (point-min) (point-max) temp-file nil 'dont)))
565 (and
566 local-copy
567 (file-exists-p local-copy)
568 (delete-file local-copy))
570 (kill-buffer temp-buffer))
572 temp-file)
574 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
577 ;; Support for loading compressed files.
578 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
579 "Documented as original."
581 (let* ((local-copy (jka-compr-file-local-copy file))
582 (load-file (or local-copy file)))
584 (unwind-protect
586 (let (inhibit-file-name-operation
587 inhibit-file-name-handlers)
588 (or nomessage
589 (message "Loading %s..." file))
591 (let ((load-force-doc-strings t))
592 (load load-file noerror t t))
593 (or nomessage
594 (message "Loading %s...done." file))
595 ;; Fix up the load history to point at the right library.
596 (let ((l (or (assoc load-file load-history)
597 ;; On MS-Windows, if load-file is in
598 ;; temporary-file-directory, it will look like
599 ;; "c:/DOCUME~1/USER/LOCALS~1/foo", whereas
600 ;; readevalloop will record its truename in
601 ;; load-history. Therefore try truename if the
602 ;; original name is not in load-history.
603 (assoc (file-truename load-file) load-history))))
604 ;; Remove .gz and .elc?.
605 (while (file-name-extension file)
606 (setq file (file-name-sans-extension file)))
607 (setcar l file)))
609 (jka-compr-delete-temp-file local-copy))
613 (defun jka-compr-byte-compiler-base-file-name (file)
614 (let ((info (jka-compr-get-compression-info file)))
615 (if (and info (jka-compr-info-strip-extension info))
616 (save-match-data
617 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
618 file)))
620 (put 'write-region 'jka-compr 'jka-compr-write-region)
621 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
622 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
623 (put 'load 'jka-compr 'jka-compr-load)
624 (put 'byte-compiler-base-file-name 'jka-compr
625 'jka-compr-byte-compiler-base-file-name)
627 ;;;###autoload
628 (defvar jka-compr-inhibit nil
629 "Non-nil means inhibit automatic uncompression temporarily.
630 Lisp programs can bind this to t to do that.
631 It is not recommended to set this variable permanently to anything but nil.")
633 ;;;###autoload
634 (defun jka-compr-handler (operation &rest args)
635 (save-match-data
636 (let ((jka-op (get operation 'jka-compr)))
637 (if (and jka-op (not jka-compr-inhibit))
638 (apply jka-op args)
639 (jka-compr-run-real-handler operation args)))))
641 ;; If we are given an operation that we don't handle,
642 ;; call the Emacs primitive for that operation,
643 ;; and manipulate the inhibit variables
644 ;; to prevent the primitive from calling our handler again.
645 (defun jka-compr-run-real-handler (operation args)
646 (let ((inhibit-file-name-handlers
647 (cons 'jka-compr-handler
648 (and (eq inhibit-file-name-operation operation)
649 inhibit-file-name-handlers)))
650 (inhibit-file-name-operation operation))
651 (apply operation args)))
653 ;;;###autoload
654 (defun jka-compr-uninstall ()
655 "Uninstall jka-compr.
656 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
657 and `inhibit-first-line-modes-suffixes' that were added
658 by `jka-compr-installed'."
659 ;; Delete from inhibit-first-line-modes-suffixes
660 ;; what jka-compr-install added.
661 (mapc
662 (function (lambda (x)
663 (and (jka-compr-info-strip-extension x)
664 (setq inhibit-first-line-modes-suffixes
665 (delete (jka-compr-info-regexp x)
666 inhibit-first-line-modes-suffixes)))))
667 jka-compr-compression-info-list--internal)
669 (let* ((fnha (cons nil file-name-handler-alist))
670 (last fnha))
672 (while (cdr last)
673 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
674 (setcdr last (cdr (cdr last)))
675 (setq last (cdr last))))
677 (setq file-name-handler-alist (cdr fnha)))
679 (let* ((ama (cons nil auto-mode-alist))
680 (last ama)
681 entry)
683 (while (cdr last)
684 (setq entry (car (cdr last)))
685 (if (or (member entry jka-compr-mode-alist-additions--internal)
686 (and (consp (cdr entry))
687 (eq (nth 2 entry) 'jka-compr)))
688 (setcdr last (cdr (cdr last)))
689 (setq last (cdr last))))
691 (setq auto-mode-alist (cdr ama)))
693 (while jka-compr-added-to-file-coding-system-alist
694 (setq file-coding-system-alist
695 (delq (car (member (pop jka-compr-added-to-file-coding-system-alist)
696 file-coding-system-alist))
697 file-coding-system-alist)))
699 ;; Remove the suffixes that were added by jka-compr.
700 (dolist (suff jka-compr-load-suffixes--internal)
701 (setq load-file-rep-suffixes (delete suff load-file-rep-suffixes)))
703 (setq jka-compr-compression-info-list--internal nil
704 jka-compr-mode-alist-additions--internal nil
705 jka-compr-load-suffixes--internal nil))
707 (provide 'jka-compr)
709 ;; arch-tag: 3f15b630-e9a7-46c4-a22a-94afdde86ebc
710 ;;; jka-compr.el ends here