org-contacts.el 26 KB

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