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