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