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