Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / arc-mode.el
blob50048c0cb39452906b5bdf3a62fa8b17a9de0718
1 ;;; arc-mode.el --- simple editing of archives
3 ;; Copyright (C) 1995, 1997-1998, 2001-2018 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 <https://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 [?\S-\ ] 'archive-previous-line)
399 (define-key map [up] 'archive-previous-line)
400 (define-key map "r" 'archive-rename-entry)
401 (define-key map "u" 'archive-unflag)
402 (define-key map "\M-\C-?" 'archive-unmark-all-files)
403 (define-key map "v" 'archive-view)
404 (define-key map "x" 'archive-expunge)
405 (define-key map "\177" 'archive-unflag-backwards)
406 (define-key map "E" 'archive-extract-other-window)
407 (define-key map "M" 'archive-chmod-entry)
408 (define-key map "G" 'archive-chgrp-entry)
409 (define-key map "O" 'archive-chown-entry)
410 ;; Let mouse-1 follow the link.
411 (define-key map [follow-link] 'mouse-face)
413 (if (fboundp 'command-remapping)
414 (progn
415 (define-key map [remap advertised-undo] 'archive-undo)
416 (define-key map [remap undo] 'archive-undo))
417 (substitute-key-definition 'advertised-undo 'archive-undo map global-map)
418 (substitute-key-definition 'undo 'archive-undo map global-map))
420 (define-key map
421 (if (featurep 'xemacs) 'button2 [mouse-2]) 'archive-extract)
423 (if (featurep 'xemacs)
424 () ; out of luck
426 (define-key map [menu-bar immediate]
427 (cons "Immediate" (make-sparse-keymap "Immediate")))
428 (define-key map [menu-bar immediate alternate]
429 '(menu-item "Alternate Display" archive-alternate-display
430 :enable (boundp (archive-name "alternate-display"))
431 :help "Toggle alternate file info display"))
432 (define-key map [menu-bar immediate view]
433 '(menu-item "View This File" archive-view
434 :help "Display file at cursor in View Mode"))
435 (define-key map [menu-bar immediate display]
436 '(menu-item "Display in Other Window" archive-display-other-window
437 :help "Display file at cursor in another window"))
438 (define-key map [menu-bar immediate find-file-other-window]
439 '(menu-item "Find in Other Window" archive-extract-other-window
440 :help "Edit file at cursor in another window"))
441 (define-key map [menu-bar immediate find-file]
442 '(menu-item "Find This File" archive-extract
443 :help "Extract file at cursor and edit it"))
445 (define-key map [menu-bar mark]
446 (cons "Mark" (make-sparse-keymap "Mark")))
447 (define-key map [menu-bar mark unmark-all]
448 '(menu-item "Unmark All" archive-unmark-all-files
449 :help "Unmark all marked files"))
450 (define-key map [menu-bar mark deletion]
451 '(menu-item "Flag" archive-flag-deleted
452 :help "Flag file at cursor for deletion"))
453 (define-key map [menu-bar mark unmark]
454 '(menu-item "Unflag" archive-unflag
455 :help "Unmark file at cursor"))
456 (define-key map [menu-bar mark mark]
457 '(menu-item "Mark" archive-mark
458 :help "Mark file at cursor"))
460 (define-key map [menu-bar operate]
461 (cons "Operate" (make-sparse-keymap "Operate")))
462 (define-key map [menu-bar operate chown]
463 '(menu-item "Change Owner..." archive-chown-entry
464 :enable (fboundp (archive-name "chown-entry"))
465 :help "Change owner of marked files"))
466 (define-key map [menu-bar operate chgrp]
467 '(menu-item "Change Group..." archive-chgrp-entry
468 :enable (fboundp (archive-name "chgrp-entry"))
469 :help "Change group ownership of marked files"))
470 (define-key map [menu-bar operate chmod]
471 '(menu-item "Change Mode..." archive-chmod-entry
472 :enable (fboundp (archive-name "chmod-entry"))
473 :help "Change mode (permissions) of marked files"))
474 (define-key map [menu-bar operate rename]
475 '(menu-item "Rename to..." archive-rename-entry
476 :enable (fboundp (archive-name "rename-entry"))
477 :help "Rename marked files"))
478 ;;(define-key map [menu-bar operate copy]
479 ;; '(menu-item "Copy to..." archive-copy))
480 (define-key map [menu-bar operate expunge]
481 '(menu-item "Expunge Marked Files" archive-expunge
482 :help "Delete all flagged files from archive"))
483 map))
484 "Local keymap for archive mode listings.")
485 (defvar archive-file-name-indent nil "Column where file names start.")
487 (defvar archive-remote nil "Non-nil if the archive is outside file system.")
488 (make-variable-buffer-local 'archive-remote)
489 (put 'archive-remote 'permanent-local t)
491 (defvar archive-member-coding-system nil "Coding-system of archive member.")
492 (make-variable-buffer-local 'archive-member-coding-system)
494 (defvar archive-alternate-display nil
495 "Non-nil when alternate information is shown.")
496 (make-variable-buffer-local 'archive-alternate-display)
497 (put 'archive-alternate-display 'permanent-local t)
499 (defvar archive-superior-buffer nil "In archive members, points to archive.")
500 (put 'archive-superior-buffer 'permanent-local t)
502 (defvar archive-subfile-mode nil "Non-nil in archive member buffers.")
503 (make-variable-buffer-local 'archive-subfile-mode)
504 (put 'archive-subfile-mode 'permanent-local t)
506 (defvar archive-file-name-coding-system nil)
507 (make-variable-buffer-local 'archive-file-name-coding-system)
508 (put 'archive-file-name-coding-system 'permanent-local t)
510 (defvar archive-files nil
511 "Vector of file descriptors.
512 Each descriptor is a vector of the form
513 [EXT-FILE-NAME INT-FILE-NAME CASE-FIDDLED MODE ...]")
514 (make-variable-buffer-local 'archive-files)
516 ;; -------------------------------------------------------------------------
517 ;;; Section: Support functions.
519 (eval-when-compile
520 (defsubst byte-after (pos)
521 "Like char-after but an eight-bit char is converted to unibyte."
522 (multibyte-char-to-unibyte (char-after pos)))
523 (defsubst insert-unibyte (&rest args)
524 "Like insert but don't make unibyte string and eight-bit char multibyte."
525 (dolist (elt args)
526 (if (integerp elt)
527 (insert (if (< elt 128) elt (decode-char 'eight-bit elt)))
528 (insert (string-to-multibyte elt)))))
531 (defsubst archive-name (suffix)
532 (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
534 (defun archive-l-e (str &optional len)
535 "Convert little endian string/vector STR to integer.
536 Alternatively, STR may be a buffer position in the current buffer
537 in which case a second argument, length LEN, should be supplied."
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 (+ (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 2048 mode)) ?- ?S)
561 (if (zerop (logand 2048 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 1024 mode)) ?- ?S)
566 (if (zerop (logand 1024 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 (+ (ash 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 (set-buffer-multibyte 'to)
750 (archive-summarize nil)
751 (setq buffer-read-only t)
752 (when (and archive-visit-single-files
753 auto-compression-mode
754 (= (length archive-files) 1))
755 (rename-buffer (concat " " (buffer-name)))
756 (archive-extract)))))
758 ;; Archive mode is suitable only for specially formatted data.
759 (put 'archive-mode 'mode-class 'special)
761 (let ((item1 '(archive-subfile-mode " Archive")))
762 (or (member item1 minor-mode-alist)
763 (setq minor-mode-alist (cons item1 minor-mode-alist))))
764 ;; -------------------------------------------------------------------------
765 (defun archive-find-type ()
766 (widen)
767 (goto-char (point-min))
768 ;; The funny [] here make it unlikely that the .elc file will be treated
769 ;; as an archive by other software.
770 (let (case-fold-search)
771 (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
772 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
773 ((looking-at "....................[\334]\247\304\375") 'zoo)
774 ((and (looking-at "\C-z") ; signature too simple, IMHO
775 (string-match "\\.[aA][rR][cC]\\'"
776 (or buffer-file-name (buffer-name))))
777 'arc)
778 ;; This pattern modeled on the BSD/GNU+Linux `file' command.
779 ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
780 ;; Note this regexp is also in archive-exe-p.
781 ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
782 ((looking-at "Rar!") 'rar)
783 ((looking-at "!<arch>\n") 'ar)
784 ((and (looking-at "MZ")
785 (re-search-forward "Rar!" (+ (point) 100000) t))
786 'rar-exe)
787 ((looking-at "7z\274\257\047\034") '7z)
788 (t (error "Buffer format not recognized")))))
789 ;; -------------------------------------------------------------------------
791 (defun archive-desummarize ()
792 (let ((inhibit-read-only t)
793 (modified (buffer-modified-p)))
794 (widen)
795 (delete-region (point-min) archive-proper-file-start)
796 (restore-buffer-modified-p modified)))
799 (defun archive-summarize (&optional shut-up)
800 "Parse the contents of the archive file in the current buffer.
801 Place a dired-like listing on the front;
802 then narrow to it, so that only that listing
803 is visible (and the real data of the buffer is hidden).
804 Optional argument SHUT-UP, if non-nil, means don't print messages
805 when parsing the archive."
806 (widen)
807 (let ((create-lockfiles nil) ; avoid changing dir mtime by lock_file
808 (inhibit-read-only t))
809 (setq archive-proper-file-start (copy-marker (point-min) t))
810 (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
811 (or shut-up
812 (message "Parsing archive file..."))
813 (buffer-disable-undo (current-buffer))
814 (setq archive-files (funcall (archive-name "summarize")))
815 (or shut-up
816 (message "Parsing archive file...done."))
817 (setq archive-proper-file-start (point-marker))
818 (narrow-to-region (point-min) (point))
819 (set-buffer-modified-p nil)
820 (buffer-enable-undo))
821 (goto-char archive-file-list-start)
822 (archive-next-line 0))
824 (defun archive-resummarize ()
825 "Recreate the contents listing of an archive."
826 (let ((no (archive-get-lineno)))
827 (archive-desummarize)
828 (archive-summarize t)
829 (goto-char archive-file-list-start)
830 (archive-next-line no)))
832 (defun archive-summarize-files (files)
833 "Insert a description of a list of files annotated with proper mouse face."
834 (setq archive-file-list-start (point-marker))
835 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
836 ;; We don't want to do an insert for each element since that takes too
837 ;; long when the archive -- which has to be moved in memory -- is large.
838 (insert
839 (apply
840 #'concat
841 (mapcar
842 (lambda (fil)
843 ;; Using `concat' here copies the text also, so we can add
844 ;; properties without problems.
845 (let ((text (concat (aref fil 0) "\n")))
846 (if (featurep 'xemacs)
847 () ; out of luck
848 (add-text-properties
849 (aref fil 1) (aref fil 2)
850 '(mouse-face highlight
851 help-echo "mouse-2: extract this file into a buffer")
852 text))
853 text))
854 files)))
855 (setq archive-file-list-end (point-marker)))
857 (defun archive-alternate-display ()
858 "Toggle alternative display.
859 To avoid very long lines archive mode does not show all information.
860 This function changes the set of information shown for each files."
861 (interactive)
862 (setq archive-alternate-display (not archive-alternate-display))
863 (archive-resummarize))
864 ;; -------------------------------------------------------------------------
865 ;;; Section: Local archive copy handling
867 (defun archive-unique-fname (fname dir)
868 "Make sure a file FNAME can be created uniquely in directory DIR.
870 If FNAME can be uniquely created in DIR, it is returned unaltered.
871 If FNAME is something our underlying filesystem can't grok, or if another
872 file by that name already exists in DIR, a unique new name is generated
873 using `make-temp-file', and the generated name is returned."
874 (let ((fullname (expand-file-name fname dir))
875 (alien (string-match file-name-invalid-regexp fname))
876 (tmpfile
877 (expand-file-name
878 (if (if (fboundp 'msdos-long-file-names)
879 (not (msdos-long-file-names)))
880 "am"
881 "arc-mode.")
882 dir)))
883 (if (or alien (file-exists-p fullname))
884 (progn
885 ;; Make sure all the leading directories in
886 ;; archive-local-name exist under archive-tmpdir, so that
887 ;; the directory structure recorded in the archive is
888 ;; reconstructed in the temporary directory.
889 (make-directory (file-name-directory tmpfile) t)
890 (make-temp-file tmpfile))
891 ;; Make sure all the leading directories in `fullname' exist
892 ;; under archive-tmpdir. This is necessary for nested archives
893 ;; (`archive-extract' sets `archive-remote' to t in case
894 ;; an archive occurs inside another archive).
895 (make-directory (file-name-directory fullname) t)
896 fullname)))
898 (defun archive-maybe-copy (archive)
899 (let ((coding-system-for-write 'no-conversion))
900 (if archive-remote
901 (let ((start (point-max))
902 ;; Sometimes ARCHIVE is invalid while its actual name, as
903 ;; recorded in its parent archive, is not. For example, an
904 ;; archive bar.zip inside another archive foo.zip gets a name
905 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
906 ;; So use the actual name if available.
907 (archive-name
908 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
909 archive)))
910 (setq archive-local-name
911 (archive-unique-fname archive-name archive-tmpdir))
912 (save-restriction
913 (widen)
914 (write-region start (point-max) archive-local-name nil 'nomessage))
915 archive-local-name)
916 (if (buffer-modified-p) (save-buffer))
917 archive)))
919 (defun archive-maybe-update (unchanged)
920 (if archive-remote
921 (let ((name archive-local-name)
922 (modified (buffer-modified-p))
923 (coding-system-for-read 'no-conversion)
924 (lno (archive-get-lineno))
925 (inhibit-read-only t))
926 (if unchanged nil
927 (setq archive-files nil)
928 (erase-buffer)
929 (insert-file-contents name)
930 (archive-mode t)
931 (goto-char archive-file-list-start)
932 (archive-next-line lno))
933 (archive-delete-local name)
934 (if (not unchanged)
935 (message
936 "Buffer `%s' must be saved for changes to take effect"
937 (buffer-name (current-buffer))))
938 (set-buffer-modified-p (or modified (not unchanged))))))
940 (defun archive-delete-local (name)
941 "Delete file NAME and its parents up to and including `archive-tmpdir'."
942 (let ((again t)
943 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
944 (condition-case nil
945 (delete-file name)
946 (error nil))
947 (while again
948 (setq name (directory-file-name (file-name-directory name)))
949 (condition-case nil
950 (delete-directory name)
951 (error nil))
952 (if (string= name top) (setq again nil)))))
953 ;; -------------------------------------------------------------------------
954 ;;; Section: Member extraction
956 (defun archive-try-jka-compr ()
957 (when (and auto-compression-mode
958 (jka-compr-get-compression-info buffer-file-name))
959 (let* ((basename (file-name-nondirectory buffer-file-name))
960 (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
961 (match-string 1 basename) basename))
962 (tmpfile (make-temp-file (file-name-sans-extension tmpname)
964 (file-name-extension tmpname 'period))))
965 (unwind-protect
966 (progn
967 (let ((coding-system-for-write 'no-conversion)
968 ;; Don't re-compress this data just before decompressing it.
969 (jka-compr-inhibit t))
970 (write-region (point-min) (point-max) tmpfile nil 'quiet))
971 (erase-buffer)
972 (let ((coding-system-for-read 'no-conversion))
973 (insert-file-contents tmpfile)))
974 (delete-file tmpfile)))))
976 (defun archive-file-name-handler (op &rest args)
977 (or (eq op 'file-exists-p)
978 (let ((file-name-handler-alist nil))
979 (apply op args))))
981 (defun archive-set-buffer-as-visiting-file (filename)
982 "Set the current buffer as if it were visiting FILENAME."
983 (save-excursion
984 (goto-char (point-min))
985 (let ((buffer-undo-list t)
986 (coding
987 (or coding-system-for-read
988 (and set-auto-coding-function
989 (save-excursion
990 (funcall set-auto-coding-function
991 filename (- (point-max) (point-min)))))
992 ;; The following let-binding of file-name-handler-alist forces
993 ;; find-file-not-found-set-buffer-file-coding-system to ignore
994 ;; the file's name (see dos-w32.el).
995 (let ((file-name-handler-alist
996 '(("" . archive-file-name-handler))))
997 (car (find-operation-coding-system
998 'insert-file-contents
999 (cons filename (current-buffer)) t))))))
1000 (unless (or coding-system-for-read
1001 enable-multibyte-characters)
1002 (setq coding
1003 (coding-system-change-text-conversion coding 'raw-text)))
1004 (unless (memq coding '(nil no-conversion))
1005 (decode-coding-region (point-min) (point-max) coding)
1006 (setq last-coding-system-used coding))
1007 (set-buffer-modified-p nil)
1008 (kill-local-variable 'buffer-file-coding-system)
1009 (after-insert-file-set-coding (- (point-max) (point-min))))))
1011 (defun archive-extract (&optional other-window-p event)
1012 "In archive mode, extract this entry of the archive into its own buffer."
1013 (interactive (list nil last-input-event))
1014 (if event (posn-set-point (event-end event)))
1015 (let* ((view-p (eq other-window-p 'view))
1016 (descr (archive-get-descr))
1017 (ename (aref descr 0))
1018 (iname (aref descr 1))
1019 (archive-buffer (current-buffer))
1020 (arcdir default-directory)
1021 (archive (buffer-file-name))
1022 (arcname (file-name-nondirectory archive))
1023 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
1024 (extractor (archive-name "extract"))
1025 ;; Members with file names which aren't valid for the
1026 ;; underlying filesystem, are treated as read-only.
1027 (read-only-p (or archive-read-only
1028 view-p
1029 (string-match file-name-invalid-regexp ename)))
1030 (arcfilename (expand-file-name (concat arcname ":" iname)))
1031 (buffer (get-buffer bufname))
1032 (just-created nil)
1033 (file-name-coding archive-file-name-coding-system))
1034 (if (and buffer
1035 (string= (buffer-file-name buffer) arcfilename))
1037 (setq archive (archive-maybe-copy archive))
1038 (setq bufname (generate-new-buffer-name bufname))
1039 (setq buffer (get-buffer-create bufname))
1040 (setq just-created t)
1041 (with-current-buffer buffer
1042 (setq buffer-file-name arcfilename)
1043 (setq buffer-file-truename
1044 (abbreviate-file-name buffer-file-name))
1045 ;; Set the default-directory to the dir of the superior buffer.
1046 (setq default-directory arcdir)
1047 (make-local-variable 'archive-superior-buffer)
1048 (setq archive-superior-buffer archive-buffer)
1049 (add-hook 'write-file-functions #'archive-write-file-member nil t)
1050 (setq archive-subfile-mode descr)
1051 (setq archive-file-name-coding-system file-name-coding)
1052 (if (and
1053 (null
1054 (let (;; We may have to encode the file name argument for
1055 ;; external programs.
1056 (coding-system-for-write
1057 (and enable-multibyte-characters
1058 archive-file-name-coding-system))
1059 ;; We read an archive member by no-conversion at
1060 ;; first, then decode appropriately by calling
1061 ;; archive-set-buffer-as-visiting-file later.
1062 (coding-system-for-read 'no-conversion)
1063 ;; Avoid changing dir mtime by lock_file
1064 (create-lockfiles nil))
1065 (condition-case err
1066 (if (fboundp extractor)
1067 (funcall extractor archive ename)
1068 (archive-*-extract archive ename
1069 (symbol-value extractor)))
1070 (error
1071 (ding (message "%s" (error-message-string err)))
1072 nil))))
1073 just-created)
1074 (progn
1075 (set-buffer-modified-p nil)
1076 (kill-buffer buffer))
1077 (archive-try-jka-compr) ;Pretty ugly hack :-(
1078 (archive-set-buffer-as-visiting-file ename)
1079 (goto-char (point-min))
1080 (rename-buffer bufname)
1081 (setq buffer-read-only read-only-p)
1082 (setq buffer-undo-list nil)
1083 (set-buffer-modified-p nil)
1084 (setq buffer-saved-size (buffer-size))
1085 (normal-mode)
1086 ;; Just in case an archive occurs inside another archive.
1087 (when (derived-mode-p 'archive-mode)
1088 (setq archive-remote t)
1089 (if read-only-p (setq archive-read-only t))
1090 ;; We will write out the archive ourselves if it is
1091 ;; part of another archive.
1092 (remove-hook 'write-contents-functions #'archive-write-file t))
1093 (run-hooks 'archive-extract-hook)
1094 (if archive-read-only
1095 (message "Note: altering this archive is not implemented."))))
1096 (archive-maybe-update t))
1097 (or (not (buffer-name buffer))
1098 (cond
1099 (view-p
1100 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
1101 ((eq other-window-p 'display) (display-buffer buffer))
1102 (other-window-p (switch-to-buffer-other-window buffer))
1103 (t (switch-to-buffer buffer))))))
1105 (defun archive-*-extract (archive name command)
1106 (let* ((default-directory (file-name-as-directory archive-tmpdir))
1107 (tmpfile (expand-file-name (file-name-nondirectory name)
1108 default-directory))
1109 exit-status success)
1110 (make-directory (directory-file-name default-directory) t)
1111 (setq exit-status
1112 (apply #'call-process
1113 (car command)
1117 (append (cdr command) (list archive name))))
1118 (cond ((and (numberp exit-status) (zerop exit-status))
1119 (if (not (file-exists-p tmpfile))
1120 (ding (message "`%s': no such file or directory" tmpfile))
1121 (insert-file-contents tmpfile)
1122 (setq success t)))
1123 ((numberp exit-status)
1124 (ding
1125 (message "`%s' exited with status %d" (car command) exit-status)))
1126 ((stringp exit-status)
1127 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1129 (ding (message "`%s' failed" (car command)))))
1130 (archive-delete-local tmpfile)
1131 success))
1133 (defun archive-extract-by-stdout (archive name command &optional stderr-test)
1134 (let ((stderr-file (make-temp-file "arc-stderr")))
1135 (unwind-protect
1136 (prog1
1137 (apply #'call-process
1138 (car command)
1140 (if stderr-file (list t stderr-file) t)
1142 (append (cdr command) (list archive name)))
1143 (with-temp-buffer
1144 (insert-file-contents stderr-file)
1145 (goto-char (point-min))
1146 (when (if (stringp stderr-test)
1147 (not (re-search-forward stderr-test nil t))
1148 (> (buffer-size) 0))
1149 (message "%s" (buffer-string)))))
1150 (if (file-exists-p stderr-file)
1151 (delete-file stderr-file)))))
1153 (defun archive-extract-by-file (archive name command &optional stdout-test)
1154 (let ((dest (make-temp-file "arc-dir" 'dir))
1155 (stdout-file (make-temp-file "arc-stdout")))
1156 (unwind-protect
1157 (prog1
1158 (apply #'call-process
1159 (car command)
1161 `(:file ,stdout-file)
1163 `(,archive ,name ,@(cdr command) ,dest))
1164 (with-temp-buffer
1165 (insert-file-contents stdout-file)
1166 (goto-char (point-min))
1167 (when (if (stringp stdout-test)
1168 (not (re-search-forward stdout-test nil t))
1169 (> (buffer-size) 0))
1170 (message "%s" (buffer-string))))
1171 (if (file-exists-p (expand-file-name name dest))
1172 (insert-file-contents-literally (expand-file-name name dest))))
1173 (if (file-exists-p stdout-file)
1174 (delete-file stdout-file))
1175 (if (file-exists-p (expand-file-name name dest))
1176 (delete-file (expand-file-name name dest)))
1177 (while (file-name-directory name)
1178 (setq name (directory-file-name (file-name-directory name)))
1179 (when (file-directory-p (expand-file-name name dest))
1180 (delete-directory (expand-file-name name dest))))
1181 (when (file-directory-p dest)
1182 (delete-directory dest)))))
1184 (defun archive-extract-other-window ()
1185 "In archive mode, find this member in another window."
1186 (interactive)
1187 (archive-extract t))
1189 (defun archive-display-other-window ()
1190 "In archive mode, display this member in another window."
1191 (interactive)
1192 (archive-extract 'display))
1194 (defun archive-view ()
1195 "In archive mode, view the member on this line."
1196 (interactive)
1197 (archive-extract 'view))
1199 (defun archive-add-new-member (arcbuf name)
1200 "Add current buffer to the archive in ARCBUF naming it NAME."
1201 (interactive
1202 (list (get-buffer
1203 (read-buffer "Buffer containing archive: "
1204 ;; Find first archive buffer and suggest that
1205 (let ((bufs (buffer-list)))
1206 (while (and bufs
1207 (not (with-current-buffer (car bufs)
1208 (derived-mode-p 'archive-mode))))
1209 (setq bufs (cdr bufs)))
1210 (if bufs
1211 (car bufs)
1212 (error "There are no archive buffers")))
1214 (read-string "File name in archive: "
1215 (if buffer-file-name
1216 (file-name-nondirectory buffer-file-name)
1217 ""))))
1218 (with-current-buffer arcbuf
1219 (or (derived-mode-p 'archive-mode)
1220 (error "Buffer is not an archive buffer"))
1221 (if archive-read-only
1222 (error "Archive is read-only")))
1223 (if (eq arcbuf (current-buffer))
1224 (error "An archive buffer cannot be added to itself"))
1225 (if (string= name "")
1226 (error "Archive members may not be given empty names"))
1227 (let ((func (with-current-buffer arcbuf
1228 (archive-name "add-new-member")))
1229 (membuf (current-buffer)))
1230 (if (fboundp func)
1231 (with-current-buffer arcbuf
1232 (funcall func buffer-file-name membuf name))
1233 (error "Adding a new member is not supported for this archive type"))))
1234 ;; -------------------------------------------------------------------------
1235 ;;; Section: IO stuff
1237 (defun archive-write-file-member ()
1238 (save-excursion
1239 (save-restriction
1240 (message "Updating archive...")
1241 (widen)
1242 (let ((writer (with-current-buffer archive-superior-buffer
1243 (archive-name "write-file-member")))
1244 (archive (with-current-buffer archive-superior-buffer
1245 (archive-maybe-copy (buffer-file-name)))))
1246 (if (fboundp writer)
1247 (funcall writer archive archive-subfile-mode)
1248 (archive-*-write-file-member archive
1249 archive-subfile-mode
1250 (symbol-value writer)))
1251 (set-buffer-modified-p nil)
1252 (message "Updating archive...done"))
1253 (set-buffer archive-superior-buffer)
1254 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1255 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1256 ;; won't reset the coding-system of this archive member.
1257 (if (local-variable-p 'archive-member-coding-system)
1258 (setq last-coding-system-used archive-member-coding-system))
1261 (defun archive-*-write-file-member (archive descr command)
1262 (let* ((ename (aref descr 0))
1263 (tmpfile (expand-file-name ename archive-tmpdir))
1264 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1265 (default-directory (file-name-as-directory top)))
1266 (unwind-protect
1267 (progn
1268 (make-directory (file-name-directory tmpfile) t)
1269 ;; If the member is itself an archive, write it without
1270 ;; the dired-like listing we created.
1271 (if (eq major-mode 'archive-mode)
1272 (archive-write-file tmpfile)
1273 (write-region nil nil tmpfile nil 'nomessage))
1274 ;; basic-save-buffer needs last-coding-system-used to have
1275 ;; the value used to write the file, so save it before any
1276 ;; further processing clobbers it (we restore it in
1277 ;; archive-write-file-member, above).
1278 (setq archive-member-coding-system last-coding-system-used)
1279 (if (aref descr 3)
1280 ;; Set the file modes, but make sure we can read it.
1281 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1282 (setq ename
1283 (encode-coding-string ename archive-file-name-coding-system))
1284 (let* ((coding-system-for-write 'no-conversion)
1285 (exitcode (apply #'call-process
1286 (car command)
1290 (append (cdr command)
1291 (list archive ename)))))
1292 (or (zerop exitcode)
1293 (error "Updating was unsuccessful (%S)" exitcode))))
1294 (archive-delete-local tmpfile))))
1296 (defun archive-write-file (&optional file)
1297 (save-excursion
1298 (let ((coding-system-for-write 'no-conversion))
1299 (write-region archive-proper-file-start (point-max)
1300 (or file buffer-file-name) nil t)
1301 (set-buffer-modified-p nil))
1303 ;; -------------------------------------------------------------------------
1304 ;;; Section: Marking and unmarking.
1306 (defun archive-flag-deleted (p &optional type)
1307 "In archive mode, mark this member to be deleted from the archive.
1308 With a prefix argument, mark that many files."
1309 (interactive "p")
1310 (or type (setq type ?D))
1311 (beginning-of-line)
1312 (let ((sign (if (>= p 0) +1 -1))
1313 (modified (buffer-modified-p))
1314 (inhibit-read-only t))
1315 (while (not (zerop p))
1316 (if (archive-get-descr t)
1317 (progn
1318 (delete-char 1)
1319 (insert type)))
1320 (forward-line sign)
1321 (setq p (- p sign)))
1322 (restore-buffer-modified-p modified))
1323 (archive-next-line 0))
1325 (defun archive-unflag (p)
1326 "In archive mode, un-mark this member if it is marked to be deleted.
1327 With a prefix argument, un-mark that many files forward."
1328 (interactive "p")
1329 (archive-flag-deleted p ?\s))
1331 (defun archive-unflag-backwards (p)
1332 "In archive mode, un-mark this member if it is marked to be deleted.
1333 With a prefix argument, un-mark that many members backward."
1334 (interactive "p")
1335 (archive-flag-deleted (- p) ?\s))
1337 (defun archive-unmark-all-files ()
1338 "Remove all marks."
1339 (interactive)
1340 (let ((modified (buffer-modified-p))
1341 (inhibit-read-only t))
1342 (save-excursion
1343 (goto-char archive-file-list-start)
1344 (while (< (point) archive-file-list-end)
1345 (or (= (following-char) ?\s)
1346 (progn (delete-char 1) (insert ?\s)))
1347 (forward-line 1)))
1348 (restore-buffer-modified-p modified)))
1350 (defun archive-mark (p)
1351 "In archive mode, mark this member for group operations.
1352 With a prefix argument, mark that many members.
1353 Use \\[archive-unmark-all-files] to remove all marks."
1354 (interactive "p")
1355 (archive-flag-deleted p ?*))
1357 (defun archive-get-marked (mark &optional default)
1358 (let (files)
1359 (save-excursion
1360 (goto-char archive-file-list-start)
1361 (while (< (point) archive-file-list-end)
1362 (if (= (following-char) mark)
1363 (setq files (cons (archive-get-descr) files)))
1364 (forward-line 1)))
1365 (or (nreverse files)
1366 (and default
1367 (list (archive-get-descr))))))
1368 ;; -------------------------------------------------------------------------
1369 ;;; Section: Operate
1371 (defun archive-next-line (p)
1372 (interactive "p")
1373 (forward-line p)
1374 (or (eobp)
1375 (forward-char archive-file-name-indent)))
1377 (defun archive-previous-line (p)
1378 (interactive "p")
1379 (archive-next-line (- p)))
1381 (defun archive-chmod-entry (new-mode)
1382 "Change the protection bits associated with all marked or this member.
1383 The new protection bits can either be specified as an octal number or
1384 as a relative change like \"g+rw\" as for chmod(2)."
1385 (interactive "sNew mode (octal or relative): ")
1386 (if archive-read-only (error "Archive is read-only"))
1387 (let ((func (archive-name "chmod-entry")))
1388 (if (fboundp func)
1389 (progn
1390 (funcall func new-mode (archive-get-marked ?* t))
1391 (archive-resummarize))
1392 (error "Setting mode bits is not supported for this archive type"))))
1394 (defun archive-chown-entry (new-uid)
1395 "Change the owner of all marked or this member."
1396 (interactive "nNew uid: ")
1397 (if archive-read-only (error "Archive is read-only"))
1398 (let ((func (archive-name "chown-entry")))
1399 (if (fboundp func)
1400 (progn
1401 (funcall func new-uid (archive-get-marked ?* t))
1402 (archive-resummarize))
1403 (error "Setting owner is not supported for this archive type"))))
1405 (defun archive-chgrp-entry (new-gid)
1406 "Change the group of all marked or this member."
1407 (interactive "nNew gid: ")
1408 (if archive-read-only (error "Archive is read-only"))
1409 (let ((func (archive-name "chgrp-entry")))
1410 (if (fboundp func)
1411 (progn
1412 (funcall func new-gid (archive-get-marked ?* t))
1413 (archive-resummarize))
1414 (error "Setting group is not supported for this archive type"))))
1416 (defun archive-expunge ()
1417 "Do the flagged deletions."
1418 (interactive)
1419 (let (files)
1420 (save-excursion
1421 (goto-char archive-file-list-start)
1422 (while (< (point) archive-file-list-end)
1423 (if (= (following-char) ?D)
1424 (setq files (cons (aref (archive-get-descr) 0) files)))
1425 (forward-line 1)))
1426 (setq files (nreverse files))
1427 (and files
1428 (or (not archive-read-only)
1429 (error "Archive is read-only"))
1430 (or (yes-or-no-p (format "Really delete %d member%s? "
1431 (length files)
1432 (if (null (cdr files)) "" "s")))
1433 (error "Operation aborted"))
1434 (let ((archive (archive-maybe-copy (buffer-file-name)))
1435 (expunger (archive-name "expunge")))
1436 (if (fboundp expunger)
1437 (funcall expunger archive files)
1438 (archive-*-expunge archive files (symbol-value expunger)))
1439 (archive-maybe-update nil)
1440 (if archive-remote
1441 (archive-resummarize)
1442 (revert-buffer))))))
1444 (defun archive-*-expunge (archive files command)
1445 (apply #'call-process
1446 (car command)
1450 (append (cdr command) (cons archive files))))
1452 (defun archive-rename-entry (newname)
1453 "Change the name associated with this entry in the archive file."
1454 (interactive "sNew name: ")
1455 (if archive-read-only (error "Archive is read-only"))
1456 (if (string= newname "")
1457 (error "Archive members may not be given empty names"))
1458 (let ((func (archive-name "rename-entry"))
1459 (descr (archive-get-descr)))
1460 (if (fboundp func)
1461 (progn
1462 (funcall func
1463 (encode-coding-string newname
1464 archive-file-name-coding-system)
1465 descr)
1466 (archive-resummarize))
1467 (error "Renaming is not supported for this archive type"))))
1469 ;; Revert the buffer and recompute the dired-like listing.
1470 (defun archive-mode-revert (&optional _no-auto-save _no-confirm)
1471 (let ((no (archive-get-lineno)))
1472 (setq archive-files nil)
1473 (let ((revert-buffer-function nil)
1474 (coding-system-for-read 'no-conversion))
1475 (revert-buffer t t))
1476 (archive-mode)
1477 (goto-char archive-file-list-start)
1478 (archive-next-line no)))
1480 (defun archive-undo ()
1481 "Undo in an archive buffer.
1482 This doesn't recover lost files, it just undoes changes in the buffer itself."
1483 (interactive)
1484 (let ((inhibit-read-only t))
1485 (undo)))
1486 ;; -------------------------------------------------------------------------
1487 ;;; Section: Arc Archives
1489 (defun archive-arc-summarize ()
1490 (let ((p 1)
1491 (totalsize 0)
1492 (maxlen 8)
1493 files
1494 visual)
1495 (while (and (< (+ p 29) (point-max))
1496 (= (byte-after p) ?\C-z)
1497 (> (byte-after (1+ p)) 0))
1498 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1499 (fnlen (or (string-match "\0" namefld) 13))
1500 (efnname (decode-coding-string (substring namefld 0 fnlen)
1501 archive-file-name-coding-system))
1502 (csize (archive-l-e (+ p 15) 4))
1503 (moddate (archive-l-e (+ p 19) 2))
1504 (modtime (archive-l-e (+ p 21) 2))
1505 (ucsize (archive-l-e (+ p 25) 4))
1506 (fiddle (string= efnname (upcase efnname)))
1507 (ifnname (if fiddle (downcase efnname) efnname))
1508 (text (format " %8d %-11s %-8s %s"
1509 ucsize
1510 (archive-dosdate moddate)
1511 (archive-dostime modtime)
1512 ifnname)))
1513 (setq maxlen (max maxlen fnlen)
1514 totalsize (+ totalsize ucsize)
1515 visual (cons (vector text
1516 (- (length text) (length ifnname))
1517 (length text))
1518 visual)
1519 files (cons (vector efnname ifnname fiddle nil (1- p))
1520 files)
1521 p (+ p 29 csize))))
1522 (goto-char (point-min))
1523 (let ((dash (concat "- -------- ----------- -------- "
1524 (make-string maxlen ?-)
1525 "\n")))
1526 (insert "M Length Date Time File\n"
1527 dash)
1528 (archive-summarize-files (nreverse visual))
1529 (insert dash
1530 (format " %8d %d file%s"
1531 totalsize
1532 (length files)
1533 (if (= 1 (length files)) "" "s"))
1534 "\n"))
1535 (apply #'vector (nreverse files))))
1537 (defun archive-arc-rename-entry (newname descr)
1538 (if (string-match "[:\\\\/]" newname)
1539 (error "File names in arc files must not contain a directory component"))
1540 (if (> (length newname) 12)
1541 (error "File names in arc files are limited to 12 characters"))
1542 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1543 (length newname))))
1544 (inhibit-read-only t))
1545 (save-restriction
1546 (save-excursion
1547 (widen)
1548 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1549 (delete-char 13)
1550 (insert-unibyte name)))))
1551 ;; -------------------------------------------------------------------------
1552 ;;; Section: Lzh Archives
1554 (defun archive-lzh-summarize (&optional start)
1555 (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
1556 (totalsize 0)
1557 (maxlen 8)
1558 files
1559 visual)
1560 (while (progn (goto-char p) ;beginning of a base header.
1561 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1562 (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
1563 (csize (archive-l-e (+ p 7) 4)) ;size of a compressed file to follow (level 0 and 2),
1564 ;size of extended headers + the compressed file to follow (level 1).
1565 (ucsize (archive-l-e (+ p 11) 4)) ;size of an uncompressed file.
1566 (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
1567 (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
1568 (hdrlvl (byte-after (+ p 20))) ;header level
1569 thsize ;total header size (base + extensions)
1570 fnlen efnname osid fiddle ifnname width p2
1571 neh ;beginning of next extension header (level 1 and 2)
1572 mode modestr uid gid text dir prname
1573 gname uname modtime moddate)
1574 (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
1575 (when (or (= hdrlvl 0) (= hdrlvl 1))
1576 (setq fnlen (byte-after (+ p 21))) ;filename length
1577 (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
1578 (decode-coding-string
1579 str archive-file-name-coding-system)))
1580 (setq p2 (+ p 22 fnlen))) ;
1581 (if (= hdrlvl 1)
1582 (setq neh (+ p2 3)) ;specific to level 1 header
1583 (if (= hdrlvl 2)
1584 (setq neh (+ p 24)))) ;specific to level 2 header
1585 (if neh ;if level 1 or 2 we expect extension headers to follow
1586 (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
1587 (etype (byte-after (+ neh 2)))) ;extension type
1588 (while (not (= ehsize 0))
1589 (cond
1590 ((= etype 1) ;file name
1591 (let ((i (+ neh 3)))
1592 (while (< i (+ neh ehsize))
1593 (setq efnname (concat efnname (char-to-string (byte-after i))))
1594 (setq i (1+ i)))))
1595 ((= etype 2) ;directory name
1596 (let ((i (+ neh 3)))
1597 (while (< i (+ neh ehsize))
1598 (setq dir (concat dir
1599 (if (= (byte-after i)
1600 255)
1602 (char-to-string
1603 (char-after i)))))
1604 (setq i (1+ i)))))
1605 ((= etype 80) ;Unix file permission
1606 (setq mode (archive-l-e (+ neh 3) 2)))
1607 ((= etype 81) ;UNIX file group/user ID
1608 (progn (setq uid (archive-l-e (+ neh 3) 2))
1609 (setq gid (archive-l-e (+ neh 5) 2))))
1610 ((= etype 82) ;UNIX file group name
1611 (let ((i (+ neh 3)))
1612 (while (< i (+ neh ehsize))
1613 (setq gname (concat gname (char-to-string (char-after i))))
1614 (setq i (1+ i)))))
1615 ((= etype 83) ;UNIX file user name
1616 (let ((i (+ neh 3)))
1617 (while (< i (+ neh ehsize))
1618 (setq uname (concat uname (char-to-string (char-after i))))
1619 (setq i (1+ i)))))
1621 (setq neh (+ neh ehsize))
1622 (setq ehsize (archive-l-e neh 2))
1623 (setq etype (byte-after (+ neh 2))))
1624 ;;get total header size for level 1 and 2 headers
1625 (setq thsize (- neh p))))
1626 (if (= hdrlvl 0) ;total header size
1627 (setq thsize hsize))
1628 ;; OS ID field not present in level 0 header, use code 0 "generic"
1629 ;; in that case as per lha program header.c get_header()
1630 (setq osid (cond ((= hdrlvl 0) 0)
1631 ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
1632 ((= hdrlvl 2) (char-after (+ p 23)))))
1633 ;; Filename fiddling must follow the lha program, otherwise the name
1634 ;; passed to "lha pq" etc won't match (which for an extract silently
1635 ;; results in no output). As of version 1.14i it goes from the OS ID,
1636 ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
1637 ;; converts "\" to "/".
1638 ;; - For 0 generic: generic_to_unix_filename() downcases if there's
1639 ;; no lower case already present, and converts "\" to "/".
1640 ;; - For 'm' macOS: macos_to_unix_filename() changes "/" to ":" and
1641 ;; ":" to "/"
1642 (setq fiddle (cond ((= ?M osid) t)
1643 ((= 0 osid) (string= efnname (upcase efnname)))))
1644 (setq ifnname (if fiddle (downcase efnname) efnname))
1645 (setq prname (if dir (concat dir ifnname) ifnname))
1646 (setq width (if prname (string-width prname) 0))
1647 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1648 (setq moddate (if (= hdrlvl 2)
1649 (archive-unixdate time1 time2) ;level 2 header in UNIX format
1650 (archive-dosdate time2))) ;level 0 and 1 header in DOS format
1651 (setq modtime (if (= hdrlvl 2)
1652 (archive-unixtime time1 time2)
1653 (archive-dostime time1)))
1654 (setq text (if archive-alternate-display
1655 (format " %8d %5S %5S %s"
1656 ucsize
1657 (or uid "?")
1658 (or gid "?")
1659 ifnname)
1660 (format " %10s %8d %-11s %-8s %s"
1661 modestr
1662 ucsize
1663 moddate
1664 modtime
1665 prname)))
1666 (setq maxlen (max maxlen width)
1667 totalsize (+ totalsize ucsize)
1668 visual (cons (vector text
1669 (- (length text) (length prname))
1670 (length text))
1671 visual)
1672 files (cons (vector prname ifnname fiddle mode (1- p))
1673 files))
1674 (cond ((= hdrlvl 1)
1675 (setq p (+ p hsize 2 csize)))
1676 ((or (= hdrlvl 2) (= hdrlvl 0))
1677 (setq p (+ p thsize 2 csize))))
1679 (goto-char (point-min))
1680 (let ((dash (concat (if archive-alternate-display
1681 "- -------- ----- ----- "
1682 "- ---------- -------- ----------- -------- ")
1683 (make-string maxlen ?-)
1684 "\n"))
1685 (header (if archive-alternate-display
1686 "M Length Uid Gid File\n"
1687 "M Filemode Length Date Time File\n"))
1688 (sumline (if archive-alternate-display
1689 " %8.0f %d file%s"
1690 " %8.0f %d file%s")))
1691 (insert header dash)
1692 (archive-summarize-files (nreverse visual))
1693 (insert dash
1694 (format sumline
1695 totalsize
1696 (length files)
1697 (if (= 1 (length files)) "" "s"))
1698 "\n"))
1699 (apply #'vector (nreverse files))))
1701 (defconst archive-lzh-alternate-display t)
1703 (defun archive-lzh-extract (archive name)
1704 (archive-extract-by-stdout archive name archive-lzh-extract))
1706 (defun archive-lzh-resum (p count)
1707 (let ((sum 0))
1708 (while (> count 0)
1709 (setq count (1- count)
1710 sum (+ sum (byte-after p))
1711 p (1+ p)))
1712 (logand sum 255)))
1714 (defun archive-lzh-rename-entry (newname descr)
1715 (save-restriction
1716 (save-excursion
1717 (widen)
1718 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1719 (oldhsize (byte-after p))
1720 (oldfnlen (byte-after (+ p 21)))
1721 (newfnlen (length newname))
1722 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1723 (inhibit-read-only t))
1724 (if (> newhsize 255)
1725 (error "The file name is too long"))
1726 (goto-char (+ p 21))
1727 (delete-char (1+ oldfnlen))
1728 (insert-unibyte newfnlen newname)
1729 (goto-char p)
1730 (delete-char 2)
1731 (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
1733 (defun archive-lzh-ogm (newval files errtxt ofs)
1734 (save-excursion
1735 (save-restriction
1736 (widen)
1737 (dolist (fil files)
1738 (let* ((p (+ archive-proper-file-start (aref fil 4)))
1739 (hsize (byte-after p))
1740 (fnlen (byte-after (+ p 21)))
1741 (p2 (+ p 22 fnlen))
1742 (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
1743 (inhibit-read-only t))
1744 (if (= creator ?U)
1745 (progn
1746 (or (numberp newval)
1747 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1748 (goto-char (+ p2 ofs))
1749 (delete-char 2)
1750 (insert-unibyte (logand newval 255) (ash newval -8))
1751 (goto-char (1+ p))
1752 (delete-char 1)
1753 (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
1754 (message "Member %s does not have %s field"
1755 (aref fil 1) errtxt)))))))
1757 (defun archive-lzh-chown-entry (newuid files)
1758 (archive-lzh-ogm newuid files "an uid" 10))
1760 (defun archive-lzh-chgrp-entry (newgid files)
1761 (archive-lzh-ogm newgid files "a gid" 12))
1763 (defun archive-lzh-chmod-entry (newmode files)
1764 (archive-lzh-ogm
1765 ;; This should work even though newmode will be dynamically accessed.
1766 (lambda (old) (archive-calc-mode old newmode t))
1767 files "a unix-style mode" 8))
1769 ;; -------------------------------------------------------------------------
1770 ;;; Section: Lzh Self-Extracting .exe Archives
1772 ;; No support for modifying these files. It looks like the lha for unix
1773 ;; program (as of version 1.14i) can't create or retain the DOS exe part.
1774 ;; If you do an "lha a" on a .exe for instance it renames and writes to a
1775 ;; plain .lzh.
1777 (defun archive-lzh-exe-summarize ()
1778 "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
1780 ;; Skip the initial executable code part and apply archive-lzh-summarize
1781 ;; to the archive part proper. The "-lh5-" etc regexp here for the start
1782 ;; is the same as in archive-find-type.
1784 ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
1785 ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
1786 ;; the header checksum, or level 2 a test of the "attribute" and size.
1788 (re-search-forward "..-l[hz][0-9ds]-" nil)
1789 (archive-lzh-summarize (match-beginning 0)))
1791 ;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
1792 ;; .lzh files
1793 (defalias 'archive-lzh-exe-extract 'archive-lzh-extract
1794 "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
1796 ;; -------------------------------------------------------------------------
1797 ;;; Section: Zip Archives
1799 (defun archive-zip-summarize ()
1800 (goto-char (- (point-max) (- 22 18)))
1801 (search-backward-regexp "[P]K\005\006")
1802 (let ((p (archive-l-e (+ (point) 16) 4))
1803 (maxlen 8)
1804 (totalsize 0)
1805 files
1806 visual
1807 emacs-int-has-32bits)
1808 (when (= p -1)
1809 ;; If the offset of end-of-central-directory is -1, this is a
1810 ;; Zip64 extended ZIP file format, and we need to glean the info
1811 ;; from Zip64 records instead.
1813 ;; First, find the Zip64 end-of-central-directory locator.
1814 (search-backward "PK\006\007")
1815 (setq p (+ (point-min)
1816 (archive-l-e (+ (point) 8) 8)))
1817 (goto-char p)
1818 ;; We should be at Zip64 end-of-central-directory record now.
1819 (or (string= "PK\006\006" (buffer-substring p (+ p 4)))
1820 (error "Unrecognized ZIP file format"))
1821 ;; Offset to central directory:
1822 (setq p (archive-l-e (+ p 48) 8)))
1823 (setq p (+ p (point-min)))
1824 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1825 (let* ((creator (byte-after (+ p 5)))
1826 ;; (method (archive-l-e (+ p 10) 2))
1827 (modtime (archive-l-e (+ p 12) 2))
1828 (moddate (archive-l-e (+ p 14) 2))
1829 (ucsize (archive-l-e (+ p 24) 4))
1830 (fnlen (archive-l-e (+ p 28) 2))
1831 (exlen (archive-l-e (+ p 30) 2))
1832 (fclen (archive-l-e (+ p 32) 2))
1833 (lheader (archive-l-e (+ p 42) 4))
1834 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1835 (decode-coding-string
1836 str archive-file-name-coding-system)))
1837 (isdir (and (= ucsize 0)
1838 (string= (file-name-nondirectory efnname) "")))
1839 (mode (cond ((memq creator '(2 3)) ; Unix
1840 (archive-l-e (+ p 40) 2))
1841 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1842 (logior ?\444
1843 (if isdir (logior 16384 ?\111) 0)
1844 (if (zerop
1845 (logand 1 (byte-after (+ p 38))))
1846 ?\222 0)))
1847 (t nil)))
1848 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1849 (fiddle (and archive-zip-case-fiddle
1850 (not (not (memq creator '(0 2 4 5 9))))
1851 (string= (upcase efnname) efnname)))
1852 (ifnname (if fiddle (downcase efnname) efnname))
1853 (width (string-width ifnname))
1854 (text (format " %10s %8d %-11s %-8s %s"
1855 modestr
1856 ucsize
1857 (archive-dosdate moddate)
1858 (archive-dostime modtime)
1859 ifnname)))
1860 (setq maxlen (max maxlen width)
1861 totalsize (+ totalsize ucsize)
1862 visual (cons (vector text
1863 (- (length text) (length ifnname))
1864 (length text))
1865 visual)
1866 files (cons (if isdir
1868 (vector efnname ifnname fiddle mode
1869 (list (1- p) lheader)))
1870 files)
1871 p (+ p 46 fnlen exlen fclen))))
1872 (goto-char (point-min))
1873 (let ((dash (concat "- ---------- -------- ----------- -------- "
1874 (make-string maxlen ?-)
1875 "\n")))
1876 (insert "M Filemode Length Date Time File\n"
1877 dash)
1878 (archive-summarize-files (nreverse visual))
1879 (insert dash
1880 (format " %8d %d file%s"
1881 totalsize
1882 (length files)
1883 (if (= 1 (length files)) "" "s"))
1884 "\n"))
1885 (apply #'vector (nreverse files))))
1887 (defun archive-zip-extract (archive name)
1888 (cond
1889 ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
1890 (archive-*-extract archive name archive-zip-extract))
1891 ((equal (car archive-zip-extract) archive-7z-program)
1892 (let ((archive-7z-extract archive-zip-extract))
1893 (archive-7z-extract archive name)))
1895 (archive-extract-by-stdout
1896 archive
1897 ;; unzip expands wildcards in NAME, so we need to quote it. But
1898 ;; not on DOS/Windows, since that fails extraction on those
1899 ;; systems (unless w32-quote-process-args is nil), and file names
1900 ;; with wildcards in zip archives don't work there anyway.
1901 ;; FIXME: Does pkunzip need similar treatment?
1902 (if (and (or (not (memq system-type '(windows-nt ms-dos)))
1903 (and (boundp 'w32-quote-process-args)
1904 (null w32-quote-process-args)))
1905 (equal (car archive-zip-extract) "unzip"))
1906 (shell-quote-argument name)
1907 name)
1908 archive-zip-extract))))
1910 (defun archive-zip-write-file-member (archive descr)
1911 (archive-*-write-file-member
1912 archive
1913 descr
1914 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1916 (defun archive-zip-chmod-entry (newmode files)
1917 (save-restriction
1918 (save-excursion
1919 (widen)
1920 (dolist (fil files)
1921 (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
1922 (creator (byte-after (+ p 5)))
1923 (oldmode (aref fil 3))
1924 (newval (archive-calc-mode oldmode newmode t))
1925 (inhibit-read-only t))
1926 (cond ((memq creator '(2 3)) ; Unix
1927 (goto-char (+ p 40))
1928 (delete-char 2)
1929 (insert-unibyte (logand newval 255) (ash newval -8)))
1930 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1931 (goto-char (+ p 38))
1932 (insert-unibyte (logior (logand (byte-after (point)) 254)
1933 (logand (logxor 1 (ash newval -7)) 1)))
1934 (delete-char 1))
1935 (t (message "Don't know how to change mode for this member"))))
1936 ))))
1937 ;; -------------------------------------------------------------------------
1938 ;;; Section: Zoo Archives
1940 (defun archive-zoo-summarize ()
1941 (let ((p (1+ (archive-l-e 25 4)))
1942 (maxlen 8)
1943 (totalsize 0)
1944 files
1945 visual)
1946 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1947 (> (archive-l-e (+ p 6) 4) 0))
1948 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1949 (moddate (archive-l-e (+ p 14) 2))
1950 (modtime (archive-l-e (+ p 16) 2))
1951 (ucsize (archive-l-e (+ p 20) 4))
1952 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1953 (dirtype (byte-after (+ p 4)))
1954 (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
1955 (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
1956 (fnlen (or (string-match "\0" namefld) 13))
1957 (efnname (let ((str
1958 (concat
1959 (if (> ldirlen 0)
1960 (concat (buffer-substring
1961 (+ p 58 lfnlen)
1962 (+ p 58 lfnlen ldirlen -1))
1963 "/")
1965 (if (> lfnlen 0)
1966 (buffer-substring (+ p 58)
1967 (+ p 58 lfnlen -1))
1968 (substring namefld 0 fnlen)))))
1969 (decode-coding-string
1970 str archive-file-name-coding-system)))
1971 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1972 (ifnname (if fiddle (downcase efnname) efnname))
1973 (width (string-width ifnname))
1974 (text (format " %8d %-11s %-8s %s"
1975 ucsize
1976 (archive-dosdate moddate)
1977 (archive-dostime modtime)
1978 ifnname)))
1979 (setq maxlen (max maxlen width)
1980 totalsize (+ totalsize ucsize)
1981 visual (cons (vector text
1982 (- (length text) (length ifnname))
1983 (length text))
1984 visual)
1985 files (cons (vector efnname ifnname fiddle nil (1- p))
1986 files)
1987 p next)))
1988 (goto-char (point-min))
1989 (let ((dash (concat "- -------- ----------- -------- "
1990 (make-string maxlen ?-)
1991 "\n")))
1992 (insert "M Length Date Time File\n"
1993 dash)
1994 (archive-summarize-files (nreverse visual))
1995 (insert dash
1996 (format " %8d %d file%s"
1997 totalsize
1998 (length files)
1999 (if (= 1 (length files)) "" "s"))
2000 "\n"))
2001 (apply #'vector (nreverse files))))
2003 (defun archive-zoo-extract (archive name)
2004 (archive-extract-by-stdout archive name archive-zoo-extract))
2006 ;; -------------------------------------------------------------------------
2007 ;;; Section: Rar Archives
2009 (defun archive-rar-summarize (&optional file)
2010 ;; File is used internally for `archive-rar-exe-summarize'.
2011 (unless file (setq file buffer-file-name))
2012 (let* ((copy (file-local-copy file))
2013 (maxname 10)
2014 (maxsize 5)
2015 (files ()))
2016 (with-temp-buffer
2017 (call-process "lsar" nil t nil "-l" (or file copy))
2018 (if copy (delete-file copy))
2019 (goto-char (point-min))
2020 (re-search-forward "^\\(\s+=+\s?+\\)+\n")
2021 (while (looking-at (concat "^\s+[0-9.]+\s+D?-+\s+" ; Flags
2022 "\\([0-9-]+\\)\s+" ; Size
2023 "\\([-0-9.%]+\\|-+\\)\s+" ; Ratio
2024 "\\([0-9a-zA-Z]+\\)\s+" ; Mode
2025 "\\([0-9-]+\\)\s+" ; Date
2026 "\\([0-9:]+\\)\s+" ; Time
2027 "\\(.*\\)\n" ; Name
2029 (goto-char (match-end 0))
2030 (let ((name (match-string 6))
2031 (size (match-string 1)))
2032 (if (> (length name) maxname) (setq maxname (length name)))
2033 (if (> (length size) maxsize) (setq maxsize (length size)))
2034 (push (vector name name nil nil
2035 ;; Size, Ratio.
2036 size (match-string 2)
2037 ;; Date, Time.
2038 (match-string 4) (match-string 5))
2039 files))))
2040 (setq files (nreverse files))
2041 (goto-char (point-min))
2042 (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
2043 (sep (format format "----------" "-----" (make-string maxsize ?-)
2044 "-----" ""))
2045 (column (length sep)))
2046 (insert (format format " Date " "Time " "Size" "Ratio" "Filename") "\n")
2047 (insert sep (make-string maxname ?-) "\n")
2048 (archive-summarize-files (mapcar (lambda (desc)
2049 (let ((text
2050 (format format
2051 (aref desc 6)
2052 (aref desc 7)
2053 (aref desc 4)
2054 (aref desc 5)
2055 (aref desc 1))))
2056 (vector text
2057 column
2058 (length text))))
2059 files))
2060 (insert sep (make-string maxname ?-) "\n")
2061 (apply #'vector files))))
2063 (defun archive-rar-extract (archive name)
2064 ;; unrar-free seems to have no way to extract to stdout or even to a file.
2065 (if (file-name-absolute-p name)
2066 ;; The code below assumes the name is relative and may do undesirable
2067 ;; things otherwise.
2068 (error "Can't extract files with non-relative names")
2069 (archive-extract-by-file archive name `("unar" "-no-directory" "-o") "Successfully extracted")))
2071 ;;; Section: Rar self-extracting .exe archives.
2073 (defun archive-rar-exe-summarize ()
2074 (let ((tmpfile (make-temp-file "rarexe")))
2075 (unwind-protect
2076 (progn
2077 (goto-char (point-min))
2078 (re-search-forward "Rar!")
2079 (write-region (match-beginning 0) (point-max) tmpfile)
2080 (archive-rar-summarize tmpfile))
2081 (delete-file tmpfile))))
2083 (defun archive-rar-exe-extract (archive name)
2084 (let* ((tmpfile (make-temp-file "rarexe"))
2085 (buf (find-buffer-visiting archive))
2086 (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
2087 (unwind-protect
2088 (progn
2089 (with-current-buffer (or buf tmpbuf)
2090 (save-excursion
2091 (save-restriction
2092 (if buf
2093 ;; point-max unwidened is assumed to be the end of the
2094 ;; summary text and the beginning of the actual file data.
2095 (progn (goto-char (point-max)) (widen))
2096 (insert-file-contents-literally archive)
2097 (goto-char (point-min)))
2098 (re-search-forward "Rar!")
2099 (write-region (match-beginning 0) (point-max) tmpfile))))
2100 (archive-rar-extract tmpfile name))
2101 (if tmpbuf (kill-buffer tmpbuf))
2102 (delete-file tmpfile))))
2104 ;; -------------------------------------------------------------------------
2105 ;;; Section: 7z Archives
2107 (defun archive-7z-summarize ()
2108 (let ((maxname 10)
2109 (maxsize 5)
2110 (file buffer-file-name)
2111 (files ()))
2112 (with-temp-buffer
2113 (call-process archive-7z-program nil t nil "l" "-slt" file)
2114 (goto-char (point-min))
2115 ;; Four dashes start the meta info section that should be skipped.
2116 ;; Archive members start with more than four dashes.
2117 (re-search-forward "^-----+\n")
2118 (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
2119 (goto-char (match-end 0))
2120 (let ((name (match-string 1))
2121 (size (save-excursion
2122 (and (re-search-forward "^Size = \\(.*\\)\n")
2123 (match-string 1))))
2124 (time (save-excursion
2125 (and (re-search-forward "^Modified = \\(.*\\)\n")
2126 (match-string 1)))))
2127 (if (> (length name) maxname) (setq maxname (length name)))
2128 (if (> (length size) maxsize) (setq maxsize (length size)))
2129 (push (vector name name nil nil time nil nil size)
2130 files))))
2131 (setq files (nreverse files))
2132 (goto-char (point-min))
2133 (let* ((format (format " %%%ds %%s %%s" maxsize))
2134 (sep (format format (make-string maxsize ?-) "-------------------" ""))
2135 (column (length sep)))
2136 (insert (format format "Size " "Date Time " " Filename") "\n")
2137 (insert sep (make-string maxname ?-) "\n")
2138 (archive-summarize-files (mapcar (lambda (desc)
2139 (let ((text
2140 (format format
2141 (aref desc 7)
2142 (aref desc 4)
2143 (aref desc 1))))
2144 (vector text
2145 column
2146 (length text))))
2147 files))
2148 (insert sep (make-string maxname ?-) "\n")
2149 (apply #'vector files))))
2151 (defun archive-7z-extract (archive name)
2152 ;; 7z doesn't provide a `quiet' option to suppress non-essential
2153 ;; stderr messages. So redirect stderr to a temp file and display it
2154 ;; in the echo area when it contains no message indicating success.
2155 (archive-extract-by-stdout
2156 archive name archive-7z-extract "Everything is Ok"))
2158 (defun archive-7z-write-file-member (archive descr)
2159 (archive-*-write-file-member
2160 archive
2161 descr
2162 archive-7z-update))
2164 ;; -------------------------------------------------------------------------
2165 ;;; Section `ar' archives.
2167 ;; TODO: we currently only handle the basic format of ar archives,
2168 ;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
2169 ;; for .deb packages.
2171 (autoload 'tar-grind-file-mode "tar-mode")
2173 (defconst archive-ar-file-header-re
2174 "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
2176 (defun archive-ar-summarize ()
2177 ;; File is used internally for `archive-rar-exe-summarize'.
2178 (let* ((maxname 10)
2179 (maxtime 16)
2180 (maxuser 5)
2181 (maxgroup 5)
2182 (maxmode 8)
2183 (maxsize 5)
2184 (files ()))
2185 (goto-char (point-min))
2186 (search-forward "!<arch>\n")
2187 (while (looking-at archive-ar-file-header-re)
2188 (let ((name (match-string 1))
2189 extname
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