(> (- (point) bol) 76))
;; We have a line longer than 76 characters, so break the
;; line.
- (goto-char (or break qword-break))
- (setq break nil
- qword-break nil)
- (skip-chars-backward " \t")
- (if (looking-at "[ \t]")
- (insert ?\n)
- (insert "\n "))
- (setq bol (1- (point)))
- ;; Don't break before the first non-LWSP characters.
- (skip-chars-forward " \t")
- (unless (eobp)
- (forward-char 1)))
+ (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
(t
(skip-chars-forward "^ \t\n\r")))
(setq first nil))
- ;; Finally, after the loop, we have a line longer than 76
- ;; characters, so break the line.
(when (and (or break qword-break)
(> (- (point) bol) 76))
- (goto-char (or break qword-break))
- (setq break nil
- qword-break nil)
- (if (or (> 0 (skip-chars-backward " \t"))
- (looking-at "[ \t]"))
- (insert ?\n)
- (insert "\n "))
- (setq bol (1- (point)))
- ;; Don't break before the first non-LWSP characters.
- (skip-chars-forward " \t")
- (unless (eobp)
- (forward-char 1))))))
+ ;; Finally, after the loop, we have a line longer than 76
+ ;; characters, so break the line.
+ (rfc2047--break-line break qword-break)))))
+
+(defun rfc2047--break-line (break qword-break)
+ (goto-char (or break qword-break))
+ (skip-chars-backward " \t")
+ (if (looking-at "[ \t]")
+ (insert ?\n)
+ (insert "\n "))
+ (prog1
+ ;; Return beginning-of-line.
+ (1- (point))
+ ;; Don't break before the first non-LWSP characters.
+ (skip-chars-forward " \t")
+ (unless (eobp)
+ (forward-char 1))))
(defun rfc2047-unfold-field ()
"Fold the current line."
--- /dev/null
+;;; rfc2047-tests.el --- tests for rfc2047.el -*- lexical-binding: t -*-
+
+;; Copyright (C) 2019 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'rfc2047)
+
+(ert-deftest test-rfc2047-fold-short ()
+ (with-temp-buffer
+ (insert "Organization: Lots Of Short Words Here Lots Of Short Words Here Lots Of Short Words Here\n")
+ (goto-char (point-min))
+ (rfc2047-fold-field)
+ (should (equal (buffer-string)
+ "Organization: Lots Of Short Words Here Lots Of Short Words Here Lots Of
+ Short Words Here
+"))))
+
+(ert-deftest test-rfc2047-fold-encoded ()
+ (with-temp-buffer
+ (insert "Subject: This is =?utf-8?Q?=C3=A1?= long subject that's =?utf-8?Q?v=C3=A9ry?= long and =?utf-8?Q?ver=C3=BD?= encoded yes indeed it =?utf-8?Q?=C3=ADs?=\n")
+ (goto-char (point-min))
+ (rfc2047-fold-field)
+ (should (equal (buffer-string)
+ "Subject: This is =?utf-8?Q?=C3=A1?= long subject that's
+ =?utf-8?Q?v=C3=A9ry?= long and =?utf-8?Q?ver=C3=BD?= encoded yes indeed it
+ =?utf-8?Q?=C3=ADs?=
+"))))
+
+;;; rfc2047-tests.el ends here