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