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))
39 (defvar mime-preview-after-decoded-position nil)
41 (defun mime-preview-play-current-entity (&optional mode)
43 It decodes current entity to call internal or external method. The
44 method is selected from variable `mime-acting-condition'.
45 If MODE is specified, play as it. Default MODE is \"play\"."
49 (let ((entity-info (get-text-property (point) 'mime-view-entity)))
51 (let ((the-buf (current-buffer))
52 (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-raw-play-entity entity-info mode)
56 (when (eq (current-buffer) raw-buffer)
58 (goto-char mime-preview-after-decoded-position)
61 (defvar mime-acting-situation-examples nil)
63 (defun mime-raw-play-entity (entity-info &optional mode)
64 "Play entity specified by ENTITY-INFO.
65 It decodes the entity to call internal or external method. The method
66 is selected from variable `mime-acting-condition'. If MODE is
67 specified, play as it. Default MODE is \"play\"."
68 (let ((beg (mime-entity-point-min entity-info))
69 (end (mime-entity-point-max entity-info))
70 (c-type (mime-entity-media-type entity-info))
71 (c-subtype (mime-entity-media-subtype entity-info))
72 (params (mime-entity-parameters entity-info))
73 (encoding (mime-entity-encoding entity-info))
79 (if (< beg (point-min))
80 (setq beg (point-min))
82 (if (< (point-max) end)
83 (setq end (point-max))
86 (setq cal (list* (cons 'type c-type)
87 (cons 'subtype c-subtype)
88 (cons 'encoding encoding)
89 (cons 'major-mode major-mode)
92 (setq cal (cons (cons 'mode mode) cal))
95 (or (ctree-match-calist mime-acting-situation-examples cal)
96 (ctree-match-calist-partially mime-acting-situation-examples cal)
99 (or (ctree-find-calist mime-acting-condition ret
100 mime-view-find-every-acting-situation)
101 (ctree-find-calist mime-acting-condition cal
102 mime-view-find-every-acting-situation)
105 (setq ret (select-menu-alist
111 (cdr (assq 'method situation)))
114 (ctree-set-calist-strictly 'mime-acting-situation-examples ret)
119 (setq method (cdr (assq 'method ret)))
120 (cond ((and (symbolp method)
122 (funcall method beg end ret)
124 ((and (listp method)(stringp (car method)))
125 (mime-activate-external-method beg end ret)
128 (mime-show-echo-buffer
129 "No method are specified for %s\n"
130 (mime-type/subtype-string c-type c-subtype))
136 ;;; @ external decoder
139 (defun mime-activate-external-method (beg end cal)
142 (narrow-to-region beg end)
144 (let ((method (cdr (assoc 'method cal)))
145 (name (mime-raw-get-filename cal))
148 (let ((file (make-temp-name
149 (expand-file-name "TM" mime-temp-directory)))
154 (if (re-search-forward "^$" nil t)
160 (write-region b end file)
161 (message "External method is starting...")
163 'name (replace-as-filename name) cal))
164 (setq cal (put-alist 'file file cal))
167 mime-echo-buffer-name (car method)
169 (mime-make-external-method-args
170 cal (cdr (cdr method)))
172 (apply (function start-process) args)
173 (mime-show-echo-buffer)
177 (defun mime-make-external-method-args (cal format)
182 (let* ((item (eval arg))
183 (ret (cdr (assoc item cal)))
187 (if (eq item 'encoding)
194 (defvar mime-echo-window-is-shared-with-bbdb t
195 "*If non-nil, mime-echo window is shared with BBDB window.")
197 (defvar mime-echo-window-height
200 (/ (window-height) 5)
202 "*Size of mime-echo window.
203 It allows function or integer. If it is function,
204 `mime-show-echo-buffer' calls it to get height of mime-echo window.
205 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
208 (defun mime-show-echo-buffer (&rest forms)
209 "Show mime-echo buffer to display MIME-playing information."
210 (get-buffer-create mime-echo-buffer-name)
211 (let ((the-win (selected-window))
212 (win (get-buffer-window mime-echo-buffer-name))
215 (if (and mime-echo-window-is-shared-with-bbdb
216 (boundp 'bbdb-buffer-name)
217 (setq win (get-buffer-window bbdb-buffer-name))
219 (set-window-buffer win mime-echo-buffer-name)
220 (select-window (get-buffer-window mime-preview-buffer))
221 (setq win (split-window-vertically
223 (if (functionp mime-echo-window-height)
224 (funcall mime-echo-window-height)
225 mime-echo-window-height)
227 (set-window-buffer win mime-echo-buffer-name)
230 (goto-char (point-max))
232 (insert (apply (function format) forms))
234 (select-window the-win)
241 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
243 (defvar mime-view-file-name-regexp-1
244 (concat mime-view-file-name-char-regexp "+\\."
245 mime-view-file-name-char-regexp "+"))
247 (defvar mime-view-file-name-regexp-2
248 (concat (regexp-* mime-view-file-name-char-regexp)
249 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
251 (defun mime-raw-get-original-filename (param &optional encoding)
252 (or (mime-raw-get-uu-filename param encoding)
254 (or (if (or (and (setq ret (mime/Content-Disposition))
255 (setq ret (assoc "filename" (cdr ret)))
257 (setq ret (assoc "name" param))
258 (setq ret (assoc "x-name" param))
260 (std11-strip-quoted-string (cdr ret))
263 (std11-find-field-body '("Content-Description"
265 (if (or (string-match mime-view-file-name-regexp-1 ret)
266 (string-match mime-view-file-name-regexp-2 ret))
267 (substring ret (match-beginning 0)(match-end 0))
272 (defun mime-raw-get-filename (param)
273 (replace-as-filename (mime-raw-get-original-filename param))
277 ;;; @ file extraction
280 (defun mime-method-to-save (beg end cal)
284 (narrow-to-region beg end)
285 (mime-raw-get-filename cal)
287 (encoding (or (cdr (assq 'encoding cal)) "7bit"))
289 (if (and name (not (string-equal name "")))
290 (expand-file-name name
291 (save-window-excursion
295 (interactive "DDirectory: ")
297 (save-window-excursion
301 (interactive "FFilename: ")
302 (expand-file-name file)))))))
304 (if (file-exists-p filename)
305 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
307 (re-search-forward "\n\n")
308 (mime-write-decoded-region (match-end 0) end filename encoding)
312 ;;; @ mail/news message
315 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
316 "Quitting method for mime-view.
317 It is registered to variable `mime-preview-quitting-method-alist'."
318 (let ((mother mime-mother-buffer)
319 (win-conf mime-preview-original-window-configuration)
321 (kill-buffer mime-raw-buffer)
322 (mime-preview-kill-buffer)
323 (set-window-configuration win-conf)
324 (pop-to-buffer mother)
327 (defun mime-method-to-display-message/rfc822 (beg end cal)
328 (let* ((cnum (mime-raw-point-to-entity-number beg))
329 (new-name (format "%s-%s" (buffer-name) cnum))
330 (mother mime-preview-buffer)
332 (cdr (or (assq major-mode mime-text-decoder-alist)
333 (assq t mime-text-decoder-alist))))
335 (setq str (buffer-substring beg end))
336 (switch-to-buffer new-name)
339 (goto-char (point-min))
340 (if (re-search-forward "^\n" nil t)
341 (delete-region (point-min) (match-end 0))
343 (setq major-mode 'mime-show-message-mode)
344 (setq mime-text-decoder text-decoder)
345 (mime-view-mode mother)
349 ;;; @ message/partial
352 (defun mime-raw-write-region (start end filename)
353 "Write current region into specified file.
354 When called from a program, takes three arguments:
355 START, END and FILENAME. START and END are buffer positions.
356 It refer `mime-raw-buffer-coding-system-alist' to choose coding-system
358 (let ((coding-system-for-write
360 (or (assq major-mode mime-raw-buffer-coding-system-alist)
361 (assq t mime-raw-buffer-coding-system-alist)
363 (write-region start end filename)
366 (defun mime-method-to-store-message/partial (beg end cal)
370 (concat "m-prts-" (user-login-name)) mime-temp-directory))
371 (id (cdr (assoc "id" cal)))
372 (number (cdr (assoc "number" cal)))
373 (total (cdr (assoc "total" cal)))
375 (mother mime-preview-buffer)
377 (or (file-exists-p root-dir)
378 (make-directory root-dir)
380 (setq id (replace-as-filename id))
381 (setq root-dir (concat root-dir "/" id))
382 (or (file-exists-p root-dir)
383 (make-directory root-dir)
385 (setq file (concat root-dir "/FULL"))
386 (if (file-exists-p file)
387 (let ((full-buf (get-buffer-create "FULL"))
388 (pwin (or (get-buffer-window mother)
389 (get-largest-window)))
391 (save-window-excursion
392 (set-buffer full-buf)
394 (as-binary-input-file (insert-file-contents file))
395 (setq major-mode 'mime-show-message-mode)
396 (mime-view-mode mother)
398 (set-window-buffer pwin
400 (set-buffer full-buf)
401 mime-preview-buffer))
404 (re-search-forward "^$")
405 (goto-char (1+ (match-end 0)))
406 (setq file (concat root-dir "/" number))
407 (mime-raw-write-region (point) end file)
408 (let ((total-file (concat root-dir "/CT")))
412 (or (file-exists-p total-file)
415 (get-buffer-create mime-temp-buffer-name))
418 (write-region (point-min)(point-max) total-file)
419 (kill-buffer (current-buffer))
421 (string-to-number total)
423 (and (file-exists-p total-file)
425 (set-buffer (find-file-noselect total-file))
427 (and (re-search-forward "[0-9]+" nil t)
429 (buffer-substring (match-beginning 0)
432 (kill-buffer (current-buffer))
435 (if (and total (> total 0))
438 (set-buffer (get-buffer-create mime-temp-buffer-name))
439 (let ((full-buf (current-buffer)))
443 (setq file (concat root-dir "/" (int-to-string i)))
444 (or (file-exists-p file)
447 (as-binary-input-file (insert-file-contents file))
448 (goto-char (point-max))
451 (as-binary-output-file
452 (write-region (point-min)(point-max)
453 (expand-file-name "FULL" root-dir)))
456 (let ((file (format "%s/%d" root-dir i)))
457 (and (file-exists-p file)
462 (let ((file (expand-file-name "CT" root-dir)))
463 (and (file-exists-p file)
466 (save-window-excursion
467 (setq major-mode 'mime-show-message-mode)
468 (mime-view-mode mother)
470 (let ((pwin (or (get-buffer-window mother)
473 (pbuf (save-excursion
474 (set-buffer full-buf)
475 mime-preview-buffer)))
476 (set-window-buffer pwin pbuf)
482 ;;; @ message/external-body
485 (defvar mime-raw-dired-function
486 (if mime/use-multi-frame
487 (function dired-other-frame)
488 (function mime-raw-dired-function-for-one-frame)
491 (defun mime-raw-dired-function-for-one-frame (dir)
492 (let ((win (or (get-buffer-window mime-preview-buffer)
493 (get-largest-window))))
498 (defun mime-method-to-display-message/external-ftp (beg end cal)
499 (let* ((site (cdr (assoc "site" cal)))
500 (directory (cdr (assoc "directory" cal)))
501 (name (cdr (assoc "name" cal)))
502 ;;(mode (cdr (assoc "mode" cal)))
503 (pathname (concat "/anonymous@" site ":" directory))
505 (message (concat "Accessing " (expand-file-name name pathname) "..."))
506 (funcall mime-raw-dired-function pathname)
507 (goto-char (point-min))
508 (search-forward name)
515 (defun mime-method-to-display-caesar (start end cal)
516 "Internal method for mime-view to display ROT13-47-48 message."
517 (let* ((cnum (mime-raw-point-to-entity-number start))
518 (new-name (format "%s-%s" (buffer-name) cnum))
519 (the-buf (current-buffer))
520 (mother mime-preview-buffer)
521 (charset (cdr (assoc "charset" cal)))
522 (encoding (cdr (assq 'encoding cal)))
525 (let ((pwin (or (get-buffer-window mother)
526 (get-largest-window)))
527 (buf (get-buffer-create new-name))
529 (set-window-buffer pwin buf)
533 (setq buffer-read-only nil)
535 (insert-buffer-substring the-buf start end)
536 (goto-char (point-min))
537 (if (re-search-forward "^\n" nil t)
538 (delete-region (point-min) (match-end 0))
540 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
541 (assq t mime-text-decoder-alist)))))
543 (funcall m charset encoding)
545 (mule-caesar-region (point-min) (point-max))
546 (set-buffer-modified-p nil)
548 (view-buffer new-name)
557 (let* ((file mime-acting-situation-examples-file)
558 (buffer (get-buffer-create " *mime-example*")))
559 (if (file-readable-p file)
564 (insert-file-contents file)
565 (eval-current-buffer))
566 (kill-buffer buffer))))
568 ;;; mime-play.el ends here