Update.
[elisp/mu-cite.git] / mu-register.el
1 ;;; mu-register.el --- registration feature of mu-cite
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MINOURA Makoto <minoura@netlaputa.or.jp>
6 ;;         MORIOKA Tomohiko <tomo@m17n.org>
7 ;;; Created: 1995-12-27 by MINOURA Makoto
8 ;; Maintainer: Katsumi Yamaoka <yamaoka@jpl.org>
9 ;; Keywords: registration, citation, mail, news
10
11 ;; This file is part of MU (Message Utilities).
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Code:
29
30 (require 'mu-cite)
31
32 (eval-when-compile (require 'static))
33
34
35 ;;; @ variables
36 ;;;
37
38 (defcustom mu-registration-file (expand-file-name "~/.mu-cite.el")
39   "The name of the user environment file for mu-cite."
40   :type 'file
41   :group 'mu-cite)
42
43 (defcustom mu-registration-file-modes 384
44   "Mode bits of `mu-registration-file', as an integer."
45   :type 'integer
46   :group 'mu-cite)
47
48 (defcustom mu-registration-file-coding-system-for-read nil
49   "Coding system used when reading a registration file."
50   :group 'mu-cite)
51
52 (defcustom mu-cite-allow-null-string-registration nil
53   "If non-nil, null-string citation-name is registered."
54   :type 'boolean
55   :group 'mu-cite)
56
57 (defvar mu-registration-symbol 'mu-citation-name-alist
58   "*Name of the variable to register citation prefix strings.")
59
60 (defvar mu-registration-file-coding-system nil
61   "Coding system used when writing a current registration file.")
62
63 (defvar mu-register-history nil)
64
65 (static-when (featurep 'xemacs)
66   (define-obsolete-variable-alias
67     'mu-cite/registration-file 'mu-registration-file)
68
69   (define-obsolete-variable-alias
70     'mu-cite/allow-null-string-registration
71     'mu-cite-allow-null-string-registration)
72
73   (define-obsolete-variable-alias
74     'mu-cite/registration-symbol 'mu-registration-symbol)
75   )
76
77
78 ;;; @ load / save registration file
79 ;;;
80
81 (defun mu-cite-load-registration-file ()
82   (if (file-readable-p mu-registration-file)
83       (with-temp-buffer
84         (if mu-registration-file-coding-system-for-read
85             (insert-file-contents-as-coding-system
86              mu-registration-file-coding-system-for-read
87              mu-registration-file)
88           (insert-file-contents mu-registration-file))
89         (setq mu-registration-file-coding-system
90               (static-cond
91                ((boundp 'buffer-file-coding-system)
92                 (symbol-value 'buffer-file-coding-system))
93                ((boundp 'file-coding-system)
94                 (symbol-value 'file-coding-system))
95                (t
96                 nil)))
97         (let ((exp (read (current-buffer))))
98           (or (eq (car (cdr exp)) mu-registration-symbol)
99               (setcar (cdr exp) mu-registration-symbol))
100           (eval exp))))
101   (or (boundp mu-registration-symbol)
102       (set mu-registration-symbol nil)))
103
104 (defun mu-cite-save-registration-file ()
105   (with-temp-buffer
106     (insert ";;; " (file-name-nondirectory mu-registration-file) "\n")
107     (insert ";;; This file is generated automatically by mu-cite "
108             mu-cite-version "\n\n")
109     (insert "(setq "
110             (symbol-name mu-registration-symbol)
111             "\n      '(")
112     (insert (mapconcat
113              (function
114               (lambda (elem)
115                 (format "(%s . %s)"
116                         (prin1-to-string
117                          (mu-cite-remove-text-properties (car elem)))
118                         (prin1-to-string
119                          (mu-cite-remove-text-properties (cdr elem))))))
120              (symbol-value mu-registration-symbol) "\n        "))
121     (insert "\n        ))\n\n")
122     (insert ";;; "
123             (file-name-nondirectory mu-registration-file)
124             " ends here\n")
125     (write-region-as-coding-system mu-registration-file-coding-system
126                                    (point-min)(point-max)
127                                    mu-registration-file nil 'nomsg)
128     (condition-case nil
129         (set-file-modes mu-registration-file mu-registration-file-modes)
130       (error nil))))
131
132
133 ;;; @ database accessors
134 ;;;
135
136 ;; get citation-name from the database
137 (defun mu-register-get-citation-name (from)
138   (cdr (assoc from (symbol-value mu-registration-symbol))))
139
140 ;; register citation-name to the database
141 (defun mu-register-add-citation-name (name from)
142   (set-alist mu-registration-symbol from name)
143   (mu-cite-save-registration-file))
144
145
146 ;;; @ methods
147 ;;;
148
149 ;;;###autoload
150 (defun mu-cite-get-prefix-method ()
151   (or (mu-register-get-citation-name (mu-cite-get-value 'address))
152       ">"))
153
154 ;;;###autoload
155 (defun mu-cite-get-prefix-register-method ()
156   (let ((addr (mu-cite-get-value 'address)))
157     (or (mu-register-get-citation-name addr)
158         (let* ((minibuffer-allow-text-properties nil)
159                (return
160                 (mu-cite-remove-text-properties
161                  (read-string "Citation name? "
162                               (or (mu-cite-get-value 'x-attribution)
163                                   (mu-cite-get-value 'full-name))
164                               'mu-register-history))))
165
166           (if (and (or mu-cite-allow-null-string-registration
167                        (not (string-equal return "")))
168                    (y-or-n-p (format "Register \"%s\"? " return)))
169               (mu-register-add-citation-name return addr))
170           return))))
171
172 ;;;###autoload
173 (defun mu-cite-get-prefix-register-verbose-method ()
174   (let* ((addr (mu-cite-get-value 'address))
175          (return1 (mu-register-get-citation-name addr))
176          (minibuffer-allow-text-properties nil)
177          (return (mu-cite-remove-text-properties
178                   (read-string "Citation name? "
179                                (or return1
180                                    (mu-cite-get-value 'x-attribution)
181                                    (mu-cite-get-value 'full-name))
182                                'mu-register-history))))
183     (if (and (or mu-cite-allow-null-string-registration
184                  (not (string-equal return "")))
185              (not (string-equal return return1))
186              (y-or-n-p (format "Register \"%s\"? " return)))
187         (mu-register-add-citation-name return addr))
188     return))
189
190
191 ;;; @ end
192 ;;;
193
194 (provide 'mu-register)
195
196 (mu-cite-load-registration-file)
197
198 ;;; mu-register.el ends here