e577f907ad1697f607ae7d4ed755bb26897bef1d
[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 ;;; Version:
11 ;;;     $Id: signature.el,v 4.0 1995/10/26 09:25:23 morioka Exp $
12 ;;; Keywords: mail, news, signature
13 ;;;
14 ;;; This file is part of tm (Tools for MIME).
15 ;;;
16
17 (require 'tl-822)
18
19 (defvar signature-insert-at-eof nil
20   "*Insert signature at the end of file if non-nil.")
21
22 (defvar signature-file-name "~/.signature"
23   "*Name of file containing the user's signature.")
24
25 (defvar signature-file-alist nil)
26
27 ;;;
28 ;;; Example:
29 ;;;
30 ;;; (setq signature-file-alist
31 ;;;       '((("Newsgroups" . "zxr")   . "~/.signature-sun")
32 ;;;         (("To" . "uramimi")       . "~/.signature-sun")
33 ;;;         (("Newsgroups" . "jokes") . "~/.signature-jokes")
34 ;;;         (("To" . "tea")           . "~/.signature-jokes")
35 ;;;         (("To" . ("sim" "oku"))   . "~/.signature-formal")
36 ;;;         ))
37
38 (defun signature/get-signature-file-name ()
39   (catch 'tag
40     (let ((r signature-file-alist) cell b f)
41       (save-excursion
42         (save-restriction
43           (narrow-to-region
44            (point-min)
45            (progn
46              (goto-char (point-min))
47              (if (re-search-forward
48                   (concat "^" (regexp-quote mail-header-separator) "$")
49                   nil t)
50                  (match-beginning 0)
51                (point-max)
52                )))
53           (while r
54             (setq cell (car r))
55             (setq b (car cell))
56             (if (setq f (rfc822/get-field-body (car b)))
57                 (cond ((listp (cdr b))
58                        (let ((r (cdr b)))
59                          (while r
60                            (if (string-match (car r) f)
61                                (throw 'tag (cdr cell))
62                              )
63                            (setq r (cdr r))
64                            ))
65                        )
66                       ((stringp (cdr b))
67                        (if (string-match (cdr b) f)
68                            (throw 'tag (cdr cell))
69                          ))
70                       ))
71             (setq r (cdr r))
72             ))
73         signature-file-name))))
74
75 (defun signature/insert-signature-at-point (&optional arg)
76   "Insert the file named by signature-file-name at the current point."
77   (interactive "P")
78   (let ((signature
79          (expand-file-name
80           (if arg
81               (read-file-name "Insert your signature: "
82                               (concat signature-file-name "-")
83                               signature-file-name
84                               nil)
85             (signature/get-signature-file-name)))))
86     (insert-file-contents signature)
87     (set-buffer-modified-p (buffer-modified-p)) ; force mode line update
88     signature))
89
90 (defun signature/insert-signature-at-eof (&optional arg)
91   "Insert the file named by signature-file-name at the end of file."
92   (interactive "P")
93   (let ((signature
94          (expand-file-name
95           (if arg
96               (read-file-name "Insert your signature: "
97                               (concat signature-file-name "-")
98                               signature-file-name
99                               nil)
100             (signature/get-signature-file-name)))))
101     (save-excursion
102       (if (file-readable-p signature)
103           (progn
104             (goto-char (point-max))
105             (if (not (bolp))
106                 (insert "\n"))
107             (delete-blank-lines)
108             (insert-file-contents signature)
109             (set-buffer-modified-p (buffer-modified-p))
110                                         ; force mode line update
111             )))
112     signature))
113
114 (defun insert-signature (&optional arg)
115   "Insert the file named by signature-file-name.  It is inserted at the
116 end of file if signature-insert-at-eof in non-nil, and otherwise at
117 the current point.  A prefix argument enables user to specify a file
118 named <signature-file-name>-DISTRIBUTION interactively."
119   (interactive "P")
120   (if signature-insert-at-eof
121         (call-interactively 'signature/insert-signature-at-eof)
122     (call-interactively 'signature/insert-signature-at-point)))
123
124
125 ;;; @ end
126 ;;;
127
128 (provide 'signature)