]> code.delx.au - gnu-emacs/blob - lisp/arc-mode.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / arc-mode.el
1 ;;; arc-mode.el --- simple editing of archives
2
3 ;; Copyright (C) 1995, 1997-1998, 2001-2011 Free Software Foundation, Inc.
4
5 ;; Author: Morten Welinder <terra@gnu.org>
6 ;; Keywords: files archives msdog editing major-mode
7 ;; Favourite-brand-of-beer: None, I hate beer.
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; NAMING: "arc" is short for "archive" and does not refer specifically
27 ;; to files whose name end in ".arc"
28 ;;
29 ;; This code does not decode any files internally, although it does
30 ;; understand the directory level of the archives. For this reason,
31 ;; you should expect this code to need more fiddling than tar-mode.el
32 ;; (although it at present has fewer bugs :-) In particular, I have
33 ;; not tested this under Ms-Dog myself.
34 ;; -------------------------------------
35 ;; INTERACTION: arc-mode.el should play together with
36 ;;
37 ;; * ange-ftp.el: Remote archives (i.e., ones that ange-ftp has brought
38 ;; to you) are handled by doing all updates on a local
39 ;; copy. When you make changes to a remote file the
40 ;; changes will first take effect when the archive buffer
41 ;; is saved. You will be warned about this.
42 ;;
43 ;; * dos-fns.el: (Part of Emacs 19). You get automatic ^M^J <--> ^J
44 ;; conversion.
45 ;;
46 ;; arc-mode.el does not work well with crypt++.el; for the archives as
47 ;; such this could be fixed (but wouldn't be useful) by declaring such
48 ;; archives to be "remote". For the members this is a general Emacs
49 ;; problem that 19.29's file formats may fix.
50 ;; -------------------------------------
51 ;; ARCHIVE TYPES: Currently only the archives below are handled, but the
52 ;; structure for handling just about anything is in place.
53 ;;
54 ;; Arc Lzh Zip Zoo Rar 7z
55 ;; --------------------------------------------
56 ;; View listing Intern Intern Intern Intern Y Y
57 ;; Extract member Y Y Y Y Y Y
58 ;; Save changed member Y Y Y Y N N
59 ;; Add new member N N N N N N
60 ;; Delete member Y Y Y Y N N
61 ;; Rename member Y Y N N N N
62 ;; Chmod - Y Y - N N
63 ;; Chown - Y - - N N
64 ;; Chgrp - Y - - N N
65 ;;
66 ;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
67 ;; on the first released version of this package.
68 ;;
69 ;; This code is partly based on tar-mode.el from Emacs.
70 ;; -------------------------------------
71 ;; ARCHIVE STRUCTURES:
72 ;; (This is mostly for myself.)
73 ;;
74 ;; ARC A series of (header,file). No interactions among members.
75 ;;
76 ;; LZH A series of (header,file). Headers are checksummed. No
77 ;; interaction among members.
78 ;; Headers come in three flavours called level 0, 1 and 2 headers.
79 ;; Level 2 header is free of DOS specific restrictions and most
80 ;; prevalently used. Also level 1 and 2 headers consist of base
81 ;; and extension headers. For more details see
82 ;; http://homepage1.nifty.com/dangan/en/Content/Program/Java/jLHA/Notes/Notes.html
83 ;; http://www.osirusoft.com/joejared/lzhformat.html
84 ;;
85 ;; ZIP A series of (lheader,fil) followed by a "central directory"
86 ;; which is a series of (cheader) followed by an end-of-
87 ;; central-dir record possibly followed by junk. The e-o-c-d
88 ;; links to c-d. cheaders link to lheaders which are basically
89 ;; cut-down versions of the cheaders.
90 ;;
91 ;; ZOO An archive header followed by a series of (header,file).
92 ;; Each member header points to the next. The archive is
93 ;; terminated by a bogus header with a zero next link.
94 ;; -------------------------------------
95 ;; HOOKS: `foo' means one of the supported archive types.
96 ;;
97 ;; archive-mode-hook
98 ;; archive-foo-mode-hook
99 ;; archive-extract-hooks
100
101 ;;; Code:
102
103 ;; -------------------------------------------------------------------------
104 ;;; Section: Configuration.
105
106 (defgroup archive nil
107 "Simple editing of archives."
108 :group 'data)
109
110 (defgroup archive-arc nil
111 "ARC-specific options to archive."
112 :group 'archive)
113
114 (defgroup archive-lzh nil
115 "LZH-specific options to archive."
116 :group 'archive)
117
118 (defgroup archive-zip nil
119 "ZIP-specific options to archive."
120 :group 'archive)
121
122 (defgroup archive-zoo nil
123 "ZOO-specific options to archive."
124 :group 'archive)
125
126 (defcustom archive-tmpdir
127 ;; make-temp-name is safe here because we use this name
128 ;; to create a directory.
129 (make-temp-name
130 (expand-file-name (if (eq system-type 'ms-dos) "ar" "archive.tmp")
131 temporary-file-directory))
132 "Directory for temporary files made by `arc-mode.el'."
133 :type 'directory
134 :group 'archive)
135
136 (defcustom archive-remote-regexp "^/[^/:]*[^/:.]:"
137 "Regexp recognizing archive files names that are not local.
138 A non-local file is one whose file name is not proper outside Emacs.
139 A local copy of the archive will be used when updating."
140 :type 'regexp
141 :group 'archive)
142
143 (defcustom archive-extract-hooks nil
144 "Hooks to run when an archive member has been extracted."
145 :type 'hook
146 :group 'archive)
147 ;; ------------------------------
148 ;; Arc archive configuration
149
150 ;; We always go via a local file since there seems to be no reliable way
151 ;; to extract to stdout without junk getting added.
152 (defcustom archive-arc-extract
153 '("arc" "x")
154 "Program and its options to run in order to extract an arc file member.
155 Extraction should happen to the current directory. Archive and member
156 name will be added."
157 :type '(list (string :tag "Program")
158 (repeat :tag "Options"
159 :inline t
160 (string :format "%v")))
161 :group 'archive-arc)
162
163 (defcustom archive-arc-expunge
164 '("arc" "d")
165 "Program and its options to run in order to delete arc file members.
166 Archive and member names will be added."
167 :type '(list (string :tag "Program")
168 (repeat :tag "Options"
169 :inline t
170 (string :format "%v")))
171 :group 'archive-arc)
172
173 (defcustom archive-arc-write-file-member
174 '("arc" "u")
175 "Program and its options to run in order to update an arc file member.
176 Archive and member name will be added."
177 :type '(list (string :tag "Program")
178 (repeat :tag "Options"
179 :inline t
180 (string :format "%v")))
181 :group 'archive-arc)
182 ;; ------------------------------
183 ;; Lzh archive configuration
184
185 (defcustom archive-lzh-extract
186 '("lha" "pq")
187 "Program and its options to run in order to extract an lzh file member.
188 Extraction should happen to standard output. Archive and member name will
189 be added."
190 :type '(list (string :tag "Program")
191 (repeat :tag "Options"
192 :inline t
193 (string :format "%v")))
194 :group 'archive-lzh)
195
196 (defcustom archive-lzh-expunge
197 '("lha" "d")
198 "Program and its options to run in order to delete lzh file members.
199 Archive and member names will be added."
200 :type '(list (string :tag "Program")
201 (repeat :tag "Options"
202 :inline t
203 (string :format "%v")))
204 :group 'archive-lzh)
205
206 (defcustom archive-lzh-write-file-member
207 '("lha" "a")
208 "Program and its options to run in order to update an lzh file member.
209 Archive and member name will be added."
210 :type '(list (string :tag "Program")
211 (repeat :tag "Options"
212 :inline t
213 (string :format "%v")))
214 :group 'archive-lzh)
215 ;; ------------------------------
216 ;; Zip archive configuration
217
218 (defcustom archive-zip-extract
219 (cond ((executable-find "unzip") '("unzip" "-qq" "-c"))
220 ((executable-find "7z") '("7z" "x" "-so"))
221 ((executable-find "pkunzip") '("pkunzip" "-e" "-o-"))
222 (t '("unzip" "-qq" "-c")))
223 "Program and its options to run in order to extract a zip file member.
224 Extraction should happen to standard output. Archive and member name will
225 be added."
226 :type '(list (string :tag "Program")
227 (repeat :tag "Options"
228 :inline t
229 (string :format "%v")))
230 :group 'archive-zip)
231
232 ;; For several reasons the latter behavior is not desirable in general.
233 ;; (1) It uses more disk space. (2) Error checking is worse or non-
234 ;; existent. (3) It tends to do funny things with other systems' file
235 ;; names.
236
237 (defcustom archive-zip-expunge
238 (if (and (not (executable-find "zip"))
239 (executable-find "pkzip"))
240 '("pkzip" "-d")
241 '("zip" "-d" "-q"))
242 "Program and its options to run in order to delete zip file members.
243 Archive and member names will be added."
244 :type '(list (string :tag "Program")
245 (repeat :tag "Options"
246 :inline t
247 (string :format "%v")))
248 :group 'archive-zip)
249
250 (defcustom archive-zip-update
251 (if (and (not (executable-find "zip"))
252 (executable-find "pkzip"))
253 '("pkzip" "-u" "-P")
254 '("zip" "-q"))
255 "Program and its options to run in order to update a zip file member.
256 Options should ensure that specified directory will be put into the zip
257 file. Archive and member name will be added."
258 :type '(list (string :tag "Program")
259 (repeat :tag "Options"
260 :inline t
261 (string :format "%v")))
262 :group 'archive-zip)
263
264 (defcustom archive-zip-update-case
265 (if (and (not (executable-find "zip"))
266 (executable-find "pkzip"))
267 '("pkzip" "-u" "-P")
268 '("zip" "-q" "-k"))
269 "Program and its options to run in order to update a case fiddled zip member.
270 Options should ensure that specified directory will be put into the zip file.
271 Archive and member name will be added."
272 :type '(list (string :tag "Program")
273 (repeat :tag "Options"
274 :inline t
275 (string :format "%v")))
276 :group 'archive-zip)
277
278 (defcustom archive-zip-case-fiddle t
279 "If non-nil then zip file members may be down-cased.
280 This case fiddling will only happen for members created by a system
281 that uses caseless file names."
282 :type 'boolean
283 :group 'archive-zip)
284 ;; ------------------------------
285 ;; Zoo archive configuration
286
287 (defcustom archive-zoo-extract
288 '("zoo" "xpq")
289 "Program and its options to run in order to extract a zoo file member.
290 Extraction should happen to standard output. Archive and member name will
291 be added."
292 :type '(list (string :tag "Program")
293 (repeat :tag "Options"
294 :inline t
295 (string :format "%v")))
296 :group 'archive-zoo)
297
298 (defcustom archive-zoo-expunge
299 '("zoo" "DqPP")
300 "Program and its options to run in order to delete zoo file members.
301 Archive and member names will be added."
302 :type '(list (string :tag "Program")
303 (repeat :tag "Options"
304 :inline t
305 (string :format "%v")))
306 :group 'archive-zoo)
307
308 (defcustom archive-zoo-write-file-member
309 '("zoo" "a")
310 "Program and its options to run in order to update a zoo file member.
311 Archive and member name will be added."
312 :type '(list (string :tag "Program")
313 (repeat :tag "Options"
314 :inline t
315 (string :format "%v")))
316 :group 'archive-zoo)
317 ;; ------------------------------
318 ;; 7z archive configuration
319
320 (defcustom archive-7z-extract
321 '("7z" "x" "-so")
322 "Program and its options to run in order to extract a 7z file member.
323 Extraction should happen to standard output. Archive and member name will
324 be added."
325 :type '(list (string :tag "Program")
326 (repeat :tag "Options"
327 :inline t
328 (string :format "%v")))
329 :group 'archive-7z)
330
331 ;; -------------------------------------------------------------------------
332 ;;; Section: Variables
333
334 (defvar archive-subtype nil "Symbol describing archive type.")
335 (defvar archive-file-list-start nil "Position of first contents line.")
336 (defvar archive-file-list-end nil "Position just after last contents line.")
337 (defvar archive-proper-file-start nil "Position of real archive's start.")
338 (defvar archive-read-only nil "Non-nil if the archive is read-only on disk.")
339 (defvar archive-local-name nil "Name of local copy of remote archive.")
340 (defvar archive-mode-map
341 (let ((map (make-keymap)))
342 (suppress-keymap map)
343 (define-key map " " 'archive-next-line)
344 (define-key map "a" 'archive-alternate-display)
345 ;;(define-key map "c" 'archive-copy)
346 (define-key map "d" 'archive-flag-deleted)
347 (define-key map "\C-d" 'archive-flag-deleted)
348 (define-key map "e" 'archive-extract)
349 (define-key map "f" 'archive-extract)
350 (define-key map "\C-m" 'archive-extract)
351 (define-key map "g" 'revert-buffer)
352 (define-key map "h" 'describe-mode)
353 (define-key map "m" 'archive-mark)
354 (define-key map "n" 'archive-next-line)
355 (define-key map "\C-n" 'archive-next-line)
356 (define-key map [down] 'archive-next-line)
357 (define-key map "o" 'archive-extract-other-window)
358 (define-key map "p" 'archive-previous-line)
359 (define-key map "q" 'quit-window)
360 (define-key map "\C-p" 'archive-previous-line)
361 (define-key map [up] 'archive-previous-line)
362 (define-key map "r" 'archive-rename-entry)
363 (define-key map "u" 'archive-unflag)
364 (define-key map "\M-\C-?" 'archive-unmark-all-files)
365 (define-key map "v" 'archive-view)
366 (define-key map "x" 'archive-expunge)
367 (define-key map "\177" 'archive-unflag-backwards)
368 (define-key map "E" 'archive-extract-other-window)
369 (define-key map "M" 'archive-chmod-entry)
370 (define-key map "G" 'archive-chgrp-entry)
371 (define-key map "O" 'archive-chown-entry)
372 ;; Let mouse-1 follow the link.
373 (define-key map [follow-link] 'mouse-face)
374
375 (if (fboundp 'command-remapping)
376 (progn
377 (define-key map [remap advertised-undo] 'archive-undo)
378 (define-key map [remap undo] 'archive-undo))
379 (substitute-key-definition 'advertised-undo 'archive-undo map global-map)
380 (substitute-key-definition 'undo 'archive-undo map global-map))
381
382 (define-key map
383 (if (featurep 'xemacs) 'button2 [mouse-2]) 'archive-extract)
384
385 (if (featurep 'xemacs)
386 () ; out of luck
387
388 (define-key map [menu-bar immediate]
389 (cons "Immediate" (make-sparse-keymap "Immediate")))
390 (define-key map [menu-bar immediate alternate]
391 '(menu-item "Alternate Display" archive-alternate-display
392 :enable (boundp (archive-name "alternate-display"))
393 :help "Toggle alternate file info display"))
394 (define-key map [menu-bar immediate view]
395 '(menu-item "View This File" archive-view
396 :help "Display file at cursor in View Mode"))
397 (define-key map [menu-bar immediate display]
398 '(menu-item "Display in Other Window" archive-display-other-window
399 :help "Display file at cursor in another window"))
400 (define-key map [menu-bar immediate find-file-other-window]
401 '(menu-item "Find in Other Window" archive-extract-other-window
402 :help "Edit file at cursor in another window"))
403 (define-key map [menu-bar immediate find-file]
404 '(menu-item "Find This File" archive-extract
405 :help "Extract file at cursor and edit it"))
406
407 (define-key map [menu-bar mark]
408 (cons "Mark" (make-sparse-keymap "Mark")))
409 (define-key map [menu-bar mark unmark-all]
410 '(menu-item "Unmark All" archive-unmark-all-files
411 :help "Unmark all marked files"))
412 (define-key map [menu-bar mark deletion]
413 '(menu-item "Flag" archive-flag-deleted
414 :help "Flag file at cursor for deletion"))
415 (define-key map [menu-bar mark unmark]
416 '(menu-item "Unflag" archive-unflag
417 :help "Unmark file at cursor"))
418 (define-key map [menu-bar mark mark]
419 '(menu-item "Mark" archive-mark
420 :help "Mark file at cursor"))
421
422 (define-key map [menu-bar operate]
423 (cons "Operate" (make-sparse-keymap "Operate")))
424 (define-key map [menu-bar operate chown]
425 '(menu-item "Change Owner..." archive-chown-entry
426 :enable (fboundp (archive-name "chown-entry"))
427 :help "Change owner of marked files"))
428 (define-key map [menu-bar operate chgrp]
429 '(menu-item "Change Group..." archive-chgrp-entry
430 :enable (fboundp (archive-name "chgrp-entry"))
431 :help "Change group ownership of marked files"))
432 (define-key map [menu-bar operate chmod]
433 '(menu-item "Change Mode..." archive-chmod-entry
434 :enable (fboundp (archive-name "chmod-entry"))
435 :help "Change mode (permissions) of marked files"))
436 (define-key map [menu-bar operate rename]
437 '(menu-item "Rename to..." archive-rename-entry
438 :enable (fboundp (archive-name "rename-entry"))
439 :help "Rename marked files"))
440 ;;(define-key map [menu-bar operate copy]
441 ;; '(menu-item "Copy to..." archive-copy))
442 (define-key map [menu-bar operate expunge]
443 '(menu-item "Expunge Marked Files" archive-expunge
444 :help "Delete all flagged files from archive"))
445 map))
446 "Local keymap for archive mode listings.")
447 (defvar archive-file-name-indent nil "Column where file names start.")
448
449 (defvar archive-remote nil "Non-nil if the archive is outside file system.")
450 (make-variable-buffer-local 'archive-remote)
451 (put 'archive-remote 'permanent-local t)
452
453 (defvar archive-member-coding-system nil "Coding-system of archive member.")
454 (make-variable-buffer-local 'archive-member-coding-system)
455
456 (defvar archive-alternate-display nil
457 "Non-nil when alternate information is shown.")
458 (make-variable-buffer-local 'archive-alternate-display)
459 (put 'archive-alternate-display 'permanent-local t)
460
461 (defvar archive-superior-buffer nil "In archive members, points to archive.")
462 (put 'archive-superior-buffer 'permanent-local t)
463
464 (defvar archive-subfile-mode nil "Non-nil in archive member buffers.")
465 (make-variable-buffer-local 'archive-subfile-mode)
466 (put 'archive-subfile-mode 'permanent-local t)
467
468 (defvar archive-file-name-coding-system nil)
469 (make-variable-buffer-local 'archive-file-name-coding-system)
470 (put 'archive-file-name-coding-system 'permanent-local t)
471
472 (defvar archive-files nil
473 "Vector of file descriptors.
474 Each descriptor is a vector of the form
475 [EXT-FILE-NAME INT-FILE-NAME CASE-FIDDLED MODE ...]")
476 (make-variable-buffer-local 'archive-files)
477
478 ;; -------------------------------------------------------------------------
479 ;;; Section: Support functions.
480
481 (eval-when-compile
482 (defsubst byte-after (pos)
483 "Like char-after but an eight-bit char is converted to unibyte."
484 (multibyte-char-to-unibyte (char-after pos)))
485 (defsubst insert-unibyte (&rest args)
486 "Like insert but don't make unibyte string and eight-bit char multibyte."
487 (dolist (elt args)
488 (if (integerp elt)
489 (insert (if (< elt 128) elt (decode-char 'eight-bit elt)))
490 (insert (string-to-multibyte elt)))))
491 )
492
493 (defsubst archive-name (suffix)
494 (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
495
496 (defun archive-l-e (str &optional len float)
497 "Convert little endian string/vector STR to integer.
498 Alternatively, STR may be a buffer position in the current buffer
499 in which case a second argument, length LEN, should be supplied.
500 FLOAT, if non-nil, means generate and return a float instead of an integer
501 \(use this for numbers that can overflow the Emacs integer)."
502 (if (stringp str)
503 (setq len (length str))
504 (setq str (buffer-substring str (+ str len))))
505 (setq str (string-as-unibyte str))
506 (let ((result 0)
507 (i 0))
508 (while (< i len)
509 (setq i (1+ i)
510 result (+ (if float (* result 256.0) (ash result 8))
511 (aref str (- len i)))))
512 result))
513
514 (defun archive-int-to-mode (mode)
515 "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------."
516 ;; FIXME: merge with tar-grind-file-mode.
517 (string
518 (if (zerop (logand 8192 mode))
519 (if (zerop (logand 16384 mode)) ?- ?d)
520 ?c) ; completeness
521 (if (zerop (logand 256 mode)) ?- ?r)
522 (if (zerop (logand 128 mode)) ?- ?w)
523 (if (zerop (logand 64 mode))
524 (if (zerop (logand 1024 mode)) ?- ?S)
525 (if (zerop (logand 1024 mode)) ?x ?s))
526 (if (zerop (logand 32 mode)) ?- ?r)
527 (if (zerop (logand 16 mode)) ?- ?w)
528 (if (zerop (logand 8 mode))
529 (if (zerop (logand 2048 mode)) ?- ?S)
530 (if (zerop (logand 2048 mode)) ?x ?s))
531 (if (zerop (logand 4 mode)) ?- ?r)
532 (if (zerop (logand 2 mode)) ?- ?w)
533 (if (zerop (logand 1 mode)) ?- ?x)))
534
535 (defun archive-calc-mode (oldmode newmode &optional error)
536 "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
537 NEWMODE may be an octal number including a leading zero in which case it
538 will become the new mode.\n
539 NEWMODE may also be a relative specification like \"og-rwx\" in which case
540 OLDMODE will be modified accordingly just like chmod(2) would have done.\n
541 If optional third argument ERROR is non-nil an error will be signaled if
542 the mode is invalid. If ERROR is nil then nil will be returned."
543 (cond ((string-match "^0[0-7]*$" newmode)
544 (let ((result 0)
545 (len (length newmode))
546 (i 1))
547 (while (< i len)
548 (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
549 i (1+ i)))
550 (logior (logand oldmode 65024) result)))
551 ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
552 (let ((who 0)
553 (result oldmode)
554 (op (aref newmode (match-beginning 2)))
555 (bits 0)
556 (i (match-beginning 3)))
557 (while (< i (match-end 3))
558 (let ((rwx (aref newmode i)))
559 (setq bits (logior bits (cond ((= rwx ?r) 292)
560 ((= rwx ?w) 146)
561 ((= rwx ?x) 73)
562 ((= rwx ?s) 3072)
563 ((= rwx ?t) 512)))
564 i (1+ i))))
565 (while (< who (match-end 1))
566 (let* ((whoc (aref newmode who))
567 (whomask (cond ((= whoc ?a) 4095)
568 ((= whoc ?u) 1472)
569 ((= whoc ?g) 2104)
570 ((= whoc ?o) 7))))
571 (if (= op ?=)
572 (setq result (logand result (lognot whomask))))
573 (if (= op ?-)
574 (setq result (logand result (lognot (logand whomask bits))))
575 (setq result (logior result (logand whomask bits)))))
576 (setq who (1+ who)))
577 result))
578 (t
579 (if error
580 (error "Invalid mode specification: %s" newmode)))))
581
582 (defun archive-dosdate (date)
583 "Stringify dos packed DATE record."
584 (let ((year (+ 1980 (logand (ash date -9) 127)))
585 (month (logand (ash date -5) 15))
586 (day (logand date 31)))
587 (if (or (> month 12) (< month 1))
588 ""
589 (format "%2d-%s-%d"
590 day
591 (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
592 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
593 year))))
594
595 (defun archive-dostime (time)
596 "Stringify dos packed TIME record."
597 (let ((hour (logand (ash time -11) 31))
598 (minute (logand (ash time -5) 63))
599 (second (* 2 (logand time 31)))) ; 2 seconds resolution
600 (format "%02d:%02d:%02d" hour minute second)))
601
602 (defun archive-unixdate (low high)
603 "Stringify Unix (LOW HIGH) date."
604 (let ((str (current-time-string (cons high low))))
605 (format "%s-%s-%s"
606 (substring str 8 10)
607 (substring str 4 7)
608 (substring str 20 24))))
609
610 (defun archive-unixtime (low high)
611 "Stringify Unix (LOW HIGH) time."
612 (let ((str (current-time-string (cons high low))))
613 (substring str 11 19)))
614
615 (defun archive-get-lineno ()
616 (if (>= (point) archive-file-list-start)
617 (count-lines archive-file-list-start
618 (line-beginning-position))
619 0))
620
621 (defun archive-get-descr (&optional noerror)
622 "Return the descriptor vector for file at point.
623 Does not signal an error if optional argument NOERROR is non-nil."
624 (let ((no (archive-get-lineno)))
625 (if (and (>= (point) archive-file-list-start)
626 (< no (length archive-files)))
627 (let ((item (aref archive-files no)))
628 (if (vectorp item)
629 item
630 (if (not noerror)
631 (error "Entry is not a regular member of the archive"))))
632 (if (not noerror)
633 (error "Line does not describe a member of the archive")))))
634 ;; -------------------------------------------------------------------------
635 ;;; Section: the mode definition
636
637 ;;;###autoload
638 (defun archive-mode (&optional force)
639 "Major mode for viewing an archive file in a dired-like way.
640 You can move around using the usual cursor motion commands.
641 Letters no longer insert themselves.
642 Type `e' to pull a file out of the archive and into its own buffer;
643 or click mouse-2 on the file's line in the archive mode buffer.
644
645 If you edit a sub-file of this archive (as with the `e' command) and
646 save it, the contents of that buffer will be saved back into the
647 archive.
648
649 \\{archive-mode-map}"
650 ;; This is not interactive because you shouldn't be turning this
651 ;; mode on and off. You can corrupt things that way.
652 (if (zerop (buffer-size))
653 ;; At present we cannot create archives from scratch
654 (funcall (or (default-value 'major-mode) 'fundamental-mode))
655 (if (and (not force) archive-files) nil
656 (let* ((type (archive-find-type))
657 (typename (capitalize (symbol-name type))))
658 (kill-all-local-variables)
659 (make-local-variable 'archive-subtype)
660 (setq archive-subtype type)
661
662 ;; Buffer contains treated image of file before the file contents
663 (make-local-variable 'revert-buffer-function)
664 (setq revert-buffer-function 'archive-mode-revert)
665 (auto-save-mode 0)
666
667 ;; Remote archives are not written by a hook.
668 (if archive-remote nil
669 (add-hook 'write-contents-functions 'archive-write-file nil t))
670
671 (make-local-variable 'require-final-newline)
672 (setq require-final-newline nil)
673 (make-local-variable 'local-enable-local-variables)
674 (setq local-enable-local-variables nil)
675
676 ;; Prevent loss of data when saving the file.
677 (make-local-variable 'file-precious-flag)
678 (setq file-precious-flag t)
679
680 (make-local-variable 'archive-read-only)
681 ;; Archives which are inside other archives and whose
682 ;; names are invalid for this OS, can't be written.
683 (setq archive-read-only
684 (or (not (file-writable-p (buffer-file-name)))
685 (and archive-subfile-mode
686 (string-match file-name-invalid-regexp
687 (aref archive-subfile-mode 0)))))
688
689 ;; Should we use a local copy when accessing from outside Emacs?
690 (make-local-variable 'archive-local-name)
691
692 ;; An archive can contain another archive whose name is invalid
693 ;; on local filesystem. Treat such archives as remote.
694 (or archive-remote
695 (setq archive-remote
696 (or (string-match archive-remote-regexp (buffer-file-name))
697 (string-match file-name-invalid-regexp
698 (buffer-file-name)))))
699
700 (setq major-mode 'archive-mode)
701 (setq mode-name (concat typename "-Archive"))
702 ;; Run archive-foo-mode-hook and archive-mode-hook
703 (run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
704 (use-local-map archive-mode-map))
705
706 (make-local-variable 'archive-proper-file-start)
707 (make-local-variable 'archive-file-list-start)
708 (make-local-variable 'archive-file-list-end)
709 (make-local-variable 'archive-file-name-indent)
710 (setq archive-file-name-coding-system
711 (or file-name-coding-system
712 default-file-name-coding-system
713 locale-coding-system))
714 (if (default-value 'enable-multibyte-characters)
715 (set-buffer-multibyte 'to))
716 (archive-summarize nil)
717 (setq buffer-read-only t))))
718
719 ;; Archive mode is suitable only for specially formatted data.
720 (put 'archive-mode 'mode-class 'special)
721
722 (let ((item1 '(archive-subfile-mode " Archive")))
723 (or (member item1 minor-mode-alist)
724 (setq minor-mode-alist (cons item1 minor-mode-alist))))
725 ;; -------------------------------------------------------------------------
726 (defun archive-find-type ()
727 (widen)
728 (goto-char (point-min))
729 ;; The funny [] here make it unlikely that the .elc file will be treated
730 ;; as an archive by other software.
731 (let (case-fold-search)
732 (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
733 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
734 ((looking-at "....................[\334]\247\304\375") 'zoo)
735 ((and (looking-at "\C-z") ; signature too simple, IMHO
736 (string-match "\\.[aA][rR][cC]$"
737 (or buffer-file-name (buffer-name))))
738 'arc)
739 ;; This pattern modeled on the BSD/GNU+Linux `file' command.
740 ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
741 ;; Note this regexp is also in archive-exe-p.
742 ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
743 ((looking-at "Rar!") 'rar)
744 ((looking-at "!<arch>\n") 'ar)
745 ((and (looking-at "MZ")
746 (re-search-forward "Rar!" (+ (point) 100000) t))
747 'rar-exe)
748 ((looking-at "7z\274\257\047\034") '7z)
749 (t (error "Buffer format not recognized")))))
750 ;; -------------------------------------------------------------------------
751
752 (defun archive-desummarize ()
753 (let ((inhibit-read-only t)
754 (modified (buffer-modified-p)))
755 (widen)
756 (delete-region (point-min) archive-proper-file-start)
757 (restore-buffer-modified-p modified)))
758
759
760 (defun archive-summarize (&optional shut-up)
761 "Parse the contents of the archive file in the current buffer.
762 Place a dired-like listing on the front;
763 then narrow to it, so that only that listing
764 is visible (and the real data of the buffer is hidden).
765 Optional argument SHUT-UP, if non-nil, means don't print messages
766 when parsing the archive."
767 (widen)
768 (let ((inhibit-read-only t))
769 (setq archive-proper-file-start (copy-marker (point-min) t))
770 (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
771 (or shut-up
772 (message "Parsing archive file..."))
773 (buffer-disable-undo (current-buffer))
774 (setq archive-files (funcall (archive-name "summarize")))
775 (or shut-up
776 (message "Parsing archive file...done."))
777 (setq archive-proper-file-start (point-marker))
778 (narrow-to-region (point-min) (point))
779 (set-buffer-modified-p nil)
780 (buffer-enable-undo))
781 (goto-char archive-file-list-start)
782 (archive-next-line 0))
783
784 (defun archive-resummarize ()
785 "Recreate the contents listing of an archive."
786 (let ((no (archive-get-lineno)))
787 (archive-desummarize)
788 (archive-summarize t)
789 (goto-char archive-file-list-start)
790 (archive-next-line no)))
791
792 (defun archive-summarize-files (files)
793 "Insert a description of a list of files annotated with proper mouse face."
794 (setq archive-file-list-start (point-marker))
795 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
796 ;; We don't want to do an insert for each element since that takes too
797 ;; long when the archive -- which has to be moved in memory -- is large.
798 (insert
799 (apply
800 (function concat)
801 (mapcar
802 (lambda (fil)
803 ;; Using `concat' here copies the text also, so we can add
804 ;; properties without problems.
805 (let ((text (concat (aref fil 0) "\n")))
806 (if (featurep 'xemacs)
807 () ; out of luck
808 (add-text-properties
809 (aref fil 1) (aref fil 2)
810 '(mouse-face highlight
811 help-echo "mouse-2: extract this file into a buffer")
812 text))
813 text))
814 files)))
815 (setq archive-file-list-end (point-marker)))
816
817 (defun archive-alternate-display ()
818 "Toggle alternative display.
819 To avoid very long lines archive mode does not show all information.
820 This function changes the set of information shown for each files."
821 (interactive)
822 (setq archive-alternate-display (not archive-alternate-display))
823 (archive-resummarize))
824 ;; -------------------------------------------------------------------------
825 ;;; Section: Local archive copy handling
826
827 (defun archive-unique-fname (fname dir)
828 "Make sure a file FNAME can be created uniquely in directory DIR.
829
830 If FNAME can be uniquely created in DIR, it is returned unaltered.
831 If FNAME is something our underlying filesystem can't grok, or if another
832 file by that name already exists in DIR, a unique new name is generated
833 using `make-temp-file', and the generated name is returned."
834 (let ((fullname (expand-file-name fname dir))
835 (alien (string-match file-name-invalid-regexp fname))
836 (tmpfile
837 (expand-file-name
838 (if (if (fboundp 'msdos-long-file-names)
839 (not (msdos-long-file-names)))
840 "am"
841 "arc-mode.")
842 dir)))
843 (if (or alien (file-exists-p fullname))
844 (progn
845 ;; Maked sure all the leading directories in
846 ;; archive-local-name exist under archive-tmpdir, so that
847 ;; the directory structure recorded in the archive is
848 ;; reconstructed in the temporary directory.
849 (make-directory (file-name-directory tmpfile) t)
850 (make-temp-file tmpfile))
851 ;; Maked sure all the leading directories in `fullname' exist
852 ;; under archive-tmpdir. This is necessary for nested archives
853 ;; (`archive-extract' sets `archive-remote' to t in case
854 ;; an archive occurs inside another archive).
855 (make-directory (file-name-directory fullname) t)
856 fullname)))
857
858 (defun archive-maybe-copy (archive)
859 (let ((coding-system-for-write 'no-conversion))
860 (if archive-remote
861 (let ((start (point-max))
862 ;; Sometimes ARCHIVE is invalid while its actual name, as
863 ;; recorded in its parent archive, is not. For example, an
864 ;; archive bar.zip inside another archive foo.zip gets a name
865 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
866 ;; So use the actual name if available.
867 (archive-name
868 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
869 archive)))
870 (setq archive-local-name
871 (archive-unique-fname archive-name archive-tmpdir))
872 (save-restriction
873 (widen)
874 (write-region start (point-max) archive-local-name nil 'nomessage))
875 archive-local-name)
876 (if (buffer-modified-p) (save-buffer))
877 archive)))
878
879 (defun archive-maybe-update (unchanged)
880 (if archive-remote
881 (let ((name archive-local-name)
882 (modified (buffer-modified-p))
883 (coding-system-for-read 'no-conversion)
884 (lno (archive-get-lineno))
885 (inhibit-read-only t))
886 (if unchanged nil
887 (setq archive-files nil)
888 (erase-buffer)
889 (insert-file-contents name)
890 (archive-mode t)
891 (goto-char archive-file-list-start)
892 (archive-next-line lno))
893 (archive-delete-local name)
894 (if (not unchanged)
895 (message
896 "Buffer `%s' must be saved for changes to take effect"
897 (buffer-name (current-buffer))))
898 (set-buffer-modified-p (or modified (not unchanged))))))
899
900 (defun archive-delete-local (name)
901 "Delete file NAME and its parents up to and including `archive-tmpdir'."
902 (let ((again t)
903 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
904 (condition-case nil
905 (delete-file name)
906 (error nil))
907 (while again
908 (setq name (directory-file-name (file-name-directory name)))
909 (condition-case nil
910 (delete-directory name)
911 (error nil))
912 (if (string= name top) (setq again nil)))))
913 ;; -------------------------------------------------------------------------
914 ;;; Section: Member extraction
915
916 (defun archive-try-jka-compr ()
917 (when (and auto-compression-mode
918 (jka-compr-get-compression-info buffer-file-name))
919 (let* ((basename (file-name-nondirectory buffer-file-name))
920 (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
921 (match-string 1 basename) basename))
922 (tmpfile (make-temp-file (file-name-sans-extension tmpname)
923 nil
924 (file-name-extension tmpname 'period))))
925 (unwind-protect
926 (progn
927 (let ((coding-system-for-write 'no-conversion)
928 ;; Don't re-compress this data just before decompressing it.
929 (jka-compr-inhibit t))
930 (write-region (point-min) (point-max) tmpfile nil 'quiet))
931 (erase-buffer)
932 (let ((coding-system-for-read 'no-conversion))
933 (insert-file-contents tmpfile)))
934 (delete-file tmpfile)))))
935
936 (defun archive-file-name-handler (op &rest args)
937 (or (eq op 'file-exists-p)
938 (let ((file-name-handler-alist nil))
939 (apply op args))))
940
941 (defun archive-set-buffer-as-visiting-file (filename)
942 "Set the current buffer as if it were visiting FILENAME."
943 (save-excursion
944 (goto-char (point-min))
945 (let ((buffer-undo-list t)
946 (coding
947 (or coding-system-for-read
948 (and set-auto-coding-function
949 (save-excursion
950 (funcall set-auto-coding-function
951 filename (- (point-max) (point-min)))))
952 ;; dos-w32.el defines the function
953 ;; find-buffer-file-type-coding-system for DOS/Windows
954 ;; systems which preserves the coding-system of existing files.
955 ;; (That function is called via file-coding-system-alist.)
956 ;; Here, we want it to act as if the extracted file existed.
957 ;; The following let-binding of file-name-handler-alist forces
958 ;; find-file-not-found-set-buffer-file-coding-system to ignore
959 ;; the file's name (see dos-w32.el).
960 (let ((file-name-handler-alist
961 '(("" . archive-file-name-handler))))
962 (car (find-operation-coding-system
963 'insert-file-contents
964 (cons filename (current-buffer)) t))))))
965 (unless (or coding-system-for-read
966 enable-multibyte-characters)
967 (setq coding
968 (coding-system-change-text-conversion coding 'raw-text)))
969 (unless (memq coding '(nil no-conversion))
970 (decode-coding-region (point-min) (point-max) coding)
971 (setq last-coding-system-used coding))
972 (set-buffer-modified-p nil)
973 (kill-local-variable 'buffer-file-coding-system)
974 (after-insert-file-set-coding (- (point-max) (point-min))))))
975
976 (define-obsolete-function-alias 'archive-mouse-extract 'archive-extract "22.1")
977
978 (defun archive-extract (&optional other-window-p event)
979 "In archive mode, extract this entry of the archive into its own buffer."
980 (interactive (list nil last-input-event))
981 (if event (posn-set-point (event-end event)))
982 (let* ((view-p (eq other-window-p 'view))
983 (descr (archive-get-descr))
984 (ename (aref descr 0))
985 (iname (aref descr 1))
986 (archive-buffer (current-buffer))
987 (arcdir default-directory)
988 (archive (buffer-file-name))
989 (arcname (file-name-nondirectory archive))
990 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
991 (extractor (archive-name "extract"))
992 ;; Members with file names which aren't valid for the
993 ;; underlying filesystem, are treated as read-only.
994 (read-only-p (or archive-read-only
995 view-p
996 (string-match file-name-invalid-regexp ename)))
997 (arcfilename (expand-file-name (concat arcname ":" iname)))
998 (buffer (get-buffer bufname))
999 (just-created nil)
1000 (file-name-coding archive-file-name-coding-system))
1001 (if (and buffer
1002 (string= (buffer-file-name buffer) arcfilename))
1003 nil
1004 (setq archive (archive-maybe-copy archive))
1005 (setq bufname (generate-new-buffer-name bufname))
1006 (setq buffer (get-buffer-create bufname))
1007 (setq just-created t)
1008 (with-current-buffer buffer
1009 (setq buffer-file-name arcfilename)
1010 (setq buffer-file-truename
1011 (abbreviate-file-name buffer-file-name))
1012 ;; Set the default-directory to the dir of the superior buffer.
1013 (setq default-directory arcdir)
1014 (make-local-variable 'archive-superior-buffer)
1015 (setq archive-superior-buffer archive-buffer)
1016 (add-hook 'write-file-functions 'archive-write-file-member nil t)
1017 (setq archive-subfile-mode descr)
1018 (setq archive-file-name-coding-system file-name-coding)
1019 (if (and
1020 (null
1021 (let (;; We may have to encode file name arguement for
1022 ;; external programs.
1023 (coding-system-for-write
1024 (and enable-multibyte-characters
1025 archive-file-name-coding-system))
1026 ;; We read an archive member by no-conversion at
1027 ;; first, then decode appropriately by calling
1028 ;; archive-set-buffer-as-visiting-file later.
1029 (coding-system-for-read 'no-conversion))
1030 (condition-case err
1031 (if (fboundp extractor)
1032 (funcall extractor archive ename)
1033 (archive-*-extract archive ename
1034 (symbol-value extractor)))
1035 (error
1036 (ding (message "%s" (error-message-string err)))
1037 nil))))
1038 just-created)
1039 (progn
1040 (set-buffer-modified-p nil)
1041 (kill-buffer buffer))
1042 (archive-try-jka-compr) ;Pretty ugly hack :-(
1043 (archive-set-buffer-as-visiting-file ename)
1044 (goto-char (point-min))
1045 (rename-buffer bufname)
1046 (setq buffer-read-only read-only-p)
1047 (setq buffer-undo-list nil)
1048 (set-buffer-modified-p nil)
1049 (setq buffer-saved-size (buffer-size))
1050 (normal-mode)
1051 ;; Just in case an archive occurs inside another archive.
1052 (when (derived-mode-p 'archive-mode)
1053 (setq archive-remote t)
1054 (if read-only-p (setq archive-read-only t))
1055 ;; We will write out the archive ourselves if it is
1056 ;; part of another archive.
1057 (remove-hook 'write-contents-functions 'archive-write-file t))
1058 (run-hooks 'archive-extract-hooks)
1059 (if archive-read-only
1060 (message "Note: altering this archive is not implemented."))))
1061 (archive-maybe-update t))
1062 (or (not (buffer-name buffer))
1063 (cond
1064 (view-p
1065 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
1066 ((eq other-window-p 'display) (display-buffer buffer))
1067 (other-window-p (switch-to-buffer-other-window buffer))
1068 (t (switch-to-buffer buffer))))))
1069
1070 (defun archive-*-extract (archive name command)
1071 (let* ((default-directory (file-name-as-directory archive-tmpdir))
1072 (tmpfile (expand-file-name (file-name-nondirectory name)
1073 default-directory))
1074 exit-status success)
1075 (make-directory (directory-file-name default-directory) t)
1076 (setq exit-status
1077 (apply 'call-process
1078 (car command)
1079 nil
1080 nil
1081 nil
1082 (append (cdr command) (list archive name))))
1083 (cond ((and (numberp exit-status) (zerop exit-status))
1084 (if (not (file-exists-p tmpfile))
1085 (ding (message "`%s': no such file or directory" tmpfile))
1086 (insert-file-contents tmpfile)
1087 (setq success t)))
1088 ((numberp exit-status)
1089 (ding
1090 (message "`%s' exited with status %d" (car command) exit-status)))
1091 ((stringp exit-status)
1092 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1093 (t
1094 (ding (message "`%s' failed" (car command)))))
1095 (archive-delete-local tmpfile)
1096 success))
1097
1098 (defun archive-extract-by-stdout (archive name command &optional stderr-file)
1099 (apply 'call-process
1100 (car command)
1101 nil
1102 (if stderr-file (list t stderr-file) t)
1103 nil
1104 (append (cdr command) (list archive name))))
1105
1106 (defun archive-extract-other-window ()
1107 "In archive mode, find this member in another window."
1108 (interactive)
1109 (archive-extract t))
1110
1111 (defun archive-display-other-window ()
1112 "In archive mode, display this member in another window."
1113 (interactive)
1114 (archive-extract 'display))
1115
1116 (defun archive-view ()
1117 "In archive mode, view the member on this line."
1118 (interactive)
1119 (archive-extract 'view))
1120
1121 (defun archive-add-new-member (arcbuf name)
1122 "Add current buffer to the archive in ARCBUF naming it NAME."
1123 (interactive
1124 (list (get-buffer
1125 (read-buffer "Buffer containing archive: "
1126 ;; Find first archive buffer and suggest that
1127 (let ((bufs (buffer-list)))
1128 (while (and bufs
1129 (not (with-current-buffer (car bufs)
1130 (derived-mode-p 'archive-mode))))
1131 (setq bufs (cdr bufs)))
1132 (if bufs
1133 (car bufs)
1134 (error "There are no archive buffers")))
1135 t))
1136 (read-string "File name in archive: "
1137 (if buffer-file-name
1138 (file-name-nondirectory buffer-file-name)
1139 ""))))
1140 (with-current-buffer arcbuf
1141 (or (derived-mode-p 'archive-mode)
1142 (error "Buffer is not an archive buffer"))
1143 (if archive-read-only
1144 (error "Archive is read-only")))
1145 (if (eq arcbuf (current-buffer))
1146 (error "An archive buffer cannot be added to itself"))
1147 (if (string= name "")
1148 (error "Archive members may not be given empty names"))
1149 (let ((func (with-current-buffer arcbuf
1150 (archive-name "add-new-member")))
1151 (membuf (current-buffer)))
1152 (if (fboundp func)
1153 (with-current-buffer arcbuf
1154 (funcall func buffer-file-name membuf name))
1155 (error "Adding a new member is not supported for this archive type"))))
1156 ;; -------------------------------------------------------------------------
1157 ;;; Section: IO stuff
1158
1159 (defun archive-write-file-member ()
1160 (save-excursion
1161 (save-restriction
1162 (message "Updating archive...")
1163 (widen)
1164 (let ((writer (with-current-buffer archive-superior-buffer
1165 (archive-name "write-file-member")))
1166 (archive (with-current-buffer archive-superior-buffer
1167 (archive-maybe-copy (buffer-file-name)))))
1168 (if (fboundp writer)
1169 (funcall writer archive archive-subfile-mode)
1170 (archive-*-write-file-member archive
1171 archive-subfile-mode
1172 (symbol-value writer)))
1173 (set-buffer-modified-p nil)
1174 (message "Updating archive...done"))
1175 (set-buffer archive-superior-buffer)
1176 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1177 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1178 ;; won't reset the coding-system of this archive member.
1179 (if (local-variable-p 'archive-member-coding-system)
1180 (setq last-coding-system-used archive-member-coding-system))
1181 t)
1182
1183 (defun archive-*-write-file-member (archive descr command)
1184 (let* ((ename (aref descr 0))
1185 (tmpfile (expand-file-name ename archive-tmpdir))
1186 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1187 (default-directory (file-name-as-directory top)))
1188 (unwind-protect
1189 (progn
1190 (make-directory (file-name-directory tmpfile) t)
1191 ;; If the member is itself an archive, write it without
1192 ;; the dired-like listing we created.
1193 (if (eq major-mode 'archive-mode)
1194 (archive-write-file tmpfile)
1195 (write-region nil nil tmpfile nil 'nomessage))
1196 ;; basic-save-buffer needs last-coding-system-used to have
1197 ;; the value used to write the file, so save it before any
1198 ;; further processing clobbers it (we restore it in
1199 ;; archive-write-file-member, above).
1200 (setq archive-member-coding-system last-coding-system-used)
1201 (if (aref descr 3)
1202 ;; Set the file modes, but make sure we can read it.
1203 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1204 (setq ename
1205 (encode-coding-string ename archive-file-name-coding-system))
1206 (let* ((coding-system-for-write 'no-conversion)
1207 (exitcode (apply 'call-process
1208 (car command)
1209 nil
1210 nil
1211 nil
1212 (append (cdr command)
1213 (list archive ename)))))
1214 (or (zerop exitcode)
1215 (error "Updating was unsuccessful (%S)" exitcode))))
1216 (archive-delete-local tmpfile))))
1217
1218 (defun archive-write-file (&optional file)
1219 (save-excursion
1220 (let ((coding-system-for-write 'no-conversion))
1221 (write-region archive-proper-file-start (point-max)
1222 (or file buffer-file-name) nil t)
1223 (set-buffer-modified-p nil))
1224 t))
1225 ;; -------------------------------------------------------------------------
1226 ;;; Section: Marking and unmarking.
1227
1228 (defun archive-flag-deleted (p &optional type)
1229 "In archive mode, mark this member to be deleted from the archive.
1230 With a prefix argument, mark that many files."
1231 (interactive "p")
1232 (or type (setq type ?D))
1233 (beginning-of-line)
1234 (let ((sign (if (>= p 0) +1 -1))
1235 (modified (buffer-modified-p))
1236 (inhibit-read-only t))
1237 (while (not (zerop p))
1238 (if (archive-get-descr t)
1239 (progn
1240 (delete-char 1)
1241 (insert type)))
1242 (forward-line sign)
1243 (setq p (- p sign)))
1244 (restore-buffer-modified-p modified))
1245 (archive-next-line 0))
1246
1247 (defun archive-unflag (p)
1248 "In archive mode, un-mark this member if it is marked to be deleted.
1249 With a prefix argument, un-mark that many files forward."
1250 (interactive "p")
1251 (archive-flag-deleted p ?\s))
1252
1253 (defun archive-unflag-backwards (p)
1254 "In archive mode, un-mark this member if it is marked to be deleted.
1255 With a prefix argument, un-mark that many members backward."
1256 (interactive "p")
1257 (archive-flag-deleted (- p) ?\s))
1258
1259 (defun archive-unmark-all-files ()
1260 "Remove all marks."
1261 (interactive)
1262 (let ((modified (buffer-modified-p))
1263 (inhibit-read-only t))
1264 (save-excursion
1265 (goto-char archive-file-list-start)
1266 (while (< (point) archive-file-list-end)
1267 (or (= (following-char) ?\s)
1268 (progn (delete-char 1) (insert ?\s)))
1269 (forward-line 1)))
1270 (restore-buffer-modified-p modified)))
1271
1272 (defun archive-mark (p)
1273 "In archive mode, mark this member for group operations.
1274 With a prefix argument, mark that many members.
1275 Use \\[archive-unmark-all-files] to remove all marks."
1276 (interactive "p")
1277 (archive-flag-deleted p ?*))
1278
1279 (defun archive-get-marked (mark &optional default)
1280 (let (files)
1281 (save-excursion
1282 (goto-char archive-file-list-start)
1283 (while (< (point) archive-file-list-end)
1284 (if (= (following-char) mark)
1285 (setq files (cons (archive-get-descr) files)))
1286 (forward-line 1)))
1287 (or (nreverse files)
1288 (and default
1289 (list (archive-get-descr))))))
1290 ;; -------------------------------------------------------------------------
1291 ;;; Section: Operate
1292
1293 (defun archive-next-line (p)
1294 (interactive "p")
1295 (forward-line p)
1296 (or (eobp)
1297 (forward-char archive-file-name-indent)))
1298
1299 (defun archive-previous-line (p)
1300 (interactive "p")
1301 (archive-next-line (- p)))
1302
1303 (defun archive-chmod-entry (new-mode)
1304 "Change the protection bits associated with all marked or this member.
1305 The new protection bits can either be specified as an octal number or
1306 as a relative change like \"g+rw\" as for chmod(2)."
1307 (interactive "sNew mode (octal or relative): ")
1308 (if archive-read-only (error "Archive is read-only"))
1309 (let ((func (archive-name "chmod-entry")))
1310 (if (fboundp func)
1311 (progn
1312 (funcall func new-mode (archive-get-marked ?* t))
1313 (archive-resummarize))
1314 (error "Setting mode bits is not supported for this archive type"))))
1315
1316 (defun archive-chown-entry (new-uid)
1317 "Change the owner of all marked or this member."
1318 (interactive "nNew uid: ")
1319 (if archive-read-only (error "Archive is read-only"))
1320 (let ((func (archive-name "chown-entry")))
1321 (if (fboundp func)
1322 (progn
1323 (funcall func new-uid (archive-get-marked ?* t))
1324 (archive-resummarize))
1325 (error "Setting owner is not supported for this archive type"))))
1326
1327 (defun archive-chgrp-entry (new-gid)
1328 "Change the group of all marked or this member."
1329 (interactive "nNew gid: ")
1330 (if archive-read-only (error "Archive is read-only"))
1331 (let ((func (archive-name "chgrp-entry")))
1332 (if (fboundp func)
1333 (progn
1334 (funcall func new-gid (archive-get-marked ?* t))
1335 (archive-resummarize))
1336 (error "Setting group is not supported for this archive type"))))
1337
1338 (defun archive-expunge ()
1339 "Do the flagged deletions."
1340 (interactive)
1341 (let (files)
1342 (save-excursion
1343 (goto-char archive-file-list-start)
1344 (while (< (point) archive-file-list-end)
1345 (if (= (following-char) ?D)
1346 (setq files (cons (aref (archive-get-descr) 0) files)))
1347 (forward-line 1)))
1348 (setq files (nreverse files))
1349 (and files
1350 (or (not archive-read-only)
1351 (error "Archive is read-only"))
1352 (or (yes-or-no-p (format "Really delete %d member%s? "
1353 (length files)
1354 (if (null (cdr files)) "" "s")))
1355 (error "Operation aborted"))
1356 (let ((archive (archive-maybe-copy (buffer-file-name)))
1357 (expunger (archive-name "expunge")))
1358 (if (fboundp expunger)
1359 (funcall expunger archive files)
1360 (archive-*-expunge archive files (symbol-value expunger)))
1361 (archive-maybe-update nil)
1362 (if archive-remote
1363 (archive-resummarize)
1364 (revert-buffer))))))
1365
1366 (defun archive-*-expunge (archive files command)
1367 (apply 'call-process
1368 (car command)
1369 nil
1370 nil
1371 nil
1372 (append (cdr command) (cons archive files))))
1373
1374 (defun archive-rename-entry (newname)
1375 "Change the name associated with this entry in the archive file."
1376 (interactive "sNew name: ")
1377 (if archive-read-only (error "Archive is read-only"))
1378 (if (string= newname "")
1379 (error "Archive members may not be given empty names"))
1380 (let ((func (archive-name "rename-entry"))
1381 (descr (archive-get-descr)))
1382 (if (fboundp func)
1383 (progn
1384 (funcall func
1385 (encode-coding-string newname
1386 archive-file-name-coding-system)
1387 descr)
1388 (archive-resummarize))
1389 (error "Renaming is not supported for this archive type"))))
1390
1391 ;; Revert the buffer and recompute the dired-like listing.
1392 (defun archive-mode-revert (&optional no-auto-save no-confirm)
1393 (let ((no (archive-get-lineno)))
1394 (setq archive-files nil)
1395 (let ((revert-buffer-function nil)
1396 (coding-system-for-read 'no-conversion))
1397 (revert-buffer t t))
1398 (archive-mode)
1399 (goto-char archive-file-list-start)
1400 (archive-next-line no)))
1401
1402 (defun archive-undo ()
1403 "Undo in an archive buffer.
1404 This doesn't recover lost files, it just undoes changes in the buffer itself."
1405 (interactive)
1406 (let ((inhibit-read-only t))
1407 (undo)))
1408 ;; -------------------------------------------------------------------------
1409 ;;; Section: Arc Archives
1410
1411 (defun archive-arc-summarize ()
1412 (let ((p 1)
1413 (totalsize 0)
1414 (maxlen 8)
1415 files
1416 visual)
1417 (while (and (< (+ p 29) (point-max))
1418 (= (byte-after p) ?\C-z)
1419 (> (byte-after (1+ p)) 0))
1420 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1421 (fnlen (or (string-match "\0" namefld) 13))
1422 (efnname (decode-coding-string (substring namefld 0 fnlen)
1423 archive-file-name-coding-system))
1424 ;; Convert to float to avoid overflow for very large files.
1425 (csize (archive-l-e (+ p 15) 4 'float))
1426 (moddate (archive-l-e (+ p 19) 2))
1427 (modtime (archive-l-e (+ p 21) 2))
1428 (ucsize (archive-l-e (+ p 25) 4 'float))
1429 (fiddle (string= efnname (upcase efnname)))
1430 (ifnname (if fiddle (downcase efnname) efnname))
1431 (text (format " %8.0f %-11s %-8s %s"
1432 ucsize
1433 (archive-dosdate moddate)
1434 (archive-dostime modtime)
1435 ifnname)))
1436 (setq maxlen (max maxlen fnlen)
1437 totalsize (+ totalsize ucsize)
1438 visual (cons (vector text
1439 (- (length text) (length ifnname))
1440 (length text))
1441 visual)
1442 files (cons (vector efnname ifnname fiddle nil (1- p))
1443 files)
1444 ;; p needs to stay an integer, since we use it in char-after
1445 ;; above. Passing through `round' limits the compressed size
1446 ;; to most-positive-fixnum, but if the compressed size exceeds
1447 ;; that, we cannot visit the archive anyway.
1448 p (+ p 29 (round csize)))))
1449 (goto-char (point-min))
1450 (let ((dash (concat "- -------- ----------- -------- "
1451 (make-string maxlen ?-)
1452 "\n")))
1453 (insert "M Length Date Time File\n"
1454 dash)
1455 (archive-summarize-files (nreverse visual))
1456 (insert dash
1457 (format " %8.0f %d file%s"
1458 totalsize
1459 (length files)
1460 (if (= 1 (length files)) "" "s"))
1461 "\n"))
1462 (apply 'vector (nreverse files))))
1463
1464 (defun archive-arc-rename-entry (newname descr)
1465 (if (string-match "[:\\\\/]" newname)
1466 (error "File names in arc files must not contain a directory component"))
1467 (if (> (length newname) 12)
1468 (error "File names in arc files are limited to 12 characters"))
1469 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1470 (length newname))))
1471 (inhibit-read-only t))
1472 (save-restriction
1473 (save-excursion
1474 (widen)
1475 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1476 (delete-char 13)
1477 (insert-unibyte name)))))
1478 ;; -------------------------------------------------------------------------
1479 ;;; Section: Lzh Archives
1480
1481 (defun archive-lzh-summarize (&optional start)
1482 (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
1483 (totalsize 0)
1484 (maxlen 8)
1485 files
1486 visual)
1487 (while (progn (goto-char p) ;beginning of a base header.
1488 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1489 (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
1490 ;; Convert to float to avoid overflow for very large files.
1491 (csize (archive-l-e (+ p 7) 4 'float)) ;size of a compressed file to follow (level 0 and 2),
1492 ;size of extended headers + the compressed file to follow (level 1).
1493 (ucsize (archive-l-e (+ p 11) 4 'float)) ;size of an uncompressed file.
1494 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1495 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
1496 (hdrlvl (byte-after (+ p 20))) ;header level
1497 thsize ;total header size (base + extensions)
1498 fnlen efnname osid fiddle ifnname width p2
1499 neh ;beginning of next extension header (level 1 and 2)
1500 mode modestr uid gid text dir prname
1501 gname uname modtime moddate)
1502 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1503 (when (or (= hdrlvl 0) (= hdrlvl 1))
1504 (setq fnlen (byte-after (+ p 21))) ;filename length
1505 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
1506 (decode-coding-string
1507 str archive-file-name-coding-system)))
1508 (setq p2 (+ p 22 fnlen))) ;
1509 (if (= hdrlvl 1)
1510 (setq neh (+ p2 3)) ;specific to level 1 header
1511 (if (= hdrlvl 2)
1512 (setq neh (+ p 24)))) ;specific to level 2 header
1513 (if neh ;if level 1 or 2 we expect extension headers to follow
1514 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
1515 (etype (byte-after (+ neh 2)))) ;extension type
1516 (while (not (= ehsize 0))
1517 (cond
1518 ((= etype 1) ;file name
1519 (let ((i (+ neh 3)))
1520 (while (< i (+ neh ehsize))
1521 (setq efnname (concat efnname (char-to-string (byte-after i))))
1522 (setq i (1+ i)))))
1523 ((= etype 2) ;directory name
1524 (let ((i (+ neh 3)))
1525 (while (< i (+ neh ehsize))
1526 (setq dir (concat dir
1527 (if (= (byte-after i)
1528 255)
1529 "/"
1530 (char-to-string
1531 (char-after i)))))
1532 (setq i (1+ i)))))
1533 ((= etype 80) ;Unix file permission
1534 (setq mode (archive-l-e (+ neh 3) 2)))
1535 ((= etype 81) ;UNIX file group/user ID
1536 (progn (setq uid (archive-l-e (+ neh 3) 2))
1537 (setq gid (archive-l-e (+ neh 5) 2))))
1538 ((= etype 82) ;UNIX file group name
1539 (let ((i (+ neh 3)))
1540 (while (< i (+ neh ehsize))
1541 (setq gname (concat gname (char-to-string (char-after i))))
1542 (setq i (1+ i)))))
1543 ((= etype 83) ;UNIX file user name
1544 (let ((i (+ neh 3)))
1545 (while (< i (+ neh ehsize))
1546 (setq uname (concat uname (char-to-string (char-after i))))
1547 (setq i (1+ i)))))
1548 )
1549 (setq neh (+ neh ehsize))
1550 (setq ehsize (archive-l-e neh 2))
1551 (setq etype (byte-after (+ neh 2))))
1552 ;;get total header size for level 1 and 2 headers
1553 (setq thsize (- neh p))))
1554 (if (= hdrlvl 0) ;total header size
1555 (setq thsize hsize))
1556 ;; OS ID field not present in level 0 header, use code 0 "generic"
1557 ;; in that case as per lha program header.c get_header()
1558 (setq osid (cond ((= hdrlvl 0) 0)
1559 ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
1560 ((= hdrlvl 2) (char-after (+ p 23)))))
1561 ;; Filename fiddling must follow the lha program, otherwise the name
1562 ;; passed to "lha pq" etc won't match (which for an extract silently
1563 ;; results in no output). As of version 1.14i it goes from the OS ID,
1564 ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
1565 ;; converts "\" to "/".
1566 ;; - For 0 generic: generic_to_unix_filename() downcases if there's
1567 ;; no lower case already present, and converts "\" to "/".
1568 ;; - For 'm' MacOS: macos_to_unix_filename() changes "/" to ":" and
1569 ;; ":" to "/"
1570 (setq fiddle (cond ((= ?M osid) t)
1571 ((= 0 osid) (string= efnname (upcase efnname)))))
1572 (setq ifnname (if fiddle (downcase efnname) efnname))
1573 (setq prname (if dir (concat dir ifnname) ifnname))
1574 (setq width (if prname (string-width prname) 0))
1575 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1576 (setq moddate (if (= hdrlvl 2)
1577 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1578 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1579 (setq modtime (if (= hdrlvl 2)
1580 (archive-unixtime time1 time2)
1581 (archive-dostime time1)))
1582 (setq text (if archive-alternate-display
1583 (format " %8.0f %5S %5S %s"
1584 ucsize
1585 (or uid "?")
1586 (or gid "?")
1587 ifnname)
1588 (format " %10s %8.0f %-11s %-8s %s"
1589 modestr
1590 ucsize
1591 moddate
1592 modtime
1593 prname)))
1594 (setq maxlen (max maxlen width)
1595 totalsize (+ totalsize ucsize)
1596 visual (cons (vector text
1597 (- (length text) (length prname))
1598 (length text))
1599 visual)
1600 files (cons (vector prname ifnname fiddle mode (1- p))
1601 files))
1602 (cond ((= hdrlvl 1)
1603 ;; p needs to stay an integer, since we use it in goto-char
1604 ;; above. Passing through `round' limits the compressed size
1605 ;; to most-positive-fixnum, but if the compressed size exceeds
1606 ;; that, we cannot visit the archive anyway.
1607 (setq p (+ p hsize 2 (round csize))))
1608 ((or (= hdrlvl 2) (= hdrlvl 0))
1609 (setq p (+ p thsize 2 (round csize)))))
1610 ))
1611 (goto-char (point-min))
1612 (let ((dash (concat (if archive-alternate-display
1613 "- -------- ----- ----- "
1614 "- ---------- -------- ----------- -------- ")
1615 (make-string maxlen ?-)
1616 "\n"))
1617 (header (if archive-alternate-display
1618 "M Length Uid Gid File\n"
1619 "M Filemode Length Date Time File\n"))
1620 (sumline (if archive-alternate-display
1621 " %8.0f %d file%s"
1622 " %8.0f %d file%s")))
1623 (insert header dash)
1624 (archive-summarize-files (nreverse visual))
1625 (insert dash
1626 (format sumline
1627 totalsize
1628 (length files)
1629 (if (= 1 (length files)) "" "s"))
1630 "\n"))
1631 (apply 'vector (nreverse files))))
1632
1633 (defconst archive-lzh-alternate-display t)
1634
1635 (defun archive-lzh-extract (archive name)
1636 (archive-extract-by-stdout archive name archive-lzh-extract))
1637
1638 (defun archive-lzh-resum (p count)
1639 (let ((sum 0))
1640 (while (> count 0)
1641 (setq count (1- count)
1642 sum (+ sum (byte-after p))
1643 p (1+ p)))
1644 (logand sum 255)))
1645
1646 (defun archive-lzh-rename-entry (newname descr)
1647 (save-restriction
1648 (save-excursion
1649 (widen)
1650 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1651 (oldhsize (byte-after p))
1652 (oldfnlen (byte-after (+ p 21)))
1653 (newfnlen (length newname))
1654 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1655 (inhibit-read-only t))
1656 (if (> newhsize 255)
1657 (error "The file name is too long"))
1658 (goto-char (+ p 21))
1659 (delete-char (1+ oldfnlen))
1660 (insert-unibyte newfnlen newname)
1661 (goto-char p)
1662 (delete-char 2)
1663 (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
1664
1665 (defun archive-lzh-ogm (newval files errtxt ofs)
1666 (save-excursion
1667 (save-restriction
1668 (widen)
1669 (dolist (fil files)
1670 (let* ((p (+ archive-proper-file-start (aref fil 4)))
1671 (hsize (byte-after p))
1672 (fnlen (byte-after (+ p 21)))
1673 (p2 (+ p 22 fnlen))
1674 (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
1675 (inhibit-read-only t))
1676 (if (= creator ?U)
1677 (progn
1678 (or (numberp newval)
1679 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1680 (goto-char (+ p2 ofs))
1681 (delete-char 2)
1682 (insert-unibyte (logand newval 255) (lsh newval -8))
1683 (goto-char (1+ p))
1684 (delete-char 1)
1685 (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
1686 (message "Member %s does not have %s field"
1687 (aref fil 1) errtxt)))))))
1688
1689 (defun archive-lzh-chown-entry (newuid files)
1690 (archive-lzh-ogm newuid files "an uid" 10))
1691
1692 (defun archive-lzh-chgrp-entry (newgid files)
1693 (archive-lzh-ogm newgid files "a gid" 12))
1694
1695 (defun archive-lzh-chmod-entry (newmode files)
1696 (archive-lzh-ogm
1697 ;; This should work even though newmode will be dynamically accessed.
1698 (lambda (old) (archive-calc-mode old newmode t))
1699 files "a unix-style mode" 8))
1700
1701 ;; -------------------------------------------------------------------------
1702 ;;; Section: Lzh Self-Extracting .exe Archives
1703 ;;
1704 ;; No support for modifying these files. It looks like the lha for unix
1705 ;; program (as of version 1.14i) can't create or retain the DOS exe part.
1706 ;; If you do an "lha a" on a .exe for instance it renames and writes to a
1707 ;; plain .lzh.
1708
1709 (defun archive-lzh-exe-summarize ()
1710 "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
1711
1712 ;; Skip the initial executable code part and apply archive-lzh-summarize
1713 ;; to the archive part proper. The "-lh5-" etc regexp here for the start
1714 ;; is the same as in archive-find-type.
1715 ;;
1716 ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
1717 ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
1718 ;; the header checksum, or level 2 a test of the "attribute" and size.
1719 ;;
1720 (re-search-forward "..-l[hz][0-9ds]-" nil)
1721 (archive-lzh-summarize (match-beginning 0)))
1722
1723 ;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
1724 ;; .lzh files
1725 (defalias 'archive-lzh-exe-extract 'archive-lzh-extract
1726 "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
1727
1728 ;; -------------------------------------------------------------------------
1729 ;;; Section: Zip Archives
1730
1731 (defun archive-zip-summarize ()
1732 (goto-char (- (point-max) (- 22 18)))
1733 (search-backward-regexp "[P]K\005\006")
1734 (let ((p (+ (point-min) (archive-l-e (+ (point) 16) 4)))
1735 (maxlen 8)
1736 (totalsize 0)
1737 files
1738 visual)
1739 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1740 (let* ((creator (byte-after (+ p 5)))
1741 ;; (method (archive-l-e (+ p 10) 2))
1742 (modtime (archive-l-e (+ p 12) 2))
1743 (moddate (archive-l-e (+ p 14) 2))
1744 ;; Convert to float to avoid overflow for very large files.
1745 (ucsize (archive-l-e (+ p 24) 4 'float))
1746 (fnlen (archive-l-e (+ p 28) 2))
1747 (exlen (archive-l-e (+ p 30) 2))
1748 (fclen (archive-l-e (+ p 32) 2))
1749 (lheader (archive-l-e (+ p 42) 4))
1750 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1751 (decode-coding-string
1752 str archive-file-name-coding-system)))
1753 (isdir (and (= ucsize 0)
1754 (string= (file-name-nondirectory efnname) "")))
1755 (mode (cond ((memq creator '(2 3)) ; Unix
1756 (archive-l-e (+ p 40) 2))
1757 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1758 (logior ?\444
1759 (if isdir (logior 16384 ?\111) 0)
1760 (if (zerop
1761 (logand 1 (byte-after (+ p 38))))
1762 ?\222 0)))
1763 (t nil)))
1764 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1765 (fiddle (and archive-zip-case-fiddle
1766 (not (not (memq creator '(0 2 4 5 9))))
1767 (string= (upcase efnname) efnname)))
1768 (ifnname (if fiddle (downcase efnname) efnname))
1769 (width (string-width ifnname))
1770 (text (format " %10s %8.0f %-11s %-8s %s"
1771 modestr
1772 ucsize
1773 (archive-dosdate moddate)
1774 (archive-dostime modtime)
1775 ifnname)))
1776 (setq maxlen (max maxlen width)
1777 totalsize (+ totalsize ucsize)
1778 visual (cons (vector text
1779 (- (length text) (length ifnname))
1780 (length text))
1781 visual)
1782 files (cons (if isdir
1783 nil
1784 (vector efnname ifnname fiddle mode
1785 (list (1- p) lheader)))
1786 files)
1787 p (+ p 46 fnlen exlen fclen))))
1788 (goto-char (point-min))
1789 (let ((dash (concat "- ---------- -------- ----------- -------- "
1790 (make-string maxlen ?-)
1791 "\n")))
1792 (insert "M Filemode Length Date Time File\n"
1793 dash)
1794 (archive-summarize-files (nreverse visual))
1795 (insert dash
1796 (format " %8.0f %d file%s"
1797 totalsize
1798 (length files)
1799 (if (= 1 (length files)) "" "s"))
1800 "\n"))
1801 (apply 'vector (nreverse files))))
1802
1803 (defun archive-zip-extract (archive name)
1804 (cond
1805 ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
1806 (archive-*-extract archive name archive-zip-extract))
1807 ((equal (car archive-zip-extract) "7z")
1808 (let ((archive-7z-extract archive-zip-extract))
1809 (archive-7z-extract archive name)))
1810 (t
1811 (archive-extract-by-stdout
1812 archive
1813 ;; unzip expands wildcards in NAME, so we need to quote it. But
1814 ;; not on DOS/Windows, since that fails extraction on those
1815 ;; systems (unless w32-quote-process-args is nil), and file names
1816 ;; with wildcards in zip archives don't work there anyway.
1817 ;; FIXME: Does pkunzip need similar treatment?
1818 (if (and (or (not (memq system-type '(windows-nt ms-dos)))
1819 (and (boundp 'w32-quote-process-args)
1820 (null w32-quote-process-args)))
1821 (equal (car archive-zip-extract) "unzip"))
1822 (shell-quote-argument name)
1823 name)
1824 archive-zip-extract))))
1825
1826 (defun archive-zip-write-file-member (archive descr)
1827 (archive-*-write-file-member
1828 archive
1829 descr
1830 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1831
1832 (defun archive-zip-chmod-entry (newmode files)
1833 (save-restriction
1834 (save-excursion
1835 (widen)
1836 (dolist (fil files)
1837 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
1838 (creator (byte-after (+ p 5)))
1839 (oldmode (aref fil 3))
1840 (newval (archive-calc-mode oldmode newmode t))
1841 (inhibit-read-only t))
1842 (cond ((memq creator '(2 3)) ; Unix
1843 (goto-char (+ p 40))
1844 (delete-char 2)
1845 (insert-unibyte (logand newval 255) (lsh newval -8)))
1846 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1847 (goto-char (+ p 38))
1848 (insert-unibyte (logior (logand (byte-after (point)) 254)
1849 (logand (logxor 1 (lsh newval -7)) 1)))
1850 (delete-char 1))
1851 (t (message "Don't know how to change mode for this member"))))
1852 ))))
1853 ;; -------------------------------------------------------------------------
1854 ;;; Section: Zoo Archives
1855
1856 (defun archive-zoo-summarize ()
1857 (let ((p (1+ (archive-l-e 25 4)))
1858 (maxlen 8)
1859 (totalsize 0)
1860 files
1861 visual)
1862 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1863 (> (archive-l-e (+ p 6) 4) 0))
1864 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1865 (moddate (archive-l-e (+ p 14) 2))
1866 (modtime (archive-l-e (+ p 16) 2))
1867 ;; Convert to float to avoid overflow for very large files.
1868 (ucsize (archive-l-e (+ p 20) 4 'float))
1869 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1870 (dirtype (byte-after (+ p 4)))
1871 (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
1872 (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
1873 (fnlen (or (string-match "\0" namefld) 13))
1874 (efnname (let ((str
1875 (concat
1876 (if (> ldirlen 0)
1877 (concat (buffer-substring
1878 (+ p 58 lfnlen)
1879 (+ p 58 lfnlen ldirlen -1))
1880 "/")
1881 "")
1882 (if (> lfnlen 0)
1883 (buffer-substring (+ p 58)
1884 (+ p 58 lfnlen -1))
1885 (substring namefld 0 fnlen)))))
1886 (decode-coding-string
1887 str archive-file-name-coding-system)))
1888 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1889 (ifnname (if fiddle (downcase efnname) efnname))
1890 (width (string-width ifnname))
1891 (text (format " %8.0f %-11s %-8s %s"
1892 ucsize
1893 (archive-dosdate moddate)
1894 (archive-dostime modtime)
1895 ifnname)))
1896 (setq maxlen (max maxlen width)
1897 totalsize (+ totalsize ucsize)
1898 visual (cons (vector text
1899 (- (length text) (length ifnname))
1900 (length text))
1901 visual)
1902 files (cons (vector efnname ifnname fiddle nil (1- p))
1903 files)
1904 p next)))
1905 (goto-char (point-min))
1906 (let ((dash (concat "- -------- ----------- -------- "
1907 (make-string maxlen ?-)
1908 "\n")))
1909 (insert "M Length Date Time File\n"
1910 dash)
1911 (archive-summarize-files (nreverse visual))
1912 (insert dash
1913 (format " %8.0f %d file%s"
1914 totalsize
1915 (length files)
1916 (if (= 1 (length files)) "" "s"))
1917 "\n"))
1918 (apply 'vector (nreverse files))))
1919
1920 (defun archive-zoo-extract (archive name)
1921 (archive-extract-by-stdout archive name archive-zoo-extract))
1922
1923 ;; -------------------------------------------------------------------------
1924 ;;; Section: Rar Archives
1925
1926 (defun archive-rar-summarize (&optional file)
1927 ;; File is used internally for `archive-rar-exe-summarize'.
1928 (unless file (setq file buffer-file-name))
1929 (let* ((copy (file-local-copy file))
1930 (maxname 10)
1931 (maxsize 5)
1932 (files ()))
1933 (with-temp-buffer
1934 (call-process "unrar-free" nil t nil "--list" (or file copy))
1935 (if copy (delete-file copy))
1936 (goto-char (point-min))
1937 (re-search-forward "^-+\n")
1938 (while (looking-at (concat " \\(.*\\)\n" ;Name.
1939 ;; Size ; Packed.
1940 " +\\([0-9]+\\) +[0-9]+"
1941 ;; Ratio ; Date'
1942 " +\\([0-9%]+\\) +\\([-0-9]+\\)"
1943 ;; Time ; Attr.
1944 " +\\([0-9:]+\\) +[^ \n]\\{6,10\\}"
1945 ;; CRC; Meth ; Var.
1946 " +[0-9A-F]+ +[^ \n]+ +[0-9.]+\n"))
1947 (goto-char (match-end 0))
1948 (let ((name (match-string 1))
1949 (size (match-string 2)))
1950 (if (> (length name) maxname) (setq maxname (length name)))
1951 (if (> (length size) maxsize) (setq maxsize (length size)))
1952 (push (vector name name nil nil
1953 ;; Size, Ratio.
1954 size (match-string 3)
1955 ;; Date, Time.
1956 (match-string 4) (match-string 5))
1957 files))))
1958 (setq files (nreverse files))
1959 (goto-char (point-min))
1960 (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
1961 (sep (format format "--------" "-----" (make-string maxsize ?-)
1962 "-----" ""))
1963 (column (length sep)))
1964 (insert (format format " Date " "Time " "Size " "Ratio" " Filename") "\n")
1965 (insert sep (make-string maxname ?-) "\n")
1966 (archive-summarize-files (mapcar (lambda (desc)
1967 (let ((text
1968 (format format
1969 (aref desc 6)
1970 (aref desc 7)
1971 (aref desc 4)
1972 (aref desc 5)
1973 (aref desc 1))))
1974 (vector text
1975 column
1976 (length text))))
1977 files))
1978 (insert sep (make-string maxname ?-) "\n")
1979 (apply 'vector files))))
1980
1981 (defun archive-rar-extract (archive name)
1982 ;; unrar-free seems to have no way to extract to stdout or even to a file.
1983 (if (file-name-absolute-p name)
1984 ;; The code below assumes the name is relative and may do undesirable
1985 ;; things otherwise.
1986 (error "Can't extract files with non-relative names")
1987 (let ((dest (make-temp-file "arc-rar" 'dir)))
1988 (unwind-protect
1989 (progn
1990 (call-process "unrar-free" nil nil nil
1991 "--extract" archive name dest)
1992 (insert-file-contents-literally (expand-file-name name dest)))
1993 (delete-file (expand-file-name name dest))
1994 (while (file-name-directory name)
1995 (setq name (directory-file-name (file-name-directory name)))
1996 (delete-directory (expand-file-name name dest)))
1997 (delete-directory dest)))))
1998
1999 ;;; Section: Rar self-extracting .exe archives.
2000
2001 (defun archive-rar-exe-summarize ()
2002 (let ((tmpfile (make-temp-file "rarexe")))
2003 (unwind-protect
2004 (progn
2005 (goto-char (point-min))
2006 (re-search-forward "Rar!")
2007 (write-region (match-beginning 0) (point-max) tmpfile)
2008 (archive-rar-summarize tmpfile))
2009 (delete-file tmpfile))))
2010
2011 (defun archive-rar-exe-extract (archive name)
2012 (let* ((tmpfile (make-temp-file "rarexe"))
2013 (buf (find-buffer-visiting archive))
2014 (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
2015 (unwind-protect
2016 (progn
2017 (with-current-buffer (or buf tmpbuf)
2018 (save-excursion
2019 (save-restriction
2020 (if buf
2021 ;; point-max unwidened is assumed to be the end of the
2022 ;; summary text and the beginning of the actual file data.
2023 (progn (goto-char (point-max)) (widen))
2024 (insert-file-contents-literally archive)
2025 (goto-char (point-min)))
2026 (re-search-forward "Rar!")
2027 (write-region (match-beginning 0) (point-max) tmpfile))))
2028 (archive-rar-extract tmpfile name))
2029 (if tmpbuf (kill-buffer tmpbuf))
2030 (delete-file tmpfile))))
2031
2032 ;; -------------------------------------------------------------------------
2033 ;;; Section: 7z Archives
2034
2035 (defun archive-7z-summarize ()
2036 (let ((maxname 10)
2037 (maxsize 5)
2038 (file buffer-file-name)
2039 (files ()))
2040 (with-temp-buffer
2041 (call-process "7z" nil t nil "l" "-slt" file)
2042 (goto-char (point-min))
2043 (re-search-forward "^-+\n")
2044 (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
2045 (goto-char (match-end 0))
2046 (let ((name (match-string 1))
2047 (size (save-excursion
2048 (and (re-search-forward "^Size = \\(.*\\)\n")
2049 (match-string 1))))
2050 (time (save-excursion
2051 (and (re-search-forward "^Modified = \\(.*\\)\n")
2052 (match-string 1)))))
2053 (if (> (length name) maxname) (setq maxname (length name)))
2054 (if (> (length size) maxsize) (setq maxsize (length size)))
2055 (push (vector name name nil nil time nil nil size)
2056 files))))
2057 (setq files (nreverse files))
2058 (goto-char (point-min))
2059 (let* ((format (format " %%%ds %%s %%s" maxsize))
2060 (sep (format format (make-string maxsize ?-) "-------------------" ""))
2061 (column (length sep)))
2062 (insert (format format "Size " "Date Time " " Filename") "\n")
2063 (insert sep (make-string maxname ?-) "\n")
2064 (archive-summarize-files (mapcar (lambda (desc)
2065 (let ((text
2066 (format format
2067 (aref desc 7)
2068 (aref desc 4)
2069 (aref desc 1))))
2070 (vector text
2071 column
2072 (length text))))
2073 files))
2074 (insert sep (make-string maxname ?-) "\n")
2075 (apply 'vector files))))
2076
2077 (defun archive-7z-extract (archive name)
2078 (let ((tmpfile (make-temp-file "7z-stderr")))
2079 ;; 7z doesn't provide a `quiet' option to suppress non-essential
2080 ;; stderr messages. So redirect stderr to a temp file and display it
2081 ;; in the echo area when it contains error messages.
2082 (prog1 (archive-extract-by-stdout
2083 archive name archive-7z-extract tmpfile)
2084 (with-temp-buffer
2085 (insert-file-contents tmpfile)
2086 (unless (search-forward "Everything is Ok" nil t)
2087 (message "%s" (buffer-string)))
2088 (delete-file tmpfile)))))
2089
2090 ;; -------------------------------------------------------------------------
2091 ;;; Section `ar' archives.
2092
2093 ;; TODO: we currently only handle the basic format of ar archives,
2094 ;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
2095 ;; for .deb packages.
2096
2097 (autoload 'tar-grind-file-mode "tar-mode")
2098
2099 (defconst archive-ar-file-header-re
2100 "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
2101
2102 (defun archive-ar-summarize ()
2103 ;; File is used internally for `archive-rar-exe-summarize'.
2104 (let* ((maxname 10)
2105 (maxtime 16)
2106 (maxuser 5)
2107 (maxgroup 5)
2108 (maxmode 8)
2109 (maxsize 5)
2110 (files ()))
2111 (goto-char (point-min))
2112 (search-forward "!<arch>\n")
2113 (while (looking-at archive-ar-file-header-re)
2114 (let ((name (match-string 1))
2115 extname
2116 ;; Emacs will automatically use float here because those
2117 ;; timestamps don't fit in our ints.
2118 (time (string-to-number (match-string 2)))
2119 (user (match-string 3))
2120 (group (match-string 4))
2121 (mode (string-to-number (match-string 5) 8))
2122 (size (string-to-number (match-string 6))))
2123 ;; Move to the beginning of the data.
2124 (goto-char (match-end 0))
2125 (setq time
2126 (format-time-string
2127 "%Y-%m-%d %H:%M"
2128 (let ((high (truncate (/ time 65536))))
2129 (list high (truncate (- time (* 65536.0 high)))))))
2130 (setq extname
2131 (cond ((equal name "// ")
2132 (propertize ".<ExtNamesTable>." 'face 'italic))
2133 ((equal name "/ ")
2134 (propertize ".<LookupTable>." 'face 'italic))
2135 ((string-match "/? *\\'" name)
2136 (substring name 0 (match-beginning 0)))))
2137 (setq user (substring user 0 (string-match " +\\'" user)))
2138 (setq group (substring group 0 (string-match " +\\'" group)))
2139 (setq mode (tar-grind-file-mode mode))
2140 ;; Move to the end of the data.
2141 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1))
2142 (setq size (number-to-string size))
2143 (if (> (length name) maxname) (setq maxname (length name)))
2144 (if (> (length time) maxtime) (setq maxtime (length time)))
2145 (if (> (length user) maxuser) (setq maxuser (length user)))
2146 (if (> (length group) maxgroup) (setq maxgroup (length group)))
2147 (if (> (length mode) maxmode) (setq maxmode (length mode)))
2148 (if (> (length size) maxsize) (setq maxsize (length size)))
2149 (push (vector name extname nil mode
2150 time user group size)
2151 files)))
2152 (setq files (nreverse files))
2153 (goto-char (point-min))
2154 (let* ((format (format "%%%ds %%%ds/%%-%ds %%%ds %%%ds %%s"
2155 maxmode maxuser maxgroup maxsize maxtime))
2156 (sep (format format (make-string maxmode ?-)
2157 (make-string maxuser ?-)
2158 (make-string maxgroup ?-)
2159 (make-string maxsize ?-)
2160 (make-string maxtime ?-) ""))
2161 (column (length sep)))
2162 (insert (format format " Mode " "User" "Group" " Size "
2163 " Date " "Filename")
2164 "\n")
2165 (insert sep (make-string maxname ?-) "\n")
2166 (archive-summarize-files (mapcar (lambda (desc)
2167 (let ((text
2168 (format format
2169 (aref desc 3)
2170 (aref desc 5)
2171 (aref desc 6)
2172 (aref desc 7)
2173 (aref desc 4)
2174 (aref desc 1))))
2175 (vector text
2176 column
2177 (length text))))
2178 files))
2179 (insert sep (make-string maxname ?-) "\n")
2180 (apply 'vector files))))
2181
2182 (defun archive-ar-extract (archive name)
2183 (let ((destbuf (current-buffer))
2184 (archivebuf (find-file-noselect archive))
2185 (from nil) size)
2186 (with-current-buffer archivebuf
2187 (save-restriction
2188 ;; We may be in archive-mode or not, so either with or without
2189 ;; narrowing and with or without a prepended summary.
2190 (save-excursion
2191 (widen)
2192 (search-forward "!<arch>\n")
2193 (while (and (not from) (looking-at archive-ar-file-header-re))
2194 (let ((this (match-string 1)))
2195 (setq size (string-to-number (match-string 6)))
2196 (goto-char (match-end 0))
2197 (if (equal name this)
2198 (setq from (point))
2199 ;; Move to the end of the data.
2200 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1)))))
2201 (when from
2202 (set-buffer-multibyte nil)
2203 (with-current-buffer destbuf
2204 ;; Do it within the `widen'.
2205 (insert-buffer-substring archivebuf from (+ from size)))
2206 (set-buffer-multibyte 'to)
2207 ;; Inform the caller that the call succeeded.
2208 t))))))
2209
2210 ;; -------------------------------------------------------------------------
2211 ;; This line was a mistake; it is kept now for compatibility.
2212 ;; rms 15 Oct 98
2213 (provide 'archive-mode)
2214
2215 (provide 'arc-mode)
2216
2217 ;;; arc-mode.el ends here