1 ;;; org-contacts.el --- Contacts management
3 ;; Copyright (C) 2010-2013 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)
47 (defgroup org-contacts nil
48 "Options about contacts management."
51 (defcustom org-contacts-files nil
52 "List of Org files to use as contacts source.
53 When set to nil, all your Org files will be used."
57 (defcustom org-contacts-email-property
"EMAIL"
58 "Name of the property for contact email address."
62 (defcustom org-contacts-address-property
"ADDRESS"
63 "Name of the property for contact address."
67 (defcustom org-contacts-birthday-property
"BIRTHDAY"
68 "Name of the property for contact birthday date."
72 (defcustom org-contacts-birthday-format
"Birthday: %l (%Y)"
73 "Format of the anniversary agenda entry.
74 The following replacements are available:
77 %l - Link to the heading
79 %Y - Number of year (ordinal)"
83 (defcustom org-contacts-last-read-mail-property
"LAST_READ_MAIL"
84 "Name of the property for contact last read email link storage."
88 (defcustom org-contacts-icon-property
"ICON"
89 "Name of the property for contact icon."
93 (defcustom org-contacts-nickname-property
"NICKNAME"
94 "Name of the property for IRC nickname match."
98 (defcustom org-contacts-icon-size
32
99 "Size of the contacts icons."
101 :group
'org-contacts
)
103 (defcustom org-contacts-icon-use-gravatar
(fboundp 'gravatar-retrieve
)
104 "Whether use Gravatar to fetch contact icons."
106 :group
'org-contacts
)
108 (defcustom org-contacts-completion-ignore-case t
109 "Ignore case when completing contacts."
111 :group
'org-contacts
)
113 (defcustom org-contacts-group-prefix
"+"
116 :group
'org-contacts
)
118 (defcustom org-contacts-matcher
(concat org-contacts-email-property
"<>\"\"")
119 "Matching rule for finding heading that are contacts.
120 This can be a tag name, or a property check."
122 :group
'org-contacts
)
124 (defcustom org-contacts-email-link-description-format
"%s (%d)"
125 "Format used to store links to email.
126 This overrides `org-email-link-description-format' if set."
130 (defcustom org-contacts-vcard-file
"contacts.vcf"
131 "Default file for vcard export."
135 (defvar org-contacts-keymap
136 (let ((map (make-sparse-keymap)))
137 (define-key map
"M" 'org-contacts-view-send-email
)
138 (define-key map
"i" 'org-contacts-view-switch-to-irc-buffer
)
140 "The keymap used in `org-contacts' result list.")
142 (defvar org-contacts-db nil
143 "Org Contacts database.")
145 (defvar org-contacts-last-update nil
146 "Last time the Org Contacts database has been updated.")
148 (defun org-contacts-files ()
149 "Return list of Org files to use for contact management."
150 (or org-contacts-files
(org-agenda-files t
'ifmode
)))
152 (defun org-contacts-db ()
153 "Return the latest Org Contacts Database"
156 (cdr (org-make-tags-matcher org-contacts-matcher
)))
158 (or (null org-contacts-last-update
)
160 (time-less-p org-contacts-last-update
161 (elt (file-attributes file
) 5)))
162 (org-contacts-files))))
165 (message "Update Org Contacts Database")
166 (dolist (file (org-contacts-files))
167 (org-check-agenda-file file
)
168 (with-current-buffer (org-get-agenda-file-buffer file
)
169 (unless (eq major-mode
'org-mode
)
170 (error "File %s is no in `org-mode'" file
))
172 '(add-to-list 'markers
(set-marker (make-marker) (point)))
175 (dolist (marker markers result
)
176 (org-with-point-at marker
178 (list (org-get-heading t
) marker
(org-entry-properties marker
'all
)))))
179 (setf org-contacts-db result
180 org-contacts-last-update
(current-time)))
183 (defun org-contacts-filter (&optional name-match tags-match
)
184 "Search for a contact maching NAME-MATCH and TAGS-MATCH.
185 If both match values are nil, return all contacts."
186 (if (and (null name-match
)
189 (loop for contact in
(org-contacts-db)
192 (org-string-match-p name-match
196 (org-string-match-p tags-match tag
))
198 (or (cdr (assoc-string "ALLTAGS" (caddr contact
))) "") ":"))))
201 (when (not (fboundp 'completion-table-case-fold
))
202 ;; That function is new in Emacs 24...
203 (defun completion-table-case-fold (table &optional dont-fold
)
204 (lambda (string pred action
)
205 (let ((completion-ignore-case (not dont-fold
)))
206 (complete-with-action action table string pred
)))))
208 (defun org-contacts-try-completion-prefix (to-match collection
&optional predicate
)
209 "Like `try-completion' but:
210 - works only with list and alist;
211 - looks at all prefixes rather than just the beginning of the string;"
212 (loop with regexp
= (concat "\\b" (regexp-quote to-match
))
218 for string
= (if (listp el
) (car el
) el
)
220 for start
= (when (or (null predicate
) (funcall predicate string
))
221 (string-match regexp string
))
224 do
(let ((end (match-end 0))
225 (len (length string
)))
228 (destructuring-bind (string start end
)
230 (values string start end
)
231 (org-contacts-common-substring
232 ret ret-start ret-end
239 (replace-regexp-in-string "\\`[ \t\n]*" "" ret
))))
241 (defun org-contacts-compare-strings (s1 start1 end1 s2 start2 end2
&optional ignore-case
)
242 "Compare the contents of two strings, using `compare-strings'.
244 This function works like `compare-strings' excepted that it
246 - The CAR is the number of characters that match at the beginning.
247 - The CDR is T is the two strings are the same and NIL otherwise."
248 (let ((ret (compare-strings s1 start1 end1 s2 start2 end2 ignore-case
)))
250 (cons (or end1
(length s1
)) t
)
251 (cons (1- (abs ret
)) nil
))))
253 (defun org-contacts-common-substring (s1 start1 end1 s2 start2 end2
)
254 "Extract the common substring between S1 and S2.
256 This function extracts the common substring between S1 and S2 and
257 adjust the part that remains common.
259 START1 and END1 delimit the part in S1 that we know is common
260 between the two strings. This applies to START2 and END2 for S2.
262 This function returns a list whose contains:
263 - The common substring found.
264 - The new value of the start of the known inner substring.
265 - The new value of the end of the known inner substring."
266 ;; Given two strings:
268 ;; s2: "fooo bar baz"
269 ;; and the inner substring is "bar"
270 ;; then: start1 = 4, end1 = 6, start2 = 5, end2 = 7
272 ;; To find the common substring we will compare two substrings:
273 ;; " oof" and " ooof" to find the beginning of the common substring.
274 ;; " baz" and " baz" to find the end of the common substring.
275 (let* ((len1 (length s1
))
276 (start1 (or start1
0))
277 (end1 (or end1 len1
))
280 (start2 (or start2
0))
281 (end2 (or end2 len2
))
283 (new-start (car (org-contacts-compare-strings
284 (substring (org-reverse-string s1
) (- len1 start1
)) nil nil
285 (substring (org-reverse-string s2
) (- len2 start2
)) nil nil
)))
287 (new-end (+ end1
(car (org-contacts-compare-strings
288 (substring s1 end1
) nil nil
289 (substring s2 end2
) nil nil
)))))
290 (list (substring s1
(- start1 new-start
) new-end
)
292 (+ new-start
(- end1 start1
)))))
294 (defun org-contacts-all-completions-prefix (to-match collection
&optional predicate
)
295 "Like `all-completions' but:
296 - works only with list and alist;
297 - looks at all prefixes rather than just the beginning of the string;"
298 (loop with regexp
= (concat "\\b" (regexp-quote to-match
))
300 for string
= (if (listp el
) (car el
) el
)
301 for match?
= (when (and (or (null predicate
) (funcall predicate string
)))
302 (string-match regexp string
))
305 (let ((end (match-end 0)))
306 (org-no-properties string
)
307 (when (< end
(length string
))
308 ;; Here we add a text property that will be used
309 ;; later to highlight the character right after
310 ;; the common part between each addresses.
311 ;; See `org-contacts-display-sort-function'.
312 (put-text-property end
(1+ end
) 'org-contacts-prefix
't string
)))
315 (defun org-contacts-make-collection-prefix (collection)
316 "Makes a collection function from COLLECTION which will match
318 (lexical-let ((collection collection
))
319 (lambda (string predicate flag
)
321 (org-contacts-try-completion-prefix string collection predicate
))
323 ;; `org-contacts-all-completions-prefix' has already been
324 ;; used to compute `all-completions'.
327 (org-contacts-test-completion-prefix string collection predicate
))
328 ((and (listp flag
) (eq (car flag
) 'boundaries
))
329 (destructuring-bind (to-ignore &rest suffix
)
331 (org-contacts-boundaries-prefix string collection predicate suffix
)))
333 (org-contacts-metadata-prefix string collection predicate
))
334 (t nil
; operation unsupported
337 (defun org-contacts-display-sort-function (completions)
338 (mapcar (lambda (string)
339 (loop with len
= (1- (length string
))
340 for i upfrom
0 to len
341 if
(memq 'org-contacts-prefix
342 (text-properties-at i string
))
343 do
(set-text-properties
345 (list 'font-lock-face
346 (if (char-equal (aref string i
)
347 (string-to-char " "))
348 ;; Spaces can't be bold.
352 do
(set-text-properties i
(1+ i
) nil string
)
353 finally
(return string
)))
356 (defun org-contacts-test-completion-prefix (string collection predicate
)
357 (find-if (lambda (el)
358 (and (or (null predicate
) (funcall predicate el
))
359 (string= string el
)))
362 (defun org-contacts-boundaries-prefix (string collection predicate suffix
)
363 (list* 'boundaries
(completion-boundaries string collection predicate suffix
)))
365 (defun org-contacts-metadata-prefix (string collection predicate
)
367 ((display-sort-function . org-contacts-display-sort-function
))))
369 (defun org-contacts-complete-group (start end string
)
370 "Complete text at START from a group.
372 A group FOO is composed of contacts with the tag FOO."
373 (let* ((completion-ignore-case org-contacts-completion-ignore-case
)
374 (group-completion-p (org-string-match-p
375 (concat "^" org-contacts-group-prefix
) string
)))
376 (when group-completion-p
377 (let ((completion-list
380 (mapcar (lambda (group)
381 (propertize (concat org-contacts-group-prefix group
)
382 'org-contacts-group group
))
384 (loop for contact in
(org-contacts-filter)
385 nconc
(org-split-string
386 (or (cdr (assoc-string "ALLTAGS" (caddr contact
))) "") ":")))))))
388 (if (= (length completion-list
) 1)
389 ;; We've foudn the correct group, returns the address
390 (lexical-let ((tag (get-text-property 0 'org-contacts-group
391 (car completion-list
))))
392 (lambda (string pred
&optional to-ignore
)
394 (loop for contact in
(org-contacts-filter
397 ;; The contact name is always the car of the assoc-list
398 ;; returned by `org-contacts-filter'.
399 for contact-name
= (car contact
)
400 ;; Grab the first email of the contact
401 for email
= (car (split-string
403 (cdr (assoc-string org-contacts-email-property
406 ;; If the user has an email address, append USER <EMAIL>.
407 if email collect
(org-contacts-format-email contact-name email
))
409 ;; We haven't found the correct group
410 (completion-table-case-fold completion-list
411 (not org-contacts-completion-ignore-case
))))))))
413 (defun org-contacts-complete-name (start end string
)
414 "Complete text at START with a user name and email."
415 (let* ((completion-ignore-case org-contacts-completion-ignore-case
)
417 (loop for contact in
(org-contacts-filter)
418 ;; The contact name is always the car of the assoc-list
419 ;; returned by `org-contacts-filter'.
420 for contact-name
= (car contact
)
421 ;; Build the list of the user email addresses.
422 for email-list
= (split-string (or
423 (cdr (assoc-string org-contacts-email-property
424 (caddr contact
))) ""))
425 ;; If the user has email addresses…
427 ;; … append a list of USER <EMAIL>.
428 nconc
(loop for email in email-list
429 collect
(org-contacts-format-email contact-name email
)))))
430 (when completion-list
432 (org-contacts-make-collection-prefix
433 (org-contacts-all-completions-prefix
435 (remove-duplicates completion-list
:test
#'equalp
)))))))
437 (defun org-contacts-message-complete-function (&optional start
)
438 "Function used in `completion-at-point-functions' in `message-mode'."
439 ;; Avoid to complete in `post-command-hook'.
440 (when completion-in-region-mode
441 (remove-hook 'post-command-hook
#'completion-in-region--postch
))
442 (let ((mail-abbrev-mode-regexp
443 "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
444 (when (mail-abbrev-in-expansion-header-p)
449 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
450 (goto-char (match-end 0))
452 (string (buffer-substring start end
)))
453 (or (org-contacts-complete-group start end string
)
454 (org-contacts-complete-name start end string
))))))
456 (defun org-contacts-gnus-get-name-email ()
457 "Get name and email address from Gnus message."
459 (gnus-with-article-headers
460 (mail-extract-address-components
461 (or (mail-fetch-field "From") "")))))
463 (defun org-contacts-gnus-article-from-get-marker ()
464 "Return a marker for a contact based on From."
465 (let* ((address (org-contacts-gnus-get-name-email))
467 (email (cadr address
)))
468 (cadar (or (org-contacts-filter
470 (concat org-contacts-email-property
"={\\b" (regexp-quote email
) "\\b}"))
473 (concat "^" name
"$")))))))
475 (defun org-contacts-gnus-article-from-goto ()
476 "Go to contact in the From address of current Gnus message."
478 (let ((marker (org-contacts-gnus-article-from-get-marker)))
480 (switch-to-buffer-other-window (marker-buffer marker
))
482 (when (eq major-mode
'org-mode
)
483 (org-show-context 'agenda
)
485 (and (outline-next-heading)
486 ;; show the next heading
487 (org-flag-heading nil
)))))))
489 (defun org-contacts-anniversaries (&optional field format
)
490 "Compute FIELD anniversary for each contact, returning FORMAT.
491 Default FIELD value is \"BIRTHDAY\".
493 Format is a string matching the following format specification:
496 %l - Link to the heading
498 %Y - Number of year (ordinal)"
499 (let ((calendar-date-style 'american
)
501 (unless format
(setq format org-contacts-birthday-format
))
502 (loop for contact in
(org-contacts-filter)
503 for anniv
= (let ((anniv (cdr (assoc-string
504 (or field org-contacts-birthday-property
)
507 (calendar-gregorian-from-absolute
508 (org-time-string-to-absolute anniv
))))
509 ;; Use `diary-anniversary' to compute anniversary.
510 if
(and anniv
(apply 'diary-anniversary anniv
))
511 collect
(format-spec format
512 `((?l .
,(org-with-point-at (cadr contact
) (org-store-link nil
)))
513 (?h .
,(car contact
))
514 (?y .
,(- (calendar-extract-year date
)
515 (calendar-extract-year anniv
)))
516 (?Y .
,(let ((years (- (calendar-extract-year date
)
517 (calendar-extract-year anniv
))))
518 (format "%d%s" years
(diary-ordinal-suffix years
)))))))))
520 (defun org-completing-read-date (prompt collection
521 &optional predicate require-match initial-input
522 hist def inherit-input-method
)
523 "Like `completing-read' but reads a date.
524 Only PROMPT and DEF are really used."
525 (org-read-date nil nil nil prompt nil def
))
527 (add-to-list 'org-property-set-functions-alist
528 `(,org-contacts-birthday-property . org-completing-read-date
))
530 (defun org-contacts-template-name (&optional return-value
)
531 "Try to return the contact name for a template.
532 If not found return RETURN-VALUE or something that would ask the user."
533 (or (car (org-contacts-gnus-get-name-email))
537 (defun org-contacts-template-email (&optional return-value
)
538 "Try to return the contact email for a template.
539 If not found return RETURN-VALUE or something that would ask the user."
540 (or (cadr (org-contacts-gnus-get-name-email))
542 (concat "%^{" org-contacts-email-property
"}p")))
544 (defun org-contacts-gnus-store-last-mail ()
545 "Store a link between mails and contacts.
547 This function should be called from `gnus-article-prepare-hook'."
548 (let ((marker (org-contacts-gnus-article-from-get-marker)))
550 (with-current-buffer (marker-buffer marker
)
553 (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
554 org-email-link-description-format
))
555 (link (gnus-with-article-buffer (org-store-link nil
))))
556 (org-set-property org-contacts-last-read-mail-property link
)))))))
558 (defun org-contacts-icon-as-string ()
559 (let ((image (org-contacts-get-icon)))
561 (propertize "-" 'display
565 `'(space :width
(,org-contacts-icon-size
)))
570 (defun org-contacts (name)
571 "Create agenda view for contacts matching NAME."
572 (interactive (list (read-string "Name: ")))
573 (let ((org-agenda-files (org-contacts-files))
574 (org-agenda-skip-function
575 (lambda () (org-agenda-skip-if nil
`(notregexp ,name
))))
576 (org-agenda-prefix-format (propertize
577 "%(org-contacts-icon-as-string)% s%(org-contacts-irc-number-of-unread-messages) "
578 'keymap org-contacts-keymap
))
579 (org-agenda-overriding-header
580 (or org-agenda-overriding-header
581 (concat "List of contacts matching `" name
"':"))))
582 (setq org-agenda-skip-regexp name
)
583 (org-tags-view nil org-contacts-matcher
)
584 (with-current-buffer org-agenda-buffer-name
585 (setq org-agenda-redo-command
586 (list 'org-contacts name
)))))
588 (defun org-contacts-completing-read (prompt
590 initial-input hist def inherit-input-method
)
591 "Call `completing-read' with contacts name as collection."
593 prompt
(org-contacts-filter) predicate t initial-input hist def inherit-input-method
))
595 (defun org-contacts-format-name (name)
596 "Trim any local formatting to get a bare name."
597 ;; Remove radio targets characters
598 (replace-regexp-in-string org-radio-target-regexp
"\\1" name
))
600 (defun org-contacts-format-email (name email
)
601 "Format a mail address."
603 (error "`email' cannot be nul"))
605 (concat (org-contacts-format-name name
) " <" email
">")
608 (defun org-contacts-check-mail-address (mail)
609 "Add MAIL address to contact at point if it does not have it."
610 (let ((mails (org-entry-get (point) org-contacts-email-property
)))
611 (unless (member mail
(split-string mails
))
613 (format "Do you want to add this address to %s?" (org-get-heading t
)))
614 (org-set-property org-contacts-email-property
(concat mails
" " mail
))))))
616 (defun org-contacts-gnus-check-mail-address ()
617 "Check that contact has the current address recorded.
618 This function should be called from `gnus-article-prepare-hook'."
619 (let ((marker (org-contacts-gnus-article-from-get-marker)))
621 (org-with-point-at marker
622 (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
624 (defun org-contacts-gnus-insinuate ()
625 "Add some hooks for Gnus user.
626 This adds `org-contacts-gnus-check-mail-address' and
627 `org-contacts-gnus-store-last-mail' to
628 `gnus-article-prepare-hook'. It also adds a binding on `;' in
629 `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
632 (define-key gnus-summary-mode-map
";" 'org-contacts-gnus-article-from-goto
)
633 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-check-mail-address
)
634 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-store-last-mail
))
636 (when (boundp 'completion-at-point-functions
)
637 (add-hook 'message-mode-hook
639 (add-to-list 'completion-at-point-functions
640 'org-contacts-message-complete-function
))))
642 (defun org-contacts-wl-get-from-header-content ()
643 "Retrieve the content of the `From' header of an email.
644 Works from wl-summary-mode and mime-view-mode - that is while viewing email.
645 Depends on Wanderlust been loaded."
646 (with-current-buffer (org-capture-get :original-buffer
)
648 ((eq major-mode
'wl-summary-mode
) (when wl-summary-buffer-elmo-folder
650 wl-summary-buffer-elmo-folder
651 (wl-summary-message-number)
653 ((eq major-mode
'mime-view-mode
) (std11-narrow-to-header)
655 (std11-fetch-field "From")
658 (defun org-contacts-wl-get-name-email ()
659 "Get name and email address from Wanderlust email.
660 See `org-contacts-wl-get-from-header-content' for limitations."
661 (let ((from (org-contacts-wl-get-from-header-content)))
663 (list (wl-address-header-extract-realname from
)
664 (wl-address-header-extract-address from
)))))
666 (defun org-contacts-template-wl-name (&optional return-value
)
667 "Try to return the contact name for a template from wl.
668 If not found, return RETURN-VALUE or something that would ask the
670 (or (car (org-contacts-wl-get-name-email))
674 (defun org-contacts-template-wl-email (&optional return-value
)
675 "Try to return the contact email for a template from Wanderlust.
676 If not found return RETURN-VALUE or something that would ask the user."
677 (or (cadr (org-contacts-wl-get-name-email))
679 (concat "%^{" org-contacts-email-property
"}p")))
681 (defun org-contacts-view-send-email (&optional ask
)
682 "Send email to the contact at point.
683 If ASK is set, ask for the email address even if there's only one
686 (let ((marker (org-get-at-bol 'org-hd-marker
)))
687 (org-with-point-at marker
688 (let ((emails (org-entry-get (point) org-contacts-email-property
)))
690 (let ((email-list (split-string emails
)))
691 (if (and (= (length email-list
) 1) (not ask
))
692 (compose-mail (org-contacts-format-email
693 (org-get-heading t
) emails
))
694 (let ((email (completing-read "Send mail to which address: " email-list
)))
695 (org-contacts-check-mail-address email
)
696 (compose-mail (org-contacts-format-email (org-get-heading t
) email
)))))
697 (error (format "This contact has no mail address set (no %s property)."
698 org-contacts-email-property
)))))))
700 (defun org-contacts-get-icon (&optional pom
)
701 "Get icon for contact at POM."
702 (setq pom
(or pom
(point)))
704 ;; Use `org-contacts-icon-property'
705 (let ((image-data (org-entry-get pom org-contacts-icon-property
)))
708 (if (fboundp 'gnus-rescale-image
)
709 (gnus-rescale-image (create-image image-data
)
710 (cons org-contacts-icon-size org-contacts-icon-size
))
711 (create-image image-data
)))))
712 ;; Next, try Gravatar
713 (when org-contacts-icon-use-gravatar
714 (let* ((gravatar-size org-contacts-icon-size
)
715 (email-list (org-entry-get pom org-contacts-email-property
))
718 (loop for email in
(split-string email-list
)
719 for gravatar
= (gravatar-retrieve-synchronously email
)
721 (not (eq gravatar
'error
)))
723 (when gravatar
(throw 'icon gravatar
))))))
725 (defun org-contacts-irc-buffer (&optional pom
)
726 "Get the IRC buffer associated with the entry at POM."
727 (setq pom
(or pom
(point)))
728 (let ((nick (org-entry-get pom org-contacts-nickname-property
)))
730 (let ((buffer (get-buffer nick
)))
732 (with-current-buffer buffer
733 (when (eq major-mode
'erc-mode
)
736 (defun org-contacts-irc-number-of-unread-messages (&optional pom
)
737 "Return the number of unread messages for contact at POM."
738 (when (boundp 'erc-modified-channels-alist
)
739 (let ((number (cadr (assoc (org-contacts-irc-buffer pom
) erc-modified-channels-alist
))))
741 (format (concat "%3d unread message" (if (> number
1) "s" " ") " ") number
)
742 (make-string 21 ?
)))))
744 (defun org-contacts-view-switch-to-irc-buffer ()
745 "Switch to the IRC buffer of the current contact if it has one."
747 (let ((marker (org-get-at-bol 'org-hd-marker
)))
748 (org-with-point-at marker
749 (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
751 (defun org-contacts-completing-read-nickname (prompt collection
752 &optional predicate require-match initial-input
753 hist def inherit-input-method
)
754 "Like `completing-read' but reads a nickname."
755 (org-completing-read prompt
(append collection
(erc-nicknames-list)) predicate require-match
756 initial-input hist def inherit-input-method
))
758 (defun erc-nicknames-list ()
759 "Return all nicknames of all ERC buffers."
760 (if (fboundp 'erc-buffer-list
)
761 (loop for buffer in
(erc-buffer-list)
762 nconc
(with-current-buffer buffer
763 (loop for user-entry in
(mapcar 'car
(erc-get-channel-user-list))
764 collect
(elt user-entry
1))))))
766 (add-to-list 'org-property-set-functions-alist
767 `(,org-contacts-nickname-property . org-contacts-completing-read-nickname
))
769 (defun org-contacts-vcard-escape (str)
770 "Escape ; , and \n in STR for the VCard format."
771 ;; Thanks to this library for the regexp:
772 ;; http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el
774 (replace-regexp-in-string
776 (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str
))))
778 (defun org-contacts-vcard-encode-name (name)
779 "Try to encode NAME as VCard's N property.
780 The N property expects
782 FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
784 Org-contacts does not specify how to encode the name. So we try
786 (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name
) ";;;"))
788 (defun org-contacts-vcard-format (contact)
789 "Formats CONTACT in VCard 3.0 format."
790 (let* ((properties (caddr contact
))
791 (name (org-contacts-vcard-escape (car contact
)))
792 (n (org-contacts-vcard-encode-name name
))
793 (email (org-contacts-vcard-escape (cdr (assoc-string org-contacts-email-property properties
))))
794 (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties
))))
795 (addr (cdr (assoc-string org-contacts-address-property properties
)))
796 (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties
))))
797 (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name
)))
799 (when email
(format "EMAIL:%s\n" email
))
801 (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr
)))
803 (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday
))))
804 (format "BDAY:%04d-%02d-%02d\n"
805 (calendar-extract-year cal-bday
)
806 (calendar-extract-month cal-bday
)
807 (calendar-extract-day cal-bday
))))
808 (when nick
(format "NICKNAME:%s\n" nick
))
811 (defun org-contacts-export-as-vcard (&optional name file to-buffer
)
812 "Export all contacts matching NAME as VCard 3.0.
813 If TO-BUFFER is nil, the content is written to FILE or
814 `org-contacts-vcard-file'. If TO-BUFFER is non-nil, the buffer
815 is created and the VCard is written into that buffer."
816 (interactive) ; TODO ask for name?
817 (let* ((filename (or file org-contacts-vcard-file
))
818 (buffer (if to-buffer
819 (get-buffer-create to-buffer
)
820 (find-file-noselect filename
))))
822 (message "Exporting...")
825 (let ((inhibit-read-only t
)) (erase-buffer))
827 (org-install-letbind)
829 (when (fboundp 'set-buffer-file-coding-system
)
830 (set-buffer-file-coding-system coding-system-for-write
))
832 (loop for contact in
(org-contacts-filter name
)
833 do
(insert (org-contacts-vcard-format contact
)))
837 (progn (save-buffer) (kill-buffer)))))
839 (defun org-contacts-show-map (&optional name
)
840 "Show contacts on a map.
841 Requires google-maps-el."
843 (unless (fboundp 'google-maps-static-show
)
844 (error "`org-contacts-show-map' requires `google-maps-el'"))
845 (google-maps-static-show
848 for contact in
(org-contacts-filter name
)
849 for addr
= (cdr (assoc-string org-contacts-address-property
(caddr contact
)))
851 collect
(cons (list addr
) (list :label
(string-to-char (car contact
)))))))
853 (provide 'org-contacts
)