1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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."
66 ;;; @ in raw-buffer (representation space)
69 (defvar mime-preview-buffer nil
70 "MIME-preview buffer corresponding with the (raw) buffer.")
71 (make-variable-buffer-local 'mime-preview-buffer)
74 (defvar mime-raw-representation-type-alist
75 '((mime-show-message-mode . binary)
76 (mime-temp-message-mode . binary)
79 "Alist of major-mode vs. representation-type of mime-raw-buffer.
80 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
81 major-mode or t. t means default. REPRESENTATION-TYPE must be
82 `binary' or `cooked'.")
85 ;; (defun mime-raw-find-entity-from-point (point &optional message-info)
86 ;; "Return entity from POINT in mime-raw-buffer.
87 ;; If optional argument MESSAGE-INFO is not specified,
88 ;; `mime-message-structure' is used."
90 ;; (setq message-info mime-message-structure))
91 ;; (if (and (<= (mime-entity-point-min message-info) point)
92 ;; (<= point (mime-entity-point-max message-info)))
93 ;; (let ((children (mime-entity-children message-info)))
97 ;; (mime-raw-find-entity-from-point point (car children))))
101 ;; (setq children (cdr children)))
103 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
106 ;;; @ in preview-buffer (presentation space)
109 (defvar mime-mother-buffer nil
110 "Mother buffer corresponding with the (MIME-preview) buffer.
111 If current MIME-preview buffer is generated by other buffer, such as
112 message/partial, it is called `mother-buffer'.")
113 (make-variable-buffer-local 'mime-mother-buffer)
115 ;; (defvar mime-raw-buffer nil
116 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
117 ;; (make-variable-buffer-local 'mime-raw-buffer)
119 (defvar mime-preview-original-window-configuration nil
120 "Window-configuration before mime-view-mode is called.")
121 (make-variable-buffer-local 'mime-preview-original-window-configuration)
123 (defun mime-preview-original-major-mode (&optional recursive point)
124 "Return major-mode of original buffer.
125 If optional argument RECURSIVE is non-nil and current buffer has
126 mime-mother-buffer, it returns original major-mode of the
128 (if (and recursive mime-mother-buffer)
130 (set-buffer mime-mother-buffer)
131 (mime-preview-original-major-mode recursive)
133 (cdr (assq 'major-mode
134 (get-text-property (or point (if (eobp)
137 'mime-view-situation)))))
140 ;;; @ entity information
143 (defun mime-entity-situation (entity &optional situation)
144 "Return situation of ENTITY."
145 (let (rest param name)
147 (unless (assq 'type situation)
148 (setq rest (or (mime-entity-content-type entity)
149 (make-mime-content-type 'text 'plain))
150 situation (cons (car rest) situation)
153 (unless (assq 'subtype situation)
155 (setq rest (or (cdr (mime-entity-content-type entity))
156 '((subtype . plain)))))
157 (setq situation (cons (car rest) situation)
161 (setq param (car rest))
162 (or (assoc (car param) situation)
163 (setq situation (cons param situation)))
164 (setq rest (cdr rest)))
166 ;; Content-Disposition
168 (unless (assq 'disposition-type situation)
169 (setq rest (mime-entity-content-disposition entity))
171 (setq situation (cons (cons 'disposition-type
172 (mime-content-disposition-type rest))
174 rest (mime-content-disposition-parameters rest))
177 (setq param (car rest)
179 (if (cond ((string= name "filename")
180 (if (assq 'filename situation)
182 (setq name 'filename)))
183 ((string= name "creation-date")
184 (if (assq 'creation-date situation)
186 (setq name 'creation-date)))
187 ((string= name "modification-date")
188 (if (assq 'modification-date situation)
190 (setq name 'modification-date)))
191 ((string= name "read-date")
192 (if (assq 'read-date situation)
194 (setq name 'read-date)))
195 ((string= name "size")
196 (if (assq 'size situation)
199 (t (setq name (cons 'disposition name))
200 (if (assoc name situation)
204 (cons (cons name (cdr param))
206 (setq rest (cdr rest)))
208 ;; Content-Transfer-Encoding
209 (or (assq 'encoding situation)
211 (cons (cons 'encoding (or (mime-entity-encoding entity)
216 ;; (or (assq 'major-mode situation)
218 ;; (cons (cons 'major-mode
219 ;; (with-current-buffer (mime-entity-buffer entity)
225 (defun mime-view-entity-title (entity)
226 (or (mime-entity-read-field entity 'Content-Description)
227 (mime-entity-read-field entity 'Subject)
228 (mime-entity-filename entity)
232 ;; (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
233 ;; "Return entity-node-id from POINT in mime-raw-buffer.
234 ;; If optional argument MESSAGE-INFO is not specified,
235 ;; `mime-message-structure' is used."
236 ;; (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
238 ;; (make-obsolete 'mime-raw-point-to-entity-node-id "don't use it.")
240 ;; (defsubst mime-raw-point-to-entity-number (point &optional message-info)
241 ;; "Return entity-number from POINT in mime-raw-buffer.
242 ;; If optional argument MESSAGE-INFO is not specified,
243 ;; `mime-message-structure' is used."
244 ;; (mime-entity-number (mime-raw-find-entity-from-point point message-info)))
246 ;; (make-obsolete 'mime-raw-point-to-entity-number "don't use it.")
248 ;; (defun mime-raw-flatten-message-info (&optional message-info)
249 ;; "Return list of entity in mime-raw-buffer.
250 ;; If optional argument MESSAGE-INFO is not specified,
251 ;; `mime-message-structure' is used."
253 ;; (setq message-info mime-message-structure))
254 ;; (let ((dest (list message-info))
255 ;; (rcl (mime-entity-children message-info)))
257 ;; (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
258 ;; (setq rcl (cdr rcl)))
262 ;;; @ presentation of preview
268 ;;; @@@ predicate function
271 (defun mime-view-entity-button-visible-p (entity)
272 "Return non-nil if header of ENTITY is visible.
273 Please redefine this function if you want to change default setting."
274 (let ((media-type (mime-entity-media-type entity))
275 (media-subtype (mime-entity-media-subtype entity)))
276 (or (not (eq media-type 'application))
277 (and (not (eq media-subtype 'x-selection))
278 (or (not (eq media-subtype 'octet-stream))
279 (let ((mother-entity (mime-entity-parent entity)))
280 (or (not (eq (mime-entity-media-type mother-entity)
282 (not (eq (mime-entity-media-subtype mother-entity)
287 ;;; @@@ entity button generator
290 (defun mime-view-insert-entity-button (entity)
291 "Insert entity-button of ENTITY."
292 (let ((entity-node-id (mime-entity-node-id entity))
293 (params (mime-entity-parameters entity))
294 (subject (mime-view-entity-title entity)))
296 (let ((access-type (assoc "access-type" params))
297 (num (or (cdr (assoc "x-part-number" params))
298 (if (consp entity-node-id)
301 (format "%s" (1+ num))
303 (reverse entity-node-id) ".")
307 (let ((server (assoc "server" params)))
308 (setq access-type (cdr access-type))
310 (format "%s %s ([%s] %s)"
311 num subject access-type (cdr server))
312 (let ((site (cdr (assoc "site" params)))
313 (dir (cdr (assoc "directory" params)))
314 (url (cdr (assoc "url" params)))
317 (format "%s %s ([%s] %s)"
318 num subject access-type url)
319 (format "%s %s ([%s] %s:%s)"
320 num subject access-type site dir))
324 (let ((media-type (mime-entity-media-type entity))
325 (media-subtype (mime-entity-media-subtype entity))
326 (charset (cdr (assoc "charset" params)))
327 (encoding (mime-entity-encoding entity)))
331 (format " <%s/%s%s%s>"
332 media-type media-subtype
334 (concat "; " charset)
337 (concat " (" encoding ")")
339 (if (>= (+ (current-column)(length rest))(window-width))
343 (function mime-preview-play-current-entity))
350 (defvar mime-header-presentation-method-alist nil
351 "Alist of major mode vs. corresponding header-presentation-method functions.
352 Each element looks like (SYMBOL . FUNCTION).
353 SYMBOL must be major mode in raw-buffer or t. t means default.
354 Interface of FUNCTION must be (ENTITY SITUATION).")
356 (defvar mime-view-ignored-field-list
357 '(".*Received:" ".*Path:" ".*Id:" "^References:"
358 "^Replied:" "^Errors-To:"
359 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
360 "^Content-Type:" "^Precedence:"
361 "^Status:" "^X-VM-.*:")
362 "All fields that match this list will be hidden in MIME preview buffer.
363 Each elements are regexp of field-name.")
365 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
366 "All fields that match this list will be displayed in MIME preview buffer.
367 Each elements are regexp of field-name.")
373 ;;; @@@ predicate function
376 (in-calist-package 'mime-view)
378 (defun mime-calist::field-match-method-as-default-rule (calist
379 field-type field-value)
380 (let ((s-field (assq field-type calist)))
381 (cond ((null s-field)
382 (cons (cons field-type field-value) calist)
386 (define-calist-field-match-method
387 'header #'mime-calist::field-match-method-as-default-rule)
389 (define-calist-field-match-method
390 'body #'mime-calist::field-match-method-as-default-rule)
393 (defvar mime-preview-condition nil
394 "Condition-tree about how to display entity.")
396 (ctree-set-calist-strictly
397 'mime-preview-condition '((type . application)(subtype . octet-stream)
400 (ctree-set-calist-strictly
401 'mime-preview-condition '((type . application)(subtype . octet-stream)
404 (ctree-set-calist-strictly
405 'mime-preview-condition '((type . application)(subtype . octet-stream)
409 (ctree-set-calist-strictly
410 'mime-preview-condition '((type . application)(subtype . pgp)
413 (ctree-set-calist-strictly
414 'mime-preview-condition '((type . application)(subtype . x-latex)
417 (ctree-set-calist-strictly
418 'mime-preview-condition '((type . application)(subtype . x-selection)
421 (ctree-set-calist-strictly
422 'mime-preview-condition '((type . application)(subtype . x-comment)
425 (ctree-set-calist-strictly
426 'mime-preview-condition '((type . message)(subtype . delivery-status)
429 (ctree-set-calist-strictly
430 'mime-preview-condition
432 (body-presentation-method . mime-display-text/plain)))
434 (ctree-set-calist-strictly
435 'mime-preview-condition
438 (body-presentation-method . mime-display-text/plain)))
440 (ctree-set-calist-strictly
441 'mime-preview-condition
442 '((type . text)(subtype . enriched)
444 (body-presentation-method . mime-display-text/enriched)))
446 (ctree-set-calist-strictly
447 'mime-preview-condition
448 '((type . text)(subtype . richtext)
450 (body-presentation-method . mime-display-text/richtext)))
452 (ctree-set-calist-strictly
453 'mime-preview-condition
454 '((type . text)(subtype . t)
456 (body-presentation-method . mime-display-text/plain)))
458 (ctree-set-calist-strictly
459 'mime-preview-condition
460 '((type . multipart)(subtype . alternative)
462 (body-presentation-method . mime-display-multipart/alternative)))
464 (ctree-set-calist-strictly
465 'mime-preview-condition '((type . message)(subtype . partial)
466 (body-presentation-method
467 . mime-display-message/partial-button)))
469 (ctree-set-calist-strictly
470 'mime-preview-condition '((type . message)(subtype . rfc822)
471 (body-presentation-method . nil)
472 (childrens-situation (header . visible)
473 (entity-button . invisible))))
475 (ctree-set-calist-strictly
476 'mime-preview-condition '((type . message)(subtype . news)
477 (body-presentation-method . nil)
478 (childrens-situation (header . visible)
479 (entity-button . invisible))))
482 ;;; @@@ entity presentation
485 (defun mime-display-text/plain (entity situation)
487 (narrow-to-region (point-max)(point-max))
488 (mime-insert-text-content entity)
489 (run-hooks 'mime-text-decode-hook)
490 (goto-char (point-max))
491 (if (not (eq (char-after (1- (point))) ?\n))
494 (mime-add-url-buttons)
495 (run-hooks 'mime-display-text/plain-hook)
498 (defun mime-display-text/richtext (entity situation)
500 (narrow-to-region (point-max)(point-max))
501 (mime-insert-text-content entity)
502 (run-hooks 'mime-text-decode-hook)
503 (let ((beg (point-min)))
504 (remove-text-properties beg (point-max) '(face nil))
505 (richtext-decode beg (point-max))
508 (defun mime-display-text/enriched (entity situation)
510 (narrow-to-region (point-max)(point-max))
511 (mime-insert-text-content entity)
512 (run-hooks 'mime-text-decode-hook)
513 (let ((beg (point-min)))
514 (remove-text-properties beg (point-max) '(face nil))
515 (enriched-decode beg (point-max))
518 (defvar mime-view-announcement-for-message/partial
519 (if (and (>= emacs-major-version 19) window-system)
521 \[[ This is message/partial style split message. ]]
522 \[[ Please press `v' key in this buffer ]]
523 \[[ or click here by mouse button-2. ]]"
525 \[[ This is message/partial style split message. ]]
526 \[[ Please press `v' key in this buffer. ]]"
529 (defun mime-display-message/partial-button (&optional entity situation)
531 (goto-char (point-max))
532 (if (not (search-backward "\n\n" nil t))
535 (goto-char (point-max))
536 (narrow-to-region (point-max)(point-max))
537 (insert mime-view-announcement-for-message/partial)
538 (mime-add-button (point-min)(point-max)
539 #'mime-preview-play-current-entity)
542 (defun mime-display-multipart/mixed (entity situation)
543 (let ((children (mime-entity-children entity))
544 (original-major-mode (cdr (assq 'major-mode situation)))
546 (cdr (assq 'childrens-situation situation))))
547 (if original-major-mode
548 (setq default-situation
549 (cons (cons 'major-mode original-major-mode)
553 (mime-display-entity (car children) nil default-situation)
554 (setq children (cdr children))
557 (defcustom mime-view-type-subtype-score-alist
558 '(((text . enriched) . 3)
559 ((text . richtext) . 2)
562 "Alist MEDIA-TYPE vs corresponding score.
563 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
565 :type '(repeat (cons (choice :tag "Media-Type"
566 (cons :tag "Type/Subtype"
567 (symbol :tag "Primary-type")
568 (symbol :tag "Subtype"))
570 (const :tag "Default" t))
573 (defun mime-display-multipart/alternative (entity situation)
574 (let* ((children (mime-entity-children entity))
575 (original-major-mode (cdr (assq 'major-mode situation)))
577 (cdr (assq 'childrens-situation situation)))
582 (if original-major-mode
583 (setq default-situation
584 (cons (cons 'major-mode original-major-mode)
591 (or (ctree-match-calist
592 mime-preview-condition
593 (append (mime-entity-situation child)
596 (if (cdr (assq 'body-presentation-method situation))
601 (cdr (assq 'type situation))
602 (cdr (assq 'subtype situation)))
603 mime-view-type-subtype-score-alist)
605 (cdr (assq 'type situation))
606 mime-view-type-subtype-score-alist)
609 mime-view-type-subtype-score-alist)
611 (if (> score max-score)
621 (let ((child (car children))
622 (situation (car situations)))
623 (mime-display-entity child (if (= i p)
625 (del-alist 'body-presentation-method
626 (copy-alist situation))))
628 (setq children (cdr children)
629 situations (cdr situations)
634 ;;; @ acting-condition
637 (defvar mime-acting-condition nil
638 "Condition-tree about how to process entity.")
640 (if (file-readable-p mailcap-file)
641 (let ((entries (mailcap-parse-file)))
643 (let ((entry (car entries))
646 (let* ((field (car entry))
647 (field-type (car field)))
648 (cond ((eq field-type 'view) (setq view field))
649 ((eq field-type 'print) (setq print field))
650 ((memq field-type '(compose composetyped edit)))
651 (t (setq shared (cons field shared))))
653 (setq entry (cdr entry))
655 (setq shared (nreverse shared))
656 (ctree-set-calist-with-default
657 'mime-acting-condition
658 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
660 (ctree-set-calist-with-default
661 'mime-acting-condition
663 (list '(mode . "print")(cons 'method (cdr view))))
666 (setq entries (cdr entries))
669 (ctree-set-calist-strictly
670 'mime-acting-condition
671 '((type . application)(subtype . octet-stream)
673 (method . mime-detect-content)
676 (ctree-set-calist-with-default
677 'mime-acting-condition
679 (method . mime-save-content)))
681 (ctree-set-calist-strictly
682 'mime-acting-condition
683 '((type . text)(subtype . x-rot13-47)(mode . "play")
684 (method . mime-view-caesar)
686 (ctree-set-calist-strictly
687 'mime-acting-condition
688 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
689 (method . mime-view-caesar)
692 (ctree-set-calist-strictly
693 'mime-acting-condition
694 '((type . message)(subtype . rfc822)(mode . "play")
695 (method . mime-view-message/rfc822)
697 (ctree-set-calist-strictly
698 'mime-acting-condition
699 '((type . message)(subtype . partial)(mode . "play")
700 (method . mime-store-message/partial-piece)
703 (ctree-set-calist-strictly
704 'mime-acting-condition
705 '((type . message)(subtype . external-body)
706 ("access-type" . "anon-ftp")
707 (method . mime-view-message/external-anon-ftp)
710 (ctree-set-calist-strictly
711 'mime-acting-condition
712 '((type . message)(subtype . external-body)
713 ("access-type" . "url")
714 (method . mime-view-message/external-url)
717 (ctree-set-calist-strictly
718 'mime-acting-condition
719 '((type . application)(subtype . octet-stream)
720 (method . mime-save-content)
724 ;;; @ quitting method
727 (defvar mime-preview-quitting-method-alist
728 '((mime-show-message-mode
729 . mime-preview-quitting-method-for-mime-show-message-mode))
730 "Alist of major-mode vs. quitting-method of mime-view.")
732 (defvar mime-preview-over-to-previous-method-alist nil
733 "Alist of major-mode vs. over-to-previous-method of mime-view.")
735 (defvar mime-preview-over-to-next-method-alist nil
736 "Alist of major-mode vs. over-to-next-method of mime-view.")
739 ;;; @ following method
742 (defvar mime-preview-following-method-alist nil
743 "Alist of major-mode vs. following-method of mime-view.")
745 (defvar mime-view-following-required-fields-list
752 (defun mime-display-entity (entity &optional situation
753 default-situation preview-buffer)
755 (setq preview-buffer (current-buffer)))
756 (let* (e nb ne nhb nbb)
757 (mime-goto-header-start-point entity)
758 (in-calist-package 'mime-view)
761 (or (ctree-match-calist mime-preview-condition
762 (append (mime-entity-situation entity)
765 (let ((button-is-invisible
766 (eq (cdr (assq 'entity-button situation)) 'invisible))
768 (eq (cdr (assq 'header situation)) 'visible))
769 (header-presentation-method
770 (or (cdr (assq 'header-presentation-method situation))
771 (cdr (assq major-mode mime-header-presentation-method-alist))))
772 (body-presentation-method
773 (cdr (assq 'body-presentation-method situation)))
774 (children (mime-entity-children entity)))
775 (set-buffer preview-buffer)
777 (narrow-to-region nb nb)
778 (or button-is-invisible
779 (if (mime-view-entity-button-visible-p entity)
780 (mime-view-insert-entity-button entity)
782 (when header-is-visible
784 (if header-presentation-method
785 (funcall header-presentation-method entity situation)
786 (mime-insert-header entity
787 mime-view-ignored-field-list
788 mime-view-visible-field-list))
789 (run-hooks 'mime-display-header-hook)
790 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
791 (goto-char (point-max))
796 ((functionp body-presentation-method)
797 (funcall body-presentation-method entity situation)
800 (when button-is-invisible
801 (goto-char (point-max))
802 (mime-view-insert-entity-button entity)
804 (or header-is-visible
806 (goto-char (point-max))
810 (setq ne (point-max))
812 (put-text-property nb ne 'mime-view-entity entity)
813 (put-text-property nb ne 'mime-view-situation situation)
814 (put-text-property nbb ne 'mime-view-entity-body entity)
817 (if (functionp body-presentation-method)
818 (funcall body-presentation-method entity situation)
819 (mime-display-multipart/mixed entity situation)
824 ;;; @ MIME viewer mode
827 (defconst mime-view-menu-title "MIME-View")
828 (defconst mime-view-menu-list
829 '((up "Move to upper entity" mime-preview-move-to-upper)
830 (previous "Move to previous entity" mime-preview-move-to-previous)
831 (next "Move to next entity" mime-preview-move-to-next)
832 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
833 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
834 (play "Play current entity" mime-preview-play-current-entity)
835 (extract "Extract current entity" mime-preview-extract-current-entity)
836 (print "Print current entity" mime-preview-print-current-entity)
838 "Menu for MIME Viewer")
840 (cond ((featurep 'xemacs)
841 (defvar mime-view-xemacs-popup-menu
842 (cons mime-view-menu-title
845 (vector (nth 1 item)(nth 2 item) t)
847 mime-view-menu-list)))
848 (defun mime-view-xemacs-popup-menu (event)
849 "Popup the menu in the MIME Viewer buffer"
851 (select-window (event-window event))
852 (set-buffer (event-buffer event))
853 (popup-menu 'mime-view-xemacs-popup-menu))
854 (defvar mouse-button-2 'button2)
857 (defvar mouse-button-2 [mouse-2])
860 (defun mime-view-define-keymap (&optional default)
861 (let ((mime-view-mode-map (if (keymapp default)
862 (copy-keymap default)
865 (define-key mime-view-mode-map
866 "u" (function mime-preview-move-to-upper))
867 (define-key mime-view-mode-map
868 "p" (function mime-preview-move-to-previous))
869 (define-key mime-view-mode-map
870 "n" (function mime-preview-move-to-next))
871 (define-key mime-view-mode-map
872 "\e\t" (function mime-preview-move-to-previous))
873 (define-key mime-view-mode-map
874 "\t" (function mime-preview-move-to-next))
875 (define-key mime-view-mode-map
876 " " (function mime-preview-scroll-up-entity))
877 (define-key mime-view-mode-map
878 "\M- " (function mime-preview-scroll-down-entity))
879 (define-key mime-view-mode-map
880 "\177" (function mime-preview-scroll-down-entity))
881 (define-key mime-view-mode-map
882 "\C-m" (function mime-preview-next-line-entity))
883 (define-key mime-view-mode-map
884 "\C-\M-m" (function mime-preview-previous-line-entity))
885 (define-key mime-view-mode-map
886 "v" (function mime-preview-play-current-entity))
887 (define-key mime-view-mode-map
888 "e" (function mime-preview-extract-current-entity))
889 (define-key mime-view-mode-map
890 "\C-c\C-p" (function mime-preview-print-current-entity))
891 (define-key mime-view-mode-map
892 "a" (function mime-preview-follow-current-entity))
893 (define-key mime-view-mode-map
894 "q" (function mime-preview-quit))
895 (define-key mime-view-mode-map
896 "\C-c\C-x" (function mime-preview-kill-buffer))
897 ;; (define-key mime-view-mode-map
898 ;; "<" (function beginning-of-buffer))
899 ;; (define-key mime-view-mode-map
900 ;; ">" (function end-of-buffer))
901 (define-key mime-view-mode-map
902 "?" (function describe-mode))
903 (define-key mime-view-mode-map
904 [tab] (function mime-preview-move-to-next))
905 (define-key mime-view-mode-map
906 [delete] (function mime-preview-scroll-down-entity))
907 (define-key mime-view-mode-map
908 [backspace] (function mime-preview-scroll-down-entity))
909 (if (functionp default)
910 (cond ((featurep 'xemacs)
911 (set-keymap-default-binding mime-view-mode-map default)
914 (setq mime-view-mode-map
915 (append mime-view-mode-map (list (cons t default))))
918 (define-key mime-view-mode-map
919 mouse-button-2 (function mime-button-dispatcher))
921 (cond ((featurep 'xemacs)
922 (define-key mime-view-mode-map
923 mouse-button-3 (function mime-view-xemacs-popup-menu))
925 ((>= emacs-major-version 19)
926 (define-key mime-view-mode-map [menu-bar mime-view]
927 (cons mime-view-menu-title
928 (make-sparse-keymap mime-view-menu-title)))
931 (define-key mime-view-mode-map
932 (vector 'menu-bar 'mime-view (car item))
933 (cons (nth 1 item)(nth 2 item))
936 (reverse mime-view-menu-list)
939 (use-local-map mime-view-mode-map)
940 (run-hooks 'mime-view-define-keymap-hook)
943 (defsubst mime-maybe-hide-echo-buffer ()
944 "Clear mime-echo buffer and delete window for it."
945 (let ((buf (get-buffer mime-echo-buffer-name)))
950 (let ((win (get-buffer-window buf)))
957 (defvar mime-view-redisplay nil)
960 (defun mime-display-message (message &optional preview-buffer
961 mother default-keymap-or-function
963 "View MESSAGE in MIME-View mode.
965 Optional argument PREVIEW-BUFFER specifies the buffer of the
966 presentation. It must be either nil or a name of preview buffer.
968 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
970 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
971 function. If it is a keymap, keymap of MIME-View mode will be added
972 to it. If it is a function, it will be bound as default binding of
973 keymap of MIME-View mode."
974 (mime-maybe-hide-echo-buffer)
975 (let ((win-conf (current-window-configuration)))
978 (concat "*Preview-" (mime-entity-name message) "*")))
979 (or original-major-mode
980 (setq original-major-mode
981 (with-current-buffer (mime-entity-header-buffer message)
983 (let ((inhibit-read-only t))
984 (set-buffer (get-buffer-create preview-buffer))
988 (setq mime-mother-buffer mother)
990 (setq mime-preview-original-window-configuration win-conf)
991 (setq major-mode 'mime-view-mode)
992 (setq mode-name "MIME-View")
993 (mime-display-entity message nil
994 `((entity-button . invisible)
996 (major-mode . ,original-major-mode))
998 (mime-view-define-keymap default-keymap-or-function)
1000 (next-single-property-change (point-min) 'mime-view-entity)))
1003 (goto-char (point-min))
1004 (search-forward "\n\n" nil t)
1006 (run-hooks 'mime-view-mode-hook)
1007 (set-buffer-modified-p nil)
1008 (setq buffer-read-only t)
1012 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1013 default-keymap-or-function
1014 representation-type)
1015 "View RAW-BUFFER in MIME-View mode.
1016 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1018 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1019 function. If it is a keymap, keymap of MIME-View mode will be added
1020 to it. If it is a function, it will be bound as default binding of
1021 keymap of MIME-View mode.
1022 Optional argument REPRESENTATION-TYPE is representation-type of
1023 message. It must be nil, `binary' or `cooked'. If it is nil,
1024 `cooked' is used as default."
1027 (setq raw-buffer (current-buffer)))
1028 (or representation-type
1029 (setq representation-type
1031 (set-buffer raw-buffer)
1032 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1033 (assq t mime-raw-representation-type-alist)))
1035 (if (eq representation-type 'binary)
1036 (setq representation-type 'buffer)
1038 (setq preview-buffer (mime-display-message
1039 (mime-open-entity representation-type raw-buffer)
1040 preview-buffer mother default-keymap-or-function))
1041 (or (get-buffer-window preview-buffer)
1042 (let ((r-win (get-buffer-window raw-buffer)))
1044 (set-window-buffer r-win preview-buffer)
1045 (let ((m-win (and mother (get-buffer-window mother))))
1047 (set-window-buffer m-win preview-buffer)
1048 (switch-to-buffer preview-buffer)
1051 (defun mime-view-mode (&optional mother ctl encoding
1052 raw-buffer preview-buffer
1053 default-keymap-or-function)
1054 "Major mode for viewing MIME message.
1056 Here is a list of the standard keys for mime-view-mode.
1061 u Move to upper content
1062 p or M-TAB Move to previous content
1063 n or TAB Move to next content
1064 SPC Scroll up or move to next content
1065 M-SPC or DEL Scroll down or move to previous content
1066 RET Move to next line
1067 M-RET Move to previous line
1068 v Decode current content as `play mode'
1069 e Decode current content as `extract mode'
1070 C-c C-p Decode current content as `print mode'
1071 a Followup to current content.
1073 button-2 Move to point under the mouse cursor
1074 and decode current content as `play mode'
1077 (unless mime-view-redisplay
1079 (if raw-buffer (set-buffer raw-buffer))
1082 (or (assq major-mode mime-raw-representation-type-alist)
1083 (assq t mime-raw-representation-type-alist)))))
1084 (if (eq type 'binary)
1087 (setq mime-message-structure (mime-open-entity type raw-buffer))
1088 (or (mime-entity-content-type mime-message-structure)
1089 (mime-entity-set-content-type-internal
1090 mime-message-structure ctl))
1092 (or (mime-entity-encoding mime-message-structure)
1093 (mime-entity-set-encoding-internal mime-message-structure encoding))
1095 (mime-display-message mime-message-structure preview-buffer
1096 mother default-keymap-or-function)
1103 (autoload 'mime-preview-play-current-entity "mime-play"
1104 "Play current entity." t)
1106 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1107 "Extract current entity into file (maybe).
1108 It decodes current entity to call internal or external method as
1109 \"extract\" mode. The method is selected from variable
1110 `mime-acting-condition'."
1112 (mime-preview-play-current-entity ignore-examples "extract")
1115 (defun mime-preview-print-current-entity (&optional ignore-examples)
1116 "Print current entity (maybe).
1117 It decodes current entity to call internal or external method as
1118 \"print\" mode. The method is selected from variable
1119 `mime-acting-condition'."
1121 (mime-preview-play-current-entity ignore-examples "print")
1128 (defun mime-preview-follow-current-entity ()
1129 "Write follow message to current entity.
1130 It calls following-method selected from variable
1131 `mime-preview-following-method-alist'."
1134 (while (null (setq entity
1135 (get-text-property (point) 'mime-view-entity)))
1139 (previous-single-property-change (point) 'mime-view-entity))
1142 (entity-node-id (mime-entity-node-id entity))
1143 (len (length entity-node-id))
1147 (if (eq (next-single-property-change (point-min)
1153 ((eq (next-single-property-change p-beg 'mime-view-entity)
1155 (setq p-beg (point))
1157 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1159 (setq p-end (point-max))
1161 ((null entity-node-id)
1162 (setq p-end (point-max))
1170 (next-single-property-change
1171 (point) 'mime-view-entity))
1173 (let ((rc (mime-entity-node-id
1174 (get-text-property (point)
1175 'mime-view-entity))))
1176 (or (equal entity-node-id
1177 (nthcdr (- (length rc) len) rc))
1182 (setq p-end (point-max))
1186 (previous-single-property-change p-end 'mime-view-entity-header))
1187 (if (or (null ph-end)
1191 (let* ((mode (mime-preview-original-major-mode 'recursive))
1193 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1195 (the-buf (current-buffer))
1198 (set-buffer (setq new-buf (get-buffer-create new-name)))
1200 (insert-buffer-substring the-buf ph-end p-end)
1201 (when (= ph-end p-beg)
1202 (goto-char (point-min))
1204 (goto-char (point-min))
1205 (let ((current-entity
1206 (if (and (eq (mime-entity-media-type entity) 'message)
1207 (eq (mime-entity-media-subtype entity) 'rfc822))
1208 (mime-entity-children entity)
1211 (while (and current-entity
1214 (with-current-buffer
1215 (mime-entity-header-buffer current-entity)
1218 (mime-entity-header-start-point
1220 (mime-entity-header-end-point
1222 (std11-header-string-except
1225 (apply (function regexp-or) fields)
1227 (if (and (eq (mime-entity-media-type
1228 current-entity) 'message)
1229 (eq (mime-entity-media-subtype
1230 current-entity) 'rfc822))
1236 (setq fields (std11-collect-field-names)
1237 current-entity (mime-entity-parent current-entity))
1240 (let ((rest mime-view-following-required-fields-list)
1243 (setq field-name (car rest))
1244 (or (std11-field-body field-name)
1247 (set-buffer the-buf)
1249 (when mime-mother-buffer
1250 (set-buffer mime-mother-buffer)
1251 (mime-entity-fetch-field
1252 (get-text-property (point)
1256 (insert (concat field-name ": " ret "\n"))
1258 (setq rest (cdr rest))
1260 (mime-decode-header-in-buffer)
1262 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1267 "Sorry, following method for %s is not implemented yet."
1276 (defun mime-preview-move-to-upper ()
1277 "Move to upper entity.
1278 If there is no upper entity, call function `mime-preview-quit'."
1281 (while (null (setq cinfo
1282 (get-text-property (point) 'mime-view-entity)))
1285 (let ((r (mime-entity-parent cinfo))
1288 (while (setq point (previous-single-property-change
1289 (point) 'mime-view-entity))
1291 (if (eq r (get-text-property (point) 'mime-view-entity))
1298 (defun mime-preview-move-to-previous ()
1299 "Move to previous entity.
1300 If there is no previous entity, it calls function registered in
1301 variable `mime-preview-over-to-previous-method-alist'."
1303 (while (null (get-text-property (point) 'mime-view-entity))
1306 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1308 (if (get-text-property (1- point) 'mime-view-entity)
1310 (goto-char (1- point))
1311 (mime-preview-move-to-previous)
1313 (let ((f (assq (mime-preview-original-major-mode)
1314 mime-preview-over-to-previous-method-alist)))
1320 (defun mime-preview-move-to-next ()
1321 "Move to next entity.
1322 If there is no previous entity, it calls function registered in
1323 variable `mime-preview-over-to-next-method-alist'."
1325 (while (and (not (eobp))
1326 (null (get-text-property (point) 'mime-view-entity)))
1329 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1333 (if (null (get-text-property point 'mime-view-entity))
1334 (mime-preview-move-to-next)
1336 (let ((f (assq (mime-preview-original-major-mode)
1337 mime-preview-over-to-next-method-alist)))
1343 (defun mime-preview-scroll-up-entity (&optional h)
1344 "Scroll up current entity.
1345 If reached to (point-max), it calls function registered in variable
1346 `mime-preview-over-to-next-method-alist'."
1349 (setq h (1- (window-height)))
1351 (if (= (point) (point-max))
1352 (let ((f (assq (mime-preview-original-major-mode)
1353 mime-preview-over-to-next-method-alist)))
1358 (or (next-single-property-change (point) 'mime-view-entity)
1361 (if (> (point) point)
1366 (defun mime-preview-scroll-down-entity (&optional h)
1367 "Scroll down current entity.
1368 If reached to (point-min), it calls function registered in variable
1369 `mime-preview-over-to-previous-method-alist'."
1372 (setq h (1- (window-height)))
1374 (if (= (point) (point-min))
1375 (let ((f (assq (mime-preview-original-major-mode)
1376 mime-preview-over-to-previous-method-alist)))
1381 (or (previous-single-property-change (point) 'mime-view-entity)
1383 (forward-line (- h))
1384 (if (< (point) point)
1388 (defun mime-preview-next-line-entity ()
1390 (mime-preview-scroll-up-entity 1)
1393 (defun mime-preview-previous-line-entity ()
1395 (mime-preview-scroll-down-entity 1)
1402 (defun mime-preview-quit ()
1403 "Quit from MIME-preview buffer.
1404 It calls function registered in variable
1405 `mime-preview-quitting-method-alist'."
1407 (let ((r (assq (mime-preview-original-major-mode)
1408 mime-preview-quitting-method-alist)))
1413 (defun mime-preview-kill-buffer ()
1415 (kill-buffer (current-buffer))
1422 (provide 'mime-view)
1424 (run-hooks 'mime-view-load-hook)
1426 ;;; mime-view.el ends here