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