1 ;;; mm-view.el --- functions for viewing MIME objects
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
27 (eval-when-compile (require 'cl))
34 (autoload 'gnus-completing-read "gnus-util")
35 (autoload 'gnus-window-inside-pixel-edges "gnus-ems")
36 (autoload 'gnus-article-prepare-display "gnus-art")
37 (autoload 'vcard-parse-string "vcard")
38 (autoload 'vcard-format-string "vcard")
39 (autoload 'fill-flowed "flow-fill")
40 (autoload 'html2text "html2text" nil t)
42 (defvar gnus-article-mime-handles)
43 (defvar gnus-newsgroup-charset)
45 (defvar w3m-cid-retrieve-function-alist)
46 (defvar w3m-current-buffer)
47 (defvar w3m-display-inline-images)
48 (defvar w3m-minor-mode-map)
50 (defvar mm-text-html-renderer-alist
51 '((w3 . mm-inline-text-html-render-with-w3)
52 (w3m . mm-inline-text-html-render-with-w3m)
53 (w3m-standalone . mm-inline-text-html-render-with-w3m-standalone)
54 (links mm-inline-render-with-file
55 mm-links-remove-leading-blank
57 (lynx mm-inline-render-with-stdin nil
58 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
59 (html2text mm-inline-render-with-function html2text))
60 "The attributes of renderer types for text/html.")
62 (defvar mm-text-html-washer-alist
63 '((w3 . gnus-article-wash-html-with-w3)
64 (w3m . gnus-article-wash-html-with-w3m)
65 (w3m-standalone . gnus-article-wash-html-with-w3m-standalone)
66 (links mm-inline-wash-with-file
67 mm-links-remove-leading-blank
69 (lynx mm-inline-wash-with-stdin nil
70 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
71 (html2text html2text))
72 "The attributes of washer types for text/html.")
74 (defcustom mm-fill-flowed t
75 "If non-nil a format=flowed article will be displayed flowed."
80 (defcustom mm-inline-large-images-proportion 0.9
81 "Maximum proportion of large image resized when
82 `mm-inline-large-images' is set to resize."
87 ;;; Internal variables.
90 ;;; Functions for displaying various formats inline
93 (defun mm-inline-image-emacs (handle)
94 (let ((b (point-marker))
95 (inhibit-read-only t))
97 (let ((image (mm-get-image handle)))
98 (if (eq mm-inline-large-images 'resize)
99 (gnus-rescale-image image
100 (let ((edges (gnus-window-inside-pixel-edges
101 (get-buffer-window (current-buffer)))))
102 (cons (truncate (* mm-inline-large-images-proportion
103 (- (nth 2 edges) (nth 0 edges))))
104 (truncate (* mm-inline-large-images-proportion
105 (- (nth 3 edges) (nth 1 edges)))))))
109 (mm-handle-set-undisplayer
113 (inhibit-read-only t))
115 (delete-region b (+ b 2)))))))
117 (defun mm-inline-image-xemacs (handle)
118 (when (featurep 'xemacs)
121 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
122 (inhibit-read-only t))
123 (mm-handle-set-undisplayer
126 (let ((b ,(point-marker))
127 (inhibit-read-only t))
128 (delete-annotation ,annot)
129 (delete-region (- b 2) b))))
130 (set-extent-property annot 'mm t)
131 (set-extent-property annot 'duplicable t))))
134 (if (featurep 'xemacs)
135 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
136 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
139 (declare-function w3-do-setup "ext:w3" ())
140 (declare-function w3-region "ext:w3-display" (st nd))
141 (declare-function w3-prepare-buffer "ext:w3-display" (&rest args))
143 (defvar mm-w3-setup nil)
144 (defun mm-setup-w3 ()
151 (setq mm-w3-setup t)))
153 (defun mm-inline-text-html-render-with-w3 (handle)
155 (let ((text (mm-get-part handle))
157 (url-standalone-mode t)
158 (url-gateway-unplugged t)
159 (w3-honor-stylesheets nil)
161 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
162 (width (window-width))
163 (charset (mail-content-type-get
164 (mm-handle-type handle) 'charset)))
166 (insert (if charset (mm-decode-string text charset) text))
168 (narrow-to-region b (point))
170 (goto-char (point-min))
171 (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
173 w3-meta-content-type-charset-regexp nil t))
174 (and (boundp 'w3-meta-charset-content-type-regexp)
176 w3-meta-charset-content-type-regexp nil t)))
178 (let ((bsubstr (buffer-substring-no-properties
181 (if (fboundp 'w3-coding-system-for-mime-charset)
182 (w3-coding-system-for-mime-charset bsubstr)
183 (mm-charset-to-coding-system bsubstr))))
184 (delete-region (point-min) (point-max))
185 (insert (mm-decode-string text charset))))
186 (save-window-excursion
188 (let ((w3-strict-width width)
189 ;; Don't let w3 set the global version of
191 (fill-column fill-column))
192 (if (or debug-on-error debug-on-quit)
193 (w3-region (point-min) (point-max))
195 (w3-region (point-min) (point-max))
197 (delete-region (point-min) (point-max))
199 (charset (mail-content-type-get
200 (mm-handle-type handle) 'charset)))
201 (if (or (eq charset 'gnus-decoded)
202 (eq mail-parse-charset 'gnus-decoded))
204 (narrow-to-region (point) (point))
205 (mm-insert-part handle)
206 (goto-char (point-max)))
207 (insert (mm-decode-string (mm-get-part handle)
210 "Error while rendering html; showing as text/plain")))))))
211 (mm-handle-set-undisplayer
214 (let ((inhibit-read-only t))
215 ,@(if (functionp 'remove-specifier)
216 '((dolist (prop '(background background-pixmap foreground))
218 (face-property 'default prop)
220 (delete-region ,(point-min-marker)
221 ,(point-max-marker)))))))))
223 (defvar mm-w3m-setup nil
224 "Whether gnus-article-mode has been setup to use emacs-w3m.")
227 (declare-function w3m-detect-meta-charset "ext:w3m" ())
228 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
230 (defun mm-setup-w3m ()
231 "Setup gnus-article-mode to use emacs-w3m."
234 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
235 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
236 w3m-cid-retrieve-function-alist))
237 (setq mm-w3m-setup t))
238 (setq w3m-display-inline-images mm-inline-text-html-with-images))
240 (defun mm-w3m-cid-retrieve-1 (url handle)
241 (dolist (elem handle)
243 (when (equal url (mm-handle-id elem))
244 (mm-insert-part elem)
245 (throw 'found-handle (mm-handle-media-type elem)))
246 (when (and (stringp (car elem))
247 (equal "multipart" (mm-handle-media-supertype elem)))
248 (mm-w3m-cid-retrieve-1 url elem)))))
250 (defun mm-w3m-cid-retrieve (url &rest args)
251 "Insert a content pointed by URL if it has the cid: scheme."
252 (when (string-match "\\`cid:" url)
253 (or (catch 'found-handle
254 (mm-w3m-cid-retrieve-1
255 (setq url (concat "<" (substring url (match-end 0)) ">"))
256 (with-current-buffer w3m-current-buffer
257 gnus-article-mime-handles)))
260 (message "Failed to find \"Content-ID: %s\"" url)))))
262 (defun mm-inline-text-html-render-with-w3m (handle)
263 "Render a text/html part using emacs-w3m."
265 (let ((text (mm-get-part handle))
267 (charset (or (mail-content-type-get (mm-handle-type handle) 'charset)
268 mail-parse-charset)))
270 (insert (if charset (mm-decode-string text charset) text))
272 (narrow-to-region b (point))
274 (goto-char (point-min))
275 (when (setq charset (w3m-detect-meta-charset))
276 (delete-region (point-min) (point-max))
277 (insert (mm-decode-string text charset))))
278 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
280 (w3m-region (point-min) (point-max) nil charset))
281 ;; Put the mark meaning this part was rendered by emacs-w3m.
282 (put-text-property (point-min) (point-max)
283 'mm-inline-text-html-with-w3m t)
284 (when (and mm-inline-text-html-with-w3m-keymap
285 (boundp 'w3m-minor-mode-map)
287 (if (and (boundp 'w3m-link-map)
289 (let* ((start (point-min))
291 (on (get-text-property start 'w3m-href-anchor))
292 (map (copy-keymap w3m-link-map))
294 (set-keymap-parent map w3m-minor-mode-map)
298 (setq next (or (text-property-any start end
299 'w3m-href-anchor nil)
301 (put-text-property start next 'keymap map))
302 (setq next (or (text-property-not-all start end
303 'w3m-href-anchor nil)
305 (put-text-property start next 'keymap w3m-minor-mode-map))
308 (put-text-property (point-min) (point-max)
309 'keymap w3m-minor-mode-map)))
310 (mm-handle-set-undisplayer
313 (let ((inhibit-read-only t))
314 (delete-region ,(point-min-marker)
315 ,(point-max-marker)))))))))
317 (defvar mm-w3m-standalone-supports-m17n-p (if (featurep 'mule) 'undecided)
318 "*T means the w3m command supports the m17n feature.")
320 (defun mm-w3m-standalone-supports-m17n-p ()
321 "Say whether the w3m command supports the m17n feature."
322 (cond ((eq mm-w3m-standalone-supports-m17n-p t) t)
323 ((eq mm-w3m-standalone-supports-m17n-p nil) nil)
324 ((not (featurep 'mule)) (setq mm-w3m-standalone-supports-m17n-p nil))
326 (let ((coding-system-for-write 'iso-2022-jp)
327 (coding-system-for-read 'iso-2022-jp)
328 (str (mm-decode-coding-string "\
329 \e$B#D#o#e#s!!#w#3#m!!#s#u#p#p#o#r#t!!#m#1#7#n!)\e(B" 'iso-2022-jp)))
330 (mm-with-multibyte-buffer
333 (point-min) (point-max) "w3m" t t nil "-dump"
334 "-T" "text/html" "-I" "iso-2022-jp" "-O" "iso-2022-jp")
335 (goto-char (point-min))
336 (search-forward str nil t)))
338 (setq mm-w3m-standalone-supports-m17n-p t))
340 ;;(message "You had better upgrade your w3m command")
341 (setq mm-w3m-standalone-supports-m17n-p nil))))
343 (defun mm-inline-text-html-render-with-w3m-standalone (handle)
344 "Render a text/html part using w3m."
345 (if (mm-w3m-standalone-supports-m17n-p)
346 (let ((source (mm-get-part handle))
347 (charset (or (mail-content-type-get (mm-handle-type handle)
349 (symbol-name mail-parse-charset)))
352 (setq cs (mm-charset-to-coding-system charset))
353 (not (eq cs 'ascii)))
355 (setq charset "iso-8859-1"
359 (mm-with-unibyte-buffer
361 (mm-enable-multibyte)
362 (let ((coding-system-for-write 'binary)
363 (coding-system-for-read cs))
365 (point-min) (point-max)
366 "w3m" t t nil "-dump" "-T" "text/html"
367 "-I" charset "-O" charset))
369 (mm-inline-render-with-stdin handle nil "w3m" "-dump" "-T" "text/html")))
371 (defun mm-links-remove-leading-blank ()
372 ;; Delete the annoying three spaces preceding each line of links
374 (goto-char (point-min))
375 (while (re-search-forward "^ " nil t)
376 (delete-region (match-beginning 0) (match-end 0))))
378 (defun mm-inline-wash-with-file (post-func cmd &rest args)
379 (let ((file (mm-make-temp-file
380 (expand-file-name "mm" mm-tmp-directory))))
381 (let ((coding-system-for-write 'binary))
382 (write-region (point-min) (point-max) file nil 'silent))
383 (delete-region (point-min) (point-max))
385 (apply 'call-process cmd nil t nil (mapcar 'eval args))
387 (and post-func (funcall post-func))))
389 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
390 (let ((coding-system-for-write 'binary))
391 (apply 'call-process-region (point-min) (point-max)
393 (and post-func (funcall post-func)))
395 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
396 (let ((source (mm-get-part handle)))
399 (mm-with-unibyte-buffer
401 (apply 'mm-inline-wash-with-file post-func cmd args)
404 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
405 (let ((source (mm-get-part handle)))
408 (mm-with-unibyte-buffer
410 (apply 'mm-inline-wash-with-stdin post-func cmd args)
413 (defun mm-inline-render-with-function (handle func &rest args)
414 (let ((source (mm-get-part handle))
415 (charset (or (mail-content-type-get (mm-handle-type handle) 'charset)
416 mail-parse-charset)))
419 (mm-with-multibyte-buffer
421 (mm-decode-string source charset)
426 (defun mm-inline-text-html (handle)
427 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
428 (entry (assq func mm-text-html-renderer-alist))
429 (inhibit-read-only t))
431 (setq func (cdr entry)))
434 (funcall func handle))
436 (apply (car func) handle (cdr func))))))
438 (defun mm-inline-text-vcard (handle)
439 (let ((inhibit-read-only t))
444 (if (fboundp 'vcard-pretty-print)
445 (vcard-pretty-print (mm-get-part handle))
447 (vcard-parse-string (mm-get-part handle)
448 'vcard-standard-filter))))))))
450 (defun mm-inline-text (handle)
452 (type (mm-handle-media-subtype handle))
453 (charset (mail-content-type-get
454 (mm-handle-type handle) 'charset))
455 (inhibit-read-only t))
456 (if (or (eq charset 'gnus-decoded)
457 ;; This is probably not entirely correct, but
458 ;; makes rfc822 parts with embedded multiparts work.
459 (eq mail-parse-charset 'gnus-decoded))
461 (narrow-to-region (point) (point))
462 (mm-insert-part handle)
463 (goto-char (point-max)))
464 (insert (mm-decode-string (mm-get-part handle) charset)))
465 (when (and mm-fill-flowed
467 (equal (cdr (assoc 'format (mm-handle-type handle)))
470 (narrow-to-region b (point))
472 (fill-flowed nil (equal (cdr (assoc 'delsp (mm-handle-type handle)))
474 (goto-char (point-max))))
476 (narrow-to-region b (point))
477 (when (member type '("enriched" "richtext"))
478 (set-text-properties (point-min) (point-max) nil)
480 (enriched-decode (point-min) (point-max))))
481 (mm-handle-set-undisplayer
484 (let ((inhibit-read-only t))
485 (delete-region ,(point-min-marker)
486 ,(point-max-marker))))))))
488 (defun mm-insert-inline (handle text)
489 "Insert TEXT inline from HANDLE."
494 (mm-handle-set-undisplayer
497 (let ((inhibit-read-only t))
498 (delete-region ,(copy-marker b)
499 ,(copy-marker (point))))))))
501 (defun mm-inline-audio (handle)
502 (message "Not implemented"))
504 (defun mm-view-sound-file ()
505 (message "Not implemented"))
507 (defun mm-w3-prepare-buffer ()
509 (let ((url-standalone-mode t)
510 (url-gateway-unplugged t)
511 (w3-honor-stylesheets nil))
512 (w3-prepare-buffer)))
514 (defun mm-view-message ()
515 (mm-enable-multibyte)
517 (let (gnus-article-mime-handles)
518 ;; Double decode problem may happen. See mm-inline-message.
519 (run-hooks 'gnus-article-decode-hook)
520 (gnus-article-prepare-display)
521 (setq handles gnus-article-mime-handles))
523 (setq gnus-article-mime-handles
524 (mm-merge-handles gnus-article-mime-handles handles))))
526 (goto-char (point-min)))
528 (defun mm-inline-message (handle)
531 (charset (mail-content-type-get
532 (mm-handle-type handle) 'charset))
533 gnus-displaying-mime handles)
536 (setq charset (intern (downcase charset)))
537 (when (eq charset 'us-ascii)
541 (narrow-to-region b b)
542 (mm-insert-part handle)
543 (let (gnus-article-mime-handles
544 ;; disable prepare hook
545 gnus-article-prepare-hook
546 (gnus-newsgroup-charset
547 (unless (eq charset 'gnus-decoded) ;; mm-uu might set it.
548 (or charset gnus-newsgroup-charset))))
549 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
550 (run-hooks 'gnus-article-decode-hook))
551 (gnus-article-prepare-display)
552 (setq handles gnus-article-mime-handles))
553 (goto-char (point-min))
556 (goto-char (point-max))
559 (insert "----------\n\n")
561 (setq gnus-article-mime-handles
562 (mm-merge-handles gnus-article-mime-handles handles)))
563 (mm-handle-set-undisplayer
566 (let ((inhibit-read-only t))
567 (if (fboundp 'remove-specifier)
568 ;; This is only valid on XEmacs.
569 (dolist (prop '(background background-pixmap foreground))
571 (face-property 'default prop) (current-buffer))))
572 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
574 (defun mm-display-inline-fontify (handle mode)
575 (let ((charset (mail-content-type-get (mm-handle-type handle) 'charset))
577 (unless (eq charset 'gnus-decoded)
578 (mm-with-unibyte-buffer
579 (mm-insert-part handle)
580 (mm-decompress-buffer
581 (or (mail-content-type-get (mm-handle-disposition handle) 'name)
582 (mail-content-type-get (mm-handle-disposition handle) 'filename))
585 (setq coding-system (mm-find-buffer-file-coding-system)))
586 (setq text (buffer-string))))
587 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
588 ;; on for buffers whose name begins with " ". That's why we use
589 ;; `with-current-buffer'/`generate-new-buffer' rather than
590 ;; `with-temp-buffer'.
591 (with-current-buffer (generate-new-buffer "*fontification*")
592 (buffer-disable-undo)
593 (mm-enable-multibyte)
594 (insert (cond ((eq charset 'gnus-decoded)
595 (with-current-buffer (mm-handle-buffer handle)
598 (mm-decode-coding-string text coding-system))
600 (mm-decode-string text charset))
604 (let ((font-lock-maximum-size nil)
605 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
606 (font-lock-mode-hook nil)
607 (font-lock-support-mode nil)
608 ;; I find font-lock a bit too verbose.
609 (font-lock-verbose nil))
611 ;; The mode function might have already turned on font-lock.
612 (unless (symbol-value 'font-lock-mode)
613 (font-lock-fontify-buffer)))
614 ;; By default, XEmacs font-lock uses non-duplicable text
615 ;; properties. This code forces all the text properties
616 ;; to be copied along with the text.
617 (when (featurep 'xemacs)
618 (map-extents (lambda (ext ignored)
619 (set-extent-property ext 'duplicable t)
621 nil nil nil nil nil 'text-prop))
622 (setq text (buffer-string))
623 (kill-buffer (current-buffer)))
624 (mm-insert-inline handle text)))
626 ;; Shouldn't these functions check whether the user even wants to use
627 ;; font-lock? At least under XEmacs, this fontification is pretty
628 ;; much unconditional. Also, it would be nice to change for the size
629 ;; of the fontified region.
631 (defun mm-display-patch-inline (handle)
632 (mm-display-inline-fontify handle 'diff-mode))
634 (defun mm-display-elisp-inline (handle)
635 (mm-display-inline-fontify handle 'emacs-lisp-mode))
637 (defun mm-display-dns-inline (handle)
638 (mm-display-inline-fontify handle 'dns-mode))
640 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
641 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
642 (defvar mm-pkcs7-signed-magic
643 "\x30\x5c\x28\x80\x5c\x7c\x81\x2e\x5c\x7c\x82\x2e\x2e\x5c\x7c\x83\x2e\x2e\
644 \x2e\x5c\x29\x06\x09\x5c\x2a\x86\x48\x86\xf7\x0d\x01\x07\x02")
646 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
647 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
648 (defvar mm-pkcs7-enveloped-magic
649 "\x30\x5c\x28\x80\x5c\x7c\x81\x2e\x5c\x7c\x82\x2e\x2e\x5c\x7c\x83\x2e\x2e\
650 \x2e\x5c\x29\x06\x09\x5c\x2a\x86\x48\x86\xf7\x0d\x01\x07\x03")
652 (defun mm-view-pkcs7-get-type (handle)
653 (mm-with-unibyte-buffer
654 (mm-insert-part handle)
655 (cond ((looking-at mm-pkcs7-enveloped-magic)
657 ((looking-at mm-pkcs7-signed-magic)
660 (error "Could not identify PKCS#7 type")))))
662 (defun mm-view-pkcs7 (handle)
663 (case (mm-view-pkcs7-get-type handle)
664 (enveloped (mm-view-pkcs7-decrypt handle))
665 (signed (mm-view-pkcs7-verify handle))
666 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
668 (defun mm-view-pkcs7-verify (handle)
669 (let ((verified nil))
671 (insert "MIME-Version: 1.0\n")
672 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
673 (insert-buffer-substring (mm-handle-buffer handle))
674 (setq verified (smime-verify-region (point-min) (point-max))))
675 (goto-char (point-min))
676 (mm-insert-part handle)
677 (if (search-forward "Content-Type: " nil t)
678 (delete-region (point-min) (match-beginning 0)))
679 (goto-char (point-max))
680 (if (re-search-backward "--\r?\n?" nil t)
681 (delete-region (match-end 0) (point-max)))
683 (insert-buffer-substring smime-details-buffer)))
684 (goto-char (point-min))
685 (while (search-forward "\r\n" nil t)
686 (replace-match "\n"))
689 (defun mm-view-pkcs7-decrypt (handle)
690 (insert-buffer-substring (mm-handle-buffer handle))
691 (goto-char (point-min))
692 (insert "MIME-Version: 1.0\n")
693 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
694 (smime-decrypt-region
695 (point-min) (point-max)
696 (if (= (length smime-keys) 1)
698 (smime-get-key-by-email
699 (gnus-completing-read
701 smime-keys nil nil nil (car-safe (car-safe smime-keys))))))
702 (goto-char (point-min))
703 (while (search-forward "\r\n" nil t)
704 (replace-match "\n"))
705 (goto-char (point-min)))
709 ;;; mm-view.el ends here