org-contacts.el 39 KB

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