added melpa bbdb dependency. simplified documentation
[bbdb-csv-import] / bbdb-csv-import.el
index c64b76ef1e8d8a464e4507d97b1e7cdf50403d73..59e6b94a089e1d3e8bdd2df4a8f9d59ebd913021 100644 (file)
@@ -6,7 +6,7 @@
 ;; Author: Ian Kelling <ian@iankelling.org>
 ;; Created: 1 Apr 2014
 ;; Version: 1.1
-;; Package-Requires: ((pcsv "1.3.3") (dash "2.5.0"))
+;; Package-Requires: ((pcsv "1.3.3") (dash "2.5.0") (bbdb "20140412.1949"))
 ;; Keywords: csv, util, bbdb
 ;; Homepage: https://gitlab.com/iankelling/bbdb-csv-import
 
 
 ;;; Installation:
 ;;
-;; Install bbdb. If you installed this file with a package manager, just
+;; If you installed this file with a package manager, just
 ;; 
 ;; (require 'bbdb-csv-import)
 ;;
 ;; Else, note the min versions of dependencies above in "Package-Requires:",
-;; and load this file. I don't know the exact minimum bbdb version.
+;; and load this file. The exact minimum bbdb version is unknown, something 3+.
 
 ;;; Usage:
 ;;
 ;; exporter claims outlook compatibility, there is a good chance it will work
 ;; out of the box.
 ;;
-;; If things don't work, you can probably fix it with a field mapping variable.
-;; By default, we use a combination of all predefined mappings, and look for
-;; every known field.  If you have data that is from something we've already
-;; tested, try using it's specific mapping table in case that works better.
-;; Here is a handy template to set each of the predefined mapping tables:
+;; If things don't work, you can probably fix it with a custom field mapping
+;; variable. It should not be too hard. Use the existing tables as an
+;; example. By default, we use a combination of all predefined mappings, and
+;; look for every known field, but it is probably best to avoid that kind of
+;; table when setting up your own as it is an unnecessary complexity in this
+;; case.  If you have a problem with data from a supported export program, start
+;; by testing its specific mapping table instead of the combined one. Here is a
+;; handy template to set each of the predefined mapping tables:
 ;; 
 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-combined)
 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-thunderbird)
 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-linkedin)
 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-outlook-web)
 ;; 
