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 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 2048 mode)) ?- ?S)
563 (if (zerop (logand 2048 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 1024 mode)) ?- ?S)
568 (if (zerop (logand 1024 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 (set-buffer-multibyte 'to)
752 (archive-summarize nil)
753 (setq buffer-read-only t)
754 (when (and archive-visit-single-files
755 auto-compression-mode
756 (= (length archive-files) 1))
757 (rename-buffer (concat " " (buffer-name)))
758 (archive-extract)))))
760 ;; Archive mode is suitable only for specially formatted data.
761 (put 'archive-mode 'mode-class 'special)
763 (let ((item1 '(archive-subfile-mode " Archive")))
764 (or (member item1 minor-mode-alist)
765 (setq minor-mode-alist (cons item1 minor-mode-alist))))
766 ;; -------------------------------------------------------------------------
767 (defun archive-find-type ()
769 (goto-char (point-min))
770 ;; The funny [] here make it unlikely that the .elc file will be treated
771 ;; as an archive by other software.
772 (let (case-fold-search)
773 (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
774 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
775 ((looking-at "....................[\334]\247\304\375") 'zoo)
776 ((and (looking-at "\C-z") ; signature too simple, IMHO
777 (string-match "\\.[aA][rR][cC]\\'"
778 (or buffer-file-name (buffer-name))))
780 ;; This pattern modeled on the BSD/GNU+Linux `file' command.
781 ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
782 ;; Note this regexp is also in archive-exe-p.
783 ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
784 ((looking-at "Rar!") 'rar)
785 ((looking-at "!<arch>\n") 'ar)
786 ((and (looking-at "MZ")
787 (re-search-forward "Rar!" (+ (point) 100000) t))
789 ((looking-at "7z\274\257\047\034") '7z)
790 (t (error "Buffer format not recognized")))))
791 ;; -------------------------------------------------------------------------
793 (defun archive-desummarize ()
794 (let ((inhibit-read-only t)
795 (modified (buffer-modified-p)))
797 (delete-region (point-min) archive-proper-file-start)
798 (restore-buffer-modified-p modified)))
801 (defun archive-summarize (&optional shut-up)
802 "Parse the contents of the archive file in the current buffer.
803 Place a dired-like listing on the front;
804 then narrow to it, so that only that listing
805 is visible (and the real data of the buffer is hidden).
806 Optional argument SHUT-UP, if non-nil, means don't print messages
807 when parsing the archive."
809 (let ((create-lockfiles nil) ; avoid changing dir mtime by lock_file
810 (inhibit-read-only t))
811 (setq archive-proper-file-start (copy-marker (point-min) t))
812 (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
814 (message "Parsing archive file..."))
815 (buffer-disable-undo (current-buffer))
816 (setq archive-files (funcall (archive-name "summarize")))
818 (message "Parsing archive file...done."))
819 (setq archive-proper-file-start (point-marker))
820 (narrow-to-region (point-min) (point))
821 (set-buffer-modified-p nil)
822 (buffer-enable-undo))
823 (goto-char archive-file-list-start)
824 (archive-next-line 0))
826 (defun archive-resummarize ()
827 "Recreate the contents listing of an archive."
828 (let ((no (archive-get-lineno)))
829 (archive-desummarize)
830 (archive-summarize t)
831 (goto-char archive-file-list-start)
832 (archive-next-line no)))
834 (defun archive-summarize-files (files)
835 "Insert a description of a list of files annotated with proper mouse face."
836 (setq archive-file-list-start (point-marker))
837 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
838 ;; We don't want to do an insert for each element since that takes too
839 ;; long when the archive -- which has to be moved in memory -- is large.
845 ;; Using `concat' here copies the text also, so we can add
846 ;; properties without problems.
847 (let ((text (concat (aref fil 0) "\n")))
848 (if (featurep 'xemacs)
851 (aref fil 1) (aref fil 2)
852 '(mouse-face highlight
853 help-echo "mouse-2: extract this file into a buffer")
857 (setq archive-file-list-end (point-marker)))
859 (defun archive-alternate-display ()
860 "Toggle alternative display.
861 To avoid very long lines archive mode does not show all information.
862 This function changes the set of information shown for each files."
864 (setq archive-alternate-display (not archive-alternate-display))
865 (archive-resummarize))
866 ;; -------------------------------------------------------------------------
867 ;;; Section: Local archive copy handling
869 (defun archive-unique-fname (fname dir)
870 "Make sure a file FNAME can be created uniquely in directory DIR.
872 If FNAME can be uniquely created in DIR, it is returned unaltered.
873 If FNAME is something our underlying filesystem can't grok, or if another
874 file by that name already exists in DIR, a unique new name is generated
875 using `make-temp-file', and the generated name is returned."
876 (let ((fullname (expand-file-name fname dir))
877 (alien (string-match file-name-invalid-regexp fname))
880 (if (if (fboundp 'msdos-long-file-names)
881 (not (msdos-long-file-names)))
885 (if (or alien (file-exists-p fullname))
887 ;; Make sure all the leading directories in
888 ;; archive-local-name exist under archive-tmpdir, so that
889 ;; the directory structure recorded in the archive is
890 ;; reconstructed in the temporary directory.
891 (make-directory (file-name-directory tmpfile) t)
892 (make-temp-file tmpfile))
893 ;; Make sure all the leading directories in `fullname' exist
894 ;; under archive-tmpdir. This is necessary for nested archives
895 ;; (`archive-extract' sets `archive-remote' to t in case
896 ;; an archive occurs inside another archive).
897 (make-directory (file-name-directory fullname) t)
900 (defun archive-maybe-copy (archive)
901 (let ((coding-system-for-write 'no-conversion))
903 (let ((start (point-max))
904 ;; Sometimes ARCHIVE is invalid while its actual name, as
905 ;; recorded in its parent archive, is not. For example, an
906 ;; archive bar.zip inside another archive foo.zip gets a name
907 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
908 ;; So use the actual name if available.
910 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
912 (setq archive-local-name
913 (archive-unique-fname archive-name archive-tmpdir))
916 (write-region start (point-max) archive-local-name nil 'nomessage))
918 (if (buffer-modified-p) (save-buffer))
921 (defun archive-maybe-update (unchanged)
923 (let ((name archive-local-name)
924 (modified (buffer-modified-p))
925 (coding-system-for-read 'no-conversion)
926 (lno (archive-get-lineno))
927 (inhibit-read-only t))
929 (setq archive-files nil)
931 (insert-file-contents name)
933 (goto-char archive-file-list-start)
934 (archive-next-line lno))
935 (archive-delete-local name)
938 "Buffer `%s' must be saved for changes to take effect"
939 (buffer-name (current-buffer))))
940 (set-buffer-modified-p (or modified (not unchanged))))))
942 (defun archive-delete-local (name)
943 "Delete file NAME and its parents up to and including `archive-tmpdir'."
945 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
950 (setq name (directory-file-name (file-name-directory name)))
952 (delete-directory name)
954 (if (string= name top) (setq again nil)))))
955 ;; -------------------------------------------------------------------------
956 ;;; Section: Member extraction
958 (defun archive-try-jka-compr ()
959 (when (and auto-compression-mode
960 (jka-compr-get-compression-info buffer-file-name))
961 (let* ((basename (file-name-nondirectory buffer-file-name))
962 (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
963 (match-string 1 basename) basename))
964 (tmpfile (make-temp-file (file-name-sans-extension tmpname)
966 (file-name-extension tmpname 'period))))
969 (let ((coding-system-for-write 'no-conversion)
970 ;; Don't re-compress this data just before decompressing it.
971 (jka-compr-inhibit t))
972 (write-region (point-min) (point-max) tmpfile nil 'quiet))
974 (let ((coding-system-for-read 'no-conversion))
975 (insert-file-contents tmpfile)))
976 (delete-file tmpfile)))))
978 (defun archive-file-name-handler (op &rest args)
979 (or (eq op 'file-exists-p)
980 (let ((file-name-handler-alist nil))
983 (defun archive-set-buffer-as-visiting-file (filename)
984 "Set the current buffer as if it were visiting FILENAME."
986 (goto-char (point-min))
987 (let ((buffer-undo-list t)
989 (or coding-system-for-read
990 (and set-auto-coding-function
992 (funcall set-auto-coding-function
993 filename (- (point-max) (point-min)))))
994 ;; The following let-binding of file-name-handler-alist forces
995 ;; find-file-not-found-set-buffer-file-coding-system to ignore
996 ;; the file's name (see dos-w32.el).
997 (let ((file-name-handler-alist
998 '(("" . archive-file-name-handler))))
999 (car (find-operation-coding-system
1000 'insert-file-contents
1001 (cons filename (current-buffer)) t))))))
1002 (unless (or coding-system-for-read
1003 enable-multibyte-characters)
1005 (coding-system-change-text-conversion coding 'raw-text)))
1006 (unless (memq coding '(nil no-conversion))
1007 (decode-coding-region (point-min) (point-max) coding)
1008 (setq last-coding-system-used coding))
1009 (set-buffer-modified-p nil)
1010 (kill-local-variable 'buffer-file-coding-system)
1011 (after-insert-file-set-coding (- (point-max) (point-min))))))
1013 (define-obsolete-function-alias 'archive-mouse-extract 'archive-extract "22.1")
1015 (defun archive-extract (&optional other-window-p event)
1016 "In archive mode, extract this entry of the archive into its own buffer."
1017 (interactive (list nil last-input-event))
1018 (if event (posn-set-point (event-end event)))
1019 (let* ((view-p (eq other-window-p 'view))
1020 (descr (archive-get-descr))
1021 (ename (aref descr 0))
1022 (iname (aref descr 1))
1023 (archive-buffer (current-buffer))
1024 (arcdir default-directory)
1025 (archive (buffer-file-name))
1026 (arcname (file-name-nondirectory archive))
1027 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
1028 (extractor (archive-name "extract"))
1029 ;; Members with file names which aren't valid for the
1030 ;; underlying filesystem, are treated as read-only.
1031 (read-only-p (or archive-read-only
1033 (string-match file-name-invalid-regexp ename)))
1034 (arcfilename (expand-file-name (concat arcname ":" iname)))
1035 (buffer (get-buffer bufname))
1037 (file-name-coding archive-file-name-coding-system))
1039 (string= (buffer-file-name buffer) arcfilename))
1041 (setq archive (archive-maybe-copy archive))
1042 (setq bufname (generate-new-buffer-name bufname))
1043 (setq buffer (get-buffer-create bufname))
1044 (setq just-created t)
1045 (with-current-buffer buffer
1046 (setq buffer-file-name arcfilename)
1047 (setq buffer-file-truename
1048 (abbreviate-file-name buffer-file-name))
1049 ;; Set the default-directory to the dir of the superior buffer.
1050 (setq default-directory arcdir)
1051 (make-local-variable 'archive-superior-buffer)
1052 (setq archive-superior-buffer archive-buffer)
1053 (add-hook 'write-file-functions #'archive-write-file-member nil t)
1054 (setq archive-subfile-mode descr)
1055 (setq archive-file-name-coding-system file-name-coding)
1058 (let (;; We may have to encode the file name argument for
1059 ;; external programs.
1060 (coding-system-for-write
1061 (and enable-multibyte-characters
1062 archive-file-name-coding-system))
1063 ;; We read an archive member by no-conversion at
1064 ;; first, then decode appropriately by calling
1065 ;; archive-set-buffer-as-visiting-file later.
1066 (coding-system-for-read 'no-conversion)
1067 ;; Avoid changing dir mtime by lock_file
1068 (create-lockfiles nil))
1070 (if (fboundp extractor)
1071 (funcall extractor archive ename)
1072 (archive-*-extract archive ename
1073 (symbol-value extractor)))
1075 (ding (message "%s" (error-message-string err)))
1079 (set-buffer-modified-p nil)
1080 (kill-buffer buffer))
1081 (archive-try-jka-compr) ;Pretty ugly hack :-(
1082 (archive-set-buffer-as-visiting-file ename)
1083 (goto-char (point-min))
1084 (rename-buffer bufname)
1085 (setq buffer-read-only read-only-p)
1086 (setq buffer-undo-list nil)
1087 (set-buffer-modified-p nil)
1088 (setq buffer-saved-size (buffer-size))
1090 ;; Just in case an archive occurs inside another archive.
1091 (when (derived-mode-p 'archive-mode)
1092 (setq archive-remote t)
1093 (if read-only-p (setq archive-read-only t))
1094 ;; We will write out the archive ourselves if it is
1095 ;; part of another archive.
1096 (remove-hook 'write-contents-functions #'archive-write-file t))
1097 (run-hooks 'archive-extract-hook)
1098 (if archive-read-only
1099 (message "Note: altering this archive is not implemented."))))
1100 (archive-maybe-update t))
1101 (or (not (buffer-name buffer))
1104 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
1105 ((eq other-window-p 'display) (display-buffer buffer))
1106 (other-window-p (switch-to-buffer-other-window buffer))
1107 (t (switch-to-buffer buffer))))))
1109 (defun archive-*-extract (archive name command)
1110 (let* ((default-directory (file-name-as-directory archive-tmpdir))
1111 (tmpfile (expand-file-name (file-name-nondirectory name)
1113 exit-status success)
1114 (make-directory (directory-file-name default-directory) t)
1116 (apply #'call-process
1121 (append (cdr command) (list archive name))))
1122 (cond ((and (numberp exit-status) (zerop exit-status))
1123 (if (not (file-exists-p tmpfile))
1124 (ding (message "`%s': no such file or directory" tmpfile))
1125 (insert-file-contents tmpfile)
1127 ((numberp exit-status)
1129 (message "`%s' exited with status %d" (car command) exit-status)))
1130 ((stringp exit-status)
1131 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1133 (ding (message "`%s' failed" (car command)))))
1134 (archive-delete-local tmpfile)
1137 (defun archive-extract-by-stdout (archive name command &optional stderr-test)
1138 (let ((stderr-file (make-temp-file "arc-stderr")))
1141 (apply #'call-process
1144 (if stderr-file (list t stderr-file) t)
1146 (append (cdr command) (list archive name)))
1148 (insert-file-contents stderr-file)
1149 (goto-char (point-min))
1150 (when (if (stringp stderr-test)
1151 (not (re-search-forward stderr-test nil t))
1152 (> (buffer-size) 0))
1153 (message "%s" (buffer-string)))))
1154 (if (file-exists-p stderr-file)
1155 (delete-file stderr-file)))))
1157 (defun archive-extract-by-file (archive name command &optional stdout-test)
1158 (let ((dest (make-temp-file "arc-dir" 'dir))
1159 (stdout-file (make-temp-file "arc-stdout")))
1162 (apply #'call-process
1165 `(:file ,stdout-file)
1167 `(,archive ,name ,@(cdr command) ,dest))
1169 (insert-file-contents stdout-file)
1170 (goto-char (point-min))
1171 (when (if (stringp stdout-test)
1172 (not (re-search-forward stdout-test nil t))
1173 (> (buffer-size) 0))
1174 (message "%s" (buffer-string))))
1175 (if (file-exists-p (expand-file-name name dest))
1176 (insert-file-contents-literally (expand-file-name name dest))))
1177 (if (file-exists-p stdout-file)
1178 (delete-file stdout-file))
1179 (if (file-exists-p (expand-file-name name dest))
1180 (delete-file (expand-file-name name dest)))
1181 (while (file-name-directory name)
1182 (setq name (directory-file-name (file-name-directory name)))
1183 (when (file-directory-p (expand-file-name name dest))
1184 (delete-directory (expand-file-name name dest))))
1185 (when (file-directory-p dest)
1186 (delete-directory dest)))))
1188 (defun archive-extract-other-window ()
1189 "In archive mode, find this member in another window."
1191 (archive-extract t))
1193 (defun archive-display-other-window ()
1194 "In archive mode, display this member in another window."
1196 (archive-extract 'display))
1198 (defun archive-view ()
1199 "In archive mode, view the member on this line."
1201 (archive-extract 'view))
1203 (defun archive-add-new-member (arcbuf name)
1204 "Add current buffer to the archive in ARCBUF naming it NAME."
1207 (read-buffer "Buffer containing archive: "
1208 ;; Find first archive buffer and suggest that
1209 (let ((bufs (buffer-list)))
1211 (not (with-current-buffer (car bufs)
1212 (derived-mode-p 'archive-mode))))
1213 (setq bufs (cdr bufs)))
1216 (error "There are no archive buffers")))
1218 (read-string "File name in archive: "
1219 (if buffer-file-name
1220 (file-name-nondirectory buffer-file-name)
1222 (with-current-buffer arcbuf
1223 (or (derived-mode-p 'archive-mode)
1224 (error "Buffer is not an archive buffer"))
1225 (if archive-read-only
1226 (error "Archive is read-only")))
1227 (if (eq arcbuf (current-buffer))
1228 (error "An archive buffer cannot be added to itself"))
1229 (if (string= name "")
1230 (error "Archive members may not be given empty names"))
1231 (let ((func (with-current-buffer arcbuf
1232 (archive-name "add-new-member")))
1233 (membuf (current-buffer)))
1235 (with-current-buffer arcbuf
1236 (funcall func buffer-file-name membuf name))
1237 (error "Adding a new member is not supported for this archive type"))))
1238 ;; -------------------------------------------------------------------------
1239 ;;; Section: IO stuff
1241 (defun archive-write-file-member ()
1244 (message "Updating archive...")
1246 (let ((writer (with-current-buffer archive-superior-buffer
1247 (archive-name "write-file-member")))
1248 (archive (with-current-buffer archive-superior-buffer
1249 (archive-maybe-copy (buffer-file-name)))))
1250 (if (fboundp writer)
1251 (funcall writer archive archive-subfile-mode)
1252 (archive-*-write-file-member archive
1253 archive-subfile-mode
1254 (symbol-value writer)))
1255 (set-buffer-modified-p nil)
1256 (message "Updating archive...done"))
1257 (set-buffer archive-superior-buffer)
1258 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1259 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1260 ;; won't reset the coding-system of this archive member.
1261 (if (local-variable-p 'archive-member-coding-system)
1262 (setq last-coding-system-used archive-member-coding-system))
1265 (defun archive-*-write-file-member (archive descr command)
1266 (let* ((ename (aref descr 0))
1267 (tmpfile (expand-file-name ename archive-tmpdir))
1268 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1269 (default-directory (file-name-as-directory top)))
1272 (make-directory (file-name-directory tmpfile) t)
1273 ;; If the member is itself an archive, write it without
1274 ;; the dired-like listing we created.
1275 (if (eq major-mode 'archive-mode)
1276 (archive-write-file tmpfile)
1277 (write-region nil nil tmpfile nil 'nomessage))
1278 ;; basic-save-buffer needs last-coding-system-used to have
1279 ;; the value used to write the file, so save it before any
1280 ;; further processing clobbers it (we restore it in
1281 ;; archive-write-file-member, above).
1282 (setq archive-member-coding-system last-coding-system-used)
1284 ;; Set the file modes, but make sure we can read it.
1285 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1287 (encode-coding-string ename archive-file-name-coding-system))
1288 (let* ((coding-system-for-write 'no-conversion)
1289 (exitcode (apply #'call-process
1294 (append (cdr command)
1295 (list archive ename)))))
1296 (or (zerop exitcode)
1297 (error "Updating was unsuccessful (%S)" exitcode))))
1298 (archive-delete-local tmpfile))))
1300 (defun archive-write-file (&optional file)
1302 (let ((coding-system-for-write 'no-conversion))
1303 (write-region archive-proper-file-start (point-max)
1304 (or file buffer-file-name) nil t)
1305 (set-buffer-modified-p nil))
1307 ;; -------------------------------------------------------------------------
1308 ;;; Section: Marking and unmarking.
1310 (defun archive-flag-deleted (p &optional type)
1311 "In archive mode, mark this member to be deleted from the archive.
1312 With a prefix argument, mark that many files."
1314 (or type (setq type ?D))
1316 (let ((sign (if (>= p 0) +1 -1))
1317 (modified (buffer-modified-p))
1318 (inhibit-read-only t))
1319 (while (not (zerop p))
1320 (if (archive-get-descr t)
1325 (setq p (- p sign)))
1326 (restore-buffer-modified-p modified))
1327 (archive-next-line 0))
1329 (defun archive-unflag (p)
1330 "In archive mode, un-mark this member if it is marked to be deleted.
1331 With a prefix argument, un-mark that many files forward."
1333 (archive-flag-deleted p ?\s))
1335 (defun archive-unflag-backwards (p)
1336 "In archive mode, un-mark this member if it is marked to be deleted.
1337 With a prefix argument, un-mark that many members backward."
1339 (archive-flag-deleted (- p) ?\s))
1341 (defun archive-unmark-all-files ()
1344 (let ((modified (buffer-modified-p))
1345 (inhibit-read-only t))
1347 (goto-char archive-file-list-start)
1348 (while (< (point) archive-file-list-end)
1349 (or (= (following-char) ?\s)
1350 (progn (delete-char 1) (insert ?\s)))
1352 (restore-buffer-modified-p modified)))
1354 (defun archive-mark (p)
1355 "In archive mode, mark this member for group operations.
1356 With a prefix argument, mark that many members.
1357 Use \\[archive-unmark-all-files] to remove all marks."
1359 (archive-flag-deleted p ?*))
1361 (defun archive-get-marked (mark &optional default)
1364 (goto-char archive-file-list-start)
1365 (while (< (point) archive-file-list-end)
1366 (if (= (following-char) mark)
1367 (setq files (cons (archive-get-descr) files)))
1369 (or (nreverse files)
1371 (list (archive-get-descr))))))
1372 ;; -------------------------------------------------------------------------
1373 ;;; Section: Operate
1375 (defun archive-next-line (p)
1379 (forward-char archive-file-name-indent)))
1381 (defun archive-previous-line (p)
1383 (archive-next-line (- p)))
1385 (defun archive-chmod-entry (new-mode)
1386 "Change the protection bits associated with all marked or this member.
1387 The new protection bits can either be specified as an octal number or
1388 as a relative change like \"g+rw\" as for chmod(2)."
1389 (interactive "sNew mode (octal or relative): ")
1390 (if archive-read-only (error "Archive is read-only"))
1391 (let ((func (archive-name "chmod-entry")))
1394 (funcall func new-mode (archive-get-marked ?* t))
1395 (archive-resummarize))
1396 (error "Setting mode bits is not supported for this archive type"))))
1398 (defun archive-chown-entry (new-uid)
1399 "Change the owner of all marked or this member."
1400 (interactive "nNew uid: ")
1401 (if archive-read-only (error "Archive is read-only"))
1402 (let ((func (archive-name "chown-entry")))
1405 (funcall func new-uid (archive-get-marked ?* t))
1406 (archive-resummarize))
1407 (error "Setting owner is not supported for this archive type"))))
1409 (defun archive-chgrp-entry (new-gid)
1410 "Change the group of all marked or this member."
1411 (interactive "nNew gid: ")
1412 (if archive-read-only (error "Archive is read-only"))
1413 (let ((func (archive-name "chgrp-entry")))
1416 (funcall func new-gid (archive-get-marked ?* t))
1417 (archive-resummarize))
1418 (error "Setting group is not supported for this archive type"))))
1420 (defun archive-expunge ()
1421 "Do the flagged deletions."
1425 (goto-char archive-file-list-start)
1426 (while (< (point) archive-file-list-end)
1427 (if (= (following-char) ?D)
1428 (setq files (cons (aref (archive-get-descr) 0) files)))
1430 (setq files (nreverse files))
1432 (or (not archive-read-only)
1433 (error "Archive is read-only"))
1434 (or (yes-or-no-p (format "Really delete %d member%s? "
1436 (if (null (cdr files)) "" "s")))
1437 (error "Operation aborted"))
1438 (let ((archive (archive-maybe-copy (buffer-file-name)))
1439 (expunger (archive-name "expunge")))
1440 (if (fboundp expunger)
1441 (funcall expunger archive files)
1442 (archive-*-expunge archive files (symbol-value expunger)))
1443 (archive-maybe-update nil)
1445 (archive-resummarize)
1446 (revert-buffer))))))
1448 (defun archive-*-expunge (archive files command)
1449 (apply #'call-process
1454 (append (cdr command) (cons archive files))))
1456 (defun archive-rename-entry (newname)
1457 "Change the name associated with this entry in the archive file."
1458 (interactive "sNew name: ")
1459 (if archive-read-only (error "Archive is read-only"))
1460 (if (string= newname "")
1461 (error "Archive members may not be given empty names"))
1462 (let ((func (archive-name "rename-entry"))
1463 (descr (archive-get-descr)))
1467 (encode-coding-string newname
1468 archive-file-name-coding-system)
1470 (archive-resummarize))
1471 (error "Renaming is not supported for this archive type"))))
1473 ;; Revert the buffer and recompute the dired-like listing.
1474 (defun archive-mode-revert (&optional _no-auto-save _no-confirm)
1475 (let ((no (archive-get-lineno)))
1476 (setq archive-files nil)
1477 (let ((revert-buffer-function nil)
1478 (coding-system-for-read 'no-conversion))
1479 (revert-buffer t t))
1481 (goto-char archive-file-list-start)
1482 (archive-next-line no)))
1484 (defun archive-undo ()
1485 "Undo in an archive buffer.
1486 This doesn't recover lost files, it just undoes changes in the buffer itself."
1488 (let ((inhibit-read-only t))
1490 ;; -------------------------------------------------------------------------
1491 ;;; Section: Arc Archives
1493 (defun archive-arc-summarize ()
1499 (while (and (< (+ p 29) (point-max))
1500 (= (byte-after p) ?\C-z)
1501 (> (byte-after (1+ p)) 0))
1502 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1503 (fnlen (or (string-match "\0" namefld) 13))
1504 (efnname (decode-coding-string (substring namefld 0 fnlen)
1505 archive-file-name-coding-system))
1506 ;; Convert to float to avoid overflow for very large files.
1507 (csize (archive-l-e (+ p 15) 4 'float))
1508 (moddate (archive-l-e (+ p 19) 2))
1509 (modtime (archive-l-e (+ p 21) 2))
1510 (ucsize (archive-l-e (+ p 25) 4 'float))
1511 (fiddle (string= efnname (upcase efnname)))
1512 (ifnname (if fiddle (downcase efnname) efnname))
1513 (text (format " %8.0f %-11s %-8s %s"
1515 (archive-dosdate moddate)
1516 (archive-dostime modtime)
1518 (setq maxlen (max maxlen fnlen)
1519 totalsize (+ totalsize ucsize)
1520 visual (cons (vector text
1521 (- (length text) (length ifnname))
1524 files (cons (vector efnname ifnname fiddle nil (1- p))
1526 ;; p needs to stay an integer, since we use it in char-after
1527 ;; above. Passing through `round' limits the compressed size
1528 ;; to most-positive-fixnum, but if the compressed size exceeds
1529 ;; that, we cannot visit the archive anyway.
1530 p (+ p 29 (round csize)))))
1531 (goto-char (point-min))
1532 (let ((dash (concat "- -------- ----------- -------- "
1533 (make-string maxlen ?-)
1535 (insert "M Length Date Time File\n"
1537 (archive-summarize-files (nreverse visual))
1539 (format " %8.0f %d file%s"
1542 (if (= 1 (length files)) "" "s"))
1544 (apply #'vector (nreverse files))))
1546 (defun archive-arc-rename-entry (newname descr)
1547 (if (string-match "[:\\\\/]" newname)
1548 (error "File names in arc files must not contain a directory component"))
1549 (if (> (length newname) 12)
1550 (error "File names in arc files are limited to 12 characters"))
1551 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1553 (inhibit-read-only t))
1557 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1559 (insert-unibyte name)))))
1560 ;; -------------------------------------------------------------------------
1561 ;;; Section: Lzh Archives
1563 (defun archive-lzh-summarize (&optional start)
1564 (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
1569 (while (progn (goto-char p) ;beginning of a base header.
1570 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1571 (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
1572 ;; Convert to float to avoid overflow for very large files.
1573 (csize (archive-l-e (+ p 7) 4 'float)) ;size of a compressed file to follow (level 0 and 2),
1574 ;size of extended headers + the compressed file to follow (level 1).
1575 (ucsize (archive-l-e (+ p 11) 4 'float)) ;size of an uncompressed file.
1576 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1577 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
1578 (hdrlvl (byte-after (+ p 20))) ;header level
1579 thsize ;total header size (base + extensions)
1580 fnlen efnname osid fiddle ifnname width p2
1581 neh ;beginning of next extension header (level 1 and 2)
1582 mode modestr uid gid text dir prname
1583 gname uname modtime moddate)
1584 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1585 (when (or (= hdrlvl 0) (= hdrlvl 1))
1586 (setq fnlen (byte-after (+ p 21))) ;filename length
1587 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
1588 (decode-coding-string
1589 str archive-file-name-coding-system)))
1590 (setq p2 (+ p 22 fnlen))) ;
1592 (setq neh (+ p2 3)) ;specific to level 1 header
1594 (setq neh (+ p 24)))) ;specific to level 2 header
1595 (if neh ;if level 1 or 2 we expect extension headers to follow
1596 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
1597 (etype (byte-after (+ neh 2)))) ;extension type
1598 (while (not (= ehsize 0))
1600 ((= etype 1) ;file name
1601 (let ((i (+ neh 3)))
1602 (while (< i (+ neh ehsize))
1603 (setq efnname (concat efnname (char-to-string (byte-after i))))
1605 ((= etype 2) ;directory name
1606 (let ((i (+ neh 3)))
1607 (while (< i (+ neh ehsize))
1608 (setq dir (concat dir
1609 (if (= (byte-after i)
1615 ((= etype 80) ;Unix file permission
1616 (setq mode (archive-l-e (+ neh 3) 2)))
1617 ((= etype 81) ;UNIX file group/user ID
1618 (progn (setq uid (archive-l-e (+ neh 3) 2))
1619 (setq gid (archive-l-e (+ neh 5) 2))))
1620 ((= etype 82) ;UNIX file group name
1621 (let ((i (+ neh 3)))
1622 (while (< i (+ neh ehsize))
1623 (setq gname (concat gname (char-to-string (char-after i))))
1625 ((= etype 83) ;UNIX file user name
1626 (let ((i (+ neh 3)))
1627 (while (< i (+ neh ehsize))
1628 (setq uname (concat uname (char-to-string (char-after i))))
1631 (setq neh (+ neh ehsize))
1632 (setq ehsize (archive-l-e neh 2))
1633 (setq etype (byte-after (+ neh 2))))
1634 ;;get total header size for level 1 and 2 headers
1635 (setq thsize (- neh p))))
1636 (if (= hdrlvl 0) ;total header size
1637 (setq thsize hsize))
1638 ;; OS ID field not present in level 0 header, use code 0 "generic"
1639 ;; in that case as per lha program header.c get_header()
1640 (setq osid (cond ((= hdrlvl 0) 0)
1641 ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
1642 ((= hdrlvl 2) (char-after (+ p 23)))))
1643 ;; Filename fiddling must follow the lha program, otherwise the name
1644 ;; passed to "lha pq" etc won't match (which for an extract silently
1645 ;; results in no output). As of version 1.14i it goes from the OS ID,
1646 ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
1647 ;; converts "\" to "/".
1648 ;; - For 0 generic: generic_to_unix_filename() downcases if there's
1649 ;; no lower case already present, and converts "\" to "/".
1650 ;; - For 'm' macOS: macos_to_unix_filename() changes "/" to ":" and
1652 (setq fiddle (cond ((= ?M osid) t)
1653 ((= 0 osid) (string= efnname (upcase efnname)))))
1654 (setq ifnname (if fiddle (downcase efnname) efnname))
1655 (setq prname (if dir (concat dir ifnname) ifnname))
1656 (setq width (if prname (string-width prname) 0))
1657 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1658 (setq moddate (if (= hdrlvl 2)
1659 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1660 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1661 (setq modtime (if (= hdrlvl 2)
1662 (archive-unixtime time1 time2)
1663 (archive-dostime time1)))
1664 (setq text (if archive-alternate-display
1665 (format " %8.0f %5S %5S %s"
1670 (format " %10s %8.0f %-11s %-8s %s"
1676 (setq maxlen (max maxlen width)
1677 totalsize (+ totalsize ucsize)
1678 visual (cons (vector text
1679 (- (length text) (length prname))
1682 files (cons (vector prname ifnname fiddle mode (1- p))
1685 ;; p needs to stay an integer, since we use it in goto-char
1686 ;; above. Passing through `round' limits the compressed size
1687 ;; to most-positive-fixnum, but if the compressed size exceeds
1688 ;; that, we cannot visit the archive anyway.
1689 (setq p (+ p hsize 2 (round csize))))
1690 ((or (= hdrlvl 2) (= hdrlvl 0))
1691 (setq p (+ p thsize 2 (round csize)))))
1693 (goto-char (point-min))
1694 (let ((dash (concat (if archive-alternate-display
1695 "- -------- ----- ----- "
1696 "- ---------- -------- ----------- -------- ")
1697 (make-string maxlen ?-)
1699 (header (if archive-alternate-display
1700 "M Length Uid Gid File\n"
1701 "M Filemode Length Date Time File\n"))
1702 (sumline (if archive-alternate-display
1704 " %8.0f %d file%s")))
1705 (insert header dash)
1706 (archive-summarize-files (nreverse visual))
1711 (if (= 1 (length files)) "" "s"))
1713 (apply #'vector (nreverse files))))
1715 (defconst archive-lzh-alternate-display t)
1717 (defun archive-lzh-extract (archive name)
1718 (archive-extract-by-stdout archive name archive-lzh-extract))
1720 (defun archive-lzh-resum (p count)
1723 (setq count (1- count)
1724 sum (+ sum (byte-after p))
1728 (defun archive-lzh-rename-entry (newname descr)
1732 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1733 (oldhsize (byte-after p))
1734 (oldfnlen (byte-after (+ p 21)))
1735 (newfnlen (length newname))
1736 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1737 (inhibit-read-only t))
1738 (if (> newhsize 255)
1739 (error "The file name is too long"))
1740 (goto-char (+ p 21))
1741 (delete-char (1+ oldfnlen))
1742 (insert-unibyte newfnlen newname)
1745 (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
1747 (defun archive-lzh-ogm (newval files errtxt ofs)
1752 (let* ((p (+ archive-proper-file-start (aref fil 4)))
1753 (hsize (byte-after p))
1754 (fnlen (byte-after (+ p 21)))
1756 (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
1757 (inhibit-read-only t))
1760 (or (numberp newval)
1761 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1762 (goto-char (+ p2 ofs))
1764 (insert-unibyte (logand newval 255) (lsh newval -8))
1767 (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
1768 (message "Member %s does not have %s field"
1769 (aref fil 1) errtxt)))))))
1771 (defun archive-lzh-chown-entry (newuid files)
1772 (archive-lzh-ogm newuid files "an uid" 10))
1774 (defun archive-lzh-chgrp-entry (newgid files)
1775 (archive-lzh-ogm newgid files "a gid" 12))
1777 (defun archive-lzh-chmod-entry (newmode files)
1779 ;; This should work even though newmode will be dynamically accessed.
1780 (lambda (old) (archive-calc-mode old newmode t))
1781 files "a unix-style mode" 8))
1783 ;; -------------------------------------------------------------------------
1784 ;;; Section: Lzh Self-Extracting .exe Archives
1786 ;; No support for modifying these files. It looks like the lha for unix
1787 ;; program (as of version 1.14i) can't create or retain the DOS exe part.
1788 ;; If you do an "lha a" on a .exe for instance it renames and writes to a
1791 (defun archive-lzh-exe-summarize ()
1792 "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
1794 ;; Skip the initial executable code part and apply archive-lzh-summarize
1795 ;; to the archive part proper. The "-lh5-" etc regexp here for the start
1796 ;; is the same as in archive-find-type.
1798 ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
1799 ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
1800 ;; the header checksum, or level 2 a test of the "attribute" and size.
1802 (re-search-forward "..-l[hz][0-9ds]-" nil)
1803 (archive-lzh-summarize (match-beginning 0)))
1805 ;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
1807 (defalias 'archive-lzh-exe-extract 'archive-lzh-extract
1808 "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
1810 ;; -------------------------------------------------------------------------
1811 ;;; Section: Zip Archives
1813 (defun archive-zip-summarize ()
1814 (goto-char (- (point-max) (- 22 18)))
1815 (search-backward-regexp "[P]K\005\006")
1816 (let ((p (archive-l-e (+ (point) 16) 4))
1821 emacs-int-has-32bits)
1823 ;; If the offset of end-of-central-directory is -1, this is a
1824 ;; Zip64 extended ZIP file format, and we need to glean the info
1825 ;; from Zip64 records instead.
1827 ;; First, find the Zip64 end-of-central-directory locator.
1828 (search-backward "PK\006\007")
1829 ;; Pay attention: the offset of Zip64 end-of-central-directory
1830 ;; is a 64-bit field, so it could overflow the Emacs integer
1831 ;; even on a 64-bit host, let alone 32-bit one. But since we've
1832 ;; already read the zip file into a buffer, and this is a byte
1833 ;; offset into the file we've read, it must be short enough, so
1834 ;; such an overflow can never happen, and we can safely read
1835 ;; these 8 bytes into an Emacs integer. Moreover, on host with
1836 ;; 32-bit Emacs integer we can only read 4 bytes, since they are
1837 ;; stored in little-endian byte order.
1838 (setq emacs-int-has-32bits (<= most-positive-fixnum #x1fffffff))
1839 (setq p (+ (point-min)
1840 (archive-l-e (+ (point) 8) (if emacs-int-has-32bits 4 8))))
1842 ;; We should be at Zip64 end-of-central-directory record now.
1843 (or (string= "PK\006\006" (buffer-substring p (+ p 4)))
1844 (error "Unrecognized ZIP file format"))
1845 ;; Offset to central directory:
1846 (setq p (archive-l-e (+ p 48) (if emacs-int-has-32bits 4 8))))
1847 (setq p (+ p (point-min)))
1848 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1849 (let* ((creator (byte-after (+ p 5)))
1850 ;; (method (archive-l-e (+ p 10) 2))
1851 (modtime (archive-l-e (+ p 12) 2))
1852 (moddate (archive-l-e (+ p 14) 2))
1853 ;; Convert to float to avoid overflow for very large files.
1854 (ucsize (archive-l-e (+ p 24) 4 'float))
1855 (fnlen (archive-l-e (+ p 28) 2))
1856 (exlen (archive-l-e (+ p 30) 2))
1857 (fclen (archive-l-e (+ p 32) 2))
1858 (lheader (archive-l-e (+ p 42) 4))
1859 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1860 (decode-coding-string
1861 str archive-file-name-coding-system)))
1862 (isdir (and (= ucsize 0)
1863 (string= (file-name-nondirectory efnname) "")))
1864 (mode (cond ((memq creator '(2 3)) ; Unix
1865 (archive-l-e (+ p 40) 2))
1866 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1868 (if isdir (logior 16384 ?\111) 0)
1870 (logand 1 (byte-after (+ p 38))))
1873 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1874 (fiddle (and archive-zip-case-fiddle
1875 (not (not (memq creator '(0 2 4 5 9))))
1876 (string= (upcase efnname) efnname)))
1877 (ifnname (if fiddle (downcase efnname) efnname))
1878 (width (string-width ifnname))
1879 (text (format " %10s %8.0f %-11s %-8s %s"
1882 (archive-dosdate moddate)
1883 (archive-dostime modtime)
1885 (setq maxlen (max maxlen width)
1886 totalsize (+ totalsize ucsize)
1887 visual (cons (vector text
1888 (- (length text) (length ifnname))
1891 files (cons (if isdir
1893 (vector efnname ifnname fiddle mode
1894 (list (1- p) lheader)))
1896 p (+ p 46 fnlen exlen fclen))))
1897 (goto-char (point-min))
1898 (let ((dash (concat "- ---------- -------- ----------- -------- "
1899 (make-string maxlen ?-)
1901 (insert "M Filemode Length Date Time File\n"
1903 (archive-summarize-files (nreverse visual))
1905 (format " %8.0f %d file%s"
1908 (if (= 1 (length files)) "" "s"))
1910 (apply #'vector (nreverse files))))
1912 (defun archive-zip-extract (archive name)
1914 ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
1915 (archive-*-extract archive name archive-zip-extract))
1916 ((equal (car archive-zip-extract) archive-7z-program)
1917 (let ((archive-7z-extract archive-zip-extract))
1918 (archive-7z-extract archive name)))
1920 (archive-extract-by-stdout
1922 ;; unzip expands wildcards in NAME, so we need to quote it. But
1923 ;; not on DOS/Windows, since that fails extraction on those
1924 ;; systems (unless w32-quote-process-args is nil), and file names
1925 ;; with wildcards in zip archives don't work there anyway.
1926 ;; FIXME: Does pkunzip need similar treatment?
1927 (if (and (or (not (memq system-type '(windows-nt ms-dos)))
1928 (and (boundp 'w32-quote-process-args)
1929 (null w32-quote-process-args)))
1930 (equal (car archive-zip-extract) "unzip"))
1931 (shell-quote-argument name)
1933 archive-zip-extract))))
1935 (defun archive-zip-write-file-member (archive descr)
1936 (archive-*-write-file-member
1939 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1941 (defun archive-zip-chmod-entry (newmode files)
1946 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
1947 (creator (byte-after (+ p 5)))
1948 (oldmode (aref fil 3))
1949 (newval (archive-calc-mode oldmode newmode t))
1950 (inhibit-read-only t))
1951 (cond ((memq creator '(2 3)) ; Unix
1952 (goto-char (+ p 40))
1954 (insert-unibyte (logand newval 255) (lsh newval -8)))
1955 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1956 (goto-char (+ p 38))
1957 (insert-unibyte (logior (logand (byte-after (point)) 254)
1958 (logand (logxor 1 (lsh newval -7)) 1)))
1960 (t (message "Don't know how to change mode for this member"))))
1962 ;; -------------------------------------------------------------------------
1963 ;;; Section: Zoo Archives
1965 (defun archive-zoo-summarize ()
1966 (let ((p (1+ (archive-l-e 25 4)))
1971 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1972 (> (archive-l-e (+ p 6) 4) 0))
1973 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1974 (moddate (archive-l-e (+ p 14) 2))
1975 (modtime (archive-l-e (+ p 16) 2))
1976 ;; Convert to float to avoid overflow for very large files.
1977 (ucsize (archive-l-e (+ p 20) 4 'float))
1978 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1979 (dirtype (byte-after (+ p 4)))
1980 (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
1981 (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
1982 (fnlen (or (string-match "\0" namefld) 13))
1986 (concat (buffer-substring
1988 (+ p 58 lfnlen ldirlen -1))
1992 (buffer-substring (+ p 58)
1994 (substring namefld 0 fnlen)))))
1995 (decode-coding-string
1996 str archive-file-name-coding-system)))
1997 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1998 (ifnname (if fiddle (downcase efnname) efnname))
1999 (width (string-width ifnname))
2000 (text (format " %8.0f %-11s %-8s %s"
2002 (archive-dosdate moddate)
2003 (archive-dostime modtime)
2005 (setq maxlen (max maxlen width)
2006 totalsize (+ totalsize ucsize)
2007 visual (cons (vector text
2008 (- (length text) (length ifnname))
2011 files (cons (vector efnname ifnname fiddle nil (1- p))
2014 (goto-char (point-min))
2015 (let ((dash (concat "- -------- ----------- -------- "
2016 (make-string maxlen ?-)
2018 (insert "M Length Date Time File\n"
2020 (archive-summarize-files (nreverse visual))
2022 (format " %8.0f %d file%s"
2025 (if (= 1 (length files)) "" "s"))
2027 (apply #'vector (nreverse files))))
2029 (defun archive-zoo-extract (archive name)
2030 (archive-extract-by-stdout archive name archive-zoo-extract))
2032 ;; -------------------------------------------------------------------------
2033 ;;; Section: Rar Archives
2035 (defun archive-rar-summarize (&optional file)
2036 ;; File is used internally for `archive-rar-exe-summarize'.
2037 (unless file (setq file buffer-file-name))
2038 (let* ((copy (file-local-copy file))
2043 (call-process "lsar" nil t nil "-l" (or file copy))
2044 (if copy (delete-file copy))
2045 (goto-char (point-min))
2046 (re-search-forward "^\\(\s+=+\s?+\\)+\n")
2047 (while (looking-at (concat "^\s+[0-9.]+\s+D?-+\s+" ; Flags
2048 "\\([0-9-]+\\)\s+" ; Size
2049 "\\([-0-9.%]+\\|-+\\)\s+" ; Ratio
2050 "\\([0-9a-zA-Z]+\\)\s+" ; Mode
2051 "\\([0-9-]+\\)\s+" ; Date
2052 "\\([0-9:]+\\)\s+" ; Time
2055 (goto-char (match-end 0))
2056 (let ((name (match-string 6))
2057 (size (match-string 1)))
2058 (if (> (length name) maxname) (setq maxname (length name)))
2059 (if (> (length size) maxsize) (setq maxsize (length size)))
2060 (push (vector name name nil nil
2062 size (match-string 2)
2064 (match-string 4) (match-string 5))
2066 (setq files (nreverse files))
2067 (goto-char (point-min))
2068 (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
2069 (sep (format format "----------" "-----" (make-string maxsize ?-)
2071 (column (length sep)))
2072 (insert (format format " Date " "Time " "Size" "Ratio" "Filename") "\n")
2073 (insert sep (make-string maxname ?-) "\n")
2074 (archive-summarize-files (mapcar (lambda (desc)
2086 (insert sep (make-string maxname ?-) "\n")
2087 (apply #'vector files))))
2089 (defun archive-rar-extract (archive name)
2090 ;; unrar-free seems to have no way to extract to stdout or even to a file.
2091 (if (file-name-absolute-p name)
2092 ;; The code below assumes the name is relative and may do undesirable
2093 ;; things otherwise.
2094 (error "Can't extract files with non-relative names")
2095 (archive-extract-by-file archive name `("unar" "-no-directory" "-o") "Successfully extracted")))
2097 ;;; Section: Rar self-extracting .exe archives.
2099 (defun archive-rar-exe-summarize ()
2100 (let ((tmpfile (make-temp-file "rarexe")))
2103 (goto-char (point-min))
2104 (re-search-forward "Rar!")
2105 (write-region (match-beginning 0) (point-max) tmpfile)
2106 (archive-rar-summarize tmpfile))
2107 (delete-file tmpfile))))
2109 (defun archive-rar-exe-extract (archive name)
2110 (let* ((tmpfile (make-temp-file "rarexe"))
2111 (buf (find-buffer-visiting archive))
2112 (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
2115 (with-current-buffer (or buf tmpbuf)
2119 ;; point-max unwidened is assumed to be the end of the
2120 ;; summary text and the beginning of the actual file data.
2121 (progn (goto-char (point-max)) (widen))
2122 (insert-file-contents-literally archive)
2123 (goto-char (point-min)))
2124 (re-search-forward "Rar!")
2125 (write-region (match-beginning 0) (point-max) tmpfile))))
2126 (archive-rar-extract tmpfile name))
2127 (if tmpbuf (kill-buffer tmpbuf))
2128 (delete-file tmpfile))))
2130 ;; -------------------------------------------------------------------------
2131 ;;; Section: 7z Archives
2133 (defun archive-7z-summarize ()
2136 (file buffer-file-name)
2139 (call-process archive-7z-program nil t nil "l" "-slt" file)
2140 (goto-char (point-min))
2141 ;; Four dashes start the meta info section that should be skipped.
2142 ;; Archive members start with more than four dashes.
2143 (re-search-forward "^-----+\n")
2144 (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
2145 (goto-char (match-end 0))
2146 (let ((name (match-string 1))
2147 (size (save-excursion
2148 (and (re-search-forward "^Size = \\(.*\\)\n")
2150 (time (save-excursion
2151 (and (re-search-forward "^Modified = \\(.*\\)\n")
2152 (match-string 1)))))
2153 (if (> (length name) maxname) (setq maxname (length name)))
2154 (if (> (length size) maxsize) (setq maxsize (length size)))
2155 (push (vector name name nil nil time nil nil size)
2157 (setq files (nreverse files))
2158 (goto-char (point-min))
2159 (let* ((format (format " %%%ds %%s %%s" maxsize))
2160 (sep (format format (make-string maxsize ?-) "-------------------" ""))
2161 (column (length sep)))
2162 (insert (format format "Size " "Date Time " " Filename") "\n")
2163 (insert sep (make-string maxname ?-) "\n")
2164 (archive-summarize-files (mapcar (lambda (desc)
2174 (insert sep (make-string maxname ?-) "\n")
2175 (apply #'vector files))))
2177 (defun archive-7z-extract (archive name)
2178 ;; 7z doesn't provide a `quiet' option to suppress non-essential
2179 ;; stderr messages. So redirect stderr to a temp file and display it
2180 ;; in the echo area when it contains no message indicating success.
2181 (archive-extract-by-stdout
2182 archive name archive-7z-extract "Everything is Ok"))
2184 (defun archive-7z-write-file-member (archive descr)
2185 (archive-*-write-file-member
2190 ;; -------------------------------------------------------------------------
2191 ;;; Section `ar' archives.
2193 ;; TODO: we currently only handle the basic format of ar archives,
2194 ;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
2195 ;; for .deb packages.
2197 (autoload 'tar-grind-file-mode "tar-mode")
2199 (defconst archive-ar-file-header-re
2200 "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
2202 (defun archive-ar-summarize ()
2203 ;; File is used internally for `archive-rar-exe-summarize'.
2211 (goto-char (point-min))
2212 (search-forward "!<arch>\n")
2213 (while (looking-at archive-ar-file-header-re)
2214 (let ((name (match-string 1))
2216 ;; Emacs will automatically use float here because those
2217 ;; timestamps don't fit in our ints.
2218 (time (string-to-number (match-string 2)))
2219 (user (match-string 3))
2220 (group (match-string 4))
2221 (mode (string-to-number (match-string 5) 8))
2222 (size (string-to-number (match-string 6))))
2223 ;; Move to the beginning of the data.
2224 (goto-char (match-end 0))
2225 (setq time (format-time-string "%Y-%m-%d %H:%M" time))
2227 (cond ((equal name "// ")
2228 (propertize ".<ExtNamesTable>." 'face 'italic))
2230 (propertize ".<LookupTable>." 'face 'italic))
2231 ((string-match "/? *\\'" name)
2232 (substring name 0 (match-beginning 0)))))
2233 (setq user (substring user 0 (string-match " +\\'" user)))
2234 (setq group (substring group 0 (string-match " +\\'" group)))
2235 (setq mode (tar-grind-file-mode mode))
2236 ;; Move to the end of the data.
2237 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1))
2238 (setq size (number-to-string size))
2239 (if (> (length name) maxname) (setq maxname (length name)))
2240 (if (> (length time) maxtime) (setq maxtime (length time)))
2241 (if (> (length user) maxuser) (setq maxuser (length user)))
2242 (if (> (length group) maxgroup) (setq maxgroup (length group)))
2243 (if (> (length mode) maxmode) (setq maxmode (length mode)))
2244 (if (> (length size) maxsize) (setq maxsize (length size)))
2245 (push (vector name extname nil mode
2246 time user group size)
2248 (setq files (nreverse files))
2249 (goto-char (point-min))
2250 (let* ((format (format "%%%ds %%%ds/%%-%ds %%%ds %%%ds %%s"
2251 maxmode maxuser maxgroup maxsize maxtime))
2252 (sep (format format (make-string maxmode ?-)
2253 (make-string maxuser ?-)
2254 (make-string maxgroup ?-)
2255 (make-string maxsize ?-)
2256 (make-string maxtime ?-) ""))
2257 (column (length sep)))
2258 (insert (format format " Mode " "User" "Group" " Size "
2259 " Date " "Filename")
2261 (insert sep (make-string maxname ?-) "\n")
2262 (archive-summarize-files (mapcar (lambda (desc)
2275 (insert sep (make-string maxname ?-) "\n")
2276 (apply #'vector files))))
2278 (defun archive-ar-extract (archive name)
2279 (let ((destbuf (current-buffer))
2280 (archivebuf (find-file-noselect archive))
2282 (with-current-buffer archivebuf
2284 ;; We may be in archive-mode or not, so either with or without
2285 ;; narrowing and with or without a prepended summary.
2288 (search-forward "!<arch>\n")
2289 (while (and (not from) (looking-at archive-ar-file-header-re))
2290 (let ((this (match-string 1)))
2291 (setq size (string-to-number (match-string 6)))
2292 (goto-char (match-end 0))
2293 (if (equal name this)
2295 ;; Move to the end of the data.
2296 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1)))))
2298 (set-buffer-multibyte nil)
2299 (with-current-buffer destbuf
2300 ;; Do it within the `widen'.
2301 (insert-buffer-substring archivebuf from (+ from size)))
2302 (set-buffer-multibyte 'to)
2303 ;; Inform the caller that the call succeeded.
2306 ;; -------------------------------------------------------------------------
2307 ;; This line was a mistake; it is kept now for compatibility.
2309 (provide 'archive-mode)
2313 ;;; arc-mode.el ends here