Commit | Line | Data |
---|---|---|
be010748 RS |
1 | ;;; jka-compr.el --- reading/writing/loading compressed files |
2 | ||
bbf5eb28 | 3 | ;; Copyright (C) 1993, 1994, 1995, 1997 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. |
114 | The value of this variable only matters if you want to discard the | |
115 | stderr of a compression/decompression program (see the documentation | |
bbf5eb28 RS |
116 | for `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] | |
060c3cc9 RS |
134 | ["\\.bz2\\'" |
135 | "bzip2ing" "bzip2" ("") | |
136 | "bunzip2ing" "bzip2" ("-d") | |
6b10a521 | 137 | nil t] |
74b2c737 RS |
138 | ["\\.tgz\\'" |
139 | "zipping" "gzip" ("-c" "-q") | |
140 | "unzipping" "gzip" ("-c" "-q" "-d") | |
141 | t nil] | |
094cf604 | 142 | ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'" |
acd622cc RS |
143 | "zipping" "gzip" ("-c" "-q") |
144 | "unzipping" "gzip" ("-c" "-q" "-d") | |
145 | t t]) | |
146 | ||
147 | "List of vectors that describe available compression techniques. | |
148 | Each element, which describes a compression technique, is a vector of | |
ede9c6a8 RS |
149 | the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS |
150 | UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS | |
151 | APPEND-FLAG EXTENSION], where: | |
acd622cc RS |
152 | |
153 | regexp is a regexp that matches filenames that are | |
154 | compressed with this format | |
155 | ||
ede9c6a8 RS |
156 | compress-msg is the message to issue to the user when doing this |
157 | type of compression (nil means no message) | |
158 | ||
acd622cc RS |
159 | compress-program is a program that performs this compression |
160 | ||
161 | compress-args is a list of args to pass to the compress program | |
162 | ||
ede9c6a8 RS |
163 | uncompress-msg is the message to issue to the user when doing this |
164 | type of uncompression (nil means no message) | |
acd622cc RS |
165 | |
166 | uncompress-program is a program that performs this compression | |
167 | ||
168 | uncompress-args is a list of args to pass to the uncompress program | |
169 | ||
170 | append-flag is non-nil if this compression technique can be | |
171 | appended | |
172 | ||
173 | auto-mode flag non-nil means strip the regexp from file names | |
174 | before attempting to set the mode. | |
175 | ||
8fb1a583 | 176 | Because of the way `call-process' is defined, discarding the stderr output of |
acd622cc | 177 | a program adds the overhead of starting a shell each time the program is |
bbf5eb28 RS |
178 | invoked." |
179 | :type '(repeat (vector regexp | |
180 | (choice :tag "Compress Message" | |
181 | (string :format "%v") | |
182 | (const :tag "No Message" nil)) | |
183 | (string :tag "Compress Program") | |
184 | (repeat :tag "Compress Arguments" string) | |
185 | (choice :tag "Uncompress Message" | |
186 | (string :format "%v") | |
187 | (const :tag "No Message" nil)) | |
188 | (string :tag "Uncompress Program") | |
189 | (repeat :tag "Uncompress Arguments" string) | |
190 | (boolean :tag "Append") | |
191 | (boolean :tag "Auto Mode"))) | |
192 | :group 'jka-compr) | |
acd622cc | 193 | |
74b2c737 RS |
194 | (defvar jka-compr-mode-alist-additions |
195 | (list (cons "\\.tgz\\'" 'tar-mode)) | |
4eec33ae RS |
196 | "A list of pairs to add to `auto-mode-alist' when jka-compr is installed.") |
197 | ||
198 | ;; List of all the elements we actually added to file-coding-system-alist. | |
199 | (defvar jka-compr-added-to-file-coding-system-alist nil) | |
acd622cc | 200 | |
555235e6 RS |
201 | (defvar jka-compr-file-name-handler-entry |
202 | nil | |
203 | "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.") | |
555235e6 | 204 | \f |
43a3bdcc | 205 | ;;; Functions for accessing the return value of jka-compr-get-compression-info |
acd622cc RS |
206 | (defun jka-compr-info-regexp (info) (aref info 0)) |
207 | (defun jka-compr-info-compress-message (info) (aref info 1)) | |
208 | (defun jka-compr-info-compress-program (info) (aref info 2)) | |
209 | (defun jka-compr-info-compress-args (info) (aref info 3)) | |
210 | (defun jka-compr-info-uncompress-message (info) (aref info 4)) | |
211 | (defun jka-compr-info-uncompress-program (info) (aref info 5)) | |
212 | (defun jka-compr-info-uncompress-args (info) (aref info 6)) | |
213 | (defun jka-compr-info-can-append (info) (aref info 7)) | |
214 | (defun jka-compr-info-strip-extension (info) (aref info 8)) | |
215 | ||
216 | ||
217 | (defun jka-compr-get-compression-info (filename) | |
218 | "Return information about the compression scheme of FILENAME. | |
219 | The determination as to which compression scheme, if any, to use is | |
99bee6a4 | 220 | based on the filename itself and `jka-compr-compression-info-list'." |
acd622cc RS |
221 | (catch 'compression-info |
222 | (let ((case-fold-search nil)) | |
223 | (mapcar | |
224 | (function (lambda (x) | |
225 | (and (string-match (jka-compr-info-regexp x) filename) | |
226 | (throw 'compression-info x)))) | |
227 | jka-compr-compression-info-list) | |
228 | nil))) | |
229 | ||
230 | ||
231 | (put 'compression-error 'error-conditions '(compression-error file-error error)) | |
232 | ||
233 | ||
30c78e11 | 234 | (defvar jka-compr-acceptable-retval-list '(0 2 141)) |
acd622cc RS |
235 | |
236 | ||
237 | (defun jka-compr-error (prog args infile message &optional errfile) | |
238 | ||
239 | (let ((errbuf (get-buffer-create " *jka-compr-error*")) | |
240 | (curbuf (current-buffer))) | |
75e9c107 RS |
241 | (with-current-buffer errbuf |
242 | (widen) (erase-buffer) | |
243 | (insert (format "Error while executing \"%s %s < %s\"\n\n" | |
244 | prog | |
245 | (mapconcat 'identity args " ") | |
246 | infile)) | |
247 | ||
248 | (and errfile | |
249 | (insert-file-contents errfile))) | |
acd622cc RS |
250 | (display-buffer errbuf)) |
251 | ||
75e9c107 RS |
252 | (signal 'compression-error |
253 | (list "Opening input file" (format "error %s" message) infile))) | |
acd622cc RS |
254 | |
255 | ||
256 | (defvar jka-compr-dd-program | |
257 | "/bin/dd") | |
258 | ||
259 | ||
dfe05fac | 260 | (defvar jka-compr-dd-blocksize 256) |
acd622cc RS |
261 | |
262 | ||
263 | (defun jka-compr-partial-uncompress (prog message args infile beg len) | |
264 | "Call program PROG with ARGS args taking input from INFILE. | |
265 | Fourth and fifth args, BEG and LEN, specify which part of the output | |
ee139ed3 | 266 | to keep: LEN chars starting BEG chars from the beginning." |
acd622cc RS |
267 | (let* ((skip (/ beg jka-compr-dd-blocksize)) |
268 | (prefix (- beg (* skip jka-compr-dd-blocksize))) | |
269 | (count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize)))) | |
270 | (start (point)) | |
acd622cc RS |
271 | (err-file (jka-compr-make-temp-name)) |
272 | (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null" | |
273 | prog | |
274 | (mapconcat 'identity args " ") | |
275 | err-file | |
276 | jka-compr-dd-program | |
277 | jka-compr-dd-blocksize | |
278 | skip | |
dfe05fac RS |
279 | ;; dd seems to be unreliable about |
280 | ;; providing the last block. So, always | |
281 | ;; read one more than you think you need. | |
282 | (if count (concat "count=" (1+ count)) "")))) | |
acd622cc RS |
283 | |
284 | (unwind-protect | |
285 | (or (memq (call-process jka-compr-shell | |
286 | infile t nil "-c" | |
287 | run-string) | |
288 | jka-compr-acceptable-retval-list) | |
289 | ||
290 | (jka-compr-error prog args infile message err-file)) | |
291 | ||
292 | (jka-compr-delete-temp-file err-file)) | |
293 | ||
ee139ed3 | 294 | ;; Delete the stuff after what we want, if there is any. |
acd622cc | 295 | (and |
dfe05fac | 296 | len |
ee139ed3 | 297 | (< (+ start prefix len) (point)) |
dfe05fac | 298 | (delete-region (+ start prefix len) (point))) |
acd622cc | 299 | |
ee139ed3 | 300 | ;; Delete the stuff before what we want. |
acd622cc RS |
301 | (delete-region start (+ start prefix)))) |
302 | ||
303 | ||
304 | (defun jka-compr-call-process (prog message infile output temp args) | |
305 | (if jka-compr-use-shell | |
306 | ||
a81635fc | 307 | (let ((err-file (jka-compr-make-temp-name)) |
baefb016 | 308 | (coding-system-for-read (or coding-system-for-read 'undecided)) |
a81635fc RS |
309 | (coding-system-for-write 'no-conversion) ) |
310 | ||
acd622cc RS |
311 | (unwind-protect |
312 | ||
313 | (or (memq | |
314 | (call-process jka-compr-shell infile | |
315 | (if (stringp output) nil output) | |
316 | nil | |
317 | "-c" | |
318 | (format "%s %s 2> %s %s" | |
319 | prog | |
320 | (mapconcat 'identity args " ") | |
321 | err-file | |
322 | (if (stringp output) | |
323 | (concat "> " output) | |
324 | ""))) | |
325 | jka-compr-acceptable-retval-list) | |
326 | ||
327 | (jka-compr-error prog args infile message err-file)) | |
328 | ||
329 | (jka-compr-delete-temp-file err-file))) | |
330 | ||
331 | (or (zerop | |
332 | (apply 'call-process | |
333 | prog | |
334 | infile | |
335 | (if (stringp output) temp output) | |
336 | nil | |
337 | args)) | |
338 | (jka-compr-error prog args infile message)) | |
339 | ||
340 | (and (stringp output) | |
75e9c107 | 341 | (with-current-buffer temp |
acd622cc | 342 | (write-region (point-min) (point-max) output) |
75e9c107 | 343 | (erase-buffer))))) |
acd622cc RS |
344 | |
345 | ||
346 | ;;; Support for temp files. Much of this was inspired if not lifted | |
347 | ;;; from ange-ftp. | |
348 | ||
bbf5eb28 | 349 | (defcustom jka-compr-temp-name-template |
7b559d16 | 350 | (expand-file-name "jka-com" system-tmp-directory) |
362b539a | 351 | "Prefix added to all temp files created by jka-compr. |
bbf5eb28 RS |
352 | There should be no more than seven characters after the final `/'." |
353 | :type 'string | |
354 | :group 'jka-compr) | |
acd622cc RS |
355 | |
356 | (defvar jka-compr-temp-name-table (make-vector 31 nil)) | |
357 | ||
358 | (defun jka-compr-make-temp-name (&optional local-copy) | |
359 | "This routine will return the name of a new file." | |
360 | (let* ((lastchar ?a) | |
361 | (prevchar ?a) | |
362 | (template (concat jka-compr-temp-name-template "aa")) | |
363 | (lastpos (1- (length template))) | |
364 | (not-done t) | |
365 | file | |
366 | entry) | |
367 | ||
368 | (while not-done | |
369 | (aset template lastpos lastchar) | |
370 | (setq file (concat (make-temp-name template) "#")) | |
371 | (setq entry (intern file jka-compr-temp-name-table)) | |
372 | (if (or (get entry 'active) | |
373 | (file-exists-p file)) | |
374 | ||
375 | (progn | |
376 | (setq lastchar (1+ lastchar)) | |
377 | (if (> lastchar ?z) | |
378 | (progn | |
379 | (setq prevchar (1+ prevchar)) | |
380 | (setq lastchar ?a) | |
381 | (if (> prevchar ?z) | |
382 | (error "Can't allocate temp file.") | |
383 | (aset template (1- lastpos) prevchar))))) | |
384 | ||
385 | (put entry 'active (not local-copy)) | |
386 | (setq not-done nil))) | |
387 | ||
388 | file)) | |
389 | ||
390 | ||
391 | (defun jka-compr-delete-temp-file (temp) | |
392 | ||
393 | (put (intern temp jka-compr-temp-name-table) | |
394 | 'active nil) | |
395 | ||
396 | (condition-case () | |
397 | (delete-file temp) | |
398 | (error nil))) | |
399 | ||
400 | ||
401 | (defun jka-compr-write-region (start end file &optional append visit) | |
acd622cc RS |
402 | (let* ((filename (expand-file-name file)) |
403 | (visit-file (if (stringp visit) (expand-file-name visit) filename)) | |
404 | (info (jka-compr-get-compression-info visit-file))) | |
405 | ||
406 | (if info | |
407 | ||
408 | (let ((can-append (jka-compr-info-can-append info)) | |
409 | (compress-program (jka-compr-info-compress-program info)) | |
410 | (compress-message (jka-compr-info-compress-message info)) | |
411 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
412 | (uncompress-message (jka-compr-info-uncompress-message info)) | |
413 | (compress-args (jka-compr-info-compress-args info)) | |
414 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
acd622cc | 415 | (base-name (file-name-nondirectory visit-file)) |
75e9c107 | 416 | temp-file temp-buffer) |
acd622cc | 417 | |
75e9c107 RS |
418 | (setq temp-buffer (get-buffer-create " *jka-compr-wr-temp*")) |
419 | (with-current-buffer temp-buffer | |
420 | (widen) (erase-buffer)) | |
acd622cc | 421 | |
30c78e11 RS |
422 | (if (and append |
423 | (not can-append) | |
424 | (file-exists-p filename)) | |
425 | ||
426 | (let* ((local-copy (file-local-copy filename)) | |
427 | (local-file (or local-copy filename))) | |
428 | ||
429 | (setq temp-file local-file)) | |
430 | ||
431 | (setq temp-file (jka-compr-make-temp-name))) | |
acd622cc RS |
432 | |
433 | (and | |
434 | compress-message | |
435 | (message "%s %s..." compress-message base-name)) | |
30c78e11 | 436 | |
8fb1a583 RS |
437 | (jka-compr-run-real-handler 'write-region |
438 | (list start end temp-file t 'dont)) | |
acd622cc | 439 | |
baefb016 KH |
440 | ;; Here we must read the output of compress program as is |
441 | ;; without any code conversion. | |
442 | (let ((coding-system-for-read 'no-conversion)) | |
443 | (jka-compr-call-process compress-program | |
444 | (concat compress-message | |
445 | " " base-name) | |
446 | temp-file | |
447 | temp-buffer | |
448 | nil | |
449 | compress-args)) | |
acd622cc | 450 | |
75e9c107 | 451 | (with-current-buffer temp-buffer |
a81635fc RS |
452 | (let ((coding-system-for-write 'no-conversion)) |
453 | (if (memq system-type '(ms-dos windows-nt)) | |
454 | (setq buffer-file-type t) ) | |
455 | (jka-compr-run-real-handler 'write-region | |
456 | (list (point-min) (point-max) | |
457 | filename | |
458 | (and append can-append) 'dont)) | |
459 | (erase-buffer)) ) | |
acd622cc RS |
460 | |
461 | (jka-compr-delete-temp-file temp-file) | |
462 | ||
463 | (and | |
464 | compress-message | |
465 | (message "%s %s...done" compress-message base-name)) | |
466 | ||
467 | (cond | |
468 | ((eq visit t) | |
469 | (setq buffer-file-name filename) | |
470 | (set-visited-file-modtime)) | |
471 | ((stringp visit) | |
472 | (setq buffer-file-name visit) | |
473 | (let ((buffer-file-name filename)) | |
474 | (set-visited-file-modtime)))) | |
475 | ||
476 | (and (or (eq visit t) | |
477 | (eq visit nil) | |
478 | (stringp visit)) | |
479 | (message "Wrote %s" visit-file)) | |
480 | ||
481 | nil) | |
482 | ||
8fb1a583 RS |
483 | (jka-compr-run-real-handler 'write-region |
484 | (list start end filename append visit))))) | |
acd622cc RS |
485 | |
486 | ||
54b2aa5c | 487 | (defun jka-compr-insert-file-contents (file &optional visit beg end replace) |
acd622cc RS |
488 | (barf-if-buffer-read-only) |
489 | ||
490 | (and (or beg end) | |
491 | visit | |
492 | (error "Attempt to visit less than an entire file")) | |
493 | ||
494 | (let* ((filename (expand-file-name file)) | |
495 | (info (jka-compr-get-compression-info filename))) | |
496 | ||
497 | (if info | |
498 | ||
499 | (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
500 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
501 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
502 | (base-name (file-name-nondirectory filename)) | |
503 | (notfound nil) | |
8fb1a583 RS |
504 | (local-copy |
505 | (jka-compr-run-real-handler 'file-local-copy (list filename))) | |
acd622cc | 506 | local-file |
a81635fc | 507 | size start |
4eec33ae RS |
508 | (coding-system-for-read |
509 | (or coding-system-for-read | |
bab00368 RS |
510 | ;; If multibyte characters are disabled, |
511 | ;; don't do that conversion. | |
512 | (and (null enable-multibyte-characters) | |
513 | 'raw-text) | |
4eec33ae RS |
514 | (let ((tail file-coding-system-alist) |
515 | (newfile | |
516 | (jka-compr-byte-compiler-base-file-name file)) | |
517 | result) | |
518 | (while tail | |
519 | (if (string-match (car (car tail)) newfile) | |
520 | (setq result (car (cdr (car tail))) | |
521 | tail nil)) | |
522 | (setq tail (cdr tail))) | |
523 | result) | |
524 | 'undecided)) ) | |
acd622cc RS |
525 | |
526 | (setq local-file (or local-copy filename)) | |
527 | ||
528 | (and | |
529 | visit | |
530 | (setq buffer-file-name filename)) | |
531 | ||
532 | (unwind-protect ; to make sure local-copy gets deleted | |
533 | ||
534 | (progn | |
535 | ||
536 | (and | |
537 | uncompress-message | |
538 | (message "%s %s..." uncompress-message base-name)) | |
539 | ||
540 | (condition-case error-code | |
541 | ||
542 | (progn | |
094cf604 RS |
543 | (if replace |
544 | (goto-char (point-min))) | |
acd622cc RS |
545 | (setq start (point)) |
546 | (if (or beg end) | |
547 | (jka-compr-partial-uncompress uncompress-program | |
548 | (concat uncompress-message | |
549 | " " base-name) | |
550 | uncompress-args | |
551 | local-file | |
552 | (or beg 0) | |
553 | (if (and beg end) | |
554 | (- end beg) | |
555 | end)) | |
ae849784 RS |
556 | ;; If visiting, bind off buffer-file-name so that |
557 | ;; file-locking will not ask whether we should | |
558 | ;; really edit the buffer. | |
559 | (let ((buffer-file-name | |
560 | (if visit nil buffer-file-name))) | |
561 | (jka-compr-call-process uncompress-program | |
562 | (concat uncompress-message | |
563 | " " base-name) | |
564 | local-file | |
565 | t | |
566 | nil | |
567 | uncompress-args))) | |
acd622cc | 568 | (setq size (- (point) start)) |
094cf604 RS |
569 | (if replace |
570 | (let* ((del-beg (point)) | |
571 | (del-end (+ del-beg size))) | |
572 | (delete-region del-beg | |
573 | (min del-end (point-max))))) | |
574 | (goto-char start)) | |
acd622cc RS |
575 | (error |
576 | (if (and (eq (car error-code) 'file-error) | |
577 | (eq (nth 3 error-code) local-file)) | |
578 | (if visit | |
579 | (setq notfound error-code) | |
580 | (signal 'file-error | |
581 | (cons "Opening input file" | |
582 | (nthcdr 2 error-code)))) | |
583 | (signal (car error-code) (cdr error-code)))))) | |
584 | ||
585 | (and | |
586 | local-copy | |
587 | (file-exists-p local-copy) | |
588 | (delete-file local-copy))) | |
589 | ||
590 | (and | |
591 | visit | |
592 | (progn | |
8fb1a583 | 593 | (unlock-buffer) |
acd622cc RS |
594 | (setq buffer-file-name filename) |
595 | (set-visited-file-modtime))) | |
596 | ||
597 | (and | |
598 | uncompress-message | |
599 | (message "%s %s...done" uncompress-message base-name)) | |
600 | ||
601 | (and | |
602 | visit | |
603 | notfound | |
604 | (signal 'file-error | |
605 | (cons "Opening input file" (nth 2 notfound)))) | |
606 | ||
094cf604 RS |
607 | ;; Run the functions that insert-file-contents would. |
608 | (let ((p after-insert-file-functions) | |
609 | (insval size)) | |
610 | (while p | |
611 | (setq insval (funcall (car p) size)) | |
612 | (if insval | |
613 | (progn | |
614 | (or (integerp insval) | |
615 | (signal 'wrong-type-argument | |
616 | (list 'integerp insval))) | |
617 | (setq size insval))) | |
618 | (setq p (cdr p)))) | |
619 | ||
acd622cc RS |
620 | (list filename size)) |
621 | ||
8fb1a583 RS |
622 | (jka-compr-run-real-handler 'insert-file-contents |
623 | (list file visit beg end replace))))) | |
acd622cc RS |
624 | |
625 | ||
626 | (defun jka-compr-file-local-copy (file) | |
acd622cc RS |
627 | (let* ((filename (expand-file-name file)) |
628 | (info (jka-compr-get-compression-info filename))) | |
629 | ||
630 | (if info | |
631 | ||
632 | (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
633 | (uncompress-program (jka-compr-info-uncompress-program info)) | |
634 | (uncompress-args (jka-compr-info-uncompress-args info)) | |
635 | (base-name (file-name-nondirectory filename)) | |
8fb1a583 RS |
636 | (local-copy |
637 | (jka-compr-run-real-handler 'file-local-copy (list filename))) | |
acd622cc | 638 | (temp-file (jka-compr-make-temp-name t)) |
30c78e11 | 639 | (temp-buffer (get-buffer-create " *jka-compr-flc-temp*")) |
acd622cc | 640 | (notfound nil) |
acd622cc RS |
641 | local-file) |
642 | ||
643 | (setq local-file (or local-copy filename)) | |
644 | ||
645 | (unwind-protect | |
646 | ||
75e9c107 | 647 | (with-current-buffer temp-buffer |
acd622cc RS |
648 | |
649 | (and | |
650 | uncompress-message | |
651 | (message "%s %s..." uncompress-message base-name)) | |
acd622cc | 652 | |
baefb016 KH |
653 | ;; Here we must read the output of uncompress program |
654 | ;; and write it to TEMP-FILE without any code | |
655 | ;; conversion. An appropriate code conversion (if | |
656 | ;; necessary) is done by the later I/O operation | |
657 | ;; (e.g. load). | |
658 | (let ((coding-system-for-read 'no-conversion) | |
659 | (coding-system-for-write 'no-conversion)) | |
660 | ||
661 | (jka-compr-call-process uncompress-program | |
662 | (concat uncompress-message | |
663 | " " base-name) | |
664 | local-file | |
665 | t | |
666 | nil | |
667 | uncompress-args) | |
668 | ||
669 | (and | |
670 | uncompress-message | |
671 | (message "%s %s...done" uncompress-message base-name)) | |
672 | ||
673 | (write-region | |
674 | (point-min) (point-max) temp-file nil 'dont))) | |
acd622cc RS |
675 | |
676 | (and | |
677 | local-copy | |
678 | (file-exists-p local-copy) | |
679 | (delete-file local-copy)) | |
680 | ||
acd622cc RS |
681 | (kill-buffer temp-buffer)) |
682 | ||
683 | temp-file) | |
684 | ||
8fb1a583 | 685 | (jka-compr-run-real-handler 'file-local-copy (list filename))))) |
acd622cc RS |
686 | |
687 | ||
688 | ;;; Support for loading compressed files. | |
689 | (defun jka-compr-load (file &optional noerror nomessage nosuffix) | |
690 | "Documented as original." | |
691 | ||
692 | (let* ((local-copy (jka-compr-file-local-copy file)) | |
693 | (load-file (or local-copy file))) | |
694 | ||
695 | (unwind-protect | |
696 | ||
8fb1a583 RS |
697 | (let (inhibit-file-name-operation |
698 | inhibit-file-name-handlers) | |
acd622cc RS |
699 | (or nomessage |
700 | (message "Loading %s..." file)) | |
701 | ||
9b37d8a9 RS |
702 | (let ((load-force-doc-strings t)) |
703 | (load load-file noerror t t)) | |
acd622cc RS |
704 | |
705 | (or nomessage | |
706 | (message "Loading %s...done." file))) | |
707 | ||
acd622cc RS |
708 | (jka-compr-delete-temp-file local-copy)) |
709 | ||
710 | t)) | |
3068998d RS |
711 | |
712 | (defun jka-compr-byte-compiler-base-file-name (file) | |
713 | (let ((info (jka-compr-get-compression-info file))) | |
714 | (if (and info (jka-compr-info-strip-extension info)) | |
715 | (save-match-data | |
716 | (substring file 0 (string-match (jka-compr-info-regexp info) file))) | |
717 | file))) | |
8fb1a583 RS |
718 | \f |
719 | (put 'write-region 'jka-compr 'jka-compr-write-region) | |
720 | (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents) | |
721 | (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy) | |
722 | (put 'load 'jka-compr 'jka-compr-load) | |
3068998d RS |
723 | (put 'byte-compiler-base-file-name 'jka-compr |
724 | 'jka-compr-byte-compiler-base-file-name) | |
acd622cc | 725 | |
9fdf055b KH |
726 | (defvar jka-compr-inhibit nil |
727 | "Non-nil means inhibit automatic uncompression temporarily. | |
728 | Lisp programs can bind this to t to do that. | |
729 | It is not recommended to set this variable permanently to anything but nil.") | |
730 | ||
acd622cc | 731 | (defun jka-compr-handler (operation &rest args) |
8fb1a583 RS |
732 | (save-match-data |
733 | (let ((jka-op (get operation 'jka-compr))) | |
9fdf055b | 734 | (if (and jka-op (not jka-compr-inhibit)) |
8fb1a583 RS |
735 | (apply jka-op args) |
736 | (jka-compr-run-real-handler operation args))))) | |
acd622cc | 737 | |
99bee6a4 RS |
738 | ;; If we are given an operation that we don't handle, |
739 | ;; call the Emacs primitive for that operation, | |
740 | ;; and manipulate the inhibit variables | |
741 | ;; to prevent the primitive from calling our handler again. | |
742 | (defun jka-compr-run-real-handler (operation args) | |
743 | (let ((inhibit-file-name-handlers | |
744 | (cons 'jka-compr-handler | |
745 | (and (eq inhibit-file-name-operation operation) | |
746 | inhibit-file-name-handlers))) | |
747 | (inhibit-file-name-operation operation)) | |
748 | (apply operation args))) | |
749 | ||
b6dca218 | 750 | ;;;###autoload(defun auto-compression-mode (&optional arg) |
e5220563 RS |
751 | ;;;###autoload "\ |
752 | ;;;###autoloadToggle automatic file compression and uncompression. | |
b6dca218 RS |
753 | ;;;###autoloadWith prefix argument ARG, turn auto compression on if positive, else off. |
754 | ;;;###autoloadReturns the new status of auto compression (non-nil means on)." | |
e5220563 | 755 | ;;;###autoload (interactive "P") |
b6dca218 RS |
756 | ;;;###autoload (if (not (fboundp 'jka-compr-installed-p)) |
757 | ;;;###autoload (progn | |
758 | ;;;###autoload (require 'jka-compr) | |
759 | ;;;###autoload ;; That turned the mode on, so make it initially off. | |
760 | ;;;###autoload (toggle-auto-compression))) | |
e5220563 | 761 | ;;;###autoload (toggle-auto-compression arg t)) |
b6dca218 | 762 | |
e5220563 | 763 | (defun toggle-auto-compression (&optional arg message) |
61793fbf | 764 | "Toggle automatic file compression and uncompression. |
acd622cc | 765 | With prefix argument ARG, turn auto compression on if positive, else off. |
e5220563 RS |
766 | Returns the new status of auto compression (non-nil means on). |
767 | If the argument MESSAGE is non-nil, it means to print a message | |
768 | saying whether the mode is now on or off." | |
769 | (interactive "P\np") | |
acd622cc RS |
770 | (let* ((installed (jka-compr-installed-p)) |
771 | (flag (if (null arg) | |
772 | (not installed) | |
773 | (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))))) | |
774 | ||
775 | (cond | |
776 | ((and flag installed) t) ; already installed | |
777 | ||
778 | ((and (not flag) (not installed)) nil) ; already not installed | |
779 | ||
780 | (flag | |
781 | (jka-compr-install)) | |
782 | ||
783 | (t | |
784 | (jka-compr-uninstall))) | |
785 | ||
786 | ||
e5220563 | 787 | (and message |
acd622cc RS |
788 | (if flag |
789 | (message "Automatic file (de)compression is now ON.") | |
790 | (message "Automatic file (de)compression is now OFF."))) | |
791 | ||
792 | flag)) | |
acd622cc RS |
793 | |
794 | (defun jka-compr-build-file-regexp () | |
795 | (concat | |
796 | "\\(" | |
797 | (mapconcat | |
798 | 'jka-compr-info-regexp | |
799 | jka-compr-compression-info-list | |
800 | "\\)\\|\\(") | |
801 | "\\)")) | |
802 | ||
803 | ||
804 | (defun jka-compr-install () | |
805 | "Install jka-compr. | |
919a07bb RS |
806 | This adds entries to `file-name-handler-alist' and `auto-mode-alist' |
807 | and `inhibit-first-line-modes-suffixes'." | |
acd622cc RS |
808 | |
809 | (setq jka-compr-file-name-handler-entry | |
810 | (cons (jka-compr-build-file-regexp) 'jka-compr-handler)) | |
811 | ||
812 | (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry | |
813 | file-name-handler-alist)) | |
814 | ||
4eec33ae RS |
815 | (setq jka-compr-added-to-file-coding-system-alist nil) |
816 | ||
acd622cc RS |
817 | (mapcar |
818 | (function (lambda (x) | |
4eec33ae RS |
819 | ;; Don't do multibyte encoding on the compressed files. |
820 | (let ((elt (cons (jka-compr-info-regexp x) | |
821 | '(no-conversion . no-conversion)))) | |
822 | (setq file-coding-system-alist | |
823 | (cons elt file-coding-system-alist)) | |
824 | (setq jka-compr-added-to-file-coding-system-alist | |
825 | (cons elt jka-compr-added-to-file-coding-system-alist))) | |
826 | ||
469f4e8c RS |
827 | (and (jka-compr-info-strip-extension x) |
828 | ;; Make entries in auto-mode-alist so that modes | |
829 | ;; are chosen right according to the file names | |
830 | ;; sans `.gz'. | |
831 | (setq auto-mode-alist | |
832 | (cons (list (jka-compr-info-regexp x) | |
833 | nil 'jka-compr) | |
834 | auto-mode-alist)) | |
835 | ;; Also add these regexps to | |
836 | ;; inhibit-first-line-modes-suffixes, so that a | |
837 | ;; -*- line in the first file of a compressed tar | |
838 | ;; file doesn't override tar-mode. | |
839 | (setq inhibit-first-line-modes-suffixes | |
840 | (cons (jka-compr-info-regexp x) | |
841 | inhibit-first-line-modes-suffixes))))) | |
74b2c737 RS |
842 | jka-compr-compression-info-list) |
843 | (setq auto-mode-alist | |
844 | (append auto-mode-alist jka-compr-mode-alist-additions))) | |
acd622cc RS |
845 | |
846 | ||
847 | (defun jka-compr-uninstall () | |
848 | "Uninstall jka-compr. | |
99bee6a4 | 849 | This removes the entries in `file-name-handler-alist' and `auto-mode-alist' |
919a07bb RS |
850 | and `inhibit-first-line-modes-suffixes' that were added |
851 | by `jka-compr-installed'." | |
852 | ;; Delete from inhibit-first-line-modes-suffixes | |
853 | ;; what jka-compr-install added. | |
854 | (mapcar | |
855 | (function (lambda (x) | |
856 | (and (jka-compr-info-strip-extension x) | |
857 | (setq inhibit-first-line-modes-suffixes | |
858 | (delete (jka-compr-info-regexp x) | |
859 | inhibit-first-line-modes-suffixes))))) | |
860 | jka-compr-compression-info-list) | |
acd622cc RS |
861 | |
862 | (let* ((fnha (cons nil file-name-handler-alist)) | |
863 | (last fnha)) | |
864 | ||
865 | (while (cdr last) | |
866 | (if (eq (cdr (car (cdr last))) 'jka-compr-handler) | |
867 | (setcdr last (cdr (cdr last))) | |
868 | (setq last (cdr last)))) | |
869 | ||
870 | (setq file-name-handler-alist (cdr fnha))) | |
871 | ||
872 | (let* ((ama (cons nil auto-mode-alist)) | |
873 | (last ama) | |
874 | entry) | |
875 | ||
876 | (while (cdr last) | |
877 | (setq entry (car (cdr last))) | |
74b2c737 RS |
878 | (if (or (member entry jka-compr-mode-alist-additions) |
879 | (and (consp (cdr entry)) | |
880 | (eq (nth 2 entry) 'jka-compr))) | |
acd622cc RS |
881 | (setcdr last (cdr (cdr last))) |
882 | (setq last (cdr last)))) | |
883 | ||
4eec33ae RS |
884 | (setq auto-mode-alist (cdr ama))) |
885 | ||
886 | (let* ((ama (cons nil file-coding-system-alist)) | |
887 | (last ama) | |
888 | entry) | |
889 | ||
890 | (while (cdr last) | |
891 | (setq entry (car (cdr last))) | |
892 | (if (member entry jka-compr-added-to-file-coding-system-alist) | |
893 | (setcdr last (cdr (cdr last))) | |
894 | (setq last (cdr last)))) | |
895 | ||
896 | (setq file-coding-system-alist (cdr ama)))) | |
acd622cc RS |
897 | |
898 | ||
899 | (defun jka-compr-installed-p () | |
900 | "Return non-nil if jka-compr is installed. | |
99bee6a4 | 901 | The return value is the entry in `file-name-handler-alist' for jka-compr." |
acd622cc RS |
902 | |
903 | (let ((fnha file-name-handler-alist) | |
904 | (installed nil)) | |
905 | ||
906 | (while (and fnha (not installed)) | |
907 | (and (eq (cdr (car fnha)) 'jka-compr-handler) | |
908 | (setq installed (car fnha))) | |
909 | (setq fnha (cdr fnha))) | |
910 | ||
911 | installed)) | |
912 | ||
913 | ||
914 | ;;; Add the file I/O hook if it does not already exist. | |
915 | ;;; Make sure that jka-compr-file-name-handler-entry is eq to the | |
916 | ;;; entry for jka-compr in file-name-handler-alist. | |
917 | (and (jka-compr-installed-p) | |
918 | (jka-compr-uninstall)) | |
919 | ||
920 | (jka-compr-install) | |
921 | ||
922 | ||
923 | (provide 'jka-compr) | |
924 | ||
925 | ;; jka-compr.el ends here. |