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 'original-major-mode
134 (get-text-property (or point (point)) 'mime-view-situation)))))
137 ;;; @ entity information
140 (defun mime-entity-situation (entity &optional situation)
141 "Return situation of ENTITY."
142 (let (rest param name)
144 (unless (assq 'type situation)
145 (setq rest (or (mime-entity-content-type entity)
146 (make-mime-content-type 'text 'plain))
147 situation (cons (car rest) situation)
150 (unless (assq 'subtype situation)
152 (setq rest (or (cdr (mime-entity-content-type entity))
153 '((subtype . plain)))))
154 (setq situation (cons (car rest) situation)
158 (setq param (car rest))
159 (or (assoc (car param) situation)
160 (setq situation (cons param situation)))
161 (setq rest (cdr rest)))
163 ;; Content-Disposition
165 (unless (assq 'disposition-type situation)
166 (setq rest (mime-entity-content-disposition entity))
168 (setq situation (cons (cons 'disposition-type
169 (mime-content-disposition-type rest))
171 rest (mime-content-disposition-parameters rest))
174 (setq param (car rest)
176 (if (cond ((string= name "filename")
177 (if (assq 'filename situation)
179 (setq name 'filename)))
180 ((string= name "creation-date")
181 (if (assq 'creation-date situation)
183 (setq name 'creation-date)))
184 ((string= name "modification-date")
185 (if (assq 'modification-date situation)
187 (setq name 'modification-date)))
188 ((string= name "read-date")
189 (if (assq 'read-date situation)
191 (setq name 'read-date)))
192 ((string= name "size")
193 (if (assq 'size situation)
196 (t (setq name (cons 'disposition name))
197 (if (assoc name situation)
201 (cons (cons name (cdr param))
203 (setq rest (cdr rest)))
205 ;; Content-Transfer-Encoding
206 (or (assq 'encoding situation)
208 (cons (cons 'encoding (or (mime-entity-encoding entity)
213 ;; (or (assq 'major-mode situation)
215 ;; (cons (cons 'major-mode
216 ;; (with-current-buffer (mime-entity-buffer entity)
222 (defun mime-view-entity-title (entity)
223 (or (mime-read-field 'Content-Description entity)
224 (mime-read-field 'Subject entity)
225 (mime-entity-filename entity)
229 ;; (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
230 ;; "Return entity-node-id from POINT in mime-raw-buffer.
231 ;; If optional argument MESSAGE-INFO is not specified,
232 ;; `mime-message-structure' is used."
233 ;; (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
235 ;; (make-obsolete 'mime-raw-point-to-entity-node-id "don't use it.")
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 ;; (make-obsolete 'mime-raw-point-to-entity-number "don't use it.")
245 ;; (defun mime-raw-flatten-message-info (&optional message-info)
246 ;; "Return list of entity in mime-raw-buffer.
247 ;; If optional argument MESSAGE-INFO is not specified,
248 ;; `mime-message-structure' is used."
250 ;; (setq message-info mime-message-structure))
251 ;; (let ((dest (list message-info))
252 ;; (rcl (mime-entity-children message-info)))
254 ;; (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
255 ;; (setq rcl (cdr rcl)))
259 ;;; @ presentation of preview
265 ;;; @@@ predicate function
268 (defun mime-view-entity-button-visible-p (entity)
269 "Return non-nil if header of ENTITY is visible.
270 Please redefine this function if you want to change default setting."
271 (let ((media-type (mime-entity-media-type entity))
272 (media-subtype (mime-entity-media-subtype entity)))
273 (or (not (eq media-type 'application))
274 (and (not (eq media-subtype 'x-selection))
275 (or (not (eq media-subtype 'octet-stream))
276 (let ((mother-entity (mime-entity-parent entity)))
277 (or (not (eq (mime-entity-media-type mother-entity)
279 (not (eq (mime-entity-media-subtype mother-entity)
284 ;;; @@@ entity button generator
287 (defun mime-view-insert-entity-button (entity)
288 "Insert entity-button of ENTITY."
289 (let ((entity-node-id (mime-entity-node-id entity))
290 (params (mime-entity-parameters entity))
291 (subject (mime-view-entity-title entity)))
293 (let ((access-type (assoc "access-type" params))
294 (num (or (cdr (assoc "x-part-number" params))
295 (if (consp entity-node-id)
298 (format "%s" (1+ num))
300 (reverse entity-node-id) ".")
304 (let ((server (assoc "server" params)))
305 (setq access-type (cdr access-type))
307 (format "%s %s ([%s] %s)"
308 num subject access-type (cdr server))
309 (let ((site (cdr (assoc "site" params)))
310 (dir (cdr (assoc "directory" params)))
311 (url (cdr (assoc "url" params)))
314 (format "%s %s ([%s] %s)"
315 num subject access-type url)
316 (format "%s %s ([%s] %s:%s)"
317 num subject access-type site dir))
321 (let ((media-type (mime-entity-media-type entity))
322 (media-subtype (mime-entity-media-subtype entity))
323 (charset (cdr (assoc "charset" params)))
324 (encoding (mime-entity-encoding entity)))
328 (format " <%s/%s%s%s>"
329 media-type media-subtype
331 (concat "; " charset)
334 (concat " (" encoding ")")
336 (if (>= (+ (current-column)(length rest))(window-width))
340 (function mime-preview-play-current-entity))
347 (defvar mime-header-presentation-method-alist nil
348 "Alist of major mode vs. corresponding header-presentation-method functions.
349 Each element looks like (SYMBOL . FUNCTION).
350 SYMBOL must be major mode in raw-buffer or t. t means default.
351 Interface of FUNCTION must be (ENTITY SITUATION).")
353 (defvar mime-view-ignored-field-list
354 '(".*Received:" ".*Path:" ".*Id:" "^References:"
355 "^Replied:" "^Errors-To:"
356 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
357 "^Content-Type:" "^Precedence:"
358 "^Status:" "^X-VM-.*:")
359 "All fields that match this list will be hidden in MIME preview buffer.
360 Each elements are regexp of field-name.")
362 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
363 "All fields that match this list will be displayed in MIME preview buffer.
364 Each elements are regexp of field-name.")
370 ;;; @@@ predicate function
373 (in-calist-package 'mime-view)
375 (defun mime-calist::field-match-method-as-default-rule (calist
376 field-type field-value)
377 (let ((s-field (assq field-type calist)))
378 (cond ((null s-field)
379 (cons (cons field-type field-value) calist)
383 (define-calist-field-match-method
384 'header #'mime-calist::field-match-method-as-default-rule)
386 (define-calist-field-match-method
387 'body #'mime-calist::field-match-method-as-default-rule)
390 (defvar mime-preview-condition nil
391 "Condition-tree about how to display entity.")
393 (ctree-set-calist-strictly
394 'mime-preview-condition '((type . application)(subtype . octet-stream)
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)
406 (ctree-set-calist-strictly
407 'mime-preview-condition '((type . application)(subtype . pgp)
410 (ctree-set-calist-strictly
411 'mime-preview-condition '((type . application)(subtype . x-latex)
414 (ctree-set-calist-strictly
415 'mime-preview-condition '((type . application)(subtype . x-selection)
418 (ctree-set-calist-strictly
419 'mime-preview-condition '((type . application)(subtype . x-comment)
422 (ctree-set-calist-strictly
423 'mime-preview-condition '((type . message)(subtype . delivery-status)
426 (ctree-set-calist-strictly
427 'mime-preview-condition
429 (body-presentation-method . mime-display-text/plain)))
431 (ctree-set-calist-strictly
432 'mime-preview-condition
435 (body-presentation-method . mime-display-text/plain)))
437 (ctree-set-calist-strictly
438 'mime-preview-condition
439 '((type . text)(subtype . enriched)
441 (body-presentation-method . mime-display-text/enriched)))
443 (ctree-set-calist-strictly
444 'mime-preview-condition
445 '((type . text)(subtype . richtext)
447 (body-presentation-method . mime-display-text/richtext)))
449 (ctree-set-calist-strictly
450 'mime-preview-condition
451 '((type . text)(subtype . t)
453 (body-presentation-method . mime-display-text/plain)))
455 (ctree-set-calist-strictly
456 'mime-preview-condition
457 '((type . multipart)(subtype . alternative)
459 (body-presentation-method . mime-display-multipart/alternative)))
461 (ctree-set-calist-strictly
462 'mime-preview-condition '((type . message)(subtype . partial)
463 (body-presentation-method
464 . mime-display-message/partial-button)))
466 (ctree-set-calist-strictly
467 'mime-preview-condition '((type . message)(subtype . rfc822)
468 (body-presentation-method . nil)
469 (childrens-situation (header . visible)
470 (entity-button . invisible))))
472 (ctree-set-calist-strictly
473 'mime-preview-condition '((type . message)(subtype . news)
474 (body-presentation-method . nil)
475 (childrens-situation (header . visible)
476 (entity-button . invisible))))
479 ;;; @@@ entity presentation
482 (defun mime-display-text/plain (entity situation)
484 (narrow-to-region (point-max)(point-max))
485 (mime-insert-text-content entity)
486 (run-hooks 'mime-text-decode-hook)
487 (goto-char (point-max))
488 (if (not (eq (char-after (1- (point))) ?\n))
491 (mime-add-url-buttons)
492 (run-hooks 'mime-display-text/plain-hook)
495 (defun mime-display-text/richtext (entity situation)
497 (narrow-to-region (point-max)(point-max))
498 (mime-insert-text-content entity)
499 (run-hooks 'mime-text-decode-hook)
500 (let ((beg (point-min)))
501 (remove-text-properties beg (point-max) '(face nil))
502 (richtext-decode beg (point-max))
505 (defun mime-display-text/enriched (entity situation)
507 (narrow-to-region (point-max)(point-max))
508 (mime-insert-text-content entity)
509 (run-hooks 'mime-text-decode-hook)
510 (let ((beg (point-min)))
511 (remove-text-properties beg (point-max) '(face nil))
512 (enriched-decode beg (point-max))
515 (defvar mime-view-announcement-for-message/partial
516 (if (and (>= emacs-major-version 19) window-system)
518 \[[ This is message/partial style split message. ]]
519 \[[ Please press `v' key in this buffer ]]
520 \[[ or click here by mouse button-2. ]]"
522 \[[ This is message/partial style split message. ]]
523 \[[ Please press `v' key in this buffer. ]]"
526 (defun mime-display-message/partial-button (&optional entity situation)
528 (goto-char (point-max))
529 (if (not (search-backward "\n\n" nil t))
532 (goto-char (point-max))
533 (narrow-to-region (point-max)(point-max))
534 (insert mime-view-announcement-for-message/partial)
535 (mime-add-button (point-min)(point-max)
536 #'mime-preview-play-current-entity)
539 (defun mime-display-multipart/mixed (entity situation)
540 (let ((children (mime-entity-children entity))
541 (original-major-mode (cdr (assq 'major-mode situation)))
543 (cdr (assq 'childrens-situation situation))))
544 (if original-major-mode
545 (setq default-situation
546 (cons (cons 'major-mode original-major-mode)
550 (mime-display-entity (car children) nil default-situation)
551 (setq children (cdr children))
554 (defcustom mime-view-type-subtype-score-alist
555 '(((text . enriched) . 3)
556 ((text . richtext) . 2)
559 "Alist MEDIA-TYPE vs corresponding score.
560 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
562 :type '(repeat (cons (choice :tag "Media-Type"
563 (cons :tag "Type/Subtype"
564 (symbol :tag "Primary-type")
565 (symbol :tag "Subtype"))
567 (const :tag "Default" t))
570 (defun mime-display-multipart/alternative (entity situation)
571 (let* ((children (mime-entity-children entity))
572 (original-major-mode (cdr (assq 'major-mode situation)))
574 (cdr (assq 'childrens-situation situation)))
579 (if original-major-mode
580 (setq default-situation
581 (cons (cons 'major-mode original-major-mode)
588 (or (ctree-match-calist
589 mime-preview-condition
590 (append (mime-entity-situation child)
593 (if (cdr (assq 'body-presentation-method situation))
598 (cdr (assq 'type situation))
599 (cdr (assq 'subtype situation)))
600 mime-view-type-subtype-score-alist)
602 (cdr (assq 'type situation))
603 mime-view-type-subtype-score-alist)
606 mime-view-type-subtype-score-alist)
608 (if (> score max-score)
618 (let ((child (car children))
619 (situation (car situations)))
620 (mime-display-entity child (if (= i p)
622 (del-alist 'body-presentation-method
623 (copy-alist situation))))
625 (setq children (cdr children)
626 situations (cdr situations)
631 ;;; @ acting-condition
634 (defvar mime-acting-condition nil
635 "Condition-tree about how to process entity.")
637 (if (file-readable-p mailcap-file)
638 (let ((entries (mailcap-parse-file)))
640 (let ((entry (car entries))
643 (let* ((field (car entry))
644 (field-type (car field)))
645 (cond ((eq field-type 'view) (setq view field))
646 ((eq field-type 'print) (setq print field))
647 ((memq field-type '(compose composetyped edit)))
648 (t (setq shared (cons field shared))))
650 (setq entry (cdr entry))
652 (setq shared (nreverse shared))
653 (ctree-set-calist-with-default
654 'mime-acting-condition
655 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
657 (ctree-set-calist-with-default
658 'mime-acting-condition
660 (list '(mode . "print")(cons 'method (cdr view))))
663 (setq entries (cdr entries))
666 (ctree-set-calist-strictly
667 'mime-acting-condition
668 '((type . application)(subtype . octet-stream)
670 (method . mime-detect-content)
673 (ctree-set-calist-with-default
674 'mime-acting-condition
676 (method . mime-save-content)))
678 (ctree-set-calist-strictly
679 'mime-acting-condition
680 '((type . text)(subtype . x-rot13-47)(mode . "play")
681 (method . mime-view-caesar)
683 (ctree-set-calist-strictly
684 'mime-acting-condition
685 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
686 (method . mime-view-caesar)
689 (ctree-set-calist-strictly
690 'mime-acting-condition
691 '((type . message)(subtype . rfc822)(mode . "play")
692 (method . mime-view-message/rfc822)
694 (ctree-set-calist-strictly
695 'mime-acting-condition
696 '((type . message)(subtype . partial)(mode . "play")
697 (method . mime-store-message/partial-piece)
700 (ctree-set-calist-strictly
701 'mime-acting-condition
702 '((type . message)(subtype . external-body)
703 ("access-type" . "anon-ftp")
704 (method . mime-view-message/external-anon-ftp)
707 (ctree-set-calist-strictly
708 'mime-acting-condition
709 '((type . message)(subtype . external-body)
710 ("access-type" . "url")
711 (method . mime-view-message/external-url)
714 (ctree-set-calist-strictly
715 'mime-acting-condition
716 '((type . application)(subtype . octet-stream)
717 (method . mime-save-content)
721 ;;; @ quitting method
724 (defvar mime-preview-quitting-method-alist
725 '((mime-show-message-mode
726 . mime-preview-quitting-method-for-mime-show-message-mode))
727 "Alist of major-mode vs. quitting-method of mime-view.")
729 (defvar mime-preview-over-to-previous-method-alist nil
730 "Alist of major-mode vs. over-to-previous-method of mime-view.")
732 (defvar mime-preview-over-to-next-method-alist nil
733 "Alist of major-mode vs. over-to-next-method of mime-view.")
736 ;;; @ following method
739 (defvar mime-preview-following-method-alist nil
740 "Alist of major-mode vs. following-method of mime-view.")
742 (defvar mime-view-following-required-fields-list
749 (defun mime-display-entity (entity &optional situation
750 default-situation preview-buffer)
752 (setq preview-buffer (current-buffer)))
753 (let* (e nb ne nhb nbb)
754 (mime-goto-header-start-point entity)
755 (in-calist-package 'mime-view)
758 (or (ctree-match-calist mime-preview-condition
759 (append (mime-entity-situation entity)
762 (let ((button-is-invisible
763 (eq (cdr (assq 'entity-button situation)) 'invisible))
765 (eq (cdr (assq 'header situation)) 'visible))
766 (header-presentation-method
767 (or (cdr (assq 'header-presentation-method situation))
768 (cdr (assq major-mode mime-header-presentation-method-alist))))
769 (body-presentation-method
770 (cdr (assq 'body-presentation-method situation)))
771 (children (mime-entity-children entity)))
772 (set-buffer preview-buffer)
774 (narrow-to-region nb nb)
775 (or button-is-invisible
776 (if (mime-view-entity-button-visible-p entity)
777 (mime-view-insert-entity-button entity)
779 (when header-is-visible
781 (if header-presentation-method
782 (funcall header-presentation-method entity situation)
783 (mime-insert-header entity
784 mime-view-ignored-field-list
785 mime-view-visible-field-list))
786 (run-hooks 'mime-display-header-hook)
787 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
788 (goto-char (point-max))
793 ((functionp body-presentation-method)
794 (funcall body-presentation-method entity situation)
797 (when button-is-invisible
798 (goto-char (point-max))
799 (mime-view-insert-entity-button entity)
801 (or header-is-visible
803 (goto-char (point-max))
807 (setq ne (point-max))
809 (put-text-property nb ne 'mime-view-entity entity)
810 (put-text-property nb ne 'mime-view-situation situation)
811 (put-text-property nbb ne 'mime-view-entity-body entity)
814 (if (functionp body-presentation-method)
815 (funcall body-presentation-method entity situation)
816 (mime-display-multipart/mixed entity situation)
821 ;;; @ MIME viewer mode
824 (defconst mime-view-menu-title "MIME-View")
825 (defconst mime-view-menu-list
826 '((up "Move to upper entity" mime-preview-move-to-upper)
827 (previous "Move to previous entity" mime-preview-move-to-previous)
828 (next "Move to next entity" mime-preview-move-to-next)
829 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
830 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
831 (play "Play current entity" mime-preview-play-current-entity)
832 (extract "Extract current entity" mime-preview-extract-current-entity)
833 (print "Print current entity" mime-preview-print-current-entity)
835 "Menu for MIME Viewer")
837 (cond ((featurep 'xemacs)
838 (defvar mime-view-xemacs-popup-menu
839 (cons mime-view-menu-title
842 (vector (nth 1 item)(nth 2 item) t)
844 mime-view-menu-list)))
845 (defun mime-view-xemacs-popup-menu (event)
846 "Popup the menu in the MIME Viewer buffer"
848 (select-window (event-window event))
849 (set-buffer (event-buffer event))
850 (popup-menu 'mime-view-xemacs-popup-menu))
851 (defvar mouse-button-2 'button2)
854 (defvar mouse-button-2 [mouse-2])
857 (defun mime-view-define-keymap (&optional default)
858 (let ((mime-view-mode-map (if (keymapp default)
859 (copy-keymap default)
862 (define-key mime-view-mode-map
863 "u" (function mime-preview-move-to-upper))
864 (define-key mime-view-mode-map
865 "p" (function mime-preview-move-to-previous))
866 (define-key mime-view-mode-map
867 "n" (function mime-preview-move-to-next))
868 (define-key mime-view-mode-map
869 "\e\t" (function mime-preview-move-to-previous))
870 (define-key mime-view-mode-map
871 "\t" (function mime-preview-move-to-next))
872 (define-key mime-view-mode-map
873 " " (function mime-preview-scroll-up-entity))
874 (define-key mime-view-mode-map
875 "\M- " (function mime-preview-scroll-down-entity))
876 (define-key mime-view-mode-map
877 "\177" (function mime-preview-scroll-down-entity))
878 (define-key mime-view-mode-map
879 "\C-m" (function mime-preview-next-line-entity))
880 (define-key mime-view-mode-map
881 "\C-\M-m" (function mime-preview-previous-line-entity))
882 (define-key mime-view-mode-map
883 "v" (function mime-preview-play-current-entity))
884 (define-key mime-view-mode-map
885 "e" (function mime-preview-extract-current-entity))
886 (define-key mime-view-mode-map
887 "\C-c\C-p" (function mime-preview-print-current-entity))
888 (define-key mime-view-mode-map
889 "a" (function mime-preview-follow-current-entity))
890 (define-key mime-view-mode-map
891 "q" (function mime-preview-quit))
892 (define-key mime-view-mode-map
893 "\C-c\C-x" (function mime-preview-kill-buffer))
894 ;; (define-key mime-view-mode-map
895 ;; "<" (function beginning-of-buffer))
896 ;; (define-key mime-view-mode-map
897 ;; ">" (function end-of-buffer))
898 (define-key mime-view-mode-map
899 "?" (function describe-mode))
900 (define-key mime-view-mode-map
901 [tab] (function mime-preview-move-to-next))
902 (define-key mime-view-mode-map
903 [delete] (function mime-preview-scroll-down-entity))
904 (define-key mime-view-mode-map
905 [backspace] (function mime-preview-scroll-down-entity))
906 (if (functionp default)
907 (cond ((featurep 'xemacs)
908 (set-keymap-default-binding mime-view-mode-map default)
911 (setq mime-view-mode-map
912 (append mime-view-mode-map (list (cons t default))))
915 (define-key mime-view-mode-map
916 mouse-button-2 (function mime-button-dispatcher))
918 (cond ((featurep 'xemacs)
919 (define-key mime-view-mode-map
920 mouse-button-3 (function mime-view-xemacs-popup-menu))
922 ((>= emacs-major-version 19)
923 (define-key mime-view-mode-map [menu-bar mime-view]
924 (cons mime-view-menu-title
925 (make-sparse-keymap mime-view-menu-title)))
928 (define-key mime-view-mode-map
929 (vector 'menu-bar 'mime-view (car item))
930 (cons (nth 1 item)(nth 2 item))
933 (reverse mime-view-menu-list)
936 (use-local-map mime-view-mode-map)
937 (run-hooks 'mime-view-define-keymap-hook)
940 (defsubst mime-maybe-hide-echo-buffer ()
941 "Clear mime-echo buffer and delete window for it."
942 (let ((buf (get-buffer mime-echo-buffer-name)))
947 (let ((win (get-buffer-window buf)))
954 (defvar mime-view-redisplay nil)
957 (defun mime-display-message (message &optional preview-buffer
958 mother default-keymap-or-function
960 "View MESSAGE in MIME-View mode.
962 Optional argument PREVIEW-BUFFER specifies the buffer of the
963 presentation. It must be either nil or a name of preview buffer.
965 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
967 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
968 function. If it is a keymap, keymap of MIME-View mode will be added
969 to it. If it is a function, it will be bound as default binding of
970 keymap of MIME-View mode."
971 (mime-maybe-hide-echo-buffer)
972 (let ((win-conf (current-window-configuration))
973 ;; (raw-buffer (mime-entity-buffer message))
977 (concat "*Preview-" (mime-entity-name message) "*")))
978 ;; (set-buffer raw-buffer)
979 ;; (setq mime-preview-buffer preview-buffer)
980 (let ((inhibit-read-only t))
981 (set-buffer (get-buffer-create preview-buffer))
984 ;; (setq mime-raw-buffer raw-buffer)
986 (setq mime-mother-buffer mother)
988 (setq mime-preview-original-window-configuration win-conf)
989 (setq major-mode 'mime-view-mode)
990 (setq mode-name "MIME-View")
991 (mime-display-entity message nil
992 `((entity-button . invisible)
994 (major-mode . ,original-major-mode))
996 (mime-view-define-keymap default-keymap-or-function)
998 (next-single-property-change (point-min) 'mime-view-entity)))
1001 (goto-char (point-min))
1002 (search-forward "\n\n" nil t)
1004 (run-hooks 'mime-view-mode-hook)
1005 (set-buffer-modified-p nil)
1006 (setq buffer-read-only t)
1010 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1011 default-keymap-or-function
1012 representation-type)
1013 "View RAW-BUFFER in MIME-View mode.
1014 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1016 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1017 function. If it is a keymap, keymap of MIME-View mode will be added
1018 to it. If it is a function, it will be bound as default binding of
1019 keymap of MIME-View mode.
1020 Optional argument REPRESENTATION-TYPE is representation-type of
1021 message. It must be nil, `binary' or `cooked'. If it is nil,
1022 `cooked' is used as default."
1025 (setq raw-buffer (current-buffer)))
1026 (or representation-type
1027 (setq representation-type
1029 (set-buffer raw-buffer)
1030 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1031 (assq t mime-raw-representation-type-alist)))
1033 (if (eq representation-type 'binary)
1034 (setq representation-type 'buffer)
1036 (setq preview-buffer (mime-display-message
1037 (mime-open-entity representation-type raw-buffer)
1038 preview-buffer mother default-keymap-or-function))
1039 (or (get-buffer-window preview-buffer)
1040 (let ((r-win (get-buffer-window raw-buffer)))
1042 (set-window-buffer r-win preview-buffer)
1043 (let ((m-win (and mother (get-buffer-window mother))))
1045 (set-window-buffer m-win preview-buffer)
1046 (switch-to-buffer preview-buffer)
1049 (defun mime-view-mode (&optional mother ctl encoding
1050 raw-buffer preview-buffer
1051 default-keymap-or-function)
1052 "Major mode for viewing MIME message.
1054 Here is a list of the standard keys for mime-view-mode.
1059 u Move to upper content
1060 p or M-TAB Move to previous content
1061 n or TAB Move to next content
1062 SPC Scroll up or move to next content
1063 M-SPC or DEL Scroll down or move to previous content
1064 RET Move to next line
1065 M-RET Move to previous line
1066 v Decode current content as `play mode'
1067 e Decode current content as `extract mode'
1068 C-c C-p Decode current content as `print mode'
1069 a Followup to current content.
1071 button-2 Move to point under the mouse cursor
1072 and decode current content as `play mode'
1075 (unless mime-view-redisplay
1077 (if raw-buffer (set-buffer raw-buffer))
1080 (or (assq major-mode mime-raw-representation-type-alist)
1081 (assq t mime-raw-representation-type-alist)))))
1082 (if (eq type 'binary)
1085 (setq mime-message-structure (mime-open-entity type raw-buffer))
1086 (or (mime-entity-content-type mime-message-structure)
1087 (mime-entity-set-content-type-internal
1088 mime-message-structure ctl))
1090 (or (mime-entity-encoding mime-message-structure)
1091 (mime-entity-set-encoding-internal mime-message-structure encoding))
1093 (mime-display-message mime-message-structure preview-buffer
1094 mother default-keymap-or-function)
1101 (autoload 'mime-preview-play-current-entity "mime-play"
1102 "Play current entity." t)
1104 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1105 "Extract current entity into file (maybe).
1106 It decodes current entity to call internal or external method as
1107 \"extract\" mode. The method is selected from variable
1108 `mime-acting-condition'."
1110 (mime-preview-play-current-entity ignore-examples "extract")
1113 (defun mime-preview-print-current-entity (&optional ignore-examples)
1114 "Print current entity (maybe).
1115 It decodes current entity to call internal or external method as
1116 \"print\" mode. The method is selected from variable
1117 `mime-acting-condition'."
1119 (mime-preview-play-current-entity ignore-examples "print")
1126 (defun mime-preview-follow-current-entity ()
1127 "Write follow message to current entity.
1128 It calls following-method selected from variable
1129 `mime-preview-following-method-alist'."
1132 (while (null (setq entity
1133 (get-text-property (point) 'mime-view-entity)))
1137 (previous-single-property-change (point) 'mime-view-entity))
1140 (entity-node-id (mime-entity-node-id entity))
1141 (len (length entity-node-id))
1145 (if (eq (next-single-property-change (point-min)
1151 ((eq (next-single-property-change p-beg 'mime-view-entity)
1153 (setq p-beg (point))
1155 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1157 (setq p-end (point-max))
1159 ((null entity-node-id)
1160 (setq p-end (point-max))
1168 (next-single-property-change
1169 (point) 'mime-view-entity))
1171 (let ((rc (mime-entity-node-id
1172 (get-text-property (point)
1173 'mime-view-entity))))
1174 (or (equal entity-node-id
1175 (nthcdr (- (length rc) len) rc))
1180 (setq p-end (point-max))
1184 (previous-single-property-change p-end 'mime-view-entity-header))
1185 (if (or (null ph-end)
1189 (let* ((mode (mime-preview-original-major-mode 'recursive))
1191 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1193 (the-buf (current-buffer))
1194 (a-buf (mime-entity-buffer entity))
1197 (set-buffer (setq new-buf (get-buffer-create new-name)))
1199 (insert-buffer-substring the-buf ph-end p-end)
1200 (when (= ph-end p-beg)
1201 (goto-char (point-min))
1203 (goto-char (point-min))
1204 (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1211 (mime-find-entity-from-node-id entity-node-id))
1214 (mime-entity-point-min ci)
1215 (mime-entity-point-max ci)
1217 (std11-header-string-except
1219 (apply (function regexp-or) fields)
1221 (if (and (eq (mime-entity-media-type ci) 'message)
1222 (eq (mime-entity-media-subtype ci) 'rfc822))
1228 (setq fields (std11-collect-field-names)
1229 entity-node-id (cdr entity-node-id))
1232 (let ((rest mime-view-following-required-fields-list))
1234 (let ((field-name (car rest)))
1235 (or (std11-field-body field-name)
1241 (set-buffer the-buf)
1242 (set-buffer mime-mother-buffer)
1243 (set-buffer mime-raw-buffer)
1244 (std11-field-body field-name)
1248 (setq rest (cdr rest))
1250 (mime-decode-header-in-buffer)
1252 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1257 "Sorry, following method for %s is not implemented yet."
1266 (defun mime-preview-move-to-upper ()
1267 "Move to upper entity.
1268 If there is no upper entity, call function `mime-preview-quit'."
1271 (while (null (setq cinfo
1272 (get-text-property (point) 'mime-view-entity)))
1275 (let ((r (mime-entity-parent cinfo))
1278 (while (setq point (previous-single-property-change
1279 (point) 'mime-view-entity))
1281 (if (eq r (get-text-property (point) 'mime-view-entity))
1288 (defun mime-preview-move-to-previous ()
1289 "Move to previous entity.
1290 If there is no previous entity, it calls function registered in
1291 variable `mime-preview-over-to-previous-method-alist'."
1293 (while (null (get-text-property (point) 'mime-view-entity))
1296 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1298 (if (get-text-property (1- point) 'mime-view-entity)
1300 (goto-char (1- point))
1301 (mime-preview-move-to-previous)
1303 (let ((f (assq (mime-preview-original-major-mode)
1304 mime-preview-over-to-previous-method-alist)))
1310 (defun mime-preview-move-to-next ()
1311 "Move to next entity.
1312 If there is no previous entity, it calls function registered in
1313 variable `mime-preview-over-to-next-method-alist'."
1315 (while (and (not (eobp))
1316 (null (get-text-property (point) 'mime-view-entity)))
1319 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1323 (if (null (get-text-property point 'mime-view-entity))
1324 (mime-preview-move-to-next)
1326 (let ((f (assq (mime-preview-original-major-mode)
1327 mime-preview-over-to-next-method-alist)))
1333 (defun mime-preview-scroll-up-entity (&optional h)
1334 "Scroll up current entity.
1335 If reached to (point-max), it calls function registered in variable
1336 `mime-preview-over-to-next-method-alist'."
1339 (setq h (1- (window-height)))
1341 (if (= (point) (point-max))
1342 (let ((f (assq (mime-preview-original-major-mode)
1343 mime-preview-over-to-next-method-alist)))
1348 (or (next-single-property-change (point) 'mime-view-entity)
1351 (if (> (point) point)
1356 (defun mime-preview-scroll-down-entity (&optional h)
1357 "Scroll down current entity.
1358 If reached to (point-min), it calls function registered in variable
1359 `mime-preview-over-to-previous-method-alist'."
1362 (setq h (1- (window-height)))
1364 (if (= (point) (point-min))
1365 (let ((f (assq (mime-preview-original-major-mode)
1366 mime-preview-over-to-previous-method-alist)))
1371 (or (previous-single-property-change (point) 'mime-view-entity)
1373 (forward-line (- h))
1374 (if (< (point) point)
1378 (defun mime-preview-next-line-entity ()
1380 (mime-preview-scroll-up-entity 1)
1383 (defun mime-preview-previous-line-entity ()
1385 (mime-preview-scroll-down-entity 1)
1392 (defun mime-preview-quit ()
1393 "Quit from MIME-preview buffer.
1394 It calls function registered in variable
1395 `mime-preview-quitting-method-alist'."
1397 (let ((r (assq (mime-preview-original-major-mode)
1398 mime-preview-quitting-method-alist)))
1403 (defun mime-preview-kill-buffer ()
1405 (kill-buffer (current-buffer))
1412 (provide 'mime-view)
1414 (run-hooks 'mime-view-load-hook)
1416 ;;; mime-view.el ends here