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