home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / arc-mode.el < prev    next >
Encoding:
Text File  |  1995-07-05  |  54.6 KB  |  1,481 lines

  1. ;;; arc-mode.el --- simple editing of archives
  2.  
  3. ;;; Copyright (C) 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Morten Welinder (terra@diku.dk)
  6. ;; Keywords: archives msdog editing major-mode
  7. ;; Favourite-brand-of-beer: None, I hate beer.
  8.  
  9. ;;; This file is part of GNU Emacs.
  10. ;;;
  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.
  15. ;;;
  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.
  20. ;;;
  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
  23. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26. ;;
  27. ;; NAMING: "arc" is short for "archive" and does not refer specifically
  28. ;; to files whose name end in ".arc"
  29. ;;
  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-Dog myself.
  35. ;; -------------------------------------
  36. ;; INTERACTION: arc-mode.el should play together with
  37. ;;
  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.
  43. ;;
  44. ;; * dos-fns.el:  (Part of Emacs 19).  You get automatic ^M^J <--> ^J
  45. ;;                conversion.
  46. ;;
  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.
  54. ;;
  55. ;;                        Arc     Lzh     Zip     Zoo
  56. ;;                        --------------------------------
  57. ;; View listing           Intern  Intern  Intern  Intern
  58. ;; Extract member         Y       Y       Y       Y
  59. ;; Save changed member    Y       Y       Y       Y
  60. ;; Add new member         N       N       N       N
  61. ;; Delete member          Y       Y       Y       Y
  62. ;; Rename member          Y       Y       N       N
  63. ;; Chmod                  -       Y       Y       -
  64. ;; Chown                  -       Y       -       -
  65. ;; Chgrp                  -       Y       -       -
  66. ;;
  67. ;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
  68. ;; on the first released version of this package.
  69. ;;
  70. ;; This code is partly based on tar-mode.el from Emacs.
  71. ;; -------------------------------------
  72. ;; ARCHIVE STRUCTURES:
  73. ;; (This is mostly for myself.)
  74. ;;
  75. ;; ARC         A series of (header,file).  No interactions among members.
  76. ;;
  77. ;; LZH         A series of (header,file).  Headers are checksummed.  No
  78. ;;             interaction among members.
  79. ;;
  80. ;; ZIP         A series of (lheader,fil) followed by a "central directory"
  81. ;;             which is a series of (cheader) followed by an end-of-
  82. ;;             central-dir record possibly followed by junk.  The e-o-c-d
  83. ;;             links to c-d.  cheaders link to lheaders which are basically
  84. ;;             cut-down versions of the cheaders.
  85. ;;
  86. ;; ZOO         An archive header followed by a series of (header,file).
  87. ;;             Each member header points to the next.  The archive is
  88. ;;             terminated by a bogus header with a zero next link.
  89. ;; -------------------------------------
  90. ;; SETUP: .emacs fodder:
  91. ;;
  92. ;; (setq auto-mode-alist
  93. ;;       (cons '("\\.\\(arc\\|zip\\|lzh\\|zoo\\)\\'" . archive-mode)
  94. ;;             auto-mode-alist))
  95. ;; (autoload 'archive-mode "arc-mode" "Major mode for editing archives." t)
  96. ;;
  97. ;; Furthermore, for msdog, you need to make sure that the archives are loaded
  98. ;; as binary files.  For arc/zip/pak/lzh/zoo this is the default.
  99. ;; -------------------------------------
  100. ;; HOOKS: `foo' means one the the supported archive types.
  101. ;;
  102. ;; archive-mode-hook
  103. ;; archive-foo-mode-hook
  104. ;; archive-extract-hooks
  105.  
  106. ;;; Code:
  107.  
  108. ;; -------------------------------------------------------------------------
  109. ;; Section: Configuration.
  110.  
  111. (defvar archive-dos-members t
  112.   "*If non-nil then recognize member files using ^M^J as line terminator
  113. and do The Right Thing.")
  114.  
  115. (defvar archive-tmpdir
  116.   (expand-file-name
  117.    (make-temp-name (if (eq system-type 'ms-dos) "ar" "archive.tmp"))
  118.    (or (getenv "TMPDIR") (getenv "TMP") "/tmp"))
  119.   "*Directory for temporary files made by arc-mode.el")
  120.  
  121. (defvar archive-remote-regexp "^/[^/:]*[^/:]:"
  122.   "*Regexp recognizing archive files names that are not local (i.e., are
  123. not proper file names outside Emacs).  A local copy a the archive will
  124. be used when updating.")
  125.  
  126. (defvar archive-extract-hooks nil
  127.   "*Hooks to run when an archive member has been extracted.")
  128. ;; ------------------------------
  129. ;; Arc archive configuration
  130.  
  131. ;; We always go via a local file since there seems to be no reliable way
  132. ;; to extract to stdout without junk getting added.
  133. (defvar archive-arc-extract
  134.   '("arc" "x")
  135.   "*Program and its options to run in order to extract an arc file member
  136. to the current directory.  Archive and member name will be added.")
  137.  
  138. (defvar archive-arc-expunge
  139.   '("arc" "d")
  140.   "*Program and its options to run in order to delete arc file members.
  141. Archive and member names will be added.")
  142.  
  143. (defvar archive-arc-write-file-member
  144.   '("arc" "u")
  145.   "*Program and its options to run in order to update an arc file member.
  146. Archive and member name will be added.")
  147. ;; ------------------------------
  148. ;; Lzh archive configuration
  149.  
  150. (defvar archive-lzh-extract
  151.   '("lha" "pq")
  152.   "*Program and its options to run in order to extract an lzh file member
  153. to standard output.  Archive and member name will be added.")
  154.  
  155. (defvar archive-lzh-expunge
  156.   '("lha" "d")
  157.   "*Program and its options to run in order to delete lzh file members.
  158. Archive and member names will be added.")
  159.  
  160. (defvar archive-lzh-write-file-member
  161.   '("lha" "a")
  162.   "*Program and its options to run in order to update an lzh file member.
  163. Archive and member name will be added.")
  164. ;; ------------------------------
  165. ;; Zip archive configuration
  166.  
  167. (defvar archive-zip-use-pkzip (memq system-type '(ms-dos windows-nt))
  168.   "*If non-nil then all zip options default to values suitable when using
  169. pkzip and pkunzip.  Only set to true for msdog systems!")
  170.  
  171. (defvar archive-zip-extract
  172.   (if archive-zip-use-pkzip '("pkunzip" "-e") '("unzip" "-qq" "-c"))
  173.   "*Program and its options to run in order to extract a zip file member
  174. to standard output.  Archive and member name will be added.\n
  175. If `archive-zip-use-pkzip' is non-nil then this program is expected to
  176. extract to a file junking the directory part of the name.")
  177.  
  178. ;; For several reasons the latter behaviour is not desireable in general.
  179. ;; (1) It uses more disk space.  (2) Error checking is worse or non-
  180. ;; existent.  (3) It tends to do funny things with other systems' file
  181. ;; names.
  182.  
  183. (defvar archive-zip-expunge
  184.   (if archive-zip-use-pkzip '("pkzip" "-d") '("zip" "-d" "-q"))
  185.   "*Program and its options to run in order to delete zip file members.
  186. Archive and member names will be added.")
  187.  
  188. (defvar archive-zip-update
  189.   (if archive-zip-use-pkzip '("pkzip" "-u") '("zip" "-q"))
  190.   "*Program and its options to run in order to update a zip file member.
  191. Options should ensure that specified directory will be put into the zip
  192. file.  Archive and member name will be added.")
  193.  
  194. (defvar archive-zip-update-case
  195.   (if archive-zip-use-pkzip archive-zip-update '("zip" "-q" "-k"))
  196.   "*Program and its options to run in order to update a case fiddled
  197. zip file member.  Options should ensure that specified directory will
  198. be put into the zip file.  Archive and member name will be added.")
  199.  
  200. (defvar archive-zip-case-fiddle t
  201.   "*If non-nil then zip file members are mapped to lower case if created
  202. by a system that under single case file names.")
  203. ;; ------------------------------
  204. ;; Zoo archive configuration
  205.  
  206. (defvar archive-zoo-extract
  207.   '("zoo" "xpq")
  208.   "*Program and its options to run in order to extract a zoo file member
  209. to standard output.  Archive and member name will be added.")
  210.  
  211. (defvar archive-zoo-expunge
  212.   '("zoo" "DqPP")
  213.   "*Program and its options to run in order to delete zoo file members.
  214. Archive and member names will be added.")
  215.  
  216. (defvar archive-zoo-write-file-member
  217.   '("zoo" "a")
  218.   "*Program and its options to run in order to update a zoo file member.
  219. Archive and member name will be added.")
  220. ;; -------------------------------------------------------------------------
  221. ;; Section: Variables
  222.  
  223. (defvar archive-subtype nil "*Symbol describing archive type.")
  224. (defvar archive-file-list-start nil "*Position of first contents line.")
  225. (defvar archive-file-list-end nil "*Position just after last contents line.")
  226. (defvar archive-proper-file-start nil "*Position of real archive's start.")
  227. (defvar archive-read-only nil "*Non-nil if the archive is read-only on disk.")
  228. (defvar archive-remote nil "*Non-nil if the archive is outside file system.")
  229. (defvar archive-local-name nil "*Name of local copy of remote archive.")
  230. (defvar archive-mode-map nil "*Local keymap for archive mode listings.")
  231. (defvar archive-file-name-indent nil "*Column where file names start.")
  232.  
  233. (defvar archive-alternate-display nil
  234.   "*Non-nil when alternate information is shown.")
  235. (make-variable-buffer-local 'archive-alternate-display)
  236. (put 'archive-alternate-display 'permanent-local t)
  237.  
  238. (defvar archive-superior-buffer nil "*In archive members, points to archive.")
  239. (put 'archive-superior-buffer 'permanent-local t)
  240.  
  241. (defvar archive-subfile-mode nil "*Non-nil in archive member buffers.")
  242. (make-variable-buffer-local 'archive-subfile-mode)
  243. (put 'archive-subfile-mode 'permanent-local t)
  244.  
  245. ;; buffer-file-type is a per-buffer variable in the msdog configuration
  246. (if (boundp 'buffer-file-type) nil
  247.   (defvar buffer-file-type nil
  248.     "*Nil for dos-style text file, non-nil otherwise.")
  249.   (make-variable-buffer-local 'buffer-file-type)
  250.   (put 'buffer-file-type 'permanent-local t)
  251.   (setq-default buffer-file-type nil))
  252.  
  253. (defvar archive-subfile-dos nil
  254.   "Negation of `buffer-file-type' which see.")
  255. (make-variable-buffer-local 'archive-subfile-dos)
  256. (put 'archive-subfile-dos 'permanent-local t)
  257.  
  258. (defvar archive-files nil "Vector of file descriptors.  Each descriptor is
  259. a vector of [ext-file-name int-file-name case-fiddled mode ...]")
  260. (make-variable-buffer-local 'archive-files)
  261.  
  262. (defvar archive-lemacs
  263.   (string-match "\\(Lucid\\|XEmacs\\)" emacs-version)
  264.   "*Non-nil when running under under Lucid Emacs or XEmacs.")
  265. ;; -------------------------------------------------------------------------
  266. ;; Section: Support functions.
  267.  
  268. (defsubst archive-name (suffix)
  269.   (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
  270.  
  271. (defun archive-l-e (str &optional len)
  272.   "Convert little endian string/vector to integer.  Alternatively, first
  273. argument may be a buffer position in the current buffer in which case a
  274. second arguemnt, length, should be supplied."
  275.   (if (stringp str)
  276.       (setq len (length str))
  277.     (setq str (buffer-substring str (+ str len))))
  278.   (let ((result 0)
  279.         (i 0))
  280.     (while (< i len)
  281.       (setq i (1+ i)
  282.             result (+ (ash result 8) (aref str (- len i)))))
  283.     result))
  284.  
  285. (defun archive-int-to-mode (mode)
  286.   "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------"
  287.   (let ((str (make-string 10 ?-)))
  288.     (or (zerop (logand 16384 mode)) (aset str 0 ?d))
  289.     (or (zerop (logand  8192 mode)) (aset str 0 ?c)) ; completeness
  290.     (or (zerop (logand   256 mode)) (aset str 1 ?r))
  291.     (or (zerop (logand   128 mode)) (aset str 2 ?w))
  292.     (or (zerop (logand    64 mode)) (aset str 3 ?x))
  293.     (or (zerop (logand    32 mode)) (aset str 4 ?r))
  294.     (or (zerop (logand    16 mode)) (aset str 5 ?w))
  295.     (or (zerop (logand     8 mode)) (aset str 6 ?x))
  296.     (or (zerop (logand     4 mode)) (aset str 7 ?r))
  297.     (or (zerop (logand     2 mode)) (aset str 8 ?w))
  298.     (or (zerop (logand     1 mode)) (aset str 9 ?x))
  299.     (or (zerop (logand  1024 mode)) (aset str 3 (if (zerop (logand 64 mode))
  300.                             ?S ?s)))
  301.     (or (zerop (logand  2048 mode)) (aset str 6 (if (zerop (logand  8 mode))
  302.                             ?S ?s)))
  303.     str))
  304.  
  305. (defun archive-calc-mode (oldmode newmode &optional error)
  306.   "From the integer OLDMODE and the string NEWMODE calculate a new file
  307. mode.\n
  308. NEWMODE may be an octal number including a leading zero in which case it
  309. will become the new mode.\n
  310. NEWMODE may also be a relative specification like \"og-rwx\" in which case
  311. OLDMODE will be modified accordingly just like chmod(2) would have done.\n
  312. If optional third argument ERROR is non-nil an error will be signaled if
  313. the mode is invalid.  If ERROR is nil then nil will be returned."
  314.   (cond ((string-match "^0[0-7]*$" newmode)
  315.      (let ((result 0)
  316.            (len (length newmode))
  317.            (i 1))
  318.        (while (< i len)
  319.          (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
  320.            i (1+ i)))
  321.        (logior (logand oldmode 65024) result)))
  322.     ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
  323.      (let ((who 0)
  324.            (result oldmode)
  325.            (op (aref newmode (match-beginning 2)))
  326.            (bits 0)
  327.            (i (match-beginning 3)))
  328.        (while (< i (match-end 3))
  329.          (let ((rwx (aref newmode i)))
  330.            (setq bits (logior bits (cond ((= rwx ?r)  292)
  331.                          ((= rwx ?w)  146)
  332.                          ((= rwx ?x)   73)
  333.                          ((= rwx ?s) 3072)
  334.                          ((= rwx ?t)  512)))
  335.              i (1+ i))))
  336.        (while (< who (match-end 1))
  337.          (let* ((whoc (aref newmode who))
  338.             (whomask (cond ((= whoc ?a) 4095)
  339.                    ((= whoc ?u) 1472)
  340.                    ((= whoc ?g) 2104)
  341.                    ((= whoc ?o)    7))))
  342.            (if (= op ?=)
  343.            (setq result (logand result (lognot whomask))))
  344.            (if (= op ?-)
  345.            (setq result (logand result (lognot (logand whomask bits))))
  346.          (setq result (logior result (logand whomask bits)))))
  347.          (setq who (1+ who)))
  348.        result))
  349.     (t
  350.      (if error
  351.          (error "Invalid mode specification: %s" newmode)))))
  352.  
  353. (defun archive-dosdate (date)
  354.   "Stringify dos packed DATE record."
  355.   (let ((year (+ 1980 (logand (ash date -9) 127)))
  356.         (month (logand (ash date -5) 15))
  357.         (day (logand date 31)))
  358.     (if (or (> month 12) (< month 1))
  359.         ""
  360.       (format "%2d-%s-%d"
  361.               day
  362.               (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
  363.                      "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
  364.               year))))
  365.  
  366. (defun archive-dostime (time)
  367.   "Stringify dos packed TIME record."
  368.   (let ((hour (logand (ash time -11) 31))
  369.         (minute (logand (ash time -5) 53))
  370.         (second (* 2 (logand time 31)))) ; 2 seconds resolution
  371.     (format "%02d:%02d:%02d" hour minute second)))
  372.  
  373. ;;(defun archive-unixdate (low high)
  374. ;;  "Stringify unix (LOW HIGH) date."
  375. ;;  (let ((str (current-time-string (cons high low))))
  376. ;;    (format "%s-%s-%s"
  377. ;;        (substring str 8 9)
  378. ;;        (substring str 4 7)
  379. ;;        (substring str 20 24))))
  380.  
  381. ;;(defun archive-unixtime (low high)
  382. ;;  "Stringify unix (LOW HIGH) time."
  383. ;;  (let ((str (current-time-string (cons high low))))
  384. ;;    (substring str 11 19)))
  385.  
  386. (defun archive-get-lineno ()
  387.   (if (>= (point) archive-file-list-start)
  388.       (count-lines archive-file-list-start
  389.            (save-excursion (beginning-of-line) (point)))
  390.     0))
  391.  
  392. (defun archive-get-descr (&optional noerror)
  393.   "Return the descriptor vector for file at point.  Do not signal an error
  394. if optional second argument NOERROR is non-nil."
  395.   (let ((no (archive-get-lineno)))
  396.     (if (and (>= (point) archive-file-list-start)
  397.              (< no (length archive-files)))
  398.     (let ((item (aref archive-files no)))
  399.       (if (vectorp item)
  400.           item
  401.         (if (not noerror)
  402.         (error "Entry is not a regular member of the archive"))))
  403.       (if (not noerror)
  404.           (error "Line does not describe a member of the archive")))))
  405. ;; -------------------------------------------------------------------------
  406. ;; Section: the mode definition
  407.  
  408. (defun archive-mode (&optional force)
  409.   "Major mode for viewing an archive file as a dired-like listing of its
  410. contents.  You can move around using the usual cursor motion commands.
  411. Letters no longer insert themselves.
  412. Type `e' to pull a file out of the archive and into its own buffer;
  413. or click mouse-2 on the file's line in the archive mode buffer.
  414.  
  415. If you edit a sub-file of this archive (as with the `e' command) and
  416. save it, the contents of that buffer will be saved back into the
  417. archive.
  418.  
  419. \\{archive-mode-map}"
  420.   ;; This is not interactive because you shouldn't be turning this
  421.   ;; mode on and off.  You can corrupt things that way.
  422.   (if (zerop (buffer-size))
  423.       ;; At present we cannot create archives from scratch
  424.       (funcall default-major-mode)
  425.     (if (and (not force) archive-files) nil
  426.       (let* ((type (archive-find-type))
  427.          (typename (copy-sequence (symbol-name type))))
  428.     (aset typename 0 (upcase (aref typename 0)))
  429.     (kill-all-local-variables)
  430.     (make-local-variable 'archive-subtype)
  431.     (setq archive-subtype type)
  432.  
  433.     ;; Buffer contains treated image of file before the file contents
  434.     (make-local-variable 'revert-buffer-function)
  435.     (setq revert-buffer-function 'archive-mode-revert)
  436.     (auto-save-mode 0)
  437.     (make-local-variable 'local-write-file-hooks)
  438.     (add-hook 'local-write-file-hooks 'archive-write-file)
  439.  
  440.     ;; Real file contents is binary
  441.     (make-local-variable 'require-final-newline)
  442.     (setq require-final-newline nil)
  443.     (make-local-variable 'enable-local-variables)
  444.     (setq enable-local-variables nil)
  445.     (setq buffer-file-type t)
  446.  
  447.     (make-local-variable 'archive-read-only)
  448.     (setq archive-read-only (not (file-writable-p (buffer-file-name))))
  449.  
  450.     ;; Should we use a local copy when accessing from outside Emacs?
  451.     (make-local-variable 'archive-local-name)
  452.     (make-local-variable 'archive-remote)
  453.     (setq archive-remote (string-match archive-remote-regexp
  454.                        (buffer-file-name)))
  455.  
  456.     (setq major-mode 'archive-mode)
  457.     (setq mode-name (concat typename "-Archive"))
  458.     ;; Run archive-foo-mode-hook and archive-mode-hook
  459.     (run-hooks (archive-name "mode-hook") 'archive-mode-hook)
  460.     (use-local-map archive-mode-map))
  461.  
  462.       (make-local-variable 'archive-proper-file-start)
  463.       (make-local-variable 'archive-file-list-start)
  464.       (make-local-variable 'archive-file-list-end)
  465.       (make-local-variable 'archive-file-name-indent)
  466.       (archive-summarize)
  467.       (setq buffer-read-only t))))
  468.  
  469. ;; Archive mode is suitable only for specially formatted data.
  470. (put 'archive-mode 'mode-class 'special)
  471. ;; -------------------------------------------------------------------------
  472. ;; Section: Key maps
  473.  
  474. (if archive-mode-map nil
  475.   (setq archive-mode-map (make-keymap))
  476.   (suppress-keymap archive-mode-map)
  477.   (define-key archive-mode-map " " 'archive-next-line)
  478.   (define-key archive-mode-map "a" 'archive-alternate-display)
  479.   ;;(define-key archive-mode-map "c" 'archive-copy)
  480.   (define-key archive-mode-map "d" 'archive-flag-deleted)
  481.   (define-key archive-mode-map "\C-d" 'archive-flag-deleted)
  482.   (define-key archive-mode-map "e" 'archive-extract)
  483.   (define-key archive-mode-map "f" 'archive-extract)
  484.   (define-key archive-mode-map "\C-m" 'archive-extract)
  485.   (define-key archive-mode-map "g" 'revert-buffer)
  486.   (define-key archive-mode-map "h" 'describe-mode)
  487.   (define-key archive-mode-map "m" 'archive-mark)
  488.   (define-key archive-mode-map "n" 'archive-next-line)
  489.   (define-key archive-mode-map "\C-n" 'archive-next-line)
  490.   (define-key archive-mode-map [down] 'archive-next-line)
  491.   (define-key archive-mode-map "o" 'archive-extract-other-window)
  492.   (define-key archive-mode-map "p" 'archive-previous-line)
  493.   (define-key archive-mode-map "\C-p" 'archive-previous-line)
  494.   (define-key archive-mode-map [up] 'archive-previous-line)
  495.   (define-key archive-mode-map "r" 'archive-rename-entry)
  496.   (define-key archive-mode-map "u" 'archive-unflag)
  497.   (define-key archive-mode-map "\M-\C-?" 'archive-unmark-all-files)
  498.   (define-key archive-mode-map "v" 'archive-view)
  499.   (define-key archive-mode-map "x" 'archive-expunge)
  500.   (define-key archive-mode-map "\177" 'archive-unflag-backwards)
  501.   (define-key archive-mode-map "E" 'archive-extract-other-window)
  502.   (define-key archive-mode-map "M" 'archive-chmod-entry)
  503.   (define-key archive-mode-map "G" 'archive-chgrp-entry)
  504.   (define-key archive-mode-map "O" 'archive-chown-entry)
  505.  
  506.   (if archive-lemacs
  507.       (progn
  508.     ;; Not a nice "solution" but it'll have to do
  509.     (define-key archive-mode-map "\C-xu" 'archive-undo)
  510.     (define-key archive-mode-map "\C-_" 'archive-undo))
  511.     (substitute-key-definition 'undo 'archive-undo
  512.                    archive-mode-map global-map))
  513.  
  514.   (define-key archive-mode-map
  515.     (if archive-lemacs 'button2 [mouse-2]) 'archive-mouse-extract)
  516.  
  517.   (if archive-lemacs
  518.       ()                ; out of luck
  519.     ;; Get rid of the Edit menu bar item to save space.
  520.     (define-key archive-mode-map [menu-bar edit] 'undefined)
  521.  
  522.     (define-key archive-mode-map [menu-bar immediate]
  523.       (cons "Immediate" (make-sparse-keymap "Immediate")))
  524.     (define-key archive-mode-map [menu-bar immediate alternate]
  525.       '("Alternate Display" . archive-alternate-display))
  526.     (put 'archive-alternate-display 'menu-enable
  527.      '(boundp (archive-name "alternate-display")))
  528.     (define-key archive-mode-map [menu-bar immediate view]
  529.       '("View This File" . archive-view))
  530.     (define-key archive-mode-map [menu-bar immediate display]
  531.       '("Display in Other Window" . archive-display-other-window))
  532.     (define-key archive-mode-map [menu-bar immediate find-file-other-window]
  533.       '("Find in Other Window" . archive-extract-other-window))
  534.     (define-key archive-mode-map [menu-bar immediate find-file]
  535.       '("Find This File" . archive-extract))
  536.  
  537.     (define-key archive-mode-map [menu-bar mark]
  538.       (cons "Mark" (make-sparse-keymap "Mark")))
  539.     (define-key archive-mode-map [menu-bar mark unmark-all]
  540.       '("Unmark All" . archive-unmark-all-files))
  541.     (define-key archive-mode-map [menu-bar mark deletion]
  542.       '("Flag" . archive-flag-deleted))
  543.     (define-key archive-mode-map [menu-bar mark unmark]
  544.       '("Unflag" . archive-unflag))
  545.     (define-key archive-mode-map [menu-bar mark mark]
  546.       '("Mark" . archive-mark))
  547.  
  548.     (define-key archive-mode-map [menu-bar operate]
  549.       (cons "Operate" (make-sparse-keymap "Operate")))
  550.     (define-key archive-mode-map [menu-bar operate chown]
  551.       '("Change Owner..." . archive-chown-entry))
  552.     (put 'archive-chown-entry 'menu-enable
  553.      '(fboundp (archive-name "chown-entry")))
  554.     (define-key archive-mode-map [menu-bar operate chgrp]
  555.       '("Change Group..." . archive-chgrp-entry))
  556.     (put 'archive-chgrp-entry 'menu-enable
  557.      '(fboundp (archive-name "chgrp-entry")))
  558.     (define-key archive-mode-map [menu-bar operate chmod]
  559.       '("Change Mode..." . archive-chmod-entry))
  560.     (put 'archive-chmod-entry 'menu-enable
  561.      '(fboundp (archive-name "chmod-entry")))
  562.     (define-key archive-mode-map [menu-bar operate rename]
  563.       '("Rename to..." . archive-rename-entry))
  564.     (put 'archive-rename-entry 'menu-enable
  565.      '(fboundp (archive-name "rename-entry")))
  566.     ;;(define-key archive-mode-map [menu-bar operate copy]
  567.     ;;  '("Copy to..." . archive-copy))
  568.     (define-key archive-mode-map [menu-bar operate expunge]
  569.       '("Expunge Marked Files" . archive-expunge))
  570.   ))
  571.  
  572. (let* ((item1 '(archive-subfile-mode " Archive"))
  573.        (item2 '(archive-subfile-dos " Dos"))
  574.        (items (if (memq system-type '(ms-dos windows-nt))
  575.           (list item1) ; msdog has its own indicator
  576.         (list item1 item2))))
  577.   (or (member item1 minor-mode-alist)
  578.       (setq minor-mode-alist (append items minor-mode-alist))))
  579. ;; -------------------------------------------------------------------------
  580. (defun archive-find-type ()
  581.   (widen)
  582.   (goto-char (point-min))
  583.   ;; The funny [] here make it unlikely that the .elc file will be treated
  584.   ;; as an archive by other software.
  585.   (let (case-fold-search)
  586.     (cond ((looking-at "[P]K\003\004") 'zip)
  587.       ((looking-at "..-l[hz][0-9]-") 'lzh)
  588.       ((looking-at "....................[\334]\247\304\375") 'zoo)
  589.       ((and (looking-at "\C-z")    ; signature too simple, IMHO
  590.         (string-match "\\.[aA][rR][cC]$"
  591.                   (or buffer-file-name (buffer-name))))
  592.        'arc)
  593.       (t (error "Buffer format not recognized.")))))
  594. ;; -------------------------------------------------------------------------
  595. (defun archive-summarize ()
  596.   "Parse the contents of the archive file in the current buffer.
  597. Place a dired-like listing on the front;
  598. then narrow to it, so that only that listing
  599. is visible (and the real data of the buffer is hidden)."
  600.   (widen)
  601.   (let (buffer-read-only)
  602.     (message "Parsing archive file...")
  603.     (buffer-disable-undo (current-buffer))
  604.     (setq archive-files (funcall (archive-name "summarize")))
  605.     (message "Parsing archive file...done.")
  606.     (setq archive-proper-file-start (point-marker))
  607.     (narrow-to-region (point-min) (point))
  608.     (set-buffer-modified-p nil)
  609.     (buffer-enable-undo))
  610.   (goto-char archive-file-list-start)
  611.   (archive-next-line 0))
  612.  
  613. (defun archive-resummarize ()
  614.   "Recreate the contents listing of an archive."
  615.   (let ((modified (buffer-modified-p))
  616.     (no (archive-get-lineno))
  617.     buffer-read-only)
  618.     (widen)
  619.     (delete-region (point-min) archive-proper-file-start)
  620.     (archive-summarize)
  621.     (set-buffer-modified-p modified)
  622.     (goto-char archive-file-list-start)
  623.     (archive-next-line no)))
  624.  
  625. (defun archive-summarize-files (files)
  626.   "Insert a desciption of a list of files annotated with proper mouse face"
  627.   (setq archive-file-list-start (point-marker))
  628.   (setq archive-file-name-indent (if files (aref (car files) 1) 0))
  629.   ;; We don't want to do an insert for each element since that takes too
  630.   ;; long when the archive -- which has to be moved in memory -- is large.
  631.   (insert
  632.    (apply
  633.     (function concat)
  634.     (mapcar
  635.      (function 
  636.       (lambda (fil)
  637.     ;; Using `concat' here copies the text also, so we can add
  638.     ;; properties without problems.
  639.     (let ((text (concat (aref fil 0) "\n")))
  640.       (if archive-lemacs
  641.           ()            ; out of luck
  642.         (put-text-property (aref fil 1) (aref fil 2)
  643.                    'mouse-face 'highlight
  644.                    text))
  645.       text)))
  646.      files)))
  647.   (setq archive-file-list-end (point-marker)))
  648.  
  649. (defun archive-alternate-display ()
  650.   "Toggle alternative display.  To avoid very long lines some archive mode
  651. don't show all information.  This function changes the set of information
  652. shown for each files."
  653.   (interactive)
  654.   (setq archive-alternate-display (not archive-alternate-display))
  655.   (archive-resummarize))
  656. ;; -------------------------------------------------------------------------
  657. ;; Section: Local archive copy handling
  658.  
  659. (defun archive-maybe-copy (archive)
  660.   (if archive-remote
  661.       (let ((start (point-max)))
  662.     (setq archive-local-name (expand-file-name
  663.                   (file-name-nondirectory archive)
  664.                   archive-tmpdir))
  665.     (make-directory archive-tmpdir t)
  666.     (save-restriction
  667.       (widen)
  668.       (write-region start (point-max) archive-local-name nil 'nomessage))
  669.     archive-local-name)
  670.     (if (buffer-modified-p) (save-buffer))
  671.     archive))
  672.  
  673. (defun archive-maybe-update (unchanged)
  674.   (if archive-remote
  675.       (let ((name archive-local-name)
  676.         (modified (buffer-modified-p))
  677.         buffer-read-only)
  678.     (if unchanged nil
  679.       (erase-buffer)
  680.       (insert-file-contents name)
  681.       (archive-mode t))
  682.     (archive-delete-local name)
  683.     (if (not unchanged)
  684.         (message "Archive file must be saved for changes to take effect"))
  685.     (set-buffer-modified-p (or modified (not unchanged))))))
  686.  
  687. (defun archive-delete-local (name)
  688.   "Delete (robust) the file NAME and its parents up to and including the
  689. value of `archive-tmpdir'."
  690.   (let ((again t)
  691.     (top (directory-file-name (file-name-as-directory archive-tmpdir))))
  692.     (condition-case nil
  693.     (delete-file name)
  694.       (error nil))
  695.     (while again
  696.       (setq name (directory-file-name (file-name-directory name)))
  697.       (condition-case nil
  698.       (delete-directory name)
  699.     (error nil))
  700.       (if (string= name top) (setq again nil)))))
  701. ;; -------------------------------------------------------------------------
  702. ;; Section: Member extraction
  703.  
  704. (defun archive-mouse-extract (event)
  705.   "Extract a file whose name you click on."
  706.   (interactive "e")
  707.   (mouse-set-point event)
  708.   (switch-to-buffer
  709.    (save-excursion
  710.      (archive-extract)
  711.      (current-buffer))))
  712.  
  713. (defun archive-extract (&optional other-window-p)
  714.   "In archive mode, extract this entry of the archive into its own buffer."
  715.   (interactive)
  716.   (let* ((view-p (eq other-window-p 'view))
  717.      (descr (archive-get-descr))
  718.          (ename (aref descr 0))
  719.          (iname (aref descr 1))
  720.          (archive-buffer (current-buffer))
  721.          (arcdir default-directory)
  722.          (archive (buffer-file-name))
  723.          (arcname (file-name-nondirectory archive))
  724.          (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
  725.          (extractor (archive-name "extract"))
  726.          (read-only-p (or archive-read-only view-p))
  727.          (buffer (get-buffer bufname))
  728.          (just-created nil))
  729.       (if buffer
  730.           nil
  731.     (setq archive (archive-maybe-copy archive))
  732.         (setq buffer (get-buffer-create bufname))
  733.         (setq just-created t)
  734.         (save-excursion
  735.           (set-buffer buffer)
  736.           (setq buffer-file-name
  737.                 (expand-file-name (concat arcname ":" iname)))
  738.           (setq buffer-file-truename
  739.                 (abbreviate-file-name buffer-file-name))
  740.           ;; Set the default-directory to the dir of the superior buffer.
  741.           (setq default-directory arcdir)
  742.           (make-local-variable 'archive-superior-buffer)
  743.           (setq archive-superior-buffer archive-buffer)
  744.           (make-local-variable 'local-write-file-hooks)
  745.           (add-hook 'local-write-file-hooks 'archive-write-file-member)
  746.           (setq archive-subfile-mode descr)
  747.       (setq archive-subfile-dos nil
  748.         buffer-file-type t)
  749.       (if (fboundp extractor)
  750.           (funcall extractor archive ename)
  751.         (archive-*-extract archive ename (symbol-value extractor)))
  752.           (if archive-dos-members (archive-check-dos))
  753.           (goto-char (point-min))
  754.           (rename-buffer bufname)
  755.           (setq buffer-read-only read-only-p)
  756.       (setq buffer-undo-list nil)
  757.           (set-buffer-modified-p nil)
  758.       (setq buffer-saved-size (buffer-size))
  759.           (normal-mode)
  760.       ;; Just in case an archive occurs inside another archive.
  761.       (if (eq major-mode 'archive-mode)
  762.           (setq archive-remote t))
  763.       (run-hooks 'archive-extract-hooks))
  764.     (archive-maybe-update t))
  765.       (if view-p
  766.           (progn
  767.             (view-buffer buffer)
  768.             (and just-created (setq view-exit-action 'kill-buffer)))
  769.         (if (eq other-window-p 'display)
  770.             (display-buffer buffer)
  771.           (if other-window-p
  772.               (switch-to-buffer-other-window buffer)
  773.             (switch-to-buffer buffer))))))
  774.  
  775. (defun archive-*-extract (archive name command)
  776.   (let* ((default-directory (file-name-as-directory archive-tmpdir))
  777.      (tmpfile (expand-file-name (file-name-nondirectory name)
  778.                     default-directory)))
  779.     (make-directory (directory-file-name default-directory) t)
  780.     (apply 'call-process
  781.        (car command)
  782.        nil
  783.        nil
  784.        nil
  785.        (append (cdr command) (list archive name)))
  786.     (insert-file-contents tmpfile)
  787.     (archive-delete-local tmpfile)))
  788.  
  789. (defun archive-extract-by-stdout (archive name command)
  790.   (let ((binary-process-output t)) ; for Ms-Dos
  791.     (apply 'call-process
  792.        (car command)
  793.        nil
  794.        t
  795.        nil
  796.        (append (cdr command) (list archive name)))))
  797.  
  798. (defun archive-extract-other-window ()
  799.   "In archive mode, find this member in another window."
  800.   (interactive)
  801.   (archive-extract t))
  802.  
  803. (defun archive-display-other-window ()
  804.   "In archive mode, display this member in another window."
  805.   (interactive)
  806.   (archive-extract 'display))
  807.  
  808. (defun archive-view ()
  809.   "In archive mode, view the member on this line."
  810.   (interactive)
  811.   (archive-extract 'view))
  812.  
  813. (defun archive-add-new-member (arcbuf name)
  814.   "Add the file in the current buffer to the archive in ARCBUF naming it
  815. NAME."
  816.   (interactive
  817.    (list (get-buffer
  818.       (read-buffer "Buffer containing archive: "
  819.                ;; Find first archive buffer and suggest that
  820.                (let ((bufs (buffer-list)))
  821.              (while (and bufs (not (eq (save-excursion
  822.                              (set-buffer (car bufs))
  823.                              major-mode)
  824.                            'archive-mode)))
  825.                (setq bufs (cdr bufs)))
  826.              (if bufs
  827.                  (car bufs)
  828.                (error "There are no archive buffers")))
  829.                t))
  830.      (read-string "File name in archive: "
  831.               (if buffer-file-name
  832.               (file-name-nondirectory buffer-file-name)
  833.             ""))))
  834.   (save-excursion
  835.     (set-buffer arcbuf)
  836.     (or (eq major-mode 'archive-mode)
  837.     (error "Buffer is not an archive buffer"))
  838.     (if archive-read-only
  839.     (error "Archive is read-only")))
  840.   (if (eq arcbuf (current-buffer))
  841.       (error "An archive buffer cannot be added to itself"))
  842.   (if (string= name "")
  843.       (error "Archive members may not be given empty names"))
  844.   (let ((func (save-excursion (set-buffer arcbuf)
  845.                   (archive-name "add-new-member")))
  846.     (membuf (current-buffer)))
  847.     (if (fboundp func)
  848.     (save-excursion
  849.       (set-buffer arcbuf)
  850.       (funcall func buffer-file-name membuf name))
  851.       (error "Adding a new member is not supported for this archive type"))))
  852. ;; -------------------------------------------------------------------------
  853. ;; Section: IO stuff
  854.  
  855. (defun archive-check-dos (&optional force)
  856.   "*If this looks like a buffer with ^M^J as line terminator then remove
  857. those ^Ms and set archive-subfile-dos."
  858.   (save-restriction
  859.     (widen)
  860.     (save-excursion
  861.       (goto-char (point-min))
  862.       (setq archive-subfile-dos
  863.         (or force (not (search-forward-regexp "[^\r]\n" nil t))))
  864.       (setq buffer-file-type (not archive-subfile-dos))
  865.       (if archive-subfile-dos
  866.           (let ((modified (buffer-modified-p)))
  867.             (buffer-disable-undo (current-buffer))
  868.             (goto-char (point-min))
  869.             (while (search-forward "\r\n" nil t)
  870.               (replace-match "\n"))
  871.             (buffer-enable-undo)
  872.             (set-buffer-modified-p modified))))))
  873.  
  874. (defun archive-write-file-member ()
  875.   (if archive-subfile-dos
  876.       (save-restriction
  877.     (widen)
  878.         (save-excursion
  879.           (goto-char (point-min))
  880.           ;; We don't want our ^M^J <--> ^J changes to show in the undo list
  881.           (let ((undo-list buffer-undo-list))
  882.             (unwind-protect
  883.                 (progn
  884.                   (setq buffer-undo-list t)
  885.                   (while (search-forward "\n" nil t)
  886.                     (replace-match "\r\n"))
  887.                   (setq archive-subfile-dos nil)
  888.                   (setq buffer-file-type t)
  889.                   ;; OK, we're now have explicit ^M^Js -- save and re-unixfy
  890.                   (archive-write-file-member))
  891.               (progn
  892.                 (archive-check-dos t)
  893.                 (setq buffer-undo-list undo-list))))
  894.           t))
  895.     (save-excursion
  896.       (save-restriction
  897.         (message "Updating archive...")
  898.         (widen)
  899.     (let ((writer  (save-excursion (set-buffer archive-superior-buffer)
  900.                        (archive-name "write-file-member")))
  901.           (archive (save-excursion (set-buffer archive-superior-buffer)
  902.                        (buffer-file-name))))
  903.       (if (fboundp writer)
  904.           (funcall writer archive archive-subfile-mode)
  905.         (archive-*-write-file-member archive
  906.                      archive-subfile-mode
  907.                      (symbol-value writer))))
  908.     (set-buffer-modified-p nil)
  909.         (message "Updating archive...done")
  910.         (set-buffer archive-superior-buffer)
  911.         (revert-buffer)
  912.         t))))
  913.  
  914. (defun archive-*-write-file-member (archive descr command)
  915.   (let* ((ename (aref descr 0))
  916.          (tmpfile (expand-file-name ename archive-tmpdir))
  917.          (top (directory-file-name (file-name-as-directory archive-tmpdir)))
  918.      (default-directory (file-name-as-directory top)))
  919.     (unwind-protect
  920.         (progn
  921.           (make-directory (file-name-directory tmpfile) t)
  922.       (write-region (point-min) (point-max) tmpfile nil 'nomessage)
  923.       (if (aref descr 3)
  924.           ;; Set the file modes, but make sure we can read it.
  925.           (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
  926.           (let ((exitcode (apply 'call-process
  927.                                  (car command)
  928.                                  nil
  929.                                  nil
  930.                                  nil
  931.                                  (append (cdr command) (list archive ename)))))
  932.             (if (equal exitcode 0)
  933.                 nil
  934.               (error "Updating was unsuccessful (%S)" exitcode))))
  935.       (archive-delete-local tmpfile))))
  936.  
  937. (defun archive-write-file ()
  938.   (save-excursion
  939.     (write-region archive-proper-file-start (point-max) buffer-file-name nil t)
  940.     (set-buffer-modified-p nil)
  941.     t))
  942. ;; -------------------------------------------------------------------------
  943. ;; Section: Marking and unmarking.
  944.  
  945. (defun archive-flag-deleted (p &optional type)
  946.   "In archive mode, mark this member to be deleted from the archive.
  947. With a prefix argument, mark that many files."
  948.   (interactive "p")
  949.   (or type (setq type ?D))
  950.   (beginning-of-line)
  951.   (let ((sign (if (>= p 0) +1 -1))
  952.     (modified (buffer-modified-p))
  953.         buffer-read-only)
  954.     (while (not (zerop p))
  955.       (if (archive-get-descr t)
  956.           (progn
  957.             (delete-char 1)
  958.             (insert type)))
  959.       (forward-line sign)
  960.       (setq p (- p sign)))
  961.     (set-buffer-modified-p modified))
  962.   (archive-next-line 0))
  963.  
  964. (defun archive-unflag (p)
  965.   "In archive mode, un-mark this member if it is marked to be deleted.
  966. With a prefix argument, un-mark that many files forward."
  967.   (interactive "p")
  968.   (archive-flag-deleted p ? ))
  969.  
  970. (defun archive-unflag-backwards (p)
  971.   "In archive mode, un-mark this member if it is marked to be deleted.
  972. With a prefix argument, un-mark that many members backward."
  973.   (interactive "p")
  974.   (archive-flag-deleted (- p) ? ))
  975.  
  976. (defun archive-unmark-all-files ()
  977.   "Remove all marks."
  978.   (interactive)
  979.   (let ((modified (buffer-modified-p))
  980.     buffer-read-only)
  981.     (save-excursion
  982.       (goto-char archive-file-list-start)
  983.       (while (< (point) archive-file-list-end)
  984.         (or (= (following-char) ? )
  985.             (progn (delete-char 1) (insert ? )))
  986.         (forward-line 1)))
  987.     (set-buffer-modified-p modified)))
  988.  
  989. (defun archive-mark (p)
  990.   "In archive mode, mark this member for group operations.
  991. With a prefix argument, mark that many members.
  992. Use \\[archive-unmark-all-files] to remove all marks."
  993.   (interactive "p")
  994.   (archive-flag-deleted p ?*))
  995.  
  996. (defun archive-get-marked (mark &optional default)
  997.   (let (files)
  998.     (save-excursion
  999.       (goto-char archive-file-list-start)
  1000.       (while (< (point) archive-file-list-end)
  1001.         (if (= (following-char) mark)
  1002.         (setq files (cons (archive-get-descr) files)))
  1003.         (forward-line 1)))
  1004.     (or (nreverse files)
  1005.     (and default
  1006.          (list (archive-get-descr))))))
  1007. ;; -------------------------------------------------------------------------
  1008. ;; Section: Operate
  1009.  
  1010. (defun archive-next-line (p)
  1011.   (interactive "p")
  1012.   (forward-line p)
  1013.   (or (eobp)
  1014.       (forward-char archive-file-name-indent)))
  1015.  
  1016. (defun archive-previous-line (p)
  1017.   (interactive "p")
  1018.   (archive-next-line (- p)))
  1019.  
  1020. (defun archive-chmod-entry (new-mode)
  1021.   "Change the protection bits associated with all marked or this member
  1022. in the archive.\n\
  1023. The new protection bits can either be specified as an octal number or
  1024. as a relative change like \"g+rw\" as for chmod(2)"
  1025.   (interactive "sNew mode (octal or relative): ")
  1026.   (if archive-read-only (error "Archive is read-only"))
  1027.   (let ((func (archive-name "chmod-entry")))
  1028.     (if (fboundp func)
  1029.     (progn
  1030.       (funcall func new-mode (archive-get-marked ?* t))
  1031.       (archive-resummarize))
  1032.       (error "Setting mode bits is not supported for this archive type"))))
  1033.  
  1034. (defun archive-chown-entry (new-uid)
  1035.   "Change the owner of all marked or this member."
  1036.   (interactive "nNew uid: ")
  1037.   (if archive-read-only (error "Archive is read-only"))
  1038.   (let ((func (archive-name "chown-entry")))
  1039.     (if (fboundp func)
  1040.     (progn
  1041.       (funcall func new-uid (archive-get-marked ?* t))
  1042.       (archive-resummarize))
  1043.       (error "Setting owner is not supported for this archive type"))))
  1044.  
  1045. (defun archive-chgrp-entry (new-gid)
  1046.   "Change the group of all marked or this member."
  1047.   (interactive "nNew gid: ")
  1048.   (if archive-read-only (error "Archive is read-only"))
  1049.   (let ((func (archive-name "chgrp-entry")))
  1050.     (if (fboundp func)
  1051.     (progn
  1052.       (funcall func new-gid (archive-get-marked ?* t))
  1053.       (archive-resummarize))
  1054.       (error "Setting group is not supported for this archive type"))))
  1055.  
  1056. (defun archive-expunge ()
  1057.   "Do the flagged deletions."
  1058.   (interactive)
  1059.   (let (files)
  1060.     (save-excursion
  1061.       (goto-char archive-file-list-start)
  1062.       (while (< (point) archive-file-list-end)
  1063.         (if (= (following-char) ?D)
  1064.         (setq files (cons (aref (archive-get-descr) 0) files)))
  1065.         (forward-line 1)))
  1066.     (setq files (nreverse files))
  1067.     (and files
  1068.      (or (not archive-read-only)
  1069.          (error "Archive is read-only"))
  1070.      (or (yes-or-no-p (format "Really delete %d member%s? "
  1071.                   (length files)
  1072.                   (if (null (cdr files)) "" "s")))
  1073.          (error "Operation aborted"))
  1074.      (let ((archive (archive-maybe-copy (buffer-file-name)))
  1075.            (expunger (archive-name "expunge")))
  1076.        (if (fboundp expunger)
  1077.            (funcall expunger archive files)
  1078.          (archive-*-expunge archive files (symbol-value expunger)))
  1079.        (archive-maybe-update nil)
  1080.        (if archive-remote
  1081.            (archive-resummarize)
  1082.          (revert-buffer))))))
  1083.  
  1084. (defun archive-*-expunge (archive files command)
  1085.   (apply 'call-process
  1086.      (car command)
  1087.      nil
  1088.      nil
  1089.      nil
  1090.      (append (cdr command) (cons archive files))))
  1091.  
  1092. (defun archive-rename-entry (newname)
  1093.   "Change the name associated with this entry in the tar file."
  1094.   (interactive "sNew name: ")
  1095.   (if archive-read-only (error "Archive is read-only"))
  1096.   (if (string= newname "")
  1097.       (error "Archive members may not be given empty names"))
  1098.   (let ((func (archive-name "rename-entry"))
  1099.     (descr (archive-get-descr)))
  1100.     (if (fboundp func)
  1101.         (progn
  1102.       (funcall func (buffer-file-name) newname descr)
  1103.       (archive-resummarize))
  1104.       (error "Renaming is not supported for this archive type"))))
  1105.  
  1106. ;; Revert the buffer and recompute the dired-like listing.
  1107. (defun archive-mode-revert (&optional no-autosave no-confirm)
  1108.   (let ((no (archive-get-lineno)))
  1109.     (setq archive-files nil)
  1110.     (let ((revert-buffer-function nil))
  1111.       (revert-buffer t t))
  1112.     (archive-mode)
  1113.     (goto-char archive-file-list-start)
  1114.     (archive-next-line no)))
  1115.  
  1116. (defun archive-undo ()
  1117.   "Undo in an archive buffer.
  1118. This doesn't recover lost files, it just undoes changes in the buffer itself."
  1119.   (interactive)
  1120.   (let (buffer-read-only)
  1121.     (undo)))
  1122. ;; -------------------------------------------------------------------------
  1123. ;; Section: Arc Archives
  1124.  
  1125. (defun archive-arc-summarize ()
  1126.   (let ((p 1)
  1127.     (totalsize 0)
  1128.     (maxlen 8)
  1129.         files
  1130.     visual)
  1131.     (while (and (< (+ p 29) (point-max))
  1132.         (= (char-after p) ?\C-z)
  1133.         (> (char-after (1+ p)) 0))
  1134.       (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
  1135.          (fnlen   (or (string-match "\0" namefld) 13))
  1136.          (efnname (substring namefld 0 fnlen))
  1137.              (csize   (archive-l-e (+ p 15) 4))
  1138.              (moddate (archive-l-e (+ p 19) 2))
  1139.              (modtime (archive-l-e (+ p 21) 2))
  1140.              (ucsize  (archive-l-e (+ p 25) 4))
  1141.          (fiddle  (string= efnname (upcase efnname)))
  1142.              (ifnname (if fiddle (downcase efnname) efnname))
  1143.              (text    (format "  %8d  %-11s  %-8s  %s"
  1144.                               ucsize
  1145.                               (archive-dosdate moddate)
  1146.                               (archive-dostime modtime)
  1147.                               ifnname)))
  1148.         (setq maxlen (max maxlen fnlen)
  1149.           totalsize (+ totalsize ucsize)
  1150.           visual (cons (vector text
  1151.                    (- (length text) (length ifnname))
  1152.                    (length text))
  1153.                visual)
  1154.           files (cons (vector efnname ifnname fiddle nil (1- p))
  1155.                           files)
  1156.               p (+ p 29 csize))))
  1157.     (goto-char (point-min))
  1158.     (let ((dash (concat "- --------  -----------  --------  "
  1159.             (make-string maxlen ?-)
  1160.             "\n")))
  1161.       (insert "M   Length  Date         Time      File\n"
  1162.           dash)
  1163.       (archive-summarize-files (nreverse visual))
  1164.       (insert dash
  1165.           (format "  %8d                         %d file%s"
  1166.               totalsize
  1167.               (length files)
  1168.               (if (= 1 (length files)) "" "s"))
  1169.           "\n"))
  1170.     (apply 'vector (nreverse files))))
  1171.  
  1172. (defun archive-arc-rename-entry (archive newname descr)
  1173.   (if (string-match "[:\\\\/]" newname)
  1174.       (error "File names in arc files may not contain a path"))
  1175.   (if (> (length newname) 12)
  1176.       (error "File names in arc files are limited to 12 characters"))
  1177.   (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
  1178.                      (length newname))))
  1179.     buffer-read-only)
  1180.     (save-restriction
  1181.       (save-excursion
  1182.     (widen)
  1183.     (goto-char (+ archive-proper-file-start (aref descr 4) 2))
  1184.     (delete-char 13)
  1185.     (insert name)))))
  1186. ;; -------------------------------------------------------------------------
  1187. ;; Section: Lzh Archives
  1188.  
  1189. (defun archive-lzh-summarize ()
  1190.   (let ((p 1)
  1191.     (totalsize 0)
  1192.     (maxlen 8)
  1193.         files
  1194.     visual)
  1195.     (while (progn (goto-char p) (looking-at "..-l[hz][0-9]-"))
  1196.       (let* ((hsize   (char-after p))
  1197.              (csize   (archive-l-e (+ p 7) 4))
  1198.              (ucsize  (archive-l-e (+ p 11) 4))
  1199.          (modtime (archive-l-e (+ p 15) 2))
  1200.          (moddate (archive-l-e (+ p 17) 2))
  1201.          (fnlen   (char-after (+ p 21)))
  1202.          (efnname (buffer-substring (+ p 22) (+ p 22 fnlen)))
  1203.          (fiddle  (string= efnname (upcase efnname)))
  1204.              (ifnname (if fiddle (downcase efnname) efnname))
  1205.          (p2      (+ p 22 fnlen))
  1206.          (creator (if (>= (- hsize fnlen) 24) (char-after (+ p2 2)) 0))
  1207.          (mode    (if (= creator ?U) (archive-l-e (+ p2 8) 2) ?\666))
  1208.          (modestr (if mode (archive-int-to-mode mode) "??????????"))
  1209.          (uid     (if (= creator ?U) (archive-l-e (+ p2 10) 2)))
  1210.          (gid     (if (= creator ?U) (archive-l-e (+ p2 12) 2)))
  1211.          (text    (if archive-alternate-display
  1212.               (format "  %8d  %5S  %5S  %s"
  1213.                   ucsize
  1214.                   (or uid "?")
  1215.                   (or gid "?")
  1216.                   ifnname)
  1217.             (format "  %10s  %8d  %-11s  %-8s  %s"
  1218.                 modestr
  1219.                 ucsize
  1220.                 (archive-dosdate moddate)
  1221.                 (archive-dostime modtime)
  1222.                 ifnname))))
  1223.         (setq maxlen (max maxlen fnlen)
  1224.           totalsize (+ totalsize ucsize)
  1225.           visual (cons (vector text
  1226.                    (- (length text) (length ifnname))
  1227.                    (length text))
  1228.                visual)
  1229.           files (cons (vector efnname ifnname fiddle mode (1- p))
  1230.                           files)
  1231.               p (+ p hsize 2 csize))))
  1232.     (goto-char (point-min))
  1233.     (let ((dash (concat (if archive-alternate-display
  1234.                 "- --------  -----  -----  "
  1235.               "- ----------  --------  -----------  --------  ")
  1236.             (make-string maxlen ?-)
  1237.             "\n"))
  1238.       (header (if archive-alternate-display
  1239.                "M   Length    Uid    Gid  File\n"
  1240.             "M   Filemode    Length  Date         Time      File\n"))
  1241.       (sumline (if archive-alternate-display
  1242.                "  %8d                %d file%s"
  1243.              "              %8d                         %d file%s")))
  1244.       (insert header dash)
  1245.       (archive-summarize-files (nreverse visual))
  1246.       (insert dash
  1247.           (format sumline
  1248.               totalsize
  1249.               (length files)
  1250.               (if (= 1 (length files)) "" "s"))
  1251.           "\n"))
  1252.     (apply 'vector (nreverse files))))
  1253.  
  1254. (defconst archive-lzh-alternate-display t)
  1255.  
  1256. (defun archive-lzh-extract (archive name)
  1257.   (archive-extract-by-stdout archive name archive-lzh-extract))
  1258.  
  1259. (defun archive-lzh-resum (p count)
  1260.   (let ((sum 0))
  1261.     (while (> count 0)
  1262.       (setq count (1- count)
  1263.         sum (+ sum (char-after p))
  1264.         p (1+ p)))
  1265.     (logand sum 255)))
  1266.  
  1267. (defun archive-lzh-rename-entry (archive newname descr)
  1268.   (save-restriction
  1269.     (save-excursion
  1270.       (widen)
  1271.       (let* ((p        (+ archive-proper-file-start (aref descr 4)))
  1272.          (oldhsize (char-after p))
  1273.          (oldfnlen (char-after (+ p 21)))
  1274.          (newfnlen (length newname))
  1275.          (newhsize (+ oldhsize newfnlen (- oldfnlen)))
  1276.          buffer-read-only)
  1277.     (if (> newhsize 255)
  1278.         (error "The file name is too long"))
  1279.     (goto-char (+ p 21))
  1280.     (delete-char (1+ oldfnlen))
  1281.     (insert newfnlen newname)
  1282.     (goto-char p)
  1283.     (delete-char 2)
  1284.     (insert newhsize (archive-lzh-resum p newhsize))))))
  1285.  
  1286. (defun archive-lzh-ogm (newval files errtxt ofs)
  1287.   (save-restriction
  1288.     (save-excursion
  1289.       (widen)
  1290.       (while files
  1291.     (let* ((fil (car files))
  1292.            (p (+ archive-proper-file-start (aref fil 4)))
  1293.            (hsize   (char-after p))
  1294.            (fnlen   (char-after (+ p 21)))
  1295.            (p2      (+ p 22 fnlen))
  1296.            (creator (if (>= (- hsize fnlen) 24) (char-after (+ p2 2)) 0))
  1297.            buffer-read-only)
  1298.       (if (= creator ?U)
  1299.           (progn
  1300.         (or (numberp newval)
  1301.             (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
  1302.         (goto-char (+ p2 ofs))
  1303.         (delete-char 2)
  1304.         (insert (logand newval 255) (lsh newval -8))
  1305.         (goto-char (1+ p))
  1306.         (delete-char 1)
  1307.         (insert (archive-lzh-resum (1+ p) hsize)))
  1308.         (message "Member %s does not have %s field"
  1309.              (aref fil 1) errtxt)))
  1310.     (setq files (cdr files))))))
  1311.  
  1312. (defun archive-lzh-chown-entry (newuid files)
  1313.   (archive-lzh-ogm newuid files "an uid" 10))
  1314.  
  1315. (defun archive-lzh-chgrp-entry (newgid files)
  1316.   (archive-lzh-ogm newgid files "a gid" 12))
  1317.  
  1318. (defun archive-lzh-chmod-entry (newmode files)
  1319.   (archive-lzh-ogm
  1320.    ;; This should work even though newmode will be dynamically accessed.
  1321.    (function (lambda (old) (archive-calc-mode old newmode t)))
  1322.    files "a unix-style mode" 8))
  1323. ;; -------------------------------------------------------------------------
  1324. ;; Section: Zip Archives
  1325.  
  1326. (defun archive-zip-summarize ()
  1327.   (goto-char (- (point-max) (- 22 18)))
  1328.   (search-backward-regexp "[P]K\005\006")
  1329.   (let ((p (1+ (archive-l-e (+ (point) 16) 4)))
  1330.         (maxlen 8)
  1331.     (totalsize 0)
  1332.         files
  1333.     visual)
  1334.     (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
  1335.       (let* ((creator (char-after (+ p 5)))
  1336.          (method  (archive-l-e (+ p 10) 2))
  1337.              (modtime (archive-l-e (+ p 12) 2))
  1338.              (moddate (archive-l-e (+ p 14) 2))
  1339.              (ucsize  (archive-l-e (+ p 24) 4))
  1340.              (fnlen   (archive-l-e (+ p 28) 2))
  1341.              (exlen   (archive-l-e (+ p 30) 2))
  1342.              (fclen   (archive-l-e (+ p 32) 2))
  1343.              (lheader (archive-l-e (+ p 42) 4))
  1344.              (efnname (buffer-substring (+ p 46) (+ p 46 fnlen)))
  1345.          (isdir   (and (= ucsize 0)
  1346.                (string= (file-name-nondirectory efnname) "")))
  1347.          (mode    (cond ((memq creator '(2 3)) ; Unix + VMS
  1348.                  (archive-l-e (+ p 40) 2))
  1349.                 ((memq creator '(0 5 6 7 10 11)) ; Dos etc.
  1350.                  (logior ?\444
  1351.                      (if isdir (logior 16384 ?\111) 0)
  1352.                      (if (zerop
  1353.                       (logand 1 (char-after (+ p 38))))
  1354.                      ?\222 0)))
  1355.                 (t nil)))
  1356.          (modestr (if mode (archive-int-to-mode mode) "??????????"))
  1357.          (fiddle  (and archive-zip-case-fiddle
  1358.                (not (not (memq creator '(0 2 4 5 9))))))
  1359.              (ifnname (if fiddle (downcase efnname) efnname))
  1360.              (text    (format "  %10s  %8d  %-11s  %-8s  %s"
  1361.                   modestr
  1362.                               ucsize
  1363.                               (archive-dosdate moddate)
  1364.                               (archive-dostime modtime)
  1365.                               ifnname)))
  1366.         (setq maxlen (max maxlen fnlen)
  1367.           totalsize (+ totalsize ucsize)
  1368.           visual (cons (vector text
  1369.                    (- (length text) (length ifnname))
  1370.                    (length text))
  1371.                visual)
  1372.           files (cons (if isdir
  1373.                   nil
  1374.                 (vector efnname ifnname fiddle mode
  1375.                     (list (1- p) lheader)))
  1376.                           files)
  1377.               p (+ p 46 fnlen exlen fclen))))
  1378.     (goto-char (point-min))
  1379.     (let ((dash (concat "- ----------  --------  -----------  --------  "
  1380.             (make-string maxlen ?-)
  1381.             "\n")))
  1382.       (insert "M Filemode      Length  Date         Time      File\n"
  1383.           dash)
  1384.       (archive-summarize-files (nreverse visual))
  1385.       (insert dash
  1386.           (format "              %8d                         %d file%s"
  1387.               totalsize
  1388.               (length files)
  1389.               (if (= 1 (length files)) "" "s"))
  1390.           "\n"))
  1391.     (apply 'vector (nreverse files))))
  1392.  
  1393. (defun archive-zip-extract (archive name)
  1394.   (if archive-zip-use-pkzip
  1395.       (archive-*-extract archive name archive-zip-extract)
  1396.     (archive-extract-by-stdout archive name archive-zip-extract)))
  1397.  
  1398. (defun archive-zip-write-file-member (archive descr)
  1399.   (archive-*-write-file-member
  1400.    archive
  1401.    descr
  1402.    (if (aref descr 2) archive-zip-update-case archive-zip-update)))
  1403.  
  1404. (defun archive-zip-chmod-entry (newmode files)
  1405.   (save-restriction
  1406.     (save-excursion
  1407.       (widen)
  1408.       (while files
  1409.     (let* ((fil (car files))
  1410.            (p (+ archive-proper-file-start (car (aref fil 4))))
  1411.            (creator (char-after (+ p 5)))
  1412.            (oldmode (aref fil 3))
  1413.            (newval  (archive-calc-mode oldmode newmode t))
  1414.            buffer-read-only)
  1415.       (cond ((memq creator '(2 3)) ; Unix + VMS
  1416.          (goto-char (+ p 40))
  1417.          (delete-char 2)
  1418.          (insert (logand newval 255) (lsh newval -8)))
  1419.         ((memq creator '(0 5 6 7 10 11)) ; Dos etc.
  1420.          (goto-char (+ p 38))
  1421.          (insert (logior (logand (char-after (point)) 254)
  1422.                  (logand (logxor 1 (lsh newval -7)) 1)))
  1423.          (delete-char 1))
  1424.         (t (message "Don't know how to change mode for this member"))))
  1425.     (setq files (cdr files))))))
  1426. ;; -------------------------------------------------------------------------
  1427. ;; Section: Zoo Archives
  1428.  
  1429. (defun archive-zoo-summarize ()
  1430.   (let ((p (1+ (archive-l-e 25 4)))
  1431.         (maxlen 8)
  1432.     (totalsize 0)
  1433.         files
  1434.     visual)
  1435.     (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
  1436.         (> (archive-l-e (+ p 6) 4) 0))
  1437.       (let* ((next    (1+ (archive-l-e (+ p 6) 4)))
  1438.              (moddate (archive-l-e (+ p 14) 2))
  1439.              (modtime (archive-l-e (+ p 16) 2))
  1440.              (ucsize  (archive-l-e (+ p 20) 4))
  1441.          (namefld (buffer-substring (+ p 38) (+ p 38 13)))
  1442.          (fnlen   (or (string-match "\0" namefld) 13))
  1443.          (efnname (substring namefld 0 fnlen))
  1444.          (fiddle  (string= efnname (upcase efnname)))
  1445.              (ifnname (if fiddle (downcase efnname) efnname))
  1446.              (text    (format "  %8d  %-11s  %-8s  %s"
  1447.                               ucsize
  1448.                               (archive-dosdate moddate)
  1449.                               (archive-dostime modtime)
  1450.                               ifnname)))
  1451.         (setq maxlen (max maxlen fnlen)
  1452.           totalsize (+ totalsize ucsize)
  1453.           visual (cons (vector text
  1454.                    (- (length text) (length ifnname))
  1455.                    (length text))
  1456.                visual)
  1457.           files (cons (vector efnname ifnname fiddle nil (1- p))
  1458.                           files)
  1459.               p next)))
  1460.     (goto-char (point-min))
  1461.     (let ((dash (concat "- --------  -----------  --------  "
  1462.             (make-string maxlen ?-)
  1463.             "\n")))
  1464.       (insert "M   Length  Date         Time      File\n"
  1465.           dash)
  1466.       (archive-summarize-files (nreverse visual))
  1467.       (insert dash
  1468.           (format "  %8d                         %d file%s"
  1469.               totalsize
  1470.               (length files)
  1471.               (if (= 1 (length files)) "" "s"))
  1472.           "\n"))
  1473.     (apply 'vector (nreverse files))))
  1474.  
  1475. (defun archive-zoo-extract (archive name)
  1476.   (archive-extract-by-stdout archive name archive-zoo-extract))
  1477. ;; -------------------------------------------------------------------------
  1478. (provide 'archive-mode)
  1479.  
  1480. ;; arc-mode.el ends here.
  1481.