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
135 (if (> (point) (buffer-size))
136 (max (1- (point-max)) (point-min))
138 'mime-view-situation)))))
141 ;;; @ entity information
144 (defun mime-entity-situation (entity &optional situation)
145 "Return situation of ENTITY."
146 (let (rest param name)
148 (unless (assq 'type situation)
149 (setq rest (or (mime-entity-content-type entity)
150 (make-mime-content-type 'text 'plain))
151 situation (cons (car rest) situation)
154 (unless (assq 'subtype situation)
156 (setq rest (or (cdr (mime-entity-content-type entity))
157 '((subtype . plain)))))
158 (setq situation (cons (car rest) situation)
162 (setq param (car rest))
163 (or (assoc (car param) situation)
164 (setq situation (cons param situation)))
165 (setq rest (cdr rest)))
167 ;; Content-Disposition
169 (unless (assq 'disposition-type situation)
170 (setq rest (mime-entity-content-disposition entity))
172 (setq situation (cons (cons 'disposition-type
173 (mime-content-disposition-type rest))
175 rest (mime-content-disposition-parameters rest))
178 (setq param (car rest)
180 (if (cond ((string= name "filename")
181 (if (assq 'filename situation)
183 (setq name 'filename)))
184 ((string= name "creation-date")
185 (if (assq 'creation-date situation)
187 (setq name 'creation-date)))
188 ((string= name "modification-date")
189 (if (assq 'modification-date situation)
191 (setq name 'modification-date)))
192 ((string= name "read-date")
193 (if (assq 'read-date situation)
195 (setq name 'read-date)))
196 ((string= name "size")
197 (if (assq 'size situation)
200 (t (setq name (cons 'disposition name))
201 (if (assoc name situation)
205 (cons (cons name (cdr param))
207 (setq rest (cdr rest)))
209 ;; Content-Transfer-Encoding
210 (or (assq 'encoding situation)
212 (cons (cons 'encoding (or (mime-entity-encoding entity)
217 ;; (or (assq 'major-mode situation)
219 ;; (cons (cons 'major-mode
220 ;; (with-current-buffer (mime-entity-buffer entity)
226 (defun mime-view-entity-title (entity)
227 (or (mime-entity-read-field entity 'Content-Description)
228 (mime-entity-read-field entity 'Subject)
229 (mime-entity-filename entity)
233 ;; (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
234 ;; "Return entity-node-id from POINT in mime-raw-buffer.
235 ;; If optional argument MESSAGE-INFO is not specified,
236 ;; `mime-message-structure' is used."
237 ;; (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
239 ;; (make-obsolete 'mime-raw-point-to-entity-node-id "don't use it.")
241 ;; (defsubst mime-raw-point-to-entity-number (point &optional message-info)
242 ;; "Return entity-number from POINT in mime-raw-buffer.
243 ;; If optional argument MESSAGE-INFO is not specified,
244 ;; `mime-message-structure' is used."
245 ;; (mime-entity-number (mime-raw-find-entity-from-point point message-info)))
247 ;; (make-obsolete 'mime-raw-point-to-entity-number "don't use it.")
249 ;; (defun mime-raw-flatten-message-info (&optional message-info)
250 ;; "Return list of entity in mime-raw-buffer.
251 ;; If optional argument MESSAGE-INFO is not specified,
252 ;; `mime-message-structure' is used."
254 ;; (setq message-info mime-message-structure))
255 ;; (let ((dest (list message-info))
256 ;; (rcl (mime-entity-children message-info)))
258 ;; (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
259 ;; (setq rcl (cdr rcl)))
263 ;;; @ presentation of preview
269 ;;; @@@ predicate function
272 (defun mime-view-entity-button-visible-p (entity)
273 "Return non-nil if header of ENTITY is visible.
274 Please redefine this function if you want to change default setting."
275 (let ((media-type (mime-entity-media-type entity))
276 (media-subtype (mime-entity-media-subtype entity)))
277 (or (not (eq media-type 'application))
278 (and (not (eq media-subtype 'x-selection))
279 (or (not (eq media-subtype 'octet-stream))
280 (let ((mother-entity (mime-entity-parent entity)))
281 (or (not (eq (mime-entity-media-type mother-entity)
283 (not (eq (mime-entity-media-subtype mother-entity)
288 ;;; @@@ entity button generator
291 (defun mime-view-insert-entity-button (entity)
292 "Insert entity-button of ENTITY."
293 (let ((entity-node-id (mime-entity-node-id entity))
294 (params (mime-entity-parameters entity))
295 (subject (mime-view-entity-title entity)))
297 (let ((access-type (assoc "access-type" params))
298 (num (or (cdr (assoc "x-part-number" params))
299 (if (consp entity-node-id)
302 (format "%s" (1+ num))
304 (reverse entity-node-id) ".")
308 (let ((server (assoc "server" params)))
309 (setq access-type (cdr access-type))
311 (format "%s %s ([%s] %s)"
312 num subject access-type (cdr server))
313 (let ((site (cdr (assoc "site" params)))
314 (dir (cdr (assoc "directory" params)))
315 (url (cdr (assoc "url" params)))
318 (format "%s %s ([%s] %s)"
319 num subject access-type url)
320 (format "%s %s ([%s] %s:%s)"
321 num subject access-type site dir))
325 (let ((media-type (mime-entity-media-type entity))
326 (media-subtype (mime-entity-media-subtype entity))
327 (charset (cdr (assoc "charset" params)))
328 (encoding (mime-entity-encoding entity)))
332 (format " <%s/%s%s%s>"
333 media-type media-subtype
335 (concat "; " charset)
338 (concat " (" encoding ")")
340 (if (>= (+ (current-column)(length rest))(window-width))
344 (function mime-preview-play-current-entity))
351 (defvar mime-header-presentation-method-alist nil
352 "Alist of major mode vs. corresponding header-presentation-method functions.
353 Each element looks like (SYMBOL . FUNCTION).
354 SYMBOL must be major mode in raw-buffer or t. t means default.
355 Interface of FUNCTION must be (ENTITY SITUATION).")
357 (defvar mime-view-ignored-field-list
358 '(".*Received:" ".*Path:" ".*Id:" "^References:"
359 "^Replied:" "^Errors-To:"
360 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
361 "^Content-Type:" "^Precedence:"
362 "^Status:" "^X-VM-.*:")
363 "All fields that match this list will be hidden in MIME preview buffer.
364 Each elements are regexp of field-name.")
366 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
367 "All fields that match this list will be displayed in MIME preview buffer.
368 Each elements are regexp of field-name.")
374 ;;; @@@ predicate function
377 (in-calist-package 'mime-view)
379 (defun mime-calist::field-match-method-as-default-rule (calist
380 field-type field-value)
381 (let ((s-field (assq field-type calist)))
382 (cond ((null s-field)
383 (cons (cons field-type field-value) calist)
387 (define-calist-field-match-method
388 'header #'mime-calist::field-match-method-as-default-rule)
390 (define-calist-field-match-method
391 'body #'mime-calist::field-match-method-as-default-rule)
394 (defvar mime-preview-condition nil
395 "Condition-tree about how to display entity.")
397 (ctree-set-calist-strictly
398 'mime-preview-condition '((type . application)(subtype . octet-stream)
401 (ctree-set-calist-strictly
402 'mime-preview-condition '((type . application)(subtype . octet-stream)
405 (ctree-set-calist-strictly
406 'mime-preview-condition '((type . application)(subtype . octet-stream)
410 (ctree-set-calist-strictly
411 'mime-preview-condition '((type . application)(subtype . pgp)
414 (ctree-set-calist-strictly
415 'mime-preview-condition '((type . application)(subtype . x-latex)
418 (ctree-set-calist-strictly
419 'mime-preview-condition '((type . application)(subtype . x-selection)
422 (ctree-set-calist-strictly
423 'mime-preview-condition '((type . application)(subtype . x-comment)
426 (ctree-set-calist-strictly
427 'mime-preview-condition '((type . message)(subtype . delivery-status)
430 (ctree-set-calist-strictly
431 'mime-preview-condition
433 (body-presentation-method . mime-display-text/plain)))
435 (ctree-set-calist-strictly
436 'mime-preview-condition
439 (body-presentation-method . mime-display-text/plain)))
441 (ctree-set-calist-strictly
442 'mime-preview-condition
443 '((type . text)(subtype . enriched)
445 (body-presentation-method . mime-display-text/enriched)))
447 (ctree-set-calist-strictly
448 'mime-preview-condition
449 '((type . text)(subtype . richtext)
451 (body-presentation-method . mime-display-text/richtext)))
453 (ctree-set-calist-strictly
454 'mime-preview-condition
455 '((type . text)(subtype . t)
457 (body-presentation-method . mime-display-text/plain)))
459 (ctree-set-calist-strictly
460 'mime-preview-condition
461 '((type . multipart)(subtype . alternative)
463 (body-presentation-method . mime-display-multipart/alternative)))
465 (ctree-set-calist-strictly
466 'mime-preview-condition '((type . message)(subtype . partial)
467 (body-presentation-method
468 . mime-display-message/partial-button)))
470 (ctree-set-calist-strictly
471 'mime-preview-condition '((type . message)(subtype . rfc822)
472 (body-presentation-method . nil)
473 (childrens-situation (header . visible)
474 (entity-button . invisible))))
476 (ctree-set-calist-strictly
477 'mime-preview-condition '((type . message)(subtype . news)
478 (body-presentation-method . nil)
479 (childrens-situation (header . visible)
480 (entity-button . invisible))))
483 ;;; @@@ entity presentation
486 (defun mime-display-text/plain (entity situation)
488 (narrow-to-region (point-max)(point-max))
489 (mime-insert-text-content entity)
490 (run-hooks 'mime-text-decode-hook)
491 (goto-char (point-max))
492 (if (not (eq (char-after (1- (point))) ?\n))
495 (mime-add-url-buttons)
496 (run-hooks 'mime-display-text/plain-hook)
499 (defun mime-display-text/richtext (entity situation)
501 (narrow-to-region (point-max)(point-max))
502 (mime-insert-text-content entity)
503 (run-hooks 'mime-text-decode-hook)
504 (let ((beg (point-min)))
505 (remove-text-properties beg (point-max) '(face nil))
506 (richtext-decode beg (point-max))
509 (defun mime-display-text/enriched (entity situation)
511 (narrow-to-region (point-max)(point-max))
512 (mime-insert-text-content entity)
513 (run-hooks 'mime-text-decode-hook)
514 (let ((beg (point-min)))
515 (remove-text-properties beg (point-max) '(face nil))
516 (enriched-decode beg (point-max))
519 (defvar mime-view-announcement-for-message/partial
520 (if (and (>= emacs-major-version 19) window-system)
522 \[[ This is message/partial style split message. ]]
523 \[[ Please press `v' key in this buffer ]]
524 \[[ or click here by mouse button-2. ]]"
526 \[[ This is message/partial style split message. ]]
527 \[[ Please press `v' key in this buffer. ]]"
530 (defun mime-display-message/partial-button (&optional entity situation)
532 (goto-char (point-max))
533 (if (not (search-backward "\n\n" nil t))
536 (goto-char (point-max))
537 (narrow-to-region (point-max)(point-max))
538 (insert mime-view-announcement-for-message/partial)
539 (mime-add-button (point-min)(point-max)
540 #'mime-preview-play-current-entity)
543 (defun mime-display-multipart/mixed (entity situation)
544 (let ((children (mime-entity-children entity))
545 (original-major-mode (cdr (assq 'major-mode situation)))
547 (cdr (assq 'childrens-situation situation))))
548 (if original-major-mode
549 (setq default-situation
550 (cons (cons 'major-mode original-major-mode)
554 (mime-display-entity (car children) nil default-situation)
555 (setq children (cdr children))
558 (defcustom mime-view-type-subtype-score-alist
559 '(((text . enriched) . 3)
560 ((text . richtext) . 2)
563 "Alist MEDIA-TYPE vs corresponding score.
564 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
566 :type '(repeat (cons (choice :tag "Media-Type"
567 (cons :tag "Type/Subtype"
568 (symbol :tag "Primary-type")
569 (symbol :tag "Subtype"))
571 (const :tag "Default" t))
574 (defun mime-display-multipart/alternative (entity situation)
575 (let* ((children (mime-entity-children entity))
576 (original-major-mode (cdr (assq 'major-mode situation)))
578 (cdr (assq 'childrens-situation situation)))
583 (if original-major-mode
584 (setq default-situation
585 (cons (cons 'major-mode original-major-mode)
592 (or (ctree-match-calist
593 mime-preview-condition
594 (append (mime-entity-situation child)
597 (if (cdr (assq 'body-presentation-method situation))
602 (cdr (assq 'type situation))
603 (cdr (assq 'subtype situation)))
604 mime-view-type-subtype-score-alist)
606 (cdr (assq 'type situation))
607 mime-view-type-subtype-score-alist)
610 mime-view-type-subtype-score-alist)
612 (if (> score max-score)
622 (let ((child (car children))
623 (situation (car situations)))
624 (mime-display-entity child (if (= i p)
626 (del-alist 'body-presentation-method
627 (copy-alist situation))))
629 (setq children (cdr children)
630 situations (cdr situations)
635 ;;; @ acting-condition
638 (defvar mime-acting-condition nil
639 "Condition-tree about how to process entity.")
641 (if (file-readable-p mailcap-file)
642 (let ((entries (mailcap-parse-file)))
644 (let ((entry (car entries))
647 (let* ((field (car entry))
648 (field-type (car field)))
649 (cond ((eq field-type 'view) (setq view field))
650 ((eq field-type 'print) (setq print field))
651 ((memq field-type '(compose composetyped edit)))
652 (t (setq shared (cons field shared))))
654 (setq entry (cdr entry))
656 (setq shared (nreverse shared))
657 (ctree-set-calist-with-default
658 'mime-acting-condition
659 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
661 (ctree-set-calist-with-default
662 'mime-acting-condition
664 (list '(mode . "print")(cons 'method (cdr view))))
667 (setq entries (cdr entries))
670 (ctree-set-calist-strictly
671 'mime-acting-condition
672 '((type . application)(subtype . octet-stream)
674 (method . mime-detect-content)
677 (ctree-set-calist-with-default
678 'mime-acting-condition
680 (method . mime-save-content)))
682 (ctree-set-calist-strictly
683 'mime-acting-condition
684 '((type . text)(subtype . x-rot13-47)(mode . "play")
685 (method . mime-view-caesar)
687 (ctree-set-calist-strictly
688 'mime-acting-condition
689 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
690 (method . mime-view-caesar)
693 (ctree-set-calist-strictly
694 'mime-acting-condition
695 '((type . message)(subtype . rfc822)(mode . "play")
696 (method . mime-view-message/rfc822)
698 (ctree-set-calist-strictly
699 'mime-acting-condition
700 '((type . message)(subtype . partial)(mode . "play")
701 (method . mime-store-message/partial-piece)
704 (ctree-set-calist-strictly
705 'mime-acting-condition
706 '((type . message)(subtype . external-body)
707 ("access-type" . "anon-ftp")
708 (method . mime-view-message/external-anon-ftp)
711 (ctree-set-calist-strictly
712 'mime-acting-condition
713 '((type . message)(subtype . external-body)
714 ("access-type" . "url")
715 (method . mime-view-message/external-url)
718 (ctree-set-calist-strictly
719 'mime-acting-condition
720 '((type . application)(subtype . octet-stream)
721 (method . mime-save-content)
725 ;;; @ quitting method
728 (defvar mime-preview-quitting-method-alist
729 '((mime-show-message-mode
730 . mime-preview-quitting-method-for-mime-show-message-mode))
731 "Alist of major-mode vs. quitting-method of mime-view.")
733 (defvar mime-preview-over-to-previous-method-alist nil
734 "Alist of major-mode vs. over-to-previous-method of mime-view.")
736 (defvar mime-preview-over-to-next-method-alist nil
737 "Alist of major-mode vs. over-to-next-method of mime-view.")
740 ;;; @ following method
743 (defvar mime-preview-following-method-alist nil
744 "Alist of major-mode vs. following-method of mime-view.")
746 (defvar mime-view-following-required-fields-list
753 (defun mime-display-entity (entity &optional situation
754 default-situation preview-buffer)
756 (setq preview-buffer (current-buffer)))
757 (let* (e nb ne nhb nbb)
758 (mime-goto-header-start-point entity)
759 (in-calist-package 'mime-view)
762 (or (ctree-match-calist mime-preview-condition
763 (append (mime-entity-situation entity)
766 (let ((button-is-invisible
767 (eq (cdr (assq 'entity-button situation)) 'invisible))
769 (eq (cdr (assq 'header situation)) 'visible))
770 (header-presentation-method
771 (or (cdr (assq 'header-presentation-method situation))
772 (cdr (assq major-mode mime-header-presentation-method-alist))))
773 (body-presentation-method
774 (cdr (assq 'body-presentation-method situation)))
775 (children (mime-entity-children entity)))
776 (set-buffer preview-buffer)
778 (narrow-to-region nb nb)
779 (or button-is-invisible
780 (if (mime-view-entity-button-visible-p entity)
781 (mime-view-insert-entity-button entity)
783 (when header-is-visible
785 (if header-presentation-method
786 (funcall header-presentation-method entity situation)
787 (mime-insert-header entity
788 mime-view-ignored-field-list
789 mime-view-visible-field-list))
790 (run-hooks 'mime-display-header-hook)
791 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
792 (goto-char (point-max))
797 ((functionp body-presentation-method)
798 (funcall body-presentation-method entity situation)
801 (when button-is-invisible
802 (goto-char (point-max))
803 (mime-view-insert-entity-button entity)
805 (or header-is-visible
807 (goto-char (point-max))
811 (setq ne (point-max))
813 (put-text-property nb ne 'mime-view-entity entity)
814 (put-text-property nb ne 'mime-view-situation situation)
815 (put-text-property nbb ne 'mime-view-entity-body entity)
818 (if (functionp body-presentation-method)
819 (funcall body-presentation-method entity situation)
820 (mime-display-multipart/mixed entity situation)
825 ;;; @ MIME viewer mode
828 (defconst mime-view-menu-title "MIME-View")
829 (defconst mime-view-menu-list
830 '((up "Move to upper entity" mime-preview-move-to-upper)
831 (previous "Move to previous entity" mime-preview-move-to-previous)
832 (next "Move to next entity" mime-preview-move-to-next)
833 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
834 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
835 (play "Play current entity" mime-preview-play-current-entity)
836 (extract "Extract current entity" mime-preview-extract-current-entity)
837 (print "Print current entity" mime-preview-print-current-entity)
839 "Menu for MIME Viewer")
841 (cond ((featurep 'xemacs)
842 (defvar mime-view-xemacs-popup-menu
843 (cons mime-view-menu-title
846 (vector (nth 1 item)(nth 2 item) t)
848 mime-view-menu-list)))
849 (defun mime-view-xemacs-popup-menu (event)
850 "Popup the menu in the MIME Viewer buffer"
852 (select-window (event-window event))
853 (set-buffer (event-buffer event))
854 (popup-menu 'mime-view-xemacs-popup-menu))
855 (defvar mouse-button-2 'button2)
858 (defvar mouse-button-2 [mouse-2])
861 (defun mime-view-define-keymap (&optional default)
862 (let ((mime-view-mode-map (if (keymapp default)
863 (copy-keymap default)
866 (define-key mime-view-mode-map
867 "u" (function mime-preview-move-to-upper))
868 (define-key mime-view-mode-map
869 "p" (function mime-preview-move-to-previous))
870 (define-key mime-view-mode-map
871 "n" (function mime-preview-move-to-next))
872 (define-key mime-view-mode-map
873 "\e\t" (function mime-preview-move-to-previous))
874 (define-key mime-view-mode-map
875 "\t" (function mime-preview-move-to-next))
876 (define-key mime-view-mode-map
877 " " (function mime-preview-scroll-up-entity))
878 (define-key mime-view-mode-map
879 "\M- " (function mime-preview-scroll-down-entity))
880 (define-key mime-view-mode-map
881 "\177" (function mime-preview-scroll-down-entity))
882 (define-key mime-view-mode-map
883 "\C-m" (function mime-preview-next-line-entity))
884 (define-key mime-view-mode-map
885 "\C-\M-m" (function mime-preview-previous-line-entity))
886 (define-key mime-view-mode-map
887 "v" (function mime-preview-play-current-entity))
888 (define-key mime-view-mode-map
889 "e" (function mime-preview-extract-current-entity))
890 (define-key mime-view-mode-map
891 "\C-c\C-p" (function mime-preview-print-current-entity))
892 (define-key mime-view-mode-map
893 "a" (function mime-preview-follow-current-entity))
894 (define-key mime-view-mode-map
895 "q" (function mime-preview-quit))
896 (define-key mime-view-mode-map
897 "\C-c\C-x" (function mime-preview-kill-buffer))
898 ;; (define-key mime-view-mode-map
899 ;; "<" (function beginning-of-buffer))
900 ;; (define-key mime-view-mode-map
901 ;; ">" (function end-of-buffer))
902 (define-key mime-view-mode-map
903 "?" (function describe-mode))
904 (define-key mime-view-mode-map
905 [tab] (function mime-preview-move-to-next))
906 (define-key mime-view-mode-map
907 [delete] (function mime-preview-scroll-down-entity))
908 (define-key mime-view-mode-map
909 [backspace] (function mime-preview-scroll-down-entity))
910 (if (functionp default)
911 (cond ((featurep 'xemacs)
912 (set-keymap-default-binding mime-view-mode-map default)
915 (setq mime-view-mode-map
916 (append mime-view-mode-map (list (cons t default))))
919 (define-key mime-view-mode-map
920 mouse-button-2 (function mime-button-dispatcher))
922 (cond ((featurep 'xemacs)
923 (define-key mime-view-mode-map
924 mouse-button-3 (function mime-view-xemacs-popup-menu))
926 ((>= emacs-major-version 19)
927 (define-key mime-view-mode-map [menu-bar mime-view]
928 (cons mime-view-menu-title
929 (make-sparse-keymap mime-view-menu-title)))
932 (define-key mime-view-mode-map
933 (vector 'menu-bar 'mime-view (car item))
934 (cons (nth 1 item)(nth 2 item))
937 (reverse mime-view-menu-list)
940 (use-local-map mime-view-mode-map)
941 (run-hooks 'mime-view-define-keymap-hook)
944 (defsubst mime-maybe-hide-echo-buffer ()
945 "Clear mime-echo buffer and delete window for it."
946 (let ((buf (get-buffer mime-echo-buffer-name)))
951 (let ((win (get-buffer-window buf)))
958 (defvar mime-view-redisplay nil)
961 (defun mime-display-message (message &optional preview-buffer
962 mother default-keymap-or-function
964 "View MESSAGE in MIME-View mode.
966 Optional argument PREVIEW-BUFFER specifies the buffer of the
967 presentation. It must be either nil or a name of preview buffer.
969 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
971 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
972 function. If it is a keymap, keymap of MIME-View mode will be added
973 to it. If it is a function, it will be bound as default binding of
974 keymap of MIME-View mode."
975 (mime-maybe-hide-echo-buffer)
976 (let ((win-conf (current-window-configuration)))
979 (concat "*Preview-" (mime-entity-name message) "*")))
980 (or original-major-mode
981 (setq original-major-mode
982 (with-current-buffer (mime-entity-header-buffer message)
984 (let ((inhibit-read-only t))
985 (set-buffer (get-buffer-create preview-buffer))
989 (setq mime-mother-buffer mother)
991 (setq mime-preview-original-window-configuration win-conf)
992 (setq major-mode 'mime-view-mode)
993 (setq mode-name "MIME-View")
994 (mime-display-entity message nil
995 `((entity-button . invisible)
997 (major-mode . ,original-major-mode))
999 (mime-view-define-keymap default-keymap-or-function)
1001 (next-single-property-change (point-min) 'mime-view-entity)))
1004 (goto-char (point-min))
1005 (search-forward "\n\n" nil t)
1007 (run-hooks 'mime-view-mode-hook)
1008 (set-buffer-modified-p nil)
1009 (setq buffer-read-only t)
1013 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1014 default-keymap-or-function
1015 representation-type)
1016 "View RAW-BUFFER in MIME-View mode.
1017 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1019 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1020 function. If it is a keymap, keymap of MIME-View mode will be added
1021 to it. If it is a function, it will be bound as default binding of
1022 keymap of MIME-View mode.
1023 Optional argument REPRESENTATION-TYPE is representation-type of
1024 message. It must be nil, `binary' or `cooked'. If it is nil,
1025 `cooked' is used as default."
1028 (setq raw-buffer (current-buffer)))
1029 (or representation-type
1030 (setq representation-type
1032 (set-buffer raw-buffer)
1033 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1034 (assq t mime-raw-representation-type-alist)))
1036 (if (eq representation-type 'binary)
1037 (setq representation-type 'buffer)
1039 (setq preview-buffer (mime-display-message
1040 (mime-open-entity representation-type raw-buffer)
1041 preview-buffer mother default-keymap-or-function))
1042 (or (get-buffer-window preview-buffer)
1043 (let ((r-win (get-buffer-window raw-buffer)))
1045 (set-window-buffer r-win preview-buffer)
1046 (let ((m-win (and mother (get-buffer-window mother))))
1048 (set-window-buffer m-win preview-buffer)
1049 (switch-to-buffer preview-buffer)
1052 (defun mime-view-mode (&optional mother ctl encoding
1053 raw-buffer preview-buffer
1054 default-keymap-or-function)
1055 "Major mode for viewing MIME message.
1057 Here is a list of the standard keys for mime-view-mode.
1062 u Move to upper content
1063 p or M-TAB Move to previous content
1064 n or TAB Move to next content
1065 SPC Scroll up or move to next content
1066 M-SPC or DEL Scroll down or move to previous content
1067 RET Move to next line
1068 M-RET Move to previous line
1069 v Decode current content as `play mode'
1070 e Decode current content as `extract mode'
1071 C-c C-p Decode current content as `print mode'
1072 a Followup to current content.
1074 button-2 Move to point under the mouse cursor
1075 and decode current content as `play mode'
1078 (unless mime-view-redisplay
1080 (if raw-buffer (set-buffer raw-buffer))
1083 (or (assq major-mode mime-raw-representation-type-alist)
1084 (assq t mime-raw-representation-type-alist)))))
1085 (if (eq type 'binary)
1088 (setq mime-message-structure (mime-open-entity type raw-buffer))
1089 (or (mime-entity-content-type mime-message-structure)
1090 (mime-entity-set-content-type-internal
1091 mime-message-structure ctl))
1093 (or (mime-entity-encoding mime-message-structure)
1094 (mime-entity-set-encoding-internal mime-message-structure encoding))
1096 (mime-display-message mime-message-structure preview-buffer
1097 mother default-keymap-or-function)
1104 (autoload 'mime-preview-play-current-entity "mime-play"
1105 "Play current entity." t)
1107 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1108 "Extract current entity into file (maybe).
1109 It decodes current entity to call internal or external method as
1110 \"extract\" mode. The method is selected from variable
1111 `mime-acting-condition'."
1113 (mime-preview-play-current-entity ignore-examples "extract")
1116 (defun mime-preview-print-current-entity (&optional ignore-examples)
1117 "Print current entity (maybe).
1118 It decodes current entity to call internal or external method as
1119 \"print\" mode. The method is selected from variable
1120 `mime-acting-condition'."
1122 (mime-preview-play-current-entity ignore-examples "print")
1129 (defun mime-preview-follow-current-entity ()
1130 "Write follow message to current entity.
1131 It calls following-method selected from variable
1132 `mime-preview-following-method-alist'."
1135 (while (null (setq entity
1136 (get-text-property (point) 'mime-view-entity)))
1140 (previous-single-property-change (point) 'mime-view-entity))
1143 (entity-node-id (mime-entity-node-id entity))
1144 (len (length entity-node-id))
1148 (if (eq (next-single-property-change (point-min)
1154 ((eq (next-single-property-change p-beg 'mime-view-entity)
1156 (setq p-beg (point))
1158 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1160 (setq p-end (point-max))
1162 ((null entity-node-id)
1163 (setq p-end (point-max))
1171 (next-single-property-change
1172 (point) 'mime-view-entity))
1174 (let ((rc (mime-entity-node-id
1175 (get-text-property (point)
1176 'mime-view-entity))))
1177 (or (equal entity-node-id
1178 (nthcdr (- (length rc) len) rc))
1183 (setq p-end (point-max))
1187 (previous-single-property-change p-end 'mime-view-entity-header))
1188 (if (or (null ph-end)
1192 (let* ((mode (mime-preview-original-major-mode 'recursive))
1194 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1196 (the-buf (current-buffer))
1199 (set-buffer (setq new-buf (get-buffer-create new-name)))
1201 (insert-buffer-substring the-buf ph-end p-end)
1202 (when (= ph-end p-beg)
1203 (goto-char (point-min))
1205 (goto-char (point-min))
1206 (let ((current-entity
1207 (if (and (eq (mime-entity-media-type entity) 'message)
1208 (eq (mime-entity-media-subtype entity) 'rfc822))
1209 (mime-entity-children entity)
1212 (while (and current-entity
1215 (with-current-buffer
1216 (mime-entity-header-buffer current-entity)
1219 (mime-entity-header-start-point
1221 (mime-entity-header-end-point
1223 (std11-header-string-except
1226 (apply (function regexp-or) fields)
1228 (if (and (eq (mime-entity-media-type
1229 current-entity) 'message)
1230 (eq (mime-entity-media-subtype
1231 current-entity) 'rfc822))
1237 (setq fields (std11-collect-field-names)
1238 current-entity (mime-entity-parent current-entity))
1241 (let ((rest mime-view-following-required-fields-list)
1244 (setq field-name (car rest))
1245 (or (std11-field-body field-name)
1248 (set-buffer the-buf)
1250 (when mime-mother-buffer
1251 (set-buffer mime-mother-buffer)
1252 (mime-entity-fetch-field
1253 (get-text-property (point)
1257 (insert (concat field-name ": " ret "\n"))
1259 (setq rest (cdr rest))
1261 (mime-decode-header-in-buffer)
1263 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1268 "Sorry, following method for %s is not implemented yet."
1277 (defun mime-preview-move-to-upper ()
1278 "Move to upper entity.
1279 If there is no upper entity, call function `mime-preview-quit'."
1282 (while (null (setq cinfo
1283 (get-text-property (point) 'mime-view-entity)))
1286 (let ((r (mime-entity-parent cinfo))
1289 (while (setq point (previous-single-property-change
1290 (point) 'mime-view-entity))
1292 (if (eq r (get-text-property (point) 'mime-view-entity))
1299 (defun mime-preview-move-to-previous ()
1300 "Move to previous entity.
1301 If there is no previous entity, it calls function registered in
1302 variable `mime-preview-over-to-previous-method-alist'."
1304 (while (null (get-text-property (point) 'mime-view-entity))
1307 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1309 (if (get-text-property (1- point) 'mime-view-entity)
1311 (goto-char (1- point))
1312 (mime-preview-move-to-previous)
1314 (let ((f (assq (mime-preview-original-major-mode)
1315 mime-preview-over-to-previous-method-alist)))
1321 (defun mime-preview-move-to-next ()
1322 "Move to next entity.
1323 If there is no previous entity, it calls function registered in
1324 variable `mime-preview-over-to-next-method-alist'."
1326 (while (and (not (eobp))
1327 (null (get-text-property (point) 'mime-view-entity)))
1330 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1334 (if (null (get-text-property point 'mime-view-entity))
1335 (mime-preview-move-to-next)
1337 (let ((f (assq (mime-preview-original-major-mode)
1338 mime-preview-over-to-next-method-alist)))
1344 (defun mime-preview-scroll-up-entity (&optional h)
1345 "Scroll up current entity.
1346 If reached to (point-max), it calls function registered in variable
1347 `mime-preview-over-to-next-method-alist'."
1350 (setq h (1- (window-height)))
1352 (if (= (point) (point-max))
1353 (let ((f (assq (mime-preview-original-major-mode)
1354 mime-preview-over-to-next-method-alist)))
1359 (or (next-single-property-change (point) 'mime-view-entity)
1362 (if (> (point) point)
1367 (defun mime-preview-scroll-down-entity (&optional h)
1368 "Scroll down current entity.
1369 If reached to (point-min), it calls function registered in variable
1370 `mime-preview-over-to-previous-method-alist'."
1373 (setq h (1- (window-height)))
1375 (if (= (point) (point-min))
1376 (let ((f (assq (mime-preview-original-major-mode)
1377 mime-preview-over-to-previous-method-alist)))
1382 (or (previous-single-property-change (point) 'mime-view-entity)
1384 (forward-line (- h))
1385 (if (< (point) point)
1389 (defun mime-preview-next-line-entity ()
1391 (mime-preview-scroll-up-entity 1)
1394 (defun mime-preview-previous-line-entity ()
1396 (mime-preview-scroll-down-entity 1)
1403 (defun mime-preview-quit ()
1404 "Quit from MIME-preview buffer.
1405 It calls function registered in variable
1406 `mime-preview-quitting-method-alist'."
1408 (let ((r (assq (mime-preview-original-major-mode)
1409 mime-preview-quitting-method-alist)))
1414 (defun mime-preview-kill-buffer ()
1416 (kill-buffer (current-buffer))
1423 (provide 'mime-view)
1425 (run-hooks 'mime-view-load-hook)
1427 ;;; mime-view.el ends here