org-contacts.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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 '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. "Like `try-completion' but:
  188. - works only with list and alist;
  189. - looks at all 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. "Like `all-completions' but:
  260. - works only with list and alist;
  261. - looks at all 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. "Makes a collection function from COLLECTION which will match
  280. on prefixes."
  281. (lexical-let ((collection collection))
  282. (lambda (string predicate flag)
  283. (cond ((eq flag nil)
  284. (org-contacts-try-completion-prefix string collection predicate))
  285. ((eq flag t)
  286. ;; `org-contacts-all-completions-prefix' has already been
  287. ;; used to compute `all-completions'.
  288. collection)
  289. ((eq flag 'lambda)
  290. (org-contacts-test-completion-prefix string collection predicate))
  291. ((and (listp flag) (eq (car flag) 'boundaries))
  292. (destructuring-bind (to-ignore &rest suffix)
  293. flag
  294. (org-contacts-boundaries-prefix string collection predicate suffix)))
  295. ((eq flag 'metadata)
  296. (org-contacts-metadata-prefix string collection predicate))
  297. (t nil ; operation unsupported
  298. )))))
  299. (defun org-contacts-display-sort-function (completions)
  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. (let ((image (org-contacts-get-icon)))
  503. (concat
  504. (propertize "-" 'display
  505. (append
  506. (if image
  507. image
  508. `'(space :width (,org-contacts-icon-size)))
  509. '(:ascent center)))
  510. " ")))
  511. ;;;###autoload
  512. (defun org-contacts (name)
  513. "Create agenda view for contacts matching NAME."
  514. (interactive (list (read-string "Name: ")))
  515. (let ((org-agenda-files (org-contacts-files))
  516. (org-agenda-skip-function
  517. (lambda () (org-agenda-skip-if nil `(notregexp ,name))))
  518. (org-agenda-prefix-format (propertize
  519. "%(org-contacts-icon-as-string)% s%(org-contacts-irc-number-of-unread-messages) "
  520. 'keymap org-contacts-keymap))
  521. (org-agenda-overriding-header
  522. (or org-agenda-overriding-header
  523. (concat "List of contacts matching `" name "':"))))
  524. (setq org-agenda-skip-regexp name)
  525. (org-tags-view nil org-contacts-matcher)
  526. (with-current-buffer org-agenda-buffer-name
  527. (setq org-agenda-redo-command
  528. (list 'org-contacts name)))))
  529. (defun org-contacts-completing-read (prompt
  530. &optional predicate
  531. initial-input hist def inherit-input-method)
  532. "Call `completing-read' with contacts name as collection."
  533. (org-completing-read
  534. prompt (org-contacts-filter) predicate t initial-input hist def inherit-input-method))
  535. (defun org-contacts-format-name (name)
  536. "Trim any local formatting to get a bare name."
  537. ;; Remove radio targets characters
  538. (replace-regexp-in-string org-radio-target-regexp "\\1" name))
  539. (defun org-contacts-format-email (name email)
  540. "Format a mail address."
  541. (unless email
  542. (error "`email' cannot be nul"))
  543. (if name
  544. (concat (org-contacts-format-name name) " <" email ">")
  545. email))
  546. (defun org-contacts-check-mail-address (mail)
  547. "Add MAIL address to contact at point if it does not have it."
  548. (let ((mails (org-entry-get (point) org-contacts-email-property)))
  549. (unless (member mail (split-string mails))
  550. (when (yes-or-no-p
  551. (format "Do you want to add this address to %s?" (org-get-heading t)))
  552. (org-set-property org-contacts-email-property (concat mails " " mail))))))
  553. (defun org-contacts-gnus-check-mail-address ()
  554. "Check that contact has the current address recorded.
  555. This function should be called from `gnus-article-prepare-hook'."
  556. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  557. (when marker
  558. (org-with-point-at marker
  559. (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
  560. (defun org-contacts-gnus-insinuate ()
  561. "Add some hooks for Gnus user.
  562. This adds `org-contacts-gnus-check-mail-address' and
  563. `org-contacts-gnus-store-last-mail' to
  564. `gnus-article-prepare-hook'. It also adds a binding on `;' in
  565. `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
  566. (require 'gnus)
  567. (require 'gnus-art)
  568. (define-key gnus-summary-mode-map ";" 'org-contacts-gnus-article-from-goto)
  569. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-check-mail-address)
  570. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-store-last-mail))
  571. (when (and org-contacts-enable-completion
  572. (boundp 'completion-at-point-functions))
  573. (add-hook 'message-mode-hook
  574. (lambda ()
  575. (add-to-list 'completion-at-point-functions
  576. 'org-contacts-message-complete-function))))
  577. (defun org-contacts-wl-get-from-header-content ()
  578. "Retrieve the content of the `From' header of an email.
  579. Works from wl-summary-mode and mime-view-mode - that is while viewing email.
  580. Depends on Wanderlust been loaded."
  581. (with-current-buffer (org-capture-get :original-buffer)
  582. (cond
  583. ((eq major-mode 'wl-summary-mode) (when (and (boundp 'wl-summary-buffer-elmo-folder)
  584. wl-summary-buffer-elmo-folder)
  585. (elmo-message-field
  586. wl-summary-buffer-elmo-folder
  587. (wl-summary-message-number)
  588. 'from)))
  589. ((eq major-mode 'mime-view-mode) (std11-narrow-to-header)
  590. (prog1
  591. (std11-fetch-field "From")
  592. (widen))))))
  593. (defun org-contacts-wl-get-name-email ()
  594. "Get name and email address from Wanderlust email.
  595. See `org-contacts-wl-get-from-header-content' for limitations."
  596. (let ((from (org-contacts-wl-get-from-header-content)))
  597. (when from
  598. (list (wl-address-header-extract-realname from)
  599. (wl-address-header-extract-address from)))))
  600. (defun org-contacts-template-wl-name (&optional return-value)
  601. "Try to return the contact name for a template from wl.
  602. If not found, return RETURN-VALUE or something that would ask the
  603. user."
  604. (or (car (org-contacts-wl-get-name-email))
  605. return-value
  606. "%^{Name}"))
  607. (defun org-contacts-template-wl-email (&optional return-value)
  608. "Try to return the contact email for a template from Wanderlust.
  609. If not found return RETURN-VALUE or something that would ask the user."
  610. (or (cadr (org-contacts-wl-get-name-email))
  611. return-value
  612. (concat "%^{" org-contacts-email-property "}p")))
  613. (defun org-contacts-view-send-email (&optional ask)
  614. "Send email to the contact at point.
  615. If ASK is set, ask for the email address even if there's only one
  616. address."
  617. (interactive "P")
  618. (let ((marker (org-get-at-bol 'org-hd-marker)))
  619. (org-with-point-at marker
  620. (let ((emails (org-entry-get (point) org-contacts-email-property)))
  621. (if emails
  622. (let ((email-list (split-string emails)))
  623. (if (and (= (length email-list) 1) (not ask))
  624. (compose-mail (org-contacts-format-email
  625. (org-get-heading t) emails))
  626. (let ((email (completing-read "Send mail to which address: " email-list)))
  627. (org-contacts-check-mail-address email)
  628. (compose-mail (org-contacts-format-email (org-get-heading t) email)))))
  629. (error (format "This contact has no mail address set (no %s property)."
  630. org-contacts-email-property)))))))
  631. (defun org-contacts-get-icon (&optional pom)
  632. "Get icon for contact at POM."
  633. (setq pom (or pom (point)))
  634. (catch 'icon
  635. ;; Use `org-contacts-icon-property'
  636. (let ((image-data (org-entry-get pom org-contacts-icon-property)))
  637. (when image-data
  638. (throw 'icon
  639. (if (fboundp 'gnus-rescale-image)
  640. (gnus-rescale-image (create-image image-data)
  641. (cons org-contacts-icon-size org-contacts-icon-size))
  642. (create-image image-data)))))
  643. ;; Next, try Gravatar
  644. (when org-contacts-icon-use-gravatar
  645. (let* ((gravatar-size org-contacts-icon-size)
  646. (email-list (org-entry-get pom org-contacts-email-property))
  647. (gravatar
  648. (when email-list
  649. (loop for email in (split-string email-list)
  650. for gravatar = (gravatar-retrieve-synchronously email)
  651. if (and gravatar
  652. (not (eq gravatar 'error)))
  653. return gravatar))))
  654. (when gravatar (throw 'icon gravatar))))))
  655. (defun org-contacts-irc-buffer (&optional pom)
  656. "Get the IRC buffer associated with the entry at POM."
  657. (setq pom (or pom (point)))
  658. (let ((nick (org-entry-get pom org-contacts-nickname-property)))
  659. (when nick
  660. (let ((buffer (get-buffer nick)))
  661. (when buffer
  662. (with-current-buffer buffer
  663. (when (eq major-mode 'erc-mode)
  664. buffer)))))))
  665. (defun org-contacts-irc-number-of-unread-messages (&optional pom)
  666. "Return the number of unread messages for contact at POM."
  667. (when (boundp 'erc-modified-channels-alist)
  668. (let ((number (cadr (assoc (org-contacts-irc-buffer pom) erc-modified-channels-alist))))
  669. (if number
  670. (format (concat "%3d unread message" (if (> number 1) "s" " ") " ") number)
  671. (make-string 21 ? )))))
  672. (defun org-contacts-view-switch-to-irc-buffer ()
  673. "Switch to the IRC buffer of the current contact if it has one."
  674. (interactive)
  675. (let ((marker (org-get-at-bol 'org-hd-marker)))
  676. (org-with-point-at marker
  677. (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
  678. (defun org-contacts-completing-read-nickname (prompt collection
  679. &optional predicate require-match initial-input
  680. hist def inherit-input-method)
  681. "Like `completing-read' but reads a nickname."
  682. (org-completing-read prompt (append collection (erc-nicknames-list)) predicate require-match
  683. initial-input hist def inherit-input-method))
  684. (defun erc-nicknames-list ()
  685. "Return all nicknames of all ERC buffers."
  686. (if (fboundp 'erc-buffer-list)
  687. (loop for buffer in (erc-buffer-list)
  688. nconc (with-current-buffer buffer
  689. (loop for user-entry in (mapcar 'car (erc-get-channel-user-list))
  690. collect (elt user-entry 1))))))
  691. (add-to-list 'org-property-set-functions-alist
  692. `(,org-contacts-nickname-property . org-contacts-completing-read-nickname))
  693. (defun org-contacts-vcard-escape (str)
  694. "Escape ; , and \n in STR for the VCard format."
  695. ;; Thanks to this library for the regexp:
  696. ;; http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el
  697. (when str
  698. (replace-regexp-in-string
  699. "\n" "\\\\n"
  700. (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str))))
  701. (defun org-contacts-vcard-encode-name (name)
  702. "Try to encode NAME as VCard's N property.
  703. The N property expects
  704. FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
  705. Org-contacts does not specify how to encode the name. So we try
  706. to do our best."
  707. (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name) ";;;"))
  708. (defun org-contacts-vcard-format (contact)
  709. "Formats CONTACT in VCard 3.0 format."
  710. (let* ((properties (caddr contact))
  711. (name (org-contacts-vcard-escape (car contact)))
  712. (n (org-contacts-vcard-encode-name name))
  713. (email (org-contacts-vcard-escape (cdr (assoc-string org-contacts-email-property properties))))
  714. (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties))))
  715. (addr (cdr (assoc-string org-contacts-address-property properties)))
  716. (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties))))
  717. (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name)))
  718. (concat head
  719. (when email (format "EMAIL:%s\n" email))
  720. (when addr
  721. (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr)))
  722. (when bday
  723. (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday))))
  724. (format "BDAY:%04d-%02d-%02d\n"
  725. (calendar-extract-year cal-bday)
  726. (calendar-extract-month cal-bday)
  727. (calendar-extract-day cal-bday))))
  728. (when nick (format "NICKNAME:%s\n" nick))
  729. "END:VCARD\n\n")))
  730. (defun org-contacts-export-as-vcard (&optional name file to-buffer)
  731. "Export all contacts matching NAME as VCard 3.0.
  732. If TO-BUFFER is nil, the content is written to FILE or
  733. `org-contacts-vcard-file'. If TO-BUFFER is non-nil, the buffer
  734. is created and the VCard is written into that buffer."
  735. (interactive) ; TODO ask for name?
  736. (let* ((filename (or file org-contacts-vcard-file))
  737. (buffer (if to-buffer
  738. (get-buffer-create to-buffer)
  739. (find-file-noselect filename))))
  740. (message "Exporting...")
  741. (set-buffer buffer)
  742. (let ((inhibit-read-only t)) (erase-buffer))
  743. (fundamental-mode)
  744. (org-install-letbind)
  745. (when (fboundp 'set-buffer-file-coding-system)
  746. (set-buffer-file-coding-system coding-system-for-write))
  747. (loop for contact in (org-contacts-filter name)
  748. do (insert (org-contacts-vcard-format contact)))
  749. (if to-buffer
  750. (current-buffer)
  751. (progn (save-buffer) (kill-buffer)))))
  752. (defun org-contacts-show-map (&optional name)
  753. "Show contacts on a map.
  754. Requires google-maps-el."
  755. (interactive)
  756. (unless (fboundp 'google-maps-static-show)
  757. (error "`org-contacts-show-map' requires `google-maps-el'"))
  758. (google-maps-static-show
  759. :markers
  760. (loop
  761. for contact in (org-contacts-filter name)
  762. for addr = (cdr (assoc-string org-contacts-address-property (caddr contact)))
  763. if addr
  764. collect (cons (list addr) (list :label (string-to-char (car contact)))))))
  765. (provide 'org-contacts)