]> code.delx.au - gnu-emacs/blob - lisp/org/org-bbdb.el
Merge from emacs-23; up to 2010-06-03T22:16:02Z!dann@ics.uci.edu
[gnu-emacs] / lisp / org / org-bbdb.el
1 ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
2
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
4
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
9 ;; Version: 7.4
10 ;;
11 ;; This file is part of GNU Emacs.
12 ;;
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.
17
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.
22
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;
27 ;;; Commentary:
28
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'.
32
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.
39 ;;
40 ;; Put the following in /somewhere/at/home/diary.org and make sure
41 ;; that this file is in `org-agenda-files`
42 ;;
43 ;; %%(org-bbdb-anniversaries)
44 ;;
45 ;; For example my diary.org looks like:
46 ;; * Anniversaries
47 ;; #+CATEGORY: Anniv
48 ;; %%(org-bbdb-anniversaries)
49 ;;
50 ;;
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.
55 ;;
56 ;; The format of an anniversary field stored in BBDB is the following
57 ;; (items in {} are optional):
58 ;;
59 ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}
60 ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}...
61 ;;
62 ;; CLASS-OR-FORMAT-STRING is one of two things:
63 ;;
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.
68 ;;
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
72 ;; anniversaries.
73 ;;
74 ;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the
75 ;; value of `org-bbdb-default-anniversary-format' ("birthday" by
76 ;; default).
77 ;;
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
82 ;;
83 ;; See the documentation of `org-bbdb-anniversary-format-alist' for
84 ;; further options.
85 ;;
86 ;; Example
87 ;;
88 ;; 1973-06-22
89 ;; 20??-??-?? wedding
90 ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
91 ;;
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.
94 ;;
95 ;;; Code:
96
97 (require 'org)
98 (eval-when-compile
99 (require 'cl))
100
101 ;; Declare external functions and variables
102
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))
116
117 (declare-function calendar-leap-year-p "calendar" (year))
118 (declare-function diary-ordinal-suffix "diary-lib" (n))
119
120 (defvar date) ;; dynamically scoped from Org
121
122 ;; Customization
123
124 (defgroup org-bbdb-anniversaries nil
125 "Customizations for including anniversaries from BBDB into Agenda."
126 :group 'org-bbdb)
127
128 (defcustom org-bbdb-default-anniversary-format "birthday"
129 "Default anniversary class."
130 :type 'string
131 :group 'org-bbdb-anniversaries
132 :require 'bbdb)
133
134 (defcustom org-bbdb-anniversary-format-alist
135 '(("birthday" lambda
136 (name years suffix)
137 (concat "Birthday: [[bbdb:" name "][" name " ("
138 (number-to-string years)
139 suffix ")]]"))
140 ("wedding" lambda
141 (name years suffix)
142 (concat "[[bbdb:" name "][" name "'s "
143 (number-to-string years)
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
152
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.
155
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)."
158 :type 'sexp
159 :group 'org-bbdb-anniversaries
160 :require 'bbdb)
161
162 (defcustom org-bbdb-anniversary-field 'anniversary
163 "The BBDB field which contains anniversaries.
164 The anniversaries are stored in the following format
165
166 YYYY-MM-DD Class-or-Format-String
167
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
172 the year.
173
174 Multiple anniversaries can be separated by \\n."
175 :type 'symbol
176 :group 'org-bbdb-anniversaries
177 :require 'bbdb)
178
179 (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
180 "How to retrieve `month date year' from the anniversary field.
181
182 Customize if you have already filled your BBDB with dates
183 different from YYYY-MM-DD. The function must return a list (month
184 date year)."
185 :type 'function
186 :group 'org-bbdb-anniversaries
187 :require 'bbdb)
188
189
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)
193
194 ;; Implementation
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)
204 link)))
205
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 (cond
211 ((eq format 'html) (format "<i>%s</i>" (or desc path)))
212 ((eq format 'latex) (format "\\textit{%s}" (or desc path)))
213 (t (or desc path))))
214
215 (defun org-bbdb-open (name)
216 "Follow a BBDB link to NAME."
217 (require 'bbdb)
218 (let ((inhibit-redisplay (not debug-on-error))
219 (bbdb-electric-p nil))
220 (catch 'exit
221 ;; Exact match on name
222 (bbdb-name (concat "\\`" name "\\'") nil)
223 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
224 ;; Exact match on name
225 (bbdb-company (concat "\\`" name "\\'") nil)
226 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
227 ;; Partial match on name
228 (bbdb-name name nil)
229 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
230 ;; Partial match on company
231 (bbdb-company name nil)
232 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
233 ;; General match including network address and notes
234 (bbdb name nil)
235 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
236 (delete-window (get-buffer-window "*BBDB*"))
237 (error "No matching BBDB record")))))
238
239 (defun org-bbdb-anniv-extract-date (time-str)
240 "Convert YYYY-MM-DD to (month date year).
241 Argument TIME-STR is the value retrieved from BBDB."
242 (multiple-value-bind (y m d) (values-list (bbdb-split time-str "-"))
243 (list (string-to-number m)
244 (string-to-number d)
245 (string-to-number y))))
246
247 (defun org-bbdb-anniv-split (str)
248 "Split multiple entries in the BBDB anniversary field.
249 Argument STR is the anniversary field in BBDB."
250 (let ((pos (string-match "[ \t]" str)))
251 (if pos (list (substring str 0 pos)
252 (bbdb-string-trim (substring str pos)))
253 (list str nil))))
254
255 (defvar org-bbdb-anniv-hash nil
256 "A hash holding anniversaries extracted from BBDB.
257 The hash table is created on first use.")
258
259 (defvar org-bbdb-updated-p t
260 "This is non-nil if BBDB has been updated since we last built the hash.")
261
262 (defun org-bbdb-make-anniv-hash ()
263 "Create a hash with anniversaries extracted from BBDB, for fast access.
264 The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
265
266 (let (split tmp annivs)
267 (clrhash org-bbdb-anniv-hash)
268 (dolist (rec (bbdb-records))
269 (when (setq annivs (bbdb-record-getprop
270 rec org-bbdb-anniversary-field))
271 (setq annivs (bbdb-split annivs "\n"))
272 (while annivs
273 (setq split (org-bbdb-anniv-split (pop annivs)))
274 (multiple-value-bind (m d y)
275 (values-list (funcall org-bbdb-extract-date-fun (car split)))
276 (setq tmp (gethash (list m d) org-bbdb-anniv-hash))
277 (puthash (list m d) (cons (list y
278 (bbdb-record-name rec)
279 (cadr split))
280 tmp)
281 org-bbdb-anniv-hash))))))
282 (setq org-bbdb-updated-p nil))
283
284 (defun org-bbdb-updated (rec)
285 "Record the fact that BBDB has been updated.
286 This is used by Org to re-create the anniversary hash table."
287 (setq org-bbdb-updated-p t))
288
289 (add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
290
291 ;;;###autoload
292 (defun org-bbdb-anniversaries()
293 "Extract anniversaries from BBDB for display in the agenda."
294 (require 'bbdb)
295 (require 'diary-lib)
296 (unless (hash-table-p org-bbdb-anniv-hash)
297 (setq org-bbdb-anniv-hash
298 (make-hash-table :test 'equal :size 366)))
299
300 (when (or org-bbdb-updated-p
301 (= 0 (hash-table-count org-bbdb-anniv-hash)))
302 (org-bbdb-make-anniv-hash))
303
304 (let* ((m (car date)) ; month
305 (d (nth 1 date)) ; day
306 (y (nth 2 date)) ; year
307 (annivs (gethash (list m d) org-bbdb-anniv-hash))
308 (text ())
309 rec recs)
310
311 ;; we don't want to miss people born on Feb. 29th
312 (when (and (= m 3) (= d 1)
313 (not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
314 (not (calendar-leap-year-p y)))
315 (setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
316 (while (setq rec (pop recs))
317 (push rec annivs)))
318
319 (when annivs
320 (while (setq rec (pop annivs))
321 (when rec
322 (let* ((class (or (nth 2 rec)
323 org-bbdb-default-anniversary-format))
324 (form (or (cdr (assoc-string
325 class org-bbdb-anniversary-format-alist t))
326 class)) ; (as format string)
327 (name (nth 1 rec))
328 (years (- y (car rec)))
329 (suffix (diary-ordinal-suffix years))
330 (tmp (cond
331 ((functionp form)
332 (funcall form name years suffix))
333 ((listp form) (eval form))
334 (t (format form name years suffix)))))
335 (org-add-props tmp nil 'org-bbdb-name name)
336 (if text
337 (setq text (append text (list tmp)))
338 (setq text (list tmp)))))
339 ))
340 text))
341
342 (defun org-bbdb-complete-link ()
343 "Read a bbdb link with name completion."
344 (require 'bbdb-com)
345 (concat "bbdb:"
346 (bbdb-record-name (car (bbdb-completing-read-record "Name: ")))))
347
348 (defun org-bbdb-anniv-export-ical ()
349 "Extract anniversaries from BBDB and convert them to icalendar format."
350 (require 'bbdb)
351 (require 'diary-lib)
352 (unless (hash-table-p org-bbdb-anniv-hash)
353 (setq org-bbdb-anniv-hash
354 (make-hash-table :test 'equal :size 366)))
355 (when (or org-bbdb-updated-p
356 (= 0 (hash-table-count org-bbdb-anniv-hash)))
357 (org-bbdb-make-anniv-hash))
358 (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash))
359
360 (defun org-bbdb-format-vevent (key recs)
361 (let (rec categ)
362 (while (setq rec (pop recs))
363 (setq categ (or (nth 2 rec) org-bbdb-default-anniversary-format))
364 (princ (format "BEGIN:VEVENT
365 UID: ANNIV-%4i%02i%02i-%s
366 DTSTART:%4i%02i%02i
367 SUMMARY:%s
368 DESCRIPTION:%s
369 CATEGORIES:%s
370 RRULE:FREQ=YEARLY
371 END:VEVENT\n"
372 (nth 0 rec) (nth 0 key) (nth 1 key)
373 (mapconcat 'identity
374 (org-split-string (nth 1 rec) "[^a-zA-Z0-90]+")
375 "-")
376 (nth 0 rec) (nth 0 key) (nth 1 key)
377 (nth 1 rec)
378 (concat (capitalize categ) " " (nth 1 rec))
379 categ)))))
380
381 (provide 'org-bbdb)
382
383
384 ;;; org-bbdb.el ends here