Update
[elisp/semi.git] / mime-view.el
1 ;;; mime-view.el --- interactive MIME viewer for GNU Emacs
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 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 (Sample of Elastic 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 'emu)
31 (require 'mime)
32 (require 'semi-def)
33 (require 'calist)
34 (require 'alist)
35 (require 'mailcap)
36
37
38 ;;; @ version
39 ;;;
40
41 (defconst mime-view-version
42   (concat (mime-product-name mime-user-interface-product) " MIME-View "
43           (mapconcat #'number-to-string
44                      (mime-product-version mime-user-interface-product) ".")
45           " (" (mime-product-code-name mime-user-interface-product) ")"))
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 (defcustom mime-preview-move-scroll nil
66   "*Decides whether to scroll when moving to next entity.
67 When t, scroll the buffer.  Non-nil but not t means scroll when
68 the next entity is within `next-screen-context-lines' from top or
69 buttom.  Nil means don't scroll at all."
70   :group 'mime-view
71   :type '(choice (const :tag "Off" nil)
72                  (const :tag "On" t)
73                  (sexp :tag "Situation" 1)))
74
75 (defcustom mime-preview-scroll-full-screen nil
76   "*When non-nil, always scroll full screen.
77 If nil, point will be moved to the next entity if exists."
78   :group 'mime-view
79   :type '(choice (const :tag "On" t)
80                  (const :tag "Off" nil)))
81
82 (defcustom mime-view-force-inline-types '(text multipart)
83   "*List of MIME types that \"attachment\" should be ignored.
84 The element can be type or type/subtype. When t, inline everything
85 if possible."
86   :group 'mime-view
87   :type '(choice (const :tag "Nothing" nil)
88                  (const :tag "All" t)
89                  (list (repeat symbol))))
90
91 (defcustom mime-view-button-place-alist
92   '((message . around)
93     (application . before)
94     (multipart/alternative . around))
95   "*Alist of MIME type or type/subtype vs. button place.
96 When around, button will be inserted before and after that part.
97 When after or before, button will be inserted that place.
98 If not specified, that type will not have button."
99   :group 'mime-view
100   :type '(choice (const :tag "Nothing" nil)
101                  (list (repeat symbol))))
102
103 ;; Rename this.
104 (defcustom mime-view-type-subtype-score-alist
105   '(((text . enriched) . 3)
106     ((text . richtext) . 2)
107     ((text . plain)    . 1)
108     (t . 0))
109   "Alist MEDIA-TYPE vs corresponding score.
110 MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
111   :group 'mime-view
112   :type '(repeat (cons (choice :tag "Media-Type"
113                                (cons :tag "Type/Subtype"
114                                      (symbol :tag "Primary-type")
115                                      (symbol :tag "Subtype"))
116                                (symbol :tag "Type")
117                                (const :tag "Default" t))
118                        integer)))
119
120 ;;; @ in raw-buffer (representation space)
121 ;;;
122
123 (defvar mime-preview-buffer nil
124   "MIME-preview buffer corresponding with the (raw) buffer.")
125 (make-variable-buffer-local 'mime-preview-buffer)
126
127
128 (defvar mime-raw-representation-type-alist
129   '((mime-show-message-mode     . binary)
130     (mime-temp-message-mode     . binary)
131     (t                          . cooked))
132   "Alist of `major-mode' vs. representation-type of mime-raw-buffer.
133 Each element looks like (SYMBOL . REPRESENTATION-TYPE).  SYMBOL is
134 `major-mode' or t.  t means default.  REPRESENTATION-TYPE must be
135 `binary' or `cooked'.")
136
137
138 ;; (defun mime-raw-find-entity-from-point (point &optional message-info)
139 ;;   "Return entity from POINT in mime-raw-buffer.
140 ;; If optional argument MESSAGE-INFO is not specified,
141 ;; `mime-message-structure' is used."
142 ;;   (or message-info
143 ;;       (setq message-info mime-message-structure))
144 ;;   (if (and (<= (mime-entity-point-min message-info) point)
145 ;;            (<= point (mime-entity-point-max message-info)))
146 ;;       (let ((children (mime-entity-children message-info)))
147 ;;         (catch 'tag
148 ;;           (while children
149 ;;             (let ((ret
150 ;;                    (mime-raw-find-entity-from-point point (car children))))
151 ;;               (if ret
152 ;;                   (throw 'tag ret)
153 ;;                 ))
154 ;;             (setq children (cdr children)))
155 ;;           message-info))))
156 ;; (make-obsolete 'mime-raw-find-entity-from-point "don't use it.")
157
158
159 ;;; @ in preview-buffer (presentation space)
160 ;;;
161
162 (defvar mime-mother-buffer nil
163   "Mother buffer corresponding with the (MIME-preview) buffer.
164 If current MIME-preview buffer is generated by other buffer, such as
165 message/partial, it is called `mother-buffer'.")
166 (make-variable-buffer-local 'mime-mother-buffer)
167
168 ;; (defvar mime-raw-buffer nil
169 ;;   "Raw buffer corresponding with the (MIME-preview) buffer.")
170 ;; (make-variable-buffer-local 'mime-raw-buffer)
171
172 (defvar mime-preview-original-window-configuration nil
173   "Window-configuration before `mime-view-mode' is called.")
174 (make-variable-buffer-local 'mime-preview-original-window-configuration)
175
176 (defun mime-preview-original-major-mode (&optional recursive point)
177   "Return major-mode of original buffer.
178 If optional argument RECURSIVE is non-nil and current buffer has
179 mime-mother-buffer, it returns original major-mode of the
180 mother-buffer."
181   (if (and recursive mime-mother-buffer)
182       (save-excursion
183         (set-buffer mime-mother-buffer)
184         (mime-preview-original-major-mode recursive))
185     (cdr (assq 'major-mode
186                (get-text-property (or point
187                                       (if (> (point) (buffer-size))
188                                           (max (1- (point-max)) (point-min))
189                                         (point)))
190                                   'mime-view-situation)))))
191
192
193 ;;; @ entity information
194 ;;;
195
196 (defun mime-entity-situation (entity &optional situation)
197   "Return situation of ENTITY."
198   (let (rest param name)
199     ;; Content-Type
200     (unless (assq 'type situation)
201       (setq rest (or (mime-entity-content-type entity)
202                      (make-mime-content-type 'text 'plain))
203             situation (cons (car rest) situation)
204             rest (cdr rest)))
205     (unless (assq 'subtype situation)
206       (or rest
207           (setq rest (or (cdr (mime-entity-content-type entity))
208                          '((subtype . plain)))))
209       (setq situation (cons (car rest) situation)
210             rest (cdr rest)))
211     (while rest
212       (setq param (car rest))
213       (or (assoc (car param) situation)
214           (setq situation (cons param situation)))
215       (setq rest (cdr rest)))
216     
217     ;; Content-Disposition
218     (setq rest nil)
219     (unless (assq 'disposition-type situation)
220       (setq rest (mime-entity-content-disposition entity))
221       (if rest
222           (setq situation (cons (cons 'disposition-type
223                                       (mime-content-disposition-type rest))
224                                 situation)
225                 rest (mime-content-disposition-parameters rest))))
226     (while rest
227       (setq param (car rest)
228             name (car param))
229       (if (cond ((string= name "filename")
230                  (if (assq 'filename situation)
231                      nil
232                    (setq name 'filename)))
233                 ((string= name "creation-date")
234                  (if (assq 'creation-date situation)
235                      nil
236                    (setq name 'creation-date)))
237                 ((string= name "modification-date")
238                  (if (assq 'modification-date situation)
239                      nil
240                    (setq name 'modification-date)))
241                 ((string= name "read-date")
242                  (if (assq 'read-date situation)
243                      nil
244                    (setq name 'read-date)))
245                 ((string= name "size")
246                  (if (assq 'size situation)
247                      nil
248                    (setq name 'size)))
249                 (t (setq name (cons 'disposition name))
250                    (if (assoc name situation)
251                        nil
252                      name)))
253           (setq situation
254                 (cons (cons name (cdr param))
255                       situation)))
256       (setq rest (cdr rest)))
257     
258     ;; Content-Transfer-Encoding
259     (or (assq 'encoding situation)
260         (setq situation
261               (cons (cons 'encoding (or (mime-entity-encoding entity)
262                                         "7bit"))
263                     situation)))
264     
265     situation))
266
267 (defun mime-view-entity-title (entity)
268   (or (mime-entity-read-field entity 'Content-Description)
269       (mime-entity-read-field entity 'Subject)
270       (mime-entity-filename entity)
271       ""))
272
273
274 ;; (defsubst mime-raw-point-to-entity-node-id (point &optional message-info)
275 ;;   "Return entity-node-id from POINT in mime-raw-buffer.
276 ;; If optional argument MESSAGE-INFO is not specified,
277 ;; `mime-message-structure' is used."
278 ;;   (mime-entity-node-id (mime-raw-find-entity-from-point point message-info)))
279
280 ;; (make-obsolete 'mime-raw-point-to-entity-node-id "don't use it.")
281
282 ;; (defsubst mime-raw-point-to-entity-number (point &optional message-info)
283 ;;   "Return entity-number from POINT in mime-raw-buffer.
284 ;; If optional argument MESSAGE-INFO is not specified,
285 ;; `mime-message-structure' is used."
286 ;;   (mime-entity-number (mime-raw-find-entity-from-point point message-info)))
287
288 ;; (make-obsolete 'mime-raw-point-to-entity-number "don't use it.")
289
290 ;; (defun mime-raw-flatten-message-info (&optional message-info)
291 ;;   "Return list of entity in mime-raw-buffer.
292 ;; If optional argument MESSAGE-INFO is not specified,
293 ;; `mime-message-structure' is used."
294 ;;   (or message-info
295 ;;       (setq message-info mime-message-structure))
296 ;;   (let ((dest (list message-info))
297 ;;         (rcl (mime-entity-children message-info)))
298 ;;     (while rcl
299 ;;       (setq dest (nconc dest (mime-raw-flatten-message-info (car rcl))))
300 ;;       (setq rcl (cdr rcl)))
301 ;;     dest))
302
303 (defmacro mime-view-header-is-visible (situation)
304   `(eq (cdr (or (assq '*header ,situation)
305                 (assq 'header ,situation)))
306        'visible))
307
308 (defmacro mime-view-body-is-visible (situation)
309   `(eq (cdr (or (assq '*body ,situation)
310                 (assq 'body ,situation)))
311        'visible))
312
313 (defmacro mime-view-children-is-invisible (situation)
314   `(eq (cdr (or (assq '*children ,situation)))
315        'invisible))
316
317 ;;; @ presentation of preview
318 ;;;
319
320 ;;; @@ entity-button
321 ;;;
322
323 ;;; @@@ predicate function
324 ;;;
325
326 ;; #### fix flim
327 (defun mime-view-entity-type/subtype (entity)
328   (if (not (mime-entity-media-type entity))
329       'text/plain
330     (intern (format "%s/%s"
331                     (mime-entity-media-type entity)
332                     (mime-entity-media-subtype entity)))))
333
334 (defun mime-view-entity-button-visible-p (entity)
335   "Return non-nil if header of ENTITY is visible.
336 You can customize the visibility by changing `mime-view-button-place-alist'."
337   (or
338    ;; Check current entity
339    ;; type/subtype
340    (memq (cdr (assq (mime-view-entity-type/subtype entity)
341                     mime-view-button-place-alist))
342          '(around before))
343    ;; type
344    (memq (cdr (assq (mime-entity-media-type entity)
345                     mime-view-button-place-alist))
346          '(around before))
347    (and (mime-entity-parent entity)
348         (let ((prev-entity
349                (cadr (memq entity
350                            (reverse (mime-entity-children
351                                      (mime-entity-parent entity)))))))
352           ;; When previous entity exists
353           (and prev-entity
354                (or
355                 ;; Check previous entity
356                 ;; type/subtype
357                 (memq (cdr
358                        (assq
359                         (mime-view-entity-type/subtype prev-entity)
360                         mime-view-button-place-alist))
361                       '(around after))
362                 ;; type
363                 (memq (cdr
364                        (assq
365                         (mime-entity-media-type prev-entity)
366                         mime-view-button-place-alist))
367                       '(around after))))))
368    ;; default for everything.
369    (memq (cdr (assq t
370                     mime-view-button-place-alist))
371          '(around before))))
372
373 ;;; @@@ entity button generator
374 ;;;
375
376 (defun mime-view-insert-entity-button (entity &optional body-is-invisible)
377   "Insert entity-button of ENTITY."
378   (let ((entity-node-id (mime-entity-node-id entity))
379         (params (mime-entity-parameters entity))
380         (subject (mime-view-entity-title entity)))
381     (mime-insert-button
382      (concat
383       (let ((access-type (assoc "access-type" params))
384             (num (or (cdr (assoc "x-part-number" params))
385                      (if (consp entity-node-id)
386                          (mapconcat (function
387                                      (lambda (num)
388                                        (format "%s" (1+ num))))
389                                     (reverse entity-node-id) ".")
390                        "0"))))
391         (cond (access-type
392                (let ((server (assoc "server" params)))
393                  (setq access-type (cdr access-type))
394                  (if server
395                      (format "%s %s ([%s] %s)"
396                              num subject access-type (cdr server))
397                    (let ((site (cdr (assoc "site" params)))
398                          (dir (cdr (assoc "directory" params)))
399                          (url (cdr (assoc "url" params))))
400                      (if url
401                          (format "%s %s ([%s] %s)"
402                                  num subject access-type url)
403                        (format "%s %s ([%s] %s:%s)"
404                                num subject access-type site dir))))))
405               (t
406                (let ((media-type (mime-entity-media-type entity))
407                      (media-subtype (mime-entity-media-subtype entity))
408                      (charset (cdr (assoc "charset" params)))
409                      (encoding (mime-entity-encoding entity)))
410                  (concat
411                   num " " subject
412                   (let ((rest
413                          (format " <%s/%s%s%s>"
414                                  media-type media-subtype
415                                  (if charset
416                                      (concat "; " charset)
417                                    "")
418                                  (if encoding
419                                      (concat " (" encoding ")")
420                                    ""))))
421                     (if (>= (+ (current-column)(length rest))(window-width))
422                         "\n\t")
423                     rest))))))
424       (if body-is-invisible
425           " ..."
426         ""))
427      (function mime-preview-play-current-entity)
428      (if body-is-invisible
429          'invisible))))
430
431
432 ;;; @@ entity-header
433 ;;;
434
435 (defvar mime-header-presentation-method-alist nil
436   "Alist of major mode vs. corresponding header-presentation-method functions.
437 Each element looks like (SYMBOL . FUNCTION).
438 SYMBOL must be major mode in raw-buffer or t.  t means default.
439 Interface of FUNCTION must be (ENTITY SITUATION).")
440
441 (defvar mime-view-ignored-field-list
442   '(".*Received:" ".*Path:" ".*Id:" "^References:"
443     "^Replied:" "^Errors-To:"
444     "^Lines:" "^Sender:" ".*Host:" "^Xref:"
445     "^Content-Type:" "^Precedence:"
446     "^Status:" "^X-VM-.*:")
447   "All fields that match this list will be hidden in MIME preview buffer.
448 Each elements are regexp of field-name.")
449
450 (defvar mime-view-visible-field-list '("^Dnas.*:" "^Message-Id:")
451   "All fields that match this list will be displayed in MIME preview buffer.
452 Each elements are regexp of field-name.")
453
454
455 ;;; @@ entity-body
456 ;;;
457
458 ;;; @@@ predicate function
459 ;;;
460
461 (in-calist-package 'mime-view)
462
463 (defun mime-calist::field-match-method-as-default-rule (calist
464                                                         field-type field-value)
465   (let ((s-field (assq field-type calist)))
466     (cond ((null s-field)
467            (cons (cons field-type field-value) calist))
468           (t calist))))
469
470 (define-calist-field-match-method
471   'header #'mime-calist::field-match-method-as-default-rule)
472
473 (define-calist-field-match-method
474   'body #'mime-calist::field-match-method-as-default-rule)
475
476
477 (defvar mime-preview-condition nil
478   "Condition-tree about how to display entity.")
479
480 ;;(ctree-set-calist-strictly
481 ;; 'mime-preview-condition '((type . application)(subtype . octet-stream)
482 ;;                         (encoding . nil)
483 ;;                         (body . visible)))
484
485 (ctree-set-calist-strictly
486  'mime-preview-condition '((type . application)(subtype . t)
487                            (encoding . "7bit")
488                            (body . visible)))
489 (ctree-set-calist-strictly
490  'mime-preview-condition '((type . application)(subtype . t)
491                            (encoding . "8bit")
492                            (body . visible)))
493
494 (ctree-set-calist-strictly
495  'mime-preview-condition '((type . application)(subtype . pgp)
496                            (body . visible)))
497
498 (ctree-set-calist-strictly
499  'mime-preview-condition '((type . application)(subtype . x-latex)
500                            (body . visible)))
501
502 (ctree-set-calist-strictly
503  'mime-preview-condition '((type . application)(subtype . x-selection)
504                            (body . visible)))
505
506 (ctree-set-calist-strictly
507  'mime-preview-condition '((type . application)(subtype . x-comment)
508                            (body . visible)))
509
510 (ctree-set-calist-strictly
511  'mime-preview-condition '((type . message)(subtype . delivery-status)
512                            (body . visible)))
513
514 (ctree-set-calist-strictly
515  'mime-preview-condition
516  '((body . visible)
517    (body-presentation-method . mime-display-text/plain)))
518
519 (ctree-set-calist-strictly
520  'mime-preview-condition
521  '((type . nil)
522    (body . visible)
523    (body-presentation-method . mime-display-text/plain)))
524
525 (ctree-set-calist-strictly
526  'mime-preview-condition
527  '((type . text)(subtype . enriched)
528    (body . visible)
529    (body-presentation-method . mime-display-text/enriched)))
530
531 (ctree-set-calist-strictly
532  'mime-preview-condition
533  '((type . text)(subtype . richtext)
534    (body . visible)
535    (body-presentation-method . mime-display-text/richtext)))
536
537 (ctree-set-calist-strictly
538  'mime-preview-condition
539  '((type . application)(subtype . x-postpet)
540    (body . visible)
541    (body-presentation-method . mime-display-application/x-postpet)))
542
543 (ctree-set-calist-strictly
544  'mime-preview-condition '((type . application)(subtype . t)
545                            (encoding . t)
546                            (body . invisible)
547                            (body-presentation-method . mime-display-detect-application/octet-stream)))
548
549 (ctree-set-calist-strictly
550  'mime-preview-condition
551  '((type . text)(subtype . t)
552    (body . visible)
553    (body-presentation-method . mime-display-text/plain)))
554
555 (ctree-set-calist-strictly
556  'mime-preview-condition
557  '((type . text)(subtype . x-rot13-47-48)
558    (body . visible)
559    (body-presentation-method . mime-display-text/x-rot13-47-48)))
560
561 (ctree-set-calist-strictly
562  'mime-preview-condition
563  '((type . multipart)(subtype . alternative)
564    (body . visible)
565    (body-presentation-method . mime-display-multipart/alternative)))
566
567 (ctree-set-calist-strictly
568  'mime-preview-condition '((type . message)(subtype . partial)
569                            (body-presentation-method
570                             . mime-display-message/partial-button)))
571
572 (ctree-set-calist-strictly
573  'mime-preview-condition '((type . message)(subtype . rfc822)
574                            (body-presentation-method . nil)
575                            (childrens-situation (header . visible)
576                                                 (entity-button . invisible))))
577
578 (ctree-set-calist-strictly
579  'mime-preview-condition '((type . message)(subtype . news)
580                            (body-presentation-method . nil)
581                            (childrens-situation (header . visible)
582                                                 (entity-button . invisible))))
583
584
585 ;;; @@@ entity presentation
586 ;;;
587
588 (defun mime-display-text/plain (entity situation)
589   (save-restriction
590     (narrow-to-region (point-max)(point-max))
591     (mime-insert-text-content entity)
592     (run-hooks 'mime-text-decode-hook)
593     (goto-char (point-max))
594     (if (not (eq (char-after (1- (point))) ?\n))
595         (insert "\n"))
596     (mime-add-url-buttons)
597     (run-hooks 'mime-display-text/plain-hook)))
598
599 (defun mime-display-text/richtext (entity situation)
600   (save-restriction
601     (narrow-to-region (point-max)(point-max))
602     (mime-insert-text-content entity)
603     (run-hooks 'mime-text-decode-hook)
604     (let ((beg (point-min)))
605       (remove-text-properties beg (point-max) '(face nil))
606       (richtext-decode beg (point-max)))))
607
608 (defun mime-display-text/enriched (entity situation)
609   (save-restriction
610     (narrow-to-region (point-max)(point-max))
611     (mime-insert-text-content entity)
612     (run-hooks 'mime-text-decode-hook)
613     (let ((beg (point-min)))
614       (remove-text-properties beg (point-max) '(face nil))
615       (enriched-decode beg (point-max)))))
616
617 (defun mime-display-text/x-rot13-47-48 (entity situation)
618   (save-restriction
619     (narrow-to-region (point-max)(point-max))
620     (mime-insert-text-content entity)
621     (goto-char (point-max))
622     (if (not (eq (char-after (1- (point))) ?\n))
623         (insert "\n"))
624     (mule-caesar-region (point-min) (point-max))
625     (mime-add-url-buttons)))
626
627 (put 'unpack 'lisp-indent-function 1)
628 (defmacro unpack (string &rest body)
629   `(let* ((*unpack*string* (string-as-unibyte ,string))
630           (*unpack*index* 0))
631      ,@body))
632
633 (defun unpack-skip (len)
634   (setq *unpack*index* (+ len *unpack*index*)))
635
636 (defun unpack-fixed (len)
637   (prog1
638       (substring *unpack*string* *unpack*index* (+ *unpack*index* len))
639     (unpack-skip len)))
640
641 (defun unpack-byte ()
642   (char-int (aref (unpack-fixed 1) 0)))
643
644 (defun unpack-short ()
645   (let* ((b0 (unpack-byte))
646          (b1 (unpack-byte)))
647     (+ (* 256 b0) b1)))
648
649 (defun unpack-long ()
650   (let* ((s0 (unpack-short))
651          (s1 (unpack-short)))
652     (+ (* 65536 s0) s1)))
653
654 (defun unpack-string ()
655   (let ((len (unpack-byte)))
656     (unpack-fixed len)))
657
658 (defun unpack-string-sjis ()
659   (decode-mime-charset-string (unpack-string) 'shift_jis))
660
661 (defun postpet-decode (string)
662   (condition-case nil
663       (unpack string
664         (let (res)
665           (unpack-skip 4)
666           (set-alist 'res 'carryingcount (unpack-long))
667           (unpack-skip 8)
668           (set-alist 'res 'sentyear (unpack-short))
669           (set-alist 'res 'sentmonth (unpack-short))
670           (set-alist 'res 'sentday (unpack-short))
671           (unpack-skip 8)
672           (set-alist 'res 'petname (unpack-string-sjis))
673           (set-alist 'res 'owner (unpack-string-sjis))
674           (set-alist 'res 'pettype (unpack-fixed 4))
675           (set-alist 'res 'health (unpack-short))
676           (unpack-skip 2)
677           (set-alist 'res 'sex (unpack-long))
678           (unpack-skip 1)
679           (set-alist 'res 'brain (unpack-byte))
680           (unpack-skip 39)
681           (set-alist 'res 'happiness (unpack-byte))
682           (unpack-skip 14)
683           (set-alist 'res 'petbirthyear (unpack-short))
684           (set-alist 'res 'petbirthmonth (unpack-short))
685           (set-alist 'res 'petbirthday (unpack-short))
686           (unpack-skip 8)
687           (set-alist 'res 'from (unpack-string))
688           (unpack-skip 5)
689           (unpack-skip 160)
690           (unpack-skip 4)
691           (unpack-skip 8)
692           (unpack-skip 8)
693           (unpack-skip 26)
694           (set-alist 'res 'treasure (unpack-short))
695           (set-alist 'res 'money (unpack-long))
696           res))
697     (error nil)))
698
699 (defun mime-display-application/x-postpet (entity situation)
700   (save-restriction
701     (narrow-to-region (point-max)(point-max))
702     (let ((pet (postpet-decode (mime-entity-content entity))))
703       (if pet
704           (insert "Petname: " (cdr (assq 'petname pet)) "\n"
705                   "Owner: " (cdr (assq 'owner pet)) "\n"
706                   "Pettype: " (cdr (assq 'pettype pet)) "\n"
707                   "From: " (cdr (assq 'from pet)) "\n"
708                   "CarryingCount: " (int-to-string (cdr (assq 'carryingcount pet))) "\n"
709                   "SentYear: " (int-to-string (cdr (assq 'sentyear pet))) "\n"
710                   "SentMonth: " (int-to-string (cdr (assq 'sentmonth pet))) "\n"
711                   "SentDay: " (int-to-string (cdr (assq 'sentday pet))) "\n"
712                   "PetbirthYear: " (int-to-string (cdr (assq 'petbirthyear pet))) "\n"
713                   "PetbirthMonth: " (int-to-string (cdr (assq 'petbirthmonth pet))) "\n"
714                   "PetbirthDay: " (int-to-string (cdr (assq 'petbirthday pet))) "\n"
715                   "Health: " (int-to-string (cdr (assq 'health pet))) "\n"
716                   "Sex: " (int-to-string (cdr (assq 'sex pet))) "\n"
717                   "Brain: " (int-to-string (cdr (assq 'brain pet))) "\n"
718                   "Happiness: " (int-to-string (cdr (assq 'happiness pet))) "\n"
719                   "Treasure: " (int-to-string (cdr (assq 'treasure pet))) "\n"
720                   "Money: " (int-to-string (cdr (assq 'money pet))) "\n")
721         (insert "Invalid format\n"))
722       (run-hooks 'mime-display-application/x-postpet-hook))))
723
724
725 (defvar mime-view-announcement-for-message/partial
726   (if (and (>= emacs-major-version 19) window-system)
727       "\
728 \[[ This is message/partial style split message. ]]
729 \[[ Please press `v' key in this buffer          ]]
730 \[[ or click here by mouse button-2.             ]]"
731     "\
732 \[[ This is message/partial style split message. ]]
733 \[[ Please press `v' key in this buffer.         ]]"))
734
735 (defun mime-display-message/partial-button (&optional entity situation)
736   (save-restriction
737     (goto-char (point-max))
738     (if (not (search-backward "\n\n" nil t))
739         (insert "\n"))
740     (goto-char (point-max))
741     (narrow-to-region (point-max)(point-max))
742     (insert mime-view-announcement-for-message/partial)
743     (mime-add-button (point-min)(point-max)
744                      #'mime-preview-play-current-entity)))
745
746 (defun mime-display-multipart/mixed (entity situation)
747   (let ((children (mime-entity-children entity))
748         (original-major-mode-cell (assq 'major-mode situation))
749         (default-situation
750           (cdr (assq 'childrens-situation situation))))
751     (if original-major-mode-cell
752         (setq default-situation
753               (cons original-major-mode-cell default-situation)))
754     (while children
755       (mime-display-entity (car children) nil default-situation)
756       (setq children (cdr children)))))
757
758 (defun mime-display-multipart/alternative (entity situation)
759   (let* ((children (mime-entity-children entity))
760          (original-major-mode-cell (assq 'major-mode situation))
761          (default-situation
762            (cdr (assq 'childrens-situation situation)))
763          (i 0)
764          (p 0)
765          (max-score 0)
766          situations)
767     (if original-major-mode-cell
768         (setq default-situation
769               (cons original-major-mode-cell default-situation)))
770     (setq situations
771           (mapcar (function
772                    (lambda (child)
773                      (let ((situation
774                             (or (ctree-match-calist
775                                  mime-preview-condition
776                                  (append (mime-entity-situation child)
777                                          default-situation))
778                                 default-situation)))
779                        (if (cdr (assq 'body-presentation-method situation))
780                            (let ((score
781                                   (cdr
782                                    (or (assoc
783                                         (cons
784                                          (cdr (assq 'type situation))
785                                          (cdr (assq 'subtype situation)))
786                                         mime-view-type-subtype-score-alist)
787                                        (assq
788                                         (cdr (assq 'type situation))
789                                         mime-view-type-subtype-score-alist)
790                                        (assq
791                                         t
792                                         mime-view-type-subtype-score-alist)))))
793                              (if (> score max-score)
794                                  (setq p i
795                                        max-score score))))
796                        (setq i (1+ i))
797                        situation)))
798                   children))
799     (setq i 0)
800     (while children
801       (let ((child (car children))
802             (situation (car situations)))
803         (mime-display-entity child (if (= i p)
804                                        situation
805                                      (del-alist 'body-presentation-method
806                                                 (copy-alist situation)))))
807       (setq children (cdr children)
808             situations (cdr situations)
809             i (1+ i)))))
810
811 (defun mime-display-detect-application/octet-stream (entity situation)
812   "Detect unknown ENTITY and display it inline.
813 This can only handle gzipped contents."
814   (or (and (mime-entity-filename entity)
815            (string-match "\\.gz$" (mime-entity-filename entity))
816            (mime-display-gzipped entity situation))
817       (mime-display-text/plain entity situation)))
818
819 (defun mime-display-gzipped (entity situation)
820   "Ungzip gzipped part and display."
821     (insert
822      (with-temp-buffer
823        (insert (mime-entity-content entity))
824        (as-binary-process
825         (call-process-region (point-min) (point-max) "gzip" t t
826                              nil "-cd"))
827        (buffer-string (point-min) (point-max))))
828     t)
829
830 (defun mime-preview-inline ()
831   "View part as text without code conversion."
832   (interactive)
833   (let ((inhibit-read-only t)
834         (entity (get-text-property (point) 'mime-view-entity))
835         (situation (get-text-property (point) 'mime-view-situation))
836         start)
837     (when (and entity
838                (not (get-text-property (point) 'mime-view-entity-header))
839                (not (memq (mime-entity-media-type entity)
840                           '(multipart message))))
841       (setq start (or (and (not (mime-entity-parent entity))
842                            (1+ (previous-single-property-change
843                                 (point)
844                                 'mime-view-entity-header)))
845                       (and (not (eq (point) (point-min)))
846                            (not (eq (get-text-property (1- (point))
847                                                        'mime-view-entity)
848                                     entity))
849                            (point))
850                       (previous-single-property-change (point)
851                                                    'mime-view-entity)
852                       (point)))
853       (delete-region start
854                      (1-
855                       (or (next-single-property-change (point)
856                                                        'mime-view-entity)
857                           (point-max))))
858       (setq start (point))
859       (if (mime-view-entity-button-visible-p entity)
860           (mime-view-insert-entity-button entity))
861       (insert (mime-entity-content entity))
862       (if (and (bolp) (eolp))
863           (delete-char 1)
864         (forward-char 1))
865       (add-text-properties start (point)
866                            (list 'mime-view-entity entity
867                                  'mime-view-situation situation))
868       (goto-char start))))
869
870 (defun mime-preview-text (&optional ask-coding)
871   "View part as text. MIME charset will be guessed automatically.
872 With prefix, it prompts for coding-system."
873   (interactive "P")
874   (let ((inhibit-read-only t)
875         (entity (get-text-property (point) 'mime-view-entity))
876         (situation (get-text-property (point) 'mime-view-situation))
877         (coding (if ask-coding
878                     (or (read-coding-system "Coding system: ")
879                         'undecided)
880                   'undecided))
881         start)
882     (when (and entity
883                (not (get-text-property (point) 'mime-view-entity-header))
884                (not (memq (mime-entity-media-type entity)
885                           '(multipart message))))
886       (setq start (or (and (not (mime-entity-parent entity))
887                            (1+ (previous-single-property-change
888                                 (point)
889                                 'mime-view-entity-header)))
890                       (and (not (eq (point) (point-min)))
891                            (not (eq (get-text-property (1- (point))
892                                                        'mime-view-entity)
893                                     entity))
894                            (point))
895                       (previous-single-property-change (point)
896                                                        'mime-view-entity)
897                       (point)))
898       (delete-region start
899                      (1-
900                       (or (next-single-property-change (point)
901                                                        'mime-view-entity)
902                           (point-max))))
903       (setq start (point))
904       (if (mime-view-entity-button-visible-p entity)
905           (mime-view-insert-entity-button entity))
906       (insert (decode-coding-string (mime-entity-content entity) coding))
907       (if (and (bolp) (eolp))
908           (delete-char 1)
909         (forward-char 1))
910       (add-text-properties start (point)
911                            (list 'mime-view-entity entity
912                                  'mime-view-situation situation))
913       (goto-char start))))
914               
915
916 (defun mime-preview-type ()
917   "View part as text without code conversion."
918   (interactive)
919   (let ((inhibit-read-only t)
920         (entity (get-text-property (point) 'mime-view-entity))
921         (situation (get-text-property (point) 'mime-view-situation))
922         (mime-view-force-inline-types t)
923         start)
924     (when (and entity
925                (not (get-text-property (point) 'mime-view-entity-header))
926                (not (memq (mime-entity-media-type entity)
927                           '(multipart message))))
928       (setq start (or (and (not (mime-entity-parent entity))
929                            (1+ (previous-single-property-change
930                                 (point)
931                                'mime-view-entity-header)))
932                       (and (not (eq (point) (point-min)))
933                            (not (eq (get-text-property (1- (point))
934                                                        'mime-view-entity)
935                                     entity))
936                            (point))
937                       (previous-single-property-change (point)
938                                                    'mime-view-entity)
939                       (point)))
940       (delete-region start
941                      (1-
942                       (or (next-single-property-change (point)
943                                                        'mime-view-entity)
944                           (point-max))))
945       (save-excursion
946         (save-restriction
947           (narrow-to-region (point) (point))
948           (mime-display-entity entity (if (eq (assq 'body situation)
949                                               'invisible)
950                                           situation
951                                         (put-alist 'body 'visible
952                                                    situation))))
953         (if (and (bolp) (eolp))
954               (delete-char 1))))))
955
956 (defun mime-preview-buttonize ()
957   (interactive)
958   (save-excursion
959     (goto-char (point-min))
960     (let ((inhibit-read-only t)
961           point)
962       (while (setq point (next-single-property-change
963                           (point) 'mime-view-entity))
964         (goto-char point)
965         (unless (get-text-property (point) 'mime-button-callback)
966           (mime-view-insert-entity-button
967            (get-text-property (point) 'mime-view-entity)))))))
968
969 (defun mime-preview-unbuttonize ()
970   (interactive)
971   (save-excursion
972     (goto-char (point-min))
973     (let ((inhibit-read-only t)
974           point)
975       (while (setq point (next-single-property-change
976                           (point) 'mime-view-entity))
977         (goto-char point)
978         (if (get-text-property (point) 'mime-button-callback)
979             (delete-region (point) (save-excursion
980                                      (goto-char
981                                       (next-single-property-change
982                                        (point) 'mime-button-callback)))))))))
983           
984
985 ;;; @ acting-condition
986 ;;;
987
988 (defvar mime-acting-condition nil
989   "Condition-tree about how to process entity.")
990
991 (if (file-readable-p mailcap-file)
992     (let ((entries (mailcap-parse-file)))
993       (while entries
994         (let ((entry (car entries))
995               view print shared)
996           (while entry
997             (let* ((field (car entry))
998                    (field-type (car field)))
999               (cond ((eq field-type 'view)  (setq view field))
1000                     ((eq field-type 'print) (setq print field))
1001                     ((memq field-type '(compose composetyped edit)))
1002                     (t (setq shared (cons field shared)))))
1003             (setq entry (cdr entry)))
1004           (setq shared (nreverse shared))
1005           (ctree-set-calist-with-default
1006            'mime-acting-condition
1007            (append shared (list '(mode . "play")(cons 'method (cdr view)))))
1008           (if print
1009               (ctree-set-calist-with-default
1010                'mime-acting-condition
1011                (append shared
1012                        (list '(mode . "print")(cons 'method (cdr view)))))))
1013         (setq entries (cdr entries)))))
1014
1015 (ctree-set-calist-strictly
1016  'mime-acting-condition
1017  '((type . application)(subtype . octet-stream)
1018    (mode . "play")
1019    (method . mime-detect-content)))
1020
1021 (ctree-set-calist-with-default
1022  'mime-acting-condition
1023  '((mode . "extract")
1024    (method . mime-save-content)))
1025
1026 (ctree-set-calist-strictly
1027  'mime-acting-condition
1028  '((type . text)(subtype . x-rot13-47)(mode . "play")
1029    (method . mime-view-caesar)))
1030 (ctree-set-calist-strictly
1031  'mime-acting-condition
1032  '((type . text)(subtype . x-rot13-47-48)(mode . "play")
1033    (method . mime-view-caesar)))
1034
1035 (ctree-set-calist-strictly
1036  'mime-acting-condition
1037  '((type . message)(subtype . rfc822)(mode . "play")
1038    (method . mime-view-message/rfc822)))
1039 (ctree-set-calist-strictly
1040  'mime-acting-condition
1041  '((type . message)(subtype . partial)(mode . "play")
1042    (method . mime-store-message/partial-piece)))
1043
1044 (ctree-set-calist-strictly
1045  'mime-acting-condition
1046  '((type . message)(subtype . external-body)
1047    ("access-type" . "anon-ftp")
1048    (method . mime-view-message/external-anon-ftp)))
1049
1050 (ctree-set-calist-strictly
1051  'mime-acting-condition
1052  '((type . message)(subtype . external-body)
1053    ("access-type" . "url")
1054    (method . mime-view-message/external-url)))
1055
1056 (ctree-set-calist-strictly
1057  'mime-acting-condition
1058  '((type . application)(subtype . octet-stream)
1059    (method . mime-save-content)))
1060
1061
1062 ;;; @ quitting method
1063 ;;;
1064
1065 (defvar mime-preview-quitting-method-alist
1066   '((mime-show-message-mode
1067      . mime-preview-quitting-method-for-mime-show-message-mode))
1068   "Alist of `major-mode' vs. quitting-method of mime-view.")
1069
1070 (defvar mime-preview-over-to-previous-method-alist nil
1071   "Alist of `major-mode' vs. over-to-previous-method of mime-view.")
1072
1073 (defvar mime-preview-over-to-next-method-alist nil
1074   "Alist of `major-mode' vs. over-to-next-method of mime-view.")
1075
1076
1077 ;;; @ following method
1078 ;;;
1079
1080 (defvar mime-preview-following-method-alist nil
1081   "Alist of `major-mode' vs. following-method of mime-view.")
1082
1083 (defvar mime-view-following-required-fields-list
1084   '("From"))
1085
1086
1087 ;;; @ buffer setup
1088 ;;;
1089
1090 (defun mime-display-entity (entity &optional situation
1091                                    default-situation preview-buffer)
1092   "Display mime-entity ENTITY."
1093   (or preview-buffer
1094       (setq preview-buffer (current-buffer)))
1095   (in-calist-package 'mime-view)
1096   (or situation
1097       (setq situation
1098             (or (ctree-match-calist mime-preview-condition
1099                                     (append (mime-entity-situation entity)
1100                                             default-situation))
1101                 default-situation)))
1102   (let ((button-is-visible
1103          ;; Kludge.
1104          (or (eq (or (cdr (assq '*entity-button situation))
1105                      (cdr (assq 'entity-button situation)))
1106                  'visible)
1107              (and (not (eq (or (cdr (assq '*entity-button situation))
1108                                (cdr (assq 'entity-button situation)))
1109                            'invisible))
1110                   (mime-view-entity-button-visible-p entity))))
1111         (header-is-visible
1112          (mime-view-header-is-visible situation))
1113         (header-presentation-method
1114          (or (cdr (assq 'header-presentation-method situation))
1115              (cdr (assq (cdr (assq 'major-mode situation))
1116                         mime-header-presentation-method-alist))))
1117         (body-is-visible
1118          (mime-view-body-is-visible situation))
1119         (body-presentation-method
1120          (cdr (assq 'body-presentation-method situation)))
1121         (children (mime-entity-children entity))
1122         (children-is-invisible (eq (cdr (assq '*children situation))
1123                                    'invisible))
1124         nb ne nhb nbb)
1125     ;; Check if attachment is specified.
1126     ;; if inline is forced or not.
1127     (unless (or (eq t mime-view-force-inline-types)
1128                 (memq (mime-entity-media-type entity)
1129                       mime-view-force-inline-types)
1130                 (memq (mime-view-entity-type/subtype entity)
1131                       mime-view-force-inline-types)
1132                 ;; whether Content-Disposition header exists.
1133                 (not (mime-entity-content-disposition entity))
1134                 (eq 'inline
1135                     (mime-content-disposition-type
1136                      (mime-entity-content-disposition entity))))
1137       ;; This is attachment
1138       (setq header-is-visible nil
1139             body-is-visible nil)
1140       (put-alist 'header 'invisible situation)
1141       (put-alist 'body 'invisible situation))
1142     (set-buffer preview-buffer)
1143     (setq nb (point))
1144     (save-restriction
1145       (narrow-to-region nb nb)
1146       (if button-is-visible
1147           (mime-view-insert-entity-button entity
1148                                           ;; work around composite type
1149                                           (not (or children
1150                                                    body-is-visible))))
1151       (when header-is-visible
1152         (setq nhb (point))
1153         (if header-presentation-method
1154             (funcall header-presentation-method entity situation)
1155           (mime-insert-header entity
1156                               mime-view-ignored-field-list
1157                               mime-view-visible-field-list))
1158         (run-hooks 'mime-display-header-hook)
1159         (put-text-property nhb (point-max) 'mime-view-entity-header entity)
1160         (goto-char (point-max))
1161         (insert "\n"))
1162       (setq nbb (point))
1163       (cond (children)
1164             ((and body-is-visible
1165                   (functionp body-presentation-method))
1166              (funcall body-presentation-method entity situation))
1167             (t
1168              ;; When both body and button is not displayed,
1169              ;; there should be a button to indicate there's a part.
1170              (unless button-is-visible
1171                (goto-char (point-max))
1172                (mime-view-insert-entity-button entity
1173                                                ;; work around composite type
1174                                                (not (or children
1175                                                         body-is-visible))))
1176              (unless header-is-visible
1177                (goto-char (point-max))
1178                (insert "\n"))))
1179       (setq ne (point-max)))
1180     (put-text-property nb ne 'mime-view-entity entity)
1181     (put-text-property nb ne 'mime-view-situation situation)
1182     (put-text-property nbb ne 'mime-view-entity-body entity)
1183     (goto-char ne)
1184     (if (and children
1185              (not children-is-invisible))
1186         (if (functionp body-presentation-method)
1187             (funcall body-presentation-method entity situation)
1188           (mime-display-multipart/mixed entity situation)))))
1189
1190 ;;; @ MIME viewer mode
1191 ;;;
1192
1193 (defconst mime-view-menu-title "MIME-View")
1194 (defconst mime-view-menu-list
1195   '((up          "Move to upper entity"    mime-preview-move-to-upper)
1196     (previous    "Move to previous entity" mime-preview-move-to-previous)
1197     (next        "Move to next entity"     mime-preview-move-to-next)
1198     (scroll-down "Scroll-down"             mime-preview-scroll-down-entity)
1199     (scroll-up   "Scroll-up"               mime-preview-scroll-up-entity)
1200     (play        "Play current entity"     mime-preview-play-current-entity)
1201     (extract     "Extract current entity"  mime-preview-extract-current-entity)
1202     (print       "Print current entity"    mime-preview-print-current-entity)
1203     (raw "View text without code conversion" mime-preview-inline)
1204     (text "View text with code conversion" mime-preview-text)
1205     (type "View internally as type" mime-preview-type))
1206   "Menu for MIME Viewer.")
1207
1208 (cond ((featurep 'xemacs)
1209        (defvar mime-view-xemacs-popup-menu
1210          (cons mime-view-menu-title
1211                (mapcar (function
1212                         (lambda (item)
1213                           (vector (nth 1 item)(nth 2 item) t)))
1214                        mime-view-menu-list)))
1215        (defun mime-view-xemacs-popup-menu (event)
1216          "Popup the menu in the MIME Viewer buffer"
1217          (interactive "e")
1218          (select-window (event-window event))
1219          (set-buffer (event-buffer event))
1220          (popup-menu 'mime-view-xemacs-popup-menu))
1221        (defvar mouse-button-2 'button2))
1222       (t
1223        (defvar mouse-button-2 [mouse-2])))
1224
1225 (defun mime-view-define-keymap (&optional default)
1226   (let ((mime-view-mode-map (if (keymapp default)
1227                                 (copy-keymap default)
1228                               (make-sparse-keymap))))
1229     (define-key mime-view-mode-map
1230       "u"        (function mime-preview-move-to-upper))
1231     (define-key mime-view-mode-map
1232       "p"        (function mime-preview-move-to-previous))
1233     (define-key mime-view-mode-map
1234       "n"        (function mime-preview-move-to-next))
1235     (define-key mime-view-mode-map
1236       "\e\t"     (function mime-preview-move-to-previous))
1237     (define-key mime-view-mode-map
1238       "\t"       (function mime-preview-move-to-next))
1239     (define-key mime-view-mode-map
1240       " "        (function mime-preview-scroll-up-entity))
1241     (define-key mime-view-mode-map
1242       "\M- "     (function mime-preview-scroll-down-entity))
1243     (define-key mime-view-mode-map
1244       "\177"     (function mime-preview-scroll-down-entity))
1245     (define-key mime-view-mode-map
1246       "\C-m"     (function mime-preview-next-line-entity))
1247     (define-key mime-view-mode-map
1248       "\C-\M-m"  (function mime-preview-previous-line-entity))
1249     (define-key mime-view-mode-map
1250       "v"        (function mime-preview-play-current-entity))
1251     (define-key mime-view-mode-map
1252       "e"        (function mime-preview-extract-current-entity))
1253     (define-key mime-view-mode-map
1254       "\C-c\C-e"        (function mime-preview-extract-current-entity))
1255     (define-key mime-view-mode-map
1256       "i"        (function mime-preview-inline))
1257     (define-key mime-view-mode-map
1258       "c"        (function mime-preview-text))
1259     (define-key mime-view-mode-map
1260       "t"        (function mime-preview-type))
1261     (define-key mime-view-mode-map
1262       "b"        (function mime-preview-buttonize))
1263     (define-key mime-view-mode-map
1264       "B"        (function mime-preview-unbuttonize))
1265     (define-key mime-view-mode-map
1266       "\C-c\C-t\C-h" (function mime-preview-toggle-header))
1267     (define-key mime-view-mode-map
1268       "\C-c\C-t\C-c" (function mime-preview-toggle-content))
1269     (define-key mime-view-mode-map
1270       "\C-c\C-p" (function mime-preview-print-current-entity))
1271     (define-key mime-view-mode-map
1272       "a"        (function mime-preview-follow-current-entity))
1273     (define-key mime-view-mode-map
1274       "q"        (function mime-preview-quit))
1275     (define-key mime-view-mode-map
1276       "\C-c\C-x" (function mime-preview-kill-buffer))
1277     ;; (define-key mime-view-mode-map
1278     ;;   "<"        (function beginning-of-buffer))
1279     ;; (define-key mime-view-mode-map
1280     ;;   ">"        (function end-of-buffer))
1281     (define-key mime-view-mode-map
1282       "?"        (function describe-mode))
1283     (define-key mime-view-mode-map
1284       [tab] (function mime-preview-move-to-next))
1285     (define-key mime-view-mode-map
1286       [delete] (function mime-preview-scroll-down-entity))
1287     (define-key mime-view-mode-map
1288       [backspace] (function mime-preview-scroll-down-entity))
1289     (if (functionp default)
1290         (cond ((featurep 'xemacs)
1291                (set-keymap-default-binding mime-view-mode-map default))
1292               (t
1293                (setq mime-view-mode-map
1294                      (append mime-view-mode-map (list (cons t default)))))))
1295     (if mouse-button-2
1296         (define-key mime-view-mode-map
1297           mouse-button-2 (function mime-button-dispatcher)))
1298     (cond ((featurep 'xemacs)
1299            (define-key mime-view-mode-map
1300              mouse-button-3 (function mime-view-xemacs-popup-menu)))
1301           ((>= emacs-major-version 19)
1302            (define-key mime-view-mode-map [menu-bar mime-view]
1303              (cons mime-view-menu-title
1304                    (make-sparse-keymap mime-view-menu-title)))
1305            (mapcar (function
1306                     (lambda (item)
1307                       (define-key mime-view-mode-map
1308                         (vector 'menu-bar 'mime-view (car item))
1309                         (cons (nth 1 item)(nth 2 item)))))
1310                    (reverse mime-view-menu-list))))
1311     (use-local-map mime-view-mode-map)
1312     (run-hooks 'mime-view-define-keymap-hook)))
1313
1314 (defsubst mime-maybe-hide-echo-buffer ()
1315   "Clear mime-echo buffer and delete window for it."
1316   (let ((buf (get-buffer mime-echo-buffer-name)))
1317     (if buf
1318         (save-excursion
1319           (set-buffer buf)
1320           (erase-buffer)
1321           (let ((win (get-buffer-window buf)))
1322             (if win
1323                 (delete-window win)))
1324           (bury-buffer buf)))))
1325
1326 (defvar mime-view-redisplay nil)
1327
1328 ;;;###autoload
1329 (defun mime-display-message (message &optional preview-buffer
1330                                      mother default-keymap-or-function
1331                                      original-major-mode)
1332   "View MESSAGE in MIME-View mode.
1333
1334 Optional argument PREVIEW-BUFFER specifies the buffer of the
1335 presentation.  It must be either nil or a name of preview buffer.
1336
1337 Optional argument MOTHER specifies mother-buffer of the preview-buffer.
1338
1339 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1340 function.  If it is a keymap, keymap of MIME-View mode will be added
1341 to it.  If it is a function, it will be bound as default binding of
1342 keymap of MIME-View mode."
1343   (mime-maybe-hide-echo-buffer)
1344   (let ((win-conf (current-window-configuration)))
1345     (or preview-buffer
1346         (setq preview-buffer
1347               (concat "*Preview-" (mime-entity-name message) "*")))
1348     (or original-major-mode
1349         (setq original-major-mode major-mode))
1350     (let ((inhibit-read-only t))
1351       (set-buffer (get-buffer-create preview-buffer))
1352       (widen)
1353       (erase-buffer)
1354       (if mother
1355           (setq mime-mother-buffer mother))
1356       (setq mime-preview-original-window-configuration win-conf)
1357       (setq major-mode 'mime-view-mode)
1358       (setq mode-name "MIME-View")
1359       (mime-display-entity message nil
1360                            `((entity-button . invisible)
1361                              (header . visible)
1362                              (major-mode . ,original-major-mode))
1363                            preview-buffer)
1364       (mime-view-define-keymap default-keymap-or-function)
1365       (set (make-local-variable 'line-move-ignore-invisible) t)
1366       (let ((point
1367              (next-single-property-change (point-min) 'mime-view-entity)))
1368         (if point
1369             (goto-char point)
1370           (goto-char (point-min))
1371           (search-forward "\n\n" nil t)))
1372       (run-hooks 'mime-view-mode-hook)
1373       (set-buffer-modified-p nil)
1374       (setq buffer-read-only t)
1375       preview-buffer)))
1376
1377 ;;;###autoload
1378 (defun mime-view-buffer (&optional raw-buffer preview-buffer mother
1379                                    default-keymap-or-function
1380                                    representation-type)
1381   "View RAW-BUFFER in MIME-View mode.
1382 Optional argument PREVIEW-BUFFER is either nil or a name of preview
1383 buffer.
1384 Optional argument DEFAULT-KEYMAP-OR-FUNCTION is nil, keymap or
1385 function.  If it is a keymap, keymap of MIME-View mode will be added
1386 to it.  If it is a function, it will be bound as default binding of
1387 keymap of MIME-View mode.
1388 Optional argument REPRESENTATION-TYPE is representation-type of
1389 message.  It must be nil, `binary' or `cooked'.  If it is nil,
1390 `cooked' is used as default."
1391   (interactive)
1392   (or raw-buffer
1393       (setq raw-buffer (current-buffer)))
1394   (or representation-type
1395       (setq representation-type
1396             (save-excursion
1397               (set-buffer raw-buffer)
1398               (cdr (or (assq major-mode mime-raw-representation-type-alist)
1399                        (assq t mime-raw-representation-type-alist))))))
1400   (if (eq representation-type 'binary)
1401       (setq representation-type 'buffer))
1402   (setq preview-buffer (mime-display-message
1403                         (mime-open-entity representation-type raw-buffer)
1404                         preview-buffer mother default-keymap-or-function))
1405   (or (get-buffer-window preview-buffer)
1406       (let ((r-win (get-buffer-window raw-buffer)))
1407         (if r-win
1408             (set-window-buffer r-win preview-buffer)
1409           (let ((m-win (and mother (get-buffer-window mother))))
1410             (if m-win
1411                 (set-window-buffer m-win preview-buffer)
1412               (switch-to-buffer preview-buffer)))))))
1413
1414 (defun mime-view-mode (&optional mother ctl encoding
1415                                  raw-buffer preview-buffer
1416                                  default-keymap-or-function)
1417   "Major mode for viewing MIME message.
1418
1419 Here is a list of the standard keys for mime-view-mode.
1420
1421 key             feature
1422 ---             -------
1423
1424 u               Move to upper content
1425 p or M-TAB      Move to previous content
1426 n or TAB        Move to next content
1427 SPC             Scroll up or move to next content
1428 M-SPC or DEL    Scroll down or move to previous content
1429 RET             Move to next line
1430 M-RET           Move to previous line
1431 v               Decode current content as `play mode'
1432 e               Decode current content as `extract mode'
1433 C-c C-p         Decode current content as `print mode'
1434 a               Followup to current content.
1435 q               Quit
1436 button-2        Move to point under the mouse cursor
1437                 and decode current content as `play mode'"
1438   (interactive)
1439   (unless mime-view-redisplay
1440     (save-excursion
1441       (if raw-buffer (set-buffer raw-buffer))
1442       (let ((type
1443              (cdr
1444               (or (assq major-mode mime-raw-representation-type-alist)
1445                   (assq t mime-raw-representation-type-alist)))))
1446         (if (eq type 'binary)
1447             (setq type 'buffer))
1448         (setq mime-message-structure (mime-open-entity type raw-buffer))
1449         (or (mime-entity-content-type mime-message-structure)
1450             (mime-entity-set-content-type-internal
1451              mime-message-structure ctl)))
1452       (or (mime-entity-encoding mime-message-structure)
1453           (mime-entity-set-encoding-internal mime-message-structure encoding))))
1454   (mime-display-message mime-message-structure preview-buffer
1455                         mother default-keymap-or-function))
1456
1457
1458 ;;; @@ playing
1459 ;;;
1460
1461 (autoload 'mime-preview-play-current-entity "mime-play"
1462   "Play current entity." t)
1463
1464 (defun mime-preview-extract-current-entity (&optional ignore-examples)
1465   "Extract current entity into file (maybe).
1466 It decodes current entity to call internal or external method as
1467 \"extract\" mode.  The method is selected from variable
1468 `mime-acting-condition'."
1469   (interactive "P")
1470   (mime-preview-play-current-entity ignore-examples "extract"))
1471
1472 (defun mime-preview-print-current-entity (&optional ignore-examples)
1473   "Print current entity (maybe).
1474 It decodes current entity to call internal or external method as
1475 \"print\" mode.  The method is selected from variable
1476 `mime-acting-condition'."
1477   (interactive "P")
1478   (mime-preview-play-current-entity ignore-examples "print"))
1479
1480
1481 ;;; @@ following
1482 ;;;
1483
1484 (defun mime-preview-follow-current-entity ()
1485   "Write follow message to current entity.
1486 It calls following-method selected from variable
1487 `mime-preview-following-method-alist'."
1488   (interactive)
1489   (let (entity position entity-node-id header-exists)
1490     (while (null (setq entity
1491                        (get-text-property (point) 'mime-view-entity)))
1492       (backward-char))
1493     (setq position (mime-preview-entity-boundary))
1494     (setq entity-node-id (mime-entity-node-id entity)
1495           header-exists
1496           ;; When on an invisible entity, there's no header.
1497           (or (mime-view-header-is-visible
1498                (get-text-property (car position) 'mime-view-situation))
1499               ;; We are on a rfc822 button.
1500               (and (eq 'message (mime-entity-media-type
1501                                  entity))
1502                    (eq 'rfc822 (mime-entity-media-subtype
1503                                 entity))
1504                    (get-text-property
1505                     (next-single-property-change
1506                      (car position) 'mime-button-callback
1507                      nil (point-max))
1508                     'mime-view-entity-header))))
1509     (let* ((mode (mime-preview-original-major-mode 'recursive))
1510            (new-name
1511             (format "%s-%s" (buffer-name) (reverse entity-node-id)))
1512            new-buf
1513            (the-buf (current-buffer))
1514            fields)
1515       (save-excursion
1516         (set-buffer (setq new-buf (get-buffer-create new-name)))
1517         (erase-buffer)
1518         (unless header-exists
1519           (insert ?\n))
1520         (insert (buffer-substring-no-properties (car position)
1521                                                 (cdr position) the-buf))
1522         (goto-char (point-min))
1523         (let ((current-entity
1524                (if (and (eq (mime-entity-media-type entity) 'message)
1525                         (eq (mime-entity-media-subtype entity) 'rfc822))
1526                    (car (mime-entity-children entity))
1527                  entity)))
1528           (while (and current-entity
1529                       (if (and (eq (mime-entity-media-type
1530                                     current-entity) 'message)
1531                                (eq (mime-entity-media-subtype
1532                                     current-entity) 'rfc822))
1533                           nil
1534                         (mime-insert-header current-entity fields)
1535                         t))
1536             (setq fields (std11-collect-field-names)
1537                   current-entity (mime-entity-parent current-entity))))
1538         (let ((rest mime-view-following-required-fields-list)
1539               field-name ret)
1540           (while rest
1541             (setq field-name (car rest))
1542             (or (std11-field-body field-name)
1543                 (progn
1544                   (save-excursion
1545                     (set-buffer the-buf)
1546                     (let ((entity (when mime-mother-buffer
1547                                     (set-buffer mime-mother-buffer)
1548                                     (get-text-property (point)
1549                                                        'mime-view-entity))))
1550                       (while (and entity
1551                                   (null (setq ret (mime-entity-fetch-field
1552                                                    entity field-name))))
1553                         (setq entity (mime-entity-parent entity)))))
1554                   (if ret
1555                       (insert (concat field-name ": " ret "\n")))))
1556             (setq rest (cdr rest))))
1557         (mime-decode-header-in-buffer))
1558       (let ((f (cdr (assq mode mime-preview-following-method-alist))))
1559         (if (functionp f)
1560             (funcall f new-buf)
1561           (message
1562            (format
1563             "Sorry, following method for %s is not implemented yet."
1564             mode)))))))
1565
1566
1567 ;;; @@ moving
1568 ;;;
1569
1570 (defun mime-preview-move-to-upper ()
1571   "Move to upper entity.
1572 If there is no upper entity, call function `mime-preview-quit'."
1573   (interactive)
1574   (let (cinfo)
1575     (while (null (setq cinfo
1576                        (get-text-property (point) 'mime-view-entity)))
1577       (backward-char))
1578     (let ((r (mime-entity-parent cinfo))
1579           point)
1580       (catch 'tag
1581         (while (setq point (previous-single-property-change
1582                             (point) 'mime-view-entity))
1583           (goto-char point)
1584           (when (eq r (get-text-property (point) 'mime-view-entity))
1585             (if (or (eq mime-preview-move-scroll t)
1586                     (and mime-preview-move-scroll
1587                          (>= point
1588                              (save-excursion
1589                                (move-to-window-line -1)
1590                                (forward-line (* -1 next-screen-context-lines))
1591                                (beginning-of-line)
1592                                (point)))))
1593                 (recenter next-screen-context-lines))
1594             (throw 'tag t)))
1595         (mime-preview-quit)))))
1596
1597 (defun mime-preview-move-to-previous ()
1598   "Move to previous entity.
1599 If there is no previous entity, it calls function registered in
1600 variable `mime-preview-over-to-previous-method-alist'."
1601   (interactive)
1602   (while (and (not (bobp))
1603               (null (get-text-property (point) 'mime-view-entity)))
1604     (backward-char))
1605   (let ((point (previous-single-property-change (point) 'mime-view-entity)))
1606     (if (and point
1607              (>= point (point-min)))
1608         (if (get-text-property (1- point) 'mime-view-entity)
1609             (progn (goto-char point)
1610                    (if
1611                     (or (eq mime-preview-move-scroll t)
1612                         (and mime-preview-move-scroll
1613                              (<= point
1614                                 (save-excursion
1615                                   (move-to-window-line 0)
1616                                   (forward-line next-screen-context-lines)
1617                                   (end-of-line)
1618                                   (point)))))
1619                         (recenter next-screen-context-lines)))
1620           (goto-char (1- point))
1621           (mime-preview-move-to-previous))
1622       (let ((f (assq (mime-preview-original-major-mode)
1623                      mime-preview-over-to-previous-method-alist)))
1624         (if f
1625             (funcall (cdr f)))))))
1626
1627 (defun mime-preview-move-to-next ()
1628   "Move to next entity.
1629 If there is no previous entity, it calls function registered in
1630 variable `mime-preview-over-to-next-method-alist'."
1631   (interactive)
1632   (while (and (not (eobp))
1633               (null (get-text-property (point) 'mime-view-entity)))
1634     (forward-char))
1635   (let ((point (next-single-property-change (point) 'mime-view-entity)))
1636     (if (and point
1637              (<= point (point-max)))
1638         (progn
1639           (goto-char point)
1640           (if (null (get-text-property point 'mime-view-entity))
1641               (mime-preview-move-to-next)
1642             (and
1643              (or (eq mime-preview-move-scroll t)
1644                  (and mime-preview-move-scroll
1645                       (>= point
1646                          (save-excursion
1647                            (move-to-window-line -1)
1648                            (forward-line
1649                             (* -1 next-screen-context-lines))
1650                            (beginning-of-line)
1651                            (point)))))
1652                  (recenter next-screen-context-lines))))
1653       (let ((f (assq (mime-preview-original-major-mode)
1654                      mime-preview-over-to-next-method-alist)))
1655         (if f
1656             (funcall (cdr f)))))))
1657
1658 (defun mime-preview-scroll-up-entity (&optional h)
1659   "Scroll up current entity.
1660 If reached to (point-max), it calls function registered in variable
1661 `mime-preview-over-to-next-method-alist'."
1662   (interactive)
1663   (if (eobp)
1664       (let ((f (assq (mime-preview-original-major-mode)
1665                      mime-preview-over-to-next-method-alist)))
1666         (if f
1667             (funcall (cdr f))))
1668     (let ((point
1669            (or (next-single-property-change (point) 'mime-view-entity)
1670                (point-max)))
1671           (bottom (window-end (selected-window))))
1672       (if (and (not h)
1673                (> bottom point)
1674                (not mime-preview-scroll-full-screen))
1675           (progn (goto-char point)
1676                  (recenter next-screen-context-lines))
1677         (condition-case nil
1678             (scroll-up h)
1679           (end-of-buffer
1680            (goto-char (point-max))))))))
1681
1682 (defun mime-preview-scroll-down-entity (&optional h)
1683   "Scroll down current entity.
1684 If reached to (point-min), it calls function registered in variable
1685 `mime-preview-over-to-previous-method-alist'."
1686   (interactive)
1687   (if (bobp)
1688       (let ((f (assq (mime-preview-original-major-mode)
1689                      mime-preview-over-to-previous-method-alist)))
1690         (if f
1691             (funcall (cdr f))))
1692     (let ((point
1693            (or (previous-single-property-change (point) 'mime-view-entity)
1694                (point-min)))
1695           (top (window-start (selected-window))))
1696       (if (and (not h)
1697                (< top point)
1698                (not mime-preview-scroll-full-screen))
1699           (progn (goto-char point)
1700                  (recenter (* -1 next-screen-context-lines)))
1701         (condition-case nil
1702             (scroll-down h)
1703           (beginning-of-buffer
1704            (goto-char (point-min))))))))
1705
1706 (defun mime-preview-next-line-entity (&optional lines)
1707   "Scroll up one line (or prefix LINES lines).
1708 If LINES is negative, scroll down LINES lines."
1709   (interactive "p")
1710   (mime-preview-scroll-up-entity (or lines 1)))
1711
1712 (defun mime-preview-previous-line-entity (&optional lines)
1713   "Scrroll down one line (or prefix LINES lines).
1714 If LINES is negative, scroll up LINES lines."
1715   (interactive "p")
1716   (mime-preview-scroll-down-entity (or lines 1)))
1717
1718 (defun mime-preview-entity-boundary (&optional point)
1719   (or point
1720       (setq point (point)))
1721   (and (eq point (point-max))
1722        (setq point (1- (point-max))))
1723   (let ((entity (get-text-property point 'mime-view-entity))
1724         (start (previous-single-property-change (1+ point) 'mime-view-entity
1725                                                 nil (point-min)))
1726         (end point)
1727         done)
1728     (while (and (mime-entity-children entity)
1729                 (not done))
1730       (if (mime-view-children-is-invisible
1731            (get-text-property point 'mime-view-situation))
1732           (setq done t)
1733         ;; If the part is shown, search the last part.
1734         (let ((child (car (last (mime-entity-children entity)))))
1735           (while (not (eq (get-text-property point 'mime-view-entity) child))
1736             (setq point (next-single-property-change point 'mime-view-entity)))
1737           (setq entity child))))
1738     (setq end (next-single-property-change point 'mime-view-entity
1739                                                  nil (point-max)))
1740     (cons start end)))
1741
1742 (defun mime-preview-toggle-header ()
1743   "Toggle display of entity header."
1744   (interactive)
1745   (let ((inhibit-read-only t)
1746         (mime-view-force-inline-types t)
1747         (position (mime-preview-entity-boundary))
1748         entity header-is-visible situation)
1749     (setq entity (get-text-property (car position) 'mime-view-entity)
1750           situation (get-text-property (car position) 'mime-view-situation))
1751     (setq header-is-visible (mime-view-header-is-visible situation))
1752     (save-excursion
1753       (delete-region (car position) (cdr position))
1754       (if header-is-visible
1755           (mime-display-entity entity
1756                                (put-alist '*entity-button 'visible
1757                                           (put-alist '*header 'invisible
1758                                                      situation)))
1759         (mime-display-entity entity 
1760                              (put-alist '*entity-button 'invisible
1761                                         (put-alist '*header 'visible
1762                                                    situation)))))))
1763
1764 (defun mime-preview-toggle-content ()
1765   "Toggle display of entity body."
1766   (interactive)
1767   (let ((inhibit-read-only t)
1768         (mime-view-force-inline-types t)
1769         (position (mime-preview-entity-boundary))
1770         entity situation)
1771     (setq entity (get-text-property (car position) 'mime-view-entity)
1772           situation (get-text-property (car position) 'mime-view-situation))
1773     (setq situation
1774           (if (mime-entity-children entity)
1775               ;; Entity body is always invisible for composite types.
1776               (if (mime-view-children-is-invisible situation)
1777                   (put-alist '*children 'visible situation)
1778                 (put-alist '*children 'invisible situation))
1779             (if (mime-view-body-is-visible situation)
1780                 (put-alist '*body 'invisible situation)
1781               (put-alist '*body 'visible situation))))
1782     (save-excursion
1783       (delete-region (car position) (cdr position))
1784       (mime-display-entity entity situation))))
1785
1786 ;;; @@ quitting
1787 ;;;
1788
1789 (defun mime-preview-quit ()
1790   "Quit from MIME-preview buffer.
1791 It calls function registered in variable
1792 `mime-preview-quitting-method-alist'."
1793   (interactive)
1794   (let ((r (assq (mime-preview-original-major-mode)
1795                  mime-preview-quitting-method-alist)))
1796     (if r
1797         (funcall (cdr r)))
1798     (kill-buffer (current-buffer))))
1799
1800 (defun mime-preview-kill-buffer ()
1801   (interactive)
1802   (kill-buffer (current-buffer)))
1803
1804
1805 ;;; @ end
1806 ;;;
1807
1808 (provide 'mime-view)
1809
1810 (run-hooks 'mime-view-load-hook)
1811
1812 ;;; mime-view.el ends here