org-contacts.el 26 KB

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