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