(mime-view-version-string): Use `mime-module-version' instead of
[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          (pcl (mime-raw-flatten-message-info message-info))
628          (the-buf (current-buffer))
629          (mode major-mode)
630          )
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       (while pcl
643         (mime-view-display-entity (car pcl) message-info the-buf obuf)
644         (setq pcl (cdr pcl))
645         )
646       (set-buffer-modified-p nil)
647       )
648     (setq buffer-read-only t)
649     (set-buffer the-buf)
650     )
651   (setq mime-preview-buffer obuf)
652   )
653
654 (defun mime-view-display-entity (entity message-info ibuf obuf)
655   "Display ENTITY."
656   (let* ((start (mime-entity-point-min entity))
657          (end (mime-entity-point-max entity))
658          (media-type (mime-entity-media-type entity))
659          (media-subtype (mime-entity-media-subtype entity))
660          (ctype (if media-type
661                     (if media-subtype
662                         (format "%s/%s" media-type media-subtype)
663                       (symbol-name media-type)
664                       )))
665          (params (mime-entity-parameters entity))
666          (encoding (mime-entity-encoding entity))
667          (entity-node-id (mime-entity-node-id entity))
668          end-of-header e nb ne subj)
669     (set-buffer ibuf)
670     (goto-char start)
671     (setq end-of-header (if (re-search-forward "^$" nil t)
672                             (1+ (match-end 0))
673                           end))
674     (if (> end-of-header end)
675         (setq end-of-header end)
676       )
677     (save-restriction
678       (narrow-to-region start end)
679       (setq subj
680             (eword-decode-string
681              (mime-raw-get-subject params encoding)))
682       )
683     (set-buffer obuf)
684     (setq nb (point))
685     (narrow-to-region nb nb)
686     (if (mime-view-entity-button-visible-p entity message-info)
687         (mime-view-insert-entity-button entity message-info subj)
688       )
689     (if (mime-view-header-visible-p entity message-info)
690         (save-restriction
691           (narrow-to-region (point)(point))
692           (insert-buffer-substring mime-raw-buffer start end-of-header)
693           (let ((f (cdr (assq mime-preview-original-major-mode
694                               mime-view-content-header-filter-alist))))
695             (if (functionp f)
696                 (funcall f)
697               (mime-view-default-content-header-filter)
698               ))
699           (run-hooks 'mime-view-content-header-filter-hook)
700           ))
701     (if (and (mime-root-entity-p entity)
702              (member
703               ctype mime-view-content-button-visible-ctype-list))
704         (save-excursion
705           (goto-char (point-max))
706           (mime-view-insert-entity-button entity message-info subj)
707           ))
708     (let* ((situation
709             (ctree-match-calist mime-preview-condition
710                                 (list* (cons 'type       media-type)
711                                        (cons 'subtype    media-subtype)
712                                        (cons 'encoding   encoding)
713                                        (cons 'major-mode major-mode)
714                                        params)))
715            (body-presentation-method
716             (cdr (assq 'body-presentation-method situation))))
717       (cond ((eq body-presentation-method 'with-filter)
718              (let ((body-filter (cdr (assq 'body-filter situation))))
719                (save-restriction
720                  (narrow-to-region (point-max)(point-max))
721                  (insert-buffer-substring mime-raw-buffer end-of-header end)
722                  (funcall body-filter situation)
723                  )))
724             ((functionp body-presentation-method)
725              (funcall body-presentation-method situation)
726              )
727             ((and (null entity-node-id)
728                   (null (mime-entity-children message-info)))
729              (goto-char (point-max))
730              (mime-view-insert-entity-button entity message-info subj)
731              ))
732       (when (mime-view-entity-separator-visible-p entity message-info)
733         (goto-char (point-max))
734         (insert "\n"))
735       )
736     (setq ne (point-max))
737     (widen)
738     (put-text-property nb ne 'mime-view-raw-buffer ibuf)
739     (put-text-property nb ne 'mime-view-entity entity)
740     (goto-char ne)
741     ))
742
743 (defun mime-raw-get-uu-filename (param &optional encoding)
744   (if (member (or encoding
745                   (cdr (assq 'encoding param))
746                   )
747               mime-view-uuencode-encoding-name-list)
748       (save-excursion
749         (or (if (re-search-forward "^begin [0-9]+ " nil t)
750                 (if (looking-at ".+$")
751                     (buffer-substring (match-beginning 0)(match-end 0))
752                   ))
753             ""))
754     ))
755
756 (defun mime-raw-get-subject (param &optional encoding)
757   (or (std11-find-field-body '("Content-Description" "Subject"))
758       (let (ret)
759         (if (or (and (setq ret (mime/Content-Disposition))
760                      (setq ret (assoc "filename" (cdr ret)))
761                      )
762                 (setq ret (assoc "name" param))
763                 (setq ret (assoc "x-name" param))
764                 )
765             (std11-strip-quoted-string (cdr ret))
766           ))
767       (mime-raw-get-uu-filename param encoding)
768       ""))
769
770
771 ;;; @ MIME viewer mode
772 ;;;
773
774 (defconst mime-view-menu-title "MIME-View")
775 (defconst mime-view-menu-list
776   '((up          "Move to upper entity"    mime-preview-move-to-upper)
777     (previous    "Move to previous entity" mime-preview-move-to-previous)
778     (next        "Move to next entity"     mime-preview-move-to-next)
779     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
780     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
781     (play        "Play current entity"     mime-preview-play-current-entity)
782     (extract     "Extract current entity"  mime-preview-extract-current-entity)
783     (print       "Print current entity"    mime-preview-print-current-entity)
784     (x-face      "Show X Face"             mime-preview-display-x-face)
785     )
786   "Menu for MIME Viewer")
787
788 (cond (running-xemacs
789        (defvar mime-view-xemacs-popup-menu
790          (cons mime-view-menu-title
791                (mapcar (function
792                         (lambda (item)
793                           (vector (nth 1 item)(nth 2 item) t)
794                           ))
795                        mime-view-menu-list)))
796        (defun mime-view-xemacs-popup-menu (event)
797          "Popup the menu in the MIME Viewer buffer"
798          (interactive "e")
799          (select-window (event-window event))
800          (set-buffer (event-buffer event))
801          (popup-menu 'mime-view-xemacs-popup-menu))
802        (defvar mouse-button-2 'button2)
803        )
804       (t
805        (defvar mouse-button-2 [mouse-2])
806        ))
807
808 (defun mime-view-define-keymap (&optional default)
809   (let ((mime-view-mode-map (if (keymapp default)
810                                 (copy-keymap default)
811                               (make-sparse-keymap)
812                               )))
813     (define-key mime-view-mode-map
814       "u"        (function mime-preview-move-to-upper))
815     (define-key mime-view-mode-map
816       "p"        (function mime-preview-move-to-previous))
817     (define-key mime-view-mode-map
818       "n"        (function mime-preview-move-to-next))
819     (define-key mime-view-mode-map
820       "\e\t"     (function mime-preview-move-to-previous))
821     (define-key mime-view-mode-map
822       "\t"       (function mime-preview-move-to-next))
823     (define-key mime-view-mode-map
824       " "        (function mime-preview-scroll-up-entity))
825     (define-key mime-view-mode-map
826       "\M- "     (function mime-preview-scroll-down-entity))
827     (define-key mime-view-mode-map
828       "\177"     (function mime-preview-scroll-down-entity))
829     (define-key mime-view-mode-map
830       "\C-m"     (function mime-preview-next-line-entity))
831     (define-key mime-view-mode-map
832       "\C-\M-m"  (function mime-preview-previous-line-entity))
833     (define-key mime-view-mode-map
834       "v"        (function mime-preview-play-current-entity))
835     (define-key mime-view-mode-map
836       "e"        (function mime-preview-extract-current-entity))
837     (define-key mime-view-mode-map
838       "\C-c\C-p" (function mime-preview-print-current-entity))
839     (define-key mime-view-mode-map
840       "a"        (function mime-preview-follow-current-entity))
841     (define-key mime-view-mode-map
842       "q"        (function mime-preview-quit))
843     (define-key mime-view-mode-map
844       "h"        (function mime-preview-show-summary))
845     (define-key mime-view-mode-map
846       "\C-c\C-x" (function mime-preview-kill-buffer))
847     ;; (define-key mime-view-mode-map
848     ;;   "<"        (function beginning-of-buffer))
849     ;; (define-key mime-view-mode-map
850     ;;   ">"        (function end-of-buffer))
851     (define-key mime-view-mode-map
852       "?"        (function describe-mode))
853     (define-key mime-view-mode-map
854       [tab] (function mime-preview-move-to-next))
855     (define-key mime-view-mode-map
856       [delete] (function mime-preview-scroll-down-entity))
857     (define-key mime-view-mode-map
858       [backspace] (function mime-preview-scroll-down-entity))
859     (if (functionp default)
860         (cond (running-xemacs
861                (set-keymap-default-binding mime-view-mode-map default)
862                )
863               (t
864                (setq mime-view-mode-map
865                      (append mime-view-mode-map (list (cons t default))))
866                )))
867     (if mouse-button-2
868         (define-key mime-view-mode-map
869           mouse-button-2 (function mime-button-dispatcher))
870       )
871     (cond (running-xemacs
872            (define-key mime-view-mode-map
873              mouse-button-3 (function mime-view-xemacs-popup-menu))
874            )
875           ((>= emacs-major-version 19)
876            (define-key mime-view-mode-map [menu-bar mime-view]
877              (cons mime-view-menu-title
878                    (make-sparse-keymap mime-view-menu-title)))
879            (mapcar (function
880                     (lambda (item)
881                       (define-key mime-view-mode-map
882                         (vector 'menu-bar 'mime-view (car item))
883                         (cons (nth 1 item)(nth 2 item))
884                         )
885                       ))
886                    (reverse mime-view-menu-list)
887                    )
888            ))
889     (use-local-map mime-view-mode-map)
890     (run-hooks 'mime-view-define-keymap-hook)
891     ))
892
893 (defsubst mime-maybe-hide-echo-buffer ()
894   "Clear mime-echo buffer and delete window for it."
895   (let ((buf (get-buffer mime-echo-buffer-name)))
896     (if buf
897         (save-excursion
898           (set-buffer buf)
899           (erase-buffer)
900           (let ((win (get-buffer-window buf)))
901             (if win
902                 (delete-window win)
903               ))
904           (bury-buffer buf)
905           ))))
906
907 (defun mime-view-mode (&optional mother ctl encoding ibuf obuf
908                                  default-keymap-or-function)
909   "Major mode for viewing MIME message.
910
911 Here is a list of the standard keys for mime-view-mode.
912
913 key             feature
914 ---             -------
915
916 u               Move to upper content
917 p or M-TAB      Move to previous content
918 n or TAB        Move to next content
919 SPC             Scroll up or move to next content
920 M-SPC or DEL    Scroll down or move to previous content
921 RET             Move to next line
922 M-RET           Move to previous line
923 v               Decode current content as `play mode'
924 e               Decode current content as `extract mode'
925 C-c C-p         Decode current content as `print mode'
926 a               Followup to current content.
927 x               Display X-Face
928 q               Quit
929 button-2        Move to point under the mouse cursor
930                 and decode current content as `play mode'
931 "
932   (interactive)
933   (mime-maybe-hide-echo-buffer)
934   (let ((ret (mime-view-setup-buffers ctl encoding ibuf obuf))
935         (win-conf (current-window-configuration))
936         )
937     (prog1
938         (switch-to-buffer ret)
939       (setq mime-preview-original-window-configuration win-conf)
940       (if mother
941           (progn
942             (setq mime-mother-buffer mother)
943             ))
944       (mime-view-define-keymap default-keymap-or-function)
945       (let ((point
946              (next-single-property-change (point-min) 'mime-view-entity)))
947         (if point
948             (goto-char point)
949           (goto-char (point-min))
950           (search-forward "\n\n" nil t)
951           ))
952       (run-hooks 'mime-view-mode-hook)
953       )))
954
955
956 ;;; @@ playing
957 ;;;
958
959 (autoload 'mime-preview-play-current-entity "mime-play"
960   "Play current entity." t)
961
962 (defun mime-preview-extract-current-entity ()
963   "Extract current entity into file (maybe).
964 It decodes current entity to call internal or external method as
965 \"extract\" mode.  The method is selected from variable
966 `mime-acting-condition'."
967   (interactive)
968   (mime-preview-play-current-entity "extract")
969   )
970
971 (defun mime-preview-print-current-entity ()
972   "Print current entity (maybe).
973 It decodes current entity to call internal or external method as
974 \"print\" mode.  The method is selected from variable
975 `mime-acting-condition'."
976   (interactive)
977   (mime-preview-play-current-entity "print")
978   )
979
980
981 ;;; @@ following
982 ;;;
983
984 (defun mime-preview-original-major-mode ()
985   "Return major-mode of original buffer.
986 If a current buffer has mime-mother-buffer, return original major-mode
987 of the mother-buffer."
988   (if mime-mother-buffer
989       (save-excursion
990         (set-buffer mime-mother-buffer)
991         (mime-preview-original-major-mode)
992         )
993     mime-preview-original-major-mode))
994
995 (defun mime-preview-follow-current-entity ()
996   "Write follow message to current entity.
997 It calls following-method selected from variable
998 `mime-view-following-method-alist'."
999   (interactive)
1000   (let ((message-info (get-text-property (point-min) 'mime-view-entity))
1001         entity)
1002     (while (null (setq entity
1003                        (get-text-property (point) 'mime-view-entity)))
1004       (backward-char)
1005       )
1006     (let* ((p-beg
1007             (previous-single-property-change (point) 'mime-view-entity))
1008            p-end
1009            (entity-node-id (mime-entity-node-id entity))
1010            (len (length entity-node-id))
1011            )
1012       (cond ((null p-beg)
1013              (setq p-beg
1014                    (if (eq (next-single-property-change (point-min)
1015                                                         'mime-view-entity)
1016                            (point))
1017                        (point)
1018                      (point-min)))
1019              )
1020             ((eq (next-single-property-change p-beg 'mime-view-entity)
1021                  (point))
1022              (setq p-beg (point))
1023              ))
1024       (setq p-end (next-single-property-change p-beg 'mime-view-entity))
1025       (cond ((null p-end)
1026              (setq p-end (point-max))
1027              )
1028             ((null entity-node-id)
1029              (setq p-end (point-max))
1030              )
1031             (t
1032              (save-excursion
1033                (goto-char p-end)
1034                (catch 'tag
1035                  (let (e)
1036                    (while (setq e
1037                                 (next-single-property-change
1038                                  (point) 'mime-view-entity))
1039                      (goto-char e)
1040                      (let ((rc (mime-entity-node-id
1041                                 (get-text-property (point)
1042                                                    'mime-view-entity))))
1043                        (or (equal entity-node-id
1044                                   (nthcdr (- (length rc) len) rc))
1045                            (throw 'tag nil)
1046                            ))
1047                      (setq p-end e)
1048                      ))
1049                  (setq p-end (point-max))
1050                  ))
1051              ))
1052       (let* ((mode (mime-preview-original-major-mode))
1053              (new-name
1054               (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1055              new-buf
1056              (the-buf (current-buffer))
1057              (a-buf mime-raw-buffer)
1058              fields)
1059         (save-excursion
1060           (set-buffer (setq new-buf (get-buffer-create new-name)))
1061           (erase-buffer)
1062           (insert-buffer-substring the-buf p-beg p-end)
1063           (goto-char (point-min))
1064           (if (mime-view-header-visible-p entity message-info)
1065               (delete-region (goto-char (point-min))
1066                              (if (re-search-forward "^$" nil t)
1067                                  (match-end 0)
1068                                (point-min)))
1069             )
1070           (goto-char (point-min))
1071           (insert "\n")
1072           (goto-char (point-min))
1073           (let ((entity-node-id (mime-entity-node-id entity)) ci str)
1074             (while (progn
1075                      (setq
1076                       str
1077                       (save-excursion
1078                         (set-buffer a-buf)
1079                         (setq
1080                          ci
1081                          (mime-raw-find-entity-from-node-id entity-node-id))
1082                         (save-restriction
1083                           (narrow-to-region
1084                            (mime-entity-point-min ci)
1085                            (mime-entity-point-max ci)
1086                            )
1087                           (std11-header-string-except
1088                            (concat "^"
1089                                    (apply (function regexp-or) fields)
1090                                    ":") ""))))
1091                      (if (and
1092                           (eq (mime-entity-media-type ci) 'message)
1093                           (eq (mime-entity-media-subtype ci) 'rfc822))
1094                          nil
1095                        (if str
1096                            (insert str)
1097                          )
1098                        entity-node-id))
1099               (setq fields (std11-collect-field-names)
1100                     entity-node-id (cdr entity-node-id))
1101               )
1102             )
1103           (let ((rest mime-view-following-required-fields-list))
1104             (while rest
1105               (let ((field-name (car rest)))
1106                 (or (std11-field-body field-name)
1107                     (insert
1108                      (format
1109                       (concat field-name
1110                               ": "
1111                               (save-excursion
1112                                 (set-buffer the-buf)
1113                                 (set-buffer mime-mother-buffer)
1114                                 (set-buffer mime-raw-buffer)
1115                                 (std11-field-body field-name)
1116                                 )
1117                               "\n")))
1118                     ))
1119               (setq rest (cdr rest))
1120               ))
1121           (eword-decode-header)
1122           )
1123         (let ((f (cdr (assq mode mime-view-following-method-alist))))
1124           (if (functionp f)
1125               (funcall f new-buf)
1126             (message
1127              (format
1128               "Sorry, following method for %s is not implemented yet."
1129               mode))
1130             ))
1131         ))))
1132
1133
1134 ;;; @@ X-Face
1135 ;;;
1136
1137 (defun mime-preview-display-x-face ()
1138   (interactive)
1139   (save-window-excursion
1140     (set-buffer mime-raw-buffer)
1141     (mime-view-x-face-function)
1142     ))
1143
1144
1145 ;;; @@ moving
1146 ;;;
1147
1148 (defun mime-preview-move-to-upper ()
1149   "Move to upper entity.
1150 If there is no upper entity, call function `mime-preview-quit'."
1151   (interactive)
1152   (let (cinfo)
1153     (while (null (setq cinfo
1154                        (get-text-property (point) 'mime-view-entity)))
1155       (backward-char)
1156       )
1157     (let ((r (mime-raw-find-entity-from-node-id
1158               (cdr (mime-entity-node-id cinfo))
1159               (get-text-property 1 'mime-view-entity)))
1160           point)
1161       (catch 'tag
1162         (while (setq point (previous-single-property-change
1163                             (point) 'mime-view-entity))
1164           (goto-char point)
1165           (if (eq r (get-text-property (point) 'mime-view-entity))
1166               (throw 'tag t)
1167             )
1168           )
1169         (mime-preview-quit)
1170         ))))
1171
1172 (defun mime-preview-move-to-previous ()
1173   "Move to previous entity.
1174 If there is no previous entity, it calls function registered in
1175 variable `mime-view-over-to-previous-method-alist'."
1176   (interactive)
1177   (while (null (get-text-property (point) 'mime-view-entity))
1178     (backward-char)
1179     )
1180   (let ((point
1181          (previous-single-property-change (point) 'mime-view-entity)))
1182     (if point
1183         (goto-char point)
1184       (let ((f (assq mime-preview-original-major-mode
1185                      mime-view-over-to-previous-method-alist)))
1186         (if f
1187             (funcall (cdr f))
1188           ))
1189       )))
1190
1191 (defun mime-preview-move-to-next ()
1192   "Move to next entity.
1193 If there is no previous entity, it calls function registered in
1194 variable `mime-view-over-to-next-method-alist'."
1195   (interactive)
1196   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1197     (if point
1198         (goto-char point)
1199       (let ((f (assq mime-preview-original-major-mode
1200                      mime-view-over-to-next-method-alist)))
1201         (if f
1202             (funcall (cdr f))
1203           ))
1204       )))
1205
1206 (defun mime-preview-scroll-up-entity (&optional h)
1207   "Scroll up current entity.
1208 If reached to (point-max), it calls function registered in variable
1209 `mime-view-over-to-next-method-alist'."
1210   (interactive)
1211   (or h
1212       (setq h (1- (window-height)))
1213       )
1214   (if (= (point) (point-max))
1215       (let ((f (assq mime-preview-original-major-mode
1216                      mime-view-over-to-next-method-alist)))
1217         (if f
1218             (funcall (cdr f))
1219           ))
1220     (let ((point
1221            (or (next-single-property-change (point) 'mime-view-entity)
1222                (point-max))))
1223       (forward-line h)
1224       (if (> (point) point)
1225           (goto-char point)
1226         )
1227       )))
1228
1229 (defun mime-preview-scroll-down-entity (&optional h)
1230   "Scroll down current entity.
1231 If reached to (point-min), it calls function registered in variable
1232 `mime-view-over-to-previous-method-alist'."
1233   (interactive)
1234   (or h
1235       (setq h (1- (window-height)))
1236       )
1237   (if (= (point) (point-min))
1238       (let ((f (assq mime-preview-original-major-mode
1239                      mime-view-over-to-previous-method-alist)))
1240         (if f
1241             (funcall (cdr f))
1242           ))
1243     (let (point)
1244       (save-excursion
1245         (catch 'tag
1246           (while (> (point) 1)
1247             (if (setq point
1248                       (previous-single-property-change (point)
1249                                                        'mime-view-entity))
1250                 (throw 'tag t)
1251               )
1252             (backward-char)
1253             )
1254           (setq point (point-min))
1255           ))
1256       (forward-line (- h))
1257       (if (< (point) point)
1258           (goto-char point)
1259         ))))
1260
1261 (defun mime-preview-next-line-entity ()
1262   (interactive)
1263   (mime-preview-scroll-up-entity 1)
1264   )
1265
1266 (defun mime-preview-previous-line-entity ()
1267   (interactive)
1268   (mime-preview-scroll-down-entity 1)
1269   )
1270
1271
1272 ;;; @@ quitting
1273 ;;;
1274
1275 (defun mime-preview-quit ()
1276   "Quit from MIME-preview buffer.
1277 It calls function registered in variable
1278 `mime-preview-quitting-method-alist'."
1279   (interactive)
1280   (let ((r (assq mime-preview-original-major-mode
1281                  mime-preview-quitting-method-alist)))
1282     (if r
1283         (funcall (cdr r))
1284       )))
1285
1286 (defun mime-preview-show-summary ()
1287   "Show summary.
1288 It calls function registered in variable
1289 `mime-view-show-summary-method'."
1290   (interactive)
1291   (let ((r (assq mime-preview-original-major-mode
1292                  mime-view-show-summary-method)))
1293     (if r
1294         (funcall (cdr r))
1295       )))
1296
1297 (defun mime-preview-kill-buffer ()
1298   (interactive)
1299   (kill-buffer (current-buffer))
1300   )
1301
1302
1303 ;;; @ end
1304 ;;;
1305
1306 (provide 'mime-view)
1307
1308 (run-hooks 'mime-view-load-hook)
1309
1310 ;;; mime-view.el ends here