minor doc update
[bbdb-csv-import] / bbdb-csv-import.el
index 28c4feed5e95a259502dcc301698310d99a2403f..fb51a9e252f4b2d4abc4b1a65673d3e7773582dd 100644 (file)
@@ -45,6 +45,9 @@
 ;;
 ;; Simply M-x `bbdb-csv-import-buffer' or `bbdb-csv-import-file'.
 ;; When called interactively, they prompt for file or buffer arguments.
+;;
+;; Then view your bbdb records: M-x bbdb .* RET
+;; If the import looks good save the bbdb database: C-x s (bbdb-save)
 
 ;;; Advanced usage / notes:
 ;;
 ;;; Custom mapping of csv fields
 ;;
 ;; If a field is handled wrong or you want to extend the program to handle a new
-;; kind of csv format, you need to setup a custom field mapping variable. It
-;; should not be too hard, but you can also  Use the existing tables as an example. By default, we
-;; use a combination of most predefined mappings, and look for all of their
-;; fields, but it is probably best to avoid that kind of table when setting up
-;; your own as it is an unnecessary complexity in that 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 if you would rather avoid the
-;; configure interface:
+;; kind of csv format, you need to setup a custom field mapping variable. Use
+;; the existing tables as an example. By default, we use a combination of most
+;; predefined mappings, and look for all of their fields, but it is probably
+;; best to avoid that kind of table when setting up your own as it is an
+;; unnecessary complexity in that 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 if you would rather avoid the configure interface:
 ;; 
 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-combined)
 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-thunderbird)
 ;; mapping table. Please send any new 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.
+;;
+;; Mapping table tips: For field names or sets of field names which go together,
+;; and are numbered, 1, 2, 3, the repeat keyword can be used to expand as many
+;; as are in your csv data.
 
 ;;; Misc tips/troubleshooting:
 ;;
 ;;
 ;; Patches and bugs are very welcome via https://gitlab.com/iankelling/bbdb-csv-import
 ;; 
-;; Questions, feedback, etc are very welcome via email to Ian Kelling
-;; <ian@iankelling.org>. I will add any useful questions, answers, etc. to this
-;; file. The scope/userbase of this project doesn't justify a mailing list, but if
-;; it ever did I would start a mailman or discourse to act as a mailing list
-;; and forum.
+;; Questions, feedback, or anything is very welcome at to the bbdb-csv-import mailing list
+;; https://lists.iankelling.org/listinfo/bbdb-csv-import, no subscription needed to post via
+;; bbdb-csv-import@lists.iankelling.org
 
-;;; known bugs:
-;; * linkedin data contains ^M characters that need to be removed before import
-;; * blank lines are not ignored
 
 
 ;;; Code:
@@ -326,10 +327,13 @@ See the commentary section of this file for more details."
 
 
 (defun  bbdb-csv-import-expand-repeats (csv-fields list)
-  "Return new list where elements from LIST in form (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 lists inside it)"
+  "Return new list where elements from LIST in form (repeat elem1
+...)  become ((elem1 ...) [(elem2 ...)] ...) for as many fields
+exist in the csv fields. elem can be a string or a tree (a list
+with lists inside it). We use the first element as a template,
+and increase its number by one, and check if it exists, and then
+increment any other elements from the repeat list which have
+numbers in them."
   (cl-flet ((replace-num (num string)
                          ;; in STRING, replace all groups of numbers with NUM
                          (replace-regexp-in-string "[0-9]+"
@@ -369,19 +373,30 @@ don't want flattened."
 
 ;;;###autoload
 (defun bbdb-csv-import-file (filename)
-  "Parse and import csv file FILENAME to bbdb."
+  "Parse and import csv file FILENAME to bbdb.
+The file will be saved to disk with blank lines and aberrant characters removed."
   (interactive "fCSV file containg contact data: ")
   (bbdb-csv-import-buffer (find-file-noselect filename)))
 
 ;;;###autoload
 (defun bbdb-csv-import-buffer (&optional buffer-or-name) 
-  "Parse and import csv BUFFER-OR-NAME to bbdb.
-Argument is a buffer or name of a buffer.
-Defaults to current buffer."
+  "Parse and import csv buffer to bbdb. Interactively, it prompts for a buffer.
+The buffer will be saved to disk with blank lines and aberrant characters removed.
+BUFFER-OR-NAME is a buffer or name of a buffer, or the current buffer if nil."
   (interactive "bBuffer containing CSV contact data: ")
   (when (null bbdb-csv-import-mapping-table)
     (error "error: `bbdb-csv-import-mapping-table' is nil. Please set it and rerun."))
-  (let* ((csv-data (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
+  (let* ((csv-buffer (get-buffer (or buffer-or-name (current-buffer))))
+         (csv-data (save-excursion
+                     (set-buffer csv-buffer)
+                     ;; deal with blank lines and ^M from linkedin
+                     (flush-lines "^\\s-*$")
+                     (goto-char (point-min))
+                     ;; remove ^M aka ret characters
+                     (while (re-search-forward (char-to-string 13) nil t)
+                       (replace-match ""))
+                     (basic-save-buffer)
+                     (pcsv-parse-file buffer-file-name)))
          (csv-fields (car csv-data))
          (csv-data (cdr csv-data))
          (allow-dupes bbdb-allow-duplicates)