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 SEMI (Sophisticated 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)
43 (defconst mime-view-version-string
44 `,(concat (car mime-module-version) " MIME-View "
45 (mapconcat #'number-to-string (cddr mime-module-version) ".")
46 " (" (cadr mime-module-version) ")"))
52 (defgroup mime-view nil
56 (defcustom mime-view-find-every-acting-situation t
57 "*Find every available acting-situation if non-nil."
61 (defcustom mime-acting-situation-examples-file "~/.mime-example"
62 "*File name of example about acting-situation demonstrated by user."
67 ;;; @ buffer local variables
73 (defvar mime-raw-message-info nil
74 "Information about structure of message.
75 Please use reference function `mime-entity-SLOT' to get value of SLOT.
77 Following is a list of slots of the structure:
79 buffer buffer includes this entity (buffer).
80 node-id node-id (list of integers)
81 header-start minimum point of header in raw-buffer
82 header-end maximum point of header in raw-buffer
83 body-start minimum point of body in raw-buffer
84 body-end maximum point of body in raw-buffer
85 content-type content-type (content-type)
86 content-disposition content-disposition (content-disposition)
87 encoding Content-Transfer-Encoding (string or nil)
88 children entities included in this entity (list of entity)
90 If an entity includes other entities in its body, such as multipart or
91 message/rfc822, `mime-entity' structures of them are included in
92 `children', so the `mime-entity' structure become a tree.")
93 (make-variable-buffer-local 'mime-raw-message-info)
96 (defvar mime-preview-buffer nil
97 "MIME-preview buffer corresponding with the (raw) buffer.")
98 (make-variable-buffer-local 'mime-preview-buffer)
101 (defvar mime-raw-representation-type nil
102 "Representation-type of mime-raw-buffer.
103 It must be nil, `binary' or `cooked'.
104 If it is nil, `mime-raw-representation-type-alist' is used as default
106 Notice that this variable is usually used as buffer local variable in
109 (make-variable-buffer-local 'mime-raw-representation-type)
111 (defvar mime-raw-representation-type-alist
112 '((mime-show-message-mode . binary)
113 (mime-temp-message-mode . binary)
116 "Alist of major-mode vs. representation-type of mime-raw-buffer.
117 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
118 major-mode or t. t means default. REPRESENTATION-TYPE must be
119 `binary' or `cooked'.
120 This value is overridden by buffer local variable
121 `mime-raw-representation-type' if it is not nil.")
124 ;;; @@ in preview-buffer
127 (defvar mime-mother-buffer nil
128 "Mother buffer corresponding with the (MIME-preview) buffer.
129 If current MIME-preview buffer is generated by other buffer, such as
130 message/partial, it is called `mother-buffer'.")
131 (make-variable-buffer-local 'mime-mother-buffer)
133 (defvar mime-raw-buffer nil
134 "Raw buffer corresponding with the (MIME-preview) buffer.")
135 (make-variable-buffer-local 'mime-raw-buffer)
137 (defvar mime-preview-original-major-mode nil
138 "Major-mode of mime-raw-buffer.")
139 (make-variable-buffer-local 'mime-preview-original-major-mode)
141 (defvar mime-preview-original-window-configuration nil
142 "Window-configuration before mime-view-mode is called.")
143 (make-variable-buffer-local 'mime-preview-original-window-configuration)
146 ;;; @ entity information
149 (defsubst mime-raw-find-entity-from-node-id (entity-node-id
150 &optional message-info)
151 "Return entity from ENTITY-NODE-ID in mime-raw-buffer.
152 If optional argument MESSAGE-INFO is not specified,
153 `mime-raw-message-info' is used."
154 (mime-raw-find-entity-from-number (reverse entity-node-id) message-info))
156 (defun mime-raw-find-entity-from-number (entity-number &optional message-info)
157 "Return entity from ENTITY-NUMBER in mime-raw-buffer.
158 If optional argument MESSAGE-INFO is not specified,
159 `mime-raw-message-info' is used."
161 (setq message-info mime-raw-message-info))
162 (if (eq entity-number t)
164 (let ((sn (car entity-number)))
167 (let ((rc (nth sn (mime-entity-children message-info))))
169 (mime-raw-find-entity-from-number (cdr entity-number) rc)
173 (defun mime-raw-find-entity-from-point (point &optional message-info)
174 "Return entity from POINT in mime-raw-buffer.
175 If optional argument MESSAGE-INFO is not specified,
176 `mime-raw-message-info' is used."
178 (setq message-info mime-raw-message-info))
179 (if (and (<= (mime-entity-point-min message-info) point)
180 (<= point (mime-entity-point-max message-info)))
181 (let ((children (mime-entity-children message-info)))
185 (mime-raw-find-entity-from-point point (car children))))
189 (setq children (cdr children)))
192 (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
193 "Return entity-node-id from POINT in mime-raw-buffer.
194 If optional argument MESSAGE-INFO is not specified,
195 `mime-raw-message-info' is used."
196 (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
198 (defsubst mime-raw-point-to-entity-number (point &optional message-info)
199 "Return entity-number from POINT in mime-raw-buffer.
200 If optional argument MESSAGE-INFO is not specified,
201 `mime-raw-message-info' is used."
202 (reverse (mime-raw-point-to-entity-node-id point message-info)))
204 (defsubst mime-entity-parent (entity &optional message-info)
205 "Return mother entity of ENTITY.
206 If optional argument MESSAGE-INFO is not specified,
207 `mime-raw-message-info' in buffer of ENTITY is used."
208 (mime-raw-find-entity-from-node-id
209 (cdr (mime-entity-node-id entity))
212 (set-buffer (mime-entity-buffer entity))
213 mime-raw-message-info))))
215 (defun mime-raw-flatten-message-info (&optional message-info)
216 "Return list of entity in mime-raw-buffer.
217 If optional argument MESSAGE-INFO is not specified,
218 `mime-raw-message-info' is used."
220 (setq message-info mime-raw-message-info))
221 (let ((dest (list message-info))
222 (rcl (mime-entity-children message-info)))
224 (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
225 (setq rcl (cdr rcl)))
229 ;;; @ presentation of preview
235 ;;; @@@ predicate function
238 (defun mime-view-entity-button-visible-p (entity message-info)
239 "Return non-nil if header of ENTITY is visible.
240 Please redefine this function if you want to change default setting."
241 (let ((media-type (mime-entity-media-type entity))
242 (media-subtype (mime-entity-media-subtype entity)))
243 (or (not (eq media-type 'application))
244 (and (not (eq media-subtype 'x-selection))
245 (or (not (eq media-subtype 'octet-stream))
247 (mime-entity-parent entity message-info)))
248 (or (not (eq (mime-entity-media-type mother-entity)
250 (not (eq (mime-entity-media-subtype mother-entity)
255 ;;; @@@ entity button generator
258 (defun mime-view-insert-entity-button (entity message-info subj)
259 "Insert entity-button of ENTITY."
260 (let ((entity-node-id (mime-entity-node-id entity))
261 (params (mime-entity-parameters entity)))
263 (let ((access-type (assoc "access-type" params))
264 (num (or (cdr (assoc "x-part-number" params))
265 (if (consp entity-node-id)
268 (format "%s" (1+ num))
270 (reverse entity-node-id) ".")
274 (let ((server (assoc "server" params)))
275 (setq access-type (cdr access-type))
277 (format "%s %s ([%s] %s)"
278 num subj access-type (cdr server))
279 (let ((site (cdr (assoc "site" params)))
280 (dir (cdr (assoc "directory" params)))
282 (format "%s %s ([%s] %s:%s)"
283 num subj access-type site dir)
287 (let ((media-type (mime-entity-media-type entity))
288 (media-subtype (mime-entity-media-subtype entity))
289 (charset (cdr (assoc "charset" params)))
290 (encoding (mime-entity-encoding entity)))
294 (format " <%s/%s%s%s>"
295 media-type media-subtype
297 (concat "; " charset)
300 (concat " (" encoding ")")
302 (if (>= (+ (current-column)(length rest))(window-width))
306 (function mime-preview-play-current-entity))
313 ;;; @@@ entity header filter
316 (defvar mime-view-content-header-filter-alist nil)
318 (defun mime-view-default-content-header-filter ()
319 (mime-view-cut-header)
320 (eword-decode-header)
323 ;;; @@@ entity field cutter
326 (defvar mime-view-ignored-field-list
327 '(".*Received" ".*Path" ".*Id" "References"
328 "Replied" "Errors-To"
329 "Lines" "Sender" ".*Host" "Xref"
330 "Content-Type" "Precedence"
332 "All fields that match this list will be hidden in MIME preview buffer.
333 Each elements are regexp of field-name.")
335 (defvar mime-view-ignored-field-regexp
337 (apply (function regexp-or) mime-view-ignored-field-list)
340 (defvar mime-view-visible-field-list '("Dnas.*" "Message-Id")
341 "All fields that match this list will be displayed in MIME preview buffer.
342 Each elements are regexp of field-name.")
344 (defun mime-view-cut-header ()
345 (goto-char (point-min))
346 (while (re-search-forward mime-view-ignored-field-regexp nil t)
347 (let* ((beg (match-beginning 0))
349 (name (buffer-substring beg end))
352 (let ((rest mime-view-visible-field-list))
354 (if (string-match (car rest) name)
357 (setq rest (cdr rest))))
360 (if (re-search-forward "^\\([^ \t]\\|$\\)" nil t)
369 ;;; @@@ predicate function
372 (defun mime-calist::field-match-method-as-default-rule (calist
373 field-type field-value)
374 (let ((s-field (assq field-type calist)))
375 (cond ((null s-field)
376 (cons (cons field-type field-value) calist)
380 (define-calist-field-match-method
381 'header #'mime-calist::field-match-method-as-default-rule)
383 (define-calist-field-match-method
384 'body #'mime-calist::field-match-method-as-default-rule)
387 (defvar mime-preview-condition nil
388 "Condition-tree about how to display entity.")
390 (ctree-set-calist-strictly
391 'mime-preview-condition '((type . application)(subtype . octet-stream)
394 (ctree-set-calist-strictly
395 'mime-preview-condition '((type . application)(subtype . octet-stream)
398 (ctree-set-calist-strictly
399 'mime-preview-condition '((type . application)(subtype . octet-stream)
403 (ctree-set-calist-strictly
404 'mime-preview-condition '((type . application)(subtype . pgp)
407 (ctree-set-calist-strictly
408 'mime-preview-condition '((type . application)(subtype . x-latex)
411 (ctree-set-calist-strictly
412 'mime-preview-condition '((type . application)(subtype . x-selection)
415 (ctree-set-calist-strictly
416 'mime-preview-condition '((type . application)(subtype . x-comment)
419 (ctree-set-calist-strictly
420 'mime-preview-condition '((type . message)(subtype . delivery-status)
423 (ctree-set-calist-strictly
424 'mime-preview-condition
426 (body-presentation-method . mime-preview-text/plain)))
428 (ctree-set-calist-strictly
429 'mime-preview-condition
432 (body-presentation-method . mime-preview-text/plain)))
434 (ctree-set-calist-strictly
435 'mime-preview-condition
436 '((type . text)(subtype . enriched)
438 (body-presentation-method . mime-preview-text/enriched)))
440 (ctree-set-calist-strictly
441 'mime-preview-condition
442 '((type . text)(subtype . richtext)
444 (body-presentation-method . mime-preview-text/richtext)))
446 (ctree-set-calist-strictly
447 'mime-preview-condition
448 '((type . text)(subtype . t)
450 (body-presentation-method . mime-preview-text/plain)))
452 (ctree-set-calist-strictly
453 'mime-preview-condition
454 '((type . multipart)(subtype . alternative)
456 (body-presentation-method . mime-preview-multipart/alternative)))
458 (ctree-set-calist-strictly
459 'mime-preview-condition '((type . message)(subtype . partial)
460 (body-presentation-method
461 . mime-preview-message/partial-button)))
463 (ctree-set-calist-strictly
464 'mime-preview-condition '((type . message)(subtype . rfc822)
465 (body-presentation-method . nil)
466 (childrens-situation (header . visible)
467 (entity-button . invisible))))
469 (ctree-set-calist-strictly
470 'mime-preview-condition '((type . message)(subtype . news)
471 (body-presentation-method . nil)
472 (childrens-situation (header . visible)
473 (entity-button . invisible))))
476 ;;; @@@ entity presentation
479 (autoload 'mime-preview-text/plain "mime-text")
480 (autoload 'mime-preview-text/enriched "mime-text")
481 (autoload 'mime-preview-text/richtext "mime-text")
483 (defvar mime-view-announcement-for-message/partial
484 (if (and (>= emacs-major-version 19) window-system)
486 \[[ This is message/partial style split message. ]]
487 \[[ Please press `v' key in this buffer ]]
488 \[[ or click here by mouse button-2. ]]"
490 \[[ This is message/partial style split message. ]]
491 \[[ Please press `v' key in this buffer. ]]"
494 (defun mime-preview-message/partial-button (&optional entity situation)
496 (goto-char (point-max))
497 (if (not (search-backward "\n\n" nil t))
500 (goto-char (point-max))
501 (narrow-to-region (point-max)(point-max))
502 (insert mime-view-announcement-for-message/partial)
503 (mime-add-button (point-min)(point-max)
504 #'mime-preview-play-current-entity)
507 (defun mime-preview-multipart/mixed (entity situation)
508 (let ((children (mime-entity-children entity))
510 (cdr (assq 'childrens-situation situation))))
512 (mime-view-display-entity (car children)
514 (set-buffer mime-raw-buffer)
515 mime-raw-message-info)
516 mime-raw-buffer (current-buffer)
518 (setq children (cdr children))
521 (defcustom mime-view-type-subtype-score-alist
522 '(((text . enriched) . 3)
523 ((text . richtext) . 2)
526 "Alist MEDIA-TYPE vs corresponding score.
527 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
529 :type '(repeat (cons (choice :tag "Media-Type"
530 (item :tag "Type/Subtype"
531 (cons symbol symbol))
532 (item :tag "Type" symbol)
533 (item :tag "Default" t))
536 (defun mime-preview-multipart/alternative (entity situation)
537 (let* ((children (mime-entity-children entity))
539 (cdr (assq 'childrens-situation situation)))
547 (or (ctree-match-calist
548 mime-preview-condition
550 (or (mime-entity-content-type child)
551 (make-mime-content-type 'text 'plain))
552 (list* (cons 'encoding
553 (mime-entity-encoding child))
554 (cons 'major-mode major-mode)
557 (if (cdr (assq 'body-presentation-method situation))
562 (cdr (assq 'type situation))
563 (cdr (assq 'subtype situation)))
564 mime-view-type-subtype-score-alist)
566 (cdr (assq 'type situation))
567 mime-view-type-subtype-score-alist)
570 mime-view-type-subtype-score-alist)
572 (if (> score max-score)
582 (let ((situation (car situations)))
583 (mime-view-display-entity (car children)
585 (set-buffer mime-raw-buffer)
586 mime-raw-message-info)
587 mime-raw-buffer (current-buffer)
591 (del-alist 'body-presentation-method
592 (copy-alist situation))))
594 (setq children (cdr children)
595 situations (cdr situations)
600 ;;; @ acting-condition
603 (defvar mime-acting-condition nil
604 "Condition-tree about how to process entity.")
606 (if (file-readable-p mailcap-file)
607 (let ((entries (mailcap-parse-file)))
609 (let ((entry (car entries))
612 (let* ((field (car entry))
613 (field-type (car field)))
614 (cond ((eq field-type 'view) (setq view field))
615 ((eq field-type 'print) (setq print field))
616 ((memq field-type '(compose composetyped edit)))
617 (t (setq shared (cons field shared))))
619 (setq entry (cdr entry))
621 (setq shared (nreverse shared))
622 (ctree-set-calist-with-default
623 'mime-acting-condition
624 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
626 (ctree-set-calist-with-default
627 'mime-acting-condition
629 (list '(mode . "print")(cons 'method (cdr view))))
632 (setq entries (cdr entries))
635 ;; (ctree-set-calist-strictly
636 ;; 'mime-acting-condition
637 ;; '((type . t)(subtype . t)(mode . "extract")
638 ;; (method . mime-method-to-save)))
639 (ctree-set-calist-with-default
640 'mime-acting-condition
642 (method . mime-method-to-save)))
644 ;; (ctree-set-calist-strictly
645 ;; 'mime-acting-condition
646 ;; '((type . text)(subtype . plain)(mode . "play")
647 ;; (method "tm-plain" nil 'file "" 'encoding 'mode 'name)
649 ;; (ctree-set-calist-strictly
650 ;; 'mime-acting-condition
651 ;; '((type . text)(subtype . plain)(mode . "print")
652 ;; (method "tm-plain" nil 'file "" 'encoding 'mode 'name)
654 ;; (ctree-set-calist-strictly
655 ;; 'mime-acting-condition
656 ;; '((type . text)(subtype . html)(mode . "play")
657 ;; (method "tm-html" nil 'file "" 'encoding 'mode 'name)
659 (ctree-set-calist-strictly
660 'mime-acting-condition
661 '((type . text)(subtype . x-rot13-47)(mode . "play")
662 (method . mime-method-to-display-caesar)
664 (ctree-set-calist-strictly
665 'mime-acting-condition
666 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
667 (method . mime-method-to-display-caesar)
670 ;; (ctree-set-calist-strictly
671 ;; 'mime-acting-condition
672 ;; '((type . audio)(subtype . basic)(mode . "play")
673 ;; (method "tm-au" nil 'file "" 'encoding 'mode 'name)
676 ;; (ctree-set-calist-strictly
677 ;; 'mime-acting-condition
678 ;; '((type . image)(mode . "play")
679 ;; (method "tm-image" nil 'file "" 'encoding 'mode 'name)
681 ;; (ctree-set-calist-strictly
682 ;; 'mime-acting-condition
683 ;; '((type . image)(mode . "print")
684 ;; (method "tm-image" nil 'file "" 'encoding 'mode 'name)
687 ;; (ctree-set-calist-strictly
688 ;; 'mime-acting-condition
689 ;; '((type . video)(subtype . mpeg)(mode . "play")
690 ;; (method "tm-mpeg" nil 'file "" 'encoding 'mode 'name)
693 ;; (ctree-set-calist-strictly
694 ;; 'mime-acting-condition
695 ;; '((type . application)(subtype . postscript)(mode . "play")
696 ;; (method "tm-ps" nil 'file "" 'encoding 'mode 'name)
698 ;; (ctree-set-calist-strictly
699 ;; 'mime-acting-condition
700 ;; '((type . application)(subtype . postscript)(mode . "print")
701 ;; (method "tm-ps" nil 'file "" 'encoding 'mode 'name)
704 (ctree-set-calist-strictly
705 'mime-acting-condition
706 '((type . message)(subtype . rfc822)(mode . "play")
707 (method . mime-method-to-display-message/rfc822)
709 (ctree-set-calist-strictly
710 'mime-acting-condition
711 '((type . message)(subtype . partial)(mode . "play")
712 (method . mime-method-to-store-message/partial)
715 (ctree-set-calist-strictly
716 'mime-acting-condition
717 '((type . message)(subtype . external-body)
718 ("access-type" . "anon-ftp")
719 (method . mime-method-to-display-message/external-ftp)
722 (ctree-set-calist-strictly
723 'mime-acting-condition
724 '((type . application)(subtype . octet-stream)
725 (method . mime-method-to-save)
729 ;;; @ quitting method
732 (defvar mime-preview-quitting-method-alist
733 '((mime-show-message-mode
734 . mime-preview-quitting-method-for-mime-show-message-mode))
735 "Alist of major-mode vs. quitting-method of mime-view.")
737 (defvar mime-view-over-to-previous-method-alist nil)
738 (defvar mime-view-over-to-next-method-alist nil)
740 (defvar mime-view-show-summary-method nil
741 "Alist of major-mode vs. show-summary-method.")
744 ;;; @ following method
747 (defvar mime-view-following-method-alist nil
748 "Alist of major-mode vs. following-method of mime-view.")
750 (defvar mime-view-following-required-fields-list
757 ;; hack from Gnus 5.0.4.
759 (defvar mime-view-x-face-to-pbm-command
760 "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm")
762 (defvar mime-view-x-face-command
763 (concat mime-view-x-face-to-pbm-command
765 "String to be executed to display an X-Face field.
766 The command will be executed in a sub-shell asynchronously.
767 The compressed face will be piped to this command.")
769 (defun mime-view-x-face-function ()
770 "Function to display X-Face field. You can redefine to customize."
771 ;; 1995/10/12 (c.f. tm-eng:130)
772 ;; fixed by Eric Ding <ericding@San-Jose.ate.slb.com>
774 (narrow-to-region (point-min) (re-search-forward "^$" nil t))
776 (goto-char (point-min))
777 (if (re-search-forward "^X-Face:[ \t]*" nil t)
778 (let ((beg (match-end 0))
779 (end (std11-field-end))
781 (call-process-region beg end "sh" nil 0 nil
782 "-c" mime-view-x-face-command)
789 (defvar mime-view-uuencode-encoding-name-list '("x-uue" "x-uuencode"))
795 (defvar mime-view-redisplay nil)
797 (defun mime-view-setup-buffers (&optional ctl encoding ibuf obuf)
803 (or mime-view-redisplay
804 (setq mime-raw-message-info (mime-parse-message ctl encoding))
806 (let ((message-info mime-raw-message-info)
807 (the-buf (current-buffer))
810 (setq obuf (concat "*Preview-" (buffer-name the-buf) "*")))
811 (set-buffer (get-buffer-create obuf))
812 (let ((inhibit-read-only t))
813 ;;(setq buffer-read-only nil)
816 (setq mime-raw-buffer the-buf)
817 (setq mime-preview-original-major-mode mode)
818 (setq major-mode 'mime-view-mode)
819 (setq mode-name "MIME-View")
820 (mime-view-display-entity message-info message-info
822 '((entity-button . invisible)
825 (set-buffer-modified-p nil)
827 (setq buffer-read-only t)
830 (setq mime-preview-buffer obuf)
833 (defun mime-view-display-entity (entity message-info ibuf obuf
836 (let* ((start (mime-entity-point-min entity))
837 (end (mime-entity-point-max entity))
838 (content-type (mime-entity-content-type entity))
839 (encoding (mime-entity-encoding entity))
840 end-of-header e nb ne subj)
843 (setq end-of-header (if (re-search-forward "^$" nil t)
846 (if (> end-of-header end)
847 (setq end-of-header end)
850 (narrow-to-region start end)
851 (setq subj (eword-decode-string (mime-raw-get-subject entity)))
855 (or (ctree-match-calist mime-preview-condition
858 (make-mime-content-type
860 (list* (cons 'encoding encoding)
861 (cons 'major-mode major-mode)
864 (let ((button-is-invisible
865 (eq (cdr (assq 'entity-button situation)) 'invisible))
867 (eq (cdr (assq 'header situation)) 'visible))
868 (body-presentation-method
869 (cdr (assq 'body-presentation-method situation)))
870 (children (mime-entity-children entity)))
873 (narrow-to-region nb nb)
874 (or button-is-invisible
875 (if (mime-view-entity-button-visible-p entity message-info)
876 (mime-view-insert-entity-button entity message-info subj)
878 (if header-is-visible
880 (narrow-to-region (point)(point))
881 (insert-buffer-substring mime-raw-buffer start end-of-header)
882 (let ((f (cdr (assq mime-preview-original-major-mode
883 mime-view-content-header-filter-alist))))
886 (mime-view-default-content-header-filter)
888 (run-hooks 'mime-view-content-header-filter-hook)
890 (cond ((eq body-presentation-method 'with-filter)
891 (let ((body-filter (cdr (assq 'body-filter situation))))
893 (narrow-to-region (point-max)(point-max))
894 (insert-buffer-substring mime-raw-buffer end-of-header end)
895 (funcall body-filter situation)
898 ((functionp body-presentation-method)
899 (funcall body-presentation-method entity situation)
902 (when button-is-invisible
903 (goto-char (point-max))
904 (mime-view-insert-entity-button entity message-info subj)
906 (or header-is-visible
908 (goto-char (point-max))
912 (setq ne (point-max))
914 (put-text-property nb ne 'mime-view-raw-buffer ibuf)
915 (put-text-property nb ne 'mime-view-entity entity)
918 (if (functionp body-presentation-method)
919 (funcall body-presentation-method entity situation)
920 (mime-preview-multipart/mixed entity situation)
924 (defun mime-raw-get-uu-filename ()
926 (if (re-search-forward "^begin [0-9]+ " nil t)
927 (if (looking-at ".+$")
928 (buffer-substring (match-beginning 0)(match-end 0))
931 (defun mime-raw-get-subject (entity)
932 (or (std11-find-field-body '("Content-Description" "Subject"))
933 (let ((ret (mime-entity-content-disposition entity)))
935 (setq ret (mime-content-disposition-filename ret))
936 (std11-strip-quoted-string ret)
938 (let ((ret (mime-entity-content-type entity)))
942 (let ((param (mime-content-type-parameters ret)))
943 (or (assoc "name" param)
944 (assoc "x-name" param))
946 (std11-strip-quoted-string ret)
948 (if (member (mime-entity-encoding entity)
949 mime-view-uuencode-encoding-name-list)
950 (mime-raw-get-uu-filename))
954 ;;; @ MIME viewer mode
957 (defconst mime-view-menu-title "MIME-View")
958 (defconst mime-view-menu-list
959 '((up "Move to upper entity" mime-preview-move-to-upper)
960 (previous "Move to previous entity" mime-preview-move-to-previous)
961 (next "Move to next entity" mime-preview-move-to-next)
962 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
963 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
964 (play "Play current entity" mime-preview-play-current-entity)
965 (extract "Extract current entity" mime-preview-extract-current-entity)
966 (print "Print current entity" mime-preview-print-current-entity)
967 (x-face "Show X Face" mime-preview-display-x-face)
969 "Menu for MIME Viewer")
971 (cond (running-xemacs
972 (defvar mime-view-xemacs-popup-menu
973 (cons mime-view-menu-title
976 (vector (nth 1 item)(nth 2 item) t)
978 mime-view-menu-list)))
979 (defun mime-view-xemacs-popup-menu (event)
980 "Popup the menu in the MIME Viewer buffer"
982 (select-window (event-window event))
983 (set-buffer (event-buffer event))
984 (popup-menu 'mime-view-xemacs-popup-menu))
985 (defvar mouse-button-2 'button2)
988 (defvar mouse-button-2 [mouse-2])
991 (defun mime-view-define-keymap (&optional default)
992 (let ((mime-view-mode-map (if (keymapp default)
993 (copy-keymap default)
996 (define-key mime-view-mode-map
997 "u" (function mime-preview-move-to-upper))
998 (define-key mime-view-mode-map
999 "p" (function mime-preview-move-to-previous))
1000 (define-key mime-view-mode-map
1001 "n" (function mime-preview-move-to-next))
1002 (define-key mime-view-mode-map
1003 "\e\t" (function mime-preview-move-to-previous))
1004 (define-key mime-view-mode-map
1005 "\t" (function mime-preview-move-to-next))
1006 (define-key mime-view-mode-map
1007 " " (function mime-preview-scroll-up-entity))
1008 (define-key mime-view-mode-map
1009 "\M- " (function mime-preview-scroll-down-entity))
1010 (define-key mime-view-mode-map
1011 "\177" (function mime-preview-scroll-down-entity))
1012 (define-key mime-view-mode-map
1013 "\C-m" (function mime-preview-next-line-entity))
1014 (define-key mime-view-mode-map
1015 "\C-\M-m" (function mime-preview-previous-line-entity))
1016 (define-key mime-view-mode-map
1017 "v" (function mime-preview-play-current-entity))
1018 (define-key mime-view-mode-map
1019 "e" (function mime-preview-extract-current-entity))
1020 (define-key mime-view-mode-map
1021 "\C-c\C-p" (function mime-preview-print-current-entity))
1022 (define-key mime-view-mode-map
1023 "a" (function mime-preview-follow-current-entity))
1024 (define-key mime-view-mode-map
1025 "q" (function mime-preview-quit))
1026 (define-key mime-view-mode-map
1027 "h" (function mime-preview-show-summary))
1028 (define-key mime-view-mode-map
1029 "\C-c\C-x" (function mime-preview-kill-buffer))
1030 ;; (define-key mime-view-mode-map
1031 ;; "<" (function beginning-of-buffer))
1032 ;; (define-key mime-view-mode-map
1033 ;; ">" (function end-of-buffer))
1034 (define-key mime-view-mode-map
1035 "?" (function describe-mode))
1036 (define-key mime-view-mode-map
1037 [tab] (function mime-preview-move-to-next))
1038 (define-key mime-view-mode-map
1039 [delete] (function mime-preview-scroll-down-entity))
1040 (define-key mime-view-mode-map
1041 [backspace] (function mime-preview-scroll-down-entity))
1042 (if (functionp default)
1043 (cond (running-xemacs
1044 (set-keymap-default-binding mime-view-mode-map default)
1047 (setq mime-view-mode-map
1048 (append mime-view-mode-map (list (cons t default))))
1051 (define-key mime-view-mode-map
1052 mouse-button-2 (function mime-button-dispatcher))
1054 (cond (running-xemacs
1055 (define-key mime-view-mode-map
1056 mouse-button-3 (function mime-view-xemacs-popup-menu))
1058 ((>= emacs-major-version 19)
1059 (define-key mime-view-mode-map [menu-bar mime-view]
1060 (cons mime-view-menu-title
1061 (make-sparse-keymap mime-view-menu-title)))
1064 (define-key mime-view-mode-map
1065 (vector 'menu-bar 'mime-view (car item))
1066 (cons (nth 1 item)(nth 2 item))
1069 (reverse mime-view-menu-list)
1072 (use-local-map mime-view-mode-map)
1073 (run-hooks 'mime-view-define-keymap-hook)
1076 (defsubst mime-maybe-hide-echo-buffer ()
1077 "Clear mime-echo buffer and delete window for it."
1078 (let ((buf (get-buffer mime-echo-buffer-name)))
1083 (let ((win (get-buffer-window buf)))
1090 (defun mime-view-mode (&optional mother ctl encoding ibuf obuf
1091 default-keymap-or-function)
1092 "Major mode for viewing MIME message.
1094 Here is a list of the standard keys for mime-view-mode.
1099 u Move to upper content
1100 p or M-TAB Move to previous content
1101 n or TAB Move to next content
1102 SPC Scroll up or move to next content
1103 M-SPC or DEL Scroll down or move to previous content
1104 RET Move to next line
1105 M-RET Move to previous line
1106 v Decode current content as `play mode'
1107 e Decode current content as `extract mode'
1108 C-c C-p Decode current content as `print mode'
1109 a Followup to current content.
1112 button-2 Move to point under the mouse cursor
1113 and decode current content as `play mode'
1116 (mime-maybe-hide-echo-buffer)
1117 (let ((ret (mime-view-setup-buffers ctl encoding ibuf obuf))
1118 (win-conf (current-window-configuration))
1121 (switch-to-buffer ret)
1122 (setq mime-preview-original-window-configuration win-conf)
1125 (setq mime-mother-buffer mother)
1127 (mime-view-define-keymap default-keymap-or-function)
1129 (next-single-property-change (point-min) 'mime-view-entity)))
1132 (goto-char (point-min))
1133 (search-forward "\n\n" nil t)
1135 (run-hooks 'mime-view-mode-hook)
1142 (autoload 'mime-preview-play-current-entity "mime-play"
1143 "Play current entity." t)
1145 (defun mime-preview-extract-current-entity ()
1146 "Extract current entity into file (maybe).
1147 It decodes current entity to call internal or external method as
1148 \"extract\" mode. The method is selected from variable
1149 `mime-acting-condition'."
1151 (mime-preview-play-current-entity "extract")
1154 (defun mime-preview-print-current-entity ()
1155 "Print current entity (maybe).
1156 It decodes current entity to call internal or external method as
1157 \"print\" mode. The method is selected from variable
1158 `mime-acting-condition'."
1160 (mime-preview-play-current-entity "print")
1167 (defun mime-preview-original-major-mode ()
1168 "Return major-mode of original buffer.
1169 If a current buffer has mime-mother-buffer, return original major-mode
1170 of the mother-buffer."
1171 (if mime-mother-buffer
1173 (set-buffer mime-mother-buffer)
1174 (mime-preview-original-major-mode)
1176 mime-preview-original-major-mode))
1178 (defun mime-preview-follow-current-entity ()
1179 "Write follow message to current entity.
1180 It calls following-method selected from variable
1181 `mime-view-following-method-alist'."
1184 (while (null (setq entity
1185 (get-text-property (point) 'mime-view-entity)))
1189 (previous-single-property-change (point) 'mime-view-entity))
1191 (entity-node-id (mime-entity-node-id entity))
1192 (len (length entity-node-id))
1196 (if (eq (next-single-property-change (point-min)
1202 ((eq (next-single-property-change p-beg 'mime-view-entity)
1204 (setq p-beg (point))
1206 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1208 (setq p-end (point-max))
1210 ((null entity-node-id)
1211 (setq p-end (point-max))
1219 (next-single-property-change
1220 (point) 'mime-view-entity))
1222 (let ((rc (mime-entity-node-id
1223 (get-text-property (point)
1224 'mime-view-entity))))
1225 (or (equal entity-node-id
1226 (nthcdr (- (length rc) len) rc))
1231 (setq p-end (point-max))
1234 (let* ((mode (mime-preview-original-major-mode))
1236 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1238 (the-buf (current-buffer))
1239 (a-buf mime-raw-buffer)
1242 (set-buffer (setq new-buf (get-buffer-create new-name)))
1244 (insert-buffer-substring the-buf p-beg p-end)
1245 (goto-char (point-min))
1246 (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1254 (mime-raw-find-entity-from-node-id entity-node-id))
1257 (mime-entity-point-min ci)
1258 (mime-entity-point-max ci)
1260 (std11-header-string-except
1262 (apply (function regexp-or) fields)
1265 (eq (mime-entity-media-type ci) 'message)
1266 (eq (mime-entity-media-subtype ci) 'rfc822))
1272 (setq fields (std11-collect-field-names)
1273 entity-node-id (cdr entity-node-id))
1276 (let ((rest mime-view-following-required-fields-list))
1278 (let ((field-name (car rest)))
1279 (or (std11-field-body field-name)
1285 (set-buffer the-buf)
1286 (set-buffer mime-mother-buffer)
1287 (set-buffer mime-raw-buffer)
1288 (std11-field-body field-name)
1292 (setq rest (cdr rest))
1294 (eword-decode-header)
1296 (let ((f (cdr (assq mode mime-view-following-method-alist))))
1301 "Sorry, following method for %s is not implemented yet."
1310 (defun mime-preview-display-x-face ()
1312 (save-window-excursion
1313 (set-buffer mime-raw-buffer)
1314 (mime-view-x-face-function)
1321 (defun mime-preview-move-to-upper ()
1322 "Move to upper entity.
1323 If there is no upper entity, call function `mime-preview-quit'."
1326 (while (null (setq cinfo
1327 (get-text-property (point) 'mime-view-entity)))
1330 (let ((r (mime-raw-find-entity-from-node-id
1331 (cdr (mime-entity-node-id cinfo))
1332 (get-text-property 1 'mime-view-entity)))
1335 (while (setq point (previous-single-property-change
1336 (point) 'mime-view-entity))
1338 (if (eq r (get-text-property (point) 'mime-view-entity))
1345 (defun mime-preview-move-to-previous ()
1346 "Move to previous entity.
1347 If there is no previous entity, it calls function registered in
1348 variable `mime-view-over-to-previous-method-alist'."
1350 (while (null (get-text-property (point) 'mime-view-entity))
1353 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1355 (if (get-text-property (1- point) 'mime-view-entity)
1357 (goto-char (1- point))
1358 (mime-preview-move-to-previous)
1360 (let ((f (assq mime-preview-original-major-mode
1361 mime-view-over-to-previous-method-alist)))
1367 (defun mime-preview-move-to-next ()
1368 "Move to next entity.
1369 If there is no previous entity, it calls function registered in
1370 variable `mime-view-over-to-next-method-alist'."
1372 (while (null (get-text-property (point) 'mime-view-entity))
1375 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1379 (if (null (get-text-property point 'mime-view-entity))
1380 (mime-preview-move-to-next)
1382 (let ((f (assq mime-preview-original-major-mode
1383 mime-view-over-to-next-method-alist)))
1389 (defun mime-preview-scroll-up-entity (&optional h)
1390 "Scroll up current entity.
1391 If reached to (point-max), it calls function registered in variable
1392 `mime-view-over-to-next-method-alist'."
1395 (setq h (1- (window-height)))
1397 (if (= (point) (point-max))
1398 (let ((f (assq mime-preview-original-major-mode
1399 mime-view-over-to-next-method-alist)))
1404 (or (next-single-property-change (point) 'mime-view-entity)
1407 (if (> (point) point)
1412 (defun mime-preview-scroll-down-entity (&optional h)
1413 "Scroll down current entity.
1414 If reached to (point-min), it calls function registered in variable
1415 `mime-view-over-to-previous-method-alist'."
1418 (setq h (1- (window-height)))
1420 (if (= (point) (point-min))
1421 (let ((f (assq mime-preview-original-major-mode
1422 mime-view-over-to-previous-method-alist)))
1431 (previous-single-property-change (point)
1437 (setq point (point-min))
1439 (forward-line (- h))
1440 (if (< (point) point)
1444 (defun mime-preview-next-line-entity ()
1446 (mime-preview-scroll-up-entity 1)
1449 (defun mime-preview-previous-line-entity ()
1451 (mime-preview-scroll-down-entity 1)
1458 (defun mime-preview-quit ()
1459 "Quit from MIME-preview buffer.
1460 It calls function registered in variable
1461 `mime-preview-quitting-method-alist'."
1463 (let ((r (assq mime-preview-original-major-mode
1464 mime-preview-quitting-method-alist)))
1469 (defun mime-preview-show-summary ()
1471 It calls function registered in variable
1472 `mime-view-show-summary-method'."
1474 (let ((r (assq mime-preview-original-major-mode
1475 mime-view-show-summary-method)))
1480 (defun mime-preview-kill-buffer ()
1482 (kill-buffer (current-buffer))
1489 (provide 'mime-view)
1491 (run-hooks 'mime-view-load-hook)
1493 ;;; mime-view.el ends here