* net/tramp-sh.el (tramp-remote-process-environment): Add "TMOUT=0".
[bpt/emacs.git] / lisp / net / eudc.el
CommitLineData
c38e0c97 1;;; eudc.el --- Emacs Unified Directory Client -*- coding: utf-8 -*-
7970b229 2
ab422c4d 3;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
7970b229 4
53015965 5;; Author: Oscar Figueiredo <oscar@cpe.fr>
c38e0c97 6;; Maintainer: Pavel Janík <Pavel@Janik.cz>
ff41c6f6 7;; Keywords: comm
7970b229
GM
8
9;; This file is part of GNU Emacs.
10
874a927a 11;; GNU Emacs is free software: you can redistribute it and/or modify
7970b229 12;; it under the terms of the GNU General Public License as published by
874a927a
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
7970b229
GM
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
874a927a 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
7970b229
GM
23
24;;; Commentary:
25;; This package provides a common interface to query directory servers using
26;; different protocols such as LDAP, CCSO PH/QI or BBDB. Queries can be
27;; made through an interactive form or inline. Inline query strings in
28;; buffers are expanded with appropriately formatted query results
29;; (especially used to expand email addresses in message buffers). EUDC
30;; also interfaces with the BBDB package to let you register query results
31;; into your own BBDB database.
32
33;;; Usage:
34;; EUDC comes with an extensive documentation, please refer to it.
35;;
36;; The main entry points of EUDC are:
37;; `eudc-query-form': Query a directory server from a query form
38;; `eudc-expand-inline': Query a directory server for the e-mail address
6c83d99f 39;; of the name before cursor and insert it in the
7970b229
GM
40;; buffer
41;; `eudc-get-phone': Get a phone number from a directory server
42;; `eudc-get-email': Get an e-mail address from a directory server
43;; `eudc-customize': Customize various aspects of EUDC
44
45;;; Code:
46
47(require 'wid-edit)
48
49(eval-and-compile
50 (if (not (fboundp 'make-overlay))
a464a6c7 51 (require 'overlay)))
7970b229
GM
52
53(unless (fboundp 'custom-menu-create)
54 (autoload 'custom-menu-create "cus-edit"))
55
56(require 'eudc-vars)
57
58
59
60;;{{{ Internal cooking
61
62;;{{{ Internal variables and compatibility tricks
63
7970b229 64(defvar eudc-form-widget-list nil)
7cd25617
DN
65
66(defvar eudc-mode-map
67 (let ((map (make-sparse-keymap)))
68 (define-key map "q" 'kill-this-buffer)
69 (define-key map "x" 'kill-this-buffer)
70 (define-key map "f" 'eudc-query-form)
71 (define-key map "b" 'eudc-try-bbdb-insert)
72 (define-key map "n" 'eudc-move-to-next-record)
73 (define-key map "p" 'eudc-move-to-previous-record)
74 map))
75(set-keymap-parent eudc-mode-map widget-keymap)
7970b229 76
adbf7978
JB
77(defvar mode-popup-menu)
78
7970b229
GM
79;; List of known servers
80;; Alist of (SERVER . PROTOCOL)
81(defvar eudc-server-hotlist nil)
82
83;; List of variables that have server- or protocol-local bindings
84(defvar eudc-local-vars nil)
85
82d72d65 86;; Protocol local. Query function
7970b229
GM
87(defvar eudc-query-function nil)
88
89;; Protocol local. A function that retrieves a list of valid attribute names
90(defvar eudc-list-attributes-function nil)
91
92;; Protocol local. A mapping between EUDC attribute names and corresponding
93;; protocol specific names. The following names are defined by EUDC and may be
94;; included in that list: `name' , `firstname', `email', `phone'
95(defvar eudc-protocol-attributes-translation-alist nil)
96
97;; Protocol local. Mapping between protocol attribute names and BBDB field
98;; names
99(defvar eudc-bbdb-conversion-alist nil)
100
101;; Protocol/Server local. Hook called upon switching to that server
102(defvar eudc-switch-to-server-hook nil)
103
104;; Protocol/Server local. Hook called upon switching from that server
105(defvar eudc-switch-from-server-hook nil)
106
107;; Protocol local. Whether the protocol supports queries with no specified
108;; attribute name
109(defvar eudc-protocol-has-default-query-attributes nil)
110
111(defun eudc-cadr (obj)
112 (car (cdr obj)))
113
114(defun eudc-cdar (obj)
115 (cdr (car obj)))
116
117(defun eudc-caar (obj)
118 (car (car obj)))
119
120(defun eudc-cdaar (obj)
121 (cdr (car (car obj))))
122
123(defun eudc-plist-member (plist prop)
124 "Return t if PROP has a value specified in PLIST."
125 (if (not (= 0 (% (length plist) 2)))
126 (error "Malformed plist"))
127 (catch 'found
128 (while plist
129 (if (eq prop (car plist))
130 (throw 'found t))
131 (setq plist (cdr (cdr plist))))
132 nil))
133
44e97401 134;; Emacs's plist-get lacks third parameter
7970b229
GM
135(defun eudc-plist-get (plist prop &optional default)
136 "Extract a value from a property list.
137PLIST is a property list, which is a list of the form
dd42d3ba 138\(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
7970b229
GM
139corresponding to the given PROP, or DEFAULT if PROP is not
140one of the properties on the list."
141 (if (eudc-plist-member plist prop)
142 (plist-get plist prop)
143 default))
144
145(defun eudc-lax-plist-get (plist prop &optional default)
146 "Extract a value from a lax property list.
147
148PLIST is a lax property list, which is a list of the form (PROP1
149VALUE1 PROP2 VALUE2...), where comparisons between properties are done
150using `equal' instead of `eq'. This function returns the value
151corresponding to PROP, or DEFAULT if PROP is not one of the
152properties on the list."
153 (if (not (= 0 (% (length plist) 2)))
154 (error "Malformed plist"))
155 (catch 'found
156 (while plist
157 (if (equal prop (car plist))
158 (throw 'found (car (cdr plist))))
159 (setq plist (cdr (cdr plist))))
160 default))
161
162(if (not (fboundp 'split-string))
163 (defun split-string (string &optional pattern)
164 "Return a list of substrings of STRING which are separated by PATTERN.
165If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
166 (or pattern
167 (setq pattern "[ \f\t\n\r\v]+"))
168 (let (parts (start 0))
169 (when (string-match pattern string 0)
170 (if (> (match-beginning 0) 0)
171 (setq parts (cons (substring string 0 (match-beginning 0)) nil)))
172 (setq start (match-end 0))
173 (while (and (string-match pattern string start)
174 (> (match-end 0) start))
175 (setq parts (cons (substring string start (match-beginning 0)) parts)
176 start (match-end 0))))
177 (nreverse (if (< start (length string))
178 (cons (substring string start) parts)
179 parts)))))
180
181(defun eudc-replace-in-string (str regexp newtext)
182 "Replace all matches in STR for REGEXP with NEWTEXT.
183Value is the new string."
184 (let ((rtn-str "")
185 (start 0)
186 match prev-start)
187 (while (setq match (string-match regexp str start))
188 (setq prev-start start
189 start (match-end 0)
190 rtn-str
191 (concat rtn-str
192 (substring str prev-start match)
193 newtext)))
194 (concat rtn-str (substring str start))))
195
82d72d65 196;;}}}
7970b229
GM
197
198;;{{{ Server and Protocol Variable Routines
199
200(defun eudc-server-local-variable-p (var)
201 "Return non-nil if VAR has server-local bindings."
202 (eudc-plist-member (get var 'eudc-locals) 'server))
203
204(defun eudc-protocol-local-variable-p (var)
205 "Return non-nil if VAR has protocol-local bindings."
206 (eudc-plist-member (get var 'eudc-locals) 'protocol))
207
208(defun eudc-default-set (var val)
209 "Set the EUDC default value of VAR to VAL.
210The current binding of VAR is not changed."
6c83d99f 211 (put var 'eudc-locals
7970b229
GM
212 (plist-put (get var 'eudc-locals) 'default val))
213 (add-to-list 'eudc-local-vars var))
214
215(defun eudc-protocol-set (var val &optional protocol)
216 "Set the PROTOCOL-local binding of VAR to VAL.
217If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
218The current binding of VAR is changed only if PROTOCOL is omitted."
219 (if (eq 'unbound (eudc-variable-default-value var))
220 (eudc-default-set var (symbol-value var)))
221 (let* ((eudc-locals (get var 'eudc-locals))
222 (protocol-locals (eudc-plist-get eudc-locals 'protocol)))
223 (setq protocol-locals (plist-put protocol-locals (or protocol
224 eudc-protocol) val))
6c83d99f 225 (setq eudc-locals
7970b229
GM
226 (plist-put eudc-locals 'protocol protocol-locals))
227 (put var 'eudc-locals eudc-locals)
228 (add-to-list 'eudc-local-vars var)
229 (unless protocol
230 (eudc-update-variable var))))
82d72d65 231
7970b229
GM
232(defun eudc-server-set (var val &optional server)
233 "Set the SERVER-local binding of VAR to VAL.
234If omitted SERVER defaults to the current value of `eudc-server'.
235The current binding of VAR is changed only if SERVER is omitted."
236 (if (eq 'unbound (eudc-variable-default-value var))
237 (eudc-default-set var (symbol-value var)))
238 (let* ((eudc-locals (get var 'eudc-locals))
239 (server-locals (eudc-plist-get eudc-locals 'server)))
240 (setq server-locals (plist-put server-locals (or server
241 eudc-server) val))
82d72d65 242 (setq eudc-locals
7970b229
GM
243 (plist-put eudc-locals 'server server-locals))
244 (put var 'eudc-locals eudc-locals)
245 (add-to-list 'eudc-local-vars var)
246 (unless server
247 (eudc-update-variable var))))
248
249
250(defun eudc-set (var val)
251 "Set the most local (server, protocol or default) binding of VAR to VAL.
252The current binding of VAR is also set to VAL"
82d72d65 253 (cond
7970b229
GM
254 ((not (eq 'unbound (eudc-variable-server-value var)))
255 (eudc-server-set var val))
256 ((not (eq 'unbound (eudc-variable-protocol-value var)))
257 (eudc-protocol-set var val))
258 (t
259 (eudc-default-set var val)))
260 (set var val))
261
262(defun eudc-variable-default-value (var)
263 "Return the default binding of VAR.
264Return `unbound' if VAR has no EUDC default value."
265 (let ((eudc-locals (get var 'eudc-locals)))
266 (if (and (boundp var)
267 eudc-locals)
268 (eudc-plist-get eudc-locals 'default 'unbound)
269 'unbound)))
270
271(defun eudc-variable-protocol-value (var &optional protocol)
272 "Return the value of VAR local to PROTOCOL.
273Return `unbound' if VAR has no value local to PROTOCOL.
274PROTOCOL defaults to `eudc-protocol'"
275 (let* ((eudc-locals (get var 'eudc-locals))
276 protocol-locals)
277 (if (not (and (boundp var)
278 eudc-locals
279 (eudc-plist-member eudc-locals 'protocol)))
280 'unbound
281 (setq protocol-locals (eudc-plist-get eudc-locals 'protocol))
82d72d65 282 (eudc-lax-plist-get protocol-locals
7970b229
GM
283 (or protocol
284 eudc-protocol) 'unbound))))
285
286(defun eudc-variable-server-value (var &optional server)
287 "Return the value of VAR local to SERVER.
288Return `unbound' if VAR has no value local to SERVER.
289SERVER defaults to `eudc-server'"
290 (let* ((eudc-locals (get var 'eudc-locals))
291 server-locals)
292 (if (not (and (boundp var)
293 eudc-locals
294 (eudc-plist-member eudc-locals 'server)))
295 'unbound
296 (setq server-locals (eudc-plist-get eudc-locals 'server))
6c83d99f 297 (eudc-lax-plist-get server-locals
7970b229
GM
298 (or server
299 eudc-server) 'unbound))))
300
301(defun eudc-update-variable (var)
302 "Set the value of VAR according to its locals.
303If the VAR has a server- or protocol-local value corresponding
304to the current `eudc-server' and `eudc-protocol' then it is set
305accordingly. Otherwise it is set to its EUDC default binding"
306 (let (val)
82d72d65 307 (cond
7970b229
GM
308 ((not (eq 'unbound (setq val (eudc-variable-server-value var))))
309 (set var val))
310 ((not (eq 'unbound (setq val (eudc-variable-protocol-value var))))
311 (set var val))
312 ((not (eq 'unbound (setq val (eudc-variable-default-value var))))
313 (set var val)))))
314
315(defun eudc-update-local-variables ()
316 "Update all EUDC variables according to their local settings."
317 (interactive)
318 (mapcar 'eudc-update-variable eudc-local-vars))
319
320(eudc-default-set 'eudc-query-function nil)
321(eudc-default-set 'eudc-list-attributes-function nil)
322(eudc-default-set 'eudc-protocol-attributes-translation-alist nil)
323(eudc-default-set 'eudc-bbdb-conversion-alist nil)
324(eudc-default-set 'eudc-switch-to-server-hook nil)
325(eudc-default-set 'eudc-switch-from-server-hook nil)
326(eudc-default-set 'eudc-protocol-has-default-query-attributes nil)
327(eudc-default-set 'eudc-attribute-display-method-alist nil)
328
329;;}}}
330
331
332;; Add PROTOCOL to the list of supported protocols
333(defun eudc-register-protocol (protocol)
334 (unless (memq protocol eudc-supported-protocols)
82d72d65 335 (setq eudc-supported-protocols
7970b229 336 (cons protocol eudc-supported-protocols))
82d72d65 337 (put 'eudc-protocol 'custom-type
7970b229 338 `(choice :menu-tag "Protocol"
82d72d65 339 ,@(mapcar (lambda (s)
7970b229
GM
340 (list 'string ':tag (symbol-name s)))
341 eudc-supported-protocols))))
342 (or (memq protocol eudc-known-protocols)
343 (setq eudc-known-protocols
344 (cons protocol eudc-known-protocols))))
345
346
347(defun eudc-translate-query (query)
348 "Translate attribute names of QUERY.
349The translation is done according to
350`eudc-protocol-attributes-translation-alist'."
351 (if eudc-protocol-attributes-translation-alist
4f91a816
SM
352 (mapcar (lambda (attribute)
353 (let ((trans (assq (car attribute)
354 (symbol-value eudc-protocol-attributes-translation-alist))))
355 (if trans
356 (cons (cdr trans) (cdr attribute))
357 attribute)))
7970b229 358 query)
82d72d65 359 query))
7970b229
GM
360
361(defun eudc-translate-attribute-list (list)
362 "Translate a list of attribute names LIST.
363The translation is done according to
364`eudc-protocol-attributes-translation-alist'."
365 (if eudc-protocol-attributes-translation-alist
366 (let (trans)
4f91a816 367 (mapcar (lambda (attribute)
7970b229
GM
368 (setq trans (assq attribute
369 (symbol-value eudc-protocol-attributes-translation-alist)))
370 (if trans
371 (cdr trans)
372 attribute))
373 list))
374 list))
375
53015965
PJ
376(defun eudc-select (choices beg end)
377 "Choose one from CHOICES using a completion.
378BEG and END delimit the text which is to be replaced."
379 (let ((replacement))
380 (setq replacement
6ce65ff6 381 (completing-read "Multiple matches found; choose one: "
53015965
PJ
382 (mapcar 'list choices)))
383 (delete-region beg end)
384 (insert replacement)))
7970b229
GM
385
386(defun eudc-query (query &optional return-attributes no-translation)
387 "Query the current directory server with QUERY.
388QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
389name and VALUE the corresponding value.
82d72d65 390If NO-TRANSLATION is non-nil, ATTR is translated according to
7970b229 391`eudc-protocol-attributes-translation-alist'.
82d72d65 392RETURN-ATTRIBUTES is a list of attributes to return defaulting to
7970b229
GM
393`eudc-default-return-attributes'."
394 (unless eudc-query-function
395 (error "Don't know how to perform the query"))
396 (if no-translation
397 (funcall eudc-query-function query (or return-attributes
398 eudc-default-return-attributes))
82d72d65
PJ
399
400 (funcall eudc-query-function
7970b229 401 (eudc-translate-query query)
82d72d65 402 (cond
7970b229
GM
403 (return-attributes
404 (eudc-translate-attribute-list return-attributes))
405 ((listp eudc-default-return-attributes)
406 (eudc-translate-attribute-list eudc-default-return-attributes))
407 (t
408 eudc-default-return-attributes)))))
409
410(defun eudc-format-attribute-name-for-display (attribute)
411 "Format a directory attribute name for display.
82d72d65 412ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
7970b229
GM
413by the corresponding user name if any. Otherwise it is capitalized and
414underscore characters are replaced by spaces."
415 (let ((match (assq attribute eudc-user-attribute-names-alist)))
416 (if match
417 (cdr match)
82d72d65
PJ
418 (capitalize
419 (mapconcat 'identity
7970b229
GM
420 (split-string (symbol-name attribute) "_")
421 " ")))))
422
423(defun eudc-print-attribute-value (field)
424 "Insert the value of the directory FIELD at point.
82d72d65
PJ
425The directory attribute name in car of FIELD is looked up in
426`eudc-attribute-display-method-alist' and the corresponding method,
7970b229
GM
427if any, is called to print the value in cdr of FIELD."
428 (let ((match (assoc (downcase (car field))
429 eudc-attribute-display-method-alist))
430 (col (current-column))
431 (val (cdr field)))
432 (if match
433 (progn
434 (eval (list (cdr match) val))
435 (insert "\n"))
436 (mapcar
437 (function
438 (lambda (val-elem)
439 (indent-to col)
440 (insert val-elem "\n")))
441 (cond
442 ((listp val) val)
443 ((stringp val) (split-string val "\n"))
444 ((null val) '(""))
445 (t (list val)))))))
446
447(defun eudc-print-record-field (field column-width)
448 "Print the record field FIELD.
449FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
82d72d65 450COLUMN-WIDTH is the width of the first display column containing the
7970b229
GM
451attribute name ATTR."
452 (let ((field-beg (point)))
453;; The record field that is passed to this function has already been processed
454;; by `eudc-format-attribute-name-for-display' so we don't need to call it
455;; again to display the attribute name
82d72d65 456 (insert (format (concat "%" (int-to-string column-width) "s: ")
7970b229
GM
457 (car field)))
458 (put-text-property field-beg (point) 'face 'bold)
459 (indent-to (+ 2 column-width))
460 (eudc-print-attribute-value field)))
461
462(defun eudc-display-records (records &optional raw-attr-names)
82d72d65 463 "Display the record list RECORDS in a formatted buffer.
7970b229
GM
464If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
465otherwise they are formatted according to `eudc-user-attribute-names-alist'."
e2c76fd8 466 (let (inhibit-read-only
7970b229
GM
467 precords
468 (width 0)
469 beg
470 first-record
471 attribute-name)
e2c76fd8
RS
472 (with-output-to-temp-buffer "*Directory Query Results*"
473 (with-current-buffer standard-output
474 (setq buffer-read-only t)
475 (setq inhibit-read-only t)
476 (erase-buffer)
477 (insert "Directory Query Result\n")
478 (insert "======================\n\n\n")
479 (if (null records)
480 (insert "No match found.\n"
481 (if eudc-strict-return-matches
482 "Try setting `eudc-strict-return-matches' to nil or change `eudc-default-return-attributes'.\n"
483 ""))
484 ;; Replace field names with user names, compute max width
485 (setq precords
82d72d65 486 (mapcar
7970b229 487 (function
e2c76fd8
RS
488 (lambda (record)
489 (mapcar
490 (function
491 (lambda (field)
492 (setq attribute-name
493 (if raw-attr-names
494 (symbol-name (car field))
495 (eudc-format-attribute-name-for-display (car field))))
496 (if (> (length attribute-name) width)
497 (setq width (length attribute-name)))
498 (cons attribute-name (cdr field))))
499 record)))
500 records))
501 ;; Display the records
502 (setq first-record (point))
8c4b8006 503 (mapc
e2c76fd8
RS
504 (function
505 (lambda (record)
506 (setq beg (point))
507 ;; Map over the record fields to print the attribute/value pairs
8c4b8006
GM
508 (mapc (function
509 (lambda (field)
510 (eudc-print-record-field field width)))
511 record)
e2c76fd8
RS
512 ;; Store the record internal format in some convenient place
513 (overlay-put (make-overlay beg (point))
514 'eudc-record
515 (car records))
516 (setq records (cdr records))
517 (insert "\n")))
518 precords))
519 (insert "\n")
520 (widget-create 'push-button
521 :notify (lambda (&rest ignore)
522 (eudc-query-form))
523 "New query")
524 (widget-insert " ")
525 (widget-create 'push-button
526 :notify (lambda (&rest ignore)
527 (kill-this-buffer))
528 "Quit")
529 (eudc-mode)
530 (widget-setup)
531 (if first-record
532 (goto-char first-record))))))
7970b229
GM
533
534(defun eudc-process-form ()
535 "Process the query form in current buffer and display the results."
536 (let (query-alist
537 value)
538 (if (not (and (boundp 'eudc-form-widget-list)
539 eudc-form-widget-list))
540 (error "Not in a directory query form buffer")
8c4b8006
GM
541 (mapc (function
542 (lambda (wid-field)
543 (setq value (widget-value (cdr wid-field)))
544 (if (not (string= value ""))
545 (setq query-alist (cons (cons (car wid-field) value)
546 query-alist)))))
547 eudc-form-widget-list)
7970b229
GM
548 (kill-buffer (current-buffer))
549 (eudc-display-records (eudc-query query-alist) eudc-use-raw-directory-names))))
82d72d65 550
7970b229
GM
551
552(defun eudc-filter-duplicate-attributes (record)
553 "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
554 (let ((rec record)
555 unique
556 duplicates
557 result)
558
559 ;; Search for multiple records
560 (while (and rec
561 (not (listp (eudc-cdar rec))))
562 (setq rec (cdr rec)))
563
564 (if (null (eudc-cdar rec))
565 (list record) ; No duplicate attrs in this record
8c4b8006
GM
566 (mapc (function
567 (lambda (field)
568 (if (listp (cdr field))
569 (setq duplicates (cons field duplicates))
570 (setq unique (cons field unique)))))
571 record)
7970b229
GM
572 (setq result (list unique))
573 ;; Map over the record fields that have multiple values
8c4b8006 574 (mapc
7970b229
GM
575 (function
576 (lambda (field)
577 (let ((method (if (consp eudc-duplicate-attribute-handling-method)
82d72d65
PJ
578 (cdr
579 (assq
580 (or
581 (car
582 (rassq
7970b229 583 (car field)
82d72d65 584 (symbol-value
7970b229
GM
585 eudc-protocol-attributes-translation-alist)))
586 (car field))
587 eudc-duplicate-attribute-handling-method))
588 eudc-duplicate-attribute-handling-method)))
589 (cond
590 ((or (null method) (eq 'list method))
82d72d65 591 (setq result
7970b229
GM
592 (eudc-add-field-to-records field result)))
593 ((eq 'first method)
82d72d65
PJ
594 (setq result
595 (eudc-add-field-to-records (cons (car field)
596 (eudc-cadr field))
7970b229
GM
597 result)))
598 ((eq 'concat method)
82d72d65 599 (setq result
7970b229 600 (eudc-add-field-to-records (cons (car field)
82d72d65 601 (mapconcat
7970b229
GM
602 'identity
603 (cdr field)
604 "\n")) result)))
605 ((eq 'duplicate method)
606 (setq result
607 (eudc-distribute-field-on-records field result)))))))
608 duplicates)
609 result)))
610
611(defun eudc-filter-partial-records (records attrs)
ff41c6f6 612 "Eliminate records that do not contain all ATTRS from RECORDS."
82d72d65
PJ
613 (delq nil
614 (mapcar
615 (function
7970b229 616 (lambda (rec)
82d72d65
PJ
617 (if (eval (cons 'and
618 (mapcar
619 (function
7970b229
GM
620 (lambda (attr)
621 (consp (assq attr rec))))
622 attrs)))
623 rec)))
624 records)))
82d72d65 625
7970b229
GM
626(defun eudc-add-field-to-records (field records)
627 "Add FIELD to each individual record in RECORDS and return the resulting list."
628 (mapcar (function
629 (lambda (r)
630 (cons field r)))
631 records))
632
633(defun eudc-distribute-field-on-records (field records)
634 "Duplicate each individual record in RECORDS according to value of FIELD.
635Each copy is added a new field containing one of the values of FIELD."
636 (let (result
637 (values (cdr field)))
638 ;; Uniquify values first
639 (while values
640 (setcdr values (delete (car values) (cdr values)))
641 (setq values (cdr values)))
8c4b8006 642 (mapc
7970b229
GM
643 (function
644 (lambda (value)
645 (let ((result-list (copy-sequence records)))
82d72d65 646 (setq result-list (eudc-add-field-to-records
7970b229
GM
647 (cons (car field) value)
648 result-list))
649 (setq result (append result-list result))
650 )))
651 (cdr field))
652 result))
653
654
655(defun eudc-mode ()
656 "Major mode used in buffers displaying the results of directory queries.
657There is no sense in calling this command from a buffer other than
658one containing the results of a directory query.
659
660These are the special commands of EUDC mode:
661 q -- Kill this buffer.
662 f -- Display a form to query the current directory server.
663 n -- Move to next record.
664 p -- Move to previous record.
665 b -- Insert record at point into the BBDB database."
666 (interactive)
667 (kill-all-local-variables)
668 (setq major-mode 'eudc-mode)
669 (setq mode-name "EUDC")
670 (use-local-map eudc-mode-map)
f8246027 671 (if (not (featurep 'xemacs))
7970b229
GM
672 (easy-menu-define eudc-emacs-menu eudc-mode-map "" (eudc-menu))
673 (setq mode-popup-menu (eudc-menu)))
57f9923e 674 (run-mode-hooks 'eudc-mode-hook))
7970b229 675
82d72d65 676;;}}}
7970b229
GM
677
678;;{{{ High-level interfaces (interactive functions)
679
680(defun eudc-customize ()
681 "Customize the EUDC package."
682 (interactive)
683 (customize-group 'eudc))
684
685;;;###autoload
686(defun eudc-set-server (server protocol &optional no-save)
687 "Set the directory server to SERVER using PROTOCOL.
82d72d65 688Unless NO-SAVE is non-nil, the server is saved as the default
7970b229
GM
689server for future sessions."
690 (interactive (list
691 (read-from-minibuffer "Directory Server: ")
82d72d65 692 (intern (completing-read "Protocol: "
4f91a816 693 (mapcar (lambda (elt)
7970b229
GM
694 (cons (symbol-name elt)
695 elt))
696 eudc-known-protocols)))))
697 (unless (or (member protocol
698 eudc-supported-protocols)
699 (load (concat "eudcb-" (symbol-name protocol)) t))
700 (error "Unsupported protocol: %s" protocol))
701 (run-hooks 'eudc-switch-from-server-hook)
702 (setq eudc-protocol protocol)
703 (setq eudc-server server)
704 (eudc-update-local-variables)
705 (run-hooks 'eudc-switch-to-server-hook)
32226619 706 (if (called-interactively-p 'interactive)
7970b229
GM
707 (message "Current directory server is now %s (%s)" eudc-server eudc-protocol))
708 (if (null no-save)
709 (eudc-save-options)))
710
711;;;###autoload
e2c76fd8
RS
712(defun eudc-get-email (name &optional error)
713 "Get the email field of NAME from the directory server.
714If ERROR is non-nil, report an error if there is none."
715 (interactive "sName: \np")
7970b229
GM
716 (or eudc-server
717 (call-interactively 'eudc-set-server))
718 (let ((result (eudc-query (list (cons 'name name)) '(email)))
719 email)
82d72d65 720 (if (null (cdr result))
7970b229 721 (setq email (eudc-cdaar result))
e2c76fd8
RS
722 (error "Multiple match--use the query form"))
723 (if error
7970b229
GM
724 (if email
725 (message "%s" email)
726 (error "No record matching %s" name)))
727 email))
728
729;;;###autoload
e2c76fd8
RS
730(defun eudc-get-phone (name &optional error)
731 "Get the phone field of NAME from the directory server.
732If ERROR is non-nil, report an error if there is none."
733 (interactive "sName: \np")
7970b229
GM
734 (or eudc-server
735 (call-interactively 'eudc-set-server))
736 (let ((result (eudc-query (list (cons 'name name)) '(phone)))
737 phone)
82d72d65 738 (if (null (cdr result))
7970b229 739 (setq phone (eudc-cdaar result))
e2c76fd8
RS
740 (error "Multiple match--use the query form"))
741 (if error
7970b229
GM
742 (if phone
743 (message "%s" phone)
744 (error "No record matching %s" name)))
745 phone))
746
747(defun eudc-get-attribute-list ()
748 "Return a list of valid attributes for the current server.
749When called interactively the list is formatted in a dedicated buffer
750otherwise a list of symbols is returned."
751 (interactive)
752 (if eudc-list-attributes-function
32226619
JB
753 (let ((entries (funcall eudc-list-attributes-function
754 (called-interactively-p 'interactive))))
82d72d65 755 (if entries
32226619 756 (if (called-interactively-p 'interactive)
7970b229
GM
757 (eudc-display-records entries t)
758 entries)))
759 (error "The %s protocol has no support for listing attributes" eudc-protocol)))
760
761(defun eudc-format-query (words format)
762 "Use FORMAT to build a EUDC query from WORDS."
763 (let (query
764 query-alist
765 key val cell)
766 (if format
767 (progn
768 (while (and words format)
82d72d65 769 (setq query-alist (cons (cons (car format) (car words))
7970b229
GM
770 query-alist))
771 (setq words (cdr words)
772 format (cdr format)))
773 ;; If the same attribute appears more than once, merge
774 ;; the corresponding values
775 (setq query-alist (nreverse query-alist))
776 (while query-alist
777 (setq key (eudc-caar query-alist)
778 val (eudc-cdar query-alist)
779 cell (assq key query))
780 (if cell
781 (setcdr cell (concat (cdr cell) " " val))
782 (setq query (cons (car query-alist) query)))
783 (setq query-alist (cdr query-alist)))
784 query)
785 (if eudc-protocol-has-default-query-attributes
786 (mapconcat 'identity words " ")
787 (list (cons 'name (mapconcat 'identity words " ")))))))
788
789(defun eudc-extract-n-word-formats (format-list n)
790 "Extract a list of N-long formats from FORMAT-LIST.
791If none try N - 1 and so forth."
792 (let (formats)
793 (while (and (null formats)
794 (> n 0))
6c83d99f 795 (setq formats
7970b229 796 (delq nil
4f91a816 797 (mapcar (lambda (format)
7970b229
GM
798 (if (= n
799 (length format))
800 format
801 nil))
802 format-list)))
803 (setq n (1- n)))
804 formats))
7970b229
GM
805
806
807;;;###autoload
808(defun eudc-expand-inline (&optional replace)
809 "Query the directory server, and expand the query string before point.
810The query string consists of the buffer substring from the point back to
82d72d65
PJ
811the preceding comma, colon or beginning of line.
812The variable `eudc-inline-query-format' controls how to associate the
7970b229 813individual inline query words with directory attribute names.
82d72d65 814After querying the server for the given string, the expansion specified by
7970b229 815`eudc-inline-expansion-format' is inserted in the buffer at point.
53015965
PJ
816If REPLACE is non-nil, then this expansion replaces the name in the buffer.
817`eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
82d72d65 818Multiple servers can be tried with the same query until one finds a match,
7970b229
GM
819see `eudc-inline-expansion-servers'"
820 (interactive)
82d72d65 821 (if (memq eudc-inline-expansion-servers
7970b229
GM
822 '(current-server server-then-hotlist))
823 (or eudc-server
824 (call-interactively 'eudc-set-server))
825 (or eudc-server-hotlist
826 (error "No server in the hotlist")))
827 (let* ((end (point))
828 (beg (save-excursion
82d72d65 829 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
9b026d9f 830 (point-at-bol) 'move)
7970b229
GM
831 (goto-char (match-end 0)))
832 (point)))
833 (query-words (split-string (buffer-substring beg end) "[ \t]+"))
834 query-formats
835 response
836 response-string
837 response-strings
838 (eudc-former-server eudc-server)
839 (eudc-former-protocol eudc-protocol)
840 servers)
841
842 ;; Prepare the list of servers to query
843 (setq servers (copy-sequence eudc-server-hotlist))
844 (setq servers
82d72d65 845 (cond
7970b229
GM
846 ((eq eudc-inline-expansion-servers 'hotlist)
847 eudc-server-hotlist)
848 ((eq eudc-inline-expansion-servers 'server-then-hotlist)
849 (cons (cons eudc-server eudc-protocol)
850 (delete (cons eudc-server eudc-protocol) servers)))
851 ((eq eudc-inline-expansion-servers 'current-server)
852 (list (cons eudc-server eudc-protocol)))
853 (t
854 (error "Wrong value for `eudc-inline-expansion-servers': %S"
855 eudc-inline-expansion-servers))))
856 (if (and eudc-max-servers-to-query
857 (> (length servers) eudc-max-servers-to-query))
858 (setcdr (nthcdr (1- eudc-max-servers-to-query) servers) nil))
859
860 (condition-case signal
861 (progn
82d72d65 862 (setq response
7970b229
GM
863 (catch 'found
864 ;; Loop on the servers
865 (while servers
866 (eudc-set-server (eudc-caar servers) (eudc-cdar servers) t)
82d72d65 867
7970b229
GM
868 ;; Determine which formats apply in the query-format list
869 (setq query-formats
82d72d65 870 (or
7970b229
GM
871 (eudc-extract-n-word-formats eudc-inline-query-format
872 (length query-words))
873 (if (null eudc-protocol-has-default-query-attributes)
874 '(name))))
82d72d65 875
7970b229
GM
876 ;; Loop on query-formats
877 (while query-formats
878 (setq response
879 (eudc-query
880 (eudc-format-query query-words (car query-formats))
881 (eudc-translate-attribute-list
882 (cdr eudc-inline-expansion-format))))
883 (if response
884 (throw 'found response))
885 (setq query-formats (cdr query-formats)))
886 (setq servers (cdr servers)))
887 ;; No more servers to try... no match found
888 nil))
889
890
891 (if (null response)
892 (error "No match")
82d72d65 893
7970b229
GM
894 ;; Process response through eudc-inline-expansion-format
895 (while response
82d72d65 896 (setq response-string (apply 'format
7970b229 897 (car eudc-inline-expansion-format)
82d72d65 898 (mapcar (function
7970b229 899 (lambda (field)
82d72d65 900 (or (cdr (assq field (car response)))
7970b229
GM
901 "")))
902 (eudc-translate-attribute-list
903 (cdr eudc-inline-expansion-format)))))
904 (if (> (length response-string) 0)
905 (setq response-strings
906 (cons response-string response-strings)))
907 (setq response (cdr response)))
82d72d65 908
7970b229
GM
909 (if (or
910 (and replace (not eudc-expansion-overwrites-query))
911 (and (not replace) eudc-expansion-overwrites-query))
53015965 912 (kill-ring-save beg end))
82d72d65 913 (cond
7970b229
GM
914 ((or (= (length response-strings) 1)
915 (null eudc-multiple-match-handling-method)
916 (eq eudc-multiple-match-handling-method 'first))
53015965 917 (delete-region beg end)
7970b229
GM
918 (insert (car response-strings)))
919 ((eq eudc-multiple-match-handling-method 'select)
53015965 920 (eudc-select response-strings beg end))
7970b229 921 ((eq eudc-multiple-match-handling-method 'all)
6ce65ff6 922 (delete-region beg end)
7970b229
GM
923 (insert (mapconcat 'identity response-strings ", ")))
924 ((eq eudc-multiple-match-handling-method 'abort)
53015965 925 (error "There is more than one match for the query"))))
7970b229
GM
926 (or (and (equal eudc-server eudc-former-server)
927 (equal eudc-protocol eudc-former-protocol))
928 (eudc-set-server eudc-former-server eudc-former-protocol t)))
6188ea49 929 (error
7970b229
GM
930 (or (and (equal eudc-server eudc-former-server)
931 (equal eudc-protocol eudc-former-protocol))
932 (eudc-set-server eudc-former-server eudc-former-protocol t))
933 (signal (car signal) (cdr signal))))))
82d72d65 934
7970b229
GM
935;;;###autoload
936(defun eudc-query-form (&optional get-fields-from-server)
937 "Display a form to query the directory server.
938If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
939queries the server for the existing fields and displays a corresponding form."
940 (interactive "P")
941 (let ((fields (or (and get-fields-from-server
942 (eudc-get-attribute-list))
943 eudc-query-form-attributes))
944 (buffer (get-buffer-create "*Directory Query Form*"))
945 prompts
946 widget
947 (width 0)
948 inhibit-read-only
949 pt)
950 (switch-to-buffer buffer)
951 (setq inhibit-read-only t)
952 (erase-buffer)
953 (kill-all-local-variables)
954 (make-local-variable 'eudc-form-widget-list)
955 (widget-insert "Directory Query Form\n")
956 (widget-insert "====================\n\n")
957 (widget-insert "Current server is: " (or eudc-server
82d72d65 958 (progn
7970b229
GM
959 (call-interactively 'eudc-set-server)
960 eudc-server))
961 "\n")
962 (widget-insert "Protocol : " (symbol-name eudc-protocol) "\n")
963 ;; Build the list of prompts
964 (setq prompts (if eudc-use-raw-directory-names
965 (mapcar 'symbol-name (eudc-translate-attribute-list fields))
966 (mapcar (function
967 (lambda (field)
968 (or (and (assq field eudc-user-attribute-names-alist)
969 (cdr (assq field eudc-user-attribute-names-alist)))
970 (capitalize (symbol-name field)))))
971 fields)))
972 ;; Loop over prompt strings to find the longest one
8c4b8006
GM
973 (mapc (function
974 (lambda (prompt)
975 (if (> (length prompt) width)
976 (setq width (length prompt)))))
977 prompts)
82d72d65
PJ
978 ;; Insert the first widget out of the mapcar to leave the cursor
979 ;; in the first field
7970b229
GM
980 (widget-insert "\n\n" (format (concat "%" (int-to-string width) "s: ") (car prompts)))
981 (setq pt (point))
982 (setq widget (widget-create 'editable-field :size 15))
983 (setq eudc-form-widget-list (cons (cons (car fields) widget)
984 eudc-form-widget-list))
985 (setq fields (cdr fields))
986 (setq prompts (cdr prompts))
8c4b8006
GM
987 (mapc (function
988 (lambda (field)
989 (widget-insert "\n\n" (format (concat "%" (int-to-string width) "s: ") (car prompts)))
990 (setq widget (widget-create 'editable-field
991 :size 15))
992 (setq eudc-form-widget-list (cons (cons field widget)
993 eudc-form-widget-list))
994 (setq prompts (cdr prompts))))
995 fields)
7970b229
GM
996 (widget-insert "\n\n")
997 (widget-create 'push-button
998 :notify (lambda (&rest ignore)
999 (eudc-process-form))
1000 "Query Server")
1001 (widget-insert " ")
1002 (widget-create 'push-button
1003 :notify (lambda (&rest ignore)
1004 (eudc-query-form))
1005 "Reset Form")
1006 (widget-insert " ")
1007 (widget-create 'push-button
1008 :notify (lambda (&rest ignore)
1009 (kill-this-buffer))
1010 "Quit")
1011 (goto-char pt)
1012 (use-local-map widget-keymap)
1013 (widget-setup))
1014 )
1015
1016(defun eudc-bookmark-server (server protocol)
1017 "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
1018 (interactive "sDirectory server: \nsProtocol: ")
1019 (if (member (cons server protocol) eudc-server-hotlist)
1020 (error "%s:%s is already in the hotlist" protocol server)
1021 (setq eudc-server-hotlist (cons (cons server protocol) eudc-server-hotlist))
1022 (eudc-install-menu)
1023 (eudc-save-options)))
1024
1025(defun eudc-bookmark-current-server ()
1026 "Add current server to the EUDC `servers' hotlist."
1027 (interactive)
1028 (eudc-bookmark-server eudc-server eudc-protocol))
1029
1030(defun eudc-save-options ()
1031 "Save options to `eudc-options-file'."
1032 (interactive)
9a529312 1033 (with-current-buffer (find-file-noselect eudc-options-file t)
7970b229
GM
1034 (goto-char (point-min))
1035 ;; delete the previous setq
1036 (let ((standard-output (current-buffer))
1037 provide-p
1038 set-hotlist-p
1039 set-server-p)
1040 (catch 'found
1041 (while t
1042 (let ((sexp (condition-case nil
1043 (read (current-buffer))
1044 (end-of-file (throw 'found nil)))))
1045 (if (listp sexp)
1046 (cond
1047 ((eq (car sexp) 'eudc-set-server)
1048 (delete-region (save-excursion
1049 (backward-sexp)
1050 (point))
1051 (point))
1052 (setq set-server-p t))
1053 ((and (eq (car sexp) 'setq)
1054 (eq (eudc-cadr sexp) 'eudc-server-hotlist))
1055 (delete-region (save-excursion
1056 (backward-sexp)
1057 (point))
1058 (point))
1059 (setq set-hotlist-p t))
1060 ((and (eq (car sexp) 'provide)
1061 (equal (eudc-cadr sexp) '(quote eudc-options-file)))
1062 (setq provide-p t)))
1063 (if (and provide-p
1064 set-hotlist-p
1065 set-server-p)
1066 (throw 'found t))))))
1067 (if (eq (point-min) (point-max))
1068 (princ ";; This file was automatically generated by eudc.el.\n\n"))
1069 (or provide-p
1070 (princ "(provide 'eudc-options-file)\n"))
1071 (or (bolp)
1072 (princ "\n"))
1073 (delete-blank-lines)
1074 (princ "(eudc-set-server ")
1075 (prin1 eudc-server)
1076 (princ " '")
1077 (prin1 eudc-protocol)
1078 (princ " t)\n")
1079 (princ "(setq eudc-server-hotlist '")
1080 (prin1 eudc-server-hotlist)
1081 (princ ")\n")
1082 (save-buffer))))
1083
1084(defun eudc-move-to-next-record ()
1085 "Move to next record, in a buffer displaying directory query results."
1086 (interactive)
1087 (if (not (eq major-mode 'eudc-mode))
1088 (error "Not in a EUDC buffer")
1089 (let ((pt (next-overlay-change (point))))
1090 (if (< pt (point-max))
1091 (goto-char (1+ pt))
1092 (error "No more records after point")))))
1093
1094(defun eudc-move-to-previous-record ()
1095 "Move to previous record, in a buffer displaying directory query results."
1096 (interactive)
1097 (if (not (eq major-mode 'eudc-mode))
1098 (error "Not in a EUDC buffer")
1099 (let ((pt (previous-overlay-change (point))))
1100 (if (> pt (point-min))
1101 (goto-char pt)
1102 (error "No more records before point")))))
1103
7970b229
GM
1104;;}}}
1105
6c83d99f 1106;;{{{ Menus and keymaps
7970b229
GM
1107
1108(require 'easymenu)
1109
7970b229
GM
1110(defconst eudc-custom-generated-menu (cdr (custom-menu-create 'eudc)))
1111
82d72d65 1112(defconst eudc-tail-menu
7970b229 1113 `(["---" nil nil]
7cd25617
DN
1114 ["Query with Form" eudc-query-form
1115 :help "Display a form to query the directory server"]
1116 ["Expand Inline Query" eudc-expand-inline
1117 :help "Query the directory server, and expand the query string before point"]
82d72d65 1118 ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
7970b229
GM
1119 (and (or (featurep 'bbdb)
1120 (prog1 (locate-library "bbdb") (message "")))
1121 (overlays-at (point))
7cd25617
DN
1122 (overlay-get (car (overlays-at (point))) 'eudc-record))
1123 :help "Insert record at point into the BBDB database"]
82d72d65 1124 ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
7970b229
GM
1125 (and (eq major-mode 'eudc-mode)
1126 (or (featurep 'bbdb)
7cd25617
DN
1127 (prog1 (locate-library "bbdb") (message ""))))
1128 :help "Insert all the records returned by a directory query into BBDB"]
7970b229 1129 ["---" nil nil]
7cd25617
DN
1130 ["Get Email" eudc-get-email
1131 :help "Get the email field of NAME from the directory server"]
1132 ["Get Phone" eudc-get-phone
1133 :help "Get the phone field of name from the directory server"]
1134 ["List Valid Attribute Names" eudc-get-attribute-list
1135 :help "Return a list of valid attributes for the current server"]
7970b229
GM
1136 ["---" nil nil]
1137 ,(cons "Customize" eudc-custom-generated-menu)))
7970b229 1138
82d72d65
PJ
1139
1140(defconst eudc-server-menu
7970b229 1141 '(["---" nil nil]
7cd25617
DN
1142 ["Bookmark Current Server" eudc-bookmark-current-server
1143 :help "Add current server to the EUDC `servers' hotlist"]
1144 ["Edit Server List" eudc-edit-hotlist
1145 :help "Edit the hotlist of directory servers in a specialized buffer"]
1146 ["New Server" eudc-set-server
1147 :help "Set the directory server to SERVER using PROTOCOL"]))
7970b229
GM
1148
1149(defun eudc-menu ()
1150 (let (command)
1151 (append '("Directory Search")
1152 (list
82d72d65 1153 (append
7970b229 1154 '("Server")
82d72d65
PJ
1155 (mapcar
1156 (function
7970b229
GM
1157 (lambda (servspec)
1158 (let* ((server (car servspec))
1159 (protocol (cdr servspec))
1160 (proto-name (symbol-name protocol)))
82d72d65
PJ
1161 (setq command (intern (concat "eudc-set-server-"
1162 server
1163 "-"
7970b229
GM
1164 proto-name)))
1165 (if (not (fboundp command))
82d72d65 1166 (fset command
7970b229
GM
1167 `(lambda ()
1168 (interactive)
1169 (eudc-set-server ,server (quote ,protocol))
82d72d65
PJ
1170 (message "Selected directory server is now %s (%s)"
1171 ,server
7970b229
GM
1172 ,proto-name))))
1173 (vector (format "%s (%s)" server proto-name)
1174 command
1175 :style 'radio
1176 :selected `(equal eudc-server ,server)))))
1177 eudc-server-hotlist)
1178 eudc-server-menu))
1179 eudc-tail-menu)))
1180
1181(defun eudc-install-menu ()
82d72d65 1182 (cond
f8246027 1183 ((and (featurep 'xemacs) (featurep 'menubar))
7970b229 1184 (add-submenu '("Tools") (eudc-menu)))
f8246027 1185 ((not (featurep 'xemacs))
82d72d65 1186 (cond
a13a3391
JPW
1187 ((fboundp 'easy-menu-create-menu)
1188 (define-key
1189 global-map
1190 [menu-bar tools directory-search]
1191 (cons "Directory Search"
1192 (easy-menu-create-menu "Directory Search" (cdr (eudc-menu))))))
7970b229
GM
1193 ((fboundp 'easy-menu-add-item)
1194 (let ((menu (eudc-menu)))
1195 (easy-menu-add-item nil '("tools") (easy-menu-create-menu (car menu)
1196 (cdr menu)))))
1197 ((fboundp 'easy-menu-create-keymaps)
1198 (easy-menu-define eudc-menu-map eudc-mode-map "Directory Client Menu" (eudc-menu))
82d72d65 1199 (define-key
7970b229 1200 global-map
82d72d65 1201 [menu-bar tools eudc]
7970b229
GM
1202 (cons "Directory Search"
1203 (easy-menu-create-keymaps "Directory Search" (cdr (eudc-menu))))))
1204 (t
1205 (error "Unknown version of easymenu"))))
1206 ))
1207
1208
1209;;; Load time initializations :
1210
1211;;; Load the options file
1212(if (and (not noninteractive)
1213 (and (locate-library eudc-options-file)
37269466 1214 (progn (message "") t)) ; Remove mode line message
7970b229
GM
1215 (not (featurep 'eudc-options-file)))
1216 (load eudc-options-file))
82d72d65 1217
7970b229
GM
1218;;; Install the full menu
1219(unless (featurep 'infodock)
1220 (eudc-install-menu))
1221
1222
1223;;; The following installs a short menu for EUDC at XEmacs startup.
1224
1225;;;###autoload
1226(defun eudc-load-eudc ()
1227 "Load the Emacs Unified Directory Client.
1228This does nothing except loading eudc by autoload side-effect."
1229 (interactive)
1230 nil)
1231
8e3aca37 1232;;;###autoload
7cd25617
DN
1233(cond
1234 ((not (featurep 'xemacs))
1235 (defvar eudc-tools-menu
1236 (let ((map (make-sparse-keymap "Directory Search")))
1237 (define-key map [phone]
8f43cbf3
DN
1238 `(menu-item ,(purecopy "Get Phone") eudc-get-phone
1239 :help ,(purecopy "Get the phone field of name from the directory server")))
7cd25617 1240 (define-key map [email]
8f43cbf3
DN
1241 `(menu-item ,(purecopy "Get Email") eudc-get-email
1242 :help ,(purecopy "Get the email field of NAME from the directory server")))
04991a1c 1243 (define-key map [separator-eudc-email] menu-bar-separator)
7cd25617 1244 (define-key map [expand-inline]
8f43cbf3
DN
1245 `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
1246 :help ,(purecopy "Query the directory server, and expand the query string before point")))
7cd25617 1247 (define-key map [query]
8f43cbf3
DN
1248 `(menu-item ,(purecopy "Query with Form") eudc-query-form
1249 :help ,(purecopy "Display a form to query the directory server")))
04991a1c 1250 (define-key map [separator-eudc-query] menu-bar-separator)
7cd25617 1251 (define-key map [new]
8f43cbf3
DN
1252 `(menu-item ,(purecopy "New Server") eudc-set-server
1253 :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
7cd25617 1254 (define-key map [load]
8f43cbf3
DN
1255 `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
1256 :help ,(purecopy "Load the Emacs Unified Directory Client")))
7cd25617
DN
1257 map))
1258 (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
1259 (t
1260 (let ((menu '("Directory Search"
1261 ["Load Hotlist of Servers" eudc-load-eudc t]
1262 ["New Server" eudc-set-server t]
1263 ["---" nil nil]
1264 ["Query with Form" eudc-query-form t]
1265 ["Expand Inline Query" eudc-expand-inline t]
1266 ["---" nil nil]
1267 ["Get Email" eudc-get-email t]
1268 ["Get Phone" eudc-get-phone t])))
1269 (if (not (featurep 'eudc-autoloads))
1270 (if (featurep 'xemacs)
1271 (if (and (featurep 'menubar)
1272 (not (featurep 'infodock)))
1273 (add-submenu '("Tools") menu))
1274 (require 'easymenu)
1275 (cond
1276 ((fboundp 'easy-menu-add-item)
1277 (easy-menu-add-item nil '("tools")
1278 (easy-menu-create-menu (car menu)
1279 (cdr menu))))
1280 ((fboundp 'easy-menu-create-keymaps)
1281 (define-key
1282 global-map
1283 [menu-bar tools eudc]
1284 (cons "Directory Search"
1285 (easy-menu-create-keymaps "Directory Search"
1286 (cdr menu)))))))))))
82d72d65 1287
8e3aca37 1288;;}}}
82d72d65 1289
7970b229
GM
1290(provide 'eudc)
1291
1292;;; eudc.el ends here