org-contacts.el 32 KB

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