1 ;;; arc-mode.el --- simple editing of archives
3 ;; Copyright (C) 1995, 1997, 1998, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Morten Welinder <terra@gnu.org>
7 ;; Keywords: archives msdog editing major-mode
8 ;; Favourite-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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; NAMING: "arc" is short for "archive" and does not refer specifically
30 ;; to files whose name end in ".arc"
32 ;; This code does not decode any files internally, although it does
33 ;; understand the directory level of the archives. For this reason,
34 ;; you should expect this code to need more fiddling than tar-mode.el
35 ;; (although it at present has fewer bugs :-) In particular, I have
36 ;; not tested this under Ms-Dog myself.
37 ;; -------------------------------------
38 ;; INTERACTION: arc-mode.el should play together with
40 ;; * ange-ftp.el: Remote archives (i.e., ones that ange-ftp has brought
41 ;; to you) are handled by doing all updates on a local
42 ;; copy. When you make changes to a remote file the
43 ;; changes will first take effect when the archive buffer
44 ;; is saved. You will be warned about this.
46 ;; * dos-fns.el: (Part of Emacs 19). You get automatic ^M^J <--> ^J
49 ;; arc-mode.el does not work well with crypt++.el; for the archives as
50 ;; such this could be fixed (but wouldn't be useful) by declaring such
51 ;; archives to be "remote". For the members this is a general Emacs
52 ;; problem that 19.29's file formats may fix.
53 ;; -------------------------------------
54 ;; ARCHIVE TYPES: Currently only the archives below are handled, but the
55 ;; structure for handling just about anything is in place.
58 ;; --------------------------------
59 ;; View listing Intern Intern Intern Intern
60 ;; Extract member Y Y Y Y
61 ;; Save changed member Y Y Y Y
62 ;; Add new member N N N N
63 ;; Delete member Y Y Y Y
64 ;; Rename member Y Y N N
69 ;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
70 ;; on the first released version of this package.
72 ;; This code is partly based on tar-mode.el from Emacs.
73 ;; -------------------------------------
74 ;; ARCHIVE STRUCTURES:
75 ;; (This is mostly for myself.)
77 ;; ARC A series of (header,file). No interactions among members.
79 ;; LZH A series of (header,file). Headers are checksummed. No
80 ;; interaction among members.
81 ;; Headers come in three flavours called level 0, 1 and 2 headers.
82 ;; Level 2 header is free of DOS specific restrictions and most
83 ;; prevalently used. Also level 1 and 2 headers consist of base
84 ;; and extension headers. For more details see
85 ;; http://homepage1.nifty.com/dangan/en/Content/Program/Java/jLHA/Notes/Notes.html
86 ;; http://www.osirusoft.com/joejared/lzhformat.html
88 ;; ZIP A series of (lheader,fil) followed by a "central directory"
89 ;; which is a series of (cheader) followed by an end-of-
90 ;; central-dir record possibly followed by junk. The e-o-c-d
91 ;; links to c-d. cheaders link to lheaders which are basically
92 ;; cut-down versions of the cheaders.
94 ;; ZOO An archive header followed by a series of (header,file).
95 ;; Each member header points to the next. The archive is
96 ;; terminated by a bogus header with a zero next link.
97 ;; -------------------------------------
98 ;; HOOKS: `foo' means one of the supported archive types.
101 ;; archive-foo-mode-hook
102 ;; archive-extract-hooks
106 ;; -------------------------------------------------------------------------
107 ;; Section: Configuration.
109 (defgroup archive nil
110 "Simple editing of archives."
113 (defgroup archive-arc nil
114 "ARC-specific options to archive."
117 (defgroup archive-lzh nil
118 "LZH-specific options to archive."
121 (defgroup archive-zip nil
122 "ZIP-specific options to archive."
125 (defgroup archive-zoo nil
126 "ZOO-specific options to archive."
129 (defcustom archive-tmpdir
130 ;; make-temp-name is safe here because we use this name
131 ;; to create a directory.
133 (expand-file-name (if (eq system-type
'ms-dos
) "ar" "archive.tmp")
134 temporary-file-directory
))
135 "Directory for temporary files made by `arc-mode.el'."
139 (defcustom archive-remote-regexp
"^/[^/:]*[^/:.]:"
140 "*Regexp recognizing archive files names that are not local.
141 A non-local file is one whose file name is not proper outside Emacs.
142 A local copy of the archive will be used when updating."
146 (defcustom archive-extract-hooks nil
147 "*Hooks to run when an archive member has been extracted."
150 ;; ------------------------------
151 ;; Arc archive configuration
153 ;; We always go via a local file since there seems to be no reliable way
154 ;; to extract to stdout without junk getting added.
155 (defcustom archive-arc-extract
157 "*Program and its options to run in order to extract an arc file member.
158 Extraction should happen to the current directory. Archive and member
160 :type
'(list (string :tag
"Program")
161 (repeat :tag
"Options"
163 (string :format
"%v")))
166 (defcustom archive-arc-expunge
168 "*Program and its options to run in order to delete arc file members.
169 Archive and member names will be added."
170 :type
'(list (string :tag
"Program")
171 (repeat :tag
"Options"
173 (string :format
"%v")))
176 (defcustom archive-arc-write-file-member
178 "*Program and its options to run in order to update an arc file member.
179 Archive and member name will be added."
180 :type
'(list (string :tag
"Program")
181 (repeat :tag
"Options"
183 (string :format
"%v")))
185 ;; ------------------------------
186 ;; Lzh archive configuration
188 (defcustom archive-lzh-extract
190 "*Program and its options to run in order to extract an lzh file member.
191 Extraction should happen to standard output. Archive and member name will
193 :type
'(list (string :tag
"Program")
194 (repeat :tag
"Options"
196 (string :format
"%v")))
199 (defcustom archive-lzh-expunge
201 "*Program and its options to run in order to delete lzh file members.
202 Archive and member names will be added."
203 :type
'(list (string :tag
"Program")
204 (repeat :tag
"Options"
206 (string :format
"%v")))
209 (defcustom archive-lzh-write-file-member
211 "*Program and its options to run in order to update an lzh file member.
212 Archive and member name will be added."
213 :type
'(list (string :tag
"Program")
214 (repeat :tag
"Options"
216 (string :format
"%v")))
218 ;; ------------------------------
219 ;; Zip archive configuration
221 (defcustom archive-zip-extract
222 (if (and (not (executable-find "unzip"))
223 (executable-find "pkunzip"))
224 '("pkunzip" "-e" "-o-")
225 '("unzip" "-qq" "-c"))
226 "*Program and its options to run in order to extract a zip file member.
227 Extraction should happen to standard output. Archive and member name will
228 be added. If `archive-zip-use-pkzip' is non-nil then this program is
229 expected to extract to a file junking the directory part of the name."
230 :type
'(list (string :tag
"Program")
231 (repeat :tag
"Options"
233 (string :format
"%v")))
236 ;; For several reasons the latter behaviour is not desirable in general.
237 ;; (1) It uses more disk space. (2) Error checking is worse or non-
238 ;; existent. (3) It tends to do funny things with other systems' file
241 (defcustom archive-zip-expunge
242 (if (and (not (executable-find "zip"))
243 (executable-find "pkzip"))
246 "*Program and its options to run in order to delete zip file members.
247 Archive and member names will be added."
248 :type
'(list (string :tag
"Program")
249 (repeat :tag
"Options"
251 (string :format
"%v")))
254 (defcustom archive-zip-update
255 (if (and (not (executable-find "zip"))
256 (executable-find "pkzip"))
259 "*Program and its options to run in order to update a zip file member.
260 Options should ensure that specified directory will be put into the zip
261 file. Archive and member name will be added."
262 :type
'(list (string :tag
"Program")
263 (repeat :tag
"Options"
265 (string :format
"%v")))
268 (defcustom archive-zip-update-case
269 (if (and (not (executable-find "zip"))
270 (executable-find "pkzip"))
273 "*Program and its options to run in order to update a case fiddled zip member.
274 Options should ensure that specified directory will be put into the zip file.
275 Archive and member name will be added."
276 :type
'(list (string :tag
"Program")
277 (repeat :tag
"Options"
279 (string :format
"%v")))
282 (defcustom archive-zip-case-fiddle t
283 "*If non-nil then zip file members may be down-cased.
284 This case fiddling will only happen for members created by a system
285 that uses caseless file names."
288 ;; ------------------------------
289 ;; Zoo archive configuration
291 (defcustom archive-zoo-extract
293 "*Program and its options to run in order to extract a zoo file member.
294 Extraction should happen to standard output. Archive and member name will
296 :type
'(list (string :tag
"Program")
297 (repeat :tag
"Options"
299 (string :format
"%v")))
302 (defcustom archive-zoo-expunge
304 "*Program and its options to run in order to delete zoo file members.
305 Archive and member names will be added."
306 :type
'(list (string :tag
"Program")
307 (repeat :tag
"Options"
309 (string :format
"%v")))
312 (defcustom archive-zoo-write-file-member
314 "*Program and its options to run in order to update a zoo file member.
315 Archive and member name will be added."
316 :type
'(list (string :tag
"Program")
317 (repeat :tag
"Options"
319 (string :format
"%v")))
321 ;; -------------------------------------------------------------------------
322 ;; Section: Variables
324 (defvar archive-subtype nil
"Symbol describing archive type.")
325 (defvar archive-file-list-start nil
"Position of first contents line.")
326 (defvar archive-file-list-end nil
"Position just after last contents line.")
327 (defvar archive-proper-file-start nil
"Position of real archive's start.")
328 (defvar archive-read-only nil
"Non-nil if the archive is read-only on disk.")
329 (defvar archive-local-name nil
"Name of local copy of remote archive.")
330 (defvar archive-mode-map
331 (let ((map (make-keymap)))
332 (suppress-keymap map
)
333 (define-key map
" " 'archive-next-line
)
334 (define-key map
"a" 'archive-alternate-display
)
335 ;;(define-key map "c" 'archive-copy)
336 (define-key map
"d" 'archive-flag-deleted
)
337 (define-key map
"\C-d" 'archive-flag-deleted
)
338 (define-key map
"e" 'archive-extract
)
339 (define-key map
"f" 'archive-extract
)
340 (define-key map
"\C-m" 'archive-extract
)
341 (define-key map
"g" 'revert-buffer
)
342 (define-key map
"h" 'describe-mode
)
343 (define-key map
"m" 'archive-mark
)
344 (define-key map
"n" 'archive-next-line
)
345 (define-key map
"\C-n" 'archive-next-line
)
346 (define-key map
[down] 'archive-next-line)
347 (define-key map "o" 'archive-extract-other-window)
348 (define-key map "p" 'archive-previous-line)
349 (define-key map "q" 'quit-window)
350 (define-key map "\C-p" 'archive-previous-line)
351 (define-key map [up] 'archive-previous-line)
352 (define-key map "r" 'archive-rename-entry)
353 (define-key map "u" 'archive-unflag)
354 (define-key map "\M-\C-?" 'archive-unmark-all-files)
355 (define-key map "v" 'archive-view)
356 (define-key map "x" 'archive-expunge)
357 (define-key map "\177" 'archive-unflag-backwards)
358 (define-key map "E" 'archive-extract-other-window)
359 (define-key map "M" 'archive-chmod-entry)
360 (define-key map "G" 'archive-chgrp-entry)
361 (define-key map "O" 'archive-chown-entry)
363 (if (fboundp 'command-remapping)
365 (define-key map [remap advertised-undo] 'archive-undo)
366 (define-key map [remap undo] 'archive-undo))
367 (substitute-key-definition 'advertised-undo 'archive-undo map global-map)
368 (substitute-key-definition 'undo 'archive-undo map global-map))
371 (if (featurep 'xemacs) 'button2 [mouse-2]) 'archive-extract)
373 (if (featurep 'xemacs)
376 (define-key map [menu-bar immediate]
377 (cons "Immediate" (make-sparse-keymap "Immediate")))
378 (define-key map [menu-bar immediate alternate]
379 '(menu-item "Alternate Display" archive-alternate-display
380 :enable (boundp (archive-name "alternate-display"))
381 :help "Toggle alternate file info display"))
382 (define-key map [menu-bar immediate view]
383 '(menu-item "View This File" archive-view
384 :help "Display file at cursor in View Mode"))
385 (define-key map [menu-bar immediate display]
386 '(menu-item "Display in Other Window" archive-display-other-window
387 :help "Display file at cursor in another window"))
388 (define-key map [menu-bar immediate find-file-other-window]
389 '(menu-item "Find in Other Window" archive-extract-other-window
390 :help "Edit file at cursor in another window"))
391 (define-key map [menu-bar immediate find-file]
392 '(menu-item "Find This File" archive-extract
393 :help "Extract file at cursor and edit it"))
395 (define-key map [menu-bar mark]
396 (cons "Mark" (make-sparse-keymap "Mark")))
397 (define-key map [menu-bar mark unmark-all]
398 '(menu-item "Unmark All" archive-unmark-all-files
399 :help "Unmark all marked files"))
400 (define-key map [menu-bar mark deletion]
401 '(menu-item "Flag" archive-flag-deleted
402 :help "Flag file at cursor for deletion"))
403 (define-key map [menu-bar mark unmark]
404 '(menu-item "Unflag" archive-unflag
405 :help "Unmark file at cursor"))
406 (define-key map [menu-bar mark mark]
407 '(menu-item "Mark" archive-mark
408 :help "Mark file at cursor"))
410 (define-key map [menu-bar operate]
411 (cons "Operate" (make-sparse-keymap "Operate")))
412 (define-key map [menu-bar operate chown]
413 '(menu-item "Change Owner..." archive-chown-entry
414 :enable (fboundp (archive-name "chown-entry"))
415 :help "Change owner of marked files"))
416 (define-key map [menu-bar operate chgrp]
417 '(menu-item "Change Group..." archive-chgrp-entry
418 :enable (fboundp (archive-name "chgrp-entry"))
419 :help "Change group ownership of marked files"))
420 (define-key map [menu-bar operate chmod]
421 '(menu-item "Change Mode..." archive-chmod-entry
422 :enable (fboundp (archive-name "chmod-entry"))
423 :help "Change mode (permissions) of marked files"))
424 (define-key map [menu-bar operate rename]
425 '(menu-item "Rename to..." archive-rename-entry
426 :enable (fboundp (archive-name "rename-entry"))
427 :help "Rename marked files"))
428 ;;(define-key map [menu-bar operate copy]
429 ;; '(menu-item "Copy to..." archive-copy))
430 (define-key map [menu-bar operate expunge]
431 '(menu-item "Expunge Marked Files" archive-expunge
432 :help "Delete all flagged files from archive"))
434 "Local keymap for archive mode listings.")
435 (defvar archive-file-name-indent nil "Column where file names start.")
437 (defvar archive-remote nil "Non-nil if the archive is outside file system.")
438 (make-variable-buffer-local 'archive-remote)
439 (put 'archive-remote 'permanent-local t)
441 (defvar archive-member-coding-system nil "Coding-system of archive member.")
442 (make-variable-buffer-local 'archive-member-coding-system)
444 (defvar archive-alternate-display nil
445 "Non-nil when alternate information is shown.")
446 (make-variable-buffer-local 'archive-alternate-display)
447 (put 'archive-alternate-display 'permanent-local t)
449 (defvar archive-superior-buffer nil "In archive members, points to archive.")
450 (put 'archive-superior-buffer 'permanent-local t)
452 (defvar archive-subfile-mode nil "Non-nil in archive member buffers.")
453 (make-variable-buffer-local 'archive-subfile-mode)
454 (put 'archive-subfile-mode 'permanent-local t)
456 (defvar archive-files nil
457 "Vector of file descriptors.
458 Each descriptor is a vector of the form
459 [EXT-FILE-NAME INT-FILE-NAME CASE-FIDDLED MODE ...]")
460 (make-variable-buffer-local 'archive-files)
462 ;; -------------------------------------------------------------------------
463 ;; Section: Support functions.
465 (defsubst archive-name (suffix)
466 (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
468 (defun archive-l-e (str &optional len)
469 "Convert little endian string/vector STR to integer.
470 Alternatively, STR may be a buffer position in the current buffer
471 in which case a second argument, length LEN, should be supplied."
473 (setq len (length str))
474 (setq str (buffer-substring str (+ str len))))
479 result (+ (ash result 8) (aref str (- len i)))))
482 (defun archive-int-to-mode (mode)
483 "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------."
484 ;; FIXME: merge with tar-grind-file-mode.
486 (if (zerop (logand 8192 mode))
487 (if (zerop (logand 16384 mode)) ?- ?d)
489 (if (zerop (logand 256 mode)) ?- ?r)
490 (if (zerop (logand 128 mode)) ?- ?w)
491 (if (zerop (logand 64 mode))
492 (if (zerop (logand 1024 mode)) ?- ?S)
493 (if (zerop (logand 1024 mode)) ?x ?s))
494 (if (zerop (logand 32 mode)) ?- ?r)
495 (if (zerop (logand 16 mode)) ?- ?w)
496 (if (zerop (logand 8 mode))
497 (if (zerop (logand 2048 mode)) ?- ?S)
498 (if (zerop (logand 2048 mode)) ?x ?s))
499 (if (zerop (logand 4 mode)) ?- ?r)
500 (if (zerop (logand 2 mode)) ?- ?w)
501 (if (zerop (logand 1 mode)) ?- ?x)))
503 (defun archive-calc-mode (oldmode newmode &optional error)
504 "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
505 NEWMODE may be an octal number including a leading zero in which case it
506 will become the new mode.\n
507 NEWMODE may also be a relative specification like \"og-rwx\" in which case
508 OLDMODE will be modified accordingly just like chmod(2) would have done.\n
509 If optional third argument ERROR is non-nil an error will be signaled if
510 the mode is invalid. If ERROR is nil then nil will be returned."
511 (cond ((string-match "^0[0-7]*$" newmode)
513 (len (length newmode))
516 (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
518 (logior (logand oldmode 65024) result)))
519 ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
522 (op (aref newmode (match-beginning 2)))
524 (i (match-beginning 3)))
525 (while (< i (match-end 3))
526 (let ((rwx (aref newmode i)))
527 (setq bits (logior bits (cond ((= rwx ?r) 292)
533 (while (< who (match-end 1))
534 (let* ((whoc (aref newmode who))
535 (whomask (cond ((= whoc ?a) 4095)
540 (setq result (logand result (lognot whomask))))
542 (setq result (logand result (lognot (logand whomask bits))))
543 (setq result (logior result (logand whomask bits)))))
548 (error "Invalid mode specification: %s" newmode)))))
550 (defun archive-dosdate (date)
551 "Stringify dos packed DATE record."
552 (let ((year (+ 1980 (logand (ash date -9) 127)))
553 (month (logand (ash date -5) 15))
554 (day (logand date 31)))
555 (if (or (> month 12) (< month 1))
559 (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
560 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
563 (defun archive-dostime (time)
564 "Stringify dos packed TIME record."
565 (let ((hour (logand (ash time -11) 31))
566 (minute (logand (ash time -5) 63))
567 (second (* 2 (logand time 31)))) ; 2 seconds resolution
568 (format "%02d:%02d:%02d" hour minute second)))
570 (defun archive-unixdate (low high)
571 "Stringify Unix (LOW HIGH) date."
572 (let ((str (current-time-string (cons high low))))
576 (substring str 20 24))))
578 (defun archive-unixtime (low high)
579 "Stringify Unix (LOW HIGH) time."
580 (let ((str (current-time-string (cons high low))))
581 (substring str 11 19)))
583 (defun archive-get-lineno ()
584 (if (>= (point) archive-file-list-start)
585 (count-lines archive-file-list-start
586 (save-excursion (beginning-of-line) (point)))
589 (defun archive-get-descr (&optional noerror)
590 "Return the descriptor vector for file at point.
591 Does not signal an error if optional argument NOERROR is non-nil."
592 (let ((no (archive-get-lineno)))
593 (if (and (>= (point) archive-file-list-start)
594 (< no (length archive-files)))
595 (let ((item (aref archive-files no)))
599 (error "Entry is not a regular member of the archive"))))
601 (error "Line does not describe a member of the archive")))))
602 ;; -------------------------------------------------------------------------
603 ;; Section: the mode definition
606 (defun archive-mode (&optional force)
607 "Major mode for viewing an archive file in a dired-like way.
608 You can move around using the usual cursor motion commands.
609 Letters no longer insert themselves.
610 Type `e' to pull a file out of the archive and into its own buffer;
611 or click mouse-2 on the file's line in the archive mode buffer.
613 If you edit a sub-file of this archive (as with the `e' command) and
614 save it, the contents of that buffer will be saved back into the
617 \\{archive-mode-map}"
618 ;; This is not interactive because you shouldn't be turning this
619 ;; mode on and off. You can corrupt things that way.
620 (if (zerop (buffer-size))
621 ;; At present we cannot create archives from scratch
622 (funcall default-major-mode)
623 (if (and (not force) archive-files) nil
624 (let* ((type (archive-find-type))
625 (typename (capitalize (symbol-name type))))
626 (kill-all-local-variables)
627 (make-local-variable 'archive-subtype)
628 (setq archive-subtype type)
630 ;; Buffer contains treated image of file before the file contents
631 (make-local-variable 'revert-buffer-function)
632 (setq revert-buffer-function 'archive-mode-revert)
635 ;; Remote archives are not written by a hook.
636 (if archive-remote nil
637 (add-hook 'write-contents-functions 'archive-write-file nil t))
639 (make-local-variable 'require-final-newline)
640 (setq require-final-newline nil)
641 (make-local-variable 'local-enable-local-variables)
642 (setq local-enable-local-variables nil)
644 ;; Prevent loss of data when saving the file.
645 (make-local-variable 'file-precious-flag)
646 (setq file-precious-flag t)
648 (make-local-variable 'archive-read-only)
649 ;; Archives which are inside other archives and whose
650 ;; names are invalid for this OS, can't be written.
651 (setq archive-read-only
652 (or (not (file-writable-p (buffer-file-name)))
653 (and archive-subfile-mode
654 (string-match file-name-invalid-regexp
655 (aref archive-subfile-mode 0)))))
657 ;; Should we use a local copy when accessing from outside Emacs?
658 (make-local-variable 'archive-local-name)
660 ;; An archive can contain another archive whose name is invalid
661 ;; on local filesystem. Treat such archives as remote.
664 (or (string-match archive-remote-regexp (buffer-file-name))
665 (string-match file-name-invalid-regexp
666 (buffer-file-name)))))
668 (setq major-mode 'archive-mode)
669 (setq mode-name (concat typename "-Archive"))
670 ;; Run archive-foo-mode-hook and archive-mode-hook
671 (run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
672 (use-local-map archive-mode-map))
674 (make-local-variable 'archive-proper-file-start)
675 (make-local-variable 'archive-file-list-start)
676 (make-local-variable 'archive-file-list-end)
677 (make-local-variable 'archive-file-name-indent)
678 (archive-summarize nil)
679 (setq buffer-read-only t))))
681 ;; Archive mode is suitable only for specially formatted data.
682 (put 'archive-mode 'mode-class 'special)
684 (let ((item1 '(archive-subfile-mode " Archive")))
685 (or (member item1 minor-mode-alist)
686 (setq minor-mode-alist (cons item1 minor-mode-alist))))
687 ;; -------------------------------------------------------------------------
688 (defun archive-find-type ()
690 (goto-char (point-min))
691 ;; The funny [] here make it unlikely that the .elc file will be treated
692 ;; as an archive by other software.
693 (let (case-fold-search)
694 (cond ((looking-at "[P]K\003\004") 'zip)
695 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
696 ((looking-at "....................[\334]\247\304\375") 'zoo)
697 ((and (looking-at "\C-z") ; signature too simple, IMHO
698 (string-match "\\.[aA][rR][cC]$"
699 (or buffer-file-name (buffer-name))))
701 (t (error "Buffer format not recognized")))))
702 ;; -------------------------------------------------------------------------
703 (defun archive-summarize (&optional shut-up)
704 "Parse the contents of the archive file in the current buffer.
705 Place a dired-like listing on the front;
706 then narrow to it, so that only that listing
707 is visible (and the real data of the buffer is hidden).
708 Optional argument SHUT-UP, if non-nil, means don't print messages
709 when parsing the archive."
711 (set-buffer-multibyte nil)
712 (let ((inhibit-read-only t))
714 (message "Parsing archive file..."))
715 (buffer-disable-undo (current-buffer))
716 (setq archive-files (funcall (archive-name "summarize")))
718 (message "Parsing archive file...done."))
719 (setq archive-proper-file-start (point-marker))
720 (narrow-to-region (point-min) (point))
721 (set-buffer-modified-p nil)
722 (buffer-enable-undo))
723 (goto-char archive-file-list-start)
724 (archive-next-line 0))
726 (defun archive-resummarize ()
727 "Recreate the contents listing of an archive."
728 (let ((modified (buffer-modified-p))
729 (no (archive-get-lineno))
730 (inhibit-read-only t))
732 (delete-region (point-min) archive-proper-file-start)
733 (archive-summarize t)
734 (restore-buffer-modified-p modified)
735 (goto-char archive-file-list-start)
736 (archive-next-line no)))
738 (defun archive-summarize-files (files)
739 "Insert a description of a list of files annotated with proper mouse face."
740 (setq archive-file-list-start (point-marker))
741 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
742 ;; We don't want to do an insert for each element since that takes too
743 ;; long when the archive -- which has to be moved in memory -- is large.
749 ;; Using `concat' here copies the text also, so we can add
750 ;; properties without problems.
751 (let ((text (concat (aref fil 0) "\n")))
752 (if (featurep 'xemacs)
755 (aref fil 1) (aref fil 2)
756 '(mouse-face highlight
757 help-echo "mouse-2: extract this file into a buffer")
761 (setq archive-file-list-end (point-marker)))
763 (defun archive-alternate-display ()
764 "Toggle alternative display.
765 To avoid very long lines archive mode does not show all information.
766 This function changes the set of information shown for each files."
768 (setq archive-alternate-display (not archive-alternate-display))
769 (archive-resummarize))
770 ;; -------------------------------------------------------------------------
771 ;; Section: Local archive copy handling
773 (defun archive-unique-fname (fname dir)
774 "Make sure a file FNAME can be created uniquely in directory DIR.
776 If FNAME can be uniquely created in DIR, it is returned unaltered.
777 If FNAME is something our underlying filesystem can't grok, or if another
778 file by that name already exists in DIR, a unique new name is generated
779 using `make-temp-file', and the generated name is returned."
780 (let ((fullname (expand-file-name fname dir))
781 (alien (string-match file-name-invalid-regexp fname)))
782 (if (or alien (file-exists-p fullname))
785 (if (if (fboundp 'msdos-long-file-names)
786 (not (msdos-long-file-names)))
792 (defun archive-maybe-copy (archive)
793 (let ((coding-system-for-write 'no-conversion))
795 (let ((start (point-max))
796 ;; Sometimes ARCHIVE is invalid while its actual name, as
797 ;; recorded in its parent archive, is not. For example, an
798 ;; archive bar.zip inside another archive foo.zip gets a name
799 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
800 ;; So use the actual name if available.
802 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
804 (make-directory archive-tmpdir t)
805 ;; If ARCHIVE includes leading directories, make sure they
806 ;; exist under archive-tmpdir.
807 (let ((arch-dir (file-name-directory archive)))
809 (make-directory (concat
810 (file-name-as-directory archive-tmpdir)
813 (setq archive-local-name
814 (archive-unique-fname archive-name archive-tmpdir))
817 (write-region start (point-max) archive-local-name nil 'nomessage))
819 (if (buffer-modified-p) (save-buffer))
822 (defun archive-maybe-update (unchanged)
824 (let ((name archive-local-name)
825 (modified (buffer-modified-p))
826 (coding-system-for-read 'no-conversion)
827 (lno (archive-get-lineno))
828 (inhibit-read-only t))
830 (setq archive-files nil)
832 (insert-file-contents name)
834 (goto-char archive-file-list-start)
835 (archive-next-line lno))
836 (archive-delete-local name)
839 "Buffer `%s' must be saved for changes to take effect"
840 (buffer-name (current-buffer))))
841 (set-buffer-modified-p (or modified (not unchanged))))))
843 (defun archive-delete-local (name)
844 "Delete file NAME and its parents up to and including `archive-tmpdir'."
846 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
851 (setq name (directory-file-name (file-name-directory name)))
853 (delete-directory name)
855 (if (string= name top) (setq again nil)))))
856 ;; -------------------------------------------------------------------------
857 ;; Section: Member extraction
859 (defun archive-file-name-handler (op &rest args)
860 (or (eq op 'file-exists-p)
861 (let ((file-name-handler-alist nil))
864 (defun archive-set-buffer-as-visiting-file (filename)
865 "Set the current buffer as if it were visiting FILENAME."
867 (goto-char (point-min))
869 (or coding-system-for-read
870 (and set-auto-coding-function
872 (funcall set-auto-coding-function
873 filename (- (point-max) (point-min)))))
874 ;; dos-w32.el defines find-operation-coding-system for
875 ;; DOS/Windows systems which preserves the coding-system
876 ;; of existing files. We want it to act here as if the
877 ;; extracted file existed.
878 (let ((file-name-handler-alist
879 '(("" . archive-file-name-handler))))
880 (car (find-operation-coding-system 'insert-file-contents
882 (if (and (not coding-system-for-read)
883 (not enable-multibyte-characters))
885 (coding-system-change-text-conversion coding 'raw-text)))
887 (not (eq coding 'no-conversion)))
888 (decode-coding-region (point-min) (point-max) coding)
889 (setq last-coding-system-used coding))
890 (set-buffer-modified-p nil)
891 (kill-local-variable 'buffer-file-coding-system)
892 (after-insert-file-set-coding (- (point-max) (point-min))))))
894 (define-obsolete-function-alias 'archive-mouse-extract 'archive-extract "22.1")
896 (defun archive-extract (&optional other-window-p event)
897 "In archive mode, extract this entry of the archive into its own buffer."
898 (interactive (list nil last-input-event))
899 (if event (mouse-set-point event))
900 (let* ((view-p (eq other-window-p 'view))
901 (descr (archive-get-descr))
902 (ename (aref descr 0))
903 (iname (aref descr 1))
904 (archive-buffer (current-buffer))
905 (arcdir default-directory)
906 (archive (buffer-file-name))
907 (arcname (file-name-nondirectory archive))
908 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
909 (extractor (archive-name "extract"))
910 ;; Members with file names which aren't valid for the
911 ;; underlying filesystem, are treated as read-only.
912 (read-only-p (or archive-read-only
914 (string-match file-name-invalid-regexp ename)))
915 (buffer (get-buffer bufname))
919 (setq archive (archive-maybe-copy archive))
920 (setq buffer (get-buffer-create bufname))
921 (setq just-created t)
922 (with-current-buffer buffer
923 (setq buffer-file-name
924 (expand-file-name (concat arcname ":" iname)))
925 (setq buffer-file-truename
926 (abbreviate-file-name buffer-file-name))
927 ;; Set the default-directory to the dir of the superior buffer.
928 (setq default-directory arcdir)
929 (make-local-variable 'archive-superior-buffer)
930 (setq archive-superior-buffer archive-buffer)
931 (add-hook 'write-file-functions 'archive-write-file-member nil t)
932 (setq archive-subfile-mode descr)
935 (let (;; We may have to encode file name arguement for
936 ;; external programs.
937 (coding-system-for-write
938 (and enable-multibyte-characters
939 file-name-coding-system))
940 ;; We read an archive member by no-conversion at
941 ;; first, then decode appropriately by calling
942 ;; archive-set-buffer-as-visiting-file later.
943 (coding-system-for-read 'no-conversion))
945 (if (fboundp extractor)
946 (funcall extractor archive ename)
947 (archive-*-extract archive ename
948 (symbol-value extractor)))
950 (ding (message "%s" (error-message-string err)))
954 (set-buffer-modified-p nil)
955 (kill-buffer buffer))
956 (archive-set-buffer-as-visiting-file ename)
957 (goto-char (point-min))
958 (rename-buffer bufname)
959 (setq buffer-read-only read-only-p)
960 (setq buffer-undo-list nil)
961 (set-buffer-modified-p nil)
962 (setq buffer-saved-size (buffer-size))
964 ;; Just in case an archive occurs inside another archive.
965 (when (derived-mode-p 'archive-mode)
966 (setq archive-remote t)
967 (if read-only-p (setq archive-read-only t))
968 ;; We will write out the archive ourselves if it is
969 ;; part of another archive.
970 (remove-hook 'write-contents-functions 'archive-write-file t))
971 (run-hooks 'archive-extract-hooks)
972 (if archive-read-only
973 (message "Note: altering this archive is not implemented."))))
974 (archive-maybe-update t))
975 (or (not (buffer-name buffer))
977 (view-p (view-buffer buffer (and just-created 'kill-buffer)))
978 ((eq other-window-p 'display) (display-buffer buffer))
979 (other-window-p (switch-to-buffer-other-window buffer))
980 (t (switch-to-buffer buffer))))))
982 (defun archive-*-extract (archive name command)
983 (let* ((default-directory (file-name-as-directory archive-tmpdir))
984 (tmpfile (expand-file-name (file-name-nondirectory name)
987 (make-directory (directory-file-name default-directory) t)
994 (append (cdr command) (list archive name))))
995 (cond ((and (numberp exit-status) (= exit-status 0))
996 (if (not (file-exists-p tmpfile))
997 (ding (message "`%s': no such file or directory" tmpfile))
998 (insert-file-contents tmpfile)
1000 ((numberp exit-status)
1002 (message "`%s' exited with status %d" (car command) exit-status)))
1003 ((stringp exit-status)
1004 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1006 (ding (message "`%s' failed" (car command)))))
1007 (archive-delete-local tmpfile)
1010 (defun archive-extract-by-stdout (archive name command)
1011 (apply 'call-process
1016 (append (cdr command) (list archive name))))
1018 (defun archive-extract-other-window ()
1019 "In archive mode, find this member in another window."
1021 (archive-extract t))
1023 (defun archive-display-other-window ()
1024 "In archive mode, display this member in another window."
1026 (archive-extract 'display))
1028 (defun archive-view ()
1029 "In archive mode, view the member on this line."
1031 (archive-extract 'view))
1033 (defun archive-add-new-member (arcbuf name)
1034 "Add current buffer to the archive in ARCBUF naming it NAME."
1037 (read-buffer "Buffer containing archive: "
1038 ;; Find first archive buffer and suggest that
1039 (let ((bufs (buffer-list)))
1041 (not (with-current-buffer (car bufs)
1042 (derived-mode-p 'archive-mode))))
1043 (setq bufs (cdr bufs)))
1046 (error "There are no archive buffers")))
1048 (read-string "File name in archive: "
1049 (if buffer-file-name
1050 (file-name-nondirectory buffer-file-name)
1052 (with-current-buffer arcbuf
1053 (or (eq major-mode 'archive-mode)
1054 (error "Buffer is not an archive buffer"))
1055 (if archive-read-only
1056 (error "Archive is read-only")))
1057 (if (eq arcbuf (current-buffer))
1058 (error "An archive buffer cannot be added to itself"))
1059 (if (string= name "")
1060 (error "Archive members may not be given empty names"))
1061 (let ((func (with-current-buffer arcbuf
1062 (archive-name "add-new-member")))
1063 (membuf (current-buffer)))
1065 (with-current-buffer arcbuf
1066 (funcall func buffer-file-name membuf name))
1067 (error "Adding a new member is not supported for this archive type"))))
1068 ;; -------------------------------------------------------------------------
1069 ;; Section: IO stuff
1071 (defun archive-write-file-member ()
1074 (message "Updating archive...")
1076 (let ((writer (with-current-buffer archive-superior-buffer
1077 (archive-name "write-file-member")))
1078 (archive (with-current-buffer archive-superior-buffer
1079 (archive-maybe-copy (buffer-file-name)))))
1080 (if (fboundp writer)
1081 (funcall writer archive archive-subfile-mode)
1082 (archive-*-write-file-member archive
1083 archive-subfile-mode
1084 (symbol-value writer)))
1085 (set-buffer-modified-p nil)
1086 (message "Updating archive...done"))
1087 (set-buffer archive-superior-buffer)
1088 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1089 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1090 ;; won't reset the coding-system of this archive member.
1091 (if (local-variable-p 'archive-member-coding-system)
1092 (setq last-coding-system-used archive-member-coding-system))
1095 (defun archive-*-write-file-member (archive descr command)
1096 (let* ((ename (aref descr 0))
1097 (tmpfile (expand-file-name ename archive-tmpdir))
1098 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1099 (default-directory (file-name-as-directory top)))
1102 (make-directory (file-name-directory tmpfile) t)
1103 ;; If the member is itself an archive, write it without
1104 ;; the dired-like listing we created.
1105 (if (eq major-mode 'archive-mode)
1106 (archive-write-file tmpfile)
1107 (write-region (point-min) (point-max) tmpfile nil 'nomessage))
1108 ;; basic-save-buffer needs last-coding-system-used to have
1109 ;; the value used to write the file, so save it before any
1110 ;; further processing clobbers it (we restore it in
1111 ;; archive-write-file-member, above).
1112 (setq archive-member-coding-system last-coding-system-used)
1114 ;; Set the file modes, but make sure we can read it.
1115 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1116 (if enable-multibyte-characters
1118 (encode-coding-string ename file-name-coding-system)))
1119 (let ((exitcode (apply 'call-process
1124 (append (cdr command) (list archive ename)))))
1125 (if (equal exitcode 0)
1127 (error "Updating was unsuccessful (%S)" exitcode))))
1128 (archive-delete-local tmpfile))))
1130 (defun archive-write-file (&optional file)
1132 (let ((coding-system-for-write 'no-conversion))
1133 (write-region archive-proper-file-start (point-max)
1134 (or file buffer-file-name) nil t)
1135 (set-buffer-modified-p nil))
1137 ;; -------------------------------------------------------------------------
1138 ;; Section: Marking and unmarking.
1140 (defun archive-flag-deleted (p &optional type)
1141 "In archive mode, mark this member to be deleted from the archive.
1142 With a prefix argument, mark that many files."
1144 (or type (setq type ?D))
1146 (let ((sign (if (>= p 0) +1 -1))
1147 (modified (buffer-modified-p))
1148 (inhibit-read-only t))
1149 (while (not (zerop p))
1150 (if (archive-get-descr t)
1155 (setq p (- p sign)))
1156 (restore-buffer-modified-p modified))
1157 (archive-next-line 0))
1159 (defun archive-unflag (p)
1160 "In archive mode, un-mark this member if it is marked to be deleted.
1161 With a prefix argument, un-mark that many files forward."
1163 (archive-flag-deleted p ?\s))
1165 (defun archive-unflag-backwards (p)
1166 "In archive mode, un-mark this member if it is marked to be deleted.
1167 With a prefix argument, un-mark that many members backward."
1169 (archive-flag-deleted (- p) ?\s))
1171 (defun archive-unmark-all-files ()
1174 (let ((modified (buffer-modified-p))
1175 (inhibit-read-only t))
1177 (goto-char archive-file-list-start)
1178 (while (< (point) archive-file-list-end)
1179 (or (= (following-char) ?\s)
1180 (progn (delete-char 1) (insert ?\s)))
1182 (restore-buffer-modified-p modified)))
1184 (defun archive-mark (p)
1185 "In archive mode, mark this member for group operations.
1186 With a prefix argument, mark that many members.
1187 Use \\[archive-unmark-all-files] to remove all marks."
1189 (archive-flag-deleted p ?*))
1191 (defun archive-get-marked (mark &optional default)
1194 (goto-char archive-file-list-start)
1195 (while (< (point) archive-file-list-end)
1196 (if (= (following-char) mark)
1197 (setq files (cons (archive-get-descr) files)))
1199 (or (nreverse files)
1201 (list (archive-get-descr))))))
1202 ;; -------------------------------------------------------------------------
1205 (defun archive-next-line (p)
1209 (forward-char archive-file-name-indent)))
1211 (defun archive-previous-line (p)
1213 (archive-next-line (- p)))
1215 (defun archive-chmod-entry (new-mode)
1216 "Change the protection bits associated with all marked or this member.
1217 The new protection bits can either be specified as an octal number or
1218 as a relative change like \"g+rw\" as for chmod(2)."
1219 (interactive "sNew mode (octal or relative): ")
1220 (if archive-read-only (error "Archive is read-only"))
1221 (let ((func (archive-name "chmod-entry")))
1224 (funcall func new-mode (archive-get-marked ?* t))
1225 (archive-resummarize))
1226 (error "Setting mode bits is not supported for this archive type"))))
1228 (defun archive-chown-entry (new-uid)
1229 "Change the owner of all marked or this member."
1230 (interactive "nNew uid: ")
1231 (if archive-read-only (error "Archive is read-only"))
1232 (let ((func (archive-name "chown-entry")))
1235 (funcall func new-uid (archive-get-marked ?* t))
1236 (archive-resummarize))
1237 (error "Setting owner is not supported for this archive type"))))
1239 (defun archive-chgrp-entry (new-gid)
1240 "Change the group of all marked or this member."
1241 (interactive "nNew gid: ")
1242 (if archive-read-only (error "Archive is read-only"))
1243 (let ((func (archive-name "chgrp-entry")))
1246 (funcall func new-gid (archive-get-marked ?* t))
1247 (archive-resummarize))
1248 (error "Setting group is not supported for this archive type"))))
1250 (defun archive-expunge ()
1251 "Do the flagged deletions."
1255 (goto-char archive-file-list-start)
1256 (while (< (point) archive-file-list-end)
1257 (if (= (following-char) ?D)
1258 (setq files (cons (aref (archive-get-descr) 0) files)))
1260 (setq files (nreverse files))
1262 (or (not archive-read-only)
1263 (error "Archive is read-only"))
1264 (or (yes-or-no-p (format "Really delete %d member%s? "
1266 (if (null (cdr files)) "" "s")))
1267 (error "Operation aborted"))
1268 (let ((archive (archive-maybe-copy (buffer-file-name)))
1269 (expunger (archive-name "expunge")))
1270 (if (fboundp expunger)
1271 (funcall expunger archive files)
1272 (archive-*-expunge archive files (symbol-value expunger)))
1273 (archive-maybe-update nil)
1275 (archive-resummarize)
1276 (revert-buffer))))))
1278 (defun archive-*-expunge (archive files command)
1279 (apply 'call-process
1284 (append (cdr command) (cons archive files))))
1286 (defun archive-rename-entry (newname)
1287 "Change the name associated with this entry in the archive file."
1288 (interactive "sNew name: ")
1289 (if archive-read-only (error "Archive is read-only"))
1290 (if (string= newname "")
1291 (error "Archive members may not be given empty names"))
1292 (let ((func (archive-name "rename-entry"))
1293 (descr (archive-get-descr)))
1297 (if enable-multibyte-characters
1298 (encode-coding-string newname file-name-coding-system)
1301 (archive-resummarize))
1302 (error "Renaming is not supported for this archive type"))))
1304 ;; Revert the buffer and recompute the dired-like listing.
1305 (defun archive-mode-revert (&optional no-auto-save no-confirm)
1306 (let ((no (archive-get-lineno)))
1307 (setq archive-files nil)
1308 (let ((revert-buffer-function nil)
1309 (coding-system-for-read 'no-conversion))
1310 (set-buffer-multibyte nil)
1311 (revert-buffer t t))
1313 (goto-char archive-file-list-start)
1314 (archive-next-line no)))
1316 (defun archive-undo ()
1317 "Undo in an archive buffer.
1318 This doesn't recover lost files, it just undoes changes in the buffer itself."
1320 (let ((inhibit-read-only t))
1322 ;; -------------------------------------------------------------------------
1323 ;; Section: Arc Archives
1325 (defun archive-arc-summarize ()
1331 (while (and (< (+ p 29) (point-max))
1332 (= (char-after p) ?\C-z)
1333 (> (char-after (1+ p)) 0))
1334 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1335 (fnlen (or (string-match "\0" namefld) 13))
1336 (efnname (substring namefld 0 fnlen))
1337 (csize (archive-l-e (+ p 15) 4))
1338 (moddate (archive-l-e (+ p 19) 2))
1339 (modtime (archive-l-e (+ p 21) 2))
1340 (ucsize (archive-l-e (+ p 25) 4))
1341 (fiddle (string= efnname (upcase efnname)))
1342 (ifnname (if fiddle (downcase efnname) efnname))
1343 (text (format " %8d %-11s %-8s %s"
1345 (archive-dosdate moddate)
1346 (archive-dostime modtime)
1348 (setq maxlen (max maxlen fnlen)
1349 totalsize (+ totalsize ucsize)
1350 visual (cons (vector text
1351 (- (length text) (length ifnname))
1354 files (cons (vector efnname ifnname fiddle nil (1- p))
1357 (goto-char (point-min))
1358 (let ((dash (concat "- -------- ----------- -------- "
1359 (make-string maxlen ?-)
1361 (insert "M Length Date Time File\n"
1363 (archive-summarize-files (nreverse visual))
1365 (format " %8d %d file%s"
1368 (if (= 1 (length files)) "" "s"))
1370 (apply 'vector (nreverse files))))
1372 (defun archive-arc-rename-entry (newname descr)
1373 (if (string-match "[:\\\\/]" newname)
1374 (error "File names in arc files must not contain a directory component"))
1375 (if (> (length newname) 12)
1376 (error "File names in arc files are limited to 12 characters"))
1377 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1379 (inhibit-read-only t))
1383 (set-buffer-multibyte nil)
1384 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1387 ;; -------------------------------------------------------------------------
1388 ;; Section: Lzh Archives
1390 (defun archive-lzh-summarize ()
1396 (while (progn (goto-char p) ;beginning of a base header.
1397 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1398 (let* ((hsize (char-after p)) ;size of the base header (level 0 and 1)
1399 (csize (archive-l-e (+ p 7) 4)) ;size of a compressed file to follow (level 0 and 2),
1400 ;size of extended headers + the compressed file to follow (level 1).
1401 (ucsize (archive-l-e (+ p 11) 4)) ;size of an uncompressed file.
1402 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1403 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
1404 (hdrlvl (char-after (+ p 20))) ;header level
1405 thsize ;total header size (base + extensions)
1406 fnlen efnname fiddle ifnname width p2
1407 neh ;beginning of next extension header (level 1 and 2)
1408 mode modestr uid gid text dir prname
1409 gname uname modtime moddate)
1410 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1411 (when (or (= hdrlvl 0) (= hdrlvl 1))
1412 (setq fnlen (char-after (+ p 21))) ;filename length
1413 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
1414 (if file-name-coding-system
1415 (decode-coding-string str file-name-coding-system)
1416 (string-as-multibyte str))))
1417 (setq p2 (+ p 22 fnlen))) ;
1419 (setq neh (+ p2 3)) ;specific to level 1 header
1421 (setq neh (+ p 24)))) ;specific to level 2 header
1422 (if neh ;if level 1 or 2 we expect extension headers to follow
1423 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
1424 (etype (char-after (+ neh 2)))) ;extension type
1425 (while (not (= ehsize 0))
1427 ((= etype 1) ;file name
1428 (let ((i (+ neh 3)))
1429 (while (< i (+ neh ehsize))
1430 (setq efnname (concat efnname (char-to-string (char-after i))))
1432 ((= etype 2) ;directory name
1433 (let ((i (+ neh 3)))
1434 (while (< i (+ neh ehsize))
1435 (setq dir (concat dir
1436 (if (= (char-after i)
1442 ((= etype 80) ;Unix file permission
1443 (setq mode (archive-l-e (+ neh 3) 2)))
1444 ((= etype 81) ;UNIX file group/user ID
1445 (progn (setq uid (archive-l-e (+ neh 3) 2))
1446 (setq gid (archive-l-e (+ neh 5) 2))))
1447 ((= etype 82) ;UNIX file group name
1448 (let ((i (+ neh 3)))
1449 (while (< i (+ neh ehsize))
1450 (setq gname (concat gname (char-to-string (char-after i))))
1452 ((= etype 83) ;UNIX file user name
1453 (let ((i (+ neh 3)))
1454 (while (< i (+ neh ehsize))
1455 (setq uname (concat uname (char-to-string (char-after i))))
1458 (setq neh (+ neh ehsize))
1459 (setq ehsize (archive-l-e neh 2))
1460 (setq etype (char-after (+ neh 2))))
1461 ;;get total header size for level 1 and 2 headers
1462 (setq thsize (- neh p))))
1463 (if (= hdrlvl 0) ;total header size
1464 (setq thsize hsize))
1465 (setq fiddle (if efnname (string= efnname (upcase efnname))))
1466 (setq ifnname (if fiddle (downcase efnname) efnname))
1467 (setq prname (if dir (concat dir ifnname) ifnname))
1468 (setq width (if prname (string-width prname) 0))
1469 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1470 (setq moddate (if (= hdrlvl 2)
1471 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1472 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1473 (setq modtime (if (= hdrlvl 2)
1474 (archive-unixtime time1 time2)
1475 (archive-dostime time1)))
1476 (setq text (if archive-alternate-display
1477 (format " %8d %5S %5S %s"
1482 (format " %10s %8d %-11s %-8s %s"
1488 (setq maxlen (max maxlen width)
1489 totalsize (+ totalsize ucsize)
1490 visual (cons (vector text
1491 (- (length text) (length prname))
1494 files (cons (vector prname ifnname fiddle mode (1- p))
1497 (setq p (+ p hsize 2 csize)))
1498 ((or (= hdrlvl 2) (= hdrlvl 0))
1499 (setq p (+ p thsize 2 csize))))
1501 (goto-char (point-min))
1502 (set-buffer-multibyte default-enable-multibyte-characters)
1503 (let ((dash (concat (if archive-alternate-display
1504 "- -------- ----- ----- "
1505 "- ---------- -------- ----------- -------- ")
1506 (make-string maxlen ?-)
1508 (header (if archive-alternate-display
1509 "M Length Uid Gid File\n"
1510 "M Filemode Length Date Time File\n"))
1511 (sumline (if archive-alternate-display
1514 (insert header dash)
1515 (archive-summarize-files (nreverse visual))
1520 (if (= 1 (length files)) "" "s"))
1522 (apply 'vector (nreverse files))))
1524 (defconst archive-lzh-alternate-display t)
1526 (defun archive-lzh-extract (archive name)
1527 (archive-extract-by-stdout archive name archive-lzh-extract))
1529 (defun archive-lzh-resum (p count)
1532 (setq count (1- count)
1533 sum (+ sum (char-after p))
1537 (defun archive-lzh-rename-entry (newname descr)
1541 (set-buffer-multibyte nil)
1542 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1543 (oldhsize (char-after p))
1544 (oldfnlen (char-after (+ p 21)))
1545 (newfnlen (length newname))
1546 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1547 (inhibit-read-only t))
1548 (if (> newhsize 255)
1549 (error "The file name is too long"))
1550 (goto-char (+ p 21))
1551 (delete-char (1+ oldfnlen))
1552 (insert newfnlen newname)
1555 (insert newhsize (archive-lzh-resum p newhsize))))))
1557 (defun archive-lzh-ogm (newval files errtxt ofs)
1561 (set-buffer-multibyte nil)
1563 (let* ((p (+ archive-proper-file-start (aref fil 4)))
1564 (hsize (char-after p))
1565 (fnlen (char-after (+ p 21)))
1567 (creator (if (>= (- hsize fnlen) 24) (char-after (+ p2 2)) 0))
1568 (inhibit-read-only t))
1571 (or (numberp newval)
1572 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1573 (goto-char (+ p2 ofs))
1575 (insert (logand newval 255) (lsh newval -8))
1578 (insert (archive-lzh-resum (1+ p) hsize)))
1579 (message "Member %s does not have %s field"
1580 (aref fil 1) errtxt)))))))
1582 (defun archive-lzh-chown-entry (newuid files)
1583 (archive-lzh-ogm newuid files "an uid" 10))
1585 (defun archive-lzh-chgrp-entry (newgid files)
1586 (archive-lzh-ogm newgid files "a gid" 12))
1588 (defun archive-lzh-chmod-entry (newmode files)
1590 ;; This should work even though newmode will be dynamically accessed.
1591 (lambda (old) (archive-calc-mode old newmode t))
1592 files "a unix-style mode" 8))
1593 ;; -------------------------------------------------------------------------
1594 ;; Section: Zip Archives
1596 (defun archive-zip-summarize ()
1597 (goto-char (- (point-max) (- 22 18)))
1598 (search-backward-regexp "[P]K\005\006")
1599 (let ((p (+ (point-min) (archive-l-e (+ (point) 16) 4)))
1604 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1605 (let* ((creator (char-after (+ p 5)))
1606 ;; (method (archive-l-e (+ p 10) 2))
1607 (modtime (archive-l-e (+ p 12) 2))
1608 (moddate (archive-l-e (+ p 14) 2))
1609 (ucsize (archive-l-e (+ p 24) 4))
1610 (fnlen (archive-l-e (+ p 28) 2))
1611 (exlen (archive-l-e (+ p 30) 2))
1612 (fclen (archive-l-e (+ p 32) 2))
1613 (lheader (archive-l-e (+ p 42) 4))
1614 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1615 (if file-name-coding-system
1616 (decode-coding-string str file-name-coding-system)
1617 (string-as-multibyte str))))
1618 (isdir (and (= ucsize 0)
1619 (string= (file-name-nondirectory efnname) "")))
1620 (mode (cond ((memq creator '(2 3)) ; Unix + VMS
1621 (archive-l-e (+ p 40) 2))
1622 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1624 (if isdir (logior 16384 ?\111) 0)
1626 (logand 1 (char-after (+ p 38))))
1629 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1630 (fiddle (and archive-zip-case-fiddle
1631 (not (not (memq creator '(0 2 4 5 9))))
1632 (string= (upcase efnname) efnname)))
1633 (ifnname (if fiddle (downcase efnname) efnname))
1634 (width (string-width ifnname))
1635 (text (format " %10s %8d %-11s %-8s %s"
1638 (archive-dosdate moddate)
1639 (archive-dostime modtime)
1641 (setq maxlen (max maxlen width)
1642 totalsize (+ totalsize ucsize)
1643 visual (cons (vector text
1644 (- (length text) (length ifnname))
1647 files (cons (if isdir
1649 (vector efnname ifnname fiddle mode
1650 (list (1- p) lheader)))
1652 p (+ p 46 fnlen exlen fclen))))
1653 (goto-char (point-min))
1654 (let ((dash (concat "- ---------- -------- ----------- -------- "
1655 (make-string maxlen ?-)
1657 (insert "M Filemode Length Date Time File\n"
1659 (archive-summarize-files (nreverse visual))
1661 (format " %8d %d file%s"
1664 (if (= 1 (length files)) "" "s"))
1666 (apply 'vector (nreverse files))))
1668 (defun archive-zip-extract (archive name)
1669 (if (equal (car archive-zip-extract) "pkzip")
1670 (archive-*-extract archive name archive-zip-extract)
1671 (archive-extract-by-stdout archive name archive-zip-extract)))
1673 (defun archive-zip-write-file-member (archive descr)
1674 (archive-*-write-file-member
1677 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1679 (defun archive-zip-chmod-entry (newmode files)
1683 (set-buffer-multibyte nil)
1685 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
1686 (creator (char-after (+ p 5)))
1687 (oldmode (aref fil 3))
1688 (newval (archive-calc-mode oldmode newmode t))
1689 (inhibit-read-only t))
1690 (cond ((memq creator '(2 3)) ; Unix + VMS
1691 (goto-char (+ p 40))
1693 (insert (logand newval 255) (lsh newval -8)))
1694 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1695 (goto-char (+ p 38))
1696 (insert (logior (logand (char-after (point)) 254)
1697 (logand (logxor 1 (lsh newval -7)) 1)))
1699 (t (message "Don't know how to change mode for this member"))))
1701 ;; -------------------------------------------------------------------------
1702 ;; Section: Zoo Archives
1704 (defun archive-zoo-summarize ()
1705 (let ((p (1+ (archive-l-e 25 4)))
1710 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1711 (> (archive-l-e (+ p 6) 4) 0))
1712 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1713 (moddate (archive-l-e (+ p 14) 2))
1714 (modtime (archive-l-e (+ p 16) 2))
1715 (ucsize (archive-l-e (+ p 20) 4))
1716 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1717 (dirtype (char-after (+ p 4)))
1718 (lfnlen (if (= dirtype 2) (char-after (+ p 56)) 0))
1719 (ldirlen (if (= dirtype 2) (char-after (+ p 57)) 0))
1720 (fnlen (or (string-match "\0" namefld) 13))
1724 (concat (buffer-substring
1726 (+ p 58 lfnlen ldirlen -1))
1730 (buffer-substring (+ p 58)
1732 (substring namefld 0 fnlen)))))
1733 (if file-name-coding-system
1734 (decode-coding-string str file-name-coding-system)
1735 (string-as-multibyte str))))
1736 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1737 (ifnname (if fiddle (downcase efnname) efnname))
1738 (width (string-width ifnname))
1739 (text (format " %8d %-11s %-8s %s"
1741 (archive-dosdate moddate)
1742 (archive-dostime modtime)
1744 (setq maxlen (max maxlen width)
1745 totalsize (+ totalsize ucsize)
1746 visual (cons (vector text
1747 (- (length text) (length ifnname))
1750 files (cons (vector efnname ifnname fiddle nil (1- p))
1753 (goto-char (point-min))
1754 (let ((dash (concat "- -------- ----------- -------- "
1755 (make-string maxlen ?-)
1757 (insert "M Length Date Time File\n"
1759 (archive-summarize-files (nreverse visual))
1761 (format " %8d %d file%s"
1764 (if (= 1 (length files)) "" "s"))
1766 (apply 'vector (nreverse files))))
1768 (defun archive-zoo-extract (archive name)
1769 (archive-extract-by-stdout archive name archive-zoo-extract))
1770 ;; -------------------------------------------------------------------------
1771 ;; This line was a mistake; it is kept now for compatibility.
1773 (provide 'archive-mode)
1777 ;; arch-tag: e5966a01-35ec-4f27-8095-a043a79b457b
1778 ;;; arc-mode.el ends here