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