org-contacts.el 33 KB

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