org-contacts.el 31 KB

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