Revert "Honour existing restrictions when visiting tasks from the agenda"
[org-mode.git] / lisp / org-bbdb.el
blob3df4c2044e962af0e454926e26747880d983cd46
1 ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
3 ;; Copyright (C) 2004-2012 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
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file implements links to BBDB database entries from within Org-mode.
29 ;; Org-mode loads this module by default - if this is not what you want,
30 ;; configure the variable `org-modules'.
32 ;; It also implements an interface (based on Ivar Rummelhoff's
33 ;; bbdb-anniv.el) for those org-mode users, who do not use the diary
34 ;; but who do want to include the anniversaries stored in the BBDB
35 ;; into the org-agenda. If you already include the `diary' into the
36 ;; agenda, you might want to prefer to include the anniversaries in
37 ;; the diary using bbdb-anniv.el.
39 ;; Put the following in /somewhere/at/home/diary.org and make sure
40 ;; that this file is in `org-agenda-files`
42 ;; %%(org-bbdb-anniversaries)
44 ;; For example my diary.org looks like:
45 ;; * Anniversaries
46 ;; #+CATEGORY: Anniv
47 ;; %%(org-bbdb-anniversaries)
50 ;; To add an anniversary to a BBDB record, press `C-o' in the record.
51 ;; You will be prompted for the field name, in this case it must be
52 ;; "anniversary". If this is the first time you are using this field,
53 ;; you need to confirm that it should be created.
55 ;; The format of an anniversary field stored in BBDB is the following
56 ;; (items in {} are optional):
58 ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}
59 ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}...
61 ;; CLASS-OR-FORMAT-STRING is one of two things:
63 ;; - an identifier for a class of anniversaries (eg. birthday or
64 ;; wedding) from `org-bbdb-anniversary-format-alist' which then
65 ;; defines the format string for this class
66 ;; - the (format) string displayed in the diary.
68 ;; You can enter multiple anniversaries for a single BBDB record by
69 ;; separating them with a newline character. At the BBDB prompt for
70 ;; the field value, type `C-q C-j' to enter a newline between two
71 ;; anniversaries.
73 ;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the
74 ;; value of `org-bbdb-default-anniversary-format' ("birthday" by
75 ;; default).
77 ;; The substitutions in the format string are (in order):
78 ;; - the name of the record containing this anniversary
79 ;; - the number of years
80 ;; - an ordinal suffix (st, nd, rd, th) for the year
82 ;; See the documentation of `org-bbdb-anniversary-format-alist' for
83 ;; further options.
85 ;; Example
87 ;; 1973-06-22
88 ;; 20??-??-?? wedding
89 ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
91 ;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB
92 ;; link from which the entry at point originates.
94 ;;; Code:
96 (require 'org)
97 (eval-when-compile
98 (require 'cl))
100 ;; Declare external functions and variables
102 (declare-function bbdb "ext:bbdb-com" (string elidep))
103 (declare-function bbdb-company "ext:bbdb-com" (string elidep))
104 (declare-function bbdb-current-record "ext:bbdb-com"
105 (&optional planning-on-modifying))
106 (declare-function bbdb-name "ext:bbdb-com" (string elidep))
107 (declare-function bbdb-completing-read-record "ext:bbdb-com"
108 (prompt &optional omit-records))
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 bbdb-record-get-field "ext:bbdb"
116 (fn file &optional arglist fileonly))
117 ;; These two functions below are part of BBDB3:
118 (declare-function bbdb-search-name "ext:bbdb" (regexp &optional layout))
119 (declare-function bbdb-search-organization "ext:bbdb" (regexp &optional layout))
121 (declare-function calendar-leap-year-p "calendar" (year))
122 (declare-function diary-ordinal-suffix "diary-lib" (n))
124 (defvar date) ;; dynamically scoped from Org
125 (defvar name) ;; dynamically scoped from Org
127 ;; Support for version 2.35
128 (defvar org-bbdb-old (fboundp 'bbdb-record-get-field-internal))
130 ;; Customization
132 (defgroup org-bbdb-anniversaries nil
133 "Customizations for including anniversaries from BBDB into Agenda."
134 :group 'org-bbdb)
136 (defcustom org-bbdb-default-anniversary-format "birthday"
137 "Default anniversary class."
138 :type 'string
139 :group 'org-bbdb-anniversaries
140 :require 'bbdb)
142 (defcustom org-bbdb-anniversary-format-alist
143 '(("birthday" lambda
144 (name years suffix)
145 (concat "Birthday: [[bbdb:" name "][" name " ("
146 (format "%s" years) ; handles numbers as well as strings
147 suffix ")]]"))
148 ("wedding" lambda
149 (name years suffix)
150 (concat "[[bbdb:" name "][" name "'s "
151 (format "%s" years)
152 suffix " wedding anniversary]]")))
153 "How different types of anniversaries should be formatted.
154 An alist of elements (STRING . FORMAT) where STRING is the name of an
155 anniversary class and format is either:
156 1) A format string with the following substitutions (in order):
157 * the name of the record containing this anniversary
158 * the number of years
159 * an ordinal suffix (st, nd, rd, th) for the year
161 2) A function to be called with three arguments: NAME YEARS SUFFIX
162 (string int string) returning a string for the diary or nil.
164 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
165 scope of variables NAME, YEARS and SUFFIX (among others)."
166 :type 'sexp
167 :group 'org-bbdb-anniversaries
168 :require 'bbdb)
170 (defcustom org-bbdb-anniversary-field 'anniversary
171 "The BBDB field which contains anniversaries.
172 The anniversaries are stored in the following format
174 YYYY-MM-DD Class-or-Format-String
176 where class is one of the customized classes for anniversaries;
177 birthday and wedding are predefined. Format-String can take three
178 substitutions 1) the name of the record containing this
179 anniversary, 2) the number of years, and 3) an ordinal suffix for
180 the year.
182 Multiple anniversaries can be separated by \\n."
183 :type 'symbol
184 :group 'org-bbdb-anniversaries
185 :require 'bbdb)
187 (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
188 "How to retrieve `month date year' from the anniversary field.
190 Customize if you have already filled your BBDB with dates
191 different from YYYY-MM-DD. The function must return a list (month
192 date year)."
193 :type 'function
194 :group 'org-bbdb-anniversaries
195 :require 'bbdb)
198 ;; Install the link type
199 (org-add-link-type "bbdb" 'org-bbdb-open 'org-bbdb-export)
200 (add-hook 'org-store-link-functions 'org-bbdb-store-link)
202 ;; Implementation
203 (defun org-bbdb-store-link ()
204 "Store a link to a BBDB database entry."
205 (when (eq major-mode 'bbdb-mode)
206 ;; This is BBDB, we make this link!
207 (let* ((rec (bbdb-current-record))
208 (name (bbdb-record-name rec))
209 (company (if org-bbdb-old
210 (bbdb-record-getprop rec 'company)
211 (car (bbdb-record-get-field rec 'organization))))
212 (link (org-make-link "bbdb:" name)))
213 (org-store-link-props :type "bbdb" :name name :company company
214 :link link :description name)
215 link)))
217 (defun org-bbdb-export (path desc format)
218 "Create the export version of a BBDB link specified by PATH or DESC.
219 If exporting to either HTML or LaTeX FORMAT the link will be
220 italicized, in all other cases it is left unchanged."
221 (when (string= desc (format "bbdb:%s" path))
222 (setq desc path))
223 (cond
224 ((eq format 'html) (format "<i>%s</i>" desc))
225 ((eq format 'latex) (format "\\textit{%s}" desc))
226 (t desc)))
228 (defun org-bbdb-open (name)
229 "Follow a BBDB link to NAME."
230 (require 'bbdb)
231 (let ((inhibit-redisplay (not debug-on-error))
232 (bbdb-electric-p nil))
233 (if org-bbdb-old
234 (org-bbdb-open-old)
235 (org-bbdb-open-new))))
237 (defun org-bbdb-open-old ()
238 (catch 'exit
239 ;; Exact match on name
240 (bbdb-name (concat "\\`" name "\\'") nil)
241 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
242 ;; Exact match on name
243 (bbdb-company (concat "\\`" name "\\'") nil)
244 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
245 ;; Partial match on name
246 (bbdb-name name nil)
247 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
248 ;; Partial match on company
249 (bbdb-company name nil)
250 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
251 ;; General match including network address and notes
252 (bbdb name nil)
253 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
254 (delete-window (get-buffer-window "*BBDB*"))
255 (error "No matching BBDB record"))))
257 (defun org-bbdb-open-new ()
258 (catch 'exit
259 ;; Exact match on name
260 (bbdb-search-name (concat "\\`" name "\\'") nil)
261 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
262 ;; Exact match on name
263 (bbdb-search-organization (concat "\\`" name "\\'") nil)
264 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
265 ;; Partial match on name
266 (bbdb-search-name name nil)
267 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
268 ;; Partial match on company
269 (bbdb-search-organization name nil)
270 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
271 ;; General match including network address and notes
272 (bbdb name nil)
273 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
274 (delete-window (get-buffer-window "*BBDB*"))
275 (error "No matching BBDB record"))))
277 (defun org-bbdb-anniv-extract-date (time-str)
278 "Convert YYYY-MM-DD to (month date year).
279 Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted
280 it will be considered unknown."
281 (multiple-value-bind (a b c) (values-list (bbdb-split time-str "-"))
282 (if (eq c nil)
283 (list (string-to-number a)
284 (string-to-number b)
285 nil)
286 (list (string-to-number b)
287 (string-to-number c)
288 (string-to-number a)))))
290 (defun org-bbdb-anniv-split (str)
291 "Split multiple entries in the BBDB anniversary field.
292 Argument STR is the anniversary field in BBDB."
293 (let ((pos (string-match "[ \t]" str)))
294 (if pos (list (substring str 0 pos)
295 (bbdb-string-trim (substring str pos)))
296 (list str nil))))
298 (defvar org-bbdb-anniv-hash nil
299 "A hash holding anniversaries extracted from BBDB.
300 The hash table is created on first use.")
302 (defvar org-bbdb-updated-p t
303 "This is non-nil if BBDB has been updated since we last built the hash.")
305 (defun org-bbdb-make-anniv-hash ()
306 "Create a hash with anniversaries extracted from BBDB, for fast access.
307 The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
309 (let (split tmp annivs)
310 (clrhash org-bbdb-anniv-hash)
311 (dolist (rec (bbdb-records))
312 (when (setq annivs (bbdb-record-getprop
313 rec org-bbdb-anniversary-field))
314 (setq annivs (bbdb-split annivs "\n"))
315 (while annivs
316 (setq split (org-bbdb-anniv-split (pop annivs)))
317 (multiple-value-bind (m d y)
318 (values-list (funcall org-bbdb-extract-date-fun (car split)))
319 (setq tmp (gethash (list m d) org-bbdb-anniv-hash))
320 (puthash (list m d) (cons (list y
321 (bbdb-record-name rec)
322 (cadr split))
323 tmp)
324 org-bbdb-anniv-hash))))))
325 (setq org-bbdb-updated-p nil))
327 (defun org-bbdb-updated (rec)
328 "Record the fact that BBDB has been updated.
329 This is used by Org to re-create the anniversary hash table."
330 (setq org-bbdb-updated-p t))
332 (add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
334 ;;;###autoload
335 (defun org-bbdb-anniversaries()
336 "Extract anniversaries from BBDB for display in the agenda."
337 (require 'bbdb)
338 (require 'diary-lib)
339 (unless (hash-table-p org-bbdb-anniv-hash)
340 (setq org-bbdb-anniv-hash
341 (make-hash-table :test 'equal :size 366)))
343 (when (or org-bbdb-updated-p
344 (= 0 (hash-table-count org-bbdb-anniv-hash)))
345 (org-bbdb-make-anniv-hash))
347 (let* ((m (car date)) ; month
348 (d (nth 1 date)) ; day
349 (y (nth 2 date)) ; year
350 (annivs (gethash (list m d) org-bbdb-anniv-hash))
351 (text ())
352 rec recs)
354 ;; we don't want to miss people born on Feb. 29th
355 (when (and (= m 3) (= d 1)
356 (not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
357 (not (calendar-leap-year-p y)))
358 (setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
359 (while (setq rec (pop recs))
360 (push rec annivs)))
362 (when annivs
363 (while (setq rec (pop annivs))
364 (when rec
365 (let* ((class (or (nth 2 rec)
366 org-bbdb-default-anniversary-format))
367 (form (or (cdr (assoc-string
368 class org-bbdb-anniversary-format-alist t))
369 class)) ; (as format string)
370 (name (nth 1 rec))
371 (years (if (eq (car rec) nil)
372 "unknown"
373 (- y (car rec))))
374 (suffix (if (eq (car rec) nil)
376 (diary-ordinal-suffix years)))
377 (tmp (cond
378 ((functionp form)
379 (funcall form name years suffix))
380 ((listp form) (eval form))
381 (t (format form name years suffix)))))
382 (org-add-props tmp nil 'org-bbdb-name name)
383 (if text
384 (setq text (append text (list tmp)))
385 (setq text (list tmp)))))
387 text))
389 (defun org-bbdb-complete-link ()
390 "Read a bbdb link with name completion."
391 (require 'bbdb-com)
392 (concat "bbdb:"
393 (bbdb-record-name (car (bbdb-completing-read-record "Name: ")))))
395 (defun org-bbdb-anniv-export-ical ()
396 "Extract anniversaries from BBDB and convert them to icalendar format."
397 (require 'bbdb)
398 (require 'diary-lib)
399 (unless (hash-table-p org-bbdb-anniv-hash)
400 (setq org-bbdb-anniv-hash
401 (make-hash-table :test 'equal :size 366)))
402 (when (or org-bbdb-updated-p
403 (= 0 (hash-table-count org-bbdb-anniv-hash)))
404 (org-bbdb-make-anniv-hash))
405 (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash))
407 (defun org-bbdb-format-vevent (key recs)
408 (let (rec categ)
409 (while (setq rec (pop recs))
410 (setq categ (or (nth 2 rec) org-bbdb-default-anniversary-format))
411 (princ (format "BEGIN:VEVENT
412 UID: ANNIV-%4i%02i%02i-%s
413 DTSTART:%4i%02i%02i
414 SUMMARY:%s
415 DESCRIPTION:%s
416 CATEGORIES:%s
417 RRULE:FREQ=YEARLY
418 END:VEVENT\n"
419 (nth 0 rec) (nth 0 key) (nth 1 key)
420 (mapconcat 'identity
421 (org-split-string (nth 1 rec) "[^a-zA-Z0-90]+")
422 "-")
423 (nth 0 rec) (nth 0 key) (nth 1 key)
424 (nth 1 rec)
425 (concat (capitalize categ) " " (nth 1 rec))
426 categ)))))
428 (provide 'org-bbdb)
430 ;;; org-bbdb.el ends here