* mime-def.el (mime-library-product): Fix typo.
[elisp/flim.git] / ew-unit.el
1 (require 'closure)
2 (require 'ew-line)
3 (require 'ew-quote)
4 (require 'ew-bq)
5
6 (provide 'ew-unit)
7
8 (defun ew-b-check (encoding encoded-text) (string-match ew-b-regexp encoded-text))
9 (defun ew-q-check (encoding encoded-text) (string-match ew-q-regexp encoded-text))
10
11 (defsubst ew-eword-p (str)
12   (let ((len (length str)))
13     (and
14      (<= 3 len)
15      (eq (aref str 0) ?=)
16      (eq (aref str 1) ??)
17      (eq (aref str (- len 2)) ??)
18      (eq (aref str (1- len)) ?=))))
19
20 (defun ew-decode-eword (str)
21   (if (string-match ew-anchored-encoded-word-regexp str)
22       (let ((charset (match-string 1 str))
23             (encoding (match-string 2 str))
24             (encoded-text (match-string 3 str))
25             bdec cdec
26             bcheck
27             tmp)
28         (if (or ew-permit-null-encoded-text
29                 (< 0 (length encoded-text)))
30             (if (and (setq bdec (ew-byte-decoder encoding))
31                      (setq cdec (ew-char-decoder charset)))
32                 (if (or (null (setq bcheck (ew-byte-checker encoding)))
33                         (funcall bcheck encoding encoded-text))
34                     (ew-quote (closure-call cdec (funcall bdec encoded-text)))
35                   (ew-quote str))
36               (ew-quote-eword charset encoding encoded-text))
37           (ew-quote str)))
38     (ew-quote str)))
39
40 (defun ew-byte-decoder (encoding)
41   (cdr (assoc (upcase encoding) ew-byte-decoder-alist)))
42
43 (defun ew-byte-checker (encoding)
44   (cdr (assoc (upcase encoding) ew-byte-checker-alist)))
45
46 (defun ew-char-decoder (charset)
47   (let ((sym (intern (downcase charset))))
48     (when (mime-charset-to-coding-system sym 'LF)
49       (closure-make
50        (lambda (str) (decode-mime-charset-string str sym 'LF))
51        sym))))