org-contacts.el 26 KB

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