emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/ebdb aa15250 252/350: Change newline approach in vCard


From: Eric Abrahamsen
Subject: [elpa] externals/ebdb aa15250 252/350: Change newline approach in vCard handling
Date: Mon, 14 Aug 2017 11:46:48 -0400 (EDT)

branch: externals/ebdb
commit aa15250ea663ffe3ab82f25bea7ca87605ee6e9c
Author: Eric Abrahamsen <address@hidden>
Commit: Eric Abrahamsen <address@hidden>

    Change newline approach in vCard handling
    
    * ebdb-vcard.el (ebdb-formatter-vcard): Instead of actually sticking
      CRLFs in the code everywhere, do it right: use Emacs' internal
      conventions, and only convert to 'utf-8-dos at the last minute, when
      saving to file. Go back through all code and revert to using plain
      \n. The formatting routine is already prepared to set the file
      coding system correctly.
      (ebdb-formatter-vcard-40): Don't override coding-system here.
      (ebdb-vcard-escape): Fix escaping.
    * ebdb-test.el (ebdb-vcard-escape/unescape): I had written a broken
      test to test broken code. Fix the test.
      (ebdb-vcard-fold/unfold): Also get literal CRLFs out of here, back
      to plain \n.
    
    Lastly, after this, we need to find the right spot to run the
    folding/unfolding routine.
---
 ebdb-test.el  | 27 ++++++++++++++++-----------
 ebdb-vcard.el | 28 ++++++++++++++--------------
 2 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/ebdb-test.el b/ebdb-test.el
index 9ff328d..718ece0 100644
--- a/ebdb-test.el
+++ b/ebdb-test.el
@@ -365,7 +365,7 @@
                 "Marry\\, uncle"))
 
   ;; Don't double-escape, part II
