org-contacts.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. ;;; org-contacts.el --- Contacts management
  2. ;; Copyright (C) 2010-2013 Julien Danjou <julien@danjou.info>
  3. ;; Author: Julien Danjou <julien@danjou.info>
  4. ;; Keywords: outlines, hypermedia, calendar
  5. ;;
  6. ;; This file is NOT part of GNU Emacs.
  7. ;;
  8. ;; This program is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. ;;
  20. ;;; Commentary:
  21. ;; This file contains the code for managing your contacts into Org-mode.
  22. ;; To enter new contacts, you can use `org-capture' and a template just like
  23. ;; this:
  24. ;; ("c" "Contacts" entry (file "~/Org/contacts.org")
  25. ;; "* %(org-contacts-template-name)
  26. ;; :PROPERTIES:
  27. ;; :EMAIL: %(org-contacts-template-email)
  28. ;; :END:")))
  29. ;;
  30. ;;; Code:
  31. (eval-when-compile
  32. (require 'cl))
  33. (require 'org)
  34. (require 'gnus-util)
  35. (require 'gnus-art)
  36. (require 'mail-utils)
  37. (require 'org-agenda)
  38. (require 'org-capture)
  39. (defgroup org-contacts nil
  40. "Options about contacts management."
  41. :group 'org)
  42. (defcustom org-contacts-files nil
  43. "List of Org files to use as contacts source.
  44. When set to nil, all your Org files will be used."
  45. :type '(repeat file)
  46. :group 'org-contacts)
  47. (defcustom org-contacts-email-property "EMAIL"
  48. "Name of the property for contact email address."
  49. :type 'string
  50. :group 'org-contacts)
  51. (defcustom org-contacts-address-property "ADDRESS"
  52. "Name of the property for contact address."
  53. :type 'string
  54. :group 'org-contacts)
  55. (defcustom org-contacts-birthday-property "BIRTHDAY"
  56. "Name of the property for contact birthday date."
  57. :type 'string
  58. :group 'org-contacts)
  59. (defcustom org-contacts-birthday-format "Birthday: %l (%Y)"
  60. "Format of the anniversary agenda entry.
  61. The following replacements are available:
  62. %h - Heading name
  63. %l - Link to the heading
  64. %y - Number of year
  65. %Y - Number of year (ordinal)"
  66. :type 'string
  67. :group 'org-contacts)
  68. (defcustom org-contacts-last-read-mail-property "LAST_READ_MAIL"
  69. "Name of the property for contact last read email link storage."
  70. :type 'string
  71. :group 'org-contacts)
  72. (defcustom org-contacts-icon-property "ICON"
  73. "Name of the property for contact icon."
  74. :type 'string
  75. :group 'org-contacts)
  76. (defcustom org-contacts-nickname-property "NICKNAME"
  77. "Name of the property for IRC nickname match."
  78. :type 'string
  79. :group 'org-contacts)
  80. (defcustom org-contacts-icon-size 32
  81. "Size of the contacts icons."
  82. :type 'string
  83. :group 'org-contacts)
  84. (defcustom org-contacts-icon-use-gravatar (fboundp 'gravatar-retrieve)
  85. "Whether use Gravatar to fetch contact icons."
  86. :type 'boolean
  87. :group 'org-contacts)
  88. (defcustom org-contacts-completion-ignore-case t
  89. "Ignore case when completing contacts."
  90. :type 'boolean
  91. :group 'org-contacts)
  92. (defcustom org-contacts-group-prefix "+"
  93. "Group prefix."
  94. :type 'string
  95. :group 'org-contacts)
  96. (defcustom org-contacts-matcher (concat org-contacts-email-property "<>\"\"")
  97. "Matching rule for finding heading that are contacts.
  98. This can be a tag name, or a property check."
  99. :type 'string
  100. :group 'org-contacts)
  101. (defcustom org-contacts-email-link-description-format "%s (%d)"
  102. "Format used to store links to email.
  103. This overrides `org-email-link-description-format' if set."
  104. :group 'org-contacts
  105. :type 'string)
  106. (defcustom org-contacts-vcard-file "contacts.vcf"
  107. "Default file for vcard export."
  108. :group 'org-contacts
  109. :type 'file)
  110. (defcustom org-contacts-enable-completion t
  111. "Enable or not the completion in `message-mode' with `org-contacts'."
  112. :group 'org-contacts
  113. :type 'boolean)
  114. ;; Decalre external functions and variables
  115. (declare-function org-reverse-string "org")
  116. (declare-function org-install-letbind "org-exp")
  117. (declare-function diary-ordinal-suffix "ext:diary-lib")
  118. (declare-function wl-summary-message-number "ext:wl-summary")
  119. (declare-function wl-address-header-extract-address "ext:wl-address")
  120. (declare-function wl-address-header-extract-realname "ext:wl-address")
  121. (declare-function erc-buffer-list "ext:erc")
  122. (declare-function erc-get-channel-user-list "ext:erc")
  123. (declare-function google-maps-static-show "ext:google-maps-static")
  124. (declare-function elmo-message-field "ext:elmo-pipe")
  125. (declare-function std11-narrow-to-header "ext:std11")
  126. (declare-function std11-fetch-field "ext:std11")
  127. (defvar org-contacts-keymap
  128. (let ((map (make-sparse-keymap)))
  129. (define-key map "M" 'org-contacts-view-send-email)
  130. (define-key map "i" 'org-contacts-view-switch-to-irc-buffer)
  131. map)
  132. "The keymap used in `org-contacts' result list.")
  133. (defvar org-contacts-db nil
  134. "Org Contacts database.")
  135. (defvar org-contacts-last-update nil
  136. "Last time the Org Contacts database has been updated.")
  137. (defun org-contacts-files ()
  138. "Return list of Org files to use for contact management."
  139. (or org-contacts-files (org-agenda-files t 'ifmode)))
  140. (defun org-contacts-db-need-update-p ()
  141. "Determine whether `org-contacts-db' needs to be refreshed."
  142. (or (null org-contacts-last-update)
  143. (org-find-if (lambda (file)
  144. (or (time-less-p org-contacts-last-update
  145. (elt (file-attributes file) 5))))
  146. (org-contacts-files))))
  147. (defun org-contacts-db ()
  148. "Return the latest Org Contacts Database."
  149. (let* (todo-only
  150. (contacts-matcher
  151. (cdr (org-make-tags-matcher org-contacts-matcher)))
  152. markers result)
  153. (when (org-contacts-db-need-update-p)
  154. (message "Update Org Contacts Database")
  155. (dolist (file (org-contacts-files))
  156. (org-check-agenda-file file)
  157. (with-current-buffer (org-get-agenda-file-buffer file)
  158. (unless (eq major-mode 'org-mode)
  159. (error "File %s is no in `org-mode'" file))
  160. (org-scan-tags
  161. '(add-to-list 'markers (set-marker (make-marker) (point)))
  162. contacts-matcher
  163. todo-only)))
  164. (dolist (marker markers result)
  165. (org-with-point-at marker
  166. (add-to-list 'result
  167. (list (org-get-heading t) marker (org-entry-properties marker 'all)))))
  168. (setf org-contacts-db result
  169. org-contacts-last-update (current-time)))
  170. org-contacts-db))
  171. (defun org-contacts-filter (&optional name-match tags-match)
  172. "Search for a contact maching NAME-MATCH and TAGS-MATCH.
  173. If both match values are nil, return all contacts."
  174. (if (and (null name-match)
  175. (null tags-match))
  176. (org-contacts-db)
  177. (loop for contact in (org-contacts-db)
  178. if (or
  179. (and name-match
  180. (org-string-match-p name-match
  181. (first contact)))
  182. (and tags-match
  183. (org-find-if (lambda (tag)
  184. (org-string-match-p tags-match tag))
  185. (org-split-string
  186. (or (cdr (assoc-string "ALLTAGS" (caddr contact))) "") ":"))))
  187. collect contact)))
  188. (when (not (fboundp 'completion-table-case-fold))
  189. ;; That function is new in Emacs 24...
  190. (defun completion-table-case-fold (table &optional dont-fold)
  191. (lambda (string pred action)
  192. (let ((completion-ignore-case (not dont-fold)))
  193. (complete-with-action action table string pred)))))
  194. (defun org-contacts-try-completion-prefix (to-match collection &optional predicate)
  195. "Custom implementation of `try-completion'.
  196. This version works only with list and alist and it looks at all
  197. prefixes rather than just the beginning of the string."
  198. (loop with regexp = (concat "\\b" (regexp-quote to-match))
  199. with ret = nil
  200. with ret-start = nil
  201. with ret-end = nil
  202. for el in collection
  203. for string = (if (listp el) (car el) el)
  204. for start = (when (or (null predicate) (funcall predicate string))
  205. (string-match regexp string))
  206. if start
  207. do (let ((end (match-end 0))
  208. (len (length string)))
  209. (if (= end len)
  210. (return t)
  211. (destructuring-bind (string start end)
  212. (if (null ret)
  213. (values string start end)
  214. (org-contacts-common-substring
  215. ret ret-start ret-end
  216. string start end))
  217. (setf ret string
  218. ret-start start
  219. ret-end end))))
  220. finally (return
  221. (replace-regexp-in-string "\\`[ \t\n]*" "" ret))))
  222. (defun org-contacts-compare-strings (s1 start1 end1 s2 start2 end2 &optional ignore-case)
  223. "Compare the contents of two strings, using `compare-strings'.
  224. This function works like `compare-strings' excepted that it
  225. returns a cons.
  226. - The CAR is the number of characters that match at the beginning.
  227. - The CDR is T is the two strings are the same and NIL otherwise."
  228. (let ((ret (compare-strings s1 start1 end1 s2 start2 end2 ignore-case)))
  229. (if (eq ret t)
  230. (cons (or end1 (length s1)) t)
  231. (cons (1- (abs ret)) nil))))
  232. (defun org-contacts-common-substring (s1 start1 end1 s2 start2 end2)
  233. "Extract the common substring between S1 and S2.
  234. This function extracts the common substring between S1 and S2 and
  235. adjust the part that remains common.
  236. START1 and END1 delimit the part in S1 that we know is common
  237. between the two strings. This applies to START2 and END2 for S2.
  238. This function returns a list whose contains:
  239. - The common substring found.
  240. - The new value of the start of the known inner substring.
  241. - The new value of the end of the known inner substring."
  242. ;; Given two strings:
  243. ;; s1: "foo bar baz"
  244. ;; s2: "fooo bar baz"
  245. ;; and the inner substring is "bar"
  246. ;; then: start1 = 4, end1 = 6, start2 = 5, end2 = 7
  247. ;;
  248. ;; To find the common substring we will compare two substrings:
  249. ;; " oof" and " ooof" to find the beginning of the common substring.
  250. ;; " baz" and " baz" to find the end of the common substring.
  251. (let* ((len1 (length s1))
  252. (start1 (or start1 0))
  253. (end1 (or end1 len1))
  254. (len2 (length s2))
  255. (start2 (or start2 0))
  256. (end2 (or end2 len2))
  257. (new-start (car (org-contacts-compare-strings
  258. (substring (org-reverse-string s1) (- len1 start1)) nil nil
  259. (substring (org-reverse-string s2) (- len2 start2)) nil nil)))
  260. (new-end (+ end1 (car (org-contacts-compare-strings
  261. (substring s1 end1) nil nil
  262. (substring s2 end2) nil nil)))))
  263. (list (substring s1 (- start1 new-start) new-end)
  264. new-start
  265. (+ new-start (- end1 start1)))))
  266. (defun org-contacts-all-completions-prefix (to-match collection &optional predicate)
  267. "Custom version of `all-completions'.
  268. This version works only with list and alist and it looks at all
  269. prefixes rather than just the beginning of the string."
  270. (loop with regexp = (concat "\\b" (regexp-quote to-match))
  271. for el in collection
  272. for string = (if (listp el) (car el) el)
  273. for match? = (when (and (or (null predicate) (funcall predicate string)))
  274. (string-match regexp string))
  275. if match?
  276. collect (progn
  277. (let ((end (match-end 0)))
  278. (org-no-properties string)
  279. (when (< end (length string))
  280. ;; Here we add a text property that will be used
  281. ;; later to highlight the character right after
  282. ;; the common part between each addresses.
  283. ;; See `org-contacts-display-sort-function'.
  284. (put-text-property end (1+ end) 'org-contacts-prefix 't string)))
  285. string)))
  286. (defun org-contacts-make-collection-prefix (collection)
  287. "Make a collection function from COLLECTION which will match on prefixes."
  288. (lexical-let ((collection collection))
  289. (lambda (string predicate flag)
  290. (cond ((eq flag nil)
  291. (org-contacts-try-completion-prefix string collection predicate))
  292. ((eq flag t)
  293. ;; `org-contacts-all-completions-prefix' has already been
  294. ;; used to compute `all-completions'.
  295. collection)
  296. ((eq flag 'lambda)
  297. (org-contacts-test-completion-prefix string collection predicate))
  298. ((and (listp flag) (eq (car flag) 'boundaries))
  299. (destructuring-bind (to-ignore &rest suffix)
  300. flag
  301. (org-contacts-boundaries-prefix string collection predicate suffix)))
  302. ((eq flag 'metadata)
  303. (org-contacts-metadata-prefix string collection predicate))
  304. (t nil ; operation unsupported
  305. )))))
  306. (defun org-contacts-display-sort-function (completions)
  307. "Sort function for contacts display."
  308. (mapcar (lambda (string)
  309. (loop with len = (1- (length string))
  310. for i upfrom 0 to len
  311. if (memq 'org-contacts-prefix
  312. (text-properties-at i string))
  313. do (set-text-properties
  314. i (1+ i)
  315. (list 'font-lock-face
  316. (if (char-equal (aref string i)
  317. (string-to-char " "))
  318. ;; Spaces can't be bold.
  319. 'underline
  320. 'bold)) string)
  321. else
  322. do (set-text-properties i (1+ i) nil string)
  323. finally (return string)))
  324. completions))
  325. (defun org-contacts-test-completion-prefix (string collection predicate)
  326. ;; Prevents `org-find-if' from redefining `predicate' and going into
  327. ;; an infinite loop.
  328. (lexical-let ((predicate predicate))
  329. (org-find-if (lambda (el)
  330. (and (or (null predicate) (funcall predicate el))
  331. (string= string el)))
  332. collection)))
  333. (defun org-contacts-boundaries-prefix (string collection predicate suffix)
  334. (list* 'boundaries (completion-boundaries string collection predicate suffix)))
  335. (defun org-contacts-metadata-prefix (string collection predicate)
  336. '(metadata .
  337. ((display-sort-function . org-contacts-display-sort-function))))
  338. (defun org-contacts-complete-group (start end string)
  339. "Complete text at START from a group.
  340. A group FOO is composed of contacts with the tag FOO."
  341. (let* ((completion-ignore-case org-contacts-completion-ignore-case)
  342. (group-completion-p (org-string-match-p
  343. (concat "^" org-contacts-group-prefix) string)))
  344. (when group-completion-p
  345. (let ((completion-list
  346. (all-completions
  347. string
  348. (mapcar (lambda (group)
  349. (propertize (concat org-contacts-group-prefix group)
  350. 'org-contacts-group group))
  351. (org-uniquify
  352. (loop for contact in (org-contacts-filter)
  353. nconc (org-split-string
  354. (or (cdr (assoc-string "ALLTAGS" (caddr contact))) "") ":")))))))
  355. (list start end
  356. (if (= (length completion-list) 1)
  357. ;; We've foudn the correct group, returns the address
  358. (lexical-let ((tag (get-text-property 0 'org-contacts-group
  359. (car completion-list))))
  360. (lambda (string pred &optional to-ignore)
  361. (mapconcat 'identity
  362. (loop for contact in (org-contacts-filter
  363. nil
  364. tag)
  365. ;; The contact name is always the car of the assoc-list
  366. ;; returned by `org-contacts-filter'.
  367. for contact-name = (car contact)
  368. ;; Grab the first email of the contact
  369. for email = (car (split-string
  370. (or
  371. (cdr (assoc-string org-contacts-email-property
  372. (caddr contact)))
  373. "")))
  374. ;; If the user has an email address, append USER <EMAIL>.
  375. if email collect (org-contacts-format-email contact-name email))
  376. ", ")))
  377. ;; We haven't found the correct group
  378. (completion-table-case-fold completion-list
  379. (not org-contacts-completion-ignore-case))))))))
  380. (defun org-contacts-complete-name (start end string)
  381. "Complete text at START with a user name and email."
  382. (let* ((completion-ignore-case org-contacts-completion-ignore-case)
  383. (completion-list
  384. (loop for contact in (org-contacts-filter)
  385. ;; The contact name is always the car of the assoc-list
  386. ;; returned by `org-contacts-filter'.
  387. for contact-name = (car contact)
  388. ;; Build the list of the user email addresses.
  389. for email-list = (split-string (or
  390. (cdr (assoc-string org-contacts-email-property
  391. (caddr contact))) ""))
  392. ;; If the user has email addresses…
  393. if email-list
  394. ;; … append a list of USER <EMAIL>.
  395. nconc (loop for email in email-list
  396. collect (org-contacts-format-email contact-name email))))
  397. (completion-list (org-contacts-all-completions-prefix
  398. string
  399. (org-uniquify completion-list))))
  400. (when completion-list
  401. (list start end
  402. (org-contacts-make-collection-prefix completion-list)))))
  403. (defun org-contacts-message-complete-function (&optional start)
  404. "Function used in `completion-at-point-functions' in `message-mode'."
  405. ;; Avoid to complete in `post-command-hook'.
  406. (when completion-in-region-mode
  407. (remove-hook 'post-command-hook #'completion-in-region--postch))
  408. (let ((mail-abbrev-mode-regexp
  409. "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
  410. (when (mail-abbrev-in-expansion-header-p)
  411. (lexical-let*
  412. ((end (point))
  413. (start (or start
  414. (save-excursion
  415. (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
  416. (goto-char (match-end 0))
  417. (point))))
  418. (string (buffer-substring start end)))
  419. (or (org-contacts-complete-group start end string)
  420. (org-contacts-complete-name start end string))))))
  421. (defun org-contacts-gnus-get-name-email ()
  422. "Get name and email address from Gnus message."
  423. (if (gnus-alive-p)
  424. (gnus-with-article-headers
  425. (mail-extract-address-components
  426. (or (mail-fetch-field "From") "")))))
  427. (defun org-contacts-gnus-article-from-get-marker ()
  428. "Return a marker for a contact based on From."
  429. (let* ((address (org-contacts-gnus-get-name-email))
  430. (name (car address))
  431. (email (cadr address)))
  432. (cadar (or (org-contacts-filter
  433. nil
  434. (concat org-contacts-email-property "={\\b" (regexp-quote email) "\\b}"))
  435. (when name
  436. (org-contacts-filter
  437. (concat "^" name "$")))))))
  438. (defun org-contacts-gnus-article-from-goto ()
  439. "Go to contact in the From address of current Gnus message."
  440. (interactive)
  441. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  442. (when marker
  443. (switch-to-buffer-other-window (marker-buffer marker))
  444. (goto-char marker)
  445. (when (eq major-mode 'org-mode)
  446. (org-show-context 'agenda)
  447. (save-excursion
  448. (and (outline-next-heading)
  449. ;; show the next heading
  450. (org-flag-heading nil)))))))
  451. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  452. (defun org-contacts-anniversaries (&optional field format)
  453. "Compute FIELD anniversary for each contact, returning FORMAT.
  454. Default FIELD value is \"BIRTHDAY\".
  455. Format is a string matching the following format specification:
  456. %h - Heading name
  457. %l - Link to the heading
  458. %y - Number of year
  459. %Y - Number of year (ordinal)"
  460. (let ((calendar-date-style 'american)
  461. (entry ""))
  462. (unless format (setq format org-contacts-birthday-format))
  463. (loop for contact in (org-contacts-filter)
  464. for anniv = (let ((anniv (cdr (assoc-string
  465. (or field org-contacts-birthday-property)
  466. (caddr contact)))))
  467. (when anniv
  468. (calendar-gregorian-from-absolute
  469. (org-time-string-to-absolute anniv))))
  470. ;; Use `diary-anniversary' to compute anniversary.
  471. if (and anniv (apply 'diary-anniversary anniv))
  472. collect (format-spec format
  473. `((?l . ,(org-with-point-at (cadr contact) (org-store-link nil)))
  474. (?h . ,(car contact))
  475. (?y . ,(- (calendar-extract-year date)
  476. (calendar-extract-year anniv)))
  477. (?Y . ,(let ((years (- (calendar-extract-year date)
  478. (calendar-extract-year anniv))))
  479. (format "%d%s" years (diary-ordinal-suffix years)))))))))
  480. (defun org-completing-read-date (prompt collection
  481. &optional predicate require-match initial-input
  482. hist def inherit-input-method)
  483. "Like `completing-read' but reads a date.
  484. Only PROMPT and DEF are really used."
  485. (org-read-date nil nil nil prompt nil def))
  486. (add-to-list 'org-property-set-functions-alist
  487. `(,org-contacts-birthday-property . org-completing-read-date))
  488. (defun org-contacts-template-name (&optional return-value)
  489. "Try to return the contact name for a template.
  490. If not found return RETURN-VALUE or something that would ask the user."
  491. (or (car (org-contacts-gnus-get-name-email))
  492. return-value
  493. "%^{Name}"))
  494. (defun org-contacts-template-email (&optional return-value)
  495. "Try to return the contact email for a template.
  496. If not found return RETURN-VALUE or something that would ask the user."
  497. (or (cadr (org-contacts-gnus-get-name-email))
  498. return-value
  499. (concat "%^{" org-contacts-email-property "}p")))
  500. (defun org-contacts-gnus-store-last-mail ()
  501. "Store a link between mails and contacts.
  502. This function should be called from `gnus-article-prepare-hook'."
  503. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  504. (when marker
  505. (with-current-buffer (marker-buffer marker)
  506. (save-excursion
  507. (goto-char marker)
  508. (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
  509. org-email-link-description-format))
  510. (link (gnus-with-article-buffer (org-store-link nil))))
  511. (org-set-property org-contacts-last-read-mail-property link)))))))
  512. (defun org-contacts-icon-as-string ()
  513. "Return the contact icon as a string."
  514. (let ((image (org-contacts-get-icon)))
  515. (concat
  516. (propertize "-" 'display
  517. (append
  518. (if image
  519. image
  520. `'(space :width (,org-contacts-icon-size)))
  521. '(:ascent center)))
  522. " ")))
  523. ;;;###autoload
  524. (defun org-contacts (name)
  525. "Create agenda view for contacts matching NAME."
  526. (interactive (list (read-string "Name: ")))
  527. (let ((org-agenda-files (org-contacts-files))
  528. (org-agenda-skip-function
  529. (lambda () (org-agenda-skip-if nil `(notregexp ,name))))
  530. (org-agenda-prefix-format (propertize
  531. "%(org-contacts-icon-as-string)% s%(org-contacts-irc-number-of-unread-messages) "
  532. 'keymap org-contacts-keymap))
  533. (org-agenda-overriding-header
  534. (or org-agenda-overriding-header
  535. (concat "List of contacts matching `" name "':"))))
  536. (setq org-agenda-skip-regexp name)
  537. (org-tags-view nil org-contacts-matcher)
  538. (with-current-buffer org-agenda-buffer-name
  539. (setq org-agenda-redo-command
  540. (list 'org-contacts name)))))
  541. (defun org-contacts-completing-read (prompt
  542. &optional predicate
  543. initial-input hist def inherit-input-method)
  544. "Call `completing-read' with contacts name as collection."
  545. (org-completing-read
  546. prompt (org-contacts-filter) predicate t initial-input hist def inherit-input-method))
  547. (defun org-contacts-format-name (name)
  548. "Trim any local formatting to get a bare NAME."
  549. ;; Remove radio targets characters
  550. (replace-regexp-in-string org-radio-target-regexp "\\1" name))
  551. (defun org-contacts-format-email (name email)
  552. "Format an EMAIL address corresponding to NAME."
  553. (unless email
  554. (error "`email' cannot be nul"))
  555. (if name
  556. (concat (org-contacts-format-name name) " <" email ">")
  557. email))
  558. (defun org-contacts-check-mail-address (mail)
  559. "Add MAIL address to contact at point if it does not have it."
  560. (let ((mails (org-entry-get (point) org-contacts-email-property)))
  561. (unless (member mail (split-string mails))
  562. (when (yes-or-no-p
  563. (format "Do you want to add this address to %s?" (org-get-heading t)))
  564. (org-set-property org-contacts-email-property (concat mails " " mail))))))
  565. (defun org-contacts-gnus-check-mail-address ()
  566. "Check that contact has the current address recorded.
  567. This function should be called from `gnus-article-prepare-hook'."
  568. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  569. (when marker
  570. (org-with-point-at marker
  571. (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
  572. (defun org-contacts-gnus-insinuate ()
  573. "Add some hooks for Gnus user.
  574. This adds `org-contacts-gnus-check-mail-address' and
  575. `org-contacts-gnus-store-last-mail' to
  576. `gnus-article-prepare-hook'. It also adds a binding on `;' in
  577. `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
  578. (require 'gnus)
  579. (require 'gnus-art)
  580. (define-key gnus-summary-mode-map ";" 'org-contacts-gnus-article-from-goto)
  581. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-check-mail-address)
  582. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-store-last-mail))
  583. (when (and org-contacts-enable-completion
  584. (boundp 'completion-at-point-functions))
  585. (add-hook 'message-mode-hook
  586. (lambda ()
  587. (add-to-list 'completion-at-point-functions
  588. 'org-contacts-message-complete-function))))
  589. (defun org-contacts-wl-get-from-header-content ()
  590. "Retrieve the content of the `From' header of an email.
  591. Works from wl-summary-mode and mime-view-mode - that is while viewing email.
  592. Depends on Wanderlust been loaded."
  593. (with-current-buffer (org-capture-get :original-buffer)
  594. (cond
  595. ((eq major-mode 'wl-summary-mode) (when (and (boundp 'wl-summary-buffer-elmo-folder)
  596. wl-summary-buffer-elmo-folder)
  597. (elmo-message-field
  598. wl-summary-buffer-elmo-folder
  599. (wl-summary-message-number)
  600. 'from)))
  601. ((eq major-mode 'mime-view-mode) (std11-narrow-to-header)
  602. (prog1
  603. (std11-fetch-field "From")
  604. (widen))))))
  605. (defun org-contacts-wl-get-name-email ()
  606. "Get name and email address from Wanderlust email.
  607. See `org-contacts-wl-get-from-header-content' for limitations."
  608. (let ((from (org-contacts-wl-get-from-header-content)))
  609. (when from
  610. (list (wl-address-header-extract-realname from)
  611. (wl-address-header-extract-address from)))))
  612. (defun org-contacts-template-wl-name (&optional return-value)
  613. "Try to return the contact name for a template from wl.
  614. If not found, return RETURN-VALUE or something that would ask the
  615. user."
  616. (or (car (org-contacts-wl-get-name-email))
  617. return-value
  618. "%^{Name}"))
  619. (defun org-contacts-template-wl-email (&optional return-value)
  620. "Try to return the contact email for a template from Wanderlust.
  621. If not found return RETURN-VALUE or something that would ask the user."
  622. (or (cadr (org-contacts-wl-get-name-email))
  623. return-value
  624. (concat "%^{" org-contacts-email-property "}p")))
  625. (defun org-contacts-view-send-email (&optional ask)
  626. "Send email to the contact at point.
  627. If ASK is set, ask for the email address even if there's only one
  628. address."
  629. (interactive "P")
  630. (let ((marker (org-get-at-bol 'org-hd-marker)))
  631. (org-with-point-at marker
  632. (let ((emails (org-entry-get (point) org-contacts-email-property)))
  633. (if emails
  634. (let ((email-list (split-string emails)))
  635. (if (and (= (length email-list) 1) (not ask))
  636. (compose-mail (org-contacts-format-email
  637. (org-get-heading t) emails))
  638. (let ((email (completing-read "Send mail to which address: " email-list)))
  639. (org-contacts-check-mail-address email)
  640. (compose-mail (org-contacts-format-email (org-get-heading t) email)))))
  641. (error (format "This contact has no mail address set (no %s property)."
  642. org-contacts-email-property)))))))
  643. (defun org-contacts-get-icon (&optional pom)
  644. "Get icon for contact at POM."
  645. (setq pom (or pom (point)))
  646. (catch 'icon
  647. ;; Use `org-contacts-icon-property'
  648. (let ((image-data (org-entry-get pom org-contacts-icon-property)))
  649. (when image-data
  650. (throw 'icon
  651. (if (fboundp 'gnus-rescale-image)
  652. (gnus-rescale-image (create-image image-data)
  653. (cons org-contacts-icon-size org-contacts-icon-size))
  654. (create-image image-data)))))
  655. ;; Next, try Gravatar
  656. (when org-contacts-icon-use-gravatar
  657. (let* ((gravatar-size org-contacts-icon-size)
  658. (email-list (org-entry-get pom org-contacts-email-property))
  659. (gravatar
  660. (when email-list
  661. (loop for email in (split-string email-list)
  662. for gravatar = (gravatar-retrieve-synchronously email)
  663. if (and gravatar
  664. (not (eq gravatar 'error)))
  665. return gravatar))))
  666. (when gravatar (throw 'icon gravatar))))))
  667. (defun org-contacts-irc-buffer (&optional pom)
  668. "Get the IRC buffer associated with the entry at POM."
  669. (setq pom (or pom (point)))
  670. (let ((nick (org-entry-get pom org-contacts-nickname-property)))
  671. (when nick
  672. (let ((buffer (get-buffer nick)))
  673. (when buffer
  674. (with-current-buffer buffer
  675. (when (eq major-mode 'erc-mode)
  676. buffer)))))))
  677. (defun org-contacts-irc-number-of-unread-messages (&optional pom)
  678. "Return the number of unread messages for contact at POM."
  679. (when (boundp 'erc-modified-channels-alist)
  680. (let ((number (cadr (assoc (org-contacts-irc-buffer pom) erc-modified-channels-alist))))
  681. (if number
  682. (format (concat "%3d unread message" (if (> number 1) "s" " ") " ") number)
  683. (make-string 21 ? )))))
  684. (defun org-contacts-view-switch-to-irc-buffer ()
  685. "Switch to the IRC buffer of the current contact if it has one."
  686. (interactive)
  687. (let ((marker (org-get-at-bol 'org-hd-marker)))
  688. (org-with-point-at marker
  689. (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
  690. (defun org-contacts-completing-read-nickname (prompt collection
  691. &optional predicate require-match initial-input
  692. hist def inherit-input-method)
  693. "Like `completing-read' but reads a nickname."
  694. (org-completing-read prompt (append collection (erc-nicknames-list)) predicate require-match
  695. initial-input hist def inherit-input-method))
  696. (defun erc-nicknames-list ()
  697. "Return all nicknames of all ERC buffers."
  698. (loop for buffer in (erc-buffer-list)
  699. nconc (with-current-buffer buffer
  700. (loop for user-entry in (mapcar 'car (erc-get-channel-user-list))
  701. collect (elt user-entry 1)))))
  702. (add-to-list 'org-property-set-functions-alist
  703. `(,org-contacts-nickname-property . org-contacts-completing-read-nickname))
  704. (defun org-contacts-vcard-escape (str)
  705. "Escape ; , and \n in STR for the VCard format."
  706. ;; Thanks to this library for the regexp:
  707. ;; http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el
  708. (when str
  709. (replace-regexp-in-string
  710. "\n" "\\\\n"
  711. (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str))))
  712. (defun org-contacts-vcard-encode-name (name)
  713. "Try to encode NAME as VCard's N property.
  714. The N property expects
  715. FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
  716. Org-contacts does not specify how to encode the name. So we try
  717. to do our best."
  718. (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name) ";;;"))
  719. (defun org-contacts-vcard-format (contact)
  720. "Formats CONTACT in VCard 3.0 format."
  721. (let* ((properties (caddr contact))
  722. (name (org-contacts-vcard-escape (car contact)))
  723. (n (org-contacts-vcard-encode-name name))
  724. (email (org-contacts-vcard-escape (cdr (assoc-string org-contacts-email-property properties))))
  725. (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties))))
  726. (addr (cdr (assoc-string org-contacts-address-property properties)))
  727. (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties))))
  728. (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name)))
  729. (concat head
  730. (when email (format "EMAIL:%s\n" email))
  731. (when addr
  732. (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr)))
  733. (when bday
  734. (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday))))
  735. (format "BDAY:%04d-%02d-%02d\n"
  736. (calendar-extract-year cal-bday)
  737. (calendar-extract-month cal-bday)
  738. (calendar-extract-day cal-bday))))
  739. (when nick (format "NICKNAME:%s\n" nick))
  740. "END:VCARD\n\n")))
  741. (defun org-contacts-export-as-vcard (&optional name file to-buffer)
  742. "Export all contacts matching NAME as VCard 3.0.
  743. If TO-BUFFER is nil, the content is written to FILE or
  744. `org-contacts-vcard-file'. If TO-BUFFER is non-nil, the buffer
  745. is created and the VCard is written into that buffer."
  746. (interactive) ; TODO ask for name?
  747. (let* ((filename (or file org-contacts-vcard-file))
  748. (buffer (if to-buffer
  749. (get-buffer-create to-buffer)
  750. (find-file-noselect filename))))
  751. (message "Exporting...")
  752. (set-buffer buffer)
  753. (let ((inhibit-read-only t)) (erase-buffer))
  754. (fundamental-mode)
  755. (org-install-letbind)
  756. (when (fboundp 'set-buffer-file-coding-system)
  757. (set-buffer-file-coding-system coding-system-for-write))
  758. (loop for contact in (org-contacts-filter name)
  759. do (insert (org-contacts-vcard-format contact)))
  760. (if to-buffer
  761. (current-buffer)
  762. (progn (save-buffer) (kill-buffer)))))
  763. (defun org-contacts-show-map (&optional name)
  764. "Show contacts on a map.
  765. Requires google-maps-el."
  766. (interactive)
  767. (unless (fboundp 'google-maps-static-show)
  768. (error "`org-contacts-show-map' requires `google-maps-el'"))
  769. (google-maps-static-show
  770. :markers
  771. (loop
  772. for contact in (org-contacts-filter name)
  773. for addr = (cdr (assoc-string org-contacts-address-property (caddr contact)))
  774. if addr
  775. collect (cons (list addr) (list :label (string-to-char (car contact)))))))
  776. (provide 'org-contacts)
  777. (provide 'org-contacts)
  778. ;;; org-contacts.el ends here