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