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