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