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