rename some variables for consistency
[bbdb-csv-import] / bbdb3-csv-import.el
index fc80b43cfb7c28028545f5391d69c59521b6359d..1b167d9fa785939d44529c2bb53692e0cf56c677 100644 (file)
 
 ;;; Commentary:
 
-;; Some tools such as Thunderbird and Outlook allow for exporting contact data
-;; as CSV (Comma Separated Value) files. This package allows for importing such
-;; files into Emacs's bbdb database, version 3+.
+;; Importer of csv (comma separated value) text into Emacs’s bbdb database,
+;; version 3+. Programs such as Thunderbird and Outlook allow for exporting
+;; contact data as csv files.
 
 ;;; Installation:
 ;;
 ;; dependencies: pcsv.el, dash.el, bbdb
 ;; These are available via marmalade/melpa or the internet
 ;;
-;; Add to init file or execute manually:
+;; Add to init file or execute manually as this may be a one time usage:
 ;; (load-file FILENAME-OF-THIS-FILE)
 ;; or
 ;; (add-to-list 'load-path DIRECTORY-CONTAINING-THIS-FILE)
 ;; Backup or rename any existing ~/.bbdb and ~/.emacs.d/bbdb while testing that
 ;; the import works correctly.
 ;;
+;; Assign bbdb3-csv-import-mapping-table to a mapping table. Some are predefined
+;; below, ie. bbdb3-csv-import-thunderbird.
+;;
 ;; Simply call `bbdb3-csv-import-buffer' or
 ;; `bbdb3-csv-import-file'. Interactively they prompt for file/buffer. Use
 ;; non-interactively for no prompts.
 ;;
 ;; Thunderbird csv data works out of the box. Otherwise you will need to create
 ;; a mapping table to suit your data and assign it to
-;; bbdb3-csv-import-mapping-table. Please send any new mapping tables upstream
-;; so I can add it to this file for other's benefit. I, Ian Kelling, am willing
-;; to help with any issues including creating a mapping table given sample data.
+;; bbdb3-csv-import-mapping-table. Note that variable's doc string and perhaps
+;; the test data within this project for more details. Please send any new
+;; mapping tables upstream so I can add it to this file for other's benefit. I,
+;; Ian Kelling, am willing to help with any issues including creating a mapping
+;; table given sample data.
 ;;
 ;; Tips for testing: bbdb doesn't work if you delete the bbdb database file in
 ;; the middle of an emacs session. If you want to empty the current bbdb database,
                       "Work State"
                       "Work ZipCode"
                       "Work Country")))
-    ("organization" ("Organization"))
+    ("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")))
+     "Custom 4" "Notes" "Job Title"))
+  "Thunderbird csv format")
 
-(defvar bbdb3-csv-import-mapping-table bbdb3-csv-import-thunderbird
+(defconst bbdb3-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" "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"
+     ("business address" (("Business Street"
+                           "Business Street 2"
+                           "Business Street 3")
+                          "Business City"
+                          "Business State"
+                          "Business Postal Code"
+                          "Business Country"))
+     ("home address" (("Home Street"
+                       "Home Street 2"
+                       "Home Street 3")
+                      "Home City"
+                      "Home State"
+                      "Home Postal Code"
+                      "Home Country"))
+     ("other address" (("Other Street"
+                        "Other Street 2"
+                        "Other Street 3")
+                       "Other City"
+                       "Other State"
+                       "Other Postal Code"
+                       "Other Country")))
+    ("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"))
+  "Linkedin export in the Outlook csv format.")
+
+
+(defvar bbdb3-csv-import-mapping-table nil
   "The table which maps bbdb3 fields to csv fields.
 Use the default as an example to map non-thunderbird data.
 Name used is firstname + lastname or name.
@@ -127,38 +166,40 @@ Defaults to current buffer."
            (map-assoc (field) (assoc-plus (car (field-map field)) csv-record)))
         
         (let ((name (let ((first (map-assoc "firstname"))
+                          (middle (map-assoc "middlename"))
                           (last (map-assoc "lastname"))
                           (name (map-assoc "name")))
-                      (if (and first last)
+                      ;; prioritize any combination of first middle last over just "name"
+                      (if (or (and first last) (and first middle) (and middle last))
                           ;; purely historical note.
                           ;; it works exactly the same but I don't use (cons first last) due to a bug
                           ;; http://www.mail-archive.com/bbdb-info%40lists.sourceforge.net/msg06388.html
-                          (concat first " " last)
-                        (or name first last ""))))
-              (phone (rd (lambda (elem)
-                           (let ((data (assoc-plus elem csv-record)))
-                             (if data (vconcat (list elem data)))))
+                          (concat (or first middle) " " (or middle last) (when (and first middle) (concat " " last) ))
+                        (or name first middle last ""))))
+              (phone (rd (lambda (mapping-elem)
+                           (let ((data (assoc-plus mapping-elem csv-record)))
+                             (if data (vconcat (list mapping-elem data)))))
                          (field-map "phone")))
-              (xfields (rd (lambda (field)
-                             (let ((value (assoc-plus field csv-record)))
+              (xfields (rd (lambda (mapping-elem)
+                             (let ((value (assoc-plus mapping-elem csv-record)))
                                (when value
-                                 (while (string-match " " field)
+                                 (while (string-match " " mapping-elem)
                                    ;; turn csv field names into symbols for extra fields
-                                   (setq field (replace-match "" nil nil field)))
-                                 (cons (make-symbol (downcase field)) value))))
+                                   (setq mapping-elem (replace-match "" nil nil mapping-elem)))
+                                 (cons (make-symbol (downcase mapping-elem)) value))))
                            (field-map "xfields")))
-              (address (rd (lambda (x)
-                             (let ((address-lines (mapcar-assoc (caadr x)))
-                                   (address-data (mapcar-assoc (cdadr x))))
+              (address (rd (lambda (mapping-elem)
+                             (let ((address-lines (mapcar-assoc (caadr mapping-elem)))
+                                   (address-data (mapcar-assoc (cdadr mapping-elem))))
                                ;; determine if non-nil and put together the  minimum set
                                (when (or (not (-all? '(lambda (arg) (zerop (length arg))) address-data))
                                          (not (-all? '(lambda (arg) (zerop (length arg))) address-lines)))
                                  (when (> 2 (length address-lines))
-                                   (setcdr (max 2 (nthcdr (-find-last-index (lambda (x) (not (null x)))
+                                   (setcdr (max 2 (nthcdr (-find-last-index (lambda (mapping-elem) (not (null mapping-elem)))
                                                                             address-lines)
                                                           address-lines)) nil))
-                                 (vconcat (list (car x)) (list address-lines) address-data))))
-                           (cdr (assoc "address" bbdb3-csv-import-mapping-table))))
+                                 (vconcat (list (car mapping-elem)) (list address-lines) address-data))))
+                           (field-map "address")))
               (mail (rd-assoc (field-map "mail")))
               (organization (rd-assoc (field-map "organization")))
               (affix (map-assoc "affix"))
@@ -188,3 +229,4 @@ Defaults to current buffer."
 (provide 'bbdb3-csv-import)
 
 ;;; bbdb3-csv-import.el ends here
+