aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2019-07-12 15:48:34 +0200
committerLars Ingebrigtsen2019-07-12 15:48:34 +0200
commit4438459eaa6cccdac2cfcc8f7d5f248bfe8d1edf (patch)
treec27964fd802ef038874eef5dd38a773f61dee16c
parentc281b9a121a251b19e3e90b67ca85fbe834b3bc2 (diff)
downloademacs-4438459eaa6cccdac2cfcc8f7d5f248bfe8d1edf.tar.gz
emacs-4438459eaa6cccdac2cfcc8f7d5f248bfe8d1edf.zip
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.
-rw-r--r--lisp/mail/rfc2047.el46
-rw-r--r--test/lisp/mail/rfc2047-tests.el46
2 files changed, 66 insertions, 26 deletions
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."
743 (> (- (point) bol) 76)) 743 (> (- (point) bol) 76))
744 ;; We have a line longer than 76 characters, so break the 744 ;; We have a line longer than 76 characters, so break the
745 ;; line. 745 ;; line.
746 (goto-char (or break qword-break)) 746 (setq bol (rfc2047--break-line break qword-break)
747 (setq break nil 747 break nil
748 qword-break nil) 748 qword-break nil))
749 (skip-chars-backward " \t")
750 (if (looking-at "[ \t]")
751 (insert ?\n)
752 (insert "\n "))
753 (setq bol (1- (point)))
754 ;; Don't break before the first non-LWSP characters.
755 (skip-chars-forward " \t")
756 (unless (eobp)
757 (forward-char 1)))
758 ;; See whether we're at a point where we can break the line 749 ;; See whether we're at a point where we can break the line
759 ;; (if it turns out to be too long). 750 ;; (if it turns out to be too long).
760 (cond 751 (cond
@@ -791,22 +782,25 @@ Point moves to the end of the region."
791 (t 782 (t
792 (skip-chars-forward "^ \t\n\r"))) 783 (skip-chars-forward "^ \t\n\r")))
793 (setq first nil)) 784 (setq first nil))
794 ;; Finally, after the loop, we have a line longer than 76
795 ;; characters, so break the line.
796 (when (and (or break qword-break) 785 (when (and (or break qword-break)
797 (> (- (point) bol) 76)) 786 (> (- (point) bol) 76))
798 (goto-char (or break qword-break)) 787 ;; Finally, after the loop, we have a line longer than 76
799 (setq break nil 788 ;; characters, so break the line.
800 qword-break nil) 789 (rfc2047--break-line break qword-break)))))
801 (if (or (> 0 (skip-chars-backward " \t")) 790
802 (looking-at "[ \t]")) 791(defun rfc2047--break-line (break qword-break)
803 (insert ?\n) 792 (goto-char (or break qword-break))
804 (insert "\n ")) 793 (skip-chars-backward " \t")
805 (setq bol (1- (point))) 794 (if (looking-at "[ \t]")
806 ;; Don't break before the first non-LWSP characters. 795 (insert ?\n)
807 (skip-chars-forward " \t") 796 (insert "\n "))
808 (unless (eobp) 797 (prog1
809 (forward-char 1)))))) 798 ;; Return beginning-of-line.
799 (1- (point))
800 ;; Don't break before the first non-LWSP characters.
801 (skip-chars-forward " \t")
802 (unless (eobp)
803 (forward-char 1))))
810 804
811(defun rfc2047-unfold-field () 805(defun rfc2047-unfold-field ()
812 "Fold the current line." 806 "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 @@
1;;; rfc2047-tests.el --- tests for rfc2047.el -*- lexical-binding: t -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Code:
21
22(require 'ert)
23(require 'rfc2047)
24
25(ert-deftest test-rfc2047-fold-short ()
26 (with-temp-buffer
27 (insert "Organization: Lots Of Short Words Here Lots Of Short Words Here Lots Of Short Words Here\n")
28 (goto-char (point-min))
29 (rfc2047-fold-field)
30 (should (equal (buffer-string)
31 "Organization: Lots Of Short Words Here Lots Of Short Words Here Lots Of
32 Short Words Here
33"))))
34
35(ert-deftest test-rfc2047-fold-encoded ()
36 (with-temp-buffer
37 (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")
38 (goto-char (point-min))
39 (rfc2047-fold-field)
40 (should (equal (buffer-string)
41 "Subject: This is =?utf-8?Q?=C3=A1?= long subject that's
42 =?utf-8?Q?v=C3=A9ry?= long and =?utf-8?Q?ver=C3=BD?= encoded yes indeed it
43 =?utf-8?Q?=C3=ADs?=
44"))))
45
46;;; rfc2047-tests.el ends here