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