+
+;;; @ text/richtext <-> text/enriched converter
+;;;
+
+(defun richtext-to-enriched-region (beg end)
+ "Convert the region of text/richtext style to text/enriched style."
+ (save-excursion
+ (save-restriction
+ (narrow-to-region beg end)
+ (goto-char (point-min))
+ (let (b e i)
+ (while (re-search-forward "[ \t]*<comment>" nil t)
+ (setq b (match-beginning 0))
+ (delete-region b
+ (if (re-search-forward "</comment>[ \t]*" nil t)
+ (match-end 0)
+ (point-max)
+ ))
+ )
+ (goto-char (point-min))
+ (while (re-search-forward "\n\n+" nil t)
+ (replace-match "\n")
+ )
+ (goto-char (point-min))
+ (while (re-search-forward "[ \t\n]*<nl>[ \t\n]*" nil t)
+ (setq b (match-beginning 0))
+ (setq e (match-end 0))
+ (setq i 1)
+ (while (looking-at "[ \t\n]*<nl>[ \t\n]*")
+ (setq e (match-end 0))
+ (setq i (1+ i))
+ (goto-char e)
+ )
+ (delete-region b e)
+ (while (>= i 0)
+ (insert "\n")
+ (setq i (1- i))
+ ))
+ (goto-char (point-min))
+ (while (search-forward "<lt>" nil t)
+ (replace-match "<<")
+ )
+ ))))
+
+(defun enriched-to-richtext-region (beg end)
+ "Convert the region of text/enriched style to text/richtext style."
+ (save-excursion
+ (save-restriction
+ (goto-char beg)
+ (and (search-forward "text/enriched")
+ (replace-match "text/richtext"))
+ (search-forward "\n\n")
+ (narrow-to-region (match-end 0) end)
+ (let (str n)
+ (goto-char (point-min))
+ (while (re-search-forward "\n\n+" nil t)
+ (setq str (buffer-substring (match-beginning 0)
+ (match-end 0)))
+ (setq n (1- (length str)))
+ (setq str "")
+ (while (> n 0)
+ (setq str (concat str "<nl>\n"))
+ (setq n (1- n))
+ )
+ (replace-match str)
+ )
+ (goto-char (point-min))
+ (while (search-forward "<<" nil t)
+ (replace-match "<lt>")
+ )
+ ))))
+
+
+;;; @ encoder and decoder
+;;;
+
+(defun richtext-decode (beg end)
+ (save-restriction
+ (narrow-to-region beg end)
+ (richtext-to-enriched-region beg (point-max))
+ (enriched-decode beg (point-max))
+ ))
+
+;; (defun richtext-encode (beg end)
+;; (save-restriction
+;; (narrow-to-region beg end)
+;; (enriched-encode beg (point-max))
+;; (enriched-to-richtext-region beg (point-max))
+;; ))
+
+
+;;; @ end
+;;;
+