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.21 1997-03-17 11:45:09 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-view-decoding-mode "play" "MIME body decoding mode")
40 (defvar mime-preview/after-decoded-position nil)
42 (defun mime-play-entity (&optional mode)
44 It decodes current entity to call internal or external method. The
45 method is selected from variable `mime/content-decoding-condition'.
46 If MODE is specified, play as it. Default MODE is \"play\"."
51 (let ((pc (mime-preview/point-pcinfo (point))))
53 (let ((the-buf (current-buffer)))
54 (setq mime-preview/after-decoded-position (point))
55 (set-buffer (mime::preview-content-info/buffer pc))
56 (mime-article/decode-content
57 (mime::preview-content-info/content-info pc) mode)
58 (if (eq (current-buffer)
59 (mime::preview-content-info/buffer pc))
62 (goto-char mime-preview/after-decoded-position)
66 (defun mime-article/decode-content (cinfo &optional mode)
68 (setq mode mime-view-decoding-mode)
70 (let ((beg (mime::content-info/point-min cinfo))
71 (end (mime::content-info/point-max cinfo))
72 (ctype (or (mime::content-info/type cinfo) "text/plain"))
73 (params (mime::content-info/parameters cinfo))
74 (encoding (mime::content-info/encoding cinfo))
77 (if (< beg (point-min))
78 (setq beg (point-min))
80 (if (< (point-max) end)
81 (setq end (point-max))
84 (setq cal (list* (cons 'type ctype)
85 (cons 'encoding encoding)
86 (cons 'major-mode major-mode)
89 (setq cal (cons (cons 'mode mode) cal))
91 (setq ret (mime/get-content-decoding-alist cal))
92 (setq method (cdr (assq 'method ret)))
93 (cond ((and (symbolp method)
95 (funcall method beg end ret)
97 ((and (listp method)(stringp (car method)))
98 (mime-article/start-external-method-region beg end ret)
101 (mime-article/show-output-buffer
102 "No method are specified for %s\n" ctype)
108 ;;; @ method selector
111 (defun mime/get-content-decoding-alist (al)
112 (get-unified-alist mime/content-decoding-condition al)
116 ;;; @ external decoder
119 (defun mime-article/start-external-method-region (beg end cal)
122 (narrow-to-region beg end)
124 (let ((method (cdr (assoc 'method cal)))
125 (name (mime-article/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/output-buffer-name (car method)
149 (mime-article/make-method-args cal
152 (apply (function start-process) args)
153 (mime-article/show-output-buffer)
157 (defun mime-article/make-method-args (cal format)
162 (let* ((item (eval arg))
163 (ret (cdr (assoc item cal)))
167 (if (eq item 'encoding)
174 (defun mime-article/show-output-buffer (&rest forms)
175 (get-buffer-create mime/output-buffer-name)
176 (let ((the-win (selected-window))
177 (win (get-buffer-window mime/output-buffer-name))
180 (if (and mime/output-buffer-window-is-shared-with-bbdb
181 (boundp 'bbdb-buffer-name)
182 (setq win (get-buffer-window bbdb-buffer-name))
184 (set-window-buffer win mime/output-buffer-name)
185 (select-window (get-buffer-window mime::article/preview-buffer))
186 (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
187 (set-window-buffer win mime/output-buffer-name)
190 (goto-char (point-max))
192 (insert (apply (function format) forms))
194 (select-window the-win)
201 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
203 (defvar mime-view-file-name-regexp-1
204 (concat mime-view-file-name-char-regexp "+\\."
205 mime-view-file-name-char-regexp "+"))
207 (defvar mime-view-file-name-regexp-2
208 (concat (regexp-* mime-view-file-name-char-regexp)
209 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
211 (defun mime-article/get-original-filename (param &optional encoding)
212 (or (mime-article/get-uu-filename param encoding)
214 (or (if (or (and (setq ret (mime/Content-Disposition))
215 (setq ret (assoc "filename" (cdr ret)))
217 (setq ret (assoc "name" param))
218 (setq ret (assoc "x-name" param))
220 (std11-strip-quoted-string (cdr ret))
223 (std11-find-field-body '("Content-Description"
225 (if (or (string-match mime-view-file-name-regexp-1 ret)
226 (string-match mime-view-file-name-regexp-2 ret))
227 (substring ret (match-beginning 0)(match-end 0))
232 (defun mime-article/get-filename (param)
233 (replace-as-filename (mime-article/get-original-filename param))
237 ;;; @ mail/news message
240 (defun mime-view-quitting-method-for-mime/show-message-mode ()
241 (let ((mother mime::preview/mother-buffer)
242 (win-conf mime::preview/original-window-configuration)
245 (mime::preview-content-info/buffer (car mime::preview/content-list)))
246 (mime-view-kill-buffer)
247 (set-window-configuration win-conf)
248 (pop-to-buffer mother)
249 ;;(goto-char (point-min))
250 ;;(mime-view-up-content)
253 (defun mime-article/view-message/rfc822 (beg end cal)
254 (let* ((cnum (mime-article/point-content-number beg))
255 (new-name (format "%s-%s" (buffer-name) cnum))
256 (mother mime::article/preview-buffer)
258 (cdr (or (assq major-mode mime-text-decoder-alist)
259 (assq t mime-text-decoder-alist))))
261 (setq str (buffer-substring beg end))
262 (switch-to-buffer new-name)
265 (goto-char (point-min))
266 (if (re-search-forward "^\n" nil t)
267 (delete-region (point-min) (match-end 0))
269 (setq major-mode 'mime/show-message-mode)
270 (setq mime-text-decoder text-decoder)
271 (mime-view-mode mother)
275 ;;; @ message/partial
278 (defvar mime-article/coding-system-alist
279 (list '(mh-show-mode . no-conversion)
280 (cons t (mime-charset-to-coding-system default-mime-charset))
283 (defun mime-article::write-region (start end file)
284 (let ((coding-system-for-write
286 (or (assq major-mode mime-article/coding-system-alist)
287 (assq t mime-article/coding-system-alist)
289 (write-region start end file)
292 (defun mime-article/decode-message/partial (beg end cal)
296 (concat "m-prts-" (user-login-name)) mime-temp-directory))
297 (id (cdr (assoc "id" cal)))
298 (number (cdr (assoc "number" cal)))
299 (total (cdr (assoc "total" cal)))
301 (mother mime::article/preview-buffer)
303 (or (file-exists-p root-dir)
304 (make-directory root-dir)
306 (setq id (replace-as-filename id))
307 (setq root-dir (concat root-dir "/" id))
308 (or (file-exists-p root-dir)
309 (make-directory root-dir)
311 (setq file (concat root-dir "/FULL"))
312 (if (file-exists-p file)
313 (let ((full-buf (get-buffer-create "FULL"))
314 (pwin (or (get-buffer-window mother)
315 (get-largest-window)))
317 (save-window-excursion
318 (set-buffer full-buf)
320 (as-binary-input-file (insert-file-contents file))
321 (setq major-mode 'mime/show-message-mode)
322 (mime-view-mode mother)
324 (set-window-buffer pwin
326 (set-buffer full-buf)
327 mime::article/preview-buffer))
330 (re-search-forward "^$")
331 (goto-char (1+ (match-end 0)))
332 (setq file (concat root-dir "/" number))
333 (mime-article::write-region (point) (point-max) file)
334 (let ((total-file (concat root-dir "/CT")))
338 (or (file-exists-p total-file)
341 (get-buffer-create mime/temp-buffer-name))
344 (write-file total-file)
345 (kill-buffer (current-buffer))
347 (string-to-number total)
349 (and (file-exists-p total-file)
351 (set-buffer (find-file-noselect total-file))
353 (and (re-search-forward "[0-9]+" nil t)
355 (buffer-substring (match-beginning 0)
358 (kill-buffer (current-buffer))
361 (if (and total (> total 0))
364 (set-buffer (get-buffer-create mime/temp-buffer-name))
365 (let ((full-buf (current-buffer)))
369 (setq file (concat root-dir "/" (int-to-string i)))
370 (or (file-exists-p file)
373 (as-binary-input-file (insert-file-contents file))
374 (goto-char (point-max))
377 (as-binary-output-file (write-file (concat root-dir "/FULL")))
380 (let ((file (format "%s/%d" root-dir i)))
381 (and (file-exists-p file)
386 (let ((file (expand-file-name "CT" root-dir)))
387 (and (file-exists-p file)
390 (save-window-excursion
391 (setq major-mode 'mime/show-message-mode)
392 (mime-view-mode mother)
394 (let ((pwin (or (get-buffer-window mother)
397 (pbuf (save-excursion
398 (set-buffer full-buf)
399 mime::article/preview-buffer)))
400 (set-window-buffer pwin pbuf)
406 ;;; @ message/external-body
409 (defvar mime-article/dired-function
410 (if mime/use-multi-frame
411 (function dired-other-frame)
412 (function mime-article/dired-function-for-one-frame)
415 (defun mime-article/dired-function-for-one-frame (dir)
416 (let ((win (or (get-buffer-window mime::article/preview-buffer)
417 (get-largest-window))))
422 (defun mime-article/decode-message/external-ftp (beg end cal)
423 (let* ((site (cdr (assoc "site" cal)))
424 (directory (cdr (assoc "directory" cal)))
425 (name (cdr (assoc "name" cal)))
426 ;;(mode (cdr (assoc "mode" cal)))
427 (pathname (concat "/anonymous@" site ":" directory))
429 (message (concat "Accessing " (expand-file-name name pathname) "..."))
430 (funcall mime-article/dired-function pathname)
431 (goto-char (point-min))
432 (search-forward name)
441 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
442 (define-key mime-view-text/plain-mode-map
443 "q" (function mime-view-text/plain-exit))
445 (defun mime-view-text/plain-mode ()
446 "\\{mime-view-text/plain-mode-map}"
447 (setq buffer-read-only t)
448 (setq major-mode 'mime-view-text/plain-mode)
449 (setq mode-name "MIME-View text/plain")
450 (use-local-map mime-view-text/plain-mode-map)
453 (defun mime-view-text/plain-exit ()
455 (kill-buffer (current-buffer))
458 (defun mime-article/decode-caesar (beg end cal)
459 (let* ((cnum (mime-article/point-content-number beg))
460 (new-name (format "%s-%s" (buffer-name) cnum))
461 (mother mime::article/preview-buffer)
462 (charset (cdr (assoc "charset" cal)))
463 (encoding (cdr (assq 'encoding cal)))
466 (setq str (buffer-substring beg end))
467 (let ((pwin (or (get-buffer-window mother)
468 (get-largest-window)))
469 (buf (get-buffer-create new-name))
471 (set-window-buffer pwin buf)
475 (setq buffer-read-only nil)
478 (goto-char (point-min))
479 (if (re-search-forward "^\n" nil t)
480 (delete-region (point-min) (match-end 0))
482 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
483 (assq t mime-text-decoder-alist)))))
485 (funcall m charset encoding)
488 (set-mark (point-min))
489 (goto-char (point-max))
492 (set-buffer-modified-p nil)
493 (mime-view-text/plain-mode)
502 ;;; mime-play.el ends here