diff options
| author | Chong Yidong | 2009-01-29 02:46:21 +0000 |
|---|---|---|
| committer | Chong Yidong | 2009-01-29 02:46:21 +0000 |
| commit | 2e55c1b7bbbb2c66e5a0d7eb756de5e5f302fd8f (patch) | |
| tree | 053e4c27d6f9613ca48bed6688da36c8c88f83e6 | |
| parent | fce18d3861208b6b2b219b4a31f02236142eb89f (diff) | |
| download | emacs-2e55c1b7bbbb2c66e5a0d7eb756de5e5f302fd8f.tar.gz emacs-2e55c1b7bbbb2c66e5a0d7eb756de5e5f302fd8f.zip | |
(rmail-redecode-body): New function, based on old version removed in
2009-01-22 change.
| -rw-r--r-- | lisp/mail/rmail.el | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/lisp/mail/rmail.el b/lisp/mail/rmail.el index e29ac965947..a1207e6d18e 100644 --- a/lisp/mail/rmail.el +++ b/lisp/mail/rmail.el | |||
| @@ -2619,6 +2619,100 @@ buffer to the end of the headers." | |||
| 2619 | (goto-char lim)))) | 2619 | (goto-char lim)))) |
| 2620 | (t (error "No headers selected for display!")))))))) | 2620 | (t (error "No headers selected for display!")))))))) |
| 2621 | 2621 | ||
| 2622 | (defun rmail-redecode-body (coding &optional raw) | ||
| 2623 | "Decode the body of the current message using coding system CODING. | ||
| 2624 | This is useful with mail messages that have malformed or missing | ||
| 2625 | charset= headers. | ||
| 2626 | |||
| 2627 | This function assumes that the current message is already decoded | ||
| 2628 | and displayed in the RMAIL buffer, but the coding system used to | ||
| 2629 | decode it was incorrect. It then encodes the message back to its | ||
| 2630 | original form, and decodes it again, using the coding system CODING. | ||
| 2631 | |||
| 2632 | Optional argument RAW, if non-nil, means don't encode the message | ||
| 2633 | before decoding it with the new CODING. This is useful if the current | ||
| 2634 | message text was produced by some function which invokes `insert', | ||
| 2635 | since `insert' leaves unibyte character codes 128 through 255 unconverted | ||
| 2636 | to multibyte. One example of such a situation is when the text was | ||
| 2637 | produced by `base64-decode-region'. | ||
| 2638 | |||
| 2639 | Interactively, invoke the function with a prefix argument to set RAW | ||
| 2640 | non-nil. | ||
| 2641 | |||
| 2642 | Note that if Emacs erroneously auto-detected one of the iso-2022 | ||
| 2643 | encodings in the message, this function might fail because the escape | ||
| 2644 | sequences that switch between character sets and also single-shift and | ||
| 2645 | locking-shift codes are impossible to recover. This function is meant | ||
| 2646 | to be used to fix messages encoded with 8-bit encodings, such as | ||
| 2647 | iso-8859, koi8-r, etc." | ||
| 2648 | (interactive "zCoding system for re-decoding this message: ") | ||
| 2649 | (when (not rmail-enable-mime) | ||
| 2650 | (save-excursion | ||
| 2651 | (set-buffer rmail-buffer) | ||
| 2652 | (rmail-swap-buffers-maybe) | ||
| 2653 | (save-restriction | ||
| 2654 | (widen) | ||
| 2655 | (let ((raw (or raw current-prefix-arg)) | ||
| 2656 | (msgbeg (rmail-msgbeg rmail-current-message)) | ||
| 2657 | (msgend (rmail-msgend rmail-current-message)) | ||
| 2658 | (buffer-read-only nil) | ||
| 2659 | body-start x-coding-header old-coding) | ||
| 2660 | (narrow-to-region msgbeg msgend) | ||
| 2661 | (goto-char (point-min)) | ||
| 2662 | (unless (setq body-start (search-forward "\n\n" (point-max) 1)) | ||
| 2663 | (error "No message body")) | ||
| 2664 | |||
| 2665 | (save-restriction | ||
| 2666 | ;; Narrow to headers | ||
| 2667 | (narrow-to-region (point-min) body-start) | ||
| 2668 | (goto-char (point-min)) | ||
| 2669 | (unless (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t) | ||
| 2670 | (error "No X-Coding-System header found")) | ||
| 2671 | (setq old-coding (intern (match-string 1))) | ||
| 2672 | (check-coding-system old-coding) | ||
| 2673 | ;; Make sure the new coding system uses the same EOL | ||
| 2674 | ;; conversion, to prevent ^M characters from popping up | ||
| 2675 | ;; all over the place. | ||
| 2676 | (setq coding | ||
| 2677 | (coding-system-change-eol-conversion | ||
| 2678 | coding (coding-system-eol-type old-coding))) | ||
| 2679 | ;; If old-coding is `undecided', encode-coding-region | ||
| 2680 | ;; will not encode the text at all. Find a proper | ||
| 2681 | ;; non-trivial encoding to use. | ||
| 2682 | (when (memq (coding-system-base old-coding) '(nil undecided)) | ||
| 2683 | (setq old-coding | ||
| 2684 | (car (find-coding-systems-region msgbeg msgend)))) | ||
| 2685 | (setq x-coding-header (point))) | ||
| 2686 | |||
| 2687 | (save-restriction | ||
| 2688 | ;; Narrow to message body | ||
| 2689 | (narrow-to-region body-start (point-max)) | ||
| 2690 | (and (null raw) | ||
| 2691 | ;; If old and new encoding are the same, it | ||
| 2692 | ;; clearly doesn't make sense to encode. | ||
| 2693 | (not (coding-system-equal | ||
| 2694 | (coding-system-base old-coding) | ||
| 2695 | (coding-system-base coding))) | ||
| 2696 | ;; If the body includes only eight-bit-* | ||
| 2697 | ;; characters, encoding might fail, e.g. with | ||
| 2698 | ;; UTF-8, and isn't needed anyway. | ||
| 2699 | (> (length (delq 'ascii | ||
| 2700 | (delq 'eight-bit-graphic | ||
| 2701 | (delq 'eight-bit-control | ||
| 2702 | (find-charset-region | ||
| 2703 | (point-min) (point-max)))))) | ||
| 2704 | 0) | ||
| 2705 | (encode-coding-region (point-min) (point-max) old-coding)) | ||
| 2706 | (decode-coding-region (point-min) (point-max) coding) | ||
| 2707 | (setq last-coding-system-used coding)) | ||
| 2708 | |||
| 2709 | ;; Rewrite the coding-system header. | ||
| 2710 | (goto-char x-coding-header) | ||
| 2711 | (delete-region (line-beginning-position) (point)) | ||
| 2712 | (insert "X-Coding-System: " | ||
| 2713 | (symbol-name last-coding-system-used)) | ||
| 2714 | (rmail-show-message-maybe)))))) | ||
| 2715 | |||
| 2622 | ;; Find all occurrences of certain fields, and highlight them. | 2716 | ;; Find all occurrences of certain fields, and highlight them. |
| 2623 | (defun rmail-highlight-headers () | 2717 | (defun rmail-highlight-headers () |
| 2624 | ;; Do this only if the system supports faces. | 2718 | ;; Do this only if the system supports faces. |