1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 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 (Sophisticated Emacs 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.
32 (require 'eword-decode)
42 (defconst mime-view-version-string
43 `,(concat (car mime-module-version) " MIME-View "
44 (mapconcat #'number-to-string (cddr mime-module-version) ".")
45 " (" (cadr mime-module-version) ")"))
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 ;;; @ buffer local variables
72 (defvar mime-raw-message-info
73 "Information about structure of message.
74 Please use reference function `mime-entity-SLOT' to get value of SLOT.
76 Following is a list of slots of the structure:
78 node-id reversed entity-number (list of integers)
79 point-min beginning point of region in raw-buffer
80 point-max end point of region in raw-buffer
81 type media-type (symbol)
82 subtype media-subtype (symbol)
83 type/subtype media-type/subtype (string or nil)
84 parameters parameter of Content-Type field (association list)
85 encoding Content-Transfer-Encoding (string or nil)
86 children entities included in this entity (list of content-infos)
88 If an entity includes other entities in its body, such as multipart or
89 message/rfc822, `mime-entity' structures of them are included in
90 `children', so the `mime-entity' structure become a tree.")
91 (make-variable-buffer-local 'mime-raw-message-info)
93 (defvar mime-preview-buffer nil
94 "MIME-preview buffer corresponding with the (raw) buffer.")
95 (make-variable-buffer-local 'mime-preview-buffer)
98 ;;; @@ in preview-buffer
101 (defvar mime-mother-buffer nil
102 "Mother buffer corresponding with the (MIME-preview) buffer.
103 If current MIME-preview buffer is generated by other buffer, such as
104 message/partial, it is called `mother-buffer'.")
105 (make-variable-buffer-local 'mime-mother-buffer)
107 (defvar mime-raw-buffer nil
108 "Raw buffer corresponding with the (MIME-preview) buffer.")
109 (make-variable-buffer-local 'mime-raw-buffer)
111 (defvar mime-preview-original-major-mode nil
112 "Major-mode of mime-raw-buffer.")
113 (make-variable-buffer-local 'mime-preview-original-major-mode)
115 (defvar mime-preview-original-window-configuration nil
116 "Window-configuration before mime-view-mode is called.")
117 (make-variable-buffer-local 'mime-preview-original-window-configuration)
120 ;;; @ entity information
123 (defsubst mime-raw-find-entity-from-node-id (entity-node-id
124 &optional message-info)
125 "Return entity from ENTITY-NODE-ID in mime-raw-buffer.
126 If optional argument MESSAGE-INFO is not specified,
127 `mime-raw-message-info' is used."
128 (mime-raw-find-entity-from-number (reverse entity-node-id) message-info))
130 (defun mime-raw-find-entity-from-number (entity-number &optional message-info)
131 "Return entity from ENTITY-NUMBER in mime-raw-buffer.
132 If optional argument MESSAGE-INFO is not specified,
133 `mime-raw-message-info' is used."
135 (setq message-info mime-raw-message-info))
136 (if (eq entity-number t)
138 (let ((sn (car entity-number)))
141 (let ((rc (nth sn (mime-entity-children message-info))))
143 (mime-raw-find-entity-from-number (cdr entity-number) rc)
147 (defun mime-raw-find-entity-from-point (point &optional message-info)
148 "Return entity from POINT in mime-raw-buffer.
149 If optional argument MESSAGE-INFO is not specified,
150 `mime-raw-message-info' is used."
152 (setq message-info mime-raw-message-info))
153 (if (and (<= (mime-entity-point-min message-info) point)
154 (<= point (mime-entity-point-max message-info)))
155 (let ((children (mime-entity-children message-info)))
159 (mime-raw-find-entity-from-point point (car children))))
163 (setq children (cdr children)))
166 (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
167 "Return entity-node-id from POINT in mime-raw-buffer.
168 If optional argument MESSAGE-INFO is not specified,
169 `mime-raw-message-info' is used."
170 (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
172 (defsubst mime-raw-point-to-entity-number (point &optional message-info)
173 "Return entity-number from POINT in mime-raw-buffer.
174 If optional argument MESSAGE-INFO is not specified,
175 `mime-raw-message-info' is used."
176 (reverse (mime-raw-point-to-entity-node-id point message-info)))
178 (defsubst mime-raw-entity-parent (entity &optional message-info)
179 "Return mother entity of ENTITY.
180 If optional argument MESSAGE-INFO is not specified,
181 `mime-raw-message-info' is used."
182 (mime-raw-find-entity-from-node-id (cdr (mime-entity-node-id entity))
185 (defun mime-raw-flatten-message-info (&optional message-info)
186 "Return list of entity in mime-raw-buffer.
187 If optional argument MESSAGE-INFO is not specified,
188 `mime-raw-message-info' is used."
190 (setq message-info mime-raw-message-info))
191 (let ((dest (list message-info))
192 (rcl (mime-entity-children message-info)))
194 (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
195 (setq rcl (cdr rcl)))
199 ;;; @ presentation of preview
205 ;;; @@@ predicate function
208 (defun mime-view-entity-button-visible-p (entity message-info)
209 "Return non-nil if header of ENTITY is visible.
210 Please redefine this function if you want to change default setting."
211 (let ((media-type (mime-entity-media-type entity))
212 (media-subtype (mime-entity-media-subtype entity)))
213 (or (not (eq media-type 'application))
214 (and (not (eq media-subtype 'x-selection))
215 (or (not (eq media-subtype 'octet-stream))
217 (mime-raw-entity-parent entity message-info)))
218 (or (not (eq (mime-entity-media-type mother-entity)
220 (not (eq (mime-entity-media-subtype mother-entity)
225 ;;; @@@ entity button generator
228 (defun mime-view-insert-entity-button (entity message-info subj)
229 "Insert entity-button of ENTITY."
230 (let ((entity-node-id (mime-entity-node-id entity))
231 (params (mime-entity-parameters entity)))
233 (let ((access-type (assoc "access-type" params))
234 (num (or (cdr (assoc "x-part-number" params))
235 (if (consp entity-node-id)
238 (format "%s" (1+ num))
240 (reverse entity-node-id) ".")
244 (let ((server (assoc "server" params)))
245 (setq access-type (cdr access-type))
247 (format "%s %s ([%s] %s)"
248 num subj access-type (cdr server))
249 (let ((site (cdr (assoc "site" params)))
250 (dir (cdr (assoc "directory" params)))
252 (format "%s %s ([%s] %s:%s)"
253 num subj access-type site dir)
257 (let ((media-type (mime-entity-media-type entity))
258 (media-subtype (mime-entity-media-subtype entity))
259 (charset (cdr (assoc "charset" params)))
260 (encoding (mime-entity-encoding entity)))
264 (format " <%s/%s%s%s>"
265 media-type media-subtype
267 (concat "; " charset)
270 (concat " (" encoding ")")
272 (if (>= (+ (current-column)(length rest))(window-width))
276 (function mime-preview-play-current-entity))
283 ;;; @@@ predicate function
286 ;; (defvar mime-view-childrens-header-showing-Content-Type-list
287 ;; '("message/rfc822" "message/news"))
289 ;; (defun mime-view-header-visible-p (entity message-info)
290 ;; "Return non-nil if header of ENTITY is visible."
291 ;; (let ((entity-node-id (mime-entity-node-id entity)))
292 ;; (member (mime-entity-type/subtype
293 ;; (mime-raw-find-entity-from-node-id
294 ;; (cdr entity-node-id) message-info))
295 ;; mime-view-childrens-header-showing-Content-Type-list)
298 ;;; @@@ entity header filter
301 (defvar mime-view-content-header-filter-alist nil)
303 (defun mime-view-default-content-header-filter ()
304 (mime-view-cut-header)
305 (eword-decode-header)
308 ;;; @@@ entity field cutter
311 (defvar mime-view-ignored-field-list
312 '(".*Received" ".*Path" ".*Id" "References"
313 "Replied" "Errors-To"
314 "Lines" "Sender" ".*Host" "Xref"
315 "Content-Type" "Precedence"
317 "All fields that match this list will be hidden in MIME preview buffer.
318 Each elements are regexp of field-name.")
320 (defvar mime-view-ignored-field-regexp
322 (apply (function regexp-or) mime-view-ignored-field-list)
325 (defvar mime-view-visible-field-list '("Dnas.*" "Message-Id")
326 "All fields that match this list will be displayed in MIME preview buffer.
327 Each elements are regexp of field-name.")
329 (defun mime-view-cut-header ()
330 (goto-char (point-min))
331 (while (re-search-forward mime-view-ignored-field-regexp nil t)
332 (let* ((beg (match-beginning 0))
334 (name (buffer-substring beg end))
337 (let ((rest mime-view-visible-field-list))
339 (if (string-match (car rest) name)
342 (setq rest (cdr rest))))
345 (if (re-search-forward "^\\([^ \t]\\|$\\)" nil t)
354 ;;; @@@ predicate function
357 (defun mime-calist::field-match-method-as-default-rule (calist
358 field-type field-value)
359 (let ((s-field (assq field-type calist)))
360 (cond ((null s-field)
361 (cons (cons field-type field-value) calist)
365 (define-calist-field-match-method
366 'header #'mime-calist::field-match-method-as-default-rule)
368 (define-calist-field-match-method
369 'body #'mime-calist::field-match-method-as-default-rule)
372 (defvar mime-preview-condition nil
373 "Condition-tree about how to display entity.")
375 (ctree-set-calist-strictly
376 'mime-preview-condition '((type . application)(subtype . octet-stream)
379 (ctree-set-calist-strictly
380 'mime-preview-condition '((type . application)(subtype . octet-stream)
383 (ctree-set-calist-strictly
384 'mime-preview-condition '((type . application)(subtype . octet-stream)
388 (ctree-set-calist-strictly
389 'mime-preview-condition '((type . application)(subtype . pgp)
392 (ctree-set-calist-strictly
393 'mime-preview-condition '((type . application)(subtype . x-latex)
396 (ctree-set-calist-strictly
397 'mime-preview-condition '((type . application)(subtype . x-selection)
400 (ctree-set-calist-strictly
401 'mime-preview-condition '((type . application)(subtype . x-comment)
404 (ctree-set-calist-strictly
405 'mime-preview-condition '((type . message)(subtype . delivery-status)
408 (ctree-set-calist-strictly
409 'mime-preview-condition '((body . visible)
410 (body-presentation-method . with-filter)
411 (body-filter . mime-preview-filter-for-text/plain)))
413 (ctree-set-calist-strictly
414 'mime-preview-condition '((type . nil)
416 (body-presentation-method . with-filter)
417 (body-filter . mime-preview-filter-for-text/plain)))
419 (ctree-set-calist-strictly
420 'mime-preview-condition '((type . text)(subtype . enriched)
422 (body-presentation-method . with-filter)
424 . mime-preview-filter-for-text/enriched)))
426 (ctree-set-calist-strictly
427 'mime-preview-condition '((type . text)(subtype . richtext)
429 (body-presentation-method . with-filter)
431 . mime-preview-filter-for-text/richtext)))
433 (ctree-set-calist-strictly
434 'mime-preview-condition '((type . text)(subtype . t)
436 (body-presentation-method . with-filter)
437 (body-filter . mime-preview-filter-for-text/plain)))
439 (ctree-set-calist-strictly
440 'mime-preview-condition '((type . message)(subtype . partial)
441 (body-presentation-method
442 . mime-view-insert-message/partial-button)))
444 (ctree-set-calist-strictly
445 'mime-preview-condition '((type . message)(subtype . rfc822)
446 (body-presentation-method . nil)
447 (childrens-situation (header . visible)
448 (entity-button . invisible))))
450 (ctree-set-calist-strictly
451 'mime-preview-condition '((type . message)(subtype . news)
452 (body-presentation-method . nil)
453 (childrens-situation (header . visible)
454 (entity-button . invisible))))
457 ;;; @@@ entity filter
460 (autoload 'mime-preview-filter-for-text/plain "mime-text")
461 (autoload 'mime-preview-filter-for-text/enriched "mime-text")
462 (autoload 'mime-preview-filter-for-text/richtext "mime-text")
464 (defvar mime-text-decoder-alist
465 '((mime-show-message-mode . mime-text-decode-buffer)
466 (mime-temp-message-mode . mime-text-decode-buffer)
467 (t . mime-text-decode-buffer-maybe)
469 "Alist of major-mode vs. mime-text-decoder.
470 Each element looks like (SYMBOL . FUNCTION). SYMBOL is major-mode or
473 Specification of FUNCTION is described in DOC-string of variable
476 This value is overridden by buffer local variable `mime-text-decoder'
480 (defvar mime-view-announcement-for-message/partial
481 (if (and (>= emacs-major-version 19) window-system)
483 \[[ This is message/partial style split message. ]]
484 \[[ Please press `v' key in this buffer ]]
485 \[[ or click here by mouse button-2. ]]"
487 \[[ This is message/partial style split message. ]]
488 \[[ Please press `v' key in this buffer. ]]"
491 (defun mime-view-insert-message/partial-button (&optional situation)
493 (goto-char (point-max))
494 (if (not (search-backward "\n\n" nil t))
497 (goto-char (point-max))
498 (narrow-to-region (point-max)(point-max))
499 (insert mime-view-announcement-for-message/partial)
500 (mime-add-button (point-min)(point-max)
501 #'mime-preview-play-current-entity)
505 ;;; @ acting-condition
508 (defvar mime-acting-condition nil
509 "Condition-tree about how to process entity.")
511 (if (file-readable-p mailcap-file)
512 (let ((entries (mailcap-parse-file)))
514 (let ((entry (car entries))
517 (let* ((field (car entry))
518 (field-type (car field)))
519 (cond ((eq field-type 'view) (setq view field))
520 ((eq field-type 'print) (setq print field))
521 ((memq field-type '(compose composetyped edit)))
522 (t (setq shared (cons field shared))))
524 (setq entry (cdr entry))
526 (setq shared (nreverse shared))
527 (ctree-set-calist-strictly
528 'mime-acting-condition
529 (append shared (list '(mode . "play")(cons 'method (cdr view)))))
531 (ctree-set-calist-strictly
532 'mime-acting-condition
534 (list '(mode . "print")(cons 'method (cdr view))))
537 (setq entries (cdr entries))
540 ;; (ctree-set-calist-strictly
541 ;; 'mime-acting-condition
542 ;; '((type . t)(subtype . t)(mode . "extract")
543 ;; (method . mime-method-to-save)))
544 (ctree-set-calist-with-default
545 'mime-acting-condition
547 (method . mime-method-to-save)))
549 ;; (ctree-set-calist-strictly
550 ;; 'mime-acting-condition
551 ;; '((type . text)(subtype . plain)(mode . "play")
552 ;; (method "tm-plain" nil 'file "" 'encoding 'mode 'name)
554 ;; (ctree-set-calist-strictly
555 ;; 'mime-acting-condition
556 ;; '((type . text)(subtype . plain)(mode . "print")
557 ;; (method "tm-plain" nil 'file "" 'encoding 'mode 'name)
559 ;; (ctree-set-calist-strictly
560 ;; 'mime-acting-condition
561 ;; '((type . text)(subtype . html)(mode . "play")
562 ;; (method "tm-html" nil 'file "" 'encoding 'mode 'name)
564 (ctree-set-calist-strictly
565 'mime-acting-condition
566 '((type . text)(subtype . x-rot13-47)(mode . "play")
567 (method . mime-method-to-display-caesar)
569 (ctree-set-calist-strictly
570 'mime-acting-condition
571 '((type . text)(subtype . x-rot13-47-48)(mode . "play")
572 (method . mime-method-to-display-caesar)
575 ;; (ctree-set-calist-strictly
576 ;; 'mime-acting-condition
577 ;; '((type . audio)(subtype . basic)(mode . "play")
578 ;; (method "tm-au" nil 'file "" 'encoding 'mode 'name)
581 ;; (ctree-set-calist-strictly
582 ;; 'mime-acting-condition
583 ;; '((type . image)(mode . "play")
584 ;; (method "tm-image" nil 'file "" 'encoding 'mode 'name)
586 ;; (ctree-set-calist-strictly
587 ;; 'mime-acting-condition
588 ;; '((type . image)(mode . "print")
589 ;; (method "tm-image" nil 'file "" 'encoding 'mode 'name)
592 ;; (ctree-set-calist-strictly
593 ;; 'mime-acting-condition
594 ;; '((type . video)(subtype . mpeg)(mode . "play")
595 ;; (method "tm-mpeg" nil 'file "" 'encoding 'mode 'name)
598 ;; (ctree-set-calist-strictly
599 ;; 'mime-acting-condition
600 ;; '((type . application)(subtype . postscript)(mode . "play")
601 ;; (method "tm-ps" nil 'file "" 'encoding 'mode 'name)
603 ;; (ctree-set-calist-strictly
604 ;; 'mime-acting-condition
605 ;; '((type . application)(subtype . postscript)(mode . "print")
606 ;; (method "tm-ps" nil 'file "" 'encoding 'mode 'name)
609 (ctree-set-calist-strictly
610 'mime-acting-condition
611 '((type . message)(subtype . rfc822)(mode . "play")
612 (method . mime-method-to-display-message/rfc822)
614 (ctree-set-calist-strictly
615 'mime-acting-condition
616 '((type . message)(subtype . partial)(mode . "play")
617 (method . mime-method-to-store-message/partial)
620 (ctree-set-calist-strictly
621 'mime-acting-condition
622 '((type . message)(subtype . external-body)
623 ("access-type" . "anon-ftp")
624 (method . mime-method-to-display-message/external-ftp)
627 (ctree-set-calist-strictly
628 'mime-acting-condition
629 '((type . application)(subtype . octet-stream)
630 (method . mime-method-to-save)
634 ;;; @ quitting method
637 (defvar mime-preview-quitting-method-alist
638 '((mime-show-message-mode
639 . mime-preview-quitting-method-for-mime-show-message-mode))
640 "Alist of major-mode vs. quitting-method of mime-view.")
642 (defvar mime-view-over-to-previous-method-alist nil)
643 (defvar mime-view-over-to-next-method-alist nil)
645 (defvar mime-view-show-summary-method nil
646 "Alist of major-mode vs. show-summary-method.")
649 ;;; @ following method
652 (defvar mime-view-following-method-alist nil
653 "Alist of major-mode vs. following-method of mime-view.")
655 (defvar mime-view-following-required-fields-list
662 ;; hack from Gnus 5.0.4.
664 (defvar mime-view-x-face-to-pbm-command
665 "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm")
667 (defvar mime-view-x-face-command
668 (concat mime-view-x-face-to-pbm-command
670 "String to be executed to display an X-Face field.
671 The command will be executed in a sub-shell asynchronously.
672 The compressed face will be piped to this command.")
674 (defun mime-view-x-face-function ()
675 "Function to display X-Face field. You can redefine to customize."
676 ;; 1995/10/12 (c.f. tm-eng:130)
677 ;; fixed by Eric Ding <ericding@San-Jose.ate.slb.com>
679 (narrow-to-region (point-min) (re-search-forward "^$" nil t))
681 (goto-char (point-min))
682 (if (re-search-forward "^X-Face:[ \t]*" nil t)
683 (let ((beg (match-end 0))
684 (end (std11-field-end))
686 (call-process-region beg end "sh" nil 0 nil
687 "-c" mime-view-x-face-command)
694 (defvar mime-view-uuencode-encoding-name-list '("x-uue" "x-uuencode"))
696 (defvar mime-raw-buffer-coding-system-alist
697 `((t . ,(mime-charset-to-coding-system default-mime-charset)))
698 "Alist of major-mode vs. corresponding coding-system of `mime-raw-buffer'.")
704 (defvar mime-view-redisplay nil)
706 (defun mime-view-setup-buffers (&optional ctl encoding ibuf obuf)
712 (or mime-view-redisplay
713 (setq mime-raw-message-info (mime-parse-message ctl encoding))
715 (let ((message-info mime-raw-message-info)
716 (the-buf (current-buffer))
719 (setq obuf (concat "*Preview-" (buffer-name the-buf) "*")))
720 (set-buffer (get-buffer-create obuf))
721 (let ((inhibit-read-only t))
722 ;;(setq buffer-read-only nil)
725 (setq mime-raw-buffer the-buf)
726 (setq mime-preview-original-major-mode mode)
727 (setq major-mode 'mime-view-mode)
728 (setq mode-name "MIME-View")
729 (mime-view-display-message message-info the-buf obuf)
730 (set-buffer-modified-p nil)
732 (setq buffer-read-only t)
735 (setq mime-preview-buffer obuf)
738 (defun mime-view-display-message (message-info ibuf obuf)
739 (let* ((start (mime-entity-point-min message-info))
740 (end (mime-entity-point-max message-info))
741 (media-type (mime-entity-media-type message-info))
742 (media-subtype (mime-entity-media-subtype message-info))
743 (params (mime-entity-parameters message-info))
744 (encoding (mime-entity-encoding message-info))
745 end-of-header e nb ne subj)
748 (setq end-of-header (if (re-search-forward "^$" nil t)
751 (if (> end-of-header end)
752 (setq end-of-header end)
755 (narrow-to-region start end)
758 (mime-raw-get-subject params encoding)))
762 (narrow-to-region nb nb)
763 ;; Insert message-header
765 (narrow-to-region (point)(point))
766 (insert-buffer-substring mime-raw-buffer start end-of-header)
767 (let ((f (cdr (assq mime-preview-original-major-mode
768 mime-view-content-header-filter-alist))))
771 (mime-view-default-content-header-filter)
773 (run-hooks 'mime-view-content-header-filter-hook)
776 (ctree-match-calist mime-preview-condition
777 (list* (cons 'type media-type)
778 (cons 'subtype media-subtype)
779 (cons 'encoding encoding)
780 (cons 'major-mode major-mode)
783 (cdr (assq 'message-button situation)))
784 (body-presentation-method
785 (cdr (assq 'body-presentation-method situation))))
786 (when (eq message-button 'visible)
787 (goto-char (point-max))
788 (mime-view-insert-entity-button message-info message-info subj)
790 (cond ((eq body-presentation-method 'with-filter)
791 (let ((body-filter (cdr (assq 'body-filter situation))))
793 (narrow-to-region (point-max)(point-max))
794 (insert-buffer-substring mime-raw-buffer end-of-header end)
795 (funcall body-filter situation)
797 ((functionp body-presentation-method)
798 (funcall body-presentation-method situation)
800 ((null (mime-entity-children message-info))
801 (goto-char (point-max))
802 (mime-view-insert-entity-button message-info message-info subj)
804 (setq ne (point-max))
806 (put-text-property nb ne 'mime-view-raw-buffer ibuf)
807 (put-text-property nb ne 'mime-view-entity message-info)
809 (let ((children (mime-entity-children message-info))
811 (cdr (assq 'childrens-situation situation))))
813 (mime-view-display-entity (car children) message-info ibuf obuf
815 (setq children (cdr children))
818 (defun mime-view-display-entity (entity message-info ibuf obuf
820 (let* ((start (mime-entity-point-min entity))
821 (end (mime-entity-point-max entity))
822 (media-type (mime-entity-media-type entity))
823 (media-subtype (mime-entity-media-subtype entity))
824 (params (mime-entity-parameters entity))
825 (encoding (mime-entity-encoding entity))
826 end-of-header e nb ne subj)
829 (setq end-of-header (if (re-search-forward "^$" nil t)
832 (if (> end-of-header end)
833 (setq end-of-header end)
836 (narrow-to-region start end)
839 (mime-raw-get-subject params encoding)))
842 (ctree-match-calist mime-preview-condition
843 (list* (cons 'type media-type)
844 (cons 'subtype media-subtype)
845 (cons 'encoding encoding)
846 (cons 'major-mode major-mode)
848 default-situation))))
850 (eq (cdr (assq 'entity-button situation)) 'invisible))
852 (eq (cdr (assq 'header situation)) 'visible))
853 (body-presentation-method
854 (cdr (assq 'body-presentation-method situation))))
857 (narrow-to-region nb nb)
858 (or button-is-invisible
859 (if (mime-view-entity-button-visible-p entity message-info)
860 (mime-view-insert-entity-button entity message-info subj)
862 (if header-is-visible
864 (narrow-to-region (point)(point))
865 (insert-buffer-substring mime-raw-buffer start end-of-header)
866 (let ((f (cdr (assq mime-preview-original-major-mode
867 mime-view-content-header-filter-alist))))
870 (mime-view-default-content-header-filter)
872 (run-hooks 'mime-view-content-header-filter-hook)
874 (cond ((eq body-presentation-method 'with-filter)
875 (let ((body-filter (cdr (assq 'body-filter situation))))
877 (narrow-to-region (point-max)(point-max))
878 (insert-buffer-substring mime-raw-buffer end-of-header end)
879 (funcall body-filter situation)
881 ((functionp body-presentation-method)
882 (funcall body-presentation-method situation)
884 (or header-is-visible
885 body-presentation-method
887 (goto-char (point-max))
890 (setq ne (point-max))
892 (put-text-property nb ne 'mime-view-raw-buffer ibuf)
893 (put-text-property nb ne 'mime-view-entity entity)
895 (let ((children (mime-entity-children entity))
897 (cdr (assq 'childrens-situation situation))))
899 (mime-view-display-entity (car children) message-info ibuf obuf
901 (setq children (cdr children))
904 (defun mime-raw-get-uu-filename (param &optional encoding)
905 (if (member (or encoding
906 (cdr (assq 'encoding param))
908 mime-view-uuencode-encoding-name-list)
910 (or (if (re-search-forward "^begin [0-9]+ " nil t)
911 (if (looking-at ".+$")
912 (buffer-substring (match-beginning 0)(match-end 0))
917 (defun mime-raw-get-subject (param &optional encoding)
918 (or (std11-find-field-body '("Content-Description" "Subject"))
920 (if (or (and (setq ret (mime/Content-Disposition))
921 (setq ret (assoc "filename" (cdr ret)))
923 (setq ret (assoc "name" param))
924 (setq ret (assoc "x-name" param))
926 (std11-strip-quoted-string (cdr ret))
928 (mime-raw-get-uu-filename param encoding)
932 ;;; @ MIME viewer mode
935 (defconst mime-view-menu-title "MIME-View")
936 (defconst mime-view-menu-list
937 '((up "Move to upper entity" mime-preview-move-to-upper)
938 (previous "Move to previous entity" mime-preview-move-to-previous)
939 (next "Move to next entity" mime-preview-move-to-next)
940 (scroll-down "Scroll-down" mime-preview-scroll-down-entity)
941 (scroll-up "Scroll-up" mime-preview-scroll-up-entity)
942 (play "Play current entity" mime-preview-play-current-entity)
943 (extract "Extract current entity" mime-preview-extract-current-entity)
944 (print "Print current entity" mime-preview-print-current-entity)
945 (x-face "Show X Face" mime-preview-display-x-face)
947 "Menu for MIME Viewer")
949 (cond (running-xemacs
950 (defvar mime-view-xemacs-popup-menu
951 (cons mime-view-menu-title
954 (vector (nth 1 item)(nth 2 item) t)
956 mime-view-menu-list)))
957 (defun mime-view-xemacs-popup-menu (event)
958 "Popup the menu in the MIME Viewer buffer"
960 (select-window (event-window event))
961 (set-buffer (event-buffer event))
962 (popup-menu 'mime-view-xemacs-popup-menu))
963 (defvar mouse-button-2 'button2)
966 (defvar mouse-button-2 [mouse-2])
969 (defun mime-view-define-keymap (&optional default)
970 (let ((mime-view-mode-map (if (keymapp default)
971 (copy-keymap default)
974 (define-key mime-view-mode-map
975 "u" (function mime-preview-move-to-upper))
976 (define-key mime-view-mode-map
977 "p" (function mime-preview-move-to-previous))
978 (define-key mime-view-mode-map
979 "n" (function mime-preview-move-to-next))
980 (define-key mime-view-mode-map
981 "\e\t" (function mime-preview-move-to-previous))
982 (define-key mime-view-mode-map
983 "\t" (function mime-preview-move-to-next))
984 (define-key mime-view-mode-map
985 " " (function mime-preview-scroll-up-entity))
986 (define-key mime-view-mode-map
987 "\M- " (function mime-preview-scroll-down-entity))
988 (define-key mime-view-mode-map
989 "\177" (function mime-preview-scroll-down-entity))
990 (define-key mime-view-mode-map
991 "\C-m" (function mime-preview-next-line-entity))
992 (define-key mime-view-mode-map
993 "\C-\M-m" (function mime-preview-previous-line-entity))
994 (define-key mime-view-mode-map
995 "v" (function mime-preview-play-current-entity))
996 (define-key mime-view-mode-map
997 "e" (function mime-preview-extract-current-entity))
998 (define-key mime-view-mode-map
999 "\C-c\C-p" (function mime-preview-print-current-entity))
1000 (define-key mime-view-mode-map
1001 "a" (function mime-preview-follow-current-entity))
1002 (define-key mime-view-mode-map
1003 "q" (function mime-preview-quit))
1004 (define-key mime-view-mode-map
1005 "h" (function mime-preview-show-summary))
1006 (define-key mime-view-mode-map
1007 "\C-c\C-x" (function mime-preview-kill-buffer))
1008 ;; (define-key mime-view-mode-map
1009 ;; "<" (function beginning-of-buffer))
1010 ;; (define-key mime-view-mode-map
1011 ;; ">" (function end-of-buffer))
1012 (define-key mime-view-mode-map
1013 "?" (function describe-mode))
1014 (define-key mime-view-mode-map
1015 [tab] (function mime-preview-move-to-next))
1016 (define-key mime-view-mode-map
1017 [delete] (function mime-preview-scroll-down-entity))
1018 (define-key mime-view-mode-map
1019 [backspace] (function mime-preview-scroll-down-entity))
1020 (if (functionp default)
1021 (cond (running-xemacs
1022 (set-keymap-default-binding mime-view-mode-map default)
1025 (setq mime-view-mode-map
1026 (append mime-view-mode-map (list (cons t default))))
1029 (define-key mime-view-mode-map
1030 mouse-button-2 (function mime-button-dispatcher))
1032 (cond (running-xemacs
1033 (define-key mime-view-mode-map
1034 mouse-button-3 (function mime-view-xemacs-popup-menu))
1036 ((>= emacs-major-version 19)
1037 (define-key mime-view-mode-map [menu-bar mime-view]
1038 (cons mime-view-menu-title
1039 (make-sparse-keymap mime-view-menu-title)))
1042 (define-key mime-view-mode-map
1043 (vector 'menu-bar 'mime-view (car item))
1044 (cons (nth 1 item)(nth 2 item))
1047 (reverse mime-view-menu-list)
1050 (use-local-map mime-view-mode-map)
1051 (run-hooks 'mime-view-define-keymap-hook)
1054 (defsubst mime-maybe-hide-echo-buffer ()
1055 "Clear mime-echo buffer and delete window for it."
1056 (let ((buf (get-buffer mime-echo-buffer-name)))
1061 (let ((win (get-buffer-window buf)))
1068 (defun mime-view-mode (&optional mother ctl encoding ibuf obuf
1069 default-keymap-or-function)
1070 "Major mode for viewing MIME message.
1072 Here is a list of the standard keys for mime-view-mode.
1077 u Move to upper content
1078 p or M-TAB Move to previous content
1079 n or TAB Move to next content
1080 SPC Scroll up or move to next content
1081 M-SPC or DEL Scroll down or move to previous content
1082 RET Move to next line
1083 M-RET Move to previous line
1084 v Decode current content as `play mode'
1085 e Decode current content as `extract mode'
1086 C-c C-p Decode current content as `print mode'
1087 a Followup to current content.
1090 button-2 Move to point under the mouse cursor
1091 and decode current content as `play mode'
1094 (mime-maybe-hide-echo-buffer)
1095 (let ((ret (mime-view-setup-buffers ctl encoding ibuf obuf))
1096 (win-conf (current-window-configuration))
1099 (switch-to-buffer ret)
1100 (setq mime-preview-original-window-configuration win-conf)
1103 (setq mime-mother-buffer mother)
1105 (mime-view-define-keymap default-keymap-or-function)
1107 (next-single-property-change (point-min) 'mime-view-entity)))
1110 (goto-char (point-min))
1111 (search-forward "\n\n" nil t)
1113 (run-hooks 'mime-view-mode-hook)
1120 (autoload 'mime-preview-play-current-entity "mime-play"
1121 "Play current entity." t)
1123 (defun mime-preview-extract-current-entity ()
1124 "Extract current entity into file (maybe).
1125 It decodes current entity to call internal or external method as
1126 \"extract\" mode. The method is selected from variable
1127 `mime-acting-condition'."
1129 (mime-preview-play-current-entity "extract")
1132 (defun mime-preview-print-current-entity ()
1133 "Print current entity (maybe).
1134 It decodes current entity to call internal or external method as
1135 \"print\" mode. The method is selected from variable
1136 `mime-acting-condition'."
1138 (mime-preview-play-current-entity "print")
1145 (defun mime-preview-original-major-mode ()
1146 "Return major-mode of original buffer.
1147 If a current buffer has mime-mother-buffer, return original major-mode
1148 of the mother-buffer."
1149 (if mime-mother-buffer
1151 (set-buffer mime-mother-buffer)
1152 (mime-preview-original-major-mode)
1154 mime-preview-original-major-mode))
1156 (defun mime-preview-follow-current-entity ()
1157 "Write follow message to current entity.
1158 It calls following-method selected from variable
1159 `mime-view-following-method-alist'."
1162 (while (null (setq entity
1163 (get-text-property (point) 'mime-view-entity)))
1167 (previous-single-property-change (point) 'mime-view-entity))
1169 (entity-node-id (mime-entity-node-id entity))
1170 (len (length entity-node-id))
1174 (if (eq (next-single-property-change (point-min)
1180 ((eq (next-single-property-change p-beg 'mime-view-entity)
1182 (setq p-beg (point))
1184 (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1186 (setq p-end (point-max))
1188 ((null entity-node-id)
1189 (setq p-end (point-max))
1197 (next-single-property-change
1198 (point) 'mime-view-entity))
1200 (let ((rc (mime-entity-node-id
1201 (get-text-property (point)
1202 'mime-view-entity))))
1203 (or (equal entity-node-id
1204 (nthcdr (- (length rc) len) rc))
1209 (setq p-end (point-max))
1212 (let* ((mode (mime-preview-original-major-mode))
1214 (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1216 (the-buf (current-buffer))
1217 (a-buf mime-raw-buffer)
1220 (set-buffer (setq new-buf (get-buffer-create new-name)))
1222 (insert-buffer-substring the-buf p-beg p-end)
1223 (goto-char (point-min))
1224 (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1232 (mime-raw-find-entity-from-node-id entity-node-id))
1235 (mime-entity-point-min ci)
1236 (mime-entity-point-max ci)
1238 (std11-header-string-except
1240 (apply (function regexp-or) fields)
1243 (eq (mime-entity-media-type ci) 'message)
1244 (eq (mime-entity-media-subtype ci) 'rfc822))
1250 (setq fields (std11-collect-field-names)
1251 entity-node-id (cdr entity-node-id))
1254 (let ((rest mime-view-following-required-fields-list))
1256 (let ((field-name (car rest)))
1257 (or (std11-field-body field-name)
1263 (set-buffer the-buf)
1264 (set-buffer mime-mother-buffer)
1265 (set-buffer mime-raw-buffer)
1266 (std11-field-body field-name)
1270 (setq rest (cdr rest))
1272 (eword-decode-header)
1274 (let ((f (cdr (assq mode mime-view-following-method-alist))))
1279 "Sorry, following method for %s is not implemented yet."
1288 (defun mime-preview-display-x-face ()
1290 (save-window-excursion
1291 (set-buffer mime-raw-buffer)
1292 (mime-view-x-face-function)
1299 (defun mime-preview-move-to-upper ()
1300 "Move to upper entity.
1301 If there is no upper entity, call function `mime-preview-quit'."
1304 (while (null (setq cinfo
1305 (get-text-property (point) 'mime-view-entity)))
1308 (let ((r (mime-raw-find-entity-from-node-id
1309 (cdr (mime-entity-node-id cinfo))
1310 (get-text-property 1 'mime-view-entity)))
1313 (while (setq point (previous-single-property-change
1314 (point) 'mime-view-entity))
1316 (if (eq r (get-text-property (point) 'mime-view-entity))
1323 (defun mime-preview-move-to-previous ()
1324 "Move to previous entity.
1325 If there is no previous entity, it calls function registered in
1326 variable `mime-view-over-to-previous-method-alist'."
1328 (while (null (get-text-property (point) 'mime-view-entity))
1332 (previous-single-property-change (point) 'mime-view-entity)))
1335 (let ((f (assq mime-preview-original-major-mode
1336 mime-view-over-to-previous-method-alist)))
1342 (defun mime-preview-move-to-next ()
1343 "Move to next entity.
1344 If there is no previous entity, it calls function registered in
1345 variable `mime-view-over-to-next-method-alist'."
1347 (let ((point (next-single-property-change (point) 'mime-view-entity)))
1350 (let ((f (assq mime-preview-original-major-mode
1351 mime-view-over-to-next-method-alist)))
1357 (defun mime-preview-scroll-up-entity (&optional h)
1358 "Scroll up current entity.
1359 If reached to (point-max), it calls function registered in variable
1360 `mime-view-over-to-next-method-alist'."
1363 (setq h (1- (window-height)))
1365 (if (= (point) (point-max))
1366 (let ((f (assq mime-preview-original-major-mode
1367 mime-view-over-to-next-method-alist)))
1372 (or (next-single-property-change (point) 'mime-view-entity)
1375 (if (> (point) point)
1380 (defun mime-preview-scroll-down-entity (&optional h)
1381 "Scroll down current entity.
1382 If reached to (point-min), it calls function registered in variable
1383 `mime-view-over-to-previous-method-alist'."
1386 (setq h (1- (window-height)))
1388 (if (= (point) (point-min))
1389 (let ((f (assq mime-preview-original-major-mode
1390 mime-view-over-to-previous-method-alist)))
1397 (while (> (point) 1)
1399 (previous-single-property-change (point)
1405 (setq point (point-min))
1407 (forward-line (- h))
1408 (if (< (point) point)
1412 (defun mime-preview-next-line-entity ()
1414 (mime-preview-scroll-up-entity 1)
1417 (defun mime-preview-previous-line-entity ()
1419 (mime-preview-scroll-down-entity 1)
1426 (defun mime-preview-quit ()
1427 "Quit from MIME-preview buffer.
1428 It calls function registered in variable
1429 `mime-preview-quitting-method-alist'."
1431 (let ((r (assq mime-preview-original-major-mode
1432 mime-preview-quitting-method-alist)))
1437 (defun mime-preview-show-summary ()
1439 It calls function registered in variable
1440 `mime-view-show-summary-method'."
1442 (let ((r (assq mime-preview-original-major-mode
1443 mime-view-show-summary-method)))
1448 (defun mime-preview-kill-buffer ()
1450 (kill-buffer (current-buffer))
1457 (provide 'mime-view)
1459 (run-hooks 'mime-view-load-hook)
1461 ;;; mime-view.el ends here