Nuke arch-tags.
[emacs.git] / lisp / jka-compr.el
blobf1ad77ae8df7da43d078f75bbf5a137cc4cf1ae9
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 (delete-file err-file)))
189 ;; Run the uncompression program directly.
190 ;; We get the whole file and must delete what we don't want.
191 (jka-compr-call-process prog message infile t nil args))
193 ;; Delete the stuff after what we want, if there is any.
194 (and
196 (< (+ start prefix len) (point))
197 (delete-region (+ start prefix len) (point)))
199 ;; Delete the stuff before what we want.
200 (delete-region start (+ start prefix))))
203 (defun jka-compr-call-process (prog message infile output temp args)
204 ;; call-process barfs if default-directory is inaccessible.
205 (let ((default-directory
206 (if (and default-directory
207 (file-accessible-directory-p default-directory))
208 default-directory
209 (file-name-directory infile))))
210 (if jka-compr-use-shell
211 (let ((err-file (jka-compr-make-temp-name))
212 (coding-system-for-read (or coding-system-for-read 'undecided))
213 (coding-system-for-write 'no-conversion))
214 (unwind-protect
215 (or (memq
216 (call-process jka-compr-shell infile
217 (if (stringp output) nil output)
219 "-c"
220 (format "%s %s 2> %s %s"
221 prog
222 (mapconcat 'identity args " ")
223 err-file
224 (if (stringp output)
225 (concat "> " output)
226 "")))
227 jka-compr-acceptable-retval-list)
228 (jka-compr-error prog args infile message err-file))
229 (delete-file err-file)))
230 (or (eq 0
231 (apply 'call-process
232 prog infile (if (stringp output) temp output)
233 nil args))
234 (jka-compr-error prog args infile message))
235 (and (stringp output)
236 (with-current-buffer temp
237 (write-region (point-min) (point-max) output)
238 (erase-buffer))))))
241 ;; Support for temp files. Much of this was inspired if not lifted
242 ;; from ange-ftp.
244 (defcustom jka-compr-temp-name-template
245 (expand-file-name "jka-com" temporary-file-directory)
246 "Prefix added to all temp files created by jka-compr.
247 There should be no more than seven characters after the final `/'."
248 :type 'string
249 :group 'jka-compr)
251 (defun jka-compr-make-temp-name (&optional local-copy)
252 "This routine will return the name of a new file."
253 (make-temp-file jka-compr-temp-name-template))
255 (defun jka-compr-write-region (start end file &optional append visit)
256 (let* ((filename (expand-file-name file))
257 (visit-file (if (stringp visit) (expand-file-name visit) filename))
258 (info (jka-compr-get-compression-info visit-file))
259 (magic (and info (jka-compr-info-file-magic-bytes info))))
261 ;; If we uncompressed this file when visiting it,
262 ;; then recompress it when writing it
263 ;; even if the contents look compressed already.
264 (if (and jka-compr-really-do-compress
265 (or (null start)
266 (= (- end start) (buffer-size))))
267 (setq magic nil))
269 (if (and info
270 ;; If the contents to be written out
271 ;; are properly compressed already,
272 ;; don't try to compress them over again.
273 (not (and magic
274 (equal (if (stringp start)
275 (substring start 0 (min (length start)
276 (length magic)))
277 (let* ((from (or start (point-min)))
278 (to (min (or end (point-max))
279 (+ from (length magic)))))
280 (buffer-substring from to)))
281 magic))))
282 (let ((can-append (jka-compr-info-can-append info))
283 (compress-program (jka-compr-info-compress-program info))
284 (compress-message (jka-compr-info-compress-message info))
285 (compress-args (jka-compr-info-compress-args info))
286 (base-name (file-name-nondirectory visit-file))
287 temp-file temp-buffer
288 ;; we need to leave `last-coding-system-used' set to its
289 ;; value after calling write-region the first time, so
290 ;; that `basic-save-buffer' sees the right value.
291 (coding-system-used last-coding-system-used))
293 (or compress-program
294 (error "No compression program defined"))
296 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
297 (with-current-buffer temp-buffer
298 (widen) (erase-buffer))
300 (if (and append
301 (not can-append)
302 (file-exists-p filename))
304 (let* ((local-copy (file-local-copy filename))
305 (local-file (or local-copy filename)))
307 (setq temp-file local-file))
309 (setq temp-file (jka-compr-make-temp-name)))
311 (and
312 compress-message
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 (message "%s %s...done" compress-message base-name))
347 (cond
348 ((eq visit t)
349 (setq buffer-file-name filename)
350 (setq jka-compr-really-do-compress t)
351 (set-visited-file-modtime))
352 ((stringp visit)
353 (setq buffer-file-name visit)
354 (let ((buffer-file-name filename))
355 (set-visited-file-modtime))))
357 (and (or (eq visit t)
358 (eq visit nil)
359 (stringp visit))
360 (message "Wrote %s" visit-file))
362 ;; ensure `last-coding-system-used' has an appropriate value
363 (setq last-coding-system-used coding-system-used)
365 nil)
367 (jka-compr-run-real-handler 'write-region
368 (list start end filename append visit)))))
371 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
372 (barf-if-buffer-read-only)
374 (and (or beg end)
375 visit
376 (error "Attempt to visit less than an entire file"))
378 (let* ((filename (expand-file-name file))
379 (info (jka-compr-get-compression-info filename)))
381 (if (not info)
383 (jka-compr-run-real-handler 'insert-file-contents
384 (list file visit beg end replace))
386 (let ((uncompress-message (jka-compr-info-uncompress-message info))
387 (uncompress-program (jka-compr-info-uncompress-program info))
388 (uncompress-args (jka-compr-info-uncompress-args info))
389 (base-name (file-name-nondirectory filename))
390 (notfound nil)
391 (local-copy
392 (jka-compr-run-real-handler 'file-local-copy (list filename)))
393 local-file
394 size start)
396 (setq local-file (or local-copy filename))
398 (and
399 visit
400 (setq buffer-file-name filename))
402 (unwind-protect ; to make sure local-copy gets deleted
404 (progn
406 (and
407 uncompress-message
408 (message "%s %s..." uncompress-message base-name))
410 (condition-case error-code
412 (let ((coding-system-for-read 'no-conversion))
413 (if replace
414 (goto-char (point-min)))
415 (setq start (point))
416 (if (or beg end)
417 (jka-compr-partial-uncompress uncompress-program
418 (concat uncompress-message
419 " " base-name)
420 uncompress-args
421 local-file
422 (or beg 0)
423 (if (and beg end)
424 (- end beg)
425 end))
426 ;; If visiting, bind off buffer-file-name so that
427 ;; file-locking will not ask whether we should
428 ;; really edit the buffer.
429 (let ((buffer-file-name
430 (if visit nil buffer-file-name)))
431 (jka-compr-call-process uncompress-program
432 (concat uncompress-message
433 " " base-name)
434 local-file
437 uncompress-args)))
438 (setq size (- (point) start))
439 (if replace
440 (delete-region (point) (point-max)))
441 (goto-char start))
442 (error
443 ;; If the file we wanted to uncompress does not exist,
444 ;; handle that according to VISIT as `insert-file-contents'
445 ;; would, maybe signaling the same error it normally would.
446 (if (and (eq (car error-code) 'file-error)
447 (eq (nth 3 error-code) local-file))
448 (if visit
449 (setq notfound error-code)
450 (signal 'file-error
451 (cons "Opening input file"
452 (nthcdr 2 error-code))))
453 ;; If the uncompression program can't be found,
454 ;; signal that as a non-file error
455 ;; so that find-file-noselect-1 won't handle it.
456 (if (and (eq (car error-code) 'file-error)
457 (equal (cadr error-code) "Searching for program"))
458 (error "Uncompression program `%s' not found"
459 (nth 3 error-code)))
460 (signal (car error-code) (cdr error-code))))))
462 (and
463 local-copy
464 (file-exists-p local-copy)
465 (delete-file local-copy)))
467 (unless notfound
468 (decode-coding-inserted-region
469 (point) (+ (point) size)
470 (jka-compr-byte-compiler-base-file-name file)
471 visit beg end replace))
473 (and
474 visit
475 (progn
476 (unlock-buffer)
477 (setq buffer-file-name filename)
478 (setq jka-compr-really-do-compress t)
479 (set-visited-file-modtime)))
481 (and
482 uncompress-message
483 (message "%s %s...done" uncompress-message base-name))
485 (and
486 visit
487 notfound
488 (signal 'file-error
489 (cons "Opening input file" (nth 2 notfound))))
491 ;; This is done in insert-file-contents after we return.
492 ;; That is a little weird, but better to go along with it now
493 ;; than to change it now.
495 ;; ;; Run the functions that insert-file-contents would.
496 ;; (let ((p after-insert-file-functions)
497 ;; (insval size))
498 ;; (while p
499 ;; (setq insval (funcall (car p) size))
500 ;; (if insval
501 ;; (progn
502 ;; (or (integerp insval)
503 ;; (signal 'wrong-type-argument
504 ;; (list 'integerp insval)))
505 ;; (setq size insval)))
506 ;; (setq p (cdr p))))
508 (or (jka-compr-info-compress-program info)
509 (message "You can't save this buffer because compression program is not defined"))
511 (list filename size)))))
514 (defun jka-compr-file-local-copy (file)
515 (let* ((filename (expand-file-name file))
516 (info (jka-compr-get-compression-info filename)))
518 (if info
520 (let ((uncompress-message (jka-compr-info-uncompress-message info))
521 (uncompress-program (jka-compr-info-uncompress-program info))
522 (uncompress-args (jka-compr-info-uncompress-args info))
523 (base-name (file-name-nondirectory filename))
524 (local-copy
525 (jka-compr-run-real-handler 'file-local-copy (list filename)))
526 (temp-file (jka-compr-make-temp-name t))
527 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
528 local-file)
530 (setq local-file (or local-copy filename))
532 (unwind-protect
534 (with-current-buffer temp-buffer
536 (and
537 uncompress-message
538 (message "%s %s..." uncompress-message base-name))
540 ;; Here we must read the output of uncompress program
541 ;; and write it to TEMP-FILE without any code
542 ;; conversion. An appropriate code conversion (if
543 ;; necessary) is done by the later I/O operation
544 ;; (e.g. load).
545 (let ((coding-system-for-read 'no-conversion)
546 (coding-system-for-write 'no-conversion))
548 (jka-compr-call-process uncompress-program
549 (concat uncompress-message
550 " " base-name)
551 local-file
554 uncompress-args)
556 (and
557 uncompress-message
558 (message "%s %s...done" uncompress-message base-name))
560 (write-region
561 (point-min) (point-max) temp-file nil 'dont)))
563 (and
564 local-copy
565 (file-exists-p local-copy)
566 (delete-file local-copy))
568 (kill-buffer temp-buffer))
570 temp-file)
572 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
575 ;; Support for loading compressed files.
576 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
577 "Documented as original."
579 (let* ((local-copy (jka-compr-file-local-copy file))
580 (load-file (or local-copy file)))
582 (unwind-protect
584 (let (inhibit-file-name-operation
585 inhibit-file-name-handlers)
586 (or nomessage
587 (message "Loading %s..." file))
589 (let ((load-force-doc-strings t))
590 (load load-file noerror t t))
591 (or nomessage
592 (message "Loading %s...done." file))
593 ;; Fix up the load history to point at the right library.
594 (let ((l (or (assoc load-file load-history)
595 ;; On MS-Windows, if load-file is in
596 ;; temporary-file-directory, it will look like
597 ;; "c:/DOCUME~1/USER/LOCALS~1/foo", whereas
598 ;; readevalloop will record its truename in
599 ;; load-history. Therefore try truename if the
600 ;; original name is not in load-history.
601 (assoc (file-truename load-file) load-history))))
602 ;; Remove .gz and .elc?.
603 (while (file-name-extension file)
604 (setq file (file-name-sans-extension file)))
605 (setcar l file)))
607 (delete-file local-copy))
611 (defun jka-compr-byte-compiler-base-file-name (file)
612 (let ((info (jka-compr-get-compression-info file)))
613 (if (and info (jka-compr-info-strip-extension info))
614 (save-match-data
615 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
616 file)))
618 (put 'write-region 'jka-compr 'jka-compr-write-region)
619 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
620 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
621 (put 'load 'jka-compr 'jka-compr-load)
622 (put 'byte-compiler-base-file-name 'jka-compr
623 'jka-compr-byte-compiler-base-file-name)
625 ;;;###autoload
626 (defvar jka-compr-inhibit nil
627 "Non-nil means inhibit automatic uncompression temporarily.
628 Lisp programs can bind this to t to do that.
629 It is not recommended to set this variable permanently to anything but nil.")
631 ;;;###autoload
632 (defun jka-compr-handler (operation &rest args)
633 (save-match-data
634 (let ((jka-op (get operation 'jka-compr)))
635 (if (and jka-op (not jka-compr-inhibit))
636 (apply jka-op args)
637 (jka-compr-run-real-handler operation args)))))
639 ;; If we are given an operation that we don't handle,
640 ;; call the Emacs primitive for that operation,
641 ;; and manipulate the inhibit variables
642 ;; to prevent the primitive from calling our handler again.
643 (defun jka-compr-run-real-handler (operation args)
644 (let ((inhibit-file-name-handlers
645 (cons 'jka-compr-handler
646 (and (eq inhibit-file-name-operation operation)
647 inhibit-file-name-handlers)))
648 (inhibit-file-name-operation operation))
649 (apply operation args)))
651 ;;;###autoload
652 (defun jka-compr-uninstall ()
653 "Uninstall jka-compr.
654 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
655 and `inhibit-first-line-modes-suffixes' that were added
656 by `jka-compr-installed'."
657 ;; Delete from inhibit-first-line-modes-suffixes
658 ;; what jka-compr-install added.
659 (mapc
660 (function (lambda (x)
661 (and (jka-compr-info-strip-extension x)
662 (setq inhibit-first-line-modes-suffixes
663 (delete (jka-compr-info-regexp x)
664 inhibit-first-line-modes-suffixes)))))
665 jka-compr-compression-info-list--internal)
667 (let* ((fnha (cons nil file-name-handler-alist))
668 (last fnha))
670 (while (cdr last)
671 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
672 (setcdr last (cdr (cdr last)))
673 (setq last (cdr last))))
675 (setq file-name-handler-alist (cdr fnha)))
677 (let* ((ama (cons nil auto-mode-alist))
678 (last ama)
679 entry)
681 (while (cdr last)
682 (setq entry (car (cdr last)))
683 (if (or (member entry jka-compr-mode-alist-additions--internal)
684 (and (consp (cdr entry))
685 (eq (nth 2 entry) 'jka-compr)))
686 (setcdr last (cdr (cdr last)))
687 (setq last (cdr last))))
689 (setq auto-mode-alist (cdr ama)))
691 (while jka-compr-added-to-file-coding-system-alist
692 (setq file-coding-system-alist
693 (delq (car (member (pop jka-compr-added-to-file-coding-system-alist)
694 file-coding-system-alist))
695 file-coding-system-alist)))
697 ;; Remove the suffixes that were added by jka-compr.
698 (dolist (suff jka-compr-load-suffixes--internal)
699 (setq load-file-rep-suffixes (delete suff load-file-rep-suffixes)))
701 (setq jka-compr-compression-info-list--internal nil
702 jka-compr-mode-alist-additions--internal nil
703 jka-compr-load-suffixes--internal nil))
705 (provide 'jka-compr)
707 ;;; jka-compr.el ends here