Exclude subdirs.el.
[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.
1abe8488
KH
38Suitable header fields are `To', `From', `CC' and `BCC', `Reply-to', and
39their `Resent-' variants.
40
96846422
RS
41Optional second arg EXCLUDE may be a regular expression defining text to be
42removed from alias expansions."
4351ed67 43 (sendmail-synch-aliases)
80a677d9
JA
44 (if (eq mail-aliases t)
45 (progn (setq mail-aliases nil) (build-mail-aliases)))
46 (goto-char beg)
47 (setq end (set-marker (make-marker) end))
48 (let ((case-fold-search nil))
49 (while (let ((case-fold-search t))
1abe8488 50 (re-search-forward "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):" end t))
80a677d9
JA
51 (skip-chars-forward " \t")
52 (let ((beg1 (point))
53 end1 pos epos seplen
54 ;; DISABLED-ALIASES records aliases temporarily disabled
55 ;; while we scan text that resulted from expanding those aliases.
56 ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN
57 ;; is where to reenable the alias (expressed as number of chars
58 ;; counting from END1).
59 (disabled-aliases nil))
60 (re-search-forward "^[^ \t]" end 'move)
61 (beginning-of-line)
62 (skip-chars-backward " \t\n")
63 (setq end1 (point-marker))
64 (goto-char beg1)
65 (while (< (point) end1)
66 (setq pos (point))
67 ;; Reenable any aliases which were disabled for ranges
68 ;; that we have passed out of.
69 (while (and disabled-aliases (> pos (- end1 (cdr (car disabled-aliases)))))
70 (setq disabled-aliases (cdr disabled-aliases)))
71 ;; EPOS gets position of end of next name;
72 ;; SEPLEN gets length of whitespace&separator that follows it.
73 (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t)
74 (setq epos (match-beginning 0)
75 seplen (- (point) epos))
76 (setq epos (marker-position end1) seplen 0))
77 (let (translation
2bc9b0c7 78 (string (buffer-substring-no-properties pos epos)))
80a677d9
JA
79 (if (and (not (assoc string disabled-aliases))
80 (setq translation
81 (cdr (assoc string mail-aliases))))
82 (progn
83 ;; This name is an alias. Disable it.
84 (setq disabled-aliases (cons (cons string (- end1 epos))
85 disabled-aliases))
86 ;; Replace the alias with its expansion
87 ;; then rescan the expansion for more aliases.
88 (goto-char pos)
89 (insert translation)
90 (if exclude
91 (let ((regexp
92 (concat "\\b\\(" exclude "\\)\\b"))
93 (end (point-marker)))
94 (goto-char pos)
95 (while (re-search-forward regexp end t)
96 (replace-match ""))
97 (goto-char end)))
98 (delete-region (point) (+ (point) (- epos pos)))
99 (goto-char pos))
100 ;; Name is not an alias. Skip to start of next name.
101 (goto-char epos)
102 (forward-char seplen))))
103 (set-marker end1 nil)))
104 (set-marker end nil)))
105
9d73ab0d
NF
106;; Called by mail-setup, or similar functions, only if the file specified
107;; by mail-personal-alias-file (usually `~/.mailrc') exists.
80a677d9 108(defun build-mail-aliases (&optional file)
9d73ab0d
NF
109 "Read mail aliases from personal aliases file and set `mail-aliases'.
110By default, this is the file specified by `mail-personal-alias-file'."
111 (setq file (expand-file-name (or file mail-personal-alias-file)))
80a677d9
JA
112 (let ((buffer nil)
113 (obuf (current-buffer)))
114 (unwind-protect
115 (progn
116 (setq buffer (generate-new-buffer "mailrc"))
117 (buffer-disable-undo buffer)
118 (set-buffer buffer)
3c55fda0
RS
119 (while file
120 (cond ((get-file-buffer file)
121 (insert (save-excursion
122 (set-buffer (get-file-buffer file))
123 (buffer-substring (point-min) (point-max)))))
124 ((file-exists-p file) (insert-file-contents file))
125 ((file-exists-p (setq file (concat "~/" file)))
126 (insert-file-contents file))
127 (t (setq file nil)))
128 ;; Don't lose if no final newline.
129 (goto-char (point-max))
130 (or (eq (preceding-char) ?\n) (newline))
131 (goto-char (point-min))
132 ;; handle "\\\n" continuation lines
133 (while (not (eobp))
134 (end-of-line)
135 (if (= (preceding-char) ?\\)
136 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
80a677d9 137 (forward-char 1)))
3c55fda0
RS
138 (goto-char (point-min))
139 ;; handle `source' directives -- Eddy/1994/May/25
140 (cond ((re-search-forward "^source[ \t]+" nil t)
141 (re-search-forward "\\S-+")
142 (setq file
143 (buffer-substring (match-beginning 0) (match-end 0)))
144 (beginning-of-line)
145 (insert "# ") ; to ensure we don't re-process this file
146 (beginning-of-line))
147 (t (setq file nil))))
80a677d9 148 (goto-char (point-min))
d9817d8c
FP
149 (while (re-search-forward
150 "^\\(a\\|alias\\|g\\|group\\)[ \t]+\\([^ \t]+\\)" nil t)
151 (let* ((name (match-string 2))
80a677d9
JA
152 (start (progn (skip-chars-forward " \t") (point))))
153 (end-of-line)
154 (define-mail-alias
155 name
37e379dd
RS
156 (buffer-substring start (point))
157 t)))
80a677d9
JA
158 mail-aliases)
159 (if buffer (kill-buffer buffer))
160 (set-buffer obuf))))
161
162;; Always autoloadable in case the user wants to define aliases
163;; interactively or in .emacs.
7229064d 164;;;###autoload
37e379dd 165(defun define-mail-alias (name definition &optional from-mailrc-file)
96846422 166 "Define NAME as a mail alias that translates to DEFINITION.
80a677d9 167This means that sending a message to NAME will actually send to DEFINITION.
fa786521
RS
168
169Normally, the addresses in DEFINITION must be separated by commas.
170If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION
171can be separated by spaces; an address can contain spaces
172if it is quoted with double-quotes."
173
80a677d9
JA
174 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
175 ;; Read the defaults first, if we have not done so.
4351ed67 176 (sendmail-synch-aliases)
80a677d9
JA
177 (if (eq mail-aliases t)
178 (progn
179 (setq mail-aliases nil)
9d73ab0d 180 (if (file-exists-p mail-personal-alias-file)
80a677d9 181 (build-mail-aliases))))
3af1a1f3
RS
182 ;; strip garbage from front and end
183 (if (string-match "\\`[ \t\n,]+" definition)
aa228418 184 (setq definition (substring definition (match-end 0))))
3af1a1f3 185 (if (string-match "[ \t\n,]+\\'" definition)
aa228418 186 (setq definition (substring definition 0 (match-beginning 0))))
37e379dd 187 (let ((result '())
cc4b6c02
RS
188 ;; If DEFINITION is null string, avoid looping even once.
189 (start (and (not (equal definition "")) 0))
37e379dd
RS
190 (L (length definition))
191 end tem)
192 (while start
193 ;; If we're reading from the mailrc file, then addresses are delimited
194 ;; by spaces, and addresses with embedded spaces must be surrounded by
195 ;; double-quotes. Otherwise, addresses are separated by commas.
196 (if from-mailrc-file
197 (if (eq ?\" (aref definition start))
198 (setq start (1+ start)
199 end (string-match "\"[ \t,]*" definition start))
cc4b6c02
RS
200 (setq end (string-match "[ \t,]+" definition start)))
201 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start)))
37e379dd
RS
202 (setq result (cons (substring definition start end) result))
203 (setq start (and end
204 (/= (match-end 0) L)
205 (match-end 0))))
206 (setq definition (mapconcat (function identity)
207 (nreverse result)
208 ", "))
80a677d9
JA
209 (setq tem (assoc name mail-aliases))
210 (if tem
211 (rplacd tem definition)
212 (setq mail-aliases (cons (cons name definition) mail-aliases)))))
6594deb0 213
e8a57935
JB
214(provide 'mailalias)
215
6594deb0 216;;; mailalias.el ends here