Cleanup uses of "-hooks".
[bpt/emacs.git] / lisp / arc-mode.el
CommitLineData
665211a3
KH
1;;; arc-mode.el --- simple editing of archives
2
acaf905b 3;; Copyright (C) 1995, 1997-1998, 2001-2012 Free Software Foundation, Inc.
665211a3 4
6b61353c 5;; Author: Morten Welinder <terra@gnu.org>
8cb5ffe8 6;; Keywords: files archives msdog editing major-mode
c5e87d10 7;; Favorite-brand-of-beer: None, I hate beer.
665211a3 8
b578f267
EN
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
b578f267
EN
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
665211a3
KH
23
24;;; Commentary:
b578f267 25
665211a3
KH
26;; NAMING: "arc" is short for "archive" and does not refer specifically
27;; to files whose name end in ".arc"
28;;
29;; This code does not decode any files internally, although it does
30;; understand the directory level of the archives. For this reason,
31;; you should expect this code to need more fiddling than tar-mode.el
32;; (although it at present has fewer bugs :-) In particular, I have
33;; not tested this under Ms-Dog myself.
34;; -------------------------------------
35;; INTERACTION: arc-mode.el should play together with
36;;
37;; * ange-ftp.el: Remote archives (i.e., ones that ange-ftp has brought
38;; to you) are handled by doing all updates on a local
39;; copy. When you make changes to a remote file the
40;; changes will first take effect when the archive buffer
41;; is saved. You will be warned about this.
42;;
43;; * dos-fns.el: (Part of Emacs 19). You get automatic ^M^J <--> ^J
44;; conversion.
45;;
46;; arc-mode.el does not work well with crypt++.el; for the archives as
47;; such this could be fixed (but wouldn't be useful) by declaring such
48;; archives to be "remote". For the members this is a general Emacs
49;; problem that 19.29's file formats may fix.
50;; -------------------------------------
51;; ARCHIVE TYPES: Currently only the archives below are handled, but the
52;; structure for handling just about anything is in place.
53;;
b3671a51
JL
54;; Arc Lzh Zip Zoo Rar 7z
55;; --------------------------------------------
56;; View listing Intern Intern Intern Intern Y Y
57;; Extract member Y Y Y Y Y Y
ac89b32c 58;; Save changed member Y Y Y Y N Y
b3671a51 59;; Add new member N N N N N N
ac89b32c 60;; Delete member Y Y Y Y N Y
b3671a51
JL
61;; Rename member Y Y N N N N
62;; Chmod - Y Y - N N
63;; Chown - Y - - N N
64;; Chgrp - Y - - N N
665211a3
KH
65;;
66;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
67;; on the first released version of this package.
68;;
69;; This code is partly based on tar-mode.el from Emacs.
70;; -------------------------------------
71;; ARCHIVE STRUCTURES:
72;; (This is mostly for myself.)
73;;
74;; ARC A series of (header,file). No interactions among members.
75;;
76;; LZH A series of (header,file). Headers are checksummed. No
77;; interaction among members.
c5e87d10 78;; Headers come in three flavors called level 0, 1 and 2 headers.
5f23d836
RS
79;; Level 2 header is free of DOS specific restrictions and most
80;; prevalently used. Also level 1 and 2 headers consist of base
81;; and extension headers. For more details see
82;; http://homepage1.nifty.com/dangan/en/Content/Program/Java/jLHA/Notes/Notes.html
83;; http://www.osirusoft.com/joejared/lzhformat.html
665211a3
KH
84;;
85;; ZIP A series of (lheader,fil) followed by a "central directory"
86;; which is a series of (cheader) followed by an end-of-
87;; central-dir record possibly followed by junk. The e-o-c-d
88;; links to c-d. cheaders link to lheaders which are basically
89;; cut-down versions of the cheaders.
90;;
91;; ZOO An archive header followed by a series of (header,file).
92;; Each member header points to the next. The archive is
93;; terminated by a bogus header with a zero next link.
94;; -------------------------------------
246e8695 95;; HOOKS: `foo' means one of the supported archive types.
665211a3
KH
96;;
97;; archive-mode-hook
98;; archive-foo-mode-hook
99;; archive-extract-hooks
100
101;;; Code:
102
103;; -------------------------------------------------------------------------
7e9a3fef 104;;; Section: Configuration.
665211a3 105
c38eb0a8
RS
106(defgroup archive nil
107 "Simple editing of archives."
108 :group 'data)
665211a3 109
c38eb0a8
RS
110(defgroup archive-arc nil
111 "ARC-specific options to archive."
112 :group 'archive)
113
114(defgroup archive-lzh nil
115 "LZH-specific options to archive."
116 :group 'archive)
117
118(defgroup archive-zip nil
119 "ZIP-specific options to archive."
120 :group 'archive)
121
122(defgroup archive-zoo nil
123 "ZOO-specific options to archive."
124 :group 'archive)
125
c38eb0a8 126(defcustom archive-tmpdir
8053add8
RS
127 ;; make-temp-name is safe here because we use this name
128 ;; to create a directory.
380683ed
EZ
129 (make-temp-name
130 (expand-file-name (if (eq system-type 'ms-dos) "ar" "archive.tmp")
0adf5079 131 temporary-file-directory))
0cf1ef26 132 "Directory for temporary files made by `arc-mode.el'."
c38eb0a8
RS
133 :type 'directory
134 :group 'archive)
665211a3 135
c38eb0a8 136(defcustom archive-remote-regexp "^/[^/:]*[^/:.]:"
9201cc28 137 "Regexp recognizing archive files names that are not local.
eeba65ed 138A non-local file is one whose file name is not proper outside Emacs.
c38eb0a8
RS
139A local copy of the archive will be used when updating."
140 :type 'regexp
141 :group 'archive)
142
d1069532
SM
143(define-obsolete-variable-alias 'archive-extract-hooks
144 'archive-extract-hook "24.3")
145(defcustom archive-extract-hook nil
146 "Hook run when an archive member has been extracted."
c38eb0a8
RS
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")
9201cc28 156 "Program and its options to run in order to extract an arc file member.
eeba65ed 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 166 '("arc" "d")
9201cc28 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 176 '("arc" "u")
9201cc28 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")
9201cc28 189 "Program and its options to run in order to extract an lzh file member.
eeba65ed 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 199 '("lha" "d")
9201cc28 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 209 '("lha" "a")
9201cc28 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
cd79ce90
JL
221 (cond ((executable-find "unzip") '("unzip" "-qq" "-c"))
222 ((executable-find "7z") '("7z" "x" "-so"))
b3671a51 223 ((executable-find "pkunzip") '("pkunzip" "-e" "-o-"))
cd79ce90 224 (t '("unzip" "-qq" "-c")))
9201cc28 225 "Program and its options to run in order to extract a zip file member.
eeba65ed 226Extraction should happen to standard output. Archive and member name will
74725d46 227be added."
c38eb0a8 228 :type '(list (string :tag "Program")
b3671a51
JL
229 (repeat :tag "Options"
230 :inline t
231 (string :format "%v")))
c38eb0a8 232 :group 'archive-zip)
665211a3 233
fffa137c 234;; For several reasons the latter behavior is not desirable in general.
665211a3
KH
235;; (1) It uses more disk space. (2) Error checking is worse or non-
236;; existent. (3) It tends to do funny things with other systems' file
237;; names.
238
c38eb0a8 239(defcustom archive-zip-expunge
cd79ce90
JL
240 (cond ((executable-find "zip") '("zip" "-d" "-q"))
241 ((executable-find "7z") '("7z" "d"))
242 ((executable-find "pkzip") '("pkzip" "-d"))
243 (t '("zip" "-d" "-q")))
9201cc28 244 "Program and its options to run in order to delete zip file members.
c38eb0a8
RS
245Archive and member names will be added."
246 :type '(list (string :tag "Program")
cd79ce90
JL
247 (repeat :tag "Options"
248 :inline t
249 (string :format "%v")))
c38eb0a8
RS
250 :group 'archive-zip)
251
252(defcustom archive-zip-update
cd79ce90
JL
253 (cond ((executable-find "zip") '("zip" "-q"))
254 ((executable-find "7z") '("7z" "u"))
255 ((executable-find "pkzip") '("pkzip" "-u" "-P"))
256 (t '("zip" "-q")))
9201cc28 257 "Program and its options to run in order to update a zip file member.
665211a3 258Options should ensure that specified directory will be put into the zip
c38eb0a8
RS
259file. Archive and member name will be added."
260 :type '(list (string :tag "Program")
cd79ce90
JL
261 (repeat :tag "Options"
262 :inline t
263 (string :format "%v")))
c38eb0a8
RS
264 :group 'archive-zip)
265
266(defcustom archive-zip-update-case
cd79ce90
JL
267 (cond ((executable-find "zip") '("zip" "-q" "-k"))
268 ((executable-find "7z") '("7z" "u"))
269 ((executable-find "pkzip") '("pkzip" "-u" "-P"))
270 (t '("zip" "-q" "-k")))
9201cc28 271 "Program and its options to run in order to update a case fiddled zip member.
eeba65ed 272Options should ensure that specified directory will be put into the zip file.
c38eb0a8
RS
273Archive and member name will be added."
274 :type '(list (string :tag "Program")
cd79ce90
JL
275 (repeat :tag "Options"
276 :inline t
277 (string :format "%v")))
c38eb0a8
RS
278 :group 'archive-zip)
279
280(defcustom archive-zip-case-fiddle t
9201cc28 281 "If non-nil then zip file members may be down-cased.
380683ed
EZ
282This case fiddling will only happen for members created by a system
283that uses caseless file names."
c38eb0a8
RS
284 :type 'boolean
285 :group 'archive-zip)
665211a3
KH
286;; ------------------------------
287;; Zoo archive configuration
288
c38eb0a8 289(defcustom archive-zoo-extract
665211a3 290 '("zoo" "xpq")
9201cc28 291 "Program and its options to run in order to extract a zoo file member.
eeba65ed 292Extraction should happen to standard output. Archive and member name will
c38eb0a8
RS
293be added."
294 :type '(list (string :tag "Program")
295 (repeat :tag "Options"
296 :inline t
297 (string :format "%v")))
298 :group 'archive-zoo)
299
300(defcustom archive-zoo-expunge
665211a3 301 '("zoo" "DqPP")
9201cc28 302 "Program and its options to run in order to delete zoo file members.
c38eb0a8
RS
303Archive and member names will be added."
304 :type '(list (string :tag "Program")
305 (repeat :tag "Options"
306 :inline t
307 (string :format "%v")))
308 :group 'archive-zoo)
309
310(defcustom archive-zoo-write-file-member
665211a3 311 '("zoo" "a")
9201cc28 312 "Program and its options to run in order to update a zoo file member.
c38eb0a8
RS
313Archive and member name will be added."
314 :type '(list (string :tag "Program")
315 (repeat :tag "Options"
316 :inline t
317 (string :format "%v")))
318 :group 'archive-zoo)
b3671a51
JL
319;; ------------------------------
320;; 7z archive configuration
321
322(defcustom archive-7z-extract
323 '("7z" "x" "-so")
324 "Program and its options to run in order to extract a 7z file member.
325Extraction should happen to standard output. Archive and member name will
326be added."
2bed3f04 327 :version "24.1"
b3671a51 328 :type '(list (string :tag "Program")
ac89b32c
JL
329 (repeat :tag "Options"
330 :inline t
331 (string :format "%v")))
332 :group 'archive-7z)
333
334(defcustom archive-7z-expunge
335 '("7z" "d")
336 "Program and its options to run in order to delete 7z file members.
337Archive and member names will be added."
2bed3f04 338 :version "24.1"
ac89b32c
JL
339 :type '(list (string :tag "Program")
340 (repeat :tag "Options"
341 :inline t
342 (string :format "%v")))
343 :group 'archive-7z)
344
345(defcustom archive-7z-update
346 '("7z" "u")
347 "Program and its options to run in order to update a 7z file member.
348Options should ensure that specified directory will be put into the 7z
349file. Archive and member name will be added."
2bed3f04 350 :version "24.1"
ac89b32c
JL
351 :type '(list (string :tag "Program")
352 (repeat :tag "Options"
353 :inline t
354 (string :format "%v")))
b3671a51
JL
355 :group 'archive-7z)
356
665211a3 357;; -------------------------------------------------------------------------
7e9a3fef 358;;; Section: Variables
665211a3 359
194600a8
JPW
360(defvar archive-subtype nil "Symbol describing archive type.")
361(defvar archive-file-list-start nil "Position of first contents line.")
362(defvar archive-file-list-end nil "Position just after last contents line.")
363(defvar archive-proper-file-start nil "Position of real archive's start.")
364(defvar archive-read-only nil "Non-nil if the archive is read-only on disk.")
365(defvar archive-local-name nil "Name of local copy of remote archive.")
941f9778
SM
366(defvar archive-mode-map
367 (let ((map (make-keymap)))
6f52d86e 368 (set-keymap-parent map special-mode-map)
941f9778
SM
369 (define-key map " " 'archive-next-line)
370 (define-key map "a" 'archive-alternate-display)
371 ;;(define-key map "c" 'archive-copy)
372 (define-key map "d" 'archive-flag-deleted)
373 (define-key map "\C-d" 'archive-flag-deleted)
374 (define-key map "e" 'archive-extract)
375 (define-key map "f" 'archive-extract)
376 (define-key map "\C-m" 'archive-extract)
941f9778
SM
377 (define-key map "m" 'archive-mark)
378 (define-key map "n" 'archive-next-line)
379 (define-key map "\C-n" 'archive-next-line)
380 (define-key map [down] 'archive-next-line)
381 (define-key map "o" 'archive-extract-other-window)
382 (define-key map "p" 'archive-previous-line)
941f9778
SM
383 (define-key map "\C-p" 'archive-previous-line)
384 (define-key map [up] 'archive-previous-line)
385 (define-key map "r" 'archive-rename-entry)
386 (define-key map "u" 'archive-unflag)
387 (define-key map "\M-\C-?" 'archive-unmark-all-files)
388 (define-key map "v" 'archive-view)
389 (define-key map "x" 'archive-expunge)
390 (define-key map "\177" 'archive-unflag-backwards)
391 (define-key map "E" 'archive-extract-other-window)
392 (define-key map "M" 'archive-chmod-entry)
393 (define-key map "G" 'archive-chgrp-entry)
394 (define-key map "O" 'archive-chown-entry)
fb3aad66
SM
395 ;; Let mouse-1 follow the link.
396 (define-key map [follow-link] 'mouse-face)
941f9778
SM
397
398 (if (fboundp 'command-remapping)
399 (progn
400 (define-key map [remap advertised-undo] 'archive-undo)
401 (define-key map [remap undo] 'archive-undo))
402 (substitute-key-definition 'advertised-undo 'archive-undo map global-map)
403 (substitute-key-definition 'undo 'archive-undo map global-map))
404
405 (define-key map
fdaaf743 406 (if (featurep 'xemacs) 'button2 [mouse-2]) 'archive-extract)
941f9778
SM
407
408 (if (featurep 'xemacs)
409 () ; out of luck
410
411 (define-key map [menu-bar immediate]
412 (cons "Immediate" (make-sparse-keymap "Immediate")))
413 (define-key map [menu-bar immediate alternate]
414 '(menu-item "Alternate Display" archive-alternate-display
415 :enable (boundp (archive-name "alternate-display"))
416 :help "Toggle alternate file info display"))
417 (define-key map [menu-bar immediate view]
418 '(menu-item "View This File" archive-view
419 :help "Display file at cursor in View Mode"))
420 (define-key map [menu-bar immediate display]
421 '(menu-item "Display in Other Window" archive-display-other-window
422 :help "Display file at cursor in another window"))
423 (define-key map [menu-bar immediate find-file-other-window]
424 '(menu-item "Find in Other Window" archive-extract-other-window
425 :help "Edit file at cursor in another window"))
426 (define-key map [menu-bar immediate find-file]
427 '(menu-item "Find This File" archive-extract
428 :help "Extract file at cursor and edit it"))
429
430 (define-key map [menu-bar mark]
431 (cons "Mark" (make-sparse-keymap "Mark")))
432 (define-key map [menu-bar mark unmark-all]
433 '(menu-item "Unmark All" archive-unmark-all-files
434 :help "Unmark all marked files"))
435 (define-key map [menu-bar mark deletion]
436 '(menu-item "Flag" archive-flag-deleted
437 :help "Flag file at cursor for deletion"))
438 (define-key map [menu-bar mark unmark]
439 '(menu-item "Unflag" archive-unflag
440 :help "Unmark file at cursor"))
441 (define-key map [menu-bar mark mark]
442 '(menu-item "Mark" archive-mark
443 :help "Mark file at cursor"))
444
445 (define-key map [menu-bar operate]
446 (cons "Operate" (make-sparse-keymap "Operate")))
447 (define-key map [menu-bar operate chown]
448 '(menu-item "Change Owner..." archive-chown-entry
449 :enable (fboundp (archive-name "chown-entry"))
450 :help "Change owner of marked files"))
451 (define-key map [menu-bar operate chgrp]
452 '(menu-item "Change Group..." archive-chgrp-entry
453 :enable (fboundp (archive-name "chgrp-entry"))
454 :help "Change group ownership of marked files"))
455 (define-key map [menu-bar operate chmod]
456 '(menu-item "Change Mode..." archive-chmod-entry
457 :enable (fboundp (archive-name "chmod-entry"))
458 :help "Change mode (permissions) of marked files"))
459 (define-key map [menu-bar operate rename]
460 '(menu-item "Rename to..." archive-rename-entry
461 :enable (fboundp (archive-name "rename-entry"))
462 :help "Rename marked files"))
463 ;;(define-key map [menu-bar operate copy]
464 ;; '(menu-item "Copy to..." archive-copy))
465 (define-key map [menu-bar operate expunge]
466 '(menu-item "Expunge Marked Files" archive-expunge
467 :help "Delete all flagged files from archive"))
468 map))
469 "Local keymap for archive mode listings.")
194600a8
JPW
470(defvar archive-file-name-indent nil "Column where file names start.")
471
472(defvar archive-remote nil "Non-nil if the archive is outside file system.")
380683ed
EZ
473(make-variable-buffer-local 'archive-remote)
474(put 'archive-remote 'permanent-local t)
475
476(defvar archive-member-coding-system nil "Coding-system of archive member.")
477(make-variable-buffer-local 'archive-member-coding-system)
478
665211a3 479(defvar archive-alternate-display nil
194600a8 480 "Non-nil when alternate information is shown.")
665211a3
KH
481(make-variable-buffer-local 'archive-alternate-display)
482(put 'archive-alternate-display 'permanent-local t)
483
194600a8 484(defvar archive-superior-buffer nil "In archive members, points to archive.")
665211a3
KH
485(put 'archive-superior-buffer 'permanent-local t)
486
194600a8 487(defvar archive-subfile-mode nil "Non-nil in archive member buffers.")
665211a3
KH
488(make-variable-buffer-local 'archive-subfile-mode)
489(put 'archive-subfile-mode 'permanent-local t)
490
ad38511a
KH
491(defvar archive-file-name-coding-system nil)
492(make-variable-buffer-local 'archive-file-name-coding-system)
493(put 'archive-file-name-coding-system 'permanent-local t)
494
c4de97b4
RS
495(defvar archive-files nil
496 "Vector of file descriptors.
497Each descriptor is a vector of the form
498 [EXT-FILE-NAME INT-FILE-NAME CASE-FIDDLED MODE ...]")
665211a3 499(make-variable-buffer-local 'archive-files)
43f657ea 500
665211a3 501;; -------------------------------------------------------------------------
7e9a3fef 502;;; Section: Support functions.
665211a3 503
ad38511a
KH
504(eval-when-compile
505 (defsubst byte-after (pos)
506 "Like char-after but an eight-bit char is converted to unibyte."
507 (multibyte-char-to-unibyte (char-after pos)))
ad38511a
KH
508 (defsubst insert-unibyte (&rest args)
509 "Like insert but don't make unibyte string and eight-bit char multibyte."
510 (dolist (elt args)
511 (if (integerp elt)
512 (insert (if (< elt 128) elt (decode-char 'eight-bit elt)))
513 (insert (string-to-multibyte elt)))))
514 )
515
665211a3
KH
516(defsubst archive-name (suffix)
517 (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
518
a12aece3 519(defun archive-l-e (str &optional len float)
0cf1ef26
JB
520 "Convert little endian string/vector STR to integer.
521Alternatively, STR may be a buffer position in the current buffer
a12aece3
EZ
522in which case a second argument, length LEN, should be supplied.
523FLOAT, if non-nil, means generate and return a float instead of an integer
524\(use this for numbers that can overflow the Emacs integer)."
665211a3
KH
525 (if (stringp str)
526 (setq len (length str))
527 (setq str (buffer-substring str (+ str len))))
8f924df7 528 (setq str (string-as-unibyte str))
665211a3
KH
529 (let ((result 0)
530 (i 0))
531 (while (< i len)
532 (setq i (1+ i)
a12aece3
EZ
533 result (+ (if float (* result 256.0) (ash result 8))
534 (aref str (- len i)))))
665211a3
KH
535 result))
536
537(defun archive-int-to-mode (mode)
ff39b9a1
SM
538 "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------."
539 ;; FIXME: merge with tar-grind-file-mode.
540 (string
541 (if (zerop (logand 8192 mode))
542 (if (zerop (logand 16384 mode)) ?- ?d)
543 ?c) ; completeness
544 (if (zerop (logand 256 mode)) ?- ?r)
545 (if (zerop (logand 128 mode)) ?- ?w)
546 (if (zerop (logand 64 mode))
547 (if (zerop (logand 1024 mode)) ?- ?S)
548 (if (zerop (logand 1024 mode)) ?x ?s))
549 (if (zerop (logand 32 mode)) ?- ?r)
550 (if (zerop (logand 16 mode)) ?- ?w)
551 (if (zerop (logand 8 mode))
552 (if (zerop (logand 2048 mode)) ?- ?S)
553 (if (zerop (logand 2048 mode)) ?x ?s))
554 (if (zerop (logand 4 mode)) ?- ?r)
555 (if (zerop (logand 2 mode)) ?- ?w)
556 (if (zerop (logand 1 mode)) ?- ?x)))
665211a3
KH
557
558(defun archive-calc-mode (oldmode newmode &optional error)
eeba65ed 559 "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
665211a3
KH
560NEWMODE may be an octal number including a leading zero in which case it
561will become the new mode.\n
562NEWMODE may also be a relative specification like \"og-rwx\" in which case
563OLDMODE will be modified accordingly just like chmod(2) would have done.\n
564If optional third argument ERROR is non-nil an error will be signaled if
565the mode is invalid. If ERROR is nil then nil will be returned."
566 (cond ((string-match "^0[0-7]*$" newmode)
567 (let ((result 0)
568 (len (length newmode))
569 (i 1))
570 (while (< i len)
571 (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
572 i (1+ i)))
573 (logior (logand oldmode 65024) result)))
574 ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
575 (let ((who 0)
576 (result oldmode)
577 (op (aref newmode (match-beginning 2)))
578 (bits 0)
579 (i (match-beginning 3)))
580 (while (< i (match-end 3))
581 (let ((rwx (aref newmode i)))
582 (setq bits (logior bits (cond ((= rwx ?r) 292)
583 ((= rwx ?w) 146)
584 ((= rwx ?x) 73)
585 ((= rwx ?s) 3072)
586 ((= rwx ?t) 512)))
587 i (1+ i))))
588 (while (< who (match-end 1))
589 (let* ((whoc (aref newmode who))
590 (whomask (cond ((= whoc ?a) 4095)
591 ((= whoc ?u) 1472)
592 ((= whoc ?g) 2104)
593 ((= whoc ?o) 7))))
594 (if (= op ?=)
595 (setq result (logand result (lognot whomask))))
596 (if (= op ?-)
597 (setq result (logand result (lognot (logand whomask bits))))
598 (setq result (logior result (logand whomask bits)))))
599 (setq who (1+ who)))
600 result))
601 (t
602 (if error
603 (error "Invalid mode specification: %s" newmode)))))
604
605(defun archive-dosdate (date)
606 "Stringify dos packed DATE record."
607 (let ((year (+ 1980 (logand (ash date -9) 127)))
608 (month (logand (ash date -5) 15))
609 (day (logand date 31)))
610 (if (or (> month 12) (< month 1))
611 ""
612 (format "%2d-%s-%d"
613 day
614 (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
615 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
616 year))))
617
618(defun archive-dostime (time)
619 "Stringify dos packed TIME record."
620 (let ((hour (logand (ash time -11) 31))
70569550 621 (minute (logand (ash time -5) 63))
665211a3
KH
622 (second (* 2 (logand time 31)))) ; 2 seconds resolution
623 (format "%02d:%02d:%02d" hour minute second)))
624
5f23d836 625(defun archive-unixdate (low high)
0cf1ef26 626 "Stringify Unix (LOW HIGH) date."
0bfcf5c5
PE
627 (let* ((time (cons high low))
628 (str (current-time-string time)))
5f23d836
RS
629 (format "%s-%s-%s"
630 (substring str 8 10)
631 (substring str 4 7)
0bfcf5c5 632 (format-time-string "%Y" time))))
665211a3 633
5f23d836 634(defun archive-unixtime (low high)
0cf1ef26 635 "Stringify Unix (LOW HIGH) time."
5f23d836
RS
636 (let ((str (current-time-string (cons high low))))
637 (substring str 11 19)))
665211a3
KH
638
639(defun archive-get-lineno ()
640 (if (>= (point) archive-file-list-start)
641 (count-lines archive-file-list-start
5ed619e0 642 (line-beginning-position))
665211a3
KH
643 0))
644
645(defun archive-get-descr (&optional noerror)
eeba65ed 646 "Return the descriptor vector for file at point.
0cf1ef26 647Does not signal an error if optional argument NOERROR is non-nil."
665211a3
KH
648 (let ((no (archive-get-lineno)))
649 (if (and (>= (point) archive-file-list-start)
650 (< no (length archive-files)))
651 (let ((item (aref archive-files no)))
652 (if (vectorp item)
653 item
654 (if (not noerror)
655 (error "Entry is not a regular member of the archive"))))
656 (if (not noerror)
657 (error "Line does not describe a member of the archive")))))
658;; -------------------------------------------------------------------------
7e9a3fef 659;;; Section: the mode definition
665211a3 660
9199a670 661;;;###autoload
665211a3 662(defun archive-mode (&optional force)
eeba65ed
RS
663 "Major mode for viewing an archive file in a dired-like way.
664You can move around using the usual cursor motion commands.
665211a3
KH
665Letters no longer insert themselves.
666Type `e' to pull a file out of the archive and into its own buffer;
667or click mouse-2 on the file's line in the archive mode buffer.
668
669If you edit a sub-file of this archive (as with the `e' command) and
670save it, the contents of that buffer will be saved back into the
671archive.
672
673\\{archive-mode-map}"
674 ;; This is not interactive because you shouldn't be turning this
675 ;; mode on and off. You can corrupt things that way.
676 (if (zerop (buffer-size))
677 ;; At present we cannot create archives from scratch
1e8eecea 678 (funcall (or (default-value 'major-mode) 'fundamental-mode))
665211a3
KH
679 (if (and (not force) archive-files) nil
680 (let* ((type (archive-find-type))
ff39b9a1 681 (typename (capitalize (symbol-name type))))
665211a3
KH
682 (kill-all-local-variables)
683 (make-local-variable 'archive-subtype)
684 (setq archive-subtype type)
685
686 ;; Buffer contains treated image of file before the file contents
687 (make-local-variable 'revert-buffer-function)
688 (setq revert-buffer-function 'archive-mode-revert)
689 (auto-save-mode 0)
665211a3 690
380683ed
EZ
691 ;; Remote archives are not written by a hook.
692 (if archive-remote nil
fdaaf743 693 (add-hook 'write-contents-functions 'archive-write-file nil t))
380683ed 694
665211a3
KH
695 (make-local-variable 'require-final-newline)
696 (setq require-final-newline nil)
28138f8c
RS
697 (make-local-variable 'local-enable-local-variables)
698 (setq local-enable-local-variables nil)
665211a3 699
938c6472
RS
700 ;; Prevent loss of data when saving the file.
701 (make-local-variable 'file-precious-flag)
702 (setq file-precious-flag t)
703
665211a3 704 (make-local-variable 'archive-read-only)
380683ed
EZ
705 ;; Archives which are inside other archives and whose
706 ;; names are invalid for this OS, can't be written.
707 (setq archive-read-only
708 (or (not (file-writable-p (buffer-file-name)))
709 (and archive-subfile-mode
4dbbd6a1 710 (string-match file-name-invalid-regexp
380683ed 711 (aref archive-subfile-mode 0)))))
665211a3
KH
712
713 ;; Should we use a local copy when accessing from outside Emacs?
714 (make-local-variable 'archive-local-name)
380683ed
EZ
715
716 ;; An archive can contain another archive whose name is invalid
717 ;; on local filesystem. Treat such archives as remote.
718 (or archive-remote
719 (setq archive-remote
720 (or (string-match archive-remote-regexp (buffer-file-name))
4dbbd6a1 721 (string-match file-name-invalid-regexp
380683ed 722 (buffer-file-name)))))
665211a3
KH
723
724 (setq major-mode 'archive-mode)
725 (setq mode-name (concat typename "-Archive"))
726 ;; Run archive-foo-mode-hook and archive-mode-hook
21a88c56 727 (run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
665211a3
KH
728 (use-local-map archive-mode-map))
729
730 (make-local-variable 'archive-proper-file-start)
731 (make-local-variable 'archive-file-list-start)
732 (make-local-variable 'archive-file-list-end)
733 (make-local-variable 'archive-file-name-indent)
ad38511a
KH
734 (setq archive-file-name-coding-system
735 (or file-name-coding-system
736 default-file-name-coding-system
737 locale-coding-system))
597e2240 738 (if (default-value 'enable-multibyte-characters)
8f924df7 739 (set-buffer-multibyte 'to))
380683ed 740 (archive-summarize nil)
665211a3
KH
741 (setq buffer-read-only t))))
742
743;; Archive mode is suitable only for specially formatted data.
744(put 'archive-mode 'mode-class 'special)
665211a3 745
941f9778 746(let ((item1 '(archive-subfile-mode " Archive")))
665211a3 747 (or (member item1 minor-mode-alist)
941f9778 748 (setq minor-mode-alist (cons item1 minor-mode-alist))))
665211a3
KH
749;; -------------------------------------------------------------------------
750(defun archive-find-type ()
751 (widen)
752 (goto-char (point-min))
753 ;; The funny [] here make it unlikely that the .elc file will be treated
754 ;; as an archive by other software.
755 (let (case-fold-search)
eb1727a4 756 (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
a56636ae 757 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
665211a3
KH
758 ((looking-at "....................[\334]\247\304\375") 'zoo)
759 ((and (looking-at "\C-z") ; signature too simple, IMHO
760 (string-match "\\.[aA][rR][cC]$"
761 (or buffer-file-name (buffer-name))))
762 'arc)
5618fbd2 763 ;; This pattern modeled on the BSD/GNU+Linux `file' command.
fcb006c4
CY
764 ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
765 ;; Note this regexp is also in archive-exe-p.
766 ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
7e9a3fef 767 ((looking-at "Rar!") 'rar)
239bf18b 768 ((looking-at "!<arch>\n") 'ar)
c9db111a
SM
769 ((and (looking-at "MZ")
770 (re-search-forward "Rar!" (+ (point) 100000) t))
771 'rar-exe)
b3671a51 772 ((looking-at "7z\274\257\047\034") '7z)
1cd7adc6 773 (t (error "Buffer format not recognized")))))
665211a3 774;; -------------------------------------------------------------------------
7e9a3fef
SM
775
776(defun archive-desummarize ()
777 (let ((inhibit-read-only t)
778 (modified (buffer-modified-p)))
779 (widen)
780 (delete-region (point-min) archive-proper-file-start)
781 (restore-buffer-modified-p modified)))
782
783
380683ed 784(defun archive-summarize (&optional shut-up)
665211a3
KH
785 "Parse the contents of the archive file in the current buffer.
786Place a dired-like listing on the front;
787then narrow to it, so that only that listing
380683ed
EZ
788is visible (and the real data of the buffer is hidden).
789Optional argument SHUT-UP, if non-nil, means don't print messages
790when parsing the archive."
665211a3 791 (widen)
4c478e6b
JL
792 (let ((buffer-file-truename nil) ; avoid changing dir mtime by lock_file
793 (inhibit-read-only t))
7e9a3fef
SM
794 (setq archive-proper-file-start (copy-marker (point-min) t))
795 (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
380683ed
EZ
796 (or shut-up
797 (message "Parsing archive file..."))
665211a3
KH
798 (buffer-disable-undo (current-buffer))
799 (setq archive-files (funcall (archive-name "summarize")))
380683ed
EZ
800 (or shut-up
801 (message "Parsing archive file...done."))
665211a3
KH
802 (setq archive-proper-file-start (point-marker))
803 (narrow-to-region (point-min) (point))
804 (set-buffer-modified-p nil)
805 (buffer-enable-undo))
806 (goto-char archive-file-list-start)
807 (archive-next-line 0))
808
809(defun archive-resummarize ()
810 "Recreate the contents listing of an archive."
7e9a3fef
SM
811 (let ((no (archive-get-lineno)))
812 (archive-desummarize)
380683ed 813 (archive-summarize t)
665211a3
KH
814 (goto-char archive-file-list-start)
815 (archive-next-line no)))
816
817(defun archive-summarize-files (files)
c4de97b4 818 "Insert a description of a list of files annotated with proper mouse face."
665211a3
KH
819 (setq archive-file-list-start (point-marker))
820 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
821 ;; We don't want to do an insert for each element since that takes too
822 ;; long when the archive -- which has to be moved in memory -- is large.
823 (insert
824 (apply
825 (function concat)
826 (mapcar
fdaaf743
SM
827 (lambda (fil)
828 ;; Using `concat' here copies the text also, so we can add
829 ;; properties without problems.
830 (let ((text (concat (aref fil 0) "\n")))
831 (if (featurep 'xemacs)
832 () ; out of luck
833 (add-text-properties
834 (aref fil 1) (aref fil 2)
835 '(mouse-face highlight
836 help-echo "mouse-2: extract this file into a buffer")
837 text))
838 text))
665211a3
KH
839 files)))
840 (setq archive-file-list-end (point-marker)))
841
842(defun archive-alternate-display ()
eeba65ed 843 "Toggle alternative display.
0cf1ef26 844To avoid very long lines archive mode does not show all information.
eeba65ed 845This function changes the set of information shown for each files."
665211a3
KH
846 (interactive)
847 (setq archive-alternate-display (not archive-alternate-display))
848 (archive-resummarize))
849;; -------------------------------------------------------------------------
7e9a3fef 850;;; Section: Local archive copy handling
665211a3 851
380683ed
EZ
852(defun archive-unique-fname (fname dir)
853 "Make sure a file FNAME can be created uniquely in directory DIR.
854
855If FNAME can be uniquely created in DIR, it is returned unaltered.
856If FNAME is something our underlying filesystem can't grok, or if another
857file by that name already exists in DIR, a unique new name is generated
ff39b9a1 858using `make-temp-file', and the generated name is returned."
380683ed 859 (let ((fullname (expand-file-name fname dir))
f5fce4ec
MA
860 (alien (string-match file-name-invalid-regexp fname))
861 (tmpfile
380683ed 862 (expand-file-name
9dacec4c
KS
863 (if (if (fboundp 'msdos-long-file-names)
864 (not (msdos-long-file-names)))
380683ed
EZ
865 "am"
866 "arc-mode.")
f5fce4ec
MA
867 dir)))
868 (if (or alien (file-exists-p fullname))
869 (progn
bbd240ce 870 ;; Make sure all the leading directories in
f5fce4ec
MA
871 ;; archive-local-name exist under archive-tmpdir, so that
872 ;; the directory structure recorded in the archive is
873 ;; reconstructed in the temporary directory.
874 (make-directory (file-name-directory tmpfile) t)
875 (make-temp-file tmpfile))
bbd240ce 876 ;; Make sure all the leading directories in `fullname' exist
f5952338
JL
877 ;; under archive-tmpdir. This is necessary for nested archives
878 ;; (`archive-extract' sets `archive-remote' to t in case
879 ;; an archive occurs inside another archive).
880 (make-directory (file-name-directory fullname) t)
380683ed
EZ
881 fullname)))
882
665211a3 883(defun archive-maybe-copy (archive)
380683ed
EZ
884 (let ((coding-system-for-write 'no-conversion))
885 (if archive-remote
886 (let ((start (point-max))
887 ;; Sometimes ARCHIVE is invalid while its actual name, as
888 ;; recorded in its parent archive, is not. For example, an
889 ;; archive bar.zip inside another archive foo.zip gets a name
890 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
891 ;; So use the actual name if available.
892 (archive-name
893 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
894 archive)))
380683ed
EZ
895 (setq archive-local-name
896 (archive-unique-fname archive-name archive-tmpdir))
897 (save-restriction
898 (widen)
899 (write-region start (point-max) archive-local-name nil 'nomessage))
900 archive-local-name)
901 (if (buffer-modified-p) (save-buffer))
902 archive)))
665211a3
KH
903
904(defun archive-maybe-update (unchanged)
905 (if archive-remote
906 (let ((name archive-local-name)
907 (modified (buffer-modified-p))
380683ed
EZ
908 (coding-system-for-read 'no-conversion)
909 (lno (archive-get-lineno))
e545bb99 910 (inhibit-read-only t))
665211a3 911 (if unchanged nil
380683ed 912 (setq archive-files nil)
665211a3
KH
913 (erase-buffer)
914 (insert-file-contents name)
380683ed
EZ
915 (archive-mode t)
916 (goto-char archive-file-list-start)
917 (archive-next-line lno))
665211a3
KH
918 (archive-delete-local name)
919 (if (not unchanged)
380683ed
EZ
920 (message
921 "Buffer `%s' must be saved for changes to take effect"
922 (buffer-name (current-buffer))))
665211a3
KH
923 (set-buffer-modified-p (or modified (not unchanged))))))
924
925(defun archive-delete-local (name)
eeba65ed 926 "Delete file NAME and its parents up to and including `archive-tmpdir'."
665211a3
KH
927 (let ((again t)
928 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
929 (condition-case nil
930 (delete-file name)
931 (error nil))
932 (while again
933 (setq name (directory-file-name (file-name-directory name)))
934 (condition-case nil
935 (delete-directory name)
936 (error nil))
937 (if (string= name top) (setq again nil)))))
938;; -------------------------------------------------------------------------
7e9a3fef 939;;; Section: Member extraction
665211a3 940
fb3aad66
SM
941(defun archive-try-jka-compr ()
942 (when (and auto-compression-mode
943 (jka-compr-get-compression-info buffer-file-name))
944 (let* ((basename (file-name-nondirectory buffer-file-name))
945 (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
946 (match-string 1 basename) basename))
947 (tmpfile (make-temp-file (file-name-sans-extension tmpname)
948 nil
949 (file-name-extension tmpname 'period))))
950 (unwind-protect
951 (progn
952 (let ((coding-system-for-write 'no-conversion)
953 ;; Don't re-compress this data just before decompressing it.
954 (jka-compr-inhibit t))
955 (write-region (point-min) (point-max) tmpfile nil 'quiet))
956 (erase-buffer)
957 (let ((coding-system-for-read 'no-conversion))
958 (insert-file-contents tmpfile)))
959 (delete-file tmpfile)))))
960
eb93d233
EZ
961(defun archive-file-name-handler (op &rest args)
962 (or (eq op 'file-exists-p)
963 (let ((file-name-handler-alist nil))
964 (apply op args))))
965
966(defun archive-set-buffer-as-visiting-file (filename)
967 "Set the current buffer as if it were visiting FILENAME."
968 (save-excursion
969 (goto-char (point-min))
ab3050bc
JL
970 (let ((buffer-undo-list t)
971 (coding
eb93d233
EZ
972 (or coding-system-for-read
973 (and set-auto-coding-function
06e3b626
EZ
974 (save-excursion
975 (funcall set-auto-coding-function
976 filename (- (point-max) (point-min)))))
e9fe3513
EZ
977 ;; dos-w32.el defines the function
978 ;; find-buffer-file-type-coding-system for DOS/Windows
979 ;; systems which preserves the coding-system of existing files.
980 ;; (That function is called via file-coding-system-alist.)
981 ;; Here, we want it to act as if the extracted file existed.
982 ;; The following let-binding of file-name-handler-alist forces
983 ;; find-file-not-found-set-buffer-file-coding-system to ignore
984 ;; the file's name (see dos-w32.el).
eb93d233
EZ
985 (let ((file-name-handler-alist
986 '(("" . archive-file-name-handler))))
e0b582b8
KH
987 (car (find-operation-coding-system
988 'insert-file-contents
989 (cons filename (current-buffer)) t))))))
fb3aad66
SM
990 (unless (or coding-system-for-read
991 enable-multibyte-characters)
992 (setq coding
993 (coding-system-change-text-conversion coding 'raw-text)))
994 (unless (memq coding '(nil no-conversion))
995 (decode-coding-region (point-min) (point-max) coding)
eb93d233
EZ
996 (setq last-coding-system-used coding))
997 (set-buffer-modified-p nil)
998 (kill-local-variable 'buffer-file-coding-system)
a38ac4c2 999 (after-insert-file-set-coding (- (point-max) (point-min))))))
eb93d233 1000
fdaaf743 1001(define-obsolete-function-alias 'archive-mouse-extract 'archive-extract "22.1")
665211a3 1002
fdaaf743 1003(defun archive-extract (&optional other-window-p event)
665211a3 1004 "In archive mode, extract this entry of the archive into its own buffer."
fdaaf743 1005 (interactive (list nil last-input-event))
440e20fc 1006 (if event (posn-set-point (event-end event)))
665211a3
KH
1007 (let* ((view-p (eq other-window-p 'view))
1008 (descr (archive-get-descr))
1009 (ename (aref descr 0))
1010 (iname (aref descr 1))
1011 (archive-buffer (current-buffer))
1012 (arcdir default-directory)
1013 (archive (buffer-file-name))
1014 (arcname (file-name-nondirectory archive))
1015 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
1016 (extractor (archive-name "extract"))
380683ed
EZ
1017 ;; Members with file names which aren't valid for the
1018 ;; underlying filesystem, are treated as read-only.
1019 (read-only-p (or archive-read-only
1020 view-p
4dbbd6a1 1021 (string-match file-name-invalid-regexp ename)))
9b1fad33 1022 (arcfilename (expand-file-name (concat arcname ":" iname)))
665211a3 1023 (buffer (get-buffer bufname))
ad38511a
KH
1024 (just-created nil)
1025 (file-name-coding archive-file-name-coding-system))
9b1fad33
CY
1026 (if (and buffer
1027 (string= (buffer-file-name buffer) arcfilename))
665211a3
KH
1028 nil
1029 (setq archive (archive-maybe-copy archive))
9b1fad33 1030 (setq bufname (generate-new-buffer-name bufname))
665211a3
KH
1031 (setq buffer (get-buffer-create bufname))
1032 (setq just-created t)
e545bb99 1033 (with-current-buffer buffer
9b1fad33 1034 (setq buffer-file-name arcfilename)
665211a3
KH
1035 (setq buffer-file-truename
1036 (abbreviate-file-name buffer-file-name))
1037 ;; Set the default-directory to the dir of the superior buffer.
1038 (setq default-directory arcdir)
1039 (make-local-variable 'archive-superior-buffer)
1040 (setq archive-superior-buffer archive-buffer)
fdaaf743 1041 (add-hook 'write-file-functions 'archive-write-file-member nil t)
665211a3 1042 (setq archive-subfile-mode descr)
ad38511a 1043 (setq archive-file-name-coding-system file-name-coding)
b48fa570
EZ
1044 (if (and
1045 (null
91af3942 1046 (let (;; We may have to encode the file name argument for
eb93d233 1047 ;; external programs.
7e5ad777
KH
1048 (coding-system-for-write
1049 (and enable-multibyte-characters
ad38511a 1050 archive-file-name-coding-system))
eb93d233
EZ
1051 ;; We read an archive member by no-conversion at
1052 ;; first, then decode appropriately by calling
1053 ;; archive-set-buffer-as-visiting-file later.
1054 (coding-system-for-read 'no-conversion))
1055 (condition-case err
1056 (if (fboundp extractor)
1057 (funcall extractor archive ename)
1058 (archive-*-extract archive ename
1059 (symbol-value extractor)))
1060 (error
1061 (ding (message "%s" (error-message-string err)))
1062 nil))))
b48fa570
EZ
1063 just-created)
1064 (progn
1065 (set-buffer-modified-p nil)
1066 (kill-buffer buffer))
fb3aad66 1067 (archive-try-jka-compr) ;Pretty ugly hack :-(
eb93d233 1068 (archive-set-buffer-as-visiting-file ename)
b48fa570
EZ
1069 (goto-char (point-min))
1070 (rename-buffer bufname)
1071 (setq buffer-read-only read-only-p)
1072 (setq buffer-undo-list nil)
1073 (set-buffer-modified-p nil)
1074 (setq buffer-saved-size (buffer-size))
1075 (normal-mode)
1076 ;; Just in case an archive occurs inside another archive.
fdaaf743
SM
1077 (when (derived-mode-p 'archive-mode)
1078 (setq archive-remote t)
1079 (if read-only-p (setq archive-read-only t))
1080 ;; We will write out the archive ourselves if it is
1081 ;; part of another archive.
1082 (remove-hook 'write-contents-functions 'archive-write-file t))
d1069532 1083 (run-hooks 'archive-extract-hook)
380683ed
EZ
1084 (if archive-read-only
1085 (message "Note: altering this archive is not implemented."))))
1086 (archive-maybe-update t))
b48fa570 1087 (or (not (buffer-name buffer))
fdaaf743 1088 (cond
fd5c9dfa
JL
1089 (view-p
1090 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
fdaaf743
SM
1091 ((eq other-window-p 'display) (display-buffer buffer))
1092 (other-window-p (switch-to-buffer-other-window buffer))
c3313451 1093 (t (switch-to-buffer buffer))))))
665211a3
KH
1094
1095(defun archive-*-extract (archive name command)
1096 (let* ((default-directory (file-name-as-directory archive-tmpdir))
1097 (tmpfile (expand-file-name (file-name-nondirectory name)
b48fa570
EZ
1098 default-directory))
1099 exit-status success)
665211a3 1100 (make-directory (directory-file-name default-directory) t)
b48fa570
EZ
1101 (setq exit-status
1102 (apply 'call-process
1103 (car command)
1104 nil
1105 nil
1106 nil
1107 (append (cdr command) (list archive name))))
3516a018 1108 (cond ((and (numberp exit-status) (zerop exit-status))
b48fa570
EZ
1109 (if (not (file-exists-p tmpfile))
1110 (ding (message "`%s': no such file or directory" tmpfile))
1111 (insert-file-contents tmpfile)
1112 (setq success t)))
1113 ((numberp exit-status)
1114 (ding
1115 (message "`%s' exited with status %d" (car command) exit-status)))
1116 ((stringp exit-status)
1117 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1118 (t
1119 (ding (message "`%s' failed" (car command)))))
1120 (archive-delete-local tmpfile)
1121 success))
665211a3 1122
53baf48a
JL
1123(defun archive-extract-by-stdout (archive name command &optional stderr-test)
1124 (let ((stderr-file (make-temp-file "arc-stderr")))
1125 (unwind-protect
1126 (prog1
1127 (apply 'call-process
1128 (car command)
1129 nil
1130 (if stderr-file (list t stderr-file) t)
1131 nil
1132 (append (cdr command) (list archive name)))
1133 (with-temp-buffer
1134 (insert-file-contents stderr-file)
1135 (goto-char (point-min))
1136 (when (if (stringp stderr-test)
1137 (not (re-search-forward stderr-test nil t))
1138 (> (buffer-size) 0))
1139 (message "%s" (buffer-string)))))
1140 (if (file-exists-p stderr-file)
1141 (delete-file stderr-file)))))
1142
1143(defun archive-extract-by-file (archive name command &optional stdout-test)
1144 (let ((dest (make-temp-file "arc-dir" 'dir))
1145 (stdout-file (make-temp-file "arc-stdout")))
1146 (unwind-protect
1147 (prog1
1148 (apply 'call-process
1149 (car command)
1150 nil
1151 `(:file ,stdout-file)
1152 nil
1153 (append (cdr command) (list archive name dest)))
1154 (with-temp-buffer
1155 (insert-file-contents stdout-file)
1156 (goto-char (point-min))
1157 (when (if (stringp stdout-test)
1158 (not (re-search-forward stdout-test nil t))
1159 (> (buffer-size) 0))
1160 (message "%s" (buffer-string))))
1161 (if (file-exists-p (expand-file-name name dest))
1162 (insert-file-contents-literally (expand-file-name name dest))))
1163 (if (file-exists-p stdout-file)
1164 (delete-file stdout-file))
1165 (if (file-exists-p (expand-file-name name dest))
1166 (delete-file (expand-file-name name dest)))
1167 (while (file-name-directory name)
1168 (setq name (directory-file-name (file-name-directory name)))
1169 (delete-directory (expand-file-name name dest)))
1170 (delete-directory dest))))
665211a3
KH
1171
1172(defun archive-extract-other-window ()
1173 "In archive mode, find this member in another window."
1174 (interactive)
1175 (archive-extract t))
1176
1177(defun archive-display-other-window ()
1178 "In archive mode, display this member in another window."
1179 (interactive)
1180 (archive-extract 'display))
1181
1182(defun archive-view ()
1183 "In archive mode, view the member on this line."
1184 (interactive)
1185 (archive-extract 'view))
1186
1187(defun archive-add-new-member (arcbuf name)
eeba65ed 1188 "Add current buffer to the archive in ARCBUF naming it NAME."
665211a3
KH
1189 (interactive
1190 (list (get-buffer
1191 (read-buffer "Buffer containing archive: "
1192 ;; Find first archive buffer and suggest that
1193 (let ((bufs (buffer-list)))
e545bb99
SM
1194 (while (and bufs
1195 (not (with-current-buffer (car bufs)
1196 (derived-mode-p 'archive-mode))))
1197 (setq bufs (cdr bufs)))
665211a3
KH
1198 (if bufs
1199 (car bufs)
1200 (error "There are no archive buffers")))
1201 t))
1202 (read-string "File name in archive: "
1203 (if buffer-file-name
1204 (file-name-nondirectory buffer-file-name)
1205 ""))))
e545bb99 1206 (with-current-buffer arcbuf
3516a018 1207 (or (derived-mode-p 'archive-mode)
665211a3
KH
1208 (error "Buffer is not an archive buffer"))
1209 (if archive-read-only
1210 (error "Archive is read-only")))
1211 (if (eq arcbuf (current-buffer))
1212 (error "An archive buffer cannot be added to itself"))
1213 (if (string= name "")
1214 (error "Archive members may not be given empty names"))
e545bb99
SM
1215 (let ((func (with-current-buffer arcbuf
1216 (archive-name "add-new-member")))
665211a3
KH
1217 (membuf (current-buffer)))
1218 (if (fboundp func)
e545bb99 1219 (with-current-buffer arcbuf
665211a3
KH
1220 (funcall func buffer-file-name membuf name))
1221 (error "Adding a new member is not supported for this archive type"))))
1222;; -------------------------------------------------------------------------
7e9a3fef 1223;;; Section: IO stuff
665211a3 1224
665211a3 1225(defun archive-write-file-member ()
b48fa570
EZ
1226 (save-excursion
1227 (save-restriction
1228 (message "Updating archive...")
1229 (widen)
e545bb99
SM
1230 (let ((writer (with-current-buffer archive-superior-buffer
1231 (archive-name "write-file-member")))
1232 (archive (with-current-buffer archive-superior-buffer
1233 (archive-maybe-copy (buffer-file-name)))))
b48fa570
EZ
1234 (if (fboundp writer)
1235 (funcall writer archive archive-subfile-mode)
1236 (archive-*-write-file-member archive
1237 archive-subfile-mode
380683ed
EZ
1238 (symbol-value writer)))
1239 (set-buffer-modified-p nil)
1240 (message "Updating archive...done"))
b48fa570 1241 (set-buffer archive-superior-buffer)
380683ed
EZ
1242 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1243 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1244 ;; won't reset the coding-system of this archive member.
1245 (if (local-variable-p 'archive-member-coding-system)
1246 (setq last-coding-system-used archive-member-coding-system))
1247 t)
665211a3
KH
1248
1249(defun archive-*-write-file-member (archive descr command)
1250 (let* ((ename (aref descr 0))
1251 (tmpfile (expand-file-name ename archive-tmpdir))
1252 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1253 (default-directory (file-name-as-directory top)))
1254 (unwind-protect
1255 (progn
1256 (make-directory (file-name-directory tmpfile) t)
380683ed
EZ
1257 ;; If the member is itself an archive, write it without
1258 ;; the dired-like listing we created.
1259 (if (eq major-mode 'archive-mode)
1260 (archive-write-file tmpfile)
ab1d3835 1261 (write-region nil nil tmpfile nil 'nomessage))
380683ed
EZ
1262 ;; basic-save-buffer needs last-coding-system-used to have
1263 ;; the value used to write the file, so save it before any
1264 ;; further processing clobbers it (we restore it in
1265 ;; archive-write-file-member, above).
1266 (setq archive-member-coding-system last-coding-system-used)
665211a3
KH
1267 (if (aref descr 3)
1268 ;; Set the file modes, but make sure we can read it.
1269 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
ad38511a
KH
1270 (setq ename
1271 (encode-coding-string ename archive-file-name-coding-system))
1272 (let* ((coding-system-for-write 'no-conversion)
1273 (exitcode (apply 'call-process
1274 (car command)
1275 nil
1276 nil
1277 nil
1278 (append (cdr command)
1279 (list archive ename)))))
3516a018
JPW
1280 (or (zerop exitcode)
1281 (error "Updating was unsuccessful (%S)" exitcode))))
665211a3
KH
1282 (archive-delete-local tmpfile))))
1283
380683ed 1284(defun archive-write-file (&optional file)
665211a3 1285 (save-excursion
380683ed
EZ
1286 (let ((coding-system-for-write 'no-conversion))
1287 (write-region archive-proper-file-start (point-max)
1288 (or file buffer-file-name) nil t)
1289 (set-buffer-modified-p nil))
665211a3
KH
1290 t))
1291;; -------------------------------------------------------------------------
7e9a3fef 1292;;; Section: Marking and unmarking.
665211a3
KH
1293
1294(defun archive-flag-deleted (p &optional type)
1295 "In archive mode, mark this member to be deleted from the archive.
1296With a prefix argument, mark that many files."
1297 (interactive "p")
1298 (or type (setq type ?D))
1299 (beginning-of-line)
1300 (let ((sign (if (>= p 0) +1 -1))
1301 (modified (buffer-modified-p))
e545bb99 1302 (inhibit-read-only t))
665211a3
KH
1303 (while (not (zerop p))
1304 (if (archive-get-descr t)
1305 (progn
1306 (delete-char 1)
1307 (insert type)))
1308 (forward-line sign)
1309 (setq p (- p sign)))
e545bb99 1310 (restore-buffer-modified-p modified))
665211a3
KH
1311 (archive-next-line 0))
1312
1313(defun archive-unflag (p)
1314 "In archive mode, un-mark this member if it is marked to be deleted.
1315With a prefix argument, un-mark that many files forward."
1316 (interactive "p")
0cf1ef26 1317 (archive-flag-deleted p ?\s))
665211a3
KH
1318
1319(defun archive-unflag-backwards (p)
1320 "In archive mode, un-mark this member if it is marked to be deleted.
1321With a prefix argument, un-mark that many members backward."
1322 (interactive "p")
0cf1ef26 1323 (archive-flag-deleted (- p) ?\s))
665211a3
KH
1324
1325(defun archive-unmark-all-files ()
1326 "Remove all marks."
1327 (interactive)
1328 (let ((modified (buffer-modified-p))
e545bb99 1329 (inhibit-read-only t))
665211a3
KH
1330 (save-excursion
1331 (goto-char archive-file-list-start)
1332 (while (< (point) archive-file-list-end)
0cf1ef26
JB
1333 (or (= (following-char) ?\s)
1334 (progn (delete-char 1) (insert ?\s)))
665211a3 1335 (forward-line 1)))
e545bb99 1336 (restore-buffer-modified-p modified)))
665211a3
KH
1337
1338(defun archive-mark (p)
1339 "In archive mode, mark this member for group operations.
1340With a prefix argument, mark that many members.
1341Use \\[archive-unmark-all-files] to remove all marks."
1342 (interactive "p")
1343 (archive-flag-deleted p ?*))
1344
1345(defun archive-get-marked (mark &optional default)
1346 (let (files)
1347 (save-excursion
1348 (goto-char archive-file-list-start)
1349 (while (< (point) archive-file-list-end)
1350 (if (= (following-char) mark)
1351 (setq files (cons (archive-get-descr) files)))
1352 (forward-line 1)))
1353 (or (nreverse files)
1354 (and default
1355 (list (archive-get-descr))))))
1356;; -------------------------------------------------------------------------
7e9a3fef 1357;;; Section: Operate
665211a3
KH
1358
1359(defun archive-next-line (p)
1360 (interactive "p")
1361 (forward-line p)
1362 (or (eobp)
1363 (forward-char archive-file-name-indent)))
1364
1365(defun archive-previous-line (p)
1366 (interactive "p")
1367 (archive-next-line (- p)))
1368
1369(defun archive-chmod-entry (new-mode)
eeba65ed 1370 "Change the protection bits associated with all marked or this member.
665211a3 1371The new protection bits can either be specified as an octal number or
0cf1ef26 1372as a relative change like \"g+rw\" as for chmod(2)."
665211a3
KH
1373 (interactive "sNew mode (octal or relative): ")
1374 (if archive-read-only (error "Archive is read-only"))
1375 (let ((func (archive-name "chmod-entry")))
1376 (if (fboundp func)
1377 (progn
1378 (funcall func new-mode (archive-get-marked ?* t))
1379 (archive-resummarize))
1380 (error "Setting mode bits is not supported for this archive type"))))
1381
1382(defun archive-chown-entry (new-uid)
1383 "Change the owner of all marked or this member."
1384 (interactive "nNew uid: ")
1385 (if archive-read-only (error "Archive is read-only"))
1386 (let ((func (archive-name "chown-entry")))
1387 (if (fboundp func)
1388 (progn
1389 (funcall func new-uid (archive-get-marked ?* t))
1390 (archive-resummarize))
1391 (error "Setting owner is not supported for this archive type"))))
1392
1393(defun archive-chgrp-entry (new-gid)
1394 "Change the group of all marked or this member."
1395 (interactive "nNew gid: ")
1396 (if archive-read-only (error "Archive is read-only"))
1397 (let ((func (archive-name "chgrp-entry")))
1398 (if (fboundp func)
1399 (progn
1400 (funcall func new-gid (archive-get-marked ?* t))
1401 (archive-resummarize))
1402 (error "Setting group is not supported for this archive type"))))
1403
1404(defun archive-expunge ()
1405 "Do the flagged deletions."
1406 (interactive)
1407 (let (files)
1408 (save-excursion
1409 (goto-char archive-file-list-start)
1410 (while (< (point) archive-file-list-end)
1411 (if (= (following-char) ?D)
1412 (setq files (cons (aref (archive-get-descr) 0) files)))
1413 (forward-line 1)))
1414 (setq files (nreverse files))
1415 (and files
1416 (or (not archive-read-only)
1417 (error "Archive is read-only"))
1418 (or (yes-or-no-p (format "Really delete %d member%s? "
1419 (length files)
1420 (if (null (cdr files)) "" "s")))
1421 (error "Operation aborted"))
1422 (let ((archive (archive-maybe-copy (buffer-file-name)))
1423 (expunger (archive-name "expunge")))
1424 (if (fboundp expunger)
1425 (funcall expunger archive files)
1426 (archive-*-expunge archive files (symbol-value expunger)))
1427 (archive-maybe-update nil)
1428 (if archive-remote
1429 (archive-resummarize)
1430 (revert-buffer))))))
1431
1432(defun archive-*-expunge (archive files command)
1433 (apply 'call-process
1434 (car command)
1435 nil
1436 nil
1437 nil
1438 (append (cdr command) (cons archive files))))
1439
1440(defun archive-rename-entry (newname)
fdaaf743 1441 "Change the name associated with this entry in the archive file."
665211a3
KH
1442 (interactive "sNew name: ")
1443 (if archive-read-only (error "Archive is read-only"))
1444 (if (string= newname "")
1445 (error "Archive members may not be given empty names"))
1446 (let ((func (archive-name "rename-entry"))
1447 (descr (archive-get-descr)))
1448 (if (fboundp func)
1449 (progn
fdaaf743 1450 (funcall func
ad38511a
KH
1451 (encode-coding-string newname
1452 archive-file-name-coding-system)
eb93d233 1453 descr)
665211a3
KH
1454 (archive-resummarize))
1455 (error "Renaming is not supported for this archive type"))))
1456
1457;; Revert the buffer and recompute the dired-like listing.
06b60517 1458(defun archive-mode-revert (&optional _no-auto-save _no-confirm)
665211a3
KH
1459 (let ((no (archive-get-lineno)))
1460 (setq archive-files nil)
380683ed
EZ
1461 (let ((revert-buffer-function nil)
1462 (coding-system-for-read 'no-conversion))
665211a3
KH
1463 (revert-buffer t t))
1464 (archive-mode)
1465 (goto-char archive-file-list-start)
1466 (archive-next-line no)))
1467
1468(defun archive-undo ()
1469 "Undo in an archive buffer.
1470This doesn't recover lost files, it just undoes changes in the buffer itself."
1471 (interactive)
e545bb99 1472 (let ((inhibit-read-only t))
665211a3
KH
1473 (undo)))
1474;; -------------------------------------------------------------------------
7e9a3fef 1475;;; Section: Arc Archives
665211a3
KH
1476
1477(defun archive-arc-summarize ()
1478 (let ((p 1)
1479 (totalsize 0)
1480 (maxlen 8)
1481 files
1482 visual)
1483 (while (and (< (+ p 29) (point-max))
ad38511a
KH
1484 (= (byte-after p) ?\C-z)
1485 (> (byte-after (1+ p)) 0))
665211a3
KH
1486 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1487 (fnlen (or (string-match "\0" namefld) 13))
ad38511a
KH
1488 (efnname (decode-coding-string (substring namefld 0 fnlen)
1489 archive-file-name-coding-system))
a12aece3
EZ
1490 ;; Convert to float to avoid overflow for very large files.
1491 (csize (archive-l-e (+ p 15) 4 'float))
665211a3
KH
1492 (moddate (archive-l-e (+ p 19) 2))
1493 (modtime (archive-l-e (+ p 21) 2))
a12aece3 1494 (ucsize (archive-l-e (+ p 25) 4 'float))
665211a3
KH
1495 (fiddle (string= efnname (upcase efnname)))
1496 (ifnname (if fiddle (downcase efnname) efnname))
a12aece3 1497 (text (format " %8.0f %-11s %-8s %s"
665211a3
KH
1498 ucsize
1499 (archive-dosdate moddate)
1500 (archive-dostime modtime)
1501 ifnname)))
1502 (setq maxlen (max maxlen fnlen)
1503 totalsize (+ totalsize ucsize)
1504 visual (cons (vector text
1505 (- (length text) (length ifnname))
1506 (length text))
1507 visual)
1508 files (cons (vector efnname ifnname fiddle nil (1- p))
1509 files)
7893e589
EZ
1510 ;; p needs to stay an integer, since we use it in char-after
1511 ;; above. Passing through `round' limits the compressed size
1512 ;; to most-positive-fixnum, but if the compressed size exceeds
1513 ;; that, we cannot visit the archive anyway.
1514 p (+ p 29 (round csize)))))
665211a3
KH
1515 (goto-char (point-min))
1516 (let ((dash (concat "- -------- ----------- -------- "
1517 (make-string maxlen ?-)
1518 "\n")))
1519 (insert "M Length Date Time File\n"
1520 dash)
1521 (archive-summarize-files (nreverse visual))
1522 (insert dash
a12aece3 1523 (format " %8.0f %d file%s"
665211a3
KH
1524 totalsize
1525 (length files)
1526 (if (= 1 (length files)) "" "s"))
1527 "\n"))
1528 (apply 'vector (nreverse files))))
1529
fdaaf743 1530(defun archive-arc-rename-entry (newname descr)
665211a3 1531 (if (string-match "[:\\\\/]" newname)
9dacec4c 1532 (error "File names in arc files must not contain a directory component"))
665211a3
KH
1533 (if (> (length newname) 12)
1534 (error "File names in arc files are limited to 12 characters"))
1535 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1536 (length newname))))
e545bb99 1537 (inhibit-read-only t))
665211a3
KH
1538 (save-restriction
1539 (save-excursion
1540 (widen)
1541 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1542 (delete-char 13)
ad38511a 1543 (insert-unibyte name)))))
665211a3 1544;; -------------------------------------------------------------------------
7e9a3fef 1545;;; Section: Lzh Archives
665211a3 1546
fcb006c4
CY
1547(defun archive-lzh-summarize (&optional start)
1548 (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
665211a3
KH
1549 (totalsize 0)
1550 (maxlen 8)
1551 files
1552 visual)
5f23d836 1553 (while (progn (goto-char p) ;beginning of a base header.
a56636ae 1554 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
8f924df7 1555 (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
a12aece3
EZ
1556 ;; Convert to float to avoid overflow for very large files.
1557 (csize (archive-l-e (+ p 7) 4 'float)) ;size of a compressed file to follow (level 0 and 2),
a28fe04b 1558 ;size of extended headers + the compressed file to follow (level 1).
a12aece3 1559 (ucsize (archive-l-e (+ p 11) 4 'float)) ;size of an uncompressed file.
5f23d836
RS
1560 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1561 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
8f924df7 1562 (hdrlvl (byte-after (+ p 20))) ;header level
5f23d836 1563 thsize ;total header size (base + extensions)
bdbfe3bf 1564 fnlen efnname osid fiddle ifnname width p2
5f23d836
RS
1565 neh ;beginning of next extension header (level 1 and 2)
1566 mode modestr uid gid text dir prname
1567 gname uname modtime moddate)
1568 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1569 (when (or (= hdrlvl 0) (= hdrlvl 1))
8f924df7 1570 (setq fnlen (byte-after (+ p 21))) ;filename length
5f23d836 1571 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
ad38511a
KH
1572 (decode-coding-string
1573 str archive-file-name-coding-system)))
5f23d836
RS
1574 (setq p2 (+ p 22 fnlen))) ;
1575 (if (= hdrlvl 1)
fdaaf743 1576 (setq neh (+ p2 3)) ;specific to level 1 header
5f23d836 1577 (if (= hdrlvl 2)
fdaaf743 1578 (setq neh (+ p 24)))) ;specific to level 2 header
5f23d836
RS
1579 (if neh ;if level 1 or 2 we expect extension headers to follow
1580 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
8f924df7 1581 (etype (byte-after (+ neh 2)))) ;extension type
5f23d836 1582 (while (not (= ehsize 0))
a56636ae 1583 (cond
5f23d836
RS
1584 ((= etype 1) ;file name
1585 (let ((i (+ neh 3)))
1586 (while (< i (+ neh ehsize))
8f924df7 1587 (setq efnname (concat efnname (char-to-string (byte-after i))))
5f23d836
RS
1588 (setq i (1+ i)))))
1589 ((= etype 2) ;directory name
1590 (let ((i (+ neh 3)))
1591 (while (< i (+ neh ehsize))
9dacec4c 1592 (setq dir (concat dir
ad38511a 1593 (if (= (byte-after i)
a56636ae
RS
1594 255)
1595 "/"
1596 (char-to-string
1597 (char-after i)))))
1598 (setq i (1+ i)))))
5f23d836
RS
1599 ((= etype 80) ;Unix file permission
1600 (setq mode (archive-l-e (+ neh 3) 2)))
1601 ((= etype 81) ;UNIX file group/user ID
1602 (progn (setq uid (archive-l-e (+ neh 3) 2))
1603 (setq gid (archive-l-e (+ neh 5) 2))))
1604 ((= etype 82) ;UNIX file group name
1605 (let ((i (+ neh 3)))
1606 (while (< i (+ neh ehsize))
1607 (setq gname (concat gname (char-to-string (char-after i))))
1608 (setq i (1+ i)))))
1609 ((= etype 83) ;UNIX file user name
1610 (let ((i (+ neh 3)))
1611 (while (< i (+ neh ehsize))
1612 (setq uname (concat uname (char-to-string (char-after i))))
1613 (setq i (1+ i)))))
a56636ae 1614 )
5f23d836
RS
1615 (setq neh (+ neh ehsize))
1616 (setq ehsize (archive-l-e neh 2))
8f924df7 1617 (setq etype (byte-after (+ neh 2))))
5f23d836
RS
1618 ;;get total header size for level 1 and 2 headers
1619 (setq thsize (- neh p))))
1620 (if (= hdrlvl 0) ;total header size
1621 (setq thsize hsize))
bdbfe3bf
CY
1622 ;; OS ID field not present in level 0 header, use code 0 "generic"
1623 ;; in that case as per lha program header.c get_header()
1624 (setq osid (cond ((= hdrlvl 0) 0)
1625 ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
1626 ((= hdrlvl 2) (char-after (+ p 23)))))
1627 ;; Filename fiddling must follow the lha program, otherwise the name
1628 ;; passed to "lha pq" etc won't match (which for an extract silently
1629 ;; results in no output). As of version 1.14i it goes from the OS ID,
1630 ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
1631 ;; converts "\" to "/".
1632 ;; - For 0 generic: generic_to_unix_filename() downcases if there's
1633 ;; no lower case already present, and converts "\" to "/".
1634 ;; - For 'm' MacOS: macos_to_unix_filename() changes "/" to ":" and
1635 ;; ":" to "/"
1636 (setq fiddle (cond ((= ?M osid) t)
1637 ((= 0 osid) (string= efnname (upcase efnname)))))
5f23d836 1638 (setq ifnname (if fiddle (downcase efnname) efnname))
9dacec4c 1639 (setq prname (if dir (concat dir ifnname) ifnname))
d2c6d975 1640 (setq width (if prname (string-width prname) 0))
a56636ae 1641 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
5f23d836
RS
1642 (setq moddate (if (= hdrlvl 2)
1643 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1644 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1645 (setq modtime (if (= hdrlvl 2)
1646 (archive-unixtime time1 time2)
1647 (archive-dostime time1)))
a56636ae 1648 (setq text (if archive-alternate-display
a12aece3 1649 (format " %8.0f %5S %5S %s"
665211a3
KH
1650 ucsize
1651 (or uid "?")
1652 (or gid "?")
1653 ifnname)
a12aece3 1654 (format " %10s %8.0f %-11s %-8s %s"
665211a3
KH
1655 modestr
1656 ucsize
5f23d836
RS
1657 moddate
1658 modtime
1659 prname)))
eb93d233 1660 (setq maxlen (max maxlen width)
665211a3
KH
1661 totalsize (+ totalsize ucsize)
1662 visual (cons (vector text
5f23d836 1663 (- (length text) (length prname))
665211a3
KH
1664 (length text))
1665 visual)
a56636ae 1666 files (cons (vector prname ifnname fiddle mode (1- p))
a28fe04b
RS
1667 files))
1668 (cond ((= hdrlvl 1)
7893e589
EZ
1669 ;; p needs to stay an integer, since we use it in goto-char
1670 ;; above. Passing through `round' limits the compressed size
1671 ;; to most-positive-fixnum, but if the compressed size exceeds
1672 ;; that, we cannot visit the archive anyway.
1673 (setq p (+ p hsize 2 (round csize))))
a28fe04b 1674 ((or (= hdrlvl 2) (= hdrlvl 0))
7893e589 1675 (setq p (+ p thsize 2 (round csize)))))
a28fe04b 1676 ))
665211a3
KH
1677 (goto-char (point-min))
1678 (let ((dash (concat (if archive-alternate-display
1679 "- -------- ----- ----- "
1680 "- ---------- -------- ----------- -------- ")
1681 (make-string maxlen ?-)
1682 "\n"))
1683 (header (if archive-alternate-display
1684 "M Length Uid Gid File\n"
1685 "M Filemode Length Date Time File\n"))
1686 (sumline (if archive-alternate-display
a12aece3
EZ
1687 " %8.0f %d file%s"
1688 " %8.0f %d file%s")))
665211a3
KH
1689 (insert header dash)
1690 (archive-summarize-files (nreverse visual))
1691 (insert dash
1692 (format sumline
1693 totalsize
1694 (length files)
1695 (if (= 1 (length files)) "" "s"))
1696 "\n"))
1697 (apply 'vector (nreverse files))))
1698
1699(defconst archive-lzh-alternate-display t)
1700
1701(defun archive-lzh-extract (archive name)
1702 (archive-extract-by-stdout archive name archive-lzh-extract))
1703
1704(defun archive-lzh-resum (p count)
1705 (let ((sum 0))
1706 (while (> count 0)
1707 (setq count (1- count)
ad38511a 1708 sum (+ sum (byte-after p))
665211a3
KH
1709 p (1+ p)))
1710 (logand sum 255)))
1711
fdaaf743 1712(defun archive-lzh-rename-entry (newname descr)
665211a3
KH
1713 (save-restriction
1714 (save-excursion
1715 (widen)
1716 (let* ((p (+ archive-proper-file-start (aref descr 4)))
ad38511a
KH
1717 (oldhsize (byte-after p))
1718 (oldfnlen (byte-after (+ p 21)))
665211a3
KH
1719 (newfnlen (length newname))
1720 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
e545bb99 1721 (inhibit-read-only t))
665211a3
KH
1722 (if (> newhsize 255)
1723 (error "The file name is too long"))
1724 (goto-char (+ p 21))
1725 (delete-char (1+ oldfnlen))
ad38511a 1726 (insert-unibyte newfnlen newname)
665211a3
KH
1727 (goto-char p)
1728 (delete-char 2)
ad38511a 1729 (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
665211a3
KH
1730
1731(defun archive-lzh-ogm (newval files errtxt ofs)
f2cd8aca
SM
1732 (save-excursion
1733 (save-restriction
665211a3 1734 (widen)
e545bb99
SM
1735 (dolist (fil files)
1736 (let* ((p (+ archive-proper-file-start (aref fil 4)))
ad38511a
KH
1737 (hsize (byte-after p))
1738 (fnlen (byte-after (+ p 21)))
665211a3 1739 (p2 (+ p 22 fnlen))
ad38511a 1740 (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
e545bb99 1741 (inhibit-read-only t))
665211a3
KH
1742 (if (= creator ?U)
1743 (progn
1744 (or (numberp newval)
1745 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1746 (goto-char (+ p2 ofs))
1747 (delete-char 2)
ad38511a 1748 (insert-unibyte (logand newval 255) (lsh newval -8))
665211a3
KH
1749 (goto-char (1+ p))
1750 (delete-char 1)
ad38511a 1751 (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
665211a3 1752 (message "Member %s does not have %s field"
e545bb99 1753 (aref fil 1) errtxt)))))))
665211a3
KH
1754
1755(defun archive-lzh-chown-entry (newuid files)
1756 (archive-lzh-ogm newuid files "an uid" 10))
1757
1758(defun archive-lzh-chgrp-entry (newgid files)
1759 (archive-lzh-ogm newgid files "a gid" 12))
1760
1761(defun archive-lzh-chmod-entry (newmode files)
1762 (archive-lzh-ogm
1763 ;; This should work even though newmode will be dynamically accessed.
fdaaf743 1764 (lambda (old) (archive-calc-mode old newmode t))
665211a3 1765 files "a unix-style mode" 8))
fcb006c4
CY
1766
1767;; -------------------------------------------------------------------------
7e9a3fef 1768;;; Section: Lzh Self-Extracting .exe Archives
fcb006c4
CY
1769;;
1770;; No support for modifying these files. It looks like the lha for unix
1771;; program (as of version 1.14i) can't create or retain the DOS exe part.
1772;; If you do an "lha a" on a .exe for instance it renames and writes to a
1773;; plain .lzh.
1774
1775(defun archive-lzh-exe-summarize ()
1776 "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
1777
1778 ;; Skip the initial executable code part and apply archive-lzh-summarize
1779 ;; to the archive part proper. The "-lh5-" etc regexp here for the start
1780 ;; is the same as in archive-find-type.
1781 ;;
1782 ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
1783 ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
1784 ;; the header checksum, or level 2 a test of the "attribute" and size.
1785 ;;
1786 (re-search-forward "..-l[hz][0-9ds]-" nil)
1787 (archive-lzh-summarize (match-beginning 0)))
1788
1789;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
1790;; .lzh files
1791(defalias 'archive-lzh-exe-extract 'archive-lzh-extract
1792 "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
1793
665211a3 1794;; -------------------------------------------------------------------------
7e9a3fef 1795;;; Section: Zip Archives
665211a3
KH
1796
1797(defun archive-zip-summarize ()
1798 (goto-char (- (point-max) (- 22 18)))
1799 (search-backward-regexp "[P]K\005\006")
8627813e 1800 (let ((p (+ (point-min) (archive-l-e (+ (point) 16) 4)))
665211a3
KH
1801 (maxlen 8)
1802 (totalsize 0)
1803 files
1804 visual)
1805 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
ad38511a 1806 (let* ((creator (byte-after (+ p 5)))
fdaaf743 1807 ;; (method (archive-l-e (+ p 10) 2))
665211a3
KH
1808 (modtime (archive-l-e (+ p 12) 2))
1809 (moddate (archive-l-e (+ p 14) 2))
a12aece3
EZ
1810 ;; Convert to float to avoid overflow for very large files.
1811 (ucsize (archive-l-e (+ p 24) 4 'float))
665211a3
KH
1812 (fnlen (archive-l-e (+ p 28) 2))
1813 (exlen (archive-l-e (+ p 30) 2))
845720b9 1814 (fclen (archive-l-e (+ p 32) 2))
665211a3 1815 (lheader (archive-l-e (+ p 42) 4))
eb93d233 1816 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
ad38511a
KH
1817 (decode-coding-string
1818 str archive-file-name-coding-system)))
665211a3
KH
1819 (isdir (and (= ucsize 0)
1820 (string= (file-name-nondirectory efnname) "")))
7c2fb837 1821 (mode (cond ((memq creator '(2 3)) ; Unix
665211a3 1822 (archive-l-e (+ p 40) 2))
380683ed 1823 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
665211a3
KH
1824 (logior ?\444
1825 (if isdir (logior 16384 ?\111) 0)
1826 (if (zerop
ad38511a 1827 (logand 1 (byte-after (+ p 38))))
665211a3
KH
1828 ?\222 0)))
1829 (t nil)))
1830 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1831 (fiddle (and archive-zip-case-fiddle
380683ed
EZ
1832 (not (not (memq creator '(0 2 4 5 9))))
1833 (string= (upcase efnname) efnname)))
665211a3 1834 (ifnname (if fiddle (downcase efnname) efnname))
eb93d233 1835 (width (string-width ifnname))
a12aece3 1836 (text (format " %10s %8.0f %-11s %-8s %s"
665211a3
KH
1837 modestr
1838 ucsize
1839 (archive-dosdate moddate)
1840 (archive-dostime modtime)
1841 ifnname)))
eb93d233 1842 (setq maxlen (max maxlen width)
665211a3
KH
1843 totalsize (+ totalsize ucsize)
1844 visual (cons (vector text
1845 (- (length text) (length ifnname))
1846 (length text))
1847 visual)
1848 files (cons (if isdir
1849 nil
1850 (vector efnname ifnname fiddle mode
1851 (list (1- p) lheader)))
1852 files)
845720b9 1853 p (+ p 46 fnlen exlen fclen))))
665211a3
KH
1854 (goto-char (point-min))
1855 (let ((dash (concat "- ---------- -------- ----------- -------- "
1856 (make-string maxlen ?-)
1857 "\n")))
1858 (insert "M Filemode Length Date Time File\n"
1859 dash)
1860 (archive-summarize-files (nreverse visual))
1861 (insert dash
a12aece3 1862 (format " %8.0f %d file%s"
665211a3
KH
1863 totalsize
1864 (length files)
1865 (if (= 1 (length files)) "" "s"))
1866 "\n"))
1867 (apply 'vector (nreverse files))))
1868
1869(defun archive-zip-extract (archive name)
b3671a51
JL
1870 (cond
1871 ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
1872 (archive-*-extract archive name archive-zip-extract))
1873 ((equal (car archive-zip-extract) "7z")
1874 (let ((archive-7z-extract archive-zip-extract))
1875 (archive-7z-extract archive name)))
1876 (t
6ba973c1
JL
1877 (archive-extract-by-stdout
1878 archive
aca54191
EZ
1879 ;; unzip expands wildcards in NAME, so we need to quote it. But
1880 ;; not on DOS/Windows, since that fails extraction on those
72a44673
EZ
1881 ;; systems (unless w32-quote-process-args is nil), and file names
1882 ;; with wildcards in zip archives don't work there anyway.
6ba973c1 1883 ;; FIXME: Does pkunzip need similar treatment?
72a44673
EZ
1884 (if (and (or (not (memq system-type '(windows-nt ms-dos)))
1885 (and (boundp 'w32-quote-process-args)
1886 (null w32-quote-process-args)))
aca54191 1887 (equal (car archive-zip-extract) "unzip"))
6ba973c1
JL
1888 (shell-quote-argument name)
1889 name)
b3671a51 1890 archive-zip-extract))))
665211a3
KH
1891
1892(defun archive-zip-write-file-member (archive descr)
1893 (archive-*-write-file-member
1894 archive
1895 descr
1896 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1897
1898(defun archive-zip-chmod-entry (newmode files)
1899 (save-restriction
1900 (save-excursion
1901 (widen)
e545bb99
SM
1902 (dolist (fil files)
1903 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
ad38511a 1904 (creator (byte-after (+ p 5)))
665211a3
KH
1905 (oldmode (aref fil 3))
1906 (newval (archive-calc-mode oldmode newmode t))
e545bb99 1907 (inhibit-read-only t))
7c2fb837 1908 (cond ((memq creator '(2 3)) ; Unix
665211a3
KH
1909 (goto-char (+ p 40))
1910 (delete-char 2)
ad38511a 1911 (insert-unibyte (logand newval 255) (lsh newval -8)))
380683ed 1912 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
665211a3 1913 (goto-char (+ p 38))
ad38511a
KH
1914 (insert-unibyte (logior (logand (byte-after (point)) 254)
1915 (logand (logxor 1 (lsh newval -7)) 1)))
665211a3
KH
1916 (delete-char 1))
1917 (t (message "Don't know how to change mode for this member"))))
e545bb99 1918 ))))
665211a3 1919;; -------------------------------------------------------------------------
7e9a3fef 1920;;; Section: Zoo Archives
665211a3
KH
1921
1922(defun archive-zoo-summarize ()
1923 (let ((p (1+ (archive-l-e 25 4)))
1924 (maxlen 8)
1925 (totalsize 0)
1926 files
1927 visual)
1928 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1929 (> (archive-l-e (+ p 6) 4) 0))
1930 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1931 (moddate (archive-l-e (+ p 14) 2))
1932 (modtime (archive-l-e (+ p 16) 2))
a12aece3
EZ
1933 ;; Convert to float to avoid overflow for very large files.
1934 (ucsize (archive-l-e (+ p 20) 4 'float))
665211a3 1935 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
ad38511a
KH
1936 (dirtype (byte-after (+ p 4)))
1937 (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
1938 (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
9913653a 1939 (fnlen (or (string-match "\0" namefld) 13))
eb93d233
EZ
1940 (efnname (let ((str
1941 (concat
1942 (if (> ldirlen 0)
1943 (concat (buffer-substring
1944 (+ p 58 lfnlen)
1945 (+ p 58 lfnlen ldirlen -1))
1946 "/")
1947 "")
1948 (if (> lfnlen 0)
1949 (buffer-substring (+ p 58)
1950 (+ p 58 lfnlen -1))
1951 (substring namefld 0 fnlen)))))
ad38511a
KH
1952 (decode-coding-string
1953 str archive-file-name-coding-system)))
83c4abcb 1954 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
665211a3 1955 (ifnname (if fiddle (downcase efnname) efnname))
eb93d233 1956 (width (string-width ifnname))
a12aece3 1957 (text (format " %8.0f %-11s %-8s %s"
665211a3
KH
1958 ucsize
1959 (archive-dosdate moddate)
1960 (archive-dostime modtime)
1961 ifnname)))
0233a189 1962 (setq maxlen (max maxlen width)
665211a3
KH
1963 totalsize (+ totalsize ucsize)
1964 visual (cons (vector text
1965 (- (length text) (length ifnname))
1966 (length text))
1967 visual)
1968 files (cons (vector efnname ifnname fiddle nil (1- p))
1969 files)
1970 p next)))
1971 (goto-char (point-min))
1972 (let ((dash (concat "- -------- ----------- -------- "
1973 (make-string maxlen ?-)
1974 "\n")))
1975 (insert "M Length Date Time File\n"
1976 dash)
1977 (archive-summarize-files (nreverse visual))
1978 (insert dash
a12aece3 1979 (format " %8.0f %d file%s"
665211a3
KH
1980 totalsize
1981 (length files)
1982 (if (= 1 (length files)) "" "s"))
1983 "\n"))
1984 (apply 'vector (nreverse files))))
1985
1986(defun archive-zoo-extract (archive name)
1987 (archive-extract-by-stdout archive name archive-zoo-extract))
7e9a3fef
SM
1988
1989;; -------------------------------------------------------------------------
1990;;; Section: Rar Archives
1991
c9db111a
SM
1992(defun archive-rar-summarize (&optional file)
1993 ;; File is used internally for `archive-rar-exe-summarize'.
1994 (unless file (setq file buffer-file-name))
1995 (let* ((copy (file-local-copy file))
7e9a3fef
SM
1996 (maxname 10)
1997 (maxsize 5)
1998 (files ()))
1999 (with-temp-buffer
2000 (call-process "unrar-free" nil t nil "--list" (or file copy))
2001 (if copy (delete-file copy))
2002 (goto-char (point-min))
2003 (re-search-forward "^-+\n")
7e9a3fef
SM
2004 (while (looking-at (concat " \\(.*\\)\n" ;Name.
2005 ;; Size ; Packed.
2006 " +\\([0-9]+\\) +[0-9]+"
2007 ;; Ratio ; Date'
2008 " +\\([0-9%]+\\) +\\([-0-9]+\\)"
2009 ;; Time ; Attr.
097d86f9 2010 " +\\([0-9:]+\\) +[^ \n]\\{6,10\\}"
7e9a3fef
SM
2011 ;; CRC; Meth ; Var.
2012 " +[0-9A-F]+ +[^ \n]+ +[0-9.]+\n"))
2013 (goto-char (match-end 0))
2014 (let ((name (match-string 1))
2015 (size (match-string 2)))
2016 (if (> (length name) maxname) (setq maxname (length name)))
2017 (if (> (length size) maxsize) (setq maxsize (length size)))
2018 (push (vector name name nil nil
2019 ;; Size, Ratio.
2020 size (match-string 3)
2021 ;; Date, Time.
2022 (match-string 4) (match-string 5))
c9db111a 2023 files))))
7e9a3fef
SM
2024 (setq files (nreverse files))
2025 (goto-char (point-min))
2026 (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
2027 (sep (format format "--------" "-----" (make-string maxsize ?-)
2028 "-----" ""))
2029 (column (length sep)))
2030 (insert (format format " Date " "Time " "Size " "Ratio" " Filename") "\n")
2031 (insert sep (make-string maxname ?-) "\n")
2032 (archive-summarize-files (mapcar (lambda (desc)
2033 (let ((text
2034 (format format
2035 (aref desc 6)
2036 (aref desc 7)
2037 (aref desc 4)
2038 (aref desc 5)
2039 (aref desc 1))))
2040 (vector text
2041 column
2042 (length text))))
2043 files))
2044 (insert sep (make-string maxname ?-) "\n")
2045 (apply 'vector files))))
2046
2047(defun archive-rar-extract (archive name)
2048 ;; unrar-free seems to have no way to extract to stdout or even to a file.
2049 (if (file-name-absolute-p name)
2050 ;; The code below assumes the name is relative and may do undesirable
2051 ;; things otherwise.
2052 (error "Can't extract files with non-relative names")
53baf48a 2053 (archive-extract-by-file archive name '("unrar-free" "--extract") "All OK")))
7e9a3fef 2054
c9db111a
SM
2055;;; Section: Rar self-extracting .exe archives.
2056
2057(defun archive-rar-exe-summarize ()
2058 (let ((tmpfile (make-temp-file "rarexe")))
2059 (unwind-protect
2060 (progn
2061 (goto-char (point-min))
2062 (re-search-forward "Rar!")
2063 (write-region (match-beginning 0) (point-max) tmpfile)
2064 (archive-rar-summarize tmpfile))
2065 (delete-file tmpfile))))
2066
2067(defun archive-rar-exe-extract (archive name)
2068 (let* ((tmpfile (make-temp-file "rarexe"))
2069 (buf (find-buffer-visiting archive))
2070 (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
2071 (unwind-protect
2072 (progn
2073 (with-current-buffer (or buf tmpbuf)
2074 (save-excursion
2075 (save-restriction
2076 (if buf
2077 ;; point-max unwidened is assumed to be the end of the
2078 ;; summary text and the beginning of the actual file data.
2079 (progn (goto-char (point-max)) (widen))
2080 (insert-file-contents-literally archive)
2081 (goto-char (point-min)))
2082 (re-search-forward "Rar!")
2083 (write-region (match-beginning 0) (point-max) tmpfile))))
2084 (archive-rar-extract tmpfile name))
2085 (if tmpbuf (kill-buffer tmpbuf))
2086 (delete-file tmpfile))))
687422df 2087
b3671a51
JL
2088;; -------------------------------------------------------------------------
2089;;; Section: 7z Archives
c9db111a 2090
b3671a51
JL
2091(defun archive-7z-summarize ()
2092 (let ((maxname 10)
2093 (maxsize 5)
2094 (file buffer-file-name)
2095 (files ()))
2096 (with-temp-buffer
2097 (call-process "7z" nil t nil "l" "-slt" file)
2098 (goto-char (point-min))
ac89b32c
JL
2099 ;; Four dashes start the meta info section that should be skipped.
2100 ;; Archive members start with more than four dashes.
2101 (re-search-forward "^-----+\n")
b3671a51
JL
2102 (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
2103 (goto-char (match-end 0))
2104 (let ((name (match-string 1))
2105 (size (save-excursion
2106 (and (re-search-forward "^Size = \\(.*\\)\n")
2107 (match-string 1))))
2108 (time (save-excursion
2109 (and (re-search-forward "^Modified = \\(.*\\)\n")
2110 (match-string 1)))))
2111 (if (> (length name) maxname) (setq maxname (length name)))
2112 (if (> (length size) maxsize) (setq maxsize (length size)))
2113 (push (vector name name nil nil time nil nil size)
2114 files))))
2115 (setq files (nreverse files))
2116 (goto-char (point-min))
2117 (let* ((format (format " %%%ds %%s %%s" maxsize))
2118 (sep (format format (make-string maxsize ?-) "-------------------" ""))
2119 (column (length sep)))
2120 (insert (format format "Size " "Date Time " " Filename") "\n")
2121 (insert sep (make-string maxname ?-) "\n")
2122 (archive-summarize-files (mapcar (lambda (desc)
2123 (let ((text
2124 (format format
2125 (aref desc 7)
2126 (aref desc 4)
2127 (aref desc 1))))
2128 (vector text
2129 column
2130 (length text))))
2131 files))
2132 (insert sep (make-string maxname ?-) "\n")
2133 (apply 'vector files))))
2134
2135(defun archive-7z-extract (archive name)
53baf48a
JL
2136 ;; 7z doesn't provide a `quiet' option to suppress non-essential
2137 ;; stderr messages. So redirect stderr to a temp file and display it
2138 ;; in the echo area when it contains no message indicating success.
2139 (archive-extract-by-stdout
2140 archive name archive-7z-extract "Everything is Ok"))
b3671a51 2141
ac89b32c
JL
2142(defun archive-7z-write-file-member (archive descr)
2143 (archive-*-write-file-member
2144 archive
2145 descr
2146 archive-7z-update))
2147
b3671a51 2148;; -------------------------------------------------------------------------
239bf18b
SM
2149;;; Section `ar' archives.
2150
2151;; TODO: we currently only handle the basic format of ar archives,
2152;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
2153;; for .deb packages.
2154
2155(autoload 'tar-grind-file-mode "tar-mode")
2156
2157(defconst archive-ar-file-header-re
2158 "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
2159
2160(defun archive-ar-summarize ()
2161 ;; File is used internally for `archive-rar-exe-summarize'.
2162 (let* ((maxname 10)
2163 (maxtime 16)
2164 (maxuser 5)
2165 (maxgroup 5)
2166 (maxmode 8)
2167 (maxsize 5)
2168 (files ()))
2169 (goto-char (point-min))
2170 (search-forward "!<arch>\n")
2171 (while (looking-at archive-ar-file-header-re)
2172 (let ((name (match-string 1))
3835d0d0 2173 extname
239bf18b
SM
2174 ;; Emacs will automatically use float here because those
2175 ;; timestamps don't fit in our ints.
2176 (time (string-to-number (match-string 2)))
2177 (user (match-string 3))
2178 (group (match-string 4))
2179 (mode (string-to-number (match-string 5) 8))
2180 (size (string-to-number (match-string 6))))
2181 ;; Move to the beginning of the data.
2182 (goto-char (match-end 0))
3835d0d0
SM
2183 (setq time
2184 (format-time-string
2185 "%Y-%m-%d %H:%M"
2186 (let ((high (truncate (/ time 65536))))
2187 (list high (truncate (- time (* 65536.0 high)))))))
2188 (setq extname
2189 (cond ((equal name "// ")
2190 (propertize ".<ExtNamesTable>." 'face 'italic))
2191 ((equal name "/ ")
2192 (propertize ".<LookupTable>." 'face 'italic))
2193 ((string-match "/? *\\'" name)
2194 (substring name 0 (match-beginning 0)))))
2195 (setq user (substring user 0 (string-match " +\\'" user)))
2196 (setq group (substring group 0 (string-match " +\\'" group)))
2197 (setq mode (tar-grind-file-mode mode))
2198 ;; Move to the end of the data.
2199 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1))
2200 (setq size (number-to-string size))
2201 (if (> (length name) maxname) (setq maxname (length name)))
2202 (if (> (length time) maxtime) (setq maxtime (length time)))
2203 (if (> (length user) maxuser) (setq maxuser (length user)))
2204 (if (> (length group) maxgroup) (setq maxgroup (length group)))
2205 (if (> (length mode) maxmode) (setq maxmode (length mode)))
2206 (if (> (length size) maxsize) (setq maxsize (length size)))
2207 (push (vector name extname nil mode
2208 time user group size)
2209 files)))
239bf18b
SM
2210 (setq files (nreverse files))
2211 (goto-char (point-min))
2212 (let* ((format (format "%%%ds %%%ds/%%-%ds %%%ds %%%ds %%s"
2213 maxmode maxuser maxgroup maxsize maxtime))
2214 (sep (format format (make-string maxmode ?-)
2215 (make-string maxuser ?-)
2216 (make-string maxgroup ?-)
2217 (make-string maxsize ?-)
2218 (make-string maxtime ?-) ""))
2219 (column (length sep)))
2220 (insert (format format " Mode " "User" "Group" " Size "
2221 " Date " "Filename")
2222 "\n")
2223 (insert sep (make-string maxname ?-) "\n")
2224 (archive-summarize-files (mapcar (lambda (desc)
2225 (let ((text
2226 (format format
2227 (aref desc 3)
2228 (aref desc 5)
2229 (aref desc 6)
2230 (aref desc 7)
2231 (aref desc 4)
2232 (aref desc 1))))
2233 (vector text
2234 column
2235 (length text))))
2236 files))
2237 (insert sep (make-string maxname ?-) "\n")
2238 (apply 'vector files))))
2239
2240(defun archive-ar-extract (archive name)
2241 (let ((destbuf (current-buffer))
2242 (archivebuf (find-file-noselect archive))
2243 (from nil) size)
2244 (with-current-buffer archivebuf
2245 (save-restriction
2246 ;; We may be in archive-mode or not, so either with or without
2247 ;; narrowing and with or without a prepended summary.
3835d0d0
SM
2248 (save-excursion
2249 (widen)
2250 (search-forward "!<arch>\n")
2251 (while (and (not from) (looking-at archive-ar-file-header-re))
2252 (let ((this (match-string 1)))
2253 (setq size (string-to-number (match-string 6)))
2254 (goto-char (match-end 0))
2255 (if (equal name this)
2256 (setq from (point))
2257 ;; Move to the end of the data.
2258 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1)))))
2259 (when from
2260 (set-buffer-multibyte nil)
2261 (with-current-buffer destbuf
2262 ;; Do it within the `widen'.
2263 (insert-buffer-substring archivebuf from (+ from size)))
2264 (set-buffer-multibyte 'to)
2265 ;; Inform the caller that the call succeeded.
2266 t))))))
239bf18b 2267
665211a3 2268;; -------------------------------------------------------------------------
0d0587b9
RS
2269;; This line was a mistake; it is kept now for compatibility.
2270;; rms 15 Oct 98
665211a3
KH
2271(provide 'archive-mode)
2272
0d0587b9
RS
2273(provide 'arc-mode)
2274
1cd7adc6 2275;;; arc-mode.el ends here