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