* nnmail.el (nnmail-cache-insert): make sure that the
[gnus] / lisp / rfc2047.el
index 971e9c4..d4d6e96 100644 (file)
@@ -110,7 +110,8 @@ The values can be:
     (cn-gb-2312 . B)
     (euc-kr . B)
     (iso-2022-jp-2 . B)
-    (iso-2022-int-1 . B))
+    (iso-2022-int-1 . B)
+    (viscii . Q))
   "Alist of MIME charsets to RFC2047 encodings.
 Valid encodings are nil, `Q' and `B'.  These indicate binary (no) encoding,
 quoted-printable and base64 respectively.")
@@ -377,8 +378,8 @@ Dynamically bind `rfc2047-encoding-type' to change that."
                    (rfc2047-encode start end)
                    (setq last-encoded t)))))
            (error
-            (message "Invalid data for rfc2047 encoding: %s"
-                     (buffer-substring b e)))))))
+            (error "Invalid data for rfc2047 encoding: %s"
+                   (buffer-substring b e)))))))
     (rfc2047-fold-region b (point))))
 
 (defun rfc2047-encode-string (string)
@@ -404,18 +405,26 @@ By default, the region is treated as containing addresses (see
               (mm-charset-to-coding-system mime-charset)))
         ;; Fixme: Better, calculate the number of non-ASCII
         ;; characters, at least for 8-bit charsets.
-        (encoding (if (assq mime-charset
-                            rfc2047-charset-encoding-alist)
-                      (cdr (assq mime-charset
+        (encoding (or (cdr (assq mime-charset
                                  rfc2047-charset-encoding-alist))
-                    'B))
+                      ;; For the charsets that don't have a preferred
+                      ;; encoding, choose the one that's shorter.
+                      (save-restriction
+                        (narrow-to-region b e)
+                        (if (eq (mm-qp-or-base64) 'base64)
+                            'B
+                          'Q))))
         (start (concat
                 "=?" (downcase (symbol-name mime-charset)) "?"
                 (downcase (symbol-name encoding)) "?"))
         (factor (case mime-charset
                   ((iso-8859-5 iso-8859-7 iso-8859-8 koi8-r) 1)
+                  ((big5 gb2312 euc-kr) 2)
                   (utf-8 4)
                   (t 8)))
+        (pre (- b (save-restriction
+                    (widen)
+                    (rfc2047-point-at-bol))))
         ;; encoded-words must not be longer than 75 characters,
         ;; including charset, encoding etc.  This leaves us with
         ;; 75 - (length start) - 2 - 2 characters.  The last 2 is for
@@ -423,6 +432,9 @@ By default, the region is treated as containing addresses (see
         ;; each character expands to 8 bytes which is expanded by a
         ;; factor of 4/3 by base64 encoding.
         (length (floor (- 75 (length start) 4) (* factor (/ 4.0 3.0))))
+        ;; Limit line length to 76 characters.
+        (length1 (max 1 (floor (- 76 (length start) 4 pre)
+                               (* factor (/ 4.0 3.0)))))
         (first t))
     (if mime-charset
        (save-restriction
@@ -431,9 +443,14 @@ By default, the region is treated as containing addresses (see
            ;; break into lines before encoding
            (goto-char (point-min))
            (while (not (eobp))
-             (goto-char (min (point-max) (+ length (point))))
+             (if first
+                 (progn
+                   (goto-char (min (point-max) (+ length1 (point))))
+                   (setq first nil))
+               (goto-char (min (point-max) (+ length (point)))))
              (unless (eobp)
-               (insert ?\n))))
+               (insert ?\n)))
+           (setq first t))
          (if (and (mm-multibyte-p)
                   (mm-coding-system-p cs))
              (mm-encode-coding-region (point-min) (point-max) cs))