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