1 ;;; eudc.el --- Emacs Unified Directory Client -*- coding: utf-8 -*-
3 ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
5 ;; Author: Oscar Figueiredo <oscar@cpe.fr>
6 ;; Pavel JanÃk <Pavel@Janik.cz>
7 ;; Maintainer: Thomas Fitzsimmons <fitzsim@fitzsim.org>
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
))
54 (unless (fboundp 'custom-menu-create
)
55 (autoload 'custom-menu-create
"cus-edit"))
61 ;;{{{ Internal cooking
63 ;;{{{ Internal variables and compatibility tricks
65 (defvar eudc-form-widget-list nil
)
68 (let ((map (make-sparse-keymap)))
69 (define-key map
"q" 'kill-this-buffer
)
70 (define-key map
"x" 'kill-this-buffer
)
71 (define-key map
"f" 'eudc-query-form
)
72 (define-key map
"b" 'eudc-try-bbdb-insert
)
73 (define-key map
"n" 'eudc-move-to-next-record
)
74 (define-key map
"p" 'eudc-move-to-previous-record
)
76 (set-keymap-parent eudc-mode-map widget-keymap
)
78 (defvar mode-popup-menu
)
80 ;; List of variables that have server- or protocol-local bindings
81 (defvar eudc-local-vars nil
)
83 ;; Protocol local. Query function
84 (defvar eudc-query-function nil
)
86 ;; Protocol local. A function that retrieves a list of valid attribute names
87 (defvar eudc-list-attributes-function nil
)
89 ;; Protocol local. A mapping between EUDC attribute names and corresponding
90 ;; protocol specific names. The following names are defined by EUDC and may be
91 ;; included in that list: `name' , `firstname', `email', `phone'
92 (defvar eudc-protocol-attributes-translation-alist nil
)
94 ;; Protocol local. Mapping between protocol attribute names and BBDB field
96 (defvar eudc-bbdb-conversion-alist nil
)
98 ;; Protocol/Server local. Hook called upon switching to that server
99 (defvar eudc-switch-to-server-hook nil
)
101 ;; Protocol/Server local. Hook called upon switching from that server
102 (defvar eudc-switch-from-server-hook nil
)
104 ;; Protocol local. Whether the protocol supports queries with no specified
106 (defvar eudc-protocol-has-default-query-attributes nil
)
108 (defun eudc-cadr (obj)
111 (defun eudc-cdar (obj)
114 (defun eudc-caar (obj)
117 (defun eudc-cdaar (obj)
118 (cdr (car (car obj
))))
120 (defun eudc-plist-member (plist prop
)
121 "Return t if PROP has a value specified in PLIST."
122 (if (not (= 0 (%
(length plist
) 2)))
123 (error "Malformed plist"))
126 (if (eq prop
(car plist
))
128 (setq plist
(cdr (cdr plist
))))
131 ;; Emacs's plist-get lacks third parameter
132 (defun eudc-plist-get (plist prop
&optional default
)
133 "Extract a value from a property list.
134 PLIST is a property list, which is a list of the form
135 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
136 corresponding to the given PROP, or DEFAULT if PROP is not
137 one of the properties on the list."
138 (if (eudc-plist-member plist prop
)
139 (plist-get plist prop
)
142 (defun eudc-lax-plist-get (plist prop
&optional default
)
143 "Extract a value from a lax property list.
145 PLIST is a lax property list, which is a list of the form (PROP1
146 VALUE1 PROP2 VALUE2...), where comparisons between properties are done
147 using `equal' instead of `eq'. This function returns the value
148 corresponding to PROP, or DEFAULT if PROP is not one of the
149 properties on the list."
150 (if (not (= 0 (%
(length plist
) 2)))
151 (error "Malformed plist"))
154 (if (equal prop
(car plist
))
155 (throw 'found
(car (cdr plist
))))
156 (setq plist
(cdr (cdr plist
))))
159 (if (not (fboundp 'split-string
))
160 (defun split-string (string &optional pattern
)
161 "Return a list of substrings of STRING which are separated by PATTERN.
162 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
164 (setq pattern
"[ \f\t\n\r\v]+"))
165 (let (parts (start 0))
166 (when (string-match pattern string
0)
167 (if (> (match-beginning 0) 0)
168 (setq parts
(cons (substring string
0 (match-beginning 0)) nil
)))
169 (setq start
(match-end 0))
170 (while (and (string-match pattern string start
)
171 (> (match-end 0) start
))
172 (setq parts
(cons (substring string start
(match-beginning 0)) parts
)
173 start
(match-end 0))))
174 (nreverse (if (< start
(length string
))
175 (cons (substring string start
) parts
)
178 (defun eudc-replace-in-string (str regexp newtext
)
179 "Replace all matches in STR for REGEXP with NEWTEXT.
180 Value is the new string."
184 (while (setq match
(string-match regexp str start
))
185 (setq prev-start start
189 (substring str prev-start match
)
191 (concat rtn-str
(substring str start
))))
195 ;;{{{ Server and Protocol Variable Routines
197 (defun eudc-server-local-variable-p (var)
198 "Return non-nil if VAR has server-local bindings."
199 (eudc-plist-member (get var
'eudc-locals
) 'server
))
201 (defun eudc-protocol-local-variable-p (var)
202 "Return non-nil if VAR has protocol-local bindings."
203 (eudc-plist-member (get var
'eudc-locals
) 'protocol
))
205 (defun eudc-default-set (var val
)
206 "Set the EUDC default value of VAR to VAL.
207 The current binding of VAR is not changed."
208 (put var
'eudc-locals
209 (plist-put (get var
'eudc-locals
) 'default val
))
210 (add-to-list 'eudc-local-vars var
))
212 (defun eudc-protocol-set (var val
&optional protocol
)
213 "Set the PROTOCOL-local binding of VAR to VAL.
214 If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
215 The current binding of VAR is changed only if PROTOCOL is omitted."
216 (if (eq 'unbound
(eudc-variable-default-value var
))
217 (eudc-default-set var
(symbol-value var
)))
218 (let* ((eudc-locals (get var
'eudc-locals
))
219 (protocol-locals (eudc-plist-get eudc-locals
'protocol
)))
220 (setq protocol-locals
(plist-put protocol-locals
(or protocol
223 (plist-put eudc-locals
'protocol protocol-locals
))
224 (put var
'eudc-locals eudc-locals
)
225 (add-to-list 'eudc-local-vars var
)
227 (eudc-update-variable var
))))
229 (defun eudc-server-set (var val
&optional server
)
230 "Set the SERVER-local binding of VAR to VAL.
231 If omitted SERVER defaults to the current value of `eudc-server'.
232 The current binding of VAR is changed only if SERVER is omitted."
233 (if (eq 'unbound
(eudc-variable-default-value var
))
234 (eudc-default-set var
(symbol-value var
)))
235 (let* ((eudc-locals (get var
'eudc-locals
))
236 (server-locals (eudc-plist-get eudc-locals
'server
)))
237 (setq server-locals
(plist-put server-locals
(or server
240 (plist-put eudc-locals
'server server-locals
))
241 (put var
'eudc-locals eudc-locals
)
242 (add-to-list 'eudc-local-vars var
)
244 (eudc-update-variable var
))))
247 (defun eudc-set (var val
)
248 "Set the most local (server, protocol or default) binding of VAR to VAL.
249 The current binding of VAR is also set to VAL"
251 ((not (eq 'unbound
(eudc-variable-server-value var
)))
252 (eudc-server-set var val
))
253 ((not (eq 'unbound
(eudc-variable-protocol-value var
)))
254 (eudc-protocol-set var val
))
256 (eudc-default-set var val
)))
259 (defun eudc-variable-default-value (var)
260 "Return the default binding of VAR.
261 Return `unbound' if VAR has no EUDC default value."
262 (let ((eudc-locals (get var
'eudc-locals
)))
263 (if (and (boundp var
)
265 (eudc-plist-get eudc-locals
'default
'unbound
)
268 (defun eudc-variable-protocol-value (var &optional protocol
)
269 "Return the value of VAR local to PROTOCOL.
270 Return `unbound' if VAR has no value local to PROTOCOL.
271 PROTOCOL defaults to `eudc-protocol'"
272 (let* ((eudc-locals (get var
'eudc-locals
))
274 (if (not (and (boundp var
)
276 (eudc-plist-member eudc-locals
'protocol
)))
278 (setq protocol-locals
(eudc-plist-get eudc-locals
'protocol
))
279 (eudc-lax-plist-get protocol-locals
281 eudc-protocol
) 'unbound
))))
283 (defun eudc-variable-server-value (var &optional server
)
284 "Return the value of VAR local to SERVER.
285 Return `unbound' if VAR has no value local to SERVER.
286 SERVER defaults to `eudc-server'"
287 (let* ((eudc-locals (get var
'eudc-locals
))
289 (if (not (and (boundp var
)
291 (eudc-plist-member eudc-locals
'server
)))
293 (setq server-locals
(eudc-plist-get eudc-locals
'server
))
294 (eudc-lax-plist-get server-locals
296 eudc-server
) 'unbound
))))
298 (defun eudc-update-variable (var)
299 "Set the value of VAR according to its locals.
300 If the VAR has a server- or protocol-local value corresponding
301 to the current `eudc-server' and `eudc-protocol' then it is set
302 accordingly. Otherwise it is set to its EUDC default binding"
305 ((not (eq 'unbound
(setq val
(eudc-variable-server-value var
))))
307 ((not (eq 'unbound
(setq val
(eudc-variable-protocol-value var
))))
309 ((not (eq 'unbound
(setq val
(eudc-variable-default-value var
))))
312 (defun eudc-update-local-variables ()
313 "Update all EUDC variables according to their local settings."
315 (mapcar 'eudc-update-variable eudc-local-vars
))
317 (eudc-default-set 'eudc-query-function nil
)
318 (eudc-default-set 'eudc-list-attributes-function nil
)
319 (eudc-default-set 'eudc-protocol-attributes-translation-alist nil
)
320 (eudc-default-set 'eudc-bbdb-conversion-alist nil
)
321 (eudc-default-set 'eudc-switch-to-server-hook nil
)
322 (eudc-default-set 'eudc-switch-from-server-hook nil
)
323 (eudc-default-set 'eudc-protocol-has-default-query-attributes nil
)
324 (eudc-default-set 'eudc-attribute-display-method-alist nil
)
329 ;; Add PROTOCOL to the list of supported protocols
330 (defun eudc-register-protocol (protocol)
331 (unless (memq protocol eudc-supported-protocols
)
332 (setq eudc-supported-protocols
333 (cons protocol eudc-supported-protocols
))
334 (put 'eudc-protocol
'custom-type
335 `(choice :menu-tag
"Protocol"
336 ,@(mapcar (lambda (s)
337 (list 'string
':tag
(symbol-name s
)))
338 eudc-supported-protocols
))))
339 (or (memq protocol eudc-known-protocols
)
340 (setq eudc-known-protocols
341 (cons protocol eudc-known-protocols
))))
344 (defun eudc-translate-query (query)
345 "Translate attribute names of QUERY.
346 The translation is done according to
347 `eudc-protocol-attributes-translation-alist'."
348 (if eudc-protocol-attributes-translation-alist
349 (mapcar (lambda (attribute)
350 (let ((trans (assq (car attribute
)
351 (symbol-value eudc-protocol-attributes-translation-alist
))))
353 (cons (cdr trans
) (cdr attribute
))
358 (defun eudc-translate-attribute-list (list)
359 "Translate a list of attribute names LIST.
360 The translation is done according to
361 `eudc-protocol-attributes-translation-alist'."
362 (if eudc-protocol-attributes-translation-alist
364 (mapcar (lambda (attribute)
365 (setq trans
(assq attribute
366 (symbol-value eudc-protocol-attributes-translation-alist
)))
373 (defun eudc-select (choices beg end
)
374 "Choose one from CHOICES using a completion.
375 BEG and END delimit the text which is to be replaced."
378 (completing-read "Multiple matches found; choose one: "
379 (mapcar 'list choices
)))
380 (delete-region beg end
)
381 (insert replacement
)))
383 (defun eudc-query (query &optional return-attributes no-translation
)
384 "Query the current directory server with QUERY.
385 QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
386 name and VALUE the corresponding value.
387 If NO-TRANSLATION is non-nil, ATTR is translated according to
388 `eudc-protocol-attributes-translation-alist'.
389 RETURN-ATTRIBUTES is a list of attributes to return defaulting to
390 `eudc-default-return-attributes'."
391 (unless eudc-query-function
392 (error "Don't know how to perform the query"))
394 (funcall eudc-query-function query
(or return-attributes
395 eudc-default-return-attributes
))
397 (funcall eudc-query-function
398 (eudc-translate-query query
)
401 (eudc-translate-attribute-list return-attributes
))
402 ((listp eudc-default-return-attributes
)
403 (eudc-translate-attribute-list eudc-default-return-attributes
))
405 eudc-default-return-attributes
)))))
407 (defun eudc-format-attribute-name-for-display (attribute)
408 "Format a directory attribute name for display.
409 ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
410 by the corresponding user name if any. Otherwise it is capitalized and
411 underscore characters are replaced by spaces."
412 (let ((match (assq attribute eudc-user-attribute-names-alist
)))
417 (split-string (symbol-name attribute
) "_")
420 (defun eudc-print-attribute-value (field)
421 "Insert the value of the directory FIELD at point.
422 The directory attribute name in car of FIELD is looked up in
423 `eudc-attribute-display-method-alist' and the corresponding method,
424 if any, is called to print the value in cdr of FIELD."
425 (let ((match (assoc (downcase (car field
))
426 eudc-attribute-display-method-alist
))
427 (col (current-column))
431 (eval (list (cdr match
) val
))
437 (insert val-elem
"\n")))
440 ((stringp val
) (split-string val
"\n"))
444 (defun eudc-print-record-field (field column-width
)
445 "Print the record field FIELD.
446 FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
447 COLUMN-WIDTH is the width of the first display column containing the
448 attribute name ATTR."
449 (let ((field-beg (point)))
450 ;; The record field that is passed to this function has already been processed
451 ;; by `eudc-format-attribute-name-for-display' so we don't need to call it
452 ;; again to display the attribute name
453 (insert (format (concat "%" (int-to-string column-width
) "s: ")
455 (put-text-property field-beg
(point) 'face
'bold
)
456 (indent-to (+ 2 column-width
))
457 (eudc-print-attribute-value field
)))
459 (defun eudc-display-records (records &optional raw-attr-names
)
460 "Display the record list RECORDS in a formatted buffer.
461 If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
462 otherwise they are formatted according to `eudc-user-attribute-names-alist'."
463 (let (inhibit-read-only
469 (with-output-to-temp-buffer "*Directory Query Results*"
470 (with-current-buffer standard-output
471 (setq buffer-read-only t
)
472 (setq inhibit-read-only t
)
474 (insert "Directory Query Result\n")
475 (insert "======================\n\n\n")
477 (insert "No match found.\n"
478 (if eudc-strict-return-matches
479 "Try setting `eudc-strict-return-matches' to nil or change `eudc-default-return-attributes'.\n"
481 ;; Replace field names with user names, compute max width
491 (symbol-name (car field
))
492 (eudc-format-attribute-name-for-display (car field
))))
493 (if (> (length attribute-name
) width
)
494 (setq width
(length attribute-name
)))
495 (cons attribute-name
(cdr field
))))
498 ;; Display the records
499 (setq first-record
(point))
504 ;; Map over the record fields to print the attribute/value pairs
507 (eudc-print-record-field field width
)))
509 ;; Store the record internal format in some convenient place
510 (overlay-put (make-overlay beg
(point))
513 (setq records
(cdr records
))
517 (widget-create 'push-button
518 :notify
(lambda (&rest _ignore
)
522 (widget-create 'push-button
523 :notify
(lambda (&rest _ignore
)
529 (goto-char first-record
))))))
531 (defun eudc-process-form ()
532 "Process the query form in current buffer and display the results."
535 (if (not (and (boundp 'eudc-form-widget-list
)
536 eudc-form-widget-list
))
537 (error "Not in a directory query form buffer")
540 (setq value
(widget-value (cdr wid-field
)))
541 (if (not (string= value
""))
542 (setq query-alist
(cons (cons (car wid-field
) value
)
544 eudc-form-widget-list
)
545 (kill-buffer (current-buffer))
546 (eudc-display-records (eudc-query query-alist
) eudc-use-raw-directory-names
))))
549 (defun eudc-filter-duplicate-attributes (record)
550 "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
556 ;; Search for multiple records
558 (not (listp (eudc-cdar rec
))))
559 (setq rec
(cdr rec
)))
561 (if (null (eudc-cdar rec
))
562 (list record
) ; No duplicate attrs in this record
565 (if (listp (cdr field
))
566 (setq duplicates
(cons field duplicates
))
567 (setq unique
(cons field unique
)))))
569 (setq result
(list unique
))
570 ;; Map over the record fields that have multiple values
574 (let ((method (if (consp eudc-duplicate-attribute-handling-method
)
582 eudc-protocol-attributes-translation-alist
)))
584 eudc-duplicate-attribute-handling-method
))
585 eudc-duplicate-attribute-handling-method
)))
587 ((or (null method
) (eq 'list method
))
589 (eudc-add-field-to-records field result
)))
592 (eudc-add-field-to-records (cons (car field
)
597 (eudc-add-field-to-records (cons (car field
)
602 ((eq 'duplicate method
)
604 (eudc-distribute-field-on-records field result
)))))))
608 (defun eudc-filter-partial-records (records attrs
)
609 "Eliminate records that do not contain all ATTRS from RECORDS."
618 (consp (assq attr rec
))))
623 (defun eudc-add-field-to-records (field records
)
624 "Add FIELD to each individual record in RECORDS and return the resulting list."
630 (defun eudc-distribute-field-on-records (field records
)
631 "Duplicate each individual record in RECORDS according to value of FIELD.
632 Each copy is added a new field containing one of the values of FIELD."
634 (values (cdr field
)))
635 ;; Uniquify values first
637 (setcdr values
(delete (car values
) (cdr values
)))
638 (setq values
(cdr values
)))
642 (let ((result-list (copy-sequence records
)))
643 (setq result-list
(eudc-add-field-to-records
644 (cons (car field
) value
)
646 (setq result
(append result-list result
))
652 (define-derived-mode eudc-mode special-mode
"EUDC"
653 "Major mode used in buffers displaying the results of directory queries.
654 There is no sense in calling this command from a buffer other than
655 one containing the results of a directory query.
657 These are the special commands of EUDC mode:
658 q -- Kill this buffer.
659 f -- Display a form to query the current directory server.
660 n -- Move to next record.
661 p -- Move to previous record.
662 b -- Insert record at point into the BBDB database."
663 (if (not (featurep 'xemacs
))
664 (easy-menu-define eudc-emacs-menu eudc-mode-map
"" (eudc-menu))
665 (setq mode-popup-menu
(eudc-menu))))
669 ;;{{{ High-level interfaces (interactive functions)
671 (defun eudc-customize ()
672 "Customize the EUDC package."
674 (customize-group 'eudc
))
677 (defun eudc-set-server (server protocol
&optional no-save
)
678 "Set the directory server to SERVER using PROTOCOL.
679 Unless NO-SAVE is non-nil, the server is saved as the default
680 server for future sessions."
682 (read-from-minibuffer "Directory Server: ")
683 (intern (completing-read "Protocol: "
684 (mapcar (lambda (elt)
685 (cons (symbol-name elt
)
687 eudc-known-protocols
)))))
688 (unless (or (null protocol
)
690 eudc-supported-protocols
)
691 (load (concat "eudcb-" (symbol-name protocol
)) t
))
692 (error "Unsupported protocol: %s" protocol
))
693 (run-hooks 'eudc-switch-from-server-hook
)
694 (setq eudc-protocol protocol
)
695 (setq eudc-server server
)
696 (eudc-update-local-variables)
697 (run-hooks 'eudc-switch-to-server-hook
)
698 (if (called-interactively-p 'interactive
)
699 (message "Current directory server is now %s (%s)" eudc-server eudc-protocol
))
701 (eudc-save-options)))
704 (defun eudc-get-email (name &optional error
)
705 "Get the email field of NAME from the directory server.
706 If ERROR is non-nil, report an error if there is none."
707 (interactive "sName: \np")
709 (call-interactively 'eudc-set-server
))
710 (let ((result (eudc-query (list (cons 'name name
)) '(email)))
712 (if (null (cdr result
))
713 (setq email
(eudc-cdaar result
))
714 (error "Multiple match--use the query form"))
718 (error "No record matching %s" name
)))
722 (defun eudc-get-phone (name &optional error
)
723 "Get the phone field of NAME from the directory server.
724 If ERROR is non-nil, report an error if there is none."
725 (interactive "sName: \np")
727 (call-interactively 'eudc-set-server
))
728 (let ((result (eudc-query (list (cons 'name name
)) '(phone)))
730 (if (null (cdr result
))
731 (setq phone
(eudc-cdaar result
))
732 (error "Multiple match--use the query form"))
736 (error "No record matching %s" name
)))
739 (defun eudc-get-attribute-list ()
740 "Return a list of valid attributes for the current server.
741 When called interactively the list is formatted in a dedicated buffer
742 otherwise a list of symbols is returned."
744 (if eudc-list-attributes-function
745 (let ((entries (funcall eudc-list-attributes-function
746 (called-interactively-p 'interactive
))))
748 (if (called-interactively-p 'interactive
)
749 (eudc-display-records entries t
)
751 (error "The %s protocol has no support for listing attributes" eudc-protocol
)))
753 (defun eudc-format-query (words format
)
754 "Use FORMAT to build a EUDC query from WORDS."
760 (while (and words format
)
761 (setq query-alist
(cons (cons (car format
) (car words
))
763 (setq words
(cdr words
)
764 format
(cdr format
)))
765 ;; If the same attribute appears more than once, merge
766 ;; the corresponding values
768 (setq key
(eudc-caar query-alist
)
769 val
(eudc-cdar query-alist
)
770 cell
(assq key query
))
772 (setcdr cell
(concat (cdr cell
) " " val
))
773 (setq query
(cons (car query-alist
) query
)))
774 (setq query-alist
(cdr query-alist
)))
776 (if eudc-protocol-has-default-query-attributes
777 (mapconcat 'identity words
" ")
778 (list (cons 'name
(mapconcat 'identity words
" ")))))))
780 (defun eudc-extract-n-word-formats (format-list n
)
781 "Extract a list of N-long formats from FORMAT-LIST.
782 If none try N - 1 and so forth."
784 (while (and (null formats
)
788 (mapcar (lambda (format)
799 (defun eudc-expand-inline (&optional replace
)
800 "Query the directory server, and expand the query string before point.
801 The query string consists of the buffer substring from the point back to
802 the preceding comma, colon or beginning of line.
803 The variable `eudc-inline-query-format' controls how to associate the
804 individual inline query words with directory attribute names.
805 After querying the server for the given string, the expansion specified by
806 `eudc-inline-expansion-format' is inserted in the buffer at point.
807 If REPLACE is non-nil, then this expansion replaces the name in the buffer.
808 `eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
809 Multiple servers can be tried with the same query until one finds a match,
810 see `eudc-inline-expansion-servers'"
813 ((eq eudc-inline-expansion-servers
'current-server
)
815 (call-interactively 'eudc-set-server
)))
816 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
818 ;; Allow server to be nil if hotlist is set.
820 (call-interactively 'eudc-set-server
)))
821 ((eq eudc-inline-expansion-servers
'hotlist
)
822 (or eudc-server-hotlist
823 (error "No server in the hotlist")))
825 (error "Wrong value for `eudc-inline-expansion-servers': %S"
826 eudc-inline-expansion-servers
)))
829 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
830 (point-at-bol) 'move
)
831 (goto-char (match-end 0)))
833 (query-words (split-string (buffer-substring-no-properties beg end
)
839 (eudc-former-server eudc-server
)
840 (eudc-former-protocol eudc-protocol
)
843 ;; Prepare the list of servers to query
844 (setq servers
(copy-sequence eudc-server-hotlist
))
847 ((eq eudc-inline-expansion-servers
'hotlist
)
849 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
851 (cons (cons eudc-server eudc-protocol
)
852 (delete (cons eudc-server eudc-protocol
) servers
))
853 eudc-server-hotlist
))
854 ((eq eudc-inline-expansion-servers
'current-server
)
855 (list (cons eudc-server eudc-protocol
)))))
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
))
864 ;; Loop on the servers
866 (eudc-set-server (eudc-caar servers
) (eudc-cdar servers
) t
)
868 ;; Determine which formats apply in the query-format list
871 (eudc-extract-n-word-formats eudc-inline-query-format
872 (length query-words
))
873 (if (null eudc-protocol-has-default-query-attributes
)
876 ;; Loop on query-formats
880 (eudc-format-query query-words
(car query-formats
))
881 (eudc-translate-attribute-list
882 (cdr eudc-inline-expansion-format
))))
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
894 ;; Process response through eudc-inline-expansion-format
896 (setq response-string
898 (car eudc-inline-expansion-format
)
901 (or (cdr (assq field
(car response
)))
903 (eudc-translate-attribute-list
904 (cdr eudc-inline-expansion-format
)))))
905 (if (> (length response-string
) 0)
906 (setq response-strings
907 (cons response-string response-strings
)))
908 (setq response
(cdr response
)))
911 (and replace
(not eudc-expansion-overwrites-query
))
912 (and (not replace
) eudc-expansion-overwrites-query
))
913 (kill-ring-save beg end
))
915 ((or (= (length response-strings
) 1)
916 (null eudc-multiple-match-handling-method
)
917 (eq eudc-multiple-match-handling-method
'first
))
918 (delete-region beg end
)
919 (insert (car response-strings
)))
920 ((eq eudc-multiple-match-handling-method
'select
)
921 (eudc-select response-strings beg end
))
922 ((eq eudc-multiple-match-handling-method
'all
)
923 (delete-region beg end
)
924 (insert (mapconcat 'identity response-strings
", ")))
925 ((eq eudc-multiple-match-handling-method
'abort
)
926 (error "There is more than one match for the query")))))
927 (or (and (equal eudc-server eudc-former-server
)
928 (equal eudc-protocol eudc-former-protocol
))
929 (eudc-set-server eudc-former-server eudc-former-protocol t
)))))
932 (defun eudc-query-form (&optional get-fields-from-server
)
933 "Display a form to query the directory server.
934 If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
935 queries the server for the existing fields and displays a corresponding form."
937 (let ((fields (or (and get-fields-from-server
938 (eudc-get-attribute-list))
939 eudc-query-form-attributes
))
940 (buffer (get-buffer-create "*Directory Query Form*"))
946 (switch-to-buffer buffer
)
947 (setq inhibit-read-only t
)
949 (kill-all-local-variables)
950 (make-local-variable 'eudc-form-widget-list
)
951 (widget-insert "Directory Query Form\n")
952 (widget-insert "====================\n\n")
953 (widget-insert "Current server is: " (or eudc-server
955 (call-interactively 'eudc-set-server
)
958 (widget-insert "Protocol : " (symbol-name eudc-protocol
) "\n")
959 ;; Build the list of prompts
960 (setq prompts
(if eudc-use-raw-directory-names
961 (mapcar 'symbol-name
(eudc-translate-attribute-list fields
))
964 (or (and (assq field eudc-user-attribute-names-alist
)
965 (cdr (assq field eudc-user-attribute-names-alist
)))
966 (capitalize (symbol-name field
)))))
968 ;; Loop over prompt strings to find the longest one
971 (if (> (length prompt
) width
)
972 (setq width
(length prompt
)))))
974 ;; Insert the first widget out of the mapcar to leave the cursor
975 ;; in the first field
976 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
978 (setq widget
(widget-create 'editable-field
:size
15))
979 (setq eudc-form-widget-list
(cons (cons (car fields
) widget
)
980 eudc-form-widget-list
))
981 (setq fields
(cdr fields
))
982 (setq prompts
(cdr prompts
))
985 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
986 (setq widget
(widget-create 'editable-field
988 (setq eudc-form-widget-list
(cons (cons field widget
)
989 eudc-form-widget-list
))
990 (setq prompts
(cdr prompts
))))
992 (widget-insert "\n\n")
993 (widget-create 'push-button
994 :notify
(lambda (&rest _ignore
)
998 (widget-create 'push-button
999 :notify
(lambda (&rest _ignore
)
1003 (widget-create 'push-button
1004 :notify
(lambda (&rest _ignore
)
1008 (use-local-map widget-keymap
)
1012 (defun eudc-bookmark-server (server protocol
)
1013 "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
1014 (interactive "sDirectory server: \nsProtocol: ")
1015 (if (member (cons server protocol
) eudc-server-hotlist
)
1016 (error "%s:%s is already in the hotlist" protocol server
)
1017 (setq eudc-server-hotlist
(cons (cons server protocol
) eudc-server-hotlist
))
1019 (eudc-save-options)))
1021 (defun eudc-bookmark-current-server ()
1022 "Add current server to the EUDC `servers' hotlist."
1024 (eudc-bookmark-server eudc-server eudc-protocol
))
1026 (defun eudc-save-options ()
1027 "Save options to `eudc-options-file'."
1029 (with-current-buffer (find-file-noselect eudc-options-file t
)
1030 (goto-char (point-min))
1031 ;; delete the previous setq
1032 (let ((standard-output (current-buffer))
1038 (let ((sexp (condition-case nil
1039 (read (current-buffer))
1040 (end-of-file (throw 'found nil
)))))
1043 ((eq (car sexp
) 'eudc-set-server
)
1044 (delete-region (save-excursion
1048 (setq set-server-p t
))
1049 ((and (eq (car sexp
) 'setq
)
1050 (eq (eudc-cadr sexp
) 'eudc-server-hotlist
))
1051 (delete-region (save-excursion
1055 (setq set-hotlist-p t
))
1056 ((and (eq (car sexp
) 'provide
)
1057 (equal (eudc-cadr sexp
) '(quote eudc-options-file
)))
1058 (setq provide-p t
)))
1062 (throw 'found t
))))))
1063 (if (eq (point-min) (point-max))
1064 (princ ";; This file was automatically generated by eudc.el.\n\n"))
1066 (princ "(provide 'eudc-options-file)\n"))
1069 (delete-blank-lines)
1070 (princ "(eudc-set-server ")
1073 (prin1 eudc-protocol
)
1075 (princ "(setq eudc-server-hotlist '")
1076 (prin1 eudc-server-hotlist
)
1080 (defun eudc-move-to-next-record ()
1081 "Move to next record, in a buffer displaying directory query results."
1083 (if (not (derived-mode-p 'eudc-mode
))
1084 (error "Not in a EUDC buffer")
1085 (let ((pt (next-overlay-change (point))))
1086 (if (< pt
(point-max))
1088 (error "No more records after point")))))
1090 (defun eudc-move-to-previous-record ()
1091 "Move to previous record, in a buffer displaying directory query results."
1093 (if (not (derived-mode-p 'eudc-mode
))
1094 (error "Not in a EUDC buffer")
1095 (let ((pt (previous-overlay-change (point))))
1096 (if (> pt
(point-min))
1098 (error "No more records before point")))))
1102 ;;{{{ Menus and keymaps
1106 (defconst eudc-custom-generated-menu
(cdr (custom-menu-create 'eudc
)))
1108 (defconst eudc-tail-menu
1110 ["Query with Form" eudc-query-form
1111 :help
"Display a form to query the directory server"]
1112 ["Expand Inline Query" eudc-expand-inline
1113 :help
"Query the directory server, and expand the query string before point"]
1114 ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
1115 (and (or (featurep 'bbdb
)
1116 (prog1 (locate-library "bbdb") (message "")))
1117 (overlays-at (point))
1118 (overlay-get (car (overlays-at (point))) 'eudc-record
))
1119 :help
"Insert record at point into the BBDB database"]
1120 ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
1121 (and (derived-mode-p 'eudc-mode
)
1122 (or (featurep 'bbdb
)
1123 (prog1 (locate-library "bbdb") (message ""))))
1124 :help
"Insert all the records returned by a directory query into BBDB"]
1126 ["Get Email" eudc-get-email
1127 :help
"Get the email field of NAME from the directory server"]
1128 ["Get Phone" eudc-get-phone
1129 :help
"Get the phone field of name from the directory server"]
1130 ["List Valid Attribute Names" eudc-get-attribute-list
1131 :help
"Return a list of valid attributes for the current server"]
1133 ,(cons "Customize" eudc-custom-generated-menu
)))
1136 (defconst eudc-server-menu
1138 ["Bookmark Current Server" eudc-bookmark-current-server
1139 :help
"Add current server to the EUDC `servers' hotlist"]
1140 ["Edit Server List" eudc-edit-hotlist
1141 :help
"Edit the hotlist of directory servers in a specialized buffer"]
1142 ["New Server" eudc-set-server
1143 :help
"Set the directory server to SERVER using PROTOCOL"]))
1147 (append '("Directory Search")
1154 (let* ((server (car servspec
))
1155 (protocol (cdr servspec
))
1156 (proto-name (symbol-name protocol
)))
1157 (setq command
(intern (concat "eudc-set-server-"
1161 (if (not (fboundp command
))
1165 (eudc-set-server ,server
(quote ,protocol
))
1166 (message "Selected directory server is now %s (%s)"
1169 (vector (format "%s (%s)" server proto-name
)
1172 :selected
`(equal eudc-server
,server
)))))
1173 eudc-server-hotlist
)
1177 (defun eudc-install-menu ()
1179 ((and (featurep 'xemacs
) (featurep 'menubar
))
1180 (add-submenu '("Tools") (eudc-menu)))
1181 ((not (featurep 'xemacs
))
1183 ((fboundp 'easy-menu-create-menu
)
1186 [menu-bar tools directory-search
]
1187 (cons "Directory Search"
1188 (easy-menu-create-menu "Directory Search" (cdr (eudc-menu))))))
1189 ((fboundp 'easy-menu-add-item
)
1190 (let ((menu (eudc-menu)))
1191 (easy-menu-add-item nil
'("tools") (easy-menu-create-menu (car menu
)
1193 ((fboundp 'easy-menu-create-keymaps
)
1194 (easy-menu-define eudc-menu-map eudc-mode-map
"Directory Client Menu" (eudc-menu))
1197 [menu-bar tools eudc
]
1198 (cons "Directory Search"
1199 (easy-menu-create-keymaps "Directory Search" (cdr (eudc-menu))))))
1201 (error "Unknown version of easymenu"))))
1205 ;;; Load time initializations :
1207 ;;; Load the options file
1208 (if (and (not noninteractive
)
1209 (and (locate-library eudc-options-file
)
1210 (progn (message "") t
)) ; Remove mode line message
1211 (not (featurep 'eudc-options-file
)))
1212 (load eudc-options-file
))
1214 ;;; Install the full menu
1215 (unless (featurep 'infodock
)
1216 (eudc-install-menu))
1219 ;;; The following installs a short menu for EUDC at XEmacs startup.
1222 (defun eudc-load-eudc ()
1223 "Load the Emacs Unified Directory Client.
1224 This does nothing except loading eudc by autoload side-effect."
1230 ((not (featurep 'xemacs
))
1231 (defvar eudc-tools-menu
1232 (let ((map (make-sparse-keymap "Directory Search")))
1233 (define-key map
[phone]
1234 `(menu-item ,(purecopy "Get Phone") eudc-get-phone
1235 :help ,(purecopy "Get the phone field of name from the directory server")))
1236 (define-key map [email]
1237 `(menu-item ,(purecopy "Get Email") eudc-get-email
1238 :help ,(purecopy "Get the email field of NAME from the directory server")))
1239 (define-key map [separator-eudc-email] menu-bar-separator)
1240 (define-key map [expand-inline]
1241 `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
1242 :help ,(purecopy "Query the directory server, and expand the query string before point")))
1243 (define-key map [query]
1244 `(menu-item ,(purecopy "Query with Form") eudc-query-form
1245 :help ,(purecopy "Display a form to query the directory server")))
1246 (define-key map [separator-eudc-query] menu-bar-separator)
1247 (define-key map [new]
1248 `(menu-item ,(purecopy "New Server") eudc-set-server
1249 :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
1250 (define-key map [load]
1251 `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
1252 :help ,(purecopy "Load the Emacs Unified Directory Client")))
1254 (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
1256 (let ((menu '("Directory Search"
1257 ["Load Hotlist of Servers" eudc-load-eudc t]
1258 ["New Server" eudc-set-server t]
1260 ["Query with Form" eudc-query-form t]
1261 ["Expand Inline Query" eudc-expand-inline t]
1263 ["Get Email" eudc-get-email t]
1264 ["Get Phone" eudc-get-phone t])))
1265 (if (not (featurep 'eudc-autoloads))
1266 (if (featurep 'xemacs)
1267 (if (and (featurep 'menubar)
1268 (not (featurep 'infodock)))
1269 (add-submenu '("Tools") menu))
1272 ((fboundp 'easy-menu-add-item)
1273 (easy-menu-add-item nil '("tools")
1274 (easy-menu-create-menu (car menu)
1276 ((fboundp 'easy-menu-create-keymaps)
1279 [menu-bar tools eudc]
1280 (cons "Directory Search"
1281 (easy-menu-create-keymaps "Directory Search"
1282 (cdr menu)))))))))))
1288 ;;; eudc.el ends here