]> git.eshelyaron.com Git - emacs.git/commitdiff
Refactor rfc2047-fold-region slightly and add a couple of tests
authorLars Ingebrigtsen <larsi@gnus.org>
Fri, 12 Jul 2019 13:48:34 +0000 (15:48 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Fri, 12 Jul 2019 13:48:34 +0000 (15:48 +0200)
* lisp/mail/rfc2047.el (rfc2047--break-line): Refactor out to
avoid code repetition...
(rfc2047-fold-region): ... from this function.

lisp/mail/rfc2047.el
test/lisp/mail/rfc2047-tests.el [new file with mode: 0644]

index 5f2abc435d1c50eccd026391bf86fc783c6450c9..9de6f02edfb3cf33c21812f96a5772e061497e4f 100644 (file)
@@ -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 (file)
index 0000000..8f7b345
--- /dev/null
@@ -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 <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