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.1 1997-02-21 05:28:56 tmorioka Exp $
9 ;; Keywords: mail, news, MIME, multimedia
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.
36 (defvar mime-preview/after-decoded-position nil)
38 (defun mime-preview/decode-content ()
40 (let ((pc (mime-preview/point-pcinfo (point))))
42 (let ((the-buf (current-buffer)))
43 (setq mime-preview/after-decoded-position (point))
44 (set-buffer (mime::preview-content-info/buffer pc))
45 (mime-article/decode-content
46 (mime::preview-content-info/content-info pc))
47 (if (eq (current-buffer)
48 (mime::preview-content-info/buffer pc))
51 (goto-char mime-preview/after-decoded-position)
55 (defun mime-article/decode-content (cinfo)
56 (let ((beg (mime::content-info/point-min cinfo))
57 (end (mime::content-info/point-max cinfo))
58 (ctype (or (mime::content-info/type cinfo) "text/plain"))
59 (params (mime::content-info/parameters cinfo))
60 (encoding (mime::content-info/encoding cinfo))
63 (if (< beg (point-min))
64 (setq beg (point-min))
66 (if (< (point-max) end)
67 (setq end (point-max))
70 (setq cal (list* (cons 'type ctype)
71 (cons 'encoding encoding)
72 (cons 'major-mode major-mode)
74 (if mime-view-decoding-mode
76 (cons 'mode mime-view-decoding-mode)
79 (setq ret (mime/get-content-decoding-alist cal))
80 (setq method (cdr (assq 'method ret)))
81 (cond ((and (symbolp method)
83 (funcall method beg end ret)
85 ((and (listp method)(stringp (car method)))
86 (mime-article/start-external-method-region beg end ret)
89 (mime-article/show-output-buffer
90 "No method are specified for %s\n" ctype)
95 (defun field-unifier-for-mode (a b)
104 (defun mime/get-content-decoding-alist (al)
105 (get-unified-alist mime/content-decoding-condition al)
109 ;;; @ external decoder
112 (defun mime-article/start-external-method-region (beg end cal)
115 (narrow-to-region beg end)
117 (let ((method (cdr (assoc 'method cal)))
118 (name (mime-article/get-filename cal))
121 (let ((file (make-temp-name
122 (expand-file-name "TM" mime/tmp-dir)))
127 (if (re-search-forward "^$" nil t)
133 (write-region b end file)
134 (message "External method is starting...")
136 'name (replace-as-filename name) cal))
137 (setq cal (put-alist 'file file cal))
140 mime/output-buffer-name (car method)
142 (mime-article/make-method-args cal
145 (apply (function start-process) args)
146 (mime-article/show-output-buffer)
150 (defun mime-article/make-method-args (cal format)
155 (let* ((item (eval arg))
156 (ret (cdr (assoc item cal)))
160 (if (eq item 'encoding)
167 (defun mime-article/show-output-buffer (&rest forms)
168 (get-buffer-create mime/output-buffer-name)
169 (let ((the-win (selected-window))
170 (win (get-buffer-window mime/output-buffer-name))
173 (if (and mime/output-buffer-window-is-shared-with-bbdb
174 (boundp 'bbdb-buffer-name)
175 (setq win (get-buffer-window bbdb-buffer-name))
177 (set-window-buffer win mime/output-buffer-name)
178 (select-window (get-buffer-window mime::article/preview-buffer))
179 (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
180 (set-window-buffer win mime/output-buffer-name)
183 (goto-char (point-max))
185 (insert (apply (function format) forms))
187 (select-window the-win)
194 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
196 (defvar mime-view-file-name-regexp-1
197 (concat mime-view-file-name-char-regexp "+\\."
198 mime-view-file-name-char-regexp "+"))
200 (defvar mime-view-file-name-regexp-2
201 (concat (regexp-* mime-view-file-name-char-regexp)
202 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
204 (defun mime-article/get-original-filename (param &optional encoding)
205 (or (mime-article/get-uu-filename param encoding)
207 (or (if (or (and (setq ret (mime/Content-Disposition))
208 (setq ret (assoc "filename" (cdr ret)))
210 (setq ret (assoc "name" param))
211 (setq ret (assoc "x-name" param))
213 (std11-strip-quoted-string (cdr ret))
216 (std11-find-field-body '("Content-Description"
218 (if (or (string-match mime-view-file-name-regexp-1 ret)
219 (string-match mime-view-file-name-regexp-2 ret))
220 (substring ret (match-beginning 0)(match-end 0))
225 (defun mime-article/get-filename (param)
226 (replace-as-filename (mime-article/get-original-filename param))
230 ;;; @ mail/news message
233 (defun mime-view-quitting-method-for-mime/show-message-mode ()
234 (let ((mother mime::preview/mother-buffer)
235 (win-conf mime::preview/original-window-configuration)
238 (mime::preview-content-info/buffer (car mime::preview/content-list)))
239 (mime-view-kill-buffer)
240 (set-window-configuration win-conf)
241 (pop-to-buffer mother)
242 ;;(goto-char (point-min))
243 ;;(mime-view-up-content)
246 (defun mime-article/view-message/rfc822 (beg end cal)
247 (let* ((cnum (mime-article/point-content-number beg))
248 (cur-buf (current-buffer))
249 (new-name (format "%s-%s" (buffer-name) cnum))
250 (mother mime::article/preview-buffer)
252 (or (cdr (assq major-mode mime-text-decoder-alist))
253 'mime-view-default-code-convert-region))
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::article/code-converter code-converter)
265 (mime/viewer-mode mother)
269 ;;; @ message/partial
272 (defvar mime-article/coding-system-alist
273 (list (cons 'mh-show-mode *noconv*)
274 (cons t (mime-charset-to-coding-system default-mime-charset))
277 (cond (running-mule-merged-emacs
278 (defun mime-article::write-region (start end file)
279 (let ((coding-system-for-write
281 (or (assq major-mode mime-article/coding-system-alist)
282 (assq t mime-article/coding-system-alist)
284 (write-region start end file)
288 running-xemacs-with-mule)
289 (defun mime-article::write-region (start end file)
290 (let ((file-coding-system
292 (or (assq major-mode mime-article/coding-system-alist)
293 (assq t mime-article/coding-system-alist)
295 (write-region start end file)
299 (defun mime-article::write-region (start end file)
300 (let ((kanji-fileio-code
302 (or (assq major-mode mime-article/kanji-code-alist)
303 (assq t mime-article/kanji-code-alist)
305 (write-region start end file)
309 (defalias 'mime-article::write-region 'write-region)
312 (defun mime-article/decode-message/partial (beg end cal)
314 (let* ((root-dir (expand-file-name
315 (concat "m-prts-" (user-login-name)) mime/tmp-dir))
316 (id (cdr (assoc "id" cal)))
317 (number (cdr (assoc "number" cal)))
318 (total (cdr (assoc "total" cal)))
320 (mother mime::article/preview-buffer)
322 (or (file-exists-p root-dir)
323 (make-directory root-dir)
325 (setq id (replace-as-filename id))
326 (setq root-dir (concat root-dir "/" id))
327 (or (file-exists-p root-dir)
328 (make-directory root-dir)
330 (setq file (concat root-dir "/FULL"))
331 (if (file-exists-p file)
332 (let ((full-buf (get-buffer-create "FULL"))
333 (pwin (or (get-buffer-window mother)
334 (get-largest-window)))
336 (save-window-excursion
337 (set-buffer full-buf)
339 (as-binary-input-file (insert-file-contents file))
340 (setq major-mode 'mime/show-message-mode)
341 (mime/viewer-mode mother)
343 (set-window-buffer pwin
345 (set-buffer full-buf)
346 mime::article/preview-buffer))
349 (re-search-forward "^$")
350 (goto-char (1+ (match-end 0)))
351 (setq file (concat root-dir "/" number))
352 (mime-article::write-region (point) (point-max) file)
353 (let ((total-file (concat root-dir "/CT")))
357 (or (file-exists-p total-file)
360 (get-buffer-create mime/temp-buffer-name))
363 (write-file total-file)
364 (kill-buffer (current-buffer))
366 (string-to-number total)
368 (and (file-exists-p total-file)
370 (set-buffer (find-file-noselect total-file))
372 (and (re-search-forward "[0-9]+" nil t)
374 (buffer-substring (match-beginning 0)
377 (kill-buffer (current-buffer))
380 (if (and total (> total 0))
383 (set-buffer (get-buffer-create mime/temp-buffer-name))
384 (let ((full-buf (current-buffer)))
388 (setq file (concat root-dir "/" (int-to-string i)))
389 (or (file-exists-p file)
392 (as-binary-input-file (insert-file-contents file))
393 (goto-char (point-max))
396 (as-binary-output-file (write-file (concat root-dir "/FULL")))
399 (let ((file (format "%s/%d" root-dir i)))
400 (and (file-exists-p file)
405 (let ((file (expand-file-name "CT" root-dir)))
406 (and (file-exists-p file)
409 (save-window-excursion
410 (setq major-mode 'mime/show-message-mode)
411 (mime/viewer-mode mother)
413 (let ((pwin (or (get-buffer-window mother)
416 (pbuf (save-excursion
417 (set-buffer full-buf)
418 mime::article/preview-buffer)))
419 (set-window-buffer pwin pbuf)
430 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
431 (define-key mime-view-text/plain-mode-map
432 "q" (function mime-view-text/plain-exit))
434 (defun mime-view-text/plain-mode ()
435 "\\{mime-view-text/plain-mode-map}"
436 (setq buffer-read-only t)
437 (setq major-mode 'mime-view-text/plain-mode)
438 (setq mode-name "MIME-View text/plain")
439 (use-local-map mime-view-text/plain-mode-map)
442 (defun mime-view-text/plain-exit ()
444 (kill-buffer (current-buffer))
447 (defun mime-article/decode-caesar (beg end cal)
448 (let* ((cnum (mime-article/point-content-number beg))
449 (cur-buf (current-buffer))
450 (new-name (format "%s-%s" (buffer-name) cnum))
451 (mother mime::article/preview-buffer)
452 (charset (cdr (assoc "charset" cal)))
453 (encoding (cdr (assq 'encoding cal)))
456 (setq str (buffer-substring beg end))
457 (let ((pwin (or (get-buffer-window mother)
458 (get-largest-window)))
459 (buf (get-buffer-create new-name))
461 (set-window-buffer pwin buf)
465 (setq buffer-read-only nil)
468 (goto-char (point-min))
469 (if (re-search-forward "^\n" nil t)
470 (delete-region (point-min) (match-end 0))
472 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
473 (assq t mime-text-decoder-alist)))))
475 (funcall m charset encoding)
478 (set-mark (point-min))
479 (goto-char (point-max))
482 (set-buffer-modified-p nil)
483 (mime-view-text/plain-mode)
492 ;;; mime-play.el ends here