org-bbdb.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.02b
  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, or (at your option)
  14. ;; 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; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;; Boston, MA 02110-1301, USA.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;; This file implements links to BBDB database entries from within Org-mode.
  27. ;; Org-mode loads this module by default - if this is not what you want,
  28. ;; configure the variable `org-modules'.
  29. ;; It also implements an interface (based on Ivar Rummelhoff's
  30. ;; bbdb-anniv.el) for those org-mode users, who do not use the diary
  31. ;; but who do want to include the anniversaries stored in the BBDB
  32. ;; into the org-agenda. If you already include the `diary' into the
  33. ;; agenda, you might want to prefer to include the anniversaries in
  34. ;; the diary using bbdb-anniv.el.
  35. ;;
  36. ;; Put the following in /somewhere/at/home/diary.org and make sure
  37. ;; that this file is in `org-agenda-files`
  38. ;;
  39. ;; %%(org-bbdb-anniversaries)
  40. ;;
  41. ;; For example my diary.org looks like:
  42. ;; * Anniversaries
  43. ;; #+CATEGORY: Anniv
  44. ;; %%(org-bbdb-anniversaries)
  45. ;;
  46. ;;
  47. ;; The anniversaries are stored in BBDB in the field `anniversary'
  48. ;; in the format
  49. ;;
  50. ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}*
  51. ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}*
  52. ;;
  53. ;; CLASS-OR-FORMAT-STRING is one of two things:
  54. ;;
  55. ;; * an identifier for a class of anniversaries (eg. birthday or
  56. ;; wedding) from `org-bbdb-anniversary-format-alist'.
  57. ;; * the (format) string displayed in the diary.
  58. ;;
  59. ;; It defaults to the value of `org-bbdb-default-anniversary-format'
  60. ;; ("birthday" by default).
  61. ;;
  62. ;; The substitutions in the format string are (in order):
  63. ;; * the name of the record containing this anniversary
  64. ;; * the number of years
  65. ;; * an ordinal suffix (st, nd, rd, th) for the year
  66. ;;
  67. ;; See the documentation of `org-bbdb-anniversary-format-alist' for
  68. ;; further options.
  69. ;;
  70. ;; Example
  71. ;;
  72. ;; 1973-06-22
  73. ;; 20??-??-?? wedding
  74. ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
  75. ;;; Code:
  76. (require 'org)
  77. (eval-when-compile
  78. (require 'cl))
  79. ;; Declare external functions and variables
  80. (declare-function bbdb "ext:bbdb-com" (string elidep))
  81. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  82. (declare-function bbdb-current-record "ext:bbdb-com"
  83. (&optional planning-on-modifying))
  84. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  85. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  86. (declare-function bbdb-record-name "ext:bbdb" (record))
  87. (declare-function bbdb-records "ext:bbdb"
  88. (&optional dont-check-disk already-in-db-buffer))
  89. (declare-function bbdb-split "ext:bbdb" (string separators))
  90. (declare-function bbdb-string-trim "ext:bbdb" (string))
  91. (declare-function calendar-leap-year-p "calendar" (year))
  92. (declare-function diary-ordinal-suffix "diary-lib" (n))
  93. (defvar date)
  94. ;; Customization
  95. (defgroup org-bbdb-anniversaries nil
  96. "Customizations for including anniversaries from BBDB into Agenda."
  97. :group 'org-bbdb)
  98. (defcustom org-bbdb-default-anniversary-format "birthday"
  99. "Default anniversary class."
  100. :type 'string
  101. :group 'org-bbdb-anniversaries
  102. :require 'bbdb)
  103. (defcustom org-bbdb-anniversary-format-alist
  104. '( ("birthday" . "Birthday: %s (%d%s)")
  105. ("wedding" . "%s's %d%s wedding anniversary") )
  106. "How different types of anniversaries should be formatted.
  107. An alist of elements (STRING . FORMAT) where STRING is the name of an
  108. anniversary class and format is either:
  109. 1) A format string with the following substitutions (in order):
  110. * the name of the record containing this anniversary
  111. * the number of years
  112. * an ordinal suffix (st, nd, rd, th) for the year
  113. 2) A function to be called with three arguments: NAME YEARS SUFFIX
  114. (string int string) returning a string for the diary or nil.
  115. 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
  116. scope of variables NAME, YEARS and SUFFIX (among others)."
  117. :type 'sexp
  118. :group 'org-bbdb-anniversaries
  119. :require 'bbdb)
  120. (defcustom org-bbdb-anniversary-field 'anniversary
  121. "The BBDB field which contains anniversaries.
  122. The anniversaries are stored in the following format
  123. YYYY-MM-DD Class-or-Format-String
  124. where class is one of the customized classes for anniversaries;
  125. birthday and wedding are predefined. Format-String can take three
  126. substitutions 1) the name of the record containing this
  127. anniversary, 2) the number of years, and 3) an ordinal suffix for
  128. the year.
  129. Multiple anniversaries can be separated by \\n"
  130. :type 'symbol
  131. :group 'org-bbdb-anniversaries
  132. :require 'bbdb)
  133. (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
  134. "How to retrieve `month date year' from the anniversary field.
  135. Customize if you have already filled your bbdb with dates
  136. different from YYYY-MM-DD. The function must return a list (month
  137. date year)"
  138. :type 'function
  139. :group 'org-bbdb-anniversaries
  140. :require 'bbdb)
  141. ;; Install the link type
  142. (org-add-link-type "bbdb" 'org-bbdb-open 'org-bbdb-export)
  143. (add-hook 'org-store-link-functions 'org-bbdb-store-link)
  144. ;; Implementation
  145. (defun org-bbdb-store-link ()
  146. "Store a link to a BBDB database entry."
  147. (when (eq major-mode 'bbdb-mode)
  148. ;; This is BBDB, we make this link!
  149. (let* ((name (bbdb-record-name (bbdb-current-record)))
  150. (company (bbdb-record-getprop (bbdb-current-record) 'company))
  151. (link (org-make-link "bbdb:" name)))
  152. (org-store-link-props :type "bbdb" :name name :company company
  153. :link link :description name)
  154. link)))
  155. (defun org-bbdb-export (path desc format)
  156. "Create the export version of a BBDB link specified by PATH or DESC.
  157. If exporting to either HTML or LaTeX FORMAT the link will be
  158. italicised, in all other cases it is left unchanged."
  159. "Create the exprt verison of a bbdb link."
  160. (cond
  161. ((eq format 'html) (format "<i>%s</i>" (or desc path)))
  162. ((eq format 'latex) (format "\\textit{%s}" (or desc path)))
  163. (t (or desc path))))
  164. (defun org-bbdb-open (name)
  165. "Follow a BBDB link to NAME."
  166. (require 'bbdb)
  167. (let ((inhibit-redisplay (not debug-on-error))
  168. (bbdb-electric-p nil))
  169. (catch 'exit
  170. ;; Exact match on name
  171. (bbdb-name (concat "\\`" name "\\'") nil)
  172. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  173. ;; Exact match on name
  174. (bbdb-company (concat "\\`" name "\\'") nil)
  175. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  176. ;; Partial match on name
  177. (bbdb-name name nil)
  178. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  179. ;; Partial match on company
  180. (bbdb-company name nil)
  181. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  182. ;; General match including network address and notes
  183. (bbdb name nil)
  184. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  185. (delete-window (get-buffer-window "*BBDB*"))
  186. (error "No matching BBDB record")))))
  187. (defun org-bbdb-anniv-extract-date (time-str)
  188. "Convert YYYY-MM-DD to (month date year).
  189. Argument TIME-STR is the value retrieved from BBDB."
  190. (multiple-value-bind (y m d) (bbdb-split time-str "-")
  191. (list (string-to-number m)
  192. (string-to-number d)
  193. (string-to-number y))))
  194. (defun org-bbdb-anniv-split (str)
  195. "Split mutliple entries in the BBDB anniversary field.
  196. Argument STR is the anniversary field in BBDB."
  197. (let ((pos (string-match "[ \t]" str)))
  198. (if pos (list (substring str 0 pos)
  199. (bbdb-string-trim (substring str pos)))
  200. (list str nil))))
  201. ;;;###autoload
  202. (defun org-bbdb-anniversaries ()
  203. "Extract anniversaries from BBDB for display in the agenda."
  204. (require 'diary-lib)
  205. (let ((dates (list (cons (cons (car date) ; month
  206. (nth 1 date)) ; day
  207. (nth 2 date)))) ; year
  208. (text ())
  209. annivs date years
  210. split class form)
  211. (dolist (rec (bbdb-records))
  212. (when (setq annivs (bbdb-record-getprop
  213. rec org-bbdb-anniversary-field))
  214. (setq annivs (bbdb-split annivs "\n"))
  215. (while annivs
  216. (setq split (org-bbdb-anniv-split (pop annivs)))
  217. (multiple-value-bind (m d y)
  218. (funcall org-bbdb-extract-date-fun (car split))
  219. (when (and (or (setq date (assoc (cons m d) dates))
  220. (and (= d 29)
  221. (= m 2)
  222. (setq date (assoc '(3 . 1) dates))
  223. (not (calendar-leap-year-p (cdr date)))))
  224. (< 0 (setq years (- (cdr date) y))))
  225. (let* ((class (or (cadr split)
  226. org-bbdb-default-anniversary-format))
  227. (form (or (cdr (assoc class
  228. org-bbdb-anniversary-format-alist))
  229. class)) ; (as format string)
  230. (name (bbdb-record-name rec))
  231. (suffix (diary-ordinal-suffix years))
  232. (tmp (cond
  233. ((functionp form)
  234. (funcall form name years suffix))
  235. ((listp form) (eval form))
  236. (t (format form name years suffix)))))
  237. (if text
  238. (setq text (append text (list tmp)))
  239. (setq text (list tmp))))
  240. )))))
  241. (when text
  242. (mapconcat 'identity text "; "))))
  243. (provide 'org-bbdb)
  244. ;; arch-tag: 9e4f275d-d080-48c1-b040-62247f66b5c2
  245. ;;; org-bbdb.el ends here