eudc.el 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. ;;; eudc.el --- Emacs Unified Directory Client
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Oscar Figueiredo <oscar@cpe.fr>
  4. ;; Maintainer: Pavel Janík <Pavel@Janik.cz>
  5. ;; Keywords: comm
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This package provides a common interface to query directory servers using
  19. ;; different protocols such as LDAP, CCSO PH/QI or BBDB. Queries can be
  20. ;; made through an interactive form or inline. Inline query strings in
  21. ;; buffers are expanded with appropriately formatted query results
  22. ;; (especially used to expand email addresses in message buffers). EUDC
  23. ;; also interfaces with the BBDB package to let you register query results
  24. ;; into your own BBDB database.
  25. ;;; Usage:
  26. ;; EUDC comes with an extensive documentation, please refer to it.
  27. ;;
  28. ;; The main entry points of EUDC are:
  29. ;; `eudc-query-form': Query a directory server from a query form
  30. ;; `eudc-expand-inline': Query a directory server for the e-mail address
  31. ;; of the name before cursor and insert it in the
  32. ;; buffer
  33. ;; `eudc-get-phone': Get a phone number from a directory server
  34. ;; `eudc-get-email': Get an e-mail address from a directory server
  35. ;; `eudc-customize': Customize various aspects of EUDC
  36. ;;; Code:
  37. (require 'wid-edit)
  38. (eval-and-compile
  39. (if (not (fboundp 'make-overlay))
  40. (require 'overlay))
  41. (if (not (fboundp 'unless))
  42. (require 'cl)))
  43. (unless (fboundp 'custom-menu-create)
  44. (autoload 'custom-menu-create "cus-edit"))
  45. (require 'eudc-vars)
  46. ;;{{{ Internal cooking
  47. ;;{{{ Internal variables and compatibility tricks
  48. (defvar eudc-form-widget-list nil)
  49. (defvar eudc-mode-map
  50. (let ((map (make-sparse-keymap)))
  51. (define-key map "q" 'kill-this-buffer)
  52. (define-key map "x" 'kill-this-buffer)
  53. (define-key map "f" 'eudc-query-form)
  54. (define-key map "b" 'eudc-try-bbdb-insert)
  55. (define-key map "n" 'eudc-move-to-next-record)
  56. (define-key map "p" 'eudc-move-to-previous-record)
  57. map))
  58. (set-keymap-parent eudc-mode-map widget-keymap)
  59. (defvar mode-popup-menu)
  60. ;; List of known servers
  61. ;; Alist of (SERVER . PROTOCOL)
  62. (defvar eudc-server-hotlist nil)
  63. ;; List of variables that have server- or protocol-local bindings
  64. (defvar eudc-local-vars nil)
  65. ;; Protocol local. Query function
  66. (defvar eudc-query-function nil)
  67. ;; Protocol local. A function that retrieves a list of valid attribute names
  68. (defvar eudc-list-attributes-function nil)
  69. ;; Protocol local. A mapping between EUDC attribute names and corresponding
  70. ;; protocol specific names. The following names are defined by EUDC and may be
  71. ;; included in that list: `name' , `firstname', `email', `phone'
  72. (defvar eudc-protocol-attributes-translation-alist nil)
  73. ;; Protocol local. Mapping between protocol attribute names and BBDB field
  74. ;; names
  75. (defvar eudc-bbdb-conversion-alist nil)
  76. ;; Protocol/Server local. Hook called upon switching to that server
  77. (defvar eudc-switch-to-server-hook nil)
  78. ;; Protocol/Server local. Hook called upon switching from that server
  79. (defvar eudc-switch-from-server-hook nil)
  80. ;; Protocol local. Whether the protocol supports queries with no specified
  81. ;; attribute name
  82. (defvar eudc-protocol-has-default-query-attributes nil)
  83. (defun eudc-cadr (obj)
  84. (car (cdr obj)))
  85. (defun eudc-cdar (obj)
  86. (cdr (car obj)))
  87. (defun eudc-caar (obj)
  88. (car (car obj)))
  89. (defun eudc-cdaar (obj)
  90. (cdr (car (car obj))))
  91. (defun eudc-plist-member (plist prop)
  92. "Return t if PROP has a value specified in PLIST."
  93. (if (not (= 0 (% (length plist) 2)))
  94. (error "Malformed plist"))
  95. (catch 'found
  96. (while plist
  97. (if (eq prop (car plist))
  98. (throw 'found t))
  99. (setq plist (cdr (cdr plist))))
  100. nil))
  101. ;; Emacs's plist-get lacks third parameter
  102. (defun eudc-plist-get (plist prop &optional default)
  103. "Extract a value from a property list.
  104. PLIST is a property list, which is a list of the form
  105. \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
  106. corresponding to the given PROP, or DEFAULT if PROP is not
  107. one of the properties on the list."
  108. (if (eudc-plist-member plist prop)
  109. (plist-get plist prop)
  110. default))
  111. (defun eudc-lax-plist-get (plist prop &optional default)
  112. "Extract a value from a lax property list.
  113. PLIST is a lax property list, which is a list of the form (PROP1
  114. VALUE1 PROP2 VALUE2...), where comparisons between properties are done
  115. using `equal' instead of `eq'. This function returns the value
  116. corresponding to PROP, or DEFAULT if PROP is not one of the
  117. properties on the list."
  118. (if (not (= 0 (% (length plist) 2)))
  119. (error "Malformed plist"))
  120. (catch 'found
  121. (while plist
  122. (if (equal prop (car plist))
  123. (throw 'found (car (cdr plist))))
  124. (setq plist (cdr (cdr plist))))
  125. default))
  126. (if (not (fboundp 'split-string))
  127. (defun split-string (string &optional pattern)
  128. "Return a list of substrings of STRING which are separated by PATTERN.
  129. If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
  130. (or pattern
  131. (setq pattern "[ \f\t\n\r\v]+"))
  132. (let (parts (start 0))
  133. (when (string-match pattern string 0)
  134. (if (> (match-beginning 0) 0)
  135. (setq parts (cons (substring string 0 (match-beginning 0)) nil)))
  136. (setq start (match-end 0))
  137. (while (and (string-match pattern string start)
  138. (> (match-end 0) start))
  139. (setq parts (cons (substring string start (match-beginning 0)) parts)
  140. start (match-end 0))))
  141. (nreverse (if (< start (length string))
  142. (cons (substring string start) parts)
  143. parts)))))
  144. (defun eudc-replace-in-string (str regexp newtext)
  145. "Replace all matches in STR for REGEXP with NEWTEXT.
  146. Value is the new string."
  147. (let ((rtn-str "")
  148. (start 0)
  149. match prev-start)
  150. (while (setq match (string-match regexp str start))
  151. (setq prev-start start
  152. start (match-end 0)
  153. rtn-str
  154. (concat rtn-str
  155. (substring str prev-start match)
  156. newtext)))
  157. (concat rtn-str (substring str start))))
  158. ;;}}}
  159. ;;{{{ Server and Protocol Variable Routines
  160. (defun eudc-server-local-variable-p (var)
  161. "Return non-nil if VAR has server-local bindings."
  162. (eudc-plist-member (get var 'eudc-locals) 'server))
  163. (defun eudc-protocol-local-variable-p (var)
  164. "Return non-nil if VAR has protocol-local bindings."
  165. (eudc-plist-member (get var 'eudc-locals) 'protocol))
  166. (defun eudc-default-set (var val)
  167. "Set the EUDC default value of VAR to VAL.
  168. The current binding of VAR is not changed."
  169. (put var 'eudc-locals
  170. (plist-put (get var 'eudc-locals) 'default val))
  171. (add-to-list 'eudc-local-vars var))
  172. (defun eudc-protocol-set (var val &optional protocol)
  173. "Set the PROTOCOL-local binding of VAR to VAL.
  174. If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
  175. The current binding of VAR is changed only if PROTOCOL is omitted."
  176. (if (eq 'unbound (eudc-variable-default-value var))
  177. (eudc-default-set var (symbol-value var)))
  178. (let* ((eudc-locals (get var 'eudc-locals))
  179. (protocol-locals (eudc-plist-get eudc-locals 'protocol)))
  180. (setq protocol-locals (plist-put protocol-locals (or protocol
  181. eudc-protocol) val))
  182. (setq eudc-locals
  183. (plist-put eudc-locals 'protocol protocol-locals))
  184. (put var 'eudc-locals eudc-locals)
  185. (add-to-list 'eudc-local-vars var)
  186. (unless protocol
  187. (eudc-update-variable var))))
  188. (defun eudc-server-set (var val &optional server)
  189. "Set the SERVER-local binding of VAR to VAL.
  190. If omitted SERVER defaults to the current value of `eudc-server'.
  191. The current binding of VAR is changed only if SERVER is omitted."
  192. (if (eq 'unbound (eudc-variable-default-value var))
  193. (eudc-default-set var (symbol-value var)))
  194. (let* ((eudc-locals (get var 'eudc-locals))
  195. (server-locals (eudc-plist-get eudc-locals 'server)))
  196. (setq server-locals (plist-put server-locals (or server
  197. eudc-server) val))
  198. (setq eudc-locals
  199. (plist-put eudc-locals 'server server-locals))
  200. (put var 'eudc-locals eudc-locals)
  201. (add-to-list 'eudc-local-vars var)
  202. (unless server
  203. (eudc-update-variable var))))
  204. (defun eudc-set (var val)
  205. "Set the most local (server, protocol or default) binding of VAR to VAL.
  206. The current binding of VAR is also set to VAL"
  207. (cond
  208. ((not (eq 'unbound (eudc-variable-server-value var)))
  209. (eudc-server-set var val))
  210. ((not (eq 'unbound (eudc-variable-protocol-value var)))
  211. (eudc-protocol-set var val))
  212. (t
  213. (eudc-default-set var val)))
  214. (set var val))
  215. (defun eudc-variable-default-value (var)
  216. "Return the default binding of VAR.
  217. Return `unbound' if VAR has no EUDC default value."
  218. (let ((eudc-locals (get var 'eudc-locals)))
  219. (if (and (boundp var)
  220. eudc-locals)
  221. (eudc-plist-get eudc-locals 'default 'unbound)
  222. 'unbound)))
  223. (defun eudc-variable-protocol-value (var &optional protocol)
  224. "Return the value of VAR local to PROTOCOL.
  225. Return `unbound' if VAR has no value local to PROTOCOL.
  226. PROTOCOL defaults to `eudc-protocol'"
  227. (let* ((eudc-locals (get var 'eudc-locals))
  228. protocol-locals)
  229. (if (not (and (boundp var)
  230. eudc-locals
  231. (eudc-plist-member eudc-locals 'protocol)))
  232. 'unbound
  233. (setq protocol-locals (eudc-plist-get eudc-locals 'protocol))
  234. (eudc-lax-plist-get protocol-locals
  235. (or protocol
  236. eudc-protocol) 'unbound))))
  237. (defun eudc-variable-server-value (var &optional server)
  238. "Return the value of VAR local to SERVER.
  239. Return `unbound' if VAR has no value local to SERVER.
  240. SERVER defaults to `eudc-server'"
  241. (let* ((eudc-locals (get var 'eudc-locals))
  242. server-locals)
  243. (if (not (and (boundp var)
  244. eudc-locals
  245. (eudc-plist-member eudc-locals 'server)))
  246. 'unbound
  247. (setq server-locals (eudc-plist-get eudc-locals 'server))
  248. (eudc-lax-plist-get server-locals
  249. (or server
  250. eudc-server) 'unbound))))
  251. (defun eudc-update-variable (var)
  252. "Set the value of VAR according to its locals.
  253. If the VAR has a server- or protocol-local value corresponding
  254. to the current `eudc-server' and `eudc-protocol' then it is set
  255. accordingly. Otherwise it is set to its EUDC default binding"
  256. (let (val)
  257. (cond
  258. ((not (eq 'unbound (setq val (eudc-variable-server-value var))))
  259. (set var val))
  260. ((not (eq 'unbound (setq val (eudc-variable-protocol-value var))))
  261. (set var val))
  262. ((not (eq 'unbound (setq val (eudc-variable-default-value var))))
  263. (set var val)))))
  264. (defun eudc-update-local-variables ()
  265. "Update all EUDC variables according to their local settings."
  266. (interactive)
  267. (mapcar 'eudc-update-variable eudc-local-vars))
  268. (eudc-default-set 'eudc-query-function nil)
  269. (eudc-default-set 'eudc-list-attributes-function nil)
  270. (eudc-default-set 'eudc-protocol-attributes-translation-alist nil)
  271. (eudc-default-set 'eudc-bbdb-conversion-alist nil)
  272. (eudc-default-set 'eudc-switch-to-server-hook nil)
  273. (eudc-default-set 'eudc-switch-from-server-hook nil)
  274. (eudc-default-set 'eudc-protocol-has-default-query-attributes nil)
  275. (eudc-default-set 'eudc-attribute-display-method-alist nil)
  276. ;;}}}
  277. ;; Add PROTOCOL to the list of supported protocols
  278. (defun eudc-register-protocol (protocol)
  279. (unless (memq protocol eudc-supported-protocols)
  280. (setq eudc-supported-protocols
  281. (cons protocol eudc-supported-protocols))
  282. (put 'eudc-protocol 'custom-type
  283. `(choice :menu-tag "Protocol"
  284. ,@(mapcar (lambda (s)
  285. (list 'string ':tag (symbol-name s)))
  286. eudc-supported-protocols))))
  287. (or (memq protocol eudc-known-protocols)
  288. (setq eudc-known-protocols
  289. (cons protocol eudc-known-protocols))))
  290. (defun eudc-translate-query (query)
  291. "Translate attribute names of QUERY.
  292. The translation is done according to
  293. `eudc-protocol-attributes-translation-alist'."
  294. (if eudc-protocol-attributes-translation-alist
  295. (mapcar (lambda (attribute)
  296. (let ((trans (assq (car attribute)
  297. (symbol-value eudc-protocol-attributes-translation-alist))))
  298. (if trans
  299. (cons (cdr trans) (cdr attribute))
  300. attribute)))
  301. query)
  302. query))
  303. (defun eudc-translate-attribute-list (list)
  304. "Translate a list of attribute names LIST.
  305. The translation is done according to
  306. `eudc-protocol-attributes-translation-alist'."
  307. (if eudc-protocol-attributes-translation-alist
  308. (let (trans)
  309. (mapcar (lambda (attribute)
  310. (setq trans (assq attribute
  311. (symbol-value eudc-protocol-attributes-translation-alist)))
  312. (if trans
  313. (cdr trans)
  314. attribute))
  315. list))
  316. list))
  317. (defun eudc-select (choices beg end)
  318. "Choose one from CHOICES using a completion.
  319. BEG and END delimit the text which is to be replaced."
  320. (let ((replacement))
  321. (setq replacement
  322. (completing-read "Multiple matches found; choose one: "
  323. (mapcar 'list choices)))
  324. (delete-region beg end)
  325. (insert replacement)))
  326. (defun eudc-query (query &optional return-attributes no-translation)
  327. "Query the current directory server with QUERY.
  328. QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
  329. name and VALUE the corresponding value.
  330. If NO-TRANSLATION is non-nil, ATTR is translated according to
  331. `eudc-protocol-attributes-translation-alist'.
  332. RETURN-ATTRIBUTES is a list of attributes to return defaulting to
  333. `eudc-default-return-attributes'."
  334. (unless eudc-query-function
  335. (error "Don't know how to perform the query"))
  336. (if no-translation
  337. (funcall eudc-query-function query (or return-attributes
  338. eudc-default-return-attributes))
  339. (funcall eudc-query-function
  340. (eudc-translate-query query)
  341. (cond
  342. (return-attributes
  343. (eudc-translate-attribute-list return-attributes))
  344. ((listp eudc-default-return-attributes)
  345. (eudc-translate-attribute-list eudc-default-return-attributes))
  346. (t
  347. eudc-default-return-attributes)))))
  348. (defun eudc-format-attribute-name-for-display (attribute)
  349. "Format a directory attribute name for display.
  350. ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
  351. by the corresponding user name if any. Otherwise it is capitalized and
  352. underscore characters are replaced by spaces."
  353. (let ((match (assq attribute eudc-user-attribute-names-alist)))
  354. (if match
  355. (cdr match)
  356. (capitalize
  357. (mapconcat 'identity
  358. (split-string (symbol-name attribute) "_")
  359. " ")))))
  360. (defun eudc-print-attribute-value (field)
  361. "Insert the value of the directory FIELD at point.
  362. The directory attribute name in car of FIELD is looked up in
  363. `eudc-attribute-display-method-alist' and the corresponding method,
  364. if any, is called to print the value in cdr of FIELD."
  365. (let ((match (assoc (downcase (car field))
  366. eudc-attribute-display-method-alist))
  367. (col (current-column))
  368. (val (cdr field)))
  369. (if match
  370. (progn
  371. (eval (list (cdr match) val))
  372. (insert "\n"))
  373. (mapcar
  374. (function
  375. (lambda (val-elem)
  376. (indent-to col)
  377. (insert val-elem "\n")))
  378. (cond
  379. ((listp val) val)
  380. ((stringp val) (split-string val "\n"))
  381. ((null val) '(""))
  382. (t (list val)))))))
  383. (defun eudc-print-record-field (field column-width)
  384. "Print the record field FIELD.
  385. FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
  386. COLUMN-WIDTH is the width of the first display column containing the
  387. attribute name ATTR."
  388. (let ((field-beg (point)))
  389. ;; The record field that is passed to this function has already been processed
  390. ;; by `eudc-format-attribute-name-for-display' so we don't need to call it
  391. ;; again to display the attribute name
  392. (insert (format (concat "%" (int-to-string column-width) "s: ")
  393. (car field)))
  394. (put-text-property field-beg (point) 'face 'bold)
  395. (indent-to (+ 2 column-width))
  396. (eudc-print-attribute-value field)))
  397. (defun eudc-display-records (records &optional raw-attr-names)
  398. "Display the record list RECORDS in a formatted buffer.
  399. If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
  400. otherwise they are formatted according to `eudc-user-attribute-names-alist'."
  401. (let (inhibit-read-only
  402. precords
  403. (width 0)
  404. beg
  405. first-record
  406. attribute-name)
  407. (with-output-to-temp-buffer "*Directory Query Results*"
  408. (with-current-buffer standard-output
  409. (setq buffer-read-only t)
  410. (setq inhibit-read-only t)
  411. (erase-buffer)
  412. (insert "Directory Query Result\n")
  413. (insert "======================\n\n\n")
  414. (if (null records)
  415. (insert "No match found.\n"
  416. (if eudc-strict-return-matches
  417. "Try setting `eudc-strict-return-matches' to nil or change `eudc-default-return-attributes'.\n"
  418. ""))
  419. ;; Replace field names with user names, compute max width
  420. (setq precords
  421. (mapcar
  422. (function
  423. (lambda (record)
  424. (mapcar
  425. (function
  426. (lambda (field)
  427. (setq attribute-name
  428. (if raw-attr-names
  429. (symbol-name (car field))
  430. (eudc-format-attribute-name-for-display (car field))))
  431. (if (> (length attribute-name) width)
  432. (setq width (length attribute-name)))
  433. (cons attribute-name (cdr field))))
  434. record)))
  435. records))
  436. ;; Display the records
  437. (setq first-record (point))
  438. (mapc
  439. (function
  440. (lambda (record)
  441. (setq beg (point))
  442. ;; Map over the record fields to print the attribute/value pairs
  443. (mapc (function
  444. (lambda (field)
  445. (eudc-print-record-field field width)))
  446. record)
  447. ;; Store the record internal format in some convenient place
  448. (overlay-put (make-overlay beg (point))
  449. 'eudc-record
  450. (car records))
  451. (setq records (cdr records))
  452. (insert "\n")))
  453. precords))
  454. (insert "\n")
  455. (widget-create 'push-button
  456. :notify (lambda (&rest ignore)
  457. (eudc-query-form))
  458. "New query")
  459. (widget-insert " ")
  460. (widget-create 'push-button
  461. :notify (lambda (&rest ignore)
  462. (kill-this-buffer))
  463. "Quit")
  464. (eudc-mode)
  465. (widget-setup)
  466. (if first-record
  467. (goto-char first-record))))))
  468. (defun eudc-process-form ()
  469. "Process the query form in current buffer and display the results."
  470. (let (query-alist
  471. value)
  472. (if (not (and (boundp 'eudc-form-widget-list)
  473. eudc-form-widget-list))
  474. (error "Not in a directory query form buffer")
  475. (mapc (function
  476. (lambda (wid-field)
  477. (setq value (widget-value (cdr wid-field)))
  478. (if (not (string= value ""))
  479. (setq query-alist (cons (cons (car wid-field) value)
  480. query-alist)))))
  481. eudc-form-widget-list)
  482. (kill-buffer (current-buffer))
  483. (eudc-display-records (eudc-query query-alist) eudc-use-raw-directory-names))))
  484. (defun eudc-filter-duplicate-attributes (record)
  485. "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
  486. (let ((rec record)
  487. unique
  488. duplicates
  489. result)
  490. ;; Search for multiple records
  491. (while (and rec
  492. (not (listp (eudc-cdar rec))))
  493. (setq rec (cdr rec)))
  494. (if (null (eudc-cdar rec))
  495. (list record) ; No duplicate attrs in this record
  496. (mapc (function
  497. (lambda (field)
  498. (if (listp (cdr field))
  499. (setq duplicates (cons field duplicates))
  500. (setq unique (cons field unique)))))
  501. record)
  502. (setq result (list unique))
  503. ;; Map over the record fields that have multiple values
  504. (mapc
  505. (function
  506. (lambda (field)
  507. (let ((method (if (consp eudc-duplicate-attribute-handling-method)
  508. (cdr
  509. (assq
  510. (or
  511. (car
  512. (rassq
  513. (car field)
  514. (symbol-value
  515. eudc-protocol-attributes-translation-alist)))
  516. (car field))
  517. eudc-duplicate-attribute-handling-method))
  518. eudc-duplicate-attribute-handling-method)))
  519. (cond
  520. ((or (null method) (eq 'list method))
  521. (setq result
  522. (eudc-add-field-to-records field result)))
  523. ((eq 'first method)
  524. (setq result
  525. (eudc-add-field-to-records (cons (car field)
  526. (eudc-cadr field))
  527. result)))
  528. ((eq 'concat method)
  529. (setq result
  530. (eudc-add-field-to-records (cons (car field)
  531. (mapconcat
  532. 'identity
  533. (cdr field)
  534. "\n")) result)))
  535. ((eq 'duplicate method)
  536. (setq result
  537. (eudc-distribute-field-on-records field result)))))))
  538. duplicates)
  539. result)))
  540. (defun eudc-filter-partial-records (records attrs)
  541. "Eliminate records that do not contain all ATTRS from RECORDS."
  542. (delq nil
  543. (mapcar
  544. (function
  545. (lambda (rec)
  546. (if (eval (cons 'and
  547. (mapcar
  548. (function
  549. (lambda (attr)
  550. (consp (assq attr rec))))
  551. attrs)))
  552. rec)))
  553. records)))
  554. (defun eudc-add-field-to-records (field records)
  555. "Add FIELD to each individual record in RECORDS and return the resulting list."
  556. (mapcar (function
  557. (lambda (r)
  558. (cons field r)))
  559. records))
  560. (defun eudc-distribute-field-on-records (field records)
  561. "Duplicate each individual record in RECORDS according to value of FIELD.
  562. Each copy is added a new field containing one of the values of FIELD."
  563. (let (result
  564. (values (cdr field)))
  565. ;; Uniquify values first
  566. (while values
  567. (setcdr values (delete (car values) (cdr values)))
  568. (setq values (cdr values)))
  569. (mapc
  570. (function
  571. (lambda (value)
  572. (let ((result-list (copy-sequence records)))
  573. (setq result-list (eudc-add-field-to-records
  574. (cons (car field) value)
  575. result-list))
  576. (setq result (append result-list result))
  577. )))
  578. (cdr field))
  579. result))
  580. (defun eudc-mode ()
  581. "Major mode used in buffers displaying the results of directory queries.
  582. There is no sense in calling this command from a buffer other than
  583. one containing the results of a directory query.
  584. These are the special commands of EUDC mode:
  585. q -- Kill this buffer.
  586. f -- Display a form to query the current directory server.
  587. n -- Move to next record.
  588. p -- Move to previous record.
  589. b -- Insert record at point into the BBDB database."
  590. (interactive)
  591. (kill-all-local-variables)
  592. (setq major-mode 'eudc-mode)
  593. (setq mode-name "EUDC")
  594. (use-local-map eudc-mode-map)
  595. (if (not (featurep 'xemacs))
  596. (easy-menu-define eudc-emacs-menu eudc-mode-map "" (eudc-menu))
  597. (setq mode-popup-menu (eudc-menu)))
  598. (run-mode-hooks 'eudc-mode-hook))
  599. ;;}}}
  600. ;;{{{ High-level interfaces (interactive functions)
  601. (defun eudc-customize ()
  602. "Customize the EUDC package."
  603. (interactive)
  604. (customize-group 'eudc))
  605. ;;;###autoload
  606. (defun eudc-set-server (server protocol &optional no-save)
  607. "Set the directory server to SERVER using PROTOCOL.
  608. Unless NO-SAVE is non-nil, the server is saved as the default
  609. server for future sessions."
  610. (interactive (list
  611. (read-from-minibuffer "Directory Server: ")
  612. (intern (completing-read "Protocol: "
  613. (mapcar (lambda (elt)
  614. (cons (symbol-name elt)
  615. elt))
  616. eudc-known-protocols)))))
  617. (unless (or (member protocol
  618. eudc-supported-protocols)
  619. (load (concat "eudcb-" (symbol-name protocol)) t))
  620. (error "Unsupported protocol: %s" protocol))
  621. (run-hooks 'eudc-switch-from-server-hook)
  622. (setq eudc-protocol protocol)
  623. (setq eudc-server server)
  624. (eudc-update-local-variables)
  625. (run-hooks 'eudc-switch-to-server-hook)
  626. (if (called-interactively-p 'interactive)
  627. (message "Current directory server is now %s (%s)" eudc-server eudc-protocol))
  628. (if (null no-save)
  629. (eudc-save-options)))
  630. ;;;###autoload
  631. (defun eudc-get-email (name &optional error)
  632. "Get the email field of NAME from the directory server.
  633. If ERROR is non-nil, report an error if there is none."
  634. (interactive "sName: \np")
  635. (or eudc-server
  636. (call-interactively 'eudc-set-server))
  637. (let ((result (eudc-query (list (cons 'name name)) '(email)))
  638. email)
  639. (if (null (cdr result))
  640. (setq email (eudc-cdaar result))
  641. (error "Multiple match--use the query form"))
  642. (if error
  643. (if email
  644. (message "%s" email)
  645. (error "No record matching %s" name)))
  646. email))
  647. ;;;###autoload
  648. (defun eudc-get-phone (name &optional error)
  649. "Get the phone field of NAME from the directory server.
  650. If ERROR is non-nil, report an error if there is none."
  651. (interactive "sName: \np")
  652. (or eudc-server
  653. (call-interactively 'eudc-set-server))
  654. (let ((result (eudc-query (list (cons 'name name)) '(phone)))
  655. phone)
  656. (if (null (cdr result))
  657. (setq phone (eudc-cdaar result))
  658. (error "Multiple match--use the query form"))
  659. (if error
  660. (if phone
  661. (message "%s" phone)
  662. (error "No record matching %s" name)))
  663. phone))
  664. (defun eudc-get-attribute-list ()
  665. "Return a list of valid attributes for the current server.
  666. When called interactively the list is formatted in a dedicated buffer
  667. otherwise a list of symbols is returned."
  668. (interactive)
  669. (if eudc-list-attributes-function
  670. (let ((entries (funcall eudc-list-attributes-function
  671. (called-interactively-p 'interactive))))
  672. (if entries
  673. (if (called-interactively-p 'interactive)
  674. (eudc-display-records entries t)
  675. entries)))
  676. (error "The %s protocol has no support for listing attributes" eudc-protocol)))
  677. (defun eudc-format-query (words format)
  678. "Use FORMAT to build a EUDC query from WORDS."
  679. (let (query
  680. query-alist
  681. key val cell)
  682. (if format
  683. (progn
  684. (while (and words format)
  685. (setq query-alist (cons (cons (car format) (car words))
  686. query-alist))
  687. (setq words (cdr words)
  688. format (cdr format)))
  689. ;; If the same attribute appears more than once, merge
  690. ;; the corresponding values
  691. (setq query-alist (nreverse query-alist))
  692. (while query-alist
  693. (setq key (eudc-caar query-alist)
  694. val (eudc-cdar query-alist)
  695. cell (assq key query))
  696. (if cell
  697. (setcdr cell (concat (cdr cell) " " val))
  698. (setq query (cons (car query-alist) query)))
  699. (setq query-alist (cdr query-alist)))
  700. query)
  701. (if eudc-protocol-has-default-query-attributes
  702. (mapconcat 'identity words " ")
  703. (list (cons 'name (mapconcat 'identity words " ")))))))
  704. (defun eudc-extract-n-word-formats (format-list n)
  705. "Extract a list of N-long formats from FORMAT-LIST.
  706. If none try N - 1 and so forth."
  707. (let (formats)
  708. (while (and (null formats)
  709. (> n 0))
  710. (setq formats
  711. (delq nil
  712. (mapcar (lambda (format)
  713. (if (= n
  714. (length format))
  715. format
  716. nil))
  717. format-list)))
  718. (setq n (1- n)))
  719. formats))
  720. ;;;###autoload
  721. (defun eudc-expand-inline (&optional replace)
  722. "Query the directory server, and expand the query string before point.
  723. The query string consists of the buffer substring from the point back to
  724. the preceding comma, colon or beginning of line.
  725. The variable `eudc-inline-query-format' controls how to associate the
  726. individual inline query words with directory attribute names.
  727. After querying the server for the given string, the expansion specified by
  728. `eudc-inline-expansion-format' is inserted in the buffer at point.
  729. If REPLACE is non-nil, then this expansion replaces the name in the buffer.
  730. `eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
  731. Multiple servers can be tried with the same query until one finds a match,
  732. see `eudc-inline-expansion-servers'"
  733. (interactive)
  734. (if (memq eudc-inline-expansion-servers
  735. '(current-server server-then-hotlist))
  736. (or eudc-server
  737. (call-interactively 'eudc-set-server))
  738. (or eudc-server-hotlist
  739. (error "No server in the hotlist")))
  740. (let* ((end (point))
  741. (beg (save-excursion
  742. (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
  743. (point-at-bol) 'move)
  744. (goto-char (match-end 0)))
  745. (point)))
  746. (query-words (split-string (buffer-substring beg end) "[ \t]+"))
  747. query-formats
  748. response
  749. response-string
  750. response-strings
  751. (eudc-former-server eudc-server)
  752. (eudc-former-protocol eudc-protocol)
  753. servers)
  754. ;; Prepare the list of servers to query
  755. (setq servers (copy-sequence eudc-server-hotlist))
  756. (setq servers
  757. (cond
  758. ((eq eudc-inline-expansion-servers 'hotlist)
  759. eudc-server-hotlist)
  760. ((eq eudc-inline-expansion-servers 'server-then-hotlist)
  761. (cons (cons eudc-server eudc-protocol)
  762. (delete (cons eudc-server eudc-protocol) servers)))
  763. ((eq eudc-inline-expansion-servers 'current-server)
  764. (list (cons eudc-server eudc-protocol)))
  765. (t
  766. (error "Wrong value for `eudc-inline-expansion-servers': %S"
  767. eudc-inline-expansion-servers))))
  768. (if (and eudc-max-servers-to-query
  769. (> (length servers) eudc-max-servers-to-query))
  770. (setcdr (nthcdr (1- eudc-max-servers-to-query) servers) nil))
  771. (condition-case signal
  772. (progn
  773. (setq response
  774. (catch 'found
  775. ;; Loop on the servers
  776. (while servers
  777. (eudc-set-server (eudc-caar servers) (eudc-cdar servers) t)
  778. ;; Determine which formats apply in the query-format list
  779. (setq query-formats
  780. (or
  781. (eudc-extract-n-word-formats eudc-inline-query-format
  782. (length query-words))
  783. (if (null eudc-protocol-has-default-query-attributes)
  784. '(name))))
  785. ;; Loop on query-formats
  786. (while query-formats
  787. (setq response
  788. (eudc-query
  789. (eudc-format-query query-words (car query-formats))
  790. (eudc-translate-attribute-list
  791. (cdr eudc-inline-expansion-format))))
  792. (if response
  793. (throw 'found response))
  794. (setq query-formats (cdr query-formats)))
  795. (setq servers (cdr servers)))
  796. ;; No more servers to try... no match found
  797. nil))
  798. (if (null response)
  799. (error "No match")
  800. ;; Process response through eudc-inline-expansion-format
  801. (while response
  802. (setq response-string (apply 'format
  803. (car eudc-inline-expansion-format)
  804. (mapcar (function
  805. (lambda (field)
  806. (or (cdr (assq field (car response)))
  807. "")))
  808. (eudc-translate-attribute-list
  809. (cdr eudc-inline-expansion-format)))))
  810. (if (> (length response-string) 0)
  811. (setq response-strings
  812. (cons response-string response-strings)))
  813. (setq response (cdr response)))
  814. (if (or
  815. (and replace (not eudc-expansion-overwrites-query))
  816. (and (not replace) eudc-expansion-overwrites-query))
  817. (kill-ring-save beg end))
  818. (cond
  819. ((or (= (length response-strings) 1)
  820. (null eudc-multiple-match-handling-method)
  821. (eq eudc-multiple-match-handling-method 'first))
  822. (delete-region beg end)
  823. (insert (car response-strings)))
  824. ((eq eudc-multiple-match-handling-method 'select)
  825. (eudc-select response-strings beg end))
  826. ((eq eudc-multiple-match-handling-method 'all)
  827. (delete-region beg end)
  828. (insert (mapconcat 'identity response-strings ", ")))
  829. ((eq eudc-multiple-match-handling-method 'abort)
  830. (error "There is more than one match for the query"))))
  831. (or (and (equal eudc-server eudc-former-server)
  832. (equal eudc-protocol eudc-former-protocol))
  833. (eudc-set-server eudc-former-server eudc-former-protocol t)))
  834. (error
  835. (or (and (equal eudc-server eudc-former-server)
  836. (equal eudc-protocol eudc-former-protocol))
  837. (eudc-set-server eudc-former-server eudc-former-protocol t))
  838. (signal (car signal) (cdr signal))))))
  839. ;;;###autoload
  840. (defun eudc-query-form (&optional get-fields-from-server)
  841. "Display a form to query the directory server.
  842. If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
  843. queries the server for the existing fields and displays a corresponding form."
  844. (interactive "P")
  845. (let ((fields (or (and get-fields-from-server
  846. (eudc-get-attribute-list))
  847. eudc-query-form-attributes))
  848. (buffer (get-buffer-create "*Directory Query Form*"))
  849. prompts
  850. widget
  851. (width 0)
  852. inhibit-read-only
  853. pt)
  854. (switch-to-buffer buffer)
  855. (setq inhibit-read-only t)
  856. (erase-buffer)
  857. (kill-all-local-variables)
  858. (make-local-variable 'eudc-form-widget-list)
  859. (widget-insert "Directory Query Form\n")
  860. (widget-insert "====================\n\n")
  861. (widget-insert "Current server is: " (or eudc-server
  862. (progn
  863. (call-interactively 'eudc-set-server)
  864. eudc-server))
  865. "\n")
  866. (widget-insert "Protocol : " (symbol-name eudc-protocol) "\n")
  867. ;; Build the list of prompts
  868. (setq prompts (if eudc-use-raw-directory-names
  869. (mapcar 'symbol-name (eudc-translate-attribute-list fields))
  870. (mapcar (function
  871. (lambda (field)
  872. (or (and (assq field eudc-user-attribute-names-alist)
  873. (cdr (assq field eudc-user-attribute-names-alist)))
  874. (capitalize (symbol-name field)))))
  875. fields)))
  876. ;; Loop over prompt strings to find the longest one
  877. (mapc (function
  878. (lambda (prompt)
  879. (if (> (length prompt) width)
  880. (setq width (length prompt)))))
  881. prompts)
  882. ;; Insert the first widget out of the mapcar to leave the cursor
  883. ;; in the first field
  884. (widget-insert "\n\n" (format (concat "%" (int-to-string width) "s: ") (car prompts)))
  885. (setq pt (point))
  886. (setq widget (widget-create 'editable-field :size 15))
  887. (setq eudc-form-widget-list (cons (cons (car fields) widget)
  888. eudc-form-widget-list))
  889. (setq fields (cdr fields))
  890. (setq prompts (cdr prompts))
  891. (mapc (function
  892. (lambda (field)
  893. (widget-insert "\n\n" (format (concat "%" (int-to-string width) "s: ") (car prompts)))
  894. (setq widget (widget-create 'editable-field
  895. :size 15))
  896. (setq eudc-form-widget-list (cons (cons field widget)
  897. eudc-form-widget-list))
  898. (setq prompts (cdr prompts))))
  899. fields)
  900. (widget-insert "\n\n")
  901. (widget-create 'push-button
  902. :notify (lambda (&rest ignore)
  903. (eudc-process-form))
  904. "Query Server")
  905. (widget-insert " ")
  906. (widget-create 'push-button
  907. :notify (lambda (&rest ignore)
  908. (eudc-query-form))
  909. "Reset Form")
  910. (widget-insert " ")
  911. (widget-create 'push-button
  912. :notify (lambda (&rest ignore)
  913. (kill-this-buffer))
  914. "Quit")
  915. (goto-char pt)
  916. (use-local-map widget-keymap)
  917. (widget-setup))
  918. )
  919. (defun eudc-bookmark-server (server protocol)
  920. "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
  921. (interactive "sDirectory server: \nsProtocol: ")
  922. (if (member (cons server protocol) eudc-server-hotlist)
  923. (error "%s:%s is already in the hotlist" protocol server)
  924. (setq eudc-server-hotlist (cons (cons server protocol) eudc-server-hotlist))
  925. (eudc-install-menu)
  926. (eudc-save-options)))
  927. (defun eudc-bookmark-current-server ()
  928. "Add current server to the EUDC `servers' hotlist."
  929. (interactive)
  930. (eudc-bookmark-server eudc-server eudc-protocol))
  931. (defun eudc-save-options ()
  932. "Save options to `eudc-options-file'."
  933. (interactive)
  934. (with-current-buffer (find-file-noselect eudc-options-file t)
  935. (goto-char (point-min))
  936. ;; delete the previous setq
  937. (let ((standard-output (current-buffer))
  938. provide-p
  939. set-hotlist-p
  940. set-server-p)
  941. (catch 'found
  942. (while t
  943. (let ((sexp (condition-case nil
  944. (read (current-buffer))
  945. (end-of-file (throw 'found nil)))))
  946. (if (listp sexp)
  947. (cond
  948. ((eq (car sexp) 'eudc-set-server)
  949. (delete-region (save-excursion
  950. (backward-sexp)
  951. (point))
  952. (point))
  953. (setq set-server-p t))
  954. ((and (eq (car sexp) 'setq)
  955. (eq (eudc-cadr sexp) 'eudc-server-hotlist))
  956. (delete-region (save-excursion
  957. (backward-sexp)
  958. (point))
  959. (point))
  960. (setq set-hotlist-p t))
  961. ((and (eq (car sexp) 'provide)
  962. (equal (eudc-cadr sexp) '(quote eudc-options-file)))
  963. (setq provide-p t)))
  964. (if (and provide-p
  965. set-hotlist-p
  966. set-server-p)
  967. (throw 'found t))))))
  968. (if (eq (point-min) (point-max))
  969. (princ ";; This file was automatically generated by eudc.el.\n\n"))
  970. (or provide-p
  971. (princ "(provide 'eudc-options-file)\n"))
  972. (or (bolp)
  973. (princ "\n"))
  974. (delete-blank-lines)
  975. (princ "(eudc-set-server ")
  976. (prin1 eudc-server)
  977. (princ " '")
  978. (prin1 eudc-protocol)
  979. (princ " t)\n")
  980. (princ "(setq eudc-server-hotlist '")
  981. (prin1 eudc-server-hotlist)
  982. (princ ")\n")
  983. (save-buffer))))
  984. (defun eudc-move-to-next-record ()
  985. "Move to next record, in a buffer displaying directory query results."
  986. (interactive)
  987. (if (not (eq major-mode 'eudc-mode))
  988. (error "Not in a EUDC buffer")
  989. (let ((pt (next-overlay-change (point))))
  990. (if (< pt (point-max))
  991. (goto-char (1+ pt))
  992. (error "No more records after point")))))
  993. (defun eudc-move-to-previous-record ()
  994. "Move to previous record, in a buffer displaying directory query results."
  995. (interactive)
  996. (if (not (eq major-mode 'eudc-mode))
  997. (error "Not in a EUDC buffer")
  998. (let ((pt (previous-overlay-change (point))))
  999. (if (> pt (point-min))
  1000. (goto-char pt)
  1001. (error "No more records before point")))))
  1002. ;;}}}
  1003. ;;{{{ Menus and keymaps
  1004. (require 'easymenu)
  1005. (defconst eudc-custom-generated-menu (cdr (custom-menu-create 'eudc)))
  1006. (defconst eudc-tail-menu
  1007. `(["---" nil nil]
  1008. ["Query with Form" eudc-query-form
  1009. :help "Display a form to query the directory server"]
  1010. ["Expand Inline Query" eudc-expand-inline
  1011. :help "Query the directory server, and expand the query string before point"]
  1012. ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
  1013. (and (or (featurep 'bbdb)
  1014. (prog1 (locate-library "bbdb") (message "")))
  1015. (overlays-at (point))
  1016. (overlay-get (car (overlays-at (point))) 'eudc-record))
  1017. :help "Insert record at point into the BBDB database"]
  1018. ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
  1019. (and (eq major-mode 'eudc-mode)
  1020. (or (featurep 'bbdb)
  1021. (prog1 (locate-library "bbdb") (message ""))))
  1022. :help "Insert all the records returned by a directory query into BBDB"]
  1023. ["---" nil nil]
  1024. ["Get Email" eudc-get-email
  1025. :help "Get the email field of NAME from the directory server"]
  1026. ["Get Phone" eudc-get-phone
  1027. :help "Get the phone field of name from the directory server"]
  1028. ["List Valid Attribute Names" eudc-get-attribute-list
  1029. :help "Return a list of valid attributes for the current server"]
  1030. ["---" nil nil]
  1031. ,(cons "Customize" eudc-custom-generated-menu)))
  1032. (defconst eudc-server-menu
  1033. '(["---" nil nil]
  1034. ["Bookmark Current Server" eudc-bookmark-current-server
  1035. :help "Add current server to the EUDC `servers' hotlist"]
  1036. ["Edit Server List" eudc-edit-hotlist
  1037. :help "Edit the hotlist of directory servers in a specialized buffer"]
  1038. ["New Server" eudc-set-server
  1039. :help "Set the directory server to SERVER using PROTOCOL"]))
  1040. (defun eudc-menu ()
  1041. (let (command)
  1042. (append '("Directory Search")
  1043. (list
  1044. (append
  1045. '("Server")
  1046. (mapcar
  1047. (function
  1048. (lambda (servspec)
  1049. (let* ((server (car servspec))
  1050. (protocol (cdr servspec))
  1051. (proto-name (symbol-name protocol)))
  1052. (setq command (intern (concat "eudc-set-server-"
  1053. server
  1054. "-"
  1055. proto-name)))
  1056. (if (not (fboundp command))
  1057. (fset command
  1058. `(lambda ()
  1059. (interactive)
  1060. (eudc-set-server ,server (quote ,protocol))
  1061. (message "Selected directory server is now %s (%s)"
  1062. ,server
  1063. ,proto-name))))
  1064. (vector (format "%s (%s)" server proto-name)
  1065. command
  1066. :style 'radio
  1067. :selected `(equal eudc-server ,server)))))
  1068. eudc-server-hotlist)
  1069. eudc-server-menu))
  1070. eudc-tail-menu)))
  1071. (defun eudc-install-menu ()
  1072. (cond
  1073. ((and (featurep 'xemacs) (featurep 'menubar))
  1074. (add-submenu '("Tools") (eudc-menu)))
  1075. ((not (featurep 'xemacs))
  1076. (cond
  1077. ((fboundp 'easy-menu-create-menu)
  1078. (define-key
  1079. global-map
  1080. [menu-bar tools directory-search]
  1081. (cons "Directory Search"
  1082. (easy-menu-create-menu "Directory Search" (cdr (eudc-menu))))))
  1083. ((fboundp 'easy-menu-add-item)
  1084. (let ((menu (eudc-menu)))
  1085. (easy-menu-add-item nil '("tools") (easy-menu-create-menu (car menu)
  1086. (cdr menu)))))
  1087. ((fboundp 'easy-menu-create-keymaps)
  1088. (easy-menu-define eudc-menu-map eudc-mode-map "Directory Client Menu" (eudc-menu))
  1089. (define-key
  1090. global-map
  1091. [menu-bar tools eudc]
  1092. (cons "Directory Search"
  1093. (easy-menu-create-keymaps "Directory Search" (cdr (eudc-menu))))))
  1094. (t
  1095. (error "Unknown version of easymenu"))))
  1096. ))
  1097. ;;; Load time initializations :
  1098. ;;; Load the options file
  1099. (if (and (not noninteractive)
  1100. (and (locate-library eudc-options-file)
  1101. (progn (message "") t)) ; Remove modeline message
  1102. (not (featurep 'eudc-options-file)))
  1103. (load eudc-options-file))
  1104. ;;; Install the full menu
  1105. (unless (featurep 'infodock)
  1106. (eudc-install-menu))
  1107. ;;; The following installs a short menu for EUDC at XEmacs startup.
  1108. ;;;###autoload
  1109. (defun eudc-load-eudc ()
  1110. "Load the Emacs Unified Directory Client.
  1111. This does nothing except loading eudc by autoload side-effect."
  1112. (interactive)
  1113. nil)
  1114. ;;;###autoload
  1115. (cond
  1116. ((not (featurep 'xemacs))
  1117. (defvar eudc-tools-menu
  1118. (let ((map (make-sparse-keymap "Directory Search")))
  1119. (define-key map [phone]
  1120. `(menu-item ,(purecopy "Get Phone") eudc-get-phone
  1121. :help ,(purecopy "Get the phone field of name from the directory server")))
  1122. (define-key map [email]
  1123. `(menu-item ,(purecopy "Get Email") eudc-get-email
  1124. :help ,(purecopy "Get the email field of NAME from the directory server")))
  1125. (define-key map [separator-eudc-email] menu-bar-separator)
  1126. (define-key map [expand-inline]
  1127. `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
  1128. :help ,(purecopy "Query the directory server, and expand the query string before point")))
  1129. (define-key map [query]
  1130. `(menu-item ,(purecopy "Query with Form") eudc-query-form
  1131. :help ,(purecopy "Display a form to query the directory server")))
  1132. (define-key map [separator-eudc-query] menu-bar-separator)
  1133. (define-key map [new]
  1134. `(menu-item ,(purecopy "New Server") eudc-set-server
  1135. :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
  1136. (define-key map [load]
  1137. `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
  1138. :help ,(purecopy "Load the Emacs Unified Directory Client")))
  1139. map))
  1140. (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
  1141. (t
  1142. (let ((menu '("Directory Search"
  1143. ["Load Hotlist of Servers" eudc-load-eudc t]
  1144. ["New Server" eudc-set-server t]
  1145. ["---" nil nil]
  1146. ["Query with Form" eudc-query-form t]
  1147. ["Expand Inline Query" eudc-expand-inline t]
  1148. ["---" nil nil]
  1149. ["Get Email" eudc-get-email t]
  1150. ["Get Phone" eudc-get-phone t])))
  1151. (if (not (featurep 'eudc-autoloads))
  1152. (if (featurep 'xemacs)
  1153. (if (and (featurep 'menubar)
  1154. (not (featurep 'infodock)))
  1155. (add-submenu '("Tools") menu))
  1156. (require 'easymenu)
  1157. (cond
  1158. ((fboundp 'easy-menu-add-item)
  1159. (easy-menu-add-item nil '("tools")
  1160. (easy-menu-create-menu (car menu)
  1161. (cdr menu))))
  1162. ((fboundp 'easy-menu-create-keymaps)
  1163. (define-key
  1164. global-map
  1165. [menu-bar tools eudc]
  1166. (cons "Directory Search"
  1167. (easy-menu-create-keymaps "Directory Search"
  1168. (cdr menu)))))))))))
  1169. ;;}}}
  1170. (provide 'eudc)
  1171. ;;; eudc.el ends here