org-contacts.el 44 KB

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