Function: rfc2047-fold-region
rfc2047-fold-region is a byte-compiled function defined in
rfc2047.el.gz.
Signature
(rfc2047-fold-region B E)
Documentation
Fold long lines in region B to E.
Source Code
;; Defined in /usr/src/emacs/lisp/mail/rfc2047.el.gz
(defun rfc2047-fold-region (b e)
"Fold long lines in region B to E."
(save-restriction
(narrow-to-region b e)
(goto-char (point-min))
(let* ((break nil)
(qword-break nil)
(bol (save-restriction
(widen)
(line-beginning-position)))
;; This function is either called with the Header: name in
;; the region or not. If it's not in the region, then we
;; may already have a space.
(first (or (= bol (point))
(save-restriction
(widen)
(save-excursion
(not (re-search-backward "[ \t]" bol t)))))))
(while (not (eobp))
(when (and (or break qword-break)
(> (- (point) bol) 76))
;; We have a line longer than 76 characters, so break the
;; line.
(setq bol (rfc2047--break-line break qword-break)
break nil
qword-break nil))
;; See whether we're at a point where we can break the line
;; (if it turns out to be too long).
(cond
;; New line, so there's nothing to break.
((eq (char-after) ?\n)
(forward-char 1)
(setq bol (point)
break nil
qword-break nil)
(skip-chars-forward " \t")
(unless (or (eobp) (eq (char-after) ?\n))
(forward-char 1)))
;; CR in CRLF; shouldn't really as this function shouldn't be
;; called after encoding for line transmission.
((eq (char-after) ?\r)
(forward-char 1))
;; Whitespace -- possible break point.
((memq (char-after) '(? ?\t))
(skip-chars-forward " \t")
;; Don't break just after the header name.
(if first
(setq first nil)
(setq break (point))))
;; If the header has been encoded (with RFC2047 encoding,
;; which looks like "=?utf-8?Q?F=C3=B3?=".
((not break)
(if (not (looking-at "=\\?[^=]"))
(if (eq (char-after) ?=)
(forward-char 1)
(skip-chars-forward "^ \t\n\r="))
;; Don't break at the start of the field.
(unless (= (point) b)
(setq qword-break (point)))
(skip-chars-forward "^ \t\n\r")))
;; Look for the next LWSP (i.e., whitespace character).
(t
(skip-chars-forward "^ \t\n\r"))))
(when (and (or break qword-break)
(> (- (point) bol) 76))
;; Finally, after the loop, we have a line longer than 76
;; characters, so break the line.
(rfc2047--break-line break qword-break)))))