Synch up with flim-1.14.3
[elisp/flim.git] / eword-decode.el
index e4143e6..328cc28 100644 (file)
     (eval-when-compile
       (concat (regexp-quote "=?")
              "\\("
-             mime-charset-regexp
+             mime-charset-regexp       ; 1
              "\\)"
+             "\\("
+             (regexp-quote "*")
+             mime-language-regexp      ; 2
+             "\\)?"
              (regexp-quote "?")
-             "\\([BbQq]\\)"
+             "\\("
+             mime-encoding-regexp      ; 3
+             "\\)"
              (regexp-quote "?")
              "\\("
-             eword-encoded-text-regexp
+             eword-encoded-text-regexp ; 4
              "\\)"
              (regexp-quote "?="))))
   )
@@ -224,7 +230,7 @@ such as a version of Net$cape)."
                                         "\\(\n?[ \t]\\)+"
                                         "\\(" eword-encoded-word-regexp "\\)")
                                 nil t)
-       (replace-match "\\1\\6")
+       (replace-match "\\1\\7")
         (goto-char (point-min))
        )
       (while (re-search-forward eword-encoded-word-regexp nil t)
@@ -522,64 +528,66 @@ If SEPARATOR is not nil, it is used as header separator."
        word))
 
 (defun eword-decode-encoded-word (word &optional must-unfold)
-  "Decode WORD if it is an encoded-word.
-
-If your emacs implementation can not decode the charset of WORD, it
-returns WORD.  Similarly the encoded-word is broken, it returns WORD.
-
-If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
-if there are in decoded encoded-word (generated by bad manner MUA such
-as a version of Net$cape)."
-  (or (if (string-match eword-encoded-word-regexp word)
-         (let ((charset
-                (substring word (match-beginning 1) (match-end 1))
-                )
-               (encoding
-                (upcase
-                 (substring word (match-beginning 2) (match-end 2))
-                 ))
-               (text
-                (substring word (match-beginning 3) (match-end 3))
-                ))
-            (condition-case err
-                (eword-decode-encoded-text charset encoding text must-unfold)
-              (error
-              (funcall eword-decode-encoded-word-error-handler word err)
-               ))
-            ))
+  "Decode WORD as an encoded-word.
+
+If charset is unknown or unsupported, return WORD.
+If encoding is unknown, or some error occurs while decoding,
+`eword-decode-encoded-word-error-handler' is called with WORD and an
+error condition.
+
+If MUST-UNFOLD is non-nil, unfold decoded WORD."
+  (or (and (string-match eword-encoded-word-regexp word)
+          (condition-case err
+              (eword-decode-encoded-text
+               ;; charset
+               (substring word (match-beginning 1)(match-end 1))
+               ;; language
+               (when (match-beginning 2)
+                 (intern
+                  (downcase
+                   (substring word (1+ (match-beginning 2))(match-end 2)))))
+               ;; encoding
+               (upcase
+                (substring word (match-beginning 3)(match-end 3)))
+               ;; encoded-text
+               (substring word (match-beginning 4)(match-end 4))
+               must-unfold)
+            (error
+             (funcall eword-decode-encoded-word-error-handler word err))))
       word))
 
 
 ;;; @ encoded-text decoder
 ;;;
 
-(defun eword-decode-encoded-text (charset encoding string
+(defun eword-decode-encoded-text (charset language encoding string
                                          &optional must-unfold)
   "Decode STRING as an encoded-text.
 
 If your emacs implementation can not decode CHARSET, it returns nil.
 
+If LANGUAGE is non-nil, it is put to `mime-language' text-property.
 If ENCODING is not \"B\" or \"Q\", it occurs error.
 So you should write error-handling code if you don't want break by errors.
 
 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
 if there are in decoded encoded-text (generated by bad manner MUA such
 as a version of Net$cape)."
-  (let ((cs (mime-charset-to-coding-system charset)))
-    (if cs
-       (let ((dest (encoded-text-decode-string string encoding)))
-         (when dest
-           (setq dest (decode-mime-charset-string dest charset))
-           (if must-unfold
-               (mapconcat (function
-                           (lambda (chr)
-                             (cond ((eq chr ?\n) "")
-                                   ((eq chr ?\t) " ")
-                                   (t (char-to-string chr)))
-                             ))
-                          (std11-unfold-string dest)
-                          "")
-             dest))))))
+  (when (mime-charset-to-coding-system charset)
+    (let ((dest (encoded-text-decode-string string encoding)))
+      (when dest
+       (setq dest (decode-mime-charset-string dest charset))
+       (when must-unfold
+         (mapconcat
+          (function
+           (lambda (chr)
+             (cond ((eq chr ?\n) "")
+                   ((eq chr ?\t) " ")
+                   (t (char-to-string chr)))))
+          (std11-unfold-string dest) ""))
+       (when language
+         (put-text-property 0 (length dest) 'mime-language language dest))
+       dest))))
 
 
 ;;; @ lexical analyze