1 ;;; eudc.el --- Emacs Unified Directory Client
3 ;; Copyright (C) 1998-2017 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-current-buffer
)
72 (define-key map
"x" 'kill-current-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 (defvar bbdb-version
)
112 (defun eudc--using-bbdb-3-or-newer-p ()
113 "Return non-nil if BBDB version is 3 or greater."
115 ;; MELPA versions of BBDB may have a bad package version, but
116 ;; they're all version 3 or later.
117 (equal bbdb-version
"@PACKAGE_VERSION@")
118 ;; Development versions of BBDB can have the format "X.YZ devo".
119 ;; Split the string just in case.
120 (version<= "3" (car (split-string bbdb-version
)))))
122 (defun eudc-plist-member (plist prop
)
123 "Return t if PROP has a value specified in PLIST."
124 (if (not (= 0 (%
(length plist
) 2)))
125 (error "Malformed plist"))
128 (if (eq prop
(car plist
))
130 (setq plist
(cdr (cdr plist
))))
133 ;; Emacs's plist-get lacks third parameter
134 (defun eudc-plist-get (plist prop
&optional default
)
135 "Extract a value from a property list.
136 PLIST is a property list, which is a list of the form
137 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
138 corresponding to the given PROP, or DEFAULT if PROP is not
139 one of the properties on the list."
140 (if (eudc-plist-member plist prop
)
141 (plist-get plist prop
)
144 (defun eudc-lax-plist-get (plist prop
&optional default
)
145 "Extract a value from a lax property list.
147 PLIST is a lax property list, which is a list of the form (PROP1
148 VALUE1 PROP2 VALUE2...), where comparisons between properties are done
149 using `equal' instead of `eq'. This function returns the value
150 corresponding to PROP, or DEFAULT if PROP is not one of the
151 properties on the list."
152 (if (not (= 0 (%
(length plist
) 2)))
153 (error "Malformed plist"))
156 (if (equal prop
(car plist
))
157 (throw 'found
(car (cdr plist
))))
158 (setq plist
(cdr (cdr plist
))))
161 (if (not (fboundp 'split-string
))
162 (defun split-string (string &optional pattern
)
163 "Return a list of substrings of STRING which are separated by PATTERN.
164 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
166 (setq pattern
"[ \f\t\n\r\v]+"))
167 (let (parts (start 0))
168 (when (string-match pattern string
0)
169 (if (> (match-beginning 0) 0)
170 (setq parts
(cons (substring string
0 (match-beginning 0)) nil
)))
171 (setq start
(match-end 0))
172 (while (and (string-match pattern string start
)
173 (> (match-end 0) start
))
174 (setq parts
(cons (substring string start
(match-beginning 0)) parts
)
175 start
(match-end 0))))
176 (nreverse (if (< start
(length string
))
177 (cons (substring string start
) parts
)
180 (defun eudc-replace-in-string (str regexp newtext
)
181 "Replace all matches in STR for REGEXP with NEWTEXT.
182 Value is the new string."
186 (while (setq match
(string-match regexp str start
))
187 (setq prev-start start
191 (substring str prev-start match
)
193 (concat rtn-str
(substring str start
))))
197 ;;{{{ Server and Protocol Variable Routines
199 (defun eudc-server-local-variable-p (var)
200 "Return non-nil if VAR has server-local bindings."
201 (eudc-plist-member (get var
'eudc-locals
) 'server
))
203 (defun eudc-protocol-local-variable-p (var)
204 "Return non-nil if VAR has protocol-local bindings."
205 (eudc-plist-member (get var
'eudc-locals
) 'protocol
))
207 (defun eudc-default-set (var val
)
208 "Set the EUDC default value of VAR to VAL.
209 The current binding of VAR is not changed."
210 (put var
'eudc-locals
211 (plist-put (get var
'eudc-locals
) 'default val
))
212 (add-to-list 'eudc-local-vars var
))
214 (defun eudc-protocol-set (var val
&optional protocol
)
215 "Set the PROTOCOL-local binding of VAR to VAL.
216 If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
217 The current binding of VAR is changed only if PROTOCOL is omitted."
218 (if (eq 'unbound
(eudc-variable-default-value var
))
219 (eudc-default-set var
(symbol-value var
)))
220 (let* ((eudc-locals (get var
'eudc-locals
))
221 (protocol-locals (eudc-plist-get eudc-locals
'protocol
)))
222 (setq protocol-locals
(plist-put protocol-locals
(or protocol
225 (plist-put eudc-locals
'protocol protocol-locals
))
226 (put var
'eudc-locals eudc-locals
)
227 (add-to-list 'eudc-local-vars var
)
229 (eudc-update-variable var
))))
231 (defun eudc-server-set (var val
&optional server
)
232 "Set the SERVER-local binding of VAR to VAL.
233 If omitted SERVER defaults to the current value of `eudc-server'.
234 The current binding of VAR is changed only if SERVER is omitted."
235 (if (eq 'unbound
(eudc-variable-default-value var
))
236 (eudc-default-set var
(symbol-value var
)))
237 (let* ((eudc-locals (get var
'eudc-locals
))
238 (server-locals (eudc-plist-get eudc-locals
'server
)))
239 (setq server-locals
(plist-put server-locals
(or server
242 (plist-put eudc-locals
'server server-locals
))
243 (put var
'eudc-locals eudc-locals
)
244 (add-to-list 'eudc-local-vars var
)
246 (eudc-update-variable var
))))
249 (defun eudc-set (var val
)
250 "Set the most local (server, protocol or default) binding of VAR to VAL.
251 The current binding of VAR is also set to VAL"
253 ((not (eq 'unbound
(eudc-variable-server-value var
)))
254 (eudc-server-set var val
))
255 ((not (eq 'unbound
(eudc-variable-protocol-value var
)))
256 (eudc-protocol-set var val
))
258 (eudc-default-set var val
)))
261 (defun eudc-variable-default-value (var)
262 "Return the default binding of VAR.
263 Return `unbound' if VAR has no EUDC default value."
264 (let ((eudc-locals (get var
'eudc-locals
)))
265 (if (and (boundp var
)
267 (eudc-plist-get eudc-locals
'default
'unbound
)
270 (defun eudc-variable-protocol-value (var &optional protocol
)
271 "Return the value of VAR local to PROTOCOL.
272 Return `unbound' if VAR has no value local to PROTOCOL.
273 PROTOCOL defaults to `eudc-protocol'"
274 (let* ((eudc-locals (get var
'eudc-locals
))
276 (if (not (and (boundp var
)
278 (eudc-plist-member eudc-locals
'protocol
)))
280 (setq protocol-locals
(eudc-plist-get eudc-locals
'protocol
))
281 (eudc-lax-plist-get protocol-locals
283 eudc-protocol
) 'unbound
))))
285 (defun eudc-variable-server-value (var &optional server
)
286 "Return the value of VAR local to SERVER.
287 Return `unbound' if VAR has no value local to SERVER.
288 SERVER defaults to `eudc-server'"
289 (let* ((eudc-locals (get var
'eudc-locals
))
291 (if (not (and (boundp var
)
293 (eudc-plist-member eudc-locals
'server
)))
295 (setq server-locals
(eudc-plist-get eudc-locals
'server
))
296 (eudc-lax-plist-get server-locals
298 eudc-server
) 'unbound
))))
300 (defun eudc-update-variable (var)
301 "Set the value of VAR according to its locals.
302 If the VAR has a server- or protocol-local value corresponding
303 to the current `eudc-server' and `eudc-protocol' then it is set
304 accordingly. Otherwise it is set to its EUDC default binding"
307 ((not (eq 'unbound
(setq val
(eudc-variable-server-value var
))))
309 ((not (eq 'unbound
(setq val
(eudc-variable-protocol-value var
))))
311 ((not (eq 'unbound
(setq val
(eudc-variable-default-value var
))))
314 (defun eudc-update-local-variables ()
315 "Update all EUDC variables according to their local settings."
317 (mapcar 'eudc-update-variable eudc-local-vars
))
319 (eudc-default-set 'eudc-query-function nil
)
320 (eudc-default-set 'eudc-list-attributes-function nil
)
321 (eudc-default-set 'eudc-protocol-attributes-translation-alist nil
)
322 (eudc-default-set 'eudc-bbdb-conversion-alist nil
)
323 (eudc-default-set 'eudc-switch-to-server-hook nil
)
324 (eudc-default-set 'eudc-switch-from-server-hook nil
)
325 (eudc-default-set 'eudc-protocol-has-default-query-attributes nil
)
326 (eudc-default-set 'eudc-attribute-display-method-alist nil
)
331 ;; Add PROTOCOL to the list of supported protocols
332 (defun eudc-register-protocol (protocol)
333 (unless (memq protocol eudc-supported-protocols
)
334 (setq eudc-supported-protocols
335 (cons protocol eudc-supported-protocols
))
336 (put 'eudc-protocol
'custom-type
337 `(choice :menu-tag
"Protocol"
338 ,@(mapcar (lambda (s)
339 (list 'string
':tag
(symbol-name s
)))
340 eudc-supported-protocols
))))
341 (or (memq protocol eudc-known-protocols
)
342 (setq eudc-known-protocols
343 (cons protocol eudc-known-protocols
))))
346 (defun eudc-translate-query (query)
347 "Translate attribute names of QUERY.
348 The translation is done according to
349 `eudc-protocol-attributes-translation-alist'."
350 (if eudc-protocol-attributes-translation-alist
351 (mapcar (lambda (attribute)
352 (let ((trans (assq (car attribute
)
353 (symbol-value eudc-protocol-attributes-translation-alist
))))
355 (cons (cdr trans
) (cdr attribute
))
360 (defun eudc-translate-attribute-list (list)
361 "Translate a list of attribute names LIST.
362 The translation is done according to
363 `eudc-protocol-attributes-translation-alist'."
364 (if eudc-protocol-attributes-translation-alist
366 (mapcar (lambda (attribute)
367 (setq trans
(assq attribute
368 (symbol-value eudc-protocol-attributes-translation-alist
)))
375 (defun eudc-select (choices beg end
)
376 "Choose one from CHOICES using a completion.
377 BEG and END delimit the text which is to be replaced."
380 (completing-read "Multiple matches found; choose one: "
381 (mapcar 'list choices
)))
382 (delete-region beg end
)
383 (insert replacement
)))
385 (defun eudc-query (query &optional return-attributes no-translation
)
386 "Query the current directory server with QUERY.
387 QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
388 name and VALUE the corresponding value.
389 If NO-TRANSLATION is non-nil, ATTR is translated according to
390 `eudc-protocol-attributes-translation-alist'.
391 RETURN-ATTRIBUTES is a list of attributes to return defaulting to
392 `eudc-default-return-attributes'."
393 (unless eudc-query-function
394 (error "Don't know how to perform the query"))
396 (funcall eudc-query-function query
(or return-attributes
397 eudc-default-return-attributes
))
399 (funcall eudc-query-function
400 (eudc-translate-query query
)
403 (eudc-translate-attribute-list return-attributes
))
404 ((listp eudc-default-return-attributes
)
405 (eudc-translate-attribute-list eudc-default-return-attributes
))
407 eudc-default-return-attributes
)))))
409 (defun eudc-format-attribute-name-for-display (attribute)
410 "Format a directory attribute name for display.
411 ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
412 by the corresponding user name if any. Otherwise it is capitalized and
413 underscore characters are replaced by spaces."
414 (let ((match (assq attribute eudc-user-attribute-names-alist
)))
419 (split-string (symbol-name attribute
) "_")
422 (defun eudc-print-attribute-value (field)
423 "Insert the value of the directory FIELD at point.
424 The directory attribute name in car of FIELD is looked up in
425 `eudc-attribute-display-method-alist' and the corresponding method,
426 if any, is called to print the value in cdr of FIELD."
427 (let ((match (assoc (downcase (car field
))
428 eudc-attribute-display-method-alist
))
429 (col (current-column))
433 (eval (list (cdr match
) val
))
439 (insert val-elem
"\n")))
442 ((stringp val
) (split-string val
"\n"))
446 (defun eudc-print-record-field (field column-width
)
447 "Print the record field FIELD.
448 FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
449 COLUMN-WIDTH is the width of the first display column containing the
450 attribute name ATTR."
451 (let ((field-beg (point)))
452 ;; The record field that is passed to this function has already been processed
453 ;; by `eudc-format-attribute-name-for-display' so we don't need to call it
454 ;; again to display the attribute name
455 (insert (format (concat "%" (int-to-string column-width
) "s: ")
457 (put-text-property field-beg
(point) 'face
'bold
)
458 (indent-to (+ 2 column-width
))
459 (eudc-print-attribute-value field
)))
461 (defun eudc-display-records (records &optional raw-attr-names
)
462 "Display the record list RECORDS in a formatted buffer.
463 If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
464 otherwise they are formatted according to `eudc-user-attribute-names-alist'."
465 (let (inhibit-read-only
471 (with-output-to-temp-buffer "*Directory Query Results*"
472 (with-current-buffer standard-output
473 (setq buffer-read-only t
)
474 (setq inhibit-read-only t
)
476 (insert "Directory Query Result\n")
477 (insert "======================\n\n\n")
479 (insert "No match found.\n"
480 (if eudc-strict-return-matches
481 "Try setting `eudc-strict-return-matches' to nil or change `eudc-default-return-attributes'.\n"
483 ;; Replace field names with user names, compute max width
493 (symbol-name (car field
))
494 (eudc-format-attribute-name-for-display (car field
))))
495 (if (> (length attribute-name
) width
)
496 (setq width
(length attribute-name
)))
497 (cons attribute-name
(cdr field
))))
500 ;; Display the records
501 (setq first-record
(point))
506 ;; Map over the record fields to print the attribute/value pairs
509 (eudc-print-record-field field width
)))
511 ;; Store the record internal format in some convenient place
512 (overlay-put (make-overlay beg
(point))
515 (setq records
(cdr records
))
519 (widget-create 'push-button
520 :notify
(lambda (&rest _ignore
)
524 (widget-create 'push-button
525 :notify
(lambda (&rest _ignore
)
531 (goto-char first-record
))))))
533 (defun eudc-process-form ()
534 "Process the query form in current buffer and display the results."
537 (if (not (and (boundp 'eudc-form-widget-list
)
538 eudc-form-widget-list
))
539 (error "Not in a directory query form buffer")
542 (setq value
(widget-value (cdr wid-field
)))
543 (if (not (string= value
""))
544 (setq query-alist
(cons (cons (car wid-field
) value
)
546 eudc-form-widget-list
)
547 (kill-buffer (current-buffer))
548 (eudc-display-records (eudc-query query-alist
) eudc-use-raw-directory-names
))))
551 (defun eudc-filter-duplicate-attributes (record)
552 "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
558 ;; Search for multiple records
560 (not (listp (cdar rec
))))
561 (setq rec
(cdr rec
)))
563 (if (null (cdar rec
))
564 (list record
) ; No duplicate attrs in this record
567 (if (listp (cdr field
))
568 (setq duplicates
(cons field duplicates
))
569 (setq unique
(cons field unique
)))))
571 (setq result
(list unique
))
572 ;; Map over the record fields that have multiple values
576 (let ((method (if (consp eudc-duplicate-attribute-handling-method
)
584 eudc-protocol-attributes-translation-alist
)))
586 eudc-duplicate-attribute-handling-method
))
587 eudc-duplicate-attribute-handling-method
)))
589 ((or (null method
) (eq 'list method
))
591 (eudc-add-field-to-records field result
)))
594 (eudc-add-field-to-records (cons (car field
)
599 (eudc-add-field-to-records (cons (car field
)
604 ((eq 'duplicate method
)
606 (eudc-distribute-field-on-records field result
)))))))
610 (defun eudc-filter-partial-records (records attrs
)
611 "Eliminate records that do not contain all ATTRS from RECORDS."
620 (consp (assq attr rec
))))
625 (defun eudc-add-field-to-records (field records
)
626 "Add FIELD to each individual record in RECORDS and return the resulting list."
632 (defun eudc-distribute-field-on-records (field records
)
633 "Duplicate each individual record in RECORDS according to value of FIELD.
634 Each copy is added a new field containing one of the values of FIELD."
636 (values (cdr field
)))
637 ;; Uniquify values first
639 (setcdr values
(delete (car values
) (cdr values
)))
640 (setq values
(cdr values
)))
644 (let ((result-list (copy-sequence records
)))
645 (setq result-list
(eudc-add-field-to-records
646 (cons (car field
) value
)
648 (setq result
(append result-list result
))
654 (define-derived-mode eudc-mode special-mode
"EUDC"
655 "Major mode used in buffers displaying the results of directory queries.
656 There is no sense in calling this command from a buffer other than
657 one containing the results of a directory query.
659 These are the special commands of EUDC mode:
660 q -- Kill this buffer.
661 f -- Display a form to query the current directory server.
662 n -- Move to next record.
663 p -- Move to previous record.
664 b -- Insert record at point into the BBDB database."
665 (if (not (featurep 'xemacs
))
666 (easy-menu-define eudc-emacs-menu eudc-mode-map
"" (eudc-menu))
667 (setq mode-popup-menu
(eudc-menu))))
671 ;;{{{ High-level interfaces (interactive functions)
673 (defun eudc-customize ()
674 "Customize the EUDC package."
676 (customize-group 'eudc
))
679 (defun eudc-set-server (server protocol
&optional no-save
)
680 "Set the directory server to SERVER using PROTOCOL.
681 Unless NO-SAVE is non-nil, the server is saved as the default
682 server for future sessions."
684 (read-from-minibuffer "Directory Server: ")
685 (intern (completing-read "Protocol: "
686 (mapcar (lambda (elt)
687 (cons (symbol-name elt
)
689 eudc-known-protocols
)))))
690 (unless (or (null 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
(cl-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
(cl-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
770 (setq key
(caar query-alist
)
771 val
(cdar query-alist
)
772 cell
(assq key query
))
774 (setcdr cell
(concat (cdr cell
) " " val
))
775 (setq query
(cons (car query-alist
) query
)))
776 (setq query-alist
(cdr query-alist
)))
778 (if eudc-protocol-has-default-query-attributes
779 (mapconcat 'identity words
" ")
780 (list (cons 'name
(mapconcat 'identity words
" ")))))))
782 (defun eudc-extract-n-word-formats (format-list n
)
783 "Extract a list of N-long formats from FORMAT-LIST.
784 If none try N - 1 and so forth."
786 (while (and (null formats
)
790 (mapcar (lambda (format)
801 (defun eudc-expand-inline (&optional replace
)
802 "Query the directory server, and expand the query string before point.
803 The query string consists of the buffer substring from the point back to
804 the preceding comma, colon or beginning of line.
805 The variable `eudc-inline-query-format' controls how to associate the
806 individual inline query words with directory attribute names.
807 After querying the server for the given string, the expansion specified by
808 `eudc-inline-expansion-format' is inserted in the buffer at point.
809 If REPLACE is non-nil, then this expansion replaces the name in the buffer.
810 `eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
811 Multiple servers can be tried with the same query until one finds a match,
812 see `eudc-inline-expansion-servers'"
815 ((eq eudc-inline-expansion-servers
'current-server
)
817 (call-interactively 'eudc-set-server
)))
818 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
820 ;; Allow server to be nil if hotlist is set.
822 (call-interactively 'eudc-set-server
)))
823 ((eq eudc-inline-expansion-servers
'hotlist
)
824 (or eudc-server-hotlist
825 (error "No server in the hotlist")))
827 (error "Wrong value for `eudc-inline-expansion-servers': %S"
828 eudc-inline-expansion-servers
)))
831 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
832 (point-at-bol) 'move
)
833 (goto-char (match-end 0)))
835 (query-words (split-string (buffer-substring-no-properties beg end
)
841 (eudc-former-server eudc-server
)
842 (eudc-former-protocol eudc-protocol
)
845 ;; Prepare the list of servers to query
846 (setq servers
(copy-sequence eudc-server-hotlist
))
849 ((eq eudc-inline-expansion-servers
'hotlist
)
851 ((eq eudc-inline-expansion-servers
'server-then-hotlist
)
853 (cons (cons eudc-server eudc-protocol
)
854 (delete (cons eudc-server eudc-protocol
) servers
))
855 eudc-server-hotlist
))
856 ((eq eudc-inline-expansion-servers
'current-server
)
857 (list (cons eudc-server eudc-protocol
)))))
858 (if (and eudc-max-servers-to-query
859 (> (length servers
) eudc-max-servers-to-query
))
860 (setcdr (nthcdr (1- eudc-max-servers-to-query
) servers
) nil
))
866 ;; Loop on the servers
868 (eudc-set-server (caar servers
) (cdar servers
) t
)
870 ;; Determine which formats apply in the query-format list
873 (eudc-extract-n-word-formats eudc-inline-query-format
874 (length query-words
))
875 (if (null eudc-protocol-has-default-query-attributes
)
878 ;; Loop on query-formats
882 (eudc-format-query query-words
(car query-formats
))
883 (eudc-translate-attribute-list
884 (cdr eudc-inline-expansion-format
))))
886 (throw 'found response
))
887 (setq query-formats
(cdr query-formats
)))
888 (setq servers
(cdr servers
)))
889 ;; No more servers to try... no match found
896 ;; Process response through eudc-inline-expansion-format
898 (setq response-string
900 (car eudc-inline-expansion-format
)
903 (or (cdr (assq field
(car response
)))
905 (eudc-translate-attribute-list
906 (cdr eudc-inline-expansion-format
)))))
907 (if (> (length response-string
) 0)
908 (setq response-strings
909 (cons response-string response-strings
)))
910 (setq response
(cdr response
)))
913 (and replace
(not eudc-expansion-overwrites-query
))
914 (and (not replace
) eudc-expansion-overwrites-query
))
915 (kill-ring-save beg end
))
917 ((or (= (length response-strings
) 1)
918 (null eudc-multiple-match-handling-method
)
919 (eq eudc-multiple-match-handling-method
'first
))
920 (delete-region beg end
)
921 (insert (car response-strings
)))
922 ((eq eudc-multiple-match-handling-method
'select
)
923 (eudc-select response-strings beg end
))
924 ((eq eudc-multiple-match-handling-method
'all
)
925 (delete-region beg end
)
926 (insert (mapconcat 'identity response-strings
", ")))
927 ((eq eudc-multiple-match-handling-method
'abort
)
928 (error "There is more than one match for the query")))))
929 (or (and (equal eudc-server eudc-former-server
)
930 (equal eudc-protocol eudc-former-protocol
))
931 (eudc-set-server eudc-former-server eudc-former-protocol t
)))))
934 (defun eudc-query-form (&optional get-fields-from-server
)
935 "Display a form to query the directory server.
936 If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
937 queries the server for the existing fields and displays a corresponding form."
939 (let ((fields (or (and get-fields-from-server
940 (eudc-get-attribute-list))
941 eudc-query-form-attributes
))
942 (buffer (get-buffer-create "*Directory Query Form*"))
948 (switch-to-buffer buffer
)
949 (setq inhibit-read-only t
)
951 (kill-all-local-variables)
952 (make-local-variable 'eudc-form-widget-list
)
953 (widget-insert "Directory Query Form\n")
954 (widget-insert "====================\n\n")
955 (widget-insert "Current server is: " (or eudc-server
957 (call-interactively 'eudc-set-server
)
960 (widget-insert "Protocol : " (symbol-name eudc-protocol
) "\n")
961 ;; Build the list of prompts
962 (setq prompts
(if eudc-use-raw-directory-names
963 (mapcar 'symbol-name
(eudc-translate-attribute-list fields
))
966 (or (and (assq field eudc-user-attribute-names-alist
)
967 (cdr (assq field eudc-user-attribute-names-alist
)))
968 (capitalize (symbol-name field
)))))
970 ;; Loop over prompt strings to find the longest one
973 (if (> (length prompt
) width
)
974 (setq width
(length prompt
)))))
976 ;; Insert the first widget out of the mapcar to leave the cursor
977 ;; in the first field
978 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
980 (setq widget
(widget-create 'editable-field
:size
15))
981 (setq eudc-form-widget-list
(cons (cons (car fields
) widget
)
982 eudc-form-widget-list
))
983 (setq fields
(cdr fields
))
984 (setq prompts
(cdr prompts
))
987 (widget-insert "\n\n" (format (concat "%" (int-to-string width
) "s: ") (car prompts
)))
988 (setq widget
(widget-create 'editable-field
990 (setq eudc-form-widget-list
(cons (cons field widget
)
991 eudc-form-widget-list
))
992 (setq prompts
(cdr prompts
))))
994 (widget-insert "\n\n")
995 (widget-create 'push-button
996 :notify
(lambda (&rest _ignore
)
1000 (widget-create 'push-button
1001 :notify
(lambda (&rest _ignore
)
1005 (widget-create 'push-button
1006 :notify
(lambda (&rest _ignore
)
1010 (use-local-map widget-keymap
)
1014 (defun eudc-bookmark-server (server protocol
)
1015 "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
1016 (interactive "sDirectory server: \nsProtocol: ")
1017 (if (member (cons server protocol
) eudc-server-hotlist
)
1018 (error "%s:%s is already in the hotlist" protocol server
)
1019 (setq eudc-server-hotlist
(cons (cons server protocol
) eudc-server-hotlist
))
1021 (eudc-save-options)))
1023 (defun eudc-bookmark-current-server ()
1024 "Add current server to the EUDC `servers' hotlist."
1026 (eudc-bookmark-server eudc-server eudc-protocol
))
1028 (defun eudc-save-options ()
1029 "Save options to `eudc-options-file'."
1031 (with-current-buffer (find-file-noselect eudc-options-file t
)
1032 (goto-char (point-min))
1033 ;; delete the previous setq
1034 (let ((standard-output (current-buffer))
1040 (let ((sexp (condition-case nil
1041 (read (current-buffer))
1042 (end-of-file (throw 'found nil
)))))
1045 ((eq (car sexp
) 'eudc-set-server
)
1046 (delete-region (save-excursion
1050 (setq set-server-p t
))
1051 ((and (eq (car sexp
) 'setq
)
1052 (eq (cadr sexp
) 'eudc-server-hotlist
))
1053 (delete-region (save-excursion
1057 (setq set-hotlist-p t
))
1058 ((and (eq (car sexp
) 'provide
)
1059 (equal (cadr sexp
) '(quote eudc-options-file
)))
1060 (setq provide-p t
)))
1064 (throw 'found t
))))))
1065 (if (eq (point-min) (point-max))
1066 (princ ";; This file was automatically generated by eudc.el.\n\n"))
1068 (princ "(provide 'eudc-options-file)\n"))
1071 (delete-blank-lines)
1072 (princ "(eudc-set-server ")
1075 (prin1 eudc-protocol
)
1077 (princ "(setq eudc-server-hotlist '")
1078 (prin1 eudc-server-hotlist
)
1082 (defun eudc-move-to-next-record ()
1083 "Move to next record, in a buffer displaying directory query results."
1085 (if (not (derived-mode-p 'eudc-mode
))
1086 (error "Not in a EUDC buffer")
1087 (let ((pt (next-overlay-change (point))))
1088 (if (< pt
(point-max))
1090 (error "No more records after point")))))
1092 (defun eudc-move-to-previous-record ()
1093 "Move to previous record, in a buffer displaying directory query results."
1095 (if (not (derived-mode-p 'eudc-mode
))
1096 (error "Not in a EUDC buffer")
1097 (let ((pt (previous-overlay-change (point))))
1098 (if (> pt
(point-min))
1100 (error "No more records before point")))))
1104 ;;{{{ Menus and keymaps
1108 (defconst eudc-custom-generated-menu
(cdr (custom-menu-create 'eudc
)))
1110 (defconst eudc-tail-menu
1112 ["Query with Form" eudc-query-form
1113 :help
"Display a form to query the directory server"]
1114 ["Expand Inline Query" eudc-expand-inline
1115 :help
"Query the directory server, and expand the query string before point"]
1116 ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
1117 (and (or (featurep 'bbdb
)
1118 (prog1 (locate-library "bbdb") (message "")))
1119 (overlays-at (point))
1120 (overlay-get (car (overlays-at (point))) 'eudc-record
))
1121 :help
"Insert record at point into the BBDB database"]
1122 ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
1123 (and (derived-mode-p 'eudc-mode
)
1124 (or (featurep 'bbdb
)
1125 (prog1 (locate-library "bbdb") (message ""))))
1126 :help
"Insert all the records returned by a directory query into BBDB"]
1128 ["Get Email" eudc-get-email
1129 :help
"Get the email field of NAME from the directory server"]
1130 ["Get Phone" eudc-get-phone
1131 :help
"Get the phone field of name from the directory server"]
1132 ["List Valid Attribute Names" eudc-get-attribute-list
1133 :help
"Return a list of valid attributes for the current server"]
1135 ,(cons "Customize" eudc-custom-generated-menu
)))
1138 (defconst eudc-server-menu
1140 ["Bookmark Current Server" eudc-bookmark-current-server
1141 :help
"Add current server to the EUDC `servers' hotlist"]
1142 ["Edit Server List" eudc-edit-hotlist
1143 :help
"Edit the hotlist of directory servers in a specialized buffer"]
1144 ["New Server" eudc-set-server
1145 :help
"Set the directory server to SERVER using PROTOCOL"]))
1149 (append '("Directory Servers")
1156 (let* ((server (car servspec
))
1157 (protocol (cdr servspec
))
1158 (proto-name (symbol-name protocol
)))
1159 (setq command
(intern (concat "eudc-set-server-"
1163 (if (not (fboundp command
))
1167 (eudc-set-server ,server
(quote ,protocol
))
1168 (message "Selected directory server is now %s (%s)"
1171 (vector (format "%s (%s)" server proto-name
)
1174 :selected
`(equal eudc-server
,server
)))))
1175 eudc-server-hotlist
)
1179 (defun eudc-install-menu ()
1181 ((and (featurep 'xemacs
) (featurep 'menubar
))
1182 (add-submenu '("Tools") (eudc-menu)))
1183 ((not (featurep 'xemacs
))
1185 ((fboundp 'easy-menu-create-menu
)
1188 [menu-bar tools directory-search
]
1189 (cons "Directory Servers"
1190 (easy-menu-create-menu "Directory Servers" (cdr (eudc-menu))))))
1191 ((fboundp 'easy-menu-add-item
)
1192 (let ((menu (eudc-menu)))
1193 (easy-menu-add-item nil
'("tools") (easy-menu-create-menu (car menu
)
1195 ((fboundp 'easy-menu-create-keymaps
)
1196 (easy-menu-define eudc-menu-map eudc-mode-map
"Directory Client Menu" (eudc-menu))
1199 [menu-bar tools eudc
]
1200 (cons "Directory Servers"
1201 (easy-menu-create-keymaps "Directory Servers"
1202 (cdr (eudc-menu))))))
1204 (error "Unknown version of easymenu"))))
1208 ;;; Load time initializations :
1210 ;;; Load the options file
1211 (if (and (not noninteractive
)
1212 (and (locate-library eudc-options-file
)
1213 (progn (message "") t
)) ; Remove mode line message
1214 (not (featurep 'eudc-options-file
)))
1215 (load eudc-options-file
))
1217 ;;; Install the full menu
1218 (unless (featurep 'infodock
)
1219 (eudc-install-menu))
1222 ;;; The following installs a short menu for EUDC at XEmacs startup.
1225 (defun eudc-load-eudc ()
1226 "Load the Emacs Unified Directory Client.
1227 This does nothing except loading eudc by autoload side-effect."
1233 ((not (featurep 'xemacs
))
1234 (defvar eudc-tools-menu
1235 (let ((map (make-sparse-keymap "Directory Servers")))
1236 (define-key map
[phone]
1237 `(menu-item ,(purecopy "Get Phone") eudc-get-phone
1238 :help ,(purecopy "Get the phone field of name from the directory server")))
1239 (define-key map [email]
1240 `(menu-item ,(purecopy "Get Email") eudc-get-email
1241 :help ,(purecopy "Get the email field of NAME from the directory server")))
1242 (define-key map [separator-eudc-email] menu-bar-separator)
1243 (define-key map [expand-inline]
1244 `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
1245 :help ,(purecopy "Query the directory server, and expand the query string before point")))
1246 (define-key map [query]
1247 `(menu-item ,(purecopy "Query with Form") eudc-query-form
1248 :help ,(purecopy "Display a form to query the directory server")))
1249 (define-key map [separator-eudc-query] menu-bar-separator)
1250 (define-key map [new]
1251 `(menu-item ,(purecopy "New Server") eudc-set-server
1252 :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
1253 (define-key map [load]
1254 `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
1255 :help ,(purecopy "Load the Emacs Unified Directory Client")))
1257 (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
1259 (let ((menu '("Directory Servers"
1260 ["Load Hotlist of Servers" eudc-load-eudc t]
1261 ["New Server" eudc-set-server t]
1263 ["Query with Form" eudc-query-form t]
1264 ["Expand Inline Query" eudc-expand-inline t]
1266 ["Get Email" eudc-get-email t]
1267 ["Get Phone" eudc-get-phone t])))
1268 (if (not (featurep 'eudc-autoloads))
1269 (if (featurep 'xemacs)
1270 (if (and (featurep 'menubar)
1271 (not (featurep 'infodock)))
1272 (add-submenu '("Tools") menu))
1275 ((fboundp 'easy-menu-add-item)
1276 (easy-menu-add-item nil '("tools")
1277 (easy-menu-create-menu (car menu)
1279 ((fboundp 'easy-menu-create-keymaps)
1282 [menu-bar tools eudc]
1283 (cons "Directory Servers"
1284 (easy-menu-create-keymaps "Directory Servers"
1285 (cdr menu)))))))))))
1291 ;;; eudc.el ends here