org-contacts.el 47 KB

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