1 ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>,
6 ;; Thomas Baumann <thomas dot baumann at ch dot tum dot de>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file implements links to BBDB database entries from within Org-mode.
30 ;; Org-mode loads this module by default - if this is not what you want,
31 ;; configure the variable `org-modules'.
33 ;; It also implements an interface (based on Ivar Rummelhoff's
34 ;; bbdb-anniv.el) for those org-mode users, who do not use the diary
35 ;; but who do want to include the anniversaries stored in the BBDB
36 ;; into the org-agenda. If you already include the `diary' into the
37 ;; agenda, you might want to prefer to include the anniversaries in
38 ;; the diary using bbdb-anniv.el.
40 ;; Put the following in /somewhere/at/home/diary.org and make sure
41 ;; that this file is in `org-agenda-files`
43 ;; %%(org-bbdb-anniversaries)
45 ;; For example my diary.org looks like:
48 ;; %%(org-bbdb-anniversaries)
51 ;; To add an anniversary to a BBDB record, press `C-o' in the record.
52 ;; You will be prompted for the field name, in this case it must be
53 ;; "anniversary". If this is the first time you are using this field,
54 ;; you need to confirm that it should be created.
56 ;; The format of an anniversary field stored in BBDB is the following
57 ;; (items in {} are optional):
59 ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}
60 ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}...
62 ;; CLASS-OR-FORMAT-STRING is one of two things:
64 ;; - an identifier for a class of anniversaries (eg. birthday or
65 ;; wedding) from `org-bbdb-anniversary-format-alist' which then
66 ;; defines the format string for this class
67 ;; - the (format) string displayed in the diary.
69 ;; You can enter multiple anniversaries for a single BBDB record by
70 ;; separating them with a newline character. At the BBDB prompt for
71 ;; the field value, type `C-q C-j' to enter a newline between two
74 ;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the
75 ;; value of `org-bbdb-default-anniversary-format' ("birthday" by
78 ;; The substitutions in the format string are (in order):
79 ;; - the name of the record containing this anniversary
80 ;; - the number of years
81 ;; - an ordinal suffix (st, nd, rd, th) for the year
83 ;; See the documentation of `org-bbdb-anniversary-format-alist' for
90 ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
92 ;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB
93 ;; link from which the entry at point originates.
101 ;; Declare external functions and variables
103 (declare-function bbdb
"ext:bbdb-com" (string elidep
))
104 (declare-function bbdb-company
"ext:bbdb-com" (string elidep
))
105 (declare-function bbdb-current-record
"ext:bbdb-com"
106 (&optional planning-on-modifying
))
107 (declare-function bbdb-name
"ext:bbdb-com" (string elidep
))
108 (declare-function bbdb-completing-read-record
"ext:bbdb-com"
109 (prompt &optional omit-records
))
110 (declare-function bbdb-record-getprop
"ext:bbdb" (record property
))
111 (declare-function bbdb-record-name
"ext:bbdb" (record))
112 (declare-function bbdb-records
"ext:bbdb"
113 (&optional dont-check-disk already-in-db-buffer
))
114 (declare-function bbdb-split
"ext:bbdb" (string separators
))
115 (declare-function bbdb-string-trim
"ext:bbdb" (string))
117 (declare-function calendar-leap-year-p
"calendar" (year))
118 (declare-function diary-ordinal-suffix
"diary-lib" (n))
120 (defvar date
) ;; dynamically scoped from Org
124 (defgroup org-bbdb-anniversaries nil
125 "Customizations for including anniversaries from BBDB into Agenda."
128 (defcustom org-bbdb-default-anniversary-format
"birthday"
129 "Default anniversary class."
131 :group
'org-bbdb-anniversaries
134 (defcustom org-bbdb-anniversary-format-alist
137 (concat "Birthday: [[bbdb:" name
"][" name
" ("
138 (format "%s" years
) ; handles numbers as well as strings
142 (concat "[[bbdb:" name
"][" name
"'s "
144 suffix
" wedding anniversary]]")))
145 "How different types of anniversaries should be formatted.
146 An alist of elements (STRING . FORMAT) where STRING is the name of an
147 anniversary class and format is either:
148 1) A format string with the following substitutions (in order):
149 * the name of the record containing this anniversary
150 * the number of years
151 * an ordinal suffix (st, nd, rd, th) for the year
153 2) A function to be called with three arguments: NAME YEARS SUFFIX
154 (string int string) returning a string for the diary or nil.
156 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
157 scope of variables NAME, YEARS and SUFFIX (among others)."
159 :group
'org-bbdb-anniversaries
162 (defcustom org-bbdb-anniversary-field
'anniversary
163 "The BBDB field which contains anniversaries.
164 The anniversaries are stored in the following format
166 YYYY-MM-DD Class-or-Format-String
168 where class is one of the customized classes for anniversaries;
169 birthday and wedding are predefined. Format-String can take three
170 substitutions 1) the name of the record containing this
171 anniversary, 2) the number of years, and 3) an ordinal suffix for
174 Multiple anniversaries can be separated by \\n."
176 :group
'org-bbdb-anniversaries
179 (defcustom org-bbdb-extract-date-fun
'org-bbdb-anniv-extract-date
180 "How to retrieve `month date year' from the anniversary field.
182 Customize if you have already filled your BBDB with dates
183 different from YYYY-MM-DD. The function must return a list (month
186 :group
'org-bbdb-anniversaries
190 ;; Install the link type
191 (org-add-link-type "bbdb" 'org-bbdb-open
'org-bbdb-export
)
192 (add-hook 'org-store-link-functions
'org-bbdb-store-link
)
195 (defun org-bbdb-store-link ()
196 "Store a link to a BBDB database entry."
197 (when (eq major-mode
'bbdb-mode
)
198 ;; This is BBDB, we make this link!
199 (let* ((name (bbdb-record-name (bbdb-current-record)))
200 (company (bbdb-record-getprop (bbdb-current-record) 'company
))
201 (link (org-make-link "bbdb:" name
)))
202 (org-store-link-props :type
"bbdb" :name name
:company company
203 :link link
:description name
)
206 (defun org-bbdb-export (path desc format
)
207 "Create the export version of a BBDB link specified by PATH or DESC.
208 If exporting to either HTML or LaTeX FORMAT the link will be
209 italicized, in all other cases it is left unchanged."
210 (when (string= desc
(format "bbdb:%s" path
))
213 ((eq format
'html
) (format "<i>%s</i>" desc
))
214 ((eq format
'latex
) (format "\\textit{%s}" desc
))
217 (defun org-bbdb-open (name)
218 "Follow a BBDB link to NAME."
220 (let ((inhibit-redisplay (not debug-on-error
))
221 (bbdb-electric-p nil
))
223 ;; Exact match on name
224 (bbdb-name (concat "\\`" name
"\\'") nil
)
225 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
226 ;; Exact match on name
227 (bbdb-company (concat "\\`" name
"\\'") nil
)
228 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
229 ;; Partial match on name
231 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
232 ;; Partial match on company
233 (bbdb-company name nil
)
234 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
235 ;; General match including network address and notes
237 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
238 (delete-window (get-buffer-window "*BBDB*"))
239 (error "No matching BBDB record")))))
241 (defun org-bbdb-anniv-extract-date (time-str)
242 "Convert YYYY-MM-DD to (month date year).
243 Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted
244 it will be considered unknown."
245 (multiple-value-bind (a b c
) (values-list (bbdb-split time-str
"-"))
247 (list (string-to-number a
)
250 (list (string-to-number b
)
252 (string-to-number a
)))))
254 (defun org-bbdb-anniv-split (str)
255 "Split multiple entries in the BBDB anniversary field.
256 Argument STR is the anniversary field in BBDB."
257 (let ((pos (string-match "[ \t]" str
)))
258 (if pos
(list (substring str
0 pos
)
259 (bbdb-string-trim (substring str pos
)))
262 (defvar org-bbdb-anniv-hash nil
263 "A hash holding anniversaries extracted from BBDB.
264 The hash table is created on first use.")
266 (defvar org-bbdb-updated-p t
267 "This is non-nil if BBDB has been updated since we last built the hash.")
269 (defun org-bbdb-make-anniv-hash ()
270 "Create a hash with anniversaries extracted from BBDB, for fast access.
271 The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
273 (let (split tmp annivs
)
274 (clrhash org-bbdb-anniv-hash
)
275 (dolist (rec (bbdb-records))
276 (when (setq annivs
(bbdb-record-getprop
277 rec org-bbdb-anniversary-field
))
278 (setq annivs
(bbdb-split annivs
"\n"))
280 (setq split
(org-bbdb-anniv-split (pop annivs
)))
281 (multiple-value-bind (m d y
)
282 (values-list (funcall org-bbdb-extract-date-fun
(car split
)))
283 (setq tmp
(gethash (list m d
) org-bbdb-anniv-hash
))
284 (puthash (list m d
) (cons (list y
285 (bbdb-record-name rec
)
288 org-bbdb-anniv-hash
))))))
289 (setq org-bbdb-updated-p nil
))
291 (defun org-bbdb-updated (rec)
292 "Record the fact that BBDB has been updated.
293 This is used by Org to re-create the anniversary hash table."
294 (setq org-bbdb-updated-p t
))
296 (add-hook 'bbdb-after-change-hook
'org-bbdb-updated
)
299 (defun org-bbdb-anniversaries()
300 "Extract anniversaries from BBDB for display in the agenda."
303 (unless (hash-table-p org-bbdb-anniv-hash
)
304 (setq org-bbdb-anniv-hash
305 (make-hash-table :test
'equal
:size
366)))
307 (when (or org-bbdb-updated-p
308 (= 0 (hash-table-count org-bbdb-anniv-hash
)))
309 (org-bbdb-make-anniv-hash))
311 (let* ((m (car date
)) ; month
312 (d (nth 1 date
)) ; day
313 (y (nth 2 date
)) ; year
314 (annivs (gethash (list m d
) org-bbdb-anniv-hash
))
318 ;; we don't want to miss people born on Feb. 29th
319 (when (and (= m
3) (= d
1)
320 (not (null (gethash (list 2 29) org-bbdb-anniv-hash
)))
321 (not (calendar-leap-year-p y
)))
322 (setq recs
(gethash (list 2 29) org-bbdb-anniv-hash
))
323 (while (setq rec
(pop recs
))
327 (while (setq rec
(pop annivs
))
329 (let* ((class (or (nth 2 rec
)
330 org-bbdb-default-anniversary-format
))
331 (form (or (cdr (assoc-string
332 class org-bbdb-anniversary-format-alist t
))
333 class
)) ; (as format string)
335 (years (if (eq (car rec
) nil
)
338 (suffix (if (eq (car rec
) nil
)
340 (diary-ordinal-suffix years
)))
343 (funcall form name years suffix
))
344 ((listp form
) (eval form
))
345 (t (format form name years suffix
)))))
346 (org-add-props tmp nil
'org-bbdb-name name
)
348 (setq text
(append text
(list tmp
)))
349 (setq text
(list tmp
)))))
353 (defun org-bbdb-complete-link ()
354 "Read a bbdb link with name completion."
357 (bbdb-record-name (car (bbdb-completing-read-record "Name: ")))))
359 (defun org-bbdb-anniv-export-ical ()
360 "Extract anniversaries from BBDB and convert them to icalendar format."
363 (unless (hash-table-p org-bbdb-anniv-hash
)
364 (setq org-bbdb-anniv-hash
365 (make-hash-table :test
'equal
:size
366)))
366 (when (or org-bbdb-updated-p
367 (= 0 (hash-table-count org-bbdb-anniv-hash
)))
368 (org-bbdb-make-anniv-hash))
369 (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash
))
371 (defun org-bbdb-format-vevent (key recs
)
373 (while (setq rec
(pop recs
))
374 (setq categ
(or (nth 2 rec
) org-bbdb-default-anniversary-format
))
375 (princ (format "BEGIN:VEVENT
376 UID: ANNIV-%4i%02i%02i-%s
383 (nth 0 rec
) (nth 0 key
) (nth 1 key
)
385 (org-split-string (nth 1 rec
) "[^a-zA-Z0-90]+")
387 (nth 0 rec
) (nth 0 key
) (nth 1 key
)
389 (concat (capitalize categ
) " " (nth 1 rec
))
396 ;;; org-bbdb.el ends here