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\"."
47 (let ((entity-info (get-text-property (point) 'mime-view-entity-info)))
49 (let ((the-buf (current-buffer))
50 (raw-buffer (get-text-property (point) 'mime-view-raw-buffer)))
51 (setq mime-preview-after-decoded-position (point))
52 (set-buffer raw-buffer)
53 (mime-raw-play-entity entity-info mode)
54 (when (eq (current-buffer) raw-buffer)
56 (goto-char mime-preview-after-decoded-position)
59 (defun mime-raw-play-entity (entity-info &optional mode)
60 "Play entity specified by ENTITY-INFO.
61 It decodes the entity to call internal or external method. The method
62 is selected from variable `mime-acting-condition'. If MODE is
63 specified, play as it. Default MODE is \"play\"."
64 (let ((beg (mime-entity-info-point-min entity-info))
65 (end (mime-entity-info-point-max entity-info))
66 (c-type (mime-entity-info-media-type entity-info))
67 (c-subtype (mime-entity-info-media-subtype entity-info))
68 (params (mime-entity-info-parameters entity-info))
69 (encoding (mime-entity-info-encoding entity-info))
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-raw-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-raw-get-original-filename (param &optional encoding)
231 (or (mime-raw-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-raw-get-filename (param)
252 (replace-as-filename (mime-raw-get-original-filename param))
256 ;;; @ file extraction
259 (defun mime-method-to-save (beg end cal)
263 (narrow-to-region beg end)
264 (mime-raw-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-preview-quitting-method-for-mime-show-message-mode ()
293 "Quitting method for mime-view.
294 It is registered to variable `mime-preview-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-raw-point-to-entity-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-raw-coding-system-alist
330 `((mh-show-mode . no-conversion)
331 (t . ,(mime-charset-to-coding-system default-mime-charset)))
332 "Alist of major-mode vs. corresponding coding-system.")
334 (defun mime-raw-write-region (start end filename)
335 "Write current region into specified file.
336 When called from a program, takes three arguments:
337 START, END and FILENAME. START and END are buffer positions.
338 It refer `mime-raw-coding-system-alist' to choose coding-system to
340 (let ((coding-system-for-write
342 (or (assq major-mode mime-raw-coding-system-alist)
343 (assq t mime-raw-coding-system-alist)
345 (write-region start end filename)
348 (defun mime-method-to-store-message/partial (beg end cal)
352 (concat "m-prts-" (user-login-name)) mime-temp-directory))
353 (id (cdr (assoc "id" cal)))
354 (number (cdr (assoc "number" cal)))
355 (total (cdr (assoc "total" cal)))
357 (mother mime-preview-buffer)
359 (or (file-exists-p root-dir)
360 (make-directory root-dir)
362 (setq id (replace-as-filename id))
363 (setq root-dir (concat root-dir "/" id))
364 (or (file-exists-p root-dir)
365 (make-directory root-dir)
367 (setq file (concat root-dir "/FULL"))
368 (if (file-exists-p file)
369 (let ((full-buf (get-buffer-create "FULL"))
370 (pwin (or (get-buffer-window mother)
371 (get-largest-window)))
373 (save-window-excursion
374 (set-buffer full-buf)
376 (as-binary-input-file (insert-file-contents file))
377 (setq major-mode 'mime-show-message-mode)
378 (mime-view-mode mother)
380 (set-window-buffer pwin
382 (set-buffer full-buf)
383 mime-preview-buffer))
386 (re-search-forward "^$")
387 (goto-char (1+ (match-end 0)))
388 (setq file (concat root-dir "/" number))
389 (mime-raw-write-region (point) end file)
390 (let ((total-file (concat root-dir "/CT")))
394 (or (file-exists-p total-file)
397 (get-buffer-create mime-temp-buffer-name))
400 (write-region (point-min)(point-max) total-file)
401 (kill-buffer (current-buffer))
403 (string-to-number total)
405 (and (file-exists-p total-file)
407 (set-buffer (find-file-noselect total-file))
409 (and (re-search-forward "[0-9]+" nil t)
411 (buffer-substring (match-beginning 0)
414 (kill-buffer (current-buffer))
417 (if (and total (> total 0))
420 (set-buffer (get-buffer-create mime-temp-buffer-name))
421 (let ((full-buf (current-buffer)))
425 (setq file (concat root-dir "/" (int-to-string i)))
426 (or (file-exists-p file)
429 (as-binary-input-file (insert-file-contents file))
430 (goto-char (point-max))
433 (as-binary-output-file
434 (write-region (point-min)(point-max)
435 (expand-file-name "FULL" root-dir)))
438 (let ((file (format "%s/%d" root-dir i)))
439 (and (file-exists-p file)
444 (let ((file (expand-file-name "CT" root-dir)))
445 (and (file-exists-p file)
448 (save-window-excursion
449 (setq major-mode 'mime-show-message-mode)
450 (mime-view-mode mother)
452 (let ((pwin (or (get-buffer-window mother)
455 (pbuf (save-excursion
456 (set-buffer full-buf)
457 mime-preview-buffer)))
458 (set-window-buffer pwin pbuf)
464 ;;; @ message/external-body
467 (defvar mime-raw-dired-function
468 (if mime/use-multi-frame
469 (function dired-other-frame)
470 (function mime-raw-dired-function-for-one-frame)
473 (defun mime-raw-dired-function-for-one-frame (dir)
474 (let ((win (or (get-buffer-window mime-preview-buffer)
475 (get-largest-window))))
480 (defun mime-method-to-display-message/external-ftp (beg end cal)
481 (let* ((site (cdr (assoc "site" cal)))
482 (directory (cdr (assoc "directory" cal)))
483 (name (cdr (assoc "name" cal)))
484 ;;(mode (cdr (assoc "mode" cal)))
485 (pathname (concat "/anonymous@" site ":" directory))
487 (message (concat "Accessing " (expand-file-name name pathname) "..."))
488 (funcall mime-raw-dired-function pathname)
489 (goto-char (point-min))
490 (search-forward name)
497 (defun mime-method-to-display-caesar (start end cal)
498 "Internal method for mime-view to display ROT13-47-48 message."
499 (let* ((cnum (mime-raw-point-to-entity-number start))
500 (new-name (format "%s-%s" (buffer-name) cnum))
501 (the-buf (current-buffer))
502 (mother mime-preview-buffer)
503 (charset (cdr (assoc "charset" cal)))
504 (encoding (cdr (assq 'encoding cal)))
507 (let ((pwin (or (get-buffer-window mother)
508 (get-largest-window)))
509 (buf (get-buffer-create new-name))
511 (set-window-buffer pwin buf)
515 (setq buffer-read-only nil)
517 (insert-buffer-substring the-buf start end)
518 (goto-char (point-min))
519 (if (re-search-forward "^\n" nil t)
520 (delete-region (point-min) (match-end 0))
522 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
523 (assq t mime-text-decoder-alist)))))
525 (funcall m charset encoding)
527 (mule-caesar-region (point-min) (point-max))
528 (set-buffer-modified-p nil)
530 (view-buffer new-name)
539 ;;; mime-play.el ends here