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.
33 (eval-when-compile (require 'mime-text))
36 (defvar mime-acting-situation-examples nil)
38 (defun mime-save-acting-situation-examples ()
39 (let* ((file mime-acting-situation-examples-file)
40 (buffer (get-buffer-create " *mime-example*")))
44 (setq buffer-file-name file)
46 (insert ";;; " (file-name-nondirectory file) "\n")
47 (insert "\n;; This file is generated automatically by "
48 mime-view-version-string "\n\n")
49 (insert ";;; Code:\n\n")
50 (pp `(setq mime-acting-situation-examples
51 ',mime-acting-situation-examples)
54 (file-name-nondirectory file)
57 (kill-buffer buffer))))
59 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
65 (defvar mime-preview-after-decoded-position nil)
67 (defun mime-preview-play-current-entity (&optional mode)
69 It decodes current entity to call internal or external method. The
70 method is selected from variable `mime-acting-condition'.
71 If MODE is specified, play as it. Default MODE is \"play\"."
75 (let ((entity-info (get-text-property (point) 'mime-view-entity)))
77 (let ((the-buf (current-buffer))
78 (raw-buffer (get-text-property (point) 'mime-view-raw-buffer)))
79 (setq mime-preview-after-decoded-position (point))
80 (set-buffer raw-buffer)
81 (mime-raw-play-entity entity-info mode)
82 (when (eq (current-buffer) raw-buffer)
84 (goto-char mime-preview-after-decoded-position)
87 (defun mime-sort-situation (situation)
98 (let ((ret (assq a-t order)))
100 (setq a-order (cdr ret))
106 (let ((ret (assq b-t order)))
108 (setq b-order (cdr ret))
113 (if (= a-order b-order)
114 (string< (format "%s" a-t)(format "%s" b-t))
119 (defun mime-raw-play-entity (entity-info &optional mode)
120 "Play entity specified by ENTITY-INFO.
121 It decodes the entity to call internal or external method. The method
122 is selected from variable `mime-acting-condition'. If MODE is
123 specified, play as it. Default MODE is \"play\"."
124 (let ((beg (mime-entity-point-min entity-info))
125 (end (mime-entity-point-max entity-info))
126 (c-type (mime-entity-media-type entity-info))
127 (c-subtype (mime-entity-media-subtype entity-info))
128 (params (mime-entity-parameters entity-info))
129 (encoding (mime-entity-encoding entity-info))
135 (if (< beg (point-min))
136 (setq beg (point-min))
138 (if (< (point-max) end)
139 (setq end (point-max))
141 (let (method cal ret)
142 (setq cal (list* (cons 'type c-type)
143 (cons 'subtype c-subtype)
144 (cons 'encoding encoding)
145 (cons 'major-mode major-mode)
148 (setq cal (cons (cons 'mode mode) cal))
151 (or (ctree-match-calist mime-acting-situation-examples cal)
152 (ctree-match-calist-partially mime-acting-situation-examples
156 (or (ctree-find-calist mime-acting-condition ret
157 mime-view-find-every-acting-situation)
158 (ctree-find-calist mime-acting-condition cal
159 mime-view-find-every-acting-situation)
162 (setq ret (select-menu-alist
168 (cdr (assq 'method situation)))
171 (setq ret (mime-sort-situation ret))
172 (ctree-set-calist-strictly 'mime-acting-situation-examples ret)
177 (setq method (cdr (assq 'method ret)))
178 (cond ((and (symbolp method)
180 (funcall method beg end ret)
183 (mime-activate-mailcap-method beg end ret)
185 ((and (listp method)(stringp (car method)))
186 (mime-activate-external-method beg end ret)
189 (mime-show-echo-buffer
190 "No method are specified for %s\n"
191 (mime-type/subtype-string c-type c-subtype))
197 ;;; @ external decoder
200 (defun mime-activate-mailcap-method (start end situation)
203 (narrow-to-region start end)
205 (let ((method (cdr (assoc 'method situation)))
206 (name (mime-raw-get-filename situation)))
207 (mime-write-decoded-region (if (re-search-forward "^$" end t)
211 (cdr (assq 'encoding situation)))
212 (message "External method is starting...")
214 (mailcap-format-command
216 (cons (cons 'filename name) situation))))
217 (start-process command mime-echo-buffer-name
218 shell-file-name shell-command-switch command)
220 (mime-show-echo-buffer)
223 (defun mime-activate-external-method (beg end cal)
226 (narrow-to-region beg end)
228 (let ((method (cdr (assoc 'method cal)))
229 (name (mime-raw-get-filename cal))
232 (let ((file (make-temp-name
233 (expand-file-name "TM" mime-temp-directory)))
238 (if (re-search-forward "^$" nil t)
244 (write-region b end file)
245 (message "External method is starting...")
247 'name (replace-as-filename name) cal))
248 (setq cal (put-alist 'file file cal))
251 mime-echo-buffer-name (car method)
253 (mime-make-external-method-args
254 cal (cdr (cdr method)))
256 (apply (function start-process) args)
257 (mime-show-echo-buffer)
261 (defun mime-make-external-method-args (cal format)
266 (let* ((item (eval arg))
267 (ret (cdr (assoc item cal)))
271 (if (eq item 'encoding)
278 (defvar mime-echo-window-is-shared-with-bbdb t
279 "*If non-nil, mime-echo window is shared with BBDB window.")
281 (defvar mime-echo-window-height
284 (/ (window-height) 5)
286 "*Size of mime-echo window.
287 It allows function or integer. If it is function,
288 `mime-show-echo-buffer' calls it to get height of mime-echo window.
289 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
292 (defun mime-show-echo-buffer (&rest forms)
293 "Show mime-echo buffer to display MIME-playing information."
294 (get-buffer-create mime-echo-buffer-name)
295 (let ((the-win (selected-window))
296 (win (get-buffer-window mime-echo-buffer-name))
299 (if (and mime-echo-window-is-shared-with-bbdb
300 (boundp 'bbdb-buffer-name)
301 (setq win (get-buffer-window bbdb-buffer-name))
303 (set-window-buffer win mime-echo-buffer-name)
304 (select-window (get-buffer-window mime-preview-buffer))
305 (setq win (split-window-vertically
307 (if (functionp mime-echo-window-height)
308 (funcall mime-echo-window-height)
309 mime-echo-window-height)
311 (set-window-buffer win mime-echo-buffer-name)
314 (goto-char (point-max))
316 (insert (apply (function format) forms))
318 (select-window the-win)
325 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
327 (defvar mime-view-file-name-regexp-1
328 (concat mime-view-file-name-char-regexp "+\\."
329 mime-view-file-name-char-regexp "+"))
331 (defvar mime-view-file-name-regexp-2
332 (concat (regexp-* mime-view-file-name-char-regexp)
333 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
335 (defun mime-raw-get-original-filename (param &optional encoding)
336 (or (mime-raw-get-uu-filename param encoding)
338 (or (if (or (and (setq ret (mime/Content-Disposition))
339 (setq ret (assoc "filename" (cdr ret)))
341 (setq ret (assoc "name" param))
342 (setq ret (assoc "x-name" param))
344 (std11-strip-quoted-string (cdr ret))
347 (std11-find-field-body '("Content-Description"
349 (if (or (string-match mime-view-file-name-regexp-1 ret)
350 (string-match mime-view-file-name-regexp-2 ret))
351 (substring ret (match-beginning 0)(match-end 0))
356 (defun mime-raw-get-filename (param)
357 (replace-as-filename (mime-raw-get-original-filename param))
361 ;;; @ file extraction
364 (defun mime-method-to-save (beg end cal)
368 (narrow-to-region beg end)
369 (mime-raw-get-filename cal)
371 (encoding (or (cdr (assq 'encoding cal)) "7bit"))
373 (if (and name (not (string-equal name "")))
374 (expand-file-name name
375 (save-window-excursion
379 (interactive "DDirectory: ")
381 (save-window-excursion
385 (interactive "FFilename: ")
386 (expand-file-name file)))))))
388 (if (file-exists-p filename)
389 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
391 (re-search-forward "\n\n")
392 (mime-write-decoded-region (match-end 0) end filename encoding)
396 ;;; @ mail/news message
399 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
400 "Quitting method for mime-view.
401 It is registered to variable `mime-preview-quitting-method-alist'."
402 (let ((mother mime-mother-buffer)
403 (win-conf mime-preview-original-window-configuration)
405 (kill-buffer mime-raw-buffer)
406 (mime-preview-kill-buffer)
407 (set-window-configuration win-conf)
408 (pop-to-buffer mother)
411 (defun mime-method-to-display-message/rfc822 (beg end cal)
412 (let* ((cnum (mime-raw-point-to-entity-number beg))
413 (new-name (format "%s-%s" (buffer-name) cnum))
414 (mother mime-preview-buffer)
416 (cdr (or (assq major-mode mime-text-decoder-alist)
417 (assq t mime-text-decoder-alist))))
419 (setq str (buffer-substring beg end))
420 (switch-to-buffer new-name)
423 (goto-char (point-min))
424 (if (re-search-forward "^\n" nil t)
425 (delete-region (point-min) (match-end 0))
427 (setq major-mode 'mime-show-message-mode)
428 (setq mime-text-decoder text-decoder)
429 (mime-view-mode mother)
433 ;;; @ message/partial
436 (defun mime-raw-write-region (start end filename)
437 "Write current region into specified file.
438 When called from a program, takes three arguments:
439 START, END and FILENAME. START and END are buffer positions.
440 It refer `mime-raw-buffer-coding-system-alist' to choose coding-system
442 (let ((coding-system-for-write
444 (or (assq major-mode mime-raw-buffer-coding-system-alist)
445 (assq t mime-raw-buffer-coding-system-alist)
447 (write-region start end filename)
450 (defun mime-method-to-store-message/partial (beg end cal)
454 (concat "m-prts-" (user-login-name)) mime-temp-directory))
455 (id (cdr (assoc "id" cal)))
456 (number (cdr (assoc "number" cal)))
457 (total (cdr (assoc "total" cal)))
459 (mother mime-preview-buffer)
461 (or (file-exists-p root-dir)
462 (make-directory root-dir)
464 (setq id (replace-as-filename id))
465 (setq root-dir (concat root-dir "/" id))
466 (or (file-exists-p root-dir)
467 (make-directory root-dir)
469 (setq file (concat root-dir "/FULL"))
470 (if (file-exists-p file)
471 (let ((full-buf (get-buffer-create "FULL"))
472 (pwin (or (get-buffer-window mother)
473 (get-largest-window)))
475 (save-window-excursion
476 (set-buffer full-buf)
478 (as-binary-input-file (insert-file-contents file))
479 (setq major-mode 'mime-show-message-mode)
480 (mime-view-mode mother)
482 (set-window-buffer pwin
484 (set-buffer full-buf)
485 mime-preview-buffer))
488 (re-search-forward "^$")
489 (goto-char (1+ (match-end 0)))
490 (setq file (concat root-dir "/" number))
491 (mime-raw-write-region (point) end file)
492 (let ((total-file (concat root-dir "/CT")))
496 (or (file-exists-p total-file)
499 (get-buffer-create mime-temp-buffer-name))
502 (write-region (point-min)(point-max) total-file)
503 (kill-buffer (current-buffer))
505 (string-to-number total)
507 (and (file-exists-p total-file)
509 (set-buffer (find-file-noselect total-file))
511 (and (re-search-forward "[0-9]+" nil t)
513 (buffer-substring (match-beginning 0)
516 (kill-buffer (current-buffer))
519 (if (and total (> total 0))
522 (set-buffer (get-buffer-create mime-temp-buffer-name))
523 (let ((full-buf (current-buffer)))
527 (setq file (concat root-dir "/" (int-to-string i)))
528 (or (file-exists-p file)
531 (as-binary-input-file (insert-file-contents file))
532 (goto-char (point-max))
535 (as-binary-output-file
536 (write-region (point-min)(point-max)
537 (expand-file-name "FULL" root-dir)))
540 (let ((file (format "%s/%d" root-dir i)))
541 (and (file-exists-p file)
546 (let ((file (expand-file-name "CT" root-dir)))
547 (and (file-exists-p file)
550 (save-window-excursion
551 (setq major-mode 'mime-show-message-mode)
552 (mime-view-mode mother)
554 (let ((pwin (or (get-buffer-window mother)
557 (pbuf (save-excursion
558 (set-buffer full-buf)
559 mime-preview-buffer)))
560 (set-window-buffer pwin pbuf)
566 ;;; @ message/external-body
569 (defvar mime-raw-dired-function
570 (if mime/use-multi-frame
571 (function dired-other-frame)
572 (function mime-raw-dired-function-for-one-frame)
575 (defun mime-raw-dired-function-for-one-frame (dir)
576 (let ((win (or (get-buffer-window mime-preview-buffer)
577 (get-largest-window))))
582 (defun mime-method-to-display-message/external-ftp (beg end cal)
583 (let* ((site (cdr (assoc "site" cal)))
584 (directory (cdr (assoc "directory" cal)))
585 (name (cdr (assoc "name" cal)))
586 ;;(mode (cdr (assoc "mode" cal)))
587 (pathname (concat "/anonymous@" site ":" directory))
589 (message (concat "Accessing " (expand-file-name name pathname) "..."))
590 (funcall mime-raw-dired-function pathname)
591 (goto-char (point-min))
592 (search-forward name)
599 (defun mime-method-to-display-caesar (start end cal)
600 "Internal method for mime-view to display ROT13-47-48 message."
601 (let* ((cnum (mime-raw-point-to-entity-number start))
602 (new-name (format "%s-%s" (buffer-name) cnum))
603 (the-buf (current-buffer))
604 (mother mime-preview-buffer)
605 (charset (cdr (assoc "charset" cal)))
606 (encoding (cdr (assq 'encoding cal)))
609 (let ((pwin (or (get-buffer-window mother)
610 (get-largest-window)))
611 (buf (get-buffer-create new-name))
613 (set-window-buffer pwin buf)
617 (setq buffer-read-only nil)
619 (insert-buffer-substring the-buf start end)
620 (goto-char (point-min))
621 (if (re-search-forward "^\n" nil t)
622 (delete-region (point-min) (match-end 0))
624 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
625 (assq t mime-text-decoder-alist)))))
627 (funcall m charset encoding)
629 (mule-caesar-region (point-min) (point-max))
630 (set-buffer-modified-p nil)
632 (view-buffer new-name)
641 (let* ((file mime-acting-situation-examples-file)
642 (buffer (get-buffer-create " *mime-example*")))
643 (if (file-readable-p file)
648 (insert-file-contents file)
649 (eval-current-buffer)
651 (or (eq (car mime-acting-situation-examples) 'type)
652 (setq mime-acting-situation-examples nil))
654 (kill-buffer buffer))))
656 ;;; mime-play.el ends here