scheme interaction mode
[bpt/emacs.git] / lisp / org / org-bbdb.el
CommitLineData
20908596
CD
1;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
2
ba318903 3;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
20908596 4
dfd98937
BG
5;; Authors: Carsten Dominik <carsten at orgmode dot org>
6;; Thomas Baumann <thomas dot baumann at ch dot tum dot de>
20908596
CD
7;; Keywords: outlines, hypermedia, calendar, wp
8;; Homepage: http://orgmode.org
20908596
CD
9;;
10;; This file is part of GNU Emacs.
11;;
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
20908596
CD
16
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.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25;;
26;;; Commentary:
27
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'.
31
20908596
CD
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.
38;;
39;; Put the following in /somewhere/at/home/diary.org and make sure
40;; that this file is in `org-agenda-files`
41;;
42;; %%(org-bbdb-anniversaries)
43;;
44;; For example my diary.org looks like:
45;; * Anniversaries
46;; #+CATEGORY: Anniv
47;; %%(org-bbdb-anniversaries)
48;;
49;;
c8d0cf5c
CD
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.
20908596 54;;
c8d0cf5c
CD
55;; The format of an anniversary field stored in BBDB is the following
56;; (items in {} are optional):
57;;
58;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}
59;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}...
20908596
CD
60;;
61;; CLASS-OR-FORMAT-STRING is one of two things:
62;;
c8d0cf5c
CD
63;; - an identifier for a class of anniversaries (eg. birthday or
64;; wedding) from `org-bbdb-anniversary-format-alist' which then
8bfe682a 65;; defines the format string for this class
c8d0cf5c
CD
66;; - the (format) string displayed in the diary.
67;;
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.
20908596 72;;
c8d0cf5c
CD
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).
20908596
CD
76;;
77;; The substitutions in the format string are (in order):
c8d0cf5c
CD
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
20908596
CD
81;;
82;; See the documentation of `org-bbdb-anniversary-format-alist' for
83;; further options.
84;;
85;; Example
86;;
87;; 1973-06-22
88;; 20??-??-?? wedding
89;; 1998-03-12 %s created bbdb-anniv.el %d years ago
b349f79f
CD
90;;
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.
93;;
20908596
CD
94;;; Code:
95
96(require 'org)
97(eval-when-compile
98 (require 'cl))
99
100;; Declare external functions and variables
101
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))
c8d0cf5c
CD
107(declare-function bbdb-completing-read-record "ext:bbdb-com"
108 (prompt &optional omit-records))
20908596
CD
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"
8223b1d2 112 (&optional dont-check-disk already-in-db-buffer))
20908596
CD
113(declare-function bbdb-split "ext:bbdb" (string separators))
114(declare-function bbdb-string-trim "ext:bbdb" (string))
153ae947
BG
115(declare-function bbdb-record-get-field "ext:bbdb" (record field))
116(declare-function bbdb-search-name "ext:bbdb-com" (regexp &optional layout))
117(declare-function bbdb-search-organization "ext:bbdb-com" (regexp &optional layout))
c8d0cf5c 118
271672fa 119;; `bbdb-record-note' was part of BBDB v3.x
8223b1d2 120(declare-function bbdb-record-note "ext:bbdb" (record label))
271672fa
BG
121;; `bbdb-record-xfield' replaces it in recent BBDB v3.x+
122(declare-function bbdb-record-xfield "ext:bbdb" (record label))
8223b1d2 123
20908596
CD
124(declare-function calendar-leap-year-p "calendar" (year))
125(declare-function diary-ordinal-suffix "diary-lib" (n))
126
8223b1d2 127(org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
e66ba1df 128
20908596
CD
129;; Customization
130
131(defgroup org-bbdb-anniversaries nil
132 "Customizations for including anniversaries from BBDB into Agenda."
133 :group 'org-bbdb)
134
135(defcustom org-bbdb-default-anniversary-format "birthday"
136 "Default anniversary class."
137 :type 'string
138 :group 'org-bbdb-anniversaries
139 :require 'bbdb)
140
141(defcustom org-bbdb-anniversary-format-alist
8223b1d2
BG
142 '(("birthday" .
143 (lambda (name years suffix)
144 (concat "Birthday: [[bbdb:" name "][" name " ("
145 (format "%s" years) ; handles numbers as well as strings
146 suffix ")]]")))
147 ("wedding" .
148 (lambda (name years suffix)
149 (concat "[[bbdb:" name "][" name "'s "
150 (format "%s" years)
151 suffix " wedding anniversary]]"))))
20908596
CD
152 "How different types of anniversaries should be formatted.
153An alist of elements (STRING . FORMAT) where STRING is the name of an
154anniversary class and format is either:
1551) A format string with the following substitutions (in order):
8223b1d2
BG
156 - the name of the record containing this anniversary
157 - the number of years
158 - an ordinal suffix (st, nd, rd, th) for the year
20908596
CD
159
1602) A function to be called with three arguments: NAME YEARS SUFFIX
161 (string int string) returning a string for the diary or nil.
162
1633) An Emacs Lisp form that should evaluate to a string (or nil) in the
164 scope of variables NAME, YEARS and SUFFIX (among others)."
8223b1d2
BG
165 :type '(alist :key-type (string :tag "Class")
166 :value-type (function :tag "Function"))
20908596
CD
167 :group 'org-bbdb-anniversaries
168 :require 'bbdb)
169
170(defcustom org-bbdb-anniversary-field 'anniversary
171 "The BBDB field which contains anniversaries.
172The anniversaries are stored in the following format
173
174YYYY-MM-DD Class-or-Format-String
175
176where class is one of the customized classes for anniversaries;
177birthday and wedding are predefined. Format-String can take three
178substitutions 1) the name of the record containing this
179anniversary, 2) the number of years, and 3) an ordinal suffix for
180the year.
181
888b663c 182Multiple anniversaries can be separated by \\n."
20908596
CD
183 :type 'symbol
184 :group 'org-bbdb-anniversaries
185 :require 'bbdb)
186
187(defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
188 "How to retrieve `month date year' from the anniversary field.
189
888b663c 190Customize if you have already filled your BBDB with dates
20908596 191different from YYYY-MM-DD. The function must return a list (month
888b663c 192date year)."
20908596
CD
193 :type 'function
194 :group 'org-bbdb-anniversaries
195 :require 'bbdb)
196
197
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)
201
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!
e66ba1df
BG
207 (let* ((rec (bbdb-current-record))
208 (name (bbdb-record-name rec))
153ae947 209 (company (if (fboundp 'bbdb-record-getprop)
e66ba1df
BG
210 (bbdb-record-getprop rec 'company)
211 (car (bbdb-record-get-field rec 'organization))))
8223b1d2 212 (link (concat "bbdb:" name)))
20908596
CD
213 (org-store-link-props :type "bbdb" :name name :company company
214 :link link :description name)
215 link)))
216
217(defun org-bbdb-export (path desc format)
218 "Create the export version of a BBDB link specified by PATH or DESC.
219If exporting to either HTML or LaTeX FORMAT the link will be
86fbb8ca 220italicized, in all other cases it is left unchanged."
3ab2c837
BG
221 (when (string= desc (format "bbdb:%s" path))
222 (setq desc path))
20908596 223 (cond
3ab2c837
BG
224 ((eq format 'html) (format "<i>%s</i>" desc))
225 ((eq format 'latex) (format "\\textit{%s}" desc))
8223b1d2
BG
226 ((eq format 'odt)
227 (format "<text:span text:style-name=\"Emphasis\">%s</text:span>" desc))
3ab2c837 228 (t desc)))
20908596
CD
229
230(defun org-bbdb-open (name)
231 "Follow a BBDB link to NAME."
153ae947 232 (require 'bbdb-com)
20908596
CD
233 (let ((inhibit-redisplay (not debug-on-error))
234 (bbdb-electric-p nil))
153ae947
BG
235 (if (fboundp 'bbdb-name)
236 (org-bbdb-open-old name)
237 (org-bbdb-open-new name))))
e66ba1df 238
153ae947 239(defun org-bbdb-open-old (name)
e66ba1df
BG
240 (catch 'exit
241 ;; Exact match on name
242 (bbdb-name (concat "\\`" name "\\'") nil)
243 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
244 ;; Exact match on name
245 (bbdb-company (concat "\\`" name "\\'") nil)
246 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
247 ;; Partial match on name
248 (bbdb-name name nil)
249 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
250 ;; Partial match on company
251 (bbdb-company name nil)
252 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
253 ;; General match including network address and notes
254 (bbdb name nil)
255 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
256 (delete-window (get-buffer-window "*BBDB*"))
257 (error "No matching BBDB record"))))
258
153ae947 259(defun org-bbdb-open-new (name)
e66ba1df
BG
260 (catch 'exit
261 ;; Exact match on name
262 (bbdb-search-name (concat "\\`" name "\\'") nil)
263 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
264 ;; Exact match on name
265 (bbdb-search-organization (concat "\\`" name "\\'") nil)
266 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
267 ;; Partial match on name
268 (bbdb-search-name name nil)
269 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
270 ;; Partial match on company
271 (bbdb-search-organization name nil)
272 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
273 ;; General match including network address and notes
274 (bbdb name nil)
275 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
276 (delete-window (get-buffer-window "*BBDB*"))
277 (error "No matching BBDB record"))))
20908596
CD
278
279(defun org-bbdb-anniv-extract-date (time-str)
280 "Convert YYYY-MM-DD to (month date year).
3ab2c837
BG
281Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted
282it will be considered unknown."
8223b1d2 283 (multiple-value-bind (a b c) (values-list (org-split-string time-str "-"))
3ab2c837
BG
284 (if (eq c nil)
285 (list (string-to-number a)
286 (string-to-number b)
287 nil)
288 (list (string-to-number b)
289 (string-to-number c)
290 (string-to-number a)))))
20908596
CD
291
292(defun org-bbdb-anniv-split (str)
888b663c 293 "Split multiple entries in the BBDB anniversary field.
20908596
CD
294Argument STR is the anniversary field in BBDB."
295 (let ((pos (string-match "[ \t]" str)))
296 (if pos (list (substring str 0 pos)
297 (bbdb-string-trim (substring str pos)))
298 (list str nil))))
299
b349f79f
CD
300(defvar org-bbdb-anniv-hash nil
301 "A hash holding anniversaries extracted from BBDB.
302The hash table is created on first use.")
20908596 303
b349f79f
CD
304(defvar org-bbdb-updated-p t
305 "This is non-nil if BBDB has been updated since we last built the hash.")
306
307(defun org-bbdb-make-anniv-hash ()
308 "Create a hash with anniversaries extracted from BBDB, for fast access.
309The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
8223b1d2 310 (let ((old-bbdb (fboundp 'bbdb-record-getprop))
271672fa
BG
311 (record-func (if (fboundp 'bbdb-record-xfield)
312 'bbdb-record-xfield
313 'bbdb-record-note))
8223b1d2 314 split tmp annivs)
b349f79f 315 (clrhash org-bbdb-anniv-hash)
20908596 316 (dolist (rec (bbdb-records))
8223b1d2
BG
317 (when (setq annivs (if old-bbdb
318 (bbdb-record-getprop
319 rec org-bbdb-anniversary-field)
271672fa
BG
320 (funcall record-func
321 rec org-bbdb-anniversary-field)))
8223b1d2
BG
322 (setq annivs (if old-bbdb
323 (bbdb-split annivs "\n")
324 ;; parameter order is reversed in new bbdb
325 (bbdb-split "\n" annivs)))
20908596
CD
326 (while annivs
327 (setq split (org-bbdb-anniv-split (pop annivs)))
328 (multiple-value-bind (m d y)
90ca3a46 329 (values-list (funcall org-bbdb-extract-date-fun (car split)))
b349f79f 330 (setq tmp (gethash (list m d) org-bbdb-anniv-hash))
ff4be292
CD
331 (puthash (list m d) (cons (list y
332 (bbdb-record-name rec)
b349f79f
CD
333 (cadr split))
334 tmp)
335 org-bbdb-anniv-hash))))))
336 (setq org-bbdb-updated-p nil))
337
338(defun org-bbdb-updated (rec)
339 "Record the fact that BBDB has been updated.
340This is used by Org to re-create the anniversary hash table."
341 (setq org-bbdb-updated-p t))
20908596 342
b349f79f
CD
343(add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
344
345;;;###autoload
bdebdb64 346(defun org-bbdb-anniversaries ()
b349f79f 347 "Extract anniversaries from BBDB for display in the agenda."
621f83e4 348 (require 'bbdb)
b349f79f
CD
349 (require 'diary-lib)
350 (unless (hash-table-p org-bbdb-anniv-hash)
351 (setq org-bbdb-anniv-hash
352 (make-hash-table :test 'equal :size 366)))
353
354 (when (or org-bbdb-updated-p
355 (= 0 (hash-table-count org-bbdb-anniv-hash)))
356 (org-bbdb-make-anniv-hash))
357
358 (let* ((m (car date)) ; month
359 (d (nth 1 date)) ; day
360 (y (nth 2 date)) ; year
361 (annivs (gethash (list m d) org-bbdb-anniv-hash))
362 (text ())
65c439fd 363 rec recs)
ff4be292 364
b349f79f 365 ;; we don't want to miss people born on Feb. 29th
621f83e4
CD
366 (when (and (= m 3) (= d 1)
367 (not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
368 (not (calendar-leap-year-p y)))
369 (setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
370 (while (setq rec (pop recs))
371 (push rec annivs)))
b349f79f
CD
372
373 (when annivs
374 (while (setq rec (pop annivs))
ff4be292 375 (when rec
b349f79f
CD
376 (let* ((class (or (nth 2 rec)
377 org-bbdb-default-anniversary-format))
afe98dfa
CD
378 (form (or (cdr (assoc-string
379 class org-bbdb-anniversary-format-alist t))
b349f79f
CD
380 class)) ; (as format string)
381 (name (nth 1 rec))
3ab2c837
BG
382 (years (if (eq (car rec) nil)
383 "unknown"
384 (- y (car rec))))
385 (suffix (if (eq (car rec) nil)
386 ""
387 (diary-ordinal-suffix years)))
b349f79f
CD
388 (tmp (cond
389 ((functionp form)
390 (funcall form name years suffix))
391 ((listp form) (eval form))
392 (t (format form name years suffix)))))
393 (org-add-props tmp nil 'org-bbdb-name name)
394 (if text
395 (setq text (append text (list tmp)))
396 (setq text (list tmp)))))
397 ))
afe98dfa 398 text))
20908596 399
c8d0cf5c
CD
400(defun org-bbdb-complete-link ()
401 "Read a bbdb link with name completion."
402 (require 'bbdb-com)
403 (concat "bbdb:"
404 (bbdb-record-name (car (bbdb-completing-read-record "Name: ")))))
405
406(defun org-bbdb-anniv-export-ical ()
e66ba1df 407 "Extract anniversaries from BBDB and convert them to icalendar format."
c8d0cf5c
CD
408 (require 'bbdb)
409 (require 'diary-lib)
410 (unless (hash-table-p org-bbdb-anniv-hash)
411 (setq org-bbdb-anniv-hash
412 (make-hash-table :test 'equal :size 366)))
413 (when (or org-bbdb-updated-p
414 (= 0 (hash-table-count org-bbdb-anniv-hash)))
415 (org-bbdb-make-anniv-hash))
416 (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash))
417
418(defun org-bbdb-format-vevent (key recs)
419 (let (rec categ)
420 (while (setq rec (pop recs))
421 (setq categ (or (nth 2 rec) org-bbdb-default-anniversary-format))
422 (princ (format "BEGIN:VEVENT
423UID: ANNIV-%4i%02i%02i-%s
424DTSTART:%4i%02i%02i
425SUMMARY:%s
426DESCRIPTION:%s
427CATEGORIES:%s
428RRULE:FREQ=YEARLY
429END:VEVENT\n"
430 (nth 0 rec) (nth 0 key) (nth 1 key)
431 (mapconcat 'identity
432 (org-split-string (nth 1 rec) "[^a-zA-Z0-90]+")
433 "-")
434 (nth 0 rec) (nth 0 key) (nth 1 key)
435 (nth 1 rec)
436 (concat (capitalize categ) " " (nth 1 rec))
437 categ)))))
438
20908596
CD
439(provide 'org-bbdb)
440
bdebdb64
BG
441;; Local variables:
442;; generated-autoload-file: "org-loaddefs.el"
443;; End:
444
20908596 445;;; org-bbdb.el ends here