aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2009-01-29 02:46:21 +0000
committerChong Yidong2009-01-29 02:46:21 +0000
commit2e55c1b7bbbb2c66e5a0d7eb756de5e5f302fd8f (patch)
tree053e4c27d6f9613ca48bed6688da36c8c88f83e6
parentfce18d3861208b6b2b219b4a31f02236142eb89f (diff)
downloademacs-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.el94
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.
2624This is useful with mail messages that have malformed or missing
2625charset= headers.
2626
2627This function assumes that the current message is already decoded
2628and displayed in the RMAIL buffer, but the coding system used to
2629decode it was incorrect. It then encodes the message back to its
2630original form, and decodes it again, using the coding system CODING.
2631
2632Optional argument RAW, if non-nil, means don't encode the message
2633before decoding it with the new CODING. This is useful if the current
2634message text was produced by some function which invokes `insert',
2635since `insert' leaves unibyte character codes 128 through 255 unconverted
2636to multibyte. One example of such a situation is when the text was
2637produced by `base64-decode-region'.
2638
2639Interactively, invoke the function with a prefix argument to set RAW
2640non-nil.
2641
2642Note that if Emacs erroneously auto-detected one of the iso-2022
2643encodings in the message, this function might fail because the escape
2644sequences that switch between character sets and also single-shift and
2645locking-shift codes are impossible to recover. This function is meant
2646to be used to fix messages encoded with 8-bit encodings, such as
2647iso-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.