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