1 ;;; rmailmm.el --- MIME decoding and display stuff for RMAIL
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 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 HEADER: the position of the beginning of a header
277 TAGLINE: the position of the beginning of a tagline
278 BODY: the position of the beginning of a body
279 END: the position of the end of the entity.
280 INDEX: index into the returned vector indicating where POS is."
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-next-item ()
322 "Move point to the next displayed item of the current MIME entity.
323 A MIME entity has three items; header, tagline, and body.
324 If we are in the last item of the entity, move point to the first
325 item of the next entity. If we reach the end of buffer, move
326 point to the first item of the first entity (i.e. the beginning
329 (if (rmail-mime-message-p)
330 (let* ((segment (rmail-mime-entity-segment (point)))
331 (next-pos (aref segment (1+ (aref segment 0))))
332 (button (next-button (point))))
333 (goto-char (if (and button (< (button-start button) next-pos))
334 (button-start button)
337 (goto-char (point-min))))))
339 (defun rmail-mime-previous-item ()
340 "Move point to the previous displayed item of the current MIME message.
341 A MIME entity has three items; header, tagline, and body.
342 If we are at the beginning of the first item of the entity, move
343 point to the last item of the previous entity. If we reach the
344 beginning of buffer, move point to the last item of the last
347 (when (rmail-mime-message-p)
349 (goto-char (point-max)))
350 (let* ((segment (rmail-mime-entity-segment (1- (point))))
351 (prev-pos (aref segment (aref segment 0)))
352 (button (previous-button (point))))
353 (goto-char (if (and button (> (button-start button) prev-pos))
354 (button-start button)
357 (defun rmail-mime-shown-mode (entity)
358 "Make MIME-entity ENTITY displayed by the default way."
359 (let ((new (aref (rmail-mime-entity-display entity) 1)))
360 (aset new 0 (aref (rmail-mime-entity-header entity) 2))
361 (aset new 1 (aref (rmail-mime-entity-tagline entity) 2))
362 (aset new 2 (aref (rmail-mime-entity-body entity) 2))))
364 (defun rmail-mime-hidden-mode (entity top)
365 "Make MIME-entity ENTITY displayed in the hidden mode.
366 If TOP is non-nil, display ENTITY only by the tagline.
367 Otherwise, don't display ENTITY."
369 (let ((new (aref (rmail-mime-entity-display entity) 1)))
373 (aset (rmail-mime-entity-body entity) 2 nil))
374 (let ((current (aref (rmail-mime-entity-display entity) 0)))
377 (aset current 2 nil)))
378 (dolist (child (rmail-mime-entity-children entity))
379 (rmail-mime-hidden-mode child nil)))
381 (defun rmail-mime-raw-mode (entity)
382 "Make MIME-entity ENTITY displayed in the raw mode."
383 (let ((new (aref (rmail-mime-entity-display entity) 1)))
387 (dolist (child (rmail-mime-entity-children entity))
388 (rmail-mime-hidden-mode child nil))))
390 (defun rmail-mime-toggle-raw (entity)
391 "Toggle on and off the raw display mode of MIME-entity ENTITY."
392 (let* ((pos (if (eobp) (1- (point-max)) (point)))
393 (entity (get-text-property pos 'rmail-mime-entity))
394 (current (aref (rmail-mime-entity-display entity) 0))
395 (segment (rmail-mime-entity-segment pos entity)))
396 (if (not (eq (aref current 0) 'raw))
397 ;; Enter the raw mode.
398 (rmail-mime-raw-mode entity)
399 ;; Enter the shown mode.
400 (rmail-mime-shown-mode entity))
401 (let ((inhibit-read-only t)
402 (modified (buffer-modified-p)))
404 (goto-char (aref segment 1))
405 (rmail-mime-insert entity)
406 (restore-buffer-modified-p modified)))))
408 (defun rmail-mime-toggle-hidden ()
409 "Toggle on and off the hidden display mode of MIME-entity ENTITY."
411 (when (rmail-mime-message-p)
412 (let* ((rmail-mime-mbox-buffer rmail-view-buffer)
413 (rmail-mime-view-buffer (current-buffer))
414 (pos (if (eobp) (1- (point-max)) (point)))
415 (entity (get-text-property pos 'rmail-mime-entity))
416 (current (aref (rmail-mime-entity-display entity) 0))
417 (segment (rmail-mime-entity-segment pos entity)))
419 ;; Enter the hidden mode.
421 ;; If point is in the body part, move it to the tagline
422 ;; (or the header if headline is not displayed).
423 (if (= (aref segment 0) 3)
424 (goto-char (aref segment 2)))
425 (rmail-mime-hidden-mode entity t)
426 ;; If the current entity is the topmost one, display the
428 (if (and rmail-mime-mbox-buffer (= (aref segment 1) (point-min)))
429 (let ((new (aref (rmail-mime-entity-display entity) 1)))
431 ;; Enter the shown mode.
432 (aset (rmail-mime-entity-body entity) 2 t)
433 (rmail-mime-shown-mode entity))
434 (let ((inhibit-read-only t)
435 (modified (buffer-modified-p))
436 (rmail-mime-mbox-buffer rmail-view-buffer)
437 (rmail-mime-view-buffer rmail-buffer))
439 (goto-char (aref segment 1))
440 (rmail-mime-insert entity)
441 (restore-buffer-modified-p modified))))))
443 (define-key rmail-mode-map "\t" 'rmail-mime-next-item)
444 (define-key rmail-mode-map [backtab] 'rmail-mime-previous-item)
445 (define-key rmail-mode-map "\r" 'rmail-mime-toggle-hidden)
449 (defun rmail-mime-insert-tagline (entity &rest item-list)
450 "Insert a tag line for MIME-entity ENTITY.
451 ITEM-LIST is a list of strings or button-elements (list) to be added
454 (let ((tag (aref (rmail-mime-entity-tagline entity) 0)))
455 (if (> (length tag) 0) (insert (substring tag 1) ":")))
456 (insert (car (rmail-mime-entity-type entity)))
457 (dolist (item item-list)
461 (apply 'insert-button item))))
464 (defun rmail-mime-insert-header (header)
465 "Decode and insert a MIME-entity header HEADER in the current buffer.
466 HEADER is a vector [BEG END DEFAULT-STATUS].
467 See `rmail-mime-entity' for the detail."
469 (last-coding-system-used nil))
471 (narrow-to-region pos pos)
472 (with-current-buffer rmail-mime-mbox-buffer
473 (let ((rmail-buffer rmail-mime-mbox-buffer)
474 (rmail-view-buffer rmail-mime-view-buffer))
476 (goto-char (aref header 0))
477 (rmail-copy-headers (point) (aref header 1)))))
478 (rfc2047-decode-region pos (point))
479 (if (and last-coding-system-used (not rmail-mime-coding-system))
480 (setq rmail-mime-coding-system last-coding-system-used))
481 (goto-char (point-min))
482 (rmail-highlight-headers)
483 (goto-char (point-max))
486 (defun rmail-mime-text-handler (content-type
488 content-transfer-encoding)
489 "Handle the current buffer as a plain text MIME part."
490 (rmail-mime-insert-text
491 (rmail-mime-entity content-type content-disposition
492 content-transfer-encoding
493 (vector (vector nil nil nil) (vector nil nil t))
494 (vector nil nil nil) (vector "" (cons nil nil) t)
495 (vector nil nil nil) nil 'rmail-mime-insert-text))
498 (defun rmail-mime-insert-decoded-text (entity)
499 "Decode and insert the text body of MIME-entity ENTITY."
500 (let* ((content-type (rmail-mime-entity-type entity))
501 (charset (cdr (assq 'charset (cdr content-type))))
502 (coding-system (if charset
503 (coding-system-from-name charset)))
504 (body (rmail-mime-entity-body entity))
506 (or (and coding-system (coding-system-p coding-system))
507 (setq coding-system 'undecided))
508 (if (stringp (aref body 0))
509 (insert (aref body 0))
510 (let ((transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
511 (insert-buffer-substring rmail-mime-mbox-buffer
512 (aref body 0) (aref body 1))
513 (cond ((string= transfer-encoding "base64")
514 (ignore-errors (base64-decode-region pos (point))))
515 ((string= transfer-encoding "quoted-printable")
516 (quoted-printable-decode-region pos (point))))))
517 (decode-coding-region pos (point) coding-system)
518 (or rmail-mime-coding-system
519 (setq rmail-mime-coding-system coding-system))
520 (or (bolp) (insert "\n"))))
522 (defun rmail-mime-insert-text (entity)
523 "Presentation handler for a plain text MIME entity."
524 (let ((current (aref (rmail-mime-entity-display entity) 0))
525 (new (aref (rmail-mime-entity-display entity) 1))
526 (header (rmail-mime-entity-header entity))
527 (tagline (rmail-mime-entity-tagline entity))
528 (body (rmail-mime-entity-body entity))
530 (segment (rmail-mime-entity-segment (point) entity)))
532 (or (integerp (aref body 0))
533 (let ((data (buffer-string)))
535 (delete-region (point-min) (point-max))))
538 (if (eq (aref current 0) (aref new 0))
539 (goto-char (aref segment 2))
541 (delete-char (- (aref segment 2) (aref segment 1))))
543 (rmail-mime-insert-header header)))
545 (if (eq (aref current 1) (aref new 1))
546 (forward-char (- (aref segment 3) (aref segment 2)))
548 (delete-char (- (aref segment 3) (aref segment 2))))
550 (rmail-mime-insert-tagline entity)))
552 (if (eq (aref current 2) (aref new 2))
553 (forward-char (- (aref segment 4) (aref segment 3)))
555 (delete-char (- (aref segment 4) (aref segment 3))))
557 (rmail-mime-insert-decoded-text entity)))
558 (put-text-property beg (point) 'rmail-mime-entity entity)))
560 ;; FIXME move to the test/ directory?
561 (defun test-rmail-mime-handler ()
562 "Test of a mail using no MIME parts at all."
563 (let ((mail "To: alex@gnu.org
564 Content-Type: text/plain; charset=koi8-r
565 Content-Transfer-Encoding: 8bit
568 \372\304\322\301\327\323\324\327\325\312\324\305\41"))
569 (switch-to-buffer (get-buffer-create "*test*"))
571 (set-buffer-multibyte nil)
574 (set-buffer-multibyte t)))
577 (defun rmail-mime-insert-image (entity)
578 "Decode and insert the image body of MIME-entity ENTITY."
579 (let* ((content-type (car (rmail-mime-entity-type entity)))
580 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
581 (body (rmail-mime-entity-body entity))
583 (if (stringp (aref body 0))
584 (setq data (aref body 0))
585 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
586 (transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
588 (set-buffer-multibyte nil)
589 (setq buffer-undo-list t)
590 (insert-buffer-substring rmail-mime-mbox-buffer
591 (aref body 0) (aref body 1))
592 (cond ((string= transfer-encoding "base64")
593 (ignore-errors (base64-decode-region (point-min) (point-max))))
594 ((string= transfer-encoding "quoted-printable")
595 (quoted-printable-decode-region (point-min) (point-max))))
597 (buffer-substring-no-properties (point-min) (point-max))))))
598 (insert-image (create-image data (cdr bulk-data) t))
601 (defun rmail-mime-image (button)
602 "Display the image associated with BUTTON."
604 (goto-char (button-end button))
605 (rmail-mime-toggle-hidden)))
607 (define-button-type 'rmail-mime-image 'action 'rmail-mime-image)
610 (defun rmail-mime-bulk-handler (content-type
612 content-transfer-encoding)
613 "Handle the current buffer as an attachment to download.
614 For images that Emacs is capable of displaying, the behavior
615 depends upon the value of `rmail-mime-show-images'."
616 (rmail-mime-insert-bulk
617 (rmail-mime-entity content-type content-disposition content-transfer-encoding
618 (vector (vector nil nil nil) (vector nil t nil))
619 (vector nil nil nil) (vector "" (cons nil nil) t)
620 (vector nil nil nil) nil 'rmail-mime-insert-bulk)))
622 (defun rmail-mime-set-bulk-data (entity)
623 "Setup the information about the attachment object for MIME-entity ENTITY.
624 The value is non-nil if and only if the attachment object should be shown
626 (let ((content-type (car (rmail-mime-entity-type entity)))
627 (size (cdr (assq 'size (cdr (rmail-mime-entity-disposition entity)))))
628 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
629 (body (rmail-mime-entity-body entity))
632 (setq size (string-to-number size)))
633 ((stringp (aref body 0))
634 (setq size (length (aref body 0))))
636 ;; Rough estimation of the size.
637 (let ((encoding (rmail-mime-entity-transfer-encoding entity)))
638 (setq size (- (aref body 1) (aref body 0)))
639 (cond ((string= encoding "base64")
640 (setq size (/ (* size 3) 4)))
641 ((string= encoding "quoted-printable")
642 (setq size (/ (* size 7) 3)))))))
645 ((string-match "text/" content-type)
647 ((string-match "image/\\(.*\\)" content-type)
648 (setq type (image-type-from-file-name
649 (concat "." (match-string 1 content-type))))
650 (if (and (memq type image-types)
651 (image-type-available-p type))
652 (if (and rmail-mime-show-images
653 (not (eq rmail-mime-show-images 'button))
654 (or (not (numberp rmail-mime-show-images))
655 (< size rmail-mime-show-images)))
658 (setcar bulk-data size)
659 (setcdr bulk-data type)
662 (defun rmail-mime-insert-bulk (entity)
663 "Presentation handler for an attachment MIME entity."
664 ;; Find the default directory for this media type.
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 (directory (catch 'directory
674 (dolist (entry rmail-mime-attachment-dirs-alist)
675 (when (string-match (car entry) (car content-type))
676 (dolist (dir (cdr entry))
677 (when (file-directory-p dir)
678 (throw 'directory dir)))))))
679 (filename (or (cdr (assq 'name (cdr content-type)))
680 (cdr (assq 'filename (cdr content-disposition)))
682 (units '(B kB MB GB))
683 (segment (rmail-mime-entity-segment (point) entity))
686 (if (integerp (aref body 0))
688 size (car bulk-data))
689 (if (stringp (aref body 0))
690 (setq data (aref body 0))
691 (setq data (string-as-unibyte (buffer-string)))
693 (rmail-mime-set-bulk-data entity)
694 (delete-region (point-min) (point-max)))
695 (setq size (length data)))
696 (while (and (> size 1024.0) ; cribbed from gnus-agent-expire-done-message
698 (setq size (/ size 1024.0)
704 (if (eq (aref current 0) (aref new 0))
705 (goto-char (aref segment 2))
707 (delete-char (- (aref segment 2) (aref segment 1))))
709 (rmail-mime-insert-header header)))
712 (if (eq (aref current 1) (aref new 1))
713 (forward-char (- (aref segment 3) (aref segment 2)))
715 (delete-char (- (aref segment 3) (aref segment 2))))
717 (rmail-mime-insert-tagline
721 :type 'rmail-mime-save
722 'help-echo "mouse-2, RET: Save attachment"
724 'directory (file-name-as-directory directory)
726 (format " (%.0f%s)" size (car units))
730 (list "Toggle show/hide"
731 :type 'rmail-mime-image
732 'help-echo "mouse-2, RET: Toggle show/hide"
733 'image-type (cdr bulk-data)
734 'image-data data)))))
736 (if (eq (aref current 2) (aref new 2))
737 (forward-char (- (aref segment 4) (aref segment 3)))
739 (delete-char (- (aref segment 4) (aref segment 3))))
741 (cond ((eq (cdr bulk-data) 'text)
742 (rmail-mime-insert-decoded-text entity))
744 (rmail-mime-insert-image entity)))))
745 (put-text-property beg (point) 'rmail-mime-entity entity)))
747 (defun test-rmail-mime-bulk-handler ()
748 "Test of a mail used as an example in RFC 2183."
749 (let ((mail "Content-Type: image/jpeg
750 Content-Disposition: attachment; filename=genome.jpeg;
751 modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";
752 Content-Description: a complete map of the human genome
753 Content-Transfer-Encoding: base64
755 iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAZQ
756 TFRF////AAAAVcLTfgAAAPZJREFUeNq9ldsOwzAIQ+3//+l1WlvA5ZLsoUiTto4TB+ISoAjy
757 +ITfRBfcAmgRFFeAm+J6uhdKdFhFWUgDkFsK0oUp/9G2//Kj7Jx+5tSKOdBscgUYiKHRS/me
758 WATQdRUvAK0Bnmshmtn79PpaLBbbOZkjKvRnjRZoRswOkG1wFchKew2g9wXVJVZL/m4+B+vv
759 9AxQQR2Q33SgAYJzzVACdAWjAfRYzYFO9n6SLnydtQHSMxYDMAKqZ/8FS/lTK+zuq3CtK64L
760 UDwbgUEAUmk2Zyg101d6PhCDySgAvTvDgKiuOrc4dLxUb7UMnhGIexyI+d6U+ABuNAP4Simx
763 (switch-to-buffer (get-buffer-create "*test*"))
768 (defun rmail-mime-multipart-handler (content-type
770 content-transfer-encoding)
771 "Handle the current buffer as a multipart MIME body.
772 The current buffer should be narrowed to the body. CONTENT-TYPE,
773 CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
774 of the respective parsed headers. See `rmail-mime-handle' for their
776 (rmail-mime-process-multipart
777 content-type content-disposition content-transfer-encoding nil)
780 (defun rmail-mime-process-multipart (content-type
782 content-transfer-encoding
784 "Process the current buffer as a multipart MIME body.
786 If PARSE-TAG is nil, modify the current buffer directly for
787 showing the MIME body and return nil.
789 Otherwise, PARSE-TAG is a string indicating the depth and index
790 number of the entity. In this case, parse the current buffer and
791 return a list of MIME-entity objects.
793 The other arguments are the same as `rmail-mime-multipart-handler'."
794 ;; Some MUAs start boundaries with "--", while it should start
795 ;; with "CRLF--", as defined by RFC 2046:
796 ;; The boundary delimiter MUST occur at the beginning of a line,
797 ;; i.e., following a CRLF, and the initial CRLF is considered to
798 ;; be attached to the boundary delimiter line rather than part
799 ;; of the preceding part.
800 ;; We currently don't handle that.
801 (let ((boundary (cdr (assq 'boundary content-type)))
802 (subtype (cadr (split-string (car content-type) "/")))
804 beg end next entities)
806 (rmail-mm-get-boundary-error-message
807 "No boundary defined" content-type content-disposition
808 content-transfer-encoding))
809 (setq boundary (concat "\n--" boundary))
810 ;; Hide the body before the first bodypart
811 (goto-char (point-min))
812 (when (and (search-forward boundary nil t)
813 (looking-at "[ \t]*\n"))
815 (narrow-to-region (match-end 0) (point-max))
816 (delete-region (point-min) (match-end 0))))
818 ;; Change content-type to the proper default one for the children.
819 (cond ((string-match "mixed" subtype)
820 (setq content-type '("text/plain")))
821 ((string-match "digest" subtype)
822 (setq content-type '("message/rfc822"))))
824 ;; Loop over all body parts, where beg points at the beginning of
825 ;; the part and end points at the end of the part. next points at
826 ;; the beginning of the next part. The current point is just
827 ;; after the boundary tag.
828 (setq beg (point-min))
829 (while (search-forward boundary nil t)
830 (setq end (match-beginning 0))
831 ;; If this is the last boundary according to RFC 2046, hide the
832 ;; epilogue, else hide the boundary only. Use a marker for
833 ;; `next' because `rmail-mime-show' may change the buffer.
834 (cond ((looking-at "--[ \t]*$")
835 (setq next (point-max-marker)))
836 ((looking-at "[ \t]*\n")
837 (setq next (copy-marker (match-end 0) t)))
839 (rmail-mm-get-boundary-error-message
840 "Malformed boundary" content-type content-disposition
841 content-transfer-encoding)))
843 (setq index (1+ index))
847 (narrow-to-region beg end)
848 (let ((child (rmail-mime-process
849 nil (format "%s/%d" parse-tag index)
850 content-type content-disposition)))
851 ;; Display a tagline.
852 (aset (aref (rmail-mime-entity-display child) 1) 1
853 (aset (rmail-mime-entity-tagline child) 2 t))
854 (push child entities)))
856 (delete-region end next)
858 (narrow-to-region beg end)
860 (goto-char (setq beg next)))
863 (setq entities (nreverse entities))
864 (if (string-match "alternative" subtype)
865 ;; Find the best entity to show, and hide all the others.
867 (dolist (child entities)
868 (if (string= (or (car (rmail-mime-entity-disposition child))
869 (car content-disposition))
871 (if (string-match "text/plain"
872 (car (rmail-mime-entity-type child)))
874 (if (string-match "text/.*"
875 (car (rmail-mime-entity-type child)))
876 (setq second child)))))
877 (or best (not second) (setq best second))
878 (dolist (child entities)
880 (rmail-mime-hidden-mode child t)))))
883 (defun test-rmail-mime-multipart-handler ()
884 "Test of a mail used as an example in RFC 2046."
885 (let ((mail "From: Nathaniel Borenstein <nsb@bellcore.com>
886 To: Ned Freed <ned@innosoft.com>
887 Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)
888 Subject: Sample message
890 Content-type: multipart/mixed; boundary=\"simple boundary\"
892 This is the preamble. It is to be ignored, though it
893 is a handy place for composition agents to include an
894 explanatory note to non-MIME conformant readers.
898 This is implicitly typed plain US-ASCII text.
899 It does NOT end with a linebreak.
901 Content-type: text/plain; charset=us-ascii
903 This is explicitly typed plain US-ASCII text.
904 It DOES end with a linebreak.
908 This is the epilogue. It is also to be ignored."))
909 (switch-to-buffer (get-buffer-create "*test*"))
912 (rmail-mime-show t)))
914 (defun rmail-mime-insert-multipart (entity)
915 "Presentation handler for a multipart MIME entity."
916 (let ((current (aref (rmail-mime-entity-display entity) 0))
917 (new (aref (rmail-mime-entity-display entity) 1))
918 (header (rmail-mime-entity-header entity))
919 (tagline (rmail-mime-entity-tagline entity))
920 (body (rmail-mime-entity-body entity))
922 (segment (rmail-mime-entity-segment (point) entity)))
924 (if (eq (aref current 0) (aref new 0))
925 (goto-char (aref segment 2))
927 (delete-char (- (aref segment 2) (aref segment 1))))
929 (rmail-mime-insert-header header)))
931 (if (eq (aref current 1) (aref new 1))
932 (forward-char (- (aref segment 3) (aref segment 2)))
934 (delete-char (- (aref segment 3) (aref segment 2))))
936 (rmail-mime-insert-tagline entity)))
938 (put-text-property beg (point) 'rmail-mime-entity entity)
940 (if (eq (aref current 2) (aref new 2))
941 (forward-char (- (aref segment 4) (aref segment 3)))
943 (delete-char (- (aref segment 4) (aref segment 3))))
945 (dolist (child (rmail-mime-entity-children entity))
946 (rmail-mime-insert child))))))
950 (defun rmail-mime-handle (content-type
952 content-transfer-encoding)
953 "Handle the current buffer as a MIME part.
954 The current buffer should be narrowed to the respective body, and
955 point should be at the beginning of the body.
957 CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
958 are the values of the respective parsed headers. The latter should
959 be downcased. The parsed headers for CONTENT-TYPE and CONTENT-DISPOSITION
966 \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
968 VALUE is a string and ATTRIBUTE is a symbol.
970 Consider the following header, for example:
972 Content-Type: multipart/mixed;
973 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
975 The parsed header value:
977 \(\"multipart/mixed\"
978 \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
979 ;; Handle the content transfer encodings we know. Unknown transfer
980 ;; encodings will be passed on to the various handlers.
981 (cond ((string= content-transfer-encoding "base64")
983 (base64-decode-region (point) (point-max)))
984 (setq content-transfer-encoding nil)))
985 ((string= content-transfer-encoding "quoted-printable")
986 (quoted-printable-decode-region (point) (point-max))
987 (setq content-transfer-encoding nil))
988 ((string= content-transfer-encoding "8bit")
989 ;; FIXME: Is this the correct way?
990 ;; No, of course not, it just means there's no decoding to do.
991 ;; (set-buffer-multibyte nil)
992 (setq content-transfer-encoding nil)
994 ;; Inline stuff requires work. Attachments are handled by the bulk
996 (if (string= "inline" (car content-disposition))
998 (dolist (entry rmail-mime-media-type-handlers-alist)
999 (when (and (string-match (car entry) (car content-type)) (not stop))
1001 (setq stop (funcall (cadr entry) content-type
1003 content-transfer-encoding))))))
1004 ;; Everything else is an attachment.
1005 (rmail-mime-bulk-handler content-type
1007 content-transfer-encoding)))
1009 (defun rmail-mime-show (&optional show-headers)
1010 "Handle the current buffer as a MIME message.
1011 If SHOW-HEADERS is non-nil, then the headers of the current part
1012 will shown as usual for a MIME message. The headers are also
1013 shown for the content type message/rfc822. This function will be
1014 called recursively if multiple parts are available.
1016 The current buffer must contain a single message. It will be
1018 (rmail-mime-process show-headers nil))
1020 (defun rmail-mime-process (show-headers parse-tag &optional
1021 default-content-type
1022 default-content-disposition)
1023 (let ((end (point-min))
1025 content-transfer-encoding
1026 content-disposition)
1027 ;; `point-min' returns the beginning and `end' points at the end
1029 (goto-char (point-min))
1030 ;; If we're showing a part without headers, then it will start
1032 (if (eq (char-after) ?\n)
1033 (setq end (1+ (point)))
1034 (when (search-forward "\n\n" nil t)
1035 (setq end (match-end 0))
1037 (narrow-to-region (point-min) end)
1038 ;; FIXME: Default disposition of the multipart entities should
1041 (mail-fetch-field "Content-Type")
1042 content-transfer-encoding
1043 (mail-fetch-field "Content-Transfer-Encoding")
1045 (mail-fetch-field "Content-Disposition")))))
1046 ;; Per RFC 2045, C-T-E is case insensitive (bug#5070), but the others
1047 ;; are not completely so. Hopefully mail-header-parse-* DTRT.
1048 (if content-transfer-encoding
1049 (setq content-transfer-encoding (downcase content-transfer-encoding)))
1052 (mail-header-parse-content-type content-type)
1053 (or default-content-type '("text/plain"))))
1054 (setq content-disposition
1055 (if content-disposition
1056 (mail-header-parse-content-disposition content-disposition)
1057 ;; If none specified, we are free to choose what we deem
1058 ;; suitable according to RFC 2183. We like inline.
1059 (or default-content-disposition '("inline"))))
1060 ;; Unrecognized disposition types are to be treated like
1061 ;; attachment according to RFC 2183.
1062 (unless (member (car content-disposition) '("inline" "attachment"))
1063 (setq content-disposition '("attachment")))
1066 (let* ((is-inline (string= (car content-disposition) "inline"))
1067 (header (vector (point-min) end nil))
1068 (tagline (vector parse-tag (cons nil nil) t))
1069 (body (vector end (point-max) is-inline))
1070 (new (vector (aref header 2) (aref tagline 2) (aref body 2)))
1071 children handler entity)
1072 (cond ((string-match "multipart/.*" (car content-type))
1074 (narrow-to-region (1- end) (point-max))
1075 (setq children (rmail-mime-process-multipart
1078 content-transfer-encoding
1080 handler 'rmail-mime-insert-multipart)))
1081 ((string-match "message/rfc822" (car content-type))
1083 (narrow-to-region end (point-max))
1084 (let* ((msg (rmail-mime-process t parse-tag
1085 '("text/plain") '("inline")))
1086 (msg-new (aref (rmail-mime-entity-display msg) 1)))
1087 ;; Show header of the child.
1089 (aset (rmail-mime-entity-header msg) 2 t)
1090 ;; Hide tagline of the child.
1091 (aset msg-new 1 nil)
1092 (aset (rmail-mime-entity-tagline msg) 2 nil)
1093 (setq children (list msg)
1094 handler 'rmail-mime-insert-multipart))))
1095 ((and is-inline (string-match "text/" (car content-type)))
1096 ;; Don't need a tagline.
1097 (aset new 1 (aset tagline 2 nil))
1098 (setq handler 'rmail-mime-insert-text))
1100 ;; Force hidden mode.
1101 (aset new 1 (aset tagline 2 t))
1102 (aset new 2 (aset body 2 nil))
1103 (setq handler 'rmail-mime-insert-bulk)))
1104 (setq entity (rmail-mime-entity content-type
1106 content-transfer-encoding
1107 (vector (vector nil nil nil) new)
1108 header tagline body children handler))
1109 (if (and (eq handler 'rmail-mime-insert-bulk)
1110 (rmail-mime-set-bulk-data entity))
1112 (aset new 2 (aset body 2 t)))
1115 ;; Hide headers and handle the part.
1116 (put-text-property (point-min) (point-max) 'rmail-mime-entity
1118 content-type content-disposition
1119 content-transfer-encoding
1120 (vector (vector 'raw nil 'raw) (vector 'raw nil 'raw))
1121 (vector nil nil 'raw) (vector "" (cons nil nil) nil)
1122 (vector nil nil 'raw) nil nil))
1124 (cond ((string= (car content-type) "message/rfc822")
1125 (narrow-to-region end (point-max)))
1127 (delete-region (point-min) end)))
1128 (rmail-mime-handle content-type content-disposition
1129 content-transfer-encoding)))))
1131 (defun rmail-mime-parse ()
1132 "Parse the current Rmail message as a MIME message.
1133 The value is a MIME-entiy object (see `rmail-mime-entity').
1134 If an error occurs, return an error message string."
1135 (let ((rmail-mime-mbox-buffer (if (rmail-buffers-swapped-p)
1139 (with-current-buffer rmail-mime-mbox-buffer
1141 (goto-char (point-min))
1142 (let* ((entity (rmail-mime-process t ""
1143 '("text/plain") '("inline")))
1144 (new (aref (rmail-mime-entity-display entity) 1)))
1146 (aset new 0 (aset (rmail-mime-entity-header entity) 2 t))
1147 ;; Show tagline if and only if body is not shown.
1149 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 nil))
1150 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 t)))
1152 (error (format "%s" err)))))
1154 (defun rmail-mime-insert (entity)
1155 "Insert a MIME-entity ENTITY in the current buffer.
1157 This function will be called recursively if multiple parts are
1159 (let ((current (aref (rmail-mime-entity-display entity) 0))
1160 (new (aref (rmail-mime-entity-display entity) 1)))
1161 (if (not (eq (aref new 0) 'raw))
1162 ;; Not a raw-mode. Each handler should handle it.
1163 (funcall (rmail-mime-entity-handler entity) entity)
1164 (let ((header (rmail-mime-entity-header entity))
1165 (tagline (rmail-mime-entity-tagline entity))
1166 (body (rmail-mime-entity-body entity))
1168 (segment (rmail-mime-entity-segment (point) entity)))
1170 (if (eq (aref current 0) (aref new 0))
1171 (goto-char (aref segment 2))
1172 (if (aref current 0)
1173 (delete-char (- (aref segment 2) (aref segment 1))))
1174 (insert-buffer-substring rmail-mime-mbox-buffer
1175 (aref header 0) (aref header 1)))
1177 (if (aref current 1)
1178 (delete-char (- (aref segment 3) (aref segment 2))))
1180 (if (eq (aref current 2) (aref new 2))
1181 (forward-char (- (aref segment 4) (aref segment 3)))
1182 (if (aref current 2)
1183 (delete-char (- (aref segment 4) (aref segment 3))))
1184 (insert-buffer-substring rmail-mime-mbox-buffer
1185 (aref body 0) (aref body 1)))
1186 (put-text-property beg (point) 'rmail-mime-entity entity)))
1188 (aset current i (aref new i)))))
1190 (define-derived-mode rmail-mime-mode fundamental-mode "RMIME"
1191 "Major mode used in `rmail-mime' buffers."
1192 (setq font-lock-defaults '(rmail-font-lock-keywords t t nil nil)))
1195 (defun rmail-mime (&optional arg)
1196 "Toggle displaying of a MIME message.
1198 The actualy behavior depends on the value of `rmail-enable-mime'.
1200 If `rmail-enable-mime' is t (default), this command change the
1201 displaying of a MIME message between decoded presentation form
1204 With ARG, toggle the displaying of the current MIME entity only.
1206 If `rmail-enable-mime' is nil, this creates a temporary
1207 \"*RMAIL*\" buffer holding a decoded copy of the message. Inline
1208 content-types are handled according to
1209 `rmail-mime-media-type-handlers-alist'. By default, this
1210 displays text and multipart messages, and offers to download
1211 attachments as specfied by `rmail-mime-attachment-dirs-alist'."
1213 (if rmail-enable-mime
1214 (if (rmail-mime-message-p)
1215 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
1216 (rmail-mime-view-buffer rmail-buffer)
1217 (entity (get-text-property (point) 'rmail-mime-entity)))
1220 (rmail-mime-toggle-raw entity))
1221 (goto-char (point-min))
1222 (rmail-mime-toggle-raw
1223 (get-text-property (point) 'rmail-mime-entity))))
1224 (message "Not a MIME message"))
1225 (let* ((data (rmail-apply-in-message rmail-current-message 'buffer-string))
1226 (buf (get-buffer-create "*RMAIL*"))
1227 (rmail-mime-mbox-buffer rmail-view-buffer)
1228 (rmail-mime-view-buffer buf))
1230 (setq buffer-undo-list t)
1231 (let ((inhibit-read-only t))
1232 ;; Decoding the message in fundamental mode for speed, only
1233 ;; switching to rmail-mime-mode at the end for display. Eg
1234 ;; quoted-printable-decode-region gets very slow otherwise (Bug#4993).
1240 (set-buffer-modified-p nil))
1241 (view-buffer buf))))
1243 (defun rmail-mm-get-boundary-error-message (message type disposition encoding)
1244 "Return MESSAGE with more information on the main mime components."
1245 (error "%s; type: %s; disposition: %s; encoding: %s"
1246 message type disposition encoding))
1248 (defun rmail-show-mime ()
1249 "Function to set in `rmail-show-mime-function' (which see)."
1250 (let ((entity (rmail-mime-parse))
1251 (rmail-mime-mbox-buffer rmail-buffer)
1252 (rmail-mime-view-buffer rmail-view-buffer)
1253 (rmail-mime-coding-system nil))
1254 (if (vectorp entity)
1255 (with-current-buffer rmail-mime-view-buffer
1257 (rmail-mime-insert entity)
1258 (if rmail-mime-coding-system
1259 (set-buffer-file-coding-system rmail-mime-coding-system t t)))
1260 ;; Decoding failed. ENTITY is an error message. Insert the
1261 ;; original message body as is, and show warning.
1262 (let ((region (with-current-buffer rmail-mime-mbox-buffer
1263 (goto-char (point-min))
1264 (re-search-forward "^$" nil t)
1266 (vector (point-min) (point) (point-max)))))
1267 (with-current-buffer rmail-mime-view-buffer
1268 (let ((inhibit-read-only t))
1270 (rmail-mime-insert-header region)
1271 (insert-buffer-substring rmail-mime-mbox-buffer
1272 (aref region 1) (aref region 2))))
1273 (set-buffer-file-coding-system 'no-conversion t t)
1274 (message "MIME decoding failed: %s" entity)))))
1276 (setq rmail-show-mime-function 'rmail-show-mime)
1278 (defun rmail-insert-mime-forwarded-message (forward-buffer)
1279 "Function to set in `rmail-insert-mime-forwarded-message-function' (which see)."
1280 (let ((rmail-mime-mbox-buffer
1281 (with-current-buffer forward-buffer rmail-view-buffer)))
1283 (narrow-to-region (point) (point))
1284 (message-forward-make-body-mime rmail-mime-mbox-buffer))))
1286 (setq rmail-insert-mime-forwarded-message-function
1287 'rmail-insert-mime-forwarded-message)
1289 (defun rmail-insert-mime-resent-message (forward-buffer)
1290 "Function to set in `rmail-insert-mime-resent-message-function' (which see)."
1291 (insert-buffer-substring
1292 (with-current-buffer forward-buffer rmail-view-buffer))
1293 (goto-char (point-min))
1294 (when (looking-at "From ")
1296 (delete-region (point-min) (point))))
1298 (setq rmail-insert-mime-resent-message-function
1299 'rmail-insert-mime-resent-message)
1301 (defun rmail-search-mime-message (msg regexp)
1302 "Function to set in `rmail-search-mime-message-function' (which see)."
1304 (narrow-to-region (rmail-msgbeg msg) (rmail-msgend msg))
1305 (let* ((rmail-mime-mbox-buffer (current-buffer))
1306 (rmail-mime-view-buffer rmail-view-buffer)
1307 (header-end (save-excursion
1308 (re-search-forward "^$" nil 'move) (point)))
1309 (body-end (point-max))
1310 (entity (rmail-mime-parse)))
1312 ;; At first, just search the headers.
1314 (insert-buffer-substring rmail-mime-mbox-buffer nil header-end)
1315 (rfc2047-decode-region (point-min) (point))
1316 (goto-char (point-min))
1317 (re-search-forward regexp nil t))
1318 ;; Next, search the body.
1320 (let* ((content-type (rmail-mime-entity-type entity))
1321 (charset (cdr (assq 'charset (cdr content-type)))))
1322 (or (not (string-match "text/.*" (car content-type)))
1324 (not (string= (downcase charset) "us-ascii"))))))
1325 ;; Search the decoded MIME message.
1327 (rmail-mime-insert entity)
1328 (goto-char (point-min))
1329 (re-search-forward regexp nil t))
1330 ;; Search the body without decoding.
1331 (goto-char header-end)
1332 (re-search-forward regexp nil t))))))
1334 (setq rmail-search-mime-message-function 'rmail-search-mime-message)
1339 ;; generated-autoload-file: "rmail.el"
1342 ;; arch-tag: 3f2c5e5d-1aef-4512-bc20-fd737c9d5dd9
1343 ;;; rmailmm.el ends here