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 (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."
65 (defcustom mime-preview-move-scroll nil
66 "*Decides whether to scroll when moving to next entity.
67 When t, scroll the buffer. Non-nil but not t means scroll when
68 the next entity is within next-screen-context-lines from top or
69 buttom. Nil means don't scroll at all."
71 :type '(choice (const :tag "Off" nil)
73 (sexp :tag "Situation" 1)))
75 ;;; @ in raw-buffer (representation space)
78 (defvar mime-preview-buffer nil
79 "MIME-preview buffer corresponding with the (raw) buffer.")
80 (make-variable-buffer-local 'mime-preview-buffer)
83 (defvar mime-raw-representation-type-alist
84 '((mime-show-message-mode . binary)
85 (mime-temp-message-mode . binary)
88 "Alist of major-mode vs. representation-type of mime-raw-buffer.
89 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
90 major-mode or t. t means default. REPRESENTATION-TYPE must be
91 `binary' or `cooked'.")
94 ;; (defun mime-raw-find-entity-from-point (point &optional message-info)
95 ;; "Return entity from POINT in mime-raw-buffer.
96 ;; If optional argument MESSAGE-INFO is not specified,
97 ;; `mime-message-structure' is used."
99 ;; (setq message-info mime-message-structure))
100 ;; (if (and (<= (mime-entity-point-min message-info) point)
101 ;; (<= point (mime-entity-point-max message-info)))
102 ;; (let ((children (mime-entity-children message-info)))
106 ;; (mime-raw-find-entity-from-point point (car children))))
110 ;; (setq children (cdr children)))
112 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
115 ;;; @ in preview-buffer (presentation space)
118 (defvar mime-mother-buffer nil
119 "Mother buffer corresponding with the (MIME-preview) buffer.
120 If current MIME-preview buffer is generated by other buffer, such as
121 message/partial, it is called `mother-buffer'.")
122 (make-variable-buffer-local 'mime-mother-buffer)
124 ;; (defvar mime-raw-buffer nil
125 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
126 ;; (make-variable-buffer-local 'mime-raw-buffer)
128 (defvar mime-preview-original-window-configuration nil
129 "Window-configuration before mime-view-mode is called.")
130 (make-variable-buffer-local 'mime-preview-original-window-configuration)
132 (defun mime-preview-original-major-mode (&optional recursive point)
133 "Return major-mode of original buffer.
134 If optional argument RECURSIVE is non-nil and current buffer has
135 mime-mother-buffer, it returns original major-mode of the
137 (if (and recursive mime-mother-buffer)
139 (set-buffer mime-mother-buffer)
140 (mime-preview-original-major-mode recursive)
142 (cdr (assq 'major-mode
143 (get-text-property (or point
144 (if (> (point) (buffer-size))
145 (max (1- (point-max)) (point-min))
147 'mime-view-situation)))))
150 ;;; @ entity information
153 (defun mime-entity-situation (entity &optional situation)
154 "Return situation of ENTITY."
155 (let (rest param name)
157 (unless (assq 'type situation)
158 (setq rest (or (mime-entity-content-type entity)
159 (make-mime-content-type 'text 'plain))
160 situation (cons (car rest) situation)
163 (unless (assq 'subtype situation)
165 (setq rest (or (cdr (mime-entity-content-type entity))
166 '((subtype . plain)))))
167 (setq situation (cons (car rest) situation)
171 (setq param (car rest))
172 (or (assoc (car param) situation)
173 (setq situation (cons param situation)))
174 (setq rest (cdr rest)))
176 ;; Content-Disposition
178 (unless (assq 'disposition-type situation)
179 (setq rest (mime-entity-content-disposition entity))
181 (setq situation (cons (cons 'disposition-type
182 (mime-content-disposition-type rest))
184 rest (mime-content-disposition-parameters rest))
187 (setq param (car rest)
189 (if (cond ((string= name "filename")
190 (if (assq 'filename situation)
192 (setq name 'filename)))
193 ((string= name "creation-date")
194 (if (assq 'creation-date situation)
196 (setq name 'creation-date)))
197 ((string= name "modification-date")
198 (if (assq 'modification-date situation)
200 (setq name 'modification-date)))
201 ((string= name "read-date")
202 (if (assq 'read-date situation)
204 (setq name 'read-date)))
205 ((string= name "size")
206 (if (assq 'size situation)
209 (t (setq name (cons 'disposition name))
210 (if (assoc name situation)
214 (cons (cons name (cdr param))
216 (setq rest (cdr rest)))
218 ;; Content-Transfer-Encoding
219 (or (assq 'encoding situation)
221 (cons (cons 'encoding (or (mime-entity-encoding entity)
227 (defsubst mime-delq-null-situation (situations field
228 &optional ignored-value)
231 (let* ((situation (car situations))
232 (cell (assq field situation)))
234 (or (eq (cdr cell) ignored-value)
235 (setq dest (cons situation dest))
237 (setq situations (cdr situations)))
240 (defun mime-compare-situation-with-example (situation example)
241 (let ((example (copy-alist example))
244 (let* ((cell (car situation))
246 (ecell (assoc key example)))
248 (if (equal cell ecell)
249 (setq match (1+ match))
250 (setq example (delq ecell example))
253 (setq situation (cdr situation))
258 (defun mime-sort-situation (situation)
268 (disposition-type . 6)
272 (let ((ret (assq a-t order)))
274 (setq a-order (cdr ret))
280 (let ((ret (assq b-t order)))
282 (setq b-order (cdr ret))
287 (if (= a-order b-order)
288 (string< (format "%s" a-t)(format "%s" b-t))
293 (defun mime-unify-situations (entity-situation condition situation-examples
294 &optional ignored-method)
296 (in-calist-package 'mime-view)
298 (mime-delq-null-situation
299 (ctree-find-calist condition entity-situation
300 mime-view-find-every-acting-situation)
301 'method ignored-method))
302 (or (assq 'ignore-examples entity-situation)
310 (let ((situation (car rest))
311 (examples situation-examples))
314 (mime-compare-situation-with-example
315 situation (caar examples)))
316 (ret-score (car ret)))
317 (cond ((> ret-score max-score)
318 (setq max-score ret-score
319 max-escore (cdar examples)
320 max-examples (list (cdr ret))
321 max-situations (list situation))
323 ((= ret-score max-score)
324 (cond ((> (cdar examples) max-escore)
325 (setq max-escore (cdar examples)
326 max-examples (list (cdr ret))
327 max-situations (list situation))
329 ((= (cdar examples) max-escore)
331 (cons (cdr ret) max-examples))
332 (or (member situation max-situations)
334 (cons situation max-situations)))
336 (setq examples (cdr examples))))
337 (setq rest (cdr rest)))
339 (setq ret max-situations)
341 (let* ((example (car max-examples))
343 (assoc example situation-examples)))
345 (setcdr cell (1+ (cdr cell)))
346 (setq situation-examples
347 (cons (cons example 0)
350 (setq max-examples (cdr max-examples))
352 (cons ret situation-examples)
353 ;; ret: list of situations
354 ;; situation-examples: new examples (notoce that contents of
355 ;; argument `situation-examples' has bees modified)
358 (defun mime-view-entity-title (entity)
359 (or (mime-entity-read-field entity 'Content-Description)
360 (mime-entity-read-field entity 'Subject)
361 (mime-entity-filename entity)
364 (defvar mime-preview-situation-example-list nil)
365 (defvar mime-acting-situation-example-list nil)
367 (defvar mime-acting-situation-example-list-max-size 16)
369 (defun mime-save-acting-situation-examples ()
370 (let* ((file mime-acting-situation-examples-file)
371 (buffer (get-buffer-create " *mime-example*")))
375 (setq buffer-file-name file)
377 (insert ";;; " (file-name-nondirectory file) "\n")
378 (insert "\n;; This file is generated automatically by "
379 mime-view-version "\n\n")
380 (insert ";;; Code:\n\n")
381 (pp `(setq mime-acting-situation-example-list
382 ',mime-acting-situation-example-list)
385 (file-name-nondirectory file)
388 (kill-buffer buffer))))
390 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
392 (defun mime-reduce-acting-situation-examples ()
393 (let ((len (length mime-acting-situation-example-list))
401 ir mime-acting-situation-example-list)
405 jr mime-acting-situation-example-list)
409 (setq ret (mime-compare-situation-with-example (car ic)(car jc))
411 det-ret (+ (length (car ic))(length (car jc)))
412 det-org (length (cdr ret))
413 freq (+ (cdr ic)(cdr jc)))
414 (cond ((< max-sim sim)
421 dest (cons (cdr ret) freq))
424 (cond ((> min-det-ret det-ret)
425 (setq min-det-ret det-ret
430 dest (cons (cdr ret) freq))
432 ((= min-det-ret det-ret)
433 (cond ((> min-det-org det-org)
434 (setq min-det-org det-org
438 dest (cons (cdr ret) freq))
440 ((= min-det-org det-org)
441 (cond ((> min-freq freq)
445 dest (cons (cdr ret) freq))
459 (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
460 (setcdr jr (cddr jr))
462 (setq mime-acting-situation-example-list
463 (cdr mime-acting-situation-example-list))
464 (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
465 (setcdr ir (cddr ir))
467 (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
468 (setcdr ir (+ (cdr ir)(cdr dest)))
469 (setq mime-acting-situation-example-list
470 (cons dest mime-acting-situation-example-list))
474 ;;; @ presentation of preview
480 ;;; @@@ predicate function
483 (defun mime-view-entity-button-visible-p (entity)
484 "Return non-nil if header of ENTITY is visible.
485 Please redefine this function if you want to change default setting."
486 (let ((media-type (mime-entity-media-type entity))
487 (media-subtype (mime-entity-media-subtype entity)))
488 (or (not (eq media-type 'application))
489 (and (not (eq media-subtype 'x-selection))
490 (or (not (eq media-subtype 'octet-stream))
491 (let ((mother-entity (mime-entity-parent entity)))
492 (or (not (eq (mime-entity-media-type mother-entity)
494 (not (eq (mime-entity-media-subtype mother-entity)
499 ;;; @@@ entity button generator
502 (defun mime-view-insert-entity-button (entity)
503 "Insert entity-button of ENTITY."
504 (let ((entity-node-id (mime-entity-node-id entity))
505 (params (mime-entity-parameters entity))
506 (subject (mime-view-entity-title entity)))
508 (let ((access-type (assoc "access-type" params))
509 (num (or (cdr (assoc "x-part-number" params))
510 (if (consp entity-node-id)
513 (format "%s" (1+ num))
515 (reverse entity-node-id) ".")
519 (let ((server (assoc "server" params)))
520 (setq access-type (cdr access-type))
522 (format "%s %s ([%s] %s)"
523 num subject access-type (cdr server))
524 (let ((site (cdr (assoc "site" params)))
525 (dir (cdr (assoc "directory" params)))
526 (url (cdr (assoc "url" params)))
529 (format "%s %s ([%s] %s)"
530 num subject access-type url)
531 (format "%s %s ([%s] %s:%s)"
532 num subject access-type site dir))
536 (let ((media-type (mime-entity-media-type entity))
537 (media-subtype (mime-entity-media-subtype entity))
538 (charset (cdr (assoc "charset" params)))
539 (encoding (mime-entity-encoding entity)))
543 (format " <%s/%s%s%s>"
544 media-type media-subtype
546 (concat "; " charset)
549 (concat " (" encoding ")")
551 (if (>= (+ (current-column)(length rest))(window-width))
555 (function mime-preview-play-current-entity))
562 (defvar mime-header-presentation-method-alist nil
563 "Alist of major mode vs. corresponding header-presentation-method functions.
564 Each element looks like (SYMBOL . FUNCTION).
565 SYMBOL must be major mode in raw-buffer or t. t means default.
566 Interface of FUNCTION must be (ENTITY SITUATION).")
568 (defvar mime-view-ignored-field-list
569 '(".*Received:" ".*Path:" ".*Id:" "^References:"
570 "^Replied:" "^Errors-To:"
571 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
572 "^Content-Type:" "^Precedence:"
573 "^Status:" "^X-VM-.*:")
574 "All fields that match this list will be hidden in MIME preview buffer.
575 Each elements are regexp of field-name.")
577 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
578 "All fields that match this list will be displayed in MIME preview buffer.
579 Each elements are regexp of field-name.")
585 ;;; @@@ predicate function
588 (in-calist-package 'mime-view)
590 (defun mime-calist::field-match-method-as-default-rule (calist
591 field-type field-value)
592 (let ((s-field (assq field-type calist)))
593 (cond ((null s-field)
594 (cons (cons field-type field-value) calist)
598 (define-calist-field-match-method
599 'header #'mime-calist::field-match-method-as-default-rule)
601 (define-calist-field-match-method
602 'body #'mime-calist::field-match-method-as-default-rule)
605 (defvar mime-preview-condition nil
606 "Condition-tree about how to display entity.")
608 (ctree-set-calist-strictly
609 'mime-preview-condition '((type . application)(subtype . octet-stream)
612 (ctree-set-calist-strictly
613 'mime-preview-condition '((type . application)(subtype . octet-stream)
616 (ctree-set-calist-strictly
617 'mime-preview-condition '((type . application)(subtype . octet-stream)
621 (ctree-set-calist-strictly
622 'mime-preview-condition '((type . application)(subtype . pgp)
625 (ctree-set-calist-strictly
626 'mime-preview-condition '((type . application)(subtype . x-latex)
629 (ctree-set-calist-strictly
630 'mime-preview-condition '((type . application)(subtype . x-selection)
633 (ctree-set-calist-strictly
634 'mime-preview-condition '((type . application)(subtype . x-comment)
637 (ctree-set-calist-strictly
638 'mime-preview-condition '((type . message)(subtype . delivery-status)
641 (ctree-set-calist-strictly
642 'mime-preview-condition
644 (body-presentation-method . mime-display-text/plain)))
646 (ctree-set-calist-strictly
647 'mime-preview-condition
650 (body-presentation-method . mime-display-text/plain)))
652 (ctree-set-calist-strictly
653 'mime-preview-condition
654 '((type . text)(subtype . enriched)
656 (body-presentation-method . mime-display-text/enriched)))
658 (ctree-set-calist-strictly
659 'mime-preview-condition
660 '((type . text)(subtype . richtext)
662 (body-presentation-method . mime-display-text/richtext)))
664 (autoload 'mime-display-application/x-postpet "postpet")
666 (ctree-set-calist-strictly
667 'mime-preview-condition
668 '((type . application)(subtype . x-postpet)
670 (body-presentation-method . mime-display-application/x-postpet)))
672 (ctree-set-calist-strictly
673 'mime-preview-condition
674 '((type . text)(subtype . t)
676 (body-presentation-method . mime-display-text/plain)))
678 (ctree-set-calist-strictly
679 'mime-preview-condition
680 '((type . multipart)(subtype . alternative)
682 (body-presentation-method . mime-display-multipart/alternative)))
684 (ctree-set-calist-strictly
685 'mime-preview-condition '((type . message)(subtype . partial)
686 (body-presentation-method
687 . mime-display-message/partial-button)))
689 (ctree-set-calist-strictly
690 'mime-preview-condition '((type . message)(subtype . rfc822)
691 (body-presentation-method . nil)
692 (childrens-situation (header . visible)
693 (entity-button . invisible))))
695 (ctree-set-calist-strictly
696 'mime-preview-condition '((type . message)(subtype . news)
697 (body-presentation-method . nil)
698 (childrens-situation (header . visible)
699 (entity-button . invisible))))
702 ;;; @@@ entity presentation
705 (defun mime-display-text/plain (entity situation)
707 (narrow-to-region (point-max)(point-max))
708 (mime-insert-text-content entity)
709 (run-hooks 'mime-text-decode-hook)
710 (goto-char (point-max))
711 (if (not (eq (char-after (1- (point))) ?\n))
714 (mime-add-url-buttons)
715 (run-hooks 'mime-display-text/plain-hook)
718 (defun mime-display-text/richtext (entity situation)
720 (narrow-to-region (point-max)(point-max))
721 (mime-insert-text-content entity)
722 (run-hooks 'mime-text-decode-hook)
723 (let ((beg (point-min)))
724 (remove-text-properties beg (point-max) '(face nil))
725 (richtext-decode beg (point-max))
728 (defun mime-display-text/enriched (entity situation)
730 (narrow-to-region (point-max)(point-max))
731 (mime-insert-text-content entity)
732 (run-hooks 'mime-text-decode-hook)
733 (let ((beg (point-min)))
734 (remove-text-properties beg (point-max) '(face nil))
735 (enriched-decode beg (point-max))
739 (defvar mime-view-announcement-for-message/partial
740 (if (and (>= emacs-major-version 19) window-system)
742 \[[ This is message/partial style split message. ]]
743 \[[ Please press `v' key in this buffer ]]
744 \[[ or click here by mouse button-2. ]]"
746 \[[ This is message/partial style split message. ]]
747 \[[ Please press `v' key in this buffer. ]]"
750 (defun mime-display-message/partial-button (&optional entity situation)
752 (goto-char (point-max))
753 (if (not (search-backward "\n\n" nil t))
756 (goto-char (point-max))
757 (narrow-to-region (point-max)(point-max))
758 (insert mime-view-announcement-for-message/partial)
759 (mime-add-button (point-min)(point-max)
760 #'mime-preview-play-current-entity)
763 (defun mime-display-multipart/mixed (entity situation)
764 (let ((children (mime-entity-children entity))
765 (original-major-mode-cell (assq 'major-mode situation))
767 (cdr (assq 'childrens-situation situation))))
768 (if original-major-mode-cell
769 (setq default-situation
770 (cons original-major-mode-cell default-situation)))
772 (mime-display-entity (car children) nil default-situation)
773 (setq children (cdr children))
776 (defcustom mime-view-type-subtype-score-alist
777 '(((text . enriched) . 3)
778 ((text . richtext) . 2)
781 "Alist MEDIA-TYPE vs corresponding score.
782 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
784 :type '(repeat (cons (choice :tag "Media-Type"
785 (cons :tag "Type/Subtype"
786 (symbol :tag "Primary-type")
787 (symbol :tag "Subtype"))
789 (const :tag "Default" t))
792 (defun mime-display-multipart/alternative (entity situation)
793 (let* ((children (mime-entity-children entity))
794 (original-major-mode-cell (assq 'major-mode situation))
796 (cdr (assq 'childrens-situation situation)))
801 (if original-major-mode-cell
802 (setq default-situation
803 (cons original-major-mode-cell default-situation)))
808 (or (ctree-match-calist
809 mime-preview-condition
810 (append (mime-entity-situation child)
813 (if (cdr (assq 'body-presentation-method situation))
818 (cdr (assq 'type situation))
819 (cdr (assq 'subtype situation)))
820 mime-view-type-subtype-score-alist)
822 (cdr (assq 'type situation))
823 mime-view-type-subtype-score-alist)
826 mime-view-type-subtype-score-alist)
828 (if (> score max-score)
838 (let ((child (car children))
839 (situation (car situations)))
840 (mime-display-entity child (if (= i p)
842 (del-alist 'body-presentation-method
843 (copy-alist situation))))
845 (setq children (cdr children)
846 situations (cdr situations)
851 ;;; @ acting-condition
854 (defvar mime-acting-condition nil
855 "Condition-tree about how to process entity.")
857 (if (file-readable-p mailcap-file)
858 (let ((entries (mailcap-parse-file)))
860 (let ((entry (car entries))
863 (let* ((field (car entry))
864 (field-type (car field)))
865 (cond ((eq field-type 'view) (setq view field))
866 ((eq field-type 'print) (setq print field))
867 ((memq field-type '(compose composetyped edit)))
868 (t (setq shared (cons field shared))))
870 (setq entry (cdr entry))
872 (setq shared (nreverse shared))
873 (ctree-set-calist-with-default
874 'mime-acting-condition
875 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
877 (ctree-set-calist-with-default
878 'mime-acting-condition
880 (list '(mode . "print")(cons 'method (cdr view))))
883 (setq entries (cdr entries))
886 (ctree-set-calist-strictly
887 'mime-acting-condition
888 '((type . application)(subtype . octet-stream)
890 (method . mime-detect-content)
893 (ctree-set-calist-with-default
894 'mime-acting-condition
896 (method . mime-save-content)))
898 (ctree-set-calist-strictly
899 'mime-acting-condition
900 '((type . text)(subtype . x-rot13-47)(mode . "play")
901 (method . mime-view-caesar)
903 (ctree-set-calist-strictly
904 'mime-acting-condition
905 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
906 (method . mime-view-caesar)
909 (ctree-set-calist-strictly
910 'mime-acting-condition
911 '((type . message)(subtype . rfc822)(mode . "play")
912 (method . mime-view-message/rfc822)
914 (ctree-set-calist-strictly
915 'mime-acting-condition
916 '((type . message)(subtype . partial)(mode . "play")
917 (method . mime-store-message/partial-piece)
920 (ctree-set-calist-strictly
921 'mime-acting-condition
922 '((type . message)(subtype . external-body)
923 ("access-type" . "anon-ftp")
924 (method . mime-view-message/external-anon-ftp)
927 (ctree-set-calist-strictly
928 'mime-acting-condition
929 '((type . message)(subtype . external-body)
930 ("access-type" . "url")
931 (method . mime-view-message/external-url)
934 (ctree-set-calist-strictly
935 'mime-acting-condition
936 '((type . application)(subtype . octet-stream)
937 (method . mime-save-content)
941 ;;; @ quitting method
944 (defvar mime-preview-quitting-method-alist
945 '((mime-show-message-mode
946 . mime-preview-quitting-method-for-mime-show-message-mode))
947 "Alist of major-mode vs. quitting-method of mime-view.")
949 (defvar mime-preview-over-to-previous-method-alist nil
950 "Alist of major-mode vs. over-to-previous-method of mime-view.")
952 (defvar mime-preview-over-to-next-method-alist nil
953 "Alist of major-mode vs. over-to-next-method of mime-view.")
956 ;;; @ following method
959 (defvar mime-preview-following-method-alist nil
960 "Alist of major-mode vs. following-method of mime-view.")
962 (defvar mime-view-following-required-fields-list
969 (defun mime-display-entity (entity &optional situation
970 default-situation preview-buffer)
972 (setq preview-buffer (current-buffer)))
973 (let* (e nb ne nhb nbb)
974 (mime-goto-header-start-point entity)
975 (in-calist-package 'mime-view)
978 (or (ctree-match-calist mime-preview-condition
979 (append (mime-entity-situation entity)
982 (let ((button-is-invisible
983 (eq (cdr (assq 'entity-button situation)) 'invisible))
985 (eq (cdr (assq 'header situation)) 'visible))
986 (header-presentation-method
987 (or (cdr (assq 'header-presentation-method situation))
988 (cdr (assq (cdr (assq 'major-mode situation))
989 mime-header-presentation-method-alist))))
990 (body-presentation-method
991 (cdr (assq 'body-presentation-method situation)))
992 (children (mime-entity-children entity)))
993 (set-buffer preview-buffer)
995 (narrow-to-region nb nb)
996 (or button-is-invisible
997 (if (mime-view-entity-button-visible-p entity)
998 (mime-view-insert-entity-button entity)
1000 (when header-is-visible
1002 (if header-presentation-method
1003 (funcall header-presentation-method entity situation)
1004 (mime-insert-header entity
1005 mime-view-ignored-field-list
1006 mime-view-visible-field-list))
1007 (run-hooks 'mime-display-header-hook)
1008 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1009 (goto-char (point-max))
1014 ((functionp body-presentation-method)
1015 (funcall body-presentation-method entity situation)
1018 (when button-is-invisible
1019 (goto-char (point-max))
1020 (mime-view-insert-entity-button entity)
1022 (or header-is-visible
1024 (goto-char (point-max))
1028 (setq ne (point-max))
1030 (put-text-property nb ne 'mime-view-entity entity)
1031 (put-text-property nb ne 'mime-view-situation situation)
1032 (put-text-property nbb ne 'mime-view-entity-body entity)
1035 (if (functionp body-presentation-method)
1036 (funcall body-presentation-method entity situation)
1037 (mime-display-multipart/mixed entity situation)
1042 ;;; @ MIME viewer mode
1045 (defconst mime-view-menu-title "MIME-View")
1046 (defconst mime-view-menu-list
1047 '((up "Move to upper entity" mime-preview-move-to-upper)
1048 (previous "Move to previous entity" mime-preview-move-to-previous)
1049 (next "Move to next entity" mime-preview-move-to-next)
1050 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
1051 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
1052 (play "Play current entity" mime-preview-play-current-entity)
1053 (extract "Extract current entity" mime-preview-extract-current-entity)
1054 (print "Print current entity" mime-preview-print-current-entity)
1056 "Menu for MIME Viewer")
1058 (cond ((featurep 'xemacs)
1059 (defvar mime-view-xemacs-popup-menu
1060 (cons mime-view-menu-title
1063 (vector (nth 1 item)(nth 2 item) t)
1065 mime-view-menu-list)))
1066 (defun mime-view-xemacs-popup-menu (event)
1067 "Popup the menu in the MIME Viewer buffer"
1069 (select-window (event-window event))
1070 (set-buffer (event-buffer event))
1071 (popup-menu 'mime-view-xemacs-popup-menu))
1072 (defvar mouse-button-2 'button2)
1075 (defvar mime-view-popup-menu
1076 (let ((menu (make-sparse-keymap mime-view-menu-title)))
1080 (list (intern (nth 1 item)) 'menu-item
1081 (nth 1 item)(nth 2 item))
1083 mime-view-menu-list))))
1084 (defun mime-view-popup-menu (event)
1085 "Popup the menu in the MIME Viewer buffer"
1087 (let ((menu mime-view-popup-menu) events func)
1088 (setq events (x-popup-menu t menu))
1090 (setq func (lookup-key menu (apply #'vector events)))
1093 (defvar mouse-button-2 [mouse-2])
1096 (defun mime-view-define-keymap (&optional default)
1097 (let ((mime-view-mode-map (if (keymapp default)
1098 (copy-keymap default)
1099 (make-sparse-keymap)
1101 (define-key mime-view-mode-map
1102 "u" (function mime-preview-move-to-upper))
1103 (define-key mime-view-mode-map
1104 "p" (function mime-preview-move-to-previous))
1105 (define-key mime-view-mode-map
1106 "n" (function mime-preview-move-to-next))
1107 (define-key mime-view-mode-map
1108 "\e\t" (function mime-preview-move-to-previous))
1109 (define-key mime-view-mode-map
1110 "\t" (function mime-preview-move-to-next))
1111 (define-key mime-view-mode-map
1112 " " (function mime-preview-scroll-up-entity))
1113 (define-key mime-view-mode-map
1114 "\M- " (function mime-preview-scroll-down-entity))
1115 (define-key mime-view-mode-map
1116 "\177" (function mime-preview-scroll-down-entity))
1117 (define-key mime-view-mode-map
1118 "\C-m" (function mime-preview-next-line-entity))
1119 (define-key mime-view-mode-map
1120 "\C-\M-m" (function mime-preview-previous-line-entity))
1121 (define-key mime-view-mode-map
1122 "v" (function mime-preview-play-current-entity))
1123 (define-key mime-view-mode-map
1124 "e" (function mime-preview-extract-current-entity))
1125 (define-key mime-view-mode-map
1126 "\C-c\C-p" (function mime-preview-print-current-entity))
1127 (define-key mime-view-mode-map
1128 "a" (function mime-preview-follow-current-entity))
1129 (define-key mime-view-mode-map
1130 "q" (function mime-preview-quit))
1131 (define-key mime-view-mode-map
1132 "\C-c\C-x" (function mime-preview-kill-buffer))
1133 ;; (define-key mime-view-mode-map
1134 ;; "<" (function beginning-of-buffer))
1135 ;; (define-key mime-view-mode-map
1136 ;; ">" (function end-of-buffer))
1137 (define-key mime-view-mode-map
1138 "?" (function describe-mode))
1139 (define-key mime-view-mode-map
1140 [tab] (function mime-preview-move-to-next))
1141 (define-key mime-view-mode-map
1142 [delete] (function mime-preview-scroll-down-entity))
1143 (define-key mime-view-mode-map
1144 [backspace] (function mime-preview-scroll-down-entity))
1145 (if (functionp default)
1146 (cond ((featurep 'xemacs)
1147 (set-keymap-default-binding mime-view-mode-map default)
1150 (setq mime-view-mode-map
1151 (append mime-view-mode-map (list (cons t default))))
1154 (define-key mime-view-mode-map
1155 mouse-button-2 (function mime-button-dispatcher))
1157 (cond ((featurep 'xemacs)
1158 (define-key mime-view-mode-map
1159 mouse-button-3 (function mime-view-xemacs-popup-menu))
1161 ((>= emacs-major-version 19)
1162 (define-key mime-view-mode-map
1163 mouse-button-3 (function mime-view-popup-menu))
1164 (define-key mime-view-mode-map [menu-bar mime-view]
1165 (cons mime-view-menu-title
1166 (make-sparse-keymap mime-view-menu-title)))
1169 (define-key mime-view-mode-map
1170 (vector 'menu-bar 'mime-view (car item))
1171 (cons (nth 1 item)(nth 2 item))
1174 (reverse mime-view-menu-list)
1177 (use-local-map mime-view-mode-map)
1178 (run-hooks 'mime-view-define-keymap-hook)
1181 (defsubst mime-maybe-hide-echo-buffer ()
1182 "Clear mime-echo buffer and delete window for it."
1183 (let ((buf (get-buffer mime-echo-buffer-name)))
1188 (let ((win (get-buffer-window buf)))
1195 (defvar mime-view-redisplay nil)
1198 (defun mime-display-message (message &optional preview-buffer
1199 mother default-keymap-or-function
1200 original-major-mode)
1201 "View MESSAGE in MIME-View mode.
1203 Optional argument PREVIEW-BUFFER specifies the buffer of the
1204 presentation. It must be either nil or a name of preview buffer.
1206 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1208 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1209 function. If it is a keymap, keymap of MIME-View mode will be added
1210 to it. If it is a function, it will be bound as default binding of
1211 keymap of MIME-View mode."
1212 (mime-maybe-hide-echo-buffer)
1213 (let ((win-conf (current-window-configuration)))
1215 (setq preview-buffer
1216 (concat "*Preview-" (mime-entity-name message) "*")))
1217 (or original-major-mode
1218 (setq original-major-mode
1219 (with-current-buffer (mime-entity-header-buffer message)
1221 (let ((inhibit-read-only t))
1222 (set-buffer (get-buffer-create preview-buffer))
1226 (setq mime-mother-buffer mother)
1228 (setq mime-preview-original-window-configuration win-conf)
1229 (setq major-mode 'mime-view-mode)
1230 (setq mode-name "MIME-View")
1231 (mime-display-entity message nil
1232 `((entity-button . invisible)
1234 (major-mode . ,original-major-mode))
1236 (mime-view-define-keymap default-keymap-or-function)
1238 (next-single-property-change (point-min) 'mime-view-entity)))
1241 (goto-char (point-min))
1242 (search-forward "\n\n" nil t)
1244 (run-hooks 'mime-view-mode-hook)
1245 (set-buffer-modified-p nil)
1246 (setq buffer-read-only t)
1250 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1251 default-keymap-or-function
1252 representation-type)
1253 "View RAW-BUFFER in MIME-View mode.
1254 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1256 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1257 function. If it is a keymap, keymap of MIME-View mode will be added
1258 to it. If it is a function, it will be bound as default binding of
1259 keymap of MIME-View mode.
1260 Optional argument REPRESENTATION-TYPE is representation-type of
1261 message. It must be nil, `binary' or `cooked'. If it is nil,
1262 `cooked' is used as default."
1265 (setq raw-buffer (current-buffer)))
1266 (or representation-type
1267 (setq representation-type
1269 (set-buffer raw-buffer)
1270 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1271 (assq t mime-raw-representation-type-alist)))
1273 (if (eq representation-type 'binary)
1274 (setq representation-type 'buffer)
1276 (setq preview-buffer (mime-display-message
1277 (mime-open-entity representation-type raw-buffer)
1278 preview-buffer mother default-keymap-or-function))
1279 (or (get-buffer-window preview-buffer)
1280 (let ((r-win (get-buffer-window raw-buffer)))
1282 (set-window-buffer r-win preview-buffer)
1283 (let ((m-win (and mother (get-buffer-window mother))))
1285 (set-window-buffer m-win preview-buffer)
1286 (switch-to-buffer preview-buffer)
1289 (defun mime-view-mode (&optional mother ctl encoding
1290 raw-buffer preview-buffer
1291 default-keymap-or-function)
1292 "Major mode for viewing MIME message.
1294 Here is a list of the standard keys for mime-view-mode.
1299 u Move to upper content
1300 p or M-TAB Move to previous content
1301 n or TAB Move to next content
1302 SPC Scroll up or move to next content
1303 M-SPC or DEL Scroll down or move to previous content
1304 RET Move to next line
1305 M-RET Move to previous line
1306 v Decode current content as `play mode'
1307 e Decode current content as `extract mode'
1308 C-c C-p Decode current content as `print mode'
1309 a Followup to current content.
1311 button-2 Move to point under the mouse cursor
1312 and decode current content as `play mode'
1315 (unless mime-view-redisplay
1317 (if raw-buffer (set-buffer raw-buffer))
1320 (or (assq major-mode mime-raw-representation-type-alist)
1321 (assq t mime-raw-representation-type-alist)))))
1322 (if (eq type 'binary)
1325 (setq mime-message-structure (mime-open-entity type raw-buffer))
1326 (or (mime-entity-content-type mime-message-structure)
1327 (mime-entity-set-content-type-internal
1328 mime-message-structure ctl))
1330 (or (mime-entity-encoding mime-message-structure)
1331 (mime-entity-set-encoding-internal mime-message-structure encoding))
1333 (mime-display-message mime-message-structure preview-buffer
1334 mother default-keymap-or-function)
1341 (autoload 'mime-preview-play-current-entity "mime-play"
1342 "Play current entity." t)
1344 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1345 "Extract current entity into file (maybe).
1346 It decodes current entity to call internal or external method as
1347 \"extract\" mode. The method is selected from variable
1348 `mime-acting-condition'."
1350 (mime-preview-play-current-entity ignore-examples "extract")
1353 (defun mime-preview-print-current-entity (&optional ignore-examples)
1354 "Print current entity (maybe).
1355 It decodes current entity to call internal or external method as
1356 \"print\" mode. The method is selected from variable
1357 `mime-acting-condition'."
1359 (mime-preview-play-current-entity ignore-examples "print")
1366 (defun mime-preview-follow-current-entity ()
1367 "Write follow message to current entity.
1368 It calls following-method selected from variable
1369 `mime-preview-following-method-alist'."
1372 (while (null (setq entity
1373 (get-text-property (point) 'mime-view-entity)))
1377 (previous-single-property-change (point) 'mime-view-entity))
1380 (entity-node-id (mime-entity-node-id entity))
1381 (len (length entity-node-id))
1385 (if (eq (next-single-property-change (point-min)
1391 ((eq (next-single-property-change p-beg 'mime-view-entity)
1393 (setq p-beg (point))
1395 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1397 (setq p-end (point-max))
1399 ((null entity-node-id)
1400 (setq p-end (point-max))
1408 (next-single-property-change
1409 (point) 'mime-view-entity))
1411 (let ((rc (mime-entity-node-id
1412 (get-text-property (point)
1413 'mime-view-entity))))
1414 (or (equal entity-node-id
1415 (nthcdr (- (length rc) len) rc))
1420 (setq p-end (point-max))
1424 (previous-single-property-change p-end 'mime-view-entity-header))
1425 (if (or (null ph-end)
1429 (let* ((mode (mime-preview-original-major-mode 'recursive))
1431 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1433 (the-buf (current-buffer))
1436 (set-buffer (setq new-buf (get-buffer-create new-name)))
1438 (insert-buffer-substring the-buf ph-end p-end)
1439 (when (= ph-end p-beg)
1440 (goto-char (point-min))
1442 (goto-char (point-min))
1443 (let ((current-entity
1444 (if (and (eq (mime-entity-media-type entity) 'message)
1445 (eq (mime-entity-media-subtype entity) 'rfc822))
1446 (mime-entity-children entity)
1449 (while (and current-entity
1452 (with-current-buffer
1453 (mime-entity-header-buffer current-entity)
1456 (mime-entity-header-start-point
1458 (mime-entity-header-end-point
1460 (std11-header-string-except
1463 (apply (function regexp-or) fields)
1465 (if (and (eq (mime-entity-media-type
1466 current-entity) 'message)
1467 (eq (mime-entity-media-subtype
1468 current-entity) 'rfc822))
1474 (setq fields (std11-collect-field-names)
1475 current-entity (mime-entity-parent current-entity))
1478 (let ((rest mime-view-following-required-fields-list)
1481 (setq field-name (car rest))
1482 (or (std11-field-body field-name)
1485 (set-buffer the-buf)
1486 (let ((entity (when mime-mother-buffer
1487 (set-buffer mime-mother-buffer)
1488 (get-text-property (point)
1489 'mime-view-entity))))
1491 (null (setq ret (mime-entity-fetch-field
1492 entity field-name))))
1493 (setq entity (mime-entity-parent entity)))))
1495 (insert (concat field-name ": " ret "\n"))
1497 (setq rest (cdr rest))
1499 (mime-decode-header-in-buffer)
1501 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1506 "Sorry, following method for %s is not implemented yet."
1515 (defun mime-preview-move-to-upper ()
1516 "Move to upper entity.
1517 If there is no upper entity, call function `mime-preview-quit'."
1520 (while (null (setq cinfo
1521 (get-text-property (point) 'mime-view-entity)))
1524 (let ((r (mime-entity-parent cinfo))
1527 (while (setq point (previous-single-property-change
1528 (point) 'mime-view-entity))
1530 (when (eq r (get-text-property (point) 'mime-view-entity))
1531 (if (or (eq mime-preview-move-scroll t)
1532 (and mime-preview-move-scroll
1535 (move-to-window-line -1)
1536 (forward-line (* -1 next-screen-context-lines))
1539 (recenter next-screen-context-lines))
1546 (defun mime-preview-move-to-previous ()
1547 "Move to previous entity.
1548 If there is no previous entity, it calls function registered in
1549 variable `mime-preview-over-to-previous-method-alist'."
1551 (while (and (not (bobp))
1552 (null (get-text-property (point) 'mime-view-entity)))
1555 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1557 (>= point (point-min)))
1558 (if (get-text-property (1- point) 'mime-view-entity)
1559 (progn (goto-char point)
1561 (or (eq mime-preview-move-scroll t)
1562 (and mime-preview-move-scroll
1565 (move-to-window-line 0)
1566 (forward-line next-screen-context-lines)
1569 (recenter (* -1 next-screen-context-lines))))
1570 (goto-char (1- point))
1571 (mime-preview-move-to-previous)
1573 (let ((f (assq (mime-preview-original-major-mode)
1574 mime-preview-over-to-previous-method-alist)))
1580 (defun mime-preview-move-to-next ()
1581 "Move to next entity.
1582 If there is no previous entity, it calls function registered in
1583 variable `mime-preview-over-to-next-method-alist'."
1585 (while (and (not (eobp))
1586 (null (get-text-property (point) 'mime-view-entity)))
1589 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1591 (<= point (point-max)))
1594 (if (null (get-text-property point 'mime-view-entity))
1595 (mime-preview-move-to-next)
1597 (or (eq mime-preview-move-scroll t)
1598 (and mime-preview-move-scroll
1601 (move-to-window-line -1)
1603 (* -1 next-screen-context-lines))
1606 (recenter next-screen-context-lines))
1608 (let ((f (assq (mime-preview-original-major-mode)
1609 mime-preview-over-to-next-method-alist)))
1615 (defun mime-preview-scroll-up-entity (&optional h)
1616 "Scroll up current entity.
1617 If reached to (point-max), it calls function registered in variable
1618 `mime-preview-over-to-next-method-alist'."
1621 (let ((f (assq (mime-preview-original-major-mode)
1622 mime-preview-over-to-next-method-alist)))
1627 (or (next-single-property-change (point) 'mime-view-entity)
1629 (bottom (window-end (selected-window))))
1632 (progn (goto-char point)
1633 (recenter next-screen-context-lines))
1637 (goto-char (point-max)))))
1640 (defun mime-preview-scroll-down-entity (&optional h)
1641 "Scroll down current entity.
1642 If reached to (point-min), it calls function registered in variable
1643 `mime-preview-over-to-previous-method-alist'."
1646 (let ((f (assq (mime-preview-original-major-mode)
1647 mime-preview-over-to-previous-method-alist)))
1652 (or (previous-single-property-change (point) 'mime-view-entity)
1654 (top (window-start (selected-window))))
1657 (progn (goto-char point)
1658 (recenter (* -1 next-screen-context-lines)))
1661 (beginning-of-buffer
1662 (goto-char (point-min)))))
1665 (defun mime-preview-next-line-entity (&optional lines)
1666 "Scroll up one line (or prefix LINES lines).
1667 If LINES is negative, scroll down LINES lines."
1669 (mime-preview-scroll-up-entity (or lines 1))
1672 (defun mime-preview-previous-line-entity (&optional lines)
1673 "Scrroll down one line (or prefix LINES lines).
1674 If LINES is negative, scroll up LINES lines."
1676 (mime-preview-scroll-down-entity (or lines 1))
1682 (defun mime-preview-quit ()
1683 "Quit from MIME-preview buffer.
1684 It calls function registered in variable
1685 `mime-preview-quitting-method-alist'."
1687 (let ((r (assq (mime-preview-original-major-mode)
1688 mime-preview-quitting-method-alist)))
1693 (defun mime-preview-kill-buffer ()
1695 (kill-buffer (current-buffer))
1702 (provide 'mime-view)
1704 (let* ((file mime-acting-situation-examples-file)
1705 (buffer (get-buffer-create " *mime-example*")))
1706 (if (file-readable-p file)
1711 (insert-file-contents file)
1716 (while (and (> (length mime-acting-situation-example-list)
1717 mime-acting-situation-example-list-max-size)
1719 (mime-reduce-acting-situation-examples)
1722 (error (setq mime-acting-situation-example-list nil)))
1724 (kill-buffer buffer))))
1726 ;;; mime-view.el ends here