f52432a937355a1fbb4f4b863066343f013d95ed
[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 (autoload 'mime-display-text/plain "mime-text")
425 (autoload 'mime-display-text/enriched "mime-text")
426 (autoload 'mime-display-text/richtext "mime-text")
427
428 (defvar mime-view-announcement-for-message/partial
429   (if (and (>= emacs-major-version 19) window-system)
430       "\
431 \[[ This is message/partial style split message. ]]
432 \[[ Please press `v' key in this buffer          ]]
433 \[[ or click here by mouse button-2.             ]]"
434     "\
435 \[[ This is message/partial style split message. ]]
436 \[[ Please press `v' key in this buffer.         ]]"
437     ))
438
439 (defun mime-display-message/partial-button (&optional entity situation)
440   (save-restriction
441     (goto-char (point-max))
442     (if (not (search-backward "\n\n" nil t))
443         (insert "\n")
444       )
445     (goto-char (point-max))
446     (narrow-to-region (point-max)(point-max))
447     (insert mime-view-announcement-for-message/partial)
448     (mime-add-button (point-min)(point-max)
449                      #'mime-preview-play-current-entity)
450     ))
451
452 (defun mime-display-multipart/mixed (entity situation)
453   (let ((children (mime-entity-children entity))
454         (default-situation
455           (cdr (assq 'childrens-situation situation))))
456     (while children
457       (mime-display-entity (car children) nil default-situation)
458       (setq children (cdr children))
459       )))
460
461 (defcustom mime-view-type-subtype-score-alist
462   '(((text . enriched) . 3)
463     ((text . richtext) . 2)
464     ((text . plain)    . 1)
465     (t . 0))
466   "Alist MEDIA-TYPE vs corresponding score.
467 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
468   :group 'mime-view
469   :type '(repeat (cons (choice :tag "Media-Type"
470                                (cons :tag "Type/Subtype"
471                                      (symbol :tag "Primary-type")
472                                      (symbol :tag "Subtype"))
473                                (symbol :tag "Type")
474                                (const :tag "Default" t))
475                        integer)))
476
477 (defun mime-display-multipart/alternative (entity situation)
478   (let* ((children (mime-entity-children entity))
479          (default-situation
480            (cdr (assq 'childrens-situation situation)))
481          (i 0)
482          (p 0)
483          (max-score 0)
484          (situations
485           (mapcar (function
486                    (lambda (child)
487                      (let ((situation
488                             (or (ctree-match-calist
489                                  mime-preview-condition
490                                  (append (mime-entity-situation child)
491                                          default-situation))
492                                 default-situation)))
493                        (if (cdr (assq 'body-presentation-method situation))
494                            (let ((score
495                                   (cdr
496                                    (or (assoc
497                                         (cons
498                                          (cdr (assq 'type situation))
499                                          (cdr (assq 'subtype situation)))
500                                         mime-view-type-subtype-score-alist)
501                                        (assq
502                                         (cdr (assq 'type situation))
503                                         mime-view-type-subtype-score-alist)
504                                        (assq
505                                         t
506                                         mime-view-type-subtype-score-alist)
507                                        ))))
508                              (if (> score max-score)
509                                  (setq p i
510                                        max-score score)
511                                )))
512                        (setq i (1+ i))
513                        situation)
514                      ))
515                   children)))
516     (setq i 0)
517     (while children
518       (let ((child (car children))
519             (situation (car situations)))
520         (mime-display-entity child (if (= i p)
521                                        situation
522                                      (del-alist 'body-presentation-method
523                                                 (copy-alist situation))))
524         )
525       (setq children (cdr children)
526             situations (cdr situations)
527             i (1+ i))
528       )))
529
530
531 ;;; @ acting-condition
532 ;;;
533
534 (defvar mime-acting-condition nil
535   "Condition-tree about how to process entity.")
536
537 (if (file-readable-p mailcap-file)
538     (let ((entries (mailcap-parse-file)))
539       (while entries
540         (let ((entry (car entries))
541               view print shared)
542           (while entry
543             (let* ((field (car entry))
544                    (field-type (car field)))
545               (cond ((eq field-type 'view)  (setq view field))
546                     ((eq field-type 'print) (setq print field))
547                     ((memq field-type '(compose composetyped edit)))
548                     (t (setq shared (cons field shared))))
549               )
550             (setq entry (cdr entry))
551             )
552           (setq shared (nreverse shared))
553           (ctree-set-calist-with-default
554            'mime-acting-condition
555            (append shared (list '(mode . "play")(cons 'method (cdr view)))))
556           (if print
557               (ctree-set-calist-with-default
558                'mime-acting-condition
559                (append shared
560                        (list '(mode . "print")(cons 'method (cdr view))))
561                ))
562           )
563         (setq entries (cdr entries))
564         )))
565
566 (ctree-set-calist-strictly
567  'mime-acting-condition
568  '((type . application)(subtype . octet-stream)
569    (mode . "play")
570    (method . mime-detect-content)
571    ))
572
573 (ctree-set-calist-with-default
574  'mime-acting-condition
575  '((mode . "extract")
576    (method . mime-save-content)))
577
578 (ctree-set-calist-strictly
579  'mime-acting-condition
580  '((type . text)(subtype . x-rot13-47)(mode . "play")
581    (method . mime-view-caesar)
582    ))
583 (ctree-set-calist-strictly
584  'mime-acting-condition
585  '((type . text)(subtype . x-rot13-47-48)(mode . "play")
586    (method . mime-view-caesar)
587    ))
588
589 (ctree-set-calist-strictly
590  'mime-acting-condition
591  '((type . message)(subtype . rfc822)(mode . "play")
592    (method . mime-view-message/rfc822)
593    ))
594 (ctree-set-calist-strictly
595  'mime-acting-condition
596  '((type . message)(subtype . partial)(mode . "play")
597    (method . mime-store-message/partial-piece)
598    ))
599
600 (ctree-set-calist-strictly
601  'mime-acting-condition
602  '((type . message)(subtype . external-body)
603    ("access-type" . "anon-ftp")
604    (method . mime-view-message/external-anon-ftp)
605    ))
606
607 (ctree-set-calist-strictly
608  'mime-acting-condition
609  '((type . message)(subtype . external-body)
610    ("access-type" . "url")
611    (method . mime-view-message/external-url)
612    ))
613
614 (ctree-set-calist-strictly
615  'mime-acting-condition
616  '((type . application)(subtype . octet-stream)
617    (method . mime-save-content)
618    ))
619
620
621 ;;; @ quitting method
622 ;;;
623
624 (defvar mime-preview-quitting-method-alist
625   '((mime-show-message-mode
626      . mime-preview-quitting-method-for-mime-show-message-mode))
627   "Alist of major-mode vs. quitting-method of mime-view.")
628
629 (defvar mime-preview-over-to-previous-method-alist nil
630   "Alist of major-mode vs. over-to-previous-method of mime-view.")
631
632 (defvar mime-preview-over-to-next-method-alist nil
633   "Alist of major-mode vs. over-to-next-method of mime-view.")
634
635
636 ;;; @ following method
637 ;;;
638
639 (defvar mime-preview-following-method-alist nil
640   "Alist of major-mode vs. following-method of mime-view.")
641
642 (defvar mime-view-following-required-fields-list
643   '("From"))
644
645
646 ;;; @ buffer setup
647 ;;;
648
649 (defun mime-display-entity (entity &optional situation
650                                    default-situation preview-buffer)
651   (or preview-buffer
652       (setq preview-buffer (current-buffer)))
653   (let* ((raw-buffer (mime-entity-buffer entity))
654          (start (mime-entity-point-min entity))
655          e nb ne)
656     (set-buffer raw-buffer)
657     (goto-char start)
658     (or situation
659         (setq situation
660               (or (ctree-match-calist mime-preview-condition
661                                       (append (mime-entity-situation entity)
662                                               default-situation))
663                   default-situation)))
664     (let ((button-is-invisible
665            (eq (cdr (assq 'entity-button situation)) 'invisible))
666           (header-is-visible
667            (eq (cdr (assq 'header situation)) 'visible))
668           (header-presentation-method
669            (or (cdr (assq 'header-presentation-method situation))
670                (cdr (assq major-mode mime-header-presentation-method-alist))))
671           (body-presentation-method
672            (cdr (assq 'body-presentation-method situation)))
673           (children (mime-entity-children entity)))
674       (set-buffer preview-buffer)
675       (setq nb (point))
676       (narrow-to-region nb nb)
677       (or button-is-invisible
678           (if (mime-view-entity-button-visible-p entity)
679               (mime-view-insert-entity-button entity)
680             ))
681       (when header-is-visible
682         (if header-presentation-method
683             (funcall header-presentation-method entity situation)
684           (mime-insert-decoded-header entity
685                                       mime-view-ignored-field-list
686                                       mime-view-visible-field-list))
687         (goto-char (point-max))
688         (insert "\n")
689         (run-hooks 'mime-display-header-hook)
690         )
691       (cond (children)
692             ((functionp body-presentation-method)
693              (funcall body-presentation-method entity situation)
694              )
695             (t
696              (when button-is-invisible
697                (goto-char (point-max))
698                (mime-view-insert-entity-button entity)
699                )
700              (or header-is-visible
701                  (progn
702                    (goto-char (point-max))
703                    (insert "\n")
704                    ))
705              ))
706       (setq ne (point-max))
707       (widen)
708       (put-text-property nb ne 'mime-view-entity entity)
709       (goto-char ne)
710       (if children
711           (if (functionp body-presentation-method)
712               (funcall body-presentation-method entity situation)
713             (mime-display-multipart/mixed entity situation)
714             ))
715       )))
716
717
718 ;;; @ MIME viewer mode
719 ;;;
720
721 (defconst mime-view-menu-title "MIME-View")
722 (defconst mime-view-menu-list
723   '((up          "Move to upper entity"    mime-preview-move-to-upper)
724     (previous    "Move to previous entity" mime-preview-move-to-previous)
725     (next        "Move to next entity"     mime-preview-move-to-next)
726     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
727     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
728     (play        "Play current entity"     mime-preview-play-current-entity)
729     (extract     "Extract current entity"  mime-preview-extract-current-entity)
730     (print       "Print current entity"    mime-preview-print-current-entity)
731     )
732   "Menu for MIME Viewer")
733
734 (cond (running-xemacs
735        (defvar mime-view-xemacs-popup-menu
736          (cons mime-view-menu-title
737                (mapcar (function
738                         (lambda (item)
739                           (vector (nth 1 item)(nth 2 item) t)
740                           ))
741                        mime-view-menu-list)))
742        (defun mime-view-xemacs-popup-menu (event)
743          "Popup the menu in the MIME Viewer buffer"
744          (interactive "e")
745          (select-window (event-window event))
746          (set-buffer (event-buffer event))
747          (popup-menu 'mime-view-xemacs-popup-menu))
748        (defvar mouse-button-2 'button2)
749        )
750       (t
751        (defvar mouse-button-2 [mouse-2])
752        ))
753
754 (defun mime-view-define-keymap (&optional default)
755   (let ((mime-view-mode-map (if (keymapp default)
756                                 (copy-keymap default)
757                               (make-sparse-keymap)
758                               )))
759     (define-key mime-view-mode-map
760       "u"        (function mime-preview-move-to-upper))
761     (define-key mime-view-mode-map
762       "p"        (function mime-preview-move-to-previous))
763     (define-key mime-view-mode-map
764       "n"        (function mime-preview-move-to-next))
765     (define-key mime-view-mode-map
766       "\e\t"     (function mime-preview-move-to-previous))
767     (define-key mime-view-mode-map
768       "\t"       (function mime-preview-move-to-next))
769     (define-key mime-view-mode-map
770       " "        (function mime-preview-scroll-up-entity))
771     (define-key mime-view-mode-map
772       "\M- "     (function mime-preview-scroll-down-entity))
773     (define-key mime-view-mode-map
774       "\177"     (function mime-preview-scroll-down-entity))
775     (define-key mime-view-mode-map
776       "\C-m"     (function mime-preview-next-line-entity))
777     (define-key mime-view-mode-map
778       "\C-\M-m"  (function mime-preview-previous-line-entity))
779     (define-key mime-view-mode-map
780       "v"        (function mime-preview-play-current-entity))
781     (define-key mime-view-mode-map
782       "e"        (function mime-preview-extract-current-entity))
783     (define-key mime-view-mode-map
784       "\C-c\C-p" (function mime-preview-print-current-entity))
785     (define-key mime-view-mode-map
786       "a"        (function mime-preview-follow-current-entity))
787     (define-key mime-view-mode-map
788       "q"        (function mime-preview-quit))
789     (define-key mime-view-mode-map
790       "\C-c\C-x" (function mime-preview-kill-buffer))
791     ;; (define-key mime-view-mode-map
792     ;;   "<"        (function beginning-of-buffer))
793     ;; (define-key mime-view-mode-map
794     ;;   ">"        (function end-of-buffer))
795     (define-key mime-view-mode-map
796       "?"        (function describe-mode))
797     (define-key mime-view-mode-map
798       [tab] (function mime-preview-move-to-next))
799     (define-key mime-view-mode-map
800       [delete] (function mime-preview-scroll-down-entity))
801     (define-key mime-view-mode-map
802       [backspace] (function mime-preview-scroll-down-entity))
803     (if (functionp default)
804         (cond (running-xemacs
805                (set-keymap-default-binding mime-view-mode-map default)
806                )
807               (t
808                (setq mime-view-mode-map
809                      (append mime-view-mode-map (list (cons t default))))
810                )))
811     (if mouse-button-2
812         (define-key mime-view-mode-map
813           mouse-button-2 (function mime-button-dispatcher))
814       )
815     (cond (running-xemacs
816            (define-key mime-view-mode-map
817              mouse-button-3 (function mime-view-xemacs-popup-menu))
818            )
819           ((>= emacs-major-version 19)
820            (define-key mime-view-mode-map [menu-bar mime-view]
821              (cons mime-view-menu-title
822                    (make-sparse-keymap mime-view-menu-title)))
823            (mapcar (function
824                     (lambda (item)
825                       (define-key mime-view-mode-map
826                         (vector 'menu-bar 'mime-view (car item))
827                         (cons (nth 1 item)(nth 2 item))
828                         )
829                       ))
830                    (reverse mime-view-menu-list)
831                    )
832            ))
833     (use-local-map mime-view-mode-map)
834     (run-hooks 'mime-view-define-keymap-hook)
835     ))
836
837 (defsubst mime-maybe-hide-echo-buffer ()
838   "Clear mime-echo buffer and delete window for it."
839   (let ((buf (get-buffer mime-echo-buffer-name)))
840     (if buf
841         (save-excursion
842           (set-buffer buf)
843           (erase-buffer)
844           (let ((win (get-buffer-window buf)))
845             (if win
846                 (delete-window win)
847               ))
848           (bury-buffer buf)
849           ))))
850
851 (defvar mime-view-redisplay nil)
852
853 (defun mime-display-message (message &optional preview-buffer
854                                      mother default-keymap-or-function)
855   (mime-maybe-hide-echo-buffer)
856   (let ((win-conf (current-window-configuration))
857         (raw-buffer (mime-entity-buffer message)))
858     (or preview-buffer
859         (setq preview-buffer
860               (concat "*Preview-" (buffer-name raw-buffer) "*")))
861     (set-buffer raw-buffer)
862     (setq mime-preview-buffer preview-buffer)
863     (let ((inhibit-read-only t))
864       (set-buffer (get-buffer-create preview-buffer))
865       (widen)
866       (erase-buffer)
867       (setq mime-raw-buffer raw-buffer)
868       (if mother
869           (setq mime-mother-buffer mother)
870         )
871       (setq mime-preview-original-window-configuration win-conf)
872       (setq major-mode 'mime-view-mode)
873       (setq mode-name "MIME-View")
874       (mime-display-entity message nil
875                            '((entity-button . invisible)
876                              (header . visible))
877                            preview-buffer)
878       (mime-view-define-keymap default-keymap-or-function)
879       (let ((point
880              (next-single-property-change (point-min) 'mime-view-entity)))
881         (if point
882             (goto-char point)
883           (goto-char (point-min))
884           (search-forward "\n\n" nil t)
885           ))
886       (run-hooks 'mime-view-mode-hook)
887       (set-buffer-modified-p nil)
888       (setq buffer-read-only t)
889       (or (get-buffer-window preview-buffer)
890           (let ((r-win (get-buffer-window raw-buffer)))
891             (if r-win
892                 (set-window-buffer r-win preview-buffer)
893               (let ((m-win (and mother (get-buffer-window mother))))
894                 (if m-win
895                     (set-window-buffer m-win preview-buffer)
896                   (switch-to-buffer preview-buffer)
897                   )))))
898       )))
899
900 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
901                                    default-keymap-or-function
902                                    representation-type)
903   "View RAW-BUFFER in MIME-View mode.
904 Optional argument PREVIEW-BUFFER is either nil or a name of preview
905 buffer.
906 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
907 function.  If it is a keymap, keymap of MIME-View mode will be added
908 to it.  If it is a function, it will be bound as default binding of
909 keymap of MIME-View mode.
910 Optional argument REPRESENTATION-TYPE is representation-type of
911 message.  It must be nil, `binary' or `cooked'.  If it is nil,
912 `binary' is used as default."
913   (interactive)
914   (or raw-buffer
915       (setq raw-buffer (current-buffer)))
916   (or representation-type
917       (setq representation-type
918             (save-excursion
919               (set-buffer raw-buffer)
920               (cdr (or (assq major-mode mime-raw-representation-type-alist)
921                        (assq t mime-raw-representation-type-alist)))
922               )))
923   (if (eq representation-type 'binary)
924       (setq representation-type 'buffer)
925     )
926   (mime-display-message
927    (mime-open-entity representation-type raw-buffer)
928    preview-buffer mother default-keymap-or-function))
929
930 (defun mime-view-mode (&optional mother ctl encoding
931                                  raw-buffer preview-buffer
932                                  default-keymap-or-function)
933   "Major mode for viewing MIME message.
934
935 Here is a list of the standard keys for mime-view-mode.
936
937 key             feature
938 ---             -------
939
940 u               Move to upper content
941 p or M-TAB      Move to previous content
942 n or TAB        Move to next content
943 SPC             Scroll up or move to next content
944 M-SPC or DEL    Scroll down or move to previous content
945 RET             Move to next line
946 M-RET           Move to previous line
947 v               Decode current content as `play mode'
948 e               Decode current content as `extract mode'
949 C-c C-p         Decode current content as `print mode'
950 a               Followup to current content.
951 q               Quit
952 button-2        Move to point under the mouse cursor
953                 and decode current content as `play mode'
954 "
955   (interactive)
956   (unless mime-view-redisplay
957     (save-excursion
958       (if raw-buffer (set-buffer raw-buffer))
959       (let ((type
960              (cdr
961               (or (assq major-mode mime-raw-representation-type-alist)
962                   (assq t mime-raw-representation-type-alist)))))
963         (if (eq type 'binary)
964             (setq type 'buffer)
965           )
966         (setq mime-message-structure (mime-open-entity type raw-buffer))
967         (or (mime-entity-content-type mime-message-structure)
968             (mime-entity-set-content-type-internal
969              mime-message-structure ctl))
970         )
971       (or (mime-entity-encoding mime-message-structure)
972           (mime-entity-set-encoding-internal mime-message-structure encoding))
973       ))
974   (mime-display-message mime-message-structure preview-buffer
975                         mother default-keymap-or-function)
976   )
977
978
979 ;;; @@ playing
980 ;;;
981
982 (autoload 'mime-preview-play-current-entity "mime-play"
983   "Play current entity." t)
984
985 (defun mime-preview-extract-current-entity (&optional ignore-examples)
986   "Extract current entity into file (maybe).
987 It decodes current entity to call internal or external method as
988 \"extract\" mode.  The method is selected from variable
989 `mime-acting-condition'."
990   (interactive "P")
991   (mime-preview-play-current-entity ignore-examples "extract")
992   )
993
994 (defun mime-preview-print-current-entity (&optional ignore-examples)
995   "Print current entity (maybe).
996 It decodes current entity to call internal or external method as
997 \"print\" mode.  The method is selected from variable
998 `mime-acting-condition'."
999   (interactive "P")
1000   (mime-preview-play-current-entity ignore-examples "print")
1001   )
1002
1003
1004 ;;; @@ following
1005 ;;;
1006
1007 (defun mime-preview-follow-current-entity ()
1008   "Write follow message to current entity.
1009 It calls following-method selected from variable
1010 `mime-preview-following-method-alist'."
1011   (interactive)
1012   (let (entity)
1013     (while (null (setq entity
1014                        (get-text-property (point) 'mime-view-entity)))
1015       (backward-char)
1016       )
1017     (let* ((p-beg
1018             (previous-single-property-change (point) 'mime-view-entity))
1019            p-end
1020            (entity-node-id (mime-entity-node-id entity))
1021            (len (length entity-node-id))
1022            )
1023       (cond ((null p-beg)
1024              (setq p-beg
1025                    (if (eq (next-single-property-change (point-min)
1026                                                         'mime-view-entity)
1027                            (point))
1028                        (point)
1029                      (point-min)))
1030              )
1031             ((eq (next-single-property-change p-beg 'mime-view-entity)
1032                  (point))
1033              (setq p-beg (point))
1034              ))
1035       (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1036       (cond ((null p-end)
1037              (setq p-end (point-max))
1038              )
1039             ((null entity-node-id)
1040              (setq p-end (point-max))
1041              )
1042             (t
1043              (save-excursion
1044                (goto-char p-end)
1045                (catch 'tag
1046                  (let (e)
1047                    (while (setq e
1048                                 (next-single-property-change
1049                                  (point) 'mime-view-entity))
1050                      (goto-char e)
1051                      (let ((rc (mime-entity-node-id
1052                                 (get-text-property (point)
1053                                                    'mime-view-entity))))
1054                        (or (equal entity-node-id
1055                                   (nthcdr (- (length rc) len) rc))
1056                            (throw 'tag nil)
1057                            ))
1058                      (setq p-end e)
1059                      ))
1060                  (setq p-end (point-max))
1061                  ))
1062              ))
1063       (let* ((mode (mime-preview-original-major-mode 'recursive))
1064              (new-name
1065               (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1066              new-buf
1067              (the-buf (current-buffer))
1068              (a-buf mime-raw-buffer)
1069              fields)
1070         (save-excursion
1071           (set-buffer (setq new-buf (get-buffer-create new-name)))
1072           (erase-buffer)
1073           (insert-buffer-substring the-buf p-beg p-end)
1074           (goto-char (point-min))
1075           (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1076             (while (progn
1077                      (setq
1078                       str
1079                       (save-excursion
1080                         (set-buffer a-buf)
1081                         (setq ci
1082                               (mime-find-entity-from-node-id entity-node-id))
1083                         (save-restriction
1084                           (narrow-to-region
1085                            (mime-entity-point-min ci)
1086                            (mime-entity-point-max ci)
1087                            )
1088                           (std11-header-string-except
1089                            (concat "^"
1090                                    (apply (function regexp-or) fields)
1091                                    ":") ""))))
1092                      (if (and
1093                           (eq (mime-entity-media-type ci) 'message)
1094                           (eq (mime-entity-media-subtype ci) 'rfc822))
1095                          nil
1096                        (if str
1097                            (insert str)
1098                          )
1099                        entity-node-id))
1100               (setq fields (std11-collect-field-names)
1101                     entity-node-id (cdr entity-node-id))
1102               )
1103             )
1104           (let ((rest mime-view-following-required-fields-list))
1105             (while rest
1106               (let ((field-name (car rest)))
1107                 (or (std11-field-body field-name)
1108                     (insert
1109                      (format
1110                       (concat field-name
1111                               ": "
1112                               (save-excursion
1113                                 (set-buffer the-buf)
1114                                 (set-buffer mime-mother-buffer)
1115                                 (set-buffer mime-raw-buffer)
1116                                 (std11-field-body field-name)
1117                                 )
1118                               "\n")))
1119                     ))
1120               (setq rest (cdr rest))
1121               ))
1122           (eword-decode-header)
1123           )
1124         (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1125           (if (functionp f)
1126               (funcall f new-buf)
1127             (message
1128              (format
1129               "Sorry, following method for %s is not implemented yet."
1130               mode))
1131             ))
1132         ))))
1133
1134
1135 ;;; @@ moving
1136 ;;;
1137
1138 (defun mime-preview-move-to-upper ()
1139   "Move to upper entity.
1140 If there is no upper entity, call function `mime-preview-quit'."
1141   (interactive)
1142   (let (cinfo)
1143     (while (null (setq cinfo
1144                        (get-text-property (point) 'mime-view-entity)))
1145       (backward-char)
1146       )
1147     (let ((r (mime-entity-parent cinfo))
1148           point)
1149       (catch 'tag
1150         (while (setq point (previous-single-property-change
1151                             (point) 'mime-view-entity))
1152           (goto-char point)
1153           (if (eq r (get-text-property (point) 'mime-view-entity))
1154               (throw 'tag t)
1155             )
1156           )
1157         (mime-preview-quit)
1158         ))))
1159
1160 (defun mime-preview-move-to-previous ()
1161   "Move to previous entity.
1162 If there is no previous entity, it calls function registered in
1163 variable `mime-preview-over-to-previous-method-alist'."
1164   (interactive)
1165   (while (null (get-text-property (point) 'mime-view-entity))
1166     (backward-char)
1167     )
1168   (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1169     (if point
1170         (if (get-text-property (1- point) 'mime-view-entity)
1171             (goto-char point)
1172           (goto-char (1- point))
1173           (mime-preview-move-to-previous)
1174           )
1175       (let ((f (assq (mime-preview-original-major-mode)
1176                      mime-preview-over-to-previous-method-alist)))
1177         (if f
1178             (funcall (cdr f))
1179           ))
1180       )))
1181
1182 (defun mime-preview-move-to-next ()
1183   "Move to next entity.
1184 If there is no previous entity, it calls function registered in
1185 variable `mime-preview-over-to-next-method-alist'."
1186   (interactive)
1187   (while (null (get-text-property (point) 'mime-view-entity))
1188     (forward-char)
1189     )
1190   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1191     (if point
1192         (progn
1193           (goto-char point)
1194           (if (null (get-text-property point 'mime-view-entity))
1195               (mime-preview-move-to-next)
1196             ))
1197       (let ((f (assq (mime-preview-original-major-mode)
1198                      mime-preview-over-to-next-method-alist)))
1199         (if f
1200             (funcall (cdr f))
1201           ))
1202       )))
1203
1204 (defun mime-preview-scroll-up-entity (&optional h)
1205   "Scroll up current entity.
1206 If reached to (point-max), it calls function registered in variable
1207 `mime-preview-over-to-next-method-alist'."
1208   (interactive)
1209   (or h
1210       (setq h (1- (window-height)))
1211       )
1212   (if (= (point) (point-max))
1213       (let ((f (assq (mime-preview-original-major-mode)
1214                      mime-preview-over-to-next-method-alist)))
1215         (if f
1216             (funcall (cdr f))
1217           ))
1218     (let ((point
1219            (or (next-single-property-change (point) 'mime-view-entity)
1220                (point-max))))
1221       (forward-line h)
1222       (if (> (point) point)
1223           (goto-char point)
1224         )
1225       )))
1226
1227 (defun mime-preview-scroll-down-entity (&optional h)
1228   "Scroll down current entity.
1229 If reached to (point-min), it calls function registered in variable
1230 `mime-preview-over-to-previous-method-alist'."
1231   (interactive)
1232   (or h
1233       (setq h (1- (window-height)))
1234       )
1235   (if (= (point) (point-min))
1236       (let ((f (assq (mime-preview-original-major-mode)
1237                      mime-preview-over-to-previous-method-alist)))
1238         (if f
1239             (funcall (cdr f))
1240           ))
1241     (let ((point
1242            (or (previous-single-property-change (point) 'mime-view-entity)
1243                (point-min))))
1244       (forward-line (- h))
1245       (if (< (point) point)
1246           (goto-char point)
1247         ))))
1248
1249 (defun mime-preview-next-line-entity ()
1250   (interactive)
1251   (mime-preview-scroll-up-entity 1)
1252   )
1253
1254 (defun mime-preview-previous-line-entity ()
1255   (interactive)
1256   (mime-preview-scroll-down-entity 1)
1257   )
1258
1259
1260 ;;; @@ quitting
1261 ;;;
1262
1263 (defun mime-preview-quit ()
1264   "Quit from MIME-preview buffer.
1265 It calls function registered in variable
1266 `mime-preview-quitting-method-alist'."
1267   (interactive)
1268   (let ((r (assq (mime-preview-original-major-mode)
1269                  mime-preview-quitting-method-alist)))
1270     (if r
1271         (funcall (cdr r))
1272       )))
1273
1274 (defun mime-preview-kill-buffer ()
1275   (interactive)
1276   (kill-buffer (current-buffer))
1277   )
1278
1279
1280 ;;; @ end
1281 ;;;
1282
1283 (provide 'mime-view)
1284
1285 (run-hooks 'mime-view-load-hook)
1286
1287 ;;; mime-view.el ends here