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