org-bbdb.el 11 KB

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