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