Mercurial > emacs
annotate lisp/jka-compr.el @ 8854:c21c9ae5f8e7
(print): sprintf arg for frame depends on VALBITS.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 17 Sep 1994 05:36:53 +0000 |
parents | ade4b542a74c |
children | 3b359f97fedb |
rev | line source |
---|---|
6002 | 1 ;;; jka-compr.el - reading/writing/loading compressed files. |
2 ;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: jka@ece.cmu.edu (Jay K. Adams) | |
5 ;; Keywords: data | |
6 | |
7 ;;; Commentary: | |
8 | |
9 ;;; This package implements low-level support for reading, writing, | |
10 ;;; and loading compressed files. It hooks into the low-level file | |
11 ;;; I/O functions (including write-region and insert-file-contents) so | |
12 ;;; that they automatically compress or uncompress a file if the file | |
13 ;;; appears to need it (based on the extension of the file name). | |
6041
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
14 ;;; Packages like Rmail, VM, GNUS, and Info should be able to work |
6002 | 15 ;;; with compressed files without modification. |
16 | |
17 | |
18 ;;; INSTRUCTIONS: | |
19 ;;; | |
20 ;;; To use jka-compr, simply load this package, and edit as usual. | |
21 ;;; Its operation should be transparent to the user (except for | |
22 ;;; messages appearing when a file is being compressed or | |
23 ;;; uncompressed). | |
24 ;;; | |
25 ;;; The variable, jka-compr-compression-info-list can be used to | |
26 ;;; customize jka-compr to work with other compression programs. | |
27 ;;; The default value of this variable allows jka-compr to work with | |
28 ;;; Unix compress and gzip. | |
29 ;;; | |
30 ;;; If you are concerned about the stderr output of gzip and other | |
31 ;;; compression/decompression programs showing up in your buffers, you | |
32 ;;; should set the discard-error flag in the compression-info-list. | |
33 ;;; This will cause the stderr of all programs to be discarded. | |
34 ;;; However, it also causes emacs to call compression/uncompression | |
35 ;;; programs through a shell (which is specified by jka-compr-shell). | |
36 ;;; This may be a drag if, on your system, starting up a shell is | |
37 ;;; slow. | |
38 ;;; | |
39 ;;; If you don't want messages about compressing and decompressing | |
40 ;;; to show up in the echo area, you can set the compress-name and | |
41 ;;; decompress-name fields of the jka-compr-compression-info-list to | |
42 ;;; nil. | |
43 | |
44 | |
45 ;;; APPLICATION NOTES: | |
46 ;;; | |
47 ;;; rmail, vm, gnus, etc. | |
48 ;;; To use compressed mail folders, .newsrc files, etc., you need | |
49 ;;; only compress the file. Since jka-compr searches for .gz | |
50 ;;; versions of the files it's finding, you need not change | |
51 ;;; variables within rmail, gnus, etc. | |
52 ;;; | |
53 ;;; | |
54 ;;; crypt++ | |
55 ;;; jka-compr can coexist with crpyt++ if you take all the decompression | |
56 ;;; entries out of the crypt-encoding-list. Clearly problems will arise if | |
57 ;;; you have two programs trying to compress/decompress files. jka-compr | |
58 ;;; will not "work with" crypt++ in the following sense: you won't be able to | |
59 ;;; decode encrypted compressed files--that is, files that have been | |
60 ;;; compressed then encrypted (in that order). Theoretically, crypt++ and | |
61 ;;; jka-compr could properly handle a file that has been encrypted then | |
62 ;;; compressed, but there is little point in trying to compress an encrypted | |
63 ;;; file. | |
64 ;;; | |
65 ;;; | |
66 ;;; tar-mode | |
67 ;;; Some people like to use extensions like .trz for compressed tar files. | |
68 ;;; To handle these sorts of files, you have to add an entry to | |
69 ;;; jka-compr-compression-info-list that looks something like this: | |
70 ;;; | |
71 ;;; ["\\.trz\\'" "\037\213" | |
72 ;;; "zip" "gzip" nil ("-q") | |
73 ;;; "unzip" "gzip" nil ("-q" "-d") | |
74 ;;; t | |
75 ;;; nil] | |
76 ;;; | |
77 ;;; The last nil in the vector (the "extension" field) prevents jka-compr | |
78 ;;; from attempting to add .trz to an ordinary file name when it is looking | |
79 ;;; for a compressed version of that file (i.e. don't look for things like | |
80 ;;; foobar.c.trz). | |
81 ;;; | |
82 ;;; Finally, to make tar-mode start up automatically, you have to add an | |
83 ;;; entry to auto-mode-alist that looks like this | |
84 ;;; | |
85 ;;; ("\\.trz\\'" . tar-mode) | |
86 ;;; | |
87 | |
88 | |
89 ;;; ACKNOWLEDGMENTS | |
90 ;;; | |
91 ;;; jka-compr is a V19 adaptation of jka-compr for V18 of Emacs. Many people | |
92 ;;; have made helpful suggestions, reported bugs, and even fixed bugs in | |
93 ;;; jka-compr. I recall the following people as being particularly helpful. | |
94 ;;; | |
95 ;;; Jean-loup Gailly | |
96 ;;; David Hughes | |
97 ;;; Richard Pieri | |
98 ;;; Daniel Quinlan | |
99 ;;; Chris P. Ross | |
100 ;;; Rick Sladkey | |
101 ;;; | |
102 ;;; Andy Norman's ange-ftp was the inspiration for the original jka-compr for | |
103 ;;; Version 18 of Emacs. | |
104 ;;; | |
105 ;;; After I had made progress on the original jka-compr for V18, I learned of a | |
106 ;;; package written by Kazushi Jam Marukawa, called jam-zcat, that did exactly | |
107 ;;; what I was trying to do. I looked over the jam-zcat source code and | |
108 ;;; probably got some ideas from it. | |
109 ;;; | |
110 | |
111 ;;; Code: | |
112 | |
113 (defvar jka-compr-shell "sh" | |
114 "*Shell to be used for calling compression programs. | |
115 The value of this variable only matters if you want to discard the | |
116 stderr of a compression/decompression program (see the documentation | |
7066 | 117 for `jka-compr-compression-info-list').") |
6002 | 118 |
119 | |
120 (defvar jka-compr-use-shell t) | |
121 | |
122 | |
123 ;;; I have this defined so that .Z files are assumed to be in unix | |
124 ;;; compress format; and .gz files, in gzip format. | |
125 (defvar jka-compr-compression-info-list | |
126 ;;[regexp | |
7616
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
127 ;; compr-message compr-prog compr-args |
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
128 ;; uncomp-message uncomp-prog uncomp-args |
6002 | 129 ;; can-append auto-mode-flag] |
8774
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
130 '(["\\.Z\\(~\\|\\.~[0-9]+~\\)?\\'" |
6002 | 131 "compressing" "compress" ("-c") |
132 "uncompressing" "uncompress" ("-c") | |
133 nil t] | |
8774
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
134 ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'" |
6002 | 135 "zipping" "gzip" ("-c" "-q") |
136 "unzipping" "gzip" ("-c" "-q" "-d") | |
137 t t]) | |
138 | |
139 "List of vectors that describe available compression techniques. | |
140 Each element, which describes a compression technique, is a vector of | |
7616
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
141 the form [REGEXP COMPRESS-MSG COMPRESS-PROGRAM COMPRESS-ARGS |
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
142 UNCOMPRESS-MSG UNCOMPRESS-PROGRAM UNCOMPRESS-ARGS |
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
143 APPEND-FLAG EXTENSION], where: |
6002 | 144 |
145 regexp is a regexp that matches filenames that are | |
146 compressed with this format | |
147 | |
7616
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
148 compress-msg is the message to issue to the user when doing this |
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
149 type of compression (nil means no message) |
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
150 |
6002 | 151 compress-program is a program that performs this compression |
152 | |
153 compress-args is a list of args to pass to the compress program | |
154 | |
7616
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
155 uncompress-msg is the message to issue to the user when doing this |
b0098553bafe
(jka-compr-compression-info-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7066
diff
changeset
|
156 type of uncompression (nil means no message) |
6002 | 157 |
158 uncompress-program is a program that performs this compression | |
159 | |
160 uncompress-args is a list of args to pass to the uncompress program | |
161 | |
162 append-flag is non-nil if this compression technique can be | |
163 appended | |
164 | |
165 auto-mode flag non-nil means strip the regexp from file names | |
166 before attempting to set the mode. | |
167 | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
168 Because of the way `call-process' is defined, discarding the stderr output of |
6002 | 169 a program adds the overhead of starting a shell each time the program is |
170 invoked.") | |
171 | |
172 | |
7617
edc853e14f7b
(jka-compr-op-table, jka-compr-file-name-handler-entry): Move defvars.
Richard M. Stallman <rms@gnu.org>
parents:
7616
diff
changeset
|
173 (defvar jka-compr-file-name-handler-entry |
edc853e14f7b
(jka-compr-op-table, jka-compr-file-name-handler-entry): Move defvars.
Richard M. Stallman <rms@gnu.org>
parents:
7616
diff
changeset
|
174 nil |
edc853e14f7b
(jka-compr-op-table, jka-compr-file-name-handler-entry): Move defvars.
Richard M. Stallman <rms@gnu.org>
parents:
7616
diff
changeset
|
175 "The entry in `file-name-handler-alist' used by the jka-compr I/O functions.") |
edc853e14f7b
(jka-compr-op-table, jka-compr-file-name-handler-entry): Move defvars.
Richard M. Stallman <rms@gnu.org>
parents:
7616
diff
changeset
|
176 |
6002 | 177 ;;; Functions for accessing the return value of jka-get-compression-info |
178 (defun jka-compr-info-regexp (info) (aref info 0)) | |
179 (defun jka-compr-info-compress-message (info) (aref info 1)) | |
180 (defun jka-compr-info-compress-program (info) (aref info 2)) | |
181 (defun jka-compr-info-compress-args (info) (aref info 3)) | |
182 (defun jka-compr-info-uncompress-message (info) (aref info 4)) | |
183 (defun jka-compr-info-uncompress-program (info) (aref info 5)) | |
184 (defun jka-compr-info-uncompress-args (info) (aref info 6)) | |
185 (defun jka-compr-info-can-append (info) (aref info 7)) | |
186 (defun jka-compr-info-strip-extension (info) (aref info 8)) | |
187 | |
188 | |
189 (defun jka-compr-get-compression-info (filename) | |
190 "Return information about the compression scheme of FILENAME. | |
191 The determination as to which compression scheme, if any, to use is | |
7066 | 192 based on the filename itself and `jka-compr-compression-info-list'." |
6002 | 193 (catch 'compression-info |
194 (let ((case-fold-search nil)) | |
195 (mapcar | |
196 (function (lambda (x) | |
197 (and (string-match (jka-compr-info-regexp x) filename) | |
198 (throw 'compression-info x)))) | |
199 jka-compr-compression-info-list) | |
200 nil))) | |
201 | |
202 | |
203 (put 'compression-error 'error-conditions '(compression-error file-error error)) | |
204 | |
205 | |
206 (defvar jka-compr-acceptable-retval-list '(0 141)) | |
207 | |
208 | |
209 (defun jka-compr-error (prog args infile message &optional errfile) | |
210 | |
211 (let ((errbuf (get-buffer-create " *jka-compr-error*")) | |
212 (curbuf (current-buffer))) | |
213 (set-buffer errbuf) | |
214 (widen) (erase-buffer) | |
215 (insert (format "Error while executing \"%s %s < %s\"\n\n" | |
216 prog | |
217 (mapconcat 'identity args " ") | |
218 infile)) | |
219 | |
220 (and errfile | |
221 (insert-file-contents errfile)) | |
222 | |
223 (set-buffer curbuf) | |
224 (display-buffer errbuf)) | |
225 | |
226 (signal 'compression-error (list "Opening input file" (format "error %s" message) infile))) | |
227 | |
228 | |
229 (defvar jka-compr-dd-program | |
230 "/bin/dd") | |
231 | |
232 | |
6041
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
233 (defvar jka-compr-dd-blocksize 256) |
6002 | 234 |
235 | |
236 (defun jka-compr-partial-uncompress (prog message args infile beg len) | |
237 "Call program PROG with ARGS args taking input from INFILE. | |
238 Fourth and fifth args, BEG and LEN, specify which part of the output | |
8065
31eb7a0e2763
(jka-compr-partial-uncompress): Handle the case where
Richard M. Stallman <rms@gnu.org>
parents:
7976
diff
changeset
|
239 to keep: LEN chars starting BEG chars from the beginning." |
6002 | 240 (let* ((skip (/ beg jka-compr-dd-blocksize)) |
241 (prefix (- beg (* skip jka-compr-dd-blocksize))) | |
242 (count (and len (1+ (/ (+ len prefix) jka-compr-dd-blocksize)))) | |
243 (start (point)) | |
244 (err-file (jka-compr-make-temp-name)) | |
245 (run-string (format "%s %s 2> %s | %s bs=%d skip=%d %s 2> /dev/null" | |
246 prog | |
247 (mapconcat 'identity args " ") | |
248 err-file | |
249 jka-compr-dd-program | |
250 jka-compr-dd-blocksize | |
251 skip | |
6041
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
252 ;; dd seems to be unreliable about |
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
253 ;; providing the last block. So, always |
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
254 ;; read one more than you think you need. |
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
255 (if count (concat "count=" (1+ count)) "")))) |
6002 | 256 |
257 (unwind-protect | |
258 (or (memq (call-process jka-compr-shell | |
259 infile t nil "-c" | |
260 run-string) | |
261 jka-compr-acceptable-retval-list) | |
262 | |
263 (jka-compr-error prog args infile message err-file)) | |
264 | |
265 (jka-compr-delete-temp-file err-file)) | |
266 | |
8065
31eb7a0e2763
(jka-compr-partial-uncompress): Handle the case where
Richard M. Stallman <rms@gnu.org>
parents:
7976
diff
changeset
|
267 ;; Delete the stuff after what we want, if there is any. |
6002 | 268 (and |
6041
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
269 len |
8065
31eb7a0e2763
(jka-compr-partial-uncompress): Handle the case where
Richard M. Stallman <rms@gnu.org>
parents:
7976
diff
changeset
|
270 (< (+ start prefix len) (point)) |
6041
649926e9c1a8
(jka-compr-partial-uncompress): Make dd give you
Richard M. Stallman <rms@gnu.org>
parents:
6002
diff
changeset
|
271 (delete-region (+ start prefix len) (point))) |
6002 | 272 |
8065
31eb7a0e2763
(jka-compr-partial-uncompress): Handle the case where
Richard M. Stallman <rms@gnu.org>
parents:
7976
diff
changeset
|
273 ;; Delete the stuff before what we want. |
6002 | 274 (delete-region start (+ start prefix)))) |
275 | |
276 | |
277 (defun jka-compr-call-process (prog message infile output temp args) | |
278 (if jka-compr-use-shell | |
279 | |
280 (let ((err-file (jka-compr-make-temp-name))) | |
281 | |
282 (unwind-protect | |
283 | |
284 (or (memq | |
285 (call-process jka-compr-shell infile | |
286 (if (stringp output) nil output) | |
287 nil | |
288 "-c" | |
289 (format "%s %s 2> %s %s" | |
290 prog | |
291 (mapconcat 'identity args " ") | |
292 err-file | |
293 (if (stringp output) | |
294 (concat "> " output) | |
295 ""))) | |
296 jka-compr-acceptable-retval-list) | |
297 | |
298 (jka-compr-error prog args infile message err-file)) | |
299 | |
300 (jka-compr-delete-temp-file err-file))) | |
301 | |
302 (or (zerop | |
303 (apply 'call-process | |
304 prog | |
305 infile | |
306 (if (stringp output) temp output) | |
307 nil | |
308 args)) | |
309 (jka-compr-error prog args infile message)) | |
310 | |
311 (and (stringp output) | |
312 (let ((cbuf (current-buffer))) | |
313 (set-buffer temp) | |
314 (write-region (point-min) (point-max) output) | |
315 (erase-buffer) | |
316 (set-buffer cbuf))))) | |
317 | |
318 | |
319 ;;; Support for temp files. Much of this was inspired if not lifted | |
320 ;;; from ange-ftp. | |
321 | |
322 (defvar jka-compr-temp-name-template | |
323 "/tmp/jka-com" | |
324 "Prefix added to all temp files created by jka-compr. | |
7066 | 325 There should be no more than seven characters after the final `/'") |
6002 | 326 |
327 (defvar jka-compr-temp-name-table (make-vector 31 nil)) | |
328 | |
329 (defun jka-compr-make-temp-name (&optional local-copy) | |
330 "This routine will return the name of a new file." | |
331 (let* ((lastchar ?a) | |
332 (prevchar ?a) | |
333 (template (concat jka-compr-temp-name-template "aa")) | |
334 (lastpos (1- (length template))) | |
335 (not-done t) | |
336 file | |
337 entry) | |
338 | |
339 (while not-done | |
340 (aset template lastpos lastchar) | |
341 (setq file (concat (make-temp-name template) "#")) | |
342 (setq entry (intern file jka-compr-temp-name-table)) | |
343 (if (or (get entry 'active) | |
344 (file-exists-p file)) | |
345 | |
346 (progn | |
347 (setq lastchar (1+ lastchar)) | |
348 (if (> lastchar ?z) | |
349 (progn | |
350 (setq prevchar (1+ prevchar)) | |
351 (setq lastchar ?a) | |
352 (if (> prevchar ?z) | |
353 (error "Can't allocate temp file.") | |
354 (aset template (1- lastpos) prevchar))))) | |
355 | |
356 (put entry 'active (not local-copy)) | |
357 (setq not-done nil))) | |
358 | |
359 file)) | |
360 | |
361 | |
362 (defun jka-compr-delete-temp-file (temp) | |
363 | |
364 (put (intern temp jka-compr-temp-name-table) | |
365 'active nil) | |
366 | |
367 (condition-case () | |
368 (delete-file temp) | |
369 (error nil))) | |
370 | |
371 | |
372 (defun jka-compr-write-region (start end file &optional append visit) | |
373 (let* ((filename (expand-file-name file)) | |
374 (visit-file (if (stringp visit) (expand-file-name visit) filename)) | |
375 (info (jka-compr-get-compression-info visit-file))) | |
376 | |
377 (if info | |
378 | |
379 (let ((can-append (jka-compr-info-can-append info)) | |
380 (compress-program (jka-compr-info-compress-program info)) | |
381 (compress-message (jka-compr-info-compress-message info)) | |
382 (uncompress-program (jka-compr-info-uncompress-program info)) | |
383 (uncompress-message (jka-compr-info-uncompress-message info)) | |
384 (compress-args (jka-compr-info-compress-args info)) | |
385 (uncompress-args (jka-compr-info-uncompress-args info)) | |
386 (temp-file (jka-compr-make-temp-name)) | |
387 (base-name (file-name-nondirectory visit-file)) | |
388 cbuf temp-buffer) | |
389 | |
390 (setq cbuf (current-buffer) | |
391 temp-buffer (get-buffer-create " *jka-compr-temp*")) | |
392 (set-buffer temp-buffer) | |
393 (widen) (erase-buffer) | |
394 (set-buffer cbuf) | |
395 | |
396 (and append | |
397 (not can-append) | |
398 (file-exists-p filename) | |
399 (let* ((local-copy (file-local-copy filename)) | |
400 (local-file (or local-copy filename))) | |
401 | |
402 (unwind-protect | |
403 | |
404 (progn | |
405 | |
406 (and | |
407 uncompress-message | |
408 (message "%s %s..." uncompress-message base-name)) | |
409 | |
410 (jka-compr-call-process uncompress-program | |
411 (concat uncompress-message | |
412 " " base-name) | |
413 local-file | |
414 temp-file | |
415 temp-buffer | |
416 uncompress-args) | |
417 (and | |
418 uncompress-message | |
419 (message "%s %s...done" uncompress-message base-name))) | |
420 | |
421 (and | |
422 local-copy | |
423 (file-exists-p local-copy) | |
424 (delete-file local-copy))))) | |
425 | |
426 (and | |
427 compress-message | |
428 (message "%s %s..." compress-message base-name)) | |
429 | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
430 (jka-compr-run-real-handler 'write-region |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
431 (list start end temp-file t 'dont)) |
6002 | 432 |
433 (jka-compr-call-process compress-program | |
434 (concat compress-message | |
435 " " base-name) | |
436 temp-file | |
437 temp-buffer | |
438 nil | |
439 compress-args) | |
440 | |
441 (set-buffer temp-buffer) | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
442 (jka-compr-run-real-handler 'write-region |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
443 (list (point-min) (point-max) |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
444 filename |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
445 (and append can-append) 'dont)) |
6002 | 446 (erase-buffer) |
447 (set-buffer cbuf) | |
448 | |
449 (jka-compr-delete-temp-file temp-file) | |
450 | |
451 (and | |
452 compress-message | |
453 (message "%s %s...done" compress-message base-name)) | |
454 | |
455 (cond | |
456 ((eq visit t) | |
457 (setq buffer-file-name filename) | |
458 (set-visited-file-modtime)) | |
459 ((stringp visit) | |
460 (setq buffer-file-name visit) | |
461 (let ((buffer-file-name filename)) | |
462 (set-visited-file-modtime)))) | |
463 | |
464 (and (or (eq visit t) | |
465 (eq visit nil) | |
466 (stringp visit)) | |
467 (message "Wrote %s" visit-file)) | |
468 | |
469 nil) | |
470 | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
471 (jka-compr-run-real-handler 'write-region |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
472 (list start end filename append visit))))) |
6002 | 473 |
474 | |
6218
f4d5338c2142
(jka-compr-insert-file-contents): Handle 5th arg.
Richard M. Stallman <rms@gnu.org>
parents:
6041
diff
changeset
|
475 (defun jka-compr-insert-file-contents (file &optional visit beg end replace) |
6002 | 476 (barf-if-buffer-read-only) |
477 | |
478 (and (or beg end) | |
479 visit | |
480 (error "Attempt to visit less than an entire file")) | |
481 | |
482 (let* ((filename (expand-file-name file)) | |
483 (info (jka-compr-get-compression-info filename))) | |
484 | |
485 (if info | |
486 | |
487 (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
488 (uncompress-program (jka-compr-info-uncompress-program info)) | |
489 (uncompress-args (jka-compr-info-uncompress-args info)) | |
490 (base-name (file-name-nondirectory filename)) | |
491 (notfound nil) | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
492 (local-copy |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
493 (jka-compr-run-real-handler 'file-local-copy (list filename))) |
6002 | 494 local-file |
495 size start) | |
496 | |
497 (setq local-file (or local-copy filename)) | |
498 | |
499 (and | |
500 visit | |
501 (setq buffer-file-name filename)) | |
502 | |
503 (unwind-protect ; to make sure local-copy gets deleted | |
504 | |
505 (progn | |
506 | |
507 (and | |
508 uncompress-message | |
509 (message "%s %s..." uncompress-message base-name)) | |
510 | |
511 (condition-case error-code | |
512 | |
513 (progn | |
8774
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
514 (if replace |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
515 (goto-char (point-min))) |
6002 | 516 (setq start (point)) |
517 (if (or beg end) | |
518 (jka-compr-partial-uncompress uncompress-program | |
519 (concat uncompress-message | |
520 " " base-name) | |
521 uncompress-args | |
522 local-file | |
523 (or beg 0) | |
524 (if (and beg end) | |
525 (- end beg) | |
526 end)) | |
527 (jka-compr-call-process uncompress-program | |
528 (concat uncompress-message | |
529 " " base-name) | |
530 local-file | |
531 t | |
532 nil | |
533 uncompress-args)) | |
534 (setq size (- (point) start)) | |
8774
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
535 (if replace |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
536 (let* ((del-beg (point)) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
537 (del-end (+ del-beg size))) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
538 (delete-region del-beg |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
539 (min del-end (point-max))))) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
540 (goto-char start)) |
6002 | 541 (error |
542 (if (and (eq (car error-code) 'file-error) | |
543 (eq (nth 3 error-code) local-file)) | |
544 (if visit | |
545 (setq notfound error-code) | |
546 (signal 'file-error | |
547 (cons "Opening input file" | |
548 (nthcdr 2 error-code)))) | |
549 (signal (car error-code) (cdr error-code)))))) | |
550 | |
551 (and | |
552 local-copy | |
553 (file-exists-p local-copy) | |
554 (delete-file local-copy))) | |
555 | |
556 (and | |
557 visit | |
558 (progn | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
559 (unlock-buffer) |
6002 | 560 (setq buffer-file-name filename) |
561 (set-visited-file-modtime))) | |
562 | |
563 (and | |
564 uncompress-message | |
565 (message "%s %s...done" uncompress-message base-name)) | |
566 | |
567 (and | |
568 visit | |
569 notfound | |
570 (signal 'file-error | |
571 (cons "Opening input file" (nth 2 notfound)))) | |
572 | |
8774
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
573 ;; Run the functions that insert-file-contents would. |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
574 (let ((p after-insert-file-functions) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
575 (insval size)) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
576 (while p |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
577 (setq insval (funcall (car p) size)) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
578 (if insval |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
579 (progn |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
580 (or (integerp insval) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
581 (signal 'wrong-type-argument |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
582 (list 'integerp insval))) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
583 (setq size insval))) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
584 (setq p (cdr p)))) |
ade4b542a74c
(jka-compr-compression-info-list): Expand regular
Richard M. Stallman <rms@gnu.org>
parents:
8593
diff
changeset
|
585 |
6002 | 586 (list filename size)) |
587 | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
588 (jka-compr-run-real-handler 'insert-file-contents |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
589 (list file visit beg end replace))))) |
6002 | 590 |
591 | |
592 (defun jka-compr-file-local-copy (file) | |
593 (let* ((filename (expand-file-name file)) | |
594 (info (jka-compr-get-compression-info filename))) | |
595 | |
596 (if info | |
597 | |
598 (let ((uncompress-message (jka-compr-info-uncompress-message info)) | |
599 (uncompress-program (jka-compr-info-uncompress-program info)) | |
600 (uncompress-args (jka-compr-info-uncompress-args info)) | |
601 (base-name (file-name-nondirectory filename)) | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
602 (local-copy |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
603 (jka-compr-run-real-handler 'file-local-copy (list filename))) |
6002 | 604 (temp-file (jka-compr-make-temp-name t)) |
605 (temp-buffer (get-buffer-create " *jka-compr-temp*")) | |
606 (notfound nil) | |
607 (cbuf (current-buffer)) | |
608 local-file) | |
609 | |
610 (setq local-file (or local-copy filename)) | |
611 | |
612 (unwind-protect | |
613 | |
614 (progn | |
615 | |
616 (and | |
617 uncompress-message | |
618 (message "%s %s..." uncompress-message base-name)) | |
619 | |
620 (set-buffer temp-buffer) | |
621 | |
622 (jka-compr-call-process uncompress-program | |
623 (concat uncompress-message | |
624 " " base-name) | |
625 local-file | |
626 t | |
627 nil | |
628 uncompress-args) | |
629 | |
630 (and | |
631 uncompress-message | |
632 (message "%s %s...done" uncompress-message base-name)) | |
633 | |
634 (write-region | |
635 (point-min) (point-max) temp-file nil 'dont)) | |
636 | |
637 (and | |
638 local-copy | |
639 (file-exists-p local-copy) | |
640 (delete-file local-copy)) | |
641 | |
642 (set-buffer cbuf) | |
643 (kill-buffer temp-buffer)) | |
644 | |
645 temp-file) | |
646 | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
647 (jka-compr-run-real-handler 'file-local-copy (list filename))))) |
6002 | 648 |
649 | |
650 ;;; Support for loading compressed files. | |
651 (defun jka-compr-load (file &optional noerror nomessage nosuffix) | |
652 "Documented as original." | |
653 | |
654 (let* ((local-copy (jka-compr-file-local-copy file)) | |
655 (load-file (or local-copy file))) | |
656 | |
657 (unwind-protect | |
658 | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
659 (let (inhibit-file-name-operation |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
660 inhibit-file-name-handlers) |
6002 | 661 (or nomessage |
662 (message "Loading %s..." file)) | |
663 | |
664 (load load-file noerror t t) | |
665 | |
666 (or nomessage | |
667 (message "Loading %s...done." file))) | |
668 | |
669 (jka-compr-delete-temp-file local-copy)) | |
670 | |
671 t)) | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
672 |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
673 (put 'write-region 'jka-compr 'jka-compr-write-region) |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
674 (put 'insert-file-contents 'jka-compr 'jka-compr-insert-file-contents) |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
675 (put 'file-local-copy 'jka-compr 'jka-compr-file-local-copy) |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
676 (put 'load 'jka-compr 'jka-compr-load) |
6002 | 677 |
678 (defun jka-compr-handler (operation &rest args) | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
679 (save-match-data |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
680 (let ((jka-op (get operation 'jka-compr))) |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
681 (if jka-op |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
682 (apply jka-op args) |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
683 (jka-compr-run-real-handler operation args))))) |
6002 | 684 |
7066 | 685 ;; If we are given an operation that we don't handle, |
686 ;; call the Emacs primitive for that operation, | |
687 ;; and manipulate the inhibit variables | |
688 ;; to prevent the primitive from calling our handler again. | |
689 (defun jka-compr-run-real-handler (operation args) | |
690 (let ((inhibit-file-name-handlers | |
691 (cons 'jka-compr-handler | |
692 (and (eq inhibit-file-name-operation operation) | |
693 inhibit-file-name-handlers))) | |
694 (inhibit-file-name-operation operation)) | |
695 (apply operation args))) | |
696 | |
6002 | 697 (defun toggle-auto-compression (arg) |
698 "Toggle automatic file compression and decompression. | |
699 With prefix argument ARG, turn auto compression on if positive, else off. | |
700 Returns the new status of auto compression (non-nil means on)." | |
701 (interactive "P") | |
702 (let* ((installed (jka-compr-installed-p)) | |
703 (flag (if (null arg) | |
704 (not installed) | |
705 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))))) | |
706 | |
707 (cond | |
708 ((and flag installed) t) ; already installed | |
709 | |
710 ((and (not flag) (not installed)) nil) ; already not installed | |
711 | |
712 (flag | |
713 (jka-compr-install)) | |
714 | |
715 (t | |
716 (jka-compr-uninstall))) | |
717 | |
718 | |
719 (and (interactive-p) | |
720 (if flag | |
721 (message "Automatic file (de)compression is now ON.") | |
722 (message "Automatic file (de)compression is now OFF."))) | |
723 | |
724 flag)) | |
725 | |
726 | |
727 (defun jka-compr-build-file-regexp () | |
728 (concat | |
729 "\\(" | |
730 (mapconcat | |
731 'jka-compr-info-regexp | |
732 jka-compr-compression-info-list | |
733 "\\)\\|\\(") | |
734 "\\)")) | |
735 | |
736 | |
737 (defun jka-compr-install () | |
738 "Install jka-compr. | |
7066 | 739 This adds entries to `file-name-handler-alist' and `auto-mode-alist'." |
6002 | 740 |
741 (setq jka-compr-file-name-handler-entry | |
742 (cons (jka-compr-build-file-regexp) 'jka-compr-handler)) | |
743 | |
744 (setq file-name-handler-alist (cons jka-compr-file-name-handler-entry | |
745 file-name-handler-alist)) | |
746 | |
7976
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
747 ;; Make entries in auto-mode-alist so that modes are chosen right |
b72b84744a85
Use plists to record handlers.
Richard M. Stallman <rms@gnu.org>
parents:
7617
diff
changeset
|
748 ;; according to the file names sans `.gz'. |
6002 | 749 (mapcar |
750 (function (lambda (x) | |
751 (and | |
752 (jka-compr-info-strip-extension x) | |
753 (setq auto-mode-alist (cons (list (jka-compr-info-regexp x) | |
754 nil 'jka-compr) | |
755 auto-mode-alist))))) | |
756 | |
757 jka-compr-compression-info-list)) | |
758 | |
759 | |
760 (defun jka-compr-uninstall () | |
761 "Uninstall jka-compr. | |
7066 | 762 This removes the entries in `file-name-handler-alist' and `auto-mode-alist' |
763 that were created by `jka-compr-installed'." | |
6002 | 764 |
765 (let* ((fnha (cons nil file-name-handler-alist)) | |
766 (last fnha)) | |
767 | |
768 (while (cdr last) | |
769 (if (eq (cdr (car (cdr last))) 'jka-compr-handler) | |
770 (setcdr last (cdr (cdr last))) | |
771 (setq last (cdr last)))) | |
772 | |
773 (setq file-name-handler-alist (cdr fnha))) | |
774 | |
775 (let* ((ama (cons nil auto-mode-alist)) | |
776 (last ama) | |
777 entry) | |
778 | |
779 (while (cdr last) | |
780 (setq entry (car (cdr last))) | |
781 (if (and (consp (cdr entry)) | |
782 (eq (nth 2 entry) 'jka-compr)) | |
783 (setcdr last (cdr (cdr last))) | |
784 (setq last (cdr last)))) | |
785 | |
786 (setq auto-mode-alist (cdr ama)))) | |
787 | |
788 | |
789 (defun jka-compr-installed-p () | |
790 "Return non-nil if jka-compr is installed. | |
7066 | 791 The return value is the entry in `file-name-handler-alist' for jka-compr." |
6002 | 792 |
793 (let ((fnha file-name-handler-alist) | |
794 (installed nil)) | |
795 | |
796 (while (and fnha (not installed)) | |
797 (and (eq (cdr (car fnha)) 'jka-compr-handler) | |
798 (setq installed (car fnha))) | |
799 (setq fnha (cdr fnha))) | |
800 | |
801 installed)) | |
802 | |
803 | |
804 ;;; Add the file I/O hook if it does not already exist. | |
805 ;;; Make sure that jka-compr-file-name-handler-entry is eq to the | |
806 ;;; entry for jka-compr in file-name-handler-alist. | |
807 (and (jka-compr-installed-p) | |
808 (jka-compr-uninstall)) | |
809 | |
810 (jka-compr-install) | |
811 | |
812 | |
813 (provide 'jka-compr) | |
814 | |
815 ;; jka-compr.el ends here. |