324ace76a7c4232fcb546bb2b5386310e6aba69d
[elisp/semi.git] / signature.el
1 ;;;
2 ;;; signature.el --- signature utility for GNU Emacs
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1994,1995 MORIOKA Tomohiko
6 ;;; Copyright (C) 1994 OKABE Yasuo
7 ;;;
8 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;;         OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp> (1994/08/01)
10 ;;; Created: 1994/7/11
11 ;;; Version:
12 ;;;     $Id: signature.el,v 7.3 1995/12/25 05:29:00 morioka Exp $
13 ;;; Keywords: mail, news, signature
14 ;;;
15 ;;; This file is part of tm (Tools for MIME).
16 ;;;
17 ;;; This program is free software; you can redistribute it and/or
18 ;;; modify it under the terms of the GNU General Public License as
19 ;;; published by the Free Software Foundation; either version 2, or
20 ;;; (at your option) any later version.
21 ;;;
22 ;;; This program is distributed in the hope that it will be useful,
23 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25 ;;; General Public License for more details.
26 ;;;
27 ;;; You should have received a copy of the GNU General Public License
28 ;;; along with This program.  If not, write to the Free Software
29 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;;;
31 ;;; Code:
32
33 (require 'tl-822)
34
35 (defvar signature-insert-at-eof nil
36   "*Insert signature at the end of file if non-nil. [signature.el]")
37
38 (defvar signature-delete-blank-lines-at-eof nil
39   "*Signature-insert-at-eof deletes blank lines at the end of file
40 if non-nil. [signature.el]")
41
42 (defvar signature-file-name "~/.signature"
43   "*Name of file containing the user's signature. [signature.el]")
44
45 (defvar signature-file-alist nil)
46
47 ;;;
48 ;;; Example:
49 ;;;
50 ;;; (setq signature-file-alist
51 ;;;       '((("Newsgroups" . "zxr")   . "~/.signature-sun")
52 ;;;         (("To" . "uramimi")       . "~/.signature-sun")
53 ;;;         (("Newsgroups" . "jokes") . "~/.signature-jokes")
54 ;;;         (("To" . "tea")           . "~/.signature-jokes")
55 ;;;         (("To" . ("sim" "oku"))   . "~/.signature-formal")
56 ;;;         ))
57
58 (defun signature/get-signature-file-name ()
59   (catch 'tag
60     (let ((r signature-file-alist) cell b f)
61       (save-excursion
62         (save-restriction
63           (narrow-to-region
64            (point-min)
65            (progn
66              (goto-char (point-min))
67              (if (re-search-forward
68                   (concat "^" (regexp-quote mail-header-separator) "$")
69                   nil t)
70                  (match-beginning 0)
71                (point-max)
72                )))
73           (while r
74             (setq cell (car r))
75             (setq b (car cell))
76             (if (setq f (rfc822/get-field-body (car b)))
77                 (cond ((listp (cdr b))
78                        (let ((r (cdr b)))
79                          (while r
80                            (if (string-match (car r) f)
81                                (throw 'tag (cdr cell))
82                              )
83                            (setq r (cdr r))
84                            ))
85                        )
86                       ((stringp (cdr b))
87                        (if (string-match (cdr b) f)
88                            (throw 'tag (cdr cell))
89                          ))
90                       ))
91             (setq r (cdr r))
92             ))
93         signature-file-name))))
94
95 (defun signature/insert-signature-at-point (&optional arg)
96   "Insert the file named by signature-file-name at the current point."
97   (interactive "P")
98   (let ((signature
99          (expand-file-name
100           (if arg
101               (read-file-name "Insert your signature: "
102                               (concat signature-file-name "-")
103                               signature-file-name
104                               nil)
105             (signature/get-signature-file-name)))))
106     (insert-file-contents signature)
107     (set-buffer-modified-p (buffer-modified-p)) ; force mode line update
108     signature))
109
110 (defun signature/insert-signature-at-eof (&optional arg)
111   "Insert the file named by signature-file-name at the end of file."
112   (interactive "P")
113   (let ((signature
114          (expand-file-name
115           (if arg
116               (read-file-name "Insert your signature: "
117                               (concat signature-file-name "-")
118                               signature-file-name
119                               nil)
120             (signature/get-signature-file-name)))))
121     (if (file-readable-p signature)
122         (progn
123           (goto-char (point-max))
124           (if (not (bolp))
125               (insert "\n"))
126           (if signature-delete-blank-lines-at-eof (delete-blank-lines))
127           (insert-file-contents signature)
128           (set-buffer-modified-p (buffer-modified-p))
129                                         ; force mode line update
130           ))
131     signature))
132
133 (defun insert-signature (&optional arg)
134   "Insert the file named by signature-file-name.  It is inserted at the
135 end of file if signature-insert-at-eof in non-nil, and otherwise at
136 the current point.  A prefix argument enables user to specify a file
137 named <signature-file-name>-DISTRIBUTION interactively."
138   (interactive "P")
139   (if signature-insert-at-eof
140         (call-interactively 'signature/insert-signature-at-eof)
141     (call-interactively 'signature/insert-signature-at-point)))
142
143
144 ;;; @ end
145 ;;;
146
147 (provide 'signature)