1 ;;; eudc.el --- Emacs Unified Directory Client
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Oscar Figueiredo <oscar@cpe.fr>
7 ;; Maintainer: Pavel JanÃk <Pavel@Janik.cz>
10 ;; This file is part of GNU Emacs.
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
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package provides a common interface to query directory servers using
27 ;; different protocols such as LDAP, CCSO PH/QI or BBDB. Queries can be
28 ;; made through an interactive form or inline. Inline query strings in
29 ;; buffers are expanded with appropriately formatted query results
30 ;; (especially used to expand email addresses in message buffers). EUDC
31 ;; also interfaces with the BBDB package to let you register query results
32 ;; into your own BBDB database.
35 ;; EUDC comes with an extensive documentation, please refer to it.
37 ;; The main entry points of EUDC are:
38 ;; `eudc-query-form': Query a directory server from a query form
39 ;; `eudc-expand-inline': Query a directory server for the e-mail address
40 ;; of the name before cursor and insert it in the
42 ;; `eudc-get-phone': Get a phone number from a directory server
43 ;; `eudc-get-email': Get an e-mail address from a directory server
44 ;; `eudc-customize': Customize various aspects of EUDC
51 (if (not (fboundp 'make-overlay
))
53 (if (not (fboundp 'unless
))
56 (unless (fboundp 'custom-menu-create
)
57 (autoload 'custom-menu-create
"cus-edit"))
63 ;;{{{ Internal cooking
65 ;;{{{ Internal variables and compatibility tricks
67 (defvar eudc-form-widget-list nil
)
70 (let ((map (make-sparse-keymap)))
71 (define-key map
"q" 'kill-this-buffer
)
72 (define-key map
"x" 'kill-this-buffer
)
73 (define-key map
"f" 'eudc-query-form
)
74 (define-key map
"b" 'eudc-try-bbdb-insert
)
75 (define-key map
"n" 'eudc-move-to-next-record
)
76 (define-key map
"p" 'eudc-move-to-previous-record
)
78 (set-keymap-parent eudc-mode-map widget-keymap
)
80 (defvar mode-popup-menu
)
82 ;; List of known servers
83 ;; Alist of (SERVER . PROTOCOL)
84 (defvar eudc-server-hotlist nil
)
86 ;; List of variables that have server- or protocol-local bindings
87 (defvar eudc-local-vars nil
)
89 ;; Protocol local. Query function
90 (defvar eudc-query-function nil
)
92 ;; Protocol local. A function that retrieves a list of valid attribute names
93 (defvar eudc-list-attributes-function nil
)
95 ;; Protocol local. A mapping between EUDC attribute names and corresponding
96 ;; protocol specific names. The following names are defined by EUDC and may be
97 ;; included in that list: `name' , `firstname', `email', `phone'
98 (defvar eudc-protocol-attributes-translation-alist nil
)
100 ;; Protocol local. Mapping between protocol attribute names and BBDB field
102 (defvar eudc-bbdb-conversion-alist nil
)
104 ;; Protocol/Server local. Hook called upon switching to that server
105 (defvar eudc-switch-to-server-hook nil
)
107 ;; Protocol/Server local. Hook called upon switching from that server
108 (defvar eudc-switch-from-server-hook nil
)
110 ;; Protocol local. Whether the protocol supports queries with no specified
112 (defvar eudc-protocol-has-default-query-attributes nil
)
114 (defun eudc-cadr (obj)
117 (defun eudc-cdar (obj)
120 (defun eudc-caar (obj)
123 (defun eudc-cdaar (obj)
124 (cdr (car (car obj
))))
126 (defun eudc-plist-member (plist prop
)
127 "Return t if PROP has a value specified in PLIST."
128 (if (not (= 0 (%
(length plist
) 2)))
129 (error "Malformed plist"))
132 (if (eq prop
(car plist
))
134 (setq plist
(cdr (cdr plist
))))
137 ;; Emacs' plist-get lacks third parameter
138 (defun eudc-plist-get (plist prop
&optional default
)
139 "Extract a value from a property list.
140 PLIST is a property list, which is a list of the form
141 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
142 corresponding to the given PROP, or DEFAULT if PROP is not
143 one of the properties on the list."
144 (if (eudc-plist-member plist prop
)
145 (plist-get plist prop
)
148 (defun eudc-lax-plist-get (plist prop
&optional default
)
149 "Extract a value from a lax property list.
151 PLIST is a lax property list, which is a list of the form (PROP1
152 VALUE1 PROP2 VALUE2...), where comparisons between properties are done
153 using `equal' instead of `eq'. This function returns the value
154 corresponding to PROP, or DEFAULT if PROP is not one of the
155 properties on the list."
156 (if (not (= 0 (%
(length plist
) 2)))
157 (error "Malformed plist"))
160 (if (equal prop
(car plist
))
161 (throw 'found
(car (cdr plist
))))
162 (setq plist
(cdr (cdr plist
))))
165 (if (not (fboundp 'split-string
))
166 (defun split-string (string &optional pattern
)
167 "Return a list of substrings of STRING which are separated by PATTERN.
168 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
170 (setq pattern
"[ \f\t\n\r\v]+"))
171 (let (parts (start 0))
172 (when (string-match pattern string
0)
173 (if (> (match-beginning 0) 0)
174 (setq parts
(cons (substring string
0 (match-beginning 0)) nil
)))
175 (setq start
(match-end 0))
176 (while (and (string-match pattern string start
)
177 (> (match-end 0) start
))
178 (setq parts
(cons (substring string start
(match-beginning 0)) parts
)
179 start
(match-end 0))))
180 (nreverse (if (< start
(length string
))
181 (cons (substring string start
) parts
)
184 (defun eudc-replace-in-string (str regexp newtext
)
185 "Replace all matches in STR for REGEXP with NEWTEXT.
186 Value is the new string."
190 (while (setq match
(string-match regexp str start
))
191 (setq prev-start start
195 (substring str prev-start match
)
197 (concat rtn-str
(substring str start
))))
201 ;;{{{ Server and Protocol Variable Routines
203 (defun eudc-server-local-variable-p (var)
204 "Return non-nil if VAR has server-local bindings."
205 (eudc-plist-member (get var
'eudc-locals
) 'server
))
207 (defun eudc-protocol-local-variable-p (var)
208 "Return non-nil if VAR has protocol-local bindings."
209 (eudc-plist-member (get var
'eudc-locals
) 'protocol
))
211 (defun eudc-default-set (var val
)
212 "Set the EUDC default value of VAR to VAL.
213 The current binding of VAR is not changed."
214 (put var
'eudc-locals
215 (plist-put (get var
'eudc-locals
) 'default val
))
216 (add-to-list 'eudc-local-vars var
))
218 (defun eudc-protocol-set (var val
&optional protocol
)
219 "Set the PROTOCOL-local binding of VAR to VAL.
220 If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
221 The current binding of VAR is changed only if PROTOCOL is omitted."
222 (if (eq 'unbound
(eudc-variable-default-value var
))
223 (eudc-default-set var
(symbol-value var
)))
224 (let* ((eudc-locals (get var
'eudc-locals
))
225 (protocol-locals (eudc-plist-get eudc-locals
'protocol
)))
226 (setq protocol-locals
(plist-put protocol-locals
(or protocol
229 (plist-put eudc-locals
'protocol protocol-locals
))
230 (put var
'eudc-locals eudc-locals
)
231 (add-to-list 'eudc-local-vars var
)
233 (eudc-update-variable var
))))
235 (defun eudc-server-set (var val
&optional server
)
236 "Set the SERVER-local binding of VAR to VAL.
237 If omitted SERVER defaults to the current value of `eudc-server'.
238 The current binding of VAR is changed only if SERVER is omitted."
239 (if (eq 'unbound
(eudc-variable-default-value var
))
240 (eudc-default-set var
(symbol-value var
)))
241 (let* ((eudc-locals (get var
'eudc-locals
))
242 (server-locals (eudc-plist-get eudc-locals
'server
)))
243 (setq server-locals
(plist-put server-locals
(or server
246 (plist-put eudc-locals
'server server-locals
))
247 (put var
'eudc-locals eudc-locals
)
248 (add-to-list 'eudc-local-vars var
)
250 (eudc-update-variable var
))))
253 (defun eudc-set (var val
)
254 "Set the most local (server, protocol or default) binding of VAR to VAL.
255 The current binding of VAR is also set to VAL"
257 ((not (eq 'unbound
(eudc-variable-server-value var
)))
258 (eudc-server-set var val
))
259 ((not (eq 'unbound
(eudc-variable-protocol-value var
)))
260 (eudc-protocol-set var val
))
262 (eudc-default-set var val
)))
265 (defun eudc-variable-default-value (var)
266 "Return the default binding of VAR.
267 Return `unbound' if VAR has no EUDC default value."
268 (let ((eudc-locals (get var
'eudc-locals
)))
269 (if (and (boundp var
)
271 (eudc-plist-get eudc-locals
'default
'unbound
)
274 (defun eudc-variable-protocol-value (var &optional protocol
)
275 "Return the value of VAR local to PROTOCOL.
276 Return `unbound' if VAR has no value local to PROTOCOL.
277 PROTOCOL defaults to `eudc-protocol'"
278 (let* ((eudc-locals (get var
'eudc-locals
))
280 (if (not (and (boundp var
)
282 (eudc-plist-member eudc-locals
'protocol
)))
284 (setq protocol-locals
(eudc-plist-get eudc-locals
'protocol
))
285 (eudc-lax-plist-get protocol-locals
287 eudc-protocol
) 'unbound
))))
289 (defun eudc-variable-server-value (var &optional server
)
290 "Return the value of VAR local to SERVER.
291 Return `unbound' if VAR has no value local to SERVER.
292 SERVER defaults to `eudc-server'"
293 (let* ((eudc-locals (get var
'eudc-locals
))
295 (if (not (and (boundp var
)
297 (eudc-plist-member eudc-locals
'server
)))
299 (setq server-locals
(eudc-plist-get eudc-locals
'server
))
300 (eudc-lax-plist-get server-locals
302 eudc-server
) 'unbound
))))
304 (defun eudc-update-variable (var)
305 "Set the value of VAR according to its locals.
306 If the VAR has a server- or protocol-local value corresponding
307 to the current `eudc-server' and `eudc-protocol' then it is set
308 accordingly. Otherwise it is set to its EUDC default binding"
311 ((not (eq 'unbound
(setq val
(eudc-variable-server-value var
))))
313 ((not (eq 'unbound
(setq val
(eudc-variable-protocol-value var
))))
315 ((not (eq 'unbound
(setq val
(eudc-variable-default-value var
))))
318 (defun eudc-update-local-variables ()
319 "Update all EUDC variables according to their local settings."
321 (mapcar 'eudc-update-variable eudc-local-vars
))
323 (eudc-default-set 'eudc-query-function nil
)
324 (eudc-default-set 'eudc-list-attributes-function nil
)
325 (eudc-default-set 'eudc-protocol-attributes-translation-alist nil
)
326 (eudc-default-set 'eudc-bbdb-conversion-alist nil
)
327 (eudc-default-set 'eudc-switch-to-server-hook nil
)
328 (eudc-default-set 'eudc-switch-from-server-hook nil
)
329 (eudc-default-set 'eudc-protocol-has-default-query-attributes nil
)
330 (eudc-default-set 'eudc-attribute-display-method-alist nil
)
335 ;; Add PROTOCOL to the list of supported protocols
336 (defun eudc-register-protocol (protocol)
337 (unless (memq protocol eudc-supported-protocols
)
338 (setq eudc-supported-protocols
339 (cons protocol eudc-supported-protocols
))
340 (put 'eudc-protocol
'custom-type
341 `(choice :menu-tag
"Protocol"
342 ,@(mapcar (lambda (s)
343 (list 'string
':tag
(symbol-name s
)))
344 eudc-supported-protocols
))))
345 (or (memq protocol eudc-known-protocols
)
346 (setq eudc-known-protocols
347 (cons protocol eudc-known-protocols
))))
350 (defun eudc-translate-query (query)
351 "Translate attribute names of QUERY.
352 The translation is done according to
353 `eudc-protocol-attributes-translation-alist'."
354 (if eudc-protocol-attributes-translation-alist
355 (mapcar '(lambda (attribute)
356 (let ((trans (assq (car attribute
)
357 (symbol-value eudc-protocol-attributes-translation-alist
))))
359 (cons (cdr trans
) (cdr attribute
))
364 (defun eudc-translate-attribute-list (list)
365 "Translate a list of attribute names LIST.
366 The translation is done according to
367 `eudc-protocol-attributes-translation-alist'."
368 (if eudc-protocol-attributes-translation-alist
370 (mapcar '(lambda (attribute)
371 (setq trans
(assq attribute
372 (symbol-value eudc-protocol-attributes-translation-alist
)))
379 (defun eudc-select (choices beg end
)
380 "Choose one from CHOICES using a completion.
381 BEG and END delimit the text which is to be replaced."
384 (completing-read "Multiple matches found; choose one: "
385 (mapcar 'list choices
)))
386 (delete-region beg end
)
387 (insert replacement
)))
389 (defun eudc-query (query &optional return-attributes no-translation
)
390 "Query the current directory server with QUERY.
391 QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
392 name and VALUE the corresponding value.
393 If NO-TRANSLATION is non-nil, ATTR is translated according to
394 `eudc-protocol-attributes-translation-alist'.
395 RETURN-ATTRIBUTES is a list of attributes to return defaulting to
396 `eudc-default-return-attributes'."
397 (unless eudc-query-function
398 (error "Don't know how to perform the query"))
400 (funcall eudc-query-function query
(or return-attributes
401 eudc-default-return-attributes
))
403 (funcall eudc-query-function
404 (eudc-translate-query query
)
407 (eudc-translate-attribute-list return-attributes
))
408 ((listp eudc-default-return-attributes
)
409 (eudc-translate-attribute-list eudc-default-return-attributes
))
411 eudc-default-return-attributes
)))))
413 (defun eudc-format-attribute-name-for-display (attribute)
414 "Format a directory attribute name for display.
415 ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
416 by the corresponding user name if any. Otherwise it is capitalized and
417 underscore characters are replaced by spaces."
418 (let ((match (assq attribute eudc-user-attribute-names-alist
)))
423 (split-string (symbol-name attribute
) "_")
426 (defun eudc-print-attribute-value (field)
427 "Insert the value of the directory FIELD at point.
428 The directory attribute name in car of FIELD is looked up in
429 `eudc-attribute-display-method-alist' and the corresponding method,
430 if any, is called to print the value in cdr of FIELD."
431 (let ((match (assoc (downcase (car field
))
432 eudc-attribute-display-method-alist
))
433 (col (current-column))
437 (eval (list (cdr match
) val
))
443 (insert val-elem
"\n")))
446 ((stringp val
) (split-string val
"\n"))
450 (defun eudc-print-record-field (field column-width
)
451 "Print the record field FIELD.
452 FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
453 COLUMN-WIDTH is the width of the first display column containing the
454 attribute name ATTR."
455 (let ((field-beg (point)))
456 ;; The record field that is passed to this function has already been processed
457 ;; by `eudc-format-attribute-name-for-display' so we don't need to call it
458 ;; again to display the attribute name
459 (insert (format (concat "%" (int-to-string column-width
) "s: ")
461 (put-text-property field-beg
(point) 'face
'bold
)
462 (indent-to (+ 2 column-width
))
463 (eudc-print-attribute-value field
)))
465 (defun eudc-display-records (records &optional raw-attr-names
)
466 "Display the record list RECORDS in a formatted buffer.
467 If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
468 otherwise they are formatted according to `eudc-user-attribute-names-alist'."
469 (let (inhibit-read-only
475 (with-output-to-temp-buffer "*Directory Query Results*"
476 (with-current-buffer standard-output
477 (setq buffer-read-only t
)
478 (setq inhibit-read-only t
)
480 (insert "Directory Query Result\n")
481 (insert "======================\n\n\n")
483 (insert "No match found.\n"
484 (if eudc-strict-return-matches
485 "Try setting `eudc-strict-return-matches' to nil or change `eudc-default-return-attributes'.\n"
487 ;; Replace field names with user names, compute max width
497 (symbol-name (car field
))
498 (eudc-format-attribute-name-for-display (car field
))))
499 (if (> (length attribute-name
) width
)
500 (setq width
(length attribute-name
)))
501 (cons attribute-name
(cdr field
))))
504 ;; Display the records
505 (setq first-record
(point))
510 ;; Map over the record fields to print the attribute/value pairs
513 (eudc-print-record-field field width
)))
515 ;; Store the record internal format in some convenient place
516 (overlay-put (make-overlay beg
(point))
519 (setq records
(cdr records
))
523 (widget-create 'push-button
524 :notify
(lambda (&rest ignore
)
528 (widget-create 'push-button
529 :notify
(lambda (&rest ignore
)
535 (goto-char first-record
))))))
537 (defun eudc-process-form ()
538 "Process the query form in current buffer and display the results."
541 (if (not (and (boundp 'eudc-form-widget-list
)
542 eudc-form-widget-list
))
543 (error "Not in a directory query form buffer")
546 (setq value
(widget-value (cdr wid-field
)))
547 (if (not (string= value
""))
548 (setq query-alist
(cons (cons (car wid-field
) value
)
550 eudc-form-widget-list
)
551 (kill-buffer (current-buffer))
552 (eudc-display-records (eudc-query query-alist
) eudc-use-raw-directory-names
))))
555 (defun eudc-filter-duplicate-attributes (record)
556 "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
562 ;; Search for multiple records
564 (not (listp (eudc-cdar rec
))))
565 (setq rec
(cdr rec
)))
567 (if (null (eudc-cdar rec
))
568 (list record
) ; No duplicate attrs in this record
571 (if (listp (cdr field
))
572 (setq duplicates
(cons field duplicates
))
573 (setq unique
(cons field unique
)))))
575 (setq result
(list unique
))
576 ;; Map over the record fields that have multiple values
580 (let ((method (if (consp eudc-duplicate-attribute-handling-method
)
588 eudc-protocol-attributes-translation-alist
)))
590 eudc-duplicate-attribute-handling-method
))
591 eudc-duplicate-attribute-handling-method
)))
593 ((or (null method
) (eq 'list method
))
595 (eudc-add-field-to-records field result
)))
598 (eudc-add-field-to-records (cons (car field
)
603 (eudc-add-field-to-records (cons (car field
)
608 ((eq 'duplicate method
)
610 (eudc-distribute-field-on-records field result
)))))))
614 (defun eudc-filter-partial-records (records attrs
)
615 "Eliminate records that do not contain all ATTRS from RECORDS."
624 (consp (assq attr rec
))))
629 (defun eudc-add-field-to-records (field records
)
630 "Add FIELD to each individual record in RECORDS and return the resulting list."
636 (defun eudc-distribute-field-on-records (field records
)
637 "Duplicate each individual record in RECORDS according to value of FIELD.
638 Each copy is added a new field containing one of the values of FIELD."
640 (values (cdr field
)))
641 ;; Uniquify values first
643 (setcdr values
(delete (car values
) (cdr values
)))
644 (setq values
(cdr values
)))
648 (let ((result-list (copy-sequence records
)))
649 (setq result-list
(eudc-add-field-to-records
650 (cons (car field
) value
)
652 (setq result
(append result-list result
))
659 "Major mode used in buffers displaying the results of directory queries.
660 There is no sense in calling this command from a buffer other than
661 one containing the results of a directory query.
663 These are the special commands of EUDC mode:
664 q -- Kill this buffer.
665 f -- Display a form to query the current directory server.
666 n -- Move to next record.
667 p -- Move to previous record.
668 b -- Insert record at point into the BBDB database."
670 (kill-all-local-variables)
671 (setq major-mode
'eudc-mode
)
672 (setq mode-name
"EUDC")
673 (use-local-map eudc-mode-map
)
674 (if (not (featurep 'xemacs
))
675 (easy-menu-define eudc-emacs-menu eudc-mode-map
"" (eudc-menu))
676 (setq mode-popup-menu
(eudc-menu)))
677 (run-mode-hooks 'eudc-mode-hook
))
681 ;;{{{ High-level interfaces (interactive functions)
683 (defun eudc-customize ()
684 "Customize the EUDC package."
686 (customize-group 'eudc
))
689 (defun eudc-set-server (server protocol
&optional no-save
)
690 "Set the directory server to SERVER using PROTOCOL.
691 Unless NO-SAVE is non-nil, the server is saved as the default
692 server for future sessions."
694 (read-from-minibuffer "Directory Server: ")
695 (intern (completing-read "Protocol: "
696 (mapcar '(lambda (elt)
697 (cons (symbol-name elt
)
699 eudc-known-protocols
)))))
700 (unless (or (member protocol
701 eudc-supported-protocols
)
702 (load (concat "eudcb-" (symbol-name protocol
)) t
))
703 (error "Unsupported protocol: %s" protocol
))
704 (run-hooks 'eudc-switch-from-server-hook
)
705 (setq eudc-protocol protocol
)
706 (setq eudc-server server
)
707 (eudc-update-local-variables)
708 (run-hooks 'eudc-switch-to-server-hook
)
709 (if (called-interactively-p 'interactive
)
710 (message "Current directory server is now %s (%s)" eudc-server eudc-protocol
))
712 (eudc-save-options)))
715 (defun eudc-get-email (name &optional error
)
716 "Get the email field of NAME from the directory server.
717 If ERROR is non-nil, report an error if there is none."
718 (interactive "sName: \np")
720 (call-interactively 'eudc-set-server
))
721 (let ((result (eudc-query (list (cons 'name name
)) '(email)))
723 (if (null (cdr result
))
724 (setq email
(eudc-cdaar result
))
725 (error "Multiple match--use the query form"))
729 (error "No record matching %s" name
)))
733 (defun eudc-get-phone (name &optional error
)
734 "Get the phone field of NAME from the directory server.
735 If ERROR is non-nil, report an error if there is none."
736 (interactive "sName: \np")
738 (call-interactively 'eudc-set-server
))
739 (let ((result (eudc-query (list (cons 'name name
)) '(phone)))
741 (if (null (cdr result
))
742 (setq phone
(eudc-cdaar result
))
743 (error "Multiple match--use the query form"))
747 (error "No record matching %s" name
)))
750 (defun eudc-get-attribute-list ()
751 "Return a list of valid attributes for the current server.
752 When called interactively the list is formatted in a dedicated buffer
753 otherwise a list of symbols is returned."
755 (if eudc-list-attributes-function
756 (let ((entries (funcall eudc-list-attributes-function
757 (called-interactively-p 'interactive
))))
759 (if (called-interactively-p 'interactive
)
760 (eudc-display-records entries t
)
762 (error "The %s protocol has no support for listing attributes" eudc-protocol
)))
764 (defun eudc-format-query (words format
)
765 "Use FORMAT to build a EUDC query from WORDS."
771 (while (and words format
)
772 (setq query-alist
(cons (cons (car format
) (car words
))
774 (setq words
(cdr words
)
775 format
(cdr format
)))
776 ;; If the same attribute appears more than once, merge
777 ;; the corresponding values
778 (setq query-alist
(nreverse query-alist
))
780 (setq key
(eudc-caar query-alist
)
781 val
(eudc-cdar query-alist
)
782 cell
(assq key query
))
784 (setcdr cell
(concat (cdr cell
) " " val
))
785 (setq query
(cons (car query-alist
) query
)))
786 (setq query-alist
(cdr query-alist
)))
788 (if eudc-protocol-has-default-query-attributes
789 (mapconcat 'identity words
" ")
790 (list (cons 'name
(mapconcat 'identity words
" ")))))))
792 (defun eudc-extract-n-word-formats (format-list n
)
793 "Extract a list of N-long formats from FORMAT-LIST.
794 If none try N - 1 and so forth."
796 (while (and (null formats
)
800 (mapcar '(lambda (format)
811 (defun eudc-expand-inline (&optional replace
)
812 "Query the directory server, and expand the query string before point.
813 The query string consists of the buffer substring from the point back to
814 the preceding comma, colon or beginning of line.
815 The variable `eudc-inline-query-format' controls how to associate the
816 individual inline query words with directory attribute names.
817 After querying the server for the given string, the expansion specified by
818 `eudc-inline-expansion-format' is inserted in the buffer at point.
819 If REPLACE is non-nil, then this expansion replaces the name in the buffer.
820 `eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
821 Multiple servers can be tried with the same query until one finds a match,
822 see `eudc-inline-expansion-servers'"
824 (if (memq eudc-inline-expansion-servers
825 '(current-server server-then-hotlist
))
827 (call-interactively 'eudc-set-server
))
828 (or eudc-server-hotlist
829 (error "No server in the hotlist")))
832 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
833 (point-at-bol) 'move
)
834 (goto-char (match-end 0)))
836 (query-words (split-string (buffer-substring beg end
) "[ \t]+"))
841 (eudc-former-server eudc-server
)
842 (eudc-former-protocol eudc-protocol
)
845 ;; Prepare the list of servers to query
846 (setq servers
(copy-sequence eudc-server-hotlist
))
849 ((eq eudc-inline-expansion-servers
'hotlist
)
851 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
852 (cons (cons eudc-server eudc-protocol
)
853 (delete (cons eudc-server eudc-protocol
) servers
)))
854 ((eq eudc-inline-expansion-servers
'current-server
)
855 (list (cons eudc-server eudc-protocol
)))
857 (error "Wrong value for `eudc-inline-expansion-servers': %S"
858 eudc-inline-expansion-servers
))))
859 (if (and eudc-max-servers-to-query
860 (> (length servers
) eudc-max-servers-to-query
))
861 (setcdr (nthcdr (1- eudc-max-servers-to-query
) servers
) nil
))
863 (condition-case signal
867 ;; Loop on the servers
869 (eudc-set-server (eudc-caar servers
) (eudc-cdar servers
) t
)
871 ;; Determine which formats apply in the query-format list
874 (eudc-extract-n-word-formats eudc-inline-query-format
875 (length query-words
))
876 (if (null eudc-protocol-has-default-query-attributes
)
879 ;; Loop on query-formats
883 (eudc-format-query query-words
(car query-formats
))
884 (eudc-translate-attribute-list
885 (cdr eudc-inline-expansion-format
))))
887 (throw 'found response
))
888 (setq query-formats
(cdr query-formats
)))
889 (setq servers
(cdr servers
)))
890 ;; No more servers to try... no match found
897 ;; Process response through eudc-inline-expansion-format
899 (setq response-string
(apply 'format
900 (car eudc-inline-expansion-format
)
903 (or (cdr (assq field
(car response
)))
905 (eudc-translate-attribute-list
906 (cdr eudc-inline-expansion-format
)))))
907 (if (> (length response-string
) 0)
908 (setq response-strings
909 (cons response-string response-strings
)))
910 (setq response
(cdr response
)))
913 (and replace
(not eudc-expansion-overwrites-query
))
914 (and (not replace
) eudc-expansion-overwrites-query
))
915 (kill-ring-save beg end
))
917 ((or (= (length response-strings
) 1)
918 (null eudc-multiple-match-handling-method
)
919 (eq eudc-multiple-match-handling-method
'first
))
920 (delete-region beg end
)
921 (insert (car response-strings
)))
922 ((eq eudc-multiple-match-handling-method
'select
)
923 (eudc-select response-strings beg end
))
924 ((eq eudc-multiple-match-handling-method
'all
)
925 (delete-region beg end
)
926 (insert (mapconcat 'identity response-strings
", ")))
927 ((eq eudc-multiple-match-handling-method
'abort
)
928 (error "There is more than one match for the query"))))
929 (or (and (equal eudc-server eudc-former-server
)
930 (equal eudc-protocol eudc-former-protocol
))
931 (eudc-set-server eudc-former-server eudc-former-protocol t
)))
933 (or (and (equal eudc-server eudc-former-server
)
934 (equal eudc-protocol eudc-former-protocol
))
935 (eudc-set-server eudc-former-server eudc-former-protocol t
))
936 (signal (car signal
) (cdr signal
))))))
939 (defun eudc-query-form (&optional get-fields-from-server
)
940 "Display a form to query the directory server.
941 If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
942 queries the server for the existing fields and displays a corresponding form."
944 (let ((fields (or (and get-fields-from-server
945 (eudc-get-attribute-list))
946 eudc-query-form-attributes
))
947 (buffer (get-buffer-create "*Directory Query Form*"))
953 (switch-to-buffer buffer
)
954 (setq inhibit-read-only t
)
956 (kill-all-local-variables)
957 (make-local-variable 'eudc-form-widget-list
)
958 (widget-insert "Directory Query Form\n")
959 (widget-insert "====================\n\n")
960 (widget-insert "Current server is: " (or eudc-server
962 (call-interactively 'eudc-set-server
)
965 (widget-insert "Protocol : " (symbol-name eudc-protocol
) "\n")
966 ;; Build the list of prompts
967 (setq prompts
(if eudc-use-raw-directory-names
968 (mapcar 'symbol-name
(eudc-translate-attribute-list fields
))
971 (or (and (assq field eudc-user-attribute-names-alist
)
972 (cdr (assq field eudc-user-attribute-names-alist
)))
973 (capitalize (symbol-name field
)))))
975 ;; Loop over prompt strings to find the longest one
978 (if (> (length prompt
) width
)
979 (setq width
(length prompt
)))))
981 ;; Insert the first widget out of the mapcar to leave the cursor
982 ;; in the first field
983 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
985 (setq widget
(widget-create 'editable-field
:size
15))
986 (setq eudc-form-widget-list
(cons (cons (car fields
) widget
)
987 eudc-form-widget-list
))
988 (setq fields
(cdr fields
))
989 (setq prompts
(cdr prompts
))
992 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
993 (setq widget
(widget-create 'editable-field
995 (setq eudc-form-widget-list
(cons (cons field widget
)
996 eudc-form-widget-list
))
997 (setq prompts
(cdr prompts
))))
999 (widget-insert "\n\n")
1000 (widget-create 'push-button
1001 :notify
(lambda (&rest ignore
)
1002 (eudc-process-form))
1005 (widget-create 'push-button
1006 :notify
(lambda (&rest ignore
)
1010 (widget-create 'push-button
1011 :notify
(lambda (&rest ignore
)
1015 (use-local-map widget-keymap
)
1019 (defun eudc-bookmark-server (server protocol
)
1020 "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
1021 (interactive "sDirectory server: \nsProtocol: ")
1022 (if (member (cons server protocol
) eudc-server-hotlist
)
1023 (error "%s:%s is already in the hotlist" protocol server
)
1024 (setq eudc-server-hotlist
(cons (cons server protocol
) eudc-server-hotlist
))
1026 (eudc-save-options)))
1028 (defun eudc-bookmark-current-server ()
1029 "Add current server to the EUDC `servers' hotlist."
1031 (eudc-bookmark-server eudc-server eudc-protocol
))
1033 (defun eudc-save-options ()
1034 "Save options to `eudc-options-file'."
1036 (with-current-buffer (find-file-noselect eudc-options-file t
)
1037 (goto-char (point-min))
1038 ;; delete the previous setq
1039 (let ((standard-output (current-buffer))
1045 (let ((sexp (condition-case nil
1046 (read (current-buffer))
1047 (end-of-file (throw 'found nil
)))))
1050 ((eq (car sexp
) 'eudc-set-server
)
1051 (delete-region (save-excursion
1055 (setq set-server-p t
))
1056 ((and (eq (car sexp
) 'setq
)
1057 (eq (eudc-cadr sexp
) 'eudc-server-hotlist
))
1058 (delete-region (save-excursion
1062 (setq set-hotlist-p t
))
1063 ((and (eq (car sexp
) 'provide
)
1064 (equal (eudc-cadr sexp
) '(quote eudc-options-file
)))
1065 (setq provide-p t
)))
1069 (throw 'found t
))))))
1070 (if (eq (point-min) (point-max))
1071 (princ ";; This file was automatically generated by eudc.el.\n\n"))
1073 (princ "(provide 'eudc-options-file)\n"))
1076 (delete-blank-lines)
1077 (princ "(eudc-set-server ")
1080 (prin1 eudc-protocol
)
1082 (princ "(setq eudc-server-hotlist '")
1083 (prin1 eudc-server-hotlist
)
1087 (defun eudc-move-to-next-record ()
1088 "Move to next record, in a buffer displaying directory query results."
1090 (if (not (eq major-mode
'eudc-mode
))
1091 (error "Not in a EUDC buffer")
1092 (let ((pt (next-overlay-change (point))))
1093 (if (< pt
(point-max))
1095 (error "No more records after point")))))
1097 (defun eudc-move-to-previous-record ()
1098 "Move to previous record, in a buffer displaying directory query results."
1100 (if (not (eq major-mode
'eudc-mode
))
1101 (error "Not in a EUDC buffer")
1102 (let ((pt (previous-overlay-change (point))))
1103 (if (> pt
(point-min))
1105 (error "No more records before point")))))
1109 ;;{{{ Menus and keymaps
1113 (defconst eudc-custom-generated-menu
(cdr (custom-menu-create 'eudc
)))
1115 (defconst eudc-tail-menu
1117 ["Query with Form" eudc-query-form
1118 :help
"Display a form to query the directory server"]
1119 ["Expand Inline Query" eudc-expand-inline
1120 :help
"Query the directory server, and expand the query string before point"]
1121 ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
1122 (and (or (featurep 'bbdb
)
1123 (prog1 (locate-library "bbdb") (message "")))
1124 (overlays-at (point))
1125 (overlay-get (car (overlays-at (point))) 'eudc-record
))
1126 :help
"Insert record at point into the BBDB database"]
1127 ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
1128 (and (eq major-mode
'eudc-mode
)
1129 (or (featurep 'bbdb
)
1130 (prog1 (locate-library "bbdb") (message ""))))
1131 :help
"Insert all the records returned by a directory query into BBDB"]
1133 ["Get Email" eudc-get-email
1134 :help
"Get the email field of NAME from the directory server"]
1135 ["Get Phone" eudc-get-phone
1136 :help
"Get the phone field of name from the directory server"]
1137 ["List Valid Attribute Names" eudc-get-attribute-list
1138 :help
"Return a list of valid attributes for the current server"]
1140 ,(cons "Customize" eudc-custom-generated-menu
)))
1143 (defconst eudc-server-menu
1145 ["Bookmark Current Server" eudc-bookmark-current-server
1146 :help
"Add current server to the EUDC `servers' hotlist"]
1147 ["Edit Server List" eudc-edit-hotlist
1148 :help
"Edit the hotlist of directory servers in a specialized buffer"]
1149 ["New Server" eudc-set-server
1150 :help
"Set the directory server to SERVER using PROTOCOL"]))
1154 (append '("Directory Search")
1161 (let* ((server (car servspec
))
1162 (protocol (cdr servspec
))
1163 (proto-name (symbol-name protocol
)))
1164 (setq command
(intern (concat "eudc-set-server-"
1168 (if (not (fboundp command
))
1172 (eudc-set-server ,server
(quote ,protocol
))
1173 (message "Selected directory server is now %s (%s)"
1176 (vector (format "%s (%s)" server proto-name
)
1179 :selected
`(equal eudc-server
,server
)))))
1180 eudc-server-hotlist
)
1184 (defun eudc-install-menu ()
1186 ((and (featurep 'xemacs
) (featurep 'menubar
))
1187 (add-submenu '("Tools") (eudc-menu)))
1188 ((not (featurep 'xemacs
))
1190 ((fboundp 'easy-menu-create-menu
)
1193 [menu-bar tools directory-search
]
1194 (cons "Directory Search"
1195 (easy-menu-create-menu "Directory Search" (cdr (eudc-menu))))))
1196 ((fboundp 'easy-menu-add-item
)
1197 (let ((menu (eudc-menu)))
1198 (easy-menu-add-item nil
'("tools") (easy-menu-create-menu (car menu
)
1200 ((fboundp 'easy-menu-create-keymaps
)
1201 (easy-menu-define eudc-menu-map eudc-mode-map
"Directory Client Menu" (eudc-menu))
1204 [menu-bar tools eudc
]
1205 (cons "Directory Search"
1206 (easy-menu-create-keymaps "Directory Search" (cdr (eudc-menu))))))
1208 (error "Unknown version of easymenu"))))
1212 ;;; Load time initializations :
1214 ;;; Load the options file
1215 (if (and (not noninteractive
)
1216 (and (locate-library eudc-options-file
)
1217 (progn (message "") t
)) ; Remove modeline message
1218 (not (featurep 'eudc-options-file
)))
1219 (load eudc-options-file
))
1221 ;;; Install the full menu
1222 (unless (featurep 'infodock
)
1223 (eudc-install-menu))
1226 ;;; The following installs a short menu for EUDC at XEmacs startup.
1229 (defun eudc-load-eudc ()
1230 "Load the Emacs Unified Directory Client.
1231 This does nothing except loading eudc by autoload side-effect."
1237 ((not (featurep 'xemacs
))
1238 (defvar eudc-tools-menu
1239 (let ((map (make-sparse-keymap "Directory Search")))
1240 (define-key map
[phone]
1241 `(menu-item ,(purecopy "Get Phone") eudc-get-phone
1242 :help ,(purecopy "Get the phone field of name from the directory server")))
1243 (define-key map [email]
1244 `(menu-item ,(purecopy "Get Email") eudc-get-email
1245 :help ,(purecopy "Get the email field of NAME from the directory server")))
1246 (define-key map [separator-eudc-email] menu-bar-separator)
1247 (define-key map [expand-inline]
1248 `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
1249 :help ,(purecopy "Query the directory server, and expand the query string before point")))
1250 (define-key map [query]
1251 `(menu-item ,(purecopy "Query with Form") eudc-query-form
1252 :help ,(purecopy "Display a form to query the directory server")))
1253 (define-key map [separator-eudc-query] menu-bar-separator)
1254 (define-key map [new]
1255 `(menu-item ,(purecopy "New Server") eudc-set-server
1256 :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
1257 (define-key map [load]
1258 `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
1259 :help ,(purecopy "Load the Emacs Unified Directory Client")))
1261 (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
1263 (let ((menu '("Directory Search"
1264 ["Load Hotlist of Servers" eudc-load-eudc t]
1265 ["New Server" eudc-set-server t]
1267 ["Query with Form" eudc-query-form t]
1268 ["Expand Inline Query" eudc-expand-inline t]
1270 ["Get Email" eudc-get-email t]
1271 ["Get Phone" eudc-get-phone t])))
1272 (if (not (featurep 'eudc-autoloads))
1273 (if (featurep 'xemacs)
1274 (if (and (featurep 'menubar)
1275 (not (featurep 'infodock)))
1276 (add-submenu '("Tools") menu))
1279 ((fboundp 'easy-menu-add-item)
1280 (easy-menu-add-item nil '("tools")
1281 (easy-menu-create-menu (car menu)
1283 ((fboundp 'easy-menu-create-keymaps)
1286 [menu-bar tools eudc]
1287 (cons "Directory Search"
1288 (easy-menu-create-keymaps "Directory Search"
1289 (cdr menu)))))))))))
1295 ;;; eudc.el ends here