org-contacts.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. ;;; org-contacts.el --- Contacts management
  2. ;; Copyright (C) 2010-2013 Julien Danjou <julien@danjou.info>
  3. ;; Author: Julien Danjou <julien@danjou.info>
  4. ;; Keywords: outlines, hypermedia, calendar
  5. ;;
  6. ;; This file is NOT part of GNU Emacs.
  7. ;;
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. ;;
  20. ;;; Commentary:
  21. ;; This file contains the code for managing your contacts into Org-mode.
  22. ;; To enter new contacts, you can use `org-capture' and a template just like
  23. ;; this:
  24. ;; ("c" "Contacts" entry (file "~/Org/contacts.org")
  25. ;; "* %(org-contacts-template-name)
  26. ;; :PROPERTIES:
  27. ;; :EMAIL: %(org-contacts-template-email)
  28. ;; :END:")))
  29. ;;
  30. ;;; Code:
  31. (eval-when-compile
  32. (require 'cl))
  33. (eval-and-compile
  34. (require 'org))
  35. (require 'gnus-util)
  36. (require 'org-agenda)
  37. (defgroup org-contacts nil
  38. "Options about contacts management."
  39. :group 'org)
  40. (defcustom org-contacts-files nil
  41. "List of Org files to use as contacts source.
  42. When set to nil, all your Org files will be used."
  43. :type '(repeat file)
  44. :group 'org-contacts)
  45. (defcustom org-contacts-email-property "EMAIL"
  46. "Name of the property for contact email address."
  47. :type 'string
  48. :group 'org-contacts)
  49. (defcustom org-contacts-address-property "ADDRESS"
  50. "Name of the property for contact address."
  51. :type 'string
  52. :group 'org-contacts)
  53. (defcustom org-contacts-birthday-property "BIRTHDAY"
  54. "Name of the property for contact birthday date."
  55. :type 'string
  56. :group 'org-contacts)
  57. (defcustom org-contacts-birthday-format "Birthday: %l (%Y)"
  58. "Format of the anniversary agenda entry.
  59. The following replacements are available:
  60. %h - Heading name
  61. %l - Link to the heading
  62. %y - Number of year
  63. %Y - Number of year (ordinal)"
  64. :type 'string
  65. :group 'org-contacts)
  66. (defcustom org-contacts-last-read-mail-property "LAST_READ_MAIL"
  67. "Name of the property for contact last read email link storage."
  68. :type 'string
  69. :group 'org-contacts)
  70. (defcustom org-contacts-icon-property "ICON"
  71. "Name of the property for contact icon."
  72. :type 'string
  73. :group 'org-contacts)
  74. (defcustom org-contacts-nickname-property "NICKNAME"
  75. "Name of the property for IRC nickname match."
  76. :type 'string
  77. :group 'org-contacts)
  78. (defcustom org-contacts-icon-size 32
  79. "Size of the contacts icons."
  80. :type 'string
  81. :group 'org-contacts)
  82. (defcustom org-contacts-icon-use-gravatar (fboundp 'gravatar-retrieve)
  83. "Whether use Gravatar to fetch contact icons."
  84. :type 'boolean
  85. :group 'org-contacts)
  86. (defcustom org-contacts-completion-ignore-case t
  87. "Ignore case when completing contacts."
  88. :type 'boolean
  89. :group 'org-contacts)
  90. (defcustom org-contacts-group-prefix "+"
  91. "Group prefix."
  92. :type 'string
  93. :group 'org-contacts)
  94. (defcustom org-contacts-matcher (concat org-contacts-email-property "<>\"\"")
  95. "Matching rule for finding heading that are contacts.
  96. This can be a tag name, or a property check."
  97. :type 'string
  98. :group 'org-contacts)
  99. (defcustom org-contacts-email-link-description-format "%s (%d)"
  100. "Format used to store links to email.
  101. This overrides `org-email-link-description-format' if set."
  102. :group 'org-contacts
  103. :type 'string)
  104. (defcustom org-contacts-vcard-file "contacts.vcf"
  105. "Default file for vcard export."
  106. :group 'org-contacts
  107. :type 'file)
  108. (defvar org-contacts-keymap
  109. (let ((map (make-sparse-keymap)))
  110. (define-key map "M" 'org-contacts-view-send-email)
  111. (define-key map "i" 'org-contacts-view-switch-to-irc-buffer)
  112. map)
  113. "The keymap used in `org-contacts' result list.")
  114. (defun org-contacts-files ()
  115. "Return list of Org files to use for contact management."
  116. (or org-contacts-files (org-agenda-files t 'ifmode)))
  117. (defun org-contacts-filter (&optional name-match tags-match)
  118. "Search for a contact maching NAME-MATCH and TAGS-MATCH.
  119. If both match values are nil, return all contacts."
  120. (let* (todo-only
  121. (tags-matcher
  122. (if tags-match
  123. (cdr (org-make-tags-matcher tags-match))
  124. t))
  125. (name-matcher
  126. (if name-match
  127. '(org-string-match-p name-match (org-get-heading t))
  128. t))
  129. (contacts-matcher
  130. (cdr (org-make-tags-matcher org-contacts-matcher)))
  131. markers result)
  132. (dolist (file (org-contacts-files))
  133. (org-check-agenda-file file)
  134. (with-current-buffer (org-get-agenda-file-buffer file)
  135. (unless (eq major-mode 'org-mode)
  136. (error "File %s is no in `org-mode'" file))
  137. (org-scan-tags
  138. '(add-to-list 'markers (set-marker (make-marker) (point)))
  139. `(and ,contacts-matcher ,tags-matcher ,name-matcher)
  140. todo-only)))
  141. (dolist (marker markers result)
  142. (org-with-point-at marker
  143. (add-to-list 'result
  144. (list (org-get-heading t) marker (org-entry-properties marker 'all)))))))
  145. (when (not (fboundp 'completion-table-case-fold))
  146. ;; That function is new in Emacs 24...
  147. (defun completion-table-case-fold (table &optional dont-fold)
  148. (lambda (string pred action)
  149. (let ((completion-ignore-case (not dont-fold)))
  150. (complete-with-action action table string pred)))))
  151. (defun org-contacts-complete-name (&optional start)
  152. "Complete text at START with a user name and email."
  153. (let* ((end (point))
  154. (start (or start
  155. (save-excursion
  156. (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
  157. (goto-char (match-end 0))
  158. (point))))
  159. (orig (buffer-substring start end))
  160. (completion-ignore-case org-contacts-completion-ignore-case)
  161. (group-completion-p (org-string-match-p
  162. (concat "^" org-contacts-group-prefix) orig))
  163. (completion-list
  164. (if group-completion-p
  165. (mapcar (lambda (group) (propertize (concat org-contacts-group-prefix group)
  166. 'org-contacts-group group))
  167. (org-uniquify
  168. (loop for contact in (org-contacts-filter)
  169. with group-list
  170. nconc (org-split-string
  171. (or (cdr (assoc-string "ALLTAGS" (caddr contact))) "") ":"))))
  172. (loop for contact in (org-contacts-filter)
  173. ;; The contact name is always the car of the assoc-list
  174. ;; returned by `org-contacts-filter'.
  175. for contact-name = (car contact)
  176. ;; Build the list of the user email addresses.
  177. for email-list = (split-string (or
  178. (cdr (assoc-string org-contacts-email-property
  179. (caddr contact))) ""))
  180. ;; If the user has email addresses…
  181. if email-list
  182. ;; … append a list of USER <EMAIL>.
  183. nconc (loop for email in email-list
  184. collect (org-contacts-format-email contact-name email)))))
  185. (completion-list (all-completions orig completion-list)))
  186. ;; If we are completing a group, and that's the only group, just return
  187. ;; the real result.
  188. (when (and group-completion-p
  189. (= (length completion-list) 1))
  190. (setq completion-list
  191. (list (concat
  192. (car completion-list) ";: "
  193. (mapconcat 'identity
  194. (loop for contact in (org-contacts-filter
  195. nil
  196. (get-text-property 0 'org-contacts-group
  197. (car completion-list)))
  198. ;; The contact name is always the car of the assoc-list
  199. ;; returned by `org-contacts-filter'.
  200. for contact-name = (car contact)
  201. ;; Grab the first email of the contact
  202. for email = (car (split-string
  203. (or
  204. (cdr (assoc-string org-contacts-email-property
  205. (caddr contact)))
  206. "")))
  207. ;; If the user has an email address, append USER <EMAIL>.
  208. if email collect (org-contacts-format-email contact-name email))
  209. ", ")))))
  210. (list start end
  211. (completion-table-case-fold completion-list
  212. (not org-contacts-completion-ignore-case)))))
  213. (defun org-contacts-message-complete-function ()
  214. "Function used in `completion-at-point-functions' in `message-mode'."
  215. (let ((mail-abbrev-mode-regexp
  216. "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
  217. (when (mail-abbrev-in-expansion-header-p)
  218. (org-contacts-complete-name))))
  219. (defun org-contacts-gnus-get-name-email ()
  220. "Get name and email address from Gnus message."
  221. (if (gnus-alive-p)
  222. (gnus-with-article-headers
  223. (mail-extract-address-components
  224. (or (mail-fetch-field "From") "")))))
  225. (defun org-contacts-gnus-article-from-get-marker ()
  226. "Return a marker for a contact based on From."
  227. (let* ((address (org-contacts-gnus-get-name-email))
  228. (name (car address))
  229. (email (cadr address)))
  230. (cadar (or (org-contacts-filter
  231. nil
  232. (concat org-contacts-email-property "={\\b" (regexp-quote email) "\\b}"))
  233. (when name
  234. (org-contacts-filter
  235. (concat "^" name "$")))))))
  236. (defun org-contacts-gnus-article-from-goto ()
  237. "Go to contact in the From address of current Gnus message."
  238. (interactive)
  239. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  240. (when marker
  241. (switch-to-buffer-other-window (marker-buffer marker))
  242. (goto-char marker)
  243. (when (eq major-mode 'org-mode)
  244. (org-show-context 'agenda)
  245. (save-excursion
  246. (and (outline-next-heading)
  247. ;; show the next heading
  248. (org-flag-heading nil)))))))
  249. (defun org-contacts-anniversaries (&optional field format)
  250. "Compute FIELD anniversary for each contact, returning FORMAT.
  251. Default FIELD value is \"BIRTHDAY\".
  252. Format is a string matching the following format specification:
  253. %h - Heading name
  254. %l - Link to the heading
  255. %y - Number of year
  256. %Y - Number of year (ordinal)"
  257. (let ((calendar-date-style 'american)
  258. (entry ""))
  259. (unless format (setq format org-contacts-birthday-format))
  260. (loop for contact in (org-contacts-filter)
  261. for anniv = (let ((anniv (cdr (assoc-string
  262. (or field org-contacts-birthday-property)
  263. (caddr contact)))))
  264. (when anniv
  265. (calendar-gregorian-from-absolute
  266. (org-time-string-to-absolute anniv))))
  267. ;; Use `diary-anniversary' to compute anniversary.
  268. if (and anniv (apply 'diary-anniversary anniv))
  269. collect (format-spec format
  270. `((?l . ,(org-with-point-at (cadr contact) (org-store-link nil)))
  271. (?h . ,(car contact))
  272. (?y . ,(- (calendar-extract-year date)
  273. (calendar-extract-year anniv)))
  274. (?Y . ,(let ((years (- (calendar-extract-year date)
  275. (calendar-extract-year anniv))))
  276. (format "%d%s" years (diary-ordinal-suffix years)))))))))
  277. (defun org-completing-read-date (prompt collection
  278. &optional predicate require-match initial-input
  279. hist def inherit-input-method)
  280. "Like `completing-read' but reads a date.
  281. Only PROMPT and DEF are really used."
  282. (org-read-date nil nil nil prompt nil def))
  283. (add-to-list 'org-property-set-functions-alist
  284. `(,org-contacts-birthday-property . org-completing-read-date))
  285. (defun org-contacts-template-name (&optional return-value)
  286. "Try to return the contact name for a template.
  287. If not found return RETURN-VALUE or something that would ask the user."
  288. (or (car (org-contacts-gnus-get-name-email))
  289. return-value
  290. "%^{Name}"))
  291. (defun org-contacts-template-email (&optional return-value)
  292. "Try to return the contact email for a template.
  293. If not found return RETURN-VALUE or something that would ask the user."
  294. (or (cadr (org-contacts-gnus-get-name-email))
  295. return-value
  296. (concat "%^{" org-contacts-email-property "}p")))
  297. (defun org-contacts-gnus-store-last-mail ()
  298. "Store a link between mails and contacts.
  299. This function should be called from `gnus-article-prepare-hook'."
  300. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  301. (when marker
  302. (with-current-buffer (marker-buffer marker)
  303. (save-excursion
  304. (goto-char marker)
  305. (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
  306. org-email-link-description-format))
  307. (link (gnus-with-article-buffer (org-store-link nil))))
  308. (org-set-property org-contacts-last-read-mail-property link)))))))
  309. (defun org-contacts-icon-as-string ()
  310. (let ((image (org-contacts-get-icon)))
  311. (concat
  312. (propertize "-" 'display
  313. (append
  314. (if image
  315. image
  316. `'(space :width (,org-contacts-icon-size)))
  317. '(:ascent center)))
  318. " ")))
  319. ;;;###autoload
  320. (defun org-contacts (name)
  321. "Create agenda view for contacts matching NAME."
  322. (interactive (list (read-string "Name: ")))
  323. (let ((org-agenda-files (org-contacts-files))
  324. (org-agenda-skip-function
  325. (lambda () (org-agenda-skip-if nil `(notregexp ,name))))
  326. (org-agenda-format (propertize
  327. "%(org-contacts-icon-as-string)% p% s%(org-contacts-irc-number-of-unread-messages)%+T"
  328. 'keymap org-contacts-keymap))
  329. (org-agenda-overriding-header
  330. (or org-agenda-overriding-header
  331. (concat "List of contacts matching `" name "':"))))
  332. (setq org-agenda-skip-regexp name)
  333. (org-tags-view nil org-contacts-matcher)
  334. (with-current-buffer org-agenda-buffer-name
  335. (setq org-agenda-redo-command
  336. (list 'org-contacts name)))))
  337. (defun org-contacts-completing-read (prompt
  338. &optional predicate
  339. initial-input hist def inherit-input-method)
  340. "Call `completing-read' with contacts name as collection."
  341. (org-completing-read
  342. prompt (org-contacts-filter) predicate t initial-input hist def inherit-input-method))
  343. (defun org-contacts-format-email (name email)
  344. "Format a mail address."
  345. (unless email
  346. (error "`email' cannot be nul"))
  347. (if name
  348. (concat name " <" email ">")
  349. email))
  350. (defun org-contacts-check-mail-address (mail)
  351. "Add MAIL address to contact at point if it does not have it."
  352. (let ((mails (org-entry-get (point) org-contacts-email-property)))
  353. (unless (member mail (split-string mails))
  354. (when (yes-or-no-p
  355. (format "Do you want to add this address to %s?" (org-get-heading t)))
  356. (org-set-property org-contacts-email-property (concat mails " " mail))))))
  357. (defun org-contacts-gnus-check-mail-address ()
  358. "Check that contact has the current address recorded.
  359. This function should be called from `gnus-article-prepare-hook'."
  360. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  361. (when marker
  362. (org-with-point-at marker
  363. (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
  364. (defun org-contacts-gnus-insinuate ()
  365. "Add some hooks for Gnus user.
  366. This adds `org-contacts-gnus-check-mail-address' and
  367. `org-contacts-gnus-store-last-mail' to
  368. `gnus-article-prepare-hook'. It also adds a binding on `;' in
  369. `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
  370. (require 'gnus)
  371. (require 'gnus-art)
  372. (define-key gnus-summary-mode-map ";" 'org-contacts-gnus-article-from-goto)
  373. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-check-mail-address)
  374. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-store-last-mail))
  375. (when (boundp 'completion-at-point-functions)
  376. (add-hook 'message-mode-hook
  377. (lambda ()
  378. (add-to-list 'completion-at-point-functions
  379. 'org-contacts-message-complete-function))))
  380. (defun org-contacts-wl-get-from-header-content ()
  381. "Retrieve the content of the `From' header of an email.
  382. Works from wl-summary-mode and mime-view-mode - that is while viewing email.
  383. Depends on Wanderlust been loaded."
  384. (with-current-buffer (org-capture-get :original-buffer)
  385. (cond
  386. ((eq major-mode 'wl-summary-mode) (when wl-summary-buffer-elmo-folder
  387. (elmo-message-field
  388. wl-summary-buffer-elmo-folder
  389. (wl-summary-message-number)
  390. 'from)))
  391. ((eq major-mode 'mime-view-mode) (std11-narrow-to-header)
  392. (prog1
  393. (std11-fetch-field "From")
  394. (widen))))))
  395. (defun org-contacts-wl-get-name-email ()
  396. "Get name and email address from Wanderlust email.
  397. See `org-contacts-wl-get-from-header-content' for limitations."
  398. (let ((from (org-contacts-wl-get-from-header-content)))
  399. (when from
  400. (list (wl-address-header-extract-realname from)
  401. (wl-address-header-extract-address from)))))
  402. (defun org-contacts-template-wl-name (&optional return-value)
  403. "Try to return the contact name for a template from wl.
  404. If not found, return RETURN-VALUE or something that would ask the
  405. user."
  406. (or (car (org-contacts-wl-get-name-email))
  407. return-value
  408. "%^{Name}"))
  409. (defun org-contacts-template-wl-email (&optional return-value)
  410. "Try to return the contact email for a template from Wanderlust.
  411. If not found return RETURN-VALUE or something that would ask the user."
  412. (or (cadr (org-contacts-wl-get-name-email))
  413. return-value
  414. (concat "%^{" org-contacts-email-property "}p")))
  415. (defun org-contacts-view-send-email (&optional ask)
  416. "Send email to the contact at point.
  417. If ASK is set, ask for the email address even if there's only one
  418. address."
  419. (interactive "P")
  420. (let ((marker (org-get-at-bol 'org-hd-marker)))
  421. (org-with-point-at marker
  422. (let ((emails (org-entry-get (point) org-contacts-email-property)))
  423. (if emails
  424. (let ((email-list (split-string emails)))
  425. (if (and (= (length email-list) 1) (not ask))
  426. (compose-mail (org-contacts-format-email
  427. (org-get-heading t) emails))
  428. (let ((email (completing-read "Send mail to which address: " email-list)))
  429. (org-contacts-check-mail-address email)
  430. (compose-mail (org-contacts-format-email (org-get-heading t) email)))))
  431. (error (format "This contact has no mail address set (no %s property)."
  432. org-contacts-email-property)))))))
  433. (defun org-contacts-get-icon (&optional pom)
  434. "Get icon for contact at POM."
  435. (setq pom (or pom (point)))
  436. (catch 'icon
  437. ;; Use `org-contacts-icon-property'
  438. (let ((image-data (org-entry-get pom org-contacts-icon-property)))
  439. (when image-data
  440. (throw 'icon
  441. (if (fboundp 'gnus-rescale-image)
  442. (gnus-rescale-image (create-image image-data)
  443. (cons org-contacts-icon-size org-contacts-icon-size))
  444. (create-image image-data)))))
  445. ;; Next, try Gravatar
  446. (when org-contacts-icon-use-gravatar
  447. (let* ((gravatar-size org-contacts-icon-size)
  448. (email-list (org-entry-get pom org-contacts-email-property))
  449. (gravatar
  450. (when email-list
  451. (loop for email in (split-string email-list)
  452. for gravatar = (gravatar-retrieve-synchronously email)
  453. if (and gravatar
  454. (not (eq gravatar 'error)))
  455. return gravatar))))
  456. (when gravatar (throw 'icon gravatar))))))
  457. (defun org-contacts-irc-buffer (&optional pom)
  458. "Get the IRC buffer associated with the entry at POM."
  459. (setq pom (or pom (point)))
  460. (let ((nick (org-entry-get pom org-contacts-nickname-property)))
  461. (when nick
  462. (let ((buffer (get-buffer nick)))
  463. (when buffer
  464. (with-current-buffer buffer
  465. (when (eq major-mode 'erc-mode)
  466. buffer)))))))
  467. (defun org-contacts-irc-number-of-unread-messages (&optional pom)
  468. "Return the number of unread messages for contact at POM."
  469. (when (boundp 'erc-modified-channels-alist)
  470. (let ((number (cadr (assoc (org-contacts-irc-buffer pom) erc-modified-channels-alist))))
  471. (if number
  472. (format (concat "%3d unread message" (if (> number 1) "s" " ") " ") number)
  473. (make-string 21 ? )))))
  474. (defun org-contacts-view-switch-to-irc-buffer ()
  475. "Switch to the IRC buffer of the current contact if it has one."
  476. (interactive)
  477. (let ((marker (org-get-at-bol 'org-hd-marker)))
  478. (org-with-point-at marker
  479. (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
  480. (defun org-contacts-completing-read-nickname (prompt collection
  481. &optional predicate require-match initial-input
  482. hist def inherit-input-method)
  483. "Like `completing-read' but reads a nickname."
  484. (org-completing-read prompt (append collection (erc-nicknames-list)) predicate require-match
  485. initial-input hist def inherit-input-method))
  486. (defun erc-nicknames-list ()
  487. "Return all nicknames of all ERC buffers."
  488. (if (fboundp 'erc-buffer-list)
  489. (loop for buffer in (erc-buffer-list)
  490. nconc (with-current-buffer buffer
  491. (loop for user-entry in (mapcar 'car (erc-get-channel-user-list))
  492. collect (elt user-entry 1))))))
  493. (add-to-list 'org-property-set-functions-alist
  494. `(,org-contacts-nickname-property . org-contacts-completing-read-nickname))
  495. (defun org-contacts-vcard-escape (str)
  496. "Escape ; , and \n in STR for the VCard format."
  497. ;; Thanks to this library for the regexp:
  498. ;; http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el
  499. (when str
  500. (replace-regexp-in-string
  501. "\n" "\\\\n"
  502. (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str))))
  503. (defun org-contacts-vcard-encode-name (name)
  504. "Try to encode NAME as VCard's N property.
  505. The N property expects
  506. FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
  507. Org-contacts does not specify how to encode the name. So we try
  508. to do our best."
  509. (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name) ";;;"))
  510. (defun org-contacts-vcard-format (contact)
  511. "Formats CONTACT in VCard 3.0 format."
  512. (let* ((properties (caddr contact))
  513. (name (org-contacts-vcard-escape (car contact)))
  514. (n (org-contacts-vcard-encode-name name))
  515. (email (org-contacts-vcard-escape (cdr (assoc-string org-contacts-email-property properties))))
  516. (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties))))
  517. (addr (cdr (assoc-string org-contacts-address-property properties)))
  518. (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties))))
  519. (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name)))
  520. (concat head
  521. (when email (format "EMAIL:%s\n" email))
  522. (when addr
  523. (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr)))
  524. (when bday
  525. (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday))))
  526. (format "BDAY:%04d-%02d-%02d\n"
  527. (calendar-extract-year cal-bday)
  528. (calendar-extract-month cal-bday)
  529. (calendar-extract-day cal-bday))))
  530. (when nick (format "NICKNAME:%s\n" nick))
  531. "END:VCARD\n\n")))
  532. (defun org-contacts-export-as-vcard (&optional name file to-buffer)
  533. "Export all contacts matching NAME as VCard 3.0.
  534. If TO-BUFFER is nil, the content is written to FILE or
  535. `org-contacts-vcard-file'. If TO-BUFFER is non-nil, the buffer
  536. is created and the VCard is written into that buffer."
  537. (interactive) ; TODO ask for name?
  538. (let* ((filename (or file org-contacts-vcard-file))
  539. (buffer (if to-buffer
  540. (get-buffer-create to-buffer)
  541. (find-file-noselect filename))))
  542. (message "Exporting...")
  543. (set-buffer buffer)
  544. (let ((inhibit-read-only t)) (erase-buffer))
  545. (fundamental-mode)
  546. (org-install-letbind)
  547. (when (fboundp 'set-buffer-file-coding-system)
  548. (set-buffer-file-coding-system coding-system-for-write))
  549. (loop for contact in (org-contacts-filter name)
  550. do (insert (org-contacts-vcard-format contact)))
  551. (if to-buffer
  552. (current-buffer)
  553. (progn (save-buffer) (kill-buffer)))))
  554. (defun org-contacts-show-map (&optional name)
  555. "Show contacts on a map.
  556. Requires google-maps-el."
  557. (interactive)
  558. (unless (fboundp 'google-maps-static-show)
  559. (error "`org-contacts-show-map' requires `google-maps-el'"))
  560. (google-maps-static-show
  561. :markers
  562. (loop
  563. for contact in (org-contacts-filter name)
  564. for addr = (cdr (assoc-string org-contacts-address-property (caddr contact)))
  565. if addr
  566. collect (cons (list addr) (list :label (string-to-char (car contact)))))))
  567. (provide 'org-contacts)