Update copyright header.
[elisp/semi.git] / signature.el
1 ;;; signature.el --- a signature utility for GNU Emacs
2
3 ;; Copyright (C) 1994,1995,1996,1997,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;;         OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
7 ;;         Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
8 ;; Maintainer: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
9 ;; Created: 1994/7/11
10 ;; Keywords: mail, news, signature
11
12 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
13
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; see the file COPYING.  If not, write to
26 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Code:
30
31 (require 'std11)
32
33
34 ;;; @ valiables
35 ;;;
36
37 (defvar signature-insert-at-eof nil
38   "*If non-nil, insert signature at the end of file.")
39
40 (defvar signature-delete-blank-lines-at-eof nil
41   "*If non-nil, signature-insert-at-eof deletes blank lines at the end
42 of file.")
43
44 (defvar signature-load-hook nil
45   "*List of functions called after signature.el is loaded.")
46
47 (defvar signature-separator "-- \n"
48   "*String to separate contents and signature.
49 It is inserted when signature is inserted at end of file.")
50
51 (defvar signature-file-name "~/.signature"
52   "*Name of file containing the user's signature.")
53
54 (defvar signature-file-alist nil
55   "*Alist of the form:
56     (((FIELD . PATTERN) . FILENAME)
57      ...)
58 PATTERN is a string or list of string. If PATTERN matches the contents of
59 FIELD, the contents of FILENAME is inserted.")
60
61 (defvar signature-file-prefix nil
62   "*String containing optional prefix for the signature file names")
63
64 (defvar signature-insert-hook nil
65   "*List of functions called before inserting a signature.")
66
67 (defvar signature-use-bbdb nil
68   "*If non-nil, Register sigtype to BBDB.")
69
70 (autoload 'signature/get-sigtype-from-bbdb "mime-bbdb")
71
72 (defun signature/get-sigtype-interactively (&optional default)
73   (read-file-name "Insert your signature: "
74                   (or default (concat signature-file-name "-"))
75                   (or default signature-file-name)
76                   nil))
77
78 (defun signature/get-signature-file-name ()
79   (save-excursion
80     (save-restriction
81       (narrow-to-region
82        (goto-char (point-min))
83        (if (re-search-forward
84             (concat "^" (regexp-quote mail-header-separator) "$")
85             nil t)
86            (match-beginning 0)
87          (point-max)
88          ))
89       (catch 'found
90         (let ((alist signature-file-alist) cell field value)
91           (while alist
92             (setq cell  (car alist)
93                   field (std11-field-body (car (car cell)))
94                   value (cdr (car cell)))
95             (cond ((functionp value)
96                    (let ((name (apply value field (cdr cell))))
97                      (if name
98                          (throw 'found
99                                 (concat signature-file-prefix name))
100                        )))
101                   ((stringp field)
102                    (cond ((consp value)
103                           (while value
104                             (if (string-match (car value) field)
105                                 (throw 'found
106                                        (concat
107                                         signature-file-prefix (cdr cell)))
108                               (setq value (cdr value))
109                               )))
110                          ((stringp value)
111                           (if (string-match value field)
112                               (throw 'found
113                                      (concat
114                                       signature-file-prefix (cdr cell)))
115                             )))))
116             (setq alist (cdr alist))
117             ))
118         signature-file-name))))
119
120 (defun insert-signature (&optional arg)
121   "Insert the file named by signature-file-name.
122 It is inserted at the end of file if signature-insert-at-eof is non-nil,
123 and otherwise at the current point.  A prefix argument enables user to
124 specify a file named <signature-file-name>-DISTRIBUTION interactively."
125   (interactive "P")
126   (let ((signature-file-name
127          (expand-file-name
128           (or (and signature-use-bbdb
129                    (signature/get-sigtype-from-bbdb arg))
130               (and arg
131                    (signature/get-sigtype-interactively))
132               (signature/get-signature-file-name))
133           )))
134     (or (file-readable-p signature-file-name)
135         (error "Cannot open signature file: %s" signature-file-name))
136     (if signature-insert-at-eof
137         (progn
138           (goto-char (point-max))
139           (or (bolp) (insert "\n"))
140           (if signature-delete-blank-lines-at-eof (delete-blank-lines))
141           ))
142     (run-hooks 'signature-insert-hook)
143     (if (= (point)(point-max))
144         (insert signature-separator)
145       )
146     (insert-file-contents signature-file-name)
147     (force-mode-line-update)
148     signature-file-name))
149
150
151 ;;; @ end
152 ;;;
153
154 (provide 'signature)
155
156 (run-hooks 'signature-load-hook)
157
158 ;;; signature.el ends here