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