* mime-view.el (mime-display-multipart/related): New function; set up
[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         (with-temp-buffer
426           (insert ";;; " (file-name-nondirectory file) "\n")
427           (insert "\n;; This file is generated automatically by "
428                   mime-view-version "\n\n")
429           (insert ";;; Code:\n\n")
430           (if mime-preview-situation-example-list
431               (pp `(setq mime-preview-situation-example-list
432                          ',mime-preview-situation-example-list)
433                   (current-buffer)))
434           (if mime-acting-situation-example-list
435               (pp `(setq mime-acting-situation-example-list
436                          ',mime-acting-situation-example-list)
437                   (current-buffer)))
438           (insert "\n;;; "
439                   (file-name-nondirectory file)
440                   " ends here.\n")
441           (static-cond
442            ((boundp 'buffer-file-coding-system)
443             (setq buffer-file-coding-system
444                   mime-situation-examples-file-coding-system))
445            ((boundp 'file-coding-system)
446             (setq file-coding-system
447                   mime-situation-examples-file-coding-system)))
448           ;; (setq buffer-file-coding-system
449           ;;       mime-situation-examples-file-coding-system)
450           (setq buffer-file-name file)
451           (save-buffer)))))
452
453 (add-hook 'kill-emacs-hook 'mime-save-situation-examples)
454
455 (defun mime-reduce-situation-examples (situation-examples)
456   (let ((len (length situation-examples))
457         i ir ic j jr jc ret
458         dest d-i d-j
459         (max-sim 0) sim
460         min-det-ret det-ret
461         min-det-org det-org
462         min-freq freq)
463     (setq i 0
464           ir situation-examples)
465     (while (< i len)
466       (setq ic (car ir)
467             j 0
468             jr situation-examples)
469       (while (< j len)
470         (unless (= i j)
471           (setq jc (car jr))
472           (setq ret (mime-compare-situation-with-example (car ic)(car jc))
473                 sim (car ret)
474                 det-ret (+ (length (car ic))(length (car jc)))
475                 det-org (length (cdr ret))
476                 freq (+ (cdr ic)(cdr jc)))
477           (cond ((< max-sim sim)
478                  (setq max-sim sim
479                        min-det-ret det-ret
480                        min-det-org det-org
481                        min-freq freq
482                        d-i i
483                        d-j j
484                        dest (cons (cdr ret) freq))
485                  )
486                 ((= max-sim sim)
487                  (cond ((> min-det-ret det-ret)
488                         (setq min-det-ret det-ret
489                               min-det-org det-org
490                               min-freq freq
491                               d-i i
492                               d-j j
493                               dest (cons (cdr ret) freq))
494                         )
495                        ((= min-det-ret det-ret)
496                         (cond ((> min-det-org det-org)
497                                (setq min-det-org det-org
498                                      min-freq freq
499                                      d-i i
500                                      d-j j
501                                      dest (cons (cdr ret) freq))
502                                )
503                               ((= min-det-org det-org)
504                                (cond ((> min-freq freq)
505                                       (setq min-freq freq
506                                             d-i i
507                                             d-j j
508                                             dest (cons (cdr ret) freq))
509                                       ))
510                                ))
511                         ))
512                  ))
513           )
514         (setq jr (cdr jr)
515               j (1+ j)))
516       (setq ir (cdr ir)
517             i (1+ i)))
518     (if (> d-i d-j)
519         (setq i d-i
520               d-i d-j
521               d-j i))
522     (setq jr (nthcdr (1- d-j) situation-examples))
523     (setcdr jr (cddr jr))
524     (if (= d-i 0)
525         (setq situation-examples
526               (cdr situation-examples))
527       (setq ir (nthcdr (1- d-i) situation-examples))
528       (setcdr ir (cddr ir))
529       )
530     (if (setq ir (assoc (car dest) situation-examples))
531         (progn
532           (setcdr ir (+ (cdr ir)(cdr dest)))
533           situation-examples)
534       (cons dest situation-examples)
535       ;; situation-examples may be modified.
536       )))
537
538
539 ;;; @ presentation of preview
540 ;;;
541
542 ;;; @@ entity-button
543 ;;;
544
545 ;;; @@@ predicate function
546 ;;;
547
548 ;; (defun mime-view-entity-button-visible-p (entity)
549 ;;   "Return non-nil if header of ENTITY is visible.
550 ;; Please redefine this function if you want to change default setting."
551 ;;   (let ((media-type (mime-entity-media-type entity))
552 ;;         (media-subtype (mime-entity-media-subtype entity)))
553 ;;     (or (not (eq media-type 'application))
554 ;;         (and (not (eq media-subtype 'x-selection))
555 ;;              (or (not (eq media-subtype 'octet-stream))
556 ;;                  (let ((mother-entity (mime-entity-parent entity)))
557 ;;                    (or (not (eq (mime-entity-media-type mother-entity)
558 ;;                                 'multipart))
559 ;;                        (not (eq (mime-entity-media-subtype mother-entity)
560 ;;                                 'encrypted)))
561 ;;                    )
562 ;;                  )))))
563
564 ;;; @@@ entity button generator
565 ;;;
566
567 (defun mime-view-insert-entity-button (entity)
568   "Insert entity-button of ENTITY."
569   (let ((entity-node-id (mime-entity-node-id entity))
570         (params (mime-entity-parameters entity))
571         (subject (mime-view-entity-title entity)))
572     (mime-insert-button
573      (let ((access-type (assoc "access-type" params))
574            (num (or (cdr (assoc "x-part-number" params))
575                     (if (consp entity-node-id)
576                         (mapconcat (function
577                                     (lambda (num)
578                                       (format "%s" (1+ num))
579                                       ))
580                                    (reverse entity-node-id) ".")
581                       "0"))
582                 ))
583        (cond (access-type
584               (let ((server (assoc "server" params)))
585                 (setq access-type (cdr access-type))
586                 (if server
587                     (format "%s %s ([%s] %s)"
588                             num subject access-type (cdr server))
589                 (let ((site (cdr (assoc "site" params)))
590                       (dir (cdr (assoc "directory" params)))
591                       (url (cdr (assoc "url" params)))
592                       )
593                   (if url
594                       (format "%s %s ([%s] %s)"
595                               num subject access-type url)
596                     (format "%s %s ([%s] %s:%s)"
597                             num subject access-type site dir))
598                   )))
599             )
600            (t
601             (let ((media-type (mime-entity-media-type entity))
602                   (media-subtype (mime-entity-media-subtype entity))
603                   (charset (cdr (assoc "charset" params)))
604                   (encoding (mime-entity-encoding entity)))
605               (concat
606                num " " subject
607                (let ((rest
608                       (format " <%s/%s%s%s>"
609                               media-type media-subtype
610                               (if charset
611                                   (concat "; " charset)
612                                 "")
613                               (if encoding
614                                   (concat " (" encoding ")")
615                                 ""))))
616                  (if (>= (+ (current-column)(length rest))(window-width))
617                      "\n\t")
618                  rest)))
619             )))
620      (function mime-preview-play-current-entity))
621     ))
622
623
624 ;;; @@ entity-header
625 ;;;
626
627 (defvar mime-header-presentation-method-alist nil
628   "Alist of major mode vs. corresponding header-presentation-method functions.
629 Each element looks like (SYMBOL . FUNCTION).
630 SYMBOL must be major mode in raw-buffer or t.  t means default.
631 Interface of FUNCTION must be (ENTITY SITUATION).")
632
633 (defvar mime-view-ignored-field-list
634   '(".*Received:" ".*Path:" ".*Id:" "^References:"
635     "^Replied:" "^Errors-To:"
636     "^Lines:" "^Sender:" ".*Host:" "^Xref:"
637     "^Content-Type:" "^Precedence:"
638     "^Status:" "^X-VM-.*:")
639   "All fields that match this list will be hidden in MIME preview buffer.
640 Each elements are regexp of field-name.")
641
642 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
643   "All fields that match this list will be displayed in MIME preview buffer.
644 Each elements are regexp of field-name.")
645
646
647 ;;; @@ entity-body
648 ;;;
649
650 ;;; @@@ predicate function
651 ;;;
652
653 (in-calist-package 'mime-view)
654
655 (defun mime-calist::field-match-method-as-default-rule (calist
656                                                         field-type field-value)
657   (let ((s-field (assq field-type calist)))
658     (cond ((null s-field)
659            (cons (cons field-type field-value) calist)
660            )
661           (t calist))))
662
663 (define-calist-field-match-method
664   'header #'mime-calist::field-match-method-as-default-rule)
665
666 (define-calist-field-match-method
667   'body #'mime-calist::field-match-method-as-default-rule)
668
669
670 (defvar mime-preview-condition nil
671   "Condition-tree about how to display entity.")
672
673 (ctree-set-calist-strictly
674  'mime-preview-condition '((type . application)(subtype . octet-stream)
675                            (encoding . nil)
676                            (body . visible)))
677 (ctree-set-calist-strictly
678  'mime-preview-condition '((type . application)(subtype . octet-stream)
679                            (encoding . "7bit")
680                            (body . visible)))
681 (ctree-set-calist-strictly
682  'mime-preview-condition '((type . application)(subtype . octet-stream)
683                            (encoding . "8bit")
684                            (body . visible)))
685
686 (ctree-set-calist-strictly
687  'mime-preview-condition '((type . application)(subtype . pgp)
688                            (body . visible)))
689
690 (ctree-set-calist-strictly
691  'mime-preview-condition '((type . application)(subtype . x-latex)
692                            (body . visible)))
693
694 (ctree-set-calist-strictly
695  'mime-preview-condition '((type . application)(subtype . x-selection)
696                            (body . visible)))
697
698 (ctree-set-calist-strictly
699  'mime-preview-condition '((type . application)(subtype . x-comment)
700                            (body . visible)))
701
702 (ctree-set-calist-strictly
703  'mime-preview-condition '((type . message)(subtype . delivery-status)
704                            (body . visible)))
705
706 (ctree-set-calist-strictly
707  'mime-preview-condition
708  '((body . visible)
709    (body-presentation-method . mime-display-text/plain)))
710
711 (ctree-set-calist-strictly
712  'mime-preview-condition
713  '((type . nil)
714    (body . visible)
715    (body-presentation-method . mime-display-text/plain)))
716
717 (ctree-set-calist-strictly
718  'mime-preview-condition
719  '((type . text)(subtype . enriched)
720    (body . visible)
721    (body-presentation-method . mime-display-text/enriched)))
722
723 (ctree-set-calist-strictly
724  'mime-preview-condition
725  '((type . text)(subtype . richtext)
726    (body . visible)
727    (body-presentation-method . mime-display-text/richtext)))
728
729 (autoload 'mime-display-application/x-postpet "postpet")
730
731 (ctree-set-calist-strictly
732  'mime-preview-condition
733  '((type . application)(subtype . x-postpet)
734    (body . visible)
735    (body-presentation-method . mime-display-application/x-postpet)))
736
737 (ctree-set-calist-strictly
738  'mime-preview-condition
739  '((type . text)(subtype . t)
740    (body . visible)
741    (body-presentation-method . mime-display-text/plain)))
742
743 (ctree-set-calist-strictly
744  'mime-preview-condition
745  '((type . multipart)(subtype . alternative)
746    (body . visible)
747    (body-presentation-method . mime-display-multipart/alternative)))
748
749 (ctree-set-calist-strictly
750  'mime-preview-condition
751  '((type . multipart)(subtype . related)
752    (body . visible)
753    (body-presentation-method . mime-display-multipart/related)))
754
755 (ctree-set-calist-strictly
756  'mime-preview-condition
757  '((type . multipart)(subtype . t)
758    (body . visible)
759    (body-presentation-method . mime-display-multipart/mixed)))
760
761 (ctree-set-calist-strictly
762  'mime-preview-condition
763  '((type . message)(subtype . partial)
764    (body . visible)
765    (body-presentation-method . mime-display-message/partial-button)))
766
767 (ctree-set-calist-strictly
768  'mime-preview-condition
769  '((type . message)(subtype . rfc822)
770    (body . visible)
771    (body-presentation-method . mime-display-multipart/mixed)
772    (childrens-situation (header . visible)
773                         (entity-button . invisible))))
774
775 (ctree-set-calist-strictly
776  'mime-preview-condition
777  '((type . message)(subtype . news)
778    (body . visible)
779    (body-presentation-method . mime-display-multipart/mixed)
780    (childrens-situation (header . visible)
781                         (entity-button . invisible))))
782
783
784 ;;; @@@ entity presentation
785 ;;;
786
787 (defun mime-display-text/plain (entity situation)
788   (save-restriction
789     (narrow-to-region (point-max)(point-max))
790     (condition-case nil
791         (mime-insert-text-content entity)
792       (error (progn
793                (message "Can't decode current entity.")
794                (sit-for 1))))
795     (run-hooks 'mime-text-decode-hook)
796     (goto-char (point-max))
797     (if (not (eq (char-after (1- (point))) ?\n))
798         (insert "\n")
799       )
800     (mime-add-url-buttons)
801     (run-hooks 'mime-display-text/plain-hook)
802     ))
803
804 (defun mime-display-text/richtext (entity situation)
805   (save-restriction
806     (narrow-to-region (point-max)(point-max))
807     (mime-insert-text-content entity)
808     (run-hooks 'mime-text-decode-hook)
809     (let ((beg (point-min)))
810       (remove-text-properties beg (point-max) '(face nil))
811       (richtext-decode beg (point-max))
812       )))
813
814 (defun mime-display-text/enriched (entity situation)
815   (save-restriction
816     (narrow-to-region (point-max)(point-max))
817     (mime-insert-text-content entity)
818     (run-hooks 'mime-text-decode-hook)
819     (let ((beg (point-min)))
820       (remove-text-properties beg (point-max) '(face nil))
821       (enriched-decode beg (point-max))
822       )))
823
824
825 (defvar mime-view-announcement-for-message/partial
826   (if (and (>= emacs-major-version 19) window-system)
827       "\
828 \[[ This is message/partial style split message. ]]
829 \[[ Please press `v' key in this buffer          ]]
830 \[[ or click here by mouse button-2.             ]]"
831     "\
832 \[[ This is message/partial style split message. ]]
833 \[[ Please press `v' key in this buffer.         ]]"
834     ))
835
836 (defun mime-display-message/partial-button (&optional entity situation)
837   (save-restriction
838     (goto-char (point-max))
839     (if (not (search-backward "\n\n" nil t))
840         (insert "\n")
841       )
842     (goto-char (point-max))
843     (narrow-to-region (point-max)(point-max))
844     (insert mime-view-announcement-for-message/partial)
845     (mime-add-button (point-min)(point-max)
846                      #'mime-preview-play-current-entity)
847     ))
848
849 (defun mime-display-multipart/mixed (entity situation)
850   (let ((children (mime-entity-children entity))
851         (original-major-mode-cell (assq 'major-mode situation))
852         (default-situation
853           (cdr (assq 'childrens-situation situation))))
854     (if original-major-mode-cell
855         (setq default-situation
856               (cons original-major-mode-cell default-situation)))
857     (while children
858       (mime-display-entity (car children) nil default-situation)
859       (setq children (cdr children))
860       )))
861
862 (defcustom mime-view-type-subtype-score-alist
863   '(((text . enriched) . 3)
864     ((text . richtext) . 2)
865     ((text . plain)    . 1)
866     (t . 0))
867   "Alist MEDIA-TYPE vs corresponding score.
868 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
869   :group 'mime-view
870   :type '(repeat (cons (choice :tag "Media-Type"
871                                (cons :tag "Type/Subtype"
872                                      (symbol :tag "Primary-type")
873                                      (symbol :tag "Subtype"))
874                                (symbol :tag "Type")
875                                (const :tag "Default" t))
876                        integer)))
877
878 (defun mime-display-multipart/alternative (entity situation)
879   (let* ((children (mime-entity-children entity))
880          (original-major-mode-cell (assq 'major-mode situation))
881          (default-situation
882            (cdr (assq 'childrens-situation situation)))
883          (i 0)
884          (p 0)
885          (max-score 0)
886          situations)
887     (if original-major-mode-cell
888         (setq default-situation
889               (cons original-major-mode-cell default-situation)))
890     (setq situations
891           (mapcar (function
892                    (lambda (child)
893                      (let ((situation
894                             (mime-find-entity-preview-situation
895                              child default-situation)))
896                        (if (cdr (assq 'body-presentation-method situation))
897                            (let ((score
898                                   (cdr
899                                    (or (assoc
900                                         (cons
901                                          (cdr (assq 'type situation))
902                                          (cdr (assq 'subtype situation)))
903                                         mime-view-type-subtype-score-alist)
904                                        (assq
905                                         (cdr (assq 'type situation))
906                                         mime-view-type-subtype-score-alist)
907                                        (assq
908                                         t
909                                         mime-view-type-subtype-score-alist)
910                                        ))))
911                              (if (> score max-score)
912                                  (setq p i
913                                        max-score score)
914                                )))
915                        (setq i (1+ i))
916                        situation)
917                      ))
918                   children))
919     (setq i 0)
920     (while children
921       (let ((child (car children))
922             (situation (car situations)))
923         (mime-display-entity child (if (= i p)
924                                        situation
925                                      (put-alist 'body 'invisible
926                                                 (copy-alist situation)))))
927       (setq children (cdr children)
928             situations (cdr situations)
929             i (1+ i)))))
930
931 (defun mime-display-multipart/related (entity situation)
932   (let* ((param-start (mime-parse-msg-id
933                        (std11-lexical-analyze
934                         (cdr (assoc "start"
935                                     (mime-content-type-parameters
936                                      (mime-entity-content-type entity)))))))
937          (start (or (and param-start (mime-find-entity-from-content-id
938                                       param-start
939                                       entity))
940                     (car (mime-entity-children entity))))
941          (original-major-mode-cell (assq 'major-mode situation))
942          (default-situation (cdr (assq 'childrens-situation situation))))
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 get-mother)
1476   (let (entity
1477         p-beg p-end
1478         entity-node-id len)
1479     (while (null (setq entity
1480                        (get-text-property (point) 'mime-view-entity)))
1481       (backward-char))
1482     (setq p-beg (previous-single-property-change (point) 'mime-view-entity))
1483     (setq entity-node-id (mime-entity-node-id entity))
1484     (setq len (length entity-node-id))
1485     (cond ((null p-beg)
1486            (setq p-beg
1487                  (if (eq (next-single-property-change (point-min)
1488                                                       'mime-view-entity)
1489                          (point))
1490                      (point)
1491                    (point-min)))
1492            )
1493           ((eq (next-single-property-change p-beg 'mime-view-entity)
1494                (point))
1495            (setq p-beg (point))
1496            ))
1497     (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1498     (cond ((null p-end)
1499            (setq p-end (point-max))
1500            )
1501           ((null entity-node-id)
1502            (setq p-end (point-max))
1503            )
1504           (get-mother
1505            (save-excursion
1506              (goto-char p-end)
1507              (catch 'tag
1508                (let (e i)
1509                  (while (setq e
1510                               (next-single-property-change
1511                                (point) 'mime-view-entity))
1512                    (goto-char e)
1513                    (let ((rc (mime-entity-node-id
1514                               (get-text-property (1- (point))
1515                                                  'mime-view-entity))))
1516                      (or (and (>= (setq i (- (length rc) len)) 0)
1517                               (equal entity-node-id (nthcdr i rc)))
1518                          (throw 'tag nil)))
1519                    (setq p-end e)))
1520                (setq p-end (point-max))))
1521            ))
1522     (vector p-beg p-end entity)))
1523
1524
1525 ;;; @@ playing
1526 ;;;
1527
1528 (autoload 'mime-preview-play-current-entity "mime-play"
1529   "Play current entity." t)
1530
1531 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1532   "Extract current entity into file (maybe).
1533 It decodes current entity to call internal or external method as
1534 \"extract\" mode.  The method is selected from variable
1535 `mime-acting-condition'."
1536   (interactive "P")
1537   (mime-preview-play-current-entity ignore-examples "extract")
1538   )
1539
1540 (defun mime-preview-print-current-entity (&optional ignore-examples)
1541   "Print current entity (maybe).
1542 It decodes current entity to call internal or external method as
1543 \"print\" mode.  The method is selected from variable
1544 `mime-acting-condition'."
1545   (interactive "P")
1546   (mime-preview-play-current-entity ignore-examples "print")
1547   )
1548
1549
1550 ;;; @@ following
1551 ;;;
1552
1553 (defun mime-preview-follow-current-entity ()
1554   "Write follow message to current entity.
1555 It calls following-method selected from variable
1556 `mime-preview-following-method-alist'."
1557   (interactive)
1558   (let ((entity (mime-preview-find-boundary-info t))
1559         p-beg p-end
1560         pb-beg)
1561     (setq p-beg (aref entity 0)
1562           p-end (aref entity 1)
1563           entity (aref entity 2))
1564     (if (get-text-property p-beg 'mime-view-entity-body)
1565         (setq pb-beg p-beg)
1566       (setq pb-beg
1567             (next-single-property-change
1568              p-beg 'mime-view-entity-body nil
1569              (or (next-single-property-change p-beg 'mime-view-entity)
1570                  p-end))))
1571     (let* ((mode (mime-preview-original-major-mode 'recursive))
1572            (entity-node-id (mime-entity-node-id entity))
1573            (new-name
1574             (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1575            new-buf
1576            (the-buf (current-buffer))
1577            fields)
1578       (save-excursion
1579         (set-buffer (setq new-buf (get-buffer-create new-name)))
1580         (erase-buffer)
1581         (insert ?\n)
1582         (insert-buffer-substring the-buf pb-beg p-end)
1583         (goto-char (point-min))
1584         (let ((current-entity
1585                (if (and (eq (mime-entity-media-type entity) 'message)
1586                         (eq (mime-entity-media-subtype entity) 'rfc822))
1587                    (car (mime-entity-children entity))
1588                  entity)))
1589           (while (and current-entity
1590                       (if (and (eq (mime-entity-media-type
1591                                     current-entity) 'message)
1592                                (eq (mime-entity-media-subtype
1593                                     current-entity) 'rfc822))
1594                           nil
1595                         (mime-insert-header current-entity fields)
1596                         t))
1597             (setq fields (std11-collect-field-names)
1598                   current-entity (mime-entity-parent current-entity))
1599             ))
1600         (let ((rest mime-view-following-required-fields-list)
1601               field-name ret)
1602           (while rest
1603             (setq field-name (car rest))
1604             (or (std11-field-body field-name)
1605                 (progn
1606                   (save-excursion
1607                     (set-buffer the-buf)
1608                     (let ((entity (when mime-mother-buffer
1609                                     (set-buffer mime-mother-buffer)
1610                                     (get-text-property (point)
1611                                                        'mime-view-entity))))
1612                       (while (and entity
1613                                   (null (setq ret (mime-entity-fetch-field
1614                                                    entity field-name))))
1615                         (setq entity (mime-entity-parent entity)))))
1616                   (if ret
1617                       (insert (concat field-name ": " ret "\n"))
1618                     )))
1619             (setq rest (cdr rest))
1620             ))
1621         )
1622       (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1623         (if (functionp f)
1624             (funcall f new-buf)
1625           (message
1626            (format
1627             "Sorry, following method for %s is not implemented yet."
1628             mode))
1629           ))
1630       )))
1631
1632
1633 ;;; @@ moving
1634 ;;;
1635
1636 (defun mime-preview-move-to-upper ()
1637   "Move to upper entity.
1638 If there is no upper entity, call function `mime-preview-quit'."
1639   (interactive)
1640   (let (cinfo)
1641     (while (null (setq cinfo
1642                        (get-text-property (point) 'mime-view-entity)))
1643       (backward-char)
1644       )
1645     (let ((r (mime-entity-parent cinfo))
1646           point)
1647       (catch 'tag
1648         (while (setq point (previous-single-property-change
1649                             (point) 'mime-view-entity))
1650           (goto-char point)
1651           (when (eq r (get-text-property (point) 'mime-view-entity))
1652             (if (or (eq mime-preview-move-scroll t)
1653                     (and mime-preview-move-scroll
1654                          (>= point
1655                              (save-excursion
1656                                (move-to-window-line -1)
1657                                (forward-line (* -1 next-screen-context-lines))
1658                                (beginning-of-line)
1659                                (point)))))
1660                 (recenter next-screen-context-lines))
1661             (throw 'tag t)
1662             )
1663           )
1664         (mime-preview-quit)
1665         ))))
1666
1667 (defun mime-preview-move-to-previous ()
1668   "Move to previous entity.
1669 If there is no previous entity, it calls function registered in
1670 variable `mime-preview-over-to-previous-method-alist'."
1671   (interactive)
1672   (while (and (not (bobp))
1673               (null (get-text-property (point) 'mime-view-entity)))
1674     (backward-char)
1675     )
1676   (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1677     (if (and point
1678              (>= point (point-min)))
1679         (if (get-text-property (1- point) 'mime-view-entity)
1680             (progn (goto-char point)
1681                    (if
1682                     (or (eq mime-preview-move-scroll t)
1683                         (and mime-preview-move-scroll
1684                              (<= point
1685                                 (save-excursion
1686                                   (move-to-window-line 0)
1687                                   (forward-line next-screen-context-lines)
1688                                   (end-of-line)
1689                                   (point)))))
1690                         (recenter (* -1 next-screen-context-lines))))
1691           (goto-char (1- point))
1692           (mime-preview-move-to-previous)
1693           )
1694       (let ((f (assq (mime-preview-original-major-mode)
1695                      mime-preview-over-to-previous-method-alist)))
1696         (if f
1697             (funcall (cdr f))
1698           ))
1699       )))
1700
1701 (defun mime-preview-move-to-next ()
1702   "Move to next entity.
1703 If there is no previous entity, it calls function registered in
1704 variable `mime-preview-over-to-next-method-alist'."
1705   (interactive)
1706   (while (and (not (eobp))
1707               (null (get-text-property (point) 'mime-view-entity)))
1708     (forward-char)
1709     )
1710   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1711     (if (and point
1712              (<= point (point-max)))
1713         (progn
1714           (goto-char point)
1715           (if (null (get-text-property point 'mime-view-entity))
1716               (mime-preview-move-to-next)
1717             (and
1718              (or (eq mime-preview-move-scroll t)
1719                  (and mime-preview-move-scroll
1720                       (>= point
1721                          (save-excursion
1722                            (move-to-window-line -1)
1723                            (forward-line
1724                             (* -1 next-screen-context-lines))
1725                            (beginning-of-line)
1726                            (point)))))
1727                  (recenter next-screen-context-lines))
1728             ))
1729       (let ((f (assq (mime-preview-original-major-mode)
1730                      mime-preview-over-to-next-method-alist)))
1731         (if f
1732             (funcall (cdr f))
1733           ))
1734       )))
1735
1736 (defun mime-preview-scroll-up-entity (&optional h)
1737   "Scroll up current entity.
1738 If reached to (point-max), it calls function registered in variable
1739 `mime-preview-over-to-next-method-alist'."
1740   (interactive)
1741   (if (eobp)
1742       (let ((f (assq (mime-preview-original-major-mode)
1743                      mime-preview-over-to-next-method-alist)))
1744         (if f
1745             (funcall (cdr f))
1746           ))
1747     (let ((point
1748            (or (next-single-property-change (point) 'mime-view-entity)
1749                (point-max)))
1750           (bottom (window-end (selected-window))))
1751       (if (and (not h)
1752                (> bottom point))
1753           (progn (goto-char point)
1754                  (recenter next-screen-context-lines))
1755         (condition-case nil
1756             (scroll-up h)
1757           (end-of-buffer
1758            (goto-char (point-max)))))
1759       )))
1760
1761 (defun mime-preview-scroll-down-entity (&optional h)
1762   "Scroll down current entity.
1763 If reached to (point-min), it calls function registered in variable
1764 `mime-preview-over-to-previous-method-alist'."
1765   (interactive)
1766   (if (bobp)
1767       (let ((f (assq (mime-preview-original-major-mode)
1768                      mime-preview-over-to-previous-method-alist)))
1769         (if f
1770             (funcall (cdr f))
1771           ))
1772     (let ((point
1773            (or (previous-single-property-change (point) 'mime-view-entity)
1774                (point-min)))
1775           (top (window-start (selected-window))))
1776       (if (and (not h)
1777                (< top point))
1778           (progn (goto-char point)
1779                  (recenter (* -1 next-screen-context-lines)))
1780         (condition-case nil
1781             (scroll-down h)
1782           (beginning-of-buffer
1783            (goto-char (point-min)))))
1784       )))
1785
1786 (defun mime-preview-next-line-entity (&optional lines)
1787   "Scroll up one line (or prefix LINES lines).
1788 If LINES is negative, scroll down LINES lines."
1789   (interactive "p")
1790   (mime-preview-scroll-up-entity (or lines 1))
1791   )
1792
1793 (defun mime-preview-previous-line-entity (&optional lines)
1794   "Scrroll down one line (or prefix LINES lines).
1795 If LINES is negative, scroll up LINES lines."
1796   (interactive "p")
1797   (mime-preview-scroll-down-entity (or lines 1))
1798   )
1799
1800
1801 ;;; @@ display
1802 ;;;
1803
1804 (defun mime-preview-toggle-display (type &optional display)
1805   (let ((situation (mime-preview-find-boundary-info))
1806         (sym (intern (concat "*" (symbol-name type))))
1807         entity p-beg p-end)
1808     (setq p-beg (aref situation 0)
1809           p-end (aref situation 1)
1810           entity (aref situation 2)
1811           situation (get-text-property p-beg 'mime-view-situation))
1812     (cond ((eq display 'invisible)
1813            (setq display nil))
1814           (display)
1815           (t
1816            (setq display
1817                  (eq (cdr (or (assq sym situation)
1818                               (assq type situation)))
1819                      'invisible))))
1820     (setq situation (put-alist sym (if display
1821                                        'visible
1822                                      'invisible)
1823                                situation))
1824     (save-excursion
1825       (let ((inhibit-read-only t))
1826         (delete-region p-beg p-end)
1827         (mime-display-entity entity situation)))
1828     (let ((ret (assoc situation mime-preview-situation-example-list)))
1829       (if ret
1830           (setcdr ret (1+ (cdr ret)))
1831         (add-to-list 'mime-preview-situation-example-list
1832                      (cons situation 0))))))
1833
1834 (defun mime-preview-toggle-header (&optional force-visible)
1835   (interactive "P")
1836   (mime-preview-toggle-display 'header force-visible))
1837
1838 (defun mime-preview-toggle-content (&optional force-visible)
1839   (interactive "P")
1840   (mime-preview-toggle-display 'body force-visible))
1841
1842 (defun mime-preview-show-header ()
1843   (interactive)
1844   (mime-preview-toggle-display 'header 'visible))
1845
1846 (defun mime-preview-show-content ()
1847   (interactive)
1848   (mime-preview-toggle-display 'body 'visible))
1849
1850 (defun mime-preview-hide-header ()
1851   (interactive)
1852   (mime-preview-toggle-display 'header 'invisible))
1853
1854 (defun mime-preview-hide-content ()
1855   (interactive)
1856   (mime-preview-toggle-display 'body 'invisible))
1857
1858     
1859 ;;; @@ quitting
1860 ;;;
1861
1862 (defun mime-preview-quit ()
1863   "Quit from MIME-preview buffer.
1864 It calls function registered in variable
1865 `mime-preview-quitting-method-alist'."
1866   (interactive)
1867   (let ((r (assq (mime-preview-original-major-mode)
1868                  mime-preview-quitting-method-alist)))
1869     (if r
1870         (funcall (cdr r))
1871       )))
1872
1873 (defun mime-preview-kill-buffer ()
1874   (interactive)
1875   (kill-buffer (current-buffer))
1876   )
1877
1878
1879 ;;; @ end
1880 ;;;
1881
1882 (provide 'mime-view)
1883
1884 (eval-when-compile
1885   (setq mime-situation-examples-file nil)
1886   ;; to avoid to read situation-examples-file at compile time.
1887   )
1888
1889 (mime-view-read-situation-examples-file)
1890
1891 ;;; mime-view.el ends here