org-contacts.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. ;;; org-contacts.el --- Contacts management
  2. ;; Copyright (C) 2010, 2011 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-and-compile
  32. (require 'org))
  33. (defgroup org-contacts nil
  34. "Options concerning contacts management."
  35. :group 'org)
  36. (defcustom org-contacts-files nil
  37. "List of Org files to use as contacts source.
  38. If set to nil, all your Org files will be used."
  39. :type '(repeat file)
  40. :group 'org-contacts)
  41. (defcustom org-contacts-email-property "EMAIL"
  42. "Name of the property for contact email address."
  43. :type 'string
  44. :group 'org-contacts)
  45. (defcustom org-contacts-birthday-property "BIRTHDAY"
  46. "Name of the property for contact birthday date."
  47. :type 'string
  48. :group 'org-contacts)
  49. (defcustom org-contacts-birthday-format "Birthday: %l (%Y)"
  50. "Format of the anniversary agenda entry. The following replacements are available:
  51. %h - Heading name
  52. %l - Link to the heading
  53. %y - Number of year
  54. %Y - Number of year (ordinal)"
  55. :type 'string
  56. :group 'org-contacts)
  57. (defcustom org-contacts-last-read-mail-property "LAST_READ_MAIL"
  58. "Name of the property for contact last read email link storage."
  59. :type 'string
  60. :group 'org-contacts)
  61. (defcustom org-contacts-icon-property "ICON"
  62. "Name of the property for contact icon."
  63. :type 'string
  64. :group 'org-contacts)
  65. (defcustom org-contacts-nickname-property "NICKNAME"
  66. "Name of the property for IRC nickname match."
  67. :type 'string
  68. :group 'org-contacts)
  69. (defcustom org-contacts-icon-size 32
  70. "Size of the contacts icons."
  71. :type 'string
  72. :group 'org-contacts)
  73. (defcustom org-contacts-icon-use-gravatar (fboundp 'gravatar-retrieve)
  74. "Whether use Gravatar to fetch contact icons."
  75. :type 'boolean
  76. :group 'org-contacts)
  77. (defcustom org-contacts-completion-ignore-case t
  78. "Ignore case when completing contacts."
  79. :type 'boolean
  80. :group 'org-contacts)
  81. (defcustom org-contacts-group-prefix "+"
  82. "Group prefix."
  83. :type 'string
  84. :group 'org-contacts)
  85. (defcustom org-contacts-matcher (concat org-contacts-email-property "<>\"\"")
  86. "Matching rule for finding heading that are contacts.
  87. This can be a tag name, or a property check."
  88. :type 'string
  89. :group 'org-contacts)
  90. (defcustom org-contacts-email-link-description-format "%s (%d)"
  91. "Format used to store links to email.
  92. This overrides `org-email-link-description-format' if set."
  93. :group 'org-contacts
  94. :type 'string)
  95. (defvar org-contacts-keymap
  96. (let ((map (make-sparse-keymap)))
  97. (define-key map "M" 'org-contacts-view-send-email)
  98. (define-key map "i" 'org-contacts-view-switch-to-irc-buffer)
  99. map)
  100. "The keymap used in `org-contacts' result list.")
  101. (defun org-contacts-files ()
  102. "Return list of Org files to use for contact management."
  103. (or org-contacts-files (org-agenda-files t 'ifmode)))
  104. (defun org-contacts-filter (&optional name-match tags-match)
  105. "Search for a contact maching NAME-MATCH and TAGS-MATCH.
  106. If both match values are nil, return all contacts."
  107. (let ((tags-matcher
  108. (if tags-match
  109. (cdr (org-make-tags-matcher tags-match))
  110. t))
  111. (name-matcher
  112. (if name-match
  113. '(org-string-match-p name-match (org-get-heading t))
  114. t))
  115. (contacts-matcher
  116. (cdr (org-make-tags-matcher org-contacts-matcher)))
  117. markers result)
  118. (dolist (file (org-contacts-files))
  119. (org-check-agenda-file file)
  120. (with-current-buffer (org-get-agenda-file-buffer file)
  121. (unless (org-mode-p)
  122. (error "File %s is no in `org-mode'" file))
  123. (org-scan-tags
  124. '(add-to-list 'markers (set-marker (make-marker) (point)))
  125. `(and ,contacts-matcher ,tags-matcher ,name-matcher))))
  126. (dolist (marker markers result)
  127. (org-with-point-at marker
  128. (add-to-list 'result
  129. (list (org-get-heading t) marker (org-entry-properties marker 'all)))))))
  130. (when (not (fboundp 'completion-table-case-fold))
  131. ;; That function is new in Emacs 24...
  132. (defun completion-table-case-fold (table string pred action)
  133. (let ((completion-ignore-case t))
  134. (complete-with-action action table string pred))))
  135. (defun org-contacts-complete-name (&optional start)
  136. "Complete text at START with a user name and email."
  137. (let* ((end (point))
  138. (start (or start
  139. (save-excursion
  140. (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
  141. (goto-char (match-end 0))
  142. (point))))
  143. (orig (buffer-substring start end))
  144. (completion-ignore-case org-contacts-completion-ignore-case)
  145. (group-completion-p (org-string-match-p (concat "^" org-contacts-group-prefix) orig))
  146. (completion-list
  147. (if group-completion-p
  148. (mapcar (lambda (group) (propertize (concat org-contacts-group-prefix group) 'org-contacts-group group))
  149. (org-uniquify
  150. (loop for contact in (org-contacts-filter)
  151. with group-list
  152. nconc (org-split-string
  153. (or (cdr (assoc-string "ALLTAGS" (caddr contact))) "") ":"))))
  154. (loop for contact in (org-contacts-filter)
  155. ;; The contact name is always the car of the assoc-list
  156. ;; returned by `org-contacts-filter'.
  157. for contact-name = (car contact)
  158. ;; Build the list of the user email addresses.
  159. for email-list = (split-string (or
  160. (cdr (assoc-string org-contacts-email-property (caddr contact)))
  161. ""))
  162. ;; If the user has email addresses…
  163. if email-list
  164. ;; … append a list of USER <EMAIL>.
  165. nconc (loop for email in email-list
  166. collect (org-contacts-format-email contact-name email)))))
  167. (completion-list (all-completions orig completion-list)))
  168. ;; If we are completing a group, and that's the only group, just return
  169. ;; the real result.
  170. (when (and group-completion-p
  171. (= (length completion-list) 1))
  172. (setq completion-list
  173. (list (concat (car completion-list) ";: "
  174. (mapconcat 'identity
  175. (loop for contact in (org-contacts-filter
  176. nil
  177. (get-text-property 0 'org-contacts-group (car completion-list)))
  178. ;; The contact name is always the car of the assoc-list
  179. ;; returned by `org-contacts-filter'.
  180. for contact-name = (car contact)
  181. ;; Grab the first email of the contact
  182. for email = (car (split-string (or
  183. (cdr (assoc-string org-contacts-email-property (caddr contact)))
  184. "")))
  185. ;; If the user has an email address, append USER <EMAIL>.
  186. if email collect (org-contacts-format-email contact-name email))
  187. ", ")))))
  188. (list start end (if org-contacts-completion-ignore-case
  189. (apply-partially #'completion-table-case-fold completion-list)
  190. completion-list))))
  191. (defun org-contacts-message-complete-function ()
  192. "Function used in `completion-at-point-functions' in `message-mode'."
  193. (let ((mail-abbrev-mode-regexp
  194. "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
  195. (when (mail-abbrev-in-expansion-header-p)
  196. (org-contacts-complete-name))))
  197. (add-hook 'message-mode-hook
  198. (lambda ()
  199. (add-to-list 'completion-at-point-functions
  200. 'org-contacts-message-complete-function)))
  201. (defun org-contacts-gnus-get-name-email ()
  202. "Get name and email address from Gnus message."
  203. (gnus-with-article-headers
  204. (mail-extract-address-components
  205. (or (mail-fetch-field "From") ""))))
  206. (defun org-contacts-gnus-article-from-get-marker ()
  207. "Return a marker for a contact based on From."
  208. (let* ((address (org-contacts-gnus-get-name-email))
  209. (name (car address))
  210. (email (cadr address)))
  211. (cadar (or (org-contacts-filter
  212. nil
  213. (concat org-contacts-email-property "={\\b" (regexp-quote email) "\\b}"))
  214. (when name
  215. (org-contacts-filter
  216. (concat "^" name "$")))))))
  217. (defun org-contacts-gnus-article-from-goto ()
  218. "Go to contact in the From address of current Gnus message."
  219. (interactive)
  220. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  221. (when marker
  222. (switch-to-buffer-other-window (marker-buffer marker))
  223. (goto-char marker)
  224. (when (org-mode-p)
  225. (org-show-context 'agenda)
  226. (save-excursion
  227. (and (outline-next-heading)
  228. ;; show the next heading
  229. (org-flag-heading nil)))))))
  230. (defun org-contacts-anniversaries (&optional field format)
  231. "Compute FIELD anniversary for each contact, returning FORMAT.
  232. Default FIELD value is \"BIRTHDAY\".
  233. Format is a string matching the following format specification:
  234. %h - Heading name
  235. %l - Link to the heading
  236. %y - Number of year
  237. %Y - Number of year (ordinal)"
  238. (let ((calendar-date-style 'american)
  239. (entry ""))
  240. (unless format (setq format org-contacts-birthday-format))
  241. (loop for contact in (org-contacts-filter)
  242. for anniv = (let ((anniv (cdr (assoc-string
  243. (or field org-contacts-birthday-property)
  244. (caddr contact)))))
  245. (when anniv
  246. (calendar-gregorian-from-absolute
  247. (org-time-string-to-absolute anniv))))
  248. ;; Use `diary-anniversary' to compute anniversary.
  249. if (and anniv (apply 'diary-anniversary anniv))
  250. collect (format-spec format
  251. `((?l . ,(org-with-point-at (cadr contact) (org-store-link nil)))
  252. (?h . ,(car contact))
  253. (?y . ,(- (calendar-extract-year date)
  254. (calendar-extract-year anniv)))
  255. (?Y . ,(let ((years (- (calendar-extract-year date)
  256. (calendar-extract-year anniv))))
  257. (format "%d%s" years (diary-ordinal-suffix years)))))))))
  258. (defun org-completing-read-date (prompt collection
  259. &optional predicate require-match initial-input
  260. hist def inherit-input-method)
  261. "Like `completing-read' but reads a date.
  262. Only PROMPT and DEF are really used."
  263. (org-read-date nil nil nil prompt nil def))
  264. (add-to-list 'org-property-set-functions-alist
  265. `(,org-contacts-birthday-property . org-completing-read-date))
  266. (defun org-contacts-template-name (&optional return-value)
  267. "Try to return the contact name for a template.
  268. If not found return RETURN-VALUE or something that would ask the user."
  269. (or (car (org-contacts-gnus-get-name-email))
  270. return-value
  271. "%^{Name}"))
  272. (defun org-contacts-template-email (&optional return-value)
  273. "Try to return the contact email for a template.
  274. If not found return RETURN-VALUE or something that would ask the user."
  275. (or (cadr (org-contacts-gnus-get-name-email))
  276. return-value
  277. (concat "%^{" org-contacts-email-property "}p")))
  278. (defun org-contacts-gnus-store-last-mail ()
  279. "Store a link between mails and contacts.
  280. This function should be called from `gnus-article-prepare-hook'."
  281. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  282. (when marker
  283. (with-current-buffer (marker-buffer marker)
  284. (save-excursion
  285. (goto-char marker)
  286. (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
  287. org-email-link-description-format))
  288. (link (gnus-with-article-buffer (org-store-link nil))))
  289. (org-set-property org-contacts-last-read-mail-property link)))))))
  290. (defun org-contacts-icon-as-string ()
  291. (let ((image (org-contacts-get-icon)))
  292. (concat
  293. (propertize "-" 'display
  294. (append
  295. (if image
  296. image
  297. `'(space :width (,org-contacts-icon-size)))
  298. '(:ascent center)))
  299. " ")))
  300. ;;;###autoload
  301. (defun org-contacts (name)
  302. "Create agenda view for contacts matching NAME."
  303. (interactive (list (read-string "Name: ")))
  304. (let ((org-agenda-files (org-contacts-files))
  305. (org-agenda-skip-function
  306. (lambda () (org-agenda-skip-if nil `(notregexp ,name))))
  307. (org-agenda-format (propertize
  308. "%(org-contacts-icon-as-string)% p% s%(org-contacts-irc-number-of-unread-messages)%+T"
  309. 'keymap org-contacts-keymap))
  310. (org-agenda-overriding-header
  311. (or org-agenda-overriding-header
  312. (concat "List of contacts matching `" name "':"))))
  313. (setq org-agenda-skip-regexp name)
  314. (org-tags-view nil org-contacts-matcher)
  315. (with-current-buffer org-agenda-buffer-name
  316. (setq org-agenda-redo-command
  317. (list 'org-contacts name)))))
  318. (defun org-contacts-completing-read (prompt
  319. &optional predicate
  320. initial-input hist def inherit-input-method)
  321. "Call `completing-read' with contacts name as collection."
  322. (org-completing-read
  323. prompt (org-contacts-filter) predicate t initial-input hist def inherit-input-method))
  324. (defun org-contacts-format-email (name email)
  325. "Format a mail address."
  326. (unless email
  327. (error "`email' cannot be nul"))
  328. (if name
  329. (concat name " <" email ">")
  330. email))
  331. (defun org-contacts-check-mail-address (mail)
  332. "Add MAIL address to contact at point if it does not have it."
  333. (let ((mails (org-entry-get (point) org-contacts-email-property)))
  334. (unless (member mail (split-string mails))
  335. (when (yes-or-no-p
  336. (format "Do you want to this address to %s?" (org-get-heading t)))
  337. (org-set-property org-contacts-email-property (concat mails " " mail))))))
  338. (defun org-contacts-gnus-check-mail-address ()
  339. "Check that contact has the current address recorded.
  340. This function should be called from `gnus-article-prepare-hook'."
  341. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  342. (when marker
  343. (org-with-point-at marker
  344. (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
  345. (defun org-contacts-gnus-insinuate ()
  346. "Add some hooks for Gnus user.
  347. This adds `org-contacts-gnus-check-mail-address' and
  348. `org-contacts-gnus-store-last-mail' to
  349. `gnus-article-prepare-hook'. It also adds a binding on `;' in
  350. `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
  351. (require 'gnus)
  352. (require 'gnus-art)
  353. (define-key gnus-summary-mode-map ";" 'org-contacts-gnus-article-from-goto)
  354. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-check-mail-address)
  355. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-store-last-mail))
  356. (defun wl-get-from-header-content ()
  357. (save-excursion
  358. (set-buffer (org-capture-get :original-buffer))
  359. (cond
  360. ((eq major-mode 'wl-summary-mode) (when wl-summary-buffer-elmo-folder
  361. (elmo-message-field
  362. wl-summary-buffer-elmo-folder
  363. (wl-summary-message-number)
  364. 'from)))
  365. ((eq major-mode 'mime-view-mode) (std11-narrow-to-header)
  366. (prog1
  367. (std11-fetch-field "From")
  368. (widen))))))
  369. (defun org-contacts-template-wl-name (&optional return-value)
  370. (let ((from (wl-get-from-header-content)))
  371. (or (and from (wl-address-header-extract-realname from))
  372. return-value
  373. "%^{Name}")))
  374. (defun org-contacts-template-wl-email (&optional return-value)
  375. (let ((from (wl-get-from-header-content)))
  376. (or (and from (wl-address-header-extract-address from))
  377. return-value
  378. (concat "%^{" org-contacts-email-property "}p"))))
  379. (defun org-contacts-view-send-email (&optional ask)
  380. "Send email to the contact at point.
  381. If ASK is set, ask for the email address even if there's only one address."
  382. (interactive "P")
  383. (let ((marker (org-get-at-bol 'org-hd-marker)))
  384. (org-with-point-at marker
  385. (let ((emails (org-entry-get (point) org-contacts-email-property)))
  386. (if emails
  387. (let ((email-list (split-string emails)))
  388. (if (and (= (length email-list) 1) (not ask))
  389. (compose-mail (org-contacts-format-email
  390. (org-get-heading t) emails))
  391. (let ((email (completing-read "Send mail to which address: " email-list)))
  392. (org-contacts-check-mail-address email)
  393. (compose-mail (org-contacts-format-email (org-get-heading t) email)))))
  394. (error (format "This contact has no mail address set (no %s property)."
  395. org-contacts-email-property)))))))
  396. (defun org-contacts-get-icon (&optional pom)
  397. "Get icon for contact at POM."
  398. (setq pom (or pom (point)))
  399. (catch 'icon
  400. ;; Use `org-contacts-icon-property'
  401. (let ((image-data (org-entry-get pom org-contacts-icon-property)))
  402. (when image-data
  403. (throw 'icon
  404. (if (fboundp 'gnus-rescale-image)
  405. (gnus-rescale-image (create-image image-data)
  406. (cons org-contacts-icon-size org-contacts-icon-size))
  407. (create-image image-data)))))
  408. ;; Next, try Gravatar
  409. (when org-contacts-icon-use-gravatar
  410. (let* ((gravatar-size org-contacts-icon-size)
  411. (email-list (org-entry-get pom org-contacts-email-property))
  412. (gravatar
  413. (when email-list
  414. (loop for email in (split-string email-list)
  415. for gravatar = (gravatar-retrieve-synchronously email)
  416. if (and gravatar
  417. (not (eq gravatar 'error)))
  418. return gravatar))))
  419. (when gravatar (throw 'icon gravatar))))))
  420. (defun org-contacts-irc-buffer (&optional pom)
  421. "Get the IRC buffer associated with the entry at POM."
  422. (setq pom (or pom (point)))
  423. (let ((nick (org-entry-get pom org-contacts-nickname-property)))
  424. (when nick
  425. (let ((buffer (get-buffer nick)))
  426. (when buffer
  427. (with-current-buffer buffer
  428. (when (eq major-mode 'erc-mode)
  429. buffer)))))))
  430. (defun org-contacts-irc-number-of-unread-messages (&optional pom)
  431. "Return the number of unread messages for contact at POM."
  432. (when (boundp 'erc-modified-channels-alist)
  433. (let ((number (cadr (assoc (org-contacts-irc-buffer pom) erc-modified-channels-alist))))
  434. (if number
  435. (format (concat "%3d unread message" (if (> number 1) "s" " ") " ") number)
  436. (make-string 21 ? )))))
  437. (defun org-contacts-view-switch-to-irc-buffer ()
  438. "Switch to the IRC buffer of the current contact if it has one."
  439. (interactive)
  440. (let ((marker (org-get-at-bol 'org-hd-marker)))
  441. (org-with-point-at marker
  442. (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
  443. (defun org-contacts-completing-read-nickname (prompt collection
  444. &optional predicate require-match initial-input
  445. hist def inherit-input-method)
  446. "Like `completing-read' but reads a nickname."
  447. (org-completing-read prompt (append collection (erc-nicknames-list)) predicate require-match
  448. initial-input hist def inherit-input-method))
  449. (defun erc-nicknames-list ()
  450. "Return all nicknames of all ERC buffers."
  451. (loop for buffer in (erc-buffer-list)
  452. nconc (with-current-buffer buffer
  453. (loop for user-entry in (mapcar 'car (erc-get-channel-user-list))
  454. collect (elt user-entry 1)))))
  455. (add-to-list 'org-property-set-functions-alist
  456. `(,org-contacts-nickname-property . org-contacts-completing-read-nickname))
  457. (provide 'org-contacts)