update.
[elisp/semi.git] / mime-play.el
1 ;;; mime-play.el --- Playback processing module for mime-view.el
2
3 ;; Copyright (C) 1994,1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
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
9
10 ;; This file is part of SEMI (Secretariat of Emacs MIME Interfaces).
11
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.
16
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.
21
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.
26
27 ;;; Code:
28
29 (require 'mime-view)
30 (require 'alist)
31 (require 'filename)
32
33 (eval-when-compile
34   (condition-case nil
35       (require 'bbdb)
36     (error (defvar bbdb-buffer-name nil)))
37   )
38
39 (defcustom mime-save-directory "~/"
40   "*Name of the directory where MIME entity will be saved in.
41 If t, it means current directory."
42   :group 'mime-view
43   :type '(choice (const :tag "Current directory" t)
44                  (directory)))
45
46 (defvar mime-acting-situation-example-list nil)
47
48 (defvar mime-acting-situation-example-list-max-size 16)
49
50 (defun mime-save-acting-situation-examples ()
51   (let* ((file mime-acting-situation-examples-file)
52          (buffer (get-buffer-create " *mime-example*")))
53     (unwind-protect
54         (save-excursion
55           (set-buffer buffer)
56           (setq buffer-file-name file)
57           (erase-buffer)
58           (insert ";;; " (file-name-nondirectory file) "\n")
59           (insert "\n;; This file is generated automatically by "
60                   mime-view-version "\n\n")
61           (insert ";;; Code:\n\n")
62           (pp `(setq mime-acting-situation-example-list
63                      ',mime-acting-situation-example-list)
64               (current-buffer))
65           (insert "\n;;; "
66                   (file-name-nondirectory file)
67                   " ends here.\n")
68           (save-buffer))
69       (kill-buffer buffer))))
70
71 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
72
73 (defun mime-reduce-acting-situation-examples ()
74   (let ((len (length mime-acting-situation-example-list))
75         i ir ic j jr jc ret
76         dest d-i d-j
77         (max-sim 0) sim
78         min-det-ret det-ret
79         min-det-org det-org
80         min-freq freq)
81     (setq i 0
82           ir mime-acting-situation-example-list)
83     (while (< i len)
84       (setq ic (car ir)
85             j 0
86             jr mime-acting-situation-example-list)
87       (while (< j len)
88         (unless (= i j)
89           (setq jc (car jr))
90           (setq ret (mime-compare-situation-with-example (car ic)(car jc))
91                 sim (car ret)
92                 det-ret (+ (length (car ic))(length (car jc)))
93                 det-org (length (cdr ret))
94                 freq (+ (cdr ic)(cdr jc)))
95           (cond ((< max-sim sim)
96                  (setq max-sim sim
97                        min-det-ret det-ret
98                        min-det-org det-org
99                        min-freq freq
100                        d-i i
101                        d-j j
102                        dest (cons (cdr ret) freq))
103                  )
104                 ((= max-sim sim)
105                  (cond ((> min-det-ret det-ret)
106                         (setq min-det-ret det-ret
107                               min-det-org det-org
108                               min-freq freq
109                               d-i i
110                               d-j j
111                               dest (cons (cdr ret) freq))
112                         )
113                        ((= min-det-ret det-ret)
114                         (cond ((> min-det-org det-org)
115                                (setq min-det-org det-org
116                                      min-freq freq
117                                      d-i i
118                                      d-j j
119                                      dest (cons (cdr ret) freq))
120                                )
121                               ((= min-det-org det-org)
122                                (cond ((> min-freq freq)
123                                       (setq min-freq freq
124                                             d-i i
125                                             d-j j
126                                             dest (cons (cdr ret) freq))
127                                       ))
128                                ))
129                         ))
130                  ))
131           )
132         (setq jr (cdr jr)
133               j (1+ j)))
134       (setq ir (cdr ir)
135             i (1+ i)))
136     (if (> d-i d-j)
137         (setq i d-i
138               d-i d-j
139               d-j i))
140     (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
141     (setcdr jr (cddr jr))
142     (if (= d-i 0)
143         (setq mime-acting-situation-example-list
144               (cdr mime-acting-situation-example-list))
145       (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
146       (setcdr ir (cddr ir))
147       )
148     (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
149         (setcdr ir (+ (cdr ir)(cdr dest)))
150       (setq mime-acting-situation-example-list
151             (cons dest mime-acting-situation-example-list))
152       )))
153
154
155 ;;; @ content decoder
156 ;;;
157
158 ;;;###autoload
159 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
160   "Play current entity.
161 It decodes current entity to call internal or external method.  The
162 method is selected from variable `mime-acting-condition'.
163 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
164 `mime-acting-situation-example-list'.
165 If MODE is specified, play as it.  Default MODE is \"play\"."
166   (interactive "P")
167   (let ((entity (get-text-property (point) 'mime-view-entity)))
168     (if entity
169         (let ((situation
170                (get-text-property (point) 'mime-view-situation)))
171           (or mode
172               (setq mode "play"))
173           (setq situation 
174                 (if (assq 'mode situation)
175                     (put-alist 'mode mode (copy-alist situation))
176                   (cons (cons 'mode mode)
177                         situation)))
178           (if ignore-examples
179               (setq situation
180                     (cons (cons 'ignore-examples ignore-examples)
181                           situation)))
182           (mime-play-entity entity situation)
183           ))))
184
185 (defun mime-sort-situation (situation)
186   (sort situation
187         #'(lambda (a b)
188             (let ((a-t (car a))
189                   (b-t (car b))
190                   (order '((type . 1)
191                            (subtype . 2)
192                            (mode . 3)
193                            (method . 4)
194                            (major-mode . 5)
195                            (disposition-type . 6)
196                            ))
197                   a-order b-order)
198               (if (symbolp a-t)
199                   (let ((ret (assq a-t order)))
200                     (if ret
201                         (setq a-order (cdr ret))
202                       (setq a-order 7)
203                       ))
204                 (setq a-order 8)
205                 )
206               (if (symbolp b-t)
207                   (let ((ret (assq b-t order)))
208                     (if ret
209                         (setq b-order (cdr ret))
210                       (setq b-order 7)
211                       ))
212                 (setq b-order 8)
213                 )
214               (if (= a-order b-order)
215                   (string< (format "%s" a-t)(format "%s" b-t))
216                 (< a-order b-order))
217               )))
218   )
219
220 (defsubst mime-delq-null-situation (situations field
221                                                &optional ignored-value)
222   (let (dest)
223     (while situations
224       (let* ((situation (car situations))
225              (cell (assq field situation)))
226         (if cell
227             (or (eq (cdr cell) ignored-value)
228                 (setq dest (cons situation dest))
229                 )))
230       (setq situations (cdr situations)))
231     dest))
232
233 (defun mime-compare-situation-with-example (situation example)
234   (let ((example (copy-alist example))
235         (match 0))
236     (while situation
237       (let* ((cell (car situation))
238              (key (car cell))
239              (ecell (assoc key example)))
240         (when ecell
241           (if (equal cell ecell)
242               (setq match (1+ match))
243             (setq example (delq ecell example))
244             ))
245         )
246       (setq situation (cdr situation))
247       )
248     (cons match example)
249     ))
250
251 ;;;###autoload
252 (defun mime-play-entity (entity &optional situation ignored-method)
253   "Play entity specified by ENTITY.
254 It decodes the entity to call internal or external method.  The method
255 is selected from variable `mime-acting-condition'.  If MODE is
256 specified, play as it.  Default MODE is \"play\"."
257   (let (method ret)
258     (in-calist-package 'mime-view)
259     (setq ret
260           (mime-delq-null-situation
261            (ctree-find-calist mime-acting-condition
262                               (mime-entity-situation entity situation)
263                               mime-view-find-every-acting-situation)
264            'method ignored-method))
265     (or (assq 'ignore-examples situation)
266         (if (cdr ret)
267             (let ((rest ret)
268                   (max-score 0)
269                   (max-escore 0)
270                   max-examples
271                   max-situations)
272               (while rest
273                 (let ((situation (car rest))
274                       (examples mime-acting-situation-example-list))
275                   (while examples
276                     (let* ((ret
277                             (mime-compare-situation-with-example
278                              situation (caar examples)))
279                            (ret-score (car ret)))
280                       (cond ((> ret-score max-score)
281                              (setq max-score ret-score
282                                    max-escore (cdar examples)
283                                    max-examples (list (cdr ret))
284                                    max-situations (list situation))
285                              )
286                             ((= ret-score max-score)
287                              (cond ((> (cdar examples) max-escore)
288                                     (setq max-escore (cdar examples)
289                                           max-examples (list (cdr ret))
290                                           max-situations (list situation))
291                                     )
292                                    ((= (cdar examples) max-escore)
293                                     (setq max-examples
294                                           (cons (cdr ret) max-examples))
295                                     (or (member situation max-situations)
296                                         (setq max-situations
297                                               (cons situation max-situations)))
298                                     )))))
299                     (setq examples (cdr examples))))
300                 (setq rest (cdr rest)))
301               (when max-situations
302                 (setq ret max-situations)
303                 (while max-examples
304                   (let* ((example (car max-examples))
305                          (cell
306                           (assoc example mime-acting-situation-example-list)))
307                     (if cell
308                         (setcdr cell (1+ (cdr cell)))
309                       (setq mime-acting-situation-example-list
310                             (cons (cons example 0)
311                                   mime-acting-situation-example-list))
312                       ))
313                   (setq max-examples (cdr max-examples))
314                   )))))
315     (cond ((cdr ret)
316            (setq ret (select-menu-alist
317                       "Methods"
318                       (mapcar (function
319                                (lambda (situation)
320                                  (cons
321                                   (format "%s"
322                                           (cdr (assq 'method situation)))
323                                   situation)))
324                               ret)))
325            (setq ret (mime-sort-situation ret))
326            (add-to-list 'mime-acting-situation-example-list (cons ret 0))
327            )
328           (t
329            (setq ret (car ret))
330            ))
331     (setq method (cdr (assq 'method ret)))
332     (cond ((and (symbolp method)
333                 (fboundp method))
334            (funcall method entity ret)
335            )
336           ((stringp method)
337            (mime-activate-mailcap-method entity ret)
338            )
339           ;; ((and (listp method)(stringp (car method)))
340           ;;  (mime-activate-external-method entity ret)
341           ;;  )
342           (t
343            (mime-show-echo-buffer "No method are specified for %s\n"
344                                   (mime-type/subtype-string
345                                    (cdr (assq 'type situation))
346                                    (cdr (assq 'subtype situation))))
347            (if (y-or-n-p "Do you want to save current entity to disk?")
348                (mime-save-content entity situation))
349            ))
350     ))
351
352
353 ;;; @ external decoder
354 ;;;
355
356 (defvar mime-mailcap-method-filename-alist nil)
357
358 (defun mime-activate-mailcap-method (entity situation)
359   (let ((method (cdr (assoc 'method situation)))
360         (name (mime-entity-safe-filename entity)))
361     (setq name
362           (if (and name (not (string= name "")))
363               (expand-file-name name temporary-file-directory)
364             (make-temp-name
365              (expand-file-name "EMI" temporary-file-directory))
366             ))
367     (mime-write-entity-content entity name)
368     (message "External method is starting...")
369     (let ((process
370            (let ((command
371                   (mailcap-format-command
372                    method
373                    (cons (cons 'filename name) situation))))
374              (start-process command mime-echo-buffer-name
375                             shell-file-name shell-command-switch command)
376              )))
377       (set-alist 'mime-mailcap-method-filename-alist process name)
378       (set-process-sentinel process 'mime-mailcap-method-sentinel)
379       )
380     ))
381
382 (defun mime-mailcap-method-sentinel (process event)
383   (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
384     (if (file-exists-p file)
385         (delete-file file)
386       ))
387   (remove-alist 'mime-mailcap-method-filename-alist process)
388   (message (format "%s %s" process event)))
389
390 (defvar mime-echo-window-is-shared-with-bbdb
391   (module-installed-p 'bbdb)
392   "*If non-nil, mime-echo window is shared with BBDB window.")
393
394 (defvar mime-echo-window-height
395   (function
396    (lambda ()
397      (/ (window-height) 5)
398      ))
399   "*Size of mime-echo window.
400 It allows function or integer.  If it is function,
401 `mime-show-echo-buffer' calls it to get height of mime-echo window.
402 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
403 window.")
404
405 (defun mime-show-echo-buffer (&rest forms)
406   "Show mime-echo buffer to display MIME-playing information."
407   (get-buffer-create mime-echo-buffer-name)
408   (let ((the-win (selected-window))
409         (win (get-buffer-window mime-echo-buffer-name)))
410     (unless win
411       (unless (and mime-echo-window-is-shared-with-bbdb
412                    (condition-case nil
413                        (setq win (get-buffer-window bbdb-buffer-name))
414                      (error nil)))
415         (select-window (get-buffer-window (or mime-preview-buffer
416                                               (current-buffer))))
417         (setq win (split-window-vertically
418                    (- (window-height)
419                       (if (functionp mime-echo-window-height)
420                           (funcall mime-echo-window-height)
421                         mime-echo-window-height)
422                       )))
423         )
424       (set-window-buffer win mime-echo-buffer-name)
425       )
426     (select-window win)
427     (goto-char (point-max))
428     (if forms
429         (let ((buffer-read-only nil))
430           (insert (apply (function format) forms))
431           ))
432     (select-window the-win)
433     ))
434
435
436 ;;; @ file name
437 ;;;
438
439 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
440
441 (defvar mime-view-file-name-regexp-1
442   (concat mime-view-file-name-char-regexp "+\\."
443           mime-view-file-name-char-regexp "+"))
444
445 (defvar mime-view-file-name-regexp-2
446   (concat (regexp-* mime-view-file-name-char-regexp)
447           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
448
449 (defun mime-entity-safe-filename (entity)
450   (let ((filename
451          (or (mime-entity-filename entity)
452              (let ((subj
453                     (or (mime-entity-read-field entity 'Content-Description)
454                         (mime-entity-read-field entity 'Subject))))
455                (if (and subj
456                         (or (string-match mime-view-file-name-regexp-1 subj)
457                             (string-match mime-view-file-name-regexp-2 subj)))
458                    (substring subj (match-beginning 0)(match-end 0))
459                  )))))
460     (if filename
461         (replace-as-filename filename)
462       )))
463
464
465 ;;; @ file extraction
466 ;;;
467
468 (defun mime-save-content (entity situation)
469   (let ((name (or (mime-entity-safe-filename entity)
470                   (format "%s" (mime-entity-media-type entity))))
471         (dir (if (eq t mime-save-directory)
472                  default-directory
473                mime-save-directory))
474         filename)
475     (setq filename (read-file-name
476                     (concat "File name: (default "
477                             (file-name-nondirectory name) ") ")
478                     dir
479                     (concat (file-name-as-directory dir)
480                             (file-name-nondirectory name))))
481     (if (file-directory-p filename)
482         (setq filename (concat (file-name-as-directory filename)
483                                (file-name-nondirectory name))))
484     (if (file-exists-p filename)
485         (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
486             (error "")))
487     (mime-write-entity-content entity (expand-file-name filename))
488     ))
489
490
491 ;;; @ file detection
492 ;;;
493
494 (defvar mime-magic-type-alist
495   '(("^\377\330\377[\340\356]..JFIF"    image jpeg)
496     ("^\211PNG"                         image png)
497     ("^GIF8[79]"                        image gif)
498     ("^II\\*\000"                       image tiff)
499     ("^MM\000\\*"                       image tiff)
500     ("^MThd"                            audio midi)
501     ("^\000\000\001\263"                video mpeg)
502     )
503   "*Alist of regexp about magic-number vs. corresponding media-types.
504 Each element looks like (REGEXP TYPE SUBTYPE).
505 REGEXP is a regular expression to match against the beginning of the
506 content of entity.
507 TYPE is symbol to indicate primary type of media-type.
508 SUBTYPE is symbol to indicate subtype of media-type.")
509
510 (defun mime-detect-content (entity situation)
511   (let (type subtype)
512     (let ((mdata (mime-entity-content entity))
513           (rest mime-magic-type-alist))
514       (while (not (let ((cell (car rest)))
515                     (if cell
516                         (if (string-match (car cell) mdata)
517                             (setq type (nth 1 cell)
518                                   subtype (nth 2 cell))
519                           )
520                       t)))
521         (setq rest (cdr rest))))
522     (setq situation (del-alist 'method (copy-alist situation)))
523     (mime-play-entity entity
524                       (if type
525                           (put-alist 'type type
526                                      (put-alist 'subtype subtype
527                                                 situation))
528                         situation)
529                       'mime-detect-content)))
530
531
532 ;;; @ mail/news message
533 ;;;
534
535 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
536   "Quitting method for mime-view.
537 It is registered to variable `mime-preview-quitting-method-alist'."
538   (let ((raw-buffer (mime-entity-buffer
539                      (get-text-property (point-min) 'mime-view-entity)))
540         (mother mime-mother-buffer)
541         (win-conf mime-preview-original-window-configuration))
542     (kill-buffer raw-buffer)
543     (mime-preview-kill-buffer)
544     (set-window-configuration win-conf)
545     (pop-to-buffer mother)
546     ))
547
548 (defun mime-view-message/rfc822 (entity situation)
549   (let* ((new-name
550           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
551          (mother (current-buffer))
552          (children (car (mime-entity-children entity)))
553          (preview-buffer
554           (mime-display-message
555            children new-name mother nil
556            (cdr (assq 'major-mode
557                       (get-text-property (point) 'mime-view-situation))))))
558     (or (get-buffer-window preview-buffer)
559         (let ((m-win (get-buffer-window mother)))
560           (if m-win
561               (set-window-buffer m-win preview-buffer)
562             (switch-to-buffer preview-buffer)
563             )))))
564
565
566 ;;; @ message/partial
567 ;;;
568
569 (defun mime-store-message/partial-piece (entity cal)
570   (let* ((root-dir
571           (expand-file-name
572            (concat "m-prts-" (user-login-name)) temporary-file-directory))
573          (id (cdr (assoc "id" cal)))
574          (number (cdr (assoc "number" cal)))
575          (total (cdr (assoc "total" cal)))
576          file
577          (mother (current-buffer)))
578     (or (file-exists-p root-dir)
579         (make-directory root-dir))
580     (setq id (replace-as-filename id))
581     (setq root-dir (concat root-dir "/" id))
582     (or (file-exists-p root-dir)
583         (make-directory root-dir))
584     (setq file (concat root-dir "/FULL"))
585     (if (file-exists-p file)
586         (let ((full-buf (get-buffer-create "FULL"))
587               (pwin (or (get-buffer-window mother)
588                         (get-largest-window)))
589               pbuf)
590           (save-window-excursion
591             (set-buffer full-buf)
592             (erase-buffer)
593             (as-binary-input-file (insert-file-contents file))
594             (setq major-mode 'mime-show-message-mode)
595             (mime-view-buffer (current-buffer) nil mother)
596             (setq pbuf (current-buffer))
597             )
598           (set-window-buffer pwin pbuf)
599           (select-window pwin)
600           )
601       (setq file (concat root-dir "/" number))
602       (mime-write-entity-body entity file)
603       (let ((total-file (concat root-dir "/CT")))
604         (setq total
605               (if total
606                   (progn
607                     (or (file-exists-p total-file)
608                         (save-excursion
609                           (set-buffer
610                            (get-buffer-create mime-temp-buffer-name))
611                           (erase-buffer)
612                           (insert total)
613                           (write-region (point-min)(point-max) total-file)
614                           (kill-buffer (current-buffer))
615                           ))
616                     (string-to-number total)
617                     )
618                 (and (file-exists-p total-file)
619                      (save-excursion
620                        (set-buffer (find-file-noselect total-file))
621                        (prog1
622                            (and (re-search-forward "[0-9]+" nil t)
623                                 (string-to-number
624                                  (buffer-substring (match-beginning 0)
625                                                    (match-end 0)))
626                                 )
627                          (kill-buffer (current-buffer))
628                          )))
629                 )))
630       (if (and total (> total 0)
631                (>= (length (directory-files root-dir nil "^[0-9]+$" t))
632                    total))
633           (catch 'tag
634             (save-excursion
635               (set-buffer (get-buffer-create mime-temp-buffer-name))
636               (let ((full-buf (current-buffer)))
637                 (erase-buffer)
638                 (let ((i 1))
639                   (while (<= i total)
640                     (setq file (concat root-dir "/" (int-to-string i)))
641                     (or (file-exists-p file)
642                         (throw 'tag nil)
643                         )
644                     (as-binary-input-file (insert-file-contents file))
645                     (goto-char (point-max))
646                     (setq i (1+ i))
647                     ))
648                 (as-binary-output-file
649                  (write-region (point-min)(point-max)
650                                (expand-file-name "FULL" root-dir)))
651                 (let ((i 1))
652                   (while (<= i total)
653                     (let ((file (format "%s/%d" root-dir i)))
654                       (and (file-exists-p file)
655                            (delete-file file)
656                            ))
657                     (setq i (1+ i))
658                     ))
659                 (let ((file (expand-file-name "CT" root-dir)))
660                   (and (file-exists-p file)
661                        (delete-file file)
662                        ))
663                 (let ((pwin (or (get-buffer-window mother)
664                                 (get-largest-window)))
665                       (pbuf (mime-display-message
666                              (mime-open-entity 'buffer (current-buffer))
667                              nil mother nil 'mime-show-message-mode)))
668                   (set-window-buffer pwin pbuf)
669                   (select-window pwin)
670                   )))))
671       )))
672
673
674 ;;; @ message/external-body
675 ;;;
676
677 (defvar mime-raw-dired-function
678   (if (and (>= emacs-major-version 19) window-system)
679       (function dired-other-frame)
680     (function mime-raw-dired-function-for-one-frame)
681     ))
682
683 (defun mime-raw-dired-function-for-one-frame (dir)
684   (let ((win (or (get-buffer-window mime-preview-buffer)
685                  (get-largest-window))))
686     (select-window win)
687     (dired dir)
688     ))
689
690 (defun mime-view-message/external-anon-ftp (entity cal)
691   (let* ((site (cdr (assoc "site" cal)))
692          (directory (cdr (assoc "directory" cal)))
693          (name (cdr (assoc "name" cal)))
694          (pathname (concat "/anonymous@" site ":" directory)))
695     (message (concat "Accessing " (expand-file-name name pathname) " ..."))
696     (funcall mime-raw-dired-function pathname)
697     (goto-char (point-min))
698     (search-forward name)
699     ))
700
701 (defvar mime-raw-browse-url-function mime-browse-url-function)
702
703 (defun mime-view-message/external-url (entity cal)
704   (let ((url (cdr (assoc "url" cal))))
705     (message (concat "Accessing " url " ..."))
706     (funcall mime-raw-browse-url-function url)))
707
708
709 ;;; @ rot13-47
710 ;;;
711
712 (defun mime-view-caesar (entity situation)
713   "Internal method for mime-view to display ROT13-47-48 message."
714   (let ((buf (get-buffer-create
715               (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
716     (with-current-buffer buf
717       (setq buffer-read-only nil)
718       (erase-buffer)
719       (mime-insert-text-content entity)
720       (mule-caesar-region (point-min) (point-max))
721       (set-buffer-modified-p nil)
722       )
723     (let ((win (get-buffer-window (current-buffer))))
724       (or (eq (selected-window) win)
725           (select-window (or win (get-largest-window)))
726           ))
727     (view-buffer buf)
728     (goto-char (point-min))
729     ))
730
731
732 ;;; @ end
733 ;;;
734
735 (provide 'mime-play)
736
737 (let* ((file mime-acting-situation-examples-file)
738        (buffer (get-buffer-create " *mime-example*")))
739   (if (file-readable-p file)
740       (unwind-protect
741           (save-excursion
742             (set-buffer buffer)
743             (erase-buffer)
744             (insert-file-contents file)
745             (eval-buffer)
746             ;; format check
747             (condition-case nil
748                 (let ((i 0))
749                   (while (and (> (length mime-acting-situation-example-list)
750                                  mime-acting-situation-example-list-max-size)
751                               (< i 16))
752                     (mime-reduce-acting-situation-examples)
753                     (setq i (1+ i))
754                     ))
755               (error (setq mime-acting-situation-example-list nil)))
756             )
757         (kill-buffer buffer))))
758
759 ;;; mime-play.el ends here