Use define-derived-mode (and derived-mode-p).
[emacs.git] / lisp / gnus / mm-decode.el
blob28d930b55f7f57e950f9dead989c384021382a76
1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
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/>.
22 ;;; Commentary:
24 ;;; Code:
26 ;; For Emacs <22.2 and XEmacs.
27 (eval-and-compile
28 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (require 'mail-parse)
31 (require 'mm-bodies)
32 (eval-when-compile (require 'cl))
34 (autoload 'gnus-map-function "gnus-util")
35 (autoload 'gnus-replace-in-string "gnus-util")
36 (autoload 'gnus-read-shell-command "gnus-util")
38 (autoload 'mm-inline-partial "mm-partial")
39 (autoload 'mm-inline-external-body "mm-extern")
40 (autoload 'mm-extern-cache-contents "mm-extern")
41 (autoload 'mm-insert-inline "mm-view")
43 (autoload 'mm-archive-decoders "mm-archive")
44 (autoload 'mm-archive-dissect-and-inline "mm-archive")
45 (autoload 'mm-dissect-archive "mm-archive")
47 (defvar gnus-current-window-configuration)
49 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
50 (add-hook 'gnus-exit-gnus-hook 'mm-temp-files-delete)
52 (defgroup mime-display ()
53 "Display of MIME in mail and news articles."
54 :link '(custom-manual "(emacs-mime)Display Customization")
55 :version "21.1"
56 :group 'mail
57 :group 'news
58 :group 'multimedia)
60 (defgroup mime-security ()
61 "MIME security in mail and news articles."
62 :link '(custom-manual "(emacs-mime)Display Customization")
63 :group 'mail
64 :group 'news
65 :group 'multimedia)
67 (defface mm-command-output
68 '((((class color)
69 (background dark))
70 (:foreground "ForestGreen"))
71 (((class color)
72 (background light))
73 (:foreground "red3"))
75 (:italic t)))
76 "Face used for displaying output from commands."
77 :group 'mime-display)
79 ;;; Convenience macros.
81 (defmacro mm-handle-buffer (handle)
82 `(nth 0 ,handle))
83 (defmacro mm-handle-type (handle)
84 `(nth 1 ,handle))
85 (defsubst mm-handle-media-type (handle)
86 (if (stringp (car handle))
87 (car handle)
88 (car (mm-handle-type handle))))
89 (defsubst mm-handle-media-supertype (handle)
90 (car (split-string (mm-handle-media-type handle) "/")))
91 (defsubst mm-handle-media-subtype (handle)
92 (cadr (split-string (mm-handle-media-type handle) "/")))
93 (defmacro mm-handle-encoding (handle)
94 `(nth 2 ,handle))
95 (defmacro mm-handle-undisplayer (handle)
96 `(nth 3 ,handle))
97 (defmacro mm-handle-set-undisplayer (handle function)
98 `(setcar (nthcdr 3 ,handle) ,function))
99 (defmacro mm-handle-disposition (handle)
100 `(nth 4 ,handle))
101 (defmacro mm-handle-description (handle)
102 `(nth 5 ,handle))
103 (defmacro mm-handle-cache (handle)
104 `(nth 6 ,handle))
105 (defmacro mm-handle-set-cache (handle contents)
106 `(setcar (nthcdr 6 ,handle) ,contents))
107 (defmacro mm-handle-id (handle)
108 `(nth 7 ,handle))
109 (defmacro mm-handle-multipart-original-buffer (handle)
110 `(get-text-property 0 'buffer (car ,handle)))
111 (defmacro mm-handle-multipart-from (handle)
112 `(get-text-property 0 'from (car ,handle)))
113 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
114 `(get-text-property 0 ,parameter (car ,handle)))
116 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
117 disposition description cache
119 `(list ,buffer ,type ,encoding ,undisplayer
120 ,disposition ,description ,cache ,id))
122 (defcustom mm-text-html-renderer
123 (cond ((fboundp 'libxml-parse-html-region) 'shr)
124 ((executable-find "w3m") 'gnus-w3m)
125 ((executable-find "links") 'links)
126 ((executable-find "lynx") 'lynx)
127 ((locate-library "w3") 'w3)
128 ((locate-library "html2text") 'html2text)
129 (t nil))
130 "Render of HTML contents.
131 It is one of defined renderer types, or a rendering function.
132 The defined renderer types are:
133 `shr': use the built-in Gnus HTML renderer;
134 `gnus-w3m': use Gnus renderer based on w3m;
135 `w3m': use emacs-w3m;
136 `w3m-standalone': use plain w3m;
137 `links': use links;
138 `lynx': use lynx;
139 `w3': use Emacs/W3;
140 `html2text': use html2text;
141 nil : use external viewer (default web browser)."
142 :version "24.1"
143 :type '(choice (const shr)
144 (const gnus-w3m)
145 (const w3)
146 (const w3m :tag "emacs-w3m")
147 (const w3m-standalone :tag "standalone w3m" )
148 (const links)
149 (const lynx)
150 (const html2text)
151 (const nil :tag "External viewer")
152 (function))
153 :group 'mime-display)
155 (defcustom mm-inline-text-html-with-images nil
156 "If non-nil, Gnus will allow retrieving images in HTML contents with
157 the <img> tags. It has no effect on Emacs/w3. See also the
158 documentation for the `mm-w3m-safe-url-regexp' variable."
159 :version "22.1"
160 :type 'boolean
161 :group 'mime-display)
163 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
164 "Regexp matching URLs which are considered to be safe.
165 Some HTML mails might contain a nasty trick used by spammers, using
166 the <img> tag which is far more evil than the [Click Here!] button.
167 It is most likely intended to check whether the ominous spam mail has
168 reached your eyes or not, in which case the spammer knows for sure
169 that your email address is valid. It is done by embedding an
170 identifier string into a URL that you might automatically retrieve
171 when displaying the image. The default value is \"\\\\`cid:\" which only
172 matches parts embedded to the Multipart/Related type MIME contents and
173 Gnus will never connect to the spammer's site arbitrarily. You may
174 set this variable to nil if you consider all urls to be safe."
175 :version "22.1"
176 :type '(choice (regexp :tag "Regexp")
177 (const :tag "All URLs are safe" nil))
178 :group 'mime-display)
180 (defcustom mm-inline-text-html-with-w3m-keymap t
181 "If non-nil, use emacs-w3m command keys in the article buffer."
182 :version "22.1"
183 :type 'boolean
184 :group 'mime-display)
186 (defcustom mm-enable-external t
187 "Indicate whether external MIME handlers should be used.
189 If t, all defined external MIME handlers are used. If nil, files are saved by
190 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
191 before the external MIME handler is invoked."
192 :version "22.1"
193 :type '(choice (const :tag "Always" t)
194 (const :tag "Never" nil)
195 (const :tag "Ask" ask))
196 :group 'mime-display)
198 (defcustom mm-inline-media-tests
199 '(("image/p?jpeg"
200 mm-inline-image
201 (lambda (handle)
202 (mm-valid-and-fit-image-p 'jpeg handle)))
203 ("image/png"
204 mm-inline-image
205 (lambda (handle)
206 (mm-valid-and-fit-image-p 'png handle)))
207 ("image/gif"
208 mm-inline-image
209 (lambda (handle)
210 (mm-valid-and-fit-image-p 'gif handle)))
211 ("image/tiff"
212 mm-inline-image
213 (lambda (handle)
214 (mm-valid-and-fit-image-p 'tiff handle)))
215 ("image/xbm"
216 mm-inline-image
217 (lambda (handle)
218 (mm-valid-and-fit-image-p 'xbm handle)))
219 ("image/x-xbitmap"
220 mm-inline-image
221 (lambda (handle)
222 (mm-valid-and-fit-image-p 'xbm handle)))
223 ("image/xpm"
224 mm-inline-image
225 (lambda (handle)
226 (mm-valid-and-fit-image-p 'xpm handle)))
227 ("image/x-xpixmap"
228 mm-inline-image
229 (lambda (handle)
230 (mm-valid-and-fit-image-p 'xpm handle)))
231 ("image/bmp"
232 mm-inline-image
233 (lambda (handle)
234 (mm-valid-and-fit-image-p 'bmp handle)))
235 ("image/x-portable-bitmap"
236 mm-inline-image
237 (lambda (handle)
238 (mm-valid-and-fit-image-p 'pbm handle)))
239 ("text/plain" mm-inline-text identity)
240 ("text/enriched" mm-inline-text identity)
241 ("text/richtext" mm-inline-text identity)
242 ("text/x-patch" mm-display-patch-inline identity)
243 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
244 ("text/x-diff" mm-display-patch-inline identity)
245 ("application/emacs-lisp" mm-display-elisp-inline identity)
246 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
247 ("application/x-shellscript" mm-display-shell-script-inline identity)
248 ("application/x-sh" mm-display-shell-script-inline identity)
249 ("text/x-sh" mm-display-shell-script-inline identity)
250 ("application/javascript" mm-display-javascript-inline identity)
251 ("text/dns" mm-display-dns-inline identity)
252 ("text/x-org" mm-display-org-inline identity)
253 ("text/html"
254 mm-inline-text-html
255 (lambda (handle)
256 mm-text-html-renderer))
257 ("text/x-vcard"
258 mm-inline-text-vcard
259 (lambda (handle)
260 (or (featurep 'vcard)
261 (locate-library "vcard"))))
262 ("message/delivery-status" mm-inline-text identity)
263 ("message/rfc822" mm-inline-message identity)
264 ("message/partial" mm-inline-partial identity)
265 ("message/external-body" mm-inline-external-body identity)
266 ("text/.*" mm-inline-text identity)
267 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity)
268 ("application/zip" mm-archive-dissect-and-inline identity)
269 ("audio/wav" mm-inline-audio
270 (lambda (handle)
271 (and (or (featurep 'nas-sound) (featurep 'native-sound))
272 (device-sound-enabled-p))))
273 ("audio/au"
274 mm-inline-audio
275 (lambda (handle)
276 (and (or (featurep 'nas-sound) (featurep 'native-sound))
277 (device-sound-enabled-p))))
278 ("application/pgp-signature" ignore identity)
279 ("application/x-pkcs7-signature" ignore identity)
280 ("application/pkcs7-signature" ignore identity)
281 ("application/x-pkcs7-mime" ignore identity)
282 ("application/pkcs7-mime" ignore identity)
283 ("multipart/alternative" ignore identity)
284 ("multipart/mixed" ignore identity)
285 ("multipart/related" ignore identity)
286 ("image/.*"
287 mm-inline-image
288 (lambda (handle)
289 (and (mm-valid-image-format-p 'imagemagick)
290 (mm-with-unibyte-buffer
291 (mm-insert-part handle)
292 (let ((image
293 (ignore-errors
294 (if (fboundp 'create-image)
295 (create-image (buffer-string) 'imagemagick 'data-p)
296 (mm-create-image-xemacs
297 (mm-handle-media-subtype handle))))))
298 (when image
299 (setcar (cdr handle) (list "image/imagemagick"))
300 (mm-image-fit-p handle)))))))
301 ;; Disable audio and image
302 ("audio/.*" ignore ignore)
303 ("image/.*" ignore ignore)
304 ;; Default to displaying as text
305 (".*" mm-inline-text mm-readable-p))
306 "Alist of media types/tests saying whether types can be displayed inline."
307 :type '(repeat (list (regexp :tag "MIME type")
308 (function :tag "Display function")
309 (function :tag "Display test")))
310 :group 'mime-display)
312 (defcustom mm-inlined-types
313 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
314 "message/partial" "message/external-body" "application/emacs-lisp"
315 "application/x-emacs-lisp"
316 "application/pgp-signature" "application/x-pkcs7-signature"
317 "application/pkcs7-signature" "application/x-pkcs7-mime"
318 "application/pkcs7-mime"
319 "application/x-gtar-compressed"
320 "application/x-tar"
321 "application/zip"
322 ;; Mutt still uses this even though it has already been withdrawn.
323 "application/pgp")
324 "List of media types that are to be displayed inline.
325 See also `mm-inline-media-tests', which says how to display a media
326 type inline."
327 :type '(repeat regexp)
328 :group 'mime-display)
330 (defcustom mm-keep-viewer-alive-types
331 '("application/postscript" "application/msword" "application/vnd.ms-excel"
332 "application/pdf" "application/x-dvi")
333 "List of media types for which the external viewer will not be killed
334 when selecting a different article."
335 :version "22.1"
336 :type '(repeat regexp)
337 :group 'mime-display)
339 (defcustom mm-automatic-display
340 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
341 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
342 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
343 "application/emacs-lisp" "application/x-emacs-lisp"
344 "application/x-pkcs7-signature"
345 "application/pkcs7-signature" "application/x-pkcs7-mime"
346 "application/pkcs7-mime"
347 ;; Mutt still uses this even though it has already been withdrawn.
348 "application/pgp\\'"
349 "text/x-org")
350 "A list of MIME types to be displayed automatically."
351 :type '(repeat regexp)
352 :group 'mime-display)
354 (defcustom mm-attachment-override-types '("text/x-vcard"
355 "application/pkcs7-mime"
356 "application/x-pkcs7-mime"
357 "application/pkcs7-signature"
358 "application/x-pkcs7-signature")
359 "Types to have \"attachment\" ignored if they can be displayed inline."
360 :type '(repeat regexp)
361 :group 'mime-display)
363 (defcustom mm-inline-override-types nil
364 "Types to be treated as attachments even if they can be displayed inline."
365 :type '(repeat regexp)
366 :group 'mime-display)
368 (defcustom mm-automatic-external-display nil
369 "List of MIME type regexps that will be displayed externally automatically."
370 :type '(repeat regexp)
371 :group 'mime-display)
373 (defcustom mm-discouraged-alternatives nil
374 "List of MIME types that are discouraged when viewing multipart/alternative.
375 Viewing agents are supposed to view the last possible part of a message,
376 as that is supposed to be the richest. However, users may prefer other
377 types instead, and this list says what types are most unwanted. If,
378 for instance, text/html parts are very unwanted, and text/richtext are
379 somewhat unwanted, then the value of this variable should be set
382 (\"text/html\" \"text/richtext\")
384 Adding \"image/.*\" might also be useful. Spammers use it as the
385 preferred part of multipart/alternative messages. See also
386 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
387 enables you to choose manually one of two types those mails include."
388 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
389 :group 'mime-display)
391 (defcustom mm-tmp-directory
392 (if (fboundp 'temp-directory)
393 (temp-directory)
394 (if (boundp 'temporary-file-directory)
395 temporary-file-directory
396 "/tmp/"))
397 "Where mm will store its temporary files."
398 :type 'directory
399 :group 'mime-display)
401 (defcustom mm-inline-large-images nil
402 "If t, then all images fit in the buffer.
403 If 'resize, try to resize the images so they fit."
404 :type '(radio
405 (const :tag "Inline large images as they are." t)
406 (const :tag "Resize large images." resize)
407 (const :tag "Do not inline large images." nil))
408 :group 'mime-display)
410 (defcustom mm-file-name-rewrite-functions
411 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
412 "List of functions used for rewriting file names of MIME parts.
413 Each function takes a file name as input and returns a file name.
415 Ready-made functions include `mm-file-name-delete-control',
416 `mm-file-name-delete-gotchas' (you should not remove these two
417 functions), `mm-file-name-delete-whitespace',
418 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
419 `mm-file-name-replace-whitespace', `capitalize', `downcase',
420 `upcase', and `upcase-initials'."
421 :type '(list (set :inline t
422 (const mm-file-name-delete-control)
423 (const mm-file-name-delete-gotchas)
424 (const mm-file-name-delete-whitespace)
425 (const mm-file-name-trim-whitespace)
426 (const mm-file-name-collapse-whitespace)
427 (const mm-file-name-replace-whitespace)
428 (const capitalize)
429 (const downcase)
430 (const upcase)
431 (const upcase-initials)
432 (repeat :inline t
433 :tag "Function"
434 function)))
435 :version "23.1" ;; No Gnus
436 :group 'mime-display)
439 (defvar mm-path-name-rewrite-functions nil
440 "*List of functions for rewriting the full file names of MIME parts.
441 This is used when viewing parts externally, and is meant for
442 transforming the absolute name so that non-compliant programs can find
443 the file where it's saved.
445 Each function takes a file name as input and returns a file name.")
447 (defvar mm-file-name-replace-whitespace nil
448 "String used for replacing whitespace characters; default is `\"_\"'.")
450 (defcustom mm-default-directory nil
451 "The default directory where mm will save files.
452 If not set, `default-directory' will be used."
453 :type '(choice directory (const :tag "Default" nil))
454 :group 'mime-display)
456 (defcustom mm-attachment-file-modes 384
457 "Set the mode bits of saved attachments to this integer."
458 :version "22.1"
459 :type 'integer
460 :group 'mime-display)
462 (defcustom mm-external-terminal-program "xterm"
463 "The program to start an external terminal."
464 :version "22.1"
465 :type 'string
466 :group 'mime-display)
468 ;;; Internal variables.
470 (defvar mm-last-shell-command "")
471 (defvar mm-content-id-alist nil)
472 (defvar mm-postponed-undisplay-list nil)
473 (defvar mm-inhibit-auto-detect-attachment nil)
474 (defvar mm-temp-files-to-be-deleted nil
475 "List of temporary files scheduled to be deleted.")
476 (defvar mm-temp-files-cache-file (concat ".mm-temp-files-" (user-login-name))
477 "Name of a file that caches a list of temporary files to be deleted.
478 The file will be saved in the directory `mm-tmp-directory'.")
480 ;; According to RFC2046, in particular, in a digest, the default
481 ;; Content-Type value for a body part is changed from "text/plain" to
482 ;; "message/rfc822".
483 (defvar mm-dissect-default-type "text/plain")
485 (autoload 'mml2015-verify "mml2015")
486 (autoload 'mml2015-verify-test "mml2015")
487 (autoload 'mml-smime-verify "mml-smime")
488 (autoload 'mml-smime-verify-test "mml-smime")
490 (defvar mm-verify-function-alist
491 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
492 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
493 mm-uu-pgp-signed-test)
494 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
495 mml-smime-verify-test)
496 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
497 mml-smime-verify-test)))
499 (defcustom mm-verify-option 'never
500 "Option of verifying signed parts.
501 `never', not verify; `always', always verify;
502 `known', only verify known protocols. Otherwise, ask user.
504 When set to `always' or `known', you should add
505 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
506 result of the verification."
507 :version "22.1"
508 :type '(choice (item always)
509 (item never)
510 (item :tag "only known protocols" known)
511 (item :tag "ask" nil))
512 :group 'mime-security)
514 (autoload 'mml2015-decrypt "mml2015")
515 (autoload 'mml2015-decrypt-test "mml2015")
517 (defvar mm-decrypt-function-alist
518 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
519 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
520 mm-uu-pgp-encrypted-test)))
522 (defcustom mm-decrypt-option nil
523 "Option of decrypting encrypted parts.
524 `never', not decrypt; `always', always decrypt;
525 `known', only decrypt known protocols. Otherwise, ask user."
526 :version "22.1"
527 :type '(choice (item always)
528 (item never)
529 (item :tag "only known protocols" known)
530 (item :tag "ask" nil))
531 :group 'mime-security)
533 (defvar mm-viewer-completion-map
534 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
535 (set-keymap-parent map minibuffer-local-completion-map)
536 ;; Should we bind other key to minibuffer-complete-word?
537 (define-key map " " 'self-insert-command)
538 map)
539 "Keymap for input viewer with completion.")
541 (defvar mm-viewer-completion-map
542 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
543 (set-keymap-parent map minibuffer-local-completion-map)
544 ;; Should we bind other key to minibuffer-complete-word?
545 (define-key map " " 'self-insert-command)
546 map)
547 "Keymap for input viewer with completion.")
549 ;;; The functions.
551 (defun mm-alist-to-plist (alist)
552 "Convert association list ALIST into the equivalent property-list form.
553 The plist is returned. This converts from
555 \((a . 1) (b . 2) (c . 3))
557 into
559 \(a 1 b 2 c 3)
561 The original alist is not modified. See also `destructive-alist-to-plist'."
562 (let (plist)
563 (while alist
564 (let ((el (car alist)))
565 (setq plist (cons (cdr el) (cons (car el) plist))))
566 (setq alist (cdr alist)))
567 (nreverse plist)))
569 (defun mm-keep-viewer-alive-p (handle)
570 "Say whether external viewer for HANDLE should stay alive."
571 (let ((types mm-keep-viewer-alive-types)
572 (type (mm-handle-media-type handle))
574 (catch 'found
575 (while (setq ty (pop types))
576 (when (string-match ty type)
577 (throw 'found t))))))
579 (defun mm-handle-set-external-undisplayer (handle function)
580 "Set the undisplayer for HANDLE to FUNCTION.
581 Postpone undisplaying of viewers for types in
582 `mm-keep-viewer-alive-types'."
583 (if (mm-keep-viewer-alive-p handle)
584 (let ((new-handle (copy-sequence handle)))
585 (mm-handle-set-undisplayer new-handle function)
586 (mm-handle-set-undisplayer handle nil)
587 (push new-handle mm-postponed-undisplay-list))
588 (mm-handle-set-undisplayer handle function)))
590 (defun mm-destroy-postponed-undisplay-list ()
591 (when mm-postponed-undisplay-list
592 (message "Destroying external MIME viewers")
593 (mm-destroy-parts mm-postponed-undisplay-list)))
595 (defun mm-temp-files-delete ()
596 "Delete temporary files and those parent directories.
597 Note that the deletion may fail if a program is catching hold of a file
598 under Windows or Cygwin. In that case, it schedules the deletion of
599 files left at the next time."
600 (let* ((coding-system-for-read mm-universal-coding-system)
601 (coding-system-for-write mm-universal-coding-system)
602 (cache-file (expand-file-name mm-temp-files-cache-file
603 mm-tmp-directory))
604 (cache (when (file-exists-p cache-file)
605 (mm-with-multibyte-buffer
606 (insert-file-contents cache-file)
607 (split-string (buffer-string) "\n" t))))
608 fails)
609 (dolist (temp (append cache mm-temp-files-to-be-deleted))
610 (when (and (file-exists-p temp)
611 (if (file-directory-p temp)
612 ;; A parent directory left at the previous time.
613 (progn
614 (ignore-errors (delete-directory temp))
615 (file-exists-p temp))
616 ;; Delete a temporary file and its parent directory.
617 (ignore-errors (delete-file temp))
618 (or (file-exists-p temp)
619 (progn
620 (setq temp (file-name-directory temp))
621 (ignore-errors (delete-directory temp))
622 (file-exists-p temp)))))
623 (push temp fails)))
624 (if fails
625 ;; Schedule the deletion of the files left at the next time.
626 (progn
627 (write-region (concat (mapconcat 'identity (nreverse fails) "\n")
628 "\n")
629 nil cache-file nil 'silent)
630 (set-file-modes cache-file #o600))
631 (when (file-exists-p cache-file)
632 (ignore-errors (delete-file cache-file))))
633 (setq mm-temp-files-to-be-deleted nil)))
635 (autoload 'message-fetch-field "message")
637 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
638 "Dissect the current buffer and return a list of MIME handles.
639 If NO-STRICT-MIME, don't require the message to have a
640 MIME-Version header before proceeding."
641 (save-excursion
642 (let (ct ctl type subtype cte cd description id result)
643 (save-restriction
644 (mail-narrow-to-head)
645 (when (or no-strict-mime
646 loose-mime
647 (mail-fetch-field "mime-version"))
648 (setq ct (mail-fetch-field "content-type")
649 ctl (and ct (mail-header-parse-content-type ct))
650 cte (mail-fetch-field "content-transfer-encoding")
651 cd (or (mail-fetch-field "content-disposition")
652 (when (and ctl
653 (eq 'mm-inline-text
654 (cadr (mm-assoc-string-match
655 mm-inline-media-tests
656 (car ctl)))))
657 "inline"))
658 ;; Newlines in description should be stripped so as
659 ;; not to break the MIME tag into two or more lines.
660 description (message-fetch-field "content-description")
661 id (mail-fetch-field "content-id"))
662 (unless from
663 (setq from (mail-fetch-field "from")))
664 ;; FIXME: In some circumstances, this code is running within
665 ;; an unibyte macro. mail-extract-address-components
666 ;; creates unibyte buffers. This `if', though not a perfect
667 ;; solution, avoids most of them.
668 (if from
669 (setq from (cadr (mail-extract-address-components from))))
670 (if description
671 (setq description (mail-decode-encoded-word-string
672 description)))))
673 (if (or (not ctl)
674 (not (string-match "/" (car ctl))))
675 (mm-dissect-singlepart
676 (list mm-dissect-default-type)
677 (and cte (intern (downcase (mail-header-strip cte))))
678 no-strict-mime
679 (and cd (mail-header-parse-content-disposition cd))
680 description)
681 (setq type (split-string (car ctl) "/"))
682 (setq subtype (cadr type)
683 type (car type))
684 (setq
685 result
686 (cond
687 ((equal type "multipart")
688 (let ((mm-dissect-default-type (if (equal subtype "digest")
689 "message/rfc822"
690 "text/plain"))
691 (start (cdr (assq 'start (cdr ctl)))))
692 (add-text-properties 0 (length (car ctl))
693 (mm-alist-to-plist (cdr ctl)) (car ctl))
695 ;; what really needs to be done here is a way to link a
696 ;; MIME handle back to it's parent MIME handle (in a multilevel
697 ;; MIME article). That would probably require changing
698 ;; the mm-handle API so we simply store the multipart buffer
699 ;; name as a text property of the "multipart/whatever" string.
700 (add-text-properties 0 (length (car ctl))
701 (list 'buffer (mm-copy-to-buffer)
702 'from from
703 'start start)
704 (car ctl))
705 (cons (car ctl) (mm-dissect-multipart ctl from))))
707 (mm-possibly-verify-or-decrypt
708 (mm-dissect-singlepart
710 (and cte (intern (downcase (mail-header-strip cte))))
711 no-strict-mime
712 (and cd (mail-header-parse-content-disposition cd))
713 description id)
714 ctl from))))
715 (when id
716 (when (string-match " *<\\(.*\\)> *" id)
717 (setq id (match-string 1 id)))
718 (push (cons id result) mm-content-id-alist))
719 result))))
721 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
722 (when (or force
723 (if (equal "text/plain" (car ctl))
724 (assoc 'format ctl)
726 ;; Guess what the type of application/octet-stream parts should
727 ;; really be.
728 (let ((filename (cdr (assq 'filename (cdr cdl)))))
729 (when (and (not mm-inhibit-auto-detect-attachment)
730 (equal (car ctl) "application/octet-stream")
731 filename
732 (string-match "\\.\\([^.]+\\)$" filename))
733 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename))))
734 (when new-type
735 (setcar ctl new-type)))))
736 (let ((handle
737 (mm-make-handle
738 (mm-copy-to-buffer) ctl cte nil cdl description nil id))
739 (decoder (assoc (car ctl) (mm-archive-decoders))))
740 (if (and decoder
741 ;; Do automatic decoding
742 (cadr decoder)
743 (executable-find (caddr decoder)))
744 (mm-dissect-archive handle)
745 handle))))
747 (defun mm-dissect-multipart (ctl from)
748 (goto-char (point-min))
749 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
750 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
751 start parts
752 (end (save-excursion
753 (goto-char (point-max))
754 (if (re-search-backward close-delimiter nil t)
755 (match-beginning 0)
756 (point-max))))
757 (mm-inhibit-auto-detect-attachment
758 (equal (car ctl) "multipart/encrypted")))
759 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
760 (while (and (< (point) end) (re-search-forward boundary end t))
761 (goto-char (match-beginning 0))
762 (when start
763 (save-excursion
764 (save-restriction
765 (narrow-to-region start (point))
766 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
767 (end-of-line 2)
768 (or (looking-at boundary)
769 (forward-line 1))
770 (setq start (point)))
771 (when (and start (< start end))
772 (save-excursion
773 (save-restriction
774 (narrow-to-region start end)
775 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
776 (mm-possibly-verify-or-decrypt (nreverse parts) ctl from)))
778 (defun mm-copy-to-buffer ()
779 "Copy the contents of the current buffer to a fresh buffer."
780 (let ((obuf (current-buffer))
781 (mb (mm-multibyte-p))
782 beg)
783 (goto-char (point-min))
784 (search-forward-regexp "^\n" nil t)
785 (setq beg (point))
786 (with-current-buffer
787 (generate-new-buffer " *mm*")
788 ;; Preserve the data's unibyteness (for url-insert-file-contents).
789 (mm-set-buffer-multibyte mb)
790 (insert-buffer-substring obuf beg)
791 (current-buffer))))
793 (defun mm-display-parts (handle &optional no-default)
794 (if (stringp (car handle))
795 (mapcar 'mm-display-parts (cdr handle))
796 (if (bufferp (car handle))
797 (save-restriction
798 (narrow-to-region (point) (point))
799 (mm-display-part handle)
800 (goto-char (point-max)))
801 (mapcar 'mm-display-parts handle))))
803 (autoload 'mailcap-parse-mailcaps "mailcap")
804 (autoload 'mailcap-mime-info "mailcap")
806 (defun mm-display-part (handle &optional no-default force)
807 "Display the MIME part represented by HANDLE.
808 Returns nil if the part is removed; inline if displayed inline;
809 external if displayed external."
810 (save-excursion
811 (mailcap-parse-mailcaps)
812 (if (and (not force)
813 (mm-handle-displayed-p handle))
814 (mm-remove-part handle)
815 (let* ((ehandle (if (equal (mm-handle-media-type handle)
816 "message/external-body")
817 (progn
818 (unless (mm-handle-cache handle)
819 (mm-extern-cache-contents handle))
820 (mm-handle-cache handle))
821 handle))
822 (type (mm-handle-media-type ehandle))
823 (method (mailcap-mime-info type))
824 (filename (or (mail-content-type-get
825 (mm-handle-disposition handle) 'filename)
826 (mail-content-type-get
827 (mm-handle-type handle) 'name)
828 "<file>"))
829 (external mm-enable-external)
830 (decoder (assoc (car (mm-handle-type handle))
831 (mm-archive-decoders))))
832 (cond
833 ((and decoder
834 (executable-find (caddr decoder)))
835 (mm-archive-dissect-and-inline handle)
836 'inline)
837 ((and (mm-inlinable-p ehandle)
838 (mm-inlined-p ehandle))
839 (forward-line 1)
840 (mm-display-inline handle)
841 'inline)
842 ((or method
843 (not no-default))
844 (if (and (not method)
845 (equal "text" (car (split-string type "/"))))
846 (progn
847 (forward-line 1)
848 (mm-insert-inline handle (mm-get-part handle))
849 'inline)
850 (setq external
851 (and method ;; If nil, we always use "save".
852 (stringp method) ;; 'mailcap-save-binary-file
853 (or (eq mm-enable-external t)
854 (and (eq mm-enable-external 'ask)
855 (y-or-n-p
856 (concat
857 "Display part (" type
858 ") using external program"
859 ;; Can non-string method ever happen?
860 (if (stringp method)
861 (concat
862 " \"" (format method filename) "\"")
864 "? "))))))
865 (if external
866 (mm-display-external
867 handle (or method 'mailcap-save-binary-file))
868 (mm-display-external
869 handle 'mailcap-save-binary-file)))))))))
871 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
872 (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
873 (declare-function term-mode "term" ())
874 (declare-function term-char-mode "term" ())
876 (defun mm-display-external (handle method)
877 "Display HANDLE using METHOD."
878 (let ((outbuf (current-buffer)))
879 (mm-with-unibyte-buffer
880 (if (functionp method)
881 (let ((cur (current-buffer)))
882 (if (eq method 'mailcap-save-binary-file)
883 (progn
884 (set-buffer (generate-new-buffer " *mm*"))
885 (setq method nil))
886 (mm-insert-part handle)
887 (mm-add-meta-html-tag handle)
888 (let ((win (get-buffer-window cur t)))
889 (when win
890 (select-window win)))
891 (switch-to-buffer (generate-new-buffer " *mm*")))
892 (buffer-disable-undo)
893 (mm-set-buffer-file-coding-system mm-binary-coding-system)
894 (insert-buffer-substring cur)
895 (goto-char (point-min))
896 (when method
897 (message "Viewing with %s" method))
898 (let ((mm (current-buffer))
899 (non-viewer (assq 'non-viewer
900 (mailcap-mime-info
901 (mm-handle-media-type handle) t))))
902 (unwind-protect
903 (if method
904 (funcall method)
905 (mm-save-part handle))
906 (when (and (not non-viewer)
907 method)
908 (mm-handle-set-undisplayer handle mm)))))
909 ;; The function is a string to be executed.
910 (mm-insert-part handle)
911 (mm-add-meta-html-tag handle)
912 (let* ((dir (mm-make-temp-file
913 (expand-file-name "emm." mm-tmp-directory) 'dir))
914 (filename (or
915 (mail-content-type-get
916 (mm-handle-disposition handle) 'filename)
917 (mail-content-type-get
918 (mm-handle-type handle) 'name)))
919 (mime-info (mailcap-mime-info
920 (mm-handle-media-type handle) t))
921 (needsterm (or (assoc "needsterm" mime-info)
922 (assoc "needsterminal" mime-info)))
923 (copiousoutput (assoc "copiousoutput" mime-info))
924 file buffer)
925 ;; We create a private sub-directory where we store our files.
926 (set-file-modes dir #o700)
927 (if filename
928 (setq file (expand-file-name
929 (gnus-map-function mm-file-name-rewrite-functions
930 (file-name-nondirectory filename))
931 dir))
932 ;; Use nametemplate (defined in RFC1524) if it is specified
933 ;; in mailcap.
934 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
935 (if (and suffix
936 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
937 (setq suffix (match-string 1 suffix))
938 ;; Otherwise, use a suffix according to
939 ;; `mailcap-mime-extensions'.
940 (setq suffix (car (rassoc (mm-handle-media-type handle)
941 mailcap-mime-extensions))))
942 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
943 nil suffix))))
944 (let ((coding-system-for-write mm-binary-coding-system))
945 (write-region (point-min) (point-max) file nil 'nomesg))
946 ;; The file is deleted after the viewer exists. If the users edits
947 ;; the file, changes will be lost. Set file to read-only to make it
948 ;; clear.
949 (set-file-modes file #o400)
950 (message "Viewing with %s" method)
951 (cond
952 (needsterm
953 (let ((command (mm-mailcap-command
954 method file (mm-handle-type handle))))
955 (unwind-protect
956 (if window-system
957 (set-process-sentinel
958 (start-process "*display*" nil
959 mm-external-terminal-program
960 "-e" shell-file-name
961 shell-command-switch command)
962 `(lambda (process state)
963 (if (eq 'exit (process-status process))
964 (run-at-time
965 60.0 nil
966 (lambda ()
967 (ignore-errors (delete-file ,file))
968 (ignore-errors (delete-directory
969 ,(file-name-directory
970 file))))))))
971 (require 'term)
972 (require 'gnus-win)
973 (set-buffer
974 (setq buffer
975 (make-term "display"
976 shell-file-name
978 shell-command-switch command)))
979 (term-mode)
980 (term-char-mode)
981 (set-process-sentinel
982 (get-buffer-process buffer)
983 `(lambda (process state)
984 (when (eq 'exit (process-status process))
985 (ignore-errors (delete-file ,file))
986 (ignore-errors
987 (delete-directory ,(file-name-directory file)))
988 (gnus-configure-windows
989 ',gnus-current-window-configuration))))
990 (gnus-configure-windows 'display-term))
991 (mm-handle-set-external-undisplayer handle (cons file buffer))
992 (add-to-list 'mm-temp-files-to-be-deleted file t))
993 (message "Displaying %s..." command))
994 'external)
995 (copiousoutput
996 (with-current-buffer outbuf
997 (forward-line 1)
998 (mm-insert-inline
999 handle
1000 (unwind-protect
1001 (progn
1002 (call-process shell-file-name nil
1003 (setq buffer
1004 (generate-new-buffer " *mm*"))
1006 shell-command-switch
1007 (mm-mailcap-command
1008 method file (mm-handle-type handle)))
1009 (if (buffer-live-p buffer)
1010 (with-current-buffer buffer
1011 (buffer-string))))
1012 (progn
1013 (ignore-errors (delete-file file))
1014 (ignore-errors (delete-directory
1015 (file-name-directory file)))
1016 (ignore-errors (kill-buffer buffer))))))
1017 'inline)
1019 ;; Deleting the temp file should be postponed for some wrappers,
1020 ;; shell scripts, and so on, which might exit right after having
1021 ;; started a viewer command as a background job.
1022 (let ((command (mm-mailcap-command
1023 method file (mm-handle-type handle))))
1024 (unwind-protect
1025 (let ((process-connection-type nil))
1026 (start-process "*display*"
1027 (setq buffer
1028 (generate-new-buffer " *mm*"))
1029 shell-file-name
1030 shell-command-switch command)
1031 (set-process-sentinel
1032 (get-buffer-process buffer)
1033 (lexical-let ((outbuf outbuf)
1034 (file file)
1035 (buffer buffer)
1036 (command command)
1037 (handle handle))
1038 (lambda (process state)
1039 (when (eq (process-status process) 'exit)
1040 (run-at-time
1041 60.0 nil
1042 (lambda ()
1043 (ignore-errors (delete-file file))
1044 (ignore-errors (delete-directory
1045 (file-name-directory file)))))
1046 (when (buffer-live-p outbuf)
1047 (with-current-buffer outbuf
1048 (let ((buffer-read-only nil)
1049 (point (point)))
1050 (forward-line 2)
1051 (let ((start (point)))
1052 (mm-insert-inline
1053 handle (with-current-buffer buffer
1054 (buffer-string)))
1055 (put-text-property start (point)
1056 'face 'mm-command-output))
1057 (goto-char point))))
1058 (when (buffer-live-p buffer)
1059 (kill-buffer buffer)))
1060 (message "Displaying %s...done" command)))))
1061 (mm-handle-set-external-undisplayer
1062 handle (cons file buffer))
1063 (add-to-list 'mm-temp-files-to-be-deleted file t))
1064 (message "Displaying %s..." command))
1065 'external)))))))
1067 (defun mm-mailcap-command (method file type-list)
1068 (let ((ctl (cdr type-list))
1069 (beg 0)
1070 (uses-stdin t)
1071 out sub total)
1072 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1073 method beg)
1074 (push (substring method beg (match-beginning 0)) out)
1075 (setq beg (match-end 0)
1076 total (match-string 0 method)
1077 sub (match-string 1 method))
1078 (cond
1079 ((string= total "%%")
1080 (push "%" out))
1081 ((or (string= total "%s")
1082 ;; We do our own quoting.
1083 (string= total "'%s'")
1084 (string= total "\"%s\""))
1085 (setq uses-stdin nil)
1086 (push (shell-quote-argument
1087 (gnus-map-function mm-path-name-rewrite-functions file)) out))
1088 ((string= total "%t")
1089 (push (shell-quote-argument (car type-list)) out))
1091 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
1092 (push (substring method beg (length method)) out)
1093 (when uses-stdin
1094 (push "<" out)
1095 (push (shell-quote-argument
1096 (gnus-map-function mm-path-name-rewrite-functions file))
1097 out))
1098 (mapconcat 'identity (nreverse out) "")))
1100 (defun mm-remove-parts (handles)
1101 "Remove the displayed MIME parts represented by HANDLES."
1102 (if (and (listp handles)
1103 (bufferp (car handles)))
1104 (mm-remove-part handles)
1105 (let (handle)
1106 (while (setq handle (pop handles))
1107 (cond
1108 ((stringp handle)
1109 (when (buffer-live-p (get-text-property 0 'buffer handle))
1110 (kill-buffer (get-text-property 0 'buffer handle))))
1111 ((and (listp handle)
1112 (stringp (car handle)))
1113 (mm-remove-parts (cdr handle)))
1115 (mm-remove-part handle)))))))
1117 (defun mm-destroy-parts (handles)
1118 "Remove the displayed MIME parts represented by HANDLES."
1119 (if (and (listp handles)
1120 (bufferp (car handles)))
1121 (mm-destroy-part handles)
1122 (let (handle)
1123 (while (setq handle (pop handles))
1124 (cond
1125 ((stringp handle)
1126 (when (buffer-live-p (get-text-property 0 'buffer handle))
1127 (kill-buffer (get-text-property 0 'buffer handle))))
1128 ((and (listp handle)
1129 (stringp (car handle)))
1130 (mm-destroy-parts handle))
1132 (mm-destroy-part handle)))))))
1134 (defun mm-remove-part (handle)
1135 "Remove the displayed MIME part represented by HANDLE."
1136 (when (listp handle)
1137 (let ((object (mm-handle-undisplayer handle)))
1138 (ignore-errors
1139 (cond
1140 ;; Internally displayed part.
1141 ((mm-annotationp object)
1142 (if (featurep 'xemacs)
1143 (delete-annotation object)))
1144 ((or (functionp object)
1145 (and (listp object)
1146 (eq (car object) 'lambda)))
1147 (funcall object))
1148 ;; Externally displayed part.
1149 ((consp object)
1150 (condition-case ()
1151 (while (get-buffer-process (cdr object))
1152 (interrupt-process (get-buffer-process (cdr object)))
1153 (message "Waiting for external displayer to die...")
1154 (sit-for 1))
1155 (quit)
1156 (error))
1157 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1158 (message "Waiting for external displayer to die...done")
1159 (ignore-errors (delete-file (car object)))
1160 (ignore-errors (delete-directory (file-name-directory
1161 (car object)))))
1162 ((bufferp object)
1163 (when (buffer-live-p object)
1164 (kill-buffer object)))))
1165 (mm-handle-set-undisplayer handle nil))))
1167 (defun mm-display-inline (handle)
1168 (let* ((type (mm-handle-media-type handle))
1169 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1170 (funcall function handle)
1171 (goto-char (point-min))))
1173 (defun mm-assoc-string-match (alist type)
1174 (dolist (elem alist)
1175 (when (string-match (car elem) type)
1176 (return elem))))
1178 (defun mm-automatic-display-p (handle)
1179 "Say whether the user wants HANDLE to be displayed automatically."
1180 (let ((methods mm-automatic-display)
1181 (type (mm-handle-media-type handle))
1182 method result)
1183 (while (setq method (pop methods))
1184 (when (and (not (mm-inline-override-p handle))
1185 (string-match method type))
1186 (setq result t
1187 methods nil)))
1188 result))
1190 (defun mm-inlinable-p (handle &optional type)
1191 "Say whether HANDLE can be displayed inline.
1192 TYPE is the mime-type of the object; it defaults to the one given
1193 in HANDLE."
1194 (unless type (setq type (mm-handle-media-type handle)))
1195 (let ((alist mm-inline-media-tests)
1196 test)
1197 (while alist
1198 (when (string-match (caar alist) type)
1199 (setq test (caddar alist)
1200 alist nil)
1201 (setq test (funcall test handle)))
1202 (pop alist))
1203 test))
1205 (defun mm-inlined-p (handle)
1206 "Say whether the user wants HANDLE to be displayed inline."
1207 (let ((methods mm-inlined-types)
1208 (type (mm-handle-media-type handle))
1209 method result)
1210 (while (setq method (pop methods))
1211 (when (and (not (mm-inline-override-p handle))
1212 (string-match method type))
1213 (setq result t
1214 methods nil)))
1215 result))
1217 (defun mm-attachment-override-p (handle)
1218 "Say whether HANDLE should have attachment behavior overridden."
1219 (let ((types mm-attachment-override-types)
1220 (type (mm-handle-media-type handle))
1222 (catch 'found
1223 (while (setq ty (pop types))
1224 (when (and (string-match ty type)
1225 (mm-inlinable-p handle))
1226 (throw 'found t))))))
1228 (defun mm-inline-override-p (handle)
1229 "Say whether HANDLE should have inline behavior overridden."
1230 (let ((types mm-inline-override-types)
1231 (type (mm-handle-media-type handle))
1233 (catch 'found
1234 (while (setq ty (pop types))
1235 (when (string-match ty type)
1236 (throw 'found t))))))
1238 (defun mm-automatic-external-display-p (type)
1239 "Return the user-defined method for TYPE."
1240 (let ((methods mm-automatic-external-display)
1241 method result)
1242 (while (setq method (pop methods))
1243 (when (string-match method type)
1244 (setq result t
1245 methods nil)))
1246 result))
1248 (defun mm-destroy-part (handle)
1249 "Destroy the data structures connected to HANDLE."
1250 (when (listp handle)
1251 (mm-remove-part handle)
1252 (when (buffer-live-p (mm-handle-buffer handle))
1253 (kill-buffer (mm-handle-buffer handle)))))
1255 (defun mm-handle-displayed-p (handle)
1256 "Say whether HANDLE is displayed or not."
1257 (mm-handle-undisplayer handle))
1260 ;;; Functions for outputting parts
1263 (defmacro mm-with-part (handle &rest forms)
1264 "Run FORMS in the temp buffer containing the contents of HANDLE."
1265 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1266 ;; chars, so the buffer should be unibyte. It may happen that the
1267 ;; handle-buffer is multibyte for some reason, in which case now is a good
1268 ;; time to adjust it, since we know at this point that it should
1269 ;; be unibyte.
1270 `(let* ((handle ,handle))
1271 (when (and (mm-handle-buffer handle)
1272 (buffer-name (mm-handle-buffer handle)))
1273 (with-temp-buffer
1274 (mm-disable-multibyte)
1275 (insert-buffer-substring (mm-handle-buffer handle))
1276 (mm-decode-content-transfer-encoding
1277 (mm-handle-encoding handle)
1278 (mm-handle-media-type handle))
1279 ,@forms))))
1280 (put 'mm-with-part 'lisp-indent-function 1)
1281 (put 'mm-with-part 'edebug-form-spec '(body))
1283 (defun mm-get-part (handle &optional no-cache)
1284 "Return the contents of HANDLE as a string.
1285 If NO-CACHE is non-nil, cached contents of a message/external-body part
1286 are ignored."
1287 (if (and (not no-cache)
1288 (equal (mm-handle-media-type handle) "message/external-body"))
1289 (progn
1290 (unless (mm-handle-cache handle)
1291 (mm-extern-cache-contents handle))
1292 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1293 (buffer-string)))
1294 (mm-with-part handle
1295 (buffer-string))))
1297 (defun mm-insert-part (handle &optional no-cache)
1298 "Insert the contents of HANDLE in the current buffer.
1299 If NO-CACHE is non-nil, cached contents of a message/external-body part
1300 are ignored."
1301 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1302 'charset)
1303 'gnus-decoded)
1304 (with-current-buffer (mm-handle-buffer handle)
1305 (buffer-string)))
1306 ((mm-multibyte-p)
1307 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1309 (mm-get-part handle no-cache)))))
1310 (save-restriction
1311 (widen)
1312 (goto-char
1313 (prog1
1314 (point)
1315 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1316 'mm-uu-extract)
1317 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1318 ;; Separate the extracted parts that have the same faces.
1319 (insert "\n" text)
1320 (insert text)))))))
1322 (defun mm-file-name-delete-whitespace (file-name)
1323 "Remove all whitespace characters from FILE-NAME."
1324 (while (string-match "\\s-+" file-name)
1325 (setq file-name (replace-match "" t t file-name)))
1326 file-name)
1328 (defun mm-file-name-trim-whitespace (file-name)
1329 "Remove leading and trailing whitespace characters from FILE-NAME."
1330 (when (string-match "\\`\\s-+" file-name)
1331 (setq file-name (substring file-name (match-end 0))))
1332 (when (string-match "\\s-+\\'" file-name)
1333 (setq file-name (substring file-name 0 (match-beginning 0))))
1334 file-name)
1336 (defun mm-file-name-collapse-whitespace (file-name)
1337 "Collapse multiple whitespace characters in FILE-NAME."
1338 (while (string-match "\\s-\\s-+" file-name)
1339 (setq file-name (replace-match " " t t file-name)))
1340 file-name)
1342 (defun mm-file-name-replace-whitespace (file-name)
1343 "Replace whitespace characters in FILE-NAME with underscores.
1344 Set the option `mm-file-name-replace-whitespace' to any other
1345 string if you do not like underscores."
1346 (let ((s (or mm-file-name-replace-whitespace "_")))
1347 (while (string-match "\\s-" file-name)
1348 (setq file-name (replace-match s t t file-name))))
1349 file-name)
1351 (defun mm-file-name-delete-control (filename)
1352 "Delete control characters from FILENAME."
1353 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1355 (defun mm-file-name-delete-gotchas (filename)
1356 "Delete shell gotchas from FILENAME."
1357 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1358 (gnus-replace-in-string filename "^[.-]+" ""))
1360 (defun mm-save-part (handle &optional prompt)
1361 "Write HANDLE to a file.
1362 PROMPT overrides the default one used to ask user for a file name."
1363 (let ((filename (or (mail-content-type-get
1364 (mm-handle-disposition handle) 'filename)
1365 (mail-content-type-get
1366 (mm-handle-type handle) 'name)))
1367 file)
1368 (when filename
1369 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1370 (file-name-nondirectory filename))))
1371 (while
1372 (progn
1373 (setq file
1374 (read-file-name
1375 (or prompt
1376 (format "Save MIME part to (default %s): "
1377 (or filename "")))
1378 (or mm-default-directory default-directory)
1379 (expand-file-name (or filename "")
1380 (or mm-default-directory default-directory))))
1381 (cond ((or (not file) (equal file ""))
1382 (message "Please enter a file name")
1384 ((and (file-directory-p file)
1385 (not filename))
1386 (message "Please enter a non-directory file name")
1388 (t nil)))
1389 (sit-for 2)
1390 (discard-input))
1391 (if (file-directory-p file)
1392 (setq file (expand-file-name filename file))
1393 (setq file (expand-file-name
1394 file (or mm-default-directory default-directory))))
1395 (setq mm-default-directory (file-name-directory file))
1396 (and (or (not (file-exists-p file))
1397 (yes-or-no-p (format "File %s already exists; overwrite? "
1398 file)))
1399 (progn
1400 (mm-save-part-to-file handle file)
1401 file))))
1403 (defun mm-add-meta-html-tag (handle &optional charset force-charset)
1404 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1405 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1406 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1407 Return t if meta tag is added or replaced."
1408 (when (equal (mm-handle-media-type handle) "text/html")
1409 (when (or charset
1410 (setq charset (mail-content-type-get (mm-handle-type handle)
1411 'charset)))
1412 (setq charset (format "\
1413 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1414 (let ((case-fold-search t))
1415 (goto-char (point-min))
1416 (if (re-search-forward "\
1417 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1418 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t)
1419 (if (and (not force-charset)
1420 (match-beginning 2)
1421 (string-match "\\`html\\'" (match-string 1)))
1422 ;; Don't modify existing meta tag.
1424 ;; Replace it with the one specifying charset.
1425 (replace-match charset)
1427 (if (re-search-forward "<head>\\s-*" nil t)
1428 (insert charset "\n")
1429 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1430 (insert "<head>\n" charset "\n</head>\n"))
1431 t)))))
1433 (defun mm-save-part-to-file (handle file)
1434 (mm-with-unibyte-buffer
1435 (mm-insert-part handle)
1436 (mm-add-meta-html-tag handle)
1437 (let ((current-file-modes (default-file-modes)))
1438 (set-default-file-modes mm-attachment-file-modes)
1439 (unwind-protect
1440 ;; Don't re-compress .gz & al. Arguably we should make
1441 ;; `file-name-handler-alist' nil, but that would chop
1442 ;; ange-ftp, which is reasonable to use here.
1443 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1444 (set-default-file-modes current-file-modes)))))
1446 (defun mm-pipe-part (handle &optional cmd)
1447 "Pipe HANDLE to a process.
1448 Use CMD as the process."
1449 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1450 (command (or cmd
1451 (gnus-read-shell-command
1452 "Shell command on MIME part: " mm-last-shell-command))))
1453 (mm-with-unibyte-buffer
1454 (mm-insert-part handle)
1455 (mm-add-meta-html-tag handle)
1456 (let ((coding-system-for-write 'binary))
1457 (shell-command-on-region (point-min) (point-max) command nil)))))
1459 (autoload 'gnus-completing-read "gnus-util")
1461 (defun mm-interactively-view-part (handle)
1462 "Display HANDLE using METHOD."
1463 (let* ((type (mm-handle-media-type handle))
1464 (methods
1465 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
1466 (mailcap-mime-info type 'all)))
1467 (method (let ((minibuffer-local-completion-map
1468 mm-viewer-completion-map))
1469 (completing-read "Viewer: " methods))))
1470 (when (string= method "")
1471 (error "No method given"))
1472 (if (string-match "^[^% \t]+$" method)
1473 (setq method (concat method " %s")))
1474 (mm-display-external handle method)))
1476 (defun mm-preferred-alternative (handles &optional preferred)
1477 "Say which of HANDLES are preferred."
1478 (let ((prec (if preferred (list preferred)
1479 (mm-preferred-alternative-precedence handles)))
1480 p h result type handle)
1481 (while (setq p (pop prec))
1482 (setq h handles)
1483 (while h
1484 (setq handle (car h))
1485 (setq type (mm-handle-media-type handle))
1486 (when (and (equal p type)
1487 (mm-automatic-display-p handle)
1488 (or (stringp (car handle))
1489 (not (mm-handle-disposition handle))
1490 (equal (car (mm-handle-disposition handle))
1491 "inline")))
1492 (setq result handle
1493 h nil
1494 prec nil))
1495 (pop h)))
1496 result))
1498 (defun mm-preferred-alternative-precedence (handles)
1499 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1500 (setq handles (reverse handles))
1501 (dolist (disc (reverse mm-discouraged-alternatives))
1502 (dolist (handle (copy-sequence handles))
1503 (when (string-match disc (mm-handle-media-type handle))
1504 (setq handles (nconc (delete handle handles) (list handle))))))
1505 ;; Remove empty parts.
1506 (dolist (handle (copy-sequence handles))
1507 (when (and (bufferp (mm-handle-buffer handle))
1508 (not (with-current-buffer (mm-handle-buffer handle)
1509 (goto-char (point-min))
1510 (re-search-forward "[^ \t\n]" nil t))))
1511 (setq handles (nconc (delete handle handles) (list handle)))))
1512 (mapcar #'mm-handle-media-type handles))
1514 (defun mm-get-content-id (id)
1515 "Return the handle(s) referred to by ID."
1516 (cdr (assoc id mm-content-id-alist)))
1518 (defconst mm-image-type-regexps
1519 '(("/\\*.*XPM.\\*/" . xpm)
1520 ("P[1-6]" . pbm)
1521 ("GIF8" . gif)
1522 ("\377\330" . jpeg)
1523 ("\211PNG\r\n" . png)
1524 ("#define" . xbm)
1525 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1526 ("%!PS" . postscript))
1527 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1528 When the first bytes of an image file match REGEXP, it is assumed to
1529 be of image type IMAGE-TYPE.")
1531 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1532 (defun mm-image-type-from-buffer ()
1533 "Determine the image type from data in the current buffer.
1534 Value is a symbol specifying the image type or nil if type cannot
1535 be determined."
1536 (let ((types mm-image-type-regexps)
1537 type)
1538 (goto-char (point-min))
1539 (while (and types (null type))
1540 (let ((regexp (car (car types)))
1541 (image-type (cdr (car types))))
1542 (when (looking-at regexp)
1543 (setq type image-type))
1544 (setq types (cdr types))))
1545 type))
1547 (defun mm-get-image (handle)
1548 "Return an image instance based on HANDLE."
1549 (let ((type (mm-handle-media-subtype handle))
1550 spec)
1551 ;; Allow some common translations.
1552 (setq type
1553 (cond
1554 ((equal type "x-pixmap")
1555 "xpm")
1556 ((equal type "x-xbitmap")
1557 "xbm")
1558 ((equal type "x-portable-bitmap")
1559 "pbm")
1560 (t type)))
1561 (or (mm-handle-cache handle)
1562 (mm-with-unibyte-buffer
1563 (mm-insert-part handle)
1564 (prog1
1565 (setq spec
1566 (ignore-errors
1567 ;; Avoid testing `make-glyph' since W3 may define
1568 ;; a bogus version of it.
1569 (if (fboundp 'create-image)
1570 (create-image (buffer-string)
1571 (or (mm-image-type-from-buffer)
1572 (intern type))
1573 'data-p)
1574 (mm-create-image-xemacs type))))
1575 (mm-handle-set-cache handle spec))))))
1577 (defun mm-create-image-xemacs (type)
1578 (when (featurep 'xemacs)
1579 (cond
1580 ((equal type "xbm")
1581 ;; xbm images require special handling, since
1582 ;; the only way to create glyphs from these
1583 ;; (without a ton of work) is to write them
1584 ;; out to a file, and then create a file
1585 ;; specifier.
1586 (let ((file (mm-make-temp-file
1587 (expand-file-name "emm" mm-tmp-directory)
1588 nil ".xbm")))
1589 (unwind-protect
1590 (progn
1591 (write-region (point-min) (point-max) file)
1592 (make-glyph (list (cons 'x file))))
1593 (ignore-errors
1594 (delete-file file)))))
1596 (make-glyph
1597 (vector
1598 (or (mm-image-type-from-buffer)
1599 (intern type))
1600 :data (buffer-string)))))))
1602 (declare-function image-size "image.c" (spec &optional pixels frame))
1604 (defun mm-image-fit-p (handle)
1605 "Say whether the image in HANDLE will fit the current window."
1606 (let ((image (mm-get-image handle)))
1607 (or (not image)
1608 (if (featurep 'xemacs)
1609 ;; XEmacs's glyphs can actually tell us about their width, so
1610 ;; let's be nice and smart about them.
1611 (or mm-inline-large-images
1612 (and (<= (glyph-width image) (window-pixel-width))
1613 (<= (glyph-height image) (window-pixel-height))))
1614 (let* ((size (image-size image))
1615 (w (car size))
1616 (h (cdr size)))
1617 (or mm-inline-large-images
1618 (and (<= h (1- (window-height))) ; Don't include mode line.
1619 (<= w (window-width)))))))))
1621 (defun mm-valid-image-format-p (format)
1622 "Say whether FORMAT can be displayed natively by Emacs."
1623 (cond
1624 ;; Handle XEmacs
1625 ((fboundp 'valid-image-instantiator-format-p)
1626 (valid-image-instantiator-format-p format))
1627 ;; Handle Emacs
1628 ((fboundp 'image-type-available-p)
1629 (and (display-graphic-p)
1630 (image-type-available-p format)))
1631 ;; Nobody else can do images yet.
1633 nil)))
1635 (defun mm-valid-and-fit-image-p (format handle)
1636 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1637 (and (mm-valid-image-format-p format)
1638 (mm-image-fit-p handle)))
1640 (defun mm-find-part-by-type (handles type &optional notp recursive)
1641 "Search in HANDLES for part with TYPE.
1642 If NOTP, returns first non-matching part.
1643 If RECURSIVE, search recursively."
1644 (let (handle)
1645 (while handles
1646 (if (and recursive (stringp (caar handles)))
1647 (if (setq handle (mm-find-part-by-type (cdar handles) type
1648 notp recursive))
1649 (setq handles nil))
1650 (if (if notp
1651 (not (equal (mm-handle-media-type (car handles)) type))
1652 (equal (mm-handle-media-type (car handles)) type))
1653 (setq handle (car handles)
1654 handles nil)))
1655 (setq handles (cdr handles)))
1656 handle))
1658 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1659 (goto-char (point-min))
1660 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1661 'boundary)))
1662 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1663 start
1664 (end (save-excursion
1665 (goto-char (point-max))
1666 (if (re-search-backward close-delimiter nil t)
1667 (match-beginning 0)
1668 (point-max))))
1669 result)
1670 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1671 (while (and (not result)
1672 (re-search-forward boundary end t))
1673 (goto-char (match-beginning 0))
1674 (when start
1675 (save-excursion
1676 (save-restriction
1677 (narrow-to-region start (1- (point)))
1678 (when (let* ((ct (mail-fetch-field "content-type"))
1679 (ctl (and ct (mail-header-parse-content-type ct))))
1680 (if notp
1681 (not (equal (car ctl) type))
1682 (equal (car ctl) type)))
1683 (setq result (buffer-string))))))
1684 (forward-line 1)
1685 (setq start (point)))
1686 (when (and (not result) start)
1687 (save-excursion
1688 (save-restriction
1689 (narrow-to-region start end)
1690 (when (let* ((ct (mail-fetch-field "content-type"))
1691 (ctl (and ct (mail-header-parse-content-type ct))))
1692 (if notp
1693 (not (equal (car ctl) type))
1694 (equal (car ctl) type)))
1695 (setq result (buffer-string))))))
1696 result))
1698 (defvar mm-security-handle nil)
1700 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1701 ;; HANDLE could be a CTL.
1702 (when handle
1703 (put-text-property 0 (length (car handle)) parameter value
1704 (car handle))))
1706 (autoload 'mm-view-pkcs7 "mm-view")
1708 (defun mm-possibly-verify-or-decrypt (parts ctl &optional from)
1709 (let ((type (car ctl))
1710 (subtype (cadr (split-string (car ctl) "/")))
1711 (mm-security-handle ctl) ;; (car CTL) is the type.
1712 protocol func functest)
1713 (cond
1714 ((or (equal type "application/x-pkcs7-mime")
1715 (equal type "application/pkcs7-mime"))
1716 (with-temp-buffer
1717 (when (and (cond
1718 ((eq mm-decrypt-option 'never) nil)
1719 ((eq mm-decrypt-option 'always) t)
1720 ((eq mm-decrypt-option 'known) t)
1721 (t (y-or-n-p
1722 (format "Decrypt (S/MIME) part? "))))
1723 (mm-view-pkcs7 parts from))
1724 (setq parts (mm-dissect-buffer t)))))
1725 ((equal subtype "signed")
1726 (unless (and (setq protocol
1727 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1728 (not (equal protocol "multipart/mixed")))
1729 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1730 (let ((protocols mm-verify-function-alist))
1731 (while protocols
1732 (if (and (or (not (setq functest (nth 3 (car protocols))))
1733 (funcall functest parts ctl))
1734 (mm-find-part-by-type parts (caar protocols) nil t))
1735 (setq protocol (caar protocols)
1736 protocols nil)
1737 (setq protocols (cdr protocols))))))
1738 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1739 (when (cond
1740 ((eq mm-verify-option 'never) nil)
1741 ((eq mm-verify-option 'always) t)
1742 ((eq mm-verify-option 'known)
1743 (and func
1744 (or (not (setq functest
1745 (nth 3 (assoc protocol
1746 mm-verify-function-alist))))
1747 (funcall functest parts ctl))))
1749 (y-or-n-p
1750 (format "Verify signed (%s) part? "
1751 (or (nth 2 (assoc protocol mm-verify-function-alist))
1752 (format "protocol=%s" protocol))))))
1753 (save-excursion
1754 (if func
1755 (setq parts (funcall func parts ctl))
1756 (mm-set-handle-multipart-parameter
1757 mm-security-handle 'gnus-details
1758 (format "Unknown sign protocol (%s)" protocol))))))
1759 ((equal subtype "encrypted")
1760 (unless (setq protocol
1761 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1762 ;; The message is broken.
1763 (let ((parts parts))
1764 (while parts
1765 (if (assoc (mm-handle-media-type (car parts))
1766 mm-decrypt-function-alist)
1767 (setq protocol (mm-handle-media-type (car parts))
1768 parts nil)
1769 (setq parts (cdr parts))))))
1770 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1771 (when (cond
1772 ((eq mm-decrypt-option 'never) nil)
1773 ((eq mm-decrypt-option 'always) t)
1774 ((eq mm-decrypt-option 'known)
1775 (and func
1776 (or (not (setq functest
1777 (nth 3 (assoc protocol
1778 mm-decrypt-function-alist))))
1779 (funcall functest parts ctl))))
1781 (y-or-n-p
1782 (format "Decrypt (%s) part? "
1783 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1784 (format "protocol=%s" protocol))))))
1785 (save-excursion
1786 (if func
1787 (setq parts (funcall func parts ctl))
1788 (mm-set-handle-multipart-parameter
1789 mm-security-handle 'gnus-details
1790 (format "Unknown encrypt protocol (%s)" protocol))))))
1791 (t nil))
1792 parts))
1794 (defun mm-multiple-handles (handles)
1795 (and (listp handles)
1796 (> (length handles) 1)
1797 (or (listp (car handles))
1798 (stringp (car handles)))))
1800 (defun mm-complicated-handles (handles)
1801 (and (listp (car handles))
1802 (> (length handles) 1)))
1804 (defun mm-merge-handles (handles1 handles2)
1805 (append
1806 (if (listp (car handles1))
1807 handles1
1808 (list handles1))
1809 (if (listp (car handles2))
1810 handles2
1811 (list handles2))))
1813 (defun mm-readable-p (handle)
1814 "Say whether the content of HANDLE is readable."
1815 (and (< (with-current-buffer (mm-handle-buffer handle)
1816 (buffer-size)) 10000)
1817 (mm-with-unibyte-buffer
1818 (mm-insert-part handle)
1819 (and (eq (mm-body-7-or-8) '7bit)
1820 (not (mm-long-lines-p 76))))))
1822 (declare-function libxml-parse-html-region "xml.c"
1823 (start end &optional base-url))
1824 (declare-function shr-insert-document "shr" (dom))
1825 (defvar shr-blocked-images)
1826 (defvar gnus-inhibit-images)
1827 (autoload 'gnus-blocked-images "gnus-art")
1829 (defun mm-shr (handle)
1830 ;; Require since we bind its variables.
1831 (require 'shr)
1832 (let ((article-buffer (current-buffer))
1833 (shr-content-function (lambda (id)
1834 (let ((handle (mm-get-content-id id)))
1835 (when handle
1836 (mm-with-part handle
1837 (buffer-string))))))
1838 shr-inhibit-images shr-blocked-images charset char)
1839 (if (and (boundp 'gnus-summary-buffer)
1840 (bufferp gnus-summary-buffer)
1841 (buffer-name gnus-summary-buffer))
1842 (with-current-buffer gnus-summary-buffer
1843 (setq shr-inhibit-images gnus-inhibit-images
1844 shr-blocked-images (gnus-blocked-images)))
1845 (setq shr-inhibit-images gnus-inhibit-images
1846 shr-blocked-images (gnus-blocked-images)))
1847 (unless handle
1848 (setq handle (mm-dissect-buffer t)))
1849 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
1850 (save-restriction
1851 (narrow-to-region (point) (point))
1852 (shr-insert-document
1853 (mm-with-part handle
1854 (insert (prog1
1855 (if (and charset
1856 (setq charset
1857 (mm-charset-to-coding-system charset
1858 nil t))
1859 (not (eq charset 'ascii)))
1860 (mm-decode-coding-string (buffer-string) charset)
1861 (mm-string-as-multibyte (buffer-string)))
1862 (erase-buffer)
1863 (mm-enable-multibyte)))
1864 (goto-char (point-min))
1865 (setq case-fold-search t)
1866 (while (re-search-forward
1867 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t)
1868 (when (setq char
1869 (cdr (assq (if (match-beginning 1)
1870 (string-to-number (match-string 1) 16)
1871 (string-to-number (match-string 2)))
1872 mm-extra-numeric-entities)))
1873 (replace-match (char-to-string char))))
1874 ;; Remove "soft hyphens".
1875 (goto-char (point-min))
1876 (while (search-forward "­" nil t)
1877 (replace-match "" t t))
1878 (libxml-parse-html-region (point-min) (point-max))))
1879 (unless (bobp)
1880 (insert "\n"))
1881 (mm-convert-shr-links)
1882 (mm-handle-set-undisplayer
1883 handle
1884 `(lambda ()
1885 (let ((inhibit-read-only t))
1886 (delete-region ,(point-min-marker)
1887 ,(point-max-marker))))))))
1889 (defvar shr-map)
1891 (autoload 'widget-convert-button "wid-edit")
1893 (defun mm-convert-shr-links ()
1894 (let ((start (point-min))
1895 end)
1896 (while (and start
1897 (< start (point-max)))
1898 (when (setq start (text-property-not-all start (point-max) 'shr-url nil))
1899 (setq end (next-single-property-change start 'shr-url nil (point-max)))
1900 (widget-convert-button
1901 'url-link start end
1902 :help-echo (get-text-property start 'help-echo)
1903 :keymap shr-map
1904 (get-text-property start 'shr-url))
1905 (put-text-property start end 'local-map nil)
1906 (setq start end)))))
1908 (defun mm-handle-filename (handle)
1909 "Return filename of HANDLE if any."
1910 (or (mail-content-type-get (mm-handle-type handle)
1911 'name)
1912 (mail-content-type-get (mm-handle-disposition handle)
1913 'filename)))
1915 (provide 'mm-decode)
1917 ;; Local Variables:
1918 ;; coding: utf-8
1919 ;; End:
1921 ;;; mm-decode.el ends here