1 ;;; org-contacts.el --- Contacts management
3 ;; Copyright (C) 2010-2012 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)
46 (defgroup org-contacts nil
47 "Options concerning contacts management."
50 (defcustom org-contacts-files nil
51 "List of Org files to use as contacts source.
52 If set to nil, all your Org files will be used."
56 (defcustom org-contacts-email-property
"EMAIL"
57 "Name of the property for contact email address."
61 (defcustom org-contacts-address-property
"ADDRESS"
62 "Name of the property for contact address."
66 (defcustom org-contacts-birthday-property
"BIRTHDAY"
67 "Name of the property for contact birthday date."
71 (defcustom org-contacts-birthday-format
"Birthday: %l (%Y)"
72 "Format of the anniversary agenda entry. The following replacements are available:
75 %l - Link to the heading
77 %Y - Number of year (ordinal)"
81 (defcustom org-contacts-last-read-mail-property
"LAST_READ_MAIL"
82 "Name of the property for contact last read email link storage."
86 (defcustom org-contacts-icon-property
"ICON"
87 "Name of the property for contact icon."
91 (defcustom org-contacts-nickname-property
"NICKNAME"
92 "Name of the property for IRC nickname match."
96 (defcustom org-contacts-icon-size
32
97 "Size of the contacts icons."
101 (defcustom org-contacts-icon-use-gravatar
(fboundp 'gravatar-retrieve
)
102 "Whether use Gravatar to fetch contact icons."
104 :group
'org-contacts
)
106 (defcustom org-contacts-completion-ignore-case t
107 "Ignore case when completing contacts."
109 :group
'org-contacts
)
111 (defcustom org-contacts-group-prefix
"+"
114 :group
'org-contacts
)
116 (defcustom org-contacts-matcher
(concat org-contacts-email-property
"<>\"\"")
117 "Matching rule for finding heading that are contacts.
118 This can be a tag name, or a property check."
120 :group
'org-contacts
)
122 (defcustom org-contacts-email-link-description-format
"%s (%d)"
123 "Format used to store links to email.
124 This overrides `org-email-link-description-format' if set."
128 (defcustom org-contacts-vcard-file
"contacts.vcf"
129 "Default file for vcard export."
133 (defvar org-contacts-keymap
134 (let ((map (make-sparse-keymap)))
135 (define-key map
"M" 'org-contacts-view-send-email
)
136 (define-key map
"i" 'org-contacts-view-switch-to-irc-buffer
)
138 "The keymap used in `org-contacts' result list.")
140 (defun org-contacts-files ()
141 "Return list of Org files to use for contact management."
142 (or org-contacts-files
(org-agenda-files t
'ifmode
)))
144 (defun org-contacts-filter (&optional name-match tags-match
)
145 "Search for a contact maching NAME-MATCH and TAGS-MATCH.
146 If both match values are nil, return all contacts."
150 (cdr (org-make-tags-matcher tags-match
))
154 '(org-string-match-p name-match
(org-get-heading t
))
157 (cdr (org-make-tags-matcher org-contacts-matcher
)))
159 (dolist (file (org-contacts-files))
160 (org-check-agenda-file file
)
161 (with-current-buffer (org-get-agenda-file-buffer file
)
162 (unless (eq major-mode
'org-mode
)
163 (error "File %s is no in `org-mode'" file
))
165 '(add-to-list 'markers
(set-marker (make-marker) (point)))
166 `(and ,contacts-matcher
,tags-matcher
,name-matcher
)
168 (dolist (marker markers result
)
169 (org-with-point-at marker
171 (list (org-get-heading t
) marker
(org-entry-properties marker
'all
)))))))
173 (when (not (fboundp 'completion-table-case-fold
))
174 ;; That function is new in Emacs 24...
175 (defun completion-table-case-fold (table &optional dont-fold
)
176 (lambda (string pred action
)
177 (let ((completion-ignore-case (not dont-fold
)))
178 (complete-with-action action table string pred
)))))
180 (defun org-contacts-complete-name (&optional start
)
181 "Complete text at START with a user name and email."
185 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
186 (goto-char (match-end 0))
188 (orig (buffer-substring start end
))
189 (completion-ignore-case org-contacts-completion-ignore-case
)
190 (group-completion-p (org-string-match-p (concat "^" org-contacts-group-prefix
) orig
))
192 (if group-completion-p
193 (mapcar (lambda (group) (propertize (concat org-contacts-group-prefix group
) 'org-contacts-group group
))
195 (loop for contact in
(org-contacts-filter)
197 nconc
(org-split-string
198 (or (cdr (assoc-string "ALLTAGS" (caddr contact
))) "") ":"))))
199 (loop for contact in
(org-contacts-filter)
200 ;; The contact name is always the car of the assoc-list
201 ;; returned by `org-contacts-filter'.
202 for contact-name
= (car contact
)
203 ;; Build the list of the user email addresses.
204 for email-list
= (split-string (or
205 (cdr (assoc-string org-contacts-email-property
(caddr contact
)))
207 ;; If the user has email addresses…
209 ;; … append a list of USER <EMAIL>.
210 nconc
(loop for email in email-list
211 collect
(org-contacts-format-email contact-name email
)))))
212 (completion-list (all-completions orig completion-list
)))
213 ;; If we are completing a group, and that's the only group, just return
215 (when (and group-completion-p
216 (= (length completion-list
) 1))
217 (setq completion-list
218 (list (concat (car completion-list
) ";: "
220 (loop for contact in
(org-contacts-filter
222 (get-text-property 0 'org-contacts-group
(car completion-list
)))
223 ;; The contact name is always the car of the assoc-list
224 ;; returned by `org-contacts-filter'.
225 for contact-name
= (car contact
)
226 ;; Grab the first email of the contact
227 for email
= (car (split-string (or
228 (cdr (assoc-string org-contacts-email-property
(caddr contact
)))
230 ;; If the user has an email address, append USER <EMAIL>.
231 if email collect
(org-contacts-format-email contact-name email
))
233 (list start end
(completion-table-case-fold completion-list
(not org-contacts-completion-ignore-case
)))))
235 (defun org-contacts-message-complete-function ()
236 "Function used in `completion-at-point-functions' in `message-mode'."
237 (let ((mail-abbrev-mode-regexp
238 "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
239 (when (mail-abbrev-in-expansion-header-p)
240 (org-contacts-complete-name))))
242 (defun org-contacts-gnus-get-name-email ()
243 "Get name and email address from Gnus message."
245 (gnus-with-article-headers
246 (mail-extract-address-components
247 (or (mail-fetch-field "From") "")))))
249 (defun org-contacts-gnus-article-from-get-marker ()
250 "Return a marker for a contact based on From."
251 (let* ((address (org-contacts-gnus-get-name-email))
253 (email (cadr address
)))
254 (cadar (or (org-contacts-filter
256 (concat org-contacts-email-property
"={\\b" (regexp-quote email
) "\\b}"))
259 (concat "^" name
"$")))))))
261 (defun org-contacts-gnus-article-from-goto ()
262 "Go to contact in the From address of current Gnus message."
264 (let ((marker (org-contacts-gnus-article-from-get-marker)))
266 (switch-to-buffer-other-window (marker-buffer marker
))
268 (when (eq major-mode
'org-mode
)
269 (org-show-context 'agenda
)
271 (and (outline-next-heading)
272 ;; show the next heading
273 (org-flag-heading nil
)))))))
275 (defun org-contacts-anniversaries (&optional field format
)
276 "Compute FIELD anniversary for each contact, returning FORMAT.
277 Default FIELD value is \"BIRTHDAY\".
279 Format is a string matching the following format specification:
282 %l - Link to the heading
284 %Y - Number of year (ordinal)"
285 (let ((calendar-date-style 'american
)
287 (unless format
(setq format org-contacts-birthday-format
))
288 (loop for contact in
(org-contacts-filter)
289 for anniv
= (let ((anniv (cdr (assoc-string
290 (or field org-contacts-birthday-property
)
293 (calendar-gregorian-from-absolute
294 (org-time-string-to-absolute anniv
))))
295 ;; Use `diary-anniversary' to compute anniversary.
296 if
(and anniv
(apply 'diary-anniversary anniv
))
297 collect
(format-spec format
298 `((?l .
,(org-with-point-at (cadr contact
) (org-store-link nil
)))
299 (?h .
,(car contact
))
300 (?y .
,(- (calendar-extract-year date
)
301 (calendar-extract-year anniv
)))
302 (?Y .
,(let ((years (- (calendar-extract-year date
)
303 (calendar-extract-year anniv
))))
304 (format "%d%s" years
(diary-ordinal-suffix years
)))))))))
306 (defun org-completing-read-date (prompt collection
307 &optional predicate require-match initial-input
308 hist def inherit-input-method
)
309 "Like `completing-read' but reads a date.
310 Only PROMPT and DEF are really used."
311 (org-read-date nil nil nil prompt nil def
))
313 (add-to-list 'org-property-set-functions-alist
314 `(,org-contacts-birthday-property . org-completing-read-date
))
316 (defun org-contacts-template-name (&optional return-value
)
317 "Try to return the contact name for a template.
318 If not found return RETURN-VALUE or something that would ask the user."
319 (or (car (org-contacts-gnus-get-name-email))
323 (defun org-contacts-template-email (&optional return-value
)
324 "Try to return the contact email for a template.
325 If not found return RETURN-VALUE or something that would ask the user."
326 (or (cadr (org-contacts-gnus-get-name-email))
328 (concat "%^{" org-contacts-email-property
"}p")))
330 (defun org-contacts-gnus-store-last-mail ()
331 "Store a link between mails and contacts.
333 This function should be called from `gnus-article-prepare-hook'."
334 (let ((marker (org-contacts-gnus-article-from-get-marker)))
336 (with-current-buffer (marker-buffer marker
)
339 (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
340 org-email-link-description-format
))
341 (link (gnus-with-article-buffer (org-store-link nil
))))
342 (org-set-property org-contacts-last-read-mail-property link
)))))))
344 (defun org-contacts-icon-as-string ()
345 (let ((image (org-contacts-get-icon)))
347 (propertize "-" 'display
351 `'(space :width
(,org-contacts-icon-size
)))
356 (defun org-contacts (name)
357 "Create agenda view for contacts matching NAME."
358 (interactive (list (read-string "Name: ")))
359 (let ((org-agenda-files (org-contacts-files))
360 (org-agenda-skip-function
361 (lambda () (org-agenda-skip-if nil
`(notregexp ,name
))))
362 (org-agenda-format (propertize
363 "%(org-contacts-icon-as-string)% p% s%(org-contacts-irc-number-of-unread-messages)%+T"
364 'keymap org-contacts-keymap
))
365 (org-agenda-overriding-header
366 (or org-agenda-overriding-header
367 (concat "List of contacts matching `" name
"':"))))
368 (setq org-agenda-skip-regexp name
)
369 (org-tags-view nil org-contacts-matcher
)
370 (with-current-buffer org-agenda-buffer-name
371 (setq org-agenda-redo-command
372 (list 'org-contacts name
)))))
374 (defun org-contacts-completing-read (prompt
376 initial-input hist def inherit-input-method
)
377 "Call `completing-read' with contacts name as collection."
379 prompt
(org-contacts-filter) predicate t initial-input hist def inherit-input-method
))
381 (defun org-contacts-format-email (name email
)
382 "Format a mail address."
384 (error "`email' cannot be nul"))
386 (concat name
" <" email
">")
389 (defun org-contacts-check-mail-address (mail)
390 "Add MAIL address to contact at point if it does not have it."
391 (let ((mails (org-entry-get (point) org-contacts-email-property
)))
392 (unless (member mail
(split-string mails
))
394 (format "Do you want to add this address to %s?" (org-get-heading t
)))
395 (org-set-property org-contacts-email-property
(concat mails
" " mail
))))))
397 (defun org-contacts-gnus-check-mail-address ()
398 "Check that contact has the current address recorded.
399 This function should be called from `gnus-article-prepare-hook'."
400 (let ((marker (org-contacts-gnus-article-from-get-marker)))
402 (org-with-point-at marker
403 (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
405 (defun org-contacts-gnus-insinuate ()
406 "Add some hooks for Gnus user.
407 This adds `org-contacts-gnus-check-mail-address' and
408 `org-contacts-gnus-store-last-mail' to
409 `gnus-article-prepare-hook'. It also adds a binding on `;' in
410 `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
413 (define-key gnus-summary-mode-map
";" 'org-contacts-gnus-article-from-goto
)
414 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-check-mail-address
)
415 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-store-last-mail
))
417 (when (boundp 'completion-at-point-functions
)
418 (add-hook 'message-mode-hook
420 (add-to-list 'completion-at-point-functions
421 'org-contacts-message-complete-function
))))
423 (defun org-contacts-wl-get-from-header-content ()
424 "Retrieve the content of the `From' header of an email.
425 Works from wl-summary-mode and mime-view-mode - that is while viewing email.
426 Depends on Wanderlust been loaded."
427 (with-current-buffer (org-capture-get :original-buffer
)
429 ((eq major-mode
'wl-summary-mode
) (when wl-summary-buffer-elmo-folder
431 wl-summary-buffer-elmo-folder
432 (wl-summary-message-number)
434 ((eq major-mode
'mime-view-mode
) (std11-narrow-to-header)
436 (std11-fetch-field "From")
439 (defun org-contacts-wl-get-name-email ()
440 "Get name and email address from wanderlust email.
441 See `org-contacts-wl-get-from-header-content' for limitations."
442 (let ((from (org-contacts-wl-get-from-header-content)))
444 (list (wl-address-header-extract-realname from
)
445 (wl-address-header-extract-address from
)))))
447 (defun org-contacts-template-wl-name (&optional return-value
)
448 "Try to return the contact name for a template from wl.
449 If not found return RETURN-VALUE or something that would ask the user."
450 (or (car (org-contacts-wl-get-name-email))
454 (defun org-contacts-template-wl-email (&optional return-value
)
455 "Try to return the contact email for a template from wl.
456 If not found return RETURN-VALUE or something that would ask the user."
457 (or (cadr (org-contacts-wl-get-name-email))
459 (concat "%^{" org-contacts-email-property
"}p")))
461 (defun org-contacts-view-send-email (&optional ask
)
462 "Send email to the contact at point.
463 If ASK is set, ask for the email address even if there's only one address."
465 (let ((marker (org-get-at-bol 'org-hd-marker
)))
466 (org-with-point-at marker
467 (let ((emails (org-entry-get (point) org-contacts-email-property
)))
469 (let ((email-list (split-string emails
)))
470 (if (and (= (length email-list
) 1) (not ask
))
471 (compose-mail (org-contacts-format-email
472 (org-get-heading t
) emails
))
473 (let ((email (completing-read "Send mail to which address: " email-list
)))
474 (org-contacts-check-mail-address email
)
475 (compose-mail (org-contacts-format-email (org-get-heading t
) email
)))))
476 (error (format "This contact has no mail address set (no %s property)."
477 org-contacts-email-property
)))))))
479 (defun org-contacts-get-icon (&optional pom
)
480 "Get icon for contact at POM."
481 (setq pom
(or pom
(point)))
483 ;; Use `org-contacts-icon-property'
484 (let ((image-data (org-entry-get pom org-contacts-icon-property
)))
487 (if (fboundp 'gnus-rescale-image
)
488 (gnus-rescale-image (create-image image-data
)
489 (cons org-contacts-icon-size org-contacts-icon-size
))
490 (create-image image-data
)))))
491 ;; Next, try Gravatar
492 (when org-contacts-icon-use-gravatar
493 (let* ((gravatar-size org-contacts-icon-size
)
494 (email-list (org-entry-get pom org-contacts-email-property
))
497 (loop for email in
(split-string email-list
)
498 for gravatar
= (gravatar-retrieve-synchronously email
)
500 (not (eq gravatar
'error
)))
502 (when gravatar
(throw 'icon gravatar
))))))
504 (defun org-contacts-irc-buffer (&optional pom
)
505 "Get the IRC buffer associated with the entry at POM."
506 (setq pom
(or pom
(point)))
507 (let ((nick (org-entry-get pom org-contacts-nickname-property
)))
509 (let ((buffer (get-buffer nick
)))
511 (with-current-buffer buffer
512 (when (eq major-mode
'erc-mode
)
515 (defun org-contacts-irc-number-of-unread-messages (&optional pom
)
516 "Return the number of unread messages for contact at POM."
517 (when (boundp 'erc-modified-channels-alist
)
518 (let ((number (cadr (assoc (org-contacts-irc-buffer pom
) erc-modified-channels-alist
))))
520 (format (concat "%3d unread message" (if (> number
1) "s" " ") " ") number
)
521 (make-string 21 ?
)))))
523 (defun org-contacts-view-switch-to-irc-buffer ()
524 "Switch to the IRC buffer of the current contact if it has one."
526 (let ((marker (org-get-at-bol 'org-hd-marker
)))
527 (org-with-point-at marker
528 (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
530 (defun org-contacts-completing-read-nickname (prompt collection
531 &optional predicate require-match initial-input
532 hist def inherit-input-method
)
533 "Like `completing-read' but reads a nickname."
534 (org-completing-read prompt
(append collection
(erc-nicknames-list)) predicate require-match
535 initial-input hist def inherit-input-method
))
537 (defun erc-nicknames-list ()
538 "Return all nicknames of all ERC buffers."
539 (if (fboundp 'erc-buffer-list
)
540 (loop for buffer in
(erc-buffer-list)
541 nconc
(with-current-buffer buffer
542 (loop for user-entry in
(mapcar 'car
(erc-get-channel-user-list))
543 collect
(elt user-entry
1))))))
545 (add-to-list 'org-property-set-functions-alist
546 `(,org-contacts-nickname-property . org-contacts-completing-read-nickname
))
548 (defun org-contacts-vcard-escape (str)
549 "Escape ; , and \n in STR for use in the VCard format.
550 Thanks to http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el for the regexp."
552 (replace-regexp-in-string "\n" "\\\\n" (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str
))))
554 (defun org-contacts-vcard-encode-name (name)
555 "Try to encode NAME as VCard's N property. The N property expects FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
556 Org-contacts does not specify how to encode the name. So we try to do our best."
557 (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name
) ";;;"))
559 (defun org-contacts-vcard-format (contact)
560 "Formats CONTACT in VCard 3.0 format."
561 (let* ((properties (caddr contact
))
562 (name (org-contacts-vcard-escape (car contact
)))
563 (n (org-contacts-vcard-encode-name name
))
564 (email (org-contacts-vcard-escape (cdr (assoc-string org-contacts-email-property properties
))))
565 (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties
))))
566 (addr (cdr (assoc-string org-contacts-address-property properties
)))
567 (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties
))))
569 (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name
)))
571 (when email
(format "EMAIL:%s\n" email
))
573 (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr
)))
575 (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday
))))
576 (format "BDAY:%04d-%02d-%02d\n"
577 (calendar-extract-year cal-bday
)
578 (calendar-extract-month cal-bday
)
579 (calendar-extract-day cal-bday
))))
580 (when nick
(format "NICKNAME:%s\n" nick
))
583 (defun org-contacts-export-as-vcard (&optional name file to-buffer
)
584 "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."
585 (interactive) ; TODO ask for name?
586 (let* ((filename (or file org-contacts-vcard-file
))
587 (buffer (if to-buffer
588 (get-buffer-create to-buffer
)
589 (find-file-noselect filename
))))
591 (message "Exporting...")
594 (let ((inhibit-read-only t
)) (erase-buffer))
596 (org-install-letbind)
598 (when (fboundp 'set-buffer-file-coding-system
)
599 (set-buffer-file-coding-system coding-system-for-write
))
601 (loop for contact in
(org-contacts-filter name
)
602 do
(insert (org-contacts-vcard-format contact
)))
606 (progn (save-buffer) (kill-buffer)))))
608 (defun org-contacts-show-map (&optional name
)
609 "Show contacts on a map. Requires google-maps-el."
611 (unless (fboundp 'google-maps-static-show
)
612 (error "org-contacts-show-map requires google-maps-el."))
613 (google-maps-static-show
616 for contact in
(org-contacts-filter name
)
617 for addr
= (cdr (assoc-string org-contacts-address-property
(caddr contact
)))
619 collect
(cons (list addr
) (list :label
(string-to-char (car contact
)))))))
621 (provide 'org-contacts
)