Fix May 6 change.
[bpt/emacs.git] / lisp / mail / mailalias.el
CommitLineData
6594deb0
ER
1;;; mailalias.el --- expand mailing address aliases defined in ~/.mailrc.
2
3a801d0c
ER
3;; Copyright (C) 1985, 1987 Free Software Foundation, Inc.
4
e5167999 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: mail
e5167999 7
80a677d9
JA
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
80a677d9
JA
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to
22;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
e41b2db1
ER
24;;; Commentary:
25
26;; Basic functions for defining and expanding mail aliases.
27;; These seal off the interface to the alias-definition parts of a
28;; .mailrc file formatted for BSD's Mail or USL's mailx.
29
e5167999 30;;; Code:
80a677d9 31
594906dd 32(require 'sendmail)
c2db7f31 33
80a677d9
JA
34;; Called from sendmail-send-it, or similar functions,
35;; only if some mail aliases are defined.
36(defun expand-mail-aliases (beg end &optional exclude)
37 "Expand all mail aliases in suitable header fields found between BEG and END.
96846422
RS
38Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
39Optional second arg EXCLUDE may be a regular expression defining text to be
40removed from alias expansions."
4351ed67 41 (sendmail-synch-aliases)
80a677d9
JA
42 (if (eq mail-aliases t)
43 (progn (setq mail-aliases nil) (build-mail-aliases)))
44 (goto-char beg)
45 (setq end (set-marker (make-marker) end))
46 (let ((case-fold-search nil))
47 (while (let ((case-fold-search t))
6f8c78e1 48 (re-search-forward "^\\(to\\|cc\\|bcc\\|resent-to\\|resent-cc\\|resent-bcc\\):" end t))
80a677d9
JA
49 (skip-chars-forward " \t")
50 (let ((beg1 (point))
51 end1 pos epos seplen
52 ;; DISABLED-ALIASES records aliases temporarily disabled
53 ;; while we scan text that resulted from expanding those aliases.
54 ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN
55 ;; is where to reenable the alias (expressed as number of chars
56 ;; counting from END1).
57 (disabled-aliases nil))
58 (re-search-forward "^[^ \t]" end 'move)
59 (beginning-of-line)
60 (skip-chars-backward " \t\n")
61 (setq end1 (point-marker))
62 (goto-char beg1)
63 (while (< (point) end1)
64 (setq pos (point))
65 ;; Reenable any aliases which were disabled for ranges
66 ;; that we have passed out of.
67 (while (and disabled-aliases (> pos (- end1 (cdr (car disabled-aliases)))))
68 (setq disabled-aliases (cdr disabled-aliases)))
69 ;; EPOS gets position of end of next name;
70 ;; SEPLEN gets length of whitespace&separator that follows it.
71 (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t)
72 (setq epos (match-beginning 0)
73 seplen (- (point) epos))
74 (setq epos (marker-position end1) seplen 0))
75 (let (translation
2bc9b0c7 76 (string (buffer-substring-no-properties pos epos)))
80a677d9
JA
77 (if (and (not (assoc string disabled-aliases))
78 (setq translation
79 (cdr (assoc string mail-aliases))))
80 (progn
81 ;; This name is an alias. Disable it.
82 (setq disabled-aliases (cons (cons string (- end1 epos))
83 disabled-aliases))
84 ;; Replace the alias with its expansion
85 ;; then rescan the expansion for more aliases.
86 (goto-char pos)
87 (insert translation)
88 (if exclude
89 (let ((regexp
90 (concat "\\b\\(" exclude "\\)\\b"))
91 (end (point-marker)))
92 (goto-char pos)
93 (while (re-search-forward regexp end t)
94 (replace-match ""))
95 (goto-char end)))
96 (delete-region (point) (+ (point) (- epos pos)))
97 (goto-char pos))
98 ;; Name is not an alias. Skip to start of next name.
99 (goto-char epos)
100 (forward-char seplen))))
101 (set-marker end1 nil)))
102 (set-marker end nil)))
103
9d73ab0d
NF
104;; Called by mail-setup, or similar functions, only if the file specified
105;; by mail-personal-alias-file (usually `~/.mailrc') exists.
80a677d9 106(defun build-mail-aliases (&optional file)
9d73ab0d
NF
107 "Read mail aliases from personal aliases file and set `mail-aliases'.
108By default, this is the file specified by `mail-personal-alias-file'."
109 (setq file (expand-file-name (or file mail-personal-alias-file)))
80a677d9
JA
110 (let ((buffer nil)
111 (obuf (current-buffer)))
112 (unwind-protect
113 (progn
114 (setq buffer (generate-new-buffer "mailrc"))
115 (buffer-disable-undo buffer)
116 (set-buffer buffer)
3c55fda0
RS
117 (while file
118 (cond ((get-file-buffer file)
119 (insert (save-excursion
120 (set-buffer (get-file-buffer file))
121 (buffer-substring (point-min) (point-max)))))
122 ((file-exists-p file) (insert-file-contents file))
123 ((file-exists-p (setq file (concat "~/" file)))
124 (insert-file-contents file))
125 (t (setq file nil)))
126 ;; Don't lose if no final newline.
127 (goto-char (point-max))
128 (or (eq (preceding-char) ?\n) (newline))
129 (goto-char (point-min))
130 ;; handle "\\\n" continuation lines
131 (while (not (eobp))
132 (end-of-line)
133 (if (= (preceding-char) ?\\)
134 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
80a677d9 135 (forward-char 1)))
3c55fda0
RS
136 (goto-char (point-min))
137 ;; handle `source' directives -- Eddy/1994/May/25
138 (cond ((re-search-forward "^source[ \t]+" nil t)
139 (re-search-forward "\\S-+")
140 (setq file
141 (buffer-substring (match-beginning 0) (match-end 0)))
142 (beginning-of-line)
143 (insert "# ") ; to ensure we don't re-process this file
144 (beginning-of-line))
145 (t (setq file nil))))
80a677d9
JA
146 (goto-char (point-min))
147 (while (or (re-search-forward "^a\\(lias\\|\\)[ \t]+" nil t)
148 (re-search-forward "^g\\(roup\\|\\)[ \t]+" nil t))
149 (re-search-forward "[^ \t]+")
150 (let* ((name (buffer-substring (match-beginning 0) (match-end 0)))
151 (start (progn (skip-chars-forward " \t") (point))))
152 (end-of-line)
153 (define-mail-alias
154 name
37e379dd
RS
155 (buffer-substring start (point))
156 t)))
80a677d9
JA
157 mail-aliases)
158 (if buffer (kill-buffer buffer))
159 (set-buffer obuf))))
160
161;; Always autoloadable in case the user wants to define aliases
162;; interactively or in .emacs.
7229064d 163;;;###autoload
37e379dd 164(defun define-mail-alias (name definition &optional from-mailrc-file)
96846422 165 "Define NAME as a mail alias that translates to DEFINITION.
80a677d9 166This means that sending a message to NAME will actually send to DEFINITION.
d066345b
RS
167DEFINITION can be one or more mail addresses separated by spaces.
168An address can contain spaces if it is quoted with double-quotes."
80a677d9
JA
169 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
170 ;; Read the defaults first, if we have not done so.
4351ed67 171 (sendmail-synch-aliases)
80a677d9
JA
172 (if (eq mail-aliases t)
173 (progn
174 (setq mail-aliases nil)
9d73ab0d 175 (if (file-exists-p mail-personal-alias-file)
80a677d9 176 (build-mail-aliases))))
3af1a1f3
RS
177 ;; strip garbage from front and end
178 (if (string-match "\\`[ \t\n,]+" definition)
aa228418 179 (setq definition (substring definition (match-end 0))))
3af1a1f3 180 (if (string-match "[ \t\n,]+\\'" definition)
aa228418 181 (setq definition (substring definition 0 (match-beginning 0))))
37e379dd 182 (let ((result '())
cc4b6c02
RS
183 ;; If DEFINITION is null string, avoid looping even once.
184 (start (and (not (equal definition "")) 0))
37e379dd
RS
185 (L (length definition))
186 end tem)
187 (while start
188 ;; If we're reading from the mailrc file, then addresses are delimited
189 ;; by spaces, and addresses with embedded spaces must be surrounded by
190 ;; double-quotes. Otherwise, addresses are separated by commas.
191 (if from-mailrc-file
192 (if (eq ?\" (aref definition start))
193 (setq start (1+ start)
194 end (string-match "\"[ \t,]*" definition start))
cc4b6c02
RS
195 (setq end (string-match "[ \t,]+" definition start)))
196 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start)))
37e379dd
RS
197 (setq result (cons (substring definition start end) result))
198 (setq start (and end
199 (/= (match-end 0) L)
200 (match-end 0))))
201 (setq definition (mapconcat (function identity)
202 (nreverse result)
203 ", "))
80a677d9
JA
204 (setq tem (assoc name mail-aliases))
205 (if tem
206 (rplacd tem definition)
207 (setq mail-aliases (cons (cons name definition) mail-aliases)))))
6594deb0 208
e8a57935
JB
209(provide 'mailalias)
210
6594deb0 211;;; mailalias.el ends here