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