org-contacts.el 26 KB

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