File: tm-tar.el

package info (click to toggle)
xemacs20 20.4-13
  • links: PTS
  • area: main
  • in suites: slink
  • size: 67,324 kB
  • ctags: 57,643
  • sloc: lisp: 586,197; ansic: 184,662; sh: 4,296; asm: 3,179; makefile: 2,021; perl: 1,059; csh: 96; sed: 22
file content (344 lines) | stat: -rw-r--r-- 9,513 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
;;;
;;; $Id: tm-tar.el,v 1.22 1995/10/21 15:34:33 H.Ueno Exp $
;;;
;;; tm-tar.el
;;;
;;; Internal viewer for
;;;    - application/x-tar
;;;    - application/x-gzip, type="tar"
;;;    - aplication/octet-stream, type="tar"
;;;    - aplication/octet-stream, type="tar+gzip"
;;;
;;; by Hiroshi Ueno <zodiac@ibm.net>
;;;	modified by Tomohiko Morioka <morioka@jaist.ac.jp>
;;;

;;; @ required modules
;;;

(require 'emu)
(require 'tm-view)

;;; @ constants
;;;

(defconst tm-tar/list-buffer "*tm-tar/List*")
(defconst tm-tar/view-buffer "*tm-tar/View*")
(defconst tm-tar/file-search-regexp "[0-9]+\:[0-9\:]+[ ]+[0-9]+[ ]+")
(defconst tm-tar/popup-menu-title "Action Menu")

;;; @ variables
;;;

(defvar tm-tar/tar-program  "gtar")
(defvar tm-tar/tar-decompress-arg '("-z"))
(defvar tm-tar/gzip-program "gzip")
(defvar tm-tar/mmencode-program "mmencode")
(defvar tm-tar/uudecode-program "uudecode")

(defvar tm-tar/popup-menu-items
  '(("View File"         . tm-tar/view-file)
    ("Key Help"          . tm-tar/helpful-message)
    ("Quit tm-tar Mode"  . exit-recursive-edit)
    ))

(cond ((string-match "XEmacs\\|Lucid" emacs-version)
       (defvar tm-tar/popup-menu
	 (cons tm-tar/popup-menu-title
		(mapcar (function
			(lambda (item)
			  (vector (car item)(cdr item) t)
			  ))
			 tm-tar/popup-menu-items)))

       (defun tm-tar/mouse-button-2 (event)
	   (popup-menu tm-tar/popup-menu)
	   )
       )
      ((>= emacs-major-version 19)
       (defun tm-tar/mouse-button-2 (event)
	 (let ((menu
		(cons tm-tar/popup-menu-title
			(list (cons "Menu Items" tm-tar/popup-menu-items))
			)))
	   (let ((func (x-popup-menu event menu)))
		 (if func
		     (funcall func)
		   ))
	     ))
       ))

(defvar tm-tar/tar-mode-map nil)
(if tm-tar/tar-mode-map
      nil
    (setq tm-tar/tar-mode-map (make-keymap))
    (suppress-keymap tm-tar/tar-mode-map)
    (define-key tm-tar/tar-mode-map "\C-c"    'exit-recursive-edit)
    (define-key tm-tar/tar-mode-map "q"       'exit-recursive-edit)
    (define-key tm-tar/tar-mode-map "n"       'tm-tar/next-line)
    (define-key tm-tar/tar-mode-map " "       'tm-tar/next-line)
    (define-key tm-tar/tar-mode-map "\C-m"    'tm-tar/next-line)
    (define-key tm-tar/tar-mode-map "p"       'tm-tar/previous-line)
    (define-key tm-tar/tar-mode-map "\177"    'tm-tar/previous-line)
    (define-key tm-tar/tar-mode-map "\C-\M-m" 'tm-tar/previous-line)
    (define-key tm-tar/tar-mode-map "v"       'tm-tar/view-file)
    (define-key tm-tar/tar-mode-map "\C-h"    'Helper-help)
    (define-key tm-tar/tar-mode-map "?"       'tm-tar/helpful-message)
    (if mouse-button-2
	(define-key tm-tar/tar-mode-map
				  mouse-button-2 'tm:button-dispatcher)
	)
  )

;;; @@ tm-tar mode functions
;;;

(defun tm-tar/tar-mode (&optional prev-buf)
  "Major mode for listing the contents of a tar archive file."
    (unwind-protect
	(let ((buffer-read-only t)
	      (mode-name "tm-tar")
	      (mode-line-buffer-identification '("%17b"))
	      )
	    (goto-char (point-min))
	    (tm-tar/move-to-filename)
	    (catch 'tm-tar/tar-mode (tm-tar/command-loop))
	 )
	(if prev-buf
	    (switch-to-buffer prev-buf)
	 )
     ))

(defun tm-tar/command-loop ()
    (let ((old-local-map (current-local-map))
	  )
	(unwind-protect
	    (progn
		(use-local-map tm-tar/tar-mode-map)
		(tm-tar/helpful-message)
		(recursive-edit)
	     )
	    (save-excursion
		(use-local-map old-local-map)
	     ))
     ))

(defun tm-tar/next-line ()
    (interactive)
    (next-line 1)
    (tm-tar/move-to-filename)
  )

(defun tm-tar/previous-line ()
    (interactive)
    (previous-line 1)
    (tm-tar/move-to-filename)
  )

(defun tm-tar/view-file ()
    (interactive)
    (let ((name (tm-tar/get-filename))
	  )
      (save-excursion
	  (switch-to-buffer tm-tar/view-buffer)
	  (setq buffer-read-only nil)
	  (erase-buffer)
	  (message "Reading a file from an archive. Please wait...")
	  (apply 'call-process tm-tar/tar-program
			 nil t nil (append tm-tar/view-args (list name)))
	  (goto-char (point-min))
       )
	(view-buffer tm-tar/view-buffer)
     ))

(defun tm-tar/get-filename ()
    (let (eol)
	(save-excursion
	    (end-of-line)
	    (setq eol (point))
	    (beginning-of-line)
	    (save-excursion
		(if (re-search-forward "^d" eol t)
			 (error "Cannot view a directory"))
	     )
	    (if (re-search-forward tm-tar/file-search-regexp eol t)
		     (progn (let ((beg (point))
				  )
				(skip-chars-forward "^ \n")
				(buffer-substring beg (point))
				))
		     (error "No file on this line")
	     ))
     ))

(defun tm-tar/move-to-filename ()
    (let ((eol (progn (end-of-line) (point)))
	  )
	(beginning-of-line)
	(re-search-forward tm-tar/file-search-regexp eol t)
     ))

(defun tm-tar/set-properties ()
    (if mouse-button-2
	(let ((beg (point-min))
	      (end (point-max))
	      )
	    (goto-char beg)
	    (save-excursion
		(while (re-search-forward tm-tar/file-search-regexp end t)
		    (tm:add-button (point)
					   (progn
						(end-of-line)
						(point))
					   'tm-tar/view-file)
		 ))
	 )))

(defun tm-tar/helpful-message ()
    (interactive)
    (message "Type %s, %s, %s, %s, %s, %s."
	(substitute-command-keys "\\[Helper-help] for help")
	(substitute-command-keys "\\[tm-tar/helpful-message] for keys")
	(substitute-command-keys "\\[tm-tar/next-line] to next")
	(substitute-command-keys "\\[tm-tar/previous-line] to prev")
	(substitute-command-keys "\\[tm-tar/view-file] to view")
	(substitute-command-keys "\\[exit-recursive-edit] to quit")
     ))

(defun tm-tar/y-or-n-p (prompt)
    (prog1
	(y-or-n-p prompt)
	(message "")
     ))

;;; @@ tar message decoder
;;

(defun mime/decode-message/tar (beg end cal)
    (if (tm-tar/y-or-n-p "Do you want to enter tm-tar mode? ")
	(let ((coding (cdr (assoc 'encoding cal)))
	      (cur-buf (current-buffer))
	      (tm-tar/tar-file-name (expand-file-name (concat (make-temp-name
			(expand-file-name "tm" mime/tmp-dir)) ".tar")))
	      (tm-tar/tmp-file-name (expand-file-name (make-temp-name
			(expand-file-name "tm" mime/tmp-dir))))
	      new-buf
	      )
	    (find-file tm-tar/tmp-file-name)
	    (setq new-buf (current-buffer))
	    (setq buffer-read-only nil)
	    (erase-buffer)
	    (save-excursion
		 (set-buffer cur-buf)
		(goto-char beg)
		(re-search-forward "^$")
		(append-to-buffer new-buf (+ (match-end 0) 1) end)
	     )
	    (if (member coding mime-viewer/uuencode-encoding-name-list)
		(progn
		    (goto-char (point-min))
		    (if (re-search-forward "^begin [0-9]+ " nil t)
			(progn
			    (kill-line)
			    (insert tm-tar/tar-file-name)
			 )
			(progn
			    (set-buffer-modified-p nil)
			    (kill-buffer new-buf)
			    (error "uuencode file signature was not found")
			 ))))
	    (save-buffer)
	    (kill-buffer new-buf)
	    (message "Listing the contents of an archive.  Please wait...")
	    (cond ((string-equal coding "base64")
		   (call-process tm-tar/mmencode-program nil nil nil "-u"
				"-o" tm-tar/tar-file-name tm-tar/tmp-file-name)
		   )
		  ((string-equal coding "quoted-printable")
		   (call-process tm-tar/mmencode-program nil nil nil "-u" "-q"
				"-o" tm-tar/tar-file-name tm-tar/tmp-file-name)
		   )
		  ((member coding mime-viewer/uuencode-encoding-name-list)
		   (call-process tm-tar/uudecode-program nil nil nil
				tm-tar/tmp-file-name)
		   )
		  (t
		   (copy-file tm-tar/tmp-file-name tm-tar/tar-file-name t)
		   ))
	    (delete-file tm-tar/tmp-file-name)
	    (setq tm-tar/list-args (list "-tvf" tm-tar/tar-file-name))
	    (setq tm-tar/view-args (list "-xOf" tm-tar/tar-file-name))
	    (if (eq 0 (call-process tm-tar/gzip-program
			    nil nil nil "-t" tm-tar/tar-file-name))
		(progn
		    (setq tm-tar/list-args
			  (append tm-tar/tar-decompress-arg tm-tar/list-args))
		    (setq tm-tar/view-args
			  (append tm-tar/tar-decompress-arg tm-tar/view-args))
		 ))
	    (switch-to-buffer tm-tar/view-buffer)
	    (switch-to-buffer tm-tar/list-buffer)
	    (setq buffer-read-only nil)
	    (erase-buffer)
	    (apply 'call-process tm-tar/tar-program
		   nil t nil tm-tar/list-args)
	    (if mouse-button-2
		 (progn
		    (make-local-variable 'tm:mother-button-dispatcher)
		    (setq tm:mother-button-dispatcher 'tm-tar/mouse-button-2)
		 ))
	    (tm-tar/set-properties)
	    (tm-tar/tar-mode mime::article/preview-buffer)
	    (kill-buffer tm-tar/view-buffer)
	    (kill-buffer tm-tar/list-buffer)
	    (delete-file tm-tar/tar-file-name)
	 )
     ))

;;; @@ program/buffer coding system
;;;

(cond ((boundp 'MULE)
       (define-program-coding-system tm-tar/view-buffer nil *autoconv*)
       )
      ((boundp 'NEMACS)
       (define-program-kanji-code tm-tar/view-buffer nil 1)
       ))

;;; @@ message types to use tm-tar
;;;

(set-atype 'mime/content-decoding-condition
	   '((type . "application/octet-stream")
	     (method . mime/decode-message/tar)
	     (mode . "play") ("type" . "tar")
	     ))

(set-atype 'mime/content-decoding-condition
	   '((type . "application/octet-stream")
	     (method . mime/decode-message/tar)
	     (mode . "play") ("type" . "tar+gzip")
	     ))

(set-atype 'mime/content-decoding-condition
	   '((type . "application/x-gzip")
	     (method . mime/decode-message/tar)
	     (mode . "play") ("type" . "tar")
	     ))

(set-atype 'mime/content-decoding-condition
	   '((type . "application/x-tar")
	     (method . mime/decode-message/tar)
	     (mode . "play")
	     ))

;;; @ end
;;;

(provide 'tm-tar)

;;; Local Variables:
;;; mode: emacs-lisp
;;; mode: outline-minor
;;; outline-regexp: ";;; @+\\|(......"
;;; End: