+(defun elmo-expand-newtext (newtext original)
+ (let ((len (length newtext))
+ (pos 0)
+ c expanded beg N did-expand)
+ (while (< pos len)
+ (setq beg pos)
+ (while (and (< pos len)
+ (not (= (aref newtext pos) ?\\)))
+ (setq pos (1+ pos)))
+ (unless (= beg pos)
+ (push (substring newtext beg pos) expanded))
+ (when (< pos len)
+ ;; We hit a \; expand it.
+ (setq did-expand t
+ pos (1+ pos)
+ c (aref newtext pos))
+ (if (not (or (= c ?\&)
+ (and (>= c ?1)
+ (<= c ?9))))
+ ;; \ followed by some character we don't expand.
+ (push (char-to-string c) expanded)
+ ;; \& or \N
+ (if (= c ?\&)
+ (setq N 0)
+ (setq N (- c ?0)))
+ (when (match-beginning N)
+ (push (substring original (match-beginning N) (match-end N))
+ expanded))))
+ (setq pos (1+ pos)))
+ (if did-expand
+ (apply (function concat) (nreverse expanded))
+ newtext)))
+
+;;; Folder parser utils.
+(defun elmo-parse-token (string &optional seps)
+ "Parse atom from STRING using SEPS as a string of separator char list."
+ (let ((len (length string))
+ (seps (and seps (string-to-char-list seps)))
+ (i 0)
+ (sep nil)
+ content c in)
+ (if (eq len 0)
+ (cons "" "")
+ (while (and (< i len) (or in (null sep)))
+ (setq c (aref string i))
+ (cond
+ ((and in (eq c ?\\))
+ (setq i (1+ i)
+ content (cons (aref string i) content)
+ i (1+ i)))
+ ((eq c ?\")
+ (setq in (not in)
+ i (1+ i)))
+ (in (setq content (cons c content)
+ i (1+ i)))
+ ((memq c seps)
+ (setq sep c))
+ (t (setq content (cons c content)
+ i (1+ i)))))
+ (if in (error "Parse error in quoted"))
+ (cons (if (null content) "" (char-list-to-string (nreverse content)))
+ (substring string i)))))
+
+(defun elmo-parse-prefixed-element (prefix string &optional seps)
+ (if (and (not (eq (length string) 0))
+ (eq (aref string 0) prefix))
+ (elmo-parse-token (substring string 1) seps)
+ (cons "" string)))
+