1 ;;; mailabbrev.el --- abbrev-expansion of mail aliases.
3 ;;; Copyright (C) 1985, 1986, 1987, 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Maintainer: Jamie Zawinski <jwz@lucid.com>
10 ;;; This file is part of GNU Emacs.
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2, or (at your option)
15 ;;; any later version.
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;;; This file ensures that, when the point is in a To:, CC:, BCC:, or From:
29 ;;; field, word-abbrevs are defined for each of your mail aliases. These
30 ;;; aliases will be defined from your .mailrc file (or the file specified by
31 ;;; the MAILRC environment variable) if it exists. Your mail aliases will
32 ;;; expand any time you type a word-delimiter at the end of an abbreviation.
34 ;;; What you see is what you get: no abbreviations will be expanded after you
35 ;;; have sent the mail, unlike the old system. This means you don't suffer
36 ;;; the annoyance of having the system do things behind your back -- if an
37 ;;; address you typed is going to be rewritten, you know it immediately,
38 ;;; instead of after the mail has been sent and it's too late to do anything
39 ;;; about it. You will never again be screwed because you forgot to delete an
40 ;;; old alias from your .mailrc when a new local user arrives and is given a
41 ;;; userid which conflicts with one of your aliases, for example.
43 ;;; Your mail alias abbrevs will be in effect only when the point is in an
44 ;;; appropriate header field. When in the body of the message, or other
45 ;;; header fields, the mail aliases will not expand. Rather, the normal
46 ;;; mode-specific abbrev table (mail-mode-abbrev-table) will be used if
47 ;;; defined. So if you use mail-mode specific abbrevs, this code will not
48 ;;; adversely affect you. You can control which header fields the abbrevs
49 ;;; are used in by changing the variable mail-abbrev-mode-regexp.
51 ;;; If auto-fill mode is on, abbrevs will wrap at commas instead of at word
52 ;;; boundaries; also, header continuation-lines will be properly indented.
54 ;;; You can also insert a mail alias with mail-interactive-insert-alias
55 ;;; (bound to C-c C-a), which prompts you for an alias (with completion)
56 ;;; and inserts its expansion at point.
58 ;;; This file fixes a bug in the old system which prohibited your .mailrc
59 ;;; file from having lines like
61 ;;; alias someone "John Doe <doe@quux.com>"
63 ;;; That is, if you want an address to have embedded spaces, simply surround it
64 ;;; with double-quotes. This is necessary because the format of the .mailrc
65 ;;; file bogusly uses spaces as address delimiters. The following line defines
66 ;;; an alias which expands to three addresses:
68 ;;; alias foobar addr-1 addr-2 "address three <addr-3>"
70 ;;; (This is bogus because mail-delivery programs want commas, not spaces,
71 ;;; but that's what the file format is, so we have to live with it.)
73 ;;; If you like, you can call the function define-mail-abbrev to define your
74 ;;; mail aliases instead of using a .mailrc file. When you call it in this
75 ;;; way, addresses are separated by commas.
77 ;;; CAVEAT: This works on most Sun systems; I have been told that some versions
78 ;;; of /bin/mail do not understand double-quotes in the .mailrc file. So you
79 ;;; should make sure your version does before including verbose addresses like
80 ;;; this. One solution to this, if you are on a system whose /bin/mail doesn't
81 ;;; work that way, (and you still want to be able to /bin/mail to send mail in
82 ;;; addition to emacs) is to define minimal aliases (without full names) in
83 ;;; your .mailrc file, and use define-mail-abbrev to redefine them when sending
84 ;;; mail from emacs; this way, mail sent from /bin/mail will work, and mail
85 ;;; sent from emacs will be pretty.
87 ;;; Aliases in the mailrc file may be nested. If you define aliases like
88 ;;; alias group1 fred ethel
89 ;;; alias group2 larry curly moe
90 ;;; alias everybody group1 group2
91 ;;; Then when you type "everybody" on the To: line, it will be expanded to
92 ;;; fred, ethyl, larry, curly, moe
94 ;;; Aliases may also contain forward references; the alias of "everybody" can
95 ;;; precede the aliases of "group1" and "group2".
97 ;;; This code also understands the "source" .mailrc command, for reading
98 ;;; aliases from some other file as well.
100 ;;; Aliases may contain hyphens, as in "alias foo-bar foo@bar"; word-abbrevs
101 ;;; normally cannot contain hyphens, but this code works around that for the
102 ;;; specific case of mail-alias word-abbrevs.
104 ;;; To read in the contents of another .mailrc-type file from emacs, use the
105 ;;; command Meta-X merge-mail-abbrevs. The rebuild-mail-abbrevs command is
106 ;;; similar, but will delete existing aliases first.
108 ;;; If you would like your aliases to be expanded when you type M-> or ^N to
109 ;;; move out of the mail-header into the message body (instead of having to
110 ;;; type SPC at the end of the abbrev before moving away) then you can do
112 ;;; (define-key mail-mode-map "\C-n" 'mail-abbrev-next-line)
113 ;;; (define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer)
115 ;;; If you want multiple addresses separated by a string other than ", " then
116 ;;; you can set the variable mail-alias-separator-string to it. This has to
117 ;;; be a comma bracketed by whitespace if you want any kind of reasonable
120 ;;; Thanks to Harald Hanche-Olsen, Michael Ernst, David Loeffler, and
121 ;;; Noah Friedman for suggestions and bug reports.
123 ;;; To use this package, do (add-hook 'mail-setup-hook 'mail-abbrevs-setup).
129 (defvar mail-abbrev-mailrc-file nil
130 "Name of file with mail aliases. If nil, ~/.mailrc is used.")
132 (defmacro mail-abbrev-mailrc-file ()
133 '(or mail-abbrev-mailrc-file
134 (setq mail-abbrev-mailrc-file
135 (or (getenv "MAILRC") "~/.mailrc"))))
137 ;; originally defined in sendmail.el - used to be an alist, now is a table.
138 (defvar mail-abbrevs nil
139 "Word-abbrev table of mail address aliases.
140 If this is nil, it means the aliases have not yet been initialized and
141 should be read from the .mailrc file. (This is distinct from there being
142 no aliases, which is represented by this being a table with no entries.)")
145 (defun mail-abbrevs-setup ()
146 (if (and (not (vectorp mail-abbrevs))
147 (file-exists-p (mail-abbrev-mailrc-file)))
148 (build-mail-abbrevs))
149 (make-local-variable 'pre-abbrev-expand-hook)
150 (setq pre-abbrev-expand-hook
151 (cond ((and (listp pre-abbrev-expand-hook)
152 (not (eq 'lambda (car pre-abbrev-expand-hook))))
153 (cons 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook))
155 (list 'sendmail-pre-abbrev-expand-hook pre-abbrev-expand-hook))))
159 (defun build-mail-abbrevs (&optional file recursivep)
160 "Read mail aliases from `~/.mailrc' file and set `mail-abbrevs'."
161 (setq file (expand-file-name (or file (mail-abbrev-mailrc-file))))
162 (if (vectorp mail-abbrevs)
164 (setq mail-abbrevs nil)
165 (define-abbrev-table 'mail-abbrevs '()))
166 (message "Parsing %s..." file)
168 (obuf (current-buffer)))
171 (setq buffer (generate-new-buffer "mailrc"))
172 (buffer-disable-undo buffer)
174 (cond ((get-file-buffer file)
175 (insert (save-excursion
176 (set-buffer (get-file-buffer file))
177 (buffer-substring (point-min) (point-max)))))
178 ((not (file-exists-p file)))
179 (t (insert-file-contents file)))
180 ;; Don't lose if no final newline.
181 (goto-char (point-max))
182 (or (eq (preceding-char) ?\n) (newline))
183 (goto-char (point-min))
184 ;; Delete comments from the file
185 (while (search-forward "# " nil t)
186 (let ((p (- (point) 2)))
188 (delete-region p (point))))
189 (goto-char (point-min))
190 ;; handle "\\\n" continuation lines
193 (if (= (preceding-char) ?\\)
194 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
196 (goto-char (point-min))
197 (while (re-search-forward
198 "^\\(a\\(lias\\)?\\|g\\(roup\\)?\\|source\\)[ \t]+" nil t)
200 (if (looking-at "source[ \t]+\\([^ \t\n]+\\)")
204 (substitute-in-file-name
205 (buffer-substring (match-beginning 1) (match-end 1)))
207 (re-search-forward "[ \t]+\\([^ \t\n]+\\)")
208 (let* ((name (buffer-substring
209 (match-beginning 1) (match-end 1)))
210 (start (progn (skip-chars-forward " \t") (point))))
212 ; (message "** %s \"%s\"" name (buffer-substring start (point)))(sit-for 1)
215 (buffer-substring start (point))
217 ;; Resolve forward references in .mailrc file.
218 ;; This would happen automatically before the first abbrev was
219 ;; expanded, but why not do it now.
220 (or recursivep (mail-resolve-all-aliases))
222 (if buffer (kill-buffer buffer))
224 (message "Parsing %s... done" file))
226 (defvar mail-alias-separator-string ", "
227 "*A string inserted between addresses in multi-address mail aliases.
228 This has to contain a comma, so \", \" is a reasonable value. You might
229 also want something like \",\\n \" to get each address on its own line.")
231 ;; define-mail-abbrev sets this flag, which causes mail-resolve-all-aliases
232 ;; to be called before expanding abbrevs if it's necessary.
233 (defvar mail-abbrev-aliases-need-to-be-resolved t)
235 ;; originally defined in mailalias.el ; build-mail-abbrevs calls this with
236 ;; stuff parsed from the .mailrc file.
239 (defun define-mail-abbrev (name definition &optional from-mailrc-file)
240 "Define NAME as a mail-abbrev that translates to DEFINITION.
241 If DEFINITION contains multiple addresses, separate them with commas."
242 ;; When this is called from build-mail-abbrevs, the third argument is
243 ;; true, and we do some evil space->comma hacking like /bin/mail does.
244 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
245 ;; Read the defaults first, if we have not done so.
246 (if (vectorp mail-abbrevs)
248 (setq mail-abbrevs nil)
249 (define-abbrev-table 'mail-abbrevs '())
250 (if (file-exists-p (mail-abbrev-mailrc-file))
251 (build-mail-abbrevs)))
252 ;; strip garbage from front and end
253 (if (string-match "\\`[ \t\n,]+" definition)
254 (setq definition (substring definition (match-end 0))))
255 (if (string-match "[ \t\n,]+\\'" definition)
256 (setq definition (substring definition 0 (match-beginning 0))))
258 (L (length definition))
259 (start (if (> L 0) 0))
262 ;; If we're reading from the mailrc file, then addresses are delimited
263 ;; by spaces, and addresses with embedded spaces must be surrounded by
264 ;; double-quotes. Otherwise, addresses are separated by commas.
266 (if (eq ?\" (aref definition start))
267 (setq start (1+ start)
268 end (string-match "\"[ \t,]*" definition start))
269 (setq end (string-match "[ \t,]+" definition start)))
270 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start)))
271 (setq result (cons (substring definition start end) result))
275 (setq definition (mapconcat (function identity)
277 mail-alias-separator-string)))
278 (setq mail-abbrev-aliases-need-to-be-resolved t)
279 (setq name (downcase name))
280 ;; use an abbrev table instead of an alist for mail-abbrevs.
281 (let ((abbrevs-changed abbrevs-changed)) ; protect this from being changed.
282 (define-abbrev mail-abbrevs name definition 'mail-abbrev-expand-hook)))
285 (defun mail-resolve-all-aliases ()
286 "Resolve all forward references in the mail aliases table."
287 (if mail-abbrev-aliases-need-to-be-resolved
289 ;; (message "Resolving mail aliases...")
290 (if (vectorp mail-abbrevs)
291 (mapatoms (function mail-resolve-all-aliases-1) mail-abbrevs))
292 (setq mail-abbrev-aliases-need-to-be-resolved nil)
293 ;; (message "Resolving mail aliases... done.")
296 (defun mail-resolve-all-aliases-1 (sym &optional so-far)
297 (if (memq sym so-far)
298 (error "mail alias loop detected: %s"
299 (mapconcat 'symbol-name (cons sym so-far) " <- ")))
300 (let ((definition (and (boundp sym) (symbol-value sym))))
305 (let ((end (string-match "[ \t\n]*,[, \t\n]*" definition start)))
306 (setq result (cons (substring definition start end) result)
307 start (and end (match-end 0)))))
309 (mapconcat (function (lambda (x)
310 (or (mail-resolve-all-aliases-1
311 (intern-soft x mail-abbrevs)
315 mail-alias-separator-string))
316 (set sym definition))))
320 (defun mail-abbrev-expand-hook ()
321 "For use as the fourth arg to define-abbrev.
322 After expanding a mail-abbrev, if fill-mode is on and we're past the
323 fill-column, break the line at the previous comma, and indent the next
331 (while (and auto-fill-function
332 (>= (current-column) fill-column)
333 (search-backward "," bol t))
335 (forward-char 1) ; Now we are just past the comma.
337 (delete-horizontal-space)
340 (setq fp (buffer-substring p (point)))
341 ;; Go to the end of the new line.
343 (if (> (current-column) fill-column)
344 ;; It's still too long; do normal auto-fill.
345 (let ((fill-prefix (or fp "\t")))
347 ;; Resume the search.
351 ;;; Syntax tables and abbrev-expansion
353 (defvar mail-abbrev-mode-regexp
354 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
355 "*Regexp to select mail-headers in which mail-abbrevs should be expanded.
356 This string it will be handed to `looking-at' with the point at the beginning
357 of the current line; if it matches, abbrev mode will be turned on, otherwise
358 it will be turned off. (You don't need to worry about continuation lines.)
359 This should be set to match those mail fields in which you want abbreviations
362 (defvar mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table)
363 "The syntax table which is used in send-mail mode message bodies.")
365 (defvar mail-mode-header-syntax-table
366 (let ((tab (copy-syntax-table text-mode-syntax-table)))
367 ;; This makes the characters "@%!._-" be considered symbol-constituents
368 ;; but not word-constituents, so forward-sexp will move you over an
369 ;; entire address, but forward-word will only move you over a sequence
370 ;; of alphanumerics. (Clearly the right thing.)
371 (modify-syntax-entry ?@ "_" tab)
372 (modify-syntax-entry ?% "_" tab)
373 (modify-syntax-entry ?! "_" tab)
374 (modify-syntax-entry ?. "_" tab)
375 (modify-syntax-entry ?_ "_" tab)
376 (modify-syntax-entry ?- "_" tab)
377 (modify-syntax-entry ?< "(>" tab)
378 (modify-syntax-entry ?> ")<" tab)
380 "The syntax table used in send-mail mode when in a mail-address header.
381 mail-mode-syntax-table is used when the cursor is in the message body or in
382 non-address headers.")
384 (defvar mail-abbrev-syntax-table
385 (let* ((tab (copy-syntax-table mail-mode-header-syntax-table))
386 (i (1- (length tab)))
387 (_ (aref (standard-syntax-table) ?_))
388 (w (aref (standard-syntax-table) ?w)))
390 (if (= (aref tab i) _) (aset tab i w))
393 "The syntax-table used for abbrev-expansion purposes; this is not actually
394 made the current syntax table of the buffer, but simply controls the set of
395 characters which may be a part of the name of a mail-alias.")
398 (defun mail-abbrev-in-expansion-header-p ()
399 "Whether point is in a mail-address header field."
400 (let ((case-fold-search t))
402 ;; we are on an appropriate header line...
405 ;; skip backwards over continuation lines.
406 (while (and (looking-at "^[ \t]")
407 (not (= (point) (point-min))))
409 ;; are we at the front of an appropriate header line?
410 (looking-at mail-abbrev-mode-regexp))
412 ;; ...and we are before the mail-header-separator
415 (goto-char (point-min))
416 (search-forward (concat "\n" mail-header-separator "\n")
420 (defvar mail-mode-abbrev-table) ; quiet the compiler
422 (defun sendmail-pre-abbrev-expand-hook ()
423 (and (and mail-abbrevs (not (eq mail-abbrevs t)))
424 (if (mail-abbrev-in-expansion-header-p)
427 ;; We are in a To: (or CC:, or whatever) header, and
428 ;; should use word-abbrevs to expand mail aliases.
430 ;; Before anything else, resolve aliases if they need it.
431 (and mail-abbrev-aliases-need-to-be-resolved
432 (mail-resolve-all-aliases))
434 ;; Now proceed with the abbrev section.
435 ;; - First, install the mail-abbrevs as the word-abbrev table.
436 ;; - Then install the mail-abbrev-syntax-table, which
437 ;; temporarily marks all of the
438 ;; non-alphanumeric-atom-characters (the "_"
439 ;; syntax ones) as being normal word-syntax. We do this
440 ;; because the C code for expand-abbrev only works on words,
441 ;; and we want these characters to be considered words for
442 ;; the purpose of abbrev expansion.
443 ;; - Then we call expand-abbrev again, recursively, to do
444 ;; the abbrev expansion with the above syntax table.
445 ;; - Then we do a trick which tells the expand-abbrev frame
446 ;; which invoked us to not continue (and thus not
447 ;; expand twice.) This means that any abbrev expansion
448 ;; will happen as a result of this function's call to
449 ;; expand-abbrev, and not as a result of the call to
450 ;; expand-abbrev which invoked *us*.
451 ;; - Then we set the syntax table to
452 ;; mail-mode-header-syntax-table, which doesn't have
453 ;; anything to do with abbrev expansion, but
454 ;; is just for the user's convenience (see its doc string.)
457 (setq local-abbrev-table mail-abbrevs)
459 ;; If the character just typed was non-alpha-symbol-syntax,
460 ;; then don't expand the abbrev now (that is, don't expand
461 ;; when the user types -.) Check the character's syntax in
462 ;; the mail-mode-header-syntax-table.
464 (set-syntax-table mail-mode-header-syntax-table)
465 (or (and (integerp last-command-char)
466 (eq (char-syntax last-command-char) ?_))
467 (let ((pre-abbrev-expand-hook nil)) ; That's us; don't loop.
468 ;; Use this table so that abbrevs can have hyphens in them.
469 (set-syntax-table mail-abbrev-syntax-table)
471 ;; Now set it back to what it was before.
472 (set-syntax-table mail-mode-header-syntax-table)))
473 (setq abbrev-start-location (point) ; This is the trick.
474 abbrev-start-location-buffer (current-buffer)))
476 ;; We're not in a mail header where mail aliases should
477 ;; be expanded, then use the normal mail-mode abbrev table
478 ;; (if any) and the normal mail-mode syntax table.
480 (setq local-abbrev-table (and (boundp 'mail-mode-abbrev-table)
481 mail-mode-abbrev-table))
482 (set-syntax-table mail-mode-syntax-table))
487 (defun merge-mail-abbrevs (file)
488 "Merge mail aliases from the given file with existing ones."
490 (let ((insert-default-directory t)
491 (default-directory (expand-file-name "~/"))
492 (def (mail-abbrev-mailrc-file)))
494 (format "Read additional aliases from file: (default %s) "
497 (expand-file-name def default-directory)
499 (build-mail-abbrevs file))
501 (defun rebuild-mail-abbrevs (file)
502 "Rebuild all the mail aliases from the given file."
504 (let ((insert-default-directory t)
505 (default-directory (expand-file-name "~/"))
506 (def (mail-abbrev-mailrc-file)))
508 (format "Read mail aliases from file: (default %s) " def)
510 (expand-file-name def default-directory)
512 (setq mail-abbrevs nil)
513 (build-mail-abbrevs file))
515 (defun mail-interactive-insert-alias (&optional alias)
516 "Prompt for and insert a mail alias."
518 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
519 (list (completing-read "Expand alias: " mail-abbrevs nil t))))
520 (if (not (vectorp mail-abbrevs)) (mail-abbrevs-setup))
521 (insert (or (and alias (symbol-value (intern-soft alias mail-abbrevs))) "")))
523 (defun mail-abbrev-next-line (&optional arg)
524 "Expand any mail abbrev, then move cursor vertically down ARG lines.
525 If there is no character in the target line exactly under the current column,
526 the cursor is positioned after the character in that line which spans this
527 column, or at the end of the line if it is not long enough.
528 If there is no line in the buffer after this one,
529 a newline character is inserted to create a line
530 and the cursor moves to that line.
532 The command \\[set-goal-column] can be used to create
533 a semipermanent goal column to which this command always moves.
534 Then it does not try to move vertically. This goal column is stored
535 in `goal-column', which is nil when there is none.
537 If you are thinking of using this in a Lisp program, consider
538 using `forward-line' instead. It is usually easier to use
539 and more reliable (no dependence on goal column, etc.)."
541 (if (looking-at "[ \t]*\n") (expand-abbrev))
542 (setq this-command 'next-line)
545 (defun mail-abbrev-end-of-buffer (&optional arg)
546 "Expand any mail abbrev, then move point to end of buffer.
547 Leave mark at previous position.
548 With arg N, put point N/10 of the way from the true end.
550 Don't use this command in Lisp programs!
551 \(goto-char (point-max)) is faster and avoids clobbering the mark."
553 (if (looking-at "[ \t]*\n") (expand-abbrev))
554 (setq this-command 'end-of-buffer)
557 (define-key mail-mode-map "\C-c\C-a" 'mail-interactive-insert-alias)
559 ;;(define-key mail-mode-map "\C-n" 'mail-abbrev-next-line)
560 ;;(define-key mail-mode-map "\M->" 'mail-abbrev-end-of-buffer)
562 (provide 'mailabbrev)