lwlib/Imakefile is removed.
[emacs.git] / lisp / arc-mode.el
blobd1128abc455e3fd153733ed763a2673ae524df6b
1 ;;; arc-mode.el --- simple editing of archives
3 ;; Copyright (C) 1995, 1997, 1998 Free Software Foundation, Inc.
5 ;; Author: Morten Welinder <terra@diku.dk>
6 ;; Keywords: archives msdog editing major-mode
7 ;; Favourite-brand-of-beer: None, I hate beer.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; NAMING: "arc" is short for "archive" and does not refer specifically
29 ;; to files whose name end in ".arc"
31 ;; This code does not decode any files internally, although it does
32 ;; understand the directory level of the archives. For this reason,
33 ;; you should expect this code to need more fiddling than tar-mode.el
34 ;; (although it at present has fewer bugs :-) In particular, I have
35 ;; not tested this under Ms-Dog myself.
36 ;; -------------------------------------
37 ;; INTERACTION: arc-mode.el should play together with
39 ;; * ange-ftp.el: Remote archives (i.e., ones that ange-ftp has brought
40 ;; to you) are handled by doing all updates on a local
41 ;; copy. When you make changes to a remote file the
42 ;; changes will first take effect when the archive buffer
43 ;; is saved. You will be warned about this.
45 ;; * dos-fns.el: (Part of Emacs 19). You get automatic ^M^J <--> ^J
46 ;; conversion.
48 ;; arc-mode.el does not work well with crypt++.el; for the archives as
49 ;; such this could be fixed (but wouldn't be useful) by declaring such
50 ;; archives to be "remote". For the members this is a general Emacs
51 ;; problem that 19.29's file formats may fix.
52 ;; -------------------------------------
53 ;; ARCHIVE TYPES: Currently only the archives below are handled, but the
54 ;; structure for handling just about anything is in place.
56 ;; Arc Lzh Zip Zoo
57 ;; --------------------------------
58 ;; View listing Intern Intern Intern Intern
59 ;; Extract member Y Y Y Y
60 ;; Save changed member Y Y Y Y
61 ;; Add new member N N N N
62 ;; Delete member Y Y Y Y
63 ;; Rename member Y Y N N
64 ;; Chmod - Y Y -
65 ;; Chown - Y - -
66 ;; Chgrp - Y - -
68 ;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
69 ;; on the first released version of this package.
71 ;; This code is partly based on tar-mode.el from Emacs.
72 ;; -------------------------------------
73 ;; ARCHIVE STRUCTURES:
74 ;; (This is mostly for myself.)
76 ;; ARC A series of (header,file). No interactions among members.
78 ;; LZH A series of (header,file). Headers are checksummed. No
79 ;; interaction among members.
81 ;; ZIP A series of (lheader,fil) followed by a "central directory"
82 ;; which is a series of (cheader) followed by an end-of-
83 ;; central-dir record possibly followed by junk. The e-o-c-d
84 ;; links to c-d. cheaders link to lheaders which are basically
85 ;; cut-down versions of the cheaders.
87 ;; ZOO An archive header followed by a series of (header,file).
88 ;; Each member header points to the next. The archive is
89 ;; terminated by a bogus header with a zero next link.
90 ;; -------------------------------------
91 ;; HOOKS: `foo' means one of the supported archive types.
93 ;; archive-mode-hook
94 ;; archive-foo-mode-hook
95 ;; archive-extract-hooks
97 ;;; Code:
99 ;; -------------------------------------------------------------------------
100 ;; Section: Configuration.
102 (defgroup archive nil
103 "Simple editing of archives."
104 :group 'data)
106 (defgroup archive-arc nil
107 "ARC-specific options to archive."
108 :group 'archive)
110 (defgroup archive-lzh nil
111 "LZH-specific options to archive."
112 :group 'archive)
114 (defgroup archive-zip nil
115 "ZIP-specific options to archive."
116 :group 'archive)
118 (defgroup archive-zoo nil
119 "ZOO-specific options to archive."
120 :group 'archive)
122 (defcustom archive-tmpdir
123 ;; make-temp-name is safe here because we use this name
124 ;; to create a directory.
125 (make-temp-name
126 (expand-file-name (if (eq system-type 'ms-dos) "ar" "archive.tmp")
127 temporary-file-directory))
128 "*Directory for temporary files made by arc-mode.el"
129 :type 'directory
130 :group 'archive)
132 (defcustom archive-remote-regexp "^/[^/:]*[^/:.]:"
133 "*Regexp recognizing archive files names that are not local.
134 A non-local file is one whose file name is not proper outside Emacs.
135 A local copy of the archive will be used when updating."
136 :type 'regexp
137 :group 'archive)
139 (defcustom archive-extract-hooks nil
140 "*Hooks to run when an archive member has been extracted."
141 :type 'hook
142 :group 'archive)
143 ;; ------------------------------
144 ;; Arc archive configuration
146 ;; We always go via a local file since there seems to be no reliable way
147 ;; to extract to stdout without junk getting added.
148 (defcustom archive-arc-extract
149 '("arc" "x")
150 "*Program and its options to run in order to extract an arc file member.
151 Extraction should happen to the current directory. Archive and member
152 name will be added."
153 :type '(list (string :tag "Program")
154 (repeat :tag "Options"
155 :inline t
156 (string :format "%v")))
157 :group 'archive-arc)
159 (defcustom archive-arc-expunge
160 '("arc" "d")
161 "*Program and its options to run in order to delete arc file members.
162 Archive and member names will be added."
163 :type '(list (string :tag "Program")
164 (repeat :tag "Options"
165 :inline t
166 (string :format "%v")))
167 :group 'archive-arc)
169 (defcustom archive-arc-write-file-member
170 '("arc" "u")
171 "*Program and its options to run in order to update an arc file member.
172 Archive and member name will be added."
173 :type '(list (string :tag "Program")
174 (repeat :tag "Options"
175 :inline t
176 (string :format "%v")))
177 :group 'archive-arc)
178 ;; ------------------------------
179 ;; Lzh archive configuration
181 (defcustom archive-lzh-extract
182 '("lha" "pq")
183 "*Program and its options to run in order to extract an lzh file member.
184 Extraction should happen to standard output. Archive and member name will
185 be added."
186 :type '(list (string :tag "Program")
187 (repeat :tag "Options"
188 :inline t
189 (string :format "%v")))
190 :group 'archive-lzh)
192 (defcustom archive-lzh-expunge
193 '("lha" "d")
194 "*Program and its options to run in order to delete lzh file members.
195 Archive and member names will be added."
196 :type '(list (string :tag "Program")
197 (repeat :tag "Options"
198 :inline t
199 (string :format "%v")))
200 :group 'archive-lzh)
202 (defcustom archive-lzh-write-file-member
203 '("lha" "a")
204 "*Program and its options to run in order to update an lzh file member.
205 Archive and member name will be added."
206 :type '(list (string :tag "Program")
207 (repeat :tag "Options"
208 :inline t
209 (string :format "%v")))
210 :group 'archive-lzh)
211 ;; ------------------------------
212 ;; Zip archive configuration
214 (defcustom archive-zip-use-pkzip (memq system-type '(ms-dos windows-nt))
215 "*If non-nil then pkzip option are used instead of zip options.
216 Only set to true for msdog systems!"
217 :type 'boolean
218 :group 'archive-zip)
220 (defcustom archive-zip-extract
221 (if archive-zip-use-pkzip '("pkunzip" "-e" "-o-") '("unzip" "-qq" "-c"))
222 "*Program and its options to run in order to extract a zip file member.
223 Extraction should happen to standard output. Archive and member name will
224 be added. If `archive-zip-use-pkzip' is non-nil then this program is
225 expected to extract to a file junking the directory part of the name."
226 :type '(list (string :tag "Program")
227 (repeat :tag "Options"
228 :inline t
229 (string :format "%v")))
230 :group 'archive-zip)
232 ;; For several reasons the latter behaviour is not desirable in general.
233 ;; (1) It uses more disk space. (2) Error checking is worse or non-
234 ;; existent. (3) It tends to do funny things with other systems' file
235 ;; names.
237 (defcustom archive-zip-expunge
238 (if archive-zip-use-pkzip '("pkzip" "-d") '("zip" "-d" "-q"))
239 "*Program and its options to run in order to delete zip file members.
240 Archive and member names will be added."
241 :type '(list (string :tag "Program")
242 (repeat :tag "Options"
243 :inline t
244 (string :format "%v")))
245 :group 'archive-zip)
247 (defcustom archive-zip-update
248 (if archive-zip-use-pkzip '("pkzip" "-u" "-P") '("zip" "-q"))
249 "*Program and its options to run in order to update a zip file member.
250 Options should ensure that specified directory will be put into the zip
251 file. Archive and member name will be added."
252 :type '(list (string :tag "Program")
253 (repeat :tag "Options"
254 :inline t
255 (string :format "%v")))
256 :group 'archive-zip)
258 (defcustom archive-zip-update-case
259 (if archive-zip-use-pkzip archive-zip-update '("zip" "-q" "-k"))
260 "*Program and its options to run in order to update a case fiddled zip member.
261 Options should ensure that specified directory will be put into the zip file.
262 Archive and member name will be added."
263 :type '(list (string :tag "Program")
264 (repeat :tag "Options"
265 :inline t
266 (string :format "%v")))
267 :group 'archive-zip)
269 (defcustom archive-zip-case-fiddle t
270 "*If non-nil then zip file members may be down-cased.
271 This case fiddling will only happen for members created by a system
272 that uses caseless file names."
273 :type 'boolean
274 :group 'archive-zip)
275 ;; ------------------------------
276 ;; Zoo archive configuration
278 (defcustom archive-zoo-extract
279 '("zoo" "xpq")
280 "*Program and its options to run in order to extract a zoo file member.
281 Extraction should happen to standard output. Archive and member name will
282 be added."
283 :type '(list (string :tag "Program")
284 (repeat :tag "Options"
285 :inline t
286 (string :format "%v")))
287 :group 'archive-zoo)
289 (defcustom archive-zoo-expunge
290 '("zoo" "DqPP")
291 "*Program and its options to run in order to delete zoo file members.
292 Archive and member names will be added."
293 :type '(list (string :tag "Program")
294 (repeat :tag "Options"
295 :inline t
296 (string :format "%v")))
297 :group 'archive-zoo)
299 (defcustom archive-zoo-write-file-member
300 '("zoo" "a")
301 "*Program and its options to run in order to update a zoo file member.
302 Archive and member name will be added."
303 :type '(list (string :tag "Program")
304 (repeat :tag "Options"
305 :inline t
306 (string :format "%v")))
307 :group 'archive-zoo)
308 ;; -------------------------------------------------------------------------
309 ;; Section: Variables
311 (defvar archive-subtype nil "*Symbol describing archive type.")
312 (defvar archive-file-list-start nil "*Position of first contents line.")
313 (defvar archive-file-list-end nil "*Position just after last contents line.")
314 (defvar archive-proper-file-start nil "*Position of real archive's start.")
315 (defvar archive-read-only nil "*Non-nil if the archive is read-only on disk.")
316 (defvar archive-local-name nil "*Name of local copy of remote archive.")
317 (defvar archive-mode-map nil "*Local keymap for archive mode listings.")
318 (defvar archive-file-name-indent nil "*Column where file names start.")
320 (defvar archive-remote nil "*Non-nil if the archive is outside file system.")
321 (make-variable-buffer-local 'archive-remote)
322 (put 'archive-remote 'permanent-local t)
324 (defvar archive-member-coding-system nil "Coding-system of archive member.")
325 (make-variable-buffer-local 'archive-member-coding-system)
327 (defvar archive-alternate-display nil
328 "*Non-nil when alternate information is shown.")
329 (make-variable-buffer-local 'archive-alternate-display)
330 (put 'archive-alternate-display 'permanent-local t)
332 (defvar archive-superior-buffer nil "*In archive members, points to archive.")
333 (put 'archive-superior-buffer 'permanent-local t)
335 (defvar archive-subfile-mode nil "*Non-nil in archive member buffers.")
336 (make-variable-buffer-local 'archive-subfile-mode)
337 (put 'archive-subfile-mode 'permanent-local t)
339 (defvar archive-files nil
340 "Vector of file descriptors.
341 Each descriptor is a vector of the form
342 [EXT-FILE-NAME INT-FILE-NAME CASE-FIDDLED MODE ...]")
343 (make-variable-buffer-local 'archive-files)
345 (defvar archive-lemacs
346 (string-match "\\(Lucid\\|Xemacs\\)" emacs-version)
347 "*Non-nil when running under under Lucid Emacs or Xemacs.")
348 ;; -------------------------------------------------------------------------
349 ;; Section: Support functions.
351 (defsubst archive-name (suffix)
352 (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
354 (defun archive-l-e (str &optional len)
355 "Convert little endian string/vector to integer.
356 Alternatively, first argument may be a buffer position in the current buffer
357 in which case a second argument, length, should be supplied."
358 (if (stringp str)
359 (setq len (length str))
360 (setq str (buffer-substring str (+ str len))))
361 (let ((result 0)
362 (i 0))
363 (while (< i len)
364 (setq i (1+ i)
365 result (+ (ash result 8) (aref str (- len i)))))
366 result))
368 (defun archive-int-to-mode (mode)
369 "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------."
370 ;; FIXME: merge with tar-grind-file-mode.
371 (string
372 (if (zerop (logand 8192 mode))
373 (if (zerop (logand 16384 mode)) ?- ?d)
374 ?c) ; completeness
375 (if (zerop (logand 256 mode)) ?- ?r)
376 (if (zerop (logand 128 mode)) ?- ?w)
377 (if (zerop (logand 64 mode))
378 (if (zerop (logand 1024 mode)) ?- ?S)
379 (if (zerop (logand 1024 mode)) ?x ?s))
380 (if (zerop (logand 32 mode)) ?- ?r)
381 (if (zerop (logand 16 mode)) ?- ?w)
382 (if (zerop (logand 8 mode))
383 (if (zerop (logand 2048 mode)) ?- ?S)
384 (if (zerop (logand 2048 mode)) ?x ?s))
385 (if (zerop (logand 4 mode)) ?- ?r)
386 (if (zerop (logand 2 mode)) ?- ?w)
387 (if (zerop (logand 1 mode)) ?- ?x)))
389 (defun archive-calc-mode (oldmode newmode &optional error)
390 "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
391 NEWMODE may be an octal number including a leading zero in which case it
392 will become the new mode.\n
393 NEWMODE may also be a relative specification like \"og-rwx\" in which case
394 OLDMODE will be modified accordingly just like chmod(2) would have done.\n
395 If optional third argument ERROR is non-nil an error will be signaled if
396 the mode is invalid. If ERROR is nil then nil will be returned."
397 (cond ((string-match "^0[0-7]*$" newmode)
398 (let ((result 0)
399 (len (length newmode))
400 (i 1))
401 (while (< i len)
402 (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
403 i (1+ i)))
404 (logior (logand oldmode 65024) result)))
405 ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
406 (let ((who 0)
407 (result oldmode)
408 (op (aref newmode (match-beginning 2)))
409 (bits 0)
410 (i (match-beginning 3)))
411 (while (< i (match-end 3))
412 (let ((rwx (aref newmode i)))
413 (setq bits (logior bits (cond ((= rwx ?r) 292)
414 ((= rwx ?w) 146)
415 ((= rwx ?x) 73)
416 ((= rwx ?s) 3072)
417 ((= rwx ?t) 512)))
418 i (1+ i))))
419 (while (< who (match-end 1))
420 (let* ((whoc (aref newmode who))
421 (whomask (cond ((= whoc ?a) 4095)
422 ((= whoc ?u) 1472)
423 ((= whoc ?g) 2104)
424 ((= whoc ?o) 7))))
425 (if (= op ?=)
426 (setq result (logand result (lognot whomask))))
427 (if (= op ?-)
428 (setq result (logand result (lognot (logand whomask bits))))
429 (setq result (logior result (logand whomask bits)))))
430 (setq who (1+ who)))
431 result))
433 (if error
434 (error "Invalid mode specification: %s" newmode)))))
436 (defun archive-dosdate (date)
437 "Stringify dos packed DATE record."
438 (let ((year (+ 1980 (logand (ash date -9) 127)))
439 (month (logand (ash date -5) 15))
440 (day (logand date 31)))
441 (if (or (> month 12) (< month 1))
443 (format "%2d-%s-%d"
445 (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
446 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
447 year))))
449 (defun archive-dostime (time)
450 "Stringify dos packed TIME record."
451 (let ((hour (logand (ash time -11) 31))
452 (minute (logand (ash time -5) 63))
453 (second (* 2 (logand time 31)))) ; 2 seconds resolution
454 (format "%02d:%02d:%02d" hour minute second)))
456 ;;(defun archive-unixdate (low high)
457 ;; "Stringify unix (LOW HIGH) date."
458 ;; (let ((str (current-time-string (cons high low))))
459 ;; (format "%s-%s-%s"
460 ;; (substring str 8 9)
461 ;; (substring str 4 7)
462 ;; (substring str 20 24))))
464 ;;(defun archive-unixtime (low high)
465 ;; "Stringify unix (LOW HIGH) time."
466 ;; (let ((str (current-time-string (cons high low))))
467 ;; (substring str 11 19)))
469 (defun archive-get-lineno ()
470 (if (>= (point) archive-file-list-start)
471 (count-lines archive-file-list-start
472 (save-excursion (beginning-of-line) (point)))
475 (defun archive-get-descr (&optional noerror)
476 "Return the descriptor vector for file at point.
477 Does not signal an error if optional second argument NOERROR is non-nil."
478 (let ((no (archive-get-lineno)))
479 (if (and (>= (point) archive-file-list-start)
480 (< no (length archive-files)))
481 (let ((item (aref archive-files no)))
482 (if (vectorp item)
483 item
484 (if (not noerror)
485 (error "Entry is not a regular member of the archive"))))
486 (if (not noerror)
487 (error "Line does not describe a member of the archive")))))
488 ;; -------------------------------------------------------------------------
489 ;; Section: the mode definition
491 ;;;###autoload
492 (defun archive-mode (&optional force)
493 "Major mode for viewing an archive file in a dired-like way.
494 You can move around using the usual cursor motion commands.
495 Letters no longer insert themselves.
496 Type `e' to pull a file out of the archive and into its own buffer;
497 or click mouse-2 on the file's line in the archive mode buffer.
499 If you edit a sub-file of this archive (as with the `e' command) and
500 save it, the contents of that buffer will be saved back into the
501 archive.
503 \\{archive-mode-map}"
504 ;; This is not interactive because you shouldn't be turning this
505 ;; mode on and off. You can corrupt things that way.
506 (if (zerop (buffer-size))
507 ;; At present we cannot create archives from scratch
508 (funcall default-major-mode)
509 (if (and (not force) archive-files) nil
510 (let* ((type (archive-find-type))
511 (typename (capitalize (symbol-name type))))
512 (kill-all-local-variables)
513 (make-local-variable 'archive-subtype)
514 (setq archive-subtype type)
516 ;; Buffer contains treated image of file before the file contents
517 (make-local-variable 'revert-buffer-function)
518 (setq revert-buffer-function 'archive-mode-revert)
519 (auto-save-mode 0)
521 ;; Remote archives are not written by a hook.
522 (if archive-remote nil
523 (make-local-variable 'write-contents-hooks)
524 (add-hook 'write-contents-hooks 'archive-write-file))
526 (make-local-variable 'require-final-newline)
527 (setq require-final-newline nil)
528 (make-local-variable 'local-enable-local-variables)
529 (setq local-enable-local-variables nil)
531 ;; Prevent loss of data when saving the file.
532 (make-local-variable 'file-precious-flag)
533 (setq file-precious-flag t)
535 (make-local-variable 'archive-read-only)
536 ;; Archives which are inside other archives and whose
537 ;; names are invalid for this OS, can't be written.
538 (setq archive-read-only
539 (or (not (file-writable-p (buffer-file-name)))
540 (and archive-subfile-mode
541 (string-match file-name-invalid-regexp
542 (aref archive-subfile-mode 0)))))
544 ;; Should we use a local copy when accessing from outside Emacs?
545 (make-local-variable 'archive-local-name)
547 ;; An archive can contain another archive whose name is invalid
548 ;; on local filesystem. Treat such archives as remote.
549 (or archive-remote
550 (setq archive-remote
551 (or (string-match archive-remote-regexp (buffer-file-name))
552 (string-match file-name-invalid-regexp
553 (buffer-file-name)))))
555 (setq major-mode 'archive-mode)
556 (setq mode-name (concat typename "-Archive"))
557 ;; Run archive-foo-mode-hook and archive-mode-hook
558 (run-hooks (archive-name "mode-hook") 'archive-mode-hook)
559 (use-local-map archive-mode-map))
561 (make-local-variable 'archive-proper-file-start)
562 (make-local-variable 'archive-file-list-start)
563 (make-local-variable 'archive-file-list-end)
564 (make-local-variable 'archive-file-name-indent)
565 (archive-summarize nil)
566 (setq buffer-read-only t))))
568 ;; Archive mode is suitable only for specially formatted data.
569 (put 'archive-mode 'mode-class 'special)
570 ;; -------------------------------------------------------------------------
571 ;; Section: Key maps
573 (if archive-mode-map nil
574 (setq archive-mode-map (make-keymap))
575 (suppress-keymap archive-mode-map)
576 (define-key archive-mode-map " " 'archive-next-line)
577 (define-key archive-mode-map "a" 'archive-alternate-display)
578 ;;(define-key archive-mode-map "c" 'archive-copy)
579 (define-key archive-mode-map "d" 'archive-flag-deleted)
580 (define-key archive-mode-map "\C-d" 'archive-flag-deleted)
581 (define-key archive-mode-map "e" 'archive-extract)
582 (define-key archive-mode-map "f" 'archive-extract)
583 (define-key archive-mode-map "\C-m" 'archive-extract)
584 (define-key archive-mode-map "g" 'revert-buffer)
585 (define-key archive-mode-map "h" 'describe-mode)
586 (define-key archive-mode-map "m" 'archive-mark)
587 (define-key archive-mode-map "n" 'archive-next-line)
588 (define-key archive-mode-map "\C-n" 'archive-next-line)
589 (define-key archive-mode-map [down] 'archive-next-line)
590 (define-key archive-mode-map "o" 'archive-extract-other-window)
591 (define-key archive-mode-map "p" 'archive-previous-line)
592 (define-key archive-mode-map "q" 'quit-window)
593 (define-key archive-mode-map "\C-p" 'archive-previous-line)
594 (define-key archive-mode-map [up] 'archive-previous-line)
595 (define-key archive-mode-map "r" 'archive-rename-entry)
596 (define-key archive-mode-map "u" 'archive-unflag)
597 (define-key archive-mode-map "\M-\C-?" 'archive-unmark-all-files)
598 (define-key archive-mode-map "v" 'archive-view)
599 (define-key archive-mode-map "x" 'archive-expunge)
600 (define-key archive-mode-map "\177" 'archive-unflag-backwards)
601 (define-key archive-mode-map "E" 'archive-extract-other-window)
602 (define-key archive-mode-map "M" 'archive-chmod-entry)
603 (define-key archive-mode-map "G" 'archive-chgrp-entry)
604 (define-key archive-mode-map "O" 'archive-chown-entry)
606 (if archive-lemacs
607 (progn
608 ;; Not a nice "solution" but it'll have to do
609 (define-key archive-mode-map "\C-xu" 'archive-undo)
610 (define-key archive-mode-map "\C-_" 'archive-undo))
611 (substitute-key-definition 'undo 'archive-undo
612 archive-mode-map global-map))
614 (define-key archive-mode-map
615 (if archive-lemacs 'button2 [mouse-2]) 'archive-mouse-extract)
617 (if archive-lemacs
618 () ; out of luck
620 (define-key archive-mode-map [menu-bar immediate]
621 (cons "Immediate" (make-sparse-keymap "Immediate")))
622 (define-key archive-mode-map [menu-bar immediate alternate]
623 '(menu-item "Alternate Display" archive-alternate-display
624 :enable (boundp (archive-name "alternate-display"))
625 :help "Toggle alternate file info display"))
626 (define-key archive-mode-map [menu-bar immediate view]
627 '(menu-item "View This File" archive-view
628 :help "Display file at cursor in View Mode"))
629 (define-key archive-mode-map [menu-bar immediate display]
630 '(menu-item "Display in Other Window" archive-display-other-window
631 :help "Display file at cursor in another window"))
632 (define-key archive-mode-map [menu-bar immediate find-file-other-window]
633 '(menu-item "Find in Other Window" archive-extract-other-window
634 :help "Edit file at cursor in another window"))
635 (define-key archive-mode-map [menu-bar immediate find-file]
636 '(menu-item "Find This File" archive-extract
637 :help "Extract file at cursor and edit it"))
639 (define-key archive-mode-map [menu-bar mark]
640 (cons "Mark" (make-sparse-keymap "Mark")))
641 (define-key archive-mode-map [menu-bar mark unmark-all]
642 '(menu-item "Unmark All" archive-unmark-all-files
643 :help "Unmark all marked files"))
644 (define-key archive-mode-map [menu-bar mark deletion]
645 '(menu-item "Flag" archive-flag-deleted
646 :help "Flag file at cursor for deletion"))
647 (define-key archive-mode-map [menu-bar mark unmark]
648 '(menu-item "Unflag" archive-unflag
649 :help "Unmark file at cursor"))
650 (define-key archive-mode-map [menu-bar mark mark]
651 '(menu-item "Mark" archive-mark
652 :help "Mark file at cursor"))
654 (define-key archive-mode-map [menu-bar operate]
655 (cons "Operate" (make-sparse-keymap "Operate")))
656 (define-key archive-mode-map [menu-bar operate chown]
657 '(menu-item "Change Owner..." archive-chown-entry
658 :enable (fboundp (archive-name "chown-entry"))
659 :help "Change owner of marked files"))
660 (define-key archive-mode-map [menu-bar operate chgrp]
661 '(menu-item "Change Group..." archive-chgrp-entry
662 :enable (fboundp (archive-name "chgrp-entry"))
663 :help "Change group ownership of marked files"))
664 (define-key archive-mode-map [menu-bar operate chmod]
665 '(menu-item "Change Mode..." archive-chmod-entry
666 :enable (fboundp (archive-name "chmod-entry"))
667 :help "Change mode (permissions) of marked files"))
668 (define-key archive-mode-map [menu-bar operate rename]
669 '(menu-item "Rename to..." archive-rename-entry
670 :enable (fboundp (archive-name "rename-entry"))
671 :help "Rename marked files"))
672 ;;(define-key archive-mode-map [menu-bar operate copy]
673 ;; '(menu-item "Copy to..." archive-copy))
674 (define-key archive-mode-map [menu-bar operate expunge]
675 '(menu-item "Expunge Marked Files" archive-expunge
676 :help "Delete all flagged files from archive"))
679 (let* ((item1 '(archive-subfile-mode " Archive"))
680 (items (list item1)))
681 (or (member item1 minor-mode-alist)
682 (setq minor-mode-alist (append items minor-mode-alist))))
683 ;; -------------------------------------------------------------------------
684 (defun archive-find-type ()
685 (widen)
686 (goto-char (point-min))
687 ;; The funny [] here make it unlikely that the .elc file will be treated
688 ;; as an archive by other software.
689 (let (case-fold-search)
690 (cond ((looking-at "[P]K\003\004") 'zip)
691 ((looking-at "..-l[hz][0-9ds]-") 'lzh)
692 ((looking-at "....................[\334]\247\304\375") 'zoo)
693 ((and (looking-at "\C-z") ; signature too simple, IMHO
694 (string-match "\\.[aA][rR][cC]$"
695 (or buffer-file-name (buffer-name))))
696 'arc)
697 (t (error "Buffer format not recognized")))))
698 ;; -------------------------------------------------------------------------
699 (defun archive-summarize (&optional shut-up)
700 "Parse the contents of the archive file in the current buffer.
701 Place a dired-like listing on the front;
702 then narrow to it, so that only that listing
703 is visible (and the real data of the buffer is hidden).
704 Optional argument SHUT-UP, if non-nil, means don't print messages
705 when parsing the archive."
706 (widen)
707 (set-buffer-multibyte nil)
708 (let (buffer-read-only)
709 (or shut-up
710 (message "Parsing archive file..."))
711 (buffer-disable-undo (current-buffer))
712 (setq archive-files (funcall (archive-name "summarize")))
713 (or shut-up
714 (message "Parsing archive file...done."))
715 (setq archive-proper-file-start (point-marker))
716 (narrow-to-region (point-min) (point))
717 (set-buffer-modified-p nil)
718 (buffer-enable-undo))
719 (goto-char archive-file-list-start)
720 (archive-next-line 0))
722 (defun archive-resummarize ()
723 "Recreate the contents listing of an archive."
724 (let ((modified (buffer-modified-p))
725 (no (archive-get-lineno))
726 buffer-read-only)
727 (widen)
728 (delete-region (point-min) archive-proper-file-start)
729 (archive-summarize t)
730 (set-buffer-modified-p modified)
731 (goto-char archive-file-list-start)
732 (archive-next-line no)))
734 (defun archive-summarize-files (files)
735 "Insert a description of a list of files annotated with proper mouse face."
736 (setq archive-file-list-start (point-marker))
737 (setq archive-file-name-indent (if files (aref (car files) 1) 0))
738 ;; We don't want to do an insert for each element since that takes too
739 ;; long when the archive -- which has to be moved in memory -- is large.
740 (insert
741 (apply
742 (function concat)
743 (mapcar
744 (function
745 (lambda (fil)
746 ;; Using `concat' here copies the text also, so we can add
747 ;; properties without problems.
748 (let ((text (concat (aref fil 0) "\n")))
749 (if archive-lemacs
750 () ; out of luck
751 (add-text-properties
752 (aref fil 1) (aref fil 2)
753 '(mouse-face highlight
754 help-echo "mouse-2: extract this file into a buffer")
755 text))
756 text)))
757 files)))
758 (setq archive-file-list-end (point-marker)))
760 (defun archive-alternate-display ()
761 "Toggle alternative display.
762 To avoid very long lines some archive mode don't show all information.
763 This function changes the set of information shown for each files."
764 (interactive)
765 (setq archive-alternate-display (not archive-alternate-display))
766 (archive-resummarize))
767 ;; -------------------------------------------------------------------------
768 ;; Section: Local archive copy handling
770 (defun archive-unique-fname (fname dir)
771 "Make sure a file FNAME can be created uniquely in directory DIR.
773 If FNAME can be uniquely created in DIR, it is returned unaltered.
774 If FNAME is something our underlying filesystem can't grok, or if another
775 file by that name already exists in DIR, a unique new name is generated
776 using `make-temp-file', and the generated name is returned."
777 (let ((fullname (expand-file-name fname dir))
778 (alien (string-match file-name-invalid-regexp fname)))
779 (if (or alien (file-exists-p fullname))
780 (make-temp-file
781 (expand-file-name
782 (if (and (fboundp 'msdos-long-file-names)
783 (not (msdos-long-file-names)))
784 "am"
785 "arc-mode.")
786 dir))
787 fullname)))
789 (defun archive-maybe-copy (archive)
790 (let ((coding-system-for-write 'no-conversion))
791 (if archive-remote
792 (let ((start (point-max))
793 ;; Sometimes ARCHIVE is invalid while its actual name, as
794 ;; recorded in its parent archive, is not. For example, an
795 ;; archive bar.zip inside another archive foo.zip gets a name
796 ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
797 ;; So use the actual name if available.
798 (archive-name
799 (or (and archive-subfile-mode (aref archive-subfile-mode 0))
800 archive)))
801 (make-directory archive-tmpdir t)
802 (setq archive-local-name
803 (archive-unique-fname archive-name archive-tmpdir))
804 (save-restriction
805 (widen)
806 (write-region start (point-max) archive-local-name nil 'nomessage))
807 archive-local-name)
808 (if (buffer-modified-p) (save-buffer))
809 archive)))
811 (defun archive-maybe-update (unchanged)
812 (if archive-remote
813 (let ((name archive-local-name)
814 (modified (buffer-modified-p))
815 (coding-system-for-read 'no-conversion)
816 (lno (archive-get-lineno))
817 buffer-read-only)
818 (if unchanged nil
819 (setq archive-files nil)
820 (erase-buffer)
821 (insert-file-contents name)
822 (archive-mode t)
823 (goto-char archive-file-list-start)
824 (archive-next-line lno))
825 (archive-delete-local name)
826 (if (not unchanged)
827 (message
828 "Buffer `%s' must be saved for changes to take effect"
829 (buffer-name (current-buffer))))
830 (set-buffer-modified-p (or modified (not unchanged))))))
832 (defun archive-delete-local (name)
833 "Delete file NAME and its parents up to and including `archive-tmpdir'."
834 (let ((again t)
835 (top (directory-file-name (file-name-as-directory archive-tmpdir))))
836 (condition-case nil
837 (delete-file name)
838 (error nil))
839 (while again
840 (setq name (directory-file-name (file-name-directory name)))
841 (condition-case nil
842 (delete-directory name)
843 (error nil))
844 (if (string= name top) (setq again nil)))))
845 ;; -------------------------------------------------------------------------
846 ;; Section: Member extraction
848 (defun archive-file-name-handler (op &rest args)
849 (or (eq op 'file-exists-p)
850 (let ((file-name-handler-alist nil))
851 (apply op args))))
853 (defun archive-set-buffer-as-visiting-file (filename)
854 "Set the current buffer as if it were visiting FILENAME."
855 (save-excursion
856 (goto-char (point-min))
857 (let ((coding
858 (or coding-system-for-read
859 (and set-auto-coding-function
860 (save-excursion
861 (funcall set-auto-coding-function
862 filename (- (point-max) (point-min)))))
863 ;; dos-w32.el defines find-operation-coding-system for
864 ;; DOS/Windows systems which preserves the coding-system
865 ;; of existing files. We want it to act here as if the
866 ;; extracted file existed.
867 (let ((file-name-handler-alist
868 '(("" . archive-file-name-handler))))
869 (car (find-operation-coding-system 'insert-file-contents
870 filename t))))))
871 (if (and (not coding-system-for-read)
872 (not enable-multibyte-characters))
873 (setq coding
874 (coding-system-change-text-conversion coding 'raw-text)))
875 (if (and coding
876 (not (eq coding 'no-conversion)))
877 (decode-coding-region (point-min) (point-max) coding)
878 (setq last-coding-system-used coding))
879 (set-buffer-modified-p nil)
880 (kill-local-variable 'buffer-file-coding-system)
881 (after-insert-file-set-buffer-file-coding-system (- (point-max)
882 (point-min))))))
884 (defun archive-mouse-extract (event)
885 "Extract a file whose name you click on."
886 (interactive "e")
887 (mouse-set-point event)
888 (switch-to-buffer
889 (save-excursion
890 (archive-extract)
891 (current-buffer))))
893 (defun archive-extract (&optional other-window-p)
894 "In archive mode, extract this entry of the archive into its own buffer."
895 (interactive)
896 (let* ((view-p (eq other-window-p 'view))
897 (descr (archive-get-descr))
898 (ename (aref descr 0))
899 (iname (aref descr 1))
900 (archive-buffer (current-buffer))
901 (arcdir default-directory)
902 (archive (buffer-file-name))
903 (arcname (file-name-nondirectory archive))
904 (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
905 (extractor (archive-name "extract"))
906 ;; Members with file names which aren't valid for the
907 ;; underlying filesystem, are treated as read-only.
908 (read-only-p (or archive-read-only
909 view-p
910 (string-match file-name-invalid-regexp ename)))
911 (buffer (get-buffer bufname))
912 (just-created nil))
913 (if buffer
915 (setq archive (archive-maybe-copy archive))
916 (setq buffer (get-buffer-create bufname))
917 (setq just-created t)
918 (save-excursion
919 (set-buffer buffer)
920 (setq buffer-file-name
921 (expand-file-name (concat arcname ":" iname)))
922 (setq buffer-file-truename
923 (abbreviate-file-name buffer-file-name))
924 ;; Set the default-directory to the dir of the superior buffer.
925 (setq default-directory arcdir)
926 (make-local-variable 'archive-superior-buffer)
927 (setq archive-superior-buffer archive-buffer)
928 (make-local-variable 'local-write-file-hooks)
929 (add-hook 'local-write-file-hooks 'archive-write-file-member)
930 (setq archive-subfile-mode descr)
931 (if (and
932 (null
933 (let (;; We may have to encode file name arguement for
934 ;; external programs.
935 (coding-system-for-write
936 (and enable-multibyte-characters
937 file-name-coding-system))
938 ;; We read an archive member by no-conversion at
939 ;; first, then decode appropriately by calling
940 ;; archive-set-buffer-as-visiting-file later.
941 (coding-system-for-read 'no-conversion))
942 (condition-case err
943 (if (fboundp extractor)
944 (funcall extractor archive ename)
945 (archive-*-extract archive ename
946 (symbol-value extractor)))
947 (error
948 (ding (message "%s" (error-message-string err)))
949 nil))))
950 just-created)
951 (progn
952 (set-buffer-modified-p nil)
953 (kill-buffer buffer))
954 (archive-set-buffer-as-visiting-file ename)
955 (goto-char (point-min))
956 (rename-buffer bufname)
957 (setq buffer-read-only read-only-p)
958 (setq buffer-undo-list nil)
959 (set-buffer-modified-p nil)
960 (setq buffer-saved-size (buffer-size))
961 (normal-mode)
962 ;; Just in case an archive occurs inside another archive.
963 (if (eq major-mode 'archive-mode)
964 (progn
965 (setq archive-remote t)
966 (if read-only-p (setq archive-read-only t))
967 ;; We will write out the archive ourselves if it is
968 ;; part of another archive.
969 (remove-hook 'write-contents-hooks 'archive-write-file t)))
970 (run-hooks 'archive-extract-hooks)
971 (if archive-read-only
972 (message "Note: altering this archive is not implemented."))))
973 (archive-maybe-update t))
974 (or (not (buffer-name buffer))
975 (progn
976 (if view-p
977 (view-buffer buffer (and just-created 'kill-buffer))
978 (if (eq other-window-p 'display)
979 (display-buffer buffer)
980 (if other-window-p
981 (switch-to-buffer-other-window buffer)
982 (switch-to-buffer buffer))))))))
984 (defun archive-*-extract (archive name command)
985 (let* ((default-directory (file-name-as-directory archive-tmpdir))
986 (tmpfile (expand-file-name (file-name-nondirectory name)
987 default-directory))
988 exit-status success)
989 (make-directory (directory-file-name default-directory) t)
990 (setq exit-status
991 (apply 'call-process
992 (car command)
996 (append (cdr command) (list archive name))))
997 (cond ((and (numberp exit-status) (= exit-status 0))
998 (if (not (file-exists-p tmpfile))
999 (ding (message "`%s': no such file or directory" tmpfile))
1000 (insert-file-contents tmpfile)
1001 (setq success t)))
1002 ((numberp exit-status)
1003 (ding
1004 (message "`%s' exited with status %d" (car command) exit-status)))
1005 ((stringp exit-status)
1006 (ding (message "`%s' aborted: %s" (car command) exit-status)))
1008 (ding (message "`%s' failed" (car command)))))
1009 (archive-delete-local tmpfile)
1010 success))
1012 (defun archive-extract-by-stdout (archive name command)
1013 (apply 'call-process
1014 (car command)
1018 (append (cdr command) (list archive name))))
1020 (defun archive-extract-other-window ()
1021 "In archive mode, find this member in another window."
1022 (interactive)
1023 (archive-extract t))
1025 (defun archive-display-other-window ()
1026 "In archive mode, display this member in another window."
1027 (interactive)
1028 (archive-extract 'display))
1030 (defun archive-view ()
1031 "In archive mode, view the member on this line."
1032 (interactive)
1033 (archive-extract 'view))
1035 (defun archive-add-new-member (arcbuf name)
1036 "Add current buffer to the archive in ARCBUF naming it NAME."
1037 (interactive
1038 (list (get-buffer
1039 (read-buffer "Buffer containing archive: "
1040 ;; Find first archive buffer and suggest that
1041 (let ((bufs (buffer-list)))
1042 (while (and bufs (not (eq (save-excursion
1043 (set-buffer (car bufs))
1044 major-mode)
1045 'archive-mode)))
1046 (setq bufs (cdr bufs)))
1047 (if bufs
1048 (car bufs)
1049 (error "There are no archive buffers")))
1051 (read-string "File name in archive: "
1052 (if buffer-file-name
1053 (file-name-nondirectory buffer-file-name)
1054 ""))))
1055 (save-excursion
1056 (set-buffer arcbuf)
1057 (or (eq major-mode 'archive-mode)
1058 (error "Buffer is not an archive buffer"))
1059 (if archive-read-only
1060 (error "Archive is read-only")))
1061 (if (eq arcbuf (current-buffer))
1062 (error "An archive buffer cannot be added to itself"))
1063 (if (string= name "")
1064 (error "Archive members may not be given empty names"))
1065 (let ((func (save-excursion (set-buffer arcbuf)
1066 (archive-name "add-new-member")))
1067 (membuf (current-buffer)))
1068 (if (fboundp func)
1069 (save-excursion
1070 (set-buffer arcbuf)
1071 (funcall func buffer-file-name membuf name))
1072 (error "Adding a new member is not supported for this archive type"))))
1073 ;; -------------------------------------------------------------------------
1074 ;; Section: IO stuff
1076 (defun archive-write-file-member ()
1077 (save-excursion
1078 (save-restriction
1079 (message "Updating archive...")
1080 (widen)
1081 (let ((writer (save-excursion (set-buffer archive-superior-buffer)
1082 (archive-name "write-file-member")))
1083 (archive (save-excursion (set-buffer archive-superior-buffer)
1084 (archive-maybe-copy (buffer-file-name)))))
1085 (if (fboundp writer)
1086 (funcall writer archive archive-subfile-mode)
1087 (archive-*-write-file-member archive
1088 archive-subfile-mode
1089 (symbol-value writer)))
1090 (set-buffer-modified-p nil)
1091 (message "Updating archive...done"))
1092 (set-buffer archive-superior-buffer)
1093 (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
1094 ;; Restore the value of last-coding-system-used, so that basic-save-buffer
1095 ;; won't reset the coding-system of this archive member.
1096 (if (local-variable-p 'archive-member-coding-system)
1097 (setq last-coding-system-used archive-member-coding-system))
1100 (defun archive-*-write-file-member (archive descr command)
1101 (let* ((ename (aref descr 0))
1102 (tmpfile (expand-file-name ename archive-tmpdir))
1103 (top (directory-file-name (file-name-as-directory archive-tmpdir)))
1104 (default-directory (file-name-as-directory top)))
1105 (unwind-protect
1106 (progn
1107 (make-directory (file-name-directory tmpfile) t)
1108 ;; If the member is itself an archive, write it without
1109 ;; the dired-like listing we created.
1110 (if (eq major-mode 'archive-mode)
1111 (archive-write-file tmpfile)
1112 (write-region (point-min) (point-max) tmpfile nil 'nomessage))
1113 ;; basic-save-buffer needs last-coding-system-used to have
1114 ;; the value used to write the file, so save it before any
1115 ;; further processing clobbers it (we restore it in
1116 ;; archive-write-file-member, above).
1117 (setq archive-member-coding-system last-coding-system-used)
1118 (if (aref descr 3)
1119 ;; Set the file modes, but make sure we can read it.
1120 (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
1121 (if enable-multibyte-characters
1122 (setq ename
1123 (encode-coding-string ename file-name-coding-system)))
1124 (let ((exitcode (apply 'call-process
1125 (car command)
1129 (append (cdr command) (list archive ename)))))
1130 (if (equal exitcode 0)
1132 (error "Updating was unsuccessful (%S)" exitcode))))
1133 (archive-delete-local tmpfile))))
1135 (defun archive-write-file (&optional file)
1136 (save-excursion
1137 (let ((coding-system-for-write 'no-conversion))
1138 (write-region archive-proper-file-start (point-max)
1139 (or file buffer-file-name) nil t)
1140 (set-buffer-modified-p nil))
1142 ;; -------------------------------------------------------------------------
1143 ;; Section: Marking and unmarking.
1145 (defun archive-flag-deleted (p &optional type)
1146 "In archive mode, mark this member to be deleted from the archive.
1147 With a prefix argument, mark that many files."
1148 (interactive "p")
1149 (or type (setq type ?D))
1150 (beginning-of-line)
1151 (let ((sign (if (>= p 0) +1 -1))
1152 (modified (buffer-modified-p))
1153 buffer-read-only)
1154 (while (not (zerop p))
1155 (if (archive-get-descr t)
1156 (progn
1157 (delete-char 1)
1158 (insert type)))
1159 (forward-line sign)
1160 (setq p (- p sign)))
1161 (set-buffer-modified-p modified))
1162 (archive-next-line 0))
1164 (defun archive-unflag (p)
1165 "In archive mode, un-mark this member if it is marked to be deleted.
1166 With a prefix argument, un-mark that many files forward."
1167 (interactive "p")
1168 (archive-flag-deleted p ? ))
1170 (defun archive-unflag-backwards (p)
1171 "In archive mode, un-mark this member if it is marked to be deleted.
1172 With a prefix argument, un-mark that many members backward."
1173 (interactive "p")
1174 (archive-flag-deleted (- p) ? ))
1176 (defun archive-unmark-all-files ()
1177 "Remove all marks."
1178 (interactive)
1179 (let ((modified (buffer-modified-p))
1180 buffer-read-only)
1181 (save-excursion
1182 (goto-char archive-file-list-start)
1183 (while (< (point) archive-file-list-end)
1184 (or (= (following-char) ? )
1185 (progn (delete-char 1) (insert ? )))
1186 (forward-line 1)))
1187 (set-buffer-modified-p modified)))
1189 (defun archive-mark (p)
1190 "In archive mode, mark this member for group operations.
1191 With a prefix argument, mark that many members.
1192 Use \\[archive-unmark-all-files] to remove all marks."
1193 (interactive "p")
1194 (archive-flag-deleted p ?*))
1196 (defun archive-get-marked (mark &optional default)
1197 (let (files)
1198 (save-excursion
1199 (goto-char archive-file-list-start)
1200 (while (< (point) archive-file-list-end)
1201 (if (= (following-char) mark)
1202 (setq files (cons (archive-get-descr) files)))
1203 (forward-line 1)))
1204 (or (nreverse files)
1205 (and default
1206 (list (archive-get-descr))))))
1207 ;; -------------------------------------------------------------------------
1208 ;; Section: Operate
1210 (defun archive-next-line (p)
1211 (interactive "p")
1212 (forward-line p)
1213 (or (eobp)
1214 (forward-char archive-file-name-indent)))
1216 (defun archive-previous-line (p)
1217 (interactive "p")
1218 (archive-next-line (- p)))
1220 (defun archive-chmod-entry (new-mode)
1221 "Change the protection bits associated with all marked or this member.
1222 The new protection bits can either be specified as an octal number or
1223 as a relative change like \"g+rw\" as for chmod(2)"
1224 (interactive "sNew mode (octal or relative): ")
1225 (if archive-read-only (error "Archive is read-only"))
1226 (let ((func (archive-name "chmod-entry")))
1227 (if (fboundp func)
1228 (progn
1229 (funcall func new-mode (archive-get-marked ?* t))
1230 (archive-resummarize))
1231 (error "Setting mode bits is not supported for this archive type"))))
1233 (defun archive-chown-entry (new-uid)
1234 "Change the owner of all marked or this member."
1235 (interactive "nNew uid: ")
1236 (if archive-read-only (error "Archive is read-only"))
1237 (let ((func (archive-name "chown-entry")))
1238 (if (fboundp func)
1239 (progn
1240 (funcall func new-uid (archive-get-marked ?* t))
1241 (archive-resummarize))
1242 (error "Setting owner is not supported for this archive type"))))
1244 (defun archive-chgrp-entry (new-gid)
1245 "Change the group of all marked or this member."
1246 (interactive "nNew gid: ")
1247 (if archive-read-only (error "Archive is read-only"))
1248 (let ((func (archive-name "chgrp-entry")))
1249 (if (fboundp func)
1250 (progn
1251 (funcall func new-gid (archive-get-marked ?* t))
1252 (archive-resummarize))
1253 (error "Setting group is not supported for this archive type"))))
1255 (defun archive-expunge ()
1256 "Do the flagged deletions."
1257 (interactive)
1258 (let (files)
1259 (save-excursion
1260 (goto-char archive-file-list-start)
1261 (while (< (point) archive-file-list-end)
1262 (if (= (following-char) ?D)
1263 (setq files (cons (aref (archive-get-descr) 0) files)))
1264 (forward-line 1)))
1265 (setq files (nreverse files))
1266 (and files
1267 (or (not archive-read-only)
1268 (error "Archive is read-only"))
1269 (or (yes-or-no-p (format "Really delete %d member%s? "
1270 (length files)
1271 (if (null (cdr files)) "" "s")))
1272 (error "Operation aborted"))
1273 (let ((archive (archive-maybe-copy (buffer-file-name)))
1274 (expunger (archive-name "expunge")))
1275 (if (fboundp expunger)
1276 (funcall expunger archive files)
1277 (archive-*-expunge archive files (symbol-value expunger)))
1278 (archive-maybe-update nil)
1279 (if archive-remote
1280 (archive-resummarize)
1281 (revert-buffer))))))
1283 (defun archive-*-expunge (archive files command)
1284 (apply 'call-process
1285 (car command)
1289 (append (cdr command) (cons archive files))))
1291 (defun archive-rename-entry (newname)
1292 "Change the name associated with this entry in the tar file."
1293 (interactive "sNew name: ")
1294 (if archive-read-only (error "Archive is read-only"))
1295 (if (string= newname "")
1296 (error "Archive members may not be given empty names"))
1297 (let ((func (archive-name "rename-entry"))
1298 (descr (archive-get-descr)))
1299 (if (fboundp func)
1300 (progn
1301 (funcall func (buffer-file-name)
1302 (if enable-multibyte-characters
1303 (encode-coding-string newname file-name-coding-system)
1304 newname)
1305 descr)
1306 (archive-resummarize))
1307 (error "Renaming is not supported for this archive type"))))
1309 ;; Revert the buffer and recompute the dired-like listing.
1310 (defun archive-mode-revert (&optional no-auto-save no-confirm)
1311 (let ((no (archive-get-lineno)))
1312 (setq archive-files nil)
1313 (let ((revert-buffer-function nil)
1314 (coding-system-for-read 'no-conversion))
1315 (set-buffer-multibyte nil)
1316 (revert-buffer t t))
1317 (archive-mode)
1318 (goto-char archive-file-list-start)
1319 (archive-next-line no)))
1321 (defun archive-undo ()
1322 "Undo in an archive buffer.
1323 This doesn't recover lost files, it just undoes changes in the buffer itself."
1324 (interactive)
1325 (let (buffer-read-only)
1326 (undo)))
1327 ;; -------------------------------------------------------------------------
1328 ;; Section: Arc Archives
1330 (defun archive-arc-summarize ()
1331 (let ((p 1)
1332 (totalsize 0)
1333 (maxlen 8)
1334 files
1335 visual)
1336 (while (and (< (+ p 29) (point-max))
1337 (= (char-after p) ?\C-z)
1338 (> (char-after (1+ p)) 0))
1339 (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
1340 (fnlen (or (string-match "\0" namefld) 13))
1341 (efnname (substring namefld 0 fnlen))
1342 (csize (archive-l-e (+ p 15) 4))
1343 (moddate (archive-l-e (+ p 19) 2))
1344 (modtime (archive-l-e (+ p 21) 2))
1345 (ucsize (archive-l-e (+ p 25) 4))
1346 (fiddle (string= efnname (upcase efnname)))
1347 (ifnname (if fiddle (downcase efnname) efnname))
1348 (text (format " %8d %-11s %-8s %s"
1349 ucsize
1350 (archive-dosdate moddate)
1351 (archive-dostime modtime)
1352 ifnname)))
1353 (setq maxlen (max maxlen fnlen)
1354 totalsize (+ totalsize ucsize)
1355 visual (cons (vector text
1356 (- (length text) (length ifnname))
1357 (length text))
1358 visual)
1359 files (cons (vector efnname ifnname fiddle nil (1- p))
1360 files)
1361 p (+ p 29 csize))))
1362 (goto-char (point-min))
1363 (let ((dash (concat "- -------- ----------- -------- "
1364 (make-string maxlen ?-)
1365 "\n")))
1366 (insert "M Length Date Time File\n"
1367 dash)
1368 (archive-summarize-files (nreverse visual))
1369 (insert dash
1370 (format " %8d %d file%s"
1371 totalsize
1372 (length files)
1373 (if (= 1 (length files)) "" "s"))
1374 "\n"))
1375 (apply 'vector (nreverse files))))
1377 (defun archive-arc-rename-entry (archive newname descr)
1378 (if (string-match "[:\\\\/]" newname)
1379 (error "File names in arc files may not contain a path"))
1380 (if (> (length newname) 12)
1381 (error "File names in arc files are limited to 12 characters"))
1382 (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
1383 (length newname))))
1384 buffer-read-only)
1385 (save-restriction
1386 (save-excursion
1387 (widen)
1388 (set-buffer-multibyte nil)
1389 (goto-char (+ archive-proper-file-start (aref descr 4) 2))
1390 (delete-char 13)
1391 (insert name)))))
1392 ;; -------------------------------------------------------------------------
1393 ;; Section: Lzh Archives
1395 (defun archive-lzh-summarize ()
1396 (let ((p 1)
1397 (totalsize 0)
1398 (maxlen 8)
1399 files
1400 visual)
1401 (while (progn (goto-char p)
1402 (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
1403 (let* ((hsize (char-after p))
1404 (csize (archive-l-e (+ p 7) 4))
1405 (ucsize (archive-l-e (+ p 11) 4))
1406 (modtime (archive-l-e (+ p 15) 2))
1407 (moddate (archive-l-e (+ p 17) 2))
1408 (hdrlvl (char-after (+ p 20)))
1409 (fnlen (char-after (+ p 21)))
1410 (efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen))))
1411 (if file-name-coding-system
1412 (decode-coding-string str file-name-coding-system)
1413 (string-as-multibyte str))))
1414 (fiddle (string= efnname (upcase efnname)))
1415 (ifnname (if fiddle (downcase efnname) efnname))
1416 (width (string-width ifnname))
1417 (p2 (+ p 22 fnlen))
1418 (creator (if (>= (- hsize fnlen) 24) (char-after (+ p2 2)) 0))
1419 mode modestr uid gid text path prname
1421 (if (= hdrlvl 0)
1422 (setq mode (if (= creator ?U) (archive-l-e (+ p2 8) 2) ?\666)
1423 uid (if (= creator ?U) (archive-l-e (+ p2 10) 2))
1424 gid (if (= creator ?U) (archive-l-e (+ p2 12) 2)))
1425 (if (= creator ?U)
1426 (let* ((p3 (+ p2 3))
1427 (hsize (archive-l-e p3 2))
1428 (etype (char-after (+ p3 2))))
1429 (while (not (= hsize 0))
1430 (cond
1431 ((= etype 2) (let ((i (+ p3 3)))
1432 (while (< i (+ p3 hsize))
1433 (setq path (concat path
1434 (if (= (char-after i)
1435 255)
1437 (char-to-string
1438 (char-after i)))))
1439 (setq i (1+ i)))))
1440 ((= etype 80) (setq mode (archive-l-e (+ p3 3) 2)))
1441 ((= etype 81) (progn (setq uid (archive-l-e (+ p3 3) 2))
1442 (setq gid (archive-l-e (+ p3 5) 2))))
1444 (setq p3 (+ p3 hsize))
1445 (setq hsize (archive-l-e p3 2))
1446 (setq etype (char-after (+ p3 2)))))))
1447 (setq prname (if path (concat path ifnname) ifnname))
1448 (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
1449 (setq text (if archive-alternate-display
1450 (format " %8d %5S %5S %s"
1451 ucsize
1452 (or uid "?")
1453 (or gid "?")
1454 ifnname)
1455 (format " %10s %8d %-11s %-8s %s"
1456 modestr
1457 ucsize
1458 (archive-dosdate moddate)
1459 (archive-dostime modtime)
1460 ifnname)))
1461 (setq maxlen (max maxlen width)
1462 totalsize (+ totalsize ucsize)
1463 visual (cons (vector text
1464 (- (length text) (length ifnname))
1465 (length text))
1466 visual)
1467 files (cons (vector prname ifnname fiddle mode (1- p))
1468 files)
1469 p (+ p hsize 2 csize))))
1470 (goto-char (point-min))
1471 (set-buffer-multibyte default-enable-multibyte-characters)
1472 (let ((dash (concat (if archive-alternate-display
1473 "- -------- ----- ----- "
1474 "- ---------- -------- ----------- -------- ")
1475 (make-string maxlen ?-)
1476 "\n"))
1477 (header (if archive-alternate-display
1478 "M Length Uid Gid File\n"
1479 "M Filemode Length Date Time File\n"))
1480 (sumline (if archive-alternate-display
1481 " %8d %d file%s"
1482 " %8d %d file%s")))
1483 (insert header dash)
1484 (archive-summarize-files (nreverse visual))
1485 (insert dash
1486 (format sumline
1487 totalsize
1488 (length files)
1489 (if (= 1 (length files)) "" "s"))
1490 "\n"))
1491 (apply 'vector (nreverse files))))
1493 (defconst archive-lzh-alternate-display t)
1495 (defun archive-lzh-extract (archive name)
1496 (archive-extract-by-stdout archive name archive-lzh-extract))
1498 (defun archive-lzh-resum (p count)
1499 (let ((sum 0))
1500 (while (> count 0)
1501 (setq count (1- count)
1502 sum (+ sum (char-after p))
1503 p (1+ p)))
1504 (logand sum 255)))
1506 (defun archive-lzh-rename-entry (archive newname descr)
1507 (save-restriction
1508 (save-excursion
1509 (widen)
1510 (set-buffer-multibyte nil)
1511 (let* ((p (+ archive-proper-file-start (aref descr 4)))
1512 (oldhsize (char-after p))
1513 (oldfnlen (char-after (+ p 21)))
1514 (newfnlen (length newname))
1515 (newhsize (+ oldhsize newfnlen (- oldfnlen)))
1516 buffer-read-only)
1517 (if (> newhsize 255)
1518 (error "The file name is too long"))
1519 (goto-char (+ p 21))
1520 (delete-char (1+ oldfnlen))
1521 (insert newfnlen newname)
1522 (goto-char p)
1523 (delete-char 2)
1524 (insert newhsize (archive-lzh-resum p newhsize))))))
1526 (defun archive-lzh-ogm (newval files errtxt ofs)
1527 (save-restriction
1528 (save-excursion
1529 (widen)
1530 (set-buffer-multibyte nil)
1531 (while files
1532 (let* ((fil (car files))
1533 (p (+ archive-proper-file-start (aref fil 4)))
1534 (hsize (char-after p))
1535 (fnlen (char-after (+ p 21)))
1536 (p2 (+ p 22 fnlen))
1537 (creator (if (>= (- hsize fnlen) 24) (char-after (+ p2 2)) 0))
1538 buffer-read-only)
1539 (if (= creator ?U)
1540 (progn
1541 (or (numberp newval)
1542 (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
1543 (goto-char (+ p2 ofs))
1544 (delete-char 2)
1545 (insert (logand newval 255) (lsh newval -8))
1546 (goto-char (1+ p))
1547 (delete-char 1)
1548 (insert (archive-lzh-resum (1+ p) hsize)))
1549 (message "Member %s does not have %s field"
1550 (aref fil 1) errtxt)))
1551 (setq files (cdr files))))))
1553 (defun archive-lzh-chown-entry (newuid files)
1554 (archive-lzh-ogm newuid files "an uid" 10))
1556 (defun archive-lzh-chgrp-entry (newgid files)
1557 (archive-lzh-ogm newgid files "a gid" 12))
1559 (defun archive-lzh-chmod-entry (newmode files)
1560 (archive-lzh-ogm
1561 ;; This should work even though newmode will be dynamically accessed.
1562 (function (lambda (old) (archive-calc-mode old newmode t)))
1563 files "a unix-style mode" 8))
1564 ;; -------------------------------------------------------------------------
1565 ;; Section: Zip Archives
1567 (defun archive-zip-summarize ()
1568 (goto-char (- (point-max) (- 22 18)))
1569 (search-backward-regexp "[P]K\005\006")
1570 (let ((p (1+ (archive-l-e (+ (point) 16) 4)))
1571 (maxlen 8)
1572 (totalsize 0)
1573 files
1574 visual)
1575 (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
1576 (let* ((creator (char-after (+ p 5)))
1577 (method (archive-l-e (+ p 10) 2))
1578 (modtime (archive-l-e (+ p 12) 2))
1579 (moddate (archive-l-e (+ p 14) 2))
1580 (ucsize (archive-l-e (+ p 24) 4))
1581 (fnlen (archive-l-e (+ p 28) 2))
1582 (exlen (archive-l-e (+ p 30) 2))
1583 (fclen (archive-l-e (+ p 32) 2))
1584 (lheader (archive-l-e (+ p 42) 4))
1585 (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
1586 (if file-name-coding-system
1587 (decode-coding-string str file-name-coding-system)
1588 (string-as-multibyte str))))
1589 (isdir (and (= ucsize 0)
1590 (string= (file-name-nondirectory efnname) "")))
1591 (mode (cond ((memq creator '(2 3)) ; Unix + VMS
1592 (archive-l-e (+ p 40) 2))
1593 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1594 (logior ?\444
1595 (if isdir (logior 16384 ?\111) 0)
1596 (if (zerop
1597 (logand 1 (char-after (+ p 38))))
1598 ?\222 0)))
1599 (t nil)))
1600 (modestr (if mode (archive-int-to-mode mode) "??????????"))
1601 (fiddle (and archive-zip-case-fiddle
1602 (not (not (memq creator '(0 2 4 5 9))))
1603 (string= (upcase efnname) efnname)))
1604 (ifnname (if fiddle (downcase efnname) efnname))
1605 (width (string-width ifnname))
1606 (text (format " %10s %8d %-11s %-8s %s"
1607 modestr
1608 ucsize
1609 (archive-dosdate moddate)
1610 (archive-dostime modtime)
1611 ifnname)))
1612 (setq maxlen (max maxlen width)
1613 totalsize (+ totalsize ucsize)
1614 visual (cons (vector text
1615 (- (length text) (length ifnname))
1616 (length text))
1617 visual)
1618 files (cons (if isdir
1620 (vector efnname ifnname fiddle mode
1621 (list (1- p) lheader)))
1622 files)
1623 p (+ p 46 fnlen exlen fclen))))
1624 (goto-char (point-min))
1625 (let ((dash (concat "- ---------- -------- ----------- -------- "
1626 (make-string maxlen ?-)
1627 "\n")))
1628 (insert "M Filemode Length Date Time File\n"
1629 dash)
1630 (archive-summarize-files (nreverse visual))
1631 (insert dash
1632 (format " %8d %d file%s"
1633 totalsize
1634 (length files)
1635 (if (= 1 (length files)) "" "s"))
1636 "\n"))
1637 (apply 'vector (nreverse files))))
1639 (defun archive-zip-extract (archive name)
1640 (if archive-zip-use-pkzip
1641 (archive-*-extract archive name archive-zip-extract)
1642 (archive-extract-by-stdout archive name archive-zip-extract)))
1644 (defun archive-zip-write-file-member (archive descr)
1645 (archive-*-write-file-member
1646 archive
1647 descr
1648 (if (aref descr 2) archive-zip-update-case archive-zip-update)))
1650 (defun archive-zip-chmod-entry (newmode files)
1651 (save-restriction
1652 (save-excursion
1653 (widen)
1654 (set-buffer-multibyte nil)
1655 (while files
1656 (let* ((fil (car files))
1657 (p (+ archive-proper-file-start (car (aref fil 4))))
1658 (creator (char-after (+ p 5)))
1659 (oldmode (aref fil 3))
1660 (newval (archive-calc-mode oldmode newmode t))
1661 buffer-read-only)
1662 (cond ((memq creator '(2 3)) ; Unix + VMS
1663 (goto-char (+ p 40))
1664 (delete-char 2)
1665 (insert (logand newval 255) (lsh newval -8)))
1666 ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
1667 (goto-char (+ p 38))
1668 (insert (logior (logand (char-after (point)) 254)
1669 (logand (logxor 1 (lsh newval -7)) 1)))
1670 (delete-char 1))
1671 (t (message "Don't know how to change mode for this member"))))
1672 (setq files (cdr files))))))
1673 ;; -------------------------------------------------------------------------
1674 ;; Section: Zoo Archives
1676 (defun archive-zoo-summarize ()
1677 (let ((p (1+ (archive-l-e 25 4)))
1678 (maxlen 8)
1679 (totalsize 0)
1680 files
1681 visual)
1682 (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
1683 (> (archive-l-e (+ p 6) 4) 0))
1684 (let* ((next (1+ (archive-l-e (+ p 6) 4)))
1685 (moddate (archive-l-e (+ p 14) 2))
1686 (modtime (archive-l-e (+ p 16) 2))
1687 (ucsize (archive-l-e (+ p 20) 4))
1688 (namefld (buffer-substring (+ p 38) (+ p 38 13)))
1689 (dirtype (char-after (+ p 4)))
1690 (lfnlen (if (= dirtype 2) (char-after (+ p 56)) 0))
1691 (ldirlen (if (= dirtype 2) (char-after (+ p 57)) 0))
1692 (fnlen (or (string-match "\0" namefld) 13))
1693 (efnname (let ((str
1694 (concat
1695 (if (> ldirlen 0)
1696 (concat (buffer-substring
1697 (+ p 58 lfnlen)
1698 (+ p 58 lfnlen ldirlen -1))
1699 "/")
1701 (if (> lfnlen 0)
1702 (buffer-substring (+ p 58)
1703 (+ p 58 lfnlen -1))
1704 (substring namefld 0 fnlen)))))
1705 (if file-name-coding-system
1706 (decode-coding-string str file-name-coding-system)
1707 (string-as-multibyte str))))
1708 (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
1709 (ifnname (if fiddle (downcase efnname) efnname))
1710 (width (string-width ifnname))
1711 (text (format " %8d %-11s %-8s %s"
1712 ucsize
1713 (archive-dosdate moddate)
1714 (archive-dostime modtime)
1715 ifnname)))
1716 (setq maxlen (max maxlen width)
1717 totalsize (+ totalsize ucsize)
1718 visual (cons (vector text
1719 (- (length text) (length ifnname))
1720 (length text))
1721 visual)
1722 files (cons (vector efnname ifnname fiddle nil (1- p))
1723 files)
1724 p next)))
1725 (goto-char (point-min))
1726 (let ((dash (concat "- -------- ----------- -------- "
1727 (make-string maxlen ?-)
1728 "\n")))
1729 (insert "M Length Date Time File\n"
1730 dash)
1731 (archive-summarize-files (nreverse visual))
1732 (insert dash
1733 (format " %8d %d file%s"
1734 totalsize
1735 (length files)
1736 (if (= 1 (length files)) "" "s"))
1737 "\n"))
1738 (apply 'vector (nreverse files))))
1740 (defun archive-zoo-extract (archive name)
1741 (archive-extract-by-stdout archive name archive-zoo-extract))
1742 ;; -------------------------------------------------------------------------
1743 ;; This line was a mistake; it is kept now for compatibility.
1744 ;; rms 15 Oct 98
1745 (provide 'archive-mode)
1747 (provide 'arc-mode)
1749 ;;; arc-mode.el ends here