1 ;;; mime-play.el --- Playback processing module for mime-view.el
3 ;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/9/26 (separated from tm-view.el)
7 ;; Renamed: 1997/2/21 from tm-play.el
8 ;; Keywords: MIME, multimedia, mail, news
10 ;; This file is part of SEMI (Secretariat of Emacs MIME Interfaces).
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.
37 (defvar mime-preview-after-decoded-position nil)
39 (defun mime-preview-play-current-entity (&optional mode)
41 It decodes current entity to call internal or external method. The
42 method is selected from variable `mime-acting-condition'.
43 If MODE is specified, play as it. Default MODE is \"play\"."
48 (let ((cinfo (get-text-property (point) 'mime-view-cinfo)))
50 (let ((the-buf (current-buffer))
51 (raw-buffer (get-text-property (point) 'mime-view-raw-buffer))
53 (setq mime-preview-after-decoded-position (point))
54 (set-buffer raw-buffer)
55 (mime-playback-entity cinfo mode)
56 (if (eq (current-buffer) raw-buffer)
59 (goto-char mime-preview-after-decoded-position)
63 (defun mime-playback-entity (cinfo &optional mode)
64 (let ((beg (mime-entity-info-point-min cinfo))
65 (end (mime-entity-info-point-max cinfo))
66 (c-type (mime-entity-info-media-type cinfo))
67 (c-subtype (mime-entity-info-media-subtype cinfo))
68 (params (mime-entity-info-parameters cinfo))
69 (encoding (mime-entity-info-encoding cinfo))
75 (if (< beg (point-min))
76 (setq beg (point-min))
78 (if (< (point-max) end)
79 (setq end (point-max))
82 (setq cal (list* (cons 'type c-type)
83 (cons 'subtype c-subtype)
84 (cons 'encoding encoding)
85 (cons 'major-mode major-mode)
88 (setq cal (cons (cons 'mode mode) cal))
90 (setq ret (mime/get-content-decoding-alist cal))
91 (setq method (cdr (assq 'method ret)))
92 (cond ((and (symbolp method)
94 (funcall method beg end ret)
96 ((and (listp method)(stringp (car method)))
97 (mime-activate-external-method beg end ret)
100 (mime-show-echo-buffer
101 "No method are specified for %s\n" ctype)
107 ;;; @ method selector
110 (defun mime/get-content-decoding-alist (al)
111 (get-unified-alist mime-acting-condition al)
115 ;;; @ external decoder
118 (defun mime-activate-external-method (beg end cal)
121 (narrow-to-region beg end)
123 (let ((method (cdr (assoc 'method cal)))
124 (name (mime-article/get-filename cal))
127 (let ((file (make-temp-name
128 (expand-file-name "TM" mime-temp-directory)))
133 (if (re-search-forward "^$" nil t)
139 (write-region b end file)
140 (message "External method is starting...")
142 'name (replace-as-filename name) cal))
143 (setq cal (put-alist 'file file cal))
146 mime-echo-buffer-name (car method)
148 (mime-make-external-method-args
149 cal (cdr (cdr method)))
151 (apply (function start-process) args)
152 (mime-show-echo-buffer)
156 (defun mime-make-external-method-args (cal format)
161 (let* ((item (eval arg))
162 (ret (cdr (assoc item cal)))
166 (if (eq item 'encoding)
173 (defvar mime-echo-window-is-shared-with-bbdb t
174 "*If non-nil, mime-echo window is shared with BBDB window.")
176 (defvar mime-echo-window-height
179 (/ (window-height) 5)
181 "*Size of mime-echo window.
182 It allows function or integer. If it is function,
183 `mime-show-echo-buffer' calls it to get height of mime-echo window.
184 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
187 (defun mime-show-echo-buffer (&rest forms)
188 "Show mime-echo buffer to display MIME-playing information."
189 (get-buffer-create mime-echo-buffer-name)
190 (let ((the-win (selected-window))
191 (win (get-buffer-window mime-echo-buffer-name))
194 (if (and mime-echo-window-is-shared-with-bbdb
195 (boundp 'bbdb-buffer-name)
196 (setq win (get-buffer-window bbdb-buffer-name))
198 (set-window-buffer win mime-echo-buffer-name)
199 (select-window (get-buffer-window mime-preview-buffer))
200 (setq win (split-window-vertically
202 (if (functionp mime-echo-window-height)
203 (funcall mime-echo-window-height)
204 mime-echo-window-height)
206 (set-window-buffer win mime-echo-buffer-name)
209 (goto-char (point-max))
211 (insert (apply (function format) forms))
213 (select-window the-win)
220 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
222 (defvar mime-view-file-name-regexp-1
223 (concat mime-view-file-name-char-regexp "+\\."
224 mime-view-file-name-char-regexp "+"))
226 (defvar mime-view-file-name-regexp-2
227 (concat (regexp-* mime-view-file-name-char-regexp)
228 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
230 (defun mime-article/get-original-filename (param &optional encoding)
231 (or (mime-article/get-uu-filename param encoding)
233 (or (if (or (and (setq ret (mime/Content-Disposition))
234 (setq ret (assoc "filename" (cdr ret)))
236 (setq ret (assoc "name" param))
237 (setq ret (assoc "x-name" param))
239 (std11-strip-quoted-string (cdr ret))
242 (std11-find-field-body '("Content-Description"
244 (if (or (string-match mime-view-file-name-regexp-1 ret)
245 (string-match mime-view-file-name-regexp-2 ret))
246 (substring ret (match-beginning 0)(match-end 0))
251 (defun mime-article/get-filename (param)
252 (replace-as-filename (mime-article/get-original-filename param))
256 ;;; @ file extraction
259 (defun mime-method-to-save (beg end cal)
263 (narrow-to-region beg end)
264 (mime-article/get-filename cal)
266 (encoding (cdr (assq 'encoding cal)))
268 (if (and name (not (string-equal name "")))
269 (expand-file-name name
273 (interactive "DDirectory: ")
278 (interactive "FFilename: ")
279 (expand-file-name file))))))
281 (if (file-exists-p filename)
282 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
284 (re-search-forward "\n\n")
285 (mime-write-decoded-region (match-end 0)(point-max) filename encoding)
289 ;;; @ mail/news message
292 (defun mime-view-quitting-method-for-mime-show-message-mode ()
293 "Quitting method for mime-view.
294 It is registered to variable `mime-view-quitting-method-alist'."
295 (let ((mother mime-mother-buffer)
296 (win-conf mime-preview-original-window-configuration)
298 (kill-buffer mime-raw-buffer)
299 (mime-preview-kill-buffer)
300 (set-window-configuration win-conf)
301 (pop-to-buffer mother)
304 (defun mime-method-to-display-message/rfc822 (beg end cal)
305 (let* ((cnum (mime-article/point-content-number beg))
306 (new-name (format "%s-%s" (buffer-name) cnum))
307 (mother mime-preview-buffer)
309 (cdr (or (assq major-mode mime-text-decoder-alist)
310 (assq t mime-text-decoder-alist))))
312 (setq str (buffer-substring beg end))
313 (switch-to-buffer new-name)
316 (goto-char (point-min))
317 (if (re-search-forward "^\n" nil t)
318 (delete-region (point-min) (match-end 0))
320 (setq major-mode 'mime-show-message-mode)
321 (setq mime-text-decoder text-decoder)
322 (mime-view-mode mother)
326 ;;; @ message/partial
329 (defvar mime-article/coding-system-alist
330 (list '(mh-show-mode . no-conversion)
331 (cons t (mime-charset-to-coding-system default-mime-charset))
334 (defun mime-article::write-region (start end file)
335 (let ((coding-system-for-write
337 (or (assq major-mode mime-article/coding-system-alist)
338 (assq t mime-article/coding-system-alist)
340 (write-region start end file)
343 (defun mime-method-to-store-message/partial (beg end cal)
347 (concat "m-prts-" (user-login-name)) mime-temp-directory))
348 (id (cdr (assoc "id" cal)))
349 (number (cdr (assoc "number" cal)))
350 (total (cdr (assoc "total" cal)))
352 (mother mime-preview-buffer)
354 (or (file-exists-p root-dir)
355 (make-directory root-dir)
357 (setq id (replace-as-filename id))
358 (setq root-dir (concat root-dir "/" id))
359 (or (file-exists-p root-dir)
360 (make-directory root-dir)
362 (setq file (concat root-dir "/FULL"))
363 (if (file-exists-p file)
364 (let ((full-buf (get-buffer-create "FULL"))
365 (pwin (or (get-buffer-window mother)
366 (get-largest-window)))
368 (save-window-excursion
369 (set-buffer full-buf)
371 (as-binary-input-file (insert-file-contents file))
372 (setq major-mode 'mime-show-message-mode)
373 (mime-view-mode mother)
375 (set-window-buffer pwin
377 (set-buffer full-buf)
378 mime-preview-buffer))
381 (re-search-forward "^$")
382 (goto-char (1+ (match-end 0)))
383 (setq file (concat root-dir "/" number))
384 (mime-article::write-region (point) end file)
385 (let ((total-file (concat root-dir "/CT")))
389 (or (file-exists-p total-file)
392 (get-buffer-create mime-temp-buffer-name))
395 (write-region (point-min)(point-max) total-file)
396 (kill-buffer (current-buffer))
398 (string-to-number total)
400 (and (file-exists-p total-file)
402 (set-buffer (find-file-noselect total-file))
404 (and (re-search-forward "[0-9]+" nil t)
406 (buffer-substring (match-beginning 0)
409 (kill-buffer (current-buffer))
412 (if (and total (> total 0))
415 (set-buffer (get-buffer-create mime-temp-buffer-name))
416 (let ((full-buf (current-buffer)))
420 (setq file (concat root-dir "/" (int-to-string i)))
421 (or (file-exists-p file)
424 (as-binary-input-file (insert-file-contents file))
425 (goto-char (point-max))
428 (as-binary-output-file
429 (write-region (point-min)(point-max)
430 (expand-file-name "FULL" root-dir)))
433 (let ((file (format "%s/%d" root-dir i)))
434 (and (file-exists-p file)
439 (let ((file (expand-file-name "CT" root-dir)))
440 (and (file-exists-p file)
443 (save-window-excursion
444 (setq major-mode 'mime-show-message-mode)
445 (mime-view-mode mother)
447 (let ((pwin (or (get-buffer-window mother)
450 (pbuf (save-excursion
451 (set-buffer full-buf)
452 mime-preview-buffer)))
453 (set-window-buffer pwin pbuf)
459 ;;; @ message/external-body
462 (defvar mime-article/dired-function
463 (if mime/use-multi-frame
464 (function dired-other-frame)
465 (function mime-article/dired-function-for-one-frame)
468 (defun mime-article/dired-function-for-one-frame (dir)
469 (let ((win (or (get-buffer-window mime-preview-buffer)
470 (get-largest-window))))
475 (defun mime-method-to-display-message/external-ftp (beg end cal)
476 (let* ((site (cdr (assoc "site" cal)))
477 (directory (cdr (assoc "directory" cal)))
478 (name (cdr (assoc "name" cal)))
479 ;;(mode (cdr (assoc "mode" cal)))
480 (pathname (concat "/anonymous@" site ":" directory))
482 (message (concat "Accessing " (expand-file-name name pathname) "..."))
483 (funcall mime-article/dired-function pathname)
484 (goto-char (point-min))
485 (search-forward name)
492 (defun mime-method-to-display-caesar (start end cal)
493 "Internal method for mime-view to display ROT13-47-48 message."
494 (let* ((cnum (mime-article/point-content-number start))
495 (new-name (format "%s-%s" (buffer-name) cnum))
496 (the-buf (current-buffer))
497 (mother mime-preview-buffer)
498 (charset (cdr (assoc "charset" cal)))
499 (encoding (cdr (assq 'encoding cal)))
502 (let ((pwin (or (get-buffer-window mother)
503 (get-largest-window)))
504 (buf (get-buffer-create new-name))
506 (set-window-buffer pwin buf)
510 (setq buffer-read-only nil)
512 (insert-buffer-substring the-buf start end)
513 (goto-char (point-min))
514 (if (re-search-forward "^\n" nil t)
515 (delete-region (point-min) (match-end 0))
517 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
518 (assq t mime-text-decoder-alist)))))
520 (funcall m charset encoding)
522 (mule-caesar-region (point-min) (point-max))
523 (set-buffer-modified-p nil)
525 (view-buffer new-name)
534 ;;; mime-play.el ends here