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