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