Checked anti.texi, errors.texi, and maps.texi.
[emacs.git] / lisp / org / org-bbdb.el
blobf9fe216082f9c30c0fc35e773036b892575f8534
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.21b
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 ;; The anniversaries are stored in BBDB in the field `anniversary'
53 ;; in the format
55 ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}*
56 ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}*
58 ;; CLASS-OR-FORMAT-STRING is one of two things:
60 ;; * an identifier for a class of anniversaries (eg. birthday or
61 ;; wedding) from `org-bbdb-anniversary-format-alist'.
62 ;; * the (format) string displayed in the diary.
64 ;; It defaults to the value of `org-bbdb-default-anniversary-format'
65 ;; ("birthday" by default).
67 ;; The substitutions in the format string are (in order):
68 ;; * the name of the record containing this anniversary
69 ;; * the number of years
70 ;; * an ordinal suffix (st, nd, rd, th) for the year
72 ;; See the documentation of `org-bbdb-anniversary-format-alist' for
73 ;; further options.
75 ;; Example
77 ;; 1973-06-22
78 ;; 20??-??-?? wedding
79 ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
81 ;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB
82 ;; link from which the entry at point originates.
84 ;;; Code:
86 (require 'org)
87 (eval-when-compile
88 (require 'cl))
90 ;; Declare external functions and variables
92 (declare-function bbdb "ext:bbdb-com" (string elidep))
93 (declare-function bbdb-company "ext:bbdb-com" (string elidep))
94 (declare-function bbdb-current-record "ext:bbdb-com"
95 (&optional planning-on-modifying))
96 (declare-function bbdb-name "ext:bbdb-com" (string elidep))
97 (declare-function bbdb-record-getprop "ext:bbdb" (record property))
98 (declare-function bbdb-record-name "ext:bbdb" (record))
99 (declare-function bbdb-records "ext:bbdb"
100 (&optional dont-check-disk already-in-db-buffer))
101 (declare-function bbdb-split "ext:bbdb" (string separators))
102 (declare-function bbdb-string-trim "ext:bbdb" (string))
103 (declare-function calendar-leap-year-p "calendar" (year))
104 (declare-function diary-ordinal-suffix "diary-lib" (n))
106 (defvar date) ;; dynamically scoped from Org
108 ;; Customization
110 (defgroup org-bbdb-anniversaries nil
111 "Customizations for including anniversaries from BBDB into Agenda."
112 :group 'org-bbdb)
114 (defcustom org-bbdb-default-anniversary-format "birthday"
115 "Default anniversary class."
116 :type 'string
117 :group 'org-bbdb-anniversaries
118 :require 'bbdb)
120 (defcustom org-bbdb-anniversary-format-alist
121 '(("birthday" lambda
122 (name years suffix)
123 (concat "Birthday: [[bbdb:" name "][" name " ("
124 (number-to-string years)
125 suffix ")]]"))
126 ("wedding" lambda
127 (name years suffix)
128 (concat "[[bbdb:" name "][" name "'s "
129 (number-to-string years)
130 suffix " wedding anniversary]]")))
131 "How different types of anniversaries should be formatted.
132 An alist of elements (STRING . FORMAT) where STRING is the name of an
133 anniversary class and format is either:
134 1) A format string with the following substitutions (in order):
135 * the name of the record containing this anniversary
136 * the number of years
137 * an ordinal suffix (st, nd, rd, th) for the year
139 2) A function to be called with three arguments: NAME YEARS SUFFIX
140 (string int string) returning a string for the diary or nil.
142 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
143 scope of variables NAME, YEARS and SUFFIX (among others)."
144 :type 'sexp
145 :group 'org-bbdb-anniversaries
146 :require 'bbdb)
148 (defcustom org-bbdb-anniversary-field 'anniversary
149 "The BBDB field which contains anniversaries.
150 The anniversaries are stored in the following format
152 YYYY-MM-DD Class-or-Format-String
154 where class is one of the customized classes for anniversaries;
155 birthday and wedding are predefined. Format-String can take three
156 substitutions 1) the name of the record containing this
157 anniversary, 2) the number of years, and 3) an ordinal suffix for
158 the year.
160 Multiple anniversaries can be separated by \\n."
161 :type 'symbol
162 :group 'org-bbdb-anniversaries
163 :require 'bbdb)
165 (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
166 "How to retrieve `month date year' from the anniversary field.
168 Customize if you have already filled your BBDB with dates
169 different from YYYY-MM-DD. The function must return a list (month
170 date year)."
171 :type 'function
172 :group 'org-bbdb-anniversaries
173 :require 'bbdb)
176 ;; Install the link type
177 (org-add-link-type "bbdb" 'org-bbdb-open 'org-bbdb-export)
178 (add-hook 'org-store-link-functions 'org-bbdb-store-link)
180 ;; Implementation
181 (defun org-bbdb-store-link ()
182 "Store a link to a BBDB database entry."
183 (when (eq major-mode 'bbdb-mode)
184 ;; This is BBDB, we make this link!
185 (let* ((name (bbdb-record-name (bbdb-current-record)))
186 (company (bbdb-record-getprop (bbdb-current-record) 'company))
187 (link (org-make-link "bbdb:" name)))
188 (org-store-link-props :type "bbdb" :name name :company company
189 :link link :description name)
190 link)))
192 (defun org-bbdb-export (path desc format)
193 "Create the export version of a BBDB link specified by PATH or DESC.
194 If exporting to either HTML or LaTeX FORMAT the link will be
195 italicised, in all other cases it is left unchanged."
196 (cond
197 ((eq format 'html) (format "<i>%s</i>" (or desc path)))
198 ((eq format 'latex) (format "\\textit{%s}" (or desc path)))
199 (t (or desc path))))
201 (defun org-bbdb-open (name)
202 "Follow a BBDB link to NAME."
203 (require 'bbdb)
204 (let ((inhibit-redisplay (not debug-on-error))
205 (bbdb-electric-p nil))
206 (catch 'exit
207 ;; Exact match on name
208 (bbdb-name (concat "\\`" name "\\'") nil)
209 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
210 ;; Exact match on name
211 (bbdb-company (concat "\\`" name "\\'") nil)
212 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
213 ;; Partial match on name
214 (bbdb-name name nil)
215 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
216 ;; Partial match on company
217 (bbdb-company name nil)
218 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
219 ;; General match including network address and notes
220 (bbdb name nil)
221 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
222 (delete-window (get-buffer-window "*BBDB*"))
223 (error "No matching BBDB record")))))
225 (defun org-bbdb-anniv-extract-date (time-str)
226 "Convert YYYY-MM-DD to (month date year).
227 Argument TIME-STR is the value retrieved from BBDB."
228 (multiple-value-bind (y m d) (values-list (bbdb-split time-str "-"))
229 (list (string-to-number m)
230 (string-to-number d)
231 (string-to-number y))))
233 (defun org-bbdb-anniv-split (str)
234 "Split multiple entries in the BBDB anniversary field.
235 Argument STR is the anniversary field in BBDB."
236 (let ((pos (string-match "[ \t]" str)))
237 (if pos (list (substring str 0 pos)
238 (bbdb-string-trim (substring str pos)))
239 (list str nil))))
241 (defvar org-bbdb-anniv-hash nil
242 "A hash holding anniversaries extracted from BBDB.
243 The hash table is created on first use.")
245 (defvar org-bbdb-updated-p t
246 "This is non-nil if BBDB has been updated since we last built the hash.")
248 (defun org-bbdb-make-anniv-hash ()
249 "Create a hash with anniversaries extracted from BBDB, for fast access.
250 The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
252 (let (split tmp annivs)
253 (clrhash org-bbdb-anniv-hash)
254 (dolist (rec (bbdb-records))
255 (when (setq annivs (bbdb-record-getprop
256 rec org-bbdb-anniversary-field))
257 (setq annivs (bbdb-split annivs "\n"))
258 (while annivs
259 (setq split (org-bbdb-anniv-split (pop annivs)))
260 (multiple-value-bind (m d y)
261 (values-list (funcall org-bbdb-extract-date-fun (car split)))
262 (setq tmp (gethash (list m d) org-bbdb-anniv-hash))
263 (puthash (list m d) (cons (list y
264 (bbdb-record-name rec)
265 (cadr split))
266 tmp)
267 org-bbdb-anniv-hash))))))
268 (setq org-bbdb-updated-p nil))
270 (defun org-bbdb-updated (rec)
271 "Record the fact that BBDB has been updated.
272 This is used by Org to re-create the anniversary hash table."
273 (setq org-bbdb-updated-p t))
275 (add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
277 ;;;###autoload
278 (defun org-bbdb-anniversaries()
279 "Extract anniversaries from BBDB for display in the agenda."
280 (require 'bbdb)
281 (require 'diary-lib)
282 (unless (hash-table-p org-bbdb-anniv-hash)
283 (setq org-bbdb-anniv-hash
284 (make-hash-table :test 'equal :size 366)))
286 (when (or org-bbdb-updated-p
287 (= 0 (hash-table-count org-bbdb-anniv-hash)))
288 (org-bbdb-make-anniv-hash))
290 (let* ((m (car date)) ; month
291 (d (nth 1 date)) ; day
292 (y (nth 2 date)) ; year
293 (annivs (gethash (list m d) org-bbdb-anniv-hash))
294 (text ())
295 rec recs)
297 ;; we don't want to miss people born on Feb. 29th
298 (when (and (= m 3) (= d 1)
299 (not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
300 (not (calendar-leap-year-p y)))
301 (setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
302 (while (setq rec (pop recs))
303 (push rec annivs)))
305 (when annivs
306 (while (setq rec (pop annivs))
307 (when rec
308 (let* ((class (or (nth 2 rec)
309 org-bbdb-default-anniversary-format))
310 (form (or (cdr (assoc class
311 org-bbdb-anniversary-format-alist))
312 class)) ; (as format string)
313 (name (nth 1 rec))
314 (years (- y (car rec)))
315 (suffix (diary-ordinal-suffix years))
316 (tmp (cond
317 ((functionp form)
318 (funcall form name years suffix))
319 ((listp form) (eval form))
320 (t (format form name years suffix)))))
321 (org-add-props tmp nil 'org-bbdb-name name)
322 (if text
323 (setq text (append text (list tmp)))
324 (setq text (list tmp)))))
326 (when text
327 (mapconcat 'identity text "; "))))
329 (provide 'org-bbdb)
331 ;; arch-tag: 9e4f275d-d080-48c1-b040-62247f66b5c2
333 ;;; org-bbdb.el ends here