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