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)))
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-point-min entity-info))
65 (end (mime-entity-point-max entity-info))
66 (c-type (mime-entity-media-type entity-info))
67 (c-subtype (mime-entity-media-subtype entity-info))
68 (params (mime-entity-parameters entity-info))
69 (encoding (mime-entity-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"
102 (mime-type/subtype-string c-type c-subtype))
108 ;;; @ method selector
111 (defun mime/get-content-decoding-alist (al)
112 (get-unified-alist mime-acting-condition al)
116 ;;; @ external decoder
119 (defun mime-activate-external-method (beg end cal)
122 (narrow-to-region beg end)
124 (let ((method (cdr (assoc 'method cal)))
125 (name (mime-raw-get-filename cal))
128 (let ((file (make-temp-name
129 (expand-file-name "TM" mime-temp-directory)))
134 (if (re-search-forward "^$" nil t)
140 (write-region b end file)
141 (message "External method is starting...")
143 'name (replace-as-filename name) cal))
144 (setq cal (put-alist 'file file cal))
147 mime-echo-buffer-name (car method)
149 (mime-make-external-method-args
150 cal (cdr (cdr method)))
152 (apply (function start-process) args)
153 (mime-show-echo-buffer)
157 (defun mime-make-external-method-args (cal format)
162 (let* ((item (eval arg))
163 (ret (cdr (assoc item cal)))
167 (if (eq item 'encoding)
174 (defvar mime-echo-window-is-shared-with-bbdb t
175 "*If non-nil, mime-echo window is shared with BBDB window.")
177 (defvar mime-echo-window-height
180 (/ (window-height) 5)
182 "*Size of mime-echo window.
183 It allows function or integer. If it is function,
184 `mime-show-echo-buffer' calls it to get height of mime-echo window.
185 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
188 (defun mime-show-echo-buffer (&rest forms)
189 "Show mime-echo buffer to display MIME-playing information."
190 (get-buffer-create mime-echo-buffer-name)
191 (let ((the-win (selected-window))
192 (win (get-buffer-window mime-echo-buffer-name))
195 (if (and mime-echo-window-is-shared-with-bbdb
196 (boundp 'bbdb-buffer-name)
197 (setq win (get-buffer-window bbdb-buffer-name))
199 (set-window-buffer win mime-echo-buffer-name)
200 (select-window (get-buffer-window mime-preview-buffer))
201 (setq win (split-window-vertically
203 (if (functionp mime-echo-window-height)
204 (funcall mime-echo-window-height)
205 mime-echo-window-height)
207 (set-window-buffer win mime-echo-buffer-name)
210 (goto-char (point-max))
212 (insert (apply (function format) forms))
214 (select-window the-win)
221 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
223 (defvar mime-view-file-name-regexp-1
224 (concat mime-view-file-name-char-regexp "+\\."
225 mime-view-file-name-char-regexp "+"))
227 (defvar mime-view-file-name-regexp-2
228 (concat (regexp-* mime-view-file-name-char-regexp)
229 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
231 (defun mime-raw-get-original-filename (param &optional encoding)
232 (or (mime-raw-get-uu-filename param encoding)
234 (or (if (or (and (setq ret (mime/Content-Disposition))
235 (setq ret (assoc "filename" (cdr ret)))
237 (setq ret (assoc "name" param))
238 (setq ret (assoc "x-name" param))
240 (std11-strip-quoted-string (cdr ret))
243 (std11-find-field-body '("Content-Description"
245 (if (or (string-match mime-view-file-name-regexp-1 ret)
246 (string-match mime-view-file-name-regexp-2 ret))
247 (substring ret (match-beginning 0)(match-end 0))
252 (defun mime-raw-get-filename (param)
253 (replace-as-filename (mime-raw-get-original-filename param))
257 ;;; @ file extraction
260 (defun mime-method-to-save (beg end cal)
264 (narrow-to-region beg end)
265 (mime-raw-get-filename cal)
267 (encoding (cdr (assq 'encoding cal)))
269 (if (and name (not (string-equal name "")))
270 (expand-file-name name
274 (interactive "DDirectory: ")
279 (interactive "FFilename: ")
280 (expand-file-name file))))))
282 (if (file-exists-p filename)
283 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
285 (re-search-forward "\n\n")
286 (mime-write-decoded-region (match-end 0) end filename encoding)
290 ;;; @ mail/news message
293 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
294 "Quitting method for mime-view.
295 It is registered to variable `mime-preview-quitting-method-alist'."
296 (let ((mother mime-mother-buffer)
297 (win-conf mime-preview-original-window-configuration)
299 (kill-buffer mime-raw-buffer)
300 (mime-preview-kill-buffer)
301 (set-window-configuration win-conf)
302 (pop-to-buffer mother)
305 (defun mime-method-to-display-message/rfc822 (beg end cal)
306 (let* ((cnum (mime-raw-point-to-entity-number beg))
307 (new-name (format "%s-%s" (buffer-name) cnum))
308 (mother mime-preview-buffer)
310 (cdr (or (assq major-mode mime-text-decoder-alist)
311 (assq t mime-text-decoder-alist))))
313 (setq str (buffer-substring beg end))
314 (switch-to-buffer new-name)
317 (goto-char (point-min))
318 (if (re-search-forward "^\n" nil t)
319 (delete-region (point-min) (match-end 0))
321 (setq major-mode 'mime-show-message-mode)
322 (setq mime-text-decoder text-decoder)
323 (mime-view-mode mother)
327 ;;; @ message/partial
330 (defun mime-raw-write-region (start end filename)
331 "Write current region into specified file.
332 When called from a program, takes three arguments:
333 START, END and FILENAME. START and END are buffer positions.
334 It refer `mime-raw-buffer-coding-system-alist' to choose coding-system
336 (let ((coding-system-for-write
338 (or (assq major-mode mime-raw-buffer-coding-system-alist)
339 (assq t mime-raw-buffer-coding-system-alist)
341 (write-region start end filename)
344 (defun mime-method-to-store-message/partial (beg end cal)
348 (concat "m-prts-" (user-login-name)) mime-temp-directory))
349 (id (cdr (assoc "id" cal)))
350 (number (cdr (assoc "number" cal)))
351 (total (cdr (assoc "total" cal)))
353 (mother mime-preview-buffer)
355 (or (file-exists-p root-dir)
356 (make-directory root-dir)
358 (setq id (replace-as-filename id))
359 (setq root-dir (concat root-dir "/" id))
360 (or (file-exists-p root-dir)
361 (make-directory root-dir)
363 (setq file (concat root-dir "/FULL"))
364 (if (file-exists-p file)
365 (let ((full-buf (get-buffer-create "FULL"))
366 (pwin (or (get-buffer-window mother)
367 (get-largest-window)))
369 (save-window-excursion
370 (set-buffer full-buf)
372 (as-binary-input-file (insert-file-contents file))
373 (setq major-mode 'mime-show-message-mode)
374 (mime-view-mode mother)
376 (set-window-buffer pwin
378 (set-buffer full-buf)
379 mime-preview-buffer))
382 (re-search-forward "^$")
383 (goto-char (1+ (match-end 0)))
384 (setq file (concat root-dir "/" number))
385 (mime-raw-write-region (point) end file)
386 (let ((total-file (concat root-dir "/CT")))
390 (or (file-exists-p total-file)
393 (get-buffer-create mime-temp-buffer-name))
396 (write-region (point-min)(point-max) total-file)
397 (kill-buffer (current-buffer))
399 (string-to-number total)
401 (and (file-exists-p total-file)
403 (set-buffer (find-file-noselect total-file))
405 (and (re-search-forward "[0-9]+" nil t)
407 (buffer-substring (match-beginning 0)
410 (kill-buffer (current-buffer))
413 (if (and total (> total 0))
416 (set-buffer (get-buffer-create mime-temp-buffer-name))
417 (let ((full-buf (current-buffer)))
421 (setq file (concat root-dir "/" (int-to-string i)))
422 (or (file-exists-p file)
425 (as-binary-input-file (insert-file-contents file))
426 (goto-char (point-max))
429 (as-binary-output-file
430 (write-region (point-min)(point-max)
431 (expand-file-name "FULL" root-dir)))
434 (let ((file (format "%s/%d" root-dir i)))
435 (and (file-exists-p file)
440 (let ((file (expand-file-name "CT" root-dir)))
441 (and (file-exists-p file)
444 (save-window-excursion
445 (setq major-mode 'mime-show-message-mode)
446 (mime-view-mode mother)
448 (let ((pwin (or (get-buffer-window mother)
451 (pbuf (save-excursion
452 (set-buffer full-buf)
453 mime-preview-buffer)))
454 (set-window-buffer pwin pbuf)
460 ;;; @ message/external-body
463 (defvar mime-raw-dired-function
464 (if mime/use-multi-frame
465 (function dired-other-frame)
466 (function mime-raw-dired-function-for-one-frame)
469 (defun mime-raw-dired-function-for-one-frame (dir)
470 (let ((win (or (get-buffer-window mime-preview-buffer)
471 (get-largest-window))))
476 (defun mime-method-to-display-message/external-ftp (beg end cal)
477 (let* ((site (cdr (assoc "site" cal)))
478 (directory (cdr (assoc "directory" cal)))
479 (name (cdr (assoc "name" cal)))
480 ;;(mode (cdr (assoc "mode" cal)))
481 (pathname (concat "/anonymous@" site ":" directory))
483 (message (concat "Accessing " (expand-file-name name pathname) "..."))
484 (funcall mime-raw-dired-function pathname)
485 (goto-char (point-min))
486 (search-forward name)
493 (defun mime-method-to-display-caesar (start end cal)
494 "Internal method for mime-view to display ROT13-47-48 message."
495 (let* ((cnum (mime-raw-point-to-entity-number start))
496 (new-name (format "%s-%s" (buffer-name) cnum))
497 (the-buf (current-buffer))
498 (mother mime-preview-buffer)
499 (charset (cdr (assoc "charset" cal)))
500 (encoding (cdr (assq 'encoding cal)))
503 (let ((pwin (or (get-buffer-window mother)
504 (get-largest-window)))
505 (buf (get-buffer-create new-name))
507 (set-window-buffer pwin buf)
511 (setq buffer-read-only nil)
513 (insert-buffer-substring the-buf start end)
514 (goto-char (point-min))
515 (if (re-search-forward "^\n" nil t)
516 (delete-region (point-min) (match-end 0))
518 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
519 (assq t mime-text-decoder-alist)))))
521 (funcall m charset encoding)
523 (mule-caesar-region (point-min) (point-max))
524 (set-buffer-modified-p nil)
526 (view-buffer new-name)
535 ;;; mime-play.el ends here