459
|
1 ;;; Abbrev-expansion of mail aliases.
|
|
2 ;;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
|
|
3 ;;; Created: 19 oct 90, Jamie Zawinski <jwz@lucid.com>
|
|
4 ;;; Last change 15-dec-91. jwz
|
|
5
|
|
6 ;;; This file is part of GNU Emacs.
|
|
7
|
|
8 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
9 ;;; it under the terms of the GNU General Public License as published by
|
|
10 ;;; the Free Software Foundation; either version 1, or (at your option)
|
|
11 ;;; any later version.
|
|
12
|
|
13 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
14 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 ;;; GNU General Public License for more details.
|
|
17
|
|
18 ;;; You should have received a copy of the GNU General Public License
|
|
19 ;;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
20 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
21
|
|
22 ;;; This file ensures that, when the point is in a To:, CC:, BCC:, or From:
|
|
23 ;;; field, word-abbrevs are defined for each of your mail aliases. These
|
|
24 ;;; aliases will be defined from your .mailrc file (or the file specified by
|
|
25 ;;; the MAILRC environment variable) if it exists. Providing abbrev-mode is
|
|
26 ;;; on in your send-mail buffer, your mail aliases will expand any time you
|
|
27 ;;; type a word-delimiter at the end of an abbreviation.
|
|
28 ;;;
|
|
29 ;;; What you see is what you get: no abbreviations will be expanded after you
|
|
30 ;;; have sent the mail, unlike the old system. This means you don't suffer
|
|
31 ;;; the annoyance of having the system do things behind your back -- if an
|
|
32 ;;; address you typed is going to be rewritten, you know it immediately,
|
|
33 ;;; instead of after the mail has been sent and it's too late to do anything
|
|
34 ;;; about it. You will never again be screwed because you forgot to delete an
|
|
35 ;;; old alias from your .mailrc when a new local user arrives and is given a
|
|
36 ;;; userid which conflicts with one of your aliases, for example.
|
|
37 ;;;
|
|
38 ;;; Your mail alias abbrevs will be in effect only when the point is in an
|
|
39 ;;; appropriate header field. When in the body of the message, or other
|
|
40 ;;; header fields, the mail aliases will not expand. Rather, the normal
|
|
41 ;;; mode-specific abbrev table (mail-mode-abbrev-table) will be used if
|
|
42 ;;; defined. So if you use mail-mode specific abbrevs, this code will not
|
|
43 ;;; adversely affect you. You can control which header fields the abbrevs
|
|
44 ;;; are used in by changing the variable mail-abbrev-mode-regexp.
|
|
45 ;;;
|
|
46 ;;; If auto-fill mode is on, abbrevs will wrap at commas instead of at word
|
|
47 ;;; boundaries; also, header continuation-lines will be properly indented.
|
|
48 ;;;
|
|
49 ;;; You can also insert a mail alias with mail-interactive-insert-alias
|
|
50 ;;; (bound to C-c C-a), which prompts you for an alias (with completion)
|
|
51 ;;; and inserts its expansion at point.
|
|
52 ;;;
|
|
53 ;;; To use this code, do something like
|
|
54 ;;;
|
|
55 ;;; (setq mail-mode-hook '(lambda () (require 'mail-abbrevs)))
|
|
56 ;;;
|
|
57 ;;; This file fixes a bug in the old system which prohibited your .mailrc
|
|
58 ;;; file from having lines like
|
|
59 ;;;
|
|
60 ;;; alias someone "John Doe <doe@quux.com>"
|
|
61 ;;;
|
|
62 ;;; That is, if you want an address to have embedded spaces, simply surround it
|
|
63 ;;; with double-quotes. This is necessary because the format of the .mailrc
|
|
64 ;;; file bogusly uses spaces as address delimiters. The following line defines
|
|
65 ;;; an alias which expands to three addresses:
|
|
66 ;;;
|
|
67 ;;; alias foobar addr-1 addr-2 "address three <addr-3>"
|
|
68 ;;;
|
|
69 ;;; (This is bogus because mail-delivery programs want commas, not spaces,
|
|
70 ;;; but that's what the file format is, so we have to live with it.)
|
|
71 ;;;
|
|
72 ;;; If you like, you can call the function define-mail-alias to define your
|
|
73 ;;; mail-aliases instead of using a .mailrc file. When you call it in this
|
|
74 ;;; way, addresses are seperated by commas.
|
|
75 ;;;
|
|
76 ;;; CAVEAT: This works on most Sun systems; I have been told that some versions
|
|
77 ;;; of /bin/mail do not understand double-quotes in the .mailrc file. So you
|
|
78 ;;; should make sure your version does before including verbose addresses like
|
|
79 ;;; this. One solution to this, if you are on a system whose /bin/mail doesn't
|
|
80 ;;; work that way, (and you still want to be able to /bin/mail to send mail in
|
|
81 ;;; addition to emacs) is to define minimal aliases (without full names) in
|
|
82 ;;; your .mailrc file, and use define-mail-alias to redefine them when sending
|
|
83 ;;; mail from emacs; this way, mail sent from /bin/mail will work, and mail
|
|
84 ;;; sent from emacs will be pretty.
|
|
85 ;;;
|
|
86 ;;; Aliases in the mailrc file may be nested. If you define aliases like
|
|
87 ;;; alias group1 fred ethel
|
|
88 ;;; alias group2 larry curly moe
|
|
89 ;;; alias everybody group1 group2
|
|
90 ;;; Then when you type "everybody" on the To: line, it will be expanded to
|
|
91 ;;; fred, ethyl, larry, curly, moe
|
|
92 ;;;
|
|
93 ;;; Aliases may also contain forward references; the alias of "everybody" can
|
|
94 ;;; preceed the aliases of "group1" and "group2".
|
|
95 ;;;
|
|
96 ;;; This code also understands the "source" .mailrc command, for reading
|
|
97 ;;; aliases from some other file as well.
|
|
98 ;;;
|
|
99 ;;; To read in the contents of another .mailrc-type file from emacs, use the
|
|
100 ;;; command Meta-X merge-mail-aliases. The rebuild-mail-aliases command is
|
|
101 ;;; similar, but will delete existing aliases first.
|
|
102 ;;;
|
|
103 ;;; If you want multiple addresses seperated by a string other than ", " then
|
|
104 ;;; you can set the variable mail-alias-seperator-string to it. This has to
|
|
105 ;;; be a comma bracketed by whitespace if you want any kind of reasonable
|
|
106 ;;; behaviour.
|
|
107 ;;;
|
|
108 ;;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and
|
|
109 ;;; Noah Friedman for suggestions and bug reports.
|
|
110
|
|
111 (require 'sendmail)
|
|
112
|
|
113 (defvar mail-abbrev-mailrc-file nil
|
|
114 "Name of file with mail aliases. If nil, ~/.mailrc is used.")
|
|
115
|
|
116 (defmacro mail-abbrev-mailrc-file ()
|
|
117 '(or mail-abbrev-mailrc-file
|
|
118 (setq mail-abbrev-mailrc-file
|
|
119 (or (getenv "MAILRC") "~/.mailrc"))))
|
|
120
|
|
121 ;; originally defined in sendmail.el - used to be an alist, now is a table.
|
|
122 (defvar mail-aliases nil
|
474
|
123 "Abbrev table of mail address aliases.
|
459
|
124 If this is nil, it means the aliases have not yet been initialized and
|
|
125 should be read from the .mailrc file. (This is distinct from there being
|
|
126 no aliases, which is represented by this being a table with no entries.)")
|
|
127
|
474
|
128 ;;;###autoload
|
459
|
129 (defun mail-aliases-setup ()
|
|
130 (if (and (not (vectorp mail-aliases))
|
|
131 (file-exists-p (mail-abbrev-mailrc-file)))
|
|
132 (build-mail-aliases))
|
|
133 (make-local-variable 'pre-abbrev-expand-hook)
|
|
134 (setq pre-abbrev-expand-hook
|
|
135 (cond ((and (listp pre-abbrev-expand-hook)
|
|
136 (not (eq 'lambda (car pre-abbrev-expand-hook))))
|
|
137 (cons 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook))
|
|
138 (t
|
|
139 (list 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook))))
|
|
140 (abbrev-mode 1))
|
|
141
|
|
142 ;;; Originally defined in mailalias.el. Changed to call define-mail-alias
|
|
143 ;;; with an additional argument.
|
|
144 (defun build-mail-aliases (&optional file recursivep)
|
|
145 "Read mail aliases from .mailrc and set mail-aliases."
|
|
146 (setq file (expand-file-name (or file (mail-abbrev-mailrc-file))))
|
|
147 (if (vectorp mail-aliases)
|
|
148 nil
|
|
149 (setq mail-aliases nil)
|
|
150 (define-abbrev-table 'mail-aliases '()))
|
|
151 (message "Parsing %s ..." file)
|
|
152 (let ((buffer nil)
|
|
153 (obuf (current-buffer)))
|
|
154 (unwind-protect
|
|
155 (progn
|
|
156 (setq buffer (generate-new-buffer "mailrc"))
|
|
157 (buffer-flush-undo buffer)
|
|
158 (set-buffer buffer)
|
|
159 (cond ((get-file-buffer file)
|
|
160 (insert (save-excursion
|
|
161 (set-buffer (get-file-buffer file))
|
|
162 (buffer-substring (point-min) (point-max)))))
|
|
163 ((not (file-exists-p file)))
|
|
164 (t (insert-file-contents file)))
|
|
165 ;; Don't lose if no final newline.
|
|
166 (goto-char (point-max))
|
|
167 (or (eq (preceding-char) ?\n) (newline))
|
|
168 (goto-char (point-min))
|
|
169 ;; Delete comments from the file
|
|
170 (while (search-forward "# " nil t)
|
|
171 (let ((p (- (point) 2)))
|
|
172 (end-of-line)
|
|
173 (delete-region p (point))))
|
|
174 (goto-char (point-min))
|
|
175 ;; handle "\\\n" continuation lines
|
|
176 (while (not (eobp))
|
|
177 (end-of-line)
|
|
178 (if (= (preceding-char) ?\\)
|
|
179 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
|
|
180 (forward-char 1)))
|
|
181 (goto-char (point-min))
|
|
182 (while (re-search-forward
|
|
183 "^\\(a\\(lias\\|\\)\\|g\\(roup\\)\\|source\\)[ \t]+" nil t)
|
|
184 (beginning-of-line)
|
|
185 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
|
|
186 (progn
|
|
187 (end-of-line)
|
|
188 (build-mail-aliases
|
|
189 (buffer-substring (match-beginning 1) (match-end 1)) t))
|
|
190 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
|
|
191 (let* ((name (buffer-substring
|
|
192 (match-beginning 1) (match-end 1)))
|
|
193 (start (progn (skip-chars-forward " \t") (point))))
|
|
194 (end-of-line)
|
|
195 ; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
|
|
196 (define-mail-alias
|
|
197 name
|
|
198 (buffer-substring start (point))
|
|
199 t))))
|
|
200 ;; Resolve forward references in .mailrc file.
|
|
201 ;; This would happen automatically before the first abbrev was
|
|
202 ;; expanded, but why not do it now.
|
|
203 (or recursivep (mail-resolve-all-aliases))
|
|
204 mail-aliases)
|
|
205 (if buffer (kill-buffer buffer))
|
|
206 (set-buffer obuf)))
|
|
207 (message "Parsing %s ... done" file))
|
|
208
|
|
209 (defvar mail-alias-seperator-string ", "
|
|
210 "*A string inserted between addresses in multi-address mail aliases.
|
|
211 This has to contain a comma, so \", \" is a reasonable value. You might
|
|
212 also want something like \",\\n \" to get each address on its own line.")
|
|
213
|
|
214 ;; define-mail-alias sets this flag, which causes mail-resolve-all-aliases
|
|
215 ;; to be called before expanding abbrevs if it's necessary.
|
|
216 (defvar mail-abbrev-aliases-need-to-be-resolved t)
|
|
217
|
|
218 ;; originally defined in mailalias.el ; build-mail-aliases calls this with
|
|
219 ;; stuff parsed from the .mailrc file.
|
|
220 ;;
|
474
|
221 ;;;###autoload
|
459
|
222 (defun define-mail-alias (name definition &optional from-mailrc-file)
|
|
223 "Define NAME as a mail-alias that translates to DEFINITION.
|
|
224 If DEFINITION contains multiple addresses, seperate them with commas."
|
|
225 ;; When this is called from build-mail-aliases, the third argument is
|
|
226 ;; true, and we do some evil space->comma hacking like /bin/mail does.
|
|
227 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
|
|
228 ;; Read the defaults first, if we have not done so.
|
|
229 (if (vectorp mail-aliases)
|
|
230 nil
|
|
231 (setq mail-aliases nil)
|
|
232 (define-abbrev-table 'mail-aliases '())
|
|
233 (if (file-exists-p (mail-abbrev-mailrc-file))
|
|
234 (build-mail-aliases)))
|
|
235 ;; strip garbage from front and end
|
|
236 (if (string-match "\\`[ \t\n,]+" definition)
|
|
237 (setq definition (substring definition (match-end 0))))
|
|
238 (if (string-match "[ \t\n,]+\\'" definition)
|
|
239 (setq definition (substring definition 0 (match-beginning 0))))
|
|
240 (let ((result '())
|
|
241 (start 0)
|
|
242 (L (length definition))
|
|
243 end)
|
|
244 (while start
|
|
245 ;; If we're reading from the mailrc file, then addresses are delimited
|
|
246 ;; by spaces, and addresses with embedded spaces must be surrounded by
|
|
247 ;; double-quotes. Otherwise, addresses are seperated by commas.
|
|
248 (if from-mailrc-file
|
|
249 (if (eq ?\" (aref definition start))
|
|
250 (setq start (1+ start)
|
|
251 end (string-match "\"[ \t,]*" definition start))
|
|
252 (setq end (string-match "[ \t,]+" definition start)))
|
|
253 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start)))
|
|
254 (setq result (cons (substring definition start end) result))
|
|
255 (setq start (and end
|
|
256 (/= (match-end 0) L)
|
|
257 (match-end 0))))
|
|
258 (setq definition (mapconcat (function identity)
|
|
259 (nreverse result)
|
|
260 mail-alias-seperator-string)))
|
|
261 (setq mail-abbrev-aliases-need-to-be-resolved t)
|
|
262 (setq name (downcase name))
|
|
263 ;; use an abbrev table instead of an alist for mail-aliases.
|
|
264 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed.
|
|
265 (define-abbrev mail-aliases name definition 'mail-abbrev-expand-hook)))
|
|
266
|
|
267
|
|
268 (defun mail-resolve-all-aliases ()
|
|
269 "Resolve all forward references in the mail aliases table."
|
|
270 (if mail-abbrev-aliases-need-to-be-resolved
|
|
271 (progn
|
|
272 ;; (message "Resolving mail aliases...")
|
|
273 (if (vectorp mail-aliases)
|
|
274 (mapatoms (function mail-resolve-all-aliases-1) mail-aliases))
|
|
275 (setq mail-abbrev-aliases-need-to-be-resolved nil)
|
|
276 ;; (message "Resolving mail aliases... done.")
|
|
277 )))
|
|
278
|
|
279 (defun mail-resolve-all-aliases-1 (sym)
|
|
280 (let ((definition (and (boundp sym) (symbol-value sym))))
|
|
281 (if definition
|
|
282 (let ((result '())
|
|
283 (start 0))
|
|
284 (while start
|
|
285 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start)))
|
|
286 (setq result (cons (substring definition start end) result)
|
|
287 start (and end (match-end 0)))))
|
|
288 (setq definition
|
|
289 (mapconcat (function (lambda (x)
|
|
290 (or (mail-resolve-all-aliases-1
|
|
291 (intern-soft x mail-aliases))
|
|
292 x)))
|
|
293 (nreverse result)
|
|
294 mail-alias-seperator-string))
|
|
295 (set sym definition))))
|
|
296 (symbol-value sym))
|
|
297
|
|
298
|
|
299 (defun mail-abbrev-expand-hook ()
|
474
|
300 "For use as the fourth arg to `define-abbrev'.
|
|
301 After expanding a mail alias, if Auto Fill mode is on and we're past the
|
|
302 fill column, break the line at the previous comma, and indent the next line."
|
459
|
303 (save-excursion
|
|
304 (let ((p (point))
|
|
305 bol)
|
|
306 (if (and (if (boundp 'auto-fill-function)
|
|
307 auto-fill-function
|
|
308 auto-fill-hook)
|
|
309 (>= (current-column) fill-column))
|
|
310 (progn
|
|
311 (beginning-of-line)
|
|
312 (setq bol (point))
|
|
313 (goto-char p)
|
|
314 (if (search-backward "," bol t)
|
|
315 (progn
|
|
316 (forward-char 1)
|
|
317 (insert "\n ")))
|
|
318 (if (> (current-column) fill-column)
|
|
319 (let ((fill-prefix " "))
|
|
320 (do-auto-fill)))
|
|
321 )))))
|
|
322
|
|
323
|
|
324 (defun mail-interactive-insert-alias (&optional alias)
|
|
325 "Prompt for and insert a mail alias."
|
|
326 (interactive (list (completing-read "Expand alias: " mail-aliases nil t)))
|
|
327 (insert (or (and alias (symbol-value (intern-soft alias mail-aliases))) "")))
|
|
328
|
|
329 (define-key mail-mode-map "\C-c\C-a" 'mail-interactive-insert-alias)
|
|
330
|
|
331
|
|
332 (defvar mail-abbrev-mode-regexp "^\\(To\\|From\\|CC\\|BCC\\):"
|
|
333 "*Regexp to select mail-headers in which mail-aliases should be expanded.
|
|
334 This string it will be handed to `looking-at' with the point at the beginning
|
|
335 of the current line; if it matches, abbrev mode will be turned on, otherwise
|
|
336 it will be turned off. (You don't need to worry about continuation lines.)
|
|
337 This should be set to match those mail fields in which you want abbreviations
|
|
338 turned on.")
|
|
339
|
|
340 (defvar mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table)
|
474
|
341 "The syntax table which is current in mail mode.")
|
459
|
342
|
|
343 (defvar mail-mode-header-syntax-table
|
|
344 (let ((tab (copy-syntax-table text-mode-syntax-table)))
|
|
345 ;; This makes the caracters "@%!._-" be considered symbol-consituents
|
|
346 ;; but not word-constituents, so forward-sexp will move you over an
|
|
347 ;; entire address, but forward-word will only move you over a sequence
|
|
348 ;; of alphanumerics. (Clearly the right thing.)
|
|
349 (modify-syntax-entry ?@ "_" tab)
|
|
350 (modify-syntax-entry ?% "_" tab)
|
|
351 (modify-syntax-entry ?! "_" tab)
|
|
352 (modify-syntax-entry ?. "_" tab)
|
|
353 (modify-syntax-entry ?_ "_" tab)
|
|
354 (modify-syntax-entry ?- "_" tab)
|
|
355 (modify-syntax-entry ?< "(>" tab)
|
|
356 (modify-syntax-entry ?> ")<" tab)
|
|
357 ;; I hate this more than you can possibly imagine.
|
|
358 ;; Do this if you want to have aliases with hyphens in them. This causes
|
|
359 ;; hyphens to be considered word-syntax, so forward-word will not stop at
|
|
360 ;; hyphens.
|
|
361 ;;(modify-syntax-entry ?- "w" tab)
|
|
362 tab)
|
|
363 "The syntax table used when the cursor is in a mail-address header.
|
|
364 mail-mode-syntax-table is used when the cursor is not in an address header.")
|
|
365
|
474
|
366 ;; This hook is run before trying to expand an abbrev in a mail buffer.
|
|
367 ;; It determines whether point is in the header, and chooses which
|
|
368 ;; abbrev table accordingly.
|
459
|
369 (defun sendmail-pre-abbrev-expand-hook ()
|
|
370 (if mail-abbrev-aliases-need-to-be-resolved
|
|
371 (mail-resolve-all-aliases))
|
|
372 (if (and mail-aliases (not (eq mail-aliases t)))
|
|
373 (let ((case-fold-search t))
|
|
374 (if (and ;;
|
|
375 ;; we are on an appropriate header line...
|
|
376 (save-excursion
|
|
377 (beginning-of-line)
|
|
378 ;; skip backwards over continuation lines.
|
|
379 (while (and (looking-at "^[ \t]")
|
|
380 (not (= (point) (point-min))))
|
|
381 (forward-line -1))
|
|
382 ;; are we at the front of an appropriate header line?
|
|
383 (looking-at mail-abbrev-mode-regexp))
|
|
384 ;;
|
|
385 ;; ...and we are before the mail-header-separator
|
|
386 (< (point)
|
|
387 (save-excursion
|
|
388 (goto-char (point-min))
|
|
389 (search-forward (concat "\n" mail-header-separator "\n")
|
|
390 nil 0)
|
|
391 (point))))
|
|
392 ;; install the mail-aliases abbrev and syntax tables...
|
|
393 (progn
|
|
394 (setq local-abbrev-table mail-aliases)
|
|
395 (set-syntax-table mail-mode-header-syntax-table))
|
|
396 ;; or install the normal mail-mode abbrev table (likely empty).
|
|
397 (progn
|
|
398 (setq local-abbrev-table (and (boundp 'mail-mode-abbrev-table)
|
|
399 mail-mode-abbrev-table))
|
|
400 (set-syntax-table mail-mode-syntax-table))))))
|
|
401
|
|
402
|
|
403 (defun merge-mail-aliases (file)
|
|
404 "Merge mail aliases from the given file with existing ones."
|
|
405 (interactive (list
|
|
406 (let ((insert-default-directory t)
|
|
407 (default-directory (expand-file-name "~/"))
|
|
408 (def (mail-abbrev-mailrc-file)))
|
|
409 (read-file-name
|
|
410 (format "Read additional aliases from file: (default %s) "
|
|
411 def)
|
|
412 default-directory
|
|
413 (expand-file-name def default-directory)
|
|
414 t))))
|
|
415 (build-mail-aliases file))
|
|
416
|
|
417 (defun rebuild-mail-aliases (file)
|
|
418 "Rebuild all the mail aliases from the given file."
|
|
419 (interactive (list
|
|
420 (let ((insert-default-directory t)
|
|
421 (default-directory (expand-file-name "~/"))
|
|
422 (def (mail-abbrev-mailrc-file)))
|
|
423 (read-file-name
|
|
424 (format "Read mail aliases from file: (default %s) " def)
|
|
425 default-directory
|
|
426 (expand-file-name def default-directory)
|
|
427 t))))
|
|
428 (setq mail-aliases nil)
|
|
429 (build-mail-aliases file))
|
584
|
430
|
459
|
431 (provide 'mail-abbrevs)
|
|
432
|