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