(mm-decode-coding-region-safely): Don't use
[gnus] / lisp / mm-bodies.el
index 94b2c41..e4c7894 100644 (file)
@@ -143,6 +143,7 @@ If no encoding was done, nil is returned."
       bits)
      ((and (not mm-use-ultra-safe-encoding)
           (not longp)
+          (not (eq '7bit (cdr (assq charset mm-body-charset-encoding-alist))))
           (or (eq t (cdr message-posting-charset))
               (memq charset (cdr message-posting-charset))
               (eq charset mail-parse-charset)))
@@ -242,9 +243,10 @@ If TYPE is `text/plain' CRLF->LF translation may occur."
       (while (search-forward "\r\n" nil t)
        (replace-match "\n" t t)))))
 
-(defun mm-decode-body (charset &optional encoding type)
+(defun mm-decode-body (charset &optional encoding type force)
   "Decode the current article that has been encoded with ENCODING.
-The characters in CHARSET should then be decoded."
+The characters in CHARSET should then be decoded.  If FORCE is non-nil
+use the supplied charset unconditionally."
   (if (stringp charset)
       (setq charset (intern (downcase charset))))
   (if (or (not charset)
@@ -270,7 +272,27 @@ The characters in CHARSET should then be decoded."
                   (or (not (eq coding-system 'ascii))
                       (setq coding-system mail-parse-charset))
                   (not (eq coding-system 'gnus-decoded)))
-         (mm-decode-coding-region (point-min) (point-max) coding-system))))))
+         (if force
+             (mm-decode-coding-region (point-min) (point-max)
+                                             coding-system)
+           (mm-decode-coding-region-safely (point-min) (point-max)
+                                           coding-system)))))))
+
+(defun mm-decode-coding-region-safely (start end coding-system)
+  "Decode region between START and END with CODING-SYSTEM.
+If CODING-SYSTEM is not a valid coding system for the text, let Emacs
+decide which coding system to use."
+  (let* ((orig (buffer-substring start end))
+        charsets)
+    (save-restriction
+      (narrow-to-region start end)
+      (mm-decode-coding-region (point-min) (point-max) coding-system)
+      (setq charsets (find-charset-region (point-min) (point-max)))
+      (when (or (memq 'eight-bit-control charsets)
+               (memq 'eight-bit-graphic charsets))
+       (delete-region (point-min) (point-max))
+       (insert orig)
+       (mm-decode-coding-region (point-min) (point-max) 'undecided)))))
 
 (defun mm-decode-string (string charset)
   "Decode STRING with CHARSET."