* mime-view.el: Add setting for 'vcard-parse-string',
[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 (autoload 'fill-flowed "flow-fill")
653
654 (ctree-set-calist-strictly
655  'mime-preview-condition
656  '((type . nil)
657    (body . visible)
658    (body-presentation-method . mime-display-text/plain)))
659
660 (ctree-set-calist-strictly
661  'mime-preview-condition
662  '((type . text)(subtype . enriched)
663    (body . visible)
664    (body-presentation-method . mime-display-text/enriched)))
665
666 (ctree-set-calist-strictly
667  'mime-preview-condition
668  '((type . text)(subtype . richtext)
669    (body . visible)
670    (body-presentation-method . mime-display-text/richtext)))
671
672 (autoload 'vcard-parse-string "vcard")
673 (autoload 'vcard-format-string "vcard")
674
675 (ctree-set-calist-strictly
676  'mime-preview-condition
677  '((type . text)(subtype . x-vcard)
678    (body . visible)
679    (body-presentation-method . mime-display-text/x-vcard)))
680
681 (autoload 'mime-display-application/x-postpet "postpet")
682
683 (ctree-set-calist-strictly
684  'mime-preview-condition
685  '((type . application)(subtype . x-postpet)
686    (body . visible)
687    (body-presentation-method . mime-display-application/x-postpet)))
688
689 (ctree-set-calist-strictly
690  'mime-preview-condition
691  '((type . text)(subtype . t)
692    (body . visible)
693    (body-presentation-method . mime-display-text/plain)))
694
695 (ctree-set-calist-strictly
696  'mime-preview-condition
697  '((type . multipart)(subtype . alternative)
698    (body . visible)
699    (body-presentation-method . mime-display-multipart/alternative)))
700
701 (ctree-set-calist-strictly
702  'mime-preview-condition
703  '((type . multipart)(subtype . t)
704    (body . visible)
705    (body-presentation-method . mime-display-multipart/mixed)))
706
707 (ctree-set-calist-strictly
708  'mime-preview-condition
709  '((type . message)(subtype . partial)
710    (body . visible)
711    (body-presentation-method . mime-display-message/partial-button)))
712
713 (ctree-set-calist-strictly
714  'mime-preview-condition
715  '((type . message)(subtype . rfc822)
716    (body . visible)
717    (body-presentation-method . mime-display-multipart/mixed)
718    (childrens-situation (header . visible)
719                         (entity-button . invisible))))
720
721 (ctree-set-calist-strictly
722  'mime-preview-condition
723  '((type . message)(subtype . news)
724    (body . visible)
725    (body-presentation-method . mime-display-multipart/mixed)
726    (childrens-situation (header . visible)
727                         (entity-button . invisible))))
728
729
730 ;;; @@@ entity presentation
731 ;;;
732
733 (defun mime-display-text/plain (entity situation)
734   (save-restriction
735     (narrow-to-region (point-max)(point-max))
736     (condition-case nil
737         (mime-insert-text-content entity)
738       (error (progn
739                (message "Can't decode current entity.")
740                (sit-for 1))))
741     (run-hooks 'mime-text-decode-hook)
742     (goto-char (point-max))
743     (if (not (eq (char-after (1- (point))) ?\n))
744         (insert "\n")
745       )
746     (if (equal (cdr (assoc "format" situation)) "flowed")
747         (fill-flowed))
748     (mime-add-url-buttons)
749     (run-hooks 'mime-display-text/plain-hook)
750     ))
751
752 (defun mime-display-text/richtext (entity situation)
753   (save-restriction
754     (narrow-to-region (point-max)(point-max))
755     (mime-insert-text-content entity)
756     (run-hooks 'mime-text-decode-hook)
757     (let ((beg (point-min)))
758       (remove-text-properties beg (point-max) '(face nil))
759       (richtext-decode beg (point-max))
760       )))
761
762 (defun mime-display-text/enriched (entity situation)
763   (save-restriction
764     (narrow-to-region (point-max)(point-max))
765     (mime-insert-text-content entity)
766     (run-hooks 'mime-text-decode-hook)
767     (let ((beg (point-min)))
768       (remove-text-properties beg (point-max) '(face nil))
769       (enriched-decode beg (point-max))
770       )))
771
772 (defun mime-display-text/x-vcard (entity situation)
773   (save-restriction
774     (narrow-to-region (point-max)(point-max))
775     (insert
776      (string-as-multibyte
777       (vcard-format-string
778        (vcard-parse-string
779         (mime-entity-content entity)
780         #'vcard-standard-filter))))
781     (if (not (eq (char-after (1- (point))) ?\n))
782         (insert "\n"))
783     (mime-add-url-buttons)
784     (run-hooks 'mime-display-text/x-vcard-hook)))
785
786 (defvar mime-view-announcement-for-message/partial
787   (if (and (>= emacs-major-version 19) window-system)
788       "\
789 \[[ This is message/partial style split message. ]]
790 \[[ Please press `v' key in this buffer          ]]
791 \[[ or click here by mouse button-2.             ]]"
792     "\
793 \[[ This is message/partial style split message. ]]
794 \[[ Please press `v' key in this buffer.         ]]"
795     ))
796
797 (defun mime-display-message/partial-button (&optional entity situation)
798   (save-restriction
799     (goto-char (point-max))
800     (if (not (search-backward "\n\n" nil t))
801         (insert "\n")
802       )
803     (goto-char (point-max))
804     (narrow-to-region (point-max)(point-max))
805     (insert mime-view-announcement-for-message/partial)
806     (mime-add-button (point-min)(point-max)
807                      #'mime-preview-play-current-entity)
808     ))
809
810 (defun mime-display-multipart/mixed (entity situation)
811   (let ((children (mime-entity-children entity))
812         (original-major-mode-cell (assq 'major-mode situation))
813         (default-situation
814           (cdr (assq 'childrens-situation situation))))
815     (if original-major-mode-cell
816         (setq default-situation
817               (cons original-major-mode-cell default-situation)))
818     (while children
819       (mime-display-entity (car children) nil default-situation)
820       (setq children (cdr children))
821       )))
822
823 (defcustom mime-view-type-subtype-score-alist
824   '(((text . enriched) . 3)
825     ((text . richtext) . 2)
826     ((text . plain)    . 1)
827     (t . 0))
828   "Alist MEDIA-TYPE vs corresponding score.
829 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
830   :group 'mime-view
831   :type '(repeat (cons (choice :tag "Media-Type"
832                                (cons :tag "Type/Subtype"
833                                      (symbol :tag "Primary-type")
834                                      (symbol :tag "Subtype"))
835                                (symbol :tag "Type")
836                                (const :tag "Default" t))
837                        integer)))
838
839 (defun mime-display-multipart/alternative (entity situation)
840   (let* ((children (mime-entity-children entity))
841          (original-major-mode-cell (assq 'major-mode situation))
842          (default-situation
843            (cdr (assq 'childrens-situation situation)))
844          (i 0)
845          (p 0)
846          (max-score 0)
847          situations)
848     (if original-major-mode-cell
849         (setq default-situation
850               (cons original-major-mode-cell default-situation)))
851     (setq situations
852           (mapcar (function
853                    (lambda (child)
854                      (let ((situation
855                             (mime-find-entity-preview-situation
856                              child default-situation)))
857                        (if (cdr (assq 'body-presentation-method situation))
858                            (let ((score
859                                   (cdr
860                                    (or (assoc
861                                         (cons
862                                          (cdr (assq 'type situation))
863                                          (cdr (assq 'subtype situation)))
864                                         mime-view-type-subtype-score-alist)
865                                        (assq
866                                         (cdr (assq 'type situation))
867                                         mime-view-type-subtype-score-alist)
868                                        (assq
869                                         t
870                                         mime-view-type-subtype-score-alist)
871                                        ))))
872                              (if (> score max-score)
873                                  (setq p i
874                                        max-score score)
875                                )))
876                        (setq i (1+ i))
877                        situation)
878                      ))
879                   children))
880     (setq i 0)
881     (while children
882       (let ((child (car children))
883             (situation (car situations)))
884         (mime-display-entity child (if (= i p)
885                                        situation
886                                      (put-alist 'body 'invisible
887                                                 (copy-alist situation)))))
888       (setq children (cdr children)
889             situations (cdr situations)
890             i (1+ i)))))
891
892
893 ;;; @ acting-condition
894 ;;;
895
896 (defvar mime-acting-condition nil
897   "Condition-tree about how to process entity.")
898
899 (if (file-readable-p mailcap-file)
900     (let ((entries (mailcap-parse-file)))
901       (while entries
902         (let ((entry (car entries))
903               view print shared)
904           (while entry
905             (let* ((field (car entry))
906                    (field-type (car field)))
907               (cond ((eq field-type 'view)  (setq view field))
908                     ((eq field-type 'print) (setq print field))
909                     ((memq field-type '(compose composetyped edit)))
910                     (t (setq shared (cons field shared))))
911               )
912             (setq entry (cdr entry))
913             )
914           (setq shared (nreverse shared))
915           (ctree-set-calist-with-default
916            'mime-acting-condition
917            (append shared (list '(mode . "play")(cons 'method (cdr view)))))
918           (if print
919               (ctree-set-calist-with-default
920                'mime-acting-condition
921                (append shared
922                        (list '(mode . "print")(cons 'method (cdr view))))
923                ))
924           )
925         (setq entries (cdr entries))
926         )))
927
928 (ctree-set-calist-strictly
929  'mime-acting-condition
930  '((type . application)(subtype . octet-stream)
931    (mode . "play")
932    (method . mime-detect-content)
933    ))
934
935 (ctree-set-calist-with-default
936  'mime-acting-condition
937  '((mode . "extract")
938    (method . mime-save-content)))
939
940 (ctree-set-calist-strictly
941  'mime-acting-condition
942  '((type . text)(subtype . x-rot13-47)(mode . "play")
943    (method . mime-view-caesar)
944    ))
945 (ctree-set-calist-strictly
946  'mime-acting-condition
947  '((type . text)(subtype . x-rot13-47-48)(mode . "play")
948    (method . mime-view-caesar)
949    ))
950
951 (ctree-set-calist-strictly
952  'mime-acting-condition
953  '((type . message)(subtype . rfc822)(mode . "play")
954    (method . mime-view-message/rfc822)
955    ))
956 (ctree-set-calist-strictly
957  'mime-acting-condition
958  '((type . message)(subtype . partial)(mode . "play")
959    (method . mime-store-message/partial-piece)
960    ))
961
962 (ctree-set-calist-strictly
963  'mime-acting-condition
964  '((type . message)(subtype . external-body)
965    ("access-type" . "anon-ftp")
966    (method . mime-view-message/external-anon-ftp)
967    ))
968
969 (ctree-set-calist-strictly
970  'mime-acting-condition
971  '((type . message)(subtype . external-body)
972    ("access-type" . "url")
973    (method . mime-view-message/external-url)
974    ))
975
976 (ctree-set-calist-strictly
977  'mime-acting-condition
978  '((type . application)(subtype . octet-stream)
979    (method . mime-save-content)
980    ))
981
982
983 ;;; @ quitting method
984 ;;;
985
986 (defvar mime-preview-quitting-method-alist
987   '((mime-show-message-mode
988      . mime-preview-quitting-method-for-mime-show-message-mode))
989   "Alist of major-mode vs. quitting-method of mime-view.")
990
991 (defvar mime-preview-over-to-previous-method-alist nil
992   "Alist of major-mode vs. over-to-previous-method of mime-view.")
993
994 (defvar mime-preview-over-to-next-method-alist nil
995   "Alist of major-mode vs. over-to-next-method of mime-view.")
996
997
998 ;;; @ following method
999 ;;;
1000
1001 (defvar mime-preview-following-method-alist nil
1002   "Alist of major-mode vs. following-method of mime-view.")
1003
1004 (defvar mime-view-following-required-fields-list
1005   '("From"))
1006
1007
1008 ;;; @ buffer setup
1009 ;;;
1010
1011 (defun mime-display-entity (entity &optional situation
1012                                    default-situation preview-buffer)
1013   (or preview-buffer
1014       (setq preview-buffer (current-buffer)))
1015   (let* (e nb ne nhb nbb)
1016     (in-calist-package 'mime-view)
1017     (or situation
1018         (setq situation
1019               (mime-find-entity-preview-situation entity default-situation)))
1020     (let ((button-is-invisible
1021            (eq (cdr (or (assq '*entity-button situation)
1022                         (assq 'entity-button situation)))
1023                'invisible))
1024           (header-is-visible
1025            (eq (cdr (or (assq '*header situation)
1026                         (assq 'header situation)))
1027                'visible))
1028           (body-is-visible
1029            (eq (cdr (or (assq '*body situation)
1030                         (assq 'body situation)))
1031                'visible))
1032           (children (mime-entity-children entity)))
1033       (set-buffer preview-buffer)
1034       (setq nb (point))
1035       (narrow-to-region nb nb)
1036       (or button-is-invisible
1037           ;; (if (mime-view-entity-button-visible-p entity)
1038           (mime-view-insert-entity-button entity)
1039           ;;   )
1040           )
1041       (if header-is-visible
1042           (let ((header-presentation-method
1043                  (or (cdr (assq 'header-presentation-method situation))
1044                      (cdr (assq (cdr (assq 'major-mode situation))
1045                                 mime-header-presentation-method-alist)))))
1046             (setq nhb (point))
1047             (if header-presentation-method
1048                 (funcall header-presentation-method entity situation)
1049               (mime-insert-header entity
1050                                   mime-view-ignored-field-list
1051                                   mime-view-visible-field-list))
1052             (run-hooks 'mime-display-header-hook)
1053             (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1054             (goto-char (point-max))
1055             (insert "\n")))
1056       (setq nbb (point))
1057       (unless children
1058         (if body-is-visible
1059             (let ((body-presentation-method
1060                    (cdr (assq 'body-presentation-method situation))))
1061               (if (functionp body-presentation-method)
1062                   (funcall body-presentation-method entity situation)
1063                 (mime-display-text/plain entity situation)))
1064           (when button-is-invisible
1065             (goto-char (point-max))
1066             (mime-view-insert-entity-button entity)
1067             )
1068           (unless header-is-visible
1069             (goto-char (point-max))
1070             (insert "\n"))
1071           ))
1072       (setq ne (point-max))
1073       (widen)
1074       (put-text-property nb ne 'mime-view-entity entity)
1075       (put-text-property nb ne 'mime-view-situation situation)
1076       (put-text-property nbb ne 'mime-view-entity-body entity)
1077       (goto-char ne)
1078       (if (and children body-is-visible)
1079           (let ((body-presentation-method
1080                  (cdr (assq 'body-presentation-method situation))))
1081             (if (functionp body-presentation-method)
1082                 (funcall body-presentation-method entity situation)
1083               (mime-display-multipart/mixed entity situation))))
1084       )))
1085
1086
1087 ;;; @ MIME viewer mode
1088 ;;;
1089
1090 (defconst mime-view-menu-title "MIME-View")
1091 (defconst mime-view-menu-list
1092   '((up          "Move to upper entity"    mime-preview-move-to-upper)
1093     (previous    "Move to previous entity" mime-preview-move-to-previous)
1094     (next        "Move to next entity"     mime-preview-move-to-next)
1095     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
1096     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
1097     (play        "Play current entity"     mime-preview-play-current-entity)
1098     (extract     "Extract current entity"  mime-preview-extract-current-entity)
1099     (print       "Print current entity"    mime-preview-print-current-entity)
1100     )
1101   "Menu for MIME Viewer")
1102
1103 (cond ((featurep 'xemacs)
1104        (defvar mime-view-xemacs-popup-menu
1105          (cons mime-view-menu-title
1106                (mapcar (function
1107                         (lambda (item)
1108                           (vector (nth 1 item)(nth 2 item) t)
1109                           ))
1110                        mime-view-menu-list)))
1111        (defun mime-view-xemacs-popup-menu (event)
1112          "Popup the menu in the MIME Viewer buffer"
1113          (interactive "e")
1114          (select-window (event-window event))
1115          (set-buffer (event-buffer event))
1116          (popup-menu 'mime-view-xemacs-popup-menu))
1117        (defvar mouse-button-2 'button2)
1118        )
1119       (t
1120        (defvar mime-view-popup-menu 
1121          (let ((menu (make-sparse-keymap mime-view-menu-title)))
1122            (nconc menu
1123                   (mapcar (function
1124                            (lambda (item)
1125                              (list (intern (nth 1 item)) 'menu-item 
1126                                    (nth 1 item)(nth 2 item))
1127                              ))
1128                           mime-view-menu-list))))
1129        (defun mime-view-popup-menu (event)
1130          "Popup the menu in the MIME Viewer buffer"
1131          (interactive "@e")
1132          (let ((menu mime-view-popup-menu) events func)
1133            (setq events (x-popup-menu t menu))
1134            (and events
1135                 (setq func (lookup-key menu (apply #'vector events)))
1136                 (commandp func)
1137                 (funcall func))))
1138        (defvar mouse-button-2 [mouse-2])
1139        ))
1140
1141 (defun mime-view-define-keymap (&optional default)
1142   (let ((mime-view-mode-map (if (keymapp default)
1143                                 (copy-keymap default)
1144                               (make-sparse-keymap)
1145                               )))
1146     (define-key mime-view-mode-map
1147       "u"        (function mime-preview-move-to-upper))
1148     (define-key mime-view-mode-map
1149       "p"        (function mime-preview-move-to-previous))
1150     (define-key mime-view-mode-map
1151       "n"        (function mime-preview-move-to-next))
1152     (define-key mime-view-mode-map
1153       "\e\t"     (function mime-preview-move-to-previous))
1154     (define-key mime-view-mode-map
1155       "\t"       (function mime-preview-move-to-next))
1156     (define-key mime-view-mode-map
1157       " "        (function mime-preview-scroll-up-entity))
1158     (define-key mime-view-mode-map
1159       "\M- "     (function mime-preview-scroll-down-entity))
1160     (define-key mime-view-mode-map
1161       "\177"     (function mime-preview-scroll-down-entity))
1162     (define-key mime-view-mode-map
1163       "\C-m"     (function mime-preview-next-line-entity))
1164     (define-key mime-view-mode-map
1165       "\C-\M-m"  (function mime-preview-previous-line-entity))
1166     (define-key mime-view-mode-map
1167       "v"        (function mime-preview-play-current-entity))
1168     (define-key mime-view-mode-map
1169       "e"        (function mime-preview-extract-current-entity))
1170     (define-key mime-view-mode-map
1171       "\C-c\C-p" (function mime-preview-print-current-entity))
1172
1173     (define-key mime-view-mode-map
1174       "\C-c\C-t\C-f" (function mime-preview-toggle-header))
1175     (define-key mime-view-mode-map
1176       "\C-c\C-th" (function mime-preview-toggle-header))
1177     (define-key mime-view-mode-map
1178       "\C-c\C-t\C-c" (function mime-preview-toggle-content))
1179
1180     (define-key mime-view-mode-map
1181       "\C-c\C-v\C-f" (function mime-preview-show-header))
1182     (define-key mime-view-mode-map
1183       "\C-c\C-vh" (function mime-preview-show-header))
1184     (define-key mime-view-mode-map
1185       "\C-c\C-v\C-c" (function mime-preview-show-content))
1186
1187     (define-key mime-view-mode-map
1188       "\C-c\C-d\C-f" (function mime-preview-hide-header))
1189     (define-key mime-view-mode-map
1190       "\C-c\C-dh" (function mime-preview-hide-header))
1191     (define-key mime-view-mode-map
1192       "\C-c\C-d\C-c" (function mime-preview-hide-content))
1193
1194     (define-key mime-view-mode-map
1195       "a"        (function mime-preview-follow-current-entity))
1196     (define-key mime-view-mode-map
1197       "q"        (function mime-preview-quit))
1198     (define-key mime-view-mode-map
1199       "\C-c\C-x" (function mime-preview-kill-buffer))
1200     ;; (define-key mime-view-mode-map
1201     ;;   "<"        (function beginning-of-buffer))
1202     ;; (define-key mime-view-mode-map
1203     ;;   ">"        (function end-of-buffer))
1204     (define-key mime-view-mode-map
1205       "?"        (function describe-mode))
1206     (define-key mime-view-mode-map
1207       [tab] (function mime-preview-move-to-next))
1208     (define-key mime-view-mode-map
1209       [delete] (function mime-preview-scroll-down-entity))
1210     (define-key mime-view-mode-map
1211       [backspace] (function mime-preview-scroll-down-entity))
1212     (if (functionp default)
1213         (cond ((featurep 'xemacs)
1214                (set-keymap-default-binding mime-view-mode-map default)
1215                )
1216               (t
1217                (setq mime-view-mode-map
1218                      (append mime-view-mode-map (list (cons t default))))
1219                )))
1220     (if mouse-button-2
1221         (define-key mime-view-mode-map
1222           mouse-button-2 (function mime-button-dispatcher))
1223       )
1224     (cond ((featurep 'xemacs)
1225            (define-key mime-view-mode-map
1226              mouse-button-3 (function mime-view-xemacs-popup-menu))
1227            )
1228           ((>= emacs-major-version 19)
1229            (define-key mime-view-mode-map
1230              mouse-button-3 (function mime-view-popup-menu))
1231            (define-key mime-view-mode-map [menu-bar mime-view]
1232              (cons mime-view-menu-title
1233                    (make-sparse-keymap mime-view-menu-title)))
1234            (mapcar (function
1235                     (lambda (item)
1236                       (define-key mime-view-mode-map
1237                         (vector 'menu-bar 'mime-view (car item))
1238                         (cons (nth 1 item)(nth 2 item))
1239                         )
1240                       ))
1241                    (reverse mime-view-menu-list)
1242                    )
1243            ))
1244     (use-local-map mime-view-mode-map)
1245     (run-hooks 'mime-view-define-keymap-hook)
1246     ))
1247
1248 (defsubst mime-maybe-hide-echo-buffer ()
1249   "Clear mime-echo buffer and delete window for it."
1250   (let ((buf (get-buffer mime-echo-buffer-name)))
1251     (if buf
1252         (save-excursion
1253           (set-buffer buf)
1254           (erase-buffer)
1255           (let ((win (get-buffer-window buf)))
1256             (if win
1257                 (delete-window win)
1258               ))
1259           (bury-buffer buf)
1260           ))))
1261
1262 (defvar mime-view-redisplay nil)
1263
1264 ;;;###autoload
1265 (defun mime-display-message (message &optional preview-buffer
1266                                      mother default-keymap-or-function
1267                                      original-major-mode)
1268   "View MESSAGE in MIME-View mode.
1269
1270 Optional argument PREVIEW-BUFFER specifies the buffer of the
1271 presentation.  It must be either nil or a name of preview buffer.
1272
1273 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1274
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   (mime-maybe-hide-echo-buffer)
1280   (let ((win-conf (current-window-configuration)))
1281     (or preview-buffer
1282         (setq preview-buffer
1283               (concat "*Preview-" (mime-entity-name message) "*")))
1284     (or original-major-mode
1285         (setq original-major-mode major-mode))
1286     (let ((inhibit-read-only t))
1287       (set-buffer (get-buffer-create preview-buffer))
1288       (widen)
1289       (erase-buffer)
1290       (if mother
1291           (setq mime-mother-buffer mother)
1292         )
1293       (setq mime-preview-original-window-configuration win-conf)
1294       (setq major-mode 'mime-view-mode)
1295       (setq mode-name "MIME-View")
1296       (mime-display-entity message nil
1297                            `((entity-button . invisible)
1298                              (header . visible)
1299                              (major-mode . ,original-major-mode))
1300                            preview-buffer)
1301       (mime-view-define-keymap default-keymap-or-function)
1302       (let ((point
1303              (next-single-property-change (point-min) 'mime-view-entity)))
1304         (if point
1305             (goto-char point)
1306           (goto-char (point-min))
1307           (search-forward "\n\n" nil t)
1308           ))
1309       (run-hooks 'mime-view-mode-hook)
1310       (set-buffer-modified-p nil)
1311       (setq buffer-read-only t)
1312       preview-buffer)))
1313
1314 ;;;###autoload
1315 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1316                                    default-keymap-or-function
1317                                    representation-type)
1318   "View RAW-BUFFER in MIME-View mode.
1319 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1320 buffer.
1321 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1322 function.  If it is a keymap, keymap of MIME-View mode will be added
1323 to it.  If it is a function, it will be bound as default binding of
1324 keymap of MIME-View mode.
1325 Optional argument REPRESENTATION-TYPE is representation-type of
1326 message.  It must be nil, `binary' or `cooked'.  If it is nil,
1327 `cooked' is used as default."
1328   (interactive)
1329   (or raw-buffer
1330       (setq raw-buffer (current-buffer)))
1331   (or representation-type
1332       (setq representation-type
1333             (save-excursion
1334               (set-buffer raw-buffer)
1335               (cdr (or (assq major-mode mime-raw-representation-type-alist)
1336                        (assq t mime-raw-representation-type-alist)))
1337               )))
1338   (if (eq representation-type 'binary)
1339       (setq representation-type 'buffer)
1340     )
1341   (setq preview-buffer (mime-display-message
1342                         (mime-open-entity representation-type raw-buffer)
1343                         preview-buffer mother default-keymap-or-function))
1344   (or (get-buffer-window preview-buffer)
1345       (let ((r-win (get-buffer-window raw-buffer)))
1346         (if r-win
1347             (set-window-buffer r-win preview-buffer)
1348           (let ((m-win (and mother (get-buffer-window mother))))
1349             (if m-win
1350                 (set-window-buffer m-win preview-buffer)
1351               (switch-to-buffer preview-buffer)
1352               ))))))
1353
1354 (defun mime-view-mode (&optional mother ctl encoding
1355                                  raw-buffer preview-buffer
1356                                  default-keymap-or-function)
1357   "Major mode for viewing MIME message.
1358
1359 Here is a list of the standard keys for mime-view-mode.
1360
1361 key             feature
1362 ---             -------
1363
1364 u               Move to upper content
1365 p or M-TAB      Move to previous content
1366 n or TAB        Move to next content
1367 SPC             Scroll up or move to next content
1368 M-SPC or DEL    Scroll down or move to previous content
1369 RET             Move to next line
1370 M-RET           Move to previous line
1371 v               Decode current content as `play mode'
1372 e               Decode current content as `extract mode'
1373 C-c C-p         Decode current content as `print mode'
1374 a               Followup to current content.
1375 q               Quit
1376 button-2        Move to point under the mouse cursor
1377                 and decode current content as `play mode'
1378 "
1379   (interactive)
1380   (unless mime-view-redisplay
1381     (save-excursion
1382       (if raw-buffer (set-buffer raw-buffer))
1383       (let ((type
1384              (cdr
1385               (or (assq major-mode mime-raw-representation-type-alist)
1386                   (assq t mime-raw-representation-type-alist)))))
1387         (if (eq type 'binary)
1388             (setq type 'buffer)
1389           )
1390         (setq mime-message-structure (mime-open-entity type raw-buffer))
1391         (or (mime-entity-content-type mime-message-structure)
1392             (mime-entity-set-content-type mime-message-structure ctl))
1393         )
1394       (or (mime-entity-encoding mime-message-structure)
1395           (mime-entity-set-encoding mime-message-structure encoding))
1396       ))
1397   (mime-display-message mime-message-structure preview-buffer
1398                         mother default-keymap-or-function)
1399   )
1400
1401
1402 ;;; @@ utility
1403 ;;;
1404
1405 (defun mime-preview-find-boundary-info (&optional get-mother)
1406   (let (entity
1407         p-beg p-end
1408         entity-node-id len)
1409     (while (null (setq entity
1410                        (get-text-property (point) 'mime-view-entity)))
1411       (backward-char))
1412     (setq p-beg (previous-single-property-change (point) 'mime-view-entity))
1413     (setq entity-node-id (mime-entity-node-id entity))
1414     (setq len (length entity-node-id))
1415     (cond ((null p-beg)
1416            (setq p-beg
1417                  (if (eq (next-single-property-change (point-min)
1418                                                       'mime-view-entity)
1419                          (point))
1420                      (point)
1421                    (point-min)))
1422            )
1423           ((eq (next-single-property-change p-beg 'mime-view-entity)
1424                (point))
1425            (setq p-beg (point))
1426            ))
1427     (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1428     (cond ((null p-end)
1429            (setq p-end (point-max))
1430            )
1431           ((null entity-node-id)
1432            (setq p-end (point-max))
1433            )
1434           (get-mother
1435            (save-excursion
1436              (goto-char p-end)
1437              (catch 'tag
1438                (let (e i)
1439                  (while (setq e
1440                               (next-single-property-change
1441                                (point) 'mime-view-entity))
1442                    (goto-char e)
1443                    (let ((rc (mime-entity-node-id
1444                               (get-text-property (1- (point))
1445                                                  'mime-view-entity))))
1446                      (or (and (>= (setq i (- (length rc) len)) 0)
1447                               (equal entity-node-id (nthcdr i rc)))
1448                          (throw 'tag nil)))
1449                    (setq p-end e)))
1450                (setq p-end (point-max))))
1451            ))
1452     (vector p-beg p-end entity)))
1453
1454
1455 ;;; @@ playing
1456 ;;;
1457
1458 (autoload 'mime-preview-play-current-entity "mime-play"
1459   "Play current entity." t)
1460
1461 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1462   "Extract current entity into file (maybe).
1463 It decodes current entity to call internal or external method as
1464 \"extract\" mode.  The method is selected from variable
1465 `mime-acting-condition'."
1466   (interactive "P")
1467   (mime-preview-play-current-entity ignore-examples "extract")
1468   )
1469
1470 (defun mime-preview-print-current-entity (&optional ignore-examples)
1471   "Print current entity (maybe).
1472 It decodes current entity to call internal or external method as
1473 \"print\" mode.  The method is selected from variable
1474 `mime-acting-condition'."
1475   (interactive "P")
1476   (mime-preview-play-current-entity ignore-examples "print")
1477   )
1478
1479
1480 ;;; @@ following
1481 ;;;
1482
1483 (defun mime-preview-follow-current-entity ()
1484   "Write follow message to current entity.
1485 It calls following-method selected from variable
1486 `mime-preview-following-method-alist'."
1487   (interactive)
1488   (let ((entity (mime-preview-find-boundary-info t))
1489         p-beg p-end
1490         pb-beg)
1491     (setq p-beg (aref entity 0)
1492           p-end (aref entity 1)
1493           entity (aref entity 2))
1494     (if (get-text-property p-beg 'mime-view-entity-body)
1495         (setq pb-beg p-beg)
1496       (setq pb-beg
1497             (next-single-property-change
1498              p-beg 'mime-view-entity-body nil
1499              (or (next-single-property-change p-beg 'mime-view-entity)
1500                  p-end))))
1501     (let* ((mode (mime-preview-original-major-mode 'recursive))
1502            (entity-node-id (mime-entity-node-id entity))
1503            (new-name
1504             (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1505            new-buf
1506            (the-buf (current-buffer))
1507            fields)
1508       (save-excursion
1509         (set-buffer (setq new-buf (get-buffer-create new-name)))
1510         (erase-buffer)
1511         (insert ?\n)
1512         (insert-buffer-substring the-buf pb-beg p-end)
1513         (goto-char (point-min))
1514         (let ((current-entity
1515                (if (and (eq (mime-entity-media-type entity) 'message)
1516                         (eq (mime-entity-media-subtype entity) 'rfc822))
1517                    (car (mime-entity-children entity))
1518                  entity)))
1519           (while (and current-entity
1520                       (if (and (eq (mime-entity-media-type
1521                                     current-entity) 'message)
1522                                (eq (mime-entity-media-subtype
1523                                     current-entity) 'rfc822))
1524                           nil
1525                         (mime-insert-header current-entity fields)
1526                         t))
1527             (setq fields (std11-collect-field-names)
1528                   current-entity (mime-entity-parent current-entity))
1529             ))
1530         (let ((rest mime-view-following-required-fields-list)
1531               field-name ret)
1532           (while rest
1533             (setq field-name (car rest))
1534             (or (std11-field-body field-name)
1535                 (progn
1536                   (save-excursion
1537                     (set-buffer the-buf)
1538                     (let ((entity (when mime-mother-buffer
1539                                     (set-buffer mime-mother-buffer)
1540                                     (get-text-property (point)
1541                                                        'mime-view-entity))))
1542                       (while (and entity
1543                                   (null (setq ret (mime-entity-fetch-field
1544                                                    entity field-name))))
1545                         (setq entity (mime-entity-parent entity)))))
1546                   (if ret
1547                       (insert (concat field-name ": " ret "\n"))
1548                     )))
1549             (setq rest (cdr rest))
1550             ))
1551         )
1552       (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1553         (if (functionp f)
1554             (funcall f new-buf)
1555           (message
1556            (format
1557             "Sorry, following method for %s is not implemented yet."
1558             mode))
1559           ))
1560       )))
1561
1562
1563 ;;; @@ moving
1564 ;;;
1565
1566 (defun mime-preview-move-to-upper ()
1567   "Move to upper entity.
1568 If there is no upper entity, call function `mime-preview-quit'."
1569   (interactive)
1570   (let (cinfo)
1571     (while (null (setq cinfo
1572                        (get-text-property (point) 'mime-view-entity)))
1573       (backward-char)
1574       )
1575     (let ((r (mime-entity-parent cinfo))
1576           point)
1577       (catch 'tag
1578         (while (setq point (previous-single-property-change
1579                             (point) 'mime-view-entity))
1580           (goto-char point)
1581           (when (eq r (get-text-property (point) 'mime-view-entity))
1582             (if (or (eq mime-preview-move-scroll t)
1583                     (and mime-preview-move-scroll
1584                          (>= point
1585                              (save-excursion
1586                                (move-to-window-line -1)
1587                                (forward-line (* -1 next-screen-context-lines))
1588                                (beginning-of-line)
1589                                (point)))))
1590                 (recenter next-screen-context-lines))
1591             (throw 'tag t)
1592             )
1593           )
1594         (mime-preview-quit)
1595         ))))
1596
1597 (defun mime-preview-move-to-previous ()
1598   "Move to previous entity.
1599 If there is no previous entity, it calls function registered in
1600 variable `mime-preview-over-to-previous-method-alist'."
1601   (interactive)
1602   (while (and (not (bobp))
1603               (null (get-text-property (point) 'mime-view-entity)))
1604     (backward-char)
1605     )
1606   (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1607     (if (and point
1608              (>= point (point-min)))
1609         (if (get-text-property (1- point) 'mime-view-entity)
1610             (progn (goto-char point)
1611                    (if
1612                     (or (eq mime-preview-move-scroll t)
1613                         (and mime-preview-move-scroll
1614                              (<= point
1615                                 (save-excursion
1616                                   (move-to-window-line 0)
1617                                   (forward-line next-screen-context-lines)
1618                                   (end-of-line)
1619                                   (point)))))
1620                         (recenter (* -1 next-screen-context-lines))))
1621           (goto-char (1- point))
1622           (mime-preview-move-to-previous)
1623           )
1624       (let ((f (assq (mime-preview-original-major-mode)
1625                      mime-preview-over-to-previous-method-alist)))
1626         (if f
1627             (funcall (cdr f))
1628           ))
1629       )))
1630
1631 (defun mime-preview-move-to-next ()
1632   "Move to next entity.
1633 If there is no previous entity, it calls function registered in
1634 variable `mime-preview-over-to-next-method-alist'."
1635   (interactive)
1636   (while (and (not (eobp))
1637               (null (get-text-property (point) 'mime-view-entity)))
1638     (forward-char)
1639     )
1640   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1641     (if (and point
1642              (<= point (point-max)))
1643         (progn
1644           (goto-char point)
1645           (if (null (get-text-property point 'mime-view-entity))
1646               (mime-preview-move-to-next)
1647             (and
1648              (or (eq mime-preview-move-scroll t)
1649                  (and mime-preview-move-scroll
1650                       (>= point
1651                          (save-excursion
1652                            (move-to-window-line -1)
1653                            (forward-line
1654                             (* -1 next-screen-context-lines))
1655                            (beginning-of-line)
1656                            (point)))))
1657                  (recenter next-screen-context-lines))
1658             ))
1659       (let ((f (assq (mime-preview-original-major-mode)
1660                      mime-preview-over-to-next-method-alist)))
1661         (if f
1662             (funcall (cdr f))
1663           ))
1664       )))
1665
1666 (defun mime-preview-scroll-up-entity (&optional h)
1667   "Scroll up current entity.
1668 If reached to (point-max), it calls function registered in variable
1669 `mime-preview-over-to-next-method-alist'."
1670   (interactive)
1671   (if (eobp)
1672       (let ((f (assq (mime-preview-original-major-mode)
1673                      mime-preview-over-to-next-method-alist)))
1674         (if f
1675             (funcall (cdr f))
1676           ))
1677     (let ((point
1678            (or (next-single-property-change (point) 'mime-view-entity)
1679                (point-max)))
1680           (bottom (window-end (selected-window))))
1681       (if (and (not h)
1682                (> bottom point))
1683           (progn (goto-char point)
1684                  (recenter next-screen-context-lines))
1685         (condition-case nil
1686             (scroll-up h)
1687           (end-of-buffer
1688            (goto-char (point-max)))))
1689       )))
1690
1691 (defun mime-preview-scroll-down-entity (&optional h)
1692   "Scroll down current entity.
1693 If reached to (point-min), it calls function registered in variable
1694 `mime-preview-over-to-previous-method-alist'."
1695   (interactive)
1696   (if (bobp)
1697       (let ((f (assq (mime-preview-original-major-mode)
1698                      mime-preview-over-to-previous-method-alist)))
1699         (if f
1700             (funcall (cdr f))
1701           ))
1702     (let ((point
1703            (or (previous-single-property-change (point) 'mime-view-entity)
1704                (point-min)))
1705           (top (window-start (selected-window))))
1706       (if (and (not h)
1707                (< top point))
1708           (progn (goto-char point)
1709                  (recenter (* -1 next-screen-context-lines)))
1710         (condition-case nil
1711             (scroll-down h)
1712           (beginning-of-buffer
1713            (goto-char (point-min)))))
1714       )))
1715
1716 (defun mime-preview-next-line-entity (&optional lines)
1717   "Scroll up one line (or prefix LINES lines).
1718 If LINES is negative, scroll down LINES lines."
1719   (interactive "p")
1720   (mime-preview-scroll-up-entity (or lines 1))
1721   )
1722
1723 (defun mime-preview-previous-line-entity (&optional lines)
1724   "Scrroll down one line (or prefix LINES lines).
1725 If LINES is negative, scroll up LINES lines."
1726   (interactive "p")
1727   (mime-preview-scroll-down-entity (or lines 1))
1728   )
1729
1730
1731 ;;; @@ display
1732 ;;;
1733
1734 (defun mime-preview-toggle-display (type &optional display)
1735   (let ((situation (mime-preview-find-boundary-info))
1736         (sym (intern (concat "*" (symbol-name type))))
1737         entity p-beg p-end)
1738     (setq p-beg (aref situation 0)
1739           p-end (aref situation 1)
1740           entity (aref situation 2)
1741           situation (get-text-property p-beg 'mime-view-situation))
1742     (cond ((eq display 'invisible)
1743            (setq display nil))
1744           (display)
1745           (t
1746            (setq display
1747                  (eq (cdr (or (assq sym situation)
1748                               (assq type situation)))
1749                      'invisible))))
1750     (setq situation (put-alist sym (if display
1751                                        'visible
1752                                      'invisible)
1753                                situation))
1754     (save-excursion
1755       (let ((inhibit-read-only t))
1756         (delete-region p-beg p-end)
1757         (mime-display-entity entity situation)))
1758     (let ((ret (assoc situation mime-preview-situation-example-list)))
1759       (if ret
1760           (setcdr ret (1+ (cdr ret)))
1761         (add-to-list 'mime-preview-situation-example-list
1762                      (cons situation 0))))))
1763
1764 (defun mime-preview-toggle-header (&optional force-visible)
1765   (interactive "P")
1766   (mime-preview-toggle-display 'header force-visible))
1767
1768 (defun mime-preview-toggle-content (&optional force-visible)
1769   (interactive "P")
1770   (mime-preview-toggle-display 'body force-visible))
1771
1772 (defun mime-preview-show-header ()
1773   (interactive)
1774   (mime-preview-toggle-display 'header 'visible))
1775
1776 (defun mime-preview-show-content ()
1777   (interactive)
1778   (mime-preview-toggle-display 'body 'visible))
1779
1780 (defun mime-preview-hide-header ()
1781   (interactive)
1782   (mime-preview-toggle-display 'header 'invisible))
1783
1784 (defun mime-preview-hide-content ()
1785   (interactive)
1786   (mime-preview-toggle-display 'body 'invisible))
1787
1788     
1789 ;;; @@ quitting
1790 ;;;
1791
1792 (defun mime-preview-quit ()
1793   "Quit from MIME-preview buffer.
1794 It calls function registered in variable
1795 `mime-preview-quitting-method-alist'."
1796   (interactive)
1797   (let ((r (assq (mime-preview-original-major-mode)
1798                  mime-preview-quitting-method-alist)))
1799     (if r
1800         (funcall (cdr r))
1801       )))
1802
1803 (defun mime-preview-kill-buffer ()
1804   (interactive)
1805   (kill-buffer (current-buffer))
1806   )
1807
1808
1809 ;;; @ end
1810 ;;;
1811
1812 (provide 'mime-view)
1813
1814 (let ((file mime-situation-examples-file))
1815   (if (file-readable-p file)
1816       (with-temp-buffer
1817         (insert-file-contents file)
1818         (setq mime-situation-examples-file-coding-system
1819               (static-cond
1820                ((boundp 'buffer-file-coding-system)
1821                 (symbol-value 'buffer-file-coding-system))
1822                ((boundp 'file-coding-system)
1823                 (symbol-value 'file-coding-system))
1824                (t nil)))
1825         (eval-buffer)
1826         ;; format check
1827         (condition-case nil
1828             (let ((i 0))
1829               (while (and (> (length mime-preview-situation-example-list)
1830                              mime-preview-situation-example-list-max-size)
1831                           (< i 16))
1832                 (setq mime-preview-situation-example-list
1833                       (mime-reduce-situation-examples
1834                        mime-preview-situation-example-list))
1835                 (setq i (1+ i))))
1836           (error (setq mime-preview-situation-example-list nil)))
1837         ;; (let ((rest mime-preview-situation-example-list))
1838         ;;   (while rest
1839         ;;     (ctree-set-calist-strictly 'mime-preview-condition
1840         ;;                                (caar rest))
1841         ;;     (setq rest (cdr rest))))
1842         (condition-case nil
1843             (let ((i 0))
1844               (while (and (> (length mime-acting-situation-example-list)
1845                              mime-acting-situation-example-list-max-size)
1846                           (< i 16))
1847                 (setq mime-acting-situation-example-list
1848                       (mime-reduce-situation-examples
1849                        mime-acting-situation-example-list))
1850                 (setq i (1+ i))))
1851           (error (setq mime-acting-situation-example-list nil))))))
1852
1853 ;;; mime-view.el ends here