Fix customize interface for planner-annotation-functions
[planner-el.git] / planner-bbdb.el
blob0a97ca9203c89a0bc8f2fe987332491e61a023e8
1 ;;; planner-bbdb.el --- BBDB integration for the Emacs Planner
3 ;; Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
4 ;; Parts copyright (C) 2004 Andrew J. Korty
6 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; Keywords: planner, gnus
8 ;; URL: http://www.plannerlove.com/
10 ;; This file is part of Planner. It is not part of GNU Emacs.
12 ;; Planner is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; Planner is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with Planner; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;;_ + Commentary:
29 ;; This file allows you to refer to your contacts easily from within
30 ;; a planner page.
32 ;; Example:
33 ;; [[bbdb://Sacha.*Chua][Sacha]] will be linked to the blog, web
34 ;; or net fields of the first matching BBDB record.
36 ;;;_ + Contributors
38 ;; Andrew J. Korty (ajk AT iu DOT edu) had the idea to add the mailto:
39 ;; URI and provided the initial patch for it.
41 ;; Yann Hodique helped to port this to Muse.
43 ;;; Code:
45 (require 'planner)
46 (require 'bbdb-com)
48 (defun planner-bbdb-get-name-from-address (address)
49 "Return the name for ADDRESS to be used in links."
50 (let* ((addr (mail-extract-address-components address))
51 (rec (apply 'bbdb-search-simple addr)))
52 (if rec
53 (bbdb-record-name rec)
54 (or (car addr) (cadr addr)))))
56 (defvar planner-bbdb-plan-field "plan"
57 "Field that contains a planner page associated with this record.")
59 ;;;###autoload
60 (defun planner-bbdb-annotation-from-bbdb ()
61 "If called from a bbdb buffer, return an annotation.
62 Suitable for use in `planner-annotation-functions'."
63 (when (eq major-mode 'bbdb-mode)
64 (or (bbdb-record-getprop
65 (bbdb-current-record) 'plan)
66 ;; From a BBDB entry with a plan page; use that. Yay!
67 (planner-make-link
68 (concat "bbdb://"
69 (planner-replace-regexp-in-string
70 " " "." (bbdb-record-name (bbdb-current-record))))
71 (bbdb-record-name (bbdb-current-record))))))
74 ;;;###autoload
75 (defun planner-bbdb-browse-url (url)
76 "If this is a BBDB URL, jump to it."
77 (when (string-match "^bbdb:/?/?\\(.+\\)" url)
78 (bbdb (match-string 1 url) nil)
79 t))
81 ;;;###autoload
82 (defun planner-bbdb-resolve-url (id)
83 "Replace ID with the blog, web or e-mail address of the BBDB record."
84 (save-match-data
85 (when (string-match "\\`bbdb:/+" id)
86 (setq id (replace-match "" t t id)))
87 (let ((record (car (bbdb-search (bbdb-records) id id id))))
88 (or (and record
89 (or (bbdb-record-getprop record 'blog)
90 (bbdb-record-getprop record 'web)
91 (when (car (bbdb-record-net record))
92 (concat "mailto:" (car (bbdb-record-net record))))))
93 nil))))
95 (planner-add-protocol "bbdb:/+" 'planner-bbdb-browse-url 'planner-bbdb-resolve-url)
96 (add-hook 'planner-annotation-functions 'planner-bbdb-annotation-from-bbdb)
97 (custom-add-option 'planner-annotation-functions 'planner-bbdb-annotation-from-bbdb)
99 (defalias 'planner-get-name-from-address 'planner-bbdb-get-name-from-address)
101 (provide 'planner-bbdb)
103 ;;; planner-bbdb.el ends here