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