From 4438459eaa6cccdac2cfcc8f7d5f248bfe8d1edf Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Fri, 12 Jul 2019 15:48:34 +0200 Subject: [PATCH] Refactor rfc2047-fold-region slightly and add a couple of tests * lisp/mail/rfc2047.el (rfc2047--break-line): Refactor out to avoid code repetition... (rfc2047-fold-region): ... from this function. --- lisp/mail/rfc2047.el | 46 ++++++++++++++------------------- test/lisp/mail/rfc2047-tests.el | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 test/lisp/mail/rfc2047-tests.el diff --git a/lisp/mail/rfc2047.el b/lisp/mail/rfc2047.el index 5f2abc435d1..9de6f02edfb 100644 --- a/lisp/mail/rfc2047.el +++ b/lisp/mail/rfc2047.el @@ -743,18 +743,9 @@ Point moves to the end of the region." (> (- (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 @@ -791,22 +782,25 @@ Point moves to the end of the region." (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." diff --git a/test/lisp/mail/rfc2047-tests.el b/test/lisp/mail/rfc2047-tests.el new file mode 100644 index 00000000000..8f7b345e71e --- /dev/null +++ b/test/lisp/mail/rfc2047-tests.el @@ -0,0 +1,46 @@ +;;; 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 . + +;;; 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 -- 2.39.5