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)))
105 ;;; @ in preview-buffer (presentation space)
108 (defvar mime-mother-buffer nil
109 "Mother buffer corresponding with the (MIME-preview) buffer.
110 If current MIME-preview buffer is generated by other buffer, such as
111 message/partial, it is called `mother-buffer'.")
112 (make-variable-buffer-local 'mime-mother-buffer)
114 (defvar mime-raw-buffer nil
115 "Raw buffer corresponding with the (MIME-preview) buffer.")
116 (make-variable-buffer-local 'mime-raw-buffer)
118 (defvar mime-preview-original-window-configuration nil
119 "Window-configuration before mime-view-mode is called.")
120 (make-variable-buffer-local 'mime-preview-original-window-configuration)
122 (defun mime-preview-original-major-mode (&optional recursive)
123 "Return major-mode of original buffer.
124 If optional argument RECURSIVE is non-nil and current buffer has
125 mime-mother-buffer, it returns original major-mode of the
127 (if (and recursive mime-mother-buffer)
129 (set-buffer mime-mother-buffer)
130 (mime-preview-original-major-mode recursive)
135 (get-text-property (point-min) 'mime-view-entity)))
139 ;;; @ entity information
142 (defun mime-entity-situation (entity &optional situation)
143 "Return situation of ENTITY."
144 (let (rest param name)
146 (unless (assq 'type situation)
147 (setq rest (or (mime-entity-content-type entity)
148 (make-mime-content-type 'text 'plain))
149 situation (cons (car rest) situation)
152 (unless (assq 'subtype situation)
154 (setq rest (or (cdr (mime-entity-content-type entity))
155 '((subtype . plain)))))
156 (setq situation (cons (car rest) situation)
160 (setq param (car rest))
161 (or (assoc (car param) situation)
162 (setq situation (cons param situation)))
163 (setq rest (cdr rest)))
165 ;; Content-Disposition
167 (unless (assq 'disposition-type situation)
168 (setq rest (mime-entity-content-disposition entity))
170 (setq situation (cons (cons 'disposition-type
171 (mime-content-disposition-type rest))
173 rest (mime-content-disposition-parameters rest))
176 (setq param (car rest)
178 (if (cond ((string= name "filename")
179 (if (assq 'filename situation)
181 (setq name 'filename)))
182 ((string= name "creation-date")
183 (if (assq 'creation-date situation)
185 (setq name 'creation-date)))
186 ((string= name "modification-date")
187 (if (assq 'modification-date situation)
189 (setq name 'modification-date)))
190 ((string= name "read-date")
191 (if (assq 'read-date situation)
193 (setq name 'read-date)))
194 ((string= name "size")
195 (if (assq 'size situation)
198 (t (setq name (cons 'disposition name))
199 (if (assoc name situation)
203 (cons (cons name (cdr param))
205 (setq rest (cdr rest)))
207 ;; Content-Transfer-Encoding
208 (or (assq 'encoding situation)
210 (cons (cons 'encoding (or (mime-entity-encoding entity)
215 (or (assq 'major-mode situation)
217 (cons (cons 'major-mode
218 (with-current-buffer (mime-entity-buffer entity)
224 (defun mime-view-entity-title (entity)
225 (or (mime-read-field 'Content-Description entity)
226 (mime-read-field 'Subject entity)
227 (mime-entity-filename entity)
231 (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
232 "Return entity-node-id from POINT in mime-raw-buffer.
233 If optional argument MESSAGE-INFO is not specified,
234 `mime-message-structure' is used."
235 (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
237 (defsubst mime-raw-point-to-entity-number (point &optional message-info)
238 "Return entity-number from POINT in mime-raw-buffer.
239 If optional argument MESSAGE-INFO is not specified,
240 `mime-message-structure' is used."
241 (mime-entity-number (mime-raw-find-entity-from-point point message-info)))
243 (defun mime-raw-flatten-message-info (&optional message-info)
244 "Return list of entity in mime-raw-buffer.
245 If optional argument MESSAGE-INFO is not specified,
246 `mime-message-structure' is used."
248 (setq message-info mime-message-structure))
249 (let ((dest (list message-info))
250 (rcl (mime-entity-children message-info)))
252 (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
253 (setq rcl (cdr rcl)))
257 ;;; @ presentation of preview
263 ;;; @@@ predicate function
266 (defun mime-view-entity-button-visible-p (entity)
267 "Return non-nil if header of ENTITY is visible.
268 Please redefine this function if you want to change default setting."
269 (let ((media-type (mime-entity-media-type entity))
270 (media-subtype (mime-entity-media-subtype entity)))
271 (or (not (eq media-type 'application))
272 (and (not (eq media-subtype 'x-selection))
273 (or (not (eq media-subtype 'octet-stream))
274 (let ((mother-entity (mime-entity-parent entity)))
275 (or (not (eq (mime-entity-media-type mother-entity)
277 (not (eq (mime-entity-media-subtype mother-entity)
282 ;;; @@@ entity button generator
285 (defun mime-view-insert-entity-button (entity)
286 "Insert entity-button of ENTITY."
287 (let ((entity-node-id (mime-entity-node-id entity))
288 (params (mime-entity-parameters entity))
289 (subject (mime-view-entity-title entity)))
291 (let ((access-type (assoc "access-type" params))
292 (num (or (cdr (assoc "x-part-number" params))
293 (if (consp entity-node-id)
296 (format "%s" (1+ num))
298 (reverse entity-node-id) ".")
302 (let ((server (assoc "server" params)))
303 (setq access-type (cdr access-type))
305 (format "%s %s ([%s] %s)"
306 num subject access-type (cdr server))
307 (let ((site (cdr (assoc "site" params)))
308 (dir (cdr (assoc "directory" params)))
309 (url (cdr (assoc "url" params)))
312 (format "%s %s ([%s] %s)"
313 num subject access-type url)
314 (format "%s %s ([%s] %s:%s)"
315 num subject access-type site dir))
319 (let ((media-type (mime-entity-media-type entity))
320 (media-subtype (mime-entity-media-subtype entity))
321 (charset (cdr (assoc "charset" params)))
322 (encoding (mime-entity-encoding entity)))
326 (format " <%s/%s%s%s>"
327 media-type media-subtype
329 (concat "; " charset)
332 (concat " (" encoding ")")
334 (if (>= (+ (current-column)(length rest))(window-width))
338 (function mime-preview-play-current-entity))
345 (defvar mime-header-presentation-method-alist nil
346 "Alist of major mode vs. corresponding header-presentation-method functions.
347 Each element looks like (SYMBOL . FUNCTION).
348 SYMBOL must be major mode in raw-buffer or t. t means default.
349 Interface of FUNCTION must be (ENTITY SITUATION).")
351 (defvar mime-view-ignored-field-list
352 '(".*Received:" ".*Path:" ".*Id:" "^References:"
353 "^Replied:" "^Errors-To:"
354 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
355 "^Content-Type:" "^Precedence:"
356 "^Status:" "^X-VM-.*:")
357 "All fields that match this list will be hidden in MIME preview buffer.
358 Each elements are regexp of field-name.")
360 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
361 "All fields that match this list will be displayed in MIME preview buffer.
362 Each elements are regexp of field-name.")
368 ;;; @@@ predicate function
371 (in-calist-package 'mime-view)
373 (defun mime-calist::field-match-method-as-default-rule (calist
374 field-type field-value)
375 (let ((s-field (assq field-type calist)))
376 (cond ((null s-field)
377 (cons (cons field-type field-value) calist)
381 (define-calist-field-match-method
382 'header #'mime-calist::field-match-method-as-default-rule)
384 (define-calist-field-match-method
385 'body #'mime-calist::field-match-method-as-default-rule)
388 (defvar mime-preview-condition nil
389 "Condition-tree about how to display entity.")
391 (ctree-set-calist-strictly
392 'mime-preview-condition '((type . application)(subtype . octet-stream)
395 (ctree-set-calist-strictly
396 'mime-preview-condition '((type . application)(subtype . octet-stream)
399 (ctree-set-calist-strictly
400 'mime-preview-condition '((type . application)(subtype . octet-stream)
404 (ctree-set-calist-strictly
405 'mime-preview-condition '((type . application)(subtype . pgp)
408 (ctree-set-calist-strictly
409 'mime-preview-condition '((type . application)(subtype . x-latex)
412 (ctree-set-calist-strictly
413 'mime-preview-condition '((type . application)(subtype . x-selection)
416 (ctree-set-calist-strictly
417 'mime-preview-condition '((type . application)(subtype . x-comment)
420 (ctree-set-calist-strictly
421 'mime-preview-condition '((type . message)(subtype . delivery-status)
424 (ctree-set-calist-strictly
425 'mime-preview-condition
427 (body-presentation-method . mime-display-text/plain)))
429 (ctree-set-calist-strictly
430 'mime-preview-condition
433 (body-presentation-method . mime-display-text/plain)))
435 (ctree-set-calist-strictly
436 'mime-preview-condition
437 '((type . text)(subtype . enriched)
439 (body-presentation-method . mime-display-text/enriched)))
441 (ctree-set-calist-strictly
442 'mime-preview-condition
443 '((type . text)(subtype . richtext)
445 (body-presentation-method . mime-display-text/richtext)))
447 (ctree-set-calist-strictly
448 'mime-preview-condition
449 '((type . text)(subtype . t)
451 (body-presentation-method . mime-display-text/plain)))
453 (ctree-set-calist-strictly
454 'mime-preview-condition
455 '((type . multipart)(subtype . alternative)
457 (body-presentation-method . mime-display-multipart/alternative)))
459 (ctree-set-calist-strictly
460 'mime-preview-condition '((type . message)(subtype . partial)
461 (body-presentation-method
462 . mime-display-message/partial-button)))
464 (ctree-set-calist-strictly
465 'mime-preview-condition '((type . message)(subtype . rfc822)
466 (body-presentation-method . nil)
467 (childrens-situation (header . visible)
468 (entity-button . invisible))))
470 (ctree-set-calist-strictly
471 'mime-preview-condition '((type . message)(subtype . news)
472 (body-presentation-method . nil)
473 (childrens-situation (header . visible)
474 (entity-button . invisible))))
477 ;;; @@@ entity presentation
480 (defun mime-display-text/plain (entity situation)
482 (narrow-to-region (point-max)(point-max))
483 (mime-insert-text-content entity)
484 (run-hooks 'mime-text-decode-hook)
485 (goto-char (point-max))
486 (if (not (eq (char-after (1- (point))) ?\n))
489 (mime-add-url-buttons)
490 (run-hooks 'mime-display-text/plain-hook)
493 (defun mime-display-text/richtext (entity situation)
495 (narrow-to-region (point-max)(point-max))
496 (mime-insert-text-content entity)
497 (run-hooks 'mime-text-decode-hook)
498 (let ((beg (point-min)))
499 (remove-text-properties beg (point-max) '(face nil))
500 (richtext-decode beg (point-max))
503 (defun mime-display-text/enriched (entity situation)
505 (narrow-to-region (point-max)(point-max))
506 (mime-insert-text-content entity)
507 (run-hooks 'mime-text-decode-hook)
508 (let ((beg (point-min)))
509 (remove-text-properties beg (point-max) '(face nil))
510 (enriched-decode beg (point-max))
513 (defvar mime-view-announcement-for-message/partial
514 (if (and (>= emacs-major-version 19) window-system)
516 \[[ This is message/partial style split message. ]]
517 \[[ Please press `v' key in this buffer ]]
518 \[[ or click here by mouse button-2. ]]"
520 \[[ This is message/partial style split message. ]]
521 \[[ Please press `v' key in this buffer. ]]"
524 (defun mime-display-message/partial-button (&optional entity situation)
526 (goto-char (point-max))
527 (if (not (search-backward "\n\n" nil t))
530 (goto-char (point-max))
531 (narrow-to-region (point-max)(point-max))
532 (insert mime-view-announcement-for-message/partial)
533 (mime-add-button (point-min)(point-max)
534 #'mime-preview-play-current-entity)
537 (defun mime-display-multipart/mixed (entity situation)
538 (let ((children (mime-entity-children entity))
540 (cdr (assq 'childrens-situation situation))))
542 (mime-display-entity (car children) nil default-situation)
543 (setq children (cdr children))
546 (defcustom mime-view-type-subtype-score-alist
547 '(((text . enriched) . 3)
548 ((text . richtext) . 2)
551 "Alist MEDIA-TYPE vs corresponding score.
552 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
554 :type '(repeat (cons (choice :tag "Media-Type"
555 (cons :tag "Type/Subtype"
556 (symbol :tag "Primary-type")
557 (symbol :tag "Subtype"))
559 (const :tag "Default" t))
562 (defun mime-display-multipart/alternative (entity situation)
563 (let* ((children (mime-entity-children entity))
565 (cdr (assq 'childrens-situation situation)))
573 (or (ctree-match-calist
574 mime-preview-condition
575 (append (mime-entity-situation child)
578 (if (cdr (assq 'body-presentation-method situation))
583 (cdr (assq 'type situation))
584 (cdr (assq 'subtype situation)))
585 mime-view-type-subtype-score-alist)
587 (cdr (assq 'type situation))
588 mime-view-type-subtype-score-alist)
591 mime-view-type-subtype-score-alist)
593 (if (> score max-score)
603 (let ((child (car children))
604 (situation (car situations)))
605 (mime-display-entity child (if (= i p)
607 (del-alist 'body-presentation-method
608 (copy-alist situation))))
610 (setq children (cdr children)
611 situations (cdr situations)
616 ;;; @ acting-condition
619 (defvar mime-acting-condition nil
620 "Condition-tree about how to process entity.")
622 (if (file-readable-p mailcap-file)
623 (let ((entries (mailcap-parse-file)))
625 (let ((entry (car entries))
628 (let* ((field (car entry))
629 (field-type (car field)))
630 (cond ((eq field-type 'view) (setq view field))
631 ((eq field-type 'print) (setq print field))
632 ((memq field-type '(compose composetyped edit)))
633 (t (setq shared (cons field shared))))
635 (setq entry (cdr entry))
637 (setq shared (nreverse shared))
638 (ctree-set-calist-with-default
639 'mime-acting-condition
640 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
642 (ctree-set-calist-with-default
643 'mime-acting-condition
645 (list '(mode . "print")(cons 'method (cdr view))))
648 (setq entries (cdr entries))
651 (ctree-set-calist-strictly
652 'mime-acting-condition
653 '((type . application)(subtype . octet-stream)
655 (method . mime-detect-content)
658 (ctree-set-calist-with-default
659 'mime-acting-condition
661 (method . mime-save-content)))
663 (ctree-set-calist-strictly
664 'mime-acting-condition
665 '((type . text)(subtype . x-rot13-47)(mode . "play")
666 (method . mime-view-caesar)
668 (ctree-set-calist-strictly
669 'mime-acting-condition
670 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
671 (method . mime-view-caesar)
674 (ctree-set-calist-strictly
675 'mime-acting-condition
676 '((type . message)(subtype . rfc822)(mode . "play")
677 (method . mime-view-message/rfc822)
679 (ctree-set-calist-strictly
680 'mime-acting-condition
681 '((type . message)(subtype . partial)(mode . "play")
682 (method . mime-store-message/partial-piece)
685 (ctree-set-calist-strictly
686 'mime-acting-condition
687 '((type . message)(subtype . external-body)
688 ("access-type" . "anon-ftp")
689 (method . mime-view-message/external-anon-ftp)
692 (ctree-set-calist-strictly
693 'mime-acting-condition
694 '((type . message)(subtype . external-body)
695 ("access-type" . "url")
696 (method . mime-view-message/external-url)
699 (ctree-set-calist-strictly
700 'mime-acting-condition
701 '((type . application)(subtype . octet-stream)
702 (method . mime-save-content)
706 ;;; @ quitting method
709 (defvar mime-preview-quitting-method-alist
710 '((mime-show-message-mode
711 . mime-preview-quitting-method-for-mime-show-message-mode))
712 "Alist of major-mode vs. quitting-method of mime-view.")
714 (defvar mime-preview-over-to-previous-method-alist nil
715 "Alist of major-mode vs. over-to-previous-method of mime-view.")
717 (defvar mime-preview-over-to-next-method-alist nil
718 "Alist of major-mode vs. over-to-next-method of mime-view.")
721 ;;; @ following method
724 (defvar mime-preview-following-method-alist nil
725 "Alist of major-mode vs. following-method of mime-view.")
727 (defvar mime-view-following-required-fields-list
734 (defun mime-display-entity (entity &optional situation
735 default-situation preview-buffer)
737 (setq preview-buffer (current-buffer)))
738 (let* ((raw-buffer (mime-entity-buffer entity))
739 (start (mime-entity-point-min entity))
741 (set-buffer raw-buffer)
743 (in-calist-package 'mime-view)
746 (or (ctree-match-calist mime-preview-condition
747 (append (mime-entity-situation entity)
750 (let ((button-is-invisible
751 (eq (cdr (assq 'entity-button situation)) 'invisible))
753 (eq (cdr (assq 'header situation)) 'visible))
754 (header-presentation-method
755 (or (cdr (assq 'header-presentation-method situation))
756 (cdr (assq major-mode mime-header-presentation-method-alist))))
757 (body-presentation-method
758 (cdr (assq 'body-presentation-method situation)))
759 (children (mime-entity-children entity)))
760 (set-buffer preview-buffer)
762 (narrow-to-region nb nb)
763 (or button-is-invisible
764 (if (mime-view-entity-button-visible-p entity)
765 (mime-view-insert-entity-button entity)
767 (when header-is-visible
769 (if header-presentation-method
770 (funcall header-presentation-method entity situation)
771 (mime-insert-header entity
772 mime-view-ignored-field-list
773 mime-view-visible-field-list))
774 (run-hooks 'mime-display-header-hook)
775 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
776 (goto-char (point-max))
781 ((functionp body-presentation-method)
782 (funcall body-presentation-method entity situation)
785 (when button-is-invisible
786 (goto-char (point-max))
787 (mime-view-insert-entity-button entity)
789 (or header-is-visible
791 (goto-char (point-max))
795 (setq ne (point-max))
797 (put-text-property nb ne 'mime-view-entity entity)
798 (put-text-property nbb ne 'mime-view-entity-body entity)
801 (if (functionp body-presentation-method)
802 (funcall body-presentation-method entity situation)
803 (mime-display-multipart/mixed entity situation)
808 ;;; @ MIME viewer mode
811 (defconst mime-view-menu-title "MIME-View")
812 (defconst mime-view-menu-list
813 '((up "Move to upper entity" mime-preview-move-to-upper)
814 (previous "Move to previous entity" mime-preview-move-to-previous)
815 (next "Move to next entity" mime-preview-move-to-next)
816 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
817 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
818 (play "Play current entity" mime-preview-play-current-entity)
819 (extract "Extract current entity" mime-preview-extract-current-entity)
820 (print "Print current entity" mime-preview-print-current-entity)
822 "Menu for MIME Viewer")
824 (cond ((featurep 'xemacs)
825 (defvar mime-view-xemacs-popup-menu
826 (cons mime-view-menu-title
829 (vector (nth 1 item)(nth 2 item) t)
831 mime-view-menu-list)))
832 (defun mime-view-xemacs-popup-menu (event)
833 "Popup the menu in the MIME Viewer buffer"
835 (select-window (event-window event))
836 (set-buffer (event-buffer event))
837 (popup-menu 'mime-view-xemacs-popup-menu))
838 (defvar mouse-button-2 'button2)
841 (defvar mouse-button-2 [mouse-2])
844 (defun mime-view-define-keymap (&optional default)
845 (let ((mime-view-mode-map (if (keymapp default)
846 (copy-keymap default)
849 (define-key mime-view-mode-map
850 "u" (function mime-preview-move-to-upper))
851 (define-key mime-view-mode-map
852 "p" (function mime-preview-move-to-previous))
853 (define-key mime-view-mode-map
854 "n" (function mime-preview-move-to-next))
855 (define-key mime-view-mode-map
856 "\e\t" (function mime-preview-move-to-previous))
857 (define-key mime-view-mode-map
858 "\t" (function mime-preview-move-to-next))
859 (define-key mime-view-mode-map
860 " " (function mime-preview-scroll-up-entity))
861 (define-key mime-view-mode-map
862 "\M- " (function mime-preview-scroll-down-entity))
863 (define-key mime-view-mode-map
864 "\177" (function mime-preview-scroll-down-entity))
865 (define-key mime-view-mode-map
866 "\C-m" (function mime-preview-next-line-entity))
867 (define-key mime-view-mode-map
868 "\C-\M-m" (function mime-preview-previous-line-entity))
869 (define-key mime-view-mode-map
870 "v" (function mime-preview-play-current-entity))
871 (define-key mime-view-mode-map
872 "e" (function mime-preview-extract-current-entity))
873 (define-key mime-view-mode-map
874 "\C-c\C-p" (function mime-preview-print-current-entity))
875 (define-key mime-view-mode-map
876 "a" (function mime-preview-follow-current-entity))
877 (define-key mime-view-mode-map
878 "q" (function mime-preview-quit))
879 (define-key mime-view-mode-map
880 "\C-c\C-x" (function mime-preview-kill-buffer))
881 ;; (define-key mime-view-mode-map
882 ;; "<" (function beginning-of-buffer))
883 ;; (define-key mime-view-mode-map
884 ;; ">" (function end-of-buffer))
885 (define-key mime-view-mode-map
886 "?" (function describe-mode))
887 (define-key mime-view-mode-map
888 [tab] (function mime-preview-move-to-next))
889 (define-key mime-view-mode-map
890 [delete] (function mime-preview-scroll-down-entity))
891 (define-key mime-view-mode-map
892 [backspace] (function mime-preview-scroll-down-entity))
893 (if (functionp default)
894 (cond ((featurep 'xemacs)
895 (set-keymap-default-binding mime-view-mode-map default)
898 (setq mime-view-mode-map
899 (append mime-view-mode-map (list (cons t default))))
902 (define-key mime-view-mode-map
903 mouse-button-2 (function mime-button-dispatcher))
905 (cond ((featurep 'xemacs)
906 (define-key mime-view-mode-map
907 mouse-button-3 (function mime-view-xemacs-popup-menu))
909 ((>= emacs-major-version 19)
910 (define-key mime-view-mode-map [menu-bar mime-view]
911 (cons mime-view-menu-title
912 (make-sparse-keymap mime-view-menu-title)))
915 (define-key mime-view-mode-map
916 (vector 'menu-bar 'mime-view (car item))
917 (cons (nth 1 item)(nth 2 item))
920 (reverse mime-view-menu-list)
923 (use-local-map mime-view-mode-map)
924 (run-hooks 'mime-view-define-keymap-hook)
927 (defsubst mime-maybe-hide-echo-buffer ()
928 "Clear mime-echo buffer and delete window for it."
929 (let ((buf (get-buffer mime-echo-buffer-name)))
934 (let ((win (get-buffer-window buf)))
941 (defvar mime-view-redisplay nil)
944 (defun mime-display-message (message &optional preview-buffer
945 mother default-keymap-or-function)
946 "View MESSAGE in MIME-View mode.
948 Optional argument PREVIEW-BUFFER specifies the buffer of the
949 presentation. It must be either nil or a name of preview buffer.
951 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
953 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
954 function. If it is a keymap, keymap of MIME-View mode will be added
955 to it. If it is a function, it will be bound as default binding of
956 keymap of MIME-View mode."
957 (mime-maybe-hide-echo-buffer)
958 (let ((win-conf (current-window-configuration))
959 (raw-buffer (mime-entity-buffer message)))
962 (concat "*Preview-" (buffer-name raw-buffer) "*")))
963 (set-buffer raw-buffer)
964 (setq mime-preview-buffer preview-buffer)
965 (let ((inhibit-read-only t))
966 (set-buffer (get-buffer-create preview-buffer))
969 (setq mime-raw-buffer raw-buffer)
971 (setq mime-mother-buffer mother)
973 (setq mime-preview-original-window-configuration win-conf)
974 (setq major-mode 'mime-view-mode)
975 (setq mode-name "MIME-View")
976 (mime-display-entity message nil
977 '((entity-button . invisible)
980 (mime-view-define-keymap default-keymap-or-function)
982 (next-single-property-change (point-min) 'mime-view-entity)))
985 (goto-char (point-min))
986 (search-forward "\n\n" nil t)
988 (run-hooks 'mime-view-mode-hook)
989 (set-buffer-modified-p nil)
990 (setq buffer-read-only t)
991 (or (get-buffer-window preview-buffer)
992 (let ((r-win (get-buffer-window raw-buffer)))
994 (set-window-buffer r-win preview-buffer)
995 (let ((m-win (and mother (get-buffer-window mother))))
997 (set-window-buffer m-win preview-buffer)
998 (switch-to-buffer preview-buffer)
1003 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1004 default-keymap-or-function
1005 representation-type)
1006 "View RAW-BUFFER in MIME-View mode.
1007 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1009 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1010 function. If it is a keymap, keymap of MIME-View mode will be added
1011 to it. If it is a function, it will be bound as default binding of
1012 keymap of MIME-View mode.
1013 Optional argument REPRESENTATION-TYPE is representation-type of
1014 message. It must be nil, `binary' or `cooked'. If it is nil,
1015 `cooked' is used as default."
1018 (setq raw-buffer (current-buffer)))
1019 (or representation-type
1020 (setq representation-type
1022 (set-buffer raw-buffer)
1023 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1024 (assq t mime-raw-representation-type-alist)))
1026 (if (eq representation-type 'binary)
1027 (setq representation-type 'buffer)
1029 (mime-display-message
1030 (mime-open-entity representation-type raw-buffer)
1031 preview-buffer mother default-keymap-or-function))
1033 (defun mime-view-mode (&optional mother ctl encoding
1034 raw-buffer preview-buffer
1035 default-keymap-or-function)
1036 "Major mode for viewing MIME message.
1038 Here is a list of the standard keys for mime-view-mode.
1043 u Move to upper content
1044 p or M-TAB Move to previous content
1045 n or TAB Move to next content
1046 SPC Scroll up or move to next content
1047 M-SPC or DEL Scroll down or move to previous content
1048 RET Move to next line
1049 M-RET Move to previous line
1050 v Decode current content as `play mode'
1051 e Decode current content as `extract mode'
1052 C-c C-p Decode current content as `print mode'
1053 a Followup to current content.
1055 button-2 Move to point under the mouse cursor
1056 and decode current content as `play mode'
1059 (unless mime-view-redisplay
1061 (if raw-buffer (set-buffer raw-buffer))
1064 (or (assq major-mode mime-raw-representation-type-alist)
1065 (assq t mime-raw-representation-type-alist)))))
1066 (if (eq type 'binary)
1069 (setq mime-message-structure (mime-open-entity type raw-buffer))
1070 (or (mime-entity-content-type mime-message-structure)
1071 (mime-entity-set-content-type-internal
1072 mime-message-structure ctl))
1074 (or (mime-entity-encoding mime-message-structure)
1075 (mime-entity-set-encoding-internal mime-message-structure encoding))
1077 (mime-display-message mime-message-structure preview-buffer
1078 mother default-keymap-or-function)
1085 (autoload 'mime-preview-play-current-entity "mime-play"
1086 "Play current entity." t)
1088 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1089 "Extract current entity into file (maybe).
1090 It decodes current entity to call internal or external method as
1091 \"extract\" mode. The method is selected from variable
1092 `mime-acting-condition'."
1094 (mime-preview-play-current-entity ignore-examples "extract")
1097 (defun mime-preview-print-current-entity (&optional ignore-examples)
1098 "Print current entity (maybe).
1099 It decodes current entity to call internal or external method as
1100 \"print\" mode. The method is selected from variable
1101 `mime-acting-condition'."
1103 (mime-preview-play-current-entity ignore-examples "print")
1110 (defun mime-preview-follow-current-entity ()
1111 "Write follow message to current entity.
1112 It calls following-method selected from variable
1113 `mime-preview-following-method-alist'."
1116 (while (null (setq entity
1117 (get-text-property (point) 'mime-view-entity)))
1121 (previous-single-property-change (point) 'mime-view-entity))
1124 (entity-node-id (mime-entity-node-id entity))
1125 (len (length entity-node-id))
1129 (if (eq (next-single-property-change (point-min)
1135 ((eq (next-single-property-change p-beg 'mime-view-entity)
1137 (setq p-beg (point))
1139 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1141 (setq p-end (point-max))
1143 ((null entity-node-id)
1144 (setq p-end (point-max))
1152 (next-single-property-change
1153 (point) 'mime-view-entity))
1155 (let ((rc (mime-entity-node-id
1156 (get-text-property (point)
1157 'mime-view-entity))))
1158 (or (equal entity-node-id
1159 (nthcdr (- (length rc) len) rc))
1164 (setq p-end (point-max))
1168 (previous-single-property-change p-end 'mime-view-entity-header))
1169 (if (or (null ph-end)
1173 (let* ((mode (mime-preview-original-major-mode 'recursive))
1175 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1177 (the-buf (current-buffer))
1178 (a-buf mime-raw-buffer)
1181 (set-buffer (setq new-buf (get-buffer-create new-name)))
1183 (insert-buffer-substring the-buf ph-end p-end)
1184 (when (= ph-end p-beg)
1185 (goto-char (point-min))
1187 (goto-char (point-min))
1188 (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1195 (mime-find-entity-from-node-id entity-node-id))
1198 (mime-entity-point-min ci)
1199 (mime-entity-point-max ci)
1201 (std11-header-string-except
1203 (apply (function regexp-or) fields)
1205 (if (and (eq (mime-entity-media-type ci) 'message)
1206 (eq (mime-entity-media-subtype ci) 'rfc822))
1212 (setq fields (std11-collect-field-names)
1213 entity-node-id (cdr entity-node-id))
1216 (let ((rest mime-view-following-required-fields-list))
1218 (let ((field-name (car rest)))
1219 (or (std11-field-body field-name)
1225 (set-buffer the-buf)
1226 (set-buffer mime-mother-buffer)
1227 (set-buffer mime-raw-buffer)
1228 (std11-field-body field-name)
1232 (setq rest (cdr rest))
1234 (mime-decode-header-in-buffer)
1236 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1241 "Sorry, following method for %s is not implemented yet."
1250 (defun mime-preview-move-to-upper ()
1251 "Move to upper entity.
1252 If there is no upper entity, call function `mime-preview-quit'."
1255 (while (null (setq cinfo
1256 (get-text-property (point) 'mime-view-entity)))
1259 (let ((r (mime-entity-parent cinfo))
1262 (while (setq point (previous-single-property-change
1263 (point) 'mime-view-entity))
1265 (if (eq r (get-text-property (point) 'mime-view-entity))
1272 (defun mime-preview-move-to-previous ()
1273 "Move to previous entity.
1274 If there is no previous entity, it calls function registered in
1275 variable `mime-preview-over-to-previous-method-alist'."
1277 (while (null (get-text-property (point) 'mime-view-entity))
1280 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1282 (if (get-text-property (1- point) 'mime-view-entity)
1284 (goto-char (1- point))
1285 (mime-preview-move-to-previous)
1287 (let ((f (assq (mime-preview-original-major-mode)
1288 mime-preview-over-to-previous-method-alist)))
1294 (defun mime-preview-move-to-next ()
1295 "Move to next entity.
1296 If there is no previous entity, it calls function registered in
1297 variable `mime-preview-over-to-next-method-alist'."
1299 (while (and (not (eobp))
1300 (null (get-text-property (point) 'mime-view-entity)))
1303 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1307 (if (null (get-text-property point 'mime-view-entity))
1308 (mime-preview-move-to-next)
1310 (let ((f (assq (mime-preview-original-major-mode)
1311 mime-preview-over-to-next-method-alist)))
1317 (defun mime-preview-scroll-up-entity (&optional h)
1318 "Scroll up current entity.
1319 If reached to (point-max), it calls function registered in variable
1320 `mime-preview-over-to-next-method-alist'."
1323 (setq h (1- (window-height)))
1325 (if (= (point) (point-max))
1326 (let ((f (assq (mime-preview-original-major-mode)
1327 mime-preview-over-to-next-method-alist)))
1332 (or (next-single-property-change (point) 'mime-view-entity)
1335 (if (> (point) point)
1340 (defun mime-preview-scroll-down-entity (&optional h)
1341 "Scroll down current entity.
1342 If reached to (point-min), it calls function registered in variable
1343 `mime-preview-over-to-previous-method-alist'."
1346 (setq h (1- (window-height)))
1348 (if (= (point) (point-min))
1349 (let ((f (assq (mime-preview-original-major-mode)
1350 mime-preview-over-to-previous-method-alist)))
1355 (or (previous-single-property-change (point) 'mime-view-entity)
1357 (forward-line (- h))
1358 (if (< (point) point)
1362 (defun mime-preview-next-line-entity ()
1364 (mime-preview-scroll-up-entity 1)
1367 (defun mime-preview-previous-line-entity ()
1369 (mime-preview-scroll-down-entity 1)
1376 (defun mime-preview-quit ()
1377 "Quit from MIME-preview buffer.
1378 It calls function registered in variable
1379 `mime-preview-quitting-method-alist'."
1381 (let ((r (assq (mime-preview-original-major-mode)
1382 mime-preview-quitting-method-alist)))
1387 (defun mime-preview-kill-buffer ()
1389 (kill-buffer (current-buffer))
1396 (provide 'mime-view)
1398 (run-hooks 'mime-view-load-hook)
1400 ;;; mime-view.el ends here