org-contacts.el 40 KB

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