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