* FLIM-ELS (flim-modules): Add `ew-var' and reorder.
[elisp/flim.git] / ew-unit.el
1 (require 'closure)
2 (require 'ew-line)
3 (require 'ew-quote)
4 (require 'mel)
5
6 (provide 'ew-unit)
7
8 (defconst ew-anchored-encoded-word-regexp
9   (concat "\\`" ew-encoded-word-regexp "\\'"))
10
11 (defconst ew-b-regexp
12   (concat "\\`\\("
13           "[A-Za-z0-9+/]"
14           "[A-Za-z0-9+/]"
15           "[A-Za-z0-9+/]"
16           "[A-Za-z0-9+/]"
17           "\\)*"
18           "[A-Za-z0-9+/]"
19           "[A-Za-z0-9+/]"
20           "\\(==\\|"
21           "[A-Za-z0-9+/]"
22           "[A-Za-z0-9+/=]"
23           "\\)\\'"))
24
25 (defconst ew-q-regexp "\\`\\([^=?]\\|=[0-9A-Fa-f][0-9A-Fa-f]\\)*\\'")
26
27 (defconst ew-byte-decoder-alist
28   '(("B" . ew-b-decode)
29     ("Q" . ew-q-decode)))
30
31 (defconst ew-byte-checker-alist
32   '(("B" . ew-b-check)
33     ("Q" . ew-q-check)))
34
35 (defun ew-b-check (encoding encoded-text) (string-match ew-b-regexp encoded-text))
36 (defun ew-q-check (encoding encoded-text) (string-match ew-q-regexp encoded-text))
37
38 (defun ew-eword-p (str)
39   (let ((len (length str)))
40     (and
41      (<= 3 len)
42      (string= (substring str 0 2) "=?")
43      (string= (substring str (- len 2) len) "?="))))
44
45 (defun ew-decode-eword (str &optional eword-filter1 eword-filter2)
46   (if (string-match ew-anchored-encoded-word-regexp str)
47       (let ((charset (match-string 1 str))
48             (encoding (match-string 2 str))
49             (encoded-text (match-string 3 str))
50             bdec cdec
51             bcheck
52             tmp)
53         (if (and (setq bdec (ew-byte-decoder encoding))
54                  (setq cdec (ew-char-decoder charset)))
55             (if (or (null (setq bcheck (ew-byte-checker encoding)))
56                     (funcall bcheck encoding encoded-text))
57                 (progn
58                   (setq tmp (closure-call cdec (funcall bdec encoded-text)))
59                   (when eword-filter1 (setq tmp (closure-call eword-filter1 tmp)))
60                   (setq tmp (ew-quote tmp))
61                   (when eword-filter2 (setq tmp (closure-call eword-filter2 tmp)))
62                   tmp)
63               (ew-quote str))
64           (ew-quote-eword charset encoding encoded-text)))
65     (ew-quote str)))
66
67 (defun ew-byte-decoder (encoding)
68   (cdr (assoc (upcase encoding) ew-byte-decoder-alist)))
69
70 (defun ew-byte-checker (encoding)
71   (cdr (assoc (upcase encoding) ew-byte-checker-alist)))
72
73 (defalias 'ew-b-decode 'base64-decode-string)
74 (defalias 'ew-q-decode 'q-encoding-decode-string)
75
76 (defconst ew-charset-aliases
77   '((us-ascii . iso-8859-1)
78     (iso-2022-jp-2 . iso-2022-7bit-ss2)))
79 (defun ew-char-decoder (charset)
80   (catch 'return 
81     (setq charset (downcase charset))
82     (let ((sym (intern charset))
83           tmp cs)
84       (when (setq tmp (assq sym ew-charset-aliases))
85         (setq sym (cdr tmp)))
86       (setq cs (intern (concat (symbol-name sym) "-unix")))
87       (when (coding-system-p cs)
88         (throw 'return
89                (closure-make (lambda (str) (decode-coding-string str cs)) cs)))
90       nil)))