* align.el:
[bpt/emacs.git] / lisp / jka-compr.el
1 ;;; jka-compr.el --- reading/writing/loading compressed files
2
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: jka@ece.cmu.edu (Jay K. Adams)
7 ;; Maintainer: FSF
8 ;; Keywords: data
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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.
34
35
36 ;; INSTRUCTIONS:
37 ;;
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).
42 ;;
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.
47 ;;
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.
52
53
54 ;; APPLICATION NOTES:
55 ;;
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.
66 ;;
67
68
69 ;; ACKNOWLEDGMENTS
70 ;;
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.
74 ;;
75 ;; Jean-loup Gailly
76 ;; David Hughes
77 ;; Richard Pieri
78 ;; Daniel Quinlan
79 ;; Chris P. Ross
80 ;; Rick Sladkey
81 ;;
82 ;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for
83 ;; Version 18 of Emacs.
84 ;;
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.
89 ;;
90
91 ;;; Code:
92
93 (require 'jka-cmpr-hook)
94
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)
100
101 (defvar jka-compr-use-shell
102 (not (memq system-type '(ms-dos windows-nt))))
103
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)
110 \f
111
112 (put 'compression-error 'error-conditions '(compression-error file-error error))
113
114
115 (defvar jka-compr-acceptable-retval-list '(0 2 141))
116
117
118 (defun jka-compr-error (prog args infile message &optional errfile)
119
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))
127
128 (and errfile
129 (insert-file-contents errfile)))
130 (display-buffer errbuf))
131
132 (signal 'compression-error
133 (list "Opening input file" (format "error %s" message) infile)))
134
135
136 (defcustom jka-compr-dd-program "/bin/dd"
137 "How to invoke `dd'."
138 :type 'string
139 :group 'jka-compr)
140
141
142 (defvar jka-compr-dd-blocksize 256)
143
144
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 (format
170 "%s %s 2> %s | %s bs=%d skip=%d %s 2> %s"
171 prog
172 (mapconcat 'identity args " ")
173 err-file
174 jka-compr-dd-program
175 jka-compr-dd-blocksize
176 skip
177 ;; dd seems to be unreliable about
178 ;; providing the last block. So, always
179 ;; read one more than you think you need.
180 (if count (format "count=%d" (1+ count)) "")
181 null-device))
182 jka-compr-acceptable-retval-list)
183 (jka-compr-error prog args infile message err-file))
184 (jka-compr-delete-temp-file err-file)))
185 ;; Run the uncompression program directly.
186 ;; We get the whole file and must delete what we don't want.
187 (jka-compr-call-process prog message infile t nil args))
188
189 ;; Delete the stuff after what we want, if there is any.
190 (and
191 len
192 (< (+ start prefix len) (point))
193 (delete-region (+ start prefix len) (point)))
194
195 ;; Delete the stuff before what we want.
196 (delete-region start (+ start prefix))))
197
198
199 (defun jka-compr-call-process (prog message infile output temp args)
200 ;; call-process barfs if default-directory is inaccessible.
201 (let ((default-directory
202 (if (and default-directory
203 (file-accessible-directory-p default-directory))
204 default-directory
205 (file-name-directory infile))))
206 (if jka-compr-use-shell
207 (let ((err-file (jka-compr-make-temp-name))
208 (coding-system-for-read (or coding-system-for-read 'undecided))
209 (coding-system-for-write 'no-conversion))
210 (unwind-protect
211 (or (memq
212 (call-process jka-compr-shell infile
213 (if (stringp output) nil output)
214 nil
215 "-c"
216 (format "%s %s 2> %s %s"
217 prog
218 (mapconcat 'identity args " ")
219 err-file
220 (if (stringp output)
221 (concat "> " output)
222 "")))
223 jka-compr-acceptable-retval-list)
224 (jka-compr-error prog args infile message err-file))
225 (jka-compr-delete-temp-file err-file)))
226 (or (eq 0
227 (apply 'call-process
228 prog infile (if (stringp output) temp output)
229 nil args))
230 (jka-compr-error prog args infile message))
231 (and (stringp output)
232 (with-current-buffer temp
233 (write-region (point-min) (point-max) output)
234 (erase-buffer))))))
235
236
237 ;; Support for temp files. Much of this was inspired if not lifted
238 ;; from ange-ftp.
239
240 (defcustom jka-compr-temp-name-template
241 (expand-file-name "jka-com" temporary-file-directory)
242 "Prefix added to all temp files created by jka-compr.
243 There should be no more than seven characters after the final `/'."
244 :type 'string
245 :group 'jka-compr)
246
247 (defun jka-compr-make-temp-name (&optional local-copy)
248 "This routine will return the name of a new file."
249 (make-temp-file jka-compr-temp-name-template))
250
251 (defalias 'jka-compr-delete-temp-file 'delete-file)
252
253
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))))
259
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))
267
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))
291
292 (or compress-program
293 (error "No compression program defined"))
294
295 (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*"))
296 (with-current-buffer temp-buffer
297 (widen) (erase-buffer))
298
299 (if (and append
300 (not can-append)
301 (file-exists-p filename))
302
303 (let* ((local-copy (file-local-copy filename))
304 (local-file (or local-copy filename)))
305
306 (setq temp-file local-file))
307
308 (setq temp-file (jka-compr-make-temp-name)))
309
310 (and
311 compress-message
312 (message "%s %s..." compress-message base-name))
313
314 (jka-compr-run-real-handler 'write-region
315 (list start end temp-file t 'dont))
316 ;; save value used by the real write-region
317 (setq coding-system-used last-coding-system-used)
318
319 ;; Here we must read the output of compress program as is
320 ;; without any code conversion.
321 (let ((coding-system-for-read 'no-conversion))
322 (jka-compr-call-process compress-program
323 (concat compress-message
324 " " base-name)
325 temp-file
326 temp-buffer
327 nil
328 compress-args))
329
330 (with-current-buffer temp-buffer
331 (let ((coding-system-for-write 'no-conversion))
332 (if (memq system-type '(ms-dos windows-nt))
333 (setq buffer-file-type t) )
334 (jka-compr-run-real-handler 'write-region
335 (list (point-min) (point-max)
336 filename
337 (and append can-append) 'dont))
338 (erase-buffer)) )
339
340 (jka-compr-delete-temp-file temp-file)
341
342 (and
343 compress-message
344 (message "%s %s...done" compress-message base-name))
345
346 (cond
347 ((eq visit t)
348 (setq buffer-file-name filename)
349 (setq jka-compr-really-do-compress t)
350 (set-visited-file-modtime))
351 ((stringp visit)
352 (setq buffer-file-name visit)
353 (let ((buffer-file-name filename))
354 (set-visited-file-modtime))))
355
356 (and (or (eq visit t)
357 (eq visit nil)
358 (stringp visit))
359 (message "Wrote %s" visit-file))
360
361 ;; ensure `last-coding-system-used' has an appropriate value
362 (setq last-coding-system-used coding-system-used)
363
364 nil)
365
366 (jka-compr-run-real-handler 'write-region
367 (list start end filename append visit)))))
368
369
370 (defun jka-compr-insert-file-contents (file &optional visit beg end replace)
371 (barf-if-buffer-read-only)
372
373 (and (or beg end)
374 visit
375 (error "Attempt to visit less than an entire file"))
376
377 (let* ((filename (expand-file-name file))
378 (info (jka-compr-get-compression-info filename)))
379
380 (if (not info)
381
382 (jka-compr-run-real-handler 'insert-file-contents
383 (list file visit beg end replace))
384
385 (let ((uncompress-message (jka-compr-info-uncompress-message info))
386 (uncompress-program (jka-compr-info-uncompress-program info))
387 (uncompress-args (jka-compr-info-uncompress-args info))
388 (base-name (file-name-nondirectory filename))
389 (notfound nil)
390 (local-copy
391 (jka-compr-run-real-handler 'file-local-copy (list filename)))
392 local-file
393 size start)
394
395 (setq local-file (or local-copy filename))
396
397 (and
398 visit
399 (setq buffer-file-name filename))
400
401 (unwind-protect ; to make sure local-copy gets deleted
402
403 (progn
404
405 (and
406 uncompress-message
407 (message "%s %s..." uncompress-message base-name))
408
409 (condition-case error-code
410
411 (let ((coding-system-for-read 'no-conversion))
412 (if replace
413 (goto-char (point-min)))
414 (setq start (point))
415 (if (or beg end)
416 (jka-compr-partial-uncompress uncompress-program
417 (concat uncompress-message
418 " " base-name)
419 uncompress-args
420 local-file
421 (or beg 0)
422 (if (and beg end)
423 (- end beg)
424 end))
425 ;; If visiting, bind off buffer-file-name so that
426 ;; file-locking will not ask whether we should
427 ;; really edit the buffer.
428 (let ((buffer-file-name
429 (if visit nil buffer-file-name)))
430 (jka-compr-call-process uncompress-program
431 (concat uncompress-message
432 " " base-name)
433 local-file
434 t
435 nil
436 uncompress-args)))
437 (setq size (- (point) start))
438 (if replace
439 (delete-region (point) (point-max)))
440 (goto-char start))
441 (error
442 ;; If the file we wanted to uncompress does not exist,
443 ;; handle that according to VISIT as `insert-file-contents'
444 ;; would, maybe signaling the same error it normally would.
445 (if (and (eq (car error-code) 'file-error)
446 (eq (nth 3 error-code) local-file))
447 (if visit
448 (setq notfound error-code)
449 (signal 'file-error
450 (cons "Opening input file"
451 (nthcdr 2 error-code))))
452 ;; If the uncompression program can't be found,
453 ;; signal that as a non-file error
454 ;; so that find-file-noselect-1 won't handle it.
455 (if (and (eq (car error-code) 'file-error)
456 (equal (cadr error-code) "Searching for program"))
457 (error "Uncompression program `%s' not found"
458 (nth 3 error-code)))
459 (signal (car error-code) (cdr error-code))))))
460
461 (and
462 local-copy
463 (file-exists-p local-copy)
464 (delete-file local-copy)))
465
466 (unless notfound
467 (decode-coding-inserted-region
468 (point) (+ (point) size)
469 (jka-compr-byte-compiler-base-file-name file)
470 visit beg end replace))
471
472 (and
473 visit
474 (progn
475 (unlock-buffer)
476 (setq buffer-file-name filename)
477 (setq jka-compr-really-do-compress t)
478 (set-visited-file-modtime)))
479
480 (and
481 uncompress-message
482 (message "%s %s...done" uncompress-message base-name))
483
484 (and
485 visit
486 notfound
487 (signal 'file-error
488 (cons "Opening input file" (nth 2 notfound))))
489
490 ;; This is done in insert-file-contents after we return.
491 ;; That is a little weird, but better to go along with it now
492 ;; than to change it now.
493
494 ;; ;; Run the functions that insert-file-contents would.
495 ;; (let ((p after-insert-file-functions)
496 ;; (insval size))
497 ;; (while p
498 ;; (setq insval (funcall (car p) size))
499 ;; (if insval
500 ;; (progn
501 ;; (or (integerp insval)
502 ;; (signal 'wrong-type-argument
503 ;; (list 'integerp insval)))
504 ;; (setq size insval)))
505 ;; (setq p (cdr p))))
506
507 (or (jka-compr-info-compress-program info)
508 (message "You can't save this buffer because compression program is not defined"))
509
510 (list filename size)))))
511
512
513 (defun jka-compr-file-local-copy (file)
514 (let* ((filename (expand-file-name file))
515 (info (jka-compr-get-compression-info filename)))
516
517 (if info
518
519 (let ((uncompress-message (jka-compr-info-uncompress-message info))
520 (uncompress-program (jka-compr-info-uncompress-program info))
521 (uncompress-args (jka-compr-info-uncompress-args info))
522 (base-name (file-name-nondirectory filename))
523 (local-copy
524 (jka-compr-run-real-handler 'file-local-copy (list filename)))
525 (temp-file (jka-compr-make-temp-name t))
526 (temp-buffer (get-buffer-create " *jka-compr-flc-temp*"))
527 local-file)
528
529 (setq local-file (or local-copy filename))
530
531 (unwind-protect
532
533 (with-current-buffer temp-buffer
534
535 (and
536 uncompress-message
537 (message "%s %s..." uncompress-message base-name))
538
539 ;; Here we must read the output of uncompress program
540 ;; and write it to TEMP-FILE without any code
541 ;; conversion. An appropriate code conversion (if
542 ;; necessary) is done by the later I/O operation
543 ;; (e.g. load).
544 (let ((coding-system-for-read 'no-conversion)
545 (coding-system-for-write 'no-conversion))
546
547 (jka-compr-call-process uncompress-program
548 (concat uncompress-message
549 " " base-name)
550 local-file
551 t
552 nil
553 uncompress-args)
554
555 (and
556 uncompress-message
557 (message "%s %s...done" uncompress-message base-name))
558
559 (write-region
560 (point-min) (point-max) temp-file nil 'dont)))
561
562 (and
563 local-copy
564 (file-exists-p local-copy)
565 (delete-file local-copy))
566
567 (kill-buffer temp-buffer))
568
569 temp-file)
570
571 (jka-compr-run-real-handler 'file-local-copy (list filename)))))
572
573
574 ;; Support for loading compressed files.
575 (defun jka-compr-load (file &optional noerror nomessage nosuffix)
576 "Documented as original."
577
578 (let* ((local-copy (jka-compr-file-local-copy file))
579 (load-file (or local-copy file)))
580
581 (unwind-protect
582
583 (let (inhibit-file-name-operation
584 inhibit-file-name-handlers)
585 (or nomessage
586 (message "Loading %s..." file))
587
588 (let ((load-force-doc-strings t))
589 (load load-file noerror t t))
590 (or nomessage
591 (message "Loading %s...done." file))
592 ;; Fix up the load history to point at the right library.
593 (let ((l (assoc load-file load-history)))
594 ;; Remove .gz and .elc?.
595 (while (file-name-extension file)
596 (setq file (file-name-sans-extension file)))
597 (setcar l file)))
598
599 (jka-compr-delete-temp-file local-copy))
600
601 t))
602
603 (defun jka-compr-byte-compiler-base-file-name (file)
604 (let ((info (jka-compr-get-compression-info file)))
605 (if (and info (jka-compr-info-strip-extension info))
606 (save-match-data
607 (substring file 0 (string-match (jka-compr-info-regexp info) file)))
608 file)))
609 \f
610 (put 'write-region 'jka-compr 'jka-compr-write-region)
611 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents)
612 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy)
613 (put 'load 'jka-compr 'jka-compr-load)
614 (put 'byte-compiler-base-file-name 'jka-compr
615 'jka-compr-byte-compiler-base-file-name)
616
617 ;;;###autoload
618 (defvar jka-compr-inhibit nil
619 "Non-nil means inhibit automatic uncompression temporarily.
620 Lisp programs can bind this to t to do that.
621 It is not recommended to set this variable permanently to anything but nil.")
622
623 ;;;###autoload
624 (defun jka-compr-handler (operation &rest args)
625 (save-match-data
626 (let ((jka-op (get operation 'jka-compr)))
627 (if (and jka-op (not jka-compr-inhibit))
628 (apply jka-op args)
629 (jka-compr-run-real-handler operation args)))))
630
631 ;; If we are given an operation that we don't handle,
632 ;; call the Emacs primitive for that operation,
633 ;; and manipulate the inhibit variables
634 ;; to prevent the primitive from calling our handler again.
635 (defun jka-compr-run-real-handler (operation args)
636 (let ((inhibit-file-name-handlers
637 (cons 'jka-compr-handler
638 (and (eq inhibit-file-name-operation operation)
639 inhibit-file-name-handlers)))
640 (inhibit-file-name-operation operation))
641 (apply operation args)))
642
643 ;;;###autoload
644 (defun jka-compr-uninstall ()
645 "Uninstall jka-compr.
646 This removes the entries in `file-name-handler-alist' and `auto-mode-alist'
647 and `inhibit-first-line-modes-suffixes' that were added
648 by `jka-compr-installed'."
649 ;; Delete from inhibit-first-line-modes-suffixes
650 ;; what jka-compr-install added.
651 (mapc
652 (function (lambda (x)
653 (and (jka-compr-info-strip-extension x)
654 (setq inhibit-first-line-modes-suffixes
655 (delete (jka-compr-info-regexp x)
656 inhibit-first-line-modes-suffixes)))))
657 jka-compr-compression-info-list--internal)
658
659 (let* ((fnha (cons nil file-name-handler-alist))
660 (last fnha))
661
662 (while (cdr last)
663 (if (eq (cdr (car (cdr last))) 'jka-compr-handler)
664 (setcdr last (cdr (cdr last)))
665 (setq last (cdr last))))
666
667 (setq file-name-handler-alist (cdr fnha)))
668
669 (let* ((ama (cons nil auto-mode-alist))
670 (last ama)
671 entry)
672
673 (while (cdr last)
674 (setq entry (car (cdr last)))
675 (if (or (member entry jka-compr-mode-alist-additions--internal)
676 (and (consp (cdr entry))
677 (eq (nth 2 entry) 'jka-compr)))
678 (setcdr last (cdr (cdr last)))
679 (setq last (cdr last))))
680
681 (setq auto-mode-alist (cdr ama)))
682
683 (while jka-compr-added-to-file-coding-system-alist
684 (setq file-coding-system-alist
685 (delq (car (member (pop jka-compr-added-to-file-coding-system-alist)
686 file-coding-system-alist))
687 file-coding-system-alist)))
688
689 ;; Remove the suffixes that were added by jka-compr.
690 (dolist (suff jka-compr-load-suffixes--internal)
691 (setq load-file-rep-suffixes (delete suff load-file-rep-suffixes)))
692
693 (setq jka-compr-compression-info-list--internal nil
694 jka-compr-mode-alist-additions--internal nil
695 jka-compr-load-suffixes--internal nil))
696
697 (provide 'jka-compr)
698
699 ;; arch-tag: 3f15b630-e9a7-46c4-a22a-94afdde86ebc
700 ;;; jka-compr.el ends here