org-bbdb.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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: 6.11a
  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. ;; The anniversaries are stored in BBDB in the field `anniversary'
  46. ;; in the format
  47. ;;
  48. ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}*
  49. ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}*
  50. ;;
  51. ;; CLASS-OR-FORMAT-STRING is one of two things:
  52. ;;
  53. ;; * an identifier for a class of anniversaries (eg. birthday or
  54. ;; wedding) from `org-bbdb-anniversary-format-alist'.
  55. ;; * the (format) string displayed in the diary.
  56. ;;
  57. ;; It defaults to the value of `org-bbdb-default-anniversary-format'
  58. ;; ("birthday" by default).
  59. ;;
  60. ;; The substitutions in the format string are (in order):
  61. ;; * the name of the record containing this anniversary
  62. ;; * the number of years
  63. ;; * an ordinal suffix (st, nd, rd, th) for the year
  64. ;;
  65. ;; See the documentation of `org-bbdb-anniversary-format-alist' for
  66. ;; further options.
  67. ;;
  68. ;; Example
  69. ;;
  70. ;; 1973-06-22
  71. ;; 20??-??-?? wedding
  72. ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
  73. ;;
  74. ;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB
  75. ;; link from which the entry at point originates.
  76. ;;
  77. ;;; Code:
  78. (require 'org)
  79. (eval-when-compile
  80. (require 'cl))
  81. ;; Declare external functions and variables
  82. (declare-function bbdb "ext:bbdb-com" (string elidep))
  83. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  84. (declare-function bbdb-current-record "ext:bbdb-com"
  85. (&optional planning-on-modifying))
  86. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  87. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  88. (declare-function bbdb-record-name "ext:bbdb" (record))
  89. (declare-function bbdb-records "ext:bbdb"
  90. (&optional dont-check-disk already-in-db-buffer))
  91. (declare-function bbdb-split "ext:bbdb" (string separators))
  92. (declare-function bbdb-string-trim "ext:bbdb" (string))
  93. (declare-function calendar-leap-year-p "calendar" (year))
  94. (declare-function diary-ordinal-suffix "diary-lib" (n))
  95. (defvar date) ;; dynamically scoped from Org
  96. ;; Customization
  97. (defgroup org-bbdb-anniversaries nil
  98. "Customizations for including anniversaries from BBDB into Agenda."
  99. :group 'org-bbdb)
  100. (defcustom org-bbdb-default-anniversary-format "birthday"
  101. "Default anniversary class."
  102. :type 'string
  103. :group 'org-bbdb-anniversaries
  104. :require 'bbdb)
  105. (defcustom org-bbdb-anniversary-format-alist
  106. '(("birthday" lambda
  107. (name years suffix)
  108. (concat "Birthday: [[bbdb:" name "][" name " ("
  109. (number-to-string years)
  110. suffix ")]]"))
  111. ("wedding" lambda
  112. (name years suffix)
  113. (concat "[[bbdb:" name "][" name "'s "
  114. (number-to-string years)
  115. suffix " wedding anniversary]]")))
  116. "How different types of anniversaries should be formatted.
  117. An alist of elements (STRING . FORMAT) where STRING is the name of an
  118. anniversary class and format is either:
  119. 1) A format string with the following substitutions (in order):
  120. * the name of the record containing this anniversary
  121. * the number of years
  122. * an ordinal suffix (st, nd, rd, th) for the year
  123. 2) A function to be called with three arguments: NAME YEARS SUFFIX
  124. (string int string) returning a string for the diary or nil.
  125. 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
  126. scope of variables NAME, YEARS and SUFFIX (among others)."
  127. :type 'sexp
  128. :group 'org-bbdb-anniversaries
  129. :require 'bbdb)
  130. (defcustom org-bbdb-anniversary-field 'anniversary
  131. "The BBDB field which contains anniversaries.
  132. The anniversaries are stored in the following format
  133. YYYY-MM-DD Class-or-Format-String
  134. where class is one of the customized classes for anniversaries;
  135. birthday and wedding are predefined. Format-String can take three
  136. substitutions 1) the name of the record containing this
  137. anniversary, 2) the number of years, and 3) an ordinal suffix for
  138. the year.
  139. Multiple anniversaries can be separated by \\n."
  140. :type 'symbol
  141. :group 'org-bbdb-anniversaries
  142. :require 'bbdb)
  143. (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
  144. "How to retrieve `month date year' from the anniversary field.
  145. Customize if you have already filled your BBDB with dates
  146. different from YYYY-MM-DD. The function must return a list (month
  147. date year)."
  148. :type 'function
  149. :group 'org-bbdb-anniversaries
  150. :require 'bbdb)
  151. ;; Install the link type
  152. (org-add-link-type "bbdb" 'org-bbdb-open 'org-bbdb-export)
  153. (add-hook 'org-store-link-functions 'org-bbdb-store-link)
  154. ;; Implementation
  155. (defun org-bbdb-store-link ()
  156. "Store a link to a BBDB database entry."
  157. (when (eq major-mode 'bbdb-mode)
  158. ;; This is BBDB, we make this link!
  159. (let* ((name (bbdb-record-name (bbdb-current-record)))
  160. (company (bbdb-record-getprop (bbdb-current-record) 'company))
  161. (link (org-make-link "bbdb:" name)))
  162. (org-store-link-props :type "bbdb" :name name :company company
  163. :link link :description name)
  164. link)))
  165. (defun org-bbdb-export (path desc format)
  166. "Create the export version of a BBDB link specified by PATH or DESC.
  167. If exporting to either HTML or LaTeX FORMAT the link will be
  168. italicised, in all other cases it is left unchanged."
  169. (cond
  170. ((eq format 'html) (format "<i>%s</i>" (or desc path)))
  171. ((eq format 'latex) (format "\\textit{%s}" (or desc path)))
  172. (t (or desc path))))
  173. (defun org-bbdb-open (name)
  174. "Follow a BBDB link to NAME."
  175. (require 'bbdb)
  176. (let ((inhibit-redisplay (not debug-on-error))
  177. (bbdb-electric-p nil))
  178. (catch 'exit
  179. ;; Exact match on name
  180. (bbdb-name (concat "\\`" name "\\'") nil)
  181. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  182. ;; Exact match on name
  183. (bbdb-company (concat "\\`" name "\\'") nil)
  184. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  185. ;; Partial match on name
  186. (bbdb-name name nil)
  187. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  188. ;; Partial match on company
  189. (bbdb-company name nil)
  190. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  191. ;; General match including network address and notes
  192. (bbdb name nil)
  193. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  194. (delete-window (get-buffer-window "*BBDB*"))
  195. (error "No matching BBDB record")))))
  196. (defun org-bbdb-anniv-extract-date (time-str)
  197. "Convert YYYY-MM-DD to (month date year).
  198. Argument TIME-STR is the value retrieved from BBDB."
  199. (multiple-value-bind (y m d) (bbdb-split time-str "-")
  200. (list (string-to-number m)
  201. (string-to-number d)
  202. (string-to-number y))))
  203. (defun org-bbdb-anniv-split (str)
  204. "Split multiple entries in the BBDB anniversary field.
  205. Argument STR is the anniversary field in BBDB."
  206. (let ((pos (string-match "[ \t]" str)))
  207. (if pos (list (substring str 0 pos)
  208. (bbdb-string-trim (substring str pos)))
  209. (list str nil))))
  210. (defvar org-bbdb-anniv-hash nil
  211. "A hash holding anniversaries extracted from BBDB.
  212. The hash table is created on first use.")
  213. (defvar org-bbdb-updated-p t
  214. "This is non-nil if BBDB has been updated since we last built the hash.")
  215. (defun org-bbdb-make-anniv-hash ()
  216. "Create a hash with anniversaries extracted from BBDB, for fast access.
  217. The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
  218. (let (split tmp annivs)
  219. (clrhash org-bbdb-anniv-hash)
  220. (dolist (rec (bbdb-records))
  221. (when (setq annivs (bbdb-record-getprop
  222. rec org-bbdb-anniversary-field))
  223. (setq annivs (bbdb-split annivs "\n"))
  224. (while annivs
  225. (setq split (org-bbdb-anniv-split (pop annivs)))
  226. (multiple-value-bind (m d y)
  227. (funcall org-bbdb-extract-date-fun (car split))
  228. (setq tmp (gethash (list m d) org-bbdb-anniv-hash))
  229. (puthash (list m d) (cons (list y
  230. (bbdb-record-name rec)
  231. (cadr split))
  232. tmp)
  233. org-bbdb-anniv-hash))))))
  234. (setq org-bbdb-updated-p nil))
  235. (defun org-bbdb-updated (rec)
  236. "Record the fact that BBDB has been updated.
  237. This is used by Org to re-create the anniversary hash table."
  238. (setq org-bbdb-updated-p t))
  239. (add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
  240. ;;;###autoload
  241. (defun org-bbdb-anniversaries()
  242. "Extract anniversaries from BBDB for display in the agenda."
  243. (require 'bbdb)
  244. (require 'diary-lib)
  245. (unless (hash-table-p org-bbdb-anniv-hash)
  246. (setq org-bbdb-anniv-hash
  247. (make-hash-table :test 'equal :size 366)))
  248. (when (or org-bbdb-updated-p
  249. (= 0 (hash-table-count org-bbdb-anniv-hash)))
  250. (org-bbdb-make-anniv-hash))
  251. (let* ((m (car date)) ; month
  252. (d (nth 1 date)) ; day
  253. (y (nth 2 date)) ; year
  254. (annivs (gethash (list m d) org-bbdb-anniv-hash))
  255. (text ())
  256. split class form rec recs)
  257. ;; we don't want to miss people born on Feb. 29th
  258. (when (and (= m 3) (= d 1)
  259. (not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
  260. (not (calendar-leap-year-p y)))
  261. (setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
  262. (while (setq rec (pop recs))
  263. (push rec annivs)))
  264. (when annivs
  265. (while (setq rec (pop annivs))
  266. (when rec
  267. (let* ((class (or (nth 2 rec)
  268. org-bbdb-default-anniversary-format))
  269. (form (or (cdr (assoc class
  270. org-bbdb-anniversary-format-alist))
  271. class)) ; (as format string)
  272. (name (nth 1 rec))
  273. (years (- y (car rec)))
  274. (suffix (diary-ordinal-suffix years))
  275. (tmp (cond
  276. ((functionp form)
  277. (funcall form name years suffix))
  278. ((listp form) (eval form))
  279. (t (format form name years suffix)))))
  280. (org-add-props tmp nil 'org-bbdb-name name)
  281. (if text
  282. (setq text (append text (list tmp)))
  283. (setq text (list tmp)))))
  284. ))
  285. (when text
  286. (mapconcat 'identity text "; "))))
  287. (provide 'org-bbdb)
  288. ;; arch-tag: 9e4f275d-d080-48c1-b040-62247f66b5c2
  289. ;;; org-bbdb.el ends here