1 ;;; arc-mode.el --- simple editing of archives
3 ;; Copyright (C) 1995, 1997-1998, 2001-2017 Free Software Foundation,
6 ;; Author: Morten Welinder <terra@gnu.org>
7 ;; Keywords: files archives ms-dos editing major-mode
8 ;; Favorite-brand-of-beer: None, I hate beer.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; NAMING: "arc" is short for "archive" and does not refer specifically
28 ;; to files whose name end in ".arc"
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-DOS myself.
35 ;; -------------------------------------
36 ;; INTERACTION: arc-mode.el should play together with
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.
44 ;; * dos-fns.el: (Part of Emacs 19). You get automatic ^M^J <--> ^J
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.
55 ;; Arc Lzh Zip Zoo Rar 7z
56 ;; --------------------------------------------
57 ;; View listing Intern Intern Intern Intern Y Y
58 ;; Extract member Y Y Y Y Y Y
59 ;; Save changed member Y Y Y Y N Y
60 ;; Add new member N N N N N N
61 ;; Delete member Y Y Y Y N Y
62 ;; Rename member Y Y N N N N
67 ;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
68 ;; on the first released version of this package.
70 ;; This code is partly based on tar-mode.el from Emacs.
71 ;; -------------------------------------
72 ;; ARCHIVE STRUCTURES:
73 ;; (This is mostly for myself.)
75 ;; ARC A series of (header,file). No interactions among members.
77 ;; LZH A series of (header,file). Headers are checksummed. No
78 ;; interaction among members.
79 ;; Headers come in three flavors called level 0, 1 and 2 headers.
80 ;; Level 2 header is free of DOS specific restrictions and most
81 ;; commonly 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
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.
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 ;; -------------------------------------
96 ;; HOOKS: `foo' means one of the supported archive types.
99 ;; archive-foo-mode-hook
100 ;; archive-extract-hook
104 ;; -------------------------------------------------------------------------
105 ;;; Section: Configuration.
107 (defgroup archive nil
108 "Simple editing of archives."
111 (defgroup archive-arc nil
112 "ARC-specific options to archive."
115 (defgroup archive-lzh nil
116 "LZH-specific options to archive."
119 (defgroup archive-zip nil
120 "ZIP-specific options to archive."
123 (defgroup archive-zoo nil
124 "ZOO-specific options to archive."
127 (defcustom archive-tmpdir
128 ;; make-temp-name is safe here because we use this name
129 ;; to create a directory.
131 (expand-file-name (if (eq system-type
'ms-dos
) "ar" "archive.tmp")
132 temporary-file-directory
))
133 "Directory for temporary files made by `arc-mode.el'."
137 (defcustom archive-remote-regexp
"^/[^/:]*[^/:.]:"
138 "Regexp recognizing archive files names that are not local.
139 A non-local file is one whose file name is not proper outside Emacs.
140 A local copy of the archive will be used when updating."
144 (define-obsolete-variable-alias 'archive-extract-hooks
145 'archive-extract-hook
"24.3")
146 (defcustom archive-extract-hook nil
147 "Hook run when an archive member has been extracted."
151 (defcustom archive-visit-single-files nil
152 "If non-nil, opening an archive with a single file visits that file.
153 If nil, visiting such an archive displays the archive summary."
155 :type
'(choice (const :tag
"Visit the single file" t
)
156 (const :tag
"Show the archive summary" nil
))
158 ;; ------------------------------
159 ;; Arc archive configuration
161 ;; We always go via a local file since there seems to be no reliable way
162 ;; to extract to stdout without junk getting added.
163 (defcustom archive-arc-extract
165 "Program and its options to run in order to extract an arc file member.
166 Extraction should happen to the current directory. Archive and member
168 :type
'(list (string :tag
"Program")
169 (repeat :tag
"Options"
171 (string :format
"%v")))
174 (defcustom archive-arc-expunge
176 "Program and its options to run in order to delete arc file members.
177 Archive and member names will be added."
178 :type
'(list (string :tag
"Program")
179 (repeat :tag
"Options"
181 (string :format
"%v")))
184 (defcustom archive-arc-write-file-member
186 "Program and its options to run in order to update an arc file member.
187 Archive and member name will be added."
188 :type
'(list (string :tag
"Program")
189 (repeat :tag
"Options"
191 (string :format
"%v")))
193 ;; ------------------------------
194 ;; Lzh archive configuration
196 (defcustom archive-lzh-extract
198 "Program and its options to run in order to extract an lzh file member.
199 Extraction should happen to standard output. Archive and member name will
201 :type
'(list (string :tag
"Program")
202 (repeat :tag
"Options"
204 (string :format
"%v")))
207 (defcustom archive-lzh-expunge
209 "Program and its options to run in order to delete lzh file members.
210 Archive and member names will be added."
211 :type
'(list (string :tag
"Program")
212 (repeat :tag
"Options"
214 (string :format
"%v")))
217 (defcustom archive-lzh-write-file-member
219 "Program and its options to run in order to update an lzh file member.
220 Archive and member name will be added."
221 :type
'(list (string :tag
"Program")
222 (repeat :tag
"Options"
224 (string :format
"%v")))
226 ;; ------------------------------
227 ;; Zip archive configuration
229 (defvar archive-7z-program
(let ((7z (or (executable-find "7z")
230 (executable-find "7za"))))
232 (file-name-nondirectory 7z
))))
234 (defcustom archive-zip-extract
235 (cond ((executable-find "unzip") '("unzip" "-qq" "-c"))
236 (archive-7z-program `(,archive-7z-program
"x" "-so"))
237 ((executable-find "pkunzip") '("pkunzip" "-e" "-o-"))
238 (t '("unzip" "-qq" "-c")))
239 "Program and its options to run in order to extract a zip file member.
240 Extraction should happen to standard output. Archive and member name will
242 :type
'(list (string :tag
"Program")
243 (repeat :tag
"Options"
245 (string :format
"%v")))
248 ;; For several reasons the latter behavior is not desirable in general.
249 ;; (1) It uses more disk space. (2) Error checking is worse or non-
250 ;; existent. (3) It tends to do funny things with other systems' file
253 (defcustom archive-zip-expunge
254 (cond ((executable-find "zip") '("zip" "-d" "-q"))
255 (archive-7z-program `(,archive-7z-program
"d"))
256 ((executable-find "pkzip") '("pkzip" "-d"))
257 (t '("zip" "-d" "-q")))
258 "Program and its options to run in order to delete zip file members.
259 Archive and member names will be added."
260 :type
'(list (string :tag
"Program")
261 (repeat :tag
"Options"
263 (string :format
"%v")))
266 (defcustom archive-zip-update
267 (cond ((executable-find "zip") '("zip" "-q"))
268 (archive-7z-program `(,archive-7z-program
"u"))
269 ((executable-find "pkzip") '("pkzip" "-u" "-P"))
271 "Program and its options to run in order to update a zip file member.
272 Options should ensure that specified directory will be put into the zip
273 file. Archive and member name will be added."
274 :type
'(list (string :tag
"Program")
275 (repeat :tag
"Options"
277 (string :format
"%v")))
280 (defcustom archive-zip-update-case
281 (cond ((executable-find "zip") '("zip" "-q" "-k"))
282 (archive-7z-program `(,archive-7z-program
"u"))
283 ((executable-find "pkzip") '("pkzip" "-u" "-P"))
284 (t '("zip" "-q" "-k")))
285 "Program and its options to run in order to update a case fiddled zip member.
286 Options should ensure that specified directory will be put into the zip file.
287 Archive and member name will be added."
288 :type
'(list (string :tag
"Program")
289 (repeat :tag
"Options"
291 (string :format
"%v")))
294 (defcustom archive-zip-case-fiddle t
295 "If non-nil then zip file members may be down-cased.
296 This case fiddling will only happen for members created by a system
297 that uses caseless file names."
300 ;; ------------------------------
301 ;; Zoo archive configuration
303 (defcustom archive-zoo-extract
305 "Program and its options to run in order to extract a zoo file member.
306 Extraction should happen to standard output. Archive and member name will
308 :type
'(list (string :tag
"Program")
309 (repeat :tag
"Options"
311 (string :format
"%v")))
314 (defcustom archive-zoo-expunge
316 "Program and its options to run in order to delete zoo file members.
317 Archive and member names will be added."
318 :type
'(list (string :tag
"Program")
319 (repeat :tag
"Options"
321 (string :format
"%v")))
324 (defcustom archive-zoo-write-file-member
326 "Program and its options to run in order to update a zoo file member.
327 Archive and member name will be added."
328 :type
'(list (string :tag
"Program")
329 (repeat :tag
"Options"
331 (string :format
"%v")))
333 ;; ------------------------------
334 ;; 7z archive configuration
336 (defcustom archive-7z-extract
337 `(,(or archive-7z-program
"7z") "x" "-so")
338 "Program and its options to run in order to extract a 7z file member.
339 Extraction should happen to standard output. Archive and member name will
342 :type
'(list (string :tag
"Program")
343 (repeat :tag
"Options"
345 (string :format
"%v")))
348 (defcustom archive-7z-expunge
349 `(,(or archive-7z-program
"7z") "d")
350 "Program and its options to run in order to delete 7z file members.
351 Archive and member names will be added."
353 :type
'(list (string :tag
"Program")
354 (repeat :tag
"Options"
356 (string :format
"%v")))
359 (defcustom archive-7z-update
360 `(,(or archive-7z-program
"7z") "u")
361 "Program and its options to run in order to update a 7z file member.
362 Options should ensure that specified directory will be put into the 7z
363 file. Archive and member name will be added."
365 :type
'(list (string :tag
"Program")
366 (repeat :tag
"Options"
368 (string :format
"%v")))
371 ;; -------------------------------------------------------------------------
372 ;;; Section: Variables
374 (defvar archive-subtype nil
"Symbol describing archive type.")
375 (defvar archive-file-list-start nil
"Position of first contents line.")
376 (defvar archive-file-list-end nil
"Position just after last contents line.")
377 (defvar archive-proper-file-start nil
"Position of real archive's start.")
378 (defvar archive-read-only nil
"Non-nil if the archive is read-only on disk.")
379 (defvar archive-local-name nil
"Name of local copy of remote archive.")
380 (defvar archive-mode-map
381 (let ((map (make-keymap)))
382 (set-keymap-parent map special-mode-map
)
383 (define-key map
" " 'archive-next-line
)
384 (define-key map
"a" 'archive-alternate-display
)
385 ;;(define-key map "c" 'archive-copy)
386 (define-key map
"d" 'archive-flag-deleted
)
387 (define-key map
"\C-d" 'archive-flag-deleted
)
388 (define-key map
"e" 'archive-extract
)
389 (define-key map
"f" 'archive-extract
)
390 (define-key map
"\C-m" 'archive-extract
)
391 (define-key map
"m" 'archive-mark
)
392 (define-key map
"n" 'archive-next-line
)
393 (define-key map
"\C-n" 'archive-next-line
)
394 (define-key map
[down] 'archive-next-line)
395 (define-key map "o" 'archive-extract-other-window)
396 (define-key map "p" 'archive-previous-line)
397 (define-key map "\C-p" 'archive-previous-line)
398 (define-key map [?\S-\ ] 'archive-previous-line)
399 (define-key map [up] 'archive-previous-line)
400 (define-key map "r" 'archive-rename-entry)
401 (define-key map "u" 'archive-unflag)
402 (define-key map "\M-\C-?" 'archive-unmark-all-files)
403 (define-key map "v" 'archive-view)
404 (define-key map "x" 'archive-expunge)
405 (define-key map "\177" 'archive-unflag-backwards)
406 (define-key map "E" 'archive-extract-other-window)
407 (define-key map "M" 'archive-chmod-entry)
408 (define-key map "G" 'archive-chgrp-entry)
409 (define-key map "O" 'archive-chown-entry)
410 ;; Let mouse-1 follow the link.
411 (define-key map [follow-link] 'mouse-face)
413 (if (fboundp 'command-remapping)
415 (define-key map [remap advertised-undo] 'archive-undo)
416 (define-key map [remap undo] 'archive-undo))
417 (substitute-key-definition 'advertised-undo 'archive-undo map global-map)
418 (substitute-key-definition 'undo 'archive-undo map global-map))
421 (if (featurep 'xemacs) 'button2 [mouse-2]) 'archive-extract)
423 (if (featurep 'xemacs)
426 (define-key map [menu-bar immediate]
427 (cons "Immediate" (make-sparse-keymap "Immediate")))
428 (define-key map [menu-bar immediate alternate]
429 '(menu-item "Alternate Display" archive-alternate-display
430 :enable (boundp (archive-name "alternate-display"))
431 :help "Toggle alternate file info display"))
432 (define-key map [menu-bar immediate view]
433 '(menu-item "View This File" archive-view
434 :help "Display file at cursor in View Mode"))
435 (define-key map [menu-bar immediate display]
436 '(menu-item "Display in Other Window" archive-display-other-window
437 :help "Display file at cursor in another window"))
438 (define-key map [menu-bar immediate find-file-other-window]
439 '(menu-item "Find in Other Window" archive-extract-other-window
440 :help "Edit file at cursor in another window"))
441 (define-key map [menu-bar immediate find-file]
442 '(menu-item "Find This File" archive-extract
443 :help "Extract file at cursor and edit it"))
445 (define-key map [menu-bar mark]
446 (cons "Mark" (make-sparse-keymap "Mark")))
447 (define-key map [menu-bar mark unmark-all]
448 '(menu-item "Unmark All" archive-unmark-all-files
449 :help "Unmark all marked files"))
450 (define-key map [menu-bar mark deletion]
451 '(menu-item "Flag" archive-flag-deleted
452 :help "Flag file at cursor for deletion"))
453 (define-key map [menu-bar mark unmark]
454 '(menu-item "Unflag" archive-unflag
455 :help "Unmark file at cursor"))
456 (define-key map [menu-bar mark mark]
457 '(menu-item "Mark" archive-mark
458 :help "Mark file at cursor"))
460 (define-key map [menu-bar operate]
461 (cons "Operate" (make-sparse-keymap "Operate")))
462 (define-key map [menu-bar operate chown]
463 '(menu-item "Change Owner..." archive-chown-entry
464 :enable (fboundp (archive-name "chown-entry"))
465 :help "Change owner of marked files"))
466 (define-key map [menu-bar operate chgrp]
467 '(menu-item "Change Group..." archive-chgrp-entry
468 :enable (fboundp (archive-name "chgrp-entry"))
469 :help "Change group ownership of marked files"))
470 (define-key map [menu-bar operate chmod]
471 '(menu-item "Change Mode..." archive-chmod-entry
472 :enable (fboundp (archive-name "chmod-entry"))
473 :help "Change mode (permissions) of marked files"))
474 (define-key map [menu-bar operate rename]
475 '(menu-item "Rename to..." archive-rename-entry
476 :enable (fboundp (archive-name "rename-entry"))
477 :help "Rename marked files"))
478 ;;(define-key map [menu-bar operate copy]
479 ;; '(menu-item "Copy to..." archive-copy))
480 (define-key map [menu-bar operate expunge]
481 '(menu-item "Expunge Marked Files" archive-expunge
482 :help "Delete all flagged files from archive"))
484 "Local keymap for archive mode listings.")
485 (defvar archive-file-name-indent nil "Column where file names start.")
487 (defvar archive-remote nil "Non-nil if the archive is outside file system.")
488 (make-variable-buffer-local 'archive-remote)
489 (put 'archive-remote 'permanent-local t)
491 (defvar archive-member-coding-system nil "Coding-system of archive member.")
492 (make-variable-buffer-local 'archive-member-coding-system)
494 (defvar archive-alternate-display nil
495 "Non-nil when alternate information is shown.")
496 (make-variable-buffer-local 'archive-alternate-display)
497 (put 'archive-alternate-display 'permanent-local t)
499 (defvar archive-superior-buffer nil "In archive members, points to archive.")
500 (put 'archive-superior-buffer 'permanent-local t)
502 (defvar archive-subfile-mode nil "Non-nil in archive member buffers.")
503 (make-variable-buffer-local 'archive-subfile-mode)
504 (put 'archive-subfile-mode 'permanent-local t)
506 (defvar archive-file-name-coding-system nil)
507 (make-variable-buffer-local 'archive-file-name-coding-system)
508 (put 'archive-file-name-coding-system 'permanent-local t)
510 (defvar archive-files nil
511 "Vector of file descriptors.
512 Each descriptor is a vector of the form
513 [EXT-FILE-NAME INT-FILE-NAME CASE-FIDDLED MODE ...]")
514 (make-variable-buffer-local 'archive-files)
516 ;; -------------------------------------------------------------------------
517 ;;; Section: Support functions.
520 (defsubst byte-after (pos)
521 "Like char-after but an eight-bit char is converted to unibyte."
522 (multibyte-char-to-unibyte (char-after pos)))
523 (defsubst insert-unibyte (&rest args)
524 "Like insert but don't make unibyte string and eight-bit char multibyte."
527 (insert (if (< elt 128) elt (decode-char 'eight-bit elt)))
528 (insert (string-to-multibyte elt)))))
531 (defsubst archive-name (suffix)
532 (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
534 (defun archive-l-e (str &optional len float)
535 "Convert little endian string/vector STR to integer.
536 Alternatively, STR may be a buffer position in the current buffer
537 in which case a second argument, length LEN, should be supplied.
538 FLOAT, if non-nil, means generate and return a float instead of an integer
539 \(use this for numbers that can overflow the Emacs integer)."
541 (setq len (length str))
542 (setq str (buffer-substring str (+ str len))))
543 (setq str (string-as-unibyte str))
548 result (+ (if float (* result 256.0) (ash result 8))
549 (aref str (- len i)))))
552 (defun archive-int-to-mode (mode)
553 "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------."
554 ;; FIXME: merge with tar-grind-file-mode.
556 (if (zerop (logand 8192 mode))
557 (if (zerop (logand 16384 mode)) ?- ?d)
559 (if (zerop (logand 256 mode)) ?- ?r)
560 (if (zerop (logand 128 mode)) ?- ?w)
561 (if (zerop (logand 64 mode))
562 (if (zerop (logand 1024 mode)) ?- ?S)
563 (if (zerop (logand 1024 mode)) ?x ?s))
564 (if (zerop (logand 32 mode)) ?- ?r)
565 (if (zerop (logand 16 mode)) ?- ?w)
566 (if (zerop (logand 8 mode))
567 (if (zerop (logand 2048 mode)) ?- ?S)
568 (if (zerop (logand 2048 mode)) ?x ?s))
569 (if (zerop (logand 4 mode)) ?- ?r)
570 (if (zerop (logand 2 mode)) ?- ?w)
571 (if (zerop (logand 1 mode)) ?- ?x)))
573 (defun archive-calc-mode (oldmode newmode &optional error)
574 "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
575 NEWMODE may be an octal number including a leading zero in which case it
576 will become the new mode.\n
577 NEWMODE may also be a relative specification like \"og-rwx\" in which case
578 OLDMODE will be modified accordingly just like chmod(2) would have done.\n
579 If optional third argument ERROR is non-nil an error will be signaled if
580 the mode is invalid. If ERROR is nil then nil will be returned."
581 (cond ((string-match "^0[0-7]*$" newmode)
583 (len (length newmode))
586 (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
588 (logior (logand oldmode 65024) result)))
589 ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
592 (op (aref newmode (match-beginning 2)))
594 (i (match-beginning 3)))
595 (while (< i (match-end 3))
596 (let ((rwx (aref newmode i)))
597 (setq bits (logior bits (cond ((= rwx ?r) 292)
603 (while (< who (match-end 1))
604 (let* ((whoc (aref newmode who))
605 (whomask (cond ((= whoc ?a) 4095)
610 (setq result (logand result (lognot whomask))))
612 (setq result (logand result (lognot (logand whomask bits))))
613 (setq result (logior result (logand whomask bits)))))
618 (error "Invalid mode specification: %s" newmode)))))
620 (defun archive-dosdate (date)
621 "Stringify dos packed DATE record."
622 (let ((year (+ 1980 (logand (ash date -9) 127)))
623 (month (logand (ash date -5) 15))
624 (day (logand date 31)))
625 (if (or (> month 12) (< month 1))
629 (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
630 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
633 (defun archive-dostime (time)
634 "Stringify dos packed TIME record."
635 (let ((hour (logand (ash time -11) 31))
636 (minute (logand (ash time -5) 63))
637 (second (* 2 (logand time 31)))) ; 2 seconds resolution
638 (format "%02d:%02d:%02d" hour minute second)))
640 (defun archive-unixdate (low high)
641 "Stringify Unix (LOW HIGH) date."
642 (let* ((time (cons high low))
643 (str (current-time-string time)))
647 (format-time-string "%Y" time))))
649 (defun archive-unixtime (low high)
650 "Stringify Unix (LOW HIGH) time."
651 (let ((str (current-time-string (cons high low))))
652 (substring str 11 19)))
654 (defun archive-get-lineno ()
655 (if (>= (point) archive-file-list-start)
656 (count-lines archive-file-list-start
657 (line-beginning-position))
660 (defun archive-get-descr (&optional noerror)
661 "Return the descriptor vector for file at point.
662 Does not signal an error if optional argument NOERROR is non-nil."
663 (let ((no (archive-get-lineno)))
664 (if (and (>= (point) archive-file-list-start)
665 (< no (length archive-files)))
666 (let ((item (aref archive-files no)))
670 (error "Entry is not a regular member of the archive"))))
672 (error "Line does not describe a member of the archive")))))
673 ;; -------------------------------------------------------------------------
674 ;;; Section: the mode definition
677 (defun archive-mode (&optional force)
678 "Major mode for viewing an archive file in a dired-like way.
679 You can move around using the usual cursor motion commands.
680 Letters no longer insert themselves.
681 Type `e' to pull a file out of the archive and into its own buffer;
682 or click mouse-2 on the file's line in the archive mode buffer.
684 If you edit a sub-file of this archive (as with the `e' command) and
685 save it, the contents of that buffer will be saved back into the
688 \\{archive-mode-map}"
689 ;; This is not interactive because you shouldn't be turning this
690 ;; mode on and off. You can corrupt things that way.
691 (if (zerop (buffer-size))
692 ;; At present we cannot create archives from scratch
693 (funcall (or (default-value 'major-mode) 'fundamental-mode))
694 (if (and (not force) archive-files) nil
695 (kill-all-local-variables)
696 (let* ((type (archive-find-type))
697 (typename (capitalize (symbol-name type))))
698 (make-local-variable 'archive-subtype)
699 (setq archive-subtype type)
701 ;; Buffer contains treated image of file before the file contents
702 (make-local-variable 'revert-buffer-function)
703 (setq revert-buffer-function 'archive-mode-revert)
706 (add-hook 'write-contents-functions 'archive-write-file nil t)
708 (make-local-variable 'require-final-newline)
709 (setq require-final-newline nil)
710 (make-local-variable 'local-enable-local-variables)
711 (setq local-enable-local-variables nil)
713 ;; Prevent loss of data when saving the file.
714 (make-local-variable 'file-precious-flag)
715 (setq file-precious-flag t)
717 (make-local-variable 'archive-read-only)
718 ;; Archives which are inside other archives and whose
719 ;; names are invalid for this OS, can't be written.
720 (setq archive-read-only
721 (or (not (file-writable-p (buffer-file-name)))
722 (and archive-subfile-mode
723 (string-match file-name-invalid-regexp
724 (aref archive-subfile-mode 0)))))
726 ;; Should we use a local copy when accessing from outside Emacs?
727 (make-local-variable 'archive-local-name)
729 ;; An archive can contain another archive whose name is invalid
730 ;; on local filesystem. Treat such archives as remote.
733 (or (string-match archive-remote-regexp (buffer-file-name))
734 (string-match file-name-invalid-regexp
735 (buffer-file-name)))))
737 (setq major-mode 'archive-mode)
738 (setq mode-name (concat typename "-Archive"))
739 ;; Run archive-foo-mode-hook and archive-mode-hook
740 (run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
741 (use-local-map archive-mode-map))
743 (make-local-variable 'archive-proper-file-start)
744 (make-local-variable 'archive-file-list-start)
745 (make-local-variable 'archive-file-list-end)
746 (make-local-variable 'archive-file-name-indent)
747 (setq archive-file-name-coding-system
748 (or file-name-coding-system
749 default-file-name-coding-system
750 locale-coding-system))
751 (if (default-value 'enable-multibyte-characters)
752 (set-buffer-multibyte 'to))
753 (archive-summarize nil)
754 (setq buffer-read-only t)
755 (when (and archive-visit-single-files
756 auto-compression-mode
757 (= (length archive-files) 1))
758 (rename-buffer (concat " " (buffer-name)))
759 (archive-extract)))))
761 ;; Archive mode is suitable only for specially formatted data.
762 (put 'archive-mode 'mode-class 'special)
764 (let ((item1 '(archive-subfile-mode " Archive")))
765 (or (member item1 minor-mode-alist)
766 (setq minor-mode-alist (cons item1 minor-mode-alist))))
767 ;; -------------------------------------------------------------------------
768 (defun archive-find-type ()
770 (goto-char (point-min))
771 ;; The funny [] here make it unlikely that the .elc file will be treated
772 ;; as an archive by other software.
773 (let (case-fold-search)
774 (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
775 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
776 ((looking-at "....................[\334]\247\304\375") 'zoo)
777 ((and (looking-at "\C-z") ; signature too simple, IMHO
778 (string-match "\\.[aA][rR][cC]\\'"
779 (or buffer-file-name (buffer-name))))
781 ;; This pattern modeled on the BSD/GNU+Linux `file' command.
782 ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
783 ;; Note this regexp is also in archive-exe-p.
784 ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
785 ((looking-at "Rar!") 'rar)
786 ((looking-at "!<arch>\n") 'ar)
787 ((and (looking-at "MZ")
788 (re-search-forward "Rar!" (+ (point) 100000) t))
790 ((looking-at "7z\274\257\047\034") '7z)
791 (t (error "Buffer format not recognized")))))
792 ;; -------------------------------------------------------------------------
794 (defun archive-desummarize ()
795 (let ((inhibit-read-only t)
796 (modified (buffer-modified-p)))
798 (delete-region (point-min) archive-proper-file-start)
799 (restore-buffer-modified-p modified)))
802 (defun archive-summarize (&optional shut-up)
803 "Parse the contents of the archive file in the current buffer.
804 Place a dired-like listing on the front;
805 then narrow to it, so that only that listing
806 is visible (and the real data of the buffer is hidden).
807 Optional argument SHUT-UP, if non-nil, means don't print messages
808 when parsing the archive."
810 (let ((buffer-file-truename nil) ; avoid changing dir mtime by lock_file
811 (inhibit-read-only t))
812 (setq archive-proper-file-start (copy-marker (point-min) t))
813 (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
815 (message "Parsing archive file..."))
816 (buffer-disable-undo (current-buffer))
817 (setq archive-files (funcall (archive-name "summarize")))
819 (message "Parsing archive file...done."))
820 (setq archive-proper-file-start (point-marker))
821 (narrow-to-region (point-min) (point))
822 (set-buffer-modified-p nil)
823 (buffer-enable-undo))
824 (goto-char archive-file-list-start)
825 (archive-next-line 0))
827 (defun archive-resummarize ()
828 "Recreate the contents listing of an archive."
829 (let ((no (archive-get-lineno)))
830 (archive-desummarize)
831 (archive-summarize t)
832 (goto-char archive-file-list-start)
833 (archive-next-line no)))
835 (defun archive-summarize-files (files)
836 "Insert a description of a list of files annotated with proper mouse face."
837 (setq archive-file-list-start (point-marker))
838 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
839 ;; We don't want to do an insert for each element since that takes too
840 ;; long when the archive -- which has to be moved in memory -- is large.
846 ;; Using `concat' here copies the text also, so we can add
847 ;; properties without problems.
848 (let ((text (concat (aref fil 0) "\n")))
849 (if (featurep 'xemacs)
852 (aref fil 1) (aref fil 2)
853 '(mouse-face highlight
854 help-echo "mouse-2: extract this file into a buffer")
858 (setq archive-file-list-end (point-marker)))
860 (defun archive-alternate-display ()
861 "Toggle alternative display.
862 To avoid very long lines archive mode does not show all information.
863 This function changes the set of information shown for each files."
865 (setq archive-alternate-display (not archive-alternate-display))
866 (archive-resummarize))
867 ;; -------------------------------------------------------------------------
868 ;;; Section: Local archive copy handling
870 (defun archive-unique-fname (fname dir)
871 "Make sure a file FNAME can be created uniquely in directory DIR.
873 If FNAME can be uniquely created in DIR, it is returned unaltered.
874 If FNAME is something our underlying filesystem can't grok, or if another
875 file by that name already exists in DIR, a unique new name is generated
876 using `make-temp-file', and the generated name is returned."
877 (let ((fullname (expand-file-name fname dir))
878 (alien (string-match file-name-invalid-regexp fname))
881 (if (if (fboundp 'msdos-long-file-names)
882 (not (msdos-long-file-names)))
886 (if (or alien (file-exists-p fullname))
888 ;; Make sure all the leading directories in
889 ;; archive-local-name exist under archive-tmpdir, so that
890 ;; the directory structure recorded in the archive is
891 ;; reconstructed in the temporary directory.
892 (make-directory (file-name-directory tmpfile) t)
893 (make-temp-file tmpfile))
894 ;; Make sure all the leading directories in `fullname' exist
895 ;; under archive-tmpdir. This is necessary for nested archives
896 ;; (`archive-extract' sets `archive-remote' to t in case
897 ;; an archive occurs inside another archive).
898 (make-directory (file-name-directory fullname) t)
901 (defun archive-maybe-copy (archive)
902 (let ((coding-system-for-write 'no-conversion))
904 (let ((start (point-max))
905 ;; Sometimes ARCHIVE is invalid while its actual name, as
906 ;; recorded in its parent archive, is not. For example, an
907 ;; archive bar.zip inside another archive foo.zip gets a name
908 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
909 ;; So use the actual name if available.
911 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
913 (setq archive-local-name
914 (archive-unique-fname archive-name archive-tmpdir))
917 (write-region start (point-max) archive-local-name nil 'nomessage))
919 (if (buffer-modified-p) (save-buffer))
922 (defun archive-maybe-update (unchanged)
924 (let ((name archive-local-name)
925 (modified (buffer-modified-p))
926 (coding-system-for-read 'no-conversion)
927 (lno (archive-get-lineno))
928 (inhibit-read-only t))
930 (setq archive-files nil)
932 (insert-file-contents name)
934 (goto-char archive-file-list-start)
935 (archive-next-line lno))
936 (archive-delete-local name)
939 "Buffer `%s' must be saved for changes to take effect"
940 (buffer-name (current-buffer))))
941 (set-buffer-modified-p (or modified (not unchanged))))))
943 (defun archive-delete-local (name)
944 "Delete file NAME and its parents up to and including `archive-tmpdir'."
946 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
951 (setq name (directory-file-name (file-name-directory name)))
953 (delete-directory name)
955 (if (string= name top) (setq again nil)))))
956 ;; -------------------------------------------------------------------------
957 ;;; Section: Member extraction
959 (defun archive-try-jka-compr ()
960 (when (and auto-compression-mode
961 (jka-compr-get-compression-info buffer-file-name))
962 (let* ((basename (file-name-nondirectory buffer-file-name))
963 (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
964 (match-string 1 basename) basename))
965 (tmpfile (make-temp-file (file-name-sans-extension tmpname)
967 (file-name-extension tmpname 'period))))
970 (let ((coding-system-for-write 'no-conversion)
971 ;; Don't re-compress this data just before decompressing it.
972 (jka-compr-inhibit t))
973 (write-region (point-min) (point-max) tmpfile nil 'quiet))
975 (let ((coding-system-for-read 'no-conversion))
976 (insert-file-contents tmpfile)))
977 (delete-file tmpfile)))))
979 (defun archive-file-name-handler (op &rest args)
980 (or (eq op 'file-exists-p)
981 (let ((file-name-handler-alist nil))
984 (defun archive-set-buffer-as-visiting-file (filename)
985 "Set the current buffer as if it were visiting FILENAME."
987 (goto-char (point-min))
988 (let ((buffer-undo-list t)
990 (or coding-system-for-read
991 (and set-auto-coding-function
993 (funcall set-auto-coding-function
994 filename (- (point-max) (point-min)))))
995 ;; The following let-binding of file-name-handler-alist forces
996 ;; find-file-not-found-set-buffer-file-coding-system to ignore
997 ;; the file's name (see dos-w32.el).
998 (let ((file-name-handler-alist
999 '(("" . archive-file-name-handler))))
1000 (car (find-operation-coding-system
1001 'insert-file-contents
1002 (cons filename (current-buffer)) t))))))
1003 (unless (or coding-system-for-read
1004 enable-multibyte-characters)
1006 (coding-system-change-text-conversion coding 'raw-text)))
1007 (unless (memq coding '(nil no-conversion))
1008 (decode-coding-region (point-min) (point-max) coding)
1009 (setq last-coding-system-used coding))
1010 (set-buffer-modified-p nil)
1011 (kill-local-variable 'buffer-file-coding-system)
1012 (after-insert-file-set-coding (- (point-max) (point-min))))))
1014 (define-obsolete-function-alias 'archive-mouse-extract 'archive-extract "22.1")
1016 (defun archive-extract (&optional other-window-p event)
1017 "In archive mode, extract this entry of the archive into its own buffer."
1018 (interactive (list nil last-input-event))
1019 (if event (posn-set-point (event-end event)))
1020 (let* ((view-p (eq other-window-p 'view))
1021 (descr (archive-get-descr))
1022 (ename (aref descr 0))
1023 (iname (aref descr 1))
1024 (archive-buffer (current-buffer))
1025 (arcdir default-directory)
1026 (archive (buffer-file-name))
1027 (arcname (file-name-nondirectory archive))
1028 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
1029 (extractor (archive-name "extract"))
1030 ;; Members with file names which aren't valid for the
1031 ;; underlying filesystem, are treated as read-only.
1032 (read-only-p (or archive-read-only
1034 (string-match file-name-invalid-regexp ename)))
1035 (arcfilename (expand-file-name (concat arcname ":" iname)))
1036 (buffer (get-buffer bufname))
1038 (file-name-coding archive-file-name-coding-system))
1040 (string= (buffer-file-name buffer) arcfilename))
1042 (setq archive (archive-maybe-copy archive))
1043 (setq bufname (generate-new-buffer-name bufname))
1044 (setq buffer (get-buffer-create bufname))
1045 (setq just-created t)
1046 (with-current-buffer buffer
1047 (setq buffer-file-name arcfilename)
1048 (setq buffer-file-truename
1049 (abbreviate-file-name buffer-file-name))
1050 ;; Set the default-directory to the dir of the superior buffer.
1051 (setq default-directory arcdir)
1052 (make-local-variable 'archive-superior-buffer)
1053 (setq archive-superior-buffer archive-buffer)
1054 (add-hook 'write-file-functions #'archive-write-file-member nil t)
1055 (setq archive-subfile-mode descr)
1056 (setq archive-file-name-coding-system file-name-coding)
1059 (let (;; We may have to encode the file name argument for
1060 ;; external programs.
1061 (coding-system-for-write
1062 (and enable-multibyte-characters
1063 archive-file-name-coding-system))
1064 ;; We read an archive member by no-conversion at
1065 ;; first, then decode appropriately by calling
1066 ;; archive-set-buffer-as-visiting-file later.
1067 (coding-system-for-read 'no-conversion))
1069 (if (fboundp extractor)
1070 (funcall extractor archive ename)
1071 (archive-*-extract archive ename
1072 (symbol-value extractor)))
1074 (ding (message "%s" (error-message-string err)))
1078 (set-buffer-modified-p nil)
1079 (kill-buffer buffer))
1080 (archive-try-jka-compr) ;Pretty ugly hack :-(
1081 (archive-set-buffer-as-visiting-file ename)
1082 (goto-char (point-min))
1083 (rename-buffer bufname)
1084 (setq buffer-read-only read-only-p)
1085 (setq buffer-undo-list nil)
1086 (set-buffer-modified-p nil)
1087 (setq buffer-saved-size (buffer-size))
1089 ;; Just in case an archive occurs inside another archive.
1090 (when (derived-mode-p 'archive-mode)
1091 (setq archive-remote t)
1092 (if read-only-p (setq archive-read-only t))
1093 ;; We will write out the archive ourselves if it is
1094 ;; part of another archive.
1095 (remove-hook 'write-contents-functions #'archive-write-file t))
1096 (run-hooks 'archive-extract-hook)
1097 (if archive-read-only
1098 (message "Note: altering this archive is not implemented."))))
1099 (archive-maybe-update t))
1100 (or (not (buffer-name buffer))
1103 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
1104 ((eq other-window-p 'display) (display-buffer buffer))
1105 (other-window-p (switch-to-buffer-other-window buffer))
1106 (t (switch-to-buffer buffer))))))
1108 (defun archive-*-extract (archive name command)
1109 (let* ((default-directory (file-name-as-directory archive-tmpdir))
1110 (tmpfile (expand-file-name (file-name-nondirectory name)
1112 exit-status success)
1113 (make-directory (directory-file-name default-directory) t)
1115 (apply #'call-process
1120 (append (cdr command) (list archive name))))
1121 (cond ((and (numberp exit-status) (zerop exit-status))
1122 (if (not (file-exists-p tmpfile))
1123 (ding (message "`%s': no such file or directory" tmpfile))
1124 (insert-file-contents tmpfile)
1126 ((numberp exit-status)
1128 (message "`%s' exited with status %d" (car command) exit-status)))
1129 ((stringp exit-status)
1130 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1132 (ding (message "`%s' failed" (car command)))))
1133 (archive-delete-local tmpfile)
1136 (defun archive-extract-by-stdout (archive name command &optional stderr-test)
1137 (let ((stderr-file (make-temp-file "arc-stderr")))
1140 (apply #'call-process
1143 (if stderr-file (list t stderr-file) t)
1145 (append (cdr command) (list archive name)))
1147 (insert-file-contents stderr-file)
1148 (goto-char (point-min))
1149 (when (if (stringp stderr-test)
1150 (not (re-search-forward stderr-test nil t))
1151 (> (buffer-size) 0))
1152 (message "%s" (buffer-string)))))
1153 (if (file-exists-p stderr-file)
1154 (delete-file stderr-file)))))
1156 (defun archive-extract-by-file (archive name command &optional stdout-test)
1157 (let ((dest (make-temp-file "arc-dir" 'dir))
1158 (stdout-file (make-temp-file "arc-stdout")))
1161 (apply #'call-process
1164 `(:file ,stdout-file)
1166 `(,archive ,name ,@(cdr command) ,dest))
1168 (insert-file-contents stdout-file)
1169 (goto-char (point-min))
1170 (when (if (stringp stdout-test)
1171 (not (re-search-forward stdout-test nil t))
1172 (> (buffer-size) 0))
1173 (message "%s" (buffer-string))))
1174 (if (file-exists-p (expand-file-name name dest))
1175 (insert-file-contents-literally (expand-file-name name dest))))
1176 (if (file-exists-p stdout-file)
1177 (delete-file stdout-file))
1178 (if (file-exists-p (expand-file-name name dest))
1179 (delete-file (expand-file-name name dest)))
1180 (while (file-name-directory name)
1181 (setq name (directory-file-name (file-name-directory name)))
1182 (when (file-directory-p (expand-file-name name dest))
1183 (delete-directory (expand-file-name name dest))))
1184 (when (file-directory-p dest)
1185 (delete-directory dest)))))
1187 (defun archive-extract-other-window ()
1188 "In archive mode, find this member in another window."
1190 (archive-extract t))
1192 (defun archive-display-other-window ()
1193 "In archive mode, display this member in another window."
1195 (archive-extract 'display))
1197 (defun archive-view ()
1198 "In archive mode, view the member on this line."
1200 (archive-extract 'view))
1202 (defun archive-add-new-member (arcbuf name)
1203 "Add current buffer to the archive in ARCBUF naming it NAME."
1206 (read-buffer "Buffer containing archive: "
1207 ;; Find first archive buffer and suggest that
1208 (let ((bufs (buffer-list)))
1210 (not (with-current-buffer (car bufs)
1211 (derived-mode-p 'archive-mode))))
1212 (setq bufs (cdr bufs)))
1215 (error "There are no archive buffers")))
1217 (read-string "File name in archive: "
1218 (if buffer-file-name
1219 (file-name-nondirectory buffer-file-name)
1221 (with-current-buffer arcbuf
1222 (or (derived-mode-p 'archive-mode)
1223 (error "Buffer is not an archive buffer"))
1224 (if archive-read-only
1225 (error "Archive is read-only")))
1226 (if (eq arcbuf (current-buffer))
1227 (error "An archive buffer cannot be added to itself"))
1228 (if (string= name "")
1229 (error "Archive members may not be given empty names"))
1230 (let ((func (with-current-buffer arcbuf
1231 (archive-name "add-new-member")))
1232 (membuf (current-buffer)))
1234 (with-current-buffer arcbuf
1235 (funcall func buffer-file-name membuf name))
1236 (error "Adding a new member is not supported for this archive type"))))
1237 ;; -------------------------------------------------------------------------
1238 ;;; Section: IO stuff
1240 (defun archive-write-file-member ()
1243 (message "Updating archive...")
1245 (let ((writer (with-current-buffer archive-superior-buffer
1246 (archive-name "write-file-member")))
1247 (archive (with-current-buffer archive-superior-buffer
1248 (archive-maybe-copy (buffer-file-name)))))
1249 (if (fboundp writer)
1250 (funcall writer archive archive-subfile-mode)
1251 (archive-*-write-file-member archive
1252 archive-subfile-mode
1253 (symbol-value writer)))
1254 (set-buffer-modified-p nil)
1255 (message "Updating archive...done"))
1256 (set-buffer archive-superior-buffer)
1257 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1258 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1259 ;; won't reset the coding-system of this archive member.
1260 (if (local-variable-p 'archive-member-coding-system)
1261 (setq last-coding-system-used archive-member-coding-system))
1264 (defun archive-*-write-file-member (archive descr command)
1265 (let* ((ename (aref descr 0))
1266 (tmpfile (expand-file-name ename archive-tmpdir))
1267 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1268 (default-directory (file-name-as-directory top)))
1271 (make-directory (file-name-directory tmpfile) t)
1272 ;; If the member is itself an archive, write it without
1273 ;; the dired-like listing we created.
1274 (if (eq major-mode 'archive-mode)
1275 (archive-write-file tmpfile)
1276 (write-region nil nil tmpfile nil 'nomessage))
1277 ;; basic-save-buffer needs last-coding-system-used to have
1278 ;; the value used to write the file, so save it before any
1279 ;; further processing clobbers it (we restore it in
1280 ;; archive-write-file-member, above).
1281 (setq archive-member-coding-system last-coding-system-used)
1283 ;; Set the file modes, but make sure we can read it.
1284 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1286 (encode-coding-string ename archive-file-name-coding-system))
1287 (let* ((coding-system-for-write 'no-conversion)
1288 (exitcode (apply #'call-process
1293 (append (cdr command)
1294 (list archive ename)))))
1295 (or (zerop exitcode)
1296 (error "Updating was unsuccessful (%S)" exitcode))))
1297 (archive-delete-local tmpfile))))
1299 (defun archive-write-file (&optional file)
1301 (let ((coding-system-for-write 'no-conversion))
1302 (write-region archive-proper-file-start (point-max)
1303 (or file buffer-file-name) nil t)
1304 (set-buffer-modified-p nil))
1306 ;; -------------------------------------------------------------------------
1307 ;;; Section: Marking and unmarking.
1309 (defun archive-flag-deleted (p &optional type)
1310 "In archive mode, mark this member to be deleted from the archive.
1311 With a prefix argument, mark that many files."
1313 (or type (setq type ?D))
1315 (let ((sign (if (>= p 0) +1 -1))
1316 (modified (buffer-modified-p))
1317 (inhibit-read-only t))
1318 (while (not (zerop p))
1319 (if (archive-get-descr t)
1324 (setq p (- p sign)))
1325 (restore-buffer-modified-p modified))
1326 (archive-next-line 0))
1328 (defun archive-unflag (p)
1329 "In archive mode, un-mark this member if it is marked to be deleted.
1330 With a prefix argument, un-mark that many files forward."
1332 (archive-flag-deleted p ?\s))
1334 (defun archive-unflag-backwards (p)
1335 "In archive mode, un-mark this member if it is marked to be deleted.
1336 With a prefix argument, un-mark that many members backward."
1338 (archive-flag-deleted (- p) ?\s))
1340 (defun archive-unmark-all-files ()
1343 (let ((modified (buffer-modified-p))
1344 (inhibit-read-only t))
1346 (goto-char archive-file-list-start)
1347 (while (< (point) archive-file-list-end)
1348 (or (= (following-char) ?\s)
1349 (progn (delete-char 1) (insert ?\s)))
1351 (restore-buffer-modified-p modified)))
1353 (defun archive-mark (p)
1354 "In archive mode, mark this member for group operations.
1355 With a prefix argument, mark that many members.
1356 Use \\[archive-unmark-all-files] to remove all marks."
1358 (archive-flag-deleted p ?*))
1360 (defun archive-get-marked (mark &optional default)
1363 (goto-char archive-file-list-start)
1364 (while (< (point) archive-file-list-end)
1365 (if (= (following-char) mark)
1366 (setq files (cons (archive-get-descr) files)))
1368 (or (nreverse files)
1370 (list (archive-get-descr))))))
1371 ;; -------------------------------------------------------------------------
1372 ;;; Section: Operate
1374 (defun archive-next-line (p)
1378 (forward-char archive-file-name-indent)))
1380 (defun archive-previous-line (p)
1382 (archive-next-line (- p)))
1384 (defun archive-chmod-entry (new-mode)
1385 "Change the protection bits associated with all marked or this member.
1386 The new protection bits can either be specified as an octal number or
1387 as a relative change like \"g+rw\" as for chmod(2)."
1388 (interactive "sNew mode (octal or relative): ")
1389 (if archive-read-only (error "Archive is read-only"))
1390 (let ((func (archive-name "chmod-entry")))
1393 (funcall func new-mode (archive-get-marked ?* t))
1394 (archive-resummarize))
1395 (error "Setting mode bits is not supported for this archive type"))))
1397 (defun archive-chown-entry (new-uid)
1398 "Change the owner of all marked or this member."
1399 (interactive "nNew uid: ")
1400 (if archive-read-only (error "Archive is read-only"))
1401 (let ((func (archive-name "chown-entry")))
1404 (funcall func new-uid (archive-get-marked ?* t))
1405 (archive-resummarize))
1406 (error "Setting owner is not supported for this archive type"))))
1408 (defun archive-chgrp-entry (new-gid)
1409 "Change the group of all marked or this member."
1410 (interactive "nNew gid: ")
1411 (if archive-read-only (error "Archive is read-only"))
1412 (let ((func (archive-name "chgrp-entry")))
1415 (funcall func new-gid (archive-get-marked ?* t))
1416 (archive-resummarize))
1417 (error "Setting group is not supported for this archive type"))))
1419 (defun archive-expunge ()
1420 "Do the flagged deletions."
1424 (goto-char archive-file-list-start)
1425 (while (< (point) archive-file-list-end)
1426 (if (= (following-char) ?D)
1427 (setq files (cons (aref (archive-get-descr) 0) files)))
1429 (setq files (nreverse files))
1431 (or (not archive-read-only)
1432 (error "Archive is read-only"))
1433 (or (yes-or-no-p (format "Really delete %d member%s? "
1435 (if (null (cdr files)) "" "s")))
1436 (error "Operation aborted"))
1437 (let ((archive (archive-maybe-copy (buffer-file-name)))
1438 (expunger (archive-name "expunge")))
1439 (if (fboundp expunger)
1440 (funcall expunger archive files)
1441 (archive-*-expunge archive files (symbol-value expunger)))
1442 (archive-maybe-update nil)
1444 (archive-resummarize)
1445 (revert-buffer))))))
1447 (defun archive-*-expunge (archive files command)
1448 (apply #'call-process
1453 (append (cdr command) (cons archive files))))
1455 (defun archive-rename-entry (newname)
1456 "Change the name associated with this entry in the archive file."
1457 (interactive "sNew name: ")
1458 (if archive-read-only (error "Archive is read-only"))
1459 (if (string= newname "")
1460 (error "Archive members may not be given empty names"))
1461 (let ((func (archive-name "rename-entry"))
1462 (descr (archive-get-descr)))
1466 (encode-coding-string newname
1467 archive-file-name-coding-system)
1469 (archive-resummarize))
1470 (error "Renaming is not supported for this archive type"))))
1472 ;; Revert the buffer and recompute the dired-like listing.
1473 (defun archive-mode-revert (&optional _no-auto-save _no-confirm)
1474 (let ((no (archive-get-lineno)))
1475 (setq archive-files nil)
1476 (let ((revert-buffer-function nil)
1477 (coding-system-for-read 'no-conversion))
1478 (revert-buffer t t))
1480 (goto-char archive-file-list-start)
1481 (archive-next-line no)))
1483 (defun archive-undo ()
1484 "Undo in an archive buffer.
1485 This doesn't recover lost files, it just undoes changes in the buffer itself."
1487 (let ((inhibit-read-only t))
1489 ;; -------------------------------------------------------------------------
1490 ;;; Section: Arc Archives
1492 (defun archive-arc-summarize ()
1498 (while (and (< (+ p 29) (point-max))
1499 (= (byte-after p) ?\C-z)
1500 (> (byte-after (1+ p)) 0))
1501 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1502 (fnlen (or (string-match "\0" namefld) 13))
1503 (efnname (decode-coding-string (substring namefld 0 fnlen)
1504 archive-file-name-coding-system))
1505 ;; Convert to float to avoid overflow for very large files.
1506 (csize (archive-l-e (+ p 15) 4 'float))
1507 (moddate (archive-l-e (+ p 19) 2))
1508 (modtime (archive-l-e (+ p 21) 2))
1509 (ucsize (archive-l-e (+ p 25) 4 'float))
1510 (fiddle (string= efnname (upcase efnname)))
1511 (ifnname (if fiddle (downcase efnname) efnname))
1512 (text (format " %8.0f %-11s %-8s %s"
1514 (archive-dosdate moddate)
1515 (archive-dostime modtime)
1517 (setq maxlen (max maxlen fnlen)
1518 totalsize (+ totalsize ucsize)
1519 visual (cons (vector text
1520 (- (length text) (length ifnname))
1523 files (cons (vector efnname ifnname fiddle nil (1- p))
1525 ;; p needs to stay an integer, since we use it in char-after
1526 ;; above. Passing through `round' limits the compressed size
1527 ;; to most-positive-fixnum, but if the compressed size exceeds
1528 ;; that, we cannot visit the archive anyway.
1529 p (+ p 29 (round csize)))))
1530 (goto-char (point-min))
1531 (let ((dash (concat "- -------- ----------- -------- "
1532 (make-string maxlen ?-)
1534 (insert "M Length Date Time File\n"
1536 (archive-summarize-files (nreverse visual))
1538 (format " %8.0f %d file%s"
1541 (if (= 1 (length files)) "" "s"))
1543 (apply #'vector (nreverse files))))
1545 (defun archive-arc-rename-entry (newname descr)
1546 (if (string-match "[:\\\\/]" newname)
1547 (error "File names in arc files must not contain a directory component"))
1548 (if (> (length newname) 12)
1549 (error "File names in arc files are limited to 12 characters"))
1550 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1552 (inhibit-read-only t))
1556 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1558 (insert-unibyte name)))))
1559 ;; -------------------------------------------------------------------------
1560 ;;; Section: Lzh Archives
1562 (defun archive-lzh-summarize (&optional start)
1563 (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
1568 (while (progn (goto-char p) ;beginning of a base header.
1569 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1570 (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
1571 ;; Convert to float to avoid overflow for very large files.
1572 (csize (archive-l-e (+ p 7) 4 'float)) ;size of a compressed file to follow (level 0 and 2),
1573 ;size of extended headers + the compressed file to follow (level 1).
1574 (ucsize (archive-l-e (+ p 11) 4 'float)) ;size of an uncompressed file.
1575 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1576 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
1577 (hdrlvl (byte-after (+ p 20))) ;header level
1578 thsize ;total header size (base + extensions)
1579 fnlen efnname osid fiddle ifnname width p2
1580 neh ;beginning of next extension header (level 1 and 2)
1581 mode modestr uid gid text dir prname
1582 gname uname modtime moddate)
1583 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1584 (when (or (= hdrlvl 0) (= hdrlvl 1))
1585 (setq fnlen (byte-after (+ p 21))) ;filename length
1586 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
1587 (decode-coding-string
1588 str archive-file-name-coding-system)))
1589 (setq p2 (+ p 22 fnlen))) ;
1591 (setq neh (+ p2 3)) ;specific to level 1 header
1593 (setq neh (+ p 24)))) ;specific to level 2 header
1594 (if neh ;if level 1 or 2 we expect extension headers to follow
1595 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
1596 (etype (byte-after (+ neh 2)))) ;extension type
1597 (while (not (= ehsize 0))
1599 ((= etype 1) ;file name
1600 (let ((i (+ neh 3)))
1601 (while (< i (+ neh ehsize))
1602 (setq efnname (concat efnname (char-to-string (byte-after i))))
1604 ((= etype 2) ;directory name
1605 (let ((i (+ neh 3)))
1606 (while (< i (+ neh ehsize))
1607 (setq dir (concat dir
1608 (if (= (byte-after i)
1614 ((= etype 80) ;Unix file permission
1615 (setq mode (archive-l-e (+ neh 3) 2)))
1616 ((= etype 81) ;UNIX file group/user ID
1617 (progn (setq uid (archive-l-e (+ neh 3) 2))
1618 (setq gid (archive-l-e (+ neh 5) 2))))
1619 ((= etype 82) ;UNIX file group name
1620 (let ((i (+ neh 3)))
1621 (while (< i (+ neh ehsize))
1622 (setq gname (concat gname (char-to-string (char-after i))))
1624 ((= etype 83) ;UNIX file user name
1625 (let ((i (+ neh 3)))
1626 (while (< i (+ neh ehsize))
1627 (setq uname (concat uname (char-to-string (char-after i))))
1630 (setq neh (+ neh ehsize))
1631 (setq ehsize (archive-l-e neh 2))
1632 (setq etype (byte-after (+ neh 2))))
1633 ;;get total header size for level 1 and 2 headers
1634 (setq thsize (- neh p))))
1635 (if (= hdrlvl 0) ;total header size
1636 (setq thsize hsize))
1637 ;; OS ID field not present in level 0 header, use code 0 "generic"
1638 ;; in that case as per lha program header.c get_header()
1639 (setq osid (cond ((= hdrlvl 0) 0)
1640 ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
1641 ((= hdrlvl 2) (char-after (+ p 23)))))
1642 ;; Filename fiddling must follow the lha program, otherwise the name
1643 ;; passed to "lha pq" etc won't match (which for an extract silently
1644 ;; results in no output). As of version 1.14i it goes from the OS ID,
1645 ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
1646 ;; converts "\" to "/".
1647 ;; - For 0 generic: generic_to_unix_filename() downcases if there's
1648 ;; no lower case already present, and converts "\" to "/".
1649 ;; - For 'm' macOS: macos_to_unix_filename() changes "/" to ":" and
1651 (setq fiddle (cond ((= ?M osid) t)
1652 ((= 0 osid) (string= efnname (upcase efnname)))))
1653 (setq ifnname (if fiddle (downcase efnname) efnname))
1654 (setq prname (if dir (concat dir ifnname) ifnname))
1655 (setq width (if prname (string-width prname) 0))
1656 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1657 (setq moddate (if (= hdrlvl 2)
1658 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1659 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1660 (setq modtime (if (= hdrlvl 2)
1661 (archive-unixtime time1 time2)
1662 (archive-dostime time1)))
1663 (setq text (if archive-alternate-display
1664 (format " %8.0f %5S %5S %s"
1669 (format " %10s %8.0f %-11s %-8s %s"
1675 (setq maxlen (max maxlen width)
1676 totalsize (+ totalsize ucsize)
1677 visual (cons (vector text
1678 (- (length text) (length prname))
1681 files (cons (vector prname ifnname fiddle mode (1- p))
1684 ;; p needs to stay an integer, since we use it in goto-char
1685 ;; above. Passing through `round' limits the compressed size
1686 ;; to most-positive-fixnum, but if the compressed size exceeds
1687 ;; that, we cannot visit the archive anyway.
1688 (setq p (+ p hsize 2 (round csize))))
1689 ((or (= hdrlvl 2) (= hdrlvl 0))
1690 (setq p (+ p thsize 2 (round csize)))))
1692 (goto-char (point-min))
1693 (let ((dash (concat (if archive-alternate-display
1694 "- -------- ----- ----- "
1695 "- ---------- -------- ----------- -------- ")
1696 (make-string maxlen ?-)
1698 (header (if archive-alternate-display
1699 "M Length Uid Gid File\n"
1700 "M Filemode Length Date Time File\n"))
1701 (sumline (if archive-alternate-display
1703 " %8.0f %d file%s")))
1704 (insert header dash)
1705 (archive-summarize-files (nreverse visual))
1710 (if (= 1 (length files)) "" "s"))
1712 (apply #'vector (nreverse files))))
1714 (defconst archive-lzh-alternate-display t)
1716 (defun archive-lzh-extract (archive name)
1717 (archive-extract-by-stdout archive name archive-lzh-extract))
1719 (defun archive-lzh-resum (p count)
1722 (setq count (1- count)
1723 sum (+ sum (byte-after p))
1727 (defun archive-lzh-rename-entry (newname descr)
1731 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1732 (oldhsize (byte-after p))
1733 (oldfnlen (byte-after (+ p 21)))
1734 (newfnlen (length newname))
1735 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1736 (inhibit-read-only t))
1737 (if (> newhsize 255)
1738 (error "The file name is too long"))
1739 (goto-char (+ p 21))
1740 (delete-char (1+ oldfnlen))
1741 (insert-unibyte newfnlen newname)
1744 (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
1746 (defun archive-lzh-ogm (newval files errtxt ofs)
1751 (let* ((p (+ archive-proper-file-start (aref fil 4)))
1752 (hsize (byte-after p))
1753 (fnlen (byte-after (+ p 21)))
1755 (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
1756 (inhibit-read-only t))
1759 (or (numberp newval)
1760 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1761 (goto-char (+ p2 ofs))
1763 (insert-unibyte (logand newval 255) (lsh newval -8))
1766 (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
1767 (message "Member %s does not have %s field"
1768 (aref fil 1) errtxt)))))))
1770 (defun archive-lzh-chown-entry (newuid files)
1771 (archive-lzh-ogm newuid files "an uid" 10))
1773 (defun archive-lzh-chgrp-entry (newgid files)
1774 (archive-lzh-ogm newgid files "a gid" 12))
1776 (defun archive-lzh-chmod-entry (newmode files)
1778 ;; This should work even though newmode will be dynamically accessed.
1779 (lambda (old) (archive-calc-mode old newmode t))
1780 files "a unix-style mode" 8))
1782 ;; -------------------------------------------------------------------------
1783 ;;; Section: Lzh Self-Extracting .exe Archives
1785 ;; No support for modifying these files. It looks like the lha for unix
1786 ;; program (as of version 1.14i) can't create or retain the DOS exe part.
1787 ;; If you do an "lha a" on a .exe for instance it renames and writes to a
1790 (defun archive-lzh-exe-summarize ()
1791 "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
1793 ;; Skip the initial executable code part and apply archive-lzh-summarize
1794 ;; to the archive part proper. The "-lh5-" etc regexp here for the start
1795 ;; is the same as in archive-find-type.
1797 ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
1798 ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
1799 ;; the header checksum, or level 2 a test of the "attribute" and size.
1801 (re-search-forward "..-l[hz][0-9ds]-" nil)
1802 (archive-lzh-summarize (match-beginning 0)))
1804 ;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
1806 (defalias 'archive-lzh-exe-extract 'archive-lzh-extract
1807 "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
1809 ;; -------------------------------------------------------------------------
1810 ;;; Section: Zip Archives
1812 (defun archive-zip-summarize ()
1813 (goto-char (- (point-max) (- 22 18)))
1814 (search-backward-regexp "[P]K\005\006")
1815 (let ((p (archive-l-e (+ (point) 16) 4))
1820 emacs-int-has-32bits)
1822 ;; If the offset of end-of-central-directory is -1, this is a
1823 ;; Zip64 extended ZIP file format, and we need to glean the info
1824 ;; from Zip64 records instead.
1826 ;; First, find the Zip64 end-of-central-directory locator.
1827 (search-backward "PK\006\007")
1828 ;; Pay attention: the offset of Zip64 end-of-central-directory
1829 ;; is a 64-bit field, so it could overflow the Emacs integer
1830 ;; even on a 64-bit host, let alone 32-bit one. But since we've
1831 ;; already read the zip file into a buffer, and this is a byte
1832 ;; offset into the file we've read, it must be short enough, so
1833 ;; such an overflow can never happen, and we can safely read
1834 ;; these 8 bytes into an Emacs integer. Moreover, on host with
1835 ;; 32-bit Emacs integer we can only read 4 bytes, since they are
1836 ;; stored in little-endian byte order.
1837 (setq emacs-int-has-32bits (<= most-positive-fixnum #x1fffffff))
1838 (setq p (+ (point-min)
1839 (archive-l-e (+ (point) 8) (if emacs-int-has-32bits 4 8))))
1841 ;; We should be at Zip64 end-of-central-directory record now.
1842 (or (string= "PK\006\006" (buffer-substring p (+ p 4)))
1843 (error "Unrecognized ZIP file format"))
1844 ;; Offset to central directory:
1845 (setq p (archive-l-e (+ p 48) (if emacs-int-has-32bits 4 8))))
1846 (setq p (+ p (point-min)))
1847 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1848 (let* ((creator (byte-after (+ p 5)))
1849 ;; (method (archive-l-e (+ p 10) 2))
1850 (modtime (archive-l-e (+ p 12) 2))
1851 (moddate (archive-l-e (+ p 14) 2))
1852 ;; Convert to float to avoid overflow for very large files.
1853 (ucsize (archive-l-e (+ p 24) 4 'float))
1854 (fnlen (archive-l-e (+ p 28) 2))
1855 (exlen (archive-l-e (+ p 30) 2))
1856 (fclen (archive-l-e (+ p 32) 2))
1857 (lheader (archive-l-e (+ p 42) 4))
1858 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1859 (decode-coding-string
1860 str archive-file-name-coding-system)))
1861 (isdir (and (= ucsize 0)
1862 (string= (file-name-nondirectory efnname) "")))
1863 (mode (cond ((memq creator '(2 3)) ; Unix
1864 (archive-l-e (+ p 40) 2))
1865 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1867 (if isdir (logior 16384 ?\111) 0)
1869 (logand 1 (byte-after (+ p 38))))
1872 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1873 (fiddle (and archive-zip-case-fiddle
1874 (not (not (memq creator '(0 2 4 5 9))))
1875 (string= (upcase efnname) efnname)))
1876 (ifnname (if fiddle (downcase efnname) efnname))
1877 (width (string-width ifnname))
1878 (text (format " %10s %8.0f %-11s %-8s %s"
1881 (archive-dosdate moddate)
1882 (archive-dostime modtime)
1884 (setq maxlen (max maxlen width)
1885 totalsize (+ totalsize ucsize)
1886 visual (cons (vector text
1887 (- (length text) (length ifnname))
1890 files (cons (if isdir
1892 (vector efnname ifnname fiddle mode
1893 (list (1- p) lheader)))
1895 p (+ p 46 fnlen exlen fclen))))
1896 (goto-char (point-min))
1897 (let ((dash (concat "- ---------- -------- ----------- -------- "
1898 (make-string maxlen ?-)
1900 (insert "M Filemode Length Date Time File\n"
1902 (archive-summarize-files (nreverse visual))
1904 (format " %8.0f %d file%s"
1907 (if (= 1 (length files)) "" "s"))
1909 (apply #'vector (nreverse files))))
1911 (defun archive-zip-extract (archive name)
1913 ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
1914 (archive-*-extract archive name archive-zip-extract))
1915 ((equal (car archive-zip-extract) archive-7z-program)
1916 (let ((archive-7z-extract archive-zip-extract))
1917 (archive-7z-extract archive name)))
1919 (archive-extract-by-stdout
1921 ;; unzip expands wildcards in NAME, so we need to quote it. But
1922 ;; not on DOS/Windows, since that fails extraction on those
1923 ;; systems (unless w32-quote-process-args is nil), and file names
1924 ;; with wildcards in zip archives don't work there anyway.
1925 ;; FIXME: Does pkunzip need similar treatment?
1926 (if (and (or (not (memq system-type '(windows-nt ms-dos)))
1927 (and (boundp 'w32-quote-process-args)
1928 (null w32-quote-process-args)))
1929 (equal (car archive-zip-extract) "unzip"))
1930 (shell-quote-argument name)
1932 archive-zip-extract))))
1934 (defun archive-zip-write-file-member (archive descr)
1935 (archive-*-write-file-member
1938 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1940 (defun archive-zip-chmod-entry (newmode files)
1945 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
1946 (creator (byte-after (+ p 5)))
1947 (oldmode (aref fil 3))
1948 (newval (archive-calc-mode oldmode newmode t))
1949 (inhibit-read-only t))
1950 (cond ((memq creator '(2 3)) ; Unix
1951 (goto-char (+ p 40))
1953 (insert-unibyte (logand newval 255) (lsh newval -8)))
1954 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1955 (goto-char (+ p 38))
1956 (insert-unibyte (logior (logand (byte-after (point)) 254)
1957 (logand (logxor 1 (lsh newval -7)) 1)))
1959 (t (message "Don't know how to change mode for this member"))))
1961 ;; -------------------------------------------------------------------------
1962 ;;; Section: Zoo Archives
1964 (defun archive-zoo-summarize ()
1965 (let ((p (1+ (archive-l-e 25 4)))
1970 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1971 (> (archive-l-e (+ p 6) 4) 0))
1972 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1973 (moddate (archive-l-e (+ p 14) 2))
1974 (modtime (archive-l-e (+ p 16) 2))
1975 ;; Convert to float to avoid overflow for very large files.
1976 (ucsize (archive-l-e (+ p 20) 4 'float))
1977 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1978 (dirtype (byte-after (+ p 4)))
1979 (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
1980 (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
1981 (fnlen (or (string-match "\0" namefld) 13))
1985 (concat (buffer-substring
1987 (+ p 58 lfnlen ldirlen -1))
1991 (buffer-substring (+ p 58)
1993 (substring namefld 0 fnlen)))))
1994 (decode-coding-string
1995 str archive-file-name-coding-system)))
1996 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1997 (ifnname (if fiddle (downcase efnname) efnname))
1998 (width (string-width ifnname))
1999 (text (format " %8.0f %-11s %-8s %s"
2001 (archive-dosdate moddate)
2002 (archive-dostime modtime)
2004 (setq maxlen (max maxlen width)
2005 totalsize (+ totalsize ucsize)
2006 visual (cons (vector text
2007 (- (length text) (length ifnname))
2010 files (cons (vector efnname ifnname fiddle nil (1- p))
2013 (goto-char (point-min))
2014 (let ((dash (concat "- -------- ----------- -------- "
2015 (make-string maxlen ?-)
2017 (insert "M Length Date Time File\n"
2019 (archive-summarize-files (nreverse visual))
2021 (format " %8.0f %d file%s"
2024 (if (= 1 (length files)) "" "s"))
2026 (apply #'vector (nreverse files))))
2028 (defun archive-zoo-extract (archive name)
2029 (archive-extract-by-stdout archive name archive-zoo-extract))
2031 ;; -------------------------------------------------------------------------
2032 ;;; Section: Rar Archives
2034 (defun archive-rar-summarize (&optional file)
2035 ;; File is used internally for `archive-rar-exe-summarize'.
2036 (unless file (setq file buffer-file-name))
2037 (let* ((copy (file-local-copy file))
2042 (call-process "lsar" nil t nil "-l" (or file copy))
2043 (if copy (delete-file copy))
2044 (goto-char (point-min))
2045 (re-search-forward "^\\(\s+=+\s?+\\)+\n")
2046 (while (looking-at (concat "^\s+[0-9.]+\s+-+\s+" ; Flags
2047 "\\([0-9-]+\\)\s+" ; Size
2048 "\\([0-9.%]+\\)\s+" ; Ratio
2049 "\\([0-9a-zA-Z]+\\)\s+" ; Mode
2050 "\\([0-9-]+\\)\s+" ; Date
2051 "\\([0-9:]+\\)\s+" ; Time
2054 (goto-char (match-end 0))
2055 (let ((name (match-string 6))
2056 (size (match-string 1)))
2057 (if (> (length name) maxname) (setq maxname (length name)))
2058 (if (> (length size) maxsize) (setq maxsize (length size)))
2059 (push (vector name name nil nil
2061 size (match-string 2)
2063 (match-string 4) (match-string 5))
2065 (setq files (nreverse files))
2066 (goto-char (point-min))
2067 (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
2068 (sep (format format "----------" "-----" (make-string maxsize ?-)
2070 (column (length sep)))
2071 (insert (format format " Date " "Time " "Size" "Ratio" "Filename") "\n")
2072 (insert sep (make-string maxname ?-) "\n")
2073 (archive-summarize-files (mapcar (lambda (desc)
2085 (insert sep (make-string maxname ?-) "\n")
2086 (apply #'vector files))))
2088 (defun archive-rar-extract (archive name)
2089 ;; unrar-free seems to have no way to extract to stdout or even to a file.
2090 (if (file-name-absolute-p name)
2091 ;; The code below assumes the name is relative and may do undesirable
2092 ;; things otherwise.
2093 (error "Can't extract files with non-relative names")
2094 (archive-extract-by-file archive name `("unar" "-no-directory" "-o") "Successfully extracted")))
2096 ;;; Section: Rar self-extracting .exe archives.
2098 (defun archive-rar-exe-summarize ()
2099 (let ((tmpfile (make-temp-file "rarexe")))
2102 (goto-char (point-min))
2103 (re-search-forward "Rar!")
2104 (write-region (match-beginning 0) (point-max) tmpfile)
2105 (archive-rar-summarize tmpfile))
2106 (delete-file tmpfile))))
2108 (defun archive-rar-exe-extract (archive name)
2109 (let* ((tmpfile (make-temp-file "rarexe"))
2110 (buf (find-buffer-visiting archive))
2111 (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
2114 (with-current-buffer (or buf tmpbuf)
2118 ;; point-max unwidened is assumed to be the end of the
2119 ;; summary text and the beginning of the actual file data.
2120 (progn (goto-char (point-max)) (widen))
2121 (insert-file-contents-literally archive)
2122 (goto-char (point-min)))
2123 (re-search-forward "Rar!")
2124 (write-region (match-beginning 0) (point-max) tmpfile))))
2125 (archive-rar-extract tmpfile name))
2126 (if tmpbuf (kill-buffer tmpbuf))
2127 (delete-file tmpfile))))
2129 ;; -------------------------------------------------------------------------
2130 ;;; Section: 7z Archives
2132 (defun archive-7z-summarize ()
2135 (file buffer-file-name)
2138 (call-process archive-7z-program nil t nil "l" "-slt" file)
2139 (goto-char (point-min))
2140 ;; Four dashes start the meta info section that should be skipped.
2141 ;; Archive members start with more than four dashes.
2142 (re-search-forward "^-----+\n")
2143 (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
2144 (goto-char (match-end 0))
2145 (let ((name (match-string 1))
2146 (size (save-excursion
2147 (and (re-search-forward "^Size = \\(.*\\)\n")
2149 (time (save-excursion
2150 (and (re-search-forward "^Modified = \\(.*\\)\n")
2151 (match-string 1)))))
2152 (if (> (length name) maxname) (setq maxname (length name)))
2153 (if (> (length size) maxsize) (setq maxsize (length size)))
2154 (push (vector name name nil nil time nil nil size)
2156 (setq files (nreverse files))
2157 (goto-char (point-min))
2158 (let* ((format (format " %%%ds %%s %%s" maxsize))
2159 (sep (format format (make-string maxsize ?-) "-------------------" ""))
2160 (column (length sep)))
2161 (insert (format format "Size " "Date Time " " Filename") "\n")
2162 (insert sep (make-string maxname ?-) "\n")
2163 (archive-summarize-files (mapcar (lambda (desc)
2173 (insert sep (make-string maxname ?-) "\n")
2174 (apply #'vector files))))
2176 (defun archive-7z-extract (archive name)
2177 ;; 7z doesn't provide a `quiet' option to suppress non-essential
2178 ;; stderr messages. So redirect stderr to a temp file and display it
2179 ;; in the echo area when it contains no message indicating success.
2180 (archive-extract-by-stdout
2181 archive name archive-7z-extract "Everything is Ok"))
2183 (defun archive-7z-write-file-member (archive descr)
2184 (archive-*-write-file-member
2189 ;; -------------------------------------------------------------------------
2190 ;;; Section `ar' archives.
2192 ;; TODO: we currently only handle the basic format of ar archives,
2193 ;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
2194 ;; for .deb packages.
2196 (autoload 'tar-grind-file-mode "tar-mode")
2198 (defconst archive-ar-file-header-re
2199 "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
2201 (defun archive-ar-summarize ()
2202 ;; File is used internally for `archive-rar-exe-summarize'.
2210 (goto-char (point-min))
2211 (search-forward "!<arch>\n")
2212 (while (looking-at archive-ar-file-header-re)
2213 (let ((name (match-string 1))
2215 ;; Emacs will automatically use float here because those
2216 ;; timestamps don't fit in our ints.
2217 (time (string-to-number (match-string 2)))
2218 (user (match-string 3))
2219 (group (match-string 4))
2220 (mode (string-to-number (match-string 5) 8))
2221 (size (string-to-number (match-string 6))))
2222 ;; Move to the beginning of the data.
2223 (goto-char (match-end 0))
2224 (setq time (format-time-string "%Y-%m-%d %H:%M" time))
2226 (cond ((equal name "// ")
2227 (propertize ".<ExtNamesTable>." 'face 'italic))
2229 (propertize ".<LookupTable>." 'face 'italic))
2230 ((string-match "/? *\\'" name)
2231 (substring name 0 (match-beginning 0)))))
2232 (setq user (substring user 0 (string-match " +\\'" user)))
2233 (setq group (substring group 0 (string-match " +\\'" group)))
2234 (setq mode (tar-grind-file-mode mode))
2235 ;; Move to the end of the data.
2236 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1))
2237 (setq size (number-to-string size))
2238 (if (> (length name) maxname) (setq maxname (length name)))
2239 (if (> (length time) maxtime) (setq maxtime (length time)))
2240 (if (> (length user) maxuser) (setq maxuser (length user)))
2241 (if (> (length group) maxgroup) (setq maxgroup (length group)))
2242 (if (> (length mode) maxmode) (setq maxmode (length mode)))
2243 (if (> (length size) maxsize) (setq maxsize (length size)))
2244 (push (vector name extname nil mode
2245 time user group size)
2247 (setq files (nreverse files))
2248 (goto-char (point-min))
2249 (let* ((format (format "%%%ds %%%ds/%%-%ds %%%ds %%%ds %%s"
2250 maxmode maxuser maxgroup maxsize maxtime))
2251 (sep (format format (make-string maxmode ?-)
2252 (make-string maxuser ?-)
2253 (make-string maxgroup ?-)
2254 (make-string maxsize ?-)
2255 (make-string maxtime ?-) ""))
2256 (column (length sep)))
2257 (insert (format format " Mode " "User" "Group" " Size "
2258 " Date " "Filename")
2260 (insert sep (make-string maxname ?-) "\n")
2261 (archive-summarize-files (mapcar (lambda (desc)
2274 (insert sep (make-string maxname ?-) "\n")
2275 (apply #'vector files))))
2277 (defun archive-ar-extract (archive name)
2278 (let ((destbuf (current-buffer))
2279 (archivebuf (find-file-noselect archive))
2281 (with-current-buffer archivebuf
2283 ;; We may be in archive-mode or not, so either with or without
2284 ;; narrowing and with or without a prepended summary.
2287 (search-forward "!<arch>\n")
2288 (while (and (not from) (looking-at archive-ar-file-header-re))
2289 (let ((this (match-string 1)))
2290 (setq size (string-to-number (match-string 6)))
2291 (goto-char (match-end 0))
2292 (if (equal name this)
2294 ;; Move to the end of the data.
2295 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1)))))
2297 (set-buffer-multibyte nil)
2298 (with-current-buffer destbuf
2299 ;; Do it within the `widen'.
2300 (insert-buffer-substring archivebuf from (+ from size)))
2301 (set-buffer-multibyte 'to)
2302 ;; Inform the caller that the call succeeded.
2305 ;; -------------------------------------------------------------------------
2306 ;; This line was a mistake; it is kept now for compatibility.
2308 (provide 'archive-mode)
2312 ;;; arc-mode.el ends here