1 ;;; mime-play.el --- decoder for mime-view.el
3 ;; Copyright (C) 1994,1995,1996,1997 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 ;; Version: $Id: mime-play.el,v 0.27 1997-03-18 14:46:40 morioka Exp $
9 ;; Keywords: MIME, multimedia, mail, news
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
38 (defvar mime-preview/after-decoded-position nil)
40 (defun mime-view-play-current-entity (&optional mode)
42 It decodes current entity to call internal or external method. The
43 method is selected from variable `mime/content-decoding-condition'.
44 If MODE is specified, play as it. Default MODE is \"play\"."
49 (let ((cinfo (get-text-property (point) 'mime-view-cinfo)))
51 (let ((the-buf (current-buffer))
52 (raw-buffer (get-text-property (point) 'mime-view-raw-buffer))
54 (setq mime-preview/after-decoded-position (point))
55 (set-buffer raw-buffer)
56 (mime-article/decode-content cinfo mode)
57 (if (eq (current-buffer) raw-buffer)
60 (goto-char mime-preview/after-decoded-position)
64 (defun mime-article/decode-content (cinfo &optional mode)
65 (let ((beg (mime::content-info/point-min cinfo))
66 (end (mime::content-info/point-max cinfo))
67 (ctype (or (mime::content-info/type cinfo) "text/plain"))
68 (params (mime::content-info/parameters cinfo))
69 (encoding (mime::content-info/encoding cinfo))
72 (if (< beg (point-min))
73 (setq beg (point-min))
75 (if (< (point-max) end)
76 (setq end (point-max))
79 (setq cal (list* (cons 'type ctype)
80 (cons 'encoding encoding)
81 (cons 'major-mode major-mode)
84 (setq cal (cons (cons 'mode mode) cal))
86 (setq ret (mime/get-content-decoding-alist cal))
87 (setq method (cdr (assq 'method ret)))
88 (cond ((and (symbolp method)
90 (funcall method beg end ret)
92 ((and (listp method)(stringp (car method)))
93 (mime-article/start-external-method-region beg end ret)
96 (mime-article/show-output-buffer
97 "No method are specified for %s\n" ctype)
103 ;;; @ method selector
106 (defun mime/get-content-decoding-alist (al)
107 (get-unified-alist mime/content-decoding-condition al)
111 ;;; @ external decoder
114 (defun mime-article/start-external-method-region (beg end cal)
117 (narrow-to-region beg end)
119 (let ((method (cdr (assoc 'method cal)))
120 (name (mime-article/get-filename cal))
123 (let ((file (make-temp-name
124 (expand-file-name "TM" mime-temp-directory)))
129 (if (re-search-forward "^$" nil t)
135 (write-region b end file)
136 (message "External method is starting...")
138 'name (replace-as-filename name) cal))
139 (setq cal (put-alist 'file file cal))
142 mime/output-buffer-name (car method)
144 (mime-article/make-method-args cal
147 (apply (function start-process) args)
148 (mime-article/show-output-buffer)
152 (defun mime-article/make-method-args (cal format)
157 (let* ((item (eval arg))
158 (ret (cdr (assoc item cal)))
162 (if (eq item 'encoding)
169 (defun mime-article/show-output-buffer (&rest forms)
170 (get-buffer-create mime/output-buffer-name)
171 (let ((the-win (selected-window))
172 (win (get-buffer-window mime/output-buffer-name))
175 (if (and mime/output-buffer-window-is-shared-with-bbdb
176 (boundp 'bbdb-buffer-name)
177 (setq win (get-buffer-window bbdb-buffer-name))
179 (set-window-buffer win mime/output-buffer-name)
180 (select-window (get-buffer-window mime-view-buffer))
181 (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
182 (set-window-buffer win mime/output-buffer-name)
185 (goto-char (point-max))
187 (insert (apply (function format) forms))
189 (select-window the-win)
196 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
198 (defvar mime-view-file-name-regexp-1
199 (concat mime-view-file-name-char-regexp "+\\."
200 mime-view-file-name-char-regexp "+"))
202 (defvar mime-view-file-name-regexp-2
203 (concat (regexp-* mime-view-file-name-char-regexp)
204 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
206 (defun mime-article/get-original-filename (param &optional encoding)
207 (or (mime-article/get-uu-filename param encoding)
209 (or (if (or (and (setq ret (mime/Content-Disposition))
210 (setq ret (assoc "filename" (cdr ret)))
212 (setq ret (assoc "name" param))
213 (setq ret (assoc "x-name" param))
215 (std11-strip-quoted-string (cdr ret))
218 (std11-find-field-body '("Content-Description"
220 (if (or (string-match mime-view-file-name-regexp-1 ret)
221 (string-match mime-view-file-name-regexp-2 ret))
222 (substring ret (match-beginning 0)(match-end 0))
227 (defun mime-article/get-filename (param)
228 (replace-as-filename (mime-article/get-original-filename param))
232 ;;; @ mail/news message
235 (defun mime-view-quitting-method-for-mime-show-message-mode ()
236 "Quitting method for mime-view.
237 It is registered to variable `mime-view-quitting-method-alist'."
238 (let ((mother mime::preview/mother-buffer)
239 (win-conf mime::preview/original-window-configuration)
241 (kill-buffer mime::preview/article-buffer)
242 (mime-view-kill-buffer)
243 (set-window-configuration win-conf)
244 (pop-to-buffer mother)
247 (defun mime-article/view-message/rfc822 (beg end cal)
248 (let* ((cnum (mime-article/point-content-number beg))
249 (new-name (format "%s-%s" (buffer-name) cnum))
250 (mother mime-view-buffer)
252 (cdr (or (assq major-mode mime-text-decoder-alist)
253 (assq t mime-text-decoder-alist))))
255 (setq str (buffer-substring beg end))
256 (switch-to-buffer new-name)
259 (goto-char (point-min))
260 (if (re-search-forward "^\n" nil t)
261 (delete-region (point-min) (match-end 0))
263 (setq major-mode 'mime-show-message-mode)
264 (setq mime-text-decoder text-decoder)
265 (mime-view-mode mother)
269 ;;; @ message/partial
272 (defvar mime-article/coding-system-alist
273 (list '(mh-show-mode . no-conversion)
274 (cons t (mime-charset-to-coding-system default-mime-charset))
277 (defun mime-article::write-region (start end file)
278 (let ((coding-system-for-write
280 (or (assq major-mode mime-article/coding-system-alist)
281 (assq t mime-article/coding-system-alist)
283 (write-region start end file)
286 (defun mime-article/decode-message/partial (beg end cal)
290 (concat "m-prts-" (user-login-name)) mime-temp-directory))
291 (id (cdr (assoc "id" cal)))
292 (number (cdr (assoc "number" cal)))
293 (total (cdr (assoc "total" cal)))
295 (mother mime-view-buffer)
297 (or (file-exists-p root-dir)
298 (make-directory root-dir)
300 (setq id (replace-as-filename id))
301 (setq root-dir (concat root-dir "/" id))
302 (or (file-exists-p root-dir)
303 (make-directory root-dir)
305 (setq file (concat root-dir "/FULL"))
306 (if (file-exists-p file)
307 (let ((full-buf (get-buffer-create "FULL"))
308 (pwin (or (get-buffer-window mother)
309 (get-largest-window)))
311 (save-window-excursion
312 (set-buffer full-buf)
314 (as-binary-input-file (insert-file-contents file))
315 (setq major-mode 'mime-show-message-mode)
316 (mime-view-mode mother)
318 (set-window-buffer pwin
320 (set-buffer full-buf)
324 (re-search-forward "^$")
325 (goto-char (1+ (match-end 0)))
326 (setq file (concat root-dir "/" number))
327 (mime-article::write-region (point) (point-max) file)
328 (let ((total-file (concat root-dir "/CT")))
332 (or (file-exists-p total-file)
335 (get-buffer-create mime/temp-buffer-name))
338 (write-file total-file)
339 (kill-buffer (current-buffer))
341 (string-to-number total)
343 (and (file-exists-p total-file)
345 (set-buffer (find-file-noselect total-file))
347 (and (re-search-forward "[0-9]+" nil t)
349 (buffer-substring (match-beginning 0)
352 (kill-buffer (current-buffer))
355 (if (and total (> total 0))
358 (set-buffer (get-buffer-create mime/temp-buffer-name))
359 (let ((full-buf (current-buffer)))
363 (setq file (concat root-dir "/" (int-to-string i)))
364 (or (file-exists-p file)
367 (as-binary-input-file (insert-file-contents file))
368 (goto-char (point-max))
371 (as-binary-output-file (write-file (concat root-dir "/FULL")))
374 (let ((file (format "%s/%d" root-dir i)))
375 (and (file-exists-p file)
380 (let ((file (expand-file-name "CT" root-dir)))
381 (and (file-exists-p file)
384 (save-window-excursion
385 (setq major-mode 'mime-show-message-mode)
386 (mime-view-mode mother)
388 (let ((pwin (or (get-buffer-window mother)
391 (pbuf (save-excursion
392 (set-buffer full-buf)
394 (set-window-buffer pwin pbuf)
400 ;;; @ message/external-body
403 (defvar mime-article/dired-function
404 (if mime/use-multi-frame
405 (function dired-other-frame)
406 (function mime-article/dired-function-for-one-frame)
409 (defun mime-article/dired-function-for-one-frame (dir)
410 (let ((win (or (get-buffer-window mime-view-buffer)
411 (get-largest-window))))
416 (defun mime-article/decode-message/external-ftp (beg end cal)
417 (let* ((site (cdr (assoc "site" cal)))
418 (directory (cdr (assoc "directory" cal)))
419 (name (cdr (assoc "name" cal)))
420 ;;(mode (cdr (assoc "mode" cal)))
421 (pathname (concat "/anonymous@" site ":" directory))
423 (message (concat "Accessing " (expand-file-name name pathname) "..."))
424 (funcall mime-article/dired-function pathname)
425 (goto-char (point-min))
426 (search-forward name)
435 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
436 (define-key mime-view-text/plain-mode-map
437 "q" (function mime-view-text/plain-exit))
439 (defun mime-view-text/plain-mode ()
440 "\\{mime-view-text/plain-mode-map}"
441 (setq buffer-read-only t)
442 (setq major-mode 'mime-view-text/plain-mode)
443 (setq mode-name "MIME-View text/plain")
444 (use-local-map mime-view-text/plain-mode-map)
447 (defun mime-view-text/plain-exit ()
449 (kill-buffer (current-buffer))
452 (defun mime-article/decode-caesar (beg end cal)
453 (let* ((cnum (mime-article/point-content-number beg))
454 (new-name (format "%s-%s" (buffer-name) cnum))
455 (mother mime-view-buffer)
456 (charset (cdr (assoc "charset" cal)))
457 (encoding (cdr (assq 'encoding cal)))
460 (setq str (buffer-substring beg end))
461 (let ((pwin (or (get-buffer-window mother)
462 (get-largest-window)))
463 (buf (get-buffer-create new-name))
465 (set-window-buffer pwin buf)
469 (setq buffer-read-only nil)
472 (goto-char (point-min))
473 (if (re-search-forward "^\n" nil t)
474 (delete-region (point-min) (match-end 0))
476 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
477 (assq t mime-text-decoder-alist)))))
479 (funcall m charset encoding)
482 (set-mark (point-min))
483 (goto-char (point-max))
486 (set-buffer-modified-p nil)
487 (mime-view-text/plain-mode)
496 ;;; mime-play.el ends here