* mime-image.el (mime-image-normalize-xbm-buffer): Add missing format
[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     (if original-major-mode-cell
943         (setq default-situation
944               (cons original-major-mode-cell default-situation)))
945     (mime-display-entity start nil default-situation)))
946
947 ;;; @ acting-condition
948 ;;;
949
950 (defvar mime-acting-condition nil
951   "Condition-tree about how to process entity.")
952
953 (defun mime-view-read-mailcap-files (&optional files)
954   (or files
955       (setq files mime-view-mailcap-files))
956   (let (entries file)
957     (while files
958       (setq file (car files))
959       (if (file-readable-p file)
960           (setq entries (append entries (mime-parse-mailcap-file file))))
961       (setq files (cdr files)))
962     (while entries
963       (let ((entry (car entries))
964             view print shared)
965         (while entry
966           (let* ((field (car entry))
967                  (field-type (car field)))
968             (cond ((eq field-type 'view)  (setq view field))
969                   ((eq field-type 'print) (setq print field))
970                   ((memq field-type '(compose composetyped edit)))
971                   (t (setq shared (cons field shared))))
972             )
973           (setq entry (cdr entry)))
974         (setq shared (nreverse shared))
975         (ctree-set-calist-with-default
976          'mime-acting-condition
977          (append shared (list '(mode . "play")(cons 'method (cdr view)))))
978         (if print
979             (ctree-set-calist-with-default
980              'mime-acting-condition
981              (append shared
982                      (list '(mode . "print")(cons 'method (cdr view)))))))
983       (setq entries (cdr entries)))))
984
985 (mime-view-read-mailcap-files)
986
987 (ctree-set-calist-strictly
988  'mime-acting-condition
989  '((type . application)(subtype . octet-stream)
990    (mode . "play")
991    (method . mime-detect-content)
992    ))
993
994 (ctree-set-calist-with-default
995  'mime-acting-condition
996  '((mode . "extract")
997    (method . mime-save-content)))
998
999 (ctree-set-calist-strictly
1000  'mime-acting-condition
1001  '((type . text)(subtype . x-rot13-47)(mode . "play")
1002    (method . mime-view-caesar)
1003    ))
1004 (ctree-set-calist-strictly
1005  'mime-acting-condition
1006  '((type . text)(subtype . x-rot13-47-48)(mode . "play")
1007    (method . mime-view-caesar)
1008    ))
1009
1010 (ctree-set-calist-strictly
1011  'mime-acting-condition
1012  '((type . message)(subtype . rfc822)(mode . "play")
1013    (method . mime-view-message/rfc822)
1014    ))
1015 (ctree-set-calist-strictly
1016  'mime-acting-condition
1017  '((type . message)(subtype . partial)(mode . "play")
1018    (method . mime-store-message/partial-piece)
1019    ))
1020
1021 (ctree-set-calist-strictly
1022  'mime-acting-condition
1023  '((type . message)(subtype . external-body)
1024    ("access-type" . "anon-ftp")
1025    (method . mime-view-message/external-anon-ftp)
1026    ))
1027
1028 (ctree-set-calist-strictly
1029  'mime-acting-condition
1030  '((type . message)(subtype . external-body)
1031    ("access-type" . "url")
1032    (method . mime-view-message/external-url)
1033    ))
1034
1035 (ctree-set-calist-strictly
1036  'mime-acting-condition
1037  '((type . application)(subtype . octet-stream)
1038    (method . mime-save-content)
1039    ))
1040
1041
1042 ;;; @ quitting method
1043 ;;;
1044
1045 (defvar mime-preview-quitting-method-alist
1046   '((mime-show-message-mode
1047      . mime-preview-quitting-method-for-mime-show-message-mode))
1048   "Alist of major-mode vs. quitting-method of mime-view.")
1049
1050 (defvar mime-preview-over-to-previous-method-alist nil
1051   "Alist of major-mode vs. over-to-previous-method of mime-view.")
1052
1053 (defvar mime-preview-over-to-next-method-alist nil
1054   "Alist of major-mode vs. over-to-next-method of mime-view.")
1055
1056
1057 ;;; @ following method
1058 ;;;
1059
1060 (defvar mime-preview-following-method-alist nil
1061   "Alist of major-mode vs. following-method of mime-view.")
1062
1063 (defvar mime-view-following-required-fields-list
1064   '("From"))
1065
1066
1067 ;;; @ buffer setup
1068 ;;;
1069
1070 (defun mime-display-entity (entity &optional situation
1071                                    default-situation preview-buffer)
1072   (or preview-buffer
1073       (setq preview-buffer (current-buffer)))
1074   (let* (e nb ne nhb nbb)
1075     (in-calist-package 'mime-view)
1076     (or situation
1077         (setq situation
1078               (mime-find-entity-preview-situation entity default-situation)))
1079     (let ((button-is-invisible
1080            (eq (cdr (or (assq '*entity-button situation)
1081                         (assq 'entity-button situation)))
1082                'invisible))
1083           (header-is-visible
1084            (eq (cdr (or (assq '*header situation)
1085                         (assq 'header situation)))
1086                'visible))
1087           (body-is-visible
1088            (eq (cdr (or (assq '*body situation)
1089                         (assq 'body situation)))
1090                'visible))
1091           (children (mime-entity-children entity)))
1092       (set-buffer preview-buffer)
1093       (setq nb (point))
1094       (narrow-to-region nb nb)
1095       (or button-is-invisible
1096           ;; (if (mime-view-entity-button-visible-p entity)
1097           (mime-view-insert-entity-button entity)
1098           ;;   )
1099           )
1100       (if header-is-visible
1101           (let ((header-presentation-method
1102                  (or (cdr (assq 'header-presentation-method situation))
1103                      (cdr (assq (cdr (assq 'major-mode situation))
1104                                 mime-header-presentation-method-alist)))))
1105             (setq nhb (point))
1106             (if header-presentation-method
1107                 (funcall header-presentation-method entity situation)
1108               (mime-insert-header entity
1109                                   mime-view-ignored-field-list
1110                                   mime-view-visible-field-list))
1111             (run-hooks 'mime-display-header-hook)
1112             (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1113             (goto-char (point-max))
1114             (insert "\n")))
1115       (setq nbb (point))
1116       (unless children
1117         (if body-is-visible
1118             (let ((body-presentation-method
1119                    (cdr (assq 'body-presentation-method situation))))
1120               (if (functionp body-presentation-method)
1121                   (funcall body-presentation-method entity situation)
1122                 (mime-display-text/plain entity situation)))
1123           (when button-is-invisible
1124             (goto-char (point-max))
1125             (mime-view-insert-entity-button entity)
1126             )
1127           (unless header-is-visible
1128             (goto-char (point-max))
1129             (insert "\n"))
1130           ))
1131       (setq ne (point-max))
1132       (widen)
1133       (put-text-property nb ne 'mime-view-entity entity)
1134       (put-text-property nb ne 'mime-view-situation situation)
1135       (put-text-property nbb ne 'mime-view-entity-body entity)
1136       (goto-char ne)
1137       (if (and children body-is-visible)
1138           (let ((body-presentation-method
1139                  (cdr (assq 'body-presentation-method situation))))
1140             (if (functionp body-presentation-method)
1141                 (funcall body-presentation-method entity situation)
1142               (mime-display-multipart/mixed entity situation))))
1143       )))
1144
1145
1146 ;;; @ MIME viewer mode
1147 ;;;
1148
1149 (defconst mime-view-menu-title "MIME-View")
1150 (defconst mime-view-menu-list
1151   '((up          "Move to upper entity"    mime-preview-move-to-upper)
1152     (previous    "Move to previous entity" mime-preview-move-to-previous)
1153     (next        "Move to next entity"     mime-preview-move-to-next)
1154     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
1155     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
1156     (play        "Play current entity"     mime-preview-play-current-entity)
1157     (extract     "Extract current entity"  mime-preview-extract-current-entity)
1158     (print       "Print current entity"    mime-preview-print-current-entity)
1159     )
1160   "Menu for MIME Viewer")
1161
1162 (cond ((featurep 'xemacs)
1163        (defvar mime-view-xemacs-popup-menu
1164          (cons mime-view-menu-title
1165                (mapcar (function
1166                         (lambda (item)
1167                           (vector (nth 1 item)(nth 2 item) t)
1168                           ))
1169                        mime-view-menu-list)))
1170        (defun mime-view-xemacs-popup-menu (event)
1171          "Popup the menu in the MIME Viewer buffer"
1172          (interactive "e")
1173          (select-window (event-window event))
1174          (set-buffer (event-buffer event))
1175          (popup-menu 'mime-view-xemacs-popup-menu))
1176        (defvar mouse-button-2 'button2)
1177        (defvar mouse-button-3 'button3)
1178        )
1179       (t
1180        (defvar mime-view-popup-menu 
1181          (let ((menu (make-sparse-keymap mime-view-menu-title)))
1182            (nconc menu
1183                   (mapcar (function
1184                            (lambda (item)
1185                              (list (intern (nth 1 item)) 'menu-item 
1186                                    (nth 1 item)(nth 2 item))
1187                              ))
1188                           mime-view-menu-list))))
1189        (defun mime-view-popup-menu (event)
1190          "Popup the menu in the MIME Viewer buffer"
1191          (interactive "@e")
1192          (let ((menu mime-view-popup-menu) events func)
1193            (setq events (x-popup-menu t menu))
1194            (and events
1195                 (setq func (lookup-key menu (apply #'vector events)))
1196                 (commandp func)
1197                 (funcall func))))
1198        (defvar mouse-button-2 [mouse-2])
1199        (defvar mouse-button-3 [mouse-3])
1200        ))
1201
1202 (defun mime-view-define-keymap (&optional default)
1203   (let ((mime-view-mode-map (if (keymapp default)
1204                                 (copy-keymap default)
1205                               (make-sparse-keymap))))
1206     (define-key mime-view-mode-map
1207       "u"        (function mime-preview-move-to-upper))
1208     (define-key mime-view-mode-map
1209       "p"        (function mime-preview-move-to-previous))
1210     (define-key mime-view-mode-map
1211       "n"        (function mime-preview-move-to-next))
1212     (define-key mime-view-mode-map
1213       "\e\t"     (function mime-preview-move-to-previous))
1214     (define-key mime-view-mode-map
1215       "\t"       (function mime-preview-move-to-next))
1216     (define-key mime-view-mode-map
1217       " "        (function mime-preview-scroll-up-entity))
1218     (define-key mime-view-mode-map
1219       "\M- "     (function mime-preview-scroll-down-entity))
1220     (define-key mime-view-mode-map
1221       "\177"     (function mime-preview-scroll-down-entity))
1222     (define-key mime-view-mode-map
1223       "\C-m"     (function mime-preview-next-line-entity))
1224     (define-key mime-view-mode-map
1225       "\C-\M-m"  (function mime-preview-previous-line-entity))
1226     (define-key mime-view-mode-map
1227       "v"        (function mime-preview-play-current-entity))
1228     (define-key mime-view-mode-map
1229       "e"        (function mime-preview-extract-current-entity))
1230     (define-key mime-view-mode-map
1231       "\C-c\C-p" (function mime-preview-print-current-entity))
1232
1233     (define-key mime-view-mode-map
1234       "\C-c\C-t\C-f" (function mime-preview-toggle-header))
1235     (define-key mime-view-mode-map
1236       "\C-c\C-th" (function mime-preview-toggle-header))
1237     (define-key mime-view-mode-map
1238       "\C-c\C-t\C-c" (function mime-preview-toggle-content))
1239
1240     (define-key mime-view-mode-map
1241       "\C-c\C-v\C-f" (function mime-preview-show-header))
1242     (define-key mime-view-mode-map
1243       "\C-c\C-vh" (function mime-preview-show-header))
1244     (define-key mime-view-mode-map
1245       "\C-c\C-v\C-c" (function mime-preview-show-content))
1246
1247     (define-key mime-view-mode-map
1248       "\C-c\C-d\C-f" (function mime-preview-hide-header))
1249     (define-key mime-view-mode-map
1250       "\C-c\C-dh" (function mime-preview-hide-header))
1251     (define-key mime-view-mode-map
1252       "\C-c\C-d\C-c" (function mime-preview-hide-content))
1253
1254     (define-key mime-view-mode-map
1255       "a"        (function mime-preview-follow-current-entity))
1256     (define-key mime-view-mode-map
1257       "q"        (function mime-preview-quit))
1258     (define-key mime-view-mode-map
1259       "\C-c\C-x" (function mime-preview-kill-buffer))
1260     ;; (define-key mime-view-mode-map
1261     ;;   "<"        (function beginning-of-buffer))
1262     ;; (define-key mime-view-mode-map
1263     ;;   ">"        (function end-of-buffer))
1264     (define-key mime-view-mode-map
1265       "?"        (function describe-mode))
1266     (define-key mime-view-mode-map
1267       [tab] (function mime-preview-move-to-next))
1268     (define-key mime-view-mode-map
1269       [delete] (function mime-preview-scroll-down-entity))
1270     (define-key mime-view-mode-map
1271       [backspace] (function mime-preview-scroll-down-entity))
1272     (if (functionp default)
1273         (cond ((featurep 'xemacs)
1274                (set-keymap-default-binding mime-view-mode-map default)
1275                )
1276               (t
1277                (setq mime-view-mode-map
1278                      (append mime-view-mode-map (list (cons t default))))
1279                )))
1280     (if mouse-button-2
1281         (define-key mime-view-mode-map
1282           mouse-button-2 (function mime-button-dispatcher))
1283       )
1284     (cond ((featurep 'xemacs)
1285            (define-key mime-view-mode-map
1286              mouse-button-3 (function mime-view-xemacs-popup-menu))
1287            )
1288           ((>= emacs-major-version 19)
1289            (define-key mime-view-mode-map
1290              mouse-button-3 (function mime-view-popup-menu))
1291            (define-key mime-view-mode-map [menu-bar mime-view]
1292              (cons mime-view-menu-title
1293                    (make-sparse-keymap mime-view-menu-title)))
1294            (mapcar (function
1295                     (lambda (item)
1296                       (define-key mime-view-mode-map
1297                         (vector 'menu-bar 'mime-view (car item))
1298                         (cons (nth 1 item)(nth 2 item)))
1299                       ))
1300                    (reverse mime-view-menu-list))
1301            ))
1302     ;; (run-hooks 'mime-view-define-keymap-hook)
1303     mime-view-mode-map))
1304
1305 (defvar mime-view-mode-default-map (mime-view-define-keymap))
1306
1307
1308 (defsubst mime-maybe-hide-echo-buffer ()
1309   "Clear mime-echo buffer and delete window for it."
1310   (let ((buf (get-buffer mime-echo-buffer-name)))
1311     (if buf
1312         (save-excursion
1313           (set-buffer buf)
1314           (erase-buffer)
1315           (let ((win (get-buffer-window buf)))
1316             (if win
1317                 (delete-window win)
1318               ))
1319           (bury-buffer buf)
1320           ))))
1321
1322 (defvar mime-view-redisplay nil)
1323
1324 ;;;###autoload
1325 (defun mime-display-message (message &optional preview-buffer
1326                                      mother default-keymap-or-function
1327                                      original-major-mode keymap)
1328   "View MESSAGE in MIME-View mode.
1329
1330 Optional argument PREVIEW-BUFFER specifies the buffer of the
1331 presentation.  It must be either nil or a name of preview buffer.
1332
1333 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1334
1335 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1336 function.  If it is a keymap, keymap of MIME-View mode will be added
1337 to it.  If it is a function, it will be bound as default binding of
1338 keymap of MIME-View mode.
1339
1340 Optional argument ORIGINAL-MAJOR-MODE is major-mode of representation
1341 buffer of MESSAGE.  If it is nil, current `major-mode' is used.
1342
1343 Optional argument KEYMAP is keymap of MIME-View mode.  If it is
1344 non-nil, DEFAULT-KEYMAP-OR-FUNCTION is ignored.  If it is nil,
1345 `mime-view-mode-default-map' is used."
1346   (mime-maybe-hide-echo-buffer)
1347   (let ((win-conf (current-window-configuration)))
1348     (or preview-buffer
1349         (setq preview-buffer
1350               (concat "*Preview-" (mime-entity-name message) "*")))
1351     (or original-major-mode
1352         (setq original-major-mode major-mode))
1353     (let ((inhibit-read-only t))
1354       (set-buffer (get-buffer-create preview-buffer))
1355       (widen)
1356       (erase-buffer)
1357       (if mother
1358           (setq mime-mother-buffer mother))
1359       (setq mime-preview-original-window-configuration win-conf)
1360       (setq major-mode 'mime-view-mode)
1361       (setq mode-name "MIME-View")
1362       (mime-display-entity message nil
1363                            `((entity-button . invisible)
1364                              (header . visible)
1365                              (major-mode . ,original-major-mode))
1366                            preview-buffer)
1367       (use-local-map
1368        (or keymap
1369            (if default-keymap-or-function
1370                (mime-view-define-keymap default-keymap-or-function)
1371              mime-view-mode-default-map)))
1372       (let ((point
1373              (next-single-property-change (point-min) 'mime-view-entity)))
1374         (if point
1375             (goto-char point)
1376           (goto-char (point-min))
1377           (search-forward "\n\n" nil t)))
1378       (run-hooks 'mime-view-mode-hook)
1379       (set-buffer-modified-p nil)
1380       (setq buffer-read-only t)
1381       preview-buffer)))
1382
1383 ;;;###autoload
1384 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1385                                    default-keymap-or-function
1386                                    representation-type)
1387   "View RAW-BUFFER in MIME-View mode.
1388 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1389 buffer.
1390 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1391 function.  If it is a keymap, keymap of MIME-View mode will be added
1392 to it.  If it is a function, it will be bound as default binding of
1393 keymap of MIME-View mode.
1394 Optional argument REPRESENTATION-TYPE is representation-type of
1395 message.  It must be nil, `binary' or `cooked'.  If it is nil,
1396 `cooked' is used as default."
1397   (interactive)
1398   (or raw-buffer
1399       (setq raw-buffer (current-buffer)))
1400   (or representation-type
1401       (setq representation-type
1402             (save-excursion
1403               (set-buffer raw-buffer)
1404               (cdr (or (assq major-mode mime-raw-representation-type-alist)
1405                        (assq t mime-raw-representation-type-alist)))
1406               )))
1407   (if (eq representation-type 'binary)
1408       (setq representation-type 'buffer)
1409     )
1410   (setq preview-buffer (mime-display-message
1411                         (mime-open-entity representation-type raw-buffer)
1412                         preview-buffer mother default-keymap-or-function))
1413   (or (get-buffer-window preview-buffer)
1414       (let ((r-win (get-buffer-window raw-buffer)))
1415         (if r-win
1416             (set-window-buffer r-win preview-buffer)
1417           (let ((m-win (and mother (get-buffer-window mother))))
1418             (if m-win
1419                 (set-window-buffer m-win preview-buffer)
1420               (switch-to-buffer preview-buffer)
1421               ))))))
1422
1423 (defun mime-view-mode (&optional mother ctl encoding
1424                                  raw-buffer preview-buffer
1425                                  default-keymap-or-function)
1426   "Major mode for viewing MIME message.
1427
1428 Here is a list of the standard keys for mime-view-mode.
1429
1430 key             feature
1431 ---             -------
1432
1433 u               Move to upper content
1434 p or M-TAB      Move to previous content
1435 n or TAB        Move to next content
1436 SPC             Scroll up or move to next content
1437 M-SPC or DEL    Scroll down or move to previous content
1438 RET             Move to next line
1439 M-RET           Move to previous line
1440 v               Decode current content as `play mode'
1441 e               Decode current content as `extract mode'
1442 C-c C-p         Decode current content as `print mode'
1443 a               Followup to current content.
1444 q               Quit
1445 button-2        Move to point under the mouse cursor
1446                 and decode current content as `play mode'
1447 "
1448   (interactive)
1449   (unless mime-view-redisplay
1450     (save-excursion
1451       (if raw-buffer (set-buffer raw-buffer))
1452       (let ((type
1453              (cdr
1454               (or (assq major-mode mime-raw-representation-type-alist)
1455                   (assq t mime-raw-representation-type-alist)))))
1456         (if (eq type 'binary)
1457             (setq type 'buffer)
1458           )
1459         (setq mime-message-structure (mime-open-entity type raw-buffer))
1460         (or (mime-entity-content-type mime-message-structure)
1461             (mime-entity-set-content-type mime-message-structure ctl))
1462         )
1463       (or (mime-entity-encoding mime-message-structure)
1464           (mime-entity-set-encoding mime-message-structure encoding))
1465       ))
1466   (mime-display-message mime-message-structure preview-buffer
1467                         mother default-keymap-or-function)
1468   )
1469
1470
1471 ;;; @@ utility
1472 ;;;
1473
1474 (defun mime-preview-find-boundary-info (&optional with-children)
1475   "Return boundary information of current part.
1476 If WITH-CHILDREN, refer boundary surrounding current part and its branches."
1477   (let (entity
1478         p-beg p-end
1479         entity-node-id len)
1480     (while (null (setq entity
1481                        (get-text-property (point) 'mime-view-entity)))
1482       (backward-char))
1483     (setq p-beg (previous-single-property-change (point) 'mime-view-entity))
1484     (setq entity-node-id (mime-entity-node-id entity))
1485     (setq len (length entity-node-id))
1486     (cond ((null p-beg)
1487            (setq p-beg
1488                  (if (eq (next-single-property-change (point-min)
1489                                                       'mime-view-entity)
1490                          (point))
1491                      (point)
1492                    (point-min)))
1493            )
1494           ((eq (next-single-property-change p-beg 'mime-view-entity)
1495                (point))
1496            (setq p-beg (point))
1497            ))
1498     (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1499     (cond ((null p-end)
1500            (setq p-end (point-max))
1501            )
1502           ((null entity-node-id)
1503            (setq p-end (point-max))
1504            )
1505           (with-children
1506            (save-excursion
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 (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 (or (next-single-property-change
1520                                     (point) 'mime-view-entity)
1521                                    (point-max)))))
1522                (setq p-end (point-max))))
1523            ))
1524     (vector p-beg p-end entity)))
1525
1526
1527 ;;; @@ playing
1528 ;;;
1529
1530 (autoload 'mime-preview-play-current-entity "mime-play"
1531   "Play current entity." t)
1532
1533 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1534   "Extract current entity into file (maybe).
1535 It decodes current entity to call internal or external method as
1536 \"extract\" mode.  The method is selected from variable
1537 `mime-acting-condition'."
1538   (interactive "P")
1539   (mime-preview-play-current-entity ignore-examples "extract")
1540   )
1541
1542 (defun mime-preview-print-current-entity (&optional ignore-examples)
1543   "Print current entity (maybe).
1544 It decodes current entity to call internal or external method as
1545 \"print\" mode.  The method is selected from variable
1546 `mime-acting-condition'."
1547   (interactive "P")
1548   (mime-preview-play-current-entity ignore-examples "print")
1549   )
1550
1551
1552 ;;; @@ following
1553 ;;;
1554
1555 (defun mime-preview-follow-current-entity ()
1556   "Write follow message to current entity.
1557 It calls following-method selected from variable
1558 `mime-preview-following-method-alist'."
1559   (interactive)
1560   (let ((entity (mime-preview-find-boundary-info t))
1561         p-beg p-end
1562         pb-beg)
1563     (setq p-beg (aref entity 0)
1564           p-end (aref entity 1)
1565           entity (aref entity 2))
1566     (if (get-text-property p-beg 'mime-view-entity-body)
1567         (setq pb-beg p-beg)
1568       (setq pb-beg
1569             (next-single-property-change
1570              p-beg 'mime-view-entity-body nil
1571              (or (next-single-property-change p-beg 'mime-view-entity)
1572                  p-end))))
1573     (let* ((mode (mime-preview-original-major-mode 'recursive))
1574            (entity-node-id (mime-entity-node-id entity))
1575            (new-name
1576             (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1577            new-buf
1578            (the-buf (current-buffer))
1579            fields)
1580       (save-excursion
1581         (set-buffer (setq new-buf (get-buffer-create new-name)))
1582         (erase-buffer)
1583         (insert ?\n)
1584         (insert-buffer-substring the-buf pb-beg p-end)
1585         (goto-char (point-min))
1586         (let ((current-entity
1587                (if (and (eq (mime-entity-media-type entity) 'message)
1588                         (eq (mime-entity-media-subtype entity) 'rfc822))
1589                    (car (mime-entity-children entity))
1590                  entity)))
1591           (while (and current-entity
1592                       (if (and (eq (mime-entity-media-type
1593                                     current-entity) 'message)
1594                                (eq (mime-entity-media-subtype
1595                                     current-entity) 'rfc822))
1596                           nil
1597                         (mime-insert-header current-entity fields)
1598                         t))
1599             (setq fields (std11-collect-field-names)
1600                   current-entity (mime-entity-parent current-entity))
1601             ))
1602         (let ((rest mime-view-following-required-fields-list)
1603               field-name ret)
1604           (while rest
1605             (setq field-name (car rest))
1606             (or (std11-field-body field-name)
1607                 (progn
1608                   (save-excursion
1609                     (set-buffer the-buf)
1610                     (let ((entity (when mime-mother-buffer
1611                                     (set-buffer mime-mother-buffer)
1612                                     (get-text-property (point)
1613                                                        'mime-view-entity))))
1614                       (while (and entity
1615                                   (null (setq ret (mime-entity-fetch-field
1616                                                    entity field-name))))
1617                         (setq entity (mime-entity-parent entity)))))
1618                   (if ret
1619                       (insert (concat field-name ": " ret "\n"))
1620                     )))
1621             (setq rest (cdr rest))
1622             ))
1623         )
1624       (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1625         (if (functionp f)
1626             (funcall f new-buf)
1627           (message
1628            "Sorry, following method for %s is not implemented yet."
1629            mode)
1630           ))
1631       )))
1632
1633
1634 ;;; @@ moving
1635 ;;;
1636
1637 (defun mime-preview-move-to-upper ()
1638   "Move to upper entity.
1639 If there is no upper entity, call function `mime-preview-quit'."
1640   (interactive)
1641   (let (cinfo)
1642     (while (null (setq cinfo
1643                        (get-text-property (point) 'mime-view-entity)))
1644       (backward-char)
1645       )
1646     (let ((r (mime-entity-parent cinfo))
1647           point)
1648       (catch 'tag
1649         (while (setq point (previous-single-property-change
1650                             (point) 'mime-view-entity))
1651           (goto-char point)
1652           (when (eq r (get-text-property (point) 'mime-view-entity))
1653             (if (or (eq mime-preview-move-scroll t)
1654                     (and mime-preview-move-scroll
1655                          (>= point
1656                              (save-excursion
1657                                (move-to-window-line -1)
1658                                (forward-line (* -1 next-screen-context-lines))
1659                                (beginning-of-line)
1660                                (point)))))
1661                 (recenter next-screen-context-lines))
1662             (throw 'tag t)
1663             )
1664           )
1665         (mime-preview-quit)
1666         ))))
1667
1668 (defun mime-preview-move-to-previous ()
1669   "Move to previous entity.
1670 If there is no previous entity, it calls function registered in
1671 variable `mime-preview-over-to-previous-method-alist'."
1672   (interactive)
1673   (while (and (not (bobp))
1674               (null (get-text-property (point) 'mime-view-entity)))
1675     (backward-char)
1676     )
1677   (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1678     (if (and point
1679              (>= point (point-min)))
1680         (if (get-text-property (1- point) 'mime-view-entity)
1681             (progn (goto-char point)
1682                    (if
1683                     (or (eq mime-preview-move-scroll t)
1684                         (and mime-preview-move-scroll
1685                              (<= point
1686                                 (save-excursion
1687                                   (move-to-window-line 0)
1688                                   (forward-line next-screen-context-lines)
1689                                   (end-of-line)
1690                                   (point)))))
1691                         (recenter (* -1 next-screen-context-lines))))
1692           (goto-char (1- point))
1693           (mime-preview-move-to-previous)
1694           )
1695       (let ((f (assq (mime-preview-original-major-mode)
1696                      mime-preview-over-to-previous-method-alist)))
1697         (if f
1698             (funcall (cdr f))
1699           ))
1700       )))
1701
1702 (defun mime-preview-move-to-next ()
1703   "Move to next entity.
1704 If there is no previous entity, it calls function registered in
1705 variable `mime-preview-over-to-next-method-alist'."
1706   (interactive)
1707   (while (and (not (eobp))
1708               (null (get-text-property (point) 'mime-view-entity)))
1709     (forward-char)
1710     )
1711   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1712     (if (and point
1713              (<= point (point-max)))
1714         (progn
1715           (goto-char point)
1716           (if (null (get-text-property point 'mime-view-entity))
1717               (mime-preview-move-to-next)
1718             (and
1719              (or (eq mime-preview-move-scroll t)
1720                  (and mime-preview-move-scroll
1721                       (>= point
1722                          (save-excursion
1723                            (move-to-window-line -1)
1724                            (forward-line
1725                             (* -1 next-screen-context-lines))
1726                            (beginning-of-line)
1727                            (point)))))
1728                  (recenter next-screen-context-lines))
1729             ))
1730       (let ((f (assq (mime-preview-original-major-mode)
1731                      mime-preview-over-to-next-method-alist)))
1732         (if f
1733             (funcall (cdr f))
1734           ))
1735       )))
1736
1737 (defun mime-preview-scroll-up-entity (&optional h)
1738   "Scroll up current entity.
1739 If reached to (point-max), it calls function registered in variable
1740 `mime-preview-over-to-next-method-alist'."
1741   (interactive)
1742   (if (eobp)
1743       (let ((f (assq (mime-preview-original-major-mode)
1744                      mime-preview-over-to-next-method-alist)))
1745         (if f
1746             (funcall (cdr f))
1747           ))
1748     (let ((point
1749            (or (next-single-property-change (point) 'mime-view-entity)
1750                (point-max)))
1751           (bottom (window-end (selected-window))))
1752       (if (and (not h)
1753                (> bottom point))
1754           (progn (goto-char point)
1755                  (recenter next-screen-context-lines))
1756         (condition-case nil
1757             (scroll-up h)
1758           (end-of-buffer
1759            (goto-char (point-max)))))
1760       )))
1761
1762 (defun mime-preview-scroll-down-entity (&optional h)
1763   "Scroll down current entity.
1764 If reached to (point-min), it calls function registered in variable
1765 `mime-preview-over-to-previous-method-alist'."
1766   (interactive)
1767   (if (bobp)
1768       (let ((f (assq (mime-preview-original-major-mode)
1769                      mime-preview-over-to-previous-method-alist)))
1770         (if f
1771             (funcall (cdr f))
1772           ))
1773     (let ((point
1774            (or (previous-single-property-change (point) 'mime-view-entity)
1775                (point-min)))
1776           (top (window-start (selected-window))))
1777       (if (and (not h)
1778                (< top point))
1779           (progn (goto-char point)
1780                  (recenter (* -1 next-screen-context-lines)))
1781         (condition-case nil
1782             (scroll-down h)
1783           (beginning-of-buffer
1784            (goto-char (point-min)))))
1785       )))
1786
1787 (defun mime-preview-next-line-entity (&optional lines)
1788   "Scroll up one line (or prefix LINES lines).
1789 If LINES is negative, scroll down LINES lines."
1790   (interactive "p")
1791   (mime-preview-scroll-up-entity (or lines 1))
1792   )
1793
1794 (defun mime-preview-previous-line-entity (&optional lines)
1795   "Scrroll down one line (or prefix LINES lines).
1796 If LINES is negative, scroll up LINES lines."
1797   (interactive "p")
1798   (mime-preview-scroll-down-entity (or lines 1))
1799   )
1800
1801
1802 ;;; @@ display
1803 ;;;
1804
1805 (defun mime-preview-toggle-display (type &optional display)
1806   (let ((situation (mime-preview-find-boundary-info t))
1807         (sym (intern (concat "*" (symbol-name type))))
1808         entity p-beg p-end)
1809     (setq p-beg (aref situation 0)
1810           p-end (aref situation 1)
1811           entity (aref situation 2)
1812           situation (get-text-property p-beg 'mime-view-situation))
1813     (cond ((eq display 'invisible)
1814            (setq display nil))
1815           (display)
1816           (t
1817            (setq display
1818                  (eq (cdr (or (assq sym situation)
1819                               (assq type situation)))
1820                      'invisible))))
1821     (setq situation (put-alist sym (if display
1822                                        'visible
1823                                      'invisible)
1824                                situation))
1825     (save-excursion
1826       (let ((inhibit-read-only t))
1827         (delete-region p-beg p-end)
1828         (mime-display-entity entity situation)))
1829     (let ((ret (assoc situation mime-preview-situation-example-list)))
1830       (if ret
1831           (setcdr ret (1+ (cdr ret)))
1832         (add-to-list 'mime-preview-situation-example-list
1833                      (cons situation 0))))))
1834
1835 (defun mime-preview-toggle-header (&optional force-visible)
1836   (interactive "P")
1837   (mime-preview-toggle-display 'header force-visible))
1838
1839 (defun mime-preview-toggle-content (&optional force-visible)
1840   (interactive "P")
1841   (mime-preview-toggle-display 'body force-visible))
1842
1843 (defun mime-preview-show-header ()
1844   (interactive)
1845   (mime-preview-toggle-display 'header 'visible))
1846
1847 (defun mime-preview-show-content ()
1848   (interactive)
1849   (mime-preview-toggle-display 'body 'visible))
1850
1851 (defun mime-preview-hide-header ()
1852   (interactive)
1853   (mime-preview-toggle-display 'header 'invisible))
1854
1855 (defun mime-preview-hide-content ()
1856   (interactive)
1857   (mime-preview-toggle-display 'body 'invisible))
1858
1859     
1860 ;;; @@ quitting
1861 ;;;
1862
1863 (defun mime-preview-quit ()
1864   "Quit from MIME-preview buffer.
1865 It calls function registered in variable
1866 `mime-preview-quitting-method-alist'."
1867   (interactive)
1868   (let ((r (assq (mime-preview-original-major-mode)
1869                  mime-preview-quitting-method-alist)))
1870     (if r
1871         (funcall (cdr r))
1872       )))
1873
1874 (defun mime-preview-kill-buffer ()
1875   (interactive)
1876   (kill-buffer (current-buffer))
1877   )
1878
1879
1880 ;;; @ end
1881 ;;;
1882
1883 (provide 'mime-view)
1884
1885 (eval-when-compile
1886   (setq mime-situation-examples-file nil)
1887   ;; to avoid to read situation-examples-file at compile time.
1888   )
1889
1890 (mime-view-read-situation-examples-file)
1891
1892 ;;; mime-view.el ends here