(mm-text-html-renderer-alist): Add w3m-standalone.
[gnus] / lisp / mm-view.el
1 ;;; mm-view.el --- functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (eval-when-compile (require 'cl))
27 (require 'mail-parse)
28 (require 'mailcap)
29 (require 'mm-bodies)
30 (require 'mm-decode)
31
32 (eval-and-compile
33   (autoload 'gnus-article-prepare-display "gnus-art")
34   (autoload 'vcard-parse-string "vcard")
35   (autoload 'vcard-format-string "vcard")
36   (autoload 'fill-flowed "flow-fill")
37   (autoload 'html2text "html2text")
38   (unless (fboundp 'diff-mode)
39     (autoload 'diff-mode "diff-mode" "" t nil)))
40
41 (defvar mm-text-html-renderer-alist
42   '((w3  . mm-inline-text-html-render-with-w3)
43     (w3m . mm-inline-text-html-render-with-w3m)
44     (w3m-standalone mm-inline-render-with-stdin nil
45                     "w3m" "-dump" "-T" "text/html")
46     (links mm-inline-render-with-file
47            mm-links-remove-leading-blank
48            "links" "-dump" file)
49     (lynx  mm-inline-render-with-stdin nil
50            "lynx" "-dump" "-force_html" "-stdin")
51     (html2text  mm-inline-render-with-function html2text))
52   "The attributes of renderer types for text/html.")
53
54 (defvar mm-text-html-washer-alist
55   '((w3  . gnus-article-wash-html-with-w3)
56     (w3m . gnus-article-wash-html-with-w3m)
57     (w3m-standalone mm-inline-render-with-stdin nil
58                     "w3m" "-dump" "-T" "text/html")
59     (links mm-inline-wash-with-file
60            mm-links-remove-leading-blank
61            "links" "-dump" file)
62     (lynx  mm-inline-wash-with-stdin nil
63            "lynx" "-dump" "-force_html" "-stdin")
64     (html2text  html2text))
65   "The attributes of washer types for text/html.")
66
67 ;;; Internal variables.
68
69 ;;;
70 ;;; Functions for displaying various formats inline
71 ;;;
72
73 (defun mm-inline-image-emacs (handle)
74   (let ((b (point-marker))
75         buffer-read-only)
76     (insert "\n")
77     (put-image (mm-get-image handle) b)
78     (mm-handle-set-undisplayer
79      handle
80      `(lambda () (remove-images ,b (1+ ,b))))))
81
82 (defun mm-inline-image-xemacs (handle)
83   (insert "\n")
84   (forward-char -1)
85   (let ((b (point))
86         (annot (make-annotation (mm-get-image handle) nil 'text))
87         buffer-read-only)
88     (mm-handle-set-undisplayer
89      handle
90      `(lambda ()
91         (let (buffer-read-only)
92           (delete-annotation ,annot)
93           (delete-region ,(set-marker (make-marker) b)
94                          ,(set-marker (make-marker) (point))))))
95     (set-extent-property annot 'mm t)
96     (set-extent-property annot 'duplicable t)))
97
98 (eval-and-compile
99   (if (featurep 'xemacs)
100       (defalias 'mm-inline-image 'mm-inline-image-xemacs)
101     (defalias 'mm-inline-image 'mm-inline-image-emacs)))
102
103 (defvar mm-w3-setup nil)
104 (defun mm-setup-w3 ()
105   (unless mm-w3-setup
106     (require 'w3)
107     (w3-do-setup)
108     (require 'url)
109     (require 'w3-vars)
110     (require 'url-vars)
111     (setq mm-w3-setup t)))
112
113 (defun mm-inline-text-html-render-with-w3 (handle)
114   (mm-setup-w3)
115   (let ((text (mm-get-part handle))
116         (b (point))
117         (url-standalone-mode t)
118         (url-gateway-unplugged t)
119         (w3-honor-stylesheets nil)
120         (url-current-object
121          (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
122         (width (window-width))
123         (charset (mail-content-type-get
124                   (mm-handle-type handle) 'charset)))
125     (save-excursion
126       (insert text)
127       (save-restriction
128         (narrow-to-region b (point))
129         (goto-char (point-min))
130         (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
131                      (re-search-forward
132                       w3-meta-content-type-charset-regexp nil t))
133                 (and (boundp 'w3-meta-charset-content-type-regexp)
134                      (re-search-forward
135                       w3-meta-charset-content-type-regexp nil t)))
136             (setq charset
137                   (or (let ((bsubstr (buffer-substring-no-properties
138                                       (match-beginning 2)
139                                       (match-end 2))))
140                         (if (fboundp 'w3-coding-system-for-mime-charset)
141                             (w3-coding-system-for-mime-charset bsubstr)
142                           (mm-charset-to-coding-system bsubstr)))
143                       charset)))
144         (delete-region (point-min) (point-max))
145         (insert (mm-decode-string text charset))
146         (save-window-excursion
147           (save-restriction
148             (let ((w3-strict-width width)
149                   ;; Don't let w3 set the global version of
150                   ;; this variable.
151                   (fill-column fill-column))
152               (if (or debug-on-error debug-on-quit)
153                   (w3-region (point-min) (point-max))
154                 (condition-case ()
155                     (w3-region (point-min) (point-max))
156                   (error
157                    (delete-region (point-min) (point-max))
158                    (let ((b (point))
159                          (charset (mail-content-type-get
160                                    (mm-handle-type handle) 'charset)))
161                      (if (or (eq charset 'gnus-decoded)
162                              (eq mail-parse-charset 'gnus-decoded))
163                        (save-restriction
164                          (narrow-to-region (point) (point))
165                          (mm-insert-part handle)
166                          (goto-char (point-max)))
167                        (insert (mm-decode-string (mm-get-part handle)
168                                                  charset))))
169                    (message
170                     "Error while rendering html; showing as text/plain")))))))
171         (mm-handle-set-undisplayer
172          handle
173          `(lambda ()
174             (let (buffer-read-only)
175               (if (functionp 'remove-specifier)
176                   (mapcar (lambda (prop)
177                             (remove-specifier
178                              (face-property 'default prop)
179                              (current-buffer)))
180                           '(background background-pixmap foreground)))
181               (delete-region ,(point-min-marker)
182                              ,(point-max-marker)))))))))
183
184 (defvar mm-w3m-mode-map nil
185   "Local keymap for inlined text/html part rendered by emacs-w3m.  It will
186 be different from `w3m-mode-map' to use in the article buffer.")
187
188 (defvar mm-w3m-mode-command-alist
189   '((backward-char)
190     (describe-mode)
191     (forward-char)
192     (goto-line)
193     (next-line)
194     (previous-line)
195     (w3m-antenna)
196     (w3m-antenna-add-current-url)
197     (w3m-bookmark-add-current-url)
198     (w3m-bookmark-add-this-url)
199     (w3m-bookmark-view)
200     (w3m-close-window)
201     (w3m-copy-buffer)
202     (w3m-delete-buffer)
203     (w3m-dtree)
204     (w3m-edit-current-url)
205     (w3m-edit-this-url)
206     (w3m-gohome)
207     (w3m-goto-url)
208     (w3m-goto-url-new-session)
209     (w3m-history)
210     (w3m-history-restore-position)
211     (w3m-history-store-position)
212     (w3m-namazu)
213     (w3m-next-buffer)
214     (w3m-previous-buffer)
215     (w3m-quit)
216     (w3m-redisplay-with-charset)
217     (w3m-reload-this-page)
218     (w3m-scroll-down-or-previous-url)
219     (w3m-scroll-up-or-next-url)
220     (w3m-search)
221     (w3m-select-buffer)
222     (w3m-switch-buffer)
223     (w3m-view-header)
224     (w3m-view-parent-page)
225     (w3m-view-previous-page)
226     (w3m-view-source)
227     (w3m-weather))
228   "Alist of commands to use for emacs-w3m in the article buffer.  Each
229 element looks like (FROM-COMMAND . TO-COMMAND); FROM-COMMAND should be
230 registered in `w3m-mode-map' which will be substituted by TO-COMMAND
231 in `mm-w3m-mode-map'.  If TO-COMMAND is nil, an article command key
232 will not be substituted.")
233
234 (defvar mm-w3m-mode-dont-bind-keys (list [up] [right] [left] [down])
235   "List of keys which should not be bound for the emacs-w3m commands.")
236
237 (defvar mm-w3m-mode-ignored-keys (list [down-mouse-2])
238   "List of keys which should ignore.")
239
240 (defvar mm-w3m-setup nil
241   "Whether gnus-article-mode has been setup to use emacs-w3m.")
242
243 (defun mm-setup-w3m ()
244   "Setup gnus-article-mode to use emacs-w3m."
245   (unless mm-w3m-setup
246     (require 'w3m)
247     (unless mm-w3m-mode-map
248       (setq mm-w3m-mode-map (copy-keymap w3m-mode-map))
249       (dolist (def mm-w3m-mode-command-alist)
250         (condition-case nil
251             (substitute-key-definition (car def) (cdr def) mm-w3m-mode-map)
252           (error)))
253       (dolist (key mm-w3m-mode-dont-bind-keys)
254         (condition-case nil
255             (define-key mm-w3m-mode-map key nil)
256           (error)))
257       (dolist (key mm-w3m-mode-ignored-keys)
258         (condition-case nil
259             (define-key mm-w3m-mode-map key 'ignore)
260           (error))))
261     (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
262       (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
263             w3m-cid-retrieve-function-alist))
264     (setq mm-w3m-setup t)))
265
266 (defun mm-w3m-cid-retrieve (url &rest args)
267   "Insert a content pointed by URL if it has the cid: scheme."
268   (when (string-match "\\`cid:" url)
269     (setq url (concat "<" (substring url (match-end 0)) ">"))
270     (catch 'found-handle
271       (dolist (handle (with-current-buffer w3m-current-buffer
272                         gnus-article-mime-handles))
273         (when (and (listp handle)
274                    (equal url (mm-handle-id handle)))
275           (mm-insert-part handle)
276           (throw 'found-handle (mm-handle-media-type handle)))))))
277
278 (defun mm-inline-text-html-render-with-w3m (handle)
279   "Render a text/html part using emacs-w3m."
280   (mm-setup-w3m)
281   (let ((text (mm-get-part handle))
282         (b (point))
283         (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
284     (save-excursion
285       (insert text)
286       (save-restriction
287         (narrow-to-region b (point))
288         (goto-char (point-min))
289         (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
290           (setq charset (or (w3m-charset-to-coding-system (match-string 2))
291                             charset)))
292         (when charset
293           (delete-region (point-min) (point-max))
294           (insert (mm-decode-string text charset)))
295         (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
296               (w3m-display-inline-images mm-inline-text-html-with-images)
297               w3m-force-redisplay)
298           (w3m-region (point-min) (point-max)))
299         (when mm-inline-text-html-with-w3m-keymap
300           (add-text-properties
301            (point-min) (point-max)
302            (append '(mm-inline-text-html-with-w3m t)
303                    (gnus-local-map-property mm-w3m-mode-map)))))
304       (mm-handle-set-undisplayer
305        handle
306        `(lambda ()
307           (let (buffer-read-only)
308             (if (functionp 'remove-specifier)
309                 (mapcar (lambda (prop)
310                           (remove-specifier
311                            (face-property 'default prop)
312                            (current-buffer)))
313                         '(background background-pixmap foreground)))
314             (delete-region ,(point-min-marker)
315                            ,(point-max-marker))))))))
316
317 (defun mm-links-remove-leading-blank ()
318   ;; Delete the annoying three spaces preceding each line of links
319   ;; output.
320   (goto-char (point-min))
321   (while (re-search-forward "^   " nil t)
322     (delete-region (match-beginning 0) (match-end 0))))
323
324 (defun mm-inline-wash-with-file (post-func cmd &rest args)
325   (let ((file (mm-make-temp-file
326                (expand-file-name "mm" mm-tmp-directory))))
327     (let ((coding-system-for-write 'binary))
328       (write-region (point-min) (point-max) file nil 'silent))
329     (delete-region (point-min) (point-max))
330     (unwind-protect
331         (apply 'call-process cmd nil t nil (mapcar 'eval args))
332       (delete-file file))
333     (and post-func (funcall post-func))))
334
335 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
336   (let ((coding-system-for-write 'binary))
337     (apply 'call-process-region (point-min) (point-max)
338            cmd t t nil args))
339   (and post-func (funcall post-func)))
340
341 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
342   (let ((source (mm-get-part handle)))
343     (mm-insert-inline
344      handle
345      (mm-with-unibyte-buffer
346        (insert source)
347        (apply 'mm-inline-wash-with-file post-func cmd args)
348        (buffer-string)))))
349
350 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
351   (let ((source (mm-get-part handle)))
352     (mm-insert-inline
353      handle
354      (mm-with-unibyte-buffer
355        (insert source)
356        (apply 'mm-inline-wash-with-stdin post-func cmd args)
357        (buffer-string)))))
358
359 (defun mm-inline-render-with-function (handle func &rest args)
360   (let ((source (mm-get-part handle)))
361     (mm-insert-inline
362      handle
363      (mm-with-unibyte-buffer
364        (insert source)
365        (apply func args)
366        (buffer-string)))))
367
368 (defun mm-inline-text-html (handle)
369   (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
370          (entry (assq func mm-text-html-renderer-alist))
371          buffer-read-only)
372     (if entry
373         (setq func (cdr entry)))
374     (cond
375      ((gnus-functionp func)
376       (funcall func handle))
377      (t
378       (apply (car func) handle (cdr func))))))
379
380 (defun mm-inline-text-vcard (handle)
381   (let (buffer-read-only)
382     (mm-insert-inline
383      handle
384      (concat "\n-- \n"
385              (ignore-errors
386                (if (fboundp 'vcard-pretty-print)
387                    (vcard-pretty-print (mm-get-part handle))
388                  (vcard-format-string
389                   (vcard-parse-string (mm-get-part handle)
390                                       'vcard-standard-filter))))))))
391
392 (defun mm-inline-text (handle)
393   (let ((b (point))
394         (type (mm-handle-media-subtype handle))
395         (charset (mail-content-type-get
396                   (mm-handle-type handle) 'charset))
397         buffer-read-only)
398     (if (or (eq charset 'gnus-decoded)
399             ;; This is probably not entirely correct, but
400             ;; makes rfc822 parts with embedded multiparts work.
401             (eq mail-parse-charset 'gnus-decoded))
402         (save-restriction
403           (narrow-to-region (point) (point))
404           (mm-insert-part handle)
405           (goto-char (point-max)))
406       (insert (mm-decode-string (mm-get-part handle) charset)))
407     (when (and (equal type "plain")
408                (equal (cdr (assoc 'format (mm-handle-type handle)))
409                       "flowed"))
410       (save-restriction
411         (narrow-to-region b (point))
412         (goto-char b)
413         (fill-flowed)
414         (goto-char (point-max))))
415     (save-restriction
416       (narrow-to-region b (point))
417       (set-text-properties (point-min) (point-max) nil)
418       (when (or (equal type "enriched")
419                 (equal type "richtext"))
420         (enriched-decode (point-min) (point-max)))
421       (mm-handle-set-undisplayer
422        handle
423        `(lambda ()
424           (let (buffer-read-only)
425             (delete-region ,(point-min-marker)
426                            ,(point-max-marker))))))))
427
428 (defun mm-insert-inline (handle text)
429   "Insert TEXT inline from HANDLE."
430   (let ((b (point)))
431     (insert text)
432     (mm-handle-set-undisplayer
433      handle
434      `(lambda ()
435         (let (buffer-read-only)
436           (delete-region ,(set-marker (make-marker) b)
437                          ,(set-marker (make-marker) (point))))))))
438
439 (defun mm-inline-audio (handle)
440   (message "Not implemented"))
441
442 (defun mm-view-sound-file ()
443   (message "Not implemented"))
444
445 (defun mm-w3-prepare-buffer ()
446   (require 'w3)
447   (let ((url-standalone-mode t)
448         (url-gateway-unplugged t)
449         (w3-honor-stylesheets nil))
450     (w3-prepare-buffer)))
451
452 (defun mm-view-message ()
453   (mm-enable-multibyte)
454   (let (handles)
455     (let (gnus-article-mime-handles)
456       ;; Double decode problem may happen.  See mm-inline-message.
457       (run-hooks 'gnus-article-decode-hook)
458       (gnus-article-prepare-display)
459       (setq handles gnus-article-mime-handles))
460     (when handles
461       (setq gnus-article-mime-handles
462             (mm-merge-handles gnus-article-mime-handles handles))))
463   (fundamental-mode)
464   (goto-char (point-min)))
465
466 (defun mm-inline-message (handle)
467   (let ((b (point))
468         (bolp (bolp))
469         (charset (mail-content-type-get
470                   (mm-handle-type handle) 'charset))
471         gnus-displaying-mime handles)
472     (when (and charset
473                (stringp charset))
474       (setq charset (intern (downcase charset)))
475       (when (eq charset 'us-ascii)
476         (setq charset nil)))
477     (save-excursion
478       (save-restriction
479         (narrow-to-region b b)
480         (mm-insert-part handle)
481         (let (gnus-article-mime-handles
482               ;; disable prepare hook
483               gnus-article-prepare-hook
484               (gnus-newsgroup-charset
485                (or charset gnus-newsgroup-charset)))
486           (run-hooks 'gnus-article-decode-hook)
487           (gnus-article-prepare-display)
488           (setq handles gnus-article-mime-handles))
489         (goto-char (point-min))
490         (unless bolp
491           (insert "\n"))
492         (goto-char (point-max))
493         (unless (bolp)
494           (insert "\n"))
495         (insert "----------\n\n")
496         (when handles
497           (setq gnus-article-mime-handles
498                 (mm-merge-handles gnus-article-mime-handles handles)))
499         (mm-handle-set-undisplayer
500          handle
501          `(lambda ()
502             (let (buffer-read-only)
503               (if (fboundp 'remove-specifier)
504                   ;; This is only valid on XEmacs.
505                   (mapcar (lambda (prop)
506                             (remove-specifier
507                              (face-property 'default prop) (current-buffer)))
508                           '(background background-pixmap foreground)))
509               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
510
511 (defun mm-display-inline-fontify (handle mode)
512   (let (text)
513     ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
514     ;; on for buffers whose name begins with " ".  That's why we use
515     ;; save-current-buffer/get-buffer-create rather than
516     ;; with-temp-buffer.
517     (save-current-buffer
518       (set-buffer (generate-new-buffer "*fontification*"))
519       (unwind-protect
520           (progn
521             (buffer-disable-undo)
522             (mm-insert-part handle)
523             (funcall mode)
524             (require 'font-lock)
525             (let ((font-lock-verbose nil))
526               ;; I find font-lock a bit too verbose.
527               (font-lock-fontify-buffer))
528             ;; By default, XEmacs font-lock uses non-duplicable text
529             ;; properties.  This code forces all the text properties
530             ;; to be copied along with the text.
531             (when (fboundp 'extent-list)
532               (map-extents (lambda (ext ignored)
533                              (set-extent-property ext 'duplicable t)
534                              nil)
535                            nil nil nil nil nil 'text-prop))
536             (setq text (buffer-string)))
537         (kill-buffer (current-buffer))))
538     (mm-insert-inline handle text)))
539
540 ;; Shouldn't these functions check whether the user even wants to use
541 ;; font-lock?  At least under XEmacs, this fontification is pretty
542 ;; much unconditional.  Also, it would be nice to change for the size
543 ;; of the fontified region.
544
545 (defun mm-display-patch-inline (handle)
546   (mm-display-inline-fontify handle 'diff-mode))
547
548 (defun mm-display-elisp-inline (handle)
549   (mm-display-inline-fontify handle 'emacs-lisp-mode))
550
551 ;;      id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
552 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
553 (defvar mm-pkcs7-signed-magic
554   (mm-string-as-unibyte
555    (apply 'concat
556           (mapcar 'char-to-string
557                   (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
558                         ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
559                         ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
560                         ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
561
562 ;;      id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
563 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
564 (defvar mm-pkcs7-enveloped-magic
565   (mm-string-as-unibyte
566    (apply 'concat
567           (mapcar 'char-to-string
568                   (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
569                         ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
570                         ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
571                         ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
572
573 (defun mm-view-pkcs7-get-type (handle)
574   (mm-with-unibyte-buffer
575     (mm-insert-part handle)
576     (cond ((looking-at mm-pkcs7-enveloped-magic)
577            'enveloped)
578           ((looking-at mm-pkcs7-signed-magic)
579            'signed)
580           (t
581            (error "Could not identify PKCS#7 type")))))
582
583 (defun mm-view-pkcs7 (handle)
584   (case (mm-view-pkcs7-get-type handle)
585     (enveloped (mm-view-pkcs7-decrypt handle))
586     (signed (mm-view-pkcs7-verify handle))
587     (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
588
589 (defun mm-view-pkcs7-verify (handle)
590   ;; A bogus implementation of PKCS#7. FIXME::
591   (mm-insert-part handle)
592   (goto-char (point-min))
593   (if (search-forward "Content-Type: " nil t)
594       (delete-region (point-min) (match-beginning 0)))
595   (goto-char (point-max))
596   (if (re-search-backward "--\r?\n?" nil t)
597       (delete-region (match-end 0) (point-max)))
598   (goto-char (point-min))
599   (while (search-forward "\r\n" nil t)
600     (replace-match "\n"))
601   (message "Verify signed PKCS#7 message is unimplemented.")
602   (sit-for 1)
603   t)
604
605 (defun mm-view-pkcs7-decrypt (handle)
606   (insert-buffer (mm-handle-buffer handle))
607   (goto-char (point-min))
608   (insert "MIME-Version: 1.0\n")
609   (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
610   (smime-decrypt-region
611    (point-min) (point-max)
612    (if (= (length smime-keys) 1)
613        (cadar smime-keys)
614      (smime-get-key-by-email
615       (completing-read
616        (concat "Decipher using which key? "
617                (if smime-keys (concat "(default " (caar smime-keys) ") ")
618                  ""))
619        smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
620   (goto-char (point-min))
621   (while (search-forward "\r\n" nil t)
622     (replace-match "\n"))
623   (goto-char (point-min)))
624
625 (provide 'mm-view)
626
627 ;;; mm-view.el ends here