org-contacts.el 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. (require 'ol)
  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
  135. (mapcar (lambda (x) (concat x "<>\"\""))
  136. (list org-contacts-email-property
  137. org-contacts-alias-property
  138. org-contacts-tel-property
  139. org-contacts-address-property
  140. org-contacts-birthday-property))
  141. "|")
  142. "Matching rule for finding heading that are contacts.
  143. This can be a tag name, or a property check."
  144. :type 'string
  145. :group 'org-contacts)
  146. (defcustom org-contacts-email-link-description-format "%s (%d)"
  147. "Format used to store links to email.
  148. This overrides `org-email-link-description-format' if set."
  149. :group 'org-contacts
  150. :type 'string)
  151. (defcustom org-contacts-vcard-file "contacts.vcf"
  152. "Default file for vcard export."
  153. :group 'org-contacts
  154. :type 'file)
  155. (defcustom org-contacts-enable-completion t
  156. "Enable or not the completion in `message-mode' with `org-contacts'."
  157. :group 'org-contacts
  158. :type 'boolean)
  159. (defcustom org-contacts-complete-functions
  160. '(org-contacts-complete-group org-contacts-complete-tags-props org-contacts-complete-name)
  161. "List of functions used to complete contacts in `message-mode'."
  162. :group 'org-contacts
  163. :type 'hook)
  164. ;; Decalre external functions and variables
  165. (declare-function org-reverse-string "org")
  166. (declare-function diary-ordinal-suffix "ext:diary-lib")
  167. (declare-function wl-summary-message-number "ext:wl-summary")
  168. (declare-function wl-address-header-extract-address "ext:wl-address")
  169. (declare-function wl-address-header-extract-realname "ext:wl-address")
  170. (declare-function erc-buffer-list "ext:erc")
  171. (declare-function erc-get-channel-user-list "ext:erc")
  172. (declare-function google-maps-static-show "ext:google-maps-static")
  173. (declare-function elmo-message-field "ext:elmo-pipe")
  174. (declare-function std11-narrow-to-header "ext:std11")
  175. (declare-function std11-fetch-field "ext:std11")
  176. (defconst org-contacts-property-values-separators "[,; \f\t\n\r\v]+"
  177. "The default value of separators for `org-contacts-split-property'.
  178. A regexp matching strings of whitespace, `,' and `;'.")
  179. (defvar org-contacts-keymap
  180. (let ((map (make-sparse-keymap)))
  181. (define-key map "M" 'org-contacts-view-send-email)
  182. (define-key map "i" 'org-contacts-view-switch-to-irc-buffer)
  183. map)
  184. "The keymap used in `org-contacts' result list.")
  185. (defvar org-contacts-db nil
  186. "Org Contacts database.")
  187. (defvar org-contacts-last-update nil
  188. "Last time the Org Contacts database has been updated.")
  189. (defun org-contacts-files ()
  190. "Return list of Org files to use for contact management."
  191. (or org-contacts-files (org-agenda-files t 'ifmode)))
  192. (defun org-contacts-db-need-update-p ()
  193. "Determine whether `org-contacts-db' needs to be refreshed."
  194. (or (null org-contacts-last-update)
  195. (cl-find-if (lambda (file)
  196. (or (time-less-p org-contacts-last-update
  197. (elt (file-attributes file) 5))))
  198. (org-contacts-files))
  199. (org-contacts-db-has-dead-markers-p org-contacts-db)))
  200. (defun org-contacts-db-has-dead-markers-p (org-contacts-db)
  201. "Returns t if at least one dead marker is found in
  202. ORG-CONTACTS-DB. A dead marker in this case is a marker pointing
  203. to dead or no buffer."
  204. ;; Scan contacts list looking for dead markers, and return t at first found.
  205. (catch 'dead-marker-found
  206. (while org-contacts-db
  207. (unless (marker-buffer (nth 1 (car org-contacts-db)))
  208. (throw 'dead-marker-found t))
  209. (setq org-contacts-db (cdr org-contacts-db)))
  210. nil))
  211. (defun org-contacts-db ()
  212. "Return the latest Org Contacts Database."
  213. (let* ((org--matcher-tags-todo-only nil)
  214. (contacts-matcher (cdr (org-make-tags-matcher org-contacts-matcher)))
  215. result)
  216. (when (org-contacts-db-need-update-p)
  217. (let ((progress-reporter
  218. (make-progress-reporter "Updating Org Contacts Database..." 0 (length org-contacts-files)))
  219. (i 0))
  220. (dolist (file (org-contacts-files))
  221. (if (catch 'nextfile
  222. ;; if file doesn't exist and the user agrees to removing it
  223. ;; from org-agendas-list, 'nextfile is thrown. Catch it here
  224. ;; and skip processing the file.
  225. ;;
  226. ;; TODO: suppose that the user has set an org-contacts-files
  227. ;; list that contains an element that doesn't exist in the
  228. ;; file system: in that case, the org-agenda-files list could
  229. ;; be updated (and saved to the customizations of the user) if
  230. ;; it contained the same file even though the org-agenda-files
  231. ;; list wasn't actually used. I don't think it is normal that
  232. ;; org-contacts updates org-agenda-files in this case, but
  233. ;; short of duplicating org-check-agenda-files and
  234. ;; org-remove-files, I don't know how to avoid it.
  235. ;;
  236. ;; A side effect of the TODO is that the faulty
  237. ;; org-contacts-files list never gets updated and thus the
  238. ;; user is always queried about the missing files when
  239. ;; org-contacts-db-need-update-p returns true.
  240. (org-check-agenda-file file))
  241. (message "Skipped %s removed from org-agenda-files list."
  242. (abbreviate-file-name file))
  243. (with-current-buffer (org-get-agenda-file-buffer file)
  244. (unless (eq major-mode 'org-mode)
  245. (error "File %s is not in `org-mode'" file))
  246. (setf result
  247. (append result
  248. (org-scan-tags 'org-contacts-at-point
  249. contacts-matcher
  250. org--matcher-tags-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. (cl-loop for contact in (org-contacts-db)
  273. if (or
  274. (and name-match
  275. (string-match-p name-match
  276. (first contact)))
  277. (and prop-match
  278. (cl-find-if (lambda (prop)
  279. (and (string= (car prop-match) (car prop))
  280. (string-match-p (cdr prop-match) (cdr prop))))
  281. (caddr contact)))
  282. (and tags-match
  283. (cl-find-if (lambda (tag)
  284. (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. (cl-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. (cl-return t)
  311. (cl-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 (cl-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. (cl-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. (cl-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. (cl-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 (cl-return string)))
  424. completions))
  425. (defun org-contacts-test-completion-prefix (string collection predicate)
  426. (cl-find-if (lambda (el)
  427. (and (or (null predicate) (funcall predicate el))
  428. (string= string el)))
  429. collection))
  430. (defun org-contacts-boundaries-prefix (string collection predicate suffix)
  431. (list* 'boundaries (completion-boundaries string collection predicate suffix)))
  432. (defun org-contacts-metadata-prefix (string collection predicate)
  433. '(metadata .
  434. ((cycle-sort-function . org-contacts-display-sort-function)
  435. (display-sort-function . org-contacts-display-sort-function))))
  436. (defun org-contacts-complete-group (start end string)
  437. "Complete text at START from a group.
  438. A group FOO is composed of contacts with the tag FOO."
  439. (let* ((completion-ignore-case org-contacts-completion-ignore-case)
  440. (group-completion-p (string-match-p
  441. (concat "^" org-contacts-group-prefix) string)))
  442. (when group-completion-p
  443. (let ((completion-list
  444. (all-completions
  445. string
  446. (mapcar (lambda (group)
  447. (propertize (concat org-contacts-group-prefix group)
  448. 'org-contacts-group group))
  449. (org-uniquify
  450. (cl-loop for contact in (org-contacts-filter)
  451. nconc (org-split-string
  452. (or (cdr (assoc-string "ALLTAGS" (caddr contact))) "") ":")))))))
  453. (list start end
  454. (if (= (length completion-list) 1)
  455. ;; We've found the correct group, returns the address
  456. (lexical-let ((tag (get-text-property 0 'org-contacts-group
  457. (car completion-list))))
  458. (lambda (string pred &optional to-ignore)
  459. (mapconcat 'identity
  460. (cl-loop for contact in (org-contacts-filter
  461. nil
  462. tag)
  463. ;; The contact name is always the car of the assoc-list
  464. ;; returned by `org-contacts-filter'.
  465. for contact-name = (car contact)
  466. ;; Grab the first email of the contact
  467. for email = (org-contacts-strip-link
  468. (or (car (org-contacts-split-property
  469. (or
  470. (cdr (assoc-string org-contacts-email-property
  471. (cl-caddr contact)))
  472. ""))) ""))
  473. ;; If the user has an email address, append USER <EMAIL>.
  474. if email collect (org-contacts-format-email contact-name email))
  475. ", ")))
  476. ;; We haven't found the correct group
  477. (completion-table-case-fold completion-list
  478. (not org-contacts-completion-ignore-case))))))))
  479. (defun org-contacts-complete-tags-props (start end string)
  480. "Insert emails that match the tags expression.
  481. For example: FOO-BAR will match entries tagged with FOO but not
  482. with BAR.
  483. See (org) Matching tags and properties for a complete
  484. description."
  485. (let* ((completion-ignore-case org-contacts-completion-ignore-case)
  486. (completion-p (string-match-p
  487. (concat "^" org-contacts-tags-props-prefix) string)))
  488. (when completion-p
  489. (let ((result
  490. (mapconcat
  491. 'identity
  492. (cl-loop for contact in (org-contacts-db)
  493. for contact-name = (car contact)
  494. for email = (org-contacts-strip-link (or (car (org-contacts-split-property
  495. (or
  496. (cdr (assoc-string org-contacts-email-property
  497. (cl-caddr contact)))
  498. ""))) ""))
  499. for tags = (cdr (assoc "TAGS" (nth 2 contact)))
  500. for tags-list = (if tags
  501. (split-string (substring (cdr (assoc "TAGS" (nth 2 contact))) 1 -1) ":")
  502. '())
  503. for marker = (nth 1 contact)
  504. if (with-current-buffer (marker-buffer marker)
  505. (save-excursion
  506. (goto-char marker)
  507. (let (todo-only)
  508. (eval (cdr (org-make-tags-matcher (cl-subseq string 1)))))))
  509. collect (org-contacts-format-email contact-name email))
  510. ",")))
  511. (when (not (string= "" result))
  512. ;; return (start end function)
  513. (lexical-let* ((to-return result))
  514. (list start end
  515. (lambda (string pred &optional to-ignore) to-return))))))))
  516. (defun org-contacts-remove-ignored-property-values (ignore-list list)
  517. "Remove all ignore-list's elements from list and you can use
  518. regular expressions in the ignore list."
  519. (cl-remove-if (lambda (el)
  520. (cl-find-if (lambda (x)
  521. (string-match-p x el))
  522. ignore-list))
  523. list))
  524. (defun org-contacts-complete-name (start end string)
  525. "Complete text at START with a user name and email."
  526. (let* ((completion-ignore-case org-contacts-completion-ignore-case)
  527. (completion-list
  528. (cl-loop for contact in (org-contacts-filter)
  529. ;; The contact name is always the car of the assoc-list
  530. ;; returned by `org-contacts-filter'.
  531. for contact-name = (car contact)
  532. ;; Build the list of the email addresses which has
  533. ;; been expired
  534. for ignore-list = (org-contacts-split-property
  535. (or (cdr (assoc-string org-contacts-ignore-property
  536. (nth 2 contact))) ""))
  537. ;; Build the list of the user email addresses.
  538. for email-list = (org-contacts-remove-ignored-property-values
  539. ignore-list
  540. (org-contacts-split-property
  541. (or (cdr (assoc-string org-contacts-email-property
  542. (nth 2 contact))) "")))
  543. ;; If the user has email addresses…
  544. if email-list
  545. ;; … append a list of USER <EMAIL>.
  546. nconc (cl-loop for email in email-list
  547. collect (org-contacts-format-email contact-name (org-contacts-strip-link email)))))
  548. (completion-list (org-contacts-all-completions-prefix
  549. string
  550. (org-uniquify completion-list))))
  551. (when completion-list
  552. (list start end
  553. (org-contacts-make-collection-prefix completion-list)))))
  554. (defun org-contacts-message-complete-function (&optional start)
  555. "Function used in `completion-at-point-functions' in `message-mode'."
  556. ;; Avoid to complete in `post-command-hook'.
  557. (when completion-in-region-mode
  558. (remove-hook 'post-command-hook #'completion-in-region--postch))
  559. (let ((mail-abbrev-mode-regexp
  560. "^\\(Resent-To\\|To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\|Disposition-Notification-To\\|Return-Receipt-To\\):"))
  561. (when (mail-abbrev-in-expansion-header-p)
  562. (lexical-let*
  563. ((end (point))
  564. (start (or start
  565. (save-excursion
  566. (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
  567. (goto-char (match-end 0))
  568. (point))))
  569. (string (buffer-substring start end)))
  570. (run-hook-with-args-until-success
  571. 'org-contacts-complete-functions start end string)))))
  572. (defun org-contacts-gnus-get-name-email ()
  573. "Get name and email address from Gnus message."
  574. (if (gnus-alive-p)
  575. (gnus-with-article-headers
  576. (mail-extract-address-components
  577. (or (mail-fetch-field "From") "")))))
  578. (defun org-contacts-gnus-article-from-get-marker ()
  579. "Return a marker for a contact based on From."
  580. (let* ((address (org-contacts-gnus-get-name-email))
  581. (name (car address))
  582. (email (cadr address)))
  583. (cl-cadar (or (org-contacts-filter
  584. nil
  585. nil
  586. (cons org-contacts-email-property (concat "\\b" (regexp-quote email) "\\b")))
  587. (when name
  588. (org-contacts-filter
  589. (concat "^" name "$")))))))
  590. (defun org-contacts-gnus-article-from-goto ()
  591. "Go to contact in the From address of current Gnus message."
  592. (interactive)
  593. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  594. (when marker
  595. (switch-to-buffer-other-window (marker-buffer marker))
  596. (goto-char marker)
  597. (when (eq major-mode 'org-mode) (org-show-context 'agenda)))))
  598. (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  599. (defun org-contacts-anniversaries (&optional field format)
  600. "Compute FIELD anniversary for each contact, returning FORMAT.
  601. Default FIELD value is \"BIRTHDAY\".
  602. Format is a string matching the following format specification:
  603. %h - Heading name
  604. %l - Link to the heading
  605. %y - Number of year
  606. %Y - Number of year (ordinal)"
  607. (let ((calendar-date-style 'american)
  608. (entry ""))
  609. (unless format (setq format org-contacts-birthday-format))
  610. (cl-loop for contact in (org-contacts-filter)
  611. for anniv = (let ((anniv (cdr (assoc-string
  612. (or field org-contacts-birthday-property)
  613. (nth 2 contact)))))
  614. (when anniv
  615. (calendar-gregorian-from-absolute
  616. (org-time-string-to-absolute anniv))))
  617. ;; Use `diary-anniversary' to compute anniversary.
  618. if (and anniv (apply 'diary-anniversary anniv))
  619. collect (format-spec format
  620. `((?l . ,(org-with-point-at (cadr contact) (org-store-link nil)))
  621. (?h . ,(car contact))
  622. (?y . ,(- (calendar-extract-year date)
  623. (calendar-extract-year anniv)))
  624. (?Y . ,(let ((years (- (calendar-extract-year date)
  625. (calendar-extract-year anniv))))
  626. (format "%d%s" years (diary-ordinal-suffix years)))))))))
  627. (defun org-completing-read-date (prompt collection
  628. &optional predicate require-match initial-input
  629. hist def inherit-input-method)
  630. "Like `completing-read' but reads a date.
  631. Only PROMPT and DEF are really used."
  632. (org-read-date nil nil nil prompt nil def))
  633. (add-to-list 'org-property-set-functions-alist
  634. `(,org-contacts-birthday-property . org-completing-read-date))
  635. (defun org-contacts-template-name (&optional return-value)
  636. "Try to return the contact name for a template.
  637. If not found return RETURN-VALUE or something that would ask the user."
  638. (or (car (org-contacts-gnus-get-name-email))
  639. return-value
  640. "%^{Name}"))
  641. (defun org-contacts-template-email (&optional return-value)
  642. "Try to return the contact email for a template.
  643. If not found return RETURN-VALUE or something that would ask the user."
  644. (or (cadr (org-contacts-gnus-get-name-email))
  645. return-value
  646. (concat "%^{" org-contacts-email-property "}p")))
  647. (defun org-contacts-gnus-store-last-mail ()
  648. "Store a link between mails and contacts.
  649. This function should be called from `gnus-article-prepare-hook'."
  650. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  651. (when marker
  652. (with-current-buffer (marker-buffer marker)
  653. (save-excursion
  654. (goto-char marker)
  655. (let* ((org-email-link-description-format (or org-contacts-email-link-description-format
  656. org-email-link-description-format))
  657. (link (gnus-with-article-buffer (org-store-link nil))))
  658. (org-set-property org-contacts-last-read-mail-property link)))))))
  659. (defun org-contacts-icon-as-string ()
  660. "Return the contact icon as a string."
  661. (let ((image (org-contacts-get-icon)))
  662. (concat
  663. (propertize "-" 'display
  664. (append
  665. (if image
  666. image
  667. `'(space :width (,org-contacts-icon-size)))
  668. '(:ascent center)))
  669. " ")))
  670. ;;;###autoload
  671. (defun org-contacts (name)
  672. "Create agenda view for contacts matching NAME."
  673. (interactive (list (read-string "Name: ")))
  674. (let ((org-agenda-files (org-contacts-files))
  675. (org-agenda-skip-function
  676. (lambda () (org-agenda-skip-if nil `(notregexp ,name))))
  677. (org-agenda-prefix-format (propertize
  678. "%(org-contacts-icon-as-string)% s%(org-contacts-irc-number-of-unread-messages) "
  679. 'keymap org-contacts-keymap))
  680. (org-agenda-overriding-header
  681. (or org-agenda-overriding-header
  682. (concat "List of contacts matching `" name "':"))))
  683. (setq org-agenda-skip-regexp name)
  684. (org-tags-view nil org-contacts-matcher)
  685. (with-current-buffer org-agenda-buffer-name
  686. (setq org-agenda-redo-command
  687. (list 'org-contacts name)))))
  688. (defun org-contacts-completing-read (prompt
  689. &optional predicate
  690. initial-input hist def inherit-input-method)
  691. "Call `completing-read' with contacts name as collection."
  692. (org-completing-read
  693. prompt (org-contacts-filter) predicate t initial-input hist def inherit-input-method))
  694. (defun org-contacts-format-name (name)
  695. "Trim any local formatting to get a bare NAME."
  696. ;; Remove radio targets characters
  697. (replace-regexp-in-string org-radio-target-regexp "\\1" name))
  698. (defun org-contacts-format-email (name email)
  699. "Format an EMAIL address corresponding to NAME."
  700. (unless email
  701. (error "`email' cannot be nul"))
  702. (if name
  703. (concat (org-contacts-format-name name) " <" email ">")
  704. email))
  705. (defun org-contacts-check-mail-address (mail)
  706. "Add MAIL address to contact at point if it does not have it."
  707. (let ((mails (org-entry-get (point) org-contacts-email-property)))
  708. (unless (member mail (split-string mails))
  709. (when (yes-or-no-p
  710. (format "Do you want to add this address to %s?" (org-get-heading t)))
  711. (org-set-property org-contacts-email-property (concat mails " " mail))))))
  712. (defun org-contacts-gnus-check-mail-address ()
  713. "Check that contact has the current address recorded.
  714. This function should be called from `gnus-article-prepare-hook'."
  715. (let ((marker (org-contacts-gnus-article-from-get-marker)))
  716. (when marker
  717. (org-with-point-at marker
  718. (org-contacts-check-mail-address (cadr (org-contacts-gnus-get-name-email)))))))
  719. (defun org-contacts-gnus-insinuate ()
  720. "Add some hooks for Gnus user.
  721. This adds `org-contacts-gnus-check-mail-address' and
  722. `org-contacts-gnus-store-last-mail' to
  723. `gnus-article-prepare-hook'. It also adds a binding on `;' in
  724. `gnus-summary-mode-map' to `org-contacts-gnus-article-from-goto'"
  725. (require 'gnus)
  726. (require 'gnus-art)
  727. (define-key gnus-summary-mode-map ";" 'org-contacts-gnus-article-from-goto)
  728. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-check-mail-address)
  729. (add-hook 'gnus-article-prepare-hook 'org-contacts-gnus-store-last-mail))
  730. (defun org-contacts-setup-completion-at-point ()
  731. "Add `org-contacts-message-complete-function' as a new function
  732. to complete the thing at point."
  733. (add-to-list 'completion-at-point-functions
  734. 'org-contacts-message-complete-function))
  735. (defun org-contacts-unload-hook ()
  736. (remove-hook 'message-mode-hook 'org-contacts-setup-completion-at-point))
  737. (when (and org-contacts-enable-completion
  738. (boundp 'completion-at-point-functions))
  739. (add-hook 'message-mode-hook 'org-contacts-setup-completion-at-point))
  740. (defun org-contacts-wl-get-from-header-content ()
  741. "Retrieve the content of the `From' header of an email.
  742. Works from wl-summary-mode and mime-view-mode - that is while viewing email.
  743. Depends on Wanderlust been loaded."
  744. (with-current-buffer (org-capture-get :original-buffer)
  745. (cond
  746. ((eq major-mode 'wl-summary-mode) (when (and (boundp 'wl-summary-buffer-elmo-folder)
  747. wl-summary-buffer-elmo-folder)
  748. (elmo-message-field
  749. wl-summary-buffer-elmo-folder
  750. (wl-summary-message-number)
  751. 'from)))
  752. ((eq major-mode 'mime-view-mode) (std11-narrow-to-header)
  753. (prog1
  754. (std11-fetch-field "From")
  755. (widen))))))
  756. (defun org-contacts-wl-get-name-email ()
  757. "Get name and email address from Wanderlust email.
  758. See `org-contacts-wl-get-from-header-content' for limitations."
  759. (let ((from (org-contacts-wl-get-from-header-content)))
  760. (when from
  761. (list (wl-address-header-extract-realname from)
  762. (wl-address-header-extract-address from)))))
  763. (defun org-contacts-template-wl-name (&optional return-value)
  764. "Try to return the contact name for a template from wl.
  765. If not found, return RETURN-VALUE or something that would ask the
  766. user."
  767. (or (car (org-contacts-wl-get-name-email))
  768. return-value
  769. "%^{Name}"))
  770. (defun org-contacts-template-wl-email (&optional return-value)
  771. "Try to return the contact email for a template from Wanderlust.
  772. If not found return RETURN-VALUE or something that would ask the user."
  773. (or (cadr (org-contacts-wl-get-name-email))
  774. return-value
  775. (concat "%^{" org-contacts-email-property "}p")))
  776. (defun org-contacts-view-send-email (&optional ask)
  777. "Send email to the contact at point.
  778. If ASK is set, ask for the email address even if there's only one
  779. address."
  780. (interactive "P")
  781. (let ((marker (org-get-at-bol 'org-hd-marker)))
  782. (org-with-point-at marker
  783. (let ((emails (org-entry-get (point) org-contacts-email-property)))
  784. (if emails
  785. (let ((email-list (org-contacts-split-property emails)))
  786. (if (and (= (length email-list) 1) (not ask))
  787. (compose-mail (org-contacts-format-email
  788. (org-get-heading t) emails))
  789. (let ((email (completing-read "Send mail to which address: " email-list)))
  790. (setq email (org-contacts-strip-link email))
  791. (org-contacts-check-mail-address email)
  792. (compose-mail (org-contacts-format-email (org-get-heading t) email)))))
  793. (error (format "This contact has no mail address set (no %s property)"
  794. org-contacts-email-property)))))))
  795. (defun org-contacts-get-icon (&optional pom)
  796. "Get icon for contact at POM."
  797. (setq pom (or pom (point)))
  798. (catch 'icon
  799. ;; Use `org-contacts-icon-property'
  800. (let ((image-data (org-entry-get pom org-contacts-icon-property)))
  801. (when image-data
  802. (throw 'icon
  803. (if (fboundp 'gnus-rescale-image)
  804. (gnus-rescale-image (create-image image-data)
  805. (cons org-contacts-icon-size org-contacts-icon-size))
  806. (create-image image-data)))))
  807. ;; Next, try Gravatar
  808. (when org-contacts-icon-use-gravatar
  809. (let* ((gravatar-size org-contacts-icon-size)
  810. (email-list (org-entry-get pom org-contacts-email-property))
  811. (gravatar
  812. (when email-list
  813. (loop for email in (org-contacts-split-property email-list)
  814. for gravatar = (gravatar-retrieve-synchronously (org-contacts-strip-link email))
  815. if (and gravatar
  816. (not (eq gravatar 'error)))
  817. return gravatar))))
  818. (when gravatar (throw 'icon gravatar))))))
  819. (defun org-contacts-irc-buffer (&optional pom)
  820. "Get the IRC buffer associated with the entry at POM."
  821. (setq pom (or pom (point)))
  822. (let ((nick (org-entry-get pom org-contacts-nickname-property)))
  823. (when nick
  824. (let ((buffer (get-buffer nick)))
  825. (when buffer
  826. (with-current-buffer buffer
  827. (when (eq major-mode 'erc-mode)
  828. buffer)))))))
  829. (defun org-contacts-irc-number-of-unread-messages (&optional pom)
  830. "Return the number of unread messages for contact at POM."
  831. (when (boundp 'erc-modified-channels-alist)
  832. (let ((number (cadr (assoc (org-contacts-irc-buffer pom) erc-modified-channels-alist))))
  833. (if number
  834. (format (concat "%3d unread message" (if (> number 1) "s" " ") " ") number)
  835. (make-string 21 ? )))))
  836. (defun org-contacts-view-switch-to-irc-buffer ()
  837. "Switch to the IRC buffer of the current contact if it has one."
  838. (interactive)
  839. (let ((marker (org-get-at-bol 'org-hd-marker)))
  840. (org-with-point-at marker
  841. (switch-to-buffer-other-window (org-contacts-irc-buffer)))))
  842. (defun org-contacts-completing-read-nickname (prompt collection
  843. &optional predicate require-match initial-input
  844. hist def inherit-input-method)
  845. "Like `completing-read' but reads a nickname."
  846. (org-completing-read prompt (append collection (erc-nicknames-list)) predicate require-match
  847. initial-input hist def inherit-input-method))
  848. (defun erc-nicknames-list ()
  849. "Return all nicknames of all ERC buffers."
  850. (loop for buffer in (erc-buffer-list)
  851. nconc (with-current-buffer buffer
  852. (loop for user-entry in (mapcar 'car (erc-get-channel-user-list))
  853. collect (elt user-entry 1)))))
  854. (add-to-list 'org-property-set-functions-alist
  855. `(,org-contacts-nickname-property . org-contacts-completing-read-nickname))
  856. (defun org-contacts-vcard-escape (str)
  857. "Escape ; , and \n in STR for the VCard format."
  858. ;; Thanks to this library for the regexp:
  859. ;; http://www.emacswiki.org/cgi-bin/wiki/bbdb-vcard-export.el
  860. (when str
  861. (replace-regexp-in-string
  862. "\n" "\\\\n"
  863. (replace-regexp-in-string "\\(;\\|,\\|\\\\\\)" "\\\\\\1" str))))
  864. (defun org-contacts-vcard-encode-name (name)
  865. "Try to encode NAME as VCard's N property.
  866. The N property expects
  867. FamilyName;GivenName;AdditionalNames;Prefix;Postfix.
  868. Org-contacts does not specify how to encode the name. So we try
  869. to do our best."
  870. (concat (replace-regexp-in-string "\\(\\w+\\) \\(.*\\)" "\\2;\\1" name) ";;;"))
  871. (defun org-contacts-vcard-format (contact)
  872. "Formats CONTACT in VCard 3.0 format."
  873. (let* ((properties (nth 2 contact))
  874. (name (org-contacts-vcard-escape (car contact)))
  875. (n (org-contacts-vcard-encode-name name))
  876. (email (cdr (assoc-string org-contacts-email-property properties)))
  877. (tel (cdr (assoc-string org-contacts-tel-property properties)))
  878. (ignore-list (cdr (assoc-string org-contacts-ignore-property properties)))
  879. (ignore-list (when ignore-list
  880. (org-contacts-split-property ignore-list)))
  881. (note (cdr (assoc-string org-contacts-note-property properties)))
  882. (bday (org-contacts-vcard-escape (cdr (assoc-string org-contacts-birthday-property properties))))
  883. (addr (cdr (assoc-string org-contacts-address-property properties)))
  884. (nick (org-contacts-vcard-escape (cdr (assoc-string org-contacts-nickname-property properties))))
  885. (head (format "BEGIN:VCARD\nVERSION:3.0\nN:%s\nFN:%s\n" n name))
  886. emails-list result phones-list)
  887. (concat head
  888. (when email (progn
  889. (setq emails-list (org-contacts-remove-ignored-property-values ignore-list (org-contacts-split-property email)))
  890. (setq result "")
  891. (while emails-list
  892. (setq result (concat result "EMAIL:" (org-contacts-strip-link (car emails-list)) "\n"))
  893. (setq emails-list (cdr emails-list)))
  894. result))
  895. (when addr
  896. (format "ADR:;;%s\n" (replace-regexp-in-string "\\, ?" ";" addr)))
  897. (when tel (progn
  898. (setq phones-list (org-contacts-remove-ignored-property-values ignore-list (org-contacts-split-property tel)))
  899. (setq result "")
  900. (while phones-list
  901. (setq result (concat result "TEL:" (org-contacts-strip-link (org-link-unescape (car phones-list))) "\n"))
  902. (setq phones-list (cdr phones-list)))
  903. result))
  904. (when bday
  905. (let ((cal-bday (calendar-gregorian-from-absolute (org-time-string-to-absolute bday))))
  906. (format "BDAY:%04d-%02d-%02d\n"
  907. (calendar-extract-year cal-bday)
  908. (calendar-extract-month cal-bday)
  909. (calendar-extract-day cal-bday))))
  910. (when nick (format "NICKNAME:%s\n" nick))
  911. (when note (format "NOTE:%s\n" note))
  912. "END:VCARD\n\n")))
  913. (defun org-contacts-export-as-vcard (&optional name file to-buffer)
  914. "Export org contacts to V-Card 3.0.
  915. By default, all contacts are exported to `org-contacts-vcard-file'.
  916. When NAME is \\[universal-argument], prompts for a contact name.
  917. When NAME is \\[universal-argument] \\[universal-argument],
  918. prompts for a contact name and a file name where to export.
  919. When NAME is \\[universal-argument] \\[universal-argument]
  920. \\[universal-argument], prompts for a contact name and a buffer where to export.
  921. If the function is not called interactively, all parameters are
  922. passed to `org-contacts-export-as-vcard-internal'."
  923. (interactive "P")
  924. (when (called-interactively-p 'any)
  925. (cl-psetf name
  926. (when name
  927. (read-string "Contact name: "
  928. (nth 0 (org-contacts-at-point))))
  929. file
  930. (when (equal name '(16))
  931. (read-file-name "File: " nil org-contacts-vcard-file))
  932. to-buffer
  933. (when (equal name '(64))
  934. (read-buffer "Buffer: "))))
  935. (org-contacts-export-as-vcard-internal name file to-buffer))
  936. (defun org-contacts-export-as-vcard-internal (&optional name file to-buffer)
  937. "Export all contacts matching NAME as VCard 3.0.
  938. If TO-BUFFER is nil, the content is written to FILE or
  939. `org-contacts-vcard-file'. If TO-BUFFER is non-nil, the buffer
  940. is created and the VCard is written into that buffer."
  941. (let* ((filename (or file org-contacts-vcard-file))
  942. (buffer (if to-buffer
  943. (get-buffer-create to-buffer)
  944. (find-file-noselect filename))))
  945. (message "Exporting...")
  946. (set-buffer buffer)
  947. (let ((inhibit-read-only t)) (erase-buffer))
  948. (fundamental-mode)
  949. (when (fboundp 'set-buffer-file-coding-system)
  950. (set-buffer-file-coding-system coding-system-for-write))
  951. (loop for contact in (org-contacts-filter name)
  952. do (insert (org-contacts-vcard-format contact)))
  953. (if to-buffer
  954. (current-buffer)
  955. (progn (save-buffer) (kill-buffer)))))
  956. (defun org-contacts-show-map (&optional name)
  957. "Show contacts on a map.
  958. Requires google-maps-el."
  959. (interactive)
  960. (unless (fboundp 'google-maps-static-show)
  961. (error "`org-contacts-show-map' requires `google-maps-el'"))
  962. (google-maps-static-show
  963. :markers
  964. (cl-loop
  965. for contact in (org-contacts-filter name)
  966. for addr = (cdr (assoc-string org-contacts-address-property (nth 2 contact)))
  967. if addr
  968. collect (cons (list addr) (list :label (string-to-char (car contact)))))))
  969. (defun org-contacts-strip-link (link)
  970. "Remove brackets, description, link type and colon from an org
  971. link string and return the pure link target."
  972. (let (startpos colonpos endpos)
  973. (setq startpos (string-match (regexp-opt '("[[tel:" "[[mailto:")) link))
  974. (if startpos
  975. (progn
  976. (setq colonpos (string-match ":" link))
  977. (setq endpos (string-match "\\]" link))
  978. (if endpos (substring link (1+ colonpos) endpos) link))
  979. (progn
  980. (setq startpos (string-match "mailto:" link))
  981. (setq colonpos (string-match ":" link))
  982. (if startpos (substring link (1+ colonpos)) link)))))
  983. ;; Add the link type supported by org-contacts-strip-link
  984. ;; so everything is in order for its use in Org files
  985. (org-link-set-parameters "tel")
  986. (defun org-contacts-split-property (string &optional separators omit-nulls)
  987. "Custom version of `split-string'.
  988. Split a property STRING into sub-strings bounded by matches
  989. for SEPARATORS but keep Org links intact.
  990. The beginning and end of STRING, and each match for SEPARATORS, are
  991. splitting points. The substrings matching SEPARATORS are removed, and
  992. the substrings between the splitting points are collected as a list,
  993. which is returned.
  994. If SEPARATORS is non-nil, it should be a regular expression
  995. matching text which separates, but is not part of, the
  996. substrings. If nil it defaults to `org-contacts-property-values-separators',
  997. normally \"[,; \f\t\n\r\v]+\", and OMIT-NULLS is forced to t.
  998. If OMIT-NULLS is t, zero-length substrings are omitted from the list \(so
  999. that for the default value of SEPARATORS leading and trailing whitespace
  1000. are effectively trimmed). If nil, all zero-length substrings are retained."
  1001. (let* ((omit-nulls (if separators omit-nulls t))
  1002. (rexp (or separators org-contacts-property-values-separators))
  1003. (inputlist (split-string string rexp omit-nulls))
  1004. (linkstring "")
  1005. (bufferstring "")
  1006. (proplist (list "")))
  1007. (while inputlist
  1008. (setq bufferstring (pop inputlist))
  1009. (if (string-match "\\[\\[" bufferstring)
  1010. (progn
  1011. (setq linkstring (concat bufferstring " "))
  1012. (while (not (string-match "\\]\\]" bufferstring))
  1013. (setq bufferstring (pop inputlist))
  1014. (setq linkstring (concat linkstring bufferstring " ")))
  1015. (setq proplist (cons (org-trim linkstring) proplist)))
  1016. (setq proplist (cons bufferstring proplist))))
  1017. (cdr (reverse proplist))))
  1018. (provide 'org-contacts)
  1019. ;;; org-contacts.el ends here