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