0
|
1 ;;; tar-mode.el --- simple editing of tar files from GNU emacs
|
|
2 ;; Keywords: unix
|
|
3
|
|
4 ;;; File: tar-mode.el
|
|
5 ;;; Description: simple editing of tar files from GNU emacs
|
|
6 ;;; Author: Jamie Zawinski <jwz@lucid.com>
|
|
7 ;;; Created: 4 Apr 1990
|
|
8 ;;; Version: 1.31, 15 Dec 93
|
|
9
|
|
10 ;;; Copyright (C) 1990-1993 Free Software Foundation, Inc.
|
|
11 ;;;
|
|
12 ;; This file is part of XEmacs.
|
|
13
|
|
14 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
15 ;; under the terms of the GNU General Public License as published by
|
|
16 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
17 ;; any later version.
|
|
18
|
|
19 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
22 ;; General Public License for more details.
|
|
23
|
|
24 ;; You should have received a copy of the GNU General Public License
|
16
|
25 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
27 ;; Boston, MA 02111-1307, USA.
|
0
|
28
|
|
29 ;;; Synched up with: Not synched with FSF.
|
|
30
|
|
31 ;;; This package attempts to make dealing with Unix 'tar' archives easier.
|
|
32 ;;; When this code is loaded, visiting a file whose name ends in '.tar' will
|
|
33 ;;; cause the contents of that archive file to be displayed in a Dired-like
|
|
34 ;;; listing. It is then possible to use the customary Dired keybindings to
|
|
35 ;;; extract sub-files from that archive, either by reading them into their own
|
|
36 ;;; editor buffers, or by copying them directly to arbitrary files on disk.
|
|
37 ;;; It is also possible to delete sub-files from within the tar file and write
|
|
38 ;;; the modified archive back to disk, or to edit sub-files within the archive
|
|
39 ;;; and re-insert the modified files into the archive. See the documentation
|
|
40 ;;; string of tar-mode for more info.
|
|
41
|
|
42 ;;; To autoload, add this to your .emacs file:
|
|
43 ;;;
|
|
44 ;;; (setq auto-mode-alist (cons '("\\.tar$" . tar-mode) auto-mode-alist))
|
|
45 ;;; (autoload 'tar-mode "tar-mode")
|
|
46 ;;;
|
|
47 ;;; But beware: for certain tar files - those whose very first file has
|
|
48 ;;; a -*- property line - autoloading won't work. See the function
|
|
49 ;;; "tar-normal-mode" to understand why.
|
|
50
|
|
51 ;;; This code now understands the extra fields that GNU tar adds to tar files.
|
|
52
|
|
53 ;;; This interacts correctly with "uncompress.el" in the Emacs library,
|
|
54 ;;; and with sufficiently recent versions of "crypt.el" by Kyle Jones.
|
|
55
|
|
56 ;;; *************** TO DO ***************
|
|
57 ;;;
|
|
58 ;;; o There should be a command to extract the whole current buffer into files
|
|
59 ;;; on disk (right now you have to do the subfiles one at a time.)
|
|
60 ;;;
|
|
61 ;;; o chmod should understand "a+x,og-w".
|
|
62 ;;;
|
|
63 ;;; o It's not possible to add a NEW file to a tar archive; not that
|
|
64 ;;; important, but still...
|
|
65 ;;;
|
|
66 ;;; o The code is less efficient that it could be - in a lot of places, I
|
|
67 ;;; pull a 512-character string out of the buffer and parse it, when I could
|
|
68 ;;; be parsing it in place, not garbaging a string. Should redo that.
|
|
69 ;;;
|
|
70 ;;; o I'd like a command that searches for a string/regexp in every subfile
|
|
71 ;;; of an archive, where <esc> would leave you in a subfile-edit buffer.
|
|
72 ;;; (Like M-s in VM and M-r in the Zmacs mail reader.)
|
|
73 ;;;
|
|
74 ;;; o Sometimes (but not always) reverting the tar-file buffer does not
|
|
75 ;;; re-grind the listing, and you are staring at the binary tar data.
|
|
76 ;;; Typing 'g' again immediately after that will always revert and re-grind
|
|
77 ;;; it, though. I have no idea why this happens.
|
|
78 ;;;
|
|
79 ;;; o If you edit a subfile, and selective-display gets set to t, then when
|
|
80 ;;; we save the subfile, we should map ^M -> ^J.
|
|
81 ;;;
|
|
82 ;;; o Block files, sparse files, continuation files, and the various header
|
|
83 ;;; types aren't editable. Actually I don't know that they work at all.
|
|
84 ;;; If you know that they work, or know that they don't, please let me know.
|
|
85 ;;;
|
|
86 ;;; o Tar files inside of tar files don't work.
|
|
87 ;;;
|
|
88 ;;; o When using crypt-mode, you can't save a compressed or encrypted subfile
|
|
89 ;;; of a tar file back into the tar file: it is saved uncompressed.
|
|
90
|
|
91 (defvar tar-anal-blocksize 20
|
|
92 "*The blocksize of tar files written by Emacs, or nil, meaning don't care.
|
|
93 The blocksize of a tar file is not really the size of the blocks; rather, it is
|
|
94 the number of blocks written with one system call. When tarring to a tape,
|
|
95 this is the size of the *tape* blocks, but when writing to a file, it doesn't
|
|
96 matter much. The only noticeable difference is that if a tar file does not
|
|
97 have a blocksize of 20, the tar program will issue a warning; all this really
|
|
98 controls is how many null padding bytes go on the end of the tar file.")
|
|
99
|
|
100 (defvar tar-update-datestamp (or (fboundp 'current-time)
|
|
101 (fboundp 'current-time-seconds))
|
|
102 "*Whether tar-mode should play fast and loose with sub-file datestamps;
|
|
103 if this is true, then editing and saving a tar file entry back into its
|
|
104 tar file will update its datestamp. If false, the datestamp is unchanged.
|
|
105 You may or may not want this - it is good in that you can tell when a file
|
|
106 in a tar archive has been changed, but it is bad for the same reason that
|
|
107 editing a file in the tar archive at all is bad - the changed version of
|
|
108 the file never exists on disk.
|
|
109
|
|
110 This does not work in Emacs 18, because there's no way to get the current
|
|
111 time as an integer - if this var is true, then editing a file sets its date
|
|
112 to midnight, Jan 1 1970 GMT, which happens to be what 0 encodes.")
|
|
113
|
|
114
|
|
115 ;;; First, duplicate some Common Lisp functions; I used to just (require 'cl)
|
|
116 ;;; but "cl.el" was messing some people up (also it's really big).
|
|
117
|
|
118 (defmacro tar-setf (form val)
|
|
119 "A mind-numbingly simple implementation of setf."
|
|
120 (let ((mform (macroexpand form (and (boundp 'byte-compile-macro-environment)
|
|
121 byte-compile-macro-environment))))
|
|
122 (cond ((symbolp mform) (list 'setq mform val))
|
|
123 ((not (consp mform)) (error "can't setf %s" form))
|
|
124 ((eq (car mform) 'aref)
|
|
125 (list 'aset (nth 1 mform) (nth 2 mform) val))
|
|
126 ((eq (car mform) 'car)
|
|
127 (list 'setcar (nth 1 mform) val))
|
|
128 ((eq (car mform) 'cdr)
|
|
129 (list 'setcdr (nth 1 mform) val))
|
|
130 (t (error "don't know how to setf %s" form)))))
|
|
131
|
|
132 (defmacro tar-dolist (control &rest body)
|
|
133 "syntax: (dolist (var-name list-expr &optional return-value) &body body)"
|
|
134 (let ((var (car control))
|
|
135 (init (car (cdr control)))
|
|
136 (val (car (cdr (cdr control)))))
|
|
137 (list 'let (list (list '_dolist_iterator_ init))
|
|
138 (list 'while '_dolist_iterator_
|
|
139 (cons 'let
|
|
140 (cons (list (list var '(car _dolist_iterator_)))
|
|
141 (append body
|
|
142 (list (list 'setq '_dolist_iterator_
|
|
143 (list 'cdr '_dolist_iterator_)))))))
|
|
144 val)))
|
|
145
|
|
146 (defmacro tar-dotimes (control &rest body)
|
|
147 "syntax: (dotimes (var-name count-expr &optional return-value) &body body)"
|
|
148 (let ((var (car control))
|
|
149 (n (car (cdr control)))
|
|
150 (val (car (cdr (cdr control)))))
|
|
151 (list 'let (list (list '_dotimes_end_ n)
|
|
152 (list var 0))
|
|
153 (cons 'while
|
|
154 (cons (list '< var '_dotimes_end_)
|
|
155 (append body
|
|
156 (list (list 'setq var (list '1+ var))))))
|
|
157 val)))
|
|
158
|
|
159
|
|
160 ;;; down to business.
|
|
161
|
|
162 (defmacro make-tar-header (name mode uid git size date ck lt ln
|
|
163 magic uname gname devmaj devmin)
|
|
164 (list 'vector name mode uid git size date ck lt ln
|
|
165 magic uname gname devmaj devmin))
|
|
166
|
|
167 (defmacro tar-header-name (x) (list 'aref x 0))
|
|
168 (defmacro tar-header-mode (x) (list 'aref x 1))
|
|
169 (defmacro tar-header-uid (x) (list 'aref x 2))
|
|
170 (defmacro tar-header-gid (x) (list 'aref x 3))
|
|
171 (defmacro tar-header-size (x) (list 'aref x 4))
|
|
172 (defmacro tar-header-date (x) (list 'aref x 5))
|
|
173 (defmacro tar-header-checksum (x) (list 'aref x 6))
|
|
174 (defmacro tar-header-link-type (x) (list 'aref x 7))
|
|
175 (defmacro tar-header-link-name (x) (list 'aref x 8))
|
|
176 (defmacro tar-header-magic (x) (list 'aref x 9))
|
|
177 (defmacro tar-header-uname (x) (list 'aref x 10))
|
|
178 (defmacro tar-header-gname (x) (list 'aref x 11))
|
|
179 (defmacro tar-header-dmaj (x) (list 'aref x 12))
|
|
180 (defmacro tar-header-dmin (x) (list 'aref x 13))
|
|
181
|
|
182 (defmacro make-tar-desc (data-start tokens)
|
|
183 (list 'cons data-start tokens))
|
|
184
|
|
185 (defmacro tar-desc-data-start (x) (list 'car x))
|
|
186 (defmacro tar-desc-tokens (x) (list 'cdr x))
|
|
187
|
|
188 (defconst tar-name-offset 0)
|
|
189 (defconst tar-mode-offset (+ tar-name-offset 100))
|
|
190 (defconst tar-uid-offset (+ tar-mode-offset 8))
|
|
191 (defconst tar-gid-offset (+ tar-uid-offset 8))
|
|
192 (defconst tar-size-offset (+ tar-gid-offset 8))
|
|
193 (defconst tar-time-offset (+ tar-size-offset 12))
|
|
194 (defconst tar-chk-offset (+ tar-time-offset 12))
|
|
195 (defconst tar-linkp-offset (+ tar-chk-offset 8))
|
|
196 (defconst tar-link-offset (+ tar-linkp-offset 1))
|
|
197 ;;; GNU-tar specific slots.
|
|
198 (defconst tar-magic-offset (+ tar-link-offset 100))
|
|
199 (defconst tar-uname-offset (+ tar-magic-offset 8))
|
|
200 (defconst tar-gname-offset (+ tar-uname-offset 32))
|
|
201 (defconst tar-dmaj-offset (+ tar-gname-offset 32))
|
|
202 (defconst tar-dmin-offset (+ tar-dmaj-offset 8))
|
|
203 (defconst tar-end-offset (+ tar-dmin-offset 8))
|
|
204
|
|
205 (defun tokenize-tar-header-block (string)
|
|
206 "Returns a 'tar-header' structure (a list of name, mode, uid, gid, size,
|
|
207 write-date, checksum, link-type, and link-name)."
|
|
208 (cond ((< (length string) 512) nil)
|
|
209 (;(some 'plusp string) ; <-- oops, massive cycle hog!
|
|
210 (or (not (= 0 (aref string 0))) ; This will do.
|
|
211 (not (= 0 (aref string 101))))
|
|
212 (let* ((name-end (1- tar-mode-offset))
|
|
213 (link-end (1- tar-magic-offset))
|
|
214 (uname-end (1- tar-gname-offset))
|
|
215 (gname-end (1- tar-dmaj-offset))
|
|
216 (link-p (aref string tar-linkp-offset))
|
|
217 (magic-str (substring string tar-magic-offset (1- tar-uname-offset)))
|
|
218 (uname-valid-p (or (string= "ustar " magic-str) (string= "GNUtar " magic-str)))
|
|
219 name
|
|
220 (nulsexp "[^\000]*\000"))
|
|
221 (and (string-match nulsexp string tar-name-offset) (setq name-end (min name-end (1- (match-end 0)))))
|
|
222 (and (string-match nulsexp string tar-link-offset) (setq link-end (min link-end (1- (match-end 0)))))
|
|
223 (and (string-match nulsexp string tar-uname-offset) (setq uname-end (min uname-end (1- (match-end 0)))))
|
|
224 (and (string-match nulsexp string tar-gname-offset) (setq gname-end (min gname-end (1- (match-end 0)))))
|
|
225 (setq name (substring string tar-name-offset name-end)
|
|
226 link-p (if (or (= link-p 0) (= link-p ?0))
|
|
227 nil
|
|
228 (- link-p ?0)))
|
|
229 (if (and (null link-p) (string-match "/$" name)) (setq link-p 5)) ; directory
|
|
230 (make-tar-header
|
|
231 name
|
|
232 (tar-parse-octal-integer string tar-mode-offset (1- tar-uid-offset))
|
|
233 (tar-parse-octal-integer string tar-uid-offset (1- tar-gid-offset))
|
|
234 (tar-parse-octal-integer string tar-gid-offset (1- tar-size-offset))
|
|
235 (tar-parse-octal-integer string tar-size-offset (1- tar-time-offset))
|
|
236 (tar-parse-octal-integer-32 string tar-time-offset (1- tar-chk-offset))
|
|
237 (tar-parse-octal-integer string tar-chk-offset (1- tar-linkp-offset))
|
|
238 link-p
|
|
239 (substring string tar-link-offset link-end)
|
|
240 uname-valid-p
|
|
241 (and uname-valid-p (substring string tar-uname-offset uname-end))
|
|
242 (and uname-valid-p (substring string tar-gname-offset gname-end))
|
|
243 (tar-parse-octal-integer string tar-dmaj-offset (1- tar-dmin-offset))
|
|
244 (tar-parse-octal-integer string tar-dmin-offset (1- tar-end-offset))
|
|
245 )))
|
|
246 (t 'empty-tar-block)))
|
|
247
|
|
248
|
|
249 (defun tar-parse-octal-integer (string &optional start end)
|
|
250 "deletes all your files, and then reboots."
|
|
251 (if (null start) (setq start 0))
|
|
252 (if (null end) (setq end (length string)))
|
|
253 (if (= (aref string start) 0)
|
|
254 0
|
|
255 (let ((n 0))
|
|
256 (while (< start end)
|
|
257 (setq n (if (< (aref string start) ?0) n
|
|
258 (+ (* n 8) (- (aref string start) 48)))
|
|
259 start (1+ start)))
|
|
260 n)))
|
|
261
|
|
262 (defun tar-parse-octal-integer-32 (string &optional start end)
|
|
263 ;; like tar-parse-octal-integer, but returns a cons of two 16-bit numbers,
|
|
264 ;; since elisp can't handle integers of that magnitude.
|
|
265 (or start (setq start 0))
|
|
266 (or end (setq end (length string)))
|
|
267 (let ((top (tar-parse-octal-integer string start (- end 6)))
|
|
268 (bot (tar-parse-octal-integer string (- end 6) end)))
|
|
269 (setq top (logior (ash top 2) (ash bot -16)))
|
|
270 (setq bot (logand bot 65535))
|
|
271 (cons top bot)))
|
|
272
|
|
273 (defun tar-parse-octal-integer-safe (string)
|
|
274 (let ((L (length string)))
|
|
275 (if (= L 0) (error "empty string"))
|
|
276 (tar-dotimes (i L)
|
|
277 (if (or (< (aref string i) ?0)
|
|
278 (> (aref string i) ?7))
|
|
279 (error "'%c' is not an octal digit."))))
|
|
280 (tar-parse-octal-integer string))
|
|
281
|
|
282
|
|
283 (defun checksum-tar-header-block (string)
|
|
284 "Computes and returns a tar-acceptable checksum for this block."
|
|
285 (let* ((chk-field-start tar-chk-offset)
|
|
286 (chk-field-end (+ chk-field-start 8))
|
|
287 (sum 0)
|
|
288 (i 0))
|
|
289 ;; Add up all of the characters except the ones in the checksum field.
|
|
290 ;; Add that field as if it were filled with spaces.
|
|
291 (while (< i chk-field-start)
|
|
292 (setq sum (+ sum (aref string i))
|
|
293 i (1+ i)))
|
|
294 (setq i chk-field-end)
|
|
295 (while (< i 512)
|
|
296 (setq sum (+ sum (aref string i))
|
|
297 i (1+ i)))
|
|
298 (+ sum (* 32 8))))
|
|
299
|
|
300 (defun check-tar-header-block-checksum (hblock desired-checksum file-name)
|
|
301 "Beep and print a warning if the checksum doesn't match."
|
|
302 (if (not (= desired-checksum (checksum-tar-header-block hblock)))
|
|
303 (progn (beep) (message "Invalid checksum for file %s!" file-name))))
|
|
304
|
|
305 (defun recompute-tar-header-block-checksum (hblock)
|
|
306 "Modifies the given string to have a valid checksum field."
|
|
307 (let* ((chk (checksum-tar-header-block hblock))
|
|
308 (chk-string (format "%6o" chk))
|
|
309 (l (length chk-string)))
|
|
310 (aset hblock 154 0)
|
|
311 (aset hblock 155 32)
|
|
312 (tar-dotimes (i l) (aset hblock (- 153 i) (aref chk-string (- l i 1)))))
|
|
313 hblock)
|
|
314
|
|
315
|
|
316 (defun tar-grind-file-mode (mode string start)
|
|
317 "Write a \"-rw--r--r-\" representing MODE into STRING beginning at START."
|
|
318 (aset string start (if (zerop (logand 256 mode)) ?- ?r))
|
|
319 (aset string (+ start 1) (if (zerop (logand 128 mode)) ?- ?w))
|
|
320 (aset string (+ start 2) (if (zerop (logand 64 mode)) ?- ?x))
|
|
321 (aset string (+ start 3) (if (zerop (logand 32 mode)) ?- ?r))
|
|
322 (aset string (+ start 4) (if (zerop (logand 16 mode)) ?- ?w))
|
|
323 (aset string (+ start 5) (if (zerop (logand 8 mode)) ?- ?x))
|
|
324 (aset string (+ start 6) (if (zerop (logand 4 mode)) ?- ?r))
|
|
325 (aset string (+ start 7) (if (zerop (logand 2 mode)) ?- ?w))
|
|
326 (aset string (+ start 8) (if (zerop (logand 1 mode)) ?- ?x))
|
|
327 (if (zerop (logand 1024 mode)) nil (aset string (+ start 2) ?s))
|
|
328 (if (zerop (logand 2048 mode)) nil (aset string (+ start 5) ?s))
|
|
329 string)
|
|
330
|
|
331
|
|
332 (defconst tar-can-print-dates (or (fboundp 'current-time)
|
|
333 (fboundp 'current-time-seconds))
|
|
334 "true if this emacs has been built with time-printing support")
|
|
335
|
|
336 (defun summarize-tar-header-block (tar-hblock &optional mod-p)
|
|
337 "Returns a line similar to the output of 'tar -vtf'."
|
|
338 (let ((name (tar-header-name tar-hblock))
|
|
339 (mode (tar-header-mode tar-hblock))
|
|
340 (uid (tar-header-uid tar-hblock))
|
|
341 (gid (tar-header-gid tar-hblock))
|
|
342 (uname (tar-header-uname tar-hblock))
|
|
343 (gname (tar-header-gname tar-hblock))
|
|
344 (size (tar-header-size tar-hblock))
|
|
345 (time (tar-header-date tar-hblock))
|
|
346 (ck (tar-header-checksum tar-hblock))
|
|
347 (link-p (tar-header-link-type tar-hblock))
|
|
348 (link-name (tar-header-link-name tar-hblock))
|
|
349 )
|
|
350 (let* ((left 11)
|
|
351 (namew 8)
|
|
352 (groupw 8)
|
|
353 (sizew 8)
|
|
354 (datew (if tar-can-print-dates 15 2))
|
|
355 (slash (1- (+ left namew)))
|
|
356 (lastdigit (+ slash groupw sizew))
|
|
357 (namestart (+ lastdigit datew))
|
|
358 (string (make-string (+ namestart (length name) (if link-p (+ 5 (length link-name)) 0)) 32))
|
|
359 (type (tar-header-link-type tar-hblock)))
|
|
360 (aset string 0 (if mod-p ?* ? ))
|
|
361 (aset string 1
|
|
362 (cond ((or (eq type nil) (eq type 0)) ?-)
|
|
363 ((eq type 1) ?l) ; link
|
|
364 ((eq type 2) ?s) ; symlink
|
|
365 ((eq type 3) ?c) ; char special
|
|
366 ((eq type 4) ?b) ; block special
|
|
367 ((eq type 5) ?d) ; directory
|
|
368 ((eq type 6) ?p) ; FIFO/pipe
|
|
369 ((eq type 20) ?*) ; directory listing
|
|
370 ((eq type 29) ?M) ; multivolume continuation
|
|
371 ((eq type 35) ?S) ; sparse
|
|
372 ((eq type 38) ?V) ; volume header
|
|
373 ))
|
|
374 (tar-grind-file-mode mode string 2)
|
|
375 (setq uid (if (= 0 (length uname)) (int-to-string uid) uname))
|
|
376 (setq gid (if (= 0 (length gname)) (int-to-string gid) gname))
|
|
377 (setq size (int-to-string size))
|
|
378 (tar-dotimes (i (min (1- namew) (length uid))) (aset string (- slash i) (aref uid (- (length uid) i 1))))
|
|
379 (aset string (1+ slash) ?/)
|
|
380 (tar-dotimes (i (min (1- groupw) (length gid))) (aset string (+ (+ slash 2) i) (aref gid i)))
|
|
381 (tar-dotimes (i (min sizew (length size))) (aset string (- lastdigit i) (aref size (- (length size) i 1))))
|
|
382
|
|
383 (if tar-can-print-dates
|
|
384 (let* ((year (substring (current-time-string) -4))
|
|
385 ;; in v18, current-time-string doesn't take an argument
|
|
386 (file (current-time-string time))
|
|
387 (file-year (substring file -4))
|
|
388 (str (if (equal year file-year)
|
|
389 (substring file 4 16)
|
|
390 (concat (substring file 4 11) " " file-year))))
|
|
391 (tar-dotimes (i 12) (aset string (- namestart (- 13 i)) (aref str i)))))
|
|
392
|
|
393 (tar-dotimes (i (length name)) (aset string (+ namestart i) (aref name i)))
|
|
394 (if (or (eq link-p 1) (eq link-p 2))
|
|
395 (progn
|
|
396 (tar-dotimes (i 3) (aset string (+ namestart 1 (length name) i) (aref (if (= link-p 1) "==>" "-->") i)))
|
|
397 (tar-dotimes (i (length link-name)) (aset string (+ namestart 5 (length name) i) (aref link-name i)))))
|
|
398 string)))
|
|
399
|
|
400
|
|
401 ;; buffer-local variables in the tar file's buffer:
|
|
402 ;;
|
|
403 (defvar tar-parse-info) ; the header structures
|
|
404 (defvar tar-header-offset) ; the end of the "pretty" data
|
|
405
|
|
406 (defun tar-summarize-buffer ()
|
|
407 "Parse the contents of the tar file in the current buffer, and place a
|
|
408 dired-like listing on the front; then narrow to it, so that only that listing
|
|
409 is visible (and the real data of the buffer is hidden)."
|
|
410 (message "parsing tar file...")
|
|
411 (let* ((result '())
|
|
412 (pos 1)
|
|
413 (bs (max 1 (- (buffer-size) 1024))) ; always 2+ empty blocks at end.
|
|
414 (bs100 (max 1 (/ bs 100)))
|
|
415 (tokens nil))
|
|
416 (while (not (eq tokens 'empty-tar-block))
|
|
417 (if (> (+ pos 512) (point-max))
|
|
418 (error "truncated tar file"))
|
|
419 (let* ((hblock (buffer-substring pos (+ pos 512))))
|
|
420 (setq tokens (tokenize-tar-header-block hblock))
|
|
421 (setq pos (+ pos 512))
|
|
422 (message "parsing tar file...%s%%"
|
|
423 ;(/ (* pos 100) bs) ; this gets round-off lossage
|
|
424 (/ pos bs100) ; this doesn't
|
|
425 )
|
|
426 (if (eq tokens 'empty-tar-block)
|
|
427 nil
|
|
428 (if (null tokens) (error "premature EOF parsing tar file."))
|
|
429 (if (eq (tar-header-link-type tokens) 20)
|
|
430 ;; Foo. There's an extra empty block after these.
|
|
431 (setq pos (+ pos 512)))
|
|
432 (let ((size (tar-header-size tokens)))
|
|
433 (if (< size 0)
|
|
434 (error "%s has size %s - corrupted."
|
|
435 (tar-header-name tokens) size))
|
|
436 ;
|
|
437 ; This is just too slow. Don't really need it anyway....
|
|
438 ;(check-tar-header-block-checksum
|
|
439 ; hblock (checksum-tar-header-block hblock)
|
|
440 ; (tar-header-name tokens))
|
|
441
|
|
442 (setq result (cons (make-tar-desc pos tokens) result))
|
|
443
|
|
444 (if (and (null (tar-header-link-type tokens))
|
|
445 (> size 0))
|
|
446 (setq pos
|
|
447 (+ pos 512 (ash (ash (1- size) -9) 9)) ; this works
|
|
448 ;(+ pos (+ size (- 512 (rem (1- size) 512)))) ; this doesn't
|
|
449 ))
|
|
450 ))))
|
|
451 (make-local-variable 'tar-parse-info)
|
|
452 (setq tar-parse-info (nreverse result)))
|
|
453 (message "parsing tar file...formatting...")
|
|
454 (save-excursion
|
|
455 (goto-char (point-min))
|
|
456 (let ((buffer-read-only nil))
|
|
457 (tar-dolist (tar-desc tar-parse-info)
|
|
458 (insert
|
|
459 (summarize-tar-header-block (tar-desc-tokens tar-desc))
|
|
460 "\n"))
|
|
461 (make-local-variable 'tar-header-offset)
|
|
462 (setq tar-header-offset (point))
|
|
463 (narrow-to-region 1 tar-header-offset)
|
|
464 (set-buffer-modified-p nil)))
|
|
465 (message "parsing tar file...done."))
|
|
466
|
|
467
|
|
468 (defvar tar-mode-map nil "*Local keymap for tar-mode listings.")
|
|
469
|
|
470 (if tar-mode-map
|
|
471 nil
|
|
472 (setq tar-mode-map (make-keymap))
|
|
473 (suppress-keymap tar-mode-map)
|
|
474 (define-key tar-mode-map " " 'tar-next-line)
|
|
475 (define-key tar-mode-map "c" 'tar-copy)
|
|
476 (define-key tar-mode-map "d" 'tar-flag-deleted)
|
|
477 (define-key tar-mode-map "\^D" 'tar-flag-deleted)
|
|
478 (define-key tar-mode-map "e" 'tar-extract)
|
|
479 (define-key tar-mode-map "f" 'tar-extract)
|
|
480 (define-key tar-mode-map "g" 'revert-buffer)
|
|
481 (define-key tar-mode-map "h" 'describe-mode)
|
|
482 (define-key tar-mode-map "n" 'tar-next-line)
|
|
483 (define-key tar-mode-map "\^N" 'tar-next-line)
|
|
484 (define-key tar-mode-map "o" 'tar-extract-other-window)
|
|
485 (define-key tar-mode-map "\^C" 'tar-copy)
|
|
486 (define-key tar-mode-map "p" 'tar-previous-line)
|
|
487 (define-key tar-mode-map "\^P" 'tar-previous-line)
|
|
488 (define-key tar-mode-map "r" 'tar-rename-entry)
|
|
489 (define-key tar-mode-map "u" 'tar-unflag)
|
|
490 (define-key tar-mode-map "v" 'tar-view)
|
|
491 (define-key tar-mode-map "x" 'tar-expunge)
|
|
492 (define-key tar-mode-map "\177" 'tar-unflag-backwards)
|
|
493 (define-key tar-mode-map "E" 'tar-extract-other-window)
|
|
494 (define-key tar-mode-map "M" 'tar-chmod-entry)
|
|
495 (define-key tar-mode-map "G" 'tar-chgrp-entry)
|
|
496 (define-key tar-mode-map "O" 'tar-chown-entry)
|
|
497
|
|
498 (cond ((string-match "XEmacs" emacs-version)
|
|
499 (define-key tar-mode-map 'button2 'tar-track-mouse-and-extract-file)
|
|
500 (define-key tar-mode-map 'button3 'tar-popup-menu)))
|
|
501 )
|
|
502
|
|
503
|
|
504 ;; XEmacs menu mouse/support added by Heiko Muenkel
|
|
505 ;; muenkel@tnt.uni-hannover.de
|
|
506
|
|
507 (autoload 'dired-mark-region "dired-xemacs-menu")
|
|
508
|
|
509 (defvar tar-menu
|
|
510 '("Tar Mode Commands"
|
|
511 ["Copy Subfile to Disk" tar-copy t]
|
|
512 ["Rename Subfile" tar-rename-entry t]
|
|
513 "----"
|
|
514 ["Delete Flaged Subfiles" tar-expunge t]
|
|
515 ["Flag Subfile for Deletion" tar-flag-deleted t]
|
|
516 ["Flag Subfiles in Region for Deletion"
|
|
517 (dired-mark-region '(tar-flag-deleted 1))
|
|
518 (mark)]
|
|
519 ["Unflag Subfile" tar-unflag t]
|
|
520 ["Unflag Subfiles in Region"
|
|
521 (dired-mark-region '(tar-flag-deleted 1 t))
|
|
522 (mark)]
|
|
523 "----"
|
|
524 ["Change Permissions of Subfile..." tar-chmod-entry t]
|
|
525 ["Change Group of Subfile..." tar-chgrp-entry t]
|
|
526 ["Change Owner of Subfile..." tar-chown-entry t]
|
|
527 "----"
|
|
528 ["Edit Subfile Other Window" tar-extract-other-window t]
|
|
529 ["Edit Subfile" tar-extract t]
|
|
530 ["View Subfile" tar-view t]
|
|
531 ))
|
|
532
|
|
533
|
|
534 (defun tar-track-mouse-and-extract-file (event)
|
|
535 "Visit the tar-file-entry upon which the mouse is clicked."
|
|
536 (interactive "e")
|
|
537 (mouse-set-point event)
|
|
538 (tar-next-line 0)
|
|
539 (let (buffer)
|
|
540 (save-excursion
|
|
541 (tar-extract)
|
|
542 (setq buffer (current-buffer)))
|
|
543 (switch-to-buffer buffer)))
|
|
544
|
|
545 (defun tar-popup-menu (event)
|
|
546 "Display the tar-mode menu."
|
|
547 (interactive "@e")
|
|
548 (mouse-set-point event)
|
|
549 (tar-next-line 0)
|
|
550 (popup-menu tar-menu))
|
|
551
|
|
552
|
|
553 ;; tar mode is suitable only for specially formatted data.
|
|
554 (put 'tar-mode 'mode-class 'special)
|
|
555 (put 'tar-subfile-mode 'mode-class 'special)
|
|
556
|
|
557 ;;;###autoload
|
|
558 (defun tar-mode ()
|
|
559 "Major mode for viewing a tar file as a dired-like listing of its contents.
|
|
560 You can move around using the usual cursor motion commands.
|
|
561 Letters no longer insert themselves.
|
|
562 Type 'e' to pull a file out of the tar file and into its own buffer.
|
|
563 Type 'c' to copy an entry from the tar file into another file on disk.
|
|
564
|
|
565 If you edit a sub-file of this archive (as with the 'e' command) and
|
|
566 save it with Control-X Control-S, the contents of that buffer will be
|
|
567 saved back into the tar-file buffer; in this way you can edit a file
|
|
568 inside of a tar archive without extracting it and re-archiving it.
|
|
569
|
|
570 See also: variables tar-update-datestamp and tar-anal-blocksize.
|
|
571 \\{tar-mode-map}"
|
|
572 ;; this is not interactive because you shouldn't be turning this
|
|
573 ;; mode on and off. You can corrupt things that way.
|
|
574 (make-local-variable 'tar-header-offset)
|
|
575 (make-local-variable 'tar-parse-info)
|
|
576 (make-local-variable 'require-final-newline)
|
|
577 (setq require-final-newline nil) ; binary data, dude...
|
|
578 (make-local-variable 'revert-buffer-function)
|
|
579 (setq revert-buffer-function 'tar-mode-revert)
|
|
580 (setq major-mode 'tar-mode)
|
|
581 (setq mode-name "Tar")
|
|
582 (use-local-map tar-mode-map)
|
|
583 (auto-save-mode 0)
|
|
584 (widen)
|
|
585 (if (and (boundp 'tar-header-offset) tar-header-offset)
|
|
586 (narrow-to-region 1 tar-header-offset)
|
|
587 (tar-summarize-buffer))
|
|
588
|
|
589 (cond ((string-match "XEmacs" emacs-version)
|
|
590 (require 'mode-motion)
|
|
591 (setq mode-motion-hook 'mode-motion-highlight-line)
|
|
592 (if (and current-menubar (not (assoc "Tar" current-menubar)))
|
|
593 (progn
|
|
594 (set-buffer-menubar (copy-sequence current-menubar))
|
|
595 (add-menu nil "Tar" (cdr tar-menu))))
|
|
596 ))
|
|
597 (run-hooks 'tar-mode-hook)
|
|
598 )
|
|
599
|
|
600 ;; buffer-local variables in subfile mode.
|
|
601 ;;
|
|
602 (defvar tar-subfile-mode nil) ; whether the minor-mode is on
|
|
603 (defvar superior-tar-buffer) ; parent buffer
|
|
604 (defvar superior-tar-descriptor) ; header object of this file
|
|
605 (defvar tar-subfile-buffer-id) ; pretty name-string
|
|
606 (defvar subfile-orig-mlbid) ; orig mode-line-buffer-identification
|
|
607
|
|
608 (defun tar-subfile-mode (p)
|
|
609 "Minor mode for editing an element of a tar-file.
|
|
610 This mode redefines ^X^S to save the current buffer back into its
|
|
611 associated tar-file buffer. You must save that buffer to actually
|
|
612 save your changes to disk."
|
|
613 (interactive "P")
|
|
614 (or (and (boundp 'superior-tar-buffer) superior-tar-buffer)
|
|
615 (error "This buffer is not an element of a tar file."))
|
|
616 (or (assq 'tar-subfile-mode minor-mode-alist)
|
|
617 (setq minor-mode-alist (append minor-mode-alist
|
|
618 (list '(tar-subfile-mode " TarFile")))))
|
|
619 (make-local-variable 'tar-subfile-mode)
|
|
620 (setq tar-subfile-mode
|
|
621 (if (null p)
|
|
622 (not tar-subfile-mode)
|
|
623 (> (prefix-numeric-value p) 0)))
|
|
624 (cond (tar-subfile-mode
|
|
625 ;; copy the local keymap so that we don't accidentally
|
|
626 ;; alter a keymap like 'lisp-mode-map' which is shared
|
|
627 ;; by all buffers in that mode.
|
|
628 (let ((m (current-local-map)))
|
|
629 (if m (use-local-map (copy-keymap m))))
|
|
630 (local-set-key "\^X\^S" 'tar-subfile-save-buffer)
|
|
631 ;; turn off auto-save.
|
|
632 (auto-save-mode nil)
|
|
633 (setq buffer-auto-save-file-name nil)
|
|
634 (run-hooks 'tar-subfile-mode-hook))
|
|
635 (t
|
|
636 ;; remove the local binding for C-x C-s.
|
|
637 (local-unset-key "\^X\^S")
|
|
638 (if subfile-orig-mlbid
|
|
639 (set (make-local-variable 'mode-line-buffer-identification)
|
|
640 subfile-orig-mlbid))
|
|
641 (setq superior-tar-buffer nil
|
|
642 superior-tar-descriptor nil
|
|
643 subfile-orig-mlbid nil)
|
|
644 ))
|
|
645 )
|
|
646
|
|
647 (defun tar-subfile-after-write-file-hook ()
|
|
648 ;; if the buffer has a filename, then it is no longer associated with
|
|
649 ;; the tar file. Turn off subfile mode.
|
|
650 (if (and buffer-file-name tar-subfile-mode)
|
|
651 (tar-subfile-mode -1)))
|
|
652
|
|
653 (defun tar-mode-revert (&optional no-autosave no-confirm)
|
|
654 "Revert this buffer and turn on tar mode again, to re-compute the
|
|
655 directory listing."
|
|
656 (setq tar-header-offset nil)
|
|
657 (let ((revert-buffer-function nil))
|
|
658 (revert-buffer t no-confirm)
|
|
659 (widen))
|
|
660 (tar-mode))
|
|
661
|
|
662
|
|
663 (defun tar-next-line (p)
|
|
664 (interactive "p")
|
|
665 (forward-line p)
|
|
666 (if (eobp) nil (forward-char (if tar-can-print-dates 48 36))))
|
|
667
|
|
668 (defun tar-previous-line (p)
|
|
669 (interactive "p")
|
|
670 (tar-next-line (- p)))
|
|
671
|
|
672 (defun tar-current-descriptor (&optional noerror)
|
|
673 "Returns the tar-descriptor of the current line, or signals an error."
|
|
674 ;; I wish lines had plists, like in ZMACS...
|
|
675 (or (nth (count-lines (point-min)
|
|
676 (save-excursion (beginning-of-line) (point)))
|
|
677 tar-parse-info)
|
|
678 (if noerror
|
|
679 nil
|
|
680 (error "This line does not describe a tar-file entry."))))
|
|
681
|
|
682
|
|
683 (defun tar-extract (&optional other-window-p)
|
|
684 "*In tar-mode, extract this entry of the tar file into its own buffer."
|
|
685 (interactive)
|
|
686 (let* ((view-p (eq other-window-p 'view))
|
|
687 (descriptor (tar-current-descriptor))
|
|
688 (tokens (tar-desc-tokens descriptor))
|
|
689 (name (tar-header-name tokens))
|
|
690 (size (tar-header-size tokens))
|
|
691 (link-p (tar-header-link-type tokens))
|
|
692 (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
|
|
693 (end (+ start size)))
|
|
694 (if link-p
|
|
695 (error "This is a %s, not a real file."
|
|
696 (cond ((eq link-p 5) "directory")
|
|
697 ((eq link-p 20) "tar directory header")
|
|
698 ((eq link-p 29) "multivolume-continuation")
|
|
699 ((eq link-p 35) "sparse entry")
|
|
700 ((eq link-p 38) "volume header")
|
|
701 (t "link"))))
|
|
702 (if (zerop size) (error "This is a zero-length file."))
|
|
703 (let* ((tar-buffer (current-buffer))
|
|
704 (bufname (file-name-nondirectory name))
|
|
705 (bufid (concat ;" (" name " in "
|
|
706 " (in "
|
|
707 (file-name-nondirectory (buffer-file-name))
|
|
708 ")"))
|
|
709 (read-only-p (or buffer-read-only view-p))
|
|
710 (buffer nil)
|
|
711 (buffers (buffer-list))
|
|
712 (just-created nil))
|
|
713 ;; find a buffer visiting this subfile from this tar file.
|
|
714 (while (and buffers (not buffer))
|
|
715 (set-buffer (car buffers))
|
|
716 (if (and (null (buffer-file-name (car buffers)))
|
|
717 (boundp 'superior-tar-descriptor)
|
|
718 (eq superior-tar-descriptor descriptor))
|
|
719 (setq buffer (car buffers))
|
|
720 (setq buffers (cdr buffers))))
|
|
721 (set-buffer tar-buffer)
|
|
722 (if buffer
|
|
723 nil
|
|
724 (setq buffer (generate-new-buffer bufname))
|
|
725 (setq just-created t)
|
|
726 (unwind-protect
|
|
727 (progn
|
|
728 (widen)
|
|
729 (save-excursion
|
|
730 (set-buffer buffer)
|
|
731 (insert-buffer-substring tar-buffer start end)
|
|
732 (goto-char 0)
|
|
733 (let ((lock-directory nil)) ; disable locking
|
|
734 (set-visited-file-name name) ; give it a name to decide mode.
|
|
735 ;; (normal-mode) ; pick a mode.
|
|
736 ;; (after-find-file nil nil) ; pick a mode; works with crypt.el
|
|
737 ;; Ok, instead of running after-find-file, just invoke the
|
|
738 ;; find-file-hooks instead. This does everything we want
|
|
739 ;; from after-find-file, without losing when visiting .tar
|
|
740 ;; files via ange-ftp: doesn't probe the ftp site for the
|
|
741 ;; name of the subfile.
|
|
742 (normal-mode t)
|
|
743 (run-hooks 'find-file-hooks)
|
|
744 (set-visited-file-name nil) ; nuke the name - not meaningful.
|
|
745 )
|
|
746 (make-local-variable 'superior-tar-buffer)
|
|
747 (make-local-variable 'superior-tar-descriptor)
|
|
748 (make-local-variable 'mode-line-buffer-identification)
|
|
749 (make-local-variable 'tar-subfile-buffer-id)
|
|
750 (make-local-variable 'subfile-orig-mlbid)
|
|
751 (setq superior-tar-buffer tar-buffer)
|
|
752 (setq superior-tar-descriptor descriptor)
|
|
753 (setq tar-subfile-buffer-id bufid)
|
|
754 (setq subfile-orig-mlbid mode-line-buffer-identification)
|
|
755 (cond ((stringp mode-line-buffer-identification)
|
|
756 (setq mode-line-buffer-identification
|
|
757 (list mode-line-buffer-identification))))
|
|
758 (let ((ms (car mode-line-buffer-identification))
|
|
759 n)
|
|
760 (cond ((and (stringp ms)
|
|
761 (string-match "%\\([0-9]+\\)b\\'" ms))
|
|
762 (setq mode-line-buffer-identification
|
|
763 (cons
|
|
764 (concat (substring ms 0
|
|
765 (1- (match-beginning 1)))
|
|
766 (substring ms (1+ (match-end 1))))
|
|
767 (cons
|
|
768 (list (car (read-from-string
|
|
769 (substring ms (match-beginning 1)
|
|
770 (match-end 1))))
|
|
771 (concat "%b" tar-subfile-buffer-id))
|
|
772 (cdr mode-line-buffer-identification)))))
|
|
773 (t
|
|
774 (setq mode-line-buffer-identification
|
|
775 (list "Emacs: "
|
|
776 (list 17
|
|
777 (concat "%b"
|
|
778 tar-subfile-buffer-id)))))))
|
|
779 (tar-subfile-mode 1)
|
|
780
|
|
781 (setq buffer-read-only read-only-p)
|
|
782 (set-buffer-modified-p nil))
|
|
783 (set-buffer tar-buffer))
|
|
784 (narrow-to-region 1 tar-header-offset)))
|
|
785 (if view-p
|
|
786 (progn
|
|
787 (view-buffer-other-window buffer)
|
|
788 (save-excursion
|
|
789 (set-buffer buffer)
|
|
790 ;; for view-less.el; view.el can't do this.
|
|
791 (set (make-local-variable 'view-kill-on-exit) t)))
|
|
792 (if other-window-p
|
|
793 (switch-to-buffer-other-window buffer)
|
|
794 (switch-to-buffer buffer))))))
|
|
795
|
|
796
|
|
797 (defun tar-extract-other-window ()
|
|
798 "*In tar-mode, extract this entry of the tar file into its own buffer."
|
|
799 (interactive)
|
|
800 (tar-extract t))
|
|
801
|
|
802 (defun tar-view ()
|
|
803 "*In tar-mode, view the tar file entry on this line."
|
|
804 (interactive)
|
|
805 (tar-extract 'view))
|
|
806
|
|
807
|
|
808 (defun tar-read-file-name (&optional prompt)
|
|
809 "Calls read-file-name, with the default being the file of the current
|
|
810 tar-file descriptor."
|
|
811 (or prompt (setq prompt "Copy to: "))
|
|
812 (let* ((default-file (expand-file-name
|
|
813 (tar-header-name (tar-desc-tokens
|
|
814 (tar-current-descriptor)))))
|
|
815 (target (expand-file-name
|
|
816 (read-file-name prompt
|
|
817 (file-name-directory default-file)
|
|
818 default-file nil))))
|
|
819 (if (or (string= "" (file-name-nondirectory target))
|
|
820 (file-directory-p target))
|
|
821 (setq target (concat (if (string-match "/$" target)
|
|
822 (substring target 0 (1- (match-end 0)))
|
|
823 target)
|
|
824 "/"
|
|
825 (file-name-nondirectory default-file))))
|
|
826 target))
|
|
827
|
|
828
|
|
829 (defun tar-copy (&optional to-file)
|
|
830 "*In tar-mode, extract this entry of the tar file into a file on disk.
|
|
831 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
|
|
832 the current tar-entry."
|
|
833 (interactive (list (tar-read-file-name)))
|
|
834 (let* ((descriptor (tar-current-descriptor))
|
|
835 (tokens (tar-desc-tokens descriptor))
|
|
836 (name (tar-header-name tokens))
|
|
837 (size (tar-header-size tokens))
|
|
838 (link-p (tar-header-link-type tokens))
|
|
839 (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
|
|
840 (end (+ start size)))
|
|
841 (if link-p (error "This is a link, not a real file."))
|
|
842 (if (zerop size) (error "This is a zero-length file."))
|
|
843 (let* ((tar-buffer (current-buffer))
|
|
844 buffer)
|
|
845 (unwind-protect
|
|
846 (progn
|
|
847 (setq buffer (generate-new-buffer "*tar-copy-tmp*"))
|
|
848 (widen)
|
|
849 (save-excursion
|
|
850 (set-buffer buffer)
|
|
851 (insert-buffer-substring tar-buffer start end)
|
|
852 (set-buffer-modified-p nil) ; in case we abort
|
|
853 (write-file to-file)
|
|
854 (message "Copied tar entry %s to %s" name to-file)
|
|
855 (set-buffer tar-buffer)))
|
|
856 (narrow-to-region 1 tar-header-offset)
|
|
857 (if buffer (kill-buffer buffer)))
|
|
858 )))
|
|
859
|
|
860
|
|
861 (defun tar-flag-deleted (p &optional unflag)
|
|
862 "*In tar mode, mark this sub-file to be deleted from the tar file.
|
|
863 With a prefix argument, mark that many files."
|
|
864 (interactive "p")
|
|
865 (beginning-of-line)
|
|
866 (tar-dotimes (i (if (< p 0) (- p) p))
|
|
867 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
|
|
868 (progn
|
|
869 (delete-char 1)
|
|
870 (insert (if unflag " " "D"))))
|
|
871 (forward-line (if (< p 0) -1 1)))
|
|
872 (if (eobp) nil (forward-char 36)))
|
|
873
|
|
874 (defun tar-unflag (p)
|
|
875 "*In tar mode, un-mark this sub-file if it is marked to be deleted.
|
|
876 With a prefix argument, un-mark that many files forward."
|
|
877 (interactive "p")
|
|
878 (tar-flag-deleted p t))
|
|
879
|
|
880 (defun tar-unflag-backwards (p)
|
|
881 "*In tar mode, un-mark this sub-file if it is marked to be deleted.
|
|
882 With a prefix argument, un-mark that many files backward."
|
|
883 (interactive "p")
|
|
884 (tar-flag-deleted (- p) t))
|
|
885
|
|
886
|
|
887 (defun tar-expunge-internal ()
|
|
888 "Expunge the tar-entry specified by the current line."
|
|
889 (let* ((descriptor (tar-current-descriptor))
|
|
890 (tokens (tar-desc-tokens descriptor))
|
|
891 (line (tar-desc-data-start descriptor))
|
|
892 (name (tar-header-name tokens))
|
|
893 (size (tar-header-size tokens))
|
|
894 (link-p (tar-header-link-type tokens))
|
|
895 (start (tar-desc-data-start descriptor))
|
|
896 (following-descs (cdr (memq descriptor tar-parse-info))))
|
|
897 (if link-p (setq size 0)) ; size lies for hard-links.
|
|
898 ;;
|
|
899 ;; delete the current line...
|
|
900 (beginning-of-line)
|
|
901 (let ((line-start (point)))
|
|
902 (end-of-line) (forward-char)
|
|
903 (let ((line-len (- (point) line-start)))
|
|
904 (delete-region line-start (point))
|
|
905 ;;
|
|
906 ;; decrement the header-pointer to be in synch...
|
|
907 (setq tar-header-offset (- tar-header-offset line-len))))
|
|
908 ;;
|
|
909 ;; delete the data pointer...
|
|
910 (setq tar-parse-info (delq descriptor tar-parse-info))
|
|
911 ;;
|
|
912 ;; delete the data from inside the file...
|
|
913 (widen)
|
|
914 (let* ((data-start (+ start tar-header-offset -513))
|
|
915 (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9))))
|
|
916 (delete-region data-start data-end)
|
|
917 ;;
|
|
918 ;; and finally, decrement the start-pointers of all following
|
|
919 ;; entries in the archive. This is a pig when deleting a bunch
|
|
920 ;; of files at once - we could optimize this to only do the
|
|
921 ;; iteration over the files that remain, or only iterate up to
|
|
922 ;; the next file to be deleted.
|
|
923 (let ((data-length (- data-end data-start)))
|
|
924 (tar-dolist (desc following-descs)
|
|
925 (tar-setf (tar-desc-data-start desc)
|
|
926 (- (tar-desc-data-start desc) data-length))))
|
|
927 ))
|
|
928 (narrow-to-region 1 tar-header-offset))
|
|
929
|
|
930
|
|
931 (defun tar-expunge (&optional noconfirm)
|
|
932 "*In tar-mode, delete all the archived files flagged for deletion.
|
|
933 This does not modify the disk image; you must save the tar file itself
|
|
934 for this to be permanent."
|
|
935 (interactive)
|
|
936 (if (or noconfirm
|
|
937 (y-or-n-p "expunge files marked for deletion? "))
|
|
938 (let ((n 0))
|
|
939 (save-excursion
|
|
940 (goto-char 0)
|
|
941 (while (not (eobp))
|
|
942 (if (looking-at "D")
|
|
943 (progn (tar-expunge-internal)
|
|
944 (setq n (1+ n)))
|
|
945 (forward-line 1)))
|
|
946 ;; after doing the deletions, add any padding that may be necessary.
|
|
947 (tar-pad-to-blocksize)
|
|
948 (narrow-to-region 1 tar-header-offset)
|
|
949 )
|
|
950 (if (zerop n)
|
|
951 (message "nothing to expunge.")
|
|
952 (message "%s expunged. Be sure to save this buffer." n)))))
|
|
953
|
|
954
|
|
955 (defun tar-clear-modification-flags ()
|
|
956 "remove the stars at the beginning of each line."
|
|
957 (save-excursion
|
|
958 (goto-char 0)
|
|
959 (while (< (point) tar-header-offset)
|
|
960 (if (looking-at "*")
|
|
961 (progn (delete-char 1) (insert " ")))
|
|
962 (forward-line 1))))
|
|
963
|
|
964
|
|
965 (defun tar-chown-entry (new-uid)
|
|
966 "*Change the user-id associated with this entry in the tar file.
|
|
967 If this tar file was written by GNU tar, then you will be able to edit
|
|
968 the user id as a string; otherwise, you must edit it as a number.
|
|
969 You can force editing as a number by calling this with a prefix arg.
|
|
970 This does not modify the disk image; you must save the tar file itself
|
|
971 for this to be permanent."
|
|
972 (interactive (list
|
|
973 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
|
|
974 (if (or current-prefix-arg
|
|
975 (not (tar-header-magic tokens)))
|
|
976 (let (n)
|
|
977 (while (not (numberp (setq n (read-minibuffer
|
|
978 "New UID number: "
|
|
979 (format "%s" (tar-header-uid tokens)))))))
|
|
980 n)
|
|
981 (read-string "New UID string: " (tar-header-uname tokens))))))
|
|
982 (cond ((stringp new-uid)
|
|
983 (tar-setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor)))
|
|
984 new-uid)
|
|
985 (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
|
|
986 (t
|
|
987 (tar-setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor)))
|
|
988 new-uid)
|
|
989 (tar-alter-one-field tar-uid-offset
|
|
990 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
|
|
991
|
|
992
|
|
993 (defun tar-chgrp-entry (new-gid)
|
|
994 "*Change the group-id associated with this entry in the tar file.
|
|
995 If this tar file was written by GNU tar, then you will be able to edit
|
|
996 the group id as a string; otherwise, you must edit it as a number.
|
|
997 You can force editing as a number by calling this with a prefix arg.
|
|
998 This does not modify the disk image; you must save the tar file itself
|
|
999 for this to be permanent."
|
|
1000 (interactive (list
|
|
1001 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
|
|
1002 (if (or current-prefix-arg
|
|
1003 (not (tar-header-magic tokens)))
|
|
1004 (let (n)
|
|
1005 (while (not (numberp (setq n (read-minibuffer
|
|
1006 "New GID number: "
|
|
1007 (format "%s" (tar-header-gid tokens)))))))
|
|
1008 n)
|
|
1009 (read-string "New GID string: " (tar-header-gname tokens))))))
|
|
1010 (cond ((stringp new-gid)
|
|
1011 (tar-setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor)))
|
|
1012 new-gid)
|
|
1013 (tar-alter-one-field tar-gname-offset
|
|
1014 (concat new-gid "\000")))
|
|
1015 (t
|
|
1016 (tar-setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor)))
|
|
1017 new-gid)
|
|
1018 (tar-alter-one-field tar-gid-offset
|
|
1019 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
|
|
1020
|
|
1021 (defun tar-rename-entry (new-name)
|
|
1022 "*Change the name associated with this entry in the tar file.
|
|
1023 This does not modify the disk image; you must save the tar file itself
|
|
1024 for this to be permanent."
|
|
1025 (interactive
|
|
1026 (list (read-string "New name: "
|
|
1027 (tar-header-name (tar-desc-tokens (tar-current-descriptor))))))
|
|
1028 (if (string= "" new-name) (error "zero length name."))
|
|
1029 (if (> (length new-name) 98) (error "name too long."))
|
|
1030 (tar-setf (tar-header-name (tar-desc-tokens (tar-current-descriptor)))
|
|
1031 new-name)
|
|
1032 (tar-alter-one-field 0
|
|
1033 (substring (concat new-name (make-string 99 0)) 0 99)))
|
|
1034
|
|
1035
|
|
1036 (defun tar-chmod-entry (new-mode)
|
|
1037 "*Change the protection bits associated with this entry in the tar file.
|
|
1038 This does not modify the disk image; you must save the tar file itself
|
|
1039 for this to be permanent."
|
|
1040 (interactive (list (tar-parse-octal-integer-safe
|
|
1041 (read-string "New protection (octal): "))))
|
|
1042 (tar-setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor)))
|
|
1043 new-mode)
|
|
1044 (tar-alter-one-field tar-mode-offset
|
|
1045 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
|
|
1046
|
|
1047
|
|
1048 (defun tar-alter-one-field (data-position new-data-string)
|
|
1049 (let* ((descriptor (tar-current-descriptor))
|
|
1050 (tokens (tar-desc-tokens descriptor)))
|
|
1051 (unwind-protect
|
|
1052 (save-excursion
|
|
1053 ;;
|
|
1054 ;; update the header-line.
|
|
1055 (beginning-of-line)
|
|
1056 (let ((p (point)))
|
|
1057 (forward-line 1)
|
|
1058 (delete-region p (point))
|
|
1059 (insert (summarize-tar-header-block tokens) "\n")
|
|
1060 (setq tar-header-offset (point-max)))
|
|
1061
|
|
1062 (widen)
|
|
1063 (let* ((start (+ (tar-desc-data-start descriptor) tar-header-offset -513)))
|
|
1064 ;;
|
|
1065 ;; delete the old field and insert a new one.
|
|
1066 (goto-char (+ start data-position))
|
|
1067 (delete-region (point) (+ (point) (length new-data-string))) ; <--
|
|
1068 (insert new-data-string) ; <--
|
|
1069 ;;
|
|
1070 ;; compute a new checksum and insert it.
|
|
1071 (let ((chk (checksum-tar-header-block
|
|
1072 (buffer-substring start (+ start 512)))))
|
|
1073 (goto-char (+ start tar-chk-offset))
|
|
1074 (delete-region (point) (+ (point) 8))
|
|
1075 (insert (format "%6o" chk))
|
|
1076 (insert 0)
|
|
1077 (insert ? )
|
|
1078 (tar-setf (tar-header-checksum tokens) chk)
|
|
1079 ;;
|
|
1080 ;; ok, make sure we didn't botch it.
|
|
1081 (check-tar-header-block-checksum
|
|
1082 (buffer-substring start (+ start 512))
|
|
1083 chk (tar-header-name tokens))
|
|
1084 )))
|
|
1085 (narrow-to-region 1 tar-header-offset))))
|
|
1086
|
|
1087
|
|
1088 (defun tar-subfile-save-buffer ()
|
|
1089 "In tar subfile mode, write this buffer back into its parent tar-file buffer.
|
|
1090 This doesn't write anything to disk - you must save the parent tar-file buffer
|
|
1091 to make your changes permanent."
|
|
1092 (interactive)
|
|
1093 (cond (buffer-file-name
|
|
1094 ;; tar-subfile buffers should have nil as buffer-file-name. If they
|
|
1095 ;; ever gain a buffer-file-name, that means they have been written to
|
|
1096 ;; a real disk file, as with ^X^W. If this happens, behave just like
|
|
1097 ;; `save-buffer.'
|
|
1098 (call-interactively 'save-buffer))
|
|
1099 (t
|
|
1100 (tar-subfile-save-buffer-internal))))
|
|
1101
|
|
1102 (defun tar-subfile-save-buffer-internal ()
|
|
1103 (if (not (and (boundp 'superior-tar-buffer) superior-tar-buffer))
|
|
1104 (error "this buffer has no superior tar file buffer."))
|
|
1105 (or (buffer-name superior-tar-buffer)
|
|
1106 (error "the superior tar file's buffer has been killed."))
|
|
1107 (if (not (and (boundp 'superior-tar-descriptor) superior-tar-descriptor))
|
|
1108 (error "this buffer doesn't have an index into its superior tar file!"))
|
|
1109
|
|
1110 ;; Notice when crypt.el has uncompressed while reading the subfile, and
|
|
1111 ;; signal an error if the user tries to save back into the parent file
|
|
1112 ;; (because it won't work - the .Z subfile it writes won't really be
|
|
1113 ;; compressed.)
|
|
1114 ;;
|
|
1115 ; ;; These are for the old crypt.el
|
|
1116 ; (if (and (boundp 'buffer-save-encrypted) buffer-save-encrypted)
|
|
1117 ; (error "Don't know how to encrypt back into a tar file."))
|
|
1118 ; (if (and (boundp 'buffer-save-compacted) buffer-save-compacted)
|
|
1119 ; (error "Don't know how to compact back into a tar file."))
|
|
1120 ; (if (and (boundp 'buffer-save-compressed) buffer-save-compressed)
|
|
1121 ; (error "Don't know how to compress back into a tar file."))
|
|
1122 ; (if (and (boundp 'buffer-save-gzipped) buffer-save-gzipped)
|
|
1123 ; (error "Don't know how to gzip back into a tar file."))
|
|
1124
|
|
1125 ;; These are for the new crypt++.el
|
|
1126 (if (and (boundp 'crypt-buffer-save-encrypted) crypt-buffer-save-encrypted)
|
|
1127 (error "Don't know how to encrypt back into a tar file."))
|
|
1128 (if (and (boundp 'crypt-buffer-save-compact) crypt-buffer-save-compact)
|
|
1129 (error "Don't know how to compact back into a tar file."))
|
|
1130 (if (and (boundp 'crypt-buffer-save-compress) crypt-buffer-save-compress)
|
|
1131 (error "Don't know how to compress back into a tar file."))
|
|
1132 (if (and (boundp 'crypt-buffer-save-gzip) crypt-buffer-save-gzip)
|
|
1133 (error "Don't know how to gzip back into a tar file."))
|
|
1134 (if (and (boundp 'crypt-buffer-save-freeze) crypt-buffer-save-freeze)
|
|
1135 (error "Don't know how to freeze back into a tar file."))
|
|
1136
|
|
1137 (save-excursion
|
|
1138 (let ((subfile (current-buffer))
|
|
1139 (subfile-size (buffer-size))
|
|
1140 (descriptor superior-tar-descriptor))
|
|
1141 (set-buffer superior-tar-buffer)
|
|
1142 (let* ((tokens (tar-desc-tokens descriptor))
|
|
1143 (start (tar-desc-data-start descriptor))
|
|
1144 (name (tar-header-name tokens))
|
|
1145 (size (tar-header-size tokens))
|
|
1146 (size-pad (ash (ash (+ size 511) -9) 9))
|
|
1147 (head (memq descriptor tar-parse-info))
|
|
1148 (following-descs (cdr head)))
|
|
1149 (if (not head)
|
|
1150 (error "Can't find this tar file entry in its parent tar file!"))
|
|
1151 (unwind-protect
|
|
1152 (save-excursion
|
|
1153 (widen)
|
|
1154 ;; delete the old data...
|
|
1155 (let* ((data-start (+ start tar-header-offset -1))
|
|
1156 (data-end (+ data-start (ash (ash (+ size 511) -9) 9))))
|
|
1157 (delete-region data-start data-end)
|
|
1158 ;; insert the new data...
|
|
1159 (goto-char data-start)
|
|
1160 (insert-buffer subfile)
|
|
1161 ;;
|
|
1162 ;; pad the new data out to a multiple of 512...
|
|
1163 (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9)))
|
|
1164 (goto-char (+ data-start subfile-size))
|
|
1165 (insert (make-string (- subfile-size-pad subfile-size) 0))
|
|
1166 ;;
|
|
1167 ;; update the data pointer of this and all following files...
|
|
1168 (tar-setf (tar-header-size tokens) subfile-size)
|
|
1169 (let ((difference (- subfile-size-pad size-pad)))
|
|
1170 (tar-dolist (desc following-descs)
|
|
1171 (tar-setf (tar-desc-data-start desc)
|
|
1172 (+ (tar-desc-data-start desc) difference))))
|
|
1173 ;;
|
|
1174 ;; Update the size field in the header block.
|
|
1175 (let ((header-start (- data-start 512)))
|
|
1176 (goto-char (+ header-start tar-size-offset))
|
|
1177 (delete-region (point) (+ (point) 12))
|
|
1178 (insert (format "%11o" subfile-size))
|
|
1179 (insert ? )
|
|
1180 ;;
|
|
1181 ;; Maybe update the datestamp.
|
|
1182 (if (not tar-update-datestamp)
|
|
1183 nil
|
|
1184 (goto-char (+ header-start tar-time-offset))
|
|
1185 (delete-region (point) (+ (point) 12))
|
|
1186 (let (now top bot)
|
|
1187 (cond ((fboundp 'current-time)
|
|
1188 (setq now (current-time))
|
|
1189 (setcdr now (car (cdr now))))
|
|
1190 ; ((fboundp 'current-time-seconds)
|
|
1191 ; (setq now (current-time-seconds)))
|
|
1192 )
|
|
1193 (setq top (car now)
|
|
1194 bot (cdr now))
|
|
1195 (cond
|
|
1196 (now
|
|
1197 (tar-setf (tar-header-date tokens) now)
|
|
1198 ;; hair to print two 16-bit numbers as one octal number.
|
|
1199 (setq bot (logior (ash (logand top 3) 16) bot))
|
|
1200 (setq top (ash top -2))
|
|
1201 (insert (format "%5o" top))
|
|
1202 (insert (format "%06o " bot)))
|
|
1203 (t
|
|
1204 ;; otherwise, set it to the epoch.
|
|
1205 (insert (format "%11o " 0))
|
|
1206 (tar-setf (tar-header-date tokens) (cons 0 0))
|
|
1207 ))))
|
|
1208 ;;
|
|
1209 ;; compute a new checksum and insert it.
|
|
1210 (let ((chk (checksum-tar-header-block
|
|
1211 (buffer-substring header-start data-start))))
|
|
1212 (goto-char (+ header-start tar-chk-offset))
|
|
1213 (delete-region (point) (+ (point) 8))
|
|
1214 (insert (format "%6o" chk))
|
|
1215 (insert 0)
|
|
1216 (insert ? )
|
|
1217 (tar-setf (tar-header-checksum tokens) chk)))
|
|
1218 ;;
|
|
1219 ;; alter the descriptor-line...
|
|
1220 ;;
|
|
1221 (let ((position (- (length tar-parse-info) (length head))))
|
|
1222 (goto-char 1)
|
|
1223 (next-line position)
|
|
1224 (beginning-of-line)
|
|
1225 (let ((p (point))
|
|
1226 (m (set-marker (make-marker) tar-header-offset)))
|
|
1227 (forward-line 1)
|
|
1228 (delete-region p (point))
|
|
1229 (insert-before-markers (summarize-tar-header-block tokens t) "\n")
|
|
1230 (setq tar-header-offset (marker-position m)))
|
|
1231 )))
|
|
1232 ;; after doing the insertion, add any final padding that may be necessary.
|
|
1233 (tar-pad-to-blocksize))
|
|
1234 (narrow-to-region 1 tar-header-offset)))
|
|
1235 (set-buffer-modified-p t) ; mark the tar file as modified
|
|
1236 (set-buffer subfile)
|
|
1237 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
|
|
1238 (message "saved into tar-buffer \"%s\" - remember to save that buffer!"
|
|
1239 (buffer-name superior-tar-buffer))
|
|
1240 )))
|
|
1241
|
|
1242
|
|
1243 (defun tar-pad-to-blocksize ()
|
|
1244 "If we are being anal about tar file blocksizes, fix up the current buffer.
|
|
1245 Leaves the region wide."
|
|
1246 (if (null tar-anal-blocksize)
|
|
1247 nil
|
|
1248 (widen)
|
|
1249 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
|
|
1250 (start (tar-desc-data-start last-desc))
|
|
1251 (tokens (tar-desc-tokens last-desc))
|
|
1252 (link-p (tar-header-link-type tokens))
|
|
1253 (size (if link-p 0 (tar-header-size tokens)))
|
|
1254 (data-end (+ start size))
|
|
1255 (bbytes (ash tar-anal-blocksize 9))
|
|
1256 (pad-to (+ bbytes (* bbytes (/ (1- data-end) bbytes))))
|
|
1257 (buffer-read-only nil) ; ##
|
|
1258 )
|
|
1259 ;; If the padding after the last data is too long, delete some;
|
|
1260 ;; else insert some until we are padded out to the right number of blocks.
|
|
1261 ;;
|
|
1262 (goto-char (+ (or tar-header-offset 0) data-end))
|
|
1263 (if (> (1+ (buffer-size)) (+ (or tar-header-offset 0) pad-to))
|
|
1264 (delete-region (+ (or tar-header-offset 0) pad-to) (1+ (buffer-size)))
|
|
1265 (insert (make-string (- (+ (or tar-header-offset 0) pad-to)
|
|
1266 (1+ (buffer-size)))
|
|
1267 0)))
|
|
1268 )))
|
|
1269
|
|
1270
|
|
1271 (defun maybe-write-tar-file ()
|
|
1272 "Used as a write-file-hook to write tar-files out correctly."
|
|
1273 ;;
|
|
1274 ;; If the current buffer is in tar-mode and has its header-offset set,
|
|
1275 ;; remove the header from the file, call the remaining write-file hooks,
|
|
1276 ;; and then write out the buffer (if and only if one of the write-file
|
|
1277 ;; hooks didn't write it already). Then put the header back on the
|
|
1278 ;; buffer. Many thanks to Piet van Oostrum for this code, which causes
|
|
1279 ;; correct interaction with crypt.el (and probably anything like it.)
|
|
1280 ;;
|
|
1281 ;; Kludge: in XEmacs Emacs, write-file-hooks is bound to nil before the
|
|
1282 ;; write-file-hooks are run, to prevent them from being run recursively
|
|
1283 ;; (this is more of a danger in v19-vintage emacses, which have both
|
|
1284 ;; write-file-hooks and write-contents-hooks.) So, we need to reference
|
|
1285 ;; an internal variable of basic-save-buffer to get the list of hooks
|
|
1286 ;; remaining to be run.
|
|
1287 ;;
|
|
1288 (and (eq major-mode 'tar-mode)
|
|
1289 (and (boundp 'tar-header-offset) tar-header-offset)
|
|
1290 (let* ((hooks (cond ((string-match "XEmacs" emacs-version)
|
|
1291 ;; Internal to basic-save-buffer in XEmacs.
|
|
1292 (symbol-value 'hooks))
|
|
1293 ((string-lessp "19" emacs-version)
|
|
1294 ;; I think this is what we need to do in fsfmacs.
|
|
1295 (append write-contents-hooks write-file-hooks))
|
|
1296 (t
|
|
1297 write-file-hooks)))
|
|
1298 (remaining-hooks (cdr (memq 'maybe-write-tar-file hooks)))
|
|
1299 header-string
|
|
1300 done)
|
|
1301 (save-excursion
|
|
1302 (save-restriction
|
|
1303 (widen)
|
|
1304 (tar-clear-modification-flags)
|
|
1305 (setq header-string (buffer-substring 1 tar-header-offset))
|
|
1306 (delete-region 1 tar-header-offset)
|
|
1307 (unwind-protect
|
|
1308 (progn
|
|
1309 (while (and remaining-hooks
|
|
1310 (not (setq done (funcall (car remaining-hooks)))))
|
|
1311 (setq remaining-hooks (cdr remaining-hooks)))
|
|
1312 (cond ((not done)
|
|
1313 (write-region 1 (1+ (buffer-size))
|
|
1314 buffer-file-name nil t)
|
|
1315 (setq done t))))
|
|
1316 (goto-char 1)
|
|
1317 (insert header-string)
|
|
1318 (set-buffer-modified-p nil))))
|
|
1319 done)))
|
|
1320
|
|
1321
|
|
1322 ;;; Patch it in.
|
|
1323
|
|
1324 (defvar tar-regexp "\\.\\(tar\\|tgz\\)$"
|
|
1325 "The regular expression used to identify tar file names.")
|
|
1326
|
|
1327 (setq auto-mode-alist
|
|
1328 (cons (cons tar-regexp 'tar-mode) auto-mode-alist))
|
|
1329
|
|
1330 ;; Note: the tar write-file-hook should go on the list *before* any other
|
|
1331 ;; hooks which might write the file. Since things like crypt-mode add things
|
|
1332 ;; to the end of the write-file-hooks, this will normally be the case.
|
|
1333
|
|
1334 ;(or (boundp 'write-file-hooks) (setq write-file-hooks nil))
|
|
1335 ;(or (listp write-file-hooks)
|
|
1336 ; (setq write-file-hooks (list write-file-hooks)))
|
|
1337 ;(or (memq 'maybe-write-tar-file write-file-hooks)
|
|
1338 ; (setq write-file-hooks
|
|
1339 ; (cons 'maybe-write-tar-file write-file-hooks)))
|
|
1340
|
|
1341 (add-hook 'write-file-hooks 'maybe-write-tar-file); ####write-contents-hooks??
|
|
1342 (cond ((boundp 'after-save-hook)
|
|
1343 (add-hook 'after-save-hook 'tar-subfile-after-write-file-hook))
|
|
1344 ((boundp 'after-write-file-hooks)
|
|
1345 (add-hook 'after-write-file-hooks 'tar-subfile-after-write-file-hook))
|
|
1346 (t (error "neither after-save-hook nor after-write-file-hooks?")))
|
|
1347
|
|
1348
|
|
1349 ;;; This is a hack. For files ending in .tar, we want -*- lines to be
|
|
1350 ;;; completely ignored - if there is one, it applies to the first file
|
|
1351 ;;; in the archive, and not the archive itself! Similarly for local
|
|
1352 ;;; variables specifications in the last file of the archive.
|
|
1353
|
|
1354 (defun tar-normal-mode (&optional find-file)
|
|
1355 "Choose the major mode for this buffer automatically.
|
|
1356 Also sets up any specified local variables of the file.
|
|
1357 Uses the visited file name, the -*- line, and the local variables spec.
|
|
1358
|
|
1359 This function is called automatically from `find-file'. In that case,
|
|
1360 if `inhibit-local-variables' is non-`nil' we require confirmation before
|
|
1361 processing a local variables spec. If you run `normal-mode' explicitly,
|
|
1362 confirmation is never required.
|
|
1363
|
|
1364 Note that this version of this function has been hacked to interact
|
|
1365 correctly with tar files - when visiting a file which matches
|
|
1366 'tar-regexp', the -*- line and local-variables are not examined,
|
|
1367 as they would apply to a file within the archive rather than the archive
|
|
1368 itself."
|
|
1369 (interactive)
|
|
1370 (if (and buffer-file-name
|
|
1371 (string-match tar-regexp buffer-file-name))
|
|
1372 (tar-mode)
|
|
1373 (tar-real-normal-mode find-file)))
|
|
1374
|
|
1375 ;; We have to shadow this as well to get along with crypt.el.
|
|
1376 ;; Shadowing this alone isn't enough, though; we need to shadow
|
|
1377 ;; tar-normal-mode in order to inhibit the local variables of the
|
|
1378 ;; last file in the tar archive.
|
|
1379 ;;
|
|
1380 (defun tar-set-auto-mode ()
|
|
1381 "Select major mode appropriate for current buffer.
|
|
1382 May base decision on visited file name (See variable auto-mode-list)
|
|
1383 or on buffer contents (-*- line or local variables spec), but does not look
|
|
1384 for the \"mode:\" local variable. For that, use hack-local-variables.
|
|
1385
|
|
1386 Note that this version of this function has been hacked to interact
|
|
1387 correctly with tar files - when visiting a file which matches
|
|
1388 'tar-regexp', the -*- line and local-variables are not examined,
|
|
1389 as they would apply to a file within the archive rather than the archive
|
|
1390 itself."
|
|
1391 (interactive)
|
|
1392 (if (and buffer-file-name
|
|
1393 (string-match tar-regexp buffer-file-name))
|
|
1394 (tar-mode)
|
|
1395 (tar-real-set-auto-mode)))
|
|
1396
|
|
1397 (if (not (fboundp 'tar-real-normal-mode))
|
|
1398 (fset 'tar-real-normal-mode (symbol-function 'normal-mode)))
|
|
1399 (fset 'normal-mode 'tar-normal-mode)
|
|
1400
|
|
1401 (if (not (fboundp 'tar-real-set-auto-mode))
|
|
1402 (fset 'tar-real-set-auto-mode (symbol-function 'set-auto-mode)))
|
|
1403 (fset 'set-auto-mode 'tar-set-auto-mode)
|
|
1404
|
|
1405 (provide 'tar-mode)
|