org-contacts.el 32 KB

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