1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Renamed: 1994/08/31 from tm-body.el
8 ;; Renamed: 1997/02/19 from tm-view.el
9 ;; Keywords: MIME, multimedia, mail, news
11 ;; This file is part of WEMI (Widget based 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.
32 (require 'eword-decode)
42 (defconst mime-view-version-string
43 `,(concat (car mime-module-version) " MIME-View "
44 (mapconcat #'number-to-string (cddr mime-module-version) ".")
45 " (" (cadr mime-module-version) ")"))
51 (defgroup mime-view nil
55 (defcustom mime-view-find-every-acting-situation t
56 "*Find every available acting-situation if non-nil."
60 (defcustom mime-acting-situation-examples-file "~/.mime-example"
61 "*File name of example about acting-situation demonstrated by user."
66 ;;; @ buffer local variables
72 (defvar mime-raw-message-info
73 "Information about structure of message.
74 Please use reference function `mime-entity-SLOT' to get value of SLOT.
76 Following is a list of slots of the structure:
78 node-id reversed entity-number (list of integers)
79 point-min beginning point of region in raw-buffer
80 point-max end point of region in raw-buffer
81 type media-type (symbol)
82 subtype media-subtype (symbol)
83 type/subtype media-type/subtype (string or nil)
84 parameters parameter of Content-Type field (association list)
85 encoding Content-Transfer-Encoding (string or nil)
86 children entities included in this entity (list of content-infos)
88 If an entity includes other entities in its body, such as multipart or
89 message/rfc822, `mime-entity' structures of them are included in
90 `children', so the `mime-entity' structure become a tree.")
91 (make-variable-buffer-local 'mime-raw-message-info)
93 (defvar mime-preview-buffer nil
94 "MIME-preview buffer corresponding with the (raw) buffer.")
95 (make-variable-buffer-local 'mime-preview-buffer)
98 ;;; @@ in preview-buffer
101 (defvar mime-mother-buffer nil
102 "Mother buffer corresponding with the (MIME-preview) buffer.
103 If current MIME-preview buffer is generated by other buffer, such as
104 message/partial, it is called `mother-buffer'.")
105 (make-variable-buffer-local 'mime-mother-buffer)
107 (defvar mime-raw-buffer nil
108 "Raw buffer corresponding with the (MIME-preview) buffer.")
109 (make-variable-buffer-local 'mime-raw-buffer)
111 (defvar mime-preview-original-major-mode nil
112 "Major-mode of mime-raw-buffer.")
113 (make-variable-buffer-local 'mime-preview-original-major-mode)
115 (defvar mime-preview-original-window-configuration nil
116 "Window-configuration before mime-view-mode is called.")
117 (make-variable-buffer-local 'mime-preview-original-window-configuration)
120 ;;; @ entity information
123 (defsubst mime-raw-find-entity-from-node-id (entity-node-id
124 &optional message-info)
125 "Return entity from ENTITY-NODE-ID in mime-raw-buffer.
126 If optional argument MESSAGE-INFO is not specified,
127 `mime-raw-message-info' is used."
128 (mime-raw-find-entity-from-number (reverse entity-node-id) message-info))
130 (defun mime-raw-find-entity-from-number (entity-number &optional message-info)
131 "Return entity from ENTITY-NUMBER in mime-raw-buffer.
132 If optional argument MESSAGE-INFO is not specified,
133 `mime-raw-message-info' is used."
135 (setq message-info mime-raw-message-info))
136 (if (eq entity-number t)
138 (let ((sn (car entity-number)))
141 (let ((rc (nth sn (mime-entity-children message-info))))
143 (mime-raw-find-entity-from-number (cdr entity-number) rc)
147 (defun mime-raw-find-entity-from-point (point &optional message-info)
148 "Return entity from POINT in mime-raw-buffer.
149 If optional argument MESSAGE-INFO is not specified,
150 `mime-raw-message-info' is used."
152 (setq message-info mime-raw-message-info))
153 (if (and (<= (mime-entity-point-min message-info) point)
154 (<= point (mime-entity-point-max message-info)))
155 (let ((children (mime-entity-children message-info)))
159 (mime-raw-find-entity-from-point point (car children))))
163 (setq children (cdr children)))
166 (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
167 "Return entity-node-id from POINT in mime-raw-buffer.
168 If optional argument MESSAGE-INFO is not specified,
169 `mime-raw-message-info' is used."
170 (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
172 (defsubst mime-raw-point-to-entity-number (point &optional message-info)
173 "Return entity-number from POINT in mime-raw-buffer.
174 If optional argument MESSAGE-INFO is not specified,
175 `mime-raw-message-info' is used."
176 (reverse (mime-raw-point-to-entity-node-id point message-info)))
178 (defsubst mime-raw-entity-parent (entity &optional message-info)
179 "Return mother entity of ENTITY.
180 If optional argument MESSAGE-INFO is not specified,
181 `mime-raw-message-info' is used."
182 (mime-raw-find-entity-from-node-id (cdr (mime-entity-node-id entity))
185 (defun mime-raw-flatten-message-info (&optional message-info)
186 "Return list of entity in mime-raw-buffer.
187 If optional argument MESSAGE-INFO is not specified,
188 `mime-raw-message-info' is used."
190 (setq message-info mime-raw-message-info))
191 (let ((dest (list message-info))
192 (rcl (mime-entity-children message-info)))
194 (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
195 (setq rcl (cdr rcl)))
199 ;;; @ presentation of preview
205 ;;; @@@ predicate function
208 (defun mime-view-entity-button-visible-p (entity message-info)
209 "Return non-nil if header of ENTITY is visible.
210 Please redefine this function if you want to change default setting."
211 (let ((media-type (mime-entity-media-type entity))
212 (media-subtype (mime-entity-media-subtype entity)))
213 (or (not (eq media-type 'application))
214 (and (not (eq media-subtype 'x-selection))
215 (or (not (eq media-subtype 'octet-stream))
217 (mime-raw-entity-parent entity message-info)))
218 (or (not (eq (mime-entity-media-type mother-entity)
220 (not (eq (mime-entity-media-subtype mother-entity)
225 ;;; @@@ entity button generator
228 (defun mime-view-insert-entity-button (entity message-info subj)
229 "Insert entity-button of ENTITY."
230 (let ((entity-node-id (mime-entity-node-id entity))
231 (params (mime-entity-parameters entity)))
233 (let ((access-type (assoc "access-type" params))
234 (num (or (cdr (assoc "x-part-number" params))
235 (if (consp entity-node-id)
238 (format "%s" (1+ num))
240 (reverse entity-node-id) ".")
244 (let ((server (assoc "server" params)))
245 (setq access-type (cdr access-type))
247 (format "%s %s ([%s] %s)"
248 num subj access-type (cdr server))
249 (let ((site (cdr (assoc "site" params)))
250 (dir (cdr (assoc "directory" params)))
252 (format "%s %s ([%s] %s:%s)"
253 num subj access-type site dir)
257 (let ((media-type (mime-entity-media-type entity))
258 (media-subtype (mime-entity-media-subtype entity))
259 (charset (cdr (assoc "charset" params)))
260 (encoding (mime-entity-encoding entity)))
264 (format " <%s/%s%s%s>"
265 media-type media-subtype
267 (concat "; " charset)
270 (concat " (" encoding ")")
272 (if (>= (+ (current-column)(length rest))(window-width))
276 (function mime-preview-play-current-entity))
283 ;;; @@@ predicate function
286 ;; (defvar mime-view-childrens-header-showing-Content-Type-list
287 ;; '("message/rfc822" "message/news"))
289 ;; (defun mime-view-header-visible-p (entity message-info)
290 ;; "Return non-nil if header of ENTITY is visible."
291 ;; (let ((entity-node-id (mime-entity-node-id entity)))
292 ;; (member (mime-entity-type/subtype
293 ;; (mime-raw-find-entity-from-node-id
294 ;; (cdr entity-node-id) message-info))
295 ;; mime-view-childrens-header-showing-Content-Type-list)
298 ;;; @@@ entity header filter
301 (defvar mime-view-content-header-filter-alist nil)
303 (defun mime-view-default-content-header-filter ()
304 (mime-view-cut-header)
305 (eword-decode-header)
308 ;;; @@@ entity field cutter
311 (defvar mime-view-ignored-field-list
312 '(".*Received" ".*Path" ".*Id" "References"
313 "Replied" "Errors-To"
314 "Lines" "Sender" ".*Host" "Xref"
315 "Content-Type" "Precedence"
317 "All fields that match this list will be hidden in MIME preview buffer.
318 Each elements are regexp of field-name.")
320 (defvar mime-view-ignored-field-regexp
322 (apply (function regexp-or) mime-view-ignored-field-list)
325 (defvar mime-view-visible-field-list '("Dnas.*" "Message-Id")
326 "All fields that match this list will be displayed in MIME preview buffer.
327 Each elements are regexp of field-name.")
329 (defun mime-view-cut-header ()
330 (goto-char (point-min))
331 (while (re-search-forward mime-view-ignored-field-regexp nil t)
332 (let* ((beg (match-beginning 0))
334 (name (buffer-substring beg end))
337 (let ((rest mime-view-visible-field-list))
339 (if (string-match (car rest) name)
342 (setq rest (cdr rest))))
345 (if (re-search-forward "^\\([^ \t]\\|$\\)" nil t)
354 ;;; @@@ predicate function
357 (defun mime-calist::field-match-method-as-default-rule (calist
358 field-type field-value)
359 (let ((s-field (assq field-type calist)))
360 (cond ((null s-field)
361 (cons (cons field-type field-value) calist)
365 (define-calist-field-match-method
366 'header #'mime-calist::field-match-method-as-default-rule)
368 (define-calist-field-match-method
369 'body #'mime-calist::field-match-method-as-default-rule)
372 (defvar mime-preview-condition nil
373 "Condition-tree about how to display entity.")
375 (ctree-set-calist-strictly
376 'mime-preview-condition '((type . application)(subtype . octet-stream)
379 (ctree-set-calist-strictly
380 'mime-preview-condition '((type . application)(subtype . octet-stream)
383 (ctree-set-calist-strictly
384 'mime-preview-condition '((type . application)(subtype . octet-stream)
388 (ctree-set-calist-strictly
389 'mime-preview-condition '((type . application)(subtype . pgp)
392 (ctree-set-calist-strictly
393 'mime-preview-condition '((type . application)(subtype . x-latex)
396 (ctree-set-calist-strictly
397 'mime-preview-condition '((type . application)(subtype . x-selection)
400 (ctree-set-calist-strictly
401 'mime-preview-condition '((type . application)(subtype . x-comment)
404 (ctree-set-calist-strictly
405 'mime-preview-condition '((type . message)(subtype . delivery-status)
408 (ctree-set-calist-strictly
409 'mime-preview-condition '((body . visible)
410 (body-presentation-method . with-filter)
411 (body-filter . mime-preview-filter-for-text/plain)))
413 (ctree-set-calist-strictly
414 'mime-preview-condition '((type . nil)
416 (body-presentation-method . with-filter)
417 (body-filter . mime-preview-filter-for-text/plain)))
419 (ctree-set-calist-strictly
420 'mime-preview-condition '((type . text)(subtype . enriched)
422 (body-presentation-method . with-filter)
424 . mime-preview-filter-for-text/enriched)))
426 (ctree-set-calist-strictly
427 'mime-preview-condition '((type . text)(subtype . richtext)
429 (body-presentation-method . with-filter)
431 . mime-preview-filter-for-text/richtext)))
433 (ctree-set-calist-strictly
434 'mime-preview-condition '((type . text)(subtype . t)
436 (body-presentation-method . with-filter)
437 (body-filter . mime-preview-filter-for-text/plain)))
439 (ctree-set-calist-strictly
440 'mime-preview-condition '((type . message)(subtype . partial)
441 (body-presentation-method
442 . mime-view-insert-message/partial-button)))
444 (ctree-set-calist-strictly
445 'mime-preview-condition '((type . message)(subtype . rfc822)
446 (body-presentation-method . nil)
447 (childrens-situation (header . visible)
448 (entity-button . invisible))))
450 (ctree-set-calist-strictly
451 'mime-preview-condition '((type . message)(subtype . news)
452 (body-presentation-method . nil)
453 (childrens-situation (header . visible)
454 (entity-button . invisible))))
457 ;;; @@@ entity filter
460 (autoload 'mime-preview-filter-for-text/plain "mime-text")
461 (autoload 'mime-preview-filter-for-text/enriched "mime-text")
462 (autoload 'mime-preview-filter-for-text/richtext "mime-text")
464 (defvar mime-text-decoder-alist
465 '((mime-show-message-mode . mime-text-decode-buffer)
466 (mime-temp-message-mode . mime-text-decode-buffer)
467 (t . mime-text-decode-buffer-maybe)
469 "Alist of major-mode vs. mime-text-decoder.
470 Each element looks like (SYMBOL . FUNCTION). SYMBOL is major-mode or
473 Specification of FUNCTION is described in DOC-string of variable
476 This value is overridden by buffer local variable `mime-text-decoder'
480 (defvar mime-view-announcement-for-message/partial
481 (if (and (>= emacs-major-version 19) window-system)
483 This is message/partial style split message.
484 Please press `v' key in this buffer or click here by mouse button-2."
486 This is message/partial style split message.
487 Please press `v' key in this buffer."
490 (defun mime-view-insert-message/partial-button (&optional situation)
492 (goto-char (point-max))
493 (if (not (search-backward "\n\n" nil t))
496 (goto-char (point-max))
497 ;;(narrow-to-region (point-max)(point-max))
498 ;;(insert mime-view-announcement-for-message/partial)
499 ;; (mime-add-button (point-min)(point-max)
500 ;; #'mime-preview-play-current-entity)
501 (mime-insert-button mime-view-announcement-for-message/partial
502 #'mime-preview-play-current-entity)
506 ;;; @ acting-condition
509 (defvar mime-acting-condition nil
510 "Condition-tree about how to process entity.")
512 (if (file-readable-p mailcap-file)
513 (let ((entries (mailcap-parse-file)))
515 (let ((entry (car entries))
518 (let* ((field (car entry))
519 (field-type (car field)))
520 (cond ((eq field-type 'view) (setq view field))
521 ((eq field-type 'print) (setq print field))
522 ((memq field-type '(compose composetyped edit)))
523 (t (setq shared (cons field shared))))
525 (setq entry (cdr entry))
527 (setq shared (nreverse shared))
528 (ctree-set-calist-strictly
529 'mime-acting-condition
530 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
532 (ctree-set-calist-strictly
533 'mime-acting-condition
535 (list '(mode . "print")(cons 'method (cdr view))))
538 (setq entries (cdr entries))
541 ;; (ctree-set-calist-strictly
542 ;; 'mime-acting-condition
543 ;; '((type . t)(subtype . t)(mode . "extract")
544 ;; (method . mime-method-to-save)))
545 (ctree-set-calist-with-default
546 'mime-acting-condition
548 (method . mime-method-to-save)))
550 ;; (ctree-set-calist-strictly
551 ;; 'mime-acting-condition
552 ;; '((type . text)(subtype . plain)(mode . "play")
553 ;; (method "tm-plain" nil 'file "" 'encoding 'mode 'name)
555 ;; (ctree-set-calist-strictly
556 ;; 'mime-acting-condition
557 ;; '((type . text)(subtype . plain)(mode . "print")
558 ;; (method "tm-plain" nil 'file "" 'encoding 'mode 'name)
560 ;; (ctree-set-calist-strictly
561 ;; 'mime-acting-condition
562 ;; '((type . text)(subtype . html)(mode . "play")
563 ;; (method "tm-html" nil 'file "" 'encoding 'mode 'name)
565 (ctree-set-calist-strictly
566 'mime-acting-condition
567 '((type . text)(subtype . x-rot13-47)(mode . "play")
568 (method . mime-method-to-display-caesar)
570 (ctree-set-calist-strictly
571 'mime-acting-condition
572 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
573 (method . mime-method-to-display-caesar)
576 ;; (ctree-set-calist-strictly
577 ;; 'mime-acting-condition
578 ;; '((type . audio)(subtype . basic)(mode . "play")
579 ;; (method "tm-au" nil 'file "" 'encoding 'mode 'name)
582 ;; (ctree-set-calist-strictly
583 ;; 'mime-acting-condition
584 ;; '((type . image)(mode . "play")
585 ;; (method "tm-image" nil 'file "" 'encoding 'mode 'name)
587 ;; (ctree-set-calist-strictly
588 ;; 'mime-acting-condition
589 ;; '((type . image)(mode . "print")
590 ;; (method "tm-image" nil 'file "" 'encoding 'mode 'name)
593 ;; (ctree-set-calist-strictly
594 ;; 'mime-acting-condition
595 ;; '((type . video)(subtype . mpeg)(mode . "play")
596 ;; (method "tm-mpeg" nil 'file "" 'encoding 'mode 'name)
599 ;; (ctree-set-calist-strictly
600 ;; 'mime-acting-condition
601 ;; '((type . application)(subtype . postscript)(mode . "play")
602 ;; (method "tm-ps" nil 'file "" 'encoding 'mode 'name)
604 ;; (ctree-set-calist-strictly
605 ;; 'mime-acting-condition
606 ;; '((type . application)(subtype . postscript)(mode . "print")
607 ;; (method "tm-ps" nil 'file "" 'encoding 'mode 'name)
610 (ctree-set-calist-strictly
611 'mime-acting-condition
612 '((type . message)(subtype . rfc822)(mode . "play")
613 (method . mime-method-to-display-message/rfc822)
615 (ctree-set-calist-strictly
616 'mime-acting-condition
617 '((type . message)(subtype . partial)(mode . "play")
618 (method . mime-method-to-store-message/partial)
621 (ctree-set-calist-strictly
622 'mime-acting-condition
623 '((type . message)(subtype . external-body)
624 ("access-type" . "anon-ftp")
625 (method . mime-method-to-display-message/external-ftp)
628 (ctree-set-calist-strictly
629 'mime-acting-condition
630 '((type . application)(subtype . octet-stream)
631 (method . mime-method-to-save)
635 ;;; @ quitting method
638 (defvar mime-preview-quitting-method-alist
639 '((mime-show-message-mode
640 . mime-preview-quitting-method-for-mime-show-message-mode))
641 "Alist of major-mode vs. quitting-method of mime-view.")
643 (defvar mime-view-over-to-previous-method-alist nil)
644 (defvar mime-view-over-to-next-method-alist nil)
646 (defvar mime-view-show-summary-method nil
647 "Alist of major-mode vs. show-summary-method.")
650 ;;; @ following method
653 (defvar mime-view-following-method-alist nil
654 "Alist of major-mode vs. following-method of mime-view.")
656 (defvar mime-view-following-required-fields-list
663 ;; hack from Gnus 5.0.4.
665 (defvar mime-view-x-face-to-pbm-command
666 "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm")
668 (defvar mime-view-x-face-command
669 (concat mime-view-x-face-to-pbm-command
671 "String to be executed to display an X-Face field.
672 The command will be executed in a sub-shell asynchronously.
673 The compressed face will be piped to this command.")
675 (defun mime-view-x-face-function ()
676 "Function to display X-Face field. You can redefine to customize."
677 ;; 1995/10/12 (c.f. tm-eng:130)
678 ;; fixed by Eric Ding <ericding@San-Jose.ate.slb.com>
680 (narrow-to-region (point-min) (re-search-forward "^$" nil t))
682 (goto-char (point-min))
683 (if (re-search-forward "^X-Face:[ \t]*" nil t)
684 (let ((beg (match-end 0))
685 (end (std11-field-end))
687 (call-process-region beg end "sh" nil 0 nil
688 "-c" mime-view-x-face-command)
695 (defvar mime-view-uuencode-encoding-name-list '("x-uue" "x-uuencode"))
697 (defvar mime-raw-buffer-coding-system-alist
698 `((t . ,(mime-charset-to-coding-system default-mime-charset)))
699 "Alist of major-mode vs. corresponding coding-system of `mime-raw-buffer'.")
705 (defvar mime-view-redisplay nil)
707 (defun mime-view-setup-buffers (&optional ctl encoding ibuf obuf)
713 (or mime-view-redisplay
714 (setq mime-raw-message-info (mime-parse-message ctl encoding))
716 (let ((message-info mime-raw-message-info)
717 (the-buf (current-buffer))
720 (setq obuf (concat "*Preview-" (buffer-name the-buf) "*")))
721 (set-buffer (get-buffer-create obuf))
722 (let ((inhibit-read-only t))
723 ;;(setq buffer-read-only nil)
726 (setq mime-raw-buffer the-buf)
727 (setq mime-preview-original-major-mode mode)
728 (setq major-mode 'mime-view-mode)
729 (setq mode-name "MIME-View")
730 (mime-view-display-message message-info the-buf obuf)
731 (set-buffer-modified-p nil)
733 (setq buffer-read-only t)
736 (setq mime-preview-buffer obuf)
739 (defun mime-view-display-message (message-info ibuf obuf)
740 (let* ((start (mime-entity-point-min message-info))
741 (end (mime-entity-point-max message-info))
742 (media-type (mime-entity-media-type message-info))
743 (media-subtype (mime-entity-media-subtype message-info))
744 (params (mime-entity-parameters message-info))
745 (encoding (mime-entity-encoding message-info))
746 end-of-header e nb ne subj)
749 (setq end-of-header (if (re-search-forward "^$" nil t)
752 (if (> end-of-header end)
753 (setq end-of-header end)
756 (narrow-to-region start end)
759 (mime-raw-get-subject params encoding)))
763 (narrow-to-region nb nb)
764 ;; Insert message-header
766 (narrow-to-region (point)(point))
767 (insert-buffer-substring mime-raw-buffer start end-of-header)
768 (let ((f (cdr (assq mime-preview-original-major-mode
769 mime-view-content-header-filter-alist))))
772 (mime-view-default-content-header-filter)
774 (run-hooks 'mime-view-content-header-filter-hook)
777 (ctree-match-calist mime-preview-condition
778 (list* (cons 'type media-type)
779 (cons 'subtype media-subtype)
780 (cons 'encoding encoding)
781 (cons 'major-mode major-mode)
784 (cdr (assq 'message-button situation)))
785 (body-presentation-method
786 (cdr (assq 'body-presentation-method situation))))
787 (when (eq message-button 'visible)
788 (goto-char (point-max))
789 (mime-view-insert-entity-button message-info message-info subj)
791 (cond ((eq body-presentation-method 'with-filter)
792 (let ((body-filter (cdr (assq 'body-filter situation))))
794 (narrow-to-region (point-max)(point-max))
795 (insert-buffer-substring mime-raw-buffer end-of-header end)
796 (funcall body-filter situation)
798 ((functionp body-presentation-method)
799 (funcall body-presentation-method situation)
801 ((null (mime-entity-children message-info))
802 (goto-char (point-max))
803 (mime-view-insert-entity-button message-info message-info subj)
805 (setq ne (point-max))
807 (put-text-property nb ne 'mime-view-raw-buffer ibuf)
808 (put-text-property nb ne 'mime-view-entity message-info)
810 (let ((children (mime-entity-children message-info))
812 (cdr (assq 'childrens-situation situation))))
814 (mime-view-display-entity (car children) message-info ibuf obuf
816 (setq children (cdr children))
819 (defun mime-view-display-entity (entity message-info ibuf obuf
821 (let* ((start (mime-entity-point-min entity))
822 (end (mime-entity-point-max entity))
823 (media-type (mime-entity-media-type entity))
824 (media-subtype (mime-entity-media-subtype entity))
825 (params (mime-entity-parameters entity))
826 (encoding (mime-entity-encoding entity))
827 end-of-header e nb ne subj)
830 (setq end-of-header (if (re-search-forward "^$" nil t)
833 (if (> end-of-header end)
834 (setq end-of-header end)
837 (narrow-to-region start end)
840 (mime-raw-get-subject params encoding)))
843 (ctree-match-calist mime-preview-condition
844 (list* (cons 'type media-type)
845 (cons 'subtype media-subtype)
846 (cons 'encoding encoding)
847 (cons 'major-mode major-mode)
849 default-situation))))
851 (eq (cdr (assq 'entity-button situation)) 'invisible))
853 (eq (cdr (assq 'header situation)) 'visible))
854 (body-presentation-method
855 (cdr (assq 'body-presentation-method situation))))
858 (narrow-to-region nb nb)
859 (or button-is-invisible
860 (if (mime-view-entity-button-visible-p entity message-info)
861 (mime-view-insert-entity-button entity message-info subj)
863 (if header-is-visible
865 (narrow-to-region (point)(point))
866 (insert-buffer-substring mime-raw-buffer start end-of-header)
867 (let ((f (cdr (assq mime-preview-original-major-mode
868 mime-view-content-header-filter-alist))))
871 (mime-view-default-content-header-filter)
873 (run-hooks 'mime-view-content-header-filter-hook)
875 (cond ((eq body-presentation-method 'with-filter)
876 (let ((body-filter (cdr (assq 'body-filter situation))))
878 (narrow-to-region (point-max)(point-max))
879 (insert-buffer-substring mime-raw-buffer end-of-header end)
880 (funcall body-filter situation)
882 ((functionp body-presentation-method)
883 (funcall body-presentation-method situation)
885 (or header-is-visible
886 body-presentation-method
888 (goto-char (point-max))
891 (setq ne (point-max))
893 (put-text-property nb ne 'mime-view-raw-buffer ibuf)
894 (put-text-property nb ne 'mime-view-entity entity)
896 (let ((children (mime-entity-children entity))
898 (cdr (assq 'childrens-situation situation))))
900 (mime-view-display-entity (car children) message-info ibuf obuf
902 (setq children (cdr children))
905 (defun mime-raw-get-uu-filename (param &optional encoding)
906 (if (member (or encoding
907 (cdr (assq 'encoding param))
909 mime-view-uuencode-encoding-name-list)
911 (or (if (re-search-forward "^begin [0-9]+ " nil t)
912 (if (looking-at ".+$")
913 (buffer-substring (match-beginning 0)(match-end 0))
918 (defun mime-raw-get-subject (param &optional encoding)
919 (or (std11-find-field-body '("Content-Description" "Subject"))
921 (if (or (and (setq ret (mime/Content-Disposition))
922 (setq ret (assoc "filename" (cdr ret)))
924 (setq ret (assoc "name" param))
925 (setq ret (assoc "x-name" param))
927 (std11-strip-quoted-string (cdr ret))
929 (mime-raw-get-uu-filename param encoding)
933 ;;; @ MIME viewer mode
936 (defconst mime-view-menu-title "MIME-View")
937 (defconst mime-view-menu-list
938 '((up "Move to upper entity" mime-preview-move-to-upper)
939 (previous "Move to previous entity" mime-preview-move-to-previous)
940 (next "Move to next entity" mime-preview-move-to-next)
941 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
942 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
943 (play "Play current entity" mime-preview-play-current-entity)
944 (extract "Extract current entity" mime-preview-extract-current-entity)
945 (print "Print current entity" mime-preview-print-current-entity)
946 (x-face "Show X Face" mime-preview-display-x-face)
948 "Menu for MIME Viewer")
950 (cond (running-xemacs
951 (defvar mime-view-xemacs-popup-menu
952 (cons mime-view-menu-title
955 (vector (nth 1 item)(nth 2 item) t)
957 mime-view-menu-list)))
958 (defun mime-view-xemacs-popup-menu (event)
959 "Popup the menu in the MIME Viewer buffer"
961 (select-window (event-window event))
962 (set-buffer (event-buffer event))
963 (popup-menu 'mime-view-xemacs-popup-menu))
964 (defvar mouse-button-2 'button2)
967 (defvar mouse-button-2 [mouse-2])
970 (defun mime-view-define-keymap (&optional default)
971 (let ((mime-view-mode-map (if (keymapp default)
972 (copy-keymap default)
975 (define-key mime-view-mode-map
976 "u" (function mime-preview-move-to-upper))
977 (define-key mime-view-mode-map
978 "p" (function mime-preview-move-to-previous))
979 (define-key mime-view-mode-map
980 "n" (function mime-preview-move-to-next))
981 (define-key mime-view-mode-map
982 "\e\t" (function mime-preview-move-to-previous))
983 (define-key mime-view-mode-map
984 "\t" (function mime-preview-move-to-next))
985 (define-key mime-view-mode-map
986 " " (function mime-preview-scroll-up-entity))
987 (define-key mime-view-mode-map
988 "\M- " (function mime-preview-scroll-down-entity))
989 (define-key mime-view-mode-map
990 "\177" (function mime-preview-scroll-down-entity))
991 (define-key mime-view-mode-map
992 "\C-m" (function mime-preview-next-line-entity))
993 (define-key mime-view-mode-map
994 "\C-\M-m" (function mime-preview-previous-line-entity))
995 (define-key mime-view-mode-map
996 "v" (function mime-preview-play-current-entity))
997 (define-key mime-view-mode-map
998 "e" (function mime-preview-extract-current-entity))
999 (define-key mime-view-mode-map
1000 "\C-c\C-p" (function mime-preview-print-current-entity))
1001 (define-key mime-view-mode-map
1002 "a" (function mime-preview-follow-current-entity))
1003 (define-key mime-view-mode-map
1004 "q" (function mime-preview-quit))
1005 (define-key mime-view-mode-map
1006 "h" (function mime-preview-show-summary))
1007 (define-key mime-view-mode-map
1008 "\C-c\C-x" (function mime-preview-kill-buffer))
1009 ;; (define-key mime-view-mode-map
1010 ;; "<" (function beginning-of-buffer))
1011 ;; (define-key mime-view-mode-map
1012 ;; ">" (function end-of-buffer))
1013 (define-key mime-view-mode-map
1014 "?" (function describe-mode))
1015 (define-key mime-view-mode-map
1016 [tab] (function mime-preview-move-to-next))
1017 (define-key mime-view-mode-map
1018 [delete] (function mime-preview-scroll-down-entity))
1019 (define-key mime-view-mode-map
1020 [backspace] (function mime-preview-scroll-down-entity))
1021 (if (functionp default)
1022 (cond (running-xemacs
1023 (set-keymap-default-binding mime-view-mode-map default)
1026 (setq mime-view-mode-map
1027 (append mime-view-mode-map (list (cons t default))))
1030 (define-key mime-view-mode-map
1031 mouse-button-2 (function mime-button-dispatcher))
1033 (cond (running-xemacs
1034 (define-key mime-view-mode-map
1035 mouse-button-3 (function mime-view-xemacs-popup-menu))
1037 ((>= emacs-major-version 19)
1038 (define-key mime-view-mode-map [menu-bar mime-view]
1039 (cons mime-view-menu-title
1040 (make-sparse-keymap mime-view-menu-title)))
1043 (define-key mime-view-mode-map
1044 (vector 'menu-bar 'mime-view (car item))
1045 (cons (nth 1 item)(nth 2 item))
1048 (reverse mime-view-menu-list)
1051 (use-local-map mime-view-mode-map)
1052 (run-hooks 'mime-view-define-keymap-hook)
1055 (defsubst mime-maybe-hide-echo-buffer ()
1056 "Clear mime-echo buffer and delete window for it."
1057 (let ((buf (get-buffer mime-echo-buffer-name)))
1062 (let ((win (get-buffer-window buf)))
1069 (defun mime-view-mode (&optional mother ctl encoding ibuf obuf
1070 default-keymap-or-function)
1071 "Major mode for viewing MIME message.
1073 Here is a list of the standard keys for mime-view-mode.
1078 u Move to upper content
1079 p or M-TAB Move to previous content
1080 n or TAB Move to next content
1081 SPC Scroll up or move to next content
1082 M-SPC or DEL Scroll down or move to previous content
1083 RET Move to next line
1084 M-RET Move to previous line
1085 v Decode current content as `play mode'
1086 e Decode current content as `extract mode'
1087 C-c C-p Decode current content as `print mode'
1088 a Followup to current content.
1091 button-2 Move to point under the mouse cursor
1092 and decode current content as `play mode'
1095 (mime-maybe-hide-echo-buffer)
1096 (let ((ret (mime-view-setup-buffers ctl encoding ibuf obuf))
1097 (win-conf (current-window-configuration))
1100 (switch-to-buffer ret)
1101 (setq mime-preview-original-window-configuration win-conf)
1104 (setq mime-mother-buffer mother)
1106 (mime-view-define-keymap default-keymap-or-function)
1108 (next-single-property-change (point-min) 'mime-view-entity)))
1111 (goto-char (point-min))
1112 (search-forward "\n\n" nil t)
1114 (run-hooks 'mime-view-mode-hook)
1121 (autoload 'mime-preview-play-current-entity "mime-play"
1122 "Play current entity." t)
1124 (defun mime-preview-extract-current-entity ()
1125 "Extract current entity into file (maybe).
1126 It decodes current entity to call internal or external method as
1127 \"extract\" mode. The method is selected from variable
1128 `mime-acting-condition'."
1130 (mime-preview-play-current-entity "extract")
1133 (defun mime-preview-print-current-entity ()
1134 "Print current entity (maybe).
1135 It decodes current entity to call internal or external method as
1136 \"print\" mode. The method is selected from variable
1137 `mime-acting-condition'."
1139 (mime-preview-play-current-entity "print")
1146 (defun mime-preview-original-major-mode ()
1147 "Return major-mode of original buffer.
1148 If a current buffer has mime-mother-buffer, return original major-mode
1149 of the mother-buffer."
1150 (if mime-mother-buffer
1152 (set-buffer mime-mother-buffer)
1153 (mime-preview-original-major-mode)
1155 mime-preview-original-major-mode))
1157 (defun mime-preview-follow-current-entity ()
1158 "Write follow message to current entity.
1159 It calls following-method selected from variable
1160 `mime-view-following-method-alist'."
1163 (while (null (setq entity
1164 (get-text-property (point) 'mime-view-entity)))
1168 (previous-single-property-change (point) 'mime-view-entity))
1170 (entity-node-id (mime-entity-node-id entity))
1171 (len (length entity-node-id))
1175 (if (eq (next-single-property-change (point-min)
1181 ((eq (next-single-property-change p-beg 'mime-view-entity)
1183 (setq p-beg (point))
1185 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1187 (setq p-end (point-max))
1189 ((null entity-node-id)
1190 (setq p-end (point-max))
1198 (next-single-property-change
1199 (point) 'mime-view-entity))
1201 (let ((rc (mime-entity-node-id
1202 (get-text-property (point)
1203 'mime-view-entity))))
1204 (or (equal entity-node-id
1205 (nthcdr (- (length rc) len) rc))
1210 (setq p-end (point-max))
1213 (let* ((mode (mime-preview-original-major-mode))
1215 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1217 (the-buf (current-buffer))
1218 (a-buf mime-raw-buffer)
1221 (set-buffer (setq new-buf (get-buffer-create new-name)))
1223 (insert-buffer-substring the-buf p-beg p-end)
1224 (goto-char (point-min))
1225 (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1233 (mime-raw-find-entity-from-node-id entity-node-id))
1236 (mime-entity-point-min ci)
1237 (mime-entity-point-max ci)
1239 (std11-header-string-except
1241 (apply (function regexp-or) fields)
1244 (eq (mime-entity-media-type ci) 'message)
1245 (eq (mime-entity-media-subtype ci) 'rfc822))
1251 (setq fields (std11-collect-field-names)
1252 entity-node-id (cdr entity-node-id))
1255 (let ((rest mime-view-following-required-fields-list))
1257 (let ((field-name (car rest)))
1258 (or (std11-field-body field-name)
1264 (set-buffer the-buf)
1265 (set-buffer mime-mother-buffer)
1266 (set-buffer mime-raw-buffer)
1267 (std11-field-body field-name)
1271 (setq rest (cdr rest))
1273 (eword-decode-header)
1275 (let ((f (cdr (assq mode mime-view-following-method-alist))))
1280 "Sorry, following method for %s is not implemented yet."
1289 (defun mime-preview-display-x-face ()
1291 (save-window-excursion
1292 (set-buffer mime-raw-buffer)
1293 (mime-view-x-face-function)
1300 (defun mime-preview-move-to-upper ()
1301 "Move to upper entity.
1302 If there is no upper entity, call function `mime-preview-quit'."
1305 (while (null (setq cinfo
1306 (get-text-property (point) 'mime-view-entity)))
1309 (let ((r (mime-raw-find-entity-from-node-id
1310 (cdr (mime-entity-node-id cinfo))
1311 (get-text-property 1 'mime-view-entity)))
1314 (while (setq point (previous-single-property-change
1315 (point) 'mime-view-entity))
1317 (if (eq r (get-text-property (point) 'mime-view-entity))
1324 (defun mime-preview-move-to-previous ()
1325 "Move to previous entity.
1326 If there is no previous entity, it calls function registered in
1327 variable `mime-view-over-to-previous-method-alist'."
1329 (while (null (get-text-property (point) 'mime-view-entity))
1333 (previous-single-property-change (point) 'mime-view-entity)))
1336 (let ((f (assq mime-preview-original-major-mode
1337 mime-view-over-to-previous-method-alist)))
1343 (defun mime-preview-move-to-next ()
1344 "Move to next entity.
1345 If there is no previous entity, it calls function registered in
1346 variable `mime-view-over-to-next-method-alist'."
1348 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1351 (let ((f (assq mime-preview-original-major-mode
1352 mime-view-over-to-next-method-alist)))
1358 (defun mime-preview-scroll-up-entity (&optional h)
1359 "Scroll up current entity.
1360 If reached to (point-max), it calls function registered in variable
1361 `mime-view-over-to-next-method-alist'."
1364 (setq h (1- (window-height)))
1366 (if (= (point) (point-max))
1367 (let ((f (assq mime-preview-original-major-mode
1368 mime-view-over-to-next-method-alist)))
1373 (or (next-single-property-change (point) 'mime-view-entity)
1376 (if (> (point) point)
1381 (defun mime-preview-scroll-down-entity (&optional h)
1382 "Scroll down current entity.
1383 If reached to (point-min), it calls function registered in variable
1384 `mime-view-over-to-previous-method-alist'."
1387 (setq h (1- (window-height)))
1389 (if (= (point) (point-min))
1390 (let ((f (assq mime-preview-original-major-mode
1391 mime-view-over-to-previous-method-alist)))
1398 (while (> (point) 1)
1400 (previous-single-property-change (point)
1406 (setq point (point-min))
1408 (forward-line (- h))
1409 (if (< (point) point)
1413 (defun mime-preview-next-line-entity ()
1415 (mime-preview-scroll-up-entity 1)
1418 (defun mime-preview-previous-line-entity ()
1420 (mime-preview-scroll-down-entity 1)
1427 (defun mime-preview-quit ()
1428 "Quit from MIME-preview buffer.
1429 It calls function registered in variable
1430 `mime-preview-quitting-method-alist'."
1432 (let ((r (assq mime-preview-original-major-mode
1433 mime-preview-quitting-method-alist)))
1438 (defun mime-preview-show-summary ()
1440 It calls function registered in variable
1441 `mime-view-show-summary-method'."
1443 (let ((r (assq mime-preview-original-major-mode
1444 mime-view-show-summary-method)))
1449 (defun mime-preview-kill-buffer ()
1451 (kill-buffer (current-buffer))
1458 (provide 'mime-view)
1460 (run-hooks 'mime-view-load-hook)
1462 ;;; mime-view.el ends here