+;;; @ internal utilities
+;;;
+
+(defun eword-decode-first-encoded-words (string
+ eword-regexp
+ after-regexp
+ &optional must-unfold)
+ "Decode MIME encoded-words in beginning of STRING.
+
+EWORD-REGEXP is the regexp that matches a encoded-word.
+Usual value is
+eword-encoded-word-in-unstructured-regexp,
+eword-encoded-text-in-phrase-regexp,
+eword-encoded-word-in-comment-regexp or
+eword-encoded-word-in-quoted-string-regexp.
+
+AFTER-REGEXP is the regexp that matches a after encoded-word.
+Usual value is
+eword-after-encoded-word-in-unstructured-regexp,
+eword-after-encoded-text-in-phrase-regexp,
+eword-after-encoded-word-in-comment-regexp or
+eword-after-encoded-word-in-quoted-string-regexp.
+
+If beginning of STRING matches EWORD-REGEXP with AFTER-REGEXP,
+returns a cons cell of decoded string(sequence of characters) and
+the rest(sequence of octets).
+
+If beginning of STRING does not matches EWORD-REGEXP and AFTER-REGEXP,
+returns nil.
+
+If an encoded-word is broken or your emacs implementation can not
+decode the charset included in it, it is returned in decoded part
+as encoded-word form.
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-words (generated by bad manner MUA
+such as a version of Net$cape)."
+ (if eword-decode-sticked-encoded-word (setq after-regexp ""))
+ (let* ((between-ewords-regexp
+ (if eword-decode-sticked-encoded-word
+ "\\(\n?[ \t]\\)*"
+ "\\(\n?[ \t]\\)+"))
+ (between-ewords-eword-after-regexp
+ (concat "\\`\\(" between-ewords-regexp "\\)"
+ "\\(" eword-regexp "\\)"
+ after-regexp))
+ (eword-after-regexp
+ (concat "\\`\\(" eword-regexp "\\)" after-regexp))
+ (src string) ; sequence of octets.
+ (dst "")) ; sequence of characters.
+ (if (string-match eword-after-regexp src)
+ (let* (p
+ (q (match-end 1))
+ (ew (substring src 0 q))
+ (dw (eword-decode-encoded-word ew must-unfold)))
+ (setq dst (concat dst dw)
+ src (substring src q))
+ (if (not (string= ew dw))
+ (progn
+ (while
+ (and
+ (string-match between-ewords-eword-after-regexp src)
+ (progn
+ (setq p (match-end 1)
+ q (match-end 3)
+ ew (substring src p q)
+ dw (eword-decode-encoded-word ew must-unfold))
+ (if (string= ew dw)
+ (progn
+ (setq dst (concat dst (substring src 0 q))
+ src (substring src q))
+ nil)
+ t)))
+ (setq dst (concat dst dw)
+ src (substring src q)))))
+ (cons dst src))
+ nil)))
+
+(defun eword-decode-entire-string (string
+ eword-regexp
+ after-regexp
+ safe-regexp
+ escape ; ?\\ or nil.
+ delimiters ; list of chars.
+ chars-must-be-quote
+ must-unfold
+ code-conversion)
+ (if (and code-conversion
+ (not (mime-charset-to-coding-system code-conversion)))
+ (setq code-conversion default-mime-charset))
+ (let ((equal-safe-regexp (concat "\\`=?" safe-regexp))
+ (dst "")
+ (buf "")
+ (src string)
+ (ew-enable t))
+ (while (< 0 (length src))
+ (let ((ch (aref src 0))
+ (decoded (and
+ ew-enable
+ (eword-decode-first-encoded-words src
+ eword-regexp after-regexp must-unfold))))
+ (if (and (not (string= buf ""))
+ (or decoded (memq ch delimiters)))
+ (setq dst (concat dst
+ (std11-wrap-as-quoted-pairs
+ (decode-mime-charset-string buf code-conversion)
+ chars-must-be-quote))
+ buf ""))
+ (cond
+ (decoded
+ (setq dst (concat dst
+ (std11-wrap-as-quoted-pairs
+ (car decoded)
+ chars-must-be-quote))
+ src (cdr decoded)))
+ ((memq ch delimiters)
+ (setq dst (concat dst (list ch))
+ src (substring src 1)
+ ew-enable t))
+ ((eq ch escape)
+ (setq buf (concat buf (list (aref src 1)))
+ src (substring src 2)
+ ew-enable t))
+ ((string-match "\\`[ \t\n]+" src)
+ (setq buf (concat buf (substring src 0 (match-end 0)))
+ src (substring src (match-end 0))
+ ew-enable t))
+ ((and (string-match equal-safe-regexp src)
+ (< 0 (match-end 0)))
+ (setq buf (concat buf (substring src 0 (match-end 0)))
+ src (substring src (match-end 0))
+ ew-enable eword-decode-sticked-encoded-word))
+ (t (error "something wrong")))))
+ (if (not (string= buf ""))
+ (setq dst (concat dst
+ (std11-wrap-as-quoted-pairs
+ (decode-mime-charset-string buf code-conversion)
+ chars-must-be-quote))))
+ dst))
+
+