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
50 (eval-when-compile (require 'cl-lib
))
53 (if (not (fboundp 'make-overlay
))
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 variables that have server- or protocol-local bindings
83 (defvar eudc-local-vars nil
)
85 ;; Protocol local. Query function
86 (defvar eudc-query-function nil
)
88 ;; Protocol local. A function that retrieves a list of valid attribute names
89 (defvar eudc-list-attributes-function nil
)
91 ;; Protocol local. A mapping between EUDC attribute names and corresponding
92 ;; protocol specific names. The following names are defined by EUDC and may be
93 ;; included in that list: `name' , `firstname', `email', `phone'
94 (defvar eudc-protocol-attributes-translation-alist nil
)
96 ;; Protocol local. Mapping between protocol attribute names and BBDB field
98 (defvar eudc-bbdb-conversion-alist nil
)
100 ;; Protocol/Server local. Hook called upon switching to that server
101 (defvar eudc-switch-to-server-hook nil
)
103 ;; Protocol/Server local. Hook called upon switching from that server
104 (defvar eudc-switch-from-server-hook nil
)
106 ;; Protocol local. Whether the protocol supports queries with no specified
108 (defvar eudc-protocol-has-default-query-attributes nil
)
110 (defun eudc-plist-member (plist prop
)
111 "Return t if PROP has a value specified in PLIST."
112 (if (not (= 0 (%
(length plist
) 2)))
113 (error "Malformed plist"))
116 (if (eq prop
(car plist
))
118 (setq plist
(cdr (cdr plist
))))
121 ;; Emacs's plist-get lacks third parameter
122 (defun eudc-plist-get (plist prop
&optional default
)
123 "Extract a value from a property list.
124 PLIST is a property list, which is a list of the form
125 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
126 corresponding to the given PROP, or DEFAULT if PROP is not
127 one of the properties on the list."
128 (if (eudc-plist-member plist prop
)
129 (plist-get plist prop
)
132 (defun eudc-lax-plist-get (plist prop
&optional default
)
133 "Extract a value from a lax property list.
135 PLIST is a lax property list, which is a list of the form (PROP1
136 VALUE1 PROP2 VALUE2...), where comparisons between properties are done
137 using `equal' instead of `eq'. This function returns the value
138 corresponding to PROP, or DEFAULT if PROP is not one of the
139 properties on the list."
140 (if (not (= 0 (%
(length plist
) 2)))
141 (error "Malformed plist"))
144 (if (equal prop
(car plist
))
145 (throw 'found
(car (cdr plist
))))
146 (setq plist
(cdr (cdr plist
))))
149 (if (not (fboundp 'split-string
))
150 (defun split-string (string &optional pattern
)
151 "Return a list of substrings of STRING which are separated by PATTERN.
152 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
154 (setq pattern
"[ \f\t\n\r\v]+"))
155 (let (parts (start 0))
156 (when (string-match pattern string
0)
157 (if (> (match-beginning 0) 0)
158 (setq parts
(cons (substring string
0 (match-beginning 0)) nil
)))
159 (setq start
(match-end 0))
160 (while (and (string-match pattern string start
)
161 (> (match-end 0) start
))
162 (setq parts
(cons (substring string start
(match-beginning 0)) parts
)
163 start
(match-end 0))))
164 (nreverse (if (< start
(length string
))
165 (cons (substring string start
) parts
)
168 (defun eudc-replace-in-string (str regexp newtext
)
169 "Replace all matches in STR for REGEXP with NEWTEXT.
170 Value is the new string."
174 (while (setq match
(string-match regexp str start
))
175 (setq prev-start start
179 (substring str prev-start match
)
181 (concat rtn-str
(substring str start
))))
185 ;;{{{ Server and Protocol Variable Routines
187 (defun eudc-server-local-variable-p (var)
188 "Return non-nil if VAR has server-local bindings."
189 (eudc-plist-member (get var
'eudc-locals
) 'server
))
191 (defun eudc-protocol-local-variable-p (var)
192 "Return non-nil if VAR has protocol-local bindings."
193 (eudc-plist-member (get var
'eudc-locals
) 'protocol
))
195 (defun eudc-default-set (var val
)
196 "Set the EUDC default value of VAR to VAL.
197 The current binding of VAR is not changed."
198 (put var
'eudc-locals
199 (plist-put (get var
'eudc-locals
) 'default val
))
200 (add-to-list 'eudc-local-vars var
))
202 (defun eudc-protocol-set (var val
&optional protocol
)
203 "Set the PROTOCOL-local binding of VAR to VAL.
204 If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
205 The current binding of VAR is changed only if PROTOCOL is omitted."
206 (if (eq 'unbound
(eudc-variable-default-value var
))
207 (eudc-default-set var
(symbol-value var
)))
208 (let* ((eudc-locals (get var
'eudc-locals
))
209 (protocol-locals (eudc-plist-get eudc-locals
'protocol
)))
210 (setq protocol-locals
(plist-put protocol-locals
(or protocol
213 (plist-put eudc-locals
'protocol protocol-locals
))
214 (put var
'eudc-locals eudc-locals
)
215 (add-to-list 'eudc-local-vars var
)
217 (eudc-update-variable var
))))
219 (defun eudc-server-set (var val
&optional server
)
220 "Set the SERVER-local binding of VAR to VAL.
221 If omitted SERVER defaults to the current value of `eudc-server'.
222 The current binding of VAR is changed only if SERVER is omitted."
223 (if (eq 'unbound
(eudc-variable-default-value var
))
224 (eudc-default-set var
(symbol-value var
)))
225 (let* ((eudc-locals (get var
'eudc-locals
))
226 (server-locals (eudc-plist-get eudc-locals
'server
)))
227 (setq server-locals
(plist-put server-locals
(or server
230 (plist-put eudc-locals
'server server-locals
))
231 (put var
'eudc-locals eudc-locals
)
232 (add-to-list 'eudc-local-vars var
)
234 (eudc-update-variable var
))))
237 (defun eudc-set (var val
)
238 "Set the most local (server, protocol or default) binding of VAR to VAL.
239 The current binding of VAR is also set to VAL"
241 ((not (eq 'unbound
(eudc-variable-server-value var
)))
242 (eudc-server-set var val
))
243 ((not (eq 'unbound
(eudc-variable-protocol-value var
)))
244 (eudc-protocol-set var val
))
246 (eudc-default-set var val
)))
249 (defun eudc-variable-default-value (var)
250 "Return the default binding of VAR.
251 Return `unbound' if VAR has no EUDC default value."
252 (let ((eudc-locals (get var
'eudc-locals
)))
253 (if (and (boundp var
)
255 (eudc-plist-get eudc-locals
'default
'unbound
)
258 (defun eudc-variable-protocol-value (var &optional protocol
)
259 "Return the value of VAR local to PROTOCOL.
260 Return `unbound' if VAR has no value local to PROTOCOL.
261 PROTOCOL defaults to `eudc-protocol'"
262 (let* ((eudc-locals (get var
'eudc-locals
))
264 (if (not (and (boundp var
)
266 (eudc-plist-member eudc-locals
'protocol
)))
268 (setq protocol-locals
(eudc-plist-get eudc-locals
'protocol
))
269 (eudc-lax-plist-get protocol-locals
271 eudc-protocol
) 'unbound
))))
273 (defun eudc-variable-server-value (var &optional server
)
274 "Return the value of VAR local to SERVER.
275 Return `unbound' if VAR has no value local to SERVER.
276 SERVER defaults to `eudc-server'"
277 (let* ((eudc-locals (get var
'eudc-locals
))
279 (if (not (and (boundp var
)
281 (eudc-plist-member eudc-locals
'server
)))
283 (setq server-locals
(eudc-plist-get eudc-locals
'server
))
284 (eudc-lax-plist-get server-locals
286 eudc-server
) 'unbound
))))
288 (defun eudc-update-variable (var)
289 "Set the value of VAR according to its locals.
290 If the VAR has a server- or protocol-local value corresponding
291 to the current `eudc-server' and `eudc-protocol' then it is set
292 accordingly. Otherwise it is set to its EUDC default binding"
295 ((not (eq 'unbound
(setq val
(eudc-variable-server-value var
))))
297 ((not (eq 'unbound
(setq val
(eudc-variable-protocol-value var
))))
299 ((not (eq 'unbound
(setq val
(eudc-variable-default-value var
))))
302 (defun eudc-update-local-variables ()
303 "Update all EUDC variables according to their local settings."
305 (mapcar 'eudc-update-variable eudc-local-vars
))
307 (eudc-default-set 'eudc-query-function nil
)
308 (eudc-default-set 'eudc-list-attributes-function nil
)
309 (eudc-default-set 'eudc-protocol-attributes-translation-alist nil
)
310 (eudc-default-set 'eudc-bbdb-conversion-alist nil
)
311 (eudc-default-set 'eudc-switch-to-server-hook nil
)
312 (eudc-default-set 'eudc-switch-from-server-hook nil
)
313 (eudc-default-set 'eudc-protocol-has-default-query-attributes nil
)
314 (eudc-default-set 'eudc-attribute-display-method-alist nil
)
319 ;; Add PROTOCOL to the list of supported protocols
320 (defun eudc-register-protocol (protocol)
321 (unless (memq protocol eudc-supported-protocols
)
322 (setq eudc-supported-protocols
323 (cons protocol eudc-supported-protocols
))
324 (put 'eudc-protocol
'custom-type
325 `(choice :menu-tag
"Protocol"
326 ,@(mapcar (lambda (s)
327 (list 'string
':tag
(symbol-name s
)))
328 eudc-supported-protocols
))))
329 (or (memq protocol eudc-known-protocols
)
330 (setq eudc-known-protocols
331 (cons protocol eudc-known-protocols
))))
334 (defun eudc-translate-query (query)
335 "Translate attribute names of QUERY.
336 The translation is done according to
337 `eudc-protocol-attributes-translation-alist'."
338 (if eudc-protocol-attributes-translation-alist
339 (mapcar (lambda (attribute)
340 (let ((trans (assq (car attribute
)
341 (symbol-value eudc-protocol-attributes-translation-alist
))))
343 (cons (cdr trans
) (cdr attribute
))
348 (defun eudc-translate-attribute-list (list)
349 "Translate a list of attribute names LIST.
350 The translation is done according to
351 `eudc-protocol-attributes-translation-alist'."
352 (if eudc-protocol-attributes-translation-alist
354 (mapcar (lambda (attribute)
355 (setq trans
(assq attribute
356 (symbol-value eudc-protocol-attributes-translation-alist
)))
363 (defun eudc-select (choices beg end
)
364 "Choose one from CHOICES using a completion.
365 BEG and END delimit the text which is to be replaced."
368 (completing-read "Multiple matches found; choose one: "
369 (mapcar 'list choices
)))
370 (delete-region beg end
)
371 (insert replacement
)))
373 (defun eudc-query (query &optional return-attributes no-translation
)
374 "Query the current directory server with QUERY.
375 QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
376 name and VALUE the corresponding value.
377 If NO-TRANSLATION is non-nil, ATTR is translated according to
378 `eudc-protocol-attributes-translation-alist'.
379 RETURN-ATTRIBUTES is a list of attributes to return defaulting to
380 `eudc-default-return-attributes'."
381 (unless eudc-query-function
382 (error "Don't know how to perform the query"))
384 (funcall eudc-query-function query
(or return-attributes
385 eudc-default-return-attributes
))
387 (funcall eudc-query-function
388 (eudc-translate-query query
)
391 (eudc-translate-attribute-list return-attributes
))
392 ((listp eudc-default-return-attributes
)
393 (eudc-translate-attribute-list eudc-default-return-attributes
))
395 eudc-default-return-attributes
)))))
397 (defun eudc-format-attribute-name-for-display (attribute)
398 "Format a directory attribute name for display.
399 ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
400 by the corresponding user name if any. Otherwise it is capitalized and
401 underscore characters are replaced by spaces."
402 (let ((match (assq attribute eudc-user-attribute-names-alist
)))
407 (split-string (symbol-name attribute
) "_")
410 (defun eudc-print-attribute-value (field)
411 "Insert the value of the directory FIELD at point.
412 The directory attribute name in car of FIELD is looked up in
413 `eudc-attribute-display-method-alist' and the corresponding method,
414 if any, is called to print the value in cdr of FIELD."
415 (let ((match (assoc (downcase (car field
))
416 eudc-attribute-display-method-alist
))
417 (col (current-column))
421 (eval (list (cdr match
) val
))
427 (insert val-elem
"\n")))
430 ((stringp val
) (split-string val
"\n"))
434 (defun eudc-print-record-field (field column-width
)
435 "Print the record field FIELD.
436 FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
437 COLUMN-WIDTH is the width of the first display column containing the
438 attribute name ATTR."
439 (let ((field-beg (point)))
440 ;; The record field that is passed to this function has already been processed
441 ;; by `eudc-format-attribute-name-for-display' so we don't need to call it
442 ;; again to display the attribute name
443 (insert (format (concat "%" (int-to-string column-width
) "s: ")
445 (put-text-property field-beg
(point) 'face
'bold
)
446 (indent-to (+ 2 column-width
))
447 (eudc-print-attribute-value field
)))
449 (defun eudc-display-records (records &optional raw-attr-names
)
450 "Display the record list RECORDS in a formatted buffer.
451 If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
452 otherwise they are formatted according to `eudc-user-attribute-names-alist'."
453 (let (inhibit-read-only
459 (with-output-to-temp-buffer "*Directory Query Results*"
460 (with-current-buffer standard-output
461 (setq buffer-read-only t
)
462 (setq inhibit-read-only t
)
464 (insert "Directory Query Result\n")
465 (insert "======================\n\n\n")
467 (insert "No match found.\n"
468 (if eudc-strict-return-matches
469 "Try setting `eudc-strict-return-matches' to nil or change `eudc-default-return-attributes'.\n"
471 ;; Replace field names with user names, compute max width
481 (symbol-name (car field
))
482 (eudc-format-attribute-name-for-display (car field
))))
483 (if (> (length attribute-name
) width
)
484 (setq width
(length attribute-name
)))
485 (cons attribute-name
(cdr field
))))
488 ;; Display the records
489 (setq first-record
(point))
494 ;; Map over the record fields to print the attribute/value pairs
497 (eudc-print-record-field field width
)))
499 ;; Store the record internal format in some convenient place
500 (overlay-put (make-overlay beg
(point))
503 (setq records
(cdr records
))
507 (widget-create 'push-button
508 :notify
(lambda (&rest _ignore
)
512 (widget-create 'push-button
513 :notify
(lambda (&rest _ignore
)
519 (goto-char first-record
))))))
521 (defun eudc-process-form ()
522 "Process the query form in current buffer and display the results."
525 (if (not (and (boundp 'eudc-form-widget-list
)
526 eudc-form-widget-list
))
527 (error "Not in a directory query form buffer")
530 (setq value
(widget-value (cdr wid-field
)))
531 (if (not (string= value
""))
532 (setq query-alist
(cons (cons (car wid-field
) value
)
534 eudc-form-widget-list
)
535 (kill-buffer (current-buffer))
536 (eudc-display-records (eudc-query query-alist
) eudc-use-raw-directory-names
))))
539 (defun eudc-filter-duplicate-attributes (record)
540 "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
546 ;; Search for multiple records
548 (not (listp (cdar rec
))))
549 (setq rec
(cdr rec
)))
551 (if (null (cdar rec
))
552 (list record
) ; No duplicate attrs in this record
555 (if (listp (cdr field
))
556 (setq duplicates
(cons field duplicates
))
557 (setq unique
(cons field unique
)))))
559 (setq result
(list unique
))
560 ;; Map over the record fields that have multiple values
564 (let ((method (if (consp eudc-duplicate-attribute-handling-method
)
572 eudc-protocol-attributes-translation-alist
)))
574 eudc-duplicate-attribute-handling-method
))
575 eudc-duplicate-attribute-handling-method
)))
577 ((or (null method
) (eq 'list method
))
579 (eudc-add-field-to-records field result
)))
582 (eudc-add-field-to-records (cons (car field
)
587 (eudc-add-field-to-records (cons (car field
)
592 ((eq 'duplicate method
)
594 (eudc-distribute-field-on-records field result
)))))))
598 (defun eudc-filter-partial-records (records attrs
)
599 "Eliminate records that do not contain all ATTRS from RECORDS."
608 (consp (assq attr rec
))))
613 (defun eudc-add-field-to-records (field records
)
614 "Add FIELD to each individual record in RECORDS and return the resulting list."
620 (defun eudc-distribute-field-on-records (field records
)
621 "Duplicate each individual record in RECORDS according to value of FIELD.
622 Each copy is added a new field containing one of the values of FIELD."
624 (values (cdr field
)))
625 ;; Uniquify values first
627 (setcdr values
(delete (car values
) (cdr values
)))
628 (setq values
(cdr values
)))
632 (let ((result-list (copy-sequence records
)))
633 (setq result-list
(eudc-add-field-to-records
634 (cons (car field
) value
)
636 (setq result
(append result-list result
))
642 (define-derived-mode eudc-mode special-mode
"EUDC"
643 "Major mode used in buffers displaying the results of directory queries.
644 There is no sense in calling this command from a buffer other than
645 one containing the results of a directory query.
647 These are the special commands of EUDC mode:
648 q -- Kill this buffer.
649 f -- Display a form to query the current directory server.
650 n -- Move to next record.
651 p -- Move to previous record.
652 b -- Insert record at point into the BBDB database."
653 (if (not (featurep 'xemacs
))
654 (easy-menu-define eudc-emacs-menu eudc-mode-map
"" (eudc-menu))
655 (setq mode-popup-menu
(eudc-menu))))
659 ;;{{{ High-level interfaces (interactive functions)
661 (defun eudc-customize ()
662 "Customize the EUDC package."
664 (customize-group 'eudc
))
667 (defun eudc-set-server (server protocol
&optional no-save
)
668 "Set the directory server to SERVER using PROTOCOL.
669 Unless NO-SAVE is non-nil, the server is saved as the default
670 server for future sessions."
672 (read-from-minibuffer "Directory Server: ")
673 (intern (completing-read "Protocol: "
674 (mapcar (lambda (elt)
675 (cons (symbol-name elt
)
677 eudc-known-protocols
)))))
678 (unless (or (null protocol
)
680 eudc-supported-protocols
)
681 (load (concat "eudcb-" (symbol-name protocol
)) t
))
682 (error "Unsupported protocol: %s" protocol
))
683 (run-hooks 'eudc-switch-from-server-hook
)
684 (setq eudc-protocol protocol
)
685 (setq eudc-server server
)
686 (eudc-update-local-variables)
687 (run-hooks 'eudc-switch-to-server-hook
)
688 (if (called-interactively-p 'interactive
)
689 (message "Current directory server is now %s (%s)" eudc-server eudc-protocol
))
691 (eudc-save-options)))
694 (defun eudc-get-email (name &optional error
)
695 "Get the email field of NAME from the directory server.
696 If ERROR is non-nil, report an error if there is none."
697 (interactive "sName: \np")
699 (call-interactively 'eudc-set-server
))
700 (let ((result (eudc-query (list (cons 'name name
)) '(email)))
702 (if (null (cdr result
))
703 (setq email
(cl-cdaar result
))
704 (error "Multiple match--use the query form"))
708 (error "No record matching %s" name
)))
712 (defun eudc-get-phone (name &optional error
)
713 "Get the phone field of NAME from the directory server.
714 If ERROR is non-nil, report an error if there is none."
715 (interactive "sName: \np")
717 (call-interactively 'eudc-set-server
))
718 (let ((result (eudc-query (list (cons 'name name
)) '(phone)))
720 (if (null (cdr result
))
721 (setq phone
(cl-cdaar result
))
722 (error "Multiple match--use the query form"))
726 (error "No record matching %s" name
)))
729 (defun eudc-get-attribute-list ()
730 "Return a list of valid attributes for the current server.
731 When called interactively the list is formatted in a dedicated buffer
732 otherwise a list of symbols is returned."
734 (if eudc-list-attributes-function
735 (let ((entries (funcall eudc-list-attributes-function
736 (called-interactively-p 'interactive
))))
738 (if (called-interactively-p 'interactive
)
739 (eudc-display-records entries t
)
741 (error "The %s protocol has no support for listing attributes" eudc-protocol
)))
743 (defun eudc-format-query (words format
)
744 "Use FORMAT to build a EUDC query from WORDS."
750 (while (and words format
)
751 (setq query-alist
(cons (cons (car format
) (car words
))
753 (setq words
(cdr words
)
754 format
(cdr format
)))
755 ;; If the same attribute appears more than once, merge
756 ;; the corresponding values
758 (setq key
(caar query-alist
)
759 val
(cdar query-alist
)
760 cell
(assq key query
))
762 (setcdr cell
(concat (cdr cell
) " " val
))
763 (setq query
(cons (car query-alist
) query
)))
764 (setq query-alist
(cdr query-alist
)))
766 (if eudc-protocol-has-default-query-attributes
767 (mapconcat 'identity words
" ")
768 (list (cons 'name
(mapconcat 'identity words
" ")))))))
770 (defun eudc-extract-n-word-formats (format-list n
)
771 "Extract a list of N-long formats from FORMAT-LIST.
772 If none try N - 1 and so forth."
774 (while (and (null formats
)
778 (mapcar (lambda (format)
789 (defun eudc-expand-inline (&optional replace
)
790 "Query the directory server, and expand the query string before point.
791 The query string consists of the buffer substring from the point back to
792 the preceding comma, colon or beginning of line.
793 The variable `eudc-inline-query-format' controls how to associate the
794 individual inline query words with directory attribute names.
795 After querying the server for the given string, the expansion specified by
796 `eudc-inline-expansion-format' is inserted in the buffer at point.
797 If REPLACE is non-nil, then this expansion replaces the name in the buffer.
798 `eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
799 Multiple servers can be tried with the same query until one finds a match,
800 see `eudc-inline-expansion-servers'"
803 ((eq eudc-inline-expansion-servers
'current-server
)
805 (call-interactively 'eudc-set-server
)))
806 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
808 ;; Allow server to be nil if hotlist is set.
810 (call-interactively 'eudc-set-server
)))
811 ((eq eudc-inline-expansion-servers
'hotlist
)
812 (or eudc-server-hotlist
813 (error "No server in the hotlist")))
815 (error "Wrong value for `eudc-inline-expansion-servers': %S"
816 eudc-inline-expansion-servers
)))
819 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
820 (point-at-bol) 'move
)
821 (goto-char (match-end 0)))
823 (query-words (split-string (buffer-substring-no-properties beg end
)
829 (eudc-former-server eudc-server
)
830 (eudc-former-protocol eudc-protocol
)
833 ;; Prepare the list of servers to query
834 (setq servers
(copy-sequence eudc-server-hotlist
))
837 ((eq eudc-inline-expansion-servers
'hotlist
)
839 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
841 (cons (cons eudc-server eudc-protocol
)
842 (delete (cons eudc-server eudc-protocol
) servers
))
843 eudc-server-hotlist
))
844 ((eq eudc-inline-expansion-servers
'current-server
)
845 (list (cons eudc-server eudc-protocol
)))))
846 (if (and eudc-max-servers-to-query
847 (> (length servers
) eudc-max-servers-to-query
))
848 (setcdr (nthcdr (1- eudc-max-servers-to-query
) servers
) nil
))
854 ;; Loop on the servers
856 (eudc-set-server (caar servers
) (cdar servers
) t
)
858 ;; Determine which formats apply in the query-format list
861 (eudc-extract-n-word-formats eudc-inline-query-format
862 (length query-words
))
863 (if (null eudc-protocol-has-default-query-attributes
)
866 ;; Loop on query-formats
870 (eudc-format-query query-words
(car query-formats
))
871 (eudc-translate-attribute-list
872 (cdr eudc-inline-expansion-format
))))
874 (throw 'found response
))
875 (setq query-formats
(cdr query-formats
)))
876 (setq servers
(cdr servers
)))
877 ;; No more servers to try... no match found
884 ;; Process response through eudc-inline-expansion-format
886 (setq response-string
888 (car eudc-inline-expansion-format
)
891 (or (cdr (assq field
(car response
)))
893 (eudc-translate-attribute-list
894 (cdr eudc-inline-expansion-format
)))))
895 (if (> (length response-string
) 0)
896 (setq response-strings
897 (cons response-string response-strings
)))
898 (setq response
(cdr response
)))
901 (and replace
(not eudc-expansion-overwrites-query
))
902 (and (not replace
) eudc-expansion-overwrites-query
))
903 (kill-ring-save beg end
))
905 ((or (= (length response-strings
) 1)
906 (null eudc-multiple-match-handling-method
)
907 (eq eudc-multiple-match-handling-method
'first
))
908 (delete-region beg end
)
909 (insert (car response-strings
)))
910 ((eq eudc-multiple-match-handling-method
'select
)
911 (eudc-select response-strings beg end
))
912 ((eq eudc-multiple-match-handling-method
'all
)
913 (delete-region beg end
)
914 (insert (mapconcat 'identity response-strings
", ")))
915 ((eq eudc-multiple-match-handling-method
'abort
)
916 (error "There is more than one match for the query")))))
917 (or (and (equal eudc-server eudc-former-server
)
918 (equal eudc-protocol eudc-former-protocol
))
919 (eudc-set-server eudc-former-server eudc-former-protocol t
)))))
922 (defun eudc-query-form (&optional get-fields-from-server
)
923 "Display a form to query the directory server.
924 If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
925 queries the server for the existing fields and displays a corresponding form."
927 (let ((fields (or (and get-fields-from-server
928 (eudc-get-attribute-list))
929 eudc-query-form-attributes
))
930 (buffer (get-buffer-create "*Directory Query Form*"))
936 (switch-to-buffer buffer
)
937 (setq inhibit-read-only t
)
939 (kill-all-local-variables)
940 (make-local-variable 'eudc-form-widget-list
)
941 (widget-insert "Directory Query Form\n")
942 (widget-insert "====================\n\n")
943 (widget-insert "Current server is: " (or eudc-server
945 (call-interactively 'eudc-set-server
)
948 (widget-insert "Protocol : " (symbol-name eudc-protocol
) "\n")
949 ;; Build the list of prompts
950 (setq prompts
(if eudc-use-raw-directory-names
951 (mapcar 'symbol-name
(eudc-translate-attribute-list fields
))
954 (or (and (assq field eudc-user-attribute-names-alist
)
955 (cdr (assq field eudc-user-attribute-names-alist
)))
956 (capitalize (symbol-name field
)))))
958 ;; Loop over prompt strings to find the longest one
961 (if (> (length prompt
) width
)
962 (setq width
(length prompt
)))))
964 ;; Insert the first widget out of the mapcar to leave the cursor
965 ;; in the first field
966 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
968 (setq widget
(widget-create 'editable-field
:size
15))
969 (setq eudc-form-widget-list
(cons (cons (car fields
) widget
)
970 eudc-form-widget-list
))
971 (setq fields
(cdr fields
))
972 (setq prompts
(cdr prompts
))
975 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
976 (setq widget
(widget-create 'editable-field
978 (setq eudc-form-widget-list
(cons (cons field widget
)
979 eudc-form-widget-list
))
980 (setq prompts
(cdr prompts
))))
982 (widget-insert "\n\n")
983 (widget-create 'push-button
984 :notify
(lambda (&rest _ignore
)
988 (widget-create 'push-button
989 :notify
(lambda (&rest _ignore
)
993 (widget-create 'push-button
994 :notify
(lambda (&rest _ignore
)
998 (use-local-map widget-keymap
)
1002 (defun eudc-bookmark-server (server protocol
)
1003 "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
1004 (interactive "sDirectory server: \nsProtocol: ")
1005 (if (member (cons server protocol
) eudc-server-hotlist
)
1006 (error "%s:%s is already in the hotlist" protocol server
)
1007 (setq eudc-server-hotlist
(cons (cons server protocol
) eudc-server-hotlist
))
1009 (eudc-save-options)))
1011 (defun eudc-bookmark-current-server ()
1012 "Add current server to the EUDC `servers' hotlist."
1014 (eudc-bookmark-server eudc-server eudc-protocol
))
1016 (defun eudc-save-options ()
1017 "Save options to `eudc-options-file'."
1019 (with-current-buffer (find-file-noselect eudc-options-file t
)
1020 (goto-char (point-min))
1021 ;; delete the previous setq
1022 (let ((standard-output (current-buffer))
1028 (let ((sexp (condition-case nil
1029 (read (current-buffer))
1030 (end-of-file (throw 'found nil
)))))
1033 ((eq (car sexp
) 'eudc-set-server
)
1034 (delete-region (save-excursion
1038 (setq set-server-p t
))
1039 ((and (eq (car sexp
) 'setq
)
1040 (eq (cadr sexp
) 'eudc-server-hotlist
))
1041 (delete-region (save-excursion
1045 (setq set-hotlist-p t
))
1046 ((and (eq (car sexp
) 'provide
)
1047 (equal (cadr sexp
) '(quote eudc-options-file
)))
1048 (setq provide-p t
)))
1052 (throw 'found t
))))))
1053 (if (eq (point-min) (point-max))
1054 (princ ";; This file was automatically generated by eudc.el.\n\n"))
1056 (princ "(provide 'eudc-options-file)\n"))
1059 (delete-blank-lines)
1060 (princ "(eudc-set-server ")
1063 (prin1 eudc-protocol
)
1065 (princ "(setq eudc-server-hotlist '")
1066 (prin1 eudc-server-hotlist
)
1070 (defun eudc-move-to-next-record ()
1071 "Move to next record, in a buffer displaying directory query results."
1073 (if (not (derived-mode-p 'eudc-mode
))
1074 (error "Not in a EUDC buffer")
1075 (let ((pt (next-overlay-change (point))))
1076 (if (< pt
(point-max))
1078 (error "No more records after point")))))
1080 (defun eudc-move-to-previous-record ()
1081 "Move to previous 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 (previous-overlay-change (point))))
1086 (if (> pt
(point-min))
1088 (error "No more records before point")))))
1092 ;;{{{ Menus and keymaps
1096 (defconst eudc-custom-generated-menu
(cdr (custom-menu-create 'eudc
)))
1098 (defconst eudc-tail-menu
1100 ["Query with Form" eudc-query-form
1101 :help
"Display a form to query the directory server"]
1102 ["Expand Inline Query" eudc-expand-inline
1103 :help
"Query the directory server, and expand the query string before point"]
1104 ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
1105 (and (or (featurep 'bbdb
)
1106 (prog1 (locate-library "bbdb") (message "")))
1107 (overlays-at (point))
1108 (overlay-get (car (overlays-at (point))) 'eudc-record
))
1109 :help
"Insert record at point into the BBDB database"]
1110 ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
1111 (and (derived-mode-p 'eudc-mode
)
1112 (or (featurep 'bbdb
)
1113 (prog1 (locate-library "bbdb") (message ""))))
1114 :help
"Insert all the records returned by a directory query into BBDB"]
1116 ["Get Email" eudc-get-email
1117 :help
"Get the email field of NAME from the directory server"]
1118 ["Get Phone" eudc-get-phone
1119 :help
"Get the phone field of name from the directory server"]
1120 ["List Valid Attribute Names" eudc-get-attribute-list
1121 :help
"Return a list of valid attributes for the current server"]
1123 ,(cons "Customize" eudc-custom-generated-menu
)))
1126 (defconst eudc-server-menu
1128 ["Bookmark Current Server" eudc-bookmark-current-server
1129 :help
"Add current server to the EUDC `servers' hotlist"]
1130 ["Edit Server List" eudc-edit-hotlist
1131 :help
"Edit the hotlist of directory servers in a specialized buffer"]
1132 ["New Server" eudc-set-server
1133 :help
"Set the directory server to SERVER using PROTOCOL"]))
1137 (append '("Directory Search")
1144 (let* ((server (car servspec
))
1145 (protocol (cdr servspec
))
1146 (proto-name (symbol-name protocol
)))
1147 (setq command
(intern (concat "eudc-set-server-"
1151 (if (not (fboundp command
))
1155 (eudc-set-server ,server
(quote ,protocol
))
1156 (message "Selected directory server is now %s (%s)"
1159 (vector (format "%s (%s)" server proto-name
)
1162 :selected
`(equal eudc-server
,server
)))))
1163 eudc-server-hotlist
)
1167 (defun eudc-install-menu ()
1169 ((and (featurep 'xemacs
) (featurep 'menubar
))
1170 (add-submenu '("Tools") (eudc-menu)))
1171 ((not (featurep 'xemacs
))
1173 ((fboundp 'easy-menu-create-menu
)
1176 [menu-bar tools directory-search
]
1177 (cons "Directory Search"
1178 (easy-menu-create-menu "Directory Search" (cdr (eudc-menu))))))
1179 ((fboundp 'easy-menu-add-item
)
1180 (let ((menu (eudc-menu)))
1181 (easy-menu-add-item nil
'("tools") (easy-menu-create-menu (car menu
)
1183 ((fboundp 'easy-menu-create-keymaps
)
1184 (easy-menu-define eudc-menu-map eudc-mode-map
"Directory Client Menu" (eudc-menu))
1187 [menu-bar tools eudc
]
1188 (cons "Directory Search"
1189 (easy-menu-create-keymaps "Directory Search" (cdr (eudc-menu))))))
1191 (error "Unknown version of easymenu"))))
1195 ;;; Load time initializations :
1197 ;;; Load the options file
1198 (if (and (not noninteractive
)
1199 (and (locate-library eudc-options-file
)
1200 (progn (message "") t
)) ; Remove mode line message
1201 (not (featurep 'eudc-options-file
)))
1202 (load eudc-options-file
))
1204 ;;; Install the full menu
1205 (unless (featurep 'infodock
)
1206 (eudc-install-menu))
1209 ;;; The following installs a short menu for EUDC at XEmacs startup.
1212 (defun eudc-load-eudc ()
1213 "Load the Emacs Unified Directory Client.
1214 This does nothing except loading eudc by autoload side-effect."
1220 ((not (featurep 'xemacs
))
1221 (defvar eudc-tools-menu
1222 (let ((map (make-sparse-keymap "Directory Search")))
1223 (define-key map
[phone]
1224 `(menu-item ,(purecopy "Get Phone") eudc-get-phone
1225 :help ,(purecopy "Get the phone field of name from the directory server")))
1226 (define-key map [email]
1227 `(menu-item ,(purecopy "Get Email") eudc-get-email
1228 :help ,(purecopy "Get the email field of NAME from the directory server")))
1229 (define-key map [separator-eudc-email] menu-bar-separator)
1230 (define-key map [expand-inline]
1231 `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
1232 :help ,(purecopy "Query the directory server, and expand the query string before point")))
1233 (define-key map [query]
1234 `(menu-item ,(purecopy "Query with Form") eudc-query-form
1235 :help ,(purecopy "Display a form to query the directory server")))
1236 (define-key map [separator-eudc-query] menu-bar-separator)
1237 (define-key map [new]
1238 `(menu-item ,(purecopy "New Server") eudc-set-server
1239 :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
1240 (define-key map [load]
1241 `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
1242 :help ,(purecopy "Load the Emacs Unified Directory Client")))
1244 (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
1246 (let ((menu '("Directory Search"
1247 ["Load Hotlist of Servers" eudc-load-eudc t]
1248 ["New Server" eudc-set-server t]
1250 ["Query with Form" eudc-query-form t]
1251 ["Expand Inline Query" eudc-expand-inline t]
1253 ["Get Email" eudc-get-email t]
1254 ["Get Phone" eudc-get-phone t])))
1255 (if (not (featurep 'eudc-autoloads))
1256 (if (featurep 'xemacs)
1257 (if (and (featurep 'menubar)
1258 (not (featurep 'infodock)))
1259 (add-submenu '("Tools") menu))
1262 ((fboundp 'easy-menu-add-item)
1263 (easy-menu-add-item nil '("tools")
1264 (easy-menu-create-menu (car menu)
1266 ((fboundp 'easy-menu-create-keymaps)
1269 [menu-bar tools eudc]
1270 (cons "Directory Search"
1271 (easy-menu-create-keymaps "Directory Search"
1272 (cdr menu)))))))))))
1278 ;;; eudc.el ends here