Mercurial > emacs
annotate lisp/net/eudc-export.el @ 94110:d7d3cc08c6cd
(diary-remind): Don't clobber `date' passed by diary-sexp-entry.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Wed, 16 Apr 2008 03:23:30 +0000 |
parents | 1e3a407766b9 |
children | 91e5880a36c1 |
rev | line source |
---|---|
38422
7a94f1c588c4
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
1 ;;; eudc-export.el --- functions to export EUDC query results |
27313 | 2 |
74509 | 3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
79714 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
27313 | 5 |
42778
6d743d659035
New maintainer. Change author's address.
Pavel Janík <Pavel@Janik.cz>
parents:
42574
diff
changeset
|
6 ;; Author: Oscar Figueiredo <oscar@cpe.fr> |
6d743d659035
New maintainer. Change author's address.
Pavel Janík <Pavel@Janik.cz>
parents:
42574
diff
changeset
|
7 ;; Maintainer: Pavel Janík <Pavel@Janik.cz> |
42574 | 8 ;; Keywords: comm |
27313 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
78230
84cf1e2214c5
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
14 ;; the Free Software Foundation; either version 3, or (at your option) |
27313 | 15 ;; any later version. |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
27313 | 26 |
27 ;;; Commentary: | |
28 | |
29 ;;; Usage: | |
30 ;; See the corresponding info file | |
31 | |
32 ;;; Code: | |
33 | |
34 (require 'eudc) | |
35 | |
36 (if (not (featurep 'bbdb)) | |
37 (load-library "bbdb")) | |
38 (if (not (featurep 'bbdb-com)) | |
39 (load-library "bbdb-com")) | |
40 | |
41 (defun eudc-create-bbdb-record (record &optional silent) | |
42 "Create a BBDB record using the RECORD alist. | |
43 RECORD is an alist of (KEY . VALUE) where KEY is a directory attribute name | |
44 symbol and VALUE is the corresponding value for the record. | |
45 If SILENT is non-nil then the created BBDB record is not displayed." | |
46 ;; This function runs in a special context where lisp symbols corresponding | |
47 ;; to field names in record are bound to the corresponding values | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
38422
diff
changeset
|
48 (eval |
27313 | 49 `(let* (,@(mapcar '(lambda (c) |
50 (list (car c) (if (listp (cdr c)) | |
51 (list 'quote (cdr c)) | |
52 (cdr c)))) | |
53 record) | |
54 bbdb-name | |
55 bbdb-company | |
56 bbdb-net | |
57 bbdb-address | |
58 bbdb-phones | |
59 bbdb-notes | |
60 spec | |
61 bbdb-record | |
62 value | |
63 (conversion-alist (symbol-value eudc-bbdb-conversion-alist))) | |
64 | |
65 ;; BBDB standard fields | |
66 (setq bbdb-name (eudc-parse-spec (cdr (assq 'name conversion-alist)) record nil) | |
67 bbdb-company (eudc-parse-spec (cdr (assq 'company conversion-alist)) record nil) | |
68 bbdb-net (eudc-parse-spec (cdr (assq 'net conversion-alist)) record nil) | |
69 bbdb-notes (eudc-parse-spec (cdr (assq 'notes conversion-alist)) record nil)) | |
70 (setq spec (cdr (assq 'address conversion-alist))) | |
71 (setq bbdb-address (delq nil (eudc-parse-spec (if (listp (car spec)) | |
72 spec | |
73 (list spec)) | |
74 record t))) | |
75 (setq spec (cdr (assq 'phone conversion-alist))) | |
76 (setq bbdb-phones (delq nil (eudc-parse-spec (if (listp (car spec)) | |
77 spec | |
78 (list spec)) | |
79 record t))) | |
80 ;; BBDB custom fields | |
81 (setq bbdb-notes (append (list (and bbdb-notes (cons 'notes bbdb-notes))) | |
82 (mapcar (function | |
83 (lambda (mapping) | |
84 (if (and (not (memq (car mapping) | |
85 '(name company net address phone notes))) | |
86 (setq value (eudc-parse-spec (cdr mapping) record nil))) | |
87 (cons (car mapping) value)))) | |
88 conversion-alist))) | |
89 (setq bbdb-notes (delq nil bbdb-notes)) | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
38422
diff
changeset
|
90 (setq bbdb-record (bbdb-create-internal bbdb-name |
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
38422
diff
changeset
|
91 bbdb-company |
27313 | 92 bbdb-net |
93 bbdb-address | |
94 bbdb-phones | |
95 bbdb-notes)) | |
96 (or silent | |
97 (bbdb-display-records (list bbdb-record)))))) | |
98 | |
99 (defun eudc-parse-spec (spec record recurse) | |
100 "Parse the conversion SPEC using RECORD. | |
101 If RECURSE is non-nil then SPEC may be a list of atomic specs." | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
38422
diff
changeset
|
102 (cond |
27313 | 103 ((or (stringp spec) |
104 (symbolp spec) | |
105 (and (listp spec) | |
106 (symbolp (car spec)) | |
107 (fboundp (car spec)))) | |
108 (condition-case nil | |
109 (eval spec) | |
110 (void-variable nil))) | |
111 ((and recurse | |
112 (listp spec)) | |
113 (mapcar '(lambda (spec-elem) | |
114 (eudc-parse-spec spec-elem record nil)) | |
115 spec)) | |
116 (t | |
117 (error "Invalid specification for `%s' in `eudc-bbdb-conversion-alist'" spec)))) | |
118 | |
119 (defun eudc-bbdbify-address (addr location) | |
120 "Parse ADDR into a vector compatible with BBDB. | |
121 ADDR should be an address string of no more than four lines or a | |
122 list of lines. | |
123 The last two lines are searched for the zip code, city and state name. | |
124 LOCATION is used as the address location for bbdb." | |
125 (let* ((addr-components (if (listp addr) | |
126 (reverse addr) | |
127 (reverse (split-string addr "\n")))) | |
128 (last1 (pop addr-components)) | |
129 (last2 (pop addr-components)) | |
130 zip city state) | |
131 (setq addr-components (nreverse addr-components)) | |
132 ;; If not containing the zip code the last line is supposed to contain a | |
133 ;; country name and the addres is supposed to be in european style | |
134 (if (not (string-match "[0-9][0-9][0-9]" last1)) | |
135 (progn | |
136 (setq state last1) | |
137 (if (string-match "\\([0-9]+\\)[ \t]+\\(.*\\)" last2) | |
138 (setq city (match-string 2 last2) | |
139 zip (string-to-number (match-string 1 last2))) | |
140 (error "Cannot parse the address"))) | |
141 (cond | |
142 ;; American style | |
143 ((string-match "\\(\\w+\\)\\W*\\([A-Z][A-Z]\\)\\W*\\([0-9]+\\)" last1) | |
144 (setq city (match-string 1 last1) | |
145 state (match-string 2 last1) | |
146 zip (string-to-number (match-string 3 last1)))) | |
147 ;; European style | |
148 ((string-match "\\([0-9]+\\)[ \t]+\\(.*\\)" last1) | |
149 (setq city (match-string 2 last1) | |
150 zip (string-to-number (match-string 1 last1)))) | |
151 (t | |
152 (error "Cannot parse the address")))) | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
38422
diff
changeset
|
153 (vector location |
27313 | 154 (or (nth 0 addr-components) "") |
155 (or (nth 1 addr-components) "") | |
156 (or (nth 2 addr-components) "") | |
157 (or city "") | |
158 (or state "") | |
159 zip))) | |
160 | |
86815
23a61077ba03
(bbdb-parse-phone-number, bbdb-string-trim): Declare as functions.
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
161 ;; External. |
23a61077ba03
(bbdb-parse-phone-number, bbdb-string-trim): Declare as functions.
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
162 (declare-function bbdb-parse-phone-number "ext:bbdb-com" |
23a61077ba03
(bbdb-parse-phone-number, bbdb-string-trim): Declare as functions.
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
163 (string &optional number-type)) |
23a61077ba03
(bbdb-parse-phone-number, bbdb-string-trim): Declare as functions.
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
164 (declare-function bbdb-string-trim "ext:bbdb" (string)) |
23a61077ba03
(bbdb-parse-phone-number, bbdb-string-trim): Declare as functions.
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
165 |
27313 | 166 (defun eudc-bbdbify-phone (phone location) |
167 "Parse PHONE into a vector compatible with BBDB. | |
168 PHONE is either a string supposedly containing a phone number or | |
169 a list of such strings which are concatenated. | |
170 LOCATION is used as the phone location for BBDB." | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
38422
diff
changeset
|
171 (cond |
27313 | 172 ((stringp phone) |
173 (let (phone-list) | |
174 (condition-case err | |
175 (setq phone-list (bbdb-parse-phone-number phone)) | |
176 (error | |
177 (if (string= "phone number unparsable." (eudc-cadr err)) | |
178 (if (not (y-or-n-p (format "BBDB claims %S to be unparsable--insert anyway? " phone))) | |
179 (error "Phone number unparsable") | |
180 (setq phone-list (list (bbdb-string-trim phone)))) | |
181 (signal (car err) (cdr err))))) | |
182 (if (= 3 (length phone-list)) | |
183 (setq phone-list (append phone-list '(nil)))) | |
184 (apply 'vector location phone-list))) | |
185 ((listp phone) | |
186 (vector location (mapconcat 'identity phone ", "))) | |
187 (t | |
188 (error "Invalid phone specification")))) | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
38422
diff
changeset
|
189 |
27313 | 190 (defun eudc-batch-export-records-to-bbdb () |
191 "Insert all the records returned by a directory query into BBDB." | |
192 (interactive) | |
193 (goto-char (point-min)) | |
194 (let ((nbrec 0) | |
195 record) | |
196 (while (eudc-move-to-next-record) | |
197 (and (overlays-at (point)) | |
198 (setq record (overlay-get (car (overlays-at (point))) 'eudc-record)) | |
199 (1+ nbrec) | |
200 (eudc-create-bbdb-record record t))) | |
201 (message "%d records imported into BBDB" nbrec))) | |
202 | |
203 ;;;###autoload | |
204 (defun eudc-insert-record-at-point-into-bbdb () | |
205 "Insert record at point into the BBDB database. | |
206 This function can only be called from a directory query result buffer." | |
207 (interactive) | |
208 (let ((record (and (overlays-at (point)) | |
209 (overlay-get (car (overlays-at (point))) 'eudc-record)))) | |
210 (if (null record) | |
211 (error "Point is not over a record") | |
212 (eudc-create-bbdb-record record)))) | |
213 | |
214 ;;;###autoload | |
215 (defun eudc-try-bbdb-insert () | |
216 "Call `eudc-insert-record-at-point-into-bbdb' if on a record." | |
217 (interactive) | |
218 (and (or (featurep 'bbdb) | |
219 (prog1 (locate-library "bbdb") (message ""))) | |
220 (overlays-at (point)) | |
221 (overlay-get (car (overlays-at (point))) 'eudc-record) | |
222 (eudc-insert-record-at-point-into-bbdb))) | |
223 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
224 ;; arch-tag: 8cbda7dc-3163-47e6-921c-6ec5083df2d7 |
27313 | 225 ;;; eudc-export.el ends here |