tm 7.34.
[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 7.1 1995/12/14 06:22:20 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     (if (file-readable-p signature)
102         (progn
103           (goto-char (point-max))
104           (if (not (bolp))
105               (insert "\n"))
106           ;;(delete-blank-lines)
107           (insert-file-contents signature)
108           (set-buffer-modified-p (buffer-modified-p))
109                                         ; force mode line update
110           ))
111     signature))
112
113 (defun insert-signature (&optional arg)
114   "Insert the file named by signature-file-name.  It is inserted at the
115 end of file if signature-insert-at-eof in non-nil, and otherwise at
116 the current point.  A prefix argument enables user to specify a file
117 named <signature-file-name>-DISTRIBUTION interactively."
118   (interactive "P")
119   (if signature-insert-at-eof
120         (call-interactively 'signature/insert-signature-at-eof)
121     (call-interactively 'signature/insert-signature-at-point)))
122
123
124 ;;; @ end
125 ;;;
126
127 (provide 'signature)