org-contacts.el 44 KB

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