Mercurial > emacs
annotate lisp/net/eudcb-bbdb.el @ 99723:a626eb454478
(auto-insert-alist): Don't use match data.
author | Lute Kamstra <lute@gnu.org> |
---|---|
date | Wed, 19 Nov 2008 05:50:54 +0000 |
parents | 91e5880a36c1 |
children | a9dc0e7c3f2b |
rev | line source |
---|---|
27313 | 1 ;;; eudcb-bbdb.el --- Emacs Unified Directory Client - BBDB Backend |
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 | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
27313 | 13 ;; it under the terms of the GNU General Public License as published by |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; (at your option) any later version. |
27313 | 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 | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
27313 | 24 |
25 ;;; Commentary: | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
26 ;; This library provides an interface to use BBDB as a backend of |
27313 | 27 ;; the Emacs Unified Directory Client. |
28 | |
29 ;;; Code: | |
30 | |
31 (require 'eudc) | |
32 (if (not (featurep 'bbdb)) | |
33 (load-library "bbdb")) | |
34 (if (not (featurep 'bbdb-com)) | |
35 (load-library "bbdb-com")) | |
36 | |
37 ;;{{{ Internal cooking | |
38 | |
39 ;; I don't like this but mapcar does not accept a parameter to the function and | |
40 ;; I don't want to use mapcar* | |
41 (defvar eudc-bbdb-current-query nil) | |
42 (defvar eudc-bbdb-current-return-attributes nil) | |
43 | |
44 (defvar eudc-bbdb-attributes-translation-alist | |
45 '((name . lastname) | |
46 (email . net) | |
47 (phone . phones)) | |
48 "Alist mapping EUDC attribute names to BBDB names.") | |
49 | |
50 (eudc-protocol-set 'eudc-query-function 'eudc-bbdb-query-internal 'bbdb) | |
51 (eudc-protocol-set 'eudc-list-attributes-function nil 'bbdb) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
52 (eudc-protocol-set 'eudc-protocol-attributes-translation-alist |
27313 | 53 'eudc-bbdb-attributes-translation-alist 'bbdb) |
54 (eudc-protocol-set 'eudc-bbdb-conversion-alist nil 'bbdb) | |
55 (eudc-protocol-set 'eudc-protocol-has-default-query-attributes nil 'bbdb) | |
56 | |
57 (defun eudc-bbdb-format-query (query) | |
58 "Format a EUDC query alist into a list suitable to `bbdb-search'." | |
59 (let* ((firstname (cdr (assq 'firstname query))) | |
60 (lastname (cdr (assq 'lastname query))) | |
61 (name (or (and firstname lastname | |
62 (concat firstname " " lastname)) | |
63 firstname | |
64 lastname)) | |
65 (company (cdr (assq 'company query))) | |
66 (net (cdr (assq 'net query))) | |
67 (notes (cdr (assq 'notes query))) | |
68 (phone (cdr (assq 'phone query)))) | |
69 (list name company net notes phone))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
70 |
27313 | 71 |
72 (defun eudc-bbdb-filter-non-matching-record (record) | |
73 "Return RECORD if it matches `eudc-bbdb-current-query', nil otherwise." | |
74 (catch 'unmatch | |
75 (progn | |
85228
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
76 (mapc |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
77 (function |
27313 | 78 (lambda (condition) |
79 (let ((attr (car condition)) | |
80 (val (cdr condition)) | |
81 (case-fold-search t) | |
82 bbdb-val) | |
83 (or (and (memq attr '(firstname lastname aka company phones addresses net)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
84 (progn |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
85 (setq bbdb-val |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
86 (eval (list (intern (concat "bbdb-record-" |
27313 | 87 (symbol-name attr))) |
88 'record))) | |
89 (if (listp bbdb-val) | |
90 (if eudc-bbdb-enable-substring-matches | |
91 (eval `(or ,@(mapcar '(lambda (subval) | |
92 (string-match val | |
93 subval)) | |
94 bbdb-val))) | |
95 (member (downcase val) | |
96 (mapcar 'downcase bbdb-val))) | |
97 (if eudc-bbdb-enable-substring-matches | |
98 (string-match val bbdb-val) | |
99 (string-equal (downcase val) (downcase bbdb-val)))))) | |
100 (throw 'unmatch nil))))) | |
101 eudc-bbdb-current-query) | |
102 record))) | |
103 | |
86816
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
104 ;; External. |
86843
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
105 (declare-function bbdb-phone-location "ext:bbdb" t) ; via bbdb-defstruct |
86816
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
106 (declare-function bbdb-phone-string "ext:bbdb" (phone)) |
86843
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
107 (declare-function bbdb-record-phones "ext:bbdb" t) ; via bbdb-defstruct |
87024
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
108 (declare-function bbdb-address-streets "ext:bbdb" t) ; via bbdb-defstruct |
86843
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
109 (declare-function bbdb-address-city "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
110 (declare-function bbdb-address-state "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
111 (declare-function bbdb-address-zip "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
112 (declare-function bbdb-address-location "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
113 (declare-function bbdb-record-addresses "ext:bbdb" t) ; via bbdb-defstruct |
86816
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
114 (declare-function bbdb-records "ext:bbdb" |
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
115 (&optional dont-check-disk already-in-db-buffer)) |
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
116 |
27313 | 117 (defun eudc-bbdb-extract-phones (record) |
118 (mapcar (function | |
119 (lambda (phone) | |
120 (if eudc-bbdb-use-locations-as-attribute-names | |
121 (cons (intern (bbdb-phone-location phone)) | |
122 (bbdb-phone-string phone)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
123 (cons 'phones (format "%s: %s" |
27313 | 124 (bbdb-phone-location phone) |
125 (bbdb-phone-string phone)))))) | |
126 (bbdb-record-phones record))) | |
127 | |
128 (defun eudc-bbdb-extract-addresses (record) | |
129 (let (s c val) | |
87024
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
130 (mapcar (lambda (address) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
131 (setq c (bbdb-address-streets address)) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
132 (dotimes (n 3) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
133 (unless (zerop (length (setq s (nth n c)))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
134 (setq val (concat val s "\n")))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
135 (setq c (bbdb-address-city address) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
136 s (bbdb-address-state address)) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
137 (setq val (concat val |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
138 (if (and (> (length c) 0) (> (length s) 0)) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
139 (concat c ", " s) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
140 c) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
141 " " |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
142 (bbdb-address-zip address))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
143 (if eudc-bbdb-use-locations-as-attribute-names |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
144 (cons (intern (bbdb-address-location address)) val) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
145 (cons 'addresses (concat (bbdb-address-location address) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
146 "\n" val)))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
147 (bbdb-record-addresses record)))) |
27313 | 148 |
149 (defun eudc-bbdb-format-record-as-result (record) | |
150 "Format the BBDB RECORD as a EUDC query result record. | |
151 The record is filtered according to `eudc-bbdb-current-return-attributes'" | |
152 (let ((attrs (or eudc-bbdb-current-return-attributes | |
153 '(firstname lastname aka company phones addresses net notes))) | |
154 attr | |
155 eudc-rec | |
156 val) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
157 (while (prog1 |
27313 | 158 (setq attr (car attrs)) |
159 (setq attrs (cdr attrs))) | |
160 (cond | |
161 ((eq attr 'phones) | |
162 (setq val (eudc-bbdb-extract-phones record))) | |
163 ((eq attr 'addresses) | |
164 (setq val (eudc-bbdb-extract-addresses record))) | |
165 ((memq attr '(firstname lastname aka company net notes)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
166 (setq val (eval |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
167 (list (intern |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
168 (concat "bbdb-record-" |
27313 | 169 (symbol-name attr))) |
170 'record)))) | |
171 (t | |
172 (setq val "Unknown BBDB attribute"))) | |
173 (if val | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
174 (cond |
27313 | 175 ((memq attr '(phones addresses)) |
176 (setq eudc-rec (append val eudc-rec))) | |
177 ((and (listp val) | |
178 (= 1 (length val))) | |
179 (setq eudc-rec (cons (cons attr (car val)) eudc-rec))) | |
180 ((> (length val) 0) | |
181 (setq eudc-rec (cons (cons attr val) eudc-rec))) | |
182 (t | |
183 (error "Unexpected attribute value"))))) | |
184 (nreverse eudc-rec))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
185 |
27313 | 186 |
187 | |
188 (defun eudc-bbdb-query-internal (query &optional return-attrs) | |
189 "Query BBDB with QUERY. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
190 QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
191 BBDB attribute names. |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
192 RETURN-ATTRS is a list of attributes to return, defaulting to |
27313 | 193 `eudc-default-return-attributes'." |
194 | |
195 (let ((eudc-bbdb-current-query query) | |
196 (eudc-bbdb-current-return-attributes return-attrs) | |
197 (query-attrs (eudc-bbdb-format-query query)) | |
198 bbdb-attrs | |
199 (records (bbdb-records)) | |
200 result | |
201 filtered) | |
202 ;; BBDB ORs its query attributes while EUDC ANDs them, hence we need to | |
203 ;; call bbdb-search iteratively on the returned records for each of the | |
204 ;; requested attributes | |
205 (while (and records (> (length query-attrs) 0)) | |
206 (setq bbdb-attrs (append bbdb-attrs (list (car query-attrs)))) | |
207 (if (car query-attrs) | |
208 (setq records (eval `(bbdb-search ,(quote records) ,@bbdb-attrs)))) | |
209 (setq query-attrs (cdr query-attrs))) | |
85228
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
210 (mapc (function |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
211 (lambda (record) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
212 (setq filtered (eudc-filter-duplicate-attributes record)) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
213 ;; If there were duplicate attributes reverse the order of the |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
214 ;; record so the unique attributes appear first |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
215 (if (> (length filtered) 1) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
216 (setq filtered (mapcar (function |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
217 (lambda (rec) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
218 (reverse rec))) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
219 filtered))) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
220 (setq result (append result filtered)))) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
221 (delq nil |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
222 (mapcar 'eudc-bbdb-format-record-as-result |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
223 (delq nil |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
224 (mapcar 'eudc-bbdb-filter-non-matching-record |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
225 records))))) |
27313 | 226 result)) |
227 | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
228 ;;}}} |
27313 | 229 |
230 ;;{{{ High-level interfaces (interactive functions) | |
231 | |
232 (defun eudc-bbdb-set-server (dummy) | |
233 "Set the EUDC server to BBDB." | |
234 (interactive) | |
235 (eudc-set-server dummy 'bbdb) | |
236 (message "BBDB server selected")) | |
237 | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
238 ;;}}} |
27313 | 239 |
240 | |
241 (eudc-register-protocol 'bbdb) | |
242 | |
243 (provide 'eudcb-bbdb) | |
244 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
245 ;; arch-tag: 38276208-75de-4dbc-ba6f-8db684c32e0a |
27313 | 246 ;;; eudcb-bbdb.el ends here |