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