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