org-bbdb.el 14 KB

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