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."
65 (defcustom mime-preview-move-scroll nil
66 "*Decides whether to scroll when moving to next entity.
67 When t, scroll the buffer. Non-nil but not t means scroll when
68 the next entity is within next-screen-context-lines from top or
69 buttom. Nil means don't scroll at all."
71 :type '(choice (const :tag "Off" nil)
73 (sexp :tag "Situation" 1)))
75 ;;; @ in raw-buffer (representation space)
78 (defvar mime-preview-buffer nil
79 "MIME-preview buffer corresponding with the (raw) buffer.")
80 (make-variable-buffer-local 'mime-preview-buffer)
83 (defvar mime-raw-representation-type-alist
84 '((mime-show-message-mode . binary)
85 (mime-temp-message-mode . binary)
88 "Alist of major-mode vs. representation-type of mime-raw-buffer.
89 Each element looks like (SYMBOL . REPRESENTATION-TYPE). SYMBOL is
90 major-mode or t. t means default. REPRESENTATION-TYPE must be
91 `binary' or `cooked'.")
94 ;; (defun mime-raw-find-entity-from-point (point &optional message-info)
95 ;; "Return entity from POINT in mime-raw-buffer.
96 ;; If optional argument MESSAGE-INFO is not specified,
97 ;; `mime-message-structure' is used."
99 ;; (setq message-info mime-message-structure))
100 ;; (if (and (<= (mime-entity-point-min message-info) point)
101 ;; (<= point (mime-entity-point-max message-info)))
102 ;; (let ((children (mime-entity-children message-info)))
106 ;; (mime-raw-find-entity-from-point point (car children))))
110 ;; (setq children (cdr children)))
112 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
115 ;;; @ in preview-buffer (presentation space)
118 (defvar mime-mother-buffer nil
119 "Mother buffer corresponding with the (MIME-preview) buffer.
120 If current MIME-preview buffer is generated by other buffer, such as
121 message/partial, it is called `mother-buffer'.")
122 (make-variable-buffer-local 'mime-mother-buffer)
124 ;; (defvar mime-raw-buffer nil
125 ;; "Raw buffer corresponding with the (MIME-preview) buffer.")
126 ;; (make-variable-buffer-local 'mime-raw-buffer)
128 (defvar mime-preview-original-window-configuration nil
129 "Window-configuration before mime-view-mode is called.")
130 (make-variable-buffer-local 'mime-preview-original-window-configuration)
132 (defun mime-preview-original-major-mode (&optional recursive point)
133 "Return major-mode of original buffer.
134 If optional argument RECURSIVE is non-nil and current buffer has
135 mime-mother-buffer, it returns original major-mode of the
137 (if (and recursive mime-mother-buffer)
139 (set-buffer mime-mother-buffer)
140 (mime-preview-original-major-mode recursive)
142 (cdr (assq 'major-mode
143 (get-text-property (or point
144 (if (> (point) (buffer-size))
145 (max (1- (point-max)) (point-min))
147 'mime-view-situation)))))
150 ;;; @ entity information
153 (defun mime-entity-situation (entity &optional situation)
154 "Return situation of ENTITY."
155 (let (rest param name)
157 (unless (assq 'type situation)
158 (setq rest (or (mime-entity-content-type entity)
159 (make-mime-content-type 'text 'plain))
160 situation (cons (car rest) situation)
163 (unless (assq 'subtype situation)
165 (setq rest (or (cdr (mime-entity-content-type entity))
166 '((subtype . plain)))))
167 (setq situation (cons (car rest) situation)
171 (setq param (car rest))
172 (or (assoc (car param) situation)
173 (setq situation (cons param situation)))
174 (setq rest (cdr rest)))
176 ;; Content-Disposition
178 (unless (assq 'disposition-type situation)
179 (setq rest (mime-entity-content-disposition entity))
181 (setq situation (cons (cons 'disposition-type
182 (mime-content-disposition-type rest))
184 rest (mime-content-disposition-parameters rest))
187 (setq param (car rest)
189 (if (cond ((string= name "filename")
190 (if (assq 'filename situation)
192 (setq name 'filename)))
193 ((string= name "creation-date")
194 (if (assq 'creation-date situation)
196 (setq name 'creation-date)))
197 ((string= name "modification-date")
198 (if (assq 'modification-date situation)
200 (setq name 'modification-date)))
201 ((string= name "read-date")
202 (if (assq 'read-date situation)
204 (setq name 'read-date)))
205 ((string= name "size")
206 (if (assq 'size situation)
209 (t (setq name (cons 'disposition name))
210 (if (assoc name situation)
214 (cons (cons name (cdr param))
216 (setq rest (cdr rest)))
218 ;; Content-Transfer-Encoding
219 (or (assq 'encoding situation)
221 (cons (cons 'encoding (or (mime-entity-encoding entity)
227 (defun mime-view-entity-title (entity)
228 (or (mime-entity-read-field entity 'Content-Description)
229 (mime-entity-read-field entity 'Subject)
230 (mime-entity-filename entity)
234 ;; (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
235 ;; "Return entity-node-id from POINT in mime-raw-buffer.
236 ;; If optional argument MESSAGE-INFO is not specified,
237 ;; `mime-message-structure' is used."
238 ;; (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
240 ;; (make-obsolete 'mime-raw-point-to-entity-node-id "don't use it.")
242 ;; (defsubst mime-raw-point-to-entity-number (point &optional message-info)
243 ;; "Return entity-number from POINT in mime-raw-buffer.
244 ;; If optional argument MESSAGE-INFO is not specified,
245 ;; `mime-message-structure' is used."
246 ;; (mime-entity-number (mime-raw-find-entity-from-point point message-info)))
248 ;; (make-obsolete 'mime-raw-point-to-entity-number "don't use it.")
250 ;; (defun mime-raw-flatten-message-info (&optional message-info)
251 ;; "Return list of entity in mime-raw-buffer.
252 ;; If optional argument MESSAGE-INFO is not specified,
253 ;; `mime-message-structure' is used."
255 ;; (setq message-info mime-message-structure))
256 ;; (let ((dest (list message-info))
257 ;; (rcl (mime-entity-children message-info)))
259 ;; (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
260 ;; (setq rcl (cdr rcl)))
264 ;;; @ presentation of preview
270 ;;; @@@ predicate function
273 (defun mime-view-entity-button-visible-p (entity)
274 "Return non-nil if header of ENTITY is visible.
275 Please redefine this function if you want to change default setting."
276 (let ((media-type (mime-entity-media-type entity))
277 (media-subtype (mime-entity-media-subtype entity)))
278 (or (not (eq media-type 'application))
279 (and (not (eq media-subtype 'x-selection))
280 (or (not (eq media-subtype 'octet-stream))
281 (let ((mother-entity (mime-entity-parent entity)))
282 (or (not (eq (mime-entity-media-type mother-entity)
284 (not (eq (mime-entity-media-subtype mother-entity)
289 ;;; @@@ entity button generator
292 (defun mime-view-insert-entity-button (entity)
293 "Insert entity-button of ENTITY."
294 (let ((entity-node-id (mime-entity-node-id entity))
295 (params (mime-entity-parameters entity))
296 (subject (mime-view-entity-title entity)))
298 (let ((access-type (assoc "access-type" params))
299 (num (or (cdr (assoc "x-part-number" params))
300 (if (consp entity-node-id)
303 (format "%s" (1+ num))
305 (reverse entity-node-id) ".")
309 (let ((server (assoc "server" params)))
310 (setq access-type (cdr access-type))
312 (format "%s %s ([%s] %s)"
313 num subject access-type (cdr server))
314 (let ((site (cdr (assoc "site" params)))
315 (dir (cdr (assoc "directory" params)))
316 (url (cdr (assoc "url" params)))
319 (format "%s %s ([%s] %s)"
320 num subject access-type url)
321 (format "%s %s ([%s] %s:%s)"
322 num subject access-type site dir))
326 (let ((media-type (mime-entity-media-type entity))
327 (media-subtype (mime-entity-media-subtype entity))
328 (charset (cdr (assoc "charset" params)))
329 (encoding (mime-entity-encoding entity)))
333 (format " <%s/%s%s%s>"
334 media-type media-subtype
336 (concat "; " charset)
339 (concat " (" encoding ")")
341 (if (>= (+ (current-column)(length rest))(window-width))
345 (function mime-preview-play-current-entity))
352 (defvar mime-header-presentation-method-alist nil
353 "Alist of major mode vs. corresponding header-presentation-method functions.
354 Each element looks like (SYMBOL . FUNCTION).
355 SYMBOL must be major mode in raw-buffer or t. t means default.
356 Interface of FUNCTION must be (ENTITY SITUATION).")
358 (defvar mime-view-ignored-field-list
359 '(".*Received:" ".*Path:" ".*Id:" "^References:"
360 "^Replied:" "^Errors-To:"
361 "^Lines:" "^Sender:" ".*Host:" "^Xref:"
362 "^Content-Type:" "^Precedence:"
363 "^Status:" "^X-VM-.*:")
364 "All fields that match this list will be hidden in MIME preview buffer.
365 Each elements are regexp of field-name.")
367 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
368 "All fields that match this list will be displayed in MIME preview buffer.
369 Each elements are regexp of field-name.")
375 ;;; @@@ predicate function
378 (in-calist-package 'mime-view)
380 (defun mime-calist::field-match-method-as-default-rule (calist
381 field-type field-value)
382 (let ((s-field (assq field-type calist)))
383 (cond ((null s-field)
384 (cons (cons field-type field-value) calist)
388 (define-calist-field-match-method
389 'header #'mime-calist::field-match-method-as-default-rule)
391 (define-calist-field-match-method
392 'body #'mime-calist::field-match-method-as-default-rule)
395 (defvar mime-preview-condition nil
396 "Condition-tree about how to display entity.")
398 (ctree-set-calist-strictly
399 'mime-preview-condition '((type . application)(subtype . octet-stream)
402 (ctree-set-calist-strictly
403 'mime-preview-condition '((type . application)(subtype . octet-stream)
406 (ctree-set-calist-strictly
407 'mime-preview-condition '((type . application)(subtype . octet-stream)
411 (ctree-set-calist-strictly
412 'mime-preview-condition '((type . application)(subtype . pgp)
415 (ctree-set-calist-strictly
416 'mime-preview-condition '((type . application)(subtype . x-latex)
419 (ctree-set-calist-strictly
420 'mime-preview-condition '((type . application)(subtype . x-selection)
423 (ctree-set-calist-strictly
424 'mime-preview-condition '((type . application)(subtype . x-comment)
427 (ctree-set-calist-strictly
428 'mime-preview-condition '((type . message)(subtype . delivery-status)
431 (ctree-set-calist-strictly
432 'mime-preview-condition
434 (body-presentation-method . mime-display-text/plain)))
436 (ctree-set-calist-strictly
437 'mime-preview-condition
440 (body-presentation-method . mime-display-text/plain)))
442 (ctree-set-calist-strictly
443 'mime-preview-condition
444 '((type . text)(subtype . enriched)
446 (body-presentation-method . mime-display-text/enriched)))
448 (ctree-set-calist-strictly
449 'mime-preview-condition
450 '((type . text)(subtype . richtext)
452 (body-presentation-method . mime-display-text/richtext)))
454 (ctree-set-calist-strictly
455 'mime-preview-condition
456 '((type . application)(subtype . x-postpet)
458 (body-presentation-method . mime-display-application/x-postpet)))
460 (ctree-set-calist-strictly
461 'mime-preview-condition
462 '((type . text)(subtype . t)
464 (body-presentation-method . mime-display-text/plain)))
466 (ctree-set-calist-strictly
467 'mime-preview-condition
468 '((type . multipart)(subtype . alternative)
470 (body-presentation-method . mime-display-multipart/alternative)))
472 (ctree-set-calist-strictly
473 'mime-preview-condition '((type . message)(subtype . partial)
474 (body-presentation-method
475 . mime-display-message/partial-button)))
477 (ctree-set-calist-strictly
478 'mime-preview-condition '((type . message)(subtype . rfc822)
479 (body-presentation-method . nil)
480 (childrens-situation (header . visible)
481 (entity-button . invisible))))
483 (ctree-set-calist-strictly
484 'mime-preview-condition '((type . message)(subtype . news)
485 (body-presentation-method . nil)
486 (childrens-situation (header . visible)
487 (entity-button . invisible))))
490 ;;; @@@ entity presentation
493 (defun mime-display-text/plain (entity situation)
495 (narrow-to-region (point-max)(point-max))
496 (mime-insert-text-content entity)
497 (run-hooks 'mime-text-decode-hook)
498 (goto-char (point-max))
499 (if (not (eq (char-after (1- (point))) ?\n))
502 (mime-add-url-buttons)
503 (run-hooks 'mime-display-text/plain-hook)
506 (defun mime-display-text/richtext (entity situation)
508 (narrow-to-region (point-max)(point-max))
509 (mime-insert-text-content entity)
510 (run-hooks 'mime-text-decode-hook)
511 (let ((beg (point-min)))
512 (remove-text-properties beg (point-max) '(face nil))
513 (richtext-decode beg (point-max))
516 (defun mime-display-text/enriched (entity situation)
518 (narrow-to-region (point-max)(point-max))
519 (mime-insert-text-content entity)
520 (run-hooks 'mime-text-decode-hook)
521 (let ((beg (point-min)))
522 (remove-text-properties beg (point-max) '(face nil))
523 (enriched-decode beg (point-max))
526 (put 'unpack 'lisp-indent-function 1)
527 (defmacro unpack (string &rest body)
528 `(let* ((*unpack*string* (string-as-unibyte ,string))
532 (defun unpack-skip (len)
533 (setq *unpack*index* (+ len *unpack*index*)))
535 (defun unpack-fixed (len)
537 (substring *unpack*string* *unpack*index* (+ *unpack*index* len))
540 (defun unpack-byte ()
541 (char-int (aref (unpack-fixed 1) 0)))
543 (defun unpack-short ()
544 (let* ((b0 (unpack-byte))
548 (defun unpack-long ()
549 (let* ((s0 (unpack-short))
551 (+ (* 65536 s0) s1)))
553 (defun unpack-string ()
554 (let ((len (unpack-byte)))
557 (defun unpack-string-sjis ()
558 (decode-mime-charset-string (unpack-string) 'shift_jis))
560 (defun postpet-decode (string)
565 (set-alist 'res 'carryingcount (unpack-long))
567 (set-alist 'res 'sentyear (unpack-short))
568 (set-alist 'res 'sentmonth (unpack-short))
569 (set-alist 'res 'sentday (unpack-short))
571 (set-alist 'res 'petname (unpack-string-sjis))
572 (set-alist 'res 'owner (unpack-string-sjis))
573 (set-alist 'res 'pettype (unpack-fixed 4))
574 (set-alist 'res 'health (unpack-short))
576 (set-alist 'res 'sex (unpack-long))
578 (set-alist 'res 'brain (unpack-byte))
580 (set-alist 'res 'happiness (unpack-byte))
582 (set-alist 'res 'petbirthyear (unpack-short))
583 (set-alist 'res 'petbirthmonth (unpack-short))
584 (set-alist 'res 'petbirthday (unpack-short))
586 (set-alist 'res 'from (unpack-string))
593 (set-alist 'res 'treasure (unpack-short))
594 (set-alist 'res 'money (unpack-long))
598 (defun mime-display-application/x-postpet (entity situation)
600 (narrow-to-region (point-max)(point-max))
601 (let ((pet (postpet-decode (mime-entity-content entity))))
603 (insert "Petname: " (cdr (assq 'petname pet)) "\n"
604 "Owner: " (cdr (assq 'owner pet)) "\n"
605 "Pettype: " (cdr (assq 'pettype pet)) "\n"
606 "From: " (cdr (assq 'from pet)) "\n"
607 "CarryingCount: " (int-to-string (cdr (assq 'carryingcount pet))) "\n"
608 "SentYear: " (int-to-string (cdr (assq 'sentyear pet))) "\n"
609 "SentMonth: " (int-to-string (cdr (assq 'sentmonth pet))) "\n"
610 "SentDay: " (int-to-string (cdr (assq 'sentday pet))) "\n"
611 "PetbirthYear: " (int-to-string (cdr (assq 'petbirthyear pet))) "\n"
612 "PetbirthMonth: " (int-to-string (cdr (assq 'petbirthmonth pet))) "\n"
613 "PetbirthDay: " (int-to-string (cdr (assq 'petbirthday pet))) "\n"
614 "Health: " (int-to-string (cdr (assq 'health pet))) "\n"
615 "Sex: " (int-to-string (cdr (assq 'sex pet))) "\n"
616 "Brain: " (int-to-string (cdr (assq 'brain pet))) "\n"
617 "Happiness: " (int-to-string (cdr (assq 'happiness pet))) "\n"
618 "Treasure: " (int-to-string (cdr (assq 'treasure pet))) "\n"
619 "Money: " (int-to-string (cdr (assq 'money pet))) "\n"
621 (insert "Invalid format\n"))
622 (run-hooks 'mime-display-application/x-postpet-hook))))
625 (defvar mime-view-announcement-for-message/partial
626 (if (and (>= emacs-major-version 19) window-system)
628 \[[ This is message/partial style split message. ]]
629 \[[ Please press `v' key in this buffer ]]
630 \[[ or click here by mouse button-2. ]]"
632 \[[ This is message/partial style split message. ]]
633 \[[ Please press `v' key in this buffer. ]]"
636 (defun mime-display-message/partial-button (&optional entity situation)
638 (goto-char (point-max))
639 (if (not (search-backward "\n\n" nil t))
642 (goto-char (point-max))
643 (narrow-to-region (point-max)(point-max))
644 (insert mime-view-announcement-for-message/partial)
645 (mime-add-button (point-min)(point-max)
646 #'mime-preview-play-current-entity)
649 (defun mime-display-multipart/mixed (entity situation)
650 (let ((children (mime-entity-children entity))
651 (original-major-mode-cell (assq 'major-mode situation))
653 (cdr (assq 'childrens-situation situation))))
654 (if original-major-mode-cell
655 (setq default-situation
656 (cons original-major-mode-cell default-situation)))
658 (mime-display-entity (car children) nil default-situation)
659 (setq children (cdr children))
662 (defcustom mime-view-type-subtype-score-alist
663 '(((text . enriched) . 3)
664 ((text . richtext) . 2)
667 "Alist MEDIA-TYPE vs corresponding score.
668 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t. t means default."
670 :type '(repeat (cons (choice :tag "Media-Type"
671 (cons :tag "Type/Subtype"
672 (symbol :tag "Primary-type")
673 (symbol :tag "Subtype"))
675 (const :tag "Default" t))
678 (defun mime-display-multipart/alternative (entity situation)
679 (let* ((children (mime-entity-children entity))
680 (original-major-mode-cell (assq 'major-mode situation))
682 (cdr (assq 'childrens-situation situation)))
687 (if original-major-mode-cell
688 (setq default-situation
689 (cons original-major-mode-cell default-situation)))
694 (or (ctree-match-calist
695 mime-preview-condition
696 (append (mime-entity-situation child)
699 (if (cdr (assq 'body-presentation-method situation))
704 (cdr (assq 'type situation))
705 (cdr (assq 'subtype situation)))
706 mime-view-type-subtype-score-alist)
708 (cdr (assq 'type situation))
709 mime-view-type-subtype-score-alist)
712 mime-view-type-subtype-score-alist)
714 (if (> score max-score)
724 (let ((child (car children))
725 (situation (car situations)))
726 (mime-display-entity child (if (= i p)
728 (del-alist 'body-presentation-method
729 (copy-alist situation))))
731 (setq children (cdr children)
732 situations (cdr situations)
737 ;;; @ acting-condition
740 (defvar mime-acting-condition nil
741 "Condition-tree about how to process entity.")
743 (if (file-readable-p mailcap-file)
744 (let ((entries (mailcap-parse-file)))
746 (let ((entry (car entries))
749 (let* ((field (car entry))
750 (field-type (car field)))
751 (cond ((eq field-type 'view) (setq view field))
752 ((eq field-type 'print) (setq print field))
753 ((memq field-type '(compose composetyped edit)))
754 (t (setq shared (cons field shared))))
756 (setq entry (cdr entry))
758 (setq shared (nreverse shared))
759 (ctree-set-calist-with-default
760 'mime-acting-condition
761 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
763 (ctree-set-calist-with-default
764 'mime-acting-condition
766 (list '(mode . "print")(cons 'method (cdr view))))
769 (setq entries (cdr entries))
772 (ctree-set-calist-strictly
773 'mime-acting-condition
774 '((type . application)(subtype . octet-stream)
776 (method . mime-detect-content)
779 (ctree-set-calist-with-default
780 'mime-acting-condition
782 (method . mime-save-content)))
784 (ctree-set-calist-strictly
785 'mime-acting-condition
786 '((type . text)(subtype . x-rot13-47)(mode . "play")
787 (method . mime-view-caesar)
789 (ctree-set-calist-strictly
790 'mime-acting-condition
791 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
792 (method . mime-view-caesar)
795 (ctree-set-calist-strictly
796 'mime-acting-condition
797 '((type . message)(subtype . rfc822)(mode . "play")
798 (method . mime-view-message/rfc822)
800 (ctree-set-calist-strictly
801 'mime-acting-condition
802 '((type . message)(subtype . partial)(mode . "play")
803 (method . mime-store-message/partial-piece)
806 (ctree-set-calist-strictly
807 'mime-acting-condition
808 '((type . message)(subtype . external-body)
809 ("access-type" . "anon-ftp")
810 (method . mime-view-message/external-anon-ftp)
813 (ctree-set-calist-strictly
814 'mime-acting-condition
815 '((type . message)(subtype . external-body)
816 ("access-type" . "url")
817 (method . mime-view-message/external-url)
820 (ctree-set-calist-strictly
821 'mime-acting-condition
822 '((type . application)(subtype . octet-stream)
823 (method . mime-save-content)
827 ;;; @ quitting method
830 (defvar mime-preview-quitting-method-alist
831 '((mime-show-message-mode
832 . mime-preview-quitting-method-for-mime-show-message-mode))
833 "Alist of major-mode vs. quitting-method of mime-view.")
835 (defvar mime-preview-over-to-previous-method-alist nil
836 "Alist of major-mode vs. over-to-previous-method of mime-view.")
838 (defvar mime-preview-over-to-next-method-alist nil
839 "Alist of major-mode vs. over-to-next-method of mime-view.")
842 ;;; @ following method
845 (defvar mime-preview-following-method-alist nil
846 "Alist of major-mode vs. following-method of mime-view.")
848 (defvar mime-view-following-required-fields-list
855 (defun mime-display-entity (entity &optional situation
856 default-situation preview-buffer)
858 (setq preview-buffer (current-buffer)))
859 (let* (e nb ne nhb nbb)
860 (mime-goto-header-start-point entity)
861 (in-calist-package 'mime-view)
864 (or (ctree-match-calist mime-preview-condition
865 (append (mime-entity-situation entity)
868 (let ((button-is-invisible
869 (eq (cdr (assq 'entity-button situation)) 'invisible))
871 (eq (cdr (assq 'header situation)) 'visible))
872 (header-presentation-method
873 (or (cdr (assq 'header-presentation-method situation))
874 (cdr (assq (cdr (assq 'major-mode situation))
875 mime-header-presentation-method-alist))))
876 (body-presentation-method
877 (cdr (assq 'body-presentation-method situation)))
878 (children (mime-entity-children entity)))
879 (set-buffer preview-buffer)
881 (narrow-to-region nb nb)
882 (or button-is-invisible
883 (if (mime-view-entity-button-visible-p entity)
884 (mime-view-insert-entity-button entity)
886 (when header-is-visible
888 (if header-presentation-method
889 (funcall header-presentation-method entity situation)
890 (mime-insert-header entity
891 mime-view-ignored-field-list
892 mime-view-visible-field-list))
893 (run-hooks 'mime-display-header-hook)
894 (put-text-property nhb (point-max) 'mime-view-entity-header entity)
895 (goto-char (point-max))
900 ((functionp body-presentation-method)
901 (funcall body-presentation-method entity situation)
904 (when button-is-invisible
905 (goto-char (point-max))
906 (mime-view-insert-entity-button entity)
908 (or header-is-visible
910 (goto-char (point-max))
914 (setq ne (point-max))
916 (put-text-property nb ne 'mime-view-entity entity)
917 (put-text-property nb ne 'mime-view-situation situation)
918 (put-text-property nbb ne 'mime-view-entity-body entity)
921 (if (functionp body-presentation-method)
922 (funcall body-presentation-method entity situation)
923 (mime-display-multipart/mixed entity situation)
928 ;;; @ MIME viewer mode
931 (defconst mime-view-menu-title "MIME-View")
932 (defconst mime-view-menu-list
933 '((up "Move to upper entity" mime-preview-move-to-upper)
934 (previous "Move to previous entity" mime-preview-move-to-previous)
935 (next "Move to next entity" mime-preview-move-to-next)
936 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
937 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
938 (play "Play current entity" mime-preview-play-current-entity)
939 (extract "Extract current entity" mime-preview-extract-current-entity)
940 (print "Print current entity" mime-preview-print-current-entity)
942 "Menu for MIME Viewer")
944 (cond ((featurep 'xemacs)
945 (defvar mime-view-xemacs-popup-menu
946 (cons mime-view-menu-title
949 (vector (nth 1 item)(nth 2 item) t)
951 mime-view-menu-list)))
952 (defun mime-view-xemacs-popup-menu (event)
953 "Popup the menu in the MIME Viewer buffer"
955 (select-window (event-window event))
956 (set-buffer (event-buffer event))
957 (popup-menu 'mime-view-xemacs-popup-menu))
958 (defvar mouse-button-2 'button2)
961 (defvar mime-view-popup-menu
962 (let ((menu (make-sparse-keymap mime-view-menu-title)))
966 (list (intern (nth 1 item)) 'menu-item
967 (nth 1 item)(nth 2 item))
969 mime-view-menu-list))))
970 (defun mime-view-popup-menu (event)
971 "Popup the menu in the MIME Viewer buffer"
973 (let ((menu mime-view-popup-menu) events func)
974 (setq events (x-popup-menu t menu))
976 (setq func (lookup-key menu (apply #'vector events)))
979 (defvar mouse-button-2 [mouse-2])
982 (defun mime-view-define-keymap (&optional default)
983 (let ((mime-view-mode-map (if (keymapp default)
984 (copy-keymap default)
987 (define-key mime-view-mode-map
988 "u" (function mime-preview-move-to-upper))
989 (define-key mime-view-mode-map
990 "p" (function mime-preview-move-to-previous))
991 (define-key mime-view-mode-map
992 "n" (function mime-preview-move-to-next))
993 (define-key mime-view-mode-map
994 "\e\t" (function mime-preview-move-to-previous))
995 (define-key mime-view-mode-map
996 "\t" (function mime-preview-move-to-next))
997 (define-key mime-view-mode-map
998 " " (function mime-preview-scroll-up-entity))
999 (define-key mime-view-mode-map
1000 "\M- " (function mime-preview-scroll-down-entity))
1001 (define-key mime-view-mode-map
1002 "\177" (function mime-preview-scroll-down-entity))
1003 (define-key mime-view-mode-map
1004 "\C-m" (function mime-preview-next-line-entity))
1005 (define-key mime-view-mode-map
1006 "\C-\M-m" (function mime-preview-previous-line-entity))
1007 (define-key mime-view-mode-map
1008 "v" (function mime-preview-play-current-entity))
1009 (define-key mime-view-mode-map
1010 "e" (function mime-preview-extract-current-entity))
1011 (define-key mime-view-mode-map
1012 "\C-c\C-p" (function mime-preview-print-current-entity))
1013 (define-key mime-view-mode-map
1014 "a" (function mime-preview-follow-current-entity))
1015 (define-key mime-view-mode-map
1016 "q" (function mime-preview-quit))
1017 (define-key mime-view-mode-map
1018 "\C-c\C-x" (function mime-preview-kill-buffer))
1019 ;; (define-key mime-view-mode-map
1020 ;; "<" (function beginning-of-buffer))
1021 ;; (define-key mime-view-mode-map
1022 ;; ">" (function end-of-buffer))
1023 (define-key mime-view-mode-map
1024 "?" (function describe-mode))
1025 (define-key mime-view-mode-map
1026 [tab] (function mime-preview-move-to-next))
1027 (define-key mime-view-mode-map
1028 [delete] (function mime-preview-scroll-down-entity))
1029 (define-key mime-view-mode-map
1030 [backspace] (function mime-preview-scroll-down-entity))
1031 (if (functionp default)
1032 (cond ((featurep 'xemacs)
1033 (set-keymap-default-binding mime-view-mode-map default)
1036 (setq mime-view-mode-map
1037 (append mime-view-mode-map (list (cons t default))))
1040 (define-key mime-view-mode-map
1041 mouse-button-2 (function mime-button-dispatcher))
1043 (cond ((featurep 'xemacs)
1044 (define-key mime-view-mode-map
1045 mouse-button-3 (function mime-view-xemacs-popup-menu))
1047 ((>= emacs-major-version 19)
1048 (define-key mime-view-mode-map
1049 mouse-button-3 (function mime-view-popup-menu))
1050 (define-key mime-view-mode-map [menu-bar mime-view]
1051 (cons mime-view-menu-title
1052 (make-sparse-keymap mime-view-menu-title)))
1055 (define-key mime-view-mode-map
1056 (vector 'menu-bar 'mime-view (car item))
1057 (cons (nth 1 item)(nth 2 item))
1060 (reverse mime-view-menu-list)
1063 (use-local-map mime-view-mode-map)
1064 (run-hooks 'mime-view-define-keymap-hook)
1067 (defsubst mime-maybe-hide-echo-buffer ()
1068 "Clear mime-echo buffer and delete window for it."
1069 (let ((buf (get-buffer mime-echo-buffer-name)))
1074 (let ((win (get-buffer-window buf)))
1081 (defvar mime-view-redisplay nil)
1084 (defun mime-display-message (message &optional preview-buffer
1085 mother default-keymap-or-function
1086 original-major-mode)
1087 "View MESSAGE in MIME-View mode.
1089 Optional argument PREVIEW-BUFFER specifies the buffer of the
1090 presentation. It must be either nil or a name of preview buffer.
1092 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1094 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1095 function. If it is a keymap, keymap of MIME-View mode will be added
1096 to it. If it is a function, it will be bound as default binding of
1097 keymap of MIME-View mode."
1098 (mime-maybe-hide-echo-buffer)
1099 (let ((win-conf (current-window-configuration)))
1101 (setq preview-buffer
1102 (concat "*Preview-" (mime-entity-name message) "*")))
1103 (or original-major-mode
1104 (setq original-major-mode
1105 (with-current-buffer (mime-entity-header-buffer message)
1107 (let ((inhibit-read-only t))
1108 (set-buffer (get-buffer-create preview-buffer))
1112 (setq mime-mother-buffer mother)
1114 (setq mime-preview-original-window-configuration win-conf)
1115 (setq major-mode 'mime-view-mode)
1116 (setq mode-name "MIME-View")
1117 (mime-display-entity message nil
1118 `((entity-button . invisible)
1120 (major-mode . ,original-major-mode))
1122 (mime-view-define-keymap default-keymap-or-function)
1124 (next-single-property-change (point-min) 'mime-view-entity)))
1127 (goto-char (point-min))
1128 (search-forward "\n\n" nil t)
1130 (run-hooks 'mime-view-mode-hook)
1131 (set-buffer-modified-p nil)
1132 (setq buffer-read-only t)
1136 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1137 default-keymap-or-function
1138 representation-type)
1139 "View RAW-BUFFER in MIME-View mode.
1140 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1142 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1143 function. If it is a keymap, keymap of MIME-View mode will be added
1144 to it. If it is a function, it will be bound as default binding of
1145 keymap of MIME-View mode.
1146 Optional argument REPRESENTATION-TYPE is representation-type of
1147 message. It must be nil, `binary' or `cooked'. If it is nil,
1148 `cooked' is used as default."
1151 (setq raw-buffer (current-buffer)))
1152 (or representation-type
1153 (setq representation-type
1155 (set-buffer raw-buffer)
1156 (cdr (or (assq major-mode mime-raw-representation-type-alist)
1157 (assq t mime-raw-representation-type-alist)))
1159 (if (eq representation-type 'binary)
1160 (setq representation-type 'buffer)
1162 (setq preview-buffer (mime-display-message
1163 (mime-open-entity representation-type raw-buffer)
1164 preview-buffer mother default-keymap-or-function))
1165 (or (get-buffer-window preview-buffer)
1166 (let ((r-win (get-buffer-window raw-buffer)))
1168 (set-window-buffer r-win preview-buffer)
1169 (let ((m-win (and mother (get-buffer-window mother))))
1171 (set-window-buffer m-win preview-buffer)
1172 (switch-to-buffer preview-buffer)
1175 (defun mime-view-mode (&optional mother ctl encoding
1176 raw-buffer preview-buffer
1177 default-keymap-or-function)
1178 "Major mode for viewing MIME message.
1180 Here is a list of the standard keys for mime-view-mode.
1185 u Move to upper content
1186 p or M-TAB Move to previous content
1187 n or TAB Move to next content
1188 SPC Scroll up or move to next content
1189 M-SPC or DEL Scroll down or move to previous content
1190 RET Move to next line
1191 M-RET Move to previous line
1192 v Decode current content as `play mode'
1193 e Decode current content as `extract mode'
1194 C-c C-p Decode current content as `print mode'
1195 a Followup to current content.
1197 button-2 Move to point under the mouse cursor
1198 and decode current content as `play mode'
1201 (unless mime-view-redisplay
1203 (if raw-buffer (set-buffer raw-buffer))
1206 (or (assq major-mode mime-raw-representation-type-alist)
1207 (assq t mime-raw-representation-type-alist)))))
1208 (if (eq type 'binary)
1211 (setq mime-message-structure (mime-open-entity type raw-buffer))
1212 (or (mime-entity-content-type mime-message-structure)
1213 (mime-entity-set-content-type-internal
1214 mime-message-structure ctl))
1216 (or (mime-entity-encoding mime-message-structure)
1217 (mime-entity-set-encoding-internal mime-message-structure encoding))
1219 (mime-display-message mime-message-structure preview-buffer
1220 mother default-keymap-or-function)
1227 (autoload 'mime-preview-play-current-entity "mime-play"
1228 "Play current entity." t)
1230 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1231 "Extract current entity into file (maybe).
1232 It decodes current entity to call internal or external method as
1233 \"extract\" mode. The method is selected from variable
1234 `mime-acting-condition'."
1236 (mime-preview-play-current-entity ignore-examples "extract")
1239 (defun mime-preview-print-current-entity (&optional ignore-examples)
1240 "Print current entity (maybe).
1241 It decodes current entity to call internal or external method as
1242 \"print\" mode. The method is selected from variable
1243 `mime-acting-condition'."
1245 (mime-preview-play-current-entity ignore-examples "print")
1252 (defun mime-preview-follow-current-entity ()
1253 "Write follow message to current entity.
1254 It calls following-method selected from variable
1255 `mime-preview-following-method-alist'."
1258 (while (null (setq entity
1259 (get-text-property (point) 'mime-view-entity)))
1263 (previous-single-property-change (point) 'mime-view-entity))
1266 (entity-node-id (mime-entity-node-id entity))
1267 (len (length entity-node-id))
1271 (if (eq (next-single-property-change (point-min)
1277 ((eq (next-single-property-change p-beg 'mime-view-entity)
1279 (setq p-beg (point))
1281 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1283 (setq p-end (point-max))
1285 ((null entity-node-id)
1286 (setq p-end (point-max))
1294 (next-single-property-change
1295 (point) 'mime-view-entity))
1297 (let ((rc (mime-entity-node-id
1298 (get-text-property (point)
1299 'mime-view-entity))))
1300 (or (equal entity-node-id
1301 (nthcdr (- (length rc) len) rc))
1306 (setq p-end (point-max))
1310 (previous-single-property-change p-end 'mime-view-entity-header))
1311 (if (or (null ph-end)
1315 (let* ((mode (mime-preview-original-major-mode 'recursive))
1317 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1319 (the-buf (current-buffer))
1322 (set-buffer (setq new-buf (get-buffer-create new-name)))
1324 (insert-buffer-substring the-buf ph-end p-end)
1325 (when (= ph-end p-beg)
1326 (goto-char (point-min))
1328 (goto-char (point-min))
1329 (let ((current-entity
1330 (if (and (eq (mime-entity-media-type entity) 'message)
1331 (eq (mime-entity-media-subtype entity) 'rfc822))
1332 (mime-entity-children entity)
1335 (while (and current-entity
1338 (with-current-buffer
1339 (mime-entity-header-buffer current-entity)
1342 (mime-entity-header-start-point
1344 (mime-entity-header-end-point
1346 (std11-header-string-except
1349 (apply (function regexp-or) fields)
1351 (if (and (eq (mime-entity-media-type
1352 current-entity) 'message)
1353 (eq (mime-entity-media-subtype
1354 current-entity) 'rfc822))
1360 (setq fields (std11-collect-field-names)
1361 current-entity (mime-entity-parent current-entity))
1364 (let ((rest mime-view-following-required-fields-list)
1367 (setq field-name (car rest))
1368 (or (std11-field-body field-name)
1371 (set-buffer the-buf)
1372 (let ((entity (when mime-mother-buffer
1373 (set-buffer mime-mother-buffer)
1374 (get-text-property (point)
1375 'mime-view-entity))))
1377 (null (setq ret (mime-entity-fetch-field
1378 entity field-name))))
1379 (setq entity (mime-entity-parent entity)))))
1381 (insert (concat field-name ": " ret "\n"))
1383 (setq rest (cdr rest))
1385 (mime-decode-header-in-buffer)
1387 (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1392 "Sorry, following method for %s is not implemented yet."
1401 (defun mime-preview-move-to-upper ()
1402 "Move to upper entity.
1403 If there is no upper entity, call function `mime-preview-quit'."
1406 (while (null (setq cinfo
1407 (get-text-property (point) 'mime-view-entity)))
1410 (let ((r (mime-entity-parent cinfo))
1413 (while (setq point (previous-single-property-change
1414 (point) 'mime-view-entity))
1416 (when (eq r (get-text-property (point) 'mime-view-entity))
1417 (if (or (eq mime-preview-move-scroll t)
1418 (and mime-preview-move-scroll
1421 (move-to-window-line -1)
1422 (forward-line (* -1 next-screen-context-lines))
1425 (recenter next-screen-context-lines))
1432 (defun mime-preview-move-to-previous ()
1433 "Move to previous entity.
1434 If there is no previous entity, it calls function registered in
1435 variable `mime-preview-over-to-previous-method-alist'."
1437 (while (and (not (bobp))
1438 (null (get-text-property (point) 'mime-view-entity)))
1441 (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1443 (>= point (point-min)))
1444 (if (get-text-property (1- point) 'mime-view-entity)
1445 (progn (goto-char point)
1447 (or (eq mime-preview-move-scroll t)
1448 (and mime-preview-move-scroll
1451 (move-to-window-line 0)
1452 (forward-line next-screen-context-lines)
1455 (recenter (* -1 next-screen-context-lines))))
1456 (goto-char (1- point))
1457 (mime-preview-move-to-previous)
1459 (let ((f (assq (mime-preview-original-major-mode)
1460 mime-preview-over-to-previous-method-alist)))
1466 (defun mime-preview-move-to-next ()
1467 "Move to next entity.
1468 If there is no previous entity, it calls function registered in
1469 variable `mime-preview-over-to-next-method-alist'."
1471 (while (and (not (eobp))
1472 (null (get-text-property (point) 'mime-view-entity)))
1475 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1477 (<= point (point-max)))
1480 (if (null (get-text-property point 'mime-view-entity))
1481 (mime-preview-move-to-next)
1483 (or (eq mime-preview-move-scroll t)
1484 (and mime-preview-move-scroll
1487 (move-to-window-line -1)
1489 (* -1 next-screen-context-lines))
1492 (recenter next-screen-context-lines))
1494 (let ((f (assq (mime-preview-original-major-mode)
1495 mime-preview-over-to-next-method-alist)))
1501 (defun mime-preview-scroll-up-entity (&optional h)
1502 "Scroll up current entity.
1503 If reached to (point-max), it calls function registered in variable
1504 `mime-preview-over-to-next-method-alist'."
1507 (let ((f (assq (mime-preview-original-major-mode)
1508 mime-preview-over-to-next-method-alist)))
1513 (or (next-single-property-change (point) 'mime-view-entity)
1515 (bottom (window-end (selected-window))))
1518 (progn (goto-char point)
1519 (recenter next-screen-context-lines))
1523 (goto-char (point-max)))))
1526 (defun mime-preview-scroll-down-entity (&optional h)
1527 "Scroll down current entity.
1528 If reached to (point-min), it calls function registered in variable
1529 `mime-preview-over-to-previous-method-alist'."
1532 (let ((f (assq (mime-preview-original-major-mode)
1533 mime-preview-over-to-previous-method-alist)))
1538 (or (previous-single-property-change (point) 'mime-view-entity)
1540 (top (window-start (selected-window))))
1543 (progn (goto-char point)
1544 (recenter (* -1 next-screen-context-lines)))
1547 (beginning-of-buffer
1548 (goto-char (point-min)))))
1551 (defun mime-preview-next-line-entity (&optional lines)
1552 "Scroll up one line (or prefix LINES lines).
1553 If LINES is negative, scroll down LINES lines."
1555 (mime-preview-scroll-up-entity (or lines 1))
1558 (defun mime-preview-previous-line-entity (&optional lines)
1559 "Scrroll down one line (or prefix LINES lines).
1560 If LINES is negative, scroll up LINES lines."
1562 (mime-preview-scroll-down-entity (or lines 1))
1568 (defun mime-preview-quit ()
1569 "Quit from MIME-preview buffer.
1570 It calls function registered in variable
1571 `mime-preview-quitting-method-alist'."
1573 (let ((r (assq (mime-preview-original-major-mode)
1574 mime-preview-quitting-method-alist)))
1579 (defun mime-preview-kill-buffer ()
1581 (kill-buffer (current-buffer))
1588 (provide 'mime-view)
1590 (run-hooks 'mime-view-load-hook)
1592 ;;; mime-view.el ends here