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