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.
40 (defconst mime-view-version
41 (concat (mime-product-name mime-user-interface-product) " MIME-View "
42 (mapconcat #'number-to-string
43 (mime-product-version mime-user-interface-product) ".")
44 " (" (mime-product-code-name mime-user-interface-product) ")"))
50 (defgroup mime-view nil
54 (defcustom mime-situation-examples-file "~/.mime-example"
55 "*File name of situation-examples demonstrated by user."
59 (defcustom mime-preview-move-scroll nil
60 "*Decides whether to scroll when moving to next entity.
61 When t, scroll the buffer. Non-nil but not t means scroll when
62 the next entity is within next-screen-context-lines from top or
63 buttom. Nil means don't scroll at all."
65 :type '(choice (const :tag "Off" nil)
67 (sexp :tag "Situation" 1)))
70 ;;; @ in raw-buffer (representation space)
73 (defvar mime-preview-buffer nil
74 "MIME-preview buffer corresponding with the (raw) buffer.")
75 (make-variable-buffer-local 'mime-preview-buffer)
78 (defvar mime-raw-representation-type-alist
79 '((mime-show-message-mode . binary)
80 (mime-temp-message-mode . binary)
82 "Alist of major-mode vs. representation-type of mime-raw-buffer.
83 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
84 major-mode or t. t means default. REPRESENTATION-TYPE must be
85 `binary' or `cooked'.")
88 ;;; @ in preview-buffer (presentation space)
91 (defvar mime-mother-buffer nil
92 "Mother buffer corresponding with the (MIME-preview) buffer.
93 If current MIME-preview buffer is generated by other buffer, such as
94 message/partial, it is called `mother-buffer'.")
95 (make-variable-buffer-local 'mime-mother-buffer)
97 ;; (defvar mime-raw-buffer nil
98 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
99 ;; (make-variable-buffer-local 'mime-raw-buffer)
101 (defvar mime-preview-original-window-configuration nil
102 "Window-configuration before mime-view-mode is called.")
103 (make-variable-buffer-local 'mime-preview-original-window-configuration)
105 (defun mime-preview-original-major-mode (&optional recursive point)
106 "Return major-mode of original buffer.
107 If optional argument RECURSIVE is non-nil and current buffer has
108 mime-mother-buffer, it returns original major-mode of the
110 (if (and recursive mime-mother-buffer)
112 (set-buffer mime-mother-buffer)
113 (mime-preview-original-major-mode recursive))
114 (cdr (assq 'major-mode
115 (get-text-property (or point
116 (if (> (point) (buffer-size))
117 (max (1- (point-max)) (point-min))
119 'mime-view-situation)))))
122 ;;; @ entity information
125 (defun mime-entity-situation (entity &optional situation)
126 "Return situation of ENTITY."
127 (let (rest param name)
129 (unless (assq 'type situation)
130 (setq rest (or (mime-entity-content-type entity)
131 (make-mime-content-type 'text 'plain))
132 situation (cons (car rest) situation)
134 (unless (assq 'subtype situation)
136 (setq rest (or (cdr (mime-entity-content-type entity))
137 '((subtype . plain)))))
138 (setq situation (cons (car rest) situation)
141 (setq param (car rest))
142 (or (assoc (car param) situation)
143 (setq situation (cons param situation)))
144 (setq rest (cdr rest)))
146 ;; Content-Disposition
148 (unless (assq 'disposition-type situation)
149 (setq rest (mime-entity-content-disposition entity))
151 (setq situation (cons (cons 'disposition-type
152 (mime-content-disposition-type rest))
154 rest (mime-content-disposition-parameters rest))))
156 (setq param (car rest)
158 (if (cond ((string= name "filename")
159 (if (assq 'filename situation)
161 (setq name 'filename)))
162 ((string= name "creation-date")
163 (if (assq 'creation-date situation)
165 (setq name 'creation-date)))
166 ((string= name "modification-date")
167 (if (assq 'modification-date situation)
169 (setq name 'modification-date)))
170 ((string= name "read-date")
171 (if (assq 'read-date situation)
173 (setq name 'read-date)))
174 ((string= name "size")
175 (if (assq 'size situation)
178 (t (setq name (cons 'disposition name))
179 (if (assoc name situation)
183 (cons (cons name (cdr param))
185 (setq rest (cdr rest)))
187 ;; Content-Transfer-Encoding
188 (or (assq 'encoding situation)
190 (cons (cons 'encoding (or (mime-entity-encoding entity)
196 (defsubst mime-delq-null-situation (situations field
197 &rest ignored-values)
200 (let* ((situation (car situations))
201 (cell (assq field situation)))
203 (or (memq (cdr cell) ignored-values)
204 (setq dest (cons situation dest)))))
205 (setq situations (cdr situations)))
208 (defun mime-compare-situation-with-example (situation example)
209 (let ((example (copy-alist example))
212 (let* ((cell (car situation))
214 (ecell (assoc key example)))
216 (if (equal cell ecell)
217 (setq match (1+ match))
218 (setq example (delq ecell example)))))
219 (setq situation (cdr situation)))
220 (cons match example)))
222 (defun mime-sort-situation (situation)
232 (disposition-type . 6)))
235 (let ((ret (assq a-t order)))
237 (setq a-order (cdr ret))
241 (let ((ret (assq b-t order)))
243 (setq b-order (cdr ret))
246 (if (= a-order b-order)
247 (string< (format "%s" a-t)(format "%s" b-t))
248 (< a-order b-order))))))
250 (defun mime-unify-situations (entity-situation
251 condition situation-examples
252 &optional required-name ignored-value
255 (in-calist-package 'mime-view)
257 (ctree-find-calist condition entity-situation
260 (setq ret (mime-delq-null-situation ret required-name
262 (or (assq 'ignore-examples entity-situation)
270 (let ((situation (car rest))
271 (examples situation-examples))
274 (mime-compare-situation-with-example
275 situation (caar examples)))
276 (ret-score (car ret)))
277 (cond ((> ret-score max-score)
278 (setq max-score ret-score
279 max-escore (cdar examples)
280 max-examples (list (cdr ret))
281 max-situations (list situation)))
282 ((= ret-score max-score)
283 (cond ((> (cdar examples) max-escore)
284 (setq max-escore (cdar examples)
285 max-examples (list (cdr ret))
286 max-situations (list situation)))
287 ((= (cdar examples) max-escore)
289 (cons (cdr ret) max-examples))
290 (or (member situation max-situations)
292 (cons situation max-situations))))))))
293 (setq examples (cdr examples))))
294 (setq rest (cdr rest)))
296 (setq ret max-situations)
298 (let* ((example (car max-examples))
300 (assoc example situation-examples)))
302 (setcdr cell (1+ (cdr cell)))
303 (setq situation-examples
304 (cons (cons example 0)
305 situation-examples))))
306 (setq max-examples (cdr max-examples)))))))
307 (cons ret situation-examples)
308 ;; ret: list of situations
309 ;; situation-examples: new examples (notoce that contents of
310 ;; argument `situation-examples' has bees modified)
313 (defun mime-view-entity-title (entity)
314 (or (mime-entity-read-field entity 'Content-Description)
315 (mime-entity-read-field entity 'Subject)
316 (mime-entity-filename entity)
319 (defvar mime-preview-situation-example-list nil)
320 (defvar mime-preview-situation-example-list-max-size 16)
321 ;; (defvar mime-preview-situation-example-condition nil)
323 (defun mime-find-entity-preview-situation (entity
324 &optional default-situation)
326 (mime-unify-situations
327 (append (mime-entity-situation entity)
329 mime-preview-condition
330 mime-preview-situation-example-list)))
331 (setq mime-preview-situation-example-list
337 (defvar mime-acting-situation-example-list nil)
338 (defvar mime-acting-situation-example-list-max-size 16)
339 (defvar mime-situation-examples-file-coding-system nil)
341 (defun mime-save-situation-examples ()
342 (if (or mime-preview-situation-example-list
343 mime-acting-situation-example-list)
344 (let ((file mime-situation-examples-file))
346 (insert ";;; " (file-name-nondirectory file) "\n")
347 (insert "\n;; This file is generated automatically by "
348 mime-view-version "\n\n")
349 (insert ";;; Code:\n\n")
350 (if mime-preview-situation-example-list
351 (pp `(setq mime-preview-situation-example-list
352 ',mime-preview-situation-example-list)
354 (if mime-acting-situation-example-list
355 (pp `(setq mime-acting-situation-example-list
356 ',mime-acting-situation-example-list)
359 (file-name-nondirectory file)
362 ((boundp 'buffer-file-coding-system)
363 (setq buffer-file-coding-system
364 mime-situation-examples-file-coding-system))
365 ((boundp 'file-coding-system)
366 (setq file-coding-system
367 mime-situation-examples-file-coding-system)))
368 (setq buffer-file-name file)
371 (add-hook 'kill-emacs-hook 'mime-save-situation-examples)
373 (defun mime-reduce-situation-examples (situation-examples)
374 (let ((len (length situation-examples))
382 ir situation-examples)
386 jr situation-examples)
390 (setq ret (mime-compare-situation-with-example (car ic)(car jc))
392 det-ret (+ (length (car ic))(length (car jc)))
393 det-org (length (cdr ret))
394 freq (+ (cdr ic)(cdr jc)))
395 (cond ((< max-sim sim)
402 dest (cons (cdr ret) freq)))
404 (cond ((> min-det-ret det-ret)
405 (setq min-det-ret det-ret
410 dest (cons (cdr ret) freq)))
411 ((= min-det-ret det-ret)
412 (cond ((> min-det-org det-org)
413 (setq min-det-org det-org
417 dest (cons (cdr ret) freq)))
418 ((= min-det-org det-org)
419 (cond ((> min-freq freq)
423 dest (cons (cdr ret) freq)))))))))))
432 (setq jr (nthcdr (1- d-j) situation-examples))
433 (setcdr jr (cddr jr))
435 (setq situation-examples
436 (cdr situation-examples))
437 (setq ir (nthcdr (1- d-i) situation-examples))
438 (setcdr ir (cddr ir)))
439 (if (setq ir (assoc (car dest) situation-examples))
441 (setcdr ir (+ (cdr ir)(cdr dest)))
443 (cons dest situation-examples)
444 ;; situation-examples may be modified.
448 ;;; @ presentation of preview
454 ;;; @@@ predicate function
457 ;; (defun mime-view-entity-button-visible-p (entity)
458 ;; "Return non-nil if header of ENTITY is visible.
459 ;; Please redefine this function if you want to change default setting."
460 ;; (let ((media-type (mime-entity-media-type entity))
461 ;; (media-subtype (mime-entity-media-subtype entity)))
462 ;; (or (not (eq media-type 'application))
463 ;; (and (not (eq media-subtype 'x-selection))
464 ;; (or (not (eq media-subtype 'octet-stream))
465 ;; (let ((mother-entity (mime-entity-parent entity)))
466 ;; (or (not (eq (mime-entity-media-type mother-entity)
468 ;; (not (eq (mime-entity-media-subtype mother-entity)
473 ;;; @@@ entity button generator
476 (defun mime-view-insert-entity-button (entity)
477 "Insert entity-button of ENTITY."
478 (let ((entity-node-id (mime-entity-node-id entity))
479 (params (mime-entity-parameters entity))
480 (subject (mime-view-entity-title entity)))
482 (let ((access-type (assoc "access-type" params))
483 (num (or (cdr (assoc "x-part-number" params))
484 (if (consp entity-node-id)
487 (format "%s" (1+ num))))
488 (reverse entity-node-id) ".")
491 (let ((server (assoc "server" params)))
492 (setq access-type (cdr access-type))
494 (format "%s %s ([%s] %s)"
495 num subject access-type (cdr server))
496 (let ((site (cdr (assoc "site" params)))
497 (dir (cdr (assoc "directory" params)))
498 (url (cdr (assoc "url" params))))
500 (format "%s %s ([%s] %s)"
501 num subject access-type url)
502 (format "%s %s ([%s] %s:%s)"
503 num subject access-type site dir))))))
505 (let ((media-type (mime-entity-media-type entity))
506 (media-subtype (mime-entity-media-subtype entity))
507 (charset (cdr (assoc "charset" params)))
508 (encoding (mime-entity-encoding entity)))
512 (format " <%s/%s%s%s>"
513 media-type media-subtype
515 (concat "; " charset)
518 (concat " (" encoding ")")
520 (if (>= (+ (current-column)(length rest))(window-width))
523 (function mime-preview-play-current-entity))))
529 (defvar mime-header-presentation-method-alist nil
530 "Alist of major mode vs. corresponding header-presentation-method functions.
531 Each element looks like (SYMBOL . FUNCTION).
532 SYMBOL must be major mode in raw-buffer or t. t means default.
533 Interface of FUNCTION must be (ENTITY SITUATION).")
535 (defvar mime-view-ignored-field-list
536 '(".*Received:" ".*Path:" ".*Id:" "^References:"
537 "^Replied:" "^Errors-To:"
538 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
539 "^Content-Type:" "^Precedence:"
540 "^Status:" "^X-VM-.*:")
541 "All fields that match this list will be hidden in MIME preview buffer.
542 Each elements are regexp of field-name.")
544 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
545 "All fields that match this list will be displayed in MIME preview buffer.
546 Each elements are regexp of field-name.")
552 ;;; @@@ predicate function
555 (in-calist-package 'mime-view)
557 (defun mime-calist::field-match-method-as-default-rule (calist
558 field-type field-value)
559 (let ((s-field (assq field-type calist)))
560 (cond ((null s-field)
561 (cons (cons field-type field-value) calist))
564 (define-calist-field-match-method
565 'header #'mime-calist::field-match-method-as-default-rule)
567 (define-calist-field-match-method
568 'body #'mime-calist::field-match-method-as-default-rule)
571 (defvar mime-preview-condition nil
572 "Condition-tree about how to display entity.")
574 (ctree-set-calist-strictly
575 'mime-preview-condition '((type . application)(subtype . octet-stream)
578 (ctree-set-calist-strictly
579 'mime-preview-condition '((type . application)(subtype . octet-stream)
582 (ctree-set-calist-strictly
583 'mime-preview-condition '((type . application)(subtype . octet-stream)
587 (ctree-set-calist-strictly
588 'mime-preview-condition '((type . application)(subtype . pgp)
591 (ctree-set-calist-strictly
592 'mime-preview-condition '((type . application)(subtype . x-latex)
595 (ctree-set-calist-strictly
596 'mime-preview-condition '((type . application)(subtype . x-selection)
599 (ctree-set-calist-strictly
600 'mime-preview-condition '((type . application)(subtype . x-comment)
603 (ctree-set-calist-strictly
604 'mime-preview-condition '((type . message)(subtype . delivery-status)
607 (ctree-set-calist-strictly
608 'mime-preview-condition
610 (body-presentation-method . mime-display-text/plain)))
612 (defvar mime-preview-fill-flowed-text
613 (module-installed-p 'flow-fill)
614 "If non-nil, fill RFC2646 \"flowed\" text.")
616 (autoload 'fill-flowed "flow-fill")
618 (ctree-set-calist-strictly
619 'mime-preview-condition
622 (body-presentation-method . mime-display-text/plain)))
624 (ctree-set-calist-strictly
625 'mime-preview-condition
626 '((type . text)(subtype . enriched)
628 (body-presentation-method . mime-display-text/enriched)))
630 (ctree-set-calist-strictly
631 'mime-preview-condition
632 '((type . text)(subtype . richtext)
634 (body-presentation-method . mime-display-text/richtext)))
636 (autoload 'mime-display-application/x-postpet "postpet")
638 (ctree-set-calist-strictly
639 'mime-preview-condition
640 '((type . application)(subtype . x-postpet)
642 (body-presentation-method . mime-display-application/x-postpet)))
644 (ctree-set-calist-strictly
645 'mime-preview-condition
646 '((type . text)(subtype . t)
648 (body-presentation-method . mime-display-text/plain)))
650 (ctree-set-calist-strictly
651 'mime-preview-condition
652 '((type . multipart)(subtype . alternative)
654 (body-presentation-method . mime-display-multipart/alternative)))
656 (ctree-set-calist-strictly
657 'mime-preview-condition
658 '((type . multipart)(subtype . t)
660 (body-presentation-method . mime-display-multipart/mixed)))
662 (ctree-set-calist-strictly
663 'mime-preview-condition
664 '((type . message)(subtype . partial)
666 (body-presentation-method . mime-display-message/partial-button)))
668 (ctree-set-calist-strictly
669 'mime-preview-condition
670 '((type . message)(subtype . rfc822)
672 (body-presentation-method . mime-display-multipart/mixed)
673 (childrens-situation (header . visible)
674 (entity-button . invisible))))
676 (ctree-set-calist-strictly
677 'mime-preview-condition
678 '((type . message)(subtype . news)
680 (body-presentation-method . mime-display-multipart/mixed)
681 (childrens-situation (header . visible)
682 (entity-button . invisible))))
685 ;;; @@@ entity presentation
688 (defun mime-display-text/plain (entity situation)
690 (narrow-to-region (point-max)(point-max))
692 (mime-insert-text-content entity)
694 (message "Can't decode current entity.")
696 (run-hooks 'mime-text-decode-hook)
697 (goto-char (point-max))
698 (if (not (eq (char-after (1- (point))) ?\n))
700 (if (and mime-preview-fill-flowed-text
701 (equal (cdr (assoc "format" situation)) "flowed"))
703 (mime-add-url-buttons)
704 (run-hooks 'mime-display-text/plain-hook)))
706 (defun mime-display-text/richtext (entity situation)
708 (narrow-to-region (point-max)(point-max))
709 (mime-insert-text-content entity)
710 (run-hooks 'mime-text-decode-hook)
711 (let ((beg (point-min)))
712 (remove-text-properties beg (point-max) '(face nil))
713 (richtext-decode beg (point-max)))))
715 (defun mime-display-text/enriched (entity situation)
717 (narrow-to-region (point-max)(point-max))
718 (mime-insert-text-content entity)
719 (run-hooks 'mime-text-decode-hook)
720 (let ((beg (point-min)))
721 (remove-text-properties beg (point-max) '(face nil))
722 (enriched-decode beg (point-max)))))
724 (defvar mime-view-announcement-for-message/partial
725 (if (and (>= emacs-major-version 19) window-system)
727 \[[ This is message/partial style split message. ]]
728 \[[ Please press `v' key in this buffer ]]
729 \[[ or click here by mouse button-2. ]]"
731 \[[ This is message/partial style split message. ]]
732 \[[ Please press `v' key in this buffer. ]]"))
734 (defun mime-display-message/partial-button (&optional entity situation)
736 (goto-char (point-max))
737 (if (not (search-backward "\n\n" nil t))
739 (goto-char (point-max))
740 (narrow-to-region (point-max)(point-max))
741 (insert mime-view-announcement-for-message/partial)
742 (mime-add-button (point-min)(point-max)
743 #'mime-preview-play-current-entity)))
745 (defun mime-display-multipart/mixed (entity situation)
746 (let ((children (mime-entity-children entity))
747 (original-major-mode-cell (assq 'major-mode situation))
749 (cdr (assq 'childrens-situation situation))))
750 (if original-major-mode-cell
751 (setq default-situation
752 (cons original-major-mode-cell default-situation)))
754 (mime-display-entity (car children) nil default-situation)
755 (setq children (cdr children)))))
757 (defcustom mime-view-type-subtype-score-alist
758 '(((text . enriched) . 3)
759 ((text . richtext) . 2)
762 "Alist MEDIA-TYPE vs corresponding score.
763 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
765 :type '(repeat (cons (choice :tag "Media-Type"
766 (cons :tag "Type/Subtype"
767 (symbol :tag "Primary-type")
768 (symbol :tag "Subtype"))
770 (const :tag "Default" t))
773 (defun mime-display-multipart/alternative (entity situation)
774 (let* ((children (mime-entity-children entity))
775 (original-major-mode-cell (assq 'major-mode situation))
777 (cdr (assq 'childrens-situation situation)))
782 (if original-major-mode-cell
783 (setq default-situation
784 (cons original-major-mode-cell default-situation)))
789 (mime-find-entity-preview-situation
790 child default-situation)))
791 (if (cdr (assq 'body-presentation-method situation))
796 (cdr (assq 'type situation))
797 (cdr (assq 'subtype situation)))
798 mime-view-type-subtype-score-alist)
800 (cdr (assq 'type situation))
801 mime-view-type-subtype-score-alist)
804 mime-view-type-subtype-score-alist)))))
805 (if (> score max-score)
813 (let ((child (car children))
814 (situation (car situations)))
815 (mime-display-entity child (if (= i p)
817 (put-alist 'body 'invisible
818 (copy-alist situation)))))
819 (setq children (cdr children)
820 situations (cdr situations)
824 ;;; @ acting-condition
827 (defvar mime-acting-condition nil
828 "Condition-tree about how to process entity.")
830 (if (file-readable-p mime-mailcap-file)
831 (let ((entries (mime-parse-mailcap-file)))
833 (let ((entry (car entries))
836 (let* ((field (car entry))
837 (field-type (car field)))
838 (cond ((eq field-type 'view) (setq view field))
839 ((eq field-type 'print) (setq print field))
840 ((memq field-type '(compose composetyped edit)))
841 (t (setq shared (cons field shared)))))
842 (setq entry (cdr entry)))
843 (setq shared (nreverse shared))
844 (ctree-set-calist-with-default
845 'mime-acting-condition
846 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
848 (ctree-set-calist-with-default
849 'mime-acting-condition
851 (list '(mode . "print")(cons 'method (cdr view)))))))
852 (setq entries (cdr entries)))))
854 (ctree-set-calist-strictly
855 'mime-acting-condition
856 '((type . application)(subtype . octet-stream)
858 (method . mime-detect-content)))
860 (ctree-set-calist-with-default
861 'mime-acting-condition
863 (method . mime-save-content)))
865 (ctree-set-calist-strictly
866 'mime-acting-condition
867 '((type . text)(subtype . x-rot13-47)(mode . "play")
868 (method . mime-view-caesar)))
869 (ctree-set-calist-strictly
870 'mime-acting-condition
871 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
872 (method . mime-view-caesar)))
874 (ctree-set-calist-strictly
875 'mime-acting-condition
876 '((type . message)(subtype . rfc822)(mode . "play")
877 (method . mime-view-message/rfc822)))
878 (ctree-set-calist-strictly
879 'mime-acting-condition
880 '((type . message)(subtype . partial)(mode . "play")
881 (method . mime-store-message/partial-piece)))
883 (ctree-set-calist-strictly
884 'mime-acting-condition
885 '((type . message)(subtype . external-body)
886 ("access-type" . "anon-ftp")
887 (method . mime-view-message/external-anon-ftp)))
889 (ctree-set-calist-strictly
890 'mime-acting-condition
891 '((type . message)(subtype . external-body)
892 ("access-type" . "url")
893 (method . mime-view-message/external-url)))
895 (ctree-set-calist-strictly
896 'mime-acting-condition
897 '((type . application)(subtype . octet-stream)
898 (method . mime-save-content)))
901 ;;; @ quitting method
904 (defvar mime-preview-quitting-method-alist
905 '((mime-show-message-mode
906 . mime-preview-quitting-method-for-mime-show-message-mode))
907 "Alist of major-mode vs. quitting-method of mime-view.")
909 (defvar mime-preview-over-to-previous-method-alist nil
910 "Alist of major-mode vs. over-to-previous-method of mime-view.")
912 (defvar mime-preview-over-to-next-method-alist nil
913 "Alist of major-mode vs. over-to-next-method of mime-view.")
916 ;;; @ following method
919 (defvar mime-preview-following-method-alist nil
920 "Alist of major-mode vs. following-method of mime-view.")
922 (defvar mime-view-following-required-fields-list
929 (defun mime-display-entity (entity &optional situation
930 default-situation preview-buffer)
932 (setq preview-buffer (current-buffer)))
933 (let* (e nb ne nhb nbb)
934 (in-calist-package 'mime-view)
937 (mime-find-entity-preview-situation entity default-situation)))
938 (let ((button-is-invisible
939 (eq (cdr (or (assq '*entity-button situation)
940 (assq 'entity-button situation)))
943 (eq (cdr (or (assq '*header situation)
944 (assq 'header situation)))
947 (eq (cdr (or (assq '*body situation)
948 (assq 'body situation)))
950 (children (mime-entity-children entity)))
951 (set-buffer preview-buffer)
953 (narrow-to-region nb nb)
954 (or button-is-invisible
955 ;; (if (mime-view-entity-button-visible-p entity)
956 (mime-view-insert-entity-button entity)
959 (if header-is-visible
960 (let ((header-presentation-method
961 (or (cdr (assq 'header-presentation-method situation))
962 (cdr (assq (cdr (assq 'major-mode situation))
963 mime-header-presentation-method-alist)))))
965 (if header-presentation-method
966 (funcall header-presentation-method entity situation)
967 (mime-insert-header entity
968 mime-view-ignored-field-list
969 mime-view-visible-field-list))
970 (mime-add-url-buttons)
971 (run-hooks 'mime-display-header-hook)
972 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
973 (goto-char (point-max))
978 (let ((body-presentation-method
979 (cdr (assq 'body-presentation-method situation))))
980 (if (functionp body-presentation-method)
981 (funcall body-presentation-method entity situation)
982 (mime-display-text/plain entity situation)))
983 (when button-is-invisible
984 (goto-char (point-max))
985 (mime-view-insert-entity-button entity))
986 (unless header-is-visible
987 (goto-char (point-max))
989 (setq ne (point-max))
991 (put-text-property nb ne 'mime-view-entity entity)
992 (put-text-property nb ne 'mime-view-situation situation)
993 (put-text-property nbb ne 'mime-view-entity-body entity)
995 (if (and children body-is-visible)
996 (let ((body-presentation-method
997 (cdr (assq 'body-presentation-method situation))))
998 (if (functionp body-presentation-method)
999 (funcall body-presentation-method entity situation)
1000 (mime-display-multipart/mixed entity situation)))))))
1003 ;;; @ MIME viewer mode
1006 (defconst mime-view-popup-menu-list
1008 ["Move to upper entity" mime-preview-move-to-upper]
1009 ["Move to previous entity" mime-preview-move-to-previous]
1010 ["Move to next entity" mime-preview-move-to-next]
1011 ["Scroll-down" mime-preview-scroll-down-entity]
1012 ["Scroll-up" mime-preview-scroll-up-entity]
1013 ["Play current entity" mime-preview-play-current-entity]
1014 ["Extract current entity" mime-preview-extract-current-entity]
1015 ["Print current entity" mime-preview-print-current-entity])
1016 "Menu for MIME Viewer")
1018 (defun mime-view-popup-menu (event)
1019 "Popup the menu in the MIME Viewer buffer"
1021 (mime-popup-menu-popup mime-view-popup-menu-list event))
1023 ;;; The current local map is taken precendence over `widget-keymap',
1024 ;;; because GNU Emacs' widget implementation doesn't set `local-map' property.
1025 ;;; So we need to specify derivation.
1026 (defvar widget-keymap)
1027 (defun mime-view-maybe-inherit-widget-keymap ()
1028 (when (boundp 'widget-keymap)
1029 (set-keymap-parent (current-local-map) widget-keymap)))
1031 (add-hook 'mime-view-define-keymap-hook 'mime-view-maybe-inherit-widget-keymap)
1033 (defun mime-view-define-keymap (&optional default)
1034 (let ((mime-view-mode-map (if (keymapp default)
1035 (copy-keymap default)
1036 (make-sparse-keymap))))
1037 (define-key mime-view-mode-map
1038 "u" (function mime-preview-move-to-upper))
1039 (define-key mime-view-mode-map
1040 "p" (function mime-preview-move-to-previous))
1041 (define-key mime-view-mode-map
1042 "n" (function mime-preview-move-to-next))
1043 (define-key mime-view-mode-map
1044 "\e\t" (function mime-preview-move-to-previous))
1045 (define-key mime-view-mode-map
1046 "\t" (function mime-preview-move-to-next))
1047 (define-key mime-view-mode-map
1048 " " (function mime-preview-scroll-up-entity))
1049 (define-key mime-view-mode-map
1050 "\M- " (function mime-preview-scroll-down-entity))
1051 (define-key mime-view-mode-map
1052 "\177" (function mime-preview-scroll-down-entity))
1053 (define-key mime-view-mode-map
1054 "\C-m" (function mime-preview-next-line-entity))
1055 (define-key mime-view-mode-map
1056 "\C-\M-m" (function mime-preview-previous-line-entity))
1057 (define-key mime-view-mode-map
1058 "v" (function mime-preview-play-current-entity))
1059 (define-key mime-view-mode-map
1060 "e" (function mime-preview-extract-current-entity))
1061 (define-key mime-view-mode-map
1062 "\C-c\C-p" (function mime-preview-print-current-entity))
1064 (define-key mime-view-mode-map
1065 "\C-c\C-t\C-f" (function mime-preview-toggle-header))
1066 (define-key mime-view-mode-map
1067 "\C-c\C-th" (function mime-preview-toggle-header))
1068 (define-key mime-view-mode-map
1069 "\C-c\C-t\C-c" (function mime-preview-toggle-content))
1071 (define-key mime-view-mode-map
1072 "\C-c\C-v\C-f" (function mime-preview-show-header))
1073 (define-key mime-view-mode-map
1074 "\C-c\C-vh" (function mime-preview-show-header))
1075 (define-key mime-view-mode-map
1076 "\C-c\C-v\C-c" (function mime-preview-show-content))
1078 (define-key mime-view-mode-map
1079 "\C-c\C-d\C-f" (function mime-preview-hide-header))
1080 (define-key mime-view-mode-map
1081 "\C-c\C-dh" (function mime-preview-hide-header))
1082 (define-key mime-view-mode-map
1083 "\C-c\C-d\C-c" (function mime-preview-hide-content))
1085 (define-key mime-view-mode-map
1086 "a" (function mime-preview-follow-current-entity))
1087 (define-key mime-view-mode-map
1088 "q" (function mime-preview-quit))
1089 (define-key mime-view-mode-map
1090 "\C-c\C-x" (function mime-preview-kill-buffer))
1091 ;; (define-key mime-view-mode-map
1092 ;; "<" (function beginning-of-buffer))
1093 ;; (define-key mime-view-mode-map
1094 ;; ">" (function end-of-buffer))
1095 (define-key mime-view-mode-map
1096 "?" (function describe-mode))
1097 (define-key mime-view-mode-map
1098 [tab] (function mime-preview-move-to-next))
1099 (define-key mime-view-mode-map
1100 [delete] (function mime-preview-scroll-down-entity))
1101 (define-key mime-view-mode-map
1102 [backspace] (function mime-preview-scroll-down-entity))
1103 (if (functionp default)
1104 (static-if (featurep 'xemacs)
1105 (set-keymap-default-binding mime-view-mode-map default)
1106 (setq mime-view-mode-map
1107 (append mime-view-mode-map (list (cons t default))))))
1108 (define-key mime-view-mode-map
1109 [down-mouse-3] (function mime-view-popup-menu))
1110 (use-local-map mime-view-mode-map)
1111 (run-hooks 'mime-view-define-keymap-hook)))
1113 (defsubst mime-maybe-hide-echo-buffer ()
1114 "Clear mime-echo buffer and delete window for it."
1115 (let ((buf (get-buffer mime-echo-buffer-name)))
1120 (let ((win (get-buffer-window buf)))
1122 (delete-window win)))
1123 (bury-buffer buf)))))
1125 (defvar mime-view-redisplay nil)
1128 (defun mime-display-message (message &optional preview-buffer
1129 mother default-keymap-or-function
1130 original-major-mode)
1131 "View MESSAGE in MIME-View mode.
1133 Optional argument PREVIEW-BUFFER specifies the buffer of the
1134 presentation. It must be either nil or a name of preview buffer.
1136 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1138 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1139 function. If it is a keymap, keymap of MIME-View mode will be added
1140 to it. If it is a function, it will be bound as default binding of
1141 keymap of MIME-View mode."
1142 (mime-maybe-hide-echo-buffer)
1143 (let ((win-conf (current-window-configuration)))
1145 (setq preview-buffer
1146 (concat "*Preview-" (mime-entity-name message) "*")))
1147 (or original-major-mode
1148 (setq original-major-mode major-mode))
1149 (let ((inhibit-read-only t))
1150 (set-buffer (get-buffer-create preview-buffer))
1154 (setq mime-mother-buffer mother))
1155 (setq mime-preview-original-window-configuration win-conf)
1156 (setq major-mode 'mime-view-mode)
1157 (setq mode-name "MIME-View")
1158 (mime-display-entity message nil
1159 `((entity-button . invisible)
1161 (major-mode . ,original-major-mode))
1163 (mime-view-define-keymap default-keymap-or-function)
1165 (next-single-property-change (point-min) 'mime-view-entity)))
1168 (goto-char (point-min))
1169 (search-forward "\n\n" nil t)))
1170 (run-hooks 'mime-view-mode-hook)
1171 (set-buffer-modified-p nil)
1172 (setq buffer-read-only t)
1176 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1177 default-keymap-or-function
1178 representation-type)
1179 "View RAW-BUFFER in MIME-View mode.
1180 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1182 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1183 function. If it is a keymap, keymap of MIME-View mode will be added
1184 to it. If it is a function, it will be bound as default binding of
1185 keymap of MIME-View mode.
1186 Optional argument REPRESENTATION-TYPE is representation-type of
1187 message. It must be nil, `binary' or `cooked'. If it is nil,
1188 `cooked' is used as default."
1191 (setq raw-buffer (current-buffer)))
1192 (or representation-type
1193 (setq representation-type
1195 (set-buffer raw-buffer)
1196 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1197 (assq t mime-raw-representation-type-alist))))))
1198 (if (eq representation-type 'binary)
1199 (setq representation-type 'buffer))
1200 (setq preview-buffer (mime-display-message
1201 (mime-open-entity representation-type raw-buffer)
1202 preview-buffer mother default-keymap-or-function))
1203 (or (get-buffer-window preview-buffer)
1204 (let ((r-win (get-buffer-window raw-buffer)))
1206 (set-window-buffer r-win preview-buffer)
1207 (let ((m-win (and mother (get-buffer-window mother))))
1209 (set-window-buffer m-win preview-buffer)
1210 (switch-to-buffer preview-buffer)))))))
1212 (defun mime-view-mode (&optional mother ctl encoding
1213 raw-buffer preview-buffer
1214 default-keymap-or-function)
1215 "Major mode for viewing MIME message.
1217 Here is a list of the standard keys for mime-view-mode.
1222 u Move to upper content
1223 p or M-TAB Move to previous content
1224 n or TAB Move to next content
1225 SPC Scroll up or move to next content
1226 M-SPC or DEL Scroll down or move to previous content
1227 RET Move to next line
1228 M-RET Move to previous line
1229 v Decode current content as `play mode'
1230 e Decode current content as `extract mode'
1231 C-c C-p Decode current content as `print mode'
1232 a Followup to current content.
1234 button-2 Move to point under the mouse cursor
1235 and decode current content as `play mode'
1238 (unless mime-view-redisplay
1240 (if raw-buffer (set-buffer raw-buffer))
1243 (or (assq major-mode mime-raw-representation-type-alist)
1244 (assq t mime-raw-representation-type-alist)))))
1245 (if (eq type 'binary)
1246 (setq type 'buffer))
1247 (setq mime-message-structure (mime-open-entity type raw-buffer))
1248 (or (mime-entity-content-type mime-message-structure)
1249 (mime-entity-set-content-type mime-message-structure ctl)))
1250 (or (mime-entity-encoding mime-message-structure)
1251 (mime-entity-set-encoding mime-message-structure encoding))))
1252 (mime-display-message mime-message-structure preview-buffer
1253 mother default-keymap-or-function))
1259 (defun mime-preview-find-boundary-info (&optional get-mother)
1263 (while (null (setq entity
1264 (get-text-property (point) 'mime-view-entity)))
1266 (setq p-beg (previous-single-property-change (point) 'mime-view-entity))
1267 (setq entity-node-id (mime-entity-node-id entity))
1268 (setq len (length entity-node-id))
1271 (if (eq (next-single-property-change (point-min)
1276 ((eq (next-single-property-change p-beg 'mime-view-entity)
1278 (setq p-beg (point))))
1279 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1281 (setq p-end (point-max)))
1282 ((null entity-node-id)
1283 (setq p-end (point-max)))
1290 (next-single-property-change
1291 (point) 'mime-view-entity))
1293 (let ((rc (mime-entity-node-id
1294 (get-text-property (1- (point))
1295 'mime-view-entity))))
1296 (or (and (>= (setq i (- (length rc) len)) 0)
1297 (equal entity-node-id (nthcdr i rc)))
1300 (setq p-end (point-max))))))
1301 (vector p-beg p-end entity)))
1307 (autoload 'mime-preview-play-current-entity "mime-play"
1308 "Play current entity." t)
1310 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1311 "Extract current entity into file (maybe).
1312 It decodes current entity to call internal or external method as
1313 \"extract\" mode. The method is selected from variable
1314 `mime-acting-condition'."
1316 (mime-preview-play-current-entity ignore-examples "extract"))
1318 (defun mime-preview-print-current-entity (&optional ignore-examples)
1319 "Print current entity (maybe).
1320 It decodes current entity to call internal or external method as
1321 \"print\" mode. The method is selected from variable
1322 `mime-acting-condition'."
1324 (mime-preview-play-current-entity ignore-examples "print"))
1330 (defun mime-preview-follow-current-entity ()
1331 "Write follow message to current entity.
1332 It calls following-method selected from variable
1333 `mime-preview-following-method-alist'."
1335 (let ((entity (mime-preview-find-boundary-info t))
1338 (setq p-beg (aref entity 0)
1339 p-end (aref entity 1)
1340 entity (aref entity 2))
1341 (if (get-text-property p-beg 'mime-view-entity-body)
1344 (next-single-property-change
1345 p-beg 'mime-view-entity-body nil
1346 (or (next-single-property-change p-beg 'mime-view-entity)
1348 (let* ((mode (mime-preview-original-major-mode 'recursive))
1349 (entity-node-id (mime-entity-node-id entity))
1351 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1353 (the-buf (current-buffer))
1356 (set-buffer (setq new-buf (get-buffer-create new-name)))
1359 (insert-buffer-substring the-buf pb-beg p-end)
1360 (goto-char (point-min))
1361 (let ((current-entity
1362 (if (and (eq (mime-entity-media-type entity) 'message)
1363 (eq (mime-entity-media-subtype entity) 'rfc822))
1364 (car (mime-entity-children entity))
1366 (while (and current-entity
1367 (if (and (eq (mime-entity-media-type
1368 current-entity) 'message)
1369 (eq (mime-entity-media-subtype
1370 current-entity) 'rfc822))
1372 (mime-insert-header current-entity fields)
1374 (setq fields (std11-collect-field-names)
1375 current-entity (mime-entity-parent current-entity))))
1376 (let ((rest mime-view-following-required-fields-list)
1379 (setq field-name (car rest))
1380 (or (std11-field-body field-name)
1383 (set-buffer the-buf)
1384 (let ((entity (when mime-mother-buffer
1385 (set-buffer mime-mother-buffer)
1386 (get-text-property (point)
1387 'mime-view-entity))))
1389 (null (setq ret (mime-entity-fetch-field
1390 entity field-name))))
1391 (setq entity (mime-entity-parent entity)))))
1393 (insert (concat field-name ": " ret "\n")))))
1394 (setq rest (cdr rest)))))
1395 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1400 "Sorry, following method for %s is not implemented yet."
1407 (defun mime-preview-move-to-upper ()
1408 "Move to upper entity.
1409 If there is no upper entity, call function `mime-preview-quit'."
1412 (while (null (setq cinfo
1413 (get-text-property (point) 'mime-view-entity)))
1415 (let ((r (mime-entity-parent cinfo))
1418 (while (setq point (previous-single-property-change
1419 (point) 'mime-view-entity))
1421 (when (eq r (get-text-property (point) 'mime-view-entity))
1422 (if (or (eq mime-preview-move-scroll t)
1423 (and mime-preview-move-scroll
1426 (move-to-window-line -1)
1427 (forward-line (* -1 next-screen-context-lines))
1430 (recenter next-screen-context-lines))
1432 (mime-preview-quit)))))
1434 (defun mime-preview-move-to-previous ()
1435 "Move to previous entity.
1436 If there is no previous entity, it calls function registered in
1437 variable `mime-preview-over-to-previous-method-alist'."
1439 (while (and (not (bobp))
1440 (null (get-text-property (point) 'mime-view-entity)))
1442 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1444 (>= point (point-min)))
1445 (if (get-text-property (1- point) 'mime-view-entity)
1446 (progn (goto-char point)
1448 (or (eq mime-preview-move-scroll t)
1449 (and mime-preview-move-scroll
1452 (move-to-window-line 0)
1453 (forward-line next-screen-context-lines)
1456 (recenter (* -1 next-screen-context-lines))))
1457 (goto-char (1- point))
1458 (mime-preview-move-to-previous))
1459 (let ((f (assq (mime-preview-original-major-mode)
1460 mime-preview-over-to-previous-method-alist)))
1462 (funcall (cdr f)))))))
1464 (defun mime-preview-move-to-next ()
1465 "Move to next entity.
1466 If there is no previous entity, it calls function registered in
1467 variable `mime-preview-over-to-next-method-alist'."
1469 (while (and (not (eobp))
1470 (null (get-text-property (point) 'mime-view-entity)))
1472 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1474 (<= point (point-max)))
1477 (if (null (get-text-property point 'mime-view-entity))
1478 (mime-preview-move-to-next)
1480 (or (eq mime-preview-move-scroll t)
1481 (and mime-preview-move-scroll
1484 (move-to-window-line -1)
1486 (* -1 next-screen-context-lines))
1489 (recenter next-screen-context-lines))))
1490 (let ((f (assq (mime-preview-original-major-mode)
1491 mime-preview-over-to-next-method-alist)))
1493 (funcall (cdr f)))))))
1495 (defun mime-preview-scroll-up-entity (&optional h)
1496 "Scroll up current entity.
1497 If reached to (point-max), it calls function registered in variable
1498 `mime-preview-over-to-next-method-alist'."
1501 (let ((f (assq (mime-preview-original-major-mode)
1502 mime-preview-over-to-next-method-alist)))
1506 (or (next-single-property-change (point) 'mime-view-entity)
1508 (bottom (window-end (selected-window))))
1511 (progn (goto-char point)
1512 (recenter next-screen-context-lines))
1516 (goto-char (point-max))))))))
1518 (defun mime-preview-scroll-down-entity (&optional h)
1519 "Scroll down current entity.
1520 If reached to (point-min), it calls function registered in variable
1521 `mime-preview-over-to-previous-method-alist'."
1524 (let ((f (assq (mime-preview-original-major-mode)
1525 mime-preview-over-to-previous-method-alist)))
1529 (or (previous-single-property-change (point) 'mime-view-entity)
1531 (top (window-start (selected-window))))
1534 (progn (goto-char point)
1535 (recenter (* -1 next-screen-context-lines)))
1538 (beginning-of-buffer
1539 (goto-char (point-min))))))))
1541 (defun mime-preview-next-line-entity (&optional lines)
1542 "Scroll up one line (or prefix LINES lines).
1543 If LINES is negative, scroll down LINES lines."
1545 (mime-preview-scroll-up-entity (or lines 1)))
1547 (defun mime-preview-previous-line-entity (&optional lines)
1548 "Scrroll down one line (or prefix LINES lines).
1549 If LINES is negative, scroll up LINES lines."
1551 (mime-preview-scroll-down-entity (or lines 1)))
1557 (defun mime-preview-toggle-display (type &optional display)
1558 (let ((situation (mime-preview-find-boundary-info))
1559 (sym (intern (concat "*" (symbol-name type))))
1561 (setq p-beg (aref situation 0)
1562 p-end (aref situation 1)
1563 entity (aref situation 2)
1564 situation (get-text-property p-beg 'mime-view-situation))
1565 (cond ((eq display 'invisible)
1570 (eq (cdr (or (assq sym situation)
1571 (assq type situation)))
1573 (setq situation (put-alist sym (if display
1578 (let ((inhibit-read-only t))
1579 (delete-region p-beg p-end)
1580 (mime-display-entity entity situation)))
1581 (let ((ret (assoc situation mime-preview-situation-example-list)))
1583 (setcdr ret (1+ (cdr ret)))
1584 (add-to-list 'mime-preview-situation-example-list
1585 (cons situation 0))))))
1587 (defun mime-preview-toggle-header (&optional force-visible)
1589 (mime-preview-toggle-display 'header force-visible))
1591 (defun mime-preview-toggle-content (&optional force-visible)
1593 (mime-preview-toggle-display 'body force-visible))
1595 (defun mime-preview-show-header ()
1597 (mime-preview-toggle-display 'header 'visible))
1599 (defun mime-preview-show-content ()
1601 (mime-preview-toggle-display 'body 'visible))
1603 (defun mime-preview-hide-header ()
1605 (mime-preview-toggle-display 'header 'invisible))
1607 (defun mime-preview-hide-content ()
1609 (mime-preview-toggle-display 'body 'invisible))
1615 (defun mime-preview-quit ()
1616 "Quit from MIME-preview buffer.
1617 It calls function registered in variable
1618 `mime-preview-quitting-method-alist'."
1620 (let ((r (assq (mime-preview-original-major-mode)
1621 mime-preview-quitting-method-alist)))
1623 (funcall (cdr r)))))
1625 (defun mime-preview-kill-buffer ()
1627 (kill-buffer (current-buffer)))
1633 (provide 'mime-view)
1635 (let ((file mime-situation-examples-file))
1636 (if (file-readable-p file)
1638 (insert-file-contents file)
1639 (setq mime-situation-examples-file-coding-system
1641 ((boundp 'buffer-file-coding-system)
1642 (symbol-value 'buffer-file-coding-system))
1643 ((boundp 'file-coding-system)
1644 (symbol-value 'file-coding-system))
1650 (while (and (> (length mime-preview-situation-example-list)
1651 mime-preview-situation-example-list-max-size)
1653 (setq mime-preview-situation-example-list
1654 (mime-reduce-situation-examples
1655 mime-preview-situation-example-list))
1657 (error (setq mime-preview-situation-example-list nil)))
1658 ;; (let ((rest mime-preview-situation-example-list))
1660 ;; (ctree-set-calist-strictly 'mime-preview-condition
1662 ;; (setq rest (cdr rest))))
1665 (while (and (> (length mime-acting-situation-example-list)
1666 mime-acting-situation-example-list-max-size)
1668 (setq mime-acting-situation-example-list
1669 (mime-reduce-situation-examples
1670 mime-acting-situation-example-list))
1672 (error (setq mime-acting-situation-example-list nil))))))
1674 ;;; mime-view.el ends here