Merge semi-1_10_2.
[elisp/semi.git] / mime-view.el
1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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 (Sophisticated Emacs 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 'mailcap)
35
36
37 ;;; @ version
38 ;;;
39
40 (defconst mime-view-version
41   (eval-when-compile
42     (concat (mime-product-name mime-user-interface-product) " MIME-View "
43             (mapconcat #'number-to-string
44                        (mime-product-version mime-user-interface-product) ".")
45             " (" (mime-product-code-name mime-user-interface-product) ")")))
46
47
48 ;;; @ variables
49 ;;;
50
51 (defgroup mime-view nil
52   "MIME view mode"
53   :group 'mime)
54
55 (defcustom mime-view-find-every-acting-situation t
56   "*Find every available acting-situation if non-nil."
57   :group 'mime-view
58   :type 'boolean)
59
60 (defcustom mime-acting-situation-examples-file "~/.mime-example"
61   "*File name of example about acting-situation demonstrated by user."
62   :group 'mime-view
63   :type 'file)
64
65
66 ;;; @ in raw-buffer (representation space)
67 ;;;
68
69 (defvar mime-preview-buffer nil
70   "MIME-preview buffer corresponding with the (raw) buffer.")
71 (make-variable-buffer-local 'mime-preview-buffer)
72
73
74 (defvar mime-raw-representation-type-alist
75   '((mime-show-message-mode     . binary)
76     (mime-temp-message-mode     . binary)
77     (t                          . cooked)
78     )
79   "Alist of major-mode vs. representation-type of mime-raw-buffer.
80 Each element looks like (SYMBOL . REPRESENTATION-TYPE).  SYMBOL is
81 major-mode or t.  t means default.  REPRESENTATION-TYPE must be
82 `binary' or `cooked'.")
83
84
85 (defun mime-raw-find-entity-from-point (point &optional message-info)
86   "Return entity from POINT in mime-raw-buffer.
87 If optional argument MESSAGE-INFO is not specified,
88 `mime-message-structure' is used."
89   (or message-info
90       (setq message-info mime-message-structure))
91   (if (and (<= (mime-entity-point-min message-info) point)
92            (<= point (mime-entity-point-max message-info)))
93       (let ((children (mime-entity-children message-info)))
94         (catch 'tag
95           (while children
96             (let ((ret
97                    (mime-raw-find-entity-from-point point (car children))))
98               (if ret
99                   (throw 'tag ret)
100                 ))
101             (setq children (cdr children)))
102           message-info))))
103
104
105 ;;; @ in preview-buffer (presentation space)
106 ;;;
107
108 (defvar mime-mother-buffer nil
109   "Mother buffer corresponding with the (MIME-preview) buffer.
110 If current MIME-preview buffer is generated by other buffer, such as
111 message/partial, it is called `mother-buffer'.")
112 (make-variable-buffer-local 'mime-mother-buffer)
113
114 (defvar mime-raw-buffer nil
115   "Raw buffer corresponding with the (MIME-preview) buffer.")
116 (make-variable-buffer-local 'mime-raw-buffer)
117
118 (defvar mime-preview-original-window-configuration nil
119   "Window-configuration before mime-view-mode is called.")
120 (make-variable-buffer-local 'mime-preview-original-window-configuration)
121
122 (defun mime-preview-original-major-mode (&optional recursive)
123   "Return major-mode of original buffer.
124 If optional argument RECURSIVE is non-nil and current buffer has
125 mime-mother-buffer, it returns original major-mode of the
126 mother-buffer."
127   (if (and recursive mime-mother-buffer)
128       (save-excursion
129         (set-buffer mime-mother-buffer)
130         (mime-preview-original-major-mode recursive)
131         )
132     (save-excursion
133       (set-buffer
134        (mime-entity-buffer
135         (get-text-property (point-min) 'mime-view-entity)))
136       major-mode)))
137
138
139 ;;; @ entity information
140 ;;;
141
142 (defun mime-entity-situation (entity)
143   "Return situation of ENTITY."
144   (append (or (mime-entity-content-type entity)
145               (make-mime-content-type 'text 'plain))
146           (let ((d (mime-entity-content-disposition entity)))
147             (cons (cons 'disposition-type
148                         (mime-content-disposition-type d))
149                   (mapcar (function
150                            (lambda (param)
151                              (let ((name (car param)))
152                                (cons (cond ((string= name "filename")
153                                             'filename)
154                                            ((string= name "creation-date")
155                                             'creation-date)
156                                            ((string= name "modification-date")
157                                             'modification-date)
158                                            ((string= name "read-date")
159                                             'read-date)
160                                            ((string= name "size")
161                                             'size)
162                                            (t (cons 'disposition (car param))))
163                                      (cdr param)))))
164                           (mime-content-disposition-parameters d))
165                   ))
166           (list (cons 'encoding (mime-entity-encoding entity))
167                 (cons 'major-mode
168                       (save-excursion
169                         (set-buffer (mime-entity-buffer entity))
170                         major-mode)))
171           ))
172
173
174 (defun mime-view-entity-title (entity)
175   (or (mime-read-field 'Content-Description entity)
176       (mime-read-field 'Subject entity)
177       (mime-entity-filename entity)
178       ""))
179
180
181 (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
182   "Return entity-node-id from POINT in mime-raw-buffer.
183 If optional argument MESSAGE-INFO is not specified,
184 `mime-message-structure' is used."
185   (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
186
187 (defsubst mime-raw-point-to-entity-number (point &optional message-info)
188   "Return entity-number from POINT in mime-raw-buffer.
189 If optional argument MESSAGE-INFO is not specified,
190 `mime-message-structure' is used."
191   (mime-entity-number (mime-raw-find-entity-from-point point message-info)))
192
193 (defun mime-raw-flatten-message-info (&optional message-info)
194   "Return list of entity in mime-raw-buffer.
195 If optional argument MESSAGE-INFO is not specified,
196 `mime-message-structure' is used."
197   (or message-info
198       (setq message-info mime-message-structure))
199   (let ((dest (list message-info))
200         (rcl (mime-entity-children message-info)))
201     (while rcl
202       (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
203       (setq rcl (cdr rcl)))
204     dest))
205
206
207 ;;; @ presentation of preview
208 ;;;
209
210 ;;; @@ entity-button
211 ;;;
212
213 ;;; @@@ predicate function
214 ;;;
215
216 (defun mime-view-entity-button-visible-p (entity)
217   "Return non-nil if header of ENTITY is visible.
218 Please redefine this function if you want to change default setting."
219   (let ((media-type (mime-entity-media-type entity))
220         (media-subtype (mime-entity-media-subtype entity)))
221     (or (not (eq media-type 'application))
222         (and (not (eq media-subtype 'x-selection))
223              (or (not (eq media-subtype 'octet-stream))
224                  (let ((mother-entity (mime-entity-parent entity)))
225                    (or (not (eq (mime-entity-media-type mother-entity)
226                                 'multipart))
227                        (not (eq (mime-entity-media-subtype mother-entity)
228                                 'encrypted)))
229                    )
230                  )))))
231
232 ;;; @@@ entity button generator
233 ;;;
234
235 (defun mime-view-insert-entity-button (entity)
236   "Insert entity-button of ENTITY."
237   (let ((entity-node-id (mime-entity-node-id entity))
238         (params (mime-entity-parameters entity))
239         (subject (mime-view-entity-title entity)))
240     (mime-insert-button
241      (let ((access-type (assoc "access-type" params))
242            (num (or (cdr (assoc "x-part-number" params))
243                     (if (consp entity-node-id)
244                         (mapconcat (function
245                                     (lambda (num)
246                                       (format "%s" (1+ num))
247                                       ))
248                                    (reverse entity-node-id) ".")
249                       "0"))
250                 ))
251        (cond (access-type
252               (let ((server (assoc "server" params)))
253                 (setq access-type (cdr access-type))
254                 (if server
255                     (format "%s %s ([%s] %s)"
256                             num subject access-type (cdr server))
257                 (let ((site (cdr (assoc "site" params)))
258                       (dir (cdr (assoc "directory" params)))
259                       )
260                   (format "%s %s ([%s] %s:%s)"
261                           num subject access-type site dir)
262                   )))
263             )
264            (t
265             (let ((media-type (mime-entity-media-type entity))
266                   (media-subtype (mime-entity-media-subtype entity))
267                   (charset (cdr (assoc "charset" params)))
268                   (encoding (mime-entity-encoding entity)))
269               (concat
270                num " " subject
271                (let ((rest
272                       (format " <%s/%s%s%s>"
273                               media-type media-subtype
274                               (if charset
275                                   (concat "; " charset)
276                                 "")
277                               (if encoding
278                                   (concat " (" encoding ")")
279                                 ""))))
280                  (if (>= (+ (current-column)(length rest))(window-width))
281                      "\n\t")
282                  rest)))
283             )))
284      (function mime-preview-play-current-entity))
285     ))
286
287
288 ;;; @@ entity-header
289 ;;;
290
291 (defvar mime-header-presentation-method-alist nil
292   "Alist of major mode vs. corresponding header-presentation-method functions.
293 Each element looks like (SYMBOL . FUNCTION).
294 SYMBOL must be major mode in raw-buffer or t.  t means default.
295 Interface of FUNCTION must be (ENTITY SITUATION).")
296
297 (defvar mime-view-ignored-field-list
298   '(".*Received:" ".*Path:" ".*Id:" "^References:"
299     "^Replied:" "^Errors-To:"
300     "^Lines:" "^Sender:" ".*Host:" "^Xref:"
301     "^Content-Type:" "^Precedence:"
302     "^Status:" "^X-VM-.*:")
303   "All fields that match this list will be hidden in MIME preview buffer.
304 Each elements are regexp of field-name.")
305
306 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
307   "All fields that match this list will be displayed in MIME preview buffer.
308 Each elements are regexp of field-name.")
309
310
311 ;;; @@ entity-body
312 ;;;
313
314 ;;; @@@ predicate function
315 ;;;
316
317 (defun mime-calist::field-match-method-as-default-rule (calist
318                                                         field-type field-value)
319   (let ((s-field (assq field-type calist)))
320     (cond ((null s-field)
321            (cons (cons field-type field-value) calist)
322            )
323           (t calist))))
324
325 (define-calist-field-match-method
326   'header #'mime-calist::field-match-method-as-default-rule)
327
328 (define-calist-field-match-method
329   'body #'mime-calist::field-match-method-as-default-rule)
330
331
332 (defvar mime-preview-condition nil
333   "Condition-tree about how to display entity.")
334
335 (ctree-set-calist-strictly
336  'mime-preview-condition '((type . application)(subtype . octet-stream)
337                            (encoding . nil)
338                            (body . visible)))
339 (ctree-set-calist-strictly
340  'mime-preview-condition '((type . application)(subtype . octet-stream)
341                            (encoding . "7bit")
342                            (body . visible)))
343 (ctree-set-calist-strictly
344  'mime-preview-condition '((type . application)(subtype . octet-stream)
345                            (encoding . "8bit")
346                            (body . visible)))
347
348 (ctree-set-calist-strictly
349  'mime-preview-condition '((type . application)(subtype . pgp)
350                            (body . visible)))
351
352 (ctree-set-calist-strictly
353  'mime-preview-condition '((type . application)(subtype . x-latex)
354                            (body . visible)))
355
356 (ctree-set-calist-strictly
357  'mime-preview-condition '((type . application)(subtype . x-selection)
358                            (body . visible)))
359
360 (ctree-set-calist-strictly
361  'mime-preview-condition '((type . application)(subtype . x-comment)
362                            (body . visible)))
363
364 (ctree-set-calist-strictly
365  'mime-preview-condition '((type . message)(subtype . delivery-status)
366                            (body . visible)))
367
368 (ctree-set-calist-strictly
369  'mime-preview-condition
370  '((body . visible)
371    (body-presentation-method . mime-display-text/plain)))
372
373 (ctree-set-calist-strictly
374  'mime-preview-condition
375  '((type . nil)
376    (body . visible)
377    (body-presentation-method . mime-display-text/plain)))
378
379 (ctree-set-calist-strictly
380  'mime-preview-condition
381  '((type . text)(subtype . enriched)
382    (body . visible)
383    (body-presentation-method . mime-display-text/enriched)))
384
385 (ctree-set-calist-strictly
386  'mime-preview-condition
387  '((type . text)(subtype . richtext)
388    (body . visible)
389    (body-presentation-method . mime-display-text/richtext)))
390
391 (ctree-set-calist-strictly
392  'mime-preview-condition
393  '((type . text)(subtype . t)
394    (body . visible)
395    (body-presentation-method . mime-display-text/plain)))
396
397 (ctree-set-calist-strictly
398  'mime-preview-condition
399  '((type . multipart)(subtype . alternative)
400    (body . visible)
401    (body-presentation-method . mime-display-multipart/alternative)))
402
403 (ctree-set-calist-strictly
404  'mime-preview-condition '((type . message)(subtype . partial)
405                            (body-presentation-method
406                             . mime-display-message/partial-button)))
407
408 (ctree-set-calist-strictly
409  'mime-preview-condition '((type . message)(subtype . rfc822)
410                            (body-presentation-method . nil)
411                            (childrens-situation (header . visible)
412                                                 (entity-button . invisible))))
413
414 (ctree-set-calist-strictly
415  'mime-preview-condition '((type . message)(subtype . news)
416                            (body-presentation-method . nil)
417                            (childrens-situation (header . visible)
418                                                 (entity-button . invisible))))
419
420
421 ;;; @@@ entity presentation
422 ;;;
423
424 (defun mime-display-text/plain (entity situation)
425   (save-restriction
426     (narrow-to-region (point-max)(point-max))
427     (mime-insert-text-content entity)
428     (run-hooks 'mime-text-decode-hook)
429     (goto-char (point-max))
430     (if (not (eq (char-after (1- (point))) ?\n))
431         (insert "\n")
432       )
433     (mime-add-url-buttons)
434     (run-hooks 'mime-display-text/plain-hook)
435     ))
436
437 (defun mime-display-text/richtext (entity situation)
438   (save-restriction
439     (narrow-to-region (point-max)(point-max))
440     (mime-insert-text-content entity)
441     (run-hooks 'mime-text-decode-hook)
442     (let ((beg (point-min)))
443       (remove-text-properties beg (point-max) '(face nil))
444       (richtext-decode beg (point-max))
445       )))
446
447 (defun mime-display-text/enriched (entity situation)
448   (save-restriction
449     (narrow-to-region (point-max)(point-max))
450     (mime-insert-text-content entity)
451     (run-hooks 'mime-text-decode-hook)
452     (let ((beg (point-min)))
453       (remove-text-properties beg (point-max) '(face nil))
454       (enriched-decode beg (point-max))
455       )))
456
457 (defvar mime-view-announcement-for-message/partial
458   (if (and (>= emacs-major-version 19) window-system)
459       "\
460 \[[ This is message/partial style split message. ]]
461 \[[ Please press `v' key in this buffer          ]]
462 \[[ or click here by mouse button-2.             ]]"
463     "\
464 \[[ This is message/partial style split message. ]]
465 \[[ Please press `v' key in this buffer.         ]]"
466     ))
467
468 (defun mime-display-message/partial-button (&optional entity situation)
469   (save-restriction
470     (goto-char (point-max))
471     (if (not (search-backward "\n\n" nil t))
472         (insert "\n")
473       )
474     (goto-char (point-max))
475     (narrow-to-region (point-max)(point-max))
476     (insert mime-view-announcement-for-message/partial)
477     (mime-add-button (point-min)(point-max)
478                      #'mime-preview-play-current-entity)
479     ))
480
481 (defun mime-display-multipart/mixed (entity situation)
482   (let ((children (mime-entity-children entity))
483         (default-situation
484           (cdr (assq 'childrens-situation situation))))
485     (while children
486       (mime-display-entity (car children) nil default-situation)
487       (setq children (cdr children))
488       )))
489
490 (defcustom mime-view-type-subtype-score-alist
491   '(((text . enriched) . 3)
492     ((text . richtext) . 2)
493     ((text . plain)    . 1)
494     (t . 0))
495   "Alist MEDIA-TYPE vs corresponding score.
496 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
497   :group 'mime-view
498   :type '(repeat (cons (choice :tag "Media-Type"
499                                (cons :tag "Type/Subtype"
500                                      (symbol :tag "Primary-type")
501                                      (symbol :tag "Subtype"))
502                                (symbol :tag "Type")
503                                (const :tag "Default" t))
504                        integer)))
505
506 (defun mime-display-multipart/alternative (entity situation)
507   (let* ((children (mime-entity-children entity))
508          (default-situation
509            (cdr (assq 'childrens-situation situation)))
510          (i 0)
511          (p 0)
512          (max-score 0)
513          (situations
514           (mapcar (function
515                    (lambda (child)
516                      (let ((situation
517                             (or (ctree-match-calist
518                                  mime-preview-condition
519                                  (append (mime-entity-situation child)
520                                          default-situation))
521                                 default-situation)))
522                        (if (cdr (assq 'body-presentation-method situation))
523                            (let ((score
524                                   (cdr
525                                    (or (assoc
526                                         (cons
527                                          (cdr (assq 'type situation))
528                                          (cdr (assq 'subtype situation)))
529                                         mime-view-type-subtype-score-alist)
530                                        (assq
531                                         (cdr (assq 'type situation))
532                                         mime-view-type-subtype-score-alist)
533                                        (assq
534                                         t
535                                         mime-view-type-subtype-score-alist)
536                                        ))))
537                              (if (> score max-score)
538                                  (setq p i
539                                        max-score score)
540                                )))
541                        (setq i (1+ i))
542                        situation)
543                      ))
544                   children)))
545     (setq i 0)
546     (while children
547       (let ((child (car children))
548             (situation (car situations)))
549         (mime-display-entity child (if (= i p)
550                                        situation
551                                      (del-alist 'body-presentation-method
552                                                 (copy-alist situation))))
553         )
554       (setq children (cdr children)
555             situations (cdr situations)
556             i (1+ i))
557       )))
558
559
560 ;;; @ acting-condition
561 ;;;
562
563 (defvar mime-acting-condition nil
564   "Condition-tree about how to process entity.")
565
566 (if (file-readable-p mailcap-file)
567     (let ((entries (mailcap-parse-file)))
568       (while entries
569         (let ((entry (car entries))
570               view print shared)
571           (while entry
572             (let* ((field (car entry))
573                    (field-type (car field)))
574               (cond ((eq field-type 'view)  (setq view field))
575                     ((eq field-type 'print) (setq print field))
576                     ((memq field-type '(compose composetyped edit)))
577                     (t (setq shared (cons field shared))))
578               )
579             (setq entry (cdr entry))
580             )
581           (setq shared (nreverse shared))
582           (ctree-set-calist-with-default
583            'mime-acting-condition
584            (append shared (list '(mode . "play")(cons 'method (cdr view)))))
585           (if print
586               (ctree-set-calist-with-default
587                'mime-acting-condition
588                (append shared
589                        (list '(mode . "print")(cons 'method (cdr view))))
590                ))
591           )
592         (setq entries (cdr entries))
593         )))
594
595 (ctree-set-calist-strictly
596  'mime-acting-condition
597  '((type . application)(subtype . octet-stream)
598    (mode . "play")
599    (method . mime-detect-content)
600    ))
601
602 (ctree-set-calist-with-default
603  'mime-acting-condition
604  '((mode . "extract")
605    (method . mime-save-content)))
606
607 (ctree-set-calist-strictly
608  'mime-acting-condition
609  '((type . text)(subtype . x-rot13-47)(mode . "play")
610    (method . mime-view-caesar)
611    ))
612 (ctree-set-calist-strictly
613  'mime-acting-condition
614  '((type . text)(subtype . x-rot13-47-48)(mode . "play")
615    (method . mime-view-caesar)
616    ))
617
618 (ctree-set-calist-strictly
619  'mime-acting-condition
620  '((type . message)(subtype . rfc822)(mode . "play")
621    (method . mime-view-message/rfc822)
622    ))
623 (ctree-set-calist-strictly
624  'mime-acting-condition
625  '((type . message)(subtype . partial)(mode . "play")
626    (method . mime-store-message/partial-piece)
627    ))
628
629 (ctree-set-calist-strictly
630  'mime-acting-condition
631  '((type . message)(subtype . external-body)
632    ("access-type" . "anon-ftp")
633    (method . mime-view-message/external-anon-ftp)
634    ))
635
636 (ctree-set-calist-strictly
637  'mime-acting-condition
638  '((type . message)(subtype . external-body)
639    ("access-type" . "url")
640    (method . mime-view-message/external-url)
641    ))
642
643 (ctree-set-calist-strictly
644  'mime-acting-condition
645  '((type . application)(subtype . octet-stream)
646    (method . mime-save-content)
647    ))
648
649
650 ;;; @ quitting method
651 ;;;
652
653 (defvar mime-preview-quitting-method-alist
654   '((mime-show-message-mode
655      . mime-preview-quitting-method-for-mime-show-message-mode))
656   "Alist of major-mode vs. quitting-method of mime-view.")
657
658 (defvar mime-preview-over-to-previous-method-alist nil
659   "Alist of major-mode vs. over-to-previous-method of mime-view.")
660
661 (defvar mime-preview-over-to-next-method-alist nil
662   "Alist of major-mode vs. over-to-next-method of mime-view.")
663
664
665 ;;; @ following method
666 ;;;
667
668 (defvar mime-preview-following-method-alist nil
669   "Alist of major-mode vs. following-method of mime-view.")
670
671 (defvar mime-view-following-required-fields-list
672   '("From"))
673
674
675 ;;; @ buffer setup
676 ;;;
677
678 (defun mime-display-entity (entity &optional situation
679                                    default-situation preview-buffer)
680   (or preview-buffer
681       (setq preview-buffer (current-buffer)))
682   (let* ((raw-buffer (mime-entity-buffer entity))
683          (start (mime-entity-point-min entity))
684          e nb ne)
685     (set-buffer raw-buffer)
686     (goto-char start)
687     (or situation
688         (setq situation
689               (or (ctree-match-calist mime-preview-condition
690                                       (append (mime-entity-situation entity)
691                                               default-situation))
692                   default-situation)))
693     (let ((button-is-invisible
694            (eq (cdr (assq 'entity-button situation)) 'invisible))
695           (header-is-visible
696            (eq (cdr (assq 'header situation)) 'visible))
697           (header-presentation-method
698            (or (cdr (assq 'header-presentation-method situation))
699                (cdr (assq major-mode mime-header-presentation-method-alist))))
700           (body-presentation-method
701            (cdr (assq 'body-presentation-method situation)))
702           (children (mime-entity-children entity)))
703       (set-buffer preview-buffer)
704       (setq nb (point))
705       (narrow-to-region nb nb)
706       (or button-is-invisible
707           (if (mime-view-entity-button-visible-p entity)
708               (mime-view-insert-entity-button entity)
709             ))
710       (when header-is-visible
711         (if header-presentation-method
712             (funcall header-presentation-method entity situation)
713           (mime-insert-header entity
714                               mime-view-ignored-field-list
715                               mime-view-visible-field-list))
716         (goto-char (point-max))
717         (insert "\n")
718         (run-hooks 'mime-display-header-hook)
719         )
720       (cond (children)
721             ((functionp body-presentation-method)
722              (funcall body-presentation-method entity situation)
723              )
724             (t
725              (when button-is-invisible
726                (goto-char (point-max))
727                (mime-view-insert-entity-button entity)
728                )
729              (or header-is-visible
730                  (progn
731                    (goto-char (point-max))
732                    (insert "\n")
733                    ))
734              ))
735       (setq ne (point-max))
736       (widen)
737       (put-text-property nb ne 'mime-view-entity entity)
738       (goto-char ne)
739       (if children
740           (if (functionp body-presentation-method)
741               (funcall body-presentation-method entity situation)
742             (mime-display-multipart/mixed entity situation)
743             ))
744       )))
745
746
747 ;;; @ MIME viewer mode
748 ;;;
749
750 (defconst mime-view-menu-title "MIME-View")
751 (defconst mime-view-menu-list
752   '((up          "Move to upper entity"    mime-preview-move-to-upper)
753     (previous    "Move to previous entity" mime-preview-move-to-previous)
754     (next        "Move to next entity"     mime-preview-move-to-next)
755     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
756     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
757     (play        "Play current entity"     mime-preview-play-current-entity)
758     (extract     "Extract current entity"  mime-preview-extract-current-entity)
759     (print       "Print current entity"    mime-preview-print-current-entity)
760     )
761   "Menu for MIME Viewer")
762
763 (cond ((featurep 'xemacs)
764        (defvar mime-view-xemacs-popup-menu
765          (cons mime-view-menu-title
766                (mapcar (function
767                         (lambda (item)
768                           (vector (nth 1 item)(nth 2 item) t)
769                           ))
770                        mime-view-menu-list)))
771        (defun mime-view-xemacs-popup-menu (event)
772          "Popup the menu in the MIME Viewer buffer"
773          (interactive "e")
774          (select-window (event-window event))
775          (set-buffer (event-buffer event))
776          (popup-menu 'mime-view-xemacs-popup-menu))
777        (defvar mouse-button-2 'button2)
778        )
779       (t
780        (defvar mouse-button-2 [mouse-2])
781        ))
782
783 (defun mime-view-define-keymap (&optional default)
784   (let ((mime-view-mode-map (if (keymapp default)
785                                 (copy-keymap default)
786                               (make-sparse-keymap)
787                               )))
788     (define-key mime-view-mode-map
789       "u"        (function mime-preview-move-to-upper))
790     (define-key mime-view-mode-map
791       "p"        (function mime-preview-move-to-previous))
792     (define-key mime-view-mode-map
793       "n"        (function mime-preview-move-to-next))
794     (define-key mime-view-mode-map
795       "\e\t"     (function mime-preview-move-to-previous))
796     (define-key mime-view-mode-map
797       "\t"       (function mime-preview-move-to-next))
798     (define-key mime-view-mode-map
799       " "        (function mime-preview-scroll-up-entity))
800     (define-key mime-view-mode-map
801       "\M- "     (function mime-preview-scroll-down-entity))
802     (define-key mime-view-mode-map
803       "\177"     (function mime-preview-scroll-down-entity))
804     (define-key mime-view-mode-map
805       "\C-m"     (function mime-preview-next-line-entity))
806     (define-key mime-view-mode-map
807       "\C-\M-m"  (function mime-preview-previous-line-entity))
808     (define-key mime-view-mode-map
809       "v"        (function mime-preview-play-current-entity))
810     (define-key mime-view-mode-map
811       "e"        (function mime-preview-extract-current-entity))
812     (define-key mime-view-mode-map
813       "\C-c\C-p" (function mime-preview-print-current-entity))
814     (define-key mime-view-mode-map
815       "a"        (function mime-preview-follow-current-entity))
816     (define-key mime-view-mode-map
817       "q"        (function mime-preview-quit))
818     (define-key mime-view-mode-map
819       "\C-c\C-x" (function mime-preview-kill-buffer))
820     ;; (define-key mime-view-mode-map
821     ;;   "<"        (function beginning-of-buffer))
822     ;; (define-key mime-view-mode-map
823     ;;   ">"        (function end-of-buffer))
824     (define-key mime-view-mode-map
825       "?"        (function describe-mode))
826     (define-key mime-view-mode-map
827       [tab] (function mime-preview-move-to-next))
828     (define-key mime-view-mode-map
829       [delete] (function mime-preview-scroll-down-entity))
830     (define-key mime-view-mode-map
831       [backspace] (function mime-preview-scroll-down-entity))
832     (if (functionp default)
833         (cond ((featurep 'xemacs)
834                (set-keymap-default-binding mime-view-mode-map default)
835                )
836               (t
837                (setq mime-view-mode-map
838                      (append mime-view-mode-map (list (cons t default))))
839                )))
840     (if mouse-button-2
841         (define-key mime-view-mode-map
842           mouse-button-2 (function mime-button-dispatcher))
843       )
844     (cond ((featurep 'xemacs)
845            (define-key mime-view-mode-map
846              mouse-button-3 (function mime-view-xemacs-popup-menu))
847            )
848           ((>= emacs-major-version 19)
849            (define-key mime-view-mode-map [menu-bar mime-view]
850              (cons mime-view-menu-title
851                    (make-sparse-keymap mime-view-menu-title)))
852            (mapcar (function
853                     (lambda (item)
854                       (define-key mime-view-mode-map
855                         (vector 'menu-bar 'mime-view (car item))
856                         (cons (nth 1 item)(nth 2 item))
857                         )
858                       ))
859                    (reverse mime-view-menu-list)
860                    )
861            ))
862     (use-local-map mime-view-mode-map)
863     (run-hooks 'mime-view-define-keymap-hook)
864     ))
865
866 (defsubst mime-maybe-hide-echo-buffer ()
867   "Clear mime-echo buffer and delete window for it."
868   (let ((buf (get-buffer mime-echo-buffer-name)))
869     (if buf
870         (save-excursion
871           (set-buffer buf)
872           (erase-buffer)
873           (let ((win (get-buffer-window buf)))
874             (if win
875                 (delete-window win)
876               ))
877           (bury-buffer buf)
878           ))))
879
880 (defvar mime-view-redisplay nil)
881
882 (defun mime-display-message (message &optional preview-buffer
883                                      mother default-keymap-or-function)
884   (mime-maybe-hide-echo-buffer)
885   (let ((win-conf (current-window-configuration))
886         (raw-buffer (mime-entity-buffer message)))
887     (or preview-buffer
888         (setq preview-buffer
889               (concat "*Preview-" (buffer-name raw-buffer) "*")))
890     (set-buffer raw-buffer)
891     (setq mime-preview-buffer preview-buffer)
892     (let ((inhibit-read-only t))
893       (set-buffer (get-buffer-create preview-buffer))
894       (widen)
895       (erase-buffer)
896       (setq mime-raw-buffer raw-buffer)
897       (if mother
898           (setq mime-mother-buffer mother)
899         )
900       (setq mime-preview-original-window-configuration win-conf)
901       (setq major-mode 'mime-view-mode)
902       (setq mode-name "MIME-View")
903       (mime-display-entity message nil
904                            '((entity-button . invisible)
905                              (header . visible))
906                            preview-buffer)
907       (mime-view-define-keymap default-keymap-or-function)
908       (let ((point
909              (next-single-property-change (point-min) 'mime-view-entity)))
910         (if point
911             (goto-char point)
912           (goto-char (point-min))
913           (search-forward "\n\n" nil t)
914           ))
915       (run-hooks 'mime-view-mode-hook)
916       (set-buffer-modified-p nil)
917       (setq buffer-read-only t)
918       (or (get-buffer-window preview-buffer)
919           (let ((r-win (get-buffer-window raw-buffer)))
920             (if r-win
921                 (set-window-buffer r-win preview-buffer)
922               (let ((m-win (and mother (get-buffer-window mother))))
923                 (if m-win
924                     (set-window-buffer m-win preview-buffer)
925                   (switch-to-buffer preview-buffer)
926                   )))))
927       )))
928
929 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
930                                    default-keymap-or-function
931                                    representation-type)
932   "View RAW-BUFFER in MIME-View mode.
933 Optional argument PREVIEW-BUFFER is either nil or a name of preview
934 buffer.
935 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
936 function.  If it is a keymap, keymap of MIME-View mode will be added
937 to it.  If it is a function, it will be bound as default binding of
938 keymap of MIME-View mode.
939 Optional argument REPRESENTATION-TYPE is representation-type of
940 message.  It must be nil, `binary' or `cooked'.  If it is nil,
941 `binary' is used as default."
942   (interactive)
943   (or raw-buffer
944       (setq raw-buffer (current-buffer)))
945   (or representation-type
946       (setq representation-type
947             (save-excursion
948               (set-buffer raw-buffer)
949               (cdr (or (assq major-mode mime-raw-representation-type-alist)
950                        (assq t mime-raw-representation-type-alist)))
951               )))
952   (if (eq representation-type 'binary)
953       (setq representation-type 'buffer)
954     )
955   (mime-display-message
956    (mime-open-entity representation-type raw-buffer)
957    preview-buffer mother default-keymap-or-function))
958
959 (defun mime-view-mode (&optional mother ctl encoding
960                                  raw-buffer preview-buffer
961                                  default-keymap-or-function)
962   "Major mode for viewing MIME message.
963
964 Here is a list of the standard keys for mime-view-mode.
965
966 key             feature
967 ---             -------
968
969 u               Move to upper content
970 p or M-TAB      Move to previous content
971 n or TAB        Move to next content
972 SPC             Scroll up or move to next content
973 M-SPC or DEL    Scroll down or move to previous content
974 RET             Move to next line
975 M-RET           Move to previous line
976 v               Decode current content as `play mode'
977 e               Decode current content as `extract mode'
978 C-c C-p         Decode current content as `print mode'
979 a               Followup to current content.
980 q               Quit
981 button-2        Move to point under the mouse cursor
982                 and decode current content as `play mode'
983 "
984   (interactive)
985   (unless mime-view-redisplay
986     (save-excursion
987       (if raw-buffer (set-buffer raw-buffer))
988       (let ((type
989              (cdr
990               (or (assq major-mode mime-raw-representation-type-alist)
991                   (assq t mime-raw-representation-type-alist)))))
992         (if (eq type 'binary)
993             (setq type 'buffer)
994           )
995         (setq mime-message-structure (mime-open-entity type raw-buffer))
996         (or (mime-entity-content-type mime-message-structure)
997             (mime-entity-set-content-type-internal
998              mime-message-structure ctl))
999         )
1000       (or (mime-entity-encoding mime-message-structure)
1001           (mime-entity-set-encoding-internal mime-message-structure encoding))
1002       ))
1003   (mime-display-message mime-message-structure preview-buffer
1004                         mother default-keymap-or-function)
1005   )
1006
1007
1008 ;;; @@ playing
1009 ;;;
1010
1011 (autoload 'mime-preview-play-current-entity "mime-play"
1012   "Play current entity." t)
1013
1014 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1015   "Extract current entity into file (maybe).
1016 It decodes current entity to call internal or external method as
1017 \"extract\" mode.  The method is selected from variable
1018 `mime-acting-condition'."
1019   (interactive "P")
1020   (mime-preview-play-current-entity ignore-examples "extract")
1021   )
1022
1023 (defun mime-preview-print-current-entity (&optional ignore-examples)
1024   "Print current entity (maybe).
1025 It decodes current entity to call internal or external method as
1026 \"print\" mode.  The method is selected from variable
1027 `mime-acting-condition'."
1028   (interactive "P")
1029   (mime-preview-play-current-entity ignore-examples "print")
1030   )
1031
1032
1033 ;;; @@ following
1034 ;;;
1035
1036 (defun mime-preview-follow-current-entity ()
1037   "Write follow message to current entity.
1038 It calls following-method selected from variable
1039 `mime-preview-following-method-alist'."
1040   (interactive)
1041   (let (entity)
1042     (while (null (setq entity
1043                        (get-text-property (point) 'mime-view-entity)))
1044       (backward-char)
1045       )
1046     (let* ((p-beg
1047             (previous-single-property-change (point) 'mime-view-entity))
1048            p-end
1049            (entity-node-id (mime-entity-node-id entity))
1050            (len (length entity-node-id))
1051            )
1052       (cond ((null p-beg)
1053              (setq p-beg
1054                    (if (eq (next-single-property-change (point-min)
1055                                                         'mime-view-entity)
1056                            (point))
1057                        (point)
1058                      (point-min)))
1059              )
1060             ((eq (next-single-property-change p-beg 'mime-view-entity)
1061                  (point))
1062              (setq p-beg (point))
1063              ))
1064       (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1065       (cond ((null p-end)
1066              (setq p-end (point-max))
1067              )
1068             ((null entity-node-id)
1069              (setq p-end (point-max))
1070              )
1071             (t
1072              (save-excursion
1073                (goto-char p-end)
1074                (catch 'tag
1075                  (let (e)
1076                    (while (setq e
1077                                 (next-single-property-change
1078                                  (point) 'mime-view-entity))
1079                      (goto-char e)
1080                      (let ((rc (mime-entity-node-id
1081                                 (get-text-property (point)
1082                                                    'mime-view-entity))))
1083                        (or (equal entity-node-id
1084                                   (nthcdr (- (length rc) len) rc))
1085                            (throw 'tag nil)
1086                            ))
1087                      (setq p-end e)
1088                      ))
1089                  (setq p-end (point-max))
1090                  ))
1091              ))
1092       (let* ((mode (mime-preview-original-major-mode 'recursive))
1093              (new-name
1094               (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1095              new-buf
1096              (the-buf (current-buffer))
1097              (a-buf mime-raw-buffer)
1098              fields)
1099         (save-excursion
1100           (set-buffer (setq new-buf (get-buffer-create new-name)))
1101           (erase-buffer)
1102           (insert-buffer-substring the-buf p-beg p-end)
1103           (goto-char (point-min))
1104           (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1105             (while (progn
1106                      (setq
1107                       str
1108                       (save-excursion
1109                         (set-buffer a-buf)
1110                         (setq ci
1111                               (mime-find-entity-from-node-id entity-node-id))
1112                         (save-restriction
1113                           (narrow-to-region
1114                            (mime-entity-point-min ci)
1115                            (mime-entity-point-max ci)
1116                            )
1117                           (std11-header-string-except
1118                            (concat "^"
1119                                    (apply (function regexp-or) fields)
1120                                    ":") ""))))
1121                      (if (and
1122                           (eq (mime-entity-media-type ci) 'message)
1123                           (eq (mime-entity-media-subtype ci) 'rfc822))
1124                          nil
1125                        (if str
1126                            (insert str)
1127                          )
1128                        entity-node-id))
1129               (setq fields (std11-collect-field-names)
1130                     entity-node-id (cdr entity-node-id))
1131               )
1132             )
1133           (let ((rest mime-view-following-required-fields-list))
1134             (while rest
1135               (let ((field-name (car rest)))
1136                 (or (std11-field-body field-name)
1137                     (insert
1138                      (format
1139                       (concat field-name
1140                               ": "
1141                               (save-excursion
1142                                 (set-buffer the-buf)
1143                                 (set-buffer mime-mother-buffer)
1144                                 (set-buffer mime-raw-buffer)
1145                                 (std11-field-body field-name)
1146                                 )
1147                               "\n")))
1148                     ))
1149               (setq rest (cdr rest))
1150               ))
1151           (eword-decode-header)
1152           )
1153         (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1154           (if (functionp f)
1155               (funcall f new-buf)
1156             (message
1157              (format
1158               "Sorry, following method for %s is not implemented yet."
1159               mode))
1160             ))
1161         ))))
1162
1163
1164 ;;; @@ moving
1165 ;;;
1166
1167 (defun mime-preview-move-to-upper ()
1168   "Move to upper entity.
1169 If there is no upper entity, call function `mime-preview-quit'."
1170   (interactive)
1171   (let (cinfo)
1172     (while (null (setq cinfo
1173                        (get-text-property (point) 'mime-view-entity)))
1174       (backward-char)
1175       )
1176     (let ((r (mime-entity-parent cinfo))
1177           point)
1178       (catch 'tag
1179         (while (setq point (previous-single-property-change
1180                             (point) 'mime-view-entity))
1181           (goto-char point)
1182           (if (eq r (get-text-property (point) 'mime-view-entity))
1183               (throw 'tag t)
1184             )
1185           )
1186         (mime-preview-quit)
1187         ))))
1188
1189 (defun mime-preview-move-to-previous ()
1190   "Move to previous entity.
1191 If there is no previous entity, it calls function registered in
1192 variable `mime-preview-over-to-previous-method-alist'."
1193   (interactive)
1194   (while (null (get-text-property (point) 'mime-view-entity))
1195     (backward-char)
1196     )
1197   (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1198     (if point
1199         (if (get-text-property (1- point) 'mime-view-entity)
1200             (goto-char point)
1201           (goto-char (1- point))
1202           (mime-preview-move-to-previous)
1203           )
1204       (let ((f (assq (mime-preview-original-major-mode)
1205                      mime-preview-over-to-previous-method-alist)))
1206         (if f
1207             (funcall (cdr f))
1208           ))
1209       )))
1210
1211 (defun mime-preview-move-to-next ()
1212   "Move to next entity.
1213 If there is no previous entity, it calls function registered in
1214 variable `mime-preview-over-to-next-method-alist'."
1215   (interactive)
1216   (while (null (get-text-property (point) 'mime-view-entity))
1217     (forward-char)
1218     )
1219   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1220     (if point
1221         (progn
1222           (goto-char point)
1223           (if (null (get-text-property point 'mime-view-entity))
1224               (mime-preview-move-to-next)
1225             ))
1226       (let ((f (assq (mime-preview-original-major-mode)
1227                      mime-preview-over-to-next-method-alist)))
1228         (if f
1229             (funcall (cdr f))
1230           ))
1231       )))
1232
1233 (defun mime-preview-scroll-up-entity (&optional h)
1234   "Scroll up current entity.
1235 If reached to (point-max), it calls function registered in variable
1236 `mime-preview-over-to-next-method-alist'."
1237   (interactive)
1238   (or h
1239       (setq h (1- (window-height)))
1240       )
1241   (if (= (point) (point-max))
1242       (let ((f (assq (mime-preview-original-major-mode)
1243                      mime-preview-over-to-next-method-alist)))
1244         (if f
1245             (funcall (cdr f))
1246           ))
1247     (let ((point
1248            (or (next-single-property-change (point) 'mime-view-entity)
1249                (point-max))))
1250       (forward-line h)
1251       (if (> (point) point)
1252           (goto-char point)
1253         )
1254       )))
1255
1256 (defun mime-preview-scroll-down-entity (&optional h)
1257   "Scroll down current entity.
1258 If reached to (point-min), it calls function registered in variable
1259 `mime-preview-over-to-previous-method-alist'."
1260   (interactive)
1261   (or h
1262       (setq h (1- (window-height)))
1263       )
1264   (if (= (point) (point-min))
1265       (let ((f (assq (mime-preview-original-major-mode)
1266                      mime-preview-over-to-previous-method-alist)))
1267         (if f
1268             (funcall (cdr f))
1269           ))
1270     (let ((point
1271            (or (previous-single-property-change (point) 'mime-view-entity)
1272                (point-min))))
1273       (forward-line (- h))
1274       (if (< (point) point)
1275           (goto-char point)
1276         ))))
1277
1278 (defun mime-preview-next-line-entity ()
1279   (interactive)
1280   (mime-preview-scroll-up-entity 1)
1281   )
1282
1283 (defun mime-preview-previous-line-entity ()
1284   (interactive)
1285   (mime-preview-scroll-down-entity 1)
1286   )
1287
1288
1289 ;;; @@ quitting
1290 ;;;
1291
1292 (defun mime-preview-quit ()
1293   "Quit from MIME-preview buffer.
1294 It calls function registered in variable
1295 `mime-preview-quitting-method-alist'."
1296   (interactive)
1297   (let ((r (assq (mime-preview-original-major-mode)
1298                  mime-preview-quitting-method-alist)))
1299     (if r
1300         (funcall (cdr r))
1301       )))
1302
1303 (defun mime-preview-kill-buffer ()
1304   (interactive)
1305   (kill-buffer (current-buffer))
1306   )
1307
1308
1309 ;;; @ end
1310 ;;;
1311
1312 (provide 'mime-view)
1313
1314 (run-hooks 'mime-view-load-hook)
1315
1316 ;;; mime-view.el ends here