Use with-temp-buffer to load `mime-situation-examples-file'.
[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 'mailcap)
36
37
38 ;;; @ version
39 ;;;
40
41 (defconst mime-view-version
42   (concat (mime-product-name mime-user-interface-product) " MIME-View "
43           (mapconcat #'number-to-string
44                      (mime-product-version mime-user-interface-product) ".")
45           " (" (mime-product-code-name mime-user-interface-product) ")"))
46
47
48 ;;; @ variables
49 ;;;
50
51 (defgroup mime-view nil
52   "MIME view mode"
53   :group 'mime)
54
55 (defcustom mime-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     (mime-insert-text-content entity)
726     (run-hooks 'mime-text-decode-hook)
727     (goto-char (point-max))
728     (if (not (eq (char-after (1- (point))) ?\n))
729         (insert "\n")
730       )
731     (mime-add-url-buttons)
732     (run-hooks 'mime-display-text/plain-hook)
733     ))
734
735 (defun mime-display-text/richtext (entity situation)
736   (save-restriction
737     (narrow-to-region (point-max)(point-max))
738     (mime-insert-text-content entity)
739     (run-hooks 'mime-text-decode-hook)
740     (let ((beg (point-min)))
741       (remove-text-properties beg (point-max) '(face nil))
742       (richtext-decode beg (point-max))
743       )))
744
745 (defun mime-display-text/enriched (entity situation)
746   (save-restriction
747     (narrow-to-region (point-max)(point-max))
748     (mime-insert-text-content entity)
749     (run-hooks 'mime-text-decode-hook)
750     (let ((beg (point-min)))
751       (remove-text-properties beg (point-max) '(face nil))
752       (enriched-decode beg (point-max))
753       )))
754
755
756 (defvar mime-view-announcement-for-message/partial
757   (if (and (>= emacs-major-version 19) window-system)
758       "\
759 \[[ This is message/partial style split message. ]]
760 \[[ Please press `v' key in this buffer          ]]
761 \[[ or click here by mouse button-2.             ]]"
762     "\
763 \[[ This is message/partial style split message. ]]
764 \[[ Please press `v' key in this buffer.         ]]"
765     ))
766
767 (defun mime-display-message/partial-button (&optional entity situation)
768   (save-restriction
769     (goto-char (point-max))
770     (if (not (search-backward "\n\n" nil t))
771         (insert "\n")
772       )
773     (goto-char (point-max))
774     (narrow-to-region (point-max)(point-max))
775     (insert mime-view-announcement-for-message/partial)
776     (mime-add-button (point-min)(point-max)
777                      #'mime-preview-play-current-entity)
778     ))
779
780 (defun mime-display-multipart/mixed (entity situation)
781   (let ((children (mime-entity-children entity))
782         (original-major-mode-cell (assq 'major-mode situation))
783         (default-situation
784           (cdr (assq 'childrens-situation situation))))
785     (if original-major-mode-cell
786         (setq default-situation
787               (cons original-major-mode-cell default-situation)))
788     (while children
789       (mime-display-entity (car children) nil default-situation)
790       (setq children (cdr children))
791       )))
792
793 (defcustom mime-view-type-subtype-score-alist
794   '(((text . enriched) . 3)
795     ((text . richtext) . 2)
796     ((text . plain)    . 1)
797     (t . 0))
798   "Alist MEDIA-TYPE vs corresponding score.
799 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
800   :group 'mime-view
801   :type '(repeat (cons (choice :tag "Media-Type"
802                                (cons :tag "Type/Subtype"
803                                      (symbol :tag "Primary-type")
804                                      (symbol :tag "Subtype"))
805                                (symbol :tag "Type")
806                                (const :tag "Default" t))
807                        integer)))
808
809 (defun mime-display-multipart/alternative (entity situation)
810   (let* ((children (mime-entity-children entity))
811          (original-major-mode-cell (assq 'major-mode situation))
812          (default-situation
813            (cdr (assq 'childrens-situation situation)))
814          (i 0)
815          (p 0)
816          (max-score 0)
817          situations)
818     (if original-major-mode-cell
819         (setq default-situation
820               (cons original-major-mode-cell default-situation)))
821     (setq situations
822           (mapcar (function
823                    (lambda (child)
824                      (let ((situation
825                             (mime-find-entity-preview-situation
826                              child default-situation)))
827                        (if (cdr (assq 'body-presentation-method situation))
828                            (let ((score
829                                   (cdr
830                                    (or (assoc
831                                         (cons
832                                          (cdr (assq 'type situation))
833                                          (cdr (assq 'subtype situation)))
834                                         mime-view-type-subtype-score-alist)
835                                        (assq
836                                         (cdr (assq 'type situation))
837                                         mime-view-type-subtype-score-alist)
838                                        (assq
839                                         t
840                                         mime-view-type-subtype-score-alist)
841                                        ))))
842                              (if (> score max-score)
843                                  (setq p i
844                                        max-score score)
845                                )))
846                        (setq i (1+ i))
847                        situation)
848                      ))
849                   children))
850     (setq i 0)
851     (while children
852       (let ((child (car children))
853             (situation (car situations)))
854         (mime-display-entity child (if (= i p)
855                                        situation
856                                      (put-alist 'body 'invisible
857                                                 (copy-alist situation)))))
858       (setq children (cdr children)
859             situations (cdr situations)
860             i (1+ i)))))
861
862
863 ;;; @ acting-condition
864 ;;;
865
866 (defvar mime-acting-condition nil
867   "Condition-tree about how to process entity.")
868
869 (if (file-readable-p mailcap-file)
870     (let ((entries (mailcap-parse-file)))
871       (while entries
872         (let ((entry (car entries))
873               view print shared)
874           (while entry
875             (let* ((field (car entry))
876                    (field-type (car field)))
877               (cond ((eq field-type 'view)  (setq view field))
878                     ((eq field-type 'print) (setq print field))
879                     ((memq field-type '(compose composetyped edit)))
880                     (t (setq shared (cons field shared))))
881               )
882             (setq entry (cdr entry))
883             )
884           (setq shared (nreverse shared))
885           (ctree-set-calist-with-default
886            'mime-acting-condition
887            (append shared (list '(mode . "play")(cons 'method (cdr view)))))
888           (if print
889               (ctree-set-calist-with-default
890                'mime-acting-condition
891                (append shared
892                        (list '(mode . "print")(cons 'method (cdr view))))
893                ))
894           )
895         (setq entries (cdr entries))
896         )))
897
898 (ctree-set-calist-strictly
899  'mime-acting-condition
900  '((type . application)(subtype . octet-stream)
901    (mode . "play")
902    (method . mime-detect-content)
903    ))
904
905 (ctree-set-calist-with-default
906  'mime-acting-condition
907  '((mode . "extract")
908    (method . mime-save-content)))
909
910 (ctree-set-calist-strictly
911  'mime-acting-condition
912  '((type . text)(subtype . x-rot13-47)(mode . "play")
913    (method . mime-view-caesar)
914    ))
915 (ctree-set-calist-strictly
916  'mime-acting-condition
917  '((type . text)(subtype . x-rot13-47-48)(mode . "play")
918    (method . mime-view-caesar)
919    ))
920
921 (ctree-set-calist-strictly
922  'mime-acting-condition
923  '((type . message)(subtype . rfc822)(mode . "play")
924    (method . mime-view-message/rfc822)
925    ))
926 (ctree-set-calist-strictly
927  'mime-acting-condition
928  '((type . message)(subtype . partial)(mode . "play")
929    (method . mime-store-message/partial-piece)
930    ))
931
932 (ctree-set-calist-strictly
933  'mime-acting-condition
934  '((type . message)(subtype . external-body)
935    ("access-type" . "anon-ftp")
936    (method . mime-view-message/external-anon-ftp)
937    ))
938
939 (ctree-set-calist-strictly
940  'mime-acting-condition
941  '((type . message)(subtype . external-body)
942    ("access-type" . "url")
943    (method . mime-view-message/external-url)
944    ))
945
946 (ctree-set-calist-strictly
947  'mime-acting-condition
948  '((type . application)(subtype . octet-stream)
949    (method . mime-save-content)
950    ))
951
952
953 ;;; @ quitting method
954 ;;;
955
956 (defvar mime-preview-quitting-method-alist
957   '((mime-show-message-mode
958      . mime-preview-quitting-method-for-mime-show-message-mode))
959   "Alist of major-mode vs. quitting-method of mime-view.")
960
961 (defvar mime-preview-over-to-previous-method-alist nil
962   "Alist of major-mode vs. over-to-previous-method of mime-view.")
963
964 (defvar mime-preview-over-to-next-method-alist nil
965   "Alist of major-mode vs. over-to-next-method of mime-view.")
966
967
968 ;;; @ following method
969 ;;;
970
971 (defvar mime-preview-following-method-alist nil
972   "Alist of major-mode vs. following-method of mime-view.")
973
974 (defvar mime-view-following-required-fields-list
975   '("From"))
976
977
978 ;;; @ buffer setup
979 ;;;
980
981 (defun mime-display-entity (entity &optional situation
982                                    default-situation preview-buffer)
983   (or preview-buffer
984       (setq preview-buffer (current-buffer)))
985   (let* (e nb ne nhb nbb)
986     (in-calist-package 'mime-view)
987     (or situation
988         (setq situation
989               (mime-find-entity-preview-situation entity default-situation)))
990     (let ((button-is-invisible
991            (eq (cdr (or (assq '*entity-button situation)
992                         (assq 'entity-button situation)))
993                'invisible))
994           (header-is-visible
995            (eq (cdr (or (assq '*header situation)
996                         (assq 'header situation)))
997                'visible))
998           (body-is-visible
999            (eq (cdr (or (assq '*body situation)
1000                         (assq 'body situation)))
1001                'visible))
1002           (children (mime-entity-children entity)))
1003       (set-buffer preview-buffer)
1004       (setq nb (point))
1005       (narrow-to-region nb nb)
1006       (or button-is-invisible
1007           ;; (if (mime-view-entity-button-visible-p entity)
1008           (mime-view-insert-entity-button entity)
1009           ;;   )
1010           )
1011       (if header-is-visible
1012           (let ((header-presentation-method
1013                  (or (cdr (assq 'header-presentation-method situation))
1014                      (cdr (assq (cdr (assq 'major-mode situation))
1015                                 mime-header-presentation-method-alist)))))
1016             (setq nhb (point))
1017             (if header-presentation-method
1018                 (funcall header-presentation-method entity situation)
1019               (mime-insert-header entity
1020                                   mime-view-ignored-field-list
1021                                   mime-view-visible-field-list))
1022             (run-hooks 'mime-display-header-hook)
1023             (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1024             (goto-char (point-max))
1025             (insert "\n")))
1026       (setq nbb (point))
1027       (unless children
1028         (if body-is-visible
1029             (let ((body-presentation-method
1030                    (cdr (assq 'body-presentation-method situation))))
1031               (if (functionp body-presentation-method)
1032                   (funcall body-presentation-method entity situation)
1033                 (mime-display-text/plain entity situation)))
1034           (when button-is-invisible
1035             (goto-char (point-max))
1036             (mime-view-insert-entity-button entity)
1037             )
1038           (unless header-is-visible
1039             (goto-char (point-max))
1040             (insert "\n"))
1041           ))
1042       (setq ne (point-max))
1043       (widen)
1044       (put-text-property nb ne 'mime-view-entity entity)
1045       (put-text-property nb ne 'mime-view-situation situation)
1046       (put-text-property nbb ne 'mime-view-entity-body entity)
1047       (goto-char ne)
1048       (if (and children body-is-visible)
1049           (let ((body-presentation-method
1050                  (cdr (assq 'body-presentation-method situation))))
1051             (if (functionp body-presentation-method)
1052                 (funcall body-presentation-method entity situation)
1053               (mime-display-multipart/mixed entity situation))))
1054       )))
1055
1056
1057 ;;; @ MIME viewer mode
1058 ;;;
1059
1060 (defconst mime-view-menu-title "MIME-View")
1061 (defconst mime-view-menu-list
1062   '((up          "Move to upper entity"    mime-preview-move-to-upper)
1063     (previous    "Move to previous entity" mime-preview-move-to-previous)
1064     (next        "Move to next entity"     mime-preview-move-to-next)
1065     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
1066     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
1067     (play        "Play current entity"     mime-preview-play-current-entity)
1068     (extract     "Extract current entity"  mime-preview-extract-current-entity)
1069     (print       "Print current entity"    mime-preview-print-current-entity)
1070     )
1071   "Menu for MIME Viewer")
1072
1073 (cond ((featurep 'xemacs)
1074        (defvar mime-view-xemacs-popup-menu
1075          (cons mime-view-menu-title
1076                (mapcar (function
1077                         (lambda (item)
1078                           (vector (nth 1 item)(nth 2 item) t)
1079                           ))
1080                        mime-view-menu-list)))
1081        (defun mime-view-xemacs-popup-menu (event)
1082          "Popup the menu in the MIME Viewer buffer"
1083          (interactive "e")
1084          (select-window (event-window event))
1085          (set-buffer (event-buffer event))
1086          (popup-menu 'mime-view-xemacs-popup-menu))
1087        (defvar mouse-button-2 'button2)
1088        )
1089       (t
1090        (defvar mime-view-popup-menu 
1091          (let ((menu (make-sparse-keymap mime-view-menu-title)))
1092            (nconc menu
1093                   (mapcar (function
1094                            (lambda (item)
1095                              (list (intern (nth 1 item)) 'menu-item 
1096                                    (nth 1 item)(nth 2 item))
1097                              ))
1098                           mime-view-menu-list))))
1099        (defun mime-view-popup-menu (event)
1100          "Popup the menu in the MIME Viewer buffer"
1101          (interactive "@e")
1102          (let ((menu mime-view-popup-menu) events func)
1103            (setq events (x-popup-menu t menu))
1104            (and events
1105                 (setq func (lookup-key menu (apply #'vector events)))
1106                 (commandp func)
1107                 (funcall func))))
1108        (defvar mouse-button-2 [mouse-2])
1109        ))
1110
1111 (defun mime-view-define-keymap (&optional default)
1112   (let ((mime-view-mode-map (if (keymapp default)
1113                                 (copy-keymap default)
1114                               (make-sparse-keymap)
1115                               )))
1116     (define-key mime-view-mode-map
1117       "u"        (function mime-preview-move-to-upper))
1118     (define-key mime-view-mode-map
1119       "p"        (function mime-preview-move-to-previous))
1120     (define-key mime-view-mode-map
1121       "n"        (function mime-preview-move-to-next))
1122     (define-key mime-view-mode-map
1123       "\e\t"     (function mime-preview-move-to-previous))
1124     (define-key mime-view-mode-map
1125       "\t"       (function mime-preview-move-to-next))
1126     (define-key mime-view-mode-map
1127       " "        (function mime-preview-scroll-up-entity))
1128     (define-key mime-view-mode-map
1129       "\M- "     (function mime-preview-scroll-down-entity))
1130     (define-key mime-view-mode-map
1131       "\177"     (function mime-preview-scroll-down-entity))
1132     (define-key mime-view-mode-map
1133       "\C-m"     (function mime-preview-next-line-entity))
1134     (define-key mime-view-mode-map
1135       "\C-\M-m"  (function mime-preview-previous-line-entity))
1136     (define-key mime-view-mode-map
1137       "v"        (function mime-preview-play-current-entity))
1138     (define-key mime-view-mode-map
1139       "e"        (function mime-preview-extract-current-entity))
1140     (define-key mime-view-mode-map
1141       "\C-c\C-p" (function mime-preview-print-current-entity))
1142     (define-key mime-view-mode-map
1143       "\C-c\C-t\C-f" (function mime-preview-toggle-header))
1144     (define-key mime-view-mode-map
1145       "\C-c\C-th" (function mime-preview-toggle-header))
1146     (define-key mime-view-mode-map
1147       "\C-c\C-t\C-c" (function mime-preview-toggle-content))
1148     (define-key mime-view-mode-map
1149       "a"        (function mime-preview-follow-current-entity))
1150     (define-key mime-view-mode-map
1151       "q"        (function mime-preview-quit))
1152     (define-key mime-view-mode-map
1153       "\C-c\C-x" (function mime-preview-kill-buffer))
1154     ;; (define-key mime-view-mode-map
1155     ;;   "<"        (function beginning-of-buffer))
1156     ;; (define-key mime-view-mode-map
1157     ;;   ">"        (function end-of-buffer))
1158     (define-key mime-view-mode-map
1159       "?"        (function describe-mode))
1160     (define-key mime-view-mode-map
1161       [tab] (function mime-preview-move-to-next))
1162     (define-key mime-view-mode-map
1163       [delete] (function mime-preview-scroll-down-entity))
1164     (define-key mime-view-mode-map
1165       [backspace] (function mime-preview-scroll-down-entity))
1166     (if (functionp default)
1167         (cond ((featurep 'xemacs)
1168                (set-keymap-default-binding mime-view-mode-map default)
1169                )
1170               (t
1171                (setq mime-view-mode-map
1172                      (append mime-view-mode-map (list (cons t default))))
1173                )))
1174     (if mouse-button-2
1175         (define-key mime-view-mode-map
1176           mouse-button-2 (function mime-button-dispatcher))
1177       )
1178     (cond ((featurep 'xemacs)
1179            (define-key mime-view-mode-map
1180              mouse-button-3 (function mime-view-xemacs-popup-menu))
1181            )
1182           ((>= emacs-major-version 19)
1183            (define-key mime-view-mode-map
1184              mouse-button-3 (function mime-view-popup-menu))
1185            (define-key mime-view-mode-map [menu-bar mime-view]
1186              (cons mime-view-menu-title
1187                    (make-sparse-keymap mime-view-menu-title)))
1188            (mapcar (function
1189                     (lambda (item)
1190                       (define-key mime-view-mode-map
1191                         (vector 'menu-bar 'mime-view (car item))
1192                         (cons (nth 1 item)(nth 2 item))
1193                         )
1194                       ))
1195                    (reverse mime-view-menu-list)
1196                    )
1197            ))
1198     (use-local-map mime-view-mode-map)
1199     (run-hooks 'mime-view-define-keymap-hook)
1200     ))
1201
1202 (defsubst mime-maybe-hide-echo-buffer ()
1203   "Clear mime-echo buffer and delete window for it."
1204   (let ((buf (get-buffer mime-echo-buffer-name)))
1205     (if buf
1206         (save-excursion
1207           (set-buffer buf)
1208           (erase-buffer)
1209           (let ((win (get-buffer-window buf)))
1210             (if win
1211                 (delete-window win)
1212               ))
1213           (bury-buffer buf)
1214           ))))
1215
1216 (defvar mime-view-redisplay nil)
1217
1218 ;;;###autoload
1219 (defun mime-display-message (message &optional preview-buffer
1220                                      mother default-keymap-or-function
1221                                      original-major-mode)
1222   "View MESSAGE in MIME-View mode.
1223
1224 Optional argument PREVIEW-BUFFER specifies the buffer of the
1225 presentation.  It must be either nil or a name of preview buffer.
1226
1227 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1228
1229 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1230 function.  If it is a keymap, keymap of MIME-View mode will be added
1231 to it.  If it is a function, it will be bound as default binding of
1232 keymap of MIME-View mode."
1233   (mime-maybe-hide-echo-buffer)
1234   (let ((win-conf (current-window-configuration)))
1235     (or preview-buffer
1236         (setq preview-buffer
1237               (concat "*Preview-" (mime-entity-name message) "*")))
1238     (or original-major-mode
1239         (setq original-major-mode major-mode))
1240     (let ((inhibit-read-only t))
1241       (set-buffer (get-buffer-create preview-buffer))
1242       (widen)
1243       (erase-buffer)
1244       (if mother
1245           (setq mime-mother-buffer mother)
1246         )
1247       (setq mime-preview-original-window-configuration win-conf)
1248       (setq major-mode 'mime-view-mode)
1249       (setq mode-name "MIME-View")
1250       (mime-display-entity message nil
1251                            `((entity-button . invisible)
1252                              (header . visible)
1253                              (major-mode . ,original-major-mode))
1254                            preview-buffer)
1255       (mime-view-define-keymap default-keymap-or-function)
1256       (let ((point
1257              (next-single-property-change (point-min) 'mime-view-entity)))
1258         (if point
1259             (goto-char point)
1260           (goto-char (point-min))
1261           (search-forward "\n\n" nil t)
1262           ))
1263       (run-hooks 'mime-view-mode-hook)
1264       (set-buffer-modified-p nil)
1265       (setq buffer-read-only t)
1266       preview-buffer)))
1267
1268 ;;;###autoload
1269 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1270                                    default-keymap-or-function
1271                                    representation-type)
1272   "View RAW-BUFFER in MIME-View mode.
1273 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1274 buffer.
1275 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1276 function.  If it is a keymap, keymap of MIME-View mode will be added
1277 to it.  If it is a function, it will be bound as default binding of
1278 keymap of MIME-View mode.
1279 Optional argument REPRESENTATION-TYPE is representation-type of
1280 message.  It must be nil, `binary' or `cooked'.  If it is nil,
1281 `cooked' is used as default."
1282   (interactive)
1283   (or raw-buffer
1284       (setq raw-buffer (current-buffer)))
1285   (or representation-type
1286       (setq representation-type
1287             (save-excursion
1288               (set-buffer raw-buffer)
1289               (cdr (or (assq major-mode mime-raw-representation-type-alist)
1290                        (assq t mime-raw-representation-type-alist)))
1291               )))
1292   (if (eq representation-type 'binary)
1293       (setq representation-type 'buffer)
1294     )
1295   (setq preview-buffer (mime-display-message
1296                         (mime-open-entity representation-type raw-buffer)
1297                         preview-buffer mother default-keymap-or-function))
1298   (or (get-buffer-window preview-buffer)
1299       (let ((r-win (get-buffer-window raw-buffer)))
1300         (if r-win
1301             (set-window-buffer r-win preview-buffer)
1302           (let ((m-win (and mother (get-buffer-window mother))))
1303             (if m-win
1304                 (set-window-buffer m-win preview-buffer)
1305               (switch-to-buffer preview-buffer)
1306               ))))))
1307
1308 (defun mime-view-mode (&optional mother ctl encoding
1309                                  raw-buffer preview-buffer
1310                                  default-keymap-or-function)
1311   "Major mode for viewing MIME message.
1312
1313 Here is a list of the standard keys for mime-view-mode.
1314
1315 key             feature
1316 ---             -------
1317
1318 u               Move to upper content
1319 p or M-TAB      Move to previous content
1320 n or TAB        Move to next content
1321 SPC             Scroll up or move to next content
1322 M-SPC or DEL    Scroll down or move to previous content
1323 RET             Move to next line
1324 M-RET           Move to previous line
1325 v               Decode current content as `play mode'
1326 e               Decode current content as `extract mode'
1327 C-c C-p         Decode current content as `print mode'
1328 a               Followup to current content.
1329 q               Quit
1330 button-2        Move to point under the mouse cursor
1331                 and decode current content as `play mode'
1332 "
1333   (interactive)
1334   (unless mime-view-redisplay
1335     (save-excursion
1336       (if raw-buffer (set-buffer raw-buffer))
1337       (let ((type
1338              (cdr
1339               (or (assq major-mode mime-raw-representation-type-alist)
1340                   (assq t mime-raw-representation-type-alist)))))
1341         (if (eq type 'binary)
1342             (setq type 'buffer)
1343           )
1344         (setq mime-message-structure (mime-open-entity type raw-buffer))
1345         (or (mime-entity-content-type mime-message-structure)
1346             (mime-entity-set-content-type-internal
1347              mime-message-structure ctl))
1348         )
1349       (or (mime-entity-encoding mime-message-structure)
1350           (mime-entity-set-encoding-internal mime-message-structure encoding))
1351       ))
1352   (mime-display-message mime-message-structure preview-buffer
1353                         mother default-keymap-or-function)
1354   )
1355
1356
1357 ;;; @@ utility
1358 ;;;
1359
1360 (defun mime-preview-find-boundary-info (&optional get-mother)
1361   (let (entity
1362         p-beg p-end
1363         entity-node-id len)
1364     (while (null (setq entity
1365                        (get-text-property (point) 'mime-view-entity)))
1366       (backward-char))
1367     (setq p-beg (previous-single-property-change (point) 'mime-view-entity))
1368     (setq entity-node-id (mime-entity-node-id entity))
1369     (setq len (length entity-node-id))
1370     (cond ((null p-beg)
1371            (setq p-beg
1372                  (if (eq (next-single-property-change (point-min)
1373                                                       'mime-view-entity)
1374                          (point))
1375                      (point)
1376                    (point-min)))
1377            )
1378           ((eq (next-single-property-change p-beg 'mime-view-entity)
1379                (point))
1380            (setq p-beg (point))
1381            ))
1382     (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1383     (cond ((null p-end)
1384            (setq p-end (point-max))
1385            )
1386           ((null entity-node-id)
1387            (setq p-end (point-max))
1388            )
1389           (get-mother
1390            (save-excursion
1391              (goto-char p-end)
1392              (catch 'tag
1393                (let (e i)
1394                  (while (setq e
1395                               (next-single-property-change
1396                                (point) 'mime-view-entity))
1397                    (goto-char e)
1398                    (let ((rc (mime-entity-node-id
1399                               (get-text-property (1- (point))
1400                                                  'mime-view-entity))))
1401                      (or (and (>= (setq i (- (length rc) len)) 0)
1402                               (equal entity-node-id (nthcdr i rc)))
1403                          (throw 'tag nil)))
1404                    (setq p-end e)))
1405                (setq p-end (point-max))))
1406            ))
1407     (vector p-beg p-end entity)))
1408
1409
1410 ;;; @@ playing
1411 ;;;
1412
1413 (autoload 'mime-preview-play-current-entity "mime-play"
1414   "Play current entity." t)
1415
1416 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1417   "Extract current entity into file (maybe).
1418 It decodes current entity to call internal or external method as
1419 \"extract\" mode.  The method is selected from variable
1420 `mime-acting-condition'."
1421   (interactive "P")
1422   (mime-preview-play-current-entity ignore-examples "extract")
1423   )
1424
1425 (defun mime-preview-print-current-entity (&optional ignore-examples)
1426   "Print current entity (maybe).
1427 It decodes current entity to call internal or external method as
1428 \"print\" mode.  The method is selected from variable
1429 `mime-acting-condition'."
1430   (interactive "P")
1431   (mime-preview-play-current-entity ignore-examples "print")
1432   )
1433
1434
1435 ;;; @@ following
1436 ;;;
1437
1438 (defun mime-preview-follow-current-entity ()
1439   "Write follow message to current entity.
1440 It calls following-method selected from variable
1441 `mime-preview-following-method-alist'."
1442   (interactive)
1443   (let ((entity (mime-preview-find-boundary-info t))
1444         p-beg p-end
1445         pb-beg)
1446     (setq p-beg (aref entity 0)
1447           p-end (aref entity 1)
1448           entity (aref entity 2))
1449     (if (get-text-property p-beg 'mime-view-entity-body)
1450         (setq pb-beg p-beg)
1451       (setq pb-beg
1452             (next-single-property-change
1453              p-beg 'mime-view-entity-body nil
1454              (or (next-single-property-change p-beg 'mime-view-entity)
1455                  p-end))))
1456     (let* ((mode (mime-preview-original-major-mode 'recursive))
1457            (entity-node-id (mime-entity-node-id entity))
1458            (new-name
1459             (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1460            new-buf
1461            (the-buf (current-buffer))
1462            fields)
1463       (save-excursion
1464         (set-buffer (setq new-buf (get-buffer-create new-name)))
1465         (erase-buffer)
1466         (insert ?\n)
1467         (insert-buffer-substring the-buf pb-beg p-end)
1468         (goto-char (point-min))
1469         (let ((current-entity
1470                (if (and (eq (mime-entity-media-type entity) 'message)
1471                         (eq (mime-entity-media-subtype entity) 'rfc822))
1472                    (car (mime-entity-children entity))
1473                  entity)))
1474           (while (and current-entity
1475                       (if (and (eq (mime-entity-media-type
1476                                     current-entity) 'message)
1477                                (eq (mime-entity-media-subtype
1478                                     current-entity) 'rfc822))
1479                           nil
1480                         (mime-insert-header current-entity fields)
1481                         t))
1482             (setq fields (std11-collect-field-names)
1483                   current-entity (mime-entity-parent current-entity))
1484             ))
1485         (let ((rest mime-view-following-required-fields-list)
1486               field-name ret)
1487           (while rest
1488             (setq field-name (car rest))
1489             (or (std11-field-body field-name)
1490                 (progn
1491                   (save-excursion
1492                     (set-buffer the-buf)
1493                     (let ((entity (when mime-mother-buffer
1494                                     (set-buffer mime-mother-buffer)
1495                                     (get-text-property (point)
1496                                                        'mime-view-entity))))
1497                       (while (and entity
1498                                   (null (setq ret (mime-entity-fetch-field
1499                                                    entity field-name))))
1500                         (setq entity (mime-entity-parent entity)))))
1501                   (if ret
1502                       (insert (concat field-name ": " ret "\n"))
1503                     )))
1504             (setq rest (cdr rest))
1505             ))
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
1686 ;;; @@ display
1687 ;;;
1688
1689 (defun mime-preview-toggle-header ()
1690   (interactive)
1691   (let ((situation (mime-preview-find-boundary-info))
1692         entity p-beg p-end)
1693     (setq p-beg (aref situation 0)
1694           p-end (aref situation 1)
1695           entity (aref situation 2)
1696           situation (get-text-property p-beg 'mime-view-situation))
1697     (let ((cell (assq '*header situation)))
1698       (if (null cell)
1699           (setq cell (assq 'header situation)))
1700       (if (eq (cdr cell) 'visible)
1701           (setq situation (put-alist '*header 'invisible situation))
1702         (setq situation (put-alist '*header 'visible situation))))
1703     (save-excursion
1704       (let ((inhibit-read-only t))
1705         (delete-region p-beg p-end)
1706         (mime-display-entity entity situation)))
1707     (let ((ret (assoc situation mime-preview-situation-example-list)))
1708       (if ret
1709           (setcdr ret (1+ (cdr ret)))
1710         (add-to-list 'mime-preview-situation-example-list
1711                      (cons situation 0))))))
1712
1713 (defun mime-preview-toggle-content ()
1714   (interactive)
1715   (let ((situation (mime-preview-find-boundary-info))
1716         entity p-beg p-end)
1717     (setq p-beg (aref situation 0)
1718           p-end (aref situation 1)
1719           entity (aref situation 2)
1720           situation (get-text-property p-beg 'mime-view-situation))
1721     (let ((cell (assq '*body situation)))
1722       (if (null cell)
1723           (setq cell (assq 'body situation)))
1724       (if (eq (cdr cell) 'visible)
1725           (setq situation (put-alist '*body 'invisible situation))
1726         (setq situation (put-alist '*body 'visible situation))))
1727     (save-excursion
1728       (let ((inhibit-read-only t))
1729         (delete-region p-beg p-end)
1730         (mime-display-entity entity situation)
1731         ))
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     
1739 ;;; @@ quitting
1740 ;;;
1741
1742 (defun mime-preview-quit ()
1743   "Quit from MIME-preview buffer.
1744 It calls function registered in variable
1745 `mime-preview-quitting-method-alist'."
1746   (interactive)
1747   (let ((r (assq (mime-preview-original-major-mode)
1748                  mime-preview-quitting-method-alist)))
1749     (if r
1750         (funcall (cdr r))
1751       )))
1752
1753 (defun mime-preview-kill-buffer ()
1754   (interactive)
1755   (kill-buffer (current-buffer))
1756   )
1757
1758
1759 ;;; @ end
1760 ;;;
1761
1762 (provide 'mime-view)
1763
1764 (let ((file mime-situation-examples-file))
1765   (if (file-readable-p file)
1766       (with-temp-buffer
1767         (insert-file-contents file)
1768         (setq mime-situation-examples-file-coding-system
1769               (static-cond
1770                ((boundp 'buffer-file-coding-system)
1771                 (symbol-value 'buffer-file-coding-system))
1772                ((boundp 'file-coding-system)
1773                 (symbol-value 'file-coding-system))
1774                (t nil)))
1775         (eval-buffer)
1776         ;; format check
1777         (condition-case nil
1778             (let ((i 0))
1779               (while (and (> (length mime-preview-situation-example-list)
1780                              mime-preview-situation-example-list-max-size)
1781                           (< i 16))
1782                 (setq mime-preview-situation-example-list
1783                       (mime-reduce-situation-examples
1784                        mime-preview-situation-example-list))
1785                 (setq i (1+ i))))
1786           (error (setq mime-preview-situation-example-list nil)))
1787         ;; (let ((rest mime-preview-situation-example-list))
1788         ;;   (while rest
1789         ;;     (ctree-set-calist-strictly 'mime-preview-condition
1790         ;;                                (caar rest))
1791         ;;     (setq rest (cdr rest))))
1792         (condition-case nil
1793             (let ((i 0))
1794               (while (and (> (length mime-acting-situation-example-list)
1795                              mime-acting-situation-example-list-max-size)
1796                           (< i 16))
1797                 (setq mime-acting-situation-example-list
1798                       (mime-reduce-situation-examples
1799                        mime-acting-situation-example-list))
1800                 (setq i (1+ i))))
1801           (error (setq mime-acting-situation-example-list nil))))))
1802
1803 ;;; mime-view.el ends here