(mime-preview-situation-example-list): New variable.
[elisp/semi.git] / mime-view.el
1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Created: 1994/07/13
7 ;;      Renamed: 1994/08/31 from tm-body.el
8 ;;      Renamed: 1997/02/19 from tm-view.el
9 ;; Keywords: MIME, multimedia, mail, news
10
11 ;; This file is part of SEMI (Sample of Elastic MIME Interfaces).
12
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.
17
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.
22
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.
27
28 ;;; Code:
29
30 (require 'emu)
31 (require 'mime)
32 (require 'semi-def)
33 (require 'calist)
34 (require 'alist)
35 (require 'mailcap)
36
37
38 ;;; @ version
39 ;;;
40
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) ")"))
46
47
48 ;;; @ variables
49 ;;;
50
51 (defgroup mime-view nil
52   "MIME view mode"
53   :group 'mime)
54
55 (defcustom mime-view-find-every-acting-situation t
56   "*Find every available acting-situation if non-nil."
57   :group 'mime-view
58   :type 'boolean)
59
60 (defcustom mime-acting-situation-examples-file "~/.mime-example"
61   "*File name of example about acting-situation demonstrated by user."
62   :group 'mime-view
63   :type 'file)
64
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."
70   :group 'mime-view
71   :type '(choice (const :tag "Off" nil)
72                  (const :tag "On" t)
73                  (sexp :tag "Situation" 1)))
74
75 ;;; @ in raw-buffer (representation space)
76 ;;;
77
78 (defvar mime-preview-buffer nil
79   "MIME-preview buffer corresponding with the (raw) buffer.")
80 (make-variable-buffer-local 'mime-preview-buffer)
81
82
83 (defvar mime-raw-representation-type-alist
84   '((mime-show-message-mode     . binary)
85     (mime-temp-message-mode     . binary)
86     (t                          . cooked)
87     )
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'.")
92
93
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."
98 ;;   (or message-info
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)))
103 ;;         (catch 'tag
104 ;;           (while children
105 ;;             (let ((ret
106 ;;                    (mime-raw-find-entity-from-point point (car children))))
107 ;;               (if ret
108 ;;                   (throw 'tag ret)
109 ;;                 ))
110 ;;             (setq children (cdr children)))
111 ;;           message-info))))
112 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
113
114
115 ;;; @ in preview-buffer (presentation space)
116 ;;;
117
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)
123
124 ;; (defvar mime-raw-buffer nil
125 ;;   "Raw buffer corresponding with the (MIME-preview) buffer.")
126 ;; (make-variable-buffer-local 'mime-raw-buffer)
127
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)
131
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
136 mother-buffer."
137   (if (and recursive mime-mother-buffer)
138       (save-excursion
139         (set-buffer mime-mother-buffer)
140         (mime-preview-original-major-mode recursive)
141         )
142     (cdr (assq 'major-mode
143                (get-text-property (or point
144                                       (if (> (point) (buffer-size))
145                                           (max (1- (point-max)) (point-min))
146                                         (point)))
147                                   'mime-view-situation)))))
148
149
150 ;;; @ entity information
151 ;;;
152
153 (defun mime-entity-situation (entity &optional situation)
154   "Return situation of ENTITY."
155   (let (rest param name)
156     ;; Content-Type
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)
161             rest (cdr rest))
162       )
163     (unless (assq 'subtype situation)
164       (or rest
165           (setq rest (or (cdr (mime-entity-content-type entity))
166                          '((subtype . plain)))))
167       (setq situation (cons (car rest) situation)
168             rest (cdr rest))
169       )
170     (while rest
171       (setq param (car rest))
172       (or (assoc (car param) situation)
173           (setq situation (cons param situation)))
174       (setq rest (cdr rest)))
175     
176     ;; Content-Disposition
177     (setq rest nil)
178     (unless (assq 'disposition-type situation)
179       (setq rest (mime-entity-content-disposition entity))
180       (if rest
181           (setq situation (cons (cons 'disposition-type
182                                       (mime-content-disposition-type rest))
183                                 situation)
184                 rest (mime-content-disposition-parameters rest))
185         ))
186     (while rest
187       (setq param (car rest)
188             name (car param))
189       (if (cond ((string= name "filename")
190                  (if (assq 'filename situation)
191                      nil
192                    (setq name 'filename)))
193                 ((string= name "creation-date")
194                  (if (assq 'creation-date situation)
195                      nil
196                    (setq name 'creation-date)))
197                 ((string= name "modification-date")
198                  (if (assq 'modification-date situation)
199                      nil
200                    (setq name 'modification-date)))
201                 ((string= name "read-date")
202                  (if (assq 'read-date situation)
203                      nil
204                    (setq name 'read-date)))
205                 ((string= name "size")
206                  (if (assq 'size situation)
207                      nil
208                    (setq name 'size)))
209                 (t (setq name (cons 'disposition name))
210                    (if (assoc name situation)
211                        nil
212                      name)))
213           (setq situation
214                 (cons (cons name (cdr param))
215                       situation)))
216       (setq rest (cdr rest)))
217     
218     ;; Content-Transfer-Encoding
219     (or (assq 'encoding situation)
220         (setq situation
221               (cons (cons 'encoding (or (mime-entity-encoding entity)
222                                         "7bit"))
223                     situation)))
224     
225     situation))
226
227 (defsubst mime-delq-null-situation (situations field
228                                                &optional ignored-value)
229   (let (dest)
230     (while situations
231       (let* ((situation (car situations))
232              (cell (assq field situation)))
233         (if cell
234             (or (eq (cdr cell) ignored-value)
235                 (setq dest (cons situation dest))
236                 )))
237       (setq situations (cdr situations)))
238     dest))
239
240 (defun mime-compare-situation-with-example (situation example)
241   (let ((example (copy-alist example))
242         (match 0))
243     (while situation
244       (let* ((cell (car situation))
245              (key (car cell))
246              (ecell (assoc key example)))
247         (when ecell
248           (if (equal cell ecell)
249               (setq match (1+ match))
250             (setq example (delq ecell example))
251             ))
252         )
253       (setq situation (cdr situation))
254       )
255     (cons match example)
256     ))
257
258 (defun mime-sort-situation (situation)
259   (sort situation
260         #'(lambda (a b)
261             (let ((a-t (car a))
262                   (b-t (car b))
263                   (order '((type . 1)
264                            (subtype . 2)
265                            (mode . 3)
266                            (method . 4)
267                            (major-mode . 5)
268                            (disposition-type . 6)
269                            ))
270                   a-order b-order)
271               (if (symbolp a-t)
272                   (let ((ret (assq a-t order)))
273                     (if ret
274                         (setq a-order (cdr ret))
275                       (setq a-order 7)
276                       ))
277                 (setq a-order 8)
278                 )
279               (if (symbolp b-t)
280                   (let ((ret (assq b-t order)))
281                     (if ret
282                         (setq b-order (cdr ret))
283                       (setq b-order 7)
284                       ))
285                 (setq b-order 8)
286                 )
287               (if (= a-order b-order)
288                   (string< (format "%s" a-t)(format "%s" b-t))
289                 (< a-order b-order))
290               )))
291   )
292
293 (defun mime-unify-situations (entity-situation condition situation-examples
294                                                &optional ignored-method)
295   (let (ret)
296     (in-calist-package 'mime-view)
297     (setq ret
298           (mime-delq-null-situation
299            (ctree-find-calist condition entity-situation
300                               mime-view-find-every-acting-situation)
301            'method ignored-method))
302     (or (assq 'ignore-examples entity-situation)
303         (if (cdr ret)
304             (let ((rest ret)
305                   (max-score 0)
306                   (max-escore 0)
307                   max-examples
308                   max-situations)
309               (while rest
310                 (let ((situation (car rest))
311                       (examples situation-examples))
312                   (while examples
313                     (let* ((ret
314                             (mime-compare-situation-with-example
315                              situation (caar examples)))
316                            (ret-score (car ret)))
317                       (cond ((> ret-score max-score)
318                              (setq max-score ret-score
319                                    max-escore (cdar examples)
320                                    max-examples (list (cdr ret))
321                                    max-situations (list situation))
322                              )
323                             ((= ret-score max-score)
324                              (cond ((> (cdar examples) max-escore)
325                                     (setq max-escore (cdar examples)
326                                           max-examples (list (cdr ret))
327                                           max-situations (list situation))
328                                     )
329                                    ((= (cdar examples) max-escore)
330                                     (setq max-examples
331                                           (cons (cdr ret) max-examples))
332                                     (or (member situation max-situations)
333                                         (setq max-situations
334                                               (cons situation max-situations)))
335                                     )))))
336                     (setq examples (cdr examples))))
337                 (setq rest (cdr rest)))
338               (when max-situations
339                 (setq ret max-situations)
340                 (while max-examples
341                   (let* ((example (car max-examples))
342                          (cell
343                           (assoc example situation-examples)))
344                     (if cell
345                         (setcdr cell (1+ (cdr cell)))
346                       (setq situation-examples
347                             (cons (cons example 0)
348                                   situation-examples))
349                       ))
350                   (setq max-examples (cdr max-examples))
351                   )))))
352     (cons ret situation-examples)
353     ;; ret: list of situations
354     ;; situation-examples: new examples (notoce that contents of
355     ;;                     argument `situation-examples' has bees modified)
356     ))
357
358 (defun mime-view-entity-title (entity)
359   (or (mime-entity-read-field entity 'Content-Description)
360       (mime-entity-read-field entity 'Subject)
361       (mime-entity-filename entity)
362       ""))
363
364 (defvar mime-preview-situation-example-list nil)
365 (defvar mime-acting-situation-example-list nil)
366
367 (defvar mime-acting-situation-example-list-max-size 16)
368
369 (defun mime-save-acting-situation-examples ()
370   (let* ((file mime-acting-situation-examples-file)
371          (buffer (get-buffer-create " *mime-example*")))
372     (unwind-protect
373         (save-excursion
374           (set-buffer buffer)
375           (setq buffer-file-name file)
376           (erase-buffer)
377           (insert ";;; " (file-name-nondirectory file) "\n")
378           (insert "\n;; This file is generated automatically by "
379                   mime-view-version "\n\n")
380           (insert ";;; Code:\n\n")
381           (pp `(setq mime-acting-situation-example-list
382                      ',mime-acting-situation-example-list)
383               (current-buffer))
384           (insert "\n;;; "
385                   (file-name-nondirectory file)
386                   " ends here.\n")
387           (save-buffer))
388       (kill-buffer buffer))))
389
390 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
391
392 (defun mime-reduce-acting-situation-examples ()
393   (let ((len (length mime-acting-situation-example-list))
394         i ir ic j jr jc ret
395         dest d-i d-j
396         (max-sim 0) sim
397         min-det-ret det-ret
398         min-det-org det-org
399         min-freq freq)
400     (setq i 0
401           ir mime-acting-situation-example-list)
402     (while (< i len)
403       (setq ic (car ir)
404             j 0
405             jr mime-acting-situation-example-list)
406       (while (< j len)
407         (unless (= i j)
408           (setq jc (car jr))
409           (setq ret (mime-compare-situation-with-example (car ic)(car jc))
410                 sim (car ret)
411                 det-ret (+ (length (car ic))(length (car jc)))
412                 det-org (length (cdr ret))
413                 freq (+ (cdr ic)(cdr jc)))
414           (cond ((< max-sim sim)
415                  (setq max-sim sim
416                        min-det-ret det-ret
417                        min-det-org det-org
418                        min-freq freq
419                        d-i i
420                        d-j j
421                        dest (cons (cdr ret) freq))
422                  )
423                 ((= max-sim sim)
424                  (cond ((> min-det-ret det-ret)
425                         (setq min-det-ret det-ret
426                               min-det-org det-org
427                               min-freq freq
428                               d-i i
429                               d-j j
430                               dest (cons (cdr ret) freq))
431                         )
432                        ((= min-det-ret det-ret)
433                         (cond ((> min-det-org det-org)
434                                (setq min-det-org det-org
435                                      min-freq freq
436                                      d-i i
437                                      d-j j
438                                      dest (cons (cdr ret) freq))
439                                )
440                               ((= min-det-org det-org)
441                                (cond ((> min-freq freq)
442                                       (setq min-freq freq
443                                             d-i i
444                                             d-j j
445                                             dest (cons (cdr ret) freq))
446                                       ))
447                                ))
448                         ))
449                  ))
450           )
451         (setq jr (cdr jr)
452               j (1+ j)))
453       (setq ir (cdr ir)
454             i (1+ i)))
455     (if (> d-i d-j)
456         (setq i d-i
457               d-i d-j
458               d-j i))
459     (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
460     (setcdr jr (cddr jr))
461     (if (= d-i 0)
462         (setq mime-acting-situation-example-list
463               (cdr mime-acting-situation-example-list))
464       (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
465       (setcdr ir (cddr ir))
466       )
467     (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
468         (setcdr ir (+ (cdr ir)(cdr dest)))
469       (setq mime-acting-situation-example-list
470             (cons dest mime-acting-situation-example-list))
471       )))
472
473
474 ;;; @ presentation of preview
475 ;;;
476
477 ;;; @@ entity-button
478 ;;;
479
480 ;;; @@@ predicate function
481 ;;;
482
483 (defun mime-view-entity-button-visible-p (entity)
484   "Return non-nil if header of ENTITY is visible.
485 Please redefine this function if you want to change default setting."
486   (let ((media-type (mime-entity-media-type entity))
487         (media-subtype (mime-entity-media-subtype entity)))
488     (or (not (eq media-type 'application))
489         (and (not (eq media-subtype 'x-selection))
490              (or (not (eq media-subtype 'octet-stream))
491                  (let ((mother-entity (mime-entity-parent entity)))
492                    (or (not (eq (mime-entity-media-type mother-entity)
493                                 'multipart))
494                        (not (eq (mime-entity-media-subtype mother-entity)
495                                 'encrypted)))
496                    )
497                  )))))
498
499 ;;; @@@ entity button generator
500 ;;;
501
502 (defun mime-view-insert-entity-button (entity)
503   "Insert entity-button of ENTITY."
504   (let ((entity-node-id (mime-entity-node-id entity))
505         (params (mime-entity-parameters entity))
506         (subject (mime-view-entity-title entity)))
507     (mime-insert-button
508      (let ((access-type (assoc "access-type" params))
509            (num (or (cdr (assoc "x-part-number" params))
510                     (if (consp entity-node-id)
511                         (mapconcat (function
512                                     (lambda (num)
513                                       (format "%s" (1+ num))
514                                       ))
515                                    (reverse entity-node-id) ".")
516                       "0"))
517                 ))
518        (cond (access-type
519               (let ((server (assoc "server" params)))
520                 (setq access-type (cdr access-type))
521                 (if server
522                     (format "%s %s ([%s] %s)"
523                             num subject access-type (cdr server))
524                 (let ((site (cdr (assoc "site" params)))
525                       (dir (cdr (assoc "directory" params)))
526                       (url (cdr (assoc "url" params)))
527                       )
528                   (if url
529                       (format "%s %s ([%s] %s)"
530                               num subject access-type url)
531                     (format "%s %s ([%s] %s:%s)"
532                             num subject access-type site dir))
533                   )))
534             )
535            (t
536             (let ((media-type (mime-entity-media-type entity))
537                   (media-subtype (mime-entity-media-subtype entity))
538                   (charset (cdr (assoc "charset" params)))
539                   (encoding (mime-entity-encoding entity)))
540               (concat
541                num " " subject
542                (let ((rest
543                       (format " <%s/%s%s%s>"
544                               media-type media-subtype
545                               (if charset
546                                   (concat "; " charset)
547                                 "")
548                               (if encoding
549                                   (concat " (" encoding ")")
550                                 ""))))
551                  (if (>= (+ (current-column)(length rest))(window-width))
552                      "\n\t")
553                  rest)))
554             )))
555      (function mime-preview-play-current-entity))
556     ))
557
558
559 ;;; @@ entity-header
560 ;;;
561
562 (defvar mime-header-presentation-method-alist nil
563   "Alist of major mode vs. corresponding header-presentation-method functions.
564 Each element looks like (SYMBOL . FUNCTION).
565 SYMBOL must be major mode in raw-buffer or t.  t means default.
566 Interface of FUNCTION must be (ENTITY SITUATION).")
567
568 (defvar mime-view-ignored-field-list
569   '(".*Received:" ".*Path:" ".*Id:" "^References:"
570     "^Replied:" "^Errors-To:"
571     "^Lines:" "^Sender:" ".*Host:" "^Xref:"
572     "^Content-Type:" "^Precedence:"
573     "^Status:" "^X-VM-.*:")
574   "All fields that match this list will be hidden in MIME preview buffer.
575 Each elements are regexp of field-name.")
576
577 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
578   "All fields that match this list will be displayed in MIME preview buffer.
579 Each elements are regexp of field-name.")
580
581
582 ;;; @@ entity-body
583 ;;;
584
585 ;;; @@@ predicate function
586 ;;;
587
588 (in-calist-package 'mime-view)
589
590 (defun mime-calist::field-match-method-as-default-rule (calist
591                                                         field-type field-value)
592   (let ((s-field (assq field-type calist)))
593     (cond ((null s-field)
594            (cons (cons field-type field-value) calist)
595            )
596           (t calist))))
597
598 (define-calist-field-match-method
599   'header #'mime-calist::field-match-method-as-default-rule)
600
601 (define-calist-field-match-method
602   'body #'mime-calist::field-match-method-as-default-rule)
603
604
605 (defvar mime-preview-condition nil
606   "Condition-tree about how to display entity.")
607
608 (ctree-set-calist-strictly
609  'mime-preview-condition '((type . application)(subtype . octet-stream)
610                            (encoding . nil)
611                            (body . visible)))
612 (ctree-set-calist-strictly
613  'mime-preview-condition '((type . application)(subtype . octet-stream)
614                            (encoding . "7bit")
615                            (body . visible)))
616 (ctree-set-calist-strictly
617  'mime-preview-condition '((type . application)(subtype . octet-stream)
618                            (encoding . "8bit")
619                            (body . visible)))
620
621 (ctree-set-calist-strictly
622  'mime-preview-condition '((type . application)(subtype . pgp)
623                            (body . visible)))
624
625 (ctree-set-calist-strictly
626  'mime-preview-condition '((type . application)(subtype . x-latex)
627                            (body . visible)))
628
629 (ctree-set-calist-strictly
630  'mime-preview-condition '((type . application)(subtype . x-selection)
631                            (body . visible)))
632
633 (ctree-set-calist-strictly
634  'mime-preview-condition '((type . application)(subtype . x-comment)
635                            (body . visible)))
636
637 (ctree-set-calist-strictly
638  'mime-preview-condition '((type . message)(subtype . delivery-status)
639                            (body . visible)))
640
641 (ctree-set-calist-strictly
642  'mime-preview-condition
643  '((body . visible)
644    (body-presentation-method . mime-display-text/plain)))
645
646 (ctree-set-calist-strictly
647  'mime-preview-condition
648  '((type . nil)
649    (body . visible)
650    (body-presentation-method . mime-display-text/plain)))
651
652 (ctree-set-calist-strictly
653  'mime-preview-condition
654  '((type . text)(subtype . enriched)
655    (body . visible)
656    (body-presentation-method . mime-display-text/enriched)))
657
658 (ctree-set-calist-strictly
659  'mime-preview-condition
660  '((type . text)(subtype . richtext)
661    (body . visible)
662    (body-presentation-method . mime-display-text/richtext)))
663
664 (autoload 'mime-display-application/x-postpet "postpet")
665
666 (ctree-set-calist-strictly
667  'mime-preview-condition
668  '((type . application)(subtype . x-postpet)
669    (body . visible)
670    (body-presentation-method . mime-display-application/x-postpet)))
671
672 (ctree-set-calist-strictly
673  'mime-preview-condition
674  '((type . text)(subtype . t)
675    (body . visible)
676    (body-presentation-method . mime-display-text/plain)))
677
678 (ctree-set-calist-strictly
679  'mime-preview-condition
680  '((type . multipart)(subtype . alternative)
681    (body . visible)
682    (body-presentation-method . mime-display-multipart/alternative)))
683
684 (ctree-set-calist-strictly
685  'mime-preview-condition '((type . message)(subtype . partial)
686                            (body-presentation-method
687                             . mime-display-message/partial-button)))
688
689 (ctree-set-calist-strictly
690  'mime-preview-condition '((type . message)(subtype . rfc822)
691                            (body-presentation-method . nil)
692                            (childrens-situation (header . visible)
693                                                 (entity-button . invisible))))
694
695 (ctree-set-calist-strictly
696  'mime-preview-condition '((type . message)(subtype . news)
697                            (body-presentation-method . nil)
698                            (childrens-situation (header . visible)
699                                                 (entity-button . invisible))))
700
701
702 ;;; @@@ entity presentation
703 ;;;
704
705 (defun mime-display-text/plain (entity situation)
706   (save-restriction
707     (narrow-to-region (point-max)(point-max))
708     (mime-insert-text-content entity)
709     (run-hooks 'mime-text-decode-hook)
710     (goto-char (point-max))
711     (if (not (eq (char-after (1- (point))) ?\n))
712         (insert "\n")
713       )
714     (mime-add-url-buttons)
715     (run-hooks 'mime-display-text/plain-hook)
716     ))
717
718 (defun mime-display-text/richtext (entity situation)
719   (save-restriction
720     (narrow-to-region (point-max)(point-max))
721     (mime-insert-text-content entity)
722     (run-hooks 'mime-text-decode-hook)
723     (let ((beg (point-min)))
724       (remove-text-properties beg (point-max) '(face nil))
725       (richtext-decode beg (point-max))
726       )))
727
728 (defun mime-display-text/enriched (entity situation)
729   (save-restriction
730     (narrow-to-region (point-max)(point-max))
731     (mime-insert-text-content entity)
732     (run-hooks 'mime-text-decode-hook)
733     (let ((beg (point-min)))
734       (remove-text-properties beg (point-max) '(face nil))
735       (enriched-decode beg (point-max))
736       )))
737
738
739 (defvar mime-view-announcement-for-message/partial
740   (if (and (>= emacs-major-version 19) window-system)
741       "\
742 \[[ This is message/partial style split message. ]]
743 \[[ Please press `v' key in this buffer          ]]
744 \[[ or click here by mouse button-2.             ]]"
745     "\
746 \[[ This is message/partial style split message. ]]
747 \[[ Please press `v' key in this buffer.         ]]"
748     ))
749
750 (defun mime-display-message/partial-button (&optional entity situation)
751   (save-restriction
752     (goto-char (point-max))
753     (if (not (search-backward "\n\n" nil t))
754         (insert "\n")
755       )
756     (goto-char (point-max))
757     (narrow-to-region (point-max)(point-max))
758     (insert mime-view-announcement-for-message/partial)
759     (mime-add-button (point-min)(point-max)
760                      #'mime-preview-play-current-entity)
761     ))
762
763 (defun mime-display-multipart/mixed (entity situation)
764   (let ((children (mime-entity-children entity))
765         (original-major-mode-cell (assq 'major-mode situation))
766         (default-situation
767           (cdr (assq 'childrens-situation situation))))
768     (if original-major-mode-cell
769         (setq default-situation
770               (cons original-major-mode-cell default-situation)))
771     (while children
772       (mime-display-entity (car children) nil default-situation)
773       (setq children (cdr children))
774       )))
775
776 (defcustom mime-view-type-subtype-score-alist
777   '(((text . enriched) . 3)
778     ((text . richtext) . 2)
779     ((text . plain)    . 1)
780     (t . 0))
781   "Alist MEDIA-TYPE vs corresponding score.
782 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
783   :group 'mime-view
784   :type '(repeat (cons (choice :tag "Media-Type"
785                                (cons :tag "Type/Subtype"
786                                      (symbol :tag "Primary-type")
787                                      (symbol :tag "Subtype"))
788                                (symbol :tag "Type")
789                                (const :tag "Default" t))
790                        integer)))
791
792 (defun mime-display-multipart/alternative (entity situation)
793   (let* ((children (mime-entity-children entity))
794          (original-major-mode-cell (assq 'major-mode situation))
795          (default-situation
796            (cdr (assq 'childrens-situation situation)))
797          (i 0)
798          (p 0)
799          (max-score 0)
800          situations)
801     (if original-major-mode-cell
802         (setq default-situation
803               (cons original-major-mode-cell default-situation)))
804     (setq situations
805           (mapcar (function
806                    (lambda (child)
807                      (let ((situation
808                             (or (ctree-match-calist
809                                  mime-preview-condition
810                                  (append (mime-entity-situation child)
811                                          default-situation))
812                                 default-situation)))
813                        (if (cdr (assq 'body-presentation-method situation))
814                            (let ((score
815                                   (cdr
816                                    (or (assoc
817                                         (cons
818                                          (cdr (assq 'type situation))
819                                          (cdr (assq 'subtype situation)))
820                                         mime-view-type-subtype-score-alist)
821                                        (assq
822                                         (cdr (assq 'type situation))
823                                         mime-view-type-subtype-score-alist)
824                                        (assq
825                                         t
826                                         mime-view-type-subtype-score-alist)
827                                        ))))
828                              (if (> score max-score)
829                                  (setq p i
830                                        max-score score)
831                                )))
832                        (setq i (1+ i))
833                        situation)
834                      ))
835                   children))
836     (setq i 0)
837     (while children
838       (let ((child (car children))
839             (situation (car situations)))
840         (mime-display-entity child (if (= i p)
841                                        situation
842                                      (del-alist 'body-presentation-method
843                                                 (copy-alist situation))))
844         )
845       (setq children (cdr children)
846             situations (cdr situations)
847             i (1+ i))
848       )))
849
850
851 ;;; @ acting-condition
852 ;;;
853
854 (defvar mime-acting-condition nil
855   "Condition-tree about how to process entity.")
856
857 (if (file-readable-p mailcap-file)
858     (let ((entries (mailcap-parse-file)))
859       (while entries
860         (let ((entry (car entries))
861               view print shared)
862           (while entry
863             (let* ((field (car entry))
864                    (field-type (car field)))
865               (cond ((eq field-type 'view)  (setq view field))
866                     ((eq field-type 'print) (setq print field))
867                     ((memq field-type '(compose composetyped edit)))
868                     (t (setq shared (cons field shared))))
869               )
870             (setq entry (cdr entry))
871             )
872           (setq shared (nreverse shared))
873           (ctree-set-calist-with-default
874            'mime-acting-condition
875            (append shared (list '(mode . "play")(cons 'method (cdr view)))))
876           (if print
877               (ctree-set-calist-with-default
878                'mime-acting-condition
879                (append shared
880                        (list '(mode . "print")(cons 'method (cdr view))))
881                ))
882           )
883         (setq entries (cdr entries))
884         )))
885
886 (ctree-set-calist-strictly
887  'mime-acting-condition
888  '((type . application)(subtype . octet-stream)
889    (mode . "play")
890    (method . mime-detect-content)
891    ))
892
893 (ctree-set-calist-with-default
894  'mime-acting-condition
895  '((mode . "extract")
896    (method . mime-save-content)))
897
898 (ctree-set-calist-strictly
899  'mime-acting-condition
900  '((type . text)(subtype . x-rot13-47)(mode . "play")
901    (method . mime-view-caesar)
902    ))
903 (ctree-set-calist-strictly
904  'mime-acting-condition
905  '((type . text)(subtype . x-rot13-47-48)(mode . "play")
906    (method . mime-view-caesar)
907    ))
908
909 (ctree-set-calist-strictly
910  'mime-acting-condition
911  '((type . message)(subtype . rfc822)(mode . "play")
912    (method . mime-view-message/rfc822)
913    ))
914 (ctree-set-calist-strictly
915  'mime-acting-condition
916  '((type . message)(subtype . partial)(mode . "play")
917    (method . mime-store-message/partial-piece)
918    ))
919
920 (ctree-set-calist-strictly
921  'mime-acting-condition
922  '((type . message)(subtype . external-body)
923    ("access-type" . "anon-ftp")
924    (method . mime-view-message/external-anon-ftp)
925    ))
926
927 (ctree-set-calist-strictly
928  'mime-acting-condition
929  '((type . message)(subtype . external-body)
930    ("access-type" . "url")
931    (method . mime-view-message/external-url)
932    ))
933
934 (ctree-set-calist-strictly
935  'mime-acting-condition
936  '((type . application)(subtype . octet-stream)
937    (method . mime-save-content)
938    ))
939
940
941 ;;; @ quitting method
942 ;;;
943
944 (defvar mime-preview-quitting-method-alist
945   '((mime-show-message-mode
946      . mime-preview-quitting-method-for-mime-show-message-mode))
947   "Alist of major-mode vs. quitting-method of mime-view.")
948
949 (defvar mime-preview-over-to-previous-method-alist nil
950   "Alist of major-mode vs. over-to-previous-method of mime-view.")
951
952 (defvar mime-preview-over-to-next-method-alist nil
953   "Alist of major-mode vs. over-to-next-method of mime-view.")
954
955
956 ;;; @ following method
957 ;;;
958
959 (defvar mime-preview-following-method-alist nil
960   "Alist of major-mode vs. following-method of mime-view.")
961
962 (defvar mime-view-following-required-fields-list
963   '("From"))
964
965
966 ;;; @ buffer setup
967 ;;;
968
969 (defun mime-display-entity (entity &optional situation
970                                    default-situation preview-buffer)
971   (or preview-buffer
972       (setq preview-buffer (current-buffer)))
973   (let* (e nb ne nhb nbb)
974     (mime-goto-header-start-point entity)
975     (in-calist-package 'mime-view)
976     (or situation
977         (setq situation
978               (or (ctree-match-calist mime-preview-condition
979                                       (append (mime-entity-situation entity)
980                                               default-situation))
981                   default-situation)))
982     (let ((button-is-invisible
983            (eq (cdr (assq 'entity-button situation)) 'invisible))
984           (header-is-visible
985            (eq (cdr (assq 'header situation)) 'visible))
986           (header-presentation-method
987            (or (cdr (assq 'header-presentation-method situation))
988                (cdr (assq (cdr (assq 'major-mode situation))
989                           mime-header-presentation-method-alist))))
990           (body-presentation-method
991            (cdr (assq 'body-presentation-method situation)))
992           (children (mime-entity-children entity)))
993       (set-buffer preview-buffer)
994       (setq nb (point))
995       (narrow-to-region nb nb)
996       (or button-is-invisible
997           (if (mime-view-entity-button-visible-p entity)
998               (mime-view-insert-entity-button entity)
999             ))
1000       (when header-is-visible
1001         (setq nhb (point))
1002         (if header-presentation-method
1003             (funcall header-presentation-method entity situation)
1004           (mime-insert-header entity
1005                               mime-view-ignored-field-list
1006                               mime-view-visible-field-list))
1007         (run-hooks 'mime-display-header-hook)
1008         (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1009         (goto-char (point-max))
1010         (insert "\n")
1011         )
1012       (setq nbb (point))
1013       (cond (children)
1014             ((functionp body-presentation-method)
1015              (funcall body-presentation-method entity situation)
1016              )
1017             (t
1018              (when button-is-invisible
1019                (goto-char (point-max))
1020                (mime-view-insert-entity-button entity)
1021                )
1022              (or header-is-visible
1023                  (progn
1024                    (goto-char (point-max))
1025                    (insert "\n")
1026                    ))
1027              ))
1028       (setq ne (point-max))
1029       (widen)
1030       (put-text-property nb ne 'mime-view-entity entity)
1031       (put-text-property nb ne 'mime-view-situation situation)
1032       (put-text-property nbb ne 'mime-view-entity-body entity)
1033       (goto-char ne)
1034       (if children
1035           (if (functionp body-presentation-method)
1036               (funcall body-presentation-method entity situation)
1037             (mime-display-multipart/mixed entity situation)
1038             ))
1039       )))
1040
1041
1042 ;;; @ MIME viewer mode
1043 ;;;
1044
1045 (defconst mime-view-menu-title "MIME-View")
1046 (defconst mime-view-menu-list
1047   '((up          "Move to upper entity"    mime-preview-move-to-upper)
1048     (previous    "Move to previous entity" mime-preview-move-to-previous)
1049     (next        "Move to next entity"     mime-preview-move-to-next)
1050     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
1051     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
1052     (play        "Play current entity"     mime-preview-play-current-entity)
1053     (extract     "Extract current entity"  mime-preview-extract-current-entity)
1054     (print       "Print current entity"    mime-preview-print-current-entity)
1055     )
1056   "Menu for MIME Viewer")
1057
1058 (cond ((featurep 'xemacs)
1059        (defvar mime-view-xemacs-popup-menu
1060          (cons mime-view-menu-title
1061                (mapcar (function
1062                         (lambda (item)
1063                           (vector (nth 1 item)(nth 2 item) t)
1064                           ))
1065                        mime-view-menu-list)))
1066        (defun mime-view-xemacs-popup-menu (event)
1067          "Popup the menu in the MIME Viewer buffer"
1068          (interactive "e")
1069          (select-window (event-window event))
1070          (set-buffer (event-buffer event))
1071          (popup-menu 'mime-view-xemacs-popup-menu))
1072        (defvar mouse-button-2 'button2)
1073        )
1074       (t
1075        (defvar mime-view-popup-menu 
1076          (let ((menu (make-sparse-keymap mime-view-menu-title)))
1077            (nconc menu
1078                   (mapcar (function
1079                            (lambda (item)
1080                              (list (intern (nth 1 item)) 'menu-item 
1081                                    (nth 1 item)(nth 2 item))
1082                              ))
1083                           mime-view-menu-list))))
1084        (defun mime-view-popup-menu (event)
1085          "Popup the menu in the MIME Viewer buffer"
1086          (interactive "@e")
1087          (let ((menu mime-view-popup-menu) events func)
1088            (setq events (x-popup-menu t menu))
1089            (and events
1090                 (setq func (lookup-key menu (apply #'vector events)))
1091                 (commandp func)
1092                 (funcall func))))
1093        (defvar mouse-button-2 [mouse-2])
1094        ))
1095
1096 (defun mime-view-define-keymap (&optional default)
1097   (let ((mime-view-mode-map (if (keymapp default)
1098                                 (copy-keymap default)
1099                               (make-sparse-keymap)
1100                               )))
1101     (define-key mime-view-mode-map
1102       "u"        (function mime-preview-move-to-upper))
1103     (define-key mime-view-mode-map
1104       "p"        (function mime-preview-move-to-previous))
1105     (define-key mime-view-mode-map
1106       "n"        (function mime-preview-move-to-next))
1107     (define-key mime-view-mode-map
1108       "\e\t"     (function mime-preview-move-to-previous))
1109     (define-key mime-view-mode-map
1110       "\t"       (function mime-preview-move-to-next))
1111     (define-key mime-view-mode-map
1112       " "        (function mime-preview-scroll-up-entity))
1113     (define-key mime-view-mode-map
1114       "\M- "     (function mime-preview-scroll-down-entity))
1115     (define-key mime-view-mode-map
1116       "\177"     (function mime-preview-scroll-down-entity))
1117     (define-key mime-view-mode-map
1118       "\C-m"     (function mime-preview-next-line-entity))
1119     (define-key mime-view-mode-map
1120       "\C-\M-m"  (function mime-preview-previous-line-entity))
1121     (define-key mime-view-mode-map
1122       "v"        (function mime-preview-play-current-entity))
1123     (define-key mime-view-mode-map
1124       "e"        (function mime-preview-extract-current-entity))
1125     (define-key mime-view-mode-map
1126       "\C-c\C-p" (function mime-preview-print-current-entity))
1127     (define-key mime-view-mode-map
1128       "a"        (function mime-preview-follow-current-entity))
1129     (define-key mime-view-mode-map
1130       "q"        (function mime-preview-quit))
1131     (define-key mime-view-mode-map
1132       "\C-c\C-x" (function mime-preview-kill-buffer))
1133     ;; (define-key mime-view-mode-map
1134     ;;   "<"        (function beginning-of-buffer))
1135     ;; (define-key mime-view-mode-map
1136     ;;   ">"        (function end-of-buffer))
1137     (define-key mime-view-mode-map
1138       "?"        (function describe-mode))
1139     (define-key mime-view-mode-map
1140       [tab] (function mime-preview-move-to-next))
1141     (define-key mime-view-mode-map
1142       [delete] (function mime-preview-scroll-down-entity))
1143     (define-key mime-view-mode-map
1144       [backspace] (function mime-preview-scroll-down-entity))
1145     (if (functionp default)
1146         (cond ((featurep 'xemacs)
1147                (set-keymap-default-binding mime-view-mode-map default)
1148                )
1149               (t
1150                (setq mime-view-mode-map
1151                      (append mime-view-mode-map (list (cons t default))))
1152                )))
1153     (if mouse-button-2
1154         (define-key mime-view-mode-map
1155           mouse-button-2 (function mime-button-dispatcher))
1156       )
1157     (cond ((featurep 'xemacs)
1158            (define-key mime-view-mode-map
1159              mouse-button-3 (function mime-view-xemacs-popup-menu))
1160            )
1161           ((>= emacs-major-version 19)
1162            (define-key mime-view-mode-map
1163              mouse-button-3 (function mime-view-popup-menu))
1164            (define-key mime-view-mode-map [menu-bar mime-view]
1165              (cons mime-view-menu-title
1166                    (make-sparse-keymap mime-view-menu-title)))
1167            (mapcar (function
1168                     (lambda (item)
1169                       (define-key mime-view-mode-map
1170                         (vector 'menu-bar 'mime-view (car item))
1171                         (cons (nth 1 item)(nth 2 item))
1172                         )
1173                       ))
1174                    (reverse mime-view-menu-list)
1175                    )
1176            ))
1177     (use-local-map mime-view-mode-map)
1178     (run-hooks 'mime-view-define-keymap-hook)
1179     ))
1180
1181 (defsubst mime-maybe-hide-echo-buffer ()
1182   "Clear mime-echo buffer and delete window for it."
1183   (let ((buf (get-buffer mime-echo-buffer-name)))
1184     (if buf
1185         (save-excursion
1186           (set-buffer buf)
1187           (erase-buffer)
1188           (let ((win (get-buffer-window buf)))
1189             (if win
1190                 (delete-window win)
1191               ))
1192           (bury-buffer buf)
1193           ))))
1194
1195 (defvar mime-view-redisplay nil)
1196
1197 ;;;###autoload
1198 (defun mime-display-message (message &optional preview-buffer
1199                                      mother default-keymap-or-function
1200                                      original-major-mode)
1201   "View MESSAGE in MIME-View mode.
1202
1203 Optional argument PREVIEW-BUFFER specifies the buffer of the
1204 presentation.  It must be either nil or a name of preview buffer.
1205
1206 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1207
1208 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1209 function.  If it is a keymap, keymap of MIME-View mode will be added
1210 to it.  If it is a function, it will be bound as default binding of
1211 keymap of MIME-View mode."
1212   (mime-maybe-hide-echo-buffer)
1213   (let ((win-conf (current-window-configuration)))
1214     (or preview-buffer
1215         (setq preview-buffer
1216               (concat "*Preview-" (mime-entity-name message) "*")))
1217     (or original-major-mode
1218         (setq original-major-mode
1219               (with-current-buffer (mime-entity-header-buffer message)
1220                 major-mode)))
1221     (let ((inhibit-read-only t))
1222       (set-buffer (get-buffer-create preview-buffer))
1223       (widen)
1224       (erase-buffer)
1225       (if mother
1226           (setq mime-mother-buffer mother)
1227         )
1228       (setq mime-preview-original-window-configuration win-conf)
1229       (setq major-mode 'mime-view-mode)
1230       (setq mode-name "MIME-View")
1231       (mime-display-entity message nil
1232                            `((entity-button . invisible)
1233                              (header . visible)
1234                              (major-mode . ,original-major-mode))
1235                            preview-buffer)
1236       (mime-view-define-keymap default-keymap-or-function)
1237       (let ((point
1238              (next-single-property-change (point-min) 'mime-view-entity)))
1239         (if point
1240             (goto-char point)
1241           (goto-char (point-min))
1242           (search-forward "\n\n" nil t)
1243           ))
1244       (run-hooks 'mime-view-mode-hook)
1245       (set-buffer-modified-p nil)
1246       (setq buffer-read-only t)
1247       preview-buffer)))
1248
1249 ;;;###autoload
1250 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1251                                    default-keymap-or-function
1252                                    representation-type)
1253   "View RAW-BUFFER in MIME-View mode.
1254 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1255 buffer.
1256 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1257 function.  If it is a keymap, keymap of MIME-View mode will be added
1258 to it.  If it is a function, it will be bound as default binding of
1259 keymap of MIME-View mode.
1260 Optional argument REPRESENTATION-TYPE is representation-type of
1261 message.  It must be nil, `binary' or `cooked'.  If it is nil,
1262 `cooked' is used as default."
1263   (interactive)
1264   (or raw-buffer
1265       (setq raw-buffer (current-buffer)))
1266   (or representation-type
1267       (setq representation-type
1268             (save-excursion
1269               (set-buffer raw-buffer)
1270               (cdr (or (assq major-mode mime-raw-representation-type-alist)
1271                        (assq t mime-raw-representation-type-alist)))
1272               )))
1273   (if (eq representation-type 'binary)
1274       (setq representation-type 'buffer)
1275     )
1276   (setq preview-buffer (mime-display-message
1277                         (mime-open-entity representation-type raw-buffer)
1278                         preview-buffer mother default-keymap-or-function))
1279   (or (get-buffer-window preview-buffer)
1280       (let ((r-win (get-buffer-window raw-buffer)))
1281         (if r-win
1282             (set-window-buffer r-win preview-buffer)
1283           (let ((m-win (and mother (get-buffer-window mother))))
1284             (if m-win
1285                 (set-window-buffer m-win preview-buffer)
1286               (switch-to-buffer preview-buffer)
1287               ))))))
1288
1289 (defun mime-view-mode (&optional mother ctl encoding
1290                                  raw-buffer preview-buffer
1291                                  default-keymap-or-function)
1292   "Major mode for viewing MIME message.
1293
1294 Here is a list of the standard keys for mime-view-mode.
1295
1296 key             feature
1297 ---             -------
1298
1299 u               Move to upper content
1300 p or M-TAB      Move to previous content
1301 n or TAB        Move to next content
1302 SPC             Scroll up or move to next content
1303 M-SPC or DEL    Scroll down or move to previous content
1304 RET             Move to next line
1305 M-RET           Move to previous line
1306 v               Decode current content as `play mode'
1307 e               Decode current content as `extract mode'
1308 C-c C-p         Decode current content as `print mode'
1309 a               Followup to current content.
1310 q               Quit
1311 button-2        Move to point under the mouse cursor
1312                 and decode current content as `play mode'
1313 "
1314   (interactive)
1315   (unless mime-view-redisplay
1316     (save-excursion
1317       (if raw-buffer (set-buffer raw-buffer))
1318       (let ((type
1319              (cdr
1320               (or (assq major-mode mime-raw-representation-type-alist)
1321                   (assq t mime-raw-representation-type-alist)))))
1322         (if (eq type 'binary)
1323             (setq type 'buffer)
1324           )
1325         (setq mime-message-structure (mime-open-entity type raw-buffer))
1326         (or (mime-entity-content-type mime-message-structure)
1327             (mime-entity-set-content-type-internal
1328              mime-message-structure ctl))
1329         )
1330       (or (mime-entity-encoding mime-message-structure)
1331           (mime-entity-set-encoding-internal mime-message-structure encoding))
1332       ))
1333   (mime-display-message mime-message-structure preview-buffer
1334                         mother default-keymap-or-function)
1335   )
1336
1337
1338 ;;; @@ playing
1339 ;;;
1340
1341 (autoload 'mime-preview-play-current-entity "mime-play"
1342   "Play current entity." t)
1343
1344 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1345   "Extract current entity into file (maybe).
1346 It decodes current entity to call internal or external method as
1347 \"extract\" mode.  The method is selected from variable
1348 `mime-acting-condition'."
1349   (interactive "P")
1350   (mime-preview-play-current-entity ignore-examples "extract")
1351   )
1352
1353 (defun mime-preview-print-current-entity (&optional ignore-examples)
1354   "Print current entity (maybe).
1355 It decodes current entity to call internal or external method as
1356 \"print\" mode.  The method is selected from variable
1357 `mime-acting-condition'."
1358   (interactive "P")
1359   (mime-preview-play-current-entity ignore-examples "print")
1360   )
1361
1362
1363 ;;; @@ following
1364 ;;;
1365
1366 (defun mime-preview-follow-current-entity ()
1367   "Write follow message to current entity.
1368 It calls following-method selected from variable
1369 `mime-preview-following-method-alist'."
1370   (interactive)
1371   (let (entity)
1372     (while (null (setq entity
1373                        (get-text-property (point) 'mime-view-entity)))
1374       (backward-char)
1375       )
1376     (let* ((p-beg
1377             (previous-single-property-change (point) 'mime-view-entity))
1378            p-end
1379            ph-end
1380            (entity-node-id (mime-entity-node-id entity))
1381            (len (length entity-node-id))
1382            )
1383       (cond ((null p-beg)
1384              (setq p-beg
1385                    (if (eq (next-single-property-change (point-min)
1386                                                         'mime-view-entity)
1387                            (point))
1388                        (point)
1389                      (point-min)))
1390              )
1391             ((eq (next-single-property-change p-beg 'mime-view-entity)
1392                  (point))
1393              (setq p-beg (point))
1394              ))
1395       (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1396       (cond ((null p-end)
1397              (setq p-end (point-max))
1398              )
1399             ((null entity-node-id)
1400              (setq p-end (point-max))
1401              )
1402             (t
1403              (save-excursion
1404                (goto-char p-end)
1405                (catch 'tag
1406                  (let (e)
1407                    (while (setq e
1408                                 (next-single-property-change
1409                                  (point) 'mime-view-entity))
1410                      (goto-char e)
1411                      (let ((rc (mime-entity-node-id
1412                                 (get-text-property (point)
1413                                                    'mime-view-entity))))
1414                        (or (equal entity-node-id
1415                                   (nthcdr (- (length rc) len) rc))
1416                            (throw 'tag nil)
1417                            ))
1418                      (setq p-end e)
1419                      ))
1420                  (setq p-end (point-max))
1421                  ))
1422              ))
1423       (setq ph-end
1424             (previous-single-property-change p-end 'mime-view-entity-header))
1425       (if (or (null ph-end)
1426               (< ph-end p-beg))
1427           (setq ph-end p-beg)
1428         )
1429       (let* ((mode (mime-preview-original-major-mode 'recursive))
1430              (new-name
1431               (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1432              new-buf
1433              (the-buf (current-buffer))
1434              fields)
1435         (save-excursion
1436           (set-buffer (setq new-buf (get-buffer-create new-name)))
1437           (erase-buffer)
1438           (insert-buffer-substring the-buf ph-end p-end)
1439           (when (= ph-end p-beg)
1440             (goto-char (point-min))
1441             (insert ?\n))
1442           (goto-char (point-min))
1443           (let ((current-entity
1444                  (if (and (eq (mime-entity-media-type entity) 'message)
1445                           (eq (mime-entity-media-subtype entity) 'rfc822))
1446                      (mime-entity-children entity)
1447                    entity))
1448                 str)
1449             (while (and current-entity
1450                         (progn
1451                           (setq str
1452                                 (with-current-buffer
1453                                     (mime-entity-header-buffer current-entity)
1454                                   (save-restriction
1455                                     (narrow-to-region
1456                                      (mime-entity-header-start-point
1457                                       current-entity)
1458                                      (mime-entity-header-end-point
1459                                       current-entity))
1460                                     (std11-header-string-except
1461                                      (concat
1462                                       "^"
1463                                       (apply (function regexp-or) fields)
1464                                       ":") ""))))
1465                           (if (and (eq (mime-entity-media-type
1466                                         current-entity) 'message)
1467                                    (eq (mime-entity-media-subtype
1468                                         current-entity) 'rfc822))
1469                               nil
1470                             (if str
1471                                 (insert str)
1472                               )
1473                             t)))
1474               (setq fields (std11-collect-field-names)
1475                     current-entity (mime-entity-parent current-entity))
1476               )
1477             )
1478           (let ((rest mime-view-following-required-fields-list)
1479                 field-name ret)
1480             (while rest
1481               (setq field-name (car rest))
1482               (or (std11-field-body field-name)
1483                   (progn
1484                     (save-excursion
1485                       (set-buffer the-buf)
1486                       (let ((entity (when mime-mother-buffer
1487                                       (set-buffer mime-mother-buffer)
1488                                       (get-text-property (point)
1489                                                          'mime-view-entity))))
1490                         (while (and entity
1491                                     (null (setq ret (mime-entity-fetch-field
1492                                                      entity field-name))))
1493                           (setq entity (mime-entity-parent entity)))))
1494                     (if ret
1495                         (insert (concat field-name ": " ret "\n"))
1496                       )))
1497               (setq rest (cdr rest))
1498               ))
1499           (mime-decode-header-in-buffer)
1500           )
1501         (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1502           (if (functionp f)
1503               (funcall f new-buf)
1504             (message
1505              (format
1506               "Sorry, following method for %s is not implemented yet."
1507               mode))
1508             ))
1509         ))))
1510
1511
1512 ;;; @@ moving
1513 ;;;
1514
1515 (defun mime-preview-move-to-upper ()
1516   "Move to upper entity.
1517 If there is no upper entity, call function `mime-preview-quit'."
1518   (interactive)
1519   (let (cinfo)
1520     (while (null (setq cinfo
1521                        (get-text-property (point) 'mime-view-entity)))
1522       (backward-char)
1523       )
1524     (let ((r (mime-entity-parent cinfo))
1525           point)
1526       (catch 'tag
1527         (while (setq point (previous-single-property-change
1528                             (point) 'mime-view-entity))
1529           (goto-char point)
1530           (when (eq r (get-text-property (point) 'mime-view-entity))
1531             (if (or (eq mime-preview-move-scroll t)
1532                     (and mime-preview-move-scroll
1533                          (>= point
1534                              (save-excursion
1535                                (move-to-window-line -1)
1536                                (forward-line (* -1 next-screen-context-lines))
1537                                (beginning-of-line)
1538                                (point)))))
1539                 (recenter next-screen-context-lines))
1540             (throw 'tag t)
1541             )
1542           )
1543         (mime-preview-quit)
1544         ))))
1545
1546 (defun mime-preview-move-to-previous ()
1547   "Move to previous entity.
1548 If there is no previous entity, it calls function registered in
1549 variable `mime-preview-over-to-previous-method-alist'."
1550   (interactive)
1551   (while (and (not (bobp))
1552               (null (get-text-property (point) 'mime-view-entity)))
1553     (backward-char)
1554     )
1555   (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1556     (if (and point
1557              (>= point (point-min)))
1558         (if (get-text-property (1- point) 'mime-view-entity)
1559             (progn (goto-char point)
1560                    (if
1561                     (or (eq mime-preview-move-scroll t)
1562                         (and mime-preview-move-scroll
1563                              (<= point
1564                                 (save-excursion
1565                                   (move-to-window-line 0)
1566                                   (forward-line next-screen-context-lines)
1567                                   (end-of-line)
1568                                   (point)))))
1569                         (recenter (* -1 next-screen-context-lines))))
1570           (goto-char (1- point))
1571           (mime-preview-move-to-previous)
1572           )
1573       (let ((f (assq (mime-preview-original-major-mode)
1574                      mime-preview-over-to-previous-method-alist)))
1575         (if f
1576             (funcall (cdr f))
1577           ))
1578       )))
1579
1580 (defun mime-preview-move-to-next ()
1581   "Move to next entity.
1582 If there is no previous entity, it calls function registered in
1583 variable `mime-preview-over-to-next-method-alist'."
1584   (interactive)
1585   (while (and (not (eobp))
1586               (null (get-text-property (point) 'mime-view-entity)))
1587     (forward-char)
1588     )
1589   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1590     (if (and point
1591              (<= point (point-max)))
1592         (progn
1593           (goto-char point)
1594           (if (null (get-text-property point 'mime-view-entity))
1595               (mime-preview-move-to-next)
1596             (and
1597              (or (eq mime-preview-move-scroll t)
1598                  (and mime-preview-move-scroll
1599                       (>= point
1600                          (save-excursion
1601                            (move-to-window-line -1)
1602                            (forward-line
1603                             (* -1 next-screen-context-lines))
1604                            (beginning-of-line)
1605                            (point)))))
1606                  (recenter next-screen-context-lines))
1607             ))
1608       (let ((f (assq (mime-preview-original-major-mode)
1609                      mime-preview-over-to-next-method-alist)))
1610         (if f
1611             (funcall (cdr f))
1612           ))
1613       )))
1614
1615 (defun mime-preview-scroll-up-entity (&optional h)
1616   "Scroll up current entity.
1617 If reached to (point-max), it calls function registered in variable
1618 `mime-preview-over-to-next-method-alist'."
1619   (interactive)
1620   (if (eobp)
1621       (let ((f (assq (mime-preview-original-major-mode)
1622                      mime-preview-over-to-next-method-alist)))
1623         (if f
1624             (funcall (cdr f))
1625           ))
1626     (let ((point
1627            (or (next-single-property-change (point) 'mime-view-entity)
1628                (point-max)))
1629           (bottom (window-end (selected-window))))
1630       (if (and (not h)
1631                (> bottom point))
1632           (progn (goto-char point)
1633                  (recenter next-screen-context-lines))
1634         (condition-case nil
1635             (scroll-up h)
1636           (end-of-buffer
1637            (goto-char (point-max)))))
1638       )))
1639
1640 (defun mime-preview-scroll-down-entity (&optional h)
1641   "Scroll down current entity.
1642 If reached to (point-min), it calls function registered in variable
1643 `mime-preview-over-to-previous-method-alist'."
1644   (interactive)
1645   (if (bobp)
1646       (let ((f (assq (mime-preview-original-major-mode)
1647                      mime-preview-over-to-previous-method-alist)))
1648         (if f
1649             (funcall (cdr f))
1650           ))
1651     (let ((point
1652            (or (previous-single-property-change (point) 'mime-view-entity)
1653                (point-min)))
1654           (top (window-start (selected-window))))
1655       (if (and (not h)
1656                (< top point))
1657           (progn (goto-char point)
1658                  (recenter (* -1 next-screen-context-lines)))
1659         (condition-case nil
1660             (scroll-down h)
1661           (beginning-of-buffer
1662            (goto-char (point-min)))))
1663       )))
1664
1665 (defun mime-preview-next-line-entity (&optional lines)
1666   "Scroll up one line (or prefix LINES lines).
1667 If LINES is negative, scroll down LINES lines."
1668   (interactive "p")
1669   (mime-preview-scroll-up-entity (or lines 1))
1670   )
1671
1672 (defun mime-preview-previous-line-entity (&optional lines)
1673   "Scrroll down one line (or prefix LINES lines).
1674 If LINES is negative, scroll up LINES lines."
1675   (interactive "p")
1676   (mime-preview-scroll-down-entity (or lines 1))
1677   )
1678
1679 ;;; @@ quitting
1680 ;;;
1681
1682 (defun mime-preview-quit ()
1683   "Quit from MIME-preview buffer.
1684 It calls function registered in variable
1685 `mime-preview-quitting-method-alist'."
1686   (interactive)
1687   (let ((r (assq (mime-preview-original-major-mode)
1688                  mime-preview-quitting-method-alist)))
1689     (if r
1690         (funcall (cdr r))
1691       )))
1692
1693 (defun mime-preview-kill-buffer ()
1694   (interactive)
1695   (kill-buffer (current-buffer))
1696   )
1697
1698
1699 ;;; @ end
1700 ;;;
1701
1702 (provide 'mime-view)
1703
1704 (let* ((file mime-acting-situation-examples-file)
1705        (buffer (get-buffer-create " *mime-example*")))
1706   (if (file-readable-p file)
1707       (unwind-protect
1708           (save-excursion
1709             (set-buffer buffer)
1710             (erase-buffer)
1711             (insert-file-contents file)
1712             (eval-buffer)
1713             ;; format check
1714             (condition-case nil
1715                 (let ((i 0))
1716                   (while (and (> (length mime-acting-situation-example-list)
1717                                  mime-acting-situation-example-list-max-size)
1718                               (< i 16))
1719                     (mime-reduce-acting-situation-examples)
1720                     (setq i (1+ i))
1721                     ))
1722               (error (setq mime-acting-situation-example-list nil)))
1723             )
1724         (kill-buffer buffer))))
1725
1726 ;;; mime-view.el ends here