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