1 ;;; org-contacts.el --- Contacts management
3 ;; Copyright (C) 2010-2011 Julien Danjou <julien@danjou.info>
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: outlines, hypermedia, calendar
8 ;; This file is NOT part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;; This file contains the code for managing your contacts into Org-mode.
28 ;; To enter new contacts, you can use `org-capture' and a template just like
31 ;; ("c" "Contacts" entry (file "~/Org/contacts.org")
32 ;; "* %(org-contacts-template-name)
34 ;; :EMAIL: %(org-contacts-template-email)
45 (defgroup org-contacts nil
46 "Options concerning contacts management."
49 (defcustom org-contacts-files nil
50 "List of Org files to use as contacts source.
51 If set to nil, all your Org files will be used."
55 (defcustom org-contacts-email-property
"EMAIL"
56 "Name of the property for contact email address."
60 (defcustom org-contacts-address-property
"ADDRESS"
61 "Name of the property for contact address."
65 (defcustom org-contacts-birthday-property
"BIRTHDAY"
66 "Name of the property for contact birthday date."
70 (defcustom org-contacts-birthday-format
"Birthday: %l (%Y)"
71 "Format of the anniversary agenda entry. The following replacements are available:
74 %l - Link to the heading
76 %Y - Number of year (ordinal)"
80 (defcustom org-contacts-last-read-mail-property
"LAST_READ_MAIL"
81 "Name of the property for contact last read email link storage."
85 (defcustom org-contacts-icon-property
"ICON"
86 "Name of the property for contact icon."
90 (defcustom org-contacts-nickname-property
"NICKNAME"
91 "Name of the property for IRC nickname match."
95 (defcustom org-contacts-icon-size
32
96 "Size of the contacts icons."
100 (defcustom org-contacts-icon-use-gravatar
(fboundp 'gravatar-retrieve
)
101 "Whether use Gravatar to fetch contact icons."
103 :group
'org-contacts
)
105 (defcustom org-contacts-completion-ignore-case t
106 "Ignore case when completing contacts."
108 :group
'org-contacts
)
110 (defcustom org-contacts-group-prefix
"+"
113 :group
'org-contacts
)
115 (defcustom org-contacts-matcher
(concat org-contacts-email-property
"<>\"\"")
116 "Matching rule for finding heading that are contacts.
117 This can be a tag name, or a property check."
119 :group
'org-contacts
)
121 (defcustom org-contacts-email-link-description-format
"%s (%d)"
122 "Format used to store links to email.
123 This overrides `org-email-link-description-format' if set."
127 (defcustom org-contacts-vcard-file
"contacts.vcf"
128 "Default file for vcard export."
132 (defvar org-contacts-keymap
133 (let ((map (make-sparse-keymap)))
134 (define-key map
"M" 'org-contacts-view-send-email
)
135 (define-key map
"i" 'org-contacts-view-switch-to-irc-buffer
)
137 "The keymap used in `org-contacts' result list.")
139 (defun org-contacts-files ()
140 "Return list of Org files to use for contact management."
141 (or org-contacts-files
(org-agenda-files t
'ifmode
)))
143 (defun org-contacts-filter (&optional name-match tags-match
)
144 "Search for a contact maching NAME-MATCH and TAGS-MATCH.
145 If both match values are nil, return all contacts."
148 (cdr (org-make-tags-matcher tags-match
))
152 '(org-string-match-p name-match
(org-get-heading t
))
155 (cdr (org-make-tags-matcher org-contacts-matcher
)))
157 (dolist (file (org-contacts-files))
158 (org-check-agenda-file file
)
159 (with-current-buffer (org-get-agenda-file-buffer file
)
161 (error "File %s is no in `org-mode'" file
))
163 '(add-to-list 'markers
(set-marker (make-marker) (point)))
164 `(and ,contacts-matcher
,tags-matcher
,name-matcher
))))
165 (dolist (marker markers result
)
166 (org-with-point-at marker
168 (list (org-get-heading t
) marker
(org-entry-properties marker
'all
)))))))
170 (when (not (fboundp 'completion-table-case-fold
))
171 ;; That function is new in Emacs 24...
172 (defun completion-table-case-fold (table string pred action
)
173 (let ((completion-ignore-case t
))
174 (complete-with-action action table string pred
))))
176 (defun org-contacts-complete-name (&optional start
)
177 "Complete text at START with a user name and email."
181 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
182 (goto-char (match-end 0))
184 (orig (buffer-substring start end
))
185 (completion-ignore-case org-contacts-completion-ignore-case
)
186 (group-completion-p (org-string-match-p (concat "^" org-contacts-group-prefix
) orig
))
188 (if group-completion-p
189 (mapcar (lambda (group) (propertize (concat org-contacts-group-prefix group
) 'org-contacts-group group
))
191 (loop for contact in
(org-contacts-filter)
193 nconc
(org-split-string
194 (or (cdr (assoc-string "ALLTAGS" (caddr contact
))) "") ":"))))
195 (loop for contact in
(org-contacts-filter)
196 ;; The contact name is always the car of the assoc-list
197 ;; returned by `org-contacts-filter'.
198 for contact-name
= (car contact
)
199 ;; Build the list of the user email addresses.
200 for email-list
= (split-string (or
201 (cdr (assoc-string org-contacts-email-property
(caddr contact
)))
203 ;; If the user has email addresses…
205 ;; … append a list of USER <EMAIL>.
206 nconc
(loop for email in email-list
207 collect
(org-contacts-format-email contact-name email
)))))
208 (completion-list (all-completions orig completion-list
)))
209 ;; If we are completing a group, and that's the only group, just return
211 (when (and group-completion-p
212 (= (length completion-list
) 1))
213 (setq completion-list
214 (list (concat (car completion-list
) ";: "
216 (loop for contact in
(org-contacts-filter
218 (get-text-property 0 'org-contacts-group
(car completion-list
)))
219 ;; The contact name is always the car of the assoc-list
220 ;; returned by `org-contacts-filter'.
221 for contact-name
= (car contact
)
222 ;; Grab the first email of the contact
223 for email
= (car (split-string (or
224 (cdr (assoc-string org-contacts-email-property
(caddr contact
)))
226 ;; If the user has an email address, append USER <EMAIL>.
227 if email collect
(org-contacts-format-email contact-name email
))
229 (list start end
(if org-contacts-completion-ignore-case
230 (apply-partially #'completion-table-case-fold completion-list
)
233 (defun org-contacts-message-complete-function ()
234 "Function used in `completion-at-point-functions' in `message-mode'."
235 (let ((mail-abbrev-mode-regexp
236 "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
237 (when (mail-abbrev-in-expansion-header-p)
238 (org-contacts-complete-name))))
240 (defun org-contacts-gnus-get-name-email ()
241 "Get name and email address from Gnus message."
243 (gnus-with-article-headers
244 (mail-extract-address-components
245 (or (mail-fetch-field "From") "")))))
247 (defun org-contacts-gnus-article-from-get-marker ()
248 "Return a marker for a contact based on From."
249 (let* ((address (org-contacts-gnus-get-name-email))
251 (email (cadr address
)))
252 (cadar (or (org-contacts-filter
254 (concat org-contacts-email-property
"={\\b" (regexp-quote email
) "\\b}"))
257 (concat "^" name
"$")))))))
259 (defun org-contacts-gnus-article-from-goto ()
260 "Go to contact in the From address of current Gnus message."
262 (let ((marker (org-contacts-gnus-article-from-get-marker)))
264 (switch-to-buffer-other-window (marker-buffer marker
))
267 (org-show-context 'agenda
)
269 (and (outline-next-heading)
270 ;; show the next heading
271 (org-flag-heading nil
)))))))
273 (defun org-contacts-anniversaries (&optional field format
)
274 "Compute FIELD anniversary for each contact, returning FORMAT.
275 Default FIELD value is \"BIRTHDAY\".
277 Format is a string matching the following format specification:
280 %l - Link to the heading
282 %Y - Number of year (ordinal)"
283 (let ((calendar-date-style 'american
)
285 (unless format
(setq format org-contacts-birthday-format
))
286 (loop for contact in
(org-contacts-filter)
287 for anniv
= (let ((anniv (cdr (assoc-string
288 (or field org-contacts-birthday-property
)
291 (calendar-gregorian-from-absolute
292 (org-time-string-to-absolute anniv
))))
293 ;; Use `diary-anniversary' to compute anniversary.
294 if
(and anniv
(apply 'diary-anniversary anniv
))
295 collect
(format-spec format
296 `((?l .
,(org-with-point-at (cadr contact
) (org-store-link nil
)))
297 (?h .
,(car contact
))
298 (?y .
,(- (calendar-extract-year date
)
299 (calendar-extract-year anniv
)))
300 (?Y .
,(let ((years (- (calendar-extract-year date
)
301 (calendar-extract-year anniv
))))
302 (format "%d%s" years
(diary-ordinal-suffix years
)))))))))
304 (defun org-completing-read-date (prompt collection
305 &optional predicate require-match initial-input
306 hist def inherit-input-method
)
307 "Like `completing-read' but reads a date.
308 Only PROMPT and DEF are really used."
309 (org-read-date nil nil nil prompt nil def
))
311 (add-to-list 'org-property-set-functions-alist
312 `(,org-contacts-birthday-property . org-completing-read-date
))
314 (defun org-contacts-template-name (&optional return-value
)
315 "Try to return the contact name for a template.
316 If not found return RETURN-VALUE or something that would ask the user."
317 (or (car (org-contacts-gnus-get-name-email))
321 (defun org-contacts-template-email (&optional return-value
)
322 "Try to return the contact email for a template.
323 If not found return RETURN-VALUE or something that would ask the user."
324 (or (cadr (org-contacts-gnus-get-name-email))
326 (concat "%^{" org-contacts-email-property
"}p")))
328 (defun org-contacts-gnus-store-last-mail ()
329 "Store a link between mails and contacts.
331 This function should be called from `gnus-article-prepare-hook'."
332 (let ((marker (org-contacts-gnus-article-from-get-marker)))
334 (with-current-buffer (marker-buffer marker
)
337 (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
338 org-email-link-description-format
))
339 (link (gnus-with-article-buffer (org-store-link nil
))))
340 (org-set-property org-contacts-last-read-mail-property link
)))))))
342 (defun org-contacts-icon-as-string ()
343 (let ((image (org-contacts-get-icon)))
345 (propertize "-" 'display
349 `'(space :width
(,org-contacts-icon-size
)))
354 (defun org-contacts (name)
355 "Create agenda view for contacts matching NAME."
356 (interactive (list (read-string "Name: ")))
357 (let ((org-agenda-files (org-contacts-files))
358 (org-agenda-skip-function
359 (lambda () (org-agenda-skip-if nil
`(notregexp ,name
))))
360 (org-agenda-format (propertize
361 "%(org-contacts-icon-as-string)% p% s%(org-contacts-irc-number-of-unread-messages)%+T"
362 'keymap org-contacts-keymap
))
363 (org-agenda-overriding-header
364 (or org-agenda-overriding-header
365 (concat "List of contacts matching `" name
"':"))))
366 (setq org-agenda-skip-regexp name
)
367 (org-tags-view nil org-contacts-matcher
)
368 (with-current-buffer org-agenda-buffer-name
369 (setq org-agenda-redo-command
370 (list 'org-contacts name
)))))
372 (defun org-contacts-completing-read (prompt
374 initial-input hist def inherit-input-method
)
375 "Call `completing-read' with contacts name as collection."
377 prompt
(org-contacts-filter) predicate t initial-input hist def inherit-input-method
))
379 (defun org-contacts-format-email (name email
)
380 "Format a mail address."
382 (error "`email' cannot be nul"))
384 (concat name
" <" email
">")
387 (defun org-contacts-check-mail-address (mail)
388 "Add MAIL address to contact at point if it does not have it."
389 (let ((mails (org-entry-get (point) org-contacts-email-property
)))
390 (unless (member mail
(split-string mails
))
392 (format "Do you want to this address to %s?" (org-get-heading t
)))
393 (org-set-property org-contacts-email-property
(concat mails
" " mail
))))))
395 (defun org-contacts-gnus-check-mail-address ()
396 "Check that contact has the current address recorded.
397 This function should be called from `gnus-article-prepare-hook'."
398 (let ((marker (org-contacts-gnus-article-from-get-marker)))
400 (org-with-point-at marker
401 (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
403 (defun org-contacts-gnus-insinuate ()
404 "Add some hooks for Gnus user.
405 This adds `org-contacts-gnus-check-mail-address' and
406 `org-contacts-gnus-store-last-mail' to
407 `gnus-article-prepare-hook'. It also adds a binding on `;' in
408 `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
411 (define-key gnus-summary-mode-map
";" 'org-contacts-gnus-article-from-goto
)
412 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-check-mail-address
)
413 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-store-last-mail
))
415 (when (boundp 'completion-at-point-functions
)
416 (add-hook 'message-mode-hook
418 (add-to-list 'completion-at-point-functions
419 'org-contacts-message-complete-function
))))
421 (defun org-contacts-wl-get-from-header-content ()
422 "Retrieve the content of the `From' header of an email.
423 Works from wl-summary-mode and mime-view-mode - that is while viewing email.
424 Depends on Wanderlust been loaded."
425 (with-current-buffer (org-capture-get :original-buffer
)
427 ((eq major-mode
'wl-summary-mode
) (when wl-summary-buffer-elmo-folder
429 wl-summary-buffer-elmo-folder
430 (wl-summary-message-number)
432 ((eq major-mode
'mime-view-mode
) (std11-narrow-to-header)
434 (std11-fetch-field "From")
437 (defun org-contacts-wl-get-name-email ()
438 "Get name and email address from wanderlust email.
439 See `org-contacts-wl-get-from-header-content' for limitations."
440 (let ((from (org-contacts-wl-get-from-header-content)))
442 (list (wl-address-header-extract-realname from
)
443 (wl-address-header-extract-address from
)))))
445 (defun org-contacts-template-wl-name (&optional return-value
)
446 "Try to return the contact name for a template from wl.
447 If not found return RETURN-VALUE or something that would ask the user."
448 (or (car (org-contacts-wl-get-name-email))
452 (defun org-contacts-template-wl-email (&optional return-value
)
453 "Try to return the contact email for a template from wl.
454 If not found return RETURN-VALUE or something that would ask the user."
455 (or (cadr (org-contacts-wl-get-name-email))
457 (concat "%^{" org-contacts-email-property
"}p")))
459 (defun org-contacts-view-send-email (&optional ask
)
460 "Send email to the contact at point.
461 If ASK is set, ask for the email address even if there's only one address."
463 (let ((marker (org-get-at-bol 'org-hd-marker
)))
464 (org-with-point-at marker
465 (let ((emails (org-entry-get (point) org-contacts-email-property
)))
467 (let ((email-list (split-string emails
)))
468 (if (and (= (length email-list
) 1) (not ask
))
469 (compose-mail (org-contacts-format-email
470 (org-get-heading t
) emails
))
471 (let ((email (completing-read "Send mail to which address: " email-list
)))
472 (org-contacts-check-mail-address email
)
473 (compose-mail (org-contacts-format-email (org-get-heading t
) email
)))))
474 (error (format "This contact has no mail address set (no %s property)."
475 org-contacts-email-property
)))))))
477 (defun org-contacts-get-icon (&optional pom
)
478 "Get icon for contact at POM."
479 (setq pom
(or pom
(point)))
481 ;; Use `org-contacts-icon-property'
482 (let ((image-data (org-entry-get pom org-contacts-icon-property
)))
485 (if (fboundp 'gnus-rescale-image
)
486 (gnus-rescale-image (create-image image-data
)
487 (cons org-contacts-icon-size org-contacts-icon-size
))
488 (create-image image-data
)))))
489 ;; Next, try Gravatar
490 (when org-contacts-icon-use-gravatar
491 (let* ((gravatar-size org-contacts-icon-size
)
492 (email-list (org-entry-get pom org-contacts-email-property
))
495 (loop for email in
(split-string email-list
)
496 for gravatar
= (gravatar-retrieve-synchronously email
)
498 (not (eq gravatar
'error
)))
500 (when gravatar
(throw 'icon gravatar
))))))
502 (defun org-contacts-irc-buffer (&optional pom
)
503 "Get the IRC buffer associated with the entry at POM."
504 (setq pom
(or pom
(point)))
505 (let ((nick (org-entry-get pom org-contacts-nickname-property
)))
507 (let ((buffer (get-buffer nick
)))
509 (with-current-buffer buffer
510 (when (eq major-mode
'erc-mode
)
513 (defun org-contacts-irc-number-of-unread-messages (&optional pom
)
514 "Return the number of unread messages for contact at POM."
515 (when (boundp 'erc-modified-channels-alist
)
516 (let ((number (cadr (assoc (org-contacts-irc-buffer pom
) erc-modified-channels-alist
))))
518 (format (concat "%3d unread message" (if (> number
1) "s" " ") " ") number
)
519 (make-string 21 ?
)))))
521 (defun org-contacts-view-switch-to-irc-buffer ()
522 "Switch to the IRC buffer of the current contact if it has one."
524 (let ((marker (org-get-at-bol 'org-hd-marker
)))
525 (org-with-point-at marker
526 (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
528 (defun org-contacts-completing-read-nickname (prompt collection
529 &optional predicate require-match initial-input
530 hist def inherit-input-method
)
531 "Like `completing-read' but reads a nickname."
532 (org-completing-read prompt
(append collection
(erc-nicknames-list)) predicate require-match
533 initial-input hist def inherit-input-method
))
535 (defun erc-nicknames-list ()
536 "Return all nicknames of all ERC buffers."
537 (if (fboundp 'erc-buffer-list
)
538 (loop for buffer in
(erc-buffer-list)
539 nconc
(with-current-buffer buffer
540 (loop for user-entry in
(mapcar 'car
(erc-get-channel-user-list))
541 collect
(elt user-entry
1))))))
543 (add-to-list 'org-property-set-functions-alist
544 `(,org-contacts-nickname-property . org-contacts-completing-read-nickname
))
546 (defun org-contacts-vcard-escape (str)
547 "Escape ; , and \n in STR for use in the VCard format.
548 Thanks to http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el for the regexp."
550 (replace-regexp-in-string "\n" "\\\\n" (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str
))))
552 (defun org-contacts-vcard-encode-name (name)
553 "Try to encode NAME as VCard's N property. The N property expects FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
554 Org-contacts does not specify how to encode the name. So we try to do our best."
555 (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name
) ";;;"))
557 (defun org-contacts-vcard-format (contact)
558 "Formats CONTACT in VCard 3.0 format."
559 (let* ((properties (caddr contact
))
560 (name (org-contacts-vcard-escape (car contact
)))
561 (n (org-contacts-vcard-encode-name name
))
562 (email (org-contacts-vcard-escape (cdr (assoc-string org-contacts-email-property properties
))))
563 (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties
))))
564 (addr (cdr (assoc-string org-contacts-address-property properties
)))
565 (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties
))))
567 (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name
)))
569 (when email
(format "EMAIL:%s\n" email
))
571 (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr
)))
573 (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday
))))
574 (format "BDAY:%04d-%02d-%02d\n"
575 (calendar-extract-year cal-bday
)
576 (calendar-extract-month cal-bday
)
577 (calendar-extract-day cal-bday
))))
578 (when nick
(format "NICKNAME:%s\n" nick
))
581 (defun org-contacts-export-as-vcard (&optional name file to-buffer
)
582 "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."
583 (interactive) ; TODO ask for name?
584 (let* ((filename (or file org-contacts-vcard-file
))
585 (buffer (if to-buffer
586 (get-buffer-create to-buffer
)
587 (find-file-noselect filename
))))
589 (message "Exporting...")
592 (let ((inhibit-read-only t
)) (erase-buffer))
594 (org-install-letbind)
596 (when (fboundp 'set-buffer-file-coding-system
)
597 (set-buffer-file-coding-system coding-system-for-write
))
599 (loop for contact in
(org-contacts-filter name
)
600 do
(insert (org-contacts-vcard-format contact
)))
604 (progn (save-buffer) (kill-buffer)))))
606 (defun org-contacts-show-map (&optional name
)
607 "Show contacts on a map. Requires google-maps-el."
609 (unless (fboundp 'google-maps-static-show
)
610 (error "org-contacts-show-map requires google-maps-el."))
611 (google-maps-static-show
614 for contact in
(org-contacts-filter name
)
615 for addr
= (cdr (assoc-string org-contacts-address-property
(caddr contact
)))
617 collect
(cons (list addr
) (list :label
(string-to-char (car contact
)))))))
619 (provide 'org-contacts
)