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