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