1 ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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:
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 string 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
75 ;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the
76 ;; value of `org-bbdb-default-anniversary-format' ("birthday" by
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
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.
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-completing-read-record
"ext:bbdb-com"
110 (prompt &optional omit-records
))
111 (declare-function bbdb-record-getprop
"ext:bbdb" (record property
))
112 (declare-function bbdb-record-name
"ext:bbdb" (record))
113 (declare-function bbdb-records
"ext:bbdb"
114 (&optional dont-check-disk already-in-db-buffer
))
115 (declare-function bbdb-split
"ext:bbdb" (string separators
))
116 (declare-function bbdb-string-trim
"ext:bbdb" (string))
118 (declare-function calendar-leap-year-p
"calendar" (year))
119 (declare-function diary-ordinal-suffix
"diary-lib" (n))
121 (defvar date
) ;; dynamically scoped from Org
125 (defgroup org-bbdb-anniversaries nil
126 "Customizations for including anniversaries from BBDB into Agenda."
129 (defcustom org-bbdb-default-anniversary-format
"birthday"
130 "Default anniversary class."
132 :group
'org-bbdb-anniversaries
135 (defcustom org-bbdb-anniversary-format-alist
138 (concat "Birthday: [[bbdb:" name
"][" name
" ("
139 (format "%s" years
) ; handles numbers as well as strings
143 (concat "[[bbdb:" name
"][" name
"'s "
145 suffix
" wedding anniversary]]")))
146 "How different types of anniversaries should be formatted.
147 An alist of elements (STRING . FORMAT) where STRING is the name of an
148 anniversary class and format is either:
149 1) A format string with the following substitutions (in order):
150 * the name of the record containing this anniversary
151 * the number of years
152 * an ordinal suffix (st, nd, rd, th) for the year
154 2) A function to be called with three arguments: NAME YEARS SUFFIX
155 (string int string) returning a string for the diary or nil.
157 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
158 scope of variables NAME, YEARS and SUFFIX (among others)."
160 :group
'org-bbdb-anniversaries
163 (defcustom org-bbdb-anniversary-field
'anniversary
164 "The BBDB field which contains anniversaries.
165 The anniversaries are stored in the following format
167 YYYY-MM-DD Class-or-Format-String
169 where class is one of the customized classes for anniversaries;
170 birthday and wedding are predefined. Format-String can take three
171 substitutions 1) the name of the record containing this
172 anniversary, 2) the number of years, and 3) an ordinal suffix for
175 Multiple anniversaries can be separated by \\n."
177 :group
'org-bbdb-anniversaries
180 (defcustom org-bbdb-extract-date-fun
'org-bbdb-anniv-extract-date
181 "How to retrieve `month date year' from the anniversary field.
183 Customize if you have already filled your BBDB with dates
184 different from YYYY-MM-DD. The function must return a list (month
187 :group
'org-bbdb-anniversaries
191 ;; Install the link type
192 (org-add-link-type "bbdb" 'org-bbdb-open
'org-bbdb-export
)
193 (add-hook 'org-store-link-functions
'org-bbdb-store-link
)
196 (defun org-bbdb-store-link ()
197 "Store a link to a BBDB database entry."
198 (when (eq major-mode
'bbdb-mode
)
199 ;; This is BBDB, we make this link!
200 (let* ((name (bbdb-record-name (bbdb-current-record)))
201 (company (bbdb-record-getprop (bbdb-current-record) 'company
))
202 (link (org-make-link "bbdb:" name
)))
203 (org-store-link-props :type
"bbdb" :name name
:company company
204 :link link
:description name
)
207 (defun org-bbdb-export (path desc format
)
208 "Create the export version of a BBDB link specified by PATH or DESC.
209 If exporting to either HTML or LaTeX FORMAT the link will be
210 italicized, in all other cases it is left unchanged."
211 (when (string= desc
(format "bbdb:%s" path
))
214 ((eq format
'html
) (format "<i>%s</i>" desc
))
215 ((eq format
'latex
) (format "\\textit{%s}" desc
))
218 (defun org-bbdb-open (name)
219 "Follow a BBDB link to NAME."
221 (let ((inhibit-redisplay (not debug-on-error
))
222 (bbdb-electric-p nil
))
224 ;; Exact match on name
225 (bbdb-name (concat "\\`" name
"\\'") nil
)
226 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
227 ;; Exact match on name
228 (bbdb-company (concat "\\`" name
"\\'") nil
)
229 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
230 ;; Partial match on name
232 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
233 ;; Partial match on company
234 (bbdb-company name nil
)
235 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
236 ;; General match including network address and notes
238 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
239 (delete-window (get-buffer-window "*BBDB*"))
240 (error "No matching BBDB record")))))
242 (defun org-bbdb-anniv-extract-date (time-str)
243 "Convert YYYY-MM-DD to (month date year).
244 Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted
245 it will be considered unknown."
246 (multiple-value-bind (a b c
) (values-list (bbdb-split time-str
"-"))
248 (list (string-to-number a
)
251 (list (string-to-number b
)
253 (string-to-number a
)))))
255 (defun org-bbdb-anniv-split (str)
256 "Split multiple entries in the BBDB anniversary field.
257 Argument STR is the anniversary field in BBDB."
258 (let ((pos (string-match "[ \t]" str
)))
259 (if pos
(list (substring str
0 pos
)
260 (bbdb-string-trim (substring str pos
)))
263 (defvar org-bbdb-anniv-hash nil
264 "A hash holding anniversaries extracted from BBDB.
265 The hash table is created on first use.")
267 (defvar org-bbdb-updated-p t
268 "This is non-nil if BBDB has been updated since we last built the hash.")
270 (defun org-bbdb-make-anniv-hash ()
271 "Create a hash with anniversaries extracted from BBDB, for fast access.
272 The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
274 (let (split tmp annivs
)
275 (clrhash org-bbdb-anniv-hash
)
276 (dolist (rec (bbdb-records))
277 (when (setq annivs
(bbdb-record-getprop
278 rec org-bbdb-anniversary-field
))
279 (setq annivs
(bbdb-split annivs
"\n"))
281 (setq split
(org-bbdb-anniv-split (pop annivs
)))
282 (multiple-value-bind (m d y
)
283 (values-list (funcall org-bbdb-extract-date-fun
(car split
)))
284 (setq tmp
(gethash (list m d
) org-bbdb-anniv-hash
))
285 (puthash (list m d
) (cons (list y
286 (bbdb-record-name rec
)
289 org-bbdb-anniv-hash
))))))
290 (setq org-bbdb-updated-p nil
))
292 (defun org-bbdb-updated (rec)
293 "Record the fact that BBDB has been updated.
294 This is used by Org to re-create the anniversary hash table."
295 (setq org-bbdb-updated-p t
))
297 (add-hook 'bbdb-after-change-hook
'org-bbdb-updated
)
300 (defun org-bbdb-anniversaries()
301 "Extract anniversaries from BBDB for display in the agenda."
304 (unless (hash-table-p org-bbdb-anniv-hash
)
305 (setq org-bbdb-anniv-hash
306 (make-hash-table :test
'equal
:size
366)))
308 (when (or org-bbdb-updated-p
309 (= 0 (hash-table-count org-bbdb-anniv-hash
)))
310 (org-bbdb-make-anniv-hash))
312 (let* ((m (car date
)) ; month
313 (d (nth 1 date
)) ; day
314 (y (nth 2 date
)) ; year
315 (annivs (gethash (list m d
) org-bbdb-anniv-hash
))
319 ;; we don't want to miss people born on Feb. 29th
320 (when (and (= m
3) (= d
1)
321 (not (null (gethash (list 2 29) org-bbdb-anniv-hash
)))
322 (not (calendar-leap-year-p y
)))
323 (setq recs
(gethash (list 2 29) org-bbdb-anniv-hash
))
324 (while (setq rec
(pop recs
))
328 (while (setq rec
(pop annivs
))
330 (let* ((class (or (nth 2 rec
)
331 org-bbdb-default-anniversary-format
))
332 (form (or (cdr (assoc-string
333 class org-bbdb-anniversary-format-alist t
))
334 class
)) ; (as format string)
336 (years (if (eq (car rec
) nil
)
339 (suffix (if (eq (car rec
) nil
)
341 (diary-ordinal-suffix years
)))
344 (funcall form name years suffix
))
345 ((listp form
) (eval form
))
346 (t (format form name years suffix
)))))
347 (org-add-props tmp nil
'org-bbdb-name name
)
349 (setq text
(append text
(list tmp
)))
350 (setq text
(list tmp
)))))
354 (defun org-bbdb-complete-link ()
355 "Read a bbdb link with name completion."
358 (bbdb-record-name (car (bbdb-completing-read-record "Name: ")))))
360 (defun org-bbdb-anniv-export-ical ()
361 "Extract anniversaries from BBDB and convert them to icalendar format."
364 (unless (hash-table-p org-bbdb-anniv-hash
)
365 (setq org-bbdb-anniv-hash
366 (make-hash-table :test
'equal
:size
366)))
367 (when (or org-bbdb-updated-p
368 (= 0 (hash-table-count org-bbdb-anniv-hash
)))
369 (org-bbdb-make-anniv-hash))
370 (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash
))
372 (defun org-bbdb-format-vevent (key recs
)
374 (while (setq rec
(pop recs
))
375 (setq categ
(or (nth 2 rec
) org-bbdb-default-anniversary-format
))
376 (princ (format "BEGIN:VEVENT
377 UID: ANNIV-%4i%02i%02i-%s
384 (nth 0 rec
) (nth 0 key
) (nth 1 key
)
386 (org-split-string (nth 1 rec
) "[^a-zA-Z0-90]+")
388 (nth 0 rec
) (nth 0 key
) (nth 1 key
)
390 (concat (capitalize categ
) " " (nth 1 rec
))
395 ;; arch-tag: 9e4f275d-d080-48c1-b040-62247f66b5c2
397 ;;; org-bbdb.el ends here