org-contacts.el 32 KB

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