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