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