*** empty log message ***
[emacs.git] / lisp / mail / mailalias.el
blob792514330fb693ed19295d706898a93ded55fe81
1 ;; Expand mailing address aliases defined in ~/.mailrc.
2 ;; Copyright (C) 1985, 1987 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; Called from sendmail-send-it, or similar functions,
22 ;; only if some mail aliases are defined.
23 (defun expand-mail-aliases (beg end &optional exclude)
24 "Expand all mail aliases in suitable header fields found between BEG and END.
25 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
26 Optional second arg EXCLUDE may be a regular expression defining text to be
27 removed from alias expansions."
28 (if (eq mail-aliases t)
29 (progn (setq mail-aliases nil) (build-mail-aliases)))
30 (goto-char beg)
31 (setq end (set-marker (make-marker) end))
32 (let ((case-fold-search nil))
33 (while (let ((case-fold-search t))
34 (re-search-forward "^\\(to\\|cc\\|bcc\\|resent-to\\|resent-cc\\|resent-bcc\\):" end t))
35 (skip-chars-forward " \t")
36 (let ((beg1 (point))
37 end1 pos epos seplen
38 ;; DISABLED-ALIASES records aliases temporarily disabled
39 ;; while we scan text that resulted from expanding those aliases.
40 ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN
41 ;; is where to reenable the alias (expressed as number of chars
42 ;; counting from END1).
43 (disabled-aliases nil))
44 (re-search-forward "^[^ \t]" end 'move)
45 (beginning-of-line)
46 (skip-chars-backward " \t\n")
47 (setq end1 (point-marker))
48 (goto-char beg1)
49 (while (< (point) end1)
50 (setq pos (point))
51 ;; Reenable any aliases which were disabled for ranges
52 ;; that we have passed out of.
53 (while (and disabled-aliases (> pos (- end1 (cdr (car disabled-aliases)))))
54 (setq disabled-aliases (cdr disabled-aliases)))
55 ;; EPOS gets position of end of next name;
56 ;; SEPLEN gets length of whitespace&separator that follows it.
57 (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t)
58 (setq epos (match-beginning 0)
59 seplen (- (point) epos))
60 (setq epos (marker-position end1) seplen 0))
61 (let (translation
62 (string (buffer-substring pos epos)))
63 (if (and (not (assoc string disabled-aliases))
64 (setq translation
65 (cdr (assoc string mail-aliases))))
66 (progn
67 ;; This name is an alias. Disable it.
68 (setq disabled-aliases (cons (cons string (- end1 epos))
69 disabled-aliases))
70 ;; Replace the alias with its expansion
71 ;; then rescan the expansion for more aliases.
72 (goto-char pos)
73 (insert translation)
74 (if exclude
75 (let ((regexp
76 (concat "\\b\\(" exclude "\\)\\b"))
77 (end (point-marker)))
78 (goto-char pos)
79 (while (re-search-forward regexp end t)
80 (replace-match ""))
81 (goto-char end)))
82 (delete-region (point) (+ (point) (- epos pos)))
83 (goto-char pos))
84 ;; Name is not an alias. Skip to start of next name.
85 (goto-char epos)
86 (forward-char seplen))))
87 (set-marker end1 nil)))
88 (set-marker end nil)))
90 ;; Called by mail-setup, or similar functions, only if ~/.mailrc exists.
91 (defun build-mail-aliases (&optional file)
92 "Read mail aliases from ~/.mailrc and set `mail-aliases'."
93 (setq file (expand-file-name (or file "~/.mailrc")))
94 (let ((buffer nil)
95 (obuf (current-buffer)))
96 (unwind-protect
97 (progn
98 (setq buffer (generate-new-buffer "mailrc"))
99 (buffer-disable-undo buffer)
100 (set-buffer buffer)
101 (cond ((get-file-buffer file)
102 (insert (save-excursion
103 (set-buffer (get-file-buffer file))
104 (buffer-substring (point-min) (point-max)))))
105 ((not (file-exists-p file)))
106 (t (insert-file-contents file)))
107 ;; Don't lose if no final newline.
108 (goto-char (point-max))
109 (or (eq (preceding-char) ?\n) (newline))
110 (goto-char (point-min))
111 ;; handle "\\\n" continuation lines
112 (while (not (eobp))
113 (end-of-line)
114 (if (= (preceding-char) ?\\)
115 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
116 (forward-char 1)))
117 (goto-char (point-min))
118 (while (or (re-search-forward "^a\\(lias\\|\\)[ \t]+" nil t)
119 (re-search-forward "^g\\(roup\\|\\)[ \t]+" nil t))
120 (re-search-forward "[^ \t]+")
121 (let* ((name (buffer-substring (match-beginning 0) (match-end 0)))
122 (start (progn (skip-chars-forward " \t") (point))))
123 (end-of-line)
124 (define-mail-alias
125 name
126 (buffer-substring start (point)))))
127 mail-aliases)
128 (if buffer (kill-buffer buffer))
129 (set-buffer obuf))))
131 ;; Always autoloadable in case the user wants to define aliases
132 ;; interactively or in .emacs.
133 (defun define-mail-alias (name definition)
134 "Define NAME as a mail alias that translates to DEFINITION.
135 This means that sending a message to NAME will actually send to DEFINITION.
136 DEFINITION can be one or more mail addresses separated by commas."
137 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
138 ;; Read the defaults first, if we have not done so.
139 (if (eq mail-aliases t)
140 (progn
141 (setq mail-aliases nil)
142 (if (file-exists-p "~/.mailrc")
143 (build-mail-aliases))))
144 (let (tem)
145 ;; ~/.mailrc contains addresses separated by spaces.
146 ;; mailers should expect addresses separated by commas.
147 (while (setq tem (string-match "[^ \t,][ \t,]+" definition tem))
148 (if (= (match-end 0) (length definition))
149 (setq definition (substring definition 0 (1+ tem)))
150 (setq definition (concat (substring definition
151 0 (1+ tem))
152 ", "
153 (substring definition (match-end 0))))
154 (setq tem (+ 3 tem))))
155 (setq tem (assoc name mail-aliases))
156 (if tem
157 (rplacd tem definition)
158 (setq mail-aliases (cons (cons name definition) mail-aliases)))))