org-contacts.el 44 KB

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