1 ;;; tm-play.el --- decoder for tm-view.el
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/9/26 (separated from tm-view.el)
7 ;; Version: $Id: tm-play.el,v 7.43 1997/03/12 14:26:55 morioka Exp $
8 ;; Keywords: mail, news, MIME, multimedia
10 ;; This file is part of tm (Tools for MIME).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program 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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (defvar mime-preview/after-decoded-position nil)
37 (defun mime-preview/decode-content ()
39 (let ((pc (mime-preview/point-pcinfo (point))))
41 (let ((the-buf (current-buffer)))
42 (setq mime-preview/after-decoded-position (point))
43 (set-buffer (mime::preview-content-info/buffer pc))
44 (mime-article/decode-content
45 (mime::preview-content-info/content-info pc))
46 (if (eq (current-buffer)
47 (mime::preview-content-info/buffer pc))
50 (goto-char mime-preview/after-decoded-position)
54 (defun mime-article/decode-content (cinfo)
55 (let ((beg (mime::content-info/point-min cinfo))
56 (end (mime::content-info/point-max cinfo))
57 (ctype (or (mime::content-info/type cinfo) "text/plain"))
58 (params (mime::content-info/parameters cinfo))
59 (encoding (mime::content-info/encoding cinfo))
62 (if (< beg (point-min))
63 (setq beg (point-min))
65 (if (< (point-max) end)
66 (setq end (point-max))
69 (setq cal (list* (cons 'type ctype)
70 (cons 'encoding encoding)
71 (cons 'major-mode major-mode)
73 (if mime-viewer/decoding-mode
75 (cons 'mode mime-viewer/decoding-mode)
78 (setq ret (mime/get-content-decoding-alist cal))
79 (setq method (cdr (assq 'method ret)))
80 (cond ((and (symbolp method)
82 (funcall method beg end ret)
84 ((and (listp method)(stringp (car method)))
85 (mime-article/start-external-method-region beg end ret)
88 (mime-article/show-output-buffer
89 "No method are specified for %s\n" ctype)
94 (defun field-unifier-for-mode (a b)
103 (defun mime/get-content-decoding-alist (al)
104 (get-unified-alist mime/content-decoding-condition al)
108 ;;; @ external decoder
111 (defun mime-article/start-external-method-region (beg end cal)
114 (narrow-to-region beg end)
116 (let ((method (cdr (assoc 'method cal)))
117 (name (mime-article/get-filename cal))
120 (let ((file (make-temp-name
121 (expand-file-name "TM" mime/tmp-dir)))
126 (if (re-search-forward "^$" nil t)
132 (write-region b end file)
133 (message "External method is starting...")
135 'name (replace-as-filename name) cal))
136 (setq cal (put-alist 'file file cal))
139 mime/output-buffer-name (car method)
141 (mime-article/make-method-args cal
144 (apply (function start-process) args)
145 (mime-article/show-output-buffer)
149 (defun mime-article/make-method-args (cal format)
154 (let* ((item (eval arg))
155 (ret (cdr (assoc item cal)))
159 (if (eq item 'encoding)
166 (defun mime-article/show-output-buffer (&rest forms)
167 (get-buffer-create mime/output-buffer-name)
168 (let ((the-win (selected-window))
169 (win (get-buffer-window mime/output-buffer-name))
172 (if (and mime/output-buffer-window-is-shared-with-bbdb
173 (boundp 'bbdb-buffer-name)
174 (setq win (get-buffer-window bbdb-buffer-name))
176 (set-window-buffer win mime/output-buffer-name)
177 (select-window (get-buffer-window mime::article/preview-buffer))
178 (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
179 (set-window-buffer win mime/output-buffer-name)
182 (goto-char (point-max))
184 (insert (apply (function format) forms))
186 (select-window the-win)
193 (defvar mime-viewer/file-name-char-regexp "[A-Za-z0-9+_-]")
195 (defvar mime-viewer/file-name-regexp-1
196 (concat mime-viewer/file-name-char-regexp "+\\."
197 mime-viewer/file-name-char-regexp "+"))
199 (defvar mime-viewer/file-name-regexp-2
200 (concat (regexp-* mime-viewer/file-name-char-regexp)
201 "\\(\\." mime-viewer/file-name-char-regexp "+\\)*"))
203 (defun mime-article/get-original-filename (param &optional encoding)
204 (or (mime-article/get-uu-filename param encoding)
206 (or (if (or (and (setq ret (mime/Content-Disposition))
207 (setq ret (assoc "filename" (cdr ret)))
209 (setq ret (assoc "name" param))
210 (setq ret (assoc "x-name" param))
212 (std11-strip-quoted-string (cdr ret))
215 (std11-find-field-body '("Content-Description"
217 (if (or (string-match mime-viewer/file-name-regexp-1 ret)
218 (string-match mime-viewer/file-name-regexp-2 ret))
219 (substring ret (match-beginning 0)(match-end 0))
224 (defun mime-article/get-filename (param)
225 (replace-as-filename (mime-article/get-original-filename param))
229 ;;; @ mail/news message
232 (defun mime-viewer/quitting-method-for-mime/show-message-mode ()
233 (let ((mother mime::preview/mother-buffer)
234 (win-conf mime::preview/original-window-configuration)
237 (mime::preview-content-info/buffer (car mime::preview/content-list)))
238 (mime-viewer/kill-buffer)
239 (set-window-configuration win-conf)
240 (pop-to-buffer mother)
241 ;;(goto-char (point-min))
242 ;;(mime-viewer/up-content)
245 (defun mime-article/view-message/rfc822 (beg end cal)
246 (let* ((cnum (mime-article/point-content-number beg))
247 (cur-buf (current-buffer))
248 (new-name (format "%s-%s" (buffer-name) cnum))
249 (mother mime::article/preview-buffer)
251 (or (cdr (assq major-mode mime-viewer/code-converter-alist))
252 'mime-viewer/default-code-convert-region))
254 (setq str (buffer-substring beg end))
255 (switch-to-buffer new-name)
258 (goto-char (point-min))
259 (if (re-search-forward "^\n" nil t)
260 (delete-region (point-min) (match-end 0))
262 (setq major-mode 'mime/show-message-mode)
263 (setq mime::article/code-converter code-converter)
264 (mime/viewer-mode mother)
268 ;;; @ message/partial
271 (defvar mime-article/coding-system-alist
272 (list (cons 'mh-show-mode *noconv*)
273 (cons t (mime-charset-to-coding-system default-mime-charset))
276 (cond ((boundp 'MULE) ; for MULE 2.3 or older
277 (defun mime-article::write-region (start end file)
278 (let ((file-coding-system
280 (or (assq major-mode mime-article/coding-system-alist)
281 (assq t mime-article/coding-system-alist)
283 (write-region start end file)
286 ((featurep 'mule) ; for Emacs/mule and XEmacs/mule
287 (defun mime-article::write-region (start end file)
288 (let ((coding-system-for-write
290 (or (assq major-mode mime-article/coding-system-alist)
291 (assq t mime-article/coding-system-alist)
293 (write-region start end file)
296 ((boundp 'NEMACS) ; for NEmacs
297 (defun mime-article::write-region (start end file)
298 (let ((kanji-fileio-code
300 (or (assq major-mode mime-article/kanji-code-alist)
301 (assq t mime-article/kanji-code-alist)
303 (write-region start end file)
306 (t ; for Emacs 19 or older and XEmacs without mule
307 (defalias 'mime-article::write-region 'write-region)
310 (defun mime-article/decode-message/partial (beg end cal)
312 (let* ((root-dir (expand-file-name
313 (concat "m-prts-" (user-login-name)) mime/tmp-dir))
314 (id (cdr (assoc "id" cal)))
315 (number (cdr (assoc "number" cal)))
316 (total (cdr (assoc "total" cal)))
318 (mother mime::article/preview-buffer)
320 (or (file-exists-p root-dir)
321 (make-directory root-dir)
323 (setq id (replace-as-filename id))
324 (setq root-dir (concat root-dir "/" id))
325 (or (file-exists-p root-dir)
326 (make-directory root-dir)
328 (setq file (concat root-dir "/FULL"))
329 (if (file-exists-p file)
330 (let ((full-buf (get-buffer-create "FULL"))
331 (pwin (or (get-buffer-window mother)
332 (get-largest-window)))
334 (save-window-excursion
335 (set-buffer full-buf)
337 (as-binary-input-file (insert-file-contents file))
338 (setq major-mode 'mime/show-message-mode)
339 (mime/viewer-mode mother)
341 (set-window-buffer pwin
343 (set-buffer full-buf)
344 mime::article/preview-buffer))
347 (re-search-forward "^$")
348 (goto-char (1+ (match-end 0)))
349 (setq file (concat root-dir "/" number))
350 (mime-article::write-region (point) (point-max) file)
351 (let ((total-file (concat root-dir "/CT")))
355 (or (file-exists-p total-file)
358 (get-buffer-create mime/temp-buffer-name))
361 (write-file total-file)
362 (kill-buffer (current-buffer))
364 (string-to-number total)
366 (and (file-exists-p total-file)
368 (set-buffer (find-file-noselect total-file))
370 (and (re-search-forward "[0-9]+" nil t)
372 (buffer-substring (match-beginning 0)
375 (kill-buffer (current-buffer))
378 (if (and total (> total 0))
381 (set-buffer (get-buffer-create mime/temp-buffer-name))
382 (let ((full-buf (current-buffer)))
386 (setq file (concat root-dir "/" (int-to-string i)))
387 (or (file-exists-p file)
390 (as-binary-input-file (insert-file-contents file))
391 (goto-char (point-max))
394 (as-binary-output-file (write-file (concat root-dir "/FULL")))
397 (let ((file (format "%s/%d" root-dir i)))
398 (and (file-exists-p file)
403 (let ((file (expand-file-name "CT" root-dir)))
404 (and (file-exists-p file)
407 (save-window-excursion
408 (setq major-mode 'mime/show-message-mode)
409 (mime/viewer-mode mother)
411 (let ((pwin (or (get-buffer-window mother)
414 (pbuf (save-excursion
415 (set-buffer full-buf)
416 mime::article/preview-buffer)))
417 (set-window-buffer pwin pbuf)
428 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
429 (define-key mime-view-text/plain-mode-map
430 "q" (function mime-view-text/plain-exit))
432 (defun mime-view-text/plain-mode ()
433 "\\{mime-view-text/plain-mode-map}"
434 (setq buffer-read-only t)
435 (setq major-mode 'mime-view-text/plain-mode)
436 (setq mode-name "MIME-View text/plain")
437 (use-local-map mime-view-text/plain-mode-map)
440 (defun mime-view-text/plain-exit ()
442 (kill-buffer (current-buffer))
445 (defun mime-article/decode-caesar (beg end cal)
446 (let* ((cnum (mime-article/point-content-number beg))
447 (cur-buf (current-buffer))
448 (new-name (format "%s-%s" (buffer-name) cnum))
449 (mother mime::article/preview-buffer)
450 (charset (cdr (assoc "charset" cal)))
451 (encoding (cdr (assq 'encoding cal)))
454 (setq str (buffer-substring beg end))
455 (let ((pwin (or (get-buffer-window mother)
456 (get-largest-window)))
457 (buf (get-buffer-create new-name))
459 (set-window-buffer pwin buf)
463 (setq buffer-read-only nil)
466 (goto-char (point-min))
467 (if (re-search-forward "^\n" nil t)
468 (delete-region (point-min) (match-end 0))
470 (let ((m (cdr (or (assq mode mime-viewer/code-converter-alist)
471 (assq t mime-viewer/code-converter-alist)))))
473 (funcall m charset encoding)
476 (set-mark (point-min))
477 (goto-char (point-max))
480 (set-buffer-modified-p nil)
481 (mime-view-text/plain-mode)
490 ;;; tm-play.el ends here