1 ;;; eudc.el --- Emacs Unified Directory Client -*- coding: utf-8 -*-
3 ;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
5 ;; Author: Oscar Figueiredo <oscar@cpe.fr>
6 ;; Maintainer: Pavel JanÃk <Pavel@Janik.cz>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
34 ;; EUDC comes with an extensive documentation, please refer to it.
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
39 ;; of the name before cursor and insert it in the
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
50 (if (not (fboundp 'make-overlay
))
53 (unless (fboundp 'custom-menu-create
)
54 (autoload 'custom-menu-create
"cus-edit"))
60 ;;{{{ Internal cooking
62 ;;{{{ Internal variables and compatibility tricks
64 (defvar eudc-form-widget-list nil
)
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
)
75 (set-keymap-parent eudc-mode-map widget-keymap
)
77 (defvar mode-popup-menu
)
79 ;; List of known servers
80 ;; Alist of (SERVER . PROTOCOL)
81 (defvar eudc-server-hotlist nil
)
83 ;; List of variables that have server- or protocol-local bindings
84 (defvar eudc-local-vars nil
)
86 ;; Protocol local. Query function
87 (defvar eudc-query-function nil
)
89 ;; Protocol local. A function that retrieves a list of valid attribute names
90 (defvar eudc-list-attributes-function nil
)
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
)
97 ;; Protocol local. Mapping between protocol attribute names and BBDB field
99 (defvar eudc-bbdb-conversion-alist nil
)
101 ;; Protocol/Server local. Hook called upon switching to that server
102 (defvar eudc-switch-to-server-hook nil
)
104 ;; Protocol/Server local. Hook called upon switching from that server
105 (defvar eudc-switch-from-server-hook nil
)
107 ;; Protocol local. Whether the protocol supports queries with no specified
109 (defvar eudc-protocol-has-default-query-attributes nil
)
111 (defun eudc-cadr (obj)
114 (defun eudc-cdar (obj)
117 (defun eudc-caar (obj)
120 (defun eudc-cdaar (obj)
121 (cdr (car (car obj
))))
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"))
129 (if (eq prop
(car plist
))
131 (setq plist
(cdr (cdr plist
))))
134 ;; Emacs's plist-get lacks third parameter
135 (defun eudc-plist-get (plist prop
&optional default
)
136 "Extract a value from a property list.
137 PLIST is a property list, which is a list of the form
138 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
139 corresponding to the given PROP, or DEFAULT if PROP is not
140 one of the properties on the list."
141 (if (eudc-plist-member plist prop
)
142 (plist-get plist prop
)
145 (defun eudc-lax-plist-get (plist prop
&optional default
)
146 "Extract a value from a lax property list.
148 PLIST is a lax property list, which is a list of the form (PROP1
149 VALUE1 PROP2 VALUE2...), where comparisons between properties are done
150 using `equal' instead of `eq'. This function returns the value
151 corresponding to PROP, or DEFAULT if PROP is not one of the
152 properties on the list."
153 (if (not (= 0 (%
(length plist
) 2)))
154 (error "Malformed plist"))
157 (if (equal prop
(car plist
))
158 (throw 'found
(car (cdr plist
))))
159 (setq plist
(cdr (cdr plist
))))
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.
165 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
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
)
181 (defun eudc-replace-in-string (str regexp newtext
)
182 "Replace all matches in STR for REGEXP with NEWTEXT.
183 Value is the new string."
187 (while (setq match
(string-match regexp str start
))
188 (setq prev-start start
192 (substring str prev-start match
)
194 (concat rtn-str
(substring str start
))))
198 ;;{{{ Server and Protocol Variable Routines
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
))
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
))
208 (defun eudc-default-set (var val
)
209 "Set the EUDC default value of VAR to VAL.
210 The current binding of VAR is not changed."
211 (put var
'eudc-locals
212 (plist-put (get var
'eudc-locals
) 'default val
))
213 (add-to-list 'eudc-local-vars var
))
215 (defun eudc-protocol-set (var val
&optional protocol
)
216 "Set the PROTOCOL-local binding of VAR to VAL.
217 If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
218 The 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
226 (plist-put eudc-locals
'protocol protocol-locals
))
227 (put var
'eudc-locals eudc-locals
)
228 (add-to-list 'eudc-local-vars var
)
230 (eudc-update-variable var
))))
232 (defun eudc-server-set (var val
&optional server
)
233 "Set the SERVER-local binding of VAR to VAL.
234 If omitted SERVER defaults to the current value of `eudc-server'.
235 The 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
243 (plist-put eudc-locals
'server server-locals
))
244 (put var
'eudc-locals eudc-locals
)
245 (add-to-list 'eudc-local-vars var
)
247 (eudc-update-variable var
))))
250 (defun eudc-set (var val
)
251 "Set the most local (server, protocol or default) binding of VAR to VAL.
252 The current binding of VAR is also set to VAL"
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
))
259 (eudc-default-set var val
)))
262 (defun eudc-variable-default-value (var)
263 "Return the default binding of VAR.
264 Return `unbound' if VAR has no EUDC default value."
265 (let ((eudc-locals (get var
'eudc-locals
)))
266 (if (and (boundp var
)
268 (eudc-plist-get eudc-locals
'default
'unbound
)
271 (defun eudc-variable-protocol-value (var &optional protocol
)
272 "Return the value of VAR local to PROTOCOL.
273 Return `unbound' if VAR has no value local to PROTOCOL.
274 PROTOCOL defaults to `eudc-protocol'"
275 (let* ((eudc-locals (get var
'eudc-locals
))
277 (if (not (and (boundp var
)
279 (eudc-plist-member eudc-locals
'protocol
)))
281 (setq protocol-locals
(eudc-plist-get eudc-locals
'protocol
))
282 (eudc-lax-plist-get protocol-locals
284 eudc-protocol
) 'unbound
))))
286 (defun eudc-variable-server-value (var &optional server
)
287 "Return the value of VAR local to SERVER.
288 Return `unbound' if VAR has no value local to SERVER.
289 SERVER defaults to `eudc-server'"
290 (let* ((eudc-locals (get var
'eudc-locals
))
292 (if (not (and (boundp var
)
294 (eudc-plist-member eudc-locals
'server
)))
296 (setq server-locals
(eudc-plist-get eudc-locals
'server
))
297 (eudc-lax-plist-get server-locals
299 eudc-server
) 'unbound
))))
301 (defun eudc-update-variable (var)
302 "Set the value of VAR according to its locals.
303 If the VAR has a server- or protocol-local value corresponding
304 to the current `eudc-server' and `eudc-protocol' then it is set
305 accordingly. Otherwise it is set to its EUDC default binding"
308 ((not (eq 'unbound
(setq val
(eudc-variable-server-value var
))))
310 ((not (eq 'unbound
(setq val
(eudc-variable-protocol-value var
))))
312 ((not (eq 'unbound
(setq val
(eudc-variable-default-value var
))))
315 (defun eudc-update-local-variables ()
316 "Update all EUDC variables according to their local settings."
318 (mapcar 'eudc-update-variable eudc-local-vars
))
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
)
332 ;; Add PROTOCOL to the list of supported protocols
333 (defun eudc-register-protocol (protocol)
334 (unless (memq protocol eudc-supported-protocols
)
335 (setq eudc-supported-protocols
336 (cons protocol eudc-supported-protocols
))
337 (put 'eudc-protocol
'custom-type
338 `(choice :menu-tag
"Protocol"
339 ,@(mapcar (lambda (s)
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
))))
347 (defun eudc-translate-query (query)
348 "Translate attribute names of QUERY.
349 The translation is done according to
350 `eudc-protocol-attributes-translation-alist'."
351 (if eudc-protocol-attributes-translation-alist
352 (mapcar (lambda (attribute)
353 (let ((trans (assq (car attribute
)
354 (symbol-value eudc-protocol-attributes-translation-alist
))))
356 (cons (cdr trans
) (cdr attribute
))
361 (defun eudc-translate-attribute-list (list)
362 "Translate a list of attribute names LIST.
363 The translation is done according to
364 `eudc-protocol-attributes-translation-alist'."
365 (if eudc-protocol-attributes-translation-alist
367 (mapcar (lambda (attribute)
368 (setq trans
(assq attribute
369 (symbol-value eudc-protocol-attributes-translation-alist
)))
376 (defun eudc-select (choices beg end
)
377 "Choose one from CHOICES using a completion.
378 BEG and END delimit the text which is to be replaced."
381 (completing-read "Multiple matches found; choose one: "
382 (mapcar 'list choices
)))
383 (delete-region beg end
)
384 (insert replacement
)))
386 (defun eudc-query (query &optional return-attributes no-translation
)
387 "Query the current directory server with QUERY.
388 QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
389 name and VALUE the corresponding value.
390 If NO-TRANSLATION is non-nil, ATTR is translated according to
391 `eudc-protocol-attributes-translation-alist'.
392 RETURN-ATTRIBUTES is a list of attributes to return defaulting to
393 `eudc-default-return-attributes'."
394 (unless eudc-query-function
395 (error "Don't know how to perform the query"))
397 (funcall eudc-query-function query
(or return-attributes
398 eudc-default-return-attributes
))
400 (funcall eudc-query-function
401 (eudc-translate-query query
)
404 (eudc-translate-attribute-list return-attributes
))
405 ((listp eudc-default-return-attributes
)
406 (eudc-translate-attribute-list eudc-default-return-attributes
))
408 eudc-default-return-attributes
)))))
410 (defun eudc-format-attribute-name-for-display (attribute)
411 "Format a directory attribute name for display.
412 ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
413 by the corresponding user name if any. Otherwise it is capitalized and
414 underscore characters are replaced by spaces."
415 (let ((match (assq attribute eudc-user-attribute-names-alist
)))
420 (split-string (symbol-name attribute
) "_")
423 (defun eudc-print-attribute-value (field)
424 "Insert the value of the directory FIELD at point.
425 The directory attribute name in car of FIELD is looked up in
426 `eudc-attribute-display-method-alist' and the corresponding method,
427 if 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))
434 (eval (list (cdr match
) val
))
440 (insert val-elem
"\n")))
443 ((stringp val
) (split-string val
"\n"))
447 (defun eudc-print-record-field (field column-width
)
448 "Print the record field FIELD.
449 FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
450 COLUMN-WIDTH is the width of the first display column containing the
451 attribute 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
456 (insert (format (concat "%" (int-to-string column-width
) "s: ")
458 (put-text-property field-beg
(point) 'face
'bold
)
459 (indent-to (+ 2 column-width
))
460 (eudc-print-attribute-value field
)))
462 (defun eudc-display-records (records &optional raw-attr-names
)
463 "Display the record list RECORDS in a formatted buffer.
464 If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
465 otherwise they are formatted according to `eudc-user-attribute-names-alist'."
466 (let (inhibit-read-only
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
)
477 (insert "Directory Query Result\n")
478 (insert "======================\n\n\n")
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"
484 ;; Replace field names with user names, compute max width
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
))))
501 ;; Display the records
502 (setq first-record
(point))
507 ;; Map over the record fields to print the attribute/value pairs
510 (eudc-print-record-field field width
)))
512 ;; Store the record internal format in some convenient place
513 (overlay-put (make-overlay beg
(point))
516 (setq records
(cdr records
))
520 (widget-create 'push-button
521 :notify
(lambda (&rest _ignore
)
525 (widget-create 'push-button
526 :notify
(lambda (&rest _ignore
)
532 (goto-char first-record
))))))
534 (defun eudc-process-form ()
535 "Process the query form in current buffer and display the results."
538 (if (not (and (boundp 'eudc-form-widget-list
)
539 eudc-form-widget-list
))
540 (error "Not in a directory query form buffer")
543 (setq value
(widget-value (cdr wid-field
)))
544 (if (not (string= value
""))
545 (setq query-alist
(cons (cons (car wid-field
) value
)
547 eudc-form-widget-list
)
548 (kill-buffer (current-buffer))
549 (eudc-display-records (eudc-query query-alist
) eudc-use-raw-directory-names
))))
552 (defun eudc-filter-duplicate-attributes (record)
553 "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
559 ;; Search for multiple records
561 (not (listp (eudc-cdar rec
))))
562 (setq rec
(cdr rec
)))
564 (if (null (eudc-cdar rec
))
565 (list record
) ; No duplicate attrs in this record
568 (if (listp (cdr field
))
569 (setq duplicates
(cons field duplicates
))
570 (setq unique
(cons field unique
)))))
572 (setq result
(list unique
))
573 ;; Map over the record fields that have multiple values
577 (let ((method (if (consp eudc-duplicate-attribute-handling-method
)
585 eudc-protocol-attributes-translation-alist
)))
587 eudc-duplicate-attribute-handling-method
))
588 eudc-duplicate-attribute-handling-method
)))
590 ((or (null method
) (eq 'list method
))
592 (eudc-add-field-to-records field result
)))
595 (eudc-add-field-to-records (cons (car field
)
600 (eudc-add-field-to-records (cons (car field
)
605 ((eq 'duplicate method
)
607 (eudc-distribute-field-on-records field result
)))))))
611 (defun eudc-filter-partial-records (records attrs
)
612 "Eliminate records that do not contain all ATTRS from RECORDS."
621 (consp (assq attr rec
))))
626 (defun eudc-add-field-to-records (field records
)
627 "Add FIELD to each individual record in RECORDS and return the resulting list."
633 (defun eudc-distribute-field-on-records (field records
)
634 "Duplicate each individual record in RECORDS according to value of FIELD.
635 Each copy is added a new field containing one of the values of FIELD."
637 (values (cdr field
)))
638 ;; Uniquify values first
640 (setcdr values
(delete (car values
) (cdr values
)))
641 (setq values
(cdr values
)))
645 (let ((result-list (copy-sequence records
)))
646 (setq result-list
(eudc-add-field-to-records
647 (cons (car field
) value
)
649 (setq result
(append result-list result
))
655 (define-derived-mode eudc-mode special-mode
"EUDC"
656 "Major mode used in buffers displaying the results of directory queries.
657 There is no sense in calling this command from a buffer other than
658 one containing the results of a directory query.
660 These 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 (if (not (featurep 'xemacs
))
667 (easy-menu-define eudc-emacs-menu eudc-mode-map
"" (eudc-menu))
668 (setq mode-popup-menu
(eudc-menu))))
672 ;;{{{ High-level interfaces (interactive functions)
674 (defun eudc-customize ()
675 "Customize the EUDC package."
677 (customize-group 'eudc
))
680 (defun eudc-set-server (server protocol
&optional no-save
)
681 "Set the directory server to SERVER using PROTOCOL.
682 Unless NO-SAVE is non-nil, the server is saved as the default
683 server for future sessions."
685 (read-from-minibuffer "Directory Server: ")
686 (intern (completing-read "Protocol: "
687 (mapcar (lambda (elt)
688 (cons (symbol-name elt
)
690 eudc-known-protocols
)))))
691 (unless (or (member protocol
692 eudc-supported-protocols
)
693 (load (concat "eudcb-" (symbol-name protocol
)) t
))
694 (error "Unsupported protocol: %s" protocol
))
695 (run-hooks 'eudc-switch-from-server-hook
)
696 (setq eudc-protocol protocol
)
697 (setq eudc-server server
)
698 (eudc-update-local-variables)
699 (run-hooks 'eudc-switch-to-server-hook
)
700 (if (called-interactively-p 'interactive
)
701 (message "Current directory server is now %s (%s)" eudc-server eudc-protocol
))
703 (eudc-save-options)))
706 (defun eudc-get-email (name &optional error
)
707 "Get the email field of NAME from the directory server.
708 If ERROR is non-nil, report an error if there is none."
709 (interactive "sName: \np")
711 (call-interactively 'eudc-set-server
))
712 (let ((result (eudc-query (list (cons 'name name
)) '(email)))
714 (if (null (cdr result
))
715 (setq email
(eudc-cdaar result
))
716 (error "Multiple match--use the query form"))
720 (error "No record matching %s" name
)))
724 (defun eudc-get-phone (name &optional error
)
725 "Get the phone field of NAME from the directory server.
726 If ERROR is non-nil, report an error if there is none."
727 (interactive "sName: \np")
729 (call-interactively 'eudc-set-server
))
730 (let ((result (eudc-query (list (cons 'name name
)) '(phone)))
732 (if (null (cdr result
))
733 (setq phone
(eudc-cdaar result
))
734 (error "Multiple match--use the query form"))
738 (error "No record matching %s" name
)))
741 (defun eudc-get-attribute-list ()
742 "Return a list of valid attributes for the current server.
743 When called interactively the list is formatted in a dedicated buffer
744 otherwise a list of symbols is returned."
746 (if eudc-list-attributes-function
747 (let ((entries (funcall eudc-list-attributes-function
748 (called-interactively-p 'interactive
))))
750 (if (called-interactively-p 'interactive
)
751 (eudc-display-records entries t
)
753 (error "The %s protocol has no support for listing attributes" eudc-protocol
)))
755 (defun eudc-format-query (words format
)
756 "Use FORMAT to build a EUDC query from WORDS."
762 (while (and words format
)
763 (setq query-alist
(cons (cons (car format
) (car words
))
765 (setq words
(cdr words
)
766 format
(cdr format
)))
767 ;; If the same attribute appears more than once, merge
768 ;; the corresponding values
769 (setq query-alist
(nreverse query-alist
))
771 (setq key
(eudc-caar query-alist
)
772 val
(eudc-cdar query-alist
)
773 cell
(assq key query
))
775 (setcdr cell
(concat (cdr cell
) " " val
))
776 (setq query
(cons (car query-alist
) query
)))
777 (setq query-alist
(cdr query-alist
)))
779 (if eudc-protocol-has-default-query-attributes
780 (mapconcat 'identity words
" ")
781 (list (cons 'name
(mapconcat 'identity words
" ")))))))
783 (defun eudc-extract-n-word-formats (format-list n
)
784 "Extract a list of N-long formats from FORMAT-LIST.
785 If none try N - 1 and so forth."
787 (while (and (null formats
)
791 (mapcar (lambda (format)
802 (defun eudc-expand-inline (&optional replace
)
803 "Query the directory server, and expand the query string before point.
804 The query string consists of the buffer substring from the point back to
805 the preceding comma, colon or beginning of line.
806 The variable `eudc-inline-query-format' controls how to associate the
807 individual inline query words with directory attribute names.
808 After querying the server for the given string, the expansion specified by
809 `eudc-inline-expansion-format' is inserted in the buffer at point.
810 If REPLACE is non-nil, then this expansion replaces the name in the buffer.
811 `eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
812 Multiple servers can be tried with the same query until one finds a match,
813 see `eudc-inline-expansion-servers'"
815 (if (memq eudc-inline-expansion-servers
816 '(current-server server-then-hotlist
))
818 (call-interactively 'eudc-set-server
))
819 (or eudc-server-hotlist
820 (error "No server in the hotlist")))
823 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
824 (point-at-bol) 'move
)
825 (goto-char (match-end 0)))
827 (query-words (split-string (buffer-substring beg end
) "[ \t]+"))
832 (eudc-former-server eudc-server
)
833 (eudc-former-protocol eudc-protocol
)
836 ;; Prepare the list of servers to query
837 (setq servers
(copy-sequence eudc-server-hotlist
))
840 ((eq eudc-inline-expansion-servers
'hotlist
)
842 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
843 (cons (cons eudc-server eudc-protocol
)
844 (delete (cons eudc-server eudc-protocol
) servers
)))
845 ((eq eudc-inline-expansion-servers
'current-server
)
846 (list (cons eudc-server eudc-protocol
)))
848 (error "Wrong value for `eudc-inline-expansion-servers': %S"
849 eudc-inline-expansion-servers
))))
850 (if (and eudc-max-servers-to-query
851 (> (length servers
) eudc-max-servers-to-query
))
852 (setcdr (nthcdr (1- eudc-max-servers-to-query
) servers
) nil
))
854 (condition-case signal
858 ;; Loop on the servers
860 (eudc-set-server (eudc-caar servers
) (eudc-cdar servers
) t
)
862 ;; Determine which formats apply in the query-format list
865 (eudc-extract-n-word-formats eudc-inline-query-format
866 (length query-words
))
867 (if (null eudc-protocol-has-default-query-attributes
)
870 ;; Loop on query-formats
874 (eudc-format-query query-words
(car query-formats
))
875 (eudc-translate-attribute-list
876 (cdr eudc-inline-expansion-format
))))
878 (throw 'found response
))
879 (setq query-formats
(cdr query-formats
)))
880 (setq servers
(cdr servers
)))
881 ;; No more servers to try... no match found
888 ;; Process response through eudc-inline-expansion-format
890 (setq response-string
(apply 'format
891 (car eudc-inline-expansion-format
)
894 (or (cdr (assq field
(car response
)))
896 (eudc-translate-attribute-list
897 (cdr eudc-inline-expansion-format
)))))
898 (if (> (length response-string
) 0)
899 (setq response-strings
900 (cons response-string response-strings
)))
901 (setq response
(cdr response
)))
904 (and replace
(not eudc-expansion-overwrites-query
))
905 (and (not replace
) eudc-expansion-overwrites-query
))
906 (kill-ring-save beg end
))
908 ((or (= (length response-strings
) 1)
909 (null eudc-multiple-match-handling-method
)
910 (eq eudc-multiple-match-handling-method
'first
))
911 (delete-region beg end
)
912 (insert (car response-strings
)))
913 ((eq eudc-multiple-match-handling-method
'select
)
914 (eudc-select response-strings beg end
))
915 ((eq eudc-multiple-match-handling-method
'all
)
916 (delete-region beg end
)
917 (insert (mapconcat 'identity response-strings
", ")))
918 ((eq eudc-multiple-match-handling-method
'abort
)
919 (error "There is more than one match for the query"))))
920 (or (and (equal eudc-server eudc-former-server
)
921 (equal eudc-protocol eudc-former-protocol
))
922 (eudc-set-server eudc-former-server eudc-former-protocol t
)))
924 (or (and (equal eudc-server eudc-former-server
)
925 (equal eudc-protocol eudc-former-protocol
))
926 (eudc-set-server eudc-former-server eudc-former-protocol t
))
927 (signal (car signal
) (cdr signal
))))))
930 (defun eudc-query-form (&optional get-fields-from-server
)
931 "Display a form to query the directory server.
932 If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
933 queries the server for the existing fields and displays a corresponding form."
935 (let ((fields (or (and get-fields-from-server
936 (eudc-get-attribute-list))
937 eudc-query-form-attributes
))
938 (buffer (get-buffer-create "*Directory Query Form*"))
944 (switch-to-buffer buffer
)
945 (setq inhibit-read-only t
)
947 (kill-all-local-variables)
948 (make-local-variable 'eudc-form-widget-list
)
949 (widget-insert "Directory Query Form\n")
950 (widget-insert "====================\n\n")
951 (widget-insert "Current server is: " (or eudc-server
953 (call-interactively 'eudc-set-server
)
956 (widget-insert "Protocol : " (symbol-name eudc-protocol
) "\n")
957 ;; Build the list of prompts
958 (setq prompts
(if eudc-use-raw-directory-names
959 (mapcar 'symbol-name
(eudc-translate-attribute-list fields
))
962 (or (and (assq field eudc-user-attribute-names-alist
)
963 (cdr (assq field eudc-user-attribute-names-alist
)))
964 (capitalize (symbol-name field
)))))
966 ;; Loop over prompt strings to find the longest one
969 (if (> (length prompt
) width
)
970 (setq width
(length prompt
)))))
972 ;; Insert the first widget out of the mapcar to leave the cursor
973 ;; in the first field
974 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
976 (setq widget
(widget-create 'editable-field
:size
15))
977 (setq eudc-form-widget-list
(cons (cons (car fields
) widget
)
978 eudc-form-widget-list
))
979 (setq fields
(cdr fields
))
980 (setq prompts
(cdr prompts
))
983 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
984 (setq widget
(widget-create 'editable-field
986 (setq eudc-form-widget-list
(cons (cons field widget
)
987 eudc-form-widget-list
))
988 (setq prompts
(cdr prompts
))))
990 (widget-insert "\n\n")
991 (widget-create 'push-button
992 :notify
(lambda (&rest _ignore
)
996 (widget-create 'push-button
997 :notify
(lambda (&rest _ignore
)
1001 (widget-create 'push-button
1002 :notify
(lambda (&rest _ignore
)
1006 (use-local-map widget-keymap
)
1010 (defun eudc-bookmark-server (server protocol
)
1011 "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
1012 (interactive "sDirectory server: \nsProtocol: ")
1013 (if (member (cons server protocol
) eudc-server-hotlist
)
1014 (error "%s:%s is already in the hotlist" protocol server
)
1015 (setq eudc-server-hotlist
(cons (cons server protocol
) eudc-server-hotlist
))
1017 (eudc-save-options)))
1019 (defun eudc-bookmark-current-server ()
1020 "Add current server to the EUDC `servers' hotlist."
1022 (eudc-bookmark-server eudc-server eudc-protocol
))
1024 (defun eudc-save-options ()
1025 "Save options to `eudc-options-file'."
1027 (with-current-buffer (find-file-noselect eudc-options-file t
)
1028 (goto-char (point-min))
1029 ;; delete the previous setq
1030 (let ((standard-output (current-buffer))
1036 (let ((sexp (condition-case nil
1037 (read (current-buffer))
1038 (end-of-file (throw 'found nil
)))))
1041 ((eq (car sexp
) 'eudc-set-server
)
1042 (delete-region (save-excursion
1046 (setq set-server-p t
))
1047 ((and (eq (car sexp
) 'setq
)
1048 (eq (eudc-cadr sexp
) 'eudc-server-hotlist
))
1049 (delete-region (save-excursion
1053 (setq set-hotlist-p t
))
1054 ((and (eq (car sexp
) 'provide
)
1055 (equal (eudc-cadr sexp
) '(quote eudc-options-file
)))
1056 (setq provide-p t
)))
1060 (throw 'found t
))))))
1061 (if (eq (point-min) (point-max))
1062 (princ ";; This file was automatically generated by eudc.el.\n\n"))
1064 (princ "(provide 'eudc-options-file)\n"))
1067 (delete-blank-lines)
1068 (princ "(eudc-set-server ")
1071 (prin1 eudc-protocol
)
1073 (princ "(setq eudc-server-hotlist '")
1074 (prin1 eudc-server-hotlist
)
1078 (defun eudc-move-to-next-record ()
1079 "Move to next record, in a buffer displaying directory query results."
1081 (if (not (derived-mode-p 'eudc-mode
))
1082 (error "Not in a EUDC buffer")
1083 (let ((pt (next-overlay-change (point))))
1084 (if (< pt
(point-max))
1086 (error "No more records after point")))))
1088 (defun eudc-move-to-previous-record ()
1089 "Move to previous record, in a buffer displaying directory query results."
1091 (if (not (derived-mode-p 'eudc-mode
))
1092 (error "Not in a EUDC buffer")
1093 (let ((pt (previous-overlay-change (point))))
1094 (if (> pt
(point-min))
1096 (error "No more records before point")))))
1100 ;;{{{ Menus and keymaps
1104 (defconst eudc-custom-generated-menu
(cdr (custom-menu-create 'eudc
)))
1106 (defconst eudc-tail-menu
1108 ["Query with Form" eudc-query-form
1109 :help
"Display a form to query the directory server"]
1110 ["Expand Inline Query" eudc-expand-inline
1111 :help
"Query the directory server, and expand the query string before point"]
1112 ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
1113 (and (or (featurep 'bbdb
)
1114 (prog1 (locate-library "bbdb") (message "")))
1115 (overlays-at (point))
1116 (overlay-get (car (overlays-at (point))) 'eudc-record
))
1117 :help
"Insert record at point into the BBDB database"]
1118 ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
1119 (and (derived-mode-p 'eudc-mode
)
1120 (or (featurep 'bbdb
)
1121 (prog1 (locate-library "bbdb") (message ""))))
1122 :help
"Insert all the records returned by a directory query into BBDB"]
1124 ["Get Email" eudc-get-email
1125 :help
"Get the email field of NAME from the directory server"]
1126 ["Get Phone" eudc-get-phone
1127 :help
"Get the phone field of name from the directory server"]
1128 ["List Valid Attribute Names" eudc-get-attribute-list
1129 :help
"Return a list of valid attributes for the current server"]
1131 ,(cons "Customize" eudc-custom-generated-menu
)))
1134 (defconst eudc-server-menu
1136 ["Bookmark Current Server" eudc-bookmark-current-server
1137 :help
"Add current server to the EUDC `servers' hotlist"]
1138 ["Edit Server List" eudc-edit-hotlist
1139 :help
"Edit the hotlist of directory servers in a specialized buffer"]
1140 ["New Server" eudc-set-server
1141 :help
"Set the directory server to SERVER using PROTOCOL"]))
1145 (append '("Directory Search")
1152 (let* ((server (car servspec
))
1153 (protocol (cdr servspec
))
1154 (proto-name (symbol-name protocol
)))
1155 (setq command
(intern (concat "eudc-set-server-"
1159 (if (not (fboundp command
))
1163 (eudc-set-server ,server
(quote ,protocol
))
1164 (message "Selected directory server is now %s (%s)"
1167 (vector (format "%s (%s)" server proto-name
)
1170 :selected
`(equal eudc-server
,server
)))))
1171 eudc-server-hotlist
)
1175 (defun eudc-install-menu ()
1177 ((and (featurep 'xemacs
) (featurep 'menubar
))
1178 (add-submenu '("Tools") (eudc-menu)))
1179 ((not (featurep 'xemacs
))
1181 ((fboundp 'easy-menu-create-menu
)
1184 [menu-bar tools directory-search
]
1185 (cons "Directory Search"
1186 (easy-menu-create-menu "Directory Search" (cdr (eudc-menu))))))
1187 ((fboundp 'easy-menu-add-item
)
1188 (let ((menu (eudc-menu)))
1189 (easy-menu-add-item nil
'("tools") (easy-menu-create-menu (car menu
)
1191 ((fboundp 'easy-menu-create-keymaps
)
1192 (easy-menu-define eudc-menu-map eudc-mode-map
"Directory Client Menu" (eudc-menu))
1195 [menu-bar tools eudc
]
1196 (cons "Directory Search"
1197 (easy-menu-create-keymaps "Directory Search" (cdr (eudc-menu))))))
1199 (error "Unknown version of easymenu"))))
1203 ;;; Load time initializations :
1205 ;;; Load the options file
1206 (if (and (not noninteractive
)
1207 (and (locate-library eudc-options-file
)
1208 (progn (message "") t
)) ; Remove mode line message
1209 (not (featurep 'eudc-options-file
)))
1210 (load eudc-options-file
))
1212 ;;; Install the full menu
1213 (unless (featurep 'infodock
)
1214 (eudc-install-menu))
1217 ;;; The following installs a short menu for EUDC at XEmacs startup.
1220 (defun eudc-load-eudc ()
1221 "Load the Emacs Unified Directory Client.
1222 This does nothing except loading eudc by autoload side-effect."
1228 ((not (featurep 'xemacs
))
1229 (defvar eudc-tools-menu
1230 (let ((map (make-sparse-keymap "Directory Search")))
1231 (define-key map
[phone]
1232 `(menu-item ,(purecopy "Get Phone") eudc-get-phone
1233 :help ,(purecopy "Get the phone field of name from the directory server")))
1234 (define-key map [email]
1235 `(menu-item ,(purecopy "Get Email") eudc-get-email
1236 :help ,(purecopy "Get the email field of NAME from the directory server")))
1237 (define-key map [separator-eudc-email] menu-bar-separator)
1238 (define-key map [expand-inline]
1239 `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
1240 :help ,(purecopy "Query the directory server, and expand the query string before point")))
1241 (define-key map [query]
1242 `(menu-item ,(purecopy "Query with Form") eudc-query-form
1243 :help ,(purecopy "Display a form to query the directory server")))
1244 (define-key map [separator-eudc-query] menu-bar-separator)
1245 (define-key map [new]
1246 `(menu-item ,(purecopy "New Server") eudc-set-server
1247 :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
1248 (define-key map [load]
1249 `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
1250 :help ,(purecopy "Load the Emacs Unified Directory Client")))
1252 (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
1254 (let ((menu '("Directory Search"
1255 ["Load Hotlist of Servers" eudc-load-eudc t]
1256 ["New Server" eudc-set-server t]
1258 ["Query with Form" eudc-query-form t]
1259 ["Expand Inline Query" eudc-expand-inline t]
1261 ["Get Email" eudc-get-email t]
1262 ["Get Phone" eudc-get-phone t])))
1263 (if (not (featurep 'eudc-autoloads))
1264 (if (featurep 'xemacs)
1265 (if (and (featurep 'menubar)
1266 (not (featurep 'infodock)))
1267 (add-submenu '("Tools") menu))
1270 ((fboundp 'easy-menu-add-item)
1271 (easy-menu-add-item nil '("tools")
1272 (easy-menu-create-menu (car menu)
1274 ((fboundp 'easy-menu-create-keymaps)
1277 [menu-bar tools eudc]
1278 (cons "Directory Search"
1279 (easy-menu-create-keymaps "Directory Search"
1280 (cdr menu)))))))))))
1286 ;;; eudc.el ends here