1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 (Sample of Elastic 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.
41 (defconst mime-view-version
42 (concat (mime-product-name mime-user-interface-product) " MIME-View "
43 (mapconcat #'number-to-string
44 (mime-product-version mime-user-interface-product) ".")
45 " (" (mime-product-code-name mime-user-interface-product) ")"))
51 (defgroup mime-view nil
55 (defvar mime-view-find-every-situations t
56 "*Find every available situations if non-nil.")
58 (defcustom mime-situation-examples-file "~/.mime-example"
59 "*File name of situation-examples demonstrated by user."
63 (defcustom mime-preview-move-scroll nil
64 "*Decides whether to scroll when moving to next entity.
65 When t, scroll the buffer. Non-nil but not t means scroll when
66 the next entity is within next-screen-context-lines from top or
67 buttom. Nil means don't scroll at all."
69 :type '(choice (const :tag "Off" nil)
71 (sexp :tag "Situation" 1)))
73 ;;; @ in raw-buffer (representation space)
76 (defvar mime-preview-buffer nil
77 "MIME-preview buffer corresponding with the (raw) buffer.")
78 (make-variable-buffer-local 'mime-preview-buffer)
81 (defvar mime-raw-representation-type-alist
82 '((mime-show-message-mode . binary)
83 (mime-temp-message-mode . binary)
86 "Alist of major-mode vs. representation-type of mime-raw-buffer.
87 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
88 major-mode or t. t means default. REPRESENTATION-TYPE must be
89 `binary' or `cooked'.")
92 ;; (defun mime-raw-find-entity-from-point (point &optional message-info)
93 ;; "Return entity from POINT in mime-raw-buffer.
94 ;; If optional argument MESSAGE-INFO is not specified,
95 ;; `mime-message-structure' is used."
97 ;; (setq message-info mime-message-structure))
98 ;; (if (and (<= (mime-entity-point-min message-info) point)
99 ;; (<= point (mime-entity-point-max message-info)))
100 ;; (let ((children (mime-entity-children message-info)))
104 ;; (mime-raw-find-entity-from-point point (car children))))
108 ;; (setq children (cdr children)))
110 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
113 ;;; @ in preview-buffer (presentation space)
116 (defvar mime-mother-buffer nil
117 "Mother buffer corresponding with the (MIME-preview) buffer.
118 If current MIME-preview buffer is generated by other buffer, such as
119 message/partial, it is called `mother-buffer'.")
120 (make-variable-buffer-local 'mime-mother-buffer)
122 ;; (defvar mime-raw-buffer nil
123 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
124 ;; (make-variable-buffer-local 'mime-raw-buffer)
126 (defvar mime-preview-original-window-configuration nil
127 "Window-configuration before mime-view-mode is called.")
128 (make-variable-buffer-local 'mime-preview-original-window-configuration)
130 (defun mime-preview-original-major-mode (&optional recursive point)
131 "Return major-mode of original buffer.
132 If optional argument RECURSIVE is non-nil and current buffer has
133 mime-mother-buffer, it returns original major-mode of the
135 (if (and recursive mime-mother-buffer)
137 (set-buffer mime-mother-buffer)
138 (mime-preview-original-major-mode recursive)
140 (cdr (assq 'major-mode
141 (get-text-property (or point
142 (if (> (point) (buffer-size))
143 (max (1- (point-max)) (point-min))
145 'mime-view-situation)))))
148 ;;; @ entity information
151 (defun mime-entity-situation (entity &optional situation)
152 "Return situation of ENTITY."
153 (let (rest param name)
155 (unless (assq 'type situation)
156 (setq rest (or (mime-entity-content-type entity)
157 (make-mime-content-type 'text 'plain))
158 situation (cons (car rest) situation)
161 (unless (assq 'subtype situation)
163 (setq rest (or (cdr (mime-entity-content-type entity))
164 '((subtype . plain)))))
165 (setq situation (cons (car rest) situation)
169 (setq param (car rest))
170 (or (assoc (car param) situation)
171 (setq situation (cons param situation)))
172 (setq rest (cdr rest)))
174 ;; Content-Disposition
176 (unless (assq 'disposition-type situation)
177 (setq rest (mime-entity-content-disposition entity))
179 (setq situation (cons (cons 'disposition-type
180 (mime-content-disposition-type rest))
182 rest (mime-content-disposition-parameters rest))
185 (setq param (car rest)
187 (if (cond ((string= name "filename")
188 (if (assq 'filename situation)
190 (setq name 'filename)))
191 ((string= name "creation-date")
192 (if (assq 'creation-date situation)
194 (setq name 'creation-date)))
195 ((string= name "modification-date")
196 (if (assq 'modification-date situation)
198 (setq name 'modification-date)))
199 ((string= name "read-date")
200 (if (assq 'read-date situation)
202 (setq name 'read-date)))
203 ((string= name "size")
204 (if (assq 'size situation)
207 (t (setq name (cons 'disposition name))
208 (if (assoc name situation)
212 (cons (cons name (cdr param))
214 (setq rest (cdr rest)))
216 ;; Content-Transfer-Encoding
217 (or (assq 'encoding situation)
219 (cons (cons 'encoding (or (mime-entity-encoding entity)
225 (defsubst mime-delq-null-situation (situations field
226 &optional ignored-value)
229 (let* ((situation (car situations))
230 (cell (assq field situation)))
232 (or (eq (cdr cell) ignored-value)
233 (setq dest (cons situation dest))
235 (setq situations (cdr situations)))
238 (defun mime-compare-situation-with-example (situation example)
239 (let ((example (copy-alist example))
242 (let* ((cell (car situation))
244 (ecell (assoc key example)))
246 (if (equal cell ecell)
247 (setq match (1+ match))
248 (setq example (delq ecell example))
251 (setq situation (cdr situation))
256 (defun mime-sort-situation (situation)
266 (disposition-type . 6)
270 (let ((ret (assq a-t order)))
272 (setq a-order (cdr ret))
278 (let ((ret (assq b-t order)))
280 (setq b-order (cdr ret))
285 (if (= a-order b-order)
286 (string< (format "%s" a-t)(format "%s" b-t))
291 (defun mime-unify-situations (entity-situation condition situation-examples
292 &optional ignored-method)
294 (in-calist-package 'mime-view)
296 (mime-delq-null-situation
297 (ctree-find-calist condition entity-situation
298 mime-view-find-every-situations)
299 'method ignored-method))
300 (or (assq 'ignore-examples entity-situation)
308 (let ((situation (car rest))
309 (examples situation-examples))
312 (mime-compare-situation-with-example
313 situation (caar examples)))
314 (ret-score (car ret)))
315 (cond ((> ret-score max-score)
316 (setq max-score ret-score
317 max-escore (cdar examples)
318 max-examples (list (cdr ret))
319 max-situations (list situation))
321 ((= ret-score max-score)
322 (cond ((> (cdar examples) max-escore)
323 (setq max-escore (cdar examples)
324 max-examples (list (cdr ret))
325 max-situations (list situation))
327 ((= (cdar examples) max-escore)
329 (cons (cdr ret) max-examples))
330 (or (member situation max-situations)
332 (cons situation max-situations)))
334 (setq examples (cdr examples))))
335 (setq rest (cdr rest)))
337 (setq ret max-situations)
339 (let* ((example (car max-examples))
341 (assoc example situation-examples)))
343 (setcdr cell (1+ (cdr cell)))
344 (setq situation-examples
345 (cons (cons example 0)
348 (setq max-examples (cdr max-examples))
350 (cons ret situation-examples)
351 ;; ret: list of situations
352 ;; situation-examples: new examples (notoce that contents of
353 ;; argument `situation-examples' has bees modified)
356 (defun mime-view-entity-title (entity)
357 (or (mime-entity-read-field entity 'Content-Description)
358 (mime-entity-read-field entity 'Subject)
359 (mime-entity-filename entity)
362 (defvar mime-preview-situation-example-list nil)
363 (defvar mime-acting-situation-example-list nil)
365 (defvar mime-preview-situation-example-list-max-size 16)
366 (defvar mime-acting-situation-example-list-max-size 16)
368 (defun mime-save-situation-examples ()
369 (if (or mime-preview-situation-example-list
370 mime-acting-situation-example-list)
371 (let* ((file mime-situation-examples-file)
372 (buffer (get-buffer-create " *mime-example*")))
376 (setq buffer-file-name file)
378 (insert ";;; " (file-name-nondirectory file) "\n")
379 (insert "\n;; This file is generated automatically by "
380 mime-view-version "\n\n")
381 (insert ";;; Code:\n\n")
382 (if mime-preview-situation-example-list
383 (pp `(setq mime-preview-situation-example-list
384 ',mime-preview-situation-example-list)
386 (if mime-acting-situation-example-list
387 (pp `(setq mime-acting-situation-example-list
388 ',mime-acting-situation-example-list)
391 (file-name-nondirectory file)
394 (kill-buffer buffer)))))
396 (add-hook 'kill-emacs-hook 'mime-save-situation-examples)
398 (defun mime-reduce-acting-situation-examples ()
399 (let ((len (length mime-acting-situation-example-list))
407 ir mime-acting-situation-example-list)
411 jr mime-acting-situation-example-list)
415 (setq ret (mime-compare-situation-with-example (car ic)(car jc))
417 det-ret (+ (length (car ic))(length (car jc)))
418 det-org (length (cdr ret))
419 freq (+ (cdr ic)(cdr jc)))
420 (cond ((< max-sim sim)
427 dest (cons (cdr ret) freq))
430 (cond ((> min-det-ret det-ret)
431 (setq min-det-ret det-ret
436 dest (cons (cdr ret) freq))
438 ((= min-det-ret det-ret)
439 (cond ((> min-det-org det-org)
440 (setq min-det-org det-org
444 dest (cons (cdr ret) freq))
446 ((= min-det-org det-org)
447 (cond ((> min-freq freq)
451 dest (cons (cdr ret) freq))
465 (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
466 (setcdr jr (cddr jr))
468 (setq mime-acting-situation-example-list
469 (cdr mime-acting-situation-example-list))
470 (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
471 (setcdr ir (cddr ir))
473 (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
474 (setcdr ir (+ (cdr ir)(cdr dest)))
475 (setq mime-acting-situation-example-list
476 (cons dest mime-acting-situation-example-list))
480 ;;; @ presentation of preview
486 ;;; @@@ predicate function
489 (defun mime-view-entity-button-visible-p (entity)
490 "Return non-nil if header of ENTITY is visible.
491 Please redefine this function if you want to change default setting."
492 (let ((media-type (mime-entity-media-type entity))
493 (media-subtype (mime-entity-media-subtype entity)))
494 (or (not (eq media-type 'application))
495 (and (not (eq media-subtype 'x-selection))
496 (or (not (eq media-subtype 'octet-stream))
497 (let ((mother-entity (mime-entity-parent entity)))
498 (or (not (eq (mime-entity-media-type mother-entity)
500 (not (eq (mime-entity-media-subtype mother-entity)
505 ;;; @@@ entity button generator
508 (defun mime-view-insert-entity-button (entity)
509 "Insert entity-button of ENTITY."
510 (let ((entity-node-id (mime-entity-node-id entity))
511 (params (mime-entity-parameters entity))
512 (subject (mime-view-entity-title entity)))
514 (let ((access-type (assoc "access-type" params))
515 (num (or (cdr (assoc "x-part-number" params))
516 (if (consp entity-node-id)
519 (format "%s" (1+ num))
521 (reverse entity-node-id) ".")
525 (let ((server (assoc "server" params)))
526 (setq access-type (cdr access-type))
528 (format "%s %s ([%s] %s)"
529 num subject access-type (cdr server))
530 (let ((site (cdr (assoc "site" params)))
531 (dir (cdr (assoc "directory" params)))
532 (url (cdr (assoc "url" params)))
535 (format "%s %s ([%s] %s)"
536 num subject access-type url)
537 (format "%s %s ([%s] %s:%s)"
538 num subject access-type site dir))
542 (let ((media-type (mime-entity-media-type entity))
543 (media-subtype (mime-entity-media-subtype entity))
544 (charset (cdr (assoc "charset" params)))
545 (encoding (mime-entity-encoding entity)))
549 (format " <%s/%s%s%s>"
550 media-type media-subtype
552 (concat "; " charset)
555 (concat " (" encoding ")")
557 (if (>= (+ (current-column)(length rest))(window-width))
561 (function mime-preview-play-current-entity))
568 (defvar mime-header-presentation-method-alist nil
569 "Alist of major mode vs. corresponding header-presentation-method functions.
570 Each element looks like (SYMBOL . FUNCTION).
571 SYMBOL must be major mode in raw-buffer or t. t means default.
572 Interface of FUNCTION must be (ENTITY SITUATION).")
574 (defvar mime-view-ignored-field-list
575 '(".*Received:" ".*Path:" ".*Id:" "^References:"
576 "^Replied:" "^Errors-To:"
577 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
578 "^Content-Type:" "^Precedence:"
579 "^Status:" "^X-VM-.*:")
580 "All fields that match this list will be hidden in MIME preview buffer.
581 Each elements are regexp of field-name.")
583 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
584 "All fields that match this list will be displayed in MIME preview buffer.
585 Each elements are regexp of field-name.")
591 ;;; @@@ predicate function
594 (in-calist-package 'mime-view)
596 (defun mime-calist::field-match-method-as-default-rule (calist
597 field-type field-value)
598 (let ((s-field (assq field-type calist)))
599 (cond ((null s-field)
600 (cons (cons field-type field-value) calist)
604 (define-calist-field-match-method
605 'header #'mime-calist::field-match-method-as-default-rule)
607 (define-calist-field-match-method
608 'body #'mime-calist::field-match-method-as-default-rule)
611 (defvar mime-preview-condition nil
612 "Condition-tree about how to display entity.")
614 (ctree-set-calist-strictly
615 'mime-preview-condition '((type . application)(subtype . octet-stream)
618 (ctree-set-calist-strictly
619 'mime-preview-condition '((type . application)(subtype . octet-stream)
622 (ctree-set-calist-strictly
623 'mime-preview-condition '((type . application)(subtype . octet-stream)
627 (ctree-set-calist-strictly
628 'mime-preview-condition '((type . application)(subtype . pgp)
631 (ctree-set-calist-strictly
632 'mime-preview-condition '((type . application)(subtype . x-latex)
635 (ctree-set-calist-strictly
636 'mime-preview-condition '((type . application)(subtype . x-selection)
639 (ctree-set-calist-strictly
640 'mime-preview-condition '((type . application)(subtype . x-comment)
643 (ctree-set-calist-strictly
644 'mime-preview-condition '((type . message)(subtype . delivery-status)
647 (ctree-set-calist-strictly
648 'mime-preview-condition
650 (body-presentation-method . mime-display-text/plain)))
652 (ctree-set-calist-strictly
653 'mime-preview-condition
656 (body-presentation-method . mime-display-text/plain)))
658 (ctree-set-calist-strictly
659 'mime-preview-condition
660 '((type . text)(subtype . enriched)
662 (body-presentation-method . mime-display-text/enriched)))
664 (ctree-set-calist-strictly
665 'mime-preview-condition
666 '((type . text)(subtype . richtext)
668 (body-presentation-method . mime-display-text/richtext)))
670 (autoload 'mime-display-application/x-postpet "postpet")
672 (ctree-set-calist-strictly
673 'mime-preview-condition
674 '((type . application)(subtype . x-postpet)
676 (body-presentation-method . mime-display-application/x-postpet)))
678 (ctree-set-calist-strictly
679 'mime-preview-condition
680 '((type . text)(subtype . t)
682 (body-presentation-method . mime-display-text/plain)))
684 (ctree-set-calist-strictly
685 'mime-preview-condition
686 '((type . multipart)(subtype . alternative)
688 (body-presentation-method . mime-display-multipart/alternative)))
690 (ctree-set-calist-strictly
691 'mime-preview-condition '((type . message)(subtype . partial)
692 (body-presentation-method
693 . mime-display-message/partial-button)))
695 (ctree-set-calist-strictly
696 'mime-preview-condition '((type . message)(subtype . rfc822)
697 (body-presentation-method . nil)
698 (childrens-situation (header . visible)
699 (entity-button . invisible))))
701 (ctree-set-calist-strictly
702 'mime-preview-condition '((type . message)(subtype . news)
703 (body-presentation-method . nil)
704 (childrens-situation (header . visible)
705 (entity-button . invisible))))
708 ;;; @@@ entity presentation
711 (defun mime-display-text/plain (entity situation)
713 (narrow-to-region (point-max)(point-max))
714 (mime-insert-text-content entity)
715 (run-hooks 'mime-text-decode-hook)
716 (goto-char (point-max))
717 (if (not (eq (char-after (1- (point))) ?\n))
720 (mime-add-url-buttons)
721 (run-hooks 'mime-display-text/plain-hook)
724 (defun mime-display-text/richtext (entity situation)
726 (narrow-to-region (point-max)(point-max))
727 (mime-insert-text-content entity)
728 (run-hooks 'mime-text-decode-hook)
729 (let ((beg (point-min)))
730 (remove-text-properties beg (point-max) '(face nil))
731 (richtext-decode beg (point-max))
734 (defun mime-display-text/enriched (entity situation)
736 (narrow-to-region (point-max)(point-max))
737 (mime-insert-text-content entity)
738 (run-hooks 'mime-text-decode-hook)
739 (let ((beg (point-min)))
740 (remove-text-properties beg (point-max) '(face nil))
741 (enriched-decode beg (point-max))
745 (defvar mime-view-announcement-for-message/partial
746 (if (and (>= emacs-major-version 19) window-system)
748 \[[ This is message/partial style split message. ]]
749 \[[ Please press `v' key in this buffer ]]
750 \[[ or click here by mouse button-2. ]]"
752 \[[ This is message/partial style split message. ]]
753 \[[ Please press `v' key in this buffer. ]]"
756 (defun mime-display-message/partial-button (&optional entity situation)
758 (goto-char (point-max))
759 (if (not (search-backward "\n\n" nil t))
762 (goto-char (point-max))
763 (narrow-to-region (point-max)(point-max))
764 (insert mime-view-announcement-for-message/partial)
765 (mime-add-button (point-min)(point-max)
766 #'mime-preview-play-current-entity)
769 (defun mime-display-multipart/mixed (entity situation)
770 (let ((children (mime-entity-children entity))
771 (original-major-mode-cell (assq 'major-mode situation))
773 (cdr (assq 'childrens-situation situation))))
774 (if original-major-mode-cell
775 (setq default-situation
776 (cons original-major-mode-cell default-situation)))
778 (mime-display-entity (car children) nil default-situation)
779 (setq children (cdr children))
782 (defcustom mime-view-type-subtype-score-alist
783 '(((text . enriched) . 3)
784 ((text . richtext) . 2)
787 "Alist MEDIA-TYPE vs corresponding score.
788 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
790 :type '(repeat (cons (choice :tag "Media-Type"
791 (cons :tag "Type/Subtype"
792 (symbol :tag "Primary-type")
793 (symbol :tag "Subtype"))
795 (const :tag "Default" t))
798 (defun mime-display-multipart/alternative (entity situation)
799 (let* ((children (mime-entity-children entity))
800 (original-major-mode-cell (assq 'major-mode situation))
802 (cdr (assq 'childrens-situation situation)))
807 (if original-major-mode-cell
808 (setq default-situation
809 (cons original-major-mode-cell default-situation)))
814 (or (ctree-match-calist
815 mime-preview-condition
816 (append (mime-entity-situation child)
819 (if (cdr (assq 'body-presentation-method situation))
824 (cdr (assq 'type situation))
825 (cdr (assq 'subtype situation)))
826 mime-view-type-subtype-score-alist)
828 (cdr (assq 'type situation))
829 mime-view-type-subtype-score-alist)
832 mime-view-type-subtype-score-alist)
834 (if (> score max-score)
844 (let ((child (car children))
845 (situation (car situations)))
846 (mime-display-entity child (if (= i p)
848 (del-alist 'body-presentation-method
849 (copy-alist situation))))
851 (setq children (cdr children)
852 situations (cdr situations)
857 ;;; @ acting-condition
860 (defvar mime-acting-condition nil
861 "Condition-tree about how to process entity.")
863 (if (file-readable-p mailcap-file)
864 (let ((entries (mailcap-parse-file)))
866 (let ((entry (car entries))
869 (let* ((field (car entry))
870 (field-type (car field)))
871 (cond ((eq field-type 'view) (setq view field))
872 ((eq field-type 'print) (setq print field))
873 ((memq field-type '(compose composetyped edit)))
874 (t (setq shared (cons field shared))))
876 (setq entry (cdr entry))
878 (setq shared (nreverse shared))
879 (ctree-set-calist-with-default
880 'mime-acting-condition
881 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
883 (ctree-set-calist-with-default
884 'mime-acting-condition
886 (list '(mode . "print")(cons 'method (cdr view))))
889 (setq entries (cdr entries))
892 (ctree-set-calist-strictly
893 'mime-acting-condition
894 '((type . application)(subtype . octet-stream)
896 (method . mime-detect-content)
899 (ctree-set-calist-with-default
900 'mime-acting-condition
902 (method . mime-save-content)))
904 (ctree-set-calist-strictly
905 'mime-acting-condition
906 '((type . text)(subtype . x-rot13-47)(mode . "play")
907 (method . mime-view-caesar)
909 (ctree-set-calist-strictly
910 'mime-acting-condition
911 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
912 (method . mime-view-caesar)
915 (ctree-set-calist-strictly
916 'mime-acting-condition
917 '((type . message)(subtype . rfc822)(mode . "play")
918 (method . mime-view-message/rfc822)
920 (ctree-set-calist-strictly
921 'mime-acting-condition
922 '((type . message)(subtype . partial)(mode . "play")
923 (method . mime-store-message/partial-piece)
926 (ctree-set-calist-strictly
927 'mime-acting-condition
928 '((type . message)(subtype . external-body)
929 ("access-type" . "anon-ftp")
930 (method . mime-view-message/external-anon-ftp)
933 (ctree-set-calist-strictly
934 'mime-acting-condition
935 '((type . message)(subtype . external-body)
936 ("access-type" . "url")
937 (method . mime-view-message/external-url)
940 (ctree-set-calist-strictly
941 'mime-acting-condition
942 '((type . application)(subtype . octet-stream)
943 (method . mime-save-content)
947 ;;; @ quitting method
950 (defvar mime-preview-quitting-method-alist
951 '((mime-show-message-mode
952 . mime-preview-quitting-method-for-mime-show-message-mode))
953 "Alist of major-mode vs. quitting-method of mime-view.")
955 (defvar mime-preview-over-to-previous-method-alist nil
956 "Alist of major-mode vs. over-to-previous-method of mime-view.")
958 (defvar mime-preview-over-to-next-method-alist nil
959 "Alist of major-mode vs. over-to-next-method of mime-view.")
962 ;;; @ following method
965 (defvar mime-preview-following-method-alist nil
966 "Alist of major-mode vs. following-method of mime-view.")
968 (defvar mime-view-following-required-fields-list
975 (defun mime-display-entity (entity &optional situation
976 default-situation preview-buffer)
978 (setq preview-buffer (current-buffer)))
979 (let* (e nb ne nhb nbb)
980 (mime-goto-header-start-point entity)
981 (in-calist-package 'mime-view)
984 (or (ctree-match-calist mime-preview-condition
985 (append (mime-entity-situation entity)
988 (let ((button-is-invisible
989 (eq (cdr (assq 'entity-button situation)) 'invisible))
991 (eq (cdr (assq 'header situation)) 'visible))
992 (header-presentation-method
993 (or (cdr (assq 'header-presentation-method situation))
994 (cdr (assq (cdr (assq 'major-mode situation))
995 mime-header-presentation-method-alist))))
996 (body-presentation-method
997 (cdr (assq 'body-presentation-method situation)))
998 (children (mime-entity-children entity)))
999 (set-buffer preview-buffer)
1001 (narrow-to-region nb nb)
1002 (or button-is-invisible
1003 (if (mime-view-entity-button-visible-p entity)
1004 (mime-view-insert-entity-button entity)
1006 (when header-is-visible
1008 (if header-presentation-method
1009 (funcall header-presentation-method entity situation)
1010 (mime-insert-header entity
1011 mime-view-ignored-field-list
1012 mime-view-visible-field-list))
1013 (run-hooks 'mime-display-header-hook)
1014 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1015 (goto-char (point-max))
1020 ((functionp body-presentation-method)
1021 (funcall body-presentation-method entity situation)
1024 (when button-is-invisible
1025 (goto-char (point-max))
1026 (mime-view-insert-entity-button entity)
1028 (or header-is-visible
1030 (goto-char (point-max))
1034 (setq ne (point-max))
1036 (put-text-property nb ne 'mime-view-entity entity)
1037 (put-text-property nb ne 'mime-view-situation situation)
1038 (put-text-property nbb ne 'mime-view-entity-body entity)
1041 (if (functionp body-presentation-method)
1042 (funcall body-presentation-method entity situation)
1043 (mime-display-multipart/mixed entity situation)
1048 ;;; @ MIME viewer mode
1051 (defconst mime-view-menu-title "MIME-View")
1052 (defconst mime-view-menu-list
1053 '((up "Move to upper entity" mime-preview-move-to-upper)
1054 (previous "Move to previous entity" mime-preview-move-to-previous)
1055 (next "Move to next entity" mime-preview-move-to-next)
1056 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
1057 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
1058 (play "Play current entity" mime-preview-play-current-entity)
1059 (extract "Extract current entity" mime-preview-extract-current-entity)
1060 (print "Print current entity" mime-preview-print-current-entity)
1062 "Menu for MIME Viewer")
1064 (cond ((featurep 'xemacs)
1065 (defvar mime-view-xemacs-popup-menu
1066 (cons mime-view-menu-title
1069 (vector (nth 1 item)(nth 2 item) t)
1071 mime-view-menu-list)))
1072 (defun mime-view-xemacs-popup-menu (event)
1073 "Popup the menu in the MIME Viewer buffer"
1075 (select-window (event-window event))
1076 (set-buffer (event-buffer event))
1077 (popup-menu 'mime-view-xemacs-popup-menu))
1078 (defvar mouse-button-2 'button2)
1081 (defvar mime-view-popup-menu
1082 (let ((menu (make-sparse-keymap mime-view-menu-title)))
1086 (list (intern (nth 1 item)) 'menu-item
1087 (nth 1 item)(nth 2 item))
1089 mime-view-menu-list))))
1090 (defun mime-view-popup-menu (event)
1091 "Popup the menu in the MIME Viewer buffer"
1093 (let ((menu mime-view-popup-menu) events func)
1094 (setq events (x-popup-menu t menu))
1096 (setq func (lookup-key menu (apply #'vector events)))
1099 (defvar mouse-button-2 [mouse-2])
1102 (defun mime-view-define-keymap (&optional default)
1103 (let ((mime-view-mode-map (if (keymapp default)
1104 (copy-keymap default)
1105 (make-sparse-keymap)
1107 (define-key mime-view-mode-map
1108 "u" (function mime-preview-move-to-upper))
1109 (define-key mime-view-mode-map
1110 "p" (function mime-preview-move-to-previous))
1111 (define-key mime-view-mode-map
1112 "n" (function mime-preview-move-to-next))
1113 (define-key mime-view-mode-map
1114 "\e\t" (function mime-preview-move-to-previous))
1115 (define-key mime-view-mode-map
1116 "\t" (function mime-preview-move-to-next))
1117 (define-key mime-view-mode-map
1118 " " (function mime-preview-scroll-up-entity))
1119 (define-key mime-view-mode-map
1120 "\M- " (function mime-preview-scroll-down-entity))
1121 (define-key mime-view-mode-map
1122 "\177" (function mime-preview-scroll-down-entity))
1123 (define-key mime-view-mode-map
1124 "\C-m" (function mime-preview-next-line-entity))
1125 (define-key mime-view-mode-map
1126 "\C-\M-m" (function mime-preview-previous-line-entity))
1127 (define-key mime-view-mode-map
1128 "v" (function mime-preview-play-current-entity))
1129 (define-key mime-view-mode-map
1130 "e" (function mime-preview-extract-current-entity))
1131 (define-key mime-view-mode-map
1132 "\C-c\C-p" (function mime-preview-print-current-entity))
1133 (define-key mime-view-mode-map
1134 "a" (function mime-preview-follow-current-entity))
1135 (define-key mime-view-mode-map
1136 "q" (function mime-preview-quit))
1137 (define-key mime-view-mode-map
1138 "\C-c\C-x" (function mime-preview-kill-buffer))
1139 ;; (define-key mime-view-mode-map
1140 ;; "<" (function beginning-of-buffer))
1141 ;; (define-key mime-view-mode-map
1142 ;; ">" (function end-of-buffer))
1143 (define-key mime-view-mode-map
1144 "?" (function describe-mode))
1145 (define-key mime-view-mode-map
1146 [tab] (function mime-preview-move-to-next))
1147 (define-key mime-view-mode-map
1148 [delete] (function mime-preview-scroll-down-entity))
1149 (define-key mime-view-mode-map
1150 [backspace] (function mime-preview-scroll-down-entity))
1151 (if (functionp default)
1152 (cond ((featurep 'xemacs)
1153 (set-keymap-default-binding mime-view-mode-map default)
1156 (setq mime-view-mode-map
1157 (append mime-view-mode-map (list (cons t default))))
1160 (define-key mime-view-mode-map
1161 mouse-button-2 (function mime-button-dispatcher))
1163 (cond ((featurep 'xemacs)
1164 (define-key mime-view-mode-map
1165 mouse-button-3 (function mime-view-xemacs-popup-menu))
1167 ((>= emacs-major-version 19)
1168 (define-key mime-view-mode-map
1169 mouse-button-3 (function mime-view-popup-menu))
1170 (define-key mime-view-mode-map [menu-bar mime-view]
1171 (cons mime-view-menu-title
1172 (make-sparse-keymap mime-view-menu-title)))
1175 (define-key mime-view-mode-map
1176 (vector 'menu-bar 'mime-view (car item))
1177 (cons (nth 1 item)(nth 2 item))
1180 (reverse mime-view-menu-list)
1183 (use-local-map mime-view-mode-map)
1184 (run-hooks 'mime-view-define-keymap-hook)
1187 (defsubst mime-maybe-hide-echo-buffer ()
1188 "Clear mime-echo buffer and delete window for it."
1189 (let ((buf (get-buffer mime-echo-buffer-name)))
1194 (let ((win (get-buffer-window buf)))
1201 (defvar mime-view-redisplay nil)
1204 (defun mime-display-message (message &optional preview-buffer
1205 mother default-keymap-or-function
1206 original-major-mode)
1207 "View MESSAGE in MIME-View mode.
1209 Optional argument PREVIEW-BUFFER specifies the buffer of the
1210 presentation. It must be either nil or a name of preview buffer.
1212 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1214 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1215 function. If it is a keymap, keymap of MIME-View mode will be added
1216 to it. If it is a function, it will be bound as default binding of
1217 keymap of MIME-View mode."
1218 (mime-maybe-hide-echo-buffer)
1219 (let ((win-conf (current-window-configuration)))
1221 (setq preview-buffer
1222 (concat "*Preview-" (mime-entity-name message) "*")))
1223 (or original-major-mode
1224 (setq original-major-mode
1225 (with-current-buffer (mime-entity-header-buffer message)
1227 (let ((inhibit-read-only t))
1228 (set-buffer (get-buffer-create preview-buffer))
1232 (setq mime-mother-buffer mother)
1234 (setq mime-preview-original-window-configuration win-conf)
1235 (setq major-mode 'mime-view-mode)
1236 (setq mode-name "MIME-View")
1237 (mime-display-entity message nil
1238 `((entity-button . invisible)
1240 (major-mode . ,original-major-mode))
1242 (mime-view-define-keymap default-keymap-or-function)
1244 (next-single-property-change (point-min) 'mime-view-entity)))
1247 (goto-char (point-min))
1248 (search-forward "\n\n" nil t)
1250 (run-hooks 'mime-view-mode-hook)
1251 (set-buffer-modified-p nil)
1252 (setq buffer-read-only t)
1256 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1257 default-keymap-or-function
1258 representation-type)
1259 "View RAW-BUFFER in MIME-View mode.
1260 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1262 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1263 function. If it is a keymap, keymap of MIME-View mode will be added
1264 to it. If it is a function, it will be bound as default binding of
1265 keymap of MIME-View mode.
1266 Optional argument REPRESENTATION-TYPE is representation-type of
1267 message. It must be nil, `binary' or `cooked'. If it is nil,
1268 `cooked' is used as default."
1271 (setq raw-buffer (current-buffer)))
1272 (or representation-type
1273 (setq representation-type
1275 (set-buffer raw-buffer)
1276 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1277 (assq t mime-raw-representation-type-alist)))
1279 (if (eq representation-type 'binary)
1280 (setq representation-type 'buffer)
1282 (setq preview-buffer (mime-display-message
1283 (mime-open-entity representation-type raw-buffer)
1284 preview-buffer mother default-keymap-or-function))
1285 (or (get-buffer-window preview-buffer)
1286 (let ((r-win (get-buffer-window raw-buffer)))
1288 (set-window-buffer r-win preview-buffer)
1289 (let ((m-win (and mother (get-buffer-window mother))))
1291 (set-window-buffer m-win preview-buffer)
1292 (switch-to-buffer preview-buffer)
1295 (defun mime-view-mode (&optional mother ctl encoding
1296 raw-buffer preview-buffer
1297 default-keymap-or-function)
1298 "Major mode for viewing MIME message.
1300 Here is a list of the standard keys for mime-view-mode.
1305 u Move to upper content
1306 p or M-TAB Move to previous content
1307 n or TAB Move to next content
1308 SPC Scroll up or move to next content
1309 M-SPC or DEL Scroll down or move to previous content
1310 RET Move to next line
1311 M-RET Move to previous line
1312 v Decode current content as `play mode'
1313 e Decode current content as `extract mode'
1314 C-c C-p Decode current content as `print mode'
1315 a Followup to current content.
1317 button-2 Move to point under the mouse cursor
1318 and decode current content as `play mode'
1321 (unless mime-view-redisplay
1323 (if raw-buffer (set-buffer raw-buffer))
1326 (or (assq major-mode mime-raw-representation-type-alist)
1327 (assq t mime-raw-representation-type-alist)))))
1328 (if (eq type 'binary)
1331 (setq mime-message-structure (mime-open-entity type raw-buffer))
1332 (or (mime-entity-content-type mime-message-structure)
1333 (mime-entity-set-content-type-internal
1334 mime-message-structure ctl))
1336 (or (mime-entity-encoding mime-message-structure)
1337 (mime-entity-set-encoding-internal mime-message-structure encoding))
1339 (mime-display-message mime-message-structure preview-buffer
1340 mother default-keymap-or-function)
1347 (autoload 'mime-preview-play-current-entity "mime-play"
1348 "Play current entity." t)
1350 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1351 "Extract current entity into file (maybe).
1352 It decodes current entity to call internal or external method as
1353 \"extract\" mode. The method is selected from variable
1354 `mime-acting-condition'."
1356 (mime-preview-play-current-entity ignore-examples "extract")
1359 (defun mime-preview-print-current-entity (&optional ignore-examples)
1360 "Print current entity (maybe).
1361 It decodes current entity to call internal or external method as
1362 \"print\" mode. The method is selected from variable
1363 `mime-acting-condition'."
1365 (mime-preview-play-current-entity ignore-examples "print")
1372 (defun mime-preview-follow-current-entity ()
1373 "Write follow message to current entity.
1374 It calls following-method selected from variable
1375 `mime-preview-following-method-alist'."
1378 (while (null (setq entity
1379 (get-text-property (point) 'mime-view-entity)))
1383 (previous-single-property-change (point) 'mime-view-entity))
1386 (entity-node-id (mime-entity-node-id entity))
1387 (len (length entity-node-id))
1391 (if (eq (next-single-property-change (point-min)
1397 ((eq (next-single-property-change p-beg 'mime-view-entity)
1399 (setq p-beg (point))
1401 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1403 (setq p-end (point-max))
1405 ((null entity-node-id)
1406 (setq p-end (point-max))
1414 (next-single-property-change
1415 (point) 'mime-view-entity))
1417 (let ((rc (mime-entity-node-id
1418 (get-text-property (point)
1419 'mime-view-entity))))
1420 (or (equal entity-node-id
1421 (nthcdr (- (length rc) len) rc))
1426 (setq p-end (point-max))
1430 (previous-single-property-change p-end 'mime-view-entity-header))
1431 (if (or (null ph-end)
1435 (let* ((mode (mime-preview-original-major-mode 'recursive))
1437 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1439 (the-buf (current-buffer))
1442 (set-buffer (setq new-buf (get-buffer-create new-name)))
1444 (insert-buffer-substring the-buf ph-end p-end)
1445 (when (= ph-end p-beg)
1446 (goto-char (point-min))
1448 (goto-char (point-min))
1449 (let ((current-entity
1450 (if (and (eq (mime-entity-media-type entity) 'message)
1451 (eq (mime-entity-media-subtype entity) 'rfc822))
1452 (mime-entity-children entity)
1455 (while (and current-entity
1458 (with-current-buffer
1459 (mime-entity-header-buffer current-entity)
1462 (mime-entity-header-start-point
1464 (mime-entity-header-end-point
1466 (std11-header-string-except
1469 (apply (function regexp-or) fields)
1471 (if (and (eq (mime-entity-media-type
1472 current-entity) 'message)
1473 (eq (mime-entity-media-subtype
1474 current-entity) 'rfc822))
1480 (setq fields (std11-collect-field-names)
1481 current-entity (mime-entity-parent current-entity))
1484 (let ((rest mime-view-following-required-fields-list)
1487 (setq field-name (car rest))
1488 (or (std11-field-body field-name)
1491 (set-buffer the-buf)
1492 (let ((entity (when mime-mother-buffer
1493 (set-buffer mime-mother-buffer)
1494 (get-text-property (point)
1495 'mime-view-entity))))
1497 (null (setq ret (mime-entity-fetch-field
1498 entity field-name))))
1499 (setq entity (mime-entity-parent entity)))))
1501 (insert (concat field-name ": " ret "\n"))
1503 (setq rest (cdr rest))
1505 (mime-decode-header-in-buffer)
1507 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1512 "Sorry, following method for %s is not implemented yet."
1521 (defun mime-preview-move-to-upper ()
1522 "Move to upper entity.
1523 If there is no upper entity, call function `mime-preview-quit'."
1526 (while (null (setq cinfo
1527 (get-text-property (point) 'mime-view-entity)))
1530 (let ((r (mime-entity-parent cinfo))
1533 (while (setq point (previous-single-property-change
1534 (point) 'mime-view-entity))
1536 (when (eq r (get-text-property (point) 'mime-view-entity))
1537 (if (or (eq mime-preview-move-scroll t)
1538 (and mime-preview-move-scroll
1541 (move-to-window-line -1)
1542 (forward-line (* -1 next-screen-context-lines))
1545 (recenter next-screen-context-lines))
1552 (defun mime-preview-move-to-previous ()
1553 "Move to previous entity.
1554 If there is no previous entity, it calls function registered in
1555 variable `mime-preview-over-to-previous-method-alist'."
1557 (while (and (not (bobp))
1558 (null (get-text-property (point) 'mime-view-entity)))
1561 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1563 (>= point (point-min)))
1564 (if (get-text-property (1- point) 'mime-view-entity)
1565 (progn (goto-char point)
1567 (or (eq mime-preview-move-scroll t)
1568 (and mime-preview-move-scroll
1571 (move-to-window-line 0)
1572 (forward-line next-screen-context-lines)
1575 (recenter (* -1 next-screen-context-lines))))
1576 (goto-char (1- point))
1577 (mime-preview-move-to-previous)
1579 (let ((f (assq (mime-preview-original-major-mode)
1580 mime-preview-over-to-previous-method-alist)))
1586 (defun mime-preview-move-to-next ()
1587 "Move to next entity.
1588 If there is no previous entity, it calls function registered in
1589 variable `mime-preview-over-to-next-method-alist'."
1591 (while (and (not (eobp))
1592 (null (get-text-property (point) 'mime-view-entity)))
1595 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1597 (<= point (point-max)))
1600 (if (null (get-text-property point 'mime-view-entity))
1601 (mime-preview-move-to-next)
1603 (or (eq mime-preview-move-scroll t)
1604 (and mime-preview-move-scroll
1607 (move-to-window-line -1)
1609 (* -1 next-screen-context-lines))
1612 (recenter next-screen-context-lines))
1614 (let ((f (assq (mime-preview-original-major-mode)
1615 mime-preview-over-to-next-method-alist)))
1621 (defun mime-preview-scroll-up-entity (&optional h)
1622 "Scroll up current entity.
1623 If reached to (point-max), it calls function registered in variable
1624 `mime-preview-over-to-next-method-alist'."
1627 (let ((f (assq (mime-preview-original-major-mode)
1628 mime-preview-over-to-next-method-alist)))
1633 (or (next-single-property-change (point) 'mime-view-entity)
1635 (bottom (window-end (selected-window))))
1638 (progn (goto-char point)
1639 (recenter next-screen-context-lines))
1643 (goto-char (point-max)))))
1646 (defun mime-preview-scroll-down-entity (&optional h)
1647 "Scroll down current entity.
1648 If reached to (point-min), it calls function registered in variable
1649 `mime-preview-over-to-previous-method-alist'."
1652 (let ((f (assq (mime-preview-original-major-mode)
1653 mime-preview-over-to-previous-method-alist)))
1658 (or (previous-single-property-change (point) 'mime-view-entity)
1660 (top (window-start (selected-window))))
1663 (progn (goto-char point)
1664 (recenter (* -1 next-screen-context-lines)))
1667 (beginning-of-buffer
1668 (goto-char (point-min)))))
1671 (defun mime-preview-next-line-entity (&optional lines)
1672 "Scroll up one line (or prefix LINES lines).
1673 If LINES is negative, scroll down LINES lines."
1675 (mime-preview-scroll-up-entity (or lines 1))
1678 (defun mime-preview-previous-line-entity (&optional lines)
1679 "Scrroll down one line (or prefix LINES lines).
1680 If LINES is negative, scroll up LINES lines."
1682 (mime-preview-scroll-down-entity (or lines 1))
1688 (defun mime-preview-quit ()
1689 "Quit from MIME-preview buffer.
1690 It calls function registered in variable
1691 `mime-preview-quitting-method-alist'."
1693 (let ((r (assq (mime-preview-original-major-mode)
1694 mime-preview-quitting-method-alist)))
1699 (defun mime-preview-kill-buffer ()
1701 (kill-buffer (current-buffer))
1708 (provide 'mime-view)
1710 (let* ((file mime-situation-examples-file)
1711 (buffer (get-buffer-create " *mime-example*")))
1712 (if (file-readable-p file)
1717 (insert-file-contents file)
1722 (while (and (> (length mime-acting-situation-example-list)
1723 mime-acting-situation-example-list-max-size)
1725 (mime-reduce-acting-situation-examples)
1728 (error (setq mime-acting-situation-example-list nil)))
1730 (kill-buffer buffer))))
1732 ;;; mime-view.el ends here