1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
3 ;; Copyright (C) 1995,96,97,98,99,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 ;;; @ in preview-buffer (presentation space)
96 (defvar mime-mother-buffer nil
97 "Mother buffer corresponding with the (MIME-preview) buffer.
98 If current MIME-preview buffer is generated by other buffer, such as
99 message/partial, it is called `mother-buffer'.")
100 (make-variable-buffer-local 'mime-mother-buffer)
102 ;; (defvar mime-raw-buffer nil
103 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
104 ;; (make-variable-buffer-local 'mime-raw-buffer)
106 (defvar mime-preview-original-window-configuration nil
107 "Window-configuration before mime-view-mode is called.")
108 (make-variable-buffer-local 'mime-preview-original-window-configuration)
110 (defun mime-preview-original-major-mode (&optional recursive point)
111 "Return major-mode of original buffer.
112 If optional argument RECURSIVE is non-nil and current buffer has
113 mime-mother-buffer, it returns original major-mode of the
115 (if (and recursive mime-mother-buffer)
117 (set-buffer mime-mother-buffer)
118 (mime-preview-original-major-mode recursive)
120 (cdr (assq 'major-mode
121 (get-text-property (or point
122 (if (> (point) (buffer-size))
123 (max (1- (point-max)) (point-min))
125 'mime-view-situation)))))
128 ;;; @ entity information
131 (defun mime-entity-situation (entity &optional situation)
132 "Return situation of ENTITY."
133 (let (rest param name)
135 (unless (assq 'type situation)
136 (setq rest (or (mime-entity-content-type entity)
137 (make-mime-content-type 'text 'plain))
138 situation (cons (car rest) situation)
141 (unless (assq 'subtype situation)
143 (setq rest (or (cdr (mime-entity-content-type entity))
144 '((subtype . plain)))))
145 (setq situation (cons (car rest) situation)
149 (setq param (car rest))
150 (or (assoc (car param) situation)
151 (setq situation (cons param situation)))
152 (setq rest (cdr rest)))
154 ;; Content-Disposition
156 (unless (assq 'disposition-type situation)
157 (setq rest (mime-entity-content-disposition entity))
159 (setq situation (cons (cons 'disposition-type
160 (mime-content-disposition-type rest))
162 rest (mime-content-disposition-parameters rest))
165 (setq param (car rest)
167 (if (cond ((string= name "filename")
168 (if (assq 'filename situation)
170 (setq name 'filename)))
171 ((string= name "creation-date")
172 (if (assq 'creation-date situation)
174 (setq name 'creation-date)))
175 ((string= name "modification-date")
176 (if (assq 'modification-date situation)
178 (setq name 'modification-date)))
179 ((string= name "read-date")
180 (if (assq 'read-date situation)
182 (setq name 'read-date)))
183 ((string= name "size")
184 (if (assq 'size situation)
187 (t (setq name (cons 'disposition name))
188 (if (assoc name situation)
192 (cons (cons name (cdr param))
194 (setq rest (cdr rest)))
196 ;; Content-Transfer-Encoding
197 (or (assq 'encoding situation)
199 (cons (cons 'encoding (or (mime-entity-encoding entity)
205 (defsubst mime-delq-null-situation (situations field
206 &optional ignored-value)
209 (let* ((situation (car situations))
210 (cell (assq field situation)))
212 (or (eq (cdr cell) ignored-value)
213 (setq dest (cons situation dest))
215 (setq situations (cdr situations)))
218 (defun mime-compare-situation-with-example (situation example)
219 (let ((example (copy-alist example))
222 (let* ((cell (car situation))
224 (ecell (assoc key example)))
226 (if (equal cell ecell)
227 (setq match (1+ match))
228 (setq example (delq ecell example))
231 (setq situation (cdr situation))
236 (defun mime-sort-situation (situation)
246 (disposition-type . 6)
250 (let ((ret (assq a-t order)))
252 (setq a-order (cdr ret))
258 (let ((ret (assq b-t order)))
260 (setq b-order (cdr ret))
265 (if (= a-order b-order)
266 (string< (format "%s" a-t)(format "%s" b-t))
271 (defun mime-unify-situations (entity-situation
272 condition situation-examples
273 &optional required-name ignored-value
276 (in-calist-package 'mime-view)
278 (ctree-find-calist condition entity-situation
281 (setq ret (mime-delq-null-situation ret required-name ignored-value)))
282 (or (assq 'ignore-examples entity-situation)
290 (let ((situation (car rest))
291 (examples situation-examples))
294 (mime-compare-situation-with-example
295 situation (caar examples)))
296 (ret-score (car ret)))
297 (cond ((> ret-score max-score)
298 (setq max-score ret-score
299 max-escore (cdar examples)
300 max-examples (list (cdr ret))
301 max-situations (list situation))
303 ((= ret-score max-score)
304 (cond ((> (cdar examples) max-escore)
305 (setq max-escore (cdar examples)
306 max-examples (list (cdr ret))
307 max-situations (list situation))
309 ((= (cdar examples) max-escore)
311 (cons (cdr ret) max-examples))
312 (or (member situation max-situations)
314 (cons situation max-situations)))
316 (setq examples (cdr examples))))
317 (setq rest (cdr rest)))
319 (setq ret max-situations)
321 (let* ((example (car max-examples))
323 (assoc example situation-examples)))
325 (setcdr cell (1+ (cdr cell)))
326 (setq situation-examples
327 (cons (cons example 0)
330 (setq max-examples (cdr max-examples))
332 (cons ret situation-examples)
333 ;; ret: list of situations
334 ;; situation-examples: new examples (notoce that contents of
335 ;; argument `situation-examples' has bees modified)
338 (defun mime-view-entity-title (entity)
339 (or (mime-entity-read-field entity 'Content-Description)
340 (mime-entity-read-field entity 'Subject)
341 (mime-entity-filename entity)
344 (defvar mime-preview-situation-example-list nil)
345 (defvar mime-preview-situation-example-list-max-size 16)
346 ;; (defvar mime-preview-situation-example-condition nil)
348 (defun mime-find-entity-preview-situation (entity
349 &optional default-situation)
351 (mime-unify-situations
352 (append (mime-entity-situation entity)
354 mime-preview-condition
355 mime-preview-situation-example-list)))
356 (setq mime-preview-situation-example-list
362 (defvar mime-acting-situation-example-list nil)
363 (defvar mime-acting-situation-example-list-max-size 16)
365 (defun mime-save-situation-examples ()
366 (if (or mime-preview-situation-example-list
367 mime-acting-situation-example-list)
368 (let* ((file mime-situation-examples-file)
369 (buffer (get-buffer-create " *mime-example*")))
373 (setq buffer-file-name file)
375 (insert ";;; " (file-name-nondirectory file) "\n")
376 (insert "\n;; This file is generated automatically by "
377 mime-view-version "\n\n")
378 (insert ";;; Code:\n\n")
379 (if mime-preview-situation-example-list
380 (pp `(setq mime-preview-situation-example-list
381 ',mime-preview-situation-example-list)
383 (if mime-acting-situation-example-list
384 (pp `(setq mime-acting-situation-example-list
385 ',mime-acting-situation-example-list)
388 (file-name-nondirectory file)
391 (kill-buffer buffer)))))
393 (add-hook 'kill-emacs-hook 'mime-save-situation-examples)
395 (defun mime-reduce-situation-examples (situation-examples)
396 (let ((len (length situation-examples))
404 ir situation-examples)
408 jr situation-examples)
412 (setq ret (mime-compare-situation-with-example (car ic)(car jc))
414 det-ret (+ (length (car ic))(length (car jc)))
415 det-org (length (cdr ret))
416 freq (+ (cdr ic)(cdr jc)))
417 (cond ((< max-sim sim)
424 dest (cons (cdr ret) freq))
427 (cond ((> min-det-ret det-ret)
428 (setq min-det-ret det-ret
433 dest (cons (cdr ret) freq))
435 ((= min-det-ret det-ret)
436 (cond ((> min-det-org det-org)
437 (setq min-det-org det-org
441 dest (cons (cdr ret) freq))
443 ((= min-det-org det-org)
444 (cond ((> min-freq freq)
448 dest (cons (cdr ret) freq))
462 (setq jr (nthcdr (1- d-j) situation-examples))
463 (setcdr jr (cddr jr))
465 (setq situation-examples
466 (cdr situation-examples))
467 (setq ir (nthcdr (1- d-i) situation-examples))
468 (setcdr ir (cddr ir))
470 (if (setq ir (assoc (car dest) situation-examples))
472 (setcdr ir (+ (cdr ir)(cdr dest)))
474 (cons dest situation-examples)
475 ;; situation-examples may be modified.
479 ;;; @ presentation of preview
485 ;;; @@@ predicate function
488 ;; (defun mime-view-entity-button-visible-p (entity)
489 ;; "Return non-nil if header of ENTITY is visible.
490 ;; Please redefine this function if you want to change default setting."
491 ;; (let ((media-type (mime-entity-media-type entity))
492 ;; (media-subtype (mime-entity-media-subtype entity)))
493 ;; (or (not (eq media-type 'application))
494 ;; (and (not (eq media-subtype 'x-selection))
495 ;; (or (not (eq media-subtype 'octet-stream))
496 ;; (let ((mother-entity (mime-entity-parent entity)))
497 ;; (or (not (eq (mime-entity-media-type mother-entity)
499 ;; (not (eq (mime-entity-media-subtype mother-entity)
504 ;;; @@@ entity button generator
507 (defun mime-view-insert-entity-button (entity)
508 "Insert entity-button of ENTITY."
509 (let ((entity-node-id (mime-entity-node-id entity))
510 (params (mime-entity-parameters entity))
511 (subject (mime-view-entity-title entity)))
513 (let ((access-type (assoc "access-type" params))
514 (num (or (cdr (assoc "x-part-number" params))
515 (if (consp entity-node-id)
518 (format "%s" (1+ num))
520 (reverse entity-node-id) ".")
524 (let ((server (assoc "server" params)))
525 (setq access-type (cdr access-type))
527 (format "%s %s ([%s] %s)"
528 num subject access-type (cdr server))
529 (let ((site (cdr (assoc "site" params)))
530 (dir (cdr (assoc "directory" params)))
531 (url (cdr (assoc "url" params)))
534 (format "%s %s ([%s] %s)"
535 num subject access-type url)
536 (format "%s %s ([%s] %s:%s)"
537 num subject access-type site dir))
541 (let ((media-type (mime-entity-media-type entity))
542 (media-subtype (mime-entity-media-subtype entity))
543 (charset (cdr (assoc "charset" params)))
544 (encoding (mime-entity-encoding entity)))
548 (format " <%s/%s%s%s>"
549 media-type media-subtype
551 (concat "; " charset)
554 (concat " (" encoding ")")
556 (if (>= (+ (current-column)(length rest))(window-width))
560 (function mime-preview-play-current-entity))
567 (defvar mime-header-presentation-method-alist nil
568 "Alist of major mode vs. corresponding header-presentation-method functions.
569 Each element looks like (SYMBOL . FUNCTION).
570 SYMBOL must be major mode in raw-buffer or t. t means default.
571 Interface of FUNCTION must be (ENTITY SITUATION).")
573 (defvar mime-view-ignored-field-list
574 '(".*Received:" ".*Path:" ".*Id:" "^References:"
575 "^Replied:" "^Errors-To:"
576 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
577 "^Content-Type:" "^Precedence:"
578 "^Status:" "^X-VM-.*:")
579 "All fields that match this list will be hidden in MIME preview buffer.
580 Each elements are regexp of field-name.")
582 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
583 "All fields that match this list will be displayed in MIME preview buffer.
584 Each elements are regexp of field-name.")
590 ;;; @@@ predicate function
593 (in-calist-package 'mime-view)
595 (defun mime-calist::field-match-method-as-default-rule (calist
596 field-type field-value)
597 (let ((s-field (assq field-type calist)))
598 (cond ((null s-field)
599 (cons (cons field-type field-value) calist)
603 (define-calist-field-match-method
604 'header #'mime-calist::field-match-method-as-default-rule)
606 (define-calist-field-match-method
607 'body #'mime-calist::field-match-method-as-default-rule)
610 (defvar mime-preview-condition nil
611 "Condition-tree about how to display entity.")
613 (ctree-set-calist-strictly
614 'mime-preview-condition '((type . application)(subtype . octet-stream)
617 (ctree-set-calist-strictly
618 'mime-preview-condition '((type . application)(subtype . octet-stream)
621 (ctree-set-calist-strictly
622 'mime-preview-condition '((type . application)(subtype . octet-stream)
626 (ctree-set-calist-strictly
627 'mime-preview-condition '((type . application)(subtype . pgp)
630 (ctree-set-calist-strictly
631 'mime-preview-condition '((type . application)(subtype . x-latex)
634 (ctree-set-calist-strictly
635 'mime-preview-condition '((type . application)(subtype . x-selection)
638 (ctree-set-calist-strictly
639 'mime-preview-condition '((type . application)(subtype . x-comment)
642 (ctree-set-calist-strictly
643 'mime-preview-condition '((type . message)(subtype . delivery-status)
646 (ctree-set-calist-strictly
647 'mime-preview-condition
649 (body-presentation-method . mime-display-text/plain)))
651 (ctree-set-calist-strictly
652 'mime-preview-condition
655 (body-presentation-method . mime-display-text/plain)))
657 (ctree-set-calist-strictly
658 'mime-preview-condition
659 '((type . text)(subtype . enriched)
661 (body-presentation-method . mime-display-text/enriched)))
663 (ctree-set-calist-strictly
664 'mime-preview-condition
665 '((type . text)(subtype . richtext)
667 (body-presentation-method . mime-display-text/richtext)))
669 (autoload 'mime-display-application/x-postpet "postpet")
671 (ctree-set-calist-strictly
672 'mime-preview-condition
673 '((type . application)(subtype . x-postpet)
675 (body-presentation-method . mime-display-application/x-postpet)))
677 (ctree-set-calist-strictly
678 'mime-preview-condition
679 '((type . text)(subtype . t)
681 (body-presentation-method . mime-display-text/plain)))
683 (ctree-set-calist-strictly
684 'mime-preview-condition
685 '((type . multipart)(subtype . alternative)
687 (body-presentation-method . mime-display-multipart/alternative)))
689 (ctree-set-calist-strictly
690 'mime-preview-condition
691 '((type . multipart)(subtype . t)
693 (body-presentation-method . mime-display-multipart/mixed)))
695 (ctree-set-calist-strictly
696 'mime-preview-condition
697 '((type . message)(subtype . partial)
699 (body-presentation-method . mime-display-message/partial-button)))
701 (ctree-set-calist-strictly
702 'mime-preview-condition
703 '((type . message)(subtype . rfc822)
705 (body-presentation-method . mime-display-multipart/mixed)
706 (childrens-situation (header . visible)
707 (entity-button . invisible))))
709 (ctree-set-calist-strictly
710 'mime-preview-condition
711 '((type . message)(subtype . news)
713 (body-presentation-method . mime-display-multipart/mixed)
714 (childrens-situation (header . visible)
715 (entity-button . invisible))))
718 ;;; @@@ entity presentation
721 (defun mime-display-text/plain (entity situation)
723 (narrow-to-region (point-max)(point-max))
724 (mime-insert-text-content entity)
725 (run-hooks 'mime-text-decode-hook)
726 (goto-char (point-max))
727 (if (not (eq (char-after (1- (point))) ?\n))
730 (mime-add-url-buttons)
731 (run-hooks 'mime-display-text/plain-hook)
734 (defun mime-display-text/richtext (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 (richtext-decode beg (point-max))
744 (defun mime-display-text/enriched (entity situation)
746 (narrow-to-region (point-max)(point-max))
747 (mime-insert-text-content entity)
748 (run-hooks 'mime-text-decode-hook)
749 (let ((beg (point-min)))
750 (remove-text-properties beg (point-max) '(face nil))
751 (enriched-decode beg (point-max))
755 (defvar mime-view-announcement-for-message/partial
756 (if (and (>= emacs-major-version 19) window-system)
758 \[[ This is message/partial style split message. ]]
759 \[[ Please press `v' key in this buffer ]]
760 \[[ or click here by mouse button-2. ]]"
762 \[[ This is message/partial style split message. ]]
763 \[[ Please press `v' key in this buffer. ]]"
766 (defun mime-display-message/partial-button (&optional entity situation)
768 (goto-char (point-max))
769 (if (not (search-backward "\n\n" nil t))
772 (goto-char (point-max))
773 (narrow-to-region (point-max)(point-max))
774 (insert mime-view-announcement-for-message/partial)
775 (mime-add-button (point-min)(point-max)
776 #'mime-preview-play-current-entity)
779 (defun mime-display-multipart/mixed (entity situation)
780 (let ((children (mime-entity-children entity))
781 (original-major-mode-cell (assq 'major-mode situation))
783 (cdr (assq 'childrens-situation situation))))
784 (if original-major-mode-cell
785 (setq default-situation
786 (cons original-major-mode-cell default-situation)))
788 (mime-display-entity (car children) nil default-situation)
789 (setq children (cdr children))
792 (defcustom mime-view-type-subtype-score-alist
793 '(((text . enriched) . 3)
794 ((text . richtext) . 2)
797 "Alist MEDIA-TYPE vs corresponding score.
798 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
800 :type '(repeat (cons (choice :tag "Media-Type"
801 (cons :tag "Type/Subtype"
802 (symbol :tag "Primary-type")
803 (symbol :tag "Subtype"))
805 (const :tag "Default" t))
808 (defun mime-display-multipart/alternative (entity situation)
809 (let* ((children (mime-entity-children entity))
810 (original-major-mode-cell (assq 'major-mode situation))
812 (cdr (assq 'childrens-situation situation)))
817 (if original-major-mode-cell
818 (setq default-situation
819 (cons original-major-mode-cell default-situation)))
824 (mime-find-entity-preview-situation
825 child default-situation)))
826 (if (cdr (assq 'body-presentation-method situation))
831 (cdr (assq 'type situation))
832 (cdr (assq 'subtype situation)))
833 mime-view-type-subtype-score-alist)
835 (cdr (assq 'type situation))
836 mime-view-type-subtype-score-alist)
839 mime-view-type-subtype-score-alist)
841 (if (> score max-score)
851 (let ((child (car children))
852 (situation (car situations)))
853 (mime-display-entity child (if (= i p)
855 (put-alist 'body 'invisible
856 (copy-alist situation)))))
857 (setq children (cdr children)
858 situations (cdr situations)
862 ;;; @ acting-condition
865 (defvar mime-acting-condition nil
866 "Condition-tree about how to process entity.")
868 (if (file-readable-p mailcap-file)
869 (let ((entries (mailcap-parse-file)))
871 (let ((entry (car entries))
874 (let* ((field (car entry))
875 (field-type (car field)))
876 (cond ((eq field-type 'view) (setq view field))
877 ((eq field-type 'print) (setq print field))
878 ((memq field-type '(compose composetyped edit)))
879 (t (setq shared (cons field shared))))
881 (setq entry (cdr entry))
883 (setq shared (nreverse shared))
884 (ctree-set-calist-with-default
885 'mime-acting-condition
886 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
888 (ctree-set-calist-with-default
889 'mime-acting-condition
891 (list '(mode . "print")(cons 'method (cdr view))))
894 (setq entries (cdr entries))
897 (ctree-set-calist-strictly
898 'mime-acting-condition
899 '((type . application)(subtype . octet-stream)
901 (method . mime-detect-content)
904 (ctree-set-calist-with-default
905 'mime-acting-condition
907 (method . mime-save-content)))
909 (ctree-set-calist-strictly
910 'mime-acting-condition
911 '((type . text)(subtype . x-rot13-47)(mode . "play")
912 (method . mime-view-caesar)
914 (ctree-set-calist-strictly
915 'mime-acting-condition
916 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
917 (method . mime-view-caesar)
920 (ctree-set-calist-strictly
921 'mime-acting-condition
922 '((type . message)(subtype . rfc822)(mode . "play")
923 (method . mime-view-message/rfc822)
925 (ctree-set-calist-strictly
926 'mime-acting-condition
927 '((type . message)(subtype . partial)(mode . "play")
928 (method . mime-store-message/partial-piece)
931 (ctree-set-calist-strictly
932 'mime-acting-condition
933 '((type . message)(subtype . external-body)
934 ("access-type" . "anon-ftp")
935 (method . mime-view-message/external-anon-ftp)
938 (ctree-set-calist-strictly
939 'mime-acting-condition
940 '((type . message)(subtype . external-body)
941 ("access-type" . "url")
942 (method . mime-view-message/external-url)
945 (ctree-set-calist-strictly
946 'mime-acting-condition
947 '((type . application)(subtype . octet-stream)
948 (method . mime-save-content)
952 ;;; @ quitting method
955 (defvar mime-preview-quitting-method-alist
956 '((mime-show-message-mode
957 . mime-preview-quitting-method-for-mime-show-message-mode))
958 "Alist of major-mode vs. quitting-method of mime-view.")
960 (defvar mime-preview-over-to-previous-method-alist nil
961 "Alist of major-mode vs. over-to-previous-method of mime-view.")
963 (defvar mime-preview-over-to-next-method-alist nil
964 "Alist of major-mode vs. over-to-next-method of mime-view.")
967 ;;; @ following method
970 (defvar mime-preview-following-method-alist nil
971 "Alist of major-mode vs. following-method of mime-view.")
973 (defvar mime-view-following-required-fields-list
980 (defun mime-display-entity (entity &optional situation
981 default-situation preview-buffer)
983 (setq preview-buffer (current-buffer)))
984 (let* (e nb ne nhb nbb)
985 (in-calist-package 'mime-view)
988 (mime-find-entity-preview-situation entity default-situation)))
989 (let ((button-is-invisible
990 (eq (cdr (or (assq '*entity-button situation)
991 (assq 'entity-button situation)))
994 (eq (cdr (or (assq '*header situation)
995 (assq 'header situation)))
998 (eq (cdr (or (assq '*body situation)
999 (assq 'body situation)))
1001 (children (mime-entity-children entity)))
1002 (set-buffer preview-buffer)
1004 (narrow-to-region nb nb)
1005 (or button-is-invisible
1006 ;; (if (mime-view-entity-button-visible-p entity)
1007 (mime-view-insert-entity-button entity)
1010 (if header-is-visible
1011 (let ((header-presentation-method
1012 (or (cdr (assq 'header-presentation-method situation))
1013 (cdr (assq (cdr (assq 'major-mode situation))
1014 mime-header-presentation-method-alist)))))
1016 (if header-presentation-method
1017 (funcall header-presentation-method entity situation)
1018 (mime-insert-header entity
1019 mime-view-ignored-field-list
1020 mime-view-visible-field-list))
1021 (run-hooks 'mime-display-header-hook)
1022 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1023 (goto-char (point-max))
1028 (let ((body-presentation-method
1029 (cdr (assq 'body-presentation-method situation))))
1030 (if (functionp body-presentation-method)
1031 (funcall body-presentation-method entity situation)
1032 (mime-display-text/plain entity situation)))
1033 (when button-is-invisible
1034 (goto-char (point-max))
1035 (mime-view-insert-entity-button entity)
1037 (unless header-is-visible
1038 (goto-char (point-max))
1041 (setq ne (point-max))
1043 (put-text-property nb ne 'mime-view-entity entity)
1044 (put-text-property nb ne 'mime-view-situation situation)
1045 (put-text-property nbb ne 'mime-view-entity-body entity)
1047 (if (and children body-is-visible)
1048 (let ((body-presentation-method
1049 (cdr (assq 'body-presentation-method situation))))
1050 (if (functionp body-presentation-method)
1051 (funcall body-presentation-method entity situation)
1052 (mime-display-multipart/mixed entity situation))))
1056 ;;; @ MIME viewer mode
1059 (defconst mime-view-menu-title "MIME-View")
1060 (defconst mime-view-menu-list
1061 '((up "Move to upper entity" mime-preview-move-to-upper)
1062 (previous "Move to previous entity" mime-preview-move-to-previous)
1063 (next "Move to next entity" mime-preview-move-to-next)
1064 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
1065 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
1066 (play "Play current entity" mime-preview-play-current-entity)
1067 (extract "Extract current entity" mime-preview-extract-current-entity)
1068 (print "Print current entity" mime-preview-print-current-entity)
1070 "Menu for MIME Viewer")
1072 (cond ((featurep 'xemacs)
1073 (defvar mime-view-xemacs-popup-menu
1074 (cons mime-view-menu-title
1077 (vector (nth 1 item)(nth 2 item) t)
1079 mime-view-menu-list)))
1080 (defun mime-view-xemacs-popup-menu (event)
1081 "Popup the menu in the MIME Viewer buffer"
1083 (select-window (event-window event))
1084 (set-buffer (event-buffer event))
1085 (popup-menu 'mime-view-xemacs-popup-menu))
1086 (defvar mouse-button-2 'button2)
1089 (defvar mime-view-popup-menu
1090 (let ((menu (make-sparse-keymap mime-view-menu-title)))
1094 (list (intern (nth 1 item)) 'menu-item
1095 (nth 1 item)(nth 2 item))
1097 mime-view-menu-list))))
1098 (defun mime-view-popup-menu (event)
1099 "Popup the menu in the MIME Viewer buffer"
1101 (let ((menu mime-view-popup-menu) events func)
1102 (setq events (x-popup-menu t menu))
1104 (setq func (lookup-key menu (apply #'vector events)))
1107 (defvar mouse-button-2 [mouse-2])
1110 (defun mime-view-define-keymap (&optional default)
1111 (let ((mime-view-mode-map (if (keymapp default)
1112 (copy-keymap default)
1113 (make-sparse-keymap)
1115 (define-key mime-view-mode-map
1116 "u" (function mime-preview-move-to-upper))
1117 (define-key mime-view-mode-map
1118 "p" (function mime-preview-move-to-previous))
1119 (define-key mime-view-mode-map
1120 "n" (function mime-preview-move-to-next))
1121 (define-key mime-view-mode-map
1122 "\e\t" (function mime-preview-move-to-previous))
1123 (define-key mime-view-mode-map
1124 "\t" (function mime-preview-move-to-next))
1125 (define-key mime-view-mode-map
1126 " " (function mime-preview-scroll-up-entity))
1127 (define-key mime-view-mode-map
1128 "\M- " (function mime-preview-scroll-down-entity))
1129 (define-key mime-view-mode-map
1130 "\177" (function mime-preview-scroll-down-entity))
1131 (define-key mime-view-mode-map
1132 "\C-m" (function mime-preview-next-line-entity))
1133 (define-key mime-view-mode-map
1134 "\C-\M-m" (function mime-preview-previous-line-entity))
1135 (define-key mime-view-mode-map
1136 "v" (function mime-preview-play-current-entity))
1137 (define-key mime-view-mode-map
1138 "e" (function mime-preview-extract-current-entity))
1139 (define-key mime-view-mode-map
1140 "\C-c\C-p" (function mime-preview-print-current-entity))
1141 (define-key mime-view-mode-map
1142 "\C-c\C-t\C-h" (function mime-preview-toggle-header))
1143 (define-key mime-view-mode-map
1144 "\C-c\C-t\C-c" (function mime-preview-toggle-content))
1145 (define-key mime-view-mode-map
1146 "a" (function mime-preview-follow-current-entity))
1147 (define-key mime-view-mode-map
1148 "q" (function mime-preview-quit))
1149 (define-key mime-view-mode-map
1150 "\C-c\C-x" (function mime-preview-kill-buffer))
1151 ;; (define-key mime-view-mode-map
1152 ;; "<" (function beginning-of-buffer))
1153 ;; (define-key mime-view-mode-map
1154 ;; ">" (function end-of-buffer))
1155 (define-key mime-view-mode-map
1156 "?" (function describe-mode))
1157 (define-key mime-view-mode-map
1158 [tab] (function mime-preview-move-to-next))
1159 (define-key mime-view-mode-map
1160 [delete] (function mime-preview-scroll-down-entity))
1161 (define-key mime-view-mode-map
1162 [backspace] (function mime-preview-scroll-down-entity))
1163 (if (functionp default)
1164 (cond ((featurep 'xemacs)
1165 (set-keymap-default-binding mime-view-mode-map default)
1168 (setq mime-view-mode-map
1169 (append mime-view-mode-map (list (cons t default))))
1172 (define-key mime-view-mode-map
1173 mouse-button-2 (function mime-button-dispatcher))
1175 (cond ((featurep 'xemacs)
1176 (define-key mime-view-mode-map
1177 mouse-button-3 (function mime-view-xemacs-popup-menu))
1179 ((>= emacs-major-version 19)
1180 (define-key mime-view-mode-map
1181 mouse-button-3 (function mime-view-popup-menu))
1182 (define-key mime-view-mode-map [menu-bar mime-view]
1183 (cons mime-view-menu-title
1184 (make-sparse-keymap mime-view-menu-title)))
1187 (define-key mime-view-mode-map
1188 (vector 'menu-bar 'mime-view (car item))
1189 (cons (nth 1 item)(nth 2 item))
1192 (reverse mime-view-menu-list)
1195 (use-local-map mime-view-mode-map)
1196 (run-hooks 'mime-view-define-keymap-hook)
1199 (defsubst mime-maybe-hide-echo-buffer ()
1200 "Clear mime-echo buffer and delete window for it."
1201 (let ((buf (get-buffer mime-echo-buffer-name)))
1206 (let ((win (get-buffer-window buf)))
1213 (defvar mime-view-redisplay nil)
1216 (defun mime-display-message (message &optional preview-buffer
1217 mother default-keymap-or-function
1218 original-major-mode)
1219 "View MESSAGE in MIME-View mode.
1221 Optional argument PREVIEW-BUFFER specifies the buffer of the
1222 presentation. It must be either nil or a name of preview buffer.
1224 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1226 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1227 function. If it is a keymap, keymap of MIME-View mode will be added
1228 to it. If it is a function, it will be bound as default binding of
1229 keymap of MIME-View mode."
1230 (mime-maybe-hide-echo-buffer)
1231 (let ((win-conf (current-window-configuration)))
1233 (setq preview-buffer
1234 (concat "*Preview-" (mime-entity-name message) "*")))
1235 (or original-major-mode
1236 (setq original-major-mode major-mode))
1237 (let ((inhibit-read-only t))
1238 (set-buffer (get-buffer-create preview-buffer))
1242 (setq mime-mother-buffer mother)
1244 (setq mime-preview-original-window-configuration win-conf)
1245 (setq major-mode 'mime-view-mode)
1246 (setq mode-name "MIME-View")
1247 (mime-display-entity message nil
1248 `((entity-button . invisible)
1250 (major-mode . ,original-major-mode))
1252 (mime-view-define-keymap default-keymap-or-function)
1254 (next-single-property-change (point-min) 'mime-view-entity)))
1257 (goto-char (point-min))
1258 (search-forward "\n\n" nil t)
1260 (run-hooks 'mime-view-mode-hook)
1261 (set-buffer-modified-p nil)
1262 (setq buffer-read-only t)
1266 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1267 default-keymap-or-function
1268 representation-type)
1269 "View RAW-BUFFER in MIME-View mode.
1270 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1272 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1273 function. If it is a keymap, keymap of MIME-View mode will be added
1274 to it. If it is a function, it will be bound as default binding of
1275 keymap of MIME-View mode.
1276 Optional argument REPRESENTATION-TYPE is representation-type of
1277 message. It must be nil, `binary' or `cooked'. If it is nil,
1278 `cooked' is used as default."
1281 (setq raw-buffer (current-buffer)))
1282 (or representation-type
1283 (setq representation-type
1285 (set-buffer raw-buffer)
1286 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1287 (assq t mime-raw-representation-type-alist)))
1289 (if (eq representation-type 'binary)
1290 (setq representation-type 'buffer)
1292 (setq preview-buffer (mime-display-message
1293 (mime-open-entity representation-type raw-buffer)
1294 preview-buffer mother default-keymap-or-function))
1295 (or (get-buffer-window preview-buffer)
1296 (let ((r-win (get-buffer-window raw-buffer)))
1298 (set-window-buffer r-win preview-buffer)
1299 (let ((m-win (and mother (get-buffer-window mother))))
1301 (set-window-buffer m-win preview-buffer)
1302 (switch-to-buffer preview-buffer)
1305 (defun mime-view-mode (&optional mother ctl encoding
1306 raw-buffer preview-buffer
1307 default-keymap-or-function)
1308 "Major mode for viewing MIME message.
1310 Here is a list of the standard keys for mime-view-mode.
1315 u Move to upper content
1316 p or M-TAB Move to previous content
1317 n or TAB Move to next content
1318 SPC Scroll up or move to next content
1319 M-SPC or DEL Scroll down or move to previous content
1320 RET Move to next line
1321 M-RET Move to previous line
1322 v Decode current content as `play mode'
1323 e Decode current content as `extract mode'
1324 C-c C-p Decode current content as `print mode'
1325 a Followup to current content.
1327 button-2 Move to point under the mouse cursor
1328 and decode current content as `play mode'
1331 (unless mime-view-redisplay
1333 (if raw-buffer (set-buffer raw-buffer))
1336 (or (assq major-mode mime-raw-representation-type-alist)
1337 (assq t mime-raw-representation-type-alist)))))
1338 (if (eq type 'binary)
1341 (setq mime-message-structure (mime-open-entity type raw-buffer))
1342 (or (mime-entity-content-type mime-message-structure)
1343 (mime-entity-set-content-type-internal
1344 mime-message-structure ctl))
1346 (or (mime-entity-encoding mime-message-structure)
1347 (mime-entity-set-encoding-internal mime-message-structure encoding))
1349 (mime-display-message mime-message-structure preview-buffer
1350 mother default-keymap-or-function)
1357 (defun mime-preview-find-boundary-info (&optional get-mother)
1361 (while (null (setq entity
1362 (get-text-property (point) 'mime-view-entity)))
1364 (setq p-beg (previous-single-property-change (point) 'mime-view-entity))
1365 (setq entity-node-id (mime-entity-node-id entity))
1366 (setq len (length entity-node-id))
1369 (if (eq (next-single-property-change (point-min)
1375 ((eq (next-single-property-change p-beg 'mime-view-entity)
1377 (setq p-beg (point))
1379 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1381 (setq p-end (point-max))
1383 ((null entity-node-id)
1384 (setq p-end (point-max))
1392 (next-single-property-change
1393 (point) 'mime-view-entity))
1395 (let ((rc (mime-entity-node-id
1396 (get-text-property (1- (point))
1397 'mime-view-entity))))
1398 (or (and (>= (setq i (- (length rc) len)) 0)
1399 (equal entity-node-id (nthcdr i rc)))
1402 (setq p-end (point-max))))
1404 (vector p-beg p-end entity)))
1410 (autoload 'mime-preview-play-current-entity "mime-play"
1411 "Play current entity." t)
1413 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1414 "Extract current entity into file (maybe).
1415 It decodes current entity to call internal or external method as
1416 \"extract\" mode. The method is selected from variable
1417 `mime-acting-condition'."
1419 (mime-preview-play-current-entity ignore-examples "extract")
1422 (defun mime-preview-print-current-entity (&optional ignore-examples)
1423 "Print current entity (maybe).
1424 It decodes current entity to call internal or external method as
1425 \"print\" mode. The method is selected from variable
1426 `mime-acting-condition'."
1428 (mime-preview-play-current-entity ignore-examples "print")
1435 (defun mime-preview-follow-current-entity ()
1436 "Write follow message to current entity.
1437 It calls following-method selected from variable
1438 `mime-preview-following-method-alist'."
1440 (let ((entity (mime-preview-find-boundary-info t))
1443 (setq p-beg (aref entity 0)
1444 p-end (aref entity 1)
1445 entity (aref entity 2))
1446 (if (get-text-property p-beg 'mime-view-entity-body)
1449 (next-single-property-change
1450 p-beg 'mime-view-entity-body nil
1451 (or (next-single-property-change p-beg 'mime-view-entity)
1453 (let* ((mode (mime-preview-original-major-mode 'recursive))
1454 (entity-node-id (mime-entity-node-id entity))
1456 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1458 (the-buf (current-buffer))
1461 (set-buffer (setq new-buf (get-buffer-create new-name)))
1464 (insert-buffer-substring the-buf pb-beg p-end)
1465 (goto-char (point-min))
1466 (let ((current-entity
1467 (if (and (eq (mime-entity-media-type entity) 'message)
1468 (eq (mime-entity-media-subtype entity) 'rfc822))
1469 (car (mime-entity-children entity))
1472 (while (and current-entity
1473 (if (and (eq (mime-entity-media-type
1474 current-entity) 'message)
1475 (eq (mime-entity-media-subtype
1476 current-entity) 'rfc822))
1478 (mime-insert-header current-entity fields)
1480 (setq fields (std11-collect-field-names)
1481 current-entity (mime-entity-parent current-entity))
1483 (let ((rest mime-view-following-required-fields-list)
1486 (setq field-name (car rest))
1487 (or (std11-field-body field-name)
1490 (set-buffer the-buf)
1491 (let ((entity (when mime-mother-buffer
1492 (set-buffer mime-mother-buffer)
1493 (get-text-property (point)
1494 'mime-view-entity))))
1496 (null (setq ret (mime-entity-fetch-field
1497 entity field-name))))
1498 (setq entity (mime-entity-parent entity)))))
1500 (insert (concat field-name ": " ret "\n"))
1502 (setq rest (cdr rest))
1505 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1510 "Sorry, following method for %s is not implemented yet."
1519 (defun mime-preview-move-to-upper ()
1520 "Move to upper entity.
1521 If there is no upper entity, call function `mime-preview-quit'."
1524 (while (null (setq cinfo
1525 (get-text-property (point) 'mime-view-entity)))
1528 (let ((r (mime-entity-parent cinfo))
1531 (while (setq point (previous-single-property-change
1532 (point) 'mime-view-entity))
1534 (when (eq r (get-text-property (point) 'mime-view-entity))
1535 (if (or (eq mime-preview-move-scroll t)
1536 (and mime-preview-move-scroll
1539 (move-to-window-line -1)
1540 (forward-line (* -1 next-screen-context-lines))
1543 (recenter next-screen-context-lines))
1550 (defun mime-preview-move-to-previous ()
1551 "Move to previous entity.
1552 If there is no previous entity, it calls function registered in
1553 variable `mime-preview-over-to-previous-method-alist'."
1555 (while (and (not (bobp))
1556 (null (get-text-property (point) 'mime-view-entity)))
1559 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1561 (>= point (point-min)))
1562 (if (get-text-property (1- point) 'mime-view-entity)
1563 (progn (goto-char point)
1565 (or (eq mime-preview-move-scroll t)
1566 (and mime-preview-move-scroll
1569 (move-to-window-line 0)
1570 (forward-line next-screen-context-lines)
1573 (recenter (* -1 next-screen-context-lines))))
1574 (goto-char (1- point))
1575 (mime-preview-move-to-previous)
1577 (let ((f (assq (mime-preview-original-major-mode)
1578 mime-preview-over-to-previous-method-alist)))
1584 (defun mime-preview-move-to-next ()
1585 "Move to next entity.
1586 If there is no previous entity, it calls function registered in
1587 variable `mime-preview-over-to-next-method-alist'."
1589 (while (and (not (eobp))
1590 (null (get-text-property (point) 'mime-view-entity)))
1593 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1595 (<= point (point-max)))
1598 (if (null (get-text-property point 'mime-view-entity))
1599 (mime-preview-move-to-next)
1601 (or (eq mime-preview-move-scroll t)
1602 (and mime-preview-move-scroll
1605 (move-to-window-line -1)
1607 (* -1 next-screen-context-lines))
1610 (recenter next-screen-context-lines))
1612 (let ((f (assq (mime-preview-original-major-mode)
1613 mime-preview-over-to-next-method-alist)))
1619 (defun mime-preview-scroll-up-entity (&optional h)
1620 "Scroll up current entity.
1621 If reached to (point-max), it calls function registered in variable
1622 `mime-preview-over-to-next-method-alist'."
1625 (let ((f (assq (mime-preview-original-major-mode)
1626 mime-preview-over-to-next-method-alist)))
1631 (or (next-single-property-change (point) 'mime-view-entity)
1633 (bottom (window-end (selected-window))))
1636 (progn (goto-char point)
1637 (recenter next-screen-context-lines))
1641 (goto-char (point-max)))))
1644 (defun mime-preview-scroll-down-entity (&optional h)
1645 "Scroll down current entity.
1646 If reached to (point-min), it calls function registered in variable
1647 `mime-preview-over-to-previous-method-alist'."
1650 (let ((f (assq (mime-preview-original-major-mode)
1651 mime-preview-over-to-previous-method-alist)))
1656 (or (previous-single-property-change (point) 'mime-view-entity)
1658 (top (window-start (selected-window))))
1661 (progn (goto-char point)
1662 (recenter (* -1 next-screen-context-lines)))
1665 (beginning-of-buffer
1666 (goto-char (point-min)))))
1669 (defun mime-preview-next-line-entity (&optional lines)
1670 "Scroll up one line (or prefix LINES lines).
1671 If LINES is negative, scroll down LINES lines."
1673 (mime-preview-scroll-up-entity (or lines 1))
1676 (defun mime-preview-previous-line-entity (&optional lines)
1677 "Scrroll down one line (or prefix LINES lines).
1678 If LINES is negative, scroll up LINES lines."
1680 (mime-preview-scroll-down-entity (or lines 1))
1687 (defun mime-preview-toggle-header ()
1689 (let ((situation (mime-preview-find-boundary-info))
1691 (setq p-beg (aref situation 0)
1692 p-end (aref situation 1)
1693 entity (aref situation 2)
1694 situation (get-text-property p-beg 'mime-view-situation))
1695 (let ((cell (assq '*header situation)))
1697 (setq cell (assq 'header situation)))
1698 (if (eq (cdr cell) 'visible)
1699 (setq situation (put-alist '*header 'invisible situation))
1700 (setq situation (put-alist '*header 'visible situation))))
1702 (let ((inhibit-read-only t))
1703 (delete-region p-beg p-end)
1704 (mime-display-entity entity situation)))
1705 (let ((ret (assoc situation mime-preview-situation-example-list)))
1707 (setcdr ret (1+ (cdr ret)))
1708 (add-to-list 'mime-preview-situation-example-list
1709 (cons situation 0))))))
1711 (defun mime-preview-toggle-content ()
1713 (let ((situation (mime-preview-find-boundary-info))
1715 (setq p-beg (aref situation 0)
1716 p-end (aref situation 1)
1717 entity (aref situation 2)
1718 situation (get-text-property p-beg 'mime-view-situation))
1719 (let ((cell (assq '*body situation)))
1721 (setq cell (assq 'body situation)))
1722 (if (eq (cdr cell) 'visible)
1723 (setq situation (put-alist '*body 'invisible situation))
1724 (setq situation (put-alist '*body 'visible situation))))
1726 (let ((inhibit-read-only t))
1727 (delete-region p-beg p-end)
1728 (mime-display-entity entity situation)
1730 (let ((ret (assoc situation mime-preview-situation-example-list)))
1732 (setcdr ret (1+ (cdr ret)))
1733 (add-to-list 'mime-preview-situation-example-list
1734 (cons situation 0))))))
1740 (defun mime-preview-quit ()
1741 "Quit from MIME-preview buffer.
1742 It calls function registered in variable
1743 `mime-preview-quitting-method-alist'."
1745 (let ((r (assq (mime-preview-original-major-mode)
1746 mime-preview-quitting-method-alist)))
1751 (defun mime-preview-kill-buffer ()
1753 (kill-buffer (current-buffer))
1760 (provide 'mime-view)
1762 (let* ((file mime-situation-examples-file)
1763 (buffer (get-buffer-create " *mime-example*")))
1764 (if (file-readable-p file)
1769 (insert-file-contents file)
1774 (while (and (> (length mime-preview-situation-example-list)
1775 mime-preview-situation-example-list-max-size)
1777 (setq mime-preview-situation-example-list
1778 (mime-reduce-situation-examples
1779 mime-preview-situation-example-list))
1782 (error (setq mime-preview-situation-example-list nil)))
1783 ;; (let ((rest mime-preview-situation-example-list))
1785 ;; (ctree-set-calist-strictly 'mime-preview-condition
1787 ;; (setq rest (cdr rest))))
1790 (while (and (> (length mime-acting-situation-example-list)
1791 mime-acting-situation-example-list-max-size)
1793 (setq mime-acting-situation-example-list
1794 (mime-reduce-situation-examples
1795 mime-acting-situation-example-list))
1798 (error (setq mime-acting-situation-example-list nil)))
1800 (kill-buffer buffer))))
1802 ;;; mime-view.el ends here