1 ;;; arc-mode.el --- simple editing of archives
3 ;; Copyright (C) 1995, 1997-1998, 2001-2018 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 <https://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)
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."
539 (setq len (length str))
540 (setq str (buffer-substring str (+ str len))))
541 (setq str (string-as-unibyte str))
546 result (+ (ash result 8)
547 (aref str (- len i)))))
550 (defun archive-int-to-mode (mode)
551 "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------."
552 ;; FIXME: merge with tar-grind-file-mode.
554 (if (zerop (logand 8192 mode))
555 (if (zerop (logand 16384 mode)) ?- ?d)
557 (if (zerop (logand 256 mode)) ?- ?r)
558 (if (zerop (logand 128 mode)) ?- ?w)
559 (if (zerop (logand 64 mode))
560 (if (zerop (logand 2048 mode)) ?- ?S)
561 (if (zerop (logand 2048 mode)) ?x ?s))
562 (if (zerop (logand 32 mode)) ?- ?r)
563 (if (zerop (logand 16 mode)) ?- ?w)
564 (if (zerop (logand 8 mode))
565 (if (zerop (logand 1024 mode)) ?- ?S)
566 (if (zerop (logand 1024 mode)) ?x ?s))
567 (if (zerop (logand 4 mode)) ?- ?r)
568 (if (zerop (logand 2 mode)) ?- ?w)
569 (if (zerop (logand 1 mode)) ?- ?x)))
571 (defun archive-calc-mode (oldmode newmode &optional error)
572 "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
573 NEWMODE may be an octal number including a leading zero in which case it
574 will become the new mode.\n
575 NEWMODE may also be a relative specification like \"og-rwx\" in which case
576 OLDMODE will be modified accordingly just like chmod(2) would have done.\n
577 If optional third argument ERROR is non-nil an error will be signaled if
578 the mode is invalid. If ERROR is nil then nil will be returned."
579 (cond ((string-match "^0[0-7]*$" newmode)
581 (len (length newmode))
584 (setq result (+ (ash result 3) (aref newmode i) (- ?0))
586 (logior (logand oldmode 65024) result)))
587 ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
590 (op (aref newmode (match-beginning 2)))
592 (i (match-beginning 3)))
593 (while (< i (match-end 3))
594 (let ((rwx (aref newmode i)))
595 (setq bits (logior bits (cond ((= rwx ?r) 292)
601 (while (< who (match-end 1))
602 (let* ((whoc (aref newmode who))
603 (whomask (cond ((= whoc ?a) 4095)
608 (setq result (logand result (lognot whomask))))
610 (setq result (logand result (lognot (logand whomask bits))))
611 (setq result (logior result (logand whomask bits)))))
616 (error "Invalid mode specification: %s" newmode)))))
618 (defun archive-dosdate (date)
619 "Stringify dos packed DATE record."
620 (let ((year (+ 1980 (logand (ash date -9) 127)))
621 (month (logand (ash date -5) 15))
622 (day (logand date 31)))
623 (if (or (> month 12) (< month 1))
627 (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
628 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
631 (defun archive-dostime (time)
632 "Stringify dos packed TIME record."
633 (let ((hour (logand (ash time -11) 31))
634 (minute (logand (ash time -5) 63))
635 (second (* 2 (logand time 31)))) ; 2 seconds resolution
636 (format "%02d:%02d:%02d" hour minute second)))
638 (defun archive-unixdate (low high)
639 "Stringify Unix (LOW HIGH) date."
640 (let* ((time (cons high low))
641 (str (current-time-string time)))
645 (format-time-string "%Y" time))))
647 (defun archive-unixtime (low high)
648 "Stringify Unix (LOW HIGH) time."
649 (let ((str (current-time-string (cons high low))))
650 (substring str 11 19)))
652 (defun archive-get-lineno ()
653 (if (>= (point) archive-file-list-start)
654 (count-lines archive-file-list-start
655 (line-beginning-position))
658 (defun archive-get-descr (&optional noerror)
659 "Return the descriptor vector for file at point.
660 Does not signal an error if optional argument NOERROR is non-nil."
661 (let ((no (archive-get-lineno)))
662 (if (and (>= (point) archive-file-list-start)
663 (< no (length archive-files)))
664 (let ((item (aref archive-files no)))
668 (error "Entry is not a regular member of the archive"))))
670 (error "Line does not describe a member of the archive")))))
671 ;; -------------------------------------------------------------------------
672 ;;; Section: the mode definition
675 (defun archive-mode (&optional force)
676 "Major mode for viewing an archive file in a dired-like way.
677 You can move around using the usual cursor motion commands.
678 Letters no longer insert themselves.
679 Type `e' to pull a file out of the archive and into its own buffer;
680 or click mouse-2 on the file's line in the archive mode buffer.
682 If you edit a sub-file of this archive (as with the `e' command) and
683 save it, the contents of that buffer will be saved back into the
686 \\{archive-mode-map}"
687 ;; This is not interactive because you shouldn't be turning this
688 ;; mode on and off. You can corrupt things that way.
689 (if (zerop (buffer-size))
690 ;; At present we cannot create archives from scratch
691 (funcall (or (default-value 'major-mode) 'fundamental-mode))
692 (if (and (not force) archive-files) nil
693 (kill-all-local-variables)
694 (let* ((type (archive-find-type))
695 (typename (capitalize (symbol-name type))))
696 (make-local-variable 'archive-subtype)
697 (setq archive-subtype type)
699 ;; Buffer contains treated image of file before the file contents
700 (make-local-variable 'revert-buffer-function)
701 (setq revert-buffer-function 'archive-mode-revert)
704 (add-hook 'write-contents-functions 'archive-write-file nil t)
706 (make-local-variable 'require-final-newline)
707 (setq require-final-newline nil)
708 (make-local-variable 'local-enable-local-variables)
709 (setq local-enable-local-variables nil)
711 ;; Prevent loss of data when saving the file.
712 (make-local-variable 'file-precious-flag)
713 (setq file-precious-flag t)
715 (make-local-variable 'archive-read-only)
716 ;; Archives which are inside other archives and whose
717 ;; names are invalid for this OS, can't be written.
718 (setq archive-read-only
719 (or (not (file-writable-p (buffer-file-name)))
720 (and archive-subfile-mode
721 (string-match file-name-invalid-regexp
722 (aref archive-subfile-mode 0)))))
724 ;; Should we use a local copy when accessing from outside Emacs?
725 (make-local-variable 'archive-local-name)
727 ;; An archive can contain another archive whose name is invalid
728 ;; on local filesystem. Treat such archives as remote.
731 (or (string-match archive-remote-regexp (buffer-file-name))
732 (string-match file-name-invalid-regexp
733 (buffer-file-name)))))
735 (setq major-mode 'archive-mode)
736 (setq mode-name (concat typename "-Archive"))
737 ;; Run archive-foo-mode-hook and archive-mode-hook
738 (run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
739 (use-local-map archive-mode-map))
741 (make-local-variable 'archive-proper-file-start)
742 (make-local-variable 'archive-file-list-start)
743 (make-local-variable 'archive-file-list-end)
744 (make-local-variable 'archive-file-name-indent)
745 (setq archive-file-name-coding-system
746 (or file-name-coding-system
747 default-file-name-coding-system
748 locale-coding-system))
749 (set-buffer-multibyte 'to)
750 (archive-summarize nil)
751 (setq buffer-read-only t)
752 (when (and archive-visit-single-files
753 auto-compression-mode
754 (= (length archive-files) 1))
755 (rename-buffer (concat " " (buffer-name)))
756 (archive-extract)))))
758 ;; Archive mode is suitable only for specially formatted data.
759 (put 'archive-mode 'mode-class 'special)
761 (let ((item1 '(archive-subfile-mode " Archive")))
762 (or (member item1 minor-mode-alist)
763 (setq minor-mode-alist (cons item1 minor-mode-alist))))
764 ;; -------------------------------------------------------------------------
765 (defun archive-find-type ()
767 (goto-char (point-min))
768 ;; The funny [] here make it unlikely that the .elc file will be treated
769 ;; as an archive by other software.
770 (let (case-fold-search)
771 (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
772 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
773 ((looking-at "....................[\334]\247\304\375") 'zoo)
774 ((and (looking-at "\C-z") ; signature too simple, IMHO
775 (string-match "\\.[aA][rR][cC]\\'"
776 (or buffer-file-name (buffer-name))))
778 ;; This pattern modeled on the BSD/GNU+Linux `file' command.
779 ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
780 ;; Note this regexp is also in archive-exe-p.
781 ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
782 ((looking-at "Rar!") 'rar)
783 ((looking-at "!<arch>\n") 'ar)
784 ((and (looking-at "MZ")
785 (re-search-forward "Rar!" (+ (point) 100000) t))
787 ((looking-at "7z\274\257\047\034") '7z)
788 (t (error "Buffer format not recognized")))))
789 ;; -------------------------------------------------------------------------
791 (defun archive-desummarize ()
792 (let ((inhibit-read-only t)
793 (modified (buffer-modified-p)))
795 (delete-region (point-min) archive-proper-file-start)
796 (restore-buffer-modified-p modified)))
799 (defun archive-summarize (&optional shut-up)
800 "Parse the contents of the archive file in the current buffer.
801 Place a dired-like listing on the front;
802 then narrow to it, so that only that listing
803 is visible (and the real data of the buffer is hidden).
804 Optional argument SHUT-UP, if non-nil, means don't print messages
805 when parsing the archive."
807 (let ((create-lockfiles nil) ; avoid changing dir mtime by lock_file
808 (inhibit-read-only t))
809 (setq archive-proper-file-start (copy-marker (point-min) t))
810 (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
812 (message "Parsing archive file..."))
813 (buffer-disable-undo (current-buffer))
814 (setq archive-files (funcall (archive-name "summarize")))
816 (message "Parsing archive file...done."))
817 (setq archive-proper-file-start (point-marker))
818 (narrow-to-region (point-min) (point))
819 (set-buffer-modified-p nil)
820 (buffer-enable-undo))
821 (goto-char archive-file-list-start)
822 (archive-next-line 0))
824 (defun archive-resummarize ()
825 "Recreate the contents listing of an archive."
826 (let ((no (archive-get-lineno)))
827 (archive-desummarize)
828 (archive-summarize t)
829 (goto-char archive-file-list-start)
830 (archive-next-line no)))
832 (defun archive-summarize-files (files)
833 "Insert a description of a list of files annotated with proper mouse face."
834 (setq archive-file-list-start (point-marker))
835 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
836 ;; We don't want to do an insert for each element since that takes too
837 ;; long when the archive -- which has to be moved in memory -- is large.
843 ;; Using `concat' here copies the text also, so we can add
844 ;; properties without problems.
845 (let ((text (concat (aref fil 0) "\n")))
846 (if (featurep 'xemacs)
849 (aref fil 1) (aref fil 2)
850 '(mouse-face highlight
851 help-echo "mouse-2: extract this file into a buffer")
855 (setq archive-file-list-end (point-marker)))
857 (defun archive-alternate-display ()
858 "Toggle alternative display.
859 To avoid very long lines archive mode does not show all information.
860 This function changes the set of information shown for each files."
862 (setq archive-alternate-display (not archive-alternate-display))
863 (archive-resummarize))
864 ;; -------------------------------------------------------------------------
865 ;;; Section: Local archive copy handling
867 (defun archive-unique-fname (fname dir)
868 "Make sure a file FNAME can be created uniquely in directory DIR.
870 If FNAME can be uniquely created in DIR, it is returned unaltered.
871 If FNAME is something our underlying filesystem can't grok, or if another
872 file by that name already exists in DIR, a unique new name is generated
873 using `make-temp-file', and the generated name is returned."
874 (let ((fullname (expand-file-name fname dir))
875 (alien (string-match file-name-invalid-regexp fname))
878 (if (if (fboundp 'msdos-long-file-names)
879 (not (msdos-long-file-names)))
883 (if (or alien (file-exists-p fullname))
885 ;; Make sure all the leading directories in
886 ;; archive-local-name exist under archive-tmpdir, so that
887 ;; the directory structure recorded in the archive is
888 ;; reconstructed in the temporary directory.
889 (make-directory (file-name-directory tmpfile) t)
890 (make-temp-file tmpfile))
891 ;; Make sure all the leading directories in `fullname' exist
892 ;; under archive-tmpdir. This is necessary for nested archives
893 ;; (`archive-extract' sets `archive-remote' to t in case
894 ;; an archive occurs inside another archive).
895 (make-directory (file-name-directory fullname) t)
898 (defun archive-maybe-copy (archive)
899 (let ((coding-system-for-write 'no-conversion))
901 (let ((start (point-max))
902 ;; Sometimes ARCHIVE is invalid while its actual name, as
903 ;; recorded in its parent archive, is not. For example, an
904 ;; archive bar.zip inside another archive foo.zip gets a name
905 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
906 ;; So use the actual name if available.
908 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
910 (setq archive-local-name
911 (archive-unique-fname archive-name archive-tmpdir))
914 (write-region start (point-max) archive-local-name nil 'nomessage))
916 (if (buffer-modified-p) (save-buffer))
919 (defun archive-maybe-update (unchanged)
921 (let ((name archive-local-name)
922 (modified (buffer-modified-p))
923 (coding-system-for-read 'no-conversion)
924 (lno (archive-get-lineno))
925 (inhibit-read-only t))
927 (setq archive-files nil)
929 (insert-file-contents name)
931 (goto-char archive-file-list-start)
932 (archive-next-line lno))
933 (archive-delete-local name)
936 "Buffer `%s' must be saved for changes to take effect"
937 (buffer-name (current-buffer))))
938 (set-buffer-modified-p (or modified (not unchanged))))))
940 (defun archive-delete-local (name)
941 "Delete file NAME and its parents up to and including `archive-tmpdir'."
943 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
948 (setq name (directory-file-name (file-name-directory name)))
950 (delete-directory name)
952 (if (string= name top) (setq again nil)))))
953 ;; -------------------------------------------------------------------------
954 ;;; Section: Member extraction
956 (defun archive-try-jka-compr ()
957 (when (and auto-compression-mode
958 (jka-compr-get-compression-info buffer-file-name))
959 (let* ((basename (file-name-nondirectory buffer-file-name))
960 (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
961 (match-string 1 basename) basename))
962 (tmpfile (make-temp-file (file-name-sans-extension tmpname)
964 (file-name-extension tmpname 'period))))
967 (let ((coding-system-for-write 'no-conversion)
968 ;; Don't re-compress this data just before decompressing it.
969 (jka-compr-inhibit t))
970 (write-region (point-min) (point-max) tmpfile nil 'quiet))
972 (let ((coding-system-for-read 'no-conversion))
973 (insert-file-contents tmpfile)))
974 (delete-file tmpfile)))))
976 (defun archive-file-name-handler (op &rest args)
977 (or (eq op 'file-exists-p)
978 (let ((file-name-handler-alist nil))
981 (defun archive-set-buffer-as-visiting-file (filename)
982 "Set the current buffer as if it were visiting FILENAME."
984 (goto-char (point-min))
985 (let ((buffer-undo-list t)
987 (or coding-system-for-read
988 (and set-auto-coding-function
990 (funcall set-auto-coding-function
991 filename (- (point-max) (point-min)))))
992 ;; The following let-binding of file-name-handler-alist forces
993 ;; find-file-not-found-set-buffer-file-coding-system to ignore
994 ;; the file's name (see dos-w32.el).
995 (let ((file-name-handler-alist
996 '(("" . archive-file-name-handler))))
997 (car (find-operation-coding-system
998 'insert-file-contents
999 (cons filename (current-buffer)) t))))))
1000 (unless (or coding-system-for-read
1001 enable-multibyte-characters)
1003 (coding-system-change-text-conversion coding 'raw-text)))
1004 (unless (memq coding '(nil no-conversion))
1005 (decode-coding-region (point-min) (point-max) coding)
1006 (setq last-coding-system-used coding))
1007 (set-buffer-modified-p nil)
1008 (kill-local-variable 'buffer-file-coding-system)
1009 (after-insert-file-set-coding (- (point-max) (point-min))))))
1011 (defun archive-extract (&optional other-window-p event)
1012 "In archive mode, extract this entry of the archive into its own buffer."
1013 (interactive (list nil last-input-event))
1014 (if event (posn-set-point (event-end event)))
1015 (let* ((view-p (eq other-window-p 'view))
1016 (descr (archive-get-descr))
1017 (ename (aref descr 0))
1018 (iname (aref descr 1))
1019 (archive-buffer (current-buffer))
1020 (arcdir default-directory)
1021 (archive (buffer-file-name))
1022 (arcname (file-name-nondirectory archive))
1023 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
1024 (extractor (archive-name "extract"))
1025 ;; Members with file names which aren't valid for the
1026 ;; underlying filesystem, are treated as read-only.
1027 (read-only-p (or archive-read-only
1029 (string-match file-name-invalid-regexp ename)))
1030 (arcfilename (expand-file-name (concat arcname ":" iname)))
1031 (buffer (get-buffer bufname))
1033 (file-name-coding archive-file-name-coding-system))
1035 (string= (buffer-file-name buffer) arcfilename))
1037 (setq archive (archive-maybe-copy archive))
1038 (setq bufname (generate-new-buffer-name bufname))
1039 (setq buffer (get-buffer-create bufname))
1040 (setq just-created t)
1041 (with-current-buffer buffer
1042 (setq buffer-file-name arcfilename)
1043 (setq buffer-file-truename
1044 (abbreviate-file-name buffer-file-name))
1045 ;; Set the default-directory to the dir of the superior buffer.
1046 (setq default-directory arcdir)
1047 (make-local-variable 'archive-superior-buffer)
1048 (setq archive-superior-buffer archive-buffer)
1049 (add-hook 'write-file-functions #'archive-write-file-member nil t)
1050 (setq archive-subfile-mode descr)
1051 (setq archive-file-name-coding-system file-name-coding)
1054 (let (;; We may have to encode the file name argument for
1055 ;; external programs.
1056 (coding-system-for-write
1057 (and enable-multibyte-characters
1058 archive-file-name-coding-system))
1059 ;; We read an archive member by no-conversion at
1060 ;; first, then decode appropriately by calling
1061 ;; archive-set-buffer-as-visiting-file later.
1062 (coding-system-for-read 'no-conversion)
1063 ;; Avoid changing dir mtime by lock_file
1064 (create-lockfiles nil))
1066 (if (fboundp extractor)
1067 (funcall extractor archive ename)
1068 (archive-*-extract archive ename
1069 (symbol-value extractor)))
1071 (ding (message "%s" (error-message-string err)))
1075 (set-buffer-modified-p nil)
1076 (kill-buffer buffer))
1077 (archive-try-jka-compr) ;Pretty ugly hack :-(
1078 (archive-set-buffer-as-visiting-file ename)
1079 (goto-char (point-min))
1080 (rename-buffer bufname)
1081 (setq buffer-read-only read-only-p)
1082 (setq buffer-undo-list nil)
1083 (set-buffer-modified-p nil)
1084 (setq buffer-saved-size (buffer-size))
1086 ;; Just in case an archive occurs inside another archive.
1087 (when (derived-mode-p 'archive-mode)
1088 (setq archive-remote t)
1089 (if read-only-p (setq archive-read-only t))
1090 ;; We will write out the archive ourselves if it is
1091 ;; part of another archive.
1092 (remove-hook 'write-contents-functions #'archive-write-file t))
1093 (run-hooks 'archive-extract-hook)
1094 (if archive-read-only
1095 (message "Note: altering this archive is not implemented."))))
1096 (archive-maybe-update t))
1097 (or (not (buffer-name buffer))
1100 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
1101 ((eq other-window-p 'display) (display-buffer buffer))
1102 (other-window-p (switch-to-buffer-other-window buffer))
1103 (t (switch-to-buffer buffer))))))
1105 (defun archive-*-extract (archive name command)
1106 (let* ((default-directory (file-name-as-directory archive-tmpdir))
1107 (tmpfile (expand-file-name (file-name-nondirectory name)
1109 exit-status success)
1110 (make-directory (directory-file-name default-directory) t)
1112 (apply #'call-process
1117 (append (cdr command) (list archive name))))
1118 (cond ((and (numberp exit-status) (zerop exit-status))
1119 (if (not (file-exists-p tmpfile))
1120 (ding (message "`%s': no such file or directory" tmpfile))
1121 (insert-file-contents tmpfile)
1123 ((numberp exit-status)
1125 (message "`%s' exited with status %d" (car command) exit-status)))
1126 ((stringp exit-status)
1127 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1129 (ding (message "`%s' failed" (car command)))))
1130 (archive-delete-local tmpfile)
1133 (defun archive-extract-by-stdout (archive name command &optional stderr-test)
1134 (let ((stderr-file (make-temp-file "arc-stderr")))
1137 (apply #'call-process
1140 (if stderr-file (list t stderr-file) t)
1142 (append (cdr command) (list archive name)))
1144 (insert-file-contents stderr-file)
1145 (goto-char (point-min))
1146 (when (if (stringp stderr-test)
1147 (not (re-search-forward stderr-test nil t))
1148 (> (buffer-size) 0))
1149 (message "%s" (buffer-string)))))
1150 (if (file-exists-p stderr-file)
1151 (delete-file stderr-file)))))
1153 (defun archive-extract-by-file (archive name command &optional stdout-test)
1154 (let ((dest (make-temp-file "arc-dir" 'dir))
1155 (stdout-file (make-temp-file "arc-stdout")))
1158 (apply #'call-process
1161 `(:file ,stdout-file)
1163 `(,archive ,name ,@(cdr command) ,dest))
1165 (insert-file-contents stdout-file)
1166 (goto-char (point-min))
1167 (when (if (stringp stdout-test)
1168 (not (re-search-forward stdout-test nil t))
1169 (> (buffer-size) 0))
1170 (message "%s" (buffer-string))))
1171 (if (file-exists-p (expand-file-name name dest))
1172 (insert-file-contents-literally (expand-file-name name dest))))
1173 (if (file-exists-p stdout-file)
1174 (delete-file stdout-file))
1175 (if (file-exists-p (expand-file-name name dest))
1176 (delete-file (expand-file-name name dest)))
1177 (while (file-name-directory name)
1178 (setq name (directory-file-name (file-name-directory name)))
1179 (when (file-directory-p (expand-file-name name dest))
1180 (delete-directory (expand-file-name name dest))))
1181 (when (file-directory-p dest)
1182 (delete-directory dest)))))
1184 (defun archive-extract-other-window ()
1185 "In archive mode, find this member in another window."
1187 (archive-extract t))
1189 (defun archive-display-other-window ()
1190 "In archive mode, display this member in another window."
1192 (archive-extract 'display))
1194 (defun archive-view ()
1195 "In archive mode, view the member on this line."
1197 (archive-extract 'view))
1199 (defun archive-add-new-member (arcbuf name)
1200 "Add current buffer to the archive in ARCBUF naming it NAME."
1203 (read-buffer "Buffer containing archive: "
1204 ;; Find first archive buffer and suggest that
1205 (let ((bufs (buffer-list)))
1207 (not (with-current-buffer (car bufs)
1208 (derived-mode-p 'archive-mode))))
1209 (setq bufs (cdr bufs)))
1212 (error "There are no archive buffers")))
1214 (read-string "File name in archive: "
1215 (if buffer-file-name
1216 (file-name-nondirectory buffer-file-name)
1218 (with-current-buffer arcbuf
1219 (or (derived-mode-p 'archive-mode)
1220 (error "Buffer is not an archive buffer"))
1221 (if archive-read-only
1222 (error "Archive is read-only")))
1223 (if (eq arcbuf (current-buffer))
1224 (error "An archive buffer cannot be added to itself"))
1225 (if (string= name "")
1226 (error "Archive members may not be given empty names"))
1227 (let ((func (with-current-buffer arcbuf
1228 (archive-name "add-new-member")))
1229 (membuf (current-buffer)))
1231 (with-current-buffer arcbuf
1232 (funcall func buffer-file-name membuf name))
1233 (error "Adding a new member is not supported for this archive type"))))
1234 ;; -------------------------------------------------------------------------
1235 ;;; Section: IO stuff
1237 (defun archive-write-file-member ()
1240 (message "Updating archive...")
1242 (let ((writer (with-current-buffer archive-superior-buffer
1243 (archive-name "write-file-member")))
1244 (archive (with-current-buffer archive-superior-buffer
1245 (archive-maybe-copy (buffer-file-name)))))
1246 (if (fboundp writer)
1247 (funcall writer archive archive-subfile-mode)
1248 (archive-*-write-file-member archive
1249 archive-subfile-mode
1250 (symbol-value writer)))
1251 (set-buffer-modified-p nil)
1252 (message "Updating archive...done"))
1253 (set-buffer archive-superior-buffer)
1254 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1255 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1256 ;; won't reset the coding-system of this archive member.
1257 (if (local-variable-p 'archive-member-coding-system)
1258 (setq last-coding-system-used archive-member-coding-system))
1261 (defun archive-*-write-file-member (archive descr command)
1262 (let* ((ename (aref descr 0))
1263 (tmpfile (expand-file-name ename archive-tmpdir))
1264 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1265 (default-directory (file-name-as-directory top)))
1268 (make-directory (file-name-directory tmpfile) t)
1269 ;; If the member is itself an archive, write it without
1270 ;; the dired-like listing we created.
1271 (if (eq major-mode 'archive-mode)
1272 (archive-write-file tmpfile)
1273 (write-region nil nil tmpfile nil 'nomessage))
1274 ;; basic-save-buffer needs last-coding-system-used to have
1275 ;; the value used to write the file, so save it before any
1276 ;; further processing clobbers it (we restore it in
1277 ;; archive-write-file-member, above).
1278 (setq archive-member-coding-system last-coding-system-used)
1280 ;; Set the file modes, but make sure we can read it.
1281 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1283 (encode-coding-string ename archive-file-name-coding-system))
1284 (let* ((coding-system-for-write 'no-conversion)
1285 (exitcode (apply #'call-process
1290 (append (cdr command)
1291 (list archive ename)))))
1292 (or (zerop exitcode)
1293 (error "Updating was unsuccessful (%S)" exitcode))))
1294 (archive-delete-local tmpfile))))
1296 (defun archive-write-file (&optional file)
1298 (let ((coding-system-for-write 'no-conversion))
1299 (write-region archive-proper-file-start (point-max)
1300 (or file buffer-file-name) nil t)
1301 (set-buffer-modified-p nil))
1303 ;; -------------------------------------------------------------------------
1304 ;;; Section: Marking and unmarking.
1306 (defun archive-flag-deleted (p &optional type)
1307 "In archive mode, mark this member to be deleted from the archive.
1308 With a prefix argument, mark that many files."
1310 (or type (setq type ?D))
1312 (let ((sign (if (>= p 0) +1 -1))
1313 (modified (buffer-modified-p))
1314 (inhibit-read-only t))
1315 (while (not (zerop p))
1316 (if (archive-get-descr t)
1321 (setq p (- p sign)))
1322 (restore-buffer-modified-p modified))
1323 (archive-next-line 0))
1325 (defun archive-unflag (p)
1326 "In archive mode, un-mark this member if it is marked to be deleted.
1327 With a prefix argument, un-mark that many files forward."
1329 (archive-flag-deleted p ?\s))
1331 (defun archive-unflag-backwards (p)
1332 "In archive mode, un-mark this member if it is marked to be deleted.
1333 With a prefix argument, un-mark that many members backward."
1335 (archive-flag-deleted (- p) ?\s))
1337 (defun archive-unmark-all-files ()
1340 (let ((modified (buffer-modified-p))
1341 (inhibit-read-only t))
1343 (goto-char archive-file-list-start)
1344 (while (< (point) archive-file-list-end)
1345 (or (= (following-char) ?\s)
1346 (progn (delete-char 1) (insert ?\s)))
1348 (restore-buffer-modified-p modified)))
1350 (defun archive-mark (p)
1351 "In archive mode, mark this member for group operations.
1352 With a prefix argument, mark that many members.
1353 Use \\[archive-unmark-all-files] to remove all marks."
1355 (archive-flag-deleted p ?*))
1357 (defun archive-get-marked (mark &optional default)
1360 (goto-char archive-file-list-start)
1361 (while (< (point) archive-file-list-end)
1362 (if (= (following-char) mark)
1363 (setq files (cons (archive-get-descr) files)))
1365 (or (nreverse files)
1367 (list (archive-get-descr))))))
1368 ;; -------------------------------------------------------------------------
1369 ;;; Section: Operate
1371 (defun archive-next-line (p)
1375 (forward-char archive-file-name-indent)))
1377 (defun archive-previous-line (p)
1379 (archive-next-line (- p)))
1381 (defun archive-chmod-entry (new-mode)
1382 "Change the protection bits associated with all marked or this member.
1383 The new protection bits can either be specified as an octal number or
1384 as a relative change like \"g+rw\" as for chmod(2)."
1385 (interactive "sNew mode (octal or relative): ")
1386 (if archive-read-only (error "Archive is read-only"))
1387 (let ((func (archive-name "chmod-entry")))
1390 (funcall func new-mode (archive-get-marked ?* t))
1391 (archive-resummarize))
1392 (error "Setting mode bits is not supported for this archive type"))))
1394 (defun archive-chown-entry (new-uid)
1395 "Change the owner of all marked or this member."
1396 (interactive "nNew uid: ")
1397 (if archive-read-only (error "Archive is read-only"))
1398 (let ((func (archive-name "chown-entry")))
1401 (funcall func new-uid (archive-get-marked ?* t))
1402 (archive-resummarize))
1403 (error "Setting owner is not supported for this archive type"))))
1405 (defun archive-chgrp-entry (new-gid)
1406 "Change the group of all marked or this member."
1407 (interactive "nNew gid: ")
1408 (if archive-read-only (error "Archive is read-only"))
1409 (let ((func (archive-name "chgrp-entry")))
1412 (funcall func new-gid (archive-get-marked ?* t))
1413 (archive-resummarize))
1414 (error "Setting group is not supported for this archive type"))))
1416 (defun archive-expunge ()
1417 "Do the flagged deletions."
1421 (goto-char archive-file-list-start)
1422 (while (< (point) archive-file-list-end)
1423 (if (= (following-char) ?D)
1424 (setq files (cons (aref (archive-get-descr) 0) files)))
1426 (setq files (nreverse files))
1428 (or (not archive-read-only)
1429 (error "Archive is read-only"))
1430 (or (yes-or-no-p (format "Really delete %d member%s? "
1432 (if (null (cdr files)) "" "s")))
1433 (error "Operation aborted"))
1434 (let ((archive (archive-maybe-copy (buffer-file-name)))
1435 (expunger (archive-name "expunge")))
1436 (if (fboundp expunger)
1437 (funcall expunger archive files)
1438 (archive-*-expunge archive files (symbol-value expunger)))
1439 (archive-maybe-update nil)
1441 (archive-resummarize)
1442 (revert-buffer))))))
1444 (defun archive-*-expunge (archive files command)
1445 (apply #'call-process
1450 (append (cdr command) (cons archive files))))
1452 (defun archive-rename-entry (newname)
1453 "Change the name associated with this entry in the archive file."
1454 (interactive "sNew name: ")
1455 (if archive-read-only (error "Archive is read-only"))
1456 (if (string= newname "")
1457 (error "Archive members may not be given empty names"))
1458 (let ((func (archive-name "rename-entry"))
1459 (descr (archive-get-descr)))
1463 (encode-coding-string newname
1464 archive-file-name-coding-system)
1466 (archive-resummarize))
1467 (error "Renaming is not supported for this archive type"))))
1469 ;; Revert the buffer and recompute the dired-like listing.
1470 (defun archive-mode-revert (&optional _no-auto-save _no-confirm)
1471 (let ((no (archive-get-lineno)))
1472 (setq archive-files nil)
1473 (let ((revert-buffer-function nil)
1474 (coding-system-for-read 'no-conversion))
1475 (revert-buffer t t))
1477 (goto-char archive-file-list-start)
1478 (archive-next-line no)))
1480 (defun archive-undo ()
1481 "Undo in an archive buffer.
1482 This doesn't recover lost files, it just undoes changes in the buffer itself."
1484 (let ((inhibit-read-only t))
1486 ;; -------------------------------------------------------------------------
1487 ;;; Section: Arc Archives
1489 (defun archive-arc-summarize ()
1495 (while (and (< (+ p 29) (point-max))
1496 (= (byte-after p) ?\C-z)
1497 (> (byte-after (1+ p)) 0))
1498 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1499 (fnlen (or (string-match "\0" namefld) 13))
1500 (efnname (decode-coding-string (substring namefld 0 fnlen)
1501 archive-file-name-coding-system))
1502 (csize (archive-l-e (+ p 15) 4))
1503 (moddate (archive-l-e (+ p 19) 2))
1504 (modtime (archive-l-e (+ p 21) 2))
1505 (ucsize (archive-l-e (+ p 25) 4))
1506 (fiddle (string= efnname (upcase efnname)))
1507 (ifnname (if fiddle (downcase efnname) efnname))
1508 (text (format " %8d %-11s %-8s %s"
1510 (archive-dosdate moddate)
1511 (archive-dostime modtime)
1513 (setq maxlen (max maxlen fnlen)
1514 totalsize (+ totalsize ucsize)
1515 visual (cons (vector text
1516 (- (length text) (length ifnname))
1519 files (cons (vector efnname ifnname fiddle nil (1- p))
1522 (goto-char (point-min))
1523 (let ((dash (concat "- -------- ----------- -------- "
1524 (make-string maxlen ?-)
1526 (insert "M Length Date Time File\n"
1528 (archive-summarize-files (nreverse visual))
1530 (format " %8d %d file%s"
1533 (if (= 1 (length files)) "" "s"))
1535 (apply #'vector (nreverse files))))
1537 (defun archive-arc-rename-entry (newname descr)
1538 (if (string-match "[:\\\\/]" newname)
1539 (error "File names in arc files must not contain a directory component"))
1540 (if (> (length newname) 12)
1541 (error "File names in arc files are limited to 12 characters"))
1542 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1544 (inhibit-read-only t))
1548 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1550 (insert-unibyte name)))))
1551 ;; -------------------------------------------------------------------------
1552 ;;; Section: Lzh Archives
1554 (defun archive-lzh-summarize (&optional start)
1555 (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
1560 (while (progn (goto-char p) ;beginning of a base header.
1561 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1562 (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
1563 (csize (archive-l-e (+ p 7) 4)) ;size of a compressed file to follow (level 0 and 2),
1564 ;size of extended headers + the compressed file to follow (level 1).
1565 (ucsize (archive-l-e (+ p 11) 4)) ;size of an uncompressed file.
1566 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1567 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
1568 (hdrlvl (byte-after (+ p 20))) ;header level
1569 thsize ;total header size (base + extensions)
1570 fnlen efnname osid fiddle ifnname width p2
1571 neh ;beginning of next extension header (level 1 and 2)
1572 mode modestr uid gid text dir prname
1573 gname uname modtime moddate)
1574 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1575 (when (or (= hdrlvl 0) (= hdrlvl 1))
1576 (setq fnlen (byte-after (+ p 21))) ;filename length
1577 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
1578 (decode-coding-string
1579 str archive-file-name-coding-system)))
1580 (setq p2 (+ p 22 fnlen))) ;
1582 (setq neh (+ p2 3)) ;specific to level 1 header
1584 (setq neh (+ p 24)))) ;specific to level 2 header
1585 (if neh ;if level 1 or 2 we expect extension headers to follow
1586 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
1587 (etype (byte-after (+ neh 2)))) ;extension type
1588 (while (not (= ehsize 0))
1590 ((= etype 1) ;file name
1591 (let ((i (+ neh 3)))
1592 (while (< i (+ neh ehsize))
1593 (setq efnname (concat efnname (char-to-string (byte-after i))))
1595 ((= etype 2) ;directory name
1596 (let ((i (+ neh 3)))
1597 (while (< i (+ neh ehsize))
1598 (setq dir (concat dir
1599 (if (= (byte-after i)
1605 ((= etype 80) ;Unix file permission
1606 (setq mode (archive-l-e (+ neh 3) 2)))
1607 ((= etype 81) ;UNIX file group/user ID
1608 (progn (setq uid (archive-l-e (+ neh 3) 2))
1609 (setq gid (archive-l-e (+ neh 5) 2))))
1610 ((= etype 82) ;UNIX file group name
1611 (let ((i (+ neh 3)))
1612 (while (< i (+ neh ehsize))
1613 (setq gname (concat gname (char-to-string (char-after i))))
1615 ((= etype 83) ;UNIX file user name
1616 (let ((i (+ neh 3)))
1617 (while (< i (+ neh ehsize))
1618 (setq uname (concat uname (char-to-string (char-after i))))
1621 (setq neh (+ neh ehsize))
1622 (setq ehsize (archive-l-e neh 2))
1623 (setq etype (byte-after (+ neh 2))))
1624 ;;get total header size for level 1 and 2 headers
1625 (setq thsize (- neh p))))
1626 (if (= hdrlvl 0) ;total header size
1627 (setq thsize hsize))
1628 ;; OS ID field not present in level 0 header, use code 0 "generic"
1629 ;; in that case as per lha program header.c get_header()
1630 (setq osid (cond ((= hdrlvl 0) 0)
1631 ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
1632 ((= hdrlvl 2) (char-after (+ p 23)))))
1633 ;; Filename fiddling must follow the lha program, otherwise the name
1634 ;; passed to "lha pq" etc won't match (which for an extract silently
1635 ;; results in no output). As of version 1.14i it goes from the OS ID,
1636 ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
1637 ;; converts "\" to "/".
1638 ;; - For 0 generic: generic_to_unix_filename() downcases if there's
1639 ;; no lower case already present, and converts "\" to "/".
1640 ;; - For 'm' macOS: macos_to_unix_filename() changes "/" to ":" and
1642 (setq fiddle (cond ((= ?M osid) t)
1643 ((= 0 osid) (string= efnname (upcase efnname)))))
1644 (setq ifnname (if fiddle (downcase efnname) efnname))
1645 (setq prname (if dir (concat dir ifnname) ifnname))
1646 (setq width (if prname (string-width prname) 0))
1647 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1648 (setq moddate (if (= hdrlvl 2)
1649 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1650 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1651 (setq modtime (if (= hdrlvl 2)
1652 (archive-unixtime time1 time2)
1653 (archive-dostime time1)))
1654 (setq text (if archive-alternate-display
1655 (format " %8d %5S %5S %s"
1660 (format " %10s %8d %-11s %-8s %s"
1666 (setq maxlen (max maxlen width)
1667 totalsize (+ totalsize ucsize)
1668 visual (cons (vector text
1669 (- (length text) (length prname))
1672 files (cons (vector prname ifnname fiddle mode (1- p))
1675 (setq p (+ p hsize 2 csize)))
1676 ((or (= hdrlvl 2) (= hdrlvl 0))
1677 (setq p (+ p thsize 2 csize))))
1679 (goto-char (point-min))
1680 (let ((dash (concat (if archive-alternate-display
1681 "- -------- ----- ----- "
1682 "- ---------- -------- ----------- -------- ")
1683 (make-string maxlen ?-)
1685 (header (if archive-alternate-display
1686 "M Length Uid Gid File\n"
1687 "M Filemode Length Date Time File\n"))
1688 (sumline (if archive-alternate-display
1690 " %8.0f %d file%s")))
1691 (insert header dash)
1692 (archive-summarize-files (nreverse visual))
1697 (if (= 1 (length files)) "" "s"))
1699 (apply #'vector (nreverse files))))
1701 (defconst archive-lzh-alternate-display t)
1703 (defun archive-lzh-extract (archive name)
1704 (archive-extract-by-stdout archive name archive-lzh-extract))
1706 (defun archive-lzh-resum (p count)
1709 (setq count (1- count)
1710 sum (+ sum (byte-after p))
1714 (defun archive-lzh-rename-entry (newname descr)
1718 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1719 (oldhsize (byte-after p))
1720 (oldfnlen (byte-after (+ p 21)))
1721 (newfnlen (length newname))
1722 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1723 (inhibit-read-only t))
1724 (if (> newhsize 255)
1725 (error "The file name is too long"))
1726 (goto-char (+ p 21))
1727 (delete-char (1+ oldfnlen))
1728 (insert-unibyte newfnlen newname)
1731 (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
1733 (defun archive-lzh-ogm (newval files errtxt ofs)
1738 (let* ((p (+ archive-proper-file-start (aref fil 4)))
1739 (hsize (byte-after p))
1740 (fnlen (byte-after (+ p 21)))
1742 (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
1743 (inhibit-read-only t))
1746 (or (numberp newval)
1747 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1748 (goto-char (+ p2 ofs))
1750 (insert-unibyte (logand newval 255) (ash newval -8))
1753 (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
1754 (message "Member %s does not have %s field"
1755 (aref fil 1) errtxt)))))))
1757 (defun archive-lzh-chown-entry (newuid files)
1758 (archive-lzh-ogm newuid files "an uid" 10))
1760 (defun archive-lzh-chgrp-entry (newgid files)
1761 (archive-lzh-ogm newgid files "a gid" 12))
1763 (defun archive-lzh-chmod-entry (newmode files)
1765 ;; This should work even though newmode will be dynamically accessed.
1766 (lambda (old) (archive-calc-mode old newmode t))
1767 files "a unix-style mode" 8))
1769 ;; -------------------------------------------------------------------------
1770 ;;; Section: Lzh Self-Extracting .exe Archives
1772 ;; No support for modifying these files. It looks like the lha for unix
1773 ;; program (as of version 1.14i) can't create or retain the DOS exe part.
1774 ;; If you do an "lha a" on a .exe for instance it renames and writes to a
1777 (defun archive-lzh-exe-summarize ()
1778 "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
1780 ;; Skip the initial executable code part and apply archive-lzh-summarize
1781 ;; to the archive part proper. The "-lh5-" etc regexp here for the start
1782 ;; is the same as in archive-find-type.
1784 ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
1785 ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
1786 ;; the header checksum, or level 2 a test of the "attribute" and size.
1788 (re-search-forward "..-l[hz][0-9ds]-" nil)
1789 (archive-lzh-summarize (match-beginning 0)))
1791 ;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
1793 (defalias 'archive-lzh-exe-extract 'archive-lzh-extract
1794 "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
1796 ;; -------------------------------------------------------------------------
1797 ;;; Section: Zip Archives
1799 (defun archive-zip-summarize ()
1800 (goto-char (- (point-max) (- 22 18)))
1801 (search-backward-regexp "[P]K\005\006")
1802 (let ((p (archive-l-e (+ (point) 16) 4))
1807 emacs-int-has-32bits)
1809 ;; If the offset of end-of-central-directory is -1, this is a
1810 ;; Zip64 extended ZIP file format, and we need to glean the info
1811 ;; from Zip64 records instead.
1813 ;; First, find the Zip64 end-of-central-directory locator.
1814 (search-backward "PK\006\007")
1815 (setq p (+ (point-min)
1816 (archive-l-e (+ (point) 8) 8)))
1818 ;; We should be at Zip64 end-of-central-directory record now.
1819 (or (string= "PK\006\006" (buffer-substring p (+ p 4)))
1820 (error "Unrecognized ZIP file format"))
1821 ;; Offset to central directory:
1822 (setq p (archive-l-e (+ p 48) 8)))
1823 (setq p (+ p (point-min)))
1824 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1825 (let* ((creator (byte-after (+ p 5)))
1826 ;; (method (archive-l-e (+ p 10) 2))
1827 (modtime (archive-l-e (+ p 12) 2))
1828 (moddate (archive-l-e (+ p 14) 2))
1829 (ucsize (archive-l-e (+ p 24) 4))
1830 (fnlen (archive-l-e (+ p 28) 2))
1831 (exlen (archive-l-e (+ p 30) 2))
1832 (fclen (archive-l-e (+ p 32) 2))
1833 (lheader (archive-l-e (+ p 42) 4))
1834 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1835 (decode-coding-string
1836 str archive-file-name-coding-system)))
1837 (isdir (and (= ucsize 0)
1838 (string= (file-name-nondirectory efnname) "")))
1839 (mode (cond ((memq creator '(2 3)) ; Unix
1840 (archive-l-e (+ p 40) 2))
1841 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1843 (if isdir (logior 16384 ?\111) 0)
1845 (logand 1 (byte-after (+ p 38))))
1848 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1849 (fiddle (and archive-zip-case-fiddle
1850 (not (not (memq creator '(0 2 4 5 9))))
1851 (string= (upcase efnname) efnname)))
1852 (ifnname (if fiddle (downcase efnname) efnname))
1853 (width (string-width ifnname))
1854 (text (format " %10s %8d %-11s %-8s %s"
1857 (archive-dosdate moddate)
1858 (archive-dostime modtime)
1860 (setq maxlen (max maxlen width)
1861 totalsize (+ totalsize ucsize)
1862 visual (cons (vector text
1863 (- (length text) (length ifnname))
1866 files (cons (if isdir
1868 (vector efnname ifnname fiddle mode
1869 (list (1- p) lheader)))
1871 p (+ p 46 fnlen exlen fclen))))
1872 (goto-char (point-min))
1873 (let ((dash (concat "- ---------- -------- ----------- -------- "
1874 (make-string maxlen ?-)
1876 (insert "M Filemode Length Date Time File\n"
1878 (archive-summarize-files (nreverse visual))
1880 (format " %8d %d file%s"
1883 (if (= 1 (length files)) "" "s"))
1885 (apply #'vector (nreverse files))))
1887 (defun archive-zip-extract (archive name)
1889 ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
1890 (archive-*-extract archive name archive-zip-extract))
1891 ((equal (car archive-zip-extract) archive-7z-program)
1892 (let ((archive-7z-extract archive-zip-extract))
1893 (archive-7z-extract archive name)))
1895 (archive-extract-by-stdout
1897 ;; unzip expands wildcards in NAME, so we need to quote it. But
1898 ;; not on DOS/Windows, since that fails extraction on those
1899 ;; systems (unless w32-quote-process-args is nil), and file names
1900 ;; with wildcards in zip archives don't work there anyway.
1901 ;; FIXME: Does pkunzip need similar treatment?
1902 (if (and (or (not (memq system-type '(windows-nt ms-dos)))
1903 (and (boundp 'w32-quote-process-args)
1904 (null w32-quote-process-args)))
1905 (equal (car archive-zip-extract) "unzip"))
1906 (shell-quote-argument name)
1908 archive-zip-extract))))
1910 (defun archive-zip-write-file-member (archive descr)
1911 (archive-*-write-file-member
1914 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1916 (defun archive-zip-chmod-entry (newmode files)
1921 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
1922 (creator (byte-after (+ p 5)))
1923 (oldmode (aref fil 3))
1924 (newval (archive-calc-mode oldmode newmode t))
1925 (inhibit-read-only t))
1926 (cond ((memq creator '(2 3)) ; Unix
1927 (goto-char (+ p 40))
1929 (insert-unibyte (logand newval 255) (ash newval -8)))
1930 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1931 (goto-char (+ p 38))
1932 (insert-unibyte (logior (logand (byte-after (point)) 254)
1933 (logand (logxor 1 (ash newval -7)) 1)))
1935 (t (message "Don't know how to change mode for this member"))))
1937 ;; -------------------------------------------------------------------------
1938 ;;; Section: Zoo Archives
1940 (defun archive-zoo-summarize ()
1941 (let ((p (1+ (archive-l-e 25 4)))
1946 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1947 (> (archive-l-e (+ p 6) 4) 0))
1948 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1949 (moddate (archive-l-e (+ p 14) 2))
1950 (modtime (archive-l-e (+ p 16) 2))
1951 (ucsize (archive-l-e (+ p 20) 4))
1952 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1953 (dirtype (byte-after (+ p 4)))
1954 (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
1955 (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
1956 (fnlen (or (string-match "\0" namefld) 13))
1960 (concat (buffer-substring
1962 (+ p 58 lfnlen ldirlen -1))
1966 (buffer-substring (+ p 58)
1968 (substring namefld 0 fnlen)))))
1969 (decode-coding-string
1970 str archive-file-name-coding-system)))
1971 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1972 (ifnname (if fiddle (downcase efnname) efnname))
1973 (width (string-width ifnname))
1974 (text (format " %8d %-11s %-8s %s"
1976 (archive-dosdate moddate)
1977 (archive-dostime modtime)
1979 (setq maxlen (max maxlen width)
1980 totalsize (+ totalsize ucsize)
1981 visual (cons (vector text
1982 (- (length text) (length ifnname))
1985 files (cons (vector efnname ifnname fiddle nil (1- p))
1988 (goto-char (point-min))
1989 (let ((dash (concat "- -------- ----------- -------- "
1990 (make-string maxlen ?-)
1992 (insert "M Length Date Time File\n"
1994 (archive-summarize-files (nreverse visual))
1996 (format " %8d %d file%s"
1999 (if (= 1 (length files)) "" "s"))
2001 (apply #'vector (nreverse files))))
2003 (defun archive-zoo-extract (archive name)
2004 (archive-extract-by-stdout archive name archive-zoo-extract))
2006 ;; -------------------------------------------------------------------------
2007 ;;; Section: Rar Archives
2009 (defun archive-rar-summarize (&optional file)
2010 ;; File is used internally for `archive-rar-exe-summarize'.
2011 (unless file (setq file buffer-file-name))
2012 (let* ((copy (file-local-copy file))
2017 (call-process "lsar" nil t nil "-l" (or file copy))
2018 (if copy (delete-file copy))
2019 (goto-char (point-min))
2020 (re-search-forward "^\\(\s+=+\s?+\\)+\n")
2021 (while (looking-at (concat "^\s+[0-9.]+\s+D?-+\s+" ; Flags
2022 "\\([0-9-]+\\)\s+" ; Size
2023 "\\([-0-9.%]+\\|-+\\)\s+" ; Ratio
2024 "\\([0-9a-zA-Z]+\\)\s+" ; Mode
2025 "\\([0-9-]+\\)\s+" ; Date
2026 "\\([0-9:]+\\)\s+" ; Time
2029 (goto-char (match-end 0))
2030 (let ((name (match-string 6))
2031 (size (match-string 1)))
2032 (if (> (length name) maxname) (setq maxname (length name)))
2033 (if (> (length size) maxsize) (setq maxsize (length size)))
2034 (push (vector name name nil nil
2036 size (match-string 2)
2038 (match-string 4) (match-string 5))
2040 (setq files (nreverse files))
2041 (goto-char (point-min))
2042 (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
2043 (sep (format format "----------" "-----" (make-string maxsize ?-)
2045 (column (length sep)))
2046 (insert (format format " Date " "Time " "Size" "Ratio" "Filename") "\n")
2047 (insert sep (make-string maxname ?-) "\n")
2048 (archive-summarize-files (mapcar (lambda (desc)
2060 (insert sep (make-string maxname ?-) "\n")
2061 (apply #'vector files))))
2063 (defun archive-rar-extract (archive name)
2064 ;; unrar-free seems to have no way to extract to stdout or even to a file.
2065 (if (file-name-absolute-p name)
2066 ;; The code below assumes the name is relative and may do undesirable
2067 ;; things otherwise.
2068 (error "Can't extract files with non-relative names")
2069 (archive-extract-by-file archive name `("unar" "-no-directory" "-o") "Successfully extracted")))
2071 ;;; Section: Rar self-extracting .exe archives.
2073 (defun archive-rar-exe-summarize ()
2074 (let ((tmpfile (make-temp-file "rarexe")))
2077 (goto-char (point-min))
2078 (re-search-forward "Rar!")
2079 (write-region (match-beginning 0) (point-max) tmpfile)
2080 (archive-rar-summarize tmpfile))
2081 (delete-file tmpfile))))
2083 (defun archive-rar-exe-extract (archive name)
2084 (let* ((tmpfile (make-temp-file "rarexe"))
2085 (buf (find-buffer-visiting archive))
2086 (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
2089 (with-current-buffer (or buf tmpbuf)
2093 ;; point-max unwidened is assumed to be the end of the
2094 ;; summary text and the beginning of the actual file data.
2095 (progn (goto-char (point-max)) (widen))
2096 (insert-file-contents-literally archive)
2097 (goto-char (point-min)))
2098 (re-search-forward "Rar!")
2099 (write-region (match-beginning 0) (point-max) tmpfile))))
2100 (archive-rar-extract tmpfile name))
2101 (if tmpbuf (kill-buffer tmpbuf))
2102 (delete-file tmpfile))))
2104 ;; -------------------------------------------------------------------------
2105 ;;; Section: 7z Archives
2107 (defun archive-7z-summarize ()
2110 (file buffer-file-name)
2113 (call-process archive-7z-program nil t nil "l" "-slt" file)
2114 (goto-char (point-min))
2115 ;; Four dashes start the meta info section that should be skipped.
2116 ;; Archive members start with more than four dashes.
2117 (re-search-forward "^-----+\n")
2118 (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
2119 (goto-char (match-end 0))
2120 (let ((name (match-string 1))
2121 (size (save-excursion
2122 (and (re-search-forward "^Size = \\(.*\\)\n")
2124 (time (save-excursion
2125 (and (re-search-forward "^Modified = \\(.*\\)\n")
2126 (match-string 1)))))
2127 (if (> (length name) maxname) (setq maxname (length name)))
2128 (if (> (length size) maxsize) (setq maxsize (length size)))
2129 (push (vector name name nil nil time nil nil size)
2131 (setq files (nreverse files))
2132 (goto-char (point-min))
2133 (let* ((format (format " %%%ds %%s %%s" maxsize))
2134 (sep (format format (make-string maxsize ?-) "-------------------" ""))
2135 (column (length sep)))
2136 (insert (format format "Size " "Date Time " " Filename") "\n")
2137 (insert sep (make-string maxname ?-) "\n")
2138 (archive-summarize-files (mapcar (lambda (desc)
2148 (insert sep (make-string maxname ?-) "\n")
2149 (apply #'vector files))))
2151 (defun archive-7z-extract (archive name)
2152 ;; 7z doesn't provide a `quiet' option to suppress non-essential
2153 ;; stderr messages. So redirect stderr to a temp file and display it
2154 ;; in the echo area when it contains no message indicating success.
2155 (archive-extract-by-stdout
2156 archive name archive-7z-extract "Everything is Ok"))
2158 (defun archive-7z-write-file-member (archive descr)
2159 (archive-*-write-file-member
2164 ;; -------------------------------------------------------------------------
2165 ;;; Section `ar' archives.
2167 ;; TODO: we currently only handle the basic format of ar archives,
2168 ;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
2169 ;; for .deb packages.
2171 (autoload 'tar-grind-file-mode "tar-mode")
2173 (defconst archive-ar-file-header-re
2174 "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
2176 (defun archive-ar-summarize ()
2177 ;; File is used internally for `archive-rar-exe-summarize'.
2185 (goto-char (point-min))
2186 (search-forward "!<arch>\n")
2187 (while (looking-at archive-ar-file-header-re)
2188 (let ((name (match-string 1))
2190 (time (string-to-number (match-string 2)))
2191 (user (match-string 3))
2192 (group (match-string 4))
2193 (mode (string-to-number (match-string 5) 8))
2194 (size (string-to-number (match-string 6))))
2195 ;; Move to the beginning of the data.
2196 (goto-char (match-end 0))
2197 (setq time (format-time-string "%Y-%m-%d %H:%M" time))
2199 (cond ((equal name "// ")
2200 (propertize ".<ExtNamesTable>." 'face 'italic))
2202 (propertize ".<LookupTable>." 'face 'italic))
2203 ((string-match "/? *\\'" name)
2204 (substring name 0 (match-beginning 0)))))
2205 (setq user (substring user 0 (string-match " +\\'" user)))
2206 (setq group (substring group 0 (string-match " +\\'" group)))
2207 (setq mode (tar-grind-file-mode mode))
2208 ;; Move to the end of the data.
2209 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1))
2210 (setq size (number-to-string size))
2211 (if (> (length name) maxname) (setq maxname (length name)))
2212 (if (> (length time) maxtime) (setq maxtime (length time)))
2213 (if (> (length user) maxuser) (setq maxuser (length user)))
2214 (if (> (length group) maxgroup) (setq maxgroup (length group)))
2215 (if (> (length mode) maxmode) (setq maxmode (length mode)))
2216 (if (> (length size) maxsize) (setq maxsize (length size)))
2217 (push (vector name extname nil mode
2218 time user group size)
2220 (setq files (nreverse files))
2221 (goto-char (point-min))
2222 (let* ((format (format "%%%ds %%%ds/%%-%ds %%%ds %%%ds %%s"
2223 maxmode maxuser maxgroup maxsize maxtime))
2224 (sep (format format (make-string maxmode ?-)
2225 (make-string maxuser ?-)
2226 (make-string maxgroup ?-)
2227 (make-string maxsize ?-)
2228 (make-string maxtime ?-) ""))
2229 (column (length sep)))
2230 (insert (format format " Mode " "User" "Group" " Size "
2231 " Date " "Filename")
2233 (insert sep (make-string maxname ?-) "\n")
2234 (archive-summarize-files (mapcar (lambda (desc)
2247 (insert sep (make-string maxname ?-) "\n")
2248 (apply #'vector files))))
2250 (defun archive-ar-extract (archive name)
2251 (let ((destbuf (current-buffer))
2252 (archivebuf (find-file-noselect archive))
2254 (with-current-buffer archivebuf
2256 ;; We may be in archive-mode or not, so either with or without
2257 ;; narrowing and with or without a prepended summary.
2260 (search-forward "!<arch>\n")
2261 (while (and (not from) (looking-at archive-ar-file-header-re))
2262 (let ((this (match-string 1)))
2263 (setq size (string-to-number (match-string 6)))
2264 (goto-char (match-end 0))
2265 (if (equal name this)
2267 ;; Move to the end of the data.
2268 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1)))))
2270 (set-buffer-multibyte nil)
2271 (with-current-buffer destbuf
2272 ;; Do it within the `widen'.
2273 (insert-buffer-substring archivebuf from (+ from size)))
2274 (set-buffer-multibyte 'to)
2275 ;; Inform the caller that the call succeeded.
2278 ;; -------------------------------------------------------------------------
2279 ;; This line was a mistake; it is kept now for compatibility.
2281 (provide 'archive-mode)
2285 ;;; arc-mode.el ends here