- (narrow-to-region start end)
- (goto-char start)
- (let ((col 0))
- (while (< (point)(point-max))
- (cond ((>= col 75)
- (insert "=\n")
- (setq col 0)
- )
- ((looking-at-as-unibyte "^From ")
- (replace-match "=46rom ")
- (backward-char 1)
- (setq col (+ col 6))
- )
- ((looking-at-as-unibyte "[ \t]\n")
- (forward-char 1)
- (insert "=\n")
- (forward-char 1)
- (setq col 0)
- )
- (t
- (let ((chr (char-after (point))))
- (cond ((= chr ?\n)
- (forward-char 1)
- (setq col 0)
- )
- ((or (= chr ?\t)
- (and (<= 32 chr)(/= chr ?=)(< chr 127))
- )
- (forward-char 1)
- (setq col (1+ col))
- )
- ((>= col 73)
- (insert "=\n")
- (setq col 0)
- )
- (t
- (delete-char 1)
- (insert (quoted-printable-quote-char chr))
- (setq col (+ col 3))
- ))
- )))
- )))))
+ (narrow-to-region (goto-char start) end)
+ (let ((col 0)
+ chr)
+ (while (not (eobp))
+ (cond
+ ((>= col 75) ; soft line break.
+ (insert "=\n")
+ (setq col 0))
+ ((eolp) ; end of line.
+ (forward-char)
+ (setq col 0))
+ (t
+ (setq chr (char-after (point)))
+ (cond
+ ((and (memq chr '(? ?\t)) ; encode WSP char before CRLF.
+ (eq (char-after (1+ (point))) ?\n))
+ (forward-char)
+ (insert "=\n")
+ (forward-char)
+ (setq col 0))
+ ((and (bolp) ; "^From " is not safe.
+ (eq chr ?F)
+ (eq (char-after (1+ (point))) ?r)
+ (eq (char-after (+ 2 (point))) ?o)
+ (eq (char-after (+ 3 (point))) ?m)
+ (eq (char-after (+ 4 (point))) ? ))
+ (delete-region (point)(1+ (point)))
+ (insert "=46") ; moved to ?r.
+ (forward-char 4) ; skip "rom ".
+ (setq col 7))
+ ((or (= chr ?\t) ; skip safe char.
+ (and (<= 32 chr)(/= chr ?=)(< chr 127)))
+ (forward-char)
+ (setq col (1+ col)))
+ ((>= col 73) ; soft line break.
+ (insert "=\n")
+ (setq col 0))
+ (t ; encode unsafe char.
+ (delete-region (point)(1+ (point)))
+ ;; (insert (quoted-printable-quote-char chr))
+ (insert
+ ?=
+ (aref quoted-printable-hex-chars (ash chr -4))
+ (aref quoted-printable-hex-chars (logand chr 15)))
+ (setq col (+ col 3)))))))))))