Mercurial > emacs
annotate lisp/mail/mailalias.el @ 9460:9f5fef5890c9
(test_completion): Fix reversed condition.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Wed, 12 Oct 1994 01:39:10 +0000 |
parents | e7fa0346e8f8 |
children | 2c7688eb344e |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
1 ;;; mailalias.el --- expand mailing address aliases defined in ~/.mailrc. |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
3 ;; Copyright (C) 1985, 1987 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
5 ;; Maintainer: FSF |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
6 ;; Keywords: mail |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
7 |
53 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
53 | 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 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1448
diff
changeset
|
24 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1448
diff
changeset
|
25 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1448
diff
changeset
|
26 ;; Basic functions for defining and expanding mail aliases. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1448
diff
changeset
|
27 ;; These seal off the interface to the alias-definition parts of a |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1448
diff
changeset
|
28 ;; .mailrc file formatted for BSD's Mail or USL's mailx. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1448
diff
changeset
|
29 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
30 ;;; Code: |
53 | 31 |
8801
eee06aef6f56
Require sendmail. Delete redundant defvar.
Karl Heuer <kwzh@gnu.org>
parents:
8494
diff
changeset
|
32 (require 'sendmail) |
1448
99fb20ef1a17
(mail-aliases): Add definition here.
Richard M. Stallman <rms@gnu.org>
parents:
1434
diff
changeset
|
33 |
53 | 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. | |
221
ce8e0192266e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
219
diff
changeset
|
38 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants. |
ce8e0192266e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
219
diff
changeset
|
39 Optional second arg EXCLUDE may be a regular expression defining text to be |
ce8e0192266e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
219
diff
changeset
|
40 removed from alias expansions." |
9005
e7fa0346e8f8
(expand-mail-aliases, define-mail-alias): Change sync-mail-aliases to
Karl Heuer <kwzh@gnu.org>
parents:
8801
diff
changeset
|
41 (sendmail-synch-aliases) |
53 | 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)) | |
219 | 48 (re-search-forward "^\\(to\\|cc\\|bcc\\|resent-to\\|resent-cc\\|resent-bcc\\):" end t)) |
53 | 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 | |
76 (string (buffer-substring pos epos))) | |
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 | |
104 ;; Called by mail-setup, or similar functions, only if ~/.mailrc exists. | |
105 (defun build-mail-aliases (&optional file) | |
1434 | 106 "Read mail aliases from `~/.mailrc' and set `mail-aliases'." |
7732
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
107 (setq file (expand-file-name (or file (or (getenv "MAILRC") "~/.mailrc")))) |
53 | 108 (let ((buffer nil) |
109 (obuf (current-buffer))) | |
110 (unwind-protect | |
111 (progn | |
112 (setq buffer (generate-new-buffer "mailrc")) | |
113 (buffer-disable-undo buffer) | |
114 (set-buffer buffer) | |
7732
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
115 (while file |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
116 (cond ((get-file-buffer file) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
117 (insert (save-excursion |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
118 (set-buffer (get-file-buffer file)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
119 (buffer-substring (point-min) (point-max))))) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
120 ((file-exists-p file) (insert-file-contents file)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
121 ((file-exists-p (setq file (concat "~/" file))) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
122 (insert-file-contents file)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
123 (t (setq file nil))) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
124 ;; Don't lose if no final newline. |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
125 (goto-char (point-max)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
126 (or (eq (preceding-char) ?\n) (newline)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
127 (goto-char (point-min)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
128 ;; handle "\\\n" continuation lines |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
129 (while (not (eobp)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
130 (end-of-line) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
131 (if (= (preceding-char) ?\\) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
132 (progn (delete-char -1) (delete-char 1) (insert ?\ )) |
53 | 133 (forward-char 1))) |
7732
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
134 (goto-char (point-min)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
135 ;; handle `source' directives -- Eddy/1994/May/25 |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
136 (cond ((re-search-forward "^source[ \t]+" nil t) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
137 (re-search-forward "\\S-+") |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
138 (setq file |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
139 (buffer-substring (match-beginning 0) (match-end 0))) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
140 (beginning-of-line) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
141 (insert "# ") ; to ensure we don't re-process this file |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
142 (beginning-of-line)) |
b99162ca5247
(build-mail-aliases): Handle source directives.
Richard M. Stallman <rms@gnu.org>
parents:
3743
diff
changeset
|
143 (t (setq file nil)))) |
53 | 144 (goto-char (point-min)) |
145 (while (or (re-search-forward "^a\\(lias\\|\\)[ \t]+" nil t) | |
146 (re-search-forward "^g\\(roup\\|\\)[ \t]+" nil t)) | |
147 (re-search-forward "[^ \t]+") | |
148 (let* ((name (buffer-substring (match-beginning 0) (match-end 0))) | |
149 (start (progn (skip-chars-forward " \t") (point)))) | |
150 (end-of-line) | |
151 (define-mail-alias | |
152 name | |
7971
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
153 (buffer-substring start (point)) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
154 t))) |
53 | 155 mail-aliases) |
156 (if buffer (kill-buffer buffer)) | |
157 (set-buffer obuf)))) | |
158 | |
159 ;; Always autoloadable in case the user wants to define aliases | |
160 ;; interactively or in .emacs. | |
256 | 161 ;;;###autoload |
7971
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
162 (defun define-mail-alias (name definition &optional from-mailrc-file) |
221
ce8e0192266e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
219
diff
changeset
|
163 "Define NAME as a mail alias that translates to DEFINITION. |
53 | 164 This means that sending a message to NAME will actually send to DEFINITION. |
7970
d8bede4feaed
(define-mail-alias): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7732
diff
changeset
|
165 DEFINITION can be one or more mail addresses separated by spaces. |
d8bede4feaed
(define-mail-alias): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
7732
diff
changeset
|
166 An address can contain spaces if it is quoted with double-quotes." |
53 | 167 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ") |
168 ;; Read the defaults first, if we have not done so. | |
9005
e7fa0346e8f8
(expand-mail-aliases, define-mail-alias): Change sync-mail-aliases to
Karl Heuer <kwzh@gnu.org>
parents:
8801
diff
changeset
|
169 (sendmail-synch-aliases) |
53 | 170 (if (eq mail-aliases t) |
171 (progn | |
172 (setq mail-aliases nil) | |
173 (if (file-exists-p "~/.mailrc") | |
174 (build-mail-aliases)))) | |
3743
e67f922b73ed
(define-mail-alias): Remove excess whitespace better.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
175 ;; strip garbage from front and end |
e67f922b73ed
(define-mail-alias): Remove excess whitespace better.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
176 (if (string-match "\\`[ \t\n,]+" definition) |
474 | 177 (setq definition (substring definition (match-end 0)))) |
3743
e67f922b73ed
(define-mail-alias): Remove excess whitespace better.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
178 (if (string-match "[ \t\n,]+\\'" definition) |
474 | 179 (setq definition (substring definition 0 (match-beginning 0)))) |
7971
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
180 (let ((result '()) |
8494
ae554ccf4fb5
(define-mail-alias): Avoid error if DEFINITION is "".
Richard M. Stallman <rms@gnu.org>
parents:
7971
diff
changeset
|
181 ;; If DEFINITION is null string, avoid looping even once. |
ae554ccf4fb5
(define-mail-alias): Avoid error if DEFINITION is "".
Richard M. Stallman <rms@gnu.org>
parents:
7971
diff
changeset
|
182 (start (and (not (equal definition "")) 0)) |
7971
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
183 (L (length definition)) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
184 end tem) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
185 (while start |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
186 ;; If we're reading from the mailrc file, then addresses are delimited |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
187 ;; by spaces, and addresses with embedded spaces must be surrounded by |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
188 ;; double-quotes. Otherwise, addresses are separated by commas. |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
189 (if from-mailrc-file |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
190 (if (eq ?\" (aref definition start)) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
191 (setq start (1+ start) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
192 end (string-match "\"[ \t,]*" definition start)) |
8494
ae554ccf4fb5
(define-mail-alias): Avoid error if DEFINITION is "".
Richard M. Stallman <rms@gnu.org>
parents:
7971
diff
changeset
|
193 (setq end (string-match "[ \t,]+" definition start))) |
ae554ccf4fb5
(define-mail-alias): Avoid error if DEFINITION is "".
Richard M. Stallman <rms@gnu.org>
parents:
7971
diff
changeset
|
194 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start))) |
7971
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
195 (setq result (cons (substring definition start end) result)) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
196 (setq start (and end |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
197 (/= (match-end 0) L) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
198 (match-end 0)))) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
199 (setq definition (mapconcat (function identity) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
200 (nreverse result) |
e02c5d5d76cd
(define-mail-alias): Copy parsing code from mailabbrev.el.
Richard M. Stallman <rms@gnu.org>
parents:
7970
diff
changeset
|
201 ", ")) |
53 | 202 (setq tem (assoc name mail-aliases)) |
203 (if tem | |
204 (rplacd tem definition) | |
205 (setq mail-aliases (cons (cons name definition) mail-aliases))))) | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
206 |
923 | 207 (provide 'mailalias) |
208 | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
209 ;;; mailalias.el ends here |