2004-01-27 Steve Youngs <sryoungs@bigpond.net.au>
[gnus] / lisp / rfc2047.el
index cc8f52f..bac6129 100644 (file)
 
 (eval-when-compile
   (require 'cl)
-  (defvar message-posting-charset)
-  (unless (fboundp 'with-syntax-table) ; not in Emacs 20
-    (defmacro with-syntax-table (table &rest body)
-      "Evaluate BODY with syntax table of current buffer set to TABLE.
-The syntax table of the current buffer is saved, BODY is evaluated, and the
-saved table is restored, even in case of an abnormal exit.
-Value is what BODY returns."
-      (let ((old-table (make-symbol "table"))
-           (old-buffer (make-symbol "buffer")))
-       `(let ((,old-table (syntax-table))
-              (,old-buffer (current-buffer)))
-          (unwind-protect
-              (progn
-                (set-syntax-table ,table)
-                ,@body)
-            (save-current-buffer
-              (set-buffer ,old-buffer)
-              (set-syntax-table ,old-table))))))))
+  (defvar message-posting-charset))
 
 (require 'qp)
 (require 'mm-util)
@@ -685,7 +668,20 @@ By default, the region is treated as containing addresses (see
               mail-parse-charset
               (not (eq mail-parse-charset 'us-ascii))
               (not (eq mail-parse-charset 'gnus-decoded)))
-         (mm-decode-coding-string string mail-parse-charset)
+         ;; `decode-coding-string' in Emacs offers a third optional
+         ;; arg NOCOPY to avoid consing a new string if the decoding
+         ;; is "trivial".  Unfortunately it currently doesn't
+         ;; consider anything else than a `nil' coding system
+         ;; trivial.
+         ;; `rfc2047-decode-string' is called multiple times for each
+         ;; article during summary buffer generation, and we really
+         ;; want to avoid unnecessary consing.  So we bypass
+         ;; `decode-coding-string' if the string is purely ASCII.
+         (if (and (fboundp 'detect-coding-string)
+                  ;; string is purely ASCII
+                  (eq (detect-coding-string string t) 'undecided))
+             string
+           (mm-decode-coding-string string mail-parse-charset))
        (mm-string-as-multibyte string)))))
 
 (defun rfc2047-parse-and-decode (word)
@@ -698,7 +694,7 @@ decodable."
      (condition-case nil
         (rfc2047-decode
          (match-string 1 word)
-         (upcase (match-string 2 word))
+         (string-to-char (match-string 2 word))
          (match-string 3 word))
        (error word))
      word)))                           ; un-decodable
@@ -716,7 +712,7 @@ decodable."
 
 (defun rfc2047-decode (charset encoding string)
   "Decode STRING from the given MIME CHARSET in the given ENCODING.
-Valid ENCODINGs are \"B\" and \"Q\".
+Valid ENCODINGs are the characters \"B\" and \"Q\".
 If your Emacs implementation can't decode CHARSET, return nil."
   (if (stringp charset)
       (setq charset (intern (downcase charset))))
@@ -736,13 +732,13 @@ If your Emacs implementation can't decode CHARSET, return nil."
        (setq cs mail-parse-charset))
       (mm-decode-coding-string
        (cond
-       ((equal "B" encoding)
+       ((char-equal ?B encoding)
         (base64-decode-string
          (rfc2047-pad-base64 string)))
-       ((equal "Q" encoding)
+       ((char-equal ?Q encoding)
         (quoted-printable-decode-string
-         (mm-replace-chars-in-string string ?_ ? )))
-       (t (error "Invalid encoding: %s" encoding)))
+         (mm-subst-char-in-string ?_ ? string t)))
+       (t (error "Invalid encoding: %c" encoding)))
        cs))))
 
 (provide 'rfc2047)