(mu-modules-to-compile): Add `mu-register'.
[elisp/mu-cite.git] / mu-bbdb.el
1 ;;; mu-bbdb.el --- `attribution' function for mu-cite with BBDB.
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Maintainer: Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: mail, news, citation, bbdb
8
9 ;; This file is part of MU (Message Utilities).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;  - How to use
29 ;;    1. bytecompile this file and copy it to the apropriate directory.
30 ;;    2. put the following lines to your ~/.emacs:
31 ;;             (add-hook 'mu-cite-load-hook
32 ;;                       (function
33 ;;                        (lambda ()
34 ;;                          (require 'mu-bbdb)
35 ;;                          )))
36
37 \f
38 ;;; Code:
39
40 (eval-when-compile (require 'cl))
41
42 ;; Pickup `module-installed-p'.
43 (require 'path-util)
44
45 (require 'mu-cite)
46 (when (module-installed-p 'bbdb)
47   (require 'bbdb))
48
49
50 ;;; @ obsolete functions
51 ;;;
52
53 ;; This part will be abolished in the future.
54
55 (eval-and-compile
56   (defconst mu-bbdb-obsolete-function-alist
57     '((mu-cite/get-bbdb-attr            mu-bbdb-get-attr)
58       (mu-cite/get-bbdb-prefix-method   mu-bbdb-get-prefix-method)
59       (mu-cite/get-bbdb-prefix-register-method
60        mu-bbdb-get-prefix-register-method)
61       (mu-cite/get-bbdb-prefix-register-verbose-method
62        mu-bbdb-get-prefix-register-verbose-method)
63       (mu-cite/set-bbdb-attr            mu-bbdb-set-attr)))
64
65   (mapcar
66    (function (lambda (elem)
67                (apply (function define-obsolete-function-alias) elem)))
68    mu-bbdb-obsolete-function-alist)
69   )
70
71
72 ;;; @ set up
73 ;;;
74
75 (defgroup mu-bbdb nil
76   "`attribution' function for mu-cite with BBDB."
77   :prefix "mu-bbdb-"
78   :group 'mu-cite
79   :group 'bbdb)
80
81 (defcustom mu-bbdb-load-hook nil
82   "List of functions called after mu-bbdb is loaded."
83   :type 'hook
84   :group 'mu-bbdb)
85
86
87 ;;; @@ prefix and registration using BBDB
88 ;;;
89
90 (defun mu-bbdb-get-prefix-method ()
91   (or (mu-bbdb-get-attr (mu-cite-get-value 'address))
92       ">"))
93
94 (defun mu-bbdb-get-attr (addr)
95   "Extract attribute information from BBDB."
96   (let ((record (bbdb-search-simple nil addr)))
97     (when record
98       (bbdb-record-getprop record 'attribution))))
99
100 (defun mu-bbdb-set-attr (attr addr)
101   "Add attribute information to BBDB."
102   (let* ((bbdb-notice-hook nil)
103          (record (bbdb-annotate-message-sender
104                   addr t
105                   (bbdb-invoke-hook-for-value
106                    bbdb/mail-auto-create-p)
107                   t)))
108     (when record
109       (bbdb-record-putprop record 'attribution attr)
110       (bbdb-change-record record nil))))
111
112 (defun mu-bbdb-get-prefix-register-method ()
113   (let ((addr (mu-cite-get-value 'address)))
114     (or (mu-bbdb-get-attr addr)
115         (let ((return
116                (read-string "Citation name? "
117                             (or (mu-cite-get-value 'x-attribution)
118                                 (mu-cite-get-value 'full-name))
119                             'mu-cite-minibuffer-history)))
120           (if (and (not (string-equal return ""))
121                    (y-or-n-p (format "Register \"%s\"? " return)))
122               (mu-bbdb-set-attr return addr))
123           return))))
124
125 (defun mu-bbdb-get-prefix-register-verbose-method ()
126   (let* ((addr (mu-cite-get-value 'address))
127          (attr (mu-bbdb-get-attr addr))
128          (return (read-string "Citation name? "
129                               (or attr
130                                   (mu-cite-get-value 'x-attribution)
131                                   (mu-cite-get-value 'full-name))
132                               'mu-cite-minibuffer-history)))
133     (if (and (not (string-equal return ""))
134              (not (string-equal return attr))
135              (y-or-n-p (format "Register \"%s\"? " return)))
136         (mu-bbdb-set-attr return addr))
137     return))
138
139 (unless (assoc 'bbdb-prefix mu-cite-default-methods-alist)
140   (setq mu-cite-default-methods-alist
141         (append mu-cite-default-methods-alist
142                 (list
143                  (cons 'bbdb-prefix
144                        (function mu-bbdb-get-prefix-method))
145                  (cons 'bbdb-prefix-register
146                        (function mu-bbdb-get-prefix-register-method))
147                  (cons 'bbdb-prefix-register-verbose
148                        (function
149                         mu-bbdb-get-prefix-register-verbose-method))))))
150
151 \f
152 ;;; @ end
153 ;;;
154
155 (provide 'mu-bbdb)
156
157 (run-hooks 'mu-bbdb-load-hook)
158
159 ;;; mu-bbdb.el ends here