org-contacts.el 33 KB

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