emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master 4438459 2/3: Refactor rfc2047-fold-region slightly


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] master 4438459 2/3: Refactor rfc2047-fold-region slightly and add a couple of tests
Date: Fri, 12 Jul 2019 09:59:48 -0400 (EDT)

branch: master
commit 4438459eaa6cccdac2cfcc8f7d5f248bfe8d1edf
Author: Lars Ingebrigtsen <address@hidden>
Commit: Lars Ingebrigtsen <address@hidden>

    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(-)

diff --git a/lisp/mail/rfc2047.el b/lisp/mail/rfc2047.el
index 5f2abc4..9de6f02 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 0000000..8f7b345
--- /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 <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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]