org-bbdb.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
  2. ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>,
  4. ;; Thomas Baumann <thomas dot baumann at ch dot tum dot de>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 7.7
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file implements links to BBDB database entries from within Org-mode.
  25. ;; Org-mode loads this module by default - if this is not what you want,
  26. ;; configure the variable `org-modules'.
  27. ;; It also implements an interface (based on Ivar Rummelhoff's
  28. ;; bbdb-anniv.el) for those org-mode users, who do not use the diary
  29. ;; but who do want to include the anniversaries stored in the BBDB
  30. ;; into the org-agenda. If you already include the `diary' into the
  31. ;; agenda, you might want to prefer to include the anniversaries in
  32. ;; the diary using bbdb-anniv.el.
  33. ;;
  34. ;; Put the following in /somewhere/at/home/diary.org and make sure
  35. ;; that this file is in `org-agenda-files`
  36. ;;
  37. ;; %%(org-bbdb-anniversaries)
  38. ;;
  39. ;; For example my diary.org looks like:
  40. ;; * Anniversaries
  41. ;; #+CATEGORY: Anniv
  42. ;; %%(org-bbdb-anniversaries)
  43. ;;
  44. ;;
  45. ;; To add an anniversary to a BBDB record, press `C-o' in the record.
  46. ;; You will be prompted for the field name, in this case it must be
  47. ;; "anniversary". If this is the first time you are using this field,
  48. ;; you need to confirm that it should be created.
  49. ;;
  50. ;; The format of an anniversary field stored in BBDB is the following
  51. ;; (items in {} are optional):
  52. ;;
  53. ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}
  54. ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}...
  55. ;;
  56. ;; CLASS-OR-FORMAT-STRING is one of two things:
  57. ;;
  58. ;; - an identifier for a class of anniversaries (eg. birthday or
  59. ;; wedding) from `org-bbdb-anniversary-format-alist' which then
  60. ;; defines the format string for this class
  61. ;; - the (format) string displayed in the diary.
  62. ;;
  63. ;; You can enter multiple anniversaries for a single BBDB record by
  64. ;; separating them with a newline character. At the BBDB prompt for
  65. ;; the field value, type `C-q C-j' to enter a newline between two
  66. ;; anniversaries.
  67. ;;
  68. ;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the
  69. ;; value of `org-bbdb-default-anniversary-format' ("birthday" by
  70. ;; default).
  71. ;;
  72. ;; The substitutions in the format string are (in order):
  73. ;; - the name of the record containing this anniversary
  74. ;; - the number of years
  75. ;; - an ordinal suffix (st, nd, rd, th) for the year
  76. ;;
  77. ;; See the documentation of `org-bbdb-anniversary-format-alist' for
  78. ;; further options.
  79. ;;
  80. ;; Example
  81. ;;
  82. ;; 1973-06-22
  83. ;; 20??-??-?? wedding
  84. ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
  85. ;;
  86. ;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB
  87. ;; link from which the entry at point originates.
  88. ;;
  89. ;;; Code:
  90. (require 'org)
  91. (eval-when-compile
  92. (require 'cl))
  93. ;; Declare external functions and variables
  94. (declare-function bbdb "ext:bbdb-com" (string elidep))
  95. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  96. (declare-function bbdb-current-record "ext:bbdb-com"
  97. (&optional planning-on-modifying))
  98. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  99. (declare-function bbdb-completing-read-record "ext:bbdb-com"
  100. (prompt &optional omit-records))
  101. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  102. (declare-function bbdb-record-name "ext:bbdb" (record))
  103. (declare-function bbdb-records "ext:bbdb"
  104. (&optional dont-check-disk already-in-db-buffer))
  105. (declare-function bbdb-split "ext:bbdb" (string separators))
  106. (declare-function bbdb-string-trim "ext:bbdb" (string))
  107. (declare-function calendar-leap-year-p "calendar" (year))
  108. (declare-function diary-ordinal-suffix "diary-lib" (n))
  109. (defvar date) ;; dynamically scoped from Org
  110. ;; Customization
  111. (defgroup org-bbdb-anniversaries nil
  112. "Customizations for including anniversaries from BBDB into Agenda."
  113. :group 'org-bbdb)
  114. (defcustom org-bbdb-default-anniversary-format "birthday"
  115. "Default anniversary class."
  116. :type 'string
  117. :group 'org-bbdb-anniversaries
  118. :require 'bbdb)
  119. (defcustom org-bbdb-anniversary-format-alist
  120. '(("birthday" lambda
  121. (name years suffix)
  122. (concat "Birthday: [[bbdb:" name "][" name " ("
  123. (format "%s" years) ; handles numbers as well as strings
  124. suffix ")]]"))
  125. ("wedding" lambda
  126. (name years suffix)
  127. (concat "[[bbdb:" name "][" name "'s "
  128. (format "%s" years)
  129. suffix " wedding anniversary]]")))
  130. "How different types of anniversaries should be formatted.
  131. An alist of elements (STRING . FORMAT) where STRING is the name of an
  132. anniversary class and format is either:
  133. 1) A format string with the following substitutions (in order):
  134. * the name of the record containing this anniversary
  135. * the number of years
  136. * an ordinal suffix (st, nd, rd, th) for the year
  137. 2) A function to be called with three arguments: NAME YEARS SUFFIX
  138. (string int string) returning a string for the diary or nil.
  139. 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
  140. scope of variables NAME, YEARS and SUFFIX (among others)."
  141. :type 'sexp
  142. :group 'org-bbdb-anniversaries
  143. :require 'bbdb)
  144. (defcustom org-bbdb-anniversary-field 'anniversary
  145. "The BBDB field which contains anniversaries.
  146. The anniversaries are stored in the following format
  147. YYYY-MM-DD Class-or-Format-String
  148. where class is one of the customized classes for anniversaries;
  149. birthday and wedding are predefined. Format-String can take three
  150. substitutions 1) the name of the record containing this
  151. anniversary, 2) the number of years, and 3) an ordinal suffix for
  152. the year.
  153. Multiple anniversaries can be separated by \\n."
  154. :type 'symbol
  155. :group 'org-bbdb-anniversaries
  156. :require 'bbdb)
  157. (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
  158. "How to retrieve `month date year' from the anniversary field.
  159. Customize if you have already filled your BBDB with dates
  160. different from YYYY-MM-DD. The function must return a list (month
  161. date year)."
  162. :type 'function
  163. :group 'org-bbdb-anniversaries
  164. :require 'bbdb)
  165. ;; Install the link type
  166. (org-add-link-type "bbdb" 'org-bbdb-open 'org-bbdb-export)
  167. (add-hook 'org-store-link-functions 'org-bbdb-store-link)
  168. ;; Implementation
  169. (defun org-bbdb-store-link ()
  170. "Store a link to a BBDB database entry."
  171. (when (eq major-mode 'bbdb-mode)
  172. ;; This is BBDB, we make this link!
  173. (let* ((name (bbdb-record-name (bbdb-current-record)))
  174. (company (bbdb-record-getprop (bbdb-current-record) 'company))
  175. (link (org-make-link "bbdb:" name)))
  176. (org-store-link-props :type "bbdb" :name name :company company
  177. :link link :description name)
  178. link)))
  179. (defun org-bbdb-export (path desc format)
  180. "Create the export version of a BBDB link specified by PATH or DESC.
  181. If exporting to either HTML or LaTeX FORMAT the link will be
  182. italicized, in all other cases it is left unchanged."
  183. (when (string= desc (format "bbdb:%s" path))
  184. (setq desc path))
  185. (cond
  186. ((eq format 'html) (format "<i>%s</i>" desc))
  187. ((eq format 'latex) (format "\\textit{%s}" desc))
  188. (t desc)))
  189. (defun org-bbdb-open (name)
  190. "Follow a BBDB link to NAME."
  191. (require 'bbdb)
  192. (let ((inhibit-redisplay (not debug-on-error))
  193. (bbdb-electric-p nil))
  194. (catch 'exit
  195. ;; Exact match on name
  196. (bbdb-name (concat "\\`" name "\\'") nil)
  197. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  198. ;; Exact match on name
  199. (bbdb-company (concat "\\`" name "\\'") nil)
  200. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  201. ;; Partial match on name
  202. (bbdb-name name nil)
  203. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  204. ;; Partial match on company
  205. (bbdb-company name nil)
  206. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  207. ;; General match including network address and notes
  208. (bbdb name nil)
  209. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  210. (delete-window (get-buffer-window "*BBDB*"))
  211. (error "No matching BBDB record")))))
  212. (defun org-bbdb-anniv-extract-date (time-str)
  213. "Convert YYYY-MM-DD to (month date year).
  214. Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted
  215. it will be considered unknown."
  216. (multiple-value-bind (a b c) (values-list (bbdb-split time-str "-"))
  217. (if (eq c nil)
  218. (list (string-to-number a)
  219. (string-to-number b)
  220. nil)
  221. (list (string-to-number b)
  222. (string-to-number c)
  223. (string-to-number a)))))
  224. (defun org-bbdb-anniv-split (str)
  225. "Split multiple entries in the BBDB anniversary field.
  226. Argument STR is the anniversary field in BBDB."
  227. (let ((pos (string-match "[ \t]" str)))
  228. (if pos (list (substring str 0 pos)
  229. (bbdb-string-trim (substring str pos)))
  230. (list str nil))))
  231. (defvar org-bbdb-anniv-hash nil
  232. "A hash holding anniversaries extracted from BBDB.
  233. The hash table is created on first use.")
  234. (defvar org-bbdb-updated-p t
  235. "This is non-nil if BBDB has been updated since we last built the hash.")
  236. (defun org-bbdb-make-anniv-hash ()
  237. "Create a hash with anniversaries extracted from BBDB, for fast access.
  238. The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
  239. (let (split tmp annivs)
  240. (clrhash org-bbdb-anniv-hash)
  241. (dolist (rec (bbdb-records))
  242. (when (setq annivs (bbdb-record-getprop
  243. rec org-bbdb-anniversary-field))
  244. (setq annivs (bbdb-split annivs "\n"))
  245. (while annivs
  246. (setq split (org-bbdb-anniv-split (pop annivs)))
  247. (multiple-value-bind (m d y)
  248. (values-list (funcall org-bbdb-extract-date-fun (car split)))
  249. (setq tmp (gethash (list m d) org-bbdb-anniv-hash))
  250. (puthash (list m d) (cons (list y
  251. (bbdb-record-name rec)
  252. (cadr split))
  253. tmp)
  254. org-bbdb-anniv-hash))))))
  255. (setq org-bbdb-updated-p nil))
  256. (defun org-bbdb-updated (rec)
  257. "Record the fact that BBDB has been updated.
  258. This is used by Org to re-create the anniversary hash table."
  259. (setq org-bbdb-updated-p t))
  260. (add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
  261. ;;;###autoload
  262. (defun org-bbdb-anniversaries()
  263. "Extract anniversaries from BBDB for display in the agenda."
  264. (require 'bbdb)
  265. (require 'diary-lib)
  266. (unless (hash-table-p org-bbdb-anniv-hash)
  267. (setq org-bbdb-anniv-hash
  268. (make-hash-table :test 'equal :size 366)))
  269. (when (or org-bbdb-updated-p
  270. (= 0 (hash-table-count org-bbdb-anniv-hash)))
  271. (org-bbdb-make-anniv-hash))
  272. (let* ((m (car date)) ; month
  273. (d (nth 1 date)) ; day
  274. (y (nth 2 date)) ; year
  275. (annivs (gethash (list m d) org-bbdb-anniv-hash))
  276. (text ())
  277. rec recs)
  278. ;; we don't want to miss people born on Feb. 29th
  279. (when (and (= m 3) (= d 1)
  280. (not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
  281. (not (calendar-leap-year-p y)))
  282. (setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
  283. (while (setq rec (pop recs))
  284. (push rec annivs)))
  285. (when annivs
  286. (while (setq rec (pop annivs))
  287. (when rec
  288. (let* ((class (or (nth 2 rec)
  289. org-bbdb-default-anniversary-format))
  290. (form (or (cdr (assoc-string
  291. class org-bbdb-anniversary-format-alist t))
  292. class)) ; (as format string)
  293. (name (nth 1 rec))
  294. (years (if (eq (car rec) nil)
  295. "unknown"
  296. (- y (car rec))))
  297. (suffix (if (eq (car rec) nil)
  298. ""
  299. (diary-ordinal-suffix years)))
  300. (tmp (cond
  301. ((functionp form)
  302. (funcall form name years suffix))
  303. ((listp form) (eval form))
  304. (t (format form name years suffix)))))
  305. (org-add-props tmp nil 'org-bbdb-name name)
  306. (if text
  307. (setq text (append text (list tmp)))
  308. (setq text (list tmp)))))
  309. ))
  310. text))
  311. (defun org-bbdb-complete-link ()
  312. "Read a bbdb link with name completion."
  313. (require 'bbdb-com)
  314. (concat "bbdb:"
  315. (bbdb-record-name (car (bbdb-completing-read-record "Name: ")))))
  316. (defun org-bbdb-anniv-export-ical ()
  317. "Extract anniversaries from BBDB and convert them to icalendar format."
  318. (require 'bbdb)
  319. (require 'diary-lib)
  320. (unless (hash-table-p org-bbdb-anniv-hash)
  321. (setq org-bbdb-anniv-hash
  322. (make-hash-table :test 'equal :size 366)))
  323. (when (or org-bbdb-updated-p
  324. (= 0 (hash-table-count org-bbdb-anniv-hash)))
  325. (org-bbdb-make-anniv-hash))
  326. (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash))
  327. (defun org-bbdb-format-vevent (key recs)
  328. (let (rec categ)
  329. (while (setq rec (pop recs))
  330. (setq categ (or (nth 2 rec) org-bbdb-default-anniversary-format))
  331. (princ (format "BEGIN:VEVENT
  332. UID: ANNIV-%4i%02i%02i-%s
  333. DTSTART:%4i%02i%02i
  334. SUMMARY:%s
  335. DESCRIPTION:%s
  336. CATEGORIES:%s
  337. RRULE:FREQ=YEARLY
  338. END:VEVENT\n"
  339. (nth 0 rec) (nth 0 key) (nth 1 key)
  340. (mapconcat 'identity
  341. (org-split-string (nth 1 rec) "[^a-zA-Z0-90]+")
  342. "-")
  343. (nth 0 rec) (nth 0 key) (nth 1 key)
  344. (nth 1 rec)
  345. (concat (capitalize categ) " " (nth 1 rec))
  346. categ)))))
  347. (provide 'org-bbdb)
  348. ;;; org-bbdb.el ends here