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