1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
7 ;; Renamed: 1994/08/31 from tm-body.el
8 ;; Renamed: 1997/02/19 from tm-view.el
9 ;; Keywords: MIME, multimedia, mail, news
11 ;; This file is part of SEMI (Sample of Elastic MIME Interfaces).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
41 (defconst mime-view-version
42 (concat (mime-product-name mime-user-interface-product) " MIME-View "
43 (mapconcat #'number-to-string
44 (mime-product-version mime-user-interface-product) ".")
45 " (" (mime-product-code-name mime-user-interface-product) ")"))
51 (defgroup mime-view nil
55 (defcustom mime-view-find-every-acting-situation t
56 "*Find every available acting-situation if non-nil."
60 (defcustom mime-acting-situation-examples-file "~/.mime-example"
61 "*File name of example about acting-situation demonstrated by user."
65 (defcustom mime-preview-move-scroll nil
66 "*Decides whether to scroll when moving to next entity.
67 When t, scroll the buffer. Non-nil but not t means scroll when
68 the next entity is within next-screen-context-lines from top or
69 buttom. Nil means don't scroll at all."
71 :type '(choice (const :tag "Off" nil)
73 (sexp :tag "Situation" 1)))
75 ;;; @ in raw-buffer (representation space)
78 (defvar mime-preview-buffer nil
79 "MIME-preview buffer corresponding with the (raw) buffer.")
80 (make-variable-buffer-local 'mime-preview-buffer)
83 (defvar mime-raw-representation-type-alist
84 '((mime-show-message-mode . binary)
85 (mime-temp-message-mode . binary)
88 "Alist of major-mode vs. representation-type of mime-raw-buffer.
89 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
90 major-mode or t. t means default. REPRESENTATION-TYPE must be
91 `binary' or `cooked'.")
94 ;; (defun mime-raw-find-entity-from-point (point &optional message-info)
95 ;; "Return entity from POINT in mime-raw-buffer.
96 ;; If optional argument MESSAGE-INFO is not specified,
97 ;; `mime-message-structure' is used."
99 ;; (setq message-info mime-message-structure))
100 ;; (if (and (<= (mime-entity-point-min message-info) point)
101 ;; (<= point (mime-entity-point-max message-info)))
102 ;; (let ((children (mime-entity-children message-info)))
106 ;; (mime-raw-find-entity-from-point point (car children))))
110 ;; (setq children (cdr children)))
112 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
115 ;;; @ in preview-buffer (presentation space)
118 (defvar mime-mother-buffer nil
119 "Mother buffer corresponding with the (MIME-preview) buffer.
120 If current MIME-preview buffer is generated by other buffer, such as
121 message/partial, it is called `mother-buffer'.")
122 (make-variable-buffer-local 'mime-mother-buffer)
124 ;; (defvar mime-raw-buffer nil
125 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
126 ;; (make-variable-buffer-local 'mime-raw-buffer)
128 (defvar mime-preview-original-window-configuration nil
129 "Window-configuration before mime-view-mode is called.")
130 (make-variable-buffer-local 'mime-preview-original-window-configuration)
132 (defun mime-preview-original-major-mode (&optional recursive point)
133 "Return major-mode of original buffer.
134 If optional argument RECURSIVE is non-nil and current buffer has
135 mime-mother-buffer, it returns original major-mode of the
137 (if (and recursive mime-mother-buffer)
139 (set-buffer mime-mother-buffer)
140 (mime-preview-original-major-mode recursive)
142 (cdr (assq 'major-mode
143 (get-text-property (or point
144 (if (> (point) (buffer-size))
145 (max (1- (point-max)) (point-min))
147 'mime-view-situation)))))
150 ;;; @ entity information
153 (defun mime-entity-situation (entity &optional situation)
154 "Return situation of ENTITY."
155 (let (rest param name)
157 (unless (assq 'type situation)
158 (setq rest (or (mime-entity-content-type entity)
159 (make-mime-content-type 'text 'plain))
160 situation (cons (car rest) situation)
163 (unless (assq 'subtype situation)
165 (setq rest (or (cdr (mime-entity-content-type entity))
166 '((subtype . plain)))))
167 (setq situation (cons (car rest) situation)
171 (setq param (car rest))
172 (or (assoc (car param) situation)
173 (setq situation (cons param situation)))
174 (setq rest (cdr rest)))
176 ;; Content-Disposition
178 (unless (assq 'disposition-type situation)
179 (setq rest (mime-entity-content-disposition entity))
181 (setq situation (cons (cons 'disposition-type
182 (mime-content-disposition-type rest))
184 rest (mime-content-disposition-parameters rest))
187 (setq param (car rest)
189 (if (cond ((string= name "filename")
190 (if (assq 'filename situation)
192 (setq name 'filename)))
193 ((string= name "creation-date")
194 (if (assq 'creation-date situation)
196 (setq name 'creation-date)))
197 ((string= name "modification-date")
198 (if (assq 'modification-date situation)
200 (setq name 'modification-date)))
201 ((string= name "read-date")
202 (if (assq 'read-date situation)
204 (setq name 'read-date)))
205 ((string= name "size")
206 (if (assq 'size situation)
209 (t (setq name (cons 'disposition name))
210 (if (assoc name situation)
214 (cons (cons name (cdr param))
216 (setq rest (cdr rest)))
218 ;; Content-Transfer-Encoding
219 (or (assq 'encoding situation)
221 (cons (cons 'encoding (or (mime-entity-encoding entity)
227 (defsubst mime-delq-null-situation (situations field
228 &optional ignored-value)
231 (let* ((situation (car situations))
232 (cell (assq field situation)))
234 (or (eq (cdr cell) ignored-value)
235 (setq dest (cons situation dest))
237 (setq situations (cdr situations)))
240 (defun mime-compare-situation-with-example (situation example)
241 (let ((example (copy-alist example))
244 (let* ((cell (car situation))
246 (ecell (assoc key example)))
248 (if (equal cell ecell)
249 (setq match (1+ match))
250 (setq example (delq ecell example))
253 (setq situation (cdr situation))
258 (defun mime-sort-situation (situation)
268 (disposition-type . 6)
272 (let ((ret (assq a-t order)))
274 (setq a-order (cdr ret))
280 (let ((ret (assq b-t order)))
282 (setq b-order (cdr ret))
287 (if (= a-order b-order)
288 (string< (format "%s" a-t)(format "%s" b-t))
293 (defun mime-unify-situations (entity-situation condition situation-examples
294 &optional ignored-method)
296 (in-calist-package 'mime-view)
298 (mime-delq-null-situation
299 (ctree-find-calist condition entity-situation
300 mime-view-find-every-acting-situation)
301 'method ignored-method))
302 (or (assq 'ignore-examples entity-situation)
310 (let ((situation (car rest))
311 (examples situation-examples))
314 (mime-compare-situation-with-example
315 situation (caar examples)))
316 (ret-score (car ret)))
317 (cond ((> ret-score max-score)
318 (setq max-score ret-score
319 max-escore (cdar examples)
320 max-examples (list (cdr ret))
321 max-situations (list situation))
323 ((= ret-score max-score)
324 (cond ((> (cdar examples) max-escore)
325 (setq max-escore (cdar examples)
326 max-examples (list (cdr ret))
327 max-situations (list situation))
329 ((= (cdar examples) max-escore)
331 (cons (cdr ret) max-examples))
332 (or (member situation max-situations)
334 (cons situation max-situations)))
336 (setq examples (cdr examples))))
337 (setq rest (cdr rest)))
339 (setq ret max-situations)
341 (let* ((example (car max-examples))
343 (assoc example situation-examples)))
345 (setcdr cell (1+ (cdr cell)))
346 (setq situation-examples
347 (cons (cons example 0)
350 (setq max-examples (cdr max-examples))
352 (cons ret situation-examples)
353 ;; ret: list of situations
354 ;; situation-examples: new examples (notoce that contents of
355 ;; argument `situation-examples' has bees modified)
358 (defun mime-view-entity-title (entity)
359 (or (mime-entity-read-field entity 'Content-Description)
360 (mime-entity-read-field entity 'Subject)
361 (mime-entity-filename entity)
365 ;;; @ presentation of preview
371 ;;; @@@ predicate function
374 (defun mime-view-entity-button-visible-p (entity)
375 "Return non-nil if header of ENTITY is visible.
376 Please redefine this function if you want to change default setting."
377 (let ((media-type (mime-entity-media-type entity))
378 (media-subtype (mime-entity-media-subtype entity)))
379 (or (not (eq media-type 'application))
380 (and (not (eq media-subtype 'x-selection))
381 (or (not (eq media-subtype 'octet-stream))
382 (let ((mother-entity (mime-entity-parent entity)))
383 (or (not (eq (mime-entity-media-type mother-entity)
385 (not (eq (mime-entity-media-subtype mother-entity)
390 ;;; @@@ entity button generator
393 (defun mime-view-insert-entity-button (entity)
394 "Insert entity-button of ENTITY."
395 (let ((entity-node-id (mime-entity-node-id entity))
396 (params (mime-entity-parameters entity))
397 (subject (mime-view-entity-title entity)))
399 (let ((access-type (assoc "access-type" params))
400 (num (or (cdr (assoc "x-part-number" params))
401 (if (consp entity-node-id)
404 (format "%s" (1+ num))
406 (reverse entity-node-id) ".")
410 (let ((server (assoc "server" params)))
411 (setq access-type (cdr access-type))
413 (format "%s %s ([%s] %s)"
414 num subject access-type (cdr server))
415 (let ((site (cdr (assoc "site" params)))
416 (dir (cdr (assoc "directory" params)))
417 (url (cdr (assoc "url" params)))
420 (format "%s %s ([%s] %s)"
421 num subject access-type url)
422 (format "%s %s ([%s] %s:%s)"
423 num subject access-type site dir))
427 (let ((media-type (mime-entity-media-type entity))
428 (media-subtype (mime-entity-media-subtype entity))
429 (charset (cdr (assoc "charset" params)))
430 (encoding (mime-entity-encoding entity)))
434 (format " <%s/%s%s%s>"
435 media-type media-subtype
437 (concat "; " charset)
440 (concat " (" encoding ")")
442 (if (>= (+ (current-column)(length rest))(window-width))
446 (function mime-preview-play-current-entity))
453 (defvar mime-header-presentation-method-alist nil
454 "Alist of major mode vs. corresponding header-presentation-method functions.
455 Each element looks like (SYMBOL . FUNCTION).
456 SYMBOL must be major mode in raw-buffer or t. t means default.
457 Interface of FUNCTION must be (ENTITY SITUATION).")
459 (defvar mime-view-ignored-field-list
460 '(".*Received:" ".*Path:" ".*Id:" "^References:"
461 "^Replied:" "^Errors-To:"
462 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
463 "^Content-Type:" "^Precedence:"
464 "^Status:" "^X-VM-.*:")
465 "All fields that match this list will be hidden in MIME preview buffer.
466 Each elements are regexp of field-name.")
468 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
469 "All fields that match this list will be displayed in MIME preview buffer.
470 Each elements are regexp of field-name.")
476 ;;; @@@ predicate function
479 (in-calist-package 'mime-view)
481 (defun mime-calist::field-match-method-as-default-rule (calist
482 field-type field-value)
483 (let ((s-field (assq field-type calist)))
484 (cond ((null s-field)
485 (cons (cons field-type field-value) calist)
489 (define-calist-field-match-method
490 'header #'mime-calist::field-match-method-as-default-rule)
492 (define-calist-field-match-method
493 'body #'mime-calist::field-match-method-as-default-rule)
496 (defvar mime-preview-condition nil
497 "Condition-tree about how to display entity.")
499 (ctree-set-calist-strictly
500 'mime-preview-condition '((type . application)(subtype . octet-stream)
503 (ctree-set-calist-strictly
504 'mime-preview-condition '((type . application)(subtype . octet-stream)
507 (ctree-set-calist-strictly
508 'mime-preview-condition '((type . application)(subtype . octet-stream)
512 (ctree-set-calist-strictly
513 'mime-preview-condition '((type . application)(subtype . pgp)
516 (ctree-set-calist-strictly
517 'mime-preview-condition '((type . application)(subtype . x-latex)
520 (ctree-set-calist-strictly
521 'mime-preview-condition '((type . application)(subtype . x-selection)
524 (ctree-set-calist-strictly
525 'mime-preview-condition '((type . application)(subtype . x-comment)
528 (ctree-set-calist-strictly
529 'mime-preview-condition '((type . message)(subtype . delivery-status)
532 (ctree-set-calist-strictly
533 'mime-preview-condition
535 (body-presentation-method . mime-display-text/plain)))
537 (ctree-set-calist-strictly
538 'mime-preview-condition
541 (body-presentation-method . mime-display-text/plain)))
543 (ctree-set-calist-strictly
544 'mime-preview-condition
545 '((type . text)(subtype . enriched)
547 (body-presentation-method . mime-display-text/enriched)))
549 (ctree-set-calist-strictly
550 'mime-preview-condition
551 '((type . text)(subtype . richtext)
553 (body-presentation-method . mime-display-text/richtext)))
555 (autoload 'mime-display-application/x-postpet "postpet")
557 (ctree-set-calist-strictly
558 'mime-preview-condition
559 '((type . application)(subtype . x-postpet)
561 (body-presentation-method . mime-display-application/x-postpet)))
563 (ctree-set-calist-strictly
564 'mime-preview-condition
565 '((type . text)(subtype . t)
567 (body-presentation-method . mime-display-text/plain)))
569 (ctree-set-calist-strictly
570 'mime-preview-condition
571 '((type . multipart)(subtype . alternative)
573 (body-presentation-method . mime-display-multipart/alternative)))
575 (ctree-set-calist-strictly
576 'mime-preview-condition '((type . message)(subtype . partial)
577 (body-presentation-method
578 . mime-display-message/partial-button)))
580 (ctree-set-calist-strictly
581 'mime-preview-condition '((type . message)(subtype . rfc822)
582 (body-presentation-method . nil)
583 (childrens-situation (header . visible)
584 (entity-button . invisible))))
586 (ctree-set-calist-strictly
587 'mime-preview-condition '((type . message)(subtype . news)
588 (body-presentation-method . nil)
589 (childrens-situation (header . visible)
590 (entity-button . invisible))))
593 ;;; @@@ entity presentation
596 (defun mime-display-text/plain (entity situation)
598 (narrow-to-region (point-max)(point-max))
599 (mime-insert-text-content entity)
600 (run-hooks 'mime-text-decode-hook)
601 (goto-char (point-max))
602 (if (not (eq (char-after (1- (point))) ?\n))
605 (mime-add-url-buttons)
606 (run-hooks 'mime-display-text/plain-hook)
609 (defun mime-display-text/richtext (entity situation)
611 (narrow-to-region (point-max)(point-max))
612 (mime-insert-text-content entity)
613 (run-hooks 'mime-text-decode-hook)
614 (let ((beg (point-min)))
615 (remove-text-properties beg (point-max) '(face nil))
616 (richtext-decode beg (point-max))
619 (defun mime-display-text/enriched (entity situation)
621 (narrow-to-region (point-max)(point-max))
622 (mime-insert-text-content entity)
623 (run-hooks 'mime-text-decode-hook)
624 (let ((beg (point-min)))
625 (remove-text-properties beg (point-max) '(face nil))
626 (enriched-decode beg (point-max))
630 (defvar mime-view-announcement-for-message/partial
631 (if (and (>= emacs-major-version 19) window-system)
633 \[[ This is message/partial style split message. ]]
634 \[[ Please press `v' key in this buffer ]]
635 \[[ or click here by mouse button-2. ]]"
637 \[[ This is message/partial style split message. ]]
638 \[[ Please press `v' key in this buffer. ]]"
641 (defun mime-display-message/partial-button (&optional entity situation)
643 (goto-char (point-max))
644 (if (not (search-backward "\n\n" nil t))
647 (goto-char (point-max))
648 (narrow-to-region (point-max)(point-max))
649 (insert mime-view-announcement-for-message/partial)
650 (mime-add-button (point-min)(point-max)
651 #'mime-preview-play-current-entity)
654 (defun mime-display-multipart/mixed (entity situation)
655 (let ((children (mime-entity-children entity))
656 (original-major-mode-cell (assq 'major-mode situation))
658 (cdr (assq 'childrens-situation situation))))
659 (if original-major-mode-cell
660 (setq default-situation
661 (cons original-major-mode-cell default-situation)))
663 (mime-display-entity (car children) nil default-situation)
664 (setq children (cdr children))
667 (defcustom mime-view-type-subtype-score-alist
668 '(((text . enriched) . 3)
669 ((text . richtext) . 2)
672 "Alist MEDIA-TYPE vs corresponding score.
673 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
675 :type '(repeat (cons (choice :tag "Media-Type"
676 (cons :tag "Type/Subtype"
677 (symbol :tag "Primary-type")
678 (symbol :tag "Subtype"))
680 (const :tag "Default" t))
683 (defun mime-display-multipart/alternative (entity situation)
684 (let* ((children (mime-entity-children entity))
685 (original-major-mode-cell (assq 'major-mode situation))
687 (cdr (assq 'childrens-situation situation)))
692 (if original-major-mode-cell
693 (setq default-situation
694 (cons original-major-mode-cell default-situation)))
699 (or (ctree-match-calist
700 mime-preview-condition
701 (append (mime-entity-situation child)
704 (if (cdr (assq 'body-presentation-method situation))
709 (cdr (assq 'type situation))
710 (cdr (assq 'subtype situation)))
711 mime-view-type-subtype-score-alist)
713 (cdr (assq 'type situation))
714 mime-view-type-subtype-score-alist)
717 mime-view-type-subtype-score-alist)
719 (if (> score max-score)
729 (let ((child (car children))
730 (situation (car situations)))
731 (mime-display-entity child (if (= i p)
733 (del-alist 'body-presentation-method
734 (copy-alist situation))))
736 (setq children (cdr children)
737 situations (cdr situations)
742 ;;; @ acting-condition
745 (defvar mime-acting-condition nil
746 "Condition-tree about how to process entity.")
748 (if (file-readable-p mailcap-file)
749 (let ((entries (mailcap-parse-file)))
751 (let ((entry (car entries))
754 (let* ((field (car entry))
755 (field-type (car field)))
756 (cond ((eq field-type 'view) (setq view field))
757 ((eq field-type 'print) (setq print field))
758 ((memq field-type '(compose composetyped edit)))
759 (t (setq shared (cons field shared))))
761 (setq entry (cdr entry))
763 (setq shared (nreverse shared))
764 (ctree-set-calist-with-default
765 'mime-acting-condition
766 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
768 (ctree-set-calist-with-default
769 'mime-acting-condition
771 (list '(mode . "print")(cons 'method (cdr view))))
774 (setq entries (cdr entries))
777 (ctree-set-calist-strictly
778 'mime-acting-condition
779 '((type . application)(subtype . octet-stream)
781 (method . mime-detect-content)
784 (ctree-set-calist-with-default
785 'mime-acting-condition
787 (method . mime-save-content)))
789 (ctree-set-calist-strictly
790 'mime-acting-condition
791 '((type . text)(subtype . x-rot13-47)(mode . "play")
792 (method . mime-view-caesar)
794 (ctree-set-calist-strictly
795 'mime-acting-condition
796 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
797 (method . mime-view-caesar)
800 (ctree-set-calist-strictly
801 'mime-acting-condition
802 '((type . message)(subtype . rfc822)(mode . "play")
803 (method . mime-view-message/rfc822)
805 (ctree-set-calist-strictly
806 'mime-acting-condition
807 '((type . message)(subtype . partial)(mode . "play")
808 (method . mime-store-message/partial-piece)
811 (ctree-set-calist-strictly
812 'mime-acting-condition
813 '((type . message)(subtype . external-body)
814 ("access-type" . "anon-ftp")
815 (method . mime-view-message/external-anon-ftp)
818 (ctree-set-calist-strictly
819 'mime-acting-condition
820 '((type . message)(subtype . external-body)
821 ("access-type" . "url")
822 (method . mime-view-message/external-url)
825 (ctree-set-calist-strictly
826 'mime-acting-condition
827 '((type . application)(subtype . octet-stream)
828 (method . mime-save-content)
832 ;;; @ quitting method
835 (defvar mime-preview-quitting-method-alist
836 '((mime-show-message-mode
837 . mime-preview-quitting-method-for-mime-show-message-mode))
838 "Alist of major-mode vs. quitting-method of mime-view.")
840 (defvar mime-preview-over-to-previous-method-alist nil
841 "Alist of major-mode vs. over-to-previous-method of mime-view.")
843 (defvar mime-preview-over-to-next-method-alist nil
844 "Alist of major-mode vs. over-to-next-method of mime-view.")
847 ;;; @ following method
850 (defvar mime-preview-following-method-alist nil
851 "Alist of major-mode vs. following-method of mime-view.")
853 (defvar mime-view-following-required-fields-list
860 (defun mime-display-entity (entity &optional situation
861 default-situation preview-buffer)
863 (setq preview-buffer (current-buffer)))
864 (let* (e nb ne nhb nbb)
865 (mime-goto-header-start-point entity)
866 (in-calist-package 'mime-view)
869 (or (ctree-match-calist mime-preview-condition
870 (append (mime-entity-situation entity)
873 (let ((button-is-invisible
874 (eq (cdr (assq 'entity-button situation)) 'invisible))
876 (eq (cdr (assq 'header situation)) 'visible))
877 (header-presentation-method
878 (or (cdr (assq 'header-presentation-method situation))
879 (cdr (assq (cdr (assq 'major-mode situation))
880 mime-header-presentation-method-alist))))
881 (body-presentation-method
882 (cdr (assq 'body-presentation-method situation)))
883 (children (mime-entity-children entity)))
884 (set-buffer preview-buffer)
886 (narrow-to-region nb nb)
887 (or button-is-invisible
888 (if (mime-view-entity-button-visible-p entity)
889 (mime-view-insert-entity-button entity)
891 (when header-is-visible
893 (if header-presentation-method
894 (funcall header-presentation-method entity situation)
895 (mime-insert-header entity
896 mime-view-ignored-field-list
897 mime-view-visible-field-list))
898 (run-hooks 'mime-display-header-hook)
899 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
900 (goto-char (point-max))
905 ((functionp body-presentation-method)
906 (funcall body-presentation-method entity situation)
909 (when button-is-invisible
910 (goto-char (point-max))
911 (mime-view-insert-entity-button entity)
913 (or header-is-visible
915 (goto-char (point-max))
919 (setq ne (point-max))
921 (put-text-property nb ne 'mime-view-entity entity)
922 (put-text-property nb ne 'mime-view-situation situation)
923 (put-text-property nbb ne 'mime-view-entity-body entity)
926 (if (functionp body-presentation-method)
927 (funcall body-presentation-method entity situation)
928 (mime-display-multipart/mixed entity situation)
933 ;;; @ MIME viewer mode
936 (defconst mime-view-menu-title "MIME-View")
937 (defconst mime-view-menu-list
938 '((up "Move to upper entity" mime-preview-move-to-upper)
939 (previous "Move to previous entity" mime-preview-move-to-previous)
940 (next "Move to next entity" mime-preview-move-to-next)
941 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
942 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
943 (play "Play current entity" mime-preview-play-current-entity)
944 (extract "Extract current entity" mime-preview-extract-current-entity)
945 (print "Print current entity" mime-preview-print-current-entity)
947 "Menu for MIME Viewer")
949 (cond ((featurep 'xemacs)
950 (defvar mime-view-xemacs-popup-menu
951 (cons mime-view-menu-title
954 (vector (nth 1 item)(nth 2 item) t)
956 mime-view-menu-list)))
957 (defun mime-view-xemacs-popup-menu (event)
958 "Popup the menu in the MIME Viewer buffer"
960 (select-window (event-window event))
961 (set-buffer (event-buffer event))
962 (popup-menu 'mime-view-xemacs-popup-menu))
963 (defvar mouse-button-2 'button2)
966 (defvar mime-view-popup-menu
967 (let ((menu (make-sparse-keymap mime-view-menu-title)))
971 (list (intern (nth 1 item)) 'menu-item
972 (nth 1 item)(nth 2 item))
974 mime-view-menu-list))))
975 (defun mime-view-popup-menu (event)
976 "Popup the menu in the MIME Viewer buffer"
978 (let ((menu mime-view-popup-menu) events func)
979 (setq events (x-popup-menu t menu))
981 (setq func (lookup-key menu (apply #'vector events)))
984 (defvar mouse-button-2 [mouse-2])
987 (defun mime-view-define-keymap (&optional default)
988 (let ((mime-view-mode-map (if (keymapp default)
989 (copy-keymap default)
992 (define-key mime-view-mode-map
993 "u" (function mime-preview-move-to-upper))
994 (define-key mime-view-mode-map
995 "p" (function mime-preview-move-to-previous))
996 (define-key mime-view-mode-map
997 "n" (function mime-preview-move-to-next))
998 (define-key mime-view-mode-map
999 "\e\t" (function mime-preview-move-to-previous))
1000 (define-key mime-view-mode-map
1001 "\t" (function mime-preview-move-to-next))
1002 (define-key mime-view-mode-map
1003 " " (function mime-preview-scroll-up-entity))
1004 (define-key mime-view-mode-map
1005 "\M- " (function mime-preview-scroll-down-entity))
1006 (define-key mime-view-mode-map
1007 "\177" (function mime-preview-scroll-down-entity))
1008 (define-key mime-view-mode-map
1009 "\C-m" (function mime-preview-next-line-entity))
1010 (define-key mime-view-mode-map
1011 "\C-\M-m" (function mime-preview-previous-line-entity))
1012 (define-key mime-view-mode-map
1013 "v" (function mime-preview-play-current-entity))
1014 (define-key mime-view-mode-map
1015 "e" (function mime-preview-extract-current-entity))
1016 (define-key mime-view-mode-map
1017 "\C-c\C-p" (function mime-preview-print-current-entity))
1018 (define-key mime-view-mode-map
1019 "a" (function mime-preview-follow-current-entity))
1020 (define-key mime-view-mode-map
1021 "q" (function mime-preview-quit))
1022 (define-key mime-view-mode-map
1023 "\C-c\C-x" (function mime-preview-kill-buffer))
1024 ;; (define-key mime-view-mode-map
1025 ;; "<" (function beginning-of-buffer))
1026 ;; (define-key mime-view-mode-map
1027 ;; ">" (function end-of-buffer))
1028 (define-key mime-view-mode-map
1029 "?" (function describe-mode))
1030 (define-key mime-view-mode-map
1031 [tab] (function mime-preview-move-to-next))
1032 (define-key mime-view-mode-map
1033 [delete] (function mime-preview-scroll-down-entity))
1034 (define-key mime-view-mode-map
1035 [backspace] (function mime-preview-scroll-down-entity))
1036 (if (functionp default)
1037 (cond ((featurep 'xemacs)
1038 (set-keymap-default-binding mime-view-mode-map default)
1041 (setq mime-view-mode-map
1042 (append mime-view-mode-map (list (cons t default))))
1045 (define-key mime-view-mode-map
1046 mouse-button-2 (function mime-button-dispatcher))
1048 (cond ((featurep 'xemacs)
1049 (define-key mime-view-mode-map
1050 mouse-button-3 (function mime-view-xemacs-popup-menu))
1052 ((>= emacs-major-version 19)
1053 (define-key mime-view-mode-map
1054 mouse-button-3 (function mime-view-popup-menu))
1055 (define-key mime-view-mode-map [menu-bar mime-view]
1056 (cons mime-view-menu-title
1057 (make-sparse-keymap mime-view-menu-title)))
1060 (define-key mime-view-mode-map
1061 (vector 'menu-bar 'mime-view (car item))
1062 (cons (nth 1 item)(nth 2 item))
1065 (reverse mime-view-menu-list)
1068 (use-local-map mime-view-mode-map)
1069 (run-hooks 'mime-view-define-keymap-hook)
1072 (defsubst mime-maybe-hide-echo-buffer ()
1073 "Clear mime-echo buffer and delete window for it."
1074 (let ((buf (get-buffer mime-echo-buffer-name)))
1079 (let ((win (get-buffer-window buf)))
1086 (defvar mime-view-redisplay nil)
1089 (defun mime-display-message (message &optional preview-buffer
1090 mother default-keymap-or-function
1091 original-major-mode)
1092 "View MESSAGE in MIME-View mode.
1094 Optional argument PREVIEW-BUFFER specifies the buffer of the
1095 presentation. It must be either nil or a name of preview buffer.
1097 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1099 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1100 function. If it is a keymap, keymap of MIME-View mode will be added
1101 to it. If it is a function, it will be bound as default binding of
1102 keymap of MIME-View mode."
1103 (mime-maybe-hide-echo-buffer)
1104 (let ((win-conf (current-window-configuration)))
1106 (setq preview-buffer
1107 (concat "*Preview-" (mime-entity-name message) "*")))
1108 (or original-major-mode
1109 (setq original-major-mode
1110 (with-current-buffer (mime-entity-header-buffer message)
1112 (let ((inhibit-read-only t))
1113 (set-buffer (get-buffer-create preview-buffer))
1117 (setq mime-mother-buffer mother)
1119 (setq mime-preview-original-window-configuration win-conf)
1120 (setq major-mode 'mime-view-mode)
1121 (setq mode-name "MIME-View")
1122 (mime-display-entity message nil
1123 `((entity-button . invisible)
1125 (major-mode . ,original-major-mode))
1127 (mime-view-define-keymap default-keymap-or-function)
1129 (next-single-property-change (point-min) 'mime-view-entity)))
1132 (goto-char (point-min))
1133 (search-forward "\n\n" nil t)
1135 (run-hooks 'mime-view-mode-hook)
1136 (set-buffer-modified-p nil)
1137 (setq buffer-read-only t)
1141 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1142 default-keymap-or-function
1143 representation-type)
1144 "View RAW-BUFFER in MIME-View mode.
1145 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1147 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1148 function. If it is a keymap, keymap of MIME-View mode will be added
1149 to it. If it is a function, it will be bound as default binding of
1150 keymap of MIME-View mode.
1151 Optional argument REPRESENTATION-TYPE is representation-type of
1152 message. It must be nil, `binary' or `cooked'. If it is nil,
1153 `cooked' is used as default."
1156 (setq raw-buffer (current-buffer)))
1157 (or representation-type
1158 (setq representation-type
1160 (set-buffer raw-buffer)
1161 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1162 (assq t mime-raw-representation-type-alist)))
1164 (if (eq representation-type 'binary)
1165 (setq representation-type 'buffer)
1167 (setq preview-buffer (mime-display-message
1168 (mime-open-entity representation-type raw-buffer)
1169 preview-buffer mother default-keymap-or-function))
1170 (or (get-buffer-window preview-buffer)
1171 (let ((r-win (get-buffer-window raw-buffer)))
1173 (set-window-buffer r-win preview-buffer)
1174 (let ((m-win (and mother (get-buffer-window mother))))
1176 (set-window-buffer m-win preview-buffer)
1177 (switch-to-buffer preview-buffer)
1180 (defun mime-view-mode (&optional mother ctl encoding
1181 raw-buffer preview-buffer
1182 default-keymap-or-function)
1183 "Major mode for viewing MIME message.
1185 Here is a list of the standard keys for mime-view-mode.
1190 u Move to upper content
1191 p or M-TAB Move to previous content
1192 n or TAB Move to next content
1193 SPC Scroll up or move to next content
1194 M-SPC or DEL Scroll down or move to previous content
1195 RET Move to next line
1196 M-RET Move to previous line
1197 v Decode current content as `play mode'
1198 e Decode current content as `extract mode'
1199 C-c C-p Decode current content as `print mode'
1200 a Followup to current content.
1202 button-2 Move to point under the mouse cursor
1203 and decode current content as `play mode'
1206 (unless mime-view-redisplay
1208 (if raw-buffer (set-buffer raw-buffer))
1211 (or (assq major-mode mime-raw-representation-type-alist)
1212 (assq t mime-raw-representation-type-alist)))))
1213 (if (eq type 'binary)
1216 (setq mime-message-structure (mime-open-entity type raw-buffer))
1217 (or (mime-entity-content-type mime-message-structure)
1218 (mime-entity-set-content-type-internal
1219 mime-message-structure ctl))
1221 (or (mime-entity-encoding mime-message-structure)
1222 (mime-entity-set-encoding-internal mime-message-structure encoding))
1224 (mime-display-message mime-message-structure preview-buffer
1225 mother default-keymap-or-function)
1232 (autoload 'mime-preview-play-current-entity "mime-play"
1233 "Play current entity." t)
1235 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1236 "Extract current entity into file (maybe).
1237 It decodes current entity to call internal or external method as
1238 \"extract\" mode. The method is selected from variable
1239 `mime-acting-condition'."
1241 (mime-preview-play-current-entity ignore-examples "extract")
1244 (defun mime-preview-print-current-entity (&optional ignore-examples)
1245 "Print current entity (maybe).
1246 It decodes current entity to call internal or external method as
1247 \"print\" mode. The method is selected from variable
1248 `mime-acting-condition'."
1250 (mime-preview-play-current-entity ignore-examples "print")
1257 (defun mime-preview-follow-current-entity ()
1258 "Write follow message to current entity.
1259 It calls following-method selected from variable
1260 `mime-preview-following-method-alist'."
1263 (while (null (setq entity
1264 (get-text-property (point) 'mime-view-entity)))
1268 (previous-single-property-change (point) 'mime-view-entity))
1271 (entity-node-id (mime-entity-node-id entity))
1272 (len (length entity-node-id))
1276 (if (eq (next-single-property-change (point-min)
1282 ((eq (next-single-property-change p-beg 'mime-view-entity)
1284 (setq p-beg (point))
1286 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1288 (setq p-end (point-max))
1290 ((null entity-node-id)
1291 (setq p-end (point-max))
1299 (next-single-property-change
1300 (point) 'mime-view-entity))
1302 (let ((rc (mime-entity-node-id
1303 (get-text-property (point)
1304 'mime-view-entity))))
1305 (or (equal entity-node-id
1306 (nthcdr (- (length rc) len) rc))
1311 (setq p-end (point-max))
1315 (previous-single-property-change p-end 'mime-view-entity-header))
1316 (if (or (null ph-end)
1320 (let* ((mode (mime-preview-original-major-mode 'recursive))
1322 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1324 (the-buf (current-buffer))
1327 (set-buffer (setq new-buf (get-buffer-create new-name)))
1329 (insert-buffer-substring the-buf ph-end p-end)
1330 (when (= ph-end p-beg)
1331 (goto-char (point-min))
1333 (goto-char (point-min))
1334 (let ((current-entity
1335 (if (and (eq (mime-entity-media-type entity) 'message)
1336 (eq (mime-entity-media-subtype entity) 'rfc822))
1337 (mime-entity-children entity)
1340 (while (and current-entity
1343 (with-current-buffer
1344 (mime-entity-header-buffer current-entity)
1347 (mime-entity-header-start-point
1349 (mime-entity-header-end-point
1351 (std11-header-string-except
1354 (apply (function regexp-or) fields)
1356 (if (and (eq (mime-entity-media-type
1357 current-entity) 'message)
1358 (eq (mime-entity-media-subtype
1359 current-entity) 'rfc822))
1365 (setq fields (std11-collect-field-names)
1366 current-entity (mime-entity-parent current-entity))
1369 (let ((rest mime-view-following-required-fields-list)
1372 (setq field-name (car rest))
1373 (or (std11-field-body field-name)
1376 (set-buffer the-buf)
1377 (let ((entity (when mime-mother-buffer
1378 (set-buffer mime-mother-buffer)
1379 (get-text-property (point)
1380 'mime-view-entity))))
1382 (null (setq ret (mime-entity-fetch-field
1383 entity field-name))))
1384 (setq entity (mime-entity-parent entity)))))
1386 (insert (concat field-name ": " ret "\n"))
1388 (setq rest (cdr rest))
1390 (mime-decode-header-in-buffer)
1392 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1397 "Sorry, following method for %s is not implemented yet."
1406 (defun mime-preview-move-to-upper ()
1407 "Move to upper entity.
1408 If there is no upper entity, call function `mime-preview-quit'."
1411 (while (null (setq cinfo
1412 (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))
1437 (defun mime-preview-move-to-previous ()
1438 "Move to previous entity.
1439 If there is no previous entity, it calls function registered in
1440 variable `mime-preview-over-to-previous-method-alist'."
1442 (while (and (not (bobp))
1443 (null (get-text-property (point) 'mime-view-entity)))
1446 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1448 (>= point (point-min)))
1449 (if (get-text-property (1- point) 'mime-view-entity)
1450 (progn (goto-char point)
1452 (or (eq mime-preview-move-scroll t)
1453 (and mime-preview-move-scroll
1456 (move-to-window-line 0)
1457 (forward-line next-screen-context-lines)
1460 (recenter (* -1 next-screen-context-lines))))
1461 (goto-char (1- point))
1462 (mime-preview-move-to-previous)
1464 (let ((f (assq (mime-preview-original-major-mode)
1465 mime-preview-over-to-previous-method-alist)))
1471 (defun mime-preview-move-to-next ()
1472 "Move to next entity.
1473 If there is no previous entity, it calls function registered in
1474 variable `mime-preview-over-to-next-method-alist'."
1476 (while (and (not (eobp))
1477 (null (get-text-property (point) 'mime-view-entity)))
1480 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1482 (<= point (point-max)))
1485 (if (null (get-text-property point 'mime-view-entity))
1486 (mime-preview-move-to-next)
1488 (or (eq mime-preview-move-scroll t)
1489 (and mime-preview-move-scroll
1492 (move-to-window-line -1)
1494 (* -1 next-screen-context-lines))
1497 (recenter next-screen-context-lines))
1499 (let ((f (assq (mime-preview-original-major-mode)
1500 mime-preview-over-to-next-method-alist)))
1506 (defun mime-preview-scroll-up-entity (&optional h)
1507 "Scroll up current entity.
1508 If reached to (point-max), it calls function registered in variable
1509 `mime-preview-over-to-next-method-alist'."
1512 (let ((f (assq (mime-preview-original-major-mode)
1513 mime-preview-over-to-next-method-alist)))
1518 (or (next-single-property-change (point) 'mime-view-entity)
1520 (bottom (window-end (selected-window))))
1523 (progn (goto-char point)
1524 (recenter next-screen-context-lines))
1528 (goto-char (point-max)))))
1531 (defun mime-preview-scroll-down-entity (&optional h)
1532 "Scroll down current entity.
1533 If reached to (point-min), it calls function registered in variable
1534 `mime-preview-over-to-previous-method-alist'."
1537 (let ((f (assq (mime-preview-original-major-mode)
1538 mime-preview-over-to-previous-method-alist)))
1543 (or (previous-single-property-change (point) 'mime-view-entity)
1545 (top (window-start (selected-window))))
1548 (progn (goto-char point)
1549 (recenter (* -1 next-screen-context-lines)))
1552 (beginning-of-buffer
1553 (goto-char (point-min)))))
1556 (defun mime-preview-next-line-entity (&optional lines)
1557 "Scroll up one line (or prefix LINES lines).
1558 If LINES is negative, scroll down LINES lines."
1560 (mime-preview-scroll-up-entity (or lines 1))
1563 (defun mime-preview-previous-line-entity (&optional lines)
1564 "Scrroll down one line (or prefix LINES lines).
1565 If LINES is negative, scroll up LINES lines."
1567 (mime-preview-scroll-down-entity (or lines 1))
1573 (defun mime-preview-quit ()
1574 "Quit from MIME-preview buffer.
1575 It calls function registered in variable
1576 `mime-preview-quitting-method-alist'."
1578 (let ((r (assq (mime-preview-original-major-mode)
1579 mime-preview-quitting-method-alist)))
1584 (defun mime-preview-kill-buffer ()
1586 (kill-buffer (current-buffer))
1593 (provide 'mime-view)
1595 (run-hooks 'mime-view-load-hook)
1597 ;;; mime-view.el ends here