* mime-def.el (mime-library-product): Fix typo.
[elisp/flim.git] / ew-dec.el
index 6688adf..d958195 100644 (file)
--- a/ew-dec.el
+++ b/ew-dec.el
@@ -4,6 +4,7 @@
 (require 'ew-scan-s)
 (require 'ew-scan-m)
 (require 'ew-scan-u)
+(require 'ew-scan-n)
 (require 'ew-parse)
 (provide 'ew-dec)
 
@@ -25,7 +26,7 @@ each line is separated by CRLF."
         (tmp (assoc key ew-decode-field-cache-buf)))
     (if tmp
        (cdr tmp)
-      (progn
+      (let ((decoded (ew-decode-field-no-cache field-name field-body)))
        (setq tmp (nthcdr ew-decode-field-cache-num
                          ew-decode-field-cache-buf))
        (if (cdr tmp)
@@ -37,9 +38,7 @@ each line is separated by CRLF."
                (cons (cons nil nil)
                      ew-decode-field-cache-buf)))
        (setcar (car ew-decode-field-cache-buf) key)
-       (setcdr (car ew-decode-field-cache-buf)
-               (ew-decode-field-no-cache
-                field-name field-body))
+       (setcdr (car ew-decode-field-cache-buf) decoded)
        (cdar ew-decode-field-cache-buf)))))
 
 (defun ew-analyze-field-to-decode (field-name field-body)
@@ -461,32 +460,106 @@ each line is separated by CRLF."
 
 ;;;
 
+(defun ew-decode-field-interest-option-order (field-name field-body)
+  (let* ((ew-decode-sticked-encoded-word nil)
+        (ew-decode-quoted-encoded-word nil)
+        (ew-ignore-75bytes-limit nil)
+        (ew-ignore-76bytes-limit nil)
+        (ew-permit-sticked-comment nil)
+        (ew-permit-sticked-special nil)
+        (ew-permit-null-encoded-text nil)
+        (decoded (make-vector (lsh 1 (length ew-option-list)) nil))
+        tmp
+        i j k
+        )
+    (aset decoded 0 (list 0 (ew-decode-field field-name field-body)))
+    (setq i 1)
+    (while (< i (length decoded))
+      (ew-restore-boolean-options i)
+      (setq tmp (ew-decode-field field-name field-body))
+      (setq j 0)
+      (while (<= (lsh 1 j) i)
+       (unless (zerop (logand i (lsh 1 j)))
+         (setq k (logand i (lognot (lsh 1 j))))
+         (when (or (not (aref decoded i))
+                   (< (car (aref decoded i))
+                      (+ (if (equal (cadr (aref decoded k)) tmp) 0 1)
+                         (car (aref decoded k)))))
+           (aset decoded i
+                 (ew-cons*
+                  (+ (if (equal (cadr (aref decoded k)) tmp) 0 1)
+                     (car (aref decoded k)))
+                  tmp
+                  (nth j ew-option-list)
+                  (cddr (aref decoded k))))))
+       (setq j (1+ j)))
+      (setq i (1+ i)))
+    (reverse (cddr (aref decoded (1- (length decoded)))))))
+
 (defun ew-decode-field-test (field-name field-body)
+  (interactive
+   (list
+    (read-string "field-name:" (or (get-text-property (point) 'original-field-name)
+                                  (save-excursion
+                                    (end-of-line)
+                                    (and
+                                     (re-search-backward "^\\([!-9;-~]+\\):" nil t)
+                                     (match-string 1)))
+                                  ""))
+    (read-string "field-body:" (or (get-text-property (point) 'original-field-body)
+                                  (save-excursion
+                                    (end-of-line)
+                                    (and
+                                     (re-search-backward "^\\([!-9;-~]+\\):" nil t)
+                                     (progn
+                                       (goto-char (match-end 0))
+                                       (looking-at ".*\\(\n[ \t].*\\)*")
+                                       (ew-lf-crlf-to-crlf (match-string 0)))))
+                                  ""))))
   (with-output-to-temp-buffer "*DOODLE*"
     (save-excursion
+      (set-buffer standard-output)
       (let ((ew-decode-sticked-encoded-word nil)
            (ew-decode-quoted-encoded-word nil)
            (ew-ignore-75bytes-limit nil)
            (ew-ignore-76bytes-limit nil)
            (ew-permit-sticked-comment nil)
-           (ew-permit-sticked-special nil))
-       (princ field-name) (princ ":") (princ field-body) (princ "\n")
-       (princ (make-string fill-column ?-)) (princ "\n")
-       (princ field-name) (princ ":") (princ (ew-decode-field-no-cache field-name field-body)) (princ "\n")
-       (setq ew-ignore-76bytes-limit t) (princ "[ew-ignore-76bytes-limit -> t]\n")
-       (princ field-name) (princ ":") (princ (ew-decode-field-no-cache field-name field-body)) (princ "\n")
-       (setq ew-ignore-75bytes-limit t) (princ "[ew-ignore-75bytes-limit -> t]\n")
-       (princ field-name) (princ ":") (princ (ew-decode-field-no-cache field-name field-body)) (princ "\n")
-       (setq ew-permit-sticked-special t) (princ "[ew-ignore-76bytes-limit -> t]\n")
-       (princ field-name) (princ ":") (princ (ew-decode-field-no-cache field-name field-body)) (princ "\n")
-       (setq ew-permit-sticked-comment t) (princ "[ew-ignore-76bytes-comment -> t]\n")
-       (princ field-name) (princ ":") (princ (ew-decode-field-no-cache field-name field-body)) (princ "\n")
-       (setq ew-decode-sticked-encoded-word t) (princ "[ew-decode-sticked-encoded-word -> t]\n")
-       (princ field-name) (princ ":") (princ (ew-decode-field-no-cache field-name field-body)) (princ "\n")
-       (setq ew-decode-quoted-encoded-word t) (princ "[ew-decode-quoted-encoded-word -> t]\n")
-       (princ field-name) (princ ":") (princ (ew-decode-field-no-cache field-name field-body)) (princ "\n")
-       ;; ew-permit-null-encoded-text is not changable when runtime.
-       ))))
+           (ew-permit-sticked-special nil)
+           (ew-permit-null-encoded-text nil)
+           (options
+            '(ew-ignore-76bytes-limit
+              ew-ignore-75bytes-limit
+              ew-permit-sticked-special
+              ew-permit-sticked-comment
+              ew-decode-sticked-encoded-word
+              ew-decode-quoted-encoded-word
+              ew-permit-null-encoded-text
+              ))
+           d1 d2)
+       (when (<= 16 (prefix-numeric-value current-prefix-arg))
+         (setq options (ew-decode-field-interest-option-order field-name field-body)))
+       (setq d1 (ew-decode-field-no-cache field-name field-body))
+       (insert field-name ":" field-body "\n"
+               (make-string 76 ?-) "\n"
+               field-name ":" d1 "\n")
+       (while options
+         (set (car options) t)
+         (insert (format "-- %s -> t\n" (car options)))
+         (setq d2 (ew-decode-field-no-cache field-name field-body))
+         (unless (equal d1 d2)
+           (insert field-name ":" d2 "\n")
+           (setq d1 d2))
+         (setq options (cdr options)))
+       (insert (make-string 76 ?-) "\n")
+       (when (<= 4 (prefix-numeric-value current-prefix-arg))
+         (mapcar
+          (lambda (frag)
+            (insert (format "%-15s %S\n"
+                            (substring (symbol-name (get frag 'type)) 3)
+                            (symbol-name frag)))
+            nil)
+          (ew-frag-list (ew-analyze-field-to-decode field-name field-body)))
+         (insert (make-string 76 ?-) "\n"))))))
 
 ;;;
 
@@ -508,4 +581,16 @@ each line is separated by CRLF."
 (ew-decode-field "To" "\"A\\BC\" <foo@bar>")
 (ew-decode-field "To" "\"\e\\$\\B\\$\\\"\e\\(\\B\" <foo@bar>")
 
+(ew-decode-field-test "Subject" " =?US-ASCII?Q??=?US-ASCII?Q?a?=")
+(ew-decode-field-test "Subject" " =?xUS-ASCII?Q??=?xUS-ASCII?Q?a?=")
+(ew-decode-field-test "Subject" " =?+US-ASCII?Q??=?+US-ASCII?Q?a?=")
+
+(ew-decode-field "From"" ()=?+US-ASCII?Q??=?+US-ASCII?Q?a?= =?US-ASCII?Q??= <akr@foo> (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)")
+
+(let ((ew-default-mime-charset 'iso-2022-jp-2))
+  (ew-decode-field-no-cache
+     "From" "\"Cl\351ment Brousset\" <cbrousset@staffandline.com>"))
+
+(ew-decode-field-no-cache "From" " \"Jacek \\\"Jaytee\\\" Szyd\263owski\" <jaytee@friko.onet.pl>")
+
 )