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