-  (should (equal (ebdb-vcard-escape "Marry\n uncle!")
+  (should (equal (ebdb-vcard-escape "Marry\\n uncle!")
                 "Marry\\n uncle!"))
 
   (should (equal (ebdb-vcard-escape "Mine 
@@ -386,22 +386,27 @@ uncle"))
 
 (ert-deftest ebdb-vcard-fold/unfold ()
   "Test line-length folding/unfolding."
-  (let ((short-lines "For sale:\r\nBaby shoes,\r\nNever used.")
+  (let ((short-lines "For sale:
+Baby shoes,
+Never used.")
        (long-lines
-        "Turns out seventy five bytes is a lot of bytes when you just have to 
keep typing and typing\r\nand typing.")
+        "Turns out seventy five bytes is a lot of bytes when you just have to 
keep typing and typing
+and typing.")
        (multibyte-lines
-        "然后还要用中文写一行没完没了的话。\r\n其实先要来一个短的,然后一行特别长的,那就是现在这行,\r\n然后可以再有一个短的"))
+        "然后还要用中文写一行没完没了的话。
+其实先要来一个短的,然后一行特别长的,那就是现在这行,
+然后可以再有一个短的"))
     (should (equal (ebdb-vcard-fold-lines short-lines)
-                  "For sale:
-Baby shoes,
+                  "For sale:
+Baby shoes,
 Never used."))
     (should (equal (ebdb-vcard-unfold-lines
                    (ebdb-vcard-fold-lines short-lines))
                   short-lines))
     (should
      (equal (ebdb-vcard-fold-lines long-lines)
-           "Turns out seventy five bytes is a lot of bytes when you just have 
to keep t
- yping and typing
+           "Turns out seventy five bytes is a lot of bytes when you just have 
to keep t
+ yping and typing
 and typing."))
     (should
      (equal (ebdb-vcard-unfold-lines
@@ -409,9 +414,9 @@ and typing."))
            long-lines))
     (should
      (equal (ebdb-vcard-fold-lines multibyte-lines)
-           "然后还要用中文写一行没完没了的话。
-其实先要来一个短的,然后一行特别长的,那就是现在这
- 行,
+           "然后还要用中文写一行没完没了的话。
+其实先要来一个短的,然后一行特别长的,那就是现在这
+ 行,
 然后可以再有一个短的"))
     (should
      (equal (ebdb-vcard-unfold-lines
diff --git a/ebdb-vcard.el b/ebdb-vcard.el
index 08b01dc..49a7a58 100644
--- a/ebdb-vcard.el
+++ b/ebdb-vcard.el
@@ -28,7 +28,8 @@
 ;;; Code:
 
 (defclass ebdb-formatter-vcard (ebdb-formatter)
-  ((version-string
+  ((coding-system :initform 'utf-8-dos)
+   (version-string
     :type string
     :allocation :class
     :documentation "The string to insert for this formatter's
@@ -43,8 +44,7 @@
 
 (defclass ebdb-formatter-vcard-40 (ebdb-formatter-vcard)
   ((version-string
-    :initform "4.0")
-   (coding-system :initform 'utf-8-emacs))
+    :initform "4.0"))
   :documentation "Formatter for vCard format 4.0.")
 
 (defgroup ebdb-vcard nil
@@ -123,7 +123,7 @@ not always respect these headings."
 (defsubst ebdb-vcard-escape (str)
   "Escape commas, semi-colons and newlines in STR."
   (replace-regexp-in-string
-   "\\([^\\]\\)\\([\n\r]+\\)" "\\1\\\\\\2"
+   "\\([^\\]\\)\\([\n]+\\)" "\\1\\\\n"
    (replace-regexp-in-string "\\([^\\]\\)\\([,;]\\)" "\\1\\\\\\2" str)))
 
 (defsubst ebdb-vcard-unescape (str)
@@ -148,7 +148,7 @@ not always respect these headings."
 ;; correctness.
 (defun ebdb-vcard-fold-lines (text)
   "Fold lines in TEXT, which represents a vCard contact."
-  (let ((lines (split-string text "\r\n"))
+  (let ((lines (split-string text "\n"))
        outlines)
     (dolist (l lines)
       (while (> (string-bytes l) 75)   ; Line is too long.
@@ -168,11 +168,11 @@ not always respect these headings."
          (push (substring l 0 75) outlines)
          (setq l (concat " " (substring l 75)))))
       (push l outlines))
-    (mapconcat #'identity (nreverse outlines) "\r\n")))
+    (mapconcat #'identity (nreverse outlines) "\n")))
 
 (defun ebdb-vcard-unfold-lines (text)
   "Unfold lines in TEXT, which represents a vCard contact."
-  (replace-regexp-in-string "\r\n[\s\t]" "" text))
+  (replace-regexp-in-string "\n[\s\t]" "" text))
 
 (cl-defmethod ebdb-fmt-process-fields ((_f ebdb-formatter-vcard)
                                       (_record ebdb-record)
@@ -235,11 +235,11 @@ All this does is split role instances into multiple 
fields."
                     (ebdb-fmt-field f fld 'normal r))))
           fields))
     (concat
-     (format "BEGIN:VCARD\r\nVERSION:%s\r\n"
+     (format "BEGIN:VCARD\nVERSION:%s\n"
             (slot-value f 'version-string))
      (ebdb-fmt-record-header f r header-fields)
      (ebdb-fmt-record-body f r body-fields)
-     "\r\nEND:VCARD\r\n")))
+     "\nEND:VCARD\n")))
 
 (cl-defmethod ebdb-fmt-record-header ((f ebdb-formatter-vcard)
                                      (r ebdb-record)
@@ -250,8 +250,8 @@ VCARDs don't really have the concept of a \"header\", so 
this
 method is just responsible for formatting the record name."
   (let ((name (car fields)))
    (concat
-    (format "FN:%s\r\n" (ebdb-string name))
-    (format "N;SORT-AS=\"%s\":%s\r\n"
+    (format "FN:%s\n" (ebdb-string name))
+    (format "N;SORT-AS=\"%s\":%s\n"
            (ebdb-record-sortkey r)
            (ebdb-fmt-field f name 'normal r)))))
 
@@ -263,19 +263,19 @@ method is just responsible for formatting the record 
name."
      (format "%s:%s"
             (car f) (cdr f)))
    fields
-   "\r\n"))
+   "\n"))
 
 (cl-defmethod ebdb-fmt-record-body :around ((_f ebdb-formatter-vcard-40)
                                            (_r ebdb-record-person)
                                            (_fields list))
   (let ((str (cl-call-next-method)))
-    (concat str "\r\nKIND:individual")))
+    (concat str "\nKIND:individual")))
 
 (cl-defmethod ebdb-fmt-record-body :around ((_f ebdb-formatter-vcard-40)
                                            (_r ebdb-record-organization)
                                            (_fields list))
   (let ((str (cl-call-next-method)))
-    (concat str "\r\nKIND:org")))
+    (concat str "\nKIND:org")))
 
 (cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
                              (field ebdb-field)



reply via email to

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