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