(archive-mode, archive-extract, archive-check-dos)
[bpt/emacs.git] / lisp / arc-mode.el
CommitLineData
665211a3
KH
1;;; arc-mode.el --- simple editing of archives
2
b578f267 3;; Copyright (C) 1995 Free Software Foundation, Inc.
665211a3
KH
4
5;; Author: Morten Welinder (terra@diku.dk)
665211a3
KH
6;; Keywords: archives msdog editing major-mode
7;; Favourite-brand-of-beer: None, I hate beer.
8
b578f267
EN
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
665211a3
KH
25
26;;; Commentary:
b578f267 27
665211a3
KH
28;; NAMING: "arc" is short for "archive" and does not refer specifically
29;; to files whose name end in ".arc"
30;;
31;; This code does not decode any files internally, although it does
32;; understand the directory level of the archives. For this reason,
33;; you should expect this code to need more fiddling than tar-mode.el
34;; (although it at present has fewer bugs :-) In particular, I have
35;; not tested this under Ms-Dog myself.
36;; -------------------------------------
37;; INTERACTION: arc-mode.el should play together with
38;;
39;; * ange-ftp.el: Remote archives (i.e., ones that ange-ftp has brought
40;; to you) are handled by doing all updates on a local
41;; copy. When you make changes to a remote file the
42;; changes will first take effect when the archive buffer
43;; is saved. You will be warned about this.
44;;
45;; * dos-fns.el: (Part of Emacs 19). You get automatic ^M^J <--> ^J
46;; conversion.
47;;
48;; arc-mode.el does not work well with crypt++.el; for the archives as
49;; such this could be fixed (but wouldn't be useful) by declaring such
50;; archives to be "remote". For the members this is a general Emacs
51;; problem that 19.29's file formats may fix.
52;; -------------------------------------
53;; ARCHIVE TYPES: Currently only the archives below are handled, but the
54;; structure for handling just about anything is in place.
55;;
56;; Arc Lzh Zip Zoo
57;; --------------------------------
58;; View listing Intern Intern Intern Intern
59;; Extract member Y Y Y Y
60;; Save changed member Y Y Y Y
61;; Add new member N N N N
62;; Delete member Y Y Y Y
63;; Rename member Y Y N N
64;; Chmod - Y Y -
65;; Chown - Y - -
66;; Chgrp - Y - -
67;;
68;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
69;; on the first released version of this package.
70;;
71;; This code is partly based on tar-mode.el from Emacs.
72;; -------------------------------------
73;; ARCHIVE STRUCTURES:
74;; (This is mostly for myself.)
75;;
76;; ARC A series of (header,file). No interactions among members.
77;;
78;; LZH A series of (header,file). Headers are checksummed. No
79;; interaction among members.
80;;
81;; ZIP A series of (lheader,fil) followed by a "central directory"
82;; which is a series of (cheader) followed by an end-of-
83;; central-dir record possibly followed by junk. The e-o-c-d
84;; links to c-d. cheaders link to lheaders which are basically
85;; cut-down versions of the cheaders.
86;;
87;; ZOO An archive header followed by a series of (header,file).
88;; Each member header points to the next. The archive is
89;; terminated by a bogus header with a zero next link.
90;; -------------------------------------
665211a3
KH
91;; HOOKS: `foo' means one the the supported archive types.
92;;
93;; archive-mode-hook
94;; archive-foo-mode-hook
95;; archive-extract-hooks
96
97;;; Code:
98
99;; -------------------------------------------------------------------------
100;; Section: Configuration.
101
102(defvar archive-dos-members t
eeba65ed 103 "*If non-nil then recognize member files using ^M^J as line terminator.")
665211a3
KH
104
105(defvar archive-tmpdir
106 (expand-file-name
107 (make-temp-name (if (eq system-type 'ms-dos) "ar" "archive.tmp"))
108 (or (getenv "TMPDIR") (getenv "TMP") "/tmp"))
109 "*Directory for temporary files made by arc-mode.el")
110
6eb236e7 111(defvar archive-remote-regexp "^/[^/:]*[^/:.]:"
eeba65ed
RS
112 "*Regexp recognizing archive files names that are not local.
113A non-local file is one whose file name is not proper outside Emacs.
b884fd7e 114A local copy of the archive will be used when updating.")
665211a3
KH
115
116(defvar archive-extract-hooks nil
117 "*Hooks to run when an archive member has been extracted.")
118;; ------------------------------
119;; Arc archive configuration
120
121;; We always go via a local file since there seems to be no reliable way
122;; to extract to stdout without junk getting added.
123(defvar archive-arc-extract
124 '("arc" "x")
eeba65ed
RS
125 "*Program and its options to run in order to extract an arc file member.
126Extraction should happen to the current directory. Archive and member
127name will be added.")
665211a3
KH
128
129(defvar archive-arc-expunge
130 '("arc" "d")
131 "*Program and its options to run in order to delete arc file members.
132Archive and member names will be added.")
133
134(defvar archive-arc-write-file-member
135 '("arc" "u")
136 "*Program and its options to run in order to update an arc file member.
137Archive and member name will be added.")
138;; ------------------------------
139;; Lzh archive configuration
140
141(defvar archive-lzh-extract
142 '("lha" "pq")
eeba65ed
RS
143 "*Program and its options to run in order to extract an lzh file member.
144Extraction should happen to standard output. Archive and member name will
145be added.")
665211a3
KH
146
147(defvar archive-lzh-expunge
148 '("lha" "d")
149 "*Program and its options to run in order to delete lzh file members.
150Archive and member names will be added.")
151
152(defvar archive-lzh-write-file-member
153 '("lha" "a")
154 "*Program and its options to run in order to update an lzh file member.
155Archive and member name will be added.")
156;; ------------------------------
157;; Zip archive configuration
158
159(defvar archive-zip-use-pkzip (memq system-type '(ms-dos windows-nt))
eeba65ed
RS
160 "*If non-nil then pkzip option are used instead of zip options.
161Only set to true for msdog systems!")
665211a3
KH
162
163(defvar archive-zip-extract
164 (if archive-zip-use-pkzip '("pkunzip" "-e") '("unzip" "-qq" "-c"))
eeba65ed
RS
165 "*Program and its options to run in order to extract a zip file member.
166Extraction should happen to standard output. Archive and member name will
167be added. If `archive-zip-use-pkzip' is non-nil then this program is
168expected to extract to a file junking the directory part of the name.")
665211a3 169
e946e18c 170;; For several reasons the latter behaviour is not desirable in general.
665211a3
KH
171;; (1) It uses more disk space. (2) Error checking is worse or non-
172;; existent. (3) It tends to do funny things with other systems' file
173;; names.
174
175(defvar archive-zip-expunge
176 (if archive-zip-use-pkzip '("pkzip" "-d") '("zip" "-d" "-q"))
177 "*Program and its options to run in order to delete zip file members.
178Archive and member names will be added.")
179
180(defvar archive-zip-update
181 (if archive-zip-use-pkzip '("pkzip" "-u") '("zip" "-q"))
182 "*Program and its options to run in order to update a zip file member.
183Options should ensure that specified directory will be put into the zip
184file. Archive and member name will be added.")
185
186(defvar archive-zip-update-case
187 (if archive-zip-use-pkzip archive-zip-update '("zip" "-q" "-k"))
eeba65ed
RS
188 "*Program and its options to run in order to update a case fiddled zip member.
189Options should ensure that specified directory will be put into the zip file.
190Archive and member name will be added.")
665211a3
KH
191
192(defvar archive-zip-case-fiddle t
eeba65ed
RS
193 "*If non-nil then zip file members are case fiddled.
194Case fiddling will only happen for members created by a system that
195uses caseless file names.")
665211a3
KH
196;; ------------------------------
197;; Zoo archive configuration
198
199(defvar archive-zoo-extract
200 '("zoo" "xpq")
eeba65ed
RS
201 "*Program and its options to run in order to extract a zoo file member.
202Extraction should happen to standard output. Archive and member name will
203be added.")
665211a3
KH
204
205(defvar archive-zoo-expunge
206 '("zoo" "DqPP")
207 "*Program and its options to run in order to delete zoo file members.
208Archive and member names will be added.")
209
210(defvar archive-zoo-write-file-member
211 '("zoo" "a")
212 "*Program and its options to run in order to update a zoo file member.
213Archive and member name will be added.")
214;; -------------------------------------------------------------------------
215;; Section: Variables
216
217(defvar archive-subtype nil "*Symbol describing archive type.")
218(defvar archive-file-list-start nil "*Position of first contents line.")
219(defvar archive-file-list-end nil "*Position just after last contents line.")
220(defvar archive-proper-file-start nil "*Position of real archive's start.")
221(defvar archive-read-only nil "*Non-nil if the archive is read-only on disk.")
222(defvar archive-remote nil "*Non-nil if the archive is outside file system.")
223(defvar archive-local-name nil "*Name of local copy of remote archive.")
224(defvar archive-mode-map nil "*Local keymap for archive mode listings.")
225(defvar archive-file-name-indent nil "*Column where file names start.")
226
227(defvar archive-alternate-display nil
228 "*Non-nil when alternate information is shown.")
229(make-variable-buffer-local 'archive-alternate-display)
230(put 'archive-alternate-display 'permanent-local t)
231
232(defvar archive-superior-buffer nil "*In archive members, points to archive.")
233(put 'archive-superior-buffer 'permanent-local t)
234
235(defvar archive-subfile-mode nil "*Non-nil in archive member buffers.")
236(make-variable-buffer-local 'archive-subfile-mode)
237(put 'archive-subfile-mode 'permanent-local t)
238
665211a3 239(defvar archive-subfile-dos nil
ab889912 240 "Negation of `buffer-file-type', which see.")
665211a3
KH
241(make-variable-buffer-local 'archive-subfile-dos)
242(put 'archive-subfile-dos 'permanent-local t)
243
244(defvar archive-files nil "Vector of file descriptors. Each descriptor is
245a vector of [ext-file-name int-file-name case-fiddled mode ...]")
246(make-variable-buffer-local 'archive-files)
43f657ea
KH
247
248(defvar archive-lemacs
249 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version)
250 "*Non-nil when running under under Lucid Emacs or Xemacs.")
665211a3
KH
251;; -------------------------------------------------------------------------
252;; Section: Support functions.
253
254(defsubst archive-name (suffix)
255 (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
256
257(defun archive-l-e (str &optional len)
eeba65ed
RS
258 "Convert little endian string/vector to integer.
259Alternatively, first argument may be a buffer position in the current buffer
260in which case a second argument, length, should be supplied."
665211a3
KH
261 (if (stringp str)
262 (setq len (length str))
263 (setq str (buffer-substring str (+ str len))))
264 (let ((result 0)
265 (i 0))
266 (while (< i len)
267 (setq i (1+ i)
268 result (+ (ash result 8) (aref str (- len i)))))
269 result))
270
271(defun archive-int-to-mode (mode)
272 "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------"
273 (let ((str (make-string 10 ?-)))
274 (or (zerop (logand 16384 mode)) (aset str 0 ?d))
275 (or (zerop (logand 8192 mode)) (aset str 0 ?c)) ; completeness
276 (or (zerop (logand 256 mode)) (aset str 1 ?r))
277 (or (zerop (logand 128 mode)) (aset str 2 ?w))
278 (or (zerop (logand 64 mode)) (aset str 3 ?x))
279 (or (zerop (logand 32 mode)) (aset str 4 ?r))
280 (or (zerop (logand 16 mode)) (aset str 5 ?w))
281 (or (zerop (logand 8 mode)) (aset str 6 ?x))
282 (or (zerop (logand 4 mode)) (aset str 7 ?r))
283 (or (zerop (logand 2 mode)) (aset str 8 ?w))
284 (or (zerop (logand 1 mode)) (aset str 9 ?x))
285 (or (zerop (logand 1024 mode)) (aset str 3 (if (zerop (logand 64 mode))
286 ?S ?s)))
287 (or (zerop (logand 2048 mode)) (aset str 6 (if (zerop (logand 8 mode))
288 ?S ?s)))
289 str))
290
291(defun archive-calc-mode (oldmode newmode &optional error)
eeba65ed 292 "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
665211a3
KH
293NEWMODE may be an octal number including a leading zero in which case it
294will become the new mode.\n
295NEWMODE may also be a relative specification like \"og-rwx\" in which case
296OLDMODE will be modified accordingly just like chmod(2) would have done.\n
297If optional third argument ERROR is non-nil an error will be signaled if
298the mode is invalid. If ERROR is nil then nil will be returned."
299 (cond ((string-match "^0[0-7]*$" newmode)
300 (let ((result 0)
301 (len (length newmode))
302 (i 1))
303 (while (< i len)
304 (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
305 i (1+ i)))
306 (logior (logand oldmode 65024) result)))
307 ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
308 (let ((who 0)
309 (result oldmode)
310 (op (aref newmode (match-beginning 2)))
311 (bits 0)
312 (i (match-beginning 3)))
313 (while (< i (match-end 3))
314 (let ((rwx (aref newmode i)))
315 (setq bits (logior bits (cond ((= rwx ?r) 292)
316 ((= rwx ?w) 146)
317 ((= rwx ?x) 73)
318 ((= rwx ?s) 3072)
319 ((= rwx ?t) 512)))
320 i (1+ i))))
321 (while (< who (match-end 1))
322 (let* ((whoc (aref newmode who))
323 (whomask (cond ((= whoc ?a) 4095)
324 ((= whoc ?u) 1472)
325 ((= whoc ?g) 2104)
326 ((= whoc ?o) 7))))
327 (if (= op ?=)
328 (setq result (logand result (lognot whomask))))
329 (if (= op ?-)
330 (setq result (logand result (lognot (logand whomask bits))))
331 (setq result (logior result (logand whomask bits)))))
332 (setq who (1+ who)))
333 result))
334 (t
335 (if error
336 (error "Invalid mode specification: %s" newmode)))))
337
338(defun archive-dosdate (date)
339 "Stringify dos packed DATE record."
340 (let ((year (+ 1980 (logand (ash date -9) 127)))
341 (month (logand (ash date -5) 15))
342 (day (logand date 31)))
343 (if (or (> month 12) (< month 1))
344 ""
345 (format "%2d-%s-%d"
346 day
347 (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
348 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
349 year))))
350
351(defun archive-dostime (time)
352 "Stringify dos packed TIME record."
353 (let ((hour (logand (ash time -11) 31))
354 (minute (logand (ash time -5) 53))
355 (second (* 2 (logand time 31)))) ; 2 seconds resolution
356 (format "%02d:%02d:%02d" hour minute second)))
357
358;;(defun archive-unixdate (low high)
359;; "Stringify unix (LOW HIGH) date."
360;; (let ((str (current-time-string (cons high low))))
361;; (format "%s-%s-%s"
362;; (substring str 8 9)
363;; (substring str 4 7)
364;; (substring str 20 24))))
365
366;;(defun archive-unixtime (low high)
367;; "Stringify unix (LOW HIGH) time."
368;; (let ((str (current-time-string (cons high low))))
369;; (substring str 11 19)))
370
371(defun archive-get-lineno ()
372 (if (>= (point) archive-file-list-start)
373 (count-lines archive-file-list-start
374 (save-excursion (beginning-of-line) (point)))
375 0))
376
377(defun archive-get-descr (&optional noerror)
eeba65ed
RS
378 "Return the descriptor vector for file at point.
379Does not signal an error if optional second argument NOERROR is non-nil."
665211a3
KH
380 (let ((no (archive-get-lineno)))
381 (if (and (>= (point) archive-file-list-start)
382 (< no (length archive-files)))
383 (let ((item (aref archive-files no)))
384 (if (vectorp item)
385 item
386 (if (not noerror)
387 (error "Entry is not a regular member of the archive"))))
388 (if (not noerror)
389 (error "Line does not describe a member of the archive")))))
390;; -------------------------------------------------------------------------
391;; Section: the mode definition
392
9199a670 393;;;###autoload
665211a3 394(defun archive-mode (&optional force)
eeba65ed
RS
395 "Major mode for viewing an archive file in a dired-like way.
396You can move around using the usual cursor motion commands.
665211a3
KH
397Letters no longer insert themselves.
398Type `e' to pull a file out of the archive and into its own buffer;
399or click mouse-2 on the file's line in the archive mode buffer.
400
401If you edit a sub-file of this archive (as with the `e' command) and
402save it, the contents of that buffer will be saved back into the
403archive.
404
405\\{archive-mode-map}"
406 ;; This is not interactive because you shouldn't be turning this
407 ;; mode on and off. You can corrupt things that way.
408 (if (zerop (buffer-size))
409 ;; At present we cannot create archives from scratch
410 (funcall default-major-mode)
411 (if (and (not force) archive-files) nil
412 (let* ((type (archive-find-type))
413 (typename (copy-sequence (symbol-name type))))
414 (aset typename 0 (upcase (aref typename 0)))
415 (kill-all-local-variables)
416 (make-local-variable 'archive-subtype)
417 (setq archive-subtype type)
418
419 ;; Buffer contains treated image of file before the file contents
420 (make-local-variable 'revert-buffer-function)
421 (setq revert-buffer-function 'archive-mode-revert)
422 (auto-save-mode 0)
423 (make-local-variable 'local-write-file-hooks)
424 (add-hook 'local-write-file-hooks 'archive-write-file)
425
426 ;; Real file contents is binary
427 (make-local-variable 'require-final-newline)
428 (setq require-final-newline nil)
429 (make-local-variable 'enable-local-variables)
430 (setq enable-local-variables nil)
ab889912
RS
431 (if (boundp 'default-buffer-file-type)
432 (setq buffer-file-type t))
665211a3
KH
433
434 (make-local-variable 'archive-read-only)
435 (setq archive-read-only (not (file-writable-p (buffer-file-name))))
436
437 ;; Should we use a local copy when accessing from outside Emacs?
438 (make-local-variable 'archive-local-name)
439 (make-local-variable 'archive-remote)
440 (setq archive-remote (string-match archive-remote-regexp
441 (buffer-file-name)))
442
443 (setq major-mode 'archive-mode)
444 (setq mode-name (concat typename "-Archive"))
445 ;; Run archive-foo-mode-hook and archive-mode-hook
446 (run-hooks (archive-name "mode-hook") 'archive-mode-hook)
447 (use-local-map archive-mode-map))
448
449 (make-local-variable 'archive-proper-file-start)
450 (make-local-variable 'archive-file-list-start)
451 (make-local-variable 'archive-file-list-end)
452 (make-local-variable 'archive-file-name-indent)
453 (archive-summarize)
454 (setq buffer-read-only t))))
455
456;; Archive mode is suitable only for specially formatted data.
457(put 'archive-mode 'mode-class 'special)
458;; -------------------------------------------------------------------------
459;; Section: Key maps
460
461(if archive-mode-map nil
462 (setq archive-mode-map (make-keymap))
463 (suppress-keymap archive-mode-map)
464 (define-key archive-mode-map " " 'archive-next-line)
465 (define-key archive-mode-map "a" 'archive-alternate-display)
466 ;;(define-key archive-mode-map "c" 'archive-copy)
467 (define-key archive-mode-map "d" 'archive-flag-deleted)
468 (define-key archive-mode-map "\C-d" 'archive-flag-deleted)
469 (define-key archive-mode-map "e" 'archive-extract)
470 (define-key archive-mode-map "f" 'archive-extract)
471 (define-key archive-mode-map "\C-m" 'archive-extract)
665211a3
KH
472 (define-key archive-mode-map "g" 'revert-buffer)
473 (define-key archive-mode-map "h" 'describe-mode)
474 (define-key archive-mode-map "m" 'archive-mark)
475 (define-key archive-mode-map "n" 'archive-next-line)
476 (define-key archive-mode-map "\C-n" 'archive-next-line)
477 (define-key archive-mode-map [down] 'archive-next-line)
478 (define-key archive-mode-map "o" 'archive-extract-other-window)
479 (define-key archive-mode-map "p" 'archive-previous-line)
480 (define-key archive-mode-map "\C-p" 'archive-previous-line)
481 (define-key archive-mode-map [up] 'archive-previous-line)
482 (define-key archive-mode-map "r" 'archive-rename-entry)
483 (define-key archive-mode-map "u" 'archive-unflag)
484 (define-key archive-mode-map "\M-\C-?" 'archive-unmark-all-files)
485 (define-key archive-mode-map "v" 'archive-view)
486 (define-key archive-mode-map "x" 'archive-expunge)
487 (define-key archive-mode-map "\177" 'archive-unflag-backwards)
488 (define-key archive-mode-map "E" 'archive-extract-other-window)
489 (define-key archive-mode-map "M" 'archive-chmod-entry)
490 (define-key archive-mode-map "G" 'archive-chgrp-entry)
491 (define-key archive-mode-map "O" 'archive-chown-entry)
43f657ea
KH
492
493 (if archive-lemacs
494 (progn
495 ;; Not a nice "solution" but it'll have to do
496 (define-key archive-mode-map "\C-xu" 'archive-undo)
497 (define-key archive-mode-map "\C-_" 'archive-undo))
498 (substitute-key-definition 'undo 'archive-undo
499 archive-mode-map global-map))
500
501 (define-key archive-mode-map
502 (if archive-lemacs 'button2 [mouse-2]) 'archive-mouse-extract)
503
504 (if archive-lemacs
505 () ; out of luck
506 ;; Get rid of the Edit menu bar item to save space.
507 (define-key archive-mode-map [menu-bar edit] 'undefined)
508
509 (define-key archive-mode-map [menu-bar immediate]
510 (cons "Immediate" (make-sparse-keymap "Immediate")))
511 (define-key archive-mode-map [menu-bar immediate alternate]
512 '("Alternate Display" . archive-alternate-display))
513 (put 'archive-alternate-display 'menu-enable
514 '(boundp (archive-name "alternate-display")))
515 (define-key archive-mode-map [menu-bar immediate view]
516 '("View This File" . archive-view))
517 (define-key archive-mode-map [menu-bar immediate display]
518 '("Display in Other Window" . archive-display-other-window))
519 (define-key archive-mode-map [menu-bar immediate find-file-other-window]
520 '("Find in Other Window" . archive-extract-other-window))
521 (define-key archive-mode-map [menu-bar immediate find-file]
522 '("Find This File" . archive-extract))
523
524 (define-key archive-mode-map [menu-bar mark]
525 (cons "Mark" (make-sparse-keymap "Mark")))
526 (define-key archive-mode-map [menu-bar mark unmark-all]
527 '("Unmark All" . archive-unmark-all-files))
528 (define-key archive-mode-map [menu-bar mark deletion]
529 '("Flag" . archive-flag-deleted))
530 (define-key archive-mode-map [menu-bar mark unmark]
531 '("Unflag" . archive-unflag))
532 (define-key archive-mode-map [menu-bar mark mark]
533 '("Mark" . archive-mark))
534
535 (define-key archive-mode-map [menu-bar operate]
536 (cons "Operate" (make-sparse-keymap "Operate")))
537 (define-key archive-mode-map [menu-bar operate chown]
538 '("Change Owner..." . archive-chown-entry))
539 (put 'archive-chown-entry 'menu-enable
540 '(fboundp (archive-name "chown-entry")))
541 (define-key archive-mode-map [menu-bar operate chgrp]
542 '("Change Group..." . archive-chgrp-entry))
543 (put 'archive-chgrp-entry 'menu-enable
544 '(fboundp (archive-name "chgrp-entry")))
545 (define-key archive-mode-map [menu-bar operate chmod]
546 '("Change Mode..." . archive-chmod-entry))
547 (put 'archive-chmod-entry 'menu-enable
548 '(fboundp (archive-name "chmod-entry")))
549 (define-key archive-mode-map [menu-bar operate rename]
550 '("Rename to..." . archive-rename-entry))
551 (put 'archive-rename-entry 'menu-enable
552 '(fboundp (archive-name "rename-entry")))
553 ;;(define-key archive-mode-map [menu-bar operate copy]
554 ;; '("Copy to..." . archive-copy))
555 (define-key archive-mode-map [menu-bar operate expunge]
556 '("Expunge Marked Files" . archive-expunge))
557 ))
665211a3
KH
558
559(let* ((item1 '(archive-subfile-mode " Archive"))
560 (item2 '(archive-subfile-dos " Dos"))
561 (items (if (memq system-type '(ms-dos windows-nt))
562 (list item1) ; msdog has its own indicator
563 (list item1 item2))))
564 (or (member item1 minor-mode-alist)
565 (setq minor-mode-alist (append items minor-mode-alist))))
566;; -------------------------------------------------------------------------
567(defun archive-find-type ()
568 (widen)
569 (goto-char (point-min))
570 ;; The funny [] here make it unlikely that the .elc file will be treated
571 ;; as an archive by other software.
572 (let (case-fold-search)
573 (cond ((looking-at "[P]K\003\004") 'zip)
574 ((looking-at "..-l[hz][0-9]-") 'lzh)
575 ((looking-at "....................[\334]\247\304\375") 'zoo)
576 ((and (looking-at "\C-z") ; signature too simple, IMHO
577 (string-match "\\.[aA][rR][cC]$"
578 (or buffer-file-name (buffer-name))))
579 'arc)
580 (t (error "Buffer format not recognized.")))))
581;; -------------------------------------------------------------------------
582(defun archive-summarize ()
583 "Parse the contents of the archive file in the current buffer.
584Place a dired-like listing on the front;
585then narrow to it, so that only that listing
586is visible (and the real data of the buffer is hidden)."
587 (widen)
588 (let (buffer-read-only)
589 (message "Parsing archive file...")
590 (buffer-disable-undo (current-buffer))
591 (setq archive-files (funcall (archive-name "summarize")))
592 (message "Parsing archive file...done.")
593 (setq archive-proper-file-start (point-marker))
594 (narrow-to-region (point-min) (point))
595 (set-buffer-modified-p nil)
596 (buffer-enable-undo))
597 (goto-char archive-file-list-start)
598 (archive-next-line 0))
599
600(defun archive-resummarize ()
601 "Recreate the contents listing of an archive."
602 (let ((modified (buffer-modified-p))
603 (no (archive-get-lineno))
604 buffer-read-only)
605 (widen)
606 (delete-region (point-min) archive-proper-file-start)
607 (archive-summarize)
608 (set-buffer-modified-p modified)
609 (goto-char archive-file-list-start)
610 (archive-next-line no)))
611
612(defun archive-summarize-files (files)
e946e18c 613 "Insert a description of a list of files annotated with proper mouse face"
665211a3
KH
614 (setq archive-file-list-start (point-marker))
615 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
616 ;; We don't want to do an insert for each element since that takes too
617 ;; long when the archive -- which has to be moved in memory -- is large.
618 (insert
619 (apply
620 (function concat)
621 (mapcar
43f657ea
KH
622 (function
623 (lambda (fil)
624 ;; Using `concat' here copies the text also, so we can add
625 ;; properties without problems.
626 (let ((text (concat (aref fil 0) "\n")))
627 (if archive-lemacs
628 () ; out of luck
629 (put-text-property (aref fil 1) (aref fil 2)
630 'mouse-face 'highlight
631 text))
632 text)))
665211a3
KH
633 files)))
634 (setq archive-file-list-end (point-marker)))
635
636(defun archive-alternate-display ()
eeba65ed
RS
637 "Toggle alternative display.
638To avoid very long lines some archive mode don't show all information.
639This function changes the set of information shown for each files."
665211a3
KH
640 (interactive)
641 (setq archive-alternate-display (not archive-alternate-display))
642 (archive-resummarize))
643;; -------------------------------------------------------------------------
644;; Section: Local archive copy handling
645
646(defun archive-maybe-copy (archive)
647 (if archive-remote
648 (let ((start (point-max)))
649 (setq archive-local-name (expand-file-name
650 (file-name-nondirectory archive)
651 archive-tmpdir))
652 (make-directory archive-tmpdir t)
653 (save-restriction
654 (widen)
655 (write-region start (point-max) archive-local-name nil 'nomessage))
656 archive-local-name)
657 (if (buffer-modified-p) (save-buffer))
658 archive))
659
660(defun archive-maybe-update (unchanged)
661 (if archive-remote
662 (let ((name archive-local-name)
663 (modified (buffer-modified-p))
664 buffer-read-only)
665 (if unchanged nil
666 (erase-buffer)
667 (insert-file-contents name)
668 (archive-mode t))
669 (archive-delete-local name)
670 (if (not unchanged)
671 (message "Archive file must be saved for changes to take effect"))
672 (set-buffer-modified-p (or modified (not unchanged))))))
673
674(defun archive-delete-local (name)
eeba65ed 675 "Delete file NAME and its parents up to and including `archive-tmpdir'."
665211a3
KH
676 (let ((again t)
677 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
678 (condition-case nil
679 (delete-file name)
680 (error nil))
681 (while again
682 (setq name (directory-file-name (file-name-directory name)))
683 (condition-case nil
684 (delete-directory name)
685 (error nil))
686 (if (string= name top) (setq again nil)))))
687;; -------------------------------------------------------------------------
688;; Section: Member extraction
689
690(defun archive-mouse-extract (event)
691 "Extract a file whose name you click on."
692 (interactive "e")
43f657ea
KH
693 (mouse-set-point event)
694 (switch-to-buffer
695 (save-excursion
696 (archive-extract)
697 (current-buffer))))
665211a3
KH
698
699(defun archive-extract (&optional other-window-p)
700 "In archive mode, extract this entry of the archive into its own buffer."
701 (interactive)
702 (let* ((view-p (eq other-window-p 'view))
703 (descr (archive-get-descr))
704 (ename (aref descr 0))
705 (iname (aref descr 1))
706 (archive-buffer (current-buffer))
707 (arcdir default-directory)
708 (archive (buffer-file-name))
709 (arcname (file-name-nondirectory archive))
710 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
711 (extractor (archive-name "extract"))
712 (read-only-p (or archive-read-only view-p))
713 (buffer (get-buffer bufname))
714 (just-created nil))
715 (if buffer
716 nil
717 (setq archive (archive-maybe-copy archive))
718 (setq buffer (get-buffer-create bufname))
719 (setq just-created t)
720 (save-excursion
721 (set-buffer buffer)
722 (setq buffer-file-name
723 (expand-file-name (concat arcname ":" iname)))
724 (setq buffer-file-truename
725 (abbreviate-file-name buffer-file-name))
726 ;; Set the default-directory to the dir of the superior buffer.
727 (setq default-directory arcdir)
728 (make-local-variable 'archive-superior-buffer)
729 (setq archive-superior-buffer archive-buffer)
730 (make-local-variable 'local-write-file-hooks)
731 (add-hook 'local-write-file-hooks 'archive-write-file-member)
732 (setq archive-subfile-mode descr)
ab889912
RS
733 (setq archive-subfile-dos nil)
734 (if (boundp 'default-buffer-file-type)
735 (setq buffer-file-type t))
665211a3
KH
736 (if (fboundp extractor)
737 (funcall extractor archive ename)
738 (archive-*-extract archive ename (symbol-value extractor)))
739 (if archive-dos-members (archive-check-dos))
740 (goto-char (point-min))
741 (rename-buffer bufname)
742 (setq buffer-read-only read-only-p)
743 (setq buffer-undo-list nil)
744 (set-buffer-modified-p nil)
745 (setq buffer-saved-size (buffer-size))
746 (normal-mode)
747 ;; Just in case an archive occurs inside another archive.
748 (if (eq major-mode 'archive-mode)
749 (setq archive-remote t))
750 (run-hooks 'archive-extract-hooks))
751 (archive-maybe-update t))
752 (if view-p
753 (progn
754 (view-buffer buffer)
755 (and just-created (setq view-exit-action 'kill-buffer)))
756 (if (eq other-window-p 'display)
757 (display-buffer buffer)
758 (if other-window-p
759 (switch-to-buffer-other-window buffer)
760 (switch-to-buffer buffer))))))
761
762(defun archive-*-extract (archive name command)
763 (let* ((default-directory (file-name-as-directory archive-tmpdir))
764 (tmpfile (expand-file-name (file-name-nondirectory name)
765 default-directory)))
766 (make-directory (directory-file-name default-directory) t)
767 (apply 'call-process
768 (car command)
769 nil
770 nil
771 nil
772 (append (cdr command) (list archive name)))
773 (insert-file-contents tmpfile)
774 (archive-delete-local tmpfile)))
775
776(defun archive-extract-by-stdout (archive name command)
777 (let ((binary-process-output t)) ; for Ms-Dos
778 (apply 'call-process
779 (car command)
780 nil
781 t
782 nil
783 (append (cdr command) (list archive name)))))
784
785(defun archive-extract-other-window ()
786 "In archive mode, find this member in another window."
787 (interactive)
788 (archive-extract t))
789
790(defun archive-display-other-window ()
791 "In archive mode, display this member in another window."
792 (interactive)
793 (archive-extract 'display))
794
795(defun archive-view ()
796 "In archive mode, view the member on this line."
797 (interactive)
798 (archive-extract 'view))
799
800(defun archive-add-new-member (arcbuf name)
eeba65ed 801 "Add current buffer to the archive in ARCBUF naming it NAME."
665211a3
KH
802 (interactive
803 (list (get-buffer
804 (read-buffer "Buffer containing archive: "
805 ;; Find first archive buffer and suggest that
806 (let ((bufs (buffer-list)))
807 (while (and bufs (not (eq (save-excursion
808 (set-buffer (car bufs))
809 major-mode)
810 'archive-mode)))
811 (setq bufs (cdr bufs)))
812 (if bufs
813 (car bufs)
814 (error "There are no archive buffers")))
815 t))
816 (read-string "File name in archive: "
817 (if buffer-file-name
818 (file-name-nondirectory buffer-file-name)
819 ""))))
820 (save-excursion
821 (set-buffer arcbuf)
822 (or (eq major-mode 'archive-mode)
823 (error "Buffer is not an archive buffer"))
824 (if archive-read-only
825 (error "Archive is read-only")))
826 (if (eq arcbuf (current-buffer))
827 (error "An archive buffer cannot be added to itself"))
828 (if (string= name "")
829 (error "Archive members may not be given empty names"))
830 (let ((func (save-excursion (set-buffer arcbuf)
831 (archive-name "add-new-member")))
832 (membuf (current-buffer)))
833 (if (fboundp func)
834 (save-excursion
835 (set-buffer arcbuf)
836 (funcall func buffer-file-name membuf name))
837 (error "Adding a new member is not supported for this archive type"))))
838;; -------------------------------------------------------------------------
839;; Section: IO stuff
840
841(defun archive-check-dos (&optional force)
eeba65ed 842 "*Possibly handle a buffer with ^M^J terminated lines."
665211a3
KH
843 (save-restriction
844 (widen)
845 (save-excursion
846 (goto-char (point-min))
847 (setq archive-subfile-dos
848 (or force (not (search-forward-regexp "[^\r]\n" nil t))))
ab889912
RS
849 (if (boundp 'default-buffer-file-type)
850 (setq buffer-file-type (not archive-subfile-dos)))
665211a3
KH
851 (if archive-subfile-dos
852 (let ((modified (buffer-modified-p)))
853 (buffer-disable-undo (current-buffer))
854 (goto-char (point-min))
855 (while (search-forward "\r\n" nil t)
856 (replace-match "\n"))
857 (buffer-enable-undo)
858 (set-buffer-modified-p modified))))))
859
860(defun archive-write-file-member ()
861 (if archive-subfile-dos
862 (save-restriction
863 (widen)
864 (save-excursion
865 (goto-char (point-min))
866 ;; We don't want our ^M^J <--> ^J changes to show in the undo list
867 (let ((undo-list buffer-undo-list))
868 (unwind-protect
869 (progn
870 (setq buffer-undo-list t)
871 (while (search-forward "\n" nil t)
872 (replace-match "\r\n"))
873 (setq archive-subfile-dos nil)
ab889912
RS
874 (if (boundp 'default-buffer-file-type)
875 (setq buffer-file-type t))
665211a3
KH
876 ;; OK, we're now have explicit ^M^Js -- save and re-unixfy
877 (archive-write-file-member))
878 (progn
879 (archive-check-dos t)
880 (setq buffer-undo-list undo-list))))
881 t))
882 (save-excursion
883 (save-restriction
884 (message "Updating archive...")
885 (widen)
886 (let ((writer (save-excursion (set-buffer archive-superior-buffer)
887 (archive-name "write-file-member")))
888 (archive (save-excursion (set-buffer archive-superior-buffer)
889 (buffer-file-name))))
890 (if (fboundp writer)
891 (funcall writer archive archive-subfile-mode)
892 (archive-*-write-file-member archive
893 archive-subfile-mode
894 (symbol-value writer))))
895 (set-buffer-modified-p nil)
896 (message "Updating archive...done")
897 (set-buffer archive-superior-buffer)
898 (revert-buffer)
899 t))))
900
901(defun archive-*-write-file-member (archive descr command)
902 (let* ((ename (aref descr 0))
903 (tmpfile (expand-file-name ename archive-tmpdir))
904 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
905 (default-directory (file-name-as-directory top)))
906 (unwind-protect
907 (progn
908 (make-directory (file-name-directory tmpfile) t)
909 (write-region (point-min) (point-max) tmpfile nil 'nomessage)
910 (if (aref descr 3)
911 ;; Set the file modes, but make sure we can read it.
912 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
913 (let ((exitcode (apply 'call-process
914 (car command)
915 nil
916 nil
917 nil
918 (append (cdr command) (list archive ename)))))
919 (if (equal exitcode 0)
920 nil
921 (error "Updating was unsuccessful (%S)" exitcode))))
922 (archive-delete-local tmpfile))))
923
924(defun archive-write-file ()
925 (save-excursion
926 (write-region archive-proper-file-start (point-max) buffer-file-name nil t)
927 (set-buffer-modified-p nil)
928 t))
929;; -------------------------------------------------------------------------
930;; Section: Marking and unmarking.
931
932(defun archive-flag-deleted (p &optional type)
933 "In archive mode, mark this member to be deleted from the archive.
934With a prefix argument, mark that many files."
935 (interactive "p")
936 (or type (setq type ?D))
937 (beginning-of-line)
938 (let ((sign (if (>= p 0) +1 -1))
939 (modified (buffer-modified-p))
940 buffer-read-only)
941 (while (not (zerop p))
942 (if (archive-get-descr t)
943 (progn
944 (delete-char 1)
945 (insert type)))
946 (forward-line sign)
947 (setq p (- p sign)))
948 (set-buffer-modified-p modified))
949 (archive-next-line 0))
950
951(defun archive-unflag (p)
952 "In archive mode, un-mark this member if it is marked to be deleted.
953With a prefix argument, un-mark that many files forward."
954 (interactive "p")
955 (archive-flag-deleted p ? ))
956
957(defun archive-unflag-backwards (p)
958 "In archive mode, un-mark this member if it is marked to be deleted.
959With a prefix argument, un-mark that many members backward."
960 (interactive "p")
961 (archive-flag-deleted (- p) ? ))
962
963(defun archive-unmark-all-files ()
964 "Remove all marks."
965 (interactive)
966 (let ((modified (buffer-modified-p))
967 buffer-read-only)
968 (save-excursion
969 (goto-char archive-file-list-start)
970 (while (< (point) archive-file-list-end)
971 (or (= (following-char) ? )
972 (progn (delete-char 1) (insert ? )))
973 (forward-line 1)))
974 (set-buffer-modified-p modified)))
975
976(defun archive-mark (p)
977 "In archive mode, mark this member for group operations.
978With a prefix argument, mark that many members.
979Use \\[archive-unmark-all-files] to remove all marks."
980 (interactive "p")
981 (archive-flag-deleted p ?*))
982
983(defun archive-get-marked (mark &optional default)
984 (let (files)
985 (save-excursion
986 (goto-char archive-file-list-start)
987 (while (< (point) archive-file-list-end)
988 (if (= (following-char) mark)
989 (setq files (cons (archive-get-descr) files)))
990 (forward-line 1)))
991 (or (nreverse files)
992 (and default
993 (list (archive-get-descr))))))
994;; -------------------------------------------------------------------------
995;; Section: Operate
996
997(defun archive-next-line (p)
998 (interactive "p")
999 (forward-line p)
1000 (or (eobp)
1001 (forward-char archive-file-name-indent)))
1002
1003(defun archive-previous-line (p)
1004 (interactive "p")
1005 (archive-next-line (- p)))
1006
1007(defun archive-chmod-entry (new-mode)
eeba65ed 1008 "Change the protection bits associated with all marked or this member.
665211a3
KH
1009The new protection bits can either be specified as an octal number or
1010as a relative change like \"g+rw\" as for chmod(2)"
1011 (interactive "sNew mode (octal or relative): ")
1012 (if archive-read-only (error "Archive is read-only"))
1013 (let ((func (archive-name "chmod-entry")))
1014 (if (fboundp func)
1015 (progn
1016 (funcall func new-mode (archive-get-marked ?* t))
1017 (archive-resummarize))
1018 (error "Setting mode bits is not supported for this archive type"))))
1019
1020(defun archive-chown-entry (new-uid)
1021 "Change the owner of all marked or this member."
1022 (interactive "nNew uid: ")
1023 (if archive-read-only (error "Archive is read-only"))
1024 (let ((func (archive-name "chown-entry")))
1025 (if (fboundp func)
1026 (progn
1027 (funcall func new-uid (archive-get-marked ?* t))
1028 (archive-resummarize))
1029 (error "Setting owner is not supported for this archive type"))))
1030
1031(defun archive-chgrp-entry (new-gid)
1032 "Change the group of all marked or this member."
1033 (interactive "nNew gid: ")
1034 (if archive-read-only (error "Archive is read-only"))
1035 (let ((func (archive-name "chgrp-entry")))
1036 (if (fboundp func)
1037 (progn
1038 (funcall func new-gid (archive-get-marked ?* t))
1039 (archive-resummarize))
1040 (error "Setting group is not supported for this archive type"))))
1041
1042(defun archive-expunge ()
1043 "Do the flagged deletions."
1044 (interactive)
1045 (let (files)
1046 (save-excursion
1047 (goto-char archive-file-list-start)
1048 (while (< (point) archive-file-list-end)
1049 (if (= (following-char) ?D)
1050 (setq files (cons (aref (archive-get-descr) 0) files)))
1051 (forward-line 1)))
1052 (setq files (nreverse files))
1053 (and files
1054 (or (not archive-read-only)
1055 (error "Archive is read-only"))
1056 (or (yes-or-no-p (format "Really delete %d member%s? "
1057 (length files)
1058 (if (null (cdr files)) "" "s")))
1059 (error "Operation aborted"))
1060 (let ((archive (archive-maybe-copy (buffer-file-name)))
1061 (expunger (archive-name "expunge")))
1062 (if (fboundp expunger)
1063 (funcall expunger archive files)
1064 (archive-*-expunge archive files (symbol-value expunger)))
1065 (archive-maybe-update nil)
1066 (if archive-remote
1067 (archive-resummarize)
1068 (revert-buffer))))))
1069
1070(defun archive-*-expunge (archive files command)
1071 (apply 'call-process
1072 (car command)
1073 nil
1074 nil
1075 nil
1076 (append (cdr command) (cons archive files))))
1077
1078(defun archive-rename-entry (newname)
1079 "Change the name associated with this entry in the tar file."
1080 (interactive "sNew name: ")
1081 (if archive-read-only (error "Archive is read-only"))
1082 (if (string= newname "")
1083 (error "Archive members may not be given empty names"))
1084 (let ((func (archive-name "rename-entry"))
1085 (descr (archive-get-descr)))
1086 (if (fboundp func)
1087 (progn
1088 (funcall func (buffer-file-name) newname descr)
1089 (archive-resummarize))
1090 (error "Renaming is not supported for this archive type"))))
1091
1092;; Revert the buffer and recompute the dired-like listing.
1093(defun archive-mode-revert (&optional no-autosave no-confirm)
1094 (let ((no (archive-get-lineno)))
1095 (setq archive-files nil)
1096 (let ((revert-buffer-function nil))
1097 (revert-buffer t t))
1098 (archive-mode)
1099 (goto-char archive-file-list-start)
1100 (archive-next-line no)))
1101
1102(defun archive-undo ()
1103 "Undo in an archive buffer.
1104This doesn't recover lost files, it just undoes changes in the buffer itself."
1105 (interactive)
1106 (let (buffer-read-only)
1107 (undo)))
1108;; -------------------------------------------------------------------------
1109;; Section: Arc Archives
1110
1111(defun archive-arc-summarize ()
1112 (let ((p 1)
1113 (totalsize 0)
1114 (maxlen 8)
1115 files
1116 visual)
1117 (while (and (< (+ p 29) (point-max))
1118 (= (char-after p) ?\C-z)
1119 (> (char-after (1+ p)) 0))
1120 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1121 (fnlen (or (string-match "\0" namefld) 13))
1122 (efnname (substring namefld 0 fnlen))
1123 (csize (archive-l-e (+ p 15) 4))
1124 (moddate (archive-l-e (+ p 19) 2))
1125 (modtime (archive-l-e (+ p 21) 2))
1126 (ucsize (archive-l-e (+ p 25) 4))
1127 (fiddle (string= efnname (upcase efnname)))
1128 (ifnname (if fiddle (downcase efnname) efnname))
1129 (text (format " %8d %-11s %-8s %s"
1130 ucsize
1131 (archive-dosdate moddate)
1132 (archive-dostime modtime)
1133 ifnname)))
1134 (setq maxlen (max maxlen fnlen)
1135 totalsize (+ totalsize ucsize)
1136 visual (cons (vector text
1137 (- (length text) (length ifnname))
1138 (length text))
1139 visual)
1140 files (cons (vector efnname ifnname fiddle nil (1- p))
1141 files)
1142 p (+ p 29 csize))))
1143 (goto-char (point-min))
1144 (let ((dash (concat "- -------- ----------- -------- "
1145 (make-string maxlen ?-)
1146 "\n")))
1147 (insert "M Length Date Time File\n"
1148 dash)
1149 (archive-summarize-files (nreverse visual))
1150 (insert dash
1151 (format " %8d %d file%s"
1152 totalsize
1153 (length files)
1154 (if (= 1 (length files)) "" "s"))
1155 "\n"))
1156 (apply 'vector (nreverse files))))
1157
1158(defun archive-arc-rename-entry (archive newname descr)
1159 (if (string-match "[:\\\\/]" newname)
1160 (error "File names in arc files may not contain a path"))
1161 (if (> (length newname) 12)
1162 (error "File names in arc files are limited to 12 characters"))
1163 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1164 (length newname))))
1165 buffer-read-only)
1166 (save-restriction
1167 (save-excursion
1168 (widen)
1169 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1170 (delete-char 13)
1171 (insert name)))))
1172;; -------------------------------------------------------------------------
1173;; Section: Lzh Archives
1174
1175(defun archive-lzh-summarize ()
1176 (let ((p 1)
1177 (totalsize 0)
1178 (maxlen 8)
1179 files
1180 visual)
1181 (while (progn (goto-char p) (looking-at "..-l[hz][0-9]-"))
1182 (let* ((hsize (char-after p))
1183 (csize (archive-l-e (+ p 7) 4))
1184 (ucsize (archive-l-e (+ p 11) 4))
1185 (modtime (archive-l-e (+ p 15) 2))
1186 (moddate (archive-l-e (+ p 17) 2))
1187 (fnlen (char-after (+ p 21)))
1188 (efnname (buffer-substring (+ p 22) (+ p 22 fnlen)))
1189 (fiddle (string= efnname (upcase efnname)))
1190 (ifnname (if fiddle (downcase efnname) efnname))
1191 (p2 (+ p 22 fnlen))
1192 (creator (if (>= (- hsize fnlen) 24) (char-after (+ p2 2)) 0))
1193 (mode (if (= creator ?U) (archive-l-e (+ p2 8) 2) ?\666))
1194 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1195 (uid (if (= creator ?U) (archive-l-e (+ p2 10) 2)))
1196 (gid (if (= creator ?U) (archive-l-e (+ p2 12) 2)))
1197 (text (if archive-alternate-display
1198 (format " %8d %5S %5S %s"
1199 ucsize
1200 (or uid "?")
1201 (or gid "?")
1202 ifnname)
1203 (format " %10s %8d %-11s %-8s %s"
1204 modestr
1205 ucsize
1206 (archive-dosdate moddate)
1207 (archive-dostime modtime)
1208 ifnname))))
1209 (setq maxlen (max maxlen fnlen)
1210 totalsize (+ totalsize ucsize)
1211 visual (cons (vector text
1212 (- (length text) (length ifnname))
1213 (length text))
1214 visual)
1215 files (cons (vector efnname ifnname fiddle mode (1- p))
1216 files)
1217 p (+ p hsize 2 csize))))
1218 (goto-char (point-min))
1219 (let ((dash (concat (if archive-alternate-display
1220 "- -------- ----- ----- "
1221 "- ---------- -------- ----------- -------- ")
1222 (make-string maxlen ?-)
1223 "\n"))
1224 (header (if archive-alternate-display
1225 "M Length Uid Gid File\n"
1226 "M Filemode Length Date Time File\n"))
1227 (sumline (if archive-alternate-display
1228 " %8d %d file%s"
1229 " %8d %d file%s")))
1230 (insert header dash)
1231 (archive-summarize-files (nreverse visual))
1232 (insert dash
1233 (format sumline
1234 totalsize
1235 (length files)
1236 (if (= 1 (length files)) "" "s"))
1237 "\n"))
1238 (apply 'vector (nreverse files))))
1239
1240(defconst archive-lzh-alternate-display t)
1241
1242(defun archive-lzh-extract (archive name)
1243 (archive-extract-by-stdout archive name archive-lzh-extract))
1244
1245(defun archive-lzh-resum (p count)
1246 (let ((sum 0))
1247 (while (> count 0)
1248 (setq count (1- count)
1249 sum (+ sum (char-after p))
1250 p (1+ p)))
1251 (logand sum 255)))
1252
1253(defun archive-lzh-rename-entry (archive newname descr)
1254 (save-restriction
1255 (save-excursion
1256 (widen)
1257 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1258 (oldhsize (char-after p))
1259 (oldfnlen (char-after (+ p 21)))
1260 (newfnlen (length newname))
1261 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1262 buffer-read-only)
1263 (if (> newhsize 255)
1264 (error "The file name is too long"))
1265 (goto-char (+ p 21))
1266 (delete-char (1+ oldfnlen))
1267 (insert newfnlen newname)
1268 (goto-char p)
1269 (delete-char 2)
1270 (insert newhsize (archive-lzh-resum p newhsize))))))
1271
1272(defun archive-lzh-ogm (newval files errtxt ofs)
1273 (save-restriction
1274 (save-excursion
1275 (widen)
1276 (while files
1277 (let* ((fil (car files))
1278 (p (+ archive-proper-file-start (aref fil 4)))
1279 (hsize (char-after p))
1280 (fnlen (char-after (+ p 21)))
1281 (p2 (+ p 22 fnlen))
1282 (creator (if (>= (- hsize fnlen) 24) (char-after (+ p2 2)) 0))
1283 buffer-read-only)
1284 (if (= creator ?U)
1285 (progn
1286 (or (numberp newval)
1287 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1288 (goto-char (+ p2 ofs))
1289 (delete-char 2)
1290 (insert (logand newval 255) (lsh newval -8))
1291 (goto-char (1+ p))
1292 (delete-char 1)
1293 (insert (archive-lzh-resum (1+ p) hsize)))
1294 (message "Member %s does not have %s field"
1295 (aref fil 1) errtxt)))
1296 (setq files (cdr files))))))
1297
1298(defun archive-lzh-chown-entry (newuid files)
1299 (archive-lzh-ogm newuid files "an uid" 10))
1300
1301(defun archive-lzh-chgrp-entry (newgid files)
1302 (archive-lzh-ogm newgid files "a gid" 12))
1303
1304(defun archive-lzh-chmod-entry (newmode files)
1305 (archive-lzh-ogm
1306 ;; This should work even though newmode will be dynamically accessed.
43f657ea 1307 (function (lambda (old) (archive-calc-mode old newmode t)))
665211a3
KH
1308 files "a unix-style mode" 8))
1309;; -------------------------------------------------------------------------
1310;; Section: Zip Archives
1311
1312(defun archive-zip-summarize ()
1313 (goto-char (- (point-max) (- 22 18)))
1314 (search-backward-regexp "[P]K\005\006")
1315 (let ((p (1+ (archive-l-e (+ (point) 16) 4)))
1316 (maxlen 8)
1317 (totalsize 0)
1318 files
1319 visual)
1320 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1321 (let* ((creator (char-after (+ p 5)))
1322 (method (archive-l-e (+ p 10) 2))
1323 (modtime (archive-l-e (+ p 12) 2))
1324 (moddate (archive-l-e (+ p 14) 2))
1325 (ucsize (archive-l-e (+ p 24) 4))
1326 (fnlen (archive-l-e (+ p 28) 2))
1327 (exlen (archive-l-e (+ p 30) 2))
845720b9 1328 (fclen (archive-l-e (+ p 32) 2))
665211a3
KH
1329 (lheader (archive-l-e (+ p 42) 4))
1330 (efnname (buffer-substring (+ p 46) (+ p 46 fnlen)))
1331 (isdir (and (= ucsize 0)
1332 (string= (file-name-nondirectory efnname) "")))
1333 (mode (cond ((memq creator '(2 3)) ; Unix + VMS
1334 (archive-l-e (+ p 40) 2))
1335 ((memq creator '(0 5 6 7 10 11)) ; Dos etc.
1336 (logior ?\444
1337 (if isdir (logior 16384 ?\111) 0)
1338 (if (zerop
1339 (logand 1 (char-after (+ p 38))))
1340 ?\222 0)))
1341 (t nil)))
1342 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1343 (fiddle (and archive-zip-case-fiddle
1344 (not (not (memq creator '(0 2 4 5 9))))))
1345 (ifnname (if fiddle (downcase efnname) efnname))
1346 (text (format " %10s %8d %-11s %-8s %s"
1347 modestr
1348 ucsize
1349 (archive-dosdate moddate)
1350 (archive-dostime modtime)
1351 ifnname)))
1352 (setq maxlen (max maxlen fnlen)
1353 totalsize (+ totalsize ucsize)
1354 visual (cons (vector text
1355 (- (length text) (length ifnname))
1356 (length text))
1357 visual)
1358 files (cons (if isdir
1359 nil
1360 (vector efnname ifnname fiddle mode
1361 (list (1- p) lheader)))
1362 files)
845720b9 1363 p (+ p 46 fnlen exlen fclen))))
665211a3
KH
1364 (goto-char (point-min))
1365 (let ((dash (concat "- ---------- -------- ----------- -------- "
1366 (make-string maxlen ?-)
1367 "\n")))
1368 (insert "M Filemode Length Date Time File\n"
1369 dash)
1370 (archive-summarize-files (nreverse visual))
1371 (insert dash
1372 (format " %8d %d file%s"
1373 totalsize
1374 (length files)
1375 (if (= 1 (length files)) "" "s"))
1376 "\n"))
1377 (apply 'vector (nreverse files))))
1378
1379(defun archive-zip-extract (archive name)
1380 (if archive-zip-use-pkzip
1381 (archive-*-extract archive name archive-zip-extract)
1382 (archive-extract-by-stdout archive name archive-zip-extract)))
1383
1384(defun archive-zip-write-file-member (archive descr)
1385 (archive-*-write-file-member
1386 archive
1387 descr
1388 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1389
1390(defun archive-zip-chmod-entry (newmode files)
1391 (save-restriction
1392 (save-excursion
1393 (widen)
1394 (while files
1395 (let* ((fil (car files))
1396 (p (+ archive-proper-file-start (car (aref fil 4))))
1397 (creator (char-after (+ p 5)))
1398 (oldmode (aref fil 3))
1399 (newval (archive-calc-mode oldmode newmode t))
1400 buffer-read-only)
1401 (cond ((memq creator '(2 3)) ; Unix + VMS
1402 (goto-char (+ p 40))
1403 (delete-char 2)
1404 (insert (logand newval 255) (lsh newval -8)))
1405 ((memq creator '(0 5 6 7 10 11)) ; Dos etc.
1406 (goto-char (+ p 38))
1407 (insert (logior (logand (char-after (point)) 254)
1408 (logand (logxor 1 (lsh newval -7)) 1)))
1409 (delete-char 1))
1410 (t (message "Don't know how to change mode for this member"))))
1411 (setq files (cdr files))))))
1412;; -------------------------------------------------------------------------
1413;; Section: Zoo Archives
1414
1415(defun archive-zoo-summarize ()
1416 (let ((p (1+ (archive-l-e 25 4)))
1417 (maxlen 8)
1418 (totalsize 0)
1419 files
1420 visual)
1421 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1422 (> (archive-l-e (+ p 6) 4) 0))
1423 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1424 (moddate (archive-l-e (+ p 14) 2))
1425 (modtime (archive-l-e (+ p 16) 2))
1426 (ucsize (archive-l-e (+ p 20) 4))
1427 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
83c4abcb
RS
1428 (dirtype (char-after (+ p 4)))
1429 (lfnlen (if (= dirtype 2) (char-after (+ p 56)) 0))
1430 (ldirlen (if (= dirtype 2) (char-after (+ p 57)) 0))
1431 (fnlen (+ ldirlen
1432 (if (> lfnlen 0)
1433 (1- lfnlen)
1434 (or (string-match "\0" namefld) 13))))
1435 (efnname (concat
1436 (if (> ldirlen 0)
1437 (concat (buffer-substring
1438 (+ p 58 lfnlen) (+ p 58 lfnlen ldirlen -1))
1439 "/")
1440 "")
1441 (if (> lfnlen 0)
1442 (buffer-substring (+ p 58) (+ p 58 lfnlen -1))
1443 (substring namefld 0 fnlen))))
1444 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
665211a3
KH
1445 (ifnname (if fiddle (downcase efnname) efnname))
1446 (text (format " %8d %-11s %-8s %s"
1447 ucsize
1448 (archive-dosdate moddate)
1449 (archive-dostime modtime)
1450 ifnname)))
1451 (setq maxlen (max maxlen fnlen)
1452 totalsize (+ totalsize ucsize)
1453 visual (cons (vector text
1454 (- (length text) (length ifnname))
1455 (length text))
1456 visual)
1457 files (cons (vector efnname ifnname fiddle nil (1- p))
1458 files)
1459 p next)))
1460 (goto-char (point-min))
1461 (let ((dash (concat "- -------- ----------- -------- "
1462 (make-string maxlen ?-)
1463 "\n")))
1464 (insert "M Length Date Time File\n"
1465 dash)
1466 (archive-summarize-files (nreverse visual))
1467 (insert dash
1468 (format " %8d %d file%s"
1469 totalsize
1470 (length files)
1471 (if (= 1 (length files)) "" "s"))
1472 "\n"))
1473 (apply 'vector (nreverse files))))
1474
1475(defun archive-zoo-extract (archive name)
1476 (archive-extract-by-stdout archive name archive-zoo-extract))
1477;; -------------------------------------------------------------------------
1478(provide 'archive-mode)
1479
1480;; arc-mode.el ends here.