1 ;;; rmailmm.el --- MIME decoding and display stuff for RMAIL
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;; Author: Alexander Pohoyda
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Essentially based on the design of Alexander Pohoyda's MIME
28 ;; extensions (mime-display.el and mime.el).
30 ;; This file provides two operation modes for viewing a MIME message.
32 ;; (1) When rmail-enable-mime is non-nil (now it is the default), the
33 ;; function `rmail-show-mime' is automatically called. That function
34 ;; shows a MIME message directly in RMAIL's view buffer.
36 ;; (2) When rmail-enable-mime is nil, the command 'v' (or M-x
37 ;; rmail-mime) shows a MIME message in a new buffer "*RMAIL*".
39 ;; Both operations share the intermediate functions rmail-mime-process
40 ;; and rmail-mime-process-multipart as below.
43 ;; +- rmail-mime-parse
44 ;; | +- rmail-mime-process <--+------------+
46 ;; | + rmail-mime-process-multipart --+
48 ;; + rmail-mime-insert <----------------+
49 ;; +- rmail-mime-insert-text |
50 ;; +- rmail-mime-insert-bulk |
51 ;; +- rmail-mime-insert-multipart --+
54 ;; +- rmail-mime-show <----------------------------------+
55 ;; +- rmail-mime-process |
56 ;; +- rmail-mime-handle |
57 ;; +- rmail-mime-text-handler |
58 ;; +- rmail-mime-bulk-handler |
59 ;; | + rmail-mime-insert-bulk
60 ;; +- rmail-mime-multipart-handler |
61 ;; +- rmail-mime-process-multipart --+
63 ;; In addition, for the case of rmail-enable-mime being non-nil, this
64 ;; file provides two functions rmail-insert-mime-forwarded-message and
65 ;; rmail-insert-mime-resent-message for composing forwarded and resent
66 ;; messages respectively.
70 ;; Make rmail-mime-media-type-handlers-alist usable in the first
72 ;; Handle multipart/alternative in the second operation mode.
73 ;; Offer the option to call external/internal viewers (doc-view, xpdf, etc).
83 (defgroup rmail-mime nil
84 "Rmail MIME handling options."
88 (defcustom rmail-mime-media-type-handlers-alist
89 '(("multipart/.*" rmail-mime-multipart-handler)
90 ("text/.*" rmail-mime-text-handler)
91 ("text/\\(x-\\)?patch" rmail-mime-bulk-handler)
92 ("\\(image\\|audio\\|video\\|application\\)/.*" rmail-mime-bulk-handler))
93 "Functions to handle various content types.
94 This is an alist with elements of the form (REGEXP FUNCTION ...).
95 The first item is a regular expression matching a content-type.
96 The remaining elements are handler functions to run, in order of
97 decreasing preference. These are called until one returns non-nil.
98 Note that this only applies to items with an inline Content-Disposition,
99 all others are handled by `rmail-mime-bulk-handler'.
100 Note also that this alist is ignored when the variable
101 `rmail-enable-mime' is non-nil."
102 :type '(alist :key-type regexp :value-type (repeat function))
106 (defcustom rmail-mime-attachment-dirs-alist
107 `(("text/.*" "~/Documents")
108 ("image/.*" "~/Pictures")
109 (".*" "~/Desktop" "~" ,temporary-file-directory))
110 "Default directories to save attachments of various types into.
111 This is an alist with elements of the form (REGEXP DIR ...).
112 The first item is a regular expression matching a content-type.
113 The remaining elements are directories, in order of decreasing preference.
114 The first directory that exists is used."
115 :type '(alist :key-type regexp :value-type (repeat directory))
119 (defcustom rmail-mime-show-images 'button
120 "What to do with image attachments that Emacs is capable of displaying.
121 If nil, do nothing special. If `button', add an extra button
122 that when pushed displays the image in the buffer. If a number,
123 automatically show images if they are smaller than that size (in
124 bytes), otherwise add a display button. Anything else means to
125 automatically display the image in the buffer."
126 :type '(choice (const :tag "Add button to view image" button)
127 (const :tag "No special treatment" nil)
128 (number :tag "Show if smaller than certain size")
129 (other :tag "Always show" show))
133 ;;; End of user options.
135 ;;; Global variables that always have let-binding when referred.
137 (defvar rmail-mime-mbox-buffer nil
138 "Buffer containing the mbox data.
139 The value is usually nil, and bound to a proper value while
142 (defvar rmail-mime-view-buffer nil
143 "Buffer showing a message.
144 The value is usually nil, and bound to a proper value while
147 (defvar rmail-mime-coding-system nil
148 "The first coding-system used for decoding a MIME entity.
149 The value is usually nil, and bound to non-nil while inserting
152 ;;; MIME-entity object
154 (defun rmail-mime-entity (type disposition transfer-encoding
155 display header tagline body children handler)
156 "Retrun a newly created MIME-entity object from arguments.
158 A MIME-entity is a vector of 9 elements:
160 [TYPE DISPOSITION TRANSFER-ENCODING DISPLAY HEADER TAGLINE BODY
163 TYPE and DISPOSITION correspond to MIME headers Content-Type and
164 Cotent-Disposition respectively, and has this format:
166 \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
168 VALUE is a string and ATTRIBUTE is a symbol.
170 Consider the following header, for example:
172 Content-Type: multipart/mixed;
173 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
175 The corresponding TYPE argument must be:
177 \(\"multipart/mixed\"
178 \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))
180 TRANSFER-ENCODING corresponds to MIME header
181 Content-Transfer-Encoding, and is a lowercased string.
183 DISPLAY is a vector [CURRENT NEW], where CURRENT indicates how
184 the header, tagline, and body of the entity are displayed now,
185 and NEW indicates how their displaying should be updated.
186 Both elements are vector [HEADER-DISPLAY TAGLINE-DISPLAY BODY-DISPLAY],
187 where each element is a symbol for the corresponding item that
190 t: displayed by the decoded presentation form
191 raw: displayed by the raw MIME data (for the header and body only)
193 HEADER and BODY are vectors [BEG END DISPLAY-FLAG], where BEG and
194 END specify the region of the header or body lines in RMAIL's
195 data (mbox) buffer, and DISPLAY-FLAG non-nil means that the
196 header or body is, by default, displayed by the decoded
199 TAGLINE is a vector [TAG BULK-DATA DISPLAY-FLAG], where TAG is a
200 string indicating the depth and index number of the entity,
201 BULK-DATA is a cons (SIZE . TYPE) indicating the size and type of
202 an attached data, DISPLAY-FLAG non-nil means that the tagline is,
203 by default, displayed.
205 CHILDREN is a list of child MIME-entities. A \"multipart/*\"
206 entity have one or more children. A \"message/rfc822\" entity
207 has just one child. Any other entity has no child.
209 HANDLER is a function to insert the entity according to DISPLAY.
210 It is called with one argument ENTITY."
211 (vector type disposition transfer-encoding
212 display header tagline body children handler))
214 ;; Accessors for a MIME-entity object.
215 (defsubst rmail-mime-entity-type (entity) (aref entity 0))
216 (defsubst rmail-mime-entity-disposition (entity) (aref entity 1))
217 (defsubst rmail-mime-entity-transfer-encoding (entity) (aref entity 2))
218 (defsubst rmail-mime-entity-display (entity) (aref entity 3))
219 (defsubst rmail-mime-entity-header (entity) (aref entity 4))
220 (defsubst rmail-mime-entity-tagline (entity) (aref entity 5))
221 (defsubst rmail-mime-entity-body (entity) (aref entity 6))
222 (defsubst rmail-mime-entity-children (entity) (aref entity 7))
223 (defsubst rmail-mime-entity-handler (entity) (aref entity 8))
225 (defsubst rmail-mime-message-p ()
226 "Non-nil if and only if the current message is a MIME."
227 (or (get-text-property (point) 'rmail-mime-entity)
228 (get-text-property (point-min) 'rmail-mime-entity)))
232 (defun rmail-mime-save (button)
233 "Save the attachment using info in the BUTTON."
234 (let* ((rmail-mime-mbox-buffer rmail-view-buffer)
235 (filename (button-get button 'filename))
236 (directory (button-get button 'directory))
237 (data (button-get button 'data))
238 (ofilename filename))
239 (setq filename (expand-file-name
240 (read-file-name (format "Save as (default: %s): " filename)
242 (expand-file-name filename directory))
244 ;; If arg is just a directory, use the default file name, but in
245 ;; that directory (copied from write-file).
246 (if (file-directory-p filename)
247 (setq filename (expand-file-name
248 (file-name-nondirectory ofilename)
249 (file-name-as-directory filename))))
251 (set-buffer-file-coding-system 'no-conversion)
252 ;; Needed e.g. by jka-compr, so if the attachment is a compressed
253 ;; file, the magic signature compares equal with the unibyte
254 ;; signature string recorded in jka-compr-compression-info-list.
255 (set-buffer-multibyte nil)
256 (setq buffer-undo-list t)
259 ;; DATA is a MIME-entity object.
260 (let ((transfer-encoding (rmail-mime-entity-transfer-encoding data))
261 (body (rmail-mime-entity-body data)))
262 (insert-buffer-substring rmail-mime-mbox-buffer
263 (aref body 0) (aref body 1))
264 (cond ((string= transfer-encoding "base64")
265 (ignore-errors (base64-decode-region (point-min) (point-max))))
266 ((string= transfer-encoding "quoted-printable")
267 (quoted-printable-decode-region (point-min) (point-max))))))
268 (write-region nil nil filename nil nil nil t))))
270 (define-button-type 'rmail-mime-save 'action 'rmail-mime-save)
272 (defun rmail-mime-entity-segment (pos &optional entity)
273 "Return a vector describing the displayed region of a MIME-entity at POS.
274 Optional 2nd argument ENTITY is the MIME-entity at POS.
275 The value is a vector [ INDEX HEADER TAGLINE BODY END], where
276 INDEX: index into the returned vector indicating where POS is (1..3).
277 HEADER: the position of the beginning of a header
278 TAGLINE: the position of the beginning of a tagline
279 BODY: the position of the beginning of a body
280 END: the position of the end of the entity."
283 (setq entity (get-text-property pos 'rmail-mime-entity)))
285 (vector 1 (point) (point) (point) (point))
286 (let ((current (aref (rmail-mime-entity-display entity) 0))
287 (beg (if (and (> pos (point-min))
288 (eq (get-text-property (1- pos) 'rmail-mime-entity)
290 (previous-single-property-change pos 'rmail-mime-entity
294 tagline-beg body-beg end)
297 (search-forward "\n\n" nil t))
298 (setq tagline-beg (point))
299 (if (>= pos tagline-beg)
303 (setq body-beg (point))
304 (if (>= pos body-beg)
307 (let ((tag (aref (rmail-mime-entity-tagline entity) 0))
309 (setq end (next-single-property-change beg 'rmail-mime-entity
311 (while (and (< end (point-max))
312 (setq entity (get-text-property end 'rmail-mime-entity)
313 tag2 (aref (rmail-mime-entity-tagline entity) 0))
314 (and (> (length tag2) 0)
315 (eq (string-match tag tag2) 0)))
316 (setq end (next-single-property-change end 'rmail-mime-entity
319 (vector index beg tagline-beg body-beg end)))))
321 (defun rmail-mime-shown-mode (entity)
322 "Make MIME-entity ENTITY displayed by the default way."
323 (let ((new (aref (rmail-mime-entity-display entity) 1)))
324 (aset new 0 (aref (rmail-mime-entity-header entity) 2))
325 (aset new 1 (aref (rmail-mime-entity-tagline entity) 2))
326 (aset new 2 (aref (rmail-mime-entity-body entity) 2)))
327 (dolist (child (rmail-mime-entity-children entity))
328 (rmail-mime-shown-mode child)))
330 (defun rmail-mime-hidden-mode (entity)
331 "Make MIME-entity ENTITY displayed in the hidden mode."
332 (let ((new (aref (rmail-mime-entity-display entity) 1)))
336 (dolist (child (rmail-mime-entity-children entity))
337 (rmail-mime-hidden-mode child)))
339 (defun rmail-mime-raw-mode (entity)
340 "Make MIME-entity ENTITY displayed in the raw mode."
341 (let ((new (aref (rmail-mime-entity-display entity) 1)))
345 (dolist (child (rmail-mime-entity-children entity))
346 (rmail-mime-raw-mode child)))
348 (defun rmail-mime-toggle-raw (entity)
349 "Toggle on and off the raw display mode of MIME-entity ENTITY."
350 (let* ((pos (if (eobp) (1- (point-max)) (point)))
351 (entity (get-text-property pos 'rmail-mime-entity))
352 (current (aref (rmail-mime-entity-display entity) 0))
353 (segment (rmail-mime-entity-segment pos entity)))
354 (if (not (eq (aref current 0) 'raw))
355 ;; Enter the raw mode.
356 (rmail-mime-raw-mode entity)
357 ;; Enter the shown mode.
358 (rmail-mime-shown-mode entity))
359 (let ((inhibit-read-only t)
360 (modified (buffer-modified-p)))
362 (goto-char (aref segment 1))
363 (rmail-mime-insert entity)
364 (restore-buffer-modified-p modified)))))
366 (defun rmail-mime-toggle-hidden ()
367 "Hide or show the body of MIME-entity at point."
369 (when (rmail-mime-message-p)
370 (let* ((rmail-mime-mbox-buffer rmail-view-buffer)
371 (rmail-mime-view-buffer (current-buffer))
372 (pos (if (eobp) (1- (point-max)) (point)))
373 (entity (get-text-property pos 'rmail-mime-entity))
374 (current (aref (rmail-mime-entity-display entity) 0))
375 (segment (rmail-mime-entity-segment pos entity)))
377 ;; Enter the hidden mode.
379 ;; If point is in the body part, move it to the tagline
380 ;; (or the header if tagline is not displayed).
381 (if (= (aref segment 0) 3)
382 (goto-char (aref segment 2)))
383 (rmail-mime-hidden-mode entity)
384 ;; If the current entity is the topmost one, display the
386 (if (and rmail-mime-mbox-buffer (= (aref segment 1) (point-min)))
387 (let ((new (aref (rmail-mime-entity-display entity) 1)))
389 ;; Enter the shown mode.
390 (rmail-mime-shown-mode entity)
391 ;; Force this body shown.
392 (aset (aref (rmail-mime-entity-display entity) 1) 2 t))
393 (let ((inhibit-read-only t)
394 (modified (buffer-modified-p))
395 (rmail-mime-mbox-buffer rmail-view-buffer)
396 (rmail-mime-view-buffer rmail-buffer))
398 (goto-char (aref segment 1))
399 (rmail-mime-insert entity)
400 (restore-buffer-modified-p modified))))))
402 (define-key rmail-mode-map "\t" 'forward-button)
403 (define-key rmail-mode-map [backtab] 'backward-button)
404 (define-key rmail-mode-map "\r" 'rmail-mime-toggle-hidden)
408 (defun rmail-mime-insert-tagline (entity &rest item-list)
409 "Insert a tag line for MIME-entity ENTITY.
410 ITEM-LIST is a list of strings or button-elements (list) to be added
413 (let ((tag (aref (rmail-mime-entity-tagline entity) 0)))
414 (if (> (length tag) 0) (insert (substring tag 1) ":")))
415 (insert (car (rmail-mime-entity-type entity)) " ")
416 (insert-button (let ((new (aref (rmail-mime-entity-display entity) 1)))
417 (if (aref new 2) "Hide" "Show"))
418 :type 'rmail-mime-toggle
419 'help-echo "mouse-2, RET: Toggle show/hide")
420 (dolist (item item-list)
424 (apply 'insert-button item))))
427 (defun rmail-mime-update-tagline (entity)
428 "Update the current tag line for MIME-entity ENTITY."
429 (let ((inhibit-read-only t)
430 (modified (buffer-modified-p))
431 ;; If we are going to show the body, the new button label is
432 ;; "Hide". Otherwise, it's "Show".
433 (label (if (aref (aref (rmail-mime-entity-display entity) 1) 2) "Hide"
435 (button (next-button (point))))
436 ;; Go to the second character of the button "Show" or "Hide".
437 (goto-char (1+ (button-start button)))
438 (setq button (button-at (point)))
441 (delete-region (point) (button-end button)))
442 (delete-region (button-start button) (point))
443 (put-text-property (point) (button-end button) 'rmail-mime-entity entity)
444 (restore-buffer-modified-p modified)
447 (defun rmail-mime-insert-header (header)
448 "Decode and insert a MIME-entity header HEADER in the current buffer.
449 HEADER is a vector [BEG END DEFAULT-STATUS].
450 See `rmail-mime-entity' for the detail."
452 (last-coding-system-used nil))
454 (narrow-to-region pos pos)
455 (with-current-buffer rmail-mime-mbox-buffer
456 (let ((rmail-buffer rmail-mime-mbox-buffer)
457 (rmail-view-buffer rmail-mime-view-buffer))
459 (goto-char (aref header 0))
460 (rmail-copy-headers (point) (aref header 1)))))
461 (rfc2047-decode-region pos (point))
462 (if (and last-coding-system-used (not rmail-mime-coding-system))
463 (setq rmail-mime-coding-system (cons last-coding-system-used nil)))
464 (goto-char (point-min))
465 (rmail-highlight-headers)
466 (goto-char (point-max))
469 (defun rmail-mime-find-header-encoding (header)
470 "Retun the last coding system used to decode HEADER.
471 HEADER is a header component of a MIME-entity object (see
472 `rmail-mime-entity')."
474 (let ((last-coding-system-used nil))
475 (with-current-buffer rmail-mime-mbox-buffer
476 (let ((rmail-buffer rmail-mime-mbox-buffer)
477 (rmail-view-buffer rmail-mime-view-buffer))
479 (goto-char (aref header 0))
480 (rmail-copy-headers (point) (aref header 1)))))
481 (rfc2047-decode-region (point-min) (point-max))
482 last-coding-system-used)))
484 (defun rmail-mime-text-handler (content-type
486 content-transfer-encoding)
487 "Handle the current buffer as a plain text MIME part."
488 (rmail-mime-insert-text
489 (rmail-mime-entity content-type content-disposition
490 content-transfer-encoding
491 (vector (vector nil nil nil) (vector nil nil t))
492 (vector nil nil nil) (vector "" (cons nil nil) t)
493 (vector nil nil nil) nil 'rmail-mime-insert-text))
496 (defun rmail-mime-insert-decoded-text (entity)
497 "Decode and insert the text body of MIME-entity ENTITY."
498 (let* ((content-type (rmail-mime-entity-type entity))
499 (charset (cdr (assq 'charset (cdr content-type))))
500 (coding-system (if charset
501 (coding-system-from-name charset)))
502 (body (rmail-mime-entity-body entity))
504 (or (and coding-system (coding-system-p coding-system))
505 (setq coding-system 'undecided))
506 (if (stringp (aref body 0))
507 (insert (aref body 0))
508 (let ((transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
509 (insert-buffer-substring rmail-mime-mbox-buffer
510 (aref body 0) (aref body 1))
511 (cond ((string= transfer-encoding "base64")
512 (ignore-errors (base64-decode-region pos (point))))
513 ((string= transfer-encoding "quoted-printable")
514 (quoted-printable-decode-region pos (point))))))
515 (decode-coding-region pos (point) coding-system)
516 (if (or (not rmail-mime-coding-system) (consp rmail-mime-coding-system))
517 (setq rmail-mime-coding-system coding-system))
518 (or (bolp) (insert "\n"))))
520 (defun rmail-mime-insert-text (entity)
521 "Presentation handler for a plain text MIME entity."
522 (let ((current (aref (rmail-mime-entity-display entity) 0))
523 (new (aref (rmail-mime-entity-display entity) 1))
524 (header (rmail-mime-entity-header entity))
525 (tagline (rmail-mime-entity-tagline entity))
526 (body (rmail-mime-entity-body entity))
528 (segment (rmail-mime-entity-segment (point) entity)))
530 (or (integerp (aref body 0))
531 (let ((data (buffer-string)))
533 (delete-region (point-min) (point-max))))
536 (if (eq (aref current 0) (aref new 0))
537 (goto-char (aref segment 2))
539 (delete-char (- (aref segment 2) (aref segment 1))))
541 (rmail-mime-insert-header header)))
543 (if (eq (aref current 1) (aref new 1))
544 (if (or (not (aref current 1))
545 (eq (aref current 2) (aref new 2)))
546 (forward-char (- (aref segment 3) (aref segment 2)))
547 (rmail-mime-update-tagline entity))
549 (delete-char (- (aref segment 3) (aref segment 2))))
551 (rmail-mime-insert-tagline entity)))
553 (if (eq (aref current 2) (aref new 2))
554 (forward-char (- (aref segment 4) (aref segment 3)))
556 (delete-char (- (aref segment 4) (aref segment 3))))
558 (rmail-mime-insert-decoded-text entity)))
559 (put-text-property beg (point) 'rmail-mime-entity entity)))
561 ;; FIXME move to the test/ directory?
562 (defun test-rmail-mime-handler ()
563 "Test of a mail using no MIME parts at all."
564 (let ((mail "To: alex@gnu.org
565 Content-Type: text/plain; charset=koi8-r
566 Content-Transfer-Encoding: 8bit
569 \372\304\322\301\327\323\324\327\325\312\324\305\41"))
570 (switch-to-buffer (get-buffer-create "*test*"))
572 (set-buffer-multibyte nil)
575 (set-buffer-multibyte t)))
578 (defun rmail-mime-insert-image (entity)
579 "Decode and insert the image body of MIME-entity ENTITY."
580 (let* ((content-type (car (rmail-mime-entity-type entity)))
581 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
582 (body (rmail-mime-entity-body entity))
584 (if (stringp (aref body 0))
585 (setq data (aref body 0))
586 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
587 (transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
589 (set-buffer-multibyte nil)
590 (setq buffer-undo-list t)
591 (insert-buffer-substring rmail-mime-mbox-buffer
592 (aref body 0) (aref body 1))
593 (cond ((string= transfer-encoding "base64")
594 (ignore-errors (base64-decode-region (point-min) (point-max))))
595 ((string= transfer-encoding "quoted-printable")
596 (quoted-printable-decode-region (point-min) (point-max))))
598 (buffer-substring-no-properties (point-min) (point-max))))))
599 (insert-image (create-image data (cdr bulk-data) t))
602 (defun rmail-mime-toggle-button (button)
603 "Hide or show the body of the MIME-entity associated with BUTTON."
605 (goto-char (button-start button))
606 (rmail-mime-toggle-hidden)))
608 (define-button-type 'rmail-mime-toggle 'action 'rmail-mime-toggle-button)
611 (defun rmail-mime-bulk-handler (content-type
613 content-transfer-encoding)
614 "Handle the current buffer as an attachment to download.
615 For images that Emacs is capable of displaying, the behavior
616 depends upon the value of `rmail-mime-show-images'."
617 (rmail-mime-insert-bulk
618 (rmail-mime-entity content-type content-disposition content-transfer-encoding
619 (vector (vector nil nil nil) (vector nil t nil))
620 (vector nil nil nil) (vector "" (cons nil nil) t)
621 (vector nil nil nil) nil 'rmail-mime-insert-bulk)))
623 (defun rmail-mime-set-bulk-data (entity)
624 "Setup the information about the attachment object for MIME-entity ENTITY.
625 The value is non-nil if and only if the attachment object should be shown
627 (let ((content-type (car (rmail-mime-entity-type entity)))
628 (size (cdr (assq 'size (cdr (rmail-mime-entity-disposition entity)))))
629 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
630 (body (rmail-mime-entity-body entity))
633 (setq size (string-to-number size)))
634 ((stringp (aref body 0))
635 (setq size (length (aref body 0))))
637 ;; Rough estimation of the size.
638 (let ((encoding (rmail-mime-entity-transfer-encoding entity)))
639 (setq size (- (aref body 1) (aref body 0)))
640 (cond ((string= encoding "base64")
641 (setq size (/ (* size 3) 4)))
642 ((string= encoding "quoted-printable")
643 (setq size (/ (* size 7) 3)))))))
646 ((string-match "text/" content-type)
648 ((string-match "image/\\(.*\\)" content-type)
649 (setq type (image-type-from-file-name
650 (concat "." (match-string 1 content-type))))
651 (if (and (memq type image-types)
652 (image-type-available-p type))
653 (if (and rmail-mime-show-images
654 (not (eq rmail-mime-show-images 'button))
655 (or (not (numberp rmail-mime-show-images))
656 (< size rmail-mime-show-images)))
659 (setcar bulk-data size)
660 (setcdr bulk-data type)
663 (defun rmail-mime-insert-bulk (entity)
664 "Presentation handler for an attachment MIME entity."
665 (let* ((content-type (rmail-mime-entity-type entity))
666 (content-disposition (rmail-mime-entity-disposition entity))
667 (current (aref (rmail-mime-entity-display entity) 0))
668 (new (aref (rmail-mime-entity-display entity) 1))
669 (header (rmail-mime-entity-header entity))
670 (tagline (rmail-mime-entity-tagline entity))
671 (bulk-data (aref tagline 1))
672 (body (rmail-mime-entity-body entity))
673 ;; Find the default directory for this media type.
674 (directory (catch 'directory
675 (dolist (entry rmail-mime-attachment-dirs-alist)
676 (when (string-match (car entry) (car content-type))
677 (dolist (dir (cdr entry))
678 (when (file-directory-p dir)
679 (throw 'directory dir)))))))
680 (filename (or (cdr (assq 'name (cdr content-type)))
681 (cdr (assq 'filename (cdr content-disposition)))
683 (units '(B kB MB GB))
684 (segment (rmail-mime-entity-segment (point) entity))
687 (if (integerp (aref body 0))
689 size (car bulk-data))
690 (if (stringp (aref body 0))
691 (setq data (aref body 0))
692 (setq data (string-as-unibyte (buffer-string)))
694 (rmail-mime-set-bulk-data entity)
695 (delete-region (point-min) (point-max)))
696 (setq size (length data)))
697 (while (and (> size 1024.0) ; cribbed from gnus-agent-expire-done-message
699 (setq size (/ size 1024.0)
705 (if (eq (aref current 0) (aref new 0))
706 (goto-char (aref segment 2))
708 (delete-char (- (aref segment 2) (aref segment 1))))
710 (rmail-mime-insert-header header)))
713 (if (eq (aref current 1) (aref new 1))
714 (if (or (not (aref current 1))
715 (eq (aref current 2) (aref new 2)))
716 (forward-char (- (aref segment 3) (aref segment 2)))
717 (rmail-mime-update-tagline entity))
719 (delete-char (- (aref segment 3) (aref segment 2))))
721 (rmail-mime-insert-tagline
725 :type 'rmail-mime-save
726 'help-echo "mouse-2, RET: Save attachment"
728 'directory (file-name-as-directory directory)
730 (format " (%.0f%s)" size (car units))
731 ;; We don't need this button because the "type" string of a
732 ;; tagline is the button to do this.
733 ;; (if (cdr bulk-data)
735 ;; (if (cdr bulk-data)
736 ;; (list "Toggle show/hide"
737 ;; :type 'rmail-mime-image
738 ;; 'help-echo "mouse-2, RET: Toggle show/hide"
739 ;; 'image-type (cdr bulk-data)
740 ;; 'image-data data))
743 (if (eq (aref current 2) (aref new 2))
744 (forward-char (- (aref segment 4) (aref segment 3)))
746 (delete-char (- (aref segment 4) (aref segment 3))))
748 (cond ((eq (cdr bulk-data) 'text)
749 (rmail-mime-insert-decoded-text entity))
751 (rmail-mime-insert-image entity))
753 ;; As we don't know how to display the body, just
754 ;; insert it as a text.
755 (rmail-mime-insert-decoded-text entity)))))
756 (put-text-property beg (point) 'rmail-mime-entity entity)))
758 (defun test-rmail-mime-bulk-handler ()
759 "Test of a mail used as an example in RFC 2183."
760 (let ((mail "Content-Type: image/jpeg
761 Content-Disposition: attachment; filename=genome.jpeg;
762 modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";
763 Content-Description: a complete map of the human genome
764 Content-Transfer-Encoding: base64
766 iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAZQ
767 TFRF////AAAAVcLTfgAAAPZJREFUeNq9ldsOwzAIQ+3//+l1WlvA5ZLsoUiTto4TB+ISoAjy
768 +ITfRBfcAmgRFFeAm+J6uhdKdFhFWUgDkFsK0oUp/9G2//Kj7Jx+5tSKOdBscgUYiKHRS/me
769 WATQdRUvAK0Bnmshmtn79PpaLBbbOZkjKvRnjRZoRswOkG1wFchKew2g9wXVJVZL/m4+B+vv
770 9AxQQR2Q33SgAYJzzVACdAWjAfRYzYFO9n6SLnydtQHSMxYDMAKqZ/8FS/lTK+zuq3CtK64L
771 UDwbgUEAUmk2Zyg101d6PhCDySgAvTvDgKiuOrc4dLxUb7UMnhGIexyI+d6U+ABuNAP4Simx
774 (switch-to-buffer (get-buffer-create "*test*"))
779 (defun rmail-mime-multipart-handler (content-type
781 content-transfer-encoding)
782 "Handle the current buffer as a multipart MIME body.
783 The current buffer should be narrowed to the body. CONTENT-TYPE,
784 CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
785 of the respective parsed headers. See `rmail-mime-handle' for their
787 (rmail-mime-process-multipart
788 content-type content-disposition content-transfer-encoding nil)
791 (defun rmail-mime-process-multipart (content-type
793 content-transfer-encoding
795 "Process the current buffer as a multipart MIME body.
797 If PARSE-TAG is nil, modify the current buffer directly for
798 showing the MIME body and return nil.
800 Otherwise, PARSE-TAG is a string indicating the depth and index
801 number of the entity. In this case, parse the current buffer and
802 return a list of MIME-entity objects.
804 The other arguments are the same as `rmail-mime-multipart-handler'."
805 ;; Some MUAs start boundaries with "--", while it should start
806 ;; with "CRLF--", as defined by RFC 2046:
807 ;; The boundary delimiter MUST occur at the beginning of a line,
808 ;; i.e., following a CRLF, and the initial CRLF is considered to
809 ;; be attached to the boundary delimiter line rather than part
810 ;; of the preceding part.
811 ;; We currently don't handle that.
812 (let ((boundary (cdr (assq 'boundary content-type)))
813 (subtype (cadr (split-string (car content-type) "/")))
815 beg end next entities)
817 (rmail-mm-get-boundary-error-message
818 "No boundary defined" content-type content-disposition
819 content-transfer-encoding))
820 (setq boundary (concat "\n--" boundary))
821 ;; Hide the body before the first bodypart
822 (goto-char (point-min))
823 (when (and (search-forward boundary nil t)
824 (looking-at "[ \t]*\n"))
826 (narrow-to-region (match-end 0) (point-max))
827 (delete-region (point-min) (match-end 0))))
829 ;; Change content-type to the proper default one for the children.
830 (cond ((string-match "mixed" subtype)
831 (setq content-type '("text/plain")))
832 ((string-match "digest" subtype)
833 (setq content-type '("message/rfc822")))
835 (setq content-type nil)))
837 ;; Loop over all body parts, where beg points at the beginning of
838 ;; the part and end points at the end of the part. next points at
839 ;; the beginning of the next part. The current point is just
840 ;; after the boundary tag.
841 (setq beg (point-min))
842 (while (search-forward boundary nil t)
843 (setq end (match-beginning 0))
844 ;; If this is the last boundary according to RFC 2046, hide the
845 ;; epilogue, else hide the boundary only. Use a marker for
846 ;; `next' because `rmail-mime-show' may change the buffer.
847 (cond ((looking-at "--[ \t]*$")
848 (setq next (point-max-marker)))
849 ((looking-at "[ \t]*\n")
850 (setq next (copy-marker (match-end 0) t)))
852 (rmail-mm-get-boundary-error-message
853 "Malformed boundary" content-type content-disposition
854 content-transfer-encoding)))
856 (setq index (1+ index))
860 (narrow-to-region beg end)
861 (let ((child (rmail-mime-process
862 nil (format "%s/%d" parse-tag index)
863 content-type content-disposition)))
864 ;; Display a tagline.
865 (aset (aref (rmail-mime-entity-display child) 1) 1
866 (aset (rmail-mime-entity-tagline child) 2 t))
867 (push child entities)))
869 (delete-region end next)
871 (narrow-to-region beg end)
873 (goto-char (setq beg next)))
876 (setq entities (nreverse entities))
877 (if (string-match "alternative" subtype)
878 ;; Find the best entity to show, and hide all the others.
880 (dolist (child entities)
881 (if (string= (or (car (rmail-mime-entity-disposition child))
882 (car content-disposition))
884 (if (string-match "text/plain"
885 (car (rmail-mime-entity-type child)))
887 (if (string-match "text/.*"
888 (car (rmail-mime-entity-type child)))
889 (setq second child)))))
890 (or best (not second) (setq best second))
891 (dolist (child entities)
892 (unless (eq best child)
893 (aset (rmail-mime-entity-body child) 2 nil)
894 (rmail-mime-hidden-mode child)))))
897 (defun test-rmail-mime-multipart-handler ()
898 "Test of a mail used as an example in RFC 2046."
899 (let ((mail "From: Nathaniel Borenstein <nsb@bellcore.com>
900 To: Ned Freed <ned@innosoft.com>
901 Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)
902 Subject: Sample message
904 Content-type: multipart/mixed; boundary=\"simple boundary\"
906 This is the preamble. It is to be ignored, though it
907 is a handy place for composition agents to include an
908 explanatory note to non-MIME conformant readers.
912 This is implicitly typed plain US-ASCII text.
913 It does NOT end with a linebreak.
915 Content-type: text/plain; charset=us-ascii
917 This is explicitly typed plain US-ASCII text.
918 It DOES end with a linebreak.
922 This is the epilogue. It is also to be ignored."))
923 (switch-to-buffer (get-buffer-create "*test*"))
926 (rmail-mime-show t)))
928 (defun rmail-mime-insert-multipart (entity)
929 "Presentation handler for a multipart MIME entity."
930 (let ((current (aref (rmail-mime-entity-display entity) 0))
931 (new (aref (rmail-mime-entity-display entity) 1))
932 (header (rmail-mime-entity-header entity))
933 (tagline (rmail-mime-entity-tagline entity))
934 (body (rmail-mime-entity-body entity))
936 (segment (rmail-mime-entity-segment (point) entity)))
938 (if (eq (aref current 0) (aref new 0))
939 (goto-char (aref segment 2))
941 (delete-char (- (aref segment 2) (aref segment 1))))
943 (rmail-mime-insert-header header)))
945 (if (eq (aref current 1) (aref new 1))
946 (if (or (not (aref current 1))
947 (eq (aref current 2) (aref new 2)))
948 (forward-char (- (aref segment 3) (aref segment 2)))
949 (rmail-mime-update-tagline entity))
951 (delete-char (- (aref segment 3) (aref segment 2))))
953 (rmail-mime-insert-tagline entity)))
955 (put-text-property beg (point) 'rmail-mime-entity entity)
958 (if (eq (aref current 2) (aref new 2))
959 (forward-char (- (aref segment 4) (aref segment 3)))
960 (dolist (child (rmail-mime-entity-children entity))
961 (rmail-mime-insert child)))
966 (defun rmail-mime-handle (content-type
968 content-transfer-encoding)
969 "Handle the current buffer as a MIME part.
970 The current buffer should be narrowed to the respective body, and
971 point should be at the beginning of the body.
973 CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
974 are the values of the respective parsed headers. The latter should
975 be downcased. The parsed headers for CONTENT-TYPE and CONTENT-DISPOSITION
982 \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
984 VALUE is a string and ATTRIBUTE is a symbol.
986 Consider the following header, for example:
988 Content-Type: multipart/mixed;
989 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
991 The parsed header value:
993 \(\"multipart/mixed\"
994 \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
995 ;; Handle the content transfer encodings we know. Unknown transfer
996 ;; encodings will be passed on to the various handlers.
997 (cond ((string= content-transfer-encoding "base64")
999 (base64-decode-region (point) (point-max)))
1000 (setq content-transfer-encoding nil)))
1001 ((string= content-transfer-encoding "quoted-printable")
1002 (quoted-printable-decode-region (point) (point-max))
1003 (setq content-transfer-encoding nil))
1004 ((string= content-transfer-encoding "8bit")
1005 ;; FIXME: Is this the correct way?
1006 ;; No, of course not, it just means there's no decoding to do.
1007 ;; (set-buffer-multibyte nil)
1008 (setq content-transfer-encoding nil)
1010 ;; Inline stuff requires work. Attachments are handled by the bulk
1012 (if (string= "inline" (car content-disposition))
1014 (dolist (entry rmail-mime-media-type-handlers-alist)
1015 (when (and (string-match (car entry) (car content-type)) (not stop))
1017 (setq stop (funcall (cadr entry) content-type
1019 content-transfer-encoding))))))
1020 ;; Everything else is an attachment.
1021 (rmail-mime-bulk-handler content-type
1023 content-transfer-encoding))
1026 (let ((entity (get-text-property (1- (point)) 'rmail-mime-entity))
1029 (setq current (aref (rmail-mime-entity-display entity) 0)
1030 new (aref (rmail-mime-entity-display entity) 1))
1032 (aset current i (aref new i)))))))
1034 (defun rmail-mime-show (&optional show-headers)
1035 "Handle the current buffer as a MIME message.
1036 If SHOW-HEADERS is non-nil, then the headers of the current part
1037 will shown as usual for a MIME message. The headers are also
1038 shown for the content type message/rfc822. This function will be
1039 called recursively if multiple parts are available.
1041 The current buffer must contain a single message. It will be
1043 (rmail-mime-process show-headers nil))
1045 (defun rmail-mime-process (show-headers parse-tag &optional
1046 default-content-type
1047 default-content-disposition)
1048 (let ((end (point-min))
1050 content-transfer-encoding
1051 content-disposition)
1052 ;; `point-min' returns the beginning and `end' points at the end
1054 (goto-char (point-min))
1055 ;; If we're showing a part without headers, then it will start
1057 (if (eq (char-after) ?\n)
1058 (setq end (1+ (point)))
1059 (when (search-forward "\n\n" nil t)
1060 (setq end (match-end 0))
1062 (narrow-to-region (point-min) end)
1063 ;; FIXME: Default disposition of the multipart entities should
1066 (mail-fetch-field "Content-Type")
1067 content-transfer-encoding
1068 (mail-fetch-field "Content-Transfer-Encoding")
1070 (mail-fetch-field "Content-Disposition")))))
1071 ;; Per RFC 2045, C-T-E is case insensitive (bug#5070), but the others
1072 ;; are not completely so. Hopefully mail-header-parse-* DTRT.
1073 (if content-transfer-encoding
1074 (setq content-transfer-encoding (downcase content-transfer-encoding)))
1077 (or (mail-header-parse-content-type content-type)
1079 (or default-content-type '("text/plain"))))
1080 (setq content-disposition
1081 (if content-disposition
1082 (mail-header-parse-content-disposition content-disposition)
1083 ;; If none specified, we are free to choose what we deem
1084 ;; suitable according to RFC 2183. We like inline.
1085 (or default-content-disposition '("inline"))))
1086 ;; Unrecognized disposition types are to be treated like
1087 ;; attachment according to RFC 2183.
1088 (unless (member (car content-disposition) '("inline" "attachment"))
1089 (setq content-disposition '("attachment")))
1092 (let* ((is-inline (string= (car content-disposition) "inline"))
1093 (header (vector (point-min) end nil))
1094 (tagline (vector parse-tag (cons nil nil) t))
1095 (body (vector end (point-max) is-inline))
1096 (new (vector (aref header 2) (aref tagline 2) (aref body 2)))
1097 children handler entity)
1098 (cond ((string-match "multipart/.*" (car content-type))
1100 (narrow-to-region (1- end) (point-max))
1101 (setq children (rmail-mime-process-multipart
1104 content-transfer-encoding
1106 handler 'rmail-mime-insert-multipart)))
1107 ((string-match "message/rfc822" (car content-type))
1109 (narrow-to-region end (point-max))
1110 (let* ((msg (rmail-mime-process t parse-tag
1111 '("text/plain") '("inline")))
1112 (msg-new (aref (rmail-mime-entity-display msg) 1)))
1113 ;; Show header of the child.
1115 (aset (rmail-mime-entity-header msg) 2 t)
1116 ;; Hide tagline of the child.
1117 (aset msg-new 1 nil)
1118 (aset (rmail-mime-entity-tagline msg) 2 nil)
1119 (setq children (list msg)
1120 handler 'rmail-mime-insert-multipart))))
1121 ((and is-inline (string-match "text/" (car content-type)))
1122 ;; Don't need a tagline.
1123 (aset new 1 (aset tagline 2 nil))
1124 (setq handler 'rmail-mime-insert-text))
1126 ;; Force hidden mode.
1127 (aset new 1 (aset tagline 2 t))
1128 (aset new 2 (aset body 2 nil))
1129 (setq handler 'rmail-mime-insert-bulk)))
1130 (setq entity (rmail-mime-entity content-type
1132 content-transfer-encoding
1133 (vector (vector nil nil nil) new)
1134 header tagline body children handler))
1135 (if (and (eq handler 'rmail-mime-insert-bulk)
1136 (rmail-mime-set-bulk-data entity))
1138 (aset new 2 (aset body 2 t)))
1141 ;; Hide headers and handle the part.
1142 (put-text-property (point-min) (point-max) 'rmail-mime-entity
1144 content-type content-disposition
1145 content-transfer-encoding
1146 (vector (vector 'raw nil 'raw) (vector 'raw nil 'raw))
1147 (vector nil nil 'raw) (vector "" (cons nil nil) nil)
1148 (vector nil nil 'raw) nil nil))
1150 (cond ((string= (car content-type) "message/rfc822")
1151 (narrow-to-region end (point-max)))
1153 (delete-region (point-min) end)))
1154 (rmail-mime-handle content-type content-disposition
1155 content-transfer-encoding)))))
1157 (defun rmail-mime-parse ()
1158 "Parse the current Rmail message as a MIME message.
1159 The value is a MIME-entiy object (see `rmail-mime-entity').
1160 If an error occurs, return an error message string."
1161 (let ((rmail-mime-mbox-buffer (if (rmail-buffers-swapped-p)
1165 (with-current-buffer rmail-mime-mbox-buffer
1167 (goto-char (point-min))
1168 (let* ((entity (rmail-mime-process t ""
1169 '("text/plain") '("inline")))
1170 (new (aref (rmail-mime-entity-display entity) 1)))
1172 (aset new 0 (aset (rmail-mime-entity-header entity) 2 t))
1173 ;; Show tagline if and only if body is not shown.
1175 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 nil))
1176 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 t)))
1178 (error (format "%s" err)))))
1180 (defun rmail-mime-insert (entity)
1181 "Insert a MIME-entity ENTITY in the current buffer.
1183 This function will be called recursively if multiple parts are
1185 (let ((current (aref (rmail-mime-entity-display entity) 0))
1186 (new (aref (rmail-mime-entity-display entity) 1)))
1187 (if (not (eq (aref new 0) 'raw))
1188 ;; Not a raw-mode. Each handler should handle it.
1189 (funcall (rmail-mime-entity-handler entity) entity)
1190 (let ((header (rmail-mime-entity-header entity))
1191 (tagline (rmail-mime-entity-tagline entity))
1192 (body (rmail-mime-entity-body entity))
1194 (segment (rmail-mime-entity-segment (point) entity)))
1196 (if (eq (aref current 0) (aref new 0))
1197 (goto-char (aref segment 2))
1198 (if (aref current 0)
1199 (delete-char (- (aref segment 2) (aref segment 1))))
1200 (insert-buffer-substring rmail-mime-mbox-buffer
1201 (aref header 0) (aref header 1)))
1203 (if (aref current 1)
1204 (delete-char (- (aref segment 3) (aref segment 2))))
1206 (let ((children (rmail-mime-entity-children entity)))
1209 (put-text-property beg (point) 'rmail-mime-entity entity)
1210 (dolist (child children)
1211 (rmail-mime-insert child)))
1212 (if (eq (aref current 2) (aref new 2))
1213 (forward-char (- (aref segment 4) (aref segment 3)))
1214 (if (aref current 2)
1215 (delete-char (- (aref segment 4) (aref segment 3))))
1216 (insert-buffer-substring rmail-mime-mbox-buffer
1217 (aref body 0) (aref body 1))
1218 (or (bolp) (insert "\n")))
1219 (put-text-property beg (point) 'rmail-mime-entity entity)))))
1221 (aset current i (aref new i)))))
1223 (define-derived-mode rmail-mime-mode fundamental-mode "RMIME"
1224 "Major mode used in `rmail-mime' buffers."
1225 (setq font-lock-defaults '(rmail-font-lock-keywords t t nil nil)))
1228 (defun rmail-mime (&optional arg)
1229 "Toggle displaying of a MIME message.
1231 The actualy behavior depends on the value of `rmail-enable-mime'.
1233 If `rmail-enable-mime' is t (default), this command change the
1234 displaying of a MIME message between decoded presentation form
1237 With ARG, toggle the displaying of the current MIME entity only.
1239 If `rmail-enable-mime' is nil, this creates a temporary
1240 \"*RMAIL*\" buffer holding a decoded copy of the message. Inline
1241 content-types are handled according to
1242 `rmail-mime-media-type-handlers-alist'. By default, this
1243 displays text and multipart messages, and offers to download
1244 attachments as specfied by `rmail-mime-attachment-dirs-alist'."
1246 (if rmail-enable-mime
1247 (with-current-buffer rmail-buffer
1248 (if (rmail-mime-message-p)
1249 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
1250 (rmail-mime-view-buffer rmail-buffer)
1251 (entity (get-text-property (point) 'rmail-mime-entity)))
1254 (rmail-mime-toggle-raw entity))
1255 (goto-char (point-min))
1256 (rmail-mime-toggle-raw
1257 (get-text-property (point) 'rmail-mime-entity))))
1258 (message "Not a MIME message")))
1259 (let* ((data (rmail-apply-in-message rmail-current-message 'buffer-string))
1260 (buf (get-buffer-create "*RMAIL*"))
1261 (rmail-mime-mbox-buffer rmail-view-buffer)
1262 (rmail-mime-view-buffer buf))
1264 (setq buffer-undo-list t)
1265 (let ((inhibit-read-only t))
1266 ;; Decoding the message in fundamental mode for speed, only
1267 ;; switching to rmail-mime-mode at the end for display. Eg
1268 ;; quoted-printable-decode-region gets very slow otherwise (Bug#4993).
1274 (set-buffer-modified-p nil))
1275 (view-buffer buf))))
1277 (defun rmail-mm-get-boundary-error-message (message type disposition encoding)
1278 "Return MESSAGE with more information on the main mime components."
1279 (error "%s; type: %s; disposition: %s; encoding: %s"
1280 message type disposition encoding))
1282 (defun rmail-show-mime ()
1283 "Function to set in `rmail-show-mime-function' (which see)."
1284 (let ((entity (rmail-mime-parse))
1285 (rmail-mime-mbox-buffer rmail-buffer)
1286 (rmail-mime-view-buffer rmail-view-buffer)
1287 (rmail-mime-coding-system nil))
1288 (if (vectorp entity)
1289 (with-current-buffer rmail-mime-view-buffer
1291 (rmail-mime-insert entity)
1292 (if (consp rmail-mime-coding-system)
1293 ;; Decoding is done by rfc2047-decode-region only for a
1294 ;; header. But, as the used coding system may have been
1295 ;; overriden by mm-charset-override-alist, we can't
1296 ;; trust (car rmail-mime-coding-system). So, here we
1297 ;; try the decoding again with mm-charset-override-alist
1299 (let ((mm-charset-override-alist nil))
1300 (setq rmail-mime-coding-system
1301 (rmail-mime-find-header-encoding
1302 (rmail-mime-entity-header entity)))))
1303 (set-buffer-file-coding-system
1304 (coding-system-base rmail-mime-coding-system) t t))
1305 ;; Decoding failed. ENTITY is an error message. Insert the
1306 ;; original message body as is, and show warning.
1307 (let ((region (with-current-buffer rmail-mime-mbox-buffer
1308 (goto-char (point-min))
1309 (re-search-forward "^$" nil t)
1311 (vector (point-min) (point) (point-max)))))
1312 (with-current-buffer rmail-mime-view-buffer
1313 (let ((inhibit-read-only t))
1315 (rmail-mime-insert-header region)
1316 (insert-buffer-substring rmail-mime-mbox-buffer
1317 (aref region 1) (aref region 2))))
1318 (set-buffer-file-coding-system 'no-conversion t t)
1319 (message "MIME decoding failed: %s" entity)))))
1321 (setq rmail-show-mime-function 'rmail-show-mime)
1323 (defun rmail-insert-mime-forwarded-message (forward-buffer)
1324 "Function to set in `rmail-insert-mime-forwarded-message-function' (which see)."
1325 (let ((rmail-mime-mbox-buffer
1326 (with-current-buffer forward-buffer rmail-view-buffer)))
1328 (narrow-to-region (point) (point))
1329 (message-forward-make-body-mime rmail-mime-mbox-buffer))))
1331 (setq rmail-insert-mime-forwarded-message-function
1332 'rmail-insert-mime-forwarded-message)
1334 (defun rmail-insert-mime-resent-message (forward-buffer)
1335 "Function to set in `rmail-insert-mime-resent-message-function' (which see)."
1336 (insert-buffer-substring
1337 (with-current-buffer forward-buffer rmail-view-buffer))
1338 (goto-char (point-min))
1339 (when (looking-at "From ")
1341 (delete-region (point-min) (point))))
1343 (setq rmail-insert-mime-resent-message-function
1344 'rmail-insert-mime-resent-message)
1346 (defun rmail-search-mime-message (msg regexp)
1347 "Function to set in `rmail-search-mime-message-function' (which see)."
1349 (narrow-to-region (rmail-msgbeg msg) (rmail-msgend msg))
1350 (let* ((rmail-mime-mbox-buffer (current-buffer))
1351 (rmail-mime-view-buffer rmail-view-buffer)
1352 (header-end (save-excursion
1353 (re-search-forward "^$" nil 'move) (point)))
1354 (body-end (point-max))
1355 (entity (rmail-mime-parse)))
1357 ;; At first, just search the headers.
1359 (insert-buffer-substring rmail-mime-mbox-buffer nil header-end)
1360 (rfc2047-decode-region (point-min) (point))
1361 (goto-char (point-min))
1362 (re-search-forward regexp nil t))
1363 ;; Next, search the body.
1365 (let* ((content-type (rmail-mime-entity-type entity))
1366 (charset (cdr (assq 'charset (cdr content-type)))))
1367 (or (not (string-match "text/.*" (car content-type)))
1369 (not (string= (downcase charset) "us-ascii"))))))
1370 ;; Search the decoded MIME message.
1372 (rmail-mime-insert entity)
1373 (goto-char (point-min))
1374 (re-search-forward regexp nil t))
1375 ;; Search the body without decoding.
1376 (goto-char header-end)
1377 (re-search-forward regexp nil t))))))
1379 (setq rmail-search-mime-message-function 'rmail-search-mime-message)
1384 ;; generated-autoload-file: "rmail.el"
1387 ;; arch-tag: 3f2c5e5d-1aef-4512-bc20-fd737c9d5dd9
1388 ;;; rmailmm.el ends here