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)))
74 ;;; @ in raw-buffer (representation space)
77 (defvar mime-preview-buffer nil
78 "MIME-preview buffer corresponding with the (raw) buffer.")
79 (make-variable-buffer-local 'mime-preview-buffer)
82 (defvar mime-raw-representation-type-alist
83 '((mime-show-message-mode . binary)
84 (mime-temp-message-mode . binary)
87 "Alist of major-mode vs. representation-type of mime-raw-buffer.
88 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
89 major-mode or t. t means default. REPRESENTATION-TYPE must be
90 `binary' or `cooked'.")
93 ;; (defun mime-raw-find-entity-from-point (point &optional message-info)
94 ;; "Return entity from POINT in mime-raw-buffer.
95 ;; If optional argument MESSAGE-INFO is not specified,
96 ;; `mime-message-structure' is used."
98 ;; (setq message-info mime-message-structure))
99 ;; (if (and (<= (mime-entity-point-min message-info) point)
100 ;; (<= point (mime-entity-point-max message-info)))
101 ;; (let ((children (mime-entity-children message-info)))
105 ;; (mime-raw-find-entity-from-point point (car children))))
109 ;; (setq children (cdr children)))
111 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
114 ;;; @ in preview-buffer (presentation space)
117 (defvar mime-mother-buffer nil
118 "Mother buffer corresponding with the (MIME-preview) buffer.
119 If current MIME-preview buffer is generated by other buffer, such as
120 message/partial, it is called `mother-buffer'.")
121 (make-variable-buffer-local 'mime-mother-buffer)
123 ;; (defvar mime-raw-buffer nil
124 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
125 ;; (make-variable-buffer-local 'mime-raw-buffer)
127 (defvar mime-preview-original-window-configuration nil
128 "Window-configuration before mime-view-mode is called.")
129 (make-variable-buffer-local 'mime-preview-original-window-configuration)
131 (defun mime-preview-original-major-mode (&optional recursive point)
132 "Return major-mode of original buffer.
133 If optional argument RECURSIVE is non-nil and current buffer has
134 mime-mother-buffer, it returns original major-mode of the
136 (if (and recursive mime-mother-buffer)
138 (set-buffer mime-mother-buffer)
139 (mime-preview-original-major-mode recursive)
141 (cdr (assq 'major-mode
142 (get-text-property (or point
143 (if (> (point) (buffer-size))
144 (max (1- (point-max)) (point-min))
146 'mime-view-situation)))))
149 ;;; @ entity information
152 (defun mime-entity-situation (entity &optional situation)
153 "Return situation of ENTITY."
154 (let (rest param name)
156 (unless (assq 'type situation)
157 (setq rest (or (mime-entity-content-type entity)
158 (make-mime-content-type 'text 'plain))
159 situation (cons (car rest) situation)
162 (unless (assq 'subtype situation)
164 (setq rest (or (cdr (mime-entity-content-type entity))
165 '((subtype . plain)))))
166 (setq situation (cons (car rest) situation)
170 (setq param (car rest))
171 (or (assoc (car param) situation)
172 (setq situation (cons param situation)))
173 (setq rest (cdr rest)))
175 ;; Content-Disposition
177 (unless (assq 'disposition-type situation)
178 (setq rest (mime-entity-content-disposition entity))
180 (setq situation (cons (cons 'disposition-type
181 (mime-content-disposition-type rest))
183 rest (mime-content-disposition-parameters rest))
186 (setq param (car rest)
188 (if (cond ((string= name "filename")
189 (if (assq 'filename situation)
191 (setq name 'filename)))
192 ((string= name "creation-date")
193 (if (assq 'creation-date situation)
195 (setq name 'creation-date)))
196 ((string= name "modification-date")
197 (if (assq 'modification-date situation)
199 (setq name 'modification-date)))
200 ((string= name "read-date")
201 (if (assq 'read-date situation)
203 (setq name 'read-date)))
204 ((string= name "size")
205 (if (assq 'size situation)
208 (t (setq name (cons 'disposition name))
209 (if (assoc name situation)
213 (cons (cons name (cdr param))
215 (setq rest (cdr rest)))
217 ;; Content-Transfer-Encoding
218 (or (assq 'encoding situation)
220 (cons (cons 'encoding (or (mime-entity-encoding entity)
226 (defsubst mime-delq-null-situation (situations field
227 &optional ignored-value)
230 (let* ((situation (car situations))
231 (cell (assq field situation)))
233 (or (eq (cdr cell) ignored-value)
234 (setq dest (cons situation dest))
236 (setq situations (cdr situations)))
239 (defun mime-compare-situation-with-example (situation example)
240 (let ((example (copy-alist example))
243 (let* ((cell (car situation))
245 (ecell (assoc key example)))
247 (if (equal cell ecell)
248 (setq match (1+ match))
249 (setq example (delq ecell example))
252 (setq situation (cdr situation))
257 (defun mime-sort-situation (situation)
267 (disposition-type . 6)
271 (let ((ret (assq a-t order)))
273 (setq a-order (cdr ret))
279 (let ((ret (assq b-t order)))
281 (setq b-order (cdr ret))
286 (if (= a-order b-order)
287 (string< (format "%s" a-t)(format "%s" b-t))
292 (defun mime-unify-situations (entity-situation
293 condition situation-examples
294 &optional required-name ignored-value)
296 (in-calist-package 'mime-view)
298 (ctree-find-calist condition entity-situation
299 mime-view-find-every-situations))
301 (setq ret (mime-delq-null-situation ret required-name ignored-value)))
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-preview-situation-example-list-max-size 16)
366 ;; (defvar mime-preview-situation-example-condition nil)
368 (defun mime-find-entity-preview-situation (entity
369 &optional default-situation)
371 (mime-unify-situations
372 (append (mime-entity-situation entity)
374 mime-preview-condition
375 mime-preview-situation-example-list)))
376 (setq mime-preview-situation-example-list
382 (defvar mime-acting-situation-example-list nil)
383 (defvar mime-acting-situation-example-list-max-size 16)
385 (defun mime-save-situation-examples ()
386 (if (or mime-preview-situation-example-list
387 mime-acting-situation-example-list)
388 (let* ((file mime-situation-examples-file)
389 (buffer (get-buffer-create " *mime-example*")))
393 (setq buffer-file-name file)
395 (insert ";;; " (file-name-nondirectory file) "\n")
396 (insert "\n;; This file is generated automatically by "
397 mime-view-version "\n\n")
398 (insert ";;; Code:\n\n")
399 (if mime-preview-situation-example-list
400 (pp `(setq mime-preview-situation-example-list
401 ',mime-preview-situation-example-list)
403 (if mime-acting-situation-example-list
404 (pp `(setq mime-acting-situation-example-list
405 ',mime-acting-situation-example-list)
408 (file-name-nondirectory file)
411 (kill-buffer buffer)))))
413 (add-hook 'kill-emacs-hook 'mime-save-situation-examples)
415 (defun mime-reduce-situation-examples (situation-examples)
416 (let ((len (length situation-examples))
424 ir situation-examples)
428 jr situation-examples)
432 (setq ret (mime-compare-situation-with-example (car ic)(car jc))
434 det-ret (+ (length (car ic))(length (car jc)))
435 det-org (length (cdr ret))
436 freq (+ (cdr ic)(cdr jc)))
437 (cond ((< max-sim sim)
444 dest (cons (cdr ret) freq))
447 (cond ((> min-det-ret det-ret)
448 (setq min-det-ret det-ret
453 dest (cons (cdr ret) freq))
455 ((= min-det-ret det-ret)
456 (cond ((> min-det-org det-org)
457 (setq min-det-org det-org
461 dest (cons (cdr ret) freq))
463 ((= min-det-org det-org)
464 (cond ((> min-freq freq)
468 dest (cons (cdr ret) freq))
482 (setq jr (nthcdr (1- d-j) situation-examples))
483 (setcdr jr (cddr jr))
485 (setq situation-examples
486 (cdr situation-examples))
487 (setq ir (nthcdr (1- d-i) situation-examples))
488 (setcdr ir (cddr ir))
490 (if (setq ir (assoc (car dest) situation-examples))
492 (setcdr ir (+ (cdr ir)(cdr dest)))
494 (cons dest situation-examples)
495 ;; situation-examples may be modified.
499 ;;; @ presentation of preview
505 ;;; @@@ predicate function
508 ;; (defun mime-view-entity-button-visible-p (entity)
509 ;; "Return non-nil if header of ENTITY is visible.
510 ;; Please redefine this function if you want to change default setting."
511 ;; (let ((media-type (mime-entity-media-type entity))
512 ;; (media-subtype (mime-entity-media-subtype entity)))
513 ;; (or (not (eq media-type 'application))
514 ;; (and (not (eq media-subtype 'x-selection))
515 ;; (or (not (eq media-subtype 'octet-stream))
516 ;; (let ((mother-entity (mime-entity-parent entity)))
517 ;; (or (not (eq (mime-entity-media-type mother-entity)
519 ;; (not (eq (mime-entity-media-subtype mother-entity)
524 ;;; @@@ entity button generator
527 (defun mime-view-insert-entity-button (entity)
528 "Insert entity-button of ENTITY."
529 (let ((entity-node-id (mime-entity-node-id entity))
530 (params (mime-entity-parameters entity))
531 (subject (mime-view-entity-title entity)))
533 (let ((access-type (assoc "access-type" params))
534 (num (or (cdr (assoc "x-part-number" params))
535 (if (consp entity-node-id)
538 (format "%s" (1+ num))
540 (reverse entity-node-id) ".")
544 (let ((server (assoc "server" params)))
545 (setq access-type (cdr access-type))
547 (format "%s %s ([%s] %s)"
548 num subject access-type (cdr server))
549 (let ((site (cdr (assoc "site" params)))
550 (dir (cdr (assoc "directory" params)))
551 (url (cdr (assoc "url" params)))
554 (format "%s %s ([%s] %s)"
555 num subject access-type url)
556 (format "%s %s ([%s] %s:%s)"
557 num subject access-type site dir))
561 (let ((media-type (mime-entity-media-type entity))
562 (media-subtype (mime-entity-media-subtype entity))
563 (charset (cdr (assoc "charset" params)))
564 (encoding (mime-entity-encoding entity)))
568 (format " <%s/%s%s%s>"
569 media-type media-subtype
571 (concat "; " charset)
574 (concat " (" encoding ")")
576 (if (>= (+ (current-column)(length rest))(window-width))
580 (function mime-preview-play-current-entity))
587 (defvar mime-header-presentation-method-alist nil
588 "Alist of major mode vs. corresponding header-presentation-method functions.
589 Each element looks like (SYMBOL . FUNCTION).
590 SYMBOL must be major mode in raw-buffer or t. t means default.
591 Interface of FUNCTION must be (ENTITY SITUATION).")
593 (defvar mime-view-ignored-field-list
594 '(".*Received:" ".*Path:" ".*Id:" "^References:"
595 "^Replied:" "^Errors-To:"
596 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
597 "^Content-Type:" "^Precedence:"
598 "^Status:" "^X-VM-.*:")
599 "All fields that match this list will be hidden in MIME preview buffer.
600 Each elements are regexp of field-name.")
602 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
603 "All fields that match this list will be displayed in MIME preview buffer.
604 Each elements are regexp of field-name.")
610 ;;; @@@ predicate function
613 (in-calist-package 'mime-view)
615 (defun mime-calist::field-match-method-as-default-rule (calist
616 field-type field-value)
617 (let ((s-field (assq field-type calist)))
618 (cond ((null s-field)
619 (cons (cons field-type field-value) calist)
623 (define-calist-field-match-method
624 'header #'mime-calist::field-match-method-as-default-rule)
626 (define-calist-field-match-method
627 'body #'mime-calist::field-match-method-as-default-rule)
630 (defvar mime-preview-condition nil
631 "Condition-tree about how to display entity.")
633 (ctree-set-calist-strictly
634 'mime-preview-condition '((type . application)(subtype . octet-stream)
637 (ctree-set-calist-strictly
638 'mime-preview-condition '((type . application)(subtype . octet-stream)
641 (ctree-set-calist-strictly
642 'mime-preview-condition '((type . application)(subtype . octet-stream)
646 (ctree-set-calist-strictly
647 'mime-preview-condition '((type . application)(subtype . pgp)
650 (ctree-set-calist-strictly
651 'mime-preview-condition '((type . application)(subtype . x-latex)
654 (ctree-set-calist-strictly
655 'mime-preview-condition '((type . application)(subtype . x-selection)
658 (ctree-set-calist-strictly
659 'mime-preview-condition '((type . application)(subtype . x-comment)
662 (ctree-set-calist-strictly
663 'mime-preview-condition '((type . message)(subtype . delivery-status)
666 (ctree-set-calist-strictly
667 'mime-preview-condition
669 (body-presentation-method . mime-display-text/plain)))
671 (ctree-set-calist-strictly
672 'mime-preview-condition
675 (body-presentation-method . mime-display-text/plain)))
677 (ctree-set-calist-strictly
678 'mime-preview-condition
679 '((type . text)(subtype . enriched)
681 (body-presentation-method . mime-display-text/enriched)))
683 (ctree-set-calist-strictly
684 'mime-preview-condition
685 '((type . text)(subtype . richtext)
687 (body-presentation-method . mime-display-text/richtext)))
689 (autoload 'mime-display-application/x-postpet "postpet")
691 (ctree-set-calist-strictly
692 'mime-preview-condition
693 '((type . application)(subtype . x-postpet)
695 (body-presentation-method . mime-display-application/x-postpet)))
697 (ctree-set-calist-strictly
698 'mime-preview-condition
699 '((type . text)(subtype . t)
701 (body-presentation-method . mime-display-text/plain)))
703 (ctree-set-calist-strictly
704 'mime-preview-condition
705 '((type . multipart)(subtype . alternative)
707 (body-presentation-method . mime-display-multipart/alternative)))
709 (ctree-set-calist-strictly
710 'mime-preview-condition '((type . message)(subtype . partial)
711 (body-presentation-method
712 . mime-display-message/partial-button)))
714 (ctree-set-calist-strictly
715 'mime-preview-condition '((type . message)(subtype . rfc822)
716 (body-presentation-method . nil)
717 (childrens-situation (header . visible)
718 (entity-button . invisible))))
720 (ctree-set-calist-strictly
721 'mime-preview-condition '((type . message)(subtype . news)
722 (body-presentation-method . nil)
723 (childrens-situation (header . visible)
724 (entity-button . invisible))))
727 ;;; @@@ entity presentation
730 (defun mime-display-text/plain (entity situation)
732 (narrow-to-region (point-max)(point-max))
733 (mime-insert-text-content entity)
734 (run-hooks 'mime-text-decode-hook)
735 (goto-char (point-max))
736 (if (not (eq (char-after (1- (point))) ?\n))
739 (mime-add-url-buttons)
740 (run-hooks 'mime-display-text/plain-hook)
743 (defun mime-display-text/richtext (entity situation)
745 (narrow-to-region (point-max)(point-max))
746 (mime-insert-text-content entity)
747 (run-hooks 'mime-text-decode-hook)
748 (let ((beg (point-min)))
749 (remove-text-properties beg (point-max) '(face nil))
750 (richtext-decode beg (point-max))
753 (defun mime-display-text/enriched (entity situation)
755 (narrow-to-region (point-max)(point-max))
756 (mime-insert-text-content entity)
757 (run-hooks 'mime-text-decode-hook)
758 (let ((beg (point-min)))
759 (remove-text-properties beg (point-max) '(face nil))
760 (enriched-decode beg (point-max))
764 (defvar mime-view-announcement-for-message/partial
765 (if (and (>= emacs-major-version 19) window-system)
767 \[[ This is message/partial style split message. ]]
768 \[[ Please press `v' key in this buffer ]]
769 \[[ or click here by mouse button-2. ]]"
771 \[[ This is message/partial style split message. ]]
772 \[[ Please press `v' key in this buffer. ]]"
775 (defun mime-display-message/partial-button (&optional entity situation)
777 (goto-char (point-max))
778 (if (not (search-backward "\n\n" nil t))
781 (goto-char (point-max))
782 (narrow-to-region (point-max)(point-max))
783 (insert mime-view-announcement-for-message/partial)
784 (mime-add-button (point-min)(point-max)
785 #'mime-preview-play-current-entity)
788 (defun mime-display-multipart/mixed (entity situation)
789 (let ((children (mime-entity-children entity))
790 (original-major-mode-cell (assq 'major-mode situation))
792 (cdr (assq 'childrens-situation situation))))
793 (if original-major-mode-cell
794 (setq default-situation
795 (cons original-major-mode-cell default-situation)))
797 (mime-display-entity (car children) nil default-situation)
798 (setq children (cdr children))
801 (defcustom mime-view-type-subtype-score-alist
802 '(((text . enriched) . 3)
803 ((text . richtext) . 2)
806 "Alist MEDIA-TYPE vs corresponding score.
807 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
809 :type '(repeat (cons (choice :tag "Media-Type"
810 (cons :tag "Type/Subtype"
811 (symbol :tag "Primary-type")
812 (symbol :tag "Subtype"))
814 (const :tag "Default" t))
817 (defun mime-display-multipart/alternative (entity situation)
818 (let* ((children (mime-entity-children entity))
819 (original-major-mode-cell (assq 'major-mode situation))
821 (cdr (assq 'childrens-situation situation)))
826 (if original-major-mode-cell
827 (setq default-situation
828 (cons original-major-mode-cell default-situation)))
833 (mime-find-entity-preview-situation
834 child default-situation)))
835 (if (cdr (assq 'body-presentation-method situation))
840 (cdr (assq 'type situation))
841 (cdr (assq 'subtype situation)))
842 mime-view-type-subtype-score-alist)
844 (cdr (assq 'type situation))
845 mime-view-type-subtype-score-alist)
848 mime-view-type-subtype-score-alist)
850 (if (> score max-score)
860 (let ((child (car children))
861 (situation (car situations)))
862 (mime-display-entity child (if (= i p)
864 (del-alist 'body-presentation-method
865 (copy-alist situation))))
867 (setq children (cdr children)
868 situations (cdr situations)
873 ;;; @ acting-condition
876 (defvar mime-acting-condition nil
877 "Condition-tree about how to process entity.")
879 (if (file-readable-p mailcap-file)
880 (let ((entries (mailcap-parse-file)))
882 (let ((entry (car entries))
885 (let* ((field (car entry))
886 (field-type (car field)))
887 (cond ((eq field-type 'view) (setq view field))
888 ((eq field-type 'print) (setq print field))
889 ((memq field-type '(compose composetyped edit)))
890 (t (setq shared (cons field shared))))
892 (setq entry (cdr entry))
894 (setq shared (nreverse shared))
895 (ctree-set-calist-with-default
896 'mime-acting-condition
897 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
899 (ctree-set-calist-with-default
900 'mime-acting-condition
902 (list '(mode . "print")(cons 'method (cdr view))))
905 (setq entries (cdr entries))
908 (ctree-set-calist-strictly
909 'mime-acting-condition
910 '((type . application)(subtype . octet-stream)
912 (method . mime-detect-content)
915 (ctree-set-calist-with-default
916 'mime-acting-condition
918 (method . mime-save-content)))
920 (ctree-set-calist-strictly
921 'mime-acting-condition
922 '((type . text)(subtype . x-rot13-47)(mode . "play")
923 (method . mime-view-caesar)
925 (ctree-set-calist-strictly
926 'mime-acting-condition
927 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
928 (method . mime-view-caesar)
931 (ctree-set-calist-strictly
932 'mime-acting-condition
933 '((type . message)(subtype . rfc822)(mode . "play")
934 (method . mime-view-message/rfc822)
936 (ctree-set-calist-strictly
937 'mime-acting-condition
938 '((type . message)(subtype . partial)(mode . "play")
939 (method . mime-store-message/partial-piece)
942 (ctree-set-calist-strictly
943 'mime-acting-condition
944 '((type . message)(subtype . external-body)
945 ("access-type" . "anon-ftp")
946 (method . mime-view-message/external-anon-ftp)
949 (ctree-set-calist-strictly
950 'mime-acting-condition
951 '((type . message)(subtype . external-body)
952 ("access-type" . "url")
953 (method . mime-view-message/external-url)
956 (ctree-set-calist-strictly
957 'mime-acting-condition
958 '((type . application)(subtype . octet-stream)
959 (method . mime-save-content)
963 ;;; @ quitting method
966 (defvar mime-preview-quitting-method-alist
967 '((mime-show-message-mode
968 . mime-preview-quitting-method-for-mime-show-message-mode))
969 "Alist of major-mode vs. quitting-method of mime-view.")
971 (defvar mime-preview-over-to-previous-method-alist nil
972 "Alist of major-mode vs. over-to-previous-method of mime-view.")
974 (defvar mime-preview-over-to-next-method-alist nil
975 "Alist of major-mode vs. over-to-next-method of mime-view.")
978 ;;; @ following method
981 (defvar mime-preview-following-method-alist nil
982 "Alist of major-mode vs. following-method of mime-view.")
984 (defvar mime-view-following-required-fields-list
991 (defun mime-display-entity (entity &optional situation
992 default-situation preview-buffer)
994 (setq preview-buffer (current-buffer)))
995 (let* (e nb ne nhb nbb)
996 (mime-goto-header-start-point entity)
997 (in-calist-package 'mime-view)
1000 (mime-find-entity-preview-situation entity default-situation)))
1001 (let ((button-is-invisible
1002 (eq (cdr (or (assq '*entity-button situation)
1003 (assq 'entity-button situation)))
1006 (eq (cdr (or (assq '*header situation)
1007 (assq 'header situation)))
1009 (header-presentation-method
1010 (or (cdr (assq 'header-presentation-method situation))
1011 (cdr (assq (cdr (assq 'major-mode situation))
1012 mime-header-presentation-method-alist))))
1013 (body-presentation-method
1014 (cdr (assq 'body-presentation-method situation)))
1015 (children (mime-entity-children entity)))
1016 (set-buffer preview-buffer)
1018 (narrow-to-region nb nb)
1019 (or button-is-invisible
1020 ;; (if (mime-view-entity-button-visible-p entity)
1021 (mime-view-insert-entity-button entity)
1024 (when header-is-visible
1026 (if header-presentation-method
1027 (funcall header-presentation-method entity situation)
1028 (mime-insert-header entity
1029 mime-view-ignored-field-list
1030 mime-view-visible-field-list))
1031 (run-hooks 'mime-display-header-hook)
1032 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1033 (goto-char (point-max))
1038 ((functionp body-presentation-method)
1039 (funcall body-presentation-method entity situation)
1042 (when button-is-invisible
1043 (goto-char (point-max))
1044 (mime-view-insert-entity-button entity)
1046 (or header-is-visible
1048 (goto-char (point-max))
1052 (setq ne (point-max))
1054 (put-text-property nb ne 'mime-view-entity entity)
1055 (put-text-property nb ne 'mime-view-situation situation)
1056 (put-text-property nbb ne 'mime-view-entity-body entity)
1059 (if (functionp body-presentation-method)
1060 (funcall body-presentation-method entity situation)
1061 (mime-display-multipart/mixed entity situation)
1066 ;;; @ MIME viewer mode
1069 (defconst mime-view-menu-title "MIME-View")
1070 (defconst mime-view-menu-list
1071 '((up "Move to upper entity" mime-preview-move-to-upper)
1072 (previous "Move to previous entity" mime-preview-move-to-previous)
1073 (next "Move to next entity" mime-preview-move-to-next)
1074 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
1075 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
1076 (play "Play current entity" mime-preview-play-current-entity)
1077 (extract "Extract current entity" mime-preview-extract-current-entity)
1078 (print "Print current entity" mime-preview-print-current-entity)
1080 "Menu for MIME Viewer")
1082 (cond ((featurep 'xemacs)
1083 (defvar mime-view-xemacs-popup-menu
1084 (cons mime-view-menu-title
1087 (vector (nth 1 item)(nth 2 item) t)
1089 mime-view-menu-list)))
1090 (defun mime-view-xemacs-popup-menu (event)
1091 "Popup the menu in the MIME Viewer buffer"
1093 (select-window (event-window event))
1094 (set-buffer (event-buffer event))
1095 (popup-menu 'mime-view-xemacs-popup-menu))
1096 (defvar mouse-button-2 'button2)
1099 (defvar mime-view-popup-menu
1100 (let ((menu (make-sparse-keymap mime-view-menu-title)))
1104 (list (intern (nth 1 item)) 'menu-item
1105 (nth 1 item)(nth 2 item))
1107 mime-view-menu-list))))
1108 (defun mime-view-popup-menu (event)
1109 "Popup the menu in the MIME Viewer buffer"
1111 (let ((menu mime-view-popup-menu) events func)
1112 (setq events (x-popup-menu t menu))
1114 (setq func (lookup-key menu (apply #'vector events)))
1117 (defvar mouse-button-2 [mouse-2])
1120 (defun mime-view-define-keymap (&optional default)
1121 (let ((mime-view-mode-map (if (keymapp default)
1122 (copy-keymap default)
1123 (make-sparse-keymap)
1125 (define-key mime-view-mode-map
1126 "u" (function mime-preview-move-to-upper))
1127 (define-key mime-view-mode-map
1128 "p" (function mime-preview-move-to-previous))
1129 (define-key mime-view-mode-map
1130 "n" (function mime-preview-move-to-next))
1131 (define-key mime-view-mode-map
1132 "\e\t" (function mime-preview-move-to-previous))
1133 (define-key mime-view-mode-map
1134 "\t" (function mime-preview-move-to-next))
1135 (define-key mime-view-mode-map
1136 " " (function mime-preview-scroll-up-entity))
1137 (define-key mime-view-mode-map
1138 "\M- " (function mime-preview-scroll-down-entity))
1139 (define-key mime-view-mode-map
1140 "\177" (function mime-preview-scroll-down-entity))
1141 (define-key mime-view-mode-map
1142 "\C-m" (function mime-preview-next-line-entity))
1143 (define-key mime-view-mode-map
1144 "\C-\M-m" (function mime-preview-previous-line-entity))
1145 (define-key mime-view-mode-map
1146 "v" (function mime-preview-play-current-entity))
1147 (define-key mime-view-mode-map
1148 "e" (function mime-preview-extract-current-entity))
1149 (define-key mime-view-mode-map
1150 "\C-c\C-p" (function mime-preview-print-current-entity))
1151 (define-key mime-view-mode-map
1152 "\C-c\C-t\C-h" (function mime-preview-toggle-header))
1153 (define-key mime-view-mode-map
1154 "a" (function mime-preview-follow-current-entity))
1155 (define-key mime-view-mode-map
1156 "q" (function mime-preview-quit))
1157 (define-key mime-view-mode-map
1158 "\C-c\C-x" (function mime-preview-kill-buffer))
1159 ;; (define-key mime-view-mode-map
1160 ;; "<" (function beginning-of-buffer))
1161 ;; (define-key mime-view-mode-map
1162 ;; ">" (function end-of-buffer))
1163 (define-key mime-view-mode-map
1164 "?" (function describe-mode))
1165 (define-key mime-view-mode-map
1166 [tab] (function mime-preview-move-to-next))
1167 (define-key mime-view-mode-map
1168 [delete] (function mime-preview-scroll-down-entity))
1169 (define-key mime-view-mode-map
1170 [backspace] (function mime-preview-scroll-down-entity))
1171 (if (functionp default)
1172 (cond ((featurep 'xemacs)
1173 (set-keymap-default-binding mime-view-mode-map default)
1176 (setq mime-view-mode-map
1177 (append mime-view-mode-map (list (cons t default))))
1180 (define-key mime-view-mode-map
1181 mouse-button-2 (function mime-button-dispatcher))
1183 (cond ((featurep 'xemacs)
1184 (define-key mime-view-mode-map
1185 mouse-button-3 (function mime-view-xemacs-popup-menu))
1187 ((>= emacs-major-version 19)
1188 (define-key mime-view-mode-map
1189 mouse-button-3 (function mime-view-popup-menu))
1190 (define-key mime-view-mode-map [menu-bar mime-view]
1191 (cons mime-view-menu-title
1192 (make-sparse-keymap mime-view-menu-title)))
1195 (define-key mime-view-mode-map
1196 (vector 'menu-bar 'mime-view (car item))
1197 (cons (nth 1 item)(nth 2 item))
1200 (reverse mime-view-menu-list)
1203 (use-local-map mime-view-mode-map)
1204 (run-hooks 'mime-view-define-keymap-hook)
1207 (defsubst mime-maybe-hide-echo-buffer ()
1208 "Clear mime-echo buffer and delete window for it."
1209 (let ((buf (get-buffer mime-echo-buffer-name)))
1214 (let ((win (get-buffer-window buf)))
1221 (defvar mime-view-redisplay nil)
1224 (defun mime-display-message (message &optional preview-buffer
1225 mother default-keymap-or-function
1226 original-major-mode)
1227 "View MESSAGE in MIME-View mode.
1229 Optional argument PREVIEW-BUFFER specifies the buffer of the
1230 presentation. It must be either nil or a name of preview buffer.
1232 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1234 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1235 function. If it is a keymap, keymap of MIME-View mode will be added
1236 to it. If it is a function, it will be bound as default binding of
1237 keymap of MIME-View mode."
1238 (mime-maybe-hide-echo-buffer)
1239 (let ((win-conf (current-window-configuration)))
1241 (setq preview-buffer
1242 (concat "*Preview-" (mime-entity-name message) "*")))
1243 (or original-major-mode
1244 (setq original-major-mode
1245 (with-current-buffer (mime-entity-header-buffer message)
1247 (let ((inhibit-read-only t))
1248 (set-buffer (get-buffer-create preview-buffer))
1252 (setq mime-mother-buffer mother)
1254 (setq mime-preview-original-window-configuration win-conf)
1255 (setq major-mode 'mime-view-mode)
1256 (setq mode-name "MIME-View")
1257 (mime-display-entity message nil
1258 `((entity-button . invisible)
1260 (major-mode . ,original-major-mode))
1262 (mime-view-define-keymap default-keymap-or-function)
1264 (next-single-property-change (point-min) 'mime-view-entity)))
1267 (goto-char (point-min))
1268 (search-forward "\n\n" nil t)
1270 (run-hooks 'mime-view-mode-hook)
1271 (set-buffer-modified-p nil)
1272 (setq buffer-read-only t)
1276 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1277 default-keymap-or-function
1278 representation-type)
1279 "View RAW-BUFFER in MIME-View mode.
1280 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1282 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1283 function. If it is a keymap, keymap of MIME-View mode will be added
1284 to it. If it is a function, it will be bound as default binding of
1285 keymap of MIME-View mode.
1286 Optional argument REPRESENTATION-TYPE is representation-type of
1287 message. It must be nil, `binary' or `cooked'. If it is nil,
1288 `cooked' is used as default."
1291 (setq raw-buffer (current-buffer)))
1292 (or representation-type
1293 (setq representation-type
1295 (set-buffer raw-buffer)
1296 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1297 (assq t mime-raw-representation-type-alist)))
1299 (if (eq representation-type 'binary)
1300 (setq representation-type 'buffer)
1302 (setq preview-buffer (mime-display-message
1303 (mime-open-entity representation-type raw-buffer)
1304 preview-buffer mother default-keymap-or-function))
1305 (or (get-buffer-window preview-buffer)
1306 (let ((r-win (get-buffer-window raw-buffer)))
1308 (set-window-buffer r-win preview-buffer)
1309 (let ((m-win (and mother (get-buffer-window mother))))
1311 (set-window-buffer m-win preview-buffer)
1312 (switch-to-buffer preview-buffer)
1315 (defun mime-view-mode (&optional mother ctl encoding
1316 raw-buffer preview-buffer
1317 default-keymap-or-function)
1318 "Major mode for viewing MIME message.
1320 Here is a list of the standard keys for mime-view-mode.
1325 u Move to upper content
1326 p or M-TAB Move to previous content
1327 n or TAB Move to next content
1328 SPC Scroll up or move to next content
1329 M-SPC or DEL Scroll down or move to previous content
1330 RET Move to next line
1331 M-RET Move to previous line
1332 v Decode current content as `play mode'
1333 e Decode current content as `extract mode'
1334 C-c C-p Decode current content as `print mode'
1335 a Followup to current content.
1337 button-2 Move to point under the mouse cursor
1338 and decode current content as `play mode'
1341 (unless mime-view-redisplay
1343 (if raw-buffer (set-buffer raw-buffer))
1346 (or (assq major-mode mime-raw-representation-type-alist)
1347 (assq t mime-raw-representation-type-alist)))))
1348 (if (eq type 'binary)
1351 (setq mime-message-structure (mime-open-entity type raw-buffer))
1352 (or (mime-entity-content-type mime-message-structure)
1353 (mime-entity-set-content-type-internal
1354 mime-message-structure ctl))
1356 (or (mime-entity-encoding mime-message-structure)
1357 (mime-entity-set-encoding-internal mime-message-structure encoding))
1359 (mime-display-message mime-message-structure preview-buffer
1360 mother default-keymap-or-function)
1367 (defun mime-preview-find-boundary-info (&optional get-mother)
1371 (while (null (setq entity
1372 (get-text-property (point) 'mime-view-entity)))
1374 (setq p-beg (previous-single-property-change (point) 'mime-view-entity))
1375 (setq entity-node-id (mime-entity-node-id entity))
1376 (setq len (length entity-node-id))
1379 (if (eq (next-single-property-change (point-min)
1385 ((eq (next-single-property-change p-beg 'mime-view-entity)
1387 (setq p-beg (point))
1389 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1391 (setq p-end (point-max))
1393 ((null entity-node-id)
1394 (setq p-end (point-max))
1402 (next-single-property-change
1403 (point) 'mime-view-entity))
1405 (let ((rc (mime-entity-node-id
1406 (get-text-property (point)
1407 'mime-view-entity))))
1408 (or (equal entity-node-id
1409 (nthcdr (- (length rc) len) rc))
1412 (setq p-end (point-max))))
1414 (vector p-beg p-end entity)))
1420 (autoload 'mime-preview-play-current-entity "mime-play"
1421 "Play current entity." t)
1423 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1424 "Extract current entity into file (maybe).
1425 It decodes current entity to call internal or external method as
1426 \"extract\" mode. The method is selected from variable
1427 `mime-acting-condition'."
1429 (mime-preview-play-current-entity ignore-examples "extract")
1432 (defun mime-preview-print-current-entity (&optional ignore-examples)
1433 "Print current entity (maybe).
1434 It decodes current entity to call internal or external method as
1435 \"print\" mode. The method is selected from variable
1436 `mime-acting-condition'."
1438 (mime-preview-play-current-entity ignore-examples "print")
1445 (defun mime-preview-follow-current-entity ()
1446 "Write follow message to current entity.
1447 It calls following-method selected from variable
1448 `mime-preview-following-method-alist'."
1450 (let ((entity (mime-preview-find-boundary-info t))
1453 (setq p-beg (aref entity 0)
1454 p-end (aref entity 1)
1455 entity (aref entity 2))
1457 (next-single-property-change p-beg 'mime-view-entity-header))
1458 (if (or (null ph-end)
1460 (setq ph-end p-end))
1461 (let* ((mode (mime-preview-original-major-mode 'recursive))
1462 (entity-node-id (mime-entity-node-id entity))
1464 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1466 (the-buf (current-buffer))
1469 (set-buffer (setq new-buf (get-buffer-create new-name)))
1471 (insert-buffer-substring the-buf ph-end p-end)
1472 (when (= ph-end p-beg)
1473 (goto-char (point-min))
1475 (goto-char (point-min))
1476 (let ((current-entity
1477 (if (and (eq (mime-entity-media-type entity) 'message)
1478 (eq (mime-entity-media-subtype entity) 'rfc822))
1479 (car (mime-entity-children entity))
1482 (while (and current-entity
1485 (with-current-buffer
1486 (mime-entity-header-buffer current-entity)
1489 (mime-entity-header-start-point
1491 (mime-entity-header-end-point
1493 (std11-header-string-except
1496 (apply (function regexp-or) fields)
1498 (if (and (eq (mime-entity-media-type
1499 current-entity) 'message)
1500 (eq (mime-entity-media-subtype
1501 current-entity) 'rfc822))
1507 (setq fields (std11-collect-field-names)
1508 current-entity (mime-entity-parent current-entity))
1511 (let ((rest mime-view-following-required-fields-list)
1514 (setq field-name (car rest))
1515 (or (std11-field-body field-name)
1518 (set-buffer the-buf)
1519 (let ((entity (when mime-mother-buffer
1520 (set-buffer mime-mother-buffer)
1521 (get-text-property (point)
1522 'mime-view-entity))))
1524 (null (setq ret (mime-entity-fetch-field
1525 entity field-name))))
1526 (setq entity (mime-entity-parent entity)))))
1528 (insert (concat field-name ": " ret "\n"))
1530 (setq rest (cdr rest))
1532 (mime-decode-header-in-buffer)
1534 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1539 "Sorry, following method for %s is not implemented yet."
1548 (defun mime-preview-move-to-upper ()
1549 "Move to upper entity.
1550 If there is no upper entity, call function `mime-preview-quit'."
1553 (while (null (setq cinfo
1554 (get-text-property (point) 'mime-view-entity)))
1557 (let ((r (mime-entity-parent cinfo))
1560 (while (setq point (previous-single-property-change
1561 (point) 'mime-view-entity))
1563 (when (eq r (get-text-property (point) 'mime-view-entity))
1564 (if (or (eq mime-preview-move-scroll t)
1565 (and mime-preview-move-scroll
1568 (move-to-window-line -1)
1569 (forward-line (* -1 next-screen-context-lines))
1572 (recenter next-screen-context-lines))
1579 (defun mime-preview-move-to-previous ()
1580 "Move to previous entity.
1581 If there is no previous entity, it calls function registered in
1582 variable `mime-preview-over-to-previous-method-alist'."
1584 (while (and (not (bobp))
1585 (null (get-text-property (point) 'mime-view-entity)))
1588 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1590 (>= point (point-min)))
1591 (if (get-text-property (1- point) 'mime-view-entity)
1592 (progn (goto-char point)
1594 (or (eq mime-preview-move-scroll t)
1595 (and mime-preview-move-scroll
1598 (move-to-window-line 0)
1599 (forward-line next-screen-context-lines)
1602 (recenter (* -1 next-screen-context-lines))))
1603 (goto-char (1- point))
1604 (mime-preview-move-to-previous)
1606 (let ((f (assq (mime-preview-original-major-mode)
1607 mime-preview-over-to-previous-method-alist)))
1613 (defun mime-preview-move-to-next ()
1614 "Move to next entity.
1615 If there is no previous entity, it calls function registered in
1616 variable `mime-preview-over-to-next-method-alist'."
1618 (while (and (not (eobp))
1619 (null (get-text-property (point) 'mime-view-entity)))
1622 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1624 (<= point (point-max)))
1627 (if (null (get-text-property point 'mime-view-entity))
1628 (mime-preview-move-to-next)
1630 (or (eq mime-preview-move-scroll t)
1631 (and mime-preview-move-scroll
1634 (move-to-window-line -1)
1636 (* -1 next-screen-context-lines))
1639 (recenter next-screen-context-lines))
1641 (let ((f (assq (mime-preview-original-major-mode)
1642 mime-preview-over-to-next-method-alist)))
1648 (defun mime-preview-scroll-up-entity (&optional h)
1649 "Scroll up current entity.
1650 If reached to (point-max), it calls function registered in variable
1651 `mime-preview-over-to-next-method-alist'."
1654 (let ((f (assq (mime-preview-original-major-mode)
1655 mime-preview-over-to-next-method-alist)))
1660 (or (next-single-property-change (point) 'mime-view-entity)
1662 (bottom (window-end (selected-window))))
1665 (progn (goto-char point)
1666 (recenter next-screen-context-lines))
1670 (goto-char (point-max)))))
1673 (defun mime-preview-scroll-down-entity (&optional h)
1674 "Scroll down current entity.
1675 If reached to (point-min), it calls function registered in variable
1676 `mime-preview-over-to-previous-method-alist'."
1679 (let ((f (assq (mime-preview-original-major-mode)
1680 mime-preview-over-to-previous-method-alist)))
1685 (or (previous-single-property-change (point) 'mime-view-entity)
1687 (top (window-start (selected-window))))
1690 (progn (goto-char point)
1691 (recenter (* -1 next-screen-context-lines)))
1694 (beginning-of-buffer
1695 (goto-char (point-min)))))
1698 (defun mime-preview-next-line-entity (&optional lines)
1699 "Scroll up one line (or prefix LINES lines).
1700 If LINES is negative, scroll down LINES lines."
1702 (mime-preview-scroll-up-entity (or lines 1))
1705 (defun mime-preview-previous-line-entity (&optional lines)
1706 "Scrroll down one line (or prefix LINES lines).
1707 If LINES is negative, scroll up LINES lines."
1709 (mime-preview-scroll-down-entity (or lines 1))
1716 (defun mime-preview-toggle-header ()
1718 (let ((situation (mime-preview-find-boundary-info))
1720 (setq p-beg (aref situation 0)
1721 p-end (aref situation 1)
1722 entity (aref situation 2)
1723 situation (get-text-property p-beg 'mime-view-situation))
1724 (let ((cell (assq '*header situation)))
1726 (setq cell (assq 'header situation)))
1727 (if (eq (cdr cell) 'visible)
1728 (setq situation (put-alist '*header 'invisible situation))
1729 (setq situation (put-alist '*header 'visible situation))))
1731 (let ((inhibit-read-only t))
1732 (delete-region p-beg p-end)
1733 (mime-display-entity entity situation)))
1734 ;; (ctree-set-calist-strictly 'mime-preview-condition situation)
1735 (let ((ret (assoc situation mime-preview-situation-example-list)))
1737 (setcdr ret (1+ (cdr ret)))
1738 (add-to-list 'mime-preview-situation-example-list
1739 (cons situation 0))))))
1745 (defun mime-preview-quit ()
1746 "Quit from MIME-preview buffer.
1747 It calls function registered in variable
1748 `mime-preview-quitting-method-alist'."
1750 (let ((r (assq (mime-preview-original-major-mode)
1751 mime-preview-quitting-method-alist)))
1756 (defun mime-preview-kill-buffer ()
1758 (kill-buffer (current-buffer))
1765 (provide 'mime-view)
1767 (let* ((file mime-situation-examples-file)
1768 (buffer (get-buffer-create " *mime-example*")))
1769 (if (file-readable-p file)
1774 (insert-file-contents file)
1779 (while (and (> (length mime-preview-situation-example-list)
1780 mime-preview-situation-example-list-max-size)
1782 (setq mime-preview-situation-example-list
1783 (mime-reduce-situation-examples
1784 mime-preview-situation-example-list))
1787 (error (setq mime-preview-situation-example-list nil)))
1788 ;; (let ((rest mime-preview-situation-example-list))
1790 ;; (ctree-set-calist-strictly 'mime-preview-condition
1792 ;; (setq rest (cdr rest))))
1795 (while (and (> (length mime-acting-situation-example-list)
1796 mime-acting-situation-example-list-max-size)
1798 (setq mime-acting-situation-example-list
1799 (mime-reduce-situation-examples
1800 mime-acting-situation-example-list))
1803 (error (setq mime-acting-situation-example-list nil)))
1805 (kill-buffer buffer))))
1807 ;;; mime-view.el ends here