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