* maintaining.texi (Registering, Tag Syntax): Tweak line and page breaks.
[emacs.git] / lisp / arc-mode.el
blob8b17208983f37985745c676443dc428d018b3a60
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 ((str (current-time-string (cons high low))))
626 (format "%s-%s-%s"
627 (substring str 8 10)
628 (substring str 4 7)
629 (substring str 20 24))))
631 (defun archive-unixtime (low high)
632 "Stringify Unix (LOW HIGH) time."
633 (let ((str (current-time-string (cons high low))))
634 (substring str 11 19)))
636 (defun archive-get-lineno ()
637 (if (>= (point) archive-file-list-start)
638 (count-lines archive-file-list-start
639 (line-beginning-position))
642 (defun archive-get-descr (&optional noerror)
643 "Return the descriptor vector for file at point.
644 Does not signal an error if optional argument NOERROR is non-nil."
645 (let ((no (archive-get-lineno)))
646 (if (and (>= (point) archive-file-list-start)
647 (< no (length archive-files)))
648 (let ((item (aref archive-files no)))
649 (if (vectorp item)
650 item
651 (if (not noerror)
652 (error "Entry is not a regular member of the archive"))))
653 (if (not noerror)
654 (error "Line does not describe a member of the archive")))))
655 ;; -------------------------------------------------------------------------
656 ;;; Section: the mode definition
658 ;;;###autoload
659 (defun archive-mode (&optional force)
660 "Major mode for viewing an archive file in a dired-like way.
661 You can move around using the usual cursor motion commands.
662 Letters no longer insert themselves.
663 Type `e' to pull a file out of the archive and into its own buffer;
664 or click mouse-2 on the file's line in the archive mode buffer.
666 If you edit a sub-file of this archive (as with the `e' command) and
667 save it, the contents of that buffer will be saved back into the
668 archive.
670 \\{archive-mode-map}"
671 ;; This is not interactive because you shouldn't be turning this
672 ;; mode on and off. You can corrupt things that way.
673 (if (zerop (buffer-size))
674 ;; At present we cannot create archives from scratch
675 (funcall (or (default-value 'major-mode) 'fundamental-mode))
676 (if (and (not force) archive-files) nil
677 (let* ((type (archive-find-type))
678 (typename (capitalize (symbol-name type))))
679 (kill-all-local-variables)
680 (make-local-variable 'archive-subtype)
681 (setq archive-subtype type)
683 ;; Buffer contains treated image of file before the file contents
684 (make-local-variable 'revert-buffer-function)
685 (setq revert-buffer-function 'archive-mode-revert)
686 (auto-save-mode 0)
688 ;; Remote archives are not written by a hook.
689 (if archive-remote nil
690 (add-hook 'write-contents-functions 'archive-write-file nil t))
692 (make-local-variable 'require-final-newline)
693 (setq require-final-newline nil)
694 (make-local-variable 'local-enable-local-variables)
695 (setq local-enable-local-variables nil)
697 ;; Prevent loss of data when saving the file.
698 (make-local-variable 'file-precious-flag)
699 (setq file-precious-flag t)
701 (make-local-variable 'archive-read-only)
702 ;; Archives which are inside other archives and whose
703 ;; names are invalid for this OS, can't be written.
704 (setq archive-read-only
705 (or (not (file-writable-p (buffer-file-name)))
706 (and archive-subfile-mode
707 (string-match file-name-invalid-regexp
708 (aref archive-subfile-mode 0)))))
710 ;; Should we use a local copy when accessing from outside Emacs?
711 (make-local-variable 'archive-local-name)
713 ;; An archive can contain another archive whose name is invalid
714 ;; on local filesystem. Treat such archives as remote.
715 (or archive-remote
716 (setq archive-remote
717 (or (string-match archive-remote-regexp (buffer-file-name))
718 (string-match file-name-invalid-regexp
719 (buffer-file-name)))))
721 (setq major-mode 'archive-mode)
722 (setq mode-name (concat typename "-Archive"))
723 ;; Run archive-foo-mode-hook and archive-mode-hook
724 (run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
725 (use-local-map archive-mode-map))
727 (make-local-variable 'archive-proper-file-start)
728 (make-local-variable 'archive-file-list-start)
729 (make-local-variable 'archive-file-list-end)
730 (make-local-variable 'archive-file-name-indent)
731 (setq archive-file-name-coding-system
732 (or file-name-coding-system
733 default-file-name-coding-system
734 locale-coding-system))
735 (if (default-value 'enable-multibyte-characters)
736 (set-buffer-multibyte 'to))
737 (archive-summarize nil)
738 (setq buffer-read-only t))))
740 ;; Archive mode is suitable only for specially formatted data.
741 (put 'archive-mode 'mode-class 'special)
743 (let ((item1 '(archive-subfile-mode " Archive")))
744 (or (member item1 minor-mode-alist)
745 (setq minor-mode-alist (cons item1 minor-mode-alist))))
746 ;; -------------------------------------------------------------------------
747 (defun archive-find-type ()
748 (widen)
749 (goto-char (point-min))
750 ;; The funny [] here make it unlikely that the .elc file will be treated
751 ;; as an archive by other software.
752 (let (case-fold-search)
753 (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
754 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
755 ((looking-at "....................[\334]\247\304\375") 'zoo)
756 ((and (looking-at "\C-z") ; signature too simple, IMHO
757 (string-match "\\.[aA][rR][cC]$"
758 (or buffer-file-name (buffer-name))))
759 'arc)
760 ;; This pattern modeled on the BSD/GNU+Linux `file' command.
761 ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
762 ;; Note this regexp is also in archive-exe-p.
763 ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
764 ((looking-at "Rar!") 'rar)
765 ((looking-at "!<arch>\n") 'ar)
766 ((and (looking-at "MZ")
767 (re-search-forward "Rar!" (+ (point) 100000) t))
768 'rar-exe)
769 ((looking-at "7z\274\257\047\034") '7z)
770 (t (error "Buffer format not recognized")))))
771 ;; -------------------------------------------------------------------------
773 (defun archive-desummarize ()
774 (let ((inhibit-read-only t)
775 (modified (buffer-modified-p)))
776 (widen)
777 (delete-region (point-min) archive-proper-file-start)
778 (restore-buffer-modified-p modified)))
781 (defun archive-summarize (&optional shut-up)
782 "Parse the contents of the archive file in the current buffer.
783 Place a dired-like listing on the front;
784 then narrow to it, so that only that listing
785 is visible (and the real data of the buffer is hidden).
786 Optional argument SHUT-UP, if non-nil, means don't print messages
787 when parsing the archive."
788 (widen)
789 (let ((inhibit-read-only t))
790 (setq archive-proper-file-start (copy-marker (point-min) t))
791 (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
792 (or shut-up
793 (message "Parsing archive file..."))
794 (buffer-disable-undo (current-buffer))
795 (setq archive-files (funcall (archive-name "summarize")))
796 (or shut-up
797 (message "Parsing archive file...done."))
798 (setq archive-proper-file-start (point-marker))
799 (narrow-to-region (point-min) (point))
800 (set-buffer-modified-p nil)
801 (buffer-enable-undo))
802 (goto-char archive-file-list-start)
803 (archive-next-line 0))
805 (defun archive-resummarize ()
806 "Recreate the contents listing of an archive."
807 (let ((no (archive-get-lineno)))
808 (archive-desummarize)
809 (archive-summarize t)
810 (goto-char archive-file-list-start)
811 (archive-next-line no)))
813 (defun archive-summarize-files (files)
814 "Insert a description of a list of files annotated with proper mouse face."
815 (setq archive-file-list-start (point-marker))
816 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
817 ;; We don't want to do an insert for each element since that takes too
818 ;; long when the archive -- which has to be moved in memory -- is large.
819 (insert
820 (apply
821 (function concat)
822 (mapcar
823 (lambda (fil)
824 ;; Using `concat' here copies the text also, so we can add
825 ;; properties without problems.
826 (let ((text (concat (aref fil 0) "\n")))
827 (if (featurep 'xemacs)
828 () ; out of luck
829 (add-text-properties
830 (aref fil 1) (aref fil 2)
831 '(mouse-face highlight
832 help-echo "mouse-2: extract this file into a buffer")
833 text))
834 text))
835 files)))
836 (setq archive-file-list-end (point-marker)))
838 (defun archive-alternate-display ()
839 "Toggle alternative display.
840 To avoid very long lines archive mode does not show all information.
841 This function changes the set of information shown for each files."
842 (interactive)
843 (setq archive-alternate-display (not archive-alternate-display))
844 (archive-resummarize))
845 ;; -------------------------------------------------------------------------
846 ;;; Section: Local archive copy handling
848 (defun archive-unique-fname (fname dir)
849 "Make sure a file FNAME can be created uniquely in directory DIR.
851 If FNAME can be uniquely created in DIR, it is returned unaltered.
852 If FNAME is something our underlying filesystem can't grok, or if another
853 file by that name already exists in DIR, a unique new name is generated
854 using `make-temp-file', and the generated name is returned."
855 (let ((fullname (expand-file-name fname dir))
856 (alien (string-match file-name-invalid-regexp fname))
857 (tmpfile
858 (expand-file-name
859 (if (if (fboundp 'msdos-long-file-names)
860 (not (msdos-long-file-names)))
861 "am"
862 "arc-mode.")
863 dir)))
864 (if (or alien (file-exists-p fullname))
865 (progn
866 ;; Make sure all the leading directories in
867 ;; archive-local-name exist under archive-tmpdir, so that
868 ;; the directory structure recorded in the archive is
869 ;; reconstructed in the temporary directory.
870 (make-directory (file-name-directory tmpfile) t)
871 (make-temp-file tmpfile))
872 ;; Make sure all the leading directories in `fullname' exist
873 ;; under archive-tmpdir. This is necessary for nested archives
874 ;; (`archive-extract' sets `archive-remote' to t in case
875 ;; an archive occurs inside another archive).
876 (make-directory (file-name-directory fullname) t)
877 fullname)))
879 (defun archive-maybe-copy (archive)
880 (let ((coding-system-for-write 'no-conversion))
881 (if archive-remote
882 (let ((start (point-max))
883 ;; Sometimes ARCHIVE is invalid while its actual name, as
884 ;; recorded in its parent archive, is not. For example, an
885 ;; archive bar.zip inside another archive foo.zip gets a name
886 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
887 ;; So use the actual name if available.
888 (archive-name
889 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
890 archive)))
891 (setq archive-local-name
892 (archive-unique-fname archive-name archive-tmpdir))
893 (save-restriction
894 (widen)
895 (write-region start (point-max) archive-local-name nil 'nomessage))
896 archive-local-name)
897 (if (buffer-modified-p) (save-buffer))
898 archive)))
900 (defun archive-maybe-update (unchanged)
901 (if archive-remote
902 (let ((name archive-local-name)
903 (modified (buffer-modified-p))
904 (coding-system-for-read 'no-conversion)
905 (lno (archive-get-lineno))
906 (inhibit-read-only t))
907 (if unchanged nil
908 (setq archive-files nil)
909 (erase-buffer)
910 (insert-file-contents name)
911 (archive-mode t)
912 (goto-char archive-file-list-start)
913 (archive-next-line lno))
914 (archive-delete-local name)
915 (if (not unchanged)
916 (message
917 "Buffer `%s' must be saved for changes to take effect"
918 (buffer-name (current-buffer))))
919 (set-buffer-modified-p (or modified (not unchanged))))))
921 (defun archive-delete-local (name)
922 "Delete file NAME and its parents up to and including `archive-tmpdir'."
923 (let ((again t)
924 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
925 (condition-case nil
926 (delete-file name)
927 (error nil))
928 (while again
929 (setq name (directory-file-name (file-name-directory name)))
930 (condition-case nil
931 (delete-directory name)
932 (error nil))
933 (if (string= name top) (setq again nil)))))
934 ;; -------------------------------------------------------------------------
935 ;;; Section: Member extraction
937 (defun archive-try-jka-compr ()
938 (when (and auto-compression-mode
939 (jka-compr-get-compression-info buffer-file-name))
940 (let* ((basename (file-name-nondirectory buffer-file-name))
941 (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
942 (match-string 1 basename) basename))
943 (tmpfile (make-temp-file (file-name-sans-extension tmpname)
945 (file-name-extension tmpname 'period))))
946 (unwind-protect
947 (progn
948 (let ((coding-system-for-write 'no-conversion)
949 ;; Don't re-compress this data just before decompressing it.
950 (jka-compr-inhibit t))
951 (write-region (point-min) (point-max) tmpfile nil 'quiet))
952 (erase-buffer)
953 (let ((coding-system-for-read 'no-conversion))
954 (insert-file-contents tmpfile)))
955 (delete-file tmpfile)))))
957 (defun archive-file-name-handler (op &rest args)
958 (or (eq op 'file-exists-p)
959 (let ((file-name-handler-alist nil))
960 (apply op args))))
962 (defun archive-set-buffer-as-visiting-file (filename)
963 "Set the current buffer as if it were visiting FILENAME."
964 (save-excursion
965 (goto-char (point-min))
966 (let ((buffer-undo-list t)
967 (coding
968 (or coding-system-for-read
969 (and set-auto-coding-function
970 (save-excursion
971 (funcall set-auto-coding-function
972 filename (- (point-max) (point-min)))))
973 ;; dos-w32.el defines the function
974 ;; find-buffer-file-type-coding-system for DOS/Windows
975 ;; systems which preserves the coding-system of existing files.
976 ;; (That function is called via file-coding-system-alist.)
977 ;; Here, we want it to act as if the extracted file existed.
978 ;; The following let-binding of file-name-handler-alist forces
979 ;; find-file-not-found-set-buffer-file-coding-system to ignore
980 ;; the file's name (see dos-w32.el).
981 (let ((file-name-handler-alist
982 '(("" . archive-file-name-handler))))
983 (car (find-operation-coding-system
984 'insert-file-contents
985 (cons filename (current-buffer)) t))))))
986 (unless (or coding-system-for-read
987 enable-multibyte-characters)
988 (setq coding
989 (coding-system-change-text-conversion coding 'raw-text)))
990 (unless (memq coding '(nil no-conversion))
991 (decode-coding-region (point-min) (point-max) coding)
992 (setq last-coding-system-used coding))
993 (set-buffer-modified-p nil)
994 (kill-local-variable 'buffer-file-coding-system)
995 (after-insert-file-set-coding (- (point-max) (point-min))))))
997 (define-obsolete-function-alias 'archive-mouse-extract 'archive-extract "22.1")
999 (defun archive-extract (&optional other-window-p event)
1000 "In archive mode, extract this entry of the archive into its own buffer."
1001 (interactive (list nil last-input-event))
1002 (if event (posn-set-point (event-end event)))
1003 (let* ((view-p (eq other-window-p 'view))
1004 (descr (archive-get-descr))
1005 (ename (aref descr 0))
1006 (iname (aref descr 1))
1007 (archive-buffer (current-buffer))
1008 (arcdir default-directory)
1009 (archive (buffer-file-name))
1010 (arcname (file-name-nondirectory archive))
1011 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
1012 (extractor (archive-name "extract"))
1013 ;; Members with file names which aren't valid for the
1014 ;; underlying filesystem, are treated as read-only.
1015 (read-only-p (or archive-read-only
1016 view-p
1017 (string-match file-name-invalid-regexp ename)))
1018 (arcfilename (expand-file-name (concat arcname ":" iname)))
1019 (buffer (get-buffer bufname))
1020 (just-created nil)
1021 (file-name-coding archive-file-name-coding-system))
1022 (if (and buffer
1023 (string= (buffer-file-name buffer) arcfilename))
1025 (setq archive (archive-maybe-copy archive))
1026 (setq bufname (generate-new-buffer-name bufname))
1027 (setq buffer (get-buffer-create bufname))
1028 (setq just-created t)
1029 (with-current-buffer buffer
1030 (setq buffer-file-name arcfilename)
1031 (setq buffer-file-truename
1032 (abbreviate-file-name buffer-file-name))
1033 ;; Set the default-directory to the dir of the superior buffer.
1034 (setq default-directory arcdir)
1035 (make-local-variable 'archive-superior-buffer)
1036 (setq archive-superior-buffer archive-buffer)
1037 (add-hook 'write-file-functions 'archive-write-file-member nil t)
1038 (setq archive-subfile-mode descr)
1039 (setq archive-file-name-coding-system file-name-coding)
1040 (if (and
1041 (null
1042 (let (;; We may have to encode the file name argument for
1043 ;; external programs.
1044 (coding-system-for-write
1045 (and enable-multibyte-characters
1046 archive-file-name-coding-system))
1047 ;; We read an archive member by no-conversion at
1048 ;; first, then decode appropriately by calling
1049 ;; archive-set-buffer-as-visiting-file later.
1050 (coding-system-for-read 'no-conversion))
1051 (condition-case err
1052 (if (fboundp extractor)
1053 (funcall extractor archive ename)
1054 (archive-*-extract archive ename
1055 (symbol-value extractor)))
1056 (error
1057 (ding (message "%s" (error-message-string err)))
1058 nil))))
1059 just-created)
1060 (progn
1061 (set-buffer-modified-p nil)
1062 (kill-buffer buffer))
1063 (archive-try-jka-compr) ;Pretty ugly hack :-(
1064 (archive-set-buffer-as-visiting-file ename)
1065 (goto-char (point-min))
1066 (rename-buffer bufname)
1067 (setq buffer-read-only read-only-p)
1068 (setq buffer-undo-list nil)
1069 (set-buffer-modified-p nil)
1070 (setq buffer-saved-size (buffer-size))
1071 (normal-mode)
1072 ;; Just in case an archive occurs inside another archive.
1073 (when (derived-mode-p 'archive-mode)
1074 (setq archive-remote t)
1075 (if read-only-p (setq archive-read-only t))
1076 ;; We will write out the archive ourselves if it is
1077 ;; part of another archive.
1078 (remove-hook 'write-contents-functions 'archive-write-file t))
1079 (run-hooks 'archive-extract-hooks)
1080 (if archive-read-only
1081 (message "Note: altering this archive is not implemented."))))
1082 (archive-maybe-update t))
1083 (or (not (buffer-name buffer))
1084 (cond
1085 (view-p
1086 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
1087 ((eq other-window-p 'display) (display-buffer buffer))
1088 (other-window-p (switch-to-buffer-other-window buffer))
1089 (t (switch-to-buffer buffer))))))
1091 (defun archive-*-extract (archive name command)
1092 (let* ((default-directory (file-name-as-directory archive-tmpdir))
1093 (tmpfile (expand-file-name (file-name-nondirectory name)
1094 default-directory))
1095 exit-status success)
1096 (make-directory (directory-file-name default-directory) t)
1097 (setq exit-status
1098 (apply 'call-process
1099 (car command)
1103 (append (cdr command) (list archive name))))
1104 (cond ((and (numberp exit-status) (zerop exit-status))
1105 (if (not (file-exists-p tmpfile))
1106 (ding (message "`%s': no such file or directory" tmpfile))
1107 (insert-file-contents tmpfile)
1108 (setq success t)))
1109 ((numberp exit-status)
1110 (ding
1111 (message "`%s' exited with status %d" (car command) exit-status)))
1112 ((stringp exit-status)
1113 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1115 (ding (message "`%s' failed" (car command)))))
1116 (archive-delete-local tmpfile)
1117 success))
1119 (defun archive-extract-by-stdout (archive name command &optional stderr-file)
1120 (apply 'call-process
1121 (car command)
1123 (if stderr-file (list t stderr-file) t)
1125 (append (cdr command) (list archive name))))
1127 (defun archive-extract-other-window ()
1128 "In archive mode, find this member in another window."
1129 (interactive)
1130 (archive-extract t))
1132 (defun archive-display-other-window ()
1133 "In archive mode, display this member in another window."
1134 (interactive)
1135 (archive-extract 'display))
1137 (defun archive-view ()
1138 "In archive mode, view the member on this line."
1139 (interactive)
1140 (archive-extract 'view))
1142 (defun archive-add-new-member (arcbuf name)
1143 "Add current buffer to the archive in ARCBUF naming it NAME."
1144 (interactive
1145 (list (get-buffer
1146 (read-buffer "Buffer containing archive: "
1147 ;; Find first archive buffer and suggest that
1148 (let ((bufs (buffer-list)))
1149 (while (and bufs
1150 (not (with-current-buffer (car bufs)
1151 (derived-mode-p 'archive-mode))))
1152 (setq bufs (cdr bufs)))
1153 (if bufs
1154 (car bufs)
1155 (error "There are no archive buffers")))
1157 (read-string "File name in archive: "
1158 (if buffer-file-name
1159 (file-name-nondirectory buffer-file-name)
1160 ""))))
1161 (with-current-buffer arcbuf
1162 (or (derived-mode-p 'archive-mode)
1163 (error "Buffer is not an archive buffer"))
1164 (if archive-read-only
1165 (error "Archive is read-only")))
1166 (if (eq arcbuf (current-buffer))
1167 (error "An archive buffer cannot be added to itself"))
1168 (if (string= name "")
1169 (error "Archive members may not be given empty names"))
1170 (let ((func (with-current-buffer arcbuf
1171 (archive-name "add-new-member")))
1172 (membuf (current-buffer)))
1173 (if (fboundp func)
1174 (with-current-buffer arcbuf
1175 (funcall func buffer-file-name membuf name))
1176 (error "Adding a new member is not supported for this archive type"))))
1177 ;; -------------------------------------------------------------------------
1178 ;;; Section: IO stuff
1180 (defun archive-write-file-member ()
1181 (save-excursion
1182 (save-restriction
1183 (message "Updating archive...")
1184 (widen)
1185 (let ((writer (with-current-buffer archive-superior-buffer
1186 (archive-name "write-file-member")))
1187 (archive (with-current-buffer archive-superior-buffer
1188 (archive-maybe-copy (buffer-file-name)))))
1189 (if (fboundp writer)
1190 (funcall writer archive archive-subfile-mode)
1191 (archive-*-write-file-member archive
1192 archive-subfile-mode
1193 (symbol-value writer)))
1194 (set-buffer-modified-p nil)
1195 (message "Updating archive...done"))
1196 (set-buffer archive-superior-buffer)
1197 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1198 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1199 ;; won't reset the coding-system of this archive member.
1200 (if (local-variable-p 'archive-member-coding-system)
1201 (setq last-coding-system-used archive-member-coding-system))
1204 (defun archive-*-write-file-member (archive descr command)
1205 (let* ((ename (aref descr 0))
1206 (tmpfile (expand-file-name ename archive-tmpdir))
1207 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1208 (default-directory (file-name-as-directory top)))
1209 (unwind-protect
1210 (progn
1211 (make-directory (file-name-directory tmpfile) t)
1212 ;; If the member is itself an archive, write it without
1213 ;; the dired-like listing we created.
1214 (if (eq major-mode 'archive-mode)
1215 (archive-write-file tmpfile)
1216 (write-region nil nil tmpfile nil 'nomessage))
1217 ;; basic-save-buffer needs last-coding-system-used to have
1218 ;; the value used to write the file, so save it before any
1219 ;; further processing clobbers it (we restore it in
1220 ;; archive-write-file-member, above).
1221 (setq archive-member-coding-system last-coding-system-used)
1222 (if (aref descr 3)
1223 ;; Set the file modes, but make sure we can read it.
1224 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1225 (setq ename
1226 (encode-coding-string ename archive-file-name-coding-system))
1227 (let* ((coding-system-for-write 'no-conversion)
1228 (exitcode (apply 'call-process
1229 (car command)
1233 (append (cdr command)
1234 (list archive ename)))))
1235 (or (zerop exitcode)
1236 (error "Updating was unsuccessful (%S)" exitcode))))
1237 (archive-delete-local tmpfile))))
1239 (defun archive-write-file (&optional file)
1240 (save-excursion
1241 (let ((coding-system-for-write 'no-conversion))
1242 (write-region archive-proper-file-start (point-max)
1243 (or file buffer-file-name) nil t)
1244 (set-buffer-modified-p nil))
1246 ;; -------------------------------------------------------------------------
1247 ;;; Section: Marking and unmarking.
1249 (defun archive-flag-deleted (p &optional type)
1250 "In archive mode, mark this member to be deleted from the archive.
1251 With a prefix argument, mark that many files."
1252 (interactive "p")
1253 (or type (setq type ?D))
1254 (beginning-of-line)
1255 (let ((sign (if (>= p 0) +1 -1))
1256 (modified (buffer-modified-p))
1257 (inhibit-read-only t))
1258 (while (not (zerop p))
1259 (if (archive-get-descr t)
1260 (progn
1261 (delete-char 1)
1262 (insert type)))
1263 (forward-line sign)
1264 (setq p (- p sign)))
1265 (restore-buffer-modified-p modified))
1266 (archive-next-line 0))
1268 (defun archive-unflag (p)
1269 "In archive mode, un-mark this member if it is marked to be deleted.
1270 With a prefix argument, un-mark that many files forward."
1271 (interactive "p")
1272 (archive-flag-deleted p ?\s))
1274 (defun archive-unflag-backwards (p)
1275 "In archive mode, un-mark this member if it is marked to be deleted.
1276 With a prefix argument, un-mark that many members backward."
1277 (interactive "p")
1278 (archive-flag-deleted (- p) ?\s))
1280 (defun archive-unmark-all-files ()
1281 "Remove all marks."
1282 (interactive)
1283 (let ((modified (buffer-modified-p))
1284 (inhibit-read-only t))
1285 (save-excursion
1286 (goto-char archive-file-list-start)
1287 (while (< (point) archive-file-list-end)
1288 (or (= (following-char) ?\s)
1289 (progn (delete-char 1) (insert ?\s)))
1290 (forward-line 1)))
1291 (restore-buffer-modified-p modified)))
1293 (defun archive-mark (p)
1294 "In archive mode, mark this member for group operations.
1295 With a prefix argument, mark that many members.
1296 Use \\[archive-unmark-all-files] to remove all marks."
1297 (interactive "p")
1298 (archive-flag-deleted p ?*))
1300 (defun archive-get-marked (mark &optional default)
1301 (let (files)
1302 (save-excursion
1303 (goto-char archive-file-list-start)
1304 (while (< (point) archive-file-list-end)
1305 (if (= (following-char) mark)
1306 (setq files (cons (archive-get-descr) files)))
1307 (forward-line 1)))
1308 (or (nreverse files)
1309 (and default
1310 (list (archive-get-descr))))))
1311 ;; -------------------------------------------------------------------------
1312 ;;; Section: Operate
1314 (defun archive-next-line (p)
1315 (interactive "p")
1316 (forward-line p)
1317 (or (eobp)
1318 (forward-char archive-file-name-indent)))
1320 (defun archive-previous-line (p)
1321 (interactive "p")
1322 (archive-next-line (- p)))
1324 (defun archive-chmod-entry (new-mode)
1325 "Change the protection bits associated with all marked or this member.
1326 The new protection bits can either be specified as an octal number or
1327 as a relative change like \"g+rw\" as for chmod(2)."
1328 (interactive "sNew mode (octal or relative): ")
1329 (if archive-read-only (error "Archive is read-only"))
1330 (let ((func (archive-name "chmod-entry")))
1331 (if (fboundp func)
1332 (progn
1333 (funcall func new-mode (archive-get-marked ?* t))
1334 (archive-resummarize))
1335 (error "Setting mode bits is not supported for this archive type"))))
1337 (defun archive-chown-entry (new-uid)
1338 "Change the owner of all marked or this member."
1339 (interactive "nNew uid: ")
1340 (if archive-read-only (error "Archive is read-only"))
1341 (let ((func (archive-name "chown-entry")))
1342 (if (fboundp func)
1343 (progn
1344 (funcall func new-uid (archive-get-marked ?* t))
1345 (archive-resummarize))
1346 (error "Setting owner is not supported for this archive type"))))
1348 (defun archive-chgrp-entry (new-gid)
1349 "Change the group of all marked or this member."
1350 (interactive "nNew gid: ")
1351 (if archive-read-only (error "Archive is read-only"))
1352 (let ((func (archive-name "chgrp-entry")))
1353 (if (fboundp func)
1354 (progn
1355 (funcall func new-gid (archive-get-marked ?* t))
1356 (archive-resummarize))
1357 (error "Setting group is not supported for this archive type"))))
1359 (defun archive-expunge ()
1360 "Do the flagged deletions."
1361 (interactive)
1362 (let (files)
1363 (save-excursion
1364 (goto-char archive-file-list-start)
1365 (while (< (point) archive-file-list-end)
1366 (if (= (following-char) ?D)
1367 (setq files (cons (aref (archive-get-descr) 0) files)))
1368 (forward-line 1)))
1369 (setq files (nreverse files))
1370 (and files
1371 (or (not archive-read-only)
1372 (error "Archive is read-only"))
1373 (or (yes-or-no-p (format "Really delete %d member%s? "
1374 (length files)
1375 (if (null (cdr files)) "" "s")))
1376 (error "Operation aborted"))
1377 (let ((archive (archive-maybe-copy (buffer-file-name)))
1378 (expunger (archive-name "expunge")))
1379 (if (fboundp expunger)
1380 (funcall expunger archive files)
1381 (archive-*-expunge archive files (symbol-value expunger)))
1382 (archive-maybe-update nil)
1383 (if archive-remote
1384 (archive-resummarize)
1385 (revert-buffer))))))
1387 (defun archive-*-expunge (archive files command)
1388 (apply 'call-process
1389 (car command)
1393 (append (cdr command) (cons archive files))))
1395 (defun archive-rename-entry (newname)
1396 "Change the name associated with this entry in the archive file."
1397 (interactive "sNew name: ")
1398 (if archive-read-only (error "Archive is read-only"))
1399 (if (string= newname "")
1400 (error "Archive members may not be given empty names"))
1401 (let ((func (archive-name "rename-entry"))
1402 (descr (archive-get-descr)))
1403 (if (fboundp func)
1404 (progn
1405 (funcall func
1406 (encode-coding-string newname
1407 archive-file-name-coding-system)
1408 descr)
1409 (archive-resummarize))
1410 (error "Renaming is not supported for this archive type"))))
1412 ;; Revert the buffer and recompute the dired-like listing.
1413 (defun archive-mode-revert (&optional _no-auto-save _no-confirm)
1414 (let ((no (archive-get-lineno)))
1415 (setq archive-files nil)
1416 (let ((revert-buffer-function nil)
1417 (coding-system-for-read 'no-conversion))
1418 (revert-buffer t t))
1419 (archive-mode)
1420 (goto-char archive-file-list-start)
1421 (archive-next-line no)))
1423 (defun archive-undo ()
1424 "Undo in an archive buffer.
1425 This doesn't recover lost files, it just undoes changes in the buffer itself."
1426 (interactive)
1427 (let ((inhibit-read-only t))
1428 (undo)))
1429 ;; -------------------------------------------------------------------------
1430 ;;; Section: Arc Archives
1432 (defun archive-arc-summarize ()
1433 (let ((p 1)
1434 (totalsize 0)
1435 (maxlen 8)
1436 files
1437 visual)
1438 (while (and (< (+ p 29) (point-max))
1439 (= (byte-after p) ?\C-z)
1440 (> (byte-after (1+ p)) 0))
1441 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1442 (fnlen (or (string-match "\0" namefld) 13))
1443 (efnname (decode-coding-string (substring namefld 0 fnlen)
1444 archive-file-name-coding-system))
1445 ;; Convert to float to avoid overflow for very large files.
1446 (csize (archive-l-e (+ p 15) 4 'float))
1447 (moddate (archive-l-e (+ p 19) 2))
1448 (modtime (archive-l-e (+ p 21) 2))
1449 (ucsize (archive-l-e (+ p 25) 4 'float))
1450 (fiddle (string= efnname (upcase efnname)))
1451 (ifnname (if fiddle (downcase efnname) efnname))
1452 (text (format " %8.0f %-11s %-8s %s"
1453 ucsize
1454 (archive-dosdate moddate)
1455 (archive-dostime modtime)
1456 ifnname)))
1457 (setq maxlen (max maxlen fnlen)
1458 totalsize (+ totalsize ucsize)
1459 visual (cons (vector text
1460 (- (length text) (length ifnname))
1461 (length text))
1462 visual)
1463 files (cons (vector efnname ifnname fiddle nil (1- p))
1464 files)
1465 ;; p needs to stay an integer, since we use it in char-after
1466 ;; above. Passing through `round' limits the compressed size
1467 ;; to most-positive-fixnum, but if the compressed size exceeds
1468 ;; that, we cannot visit the archive anyway.
1469 p (+ p 29 (round csize)))))
1470 (goto-char (point-min))
1471 (let ((dash (concat "- -------- ----------- -------- "
1472 (make-string maxlen ?-)
1473 "\n")))
1474 (insert "M Length Date Time File\n"
1475 dash)
1476 (archive-summarize-files (nreverse visual))
1477 (insert dash
1478 (format " %8.0f %d file%s"
1479 totalsize
1480 (length files)
1481 (if (= 1 (length files)) "" "s"))
1482 "\n"))
1483 (apply 'vector (nreverse files))))
1485 (defun archive-arc-rename-entry (newname descr)
1486 (if (string-match "[:\\\\/]" newname)
1487 (error "File names in arc files must not contain a directory component"))
1488 (if (> (length newname) 12)
1489 (error "File names in arc files are limited to 12 characters"))
1490 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1491 (length newname))))
1492 (inhibit-read-only t))
1493 (save-restriction
1494 (save-excursion
1495 (widen)
1496 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1497 (delete-char 13)
1498 (insert-unibyte name)))))
1499 ;; -------------------------------------------------------------------------
1500 ;;; Section: Lzh Archives
1502 (defun archive-lzh-summarize (&optional start)
1503 (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
1504 (totalsize 0)
1505 (maxlen 8)
1506 files
1507 visual)
1508 (while (progn (goto-char p) ;beginning of a base header.
1509 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1510 (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
1511 ;; Convert to float to avoid overflow for very large files.
1512 (csize (archive-l-e (+ p 7) 4 'float)) ;size of a compressed file to follow (level 0 and 2),
1513 ;size of extended headers + the compressed file to follow (level 1).
1514 (ucsize (archive-l-e (+ p 11) 4 'float)) ;size of an uncompressed file.
1515 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1516 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
1517 (hdrlvl (byte-after (+ p 20))) ;header level
1518 thsize ;total header size (base + extensions)
1519 fnlen efnname osid fiddle ifnname width p2
1520 neh ;beginning of next extension header (level 1 and 2)
1521 mode modestr uid gid text dir prname
1522 gname uname modtime moddate)
1523 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1524 (when (or (= hdrlvl 0) (= hdrlvl 1))
1525 (setq fnlen (byte-after (+ p 21))) ;filename length
1526 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
1527 (decode-coding-string
1528 str archive-file-name-coding-system)))
1529 (setq p2 (+ p 22 fnlen))) ;
1530 (if (= hdrlvl 1)
1531 (setq neh (+ p2 3)) ;specific to level 1 header
1532 (if (= hdrlvl 2)
1533 (setq neh (+ p 24)))) ;specific to level 2 header
1534 (if neh ;if level 1 or 2 we expect extension headers to follow
1535 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
1536 (etype (byte-after (+ neh 2)))) ;extension type
1537 (while (not (= ehsize 0))
1538 (cond
1539 ((= etype 1) ;file name
1540 (let ((i (+ neh 3)))
1541 (while (< i (+ neh ehsize))
1542 (setq efnname (concat efnname (char-to-string (byte-after i))))
1543 (setq i (1+ i)))))
1544 ((= etype 2) ;directory name
1545 (let ((i (+ neh 3)))
1546 (while (< i (+ neh ehsize))
1547 (setq dir (concat dir
1548 (if (= (byte-after i)
1549 255)
1551 (char-to-string
1552 (char-after i)))))
1553 (setq i (1+ i)))))
1554 ((= etype 80) ;Unix file permission
1555 (setq mode (archive-l-e (+ neh 3) 2)))
1556 ((= etype 81) ;UNIX file group/user ID
1557 (progn (setq uid (archive-l-e (+ neh 3) 2))
1558 (setq gid (archive-l-e (+ neh 5) 2))))
1559 ((= etype 82) ;UNIX file group name
1560 (let ((i (+ neh 3)))
1561 (while (< i (+ neh ehsize))
1562 (setq gname (concat gname (char-to-string (char-after i))))
1563 (setq i (1+ i)))))
1564 ((= etype 83) ;UNIX file user name
1565 (let ((i (+ neh 3)))
1566 (while (< i (+ neh ehsize))
1567 (setq uname (concat uname (char-to-string (char-after i))))
1568 (setq i (1+ i)))))
1570 (setq neh (+ neh ehsize))
1571 (setq ehsize (archive-l-e neh 2))
1572 (setq etype (byte-after (+ neh 2))))
1573 ;;get total header size for level 1 and 2 headers
1574 (setq thsize (- neh p))))
1575 (if (= hdrlvl 0) ;total header size
1576 (setq thsize hsize))
1577 ;; OS ID field not present in level 0 header, use code 0 "generic"
1578 ;; in that case as per lha program header.c get_header()
1579 (setq osid (cond ((= hdrlvl 0) 0)
1580 ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
1581 ((= hdrlvl 2) (char-after (+ p 23)))))
1582 ;; Filename fiddling must follow the lha program, otherwise the name
1583 ;; passed to "lha pq" etc won't match (which for an extract silently
1584 ;; results in no output). As of version 1.14i it goes from the OS ID,
1585 ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
1586 ;; converts "\" to "/".
1587 ;; - For 0 generic: generic_to_unix_filename() downcases if there's
1588 ;; no lower case already present, and converts "\" to "/".
1589 ;; - For 'm' MacOS: macos_to_unix_filename() changes "/" to ":" and
1590 ;; ":" to "/"
1591 (setq fiddle (cond ((= ?M osid) t)
1592 ((= 0 osid) (string= efnname (upcase efnname)))))
1593 (setq ifnname (if fiddle (downcase efnname) efnname))
1594 (setq prname (if dir (concat dir ifnname) ifnname))
1595 (setq width (if prname (string-width prname) 0))
1596 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1597 (setq moddate (if (= hdrlvl 2)
1598 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1599 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1600 (setq modtime (if (= hdrlvl 2)
1601 (archive-unixtime time1 time2)
1602 (archive-dostime time1)))
1603 (setq text (if archive-alternate-display
1604 (format " %8.0f %5S %5S %s"
1605 ucsize
1606 (or uid "?")
1607 (or gid "?")
1608 ifnname)
1609 (format " %10s %8.0f %-11s %-8s %s"
1610 modestr
1611 ucsize
1612 moddate
1613 modtime
1614 prname)))
1615 (setq maxlen (max maxlen width)
1616 totalsize (+ totalsize ucsize)
1617 visual (cons (vector text
1618 (- (length text) (length prname))
1619 (length text))
1620 visual)
1621 files (cons (vector prname ifnname fiddle mode (1- p))
1622 files))
1623 (cond ((= hdrlvl 1)
1624 ;; p needs to stay an integer, since we use it in goto-char
1625 ;; above. Passing through `round' limits the compressed size
1626 ;; to most-positive-fixnum, but if the compressed size exceeds
1627 ;; that, we cannot visit the archive anyway.
1628 (setq p (+ p hsize 2 (round csize))))
1629 ((or (= hdrlvl 2) (= hdrlvl 0))
1630 (setq p (+ p thsize 2 (round csize)))))
1632 (goto-char (point-min))
1633 (let ((dash (concat (if archive-alternate-display
1634 "- -------- ----- ----- "
1635 "- ---------- -------- ----------- -------- ")
1636 (make-string maxlen ?-)
1637 "\n"))
1638 (header (if archive-alternate-display
1639 "M Length Uid Gid File\n"
1640 "M Filemode Length Date Time File\n"))
1641 (sumline (if archive-alternate-display
1642 " %8.0f %d file%s"
1643 " %8.0f %d file%s")))
1644 (insert header dash)
1645 (archive-summarize-files (nreverse visual))
1646 (insert dash
1647 (format sumline
1648 totalsize
1649 (length files)
1650 (if (= 1 (length files)) "" "s"))
1651 "\n"))
1652 (apply 'vector (nreverse files))))
1654 (defconst archive-lzh-alternate-display t)
1656 (defun archive-lzh-extract (archive name)
1657 (archive-extract-by-stdout archive name archive-lzh-extract))
1659 (defun archive-lzh-resum (p count)
1660 (let ((sum 0))
1661 (while (> count 0)
1662 (setq count (1- count)
1663 sum (+ sum (byte-after p))
1664 p (1+ p)))
1665 (logand sum 255)))
1667 (defun archive-lzh-rename-entry (newname descr)
1668 (save-restriction
1669 (save-excursion
1670 (widen)
1671 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1672 (oldhsize (byte-after p))
1673 (oldfnlen (byte-after (+ p 21)))
1674 (newfnlen (length newname))
1675 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1676 (inhibit-read-only t))
1677 (if (> newhsize 255)
1678 (error "The file name is too long"))
1679 (goto-char (+ p 21))
1680 (delete-char (1+ oldfnlen))
1681 (insert-unibyte newfnlen newname)
1682 (goto-char p)
1683 (delete-char 2)
1684 (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
1686 (defun archive-lzh-ogm (newval files errtxt ofs)
1687 (save-excursion
1688 (save-restriction
1689 (widen)
1690 (dolist (fil files)
1691 (let* ((p (+ archive-proper-file-start (aref fil 4)))
1692 (hsize (byte-after p))
1693 (fnlen (byte-after (+ p 21)))
1694 (p2 (+ p 22 fnlen))
1695 (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
1696 (inhibit-read-only t))
1697 (if (= creator ?U)
1698 (progn
1699 (or (numberp newval)
1700 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1701 (goto-char (+ p2 ofs))
1702 (delete-char 2)
1703 (insert-unibyte (logand newval 255) (lsh newval -8))
1704 (goto-char (1+ p))
1705 (delete-char 1)
1706 (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
1707 (message "Member %s does not have %s field"
1708 (aref fil 1) errtxt)))))))
1710 (defun archive-lzh-chown-entry (newuid files)
1711 (archive-lzh-ogm newuid files "an uid" 10))
1713 (defun archive-lzh-chgrp-entry (newgid files)
1714 (archive-lzh-ogm newgid files "a gid" 12))
1716 (defun archive-lzh-chmod-entry (newmode files)
1717 (archive-lzh-ogm
1718 ;; This should work even though newmode will be dynamically accessed.
1719 (lambda (old) (archive-calc-mode old newmode t))
1720 files "a unix-style mode" 8))
1722 ;; -------------------------------------------------------------------------
1723 ;;; Section: Lzh Self-Extracting .exe Archives
1725 ;; No support for modifying these files. It looks like the lha for unix
1726 ;; program (as of version 1.14i) can't create or retain the DOS exe part.
1727 ;; If you do an "lha a" on a .exe for instance it renames and writes to a
1728 ;; plain .lzh.
1730 (defun archive-lzh-exe-summarize ()
1731 "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
1733 ;; Skip the initial executable code part and apply archive-lzh-summarize
1734 ;; to the archive part proper. The "-lh5-" etc regexp here for the start
1735 ;; is the same as in archive-find-type.
1737 ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
1738 ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
1739 ;; the header checksum, or level 2 a test of the "attribute" and size.
1741 (re-search-forward "..-l[hz][0-9ds]-" nil)
1742 (archive-lzh-summarize (match-beginning 0)))
1744 ;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
1745 ;; .lzh files
1746 (defalias 'archive-lzh-exe-extract 'archive-lzh-extract
1747 "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
1749 ;; -------------------------------------------------------------------------
1750 ;;; Section: Zip Archives
1752 (defun archive-zip-summarize ()
1753 (goto-char (- (point-max) (- 22 18)))
1754 (search-backward-regexp "[P]K\005\006")
1755 (let ((p (+ (point-min) (archive-l-e (+ (point) 16) 4)))
1756 (maxlen 8)
1757 (totalsize 0)
1758 files
1759 visual)
1760 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1761 (let* ((creator (byte-after (+ p 5)))
1762 ;; (method (archive-l-e (+ p 10) 2))
1763 (modtime (archive-l-e (+ p 12) 2))
1764 (moddate (archive-l-e (+ p 14) 2))
1765 ;; Convert to float to avoid overflow for very large files.
1766 (ucsize (archive-l-e (+ p 24) 4 'float))
1767 (fnlen (archive-l-e (+ p 28) 2))
1768 (exlen (archive-l-e (+ p 30) 2))
1769 (fclen (archive-l-e (+ p 32) 2))
1770 (lheader (archive-l-e (+ p 42) 4))
1771 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1772 (decode-coding-string
1773 str archive-file-name-coding-system)))
1774 (isdir (and (= ucsize 0)
1775 (string= (file-name-nondirectory efnname) "")))
1776 (mode (cond ((memq creator '(2 3)) ; Unix
1777 (archive-l-e (+ p 40) 2))
1778 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1779 (logior ?\444
1780 (if isdir (logior 16384 ?\111) 0)
1781 (if (zerop
1782 (logand 1 (byte-after (+ p 38))))
1783 ?\222 0)))
1784 (t nil)))
1785 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1786 (fiddle (and archive-zip-case-fiddle
1787 (not (not (memq creator '(0 2 4 5 9))))
1788 (string= (upcase efnname) efnname)))
1789 (ifnname (if fiddle (downcase efnname) efnname))
1790 (width (string-width ifnname))
1791 (text (format " %10s %8.0f %-11s %-8s %s"
1792 modestr
1793 ucsize
1794 (archive-dosdate moddate)
1795 (archive-dostime modtime)
1796 ifnname)))
1797 (setq maxlen (max maxlen width)
1798 totalsize (+ totalsize ucsize)
1799 visual (cons (vector text
1800 (- (length text) (length ifnname))
1801 (length text))
1802 visual)
1803 files (cons (if isdir
1805 (vector efnname ifnname fiddle mode
1806 (list (1- p) lheader)))
1807 files)
1808 p (+ p 46 fnlen exlen fclen))))
1809 (goto-char (point-min))
1810 (let ((dash (concat "- ---------- -------- ----------- -------- "
1811 (make-string maxlen ?-)
1812 "\n")))
1813 (insert "M Filemode Length Date Time File\n"
1814 dash)
1815 (archive-summarize-files (nreverse visual))
1816 (insert dash
1817 (format " %8.0f %d file%s"
1818 totalsize
1819 (length files)
1820 (if (= 1 (length files)) "" "s"))
1821 "\n"))
1822 (apply 'vector (nreverse files))))
1824 (defun archive-zip-extract (archive name)
1825 (cond
1826 ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
1827 (archive-*-extract archive name archive-zip-extract))
1828 ((equal (car archive-zip-extract) "7z")
1829 (let ((archive-7z-extract archive-zip-extract))
1830 (archive-7z-extract archive name)))
1832 (archive-extract-by-stdout
1833 archive
1834 ;; unzip expands wildcards in NAME, so we need to quote it. But
1835 ;; not on DOS/Windows, since that fails extraction on those
1836 ;; systems (unless w32-quote-process-args is nil), and file names
1837 ;; with wildcards in zip archives don't work there anyway.
1838 ;; FIXME: Does pkunzip need similar treatment?
1839 (if (and (or (not (memq system-type '(windows-nt ms-dos)))
1840 (and (boundp 'w32-quote-process-args)
1841 (null w32-quote-process-args)))
1842 (equal (car archive-zip-extract) "unzip"))
1843 (shell-quote-argument name)
1844 name)
1845 archive-zip-extract))))
1847 (defun archive-zip-write-file-member (archive descr)
1848 (archive-*-write-file-member
1849 archive
1850 descr
1851 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1853 (defun archive-zip-chmod-entry (newmode files)
1854 (save-restriction
1855 (save-excursion
1856 (widen)
1857 (dolist (fil files)
1858 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
1859 (creator (byte-after (+ p 5)))
1860 (oldmode (aref fil 3))
1861 (newval (archive-calc-mode oldmode newmode t))
1862 (inhibit-read-only t))
1863 (cond ((memq creator '(2 3)) ; Unix
1864 (goto-char (+ p 40))
1865 (delete-char 2)
1866 (insert-unibyte (logand newval 255) (lsh newval -8)))
1867 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1868 (goto-char (+ p 38))
1869 (insert-unibyte (logior (logand (byte-after (point)) 254)
1870 (logand (logxor 1 (lsh newval -7)) 1)))
1871 (delete-char 1))
1872 (t (message "Don't know how to change mode for this member"))))
1873 ))))
1874 ;; -------------------------------------------------------------------------
1875 ;;; Section: Zoo Archives
1877 (defun archive-zoo-summarize ()
1878 (let ((p (1+ (archive-l-e 25 4)))
1879 (maxlen 8)
1880 (totalsize 0)
1881 files
1882 visual)
1883 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1884 (> (archive-l-e (+ p 6) 4) 0))
1885 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1886 (moddate (archive-l-e (+ p 14) 2))
1887 (modtime (archive-l-e (+ p 16) 2))
1888 ;; Convert to float to avoid overflow for very large files.
1889 (ucsize (archive-l-e (+ p 20) 4 'float))
1890 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1891 (dirtype (byte-after (+ p 4)))
1892 (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
1893 (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
1894 (fnlen (or (string-match "\0" namefld) 13))
1895 (efnname (let ((str
1896 (concat
1897 (if (> ldirlen 0)
1898 (concat (buffer-substring
1899 (+ p 58 lfnlen)
1900 (+ p 58 lfnlen ldirlen -1))
1901 "/")
1903 (if (> lfnlen 0)
1904 (buffer-substring (+ p 58)
1905 (+ p 58 lfnlen -1))
1906 (substring namefld 0 fnlen)))))
1907 (decode-coding-string
1908 str archive-file-name-coding-system)))
1909 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1910 (ifnname (if fiddle (downcase efnname) efnname))
1911 (width (string-width ifnname))
1912 (text (format " %8.0f %-11s %-8s %s"
1913 ucsize
1914 (archive-dosdate moddate)
1915 (archive-dostime modtime)
1916 ifnname)))
1917 (setq maxlen (max maxlen width)
1918 totalsize (+ totalsize ucsize)
1919 visual (cons (vector text
1920 (- (length text) (length ifnname))
1921 (length text))
1922 visual)
1923 files (cons (vector efnname ifnname fiddle nil (1- p))
1924 files)
1925 p next)))
1926 (goto-char (point-min))
1927 (let ((dash (concat "- -------- ----------- -------- "
1928 (make-string maxlen ?-)
1929 "\n")))
1930 (insert "M Length Date Time File\n"
1931 dash)
1932 (archive-summarize-files (nreverse visual))
1933 (insert dash
1934 (format " %8.0f %d file%s"
1935 totalsize
1936 (length files)
1937 (if (= 1 (length files)) "" "s"))
1938 "\n"))
1939 (apply 'vector (nreverse files))))
1941 (defun archive-zoo-extract (archive name)
1942 (archive-extract-by-stdout archive name archive-zoo-extract))
1944 ;; -------------------------------------------------------------------------
1945 ;;; Section: Rar Archives
1947 (defun archive-rar-summarize (&optional file)
1948 ;; File is used internally for `archive-rar-exe-summarize'.
1949 (unless file (setq file buffer-file-name))
1950 (let* ((copy (file-local-copy file))
1951 (maxname 10)
1952 (maxsize 5)
1953 (files ()))
1954 (with-temp-buffer
1955 (call-process "unrar-free" nil t nil "--list" (or file copy))
1956 (if copy (delete-file copy))
1957 (goto-char (point-min))
1958 (re-search-forward "^-+\n")
1959 (while (looking-at (concat " \\(.*\\)\n" ;Name.
1960 ;; Size ; Packed.
1961 " +\\([0-9]+\\) +[0-9]+"
1962 ;; Ratio ; Date'
1963 " +\\([0-9%]+\\) +\\([-0-9]+\\)"
1964 ;; Time ; Attr.
1965 " +\\([0-9:]+\\) +[^ \n]\\{6,10\\}"
1966 ;; CRC; Meth ; Var.
1967 " +[0-9A-F]+ +[^ \n]+ +[0-9.]+\n"))
1968 (goto-char (match-end 0))
1969 (let ((name (match-string 1))
1970 (size (match-string 2)))
1971 (if (> (length name) maxname) (setq maxname (length name)))
1972 (if (> (length size) maxsize) (setq maxsize (length size)))
1973 (push (vector name name nil nil
1974 ;; Size, Ratio.
1975 size (match-string 3)
1976 ;; Date, Time.
1977 (match-string 4) (match-string 5))
1978 files))))
1979 (setq files (nreverse files))
1980 (goto-char (point-min))
1981 (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
1982 (sep (format format "--------" "-----" (make-string maxsize ?-)
1983 "-----" ""))
1984 (column (length sep)))
1985 (insert (format format " Date " "Time " "Size " "Ratio" " Filename") "\n")
1986 (insert sep (make-string maxname ?-) "\n")
1987 (archive-summarize-files (mapcar (lambda (desc)
1988 (let ((text
1989 (format format
1990 (aref desc 6)
1991 (aref desc 7)
1992 (aref desc 4)
1993 (aref desc 5)
1994 (aref desc 1))))
1995 (vector text
1996 column
1997 (length text))))
1998 files))
1999 (insert sep (make-string maxname ?-) "\n")
2000 (apply 'vector files))))
2002 (defun archive-rar-extract (archive name)
2003 ;; unrar-free seems to have no way to extract to stdout or even to a file.
2004 (if (file-name-absolute-p name)
2005 ;; The code below assumes the name is relative and may do undesirable
2006 ;; things otherwise.
2007 (error "Can't extract files with non-relative names")
2008 (let ((dest (make-temp-file "arc-rar" 'dir)))
2009 (unwind-protect
2010 (progn
2011 (call-process "unrar-free" nil nil nil
2012 "--extract" archive name dest)
2013 (insert-file-contents-literally (expand-file-name name dest)))
2014 (delete-file (expand-file-name name dest))
2015 (while (file-name-directory name)
2016 (setq name (directory-file-name (file-name-directory name)))
2017 (delete-directory (expand-file-name name dest)))
2018 (delete-directory dest)))))
2020 ;;; Section: Rar self-extracting .exe archives.
2022 (defun archive-rar-exe-summarize ()
2023 (let ((tmpfile (make-temp-file "rarexe")))
2024 (unwind-protect
2025 (progn
2026 (goto-char (point-min))
2027 (re-search-forward "Rar!")
2028 (write-region (match-beginning 0) (point-max) tmpfile)
2029 (archive-rar-summarize tmpfile))
2030 (delete-file tmpfile))))
2032 (defun archive-rar-exe-extract (archive name)
2033 (let* ((tmpfile (make-temp-file "rarexe"))
2034 (buf (find-buffer-visiting archive))
2035 (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
2036 (unwind-protect
2037 (progn
2038 (with-current-buffer (or buf tmpbuf)
2039 (save-excursion
2040 (save-restriction
2041 (if buf
2042 ;; point-max unwidened is assumed to be the end of the
2043 ;; summary text and the beginning of the actual file data.
2044 (progn (goto-char (point-max)) (widen))
2045 (insert-file-contents-literally archive)
2046 (goto-char (point-min)))
2047 (re-search-forward "Rar!")
2048 (write-region (match-beginning 0) (point-max) tmpfile))))
2049 (archive-rar-extract tmpfile name))
2050 (if tmpbuf (kill-buffer tmpbuf))
2051 (delete-file tmpfile))))
2053 ;; -------------------------------------------------------------------------
2054 ;;; Section: 7z Archives
2056 (defun archive-7z-summarize ()
2057 (let ((maxname 10)
2058 (maxsize 5)
2059 (file buffer-file-name)
2060 (files ()))
2061 (with-temp-buffer
2062 (call-process "7z" nil t nil "l" "-slt" file)
2063 (goto-char (point-min))
2064 ;; Four dashes start the meta info section that should be skipped.
2065 ;; Archive members start with more than four dashes.
2066 (re-search-forward "^-----+\n")
2067 (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
2068 (goto-char (match-end 0))
2069 (let ((name (match-string 1))
2070 (size (save-excursion
2071 (and (re-search-forward "^Size = \\(.*\\)\n")
2072 (match-string 1))))
2073 (time (save-excursion
2074 (and (re-search-forward "^Modified = \\(.*\\)\n")
2075 (match-string 1)))))
2076 (if (> (length name) maxname) (setq maxname (length name)))
2077 (if (> (length size) maxsize) (setq maxsize (length size)))
2078 (push (vector name name nil nil time nil nil size)
2079 files))))
2080 (setq files (nreverse files))
2081 (goto-char (point-min))
2082 (let* ((format (format " %%%ds %%s %%s" maxsize))
2083 (sep (format format (make-string maxsize ?-) "-------------------" ""))
2084 (column (length sep)))
2085 (insert (format format "Size " "Date Time " " Filename") "\n")
2086 (insert sep (make-string maxname ?-) "\n")
2087 (archive-summarize-files (mapcar (lambda (desc)
2088 (let ((text
2089 (format format
2090 (aref desc 7)
2091 (aref desc 4)
2092 (aref desc 1))))
2093 (vector text
2094 column
2095 (length text))))
2096 files))
2097 (insert sep (make-string maxname ?-) "\n")
2098 (apply 'vector files))))
2100 (defun archive-7z-extract (archive name)
2101 (let ((tmpfile (make-temp-file "7z-stderr")))
2102 ;; 7z doesn't provide a `quiet' option to suppress non-essential
2103 ;; stderr messages. So redirect stderr to a temp file and display it
2104 ;; in the echo area when it contains error messages.
2105 (prog1 (archive-extract-by-stdout
2106 archive name archive-7z-extract tmpfile)
2107 (with-temp-buffer
2108 (insert-file-contents tmpfile)
2109 (unless (search-forward "Everything is Ok" nil t)
2110 (message "%s" (buffer-string)))
2111 (delete-file tmpfile)))))
2113 (defun archive-7z-write-file-member (archive descr)
2114 (archive-*-write-file-member
2115 archive
2116 descr
2117 archive-7z-update))
2119 ;; -------------------------------------------------------------------------
2120 ;;; Section `ar' archives.
2122 ;; TODO: we currently only handle the basic format of ar archives,
2123 ;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
2124 ;; for .deb packages.
2126 (autoload 'tar-grind-file-mode "tar-mode")
2128 (defconst archive-ar-file-header-re
2129 "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
2131 (defun archive-ar-summarize ()
2132 ;; File is used internally for `archive-rar-exe-summarize'.
2133 (let* ((maxname 10)
2134 (maxtime 16)
2135 (maxuser 5)
2136 (maxgroup 5)
2137 (maxmode 8)
2138 (maxsize 5)
2139 (files ()))
2140 (goto-char (point-min))
2141 (search-forward "!<arch>\n")
2142 (while (looking-at archive-ar-file-header-re)
2143 (let ((name (match-string 1))
2144 extname
2145 ;; Emacs will automatically use float here because those
2146 ;; timestamps don't fit in our ints.
2147 (time (string-to-number (match-string 2)))
2148 (user (match-string 3))
2149 (group (match-string 4))
2150 (mode (string-to-number (match-string 5) 8))
2151 (size (string-to-number (match-string 6))))
2152 ;; Move to the beginning of the data.
2153 (goto-char (match-end 0))
2154 (setq time
2155 (format-time-string
2156 "%Y-%m-%d %H:%M"
2157 (let ((high (truncate (/ time 65536))))
2158 (list high (truncate (- time (* 65536.0 high)))))))
2159 (setq extname
2160 (cond ((equal name "// ")
2161 (propertize ".<ExtNamesTable>." 'face 'italic))
2162 ((equal name "/ ")
2163 (propertize ".<LookupTable>." 'face 'italic))
2164 ((string-match "/? *\\'" name)
2165 (substring name 0 (match-beginning 0)))))
2166 (setq user (substring user 0 (string-match " +\\'" user)))
2167 (setq group (substring group 0 (string-match " +\\'" group)))
2168 (setq mode (tar-grind-file-mode mode))
2169 ;; Move to the end of the data.
2170 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1))
2171 (setq size (number-to-string size))
2172 (if (> (length name) maxname) (setq maxname (length name)))
2173 (if (> (length time) maxtime) (setq maxtime (length time)))
2174 (if (> (length user) maxuser) (setq maxuser (length user)))
2175 (if (> (length group) maxgroup) (setq maxgroup (length group)))
2176 (if (> (length mode) maxmode) (setq maxmode (length mode)))
2177 (if (> (length size) maxsize) (setq maxsize (length size)))
2178 (push (vector name extname nil mode
2179 time user group size)
2180 files)))
2181 (setq files (nreverse files))
2182 (goto-char (point-min))
2183 (let* ((format (format "%%%ds %%%ds/%%-%ds %%%ds %%%ds %%s"
2184 maxmode maxuser maxgroup maxsize maxtime))
2185 (sep (format format (make-string maxmode ?-)
2186 (make-string maxuser ?-)
2187 (make-string maxgroup ?-)
2188 (make-string maxsize ?-)
2189 (make-string maxtime ?-) ""))
2190 (column (length sep)))
2191 (insert (format format " Mode " "User" "Group" " Size "
2192 " Date " "Filename")
2193 "\n")
2194 (insert sep (make-string maxname ?-) "\n")
2195 (archive-summarize-files (mapcar (lambda (desc)
2196 (let ((text
2197 (format format
2198 (aref desc 3)
2199 (aref desc 5)
2200 (aref desc 6)
2201 (aref desc 7)
2202 (aref desc 4)
2203 (aref desc 1))))
2204 (vector text
2205 column
2206 (length text))))
2207 files))
2208 (insert sep (make-string maxname ?-) "\n")
2209 (apply 'vector files))))
2211 (defun archive-ar-extract (archive name)
2212 (let ((destbuf (current-buffer))
2213 (archivebuf (find-file-noselect archive))
2214 (from nil) size)
2215 (with-current-buffer archivebuf
2216 (save-restriction
2217 ;; We may be in archive-mode or not, so either with or without
2218 ;; narrowing and with or without a prepended summary.
2219 (save-excursion
2220 (widen)
2221 (search-forward "!<arch>\n")
2222 (while (and (not from) (looking-at archive-ar-file-header-re))
2223 (let ((this (match-string 1)))
2224 (setq size (string-to-number (match-string 6)))
2225 (goto-char (match-end 0))
2226 (if (equal name this)
2227 (setq from (point))
2228 ;; Move to the end of the data.
2229 (forward-char size) (if (eq ?\n (char-after)) (forward-char 1)))))
2230 (when from
2231 (set-buffer-multibyte nil)
2232 (with-current-buffer destbuf
2233 ;; Do it within the `widen'.
2234 (insert-buffer-substring archivebuf from (+ from size)))
2235 (set-buffer-multibyte 'to)
2236 ;; Inform the caller that the call succeeded.
2237 t))))))
2239 ;; -------------------------------------------------------------------------
2240 ;; This line was a mistake; it is kept now for compatibility.
2241 ;; rms 15 Oct 98
2242 (provide 'archive-mode)
2244 (provide 'arc-mode)
2246 ;;; arc-mode.el ends here