1 ;;; org-contacts.el --- Contacts management
3 ;; Copyright (C) 2010-2014 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 ;; This program 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 ;; This program 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 minimal template just like
31 ;; ("c" "Contacts" entry (file "~/Org/contacts.org")
32 ;; "* %(org-contacts-template-name)
34 ;; :EMAIL: %(org-contacts-template-email)
37 ;; You can also use a complex template, for example:
39 ;; ("c" "Contacts" entry (file "~/Org/contacts.org")
40 ;; "* %(org-contacts-template-name)
42 ;; :EMAIL: %(org-contacts-template-email)
61 (require 'org-capture
)
64 (defgroup org-contacts nil
65 "Options about contacts management."
68 (defcustom org-contacts-files nil
69 "List of Org files to use as contacts source.
70 When set to nil, all your Org files will be used."
74 (defcustom org-contacts-email-property
"EMAIL"
75 "Name of the property for contact email address."
79 (defcustom org-contacts-tel-property
"PHONE"
80 "Name of the property for contact phone number."
84 (defcustom org-contacts-address-property
"ADDRESS"
85 "Name of the property for contact address."
89 (defcustom org-contacts-birthday-property
"BIRTHDAY"
90 "Name of the property for contact birthday date."
94 (defcustom org-contacts-note-property
"NOTE"
95 "Name of the property for contact note."
99 (defcustom org-contacts-alias-property
"ALIAS"
100 "Name of the property for contact name alias."
102 :group
'org-contacts
)
104 (defcustom org-contacts-ignore-property
"IGNORE"
105 "Name of the property, which values will be ignored when
106 completing or exporting to vcard."
108 :group
'org-contacts
)
111 (defcustom org-contacts-birthday-format
"Birthday: %l (%Y)"
112 "Format of the anniversary agenda entry.
113 The following replacements are available:
116 %l - Link to the heading
118 %Y - Number of year (ordinal)"
120 :group
'org-contacts
)
122 (defcustom org-contacts-last-read-mail-property
"LAST_READ_MAIL"
123 "Name of the property for contact last read email link storage."
125 :group
'org-contacts
)
127 (defcustom org-contacts-icon-property
"ICON"
128 "Name of the property for contact icon."
130 :group
'org-contacts
)
132 (defcustom org-contacts-nickname-property
"NICKNAME"
133 "Name of the property for IRC nickname match."
135 :group
'org-contacts
)
137 (defcustom org-contacts-icon-size
32
138 "Size of the contacts icons."
140 :group
'org-contacts
)
142 (defcustom org-contacts-icon-use-gravatar
(fboundp 'gravatar-retrieve
)
143 "Whether use Gravatar to fetch contact icons."
145 :group
'org-contacts
)
147 (defcustom org-contacts-completion-ignore-case t
148 "Ignore case when completing contacts."
150 :group
'org-contacts
)
152 (defcustom org-contacts-group-prefix
"+"
155 :group
'org-contacts
)
157 (defcustom org-contacts-tags-props-prefix
"#"
158 "Tags and properties prefix."
160 :group
'org-contacts
)
162 (defcustom org-contacts-matcher
163 (mapconcat #'identity
164 (mapcar (lambda (x) (concat x
"<>\"\""))
165 (list org-contacts-email-property
166 org-contacts-alias-property
167 org-contacts-tel-property
168 org-contacts-address-property
169 org-contacts-birthday-property
))
171 "Matching rule for finding heading that are contacts.
172 This can be a tag name, or a property check."
174 :group
'org-contacts
)
176 (defcustom org-contacts-email-link-description-format
"%s (%d)"
177 "Format used to store links to email.
178 This overrides `org-email-link-description-format' if set."
182 (defcustom org-contacts-vcard-file
"contacts.vcf"
183 "Default file for vcard export."
187 (defcustom org-contacts-enable-completion t
188 "Enable or not the completion in `message-mode' with `org-contacts'."
192 (defcustom org-contacts-complete-functions
193 '(org-contacts-complete-group org-contacts-complete-tags-props org-contacts-complete-name
)
194 "List of functions used to complete contacts in `message-mode'."
198 ;; Decalre external functions and variables
199 (declare-function org-reverse-string
"org")
200 (declare-function diary-ordinal-suffix
"ext:diary-lib")
201 (declare-function wl-summary-message-number
"ext:wl-summary")
202 (declare-function wl-address-header-extract-address
"ext:wl-address")
203 (declare-function wl-address-header-extract-realname
"ext:wl-address")
204 (declare-function erc-buffer-list
"ext:erc")
205 (declare-function erc-get-channel-user-list
"ext:erc")
206 (declare-function google-maps-static-show
"ext:google-maps-static")
207 (declare-function elmo-message-field
"ext:elmo-pipe")
208 (declare-function std11-narrow-to-header
"ext:std11")
209 (declare-function std11-fetch-field
"ext:std11")
211 (defconst org-contacts-property-values-separators
"[,; \f\t\n\r\v]+"
212 "The default value of separators for `org-contacts-split-property'.
214 A regexp matching strings of whitespace, `,' and `;'.")
216 (defvar org-contacts-keymap
217 (let ((map (make-sparse-keymap)))
218 (define-key map
"M" 'org-contacts-view-send-email
)
219 (define-key map
"i" 'org-contacts-view-switch-to-irc-buffer
)
221 "The keymap used in `org-contacts' result list.")
223 (defvar org-contacts-db nil
224 "Org Contacts database.")
226 (defvar org-contacts-last-update nil
227 "Last time the Org Contacts database has been updated.")
229 (defun org-contacts-files ()
230 "Return list of Org files to use for contact management."
231 (or org-contacts-files
(org-agenda-files t
'ifmode
)))
233 (defun org-contacts-db-need-update-p ()
234 "Determine whether `org-contacts-db' needs to be refreshed."
235 (or (null org-contacts-last-update
)
236 (cl-find-if (lambda (file)
237 (or (time-less-p org-contacts-last-update
238 (elt (file-attributes file
) 5))))
239 (org-contacts-files))
240 (org-contacts-db-has-dead-markers-p org-contacts-db
)))
242 (defun org-contacts-db-has-dead-markers-p (org-contacts-db)
243 "Returns t if at least one dead marker is found in
244 ORG-CONTACTS-DB. A dead marker in this case is a marker pointing
245 to dead or no buffer."
246 ;; Scan contacts list looking for dead markers, and return t at first found.
247 (catch 'dead-marker-found
248 (while org-contacts-db
249 (unless (marker-buffer (nth 1 (car org-contacts-db
)))
250 (throw 'dead-marker-found t
))
251 (setq org-contacts-db
(cdr org-contacts-db
)))
254 (defun org-contacts-db ()
255 "Return the latest Org Contacts Database."
256 (let* ((org--matcher-tags-todo-only nil
)
257 (contacts-matcher (cdr (org-make-tags-matcher org-contacts-matcher
)))
259 (when (org-contacts-db-need-update-p)
260 (let ((progress-reporter
261 (make-progress-reporter "Updating Org Contacts Database..." 0 (length org-contacts-files
)))
263 (dolist (file (org-contacts-files))
265 ;; if file doesn't exist and the user agrees to removing it
266 ;; from org-agendas-list, 'nextfile is thrown. Catch it here
267 ;; and skip processing the file.
269 ;; TODO: suppose that the user has set an org-contacts-files
270 ;; list that contains an element that doesn't exist in the
271 ;; file system: in that case, the org-agenda-files list could
272 ;; be updated (and saved to the customizations of the user) if
273 ;; it contained the same file even though the org-agenda-files
274 ;; list wasn't actually used. I don't think it is normal that
275 ;; org-contacts updates org-agenda-files in this case, but
276 ;; short of duplicating org-check-agenda-files and
277 ;; org-remove-files, I don't know how to avoid it.
279 ;; A side effect of the TODO is that the faulty
280 ;; org-contacts-files list never gets updated and thus the
281 ;; user is always queried about the missing files when
282 ;; org-contacts-db-need-update-p returns true.
283 (org-check-agenda-file file
))
284 (message "Skipped %s removed from org-agenda-files list."
285 (abbreviate-file-name file
))
286 (with-current-buffer (org-get-agenda-file-buffer file
)
287 (unless (eq major-mode
'org-mode
)
288 (error "File %s is not in `org-mode'" file
))
291 (org-scan-tags 'org-contacts-at-point
293 org--matcher-tags-todo-only
)))))
294 (progress-reporter-update progress-reporter
(setq i
(1+ i
))))
295 (setf org-contacts-db result
296 org-contacts-last-update
(current-time))
297 (progress-reporter-done progress-reporter
)))
300 (defun org-contacts-at-point (&optional pom
)
301 "Return the contacts at point-or-marker POM or current position
303 (setq pom
(or pom
(point)))
304 (org-with-point-at pom
305 (list (org-get-heading t
) (set-marker (make-marker) pom
) (org-entry-properties pom
'all
))))
307 (defun org-contacts-filter (&optional name-match tags-match prop-match
)
308 "Search for a contact matching any of NAME-MATCH, TAGS-MATCH, PROP-MATCH.
309 If all match values are nil, return all contacts.
311 The optional PROP-MATCH argument is a single (PROP . VALUE) cons
312 cell corresponding to the contact properties.
314 (if (and (null name-match
)
318 (cl-loop for contact in
(org-contacts-db)
321 (string-match-p name-match
324 (cl-find-if (lambda (prop)
325 (and (string= (car prop-match
) (car prop
))
326 (string-match-p (cdr prop-match
) (cdr prop
))))
329 (cl-find-if (lambda (tag)
330 (string-match-p tags-match tag
))
332 (or (cdr (assoc-string "ALLTAGS" (caddr contact
))) "") ":"))))
335 (when (not (fboundp 'completion-table-case-fold
))
336 ;; That function is new in Emacs 24...
337 (defun completion-table-case-fold (table &optional dont-fold
)
338 (lambda (string pred action
)
339 (let ((completion-ignore-case (not dont-fold
)))
340 (complete-with-action action table string pred
)))))
342 (defun org-contacts-try-completion-prefix (to-match collection
&optional predicate
)
343 "Custom implementation of `try-completion'.
344 This version works only with list and alist and it looks at all
345 prefixes rather than just the beginning of the string."
346 (cl-loop with regexp
= (concat "\\b" (regexp-quote to-match
))
352 for string
= (if (listp el
) (car el
) el
)
354 for start
= (when (or (null predicate
) (funcall predicate string
))
355 (string-match regexp string
))
358 do
(let ((end (match-end 0))
359 (len (length string
)))
362 (cl-destructuring-bind (string start end
)
364 (values string start end
)
365 (org-contacts-common-substring
366 ret ret-start ret-end
373 (replace-regexp-in-string "\\`[ \t\n]*" "" ret
))))
375 (defun org-contacts-compare-strings (s1 start1 end1 s2 start2 end2
&optional ignore-case
)
376 "Compare the contents of two strings, using `compare-strings'.
378 This function works like `compare-strings' excepted that it
380 - The CAR is the number of characters that match at the beginning.
381 - The CDR is T is the two strings are the same and NIL otherwise."
382 (let ((ret (compare-strings s1 start1 end1 s2 start2 end2 ignore-case
)))
384 (cons (or end1
(length s1
)) t
)
385 (cons (1- (abs ret
)) nil
))))
387 (defun org-contacts-common-substring (s1 start1 end1 s2 start2 end2
)
388 "Extract the common substring between S1 and S2.
390 This function extracts the common substring between S1 and S2 and
391 adjust the part that remains common.
393 START1 and END1 delimit the part in S1 that we know is common
394 between the two strings. This applies to START2 and END2 for S2.
396 This function returns a list whose contains:
397 - The common substring found.
398 - The new value of the start of the known inner substring.
399 - The new value of the end of the known inner substring."
400 ;; Given two strings:
402 ;; s2: "fooo bar baz"
403 ;; and the inner substring is "bar"
404 ;; then: start1 = 4, end1 = 6, start2 = 5, end2 = 7
406 ;; To find the common substring we will compare two substrings:
407 ;; " oof" and " ooof" to find the beginning of the common substring.
408 ;; " baz" and " baz" to find the end of the common substring.
409 (let* ((len1 (length s1
))
410 (start1 (or start1
0))
411 (end1 (or end1 len1
))
414 (start2 (or start2
0))
415 (end2 (or end2 len2
))
417 (new-start (car (org-contacts-compare-strings
418 (substring (org-reverse-string s1
) (- len1 start1
)) nil nil
419 (substring (org-reverse-string s2
) (- len2 start2
)) nil nil
)))
421 (new-end (+ end1
(car (org-contacts-compare-strings
422 (substring s1 end1
) nil nil
423 (substring s2 end2
) nil nil
)))))
424 (list (substring s1
(- start1 new-start
) new-end
)
426 (+ new-start
(- end1 start1
)))))
428 (defun org-contacts-all-completions-prefix (to-match collection
&optional predicate
)
429 "Custom version of `all-completions'.
430 This version works only with list and alist and it looks at all
431 prefixes rather than just the beginning of the string."
432 (cl-loop with regexp
= (concat "\\b" (regexp-quote to-match
))
434 for string
= (if (listp el
) (car el
) el
)
435 for match?
= (when (and (or (null predicate
) (funcall predicate string
)))
436 (string-match regexp string
))
439 (let ((end (match-end 0)))
440 (org-no-properties string
)
441 (when (< end
(length string
))
442 ;; Here we add a text property that will be used
443 ;; later to highlight the character right after
444 ;; the common part between each addresses.
445 ;; See `org-contacts-display-sort-function'.
446 (put-text-property end
(1+ end
) 'org-contacts-prefix
't string
)))
449 (defun org-contacts-make-collection-prefix (collection)
450 "Make a collection function from COLLECTION which will match on prefixes."
451 (lexical-let ((collection collection
))
452 (lambda (string predicate flag
)
454 (org-contacts-try-completion-prefix string collection predicate
))
456 ;; `org-contacts-all-completions-prefix' has already been
457 ;; used to compute `all-completions'.
460 (org-contacts-test-completion-prefix string collection predicate
))
461 ((and (listp flag
) (eq (car flag
) 'boundaries
))
462 (cl-destructuring-bind (to-ignore &rest suffix
)
464 (org-contacts-boundaries-prefix string collection predicate suffix
)))
466 (org-contacts-metadata-prefix string collection predicate
))
467 (t nil
; operation unsupported
470 (defun org-contacts-display-sort-function (completions)
471 "Sort function for contacts display."
472 (mapcar (lambda (string)
473 (cl-loop with len
= (1- (length string
))
474 for i upfrom
0 to len
475 if
(memq 'org-contacts-prefix
476 (text-properties-at i string
))
477 do
(set-text-properties
479 (list 'font-lock-face
480 (if (char-equal (aref string i
)
481 (string-to-char " "))
482 ;; Spaces can't be bold.
486 do
(set-text-properties i
(1+ i
) nil string
)
487 finally
(cl-return string
)))
490 (defun org-contacts-test-completion-prefix (string collection predicate
)
491 (cl-find-if (lambda (el)
492 (and (or (null predicate
) (funcall predicate el
))
493 (string= string el
)))
496 (defun org-contacts-boundaries-prefix (string collection predicate suffix
)
497 (list* 'boundaries
(completion-boundaries string collection predicate suffix
)))
499 (defun org-contacts-metadata-prefix (string collection predicate
)
501 ((cycle-sort-function . org-contacts-display-sort-function
)
502 (display-sort-function . org-contacts-display-sort-function
))))
504 (defun org-contacts-complete-group (start end string
)
505 "Complete text at START from a group.
507 A group FOO is composed of contacts with the tag FOO."
508 (let* ((completion-ignore-case org-contacts-completion-ignore-case
)
509 (group-completion-p (string-match-p
510 (concat "^" org-contacts-group-prefix
) string
)))
511 (when group-completion-p
512 (let ((completion-list
515 (mapcar (lambda (group)
516 (propertize (concat org-contacts-group-prefix group
)
517 'org-contacts-group group
))
519 (cl-loop for contact in
(org-contacts-filter)
520 nconc
(org-split-string
521 (or (cdr (assoc-string "ALLTAGS" (caddr contact
))) "") ":")))))))
523 (if (= (length completion-list
) 1)
524 ;; We've found the correct group, returns the address
525 (lexical-let ((tag (get-text-property 0 'org-contacts-group
526 (car completion-list
))))
527 (lambda (string pred
&optional to-ignore
)
529 (cl-loop for contact in
(org-contacts-filter
532 ;; The contact name is always the car of the assoc-list
533 ;; returned by `org-contacts-filter'.
534 for contact-name
= (car contact
)
535 ;; Grab the first email of the contact
536 for email
= (org-contacts-strip-link
537 (or (car (org-contacts-split-property
539 (cdr (assoc-string org-contacts-email-property
542 ;; If the user has an email address, append USER <EMAIL>.
543 if email collect
(org-contacts-format-email contact-name email
))
545 ;; We haven't found the correct group
546 (completion-table-case-fold completion-list
547 (not org-contacts-completion-ignore-case
))))))))
549 (defun org-contacts-complete-tags-props (start end string
)
550 "Insert emails that match the tags expression.
552 For example: FOO-BAR will match entries tagged with FOO but not
555 See (org) Matching tags and properties for a complete
557 (let* ((completion-ignore-case org-contacts-completion-ignore-case
)
558 (completion-p (string-match-p
559 (concat "^" org-contacts-tags-props-prefix
) string
)))
564 (cl-loop for contact in
(org-contacts-db)
565 for contact-name
= (car contact
)
566 for email
= (org-contacts-strip-link (or (car (org-contacts-split-property
568 (cdr (assoc-string org-contacts-email-property
571 for tags
= (cdr (assoc "TAGS" (nth 2 contact
)))
572 for tags-list
= (if tags
573 (split-string (substring (cdr (assoc "TAGS" (nth 2 contact
))) 1 -
1) ":")
575 for marker
= (nth 1 contact
)
576 if
(with-current-buffer (marker-buffer marker
)
580 (eval (cdr (org-make-tags-matcher (cl-subseq string
1)))))))
581 collect
(org-contacts-format-email contact-name email
))
583 (when (not (string= "" result
))
584 ;; return (start end function)
585 (lexical-let* ((to-return result
))
587 (lambda (string pred
&optional to-ignore
) to-return
))))))))
589 (defun org-contacts-remove-ignored-property-values (ignore-list list
)
590 "Remove all ignore-list's elements from list and you can use
591 regular expressions in the ignore list."
592 (cl-remove-if (lambda (el)
593 (cl-find-if (lambda (x)
594 (string-match-p x el
))
598 (defun org-contacts-complete-name (start end string
)
599 "Complete text at START with a user name and email."
600 (let* ((completion-ignore-case org-contacts-completion-ignore-case
)
602 (cl-loop for contact in
(org-contacts-filter)
603 ;; The contact name is always the car of the assoc-list
604 ;; returned by `org-contacts-filter'.
605 for contact-name
= (car contact
)
607 ;; Build the list of the email addresses which has
609 for ignore-list
= (org-contacts-split-property
610 (or (cdr (assoc-string org-contacts-ignore-property
611 (nth 2 contact
))) ""))
612 ;; Build the list of the user email addresses.
613 for email-list
= (org-contacts-remove-ignored-property-values
615 (org-contacts-split-property
616 (or (cdr (assoc-string org-contacts-email-property
617 (nth 2 contact
))) "")))
618 ;; If the user has email addresses…
620 ;; … append a list of USER <EMAIL>.
621 nconc
(cl-loop for email in email-list
622 collect
(org-contacts-format-email contact-name
(org-contacts-strip-link email
)))))
623 (completion-list (org-contacts-all-completions-prefix
625 (org-uniquify completion-list
))))
626 (when completion-list
628 (org-contacts-make-collection-prefix completion-list
)))))
630 (defun org-contacts-message-complete-function (&optional start
)
631 "Function used in `completion-at-point-functions' in `message-mode'."
632 ;; Avoid to complete in `post-command-hook'.
633 (when completion-in-region-mode
634 (remove-hook 'post-command-hook
#'completion-in-region--postch
))
635 (let ((mail-abbrev-mode-regexp
636 "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
637 (when (mail-abbrev-in-expansion-header-p)
642 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
643 (goto-char (match-end 0))
645 (string (buffer-substring start end
)))
646 (run-hook-with-args-until-success
647 'org-contacts-complete-functions start end string
)))))
649 (defun org-contacts-gnus-get-name-email ()
650 "Get name and email address from Gnus message."
652 (gnus-with-article-headers
653 (mail-extract-address-components
654 (or (mail-fetch-field "From") "")))))
656 (defun org-contacts-gnus-article-from-get-marker ()
657 "Return a marker for a contact based on From."
658 (let* ((address (org-contacts-gnus-get-name-email))
660 (email (cadr address
)))
661 (cl-cadar (or (org-contacts-filter
664 (cons org-contacts-email-property
(concat "\\b" (regexp-quote email
) "\\b")))
667 (concat "^" name
"$")))))))
669 (defun org-contacts-gnus-article-from-goto ()
670 "Go to contact in the From address of current Gnus message."
672 (let ((marker (org-contacts-gnus-article-from-get-marker)))
674 (switch-to-buffer-other-window (marker-buffer marker
))
676 (when (eq major-mode
'org-mode
) (org-show-context 'agenda
)))))
678 (with-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
679 (defun org-contacts-anniversaries (&optional field format
)
680 "Compute FIELD anniversary for each contact, returning FORMAT.
681 Default FIELD value is \"BIRTHDAY\".
683 Format is a string matching the following format specification:
686 %l - Link to the heading
688 %Y - Number of year (ordinal)"
689 (let ((calendar-date-style 'american
)
691 (unless format
(setq format org-contacts-birthday-format
))
692 (cl-loop for contact in
(org-contacts-filter)
693 for anniv
= (let ((anniv (cdr (assoc-string
694 (or field org-contacts-birthday-property
)
697 (calendar-gregorian-from-absolute
698 (org-time-string-to-absolute anniv
))))
699 ;; Use `diary-anniversary' to compute anniversary.
700 if
(and anniv
(apply 'diary-anniversary anniv
))
701 collect
(format-spec format
702 `((?l .
,(org-with-point-at (cadr contact
) (org-store-link nil
)))
703 (?h .
,(car contact
))
704 (?y .
,(- (calendar-extract-year date
)
705 (calendar-extract-year anniv
)))
706 (?Y .
,(let ((years (- (calendar-extract-year date
)
707 (calendar-extract-year anniv
))))
708 (format "%d%s" years
(diary-ordinal-suffix years
)))))))))
710 (defun org-completing-read-date (prompt collection
711 &optional predicate require-match initial-input
712 hist def inherit-input-method
)
713 "Like `completing-read' but reads a date.
714 Only PROMPT and DEF are really used."
715 (org-read-date nil nil nil prompt nil def
))
717 (add-to-list 'org-property-set-functions-alist
718 `(,org-contacts-birthday-property . org-completing-read-date
))
720 (defun org-contacts-template-name (&optional return-value
)
721 "Try to return the contact name for a template.
722 If not found return RETURN-VALUE or something that would ask the user."
723 (or (car (org-contacts-gnus-get-name-email))
727 (defun org-contacts-template-email (&optional return-value
)
728 "Try to return the contact email for a template.
729 If not found return RETURN-VALUE or something that would ask the user."
730 (or (cadr (org-contacts-gnus-get-name-email))
732 (concat "%^{" org-contacts-email-property
"}p")))
734 (defun org-contacts-gnus-store-last-mail ()
735 "Store a link between mails and contacts.
737 This function should be called from `gnus-article-prepare-hook'."
738 (let ((marker (org-contacts-gnus-article-from-get-marker)))
740 (with-current-buffer (marker-buffer marker
)
743 (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
744 org-email-link-description-format
))
745 (link (gnus-with-article-buffer (org-store-link nil
))))
746 (org-set-property org-contacts-last-read-mail-property link
)))))))
748 (defun org-contacts-icon-as-string ()
749 "Return the contact icon as a string."
750 (let ((image (org-contacts-get-icon)))
752 (propertize "-" 'display
756 `'(space :width
(,org-contacts-icon-size
)))
761 (defun org-contacts (name)
762 "Create agenda view for contacts matching NAME."
763 (interactive (list (read-string "Name: ")))
764 (let ((org-agenda-files (org-contacts-files))
765 (org-agenda-skip-function
766 (lambda () (org-agenda-skip-if nil
`(notregexp ,name
))))
767 (org-agenda-prefix-format (propertize
768 "%(org-contacts-icon-as-string)% s%(org-contacts-irc-number-of-unread-messages) "
769 'keymap org-contacts-keymap
))
770 (org-agenda-overriding-header
771 (or org-agenda-overriding-header
772 (concat "List of contacts matching `" name
"':"))))
773 (setq org-agenda-skip-regexp name
)
774 (org-tags-view nil org-contacts-matcher
)
775 (with-current-buffer org-agenda-buffer-name
776 (setq org-agenda-redo-command
777 (list 'org-contacts name
)))))
779 (defun org-contacts-completing-read (prompt
781 initial-input hist def inherit-input-method
)
782 "Call `completing-read' with contacts name as collection."
784 prompt
(org-contacts-filter) predicate t initial-input hist def inherit-input-method
))
786 (defun org-contacts-format-name (name)
787 "Trim any local formatting to get a bare NAME."
788 ;; Remove radio targets characters
789 (replace-regexp-in-string org-radio-target-regexp
"\\1" name
))
791 (defun org-contacts-format-email (name email
)
792 "Format an EMAIL address corresponding to NAME."
794 (error "`email' cannot be nul"))
796 (concat (org-contacts-format-name name
) " <" email
">")
799 (defun org-contacts-check-mail-address (mail)
800 "Add MAIL address to contact at point if it does not have it."
801 (let ((mails (org-entry-get (point) org-contacts-email-property
)))
802 (unless (member mail
(split-string mails
))
804 (format "Do you want to add this address to %s?" (org-get-heading t
)))
805 (org-set-property org-contacts-email-property
(concat mails
" " mail
))))))
807 (defun org-contacts-gnus-check-mail-address ()
808 "Check that contact has the current address recorded.
809 This function should be called from `gnus-article-prepare-hook'."
810 (let ((marker (org-contacts-gnus-article-from-get-marker)))
812 (org-with-point-at marker
813 (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
815 (defun org-contacts-gnus-insinuate ()
816 "Add some hooks for Gnus user.
817 This adds `org-contacts-gnus-check-mail-address' and
818 `org-contacts-gnus-store-last-mail' to
819 `gnus-article-prepare-hook'. It also adds a binding on `;' in
820 `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
823 (define-key gnus-summary-mode-map
";" 'org-contacts-gnus-article-from-goto
)
824 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-check-mail-address
)
825 (add-hook 'gnus-article-prepare-hook
'org-contacts-gnus-store-last-mail
))
827 (defun org-contacts-setup-completion-at-point ()
828 "Add `org-contacts-message-complete-function' as a new function
829 to complete the thing at point."
830 (add-to-list 'completion-at-point-functions
831 'org-contacts-message-complete-function
))
833 (defun org-contacts-unload-hook ()
834 (remove-hook 'message-mode-hook
'org-contacts-setup-completion-at-point
))
836 (when (and org-contacts-enable-completion
837 (boundp 'completion-at-point-functions
))
838 (add-hook 'message-mode-hook
'org-contacts-setup-completion-at-point
))
840 (defun org-contacts-wl-get-from-header-content ()
841 "Retrieve the content of the `From' header of an email.
842 Works from wl-summary-mode and mime-view-mode - that is while viewing email.
843 Depends on Wanderlust been loaded."
844 (with-current-buffer (org-capture-get :original-buffer
)
846 ((eq major-mode
'wl-summary-mode
) (when (and (boundp 'wl-summary-buffer-elmo-folder
)
847 wl-summary-buffer-elmo-folder
)
849 wl-summary-buffer-elmo-folder
850 (wl-summary-message-number)
852 ((eq major-mode
'mime-view-mode
) (std11-narrow-to-header)
854 (std11-fetch-field "From")
857 (defun org-contacts-wl-get-name-email ()
858 "Get name and email address from Wanderlust email.
859 See `org-contacts-wl-get-from-header-content' for limitations."
860 (let ((from (org-contacts-wl-get-from-header-content)))
862 (list (wl-address-header-extract-realname from
)
863 (wl-address-header-extract-address from
)))))
865 (defun org-contacts-template-wl-name (&optional return-value
)
866 "Try to return the contact name for a template from wl.
867 If not found, return RETURN-VALUE or something that would ask the
869 (or (car (org-contacts-wl-get-name-email))
873 (defun org-contacts-template-wl-email (&optional return-value
)
874 "Try to return the contact email for a template from Wanderlust.
875 If not found return RETURN-VALUE or something that would ask the user."
876 (or (cadr (org-contacts-wl-get-name-email))
878 (concat "%^{" org-contacts-email-property
"}p")))
880 (defun org-contacts-view-send-email (&optional ask
)
881 "Send email to the contact at point.
882 If ASK is set, ask for the email address even if there's only one
885 (let ((marker (org-get-at-bol 'org-hd-marker
)))
886 (org-with-point-at marker
887 (let ((emails (org-entry-get (point) org-contacts-email-property
)))
889 (let ((email-list (org-contacts-split-property emails
)))
890 (if (and (= (length email-list
) 1) (not ask
))
891 (compose-mail (org-contacts-format-email
892 (org-get-heading t
) emails
))
893 (let ((email (completing-read "Send mail to which address: " email-list
)))
894 (setq email
(org-contacts-strip-link email
))
895 (org-contacts-check-mail-address email
)
896 (compose-mail (org-contacts-format-email (org-get-heading t
) email
)))))
897 (error (format "This contact has no mail address set (no %s property)"
898 org-contacts-email-property
)))))))
900 (defun org-contacts-get-icon (&optional pom
)
901 "Get icon for contact at POM."
902 (setq pom
(or pom
(point)))
904 ;; Use `org-contacts-icon-property'
905 (let ((image-data (org-entry-get pom org-contacts-icon-property
)))
908 (if (fboundp 'gnus-rescale-image
)
909 (gnus-rescale-image (create-image image-data
)
910 (cons org-contacts-icon-size org-contacts-icon-size
))
911 (create-image image-data
)))))
912 ;; Next, try Gravatar
913 (when org-contacts-icon-use-gravatar
914 (let* ((gravatar-size org-contacts-icon-size
)
915 (email-list (org-entry-get pom org-contacts-email-property
))
918 (loop for email in
(org-contacts-split-property email-list
)
919 for gravatar
= (gravatar-retrieve-synchronously (org-contacts-strip-link email
))
921 (not (eq gravatar
'error
)))
923 (when gravatar
(throw 'icon gravatar
))))))
925 (defun org-contacts-irc-buffer (&optional pom
)
926 "Get the IRC buffer associated with the entry at POM."
927 (setq pom
(or pom
(point)))
928 (let ((nick (org-entry-get pom org-contacts-nickname-property
)))
930 (let ((buffer (get-buffer nick
)))
932 (with-current-buffer buffer
933 (when (eq major-mode
'erc-mode
)
936 (defun org-contacts-irc-number-of-unread-messages (&optional pom
)
937 "Return the number of unread messages for contact at POM."
938 (when (boundp 'erc-modified-channels-alist
)
939 (let ((number (cadr (assoc (org-contacts-irc-buffer pom
) erc-modified-channels-alist
))))
941 (format (concat "%3d unread message" (if (> number
1) "s" " ") " ") number
)
942 (make-string 21 ?
)))))
944 (defun org-contacts-view-switch-to-irc-buffer ()
945 "Switch to the IRC buffer of the current contact if it has one."
947 (let ((marker (org-get-at-bol 'org-hd-marker
)))
948 (org-with-point-at marker
949 (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
951 (defun org-contacts-completing-read-nickname (prompt collection
952 &optional predicate require-match initial-input
953 hist def inherit-input-method
)
954 "Like `completing-read' but reads a nickname."
955 (org-completing-read prompt
(append collection
(erc-nicknames-list)) predicate require-match
956 initial-input hist def inherit-input-method
))
958 (defun erc-nicknames-list ()
959 "Return all nicknames of all ERC buffers."
960 (loop for buffer in
(erc-buffer-list)
961 nconc
(with-current-buffer buffer
962 (loop for user-entry in
(mapcar 'car
(erc-get-channel-user-list))
963 collect
(elt user-entry
1)))))
965 (add-to-list 'org-property-set-functions-alist
966 `(,org-contacts-nickname-property . org-contacts-completing-read-nickname
))
968 (defun org-contacts-vcard-escape (str)
969 "Escape ; , and \n in STR for the VCard format."
970 ;; Thanks to this library for the regexp:
971 ;; http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el
973 (replace-regexp-in-string
975 (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str
))))
977 (defun org-contacts-vcard-encode-name (name)
978 "Try to encode NAME as VCard's N property.
979 The N property expects
981 FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
983 Org-contacts does not specify how to encode the name. So we try
985 (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name
) ";;;"))
987 (defun org-contacts-vcard-format (contact)
988 "Formats CONTACT in VCard 3.0 format."
989 (let* ((properties (nth 2 contact
))
990 (name (org-contacts-vcard-escape (car contact
)))
991 (n (org-contacts-vcard-encode-name name
))
992 (email (cdr (assoc-string org-contacts-email-property properties
)))
993 (tel (cdr (assoc-string org-contacts-tel-property properties
)))
994 (ignore-list (cdr (assoc-string org-contacts-ignore-property properties
)))
995 (ignore-list (when ignore-list
996 (org-contacts-split-property ignore-list
)))
997 (note (cdr (assoc-string org-contacts-note-property properties
)))
998 (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties
))))
999 (addr (cdr (assoc-string org-contacts-address-property properties
)))
1000 (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties
))))
1001 (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name
))
1002 emails-list result phones-list
)
1005 (setq emails-list
(org-contacts-remove-ignored-property-values ignore-list
(org-contacts-split-property email
)))
1008 (setq result
(concat result
"EMAIL:" (org-contacts-strip-link (car emails-list
)) "\n"))
1009 (setq emails-list
(cdr emails-list
)))
1012 (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr
)))
1014 (setq phones-list
(org-contacts-remove-ignored-property-values ignore-list
(org-contacts-split-property tel
)))
1017 (setq result
(concat result
"TEL:" (org-contacts-strip-link (org-link-unescape (car phones-list
))) "\n"))
1018 (setq phones-list
(cdr phones-list
)))
1021 (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday
))))
1022 (format "BDAY:%04d-%02d-%02d\n"
1023 (calendar-extract-year cal-bday
)
1024 (calendar-extract-month cal-bday
)
1025 (calendar-extract-day cal-bday
))))
1026 (when nick
(format "NICKNAME:%s\n" nick
))
1027 (when note
(format "NOTE:%s\n" note
))
1030 (defun org-contacts-export-as-vcard (&optional name file to-buffer
)
1031 "Export org contacts to V-Card 3.0.
1033 By default, all contacts are exported to `org-contacts-vcard-file'.
1035 When NAME is \\[universal-argument], prompts for a contact name.
1037 When NAME is \\[universal-argument] \\[universal-argument],
1038 prompts for a contact name and a file name where to export.
1040 When NAME is \\[universal-argument] \\[universal-argument]
1041 \\[universal-argument], prompts for a contact name and a buffer where to export.
1043 If the function is not called interactively, all parameters are
1044 passed to `org-contacts-export-as-vcard-internal'."
1046 (when (called-interactively-p 'any
)
1049 (read-string "Contact name: "
1050 (nth 0 (org-contacts-at-point))))
1052 (when (equal name
'(16))
1053 (read-file-name "File: " nil org-contacts-vcard-file
))
1055 (when (equal name
'(64))
1056 (read-buffer "Buffer: "))))
1057 (org-contacts-export-as-vcard-internal name file to-buffer
))
1059 (defun org-contacts-export-as-vcard-internal (&optional name file to-buffer
)
1060 "Export all contacts matching NAME as VCard 3.0.
1061 If TO-BUFFER is nil, the content is written to FILE or
1062 `org-contacts-vcard-file'. If TO-BUFFER is non-nil, the buffer
1063 is created and the VCard is written into that buffer."
1064 (let* ((filename (or file org-contacts-vcard-file
))
1065 (buffer (if to-buffer
1066 (get-buffer-create to-buffer
)
1067 (find-file-noselect filename
))))
1068 (message "Exporting...")
1070 (let ((inhibit-read-only t
)) (erase-buffer))
1072 (when (fboundp 'set-buffer-file-coding-system
)
1073 (set-buffer-file-coding-system coding-system-for-write
))
1074 (loop for contact in
(org-contacts-filter name
)
1075 do
(insert (org-contacts-vcard-format contact
)))
1078 (progn (save-buffer) (kill-buffer)))))
1080 (defun org-contacts-show-map (&optional name
)
1081 "Show contacts on a map.
1082 Requires google-maps-el."
1084 (unless (fboundp 'google-maps-static-show
)
1085 (error "`org-contacts-show-map' requires `google-maps-el'"))
1086 (google-maps-static-show
1089 for contact in
(org-contacts-filter name
)
1090 for addr
= (cdr (assoc-string org-contacts-address-property
(nth 2 contact
)))
1092 collect
(cons (list addr
) (list :label
(string-to-char (car contact
)))))))
1094 (defun org-contacts-strip-link (link)
1095 "Remove brackets, description, link type and colon from an org
1096 link string and return the pure link target."
1097 (let (startpos colonpos endpos
)
1098 (setq startpos
(string-match (regexp-opt '("[[tel:" "[[mailto:")) link
))
1101 (setq colonpos
(string-match ":" link
))
1102 (setq endpos
(string-match "\\]" link
))
1103 (if endpos
(substring link
(1+ colonpos
) endpos
) link
))
1105 (setq startpos
(string-match "mailto:" link
))
1106 (setq colonpos
(string-match ":" link
))
1107 (if startpos
(substring link
(1+ colonpos
)) link
)))))
1109 ;; Add the link type supported by org-contacts-strip-link
1110 ;; so everything is in order for its use in Org files
1111 (org-link-set-parameters "tel")
1113 (defun org-contacts-split-property (string &optional separators omit-nulls
)
1114 "Custom version of `split-string'.
1115 Split a property STRING into sub-strings bounded by matches
1116 for SEPARATORS but keep Org links intact.
1118 The beginning and end of STRING, and each match for SEPARATORS, are
1119 splitting points. The substrings matching SEPARATORS are removed, and
1120 the substrings between the splitting points are collected as a list,
1123 If SEPARATORS is non-nil, it should be a regular expression
1124 matching text which separates, but is not part of, the
1125 substrings. If nil it defaults to `org-contacts-property-values-separators',
1126 normally \"[,; \f\t\n\r\v]+\", and OMIT-NULLS is forced to t.
1128 If OMIT-NULLS is t, zero-length substrings are omitted from the list \(so
1129 that for the default value of SEPARATORS leading and trailing whitespace
1130 are effectively trimmed). If nil, all zero-length substrings are retained."
1131 (let* ((omit-nulls (if separators omit-nulls t
))
1132 (rexp (or separators org-contacts-property-values-separators
))
1133 (inputlist (split-string string rexp omit-nulls
))
1136 (proplist (list "")))
1138 (setq bufferstring
(pop inputlist
))
1139 (if (string-match "\\[\\[" bufferstring
)
1141 (setq linkstring
(concat bufferstring
" "))
1142 (while (not (string-match "\\]\\]" bufferstring
))
1143 (setq bufferstring
(pop inputlist
))
1144 (setq linkstring
(concat linkstring bufferstring
" ")))
1145 (setq proplist
(cons (org-trim linkstring
) proplist
)))
1146 (setq proplist
(cons bufferstring proplist
))))
1147 (cdr (reverse proplist
))))
1149 (provide 'org-contacts
)
1151 ;;; org-contacts.el ends here