-;; If you need to define your own mapping table, it should not be too hard. Use
-;; the existing tables as an example. Probably best to ignore the combined table
-;; as it is an unnecessary complexity when working on a new table. The doc
-;; string for `bbdb-create-internal' may also be useful. Please send any
-;; new mapping tables to the maintainer listed in this file. The maintainer
-;; should be able to help with any issues and may create a new mapping table
-;; given sample data.
+;; In addition to the examples, the doc string for `bbdb-create-internal' may
+;; also be useful. Please send any new mapping tables to the maintainer listed
+;; in this file. The maintainer should be able to help with any issues and may
+;; create a new mapping table given sample data.
 ;;
 ;; Misc tips/troubleshooting:
 ;; - ASynK looks promising for syncing bbdb/google/outlook.
 
 
 (defconst bbdb-csv-import-thunderbird
-  '(("firstname" "First Name")
-    ("lastname" "Last Name")
-    ("name" "Display Name")
-    ("aka" "Nickname")
-    ("mail" "Primary Email" "Secondary Email")
-    ("phone" "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
-    ("address"
+  '((:namelist "First Name" "Last Name")
+    (:name "Display Name")
+    (:aka "Nickname")
+    (:mail "Primary Email" "Secondary Email")
+    (:phone "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
+    (:address
      (("home address"
        (("Home Address" "Home Address 2")
         "Home City" "Home State"
        (("Work Address" "Work Address 2")
         "Work City" "Work State"
         "Work ZipCode" "Work Country"))))
-    ("organization" "Organization")
-    ("xfields" "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
+    (:organization "Organization")
+    (:xfields "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
      "Birth Day" "Department" "Custom 1" "Custom 2" "Custom 3"
      "Custom 4" "Notes" "Job Title"))
   "Thunderbird csv format")
 
 (defconst bbdb-csv-import-linkedin
-  '(("firstname" "First Name")
-    ("lastname" "Last Name")
-    ("middlename" "Middle Name")
-    ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
-    ("phone"
+  '((:namelist "First Name" "Middle Name" "Last Name")
+    (:mail "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
+    (:phone
      "Assistant's Phone" "Business Fax" "Business Phone"
      "Business Phone 2" "Callback" "Car Phone"
      "Company Main Phone" "Home Fax" "Home Phone"
      "Home Phone 2" "ISDN" "Mobile Phone"
      "Other Fax" "Other Phone" "Pager"
      "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
-    ("address"
+    (:address
      (("business address"
        (("Business Street" "Business Street 2" "Business Street 3")
         "Business City" "Business State"
        (("Other Street" "Other Street 2" "Other Street 3")
         "Other City" "Other State"
         "Other Postal Code" "Other Country"))))
-    ("organization" "Company")
-    ("xfields"
+    (:organization "Company")
+    (:xfields
      "Suffix" "Department" "Job Title" "Assistant's Name"
      "Birthday" "Manager's Name" "Notes" "Other Address PO Box"
      "Spouse" "Web Page" "Personal Web Page"))
 ;; If you don't like this, just delete them from this fiel.
 ;; If you want some other special handling, it will need to be coded.
 (defconst bbdb-csv-import-gmail
-  '(("firstname" "Given Name")
-    ("lastname" "Family Name")
-    ("name" "Name")
-    ("mail" (repeat "E-mail 1 - Value"))
-    ("phone" (repeat ("Phone 1 - Type" "Phone 1 - Value")))
-    ("address"
+  '((:namelist "Given Name" "Family Name")
+    (:name "Name")
+    (:mail (repeat "E-mail 1 - Value"))
+    (:phone (repeat ("Phone 1 - Type" "Phone 1 - Value")))
+    (:address
      (repeat (("Address 1 - Type")
               (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
                "Address 1 - City" "Address 1 - Region"
                "Address 1 - Postal Code" "Address 1 - Country"))))
-    ("organization" (repeat "Organization 1 - Name"))
-    ("xfields"
+    (:organization (repeat "Organization 1 - Name"))
+    (:xfields
      "Additional Name" "Yomi Name" "Given Name Yomi"
      "Additional Name Yomi" "Family Name Yomi" "Name Prefix"
      "Name Suffix" "Initials" "Nickname"
    would create useless custom fields.")
 
 (defconst bbdb-csv-import-outlook-web
-  '(("firstname" "First Name")
-    ("lastname" "Last Name")
-    ("middlename" "Middle Name")
-    ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
-    ("phone"
+  '((:namelist "First Name" "Middle Name" "Last Name")
+    (:mail "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
+    (:phone
      "Assistant's Phone" "Business Fax" "Business Phone"
      "Business Phone 2" "Callback" "Car Phone"
      "Company Main Phone" "Home Fax" "Home Phone"
      "Home Phone 2" "ISDN" "Mobile Phone"
      "Other Fax" "Other Phone" "Pager"
      "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
-    ("address"
+    (:address
      (("business address"
        (("Business Street")
         "Business City" "Business State"
        (("Other Street")
         "Other City" "Other State"
         "Other Postal Code" "Other Country"))))
-    ("organization" "Company")
-    ("xfields"
+    (:organization "Company")
+    (:xfields
      "Anniversary" "Family Name Yomi" "Given Name Yomi"
      "Suffix" "Department" "Job Title" "Birthday" "Manager's Name" "Notes"
      "Spouse" "Web Page"))
@@ -255,15 +249,14 @@ Adds email labels as custom fields.")
 
 (defconst bbdb-csv-import-combined
   (list
-   (bbdb-csv-import-merge-map "firstname")
-   (bbdb-csv-import-merge-map "middlename")
-   (bbdb-csv-import-merge-map "lastname")
-   (bbdb-csv-import-merge-map "name")
-   (bbdb-csv-import-merge-map "aka")
-   (bbdb-csv-import-merge-map "mail")
-   (bbdb-csv-import-merge-map "phone")
+   ;; manually combined for proper ordering
+   '(:namelist "First Name" "Given Name" "Middle Name" "Last Name" "Family Name")
+   (bbdb-csv-import-merge-map :name)
+   (bbdb-csv-import-merge-map :aka)
+   (bbdb-csv-import-merge-map :mail)
+   (bbdb-csv-import-merge-map :phone)
    ;; manually combined the addresses. Because it was easier.
-   '("address"
+   '(:address
      (repeat (("Address 1 - Type")
               (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
                "Address 1 - City" "Address 1 - Region"
@@ -285,8 +278,8 @@ Adds email labels as custom fields.")
        (("Other Street" "Other Street 2" "Other Street 3")
         "Other City" "Other State"
         "Other Postal Code" "Other Country"))))
-   (bbdb-csv-import-merge-map "organization")
-   (bbdb-csv-import-merge-map "xfields")))
+   (bbdb-csv-import-merge-map :organization)
+   (bbdb-csv-import-merge-map :xfields)))
 
 (defvar bbdb-csv-import-mapping-table bbdb-csv-import-combined
   "The table which maps bbdb fields to csv fields. The default should work for most cases.
@@ -315,14 +308,14 @@ Defaults to current buffer."
     ;; convenient function names
     (fset 'rd 'bbdb-csv-import-rd)
     (fset 'assoc-plus 'bbdb-csv-import-assoc-plus)
-    (fset 'flatten1 'bbdb-csv-import-flatten1)
     ;; Easier to allow duplicates and handle them post import vs failing as
     ;; soon as we find one.
     (setq bbdb-allow-duplicates t)
     ;; loop over the csv records
     (while (setq csv-record (map 'list 'cons csv-fields (pop csv-contents)))
       (cl-flet*
-          ((replace-num (num string)
+          ((ca (key list) (cdr (assoc key list))) ;; utility function
+           (replace-num (num string)
                         ;; in STRING, replace all groups of numbers with NUM
                         (replace-regexp-in-string "[0-9]+" (number-to-string num) string))
            (expand-repeats (list)
@@ -330,30 +323,34 @@ Defaults to current buffer."
                            ;; (repeat elem1 ...) become ((elem1 ...) [(elem2 ...)] ...)
                            ;; For as many repeating numbered fields exist in the csv fields.
                            ;; elem can be a string or a tree (a list with possibly lists inside it)
-                           (--reduce-from (if (not (and (consp it) (eq (car it) 'repeat)))
-                                              (cons it acc)
-                                            (setq it (cdr it))
-                                            (let* ((i 1)
-                                                   (first-field (car (flatten it))))
-                                              (setq acc (cons it acc))
-                                              ;; use first-field to test if there is another repetition.
-                                              (while (member (replace-num (setq i (1+ i)) first-field) csv-fields)
-                                                (cl-labels ((fun (cell)
-                                                                 (if (consp cell)
-                                                                     (mapcar #'fun cell)
-                                                                   (replace-num i cell))))
-                                                  (setq acc (cons (fun it) acc))))
-                                              acc))
-                                          nil list))
+                           (--reduce-from
+                            (if (not (and (consp it) (eq (car it) 'repeat)))
+                                (cons it acc)
+                              (setq it (cdr it))
+                              (let* ((i 1)
+                                     (first-field (car (flatten it))))
+                                (setq acc (cons it acc))
+                                ;; use first-field to test if there is another repetition.
+                                (while (member
+                                        (replace-num (setq i (1+ i)) first-field)
+                                        csv-fields)
+                                  (cl-labels ((fun (cell)
+                                                   (if (consp cell)
+                                                       (mapcar #'fun cell)
+                                                     (replace-num i cell))))
+                                    (setq acc (cons (fun it) acc))))
+                                acc))
+                            nil list))
            (map-bbdb (root)
-                      ;; ROOT = a root element from bbdb-csv-import-mapping-table.
-                      ;; Get the actual csv-fields, including variably repeated ones. flattened
-                      ;; by one because repeated fields are put in sub-lists, but
-                      ;; after expanding them, that extra depth is no longer
-                      ;; useful. Small quirk: address mappings without 'repeat
-                      ;; need to be grouped in a list because they contain sublists that we
-                      ;; don't want flattened. Better this than more complex code.
-                      (flatten1 (expand-repeats (cdr (assoc root bbdb-csv-import-mapping-table)))))
+                     ;; ROOT = a root element from bbdb-csv-import-mapping-table.
+                     ;; Get the actual csv-fields, including variably repeated ones. flattened
+                     ;; by one because repeated fields are put in sub-lists, but
+                     ;; after expanding them, that extra depth is no longer
+                     ;; useful. Small quirk: address mappings without 'repeat
+                     ;; need to be grouped in a list because they contain sublists that we
+                     ;; don't want flattened. Better this than more complex code.
+                     (bbdb-csv-import-flatten1
+                      (expand-repeats (ca root bbdb-csv-import-mapping-table))))
            (rd-assoc (root)
                      ;; given ROOT, return a list of data, ignoring empty fields
                      (rd (lambda (elem) (assoc-plus elem csv-record)) (map-bbdb root)))
@@ -361,25 +358,20 @@ Defaults to current buffer."
                          ;; E = data-field-name | (field-name-field data-field)
                          ;; get data from the csv-record and return
                          ;; (field-name data) or nil.
-                         (let ((data-name (if (consp e) (cdr (assoc (car e) csv-record)) e))
+                         (let ((data-name (if (consp e) (ca (car e) csv-record) e))
                                (data (assoc-plus (if (consp e) (cadr e) e) csv-record)))
                            (if data (list data-name data))))
            (map-assoc (field)
                       ;; For simple mappings, get a single result
                       (car (rd-assoc field))))
 
-        (let ((name (let ((first (map-assoc "firstname"))
-                          (middle (map-assoc "middlename"))
-                          (last (map-assoc "lastname"))
-                          (name (map-assoc "name")))
-                      ;; prioritize any combination of first middle last over just "name"
-                      (if (or (and first last) (and first middle) (and middle last))
-                          ;; purely historical note.
-                          ;; using (cons first last) as argument works the same as (concat first " " last)
-                          (concat (or first middle) " " (or middle last) (when (and first middle) (concat " " last) ))
-                        (or name first middle last ""))))
-              (phone (rd 'vconcat (rd #'assoc-expand (map-bbdb "phone"))))
-              (mail (rd-assoc "mail"))
+        (let ((name (let ((name (rd-assoc :namelist)))
+                      ;; prioritize any combination of first middle last over :name
+                      (if (>= (length name) 2)
+                          (mapconcat 'identity name " ")
+                        (map-assoc :name))))
+              (phone (rd 'vconcat (rd #'assoc-expand (map-bbdb :phone))))
+              (mail (rd-assoc :mail))
               (xfields (rd (lambda (list)
                              (let ((e (car list)))
                                (while (string-match "-" e)
@@ -388,7 +380,7 @@ Defaults to current buffer."
                                  (setq e (replace-match "-" nil nil e)))
                                (setq e (make-symbol (downcase e)))
                                (cons e (cadr list)))) ;; change from (a b) to (a . b)
-                           (rd #'assoc-expand (map-bbdb "xfields"))))
+                           (rd #'assoc-expand (map-bbdb :xfields))))
               (address (rd (lambda (e)
                              
                              (let ((address-lines (rd (lambda (elem)
@@ -397,7 +389,7 @@ Defaults to current buffer."
                                    ;; little bit of special handling so we can
                                    ;; use the combined mapping
                                    (address-data (--reduce-from (if (member it csv-fields)
-                                                                    (cons (cdr (assoc it csv-record)) acc)
+                                                                    (cons (ca it csv-record) acc)
                                                                   acc)
                                                                 nil (cdadr e)))
                                    (elem-name (car e)))
@@ -407,7 +399,7 @@ Defaults to current buffer."
                                (setq address-lines (append address-lines
                                                            (-repeat (- 2 (length address-lines)) "")))
                                (when (consp elem-name)
-                                 (setq elem-name (cdr (assoc (car elem-name) csv-record))))
+                                 (setq elem-name (ca (car elem-name) csv-record)))
                                
                                ;; determine if non-nil and put together the  minimum set
                                (when (or (not (--all? (zerop (length it)) address-data))
@@ -417,10 +409,10 @@ Defaults to current buffer."
                                                                              address-lines)
                                                           address-lines)) nil))
                                  (vconcat (list elem-name) (list address-lines) address-data))))
-                           (map-bbdb "address")))
-              (organization (rd-assoc "organization"))
+                           (map-bbdb :address)))
+              (organization (rd-assoc :organization))
               (affix (map-assoc "affix"))
-              (aka (rd-assoc "aka")))
+              (aka (rd-assoc :aka)))
           (bbdb-create-internal name affix aka organization mail phone address xfields t))))
     (setq bbdb-allow-duplicates initial-duplicate-value)))