2 ;;; tm-view.el --- interactive MIME viewer for GNU Emacs
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1994,1995 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Keywords: mail, news, MIME, multimedia
10 ;;; This file is part of tm (Tools for MIME).
27 (defconst mime-viewer/RCS-ID
28 "$Id: tm-view.el,v 7.13 1995/10/12 06:10:04 morioka Exp $")
30 (defconst mime-viewer/version (get-version-string mime-viewer/RCS-ID))
31 (defconst mime/viewer-version mime-viewer/version)
37 (defvar mime/content-decoding-condition
38 '(((type . "text/plain")
39 (method "tm-plain" nil 'file 'type 'encoding 'mode 'name))
40 ;;((type . "text/x-latex")
41 ;; (method "tm-latex" nil 'file 'type 'encoding 'mode 'name))
42 ((type . "audio/basic")
43 (method "tm-au" nil 'file 'type 'encoding 'mode 'name))
45 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
46 ((type . "image/jpeg")
47 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
48 ((type . "image/tiff")
49 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
50 ((type . "image/x-tiff")
51 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
52 ((type . "image/x-xbm")
53 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
54 ((type . "image/x-pic")
55 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
56 ((type . "video/mpeg")
57 (method "tm-mpeg" nil 'file 'type 'encoding 'mode 'name))
58 ((type . "application/octet-stream")
59 (method "tm-file" nil 'file 'type 'encoding 'mode 'name))
60 ;;((type . "message/external-body")
61 ;; (method "xterm" nil
62 ;; "-e" "showexternal"
63 ;; 'file '"access-type" '"name" '"site" '"directory"))
64 ((type . "message/partial")
65 (method . mime/decode-message/partial-region))
67 "-m" "tm" "-x" "-d" "-z" "-e" 'file)(mode . "play"))
68 ((method "tm-file" nil 'file 'type 'encoding 'mode 'name)
72 (defvar mime-viewer/childrens-header-showing-Content-Type-list
73 '("message/rfc822" "message/news"))
75 (defvar mime-viewer/default-showing-Content-Type-list
76 '("text/plain" "text/richtext" "text/enriched"
77 "text/x-latex" "application/x-latex"
78 "application/octet-stream" nil
80 "application/x-selection" "application/x-comment"))
82 (defvar mime-viewer/content-subject-omitting-Content-Type-list
83 '("application/x-selection"))
85 (defvar mime-viewer/uuencode-encoding-name-list '("x-uue" "x-uuencode"))
87 (defvar mime-viewer/ignored-field-list
88 '("Received" "Return-Path" "Replied" "Errors-To"
89 "Lines" "Sender" "Path" "Nntp-Posting-Host"
90 "Content-Type" "Precedence" "X-Face"))
92 (defvar mime-viewer/ignored-field-regexp)
94 (defvar mime-viewer/announcement-for-message/partial
95 (if (and (>= emacs-major-version 19) window-system)
97 \[[ This is message/partial style split message. ]]
98 \[[ Please press `v' key in this buffer ]]
99 \[[ or click here by mouse button-2. ]]"
101 \[[ This is message/partial style split message. ]]
102 \[[ Please press `v' key in this buffer. ]]"
106 ;;; @@ predicate functions
109 (defun mime-viewer/header-visible-p (cnum cinfo &optional ctype)
113 (mime::content-info/type
114 (mime-article/cnum-to-cinfo (butlast cnum) cinfo)
116 (member ctype mime-viewer/childrens-header-showing-Content-Type-list)
119 (defun mime-viewer/body-visible-p (cnum cinfo &optional ctype)
123 (mime::content-info/type
124 (setq ccinfo (mime-article/cnum-to-cinfo cnum cinfo))
127 (and (member ctype mime-viewer/default-showing-Content-Type-list)
128 (if (string-equal ctype "application/octet-stream")
131 (setq ccinfo (mime-article/cnum-to-cinfo cnum cinfo))
133 (member (mime::content-info/encoding ccinfo)
134 '(nil "7bit" "8bit"))
140 ;;; @@ content subject
143 (defun mime-viewer/insert-content-subject
144 (cnum cinfo ctype params subj)
146 (narrow-to-region (point)(point))
148 (let ((access-type (assoc "access-type" params))
149 (num (or (assoc-value "x-part-number" params)
153 (format "%s" (1+ num))
159 (let ((server (assoc "server" params)))
160 (setq access-type (cdr access-type))
162 (format "[%s %s ([%s] %s)]\n" num subj
163 access-type (cdr server))
164 (let ((site (assoc-value "site" params))
165 (dir (assoc-value "directory" params))
167 (format "[%s %s ([%s] %s:%s)]\n" num subj
168 access-type site dir)
170 (format "[%s %s (%s)]\n" num subj ctype)
172 (tm:add-button (point-min)(1- (point-max))
173 (function mime-viewer/play-content))
176 (defun mime-viewer/default-content-subject-function
177 (cnum cinfo ctype params subj)
178 (if (and (listp cnum)
181 mime-viewer/content-subject-omitting-Content-Type-list))
183 (mime-viewer/insert-content-subject
184 cnum cinfo ctype params subj)
187 (defvar mime-viewer/content-subject-function
188 (function mime-viewer/default-content-subject-function))
191 ;;; @@ content header filter
194 (defun mime-viewer/default-content-header-filter ()
195 (goto-char (point-min))
196 (while (and (re-search-forward mime-viewer/ignored-field-regexp nil t)
202 (re-search-forward "^\\([^ \t]\\|$\\)" nil t)
206 (mime/decode-message-header)
209 (defvar mime-viewer/content-header-filter-alist nil)
212 ;;; @@ content filter
215 (defvar mime-viewer/content-filter-alist
216 '(("text/plain" . mime-viewer/filter-text/plain)
217 ("application/pgp" . mime-viewer/filter-text/plain)
218 (nil . mime-viewer/filter-text/plain)))
220 (defun mime-viewer/default-content-filter (cnum cinfo ctype params subj)
224 ;;; @@ content separator
227 (defun mime-viewer/default-content-separator (cnum cinfo ctype params subj)
228 (if (and (not (mime-viewer/header-visible-p cnum cinfo ctype))
229 (not (mime-viewer/body-visible-p cnum cinfo ctype))
232 (goto-char (point-max))
237 ;;; @@ buffer local variables
240 (defvar mime::article/content-info nil)
241 (defvar mime::article/preview-buffer nil)
243 (defvar mime::preview/article-buffer nil)
244 (defvar mime::preview/content-list nil)
245 (defvar mime::preview/original-major-mode nil)
248 ;;; @@ quitting method
251 (defvar mime-viewer/quitting-method-alist
252 '((mime/show-message-mode
254 (set-window-configuration
255 mime/show-mode-old-window-configuration)
256 (let ((mother mime/mother-buffer))
258 (mime::preview-content-info/buffer
259 (car mime::preview/content-list)))
260 (mime-viewer/kill-buffer)
261 (pop-to-buffer mother)
262 (goto-char (point-min))
263 (mime-viewer/up-content)
271 ;; hack from Gnus 5.0.4.
273 (defvar mime-viewer/x-face-command
274 "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
275 "String to be executed to display an X-Face field.
276 The command will be executed in a sub-shell asynchronously.
277 The compressed face will be piped to this command.")
279 (defun mime-viewer/x-face-function ()
280 "Function to display X-Face field. You can redefine to customize."
281 (goto-char (point-min))
282 (if (re-search-forward "^X-Face:[ \t]*" nil t)
283 (let ((beg (match-end 0))
284 (end (rfc822/field-end))
286 (call-process-region beg end "sh" nil 0 nil
287 "-c" mime-viewer/x-face-command)
291 ;;; @ data structures
294 ;;; @@ preview-content-info
297 (define-structure mime::preview-content-info
298 point-min point-max buffer content-info)
304 (defun mime-viewer/setup-buffer (&optional ctl encoding ibuf obuf)
310 (make-variable-buffer-local 'mime::article/content-info)
311 (setq mime::article/content-info (mime/parse-message ctl encoding))
312 (let ((ret (mime-viewer/make-preview-buffer obuf)))
313 (make-variable-buffer-local 'mime::article/preview-buffer)
314 (setq mime::article/preview-buffer (car ret))
317 (defun mime-viewer/make-preview-buffer (&optional obuf)
318 (let ((cinfo mime::article/content-info)
319 (the-buf (current-buffer))
323 (setq obuf (concat "*Preview-" (buffer-name the-buf) "*"))
325 (setq pcl (mime/flatten-content-info cinfo))
326 (let ((bf (get-buffer obuf)))
330 (setq buffer-read-only nil)
333 (setq bf (get-buffer-create obuf))
336 (make-variable-buffer-local 'mime::preview/article-buffer)
337 (setq mime::preview/article-buffer the-buf)
338 (make-variable-buffer-local 'mime::preview/original-major-mode)
339 (setq mime::preview/original-major-mode mode)
340 (setq major-mode 'mime/viewer-mode)
341 (setq mode-name "MIME-View")
346 (mime-viewer/display-content content cinfo the-buf obuf)
349 (set-buffer-modified-p nil)
350 (setq buffer-read-only t)
355 (defun mime-viewer/display-content (content cinfo ibuf obuf)
356 (let* ((beg (mime::content-info/point-min content))
357 (end (mime::content-info/point-max content))
358 (ctype (mime::content-info/type content))
359 (params (mime::content-info/parameters content))
360 (encoding (mime::content-info/encoding content))
361 (cnum (mime::content-info/rcnum content))
369 (setq he (if (re-search-forward "^$" nil t)
376 (narrow-to-region beg end)
377 (setq subj (mime-viewer/get-subject params encoding))
381 (narrow-to-region nb nb)
382 (funcall mime-viewer/content-subject-function
383 cnum cinfo ctype params subj)
385 (if (mime-viewer/header-visible-p cnum cinfo ctype)
386 (mime-viewer/display-header beg he obuf)
388 (cond ((mime-viewer/body-visible-p cnum cinfo ctype)
389 (mime-viewer/display-body he end obuf
390 cnum cinfo ctype params subj encoding)
392 ((equal ctype "message/partial")
393 (mime-viewer/display-message/partial obuf)
396 (null (mime::content-info/children cinfo))
399 (goto-char (point-max))
400 (mime-viewer/insert-content-subject cnum cinfo ctype params subj)
402 (t (set-buffer obuf))
404 (mime-viewer/default-content-separator cnum cinfo ctype params subj)
407 (setq ne (point-max))
409 (mime::preview-content-info/create nb (1- ne) ibuf content)
414 (defun mime-viewer/display-header (beg end obuf)
415 (let ((str (buffer-substring beg end))
416 (f (assq major-mode mime-viewer/content-header-filter-alist))
421 (narrow-to-region (point)(point))
423 (if (and f (setq f (cdr f)))
425 (mime-viewer/default-content-header-filter)
427 (run-hooks 'mime-viewer/content-header-filter-hook)
430 (defun mime-viewer/display-body (beg end obuf
431 cnum cinfo ctype params subj encoding)
432 (let ((str (buffer-substring beg end))
436 (setq be (point-max))
437 (narrow-to-region be be)
439 (let ((f (assoc-value ctype mime-viewer/content-filter-alist)))
440 (if (and f (fboundp f))
441 (funcall f ctype params encoding)
442 (mime-viewer/default-content-filter cnum cinfo ctype params subj)
446 (defun mime-viewer/display-message/partial (obuf)
449 (goto-char (point-max))
450 (if (not (search-backward "\n\n" nil t))
453 (let ((be (point-max)))
454 (narrow-to-region be be)
455 (insert mime-viewer/announcement-for-message/partial)
456 (tm:add-button (point-min)(point-max)
457 (function mime-viewer/play-content))
460 (defun mime-viewer/get-subject (param &optional encoding)
461 (if (member (or encoding
462 (cdr (assq 'encoding param))
464 mime-viewer/uuencode-encoding-name-list)
466 (or (if (re-search-forward "^begin [0-9]+ " nil t)
467 (if (looking-at ".+$")
468 (buffer-substring (match-beginning 0)(match-end 0))
472 (or (and (setq ret (assoc "name" param))
473 (rfc822/strip-quoted-string (cdr ret))
475 (and (setq ret (assoc "x-name" param))
476 (rfc822/strip-quoted-string (cdr ret))
481 (goto-char (point-min))
482 (narrow-to-region (point-min)
483 (or (and (search-forward "\n\n" nil t)
488 (rfc822/get-field-body "Content-Description")
489 (rfc822/get-field-body "Subject")
495 ;;; @ content information
498 (defun mime-article/point-content-number (p &optional cinfo)
500 (setq cinfo mime::article/content-info)
502 (let ((b (mime::content-info/point-min cinfo))
503 (e (mime::content-info/point-max cinfo))
504 (c (mime::content-info/children cinfo))
506 (if (and (<= b p)(<= p e))
507 (or (let (co ret (sn 0))
511 (setq ret (mime-article/point-content-number p co))
512 (cond ((eq ret t) (throw 'tag (list sn)))
513 (ret (throw 'tag (cons sn ret)))
520 (defun mime-article/cnum-to-cinfo (cn &optional cinfo)
522 (setq cinfo mime::article/content-info)
529 (let ((rc (nth sn (mime::content-info/children cinfo))))
531 (mime-article/cnum-to-cinfo (cdr cn) rc)
535 (defun mime/flatten-content-info (&optional cinfo)
537 (setq cinfo mime::article/content-info)
539 (let ((dest (list cinfo))
540 (rcl (mime::content-info/children cinfo))
543 (setq dest (nconc dest (mime/flatten-content-info (car rcl))))
548 (defun mime::point-preview-content (p &optional pcl)
550 (setq pcl mime::preview/content-list)
556 (if (and (<= (mime::preview-content-info/point-min cell) p)
557 (<= p (mime::preview-content-info/point-max cell))
570 (defvar mime-viewer/code-converter-alist nil)
572 (defun mime-viewer/default-code-convert-region
573 (beg end charset &optional encoding)
574 (if (member encoding '("quoted-printable" "base64"))
575 (mime/code-convert-region-to-emacs beg (point-max) charset)
578 (defun mime-viewer/filter-text/plain (ctype params encoding)
579 (let ((charset (cdr (assoc "charset" params)))
580 (beg (point-min)) (end (point-max))
582 (goto-char (point-min))
583 (cond ((string= encoding "quoted-printable")
584 (quoted-printable-decode-region beg end)
586 ((string= encoding "base64")
587 (base64-decode-region beg end)
589 (let* ((mode mime::preview/original-major-mode)
590 (m (assq mode mime-viewer/code-converter-alist))
592 (if (and m (fboundp (setq m (cdr m))))
593 (funcall m beg (point-max) charset encoding)
594 (mime-viewer/default-code-convert-region beg (point-max)
597 (goto-char (point-max))
598 (if (not (eq (char-after (1- (point))) ?\n))
601 (if browse-url-browser-function
603 (goto-char (point-min))
604 (while (re-search-forward tm:URL-regexp nil t)
605 (tm:add-button (match-beginning 0)(match-end 0)
606 (function tm:browse-url))
608 (run-hooks 'mime-viewer/plain-text-preview-hook)
612 ;;; @ MIME viewer mode
615 (defvar mime/viewer-mode-map nil)
616 (if (null mime/viewer-mode-map)
618 (setq mime/viewer-mode-map (make-keymap))
619 (suppress-keymap mime/viewer-mode-map)
620 (define-key mime/viewer-mode-map
621 "u" (function mime-viewer/up-content))
622 (define-key mime/viewer-mode-map
623 "p" (function mime-viewer/previous-content))
624 (define-key mime/viewer-mode-map
625 "n" (function mime-viewer/next-content))
626 (define-key mime/viewer-mode-map
627 " " (function mime-viewer/scroll-up-content))
628 (define-key mime/viewer-mode-map
629 "\M- " (function mime-viewer/scroll-down-content))
630 (define-key mime/viewer-mode-map
631 "\177" (function mime-viewer/scroll-down-content))
632 (define-key mime/viewer-mode-map
633 "\C-m" (function mime-viewer/next-line-content))
634 (define-key mime/viewer-mode-map
635 "\C-\M-m" (function mime-viewer/previous-line-content))
636 (define-key mime/viewer-mode-map
637 "v" (function mime-viewer/play-content))
638 (define-key mime/viewer-mode-map
639 "e" (function mime-viewer/extract-content))
640 (define-key mime/viewer-mode-map
641 "\C-c\C-p" (function mime-viewer/print-content))
642 (define-key mime/viewer-mode-map
643 "f" (function mime-viewer/display-x-face))
644 (define-key mime/viewer-mode-map
645 "q" (function mime-viewer/quit))
646 (define-key mime/viewer-mode-map
647 "\C-c\C-x" (function mime-viewer/kill-buffer))
649 (define-key mime/viewer-mode-map
650 mouse-button-2 (function tm:button-dispatcher))
654 (defun mime/viewer-mode (&optional mother ctl encoding ibuf obuf)
655 "Major mode for viewing MIME message.
657 Here is a list of the standard keys for mime/viewer-mode.
662 u Move to upper content
663 p Move to previous content
664 n Move to next content
665 SPC Scroll up or move to next content
666 M-SPC Scroll down or move to previous content
667 DEL Scroll down or move to previous content
668 RET Move to next line
669 M-RET Move to previous line
670 v Decode current content as `play mode'
671 e Decode current content as `extract mode'
672 C-c C-p Decode current content as `print mode'
675 button-2 Move to point under the mouse cursor
676 and decode current content as `play mode'
679 Here are all the commands with their current binding,
682 \\{mime/viewer-mode-map}
685 (setq mime-viewer/ignored-field-regexp
687 (mapconcat (function regexp-quote)
688 mime-viewer/ignored-field-list "\\|")
690 (let ((buf (get-buffer mime/output-buffer-name)))
696 (let ((ret (mime-viewer/setup-buffer ctl encoding ibuf obuf)))
698 (switch-to-buffer (car ret))
701 (make-variable-buffer-local
702 'mime/show-mode-old-window-configuration)
703 (setq mime/show-mode-old-window-configuration
704 (current-window-configuration))
705 (make-variable-buffer-local 'mime/mother-buffer)
706 (setq mime/mother-buffer mother)
708 (use-local-map mime/viewer-mode-map)
709 (make-variable-buffer-local 'mime::preview/content-list)
710 (setq mime::preview/content-list (nth 1 ret))
712 (let ((ce (mime::preview-content-info/point-max
713 (car mime::preview/content-list)
716 (goto-char (point-min))
717 (search-forward "\n\n" nil t)
718 (setq e (match-end 0))
722 (run-hooks 'mime/viewer-mode-hook)
725 (defun mime-preview/point-content-number (point)
726 (save-window-excursion
727 (let ((pc (mime::point-preview-content (point)))
729 (switch-to-buffer (mime::preview-content-info/buffer pc))
730 (setq cinfo (mime::preview-content-info/content-info pc))
731 (mime-article/point-content-number (mime::content-info/point-min cinfo))
734 (defun mime-preview/cinfo-to-pcinfo (cinfo)
735 (let ((rpcl mime::preview/content-list) cell)
738 (setq cell (car rpcl))
739 (if (eq cinfo (mime::preview-content-info/content-info cell))
742 (setq rpcl (cdr rpcl))
745 (autoload 'mime-preview/decode-content "tm-play")
747 (defvar mime-viewer/decoding-mode "play" "MIME body decoding mode")
749 (defun mime-viewer/play-content ()
751 (let ((mime-viewer/decoding-mode "play"))
752 (mime-preview/decode-content)
755 (defun mime-viewer/extract-content ()
757 (let ((mime-viewer/decoding-mode "extract"))
758 (mime-preview/decode-content)
761 (defun mime-viewer/print-content ()
763 (let ((mime-viewer/decoding-mode "print"))
764 (mime-preview/decode-content)
767 (defun mime-viewer/display-x-face ()
769 (save-window-excursion
770 (set-buffer mime::preview/article-buffer)
771 (mime-viewer/x-face-function)
774 (defun mime-viewer/up-content ()
776 (let ((pc (mime::point-preview-content (point))) cinfo
777 (the-buf (current-buffer))
779 (switch-to-buffer (mime::preview-content-info/buffer pc))
780 (setq cinfo (mime::preview-content-info/content-info pc))
781 (setq cn (mime-article/point-content-number
782 (mime::content-info/point-min cinfo)))
784 (mime-viewer/quit the-buf
785 (mime::preview-content-info/buffer pc)
787 (setq r (mime-article/cnum-to-cinfo (butlast cn)))
788 (switch-to-buffer the-buf)
790 (let ((rpcl mime::preview/content-list) cell)
792 (setq cell (car rpcl))
793 (if (eq r (mime::preview-content-info/content-info cell))
795 (goto-char (mime::preview-content-info/point-min cell))
798 (setq rpcl (cdr rpcl))
802 (defun mime-viewer/previous-content ()
804 (let* ((pcl mime::preview/content-list)
806 (i (- (length pcl) 1))
810 (setq beg (mime::preview-content-info/point-min (nth i pcl)))
812 (throw 'tag (goto-char beg))
818 (defun mime-viewer/next-content ()
820 (let ((pcl mime::preview/content-list)
825 (setq beg (mime::preview-content-info/point-min (car pcl)))
827 (throw 'tag (goto-char beg))
833 (defun mime-viewer/scroll-up-content (&optional h)
836 (setq h (- (window-height) 1))
838 (let ((pcl mime::preview/content-list)
844 (setq beg (mime::preview-content-info/point-min (car pcl)))
858 (defun mime-viewer/scroll-down-content (&optional h)
861 (setq h (- (window-height) 1))
863 (let ((pcl mime::preview/content-list)
867 (or (let ((i (- (length pcl) 1)))
870 (setq beg (mime::preview-content-info/point-min
883 (defun mime-viewer/next-line-content ()
885 (mime-viewer/scroll-up-content 1)
888 (defun mime-viewer/previous-line-content ()
890 (mime-viewer/scroll-down-content 1)
893 (defun mime-viewer/quit (&optional the-buf buf)
896 (setq the-buf (current-buffer))
899 (setq buf (mime::preview-content-info/buffer
900 (mime::point-preview-content (point))))
903 (switch-to-buffer buf)
904 (assoc major-mode mime-viewer/quitting-method-alist)
908 (switch-to-buffer the-buf)
913 (defun mime-viewer/kill-buffer ()
915 (kill-buffer (current-buffer))
924 (run-hooks 'tm-view-load-hook)