6b1c0b7278bd136fad4b15102f54e6fb14a47e01
[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
33 ;;; @ variables
34 ;;;
35
36 (defcustom mu-registration-file (expand-file-name "~/.mu-cite.el")
37   "The name of the user environment file for mu-cite."
38   :type 'file
39   :group 'mu-cite)
40
41 (defcustom mu-registration-file-modes 384
42   "Mode bits of `mu-registration-file', as an integer."
43   :type 'integer
44   :group 'mu-cite)
45
46 (defcustom mu-registration-file-coding-system-for-read nil
47   "Coding system used when reading a registration file."
48   :group 'mu-cite)
49
50 (defcustom mu-cite-allow-null-string-registration nil
51   "If non-nil, null-string citation-name is registered."
52   :type 'boolean
53   :group 'mu-cite)
54
55 (defvar mu-registration-symbol 'mu-citation-name-alist
56   "*Name of the variable to register citation prefix strings.")
57
58 (defvar mu-registration-file-coding-system nil
59   "Coding system used when writing a current registration file.")
60
61 (defvar mu-register-history nil)
62
63 (eval-when-compile (require 'static))
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         (insert-file-contents-as-coding-system
85          mu-registration-file-coding-system-for-read
86          mu-registration-file)
87         (setq mu-registration-file-coding-system
88               buffer-file-coding-system)
89         (let ((exp (read (current-buffer))))
90           (or (eq (car (cdr exp)) mu-registration-symbol)
91               (setcar (cdr exp) mu-registration-symbol))
92           (eval exp))))
93   (or (boundp mu-registration-symbol)
94       (set mu-registration-symbol nil)))
95
96 (defun mu-cite-save-registration-file ()
97   (with-temp-buffer
98     (insert ";;; " (file-name-nondirectory mu-registration-file) "\n")
99     (insert ";;; This file is generated automatically by mu-cite "
100             mu-cite-version "\n\n")
101     (insert "(setq "
102             (symbol-name mu-registration-symbol)
103             "\n      '(")
104     (insert (mapconcat
105              (function prin1-to-string)
106              (symbol-value mu-registration-symbol) "\n        "))
107     (insert "\n        ))\n\n")
108     (insert ";;; "
109             (file-name-nondirectory mu-registration-file)
110             " ends here.\n")
111     (write-region-as-coding-system mu-registration-file-coding-system
112                                    (point-min)(point-max)
113                                    mu-registration-file nil 'nomsg)
114     (condition-case nil
115         (set-file-modes mu-registration-file mu-registration-file-modes)
116       (error nil))))
117
118
119 ;;; @ database accessors
120 ;;;
121
122 ;; get citation-name from the database
123 (defun mu-register-get-citation-name (from)
124   (cdr (assoc from (symbol-value mu-registration-symbol))))
125
126 ;; register citation-name to the database
127 (defun mu-register-add-citation-name (name from)
128   (set-alist mu-registration-symbol from name)
129   (mu-cite-save-registration-file))
130
131
132 ;;; @ methods
133 ;;;
134
135 ;;;###autoload
136 (defun mu-cite-get-prefix-method ()
137   (or (mu-register-get-citation-name (mu-cite-get-value 'address))
138       ">"))
139
140 ;;;###autoload
141 (defun mu-cite-get-prefix-register-method ()
142   (let ((addr (mu-cite-get-value 'address)))
143     (or (mu-register-get-citation-name addr)
144         (let ((return
145                (read-string "Citation name? "
146                             (or (mu-cite-get-value 'x-attribution)
147                                 (mu-cite-get-value 'full-name))
148                             'mu-register-history)))
149           (when (and (or mu-cite-allow-null-string-registration
150                          (not (string-equal return "")))
151                      (y-or-n-p (format "Register \"%s\"? " return)))
152             (mu-register-add-citation-name return addr))
153           return))))
154
155 ;;;###autoload
156 (defun mu-cite-get-prefix-register-verbose-method ()
157   (let* ((addr (mu-cite-get-value 'address))
158          (return1 (mu-register-get-citation-name addr))
159          (return (read-string "Citation name? "
160                               (or return1
161                                   (mu-cite-get-value 'x-attribution)
162                                   (mu-cite-get-value 'full-name))
163                               'mu-register-history)))
164     (when (and (or mu-cite-allow-null-string-registration
165                    (not (string-equal return "")))
166                (not (string-equal return return1))
167                (y-or-n-p (format "Register \"%s\"? " return)))
168       (mu-register-add-citation-name return addr))
169     return))
170
171
172 ;;; @ end
173 ;;;
174
175 (provide 'mu-register)
176
177 (mu-cite-load-registration-file)
178
179 ;;; mu-register.el ends here