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