Minor doc string improvements.
[bbdb-csv-import] / bbdb3-csv-import.el
1 ;;; bbdb3-csv-import.el --- import csv to bbdb version 3+ -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014 by Ian Kelling
4
5 ;; Author: Ian Kelling <ian@iankelling.org>
6 ;; Created: 1 Apr 2014
7 ;; Version: 1.0
8 ;; Keywords: csv, util, bbdb
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Importer of csv (comma separated value) text into Emacs’s bbdb database,
26 ;; version 3+. Programs such as Thunderbird and Outlook allow for exporting
27 ;; contact data as csv files.
28
29 ;;; Installation:
30 ;;
31 ;; dependencies: pcsv.el, dash.el, bbdb
32 ;; These are available via marmalade/melpa or the internet
33 ;;
34 ;; Add to init file or execute manually:
35 ;; (load-file FILENAME-OF-THIS-FILE)
36 ;; or
37 ;; (add-to-list 'load-path DIRECTORY-CONTAINING-THIS-FILE)
38 ;; (require 'bbdb3-csv-import)
39
40 ;;; Usage:
41 ;;
42 ;; Backup or rename any existing ~/.bbdb and ~/.emacs.d/bbdb while testing that
43 ;; the import works correctly.
44 ;;
45 ;; Simply call `bbdb3-csv-import-buffer' or
46 ;; `bbdb3-csv-import-file'. Interactively they prompt for file/buffer. Use
47 ;; non-interactively for no prompts.
48 ;;
49 ;; Thunderbird csv data works out of the box. Otherwise you will need to create
50 ;; a mapping table to suit your data and assign it to
51 ;; bbdb3-csv-import-mapping-table. Note that variable's doc string and perhaps
52 ;; the test data within this project for more details. Please send any new
53 ;; mapping tables upstream so I can add it to this file for other's benefit. I,
54 ;; Ian Kelling, am willing to help with any issues including creating a mapping
55 ;; table given sample data.
56 ;;
57 ;; Tips for testing: bbdb doesn't work if you delete the bbdb database file in
58 ;; the middle of an emacs session. If you want to empty the current bbdb database,
59 ;; do M-x bbdb then .* then C-u * d on the beginning of a record.
60
61 (require 'pcsv)
62 (require 'dash)
63 (require 'bbdb-com)
64 (eval-when-compile (require 'cl))
65
66 (defconst bbdb3-csv-import-thunderbird
67 '(("firstname" "First Name")
68 ("lastname" "Last Name")
69 ("name" "Display Name")
70 ("aka" "Nickname")
71 ("mail" "Primary Email" "Secondary Email")
72 ("phone" "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
73 ("address"
74 ("home address" (("Home Address"
75 "Home Address 2")
76 "Home City"
77 "Home State"
78 "Home ZipCode"
79 "Home Country"))
80 ("work address" (("Work Address"
81 "Work Address 2")
82 "Work City"
83 "Work State"
84 "Work ZipCode"
85 "Work Country")))
86 ("organization" ("Organization"))
87 ("xfields" "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
88 "Birth Day" "Department" "Custom 1" "Custom 2" "Custom 3"
89 "Custom 4" "Notes" "Job Title")))
90
91 (defvar bbdb3-csv-import-mapping-table bbdb3-csv-import-thunderbird
92 "The table which maps bbdb3 fields to csv fields.
93 Use the default as an example to map non-thunderbird data.
94 Name used is firstname + lastname or name.
95 After the car, all names should map to whatever csv
96 field names are used in the first row of csv data.
97 Many fields are optional. If you aren't sure if one is,
98 best to just try it. The doc string for `bbdb-create-internal'
99 may be useful for determining which fields are required.")
100
101 ;;;###autoload
102 (defun bbdb3-csv-import-file (filename)
103 "Parse and import csv file FILENAME to bbdb3."
104 (interactive "fCSV file containg contact data: ")
105 (bbdb3-csv-import-buffer (find-file-noselect filename)))
106
107
108 ;;;###autoload
109 (defun bbdb3-csv-import-buffer (&optional buffer-or-name)
110 "Parse and import csv BUFFER-OR-NAME to bbdb3.
111 Argument is a buffer or name of a buffer.
112 Defaults to current buffer."
113 (interactive "bBuffer containing CSV contact data: ")
114 (let* ((csv-fields (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
115 (csv-contents (cdr csv-fields))
116 (csv-fields (car csv-fields))
117 (initial-duplicate-value bbdb-allow-duplicates)
118 csv-record)
119 ;; Easier to allow duplicates and handle them post import vs failing as
120 ;; soon as we find one.
121 (setq bbdb-allow-duplicates t)
122 (while (setq csv-record (map 'list 'cons csv-fields (pop csv-contents)))
123 (cl-flet*
124 ((rd (func list) (bbdb3-csv-import-reduce func list)) ;; just a local defalias
125 (assoc-plus (key list) (bbdb3-csv-import-assoc-plus key list)) ;; defalias
126 (rd-assoc (list) (rd (lambda (elem) (assoc-plus elem csv-record)) list))
127 (mapcar-assoc (list) (mapcar (lambda (elem) (cdr (assoc elem csv-record))) list))
128 (field-map (field) (cdr (assoc field bbdb3-csv-import-mapping-table)))
129 (map-assoc (field) (assoc-plus (car (field-map field)) csv-record)))
130
131 (let ((name (let ((first (map-assoc "firstname"))
132 (last (map-assoc "lastname"))
133 (name (map-assoc "name")))
134 (if (and first last)
135 ;; purely historical note.
136 ;; it works exactly the same but I don't use (cons first last) due to a bug
137 ;; http://www.mail-archive.com/bbdb-info%40lists.sourceforge.net/msg06388.html
138 (concat first " " last)
139 (or name first last ""))))
140 (phone (rd (lambda (elem)
141 (let ((data (assoc-plus elem csv-record)))
142 (if data (vconcat (list elem data)))))
143 (field-map "phone")))
144 (xfields (rd (lambda (field)
145 (let ((value (assoc-plus field csv-record)))
146 (when value
147 (while (string-match " " field)
148 ;; turn csv field names into symbols for extra fields
149 (setq field (replace-match "" nil nil field)))
150 (cons (make-symbol (downcase field)) value))))
151 (field-map "xfields")))
152 (address (rd (lambda (x)
153 (let ((address-lines (mapcar-assoc (caadr x)))
154 (address-data (mapcar-assoc (cdadr x))))
155 ;; determine if non-nil and put together the minimum set
156 (when (or (not (-all? '(lambda (arg) (zerop (length arg))) address-data))
157 (not (-all? '(lambda (arg) (zerop (length arg))) address-lines)))
158 (when (> 2 (length address-lines))
159 (setcdr (max 2 (nthcdr (-find-last-index (lambda (x) (not (null x)))
160 address-lines)
161 address-lines)) nil))
162 (vconcat (list (car x)) (list address-lines) address-data))))
163 (cdr (assoc "address" bbdb3-csv-import-mapping-table))))
164 (mail (rd-assoc (field-map "mail")))
165 (organization (rd-assoc (field-map "organization")))
166 (affix (map-assoc "affix"))
167 (aka (rd-assoc (field-map "aka"))))
168 (bbdb-create-internal name affix aka organization mail
169 phone address xfields t))))
170 (setq bbdb-allow-duplicates initial-duplicate-value)))
171
172
173 ;;;###autoload
174 (defun bbdb3-csv-import-reduce (func list)
175 "like mapcar but don't build nil results into the resulting list"
176 (-reduce-from (lambda (acc elem)
177 (let ((funcreturn (funcall func elem)))
178 (if funcreturn
179 (cons funcreturn acc)
180 acc)))
181 nil list))
182
183 ;;;###autoload
184 (defun bbdb3-csv-import-assoc-plus (key list)
185 "Like `assoc' but turn an empty string result to nil."
186 (let ((result (cdr (assoc key list))))
187 (when (not (string= "" result))
188 result)))
189
190 (provide 'bbdb3-csv-import)
191
192 ;;; bbdb3-csv-import.el ends here