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.4 1997-02-27 09:00:33 tmorioka 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.
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)
102 (defun put-alist (item value alist)
103 "Modify ALIST to set VALUE to ITEM.
104 If there is a pair whose car is ITEM, replace its cdr by VALUE.
105 If there is not such pair, create new pair (ITEM . VALUE) and
106 return new alist whose car is the new pair and cdr is ALIST.
107 \[tomo's ELIS like function]"
108 (let ((pair (assoc item alist)))
113 (cons (cons item value) alist)
116 (defun del-alist (item alist)
117 "If there is a pair whose key is ITEM, delete it from ALIST.
118 \[tomo's ELIS emulating function]"
119 (if (equal item (car (car alist)))
125 (while (not (null r))
126 (if (equal item (car (car r)))
139 (defun put-fields (tp c)
144 (if (not (if (setq ret (assoc (car f) c))
145 (equal (cdr ret)(cdr f))
157 (defun field-unifier-for-default (a b)
159 (cond ((equal a b) a)
167 (defun field-unifier-for-mode (a b)
176 (defun field-unify (a b)
177 (let ((sym (intern (concat "field-unifier-for-" (intern (car a))))))
178 (if (not (fboundp sym))
179 (setq sym (function field-unifier-for-default))
188 (defun assoc-unify (class instance)
190 (let ((cla (copy-alist class))
191 (ins (copy-alist instance))
193 cell aret ret prev rest)
196 (setq aret (assoc (car cell) ins))
198 (if (setq ret (field-unify cell aret))
201 (setq prev (put-alist (car (car ret))
206 (setq rest (put-alist (car (nth 2 ret))
210 (setq cla (put-alist (car cell)(cdr (nth 1 ret)) cla))
211 (setq ins (del-alist (car cell) ins))
217 (setq r (copy-alist ins))
220 (setq aret (assoc (car cell) cla))
222 (if (setq ret (field-unify cell aret))
225 (setq prev (put-alist (car (car ret))
230 (setq rest (put-alist (car (nth 2 ret))
234 (setq cla (del-alist (car cell) cla))
235 (setq ins (put-alist (car cell)(cdr (nth 1 ret)) ins))
241 (list prev (append cla ins) rest)
244 (defun get-unified-alist (db al)
248 (if (setq ret (nth 1 (assoc-unify (car r) al)))
254 (defun delete-atype (atl al)
255 (let* ((r atl) ret oal)
259 (if (setq ret (nth 1 (assoc-unify (car r) al)))
267 (defun remove-atype (sym al)
269 (set sym (delete-atype (eval sym) al))
272 (defun replace-atype (atl old-al new-al)
273 (let* ((r atl) ret oal)
276 (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
277 (throw 'tag (rplaca r new-al))
283 (defun set-atype (sym al &rest options)
284 (if (null (boundp sym))
286 (let* ((replacement (memq 'replacement options))
287 (ignore-fields (car (cdr (memq 'ignore options))))
288 (remove (or (car (cdr (memq 'remove options)))
289 (let ((ral (copy-alist al)))
292 (setq ral (del-alist type ral))
299 (replace-atype (eval sym) remove al)
302 (delete-atype (eval sym) remove)
310 (defun mime/get-content-decoding-alist (al)
311 (get-unified-alist mime/content-decoding-condition al)
315 ;;; @ external decoder
318 (defun mime-article/start-external-method-region (beg end cal)
321 (narrow-to-region beg end)
323 (let ((method (cdr (assoc 'method cal)))
324 (name (mime-article/get-filename cal))
327 (let ((file (make-temp-name
328 (expand-file-name "TM" mime/tmp-dir)))
333 (if (re-search-forward "^$" nil t)
339 (write-region b end file)
340 (message "External method is starting...")
342 'name (replace-as-filename name) cal))
343 (setq cal (put-alist 'file file cal))
346 mime/output-buffer-name (car method)
348 (mime-article/make-method-args cal
351 (apply (function start-process) args)
352 (mime-article/show-output-buffer)
356 (defun mime-article/make-method-args (cal format)
361 (let* ((item (eval arg))
362 (ret (cdr (assoc item cal)))
366 (if (eq item 'encoding)
373 (defun mime-article/show-output-buffer (&rest forms)
374 (get-buffer-create mime/output-buffer-name)
375 (let ((the-win (selected-window))
376 (win (get-buffer-window mime/output-buffer-name))
379 (if (and mime/output-buffer-window-is-shared-with-bbdb
380 (boundp 'bbdb-buffer-name)
381 (setq win (get-buffer-window bbdb-buffer-name))
383 (set-window-buffer win mime/output-buffer-name)
384 (select-window (get-buffer-window mime::article/preview-buffer))
385 (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
386 (set-window-buffer win mime/output-buffer-name)
389 (goto-char (point-max))
391 (insert (apply (function format) forms))
393 (select-window the-win)
400 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
402 (defvar mime-view-file-name-regexp-1
403 (concat mime-view-file-name-char-regexp "+\\."
404 mime-view-file-name-char-regexp "+"))
406 (defvar mime-view-file-name-regexp-2
407 (concat (regexp-* mime-view-file-name-char-regexp)
408 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
410 (defun mime-article/get-original-filename (param &optional encoding)
411 (or (mime-article/get-uu-filename param encoding)
413 (or (if (or (and (setq ret (mime/Content-Disposition))
414 (setq ret (assoc "filename" (cdr ret)))
416 (setq ret (assoc "name" param))
417 (setq ret (assoc "x-name" param))
419 (std11-strip-quoted-string (cdr ret))
422 (std11-find-field-body '("Content-Description"
424 (if (or (string-match mime-view-file-name-regexp-1 ret)
425 (string-match mime-view-file-name-regexp-2 ret))
426 (substring ret (match-beginning 0)(match-end 0))
431 (defun mime-article/get-filename (param)
432 (replace-as-filename (mime-article/get-original-filename param))
436 ;;; @ mail/news message
439 (defun mime-view-quitting-method-for-mime/show-message-mode ()
440 (let ((mother mime::preview/mother-buffer)
441 (win-conf mime::preview/original-window-configuration)
444 (mime::preview-content-info/buffer (car mime::preview/content-list)))
445 (mime-view-kill-buffer)
446 (set-window-configuration win-conf)
447 (pop-to-buffer mother)
448 ;;(goto-char (point-min))
449 ;;(mime-view-up-content)
452 (defun mime-article/view-message/rfc822 (beg end cal)
453 (let* ((cnum (mime-article/point-content-number beg))
454 (cur-buf (current-buffer))
455 (new-name (format "%s-%s" (buffer-name) cnum))
456 (mother mime::article/preview-buffer)
458 (or (cdr (assq major-mode mime-text-decoder-alist))
459 'mime-view-default-code-convert-region))
461 (setq str (buffer-substring beg end))
462 (switch-to-buffer new-name)
465 (goto-char (point-min))
466 (if (re-search-forward "^\n" nil t)
467 (delete-region (point-min) (match-end 0))
469 (setq major-mode 'mime/show-message-mode)
470 (setq mime::article/code-converter code-converter)
471 (mime-view-mode mother)
475 ;;; @ message/partial
478 (defvar mime-article/coding-system-alist
479 (list (cons 'mh-show-mode *noconv*)
480 (cons t (mime-charset-to-coding-system default-mime-charset))
483 (cond (running-mule-merged-emacs
484 (defun mime-article::write-region (start end file)
485 (let ((coding-system-for-write
487 (or (assq major-mode mime-article/coding-system-alist)
488 (assq t mime-article/coding-system-alist)
490 (write-region start end file)
494 running-xemacs-with-mule)
495 (defun mime-article::write-region (start end file)
496 (let ((file-coding-system
498 (or (assq major-mode mime-article/coding-system-alist)
499 (assq t mime-article/coding-system-alist)
501 (write-region start end file)
505 (defun mime-article::write-region (start end file)
506 (let ((kanji-fileio-code
508 (or (assq major-mode mime-article/kanji-code-alist)
509 (assq t mime-article/kanji-code-alist)
511 (write-region start end file)
515 (defalias 'mime-article::write-region 'write-region)
518 (defun mime-article/decode-message/partial (beg end cal)
520 (let* ((root-dir (expand-file-name
521 (concat "m-prts-" (user-login-name)) mime/tmp-dir))
522 (id (cdr (assoc "id" cal)))
523 (number (cdr (assoc "number" cal)))
524 (total (cdr (assoc "total" cal)))
526 (mother mime::article/preview-buffer)
528 (or (file-exists-p root-dir)
529 (make-directory root-dir)
531 (setq id (replace-as-filename id))
532 (setq root-dir (concat root-dir "/" id))
533 (or (file-exists-p root-dir)
534 (make-directory root-dir)
536 (setq file (concat root-dir "/FULL"))
537 (if (file-exists-p file)
538 (let ((full-buf (get-buffer-create "FULL"))
539 (pwin (or (get-buffer-window mother)
540 (get-largest-window)))
542 (save-window-excursion
543 (set-buffer full-buf)
545 (as-binary-input-file (insert-file-contents file))
546 (setq major-mode 'mime/show-message-mode)
547 (mime-view-mode mother)
549 (set-window-buffer pwin
551 (set-buffer full-buf)
552 mime::article/preview-buffer))
555 (re-search-forward "^$")
556 (goto-char (1+ (match-end 0)))
557 (setq file (concat root-dir "/" number))
558 (mime-article::write-region (point) (point-max) file)
559 (let ((total-file (concat root-dir "/CT")))
563 (or (file-exists-p total-file)
566 (get-buffer-create mime/temp-buffer-name))
569 (write-file total-file)
570 (kill-buffer (current-buffer))
572 (string-to-number total)
574 (and (file-exists-p total-file)
576 (set-buffer (find-file-noselect total-file))
578 (and (re-search-forward "[0-9]+" nil t)
580 (buffer-substring (match-beginning 0)
583 (kill-buffer (current-buffer))
586 (if (and total (> total 0))
589 (set-buffer (get-buffer-create mime/temp-buffer-name))
590 (let ((full-buf (current-buffer)))
594 (setq file (concat root-dir "/" (int-to-string i)))
595 (or (file-exists-p file)
598 (as-binary-input-file (insert-file-contents file))
599 (goto-char (point-max))
602 (as-binary-output-file (write-file (concat root-dir "/FULL")))
605 (let ((file (format "%s/%d" root-dir i)))
606 (and (file-exists-p file)
611 (let ((file (expand-file-name "CT" root-dir)))
612 (and (file-exists-p file)
615 (save-window-excursion
616 (setq major-mode 'mime/show-message-mode)
617 (mime-view-mode mother)
619 (let ((pwin (or (get-buffer-window mother)
622 (pbuf (save-excursion
623 (set-buffer full-buf)
624 mime::article/preview-buffer)))
625 (set-window-buffer pwin pbuf)
636 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
637 (define-key mime-view-text/plain-mode-map
638 "q" (function mime-view-text/plain-exit))
640 (defun mime-view-text/plain-mode ()
641 "\\{mime-view-text/plain-mode-map}"
642 (setq buffer-read-only t)
643 (setq major-mode 'mime-view-text/plain-mode)
644 (setq mode-name "MIME-View text/plain")
645 (use-local-map mime-view-text/plain-mode-map)
648 (defun mime-view-text/plain-exit ()
650 (kill-buffer (current-buffer))
653 (defun mime-article/decode-caesar (beg end cal)
654 (let* ((cnum (mime-article/point-content-number beg))
655 (cur-buf (current-buffer))
656 (new-name (format "%s-%s" (buffer-name) cnum))
657 (mother mime::article/preview-buffer)
658 (charset (cdr (assoc "charset" cal)))
659 (encoding (cdr (assq 'encoding cal)))
662 (setq str (buffer-substring beg end))
663 (let ((pwin (or (get-buffer-window mother)
664 (get-largest-window)))
665 (buf (get-buffer-create new-name))
667 (set-window-buffer pwin buf)
671 (setq buffer-read-only nil)
674 (goto-char (point-min))
675 (if (re-search-forward "^\n" nil t)
676 (delete-region (point-min) (match-end 0))
678 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
679 (assq t mime-text-decoder-alist)))))
681 (funcall m charset encoding)
684 (set-mark (point-min))
685 (goto-char (point-max))
688 (set-buffer-modified-p nil)
689 (mime-view-text/plain-mode)
698 ;;; mime-play.el ends here